ide-park.c 2.8 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. 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. drive->sleep = timeout;
  17. wake_up_all(&ide_park_wq);
  18. if (reset_timer && hwgroup->sleeping &&
  19. del_timer(&hwgroup->timer)) {
  20. hwgroup->sleeping = 0;
  21. hwgroup->busy = 0;
  22. blk_start_queueing(q);
  23. }
  24. spin_unlock_irq(&hwgroup->lock);
  25. return;
  26. }
  27. spin_unlock_irq(&hwgroup->lock);
  28. rq = blk_get_request(q, READ, __GFP_WAIT);
  29. rq->cmd[0] = REQ_PARK_HEADS;
  30. rq->cmd_len = 1;
  31. rq->cmd_type = REQ_TYPE_SPECIAL;
  32. rq->special = &timeout;
  33. rc = blk_execute_rq(q, NULL, rq, 1);
  34. blk_put_request(rq);
  35. if (rc)
  36. goto out;
  37. /*
  38. * Make sure that *some* command is sent to the drive after the
  39. * timeout has expired, so power management will be reenabled.
  40. */
  41. rq = blk_get_request(q, READ, GFP_NOWAIT);
  42. if (unlikely(!rq))
  43. goto out;
  44. rq->cmd[0] = REQ_UNPARK_HEADS;
  45. rq->cmd_len = 1;
  46. rq->cmd_type = REQ_TYPE_SPECIAL;
  47. elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
  48. out:
  49. return;
  50. }
  51. ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
  52. char *buf)
  53. {
  54. ide_drive_t *drive = to_ide_device(dev);
  55. ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
  56. unsigned long now;
  57. unsigned int msecs;
  58. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  59. return -EOPNOTSUPP;
  60. spin_lock_irq(&hwgroup->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(&hwgroup->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. }