scsi_sysfs.c 27 KB

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