scsi_sysfs.c 28 KB

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