2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2013 QLogic Corporation
5 * See LICENSE.qlcnic for copyright and licensing details.
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/swab.h>
11 #include <linux/dma-mapping.h>
13 #include <linux/ipv6.h>
14 #include <linux/inetdevice.h>
15 #include <linux/sysfs.h>
16 #include <linux/aer.h>
17 #include <linux/log2.h>
18 #ifdef CONFIG_QLCNIC_HWMON
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
24 #include "qlcnic_hw.h"
26 int qlcnicvf_config_bridged_mode(struct qlcnic_adapter
*adapter
, u32 enable
)
31 int qlcnicvf_config_led(struct qlcnic_adapter
*adapter
, u32 state
, u32 rate
)
36 static ssize_t
qlcnic_store_bridged_mode(struct device
*dev
,
37 struct device_attribute
*attr
,
38 const char *buf
, size_t len
)
40 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
44 if (!(adapter
->ahw
->capabilities
& QLCNIC_FW_CAPABILITY_BDG
))
47 if (!test_bit(__QLCNIC_DEV_UP
, &adapter
->state
))
50 if (kstrtoul(buf
, 2, &new))
53 if (!qlcnic_config_bridged_mode(adapter
, !!new))
60 static ssize_t
qlcnic_show_bridged_mode(struct device
*dev
,
61 struct device_attribute
*attr
,
64 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
67 if (adapter
->ahw
->capabilities
& QLCNIC_FW_CAPABILITY_BDG
)
68 bridged_mode
= !!(adapter
->flags
& QLCNIC_BRIDGE_ENABLED
);
70 return sprintf(buf
, "%d\n", bridged_mode
);
73 static ssize_t
qlcnic_store_diag_mode(struct device
*dev
,
74 struct device_attribute
*attr
,
75 const char *buf
, size_t len
)
77 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
80 if (kstrtoul(buf
, 2, &new))
83 if (!!new != !!(adapter
->flags
& QLCNIC_DIAG_ENABLED
))
84 adapter
->flags
^= QLCNIC_DIAG_ENABLED
;
89 static ssize_t
qlcnic_show_diag_mode(struct device
*dev
,
90 struct device_attribute
*attr
, char *buf
)
92 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
93 return sprintf(buf
, "%d\n", !!(adapter
->flags
& QLCNIC_DIAG_ENABLED
));
96 static int qlcnic_validate_beacon(struct qlcnic_adapter
*adapter
, u16 beacon
,
100 *state
= MSB(beacon
);
102 QLCDB(adapter
, DRV
, "rate %x state %x\n", *rate
, *state
);
105 *rate
= __QLCNIC_MAX_LED_RATE
;
107 } else if (*state
> __QLCNIC_MAX_LED_STATE
) {
111 if ((!*rate
) || (*rate
> __QLCNIC_MAX_LED_RATE
))
117 static int qlcnic_83xx_store_beacon(struct qlcnic_adapter
*adapter
,
118 const char *buf
, size_t len
)
120 struct qlcnic_hardware_context
*ahw
= adapter
->ahw
;
121 unsigned long h_beacon
;
124 if (test_bit(__QLCNIC_RESETTING
, &adapter
->state
))
127 if (kstrtoul(buf
, 2, &h_beacon
))
130 qlcnic_get_beacon_state(adapter
);
132 if (ahw
->beacon_state
== h_beacon
)
136 if (!ahw
->beacon_state
) {
137 if (test_and_set_bit(__QLCNIC_LED_ENABLE
, &adapter
->state
)) {
144 err
= qlcnic_83xx_config_led(adapter
, 1, h_beacon
);
146 err
= qlcnic_83xx_config_led(adapter
, 0, !h_beacon
);
148 ahw
->beacon_state
= h_beacon
;
150 if (!ahw
->beacon_state
)
151 clear_bit(__QLCNIC_LED_ENABLE
, &adapter
->state
);
157 static int qlcnic_82xx_store_beacon(struct qlcnic_adapter
*adapter
,
158 const char *buf
, size_t len
)
160 struct qlcnic_hardware_context
*ahw
= adapter
->ahw
;
161 int err
, drv_sds_rings
= adapter
->drv_sds_rings
;
165 if (len
!= sizeof(u16
))
168 memcpy(&beacon
, buf
, sizeof(u16
));
169 err
= qlcnic_validate_beacon(adapter
, beacon
, &b_state
, &b_rate
);
173 qlcnic_get_beacon_state(adapter
);
175 if (ahw
->beacon_state
== b_state
)
179 if (!ahw
->beacon_state
) {
180 if (test_and_set_bit(__QLCNIC_LED_ENABLE
, &adapter
->state
)) {
186 if (test_bit(__QLCNIC_RESETTING
, &adapter
->state
)) {
191 if (!test_bit(__QLCNIC_DEV_UP
, &adapter
->state
)) {
192 err
= qlcnic_diag_alloc_res(adapter
->netdev
, QLCNIC_LED_TEST
);
195 set_bit(__QLCNIC_DIAG_RES_ALLOC
, &adapter
->state
);
198 err
= qlcnic_config_led(adapter
, b_state
, b_rate
);
201 ahw
->beacon_state
= b_state
;
204 if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC
, &adapter
->state
))
205 qlcnic_diag_free_res(adapter
->netdev
, drv_sds_rings
);
208 if (!ahw
->beacon_state
)
209 clear_bit(__QLCNIC_LED_ENABLE
, &adapter
->state
);
215 static ssize_t
qlcnic_store_beacon(struct device
*dev
,
216 struct device_attribute
*attr
,
217 const char *buf
, size_t len
)
219 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
222 if (adapter
->ahw
->op_mode
== QLCNIC_NON_PRIV_FUNC
) {
224 "LED test not supported in non privileged mode\n");
228 if (qlcnic_82xx_check(adapter
))
229 err
= qlcnic_82xx_store_beacon(adapter
, buf
, len
);
230 else if (qlcnic_83xx_check(adapter
))
231 err
= qlcnic_83xx_store_beacon(adapter
, buf
, len
);
238 static ssize_t
qlcnic_show_beacon(struct device
*dev
,
239 struct device_attribute
*attr
, char *buf
)
241 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
243 return sprintf(buf
, "%d\n", adapter
->ahw
->beacon_state
);
246 static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter
*adapter
,
247 loff_t offset
, size_t size
)
251 if (!(adapter
->flags
& QLCNIC_DIAG_ENABLED
))
254 if (offset
< QLCNIC_PCI_CRBSPACE
) {
255 if (ADDR_IN_RANGE(offset
, QLCNIC_PCI_CAMQM
,
256 QLCNIC_PCI_CAMQM_END
))
262 if ((size
!= crb_size
) || (offset
& (crb_size
-1)))
268 static ssize_t
qlcnic_sysfs_read_crb(struct file
*filp
, struct kobject
*kobj
,
269 struct bin_attribute
*attr
, char *buf
,
270 loff_t offset
, size_t size
)
272 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
273 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
276 ret
= qlcnic_sysfs_validate_crb(adapter
, offset
, size
);
279 qlcnic_read_crb(adapter
, buf
, offset
, size
);
280 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
285 static ssize_t
qlcnic_sysfs_write_crb(struct file
*filp
, struct kobject
*kobj
,
286 struct bin_attribute
*attr
, char *buf
,
287 loff_t offset
, size_t size
)
289 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
290 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
293 ret
= qlcnic_sysfs_validate_crb(adapter
, offset
, size
);
297 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
298 qlcnic_write_crb(adapter
, buf
, offset
, size
);
302 static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter
*adapter
,
303 loff_t offset
, size_t size
)
305 if (!(adapter
->flags
& QLCNIC_DIAG_ENABLED
))
308 if ((size
!= 8) || (offset
& 0x7))
314 static ssize_t
qlcnic_sysfs_read_mem(struct file
*filp
, struct kobject
*kobj
,
315 struct bin_attribute
*attr
, char *buf
,
316 loff_t offset
, size_t size
)
318 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
319 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
323 ret
= qlcnic_sysfs_validate_mem(adapter
, offset
, size
);
327 if (qlcnic_pci_mem_read_2M(adapter
, offset
, &data
))
330 memcpy(buf
, &data
, size
);
331 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
336 static ssize_t
qlcnic_sysfs_write_mem(struct file
*filp
, struct kobject
*kobj
,
337 struct bin_attribute
*attr
, char *buf
,
338 loff_t offset
, size_t size
)
340 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
341 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
345 ret
= qlcnic_sysfs_validate_mem(adapter
, offset
, size
);
349 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
350 memcpy(&data
, buf
, size
);
352 if (qlcnic_pci_mem_write_2M(adapter
, offset
, data
))
358 int qlcnic_is_valid_nic_func(struct qlcnic_adapter
*adapter
, u8 pci_func
)
362 for (i
= 0; i
< adapter
->ahw
->total_nic_func
; i
++) {
363 if (adapter
->npars
[i
].pci_func
== pci_func
)
367 dev_err(&adapter
->pdev
->dev
, "%s: Invalid nic function\n", __func__
);
371 static int validate_pm_config(struct qlcnic_adapter
*adapter
,
372 struct qlcnic_pm_func_cfg
*pm_cfg
, int count
)
374 u8 src_pci_func
, s_esw_id
, d_esw_id
;
376 int i
, src_index
, dest_index
;
378 for (i
= 0; i
< count
; i
++) {
379 src_pci_func
= pm_cfg
[i
].pci_func
;
380 dest_pci_func
= pm_cfg
[i
].dest_npar
;
381 src_index
= qlcnic_is_valid_nic_func(adapter
, src_pci_func
);
385 dest_index
= qlcnic_is_valid_nic_func(adapter
, dest_pci_func
);
389 s_esw_id
= adapter
->npars
[src_index
].phy_port
;
390 d_esw_id
= adapter
->npars
[dest_index
].phy_port
;
392 if (s_esw_id
!= d_esw_id
)
399 static ssize_t
qlcnic_sysfs_write_pm_config(struct file
*filp
,
400 struct kobject
*kobj
,
401 struct bin_attribute
*attr
,
402 char *buf
, loff_t offset
,
405 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
406 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
407 struct qlcnic_pm_func_cfg
*pm_cfg
;
408 u32 id
, action
, pci_func
;
409 int count
, rem
, i
, ret
, index
;
411 count
= size
/ sizeof(struct qlcnic_pm_func_cfg
);
412 rem
= size
% sizeof(struct qlcnic_pm_func_cfg
);
416 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
417 pm_cfg
= (struct qlcnic_pm_func_cfg
*)buf
;
418 ret
= validate_pm_config(adapter
, pm_cfg
, count
);
422 for (i
= 0; i
< count
; i
++) {
423 pci_func
= pm_cfg
[i
].pci_func
;
424 action
= !!pm_cfg
[i
].action
;
425 index
= qlcnic_is_valid_nic_func(adapter
, pci_func
);
429 id
= adapter
->npars
[index
].phy_port
;
430 ret
= qlcnic_config_port_mirroring(adapter
, id
,
436 for (i
= 0; i
< count
; i
++) {
437 pci_func
= pm_cfg
[i
].pci_func
;
438 index
= qlcnic_is_valid_nic_func(adapter
, pci_func
);
441 id
= adapter
->npars
[index
].phy_port
;
442 adapter
->npars
[index
].enable_pm
= !!pm_cfg
[i
].action
;
443 adapter
->npars
[index
].dest_npar
= id
;
449 static ssize_t
qlcnic_sysfs_read_pm_config(struct file
*filp
,
450 struct kobject
*kobj
,
451 struct bin_attribute
*attr
,
452 char *buf
, loff_t offset
,
455 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
456 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
457 struct qlcnic_pm_func_cfg
*pm_cfg
;
462 memset(buf
, 0, size
);
463 pm_cfg
= (struct qlcnic_pm_func_cfg
*)buf
;
464 count
= size
/ sizeof(struct qlcnic_pm_func_cfg
);
465 for (i
= 0; i
< adapter
->ahw
->total_nic_func
; i
++) {
466 pci_func
= adapter
->npars
[i
].pci_func
;
467 if (pci_func
>= count
) {
468 dev_dbg(dev
, "%s: Total nic functions[%d], App sent function count[%d]\n",
469 __func__
, adapter
->ahw
->total_nic_func
, count
);
472 if (!adapter
->npars
[i
].eswitch_status
)
475 pm_cfg
[pci_func
].action
= adapter
->npars
[i
].enable_pm
;
476 pm_cfg
[pci_func
].dest_npar
= 0;
477 pm_cfg
[pci_func
].pci_func
= i
;
479 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
483 static int validate_esw_config(struct qlcnic_adapter
*adapter
,
484 struct qlcnic_esw_func_cfg
*esw_cfg
, int count
)
486 struct qlcnic_hardware_context
*ahw
= adapter
->ahw
;
491 if (qlcnic_82xx_check(adapter
))
492 op_mode
= readl(ahw
->pci_base0
+ QLCNIC_DRV_OP_MODE
);
494 op_mode
= QLCRDX(ahw
, QLC_83XX_DRV_OP_MODE
);
496 for (i
= 0; i
< count
; i
++) {
497 pci_func
= esw_cfg
[i
].pci_func
;
498 if (pci_func
>= ahw
->max_vnic_func
)
501 if (adapter
->ahw
->op_mode
== QLCNIC_MGMT_FUNC
)
502 if (qlcnic_is_valid_nic_func(adapter
, pci_func
) < 0)
505 switch (esw_cfg
[i
].op_mode
) {
506 case QLCNIC_PORT_DEFAULTS
:
507 if (qlcnic_82xx_check(adapter
)) {
508 ret
= QLC_DEV_GET_DRV(op_mode
, pci_func
);
510 ret
= QLC_83XX_GET_FUNC_PRIVILEGE(op_mode
,
512 esw_cfg
[i
].offload_flags
= 0;
515 if (ret
!= QLCNIC_NON_PRIV_FUNC
) {
516 if (esw_cfg
[i
].mac_anti_spoof
!= 0)
518 if (esw_cfg
[i
].mac_override
!= 1)
520 if (esw_cfg
[i
].promisc_mode
!= 1)
524 case QLCNIC_ADD_VLAN
:
525 if (!IS_VALID_VLAN(esw_cfg
[i
].vlan_id
))
527 if (!esw_cfg
[i
].op_type
)
530 case QLCNIC_DEL_VLAN
:
531 if (!esw_cfg
[i
].op_type
)
542 static ssize_t
qlcnic_sysfs_write_esw_config(struct file
*file
,
543 struct kobject
*kobj
,
544 struct bin_attribute
*attr
,
545 char *buf
, loff_t offset
,
548 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
549 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
550 struct qlcnic_esw_func_cfg
*esw_cfg
;
551 struct qlcnic_npar_info
*npar
;
552 int count
, rem
, i
, ret
;
554 u8 op_mode
= 0, pci_func
;
556 count
= size
/ sizeof(struct qlcnic_esw_func_cfg
);
557 rem
= size
% sizeof(struct qlcnic_esw_func_cfg
);
561 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
562 esw_cfg
= (struct qlcnic_esw_func_cfg
*)buf
;
563 ret
= validate_esw_config(adapter
, esw_cfg
, count
);
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
]))
572 if (adapter
->ahw
->pci_func
!= esw_cfg
[i
].pci_func
)
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
;
580 switch (esw_cfg
[i
].op_mode
) {
581 case QLCNIC_PORT_DEFAULTS
:
582 qlcnic_set_eswitch_port_features(adapter
, &esw_cfg
[i
]);
584 qlcnic_set_netdev_features(adapter
, &esw_cfg
[i
]);
587 case QLCNIC_ADD_VLAN
:
588 qlcnic_set_vlan_config(adapter
, &esw_cfg
[i
]);
590 case QLCNIC_DEL_VLAN
:
591 esw_cfg
[i
].vlan_id
= 0;
592 qlcnic_set_vlan_config(adapter
, &esw_cfg
[i
]);
597 if (adapter
->ahw
->op_mode
!= QLCNIC_MGMT_FUNC
)
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
);
605 npar
= &adapter
->npars
[index
];
606 switch (esw_cfg
[i
].op_mode
) {
607 case QLCNIC_PORT_DEFAULTS
:
608 npar
->promisc_mode
= esw_cfg
[i
].promisc_mode
;
609 npar
->mac_override
= esw_cfg
[i
].mac_override
;
610 npar
->offload_flags
= esw_cfg
[i
].offload_flags
;
611 npar
->mac_anti_spoof
= esw_cfg
[i
].mac_anti_spoof
;
612 npar
->discard_tagged
= esw_cfg
[i
].discard_tagged
;
614 case QLCNIC_ADD_VLAN
:
615 npar
->pvid
= esw_cfg
[i
].vlan_id
;
617 case QLCNIC_DEL_VLAN
:
626 static ssize_t
qlcnic_sysfs_read_esw_config(struct file
*file
,
627 struct kobject
*kobj
,
628 struct bin_attribute
*attr
,
629 char *buf
, loff_t offset
,
632 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
633 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
634 struct qlcnic_esw_func_cfg
*esw_cfg
;
639 memset(buf
, 0, size
);
640 esw_cfg
= (struct qlcnic_esw_func_cfg
*)buf
;
641 count
= size
/ sizeof(struct qlcnic_esw_func_cfg
);
642 for (i
= 0; i
< adapter
->ahw
->total_nic_func
; i
++) {
643 pci_func
= adapter
->npars
[i
].pci_func
;
644 if (pci_func
>= count
) {
645 dev_dbg(dev
, "%s: Total nic functions[%d], App sent function count[%d]\n",
646 __func__
, adapter
->ahw
->total_nic_func
, count
);
649 if (!adapter
->npars
[i
].eswitch_status
)
652 esw_cfg
[pci_func
].pci_func
= pci_func
;
653 if (qlcnic_get_eswitch_port_config(adapter
, &esw_cfg
[pci_func
]))
656 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
660 static int validate_npar_config(struct qlcnic_adapter
*adapter
,
661 struct qlcnic_npar_func_cfg
*np_cfg
,
666 for (i
= 0; i
< count
; i
++) {
667 pci_func
= np_cfg
[i
].pci_func
;
668 if (qlcnic_is_valid_nic_func(adapter
, pci_func
) < 0)
671 if (!IS_VALID_BW(np_cfg
[i
].min_bw
) ||
672 !IS_VALID_BW(np_cfg
[i
].max_bw
))
678 static ssize_t
qlcnic_sysfs_write_npar_config(struct file
*file
,
679 struct kobject
*kobj
,
680 struct bin_attribute
*attr
,
681 char *buf
, loff_t offset
,
684 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
685 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
686 struct qlcnic_info nic_info
;
687 struct qlcnic_npar_func_cfg
*np_cfg
;
688 int i
, count
, rem
, ret
, index
;
691 count
= size
/ sizeof(struct qlcnic_npar_func_cfg
);
692 rem
= size
% sizeof(struct qlcnic_npar_func_cfg
);
696 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
697 np_cfg
= (struct qlcnic_npar_func_cfg
*)buf
;
698 ret
= validate_npar_config(adapter
, np_cfg
, count
);
702 for (i
= 0; i
< count
; i
++) {
703 pci_func
= np_cfg
[i
].pci_func
;
705 memset(&nic_info
, 0, sizeof(struct qlcnic_info
));
706 ret
= qlcnic_get_nic_info(adapter
, &nic_info
, pci_func
);
709 nic_info
.pci_func
= pci_func
;
710 nic_info
.min_tx_bw
= np_cfg
[i
].min_bw
;
711 nic_info
.max_tx_bw
= np_cfg
[i
].max_bw
;
712 ret
= qlcnic_set_nic_info(adapter
, &nic_info
);
715 index
= qlcnic_is_valid_nic_func(adapter
, pci_func
);
718 adapter
->npars
[index
].min_bw
= nic_info
.min_tx_bw
;
719 adapter
->npars
[index
].max_bw
= nic_info
.max_tx_bw
;
725 static ssize_t
qlcnic_sysfs_read_npar_config(struct file
*file
,
726 struct kobject
*kobj
,
727 struct bin_attribute
*attr
,
728 char *buf
, loff_t offset
,
731 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
732 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
733 struct qlcnic_npar_func_cfg
*np_cfg
;
734 struct qlcnic_info nic_info
;
739 memset(&nic_info
, 0, sizeof(struct qlcnic_info
));
740 memset(buf
, 0, size
);
741 np_cfg
= (struct qlcnic_npar_func_cfg
*)buf
;
743 count
= size
/ sizeof(struct qlcnic_npar_func_cfg
);
744 for (i
= 0; i
< adapter
->ahw
->total_nic_func
; i
++) {
745 if (adapter
->npars
[i
].pci_func
>= count
) {
746 dev_dbg(dev
, "%s: Total nic functions[%d], App sent function count[%d]\n",
747 __func__
, adapter
->ahw
->total_nic_func
, count
);
750 if (!adapter
->npars
[i
].eswitch_status
)
752 pci_func
= adapter
->npars
[i
].pci_func
;
753 if (qlcnic_is_valid_nic_func(adapter
, pci_func
) < 0)
755 ret
= qlcnic_get_nic_info(adapter
, &nic_info
, pci_func
);
759 np_cfg
[pci_func
].pci_func
= pci_func
;
760 np_cfg
[pci_func
].op_mode
= (u8
)nic_info
.op_mode
;
761 np_cfg
[pci_func
].port_num
= nic_info
.phys_port
;
762 np_cfg
[pci_func
].fw_capab
= nic_info
.capabilities
;
763 np_cfg
[pci_func
].min_bw
= nic_info
.min_tx_bw
;
764 np_cfg
[pci_func
].max_bw
= nic_info
.max_tx_bw
;
765 np_cfg
[pci_func
].max_tx_queues
= nic_info
.max_tx_ques
;
766 np_cfg
[pci_func
].max_rx_queues
= nic_info
.max_rx_ques
;
768 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
772 static ssize_t
qlcnic_sysfs_get_port_stats(struct file
*file
,
773 struct kobject
*kobj
,
774 struct bin_attribute
*attr
,
775 char *buf
, loff_t offset
,
778 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
779 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
780 struct qlcnic_esw_statistics port_stats
;
783 if (qlcnic_83xx_check(adapter
))
786 if (size
!= sizeof(struct qlcnic_esw_statistics
))
789 if (offset
>= adapter
->ahw
->max_vnic_func
)
792 memset(&port_stats
, 0, size
);
793 ret
= qlcnic_get_port_stats(adapter
, offset
, QLCNIC_QUERY_RX_COUNTER
,
798 ret
= qlcnic_get_port_stats(adapter
, offset
, QLCNIC_QUERY_TX_COUNTER
,
803 memcpy(buf
, &port_stats
, size
);
807 static ssize_t
qlcnic_sysfs_get_esw_stats(struct file
*file
,
808 struct kobject
*kobj
,
809 struct bin_attribute
*attr
,
810 char *buf
, loff_t offset
,
813 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
814 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
815 struct qlcnic_esw_statistics esw_stats
;
818 if (qlcnic_83xx_check(adapter
))
821 if (size
!= sizeof(struct qlcnic_esw_statistics
))
824 if (offset
>= QLCNIC_NIU_MAX_XG_PORTS
)
827 memset(&esw_stats
, 0, size
);
828 ret
= qlcnic_get_eswitch_stats(adapter
, offset
, QLCNIC_QUERY_RX_COUNTER
,
833 ret
= qlcnic_get_eswitch_stats(adapter
, offset
, QLCNIC_QUERY_TX_COUNTER
,
838 memcpy(buf
, &esw_stats
, size
);
842 static ssize_t
qlcnic_sysfs_clear_esw_stats(struct file
*file
,
843 struct kobject
*kobj
,
844 struct bin_attribute
*attr
,
845 char *buf
, loff_t offset
,
848 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
849 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
852 if (qlcnic_83xx_check(adapter
))
855 if (offset
>= QLCNIC_NIU_MAX_XG_PORTS
)
858 ret
= qlcnic_clear_esw_stats(adapter
, QLCNIC_STATS_ESWITCH
, offset
,
859 QLCNIC_QUERY_RX_COUNTER
);
863 ret
= qlcnic_clear_esw_stats(adapter
, QLCNIC_STATS_ESWITCH
, offset
,
864 QLCNIC_QUERY_TX_COUNTER
);
871 static ssize_t
qlcnic_sysfs_clear_port_stats(struct file
*file
,
872 struct kobject
*kobj
,
873 struct bin_attribute
*attr
,
874 char *buf
, loff_t offset
,
878 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
879 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
882 if (qlcnic_83xx_check(adapter
))
885 if (offset
>= adapter
->ahw
->max_vnic_func
)
888 ret
= qlcnic_clear_esw_stats(adapter
, QLCNIC_STATS_PORT
, offset
,
889 QLCNIC_QUERY_RX_COUNTER
);
893 ret
= qlcnic_clear_esw_stats(adapter
, QLCNIC_STATS_PORT
, offset
,
894 QLCNIC_QUERY_TX_COUNTER
);
901 static ssize_t
qlcnic_sysfs_read_pci_config(struct file
*file
,
902 struct kobject
*kobj
,
903 struct bin_attribute
*attr
,
904 char *buf
, loff_t offset
,
907 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
908 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
909 struct qlcnic_pci_func_cfg
*pci_cfg
;
910 struct qlcnic_pci_info
*pci_info
;
914 pci_info
= kcalloc(size
, sizeof(*pci_info
), GFP_KERNEL
);
918 ret
= qlcnic_get_pci_info(adapter
, pci_info
);
924 pci_cfg
= (struct qlcnic_pci_func_cfg
*)buf
;
925 count
= size
/ sizeof(struct qlcnic_pci_func_cfg
);
926 qlcnic_swap32_buffer((u32
*)pci_info
, size
/ sizeof(u32
));
927 for (i
= 0; i
< count
; i
++) {
928 pci_cfg
[i
].pci_func
= pci_info
[i
].id
;
929 pci_cfg
[i
].func_type
= pci_info
[i
].type
;
930 pci_cfg
[i
].func_state
= 0;
931 pci_cfg
[i
].port_num
= pci_info
[i
].default_port
;
932 pci_cfg
[i
].min_bw
= pci_info
[i
].tx_min_bw
;
933 pci_cfg
[i
].max_bw
= pci_info
[i
].tx_max_bw
;
934 memcpy(&pci_cfg
[i
].def_mac_addr
, &pci_info
[i
].mac
, ETH_ALEN
);
941 static ssize_t
qlcnic_83xx_sysfs_flash_read_handler(struct file
*filp
,
942 struct kobject
*kobj
,
943 struct bin_attribute
*attr
,
944 char *buf
, loff_t offset
,
947 unsigned char *p_read_buf
;
949 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
950 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
955 count
= size
/ sizeof(u32
);
957 if (size
% sizeof(u32
))
960 p_read_buf
= kcalloc(size
, sizeof(unsigned char), GFP_KERNEL
);
963 if (qlcnic_83xx_lock_flash(adapter
) != 0) {
968 ret
= qlcnic_83xx_lockless_flash_read32(adapter
, offset
, p_read_buf
,
972 qlcnic_83xx_unlock_flash(adapter
);
977 qlcnic_83xx_unlock_flash(adapter
);
978 qlcnic_swap32_buffer((u32
*)p_read_buf
, count
);
979 memcpy(buf
, p_read_buf
, size
);
985 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter
*adapter
,
986 char *buf
, loff_t offset
,
990 unsigned char *p_cache
, *p_src
;
992 p_cache
= kcalloc(size
, sizeof(unsigned char), GFP_KERNEL
);
996 count
= size
/ sizeof(u32
);
997 qlcnic_swap32_buffer((u32
*)buf
, count
);
998 memcpy(p_cache
, buf
, size
);
1001 if (qlcnic_83xx_lock_flash(adapter
) != 0) {
1006 if (adapter
->ahw
->fdt
.mfg_id
== adapter
->flash_mfg_id
) {
1007 ret
= qlcnic_83xx_enable_flash_write(adapter
);
1010 qlcnic_83xx_unlock_flash(adapter
);
1015 for (i
= 0; i
< count
/ QLC_83XX_FLASH_WRITE_MAX
; i
++) {
1016 ret
= qlcnic_83xx_flash_bulk_write(adapter
, offset
,
1018 QLC_83XX_FLASH_WRITE_MAX
);
1021 if (adapter
->ahw
->fdt
.mfg_id
== adapter
->flash_mfg_id
) {
1022 ret
= qlcnic_83xx_disable_flash_write(adapter
);
1025 qlcnic_83xx_unlock_flash(adapter
);
1031 qlcnic_83xx_unlock_flash(adapter
);
1035 p_src
= p_src
+ sizeof(u32
)*QLC_83XX_FLASH_WRITE_MAX
;
1036 offset
= offset
+ sizeof(u32
)*QLC_83XX_FLASH_WRITE_MAX
;
1039 if (adapter
->ahw
->fdt
.mfg_id
== adapter
->flash_mfg_id
) {
1040 ret
= qlcnic_83xx_disable_flash_write(adapter
);
1043 qlcnic_83xx_unlock_flash(adapter
);
1049 qlcnic_83xx_unlock_flash(adapter
);
1054 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter
*adapter
,
1055 char *buf
, loff_t offset
, size_t size
)
1058 unsigned char *p_cache
, *p_src
;
1060 p_cache
= kcalloc(size
, sizeof(unsigned char), GFP_KERNEL
);
1064 qlcnic_swap32_buffer((u32
*)buf
, size
/ sizeof(u32
));
1065 memcpy(p_cache
, buf
, size
);
1067 count
= size
/ sizeof(u32
);
1069 if (qlcnic_83xx_lock_flash(adapter
) != 0) {
1074 if (adapter
->ahw
->fdt
.mfg_id
== adapter
->flash_mfg_id
) {
1075 ret
= qlcnic_83xx_enable_flash_write(adapter
);
1078 qlcnic_83xx_unlock_flash(adapter
);
1083 for (i
= 0; i
< count
; i
++) {
1084 ret
= qlcnic_83xx_flash_write32(adapter
, offset
, (u32
*)p_src
);
1086 if (adapter
->ahw
->fdt
.mfg_id
== adapter
->flash_mfg_id
) {
1087 ret
= qlcnic_83xx_disable_flash_write(adapter
);
1090 qlcnic_83xx_unlock_flash(adapter
);
1095 qlcnic_83xx_unlock_flash(adapter
);
1099 p_src
= p_src
+ sizeof(u32
);
1100 offset
= offset
+ sizeof(u32
);
1103 if (adapter
->ahw
->fdt
.mfg_id
== adapter
->flash_mfg_id
) {
1104 ret
= qlcnic_83xx_disable_flash_write(adapter
);
1107 qlcnic_83xx_unlock_flash(adapter
);
1113 qlcnic_83xx_unlock_flash(adapter
);
1118 static ssize_t
qlcnic_83xx_sysfs_flash_write_handler(struct file
*filp
,
1119 struct kobject
*kobj
,
1120 struct bin_attribute
*attr
,
1121 char *buf
, loff_t offset
,
1125 static int flash_mode
;
1127 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1128 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
1130 ret
= kstrtoul(buf
, 16, &data
);
1133 case QLC_83XX_FLASH_SECTOR_ERASE_CMD
:
1134 flash_mode
= QLC_83XX_ERASE_MODE
;
1135 ret
= qlcnic_83xx_erase_flash_sector(adapter
, offset
);
1137 dev_err(&adapter
->pdev
->dev
,
1138 "%s failed at %d\n", __func__
, __LINE__
);
1143 case QLC_83XX_FLASH_BULK_WRITE_CMD
:
1144 flash_mode
= QLC_83XX_BULK_WRITE_MODE
;
1147 case QLC_83XX_FLASH_WRITE_CMD
:
1148 flash_mode
= QLC_83XX_WRITE_MODE
;
1151 if (flash_mode
== QLC_83XX_BULK_WRITE_MODE
) {
1152 ret
= qlcnic_83xx_sysfs_flash_bulk_write(adapter
, buf
,
1155 dev_err(&adapter
->pdev
->dev
,
1156 "%s failed at %d\n",
1157 __func__
, __LINE__
);
1162 if (flash_mode
== QLC_83XX_WRITE_MODE
) {
1163 ret
= qlcnic_83xx_sysfs_flash_write(adapter
, buf
,
1166 dev_err(&adapter
->pdev
->dev
,
1167 "%s failed at %d\n", __func__
,
1177 static const struct device_attribute dev_attr_bridged_mode
= {
1178 .attr
= {.name
= "bridged_mode", .mode
= (S_IRUGO
| S_IWUSR
)},
1179 .show
= qlcnic_show_bridged_mode
,
1180 .store
= qlcnic_store_bridged_mode
,
1183 static const struct device_attribute dev_attr_diag_mode
= {
1184 .attr
= {.name
= "diag_mode", .mode
= (S_IRUGO
| S_IWUSR
)},
1185 .show
= qlcnic_show_diag_mode
,
1186 .store
= qlcnic_store_diag_mode
,
1189 static const struct device_attribute dev_attr_beacon
= {
1190 .attr
= {.name
= "beacon", .mode
= (S_IRUGO
| S_IWUSR
)},
1191 .show
= qlcnic_show_beacon
,
1192 .store
= qlcnic_store_beacon
,
1195 static const struct bin_attribute bin_attr_crb
= {
1196 .attr
= {.name
= "crb", .mode
= (S_IRUGO
| S_IWUSR
)},
1198 .read
= qlcnic_sysfs_read_crb
,
1199 .write
= qlcnic_sysfs_write_crb
,
1202 static const struct bin_attribute bin_attr_mem
= {
1203 .attr
= {.name
= "mem", .mode
= (S_IRUGO
| S_IWUSR
)},
1205 .read
= qlcnic_sysfs_read_mem
,
1206 .write
= qlcnic_sysfs_write_mem
,
1209 static const struct bin_attribute bin_attr_npar_config
= {
1210 .attr
= {.name
= "npar_config", .mode
= (S_IRUGO
| S_IWUSR
)},
1212 .read
= qlcnic_sysfs_read_npar_config
,
1213 .write
= qlcnic_sysfs_write_npar_config
,
1216 static const struct bin_attribute bin_attr_pci_config
= {
1217 .attr
= {.name
= "pci_config", .mode
= (S_IRUGO
| S_IWUSR
)},
1219 .read
= qlcnic_sysfs_read_pci_config
,
1223 static const struct bin_attribute bin_attr_port_stats
= {
1224 .attr
= {.name
= "port_stats", .mode
= (S_IRUGO
| S_IWUSR
)},
1226 .read
= qlcnic_sysfs_get_port_stats
,
1227 .write
= qlcnic_sysfs_clear_port_stats
,
1230 static const struct bin_attribute bin_attr_esw_stats
= {
1231 .attr
= {.name
= "esw_stats", .mode
= (S_IRUGO
| S_IWUSR
)},
1233 .read
= qlcnic_sysfs_get_esw_stats
,
1234 .write
= qlcnic_sysfs_clear_esw_stats
,
1237 static const struct bin_attribute bin_attr_esw_config
= {
1238 .attr
= {.name
= "esw_config", .mode
= (S_IRUGO
| S_IWUSR
)},
1240 .read
= qlcnic_sysfs_read_esw_config
,
1241 .write
= qlcnic_sysfs_write_esw_config
,
1244 static const struct bin_attribute bin_attr_pm_config
= {
1245 .attr
= {.name
= "pm_config", .mode
= (S_IRUGO
| S_IWUSR
)},
1247 .read
= qlcnic_sysfs_read_pm_config
,
1248 .write
= qlcnic_sysfs_write_pm_config
,
1251 static const struct bin_attribute bin_attr_flash
= {
1252 .attr
= {.name
= "flash", .mode
= (S_IRUGO
| S_IWUSR
)},
1254 .read
= qlcnic_83xx_sysfs_flash_read_handler
,
1255 .write
= qlcnic_83xx_sysfs_flash_write_handler
,
1258 #ifdef CONFIG_QLCNIC_HWMON
1260 static ssize_t
qlcnic_hwmon_show_temp(struct device
*dev
,
1261 struct device_attribute
*dev_attr
,
1264 struct qlcnic_adapter
*adapter
= dev_get_drvdata(dev
);
1265 unsigned int temperature
= 0, value
= 0;
1267 if (qlcnic_83xx_check(adapter
))
1268 value
= QLCRDX(adapter
->ahw
, QLC_83XX_ASIC_TEMP
);
1269 else if (qlcnic_82xx_check(adapter
))
1270 value
= QLC_SHARED_REG_RD32(adapter
, QLCNIC_ASIC_TEMP
);
1272 temperature
= qlcnic_get_temp_val(value
);
1273 /* display millidegree celcius */
1274 temperature
*= 1000;
1275 return sprintf(buf
, "%u\n", temperature
);
1278 /* hwmon-sysfs attributes */
1279 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
,
1280 qlcnic_hwmon_show_temp
, NULL
, 1);
1282 static struct attribute
*qlcnic_hwmon_attrs
[] = {
1283 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1287 ATTRIBUTE_GROUPS(qlcnic_hwmon
);
1289 void qlcnic_register_hwmon_dev(struct qlcnic_adapter
*adapter
)
1291 struct device
*dev
= &adapter
->pdev
->dev
;
1292 struct device
*hwmon_dev
;
1294 /* Skip hwmon registration for a VF device */
1295 if (qlcnic_sriov_vf_check(adapter
)) {
1296 adapter
->ahw
->hwmon_dev
= NULL
;
1299 hwmon_dev
= hwmon_device_register_with_groups(dev
, qlcnic_driver_name
,
1301 qlcnic_hwmon_groups
);
1302 if (IS_ERR(hwmon_dev
)) {
1303 dev_err(dev
, "Cannot register with hwmon, err=%ld\n",
1304 PTR_ERR(hwmon_dev
));
1307 adapter
->ahw
->hwmon_dev
= hwmon_dev
;
1310 void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter
*adapter
)
1312 struct device
*hwmon_dev
= adapter
->ahw
->hwmon_dev
;
1314 hwmon_device_unregister(hwmon_dev
);
1315 adapter
->ahw
->hwmon_dev
= NULL
;
1320 void qlcnic_create_sysfs_entries(struct qlcnic_adapter
*adapter
)
1322 struct device
*dev
= &adapter
->pdev
->dev
;
1324 if (adapter
->ahw
->capabilities
& QLCNIC_FW_CAPABILITY_BDG
)
1325 if (device_create_file(dev
, &dev_attr_bridged_mode
))
1327 "failed to create bridged_mode sysfs entry\n");
1330 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter
*adapter
)
1332 struct device
*dev
= &adapter
->pdev
->dev
;
1334 if (adapter
->ahw
->capabilities
& QLCNIC_FW_CAPABILITY_BDG
)
1335 device_remove_file(dev
, &dev_attr_bridged_mode
);
1338 static void qlcnic_create_diag_entries(struct qlcnic_adapter
*adapter
)
1340 struct device
*dev
= &adapter
->pdev
->dev
;
1342 if (device_create_bin_file(dev
, &bin_attr_port_stats
))
1343 dev_info(dev
, "failed to create port stats sysfs entry");
1345 if (adapter
->ahw
->op_mode
== QLCNIC_NON_PRIV_FUNC
)
1347 if (device_create_file(dev
, &dev_attr_diag_mode
))
1348 dev_info(dev
, "failed to create diag_mode sysfs entry\n");
1349 if (device_create_bin_file(dev
, &bin_attr_crb
))
1350 dev_info(dev
, "failed to create crb sysfs entry\n");
1351 if (device_create_bin_file(dev
, &bin_attr_mem
))
1352 dev_info(dev
, "failed to create mem sysfs entry\n");
1354 if (test_bit(__QLCNIC_MAINTENANCE_MODE
, &adapter
->state
))
1357 if (device_create_bin_file(dev
, &bin_attr_pci_config
))
1358 dev_info(dev
, "failed to create pci config sysfs entry");
1360 if (device_create_file(dev
, &dev_attr_beacon
))
1361 dev_info(dev
, "failed to create beacon sysfs entry");
1363 if (!(adapter
->flags
& QLCNIC_ESWITCH_ENABLED
))
1365 if (device_create_bin_file(dev
, &bin_attr_esw_config
))
1366 dev_info(dev
, "failed to create esw config sysfs entry");
1367 if (adapter
->ahw
->op_mode
!= QLCNIC_MGMT_FUNC
)
1369 if (device_create_bin_file(dev
, &bin_attr_npar_config
))
1370 dev_info(dev
, "failed to create npar config sysfs entry");
1371 if (device_create_bin_file(dev
, &bin_attr_pm_config
))
1372 dev_info(dev
, "failed to create pm config sysfs entry");
1373 if (device_create_bin_file(dev
, &bin_attr_esw_stats
))
1374 dev_info(dev
, "failed to create eswitch stats sysfs entry");
1377 static void qlcnic_remove_diag_entries(struct qlcnic_adapter
*adapter
)
1379 struct device
*dev
= &adapter
->pdev
->dev
;
1381 device_remove_bin_file(dev
, &bin_attr_port_stats
);
1383 if (adapter
->ahw
->op_mode
== QLCNIC_NON_PRIV_FUNC
)
1385 device_remove_file(dev
, &dev_attr_diag_mode
);
1386 device_remove_bin_file(dev
, &bin_attr_crb
);
1387 device_remove_bin_file(dev
, &bin_attr_mem
);
1389 if (test_bit(__QLCNIC_MAINTENANCE_MODE
, &adapter
->state
))
1392 device_remove_bin_file(dev
, &bin_attr_pci_config
);
1393 device_remove_file(dev
, &dev_attr_beacon
);
1394 if (!(adapter
->flags
& QLCNIC_ESWITCH_ENABLED
))
1396 device_remove_bin_file(dev
, &bin_attr_esw_config
);
1397 if (adapter
->ahw
->op_mode
!= QLCNIC_MGMT_FUNC
)
1399 device_remove_bin_file(dev
, &bin_attr_npar_config
);
1400 device_remove_bin_file(dev
, &bin_attr_pm_config
);
1401 device_remove_bin_file(dev
, &bin_attr_esw_stats
);
1404 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter
*adapter
)
1406 qlcnic_create_diag_entries(adapter
);
1409 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter
*adapter
)
1411 qlcnic_remove_diag_entries(adapter
);
1414 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter
*adapter
)
1416 struct device
*dev
= &adapter
->pdev
->dev
;
1418 qlcnic_create_diag_entries(adapter
);
1420 if (sysfs_create_bin_file(&dev
->kobj
, &bin_attr_flash
))
1421 dev_info(dev
, "failed to create flash sysfs entry\n");
1424 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter
*adapter
)
1426 struct device
*dev
= &adapter
->pdev
->dev
;
1428 qlcnic_remove_diag_entries(adapter
);
1429 sysfs_remove_bin_file(&dev
->kobj
, &bin_attr_flash
);