scsi_sysfs.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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;
  302. if (dev->type != &scsi_dev_type)
  303. return 0;
  304. sdev = to_scsi_device(dev);
  305. add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
  306. return 0;
  307. }
  308. static int scsi_bus_suspend(struct device * dev, pm_message_t state)
  309. {
  310. struct device_driver *drv;
  311. struct scsi_device *sdev;
  312. int err;
  313. if (dev->type != &scsi_dev_type)
  314. return 0;
  315. drv = dev->driver;
  316. sdev = to_scsi_device(dev);
  317. err = scsi_device_quiesce(sdev);
  318. if (err)
  319. return err;
  320. if (drv && drv->suspend) {
  321. err = drv->suspend(dev, state);
  322. if (err)
  323. return err;
  324. }
  325. return 0;
  326. }
  327. static int scsi_bus_resume(struct device * dev)
  328. {
  329. struct device_driver *drv;
  330. struct scsi_device *sdev;
  331. int err = 0;
  332. if (dev->type != &scsi_dev_type)
  333. return 0;
  334. drv = dev->driver;
  335. sdev = to_scsi_device(dev);
  336. if (drv && drv->resume)
  337. err = drv->resume(dev);
  338. scsi_device_resume(sdev);
  339. return err;
  340. }
  341. static int scsi_bus_remove(struct device *dev)
  342. {
  343. struct device_driver *drv = dev->driver;
  344. struct scsi_device *sdev = to_scsi_device(dev);
  345. int err = 0;
  346. /* reset the prep_fn back to the default since the
  347. * driver may have altered it and it's being removed */
  348. blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
  349. if (drv && drv->remove)
  350. err = drv->remove(dev);
  351. return 0;
  352. }
  353. struct bus_type scsi_bus_type = {
  354. .name = "scsi",
  355. .match = scsi_bus_match,
  356. .uevent = scsi_bus_uevent,
  357. .suspend = scsi_bus_suspend,
  358. .resume = scsi_bus_resume,
  359. .remove = scsi_bus_remove,
  360. };
  361. EXPORT_SYMBOL_GPL(scsi_bus_type);
  362. int scsi_sysfs_register(void)
  363. {
  364. int error;
  365. error = bus_register(&scsi_bus_type);
  366. if (!error) {
  367. error = class_register(&sdev_class);
  368. if (error)
  369. bus_unregister(&scsi_bus_type);
  370. }
  371. return error;
  372. }
  373. void scsi_sysfs_unregister(void)
  374. {
  375. class_unregister(&sdev_class);
  376. bus_unregister(&scsi_bus_type);
  377. }
  378. /*
  379. * sdev_show_function: macro to create an attr function that can be used to
  380. * show a non-bit field.
  381. */
  382. #define sdev_show_function(field, format_string) \
  383. static ssize_t \
  384. sdev_show_##field (struct device *dev, struct device_attribute *attr, \
  385. char *buf) \
  386. { \
  387. struct scsi_device *sdev; \
  388. sdev = to_scsi_device(dev); \
  389. return snprintf (buf, 20, format_string, sdev->field); \
  390. } \
  391. /*
  392. * sdev_rd_attr: macro to create a function and attribute variable for a
  393. * read only field.
  394. */
  395. #define sdev_rd_attr(field, format_string) \
  396. sdev_show_function(field, format_string) \
  397. static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
  398. /*
  399. * sdev_rd_attr: create a function and attribute variable for a
  400. * read/write field.
  401. */
  402. #define sdev_rw_attr(field, format_string) \
  403. sdev_show_function(field, format_string) \
  404. \
  405. static ssize_t \
  406. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  407. const char *buf, size_t count) \
  408. { \
  409. struct scsi_device *sdev; \
  410. sdev = to_scsi_device(dev); \
  411. snscanf (buf, 20, format_string, &sdev->field); \
  412. return count; \
  413. } \
  414. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  415. /* Currently we don't export bit fields, but we might in future,
  416. * so leave this code in */
  417. #if 0
  418. /*
  419. * sdev_rd_attr: create a function and attribute variable for a
  420. * read/write bit field.
  421. */
  422. #define sdev_rw_attr_bit(field) \
  423. sdev_show_function(field, "%d\n") \
  424. \
  425. static ssize_t \
  426. sdev_store_##field (struct device *dev, struct device_attribute *attr, \
  427. const char *buf, size_t count) \
  428. { \
  429. int ret; \
  430. struct scsi_device *sdev; \
  431. ret = scsi_sdev_check_buf_bit(buf); \
  432. if (ret >= 0) { \
  433. sdev = to_scsi_device(dev); \
  434. sdev->field = ret; \
  435. ret = count; \
  436. } \
  437. return ret; \
  438. } \
  439. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
  440. /*
  441. * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
  442. * else return -EINVAL.
  443. */
  444. static int scsi_sdev_check_buf_bit(const char *buf)
  445. {
  446. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  447. if (buf[0] == '1')
  448. return 1;
  449. else if (buf[0] == '0')
  450. return 0;
  451. else
  452. return -EINVAL;
  453. } else
  454. return -EINVAL;
  455. }
  456. #endif
  457. /*
  458. * Create the actual show/store functions and data structures.
  459. */
  460. sdev_rd_attr (device_blocked, "%d\n");
  461. sdev_rd_attr (queue_depth, "%d\n");
  462. sdev_rd_attr (type, "%d\n");
  463. sdev_rd_attr (scsi_level, "%d\n");
  464. sdev_rd_attr (vendor, "%.8s\n");
  465. sdev_rd_attr (model, "%.16s\n");
  466. sdev_rd_attr (rev, "%.4s\n");
  467. static ssize_t
  468. sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
  469. {
  470. struct scsi_device *sdev;
  471. sdev = to_scsi_device(dev);
  472. return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
  473. }
  474. static ssize_t
  475. sdev_store_timeout (struct device *dev, struct device_attribute *attr,
  476. const char *buf, size_t count)
  477. {
  478. struct scsi_device *sdev;
  479. int timeout;
  480. sdev = to_scsi_device(dev);
  481. sscanf (buf, "%d\n", &timeout);
  482. sdev->timeout = timeout * HZ;
  483. return count;
  484. }
  485. static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
  486. static ssize_t
  487. store_rescan_field (struct device *dev, struct device_attribute *attr,
  488. const char *buf, size_t count)
  489. {
  490. scsi_rescan_device(dev);
  491. return count;
  492. }
  493. static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
  494. static void sdev_store_delete_callback(struct device *dev)
  495. {
  496. scsi_remove_device(to_scsi_device(dev));
  497. }
  498. static ssize_t
  499. sdev_store_delete(struct device *dev, struct device_attribute *attr,
  500. const char *buf, size_t count)
  501. {
  502. int rc;
  503. /* An attribute cannot be unregistered by one of its own methods,
  504. * so we have to use this roundabout approach.
  505. */
  506. rc = device_schedule_callback(dev, sdev_store_delete_callback);
  507. if (rc)
  508. count = rc;
  509. return count;
  510. };
  511. static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
  512. static ssize_t
  513. store_state_field(struct device *dev, struct device_attribute *attr,
  514. const char *buf, size_t count)
  515. {
  516. int i;
  517. struct scsi_device *sdev = to_scsi_device(dev);
  518. enum scsi_device_state state = 0;
  519. for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  520. const int len = strlen(sdev_states[i].name);
  521. if (strncmp(sdev_states[i].name, buf, len) == 0 &&
  522. buf[len] == '\n') {
  523. state = sdev_states[i].value;
  524. break;
  525. }
  526. }
  527. if (!state)
  528. return -EINVAL;
  529. if (scsi_device_set_state(sdev, state))
  530. return -EINVAL;
  531. return count;
  532. }
  533. static ssize_t
  534. show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
  535. {
  536. struct scsi_device *sdev = to_scsi_device(dev);
  537. const char *name = scsi_device_state_name(sdev->sdev_state);
  538. if (!name)
  539. return -EINVAL;
  540. return snprintf(buf, 20, "%s\n", name);
  541. }
  542. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
  543. static ssize_t
  544. show_queue_type_field(struct device *dev, struct device_attribute *attr,
  545. char *buf)
  546. {
  547. struct scsi_device *sdev = to_scsi_device(dev);
  548. const char *name = "none";
  549. if (sdev->ordered_tags)
  550. name = "ordered";
  551. else if (sdev->simple_tags)
  552. name = "simple";
  553. return snprintf(buf, 20, "%s\n", name);
  554. }
  555. static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
  556. static ssize_t
  557. show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
  558. {
  559. return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
  560. }
  561. static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
  562. #define show_sdev_iostat(field) \
  563. static ssize_t \
  564. show_iostat_##field(struct device *dev, struct device_attribute *attr, \
  565. char *buf) \
  566. { \
  567. struct scsi_device *sdev = to_scsi_device(dev); \
  568. unsigned long long count = atomic_read(&sdev->field); \
  569. return snprintf(buf, 20, "0x%llx\n", count); \
  570. } \
  571. static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
  572. show_sdev_iostat(iorequest_cnt);
  573. show_sdev_iostat(iodone_cnt);
  574. show_sdev_iostat(ioerr_cnt);
  575. static ssize_t
  576. sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  577. {
  578. struct scsi_device *sdev;
  579. sdev = to_scsi_device(dev);
  580. return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
  581. }
  582. static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
  583. #define DECLARE_EVT_SHOW(name, Cap_name) \
  584. static ssize_t \
  585. sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
  586. char *buf) \
  587. { \
  588. struct scsi_device *sdev = to_scsi_device(dev); \
  589. int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
  590. return snprintf(buf, 20, "%d\n", val); \
  591. }
  592. #define DECLARE_EVT_STORE(name, Cap_name) \
  593. static ssize_t \
  594. sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
  595. const char *buf, size_t count) \
  596. { \
  597. struct scsi_device *sdev = to_scsi_device(dev); \
  598. int val = simple_strtoul(buf, NULL, 0); \
  599. if (val == 0) \
  600. clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  601. else if (val == 1) \
  602. set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
  603. else \
  604. return -EINVAL; \
  605. return count; \
  606. }
  607. #define DECLARE_EVT(name, Cap_name) \
  608. DECLARE_EVT_SHOW(name, Cap_name) \
  609. DECLARE_EVT_STORE(name, Cap_name) \
  610. static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
  611. sdev_store_evt_##name);
  612. #define REF_EVT(name) &dev_attr_evt_##name.attr
  613. DECLARE_EVT(media_change, MEDIA_CHANGE)
  614. /* Default template for device attributes. May NOT be modified */
  615. static struct attribute *scsi_sdev_attrs[] = {
  616. &dev_attr_device_blocked.attr,
  617. &dev_attr_type.attr,
  618. &dev_attr_scsi_level.attr,
  619. &dev_attr_vendor.attr,
  620. &dev_attr_model.attr,
  621. &dev_attr_rev.attr,
  622. &dev_attr_rescan.attr,
  623. &dev_attr_delete.attr,
  624. &dev_attr_state.attr,
  625. &dev_attr_timeout.attr,
  626. &dev_attr_iocounterbits.attr,
  627. &dev_attr_iorequest_cnt.attr,
  628. &dev_attr_iodone_cnt.attr,
  629. &dev_attr_ioerr_cnt.attr,
  630. &dev_attr_modalias.attr,
  631. REF_EVT(media_change),
  632. NULL
  633. };
  634. static struct attribute_group scsi_sdev_attr_group = {
  635. .attrs = scsi_sdev_attrs,
  636. };
  637. static struct attribute_group *scsi_sdev_attr_groups[] = {
  638. &scsi_sdev_attr_group,
  639. NULL
  640. };
  641. static ssize_t
  642. sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
  643. const char *buf, size_t count)
  644. {
  645. int depth, retval;
  646. struct scsi_device *sdev = to_scsi_device(dev);
  647. struct scsi_host_template *sht = sdev->host->hostt;
  648. if (!sht->change_queue_depth)
  649. return -EINVAL;
  650. depth = simple_strtoul(buf, NULL, 0);
  651. if (depth < 1)
  652. return -EINVAL;
  653. retval = sht->change_queue_depth(sdev, depth);
  654. if (retval < 0)
  655. return retval;
  656. return count;
  657. }
  658. static struct device_attribute sdev_attr_queue_depth_rw =
  659. __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  660. sdev_store_queue_depth_rw);
  661. static ssize_t
  662. sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
  663. const char *buf, size_t count)
  664. {
  665. struct scsi_device *sdev = to_scsi_device(dev);
  666. struct scsi_host_template *sht = sdev->host->hostt;
  667. int tag_type = 0, retval;
  668. int prev_tag_type = scsi_get_tag_type(sdev);
  669. if (!sdev->tagged_supported || !sht->change_queue_type)
  670. return -EINVAL;
  671. if (strncmp(buf, "ordered", 7) == 0)
  672. tag_type = MSG_ORDERED_TAG;
  673. else if (strncmp(buf, "simple", 6) == 0)
  674. tag_type = MSG_SIMPLE_TAG;
  675. else if (strncmp(buf, "none", 4) != 0)
  676. return -EINVAL;
  677. if (tag_type == prev_tag_type)
  678. return count;
  679. retval = sht->change_queue_type(sdev, tag_type);
  680. if (retval < 0)
  681. return retval;
  682. return count;
  683. }
  684. static int scsi_target_add(struct scsi_target *starget)
  685. {
  686. int error;
  687. if (starget->state != STARGET_CREATED)
  688. return 0;
  689. error = device_add(&starget->dev);
  690. if (error) {
  691. dev_err(&starget->dev, "target device_add failed, error %d\n", error);
  692. get_device(&starget->dev);
  693. scsi_target_reap(starget);
  694. put_device(&starget->dev);
  695. return error;
  696. }
  697. transport_add_device(&starget->dev);
  698. starget->state = STARGET_RUNNING;
  699. return 0;
  700. }
  701. static struct device_attribute sdev_attr_queue_type_rw =
  702. __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  703. sdev_store_queue_type_rw);
  704. /**
  705. * scsi_sysfs_add_sdev - add scsi device to sysfs
  706. * @sdev: scsi_device to add
  707. *
  708. * Return value:
  709. * 0 on Success / non-zero on Failure
  710. **/
  711. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  712. {
  713. int error, i;
  714. struct request_queue *rq = sdev->request_queue;
  715. struct scsi_target *starget = sdev->sdev_target;
  716. if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
  717. return error;
  718. error = scsi_target_add(starget);
  719. if (error)
  720. return error;
  721. transport_configure_device(&starget->dev);
  722. error = device_add(&sdev->sdev_gendev);
  723. if (error) {
  724. put_device(sdev->sdev_gendev.parent);
  725. printk(KERN_INFO "error 1\n");
  726. return error;
  727. }
  728. error = device_add(&sdev->sdev_dev);
  729. if (error) {
  730. printk(KERN_INFO "error 2\n");
  731. goto clean_device;
  732. }
  733. /* take a reference for the sdev_dev; this is
  734. * released by the sdev_class .release */
  735. get_device(&sdev->sdev_gendev);
  736. /* create queue files, which may be writable, depending on the host */
  737. if (sdev->host->hostt->change_queue_depth)
  738. error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
  739. else
  740. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
  741. if (error) {
  742. __scsi_remove_device(sdev);
  743. goto out;
  744. }
  745. if (sdev->host->hostt->change_queue_type)
  746. error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
  747. else
  748. error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
  749. if (error) {
  750. __scsi_remove_device(sdev);
  751. goto out;
  752. }
  753. error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
  754. if (error)
  755. sdev_printk(KERN_INFO, sdev,
  756. "Failed to register bsg queue, errno=%d\n", error);
  757. /* we're treating error on bsg register as non-fatal, so pretend
  758. * nothing went wrong */
  759. error = 0;
  760. /* add additional host specific attributes */
  761. if (sdev->host->hostt->sdev_attrs) {
  762. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  763. error = device_create_file(&sdev->sdev_gendev,
  764. sdev->host->hostt->sdev_attrs[i]);
  765. if (error) {
  766. __scsi_remove_device(sdev);
  767. goto out;
  768. }
  769. }
  770. }
  771. transport_add_device(&sdev->sdev_gendev);
  772. out:
  773. return error;
  774. clean_device:
  775. scsi_device_set_state(sdev, SDEV_CANCEL);
  776. device_del(&sdev->sdev_gendev);
  777. transport_destroy_device(&sdev->sdev_gendev);
  778. put_device(&sdev->sdev_gendev);
  779. return error;
  780. }
  781. void __scsi_remove_device(struct scsi_device *sdev)
  782. {
  783. struct device *dev = &sdev->sdev_gendev;
  784. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  785. return;
  786. bsg_unregister_queue(sdev->request_queue);
  787. device_unregister(&sdev->sdev_dev);
  788. transport_remove_device(dev);
  789. device_del(dev);
  790. scsi_device_set_state(sdev, SDEV_DEL);
  791. if (sdev->host->hostt->slave_destroy)
  792. sdev->host->hostt->slave_destroy(sdev);
  793. transport_destroy_device(dev);
  794. put_device(dev);
  795. }
  796. /**
  797. * scsi_remove_device - unregister a device from the scsi bus
  798. * @sdev: scsi_device to unregister
  799. **/
  800. void scsi_remove_device(struct scsi_device *sdev)
  801. {
  802. struct Scsi_Host *shost = sdev->host;
  803. mutex_lock(&shost->scan_mutex);
  804. __scsi_remove_device(sdev);
  805. mutex_unlock(&shost->scan_mutex);
  806. }
  807. EXPORT_SYMBOL(scsi_remove_device);
  808. static void __scsi_remove_target(struct scsi_target *starget)
  809. {
  810. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  811. unsigned long flags;
  812. struct scsi_device *sdev;
  813. spin_lock_irqsave(shost->host_lock, flags);
  814. starget->reap_ref++;
  815. restart:
  816. list_for_each_entry(sdev, &shost->__devices, siblings) {
  817. if (sdev->channel != starget->channel ||
  818. sdev->id != starget->id ||
  819. sdev->sdev_state == SDEV_DEL)
  820. continue;
  821. spin_unlock_irqrestore(shost->host_lock, flags);
  822. scsi_remove_device(sdev);
  823. spin_lock_irqsave(shost->host_lock, flags);
  824. goto restart;
  825. }
  826. spin_unlock_irqrestore(shost->host_lock, flags);
  827. scsi_target_reap(starget);
  828. }
  829. static int __remove_child (struct device * dev, void * data)
  830. {
  831. if (scsi_is_target_device(dev))
  832. __scsi_remove_target(to_scsi_target(dev));
  833. return 0;
  834. }
  835. /**
  836. * scsi_remove_target - try to remove a target and all its devices
  837. * @dev: generic starget or parent of generic stargets to be removed
  838. *
  839. * Note: This is slightly racy. It is possible that if the user
  840. * requests the addition of another device then the target won't be
  841. * removed.
  842. */
  843. void scsi_remove_target(struct device *dev)
  844. {
  845. struct device *rdev;
  846. if (scsi_is_target_device(dev)) {
  847. __scsi_remove_target(to_scsi_target(dev));
  848. return;
  849. }
  850. rdev = get_device(dev);
  851. device_for_each_child(dev, NULL, __remove_child);
  852. put_device(rdev);
  853. }
  854. EXPORT_SYMBOL(scsi_remove_target);
  855. int scsi_register_driver(struct device_driver *drv)
  856. {
  857. drv->bus = &scsi_bus_type;
  858. return driver_register(drv);
  859. }
  860. EXPORT_SYMBOL(scsi_register_driver);
  861. int scsi_register_interface(struct class_interface *intf)
  862. {
  863. intf->class = &sdev_class;
  864. return class_interface_register(intf);
  865. }
  866. EXPORT_SYMBOL(scsi_register_interface);
  867. /**
  868. * scsi_sysfs_add_host - add scsi host to subsystem
  869. * @shost: scsi host struct to add to subsystem
  870. * @dev: parent struct device pointer
  871. **/
  872. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  873. {
  874. int error, i;
  875. /* add host specific attributes */
  876. if (shost->hostt->shost_attrs) {
  877. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  878. error = device_create_file(&shost->shost_dev,
  879. shost->hostt->shost_attrs[i]);
  880. if (error)
  881. return error;
  882. }
  883. }
  884. transport_register_device(&shost->shost_gendev);
  885. transport_configure_device(&shost->shost_gendev);
  886. return 0;
  887. }
  888. static struct device_type scsi_dev_type = {
  889. .name = "scsi_device",
  890. .release = scsi_device_dev_release,
  891. .groups = scsi_sdev_attr_groups,
  892. };
  893. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  894. {
  895. unsigned long flags;
  896. struct Scsi_Host *shost = sdev->host;
  897. struct scsi_target *starget = sdev->sdev_target;
  898. device_initialize(&sdev->sdev_gendev);
  899. sdev->sdev_gendev.bus = &scsi_bus_type;
  900. sdev->sdev_gendev.type = &scsi_dev_type;
  901. sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
  902. sdev->host->host_no, sdev->channel, sdev->id,
  903. sdev->lun);
  904. device_initialize(&sdev->sdev_dev);
  905. sdev->sdev_dev.parent = &sdev->sdev_gendev;
  906. sdev->sdev_dev.class = &sdev_class;
  907. snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
  908. "%d:%d:%d:%d", sdev->host->host_no,
  909. sdev->channel, sdev->id, sdev->lun);
  910. sdev->scsi_level = starget->scsi_level;
  911. transport_setup_device(&sdev->sdev_gendev);
  912. spin_lock_irqsave(shost->host_lock, flags);
  913. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  914. list_add_tail(&sdev->siblings, &shost->__devices);
  915. spin_unlock_irqrestore(shost->host_lock, flags);
  916. }
  917. int scsi_is_sdev_device(const struct device *dev)
  918. {
  919. return dev->type == &scsi_dev_type;
  920. }
  921. EXPORT_SYMBOL(scsi_is_sdev_device);
  922. /* A blank transport template that is used in drivers that don't
  923. * yet implement Transport Attributes */
  924. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };