|
8 | 8 |
|
9 | 9 | #include <linux/kernel.h> |
10 | 10 | #include <linux/module.h> |
| 11 | +#include <scsi/scsi_device.h> |
11 | 12 | #include <linux/libata.h> |
12 | 13 |
|
13 | 14 | #include "libata.h" |
@@ -764,3 +765,300 @@ bool sata_lpm_ignore_phy_events(struct ata_link *link) |
764 | 765 | return false; |
765 | 766 | } |
766 | 767 | EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events); |
| 768 | + |
| 769 | +static const char *ata_lpm_policy_names[] = { |
| 770 | + [ATA_LPM_UNKNOWN] = "max_performance", |
| 771 | + [ATA_LPM_MAX_POWER] = "max_performance", |
| 772 | + [ATA_LPM_MED_POWER] = "medium_power", |
| 773 | + [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm", |
| 774 | + [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial", |
| 775 | + [ATA_LPM_MIN_POWER] = "min_power", |
| 776 | +}; |
| 777 | + |
| 778 | +static ssize_t ata_scsi_lpm_store(struct device *device, |
| 779 | + struct device_attribute *attr, |
| 780 | + const char *buf, size_t count) |
| 781 | +{ |
| 782 | + struct Scsi_Host *shost = class_to_shost(device); |
| 783 | + struct ata_port *ap = ata_shost_to_port(shost); |
| 784 | + struct ata_link *link; |
| 785 | + struct ata_device *dev; |
| 786 | + enum ata_lpm_policy policy; |
| 787 | + unsigned long flags; |
| 788 | + |
| 789 | + /* UNKNOWN is internal state, iterate from MAX_POWER */ |
| 790 | + for (policy = ATA_LPM_MAX_POWER; |
| 791 | + policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) { |
| 792 | + const char *name = ata_lpm_policy_names[policy]; |
| 793 | + |
| 794 | + if (strncmp(name, buf, strlen(name)) == 0) |
| 795 | + break; |
| 796 | + } |
| 797 | + if (policy == ARRAY_SIZE(ata_lpm_policy_names)) |
| 798 | + return -EINVAL; |
| 799 | + |
| 800 | + spin_lock_irqsave(ap->lock, flags); |
| 801 | + |
| 802 | + ata_for_each_link(link, ap, EDGE) { |
| 803 | + ata_for_each_dev(dev, &ap->link, ENABLED) { |
| 804 | + if (dev->horkage & ATA_HORKAGE_NOLPM) { |
| 805 | + count = -EOPNOTSUPP; |
| 806 | + goto out_unlock; |
| 807 | + } |
| 808 | + } |
| 809 | + } |
| 810 | + |
| 811 | + ap->target_lpm_policy = policy; |
| 812 | + ata_port_schedule_eh(ap); |
| 813 | +out_unlock: |
| 814 | + spin_unlock_irqrestore(ap->lock, flags); |
| 815 | + return count; |
| 816 | +} |
| 817 | + |
| 818 | +static ssize_t ata_scsi_lpm_show(struct device *dev, |
| 819 | + struct device_attribute *attr, char *buf) |
| 820 | +{ |
| 821 | + struct Scsi_Host *shost = class_to_shost(dev); |
| 822 | + struct ata_port *ap = ata_shost_to_port(shost); |
| 823 | + |
| 824 | + if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names)) |
| 825 | + return -EINVAL; |
| 826 | + |
| 827 | + return snprintf(buf, PAGE_SIZE, "%s\n", |
| 828 | + ata_lpm_policy_names[ap->target_lpm_policy]); |
| 829 | +} |
| 830 | +DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR, |
| 831 | + ata_scsi_lpm_show, ata_scsi_lpm_store); |
| 832 | +EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy); |
| 833 | + |
| 834 | +static ssize_t ata_ncq_prio_enable_show(struct device *device, |
| 835 | + struct device_attribute *attr, |
| 836 | + char *buf) |
| 837 | +{ |
| 838 | + struct scsi_device *sdev = to_scsi_device(device); |
| 839 | + struct ata_port *ap; |
| 840 | + struct ata_device *dev; |
| 841 | + bool ncq_prio_enable; |
| 842 | + int rc = 0; |
| 843 | + |
| 844 | + ap = ata_shost_to_port(sdev->host); |
| 845 | + |
| 846 | + spin_lock_irq(ap->lock); |
| 847 | + dev = ata_scsi_find_dev(ap, sdev); |
| 848 | + if (!dev) { |
| 849 | + rc = -ENODEV; |
| 850 | + goto unlock; |
| 851 | + } |
| 852 | + |
| 853 | + ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE; |
| 854 | + |
| 855 | +unlock: |
| 856 | + spin_unlock_irq(ap->lock); |
| 857 | + |
| 858 | + return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable); |
| 859 | +} |
| 860 | + |
| 861 | +static ssize_t ata_ncq_prio_enable_store(struct device *device, |
| 862 | + struct device_attribute *attr, |
| 863 | + const char *buf, size_t len) |
| 864 | +{ |
| 865 | + struct scsi_device *sdev = to_scsi_device(device); |
| 866 | + struct ata_port *ap; |
| 867 | + struct ata_device *dev; |
| 868 | + long int input; |
| 869 | + int rc; |
| 870 | + |
| 871 | + rc = kstrtol(buf, 10, &input); |
| 872 | + if (rc) |
| 873 | + return rc; |
| 874 | + if ((input < 0) || (input > 1)) |
| 875 | + return -EINVAL; |
| 876 | + |
| 877 | + ap = ata_shost_to_port(sdev->host); |
| 878 | + dev = ata_scsi_find_dev(ap, sdev); |
| 879 | + if (unlikely(!dev)) |
| 880 | + return -ENODEV; |
| 881 | + |
| 882 | + spin_lock_irq(ap->lock); |
| 883 | + if (input) |
| 884 | + dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE; |
| 885 | + else |
| 886 | + dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE; |
| 887 | + |
| 888 | + dev->link->eh_info.action |= ATA_EH_REVALIDATE; |
| 889 | + dev->link->eh_info.flags |= ATA_EHI_QUIET; |
| 890 | + ata_port_schedule_eh(ap); |
| 891 | + spin_unlock_irq(ap->lock); |
| 892 | + |
| 893 | + ata_port_wait_eh(ap); |
| 894 | + |
| 895 | + if (input) { |
| 896 | + spin_lock_irq(ap->lock); |
| 897 | + if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) { |
| 898 | + dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE; |
| 899 | + rc = -EIO; |
| 900 | + } |
| 901 | + spin_unlock_irq(ap->lock); |
| 902 | + } |
| 903 | + |
| 904 | + return rc ? rc : len; |
| 905 | +} |
| 906 | + |
| 907 | +DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR, |
| 908 | + ata_ncq_prio_enable_show, ata_ncq_prio_enable_store); |
| 909 | +EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable); |
| 910 | + |
| 911 | +struct device_attribute *ata_ncq_sdev_attrs[] = { |
| 912 | + &dev_attr_unload_heads, |
| 913 | + &dev_attr_ncq_prio_enable, |
| 914 | + NULL |
| 915 | +}; |
| 916 | +EXPORT_SYMBOL_GPL(ata_ncq_sdev_attrs); |
| 917 | + |
| 918 | +static ssize_t |
| 919 | +ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr, |
| 920 | + const char *buf, size_t count) |
| 921 | +{ |
| 922 | + struct Scsi_Host *shost = class_to_shost(dev); |
| 923 | + struct ata_port *ap = ata_shost_to_port(shost); |
| 924 | + if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM)) |
| 925 | + return ap->ops->em_store(ap, buf, count); |
| 926 | + return -EINVAL; |
| 927 | +} |
| 928 | + |
| 929 | +static ssize_t |
| 930 | +ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr, |
| 931 | + char *buf) |
| 932 | +{ |
| 933 | + struct Scsi_Host *shost = class_to_shost(dev); |
| 934 | + struct ata_port *ap = ata_shost_to_port(shost); |
| 935 | + |
| 936 | + if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM)) |
| 937 | + return ap->ops->em_show(ap, buf); |
| 938 | + return -EINVAL; |
| 939 | +} |
| 940 | +DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR, |
| 941 | + ata_scsi_em_message_show, ata_scsi_em_message_store); |
| 942 | +EXPORT_SYMBOL_GPL(dev_attr_em_message); |
| 943 | + |
| 944 | +static ssize_t |
| 945 | +ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr, |
| 946 | + char *buf) |
| 947 | +{ |
| 948 | + struct Scsi_Host *shost = class_to_shost(dev); |
| 949 | + struct ata_port *ap = ata_shost_to_port(shost); |
| 950 | + |
| 951 | + return snprintf(buf, 23, "%d\n", ap->em_message_type); |
| 952 | +} |
| 953 | +DEVICE_ATTR(em_message_type, S_IRUGO, |
| 954 | + ata_scsi_em_message_type_show, NULL); |
| 955 | +EXPORT_SYMBOL_GPL(dev_attr_em_message_type); |
| 956 | + |
| 957 | +static ssize_t |
| 958 | +ata_scsi_activity_show(struct device *dev, struct device_attribute *attr, |
| 959 | + char *buf) |
| 960 | +{ |
| 961 | + struct scsi_device *sdev = to_scsi_device(dev); |
| 962 | + struct ata_port *ap = ata_shost_to_port(sdev->host); |
| 963 | + struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); |
| 964 | + |
| 965 | + if (atadev && ap->ops->sw_activity_show && |
| 966 | + (ap->flags & ATA_FLAG_SW_ACTIVITY)) |
| 967 | + return ap->ops->sw_activity_show(atadev, buf); |
| 968 | + return -EINVAL; |
| 969 | +} |
| 970 | + |
| 971 | +static ssize_t |
| 972 | +ata_scsi_activity_store(struct device *dev, struct device_attribute *attr, |
| 973 | + const char *buf, size_t count) |
| 974 | +{ |
| 975 | + struct scsi_device *sdev = to_scsi_device(dev); |
| 976 | + struct ata_port *ap = ata_shost_to_port(sdev->host); |
| 977 | + struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); |
| 978 | + enum sw_activity val; |
| 979 | + int rc; |
| 980 | + |
| 981 | + if (atadev && ap->ops->sw_activity_store && |
| 982 | + (ap->flags & ATA_FLAG_SW_ACTIVITY)) { |
| 983 | + val = simple_strtoul(buf, NULL, 0); |
| 984 | + switch (val) { |
| 985 | + case OFF: case BLINK_ON: case BLINK_OFF: |
| 986 | + rc = ap->ops->sw_activity_store(atadev, val); |
| 987 | + if (!rc) |
| 988 | + return count; |
| 989 | + else |
| 990 | + return rc; |
| 991 | + } |
| 992 | + } |
| 993 | + return -EINVAL; |
| 994 | +} |
| 995 | +DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show, |
| 996 | + ata_scsi_activity_store); |
| 997 | +EXPORT_SYMBOL_GPL(dev_attr_sw_activity); |
| 998 | + |
| 999 | +/** |
| 1000 | + * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth |
| 1001 | + * @ap: ATA port to which the device change the queue depth |
| 1002 | + * @sdev: SCSI device to configure queue depth for |
| 1003 | + * @queue_depth: new queue depth |
| 1004 | + * |
| 1005 | + * libsas and libata have different approaches for associating a sdev to |
| 1006 | + * its ata_port. |
| 1007 | + * |
| 1008 | + */ |
| 1009 | +int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev, |
| 1010 | + int queue_depth) |
| 1011 | +{ |
| 1012 | + struct ata_device *dev; |
| 1013 | + unsigned long flags; |
| 1014 | + |
| 1015 | + if (queue_depth < 1 || queue_depth == sdev->queue_depth) |
| 1016 | + return sdev->queue_depth; |
| 1017 | + |
| 1018 | + dev = ata_scsi_find_dev(ap, sdev); |
| 1019 | + if (!dev || !ata_dev_enabled(dev)) |
| 1020 | + return sdev->queue_depth; |
| 1021 | + |
| 1022 | + /* NCQ enabled? */ |
| 1023 | + spin_lock_irqsave(ap->lock, flags); |
| 1024 | + dev->flags &= ~ATA_DFLAG_NCQ_OFF; |
| 1025 | + if (queue_depth == 1 || !ata_ncq_enabled(dev)) { |
| 1026 | + dev->flags |= ATA_DFLAG_NCQ_OFF; |
| 1027 | + queue_depth = 1; |
| 1028 | + } |
| 1029 | + spin_unlock_irqrestore(ap->lock, flags); |
| 1030 | + |
| 1031 | + /* limit and apply queue depth */ |
| 1032 | + queue_depth = min(queue_depth, sdev->host->can_queue); |
| 1033 | + queue_depth = min(queue_depth, ata_id_queue_depth(dev->id)); |
| 1034 | + queue_depth = min(queue_depth, ATA_MAX_QUEUE); |
| 1035 | + |
| 1036 | + if (sdev->queue_depth == queue_depth) |
| 1037 | + return -EINVAL; |
| 1038 | + |
| 1039 | + return scsi_change_queue_depth(sdev, queue_depth); |
| 1040 | +} |
| 1041 | +EXPORT_SYMBOL_GPL(__ata_change_queue_depth); |
| 1042 | + |
| 1043 | +/** |
| 1044 | + * ata_scsi_change_queue_depth - SCSI callback for queue depth config |
| 1045 | + * @sdev: SCSI device to configure queue depth for |
| 1046 | + * @queue_depth: new queue depth |
| 1047 | + * |
| 1048 | + * This is libata standard hostt->change_queue_depth callback. |
| 1049 | + * SCSI will call into this callback when user tries to set queue |
| 1050 | + * depth via sysfs. |
| 1051 | + * |
| 1052 | + * LOCKING: |
| 1053 | + * SCSI layer (we don't care) |
| 1054 | + * |
| 1055 | + * RETURNS: |
| 1056 | + * Newly configured queue depth. |
| 1057 | + */ |
| 1058 | +int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth) |
| 1059 | +{ |
| 1060 | + struct ata_port *ap = ata_shost_to_port(sdev->host); |
| 1061 | + |
| 1062 | + return __ata_change_queue_depth(ap, sdev, queue_depth); |
| 1063 | +} |
| 1064 | +EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth); |
0 commit comments