scsi_sysfs.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. };
  33. const char *scsi_device_state_name(enum scsi_device_state state)
  34. {
  35. int i;
  36. char *name = NULL;
  37. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  38. if (sdev_states[i].value == state) {
  39. name = sdev_states[i].name;
  40. break;
  41. }
  42. }
  43. return name;
  44. }
  45. static const struct {
  46. enum scsi_host_state value;
  47. char *name;
  48. } shost_states[] = {
  49. { SHOST_CREATED, "created" },
  50. { SHOST_RUNNING, "running" },
  51. { SHOST_CANCEL, "cancel" },
  52. { SHOST_DEL, "deleted" },
  53. { SHOST_RECOVERY, "recovery" },
  54. { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
  55. { SHOST_DEL_RECOVERY, "deleted/recovery", },
  56. };
  57. const char *scsi_host_state_name(enum scsi_host_state state)
  58. {
  59. int i;
  60. char *name = NULL;
  61. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  62. if (shost_states[i].value == state) {
  63. name = shost_states[i].name;
  64. break;
  65. }
  66. }
  67. return name;
  68. }
  69. static int check_set(unsigned int *val, char *src)
  70. {
  71. char *last;
  72. if (strncmp(src, "-", 20) == 0) {
  73. *val = SCAN_WILD_CARD;
  74. } else {
  75. /*
  76. * Doesn't check for int overflow
  77. */
  78. *val = simple_strtoul(src, &last, 0);
  79. if (*last != '\0')
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. static int scsi_scan(struct Scsi_Host *shost, const char *str)
  85. {
  86. char s1[15], s2[15], s3[15], junk;
  87. unsigned int channel, id, lun;
  88. int res;
  89. res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
  90. if (res != 3)
  91. return -EINVAL;
  92. if (check_set(&channel, s1))
  93. return -EINVAL;
  94. if (check_set(&id, s2))
  95. return -EINVAL;
  96. if (check_set(&lun, s3))
  97. return -EINVAL;
  98. if (shost->transportt->user_scan)
  99. res = shost->transportt->user_scan(shost, channel, id, lun);
  100. else
  101. res = scsi_scan_host_selected(shost, channel, id, lun, 1);
  102. return res;
  103. }
  104. /*
  105. * shost_show_function: macro to create an attr function that can be used to
  106. * show a non-bit field.
  107. */
  108. #define shost_show_function(name, field, format_string) \
  109. static ssize_t \
  110. show_##name (struct device *dev, struct device_attribute *attr, \
  111. char *buf) \
  112. { \
  113. struct Scsi_Host *shost = class_to_shost(dev); \
  114. return snprintf (buf, 20, format_string, shost->field); \
  115. }
  116. /*
  117. * shost_rd_attr: macro to create a function and attribute variable for a
  118. * read only field.
  119. */
  120. #define shost_rd_attr2(name, field, format_string) \
  121. shost_show_function(name, field, format_string) \
  122. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  123. #define shost_rd_attr(field, format_string) \
  124. shost_rd_attr2(field, field, format_string)
  125. /*
  126. * Create the actual show/store functions and data structures.
  127. */
  128. static ssize_t
  129. store_scan(struct device *dev, struct device_attribute *attr,
  130. const char *buf, size_t count)
  131. {
  132. struct Scsi_Host *shost = class_to_shost(dev);
  133. int res;
  134. res = scsi_scan(shost, buf);
  135. if (res == 0)
  136. res = count;
  137. return res;
  138. };
  139. static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
  140. static ssize_t
  141. store_shost_state(struct device *dev, struct device_attribute *attr,
  142. const char *buf, size_t count)
  143. {
  144. int i;
  145. struct Scsi_Host *shost = class_to_shost(dev);
  146. enum scsi_host_state state = 0;
  147. for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  148. const int len = strlen(shost_states[i].name);
  149. if (strncmp(shost_states[i].name, buf, len) == 0 &&
  150. buf[len] == '\n') {
  151. state = shost_states[i].value;
  152. break;
  153. }
  154. }
  155. if (!state)
  156. return -EINVAL;
  157. if (scsi_host_set_state(shost, state))
  158. return -EINVAL;
  159. return count;
  160. }
  161. static ssize_t
  162. show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
  163. {
  164. struct Scsi_Host *shost = class_to_shost(dev);
  165. const char *name = scsi_host_state_name(shost->shost_state);
  166. if (!name)
  167. return -EINVAL;
  168. return snprintf(buf, 20, "%s\n", name);
  169. }
  170. /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
  171. struct device_attribute dev_attr_hstate =
  172. __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
  173. static ssize_t
  174. show_shost_mode(unsigned int mode, char *buf)
  175. {
  176. ssize_t len = 0;
  177. if (mode & MODE_INITIATOR)
  178. len = sprintf(buf, "%s", "Initiator");
  179. if (mode & MODE_TARGET)
  180. len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
  181. len += sprintf(buf + len, "\n");
  182. return len;
  183. }
  184. static ssize_t
  185. show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
  186. char *buf)
  187. {
  188. struct Scsi_Host *shost = class_to_shost(dev);
  189. unsigned int supported_mode = shost->hostt->supported_mode;
  190. if (supported_mode == MODE_UNKNOWN)
  191. /* by default this should be initiator */
  192. supported_mode = MODE_INITIATOR;
  193. return show_shost_mode(supported_mode, buf);
  194. }
  195. static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
  196. static ssize_t
  197. show_shost_active_mode(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct Scsi_Host *shost = class_to_shost(dev);
  201. if (shost->active_mode == MODE_UNKNOWN)
  202. return snprintf(buf, 20, "unknown\n");
  203. else
  204. return show_shost_mode(shost->active_mode, buf);
  205. }
  206. static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
  207. shost_rd_attr(unique_id, "%u\n");
  208. shost_rd_attr(host_busy, "%hu\n");
  209. shost_rd_attr(cmd_per_lun, "%hd\n");
  210. shost_rd_attr(can_queue, "%hd\n");
  211. shost_rd_attr(sg_tablesize, "%hu\n");
  212. shost_rd_attr(unchecked_isa_dma, "%d\n");
  213. shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
  214. static struct attribute *scsi_sysfs_shost_attrs[] = {
  215. &dev_attr_unique_id.attr,
  216. &dev_attr_host_busy.attr,
  217. &dev_attr_cmd_per_lun.attr,
  218. &dev_attr_can_queue.attr,
  219. &dev_attr_sg_tablesize.attr,
  220. &dev_attr_unchecked_isa_dma.attr,
  221. &dev_attr_proc_name.attr,
  222. &dev_attr_scan.attr,
  223. &dev_attr_hstate.attr,
  224. &dev_attr_supported_mode.attr,
  225. &dev_attr_active_mode.attr,
  226. NULL
  227. };
  228. struct attribute_group scsi_shost_attr_group = {
  229. .attrs = scsi_sysfs_shost_attrs,
  230. };
  231. struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
  232. &scsi_shost_attr_group,
  233. NULL
  234. };
  235. static void scsi_device_cls_release(struct device *class_dev)
  236. {
  237. struct scsi_device *sdev;
  238. sdev = class_to_sdev(class_dev);
  239. put_device(&sdev->sdev_gendev);
  240. }
  241. static void scsi_device_dev_release_usercontext(struct work_struct *work)
  242. {
  243. struct scsi_device *sdev;
  244. struct device *parent;
  245. struct scsi_target *starget;
  246. struct list_head *this, *tmp;
  247. unsigned long flags;
  248. sdev = container_of(work, struct scsi_device, ew.work);
  249. parent = sdev->sdev_gendev.parent;
  250. starget = to_scsi_target(parent);
  251. spin_lock_irqsave(sdev->host->host_lock, flags);
  252. starget->reap_ref++;
  253. list_del(&sdev->siblings);
  254. list_del(&sdev->same_target_siblings);
  255. list_del(&sdev->starved_entry);
  256. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  257. cancel_work_sync(&sdev->event_work);
  258. list_for_each_safe(this, tmp, &sdev->event_list) {
  259. struct scsi_event *evt;
  260. evt = list_entry(this, struct scsi_event, node);
  261. list_del(&evt->node);
  262. kfree(evt);
  263. }
  264. if (sdev->request_queue) {
  265. sdev->request_queue->queuedata = NULL;
  266. /* user context needed to free queue */
  267. scsi_free_queue(sdev->request_queue);
  268. /* temporary expedient, try to catch use of queue lock
  269. * after free of sdev */
  270. sdev->request_queue = NULL;
  271. }
  272. scsi_target_reap(scsi_target(sdev));
  273. kfree(sdev->inquiry);
  274. kfree(sdev);
  275. if (parent)
  276. put_device(parent);
  277. }
  278. static void scsi_device_dev_release(struct device *dev)
  279. {
  280. struct scsi_device *sdp = to_scsi_device(dev);
  281. execute_in_process_context(scsi_device_dev_release_usercontext,
  282. &sdp->ew);
  283. }
  284. static struct class sdev_class = {
  285. .name = "scsi_device",
  286. .dev_release = scsi_device_cls_release,
  287. };
  288. /* all probing is done in the individual ->probe routines */
  289. static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
  290. {
  291. struct scsi_device *sdp;
  292. if (dev->type != &scsi_dev_type)
  293. return 0;
  294. sdp = to_scsi_device(dev);
  295. if (sdp->no_uld_attach)
  296. return 0;
  297. return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
  298. }
  299. static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  300. {
  301. struct scsi_device *sdev = to_scsi_device(dev);
  302. add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
  303. return 0;
  304. }
  305. static int scsi_bus_suspend(struct device * dev, pm_message_t state)
  306. {
  307. struct device_driver *drv;
  308. struct scsi_device *sdev;
  309. int err;
  310. if (dev->type != &scsi_dev_type)
  311. return 0;
  312. drv = dev->driver;
  313. sdev = to_scsi_device(dev);
  314. err = scsi_device_quiesce(sdev);
  315. if (err)
  316. return err;
  317. if (drv && drv->suspend) {
  318. err = drv->suspend(dev, state);
  319. if (err)
  320. return err;
  321. }
  322. return 0;
  323. }
  324. static int scsi_bus_resume(struct device * dev)
  325. {
  326. struct device_driver *drv;
  327. struct scsi_device *sdev;
  328. int err = 0;
  329. if (dev->type != &scsi_dev_type)
  330. return 0;
  331. drv = dev->driver;
  332. sdev = to_scsi_device(dev);
  333. if (drv && drv->resume)
  334. err = drv->resume(dev);
  335. scsi_device_resume(sdev);
  336. return err;
  337. }
  338. static int scsi_bus_remove(struct device *dev)
  339. {
  340. struct device_driver *drv = dev->driver;
  341. struct scsi_device *sdev = to_scsi_device(dev);
  342. int err = 0;
  343. /* reset the prep_fn back to the default since the
  344. * driver may have altered it and it's being removed */
  345. blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
  346. if (drv && drv->remove)
  347. err = drv->remove(dev);
  348. return 0;
  349. }
  350. struct bus_type scsi_bus_type = {
  351. .name = "scsi",
  352. .match = scsi_bus_match,
  353. .uevent = scsi_bus_uevent,
  354. .suspend = scsi_bus_suspend,
  355. .resume = scsi_bus_resume,
  356. .remove = scsi_bus_remove,
  357. };
  358. int scsi_sysfs_register(void)
  359. {
  360. int error;
  361. error = bus_register(&scsi_bus_type);
  362. if (!error) {
  363. error = class_register(&sdev_class);
  364. if (error)
  365. bus_unregister(&scsi_bus_type);
  366. }
  367. return error;
  368. }
  369. void scsi_sysfs_unregister(void)
  370. {
  371. class_unregister(&sdev_class);
  372. bus_unregister(&scsi_bus_type);
  373. }
  374. /*
  375. * sdev_show_function: macro to create an attr function that can be used to
  376. * show a non-bit field.
  377. */
  378. #define sdev_show_function(field, format_string) \
  379. static ssize_t \
  380. sdev_show_##field (struct device *dev, struct device_attribute *attr, \
  381. char *buf) \
  382. { \
  383. struct scsi_device *sdev; \
  384. sdev = to_scsi_device(dev); \
  385. return snprintf (buf, 20, format_string, sdev->field); \
  386. } \
  387. /*
  388. * sdev_rd_attr: macro to create a function and attribute variable for a
  389. * read only field.
  390. */
  391. #define sdev_rd_attr(field, format_string) \
  392. sdev_show_function(field, format_string) \
  393. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  394. /*
  395. * sdev_rd_attr: create a function and attribute variable for a
  396. * read/write field.
  397. */
  398. #define sdev_rw_attr(field, format_string) \
  399. sdev_show_function(field, format_string) \
  400. \
  401. static ssize_t \
  402. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  403. const char *buf, size_t count) \
  404. { \
  405. struct scsi_device *sdev; \
  406. sdev = to_scsi_device(dev); \
  407. snscanf (buf, 20, format_string, &sdev->field); \
  408. return count; \
  409. } \
  410. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  411. /* Currently we don't export bit fields, but we might in future,
  412. * so leave this code in */
  413. #if 0
  414. /*
  415. * sdev_rd_attr: create a function and attribute variable for a
  416. * read/write bit field.
  417. */
  418. #define sdev_rw_attr_bit(field) \
  419. sdev_show_function(field, "%d\n") \
  420. \
  421. static ssize_t \
  422. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  423. const char *buf, size_t count) \
  424. { \
  425. int ret; \
  426. struct scsi_device *sdev; \
  427. ret = scsi_sdev_check_buf_bit(buf); \
  428. if (ret >= 0) { \
  429. sdev = to_scsi_device(dev); \
  430. sdev->field = ret; \
  431. ret = count; \
  432. } \
  433. return ret; \
  434. } \
  435. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  436. /*
  437. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  438. * else return -EINVAL.
  439. */
  440. static int scsi_sdev_check_buf_bit(const char *buf)
  441. {
  442. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  443. if (buf[0] == '1')
  444. return 1;
  445. else if (buf[0] == '0')
  446. return 0;
  447. else
  448. return -EINVAL;
  449. } else
  450. return -EINVAL;
  451. }
  452. #endif
  453. /*
  454. * Create the actual show/store functions and data structures.
  455. */
  456. sdev_rd_attr (device_blocked, "%d\n");
  457. sdev_rd_attr (queue_depth, "%d\n");
  458. sdev_rd_attr (type, "%d\n");
  459. sdev_rd_attr (scsi_level, "%d\n");
  460. sdev_rd_attr (vendor, "%.8s\n");
  461. sdev_rd_attr (model, "%.16s\n");
  462. sdev_rd_attr (rev, "%.4s\n");
  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->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. sdev->timeout = 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 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. if (retval < 0)
  651. return retval;
  652. return count;
  653. }
  654. static struct device_attribute sdev_attr_queue_depth_rw =
  655. __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  656. sdev_store_queue_depth_rw);
  657. static ssize_t
  658. sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
  659. const char *buf, size_t count)
  660. {
  661. struct scsi_device *sdev = to_scsi_device(dev);
  662. struct scsi_host_template *sht = sdev->host->hostt;
  663. int tag_type = 0, retval;
  664. int prev_tag_type = scsi_get_tag_type(sdev);
  665. if (!sdev->tagged_supported || !sht->change_queue_type)
  666. return -EINVAL;
  667. if (strncmp(buf, "ordered", 7) == 0)
  668. tag_type = MSG_ORDERED_TAG;
  669. else if (strncmp(buf, "simple", 6) == 0)
  670. tag_type = MSG_SIMPLE_TAG;
  671. else if (strncmp(buf, "none", 4) != 0)
  672. return -EINVAL;
  673. if (tag_type == prev_tag_type)
  674. return count;
  675. retval = sht->change_queue_type(sdev, tag_type);
  676. if (retval < 0)
  677. return retval;
  678. return count;
  679. }
  680. static int scsi_target_add(struct scsi_target *starget)
  681. {
  682. int error;
  683. if (starget->state != STARGET_CREATED)
  684. return 0;
  685. error = device_add(&starget->dev);
  686. if (error) {
  687. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  688. get_device(&starget->dev);
  689. scsi_target_reap(starget);
  690. put_device(&starget->dev);
  691. return error;
  692. }
  693. transport_add_device(&starget->dev);
  694. starget->state = STARGET_RUNNING;
  695. return 0;
  696. }
  697. static struct device_attribute sdev_attr_queue_type_rw =
  698. __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  699. sdev_store_queue_type_rw);
  700. /**
  701. * scsi_sysfs_add_sdev - add scsi device to sysfs
  702. * @sdev: scsi_device to add
  703. *
  704. * Return value:
  705. * 0 on Success / non-zero on Failure
  706. **/
  707. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  708. {
  709. int error, i;
  710. struct request_queue *rq = sdev->request_queue;
  711. struct scsi_target *starget = sdev->sdev_target;
  712. if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
  713. return error;
  714. error = scsi_target_add(starget);
  715. if (error)
  716. return error;
  717. transport_configure_device(&starget->dev);
  718. error = device_add(&sdev->sdev_gendev);
  719. if (error) {
  720. put_device(sdev->sdev_gendev.parent);
  721. printk(KERN_INFO "error 1\n");
  722. return error;
  723. }
  724. error = device_add(&sdev->sdev_dev);
  725. if (error) {
  726. printk(KERN_INFO "error 2\n");
  727. goto clean_device;
  728. }
  729. /* take a reference for the sdev_dev; this is
  730. * released by the sdev_class .release */
  731. get_device(&sdev->sdev_gendev);
  732. /* create queue files, which may be writable, depending on the host */
  733. if (sdev->host->hostt->change_queue_depth)
  734. error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
  735. else
  736. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
  737. if (error) {
  738. __scsi_remove_device(sdev);
  739. goto out;
  740. }
  741. if (sdev->host->hostt->change_queue_type)
  742. error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
  743. else
  744. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
  745. if (error) {
  746. __scsi_remove_device(sdev);
  747. goto out;
  748. }
  749. error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
  750. if (error)
  751. sdev_printk(KERN_INFO, sdev,
  752. "Failed to register bsg queue, errno=%d\n", error);
  753. /* we're treating error on bsg register as non-fatal, so pretend
  754. * nothing went wrong */
  755. error = 0;
  756. /* add additional host specific attributes */
  757. if (sdev->host->hostt->sdev_attrs) {
  758. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  759. error = device_create_file(&sdev->sdev_gendev,
  760. sdev->host->hostt->sdev_attrs[i]);
  761. if (error) {
  762. __scsi_remove_device(sdev);
  763. goto out;
  764. }
  765. }
  766. }
  767. transport_add_device(&sdev->sdev_gendev);
  768. out:
  769. return error;
  770. clean_device:
  771. scsi_device_set_state(sdev, SDEV_CANCEL);
  772. device_del(&sdev->sdev_gendev);
  773. transport_destroy_device(&sdev->sdev_gendev);
  774. put_device(&sdev->sdev_gendev);
  775. return error;
  776. }
  777. void __scsi_remove_device(struct scsi_device *sdev)
  778. {
  779. struct device *dev = &sdev->sdev_gendev;
  780. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  781. return;
  782. bsg_unregister_queue(sdev->request_queue);
  783. device_unregister(&sdev->sdev_dev);
  784. transport_remove_device(dev);
  785. device_del(dev);
  786. scsi_device_set_state(sdev, SDEV_DEL);
  787. if (sdev->host->hostt->slave_destroy)
  788. sdev->host->hostt->slave_destroy(sdev);
  789. transport_destroy_device(dev);
  790. put_device(dev);
  791. }
  792. /**
  793. * scsi_remove_device - unregister a device from the scsi bus
  794. * @sdev: scsi_device to unregister
  795. **/
  796. void scsi_remove_device(struct scsi_device *sdev)
  797. {
  798. struct Scsi_Host *shost = sdev->host;
  799. mutex_lock(&shost->scan_mutex);
  800. __scsi_remove_device(sdev);
  801. mutex_unlock(&shost->scan_mutex);
  802. }
  803. EXPORT_SYMBOL(scsi_remove_device);
  804. static void __scsi_remove_target(struct scsi_target *starget)
  805. {
  806. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  807. unsigned long flags;
  808. struct scsi_device *sdev;
  809. spin_lock_irqsave(shost->host_lock, flags);
  810. starget->reap_ref++;
  811. restart:
  812. list_for_each_entry(sdev, &shost->__devices, siblings) {
  813. if (sdev->channel != starget->channel ||
  814. sdev->id != starget->id ||
  815. sdev->sdev_state == SDEV_DEL)
  816. continue;
  817. spin_unlock_irqrestore(shost->host_lock, flags);
  818. scsi_remove_device(sdev);
  819. spin_lock_irqsave(shost->host_lock, flags);
  820. goto restart;
  821. }
  822. spin_unlock_irqrestore(shost->host_lock, flags);
  823. scsi_target_reap(starget);
  824. }
  825. static int __remove_child (struct device * dev, void * data)
  826. {
  827. if (scsi_is_target_device(dev))
  828. __scsi_remove_target(to_scsi_target(dev));
  829. return 0;
  830. }
  831. /**
  832. * scsi_remove_target - try to remove a target and all its devices
  833. * @dev: generic starget or parent of generic stargets to be removed
  834. *
  835. * Note: This is slightly racy. It is possible that if the user
  836. * requests the addition of another device then the target won't be
  837. * removed.
  838. */
  839. void scsi_remove_target(struct device *dev)
  840. {
  841. struct device *rdev;
  842. if (scsi_is_target_device(dev)) {
  843. __scsi_remove_target(to_scsi_target(dev));
  844. return;
  845. }
  846. rdev = get_device(dev);
  847. device_for_each_child(dev, NULL, __remove_child);
  848. put_device(rdev);
  849. }
  850. EXPORT_SYMBOL(scsi_remove_target);
  851. int scsi_register_driver(struct device_driver *drv)
  852. {
  853. drv->bus = &scsi_bus_type;
  854. return driver_register(drv);
  855. }
  856. EXPORT_SYMBOL(scsi_register_driver);
  857. int scsi_register_interface(struct class_interface *intf)
  858. {
  859. intf->class = &sdev_class;
  860. return class_interface_register(intf);
  861. }
  862. EXPORT_SYMBOL(scsi_register_interface);
  863. /**
  864. * scsi_sysfs_add_host - add scsi host to subsystem
  865. * @shost: scsi host struct to add to subsystem
  866. * @dev: parent struct device pointer
  867. **/
  868. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  869. {
  870. int error, i;
  871. /* add host specific attributes */
  872. if (shost->hostt->shost_attrs) {
  873. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  874. error = device_create_file(&shost->shost_dev,
  875. shost->hostt->shost_attrs[i]);
  876. if (error)
  877. return error;
  878. }
  879. }
  880. transport_register_device(&shost->shost_gendev);
  881. transport_configure_device(&shost->shost_gendev);
  882. return 0;
  883. }
  884. static struct device_type scsi_dev_type = {
  885. .name = "scsi_device",
  886. .release = scsi_device_dev_release,
  887. .groups = scsi_sdev_attr_groups,
  888. };
  889. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  890. {
  891. unsigned long flags;
  892. struct Scsi_Host *shost = sdev->host;
  893. struct scsi_target *starget = sdev->sdev_target;
  894. device_initialize(&sdev->sdev_gendev);
  895. sdev->sdev_gendev.bus = &scsi_bus_type;
  896. sdev->sdev_gendev.type = &scsi_dev_type;
  897. sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
  898. sdev->host->host_no, sdev->channel, sdev->id,
  899. sdev->lun);
  900. device_initialize(&sdev->sdev_dev);
  901. sdev->sdev_dev.parent = &sdev->sdev_gendev;
  902. sdev->sdev_dev.class = &sdev_class;
  903. snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
  904. "%d:%d:%d:%d", sdev->host->host_no,
  905. sdev->channel, sdev->id, sdev->lun);
  906. sdev->scsi_level = starget->scsi_level;
  907. transport_setup_device(&sdev->sdev_gendev);
  908. spin_lock_irqsave(shost->host_lock, flags);
  909. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  910. list_add_tail(&sdev->siblings, &shost->__devices);
  911. spin_unlock_irqrestore(shost->host_lock, flags);
  912. }
  913. int scsi_is_sdev_device(const struct device *dev)
  914. {
  915. return dev->type == &scsi_dev_type;
  916. }
  917. EXPORT_SYMBOL(scsi_is_sdev_device);
  918. /* A blank transport template that is used in drivers that don't
  919. * yet implement Transport Attributes */
  920. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };