ide-pm.c 6.84 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include <linux/kernel.h>
3
#include <linux/gfp.h>
4 5 6 7
#include <linux/ide.h>

int generic_ide_suspend(struct device *dev, pm_message_t mesg)
{
8
	ide_drive_t *drive = to_ide_device(dev);
9
	ide_drive_t *pair = ide_get_pair_dev(drive);
10
	ide_hwif_t *hwif = drive->hwif;
11
	struct request *rq;
12
	struct ide_pm_state rqpm;
13 14
	int ret;

15 16 17 18 19
	if (ide_port_acpi(hwif)) {
		/* call ACPI _GTM only once */
		if ((drive->dn & 1) == 0 || pair == NULL)
			ide_acpi_get_timing(hwif);
	}
20 21

	memset(&rqpm, 0, sizeof(rqpm));
22
	rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
23
	ide_req(rq)->type = ATA_PRIV_PM_SUSPEND;
24
	ide_req(rq)->special = &rqpm;
25 26 27 28 29
	rqpm.pm_step = IDE_PM_START_SUSPEND;
	if (mesg.event == PM_EVENT_PRETHAW)
		mesg.event = PM_EVENT_FREEZE;
	rqpm.pm_state = mesg.event;

30
	blk_execute_rq(drive->queue, NULL, rq, 0);
31
	ret = scsi_req(rq)->result ? -EIO : 0;
32 33
	blk_put_request(rq);

34 35 36 37 38
	if (ret == 0 && ide_port_acpi(hwif)) {
		/* call ACPI _PS3 only after both devices are suspended */
		if ((drive->dn & 1) || pair == NULL)
			ide_acpi_set_state(hwif, 0);
	}
39 40 41 42

	return ret;
}

43 44 45 46 47
static int ide_pm_execute_rq(struct request *rq)
{
	struct request_queue *q = rq->q;

	if (unlikely(blk_queue_dying(q))) {
48
		rq->rq_flags |= RQF_QUIET;
49
		scsi_req(rq)->result = -ENXIO;
Jens Axboe's avatar
Jens Axboe committed
50
		blk_mq_end_request(rq, BLK_STS_OK);
51 52
		return -ENXIO;
	}
Jens Axboe's avatar
Jens Axboe committed
53
	blk_execute_rq(q, NULL, rq, true);
54

55
	return scsi_req(rq)->result ? -EIO : 0;
56 57
}

58 59
int generic_ide_resume(struct device *dev)
{
60
	ide_drive_t *drive = to_ide_device(dev);
61
	ide_drive_t *pair = ide_get_pair_dev(drive);
62
	ide_hwif_t *hwif = drive->hwif;
63
	struct request *rq;
64
	struct ide_pm_state rqpm;
65 66
	int err;

Jens Axboe's avatar
Jens Axboe committed
67 68
	blk_mq_start_stopped_hw_queues(drive->queue, true);

69 70 71 72 73 74
	if (ide_port_acpi(hwif)) {
		/* call ACPI _PS0 / _STM only once */
		if ((drive->dn & 1) == 0 || pair == NULL) {
			ide_acpi_set_state(hwif, 1);
			ide_acpi_push_timing(hwif);
		}
75

76 77
		ide_acpi_exec_tfs(drive);
	}
78 79

	memset(&rqpm, 0, sizeof(rqpm));
80
	rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_PREEMPT);
81
	ide_req(rq)->type = ATA_PRIV_PM_RESUME;
82
	ide_req(rq)->special = &rqpm;
83 84 85
	rqpm.pm_step = IDE_PM_START_RESUME;
	rqpm.pm_state = PM_EVENT_ON;

86
	err = ide_pm_execute_rq(rq);
87 88 89
	blk_put_request(rq);

	if (err == 0 && dev->driver) {
90
		struct ide_driver *drv = to_ide_driver(dev->driver);
91 92 93 94 95 96 97 98 99 100

		if (drv->resume)
			drv->resume(drive);
	}

	return err;
}

void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
{
101
	struct ide_pm_state *pm = ide_req(rq)->special;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

#ifdef DEBUG_PM
	printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
		drive->name, pm->pm_step);
#endif
	if (drive->media != ide_disk)
		return;

	switch (pm->pm_step) {
	case IDE_PM_FLUSH_CACHE:	/* Suspend step 1 (flush cache) */
		if (pm->pm_state == PM_EVENT_FREEZE)
			pm->pm_step = IDE_PM_COMPLETED;
		else
			pm->pm_step = IDE_PM_STANDBY;
		break;
	case IDE_PM_STANDBY:		/* Suspend step 2 (standby) */
		pm->pm_step = IDE_PM_COMPLETED;
		break;
	case IDE_PM_RESTORE_PIO:	/* Resume step 1 (restore PIO) */
		pm->pm_step = IDE_PM_IDLE;
		break;
	case IDE_PM_IDLE:		/* Resume step 2 (idle)*/
		pm->pm_step = IDE_PM_RESTORE_DMA;
		break;
	}
}

ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
{
131
	struct ide_pm_state *pm = ide_req(rq)->special;
Tejun Heo's avatar
Tejun Heo committed
132
	struct ide_cmd cmd = { };
133 134 135 136 137 138 139 140 141 142 143 144

	switch (pm->pm_step) {
	case IDE_PM_FLUSH_CACHE:	/* Suspend step 1 (flush cache) */
		if (drive->media != ide_disk)
			break;
		/* Not supported? Switch to next step now. */
		if (ata_id_flush_enabled(drive->id) == 0 ||
		    (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
			ide_complete_power_step(drive, rq);
			return ide_stopped;
		}
		if (ata_id_flush_ext_enabled(drive->id))
Tejun Heo's avatar
Tejun Heo committed
145
			cmd.tf.command = ATA_CMD_FLUSH_EXT;
146
		else
Tejun Heo's avatar
Tejun Heo committed
147
			cmd.tf.command = ATA_CMD_FLUSH;
148 149
		goto out_do_tf;
	case IDE_PM_STANDBY:		/* Suspend step 2 (standby) */
Tejun Heo's avatar
Tejun Heo committed
150
		cmd.tf.command = ATA_CMD_STANDBYNOW1;
151 152 153 154 155 156 157 158 159 160 161 162
		goto out_do_tf;
	case IDE_PM_RESTORE_PIO:	/* Resume step 1 (restore PIO) */
		ide_set_max_pio(drive);
		/*
		 * skip IDE_PM_IDLE for ATAPI devices
		 */
		if (drive->media != ide_disk)
			pm->pm_step = IDE_PM_RESTORE_DMA;
		else
			ide_complete_power_step(drive, rq);
		return ide_stopped;
	case IDE_PM_IDLE:		/* Resume step 2 (idle) */
Tejun Heo's avatar
Tejun Heo committed
163
		cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		goto out_do_tf;
	case IDE_PM_RESTORE_DMA:	/* Resume step 3 (restore DMA) */
		/*
		 * Right now, all we do is call ide_set_dma(drive),
		 * we could be smarter and check for current xfer_speed
		 * in struct drive etc...
		 */
		if (drive->hwif->dma_ops == NULL)
			break;
		/*
		 * TODO: respect IDE_DFLAG_USING_DMA
		 */
		ide_set_dma(drive);
		break;
	}

	pm->pm_step = IDE_PM_COMPLETED;
181

182 183 184
	return ide_stopped;

out_do_tf:
Tejun Heo's avatar
Tejun Heo committed
185 186 187
	cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
	cmd.valid.in.tf  = IDE_VALID_IN_TF  | IDE_VALID_DEVICE;
	cmd.protocol = ATA_PROT_NODATA;
188

Tejun Heo's avatar
Tejun Heo committed
189
	return do_rw_taskfile(drive, &cmd);
190 191 192
}

/**
193
 *	ide_complete_pm_rq - end the current Power Management request
194 195 196 197 198 199
 *	@drive: target drive
 *	@rq: request
 *
 *	This function cleans up the current PM request and stops the queue
 *	if necessary.
 */
200
void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
201 202
{
	struct request_queue *q = drive->queue;
203
	struct ide_pm_state *pm = ide_req(rq)->special;
204

205 206 207 208
	ide_complete_power_step(drive, rq);
	if (pm->pm_step != IDE_PM_COMPLETED)
		return;

209 210
#ifdef DEBUG_PM
	printk("%s: completing PM request, %s\n", drive->name,
211
	       (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND) ? "suspend" : "resume");
212
#endif
213
	if (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND)
Jens Axboe's avatar
Jens Axboe committed
214
		blk_mq_stop_hw_queues(q);
215
	else
216 217
		drive->dev_flags &= ~IDE_DFLAG_BLOCKED;

218
	drive->hwif->rq = NULL;
219

Jens Axboe's avatar
Jens Axboe committed
220
	blk_mq_end_request(rq, BLK_STS_OK);
221 222 223 224
}

void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
{
225
	struct ide_pm_state *pm = ide_req(rq)->special;
226

227
	if (blk_rq_is_private(rq) &&
228
	    ide_req(rq)->type == ATA_PRIV_PM_SUSPEND &&
229 230 231
	    pm->pm_step == IDE_PM_START_SUSPEND)
		/* Mark drive blocked when starting the suspend sequence. */
		drive->dev_flags |= IDE_DFLAG_BLOCKED;
232
	else if (blk_rq_is_private(rq) &&
233
	         ide_req(rq)->type == ATA_PRIV_PM_RESUME &&
234 235 236 237 238 239 240 241 242 243
		 pm->pm_step == IDE_PM_START_RESUME) {
		/*
		 * The first thing we do on wakeup is to wait for BSY bit to
		 * go away (with a looong timeout) as a drive on this hwif may
		 * just be POSTing itself.
		 * We do that before even selecting as the "other" device on
		 * the bus may be broken enough to walk on our toes at this
		 * point.
		 */
		ide_hwif_t *hwif = drive->hwif;
244
		const struct ide_tp_ops *tp_ops = hwif->tp_ops;
245
		struct request_queue *q = drive->queue;
246 247 248 249 250 251 252
		int rc;
#ifdef DEBUG_PM
		printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
#endif
		rc = ide_wait_not_busy(hwif, 35000);
		if (rc)
			printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
253 254
		tp_ops->dev_select(drive);
		tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
255 256 257
		rc = ide_wait_not_busy(hwif, 100000);
		if (rc)
			printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
258

Jens Axboe's avatar
Jens Axboe committed
259
		blk_mq_start_hw_queues(q);
260 261
	}
}