ide-park.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/gfp.h>
  4. #include <linux/ide.h>
  5. #include <linux/jiffies.h>
  6. #include <linux/blkdev.h>
  7. DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
  8. static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
  9. {
  10. ide_hwif_t *hwif = drive->hwif;
  11. struct request_queue *q = drive->queue;
  12. struct request *rq;
  13. int rc;
  14. timeout += jiffies;
  15. spin_lock_irq(&hwif->lock);
  16. if (drive->dev_flags & IDE_DFLAG_PARKED) {
  17. int reset_timer = time_before(timeout, drive->sleep);
  18. int start_queue = 0;
  19. drive->sleep = timeout;
  20. wake_up_all(&ide_park_wq);
  21. if (reset_timer && del_timer(&hwif->timer))
  22. start_queue = 1;
  23. spin_unlock_irq(&hwif->lock);
  24. if (start_queue)
  25. blk_run_queue(q);
  26. return;
  27. }
  28. spin_unlock_irq(&hwif->lock);
  29. rq = blk_get_request(q, REQ_OP_DRV_IN, 0);
  30. scsi_req(rq)->cmd[0] = REQ_PARK_HEADS;
  31. scsi_req(rq)->cmd_len = 1;
  32. ide_req(rq)->type = ATA_PRIV_MISC;
  33. rq->special = &timeout;
  34. blk_execute_rq(q, NULL, rq, 1);
  35. rc = scsi_req(rq)->result ? -EIO : 0;
  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, REQ_OP_DRV_IN, BLK_MQ_REQ_NOWAIT);
  44. if (IS_ERR(rq))
  45. goto out;
  46. scsi_req(rq)->cmd[0] = REQ_UNPARK_HEADS;
  47. scsi_req(rq)->cmd_len = 1;
  48. ide_req(rq)->type = ATA_PRIV_MISC;
  49. elv_add_request(q, rq, ELEVATOR_INSERT_FRONT);
  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 (scsi_req(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 = kstrtol(buf, 10, &input);
  102. if (rc)
  103. return rc;
  104. if (input < -2)
  105. return -EINVAL;
  106. if (input > MAX_PARK_TIMEOUT) {
  107. input = MAX_PARK_TIMEOUT;
  108. rc = -EOVERFLOW;
  109. }
  110. mutex_lock(&ide_setting_mtx);
  111. if (input >= 0) {
  112. if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
  113. rc = -EOPNOTSUPP;
  114. else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
  115. issue_park_cmd(drive, msecs_to_jiffies(input));
  116. } else {
  117. if (drive->media == ide_disk)
  118. switch (input) {
  119. case -1:
  120. drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
  121. break;
  122. case -2:
  123. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  124. break;
  125. }
  126. else
  127. rc = -EOPNOTSUPP;
  128. }
  129. mutex_unlock(&ide_setting_mtx);
  130. return rc ? rc : len;
  131. }