scsi_sysfs.c 27 KB

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