ide-park.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, REQ_OP_DRV_IN, __GFP_RECLAIM);
  29. scsi_req(rq)->cmd[0] = REQ_PARK_HEADS;
  30. scsi_req(rq)->cmd_len = 1;
  31. ide_req(rq)->type = ATA_PRIV_MISC;
  32. rq->special = &timeout;
  33. blk_execute_rq(q, NULL, rq, 1);
  34. rc = scsi_req(rq)->result ? -EIO : 0;
  35. blk_put_request(rq);
  36. if (rc)
  37. goto out;
  38. /*
  39. * Make sure that *some* command is sent to the drive after the
  40. * timeout has expired, so power management will be reenabled.
  41. */
  42. rq = blk_get_request(q, REQ_OP_DRV_IN, GFP_NOWAIT);
  43. if (IS_ERR(rq))
  44. goto out;
  45. scsi_req(rq)->cmd[0] = REQ_UNPARK_HEADS;
  46. scsi_req(rq)->cmd_len = 1;
  47. ide_req(rq)->type = ATA_PRIV_MISC;
  48. elv_add_request(q, rq, ELEVATOR_INSERT_FRONT);
  49. out:
  50. return;
  51. }
  52. ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
  53. {
  54. struct ide_cmd cmd;
  55. struct ide_taskfile *tf = &cmd.tf;
  56. memset(&cmd, 0, sizeof(cmd));
  57. if (scsi_req(rq)->cmd[0] == REQ_PARK_HEADS) {
  58. drive->sleep = *(unsigned long *)rq->special;
  59. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  60. tf->command = ATA_CMD_IDLEIMMEDIATE;
  61. tf->feature = 0x44;
  62. tf->lbal = 0x4c;
  63. tf->lbam = 0x4e;
  64. tf->lbah = 0x55;
  65. cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
  66. cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_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 = kstrtol(buf, 10, &input);
  101. if (rc)
  102. return rc;
  103. if (input < -2)
  104. return -EINVAL;
  105. if (input > MAX_PARK_TIMEOUT) {
  106. input = MAX_PARK_TIMEOUT;
  107. rc = -EOVERFLOW;
  108. }
  109. mutex_lock(&ide_setting_mtx);
  110. if (input >= 0) {
  111. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  112. rc = -EOPNOTSUPP;
  113. else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
  114. issue_park_cmd(drive, msecs_to_jiffies(input));
  115. } else {
  116. if (drive->media == ide_disk)
  117. switch (input) {
  118. case -1:
  119. drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
  120. break;
  121. case -2:
  122. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  123. break;
  124. }
  125. else
  126. rc = -EOPNOTSUPP;
  127. }
  128. mutex_unlock(&ide_setting_mtx);
  129. return rc ? rc : len;
  130. }