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