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