scsi_sysfs.c 21 KB

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