blob: a914023d6d035d9e530178ff2422a7bf386cc2cd [file] [log] [blame]
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +02001#include <linux/kernel.h>
2#include <linux/ide.h>
3#include <linux/jiffies.h>
4#include <linux/blkdev.h>
5
6DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
7
8static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
9{
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010010 ide_hwif_t *hwif = drive->hwif;
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020011 struct request_queue *q = drive->queue;
12 struct request *rq;
13 int rc;
14
15 timeout += jiffies;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010016 spin_lock_irq(&hwif->lock);
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020017 if (drive->dev_flags & IDE_DFLAG_PARKED) {
Bartlomiej Zolnierkiewicz2a2ca6a2008-12-29 20:27:31 +010018 int reset_timer = time_before(timeout, drive->sleep);
Bartlomiej Zolnierkiewicz201bffa2009-01-02 16:12:50 +010019 int start_queue = 0;
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020020
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020021 drive->sleep = timeout;
22 wake_up_all(&ide_park_wq);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010023 if (reset_timer && del_timer(&hwif->timer))
Bartlomiej Zolnierkiewicz201bffa2009-01-02 16:12:50 +010024 start_queue = 1;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010025 spin_unlock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz201bffa2009-01-02 16:12:50 +010026
Tejun Heo853280a2009-04-19 07:00:41 +090027 if (start_queue)
28 blk_run_queue(q);
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020029 return;
30 }
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010031 spin_unlock_irq(&hwif->lock);
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020032
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
56out:
57 return;
58}
59
Bartlomiej Zolnierkiewiczc4e66c32009-03-24 23:22:44 +010060ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
61{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010062 struct ide_cmd cmd;
63 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewiczc4e66c32009-03-24 23:22:44 +010064
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010065 memset(&cmd, 0, sizeof(cmd));
Bartlomiej Zolnierkiewiczc4e66c32009-03-24 23:22:44 +010066 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;
Sergei Shtylyov60f85012009-04-08 14:13:01 +020074 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
75 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewiczc4e66c32009-03-24 23:22:44 +010076 } else /* cmd == REQ_UNPARK_HEADS */
77 tf->command = ATA_CMD_CHK_POWER;
78
Bartlomiej Zolnierkiewiczd364c7f2009-03-27 12:46:42 +010079 cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010080 cmd.protocol = ATA_PROT_NODATA;
81
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010082 cmd.rq = rq;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010083
84 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewiczc4e66c32009-03-24 23:22:44 +010085}
86
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020087ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
88 char *buf)
89{
90 ide_drive_t *drive = to_ide_device(dev);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010091 ide_hwif_t *hwif = drive->hwif;
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020092 unsigned long now;
93 unsigned int msecs;
94
95 if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
96 return -EOPNOTSUPP;
97
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +010098 spin_lock_irq(&hwif->lock);
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +020099 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;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100105 spin_unlock_irq(&hwif->lock);
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +0200106
107 return snprintf(buf, 20, "%u\n", msecs);
108}
109
110ssize_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}