Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * IDE ioctls handling. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/hdreg.h> |
| 6 | #include <linux/ide.h> |
| 7 | |
| 8 | static const struct ide_ioctl_devset ide_ioctl_settings[] = { |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 9 | { HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit }, |
| 10 | { HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings }, |
| 11 | { HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq }, |
| 12 | { HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma }, |
| 13 | { -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode }, |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 14 | { 0 } |
| 15 | }; |
| 16 | |
| 17 | int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev, |
| 18 | unsigned int cmd, unsigned long arg, |
| 19 | const struct ide_ioctl_devset *s) |
| 20 | { |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 21 | const struct ide_devset *ds; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 22 | int err = -EOPNOTSUPP; |
| 23 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 24 | for (; (ds = s->setting); s++) { |
| 25 | if (ds->get && s->get_ioctl == cmd) |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 26 | goto read_val; |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 27 | else if (ds->set && s->set_ioctl == cmd) |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 28 | goto set_val; |
| 29 | } |
| 30 | |
| 31 | return err; |
| 32 | |
| 33 | read_val: |
| 34 | mutex_lock(&ide_setting_mtx); |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 35 | err = ds->get(drive); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 36 | mutex_unlock(&ide_setting_mtx); |
| 37 | return err >= 0 ? put_user(err, (long __user *)arg) : err; |
| 38 | |
| 39 | set_val: |
| 40 | if (bdev != bdev->bd_contains) |
| 41 | err = -EINVAL; |
| 42 | else { |
| 43 | if (!capable(CAP_SYS_ADMIN)) |
| 44 | err = -EACCES; |
| 45 | else { |
| 46 | mutex_lock(&ide_setting_mtx); |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 47 | err = ide_devset_execute(drive, ds, arg); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 48 | mutex_unlock(&ide_setting_mtx); |
| 49 | } |
| 50 | } |
| 51 | return err; |
| 52 | } |
| 53 | EXPORT_SYMBOL_GPL(ide_setting_ioctl); |
| 54 | |
| 55 | static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd, |
| 56 | unsigned long arg) |
| 57 | { |
| 58 | u16 *id = NULL; |
| 59 | int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142; |
| 60 | int rc = 0; |
| 61 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 62 | if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) { |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 63 | rc = -ENOMSG; |
| 64 | goto out; |
| 65 | } |
| 66 | |
| 67 | id = kmalloc(size, GFP_KERNEL); |
| 68 | if (id == NULL) { |
| 69 | rc = -ENOMEM; |
| 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | memcpy(id, drive->id, size); |
| 74 | ata_id_to_hd_driveid(id); |
| 75 | |
| 76 | if (copy_to_user((void __user *)arg, id, size)) |
| 77 | rc = -EFAULT; |
| 78 | |
| 79 | kfree(id); |
| 80 | out: |
| 81 | return rc; |
| 82 | } |
| 83 | |
| 84 | static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg) |
| 85 | { |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 86 | return put_user((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) |
| 87 | << IDE_NICE_DSC_OVERLAP) | |
| 88 | (!!(drive->dev_flags & IDE_DFLAG_NICE1) |
| 89 | << IDE_NICE_1), (long __user *)arg); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg) |
| 93 | { |
| 94 | if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1)))) |
| 95 | return -EPERM; |
| 96 | |
| 97 | if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) && |
Borislav Petkov | 5317464 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 98 | (drive->media != ide_tape)) |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 99 | return -EPERM; |
| 100 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 101 | if ((arg >> IDE_NICE_DSC_OVERLAP) & 1) |
| 102 | drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP; |
| 103 | else |
| 104 | drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; |
| 105 | |
| 106 | if ((arg >> IDE_NICE_1) & 1) |
| 107 | drive->dev_flags |= IDE_DFLAG_NICE1; |
| 108 | else |
| 109 | drive->dev_flags &= ~IDE_DFLAG_NICE1; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 114 | static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg) |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 115 | { |
| 116 | u8 *buf = NULL; |
| 117 | int bufsize = 0, err = 0; |
| 118 | u8 args[4], xfer_rate = 0; |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 119 | struct ide_cmd cmd; |
| 120 | struct ide_taskfile *tf = &cmd.tf; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 121 | u16 *id = drive->id; |
| 122 | |
| 123 | if (NULL == (void *) arg) { |
| 124 | struct request *rq; |
| 125 | |
| 126 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 127 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; |
| 128 | err = blk_execute_rq(drive->queue, NULL, rq, 0); |
| 129 | blk_put_request(rq); |
| 130 | |
| 131 | return err; |
| 132 | } |
| 133 | |
| 134 | if (copy_from_user(args, (void __user *)arg, 4)) |
| 135 | return -EFAULT; |
| 136 | |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 137 | memset(&cmd, 0, sizeof(cmd)); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 138 | tf->feature = args[2]; |
| 139 | if (args[0] == ATA_CMD_SMART) { |
| 140 | tf->nsect = args[3]; |
| 141 | tf->lbal = args[1]; |
| 142 | tf->lbam = 0x4f; |
| 143 | tf->lbah = 0xc2; |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 144 | cmd.valid.out.tf = IDE_VALID_OUT_TF; |
| 145 | cmd.valid.in.tf = IDE_VALID_NSECT; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 146 | } else { |
| 147 | tf->nsect = args[1]; |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 148 | cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT; |
| 149 | cmd.valid.in.tf = IDE_VALID_NSECT; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 150 | } |
| 151 | tf->command = args[0]; |
Bartlomiej Zolnierkiewicz | 0dfb991 | 2009-03-27 12:46:39 +0100 | [diff] [blame] | 152 | cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 153 | |
| 154 | if (args[3]) { |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 155 | cmd.tf_flags |= IDE_TFLAG_IO_16BIT; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 156 | bufsize = SECTOR_SIZE * args[3]; |
| 157 | buf = kzalloc(bufsize, GFP_KERNEL); |
| 158 | if (buf == NULL) |
| 159 | return -ENOMEM; |
| 160 | } |
| 161 | |
| 162 | if (tf->command == ATA_CMD_SET_FEATURES && |
| 163 | tf->feature == SETFEATURES_XFER && |
| 164 | tf->nsect >= XFER_SW_DMA_0 && |
| 165 | (id[ATA_ID_UDMA_MODES] || |
| 166 | id[ATA_ID_MWDMA_MODES] || |
| 167 | id[ATA_ID_SWDMA_MODES])) { |
| 168 | xfer_rate = args[1]; |
| 169 | if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) { |
| 170 | printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot " |
| 171 | "be set\n", drive->name); |
| 172 | goto abort; |
| 173 | } |
| 174 | } |
| 175 | |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 176 | err = ide_raw_taskfile(drive, &cmd, buf, args[3]); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 177 | |
| 178 | args[0] = tf->status; |
| 179 | args[1] = tf->error; |
| 180 | args[2] = tf->nsect; |
| 181 | |
| 182 | if (!err && xfer_rate) { |
| 183 | /* active-retuning-calls future */ |
| 184 | ide_set_xfer_rate(drive, xfer_rate); |
| 185 | ide_driveid_update(drive); |
| 186 | } |
| 187 | abort: |
| 188 | if (copy_to_user((void __user *)arg, &args, 4)) |
| 189 | err = -EFAULT; |
| 190 | if (buf) { |
| 191 | if (copy_to_user((void __user *)(arg + 4), buf, bufsize)) |
| 192 | err = -EFAULT; |
| 193 | kfree(buf); |
| 194 | } |
| 195 | return err; |
| 196 | } |
| 197 | |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 198 | static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg) |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 199 | { |
| 200 | void __user *p = (void __user *)arg; |
| 201 | int err = 0; |
| 202 | u8 args[7]; |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 203 | struct ide_cmd cmd; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 204 | |
| 205 | if (copy_from_user(args, p, 7)) |
| 206 | return -EFAULT; |
| 207 | |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 208 | memset(&cmd, 0, sizeof(cmd)); |
Sergei Shtylyov | 745483f | 2009-04-08 14:13:02 +0200 | [diff] [blame^] | 209 | memcpy(&cmd.tf.feature, &args[1], 6); |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 210 | cmd.tf.command = args[0]; |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 211 | cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE; |
| 212 | cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 213 | |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 214 | err = ide_no_data_taskfile(drive, &cmd); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 215 | |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 216 | args[0] = cmd.tf.command; |
Sergei Shtylyov | 745483f | 2009-04-08 14:13:02 +0200 | [diff] [blame^] | 217 | memcpy(&args[1], &cmd.tf.feature, 6); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 218 | |
| 219 | if (copy_to_user(p, args, 7)) |
| 220 | err = -EFAULT; |
| 221 | |
| 222 | return err; |
| 223 | } |
| 224 | |
| 225 | static int generic_drive_reset(ide_drive_t *drive) |
| 226 | { |
| 227 | struct request *rq; |
| 228 | int ret = 0; |
| 229 | |
| 230 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 231 | rq->cmd_type = REQ_TYPE_SPECIAL; |
| 232 | rq->cmd_len = 1; |
| 233 | rq->cmd[0] = REQ_DRIVE_RESET; |
| 234 | rq->cmd_flags |= REQ_SOFTBARRIER; |
| 235 | if (blk_execute_rq(drive->queue, NULL, rq, 1)) |
| 236 | ret = rq->errors; |
| 237 | blk_put_request(rq); |
| 238 | return ret; |
| 239 | } |
| 240 | |
Al Viro | 1bddd9e | 2008-09-02 17:19:43 -0400 | [diff] [blame] | 241 | int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev, |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 242 | unsigned int cmd, unsigned long arg) |
| 243 | { |
| 244 | int err; |
| 245 | |
| 246 | err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings); |
| 247 | if (err != -EOPNOTSUPP) |
| 248 | return err; |
| 249 | |
| 250 | switch (cmd) { |
| 251 | case HDIO_OBSOLETE_IDENTITY: |
| 252 | case HDIO_GET_IDENTITY: |
| 253 | if (bdev != bdev->bd_contains) |
| 254 | return -EINVAL; |
| 255 | return ide_get_identity_ioctl(drive, cmd, arg); |
| 256 | case HDIO_GET_NICE: |
| 257 | return ide_get_nice_ioctl(drive, arg); |
| 258 | case HDIO_SET_NICE: |
| 259 | if (!capable(CAP_SYS_ADMIN)) |
| 260 | return -EACCES; |
| 261 | return ide_set_nice_ioctl(drive, arg); |
| 262 | #ifdef CONFIG_IDE_TASK_IOCTL |
| 263 | case HDIO_DRIVE_TASKFILE: |
| 264 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 265 | return -EACCES; |
| 266 | if (drive->media == ide_disk) |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 267 | return ide_taskfile_ioctl(drive, arg); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 268 | return -ENOMSG; |
| 269 | #endif |
| 270 | case HDIO_DRIVE_CMD: |
| 271 | if (!capable(CAP_SYS_RAWIO)) |
| 272 | return -EACCES; |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 273 | return ide_cmd_ioctl(drive, arg); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 274 | case HDIO_DRIVE_TASK: |
| 275 | if (!capable(CAP_SYS_RAWIO)) |
| 276 | return -EACCES; |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 277 | return ide_task_ioctl(drive, arg); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 278 | case HDIO_DRIVE_RESET: |
| 279 | if (!capable(CAP_SYS_ADMIN)) |
| 280 | return -EACCES; |
| 281 | return generic_drive_reset(drive); |
| 282 | case HDIO_GET_BUSSTATE: |
| 283 | if (!capable(CAP_SYS_ADMIN)) |
| 284 | return -EACCES; |
| 285 | if (put_user(BUSSTATE_ON, (long __user *)arg)) |
| 286 | return -EFAULT; |
| 287 | return 0; |
| 288 | case HDIO_SET_BUSSTATE: |
| 289 | if (!capable(CAP_SYS_ADMIN)) |
| 290 | return -EACCES; |
| 291 | return -EOPNOTSUPP; |
| 292 | default: |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | } |
| 296 | EXPORT_SYMBOL(generic_ide_ioctl); |