blob: 8c0d17297a7a0ab0950edf4aa06321e85fd65098 [file] [log] [blame]
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +02001/*
2 * IDE ioctls handling.
3 */
4
Paul Gortmaker38789fd2011-07-17 15:33:58 -04005#include <linux/export.h>
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +02006#include <linux/hdreg.h>
7#include <linux/ide.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +02009
10static const struct ide_ioctl_devset ide_ioctl_settings[] = {
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020011{ HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit },
12{ HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings },
13{ HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq },
14{ HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma },
15{ -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode },
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020016{ 0 }
17};
18
19int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev,
20 unsigned int cmd, unsigned long arg,
21 const struct ide_ioctl_devset *s)
22{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020023 const struct ide_devset *ds;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020024 int err = -EOPNOTSUPP;
25
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020026 for (; (ds = s->setting); s++) {
27 if (ds->get && s->get_ioctl == cmd)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020028 goto read_val;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020029 else if (ds->set && s->set_ioctl == cmd)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020030 goto set_val;
31 }
32
33 return err;
34
35read_val:
36 mutex_lock(&ide_setting_mtx);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020037 err = ds->get(drive);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020038 mutex_unlock(&ide_setting_mtx);
39 return err >= 0 ? put_user(err, (long __user *)arg) : err;
40
41set_val:
42 if (bdev != bdev->bd_contains)
43 err = -EINVAL;
44 else {
45 if (!capable(CAP_SYS_ADMIN))
46 err = -EACCES;
47 else {
48 mutex_lock(&ide_setting_mtx);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020049 err = ide_devset_execute(drive, ds, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020050 mutex_unlock(&ide_setting_mtx);
51 }
52 }
53 return err;
54}
55EXPORT_SYMBOL_GPL(ide_setting_ioctl);
56
57static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
58 unsigned long arg)
59{
60 u16 *id = NULL;
61 int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
62 int rc = 0;
63
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020064 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020065 rc = -ENOMSG;
66 goto out;
67 }
68
Christian Engelmayere18ed142009-06-29 19:31:41 -070069 /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
70 id = kmalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020071 if (id == NULL) {
72 rc = -ENOMEM;
73 goto out;
74 }
75
76 memcpy(id, drive->id, size);
77 ata_id_to_hd_driveid(id);
78
79 if (copy_to_user((void __user *)arg, id, size))
80 rc = -EFAULT;
81
82 kfree(id);
83out:
84 return rc;
85}
86
87static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
88{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020089 return put_user((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
90 << IDE_NICE_DSC_OVERLAP) |
91 (!!(drive->dev_flags & IDE_DFLAG_NICE1)
92 << IDE_NICE_1), (long __user *)arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020093}
94
95static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
96{
97 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
98 return -EPERM;
99
100 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
Borislav Petkov53174642009-01-02 16:12:54 +0100101 (drive->media != ide_tape))
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200102 return -EPERM;
103
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200104 if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
105 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
106 else
107 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
108
109 if ((arg >> IDE_NICE_1) & 1)
110 drive->dev_flags |= IDE_DFLAG_NICE1;
111 else
112 drive->dev_flags &= ~IDE_DFLAG_NICE1;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200113
114 return 0;
115}
116
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100117static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200118{
119 u8 *buf = NULL;
120 int bufsize = 0, err = 0;
121 u8 args[4], xfer_rate = 0;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100122 struct ide_cmd cmd;
123 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200124
125 if (NULL == (void *) arg) {
126 struct request *rq;
127
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100128 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100129 scsi_req_init(rq);
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 Hellwigaebf5262017-01-31 16:57:31 +0100226 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100227 scsi_req_init(rq);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100228 ide_req(rq)->type = ATA_PRIV_MISC;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100229 scsi_req(rq)->cmd_len = 1;
230 scsi_req(rq)->cmd[0] = REQ_DRIVE_RESET;
Christoph Hellwigb7819b92017-04-20 16:02:55 +0200231 blk_execute_rq(drive->queue, NULL, rq, 1);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200232 ret = scsi_req(rq)->result;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200233 blk_put_request(rq);
234 return ret;
235}
236
Al Viro1bddd9e2008-09-02 17:19:43 -0400237int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200238 unsigned int cmd, unsigned long arg)
239{
240 int err;
241
242 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
243 if (err != -EOPNOTSUPP)
244 return err;
245
246 switch (cmd) {
247 case HDIO_OBSOLETE_IDENTITY:
248 case HDIO_GET_IDENTITY:
249 if (bdev != bdev->bd_contains)
250 return -EINVAL;
251 return ide_get_identity_ioctl(drive, cmd, arg);
252 case HDIO_GET_NICE:
253 return ide_get_nice_ioctl(drive, arg);
254 case HDIO_SET_NICE:
255 if (!capable(CAP_SYS_ADMIN))
256 return -EACCES;
257 return ide_set_nice_ioctl(drive, arg);
258#ifdef CONFIG_IDE_TASK_IOCTL
259 case HDIO_DRIVE_TASKFILE:
260 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
261 return -EACCES;
262 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100263 return ide_taskfile_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200264 return -ENOMSG;
265#endif
266 case HDIO_DRIVE_CMD:
267 if (!capable(CAP_SYS_RAWIO))
268 return -EACCES;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100269 return ide_cmd_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200270 case HDIO_DRIVE_TASK:
271 if (!capable(CAP_SYS_RAWIO))
272 return -EACCES;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100273 return ide_task_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200274 case HDIO_DRIVE_RESET:
275 if (!capable(CAP_SYS_ADMIN))
276 return -EACCES;
277 return generic_drive_reset(drive);
278 case HDIO_GET_BUSSTATE:
279 if (!capable(CAP_SYS_ADMIN))
280 return -EACCES;
281 if (put_user(BUSSTATE_ON, (long __user *)arg))
282 return -EFAULT;
283 return 0;
284 case HDIO_SET_BUSSTATE:
285 if (!capable(CAP_SYS_ADMIN))
286 return -EACCES;
287 return -EOPNOTSUPP;
288 default:
289 return -EINVAL;
290 }
291}
292EXPORT_SYMBOL(generic_ide_ioctl);