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