scsi_sysfs.c 23 KB

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