scsi_sysfs.c 25 KB

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