blob: d48c17003874540a161be616c0552cd730929b69 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +02002/*
3 * IDE ioctls handling.
4 */
5
Paul Gortmaker38789fd2011-07-17 15:33:58 -04006#include <linux/export.h>
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +02007#include <linux/hdreg.h>
8#include <linux/ide.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020010
11static const struct ide_ioctl_devset ide_ioctl_settings[] = {
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020012{ HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit },
13{ HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings },
14{ HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq },
15{ HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma },
16{ -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode },
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020017{ 0 }
18};
19
20int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev,
21 unsigned int cmd, unsigned long arg,
22 const struct ide_ioctl_devset *s)
23{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020024 const struct ide_devset *ds;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020025 int err = -EOPNOTSUPP;
26
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020027 for (; (ds = s->setting); s++) {
28 if (ds->get && s->get_ioctl == cmd)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020029 goto read_val;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020030 else if (ds->set && s->set_ioctl == cmd)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020031 goto set_val;
32 }
33
34 return err;
35
36read_val:
37 mutex_lock(&ide_setting_mtx);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020038 err = ds->get(drive);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020039 mutex_unlock(&ide_setting_mtx);
40 return err >= 0 ? put_user(err, (long __user *)arg) : err;
41
42set_val:
43 if (bdev != bdev->bd_contains)
44 err = -EINVAL;
45 else {
46 if (!capable(CAP_SYS_ADMIN))
47 err = -EACCES;
48 else {
49 mutex_lock(&ide_setting_mtx);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020050 err = ide_devset_execute(drive, ds, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020051 mutex_unlock(&ide_setting_mtx);
52 }
53 }
54 return err;
55}
56EXPORT_SYMBOL_GPL(ide_setting_ioctl);
57
58static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
59 unsigned long arg)
60{
61 u16 *id = NULL;
62 int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
63 int rc = 0;
64
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020065 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020066 rc = -ENOMSG;
67 goto out;
68 }
69
Christian Engelmayere18ed142009-06-29 19:31:41 -070070 /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
71 id = kmalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020072 if (id == NULL) {
73 rc = -ENOMEM;
74 goto out;
75 }
76
77 memcpy(id, drive->id, size);
78 ata_id_to_hd_driveid(id);
79
80 if (copy_to_user((void __user *)arg, id, size))
81 rc = -EFAULT;
82
83 kfree(id);
84out:
85 return rc;
86}
87
88static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
89{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020090 return put_user((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
91 << IDE_NICE_DSC_OVERLAP) |
92 (!!(drive->dev_flags & IDE_DFLAG_NICE1)
93 << IDE_NICE_1), (long __user *)arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020094}
95
96static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
97{
98 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
99 return -EPERM;
100
101 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
Borislav Petkov53174642009-01-02 16:12:54 +0100102 (drive->media != ide_tape))
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200103 return -EPERM;
104
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200105 if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
106 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
107 else
108 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
109
110 if ((arg >> IDE_NICE_1) & 1)
111 drive->dev_flags |= IDE_DFLAG_NICE1;
112 else
113 drive->dev_flags &= ~IDE_DFLAG_NICE1;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200114
115 return 0;
116}
117
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100118static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200119{
120 u8 *buf = NULL;
121 int bufsize = 0, err = 0;
122 u8 args[4], xfer_rate = 0;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100123 struct ide_cmd cmd;
124 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200125
126 if (NULL == (void *) arg) {
127 struct request *rq;
128
Christoph Hellwigff005a02018-05-09 09:54:05 +0200129 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100130 ide_req(rq)->type = ATA_PRIV_TASKFILE;
Christoph Hellwigb7819b92017-04-20 16:02:55 +0200131 blk_execute_rq(drive->queue, NULL, rq, 0);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200132 err = scsi_req(rq)->result ? -EIO : 0;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200133 blk_put_request(rq);
134
135 return err;
136 }
137
138 if (copy_from_user(args, (void __user *)arg, 4))
139 return -EFAULT;
140
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100141 memset(&cmd, 0, sizeof(cmd));
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200142 tf->feature = args[2];
143 if (args[0] == ATA_CMD_SMART) {
144 tf->nsect = args[3];
145 tf->lbal = args[1];
Xinghai Yu6fcf0492013-07-25 18:32:42 +0800146 tf->lbam = ATA_SMART_LBAM_PASS;
147 tf->lbah = ATA_SMART_LBAH_PASS;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200148 cmd.valid.out.tf = IDE_VALID_OUT_TF;
149 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200150 } else {
151 tf->nsect = args[1];
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200152 cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
153 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200154 }
155 tf->command = args[0];
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100156 cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200157
158 if (args[3]) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100159 cmd.tf_flags |= IDE_TFLAG_IO_16BIT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200160 bufsize = SECTOR_SIZE * args[3];
161 buf = kzalloc(bufsize, GFP_KERNEL);
162 if (buf == NULL)
163 return -ENOMEM;
164 }
165
166 if (tf->command == ATA_CMD_SET_FEATURES &&
167 tf->feature == SETFEATURES_XFER &&
Bartlomiej Zolnierkiewicza9c41502009-06-15 22:13:45 +0200168 tf->nsect >= XFER_SW_DMA_0) {
Hemant Pedanekar28c19692009-11-25 15:04:54 -0800169 xfer_rate = ide_find_dma_mode(drive, tf->nsect);
Bartlomiej Zolnierkiewicza9c41502009-06-15 22:13:45 +0200170 if (xfer_rate != tf->nsect) {
171 err = -EINVAL;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200172 goto abort;
173 }
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000174
175 cmd.tf_flags |= IDE_TFLAG_SET_XFER;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200176 }
177
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100178 err = ide_raw_taskfile(drive, &cmd, buf, args[3]);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200179
180 args[0] = tf->status;
181 args[1] = tf->error;
182 args[2] = tf->nsect;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200183abort:
184 if (copy_to_user((void __user *)arg, &args, 4))
185 err = -EFAULT;
186 if (buf) {
187 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
188 err = -EFAULT;
189 kfree(buf);
190 }
191 return err;
192}
193
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100194static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200195{
196 void __user *p = (void __user *)arg;
197 int err = 0;
198 u8 args[7];
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100199 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200200
201 if (copy_from_user(args, p, 7))
202 return -EFAULT;
203
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100204 memset(&cmd, 0, sizeof(cmd));
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200205 memcpy(&cmd.tf.feature, &args[1], 6);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100206 cmd.tf.command = args[0];
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200207 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
208 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200209
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100210 err = ide_no_data_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200211
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100212 args[0] = cmd.tf.command;
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200213 memcpy(&args[1], &cmd.tf.feature, 6);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200214
215 if (copy_to_user(p, args, 7))
216 err = -EFAULT;
217
218 return err;
219}
220
221static int generic_drive_reset(ide_drive_t *drive)
222{
223 struct request *rq;
224 int ret = 0;
225
Christoph Hellwigff005a02018-05-09 09:54:05 +0200226 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100227 ide_req(rq)->type = ATA_PRIV_MISC;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100228 scsi_req(rq)->cmd_len = 1;
229 scsi_req(rq)->cmd[0] = REQ_DRIVE_RESET;
Christoph Hellwigb7819b92017-04-20 16:02:55 +0200230 blk_execute_rq(drive->queue, NULL, rq, 1);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200231 ret = scsi_req(rq)->result;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200232 blk_put_request(rq);
233 return ret;
234}
235
Al Viro1bddd9e2008-09-02 17:19:43 -0400236int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200237 unsigned int cmd, unsigned long arg)
238{
239 int err;
240
241 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
242 if (err != -EOPNOTSUPP)
243 return err;
244
245 switch (cmd) {
246 case HDIO_OBSOLETE_IDENTITY:
247 case HDIO_GET_IDENTITY:
248 if (bdev != bdev->bd_contains)
249 return -EINVAL;
250 return ide_get_identity_ioctl(drive, cmd, arg);
251 case HDIO_GET_NICE:
252 return ide_get_nice_ioctl(drive, arg);
253 case HDIO_SET_NICE:
254 if (!capable(CAP_SYS_ADMIN))
255 return -EACCES;
256 return ide_set_nice_ioctl(drive, arg);
257#ifdef CONFIG_IDE_TASK_IOCTL
258 case HDIO_DRIVE_TASKFILE:
259 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
260 return -EACCES;
261 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100262 return ide_taskfile_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200263 return -ENOMSG;
264#endif
265 case HDIO_DRIVE_CMD:
266 if (!capable(CAP_SYS_RAWIO))
267 return -EACCES;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100268 return ide_cmd_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200269 case HDIO_DRIVE_TASK:
270 if (!capable(CAP_SYS_RAWIO))
271 return -EACCES;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100272 return ide_task_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200273 case HDIO_DRIVE_RESET:
274 if (!capable(CAP_SYS_ADMIN))
275 return -EACCES;
276 return generic_drive_reset(drive);
277 case HDIO_GET_BUSSTATE:
278 if (!capable(CAP_SYS_ADMIN))
279 return -EACCES;
280 if (put_user(BUSSTATE_ON, (long __user *)arg))
281 return -EFAULT;
282 return 0;
283 case HDIO_SET_BUSSTATE:
284 if (!capable(CAP_SYS_ADMIN))
285 return -EACCES;
286 return -EOPNOTSUPP;
287 default:
288 return -EINVAL;
289 }
290}
291EXPORT_SYMBOL(generic_ide_ioctl);