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