scsi_sysfs.c 25 KB

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