scsi_sysfs.c 27 KB

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