Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
05236ea6 BZ |
2 | /* |
3 | * IDE ioctls handling. | |
4 | */ | |
5 | ||
38789fda | 6 | #include <linux/export.h> |
05236ea6 BZ |
7 | #include <linux/hdreg.h> |
8 | #include <linux/ide.h> | |
5a0e3ad6 | 9 | #include <linux/slab.h> |
05236ea6 BZ |
10 | |
11 | static const struct ide_ioctl_devset ide_ioctl_settings[] = { | |
92f1f8fd EO |
12 | { 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 }, | |
05236ea6 BZ |
17 | { 0 } |
18 | }; | |
19 | ||
20 | int 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 | { | |
92f1f8fd | 24 | const struct ide_devset *ds; |
05236ea6 BZ |
25 | int err = -EOPNOTSUPP; |
26 | ||
92f1f8fd EO |
27 | for (; (ds = s->setting); s++) { |
28 | if (ds->get && s->get_ioctl == cmd) | |
05236ea6 | 29 | goto read_val; |
92f1f8fd | 30 | else if (ds->set && s->set_ioctl == cmd) |
05236ea6 BZ |
31 | goto set_val; |
32 | } | |
33 | ||
34 | return err; | |
35 | ||
36 | read_val: | |
37 | mutex_lock(&ide_setting_mtx); | |
92f1f8fd | 38 | err = ds->get(drive); |
05236ea6 BZ |
39 | mutex_unlock(&ide_setting_mtx); |
40 | return err >= 0 ? put_user(err, (long __user *)arg) : err; | |
41 | ||
42 | set_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); | |
92f1f8fd | 50 | err = ide_devset_execute(drive, ds, arg); |
05236ea6 BZ |
51 | mutex_unlock(&ide_setting_mtx); |
52 | } | |
53 | } | |
54 | return err; | |
55 | } | |
56 | EXPORT_SYMBOL_GPL(ide_setting_ioctl); | |
57 | ||
58 | static 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 | ||
97100fc8 | 65 | if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) { |
05236ea6 BZ |
66 | rc = -ENOMSG; |
67 | goto out; | |
68 | } | |
69 | ||
e18ed145 CE |
70 | /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */ |
71 | id = kmalloc(ATA_ID_WORDS * 2, GFP_KERNEL); | |
05236ea6 BZ |
72 | 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); | |
84 | out: | |
85 | return rc; | |
86 | } | |
87 | ||
88 | static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg) | |
89 | { | |
97100fc8 BZ |
90 | 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); | |
05236ea6 BZ |
94 | } |
95 | ||
96 | static 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) && | |
5317464d | 102 | (drive->media != ide_tape)) |
05236ea6 BZ |
103 | return -EPERM; |
104 | ||
97100fc8 BZ |
105 | 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; | |
05236ea6 BZ |
114 | |
115 | return 0; | |
116 | } | |
117 | ||
22aa4b32 | 118 | static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg) |
05236ea6 BZ |
119 | { |
120 | u8 *buf = NULL; | |
121 | int bufsize = 0, err = 0; | |
122 | u8 args[4], xfer_rate = 0; | |
22aa4b32 BZ |
123 | struct ide_cmd cmd; |
124 | struct ide_taskfile *tf = &cmd.tf; | |
05236ea6 BZ |
125 | |
126 | if (NULL == (void *) arg) { | |
127 | struct request *rq; | |
128 | ||
ff005a06 | 129 | rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0); |
2f5a8e80 | 130 | ide_req(rq)->type = ATA_PRIV_TASKFILE; |
b7819b92 | 131 | blk_execute_rq(drive->queue, NULL, rq, 0); |
17d5363b | 132 | err = scsi_req(rq)->result ? -EIO : 0; |
05236ea6 BZ |
133 | blk_put_request(rq); |
134 | ||
135 | return err; | |
136 | } | |
137 | ||
138 | if (copy_from_user(args, (void __user *)arg, 4)) | |
139 | return -EFAULT; | |
140 | ||
22aa4b32 | 141 | memset(&cmd, 0, sizeof(cmd)); |
05236ea6 BZ |
142 | tf->feature = args[2]; |
143 | if (args[0] == ATA_CMD_SMART) { | |
144 | tf->nsect = args[3]; | |
145 | tf->lbal = args[1]; | |
6fcf0497 XY |
146 | tf->lbam = ATA_SMART_LBAM_PASS; |
147 | tf->lbah = ATA_SMART_LBAH_PASS; | |
60f85019 SS |
148 | cmd.valid.out.tf = IDE_VALID_OUT_TF; |
149 | cmd.valid.in.tf = IDE_VALID_NSECT; | |
05236ea6 BZ |
150 | } else { |
151 | tf->nsect = args[1]; | |
60f85019 SS |
152 | cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT; |
153 | cmd.valid.in.tf = IDE_VALID_NSECT; | |
05236ea6 BZ |
154 | } |
155 | tf->command = args[0]; | |
0dfb991c | 156 | cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA; |
05236ea6 BZ |
157 | |
158 | if (args[3]) { | |
22aa4b32 | 159 | cmd.tf_flags |= IDE_TFLAG_IO_16BIT; |
05236ea6 BZ |
160 | 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 && | |
a9c41509 | 168 | tf->nsect >= XFER_SW_DMA_0) { |
28c1969f | 169 | xfer_rate = ide_find_dma_mode(drive, tf->nsect); |
a9c41509 BZ |
170 | if (xfer_rate != tf->nsect) { |
171 | err = -EINVAL; | |
05236ea6 BZ |
172 | goto abort; |
173 | } | |
665d66e8 BZ |
174 | |
175 | cmd.tf_flags |= IDE_TFLAG_SET_XFER; | |
05236ea6 BZ |
176 | } |
177 | ||
22aa4b32 | 178 | err = ide_raw_taskfile(drive, &cmd, buf, args[3]); |
05236ea6 BZ |
179 | |
180 | args[0] = tf->status; | |
181 | args[1] = tf->error; | |
182 | args[2] = tf->nsect; | |
05236ea6 BZ |
183 | abort: |
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 | ||
22aa4b32 | 194 | static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg) |
05236ea6 BZ |
195 | { |
196 | void __user *p = (void __user *)arg; | |
197 | int err = 0; | |
198 | u8 args[7]; | |
22aa4b32 | 199 | struct ide_cmd cmd; |
05236ea6 BZ |
200 | |
201 | if (copy_from_user(args, p, 7)) | |
202 | return -EFAULT; | |
203 | ||
22aa4b32 | 204 | memset(&cmd, 0, sizeof(cmd)); |
745483f1 | 205 | memcpy(&cmd.tf.feature, &args[1], 6); |
22aa4b32 | 206 | cmd.tf.command = args[0]; |
60f85019 SS |
207 | cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE; |
208 | cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE; | |
05236ea6 | 209 | |
22aa4b32 | 210 | err = ide_no_data_taskfile(drive, &cmd); |
05236ea6 | 211 | |
22aa4b32 | 212 | args[0] = cmd.tf.command; |
745483f1 | 213 | memcpy(&args[1], &cmd.tf.feature, 6); |
05236ea6 BZ |
214 | |
215 | if (copy_to_user(p, args, 7)) | |
216 | err = -EFAULT; | |
217 | ||
218 | return err; | |
219 | } | |
220 | ||
221 | static int generic_drive_reset(ide_drive_t *drive) | |
222 | { | |
223 | struct request *rq; | |
224 | int ret = 0; | |
225 | ||
ff005a06 | 226 | rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0); |
2f5a8e80 | 227 | ide_req(rq)->type = ATA_PRIV_MISC; |
82ed4db4 CH |
228 | scsi_req(rq)->cmd_len = 1; |
229 | scsi_req(rq)->cmd[0] = REQ_DRIVE_RESET; | |
b7819b92 | 230 | blk_execute_rq(drive->queue, NULL, rq, 1); |
17d5363b | 231 | ret = scsi_req(rq)->result; |
05236ea6 BZ |
232 | blk_put_request(rq); |
233 | return ret; | |
234 | } | |
235 | ||
1bddd9e6 | 236 | int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev, |
05236ea6 BZ |
237 | 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) | |
22aa4b32 | 262 | return ide_taskfile_ioctl(drive, arg); |
05236ea6 BZ |
263 | return -ENOMSG; |
264 | #endif | |
265 | case HDIO_DRIVE_CMD: | |
266 | if (!capable(CAP_SYS_RAWIO)) | |
267 | return -EACCES; | |
22aa4b32 | 268 | return ide_cmd_ioctl(drive, arg); |
05236ea6 BZ |
269 | case HDIO_DRIVE_TASK: |
270 | if (!capable(CAP_SYS_RAWIO)) | |
271 | return -EACCES; | |
22aa4b32 | 272 | return ide_task_ioctl(drive, arg); |
05236ea6 BZ |
273 | 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 | } | |
291 | EXPORT_SYMBOL(generic_ide_ioctl); |