scsi_sysfs.c 27 KB

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