scsi_sysfs.c 39 KB

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