scsi_sysfs.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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 int *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_strtoul(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[15], junk;
  91. unsigned int channel, id, lun;
  92. int res;
  93. res = sscanf(str, "%10s %10s %10s %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. return sprintf(buf, "%d\n", shost->eh_deadline / HZ);
  245. }
  246. static ssize_t
  247. store_shost_eh_deadline(struct device *dev, struct device_attribute *attr,
  248. const char *buf, size_t count)
  249. {
  250. struct Scsi_Host *shost = class_to_shost(dev);
  251. int ret = -EINVAL;
  252. int deadline;
  253. unsigned long flags;
  254. if (shost->transportt && shost->transportt->eh_strategy_handler)
  255. return ret;
  256. if (sscanf(buf, "%d\n", &deadline) == 1) {
  257. spin_lock_irqsave(shost->host_lock, flags);
  258. if (scsi_host_in_recovery(shost))
  259. ret = -EBUSY;
  260. else {
  261. shost->eh_deadline = deadline * HZ;
  262. ret = count;
  263. }
  264. spin_unlock_irqrestore(shost->host_lock, flags);
  265. }
  266. return ret;
  267. }
  268. static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
  269. shost_rd_attr(unique_id, "%u\n");
  270. shost_rd_attr(host_busy, "%hu\n");
  271. shost_rd_attr(cmd_per_lun, "%hd\n");
  272. shost_rd_attr(can_queue, "%hd\n");
  273. shost_rd_attr(sg_tablesize, "%hu\n");
  274. shost_rd_attr(sg_prot_tablesize, "%hu\n");
  275. shost_rd_attr(unchecked_isa_dma, "%d\n");
  276. shost_rd_attr(prot_capabilities, "%u\n");
  277. shost_rd_attr(prot_guard_type, "%hd\n");
  278. shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
  279. static struct attribute *scsi_sysfs_shost_attrs[] = {
  280. &dev_attr_unique_id.attr,
  281. &dev_attr_host_busy.attr,
  282. &dev_attr_cmd_per_lun.attr,
  283. &dev_attr_can_queue.attr,
  284. &dev_attr_sg_tablesize.attr,
  285. &dev_attr_sg_prot_tablesize.attr,
  286. &dev_attr_unchecked_isa_dma.attr,
  287. &dev_attr_proc_name.attr,
  288. &dev_attr_scan.attr,
  289. &dev_attr_hstate.attr,
  290. &dev_attr_supported_mode.attr,
  291. &dev_attr_active_mode.attr,
  292. &dev_attr_prot_capabilities.attr,
  293. &dev_attr_prot_guard_type.attr,
  294. &dev_attr_host_reset.attr,
  295. &dev_attr_eh_deadline.attr,
  296. NULL
  297. };
  298. struct attribute_group scsi_shost_attr_group = {
  299. .attrs = scsi_sysfs_shost_attrs,
  300. };
  301. const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
  302. &scsi_shost_attr_group,
  303. NULL
  304. };
  305. static void scsi_device_cls_release(struct device *class_dev)
  306. {
  307. struct scsi_device *sdev;
  308. sdev = class_to_sdev(class_dev);
  309. put_device(&sdev->sdev_gendev);
  310. }
  311. static void scsi_device_dev_release_usercontext(struct work_struct *work)
  312. {
  313. struct scsi_device *sdev;
  314. struct device *parent;
  315. struct scsi_target *starget;
  316. struct list_head *this, *tmp;
  317. unsigned long flags;
  318. sdev = container_of(work, struct scsi_device, ew.work);
  319. parent = sdev->sdev_gendev.parent;
  320. starget = to_scsi_target(parent);
  321. spin_lock_irqsave(sdev->host->host_lock, flags);
  322. starget->reap_ref++;
  323. list_del(&sdev->siblings);
  324. list_del(&sdev->same_target_siblings);
  325. list_del(&sdev->starved_entry);
  326. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  327. cancel_work_sync(&sdev->event_work);
  328. list_for_each_safe(this, tmp, &sdev->event_list) {
  329. struct scsi_event *evt;
  330. evt = list_entry(this, struct scsi_event, node);
  331. list_del(&evt->node);
  332. kfree(evt);
  333. }
  334. blk_put_queue(sdev->request_queue);
  335. /* NULL queue means the device can't be used */
  336. sdev->request_queue = NULL;
  337. scsi_target_reap(scsi_target(sdev));
  338. kfree(sdev->inquiry);
  339. kfree(sdev);
  340. if (parent)
  341. put_device(parent);
  342. }
  343. static void scsi_device_dev_release(struct device *dev)
  344. {
  345. struct scsi_device *sdp = to_scsi_device(dev);
  346. execute_in_process_context(scsi_device_dev_release_usercontext,
  347. &sdp->ew);
  348. }
  349. static struct class sdev_class = {
  350. .name = "scsi_device",
  351. .dev_release = scsi_device_cls_release,
  352. };
  353. /* all probing is done in the individual ->probe routines */
  354. static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
  355. {
  356. struct scsi_device *sdp;
  357. if (dev->type != &scsi_dev_type)
  358. return 0;
  359. sdp = to_scsi_device(dev);
  360. if (sdp->no_uld_attach)
  361. return 0;
  362. return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
  363. }
  364. static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  365. {
  366. struct scsi_device *sdev;
  367. if (dev->type != &scsi_dev_type)
  368. return 0;
  369. sdev = to_scsi_device(dev);
  370. add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
  371. return 0;
  372. }
  373. struct bus_type scsi_bus_type = {
  374. .name = "scsi",
  375. .match = scsi_bus_match,
  376. .uevent = scsi_bus_uevent,
  377. #ifdef CONFIG_PM
  378. .pm = &scsi_bus_pm_ops,
  379. #endif
  380. };
  381. EXPORT_SYMBOL_GPL(scsi_bus_type);
  382. int scsi_sysfs_register(void)
  383. {
  384. int error;
  385. error = bus_register(&scsi_bus_type);
  386. if (!error) {
  387. error = class_register(&sdev_class);
  388. if (error)
  389. bus_unregister(&scsi_bus_type);
  390. }
  391. return error;
  392. }
  393. void scsi_sysfs_unregister(void)
  394. {
  395. class_unregister(&sdev_class);
  396. bus_unregister(&scsi_bus_type);
  397. }
  398. /*
  399. * sdev_show_function: macro to create an attr function that can be used to
  400. * show a non-bit field.
  401. */
  402. #define sdev_show_function(field, format_string) \
  403. static ssize_t \
  404. sdev_show_##field (struct device *dev, struct device_attribute *attr, \
  405. char *buf) \
  406. { \
  407. struct scsi_device *sdev; \
  408. sdev = to_scsi_device(dev); \
  409. return snprintf (buf, 20, format_string, sdev->field); \
  410. } \
  411. /*
  412. * sdev_rd_attr: macro to create a function and attribute variable for a
  413. * read only field.
  414. */
  415. #define sdev_rd_attr(field, format_string) \
  416. sdev_show_function(field, format_string) \
  417. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  418. /*
  419. * sdev_rw_attr: create a function and attribute variable for a
  420. * read/write field.
  421. */
  422. #define sdev_rw_attr(field, format_string) \
  423. sdev_show_function(field, format_string) \
  424. \
  425. static ssize_t \
  426. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  427. const char *buf, size_t count) \
  428. { \
  429. struct scsi_device *sdev; \
  430. sdev = to_scsi_device(dev); \
  431. sscanf (buf, format_string, &sdev->field); \
  432. return count; \
  433. } \
  434. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  435. /* Currently we don't export bit fields, but we might in future,
  436. * so leave this code in */
  437. #if 0
  438. /*
  439. * sdev_rd_attr: create a function and attribute variable for a
  440. * read/write bit field.
  441. */
  442. #define sdev_rw_attr_bit(field) \
  443. sdev_show_function(field, "%d\n") \
  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. int ret; \
  450. struct scsi_device *sdev; \
  451. ret = scsi_sdev_check_buf_bit(buf); \
  452. if (ret >= 0) { \
  453. sdev = to_scsi_device(dev); \
  454. sdev->field = ret; \
  455. ret = count; \
  456. } \
  457. return ret; \
  458. } \
  459. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  460. /*
  461. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  462. * else return -EINVAL.
  463. */
  464. static int scsi_sdev_check_buf_bit(const char *buf)
  465. {
  466. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  467. if (buf[0] == '1')
  468. return 1;
  469. else if (buf[0] == '0')
  470. return 0;
  471. else
  472. return -EINVAL;
  473. } else
  474. return -EINVAL;
  475. }
  476. #endif
  477. /*
  478. * Create the actual show/store functions and data structures.
  479. */
  480. sdev_rd_attr (device_blocked, "%d\n");
  481. sdev_rd_attr (queue_depth, "%d\n");
  482. sdev_rd_attr (device_busy, "%d\n");
  483. sdev_rd_attr (type, "%d\n");
  484. sdev_rd_attr (scsi_level, "%d\n");
  485. sdev_rd_attr (vendor, "%.8s\n");
  486. sdev_rd_attr (model, "%.16s\n");
  487. sdev_rd_attr (rev, "%.4s\n");
  488. /*
  489. * TODO: can we make these symlinks to the block layer ones?
  490. */
  491. static ssize_t
  492. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  493. {
  494. struct scsi_device *sdev;
  495. sdev = to_scsi_device(dev);
  496. return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
  497. }
  498. static ssize_t
  499. sdev_store_timeout (struct device *dev, struct device_attribute *attr,
  500. const char *buf, size_t count)
  501. {
  502. struct scsi_device *sdev;
  503. int timeout;
  504. sdev = to_scsi_device(dev);
  505. sscanf (buf, "%d\n", &timeout);
  506. blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
  507. return count;
  508. }
  509. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  510. static ssize_t
  511. sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  512. {
  513. struct scsi_device *sdev;
  514. sdev = to_scsi_device(dev);
  515. return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
  516. }
  517. static ssize_t
  518. sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
  519. const char *buf, size_t count)
  520. {
  521. struct scsi_device *sdev;
  522. unsigned int eh_timeout;
  523. int err;
  524. if (!capable(CAP_SYS_ADMIN))
  525. return -EACCES;
  526. sdev = to_scsi_device(dev);
  527. err = kstrtouint(buf, 10, &eh_timeout);
  528. if (err)
  529. return err;
  530. sdev->eh_timeout = eh_timeout * HZ;
  531. return count;
  532. }
  533. static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
  534. static ssize_t
  535. store_rescan_field (struct device *dev, struct device_attribute *attr,
  536. const char *buf, size_t count)
  537. {
  538. scsi_rescan_device(dev);
  539. return count;
  540. }
  541. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  542. static void sdev_store_delete_callback(struct device *dev)
  543. {
  544. scsi_remove_device(to_scsi_device(dev));
  545. }
  546. static ssize_t
  547. sdev_store_delete(struct device *dev, struct device_attribute *attr,
  548. const char *buf, size_t count)
  549. {
  550. int rc;
  551. /* An attribute cannot be unregistered by one of its own methods,
  552. * so we have to use this roundabout approach.
  553. */
  554. rc = device_schedule_callback(dev, sdev_store_delete_callback);
  555. if (rc)
  556. count = rc;
  557. return count;
  558. };
  559. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  560. static ssize_t
  561. store_state_field(struct device *dev, struct device_attribute *attr,
  562. const char *buf, size_t count)
  563. {
  564. int i;
  565. struct scsi_device *sdev = to_scsi_device(dev);
  566. enum scsi_device_state state = 0;
  567. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  568. const int len = strlen(sdev_states[i].name);
  569. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  570. buf[len] == '\n') {
  571. state = sdev_states[i].value;
  572. break;
  573. }
  574. }
  575. if (!state)
  576. return -EINVAL;
  577. if (scsi_device_set_state(sdev, state))
  578. return -EINVAL;
  579. return count;
  580. }
  581. static ssize_t
  582. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  583. {
  584. struct scsi_device *sdev = to_scsi_device(dev);
  585. const char *name = scsi_device_state_name(sdev->sdev_state);
  586. if (!name)
  587. return -EINVAL;
  588. return snprintf(buf, 20, "%s\n", name);
  589. }
  590. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  591. static ssize_t
  592. show_queue_type_field(struct device *dev, struct device_attribute *attr,
  593. char *buf)
  594. {
  595. struct scsi_device *sdev = to_scsi_device(dev);
  596. const char *name = "none";
  597. if (sdev->ordered_tags)
  598. name = "ordered";
  599. else if (sdev->simple_tags)
  600. name = "simple";
  601. return snprintf(buf, 20, "%s\n", name);
  602. }
  603. static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
  604. static ssize_t
  605. show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
  606. {
  607. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  608. }
  609. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  610. #define show_sdev_iostat(field) \
  611. static ssize_t \
  612. show_iostat_##field(struct device *dev, struct device_attribute *attr, \
  613. char *buf) \
  614. { \
  615. struct scsi_device *sdev = to_scsi_device(dev); \
  616. unsigned long long count = atomic_read(&sdev->field); \
  617. return snprintf(buf, 20, "0x%llx\n", count); \
  618. } \
  619. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  620. show_sdev_iostat(iorequest_cnt);
  621. show_sdev_iostat(iodone_cnt);
  622. show_sdev_iostat(ioerr_cnt);
  623. static ssize_t
  624. sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  625. {
  626. struct scsi_device *sdev;
  627. sdev = to_scsi_device(dev);
  628. return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
  629. }
  630. static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
  631. #define DECLARE_EVT_SHOW(name, Cap_name) \
  632. static ssize_t \
  633. sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
  634. char *buf) \
  635. { \
  636. struct scsi_device *sdev = to_scsi_device(dev); \
  637. int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
  638. return snprintf(buf, 20, "%d\n", val); \
  639. }
  640. #define DECLARE_EVT_STORE(name, Cap_name) \
  641. static ssize_t \
  642. sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
  643. const char *buf, size_t count) \
  644. { \
  645. struct scsi_device *sdev = to_scsi_device(dev); \
  646. int val = simple_strtoul(buf, NULL, 0); \
  647. if (val == 0) \
  648. clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  649. else if (val == 1) \
  650. set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  651. else \
  652. return -EINVAL; \
  653. return count; \
  654. }
  655. #define DECLARE_EVT(name, Cap_name) \
  656. DECLARE_EVT_SHOW(name, Cap_name) \
  657. DECLARE_EVT_STORE(name, Cap_name) \
  658. static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
  659. sdev_store_evt_##name);
  660. #define REF_EVT(name) &dev_attr_evt_##name.attr
  661. DECLARE_EVT(media_change, MEDIA_CHANGE)
  662. DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
  663. DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
  664. DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
  665. DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
  666. DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
  667. /* Default template for device attributes. May NOT be modified */
  668. static struct attribute *scsi_sdev_attrs[] = {
  669. &dev_attr_device_blocked.attr,
  670. &dev_attr_type.attr,
  671. &dev_attr_scsi_level.attr,
  672. &dev_attr_device_busy.attr,
  673. &dev_attr_vendor.attr,
  674. &dev_attr_model.attr,
  675. &dev_attr_rev.attr,
  676. &dev_attr_rescan.attr,
  677. &dev_attr_delete.attr,
  678. &dev_attr_state.attr,
  679. &dev_attr_timeout.attr,
  680. &dev_attr_eh_timeout.attr,
  681. &dev_attr_iocounterbits.attr,
  682. &dev_attr_iorequest_cnt.attr,
  683. &dev_attr_iodone_cnt.attr,
  684. &dev_attr_ioerr_cnt.attr,
  685. &dev_attr_modalias.attr,
  686. REF_EVT(media_change),
  687. REF_EVT(inquiry_change_reported),
  688. REF_EVT(capacity_change_reported),
  689. REF_EVT(soft_threshold_reached),
  690. REF_EVT(mode_parameter_change_reported),
  691. REF_EVT(lun_change_reported),
  692. NULL
  693. };
  694. static struct attribute_group scsi_sdev_attr_group = {
  695. .attrs = scsi_sdev_attrs,
  696. };
  697. static const struct attribute_group *scsi_sdev_attr_groups[] = {
  698. &scsi_sdev_attr_group,
  699. NULL
  700. };
  701. static ssize_t
  702. sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
  703. const char *buf, size_t count)
  704. {
  705. int depth, retval;
  706. struct scsi_device *sdev = to_scsi_device(dev);
  707. struct scsi_host_template *sht = sdev->host->hostt;
  708. if (!sht->change_queue_depth)
  709. return -EINVAL;
  710. depth = simple_strtoul(buf, NULL, 0);
  711. if (depth < 1)
  712. return -EINVAL;
  713. retval = sht->change_queue_depth(sdev, depth,
  714. SCSI_QDEPTH_DEFAULT);
  715. if (retval < 0)
  716. return retval;
  717. sdev->max_queue_depth = sdev->queue_depth;
  718. return count;
  719. }
  720. static struct device_attribute sdev_attr_queue_depth_rw =
  721. __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  722. sdev_store_queue_depth_rw);
  723. static ssize_t
  724. sdev_show_queue_ramp_up_period(struct device *dev,
  725. struct device_attribute *attr,
  726. char *buf)
  727. {
  728. struct scsi_device *sdev;
  729. sdev = to_scsi_device(dev);
  730. return snprintf(buf, 20, "%u\n",
  731. jiffies_to_msecs(sdev->queue_ramp_up_period));
  732. }
  733. static ssize_t
  734. sdev_store_queue_ramp_up_period(struct device *dev,
  735. struct device_attribute *attr,
  736. const char *buf, size_t count)
  737. {
  738. struct scsi_device *sdev = to_scsi_device(dev);
  739. unsigned long period;
  740. if (strict_strtoul(buf, 10, &period))
  741. return -EINVAL;
  742. sdev->queue_ramp_up_period = msecs_to_jiffies(period);
  743. return period;
  744. }
  745. static struct device_attribute sdev_attr_queue_ramp_up_period =
  746. __ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
  747. sdev_show_queue_ramp_up_period,
  748. sdev_store_queue_ramp_up_period);
  749. static ssize_t
  750. sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
  751. const char *buf, size_t count)
  752. {
  753. struct scsi_device *sdev = to_scsi_device(dev);
  754. struct scsi_host_template *sht = sdev->host->hostt;
  755. int tag_type = 0, retval;
  756. int prev_tag_type = scsi_get_tag_type(sdev);
  757. if (!sdev->tagged_supported || !sht->change_queue_type)
  758. return -EINVAL;
  759. if (strncmp(buf, "ordered", 7) == 0)
  760. tag_type = MSG_ORDERED_TAG;
  761. else if (strncmp(buf, "simple", 6) == 0)
  762. tag_type = MSG_SIMPLE_TAG;
  763. else if (strncmp(buf, "none", 4) != 0)
  764. return -EINVAL;
  765. if (tag_type == prev_tag_type)
  766. return count;
  767. retval = sht->change_queue_type(sdev, tag_type);
  768. if (retval < 0)
  769. return retval;
  770. return count;
  771. }
  772. static int scsi_target_add(struct scsi_target *starget)
  773. {
  774. int error;
  775. if (starget->state != STARGET_CREATED)
  776. return 0;
  777. error = device_add(&starget->dev);
  778. if (error) {
  779. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  780. return error;
  781. }
  782. transport_add_device(&starget->dev);
  783. starget->state = STARGET_RUNNING;
  784. pm_runtime_set_active(&starget->dev);
  785. pm_runtime_enable(&starget->dev);
  786. device_enable_async_suspend(&starget->dev);
  787. return 0;
  788. }
  789. static struct device_attribute sdev_attr_queue_type_rw =
  790. __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  791. sdev_store_queue_type_rw);
  792. /**
  793. * scsi_sysfs_add_sdev - add scsi device to sysfs
  794. * @sdev: scsi_device to add
  795. *
  796. * Return value:
  797. * 0 on Success / non-zero on Failure
  798. **/
  799. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  800. {
  801. int error, i;
  802. struct request_queue *rq = sdev->request_queue;
  803. struct scsi_target *starget = sdev->sdev_target;
  804. error = scsi_device_set_state(sdev, SDEV_RUNNING);
  805. if (error)
  806. return error;
  807. error = scsi_target_add(starget);
  808. if (error)
  809. return error;
  810. transport_configure_device(&starget->dev);
  811. device_enable_async_suspend(&sdev->sdev_gendev);
  812. scsi_autopm_get_target(starget);
  813. pm_runtime_set_active(&sdev->sdev_gendev);
  814. pm_runtime_forbid(&sdev->sdev_gendev);
  815. pm_runtime_enable(&sdev->sdev_gendev);
  816. scsi_autopm_put_target(starget);
  817. /* The following call will keep sdev active indefinitely, until
  818. * its driver does a corresponding scsi_autopm_pm_device(). Only
  819. * drivers supporting autosuspend will do this.
  820. */
  821. scsi_autopm_get_device(sdev);
  822. error = device_add(&sdev->sdev_gendev);
  823. if (error) {
  824. sdev_printk(KERN_INFO, sdev,
  825. "failed to add device: %d\n", error);
  826. return error;
  827. }
  828. device_enable_async_suspend(&sdev->sdev_dev);
  829. error = device_add(&sdev->sdev_dev);
  830. if (error) {
  831. sdev_printk(KERN_INFO, sdev,
  832. "failed to add class device: %d\n", error);
  833. device_del(&sdev->sdev_gendev);
  834. return error;
  835. }
  836. transport_add_device(&sdev->sdev_gendev);
  837. sdev->is_visible = 1;
  838. /* create queue files, which may be writable, depending on the host */
  839. if (sdev->host->hostt->change_queue_depth) {
  840. error = device_create_file(&sdev->sdev_gendev,
  841. &sdev_attr_queue_depth_rw);
  842. error = device_create_file(&sdev->sdev_gendev,
  843. &sdev_attr_queue_ramp_up_period);
  844. }
  845. else
  846. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
  847. if (error)
  848. return error;
  849. if (sdev->host->hostt->change_queue_type)
  850. error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
  851. else
  852. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
  853. if (error)
  854. return error;
  855. error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
  856. if (error)
  857. /* we're treating error on bsg register as non-fatal,
  858. * so pretend nothing went wrong */
  859. sdev_printk(KERN_INFO, sdev,
  860. "Failed to register bsg queue, errno=%d\n", error);
  861. /* add additional host specific attributes */
  862. if (sdev->host->hostt->sdev_attrs) {
  863. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  864. error = device_create_file(&sdev->sdev_gendev,
  865. sdev->host->hostt->sdev_attrs[i]);
  866. if (error)
  867. return error;
  868. }
  869. }
  870. return error;
  871. }
  872. void __scsi_remove_device(struct scsi_device *sdev)
  873. {
  874. struct device *dev = &sdev->sdev_gendev;
  875. if (sdev->is_visible) {
  876. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  877. return;
  878. bsg_unregister_queue(sdev->request_queue);
  879. device_unregister(&sdev->sdev_dev);
  880. transport_remove_device(dev);
  881. device_del(dev);
  882. } else
  883. put_device(&sdev->sdev_dev);
  884. /*
  885. * Stop accepting new requests and wait until all queuecommand() and
  886. * scsi_run_queue() invocations have finished before tearing down the
  887. * device.
  888. */
  889. scsi_device_set_state(sdev, SDEV_DEL);
  890. blk_cleanup_queue(sdev->request_queue);
  891. cancel_work_sync(&sdev->requeue_work);
  892. if (sdev->host->hostt->slave_destroy)
  893. sdev->host->hostt->slave_destroy(sdev);
  894. transport_destroy_device(dev);
  895. put_device(dev);
  896. }
  897. /**
  898. * scsi_remove_device - unregister a device from the scsi bus
  899. * @sdev: scsi_device to unregister
  900. **/
  901. void scsi_remove_device(struct scsi_device *sdev)
  902. {
  903. struct Scsi_Host *shost = sdev->host;
  904. mutex_lock(&shost->scan_mutex);
  905. __scsi_remove_device(sdev);
  906. mutex_unlock(&shost->scan_mutex);
  907. }
  908. EXPORT_SYMBOL(scsi_remove_device);
  909. static void __scsi_remove_target(struct scsi_target *starget)
  910. {
  911. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  912. unsigned long flags;
  913. struct scsi_device *sdev;
  914. spin_lock_irqsave(shost->host_lock, flags);
  915. restart:
  916. list_for_each_entry(sdev, &shost->__devices, siblings) {
  917. if (sdev->channel != starget->channel ||
  918. sdev->id != starget->id ||
  919. scsi_device_get(sdev))
  920. continue;
  921. spin_unlock_irqrestore(shost->host_lock, flags);
  922. scsi_remove_device(sdev);
  923. scsi_device_put(sdev);
  924. spin_lock_irqsave(shost->host_lock, flags);
  925. goto restart;
  926. }
  927. spin_unlock_irqrestore(shost->host_lock, flags);
  928. }
  929. /**
  930. * scsi_remove_target - try to remove a target and all its devices
  931. * @dev: generic starget or parent of generic stargets to be removed
  932. *
  933. * Note: This is slightly racy. It is possible that if the user
  934. * requests the addition of another device then the target won't be
  935. * removed.
  936. */
  937. void scsi_remove_target(struct device *dev)
  938. {
  939. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  940. struct scsi_target *starget, *last = NULL;
  941. unsigned long flags;
  942. /* remove targets being careful to lookup next entry before
  943. * deleting the last
  944. */
  945. spin_lock_irqsave(shost->host_lock, flags);
  946. list_for_each_entry(starget, &shost->__targets, siblings) {
  947. if (starget->state == STARGET_DEL)
  948. continue;
  949. if (starget->dev.parent == dev || &starget->dev == dev) {
  950. /* assuming new targets arrive at the end */
  951. starget->reap_ref++;
  952. spin_unlock_irqrestore(shost->host_lock, flags);
  953. if (last)
  954. scsi_target_reap(last);
  955. last = starget;
  956. __scsi_remove_target(starget);
  957. spin_lock_irqsave(shost->host_lock, flags);
  958. }
  959. }
  960. spin_unlock_irqrestore(shost->host_lock, flags);
  961. if (last)
  962. scsi_target_reap(last);
  963. }
  964. EXPORT_SYMBOL(scsi_remove_target);
  965. int scsi_register_driver(struct device_driver *drv)
  966. {
  967. drv->bus = &scsi_bus_type;
  968. return driver_register(drv);
  969. }
  970. EXPORT_SYMBOL(scsi_register_driver);
  971. int scsi_register_interface(struct class_interface *intf)
  972. {
  973. intf->class = &sdev_class;
  974. return class_interface_register(intf);
  975. }
  976. EXPORT_SYMBOL(scsi_register_interface);
  977. /**
  978. * scsi_sysfs_add_host - add scsi host to subsystem
  979. * @shost: scsi host struct to add to subsystem
  980. **/
  981. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  982. {
  983. int error, i;
  984. /* add host specific attributes */
  985. if (shost->hostt->shost_attrs) {
  986. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  987. error = device_create_file(&shost->shost_dev,
  988. shost->hostt->shost_attrs[i]);
  989. if (error)
  990. return error;
  991. }
  992. }
  993. transport_register_device(&shost->shost_gendev);
  994. transport_configure_device(&shost->shost_gendev);
  995. return 0;
  996. }
  997. static struct device_type scsi_dev_type = {
  998. .name = "scsi_device",
  999. .release = scsi_device_dev_release,
  1000. .groups = scsi_sdev_attr_groups,
  1001. };
  1002. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  1003. {
  1004. unsigned long flags;
  1005. struct Scsi_Host *shost = sdev->host;
  1006. struct scsi_target *starget = sdev->sdev_target;
  1007. device_initialize(&sdev->sdev_gendev);
  1008. sdev->sdev_gendev.bus = &scsi_bus_type;
  1009. sdev->sdev_gendev.type = &scsi_dev_type;
  1010. dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
  1011. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  1012. device_initialize(&sdev->sdev_dev);
  1013. sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
  1014. sdev->sdev_dev.class = &sdev_class;
  1015. dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
  1016. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  1017. sdev->scsi_level = starget->scsi_level;
  1018. transport_setup_device(&sdev->sdev_gendev);
  1019. spin_lock_irqsave(shost->host_lock, flags);
  1020. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  1021. list_add_tail(&sdev->siblings, &shost->__devices);
  1022. spin_unlock_irqrestore(shost->host_lock, flags);
  1023. }
  1024. int scsi_is_sdev_device(const struct device *dev)
  1025. {
  1026. return dev->type == &scsi_dev_type;
  1027. }
  1028. EXPORT_SYMBOL(scsi_is_sdev_device);
  1029. /* A blank transport template that is used in drivers that don't
  1030. * yet implement Transport Attributes */
  1031. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };