scsi_sysfs.c 22 KB

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