ide-park.c 2.8 KB

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