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 < adapter->ahw->act_pci_func; i++) {
469 pci_func = adapter->npars[i].pci_func;
470 pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
471 pm_cfg[pci_func].dest_npar = 0;
472 pm_cfg[pci_func].pci_func = i;
474 memcpy(buf, &pm_cfg, size);
479 static int validate_esw_config(struct qlcnic_adapter *adapter,
480 struct qlcnic_esw_func_cfg *esw_cfg, int count)
486 if (qlcnic_82xx_check(adapter))
487 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
489 op_mode = QLCRDX(adapter->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 >= QLCNIC_MAX_PCI_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);
597 npar = &adapter->npars[index];
598 switch (esw_cfg[i].op_mode) {
599 case QLCNIC_PORT_DEFAULTS:
600 npar->promisc_mode = esw_cfg[i].promisc_mode;
601 npar->mac_override = esw_cfg[i].mac_override;
602 npar->offload_flags = esw_cfg[i].offload_flags;
603 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
604 npar->discard_tagged = esw_cfg[i].discard_tagged;
606 case QLCNIC_ADD_VLAN:
607 npar->pvid = esw_cfg[i].vlan_id;
609 case QLCNIC_DEL_VLAN:
618 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
619 struct kobject *kobj,
620 struct bin_attribute *attr,
621 char *buf, loff_t offset,
624 struct device *dev = container_of(kobj, struct device, kobj);
625 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
626 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
629 if (size != sizeof(esw_cfg))
630 return QL_STATUS_INVALID_PARAM;
633 sizeof(struct qlcnic_esw_func_cfg) * QLCNIC_MAX_PCI_FUNC);
635 for (i = 0; i < adapter->ahw->act_pci_func; i++) {
636 pci_func = adapter->npars[i].pci_func;
637 esw_cfg[pci_func].pci_func = pci_func;
638 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
639 return QL_STATUS_INVALID_PARAM;
642 memcpy(buf, &esw_cfg, size);
647 static int validate_npar_config(struct qlcnic_adapter *adapter,
648 struct qlcnic_npar_func_cfg *np_cfg,
653 for (i = 0; i < count; i++) {
654 pci_func = np_cfg[i].pci_func;
655 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
656 return QL_STATUS_INVALID_PARAM;
658 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
659 !IS_VALID_BW(np_cfg[i].max_bw))
660 return QL_STATUS_INVALID_PARAM;
665 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
666 struct kobject *kobj,
667 struct bin_attribute *attr,
668 char *buf, loff_t offset,
671 struct device *dev = container_of(kobj, struct device, kobj);
672 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
673 struct qlcnic_info nic_info;
674 struct qlcnic_npar_func_cfg *np_cfg;
675 int i, count, rem, ret, index;
678 count = size / sizeof(struct qlcnic_npar_func_cfg);
679 rem = size % sizeof(struct qlcnic_npar_func_cfg);
681 return QL_STATUS_INVALID_PARAM;
683 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
684 ret = validate_npar_config(adapter, np_cfg, count);
688 for (i = 0; i < count; i++) {
689 pci_func = np_cfg[i].pci_func;
691 memset(&nic_info, 0, sizeof(struct qlcnic_info));
692 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
695 nic_info.pci_func = pci_func;
696 nic_info.min_tx_bw = np_cfg[i].min_bw;
697 nic_info.max_tx_bw = np_cfg[i].max_bw;
698 ret = qlcnic_set_nic_info(adapter, &nic_info);
701 index = qlcnic_is_valid_nic_func(adapter, pci_func);
702 adapter->npars[index].min_bw = nic_info.min_tx_bw;
703 adapter->npars[index].max_bw = nic_info.max_tx_bw;
709 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
710 struct kobject *kobj,
711 struct bin_attribute *attr,
712 char *buf, loff_t offset,
715 struct device *dev = container_of(kobj, struct device, kobj);
716 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
717 struct qlcnic_info nic_info;
718 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
721 if (size != sizeof(np_cfg))
722 return QL_STATUS_INVALID_PARAM;
724 memset(&nic_info, 0, sizeof(struct qlcnic_info));
726 sizeof(struct qlcnic_npar_func_cfg) * QLCNIC_MAX_PCI_FUNC);
728 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
729 if (qlcnic_is_valid_nic_func(adapter, i) < 0)
731 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
735 np_cfg[i].pci_func = i;
736 np_cfg[i].op_mode = (u8)nic_info.op_mode;
737 np_cfg[i].port_num = nic_info.phys_port;
738 np_cfg[i].fw_capab = nic_info.capabilities;
739 np_cfg[i].min_bw = nic_info.min_tx_bw;
740 np_cfg[i].max_bw = nic_info.max_tx_bw;
741 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
742 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
745 memcpy(buf, &np_cfg, size);
749 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
750 struct kobject *kobj,
751 struct bin_attribute *attr,
752 char *buf, loff_t offset,
755 struct device *dev = container_of(kobj, struct device, kobj);
756 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
757 struct qlcnic_esw_statistics port_stats;
760 if (qlcnic_83xx_check(adapter))
761 return QLC_STATUS_UNSUPPORTED_CMD;
763 if (size != sizeof(struct qlcnic_esw_statistics))
764 return QL_STATUS_INVALID_PARAM;
766 if (offset >= QLCNIC_MAX_PCI_FUNC)
767 return QL_STATUS_INVALID_PARAM;
769 memset(&port_stats, 0, size);
770 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
775 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
780 memcpy(buf, &port_stats, size);
784 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
785 struct kobject *kobj,
786 struct bin_attribute *attr,
787 char *buf, loff_t offset,
790 struct device *dev = container_of(kobj, struct device, kobj);
791 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
792 struct qlcnic_esw_statistics esw_stats;
795 if (qlcnic_83xx_check(adapter))
796 return QLC_STATUS_UNSUPPORTED_CMD;
798 if (size != sizeof(struct qlcnic_esw_statistics))
799 return QL_STATUS_INVALID_PARAM;
801 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
802 return QL_STATUS_INVALID_PARAM;
804 memset(&esw_stats, 0, size);
805 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
810 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
815 memcpy(buf, &esw_stats, size);
819 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
820 struct kobject *kobj,
821 struct bin_attribute *attr,
822 char *buf, loff_t offset,
825 struct device *dev = container_of(kobj, struct device, kobj);
826 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
829 if (qlcnic_83xx_check(adapter))
830 return QLC_STATUS_UNSUPPORTED_CMD;
832 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
833 return QL_STATUS_INVALID_PARAM;
835 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
836 QLCNIC_QUERY_RX_COUNTER);
840 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
841 QLCNIC_QUERY_TX_COUNTER);
848 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
849 struct kobject *kobj,
850 struct bin_attribute *attr,
851 char *buf, loff_t offset,
855 struct device *dev = container_of(kobj, struct device, kobj);
856 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
859 if (qlcnic_83xx_check(adapter))
860 return QLC_STATUS_UNSUPPORTED_CMD;
862 if (offset >= QLCNIC_MAX_PCI_FUNC)
863 return QL_STATUS_INVALID_PARAM;
865 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
866 QLCNIC_QUERY_RX_COUNTER);
870 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
871 QLCNIC_QUERY_TX_COUNTER);
878 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
879 struct kobject *kobj,
880 struct bin_attribute *attr,
881 char *buf, loff_t offset,
884 struct device *dev = container_of(kobj, struct device, kobj);
885 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
886 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
887 struct qlcnic_pci_info *pci_info;
890 if (size != sizeof(pci_cfg))
891 return QL_STATUS_INVALID_PARAM;
893 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
897 ret = qlcnic_get_pci_info(adapter, pci_info);
904 sizeof(struct qlcnic_pci_func_cfg) * QLCNIC_MAX_PCI_FUNC);
906 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
907 pci_cfg[i].pci_func = pci_info[i].id;
908 pci_cfg[i].func_type = pci_info[i].type;
909 pci_cfg[i].port_num = pci_info[i].default_port;
910 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
911 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
912 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
915 memcpy(buf, &pci_cfg, size);
920 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
921 struct kobject *kobj,
922 struct bin_attribute *attr,
923 char *buf, loff_t offset,
926 unsigned char *p_read_buf;
928 struct device *dev = container_of(kobj, struct device, kobj);
929 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
932 return QL_STATUS_INVALID_PARAM;
934 return QL_STATUS_INVALID_PARAM;
936 count = size / sizeof(u32);
938 if (size % sizeof(u32))
941 p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
944 if (qlcnic_83xx_lock_flash(adapter) != 0) {
949 ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
953 qlcnic_83xx_unlock_flash(adapter);
958 qlcnic_83xx_unlock_flash(adapter);
959 memcpy(buf, p_read_buf, size);
965 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
966 char *buf, loff_t offset,
970 unsigned char *p_cache, *p_src;
972 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
976 memcpy(p_cache, buf, size);
978 count = size / sizeof(u32);
980 if (qlcnic_83xx_lock_flash(adapter) != 0) {
985 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
986 ret = qlcnic_83xx_enable_flash_write(adapter);
989 qlcnic_83xx_unlock_flash(adapter);
994 for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
995 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
997 QLC_83XX_FLASH_WRITE_MAX);
1000 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1001 ret = qlcnic_83xx_disable_flash_write(adapter);
1004 qlcnic_83xx_unlock_flash(adapter);
1010 qlcnic_83xx_unlock_flash(adapter);
1014 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1015 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1018 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1019 ret = qlcnic_83xx_disable_flash_write(adapter);
1022 qlcnic_83xx_unlock_flash(adapter);
1028 qlcnic_83xx_unlock_flash(adapter);
1033 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1034 char *buf, loff_t offset, size_t size)
1037 unsigned char *p_cache, *p_src;
1039 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1043 memcpy(p_cache, buf, size);
1045 count = size / sizeof(u32);
1047 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1052 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1053 ret = qlcnic_83xx_enable_flash_write(adapter);
1056 qlcnic_83xx_unlock_flash(adapter);
1061 for (i = 0; i < count; i++) {
1062 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1064 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1065 ret = qlcnic_83xx_disable_flash_write(adapter);
1068 qlcnic_83xx_unlock_flash(adapter);
1073 qlcnic_83xx_unlock_flash(adapter);
1077 p_src = p_src + sizeof(u32);
1078 offset = offset + sizeof(u32);
1081 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1082 ret = qlcnic_83xx_disable_flash_write(adapter);
1085 qlcnic_83xx_unlock_flash(adapter);
1091 qlcnic_83xx_unlock_flash(adapter);
1096 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1097 struct kobject *kobj,
1098 struct bin_attribute *attr,
1099 char *buf, loff_t offset,
1103 static int flash_mode;
1105 struct device *dev = container_of(kobj, struct device, kobj);
1106 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1109 return QL_STATUS_INVALID_PARAM;
1111 ret = kstrtoul(buf, 16, &data);
1114 case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1115 flash_mode = QLC_83XX_ERASE_MODE;
1116 ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1118 dev_err(&adapter->pdev->dev,
1119 "%s failed at %d\n", __func__, __LINE__);
1124 case QLC_83XX_FLASH_BULK_WRITE_CMD:
1125 flash_mode = QLC_83XX_BULK_WRITE_MODE;
1128 case QLC_83XX_FLASH_WRITE_CMD:
1129 flash_mode = QLC_83XX_WRITE_MODE;
1132 if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1133 ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1136 dev_err(&adapter->pdev->dev,
1137 "%s failed at %d\n",
1138 __func__, __LINE__);
1143 if (flash_mode == QLC_83XX_WRITE_MODE) {
1144 ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1147 dev_err(&adapter->pdev->dev,
1148 "%s failed at %d\n", __func__,
1158 static struct device_attribute dev_attr_bridged_mode = {
1159 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1160 .show = qlcnic_show_bridged_mode,
1161 .store = qlcnic_store_bridged_mode,
1164 static struct device_attribute dev_attr_diag_mode = {
1165 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1166 .show = qlcnic_show_diag_mode,
1167 .store = qlcnic_store_diag_mode,
1170 static struct device_attribute dev_attr_beacon = {
1171 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1172 .show = qlcnic_show_beacon,
1173 .store = qlcnic_store_beacon,
1176 static struct bin_attribute bin_attr_crb = {
1177 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1179 .read = qlcnic_sysfs_read_crb,
1180 .write = qlcnic_sysfs_write_crb,
1183 static struct bin_attribute bin_attr_mem = {
1184 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1186 .read = qlcnic_sysfs_read_mem,
1187 .write = qlcnic_sysfs_write_mem,
1190 static struct bin_attribute bin_attr_npar_config = {
1191 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1193 .read = qlcnic_sysfs_read_npar_config,
1194 .write = qlcnic_sysfs_write_npar_config,
1197 static struct bin_attribute bin_attr_pci_config = {
1198 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1200 .read = qlcnic_sysfs_read_pci_config,
1204 static struct bin_attribute bin_attr_port_stats = {
1205 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1207 .read = qlcnic_sysfs_get_port_stats,
1208 .write = qlcnic_sysfs_clear_port_stats,
1211 static struct bin_attribute bin_attr_esw_stats = {
1212 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1214 .read = qlcnic_sysfs_get_esw_stats,
1215 .write = qlcnic_sysfs_clear_esw_stats,
1218 static struct bin_attribute bin_attr_esw_config = {
1219 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1221 .read = qlcnic_sysfs_read_esw_config,
1222 .write = qlcnic_sysfs_write_esw_config,
1225 static struct bin_attribute bin_attr_pm_config = {
1226 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1228 .read = qlcnic_sysfs_read_pm_config,
1229 .write = qlcnic_sysfs_write_pm_config,
1232 static struct bin_attribute bin_attr_flash = {
1233 .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1235 .read = qlcnic_83xx_sysfs_flash_read_handler,
1236 .write = qlcnic_83xx_sysfs_flash_write_handler,
1239 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1241 struct device *dev = &adapter->pdev->dev;
1243 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1244 if (device_create_file(dev, &dev_attr_bridged_mode))
1246 "failed to create bridged_mode sysfs entry\n");
1249 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1251 struct device *dev = &adapter->pdev->dev;
1253 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1254 device_remove_file(dev, &dev_attr_bridged_mode);
1257 void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1259 struct device *dev = &adapter->pdev->dev;
1261 if (device_create_bin_file(dev, &bin_attr_port_stats))
1262 dev_info(dev, "failed to create port stats sysfs entry");
1264 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1266 if (device_create_file(dev, &dev_attr_diag_mode))
1267 dev_info(dev, "failed to create diag_mode sysfs entry\n");
1268 if (device_create_bin_file(dev, &bin_attr_crb))
1269 dev_info(dev, "failed to create crb sysfs entry\n");
1270 if (device_create_bin_file(dev, &bin_attr_mem))
1271 dev_info(dev, "failed to create mem sysfs entry\n");
1273 if (device_create_bin_file(dev, &bin_attr_pci_config))
1274 dev_info(dev, "failed to create pci config sysfs entry");
1275 if (device_create_file(dev, &dev_attr_beacon))
1276 dev_info(dev, "failed to create beacon sysfs entry");
1278 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1280 if (device_create_bin_file(dev, &bin_attr_esw_config))
1281 dev_info(dev, "failed to create esw config sysfs entry");
1282 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1284 if (device_create_bin_file(dev, &bin_attr_npar_config))
1285 dev_info(dev, "failed to create npar config sysfs entry");
1286 if (device_create_bin_file(dev, &bin_attr_pm_config))
1287 dev_info(dev, "failed to create pm config sysfs entry");
1288 if (device_create_bin_file(dev, &bin_attr_esw_stats))
1289 dev_info(dev, "failed to create eswitch stats sysfs entry");
1292 void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1294 struct device *dev = &adapter->pdev->dev;
1296 device_remove_bin_file(dev, &bin_attr_port_stats);
1298 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1300 device_remove_file(dev, &dev_attr_diag_mode);
1301 device_remove_bin_file(dev, &bin_attr_crb);
1302 device_remove_bin_file(dev, &bin_attr_mem);
1303 device_remove_bin_file(dev, &bin_attr_pci_config);
1304 device_remove_file(dev, &dev_attr_beacon);
1305 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1307 device_remove_bin_file(dev, &bin_attr_esw_config);
1308 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1310 device_remove_bin_file(dev, &bin_attr_npar_config);
1311 device_remove_bin_file(dev, &bin_attr_pm_config);
1312 device_remove_bin_file(dev, &bin_attr_esw_stats);
1315 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1317 qlcnic_create_diag_entries(adapter);
1320 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1322 qlcnic_remove_diag_entries(adapter);
1325 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1327 struct device *dev = &adapter->pdev->dev;
1329 qlcnic_create_diag_entries(adapter);
1331 if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1332 dev_info(dev, "failed to create flash sysfs entry\n");
1335 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1337 struct device *dev = &adapter->pdev->dev;
1339 qlcnic_remove_diag_entries(adapter);
1340 sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);