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);
287 static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
288 struct bin_attribute *attr, char *buf,
289 loff_t offset, size_t size)
291 struct device *dev = container_of(kobj, struct device, kobj);
292 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
295 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
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);
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 memcpy(&data, buf, size);
351 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
357 int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
361 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
362 if (adapter->npars[i].pci_func == pci_func)
368 static int validate_pm_config(struct qlcnic_adapter *adapter,
369 struct qlcnic_pm_func_cfg *pm_cfg, int count)
371 u8 src_pci_func, s_esw_id, d_esw_id;
373 int i, src_index, dest_index;
375 for (i = 0; i < count; i++) {
376 src_pci_func = pm_cfg[i].pci_func;
377 dest_pci_func = pm_cfg[i].dest_npar;
378 src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
380 return QL_STATUS_INVALID_PARAM;
382 dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
384 return QL_STATUS_INVALID_PARAM;
386 s_esw_id = adapter->npars[src_index].phy_port;
387 d_esw_id = adapter->npars[dest_index].phy_port;
389 if (s_esw_id != d_esw_id)
390 return QL_STATUS_INVALID_PARAM;
396 static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
397 struct kobject *kobj,
398 struct bin_attribute *attr,
399 char *buf, loff_t offset,
402 struct device *dev = container_of(kobj, struct device, kobj);
403 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
404 struct qlcnic_pm_func_cfg *pm_cfg;
405 u32 id, action, pci_func;
406 int count, rem, i, ret, index;
408 count = size / sizeof(struct qlcnic_pm_func_cfg);
409 rem = size % sizeof(struct qlcnic_pm_func_cfg);
411 return QL_STATUS_INVALID_PARAM;
413 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
414 ret = validate_pm_config(adapter, pm_cfg, count);
418 for (i = 0; i < count; i++) {
419 pci_func = pm_cfg[i].pci_func;
420 action = !!pm_cfg[i].action;
421 index = qlcnic_is_valid_nic_func(adapter, pci_func);
423 return QL_STATUS_INVALID_PARAM;
425 id = adapter->npars[index].phy_port;
426 ret = qlcnic_config_port_mirroring(adapter, id,
432 for (i = 0; i < count; i++) {
433 pci_func = pm_cfg[i].pci_func;
434 index = qlcnic_is_valid_nic_func(adapter, pci_func);
436 return QL_STATUS_INVALID_PARAM;
437 id = adapter->npars[index].phy_port;
438 adapter->npars[index].enable_pm = !!pm_cfg[i].action;
439 adapter->npars[index].dest_npar = id;
445 static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
446 struct kobject *kobj,
447 struct bin_attribute *attr,
448 char *buf, loff_t offset,
451 struct device *dev = container_of(kobj, struct device, kobj);
452 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
453 struct qlcnic_pm_func_cfg *pm_cfg;
458 memset(buf, 0, size);
459 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
460 count = size / sizeof(struct qlcnic_pm_func_cfg);
461 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
462 pci_func = adapter->npars[i].pci_func;
463 if (pci_func >= count) {
464 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
465 __func__, adapter->ahw->total_nic_func, count);
468 if (!adapter->npars[i].eswitch_status)
471 pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
472 pm_cfg[pci_func].dest_npar = 0;
473 pm_cfg[pci_func].pci_func = i;
478 static int validate_esw_config(struct qlcnic_adapter *adapter,
479 struct qlcnic_esw_func_cfg *esw_cfg, int count)
481 struct qlcnic_hardware_context *ahw = adapter->ahw;
486 if (qlcnic_82xx_check(adapter))
487 op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
489 op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
491 for (i = 0; i < count; i++) {
492 pci_func = esw_cfg[i].pci_func;
493 if (pci_func >= ahw->max_vnic_func)
494 return QL_STATUS_INVALID_PARAM;
496 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
497 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
498 return QL_STATUS_INVALID_PARAM;
500 switch (esw_cfg[i].op_mode) {
501 case QLCNIC_PORT_DEFAULTS:
502 if (qlcnic_82xx_check(adapter)) {
503 ret = QLC_DEV_GET_DRV(op_mode, pci_func);
505 ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
507 esw_cfg[i].offload_flags = 0;
510 if (ret != QLCNIC_NON_PRIV_FUNC) {
511 if (esw_cfg[i].mac_anti_spoof != 0)
512 return QL_STATUS_INVALID_PARAM;
513 if (esw_cfg[i].mac_override != 1)
514 return QL_STATUS_INVALID_PARAM;
515 if (esw_cfg[i].promisc_mode != 1)
516 return QL_STATUS_INVALID_PARAM;
519 case QLCNIC_ADD_VLAN:
520 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
521 return QL_STATUS_INVALID_PARAM;
522 if (!esw_cfg[i].op_type)
523 return QL_STATUS_INVALID_PARAM;
525 case QLCNIC_DEL_VLAN:
526 if (!esw_cfg[i].op_type)
527 return QL_STATUS_INVALID_PARAM;
530 return QL_STATUS_INVALID_PARAM;
537 static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
538 struct kobject *kobj,
539 struct bin_attribute *attr,
540 char *buf, loff_t offset,
543 struct device *dev = container_of(kobj, struct device, kobj);
544 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
545 struct qlcnic_esw_func_cfg *esw_cfg;
546 struct qlcnic_npar_info *npar;
547 int count, rem, i, ret;
549 u8 op_mode = 0, pci_func;
551 count = size / sizeof(struct qlcnic_esw_func_cfg);
552 rem = size % sizeof(struct qlcnic_esw_func_cfg);
554 return QL_STATUS_INVALID_PARAM;
556 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
557 ret = validate_esw_config(adapter, esw_cfg, count);
561 for (i = 0; i < count; i++) {
562 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
563 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
564 return QL_STATUS_INVALID_PARAM;
566 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
569 op_mode = esw_cfg[i].op_mode;
570 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
571 esw_cfg[i].op_mode = op_mode;
572 esw_cfg[i].pci_func = adapter->ahw->pci_func;
574 switch (esw_cfg[i].op_mode) {
575 case QLCNIC_PORT_DEFAULTS:
576 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
578 qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
581 case QLCNIC_ADD_VLAN:
582 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
584 case QLCNIC_DEL_VLAN:
585 esw_cfg[i].vlan_id = 0;
586 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
591 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
594 for (i = 0; i < count; i++) {
595 pci_func = esw_cfg[i].pci_func;
596 index = qlcnic_is_valid_nic_func(adapter, pci_func);
598 return QL_STATUS_INVALID_PARAM;
599 npar = &adapter->npars[index];
600 switch (esw_cfg[i].op_mode) {
601 case QLCNIC_PORT_DEFAULTS:
602 npar->promisc_mode = esw_cfg[i].promisc_mode;
603 npar->mac_override = esw_cfg[i].mac_override;
604 npar->offload_flags = esw_cfg[i].offload_flags;
605 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
606 npar->discard_tagged = esw_cfg[i].discard_tagged;
608 case QLCNIC_ADD_VLAN:
609 npar->pvid = esw_cfg[i].vlan_id;
611 case QLCNIC_DEL_VLAN:
620 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
621 struct kobject *kobj,
622 struct bin_attribute *attr,
623 char *buf, loff_t offset,
626 struct device *dev = container_of(kobj, struct device, kobj);
627 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
628 struct qlcnic_esw_func_cfg *esw_cfg;
633 memset(buf, 0, size);
634 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
635 count = size / sizeof(struct qlcnic_esw_func_cfg);
636 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
637 pci_func = adapter->npars[i].pci_func;
638 if (pci_func >= count) {
639 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
640 __func__, adapter->ahw->total_nic_func, count);
643 if (!adapter->npars[i].eswitch_status)
646 esw_cfg[pci_func].pci_func = pci_func;
647 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
648 return QL_STATUS_INVALID_PARAM;
653 static int validate_npar_config(struct qlcnic_adapter *adapter,
654 struct qlcnic_npar_func_cfg *np_cfg,
659 for (i = 0; i < count; i++) {
660 pci_func = np_cfg[i].pci_func;
661 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
662 return QL_STATUS_INVALID_PARAM;
664 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
665 !IS_VALID_BW(np_cfg[i].max_bw))
666 return QL_STATUS_INVALID_PARAM;
671 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
672 struct kobject *kobj,
673 struct bin_attribute *attr,
674 char *buf, loff_t offset,
677 struct device *dev = container_of(kobj, struct device, kobj);
678 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
679 struct qlcnic_info nic_info;
680 struct qlcnic_npar_func_cfg *np_cfg;
681 int i, count, rem, ret, index;
684 count = size / sizeof(struct qlcnic_npar_func_cfg);
685 rem = size % sizeof(struct qlcnic_npar_func_cfg);
687 return QL_STATUS_INVALID_PARAM;
689 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
690 ret = validate_npar_config(adapter, np_cfg, count);
694 for (i = 0; i < count; i++) {
695 pci_func = np_cfg[i].pci_func;
697 memset(&nic_info, 0, sizeof(struct qlcnic_info));
698 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
701 nic_info.pci_func = pci_func;
702 nic_info.min_tx_bw = np_cfg[i].min_bw;
703 nic_info.max_tx_bw = np_cfg[i].max_bw;
704 ret = qlcnic_set_nic_info(adapter, &nic_info);
707 index = qlcnic_is_valid_nic_func(adapter, pci_func);
709 return QL_STATUS_INVALID_PARAM;
710 adapter->npars[index].min_bw = nic_info.min_tx_bw;
711 adapter->npars[index].max_bw = nic_info.max_tx_bw;
717 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
718 struct kobject *kobj,
719 struct bin_attribute *attr,
720 char *buf, loff_t offset,
723 struct device *dev = container_of(kobj, struct device, kobj);
724 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
725 struct qlcnic_npar_func_cfg *np_cfg;
726 struct qlcnic_info nic_info;
731 memset(&nic_info, 0, sizeof(struct qlcnic_info));
732 memset(buf, 0, size);
733 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
735 count = size / sizeof(struct qlcnic_npar_func_cfg);
736 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
737 if (adapter->npars[i].pci_func >= count) {
738 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
739 __func__, adapter->ahw->total_nic_func, count);
742 if (!adapter->npars[i].eswitch_status)
744 pci_func = adapter->npars[i].pci_func;
745 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
747 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
751 np_cfg[pci_func].pci_func = pci_func;
752 np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
753 np_cfg[pci_func].port_num = nic_info.phys_port;
754 np_cfg[pci_func].fw_capab = nic_info.capabilities;
755 np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
756 np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
757 np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
758 np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
763 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
764 struct kobject *kobj,
765 struct bin_attribute *attr,
766 char *buf, loff_t offset,
769 struct device *dev = container_of(kobj, struct device, kobj);
770 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
771 struct qlcnic_esw_statistics port_stats;
774 if (qlcnic_83xx_check(adapter))
775 return QLC_STATUS_UNSUPPORTED_CMD;
777 if (size != sizeof(struct qlcnic_esw_statistics))
778 return QL_STATUS_INVALID_PARAM;
780 if (offset >= adapter->ahw->max_vnic_func)
781 return QL_STATUS_INVALID_PARAM;
783 memset(&port_stats, 0, size);
784 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
789 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
794 memcpy(buf, &port_stats, size);
798 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
799 struct kobject *kobj,
800 struct bin_attribute *attr,
801 char *buf, loff_t offset,
804 struct device *dev = container_of(kobj, struct device, kobj);
805 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
806 struct qlcnic_esw_statistics esw_stats;
809 if (qlcnic_83xx_check(adapter))
810 return QLC_STATUS_UNSUPPORTED_CMD;
812 if (size != sizeof(struct qlcnic_esw_statistics))
813 return QL_STATUS_INVALID_PARAM;
815 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
816 return QL_STATUS_INVALID_PARAM;
818 memset(&esw_stats, 0, size);
819 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
824 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
829 memcpy(buf, &esw_stats, size);
833 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
834 struct kobject *kobj,
835 struct bin_attribute *attr,
836 char *buf, loff_t offset,
839 struct device *dev = container_of(kobj, struct device, kobj);
840 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
843 if (qlcnic_83xx_check(adapter))
844 return QLC_STATUS_UNSUPPORTED_CMD;
846 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
847 return QL_STATUS_INVALID_PARAM;
849 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
850 QLCNIC_QUERY_RX_COUNTER);
854 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
855 QLCNIC_QUERY_TX_COUNTER);
862 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
863 struct kobject *kobj,
864 struct bin_attribute *attr,
865 char *buf, loff_t offset,
869 struct device *dev = container_of(kobj, struct device, kobj);
870 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
873 if (qlcnic_83xx_check(adapter))
874 return QLC_STATUS_UNSUPPORTED_CMD;
876 if (offset >= adapter->ahw->max_vnic_func)
877 return QL_STATUS_INVALID_PARAM;
879 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
880 QLCNIC_QUERY_RX_COUNTER);
884 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
885 QLCNIC_QUERY_TX_COUNTER);
892 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
893 struct kobject *kobj,
894 struct bin_attribute *attr,
895 char *buf, loff_t offset,
898 struct device *dev = container_of(kobj, struct device, kobj);
899 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
900 struct qlcnic_pci_func_cfg *pci_cfg;
901 struct qlcnic_pci_info *pci_info;
905 pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
909 ret = qlcnic_get_pci_info(adapter, pci_info);
915 pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
916 count = size / sizeof(struct qlcnic_pci_func_cfg);
917 for (i = 0; i < count; i++) {
918 pci_cfg[i].pci_func = pci_info[i].id;
919 pci_cfg[i].func_type = pci_info[i].type;
920 pci_cfg[i].func_state = 0;
921 pci_cfg[i].port_num = pci_info[i].default_port;
922 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
923 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
924 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
931 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
932 struct kobject *kobj,
933 struct bin_attribute *attr,
934 char *buf, loff_t offset,
937 unsigned char *p_read_buf;
939 struct device *dev = container_of(kobj, struct device, kobj);
940 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
943 return QL_STATUS_INVALID_PARAM;
945 return QL_STATUS_INVALID_PARAM;
947 count = size / sizeof(u32);
949 if (size % sizeof(u32))
952 p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
955 if (qlcnic_83xx_lock_flash(adapter) != 0) {
960 ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
964 qlcnic_83xx_unlock_flash(adapter);
969 qlcnic_83xx_unlock_flash(adapter);
970 memcpy(buf, p_read_buf, size);
976 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
977 char *buf, loff_t offset,
981 unsigned char *p_cache, *p_src;
983 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
987 memcpy(p_cache, buf, size);
989 count = size / sizeof(u32);
991 if (qlcnic_83xx_lock_flash(adapter) != 0) {
996 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
997 ret = qlcnic_83xx_enable_flash_write(adapter);
1000 qlcnic_83xx_unlock_flash(adapter);
1005 for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1006 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1008 QLC_83XX_FLASH_WRITE_MAX);
1011 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1012 ret = qlcnic_83xx_disable_flash_write(adapter);
1015 qlcnic_83xx_unlock_flash(adapter);
1021 qlcnic_83xx_unlock_flash(adapter);
1025 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1026 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1029 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1030 ret = qlcnic_83xx_disable_flash_write(adapter);
1033 qlcnic_83xx_unlock_flash(adapter);
1039 qlcnic_83xx_unlock_flash(adapter);
1044 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1045 char *buf, loff_t offset, size_t size)
1048 unsigned char *p_cache, *p_src;
1050 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1054 memcpy(p_cache, buf, size);
1056 count = size / sizeof(u32);
1058 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1063 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1064 ret = qlcnic_83xx_enable_flash_write(adapter);
1067 qlcnic_83xx_unlock_flash(adapter);
1072 for (i = 0; i < count; i++) {
1073 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1075 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1076 ret = qlcnic_83xx_disable_flash_write(adapter);
1079 qlcnic_83xx_unlock_flash(adapter);
1084 qlcnic_83xx_unlock_flash(adapter);
1088 p_src = p_src + sizeof(u32);
1089 offset = offset + sizeof(u32);
1092 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1093 ret = qlcnic_83xx_disable_flash_write(adapter);
1096 qlcnic_83xx_unlock_flash(adapter);
1102 qlcnic_83xx_unlock_flash(adapter);
1107 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1108 struct kobject *kobj,
1109 struct bin_attribute *attr,
1110 char *buf, loff_t offset,
1114 static int flash_mode;
1116 struct device *dev = container_of(kobj, struct device, kobj);
1117 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1120 return QL_STATUS_INVALID_PARAM;
1122 ret = kstrtoul(buf, 16, &data);
1125 case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1126 flash_mode = QLC_83XX_ERASE_MODE;
1127 ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1129 dev_err(&adapter->pdev->dev,
1130 "%s failed at %d\n", __func__, __LINE__);
1135 case QLC_83XX_FLASH_BULK_WRITE_CMD:
1136 flash_mode = QLC_83XX_BULK_WRITE_MODE;
1139 case QLC_83XX_FLASH_WRITE_CMD:
1140 flash_mode = QLC_83XX_WRITE_MODE;
1143 if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1144 ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1147 dev_err(&adapter->pdev->dev,
1148 "%s failed at %d\n",
1149 __func__, __LINE__);
1154 if (flash_mode == QLC_83XX_WRITE_MODE) {
1155 ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1158 dev_err(&adapter->pdev->dev,
1159 "%s failed at %d\n", __func__,
1169 static struct device_attribute dev_attr_bridged_mode = {
1170 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1171 .show = qlcnic_show_bridged_mode,
1172 .store = qlcnic_store_bridged_mode,
1175 static struct device_attribute dev_attr_diag_mode = {
1176 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1177 .show = qlcnic_show_diag_mode,
1178 .store = qlcnic_store_diag_mode,
1181 static struct device_attribute dev_attr_beacon = {
1182 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1183 .show = qlcnic_show_beacon,
1184 .store = qlcnic_store_beacon,
1187 static struct bin_attribute bin_attr_crb = {
1188 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1190 .read = qlcnic_sysfs_read_crb,
1191 .write = qlcnic_sysfs_write_crb,
1194 static struct bin_attribute bin_attr_mem = {
1195 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1197 .read = qlcnic_sysfs_read_mem,
1198 .write = qlcnic_sysfs_write_mem,
1201 static struct bin_attribute bin_attr_npar_config = {
1202 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1204 .read = qlcnic_sysfs_read_npar_config,
1205 .write = qlcnic_sysfs_write_npar_config,
1208 static struct bin_attribute bin_attr_pci_config = {
1209 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1211 .read = qlcnic_sysfs_read_pci_config,
1215 static struct bin_attribute bin_attr_port_stats = {
1216 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1218 .read = qlcnic_sysfs_get_port_stats,
1219 .write = qlcnic_sysfs_clear_port_stats,
1222 static struct bin_attribute bin_attr_esw_stats = {
1223 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1225 .read = qlcnic_sysfs_get_esw_stats,
1226 .write = qlcnic_sysfs_clear_esw_stats,
1229 static struct bin_attribute bin_attr_esw_config = {
1230 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1232 .read = qlcnic_sysfs_read_esw_config,
1233 .write = qlcnic_sysfs_write_esw_config,
1236 static struct bin_attribute bin_attr_pm_config = {
1237 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1239 .read = qlcnic_sysfs_read_pm_config,
1240 .write = qlcnic_sysfs_write_pm_config,
1243 static struct bin_attribute bin_attr_flash = {
1244 .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1246 .read = qlcnic_83xx_sysfs_flash_read_handler,
1247 .write = qlcnic_83xx_sysfs_flash_write_handler,
1250 #ifdef CONFIG_QLCNIC_HWMON
1252 static ssize_t qlcnic_hwmon_show_temp(struct device *dev,
1253 struct device_attribute *dev_attr,
1256 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1257 unsigned int temperature = 0, value = 0;
1259 if (qlcnic_83xx_check(adapter))
1260 value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
1261 else if (qlcnic_82xx_check(adapter))
1262 value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
1264 temperature = qlcnic_get_temp_val(value);
1265 /* display millidegree celcius */
1266 temperature *= 1000;
1267 return sprintf(buf, "%u\n", temperature);
1270 /* hwmon-sysfs attributes */
1271 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1272 qlcnic_hwmon_show_temp, NULL, 1);
1274 static struct attribute *qlcnic_hwmon_attrs[] = {
1275 &sensor_dev_attr_temp1_input.dev_attr.attr,
1279 ATTRIBUTE_GROUPS(qlcnic_hwmon);
1281 void qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
1283 struct device *dev = &adapter->pdev->dev;
1284 struct device *hwmon_dev;
1286 /* Skip hwmon registration for a VF device */
1287 if (qlcnic_sriov_vf_check(adapter)) {
1288 adapter->ahw->hwmon_dev = NULL;
1291 hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
1293 qlcnic_hwmon_groups);
1294 if (IS_ERR(hwmon_dev)) {
1295 dev_err(dev, "Cannot register with hwmon, err=%ld\n",
1296 PTR_ERR(hwmon_dev));
1299 adapter->ahw->hwmon_dev = hwmon_dev;
1302 void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
1304 struct device *hwmon_dev = adapter->ahw->hwmon_dev;
1306 hwmon_device_unregister(hwmon_dev);
1307 adapter->ahw->hwmon_dev = NULL;
1312 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1314 struct device *dev = &adapter->pdev->dev;
1316 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1317 if (device_create_file(dev, &dev_attr_bridged_mode))
1319 "failed to create bridged_mode sysfs entry\n");
1322 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1324 struct device *dev = &adapter->pdev->dev;
1326 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1327 device_remove_file(dev, &dev_attr_bridged_mode);
1330 static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1332 struct device *dev = &adapter->pdev->dev;
1334 if (device_create_bin_file(dev, &bin_attr_port_stats))
1335 dev_info(dev, "failed to create port stats sysfs entry");
1337 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1339 if (device_create_file(dev, &dev_attr_diag_mode))
1340 dev_info(dev, "failed to create diag_mode sysfs entry\n");
1341 if (device_create_bin_file(dev, &bin_attr_crb))
1342 dev_info(dev, "failed to create crb sysfs entry\n");
1343 if (device_create_bin_file(dev, &bin_attr_mem))
1344 dev_info(dev, "failed to create mem sysfs entry\n");
1346 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1349 if (device_create_bin_file(dev, &bin_attr_pci_config))
1350 dev_info(dev, "failed to create pci config sysfs entry");
1352 if (device_create_file(dev, &dev_attr_beacon))
1353 dev_info(dev, "failed to create beacon sysfs entry");
1355 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1357 if (device_create_bin_file(dev, &bin_attr_esw_config))
1358 dev_info(dev, "failed to create esw config sysfs entry");
1359 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1361 if (device_create_bin_file(dev, &bin_attr_npar_config))
1362 dev_info(dev, "failed to create npar config sysfs entry");
1363 if (device_create_bin_file(dev, &bin_attr_pm_config))
1364 dev_info(dev, "failed to create pm config sysfs entry");
1365 if (device_create_bin_file(dev, &bin_attr_esw_stats))
1366 dev_info(dev, "failed to create eswitch stats sysfs entry");
1369 static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1371 struct device *dev = &adapter->pdev->dev;
1373 device_remove_bin_file(dev, &bin_attr_port_stats);
1375 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1377 device_remove_file(dev, &dev_attr_diag_mode);
1378 device_remove_bin_file(dev, &bin_attr_crb);
1379 device_remove_bin_file(dev, &bin_attr_mem);
1381 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1384 device_remove_bin_file(dev, &bin_attr_pci_config);
1385 device_remove_file(dev, &dev_attr_beacon);
1386 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1388 device_remove_bin_file(dev, &bin_attr_esw_config);
1389 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1391 device_remove_bin_file(dev, &bin_attr_npar_config);
1392 device_remove_bin_file(dev, &bin_attr_pm_config);
1393 device_remove_bin_file(dev, &bin_attr_esw_stats);
1396 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1398 qlcnic_create_diag_entries(adapter);
1401 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1403 qlcnic_remove_diag_entries(adapter);
1406 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1408 struct device *dev = &adapter->pdev->dev;
1410 qlcnic_create_diag_entries(adapter);
1412 if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1413 dev_info(dev, "failed to create flash sysfs entry\n");
1416 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1418 struct device *dev = &adapter->pdev->dev;
1420 qlcnic_remove_diag_entries(adapter);
1421 sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);