scsi_sysfs.c 38 KB

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