]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/ide/ide-pm.c
ide: convert to blk-mq
[linux.git] / drivers / ide / ide-pm.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/gfp.h>
4 #include <linux/ide.h>
5
6 int generic_ide_suspend(struct device *dev, pm_message_t mesg)
7 {
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         int ret;
14
15         if (ide_port_acpi(hwif)) {
16                 /* call ACPI _GTM only once */
17                 if ((drive->dn & 1) == 0 || pair == NULL)
18                         ide_acpi_get_timing(hwif);
19         }
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         rq->special = &rqpm;
25         rqpm.pm_step = IDE_PM_START_SUSPEND;
26         if (mesg.event == PM_EVENT_PRETHAW)
27                 mesg.event = PM_EVENT_FREEZE;
28         rqpm.pm_state = mesg.event;
29
30         blk_execute_rq(drive->queue, NULL, rq, 0);
31         ret = scsi_req(rq)->result ? -EIO : 0;
32         blk_put_request(rq);
33
34         if (ret == 0 && ide_port_acpi(hwif)) {
35                 /* call ACPI _PS3 only after both devices are suspended */
36                 if ((drive->dn & 1) || pair == NULL)
37                         ide_acpi_set_state(hwif, 0);
38         }
39
40         return ret;
41 }
42
43 static int ide_pm_execute_rq(struct request *rq)
44 {
45         struct request_queue *q = rq->q;
46
47         spin_lock_irq(q->queue_lock);
48         if (unlikely(blk_queue_dying(q))) {
49                 rq->rq_flags |= RQF_QUIET;
50                 scsi_req(rq)->result = -ENXIO;
51                 spin_unlock_irq(q->queue_lock);
52                 blk_mq_end_request(rq, BLK_STS_OK);
53                 return -ENXIO;
54         }
55         spin_unlock_irq(q->queue_lock);
56         blk_execute_rq(q, NULL, rq, true);
57
58         return scsi_req(rq)->result ? -EIO : 0;
59 }
60
61 int generic_ide_resume(struct device *dev)
62 {
63         ide_drive_t *drive = to_ide_device(dev);
64         ide_drive_t *pair = ide_get_pair_dev(drive);
65         ide_hwif_t *hwif = drive->hwif;
66         struct request *rq;
67         struct ide_pm_state rqpm;
68         int err;
69
70         blk_mq_start_stopped_hw_queues(drive->queue, true);
71
72         if (ide_port_acpi(hwif)) {
73                 /* call ACPI _PS0 / _STM only once */
74                 if ((drive->dn & 1) == 0 || pair == NULL) {
75                         ide_acpi_set_state(hwif, 1);
76                         ide_acpi_push_timing(hwif);
77                 }
78
79                 ide_acpi_exec_tfs(drive);
80         }
81
82         memset(&rqpm, 0, sizeof(rqpm));
83         rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_PREEMPT);
84         ide_req(rq)->type = ATA_PRIV_PM_RESUME;
85         rq->special = &rqpm;
86         rqpm.pm_step = IDE_PM_START_RESUME;
87         rqpm.pm_state = PM_EVENT_ON;
88
89         err = ide_pm_execute_rq(rq);
90         blk_put_request(rq);
91
92         if (err == 0 && dev->driver) {
93                 struct ide_driver *drv = to_ide_driver(dev->driver);
94
95                 if (drv->resume)
96                         drv->resume(drive);
97         }
98
99         return err;
100 }
101
102 void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
103 {
104         struct ide_pm_state *pm = rq->special;
105
106 #ifdef DEBUG_PM
107         printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
108                 drive->name, pm->pm_step);
109 #endif
110         if (drive->media != ide_disk)
111                 return;
112
113         switch (pm->pm_step) {
114         case IDE_PM_FLUSH_CACHE:        /* Suspend step 1 (flush cache) */
115                 if (pm->pm_state == PM_EVENT_FREEZE)
116                         pm->pm_step = IDE_PM_COMPLETED;
117                 else
118                         pm->pm_step = IDE_PM_STANDBY;
119                 break;
120         case IDE_PM_STANDBY:            /* Suspend step 2 (standby) */
121                 pm->pm_step = IDE_PM_COMPLETED;
122                 break;
123         case IDE_PM_RESTORE_PIO:        /* Resume step 1 (restore PIO) */
124                 pm->pm_step = IDE_PM_IDLE;
125                 break;
126         case IDE_PM_IDLE:               /* Resume step 2 (idle)*/
127                 pm->pm_step = IDE_PM_RESTORE_DMA;
128                 break;
129         }
130 }
131
132 ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
133 {
134         struct ide_pm_state *pm = rq->special;
135         struct ide_cmd cmd = { };
136
137         switch (pm->pm_step) {
138         case IDE_PM_FLUSH_CACHE:        /* Suspend step 1 (flush cache) */
139                 if (drive->media != ide_disk)
140                         break;
141                 /* Not supported? Switch to next step now. */
142                 if (ata_id_flush_enabled(drive->id) == 0 ||
143                     (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
144                         ide_complete_power_step(drive, rq);
145                         return ide_stopped;
146                 }
147                 if (ata_id_flush_ext_enabled(drive->id))
148                         cmd.tf.command = ATA_CMD_FLUSH_EXT;
149                 else
150                         cmd.tf.command = ATA_CMD_FLUSH;
151                 goto out_do_tf;
152         case IDE_PM_STANDBY:            /* Suspend step 2 (standby) */
153                 cmd.tf.command = ATA_CMD_STANDBYNOW1;
154                 goto out_do_tf;
155         case IDE_PM_RESTORE_PIO:        /* Resume step 1 (restore PIO) */
156                 ide_set_max_pio(drive);
157                 /*
158                  * skip IDE_PM_IDLE for ATAPI devices
159                  */
160                 if (drive->media != ide_disk)
161                         pm->pm_step = IDE_PM_RESTORE_DMA;
162                 else
163                         ide_complete_power_step(drive, rq);
164                 return ide_stopped;
165         case IDE_PM_IDLE:               /* Resume step 2 (idle) */
166                 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
167                 goto out_do_tf;
168         case IDE_PM_RESTORE_DMA:        /* Resume step 3 (restore DMA) */
169                 /*
170                  * Right now, all we do is call ide_set_dma(drive),
171                  * we could be smarter and check for current xfer_speed
172                  * in struct drive etc...
173                  */
174                 if (drive->hwif->dma_ops == NULL)
175                         break;
176                 /*
177                  * TODO: respect IDE_DFLAG_USING_DMA
178                  */
179                 ide_set_dma(drive);
180                 break;
181         }
182
183         pm->pm_step = IDE_PM_COMPLETED;
184
185         return ide_stopped;
186
187 out_do_tf:
188         cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
189         cmd.valid.in.tf  = IDE_VALID_IN_TF  | IDE_VALID_DEVICE;
190         cmd.protocol = ATA_PROT_NODATA;
191
192         return do_rw_taskfile(drive, &cmd);
193 }
194
195 /**
196  *      ide_complete_pm_rq - end the current Power Management request
197  *      @drive: target drive
198  *      @rq: request
199  *
200  *      This function cleans up the current PM request and stops the queue
201  *      if necessary.
202  */
203 void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
204 {
205         struct request_queue *q = drive->queue;
206         struct ide_pm_state *pm = rq->special;
207         unsigned long flags;
208
209         ide_complete_power_step(drive, rq);
210         if (pm->pm_step != IDE_PM_COMPLETED)
211                 return;
212
213 #ifdef DEBUG_PM
214         printk("%s: completing PM request, %s\n", drive->name,
215                (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND) ? "suspend" : "resume");
216 #endif
217         spin_lock_irqsave(q->queue_lock, flags);
218         if (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND)
219                 blk_mq_stop_hw_queues(q);
220         else
221                 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
222         spin_unlock_irqrestore(q->queue_lock, flags);
223
224         drive->hwif->rq = NULL;
225
226         blk_mq_end_request(rq, BLK_STS_OK);
227 }
228
229 void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
230 {
231         struct ide_pm_state *pm = rq->special;
232
233         if (blk_rq_is_private(rq) &&
234             ide_req(rq)->type == ATA_PRIV_PM_SUSPEND &&
235             pm->pm_step == IDE_PM_START_SUSPEND)
236                 /* Mark drive blocked when starting the suspend sequence. */
237                 drive->dev_flags |= IDE_DFLAG_BLOCKED;
238         else if (blk_rq_is_private(rq) &&
239                  ide_req(rq)->type == ATA_PRIV_PM_RESUME &&
240                  pm->pm_step == IDE_PM_START_RESUME) {
241                 /*
242                  * The first thing we do on wakeup is to wait for BSY bit to
243                  * go away (with a looong timeout) as a drive on this hwif may
244                  * just be POSTing itself.
245                  * We do that before even selecting as the "other" device on
246                  * the bus may be broken enough to walk on our toes at this
247                  * point.
248                  */
249                 ide_hwif_t *hwif = drive->hwif;
250                 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
251                 struct request_queue *q = drive->queue;
252                 int rc;
253 #ifdef DEBUG_PM
254                 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
255 #endif
256                 rc = ide_wait_not_busy(hwif, 35000);
257                 if (rc)
258                         printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
259                 tp_ops->dev_select(drive);
260                 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
261                 rc = ide_wait_not_busy(hwif, 100000);
262                 if (rc)
263                         printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
264
265                 blk_mq_start_hw_queues(q);
266         }
267 }