scsi_sysfs.c 44 KB

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