^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/ide.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) ide_hwif_t *hwif = drive->hwif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct request_queue *q = drive->queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) timeout += jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) spin_lock_irq(&hwif->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (drive->dev_flags & IDE_DFLAG_PARKED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int reset_timer = time_before(timeout, drive->sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int start_queue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) drive->sleep = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) wake_up_all(&ide_park_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (reset_timer && del_timer(&hwif->timer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) start_queue = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) spin_unlock_irq(&hwif->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (start_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) blk_mq_run_hw_queues(q, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) spin_unlock_irq(&hwif->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) rq = blk_get_request(q, REQ_OP_DRV_IN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) scsi_req(rq)->cmd[0] = REQ_PARK_HEADS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) scsi_req(rq)->cmd_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ide_req(rq)->type = ATA_PRIV_MISC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ide_req(rq)->special = &timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) blk_execute_rq(q, NULL, rq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) rc = scsi_req(rq)->result ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) blk_put_request(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Make sure that *some* command is sent to the drive after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * timeout has expired, so power management will be reenabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) rq = blk_get_request(q, REQ_OP_DRV_IN, BLK_MQ_REQ_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (IS_ERR(rq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) scsi_req(rq)->cmd[0] = REQ_UNPARK_HEADS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) scsi_req(rq)->cmd_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ide_req(rq)->type = ATA_PRIV_MISC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) spin_lock_irq(&hwif->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ide_insert_request_head(drive, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) spin_unlock_irq(&hwif->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct ide_cmd cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ide_taskfile *tf = &cmd.tf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) memset(&cmd, 0, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (scsi_req(rq)->cmd[0] == REQ_PARK_HEADS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) drive->sleep = *(unsigned long *)ide_req(rq)->special;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) drive->dev_flags |= IDE_DFLAG_SLEEPING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) tf->command = ATA_CMD_IDLEIMMEDIATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) tf->feature = 0x44;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) tf->lbal = 0x4c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) tf->lbam = 0x4e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) tf->lbah = 0x55;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) } else /* cmd == REQ_UNPARK_HEADS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) tf->command = ATA_CMD_CHK_POWER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) cmd.protocol = ATA_PROT_NODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) cmd.rq = rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return do_rw_taskfile(drive, &cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ide_drive_t *drive = to_ide_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ide_hwif_t *hwif = drive->hwif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned long now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unsigned int msecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) spin_lock_irq(&hwif->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (drive->dev_flags & IDE_DFLAG_PARKED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) time_after(drive->sleep, now))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) msecs = jiffies_to_msecs(drive->sleep - now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) msecs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) spin_unlock_irq(&hwif->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return snprintf(buf, 20, "%u\n", msecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define MAX_PARK_TIMEOUT 30000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ide_drive_t *drive = to_ide_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) long int input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) rc = kstrtol(buf, 10, &input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (input < -2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (input > MAX_PARK_TIMEOUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) input = MAX_PARK_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) rc = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) mutex_lock(&ide_setting_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (input >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) rc = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) issue_park_cmd(drive, msecs_to_jiffies(input));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (drive->media == ide_disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) switch (input) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) case -1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) case -2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) rc = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) mutex_unlock(&ide_setting_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return rc ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }