scsi_sysfs.c 28 KB

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