initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / block / scsi_ioctl.c
blobf8fb5322febb705772fbc8a3636e212f5b3207c4
1 /*
2 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/string.h>
22 #include <linux/module.h>
23 #include <linux/blkdev.h>
24 #include <linux/completion.h>
25 #include <linux/cdrom.h>
26 #include <linux/slab.h>
27 #include <linux/times.h>
28 #include <asm/uaccess.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_ioctl.h>
32 #include <scsi/scsi_cmnd.h>
34 /* Command group 3 is reserved and should never be used. */
35 const unsigned char scsi_command_size[8] =
37 6, 10, 10, 12,
38 16, 12, 10, 10
41 EXPORT_SYMBOL(scsi_command_size);
43 #define BLK_DEFAULT_TIMEOUT (60 * HZ)
45 #include <scsi/sg.h>
47 static int sg_get_version(int __user *p)
49 static int sg_version_num = 30527;
50 return put_user(sg_version_num, p);
53 static int scsi_get_idlun(request_queue_t *q, int __user *p)
55 return put_user(0, p);
58 static int scsi_get_bus(request_queue_t *q, int __user *p)
60 return put_user(0, p);
63 static int sg_get_timeout(request_queue_t *q)
65 return q->sg_timeout / (HZ / USER_HZ);
68 static int sg_set_timeout(request_queue_t *q, int __user *p)
70 int timeout, err = get_user(timeout, p);
72 if (!err)
73 q->sg_timeout = timeout * (HZ / USER_HZ);
75 return err;
78 static int sg_get_reserved_size(request_queue_t *q, int __user *p)
80 return put_user(q->sg_reserved_size, p);
83 static int sg_set_reserved_size(request_queue_t *q, int __user *p)
85 int size, err = get_user(size, p);
87 if (err)
88 return err;
90 if (size < 0)
91 return -EINVAL;
92 if (size > (q->max_sectors << 9))
93 size = q->max_sectors << 9;
95 q->sg_reserved_size = size;
96 return 0;
100 * will always return that we are ATAPI even for a real SCSI drive, I'm not
101 * so sure this is worth doing anything about (why would you care??)
103 static int sg_emulated_host(request_queue_t *q, int __user *p)
105 return put_user(1, p);
108 #define CMD_READ_SAFE 0x01
109 #define CMD_WRITE_SAFE 0x02
110 #define safe_for_read(cmd) [cmd] = CMD_READ_SAFE
111 #define safe_for_write(cmd) [cmd] = CMD_WRITE_SAFE
113 static int verify_command(struct file *file, unsigned char *cmd)
115 static const unsigned char cmd_type[256] = {
117 /* Basic read-only commands */
118 safe_for_read(TEST_UNIT_READY),
119 safe_for_read(REQUEST_SENSE),
120 safe_for_read(READ_6),
121 safe_for_read(READ_10),
122 safe_for_read(READ_12),
123 safe_for_read(READ_16),
124 safe_for_read(READ_BUFFER),
125 safe_for_read(READ_LONG),
126 safe_for_read(INQUIRY),
127 safe_for_read(MODE_SENSE),
128 safe_for_read(MODE_SENSE_10),
129 safe_for_read(START_STOP),
130 safe_for_read(GPCMD_VERIFY_10),
131 safe_for_read(VERIFY_16),
132 safe_for_read(READ_BUFFER),
134 /* Audio CD commands */
135 safe_for_read(GPCMD_PLAY_CD),
136 safe_for_read(GPCMD_PLAY_AUDIO_10),
137 safe_for_read(GPCMD_PLAY_AUDIO_MSF),
138 safe_for_read(GPCMD_PLAY_AUDIO_TI),
139 safe_for_read(GPCMD_PAUSE_RESUME),
141 /* CD/DVD data reading */
142 safe_for_read(GPCMD_READ_CD),
143 safe_for_read(GPCMD_READ_CD_MSF),
144 safe_for_read(GPCMD_READ_DISC_INFO),
145 safe_for_read(GPCMD_READ_CDVD_CAPACITY),
146 safe_for_read(GPCMD_READ_DVD_STRUCTURE),
147 safe_for_read(GPCMD_READ_HEADER),
148 safe_for_read(GPCMD_READ_TRACK_RZONE_INFO),
149 safe_for_read(GPCMD_READ_SUBCHANNEL),
150 safe_for_read(GPCMD_READ_TOC_PMA_ATIP),
151 safe_for_read(GPCMD_REPORT_KEY),
152 safe_for_read(GPCMD_SCAN),
153 safe_for_read(GPCMD_GET_CONFIGURATION),
154 safe_for_read(GPCMD_READ_FORMAT_CAPACITIES),
155 safe_for_read(GPCMD_GET_EVENT_STATUS_NOTIFICATION),
156 safe_for_read(GPCMD_GET_PERFORMANCE),
157 safe_for_read(GPCMD_SEEK),
158 safe_for_read(GPCMD_STOP_PLAY_SCAN),
160 /* Basic writing commands */
161 safe_for_write(WRITE_6),
162 safe_for_write(WRITE_10),
163 safe_for_write(WRITE_VERIFY),
164 safe_for_write(WRITE_12),
165 safe_for_write(WRITE_VERIFY_12),
166 safe_for_write(WRITE_16),
167 safe_for_write(WRITE_LONG),
168 safe_for_write(ERASE),
169 safe_for_write(GPCMD_MODE_SELECT_10),
170 safe_for_write(MODE_SELECT),
171 safe_for_write(GPCMD_BLANK),
172 safe_for_write(GPCMD_CLOSE_TRACK),
173 safe_for_write(GPCMD_FLUSH_CACHE),
174 safe_for_write(GPCMD_FORMAT_UNIT),
175 safe_for_write(GPCMD_REPAIR_RZONE_TRACK),
176 safe_for_write(GPCMD_RESERVE_RZONE_TRACK),
177 safe_for_write(GPCMD_SEND_DVD_STRUCTURE),
178 safe_for_write(GPCMD_SEND_EVENT),
179 safe_for_write(GPCMD_SEND_KEY),
180 safe_for_write(GPCMD_SEND_OPC),
181 safe_for_write(GPCMD_SEND_CUE_SHEET),
182 safe_for_write(GPCMD_SET_SPEED),
183 safe_for_write(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL),
184 safe_for_write(GPCMD_LOAD_UNLOAD),
185 safe_for_write(GPCMD_SET_STREAMING),
187 unsigned char type = cmd_type[cmd[0]];
189 /* Anybody who can open the device can do a read-safe command */
190 if (type & CMD_READ_SAFE)
191 return 0;
193 /* Write-safe commands just require a writable open.. */
194 if (type & CMD_WRITE_SAFE) {
195 if (file->f_mode & FMODE_WRITE)
196 return 0;
199 /* And root can do any command.. */
200 if (capable(CAP_SYS_RAWIO))
201 return 0;
203 /* Otherwise fail it with an "Operation not permitted" */
204 return -EPERM;
207 static int sg_io(struct file *file, request_queue_t *q,
208 struct gendisk *bd_disk, struct sg_io_hdr *hdr)
210 unsigned long start_time;
211 int reading, writing;
212 struct request *rq;
213 struct bio *bio;
214 char sense[SCSI_SENSE_BUFFERSIZE];
215 unsigned char cmd[BLK_MAX_CDB];
217 if (hdr->interface_id != 'S')
218 return -EINVAL;
219 if (hdr->cmd_len > BLK_MAX_CDB)
220 return -EINVAL;
221 if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
222 return -EFAULT;
223 if (verify_command(file, cmd))
224 return -EPERM;
227 * we'll do that later
229 if (hdr->iovec_count)
230 return -EOPNOTSUPP;
232 if (hdr->dxfer_len > (q->max_sectors << 9))
233 return -EIO;
235 reading = writing = 0;
236 if (hdr->dxfer_len) {
237 switch (hdr->dxfer_direction) {
238 default:
239 return -EINVAL;
240 case SG_DXFER_TO_FROM_DEV:
241 reading = 1;
242 /* fall through */
243 case SG_DXFER_TO_DEV:
244 writing = 1;
245 break;
246 case SG_DXFER_FROM_DEV:
247 reading = 1;
248 break;
251 rq = blk_rq_map_user(q, writing ? WRITE : READ, hdr->dxferp,
252 hdr->dxfer_len);
254 if (IS_ERR(rq))
255 return PTR_ERR(rq);
256 } else
257 rq = blk_get_request(q, READ, __GFP_WAIT);
260 * fill in request structure
262 rq->cmd_len = hdr->cmd_len;
263 memcpy(rq->cmd, cmd, hdr->cmd_len);
264 if (sizeof(rq->cmd) != hdr->cmd_len)
265 memset(rq->cmd + hdr->cmd_len, 0, sizeof(rq->cmd) - hdr->cmd_len);
267 memset(sense, 0, sizeof(sense));
268 rq->sense = sense;
269 rq->sense_len = 0;
271 rq->flags |= REQ_BLOCK_PC;
272 bio = rq->bio;
275 * bounce this after holding a reference to the original bio, it's
276 * needed for proper unmapping
278 if (rq->bio)
279 blk_queue_bounce(q, &rq->bio);
281 rq->timeout = (hdr->timeout * HZ) / 1000;
282 if (!rq->timeout)
283 rq->timeout = q->sg_timeout;
284 if (!rq->timeout)
285 rq->timeout = BLK_DEFAULT_TIMEOUT;
287 start_time = jiffies;
289 /* ignore return value. All information is passed back to caller
290 * (if he doesn't check that is his problem).
291 * N.B. a non-zero SCSI status is _not_ necessarily an error.
293 blk_execute_rq(q, bd_disk, rq);
295 /* write to all output members */
296 hdr->status = rq->errors;
297 hdr->masked_status = (hdr->status >> 1) & 0x1f;
298 hdr->msg_status = 0;
299 hdr->host_status = 0;
300 hdr->driver_status = 0;
301 hdr->info = 0;
302 if (hdr->masked_status || hdr->host_status || hdr->driver_status)
303 hdr->info |= SG_INFO_CHECK;
304 hdr->resid = rq->data_len;
305 hdr->duration = ((jiffies - start_time) * 1000) / HZ;
306 hdr->sb_len_wr = 0;
308 if (rq->sense_len && hdr->sbp) {
309 int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
311 if (!copy_to_user(hdr->sbp, rq->sense, len))
312 hdr->sb_len_wr = len;
315 if (blk_rq_unmap_user(rq, bio, hdr->dxfer_len))
316 return -EFAULT;
318 /* may not have succeeded, but output values written to control
319 * structure (struct sg_io_hdr). */
320 return 0;
323 #define FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
324 #define START_STOP_TIMEOUT (60 * HZ)
325 #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
326 #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
327 #define READ_DEFECT_DATA_TIMEOUT (60 * HZ )
328 #define OMAX_SB_LEN 16 /* For backward compatibility */
330 static int sg_scsi_ioctl(struct file *file, request_queue_t *q,
331 struct gendisk *bd_disk, Scsi_Ioctl_Command __user *sic)
333 struct request *rq;
334 int err, in_len, out_len, bytes, opcode, cmdlen;
335 char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
338 * get in an out lengths, verify they don't exceed a page worth of data
340 if (get_user(in_len, &sic->inlen))
341 return -EFAULT;
342 if (get_user(out_len, &sic->outlen))
343 return -EFAULT;
344 if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
345 return -EINVAL;
346 if (get_user(opcode, sic->data))
347 return -EFAULT;
349 bytes = max(in_len, out_len);
350 if (bytes) {
351 buffer = kmalloc(bytes, q->bounce_gfp | GFP_USER);
352 if (!buffer)
353 return -ENOMEM;
355 memset(buffer, 0, bytes);
358 rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
360 cmdlen = COMMAND_SIZE(opcode);
363 * get command and data to send to device, if any
365 err = -EFAULT;
366 rq->cmd_len = cmdlen;
367 if (copy_from_user(rq->cmd, sic->data, cmdlen))
368 goto error;
370 if (copy_from_user(buffer, sic->data + cmdlen, in_len))
371 goto error;
373 err = verify_command(file, rq->cmd);
374 if (err)
375 goto error;
377 switch (opcode) {
378 case SEND_DIAGNOSTIC:
379 case FORMAT_UNIT:
380 rq->timeout = FORMAT_UNIT_TIMEOUT;
381 break;
382 case START_STOP:
383 rq->timeout = START_STOP_TIMEOUT;
384 break;
385 case MOVE_MEDIUM:
386 rq->timeout = MOVE_MEDIUM_TIMEOUT;
387 break;
388 case READ_ELEMENT_STATUS:
389 rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
390 break;
391 case READ_DEFECT_DATA:
392 rq->timeout = READ_DEFECT_DATA_TIMEOUT;
393 break;
394 default:
395 rq->timeout = BLK_DEFAULT_TIMEOUT;
396 break;
399 memset(sense, 0, sizeof(sense));
400 rq->sense = sense;
401 rq->sense_len = 0;
403 rq->data = buffer;
404 rq->data_len = bytes;
405 rq->flags |= REQ_BLOCK_PC;
407 blk_execute_rq(q, bd_disk, rq);
408 err = rq->errors & 0xff; /* only 8 bit SCSI status */
409 if (err) {
410 if (rq->sense_len && rq->sense) {
411 bytes = (OMAX_SB_LEN > rq->sense_len) ?
412 rq->sense_len : OMAX_SB_LEN;
413 if (copy_to_user(sic->data, rq->sense, bytes))
414 err = -EFAULT;
416 } else {
417 if (copy_to_user(sic->data, buffer, out_len))
418 err = -EFAULT;
421 error:
422 kfree(buffer);
423 blk_put_request(rq);
424 return err;
427 int scsi_cmd_ioctl(struct file *file, struct gendisk *bd_disk, unsigned int cmd, void __user *arg)
429 request_queue_t *q;
430 struct request *rq;
431 int close = 0, err;
433 q = bd_disk->queue;
434 if (!q)
435 return -ENXIO;
437 if (blk_get_queue(q))
438 return -ENXIO;
440 switch (cmd) {
442 * new sgv3 interface
444 case SG_GET_VERSION_NUM:
445 err = sg_get_version(arg);
446 break;
447 case SCSI_IOCTL_GET_IDLUN:
448 err = scsi_get_idlun(q, arg);
449 break;
450 case SCSI_IOCTL_GET_BUS_NUMBER:
451 err = scsi_get_bus(q, arg);
452 break;
453 case SG_SET_TIMEOUT:
454 err = sg_set_timeout(q, arg);
455 break;
456 case SG_GET_TIMEOUT:
457 err = sg_get_timeout(q);
458 break;
459 case SG_GET_RESERVED_SIZE:
460 err = sg_get_reserved_size(q, arg);
461 break;
462 case SG_SET_RESERVED_SIZE:
463 err = sg_set_reserved_size(q, arg);
464 break;
465 case SG_EMULATED_HOST:
466 err = sg_emulated_host(q, arg);
467 break;
468 case SG_IO: {
469 struct sg_io_hdr hdr;
471 err = -EFAULT;
472 if (copy_from_user(&hdr, arg, sizeof(hdr)))
473 break;
474 err = sg_io(file, q, bd_disk, &hdr);
475 if (err == -EFAULT)
476 break;
478 if (copy_to_user(arg, &hdr, sizeof(hdr)))
479 err = -EFAULT;
480 break;
482 case CDROM_SEND_PACKET: {
483 struct cdrom_generic_command cgc;
484 struct sg_io_hdr hdr;
486 err = -EFAULT;
487 if (copy_from_user(&cgc, arg, sizeof(cgc)))
488 break;
489 cgc.timeout = clock_t_to_jiffies(cgc.timeout);
490 memset(&hdr, 0, sizeof(hdr));
491 hdr.interface_id = 'S';
492 hdr.cmd_len = sizeof(cgc.cmd);
493 hdr.dxfer_len = cgc.buflen;
494 err = 0;
495 switch (cgc.data_direction) {
496 case CGC_DATA_UNKNOWN:
497 hdr.dxfer_direction = SG_DXFER_UNKNOWN;
498 break;
499 case CGC_DATA_WRITE:
500 hdr.dxfer_direction = SG_DXFER_TO_DEV;
501 break;
502 case CGC_DATA_READ:
503 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
504 break;
505 case CGC_DATA_NONE:
506 hdr.dxfer_direction = SG_DXFER_NONE;
507 break;
508 default:
509 err = -EINVAL;
511 if (err)
512 break;
514 hdr.dxferp = cgc.buffer;
515 hdr.sbp = cgc.sense;
516 if (hdr.sbp)
517 hdr.mx_sb_len = sizeof(struct request_sense);
518 hdr.timeout = cgc.timeout;
519 hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
520 hdr.cmd_len = sizeof(cgc.cmd);
522 err = sg_io(file, q, bd_disk, &hdr);
523 if (err == -EFAULT)
524 break;
526 if (hdr.status)
527 err = -EIO;
529 cgc.stat = err;
530 cgc.buflen = hdr.resid;
531 if (copy_to_user(arg, &cgc, sizeof(cgc)))
532 err = -EFAULT;
534 break;
538 * old junk scsi send command ioctl
540 case SCSI_IOCTL_SEND_COMMAND:
541 printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
542 err = -EINVAL;
543 if (!arg)
544 break;
546 err = sg_scsi_ioctl(file, q, bd_disk, arg);
547 break;
548 case CDROMCLOSETRAY:
549 close = 1;
550 case CDROMEJECT:
551 rq = blk_get_request(q, WRITE, __GFP_WAIT);
552 rq->flags |= REQ_BLOCK_PC;
553 rq->data = NULL;
554 rq->data_len = 0;
555 rq->timeout = BLK_DEFAULT_TIMEOUT;
556 memset(rq->cmd, 0, sizeof(rq->cmd));
557 rq->cmd[0] = GPCMD_START_STOP_UNIT;
558 rq->cmd[4] = 0x02 + (close != 0);
559 rq->cmd_len = 6;
560 err = blk_execute_rq(q, bd_disk, rq);
561 blk_put_request(rq);
562 break;
563 default:
564 err = -ENOTTY;
567 blk_put_queue(q);
568 return err;
571 EXPORT_SYMBOL(scsi_cmd_ioctl);