scsi_sysfs.c 27 KB

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