blob: 9965ecd5078c63e37aa4fb34d1b7df722fd22d63 [file] [log] [blame]
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +02001/*
2 * IDE ioctls handling.
3 */
4
5#include <linux/hdreg.h>
6#include <linux/ide.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +02008
9static const struct ide_ioctl_devset ide_ioctl_settings[] = {
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020010{ HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit },
11{ HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings },
12{ HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq },
13{ HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma },
14{ -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode },
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020015{ 0 }
16};
17
18int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev,
19 unsigned int cmd, unsigned long arg,
20 const struct ide_ioctl_devset *s)
21{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020022 const struct ide_devset *ds;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020023 int err = -EOPNOTSUPP;
24
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020025 for (; (ds = s->setting); s++) {
26 if (ds->get && s->get_ioctl == cmd)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020027 goto read_val;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020028 else if (ds->set && s->set_ioctl == cmd)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020029 goto set_val;
30 }
31
32 return err;
33
34read_val:
35 mutex_lock(&ide_setting_mtx);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020036 err = ds->get(drive);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020037 mutex_unlock(&ide_setting_mtx);
38 return err >= 0 ? put_user(err, (long __user *)arg) : err;
39
40set_val:
41 if (bdev != bdev->bd_contains)
42 err = -EINVAL;
43 else {
44 if (!capable(CAP_SYS_ADMIN))
45 err = -EACCES;
46 else {
47 mutex_lock(&ide_setting_mtx);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020048 err = ide_devset_execute(drive, ds, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020049 mutex_unlock(&ide_setting_mtx);
50 }
51 }
52 return err;
53}
54EXPORT_SYMBOL_GPL(ide_setting_ioctl);
55
56static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
57 unsigned long arg)
58{
59 u16 *id = NULL;
60 int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
61 int rc = 0;
62
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020063 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020064 rc = -ENOMSG;
65 goto out;
66 }
67
Christian Engelmayere18ed142009-06-29 19:31:41 -070068 /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
69 id = kmalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020070 if (id == NULL) {
71 rc = -ENOMEM;
72 goto out;
73 }
74
75 memcpy(id, drive->id, size);
76 ata_id_to_hd_driveid(id);
77
78 if (copy_to_user((void __user *)arg, id, size))
79 rc = -EFAULT;
80
81 kfree(id);
82out:
83 return rc;
84}
85
86static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
87{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020088 return put_user((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
89 << IDE_NICE_DSC_OVERLAP) |
90 (!!(drive->dev_flags & IDE_DFLAG_NICE1)
91 << IDE_NICE_1), (long __user *)arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020092}
93
94static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
95{
96 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
97 return -EPERM;
98
99 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
Borislav Petkov53174642009-01-02 16:12:54 +0100100 (drive->media != ide_tape))
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200101 return -EPERM;
102
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200103 if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
104 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
105 else
106 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
107
108 if ((arg >> IDE_NICE_1) & 1)
109 drive->dev_flags |= IDE_DFLAG_NICE1;
110 else
111 drive->dev_flags &= ~IDE_DFLAG_NICE1;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200112
113 return 0;
114}
115
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100116static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200117{
118 u8 *buf = NULL;
119 int bufsize = 0, err = 0;
120 u8 args[4], xfer_rate = 0;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100121 struct ide_cmd cmd;
122 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200123
124 if (NULL == (void *) arg) {
125 struct request *rq;
126
127 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
128 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
129 err = blk_execute_rq(drive->queue, NULL, rq, 0);
130 blk_put_request(rq);
131
132 return err;
133 }
134
135 if (copy_from_user(args, (void __user *)arg, 4))
136 return -EFAULT;
137
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100138 memset(&cmd, 0, sizeof(cmd));
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200139 tf->feature = args[2];
140 if (args[0] == ATA_CMD_SMART) {
141 tf->nsect = args[3];
142 tf->lbal = args[1];
143 tf->lbam = 0x4f;
144 tf->lbah = 0xc2;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200145 cmd.valid.out.tf = IDE_VALID_OUT_TF;
146 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200147 } else {
148 tf->nsect = args[1];
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200149 cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
150 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200151 }
152 tf->command = args[0];
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100153 cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200154
155 if (args[3]) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100156 cmd.tf_flags |= IDE_TFLAG_IO_16BIT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200157 bufsize = SECTOR_SIZE * args[3];
158 buf = kzalloc(bufsize, GFP_KERNEL);
159 if (buf == NULL)
160 return -ENOMEM;
161 }
162
163 if (tf->command == ATA_CMD_SET_FEATURES &&
164 tf->feature == SETFEATURES_XFER &&
Bartlomiej Zolnierkiewicza9c41502009-06-15 22:13:45 +0200165 tf->nsect >= XFER_SW_DMA_0) {
Hemant Pedanekar28c19692009-11-25 15:04:54 -0800166 xfer_rate = ide_find_dma_mode(drive, tf->nsect);
Bartlomiej Zolnierkiewicza9c41502009-06-15 22:13:45 +0200167 if (xfer_rate != tf->nsect) {
168 err = -EINVAL;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200169 goto abort;
170 }
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000171
172 cmd.tf_flags |= IDE_TFLAG_SET_XFER;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200173 }
174
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100175 err = ide_raw_taskfile(drive, &cmd, buf, args[3]);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200176
177 args[0] = tf->status;
178 args[1] = tf->error;
179 args[2] = tf->nsect;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200180abort:
181 if (copy_to_user((void __user *)arg, &args, 4))
182 err = -EFAULT;
183 if (buf) {
184 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
185 err = -EFAULT;
186 kfree(buf);
187 }
188 return err;
189}
190
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100191static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200192{
193 void __user *p = (void __user *)arg;
194 int err = 0;
195 u8 args[7];
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100196 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200197
198 if (copy_from_user(args, p, 7))
199 return -EFAULT;
200
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100201 memset(&cmd, 0, sizeof(cmd));
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200202 memcpy(&cmd.tf.feature, &args[1], 6);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100203 cmd.tf.command = args[0];
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200204 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
205 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200206
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100207 err = ide_no_data_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200208
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100209 args[0] = cmd.tf.command;
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200210 memcpy(&args[1], &cmd.tf.feature, 6);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200211
212 if (copy_to_user(p, args, 7))
213 err = -EFAULT;
214
215 return err;
216}
217
218static int generic_drive_reset(ide_drive_t *drive)
219{
220 struct request *rq;
221 int ret = 0;
222
223 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
224 rq->cmd_type = REQ_TYPE_SPECIAL;
225 rq->cmd_len = 1;
226 rq->cmd[0] = REQ_DRIVE_RESET;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200227 if (blk_execute_rq(drive->queue, NULL, rq, 1))
228 ret = rq->errors;
229 blk_put_request(rq);
230 return ret;
231}
232
Al Viro1bddd9e2008-09-02 17:19:43 -0400233int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200234 unsigned int cmd, unsigned long arg)
235{
236 int err;
237
238 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
239 if (err != -EOPNOTSUPP)
240 return err;
241
242 switch (cmd) {
243 case HDIO_OBSOLETE_IDENTITY:
244 case HDIO_GET_IDENTITY:
245 if (bdev != bdev->bd_contains)
246 return -EINVAL;
247 return ide_get_identity_ioctl(drive, cmd, arg);
248 case HDIO_GET_NICE:
249 return ide_get_nice_ioctl(drive, arg);
250 case HDIO_SET_NICE:
251 if (!capable(CAP_SYS_ADMIN))
252 return -EACCES;
253 return ide_set_nice_ioctl(drive, arg);
254#ifdef CONFIG_IDE_TASK_IOCTL
255 case HDIO_DRIVE_TASKFILE:
256 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
257 return -EACCES;
258 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100259 return ide_taskfile_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200260 return -ENOMSG;
261#endif
262 case HDIO_DRIVE_CMD:
263 if (!capable(CAP_SYS_RAWIO))
264 return -EACCES;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100265 return ide_cmd_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200266 case HDIO_DRIVE_TASK:
267 if (!capable(CAP_SYS_RAWIO))
268 return -EACCES;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100269 return ide_task_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200270 case HDIO_DRIVE_RESET:
271 if (!capable(CAP_SYS_ADMIN))
272 return -EACCES;
273 return generic_drive_reset(drive);
274 case HDIO_GET_BUSSTATE:
275 if (!capable(CAP_SYS_ADMIN))
276 return -EACCES;
277 if (put_user(BUSSTATE_ON, (long __user *)arg))
278 return -EFAULT;
279 return 0;
280 case HDIO_SET_BUSSTATE:
281 if (!capable(CAP_SYS_ADMIN))
282 return -EACCES;
283 return -EOPNOTSUPP;
284 default:
285 return -EINVAL;
286 }
287}
288EXPORT_SYMBOL(generic_ide_ioctl);