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>
12 #include "qlcnic_hw.h"
14 #include <linux/swab.h>
15 #include <linux/dma-mapping.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>
27 #define QLC_STATUS_UNSUPPORTED_CMD -2
29 int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
34 int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
39 static ssize_t qlcnic_store_bridged_mode(struct device *dev,
40 struct device_attribute *attr,
41 const char *buf, size_t len)
43 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
47 if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
50 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
53 if (kstrtoul(buf, 2, &new))
56 if (!qlcnic_config_bridged_mode(adapter, !!new))
63 static ssize_t qlcnic_show_bridged_mode(struct device *dev,
64 struct device_attribute *attr,
67 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
70 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
71 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
73 return sprintf(buf, "%d\n", bridged_mode);
76 static ssize_t qlcnic_store_diag_mode(struct device *dev,
77 struct device_attribute *attr,
78 const char *buf, size_t len)
80 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
83 if (kstrtoul(buf, 2, &new))
86 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
87 adapter->flags ^= QLCNIC_DIAG_ENABLED;
92 static ssize_t qlcnic_show_diag_mode(struct device *dev,
93 struct device_attribute *attr, char *buf)
95 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
96 return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
99 static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
103 *state = MSB(beacon);
105 QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
108 *rate = __QLCNIC_MAX_LED_RATE;
110 } else if (*state > __QLCNIC_MAX_LED_STATE) {
114 if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
120 static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
121 const char *buf, size_t len)
123 struct qlcnic_hardware_context *ahw = adapter->ahw;
124 unsigned long h_beacon;
127 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
130 if (kstrtoul(buf, 2, &h_beacon))
133 qlcnic_get_beacon_state(adapter);
135 if (ahw->beacon_state == h_beacon)
139 if (!ahw->beacon_state) {
140 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
147 err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
149 err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
151 ahw->beacon_state = h_beacon;
153 if (!ahw->beacon_state)
154 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
160 static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
161 const char *buf, size_t len)
163 struct qlcnic_hardware_context *ahw = adapter->ahw;
164 int err, drv_sds_rings = adapter->drv_sds_rings;
168 if (len != sizeof(u16))
169 return QL_STATUS_INVALID_PARAM;
171 memcpy(&beacon, buf, sizeof(u16));
172 err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
176 qlcnic_get_beacon_state(adapter);
178 if (ahw->beacon_state == b_state)
182 if (!ahw->beacon_state) {
183 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
189 if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
194 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
195 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
198 set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
201 err = qlcnic_config_led(adapter, b_state, b_rate);
204 ahw->beacon_state = b_state;
207 if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
208 qlcnic_diag_free_res(adapter->netdev, drv_sds_rings);
211 if (!ahw->beacon_state)
212 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
218 static ssize_t qlcnic_store_beacon(struct device *dev,
219 struct device_attribute *attr,
220 const char *buf, size_t len)
222 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
225 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
227 "LED test not supported in non privileged mode\n");
231 if (qlcnic_82xx_check(adapter))
232 err = qlcnic_82xx_store_beacon(adapter, buf, len);
233 else if (qlcnic_83xx_check(adapter))
234 err = qlcnic_83xx_store_beacon(adapter, buf, len);
241 static ssize_t qlcnic_show_beacon(struct device *dev,
242 struct device_attribute *attr, char *buf)
244 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
246 return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
249 static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
250 loff_t offset, size_t size)
254 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
257 if (offset < QLCNIC_PCI_CRBSPACE) {
258 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
259 QLCNIC_PCI_CAMQM_END))
265 if ((size != crb_size) || (offset & (crb_size-1)))
271 static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
272 struct bin_attribute *attr, char *buf,
273 loff_t offset, size_t size)
275 struct device *dev = container_of(kobj, struct device, kobj);
276 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
279 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
282 qlcnic_read_crb(adapter, buf, offset, size);
283 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
288 static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
289 struct bin_attribute *attr, char *buf,
290 loff_t offset, size_t size)
292 struct device *dev = container_of(kobj, struct device, kobj);
293 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
296 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
300 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
301 qlcnic_write_crb(adapter, buf, offset, size);
305 static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
306 loff_t offset, size_t size)
308 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
311 if ((size != 8) || (offset & 0x7))
317 static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
318 struct bin_attribute *attr, char *buf,
319 loff_t offset, size_t size)
321 struct device *dev = container_of(kobj, struct device, kobj);
322 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
326 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
330 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
333 memcpy(buf, &data, size);
334 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
339 static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
340 struct bin_attribute *attr, char *buf,
341 loff_t offset, size_t size)
343 struct device *dev = container_of(kobj, struct device, kobj);
344 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
348 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
352 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
353 memcpy(&data, buf, size);
355 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
361 int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
365 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
366 if (adapter->npars[i].pci_func == pci_func)
370 dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
374 static int validate_pm_config(struct qlcnic_adapter *adapter,
375 struct qlcnic_pm_func_cfg *pm_cfg, int count)
377 u8 src_pci_func, s_esw_id, d_esw_id;
379 int i, src_index, dest_index;
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);
386 return QL_STATUS_INVALID_PARAM;
388 dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
390 return QL_STATUS_INVALID_PARAM;
392 s_esw_id = adapter->npars[src_index].phy_port;
393 d_esw_id = adapter->npars[dest_index].phy_port;
395 if (s_esw_id != d_esw_id)
396 return QL_STATUS_INVALID_PARAM;
402 static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
403 struct kobject *kobj,
404 struct bin_attribute *attr,
405 char *buf, loff_t offset,
408 struct device *dev = container_of(kobj, struct device, kobj);
409 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
410 struct qlcnic_pm_func_cfg *pm_cfg;
411 u32 id, action, pci_func;
412 int count, rem, i, ret, index;
414 count = size / sizeof(struct qlcnic_pm_func_cfg);
415 rem = size % sizeof(struct qlcnic_pm_func_cfg);
417 return QL_STATUS_INVALID_PARAM;
419 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
420 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
421 ret = validate_pm_config(adapter, pm_cfg, count);
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);
430 return QL_STATUS_INVALID_PARAM;
432 id = adapter->npars[index].phy_port;
433 ret = qlcnic_config_port_mirroring(adapter, id,
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);
443 return QL_STATUS_INVALID_PARAM;
444 id = adapter->npars[index].phy_port;
445 adapter->npars[index].enable_pm = !!pm_cfg[i].action;
446 adapter->npars[index].dest_npar = id;
452 static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
453 struct kobject *kobj,
454 struct bin_attribute *attr,
455 char *buf, loff_t offset,
458 struct device *dev = container_of(kobj, struct device, kobj);
459 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
460 struct qlcnic_pm_func_cfg *pm_cfg;
465 memset(buf, 0, size);
466 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
467 count = size / sizeof(struct qlcnic_pm_func_cfg);
468 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
469 pci_func = adapter->npars[i].pci_func;
470 if (pci_func >= count) {
471 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
472 __func__, adapter->ahw->total_nic_func, count);
475 if (!adapter->npars[i].eswitch_status)
478 pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
479 pm_cfg[pci_func].dest_npar = 0;
480 pm_cfg[pci_func].pci_func = i;
482 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
486 static int validate_esw_config(struct qlcnic_adapter *adapter,
487 struct qlcnic_esw_func_cfg *esw_cfg, int count)
489 struct qlcnic_hardware_context *ahw = adapter->ahw;
494 if (qlcnic_82xx_check(adapter))
495 op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
497 op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
499 for (i = 0; i < count; i++) {
500 pci_func = esw_cfg[i].pci_func;
501 if (pci_func >= ahw->max_vnic_func)
502 return QL_STATUS_INVALID_PARAM;
504 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
505 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
506 return QL_STATUS_INVALID_PARAM;
508 switch (esw_cfg[i].op_mode) {
509 case QLCNIC_PORT_DEFAULTS:
510 if (qlcnic_82xx_check(adapter)) {
511 ret = QLC_DEV_GET_DRV(op_mode, pci_func);
513 ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
515 esw_cfg[i].offload_flags = 0;
518 if (ret != QLCNIC_NON_PRIV_FUNC) {
519 if (esw_cfg[i].mac_anti_spoof != 0)
520 return QL_STATUS_INVALID_PARAM;
521 if (esw_cfg[i].mac_override != 1)
522 return QL_STATUS_INVALID_PARAM;
523 if (esw_cfg[i].promisc_mode != 1)
524 return QL_STATUS_INVALID_PARAM;
527 case QLCNIC_ADD_VLAN:
528 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
529 return QL_STATUS_INVALID_PARAM;
530 if (!esw_cfg[i].op_type)
531 return QL_STATUS_INVALID_PARAM;
533 case QLCNIC_DEL_VLAN:
534 if (!esw_cfg[i].op_type)
535 return QL_STATUS_INVALID_PARAM;
538 return QL_STATUS_INVALID_PARAM;
545 static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
546 struct kobject *kobj,
547 struct bin_attribute *attr,
548 char *buf, loff_t offset,
551 struct device *dev = container_of(kobj, struct device, kobj);
552 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
553 struct qlcnic_esw_func_cfg *esw_cfg;
554 struct qlcnic_npar_info *npar;
555 int count, rem, i, ret;
557 u8 op_mode = 0, pci_func;
559 count = size / sizeof(struct qlcnic_esw_func_cfg);
560 rem = size % sizeof(struct qlcnic_esw_func_cfg);
562 return QL_STATUS_INVALID_PARAM;
564 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
565 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
566 ret = validate_esw_config(adapter, esw_cfg, count);
570 for (i = 0; i < count; i++) {
571 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
572 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
573 return QL_STATUS_INVALID_PARAM;
575 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
578 op_mode = esw_cfg[i].op_mode;
579 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
580 esw_cfg[i].op_mode = op_mode;
581 esw_cfg[i].pci_func = adapter->ahw->pci_func;
583 switch (esw_cfg[i].op_mode) {
584 case QLCNIC_PORT_DEFAULTS:
585 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
587 qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
590 case QLCNIC_ADD_VLAN:
591 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
593 case QLCNIC_DEL_VLAN:
594 esw_cfg[i].vlan_id = 0;
595 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
600 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
603 for (i = 0; i < count; i++) {
604 pci_func = esw_cfg[i].pci_func;
605 index = qlcnic_is_valid_nic_func(adapter, pci_func);
607 return QL_STATUS_INVALID_PARAM;
608 npar = &adapter->npars[index];
609 switch (esw_cfg[i].op_mode) {
610 case QLCNIC_PORT_DEFAULTS:
611 npar->promisc_mode = esw_cfg[i].promisc_mode;
612 npar->mac_override = esw_cfg[i].mac_override;
613 npar->offload_flags = esw_cfg[i].offload_flags;
614 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
615 npar->discard_tagged = esw_cfg[i].discard_tagged;
617 case QLCNIC_ADD_VLAN:
618 npar->pvid = esw_cfg[i].vlan_id;
620 case QLCNIC_DEL_VLAN:
629 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
630 struct kobject *kobj,
631 struct bin_attribute *attr,
632 char *buf, loff_t offset,
635 struct device *dev = container_of(kobj, struct device, kobj);
636 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
637 struct qlcnic_esw_func_cfg *esw_cfg;
642 memset(buf, 0, size);
643 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
644 count = size / sizeof(struct qlcnic_esw_func_cfg);
645 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
646 pci_func = adapter->npars[i].pci_func;
647 if (pci_func >= count) {
648 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
649 __func__, adapter->ahw->total_nic_func, count);
652 if (!adapter->npars[i].eswitch_status)
655 esw_cfg[pci_func].pci_func = pci_func;
656 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
657 return QL_STATUS_INVALID_PARAM;
659 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
663 static int validate_npar_config(struct qlcnic_adapter *adapter,
664 struct qlcnic_npar_func_cfg *np_cfg,
669 for (i = 0; i < count; i++) {
670 pci_func = np_cfg[i].pci_func;
671 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
672 return QL_STATUS_INVALID_PARAM;
674 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
675 !IS_VALID_BW(np_cfg[i].max_bw))
676 return QL_STATUS_INVALID_PARAM;
681 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
682 struct kobject *kobj,
683 struct bin_attribute *attr,
684 char *buf, loff_t offset,
687 struct device *dev = container_of(kobj, struct device, kobj);
688 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
689 struct qlcnic_info nic_info;
690 struct qlcnic_npar_func_cfg *np_cfg;
691 int i, count, rem, ret, index;
694 count = size / sizeof(struct qlcnic_npar_func_cfg);
695 rem = size % sizeof(struct qlcnic_npar_func_cfg);
697 return QL_STATUS_INVALID_PARAM;
699 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
700 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
701 ret = validate_npar_config(adapter, np_cfg, count);
705 for (i = 0; i < count; i++) {
706 pci_func = np_cfg[i].pci_func;
708 memset(&nic_info, 0, sizeof(struct qlcnic_info));
709 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
712 nic_info.pci_func = pci_func;
713 nic_info.min_tx_bw = np_cfg[i].min_bw;
714 nic_info.max_tx_bw = np_cfg[i].max_bw;
715 ret = qlcnic_set_nic_info(adapter, &nic_info);
718 index = qlcnic_is_valid_nic_func(adapter, pci_func);
720 return QL_STATUS_INVALID_PARAM;
721 adapter->npars[index].min_bw = nic_info.min_tx_bw;
722 adapter->npars[index].max_bw = nic_info.max_tx_bw;
728 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
729 struct kobject *kobj,
730 struct bin_attribute *attr,
731 char *buf, loff_t offset,
734 struct device *dev = container_of(kobj, struct device, kobj);
735 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
736 struct qlcnic_npar_func_cfg *np_cfg;
737 struct qlcnic_info nic_info;
742 memset(&nic_info, 0, sizeof(struct qlcnic_info));
743 memset(buf, 0, size);
744 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
746 count = size / sizeof(struct qlcnic_npar_func_cfg);
747 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
748 if (adapter->npars[i].pci_func >= count) {
749 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
750 __func__, adapter->ahw->total_nic_func, count);
753 if (!adapter->npars[i].eswitch_status)
755 pci_func = adapter->npars[i].pci_func;
756 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
758 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
762 np_cfg[pci_func].pci_func = pci_func;
763 np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
764 np_cfg[pci_func].port_num = nic_info.phys_port;
765 np_cfg[pci_func].fw_capab = nic_info.capabilities;
766 np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
767 np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
768 np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
769 np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
771 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
775 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
776 struct kobject *kobj,
777 struct bin_attribute *attr,
778 char *buf, loff_t offset,
781 struct device *dev = container_of(kobj, struct device, kobj);
782 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
783 struct qlcnic_esw_statistics port_stats;
786 if (qlcnic_83xx_check(adapter))
787 return QLC_STATUS_UNSUPPORTED_CMD;
789 if (size != sizeof(struct qlcnic_esw_statistics))
790 return QL_STATUS_INVALID_PARAM;
792 if (offset >= adapter->ahw->max_vnic_func)
793 return QL_STATUS_INVALID_PARAM;
795 memset(&port_stats, 0, size);
796 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
801 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
806 memcpy(buf, &port_stats, size);
810 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
811 struct kobject *kobj,
812 struct bin_attribute *attr,
813 char *buf, loff_t offset,
816 struct device *dev = container_of(kobj, struct device, kobj);
817 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
818 struct qlcnic_esw_statistics esw_stats;
821 if (qlcnic_83xx_check(adapter))
822 return QLC_STATUS_UNSUPPORTED_CMD;
824 if (size != sizeof(struct qlcnic_esw_statistics))
825 return QL_STATUS_INVALID_PARAM;
827 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
828 return QL_STATUS_INVALID_PARAM;
830 memset(&esw_stats, 0, size);
831 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
836 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
841 memcpy(buf, &esw_stats, size);
845 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
846 struct kobject *kobj,
847 struct bin_attribute *attr,
848 char *buf, loff_t offset,
851 struct device *dev = container_of(kobj, struct device, kobj);
852 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
855 if (qlcnic_83xx_check(adapter))
856 return QLC_STATUS_UNSUPPORTED_CMD;
858 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
859 return QL_STATUS_INVALID_PARAM;
861 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
862 QLCNIC_QUERY_RX_COUNTER);
866 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
867 QLCNIC_QUERY_TX_COUNTER);
874 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
875 struct kobject *kobj,
876 struct bin_attribute *attr,
877 char *buf, loff_t offset,
881 struct device *dev = container_of(kobj, struct device, kobj);
882 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
885 if (qlcnic_83xx_check(adapter))
886 return QLC_STATUS_UNSUPPORTED_CMD;
888 if (offset >= adapter->ahw->max_vnic_func)
889 return QL_STATUS_INVALID_PARAM;
891 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
892 QLCNIC_QUERY_RX_COUNTER);
896 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
897 QLCNIC_QUERY_TX_COUNTER);
904 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
905 struct kobject *kobj,
906 struct bin_attribute *attr,
907 char *buf, loff_t offset,
910 struct device *dev = container_of(kobj, struct device, kobj);
911 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
912 struct qlcnic_pci_func_cfg *pci_cfg;
913 struct qlcnic_pci_info *pci_info;
917 pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
921 ret = qlcnic_get_pci_info(adapter, pci_info);
927 pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
928 count = size / sizeof(struct qlcnic_pci_func_cfg);
929 qlcnic_swap32_buffer((u32 *)pci_info, size / sizeof(u32));
930 for (i = 0; i < count; i++) {
931 pci_cfg[i].pci_func = pci_info[i].id;
932 pci_cfg[i].func_type = pci_info[i].type;
933 pci_cfg[i].func_state = 0;
934 pci_cfg[i].port_num = pci_info[i].default_port;
935 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
936 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
937 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
944 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
945 struct kobject *kobj,
946 struct bin_attribute *attr,
947 char *buf, loff_t offset,
950 unsigned char *p_read_buf;
952 struct device *dev = container_of(kobj, struct device, kobj);
953 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
956 return QL_STATUS_INVALID_PARAM;
958 return QL_STATUS_INVALID_PARAM;
960 count = size / sizeof(u32);
962 if (size % sizeof(u32))
965 p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
968 if (qlcnic_83xx_lock_flash(adapter) != 0) {
973 ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
977 qlcnic_83xx_unlock_flash(adapter);
982 qlcnic_83xx_unlock_flash(adapter);
983 qlcnic_swap32_buffer((u32 *)p_read_buf, count);
984 memcpy(buf, p_read_buf, size);
990 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
991 char *buf, loff_t offset,
995 unsigned char *p_cache, *p_src;
997 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1001 count = size / sizeof(u32);
1002 qlcnic_swap32_buffer((u32 *)buf, count);
1003 memcpy(p_cache, buf, size);
1006 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1011 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1012 ret = qlcnic_83xx_enable_flash_write(adapter);
1015 qlcnic_83xx_unlock_flash(adapter);
1020 for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1021 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1023 QLC_83XX_FLASH_WRITE_MAX);
1026 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1027 ret = qlcnic_83xx_disable_flash_write(adapter);
1030 qlcnic_83xx_unlock_flash(adapter);
1036 qlcnic_83xx_unlock_flash(adapter);
1040 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1041 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1044 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1045 ret = qlcnic_83xx_disable_flash_write(adapter);
1048 qlcnic_83xx_unlock_flash(adapter);
1054 qlcnic_83xx_unlock_flash(adapter);
1059 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1060 char *buf, loff_t offset, size_t size)
1063 unsigned char *p_cache, *p_src;
1065 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1069 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
1070 memcpy(p_cache, buf, size);
1072 count = size / sizeof(u32);
1074 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1079 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1080 ret = qlcnic_83xx_enable_flash_write(adapter);
1083 qlcnic_83xx_unlock_flash(adapter);
1088 for (i = 0; i < count; i++) {
1089 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1091 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1092 ret = qlcnic_83xx_disable_flash_write(adapter);
1095 qlcnic_83xx_unlock_flash(adapter);
1100 qlcnic_83xx_unlock_flash(adapter);
1104 p_src = p_src + sizeof(u32);
1105 offset = offset + sizeof(u32);
1108 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1109 ret = qlcnic_83xx_disable_flash_write(adapter);
1112 qlcnic_83xx_unlock_flash(adapter);
1118 qlcnic_83xx_unlock_flash(adapter);
1123 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1124 struct kobject *kobj,
1125 struct bin_attribute *attr,
1126 char *buf, loff_t offset,
1130 static int flash_mode;
1132 struct device *dev = container_of(kobj, struct device, kobj);
1133 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1136 return QL_STATUS_INVALID_PARAM;
1138 ret = kstrtoul(buf, 16, &data);
1141 case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1142 flash_mode = QLC_83XX_ERASE_MODE;
1143 ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1145 dev_err(&adapter->pdev->dev,
1146 "%s failed at %d\n", __func__, __LINE__);
1151 case QLC_83XX_FLASH_BULK_WRITE_CMD:
1152 flash_mode = QLC_83XX_BULK_WRITE_MODE;
1155 case QLC_83XX_FLASH_WRITE_CMD:
1156 flash_mode = QLC_83XX_WRITE_MODE;
1159 if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1160 ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1163 dev_err(&adapter->pdev->dev,
1164 "%s failed at %d\n",
1165 __func__, __LINE__);
1170 if (flash_mode == QLC_83XX_WRITE_MODE) {
1171 ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1174 dev_err(&adapter->pdev->dev,
1175 "%s failed at %d\n", __func__,
1185 static struct device_attribute dev_attr_bridged_mode = {
1186 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1187 .show = qlcnic_show_bridged_mode,
1188 .store = qlcnic_store_bridged_mode,
1191 static struct device_attribute dev_attr_diag_mode = {
1192 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1193 .show = qlcnic_show_diag_mode,
1194 .store = qlcnic_store_diag_mode,
1197 static struct device_attribute dev_attr_beacon = {
1198 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1199 .show = qlcnic_show_beacon,
1200 .store = qlcnic_store_beacon,
1203 static struct bin_attribute bin_attr_crb = {
1204 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1206 .read = qlcnic_sysfs_read_crb,
1207 .write = qlcnic_sysfs_write_crb,
1210 static struct bin_attribute bin_attr_mem = {
1211 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1213 .read = qlcnic_sysfs_read_mem,
1214 .write = qlcnic_sysfs_write_mem,
1217 static struct bin_attribute bin_attr_npar_config = {
1218 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1220 .read = qlcnic_sysfs_read_npar_config,
1221 .write = qlcnic_sysfs_write_npar_config,
1224 static struct bin_attribute bin_attr_pci_config = {
1225 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1227 .read = qlcnic_sysfs_read_pci_config,
1231 static struct bin_attribute bin_attr_port_stats = {
1232 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1234 .read = qlcnic_sysfs_get_port_stats,
1235 .write = qlcnic_sysfs_clear_port_stats,
1238 static struct bin_attribute bin_attr_esw_stats = {
1239 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1241 .read = qlcnic_sysfs_get_esw_stats,
1242 .write = qlcnic_sysfs_clear_esw_stats,
1245 static struct bin_attribute bin_attr_esw_config = {
1246 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1248 .read = qlcnic_sysfs_read_esw_config,
1249 .write = qlcnic_sysfs_write_esw_config,
1252 static struct bin_attribute bin_attr_pm_config = {
1253 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1255 .read = qlcnic_sysfs_read_pm_config,
1256 .write = qlcnic_sysfs_write_pm_config,
1259 static struct bin_attribute bin_attr_flash = {
1260 .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1262 .read = qlcnic_83xx_sysfs_flash_read_handler,
1263 .write = qlcnic_83xx_sysfs_flash_write_handler,
1266 #ifdef CONFIG_QLCNIC_HWMON
1268 static ssize_t qlcnic_hwmon_show_temp(struct device *dev,
1269 struct device_attribute *dev_attr,
1272 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1273 unsigned int temperature = 0, value = 0;
1275 if (qlcnic_83xx_check(adapter))
1276 value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
1277 else if (qlcnic_82xx_check(adapter))
1278 value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
1280 temperature = qlcnic_get_temp_val(value);
1281 /* display millidegree celcius */
1282 temperature *= 1000;
1283 return sprintf(buf, "%u\n", temperature);
1286 /* hwmon-sysfs attributes */
1287 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1288 qlcnic_hwmon_show_temp, NULL, 1);
1290 static struct attribute *qlcnic_hwmon_attrs[] = {
1291 &sensor_dev_attr_temp1_input.dev_attr.attr,
1295 ATTRIBUTE_GROUPS(qlcnic_hwmon);
1297 void qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
1299 struct device *dev = &adapter->pdev->dev;
1300 struct device *hwmon_dev;
1302 /* Skip hwmon registration for a VF device */
1303 if (qlcnic_sriov_vf_check(adapter)) {
1304 adapter->ahw->hwmon_dev = NULL;
1307 hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
1309 qlcnic_hwmon_groups);
1310 if (IS_ERR(hwmon_dev)) {
1311 dev_err(dev, "Cannot register with hwmon, err=%ld\n",
1312 PTR_ERR(hwmon_dev));
1315 adapter->ahw->hwmon_dev = hwmon_dev;
1318 void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
1320 struct device *hwmon_dev = adapter->ahw->hwmon_dev;
1322 hwmon_device_unregister(hwmon_dev);
1323 adapter->ahw->hwmon_dev = NULL;
1328 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1330 struct device *dev = &adapter->pdev->dev;
1332 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1333 if (device_create_file(dev, &dev_attr_bridged_mode))
1335 "failed to create bridged_mode sysfs entry\n");
1338 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1340 struct device *dev = &adapter->pdev->dev;
1342 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1343 device_remove_file(dev, &dev_attr_bridged_mode);
1346 static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1348 struct device *dev = &adapter->pdev->dev;
1350 if (device_create_bin_file(dev, &bin_attr_port_stats))
1351 dev_info(dev, "failed to create port stats sysfs entry");
1353 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1355 if (device_create_file(dev, &dev_attr_diag_mode))
1356 dev_info(dev, "failed to create diag_mode sysfs entry\n");
1357 if (device_create_bin_file(dev, &bin_attr_crb))
1358 dev_info(dev, "failed to create crb sysfs entry\n");
1359 if (device_create_bin_file(dev, &bin_attr_mem))
1360 dev_info(dev, "failed to create mem sysfs entry\n");
1362 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1365 if (device_create_bin_file(dev, &bin_attr_pci_config))
1366 dev_info(dev, "failed to create pci config sysfs entry");
1368 if (device_create_file(dev, &dev_attr_beacon))
1369 dev_info(dev, "failed to create beacon sysfs entry");
1371 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1373 if (device_create_bin_file(dev, &bin_attr_esw_config))
1374 dev_info(dev, "failed to create esw config sysfs entry");
1375 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1377 if (device_create_bin_file(dev, &bin_attr_npar_config))
1378 dev_info(dev, "failed to create npar config sysfs entry");
1379 if (device_create_bin_file(dev, &bin_attr_pm_config))
1380 dev_info(dev, "failed to create pm config sysfs entry");
1381 if (device_create_bin_file(dev, &bin_attr_esw_stats))
1382 dev_info(dev, "failed to create eswitch stats sysfs entry");
1385 static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1387 struct device *dev = &adapter->pdev->dev;
1389 device_remove_bin_file(dev, &bin_attr_port_stats);
1391 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1393 device_remove_file(dev, &dev_attr_diag_mode);
1394 device_remove_bin_file(dev, &bin_attr_crb);
1395 device_remove_bin_file(dev, &bin_attr_mem);
1397 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1400 device_remove_bin_file(dev, &bin_attr_pci_config);
1401 device_remove_file(dev, &dev_attr_beacon);
1402 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1404 device_remove_bin_file(dev, &bin_attr_esw_config);
1405 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1407 device_remove_bin_file(dev, &bin_attr_npar_config);
1408 device_remove_bin_file(dev, &bin_attr_pm_config);
1409 device_remove_bin_file(dev, &bin_attr_esw_stats);
1412 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1414 qlcnic_create_diag_entries(adapter);
1417 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1419 qlcnic_remove_diag_entries(adapter);
1422 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1424 struct device *dev = &adapter->pdev->dev;
1426 qlcnic_create_diag_entries(adapter);
1428 if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1429 dev_info(dev, "failed to create flash sysfs entry\n");
1432 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1434 struct device *dev = &adapter->pdev->dev;
1436 qlcnic_remove_diag_entries(adapter);
1437 sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);