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] =
41 EXPORT_SYMBOL(scsi_command_size);
43 #define BLK_DEFAULT_TIMEOUT (60 * HZ)
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);
73 q->sg_timeout = timeout * (HZ / USER_HZ);
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);
92 if (size > (q->max_sectors << 9))
95 q->sg_reserved_size = size;
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 static int sg_io(request_queue_t *q, struct gendisk *bd_disk,
109 struct sg_io_hdr *hdr)
111 unsigned long start_time;
112 int reading, writing;
115 char sense[SCSI_SENSE_BUFFERSIZE];
116 unsigned char cmd[BLK_MAX_CDB];
118 if (hdr->interface_id != 'S')
120 if (hdr->cmd_len > BLK_MAX_CDB)
122 if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
126 * we'll do that later
128 if (hdr->iovec_count)
131 if (hdr->dxfer_len > (q->max_sectors << 9))
134 reading = writing = 0;
135 if (hdr->dxfer_len) {
136 switch (hdr->dxfer_direction) {
139 case SG_DXFER_TO_FROM_DEV:
142 case SG_DXFER_TO_DEV:
145 case SG_DXFER_FROM_DEV:
150 rq = blk_rq_map_user(q, writing ? WRITE : READ, hdr->dxferp,
156 rq = blk_get_request(q, READ, __GFP_WAIT);
159 * fill in request structure
161 rq->cmd_len = hdr->cmd_len;
162 memcpy(rq->cmd, cmd, hdr->cmd_len);
163 if (sizeof(rq->cmd) != hdr->cmd_len)
164 memset(rq->cmd + hdr->cmd_len, 0, sizeof(rq->cmd) - hdr->cmd_len);
166 memset(sense, 0, sizeof(sense));
170 rq->flags |= REQ_BLOCK_PC;
173 rq->timeout = (hdr->timeout * HZ) / 1000;
175 rq->timeout = q->sg_timeout;
177 rq->timeout = BLK_DEFAULT_TIMEOUT;
179 start_time = jiffies;
181 /* ignore return value. All information is passed back to caller
182 * (if he doesn't check that is his problem).
183 * N.B. a non-zero SCSI status is _not_ necessarily an error.
185 blk_execute_rq(q, bd_disk, rq);
187 /* write to all output members */
188 hdr->status = rq->errors;
189 hdr->masked_status = (hdr->status >> 1) & 0x1f;
191 hdr->host_status = 0;
192 hdr->driver_status = 0;
194 if (hdr->masked_status || hdr->host_status || hdr->driver_status)
195 hdr->info |= SG_INFO_CHECK;
196 hdr->resid = rq->data_len;
197 hdr->duration = ((jiffies - start_time) * 1000) / HZ;
200 if (rq->sense_len && hdr->sbp) {
201 int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
203 if (!copy_to_user(hdr->sbp, rq->sense, len))
204 hdr->sb_len_wr = len;
207 if (blk_rq_unmap_user(rq, hdr->dxferp, bio, hdr->dxfer_len))
210 /* may not have succeeded, but output values written to control
211 * structure (struct sg_io_hdr). */
215 #define FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
216 #define START_STOP_TIMEOUT (60 * HZ)
217 #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
218 #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
219 #define READ_DEFECT_DATA_TIMEOUT (60 * HZ )
220 #define OMAX_SB_LEN 16 /* For backward compatibility */
222 static int sg_scsi_ioctl(request_queue_t *q, struct gendisk *bd_disk,
223 Scsi_Ioctl_Command __user *sic)
226 int err, in_len, out_len, bytes, opcode, cmdlen;
227 char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
230 * get in an out lengths, verify they don't exceed a page worth of data
232 if (get_user(in_len, &sic->inlen))
234 if (get_user(out_len, &sic->outlen))
236 if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
238 if (get_user(opcode, sic->data))
241 bytes = max(in_len, out_len);
243 buffer = kmalloc(bytes, q->bounce_gfp | GFP_USER);
247 memset(buffer, 0, bytes);
250 rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
252 cmdlen = COMMAND_SIZE(opcode);
255 * get command and data to send to device, if any
258 rq->cmd_len = cmdlen;
259 if (copy_from_user(rq->cmd, sic->data, cmdlen))
262 if (copy_from_user(buffer, sic->data + cmdlen, in_len))
266 case SEND_DIAGNOSTIC:
268 rq->timeout = FORMAT_UNIT_TIMEOUT;
271 rq->timeout = START_STOP_TIMEOUT;
274 rq->timeout = MOVE_MEDIUM_TIMEOUT;
276 case READ_ELEMENT_STATUS:
277 rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
279 case READ_DEFECT_DATA:
280 rq->timeout = READ_DEFECT_DATA_TIMEOUT;
283 rq->timeout = BLK_DEFAULT_TIMEOUT;
287 memset(sense, 0, sizeof(sense));
292 rq->data_len = bytes;
293 rq->flags |= REQ_BLOCK_PC;
295 blk_execute_rq(q, bd_disk, rq);
296 err = rq->errors & 0xff; /* only 8 bit SCSI status */
298 if (rq->sense_len && rq->sense) {
299 bytes = (OMAX_SB_LEN > rq->sense_len) ?
300 rq->sense_len : OMAX_SB_LEN;
301 if (copy_to_user(sic->data, rq->sense, bytes))
305 if (copy_to_user(sic->data, buffer, out_len))
315 int scsi_cmd_ioctl(struct gendisk *bd_disk, unsigned int cmd, void __user *arg)
325 if (blk_get_queue(q))
332 case SG_GET_VERSION_NUM:
333 err = sg_get_version(arg);
335 case SCSI_IOCTL_GET_IDLUN:
336 err = scsi_get_idlun(q, arg);
338 case SCSI_IOCTL_GET_BUS_NUMBER:
339 err = scsi_get_bus(q, arg);
342 err = sg_set_timeout(q, arg);
345 err = sg_get_timeout(q);
347 case SG_GET_RESERVED_SIZE:
348 err = sg_get_reserved_size(q, arg);
350 case SG_SET_RESERVED_SIZE:
351 err = sg_set_reserved_size(q, arg);
353 case SG_EMULATED_HOST:
354 err = sg_emulated_host(q, arg);
357 struct sg_io_hdr hdr;
360 if (copy_from_user(&hdr, arg, sizeof(hdr)))
362 err = sg_io(q, bd_disk, &hdr);
366 if (copy_to_user(arg, &hdr, sizeof(hdr)))
370 case CDROM_SEND_PACKET: {
371 struct cdrom_generic_command cgc;
372 struct sg_io_hdr hdr;
375 if (copy_from_user(&cgc, arg, sizeof(cgc)))
377 cgc.timeout = clock_t_to_jiffies(cgc.timeout);
378 memset(&hdr, 0, sizeof(hdr));
379 hdr.interface_id = 'S';
380 hdr.cmd_len = sizeof(cgc.cmd);
381 hdr.dxfer_len = cgc.buflen;
383 switch (cgc.data_direction) {
384 case CGC_DATA_UNKNOWN:
385 hdr.dxfer_direction = SG_DXFER_UNKNOWN;
388 hdr.dxfer_direction = SG_DXFER_TO_DEV;
391 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
394 hdr.dxfer_direction = SG_DXFER_NONE;
402 hdr.dxferp = cgc.buffer;
405 hdr.mx_sb_len = sizeof(struct request_sense);
406 hdr.timeout = cgc.timeout;
407 hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
408 hdr.cmd_len = sizeof(cgc.cmd);
410 err = sg_io(q, bd_disk, &hdr);
418 cgc.buflen = hdr.resid;
419 if (copy_to_user(arg, &cgc, sizeof(cgc)))
426 * old junk scsi send command ioctl
428 case SCSI_IOCTL_SEND_COMMAND:
433 err = sg_scsi_ioctl(q, bd_disk, arg);
438 rq = blk_get_request(q, WRITE, __GFP_WAIT);
439 rq->flags |= REQ_BLOCK_PC;
442 rq->timeout = BLK_DEFAULT_TIMEOUT;
443 memset(rq->cmd, 0, sizeof(rq->cmd));
444 rq->cmd[0] = GPCMD_START_STOP_UNIT;
445 rq->cmd[4] = 0x02 + (close != 0);
447 err = blk_execute_rq(q, bd_disk, rq);
458 EXPORT_SYMBOL(scsi_cmd_ioctl);