ide-park.c 3.7 KB

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