scsi_sysfs.c 23 KB

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