scsi_sysfs.c 28 KB

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