scsi_sysfs.c 30 KB

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