ide-park.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <linux/kernel.h>
  2. #include <linux/ide.h>
  3. #include <linux/jiffies.h>
  4. #include <linux/blkdev.h>
  5. DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
  6. static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
  7. {
  8. struct request_queue *q = drive->queue;
  9. struct request *rq;
  10. int rc;
  11. timeout += jiffies;
  12. spin_lock_irq(&ide_lock);
  13. if (drive->dev_flags & IDE_DFLAG_PARKED) {
  14. ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
  15. int reset_timer;
  16. reset_timer = time_before(timeout, drive->sleep);
  17. drive->sleep = timeout;
  18. wake_up_all(&ide_park_wq);
  19. if (reset_timer && hwgroup->sleeping &&
  20. del_timer(&hwgroup->timer)) {
  21. hwgroup->sleeping = 0;
  22. hwgroup->busy = 0;
  23. blk_start_queueing(q);
  24. }
  25. spin_unlock_irq(&ide_lock);
  26. return;
  27. }
  28. spin_unlock_irq(&ide_lock);
  29. rq = blk_get_request(q, READ, __GFP_WAIT);
  30. rq->cmd[0] = REQ_PARK_HEADS;
  31. rq->cmd_len = 1;
  32. rq->cmd_type = REQ_TYPE_SPECIAL;
  33. rq->special = &timeout;
  34. rc = blk_execute_rq(q, NULL, rq, 1);
  35. blk_put_request(rq);
  36. if (rc)
  37. goto out;
  38. /*
  39. * Make sure that *some* command is sent to the drive after the
  40. * timeout has expired, so power management will be reenabled.
  41. */
  42. rq = blk_get_request(q, READ, GFP_NOWAIT);
  43. if (unlikely(!rq))
  44. goto out;
  45. rq->cmd[0] = REQ_UNPARK_HEADS;
  46. rq->cmd_len = 1;
  47. rq->cmd_type = REQ_TYPE_SPECIAL;
  48. elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
  49. out:
  50. return;
  51. }
  52. ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
  53. char *buf)
  54. {
  55. ide_drive_t *drive = to_ide_device(dev);
  56. unsigned long now;
  57. unsigned int msecs;
  58. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  59. return -EOPNOTSUPP;
  60. spin_lock_irq(&ide_lock);
  61. now = jiffies;
  62. if (drive->dev_flags & IDE_DFLAG_PARKED &&
  63. time_after(drive->sleep, now))
  64. msecs = jiffies_to_msecs(drive->sleep - now);
  65. else
  66. msecs = 0;
  67. spin_unlock_irq(&ide_lock);
  68. return snprintf(buf, 20, "%u\n", msecs);
  69. }
  70. ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
  71. const char *buf, size_t len)
  72. {
  73. #define MAX_PARK_TIMEOUT 30000
  74. ide_drive_t *drive = to_ide_device(dev);
  75. long int input;
  76. int rc;
  77. rc = strict_strtol(buf, 10, &input);
  78. if (rc || input < -2)
  79. return -EINVAL;
  80. if (input > MAX_PARK_TIMEOUT) {
  81. input = MAX_PARK_TIMEOUT;
  82. rc = -EOVERFLOW;
  83. }
  84. mutex_lock(&ide_setting_mtx);
  85. if (input >= 0) {
  86. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  87. rc = -EOPNOTSUPP;
  88. else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
  89. issue_park_cmd(drive, msecs_to_jiffies(input));
  90. } else {
  91. if (drive->media == ide_disk)
  92. switch (input) {
  93. case -1:
  94. drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
  95. break;
  96. case -2:
  97. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  98. break;
  99. }
  100. else
  101. rc = -EOPNOTSUPP;
  102. }
  103. mutex_unlock(&ide_setting_mtx);
  104. return rc ? rc : len;
  105. }