scsi_sysfs.c 24 KB

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