]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/ide/ide-park.c
Merge tag 'drm-fixes-for-v4.13-rc4' of git://people.freedesktop.org/~airlied/linux
[karo-tx-linux.git] / drivers / ide / ide-park.c
1 #include <linux/kernel.h>
2 #include <linux/gfp.h>
3 #include <linux/ide.h>
4 #include <linux/jiffies.h>
5 #include <linux/blkdev.h>
6
7 DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
8
9 static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
10 {
11         ide_hwif_t *hwif = drive->hwif;
12         struct request_queue *q = drive->queue;
13         struct request *rq;
14         int rc;
15
16         timeout += jiffies;
17         spin_lock_irq(&hwif->lock);
18         if (drive->dev_flags & IDE_DFLAG_PARKED) {
19                 int reset_timer = time_before(timeout, drive->sleep);
20                 int start_queue = 0;
21
22                 drive->sleep = timeout;
23                 wake_up_all(&ide_park_wq);
24                 if (reset_timer && del_timer(&hwif->timer))
25                         start_queue = 1;
26                 spin_unlock_irq(&hwif->lock);
27
28                 if (start_queue)
29                         blk_run_queue(q);
30                 return;
31         }
32         spin_unlock_irq(&hwif->lock);
33
34         rq = blk_get_request(q, REQ_OP_DRV_IN, __GFP_RECLAIM);
35         scsi_req(rq)->cmd[0] = REQ_PARK_HEADS;
36         scsi_req(rq)->cmd_len = 1;
37         ide_req(rq)->type = ATA_PRIV_MISC;
38         rq->special = &timeout;
39         blk_execute_rq(q, NULL, rq, 1);
40         rc = scsi_req(rq)->result ? -EIO : 0;
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          */
49         rq = blk_get_request(q, REQ_OP_DRV_IN, GFP_NOWAIT);
50         if (IS_ERR(rq))
51                 goto out;
52
53         scsi_req(rq)->cmd[0] = REQ_UNPARK_HEADS;
54         scsi_req(rq)->cmd_len = 1;
55         ide_req(rq)->type = ATA_PRIV_MISC;
56         elv_add_request(q, rq, ELEVATOR_INSERT_FRONT);
57
58 out:
59         return;
60 }
61
62 ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
63 {
64         struct ide_cmd cmd;
65         struct ide_taskfile *tf = &cmd.tf;
66
67         memset(&cmd, 0, sizeof(cmd));
68         if (scsi_req(rq)->cmd[0] == REQ_PARK_HEADS) {
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;
76                 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
77                 cmd.valid.in.tf  = IDE_VALID_IN_TF  | IDE_VALID_DEVICE;
78         } else          /* cmd == REQ_UNPARK_HEADS */
79                 tf->command = ATA_CMD_CHK_POWER;
80
81         cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
82         cmd.protocol = ATA_PROT_NODATA;
83
84         cmd.rq = rq;
85
86         return do_rw_taskfile(drive, &cmd);
87 }
88
89 ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
90                       char *buf)
91 {
92         ide_drive_t *drive = to_ide_device(dev);
93         ide_hwif_t *hwif = drive->hwif;
94         unsigned long now;
95         unsigned int msecs;
96
97         if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
98                 return -EOPNOTSUPP;
99
100         spin_lock_irq(&hwif->lock);
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;
107         spin_unlock_irq(&hwif->lock);
108
109         return snprintf(buf, 20, "%u\n", msecs);
110 }
111
112 ssize_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
120         rc = kstrtol(buf, 10, &input);
121         if (rc)
122                 return rc;
123         if (input < -2)
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 }