Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 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 | { |
Bartlomiej Zolnierkiewicz | 2a2ca6a | 2008-12-29 20:27:31 +0100 | [diff] [blame] | 10 | ide_hwgroup_t *hwgroup = drive->hwif->hwgroup; |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 11 | struct request_queue *q = drive->queue; |
| 12 | struct request *rq; |
| 13 | int rc; |
| 14 | |
| 15 | timeout += jiffies; |
Bartlomiej Zolnierkiewicz | 2a2ca6a | 2008-12-29 20:27:31 +0100 | [diff] [blame] | 16 | spin_lock_irq(&hwgroup->lock); |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 17 | if (drive->dev_flags & IDE_DFLAG_PARKED) { |
Bartlomiej Zolnierkiewicz | 2a2ca6a | 2008-12-29 20:27:31 +0100 | [diff] [blame] | 18 | int reset_timer = time_before(timeout, drive->sleep); |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 19 | |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 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; |
| 25 | hwgroup->busy = 0; |
| 26 | blk_start_queueing(q); |
| 27 | } |
Bartlomiej Zolnierkiewicz | 2a2ca6a | 2008-12-29 20:27:31 +0100 | [diff] [blame] | 28 | spin_unlock_irq(&hwgroup->lock); |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 29 | return; |
| 30 | } |
Bartlomiej Zolnierkiewicz | 2a2ca6a | 2008-12-29 20:27:31 +0100 | [diff] [blame] | 31 | spin_unlock_irq(&hwgroup->lock); |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 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 | ssize_t ide_park_show(struct device *dev, struct device_attribute *attr, |
| 61 | char *buf) |
| 62 | { |
| 63 | ide_drive_t *drive = to_ide_device(dev); |
Bartlomiej Zolnierkiewicz | 2a2ca6a | 2008-12-29 20:27:31 +0100 | [diff] [blame] | 64 | ide_hwgroup_t *hwgroup = drive->hwif->hwgroup; |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 65 | unsigned long now; |
| 66 | unsigned int msecs; |
| 67 | |
| 68 | if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD) |
| 69 | return -EOPNOTSUPP; |
| 70 | |
Bartlomiej Zolnierkiewicz | 2a2ca6a | 2008-12-29 20:27:31 +0100 | [diff] [blame] | 71 | spin_lock_irq(&hwgroup->lock); |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 72 | now = jiffies; |
| 73 | if (drive->dev_flags & IDE_DFLAG_PARKED && |
| 74 | time_after(drive->sleep, now)) |
| 75 | msecs = jiffies_to_msecs(drive->sleep - now); |
| 76 | else |
| 77 | msecs = 0; |
Bartlomiej Zolnierkiewicz | 2a2ca6a | 2008-12-29 20:27:31 +0100 | [diff] [blame] | 78 | spin_unlock_irq(&hwgroup->lock); |
Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 79 | |
| 80 | return snprintf(buf, 20, "%u\n", msecs); |
| 81 | } |
| 82 | |
| 83 | ssize_t ide_park_store(struct device *dev, struct device_attribute *attr, |
| 84 | const char *buf, size_t len) |
| 85 | { |
| 86 | #define MAX_PARK_TIMEOUT 30000 |
| 87 | ide_drive_t *drive = to_ide_device(dev); |
| 88 | long int input; |
| 89 | int rc; |
| 90 | |
| 91 | rc = strict_strtol(buf, 10, &input); |
| 92 | if (rc || input < -2) |
| 93 | return -EINVAL; |
| 94 | if (input > MAX_PARK_TIMEOUT) { |
| 95 | input = MAX_PARK_TIMEOUT; |
| 96 | rc = -EOVERFLOW; |
| 97 | } |
| 98 | |
| 99 | mutex_lock(&ide_setting_mtx); |
| 100 | if (input >= 0) { |
| 101 | if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD) |
| 102 | rc = -EOPNOTSUPP; |
| 103 | else if (input || drive->dev_flags & IDE_DFLAG_PARKED) |
| 104 | issue_park_cmd(drive, msecs_to_jiffies(input)); |
| 105 | } else { |
| 106 | if (drive->media == ide_disk) |
| 107 | switch (input) { |
| 108 | case -1: |
| 109 | drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD; |
| 110 | break; |
| 111 | case -2: |
| 112 | drive->dev_flags |= IDE_DFLAG_NO_UNLOAD; |
| 113 | break; |
| 114 | } |
| 115 | else |
| 116 | rc = -EOPNOTSUPP; |
| 117 | } |
| 118 | mutex_unlock(&ide_setting_mtx); |
| 119 | |
| 120 | return rc ? rc : len; |
| 121 | } |