scsi_sysfs.c 30 KB

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