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 int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
32 int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
37 static ssize_t qlcnic_store_bridged_mode(struct device *dev,
38 struct device_attribute *attr,
39 const char *buf, size_t len)
41 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
45 if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
48 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
51 if (kstrtoul(buf, 2, &new))
54 if (!qlcnic_config_bridged_mode(adapter, !!new))
61 static ssize_t qlcnic_show_bridged_mode(struct device *dev,
62 struct device_attribute *attr,
65 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
68 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
69 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
71 return sprintf(buf, "%d\n", bridged_mode);
74 static ssize_t qlcnic_store_diag_mode(struct device *dev,
75 struct device_attribute *attr,
76 const char *buf, size_t len)
78 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
81 if (kstrtoul(buf, 2, &new))
84 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
85 adapter->flags ^= QLCNIC_DIAG_ENABLED;
90 static ssize_t qlcnic_show_diag_mode(struct device *dev,
91 struct device_attribute *attr, char *buf)
93 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
94 return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
97 static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
101 *state = MSB(beacon);
103 QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
106 *rate = __QLCNIC_MAX_LED_RATE;
108 } else if (*state > __QLCNIC_MAX_LED_STATE) {
112 if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
118 static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
119 const char *buf, size_t len)
121 struct qlcnic_hardware_context *ahw = adapter->ahw;
122 unsigned long h_beacon;
125 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
128 if (kstrtoul(buf, 2, &h_beacon))
131 qlcnic_get_beacon_state(adapter);
133 if (ahw->beacon_state == h_beacon)
137 if (!ahw->beacon_state) {
138 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
145 err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
147 err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
149 ahw->beacon_state = h_beacon;
151 if (!ahw->beacon_state)
152 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
158 static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
159 const char *buf, size_t len)
161 struct qlcnic_hardware_context *ahw = adapter->ahw;
162 int err, drv_sds_rings = adapter->drv_sds_rings;
166 if (len != sizeof(u16))
169 memcpy(&beacon, buf, sizeof(u16));
170 err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
174 qlcnic_get_beacon_state(adapter);
176 if (ahw->beacon_state == b_state)
180 if (!ahw->beacon_state) {
181 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
187 if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
192 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
193 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
196 set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
199 err = qlcnic_config_led(adapter, b_state, b_rate);
202 ahw->beacon_state = b_state;
205 if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
206 qlcnic_diag_free_res(adapter->netdev, drv_sds_rings);
209 if (!ahw->beacon_state)
210 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
216 static ssize_t qlcnic_store_beacon(struct device *dev,
217 struct device_attribute *attr,
218 const char *buf, size_t len)
220 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
223 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
225 "LED test not supported in non privileged mode\n");
229 if (qlcnic_82xx_check(adapter))
230 err = qlcnic_82xx_store_beacon(adapter, buf, len);
231 else if (qlcnic_83xx_check(adapter))
232 err = qlcnic_83xx_store_beacon(adapter, buf, len);
239 static ssize_t qlcnic_show_beacon(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
244 return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
247 static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
248 loff_t offset, size_t size)
252 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
255 if (offset < QLCNIC_PCI_CRBSPACE) {
256 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
257 QLCNIC_PCI_CAMQM_END))
263 if ((size != crb_size) || (offset & (crb_size-1)))
269 static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
270 struct bin_attribute *attr, char *buf,
271 loff_t offset, size_t size)
273 struct device *dev = container_of(kobj, struct device, kobj);
274 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
277 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
280 qlcnic_read_crb(adapter, buf, offset, size);
281 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
286 static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
287 struct bin_attribute *attr, char *buf,
288 loff_t offset, size_t size)
290 struct device *dev = container_of(kobj, struct device, kobj);
291 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
294 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
298 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
299 qlcnic_write_crb(adapter, buf, offset, size);
303 static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
304 loff_t offset, size_t size)
306 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
309 if ((size != 8) || (offset & 0x7))
315 static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
316 struct bin_attribute *attr, char *buf,
317 loff_t offset, size_t size)
319 struct device *dev = container_of(kobj, struct device, kobj);
320 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
324 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
328 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
331 memcpy(buf, &data, size);
332 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
337 static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
338 struct bin_attribute *attr, char *buf,
339 loff_t offset, size_t size)
341 struct device *dev = container_of(kobj, struct device, kobj);
342 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
346 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
350 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
351 memcpy(&data, buf, size);
353 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
359 int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
363 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
364 if (adapter->npars[i].pci_func == pci_func)
368 dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
372 static int validate_pm_config(struct qlcnic_adapter *adapter,
373 struct qlcnic_pm_func_cfg *pm_cfg, int count)
375 u8 src_pci_func, s_esw_id, d_esw_id;
377 int i, src_index, dest_index;
379 for (i = 0; i < count; i++) {
380 src_pci_func = pm_cfg[i].pci_func;
381 dest_pci_func = pm_cfg[i].dest_npar;
382 src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
386 dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
390 s_esw_id = adapter->npars[src_index].phy_port;
391 d_esw_id = adapter->npars[dest_index].phy_port;
393 if (s_esw_id != d_esw_id)
400 static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
401 struct kobject *kobj,
402 struct bin_attribute *attr,
403 char *buf, loff_t offset,
406 struct device *dev = container_of(kobj, struct device, kobj);
407 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
408 struct qlcnic_pm_func_cfg *pm_cfg;
409 u32 id, action, pci_func;
410 int count, rem, i, ret, index;
412 count = size / sizeof(struct qlcnic_pm_func_cfg);
413 rem = size % sizeof(struct qlcnic_pm_func_cfg);
417 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
418 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
419 ret = validate_pm_config(adapter, pm_cfg, count);
423 for (i = 0; i < count; i++) {
424 pci_func = pm_cfg[i].pci_func;
425 action = !!pm_cfg[i].action;
426 index = qlcnic_is_valid_nic_func(adapter, pci_func);
430 id = adapter->npars[index].phy_port;
431 ret = qlcnic_config_port_mirroring(adapter, id,
437 for (i = 0; i < count; i++) {
438 pci_func = pm_cfg[i].pci_func;
439 index = qlcnic_is_valid_nic_func(adapter, pci_func);
442 id = adapter->npars[index].phy_port;
443 adapter->npars[index].enable_pm = !!pm_cfg[i].action;
444 adapter->npars[index].dest_npar = id;
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,
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;
463 memset(buf, 0, size);
464 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
465 count = size / sizeof(struct qlcnic_pm_func_cfg);
466 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
467 pci_func = adapter->npars[i].pci_func;
468 if (pci_func >= count) {
469 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
470 __func__, adapter->ahw->total_nic_func, count);
473 if (!adapter->npars[i].eswitch_status)
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;
480 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
484 static int validate_esw_config(struct qlcnic_adapter *adapter,
485 struct qlcnic_esw_func_cfg *esw_cfg, int count)
487 struct qlcnic_hardware_context *ahw = adapter->ahw;
492 if (qlcnic_82xx_check(adapter))
493 op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
495 op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
497 for (i = 0; i < count; i++) {
498 pci_func = esw_cfg[i].pci_func;
499 if (pci_func >= ahw->max_vnic_func)
502 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
503 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
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);
511 ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
513 esw_cfg[i].offload_flags = 0;
516 if (ret != QLCNIC_NON_PRIV_FUNC) {
517 if (esw_cfg[i].mac_anti_spoof != 0)
519 if (esw_cfg[i].mac_override != 1)
521 if (esw_cfg[i].promisc_mode != 1)
525 case QLCNIC_ADD_VLAN:
526 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
528 if (!esw_cfg[i].op_type)
531 case QLCNIC_DEL_VLAN:
532 if (!esw_cfg[i].op_type)
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,
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;
555 u8 op_mode = 0, pci_func;
557 count = size / sizeof(struct qlcnic_esw_func_cfg);
558 rem = size % sizeof(struct qlcnic_esw_func_cfg);
562 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
563 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
564 ret = validate_esw_config(adapter, esw_cfg, count);
568 for (i = 0; i < count; i++) {
569 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
570 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
573 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
576 op_mode = esw_cfg[i].op_mode;
577 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
578 esw_cfg[i].op_mode = op_mode;
579 esw_cfg[i].pci_func = adapter->ahw->pci_func;
581 switch (esw_cfg[i].op_mode) {
582 case QLCNIC_PORT_DEFAULTS:
583 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
585 qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
588 case QLCNIC_ADD_VLAN:
589 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
591 case QLCNIC_DEL_VLAN:
592 esw_cfg[i].vlan_id = 0;
593 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
598 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
601 for (i = 0; i < count; i++) {
602 pci_func = esw_cfg[i].pci_func;
603 index = qlcnic_is_valid_nic_func(adapter, pci_func);
606 npar = &adapter->npars[index];
607 switch (esw_cfg[i].op_mode) {
608 case QLCNIC_PORT_DEFAULTS:
609 npar->promisc_mode = esw_cfg[i].promisc_mode;
610 npar->mac_override = esw_cfg[i].mac_override;
611 npar->offload_flags = esw_cfg[i].offload_flags;
612 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
613 npar->discard_tagged = esw_cfg[i].discard_tagged;
615 case QLCNIC_ADD_VLAN:
616 npar->pvid = esw_cfg[i].vlan_id;
618 case QLCNIC_DEL_VLAN:
627 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
628 struct kobject *kobj,
629 struct bin_attribute *attr,
630 char *buf, loff_t offset,
633 struct device *dev = container_of(kobj, struct device, kobj);
634 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
635 struct qlcnic_esw_func_cfg *esw_cfg;
640 memset(buf, 0, size);
641 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
642 count = size / sizeof(struct qlcnic_esw_func_cfg);
643 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
644 pci_func = adapter->npars[i].pci_func;
645 if (pci_func >= count) {
646 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
647 __func__, adapter->ahw->total_nic_func, count);
650 if (!adapter->npars[i].eswitch_status)
653 esw_cfg[pci_func].pci_func = pci_func;
654 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
657 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
661 static int validate_npar_config(struct qlcnic_adapter *adapter,
662 struct qlcnic_npar_func_cfg *np_cfg,
667 for (i = 0; i < count; i++) {
668 pci_func = np_cfg[i].pci_func;
669 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
672 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
673 !IS_VALID_BW(np_cfg[i].max_bw))
679 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
680 struct kobject *kobj,
681 struct bin_attribute *attr,
682 char *buf, loff_t offset,
685 struct device *dev = container_of(kobj, struct device, kobj);
686 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
687 struct qlcnic_info nic_info;
688 struct qlcnic_npar_func_cfg *np_cfg;
689 int i, count, rem, ret, index;
692 count = size / sizeof(struct qlcnic_npar_func_cfg);
693 rem = size % sizeof(struct qlcnic_npar_func_cfg);
697 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
698 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
699 ret = validate_npar_config(adapter, np_cfg, count);
703 for (i = 0; i < count; i++) {
704 pci_func = np_cfg[i].pci_func;
706 memset(&nic_info, 0, sizeof(struct qlcnic_info));
707 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
710 nic_info.pci_func = pci_func;
711 nic_info.min_tx_bw = np_cfg[i].min_bw;
712 nic_info.max_tx_bw = np_cfg[i].max_bw;
713 ret = qlcnic_set_nic_info(adapter, &nic_info);
716 index = qlcnic_is_valid_nic_func(adapter, pci_func);
719 adapter->npars[index].min_bw = nic_info.min_tx_bw;
720 adapter->npars[index].max_bw = nic_info.max_tx_bw;
726 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
727 struct kobject *kobj,
728 struct bin_attribute *attr,
729 char *buf, loff_t offset,
732 struct device *dev = container_of(kobj, struct device, kobj);
733 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
734 struct qlcnic_npar_func_cfg *np_cfg;
735 struct qlcnic_info nic_info;
740 memset(&nic_info, 0, sizeof(struct qlcnic_info));
741 memset(buf, 0, size);
742 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
744 count = size / sizeof(struct qlcnic_npar_func_cfg);
745 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
746 if (adapter->npars[i].pci_func >= count) {
747 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
748 __func__, adapter->ahw->total_nic_func, count);
751 if (!adapter->npars[i].eswitch_status)
753 pci_func = adapter->npars[i].pci_func;
754 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
756 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
760 np_cfg[pci_func].pci_func = pci_func;
761 np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
762 np_cfg[pci_func].port_num = nic_info.phys_port;
763 np_cfg[pci_func].fw_capab = nic_info.capabilities;
764 np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
765 np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
766 np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
767 np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
769 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
773 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
774 struct kobject *kobj,
775 struct bin_attribute *attr,
776 char *buf, loff_t offset,
779 struct device *dev = container_of(kobj, struct device, kobj);
780 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
781 struct qlcnic_esw_statistics port_stats;
784 if (qlcnic_83xx_check(adapter))
787 if (size != sizeof(struct qlcnic_esw_statistics))
790 if (offset >= adapter->ahw->max_vnic_func)
793 memset(&port_stats, 0, size);
794 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
799 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
804 memcpy(buf, &port_stats, size);
808 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
809 struct kobject *kobj,
810 struct bin_attribute *attr,
811 char *buf, loff_t offset,
814 struct device *dev = container_of(kobj, struct device, kobj);
815 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
816 struct qlcnic_esw_statistics esw_stats;
819 if (qlcnic_83xx_check(adapter))
822 if (size != sizeof(struct qlcnic_esw_statistics))
825 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
828 memset(&esw_stats, 0, size);
829 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
834 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
839 memcpy(buf, &esw_stats, size);
843 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
844 struct kobject *kobj,
845 struct bin_attribute *attr,
846 char *buf, loff_t offset,
849 struct device *dev = container_of(kobj, struct device, kobj);
850 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
853 if (qlcnic_83xx_check(adapter))
856 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
859 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
860 QLCNIC_QUERY_RX_COUNTER);
864 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
865 QLCNIC_QUERY_TX_COUNTER);
872 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
873 struct kobject *kobj,
874 struct bin_attribute *attr,
875 char *buf, loff_t offset,
879 struct device *dev = container_of(kobj, struct device, kobj);
880 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
883 if (qlcnic_83xx_check(adapter))
886 if (offset >= adapter->ahw->max_vnic_func)
889 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
890 QLCNIC_QUERY_RX_COUNTER);
894 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
895 QLCNIC_QUERY_TX_COUNTER);
902 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
903 struct kobject *kobj,
904 struct bin_attribute *attr,
905 char *buf, loff_t offset,
908 struct device *dev = container_of(kobj, struct device, kobj);
909 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
910 struct qlcnic_pci_func_cfg *pci_cfg;
911 struct qlcnic_pci_info *pci_info;
915 pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
919 ret = qlcnic_get_pci_info(adapter, pci_info);
925 pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
926 count = size / sizeof(struct qlcnic_pci_func_cfg);
927 qlcnic_swap32_buffer((u32 *)pci_info, size / sizeof(u32));
928 for (i = 0; i < count; i++) {
929 pci_cfg[i].pci_func = pci_info[i].id;
930 pci_cfg[i].func_type = pci_info[i].type;
931 pci_cfg[i].func_state = 0;
932 pci_cfg[i].port_num = pci_info[i].default_port;
933 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
934 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
935 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
942 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
943 struct kobject *kobj,
944 struct bin_attribute *attr,
945 char *buf, loff_t offset,
948 unsigned char *p_read_buf;
950 struct device *dev = container_of(kobj, struct device, kobj);
951 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
956 count = size / sizeof(u32);
958 if (size % sizeof(u32))
961 p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
964 if (qlcnic_83xx_lock_flash(adapter) != 0) {
969 ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
973 qlcnic_83xx_unlock_flash(adapter);
978 qlcnic_83xx_unlock_flash(adapter);
979 qlcnic_swap32_buffer((u32 *)p_read_buf, count);
980 memcpy(buf, p_read_buf, size);
986 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
987 char *buf, loff_t offset,
991 unsigned char *p_cache, *p_src;
993 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
997 count = size / sizeof(u32);
998 qlcnic_swap32_buffer((u32 *)buf, count);
999 memcpy(p_cache, buf, size);
1002 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1007 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1008 ret = qlcnic_83xx_enable_flash_write(adapter);
1011 qlcnic_83xx_unlock_flash(adapter);
1016 for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1017 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1019 QLC_83XX_FLASH_WRITE_MAX);
1022 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1023 ret = qlcnic_83xx_disable_flash_write(adapter);
1026 qlcnic_83xx_unlock_flash(adapter);
1032 qlcnic_83xx_unlock_flash(adapter);
1036 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1037 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1040 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1041 ret = qlcnic_83xx_disable_flash_write(adapter);
1044 qlcnic_83xx_unlock_flash(adapter);
1050 qlcnic_83xx_unlock_flash(adapter);
1055 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1056 char *buf, loff_t offset, size_t size)
1059 unsigned char *p_cache, *p_src;
1061 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1065 qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
1066 memcpy(p_cache, buf, size);
1068 count = size / sizeof(u32);
1070 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1075 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1076 ret = qlcnic_83xx_enable_flash_write(adapter);
1079 qlcnic_83xx_unlock_flash(adapter);
1084 for (i = 0; i < count; i++) {
1085 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1087 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1088 ret = qlcnic_83xx_disable_flash_write(adapter);
1091 qlcnic_83xx_unlock_flash(adapter);
1096 qlcnic_83xx_unlock_flash(adapter);
1100 p_src = p_src + sizeof(u32);
1101 offset = offset + sizeof(u32);
1104 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1105 ret = qlcnic_83xx_disable_flash_write(adapter);
1108 qlcnic_83xx_unlock_flash(adapter);
1114 qlcnic_83xx_unlock_flash(adapter);
1119 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1120 struct kobject *kobj,
1121 struct bin_attribute *attr,
1122 char *buf, loff_t offset,
1126 static int flash_mode;
1128 struct device *dev = container_of(kobj, struct device, kobj);
1129 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1131 ret = kstrtoul(buf, 16, &data);
1134 case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1135 flash_mode = QLC_83XX_ERASE_MODE;
1136 ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1138 dev_err(&adapter->pdev->dev,
1139 "%s failed at %d\n", __func__, __LINE__);
1144 case QLC_83XX_FLASH_BULK_WRITE_CMD:
1145 flash_mode = QLC_83XX_BULK_WRITE_MODE;
1148 case QLC_83XX_FLASH_WRITE_CMD:
1149 flash_mode = QLC_83XX_WRITE_MODE;
1152 if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1153 ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1156 dev_err(&adapter->pdev->dev,
1157 "%s failed at %d\n",
1158 __func__, __LINE__);
1163 if (flash_mode == QLC_83XX_WRITE_MODE) {
1164 ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1167 dev_err(&adapter->pdev->dev,
1168 "%s failed at %d\n", __func__,
1178 static struct device_attribute dev_attr_bridged_mode = {
1179 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1180 .show = qlcnic_show_bridged_mode,
1181 .store = qlcnic_store_bridged_mode,
1184 static struct device_attribute dev_attr_diag_mode = {
1185 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1186 .show = qlcnic_show_diag_mode,
1187 .store = qlcnic_store_diag_mode,
1190 static struct device_attribute dev_attr_beacon = {
1191 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1192 .show = qlcnic_show_beacon,
1193 .store = qlcnic_store_beacon,
1196 static struct bin_attribute bin_attr_crb = {
1197 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1199 .read = qlcnic_sysfs_read_crb,
1200 .write = qlcnic_sysfs_write_crb,
1203 static struct bin_attribute bin_attr_mem = {
1204 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1206 .read = qlcnic_sysfs_read_mem,
1207 .write = qlcnic_sysfs_write_mem,
1210 static struct bin_attribute bin_attr_npar_config = {
1211 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1213 .read = qlcnic_sysfs_read_npar_config,
1214 .write = qlcnic_sysfs_write_npar_config,
1217 static struct bin_attribute bin_attr_pci_config = {
1218 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1220 .read = qlcnic_sysfs_read_pci_config,
1224 static struct bin_attribute bin_attr_port_stats = {
1225 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1227 .read = qlcnic_sysfs_get_port_stats,
1228 .write = qlcnic_sysfs_clear_port_stats,
1231 static struct bin_attribute bin_attr_esw_stats = {
1232 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1234 .read = qlcnic_sysfs_get_esw_stats,
1235 .write = qlcnic_sysfs_clear_esw_stats,
1238 static struct bin_attribute bin_attr_esw_config = {
1239 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1241 .read = qlcnic_sysfs_read_esw_config,
1242 .write = qlcnic_sysfs_write_esw_config,
1245 static struct bin_attribute bin_attr_pm_config = {
1246 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1248 .read = qlcnic_sysfs_read_pm_config,
1249 .write = qlcnic_sysfs_write_pm_config,
1252 static struct bin_attribute bin_attr_flash = {
1253 .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1255 .read = qlcnic_83xx_sysfs_flash_read_handler,
1256 .write = qlcnic_83xx_sysfs_flash_write_handler,
1259 #ifdef CONFIG_QLCNIC_HWMON
1261 static ssize_t qlcnic_hwmon_show_temp(struct device *dev,
1262 struct device_attribute *dev_attr,
1265 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1266 unsigned int temperature = 0, value = 0;
1268 if (qlcnic_83xx_check(adapter))
1269 value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
1270 else if (qlcnic_82xx_check(adapter))
1271 value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
1273 temperature = qlcnic_get_temp_val(value);
1274 /* display millidegree celcius */
1275 temperature *= 1000;
1276 return sprintf(buf, "%u\n", temperature);
1279 /* hwmon-sysfs attributes */
1280 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1281 qlcnic_hwmon_show_temp, NULL, 1);
1283 static struct attribute *qlcnic_hwmon_attrs[] = {
1284 &sensor_dev_attr_temp1_input.dev_attr.attr,
1288 ATTRIBUTE_GROUPS(qlcnic_hwmon);
1290 void qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
1292 struct device *dev = &adapter->pdev->dev;
1293 struct device *hwmon_dev;
1295 /* Skip hwmon registration for a VF device */
1296 if (qlcnic_sriov_vf_check(adapter)) {
1297 adapter->ahw->hwmon_dev = NULL;
1300 hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
1302 qlcnic_hwmon_groups);
1303 if (IS_ERR(hwmon_dev)) {
1304 dev_err(dev, "Cannot register with hwmon, err=%ld\n",
1305 PTR_ERR(hwmon_dev));
1308 adapter->ahw->hwmon_dev = hwmon_dev;
1311 void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
1313 struct device *hwmon_dev = adapter->ahw->hwmon_dev;
1315 hwmon_device_unregister(hwmon_dev);
1316 adapter->ahw->hwmon_dev = NULL;
1321 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1323 struct device *dev = &adapter->pdev->dev;
1325 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1326 if (device_create_file(dev, &dev_attr_bridged_mode))
1328 "failed to create bridged_mode sysfs entry\n");
1331 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1333 struct device *dev = &adapter->pdev->dev;
1335 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1336 device_remove_file(dev, &dev_attr_bridged_mode);
1339 static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1341 struct device *dev = &adapter->pdev->dev;
1343 if (device_create_bin_file(dev, &bin_attr_port_stats))
1344 dev_info(dev, "failed to create port stats sysfs entry");
1346 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1348 if (device_create_file(dev, &dev_attr_diag_mode))
1349 dev_info(dev, "failed to create diag_mode sysfs entry\n");
1350 if (device_create_bin_file(dev, &bin_attr_crb))
1351 dev_info(dev, "failed to create crb sysfs entry\n");
1352 if (device_create_bin_file(dev, &bin_attr_mem))
1353 dev_info(dev, "failed to create mem sysfs entry\n");
1355 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1358 if (device_create_bin_file(dev, &bin_attr_pci_config))
1359 dev_info(dev, "failed to create pci config sysfs entry");
1361 if (device_create_file(dev, &dev_attr_beacon))
1362 dev_info(dev, "failed to create beacon sysfs entry");
1364 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1366 if (device_create_bin_file(dev, &bin_attr_esw_config))
1367 dev_info(dev, "failed to create esw config sysfs entry");
1368 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1370 if (device_create_bin_file(dev, &bin_attr_npar_config))
1371 dev_info(dev, "failed to create npar config sysfs entry");
1372 if (device_create_bin_file(dev, &bin_attr_pm_config))
1373 dev_info(dev, "failed to create pm config sysfs entry");
1374 if (device_create_bin_file(dev, &bin_attr_esw_stats))
1375 dev_info(dev, "failed to create eswitch stats sysfs entry");
1378 static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1380 struct device *dev = &adapter->pdev->dev;
1382 device_remove_bin_file(dev, &bin_attr_port_stats);
1384 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1386 device_remove_file(dev, &dev_attr_diag_mode);
1387 device_remove_bin_file(dev, &bin_attr_crb);
1388 device_remove_bin_file(dev, &bin_attr_mem);
1390 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1393 device_remove_bin_file(dev, &bin_attr_pci_config);
1394 device_remove_file(dev, &dev_attr_beacon);
1395 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1397 device_remove_bin_file(dev, &bin_attr_esw_config);
1398 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1400 device_remove_bin_file(dev, &bin_attr_npar_config);
1401 device_remove_bin_file(dev, &bin_attr_pm_config);
1402 device_remove_bin_file(dev, &bin_attr_esw_stats);
1405 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1407 qlcnic_create_diag_entries(adapter);
1410 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1412 qlcnic_remove_diag_entries(adapter);
1415 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1417 struct device *dev = &adapter->pdev->dev;
1419 qlcnic_create_diag_entries(adapter);
1421 if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1422 dev_info(dev, "failed to create flash sysfs entry\n");
1425 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1427 struct device *dev = &adapter->pdev->dev;
1429 qlcnic_remove_diag_entries(adapter);
1430 sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);