^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * scsi_sysfs.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * SCSI sysfs interface routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Created to pull SCSI mid layer sysfs routines into one file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <scsi/scsi_tcq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <scsi/scsi_dh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <scsi/scsi_transport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <scsi/scsi_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <scsi/scsi_devinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "scsi_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "scsi_logging.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct device_type scsi_dev_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) enum scsi_device_state value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) } sdev_states[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) { SDEV_CREATED, "created" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) { SDEV_RUNNING, "running" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) { SDEV_CANCEL, "cancel" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) { SDEV_DEL, "deleted" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) { SDEV_QUIESCE, "quiesce" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) { SDEV_OFFLINE, "offline" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) { SDEV_TRANSPORT_OFFLINE, "transport-offline" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) { SDEV_BLOCK, "blocked" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) { SDEV_CREATED_BLOCK, "created-blocked" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) const char *scsi_device_state_name(enum scsi_device_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (sdev_states[i].value == state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) name = sdev_states[i].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) enum scsi_host_state value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) } shost_states[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) { SHOST_CREATED, "created" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) { SHOST_RUNNING, "running" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) { SHOST_CANCEL, "cancel" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) { SHOST_DEL, "deleted" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) { SHOST_RECOVERY, "recovery" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) { SHOST_DEL_RECOVERY, "deleted/recovery", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) const char *scsi_host_state_name(enum scsi_host_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (shost_states[i].value == state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) name = shost_states[i].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #ifdef CONFIG_SCSI_DH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned char value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) } sdev_access_states[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) { SCSI_ACCESS_STATE_OPTIMAL, "active/optimized" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) { SCSI_ACCESS_STATE_ACTIVE, "active/non-optimized" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) { SCSI_ACCESS_STATE_STANDBY, "standby" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) { SCSI_ACCESS_STATE_UNAVAILABLE, "unavailable" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) { SCSI_ACCESS_STATE_LBA, "lba-dependent" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) { SCSI_ACCESS_STATE_OFFLINE, "offline" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) { SCSI_ACCESS_STATE_TRANSITIONING, "transitioning" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static const char *scsi_access_state_name(unsigned char state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) for (i = 0; i < ARRAY_SIZE(sdev_access_states); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (sdev_access_states[i].value == state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) name = sdev_access_states[i].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int check_set(unsigned long long *val, char *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) char *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (strcmp(src, "-") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) *val = SCAN_WILD_CARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Doesn't check for int overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *val = simple_strtoull(src, &last, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (*last != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int scsi_scan(struct Scsi_Host *shost, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) char s1[15], s2[15], s3[17], junk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned long long channel, id, lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (res != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (check_set(&channel, s1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (check_set(&id, s2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (check_set(&lun, s3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (shost->transportt->user_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) res = shost->transportt->user_scan(shost, channel, id, lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) res = scsi_scan_host_selected(shost, channel, id, lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) SCSI_SCAN_MANUAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * shost_show_function: macro to create an attr function that can be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * show a non-bit field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #define shost_show_function(name, field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) show_##name (struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct Scsi_Host *shost = class_to_shost(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return snprintf (buf, 20, format_string, shost->field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * shost_rd_attr: macro to create a function and attribute variable for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * read only field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #define shost_rd_attr2(name, field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) shost_show_function(name, field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #define shost_rd_attr(field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) shost_rd_attr2(field, field, format_string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Create the actual show/store functions and data structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) store_scan(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) res = scsi_scan(shost, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (res == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) res = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) store_shost_state(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) enum scsi_host_state state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) const int len = strlen(shost_states[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (strncmp(shost_states[i].name, buf, len) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) buf[len] == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) state = shost_states[i].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (!state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (scsi_host_set_state(shost, state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) const char *name = scsi_host_state_name(shost->shost_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return snprintf(buf, 20, "%s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static struct device_attribute dev_attr_hstate =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) show_shost_mode(unsigned int mode, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ssize_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (mode & MODE_INITIATOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) len = sprintf(buf, "%s", "Initiator");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (mode & MODE_TARGET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) len += sprintf(buf + len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) unsigned int supported_mode = shost->hostt->supported_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (supported_mode == MODE_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /* by default this should be initiator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) supported_mode = MODE_INITIATOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return show_shost_mode(supported_mode, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) show_shost_active_mode(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (shost->active_mode == MODE_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return snprintf(buf, 20, "unknown\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return show_shost_mode(shost->active_mode, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int check_reset_type(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (sysfs_streq(str, "adapter"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return SCSI_ADAPTER_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) else if (sysfs_streq(str, "firmware"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return SCSI_FIRMWARE_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) store_host_reset(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct scsi_host_template *sht = shost->hostt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) type = check_reset_type(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) goto exit_store_host_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (sht->host_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ret = sht->host_reset(shost, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) exit_store_host_reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) show_shost_eh_deadline(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (shost->eh_deadline == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return snprintf(buf, strlen("off") + 2, "off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return sprintf(buf, "%u\n", shost->eh_deadline / HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) store_shost_eh_deadline(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) unsigned long deadline, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (shost->transportt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) (shost->transportt->eh_strategy_handler ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) !shost->hostt->eh_host_reset_handler))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (!strncmp(buf, "off", strlen("off")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) deadline = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ret = kstrtoul(buf, 10, &deadline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (deadline * HZ > UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (scsi_host_in_recovery(shost))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (deadline == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) shost->eh_deadline = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) shost->eh_deadline = deadline * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) shost_rd_attr(unique_id, "%u\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) shost_rd_attr(cmd_per_lun, "%hd\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) shost_rd_attr(can_queue, "%hd\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) shost_rd_attr(sg_tablesize, "%hu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) shost_rd_attr(sg_prot_tablesize, "%hu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) shost_rd_attr(unchecked_isa_dma, "%d\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) shost_rd_attr(prot_capabilities, "%u\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) shost_rd_attr(prot_guard_type, "%hd\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return snprintf(buf, 20, "%d\n", scsi_host_busy(shost));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) show_use_blk_mq(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return sprintf(buf, "1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static DEVICE_ATTR(use_blk_mq, S_IRUGO, show_use_blk_mq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) show_nr_hw_queues(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct Scsi_Host *shost = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct blk_mq_tag_set *tag_set = &shost->tag_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return snprintf(buf, 20, "%d\n", tag_set->nr_hw_queues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static DEVICE_ATTR(nr_hw_queues, S_IRUGO, show_nr_hw_queues, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static struct attribute *scsi_sysfs_shost_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) &dev_attr_use_blk_mq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) &dev_attr_unique_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) &dev_attr_host_busy.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) &dev_attr_cmd_per_lun.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) &dev_attr_can_queue.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) &dev_attr_sg_tablesize.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) &dev_attr_sg_prot_tablesize.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) &dev_attr_unchecked_isa_dma.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) &dev_attr_proc_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) &dev_attr_scan.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) &dev_attr_hstate.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) &dev_attr_supported_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) &dev_attr_active_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) &dev_attr_prot_capabilities.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) &dev_attr_prot_guard_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) &dev_attr_host_reset.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) &dev_attr_eh_deadline.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) &dev_attr_nr_hw_queues.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static struct attribute_group scsi_shost_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) .attrs = scsi_sysfs_shost_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) &scsi_shost_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static void scsi_device_cls_release(struct device *class_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) sdev = class_to_sdev(class_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) put_device(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static void scsi_device_dev_release_usercontext(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct device *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct list_head *this, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) sdev = container_of(work, struct scsi_device, ew.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) mod = sdev->host->hostt->module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) scsi_dh_release_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) parent = sdev->sdev_gendev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) spin_lock_irqsave(sdev->host->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) list_del(&sdev->siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) list_del(&sdev->same_target_siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) list_del(&sdev->starved_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) spin_unlock_irqrestore(sdev->host->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) cancel_work_sync(&sdev->event_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) list_for_each_safe(this, tmp, &sdev->event_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct scsi_event *evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) evt = list_entry(this, struct scsi_event, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) list_del(&evt->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) kfree(evt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) blk_put_queue(sdev->request_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* NULL queue means the device can't be used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) sdev->request_queue = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) mutex_lock(&sdev->inquiry_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) vpd_pg0 = rcu_replace_pointer(sdev->vpd_pg0, vpd_pg0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) lockdep_is_held(&sdev->inquiry_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) vpd_pg80 = rcu_replace_pointer(sdev->vpd_pg80, vpd_pg80,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) lockdep_is_held(&sdev->inquiry_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) vpd_pg83 = rcu_replace_pointer(sdev->vpd_pg83, vpd_pg83,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) lockdep_is_held(&sdev->inquiry_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) vpd_pg89 = rcu_replace_pointer(sdev->vpd_pg89, vpd_pg89,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) lockdep_is_held(&sdev->inquiry_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) mutex_unlock(&sdev->inquiry_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (vpd_pg0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) kfree_rcu(vpd_pg0, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (vpd_pg83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) kfree_rcu(vpd_pg83, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (vpd_pg80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) kfree_rcu(vpd_pg80, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (vpd_pg89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) kfree_rcu(vpd_pg89, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) kfree(sdev->inquiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) kfree(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) put_device(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) module_put(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static void scsi_device_dev_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) struct scsi_device *sdp = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /* Set module pointer as NULL in case of module unloading */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (!try_module_get(sdp->host->hostt->module))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) sdp->host->hostt->module = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) execute_in_process_context(scsi_device_dev_release_usercontext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) &sdp->ew);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static struct class sdev_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) .name = "scsi_device",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) .dev_release = scsi_device_cls_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /* all probing is done in the individual ->probe routines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) struct scsi_device *sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (dev->type != &scsi_dev_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) sdp = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (sdp->no_uld_attach)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (dev->type != &scsi_dev_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct bus_type scsi_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) .name = "scsi",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) .match = scsi_bus_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) .uevent = scsi_bus_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) .pm = &scsi_bus_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) EXPORT_SYMBOL_GPL(scsi_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int scsi_sysfs_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) error = bus_register(&scsi_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) error = class_register(&sdev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) bus_unregister(&scsi_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) void scsi_sysfs_unregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) class_unregister(&sdev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) bus_unregister(&scsi_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * sdev_show_function: macro to create an attr function that can be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * show a non-bit field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) #define sdev_show_function(field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) sdev_show_##field (struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct scsi_device *sdev; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) sdev = to_scsi_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return snprintf (buf, 20, format_string, sdev->field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * sdev_rd_attr: macro to create a function and attribute variable for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * read only field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) #define sdev_rd_attr(field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) sdev_show_function(field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * sdev_rw_attr: create a function and attribute variable for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * read/write field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) #define sdev_rw_attr(field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) sdev_show_function(field, format_string) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) sdev_store_##field (struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) const char *buf, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) struct scsi_device *sdev; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) sdev = to_scsi_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) sscanf (buf, format_string, &sdev->field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return count; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /* Currently we don't export bit fields, but we might in future,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * so leave this code in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * sdev_rd_attr: create a function and attribute variable for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * read/write bit field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) #define sdev_rw_attr_bit(field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) sdev_show_function(field, "%d\n") \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) sdev_store_##field (struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) const char *buf, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) int ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) struct scsi_device *sdev; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) ret = scsi_sdev_check_buf_bit(buf); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (ret >= 0) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) sdev = to_scsi_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) sdev->field = ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) ret = count; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * else return -EINVAL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) static int scsi_sdev_check_buf_bit(const char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (buf[0] == '1')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) else if (buf[0] == '0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * Create the actual show/store functions and data structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) sdev_rd_attr (type, "%d\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) sdev_rd_attr (scsi_level, "%d\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) sdev_rd_attr (vendor, "%.8s\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) sdev_rd_attr (model, "%.16s\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) sdev_rd_attr (rev, "%.4s\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_busy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * TODO: can we make these symlinks to the block layer ones?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) sdev_store_timeout (struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) int timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) sscanf (buf, "%d\n", &timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) unsigned int eh_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) err = kstrtouint(buf, 10, &eh_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) sdev->eh_timeout = eh_timeout * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) store_rescan_field (struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) scsi_rescan_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) sdev_store_delete(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * We need to try to get module, avoiding the module been removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * during delete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (scsi_device_get(sdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) WARN_ON_ONCE(!kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) * Concurrent writes into the "delete" sysfs attribute may trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * concurrent calls to device_remove_file() and scsi_remove_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * device_remove_file() handles concurrent removal calls by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * serializing these and by ignoring the second and later removal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) * attempts. Concurrent calls of scsi_remove_device() are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) * serialized. The second and later calls of scsi_remove_device() are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * ignored because the first call of that function changes the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * state into SDEV_DEL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) device_remove_file(dev, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) sysfs_unbreak_active_protection(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) scsi_device_put(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) store_state_field(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) enum scsi_device_state state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) bool rescan_dev = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) const int len = strlen(sdev_states[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (strncmp(sdev_states[i].name, buf, len) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) buf[len] == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) state = sdev_states[i].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) case SDEV_RUNNING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) case SDEV_OFFLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) mutex_lock(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) ret = scsi_device_set_state(sdev, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (ret == 0 && state == SDEV_RUNNING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) rescan_dev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) mutex_unlock(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (rescan_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * If the device state changes to SDEV_RUNNING, we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) * run the queue to avoid I/O hang, and rescan the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) * to revalidate it. Running the queue first is necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * because another thread may be waiting inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * blk_mq_freeze_queue_wait() and because that call may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * waiting for pending I/O to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) blk_mq_run_hw_queues(sdev->request_queue, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) scsi_rescan_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) return ret == 0 ? count : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) const char *name = scsi_device_state_name(sdev->sdev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return snprintf(buf, 20, "%s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) show_queue_type_field(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) const char *name = "none";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (sdev->simple_tags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) name = "simple";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) return snprintf(buf, 20, "%s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) store_queue_type_field(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (!sdev->tagged_supported)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) "ignoring write to deprecated queue_type attribute");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) store_queue_type_field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) #define sdev_vpd_pg_attr(_page) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) show_vpd_##_page(struct file *filp, struct kobject *kobj, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) struct bin_attribute *bin_attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) char *buf, loff_t off, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) struct device *dev = kobj_to_dev(kobj); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) struct scsi_device *sdev = to_scsi_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) struct scsi_vpd *vpd_page; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) int ret = -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) rcu_read_lock(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) vpd_page = rcu_dereference(sdev->vpd_##_page); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (vpd_page) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) ret = memory_read_from_buffer(buf, count, &off, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) vpd_page->data, vpd_page->len); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) rcu_read_unlock(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) static struct bin_attribute dev_attr_vpd_##_page = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) .attr = {.name = __stringify(vpd_##_page), .mode = S_IRUGO }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) .size = 0, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) .read = show_vpd_##_page, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) sdev_vpd_pg_attr(pg83);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) sdev_vpd_pg_attr(pg80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) sdev_vpd_pg_attr(pg89);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) sdev_vpd_pg_attr(pg0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) char *buf, loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (!sdev->inquiry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) return memory_read_from_buffer(buf, count, &off, sdev->inquiry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) sdev->inquiry_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) static struct bin_attribute dev_attr_inquiry = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) .attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) .name = "inquiry",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) .mode = S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) .size = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) .read = show_inquiry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) #define show_sdev_iostat(field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) show_iostat_##field(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) struct scsi_device *sdev = to_scsi_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) unsigned long long count = atomic_read(&sdev->field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) return snprintf(buf, 20, "0x%llx\n", count); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) show_sdev_iostat(iorequest_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) show_sdev_iostat(iodone_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) show_sdev_iostat(ioerr_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) #define DECLARE_EVT_SHOW(name, Cap_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) struct scsi_device *sdev = to_scsi_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) return snprintf(buf, 20, "%d\n", val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) #define DECLARE_EVT_STORE(name, Cap_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) const char *buf, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) struct scsi_device *sdev = to_scsi_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) int val = simple_strtoul(buf, NULL, 0); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) if (val == 0) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) else if (val == 1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) else \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) return count; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) #define DECLARE_EVT(name, Cap_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) DECLARE_EVT_SHOW(name, Cap_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) DECLARE_EVT_STORE(name, Cap_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) sdev_store_evt_##name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) #define REF_EVT(name) &dev_attr_evt_##name.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) DECLARE_EVT(media_change, MEDIA_CHANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) int depth, retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) struct scsi_host_template *sht = sdev->host->hostt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (!sht->change_queue_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) depth = simple_strtoul(buf, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (depth < 1 || depth > sdev->host->can_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) retval = sht->change_queue_depth(sdev, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) sdev->max_queue_depth = sdev->queue_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) sdev_show_function(queue_depth, "%d\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) sdev_store_queue_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) sdev_show_wwid(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) count = scsi_vpd_lun_id(sdev, buf, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) if (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) buf[count] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) #define BLIST_FLAG_NAME(name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) [const_ilog2((__force __u64)BLIST_##name)] = #name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) static const char *const sdev_bflags_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) #include "scsi_devinfo_tbl.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) #undef BLIST_FLAG_NAME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) ssize_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) for (i = 0; i < sizeof(sdev->sdev_bflags) * BITS_PER_BYTE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) const char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) if (!(sdev->sdev_bflags & (__force blist_flags_t)BIT(i)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (i < ARRAY_SIZE(sdev_bflags_name) && sdev_bflags_name[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) name = sdev_bflags_name[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) len += scnprintf(buf + len, PAGE_SIZE - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) "%s%s", len ? " " : "", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) len += scnprintf(buf + len, PAGE_SIZE - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) "%sINVALID_BIT(%d)", len ? " " : "", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) if (len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) #ifdef CONFIG_SCSI_DH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) sdev_show_dh_state(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) if (!sdev->handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) return snprintf(buf, 20, "detached\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) return snprintf(buf, 20, "%s\n", sdev->handler->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) sdev_store_dh_state(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) if (sdev->sdev_state == SDEV_CANCEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) sdev->sdev_state == SDEV_DEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) if (!sdev->handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) * Attach to a device handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) err = scsi_dh_attach(sdev->request_queue, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) } else if (!strncmp(buf, "activate", 8)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * Activate a device handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) if (sdev->handler->activate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) err = sdev->handler->activate(sdev, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) } else if (!strncmp(buf, "detach", 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) * Detach from a device handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) sdev_printk(KERN_WARNING, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) "can't detach handler %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) sdev->handler->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) return err < 0 ? err : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) sdev_store_dh_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) sdev_show_access_state(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) unsigned char access_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) const char *access_state_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) if (!sdev->handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) access_state_name = scsi_access_state_name(access_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) return sprintf(buf, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) access_state_name ? access_state_name : "unknown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) sdev_show_preferred_path(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) if (!sdev->handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) if (sdev->access_state & SCSI_ACCESS_STATE_PREFERRED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) return sprintf(buf, "1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) return sprintf(buf, "0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) sdev_show_queue_ramp_up_period(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) return snprintf(buf, 20, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) jiffies_to_msecs(sdev->queue_ramp_up_period));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) sdev_store_queue_ramp_up_period(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) unsigned int period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (kstrtouint(buf, 10, &period))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) sdev->queue_ramp_up_period = msecs_to_jiffies(period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) sdev_show_queue_ramp_up_period,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) sdev_store_queue_ramp_up_period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) struct attribute *attr, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) if (attr == &dev_attr_queue_depth.attr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) !sdev->host->hostt->change_queue_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) return S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (attr == &dev_attr_queue_ramp_up_period.attr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) !sdev->host->hostt->change_queue_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) #ifdef CONFIG_SCSI_DH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) if (attr == &dev_attr_access_state.attr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) !sdev->handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) if (attr == &dev_attr_preferred_path.attr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) !sdev->handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) struct bin_attribute *attr, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) if (attr == &dev_attr_vpd_pg0 && !sdev->vpd_pg0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) if (attr == &dev_attr_vpd_pg80 && !sdev->vpd_pg80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) if (attr == &dev_attr_vpd_pg89 && !sdev->vpd_pg89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) return S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) /* Default template for device attributes. May NOT be modified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) static struct attribute *scsi_sdev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) &dev_attr_device_blocked.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) &dev_attr_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) &dev_attr_scsi_level.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) &dev_attr_device_busy.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) &dev_attr_vendor.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) &dev_attr_model.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) &dev_attr_rev.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) &dev_attr_rescan.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) &dev_attr_delete.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) &dev_attr_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) &dev_attr_timeout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) &dev_attr_eh_timeout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) &dev_attr_iocounterbits.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) &dev_attr_iorequest_cnt.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) &dev_attr_iodone_cnt.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) &dev_attr_ioerr_cnt.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) &dev_attr_modalias.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) &dev_attr_queue_depth.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) &dev_attr_queue_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) &dev_attr_wwid.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) &dev_attr_blacklist.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) #ifdef CONFIG_SCSI_DH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) &dev_attr_dh_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) &dev_attr_access_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) &dev_attr_preferred_path.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) &dev_attr_queue_ramp_up_period.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) REF_EVT(media_change),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) REF_EVT(inquiry_change_reported),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) REF_EVT(capacity_change_reported),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) REF_EVT(soft_threshold_reached),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) REF_EVT(mode_parameter_change_reported),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) REF_EVT(lun_change_reported),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) static struct bin_attribute *scsi_sdev_bin_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) &dev_attr_vpd_pg0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) &dev_attr_vpd_pg83,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) &dev_attr_vpd_pg80,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) &dev_attr_vpd_pg89,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) &dev_attr_inquiry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) static struct attribute_group scsi_sdev_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) .attrs = scsi_sdev_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) .bin_attrs = scsi_sdev_bin_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) .is_visible = scsi_sdev_attr_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) .is_bin_visible = scsi_sdev_bin_attr_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) static const struct attribute_group *scsi_sdev_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) &scsi_sdev_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) static int scsi_target_add(struct scsi_target *starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) if (starget->state != STARGET_CREATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) error = device_add(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) dev_err(&starget->dev, "target device_add failed, error %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) transport_add_device(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) starget->state = STARGET_RUNNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) pm_runtime_set_active(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) pm_runtime_enable(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) device_enable_async_suspend(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) * scsi_sysfs_add_sdev - add scsi device to sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) * @sdev: scsi_device to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) * 0 on Success / non-zero on Failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) int scsi_sysfs_add_sdev(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) int error, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) struct request_queue *rq = sdev->request_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) struct scsi_target *starget = sdev->sdev_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) error = scsi_target_add(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) transport_configure_device(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) device_enable_async_suspend(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) scsi_autopm_get_target(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) pm_runtime_set_active(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) if (!sdev->rpm_autosuspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) pm_runtime_forbid(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) pm_runtime_enable(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) scsi_autopm_put_target(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) scsi_autopm_get_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) scsi_dh_add_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) error = device_add(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) "failed to add device: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) device_enable_async_suspend(&sdev->sdev_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) error = device_add(&sdev->sdev_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) "failed to add class device: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) device_del(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) transport_add_device(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) sdev->is_visible = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) error = bsg_scsi_register_queue(rq, &sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) /* we're treating error on bsg register as non-fatal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) * so pretend nothing went wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) "Failed to register bsg queue, errno=%d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) /* add additional host specific attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) if (sdev->host->hostt->sdev_attrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) error = device_create_file(&sdev->sdev_gendev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) sdev->host->hostt->sdev_attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) if (sdev->host->hostt->sdev_groups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) error = sysfs_create_groups(&sdev->sdev_gendev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) sdev->host->hostt->sdev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) scsi_autopm_put_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) void __scsi_remove_device(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) struct device *dev = &sdev->sdev_gendev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) * This cleanup path is not reentrant and while it is impossible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) * to get a new reference with scsi_device_get() someone can still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) * hold a previously acquired one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) if (sdev->sdev_state == SDEV_DEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) if (sdev->is_visible) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) * If scsi_internal_target_block() is running concurrently,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) * wait until it has finished before changing the device state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) mutex_lock(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) * If blocked, we go straight to DEL and restart the queue so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) * any commands issued during driver shutdown (like sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) * cache) are errored immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) res = scsi_device_set_state(sdev, SDEV_CANCEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) if (res != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) res = scsi_device_set_state(sdev, SDEV_DEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) if (res == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) scsi_start_queue(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) mutex_unlock(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) if (res != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) if (sdev->host->hostt->sdev_groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) sysfs_remove_groups(&sdev->sdev_gendev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) sdev->host->hostt->sdev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) bsg_unregister_queue(sdev->request_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) device_unregister(&sdev->sdev_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) transport_remove_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) device_del(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) put_device(&sdev->sdev_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) * Stop accepting new requests and wait until all queuecommand() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) * scsi_run_queue() invocations have finished before tearing down the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) * device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) mutex_lock(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) scsi_device_set_state(sdev, SDEV_DEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) mutex_unlock(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) blk_cleanup_queue(sdev->request_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) cancel_work_sync(&sdev->requeue_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) if (sdev->host->hostt->slave_destroy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) sdev->host->hostt->slave_destroy(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) transport_destroy_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) * Paired with the kref_get() in scsi_sysfs_initialize(). We have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) * remoed sysfs visibility from the device, so make the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) * invisible if this was the last device underneath it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) scsi_target_reap(scsi_target(sdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) * scsi_remove_device - unregister a device from the scsi bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) * @sdev: scsi_device to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) void scsi_remove_device(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) struct Scsi_Host *shost = sdev->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) mutex_lock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) __scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) EXPORT_SYMBOL(scsi_remove_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) static void __scsi_remove_target(struct scsi_target *starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) list_for_each_entry(sdev, &shost->__devices, siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) * We cannot call scsi_device_get() here, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) * we might've been called from rmmod() causing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) * scsi_device_get() to fail the module_is_live()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) * check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) if (sdev->channel != starget->channel ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) sdev->id != starget->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) if (sdev->sdev_state == SDEV_DEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) sdev->sdev_state == SDEV_CANCEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) !get_device(&sdev->sdev_gendev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) put_device(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) * scsi_remove_target - try to remove a target and all its devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) * @dev: generic starget or parent of generic stargets to be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) * Note: This is slightly racy. It is possible that if the user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) * requests the addition of another device then the target won't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) * removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) void scsi_remove_target(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) struct Scsi_Host *shost = dev_to_shost(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) struct scsi_target *starget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) list_for_each_entry(starget, &shost->__targets, siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) if (starget->state == STARGET_DEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) starget->state == STARGET_REMOVE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) starget->state == STARGET_CREATED_REMOVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) if (starget->dev.parent == dev || &starget->dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) kref_get(&starget->reap_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) if (starget->state == STARGET_CREATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) starget->state = STARGET_CREATED_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) starget->state = STARGET_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) __scsi_remove_target(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) scsi_target_reap(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) EXPORT_SYMBOL(scsi_remove_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) int scsi_register_driver(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) drv->bus = &scsi_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) return driver_register(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) EXPORT_SYMBOL(scsi_register_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) int scsi_register_interface(struct class_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) intf->class = &sdev_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) return class_interface_register(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) EXPORT_SYMBOL(scsi_register_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) * scsi_sysfs_add_host - add scsi host to subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) * @shost: scsi host struct to add to subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) int scsi_sysfs_add_host(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) int error, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) /* add host specific attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) if (shost->hostt->shost_attrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) for (i = 0; shost->hostt->shost_attrs[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) error = device_create_file(&shost->shost_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) shost->hostt->shost_attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) transport_register_device(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) transport_configure_device(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) static struct device_type scsi_dev_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) .name = "scsi_device",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) .release = scsi_device_dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) .groups = scsi_sdev_attr_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) void scsi_sysfs_device_initialize(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) struct Scsi_Host *shost = sdev->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) struct scsi_target *starget = sdev->sdev_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) device_initialize(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) sdev->sdev_gendev.bus = &scsi_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) sdev->sdev_gendev.type = &scsi_dev_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) device_initialize(&sdev->sdev_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) sdev->sdev_dev.class = &sdev_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) * Get a default scsi_level from the target (derived from sibling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) * devices). This is the best we can do for guessing how to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) * sdev->lun_in_cdb for the initial INQUIRY command. For LUN 0 the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) * setting doesn't matter, because all the bits are zero anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) * But it does matter for higher LUNs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) sdev->scsi_level = starget->scsi_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) if (sdev->scsi_level <= SCSI_2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) sdev->scsi_level != SCSI_UNKNOWN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) !shost->no_scsi2_lun_in_cdb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) sdev->lun_in_cdb = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) transport_setup_device(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) list_add_tail(&sdev->same_target_siblings, &starget->devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) list_add_tail(&sdev->siblings, &shost->__devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) * device can now only be removed via __scsi_remove_device() so hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) * the target. Target will be held in CREATED state until something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) * beneath it becomes visible (in which case it moves to RUNNING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) kref_get(&starget->reap_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) int scsi_is_sdev_device(const struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) return dev->type == &scsi_dev_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) EXPORT_SYMBOL(scsi_is_sdev_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) /* A blank transport template that is used in drivers that don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) * yet implement Transport Attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };