scsi_sysfs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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_BLOCK, "blocked" },
  34. { SDEV_CREATED_BLOCK, "created-blocked" },
  35. };
  36. const char *scsi_device_state_name(enum scsi_device_state state)
  37. {
  38. int i;
  39. char *name = NULL;
  40. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  41. if (sdev_states[i].value == state) {
  42. name = sdev_states[i].name;
  43. break;
  44. }
  45. }
  46. return name;
  47. }
  48. static const struct {
  49. enum scsi_host_state value;
  50. char *name;
  51. } shost_states[] = {
  52. { SHOST_CREATED, "created" },
  53. { SHOST_RUNNING, "running" },
  54. { SHOST_CANCEL, "cancel" },
  55. { SHOST_DEL, "deleted" },
  56. { SHOST_RECOVERY, "recovery" },
  57. { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
  58. { SHOST_DEL_RECOVERY, "deleted/recovery", },
  59. };
  60. const char *scsi_host_state_name(enum scsi_host_state state)
  61. {
  62. int i;
  63. char *name = NULL;
  64. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  65. if (shost_states[i].value == state) {
  66. name = shost_states[i].name;
  67. break;
  68. }
  69. }
  70. return name;
  71. }
  72. static int check_set(unsigned int *val, char *src)
  73. {
  74. char *last;
  75. if (strncmp(src, "-", 20) == 0) {
  76. *val = SCAN_WILD_CARD;
  77. } else {
  78. /*
  79. * Doesn't check for int overflow
  80. */
  81. *val = simple_strtoul(src, &last, 0);
  82. if (*last != '\0')
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. static int scsi_scan(struct Scsi_Host *shost, const char *str)
  88. {
  89. char s1[15], s2[15], s3[15], junk;
  90. unsigned int channel, id, lun;
  91. int res;
  92. res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
  93. if (res != 3)
  94. return -EINVAL;
  95. if (check_set(&channel, s1))
  96. return -EINVAL;
  97. if (check_set(&id, s2))
  98. return -EINVAL;
  99. if (check_set(&lun, s3))
  100. return -EINVAL;
  101. if (shost->transportt->user_scan)
  102. res = shost->transportt->user_scan(shost, channel, id, lun);
  103. else
  104. res = scsi_scan_host_selected(shost, channel, id, lun, 1);
  105. return res;
  106. }
  107. /*
  108. * shost_show_function: macro to create an attr function that can be used to
  109. * show a non-bit field.
  110. */
  111. #define shost_show_function(name, field, format_string) \
  112. static ssize_t \
  113. show_##name (struct device *dev, struct device_attribute *attr, \
  114. char *buf) \
  115. { \
  116. struct Scsi_Host *shost = class_to_shost(dev); \
  117. return snprintf (buf, 20, format_string, shost->field); \
  118. }
  119. /*
  120. * shost_rd_attr: macro to create a function and attribute variable for a
  121. * read only field.
  122. */
  123. #define shost_rd_attr2(name, field, format_string) \
  124. shost_show_function(name, field, format_string) \
  125. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  126. #define shost_rd_attr(field, format_string) \
  127. shost_rd_attr2(field, field, format_string)
  128. /*
  129. * Create the actual show/store functions and data structures.
  130. */
  131. static ssize_t
  132. store_scan(struct device *dev, struct device_attribute *attr,
  133. const char *buf, size_t count)
  134. {
  135. struct Scsi_Host *shost = class_to_shost(dev);
  136. int res;
  137. res = scsi_scan(shost, buf);
  138. if (res == 0)
  139. res = count;
  140. return res;
  141. };
  142. static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
  143. static ssize_t
  144. store_shost_state(struct device *dev, struct device_attribute *attr,
  145. const char *buf, size_t count)
  146. {
  147. int i;
  148. struct Scsi_Host *shost = class_to_shost(dev);
  149. enum scsi_host_state state = 0;
  150. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  151. const int len = strlen(shost_states[i].name);
  152. if (strncmp(shost_states[i].name, buf, len) == 0 &&
  153. buf[len] == '\n') {
  154. state = shost_states[i].value;
  155. break;
  156. }
  157. }
  158. if (!state)
  159. return -EINVAL;
  160. if (scsi_host_set_state(shost, state))
  161. return -EINVAL;
  162. return count;
  163. }
  164. static ssize_t
  165. show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
  166. {
  167. struct Scsi_Host *shost = class_to_shost(dev);
  168. const char *name = scsi_host_state_name(shost->shost_state);
  169. if (!name)
  170. return -EINVAL;
  171. return snprintf(buf, 20, "%s\n", name);
  172. }
  173. /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
  174. struct device_attribute dev_attr_hstate =
  175. __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
  176. static ssize_t
  177. show_shost_mode(unsigned int mode, char *buf)
  178. {
  179. ssize_t len = 0;
  180. if (mode & MODE_INITIATOR)
  181. len = sprintf(buf, "%s", "Initiator");
  182. if (mode & MODE_TARGET)
  183. len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
  184. len += sprintf(buf + len, "\n");
  185. return len;
  186. }
  187. static ssize_t
  188. show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
  189. char *buf)
  190. {
  191. struct Scsi_Host *shost = class_to_shost(dev);
  192. unsigned int supported_mode = shost->hostt->supported_mode;
  193. if (supported_mode == MODE_UNKNOWN)
  194. /* by default this should be initiator */
  195. supported_mode = MODE_INITIATOR;
  196. return show_shost_mode(supported_mode, buf);
  197. }
  198. static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
  199. static ssize_t
  200. show_shost_active_mode(struct device *dev,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. struct Scsi_Host *shost = class_to_shost(dev);
  204. if (shost->active_mode == MODE_UNKNOWN)
  205. return snprintf(buf, 20, "unknown\n");
  206. else
  207. return show_shost_mode(shost->active_mode, buf);
  208. }
  209. static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
  210. shost_rd_attr(unique_id, "%u\n");
  211. shost_rd_attr(host_busy, "%hu\n");
  212. shost_rd_attr(cmd_per_lun, "%hd\n");
  213. shost_rd_attr(can_queue, "%hd\n");
  214. shost_rd_attr(sg_tablesize, "%hu\n");
  215. shost_rd_attr(sg_prot_tablesize, "%hu\n");
  216. shost_rd_attr(unchecked_isa_dma, "%d\n");
  217. shost_rd_attr(prot_capabilities, "%u\n");
  218. shost_rd_attr(prot_guard_type, "%hd\n");
  219. shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
  220. static struct attribute *scsi_sysfs_shost_attrs[] = {
  221. &dev_attr_unique_id.attr,
  222. &dev_attr_host_busy.attr,
  223. &dev_attr_cmd_per_lun.attr,
  224. &dev_attr_can_queue.attr,
  225. &dev_attr_sg_tablesize.attr,
  226. &dev_attr_sg_prot_tablesize.attr,
  227. &dev_attr_unchecked_isa_dma.attr,
  228. &dev_attr_proc_name.attr,
  229. &dev_attr_scan.attr,
  230. &dev_attr_hstate.attr,
  231. &dev_attr_supported_mode.attr,
  232. &dev_attr_active_mode.attr,
  233. &dev_attr_prot_capabilities.attr,
  234. &dev_attr_prot_guard_type.attr,
  235. NULL
  236. };
  237. struct attribute_group scsi_shost_attr_group = {
  238. .attrs = scsi_sysfs_shost_attrs,
  239. };
  240. const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
  241. &scsi_shost_attr_group,
  242. NULL
  243. };
  244. static void scsi_device_cls_release(struct device *class_dev)
  245. {
  246. struct scsi_device *sdev;
  247. sdev = class_to_sdev(class_dev);
  248. put_device(&sdev->sdev_gendev);
  249. }
  250. static void scsi_device_dev_release_usercontext(struct work_struct *work)
  251. {
  252. struct scsi_device *sdev;
  253. struct device *parent;
  254. struct scsi_target *starget;
  255. struct list_head *this, *tmp;
  256. unsigned long flags;
  257. sdev = container_of(work, struct scsi_device, ew.work);
  258. parent = sdev->sdev_gendev.parent;
  259. starget = to_scsi_target(parent);
  260. spin_lock_irqsave(sdev->host->host_lock, flags);
  261. starget->reap_ref++;
  262. list_del(&sdev->siblings);
  263. list_del(&sdev->same_target_siblings);
  264. list_del(&sdev->starved_entry);
  265. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  266. cancel_work_sync(&sdev->event_work);
  267. list_for_each_safe(this, tmp, &sdev->event_list) {
  268. struct scsi_event *evt;
  269. evt = list_entry(this, struct scsi_event, node);
  270. list_del(&evt->node);
  271. kfree(evt);
  272. }
  273. blk_put_queue(sdev->request_queue);
  274. /* NULL queue means the device can't be used */
  275. sdev->request_queue = NULL;
  276. scsi_target_reap(scsi_target(sdev));
  277. kfree(sdev->inquiry);
  278. kfree(sdev);
  279. if (parent)
  280. put_device(parent);
  281. }
  282. static void scsi_device_dev_release(struct device *dev)
  283. {
  284. struct scsi_device *sdp = to_scsi_device(dev);
  285. execute_in_process_context(scsi_device_dev_release_usercontext,
  286. &sdp->ew);
  287. }
  288. static struct class sdev_class = {
  289. .name = "scsi_device",
  290. .dev_release = scsi_device_cls_release,
  291. };
  292. /* all probing is done in the individual ->probe routines */
  293. static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
  294. {
  295. struct scsi_device *sdp;
  296. if (dev->type != &scsi_dev_type)
  297. return 0;
  298. sdp = to_scsi_device(dev);
  299. if (sdp->no_uld_attach)
  300. return 0;
  301. return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
  302. }
  303. static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  304. {
  305. struct scsi_device *sdev;
  306. if (dev->type != &scsi_dev_type)
  307. return 0;
  308. sdev = to_scsi_device(dev);
  309. add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
  310. return 0;
  311. }
  312. struct bus_type scsi_bus_type = {
  313. .name = "scsi",
  314. .match = scsi_bus_match,
  315. .uevent = scsi_bus_uevent,
  316. #ifdef CONFIG_PM
  317. .pm = &scsi_bus_pm_ops,
  318. #endif
  319. };
  320. EXPORT_SYMBOL_GPL(scsi_bus_type);
  321. int scsi_sysfs_register(void)
  322. {
  323. int error;
  324. error = bus_register(&scsi_bus_type);
  325. if (!error) {
  326. error = class_register(&sdev_class);
  327. if (error)
  328. bus_unregister(&scsi_bus_type);
  329. }
  330. return error;
  331. }
  332. void scsi_sysfs_unregister(void)
  333. {
  334. class_unregister(&sdev_class);
  335. bus_unregister(&scsi_bus_type);
  336. }
  337. /*
  338. * sdev_show_function: macro to create an attr function that can be used to
  339. * show a non-bit field.
  340. */
  341. #define sdev_show_function(field, format_string) \
  342. static ssize_t \
  343. sdev_show_##field (struct device *dev, struct device_attribute *attr, \
  344. char *buf) \
  345. { \
  346. struct scsi_device *sdev; \
  347. sdev = to_scsi_device(dev); \
  348. return snprintf (buf, 20, format_string, sdev->field); \
  349. } \
  350. /*
  351. * sdev_rd_attr: macro to create a function and attribute variable for a
  352. * read only field.
  353. */
  354. #define sdev_rd_attr(field, format_string) \
  355. sdev_show_function(field, format_string) \
  356. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  357. /*
  358. * sdev_rw_attr: create a function and attribute variable for a
  359. * read/write field.
  360. */
  361. #define sdev_rw_attr(field, format_string) \
  362. sdev_show_function(field, format_string) \
  363. \
  364. static ssize_t \
  365. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  366. const char *buf, size_t count) \
  367. { \
  368. struct scsi_device *sdev; \
  369. sdev = to_scsi_device(dev); \
  370. sscanf (buf, format_string, &sdev->field); \
  371. return count; \
  372. } \
  373. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  374. /* Currently we don't export bit fields, but we might in future,
  375. * so leave this code in */
  376. #if 0
  377. /*
  378. * sdev_rd_attr: create a function and attribute variable for a
  379. * read/write bit field.
  380. */
  381. #define sdev_rw_attr_bit(field) \
  382. sdev_show_function(field, "%d\n") \
  383. \
  384. static ssize_t \
  385. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  386. const char *buf, size_t count) \
  387. { \
  388. int ret; \
  389. struct scsi_device *sdev; \
  390. ret = scsi_sdev_check_buf_bit(buf); \
  391. if (ret >= 0) { \
  392. sdev = to_scsi_device(dev); \
  393. sdev->field = ret; \
  394. ret = count; \
  395. } \
  396. return ret; \
  397. } \
  398. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  399. /*
  400. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  401. * else return -EINVAL.
  402. */
  403. static int scsi_sdev_check_buf_bit(const char *buf)
  404. {
  405. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  406. if (buf[0] == '1')
  407. return 1;
  408. else if (buf[0] == '0')
  409. return 0;
  410. else
  411. return -EINVAL;
  412. } else
  413. return -EINVAL;
  414. }
  415. #endif
  416. /*
  417. * Create the actual show/store functions and data structures.
  418. */
  419. sdev_rd_attr (device_blocked, "%d\n");
  420. sdev_rd_attr (queue_depth, "%d\n");
  421. sdev_rd_attr (type, "%d\n");
  422. sdev_rd_attr (scsi_level, "%d\n");
  423. sdev_rd_attr (vendor, "%.8s\n");
  424. sdev_rd_attr (model, "%.16s\n");
  425. sdev_rd_attr (rev, "%.4s\n");
  426. /*
  427. * TODO: can we make these symlinks to the block layer ones?
  428. */
  429. static ssize_t
  430. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  431. {
  432. struct scsi_device *sdev;
  433. sdev = to_scsi_device(dev);
  434. return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
  435. }
  436. static ssize_t
  437. sdev_store_timeout (struct device *dev, struct device_attribute *attr,
  438. const char *buf, size_t count)
  439. {
  440. struct scsi_device *sdev;
  441. int timeout;
  442. sdev = to_scsi_device(dev);
  443. sscanf (buf, "%d\n", &timeout);
  444. blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
  445. return count;
  446. }
  447. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  448. static ssize_t
  449. store_rescan_field (struct device *dev, struct device_attribute *attr,
  450. const char *buf, size_t count)
  451. {
  452. scsi_rescan_device(dev);
  453. return count;
  454. }
  455. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  456. static void sdev_store_delete_callback(struct device *dev)
  457. {
  458. scsi_remove_device(to_scsi_device(dev));
  459. }
  460. static ssize_t
  461. sdev_store_delete(struct device *dev, struct device_attribute *attr,
  462. const char *buf, size_t count)
  463. {
  464. int rc;
  465. /* An attribute cannot be unregistered by one of its own methods,
  466. * so we have to use this roundabout approach.
  467. */
  468. rc = device_schedule_callback(dev, sdev_store_delete_callback);
  469. if (rc)
  470. count = rc;
  471. return count;
  472. };
  473. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  474. static ssize_t
  475. store_state_field(struct device *dev, struct device_attribute *attr,
  476. const char *buf, size_t count)
  477. {
  478. int i;
  479. struct scsi_device *sdev = to_scsi_device(dev);
  480. enum scsi_device_state state = 0;
  481. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  482. const int len = strlen(sdev_states[i].name);
  483. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  484. buf[len] == '\n') {
  485. state = sdev_states[i].value;
  486. break;
  487. }
  488. }
  489. if (!state)
  490. return -EINVAL;
  491. if (scsi_device_set_state(sdev, state))
  492. return -EINVAL;
  493. return count;
  494. }
  495. static ssize_t
  496. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  497. {
  498. struct scsi_device *sdev = to_scsi_device(dev);
  499. const char *name = scsi_device_state_name(sdev->sdev_state);
  500. if (!name)
  501. return -EINVAL;
  502. return snprintf(buf, 20, "%s\n", name);
  503. }
  504. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  505. static ssize_t
  506. show_queue_type_field(struct device *dev, struct device_attribute *attr,
  507. char *buf)
  508. {
  509. struct scsi_device *sdev = to_scsi_device(dev);
  510. const char *name = "none";
  511. if (sdev->ordered_tags)
  512. name = "ordered";
  513. else if (sdev->simple_tags)
  514. name = "simple";
  515. return snprintf(buf, 20, "%s\n", name);
  516. }
  517. static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
  518. static ssize_t
  519. show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
  520. {
  521. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  522. }
  523. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  524. #define show_sdev_iostat(field) \
  525. static ssize_t \
  526. show_iostat_##field(struct device *dev, struct device_attribute *attr, \
  527. char *buf) \
  528. { \
  529. struct scsi_device *sdev = to_scsi_device(dev); \
  530. unsigned long long count = atomic_read(&sdev->field); \
  531. return snprintf(buf, 20, "0x%llx\n", count); \
  532. } \
  533. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  534. show_sdev_iostat(iorequest_cnt);
  535. show_sdev_iostat(iodone_cnt);
  536. show_sdev_iostat(ioerr_cnt);
  537. static ssize_t
  538. sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  539. {
  540. struct scsi_device *sdev;
  541. sdev = to_scsi_device(dev);
  542. return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
  543. }
  544. static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
  545. #define DECLARE_EVT_SHOW(name, Cap_name) \
  546. static ssize_t \
  547. sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
  548. char *buf) \
  549. { \
  550. struct scsi_device *sdev = to_scsi_device(dev); \
  551. int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
  552. return snprintf(buf, 20, "%d\n", val); \
  553. }
  554. #define DECLARE_EVT_STORE(name, Cap_name) \
  555. static ssize_t \
  556. sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
  557. const char *buf, size_t count) \
  558. { \
  559. struct scsi_device *sdev = to_scsi_device(dev); \
  560. int val = simple_strtoul(buf, NULL, 0); \
  561. if (val == 0) \
  562. clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  563. else if (val == 1) \
  564. set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  565. else \
  566. return -EINVAL; \
  567. return count; \
  568. }
  569. #define DECLARE_EVT(name, Cap_name) \
  570. DECLARE_EVT_SHOW(name, Cap_name) \
  571. DECLARE_EVT_STORE(name, Cap_name) \
  572. static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
  573. sdev_store_evt_##name);
  574. #define REF_EVT(name) &dev_attr_evt_##name.attr
  575. DECLARE_EVT(media_change, MEDIA_CHANGE)
  576. /* Default template for device attributes. May NOT be modified */
  577. static struct attribute *scsi_sdev_attrs[] = {
  578. &dev_attr_device_blocked.attr,
  579. &dev_attr_type.attr,
  580. &dev_attr_scsi_level.attr,
  581. &dev_attr_vendor.attr,
  582. &dev_attr_model.attr,
  583. &dev_attr_rev.attr,
  584. &dev_attr_rescan.attr,
  585. &dev_attr_delete.attr,
  586. &dev_attr_state.attr,
  587. &dev_attr_timeout.attr,
  588. &dev_attr_iocounterbits.attr,
  589. &dev_attr_iorequest_cnt.attr,
  590. &dev_attr_iodone_cnt.attr,
  591. &dev_attr_ioerr_cnt.attr,
  592. &dev_attr_modalias.attr,
  593. REF_EVT(media_change),
  594. NULL
  595. };
  596. static struct attribute_group scsi_sdev_attr_group = {
  597. .attrs = scsi_sdev_attrs,
  598. };
  599. static const struct attribute_group *scsi_sdev_attr_groups[] = {
  600. &scsi_sdev_attr_group,
  601. NULL
  602. };
  603. static ssize_t
  604. sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
  605. const char *buf, size_t count)
  606. {
  607. int depth, retval;
  608. struct scsi_device *sdev = to_scsi_device(dev);
  609. struct scsi_host_template *sht = sdev->host->hostt;
  610. if (!sht->change_queue_depth)
  611. return -EINVAL;
  612. depth = simple_strtoul(buf, NULL, 0);
  613. if (depth < 1)
  614. return -EINVAL;
  615. retval = sht->change_queue_depth(sdev, depth,
  616. SCSI_QDEPTH_DEFAULT);
  617. if (retval < 0)
  618. return retval;
  619. sdev->max_queue_depth = sdev->queue_depth;
  620. return count;
  621. }
  622. static struct device_attribute sdev_attr_queue_depth_rw =
  623. __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  624. sdev_store_queue_depth_rw);
  625. static ssize_t
  626. sdev_show_queue_ramp_up_period(struct device *dev,
  627. struct device_attribute *attr,
  628. char *buf)
  629. {
  630. struct scsi_device *sdev;
  631. sdev = to_scsi_device(dev);
  632. return snprintf(buf, 20, "%u\n",
  633. jiffies_to_msecs(sdev->queue_ramp_up_period));
  634. }
  635. static ssize_t
  636. sdev_store_queue_ramp_up_period(struct device *dev,
  637. struct device_attribute *attr,
  638. const char *buf, size_t count)
  639. {
  640. struct scsi_device *sdev = to_scsi_device(dev);
  641. unsigned long period;
  642. if (strict_strtoul(buf, 10, &period))
  643. return -EINVAL;
  644. sdev->queue_ramp_up_period = msecs_to_jiffies(period);
  645. return period;
  646. }
  647. static struct device_attribute sdev_attr_queue_ramp_up_period =
  648. __ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
  649. sdev_show_queue_ramp_up_period,
  650. sdev_store_queue_ramp_up_period);
  651. static ssize_t
  652. sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
  653. const char *buf, size_t count)
  654. {
  655. struct scsi_device *sdev = to_scsi_device(dev);
  656. struct scsi_host_template *sht = sdev->host->hostt;
  657. int tag_type = 0, retval;
  658. int prev_tag_type = scsi_get_tag_type(sdev);
  659. if (!sdev->tagged_supported || !sht->change_queue_type)
  660. return -EINVAL;
  661. if (strncmp(buf, "ordered", 7) == 0)
  662. tag_type = MSG_ORDERED_TAG;
  663. else if (strncmp(buf, "simple", 6) == 0)
  664. tag_type = MSG_SIMPLE_TAG;
  665. else if (strncmp(buf, "none", 4) != 0)
  666. return -EINVAL;
  667. if (tag_type == prev_tag_type)
  668. return count;
  669. retval = sht->change_queue_type(sdev, tag_type);
  670. if (retval < 0)
  671. return retval;
  672. return count;
  673. }
  674. static int scsi_target_add(struct scsi_target *starget)
  675. {
  676. int error;
  677. if (starget->state != STARGET_CREATED)
  678. return 0;
  679. error = device_add(&starget->dev);
  680. if (error) {
  681. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  682. return error;
  683. }
  684. transport_add_device(&starget->dev);
  685. starget->state = STARGET_RUNNING;
  686. pm_runtime_set_active(&starget->dev);
  687. pm_runtime_enable(&starget->dev);
  688. device_enable_async_suspend(&starget->dev);
  689. return 0;
  690. }
  691. static struct device_attribute sdev_attr_queue_type_rw =
  692. __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  693. sdev_store_queue_type_rw);
  694. /**
  695. * scsi_sysfs_add_sdev - add scsi device to sysfs
  696. * @sdev: scsi_device to add
  697. *
  698. * Return value:
  699. * 0 on Success / non-zero on Failure
  700. **/
  701. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  702. {
  703. int error, i;
  704. struct request_queue *rq = sdev->request_queue;
  705. struct scsi_target *starget = sdev->sdev_target;
  706. error = scsi_device_set_state(sdev, SDEV_RUNNING);
  707. if (error)
  708. return error;
  709. error = scsi_target_add(starget);
  710. if (error)
  711. return error;
  712. transport_configure_device(&starget->dev);
  713. device_enable_async_suspend(&sdev->sdev_gendev);
  714. scsi_autopm_get_target(starget);
  715. pm_runtime_set_active(&sdev->sdev_gendev);
  716. pm_runtime_forbid(&sdev->sdev_gendev);
  717. pm_runtime_enable(&sdev->sdev_gendev);
  718. scsi_autopm_put_target(starget);
  719. /* The following call will keep sdev active indefinitely, until
  720. * its driver does a corresponding scsi_autopm_pm_device(). Only
  721. * drivers supporting autosuspend will do this.
  722. */
  723. scsi_autopm_get_device(sdev);
  724. error = device_add(&sdev->sdev_gendev);
  725. if (error) {
  726. sdev_printk(KERN_INFO, sdev,
  727. "failed to add device: %d\n", error);
  728. return error;
  729. }
  730. device_enable_async_suspend(&sdev->sdev_dev);
  731. error = device_add(&sdev->sdev_dev);
  732. if (error) {
  733. sdev_printk(KERN_INFO, sdev,
  734. "failed to add class device: %d\n", error);
  735. device_del(&sdev->sdev_gendev);
  736. return error;
  737. }
  738. transport_add_device(&sdev->sdev_gendev);
  739. sdev->is_visible = 1;
  740. /* create queue files, which may be writable, depending on the host */
  741. if (sdev->host->hostt->change_queue_depth) {
  742. error = device_create_file(&sdev->sdev_gendev,
  743. &sdev_attr_queue_depth_rw);
  744. error = device_create_file(&sdev->sdev_gendev,
  745. &sdev_attr_queue_ramp_up_period);
  746. }
  747. else
  748. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
  749. if (error)
  750. return error;
  751. if (sdev->host->hostt->change_queue_type)
  752. error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
  753. else
  754. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
  755. if (error)
  756. return error;
  757. error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
  758. if (error)
  759. /* we're treating error on bsg register as non-fatal,
  760. * so pretend nothing went wrong */
  761. sdev_printk(KERN_INFO, sdev,
  762. "Failed to register bsg queue, errno=%d\n", error);
  763. /* add additional host specific attributes */
  764. if (sdev->host->hostt->sdev_attrs) {
  765. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  766. error = device_create_file(&sdev->sdev_gendev,
  767. sdev->host->hostt->sdev_attrs[i]);
  768. if (error)
  769. return error;
  770. }
  771. }
  772. return error;
  773. }
  774. void __scsi_remove_device(struct scsi_device *sdev)
  775. {
  776. struct device *dev = &sdev->sdev_gendev;
  777. if (sdev->is_visible) {
  778. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  779. return;
  780. bsg_unregister_queue(sdev->request_queue);
  781. device_unregister(&sdev->sdev_dev);
  782. transport_remove_device(dev);
  783. device_del(dev);
  784. } else
  785. put_device(&sdev->sdev_dev);
  786. scsi_device_set_state(sdev, SDEV_DEL);
  787. if (sdev->host->hostt->slave_destroy)
  788. sdev->host->hostt->slave_destroy(sdev);
  789. transport_destroy_device(dev);
  790. /* cause the request function to reject all I/O requests */
  791. sdev->request_queue->queuedata = NULL;
  792. /* Freeing the queue signals to block that we're done */
  793. scsi_free_queue(sdev->request_queue);
  794. put_device(dev);
  795. }
  796. /**
  797. * scsi_remove_device - unregister a device from the scsi bus
  798. * @sdev: scsi_device to unregister
  799. **/
  800. void scsi_remove_device(struct scsi_device *sdev)
  801. {
  802. struct Scsi_Host *shost = sdev->host;
  803. mutex_lock(&shost->scan_mutex);
  804. __scsi_remove_device(sdev);
  805. mutex_unlock(&shost->scan_mutex);
  806. }
  807. EXPORT_SYMBOL(scsi_remove_device);
  808. static void __scsi_remove_target(struct scsi_target *starget)
  809. {
  810. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  811. unsigned long flags;
  812. struct scsi_device *sdev;
  813. spin_lock_irqsave(shost->host_lock, flags);
  814. starget->reap_ref++;
  815. restart:
  816. list_for_each_entry(sdev, &shost->__devices, siblings) {
  817. if (sdev->channel != starget->channel ||
  818. sdev->id != starget->id ||
  819. scsi_device_get(sdev))
  820. continue;
  821. spin_unlock_irqrestore(shost->host_lock, flags);
  822. scsi_remove_device(sdev);
  823. scsi_device_put(sdev);
  824. spin_lock_irqsave(shost->host_lock, flags);
  825. goto restart;
  826. }
  827. spin_unlock_irqrestore(shost->host_lock, flags);
  828. scsi_target_reap(starget);
  829. }
  830. static int __remove_child (struct device * dev, void * data)
  831. {
  832. if (scsi_is_target_device(dev))
  833. __scsi_remove_target(to_scsi_target(dev));
  834. return 0;
  835. }
  836. /**
  837. * scsi_remove_target - try to remove a target and all its devices
  838. * @dev: generic starget or parent of generic stargets to be removed
  839. *
  840. * Note: This is slightly racy. It is possible that if the user
  841. * requests the addition of another device then the target won't be
  842. * removed.
  843. */
  844. void scsi_remove_target(struct device *dev)
  845. {
  846. if (scsi_is_target_device(dev)) {
  847. __scsi_remove_target(to_scsi_target(dev));
  848. return;
  849. }
  850. get_device(dev);
  851. device_for_each_child(dev, NULL, __remove_child);
  852. put_device(dev);
  853. }
  854. EXPORT_SYMBOL(scsi_remove_target);
  855. int scsi_register_driver(struct device_driver *drv)
  856. {
  857. drv->bus = &scsi_bus_type;
  858. return driver_register(drv);
  859. }
  860. EXPORT_SYMBOL(scsi_register_driver);
  861. int scsi_register_interface(struct class_interface *intf)
  862. {
  863. intf->class = &sdev_class;
  864. return class_interface_register(intf);
  865. }
  866. EXPORT_SYMBOL(scsi_register_interface);
  867. /**
  868. * scsi_sysfs_add_host - add scsi host to subsystem
  869. * @shost: scsi host struct to add to subsystem
  870. **/
  871. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  872. {
  873. int error, i;
  874. /* add host specific attributes */
  875. if (shost->hostt->shost_attrs) {
  876. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  877. error = device_create_file(&shost->shost_dev,
  878. shost->hostt->shost_attrs[i]);
  879. if (error)
  880. return error;
  881. }
  882. }
  883. transport_register_device(&shost->shost_gendev);
  884. transport_configure_device(&shost->shost_gendev);
  885. return 0;
  886. }
  887. static struct device_type scsi_dev_type = {
  888. .name = "scsi_device",
  889. .release = scsi_device_dev_release,
  890. .groups = scsi_sdev_attr_groups,
  891. };
  892. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  893. {
  894. unsigned long flags;
  895. struct Scsi_Host *shost = sdev->host;
  896. struct scsi_target *starget = sdev->sdev_target;
  897. device_initialize(&sdev->sdev_gendev);
  898. sdev->sdev_gendev.bus = &scsi_bus_type;
  899. sdev->sdev_gendev.type = &scsi_dev_type;
  900. dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
  901. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  902. device_initialize(&sdev->sdev_dev);
  903. sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
  904. sdev->sdev_dev.class = &sdev_class;
  905. dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
  906. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  907. sdev->scsi_level = starget->scsi_level;
  908. transport_setup_device(&sdev->sdev_gendev);
  909. spin_lock_irqsave(shost->host_lock, flags);
  910. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  911. list_add_tail(&sdev->siblings, &shost->__devices);
  912. spin_unlock_irqrestore(shost->host_lock, flags);
  913. }
  914. int scsi_is_sdev_device(const struct device *dev)
  915. {
  916. return dev->type == &scsi_dev_type;
  917. }
  918. EXPORT_SYMBOL(scsi_is_sdev_device);
  919. /* A blank transport template that is used in drivers that don't
  920. * yet implement Transport Attributes */
  921. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };