| 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 | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 10 | ide_hwif_t *hwif = drive->hwif; | 
| 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 | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 16 | spin_lock_irq(&hwif->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); | 
| Bartlomiej Zolnierkiewicz | 201bffa | 2009-01-02 16:12:50 +0100 | [diff] [blame] | 19 | int start_queue = 0; | 
| Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 20 |  | 
| Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 21 | drive->sleep = timeout; | 
|  | 22 | wake_up_all(&ide_park_wq); | 
| Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 23 | if (reset_timer && del_timer(&hwif->timer)) | 
| Bartlomiej Zolnierkiewicz | 201bffa | 2009-01-02 16:12:50 +0100 | [diff] [blame] | 24 | start_queue = 1; | 
| Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 25 | spin_unlock_irq(&hwif->lock); | 
| Bartlomiej Zolnierkiewicz | 201bffa | 2009-01-02 16:12:50 +0100 | [diff] [blame] | 26 |  | 
| Tejun Heo | 853280a | 2009-04-19 07:00:41 +0900 | [diff] [blame] | 27 | if (start_queue) | 
|  | 28 | blk_run_queue(q); | 
| Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 29 | return; | 
|  | 30 | } | 
| Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 31 | spin_unlock_irq(&hwif->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 |  | 
| Bartlomiej Zolnierkiewicz | c4e66c3 | 2009-03-24 23:22:44 +0100 | [diff] [blame] | 60 | ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq) | 
|  | 61 | { | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 62 | struct ide_cmd cmd; | 
|  | 63 | struct ide_taskfile *tf = &cmd.tf; | 
| Bartlomiej Zolnierkiewicz | c4e66c3 | 2009-03-24 23:22:44 +0100 | [diff] [blame] | 64 |  | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 65 | memset(&cmd, 0, sizeof(cmd)); | 
| Bartlomiej Zolnierkiewicz | c4e66c3 | 2009-03-24 23:22:44 +0100 | [diff] [blame] | 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; | 
| Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 74 | cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE; | 
|  | 75 | cmd.valid.in.tf  = IDE_VALID_IN_TF  | IDE_VALID_DEVICE; | 
| Bartlomiej Zolnierkiewicz | c4e66c3 | 2009-03-24 23:22:44 +0100 | [diff] [blame] | 76 | } else		/* cmd == REQ_UNPARK_HEADS */ | 
|  | 77 | tf->command = ATA_CMD_CHK_POWER; | 
|  | 78 |  | 
| Bartlomiej Zolnierkiewicz | d364c7f | 2009-03-27 12:46:42 +0100 | [diff] [blame] | 79 | cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER; | 
| Bartlomiej Zolnierkiewicz | 0dfb991 | 2009-03-27 12:46:39 +0100 | [diff] [blame] | 80 | cmd.protocol = ATA_PROT_NODATA; | 
|  | 81 |  | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 82 | cmd.rq = rq; | 
| Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 83 |  | 
|  | 84 | return do_rw_taskfile(drive, &cmd); | 
| Bartlomiej Zolnierkiewicz | c4e66c3 | 2009-03-24 23:22:44 +0100 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 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); | 
| Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 91 | ide_hwif_t *hwif = drive->hwif; | 
| Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 92 | unsigned long now; | 
|  | 93 | unsigned int msecs; | 
|  | 94 |  | 
|  | 95 | if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD) | 
|  | 96 | return -EOPNOTSUPP; | 
|  | 97 |  | 
| Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 98 | spin_lock_irq(&hwif->lock); | 
| Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 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; | 
| Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 105 | spin_unlock_irq(&hwif->lock); | 
| Elias Oltmanns | 4abdc6e | 2008-10-13 21:39:50 +0200 | [diff] [blame] | 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 | } |