1 #include <linux/kernel.h>
3 #include <linux/jiffies.h>
4 #include <linux/blkdev.h>
6 DECLARE_WAIT_QUEUE_HEAD(ide_park_wq
);
8 static void issue_park_cmd(ide_drive_t
*drive
, unsigned long timeout
)
10 ide_hwgroup_t
*hwgroup
= drive
->hwif
->hwgroup
;
11 struct request_queue
*q
= drive
->queue
;
16 spin_lock_irq(&hwgroup
->lock
);
17 if (drive
->dev_flags
& IDE_DFLAG_PARKED
) {
18 int reset_timer
= time_before(timeout
, drive
->sleep
);
20 drive
->sleep
= timeout
;
21 wake_up_all(&ide_park_wq
);
22 if (reset_timer
&& hwgroup
->sleeping
&&
23 del_timer(&hwgroup
->timer
)) {
24 hwgroup
->sleeping
= 0;
26 blk_start_queueing(q
);
28 spin_unlock_irq(&hwgroup
->lock
);
31 spin_unlock_irq(&hwgroup
->lock
);
33 rq
= blk_get_request(q
, READ
, __GFP_WAIT
);
34 rq
->cmd
[0] = REQ_PARK_HEADS
;
36 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
37 rq
->special
= &timeout
;
38 rc
= blk_execute_rq(q
, NULL
, rq
, 1);
44 * Make sure that *some* command is sent to the drive after the
45 * timeout has expired, so power management will be reenabled.
47 rq
= blk_get_request(q
, READ
, GFP_NOWAIT
);
51 rq
->cmd
[0] = REQ_UNPARK_HEADS
;
53 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
54 elv_add_request(q
, rq
, ELEVATOR_INSERT_FRONT
, 1);
60 ssize_t
ide_park_show(struct device
*dev
, struct device_attribute
*attr
,
63 ide_drive_t
*drive
= to_ide_device(dev
);
64 ide_hwgroup_t
*hwgroup
= drive
->hwif
->hwgroup
;
68 if (drive
->dev_flags
& IDE_DFLAG_NO_UNLOAD
)
71 spin_lock_irq(&hwgroup
->lock
);
73 if (drive
->dev_flags
& IDE_DFLAG_PARKED
&&
74 time_after(drive
->sleep
, now
))
75 msecs
= jiffies_to_msecs(drive
->sleep
- now
);
78 spin_unlock_irq(&hwgroup
->lock
);
80 return snprintf(buf
, 20, "%u\n", msecs
);
83 ssize_t
ide_park_store(struct device
*dev
, struct device_attribute
*attr
,
84 const char *buf
, size_t len
)
86 #define MAX_PARK_TIMEOUT 30000
87 ide_drive_t
*drive
= to_ide_device(dev
);
91 rc
= strict_strtol(buf
, 10, &input
);
94 if (input
> MAX_PARK_TIMEOUT
) {
95 input
= MAX_PARK_TIMEOUT
;
99 mutex_lock(&ide_setting_mtx
);
101 if (drive
->dev_flags
& IDE_DFLAG_NO_UNLOAD
)
103 else if (input
|| drive
->dev_flags
& IDE_DFLAG_PARKED
)
104 issue_park_cmd(drive
, msecs_to_jiffies(input
));
106 if (drive
->media
== ide_disk
)
109 drive
->dev_flags
&= ~IDE_DFLAG_NO_UNLOAD
;
112 drive
->dev_flags
|= IDE_DFLAG_NO_UNLOAD
;
118 mutex_unlock(&ide_setting_mtx
);
120 return rc
? rc
: len
;