scsi_sysfs.c 27 KB

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