scsi_sysfs.c 25 KB

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