hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/scsi/scsi_sysfs.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * scsi_sysfs.c
34 *
....@@ -367,7 +368,6 @@
367368
368369 static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
369370
370
-shost_rd_attr(use_blk_mq, "%d\n");
371371 shost_rd_attr(unique_id, "%u\n");
372372 shost_rd_attr(cmd_per_lun, "%hd\n");
373373 shost_rd_attr(can_queue, "%hd\n");
....@@ -385,6 +385,23 @@
385385 return snprintf(buf, 20, "%d\n", scsi_host_busy(shost));
386386 }
387387 static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
388
+
389
+static ssize_t
390
+show_use_blk_mq(struct device *dev, struct device_attribute *attr, char *buf)
391
+{
392
+ return sprintf(buf, "1\n");
393
+}
394
+static DEVICE_ATTR(use_blk_mq, S_IRUGO, show_use_blk_mq, NULL);
395
+
396
+static ssize_t
397
+show_nr_hw_queues(struct device *dev, struct device_attribute *attr, char *buf)
398
+{
399
+ struct Scsi_Host *shost = class_to_shost(dev);
400
+ struct blk_mq_tag_set *tag_set = &shost->tag_set;
401
+
402
+ return snprintf(buf, 20, "%d\n", tag_set->nr_hw_queues);
403
+}
404
+static DEVICE_ATTR(nr_hw_queues, S_IRUGO, show_nr_hw_queues, NULL);
388405
389406 static struct attribute *scsi_sysfs_shost_attrs[] = {
390407 &dev_attr_use_blk_mq.attr,
....@@ -404,6 +421,7 @@
404421 &dev_attr_prot_guard_type.attr,
405422 &dev_attr_host_reset.attr,
406423 &dev_attr_eh_deadline.attr,
424
+ &dev_attr_nr_hw_queues.attr,
407425 NULL
408426 };
409427
....@@ -430,6 +448,7 @@
430448 struct device *parent;
431449 struct list_head *this, *tmp;
432450 struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
451
+ struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
433452 unsigned long flags;
434453 struct module *mod;
435454
....@@ -462,16 +481,24 @@
462481 sdev->request_queue = NULL;
463482
464483 mutex_lock(&sdev->inquiry_mutex);
465
- rcu_swap_protected(sdev->vpd_pg80, vpd_pg80,
466
- lockdep_is_held(&sdev->inquiry_mutex));
467
- rcu_swap_protected(sdev->vpd_pg83, vpd_pg83,
468
- lockdep_is_held(&sdev->inquiry_mutex));
484
+ vpd_pg0 = rcu_replace_pointer(sdev->vpd_pg0, vpd_pg0,
485
+ lockdep_is_held(&sdev->inquiry_mutex));
486
+ vpd_pg80 = rcu_replace_pointer(sdev->vpd_pg80, vpd_pg80,
487
+ lockdep_is_held(&sdev->inquiry_mutex));
488
+ vpd_pg83 = rcu_replace_pointer(sdev->vpd_pg83, vpd_pg83,
489
+ lockdep_is_held(&sdev->inquiry_mutex));
490
+ vpd_pg89 = rcu_replace_pointer(sdev->vpd_pg89, vpd_pg89,
491
+ lockdep_is_held(&sdev->inquiry_mutex));
469492 mutex_unlock(&sdev->inquiry_mutex);
470493
494
+ if (vpd_pg0)
495
+ kfree_rcu(vpd_pg0, rcu);
471496 if (vpd_pg83)
472497 kfree_rcu(vpd_pg83, rcu);
473498 if (vpd_pg80)
474499 kfree_rcu(vpd_pg80, rcu);
500
+ if (vpd_pg89)
501
+ kfree_rcu(vpd_pg89, rcu);
475502 kfree(sdev->inquiry);
476503 kfree(sdev);
477504
....@@ -769,6 +796,7 @@
769796 int i, ret;
770797 struct scsi_device *sdev = to_scsi_device(dev);
771798 enum scsi_device_state state = 0;
799
+ bool rescan_dev = false;
772800
773801 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
774802 const int len = strlen(sdev_states[i].name);
....@@ -778,12 +806,44 @@
778806 break;
779807 }
780808 }
781
- if (!state)
809
+ switch (state) {
810
+ case SDEV_RUNNING:
811
+ case SDEV_OFFLINE:
812
+ break;
813
+ default:
782814 return -EINVAL;
815
+ }
783816
784817 mutex_lock(&sdev->state_mutex);
785
- ret = scsi_device_set_state(sdev, state);
818
+ switch (sdev->sdev_state) {
819
+ case SDEV_RUNNING:
820
+ case SDEV_OFFLINE:
821
+ break;
822
+ default:
823
+ mutex_unlock(&sdev->state_mutex);
824
+ return -EINVAL;
825
+ }
826
+ if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING) {
827
+ ret = 0;
828
+ } else {
829
+ ret = scsi_device_set_state(sdev, state);
830
+ if (ret == 0 && state == SDEV_RUNNING)
831
+ rescan_dev = true;
832
+ }
786833 mutex_unlock(&sdev->state_mutex);
834
+
835
+ if (rescan_dev) {
836
+ /*
837
+ * If the device state changes to SDEV_RUNNING, we need to
838
+ * run the queue to avoid I/O hang, and rescan the device
839
+ * to revalidate it. Running the queue first is necessary
840
+ * because another thread may be waiting inside
841
+ * blk_mq_freeze_queue_wait() and because that call may be
842
+ * waiting for pending I/O to finish.
843
+ */
844
+ blk_mq_run_hw_queues(sdev->request_queue, true);
845
+ scsi_rescan_device(dev);
846
+ }
787847
788848 return ret == 0 ? count : -EINVAL;
789849 }
....@@ -838,7 +898,7 @@
838898 struct bin_attribute *bin_attr, \
839899 char *buf, loff_t off, size_t count) \
840900 { \
841
- struct device *dev = container_of(kobj, struct device, kobj); \
901
+ struct device *dev = kobj_to_dev(kobj); \
842902 struct scsi_device *sdev = to_scsi_device(dev); \
843903 struct scsi_vpd *vpd_page; \
844904 int ret = -EINVAL; \
....@@ -859,12 +919,14 @@
859919
860920 sdev_vpd_pg_attr(pg83);
861921 sdev_vpd_pg_attr(pg80);
922
+sdev_vpd_pg_attr(pg89);
923
+sdev_vpd_pg_attr(pg0);
862924
863925 static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
864926 struct bin_attribute *bin_attr,
865927 char *buf, loff_t off, size_t count)
866928 {
867
- struct device *dev = container_of(kobj, struct device, kobj);
929
+ struct device *dev = kobj_to_dev(kobj);
868930 struct scsi_device *sdev = to_scsi_device(dev);
869931
870932 if (!sdev->inquiry)
....@@ -1025,14 +1087,14 @@
10251087 name = sdev_bflags_name[i];
10261088
10271089 if (name)
1028
- len += snprintf(buf + len, PAGE_SIZE - len,
1029
- "%s%s", len ? " " : "", name);
1090
+ len += scnprintf(buf + len, PAGE_SIZE - len,
1091
+ "%s%s", len ? " " : "", name);
10301092 else
1031
- len += snprintf(buf + len, PAGE_SIZE - len,
1032
- "%sINVALID_BIT(%d)", len ? " " : "", i);
1093
+ len += scnprintf(buf + len, PAGE_SIZE - len,
1094
+ "%sINVALID_BIT(%d)", len ? " " : "", i);
10331095 }
10341096 if (len)
1035
- len += snprintf(buf + len, PAGE_SIZE - len, "\n");
1097
+ len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
10361098 return len;
10371099 }
10381100 static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);
....@@ -1161,7 +1223,7 @@
11611223 static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
11621224 struct attribute *attr, int i)
11631225 {
1164
- struct device *dev = container_of(kobj, struct device, kobj);
1226
+ struct device *dev = kobj_to_dev(kobj);
11651227 struct scsi_device *sdev = to_scsi_device(dev);
11661228
11671229
....@@ -1187,14 +1249,20 @@
11871249 static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
11881250 struct bin_attribute *attr, int i)
11891251 {
1190
- struct device *dev = container_of(kobj, struct device, kobj);
1252
+ struct device *dev = kobj_to_dev(kobj);
11911253 struct scsi_device *sdev = to_scsi_device(dev);
11921254
1255
+
1256
+ if (attr == &dev_attr_vpd_pg0 && !sdev->vpd_pg0)
1257
+ return 0;
11931258
11941259 if (attr == &dev_attr_vpd_pg80 && !sdev->vpd_pg80)
11951260 return 0;
11961261
11971262 if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
1263
+ return 0;
1264
+
1265
+ if (attr == &dev_attr_vpd_pg89 && !sdev->vpd_pg89)
11981266 return 0;
11991267
12001268 return S_IRUGO;
....@@ -1239,8 +1307,10 @@
12391307 };
12401308
12411309 static struct bin_attribute *scsi_sdev_bin_attrs[] = {
1310
+ &dev_attr_vpd_pg0,
12421311 &dev_attr_vpd_pg83,
12431312 &dev_attr_vpd_pg80,
1313
+ &dev_attr_vpd_pg89,
12441314 &dev_attr_inquiry,
12451315 NULL
12461316 };