ide-park.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 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(&hwif->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. ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
  54. {
  55. struct ide_cmd cmd;
  56. struct ide_taskfile *tf = &cmd.tf;
  57. memset(&cmd, 0, sizeof(cmd));
  58. if (rq->cmd[0] == REQ_PARK_HEADS) {
  59. drive->sleep = *(unsigned long *)rq->special;
  60. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  61. tf->command = ATA_CMD_IDLEIMMEDIATE;
  62. tf->feature = 0x44;
  63. tf->lbal = 0x4c;
  64. tf->lbam = 0x4e;
  65. tf->lbah = 0x55;
  66. cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
  67. cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
  68. } else /* cmd == REQ_UNPARK_HEADS */
  69. tf->command = ATA_CMD_CHK_POWER;
  70. cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
  71. cmd.protocol = ATA_PROT_NODATA;
  72. cmd.rq = rq;
  73. return do_rw_taskfile(drive, &cmd);
  74. }
  75. ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
  76. char *buf)
  77. {
  78. ide_drive_t *drive = to_ide_device(dev);
  79. ide_hwif_t *hwif = drive->hwif;
  80. unsigned long now;
  81. unsigned int msecs;
  82. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  83. return -EOPNOTSUPP;
  84. spin_lock_irq(&hwif->lock);
  85. now = jiffies;
  86. if (drive->dev_flags & IDE_DFLAG_PARKED &&
  87. time_after(drive->sleep, now))
  88. msecs = jiffies_to_msecs(drive->sleep - now);
  89. else
  90. msecs = 0;
  91. spin_unlock_irq(&hwif->lock);
  92. return snprintf(buf, 20, "%u\n", msecs);
  93. }
  94. ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
  95. const char *buf, size_t len)
  96. {
  97. #define MAX_PARK_TIMEOUT 30000
  98. ide_drive_t *drive = to_ide_device(dev);
  99. long int input;
  100. int rc;
  101. rc = strict_strtol(buf, 10, &input);
  102. if (rc || input < -2)
  103. return -EINVAL;
  104. if (input > MAX_PARK_TIMEOUT) {
  105. input = MAX_PARK_TIMEOUT;
  106. rc = -EOVERFLOW;
  107. }
  108. mutex_lock(&ide_setting_mtx);
  109. if (input >= 0) {
  110. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  111. rc = -EOPNOTSUPP;
  112. else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
  113. issue_park_cmd(drive, msecs_to_jiffies(input));
  114. } else {
  115. if (drive->media == ide_disk)
  116. switch (input) {
  117. case -1:
  118. drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
  119. break;
  120. case -2:
  121. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  122. break;
  123. }
  124. else
  125. rc = -EOPNOTSUPP;
  126. }
  127. mutex_unlock(&ide_setting_mtx);
  128. return rc ? rc : len;
  129. }