scsi_sysfs.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <scsi/scsi.h>
  15. #include <scsi/scsi_device.h>
  16. #include <scsi/scsi_host.h>
  17. #include <scsi/scsi_tcq.h>
  18. #include <scsi/scsi_dh.h>
  19. #include <scsi/scsi_transport.h>
  20. #include <scsi/scsi_driver.h>
  21. #include <scsi/scsi_devinfo.h>
  22. #include "scsi_priv.h"
  23. #include "scsi_logging.h"
  24. static struct device_type scsi_dev_type;
  25. static const struct {
  26. enum scsi_device_state value;
  27. char *name;
  28. } sdev_states[] = {
  29. { SDEV_CREATED, "created" },
  30. { SDEV_RUNNING, "running" },
  31. { SDEV_CANCEL, "cancel" },
  32. { SDEV_DEL, "deleted" },
  33. { SDEV_QUIESCE, "quiesce" },
  34. { SDEV_OFFLINE, "offline" },
  35. { SDEV_TRANSPORT_OFFLINE, "transport-offline" },
  36. { SDEV_BLOCK, "blocked" },
  37. { SDEV_CREATED_BLOCK, "created-blocked" },
  38. };
  39. const char *scsi_device_state_name(enum scsi_device_state state)
  40. {
  41. int i;
  42. char *name = NULL;
  43. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  44. if (sdev_states[i].value == state) {
  45. name = sdev_states[i].name;
  46. break;
  47. }
  48. }
  49. return name;
  50. }
  51. static const struct {
  52. enum scsi_host_state value;
  53. char *name;
  54. } shost_states[] = {
  55. { SHOST_CREATED, "created" },
  56. { SHOST_RUNNING, "running" },
  57. { SHOST_CANCEL, "cancel" },
  58. { SHOST_DEL, "deleted" },
  59. { SHOST_RECOVERY, "recovery" },
  60. { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
  61. { SHOST_DEL_RECOVERY, "deleted/recovery", },
  62. };
  63. const char *scsi_host_state_name(enum scsi_host_state state)
  64. {
  65. int i;
  66. char *name = NULL;
  67. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  68. if (shost_states[i].value == state) {
  69. name = shost_states[i].name;
  70. break;
  71. }
  72. }
  73. return name;
  74. }
  75. #ifdef CONFIG_SCSI_DH
  76. static const struct {
  77. unsigned char value;
  78. char *name;
  79. } sdev_access_states[] = {
  80. { SCSI_ACCESS_STATE_OPTIMAL, "active/optimized" },
  81. { SCSI_ACCESS_STATE_ACTIVE, "active/non-optimized" },
  82. { SCSI_ACCESS_STATE_STANDBY, "standby" },
  83. { SCSI_ACCESS_STATE_UNAVAILABLE, "unavailable" },
  84. { SCSI_ACCESS_STATE_LBA, "lba-dependent" },
  85. { SCSI_ACCESS_STATE_OFFLINE, "offline" },
  86. { SCSI_ACCESS_STATE_TRANSITIONING, "transitioning" },
  87. };
  88. static const char *scsi_access_state_name(unsigned char state)
  89. {
  90. int i;
  91. char *name = NULL;
  92. for (i = 0; i < ARRAY_SIZE(sdev_access_states); i++) {
  93. if (sdev_access_states[i].value == state) {
  94. name = sdev_access_states[i].name;
  95. break;
  96. }
  97. }
  98. return name;
  99. }
  100. #endif
  101. static int check_set(unsigned long long *val, char *src)
  102. {
  103. char *last;
  104. if (strcmp(src, "-") == 0) {
  105. *val = SCAN_WILD_CARD;
  106. } else {
  107. /*
  108. * Doesn't check for int overflow
  109. */
  110. *val = simple_strtoull(src, &last, 0);
  111. if (*last != '\0')
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. static int scsi_scan(struct Scsi_Host *shost, const char *str)
  117. {
  118. char s1[15], s2[15], s3[17], junk;
  119. unsigned long long channel, id, lun;
  120. int res;
  121. res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk);
  122. if (res != 3)
  123. return -EINVAL;
  124. if (check_set(&channel, s1))
  125. return -EINVAL;
  126. if (check_set(&id, s2))
  127. return -EINVAL;
  128. if (check_set(&lun, s3))
  129. return -EINVAL;
  130. if (shost->transportt->user_scan)
  131. res = shost->transportt->user_scan(shost, channel, id, lun);
  132. else
  133. res = scsi_scan_host_selected(shost, channel, id, lun,
  134. SCSI_SCAN_MANUAL);
  135. return res;
  136. }
  137. /*
  138. * shost_show_function: macro to create an attr function that can be used to
  139. * show a non-bit field.
  140. */
  141. #define shost_show_function(name, field, format_string) \
  142. static ssize_t \
  143. show_##name (struct device *dev, struct device_attribute *attr, \
  144. char *buf) \
  145. { \
  146. struct Scsi_Host *shost = class_to_shost(dev); \
  147. return snprintf (buf, 20, format_string, shost->field); \
  148. }
  149. /*
  150. * shost_rd_attr: macro to create a function and attribute variable for a
  151. * read only field.
  152. */
  153. #define shost_rd_attr2(name, field, format_string) \
  154. shost_show_function(name, field, format_string) \
  155. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  156. #define shost_rd_attr(field, format_string) \
  157. shost_rd_attr2(field, field, format_string)
  158. /*
  159. * Create the actual show/store functions and data structures.
  160. */
  161. static ssize_t
  162. store_scan(struct device *dev, struct device_attribute *attr,
  163. const char *buf, size_t count)
  164. {
  165. struct Scsi_Host *shost = class_to_shost(dev);
  166. int res;
  167. res = scsi_scan(shost, buf);
  168. if (res == 0)
  169. res = count;
  170. return res;
  171. };
  172. static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
  173. static ssize_t
  174. store_shost_state(struct device *dev, struct device_attribute *attr,
  175. const char *buf, size_t count)
  176. {
  177. int i;
  178. struct Scsi_Host *shost = class_to_shost(dev);
  179. enum scsi_host_state state = 0;
  180. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  181. const int len = strlen(shost_states[i].name);
  182. if (strncmp(shost_states[i].name, buf, len) == 0 &&
  183. buf[len] == '\n') {
  184. state = shost_states[i].value;
  185. break;
  186. }
  187. }
  188. if (!state)
  189. return -EINVAL;
  190. if (scsi_host_set_state(shost, state))
  191. return -EINVAL;
  192. return count;
  193. }
  194. static ssize_t
  195. show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
  196. {
  197. struct Scsi_Host *shost = class_to_shost(dev);
  198. const char *name = scsi_host_state_name(shost->shost_state);
  199. if (!name)
  200. return -EINVAL;
  201. return snprintf(buf, 20, "%s\n", name);
  202. }
  203. /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
  204. static struct device_attribute dev_attr_hstate =
  205. __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
  206. static ssize_t
  207. show_shost_mode(unsigned int mode, char *buf)
  208. {
  209. ssize_t len = 0;
  210. if (mode & MODE_INITIATOR)
  211. len = sprintf(buf, "%s", "Initiator");
  212. if (mode & MODE_TARGET)
  213. len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
  214. len += sprintf(buf + len, "\n");
  215. return len;
  216. }
  217. static ssize_t
  218. show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
  219. char *buf)
  220. {
  221. struct Scsi_Host *shost = class_to_shost(dev);
  222. unsigned int supported_mode = shost->hostt->supported_mode;
  223. if (supported_mode == MODE_UNKNOWN)
  224. /* by default this should be initiator */
  225. supported_mode = MODE_INITIATOR;
  226. return show_shost_mode(supported_mode, buf);
  227. }
  228. static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
  229. static ssize_t
  230. show_shost_active_mode(struct device *dev,
  231. struct device_attribute *attr, char *buf)
  232. {
  233. struct Scsi_Host *shost = class_to_shost(dev);
  234. if (shost->active_mode == MODE_UNKNOWN)
  235. return snprintf(buf, 20, "unknown\n");
  236. else
  237. return show_shost_mode(shost->active_mode, buf);
  238. }
  239. static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
  240. static int check_reset_type(const char *str)
  241. {
  242. if (sysfs_streq(str, "adapter"))
  243. return SCSI_ADAPTER_RESET;
  244. else if (sysfs_streq(str, "firmware"))
  245. return SCSI_FIRMWARE_RESET;
  246. else
  247. return 0;
  248. }
  249. static ssize_t
  250. store_host_reset(struct device *dev, struct device_attribute *attr,
  251. const char *buf, size_t count)
  252. {
  253. struct Scsi_Host *shost = class_to_shost(dev);
  254. struct scsi_host_template *sht = shost->hostt;
  255. int ret = -EINVAL;
  256. int type;
  257. type = check_reset_type(buf);
  258. if (!type)
  259. goto exit_store_host_reset;
  260. if (sht->host_reset)
  261. ret = sht->host_reset(shost, type);
  262. else
  263. ret = -EOPNOTSUPP;
  264. exit_store_host_reset:
  265. if (ret == 0)
  266. ret = count;
  267. return ret;
  268. }
  269. static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
  270. static ssize_t
  271. show_shost_eh_deadline(struct device *dev,
  272. struct device_attribute *attr, char *buf)
  273. {
  274. struct Scsi_Host *shost = class_to_shost(dev);
  275. if (shost->eh_deadline == -1)
  276. return snprintf(buf, strlen("off") + 2, "off\n");
  277. return sprintf(buf, "%u\n", shost->eh_deadline / HZ);
  278. }
  279. static ssize_t
  280. store_shost_eh_deadline(struct device *dev, struct device_attribute *attr,
  281. const char *buf, size_t count)
  282. {
  283. struct Scsi_Host *shost = class_to_shost(dev);
  284. int ret = -EINVAL;
  285. unsigned long deadline, flags;
  286. if (shost->transportt &&
  287. (shost->transportt->eh_strategy_handler ||
  288. !shost->hostt->eh_host_reset_handler))
  289. return ret;
  290. if (!strncmp(buf, "off", strlen("off")))
  291. deadline = -1;
  292. else {
  293. ret = kstrtoul(buf, 10, &deadline);
  294. if (ret)
  295. return ret;
  296. if (deadline * HZ > UINT_MAX)
  297. return -EINVAL;
  298. }
  299. spin_lock_irqsave(shost->host_lock, flags);
  300. if (scsi_host_in_recovery(shost))
  301. ret = -EBUSY;
  302. else {
  303. if (deadline == -1)
  304. shost->eh_deadline = -1;
  305. else
  306. shost->eh_deadline = deadline * HZ;
  307. ret = count;
  308. }
  309. spin_unlock_irqrestore(shost->host_lock, flags);
  310. return ret;
  311. }
  312. static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
  313. shost_rd_attr(use_blk_mq, "%d\n");
  314. shost_rd_attr(unique_id, "%u\n");
  315. shost_rd_attr(cmd_per_lun, "%hd\n");
  316. shost_rd_attr(can_queue, "%hd\n");
  317. shost_rd_attr(sg_tablesize, "%hu\n");
  318. shost_rd_attr(sg_prot_tablesize, "%hu\n");
  319. shost_rd_attr(unchecked_isa_dma, "%d\n");
  320. shost_rd_attr(prot_capabilities, "%u\n");
  321. shost_rd_attr(prot_guard_type, "%hd\n");
  322. shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
  323. static ssize_t
  324. show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
  325. {
  326. struct Scsi_Host *shost = class_to_shost(dev);
  327. return snprintf(buf, 20, "%d\n", scsi_host_busy(shost));
  328. }
  329. static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
  330. static struct attribute *scsi_sysfs_shost_attrs[] = {
  331. &dev_attr_use_blk_mq.attr,
  332. &dev_attr_unique_id.attr,
  333. &dev_attr_host_busy.attr,
  334. &dev_attr_cmd_per_lun.attr,
  335. &dev_attr_can_queue.attr,
  336. &dev_attr_sg_tablesize.attr,
  337. &dev_attr_sg_prot_tablesize.attr,
  338. &dev_attr_unchecked_isa_dma.attr,
  339. &dev_attr_proc_name.attr,
  340. &dev_attr_scan.attr,
  341. &dev_attr_hstate.attr,
  342. &dev_attr_supported_mode.attr,
  343. &dev_attr_active_mode.attr,
  344. &dev_attr_prot_capabilities.attr,
  345. &dev_attr_prot_guard_type.attr,
  346. &dev_attr_host_reset.attr,
  347. &dev_attr_eh_deadline.attr,
  348. NULL
  349. };
  350. static struct attribute_group scsi_shost_attr_group = {
  351. .attrs = scsi_sysfs_shost_attrs,
  352. };
  353. const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
  354. &scsi_shost_attr_group,
  355. NULL
  356. };
  357. static void scsi_device_cls_release(struct device *class_dev)
  358. {
  359. struct scsi_device *sdev;
  360. sdev = class_to_sdev(class_dev);
  361. put_device(&sdev->sdev_gendev);
  362. }
  363. static void scsi_device_dev_release_usercontext(struct work_struct *work)
  364. {
  365. struct scsi_device *sdev;
  366. struct device *parent;
  367. struct list_head *this, *tmp;
  368. struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
  369. unsigned long flags;
  370. sdev = container_of(work, struct scsi_device, ew.work);
  371. scsi_dh_release_device(sdev);
  372. parent = sdev->sdev_gendev.parent;
  373. spin_lock_irqsave(sdev->host->host_lock, flags);
  374. list_del(&sdev->siblings);
  375. list_del(&sdev->same_target_siblings);
  376. list_del(&sdev->starved_entry);
  377. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  378. cancel_work_sync(&sdev->event_work);
  379. list_for_each_safe(this, tmp, &sdev->event_list) {
  380. struct scsi_event *evt;
  381. evt = list_entry(this, struct scsi_event, node);
  382. list_del(&evt->node);
  383. kfree(evt);
  384. }
  385. blk_put_queue(sdev->request_queue);
  386. /* NULL queue means the device can't be used */
  387. sdev->request_queue = NULL;
  388. mutex_lock(&sdev->inquiry_mutex);
  389. rcu_swap_protected(sdev->vpd_pg80, vpd_pg80,
  390. lockdep_is_held(&sdev->inquiry_mutex));
  391. rcu_swap_protected(sdev->vpd_pg83, vpd_pg83,
  392. lockdep_is_held(&sdev->inquiry_mutex));
  393. mutex_unlock(&sdev->inquiry_mutex);
  394. if (vpd_pg83)
  395. kfree_rcu(vpd_pg83, rcu);
  396. if (vpd_pg80)
  397. kfree_rcu(vpd_pg80, rcu);
  398. kfree(sdev->inquiry);
  399. kfree(sdev);
  400. if (parent)
  401. put_device(parent);
  402. }
  403. static void scsi_device_dev_release(struct device *dev)
  404. {
  405. struct scsi_device *sdp = to_scsi_device(dev);
  406. execute_in_process_context(scsi_device_dev_release_usercontext,
  407. &sdp->ew);
  408. }
  409. static struct class sdev_class = {
  410. .name = "scsi_device",
  411. .dev_release = scsi_device_cls_release,
  412. };
  413. /* all probing is done in the individual ->probe routines */
  414. static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
  415. {
  416. struct scsi_device *sdp;
  417. if (dev->type != &scsi_dev_type)
  418. return 0;
  419. sdp = to_scsi_device(dev);
  420. if (sdp->no_uld_attach)
  421. return 0;
  422. return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
  423. }
  424. static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  425. {
  426. struct scsi_device *sdev;
  427. if (dev->type != &scsi_dev_type)
  428. return 0;
  429. sdev = to_scsi_device(dev);
  430. add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
  431. return 0;
  432. }
  433. struct bus_type scsi_bus_type = {
  434. .name = "scsi",
  435. .match = scsi_bus_match,
  436. .uevent = scsi_bus_uevent,
  437. #ifdef CONFIG_PM
  438. .pm = &scsi_bus_pm_ops,
  439. #endif
  440. };
  441. EXPORT_SYMBOL_GPL(scsi_bus_type);
  442. int scsi_sysfs_register(void)
  443. {
  444. int error;
  445. error = bus_register(&scsi_bus_type);
  446. if (!error) {
  447. error = class_register(&sdev_class);
  448. if (error)
  449. bus_unregister(&scsi_bus_type);
  450. }
  451. return error;
  452. }
  453. void scsi_sysfs_unregister(void)
  454. {
  455. class_unregister(&sdev_class);
  456. bus_unregister(&scsi_bus_type);
  457. }
  458. /*
  459. * sdev_show_function: macro to create an attr function that can be used to
  460. * show a non-bit field.
  461. */
  462. #define sdev_show_function(field, format_string) \
  463. static ssize_t \
  464. sdev_show_##field (struct device *dev, struct device_attribute *attr, \
  465. char *buf) \
  466. { \
  467. struct scsi_device *sdev; \
  468. sdev = to_scsi_device(dev); \
  469. return snprintf (buf, 20, format_string, sdev->field); \
  470. } \
  471. /*
  472. * sdev_rd_attr: macro to create a function and attribute variable for a
  473. * read only field.
  474. */
  475. #define sdev_rd_attr(field, format_string) \
  476. sdev_show_function(field, format_string) \
  477. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  478. /*
  479. * sdev_rw_attr: create a function and attribute variable for a
  480. * read/write field.
  481. */
  482. #define sdev_rw_attr(field, format_string) \
  483. sdev_show_function(field, format_string) \
  484. \
  485. static ssize_t \
  486. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  487. const char *buf, size_t count) \
  488. { \
  489. struct scsi_device *sdev; \
  490. sdev = to_scsi_device(dev); \
  491. sscanf (buf, format_string, &sdev->field); \
  492. return count; \
  493. } \
  494. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  495. /* Currently we don't export bit fields, but we might in future,
  496. * so leave this code in */
  497. #if 0
  498. /*
  499. * sdev_rd_attr: create a function and attribute variable for a
  500. * read/write bit field.
  501. */
  502. #define sdev_rw_attr_bit(field) \
  503. sdev_show_function(field, "%d\n") \
  504. \
  505. static ssize_t \
  506. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  507. const char *buf, size_t count) \
  508. { \
  509. int ret; \
  510. struct scsi_device *sdev; \
  511. ret = scsi_sdev_check_buf_bit(buf); \
  512. if (ret >= 0) { \
  513. sdev = to_scsi_device(dev); \
  514. sdev->field = ret; \
  515. ret = count; \
  516. } \
  517. return ret; \
  518. } \
  519. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  520. /*
  521. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  522. * else return -EINVAL.
  523. */
  524. static int scsi_sdev_check_buf_bit(const char *buf)
  525. {
  526. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  527. if (buf[0] == '1')
  528. return 1;
  529. else if (buf[0] == '0')
  530. return 0;
  531. else
  532. return -EINVAL;
  533. } else
  534. return -EINVAL;
  535. }
  536. #endif
  537. /*
  538. * Create the actual show/store functions and data structures.
  539. */
  540. sdev_rd_attr (type, "%d\n");
  541. sdev_rd_attr (scsi_level, "%d\n");
  542. sdev_rd_attr (vendor, "%.8s\n");
  543. sdev_rd_attr (model, "%.16s\n");
  544. sdev_rd_attr (rev, "%.4s\n");
  545. static ssize_t
  546. sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
  547. char *buf)
  548. {
  549. struct scsi_device *sdev = to_scsi_device(dev);
  550. return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_busy));
  551. }
  552. static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
  553. static ssize_t
  554. sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
  555. char *buf)
  556. {
  557. struct scsi_device *sdev = to_scsi_device(dev);
  558. return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
  559. }
  560. static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
  561. /*
  562. * TODO: can we make these symlinks to the block layer ones?
  563. */
  564. static ssize_t
  565. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  566. {
  567. struct scsi_device *sdev;
  568. sdev = to_scsi_device(dev);
  569. return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
  570. }
  571. static ssize_t
  572. sdev_store_timeout (struct device *dev, struct device_attribute *attr,
  573. const char *buf, size_t count)
  574. {
  575. struct scsi_device *sdev;
  576. int timeout;
  577. sdev = to_scsi_device(dev);
  578. sscanf (buf, "%d\n", &timeout);
  579. blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
  580. return count;
  581. }
  582. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  583. static ssize_t
  584. sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  585. {
  586. struct scsi_device *sdev;
  587. sdev = to_scsi_device(dev);
  588. return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
  589. }
  590. static ssize_t
  591. sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
  592. const char *buf, size_t count)
  593. {
  594. struct scsi_device *sdev;
  595. unsigned int eh_timeout;
  596. int err;
  597. if (!capable(CAP_SYS_ADMIN))
  598. return -EACCES;
  599. sdev = to_scsi_device(dev);
  600. err = kstrtouint(buf, 10, &eh_timeout);
  601. if (err)
  602. return err;
  603. sdev->eh_timeout = eh_timeout * HZ;
  604. return count;
  605. }
  606. static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
  607. static ssize_t
  608. store_rescan_field (struct device *dev, struct device_attribute *attr,
  609. const char *buf, size_t count)
  610. {
  611. scsi_rescan_device(dev);
  612. return count;
  613. }
  614. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  615. static ssize_t
  616. sdev_store_delete(struct device *dev, struct device_attribute *attr,
  617. const char *buf, size_t count)
  618. {
  619. struct kernfs_node *kn;
  620. struct scsi_device *sdev = to_scsi_device(dev);
  621. /*
  622. * We need to try to get module, avoiding the module been removed
  623. * during delete.
  624. */
  625. if (scsi_device_get(sdev))
  626. return -ENODEV;
  627. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  628. WARN_ON_ONCE(!kn);
  629. /*
  630. * Concurrent writes into the "delete" sysfs attribute may trigger
  631. * concurrent calls to device_remove_file() and scsi_remove_device().
  632. * device_remove_file() handles concurrent removal calls by
  633. * serializing these and by ignoring the second and later removal
  634. * attempts. Concurrent calls of scsi_remove_device() are
  635. * serialized. The second and later calls of scsi_remove_device() are
  636. * ignored because the first call of that function changes the device
  637. * state into SDEV_DEL.
  638. */
  639. device_remove_file(dev, attr);
  640. scsi_remove_device(sdev);
  641. if (kn)
  642. sysfs_unbreak_active_protection(kn);
  643. scsi_device_put(sdev);
  644. return count;
  645. };
  646. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  647. static ssize_t
  648. store_state_field(struct device *dev, struct device_attribute *attr,
  649. const char *buf, size_t count)
  650. {
  651. int i, ret;
  652. struct scsi_device *sdev = to_scsi_device(dev);
  653. enum scsi_device_state state = 0;
  654. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  655. const int len = strlen(sdev_states[i].name);
  656. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  657. buf[len] == '\n') {
  658. state = sdev_states[i].value;
  659. break;
  660. }
  661. }
  662. if (!state)
  663. return -EINVAL;
  664. mutex_lock(&sdev->state_mutex);
  665. ret = scsi_device_set_state(sdev, state);
  666. mutex_unlock(&sdev->state_mutex);
  667. return ret == 0 ? count : -EINVAL;
  668. }
  669. static ssize_t
  670. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  671. {
  672. struct scsi_device *sdev = to_scsi_device(dev);
  673. const char *name = scsi_device_state_name(sdev->sdev_state);
  674. if (!name)
  675. return -EINVAL;
  676. return snprintf(buf, 20, "%s\n", name);
  677. }
  678. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  679. static ssize_t
  680. show_queue_type_field(struct device *dev, struct device_attribute *attr,
  681. char *buf)
  682. {
  683. struct scsi_device *sdev = to_scsi_device(dev);
  684. const char *name = "none";
  685. if (sdev->simple_tags)
  686. name = "simple";
  687. return snprintf(buf, 20, "%s\n", name);
  688. }
  689. static ssize_t
  690. store_queue_type_field(struct device *dev, struct device_attribute *attr,
  691. const char *buf, size_t count)
  692. {
  693. struct scsi_device *sdev = to_scsi_device(dev);
  694. if (!sdev->tagged_supported)
  695. return -EINVAL;
  696. sdev_printk(KERN_INFO, sdev,
  697. "ignoring write to deprecated queue_type attribute");
  698. return count;
  699. }
  700. static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  701. store_queue_type_field);
  702. #define sdev_vpd_pg_attr(_page) \
  703. static ssize_t \
  704. show_vpd_##_page(struct file *filp, struct kobject *kobj, \
  705. struct bin_attribute *bin_attr, \
  706. char *buf, loff_t off, size_t count) \
  707. { \
  708. struct device *dev = container_of(kobj, struct device, kobj); \
  709. struct scsi_device *sdev = to_scsi_device(dev); \
  710. struct scsi_vpd *vpd_page; \
  711. int ret = -EINVAL; \
  712. \
  713. rcu_read_lock(); \
  714. vpd_page = rcu_dereference(sdev->vpd_##_page); \
  715. if (vpd_page) \
  716. ret = memory_read_from_buffer(buf, count, &off, \
  717. vpd_page->data, vpd_page->len); \
  718. rcu_read_unlock(); \
  719. return ret; \
  720. } \
  721. static struct bin_attribute dev_attr_vpd_##_page = { \
  722. .attr = {.name = __stringify(vpd_##_page), .mode = S_IRUGO }, \
  723. .size = 0, \
  724. .read = show_vpd_##_page, \
  725. };
  726. sdev_vpd_pg_attr(pg83);
  727. sdev_vpd_pg_attr(pg80);
  728. static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
  729. struct bin_attribute *bin_attr,
  730. char *buf, loff_t off, size_t count)
  731. {
  732. struct device *dev = container_of(kobj, struct device, kobj);
  733. struct scsi_device *sdev = to_scsi_device(dev);
  734. if (!sdev->inquiry)
  735. return -EINVAL;
  736. return memory_read_from_buffer(buf, count, &off, sdev->inquiry,
  737. sdev->inquiry_len);
  738. }
  739. static struct bin_attribute dev_attr_inquiry = {
  740. .attr = {
  741. .name = "inquiry",
  742. .mode = S_IRUGO,
  743. },
  744. .size = 0,
  745. .read = show_inquiry,
  746. };
  747. static ssize_t
  748. show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
  749. char *buf)
  750. {
  751. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  752. }
  753. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  754. #define show_sdev_iostat(field) \
  755. static ssize_t \
  756. show_iostat_##field(struct device *dev, struct device_attribute *attr, \
  757. char *buf) \
  758. { \
  759. struct scsi_device *sdev = to_scsi_device(dev); \
  760. unsigned long long count = atomic_read(&sdev->field); \
  761. return snprintf(buf, 20, "0x%llx\n", count); \
  762. } \
  763. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  764. show_sdev_iostat(iorequest_cnt);
  765. show_sdev_iostat(iodone_cnt);
  766. show_sdev_iostat(ioerr_cnt);
  767. static ssize_t
  768. sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  769. {
  770. struct scsi_device *sdev;
  771. sdev = to_scsi_device(dev);
  772. return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
  773. }
  774. static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
  775. #define DECLARE_EVT_SHOW(name, Cap_name) \
  776. static ssize_t \
  777. sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
  778. char *buf) \
  779. { \
  780. struct scsi_device *sdev = to_scsi_device(dev); \
  781. int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
  782. return snprintf(buf, 20, "%d\n", val); \
  783. }
  784. #define DECLARE_EVT_STORE(name, Cap_name) \
  785. static ssize_t \
  786. sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
  787. const char *buf, size_t count) \
  788. { \
  789. struct scsi_device *sdev = to_scsi_device(dev); \
  790. int val = simple_strtoul(buf, NULL, 0); \
  791. if (val == 0) \
  792. clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  793. else if (val == 1) \
  794. set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  795. else \
  796. return -EINVAL; \
  797. return count; \
  798. }
  799. #define DECLARE_EVT(name, Cap_name) \
  800. DECLARE_EVT_SHOW(name, Cap_name) \
  801. DECLARE_EVT_STORE(name, Cap_name) \
  802. static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
  803. sdev_store_evt_##name);
  804. #define REF_EVT(name) &dev_attr_evt_##name.attr
  805. DECLARE_EVT(media_change, MEDIA_CHANGE)
  806. DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
  807. DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
  808. DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
  809. DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
  810. DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
  811. static ssize_t
  812. sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
  813. const char *buf, size_t count)
  814. {
  815. int depth, retval;
  816. struct scsi_device *sdev = to_scsi_device(dev);
  817. struct scsi_host_template *sht = sdev->host->hostt;
  818. if (!sht->change_queue_depth)
  819. return -EINVAL;
  820. depth = simple_strtoul(buf, NULL, 0);
  821. if (depth < 1 || depth > sdev->host->can_queue)
  822. return -EINVAL;
  823. retval = sht->change_queue_depth(sdev, depth);
  824. if (retval < 0)
  825. return retval;
  826. sdev->max_queue_depth = sdev->queue_depth;
  827. return count;
  828. }
  829. sdev_show_function(queue_depth, "%d\n");
  830. static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  831. sdev_store_queue_depth);
  832. static ssize_t
  833. sdev_show_wwid(struct device *dev, struct device_attribute *attr,
  834. char *buf)
  835. {
  836. struct scsi_device *sdev = to_scsi_device(dev);
  837. ssize_t count;
  838. count = scsi_vpd_lun_id(sdev, buf, PAGE_SIZE);
  839. if (count > 0) {
  840. buf[count] = '\n';
  841. count++;
  842. }
  843. return count;
  844. }
  845. static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
  846. #define BLIST_FLAG_NAME(name) \
  847. [const_ilog2((__force __u64)BLIST_##name)] = #name
  848. static const char *const sdev_bflags_name[] = {
  849. #include "scsi_devinfo_tbl.c"
  850. };
  851. #undef BLIST_FLAG_NAME
  852. static ssize_t
  853. sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
  854. char *buf)
  855. {
  856. struct scsi_device *sdev = to_scsi_device(dev);
  857. int i;
  858. ssize_t len = 0;
  859. for (i = 0; i < sizeof(sdev->sdev_bflags) * BITS_PER_BYTE; i++) {
  860. const char *name = NULL;
  861. if (!(sdev->sdev_bflags & (__force blist_flags_t)BIT(i)))
  862. continue;
  863. if (i < ARRAY_SIZE(sdev_bflags_name) && sdev_bflags_name[i])
  864. name = sdev_bflags_name[i];
  865. if (name)
  866. len += snprintf(buf + len, PAGE_SIZE - len,
  867. "%s%s", len ? " " : "", name);
  868. else
  869. len += snprintf(buf + len, PAGE_SIZE - len,
  870. "%sINVALID_BIT(%d)", len ? " " : "", i);
  871. }
  872. if (len)
  873. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  874. return len;
  875. }
  876. static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);
  877. #ifdef CONFIG_SCSI_DH
  878. static ssize_t
  879. sdev_show_dh_state(struct device *dev, struct device_attribute *attr,
  880. char *buf)
  881. {
  882. struct scsi_device *sdev = to_scsi_device(dev);
  883. if (!sdev->handler)
  884. return snprintf(buf, 20, "detached\n");
  885. return snprintf(buf, 20, "%s\n", sdev->handler->name);
  886. }
  887. static ssize_t
  888. sdev_store_dh_state(struct device *dev, struct device_attribute *attr,
  889. const char *buf, size_t count)
  890. {
  891. struct scsi_device *sdev = to_scsi_device(dev);
  892. int err = -EINVAL;
  893. if (sdev->sdev_state == SDEV_CANCEL ||
  894. sdev->sdev_state == SDEV_DEL)
  895. return -ENODEV;
  896. if (!sdev->handler) {
  897. /*
  898. * Attach to a device handler
  899. */
  900. err = scsi_dh_attach(sdev->request_queue, buf);
  901. } else if (!strncmp(buf, "activate", 8)) {
  902. /*
  903. * Activate a device handler
  904. */
  905. if (sdev->handler->activate)
  906. err = sdev->handler->activate(sdev, NULL, NULL);
  907. else
  908. err = 0;
  909. } else if (!strncmp(buf, "detach", 6)) {
  910. /*
  911. * Detach from a device handler
  912. */
  913. sdev_printk(KERN_WARNING, sdev,
  914. "can't detach handler %s.\n",
  915. sdev->handler->name);
  916. err = -EINVAL;
  917. }
  918. return err < 0 ? err : count;
  919. }
  920. static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state,
  921. sdev_store_dh_state);
  922. static ssize_t
  923. sdev_show_access_state(struct device *dev,
  924. struct device_attribute *attr,
  925. char *buf)
  926. {
  927. struct scsi_device *sdev = to_scsi_device(dev);
  928. unsigned char access_state;
  929. const char *access_state_name;
  930. if (!sdev->handler)
  931. return -EINVAL;
  932. access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
  933. access_state_name = scsi_access_state_name(access_state);
  934. return sprintf(buf, "%s\n",
  935. access_state_name ? access_state_name : "unknown");
  936. }
  937. static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
  938. static ssize_t
  939. sdev_show_preferred_path(struct device *dev,
  940. struct device_attribute *attr,
  941. char *buf)
  942. {
  943. struct scsi_device *sdev = to_scsi_device(dev);
  944. if (!sdev->handler)
  945. return -EINVAL;
  946. if (sdev->access_state & SCSI_ACCESS_STATE_PREFERRED)
  947. return sprintf(buf, "1\n");
  948. else
  949. return sprintf(buf, "0\n");
  950. }
  951. static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
  952. #endif
  953. static ssize_t
  954. sdev_show_queue_ramp_up_period(struct device *dev,
  955. struct device_attribute *attr,
  956. char *buf)
  957. {
  958. struct scsi_device *sdev;
  959. sdev = to_scsi_device(dev);
  960. return snprintf(buf, 20, "%u\n",
  961. jiffies_to_msecs(sdev->queue_ramp_up_period));
  962. }
  963. static ssize_t
  964. sdev_store_queue_ramp_up_period(struct device *dev,
  965. struct device_attribute *attr,
  966. const char *buf, size_t count)
  967. {
  968. struct scsi_device *sdev = to_scsi_device(dev);
  969. unsigned int period;
  970. if (kstrtouint(buf, 10, &period))
  971. return -EINVAL;
  972. sdev->queue_ramp_up_period = msecs_to_jiffies(period);
  973. return count;
  974. }
  975. static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
  976. sdev_show_queue_ramp_up_period,
  977. sdev_store_queue_ramp_up_period);
  978. static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
  979. struct attribute *attr, int i)
  980. {
  981. struct device *dev = container_of(kobj, struct device, kobj);
  982. struct scsi_device *sdev = to_scsi_device(dev);
  983. if (attr == &dev_attr_queue_depth.attr &&
  984. !sdev->host->hostt->change_queue_depth)
  985. return S_IRUGO;
  986. if (attr == &dev_attr_queue_ramp_up_period.attr &&
  987. !sdev->host->hostt->change_queue_depth)
  988. return 0;
  989. #ifdef CONFIG_SCSI_DH
  990. if (attr == &dev_attr_access_state.attr &&
  991. !sdev->handler)
  992. return 0;
  993. if (attr == &dev_attr_preferred_path.attr &&
  994. !sdev->handler)
  995. return 0;
  996. #endif
  997. return attr->mode;
  998. }
  999. static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
  1000. struct bin_attribute *attr, int i)
  1001. {
  1002. struct device *dev = container_of(kobj, struct device, kobj);
  1003. struct scsi_device *sdev = to_scsi_device(dev);
  1004. if (attr == &dev_attr_vpd_pg80 && !sdev->vpd_pg80)
  1005. return 0;
  1006. if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
  1007. return 0;
  1008. return S_IRUGO;
  1009. }
  1010. /* Default template for device attributes. May NOT be modified */
  1011. static struct attribute *scsi_sdev_attrs[] = {
  1012. &dev_attr_device_blocked.attr,
  1013. &dev_attr_type.attr,
  1014. &dev_attr_scsi_level.attr,
  1015. &dev_attr_device_busy.attr,
  1016. &dev_attr_vendor.attr,
  1017. &dev_attr_model.attr,
  1018. &dev_attr_rev.attr,
  1019. &dev_attr_rescan.attr,
  1020. &dev_attr_delete.attr,
  1021. &dev_attr_state.attr,
  1022. &dev_attr_timeout.attr,
  1023. &dev_attr_eh_timeout.attr,
  1024. &dev_attr_iocounterbits.attr,
  1025. &dev_attr_iorequest_cnt.attr,
  1026. &dev_attr_iodone_cnt.attr,
  1027. &dev_attr_ioerr_cnt.attr,
  1028. &dev_attr_modalias.attr,
  1029. &dev_attr_queue_depth.attr,
  1030. &dev_attr_queue_type.attr,
  1031. &dev_attr_wwid.attr,
  1032. &dev_attr_blacklist.attr,
  1033. #ifdef CONFIG_SCSI_DH
  1034. &dev_attr_dh_state.attr,
  1035. &dev_attr_access_state.attr,
  1036. &dev_attr_preferred_path.attr,
  1037. #endif
  1038. &dev_attr_queue_ramp_up_period.attr,
  1039. REF_EVT(media_change),
  1040. REF_EVT(inquiry_change_reported),
  1041. REF_EVT(capacity_change_reported),
  1042. REF_EVT(soft_threshold_reached),
  1043. REF_EVT(mode_parameter_change_reported),
  1044. REF_EVT(lun_change_reported),
  1045. NULL
  1046. };
  1047. static struct bin_attribute *scsi_sdev_bin_attrs[] = {
  1048. &dev_attr_vpd_pg83,
  1049. &dev_attr_vpd_pg80,
  1050. &dev_attr_inquiry,
  1051. NULL
  1052. };
  1053. static struct attribute_group scsi_sdev_attr_group = {
  1054. .attrs = scsi_sdev_attrs,
  1055. .bin_attrs = scsi_sdev_bin_attrs,
  1056. .is_visible = scsi_sdev_attr_is_visible,
  1057. .is_bin_visible = scsi_sdev_bin_attr_is_visible,
  1058. };
  1059. static const struct attribute_group *scsi_sdev_attr_groups[] = {
  1060. &scsi_sdev_attr_group,
  1061. NULL
  1062. };
  1063. static int scsi_target_add(struct scsi_target *starget)
  1064. {
  1065. int error;
  1066. if (starget->state != STARGET_CREATED)
  1067. return 0;
  1068. error = device_add(&starget->dev);
  1069. if (error) {
  1070. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  1071. return error;
  1072. }
  1073. transport_add_device(&starget->dev);
  1074. starget->state = STARGET_RUNNING;
  1075. pm_runtime_set_active(&starget->dev);
  1076. pm_runtime_enable(&starget->dev);
  1077. device_enable_async_suspend(&starget->dev);
  1078. return 0;
  1079. }
  1080. /**
  1081. * scsi_sysfs_add_sdev - add scsi device to sysfs
  1082. * @sdev: scsi_device to add
  1083. *
  1084. * Return value:
  1085. * 0 on Success / non-zero on Failure
  1086. **/
  1087. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  1088. {
  1089. int error, i;
  1090. struct request_queue *rq = sdev->request_queue;
  1091. struct scsi_target *starget = sdev->sdev_target;
  1092. error = scsi_target_add(starget);
  1093. if (error)
  1094. return error;
  1095. transport_configure_device(&starget->dev);
  1096. device_enable_async_suspend(&sdev->sdev_gendev);
  1097. scsi_autopm_get_target(starget);
  1098. pm_runtime_set_active(&sdev->sdev_gendev);
  1099. pm_runtime_forbid(&sdev->sdev_gendev);
  1100. pm_runtime_enable(&sdev->sdev_gendev);
  1101. scsi_autopm_put_target(starget);
  1102. scsi_autopm_get_device(sdev);
  1103. scsi_dh_add_device(sdev);
  1104. error = device_add(&sdev->sdev_gendev);
  1105. if (error) {
  1106. sdev_printk(KERN_INFO, sdev,
  1107. "failed to add device: %d\n", error);
  1108. return error;
  1109. }
  1110. device_enable_async_suspend(&sdev->sdev_dev);
  1111. error = device_add(&sdev->sdev_dev);
  1112. if (error) {
  1113. sdev_printk(KERN_INFO, sdev,
  1114. "failed to add class device: %d\n", error);
  1115. device_del(&sdev->sdev_gendev);
  1116. return error;
  1117. }
  1118. transport_add_device(&sdev->sdev_gendev);
  1119. sdev->is_visible = 1;
  1120. error = bsg_scsi_register_queue(rq, &sdev->sdev_gendev);
  1121. if (error)
  1122. /* we're treating error on bsg register as non-fatal,
  1123. * so pretend nothing went wrong */
  1124. sdev_printk(KERN_INFO, sdev,
  1125. "Failed to register bsg queue, errno=%d\n", error);
  1126. /* add additional host specific attributes */
  1127. if (sdev->host->hostt->sdev_attrs) {
  1128. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  1129. error = device_create_file(&sdev->sdev_gendev,
  1130. sdev->host->hostt->sdev_attrs[i]);
  1131. if (error)
  1132. return error;
  1133. }
  1134. }
  1135. if (sdev->host->hostt->sdev_groups) {
  1136. error = sysfs_create_groups(&sdev->sdev_gendev.kobj,
  1137. sdev->host->hostt->sdev_groups);
  1138. if (error)
  1139. return error;
  1140. }
  1141. scsi_autopm_put_device(sdev);
  1142. return error;
  1143. }
  1144. void __scsi_remove_device(struct scsi_device *sdev)
  1145. {
  1146. struct device *dev = &sdev->sdev_gendev;
  1147. int res;
  1148. /*
  1149. * This cleanup path is not reentrant and while it is impossible
  1150. * to get a new reference with scsi_device_get() someone can still
  1151. * hold a previously acquired one.
  1152. */
  1153. if (sdev->sdev_state == SDEV_DEL)
  1154. return;
  1155. if (sdev->is_visible) {
  1156. /*
  1157. * If scsi_internal_target_block() is running concurrently,
  1158. * wait until it has finished before changing the device state.
  1159. */
  1160. mutex_lock(&sdev->state_mutex);
  1161. /*
  1162. * If blocked, we go straight to DEL and restart the queue so
  1163. * any commands issued during driver shutdown (like sync
  1164. * cache) are errored immediately.
  1165. */
  1166. res = scsi_device_set_state(sdev, SDEV_CANCEL);
  1167. if (res != 0) {
  1168. res = scsi_device_set_state(sdev, SDEV_DEL);
  1169. if (res == 0)
  1170. scsi_start_queue(sdev);
  1171. }
  1172. mutex_unlock(&sdev->state_mutex);
  1173. if (res != 0)
  1174. return;
  1175. if (sdev->host->hostt->sdev_groups)
  1176. sysfs_remove_groups(&sdev->sdev_gendev.kobj,
  1177. sdev->host->hostt->sdev_groups);
  1178. bsg_unregister_queue(sdev->request_queue);
  1179. device_unregister(&sdev->sdev_dev);
  1180. transport_remove_device(dev);
  1181. device_del(dev);
  1182. } else
  1183. put_device(&sdev->sdev_dev);
  1184. /*
  1185. * Stop accepting new requests and wait until all queuecommand() and
  1186. * scsi_run_queue() invocations have finished before tearing down the
  1187. * device.
  1188. */
  1189. mutex_lock(&sdev->state_mutex);
  1190. scsi_device_set_state(sdev, SDEV_DEL);
  1191. mutex_unlock(&sdev->state_mutex);
  1192. blk_cleanup_queue(sdev->request_queue);
  1193. cancel_work_sync(&sdev->requeue_work);
  1194. if (sdev->host->hostt->slave_destroy)
  1195. sdev->host->hostt->slave_destroy(sdev);
  1196. transport_destroy_device(dev);
  1197. /*
  1198. * Paired with the kref_get() in scsi_sysfs_initialize(). We have
  1199. * remoed sysfs visibility from the device, so make the target
  1200. * invisible if this was the last device underneath it.
  1201. */
  1202. scsi_target_reap(scsi_target(sdev));
  1203. put_device(dev);
  1204. }
  1205. /**
  1206. * scsi_remove_device - unregister a device from the scsi bus
  1207. * @sdev: scsi_device to unregister
  1208. **/
  1209. void scsi_remove_device(struct scsi_device *sdev)
  1210. {
  1211. struct Scsi_Host *shost = sdev->host;
  1212. mutex_lock(&shost->scan_mutex);
  1213. __scsi_remove_device(sdev);
  1214. mutex_unlock(&shost->scan_mutex);
  1215. }
  1216. EXPORT_SYMBOL(scsi_remove_device);
  1217. static void __scsi_remove_target(struct scsi_target *starget)
  1218. {
  1219. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1220. unsigned long flags;
  1221. struct scsi_device *sdev;
  1222. spin_lock_irqsave(shost->host_lock, flags);
  1223. restart:
  1224. list_for_each_entry(sdev, &shost->__devices, siblings) {
  1225. /*
  1226. * We cannot call scsi_device_get() here, as
  1227. * we might've been called from rmmod() causing
  1228. * scsi_device_get() to fail the module_is_live()
  1229. * check.
  1230. */
  1231. if (sdev->channel != starget->channel ||
  1232. sdev->id != starget->id)
  1233. continue;
  1234. if (sdev->sdev_state == SDEV_DEL ||
  1235. sdev->sdev_state == SDEV_CANCEL ||
  1236. !get_device(&sdev->sdev_gendev))
  1237. continue;
  1238. spin_unlock_irqrestore(shost->host_lock, flags);
  1239. scsi_remove_device(sdev);
  1240. put_device(&sdev->sdev_gendev);
  1241. spin_lock_irqsave(shost->host_lock, flags);
  1242. goto restart;
  1243. }
  1244. spin_unlock_irqrestore(shost->host_lock, flags);
  1245. }
  1246. /**
  1247. * scsi_remove_target - try to remove a target and all its devices
  1248. * @dev: generic starget or parent of generic stargets to be removed
  1249. *
  1250. * Note: This is slightly racy. It is possible that if the user
  1251. * requests the addition of another device then the target won't be
  1252. * removed.
  1253. */
  1254. void scsi_remove_target(struct device *dev)
  1255. {
  1256. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  1257. struct scsi_target *starget;
  1258. unsigned long flags;
  1259. restart:
  1260. spin_lock_irqsave(shost->host_lock, flags);
  1261. list_for_each_entry(starget, &shost->__targets, siblings) {
  1262. if (starget->state == STARGET_DEL ||
  1263. starget->state == STARGET_REMOVE ||
  1264. starget->state == STARGET_CREATED_REMOVE)
  1265. continue;
  1266. if (starget->dev.parent == dev || &starget->dev == dev) {
  1267. kref_get(&starget->reap_ref);
  1268. if (starget->state == STARGET_CREATED)
  1269. starget->state = STARGET_CREATED_REMOVE;
  1270. else
  1271. starget->state = STARGET_REMOVE;
  1272. spin_unlock_irqrestore(shost->host_lock, flags);
  1273. __scsi_remove_target(starget);
  1274. scsi_target_reap(starget);
  1275. goto restart;
  1276. }
  1277. }
  1278. spin_unlock_irqrestore(shost->host_lock, flags);
  1279. }
  1280. EXPORT_SYMBOL(scsi_remove_target);
  1281. int scsi_register_driver(struct device_driver *drv)
  1282. {
  1283. drv->bus = &scsi_bus_type;
  1284. return driver_register(drv);
  1285. }
  1286. EXPORT_SYMBOL(scsi_register_driver);
  1287. int scsi_register_interface(struct class_interface *intf)
  1288. {
  1289. intf->class = &sdev_class;
  1290. return class_interface_register(intf);
  1291. }
  1292. EXPORT_SYMBOL(scsi_register_interface);
  1293. /**
  1294. * scsi_sysfs_add_host - add scsi host to subsystem
  1295. * @shost: scsi host struct to add to subsystem
  1296. **/
  1297. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  1298. {
  1299. int error, i;
  1300. /* add host specific attributes */
  1301. if (shost->hostt->shost_attrs) {
  1302. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  1303. error = device_create_file(&shost->shost_dev,
  1304. shost->hostt->shost_attrs[i]);
  1305. if (error)
  1306. return error;
  1307. }
  1308. }
  1309. transport_register_device(&shost->shost_gendev);
  1310. transport_configure_device(&shost->shost_gendev);
  1311. return 0;
  1312. }
  1313. static struct device_type scsi_dev_type = {
  1314. .name = "scsi_device",
  1315. .release = scsi_device_dev_release,
  1316. .groups = scsi_sdev_attr_groups,
  1317. };
  1318. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  1319. {
  1320. unsigned long flags;
  1321. struct Scsi_Host *shost = sdev->host;
  1322. struct scsi_target *starget = sdev->sdev_target;
  1323. device_initialize(&sdev->sdev_gendev);
  1324. sdev->sdev_gendev.bus = &scsi_bus_type;
  1325. sdev->sdev_gendev.type = &scsi_dev_type;
  1326. dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
  1327. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  1328. device_initialize(&sdev->sdev_dev);
  1329. sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
  1330. sdev->sdev_dev.class = &sdev_class;
  1331. dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
  1332. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  1333. /*
  1334. * Get a default scsi_level from the target (derived from sibling
  1335. * devices). This is the best we can do for guessing how to set
  1336. * sdev->lun_in_cdb for the initial INQUIRY command. For LUN 0 the
  1337. * setting doesn't matter, because all the bits are zero anyway.
  1338. * But it does matter for higher LUNs.
  1339. */
  1340. sdev->scsi_level = starget->scsi_level;
  1341. if (sdev->scsi_level <= SCSI_2 &&
  1342. sdev->scsi_level != SCSI_UNKNOWN &&
  1343. !shost->no_scsi2_lun_in_cdb)
  1344. sdev->lun_in_cdb = 1;
  1345. transport_setup_device(&sdev->sdev_gendev);
  1346. spin_lock_irqsave(shost->host_lock, flags);
  1347. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  1348. list_add_tail(&sdev->siblings, &shost->__devices);
  1349. spin_unlock_irqrestore(shost->host_lock, flags);
  1350. /*
  1351. * device can now only be removed via __scsi_remove_device() so hold
  1352. * the target. Target will be held in CREATED state until something
  1353. * beneath it becomes visible (in which case it moves to RUNNING)
  1354. */
  1355. kref_get(&starget->reap_ref);
  1356. }
  1357. int scsi_is_sdev_device(const struct device *dev)
  1358. {
  1359. return dev->type == &scsi_dev_type;
  1360. }
  1361. EXPORT_SYMBOL(scsi_is_sdev_device);
  1362. /* A blank transport template that is used in drivers that don't
  1363. * yet implement Transport Attributes */
  1364. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };