ide-park.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  67. } else /* cmd == REQ_UNPARK_HEADS */
  68. tf->command = ATA_CMD_CHK_POWER;
  69. cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
  70. cmd.protocol = ATA_PROT_NODATA;
  71. cmd.rq = rq;
  72. return do_rw_taskfile(drive, &cmd);
  73. }
  74. ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
  75. char *buf)
  76. {
  77. ide_drive_t *drive = to_ide_device(dev);
  78. ide_hwif_t *hwif = drive->hwif;
  79. unsigned long now;
  80. unsigned int msecs;
  81. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  82. return -EOPNOTSUPP;
  83. spin_lock_irq(&hwif->lock);
  84. now = jiffies;
  85. if (drive->dev_flags & IDE_DFLAG_PARKED &&
  86. time_after(drive->sleep, now))
  87. msecs = jiffies_to_msecs(drive->sleep - now);
  88. else
  89. msecs = 0;
  90. spin_unlock_irq(&hwif->lock);
  91. return snprintf(buf, 20, "%u\n", msecs);
  92. }
  93. ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
  94. const char *buf, size_t len)
  95. {
  96. #define MAX_PARK_TIMEOUT 30000
  97. ide_drive_t *drive = to_ide_device(dev);
  98. long int input;
  99. int rc;
  100. rc = strict_strtol(buf, 10, &input);
  101. if (rc || input < -2)
  102. return -EINVAL;
  103. if (input > MAX_PARK_TIMEOUT) {
  104. input = MAX_PARK_TIMEOUT;
  105. rc = -EOVERFLOW;
  106. }
  107. mutex_lock(&ide_setting_mtx);
  108. if (input >= 0) {
  109. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  110. rc = -EOPNOTSUPP;
  111. else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
  112. issue_park_cmd(drive, msecs_to_jiffies(input));
  113. } else {
  114. if (drive->media == ide_disk)
  115. switch (input) {
  116. case -1:
  117. drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
  118. break;
  119. case -2:
  120. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  121. break;
  122. }
  123. else
  124. rc = -EOPNOTSUPP;
  125. }
  126. mutex_unlock(&ide_setting_mtx);
  127. return rc ? rc : len;
  128. }