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