scsi_sysfs.c 27 KB

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