scsi_sysfs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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 (type, "%d\n");
  452. sdev_rd_attr (scsi_level, "%d\n");
  453. sdev_rd_attr (vendor, "%.8s\n");
  454. sdev_rd_attr (model, "%.16s\n");
  455. sdev_rd_attr (rev, "%.4s\n");
  456. /*
  457. * TODO: can we make these symlinks to the block layer ones?
  458. */
  459. static ssize_t
  460. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  461. {
  462. struct scsi_device *sdev;
  463. sdev = to_scsi_device(dev);
  464. return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
  465. }
  466. static ssize_t
  467. sdev_store_timeout (struct device *dev, struct device_attribute *attr,
  468. const char *buf, size_t count)
  469. {
  470. struct scsi_device *sdev;
  471. int timeout;
  472. sdev = to_scsi_device(dev);
  473. sscanf (buf, "%d\n", &timeout);
  474. blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
  475. return count;
  476. }
  477. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  478. static ssize_t
  479. sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  480. {
  481. struct scsi_device *sdev;
  482. sdev = to_scsi_device(dev);
  483. return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
  484. }
  485. static ssize_t
  486. sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
  487. const char *buf, size_t count)
  488. {
  489. struct scsi_device *sdev;
  490. unsigned int eh_timeout;
  491. int err;
  492. if (!capable(CAP_SYS_ADMIN))
  493. return -EACCES;
  494. sdev = to_scsi_device(dev);
  495. err = kstrtouint(buf, 10, &eh_timeout);
  496. if (err)
  497. return err;
  498. sdev->eh_timeout = eh_timeout * HZ;
  499. return count;
  500. }
  501. static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
  502. static ssize_t
  503. store_rescan_field (struct device *dev, struct device_attribute *attr,
  504. const char *buf, size_t count)
  505. {
  506. scsi_rescan_device(dev);
  507. return count;
  508. }
  509. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  510. static void sdev_store_delete_callback(struct device *dev)
  511. {
  512. scsi_remove_device(to_scsi_device(dev));
  513. }
  514. static ssize_t
  515. sdev_store_delete(struct device *dev, struct device_attribute *attr,
  516. const char *buf, size_t count)
  517. {
  518. int rc;
  519. /* An attribute cannot be unregistered by one of its own methods,
  520. * so we have to use this roundabout approach.
  521. */
  522. rc = device_schedule_callback(dev, sdev_store_delete_callback);
  523. if (rc)
  524. count = rc;
  525. return count;
  526. };
  527. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  528. static ssize_t
  529. store_state_field(struct device *dev, struct device_attribute *attr,
  530. const char *buf, size_t count)
  531. {
  532. int i;
  533. struct scsi_device *sdev = to_scsi_device(dev);
  534. enum scsi_device_state state = 0;
  535. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  536. const int len = strlen(sdev_states[i].name);
  537. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  538. buf[len] == '\n') {
  539. state = sdev_states[i].value;
  540. break;
  541. }
  542. }
  543. if (!state)
  544. return -EINVAL;
  545. if (scsi_device_set_state(sdev, state))
  546. return -EINVAL;
  547. return count;
  548. }
  549. static ssize_t
  550. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  551. {
  552. struct scsi_device *sdev = to_scsi_device(dev);
  553. const char *name = scsi_device_state_name(sdev->sdev_state);
  554. if (!name)
  555. return -EINVAL;
  556. return snprintf(buf, 20, "%s\n", name);
  557. }
  558. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  559. static ssize_t
  560. show_queue_type_field(struct device *dev, struct device_attribute *attr,
  561. char *buf)
  562. {
  563. struct scsi_device *sdev = to_scsi_device(dev);
  564. const char *name = "none";
  565. if (sdev->ordered_tags)
  566. name = "ordered";
  567. else if (sdev->simple_tags)
  568. name = "simple";
  569. return snprintf(buf, 20, "%s\n", name);
  570. }
  571. static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
  572. static ssize_t
  573. show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
  574. {
  575. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  576. }
  577. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  578. #define show_sdev_iostat(field) \
  579. static ssize_t \
  580. show_iostat_##field(struct device *dev, struct device_attribute *attr, \
  581. char *buf) \
  582. { \
  583. struct scsi_device *sdev = to_scsi_device(dev); \
  584. unsigned long long count = atomic_read(&sdev->field); \
  585. return snprintf(buf, 20, "0x%llx\n", count); \
  586. } \
  587. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  588. show_sdev_iostat(iorequest_cnt);
  589. show_sdev_iostat(iodone_cnt);
  590. show_sdev_iostat(ioerr_cnt);
  591. static ssize_t
  592. sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  593. {
  594. struct scsi_device *sdev;
  595. sdev = to_scsi_device(dev);
  596. return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
  597. }
  598. static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
  599. #define DECLARE_EVT_SHOW(name, Cap_name) \
  600. static ssize_t \
  601. sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
  602. char *buf) \
  603. { \
  604. struct scsi_device *sdev = to_scsi_device(dev); \
  605. int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
  606. return snprintf(buf, 20, "%d\n", val); \
  607. }
  608. #define DECLARE_EVT_STORE(name, Cap_name) \
  609. static ssize_t \
  610. sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
  611. const char *buf, size_t count) \
  612. { \
  613. struct scsi_device *sdev = to_scsi_device(dev); \
  614. int val = simple_strtoul(buf, NULL, 0); \
  615. if (val == 0) \
  616. clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  617. else if (val == 1) \
  618. set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  619. else \
  620. return -EINVAL; \
  621. return count; \
  622. }
  623. #define DECLARE_EVT(name, Cap_name) \
  624. DECLARE_EVT_SHOW(name, Cap_name) \
  625. DECLARE_EVT_STORE(name, Cap_name) \
  626. static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
  627. sdev_store_evt_##name);
  628. #define REF_EVT(name) &dev_attr_evt_##name.attr
  629. DECLARE_EVT(media_change, MEDIA_CHANGE)
  630. /* Default template for device attributes. May NOT be modified */
  631. static struct attribute *scsi_sdev_attrs[] = {
  632. &dev_attr_device_blocked.attr,
  633. &dev_attr_type.attr,
  634. &dev_attr_scsi_level.attr,
  635. &dev_attr_vendor.attr,
  636. &dev_attr_model.attr,
  637. &dev_attr_rev.attr,
  638. &dev_attr_rescan.attr,
  639. &dev_attr_delete.attr,
  640. &dev_attr_state.attr,
  641. &dev_attr_timeout.attr,
  642. &dev_attr_eh_timeout.attr,
  643. &dev_attr_iocounterbits.attr,
  644. &dev_attr_iorequest_cnt.attr,
  645. &dev_attr_iodone_cnt.attr,
  646. &dev_attr_ioerr_cnt.attr,
  647. &dev_attr_modalias.attr,
  648. REF_EVT(media_change),
  649. NULL
  650. };
  651. static struct attribute_group scsi_sdev_attr_group = {
  652. .attrs = scsi_sdev_attrs,
  653. };
  654. static const struct attribute_group *scsi_sdev_attr_groups[] = {
  655. &scsi_sdev_attr_group,
  656. NULL
  657. };
  658. static ssize_t
  659. sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
  660. const char *buf, size_t count)
  661. {
  662. int depth, retval;
  663. struct scsi_device *sdev = to_scsi_device(dev);
  664. struct scsi_host_template *sht = sdev->host->hostt;
  665. if (!sht->change_queue_depth)
  666. return -EINVAL;
  667. depth = simple_strtoul(buf, NULL, 0);
  668. if (depth < 1)
  669. return -EINVAL;
  670. retval = sht->change_queue_depth(sdev, depth,
  671. SCSI_QDEPTH_DEFAULT);
  672. if (retval < 0)
  673. return retval;
  674. sdev->max_queue_depth = sdev->queue_depth;
  675. return count;
  676. }
  677. static struct device_attribute sdev_attr_queue_depth_rw =
  678. __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  679. sdev_store_queue_depth_rw);
  680. static ssize_t
  681. sdev_show_queue_ramp_up_period(struct device *dev,
  682. struct device_attribute *attr,
  683. char *buf)
  684. {
  685. struct scsi_device *sdev;
  686. sdev = to_scsi_device(dev);
  687. return snprintf(buf, 20, "%u\n",
  688. jiffies_to_msecs(sdev->queue_ramp_up_period));
  689. }
  690. static ssize_t
  691. sdev_store_queue_ramp_up_period(struct device *dev,
  692. struct device_attribute *attr,
  693. const char *buf, size_t count)
  694. {
  695. struct scsi_device *sdev = to_scsi_device(dev);
  696. unsigned long period;
  697. if (strict_strtoul(buf, 10, &period))
  698. return -EINVAL;
  699. sdev->queue_ramp_up_period = msecs_to_jiffies(period);
  700. return period;
  701. }
  702. static struct device_attribute sdev_attr_queue_ramp_up_period =
  703. __ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
  704. sdev_show_queue_ramp_up_period,
  705. sdev_store_queue_ramp_up_period);
  706. static ssize_t
  707. sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
  708. const char *buf, size_t count)
  709. {
  710. struct scsi_device *sdev = to_scsi_device(dev);
  711. struct scsi_host_template *sht = sdev->host->hostt;
  712. int tag_type = 0, retval;
  713. int prev_tag_type = scsi_get_tag_type(sdev);
  714. if (!sdev->tagged_supported || !sht->change_queue_type)
  715. return -EINVAL;
  716. if (strncmp(buf, "ordered", 7) == 0)
  717. tag_type = MSG_ORDERED_TAG;
  718. else if (strncmp(buf, "simple", 6) == 0)
  719. tag_type = MSG_SIMPLE_TAG;
  720. else if (strncmp(buf, "none", 4) != 0)
  721. return -EINVAL;
  722. if (tag_type == prev_tag_type)
  723. return count;
  724. retval = sht->change_queue_type(sdev, tag_type);
  725. if (retval < 0)
  726. return retval;
  727. return count;
  728. }
  729. static int scsi_target_add(struct scsi_target *starget)
  730. {
  731. int error;
  732. if (starget->state != STARGET_CREATED)
  733. return 0;
  734. error = device_add(&starget->dev);
  735. if (error) {
  736. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  737. return error;
  738. }
  739. transport_add_device(&starget->dev);
  740. starget->state = STARGET_RUNNING;
  741. pm_runtime_set_active(&starget->dev);
  742. pm_runtime_enable(&starget->dev);
  743. device_enable_async_suspend(&starget->dev);
  744. return 0;
  745. }
  746. static struct device_attribute sdev_attr_queue_type_rw =
  747. __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  748. sdev_store_queue_type_rw);
  749. /**
  750. * scsi_sysfs_add_sdev - add scsi device to sysfs
  751. * @sdev: scsi_device to add
  752. *
  753. * Return value:
  754. * 0 on Success / non-zero on Failure
  755. **/
  756. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  757. {
  758. int error, i;
  759. struct request_queue *rq = sdev->request_queue;
  760. struct scsi_target *starget = sdev->sdev_target;
  761. error = scsi_device_set_state(sdev, SDEV_RUNNING);
  762. if (error)
  763. return error;
  764. error = scsi_target_add(starget);
  765. if (error)
  766. return error;
  767. transport_configure_device(&starget->dev);
  768. device_enable_async_suspend(&sdev->sdev_gendev);
  769. scsi_autopm_get_target(starget);
  770. pm_runtime_set_active(&sdev->sdev_gendev);
  771. pm_runtime_forbid(&sdev->sdev_gendev);
  772. pm_runtime_enable(&sdev->sdev_gendev);
  773. scsi_autopm_put_target(starget);
  774. /* The following call will keep sdev active indefinitely, until
  775. * its driver does a corresponding scsi_autopm_pm_device(). Only
  776. * drivers supporting autosuspend will do this.
  777. */
  778. scsi_autopm_get_device(sdev);
  779. error = device_add(&sdev->sdev_gendev);
  780. if (error) {
  781. sdev_printk(KERN_INFO, sdev,
  782. "failed to add device: %d\n", error);
  783. return error;
  784. }
  785. device_enable_async_suspend(&sdev->sdev_dev);
  786. error = device_add(&sdev->sdev_dev);
  787. if (error) {
  788. sdev_printk(KERN_INFO, sdev,
  789. "failed to add class device: %d\n", error);
  790. device_del(&sdev->sdev_gendev);
  791. return error;
  792. }
  793. transport_add_device(&sdev->sdev_gendev);
  794. sdev->is_visible = 1;
  795. /* create queue files, which may be writable, depending on the host */
  796. if (sdev->host->hostt->change_queue_depth) {
  797. error = device_create_file(&sdev->sdev_gendev,
  798. &sdev_attr_queue_depth_rw);
  799. error = device_create_file(&sdev->sdev_gendev,
  800. &sdev_attr_queue_ramp_up_period);
  801. }
  802. else
  803. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
  804. if (error)
  805. return error;
  806. if (sdev->host->hostt->change_queue_type)
  807. error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
  808. else
  809. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
  810. if (error)
  811. return error;
  812. error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
  813. if (error)
  814. /* we're treating error on bsg register as non-fatal,
  815. * so pretend nothing went wrong */
  816. sdev_printk(KERN_INFO, sdev,
  817. "Failed to register bsg queue, errno=%d\n", error);
  818. /* add additional host specific attributes */
  819. if (sdev->host->hostt->sdev_attrs) {
  820. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  821. error = device_create_file(&sdev->sdev_gendev,
  822. sdev->host->hostt->sdev_attrs[i]);
  823. if (error)
  824. return error;
  825. }
  826. }
  827. return error;
  828. }
  829. void __scsi_remove_device(struct scsi_device *sdev)
  830. {
  831. struct device *dev = &sdev->sdev_gendev;
  832. if (sdev->is_visible) {
  833. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  834. return;
  835. bsg_unregister_queue(sdev->request_queue);
  836. device_unregister(&sdev->sdev_dev);
  837. transport_remove_device(dev);
  838. device_del(dev);
  839. } else
  840. put_device(&sdev->sdev_dev);
  841. /*
  842. * Stop accepting new requests and wait until all queuecommand() and
  843. * scsi_run_queue() invocations have finished before tearing down the
  844. * device.
  845. */
  846. scsi_device_set_state(sdev, SDEV_DEL);
  847. blk_cleanup_queue(sdev->request_queue);
  848. cancel_work_sync(&sdev->requeue_work);
  849. if (sdev->host->hostt->slave_destroy)
  850. sdev->host->hostt->slave_destroy(sdev);
  851. transport_destroy_device(dev);
  852. put_device(dev);
  853. }
  854. /**
  855. * scsi_remove_device - unregister a device from the scsi bus
  856. * @sdev: scsi_device to unregister
  857. **/
  858. void scsi_remove_device(struct scsi_device *sdev)
  859. {
  860. struct Scsi_Host *shost = sdev->host;
  861. mutex_lock(&shost->scan_mutex);
  862. __scsi_remove_device(sdev);
  863. mutex_unlock(&shost->scan_mutex);
  864. }
  865. EXPORT_SYMBOL(scsi_remove_device);
  866. static void __scsi_remove_target(struct scsi_target *starget)
  867. {
  868. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  869. unsigned long flags;
  870. struct scsi_device *sdev;
  871. spin_lock_irqsave(shost->host_lock, flags);
  872. restart:
  873. list_for_each_entry(sdev, &shost->__devices, siblings) {
  874. if (sdev->channel != starget->channel ||
  875. sdev->id != starget->id ||
  876. scsi_device_get(sdev))
  877. continue;
  878. spin_unlock_irqrestore(shost->host_lock, flags);
  879. scsi_remove_device(sdev);
  880. scsi_device_put(sdev);
  881. spin_lock_irqsave(shost->host_lock, flags);
  882. goto restart;
  883. }
  884. spin_unlock_irqrestore(shost->host_lock, flags);
  885. }
  886. /**
  887. * scsi_remove_target - try to remove a target and all its devices
  888. * @dev: generic starget or parent of generic stargets to be removed
  889. *
  890. * Note: This is slightly racy. It is possible that if the user
  891. * requests the addition of another device then the target won't be
  892. * removed.
  893. */
  894. void scsi_remove_target(struct device *dev)
  895. {
  896. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  897. struct scsi_target *starget, *last = NULL;
  898. unsigned long flags;
  899. /* remove targets being careful to lookup next entry before
  900. * deleting the last
  901. */
  902. spin_lock_irqsave(shost->host_lock, flags);
  903. list_for_each_entry(starget, &shost->__targets, siblings) {
  904. if (starget->state == STARGET_DEL)
  905. continue;
  906. if (starget->dev.parent == dev || &starget->dev == dev) {
  907. /* assuming new targets arrive at the end */
  908. starget->reap_ref++;
  909. spin_unlock_irqrestore(shost->host_lock, flags);
  910. if (last)
  911. scsi_target_reap(last);
  912. last = starget;
  913. __scsi_remove_target(starget);
  914. spin_lock_irqsave(shost->host_lock, flags);
  915. }
  916. }
  917. spin_unlock_irqrestore(shost->host_lock, flags);
  918. if (last)
  919. scsi_target_reap(last);
  920. }
  921. EXPORT_SYMBOL(scsi_remove_target);
  922. int scsi_register_driver(struct device_driver *drv)
  923. {
  924. drv->bus = &scsi_bus_type;
  925. return driver_register(drv);
  926. }
  927. EXPORT_SYMBOL(scsi_register_driver);
  928. int scsi_register_interface(struct class_interface *intf)
  929. {
  930. intf->class = &sdev_class;
  931. return class_interface_register(intf);
  932. }
  933. EXPORT_SYMBOL(scsi_register_interface);
  934. /**
  935. * scsi_sysfs_add_host - add scsi host to subsystem
  936. * @shost: scsi host struct to add to subsystem
  937. **/
  938. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  939. {
  940. int error, i;
  941. /* add host specific attributes */
  942. if (shost->hostt->shost_attrs) {
  943. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  944. error = device_create_file(&shost->shost_dev,
  945. shost->hostt->shost_attrs[i]);
  946. if (error)
  947. return error;
  948. }
  949. }
  950. transport_register_device(&shost->shost_gendev);
  951. transport_configure_device(&shost->shost_gendev);
  952. return 0;
  953. }
  954. static struct device_type scsi_dev_type = {
  955. .name = "scsi_device",
  956. .release = scsi_device_dev_release,
  957. .groups = scsi_sdev_attr_groups,
  958. };
  959. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  960. {
  961. unsigned long flags;
  962. struct Scsi_Host *shost = sdev->host;
  963. struct scsi_target *starget = sdev->sdev_target;
  964. device_initialize(&sdev->sdev_gendev);
  965. sdev->sdev_gendev.bus = &scsi_bus_type;
  966. sdev->sdev_gendev.type = &scsi_dev_type;
  967. dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
  968. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  969. device_initialize(&sdev->sdev_dev);
  970. sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
  971. sdev->sdev_dev.class = &sdev_class;
  972. dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
  973. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  974. sdev->scsi_level = starget->scsi_level;
  975. transport_setup_device(&sdev->sdev_gendev);
  976. spin_lock_irqsave(shost->host_lock, flags);
  977. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  978. list_add_tail(&sdev->siblings, &shost->__devices);
  979. spin_unlock_irqrestore(shost->host_lock, flags);
  980. }
  981. int scsi_is_sdev_device(const struct device *dev)
  982. {
  983. return dev->type == &scsi_dev_type;
  984. }
  985. EXPORT_SYMBOL(scsi_is_sdev_device);
  986. /* A blank transport template that is used in drivers that don't
  987. * yet implement Transport Attributes */
  988. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };