scsi_sysfs.c 24 KB

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