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