scsi_sysfs.c 27 KB

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