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 (strict_strtoul(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 (strict_strtoul(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 ssize_t qlcnic_store_beacon(struct device *dev,
118 struct device_attribute *attr,
119 const char *buf, size_t len)
121 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
122 struct qlcnic_hardware_context *ahw = adapter->ahw;
123 int err, max_sds_rings = adapter->max_sds_rings;
126 unsigned long h_beacon;
128 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
130 "LED test not supported in non privileged mode\n");
134 if (qlcnic_83xx_check(adapter) &&
135 !test_bit(__QLCNIC_RESETTING, &adapter->state)) {
136 if (kstrtoul(buf, 2, &h_beacon))
139 if (ahw->beacon_state == h_beacon)
143 if (!ahw->beacon_state) {
144 if (test_and_set_bit(__QLCNIC_LED_ENABLE,
151 err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
155 err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
159 /* set the current beacon state */
160 ahw->beacon_state = h_beacon;
162 if (!ahw->beacon_state)
163 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
169 if (len != sizeof(u16))
170 return QL_STATUS_INVALID_PARAM;
172 memcpy(&beacon, buf, sizeof(u16));
173 err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
177 if (adapter->ahw->beacon_state == b_state)
182 if (!adapter->ahw->beacon_state)
183 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
188 if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
193 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
194 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
197 set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
200 err = qlcnic_config_led(adapter, b_state, b_rate);
203 ahw->beacon_state = b_state;
206 if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
207 qlcnic_diag_free_res(adapter->netdev, max_sds_rings);
210 if (!adapter->ahw->beacon_state)
211 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
217 static ssize_t qlcnic_show_beacon(struct device *dev,
218 struct device_attribute *attr, char *buf)
220 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
222 return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
225 static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
226 loff_t offset, size_t size)
230 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
233 if (offset < QLCNIC_PCI_CRBSPACE) {
234 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
235 QLCNIC_PCI_CAMQM_END))
241 if ((size != crb_size) || (offset & (crb_size-1)))
247 static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
248 struct bin_attribute *attr, char *buf,
249 loff_t offset, size_t size)
251 struct device *dev = container_of(kobj, struct device, kobj);
252 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
255 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
258 qlcnic_read_crb(adapter, buf, offset, size);
263 static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
264 struct bin_attribute *attr, char *buf,
265 loff_t offset, size_t size)
267 struct device *dev = container_of(kobj, struct device, kobj);
268 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
271 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
275 qlcnic_write_crb(adapter, buf, offset, size);
279 static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
280 loff_t offset, size_t size)
282 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
285 if ((size != 8) || (offset & 0x7))
291 static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
292 struct bin_attribute *attr, char *buf,
293 loff_t offset, size_t size)
295 struct device *dev = container_of(kobj, struct device, kobj);
296 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
300 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
304 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
307 memcpy(buf, &data, size);
312 static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
313 struct bin_attribute *attr, char *buf,
314 loff_t offset, size_t size)
316 struct device *dev = container_of(kobj, struct device, kobj);
317 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
321 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
325 memcpy(&data, buf, size);
327 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
333 static int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
336 for (i = 0; i < adapter->ahw->act_pci_func; i++) {
337 if (adapter->npars[i].pci_func == pci_func)
344 static int validate_pm_config(struct qlcnic_adapter *adapter,
345 struct qlcnic_pm_func_cfg *pm_cfg, int count)
347 u8 src_pci_func, s_esw_id, d_esw_id;
349 int i, src_index, dest_index;
351 for (i = 0; i < count; i++) {
352 src_pci_func = pm_cfg[i].pci_func;
353 dest_pci_func = pm_cfg[i].dest_npar;
354 src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
357 return QL_STATUS_INVALID_PARAM;
359 dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
361 return QL_STATUS_INVALID_PARAM;
363 s_esw_id = adapter->npars[src_index].phy_port;
364 d_esw_id = adapter->npars[dest_index].phy_port;
366 if (s_esw_id != d_esw_id)
367 return QL_STATUS_INVALID_PARAM;
373 static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
374 struct kobject *kobj,
375 struct bin_attribute *attr,
376 char *buf, loff_t offset,
379 struct device *dev = container_of(kobj, struct device, kobj);
380 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
381 struct qlcnic_pm_func_cfg *pm_cfg;
382 u32 id, action, pci_func;
383 int count, rem, i, ret, index;
385 count = size / sizeof(struct qlcnic_pm_func_cfg);
386 rem = size % sizeof(struct qlcnic_pm_func_cfg);
388 return QL_STATUS_INVALID_PARAM;
390 pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
391 ret = validate_pm_config(adapter, pm_cfg, count);
395 for (i = 0; i < count; i++) {
396 pci_func = pm_cfg[i].pci_func;
397 action = !!pm_cfg[i].action;
398 index = qlcnic_is_valid_nic_func(adapter, pci_func);
400 return QL_STATUS_INVALID_PARAM;
402 id = adapter->npars[index].phy_port;
403 ret = qlcnic_config_port_mirroring(adapter, id,
409 for (i = 0; i < count; i++) {
410 pci_func = pm_cfg[i].pci_func;
411 index = qlcnic_is_valid_nic_func(adapter, pci_func);
412 id = adapter->npars[index].phy_port;
413 adapter->npars[index].enable_pm = !!pm_cfg[i].action;
414 adapter->npars[index].dest_npar = id;
420 static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
421 struct kobject *kobj,
422 struct bin_attribute *attr,
423 char *buf, loff_t offset,
426 struct device *dev = container_of(kobj, struct device, kobj);
427 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
428 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
432 if (size != sizeof(pm_cfg))
433 return QL_STATUS_INVALID_PARAM;
436 sizeof(struct qlcnic_pm_func_cfg) * QLCNIC_MAX_PCI_FUNC);
438 for (i = 0; i < adapter->ahw->act_pci_func; i++) {
439 pci_func = adapter->npars[i].pci_func;
440 pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
441 pm_cfg[pci_func].dest_npar = 0;
442 pm_cfg[pci_func].pci_func = i;
444 memcpy(buf, &pm_cfg, size);
449 static int validate_esw_config(struct qlcnic_adapter *adapter,
450 struct qlcnic_esw_func_cfg *esw_cfg, int count)
456 if (qlcnic_82xx_check(adapter))
457 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
459 op_mode = QLCRDX(adapter->ahw, QLC_83XX_DRV_OP_MODE);
461 for (i = 0; i < count; i++) {
462 pci_func = esw_cfg[i].pci_func;
463 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
464 return QL_STATUS_INVALID_PARAM;
466 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
467 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
468 return QL_STATUS_INVALID_PARAM;
470 switch (esw_cfg[i].op_mode) {
471 case QLCNIC_PORT_DEFAULTS:
472 if (qlcnic_82xx_check(adapter)) {
473 ret = QLC_DEV_GET_DRV(op_mode, pci_func);
475 ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
477 esw_cfg[i].offload_flags = 0;
480 if (ret != QLCNIC_NON_PRIV_FUNC) {
481 if (esw_cfg[i].mac_anti_spoof != 0)
482 return QL_STATUS_INVALID_PARAM;
483 if (esw_cfg[i].mac_override != 1)
484 return QL_STATUS_INVALID_PARAM;
485 if (esw_cfg[i].promisc_mode != 1)
486 return QL_STATUS_INVALID_PARAM;
489 case QLCNIC_ADD_VLAN:
490 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
491 return QL_STATUS_INVALID_PARAM;
492 if (!esw_cfg[i].op_type)
493 return QL_STATUS_INVALID_PARAM;
495 case QLCNIC_DEL_VLAN:
496 if (!esw_cfg[i].op_type)
497 return QL_STATUS_INVALID_PARAM;
500 return QL_STATUS_INVALID_PARAM;
507 static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
508 struct kobject *kobj,
509 struct bin_attribute *attr,
510 char *buf, loff_t offset,
513 struct device *dev = container_of(kobj, struct device, kobj);
514 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
515 struct qlcnic_esw_func_cfg *esw_cfg;
516 struct qlcnic_npar_info *npar;
517 int count, rem, i, ret;
519 u8 op_mode = 0, pci_func;
521 count = size / sizeof(struct qlcnic_esw_func_cfg);
522 rem = size % sizeof(struct qlcnic_esw_func_cfg);
524 return QL_STATUS_INVALID_PARAM;
526 esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
527 ret = validate_esw_config(adapter, esw_cfg, count);
531 for (i = 0; i < count; i++) {
532 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
533 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
534 return QL_STATUS_INVALID_PARAM;
536 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
539 op_mode = esw_cfg[i].op_mode;
540 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
541 esw_cfg[i].op_mode = op_mode;
542 esw_cfg[i].pci_func = adapter->ahw->pci_func;
544 switch (esw_cfg[i].op_mode) {
545 case QLCNIC_PORT_DEFAULTS:
546 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
548 case QLCNIC_ADD_VLAN:
549 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
551 case QLCNIC_DEL_VLAN:
552 esw_cfg[i].vlan_id = 0;
553 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
558 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
561 for (i = 0; i < count; i++) {
562 pci_func = esw_cfg[i].pci_func;
563 index = qlcnic_is_valid_nic_func(adapter, pci_func);
564 npar = &adapter->npars[index];
565 switch (esw_cfg[i].op_mode) {
566 case QLCNIC_PORT_DEFAULTS:
567 npar->promisc_mode = esw_cfg[i].promisc_mode;
568 npar->mac_override = esw_cfg[i].mac_override;
569 npar->offload_flags = esw_cfg[i].offload_flags;
570 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
571 npar->discard_tagged = esw_cfg[i].discard_tagged;
573 case QLCNIC_ADD_VLAN:
574 npar->pvid = esw_cfg[i].vlan_id;
576 case QLCNIC_DEL_VLAN:
585 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
586 struct kobject *kobj,
587 struct bin_attribute *attr,
588 char *buf, loff_t offset,
591 struct device *dev = container_of(kobj, struct device, kobj);
592 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
593 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
596 if (size != sizeof(esw_cfg))
597 return QL_STATUS_INVALID_PARAM;
600 sizeof(struct qlcnic_esw_func_cfg) * QLCNIC_MAX_PCI_FUNC);
602 for (i = 0; i < adapter->ahw->act_pci_func; i++) {
603 pci_func = adapter->npars[i].pci_func;
604 esw_cfg[pci_func].pci_func = pci_func;
605 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
606 return QL_STATUS_INVALID_PARAM;
609 memcpy(buf, &esw_cfg, size);
614 static int validate_npar_config(struct qlcnic_adapter *adapter,
615 struct qlcnic_npar_func_cfg *np_cfg,
620 for (i = 0; i < count; i++) {
621 pci_func = np_cfg[i].pci_func;
622 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
623 return QL_STATUS_INVALID_PARAM;
625 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
626 !IS_VALID_BW(np_cfg[i].max_bw))
627 return QL_STATUS_INVALID_PARAM;
632 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
633 struct kobject *kobj,
634 struct bin_attribute *attr,
635 char *buf, loff_t offset,
638 struct device *dev = container_of(kobj, struct device, kobj);
639 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
640 struct qlcnic_info nic_info;
641 struct qlcnic_npar_func_cfg *np_cfg;
642 int i, count, rem, ret, index;
645 count = size / sizeof(struct qlcnic_npar_func_cfg);
646 rem = size % sizeof(struct qlcnic_npar_func_cfg);
648 return QL_STATUS_INVALID_PARAM;
650 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
651 ret = validate_npar_config(adapter, np_cfg, count);
655 for (i = 0; i < count; i++) {
656 pci_func = np_cfg[i].pci_func;
658 memset(&nic_info, 0, sizeof(struct qlcnic_info));
659 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
662 nic_info.pci_func = pci_func;
663 nic_info.min_tx_bw = np_cfg[i].min_bw;
664 nic_info.max_tx_bw = np_cfg[i].max_bw;
665 ret = qlcnic_set_nic_info(adapter, &nic_info);
668 index = qlcnic_is_valid_nic_func(adapter, pci_func);
669 adapter->npars[index].min_bw = nic_info.min_tx_bw;
670 adapter->npars[index].max_bw = nic_info.max_tx_bw;
676 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
677 struct kobject *kobj,
678 struct bin_attribute *attr,
679 char *buf, loff_t offset,
682 struct device *dev = container_of(kobj, struct device, kobj);
683 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
684 struct qlcnic_info nic_info;
685 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
688 if (size != sizeof(np_cfg))
689 return QL_STATUS_INVALID_PARAM;
691 memset(&nic_info, 0, sizeof(struct qlcnic_info));
693 sizeof(struct qlcnic_npar_func_cfg) * QLCNIC_MAX_PCI_FUNC);
695 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
696 if (qlcnic_is_valid_nic_func(adapter, i) < 0)
698 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
702 np_cfg[i].pci_func = i;
703 np_cfg[i].op_mode = (u8)nic_info.op_mode;
704 np_cfg[i].port_num = nic_info.phys_port;
705 np_cfg[i].fw_capab = nic_info.capabilities;
706 np_cfg[i].min_bw = nic_info.min_tx_bw;
707 np_cfg[i].max_bw = nic_info.max_tx_bw;
708 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
709 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
712 memcpy(buf, &np_cfg, size);
716 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
717 struct kobject *kobj,
718 struct bin_attribute *attr,
719 char *buf, loff_t offset,
722 struct device *dev = container_of(kobj, struct device, kobj);
723 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
724 struct qlcnic_esw_statistics port_stats;
727 if (qlcnic_83xx_check(adapter))
728 return QLC_STATUS_UNSUPPORTED_CMD;
730 if (size != sizeof(struct qlcnic_esw_statistics))
731 return QL_STATUS_INVALID_PARAM;
733 if (offset >= QLCNIC_MAX_PCI_FUNC)
734 return QL_STATUS_INVALID_PARAM;
736 memset(&port_stats, 0, size);
737 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
742 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
747 memcpy(buf, &port_stats, size);
751 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
752 struct kobject *kobj,
753 struct bin_attribute *attr,
754 char *buf, loff_t offset,
757 struct device *dev = container_of(kobj, struct device, kobj);
758 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
759 struct qlcnic_esw_statistics esw_stats;
762 if (qlcnic_83xx_check(adapter))
763 return QLC_STATUS_UNSUPPORTED_CMD;
765 if (size != sizeof(struct qlcnic_esw_statistics))
766 return QL_STATUS_INVALID_PARAM;
768 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
769 return QL_STATUS_INVALID_PARAM;
771 memset(&esw_stats, 0, size);
772 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
777 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
782 memcpy(buf, &esw_stats, size);
786 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
787 struct kobject *kobj,
788 struct bin_attribute *attr,
789 char *buf, loff_t offset,
792 struct device *dev = container_of(kobj, struct device, kobj);
793 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
796 if (qlcnic_83xx_check(adapter))
797 return QLC_STATUS_UNSUPPORTED_CMD;
799 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
800 return QL_STATUS_INVALID_PARAM;
802 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
803 QLCNIC_QUERY_RX_COUNTER);
807 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
808 QLCNIC_QUERY_TX_COUNTER);
815 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
816 struct kobject *kobj,
817 struct bin_attribute *attr,
818 char *buf, loff_t offset,
822 struct device *dev = container_of(kobj, struct device, kobj);
823 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
826 if (qlcnic_83xx_check(adapter))
827 return QLC_STATUS_UNSUPPORTED_CMD;
829 if (offset >= QLCNIC_MAX_PCI_FUNC)
830 return QL_STATUS_INVALID_PARAM;
832 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
833 QLCNIC_QUERY_RX_COUNTER);
837 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
838 QLCNIC_QUERY_TX_COUNTER);
845 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
846 struct kobject *kobj,
847 struct bin_attribute *attr,
848 char *buf, loff_t offset,
851 struct device *dev = container_of(kobj, struct device, kobj);
852 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
853 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
854 struct qlcnic_pci_info *pci_info;
857 if (size != sizeof(pci_cfg))
858 return QL_STATUS_INVALID_PARAM;
860 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
864 ret = qlcnic_get_pci_info(adapter, pci_info);
871 sizeof(struct qlcnic_pci_func_cfg) * QLCNIC_MAX_PCI_FUNC);
873 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
874 pci_cfg[i].pci_func = pci_info[i].id;
875 pci_cfg[i].func_type = pci_info[i].type;
876 pci_cfg[i].port_num = pci_info[i].default_port;
877 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
878 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
879 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
882 memcpy(buf, &pci_cfg, size);
887 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
888 struct kobject *kobj,
889 struct bin_attribute *attr,
890 char *buf, loff_t offset,
893 unsigned char *p_read_buf;
895 struct device *dev = container_of(kobj, struct device, kobj);
896 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
899 return QL_STATUS_INVALID_PARAM;
901 return QL_STATUS_INVALID_PARAM;
903 count = size / sizeof(u32);
905 if (size % sizeof(u32))
908 p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
911 if (qlcnic_83xx_lock_flash(adapter) != 0) {
916 ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
920 qlcnic_83xx_unlock_flash(adapter);
925 qlcnic_83xx_unlock_flash(adapter);
926 memcpy(buf, p_read_buf, size);
932 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
933 char *buf, loff_t offset,
937 unsigned char *p_cache, *p_src;
939 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
943 memcpy(p_cache, buf, size);
945 count = size / sizeof(u32);
947 if (qlcnic_83xx_lock_flash(adapter) != 0) {
952 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
953 ret = qlcnic_83xx_enable_flash_write(adapter);
956 qlcnic_83xx_unlock_flash(adapter);
961 for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
962 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
964 QLC_83XX_FLASH_WRITE_MAX);
967 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
968 ret = qlcnic_83xx_disable_flash_write(adapter);
971 qlcnic_83xx_unlock_flash(adapter);
977 qlcnic_83xx_unlock_flash(adapter);
981 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
982 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
985 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
986 ret = qlcnic_83xx_disable_flash_write(adapter);
989 qlcnic_83xx_unlock_flash(adapter);
995 qlcnic_83xx_unlock_flash(adapter);
1000 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1001 char *buf, loff_t offset, size_t size)
1004 unsigned char *p_cache, *p_src;
1006 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1010 memcpy(p_cache, buf, size);
1012 count = size / sizeof(u32);
1014 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1019 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1020 ret = qlcnic_83xx_enable_flash_write(adapter);
1023 qlcnic_83xx_unlock_flash(adapter);
1028 for (i = 0; i < count; i++) {
1029 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
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);
1040 qlcnic_83xx_unlock_flash(adapter);
1044 p_src = p_src + sizeof(u32);
1045 offset = offset + sizeof(u32);
1048 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1049 ret = qlcnic_83xx_disable_flash_write(adapter);
1052 qlcnic_83xx_unlock_flash(adapter);
1058 qlcnic_83xx_unlock_flash(adapter);
1063 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1064 struct kobject *kobj,
1065 struct bin_attribute *attr,
1066 char *buf, loff_t offset,
1070 static int flash_mode;
1072 struct device *dev = container_of(kobj, struct device, kobj);
1073 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1076 return QL_STATUS_INVALID_PARAM;
1078 ret = kstrtoul(buf, 16, &data);
1081 case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1082 flash_mode = QLC_83XX_ERASE_MODE;
1083 ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1085 dev_err(&adapter->pdev->dev,
1086 "%s failed at %d\n", __func__, __LINE__);
1091 case QLC_83XX_FLASH_BULK_WRITE_CMD:
1092 flash_mode = QLC_83XX_BULK_WRITE_MODE;
1095 case QLC_83XX_FLASH_WRITE_CMD:
1096 flash_mode = QLC_83XX_WRITE_MODE;
1099 if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1100 ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1103 dev_err(&adapter->pdev->dev,
1104 "%s failed at %d\n",
1105 __func__, __LINE__);
1110 if (flash_mode == QLC_83XX_WRITE_MODE) {
1111 ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1114 dev_err(&adapter->pdev->dev,
1115 "%s failed at %d\n", __func__,
1125 static struct device_attribute dev_attr_bridged_mode = {
1126 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1127 .show = qlcnic_show_bridged_mode,
1128 .store = qlcnic_store_bridged_mode,
1131 static struct device_attribute dev_attr_diag_mode = {
1132 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1133 .show = qlcnic_show_diag_mode,
1134 .store = qlcnic_store_diag_mode,
1137 static struct device_attribute dev_attr_beacon = {
1138 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1139 .show = qlcnic_show_beacon,
1140 .store = qlcnic_store_beacon,
1143 static struct bin_attribute bin_attr_crb = {
1144 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1146 .read = qlcnic_sysfs_read_crb,
1147 .write = qlcnic_sysfs_write_crb,
1150 static struct bin_attribute bin_attr_mem = {
1151 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1153 .read = qlcnic_sysfs_read_mem,
1154 .write = qlcnic_sysfs_write_mem,
1157 static struct bin_attribute bin_attr_npar_config = {
1158 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1160 .read = qlcnic_sysfs_read_npar_config,
1161 .write = qlcnic_sysfs_write_npar_config,
1164 static struct bin_attribute bin_attr_pci_config = {
1165 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1167 .read = qlcnic_sysfs_read_pci_config,
1171 static struct bin_attribute bin_attr_port_stats = {
1172 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1174 .read = qlcnic_sysfs_get_port_stats,
1175 .write = qlcnic_sysfs_clear_port_stats,
1178 static struct bin_attribute bin_attr_esw_stats = {
1179 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1181 .read = qlcnic_sysfs_get_esw_stats,
1182 .write = qlcnic_sysfs_clear_esw_stats,
1185 static struct bin_attribute bin_attr_esw_config = {
1186 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1188 .read = qlcnic_sysfs_read_esw_config,
1189 .write = qlcnic_sysfs_write_esw_config,
1192 static struct bin_attribute bin_attr_pm_config = {
1193 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1195 .read = qlcnic_sysfs_read_pm_config,
1196 .write = qlcnic_sysfs_write_pm_config,
1199 static struct bin_attribute bin_attr_flash = {
1200 .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1202 .read = qlcnic_83xx_sysfs_flash_read_handler,
1203 .write = qlcnic_83xx_sysfs_flash_write_handler,
1206 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1208 struct device *dev = &adapter->pdev->dev;
1210 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1211 if (device_create_file(dev, &dev_attr_bridged_mode))
1213 "failed to create bridged_mode sysfs entry\n");
1216 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1218 struct device *dev = &adapter->pdev->dev;
1220 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1221 device_remove_file(dev, &dev_attr_bridged_mode);
1224 void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1226 struct device *dev = &adapter->pdev->dev;
1228 if (device_create_bin_file(dev, &bin_attr_port_stats))
1229 dev_info(dev, "failed to create port stats sysfs entry");
1231 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1233 if (device_create_file(dev, &dev_attr_diag_mode))
1234 dev_info(dev, "failed to create diag_mode sysfs entry\n");
1235 if (device_create_bin_file(dev, &bin_attr_crb))
1236 dev_info(dev, "failed to create crb sysfs entry\n");
1237 if (device_create_bin_file(dev, &bin_attr_mem))
1238 dev_info(dev, "failed to create mem sysfs entry\n");
1240 if (device_create_bin_file(dev, &bin_attr_pci_config))
1241 dev_info(dev, "failed to create pci config sysfs entry");
1242 if (device_create_file(dev, &dev_attr_beacon))
1243 dev_info(dev, "failed to create beacon sysfs entry");
1245 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1247 if (device_create_bin_file(dev, &bin_attr_esw_config))
1248 dev_info(dev, "failed to create esw config sysfs entry");
1249 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1251 if (device_create_bin_file(dev, &bin_attr_npar_config))
1252 dev_info(dev, "failed to create npar config sysfs entry");
1253 if (device_create_bin_file(dev, &bin_attr_pm_config))
1254 dev_info(dev, "failed to create pm config sysfs entry");
1255 if (device_create_bin_file(dev, &bin_attr_esw_stats))
1256 dev_info(dev, "failed to create eswitch stats sysfs entry");
1259 void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1261 struct device *dev = &adapter->pdev->dev;
1263 device_remove_bin_file(dev, &bin_attr_port_stats);
1265 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1267 device_remove_file(dev, &dev_attr_diag_mode);
1268 device_remove_bin_file(dev, &bin_attr_crb);
1269 device_remove_bin_file(dev, &bin_attr_mem);
1270 device_remove_bin_file(dev, &bin_attr_pci_config);
1271 device_remove_file(dev, &dev_attr_beacon);
1272 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1274 device_remove_bin_file(dev, &bin_attr_esw_config);
1275 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1277 device_remove_bin_file(dev, &bin_attr_npar_config);
1278 device_remove_bin_file(dev, &bin_attr_pm_config);
1279 device_remove_bin_file(dev, &bin_attr_esw_stats);
1282 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1284 qlcnic_create_diag_entries(adapter);
1287 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1289 qlcnic_remove_diag_entries(adapter);
1292 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1294 struct device *dev = &adapter->pdev->dev;
1296 qlcnic_create_diag_entries(adapter);
1298 if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1299 dev_info(dev, "failed to create flash sysfs entry\n");
1302 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1304 struct device *dev = &adapter->pdev->dev;
1306 qlcnic_remove_diag_entries(adapter);
1307 sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);