scsi_sysfs.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * scsi_sysfs.c
  3. *
  4. * SCSI sysfs interface routines.
  5. *
  6. * Created to pull SCSI mid layer sysfs routines into one file.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <scsi/scsi.h>
  15. #include <scsi/scsi_device.h>
  16. #include <scsi/scsi_host.h>
  17. #include <scsi/scsi_tcq.h>
  18. #include <scsi/scsi_transport.h>
  19. #include <scsi/scsi_driver.h>
  20. #include "scsi_priv.h"
  21. #include "scsi_logging.h"
  22. static struct device_type scsi_dev_type;
  23. static const struct {
  24. enum scsi_device_state value;
  25. char *name;
  26. } sdev_states[] = {
  27. { SDEV_CREATED, "created" },
  28. { SDEV_RUNNING, "running" },
  29. { SDEV_CANCEL, "cancel" },
  30. { SDEV_DEL, "deleted" },
  31. { SDEV_QUIESCE, "quiesce" },
  32. { SDEV_OFFLINE, "offline" },
  33. { SDEV_TRANSPORT_OFFLINE, "transport-offline" },
  34. { SDEV_BLOCK, "blocked" },
  35. { SDEV_CREATED_BLOCK, "created-blocked" },
  36. };
  37. const char *scsi_device_state_name(enum scsi_device_state state)
  38. {
  39. int i;
  40. char *name = NULL;
  41. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  42. if (sdev_states[i].value == state) {
  43. name = sdev_states[i].name;
  44. break;
  45. }
  46. }
  47. return name;
  48. }
  49. static const struct {
  50. enum scsi_host_state value;
  51. char *name;
  52. } shost_states[] = {
  53. { SHOST_CREATED, "created" },
  54. { SHOST_RUNNING, "running" },
  55. { SHOST_CANCEL, "cancel" },
  56. { SHOST_DEL, "deleted" },
  57. { SHOST_RECOVERY, "recovery" },
  58. { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
  59. { SHOST_DEL_RECOVERY, "deleted/recovery", },
  60. };
  61. const char *scsi_host_state_name(enum scsi_host_state state)
  62. {
  63. int i;
  64. char *name = NULL;
  65. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  66. if (shost_states[i].value == state) {
  67. name = shost_states[i].name;
  68. break;
  69. }
  70. }
  71. return name;
  72. }
  73. static int check_set(unsigned long long *val, char *src)
  74. {
  75. char *last;
  76. if (strncmp(src, "-", 20) == 0) {
  77. *val = SCAN_WILD_CARD;
  78. } else {
  79. /*
  80. * Doesn't check for int overflow
  81. */
  82. *val = simple_strtoull(src, &last, 0);
  83. if (*last != '\0')
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. static int scsi_scan(struct Scsi_Host *shost, const char *str)
  89. {
  90. char s1[15], s2[15], s3[17], junk;
  91. unsigned long long channel, id, lun;
  92. int res;
  93. res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk);
  94. if (res != 3)
  95. return -EINVAL;
  96. if (check_set(&channel, s1))
  97. return -EINVAL;
  98. if (check_set(&id, s2))
  99. return -EINVAL;
  100. if (check_set(&lun, s3))
  101. return -EINVAL;
  102. if (shost->transportt->user_scan)
  103. res = shost->transportt->user_scan(shost, channel, id, lun);
  104. else
  105. res = scsi_scan_host_selected(shost, channel, id, lun, 1);
  106. return res;
  107. }
  108. /*
  109. * shost_show_function: macro to create an attr function that can be used to
  110. * show a non-bit field.
  111. */
  112. #define shost_show_function(name, field, format_string) \
  113. static ssize_t \
  114. show_##name (struct device *dev, struct device_attribute *attr, \
  115. char *buf) \
  116. { \
  117. struct Scsi_Host *shost = class_to_shost(dev); \
  118. return snprintf (buf, 20, format_string, shost->field); \
  119. }
  120. /*
  121. * shost_rd_attr: macro to create a function and attribute variable for a
  122. * read only field.
  123. */
  124. #define shost_rd_attr2(name, field, format_string) \
  125. shost_show_function(name, field, format_string) \
  126. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  127. #define shost_rd_attr(field, format_string) \
  128. shost_rd_attr2(field, field, format_string)
  129. /*
  130. * Create the actual show/store functions and data structures.
  131. */
  132. static ssize_t
  133. store_scan(struct device *dev, struct device_attribute *attr,
  134. const char *buf, size_t count)
  135. {
  136. struct Scsi_Host *shost = class_to_shost(dev);
  137. int res;
  138. res = scsi_scan(shost, buf);
  139. if (res == 0)
  140. res = count;
  141. return res;
  142. };
  143. static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
  144. static ssize_t
  145. store_shost_state(struct device *dev, struct device_attribute *attr,
  146. const char *buf, size_t count)
  147. {
  148. int i;
  149. struct Scsi_Host *shost = class_to_shost(dev);
  150. enum scsi_host_state state = 0;
  151. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  152. const int len = strlen(shost_states[i].name);
  153. if (strncmp(shost_states[i].name, buf, len) == 0 &&
  154. buf[len] == '\n') {
  155. state = shost_states[i].value;
  156. break;
  157. }
  158. }
  159. if (!state)
  160. return -EINVAL;
  161. if (scsi_host_set_state(shost, state))
  162. return -EINVAL;
  163. return count;
  164. }
  165. static ssize_t
  166. show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
  167. {
  168. struct Scsi_Host *shost = class_to_shost(dev);
  169. const char *name = scsi_host_state_name(shost->shost_state);
  170. if (!name)
  171. return -EINVAL;
  172. return snprintf(buf, 20, "%s\n", name);
  173. }
  174. /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
  175. struct device_attribute dev_attr_hstate =
  176. __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
  177. static ssize_t
  178. show_shost_mode(unsigned int mode, char *buf)
  179. {
  180. ssize_t len = 0;
  181. if (mode & MODE_INITIATOR)
  182. len = sprintf(buf, "%s", "Initiator");
  183. if (mode & MODE_TARGET)
  184. len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
  185. len += sprintf(buf + len, "\n");
  186. return len;
  187. }
  188. static ssize_t
  189. show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
  190. char *buf)
  191. {
  192. struct Scsi_Host *shost = class_to_shost(dev);
  193. unsigned int supported_mode = shost->hostt->supported_mode;
  194. if (supported_mode == MODE_UNKNOWN)
  195. /* by default this should be initiator */
  196. supported_mode = MODE_INITIATOR;
  197. return show_shost_mode(supported_mode, buf);
  198. }
  199. static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
  200. static ssize_t
  201. show_shost_active_mode(struct device *dev,
  202. struct device_attribute *attr, char *buf)
  203. {
  204. struct Scsi_Host *shost = class_to_shost(dev);
  205. if (shost->active_mode == MODE_UNKNOWN)
  206. return snprintf(buf, 20, "unknown\n");
  207. else
  208. return show_shost_mode(shost->active_mode, buf);
  209. }
  210. static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
  211. static int check_reset_type(const char *str)
  212. {
  213. if (sysfs_streq(str, "adapter"))
  214. return SCSI_ADAPTER_RESET;
  215. else if (sysfs_streq(str, "firmware"))
  216. return SCSI_FIRMWARE_RESET;
  217. else
  218. return 0;
  219. }
  220. static ssize_t
  221. store_host_reset(struct device *dev, struct device_attribute *attr,
  222. const char *buf, size_t count)
  223. {
  224. struct Scsi_Host *shost = class_to_shost(dev);
  225. struct scsi_host_template *sht = shost->hostt;
  226. int ret = -EINVAL;
  227. int type;
  228. type = check_reset_type(buf);
  229. if (!type)
  230. goto exit_store_host_reset;
  231. if (sht->host_reset)
  232. ret = sht->host_reset(shost, type);
  233. exit_store_host_reset:
  234. if (ret == 0)
  235. ret = count;
  236. return ret;
  237. }
  238. static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
  239. static ssize_t
  240. show_shost_eh_deadline(struct device *dev,
  241. struct device_attribute *attr, char *buf)
  242. {
  243. struct Scsi_Host *shost = class_to_shost(dev);
  244. if (shost->eh_deadline == -1)
  245. return snprintf(buf, strlen("off") + 2, "off\n");
  246. return sprintf(buf, "%u\n", shost->eh_deadline / HZ);
  247. }
  248. static ssize_t
  249. store_shost_eh_deadline(struct device *dev, struct device_attribute *attr,
  250. const char *buf, size_t count)
  251. {
  252. struct Scsi_Host *shost = class_to_shost(dev);
  253. int ret = -EINVAL;
  254. unsigned long deadline, flags;
  255. if (shost->transportt &&
  256. (shost->transportt->eh_strategy_handler ||
  257. !shost->hostt->eh_host_reset_handler))
  258. return ret;
  259. if (!strncmp(buf, "off", strlen("off")))
  260. deadline = -1;
  261. else {
  262. ret = kstrtoul(buf, 10, &deadline);
  263. if (ret)
  264. return ret;
  265. if (deadline * HZ > UINT_MAX)
  266. return -EINVAL;
  267. }
  268. spin_lock_irqsave(shost->host_lock, flags);
  269. if (scsi_host_in_recovery(shost))
  270. ret = -EBUSY;
  271. else {
  272. if (deadline == -1)
  273. shost->eh_deadline = -1;
  274. else
  275. shost->eh_deadline = deadline * HZ;
  276. ret = count;
  277. }
  278. spin_unlock_irqrestore(shost->host_lock, flags);
  279. return ret;
  280. }
  281. static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
  282. shost_rd_attr(use_blk_mq, "%d\n");
  283. shost_rd_attr(unique_id, "%u\n");
  284. shost_rd_attr(cmd_per_lun, "%hd\n");
  285. shost_rd_attr(can_queue, "%hd\n");
  286. shost_rd_attr(sg_tablesize, "%hu\n");
  287. shost_rd_attr(sg_prot_tablesize, "%hu\n");
  288. shost_rd_attr(unchecked_isa_dma, "%d\n");
  289. shost_rd_attr(prot_capabilities, "%u\n");
  290. shost_rd_attr(prot_guard_type, "%hd\n");
  291. shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
  292. static ssize_t
  293. show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
  294. {
  295. struct Scsi_Host *shost = class_to_shost(dev);
  296. return snprintf(buf, 20, "%d\n", atomic_read(&shost->host_busy));
  297. }
  298. static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
  299. static struct attribute *scsi_sysfs_shost_attrs[] = {
  300. &dev_attr_use_blk_mq.attr,
  301. &dev_attr_unique_id.attr,
  302. &dev_attr_host_busy.attr,
  303. &dev_attr_cmd_per_lun.attr,
  304. &dev_attr_can_queue.attr,
  305. &dev_attr_sg_tablesize.attr,
  306. &dev_attr_sg_prot_tablesize.attr,
  307. &dev_attr_unchecked_isa_dma.attr,
  308. &dev_attr_proc_name.attr,
  309. &dev_attr_scan.attr,
  310. &dev_attr_hstate.attr,
  311. &dev_attr_supported_mode.attr,
  312. &dev_attr_active_mode.attr,
  313. &dev_attr_prot_capabilities.attr,
  314. &dev_attr_prot_guard_type.attr,
  315. &dev_attr_host_reset.attr,
  316. &dev_attr_eh_deadline.attr,
  317. NULL
  318. };
  319. struct attribute_group scsi_shost_attr_group = {
  320. .attrs = scsi_sysfs_shost_attrs,
  321. };
  322. const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
  323. &scsi_shost_attr_group,
  324. NULL
  325. };
  326. static void scsi_device_cls_release(struct device *class_dev)
  327. {
  328. struct scsi_device *sdev;
  329. sdev = class_to_sdev(class_dev);
  330. put_device(&sdev->sdev_gendev);
  331. }
  332. static void scsi_device_dev_release_usercontext(struct work_struct *work)
  333. {
  334. struct scsi_device *sdev;
  335. struct device *parent;
  336. struct list_head *this, *tmp;
  337. unsigned long flags;
  338. sdev = container_of(work, struct scsi_device, ew.work);
  339. scsi_dh_release_device(sdev);
  340. parent = sdev->sdev_gendev.parent;
  341. spin_lock_irqsave(sdev->host->host_lock, flags);
  342. list_del(&sdev->siblings);
  343. list_del(&sdev->same_target_siblings);
  344. list_del(&sdev->starved_entry);
  345. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  346. cancel_work_sync(&sdev->event_work);
  347. list_for_each_safe(this, tmp, &sdev->event_list) {
  348. struct scsi_event *evt;
  349. evt = list_entry(this, struct scsi_event, node);
  350. list_del(&evt->node);
  351. kfree(evt);
  352. }
  353. blk_put_queue(sdev->request_queue);
  354. /* NULL queue means the device can't be used */
  355. sdev->request_queue = NULL;
  356. kfree(sdev->vpd_pg83);
  357. kfree(sdev->vpd_pg80);
  358. kfree(sdev->inquiry);
  359. kfree(sdev);
  360. if (parent)
  361. put_device(parent);
  362. }
  363. static void scsi_device_dev_release(struct device *dev)
  364. {
  365. struct scsi_device *sdp = to_scsi_device(dev);
  366. execute_in_process_context(scsi_device_dev_release_usercontext,
  367. &sdp->ew);
  368. }
  369. static struct class sdev_class = {
  370. .name = "scsi_device",
  371. .dev_release = scsi_device_cls_release,
  372. };
  373. /* all probing is done in the individual ->probe routines */
  374. static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
  375. {
  376. struct scsi_device *sdp;
  377. if (dev->type != &scsi_dev_type)
  378. return 0;
  379. sdp = to_scsi_device(dev);
  380. if (sdp->no_uld_attach)
  381. return 0;
  382. return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
  383. }
  384. static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  385. {
  386. struct scsi_device *sdev;
  387. if (dev->type != &scsi_dev_type)
  388. return 0;
  389. sdev = to_scsi_device(dev);
  390. add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
  391. return 0;
  392. }
  393. struct bus_type scsi_bus_type = {
  394. .name = "scsi",
  395. .match = scsi_bus_match,
  396. .uevent = scsi_bus_uevent,
  397. #ifdef CONFIG_PM
  398. .pm = &scsi_bus_pm_ops,
  399. #endif
  400. };
  401. EXPORT_SYMBOL_GPL(scsi_bus_type);
  402. int scsi_sysfs_register(void)
  403. {
  404. int error;
  405. error = bus_register(&scsi_bus_type);
  406. if (!error) {
  407. error = class_register(&sdev_class);
  408. if (error)
  409. bus_unregister(&scsi_bus_type);
  410. }
  411. return error;
  412. }
  413. void scsi_sysfs_unregister(void)
  414. {
  415. class_unregister(&sdev_class);
  416. bus_unregister(&scsi_bus_type);
  417. }
  418. /*
  419. * sdev_show_function: macro to create an attr function that can be used to
  420. * show a non-bit field.
  421. */
  422. #define sdev_show_function(field, format_string) \
  423. static ssize_t \
  424. sdev_show_##field (struct device *dev, struct device_attribute *attr, \
  425. char *buf) \
  426. { \
  427. struct scsi_device *sdev; \
  428. sdev = to_scsi_device(dev); \
  429. return snprintf (buf, 20, format_string, sdev->field); \
  430. } \
  431. /*
  432. * sdev_rd_attr: macro to create a function and attribute variable for a
  433. * read only field.
  434. */
  435. #define sdev_rd_attr(field, format_string) \
  436. sdev_show_function(field, format_string) \
  437. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  438. /*
  439. * sdev_rw_attr: create a function and attribute variable for a
  440. * read/write field.
  441. */
  442. #define sdev_rw_attr(field, format_string) \
  443. sdev_show_function(field, format_string) \
  444. \
  445. static ssize_t \
  446. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  447. const char *buf, size_t count) \
  448. { \
  449. struct scsi_device *sdev; \
  450. sdev = to_scsi_device(dev); \
  451. sscanf (buf, format_string, &sdev->field); \
  452. return count; \
  453. } \
  454. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  455. /* Currently we don't export bit fields, but we might in future,
  456. * so leave this code in */
  457. #if 0
  458. /*
  459. * sdev_rd_attr: create a function and attribute variable for a
  460. * read/write bit field.
  461. */
  462. #define sdev_rw_attr_bit(field) \
  463. sdev_show_function(field, "%d\n") \
  464. \
  465. static ssize_t \
  466. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  467. const char *buf, size_t count) \
  468. { \
  469. int ret; \
  470. struct scsi_device *sdev; \
  471. ret = scsi_sdev_check_buf_bit(buf); \
  472. if (ret >= 0) { \
  473. sdev = to_scsi_device(dev); \
  474. sdev->field = ret; \
  475. ret = count; \
  476. } \
  477. return ret; \
  478. } \
  479. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  480. /*
  481. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  482. * else return -EINVAL.
  483. */
  484. static int scsi_sdev_check_buf_bit(const char *buf)
  485. {
  486. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  487. if (buf[0] == '1')
  488. return 1;
  489. else if (buf[0] == '0')
  490. return 0;
  491. else
  492. return -EINVAL;
  493. } else
  494. return -EINVAL;
  495. }
  496. #endif
  497. /*
  498. * Create the actual show/store functions and data structures.
  499. */
  500. sdev_rd_attr (type, "%d\n");
  501. sdev_rd_attr (scsi_level, "%d\n");
  502. sdev_rd_attr (vendor, "%.8s\n");
  503. sdev_rd_attr (model, "%.16s\n");
  504. sdev_rd_attr (rev, "%.4s\n");
  505. static ssize_t
  506. sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
  507. char *buf)
  508. {
  509. struct scsi_device *sdev = to_scsi_device(dev);
  510. return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_busy));
  511. }
  512. static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
  513. static ssize_t
  514. sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
  515. char *buf)
  516. {
  517. struct scsi_device *sdev = to_scsi_device(dev);
  518. return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
  519. }
  520. static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
  521. /*
  522. * TODO: can we make these symlinks to the block layer ones?
  523. */
  524. static ssize_t
  525. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  526. {
  527. struct scsi_device *sdev;
  528. sdev = to_scsi_device(dev);
  529. return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
  530. }
  531. static ssize_t
  532. sdev_store_timeout (struct device *dev, struct device_attribute *attr,
  533. const char *buf, size_t count)
  534. {
  535. struct scsi_device *sdev;
  536. int timeout;
  537. sdev = to_scsi_device(dev);
  538. sscanf (buf, "%d\n", &timeout);
  539. blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
  540. return count;
  541. }
  542. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  543. static ssize_t
  544. sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  545. {
  546. struct scsi_device *sdev;
  547. sdev = to_scsi_device(dev);
  548. return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
  549. }
  550. static ssize_t
  551. sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
  552. const char *buf, size_t count)
  553. {
  554. struct scsi_device *sdev;
  555. unsigned int eh_timeout;
  556. int err;
  557. if (!capable(CAP_SYS_ADMIN))
  558. return -EACCES;
  559. sdev = to_scsi_device(dev);
  560. err = kstrtouint(buf, 10, &eh_timeout);
  561. if (err)
  562. return err;
  563. sdev->eh_timeout = eh_timeout * HZ;
  564. return count;
  565. }
  566. static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
  567. static ssize_t
  568. store_rescan_field (struct device *dev, struct device_attribute *attr,
  569. const char *buf, size_t count)
  570. {
  571. scsi_rescan_device(dev);
  572. return count;
  573. }
  574. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  575. static ssize_t
  576. sdev_store_delete(struct device *dev, struct device_attribute *attr,
  577. const char *buf, size_t count)
  578. {
  579. struct kernfs_node *kn;
  580. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  581. WARN_ON_ONCE(!kn);
  582. /*
  583. * Concurrent writes into the "delete" sysfs attribute may trigger
  584. * concurrent calls to device_remove_file() and scsi_remove_device().
  585. * device_remove_file() handles concurrent removal calls by
  586. * serializing these and by ignoring the second and later removal
  587. * attempts. Concurrent calls of scsi_remove_device() are
  588. * serialized. The second and later calls of scsi_remove_device() are
  589. * ignored because the first call of that function changes the device
  590. * state into SDEV_DEL.
  591. */
  592. device_remove_file(dev, attr);
  593. scsi_remove_device(to_scsi_device(dev));
  594. if (kn)
  595. sysfs_unbreak_active_protection(kn);
  596. return count;
  597. };
  598. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  599. static ssize_t
  600. store_state_field(struct device *dev, struct device_attribute *attr,
  601. const char *buf, size_t count)
  602. {
  603. int i;
  604. struct scsi_device *sdev = to_scsi_device(dev);
  605. enum scsi_device_state state = 0;
  606. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  607. const int len = strlen(sdev_states[i].name);
  608. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  609. buf[len] == '\n') {
  610. state = sdev_states[i].value;
  611. break;
  612. }
  613. }
  614. if (!state)
  615. return -EINVAL;
  616. if (scsi_device_set_state(sdev, state))
  617. return -EINVAL;
  618. return count;
  619. }
  620. static ssize_t
  621. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  622. {
  623. struct scsi_device *sdev = to_scsi_device(dev);
  624. const char *name = scsi_device_state_name(sdev->sdev_state);
  625. if (!name)
  626. return -EINVAL;
  627. return snprintf(buf, 20, "%s\n", name);
  628. }
  629. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  630. static ssize_t
  631. show_queue_type_field(struct device *dev, struct device_attribute *attr,
  632. char *buf)
  633. {
  634. struct scsi_device *sdev = to_scsi_device(dev);
  635. const char *name = "none";
  636. if (sdev->simple_tags)
  637. name = "simple";
  638. return snprintf(buf, 20, "%s\n", name);
  639. }
  640. static ssize_t
  641. store_queue_type_field(struct device *dev, struct device_attribute *attr,
  642. const char *buf, size_t count)
  643. {
  644. struct scsi_device *sdev = to_scsi_device(dev);
  645. if (!sdev->tagged_supported)
  646. return -EINVAL;
  647. sdev_printk(KERN_INFO, sdev,
  648. "ignoring write to deprecated queue_type attribute");
  649. return count;
  650. }
  651. static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  652. store_queue_type_field);
  653. #define sdev_vpd_pg_attr(_page) \
  654. static ssize_t \
  655. show_vpd_##_page(struct file *filp, struct kobject *kobj, \
  656. struct bin_attribute *bin_attr, \
  657. char *buf, loff_t off, size_t count) \
  658. { \
  659. struct device *dev = container_of(kobj, struct device, kobj); \
  660. struct scsi_device *sdev = to_scsi_device(dev); \
  661. if (!sdev->vpd_##_page) \
  662. return -EINVAL; \
  663. return memory_read_from_buffer(buf, count, &off, \
  664. sdev->vpd_##_page, \
  665. sdev->vpd_##_page##_len); \
  666. } \
  667. static struct bin_attribute dev_attr_vpd_##_page = { \
  668. .attr = {.name = __stringify(vpd_##_page), .mode = S_IRUGO }, \
  669. .size = 0, \
  670. .read = show_vpd_##_page, \
  671. };
  672. sdev_vpd_pg_attr(pg83);
  673. sdev_vpd_pg_attr(pg80);
  674. static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
  675. struct bin_attribute *bin_attr,
  676. char *buf, loff_t off, size_t count)
  677. {
  678. struct device *dev = container_of(kobj, struct device, kobj);
  679. struct scsi_device *sdev = to_scsi_device(dev);
  680. if (!sdev->inquiry)
  681. return -EINVAL;
  682. return memory_read_from_buffer(buf, count, &off, sdev->inquiry,
  683. sdev->inquiry_len);
  684. }
  685. static struct bin_attribute dev_attr_inquiry = {
  686. .attr = {
  687. .name = "inquiry",
  688. .mode = S_IRUGO,
  689. },
  690. .size = 0,
  691. .read = show_inquiry,
  692. };
  693. static ssize_t
  694. show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
  695. char *buf)
  696. {
  697. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  698. }
  699. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  700. #define show_sdev_iostat(field) \
  701. static ssize_t \
  702. show_iostat_##field(struct device *dev, struct device_attribute *attr, \
  703. char *buf) \
  704. { \
  705. struct scsi_device *sdev = to_scsi_device(dev); \
  706. unsigned long long count = atomic_read(&sdev->field); \
  707. return snprintf(buf, 20, "0x%llx\n", count); \
  708. } \
  709. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  710. show_sdev_iostat(iorequest_cnt);
  711. show_sdev_iostat(iodone_cnt);
  712. show_sdev_iostat(ioerr_cnt);
  713. static ssize_t
  714. sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  715. {
  716. struct scsi_device *sdev;
  717. sdev = to_scsi_device(dev);
  718. return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
  719. }
  720. static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
  721. #define DECLARE_EVT_SHOW(name, Cap_name) \
  722. static ssize_t \
  723. sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
  724. char *buf) \
  725. { \
  726. struct scsi_device *sdev = to_scsi_device(dev); \
  727. int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
  728. return snprintf(buf, 20, "%d\n", val); \
  729. }
  730. #define DECLARE_EVT_STORE(name, Cap_name) \
  731. static ssize_t \
  732. sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
  733. const char *buf, size_t count) \
  734. { \
  735. struct scsi_device *sdev = to_scsi_device(dev); \
  736. int val = simple_strtoul(buf, NULL, 0); \
  737. if (val == 0) \
  738. clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  739. else if (val == 1) \
  740. set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  741. else \
  742. return -EINVAL; \
  743. return count; \
  744. }
  745. #define DECLARE_EVT(name, Cap_name) \
  746. DECLARE_EVT_SHOW(name, Cap_name) \
  747. DECLARE_EVT_STORE(name, Cap_name) \
  748. static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
  749. sdev_store_evt_##name);
  750. #define REF_EVT(name) &dev_attr_evt_##name.attr
  751. DECLARE_EVT(media_change, MEDIA_CHANGE)
  752. DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
  753. DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
  754. DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
  755. DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
  756. DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
  757. static ssize_t
  758. sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
  759. const char *buf, size_t count)
  760. {
  761. int depth, retval;
  762. struct scsi_device *sdev = to_scsi_device(dev);
  763. struct scsi_host_template *sht = sdev->host->hostt;
  764. if (!sht->change_queue_depth)
  765. return -EINVAL;
  766. depth = simple_strtoul(buf, NULL, 0);
  767. if (depth < 1 || depth > sdev->host->can_queue)
  768. return -EINVAL;
  769. retval = sht->change_queue_depth(sdev, depth);
  770. if (retval < 0)
  771. return retval;
  772. sdev->max_queue_depth = sdev->queue_depth;
  773. return count;
  774. }
  775. sdev_show_function(queue_depth, "%d\n");
  776. static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  777. sdev_store_queue_depth);
  778. static ssize_t
  779. sdev_show_queue_ramp_up_period(struct device *dev,
  780. struct device_attribute *attr,
  781. char *buf)
  782. {
  783. struct scsi_device *sdev;
  784. sdev = to_scsi_device(dev);
  785. return snprintf(buf, 20, "%u\n",
  786. jiffies_to_msecs(sdev->queue_ramp_up_period));
  787. }
  788. static ssize_t
  789. sdev_store_queue_ramp_up_period(struct device *dev,
  790. struct device_attribute *attr,
  791. const char *buf, size_t count)
  792. {
  793. struct scsi_device *sdev = to_scsi_device(dev);
  794. unsigned int period;
  795. if (kstrtouint(buf, 10, &period))
  796. return -EINVAL;
  797. sdev->queue_ramp_up_period = msecs_to_jiffies(period);
  798. return count;
  799. }
  800. static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
  801. sdev_show_queue_ramp_up_period,
  802. sdev_store_queue_ramp_up_period);
  803. static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
  804. struct attribute *attr, int i)
  805. {
  806. struct device *dev = container_of(kobj, struct device, kobj);
  807. struct scsi_device *sdev = to_scsi_device(dev);
  808. if (attr == &dev_attr_queue_depth.attr &&
  809. !sdev->host->hostt->change_queue_depth)
  810. return S_IRUGO;
  811. if (attr == &dev_attr_queue_ramp_up_period.attr &&
  812. !sdev->host->hostt->change_queue_depth)
  813. return 0;
  814. return attr->mode;
  815. }
  816. /* Default template for device attributes. May NOT be modified */
  817. static struct attribute *scsi_sdev_attrs[] = {
  818. &dev_attr_device_blocked.attr,
  819. &dev_attr_type.attr,
  820. &dev_attr_scsi_level.attr,
  821. &dev_attr_device_busy.attr,
  822. &dev_attr_vendor.attr,
  823. &dev_attr_model.attr,
  824. &dev_attr_rev.attr,
  825. &dev_attr_rescan.attr,
  826. &dev_attr_delete.attr,
  827. &dev_attr_state.attr,
  828. &dev_attr_timeout.attr,
  829. &dev_attr_eh_timeout.attr,
  830. &dev_attr_iocounterbits.attr,
  831. &dev_attr_iorequest_cnt.attr,
  832. &dev_attr_iodone_cnt.attr,
  833. &dev_attr_ioerr_cnt.attr,
  834. &dev_attr_modalias.attr,
  835. &dev_attr_queue_depth.attr,
  836. &dev_attr_queue_type.attr,
  837. &dev_attr_queue_ramp_up_period.attr,
  838. REF_EVT(media_change),
  839. REF_EVT(inquiry_change_reported),
  840. REF_EVT(capacity_change_reported),
  841. REF_EVT(soft_threshold_reached),
  842. REF_EVT(mode_parameter_change_reported),
  843. REF_EVT(lun_change_reported),
  844. NULL
  845. };
  846. static struct bin_attribute *scsi_sdev_bin_attrs[] = {
  847. &dev_attr_vpd_pg83,
  848. &dev_attr_vpd_pg80,
  849. &dev_attr_inquiry,
  850. NULL
  851. };
  852. static struct attribute_group scsi_sdev_attr_group = {
  853. .attrs = scsi_sdev_attrs,
  854. .bin_attrs = scsi_sdev_bin_attrs,
  855. .is_visible = scsi_sdev_attr_is_visible,
  856. };
  857. static const struct attribute_group *scsi_sdev_attr_groups[] = {
  858. &scsi_sdev_attr_group,
  859. NULL
  860. };
  861. static int scsi_target_add(struct scsi_target *starget)
  862. {
  863. int error;
  864. if (starget->state != STARGET_CREATED)
  865. return 0;
  866. error = device_add(&starget->dev);
  867. if (error) {
  868. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  869. return error;
  870. }
  871. transport_add_device(&starget->dev);
  872. starget->state = STARGET_RUNNING;
  873. pm_runtime_set_active(&starget->dev);
  874. pm_runtime_enable(&starget->dev);
  875. device_enable_async_suspend(&starget->dev);
  876. return 0;
  877. }
  878. /**
  879. * scsi_sysfs_add_sdev - add scsi device to sysfs
  880. * @sdev: scsi_device to add
  881. *
  882. * Return value:
  883. * 0 on Success / non-zero on Failure
  884. **/
  885. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  886. {
  887. int error, i;
  888. struct request_queue *rq = sdev->request_queue;
  889. struct scsi_target *starget = sdev->sdev_target;
  890. error = scsi_target_add(starget);
  891. if (error)
  892. return error;
  893. transport_configure_device(&starget->dev);
  894. device_enable_async_suspend(&sdev->sdev_gendev);
  895. scsi_autopm_get_target(starget);
  896. pm_runtime_set_active(&sdev->sdev_gendev);
  897. pm_runtime_forbid(&sdev->sdev_gendev);
  898. pm_runtime_enable(&sdev->sdev_gendev);
  899. scsi_autopm_put_target(starget);
  900. scsi_autopm_get_device(sdev);
  901. error = device_add(&sdev->sdev_gendev);
  902. if (error) {
  903. sdev_printk(KERN_INFO, sdev,
  904. "failed to add device: %d\n", error);
  905. return error;
  906. }
  907. error = scsi_dh_add_device(sdev);
  908. if (error)
  909. /*
  910. * device_handler is optional, so any error can be ignored
  911. */
  912. sdev_printk(KERN_INFO, sdev,
  913. "failed to add device handler: %d\n", error);
  914. device_enable_async_suspend(&sdev->sdev_dev);
  915. error = device_add(&sdev->sdev_dev);
  916. if (error) {
  917. sdev_printk(KERN_INFO, sdev,
  918. "failed to add class device: %d\n", error);
  919. scsi_dh_remove_device(sdev);
  920. device_del(&sdev->sdev_gendev);
  921. return error;
  922. }
  923. transport_add_device(&sdev->sdev_gendev);
  924. sdev->is_visible = 1;
  925. error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
  926. if (error)
  927. /* we're treating error on bsg register as non-fatal,
  928. * so pretend nothing went wrong */
  929. sdev_printk(KERN_INFO, sdev,
  930. "Failed to register bsg queue, errno=%d\n", error);
  931. /* add additional host specific attributes */
  932. if (sdev->host->hostt->sdev_attrs) {
  933. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  934. error = device_create_file(&sdev->sdev_gendev,
  935. sdev->host->hostt->sdev_attrs[i]);
  936. if (error)
  937. return error;
  938. }
  939. }
  940. scsi_autopm_put_device(sdev);
  941. return error;
  942. }
  943. void __scsi_remove_device(struct scsi_device *sdev)
  944. {
  945. struct device *dev = &sdev->sdev_gendev;
  946. /*
  947. * This cleanup path is not reentrant and while it is impossible
  948. * to get a new reference with scsi_device_get() someone can still
  949. * hold a previously acquired one.
  950. */
  951. if (sdev->sdev_state == SDEV_DEL)
  952. return;
  953. if (sdev->is_visible) {
  954. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  955. return;
  956. bsg_unregister_queue(sdev->request_queue);
  957. device_unregister(&sdev->sdev_dev);
  958. transport_remove_device(dev);
  959. scsi_dh_remove_device(sdev);
  960. device_del(dev);
  961. } else
  962. put_device(&sdev->sdev_dev);
  963. /*
  964. * Stop accepting new requests and wait until all queuecommand() and
  965. * scsi_run_queue() invocations have finished before tearing down the
  966. * device.
  967. */
  968. scsi_device_set_state(sdev, SDEV_DEL);
  969. blk_cleanup_queue(sdev->request_queue);
  970. cancel_work_sync(&sdev->requeue_work);
  971. if (sdev->host->hostt->slave_destroy)
  972. sdev->host->hostt->slave_destroy(sdev);
  973. transport_destroy_device(dev);
  974. /*
  975. * Paired with the kref_get() in scsi_sysfs_initialize(). We have
  976. * remoed sysfs visibility from the device, so make the target
  977. * invisible if this was the last device underneath it.
  978. */
  979. scsi_target_reap(scsi_target(sdev));
  980. put_device(dev);
  981. }
  982. /**
  983. * scsi_remove_device - unregister a device from the scsi bus
  984. * @sdev: scsi_device to unregister
  985. **/
  986. void scsi_remove_device(struct scsi_device *sdev)
  987. {
  988. struct Scsi_Host *shost = sdev->host;
  989. mutex_lock(&shost->scan_mutex);
  990. __scsi_remove_device(sdev);
  991. mutex_unlock(&shost->scan_mutex);
  992. }
  993. EXPORT_SYMBOL(scsi_remove_device);
  994. static void __scsi_remove_target(struct scsi_target *starget)
  995. {
  996. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  997. unsigned long flags;
  998. struct scsi_device *sdev;
  999. spin_lock_irqsave(shost->host_lock, flags);
  1000. restart:
  1001. list_for_each_entry(sdev, &shost->__devices, siblings) {
  1002. if (sdev->channel != starget->channel ||
  1003. sdev->id != starget->id ||
  1004. scsi_device_get(sdev))
  1005. continue;
  1006. spin_unlock_irqrestore(shost->host_lock, flags);
  1007. scsi_remove_device(sdev);
  1008. scsi_device_put(sdev);
  1009. spin_lock_irqsave(shost->host_lock, flags);
  1010. goto restart;
  1011. }
  1012. spin_unlock_irqrestore(shost->host_lock, flags);
  1013. }
  1014. /**
  1015. * scsi_remove_target - try to remove a target and all its devices
  1016. * @dev: generic starget or parent of generic stargets to be removed
  1017. *
  1018. * Note: This is slightly racy. It is possible that if the user
  1019. * requests the addition of another device then the target won't be
  1020. * removed.
  1021. */
  1022. void scsi_remove_target(struct device *dev)
  1023. {
  1024. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  1025. struct scsi_target *starget;
  1026. unsigned long flags;
  1027. restart:
  1028. spin_lock_irqsave(shost->host_lock, flags);
  1029. list_for_each_entry(starget, &shost->__targets, siblings) {
  1030. if (starget->state == STARGET_DEL ||
  1031. starget->state == STARGET_REMOVE ||
  1032. starget->state == STARGET_CREATED_REMOVE)
  1033. continue;
  1034. if (starget->dev.parent == dev || &starget->dev == dev) {
  1035. kref_get(&starget->reap_ref);
  1036. if (starget->state == STARGET_CREATED)
  1037. starget->state = STARGET_CREATED_REMOVE;
  1038. else
  1039. starget->state = STARGET_REMOVE;
  1040. spin_unlock_irqrestore(shost->host_lock, flags);
  1041. __scsi_remove_target(starget);
  1042. scsi_target_reap(starget);
  1043. goto restart;
  1044. }
  1045. }
  1046. spin_unlock_irqrestore(shost->host_lock, flags);
  1047. }
  1048. EXPORT_SYMBOL(scsi_remove_target);
  1049. int scsi_register_driver(struct device_driver *drv)
  1050. {
  1051. drv->bus = &scsi_bus_type;
  1052. return driver_register(drv);
  1053. }
  1054. EXPORT_SYMBOL(scsi_register_driver);
  1055. int scsi_register_interface(struct class_interface *intf)
  1056. {
  1057. intf->class = &sdev_class;
  1058. return class_interface_register(intf);
  1059. }
  1060. EXPORT_SYMBOL(scsi_register_interface);
  1061. /**
  1062. * scsi_sysfs_add_host - add scsi host to subsystem
  1063. * @shost: scsi host struct to add to subsystem
  1064. **/
  1065. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  1066. {
  1067. int error, i;
  1068. /* add host specific attributes */
  1069. if (shost->hostt->shost_attrs) {
  1070. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  1071. error = device_create_file(&shost->shost_dev,
  1072. shost->hostt->shost_attrs[i]);
  1073. if (error)
  1074. return error;
  1075. }
  1076. }
  1077. transport_register_device(&shost->shost_gendev);
  1078. transport_configure_device(&shost->shost_gendev);
  1079. return 0;
  1080. }
  1081. static struct device_type scsi_dev_type = {
  1082. .name = "scsi_device",
  1083. .release = scsi_device_dev_release,
  1084. .groups = scsi_sdev_attr_groups,
  1085. };
  1086. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  1087. {
  1088. unsigned long flags;
  1089. struct Scsi_Host *shost = sdev->host;
  1090. struct scsi_target *starget = sdev->sdev_target;
  1091. device_initialize(&sdev->sdev_gendev);
  1092. sdev->sdev_gendev.bus = &scsi_bus_type;
  1093. sdev->sdev_gendev.type = &scsi_dev_type;
  1094. dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
  1095. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  1096. device_initialize(&sdev->sdev_dev);
  1097. sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
  1098. sdev->sdev_dev.class = &sdev_class;
  1099. dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
  1100. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  1101. /*
  1102. * Get a default scsi_level from the target (derived from sibling
  1103. * devices). This is the best we can do for guessing how to set
  1104. * sdev->lun_in_cdb for the initial INQUIRY command. For LUN 0 the
  1105. * setting doesn't matter, because all the bits are zero anyway.
  1106. * But it does matter for higher LUNs.
  1107. */
  1108. sdev->scsi_level = starget->scsi_level;
  1109. if (sdev->scsi_level <= SCSI_2 &&
  1110. sdev->scsi_level != SCSI_UNKNOWN &&
  1111. !shost->no_scsi2_lun_in_cdb)
  1112. sdev->lun_in_cdb = 1;
  1113. transport_setup_device(&sdev->sdev_gendev);
  1114. spin_lock_irqsave(shost->host_lock, flags);
  1115. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  1116. list_add_tail(&sdev->siblings, &shost->__devices);
  1117. spin_unlock_irqrestore(shost->host_lock, flags);
  1118. /*
  1119. * device can now only be removed via __scsi_remove_device() so hold
  1120. * the target. Target will be held in CREATED state until something
  1121. * beneath it becomes visible (in which case it moves to RUNNING)
  1122. */
  1123. kref_get(&starget->reap_ref);
  1124. }
  1125. int scsi_is_sdev_device(const struct device *dev)
  1126. {
  1127. return dev->type == &scsi_dev_type;
  1128. }
  1129. EXPORT_SYMBOL(scsi_is_sdev_device);
  1130. /* A blank transport template that is used in drivers that don't
  1131. * yet implement Transport Attributes */
  1132. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };