scsi_sysfs.c 27 KB

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