scsi_sysfs.c 27 KB

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