scsi_sysfs.c 27 KB

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