scsi_sysfs.c 27 KB

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