scsi_sysfs.c 29 KB

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