blob: 82f252c3ee6e500eb85fc466f1fc26e5c71b6d5a [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>
7
8static const struct ide_ioctl_devset ide_ioctl_settings[] = {
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02009{ 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 Zolnierkiewicz05236ea2008-10-10 22:39:33 +020014{ 0 }
15};
16
17int 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 Oltmanns92f1f8f2008-10-10 22:39:40 +020021 const struct ide_devset *ds;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020022 int err = -EOPNOTSUPP;
23
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020024 for (; (ds = s->setting); s++) {
25 if (ds->get && s->get_ioctl == cmd)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020026 goto read_val;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020027 else if (ds->set && s->set_ioctl == cmd)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020028 goto set_val;
29 }
30
31 return err;
32
33read_val:
34 mutex_lock(&ide_setting_mtx);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +020035 err = ds->get(drive);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020036 mutex_unlock(&ide_setting_mtx);
37 return err >= 0 ? put_user(err, (long __user *)arg) : err;
38
39set_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 Oltmanns92f1f8f2008-10-10 22:39:40 +020047 err = ide_devset_execute(drive, ds, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020048 mutex_unlock(&ide_setting_mtx);
49 }
50 }
51 return err;
52}
53EXPORT_SYMBOL_GPL(ide_setting_ioctl);
54
55static 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 Zolnierkiewicz97100fc2008-10-13 21:39:36 +020062 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020063 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);
80out:
81 return rc;
82}
83
84static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
85{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020086 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 Zolnierkiewicz05236ea2008-10-10 22:39:33 +020090}
91
92static 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 Petkov53174642009-01-02 16:12:54 +010098 (drive->media != ide_tape))
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +020099 return -EPERM;
100
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200101 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 Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200110
111 return 0;
112}
113
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100114static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200115{
116 u8 *buf = NULL;
117 int bufsize = 0, err = 0;
118 u8 args[4], xfer_rate = 0;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100119 struct ide_cmd cmd;
120 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200121
122 if (NULL == (void *) arg) {
123 struct request *rq;
124
125 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
126 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
127 err = blk_execute_rq(drive->queue, NULL, rq, 0);
128 blk_put_request(rq);
129
130 return err;
131 }
132
133 if (copy_from_user(args, (void __user *)arg, 4))
134 return -EFAULT;
135
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100136 memset(&cmd, 0, sizeof(cmd));
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200137 tf->feature = args[2];
138 if (args[0] == ATA_CMD_SMART) {
139 tf->nsect = args[3];
140 tf->lbal = args[1];
141 tf->lbam = 0x4f;
142 tf->lbah = 0xc2;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200143 cmd.valid.out.tf = IDE_VALID_OUT_TF;
144 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200145 } else {
146 tf->nsect = args[1];
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200147 cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
148 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200149 }
150 tf->command = args[0];
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100151 cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200152
153 if (args[3]) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100154 cmd.tf_flags |= IDE_TFLAG_IO_16BIT;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200155 bufsize = SECTOR_SIZE * args[3];
156 buf = kzalloc(bufsize, GFP_KERNEL);
157 if (buf == NULL)
158 return -ENOMEM;
159 }
160
161 if (tf->command == ATA_CMD_SET_FEATURES &&
162 tf->feature == SETFEATURES_XFER &&
Bartlomiej Zolnierkiewicza9c41502009-06-15 22:13:45 +0200163 tf->nsect >= XFER_SW_DMA_0) {
164 xfer_rate = ide_find_dma_mode(drive, XFER_UDMA_6);
165 if (xfer_rate != tf->nsect) {
166 err = -EINVAL;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200167 goto abort;
168 }
169 }
170
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100171 err = ide_raw_taskfile(drive, &cmd, buf, args[3]);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200172
173 args[0] = tf->status;
174 args[1] = tf->error;
175 args[2] = tf->nsect;
176
177 if (!err && xfer_rate) {
178 /* active-retuning-calls future */
179 ide_set_xfer_rate(drive, xfer_rate);
180 ide_driveid_update(drive);
181 }
182abort:
183 if (copy_to_user((void __user *)arg, &args, 4))
184 err = -EFAULT;
185 if (buf) {
186 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
187 err = -EFAULT;
188 kfree(buf);
189 }
190 return err;
191}
192
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100193static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg)
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200194{
195 void __user *p = (void __user *)arg;
196 int err = 0;
197 u8 args[7];
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100198 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200199
200 if (copy_from_user(args, p, 7))
201 return -EFAULT;
202
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100203 memset(&cmd, 0, sizeof(cmd));
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200204 memcpy(&cmd.tf.feature, &args[1], 6);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100205 cmd.tf.command = args[0];
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200206 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
207 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200208
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100209 err = ide_no_data_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200210
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100211 args[0] = cmd.tf.command;
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200212 memcpy(&args[1], &cmd.tf.feature, 6);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200213
214 if (copy_to_user(p, args, 7))
215 err = -EFAULT;
216
217 return err;
218}
219
220static int generic_drive_reset(ide_drive_t *drive)
221{
222 struct request *rq;
223 int ret = 0;
224
225 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
226 rq->cmd_type = REQ_TYPE_SPECIAL;
227 rq->cmd_len = 1;
228 rq->cmd[0] = REQ_DRIVE_RESET;
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200229 if (blk_execute_rq(drive->queue, NULL, rq, 1))
230 ret = rq->errors;
231 blk_put_request(rq);
232 return ret;
233}
234
Al Viro1bddd9e2008-09-02 17:19:43 -0400235int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200236 unsigned int cmd, unsigned long arg)
237{
238 int err;
239
240 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
241 if (err != -EOPNOTSUPP)
242 return err;
243
244 switch (cmd) {
245 case HDIO_OBSOLETE_IDENTITY:
246 case HDIO_GET_IDENTITY:
247 if (bdev != bdev->bd_contains)
248 return -EINVAL;
249 return ide_get_identity_ioctl(drive, cmd, arg);
250 case HDIO_GET_NICE:
251 return ide_get_nice_ioctl(drive, arg);
252 case HDIO_SET_NICE:
253 if (!capable(CAP_SYS_ADMIN))
254 return -EACCES;
255 return ide_set_nice_ioctl(drive, arg);
256#ifdef CONFIG_IDE_TASK_IOCTL
257 case HDIO_DRIVE_TASKFILE:
258 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
259 return -EACCES;
260 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100261 return ide_taskfile_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200262 return -ENOMSG;
263#endif
264 case HDIO_DRIVE_CMD:
265 if (!capable(CAP_SYS_RAWIO))
266 return -EACCES;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100267 return ide_cmd_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200268 case HDIO_DRIVE_TASK:
269 if (!capable(CAP_SYS_RAWIO))
270 return -EACCES;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100271 return ide_task_ioctl(drive, arg);
Bartlomiej Zolnierkiewicz05236ea2008-10-10 22:39:33 +0200272 case HDIO_DRIVE_RESET:
273 if (!capable(CAP_SYS_ADMIN))
274 return -EACCES;
275 return generic_drive_reset(drive);
276 case HDIO_GET_BUSSTATE:
277 if (!capable(CAP_SYS_ADMIN))
278 return -EACCES;
279 if (put_user(BUSSTATE_ON, (long __user *)arg))
280 return -EFAULT;
281 return 0;
282 case HDIO_SET_BUSSTATE:
283 if (!capable(CAP_SYS_ADMIN))
284 return -EACCES;
285 return -EOPNOTSUPP;
286 default:
287 return -EINVAL;
288 }
289}
290EXPORT_SYMBOL(generic_ide_ioctl);