ide-park.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_hwif_t *hwif = drive->hwif;
  9. struct request_queue *q = drive->queue;
  10. struct request *rq;
  11. int rc;
  12. timeout += jiffies;
  13. spin_lock_irq(&hwif->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(&hwif->timer))
  20. start_queue = 1;
  21. spin_unlock_irq(&hwif->lock);
  22. if (start_queue)
  23. blk_run_queue(q);
  24. return;
  25. }
  26. spin_unlock_irq(&hwif->lock);
  27. rq = blk_get_request(q, READ, __GFP_WAIT);
  28. rq->cmd[0] = REQ_PARK_HEADS;
  29. rq->cmd_len = 1;
  30. rq->cmd_type = REQ_TYPE_SPECIAL;
  31. rq->special = &timeout;
  32. rc = blk_execute_rq(q, NULL, rq, 1);
  33. blk_put_request(rq);
  34. if (rc)
  35. goto out;
  36. /*
  37. * Make sure that *some* command is sent to the drive after the
  38. * timeout has expired, so power management will be reenabled.
  39. */
  40. rq = blk_get_request(q, READ, GFP_NOWAIT);
  41. if (unlikely(!rq))
  42. goto out;
  43. rq->cmd[0] = REQ_UNPARK_HEADS;
  44. rq->cmd_len = 1;
  45. rq->cmd_type = REQ_TYPE_SPECIAL;
  46. elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
  47. out:
  48. return;
  49. }
  50. ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
  51. {
  52. struct ide_cmd cmd;
  53. struct ide_taskfile *tf = &cmd.tf;
  54. memset(&cmd, 0, sizeof(cmd));
  55. if (rq->cmd[0] == REQ_PARK_HEADS) {
  56. drive->sleep = *(unsigned long *)rq->special;
  57. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  58. tf->command = ATA_CMD_IDLEIMMEDIATE;
  59. tf->feature = 0x44;
  60. tf->lbal = 0x4c;
  61. tf->lbam = 0x4e;
  62. tf->lbah = 0x55;
  63. cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
  64. cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
  65. } else /* cmd == REQ_UNPARK_HEADS */
  66. tf->command = ATA_CMD_CHK_POWER;
  67. cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
  68. cmd.protocol = ATA_PROT_NODATA;
  69. cmd.rq = rq;
  70. return do_rw_taskfile(drive, &cmd);
  71. }
  72. ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
  73. char *buf)
  74. {
  75. ide_drive_t *drive = to_ide_device(dev);
  76. ide_hwif_t *hwif = drive->hwif;
  77. unsigned long now;
  78. unsigned int msecs;
  79. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  80. return -EOPNOTSUPP;
  81. spin_lock_irq(&hwif->lock);
  82. now = jiffies;
  83. if (drive->dev_flags & IDE_DFLAG_PARKED &&
  84. time_after(drive->sleep, now))
  85. msecs = jiffies_to_msecs(drive->sleep - now);
  86. else
  87. msecs = 0;
  88. spin_unlock_irq(&hwif->lock);
  89. return snprintf(buf, 20, "%u\n", msecs);
  90. }
  91. ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
  92. const char *buf, size_t len)
  93. {
  94. #define MAX_PARK_TIMEOUT 30000
  95. ide_drive_t *drive = to_ide_device(dev);
  96. long int input;
  97. int rc;
  98. rc = strict_strtol(buf, 10, &input);
  99. if (rc || input < -2)
  100. return -EINVAL;
  101. if (input > MAX_PARK_TIMEOUT) {
  102. input = MAX_PARK_TIMEOUT;
  103. rc = -EOVERFLOW;
  104. }
  105. mutex_lock(&ide_setting_mtx);
  106. if (input >= 0) {
  107. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  108. rc = -EOPNOTSUPP;
  109. else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
  110. issue_park_cmd(drive, msecs_to_jiffies(input));
  111. } else {
  112. if (drive->media == ide_disk)
  113. switch (input) {
  114. case -1:
  115. drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
  116. break;
  117. case -2:
  118. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  119. break;
  120. }
  121. else
  122. rc = -EOPNOTSUPP;
  123. }
  124. mutex_unlock(&ide_setting_mtx);
  125. return rc ? rc : len;
  126. }