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)
366 dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
370 static int validate_pm_config(struct qlcnic_adapter *adapter,
371 struct qlcnic_pm_func_cfg *pm_cfg, int count)
373 u8 src_pci_func, s_esw_id, d_esw_id;
375 int i, src_index, dest_index;
377 for (i = 0; i < count; i++) {
378 src_pci_func = pm_cfg[i].pci_func;
379 dest_pci_func = pm_cfg[i].dest_npar;
380 src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
382 return QL_STATUS_INVALID_PARAM;
384 dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
386 return QL_STATUS_INVALID_PARAM;
388 s_esw_id = adapter->npars[src_index].phy_port;
389 d_esw_id = adapter->npars[dest_index].phy_port;
391 if (s_esw_id != d_esw_id)
392 return QL_STATUS_INVALID_PARAM;
398 static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
399 struct kobject *kobj,
400 struct bin_attribute *attr,
401 char *buf, loff_t offset,
404 struct device *dev = container_of(kobj, struct device, kobj);
405 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
406 struct qlcnic_pm_func_cfg *pm_cfg;
407 u32 id, action, pci_func;
408 int count, rem, i, ret, index;
410 count = size / sizeof(struct qlcnic_pm_func_cfg);
411 rem = size % sizeof(struct qlcnic_pm_func_cfg);
413 return QL_STATUS_INVALID_PARAM;
415 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
416 ret = validate_pm_config(adapter, pm_cfg, count);
420 for (i = 0; i < count; i++) {
421 pci_func = pm_cfg[i].pci_func;
422 action = !!pm_cfg[i].action;
423 index = qlcnic_is_valid_nic_func(adapter, pci_func);
425 return QL_STATUS_INVALID_PARAM;
427 id = adapter->npars[index].phy_port;
428 ret = qlcnic_config_port_mirroring(adapter, id,
434 for (i = 0; i < count; i++) {
435 pci_func = pm_cfg[i].pci_func;
436 index = qlcnic_is_valid_nic_func(adapter, pci_func);
438 return QL_STATUS_INVALID_PARAM;
439 id = adapter->npars[index].phy_port;
440 adapter->npars[index].enable_pm = !!pm_cfg[i].action;
441 adapter->npars[index].dest_npar = id;
447 static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
448 struct kobject *kobj,
449 struct bin_attribute *attr,
450 char *buf, loff_t offset,
453 struct device *dev = container_of(kobj, struct device, kobj);
454 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
455 struct qlcnic_pm_func_cfg *pm_cfg;
460 memset(buf, 0, size);
461 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
462 count = size / sizeof(struct qlcnic_pm_func_cfg);
463 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
464 pci_func = adapter->npars[i].pci_func;
465 if (pci_func >= count) {
466 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
467 __func__, adapter->ahw->total_nic_func, count);
470 if (!adapter->npars[i].eswitch_status)
473 pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
474 pm_cfg[pci_func].dest_npar = 0;
475 pm_cfg[pci_func].pci_func = i;
480 static int validate_esw_config(struct qlcnic_adapter *adapter,
481 struct qlcnic_esw_func_cfg *esw_cfg, int count)
483 struct qlcnic_hardware_context *ahw = adapter->ahw;
488 if (qlcnic_82xx_check(adapter))
489 op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
491 op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
493 for (i = 0; i < count; i++) {
494 pci_func = esw_cfg[i].pci_func;
495 if (pci_func >= ahw->max_vnic_func)
496 return QL_STATUS_INVALID_PARAM;
498 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
499 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
500 return QL_STATUS_INVALID_PARAM;
502 switch (esw_cfg[i].op_mode) {
503 case QLCNIC_PORT_DEFAULTS:
504 if (qlcnic_82xx_check(adapter)) {
505 ret = QLC_DEV_GET_DRV(op_mode, pci_func);
507 ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
509 esw_cfg[i].offload_flags = 0;
512 if (ret != QLCNIC_NON_PRIV_FUNC) {
513 if (esw_cfg[i].mac_anti_spoof != 0)
514 return QL_STATUS_INVALID_PARAM;
515 if (esw_cfg[i].mac_override != 1)
516 return QL_STATUS_INVALID_PARAM;
517 if (esw_cfg[i].promisc_mode != 1)
518 return QL_STATUS_INVALID_PARAM;
521 case QLCNIC_ADD_VLAN:
522 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
523 return QL_STATUS_INVALID_PARAM;
524 if (!esw_cfg[i].op_type)
525 return QL_STATUS_INVALID_PARAM;
527 case QLCNIC_DEL_VLAN:
528 if (!esw_cfg[i].op_type)
529 return QL_STATUS_INVALID_PARAM;
532 return QL_STATUS_INVALID_PARAM;
539 static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
540 struct kobject *kobj,
541 struct bin_attribute *attr,
542 char *buf, loff_t offset,
545 struct device *dev = container_of(kobj, struct device, kobj);
546 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
547 struct qlcnic_esw_func_cfg *esw_cfg;
548 struct qlcnic_npar_info *npar;
549 int count, rem, i, ret;
551 u8 op_mode = 0, pci_func;
553 count = size / sizeof(struct qlcnic_esw_func_cfg);
554 rem = size % sizeof(struct qlcnic_esw_func_cfg);
556 return QL_STATUS_INVALID_PARAM;
558 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
559 ret = validate_esw_config(adapter, esw_cfg, count);
563 for (i = 0; i < count; i++) {
564 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
565 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
566 return QL_STATUS_INVALID_PARAM;
568 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
571 op_mode = esw_cfg[i].op_mode;
572 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
573 esw_cfg[i].op_mode = op_mode;
574 esw_cfg[i].pci_func = adapter->ahw->pci_func;
576 switch (esw_cfg[i].op_mode) {
577 case QLCNIC_PORT_DEFAULTS:
578 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
580 qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
583 case QLCNIC_ADD_VLAN:
584 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
586 case QLCNIC_DEL_VLAN:
587 esw_cfg[i].vlan_id = 0;
588 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
593 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
596 for (i = 0; i < count; i++) {
597 pci_func = esw_cfg[i].pci_func;
598 index = qlcnic_is_valid_nic_func(adapter, pci_func);
600 return QL_STATUS_INVALID_PARAM;
601 npar = &adapter->npars[index];
602 switch (esw_cfg[i].op_mode) {
603 case QLCNIC_PORT_DEFAULTS:
604 npar->promisc_mode = esw_cfg[i].promisc_mode;
605 npar->mac_override = esw_cfg[i].mac_override;
606 npar->offload_flags = esw_cfg[i].offload_flags;
607 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
608 npar->discard_tagged = esw_cfg[i].discard_tagged;
610 case QLCNIC_ADD_VLAN:
611 npar->pvid = esw_cfg[i].vlan_id;
613 case QLCNIC_DEL_VLAN:
622 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
623 struct kobject *kobj,
624 struct bin_attribute *attr,
625 char *buf, loff_t offset,
628 struct device *dev = container_of(kobj, struct device, kobj);
629 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
630 struct qlcnic_esw_func_cfg *esw_cfg;
635 memset(buf, 0, size);
636 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
637 count = size / sizeof(struct qlcnic_esw_func_cfg);
638 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
639 pci_func = adapter->npars[i].pci_func;
640 if (pci_func >= count) {
641 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
642 __func__, adapter->ahw->total_nic_func, count);
645 if (!adapter->npars[i].eswitch_status)
648 esw_cfg[pci_func].pci_func = pci_func;
649 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
650 return QL_STATUS_INVALID_PARAM;
655 static int validate_npar_config(struct qlcnic_adapter *adapter,
656 struct qlcnic_npar_func_cfg *np_cfg,
661 for (i = 0; i < count; i++) {
662 pci_func = np_cfg[i].pci_func;
663 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
664 return QL_STATUS_INVALID_PARAM;
666 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
667 !IS_VALID_BW(np_cfg[i].max_bw))
668 return QL_STATUS_INVALID_PARAM;
673 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
674 struct kobject *kobj,
675 struct bin_attribute *attr,
676 char *buf, loff_t offset,
679 struct device *dev = container_of(kobj, struct device, kobj);
680 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
681 struct qlcnic_info nic_info;
682 struct qlcnic_npar_func_cfg *np_cfg;
683 int i, count, rem, ret, index;
686 count = size / sizeof(struct qlcnic_npar_func_cfg);
687 rem = size % sizeof(struct qlcnic_npar_func_cfg);
689 return QL_STATUS_INVALID_PARAM;
691 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
692 ret = validate_npar_config(adapter, np_cfg, count);
696 for (i = 0; i < count; i++) {
697 pci_func = np_cfg[i].pci_func;
699 memset(&nic_info, 0, sizeof(struct qlcnic_info));
700 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
703 nic_info.pci_func = pci_func;
704 nic_info.min_tx_bw = np_cfg[i].min_bw;
705 nic_info.max_tx_bw = np_cfg[i].max_bw;
706 ret = qlcnic_set_nic_info(adapter, &nic_info);
709 index = qlcnic_is_valid_nic_func(adapter, pci_func);
711 return QL_STATUS_INVALID_PARAM;
712 adapter->npars[index].min_bw = nic_info.min_tx_bw;
713 adapter->npars[index].max_bw = nic_info.max_tx_bw;
719 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
720 struct kobject *kobj,
721 struct bin_attribute *attr,
722 char *buf, loff_t offset,
725 struct device *dev = container_of(kobj, struct device, kobj);
726 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
727 struct qlcnic_npar_func_cfg *np_cfg;
728 struct qlcnic_info nic_info;
733 memset(&nic_info, 0, sizeof(struct qlcnic_info));
734 memset(buf, 0, size);
735 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
737 count = size / sizeof(struct qlcnic_npar_func_cfg);
738 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
739 if (adapter->npars[i].pci_func >= count) {
740 dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
741 __func__, adapter->ahw->total_nic_func, count);
744 if (!adapter->npars[i].eswitch_status)
746 pci_func = adapter->npars[i].pci_func;
747 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
749 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
753 np_cfg[pci_func].pci_func = pci_func;
754 np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
755 np_cfg[pci_func].port_num = nic_info.phys_port;
756 np_cfg[pci_func].fw_capab = nic_info.capabilities;
757 np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
758 np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
759 np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
760 np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
765 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
766 struct kobject *kobj,
767 struct bin_attribute *attr,
768 char *buf, loff_t offset,
771 struct device *dev = container_of(kobj, struct device, kobj);
772 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
773 struct qlcnic_esw_statistics port_stats;
776 if (qlcnic_83xx_check(adapter))
777 return QLC_STATUS_UNSUPPORTED_CMD;
779 if (size != sizeof(struct qlcnic_esw_statistics))
780 return QL_STATUS_INVALID_PARAM;
782 if (offset >= adapter->ahw->max_vnic_func)
783 return QL_STATUS_INVALID_PARAM;
785 memset(&port_stats, 0, size);
786 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
791 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
796 memcpy(buf, &port_stats, size);
800 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
801 struct kobject *kobj,
802 struct bin_attribute *attr,
803 char *buf, loff_t offset,
806 struct device *dev = container_of(kobj, struct device, kobj);
807 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
808 struct qlcnic_esw_statistics esw_stats;
811 if (qlcnic_83xx_check(adapter))
812 return QLC_STATUS_UNSUPPORTED_CMD;
814 if (size != sizeof(struct qlcnic_esw_statistics))
815 return QL_STATUS_INVALID_PARAM;
817 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
818 return QL_STATUS_INVALID_PARAM;
820 memset(&esw_stats, 0, size);
821 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
826 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
831 memcpy(buf, &esw_stats, size);
835 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
836 struct kobject *kobj,
837 struct bin_attribute *attr,
838 char *buf, loff_t offset,
841 struct device *dev = container_of(kobj, struct device, kobj);
842 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
845 if (qlcnic_83xx_check(adapter))
846 return QLC_STATUS_UNSUPPORTED_CMD;
848 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
849 return QL_STATUS_INVALID_PARAM;
851 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
852 QLCNIC_QUERY_RX_COUNTER);
856 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
857 QLCNIC_QUERY_TX_COUNTER);
864 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
865 struct kobject *kobj,
866 struct bin_attribute *attr,
867 char *buf, loff_t offset,
871 struct device *dev = container_of(kobj, struct device, kobj);
872 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
875 if (qlcnic_83xx_check(adapter))
876 return QLC_STATUS_UNSUPPORTED_CMD;
878 if (offset >= adapter->ahw->max_vnic_func)
879 return QL_STATUS_INVALID_PARAM;
881 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
882 QLCNIC_QUERY_RX_COUNTER);
886 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
887 QLCNIC_QUERY_TX_COUNTER);
894 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
895 struct kobject *kobj,
896 struct bin_attribute *attr,
897 char *buf, loff_t offset,
900 struct device *dev = container_of(kobj, struct device, kobj);
901 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
902 struct qlcnic_pci_func_cfg *pci_cfg;
903 struct qlcnic_pci_info *pci_info;
907 pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
911 ret = qlcnic_get_pci_info(adapter, pci_info);
917 pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
918 count = size / sizeof(struct qlcnic_pci_func_cfg);
919 for (i = 0; i < count; i++) {
920 pci_cfg[i].pci_func = pci_info[i].id;
921 pci_cfg[i].func_type = pci_info[i].type;
922 pci_cfg[i].func_state = 0;
923 pci_cfg[i].port_num = pci_info[i].default_port;
924 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
925 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
926 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
933 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
934 struct kobject *kobj,
935 struct bin_attribute *attr,
936 char *buf, loff_t offset,
939 unsigned char *p_read_buf;
941 struct device *dev = container_of(kobj, struct device, kobj);
942 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
945 return QL_STATUS_INVALID_PARAM;
947 return QL_STATUS_INVALID_PARAM;
949 count = size / sizeof(u32);
951 if (size % sizeof(u32))
954 p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
957 if (qlcnic_83xx_lock_flash(adapter) != 0) {
962 ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
966 qlcnic_83xx_unlock_flash(adapter);
971 qlcnic_83xx_unlock_flash(adapter);
972 memcpy(buf, p_read_buf, size);
978 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
979 char *buf, loff_t offset,
983 unsigned char *p_cache, *p_src;
985 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
989 memcpy(p_cache, buf, size);
991 count = size / sizeof(u32);
993 if (qlcnic_83xx_lock_flash(adapter) != 0) {
998 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
999 ret = qlcnic_83xx_enable_flash_write(adapter);
1002 qlcnic_83xx_unlock_flash(adapter);
1007 for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1008 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1010 QLC_83XX_FLASH_WRITE_MAX);
1013 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1014 ret = qlcnic_83xx_disable_flash_write(adapter);
1017 qlcnic_83xx_unlock_flash(adapter);
1023 qlcnic_83xx_unlock_flash(adapter);
1027 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1028 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1031 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1032 ret = qlcnic_83xx_disable_flash_write(adapter);
1035 qlcnic_83xx_unlock_flash(adapter);
1041 qlcnic_83xx_unlock_flash(adapter);
1046 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1047 char *buf, loff_t offset, size_t size)
1050 unsigned char *p_cache, *p_src;
1052 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1056 memcpy(p_cache, buf, size);
1058 count = size / sizeof(u32);
1060 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1065 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1066 ret = qlcnic_83xx_enable_flash_write(adapter);
1069 qlcnic_83xx_unlock_flash(adapter);
1074 for (i = 0; i < count; i++) {
1075 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1077 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1078 ret = qlcnic_83xx_disable_flash_write(adapter);
1081 qlcnic_83xx_unlock_flash(adapter);
1086 qlcnic_83xx_unlock_flash(adapter);
1090 p_src = p_src + sizeof(u32);
1091 offset = offset + sizeof(u32);
1094 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1095 ret = qlcnic_83xx_disable_flash_write(adapter);
1098 qlcnic_83xx_unlock_flash(adapter);
1104 qlcnic_83xx_unlock_flash(adapter);
1109 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1110 struct kobject *kobj,
1111 struct bin_attribute *attr,
1112 char *buf, loff_t offset,
1116 static int flash_mode;
1118 struct device *dev = container_of(kobj, struct device, kobj);
1119 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1122 return QL_STATUS_INVALID_PARAM;
1124 ret = kstrtoul(buf, 16, &data);
1127 case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1128 flash_mode = QLC_83XX_ERASE_MODE;
1129 ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1131 dev_err(&adapter->pdev->dev,
1132 "%s failed at %d\n", __func__, __LINE__);
1137 case QLC_83XX_FLASH_BULK_WRITE_CMD:
1138 flash_mode = QLC_83XX_BULK_WRITE_MODE;
1141 case QLC_83XX_FLASH_WRITE_CMD:
1142 flash_mode = QLC_83XX_WRITE_MODE;
1145 if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1146 ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1149 dev_err(&adapter->pdev->dev,
1150 "%s failed at %d\n",
1151 __func__, __LINE__);
1156 if (flash_mode == QLC_83XX_WRITE_MODE) {
1157 ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1160 dev_err(&adapter->pdev->dev,
1161 "%s failed at %d\n", __func__,
1171 static struct device_attribute dev_attr_bridged_mode = {
1172 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1173 .show = qlcnic_show_bridged_mode,
1174 .store = qlcnic_store_bridged_mode,
1177 static struct device_attribute dev_attr_diag_mode = {
1178 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1179 .show = qlcnic_show_diag_mode,
1180 .store = qlcnic_store_diag_mode,
1183 static struct device_attribute dev_attr_beacon = {
1184 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1185 .show = qlcnic_show_beacon,
1186 .store = qlcnic_store_beacon,
1189 static struct bin_attribute bin_attr_crb = {
1190 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1192 .read = qlcnic_sysfs_read_crb,
1193 .write = qlcnic_sysfs_write_crb,
1196 static struct bin_attribute bin_attr_mem = {
1197 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1199 .read = qlcnic_sysfs_read_mem,
1200 .write = qlcnic_sysfs_write_mem,
1203 static struct bin_attribute bin_attr_npar_config = {
1204 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1206 .read = qlcnic_sysfs_read_npar_config,
1207 .write = qlcnic_sysfs_write_npar_config,
1210 static struct bin_attribute bin_attr_pci_config = {
1211 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1213 .read = qlcnic_sysfs_read_pci_config,
1217 static struct bin_attribute bin_attr_port_stats = {
1218 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1220 .read = qlcnic_sysfs_get_port_stats,
1221 .write = qlcnic_sysfs_clear_port_stats,
1224 static struct bin_attribute bin_attr_esw_stats = {
1225 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1227 .read = qlcnic_sysfs_get_esw_stats,
1228 .write = qlcnic_sysfs_clear_esw_stats,
1231 static struct bin_attribute bin_attr_esw_config = {
1232 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1234 .read = qlcnic_sysfs_read_esw_config,
1235 .write = qlcnic_sysfs_write_esw_config,
1238 static struct bin_attribute bin_attr_pm_config = {
1239 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1241 .read = qlcnic_sysfs_read_pm_config,
1242 .write = qlcnic_sysfs_write_pm_config,
1245 static struct bin_attribute bin_attr_flash = {
1246 .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1248 .read = qlcnic_83xx_sysfs_flash_read_handler,
1249 .write = qlcnic_83xx_sysfs_flash_write_handler,
1252 #ifdef CONFIG_QLCNIC_HWMON
1254 static ssize_t qlcnic_hwmon_show_temp(struct device *dev,
1255 struct device_attribute *dev_attr,
1258 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1259 unsigned int temperature = 0, value = 0;
1261 if (qlcnic_83xx_check(adapter))
1262 value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
1263 else if (qlcnic_82xx_check(adapter))
1264 value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
1266 temperature = qlcnic_get_temp_val(value);
1267 /* display millidegree celcius */
1268 temperature *= 1000;
1269 return sprintf(buf, "%u\n", temperature);
1272 /* hwmon-sysfs attributes */
1273 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1274 qlcnic_hwmon_show_temp, NULL, 1);
1276 static struct attribute *qlcnic_hwmon_attrs[] = {
1277 &sensor_dev_attr_temp1_input.dev_attr.attr,
1281 ATTRIBUTE_GROUPS(qlcnic_hwmon);
1283 void qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
1285 struct device *dev = &adapter->pdev->dev;
1286 struct device *hwmon_dev;
1288 /* Skip hwmon registration for a VF device */
1289 if (qlcnic_sriov_vf_check(adapter)) {
1290 adapter->ahw->hwmon_dev = NULL;
1293 hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
1295 qlcnic_hwmon_groups);
1296 if (IS_ERR(hwmon_dev)) {
1297 dev_err(dev, "Cannot register with hwmon, err=%ld\n",
1298 PTR_ERR(hwmon_dev));
1301 adapter->ahw->hwmon_dev = hwmon_dev;
1304 void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
1306 struct device *hwmon_dev = adapter->ahw->hwmon_dev;
1308 hwmon_device_unregister(hwmon_dev);
1309 adapter->ahw->hwmon_dev = NULL;
1314 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1316 struct device *dev = &adapter->pdev->dev;
1318 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1319 if (device_create_file(dev, &dev_attr_bridged_mode))
1321 "failed to create bridged_mode sysfs entry\n");
1324 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1326 struct device *dev = &adapter->pdev->dev;
1328 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1329 device_remove_file(dev, &dev_attr_bridged_mode);
1332 static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1334 struct device *dev = &adapter->pdev->dev;
1336 if (device_create_bin_file(dev, &bin_attr_port_stats))
1337 dev_info(dev, "failed to create port stats sysfs entry");
1339 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1341 if (device_create_file(dev, &dev_attr_diag_mode))
1342 dev_info(dev, "failed to create diag_mode sysfs entry\n");
1343 if (device_create_bin_file(dev, &bin_attr_crb))
1344 dev_info(dev, "failed to create crb sysfs entry\n");
1345 if (device_create_bin_file(dev, &bin_attr_mem))
1346 dev_info(dev, "failed to create mem sysfs entry\n");
1348 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1351 if (device_create_bin_file(dev, &bin_attr_pci_config))
1352 dev_info(dev, "failed to create pci config sysfs entry");
1354 if (device_create_file(dev, &dev_attr_beacon))
1355 dev_info(dev, "failed to create beacon sysfs entry");
1357 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1359 if (device_create_bin_file(dev, &bin_attr_esw_config))
1360 dev_info(dev, "failed to create esw config sysfs entry");
1361 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1363 if (device_create_bin_file(dev, &bin_attr_npar_config))
1364 dev_info(dev, "failed to create npar config sysfs entry");
1365 if (device_create_bin_file(dev, &bin_attr_pm_config))
1366 dev_info(dev, "failed to create pm config sysfs entry");
1367 if (device_create_bin_file(dev, &bin_attr_esw_stats))
1368 dev_info(dev, "failed to create eswitch stats sysfs entry");
1371 static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1373 struct device *dev = &adapter->pdev->dev;
1375 device_remove_bin_file(dev, &bin_attr_port_stats);
1377 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1379 device_remove_file(dev, &dev_attr_diag_mode);
1380 device_remove_bin_file(dev, &bin_attr_crb);
1381 device_remove_bin_file(dev, &bin_attr_mem);
1383 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1386 device_remove_bin_file(dev, &bin_attr_pci_config);
1387 device_remove_file(dev, &dev_attr_beacon);
1388 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1390 device_remove_bin_file(dev, &bin_attr_esw_config);
1391 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1393 device_remove_bin_file(dev, &bin_attr_npar_config);
1394 device_remove_bin_file(dev, &bin_attr_pm_config);
1395 device_remove_bin_file(dev, &bin_attr_esw_stats);
1398 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1400 qlcnic_create_diag_entries(adapter);
1403 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1405 qlcnic_remove_diag_entries(adapter);
1408 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1410 struct device *dev = &adapter->pdev->dev;
1412 qlcnic_create_diag_entries(adapter);
1414 if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1415 dev_info(dev, "failed to create flash sysfs entry\n");
1418 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1420 struct device *dev = &adapter->pdev->dev;
1422 qlcnic_remove_diag_entries(adapter);
1423 sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);