| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 1 | #include <linux/kernel.h> | 
 | 2 | #include <linux/ide.h> | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 3 |  | 
 | 4 | int generic_ide_suspend(struct device *dev, pm_message_t mesg) | 
 | 5 | { | 
 | 6 | 	ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive); | 
| Bartlomiej Zolnierkiewicz | 898ec22 | 2009-01-06 17:20:52 +0100 | [diff] [blame] | 7 | 	ide_hwif_t *hwif = drive->hwif; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 8 | 	struct request *rq; | 
 | 9 | 	struct request_pm_state rqpm; | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 10 | 	struct ide_cmd cmd; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 11 | 	int ret; | 
 | 12 |  | 
 | 13 | 	/* call ACPI _GTM only once */ | 
 | 14 | 	if ((drive->dn & 1) == 0 || pair == NULL) | 
 | 15 | 		ide_acpi_get_timing(hwif); | 
 | 16 |  | 
 | 17 | 	memset(&rqpm, 0, sizeof(rqpm)); | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 18 | 	memset(&cmd, 0, sizeof(cmd)); | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 19 | 	rq = blk_get_request(drive->queue, READ, __GFP_WAIT); | 
 | 20 | 	rq->cmd_type = REQ_TYPE_PM_SUSPEND; | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 21 | 	rq->special = &cmd; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 22 | 	rq->data = &rqpm; | 
 | 23 | 	rqpm.pm_step = IDE_PM_START_SUSPEND; | 
 | 24 | 	if (mesg.event == PM_EVENT_PRETHAW) | 
 | 25 | 		mesg.event = PM_EVENT_FREEZE; | 
 | 26 | 	rqpm.pm_state = mesg.event; | 
 | 27 |  | 
 | 28 | 	ret = blk_execute_rq(drive->queue, NULL, rq, 0); | 
 | 29 | 	blk_put_request(rq); | 
 | 30 |  | 
 | 31 | 	/* call ACPI _PS3 only after both devices are suspended */ | 
 | 32 | 	if (ret == 0 && ((drive->dn & 1) || pair == NULL)) | 
 | 33 | 		ide_acpi_set_state(hwif, 0); | 
 | 34 |  | 
 | 35 | 	return ret; | 
 | 36 | } | 
 | 37 |  | 
 | 38 | int generic_ide_resume(struct device *dev) | 
 | 39 | { | 
 | 40 | 	ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive); | 
| Bartlomiej Zolnierkiewicz | 898ec22 | 2009-01-06 17:20:52 +0100 | [diff] [blame] | 41 | 	ide_hwif_t *hwif = drive->hwif; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 42 | 	struct request *rq; | 
 | 43 | 	struct request_pm_state rqpm; | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 44 | 	struct ide_cmd cmd; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 45 | 	int err; | 
 | 46 |  | 
 | 47 | 	/* call ACPI _PS0 / _STM only once */ | 
 | 48 | 	if ((drive->dn & 1) == 0 || pair == NULL) { | 
 | 49 | 		ide_acpi_set_state(hwif, 1); | 
 | 50 | 		ide_acpi_push_timing(hwif); | 
 | 51 | 	} | 
 | 52 |  | 
 | 53 | 	ide_acpi_exec_tfs(drive); | 
 | 54 |  | 
 | 55 | 	memset(&rqpm, 0, sizeof(rqpm)); | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 56 | 	memset(&cmd, 0, sizeof(cmd)); | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 57 | 	rq = blk_get_request(drive->queue, READ, __GFP_WAIT); | 
 | 58 | 	rq->cmd_type = REQ_TYPE_PM_RESUME; | 
 | 59 | 	rq->cmd_flags |= REQ_PREEMPT; | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 60 | 	rq->special = &cmd; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 61 | 	rq->data = &rqpm; | 
 | 62 | 	rqpm.pm_step = IDE_PM_START_RESUME; | 
 | 63 | 	rqpm.pm_state = PM_EVENT_ON; | 
 | 64 |  | 
 | 65 | 	err = blk_execute_rq(drive->queue, NULL, rq, 1); | 
 | 66 | 	blk_put_request(rq); | 
 | 67 |  | 
 | 68 | 	if (err == 0 && dev->driver) { | 
| Bartlomiej Zolnierkiewicz | 7f3c868 | 2009-01-06 17:20:53 +0100 | [diff] [blame] | 69 | 		struct ide_driver *drv = to_ide_driver(dev->driver); | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 70 |  | 
 | 71 | 		if (drv->resume) | 
 | 72 | 			drv->resume(drive); | 
 | 73 | 	} | 
 | 74 |  | 
 | 75 | 	return err; | 
 | 76 | } | 
 | 77 |  | 
 | 78 | void ide_complete_power_step(ide_drive_t *drive, struct request *rq) | 
 | 79 | { | 
 | 80 | 	struct request_pm_state *pm = rq->data; | 
 | 81 |  | 
 | 82 | #ifdef DEBUG_PM | 
 | 83 | 	printk(KERN_INFO "%s: complete_power_step(step: %d)\n", | 
 | 84 | 		drive->name, pm->pm_step); | 
 | 85 | #endif | 
 | 86 | 	if (drive->media != ide_disk) | 
 | 87 | 		return; | 
 | 88 |  | 
 | 89 | 	switch (pm->pm_step) { | 
 | 90 | 	case IDE_PM_FLUSH_CACHE:	/* Suspend step 1 (flush cache) */ | 
 | 91 | 		if (pm->pm_state == PM_EVENT_FREEZE) | 
 | 92 | 			pm->pm_step = IDE_PM_COMPLETED; | 
 | 93 | 		else | 
 | 94 | 			pm->pm_step = IDE_PM_STANDBY; | 
 | 95 | 		break; | 
 | 96 | 	case IDE_PM_STANDBY:		/* Suspend step 2 (standby) */ | 
 | 97 | 		pm->pm_step = IDE_PM_COMPLETED; | 
 | 98 | 		break; | 
 | 99 | 	case IDE_PM_RESTORE_PIO:	/* Resume step 1 (restore PIO) */ | 
 | 100 | 		pm->pm_step = IDE_PM_IDLE; | 
 | 101 | 		break; | 
 | 102 | 	case IDE_PM_IDLE:		/* Resume step 2 (idle)*/ | 
 | 103 | 		pm->pm_step = IDE_PM_RESTORE_DMA; | 
 | 104 | 		break; | 
 | 105 | 	} | 
 | 106 | } | 
 | 107 |  | 
 | 108 | ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq) | 
 | 109 | { | 
 | 110 | 	struct request_pm_state *pm = rq->data; | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 111 | 	struct ide_cmd *cmd = rq->special; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 112 |  | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 113 | 	memset(cmd, 0, sizeof(*cmd)); | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 114 |  | 
 | 115 | 	switch (pm->pm_step) { | 
 | 116 | 	case IDE_PM_FLUSH_CACHE:	/* Suspend step 1 (flush cache) */ | 
 | 117 | 		if (drive->media != ide_disk) | 
 | 118 | 			break; | 
 | 119 | 		/* Not supported? Switch to next step now. */ | 
 | 120 | 		if (ata_id_flush_enabled(drive->id) == 0 || | 
 | 121 | 		    (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) { | 
 | 122 | 			ide_complete_power_step(drive, rq); | 
 | 123 | 			return ide_stopped; | 
 | 124 | 		} | 
 | 125 | 		if (ata_id_flush_ext_enabled(drive->id)) | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 126 | 			cmd->tf.command = ATA_CMD_FLUSH_EXT; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 127 | 		else | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 128 | 			cmd->tf.command = ATA_CMD_FLUSH; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 129 | 		goto out_do_tf; | 
 | 130 | 	case IDE_PM_STANDBY:		/* Suspend step 2 (standby) */ | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 131 | 		cmd->tf.command = ATA_CMD_STANDBYNOW1; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 132 | 		goto out_do_tf; | 
 | 133 | 	case IDE_PM_RESTORE_PIO:	/* Resume step 1 (restore PIO) */ | 
 | 134 | 		ide_set_max_pio(drive); | 
 | 135 | 		/* | 
 | 136 | 		 * skip IDE_PM_IDLE for ATAPI devices | 
 | 137 | 		 */ | 
 | 138 | 		if (drive->media != ide_disk) | 
 | 139 | 			pm->pm_step = IDE_PM_RESTORE_DMA; | 
 | 140 | 		else | 
 | 141 | 			ide_complete_power_step(drive, rq); | 
 | 142 | 		return ide_stopped; | 
 | 143 | 	case IDE_PM_IDLE:		/* Resume step 2 (idle) */ | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 144 | 		cmd->tf.command = ATA_CMD_IDLEIMMEDIATE; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 145 | 		goto out_do_tf; | 
 | 146 | 	case IDE_PM_RESTORE_DMA:	/* Resume step 3 (restore DMA) */ | 
 | 147 | 		/* | 
 | 148 | 		 * Right now, all we do is call ide_set_dma(drive), | 
 | 149 | 		 * we could be smarter and check for current xfer_speed | 
 | 150 | 		 * in struct drive etc... | 
 | 151 | 		 */ | 
 | 152 | 		if (drive->hwif->dma_ops == NULL) | 
 | 153 | 			break; | 
 | 154 | 		/* | 
 | 155 | 		 * TODO: respect IDE_DFLAG_USING_DMA | 
 | 156 | 		 */ | 
 | 157 | 		ide_set_dma(drive); | 
 | 158 | 		break; | 
 | 159 | 	} | 
 | 160 |  | 
 | 161 | 	pm->pm_step = IDE_PM_COMPLETED; | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 162 |  | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 163 | 	return ide_stopped; | 
 | 164 |  | 
 | 165 | out_do_tf: | 
| Bartlomiej Zolnierkiewicz | 0dfb991 | 2009-03-27 12:46:39 +0100 | [diff] [blame] | 166 | 	cmd->tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; | 
 | 167 | 	cmd->protocol = ATA_PROT_NODATA; | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 168 |  | 
 | 169 | 	return do_rw_taskfile(drive, cmd); | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 170 | } | 
 | 171 |  | 
 | 172 | /** | 
| Bartlomiej Zolnierkiewicz | 3616b65 | 2009-03-27 12:46:29 +0100 | [diff] [blame] | 173 |  *	ide_complete_pm_rq - end the current Power Management request | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 174 |  *	@drive: target drive | 
 | 175 |  *	@rq: request | 
 | 176 |  * | 
 | 177 |  *	This function cleans up the current PM request and stops the queue | 
 | 178 |  *	if necessary. | 
 | 179 |  */ | 
| Bartlomiej Zolnierkiewicz | 3616b65 | 2009-03-27 12:46:29 +0100 | [diff] [blame] | 180 | void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq) | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 181 | { | 
 | 182 | 	struct request_queue *q = drive->queue; | 
| Bartlomiej Zolnierkiewicz | 3616b65 | 2009-03-27 12:46:29 +0100 | [diff] [blame] | 183 | 	struct request_pm_state *pm = rq->data; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 184 | 	unsigned long flags; | 
 | 185 |  | 
| Bartlomiej Zolnierkiewicz | 3616b65 | 2009-03-27 12:46:29 +0100 | [diff] [blame] | 186 | 	ide_complete_power_step(drive, rq); | 
 | 187 | 	if (pm->pm_step != IDE_PM_COMPLETED) | 
 | 188 | 		return; | 
 | 189 |  | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 190 | #ifdef DEBUG_PM | 
 | 191 | 	printk("%s: completing PM request, %s\n", drive->name, | 
 | 192 | 	       blk_pm_suspend_request(rq) ? "suspend" : "resume"); | 
 | 193 | #endif | 
 | 194 | 	spin_lock_irqsave(q->queue_lock, flags); | 
| Bartlomiej Zolnierkiewicz | 2ea5521 | 2009-01-14 19:19:04 +0100 | [diff] [blame] | 195 | 	if (blk_pm_suspend_request(rq)) | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 196 | 		blk_stop_queue(q); | 
| Bartlomiej Zolnierkiewicz | 2ea5521 | 2009-01-14 19:19:04 +0100 | [diff] [blame] | 197 | 	else | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 198 | 		drive->dev_flags &= ~IDE_DFLAG_BLOCKED; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 199 | 	spin_unlock_irqrestore(q->queue_lock, flags); | 
 | 200 |  | 
| Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 201 | 	drive->hwif->rq = NULL; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 202 |  | 
 | 203 | 	if (blk_end_request(rq, 0, 0)) | 
 | 204 | 		BUG(); | 
 | 205 | } | 
 | 206 |  | 
 | 207 | void ide_check_pm_state(ide_drive_t *drive, struct request *rq) | 
 | 208 | { | 
 | 209 | 	struct request_pm_state *pm = rq->data; | 
 | 210 |  | 
 | 211 | 	if (blk_pm_suspend_request(rq) && | 
 | 212 | 	    pm->pm_step == IDE_PM_START_SUSPEND) | 
 | 213 | 		/* Mark drive blocked when starting the suspend sequence. */ | 
 | 214 | 		drive->dev_flags |= IDE_DFLAG_BLOCKED; | 
 | 215 | 	else if (blk_pm_resume_request(rq) && | 
 | 216 | 		 pm->pm_step == IDE_PM_START_RESUME) { | 
 | 217 | 		/* | 
 | 218 | 		 * The first thing we do on wakeup is to wait for BSY bit to | 
 | 219 | 		 * go away (with a looong timeout) as a drive on this hwif may | 
 | 220 | 		 * just be POSTing itself. | 
 | 221 | 		 * We do that before even selecting as the "other" device on | 
 | 222 | 		 * the bus may be broken enough to walk on our toes at this | 
 | 223 | 		 * point. | 
 | 224 | 		 */ | 
 | 225 | 		ide_hwif_t *hwif = drive->hwif; | 
| Sergei Shtylyov | fdd88f0 | 2009-03-31 20:15:33 +0200 | [diff] [blame] | 226 | 		const struct ide_tp_ops *tp_ops = hwif->tp_ops; | 
| Bartlomiej Zolnierkiewicz | 2ea5521 | 2009-01-14 19:19:04 +0100 | [diff] [blame] | 227 | 		struct request_queue *q = drive->queue; | 
 | 228 | 		unsigned long flags; | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 229 | 		int rc; | 
 | 230 | #ifdef DEBUG_PM | 
 | 231 | 		printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name); | 
 | 232 | #endif | 
 | 233 | 		rc = ide_wait_not_busy(hwif, 35000); | 
 | 234 | 		if (rc) | 
 | 235 | 			printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name); | 
| Sergei Shtylyov | fdd88f0 | 2009-03-31 20:15:33 +0200 | [diff] [blame] | 236 | 		tp_ops->dev_select(drive); | 
 | 237 | 		tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS); | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 238 | 		rc = ide_wait_not_busy(hwif, 100000); | 
 | 239 | 		if (rc) | 
 | 240 | 			printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name); | 
| Bartlomiej Zolnierkiewicz | 2ea5521 | 2009-01-14 19:19:04 +0100 | [diff] [blame] | 241 |  | 
 | 242 | 		spin_lock_irqsave(q->queue_lock, flags); | 
 | 243 | 		blk_start_queue(q); | 
 | 244 | 		spin_unlock_irqrestore(q->queue_lock, flags); | 
| Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 245 | 	} | 
 | 246 | } |