scsi_sysfs.c 44 KB

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