scsi_sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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/config.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/device.h>
  13. #include <scsi/scsi.h>
  14. #include <scsi/scsi_device.h>
  15. #include <scsi/scsi_host.h>
  16. #include <scsi/scsi_tcq.h>
  17. #include <scsi/scsi_transport.h>
  18. #include "scsi_priv.h"
  19. #include "scsi_logging.h"
  20. static const struct {
  21. enum scsi_device_state value;
  22. char *name;
  23. } sdev_states[] = {
  24. { SDEV_CREATED, "created" },
  25. { SDEV_RUNNING, "running" },
  26. { SDEV_CANCEL, "cancel" },
  27. { SDEV_DEL, "deleted" },
  28. { SDEV_QUIESCE, "quiesce" },
  29. { SDEV_OFFLINE, "offline" },
  30. { SDEV_BLOCK, "blocked" },
  31. };
  32. const char *scsi_device_state_name(enum scsi_device_state state)
  33. {
  34. int i;
  35. char *name = NULL;
  36. for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
  37. if (sdev_states[i].value == state) {
  38. name = sdev_states[i].name;
  39. break;
  40. }
  41. }
  42. return name;
  43. }
  44. static const struct {
  45. enum scsi_host_state value;
  46. char *name;
  47. } shost_states[] = {
  48. { SHOST_CREATED, "created" },
  49. { SHOST_RUNNING, "running" },
  50. { SHOST_CANCEL, "cancel" },
  51. { SHOST_DEL, "deleted" },
  52. { SHOST_RECOVERY, "recovery" },
  53. { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
  54. { SHOST_DEL_RECOVERY, "deleted/recovery", },
  55. };
  56. const char *scsi_host_state_name(enum scsi_host_state state)
  57. {
  58. int i;
  59. char *name = NULL;
  60. for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
  61. if (shost_states[i].value == state) {
  62. name = shost_states[i].name;
  63. break;
  64. }
  65. }
  66. return name;
  67. }
  68. static int check_set(unsigned int *val, char *src)
  69. {
  70. char *last;
  71. if (strncmp(src, "-", 20) == 0) {
  72. *val = SCAN_WILD_CARD;
  73. } else {
  74. /*
  75. * Doesn't check for int overflow
  76. */
  77. *val = simple_strtoul(src, &last, 0);
  78. if (*last != '\0')
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. static int scsi_scan(struct Scsi_Host *shost, const char *str)
  84. {
  85. char s1[15], s2[15], s3[15], junk;
  86. unsigned int channel, id, lun;
  87. int res;
  88. res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
  89. if (res != 3)
  90. return -EINVAL;
  91. if (check_set(&channel, s1))
  92. return -EINVAL;
  93. if (check_set(&id, s2))
  94. return -EINVAL;
  95. if (check_set(&lun, s3))
  96. return -EINVAL;
  97. res = scsi_scan_host_selected(shost, channel, id, lun, 1);
  98. return res;
  99. }
  100. /*
  101. * shost_show_function: macro to create an attr function that can be used to
  102. * show a non-bit field.
  103. */
  104. #define shost_show_function(name, field, format_string) \
  105. static ssize_t \
  106. show_##name (struct class_device *class_dev, char *buf) \
  107. { \
  108. struct Scsi_Host *shost = class_to_shost(class_dev); \
  109. return snprintf (buf, 20, format_string, shost->field); \
  110. }
  111. /*
  112. * shost_rd_attr: macro to create a function and attribute variable for a
  113. * read only field.
  114. */
  115. #define shost_rd_attr2(name, field, format_string) \
  116. shost_show_function(name, field, format_string) \
  117. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  118. #define shost_rd_attr(field, format_string) \
  119. shost_rd_attr2(field, field, format_string)
  120. /*
  121. * Create the actual show/store functions and data structures.
  122. */
  123. static ssize_t store_scan(struct class_device *class_dev, const char *buf,
  124. size_t count)
  125. {
  126. struct Scsi_Host *shost = class_to_shost(class_dev);
  127. int res;
  128. res = scsi_scan(shost, buf);
  129. if (res == 0)
  130. res = count;
  131. return res;
  132. };
  133. static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
  134. static ssize_t
  135. store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
  136. {
  137. int i;
  138. struct Scsi_Host *shost = class_to_shost(class_dev);
  139. enum scsi_host_state state = 0;
  140. for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
  141. const int len = strlen(shost_states[i].name);
  142. if (strncmp(shost_states[i].name, buf, len) == 0 &&
  143. buf[len] == '\n') {
  144. state = shost_states[i].value;
  145. break;
  146. }
  147. }
  148. if (!state)
  149. return -EINVAL;
  150. if (scsi_host_set_state(shost, state))
  151. return -EINVAL;
  152. return count;
  153. }
  154. static ssize_t
  155. show_shost_state(struct class_device *class_dev, char *buf)
  156. {
  157. struct Scsi_Host *shost = class_to_shost(class_dev);
  158. const char *name = scsi_host_state_name(shost->shost_state);
  159. if (!name)
  160. return -EINVAL;
  161. return snprintf(buf, 20, "%s\n", name);
  162. }
  163. static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
  164. shost_rd_attr(unique_id, "%u\n");
  165. shost_rd_attr(host_busy, "%hu\n");
  166. shost_rd_attr(cmd_per_lun, "%hd\n");
  167. shost_rd_attr(sg_tablesize, "%hu\n");
  168. shost_rd_attr(unchecked_isa_dma, "%d\n");
  169. shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
  170. static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
  171. &class_device_attr_unique_id,
  172. &class_device_attr_host_busy,
  173. &class_device_attr_cmd_per_lun,
  174. &class_device_attr_sg_tablesize,
  175. &class_device_attr_unchecked_isa_dma,
  176. &class_device_attr_proc_name,
  177. &class_device_attr_scan,
  178. &class_device_attr_state,
  179. NULL
  180. };
  181. static void scsi_device_cls_release(struct class_device *class_dev)
  182. {
  183. struct scsi_device *sdev;
  184. sdev = class_to_sdev(class_dev);
  185. put_device(&sdev->sdev_gendev);
  186. }
  187. static void scsi_device_dev_release(struct device *dev)
  188. {
  189. struct scsi_device *sdev;
  190. struct device *parent;
  191. struct scsi_target *starget;
  192. unsigned long flags;
  193. parent = dev->parent;
  194. sdev = to_scsi_device(dev);
  195. starget = to_scsi_target(parent);
  196. spin_lock_irqsave(sdev->host->host_lock, flags);
  197. starget->reap_ref++;
  198. list_del(&sdev->siblings);
  199. list_del(&sdev->same_target_siblings);
  200. list_del(&sdev->starved_entry);
  201. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  202. if (sdev->request_queue) {
  203. sdev->request_queue->queuedata = NULL;
  204. scsi_free_queue(sdev->request_queue);
  205. /* temporary expedient, try to catch use of queue lock
  206. * after free of sdev */
  207. sdev->request_queue = NULL;
  208. }
  209. scsi_target_reap(scsi_target(sdev));
  210. kfree(sdev->inquiry);
  211. kfree(sdev);
  212. if (parent)
  213. put_device(parent);
  214. }
  215. static struct class sdev_class = {
  216. .name = "scsi_device",
  217. .release = scsi_device_cls_release,
  218. };
  219. /* all probing is done in the individual ->probe routines */
  220. static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
  221. {
  222. struct scsi_device *sdp = to_scsi_device(dev);
  223. if (sdp->no_uld_attach)
  224. return 0;
  225. return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
  226. }
  227. static int scsi_bus_suspend(struct device * dev, pm_message_t state)
  228. {
  229. struct scsi_device *sdev = to_scsi_device(dev);
  230. struct scsi_host_template *sht = sdev->host->hostt;
  231. int err;
  232. err = scsi_device_quiesce(sdev);
  233. if (err)
  234. return err;
  235. if (sht->suspend)
  236. err = sht->suspend(sdev);
  237. return err;
  238. }
  239. static int scsi_bus_resume(struct device * dev)
  240. {
  241. struct scsi_device *sdev = to_scsi_device(dev);
  242. struct scsi_host_template *sht = sdev->host->hostt;
  243. int err = 0;
  244. if (sht->resume)
  245. err = sht->resume(sdev);
  246. scsi_device_resume(sdev);
  247. return err;
  248. }
  249. struct bus_type scsi_bus_type = {
  250. .name = "scsi",
  251. .match = scsi_bus_match,
  252. .suspend = scsi_bus_suspend,
  253. .resume = scsi_bus_resume,
  254. };
  255. int scsi_sysfs_register(void)
  256. {
  257. int error;
  258. error = bus_register(&scsi_bus_type);
  259. if (!error) {
  260. error = class_register(&sdev_class);
  261. if (error)
  262. bus_unregister(&scsi_bus_type);
  263. }
  264. return error;
  265. }
  266. void scsi_sysfs_unregister(void)
  267. {
  268. class_unregister(&sdev_class);
  269. bus_unregister(&scsi_bus_type);
  270. }
  271. /*
  272. * sdev_show_function: macro to create an attr function that can be used to
  273. * show a non-bit field.
  274. */
  275. #define sdev_show_function(field, format_string) \
  276. static ssize_t \
  277. sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  278. { \
  279. struct scsi_device *sdev; \
  280. sdev = to_scsi_device(dev); \
  281. return snprintf (buf, 20, format_string, sdev->field); \
  282. } \
  283. /*
  284. * sdev_rd_attr: macro to create a function and attribute variable for a
  285. * read only field.
  286. */
  287. #define sdev_rd_attr(field, format_string) \
  288. sdev_show_function(field, format_string) \
  289. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  290. /*
  291. * sdev_rd_attr: create a function and attribute variable for a
  292. * read/write field.
  293. */
  294. #define sdev_rw_attr(field, format_string) \
  295. sdev_show_function(field, format_string) \
  296. \
  297. static ssize_t \
  298. sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  299. { \
  300. struct scsi_device *sdev; \
  301. sdev = to_scsi_device(dev); \
  302. snscanf (buf, 20, format_string, &sdev->field); \
  303. return count; \
  304. } \
  305. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  306. /* Currently we don't export bit fields, but we might in future,
  307. * so leave this code in */
  308. #if 0
  309. /*
  310. * sdev_rd_attr: create a function and attribute variable for a
  311. * read/write bit field.
  312. */
  313. #define sdev_rw_attr_bit(field) \
  314. sdev_show_function(field, "%d\n") \
  315. \
  316. static ssize_t \
  317. sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  318. { \
  319. int ret; \
  320. struct scsi_device *sdev; \
  321. ret = scsi_sdev_check_buf_bit(buf); \
  322. if (ret >= 0) { \
  323. sdev = to_scsi_device(dev); \
  324. sdev->field = ret; \
  325. ret = count; \
  326. } \
  327. return ret; \
  328. } \
  329. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  330. /*
  331. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  332. * else return -EINVAL.
  333. */
  334. static int scsi_sdev_check_buf_bit(const char *buf)
  335. {
  336. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  337. if (buf[0] == '1')
  338. return 1;
  339. else if (buf[0] == '0')
  340. return 0;
  341. else
  342. return -EINVAL;
  343. } else
  344. return -EINVAL;
  345. }
  346. #endif
  347. /*
  348. * Create the actual show/store functions and data structures.
  349. */
  350. sdev_rd_attr (device_blocked, "%d\n");
  351. sdev_rd_attr (queue_depth, "%d\n");
  352. sdev_rd_attr (type, "%d\n");
  353. sdev_rd_attr (scsi_level, "%d\n");
  354. sdev_rd_attr (vendor, "%.8s\n");
  355. sdev_rd_attr (model, "%.16s\n");
  356. sdev_rd_attr (rev, "%.4s\n");
  357. static ssize_t
  358. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  359. {
  360. struct scsi_device *sdev;
  361. sdev = to_scsi_device(dev);
  362. return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
  363. }
  364. static ssize_t
  365. sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  366. {
  367. struct scsi_device *sdev;
  368. int timeout;
  369. sdev = to_scsi_device(dev);
  370. sscanf (buf, "%d\n", &timeout);
  371. sdev->timeout = timeout * HZ;
  372. return count;
  373. }
  374. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  375. static ssize_t
  376. store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  377. {
  378. scsi_rescan_device(dev);
  379. return count;
  380. }
  381. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  382. static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
  383. size_t count)
  384. {
  385. scsi_remove_device(to_scsi_device(dev));
  386. return count;
  387. };
  388. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  389. static ssize_t
  390. store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  391. {
  392. int i;
  393. struct scsi_device *sdev = to_scsi_device(dev);
  394. enum scsi_device_state state = 0;
  395. for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
  396. const int len = strlen(sdev_states[i].name);
  397. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  398. buf[len] == '\n') {
  399. state = sdev_states[i].value;
  400. break;
  401. }
  402. }
  403. if (!state)
  404. return -EINVAL;
  405. if (scsi_device_set_state(sdev, state))
  406. return -EINVAL;
  407. return count;
  408. }
  409. static ssize_t
  410. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  411. {
  412. struct scsi_device *sdev = to_scsi_device(dev);
  413. const char *name = scsi_device_state_name(sdev->sdev_state);
  414. if (!name)
  415. return -EINVAL;
  416. return snprintf(buf, 20, "%s\n", name);
  417. }
  418. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  419. static ssize_t
  420. show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
  421. {
  422. struct scsi_device *sdev = to_scsi_device(dev);
  423. const char *name = "none";
  424. if (sdev->ordered_tags)
  425. name = "ordered";
  426. else if (sdev->simple_tags)
  427. name = "simple";
  428. return snprintf(buf, 20, "%s\n", name);
  429. }
  430. static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
  431. static ssize_t
  432. show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
  433. {
  434. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  435. }
  436. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  437. #define show_sdev_iostat(field) \
  438. static ssize_t \
  439. show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \
  440. { \
  441. struct scsi_device *sdev = to_scsi_device(dev); \
  442. unsigned long long count = atomic_read(&sdev->field); \
  443. return snprintf(buf, 20, "0x%llx\n", count); \
  444. } \
  445. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  446. show_sdev_iostat(iorequest_cnt);
  447. show_sdev_iostat(iodone_cnt);
  448. show_sdev_iostat(ioerr_cnt);
  449. /* Default template for device attributes. May NOT be modified */
  450. static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
  451. &dev_attr_device_blocked,
  452. &dev_attr_queue_depth,
  453. &dev_attr_queue_type,
  454. &dev_attr_type,
  455. &dev_attr_scsi_level,
  456. &dev_attr_vendor,
  457. &dev_attr_model,
  458. &dev_attr_rev,
  459. &dev_attr_rescan,
  460. &dev_attr_delete,
  461. &dev_attr_state,
  462. &dev_attr_timeout,
  463. &dev_attr_iocounterbits,
  464. &dev_attr_iorequest_cnt,
  465. &dev_attr_iodone_cnt,
  466. &dev_attr_ioerr_cnt,
  467. NULL
  468. };
  469. static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
  470. size_t count)
  471. {
  472. int depth, retval;
  473. struct scsi_device *sdev = to_scsi_device(dev);
  474. struct scsi_host_template *sht = sdev->host->hostt;
  475. if (!sht->change_queue_depth)
  476. return -EINVAL;
  477. depth = simple_strtoul(buf, NULL, 0);
  478. if (depth < 1)
  479. return -EINVAL;
  480. retval = sht->change_queue_depth(sdev, depth);
  481. if (retval < 0)
  482. return retval;
  483. return count;
  484. }
  485. static struct device_attribute sdev_attr_queue_depth_rw =
  486. __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  487. sdev_store_queue_depth_rw);
  488. static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
  489. size_t count)
  490. {
  491. struct scsi_device *sdev = to_scsi_device(dev);
  492. struct scsi_host_template *sht = sdev->host->hostt;
  493. int tag_type = 0, retval;
  494. int prev_tag_type = scsi_get_tag_type(sdev);
  495. if (!sdev->tagged_supported || !sht->change_queue_type)
  496. return -EINVAL;
  497. if (strncmp(buf, "ordered", 7) == 0)
  498. tag_type = MSG_ORDERED_TAG;
  499. else if (strncmp(buf, "simple", 6) == 0)
  500. tag_type = MSG_SIMPLE_TAG;
  501. else if (strncmp(buf, "none", 4) != 0)
  502. return -EINVAL;
  503. if (tag_type == prev_tag_type)
  504. return count;
  505. retval = sht->change_queue_type(sdev, tag_type);
  506. if (retval < 0)
  507. return retval;
  508. return count;
  509. }
  510. static struct device_attribute sdev_attr_queue_type_rw =
  511. __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  512. sdev_store_queue_type_rw);
  513. static struct device_attribute *attr_changed_internally(
  514. struct Scsi_Host *shost,
  515. struct device_attribute * attr)
  516. {
  517. if (!strcmp("queue_depth", attr->attr.name)
  518. && shost->hostt->change_queue_depth)
  519. return &sdev_attr_queue_depth_rw;
  520. else if (!strcmp("queue_type", attr->attr.name)
  521. && shost->hostt->change_queue_type)
  522. return &sdev_attr_queue_type_rw;
  523. return attr;
  524. }
  525. static struct device_attribute *attr_overridden(
  526. struct device_attribute **attrs,
  527. struct device_attribute *attr)
  528. {
  529. int i;
  530. if (!attrs)
  531. return NULL;
  532. for (i = 0; attrs[i]; i++)
  533. if (!strcmp(attrs[i]->attr.name, attr->attr.name))
  534. return attrs[i];
  535. return NULL;
  536. }
  537. static int attr_add(struct device *dev, struct device_attribute *attr)
  538. {
  539. struct device_attribute *base_attr;
  540. /*
  541. * Spare the caller from having to copy things it's not interested in.
  542. */
  543. base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
  544. if (base_attr) {
  545. /* extend permissions */
  546. attr->attr.mode |= base_attr->attr.mode;
  547. /* override null show/store with default */
  548. if (!attr->show)
  549. attr->show = base_attr->show;
  550. if (!attr->store)
  551. attr->store = base_attr->store;
  552. }
  553. return device_create_file(dev, attr);
  554. }
  555. /**
  556. * scsi_sysfs_add_sdev - add scsi device to sysfs
  557. * @sdev: scsi_device to add
  558. *
  559. * Return value:
  560. * 0 on Success / non-zero on Failure
  561. **/
  562. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  563. {
  564. int error, i;
  565. if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
  566. return error;
  567. error = device_add(&sdev->sdev_gendev);
  568. if (error) {
  569. put_device(sdev->sdev_gendev.parent);
  570. printk(KERN_INFO "error 1\n");
  571. return error;
  572. }
  573. error = class_device_add(&sdev->sdev_classdev);
  574. if (error) {
  575. printk(KERN_INFO "error 2\n");
  576. goto clean_device;
  577. }
  578. /* take a reference for the sdev_classdev; this is
  579. * released by the sdev_class .release */
  580. get_device(&sdev->sdev_gendev);
  581. if (sdev->host->hostt->sdev_attrs) {
  582. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  583. error = attr_add(&sdev->sdev_gendev,
  584. sdev->host->hostt->sdev_attrs[i]);
  585. if (error) {
  586. __scsi_remove_device(sdev);
  587. goto out;
  588. }
  589. }
  590. }
  591. for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
  592. if (!attr_overridden(sdev->host->hostt->sdev_attrs,
  593. scsi_sysfs_sdev_attrs[i])) {
  594. struct device_attribute * attr =
  595. attr_changed_internally(sdev->host,
  596. scsi_sysfs_sdev_attrs[i]);
  597. error = device_create_file(&sdev->sdev_gendev, attr);
  598. if (error) {
  599. __scsi_remove_device(sdev);
  600. goto out;
  601. }
  602. }
  603. }
  604. transport_add_device(&sdev->sdev_gendev);
  605. out:
  606. return error;
  607. clean_device:
  608. scsi_device_set_state(sdev, SDEV_CANCEL);
  609. device_del(&sdev->sdev_gendev);
  610. transport_destroy_device(&sdev->sdev_gendev);
  611. put_device(&sdev->sdev_gendev);
  612. return error;
  613. }
  614. void __scsi_remove_device(struct scsi_device *sdev)
  615. {
  616. struct device *dev = &sdev->sdev_gendev;
  617. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  618. return;
  619. class_device_unregister(&sdev->sdev_classdev);
  620. transport_remove_device(dev);
  621. device_del(dev);
  622. scsi_device_set_state(sdev, SDEV_DEL);
  623. if (sdev->host->hostt->slave_destroy)
  624. sdev->host->hostt->slave_destroy(sdev);
  625. transport_destroy_device(dev);
  626. put_device(dev);
  627. }
  628. /**
  629. * scsi_remove_device - unregister a device from the scsi bus
  630. * @sdev: scsi_device to unregister
  631. **/
  632. void scsi_remove_device(struct scsi_device *sdev)
  633. {
  634. struct Scsi_Host *shost = sdev->host;
  635. mutex_lock(&shost->scan_mutex);
  636. __scsi_remove_device(sdev);
  637. mutex_unlock(&shost->scan_mutex);
  638. }
  639. EXPORT_SYMBOL(scsi_remove_device);
  640. void __scsi_remove_target(struct scsi_target *starget)
  641. {
  642. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  643. unsigned long flags;
  644. struct scsi_device *sdev;
  645. spin_lock_irqsave(shost->host_lock, flags);
  646. starget->reap_ref++;
  647. restart:
  648. list_for_each_entry(sdev, &shost->__devices, siblings) {
  649. if (sdev->channel != starget->channel ||
  650. sdev->id != starget->id ||
  651. sdev->sdev_state == SDEV_DEL)
  652. continue;
  653. spin_unlock_irqrestore(shost->host_lock, flags);
  654. scsi_remove_device(sdev);
  655. spin_lock_irqsave(shost->host_lock, flags);
  656. goto restart;
  657. }
  658. spin_unlock_irqrestore(shost->host_lock, flags);
  659. scsi_target_reap(starget);
  660. }
  661. static int __remove_child (struct device * dev, void * data)
  662. {
  663. if (scsi_is_target_device(dev))
  664. __scsi_remove_target(to_scsi_target(dev));
  665. return 0;
  666. }
  667. /**
  668. * scsi_remove_target - try to remove a target and all its devices
  669. * @dev: generic starget or parent of generic stargets to be removed
  670. *
  671. * Note: This is slightly racy. It is possible that if the user
  672. * requests the addition of another device then the target won't be
  673. * removed.
  674. */
  675. void scsi_remove_target(struct device *dev)
  676. {
  677. struct device *rdev;
  678. if (scsi_is_target_device(dev)) {
  679. __scsi_remove_target(to_scsi_target(dev));
  680. return;
  681. }
  682. rdev = get_device(dev);
  683. device_for_each_child(dev, NULL, __remove_child);
  684. put_device(rdev);
  685. }
  686. EXPORT_SYMBOL(scsi_remove_target);
  687. int scsi_register_driver(struct device_driver *drv)
  688. {
  689. drv->bus = &scsi_bus_type;
  690. return driver_register(drv);
  691. }
  692. EXPORT_SYMBOL(scsi_register_driver);
  693. int scsi_register_interface(struct class_interface *intf)
  694. {
  695. intf->class = &sdev_class;
  696. return class_interface_register(intf);
  697. }
  698. EXPORT_SYMBOL(scsi_register_interface);
  699. static struct class_device_attribute *class_attr_overridden(
  700. struct class_device_attribute **attrs,
  701. struct class_device_attribute *attr)
  702. {
  703. int i;
  704. if (!attrs)
  705. return NULL;
  706. for (i = 0; attrs[i]; i++)
  707. if (!strcmp(attrs[i]->attr.name, attr->attr.name))
  708. return attrs[i];
  709. return NULL;
  710. }
  711. static int class_attr_add(struct class_device *classdev,
  712. struct class_device_attribute *attr)
  713. {
  714. struct class_device_attribute *base_attr;
  715. /*
  716. * Spare the caller from having to copy things it's not interested in.
  717. */
  718. base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
  719. if (base_attr) {
  720. /* extend permissions */
  721. attr->attr.mode |= base_attr->attr.mode;
  722. /* override null show/store with default */
  723. if (!attr->show)
  724. attr->show = base_attr->show;
  725. if (!attr->store)
  726. attr->store = base_attr->store;
  727. }
  728. return class_device_create_file(classdev, attr);
  729. }
  730. /**
  731. * scsi_sysfs_add_host - add scsi host to subsystem
  732. * @shost: scsi host struct to add to subsystem
  733. * @dev: parent struct device pointer
  734. **/
  735. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  736. {
  737. int error, i;
  738. if (shost->hostt->shost_attrs) {
  739. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  740. error = class_attr_add(&shost->shost_classdev,
  741. shost->hostt->shost_attrs[i]);
  742. if (error)
  743. return error;
  744. }
  745. }
  746. for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
  747. if (!class_attr_overridden(shost->hostt->shost_attrs,
  748. scsi_sysfs_shost_attrs[i])) {
  749. error = class_device_create_file(&shost->shost_classdev,
  750. scsi_sysfs_shost_attrs[i]);
  751. if (error)
  752. return error;
  753. }
  754. }
  755. transport_register_device(&shost->shost_gendev);
  756. return 0;
  757. }
  758. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  759. {
  760. unsigned long flags;
  761. struct Scsi_Host *shost = sdev->host;
  762. struct scsi_target *starget = sdev->sdev_target;
  763. device_initialize(&sdev->sdev_gendev);
  764. sdev->sdev_gendev.bus = &scsi_bus_type;
  765. sdev->sdev_gendev.release = scsi_device_dev_release;
  766. sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
  767. sdev->host->host_no, sdev->channel, sdev->id,
  768. sdev->lun);
  769. class_device_initialize(&sdev->sdev_classdev);
  770. sdev->sdev_classdev.dev = &sdev->sdev_gendev;
  771. sdev->sdev_classdev.class = &sdev_class;
  772. snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
  773. "%d:%d:%d:%d", sdev->host->host_no,
  774. sdev->channel, sdev->id, sdev->lun);
  775. sdev->scsi_level = SCSI_2;
  776. transport_setup_device(&sdev->sdev_gendev);
  777. spin_lock_irqsave(shost->host_lock, flags);
  778. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  779. list_add_tail(&sdev->siblings, &shost->__devices);
  780. spin_unlock_irqrestore(shost->host_lock, flags);
  781. }
  782. int scsi_is_sdev_device(const struct device *dev)
  783. {
  784. return dev->release == scsi_device_dev_release;
  785. }
  786. EXPORT_SYMBOL(scsi_is_sdev_device);
  787. /* A blank transport template that is used in drivers that don't
  788. * yet implement Transport Attributes */
  789. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };