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/vmalloc.h>
10 #include <linux/interrupt.h>
13 #include "qlcnic_hw.h"
15 #include <linux/swab.h>
16 #include <linux/dma-mapping.h>
18 #include <linux/ipv6.h>
19 #include <linux/inetdevice.h>
20 #include <linux/sysfs.h>
21 #include <linux/aer.h>
22 #include <linux/log2.h>
24 #define QLC_STATUS_UNSUPPORTED_CMD -2
26 int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
31 int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
36 static ssize_t qlcnic_store_bridged_mode(struct device *dev,
37 struct device_attribute *attr,
38 const char *buf, size_t len)
40 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
44 if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
47 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
50 if (kstrtoul(buf, 2, &new))
53 if (!qlcnic_config_bridged_mode(adapter, !!new))
60 static ssize_t qlcnic_show_bridged_mode(struct device *dev,
61 struct device_attribute *attr,
64 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
67 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
68 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
70 return sprintf(buf, "%d\n", bridged_mode);
73 static ssize_t qlcnic_store_diag_mode(struct device *dev,
74 struct device_attribute *attr,
75 const char *buf, size_t len)
77 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
80 if (kstrtoul(buf, 2, &new))
83 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
84 adapter->flags ^= QLCNIC_DIAG_ENABLED;
89 static ssize_t qlcnic_show_diag_mode(struct device *dev,
90 struct device_attribute *attr, char *buf)
92 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
93 return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
96 static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
100 *state = MSB(beacon);
102 QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
105 *rate = __QLCNIC_MAX_LED_RATE;
107 } else if (*state > __QLCNIC_MAX_LED_STATE) {
111 if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
117 static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
118 const char *buf, size_t len)
120 struct qlcnic_hardware_context *ahw = adapter->ahw;
121 unsigned long h_beacon;
124 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
127 if (kstrtoul(buf, 2, &h_beacon))
130 if (ahw->beacon_state == h_beacon)
134 if (!ahw->beacon_state) {
135 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
142 err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
144 err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
146 ahw->beacon_state = h_beacon;
148 if (!ahw->beacon_state)
149 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
155 static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
156 const char *buf, size_t len)
158 struct qlcnic_hardware_context *ahw = adapter->ahw;
159 int err, max_sds_rings = adapter->max_sds_rings;
161 u8 h_beacon_state, b_state, b_rate;
163 if (len != sizeof(u16))
164 return QL_STATUS_INVALID_PARAM;
166 memcpy(&beacon, buf, sizeof(u16));
167 err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
171 if (ahw->extra_capability[0] & QLCNIC_FW_CAPABILITY_2_BEACON) {
172 err = qlcnic_get_beacon_state(adapter, &h_beacon_state);
174 netdev_err(adapter->netdev,
175 "Failed to get current beacon state\n");
177 if (h_beacon_state == QLCNIC_BEACON_DISABLE)
178 ahw->beacon_state = 0;
179 else if (h_beacon_state == QLCNIC_BEACON_EANBLE)
180 ahw->beacon_state = 2;
184 if (ahw->beacon_state == b_state)
188 if (!ahw->beacon_state) {
189 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
195 if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
200 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
201 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
204 set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
207 err = qlcnic_config_led(adapter, b_state, b_rate);
210 ahw->beacon_state = b_state;
213 if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
214 qlcnic_diag_free_res(adapter->netdev, max_sds_rings);
217 if (!ahw->beacon_state)
218 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
224 static ssize_t qlcnic_store_beacon(struct device *dev,
225 struct device_attribute *attr,
226 const char *buf, size_t len)
228 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
231 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
233 "LED test not supported in non privileged mode\n");
237 if (qlcnic_82xx_check(adapter))
238 err = qlcnic_82xx_store_beacon(adapter, buf, len);
239 else if (qlcnic_83xx_check(adapter))
240 err = qlcnic_83xx_store_beacon(adapter, buf, len);
247 static ssize_t qlcnic_show_beacon(struct device *dev,
248 struct device_attribute *attr, char *buf)
250 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
252 return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
255 static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
256 loff_t offset, size_t size)
260 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
263 if (offset < QLCNIC_PCI_CRBSPACE) {
264 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
265 QLCNIC_PCI_CAMQM_END))
271 if ((size != crb_size) || (offset & (crb_size-1)))
277 static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
278 struct bin_attribute *attr, char *buf,
279 loff_t offset, size_t size)
281 struct device *dev = container_of(kobj, struct device, kobj);
282 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
285 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
288 qlcnic_read_crb(adapter, buf, offset, size);
293 static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
294 struct bin_attribute *attr, char *buf,
295 loff_t offset, size_t size)
297 struct device *dev = container_of(kobj, struct device, kobj);
298 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
301 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
305 qlcnic_write_crb(adapter, buf, offset, size);
309 static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
310 loff_t offset, size_t size)
312 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
315 if ((size != 8) || (offset & 0x7))
321 static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
322 struct bin_attribute *attr, char *buf,
323 loff_t offset, size_t size)
325 struct device *dev = container_of(kobj, struct device, kobj);
326 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
330 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
334 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
337 memcpy(buf, &data, size);
342 static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
343 struct bin_attribute *attr, char *buf,
344 loff_t offset, size_t size)
346 struct device *dev = container_of(kobj, struct device, kobj);
347 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
351 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
355 memcpy(&data, buf, size);
357 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
363 static int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
366 for (i = 0; i < adapter->ahw->act_pci_func; i++) {
367 if (adapter->npars[i].pci_func == pci_func)
374 static int validate_pm_config(struct qlcnic_adapter *adapter,
375 struct qlcnic_pm_func_cfg *pm_cfg, int count)
377 u8 src_pci_func, s_esw_id, d_esw_id;
379 int i, src_index, dest_index;
381 for (i = 0; i < count; i++) {
382 src_pci_func = pm_cfg[i].pci_func;
383 dest_pci_func = pm_cfg[i].dest_npar;
384 src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
387 return QL_STATUS_INVALID_PARAM;
389 dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
391 return QL_STATUS_INVALID_PARAM;
393 s_esw_id = adapter->npars[src_index].phy_port;
394 d_esw_id = adapter->npars[dest_index].phy_port;
396 if (s_esw_id != d_esw_id)
397 return QL_STATUS_INVALID_PARAM;
403 static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
404 struct kobject *kobj,
405 struct bin_attribute *attr,
406 char *buf, loff_t offset,
409 struct device *dev = container_of(kobj, struct device, kobj);
410 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
411 struct qlcnic_pm_func_cfg *pm_cfg;
412 u32 id, action, pci_func;
413 int count, rem, i, ret, index;
415 count = size / sizeof(struct qlcnic_pm_func_cfg);
416 rem = size % sizeof(struct qlcnic_pm_func_cfg);
418 return QL_STATUS_INVALID_PARAM;
420 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
421 ret = validate_pm_config(adapter, pm_cfg, count);
425 for (i = 0; i < count; i++) {
426 pci_func = pm_cfg[i].pci_func;
427 action = !!pm_cfg[i].action;
428 index = qlcnic_is_valid_nic_func(adapter, pci_func);
430 return QL_STATUS_INVALID_PARAM;
432 id = adapter->npars[index].phy_port;
433 ret = qlcnic_config_port_mirroring(adapter, id,
439 for (i = 0; i < count; i++) {
440 pci_func = pm_cfg[i].pci_func;
441 index = qlcnic_is_valid_nic_func(adapter, pci_func);
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[QLCNIC_MAX_PCI_FUNC];
462 if (size != sizeof(pm_cfg))
463 return QL_STATUS_INVALID_PARAM;
466 sizeof(struct qlcnic_pm_func_cfg) * QLCNIC_MAX_PCI_FUNC);
468 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
469 pci_func = adapter->npars[i].pci_func;
470 if (!adapter->npars[i].active)
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 memcpy(buf, &pm_cfg, size);
485 static int validate_esw_config(struct qlcnic_adapter *adapter,
486 struct qlcnic_esw_func_cfg *esw_cfg, int count)
492 if (qlcnic_82xx_check(adapter))
493 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
495 op_mode = QLCRDX(adapter->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 >= QLCNIC_MAX_PCI_FUNC)
500 return QL_STATUS_INVALID_PARAM;
502 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
503 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
504 return QL_STATUS_INVALID_PARAM;
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)
518 return QL_STATUS_INVALID_PARAM;
519 if (esw_cfg[i].mac_override != 1)
520 return QL_STATUS_INVALID_PARAM;
521 if (esw_cfg[i].promisc_mode != 1)
522 return QL_STATUS_INVALID_PARAM;
525 case QLCNIC_ADD_VLAN:
526 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
527 return QL_STATUS_INVALID_PARAM;
528 if (!esw_cfg[i].op_type)
529 return QL_STATUS_INVALID_PARAM;
531 case QLCNIC_DEL_VLAN:
532 if (!esw_cfg[i].op_type)
533 return QL_STATUS_INVALID_PARAM;
536 return QL_STATUS_INVALID_PARAM;
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);
560 return QL_STATUS_INVALID_PARAM;
562 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
563 ret = validate_esw_config(adapter, esw_cfg, count);
567 for (i = 0; i < count; i++) {
568 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
569 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
570 return QL_STATUS_INVALID_PARAM;
572 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
575 op_mode = esw_cfg[i].op_mode;
576 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
577 esw_cfg[i].op_mode = op_mode;
578 esw_cfg[i].pci_func = adapter->ahw->pci_func;
580 switch (esw_cfg[i].op_mode) {
581 case QLCNIC_PORT_DEFAULTS:
582 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
584 qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
587 case QLCNIC_ADD_VLAN:
588 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
590 case QLCNIC_DEL_VLAN:
591 esw_cfg[i].vlan_id = 0;
592 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
597 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
600 for (i = 0; i < count; i++) {
601 pci_func = esw_cfg[i].pci_func;
602 index = qlcnic_is_valid_nic_func(adapter, pci_func);
603 npar = &adapter->npars[index];
604 switch (esw_cfg[i].op_mode) {
605 case QLCNIC_PORT_DEFAULTS:
606 npar->promisc_mode = esw_cfg[i].promisc_mode;
607 npar->mac_override = esw_cfg[i].mac_override;
608 npar->offload_flags = esw_cfg[i].offload_flags;
609 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
610 npar->discard_tagged = esw_cfg[i].discard_tagged;
612 case QLCNIC_ADD_VLAN:
613 npar->pvid = esw_cfg[i].vlan_id;
615 case QLCNIC_DEL_VLAN:
624 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
625 struct kobject *kobj,
626 struct bin_attribute *attr,
627 char *buf, loff_t offset,
630 struct device *dev = container_of(kobj, struct device, kobj);
631 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
632 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
635 if (size != sizeof(esw_cfg))
636 return QL_STATUS_INVALID_PARAM;
639 sizeof(struct qlcnic_esw_func_cfg) * QLCNIC_MAX_PCI_FUNC);
641 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
642 pci_func = adapter->npars[i].pci_func;
643 if (!adapter->npars[i].active)
646 if (!adapter->npars[i].eswitch_status)
649 esw_cfg[pci_func].pci_func = pci_func;
650 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
651 return QL_STATUS_INVALID_PARAM;
654 memcpy(buf, &esw_cfg, size);
659 static int validate_npar_config(struct qlcnic_adapter *adapter,
660 struct qlcnic_npar_func_cfg *np_cfg,
665 for (i = 0; i < count; i++) {
666 pci_func = np_cfg[i].pci_func;
667 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
668 return QL_STATUS_INVALID_PARAM;
670 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
671 !IS_VALID_BW(np_cfg[i].max_bw))
672 return QL_STATUS_INVALID_PARAM;
677 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
678 struct kobject *kobj,
679 struct bin_attribute *attr,
680 char *buf, loff_t offset,
683 struct device *dev = container_of(kobj, struct device, kobj);
684 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
685 struct qlcnic_info nic_info;
686 struct qlcnic_npar_func_cfg *np_cfg;
687 int i, count, rem, ret, index;
690 count = size / sizeof(struct qlcnic_npar_func_cfg);
691 rem = size % sizeof(struct qlcnic_npar_func_cfg);
693 return QL_STATUS_INVALID_PARAM;
695 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
696 ret = validate_npar_config(adapter, np_cfg, count);
700 for (i = 0; i < count; i++) {
701 pci_func = np_cfg[i].pci_func;
703 memset(&nic_info, 0, sizeof(struct qlcnic_info));
704 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
707 nic_info.pci_func = pci_func;
708 nic_info.min_tx_bw = np_cfg[i].min_bw;
709 nic_info.max_tx_bw = np_cfg[i].max_bw;
710 ret = qlcnic_set_nic_info(adapter, &nic_info);
713 index = qlcnic_is_valid_nic_func(adapter, pci_func);
714 adapter->npars[index].min_bw = nic_info.min_tx_bw;
715 adapter->npars[index].max_bw = nic_info.max_tx_bw;
721 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
722 struct kobject *kobj,
723 struct bin_attribute *attr,
724 char *buf, loff_t offset,
727 struct device *dev = container_of(kobj, struct device, kobj);
728 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
729 struct qlcnic_info nic_info;
730 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
733 if (size != sizeof(np_cfg))
734 return QL_STATUS_INVALID_PARAM;
736 memset(&nic_info, 0, sizeof(struct qlcnic_info));
738 sizeof(struct qlcnic_npar_func_cfg) * QLCNIC_MAX_PCI_FUNC);
740 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
741 if (qlcnic_is_valid_nic_func(adapter, i) < 0)
743 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
747 if (!adapter->npars[i].eswitch_status)
750 np_cfg[i].pci_func = i;
751 np_cfg[i].op_mode = (u8)nic_info.op_mode;
752 np_cfg[i].port_num = nic_info.phys_port;
753 np_cfg[i].fw_capab = nic_info.capabilities;
754 np_cfg[i].min_bw = nic_info.min_tx_bw;
755 np_cfg[i].max_bw = nic_info.max_tx_bw;
756 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
757 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
760 memcpy(buf, &np_cfg, size);
764 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
765 struct kobject *kobj,
766 struct bin_attribute *attr,
767 char *buf, loff_t offset,
770 struct device *dev = container_of(kobj, struct device, kobj);
771 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
772 struct qlcnic_esw_statistics port_stats;
775 if (qlcnic_83xx_check(adapter))
776 return QLC_STATUS_UNSUPPORTED_CMD;
778 if (size != sizeof(struct qlcnic_esw_statistics))
779 return QL_STATUS_INVALID_PARAM;
781 if (offset >= QLCNIC_MAX_PCI_FUNC)
782 return QL_STATUS_INVALID_PARAM;
784 memset(&port_stats, 0, size);
785 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
790 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
795 memcpy(buf, &port_stats, size);
799 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
800 struct kobject *kobj,
801 struct bin_attribute *attr,
802 char *buf, loff_t offset,
805 struct device *dev = container_of(kobj, struct device, kobj);
806 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
807 struct qlcnic_esw_statistics esw_stats;
810 if (qlcnic_83xx_check(adapter))
811 return QLC_STATUS_UNSUPPORTED_CMD;
813 if (size != sizeof(struct qlcnic_esw_statistics))
814 return QL_STATUS_INVALID_PARAM;
816 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
817 return QL_STATUS_INVALID_PARAM;
819 memset(&esw_stats, 0, size);
820 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
825 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
830 memcpy(buf, &esw_stats, size);
834 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
835 struct kobject *kobj,
836 struct bin_attribute *attr,
837 char *buf, loff_t offset,
840 struct device *dev = container_of(kobj, struct device, kobj);
841 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
844 if (qlcnic_83xx_check(adapter))
845 return QLC_STATUS_UNSUPPORTED_CMD;
847 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
848 return QL_STATUS_INVALID_PARAM;
850 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
851 QLCNIC_QUERY_RX_COUNTER);
855 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
856 QLCNIC_QUERY_TX_COUNTER);
863 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
864 struct kobject *kobj,
865 struct bin_attribute *attr,
866 char *buf, loff_t offset,
870 struct device *dev = container_of(kobj, struct device, kobj);
871 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
874 if (qlcnic_83xx_check(adapter))
875 return QLC_STATUS_UNSUPPORTED_CMD;
877 if (offset >= QLCNIC_MAX_PCI_FUNC)
878 return QL_STATUS_INVALID_PARAM;
880 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
881 QLCNIC_QUERY_RX_COUNTER);
885 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
886 QLCNIC_QUERY_TX_COUNTER);
893 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
894 struct kobject *kobj,
895 struct bin_attribute *attr,
896 char *buf, loff_t offset,
899 struct device *dev = container_of(kobj, struct device, kobj);
900 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
901 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
902 struct qlcnic_pci_info *pci_info;
905 if (size != sizeof(pci_cfg))
906 return QL_STATUS_INVALID_PARAM;
908 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
912 ret = qlcnic_get_pci_info(adapter, pci_info);
919 sizeof(struct qlcnic_pci_func_cfg) * QLCNIC_MAX_PCI_FUNC);
921 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
922 pci_cfg[i].pci_func = pci_info[i].id;
923 pci_cfg[i].func_type = pci_info[i].type;
924 pci_cfg[i].port_num = pci_info[i].default_port;
925 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
926 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
927 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
930 memcpy(buf, &pci_cfg, size);
935 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
936 struct kobject *kobj,
937 struct bin_attribute *attr,
938 char *buf, loff_t offset,
941 unsigned char *p_read_buf;
943 struct device *dev = container_of(kobj, struct device, kobj);
944 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
947 return QL_STATUS_INVALID_PARAM;
949 return QL_STATUS_INVALID_PARAM;
951 count = size / sizeof(u32);
953 if (size % sizeof(u32))
956 p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
959 if (qlcnic_83xx_lock_flash(adapter) != 0) {
964 ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
968 qlcnic_83xx_unlock_flash(adapter);
973 qlcnic_83xx_unlock_flash(adapter);
974 memcpy(buf, p_read_buf, size);
980 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
981 char *buf, loff_t offset,
985 unsigned char *p_cache, *p_src;
987 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
991 memcpy(p_cache, buf, size);
993 count = size / sizeof(u32);
995 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1000 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1001 ret = qlcnic_83xx_enable_flash_write(adapter);
1004 qlcnic_83xx_unlock_flash(adapter);
1009 for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1010 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1012 QLC_83XX_FLASH_WRITE_MAX);
1015 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1016 ret = qlcnic_83xx_disable_flash_write(adapter);
1019 qlcnic_83xx_unlock_flash(adapter);
1025 qlcnic_83xx_unlock_flash(adapter);
1029 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1030 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1033 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1034 ret = qlcnic_83xx_disable_flash_write(adapter);
1037 qlcnic_83xx_unlock_flash(adapter);
1043 qlcnic_83xx_unlock_flash(adapter);
1048 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1049 char *buf, loff_t offset, size_t size)
1052 unsigned char *p_cache, *p_src;
1054 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1058 memcpy(p_cache, buf, size);
1060 count = size / sizeof(u32);
1062 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1067 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1068 ret = qlcnic_83xx_enable_flash_write(adapter);
1071 qlcnic_83xx_unlock_flash(adapter);
1076 for (i = 0; i < count; i++) {
1077 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1079 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1080 ret = qlcnic_83xx_disable_flash_write(adapter);
1083 qlcnic_83xx_unlock_flash(adapter);
1088 qlcnic_83xx_unlock_flash(adapter);
1092 p_src = p_src + sizeof(u32);
1093 offset = offset + sizeof(u32);
1096 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1097 ret = qlcnic_83xx_disable_flash_write(adapter);
1100 qlcnic_83xx_unlock_flash(adapter);
1106 qlcnic_83xx_unlock_flash(adapter);
1111 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1112 struct kobject *kobj,
1113 struct bin_attribute *attr,
1114 char *buf, loff_t offset,
1118 static int flash_mode;
1120 struct device *dev = container_of(kobj, struct device, kobj);
1121 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1124 return QL_STATUS_INVALID_PARAM;
1126 ret = kstrtoul(buf, 16, &data);
1129 case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1130 flash_mode = QLC_83XX_ERASE_MODE;
1131 ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1133 dev_err(&adapter->pdev->dev,
1134 "%s failed at %d\n", __func__, __LINE__);
1139 case QLC_83XX_FLASH_BULK_WRITE_CMD:
1140 flash_mode = QLC_83XX_BULK_WRITE_MODE;
1143 case QLC_83XX_FLASH_WRITE_CMD:
1144 flash_mode = QLC_83XX_WRITE_MODE;
1147 if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1148 ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1151 dev_err(&adapter->pdev->dev,
1152 "%s failed at %d\n",
1153 __func__, __LINE__);
1158 if (flash_mode == QLC_83XX_WRITE_MODE) {
1159 ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1162 dev_err(&adapter->pdev->dev,
1163 "%s failed at %d\n", __func__,
1173 static struct device_attribute dev_attr_bridged_mode = {
1174 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1175 .show = qlcnic_show_bridged_mode,
1176 .store = qlcnic_store_bridged_mode,
1179 static struct device_attribute dev_attr_diag_mode = {
1180 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1181 .show = qlcnic_show_diag_mode,
1182 .store = qlcnic_store_diag_mode,
1185 static struct device_attribute dev_attr_beacon = {
1186 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1187 .show = qlcnic_show_beacon,
1188 .store = qlcnic_store_beacon,
1191 static struct bin_attribute bin_attr_crb = {
1192 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1194 .read = qlcnic_sysfs_read_crb,
1195 .write = qlcnic_sysfs_write_crb,
1198 static struct bin_attribute bin_attr_mem = {
1199 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1201 .read = qlcnic_sysfs_read_mem,
1202 .write = qlcnic_sysfs_write_mem,
1205 static struct bin_attribute bin_attr_npar_config = {
1206 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1208 .read = qlcnic_sysfs_read_npar_config,
1209 .write = qlcnic_sysfs_write_npar_config,
1212 static struct bin_attribute bin_attr_pci_config = {
1213 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1215 .read = qlcnic_sysfs_read_pci_config,
1219 static struct bin_attribute bin_attr_port_stats = {
1220 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1222 .read = qlcnic_sysfs_get_port_stats,
1223 .write = qlcnic_sysfs_clear_port_stats,
1226 static struct bin_attribute bin_attr_esw_stats = {
1227 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1229 .read = qlcnic_sysfs_get_esw_stats,
1230 .write = qlcnic_sysfs_clear_esw_stats,
1233 static struct bin_attribute bin_attr_esw_config = {
1234 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1236 .read = qlcnic_sysfs_read_esw_config,
1237 .write = qlcnic_sysfs_write_esw_config,
1240 static struct bin_attribute bin_attr_pm_config = {
1241 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1243 .read = qlcnic_sysfs_read_pm_config,
1244 .write = qlcnic_sysfs_write_pm_config,
1247 static struct bin_attribute bin_attr_flash = {
1248 .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1250 .read = qlcnic_83xx_sysfs_flash_read_handler,
1251 .write = qlcnic_83xx_sysfs_flash_write_handler,
1254 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1256 struct device *dev = &adapter->pdev->dev;
1258 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1259 if (device_create_file(dev, &dev_attr_bridged_mode))
1261 "failed to create bridged_mode sysfs entry\n");
1264 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1266 struct device *dev = &adapter->pdev->dev;
1268 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1269 device_remove_file(dev, &dev_attr_bridged_mode);
1272 void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1274 struct device *dev = &adapter->pdev->dev;
1276 if (device_create_bin_file(dev, &bin_attr_port_stats))
1277 dev_info(dev, "failed to create port stats sysfs entry");
1279 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1281 if (device_create_file(dev, &dev_attr_diag_mode))
1282 dev_info(dev, "failed to create diag_mode sysfs entry\n");
1283 if (device_create_bin_file(dev, &bin_attr_crb))
1284 dev_info(dev, "failed to create crb sysfs entry\n");
1285 if (device_create_bin_file(dev, &bin_attr_mem))
1286 dev_info(dev, "failed to create mem sysfs entry\n");
1288 if (device_create_bin_file(dev, &bin_attr_pci_config))
1289 dev_info(dev, "failed to create pci config sysfs entry");
1290 if (device_create_file(dev, &dev_attr_beacon))
1291 dev_info(dev, "failed to create beacon sysfs entry");
1293 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1295 if (device_create_bin_file(dev, &bin_attr_esw_config))
1296 dev_info(dev, "failed to create esw config sysfs entry");
1297 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1299 if (device_create_bin_file(dev, &bin_attr_npar_config))
1300 dev_info(dev, "failed to create npar config sysfs entry");
1301 if (device_create_bin_file(dev, &bin_attr_pm_config))
1302 dev_info(dev, "failed to create pm config sysfs entry");
1303 if (device_create_bin_file(dev, &bin_attr_esw_stats))
1304 dev_info(dev, "failed to create eswitch stats sysfs entry");
1307 void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1309 struct device *dev = &adapter->pdev->dev;
1311 device_remove_bin_file(dev, &bin_attr_port_stats);
1313 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1315 device_remove_file(dev, &dev_attr_diag_mode);
1316 device_remove_bin_file(dev, &bin_attr_crb);
1317 device_remove_bin_file(dev, &bin_attr_mem);
1318 device_remove_bin_file(dev, &bin_attr_pci_config);
1319 device_remove_file(dev, &dev_attr_beacon);
1320 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1322 device_remove_bin_file(dev, &bin_attr_esw_config);
1323 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1325 device_remove_bin_file(dev, &bin_attr_npar_config);
1326 device_remove_bin_file(dev, &bin_attr_pm_config);
1327 device_remove_bin_file(dev, &bin_attr_esw_stats);
1330 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1332 qlcnic_create_diag_entries(adapter);
1335 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1337 qlcnic_remove_diag_entries(adapter);
1340 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1342 struct device *dev = &adapter->pdev->dev;
1344 qlcnic_create_diag_entries(adapter);
1346 if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1347 dev_info(dev, "failed to create flash sysfs entry\n");
1350 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1352 struct device *dev = &adapter->pdev->dev;
1354 qlcnic_remove_diag_entries(adapter);
1355 sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);