ide-park.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <linux/kernel.h>
  2. #include <linux/gfp.h>
  3. #include <linux/ide.h>
  4. #include <linux/jiffies.h>
  5. #include <linux/blkdev.h>
  6. DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
  7. static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
  8. {
  9. ide_hwif_t *hwif = drive->hwif;
  10. struct request_queue *q = drive->queue;
  11. struct request *rq;
  12. int rc;
  13. timeout += jiffies;
  14. spin_lock_irq(&hwif->lock);
  15. if (drive->dev_flags & IDE_DFLAG_PARKED) {
  16. int reset_timer = time_before(timeout, drive->sleep);
  17. int start_queue = 0;
  18. drive->sleep = timeout;
  19. wake_up_all(&ide_park_wq);
  20. if (reset_timer && del_timer(&hwif->timer))
  21. start_queue = 1;
  22. spin_unlock_irq(&hwif->lock);
  23. if (start_queue)
  24. blk_run_queue(q);
  25. return;
  26. }
  27. spin_unlock_irq(&hwif->lock);
  28. rq = blk_get_request(q, READ, __GFP_RECLAIM);
  29. rq->cmd[0] = REQ_PARK_HEADS;
  30. rq->cmd_len = 1;
  31. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  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 (IS_ERR(rq))
  43. goto out;
  44. rq->cmd[0] = REQ_UNPARK_HEADS;
  45. rq->cmd_len = 1;
  46. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  47. elv_add_request(q, rq, ELEVATOR_INSERT_FRONT);
  48. out:
  49. return;
  50. }
  51. ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
  52. {
  53. struct ide_cmd cmd;
  54. struct ide_taskfile *tf = &cmd.tf;
  55. memset(&cmd, 0, sizeof(cmd));
  56. if (rq->cmd[0] == REQ_PARK_HEADS) {
  57. drive->sleep = *(unsigned long *)rq->special;
  58. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  59. tf->command = ATA_CMD_IDLEIMMEDIATE;
  60. tf->feature = 0x44;
  61. tf->lbal = 0x4c;
  62. tf->lbam = 0x4e;
  63. tf->lbah = 0x55;
  64. cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
  65. cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
  66. } else /* cmd == REQ_UNPARK_HEADS */
  67. tf->command = ATA_CMD_CHK_POWER;
  68. cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
  69. cmd.protocol = ATA_PROT_NODATA;
  70. cmd.rq = rq;
  71. return do_rw_taskfile(drive, &cmd);
  72. }
  73. ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
  74. char *buf)
  75. {
  76. ide_drive_t *drive = to_ide_device(dev);
  77. ide_hwif_t *hwif = drive->hwif;
  78. unsigned long now;
  79. unsigned int msecs;
  80. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  81. return -EOPNOTSUPP;
  82. spin_lock_irq(&hwif->lock);
  83. now = jiffies;
  84. if (drive->dev_flags & IDE_DFLAG_PARKED &&
  85. time_after(drive->sleep, now))
  86. msecs = jiffies_to_msecs(drive->sleep - now);
  87. else
  88. msecs = 0;
  89. spin_unlock_irq(&hwif->lock);
  90. return snprintf(buf, 20, "%u\n", msecs);
  91. }
  92. ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
  93. const char *buf, size_t len)
  94. {
  95. #define MAX_PARK_TIMEOUT 30000
  96. ide_drive_t *drive = to_ide_device(dev);
  97. long int input;
  98. int rc;
  99. rc = kstrtol(buf, 10, &input);
  100. if (rc)
  101. return rc;
  102. if (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. }