scsi_sysfs.c 27 KB

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