scsi_sysfs.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 device_attribute *scsi_sysfs_sdev_attrs[] = {
  527. &dev_attr_device_blocked,
  528. &dev_attr_queue_depth,
  529. &dev_attr_queue_type,
  530. &dev_attr_type,
  531. &dev_attr_scsi_level,
  532. &dev_attr_vendor,
  533. &dev_attr_model,
  534. &dev_attr_rev,
  535. &dev_attr_rescan,
  536. &dev_attr_delete,
  537. &dev_attr_state,
  538. &dev_attr_timeout,
  539. &dev_attr_iocounterbits,
  540. &dev_attr_iorequest_cnt,
  541. &dev_attr_iodone_cnt,
  542. &dev_attr_ioerr_cnt,
  543. &dev_attr_modalias,
  544. NULL
  545. };
  546. static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
  547. size_t count)
  548. {
  549. int depth, retval;
  550. struct scsi_device *sdev = to_scsi_device(dev);
  551. struct scsi_host_template *sht = sdev->host->hostt;
  552. if (!sht->change_queue_depth)
  553. return -EINVAL;
  554. depth = simple_strtoul(buf, NULL, 0);
  555. if (depth < 1)
  556. return -EINVAL;
  557. retval = sht->change_queue_depth(sdev, depth);
  558. if (retval < 0)
  559. return retval;
  560. return count;
  561. }
  562. static struct device_attribute sdev_attr_queue_depth_rw =
  563. __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
  564. sdev_store_queue_depth_rw);
  565. static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
  566. size_t count)
  567. {
  568. struct scsi_device *sdev = to_scsi_device(dev);
  569. struct scsi_host_template *sht = sdev->host->hostt;
  570. int tag_type = 0, retval;
  571. int prev_tag_type = scsi_get_tag_type(sdev);
  572. if (!sdev->tagged_supported || !sht->change_queue_type)
  573. return -EINVAL;
  574. if (strncmp(buf, "ordered", 7) == 0)
  575. tag_type = MSG_ORDERED_TAG;
  576. else if (strncmp(buf, "simple", 6) == 0)
  577. tag_type = MSG_SIMPLE_TAG;
  578. else if (strncmp(buf, "none", 4) != 0)
  579. return -EINVAL;
  580. if (tag_type == prev_tag_type)
  581. return count;
  582. retval = sht->change_queue_type(sdev, tag_type);
  583. if (retval < 0)
  584. return retval;
  585. return count;
  586. }
  587. static struct device_attribute sdev_attr_queue_type_rw =
  588. __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
  589. sdev_store_queue_type_rw);
  590. static struct device_attribute *attr_changed_internally(
  591. struct Scsi_Host *shost,
  592. struct device_attribute * attr)
  593. {
  594. if (!strcmp("queue_depth", attr->attr.name)
  595. && shost->hostt->change_queue_depth)
  596. return &sdev_attr_queue_depth_rw;
  597. else if (!strcmp("queue_type", attr->attr.name)
  598. && shost->hostt->change_queue_type)
  599. return &sdev_attr_queue_type_rw;
  600. return attr;
  601. }
  602. static struct device_attribute *attr_overridden(
  603. struct device_attribute **attrs,
  604. struct device_attribute *attr)
  605. {
  606. int i;
  607. if (!attrs)
  608. return NULL;
  609. for (i = 0; attrs[i]; i++)
  610. if (!strcmp(attrs[i]->attr.name, attr->attr.name))
  611. return attrs[i];
  612. return NULL;
  613. }
  614. static int attr_add(struct device *dev, struct device_attribute *attr)
  615. {
  616. struct device_attribute *base_attr;
  617. /*
  618. * Spare the caller from having to copy things it's not interested in.
  619. */
  620. base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
  621. if (base_attr) {
  622. /* extend permissions */
  623. attr->attr.mode |= base_attr->attr.mode;
  624. /* override null show/store with default */
  625. if (!attr->show)
  626. attr->show = base_attr->show;
  627. if (!attr->store)
  628. attr->store = base_attr->store;
  629. }
  630. return device_create_file(dev, attr);
  631. }
  632. /**
  633. * scsi_sysfs_add_sdev - add scsi device to sysfs
  634. * @sdev: scsi_device to add
  635. *
  636. * Return value:
  637. * 0 on Success / non-zero on Failure
  638. **/
  639. int scsi_sysfs_add_sdev(struct scsi_device *sdev)
  640. {
  641. int error, i;
  642. struct request_queue *rq = sdev->request_queue;
  643. if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
  644. return error;
  645. error = device_add(&sdev->sdev_gendev);
  646. if (error) {
  647. put_device(sdev->sdev_gendev.parent);
  648. printk(KERN_INFO "error 1\n");
  649. return error;
  650. }
  651. error = class_device_add(&sdev->sdev_classdev);
  652. if (error) {
  653. printk(KERN_INFO "error 2\n");
  654. goto clean_device;
  655. }
  656. /* take a reference for the sdev_classdev; this is
  657. * released by the sdev_class .release */
  658. get_device(&sdev->sdev_gendev);
  659. error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);
  660. if (error)
  661. sdev_printk(KERN_INFO, sdev,
  662. "Failed to register bsg queue, errno=%d\n", error);
  663. /* we're treating error on bsg register as non-fatal, so pretend
  664. * nothing went wrong */
  665. error = 0;
  666. if (sdev->host->hostt->sdev_attrs) {
  667. for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
  668. error = attr_add(&sdev->sdev_gendev,
  669. sdev->host->hostt->sdev_attrs[i]);
  670. if (error) {
  671. __scsi_remove_device(sdev);
  672. goto out;
  673. }
  674. }
  675. }
  676. for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
  677. if (!attr_overridden(sdev->host->hostt->sdev_attrs,
  678. scsi_sysfs_sdev_attrs[i])) {
  679. struct device_attribute * attr =
  680. attr_changed_internally(sdev->host,
  681. scsi_sysfs_sdev_attrs[i]);
  682. error = device_create_file(&sdev->sdev_gendev, attr);
  683. if (error) {
  684. __scsi_remove_device(sdev);
  685. goto out;
  686. }
  687. }
  688. }
  689. transport_add_device(&sdev->sdev_gendev);
  690. out:
  691. return error;
  692. clean_device:
  693. scsi_device_set_state(sdev, SDEV_CANCEL);
  694. device_del(&sdev->sdev_gendev);
  695. transport_destroy_device(&sdev->sdev_gendev);
  696. put_device(&sdev->sdev_gendev);
  697. return error;
  698. }
  699. void __scsi_remove_device(struct scsi_device *sdev)
  700. {
  701. struct device *dev = &sdev->sdev_gendev;
  702. if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
  703. return;
  704. bsg_unregister_queue(sdev->request_queue);
  705. class_device_unregister(&sdev->sdev_classdev);
  706. transport_remove_device(dev);
  707. device_del(dev);
  708. scsi_device_set_state(sdev, SDEV_DEL);
  709. if (sdev->host->hostt->slave_destroy)
  710. sdev->host->hostt->slave_destroy(sdev);
  711. transport_destroy_device(dev);
  712. put_device(dev);
  713. }
  714. /**
  715. * scsi_remove_device - unregister a device from the scsi bus
  716. * @sdev: scsi_device to unregister
  717. **/
  718. void scsi_remove_device(struct scsi_device *sdev)
  719. {
  720. struct Scsi_Host *shost = sdev->host;
  721. mutex_lock(&shost->scan_mutex);
  722. __scsi_remove_device(sdev);
  723. mutex_unlock(&shost->scan_mutex);
  724. }
  725. EXPORT_SYMBOL(scsi_remove_device);
  726. static void __scsi_remove_target(struct scsi_target *starget)
  727. {
  728. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  729. unsigned long flags;
  730. struct scsi_device *sdev;
  731. spin_lock_irqsave(shost->host_lock, flags);
  732. starget->reap_ref++;
  733. restart:
  734. list_for_each_entry(sdev, &shost->__devices, siblings) {
  735. if (sdev->channel != starget->channel ||
  736. sdev->id != starget->id ||
  737. sdev->sdev_state == SDEV_DEL)
  738. continue;
  739. spin_unlock_irqrestore(shost->host_lock, flags);
  740. scsi_remove_device(sdev);
  741. spin_lock_irqsave(shost->host_lock, flags);
  742. goto restart;
  743. }
  744. spin_unlock_irqrestore(shost->host_lock, flags);
  745. scsi_target_reap(starget);
  746. }
  747. static int __remove_child (struct device * dev, void * data)
  748. {
  749. if (scsi_is_target_device(dev))
  750. __scsi_remove_target(to_scsi_target(dev));
  751. return 0;
  752. }
  753. /**
  754. * scsi_remove_target - try to remove a target and all its devices
  755. * @dev: generic starget or parent of generic stargets to be removed
  756. *
  757. * Note: This is slightly racy. It is possible that if the user
  758. * requests the addition of another device then the target won't be
  759. * removed.
  760. */
  761. void scsi_remove_target(struct device *dev)
  762. {
  763. struct device *rdev;
  764. if (scsi_is_target_device(dev)) {
  765. __scsi_remove_target(to_scsi_target(dev));
  766. return;
  767. }
  768. rdev = get_device(dev);
  769. device_for_each_child(dev, NULL, __remove_child);
  770. put_device(rdev);
  771. }
  772. EXPORT_SYMBOL(scsi_remove_target);
  773. int scsi_register_driver(struct device_driver *drv)
  774. {
  775. drv->bus = &scsi_bus_type;
  776. return driver_register(drv);
  777. }
  778. EXPORT_SYMBOL(scsi_register_driver);
  779. int scsi_register_interface(struct class_interface *intf)
  780. {
  781. intf->class = &sdev_class;
  782. return class_interface_register(intf);
  783. }
  784. EXPORT_SYMBOL(scsi_register_interface);
  785. static struct class_device_attribute *class_attr_overridden(
  786. struct class_device_attribute **attrs,
  787. struct class_device_attribute *attr)
  788. {
  789. int i;
  790. if (!attrs)
  791. return NULL;
  792. for (i = 0; attrs[i]; i++)
  793. if (!strcmp(attrs[i]->attr.name, attr->attr.name))
  794. return attrs[i];
  795. return NULL;
  796. }
  797. static int class_attr_add(struct class_device *classdev,
  798. struct class_device_attribute *attr)
  799. {
  800. struct class_device_attribute *base_attr;
  801. /*
  802. * Spare the caller from having to copy things it's not interested in.
  803. */
  804. base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
  805. if (base_attr) {
  806. /* extend permissions */
  807. attr->attr.mode |= base_attr->attr.mode;
  808. /* override null show/store with default */
  809. if (!attr->show)
  810. attr->show = base_attr->show;
  811. if (!attr->store)
  812. attr->store = base_attr->store;
  813. }
  814. return class_device_create_file(classdev, attr);
  815. }
  816. /**
  817. * scsi_sysfs_add_host - add scsi host to subsystem
  818. * @shost: scsi host struct to add to subsystem
  819. * @dev: parent struct device pointer
  820. **/
  821. int scsi_sysfs_add_host(struct Scsi_Host *shost)
  822. {
  823. int error, i;
  824. if (shost->hostt->shost_attrs) {
  825. for (i = 0; shost->hostt->shost_attrs[i]; i++) {
  826. error = class_attr_add(&shost->shost_classdev,
  827. shost->hostt->shost_attrs[i]);
  828. if (error)
  829. return error;
  830. }
  831. }
  832. for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
  833. if (!class_attr_overridden(shost->hostt->shost_attrs,
  834. scsi_sysfs_shost_attrs[i])) {
  835. error = class_device_create_file(&shost->shost_classdev,
  836. scsi_sysfs_shost_attrs[i]);
  837. if (error)
  838. return error;
  839. }
  840. }
  841. transport_register_device(&shost->shost_gendev);
  842. return 0;
  843. }
  844. void scsi_sysfs_device_initialize(struct scsi_device *sdev)
  845. {
  846. unsigned long flags;
  847. struct Scsi_Host *shost = sdev->host;
  848. struct scsi_target *starget = sdev->sdev_target;
  849. device_initialize(&sdev->sdev_gendev);
  850. sdev->sdev_gendev.bus = &scsi_bus_type;
  851. sdev->sdev_gendev.release = scsi_device_dev_release;
  852. sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
  853. sdev->host->host_no, sdev->channel, sdev->id,
  854. sdev->lun);
  855. class_device_initialize(&sdev->sdev_classdev);
  856. sdev->sdev_classdev.dev = &sdev->sdev_gendev;
  857. sdev->sdev_classdev.class = &sdev_class;
  858. snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
  859. "%d:%d:%d:%d", sdev->host->host_no,
  860. sdev->channel, sdev->id, sdev->lun);
  861. sdev->scsi_level = starget->scsi_level;
  862. transport_setup_device(&sdev->sdev_gendev);
  863. spin_lock_irqsave(shost->host_lock, flags);
  864. list_add_tail(&sdev->same_target_siblings, &starget->devices);
  865. list_add_tail(&sdev->siblings, &shost->__devices);
  866. spin_unlock_irqrestore(shost->host_lock, flags);
  867. }
  868. int scsi_is_sdev_device(const struct device *dev)
  869. {
  870. return dev->release == scsi_device_dev_release;
  871. }
  872. EXPORT_SYMBOL(scsi_is_sdev_device);
  873. /* A blank transport template that is used in drivers that don't
  874. * yet implement Transport Attributes */
  875. struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };