scsi_sysfs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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_transport.h>
  19. #include <scsi/scsi_driver.h>
  20. #include "scsi_priv.h"
  21. #include "scsi_logging.h"
  22. static struct device_type scsi_dev_type;
  23. static const struct {
  24. enum scsi_device_state value;
  25. char *name;
  26. } sdev_states[] = {
  27. { SDEV_CREATED, "created" },
  28. { SDEV_RUNNING, "running" },
  29. { SDEV_CANCEL, "cancel" },
  30. { SDEV_DEL, "deleted" },
  31. { SDEV_QUIESCE, "quiesce" },
  32. { SDEV_OFFLINE, "offline" },
  33. { SDEV_TRANSPORT_OFFLINE, "transport-offline" },
  34. { SDEV_BLOCK, "blocked" },
  35. { SDEV_CREATED_BLOCK, "created-blocked" },
  36. };
  37. const char *scsi_device_state_name(enum scsi_device_state state)
  38. {
  39. int i;
  40. char *name = NULL;
  41. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  42. if (sdev_states[i].value == state) {
  43. name = sdev_states[i].name;
  44. break;
  45. }
  46. }
  47. return name;
  48. }
  49. static const struct {
  50. enum scsi_host_state value;
  51. char *name;
  52. } shost_states[] = {
  53. { SHOST_CREATED, "created" },
  54. { SHOST_RUNNING, "running" },
  55. { SHOST_CANCEL, "cancel" },
  56. { SHOST_DEL, "deleted" },
  57. { SHOST_RECOVERY, "recovery" },
  58. { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
  59. { SHOST_DEL_RECOVERY, "deleted/recovery", },
  60. };
  61. const char *scsi_host_state_name(enum scsi_host_state state)
  62. {
  63. int i;
  64. char *name = NULL;
  65. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  66. if (shost_states[i].value == state) {
  67. name = shost_states[i].name;
  68. break;
  69. }
  70. }
  71. return name;
  72. }
  73. static int check_set(unsigned int *val, char *src)
  74. {
  75. char *last;
  76. if (strncmp(src, "-", 20) == 0) {
  77. *val = SCAN_WILD_CARD;
  78. } else {
  79. /*
  80. * Doesn't check for int overflow
  81. */
  82. *val = simple_strtoul(src, &last, 0);
  83. if (*last != '\0')
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. static int scsi_scan(struct Scsi_Host *shost, const char *str)
  89. {
  90. char s1[15], s2[15], s3[15], junk;
  91. unsigned int channel, id, lun;
  92. int res;
  93. res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
  94. if (res != 3)
  95. return -EINVAL;
  96. if (check_set(&channel, s1))
  97. return -EINVAL;
  98. if (check_set(&id, s2))
  99. return -EINVAL;
  100. if (check_set(&lun, s3))
  101. return -EINVAL;
  102. if (shost->transportt->user_scan)
  103. res = shost->transportt->user_scan(shost, channel, id, lun);
  104. else
  105. res = scsi_scan_host_selected(shost, channel, id, lun, 1);
  106. return res;
  107. }
  108. /*
  109. * shost_show_function: macro to create an attr function that can be used to
  110. * show a non-bit field.
  111. */
  112. #define shost_show_function(name, field, format_string) \
  113. static ssize_t \
  114. show_##name (struct device *dev, struct device_attribute *attr, \
  115. char *buf) \
  116. { \
  117. struct Scsi_Host *shost = class_to_shost(dev); \
  118. return snprintf (buf, 20, format_string, shost->field); \
  119. }
  120. /*
  121. * shost_rd_attr: macro to create a function and attribute variable for a
  122. * read only field.
  123. */
  124. #define shost_rd_attr2(name, field, format_string) \
  125. shost_show_function(name, field, format_string) \
  126. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  127. #define shost_rd_attr(field, format_string) \
  128. shost_rd_attr2(field, field, format_string)
  129. /*
  130. * Create the actual show/store functions and data structures.
  131. */
  132. static ssize_t
  133. store_scan(struct device *dev, struct device_attribute *attr,
  134. const char *buf, size_t count)
  135. {
  136. struct Scsi_Host *shost = class_to_shost(dev);
  137. int res;
  138. res = scsi_scan(shost, buf);
  139. if (res == 0)
  140. res = count;
  141. return res;
  142. };
  143. static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
  144. static ssize_t
  145. store_shost_state(struct device *dev, struct device_attribute *attr,
  146. const char *buf, size_t count)
  147. {
  148. int i;
  149. struct Scsi_Host *shost = class_to_shost(dev);
  150. enum scsi_host_state state = 0;
  151. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  152. const int len = strlen(shost_states[i].name);
  153. if (strncmp(shost_states[i].name, buf, len) == 0 &&
  154. buf[len] == '\n') {
  155. state = shost_states[i].value;
  156. break;
  157. }
  158. }
  159. if (!state)
  160. return -EINVAL;
  161. if (scsi_host_set_state(shost, state))
  162. return -EINVAL;
  163. return count;
  164. }
  165. static ssize_t
  166. show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
  167. {
  168. struct Scsi_Host *shost = class_to_shost(dev);
  169. const char *name = scsi_host_state_name(shost->shost_state);
  170. if (!name)
  171. return -EINVAL;
  172. return snprintf(buf, 20, "%s\n", name);
  173. }
  174. /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
  175. struct device_attribute dev_attr_hstate =
  176. __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
  177. static ssize_t
  178. show_shost_mode(unsigned int mode, char *buf)
  179. {
  180. ssize_t len = 0;
  181. if (mode & MODE_INITIATOR)
  182. len = sprintf(buf, "%s", "Initiator");
  183. if (mode & MODE_TARGET)
  184. len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
  185. len += sprintf(buf + len, "\n");
  186. return len;
  187. }
  188. static ssize_t
  189. show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
  190. char *buf)
  191. {
  192. struct Scsi_Host *shost = class_to_shost(dev);
  193. unsigned int supported_mode = shost->hostt->supported_mode;
  194. if (supported_mode == MODE_UNKNOWN)
  195. /* by default this should be initiator */
  196. supported_mode = MODE_INITIATOR;
  197. return show_shost_mode(supported_mode, buf);
  198. }
  199. static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
  200. static ssize_t
  201. show_shost_active_mode(struct device *dev,
  202. struct device_attribute *attr, char *buf)
  203. {
  204. struct Scsi_Host *shost = class_to_shost(dev);
  205. if (shost->active_mode == MODE_UNKNOWN)
  206. return snprintf(buf, 20, "unknown\n");
  207. else
  208. return show_shost_mode(shost->active_mode, buf);
  209. }
  210. static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
  211. static int check_reset_type(char *str)
  212. {
  213. if (strncmp(str, "adapter", 10) == 0)
  214. return SCSI_ADAPTER_RESET;
  215. else if (strncmp(str, "firmware", 10) == 0)
  216. return SCSI_FIRMWARE_RESET;
  217. else
  218. return 0;
  219. }
  220. static ssize_t
  221. store_host_reset(struct device *dev, struct device_attribute *attr,
  222. const char *buf, size_t count)
  223. {
  224. struct Scsi_Host *shost = class_to_shost(dev);
  225. struct scsi_host_template *sht = shost->hostt;
  226. int ret = -EINVAL;
  227. char str[10];
  228. int type;
  229. sscanf(buf, "%s", str);
  230. type = check_reset_type(str);
  231. if (!type)
  232. goto exit_store_host_reset;
  233. if (sht->host_reset)
  234. ret = sht->host_reset(shost, type);
  235. exit_store_host_reset:
  236. if (ret == 0)
  237. ret = count;
  238. return ret;
  239. }
  240. static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
  241. shost_rd_attr(unique_id, "%u\n");
  242. shost_rd_attr(host_busy, "%hu\n");
  243. shost_rd_attr(cmd_per_lun, "%hd\n");
  244. shost_rd_attr(can_queue, "%hd\n");
  245. shost_rd_attr(sg_tablesize, "%hu\n");
  246. shost_rd_attr(sg_prot_tablesize, "%hu\n");
  247. shost_rd_attr(unchecked_isa_dma, "%d\n");
  248. shost_rd_attr(prot_capabilities, "%u\n");
  249. shost_rd_attr(prot_guard_type, "%hd\n");
  250. shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
  251. static struct attribute *scsi_sysfs_shost_attrs[] = {
  252. &dev_attr_unique_id.attr,
  253. &dev_attr_host_busy.attr,
  254. &dev_attr_cmd_per_lun.attr,
  255. &dev_attr_can_queue.attr,
  256. &dev_attr_sg_tablesize.attr,
  257. &dev_attr_sg_prot_tablesize.attr,
  258. &dev_attr_unchecked_isa_dma.attr,
  259. &dev_attr_proc_name.attr,
  260. &dev_attr_scan.attr,
  261. &dev_attr_hstate.attr,
  262. &dev_attr_supported_mode.attr,
  263. &dev_attr_active_mode.attr,
  264. &dev_attr_prot_capabilities.attr,
  265. &dev_attr_prot_guard_type.attr,
  266. &dev_attr_host_reset.attr,
  267. NULL
  268. };
  269. struct attribute_group scsi_shost_attr_group = {
  270. .attrs = scsi_sysfs_shost_attrs,
  271. };
  272. const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
  273. &scsi_shost_attr_group,
  274. NULL
  275. };
  276. static void scsi_device_cls_release(struct device *class_dev)
  277. {
  278. struct scsi_device *sdev;
  279. sdev = class_to_sdev(class_dev);
  280. put_device(&sdev->sdev_gendev);
  281. }
  282. static void scsi_device_dev_release_usercontext(struct work_struct *work)
  283. {
  284. struct scsi_device *sdev;
  285. struct device *parent;
  286. struct scsi_target *starget;
  287. struct list_head *this, *tmp;
  288. unsigned long flags;
  289. sdev = container_of(work, struct scsi_device, ew.work);
  290. parent = sdev->sdev_gendev.parent;
  291. starget = to_scsi_target(parent);
  292. spin_lock_irqsave(sdev->host->host_lock, flags);
  293. starget->reap_ref++;
  294. list_del(&sdev->siblings);
  295. list_del(&sdev->same_target_siblings);
  296. list_del(&sdev->starved_entry);
  297. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  298. cancel_work_sync(&sdev->event_work);
  299. list_for_each_safe(this, tmp, &sdev->event_list) {
  300. struct scsi_event *evt;
  301. evt = list_entry(this, struct scsi_event, node);
  302. list_del(&evt->node);
  303. kfree(evt);
  304. }
  305. blk_put_queue(sdev->request_queue);
  306. /* NULL queue means the device can't be used */
  307. sdev->request_queue = NULL;
  308. scsi_target_reap(scsi_target(sdev));
  309. kfree(sdev->inquiry);
  310. kfree(sdev);
  311. if (parent)
  312. put_device(parent);
  313. }
  314. static void scsi_device_dev_release(struct device *dev)
  315. {
  316. struct scsi_device *sdp = to_scsi_device(dev);
  317. execute_in_process_context(scsi_device_dev_release_usercontext,
  318. &sdp->ew);
  319. }
  320. static struct class sdev_class = {
  321. .name = "scsi_device",
  322. .dev_release = scsi_device_cls_release,
  323. };
  324. /* all probing is done in the individual ->probe routines */
  325. static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
  326. {
  327. struct scsi_device *sdp;
  328. if (dev->type != &scsi_dev_type)
  329. return 0;
  330. sdp = to_scsi_device(dev);
  331. if (sdp->no_uld_attach)
  332. return 0;
  333. return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
  334. }
  335. static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  336. {
  337. struct scsi_device *sdev;
  338. if (dev->type != &scsi_dev_type)
  339. return 0;
  340. sdev = to_scsi_device(dev);
  341. add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
  342. return 0;
  343. }
  344. struct bus_type scsi_bus_type = {
  345. .name = "scsi",
  346. .match = scsi_bus_match,
  347. .uevent = scsi_bus_uevent,
  348. #ifdef CONFIG_PM
  349. .pm = &scsi_bus_pm_ops,
  350. #endif
  351. };
  352. EXPORT_SYMBOL_GPL(scsi_bus_type);
  353. int scsi_sysfs_register(void)
  354. {
  355. int error;
  356. error = bus_register(&scsi_bus_type);
  357. if (!error) {
  358. error = class_register(&sdev_class);
  359. if (error)
  360. bus_unregister(&scsi_bus_type);
  361. }
  362. return error;
  363. }
  364. void scsi_sysfs_unregister(void)
  365. {
  366. class_unregister(&sdev_class);
  367. bus_unregister(&scsi_bus_type);
  368. }
  369. /*
  370. * sdev_show_function: macro to create an attr function that can be used to
  371. * show a non-bit field.
  372. */
  373. #define sdev_show_function(field, format_string) \
  374. static ssize_t \
  375. sdev_show_##field (struct device *dev, struct device_attribute *attr, \
  376. char *buf) \
  377. { \
  378. struct scsi_device *sdev; \
  379. sdev = to_scsi_device(dev); \
  380. return snprintf (buf, 20, format_string, sdev->field); \
  381. } \
  382. /*
  383. * sdev_rd_attr: macro to create a function and attribute variable for a
  384. * read only field.
  385. */
  386. #define sdev_rd_attr(field, format_string) \
  387. sdev_show_function(field, format_string) \
  388. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  389. /*
  390. * sdev_rw_attr: create a function and attribute variable for a
  391. * read/write field.
  392. */
  393. #define sdev_rw_attr(field, format_string) \
  394. sdev_show_function(field, format_string) \
  395. \
  396. static ssize_t \
  397. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  398. const char *buf, size_t count) \
  399. { \
  400. struct scsi_device *sdev; \
  401. sdev = to_scsi_device(dev); \
  402. sscanf (buf, format_string, &sdev->field); \
  403. return count; \
  404. } \
  405. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  406. /* Currently we don't export bit fields, but we might in future,
  407. * so leave this code in */
  408. #if 0
  409. /*
  410. * sdev_rd_attr: create a function and attribute variable for a
  411. * read/write bit field.
  412. */
  413. #define sdev_rw_attr_bit(field) \
  414. sdev_show_function(field, "%d\n") \
  415. \
  416. static ssize_t \
  417. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  418. const char *buf, size_t count) \
  419. { \
  420. int ret; \
  421. struct scsi_device *sdev; \
  422. ret = scsi_sdev_check_buf_bit(buf); \
  423. if (ret >= 0) { \
  424. sdev = to_scsi_device(dev); \
  425. sdev->field = ret; \
  426. ret = count; \
  427. } \
  428. return ret; \
  429. } \
  430. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  431. /*
  432. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  433. * else return -EINVAL.
  434. */
  435. static int scsi_sdev_check_buf_bit(const char *buf)
  436. {
  437. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  438. if (buf[0] == '1')
  439. return 1;
  440. else if (buf[0] == '0')
  441. return 0;
  442. else
  443. return -EINVAL;
  444. } else
  445. return -EINVAL;
  446. }
  447. #endif
  448. /*
  449. * Create the actual show/store functions and data structures.
  450. */
  451. sdev_rd_attr (device_blocked, "%d\n");
  452. sdev_rd_attr (queue_depth, "%d\n");
  453. sdev_rd_attr (type, "%d\n");
  454. sdev_rd_attr (scsi_level, "%d\n");
  455. sdev_rd_attr (vendor, "%.8s\n");
  456. sdev_rd_attr (model, "%.16s\n");
  457. sdev_rd_attr (rev, "%.4s\n");
  458. /*
  459. * TODO: can we make these symlinks to the block layer ones?
  460. */
  461. static ssize_t
  462. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  463. {
  464. struct scsi_device *sdev;
  465. sdev = to_scsi_device(dev);
  466. return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
  467. }
  468. static ssize_t
  469. sdev_store_timeout (struct device *dev, struct device_attribute *attr,
  470. const char *buf, size_t count)
  471. {
  472. struct scsi_device *sdev;
  473. int timeout;
  474. sdev = to_scsi_device(dev);
  475. sscanf (buf, "%d\n", &timeout);
  476. blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
  477. return count;
  478. }
  479. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  480. static ssize_t
  481. store_rescan_field (struct device *dev, struct device_attribute *attr,
  482. const char *buf, size_t count)
  483. {
  484. scsi_rescan_device(dev);
  485. return count;
  486. }
  487. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  488. static void sdev_store_delete_callback(struct device *dev)
  489. {
  490. scsi_remove_device(to_scsi_device(dev));
  491. }
  492. static ssize_t
  493. sdev_store_delete(struct device *dev, struct device_attribute *attr,
  494. const char *buf, size_t count)
  495. {
  496. int rc;
  497. /* An attribute cannot be unregistered by one of its own methods,
  498. * so we have to use this roundabout approach.
  499. */
  500. rc = device_schedule_callback(dev, sdev_store_delete_callback);
  501. if (rc)
  502. count = rc;
  503. return count;
  504. };
  505. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  506. static ssize_t
  507. store_state_field(struct device *dev, struct device_attribute *attr,
  508. const char *buf, size_t count)
  509. {
  510. int i;
  511. struct scsi_device *sdev = to_scsi_device(dev);
  512. enum scsi_device_state state = 0;
  513. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  514. const int len = strlen(sdev_states[i].name);
  515. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  516. buf[len] == '\n') {
  517. state = sdev_states[i].value;
  518. break;
  519. }
  520. }
  521. if (!state)
  522. return -EINVAL;
  523. if (scsi_device_set_state(sdev, state))
  524. return -EINVAL;
  525. return count;
  526. }
  527. static ssize_t
  528. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  529. {
  530. struct scsi_device *sdev = to_scsi_device(dev);
  531. const char *name = scsi_device_state_name(sdev->sdev_state);
  532. if (!name)
  533. return -EINVAL;
  534. return snprintf(buf, 20, "%s\n", name);
  535. }
  536. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  537. static ssize_t
  538. show_queue_type_field(struct device *dev, struct device_attribute *attr,
  539. char *buf)
  540. {
  541. struct scsi_device *sdev = to_scsi_device(dev);
  542. const char *name = "none";
  543. if (sdev->ordered_tags)
  544. name = "ordered";
  545. else if (sdev->simple_tags)
  546. name = "simple";
  547. return snprintf(buf, 20, "%s\n", name);
  548. }
  549. static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
  550. static ssize_t
  551. show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
  552. {
  553. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  554. }
  555. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  556. #define show_sdev_iostat(field) \
  557. static ssize_t \
  558. show_iostat_##field(struct device *dev, struct device_attribute *attr, \
  559. char *buf) \
  560. { \
  561. struct scsi_device *sdev = to_scsi_device(dev); \
  562. unsigned long long count = atomic_read(&sdev->field); \
  563. return snprintf(buf, 20, "0x%llx\n", count); \
  564. } \
  565. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  566. show_sdev_iostat(iorequest_cnt);
  567. show_sdev_iostat(iodone_cnt);
  568. show_sdev_iostat(ioerr_cnt);
  569. static ssize_t
  570. sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  571. {
  572. struct scsi_device *sdev;
  573. sdev = to_scsi_device(dev);
  574. return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
  575. }
  576. static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
  577. #define DECLARE_EVT_SHOW(name, Cap_name) \
  578. static ssize_t \
  579. sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
  580. char *buf) \
  581. { \
  582. struct scsi_device *sdev = to_scsi_device(dev); \
  583. int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
  584. return snprintf(buf, 20, "%d\n", val); \
  585. }
  586. #define DECLARE_EVT_STORE(name, Cap_name) \
  587. static ssize_t \
  588. sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
  589. const char *buf, size_t count) \
  590. { \
  591. struct scsi_device *sdev = to_scsi_device(dev); \
  592. int val = simple_strtoul(buf, NULL, 0); \
  593. if (val == 0) \
  594. clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  595. else if (val == 1) \
  596. set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  597. else \
  598. return -EINVAL; \
  599. return count; \
  600. }
  601. #define DECLARE_EVT(name, Cap_name) \
  602. DECLARE_EVT_SHOW(name, Cap_name) \
  603. DECLARE_EVT_STORE(name, Cap_name) \
  604. static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
  605. sdev_store_evt_##name);
  606. #define REF_EVT(name) &dev_attr_evt_##name.attr
  607. DECLARE_EVT(media_change, MEDIA_CHANGE)
  608. /* Default template for device attributes. May NOT be modified */
  609. static struct attribute *scsi_sdev_attrs[] = {
  610. &dev_attr_device_blocked.attr,
  611. &dev_attr_type.attr,
  612. &dev_attr_scsi_level.attr,
  613. &dev_attr_vendor.attr,
  614. &dev_attr_model.attr,
  615. &dev_attr_rev.attr,
  616. &dev_attr_rescan.attr,
  617. &dev_attr_delete.attr,
  618. &dev_attr_state.attr,
  619. &dev_attr_timeout.attr,
  620. &dev_attr_iocounterbits.attr,
  621. &dev_attr_iorequest_cnt.attr,
  622. &dev_attr_iodone_cnt.attr,
  623. &dev_attr_ioerr_cnt.attr,
  624. &dev_attr_modalias.attr,
  625. REF_EVT(media_change),
  626. NULL
  627. };
  628. static struct attribute_group scsi_sdev_attr_group = {
  629. .attrs = scsi_sdev_attrs,
  630. };
  631. static const struct attribute_group *scsi_sdev_attr_groups[] = {
  632. &scsi_sdev_attr_group,
  633. NULL
  634. };
  635. static ssize_t
  636. sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
  637. const char *buf, size_t count)
  638. {
  639. int depth, retval;
  640. struct scsi_device *sdev = to_scsi_device(dev);
  641. struct scsi_host_template *sht = sdev->host->hostt;
  642. if (!sht->change_queue_depth)
  643. return -EINVAL;
  644. depth = simple_strtoul(buf, NULL, 0);
  645. if (depth < 1)
  646. return -EINVAL;
  647. retval = sht->change_queue_depth(sdev, depth,
  648. SCSI_QDEPTH_DEFAULT);
  649. if (retval < 0)
  650. return retval;
  651. sdev->max_queue_depth = sdev->queue_depth;
  652. return count;
  653. }
  654. static struct device_attribute sdev_attr_queue_depth_rw =
  655. __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  656. sdev_store_queue_depth_rw);
  657. static ssize_t
  658. sdev_show_queue_ramp_up_period(struct device *dev,
  659. struct device_attribute *attr,
  660. char *buf)
  661. {
  662. struct scsi_device *sdev;
  663. sdev = to_scsi_device(dev);
  664. return snprintf(buf, 20, "%u\n",
  665. jiffies_to_msecs(sdev->queue_ramp_up_period));
  666. }
  667. static ssize_t
  668. sdev_store_queue_ramp_up_period(struct device *dev,
  669. struct device_attribute *attr,
  670. const char *buf, size_t count)
  671. {
  672. struct scsi_device *sdev = to_scsi_device(dev);
  673. unsigned long period;
  674. if (strict_strtoul(buf, 10, &period))
  675. return -EINVAL;
  676. sdev->queue_ramp_up_period = msecs_to_jiffies(period);
  677. return period;
  678. }
  679. static struct device_attribute sdev_attr_queue_ramp_up_period =
  680. __ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
  681. sdev_show_queue_ramp_up_period,
  682. sdev_store_queue_ramp_up_period);
  683. static ssize_t
  684. sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
  685. const char *buf, size_t count)
  686. {
  687. struct scsi_device *sdev = to_scsi_device(dev);
  688. struct scsi_host_template *sht = sdev->host->hostt;
  689. int tag_type = 0, retval;
  690. int prev_tag_type = scsi_get_tag_type(sdev);
  691. if (!sdev->tagged_supported || !sht->change_queue_type)
  692. return -EINVAL;
  693. if (strncmp(buf, "ordered", 7) == 0)
  694. tag_type = MSG_ORDERED_TAG;
  695. else if (strncmp(buf, "simple", 6) == 0)
  696. tag_type = MSG_SIMPLE_TAG;
  697. else if (strncmp(buf, "none", 4) != 0)
  698. return -EINVAL;
  699. if (tag_type == prev_tag_type)
  700. return count;
  701. retval = sht->change_queue_type(sdev, tag_type);
  702. if (retval < 0)
  703. return retval;
  704. return count;
  705. }
  706. static int scsi_target_add(struct scsi_target *starget)
  707. {
  708. int error;
  709. if (starget->state != STARGET_CREATED)
  710. return 0;
  711. error = device_add(&starget->dev);
  712. if (error) {
  713. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  714. return error;
  715. }
  716. transport_add_device(&starget->dev);
  717. starget->state = STARGET_RUNNING;
  718. pm_runtime_set_active(&starget->dev);
  719. pm_runtime_enable(&starget->dev);
  720. device_enable_async_suspend(&starget->dev);
  721. return 0;
  722. }
  723. static struct device_attribute sdev_attr_queue_type_rw =
  724. __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  725. sdev_store_queue_type_rw);
  726. /**
  727. * scsi_sysfs_add_sdev - add scsi device to sysfs
  728. * @sdev: scsi_device to add
  729. *
  730. * Return value:
  731. * 0 on Success / non-zero on Failure
  732. **/
  733. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  734. {
  735. int error, i;
  736. struct request_queue *rq = sdev->request_queue;
  737. struct scsi_target *starget = sdev->sdev_target;
  738. error = scsi_device_set_state(sdev, SDEV_RUNNING);
  739. if (error)
  740. return error;
  741. error = scsi_target_add(starget);
  742. if (error)
  743. return error;
  744. transport_configure_device(&starget->dev);
  745. device_enable_async_suspend(&sdev->sdev_gendev);
  746. scsi_autopm_get_target(starget);
  747. pm_runtime_set_active(&sdev->sdev_gendev);
  748. pm_runtime_forbid(&sdev->sdev_gendev);
  749. pm_runtime_enable(&sdev->sdev_gendev);
  750. scsi_autopm_put_target(starget);
  751. /* The following call will keep sdev active indefinitely, until
  752. * its driver does a corresponding scsi_autopm_pm_device(). Only
  753. * drivers supporting autosuspend will do this.
  754. */
  755. scsi_autopm_get_device(sdev);
  756. error = device_add(&sdev->sdev_gendev);
  757. if (error) {
  758. sdev_printk(KERN_INFO, sdev,
  759. "failed to add device: %d\n", error);
  760. return error;
  761. }
  762. device_enable_async_suspend(&sdev->sdev_dev);
  763. error = device_add(&sdev->sdev_dev);
  764. if (error) {
  765. sdev_printk(KERN_INFO, sdev,
  766. "failed to add class device: %d\n", error);
  767. device_del(&sdev->sdev_gendev);
  768. return error;
  769. }
  770. transport_add_device(&sdev->sdev_gendev);
  771. sdev->is_visible = 1;
  772. /* create queue files, which may be writable, depending on the host */
  773. if (sdev->host->hostt->change_queue_depth) {
  774. error = device_create_file(&sdev->sdev_gendev,
  775. &sdev_attr_queue_depth_rw);
  776. error = device_create_file(&sdev->sdev_gendev,
  777. &sdev_attr_queue_ramp_up_period);
  778. }
  779. else
  780. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
  781. if (error)
  782. return error;
  783. if (sdev->host->hostt->change_queue_type)
  784. error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
  785. else
  786. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
  787. if (error)
  788. return error;
  789. error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
  790. if (error)
  791. /* we're treating error on bsg register as non-fatal,
  792. * so pretend nothing went wrong */
  793. sdev_printk(KERN_INFO, sdev,
  794. "Failed to register bsg queue, errno=%d\n", error);
  795. /* add additional host specific attributes */
  796. if (sdev->host->hostt->sdev_attrs) {
  797. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  798. error = device_create_file(&sdev->sdev_gendev,
  799. sdev->host->hostt->sdev_attrs[i]);
  800. if (error)
  801. return error;
  802. }
  803. }
  804. return error;
  805. }
  806. void __scsi_remove_device(struct scsi_device *sdev)
  807. {
  808. struct device *dev = &sdev->sdev_gendev;
  809. if (sdev->is_visible) {
  810. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  811. return;
  812. bsg_unregister_queue(sdev->request_queue);
  813. device_unregister(&sdev->sdev_dev);
  814. transport_remove_device(dev);
  815. device_del(dev);
  816. } else
  817. put_device(&sdev->sdev_dev);
  818. /*
  819. * Stop accepting new requests and wait until all queuecommand() and
  820. * scsi_run_queue() invocations have finished before tearing down the
  821. * device.
  822. */
  823. scsi_device_set_state(sdev, SDEV_DEL);
  824. blk_cleanup_queue(sdev->request_queue);
  825. cancel_work_sync(&sdev->requeue_work);
  826. if (sdev->host->hostt->slave_destroy)
  827. sdev->host->hostt->slave_destroy(sdev);
  828. transport_destroy_device(dev);
  829. put_device(dev);
  830. }
  831. /**
  832. * scsi_remove_device - unregister a device from the scsi bus
  833. * @sdev: scsi_device to unregister
  834. **/
  835. void scsi_remove_device(struct scsi_device *sdev)
  836. {
  837. struct Scsi_Host *shost = sdev->host;
  838. mutex_lock(&shost->scan_mutex);
  839. __scsi_remove_device(sdev);
  840. mutex_unlock(&shost->scan_mutex);
  841. }
  842. EXPORT_SYMBOL(scsi_remove_device);
  843. static void __scsi_remove_target(struct scsi_target *starget)
  844. {
  845. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  846. unsigned long flags;
  847. struct scsi_device *sdev;
  848. spin_lock_irqsave(shost->host_lock, flags);
  849. restart:
  850. list_for_each_entry(sdev, &shost->__devices, siblings) {
  851. if (sdev->channel != starget->channel ||
  852. sdev->id != starget->id ||
  853. scsi_device_get(sdev))
  854. continue;
  855. spin_unlock_irqrestore(shost->host_lock, flags);
  856. scsi_remove_device(sdev);
  857. scsi_device_put(sdev);
  858. spin_lock_irqsave(shost->host_lock, flags);
  859. goto restart;
  860. }
  861. spin_unlock_irqrestore(shost->host_lock, flags);
  862. }
  863. /**
  864. * scsi_remove_target - try to remove a target and all its devices
  865. * @dev: generic starget or parent of generic stargets to be removed
  866. *
  867. * Note: This is slightly racy. It is possible that if the user
  868. * requests the addition of another device then the target won't be
  869. * removed.
  870. */
  871. void scsi_remove_target(struct device *dev)
  872. {
  873. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  874. struct scsi_target *starget, *found;
  875. unsigned long flags;
  876. restart:
  877. found = NULL;
  878. spin_lock_irqsave(shost->host_lock, flags);
  879. list_for_each_entry(starget, &shost->__targets, siblings) {
  880. if (starget->state == STARGET_DEL)
  881. continue;
  882. if (starget->dev.parent == dev || &starget->dev == dev) {
  883. found = starget;
  884. found->reap_ref++;
  885. break;
  886. }
  887. }
  888. spin_unlock_irqrestore(shost->host_lock, flags);
  889. if (found) {
  890. __scsi_remove_target(found);
  891. scsi_target_reap(found);
  892. /* in the case where @dev has multiple starget children,
  893. * continue removing.
  894. *
  895. * FIXME: does such a case exist?
  896. */
  897. goto restart;
  898. }
  899. }
  900. EXPORT_SYMBOL(scsi_remove_target);
  901. int scsi_register_driver(struct device_driver *drv)
  902. {
  903. drv->bus = &scsi_bus_type;
  904. return driver_register(drv);
  905. }
  906. EXPORT_SYMBOL(scsi_register_driver);
  907. int scsi_register_interface(struct class_interface *intf)
  908. {
  909. intf->class = &sdev_class;
  910. return class_interface_register(intf);
  911. }
  912. EXPORT_SYMBOL(scsi_register_interface);
  913. /**
  914. * scsi_sysfs_add_host - add scsi host to subsystem
  915. * @shost: scsi host struct to add to subsystem
  916. **/
  917. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  918. {
  919. int error, i;
  920. /* add host specific attributes */
  921. if (shost->hostt->shost_attrs) {
  922. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  923. error = device_create_file(&shost->shost_dev,
  924. shost->hostt->shost_attrs[i]);
  925. if (error)
  926. return error;
  927. }
  928. }
  929. transport_register_device(&shost->shost_gendev);
  930. transport_configure_device(&shost->shost_gendev);
  931. return 0;
  932. }
  933. static struct device_type scsi_dev_type = {
  934. .name = "scsi_device",
  935. .release = scsi_device_dev_release,
  936. .groups = scsi_sdev_attr_groups,
  937. };
  938. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  939. {
  940. unsigned long flags;
  941. struct Scsi_Host *shost = sdev->host;
  942. struct scsi_target *starget = sdev->sdev_target;
  943. device_initialize(&sdev->sdev_gendev);
  944. sdev->sdev_gendev.bus = &scsi_bus_type;
  945. sdev->sdev_gendev.type = &scsi_dev_type;
  946. dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
  947. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  948. device_initialize(&sdev->sdev_dev);
  949. sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
  950. sdev->sdev_dev.class = &sdev_class;
  951. dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
  952. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  953. sdev->scsi_level = starget->scsi_level;
  954. transport_setup_device(&sdev->sdev_gendev);
  955. spin_lock_irqsave(shost->host_lock, flags);
  956. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  957. list_add_tail(&sdev->siblings, &shost->__devices);
  958. spin_unlock_irqrestore(shost->host_lock, flags);
  959. }
  960. int scsi_is_sdev_device(const struct device *dev)
  961. {
  962. return dev->type == &scsi_dev_type;
  963. }
  964. EXPORT_SYMBOL(scsi_is_sdev_device);
  965. /* A blank transport template that is used in drivers that don't
  966. * yet implement Transport Attributes */
  967. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };