scsi_sysfs.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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 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 | S_IWUSR, 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 | S_IWUSR, 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. 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_usercontext(struct work_struct *work)
  378. {
  379. struct scsi_device *sdev;
  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. unsigned long flags;
  386. struct module *mod;
  387. sdev = container_of(work, struct scsi_device, ew.work);
  388. mod = sdev->host->hostt->module;
  389. might_sleep();
  390. scsi_dh_release_device(sdev);
  391. parent = sdev->sdev_gendev.parent;
  392. spin_lock_irqsave(sdev->host->host_lock, flags);
  393. list_del(&sdev->siblings);
  394. list_del(&sdev->same_target_siblings);
  395. list_del(&sdev->starved_entry);
  396. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  397. cancel_work_sync(&sdev->event_work);
  398. list_for_each_safe(this, tmp, &sdev->event_list) {
  399. struct scsi_event *evt;
  400. evt = list_entry(this, struct scsi_event, node);
  401. list_del(&evt->node);
  402. kfree(evt);
  403. }
  404. blk_put_queue(sdev->request_queue);
  405. /* NULL queue means the device can't be used */
  406. sdev->request_queue = NULL;
  407. sbitmap_free(&sdev->budget_map);
  408. mutex_lock(&sdev->inquiry_mutex);
  409. vpd_pg0 = rcu_replace_pointer(sdev->vpd_pg0, vpd_pg0,
  410. lockdep_is_held(&sdev->inquiry_mutex));
  411. vpd_pg80 = rcu_replace_pointer(sdev->vpd_pg80, vpd_pg80,
  412. lockdep_is_held(&sdev->inquiry_mutex));
  413. vpd_pg83 = rcu_replace_pointer(sdev->vpd_pg83, vpd_pg83,
  414. lockdep_is_held(&sdev->inquiry_mutex));
  415. vpd_pg89 = rcu_replace_pointer(sdev->vpd_pg89, vpd_pg89,
  416. lockdep_is_held(&sdev->inquiry_mutex));
  417. vpd_pgb0 = rcu_replace_pointer(sdev->vpd_pgb0, vpd_pgb0,
  418. lockdep_is_held(&sdev->inquiry_mutex));
  419. vpd_pgb1 = rcu_replace_pointer(sdev->vpd_pgb1, vpd_pgb1,
  420. lockdep_is_held(&sdev->inquiry_mutex));
  421. vpd_pgb2 = rcu_replace_pointer(sdev->vpd_pgb2, vpd_pgb2,
  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. kfree(sdev->inquiry);
  439. kfree(sdev);
  440. if (parent)
  441. put_device(parent);
  442. module_put(mod);
  443. }
  444. static void scsi_device_dev_release(struct device *dev)
  445. {
  446. struct scsi_device *sdp = to_scsi_device(dev);
  447. /* Set module pointer as NULL in case of module unloading */
  448. if (!try_module_get(sdp->host->hostt->module))
  449. sdp->host->hostt->module = NULL;
  450. execute_in_process_context(scsi_device_dev_release_usercontext,
  451. &sdp->ew);
  452. }
  453. static struct class sdev_class = {
  454. .name = "scsi_device",
  455. .dev_release = scsi_device_cls_release,
  456. };
  457. /* all probing is done in the individual ->probe routines */
  458. static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
  459. {
  460. struct scsi_device *sdp;
  461. if (dev->type != &scsi_dev_type)
  462. return 0;
  463. sdp = to_scsi_device(dev);
  464. if (sdp->no_uld_attach)
  465. return 0;
  466. return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
  467. }
  468. static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  469. {
  470. struct scsi_device *sdev;
  471. if (dev->type != &scsi_dev_type)
  472. return 0;
  473. sdev = to_scsi_device(dev);
  474. add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
  475. return 0;
  476. }
  477. struct bus_type scsi_bus_type = {
  478. .name = "scsi",
  479. .match = scsi_bus_match,
  480. .uevent = scsi_bus_uevent,
  481. #ifdef CONFIG_PM
  482. .pm = &scsi_bus_pm_ops,
  483. #endif
  484. };
  485. int scsi_sysfs_register(void)
  486. {
  487. int error;
  488. error = bus_register(&scsi_bus_type);
  489. if (!error) {
  490. error = class_register(&sdev_class);
  491. if (error)
  492. bus_unregister(&scsi_bus_type);
  493. }
  494. return error;
  495. }
  496. void scsi_sysfs_unregister(void)
  497. {
  498. class_unregister(&sdev_class);
  499. bus_unregister(&scsi_bus_type);
  500. }
  501. /*
  502. * sdev_show_function: macro to create an attr function that can be used to
  503. * show a non-bit field.
  504. */
  505. #define sdev_show_function(field, format_string) \
  506. static ssize_t \
  507. sdev_show_##field (struct device *dev, struct device_attribute *attr, \
  508. char *buf) \
  509. { \
  510. struct scsi_device *sdev; \
  511. sdev = to_scsi_device(dev); \
  512. return snprintf (buf, 20, format_string, sdev->field); \
  513. } \
  514. /*
  515. * sdev_rd_attr: macro to create a function and attribute variable for a
  516. * read only field.
  517. */
  518. #define sdev_rd_attr(field, format_string) \
  519. sdev_show_function(field, format_string) \
  520. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  521. /*
  522. * sdev_rw_attr: create a function and attribute variable for a
  523. * read/write field.
  524. */
  525. #define sdev_rw_attr(field, format_string) \
  526. sdev_show_function(field, format_string) \
  527. \
  528. static ssize_t \
  529. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  530. const char *buf, size_t count) \
  531. { \
  532. struct scsi_device *sdev; \
  533. sdev = to_scsi_device(dev); \
  534. sscanf (buf, format_string, &sdev->field); \
  535. return count; \
  536. } \
  537. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  538. /* Currently we don't export bit fields, but we might in future,
  539. * so leave this code in */
  540. #if 0
  541. /*
  542. * sdev_rd_attr: create a function and attribute variable for a
  543. * read/write bit field.
  544. */
  545. #define sdev_rw_attr_bit(field) \
  546. sdev_show_function(field, "%d\n") \
  547. \
  548. static ssize_t \
  549. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  550. const char *buf, size_t count) \
  551. { \
  552. int ret; \
  553. struct scsi_device *sdev; \
  554. ret = scsi_sdev_check_buf_bit(buf); \
  555. if (ret >= 0) { \
  556. sdev = to_scsi_device(dev); \
  557. sdev->field = ret; \
  558. ret = count; \
  559. } \
  560. return ret; \
  561. } \
  562. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  563. /*
  564. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  565. * else return -EINVAL.
  566. */
  567. static int scsi_sdev_check_buf_bit(const char *buf)
  568. {
  569. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  570. if (buf[0] == '1')
  571. return 1;
  572. else if (buf[0] == '0')
  573. return 0;
  574. else
  575. return -EINVAL;
  576. } else
  577. return -EINVAL;
  578. }
  579. #endif
  580. /*
  581. * Create the actual show/store functions and data structures.
  582. */
  583. sdev_rd_attr (type, "%d\n");
  584. sdev_rd_attr (scsi_level, "%d\n");
  585. sdev_rd_attr (vendor, "%.8s\n");
  586. sdev_rd_attr (model, "%.16s\n");
  587. sdev_rd_attr (rev, "%.4s\n");
  588. static ssize_t
  589. sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
  590. char *buf)
  591. {
  592. struct scsi_device *sdev = to_scsi_device(dev);
  593. return snprintf(buf, 20, "%d\n", scsi_device_busy(sdev));
  594. }
  595. static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
  596. static ssize_t
  597. sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
  598. char *buf)
  599. {
  600. struct scsi_device *sdev = to_scsi_device(dev);
  601. return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
  602. }
  603. static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
  604. /*
  605. * TODO: can we make these symlinks to the block layer ones?
  606. */
  607. static ssize_t
  608. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  609. {
  610. struct scsi_device *sdev;
  611. sdev = to_scsi_device(dev);
  612. return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
  613. }
  614. static ssize_t
  615. sdev_store_timeout (struct device *dev, struct device_attribute *attr,
  616. const char *buf, size_t count)
  617. {
  618. struct scsi_device *sdev;
  619. int timeout;
  620. sdev = to_scsi_device(dev);
  621. sscanf (buf, "%d\n", &timeout);
  622. blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
  623. return count;
  624. }
  625. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  626. static ssize_t
  627. sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  628. {
  629. struct scsi_device *sdev;
  630. sdev = to_scsi_device(dev);
  631. return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
  632. }
  633. static ssize_t
  634. sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
  635. const char *buf, size_t count)
  636. {
  637. struct scsi_device *sdev;
  638. unsigned int eh_timeout;
  639. int err;
  640. if (!capable(CAP_SYS_ADMIN))
  641. return -EACCES;
  642. sdev = to_scsi_device(dev);
  643. err = kstrtouint(buf, 10, &eh_timeout);
  644. if (err)
  645. return err;
  646. sdev->eh_timeout = eh_timeout * HZ;
  647. return count;
  648. }
  649. static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
  650. static ssize_t
  651. store_rescan_field (struct device *dev, struct device_attribute *attr,
  652. const char *buf, size_t count)
  653. {
  654. scsi_rescan_device(to_scsi_device(dev));
  655. return count;
  656. }
  657. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  658. static ssize_t
  659. sdev_store_delete(struct device *dev, struct device_attribute *attr,
  660. const char *buf, size_t count)
  661. {
  662. struct kernfs_node *kn;
  663. struct scsi_device *sdev = to_scsi_device(dev);
  664. /*
  665. * We need to try to get module, avoiding the module been removed
  666. * during delete.
  667. */
  668. if (scsi_device_get(sdev))
  669. return -ENODEV;
  670. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  671. WARN_ON_ONCE(!kn);
  672. /*
  673. * Concurrent writes into the "delete" sysfs attribute may trigger
  674. * concurrent calls to device_remove_file() and scsi_remove_device().
  675. * device_remove_file() handles concurrent removal calls by
  676. * serializing these and by ignoring the second and later removal
  677. * attempts. Concurrent calls of scsi_remove_device() are
  678. * serialized. The second and later calls of scsi_remove_device() are
  679. * ignored because the first call of that function changes the device
  680. * state into SDEV_DEL.
  681. */
  682. device_remove_file(dev, attr);
  683. scsi_remove_device(sdev);
  684. if (kn)
  685. sysfs_unbreak_active_protection(kn);
  686. scsi_device_put(sdev);
  687. return count;
  688. };
  689. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  690. static ssize_t
  691. store_state_field(struct device *dev, struct device_attribute *attr,
  692. const char *buf, size_t count)
  693. {
  694. int i, ret;
  695. struct scsi_device *sdev = to_scsi_device(dev);
  696. enum scsi_device_state state = 0;
  697. bool rescan_dev = false;
  698. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  699. const int len = strlen(sdev_states[i].name);
  700. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  701. buf[len] == '\n') {
  702. state = sdev_states[i].value;
  703. break;
  704. }
  705. }
  706. switch (state) {
  707. case SDEV_RUNNING:
  708. case SDEV_OFFLINE:
  709. break;
  710. default:
  711. return -EINVAL;
  712. }
  713. mutex_lock(&sdev->state_mutex);
  714. switch (sdev->sdev_state) {
  715. case SDEV_RUNNING:
  716. case SDEV_OFFLINE:
  717. break;
  718. default:
  719. mutex_unlock(&sdev->state_mutex);
  720. return -EINVAL;
  721. }
  722. if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING) {
  723. ret = 0;
  724. } else {
  725. ret = scsi_device_set_state(sdev, state);
  726. if (ret == 0 && state == SDEV_RUNNING)
  727. rescan_dev = true;
  728. }
  729. mutex_unlock(&sdev->state_mutex);
  730. if (rescan_dev) {
  731. /*
  732. * If the device state changes to SDEV_RUNNING, we need to
  733. * run the queue to avoid I/O hang, and rescan the device
  734. * to revalidate it. Running the queue first is necessary
  735. * because another thread may be waiting inside
  736. * blk_mq_freeze_queue_wait() and because that call may be
  737. * waiting for pending I/O to finish.
  738. */
  739. blk_mq_run_hw_queues(sdev->request_queue, true);
  740. scsi_rescan_device(sdev);
  741. }
  742. return ret == 0 ? count : -EINVAL;
  743. }
  744. static ssize_t
  745. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  746. {
  747. struct scsi_device *sdev = to_scsi_device(dev);
  748. const char *name = scsi_device_state_name(sdev->sdev_state);
  749. if (!name)
  750. return -EINVAL;
  751. return snprintf(buf, 20, "%s\n", name);
  752. }
  753. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  754. static ssize_t
  755. show_queue_type_field(struct device *dev, struct device_attribute *attr,
  756. char *buf)
  757. {
  758. struct scsi_device *sdev = to_scsi_device(dev);
  759. const char *name = "none";
  760. if (sdev->simple_tags)
  761. name = "simple";
  762. return snprintf(buf, 20, "%s\n", name);
  763. }
  764. static ssize_t
  765. store_queue_type_field(struct device *dev, struct device_attribute *attr,
  766. const char *buf, size_t count)
  767. {
  768. struct scsi_device *sdev = to_scsi_device(dev);
  769. if (!sdev->tagged_supported)
  770. return -EINVAL;
  771. sdev_printk(KERN_INFO, sdev,
  772. "ignoring write to deprecated queue_type attribute");
  773. return count;
  774. }
  775. static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  776. store_queue_type_field);
  777. #define sdev_vpd_pg_attr(_page) \
  778. static ssize_t \
  779. show_vpd_##_page(struct file *filp, struct kobject *kobj, \
  780. struct bin_attribute *bin_attr, \
  781. char *buf, loff_t off, size_t count) \
  782. { \
  783. struct device *dev = kobj_to_dev(kobj); \
  784. struct scsi_device *sdev = to_scsi_device(dev); \
  785. struct scsi_vpd *vpd_page; \
  786. int ret = -EINVAL; \
  787. \
  788. rcu_read_lock(); \
  789. vpd_page = rcu_dereference(sdev->vpd_##_page); \
  790. if (vpd_page) \
  791. ret = memory_read_from_buffer(buf, count, &off, \
  792. vpd_page->data, vpd_page->len); \
  793. rcu_read_unlock(); \
  794. return ret; \
  795. } \
  796. static struct bin_attribute dev_attr_vpd_##_page = { \
  797. .attr = {.name = __stringify(vpd_##_page), .mode = S_IRUGO }, \
  798. .size = 0, \
  799. .read = show_vpd_##_page, \
  800. };
  801. sdev_vpd_pg_attr(pg83);
  802. sdev_vpd_pg_attr(pg80);
  803. sdev_vpd_pg_attr(pg89);
  804. sdev_vpd_pg_attr(pgb0);
  805. sdev_vpd_pg_attr(pgb1);
  806. sdev_vpd_pg_attr(pgb2);
  807. sdev_vpd_pg_attr(pg0);
  808. static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
  809. struct bin_attribute *bin_attr,
  810. char *buf, loff_t off, size_t count)
  811. {
  812. struct device *dev = kobj_to_dev(kobj);
  813. struct scsi_device *sdev = to_scsi_device(dev);
  814. if (!sdev->inquiry)
  815. return -EINVAL;
  816. return memory_read_from_buffer(buf, count, &off, sdev->inquiry,
  817. sdev->inquiry_len);
  818. }
  819. static struct bin_attribute dev_attr_inquiry = {
  820. .attr = {
  821. .name = "inquiry",
  822. .mode = S_IRUGO,
  823. },
  824. .size = 0,
  825. .read = show_inquiry,
  826. };
  827. static ssize_t
  828. show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
  829. char *buf)
  830. {
  831. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  832. }
  833. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  834. #define show_sdev_iostat(field) \
  835. static ssize_t \
  836. show_iostat_##field(struct device *dev, struct device_attribute *attr, \
  837. char *buf) \
  838. { \
  839. struct scsi_device *sdev = to_scsi_device(dev); \
  840. unsigned long long count = atomic_read(&sdev->field); \
  841. return snprintf(buf, 20, "0x%llx\n", count); \
  842. } \
  843. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  844. show_sdev_iostat(iorequest_cnt);
  845. show_sdev_iostat(iodone_cnt);
  846. show_sdev_iostat(ioerr_cnt);
  847. show_sdev_iostat(iotmo_cnt);
  848. static ssize_t
  849. sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  850. {
  851. struct scsi_device *sdev;
  852. sdev = to_scsi_device(dev);
  853. return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
  854. }
  855. static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
  856. #define DECLARE_EVT_SHOW(name, Cap_name) \
  857. static ssize_t \
  858. sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
  859. char *buf) \
  860. { \
  861. struct scsi_device *sdev = to_scsi_device(dev); \
  862. int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
  863. return snprintf(buf, 20, "%d\n", val); \
  864. }
  865. #define DECLARE_EVT_STORE(name, Cap_name) \
  866. static ssize_t \
  867. sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
  868. const char *buf, size_t count) \
  869. { \
  870. struct scsi_device *sdev = to_scsi_device(dev); \
  871. int val = simple_strtoul(buf, NULL, 0); \
  872. if (val == 0) \
  873. clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  874. else if (val == 1) \
  875. set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  876. else \
  877. return -EINVAL; \
  878. return count; \
  879. }
  880. #define DECLARE_EVT(name, Cap_name) \
  881. DECLARE_EVT_SHOW(name, Cap_name) \
  882. DECLARE_EVT_STORE(name, Cap_name) \
  883. static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
  884. sdev_store_evt_##name);
  885. #define REF_EVT(name) &dev_attr_evt_##name.attr
  886. DECLARE_EVT(media_change, MEDIA_CHANGE)
  887. DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
  888. DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
  889. DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
  890. DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
  891. DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
  892. static ssize_t
  893. sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
  894. const char *buf, size_t count)
  895. {
  896. int depth, retval;
  897. struct scsi_device *sdev = to_scsi_device(dev);
  898. struct scsi_host_template *sht = sdev->host->hostt;
  899. if (!sht->change_queue_depth)
  900. return -EINVAL;
  901. depth = simple_strtoul(buf, NULL, 0);
  902. if (depth < 1 || depth > sdev->host->can_queue)
  903. return -EINVAL;
  904. retval = sht->change_queue_depth(sdev, depth);
  905. if (retval < 0)
  906. return retval;
  907. sdev->max_queue_depth = sdev->queue_depth;
  908. return count;
  909. }
  910. sdev_show_function(queue_depth, "%d\n");
  911. static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  912. sdev_store_queue_depth);
  913. static ssize_t
  914. sdev_show_wwid(struct device *dev, struct device_attribute *attr,
  915. char *buf)
  916. {
  917. struct scsi_device *sdev = to_scsi_device(dev);
  918. ssize_t count;
  919. count = scsi_vpd_lun_id(sdev, buf, PAGE_SIZE);
  920. if (count > 0) {
  921. buf[count] = '\n';
  922. count++;
  923. }
  924. return count;
  925. }
  926. static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
  927. #define BLIST_FLAG_NAME(name) \
  928. [const_ilog2((__force __u64)BLIST_##name)] = #name
  929. static const char *const sdev_bflags_name[] = {
  930. #include "scsi_devinfo_tbl.c"
  931. };
  932. #undef BLIST_FLAG_NAME
  933. static ssize_t
  934. sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
  935. char *buf)
  936. {
  937. struct scsi_device *sdev = to_scsi_device(dev);
  938. int i;
  939. ssize_t len = 0;
  940. for (i = 0; i < sizeof(sdev->sdev_bflags) * BITS_PER_BYTE; i++) {
  941. const char *name = NULL;
  942. if (!(sdev->sdev_bflags & (__force blist_flags_t)BIT(i)))
  943. continue;
  944. if (i < ARRAY_SIZE(sdev_bflags_name) && sdev_bflags_name[i])
  945. name = sdev_bflags_name[i];
  946. if (name)
  947. len += scnprintf(buf + len, PAGE_SIZE - len,
  948. "%s%s", len ? " " : "", name);
  949. else
  950. len += scnprintf(buf + len, PAGE_SIZE - len,
  951. "%sINVALID_BIT(%d)", len ? " " : "", i);
  952. }
  953. if (len)
  954. len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
  955. return len;
  956. }
  957. static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);
  958. #ifdef CONFIG_SCSI_DH
  959. static ssize_t
  960. sdev_show_dh_state(struct device *dev, struct device_attribute *attr,
  961. char *buf)
  962. {
  963. struct scsi_device *sdev = to_scsi_device(dev);
  964. if (!sdev->handler)
  965. return snprintf(buf, 20, "detached\n");
  966. return snprintf(buf, 20, "%s\n", sdev->handler->name);
  967. }
  968. static ssize_t
  969. sdev_store_dh_state(struct device *dev, struct device_attribute *attr,
  970. const char *buf, size_t count)
  971. {
  972. struct scsi_device *sdev = to_scsi_device(dev);
  973. int err = -EINVAL;
  974. if (sdev->sdev_state == SDEV_CANCEL ||
  975. sdev->sdev_state == SDEV_DEL)
  976. return -ENODEV;
  977. if (!sdev->handler) {
  978. /*
  979. * Attach to a device handler
  980. */
  981. err = scsi_dh_attach(sdev->request_queue, buf);
  982. } else if (!strncmp(buf, "activate", 8)) {
  983. /*
  984. * Activate a device handler
  985. */
  986. if (sdev->handler->activate)
  987. err = sdev->handler->activate(sdev, NULL, NULL);
  988. else
  989. err = 0;
  990. } else if (!strncmp(buf, "detach", 6)) {
  991. /*
  992. * Detach from a device handler
  993. */
  994. sdev_printk(KERN_WARNING, sdev,
  995. "can't detach handler %s.\n",
  996. sdev->handler->name);
  997. err = -EINVAL;
  998. }
  999. return err < 0 ? err : count;
  1000. }
  1001. static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state,
  1002. sdev_store_dh_state);
  1003. static ssize_t
  1004. sdev_show_access_state(struct device *dev,
  1005. struct device_attribute *attr,
  1006. char *buf)
  1007. {
  1008. struct scsi_device *sdev = to_scsi_device(dev);
  1009. unsigned char access_state;
  1010. const char *access_state_name;
  1011. if (!sdev->handler)
  1012. return -EINVAL;
  1013. access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
  1014. access_state_name = scsi_access_state_name(access_state);
  1015. return sprintf(buf, "%s\n",
  1016. access_state_name ? access_state_name : "unknown");
  1017. }
  1018. static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
  1019. static ssize_t
  1020. sdev_show_preferred_path(struct device *dev,
  1021. struct device_attribute *attr,
  1022. char *buf)
  1023. {
  1024. struct scsi_device *sdev = to_scsi_device(dev);
  1025. if (!sdev->handler)
  1026. return -EINVAL;
  1027. if (sdev->access_state & SCSI_ACCESS_STATE_PREFERRED)
  1028. return sprintf(buf, "1\n");
  1029. else
  1030. return sprintf(buf, "0\n");
  1031. }
  1032. static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
  1033. #endif
  1034. static ssize_t
  1035. sdev_show_queue_ramp_up_period(struct device *dev,
  1036. struct device_attribute *attr,
  1037. char *buf)
  1038. {
  1039. struct scsi_device *sdev;
  1040. sdev = to_scsi_device(dev);
  1041. return snprintf(buf, 20, "%u\n",
  1042. jiffies_to_msecs(sdev->queue_ramp_up_period));
  1043. }
  1044. static ssize_t
  1045. sdev_store_queue_ramp_up_period(struct device *dev,
  1046. struct device_attribute *attr,
  1047. const char *buf, size_t count)
  1048. {
  1049. struct scsi_device *sdev = to_scsi_device(dev);
  1050. unsigned int period;
  1051. if (kstrtouint(buf, 10, &period))
  1052. return -EINVAL;
  1053. sdev->queue_ramp_up_period = msecs_to_jiffies(period);
  1054. return count;
  1055. }
  1056. static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
  1057. sdev_show_queue_ramp_up_period,
  1058. sdev_store_queue_ramp_up_period);
  1059. static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
  1060. struct attribute *attr, int i)
  1061. {
  1062. struct device *dev = kobj_to_dev(kobj);
  1063. struct scsi_device *sdev = to_scsi_device(dev);
  1064. if (attr == &dev_attr_queue_depth.attr &&
  1065. !sdev->host->hostt->change_queue_depth)
  1066. return S_IRUGO;
  1067. if (attr == &dev_attr_queue_ramp_up_period.attr &&
  1068. !sdev->host->hostt->change_queue_depth)
  1069. return 0;
  1070. return attr->mode;
  1071. }
  1072. static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
  1073. struct bin_attribute *attr, int i)
  1074. {
  1075. struct device *dev = kobj_to_dev(kobj);
  1076. struct scsi_device *sdev = to_scsi_device(dev);
  1077. if (attr == &dev_attr_vpd_pg0 && !sdev->vpd_pg0)
  1078. return 0;
  1079. if (attr == &dev_attr_vpd_pg80 && !sdev->vpd_pg80)
  1080. return 0;
  1081. if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
  1082. return 0;
  1083. if (attr == &dev_attr_vpd_pg89 && !sdev->vpd_pg89)
  1084. return 0;
  1085. if (attr == &dev_attr_vpd_pgb0 && !sdev->vpd_pgb0)
  1086. return 0;
  1087. if (attr == &dev_attr_vpd_pgb1 && !sdev->vpd_pgb1)
  1088. return 0;
  1089. if (attr == &dev_attr_vpd_pgb2 && !sdev->vpd_pgb2)
  1090. return 0;
  1091. return S_IRUGO;
  1092. }
  1093. /* Default template for device attributes. May NOT be modified */
  1094. static struct attribute *scsi_sdev_attrs[] = {
  1095. &dev_attr_device_blocked.attr,
  1096. &dev_attr_type.attr,
  1097. &dev_attr_scsi_level.attr,
  1098. &dev_attr_device_busy.attr,
  1099. &dev_attr_vendor.attr,
  1100. &dev_attr_model.attr,
  1101. &dev_attr_rev.attr,
  1102. &dev_attr_rescan.attr,
  1103. &dev_attr_delete.attr,
  1104. &dev_attr_state.attr,
  1105. &dev_attr_timeout.attr,
  1106. &dev_attr_eh_timeout.attr,
  1107. &dev_attr_iocounterbits.attr,
  1108. &dev_attr_iorequest_cnt.attr,
  1109. &dev_attr_iodone_cnt.attr,
  1110. &dev_attr_ioerr_cnt.attr,
  1111. &dev_attr_iotmo_cnt.attr,
  1112. &dev_attr_modalias.attr,
  1113. &dev_attr_queue_depth.attr,
  1114. &dev_attr_queue_type.attr,
  1115. &dev_attr_wwid.attr,
  1116. &dev_attr_blacklist.attr,
  1117. #ifdef CONFIG_SCSI_DH
  1118. &dev_attr_dh_state.attr,
  1119. &dev_attr_access_state.attr,
  1120. &dev_attr_preferred_path.attr,
  1121. #endif
  1122. &dev_attr_queue_ramp_up_period.attr,
  1123. REF_EVT(media_change),
  1124. REF_EVT(inquiry_change_reported),
  1125. REF_EVT(capacity_change_reported),
  1126. REF_EVT(soft_threshold_reached),
  1127. REF_EVT(mode_parameter_change_reported),
  1128. REF_EVT(lun_change_reported),
  1129. NULL
  1130. };
  1131. static struct bin_attribute *scsi_sdev_bin_attrs[] = {
  1132. &dev_attr_vpd_pg0,
  1133. &dev_attr_vpd_pg83,
  1134. &dev_attr_vpd_pg80,
  1135. &dev_attr_vpd_pg89,
  1136. &dev_attr_vpd_pgb0,
  1137. &dev_attr_vpd_pgb1,
  1138. &dev_attr_vpd_pgb2,
  1139. &dev_attr_inquiry,
  1140. NULL
  1141. };
  1142. static struct attribute_group scsi_sdev_attr_group = {
  1143. .attrs = scsi_sdev_attrs,
  1144. .bin_attrs = scsi_sdev_bin_attrs,
  1145. .is_visible = scsi_sdev_attr_is_visible,
  1146. .is_bin_visible = scsi_sdev_bin_attr_is_visible,
  1147. };
  1148. static const struct attribute_group *scsi_sdev_attr_groups[] = {
  1149. &scsi_sdev_attr_group,
  1150. NULL
  1151. };
  1152. static int scsi_target_add(struct scsi_target *starget)
  1153. {
  1154. int error;
  1155. if (starget->state != STARGET_CREATED)
  1156. return 0;
  1157. error = device_add(&starget->dev);
  1158. if (error) {
  1159. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  1160. return error;
  1161. }
  1162. transport_add_device(&starget->dev);
  1163. starget->state = STARGET_RUNNING;
  1164. pm_runtime_set_active(&starget->dev);
  1165. pm_runtime_enable(&starget->dev);
  1166. device_enable_async_suspend(&starget->dev);
  1167. return 0;
  1168. }
  1169. /**
  1170. * scsi_sysfs_add_sdev - add scsi device to sysfs
  1171. * @sdev: scsi_device to add
  1172. *
  1173. * Return value:
  1174. * 0 on Success / non-zero on Failure
  1175. **/
  1176. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  1177. {
  1178. int error;
  1179. struct scsi_target *starget = sdev->sdev_target;
  1180. error = scsi_target_add(starget);
  1181. if (error)
  1182. return error;
  1183. transport_configure_device(&starget->dev);
  1184. device_enable_async_suspend(&sdev->sdev_gendev);
  1185. scsi_autopm_get_target(starget);
  1186. pm_runtime_set_active(&sdev->sdev_gendev);
  1187. if (!sdev->rpm_autosuspend)
  1188. pm_runtime_forbid(&sdev->sdev_gendev);
  1189. pm_runtime_enable(&sdev->sdev_gendev);
  1190. scsi_autopm_put_target(starget);
  1191. scsi_autopm_get_device(sdev);
  1192. scsi_dh_add_device(sdev);
  1193. error = device_add(&sdev->sdev_gendev);
  1194. if (error) {
  1195. sdev_printk(KERN_INFO, sdev,
  1196. "failed to add device: %d\n", error);
  1197. return error;
  1198. }
  1199. device_enable_async_suspend(&sdev->sdev_dev);
  1200. error = device_add(&sdev->sdev_dev);
  1201. if (error) {
  1202. sdev_printk(KERN_INFO, sdev,
  1203. "failed to add class device: %d\n", error);
  1204. device_del(&sdev->sdev_gendev);
  1205. return error;
  1206. }
  1207. transport_add_device(&sdev->sdev_gendev);
  1208. sdev->is_visible = 1;
  1209. if (IS_ENABLED(CONFIG_BLK_DEV_BSG)) {
  1210. sdev->bsg_dev = scsi_bsg_register_queue(sdev);
  1211. if (IS_ERR(sdev->bsg_dev)) {
  1212. error = PTR_ERR(sdev->bsg_dev);
  1213. sdev_printk(KERN_INFO, sdev,
  1214. "Failed to register bsg queue, errno=%d\n",
  1215. error);
  1216. sdev->bsg_dev = NULL;
  1217. }
  1218. }
  1219. scsi_autopm_put_device(sdev);
  1220. return error;
  1221. }
  1222. void __scsi_remove_device(struct scsi_device *sdev)
  1223. {
  1224. struct device *dev = &sdev->sdev_gendev;
  1225. int res;
  1226. /*
  1227. * This cleanup path is not reentrant and while it is impossible
  1228. * to get a new reference with scsi_device_get() someone can still
  1229. * hold a previously acquired one.
  1230. */
  1231. if (sdev->sdev_state == SDEV_DEL)
  1232. return;
  1233. if (sdev->is_visible) {
  1234. /*
  1235. * If scsi_internal_target_block() is running concurrently,
  1236. * wait until it has finished before changing the device state.
  1237. */
  1238. mutex_lock(&sdev->state_mutex);
  1239. /*
  1240. * If blocked, we go straight to DEL and restart the queue so
  1241. * any commands issued during driver shutdown (like sync
  1242. * cache) are errored immediately.
  1243. */
  1244. res = scsi_device_set_state(sdev, SDEV_CANCEL);
  1245. if (res != 0) {
  1246. res = scsi_device_set_state(sdev, SDEV_DEL);
  1247. if (res == 0)
  1248. scsi_start_queue(sdev);
  1249. }
  1250. mutex_unlock(&sdev->state_mutex);
  1251. if (res != 0)
  1252. return;
  1253. if (IS_ENABLED(CONFIG_BLK_DEV_BSG) && sdev->bsg_dev)
  1254. bsg_unregister_queue(sdev->bsg_dev);
  1255. device_unregister(&sdev->sdev_dev);
  1256. transport_remove_device(dev);
  1257. device_del(dev);
  1258. } else
  1259. put_device(&sdev->sdev_dev);
  1260. /*
  1261. * Stop accepting new requests and wait until all queuecommand() and
  1262. * scsi_run_queue() invocations have finished before tearing down the
  1263. * device.
  1264. */
  1265. mutex_lock(&sdev->state_mutex);
  1266. scsi_device_set_state(sdev, SDEV_DEL);
  1267. mutex_unlock(&sdev->state_mutex);
  1268. blk_mq_destroy_queue(sdev->request_queue);
  1269. blk_put_queue(sdev->request_queue);
  1270. kref_put(&sdev->host->tagset_refcnt, scsi_mq_free_tags);
  1271. cancel_work_sync(&sdev->requeue_work);
  1272. if (sdev->host->hostt->slave_destroy)
  1273. sdev->host->hostt->slave_destroy(sdev);
  1274. transport_destroy_device(dev);
  1275. /*
  1276. * Paired with the kref_get() in scsi_sysfs_initialize(). We have
  1277. * removed sysfs visibility from the device, so make the target
  1278. * invisible if this was the last device underneath it.
  1279. */
  1280. scsi_target_reap(scsi_target(sdev));
  1281. put_device(dev);
  1282. }
  1283. /**
  1284. * scsi_remove_device - unregister a device from the scsi bus
  1285. * @sdev: scsi_device to unregister
  1286. **/
  1287. void scsi_remove_device(struct scsi_device *sdev)
  1288. {
  1289. struct Scsi_Host *shost = sdev->host;
  1290. mutex_lock(&shost->scan_mutex);
  1291. __scsi_remove_device(sdev);
  1292. mutex_unlock(&shost->scan_mutex);
  1293. }
  1294. EXPORT_SYMBOL(scsi_remove_device);
  1295. static void __scsi_remove_target(struct scsi_target *starget)
  1296. {
  1297. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1298. unsigned long flags;
  1299. struct scsi_device *sdev;
  1300. spin_lock_irqsave(shost->host_lock, flags);
  1301. restart:
  1302. list_for_each_entry(sdev, &shost->__devices, siblings) {
  1303. /*
  1304. * We cannot call scsi_device_get() here, as
  1305. * we might've been called from rmmod() causing
  1306. * scsi_device_get() to fail the module_is_live()
  1307. * check.
  1308. */
  1309. if (sdev->channel != starget->channel ||
  1310. sdev->id != starget->id)
  1311. continue;
  1312. if (sdev->sdev_state == SDEV_DEL ||
  1313. sdev->sdev_state == SDEV_CANCEL ||
  1314. !get_device(&sdev->sdev_gendev))
  1315. continue;
  1316. spin_unlock_irqrestore(shost->host_lock, flags);
  1317. scsi_remove_device(sdev);
  1318. put_device(&sdev->sdev_gendev);
  1319. spin_lock_irqsave(shost->host_lock, flags);
  1320. goto restart;
  1321. }
  1322. spin_unlock_irqrestore(shost->host_lock, flags);
  1323. }
  1324. /**
  1325. * scsi_remove_target - try to remove a target and all its devices
  1326. * @dev: generic starget or parent of generic stargets to be removed
  1327. *
  1328. * Note: This is slightly racy. It is possible that if the user
  1329. * requests the addition of another device then the target won't be
  1330. * removed.
  1331. */
  1332. void scsi_remove_target(struct device *dev)
  1333. {
  1334. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  1335. struct scsi_target *starget;
  1336. unsigned long flags;
  1337. restart:
  1338. spin_lock_irqsave(shost->host_lock, flags);
  1339. list_for_each_entry(starget, &shost->__targets, siblings) {
  1340. if (starget->state == STARGET_DEL ||
  1341. starget->state == STARGET_REMOVE ||
  1342. starget->state == STARGET_CREATED_REMOVE)
  1343. continue;
  1344. if (starget->dev.parent == dev || &starget->dev == dev) {
  1345. kref_get(&starget->reap_ref);
  1346. if (starget->state == STARGET_CREATED)
  1347. starget->state = STARGET_CREATED_REMOVE;
  1348. else
  1349. starget->state = STARGET_REMOVE;
  1350. spin_unlock_irqrestore(shost->host_lock, flags);
  1351. __scsi_remove_target(starget);
  1352. scsi_target_reap(starget);
  1353. goto restart;
  1354. }
  1355. }
  1356. spin_unlock_irqrestore(shost->host_lock, flags);
  1357. }
  1358. EXPORT_SYMBOL(scsi_remove_target);
  1359. int scsi_register_driver(struct device_driver *drv)
  1360. {
  1361. drv->bus = &scsi_bus_type;
  1362. return driver_register(drv);
  1363. }
  1364. EXPORT_SYMBOL(scsi_register_driver);
  1365. int scsi_register_interface(struct class_interface *intf)
  1366. {
  1367. intf->class = &sdev_class;
  1368. return class_interface_register(intf);
  1369. }
  1370. EXPORT_SYMBOL(scsi_register_interface);
  1371. /**
  1372. * scsi_sysfs_add_host - add scsi host to subsystem
  1373. * @shost: scsi host struct to add to subsystem
  1374. **/
  1375. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  1376. {
  1377. transport_register_device(&shost->shost_gendev);
  1378. transport_configure_device(&shost->shost_gendev);
  1379. return 0;
  1380. }
  1381. static struct device_type scsi_dev_type = {
  1382. .name = "scsi_device",
  1383. .release = scsi_device_dev_release,
  1384. .groups = scsi_sdev_attr_groups,
  1385. };
  1386. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  1387. {
  1388. unsigned long flags;
  1389. struct Scsi_Host *shost = sdev->host;
  1390. struct scsi_host_template *hostt = shost->hostt;
  1391. struct scsi_target *starget = sdev->sdev_target;
  1392. device_initialize(&sdev->sdev_gendev);
  1393. sdev->sdev_gendev.bus = &scsi_bus_type;
  1394. sdev->sdev_gendev.type = &scsi_dev_type;
  1395. scsi_enable_async_suspend(&sdev->sdev_gendev);
  1396. dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
  1397. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  1398. sdev->sdev_gendev.groups = hostt->sdev_groups;
  1399. device_initialize(&sdev->sdev_dev);
  1400. sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
  1401. sdev->sdev_dev.class = &sdev_class;
  1402. dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
  1403. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  1404. /*
  1405. * Get a default scsi_level from the target (derived from sibling
  1406. * devices). This is the best we can do for guessing how to set
  1407. * sdev->lun_in_cdb for the initial INQUIRY command. For LUN 0 the
  1408. * setting doesn't matter, because all the bits are zero anyway.
  1409. * But it does matter for higher LUNs.
  1410. */
  1411. sdev->scsi_level = starget->scsi_level;
  1412. if (sdev->scsi_level <= SCSI_2 &&
  1413. sdev->scsi_level != SCSI_UNKNOWN &&
  1414. !shost->no_scsi2_lun_in_cdb)
  1415. sdev->lun_in_cdb = 1;
  1416. transport_setup_device(&sdev->sdev_gendev);
  1417. spin_lock_irqsave(shost->host_lock, flags);
  1418. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  1419. list_add_tail(&sdev->siblings, &shost->__devices);
  1420. spin_unlock_irqrestore(shost->host_lock, flags);
  1421. /*
  1422. * device can now only be removed via __scsi_remove_device() so hold
  1423. * the target. Target will be held in CREATED state until something
  1424. * beneath it becomes visible (in which case it moves to RUNNING)
  1425. */
  1426. kref_get(&starget->reap_ref);
  1427. }
  1428. int scsi_is_sdev_device(const struct device *dev)
  1429. {
  1430. return dev->type == &scsi_dev_type;
  1431. }
  1432. EXPORT_SYMBOL(scsi_is_sdev_device);
  1433. /* A blank transport template that is used in drivers that don't
  1434. * yet implement Transport Attributes */
  1435. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };