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 qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
551 case QLCNIC_ADD_VLAN:
552 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
554 case QLCNIC_DEL_VLAN:
555 esw_cfg[i].vlan_id = 0;
556 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
561 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
564 for (i = 0; i < count; i++) {
565 pci_func = esw_cfg[i].pci_func;
566 index = qlcnic_is_valid_nic_func(adapter, pci_func);
567 npar = &adapter->npars[index];
568 switch (esw_cfg[i].op_mode) {
569 case QLCNIC_PORT_DEFAULTS:
570 npar->promisc_mode = esw_cfg[i].promisc_mode;
571 npar->mac_override = esw_cfg[i].mac_override;
572 npar->offload_flags = esw_cfg[i].offload_flags;
573 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
574 npar->discard_tagged = esw_cfg[i].discard_tagged;
576 case QLCNIC_ADD_VLAN:
577 npar->pvid = esw_cfg[i].vlan_id;
579 case QLCNIC_DEL_VLAN:
588 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
589 struct kobject *kobj,
590 struct bin_attribute *attr,
591 char *buf, loff_t offset,
594 struct device *dev = container_of(kobj, struct device, kobj);
595 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
596 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
599 if (size != sizeof(esw_cfg))
600 return QL_STATUS_INVALID_PARAM;
603 sizeof(struct qlcnic_esw_func_cfg) * QLCNIC_MAX_PCI_FUNC);
605 for (i = 0; i < adapter->ahw->act_pci_func; i++) {
606 pci_func = adapter->npars[i].pci_func;
607 esw_cfg[pci_func].pci_func = pci_func;
608 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
609 return QL_STATUS_INVALID_PARAM;
612 memcpy(buf, &esw_cfg, size);
617 static int validate_npar_config(struct qlcnic_adapter *adapter,
618 struct qlcnic_npar_func_cfg *np_cfg,
623 for (i = 0; i < count; i++) {
624 pci_func = np_cfg[i].pci_func;
625 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
626 return QL_STATUS_INVALID_PARAM;
628 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
629 !IS_VALID_BW(np_cfg[i].max_bw))
630 return QL_STATUS_INVALID_PARAM;
635 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
636 struct kobject *kobj,
637 struct bin_attribute *attr,
638 char *buf, loff_t offset,
641 struct device *dev = container_of(kobj, struct device, kobj);
642 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
643 struct qlcnic_info nic_info;
644 struct qlcnic_npar_func_cfg *np_cfg;
645 int i, count, rem, ret, index;
648 count = size / sizeof(struct qlcnic_npar_func_cfg);
649 rem = size % sizeof(struct qlcnic_npar_func_cfg);
651 return QL_STATUS_INVALID_PARAM;
653 np_cfg = (struct qlcnic_npar_func_cfg *)buf;
654 ret = validate_npar_config(adapter, np_cfg, count);
658 for (i = 0; i < count; i++) {
659 pci_func = np_cfg[i].pci_func;
661 memset(&nic_info, 0, sizeof(struct qlcnic_info));
662 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
665 nic_info.pci_func = pci_func;
666 nic_info.min_tx_bw = np_cfg[i].min_bw;
667 nic_info.max_tx_bw = np_cfg[i].max_bw;
668 ret = qlcnic_set_nic_info(adapter, &nic_info);
671 index = qlcnic_is_valid_nic_func(adapter, pci_func);
672 adapter->npars[index].min_bw = nic_info.min_tx_bw;
673 adapter->npars[index].max_bw = nic_info.max_tx_bw;
679 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
680 struct kobject *kobj,
681 struct bin_attribute *attr,
682 char *buf, loff_t offset,
685 struct device *dev = container_of(kobj, struct device, kobj);
686 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
687 struct qlcnic_info nic_info;
688 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
691 if (size != sizeof(np_cfg))
692 return QL_STATUS_INVALID_PARAM;
694 memset(&nic_info, 0, sizeof(struct qlcnic_info));
696 sizeof(struct qlcnic_npar_func_cfg) * QLCNIC_MAX_PCI_FUNC);
698 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
699 if (qlcnic_is_valid_nic_func(adapter, i) < 0)
701 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
705 np_cfg[i].pci_func = i;
706 np_cfg[i].op_mode = (u8)nic_info.op_mode;
707 np_cfg[i].port_num = nic_info.phys_port;
708 np_cfg[i].fw_capab = nic_info.capabilities;
709 np_cfg[i].min_bw = nic_info.min_tx_bw;
710 np_cfg[i].max_bw = nic_info.max_tx_bw;
711 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
712 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
715 memcpy(buf, &np_cfg, size);
719 static ssize_t qlcnic_sysfs_get_port_stats(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_esw_statistics port_stats;
730 if (qlcnic_83xx_check(adapter))
731 return QLC_STATUS_UNSUPPORTED_CMD;
733 if (size != sizeof(struct qlcnic_esw_statistics))
734 return QL_STATUS_INVALID_PARAM;
736 if (offset >= QLCNIC_MAX_PCI_FUNC)
737 return QL_STATUS_INVALID_PARAM;
739 memset(&port_stats, 0, size);
740 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
745 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
750 memcpy(buf, &port_stats, size);
754 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
755 struct kobject *kobj,
756 struct bin_attribute *attr,
757 char *buf, loff_t offset,
760 struct device *dev = container_of(kobj, struct device, kobj);
761 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
762 struct qlcnic_esw_statistics esw_stats;
765 if (qlcnic_83xx_check(adapter))
766 return QLC_STATUS_UNSUPPORTED_CMD;
768 if (size != sizeof(struct qlcnic_esw_statistics))
769 return QL_STATUS_INVALID_PARAM;
771 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
772 return QL_STATUS_INVALID_PARAM;
774 memset(&esw_stats, 0, size);
775 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
780 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
785 memcpy(buf, &esw_stats, size);
789 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
790 struct kobject *kobj,
791 struct bin_attribute *attr,
792 char *buf, loff_t offset,
795 struct device *dev = container_of(kobj, struct device, kobj);
796 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
799 if (qlcnic_83xx_check(adapter))
800 return QLC_STATUS_UNSUPPORTED_CMD;
802 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
803 return QL_STATUS_INVALID_PARAM;
805 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
806 QLCNIC_QUERY_RX_COUNTER);
810 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
811 QLCNIC_QUERY_TX_COUNTER);
818 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
819 struct kobject *kobj,
820 struct bin_attribute *attr,
821 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_MAX_PCI_FUNC)
833 return QL_STATUS_INVALID_PARAM;
835 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
836 QLCNIC_QUERY_RX_COUNTER);
840 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
841 QLCNIC_QUERY_TX_COUNTER);
848 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
849 struct kobject *kobj,
850 struct bin_attribute *attr,
851 char *buf, loff_t offset,
854 struct device *dev = container_of(kobj, struct device, kobj);
855 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
856 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
857 struct qlcnic_pci_info *pci_info;
860 if (size != sizeof(pci_cfg))
861 return QL_STATUS_INVALID_PARAM;
863 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
867 ret = qlcnic_get_pci_info(adapter, pci_info);
874 sizeof(struct qlcnic_pci_func_cfg) * QLCNIC_MAX_PCI_FUNC);
876 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
877 pci_cfg[i].pci_func = pci_info[i].id;
878 pci_cfg[i].func_type = pci_info[i].type;
879 pci_cfg[i].port_num = pci_info[i].default_port;
880 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
881 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
882 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
885 memcpy(buf, &pci_cfg, size);
890 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
891 struct kobject *kobj,
892 struct bin_attribute *attr,
893 char *buf, loff_t offset,
896 unsigned char *p_read_buf;
898 struct device *dev = container_of(kobj, struct device, kobj);
899 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
902 return QL_STATUS_INVALID_PARAM;
904 return QL_STATUS_INVALID_PARAM;
906 count = size / sizeof(u32);
908 if (size % sizeof(u32))
911 p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
914 if (qlcnic_83xx_lock_flash(adapter) != 0) {
919 ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
923 qlcnic_83xx_unlock_flash(adapter);
928 qlcnic_83xx_unlock_flash(adapter);
929 memcpy(buf, p_read_buf, size);
935 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
936 char *buf, loff_t offset,
940 unsigned char *p_cache, *p_src;
942 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
946 memcpy(p_cache, buf, size);
948 count = size / sizeof(u32);
950 if (qlcnic_83xx_lock_flash(adapter) != 0) {
955 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
956 ret = qlcnic_83xx_enable_flash_write(adapter);
959 qlcnic_83xx_unlock_flash(adapter);
964 for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
965 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
967 QLC_83XX_FLASH_WRITE_MAX);
970 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
971 ret = qlcnic_83xx_disable_flash_write(adapter);
974 qlcnic_83xx_unlock_flash(adapter);
980 qlcnic_83xx_unlock_flash(adapter);
984 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
985 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
988 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
989 ret = qlcnic_83xx_disable_flash_write(adapter);
992 qlcnic_83xx_unlock_flash(adapter);
998 qlcnic_83xx_unlock_flash(adapter);
1003 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1004 char *buf, loff_t offset, size_t size)
1007 unsigned char *p_cache, *p_src;
1009 p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1013 memcpy(p_cache, buf, size);
1015 count = size / sizeof(u32);
1017 if (qlcnic_83xx_lock_flash(adapter) != 0) {
1022 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1023 ret = qlcnic_83xx_enable_flash_write(adapter);
1026 qlcnic_83xx_unlock_flash(adapter);
1031 for (i = 0; i < count; i++) {
1032 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1034 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1035 ret = qlcnic_83xx_disable_flash_write(adapter);
1038 qlcnic_83xx_unlock_flash(adapter);
1043 qlcnic_83xx_unlock_flash(adapter);
1047 p_src = p_src + sizeof(u32);
1048 offset = offset + sizeof(u32);
1051 if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1052 ret = qlcnic_83xx_disable_flash_write(adapter);
1055 qlcnic_83xx_unlock_flash(adapter);
1061 qlcnic_83xx_unlock_flash(adapter);
1066 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1067 struct kobject *kobj,
1068 struct bin_attribute *attr,
1069 char *buf, loff_t offset,
1073 static int flash_mode;
1075 struct device *dev = container_of(kobj, struct device, kobj);
1076 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1079 return QL_STATUS_INVALID_PARAM;
1081 ret = kstrtoul(buf, 16, &data);
1084 case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1085 flash_mode = QLC_83XX_ERASE_MODE;
1086 ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1088 dev_err(&adapter->pdev->dev,
1089 "%s failed at %d\n", __func__, __LINE__);
1094 case QLC_83XX_FLASH_BULK_WRITE_CMD:
1095 flash_mode = QLC_83XX_BULK_WRITE_MODE;
1098 case QLC_83XX_FLASH_WRITE_CMD:
1099 flash_mode = QLC_83XX_WRITE_MODE;
1102 if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1103 ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1106 dev_err(&adapter->pdev->dev,
1107 "%s failed at %d\n",
1108 __func__, __LINE__);
1113 if (flash_mode == QLC_83XX_WRITE_MODE) {
1114 ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1117 dev_err(&adapter->pdev->dev,
1118 "%s failed at %d\n", __func__,
1128 static struct device_attribute dev_attr_bridged_mode = {
1129 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1130 .show = qlcnic_show_bridged_mode,
1131 .store = qlcnic_store_bridged_mode,
1134 static struct device_attribute dev_attr_diag_mode = {
1135 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1136 .show = qlcnic_show_diag_mode,
1137 .store = qlcnic_store_diag_mode,
1140 static struct device_attribute dev_attr_beacon = {
1141 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1142 .show = qlcnic_show_beacon,
1143 .store = qlcnic_store_beacon,
1146 static struct bin_attribute bin_attr_crb = {
1147 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1149 .read = qlcnic_sysfs_read_crb,
1150 .write = qlcnic_sysfs_write_crb,
1153 static struct bin_attribute bin_attr_mem = {
1154 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1156 .read = qlcnic_sysfs_read_mem,
1157 .write = qlcnic_sysfs_write_mem,
1160 static struct bin_attribute bin_attr_npar_config = {
1161 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1163 .read = qlcnic_sysfs_read_npar_config,
1164 .write = qlcnic_sysfs_write_npar_config,
1167 static struct bin_attribute bin_attr_pci_config = {
1168 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1170 .read = qlcnic_sysfs_read_pci_config,
1174 static struct bin_attribute bin_attr_port_stats = {
1175 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1177 .read = qlcnic_sysfs_get_port_stats,
1178 .write = qlcnic_sysfs_clear_port_stats,
1181 static struct bin_attribute bin_attr_esw_stats = {
1182 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1184 .read = qlcnic_sysfs_get_esw_stats,
1185 .write = qlcnic_sysfs_clear_esw_stats,
1188 static struct bin_attribute bin_attr_esw_config = {
1189 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1191 .read = qlcnic_sysfs_read_esw_config,
1192 .write = qlcnic_sysfs_write_esw_config,
1195 static struct bin_attribute bin_attr_pm_config = {
1196 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1198 .read = qlcnic_sysfs_read_pm_config,
1199 .write = qlcnic_sysfs_write_pm_config,
1202 static struct bin_attribute bin_attr_flash = {
1203 .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1205 .read = qlcnic_83xx_sysfs_flash_read_handler,
1206 .write = qlcnic_83xx_sysfs_flash_write_handler,
1209 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1211 struct device *dev = &adapter->pdev->dev;
1213 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1214 if (device_create_file(dev, &dev_attr_bridged_mode))
1216 "failed to create bridged_mode sysfs entry\n");
1219 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1221 struct device *dev = &adapter->pdev->dev;
1223 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1224 device_remove_file(dev, &dev_attr_bridged_mode);
1227 void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1229 struct device *dev = &adapter->pdev->dev;
1231 if (device_create_bin_file(dev, &bin_attr_port_stats))
1232 dev_info(dev, "failed to create port stats sysfs entry");
1234 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1236 if (device_create_file(dev, &dev_attr_diag_mode))
1237 dev_info(dev, "failed to create diag_mode sysfs entry\n");
1238 if (device_create_bin_file(dev, &bin_attr_crb))
1239 dev_info(dev, "failed to create crb sysfs entry\n");
1240 if (device_create_bin_file(dev, &bin_attr_mem))
1241 dev_info(dev, "failed to create mem sysfs entry\n");
1243 if (device_create_bin_file(dev, &bin_attr_pci_config))
1244 dev_info(dev, "failed to create pci config sysfs entry");
1245 if (device_create_file(dev, &dev_attr_beacon))
1246 dev_info(dev, "failed to create beacon sysfs entry");
1248 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1250 if (device_create_bin_file(dev, &bin_attr_esw_config))
1251 dev_info(dev, "failed to create esw config sysfs entry");
1252 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1254 if (device_create_bin_file(dev, &bin_attr_npar_config))
1255 dev_info(dev, "failed to create npar config sysfs entry");
1256 if (device_create_bin_file(dev, &bin_attr_pm_config))
1257 dev_info(dev, "failed to create pm config sysfs entry");
1258 if (device_create_bin_file(dev, &bin_attr_esw_stats))
1259 dev_info(dev, "failed to create eswitch stats sysfs entry");
1262 void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1264 struct device *dev = &adapter->pdev->dev;
1266 device_remove_bin_file(dev, &bin_attr_port_stats);
1268 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1270 device_remove_file(dev, &dev_attr_diag_mode);
1271 device_remove_bin_file(dev, &bin_attr_crb);
1272 device_remove_bin_file(dev, &bin_attr_mem);
1273 device_remove_bin_file(dev, &bin_attr_pci_config);
1274 device_remove_file(dev, &dev_attr_beacon);
1275 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1277 device_remove_bin_file(dev, &bin_attr_esw_config);
1278 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1280 device_remove_bin_file(dev, &bin_attr_npar_config);
1281 device_remove_bin_file(dev, &bin_attr_pm_config);
1282 device_remove_bin_file(dev, &bin_attr_esw_stats);
1285 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1287 qlcnic_create_diag_entries(adapter);
1290 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1292 qlcnic_remove_diag_entries(adapter);
1295 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1297 struct device *dev = &adapter->pdev->dev;
1299 qlcnic_create_diag_entries(adapter);
1301 if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1302 dev_info(dev, "failed to create flash sysfs entry\n");
1305 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1307 struct device *dev = &adapter->pdev->dev;
1309 qlcnic_remove_diag_entries(adapter);
1310 sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);