3 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 08/23/2000
4 * - get rid of some verify_areas and use __copy*user and __get/put_user
5 * for the ones that remain
7 #include <linux/module.h>
8 #include <linux/blkdev.h>
9 #include <linux/interrupt.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
14 #include <linux/string.h>
15 #include <asm/uaccess.h>
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_device.h>
19 #include <scsi/scsi_eh.h>
20 #include <scsi/scsi_host.h>
21 #include <scsi/scsi_ioctl.h>
22 #include <scsi/scsi_request.h>
24 #include <scsi/scsi_dbg.h>
26 #include "scsi_logging.h"
28 #define NORMAL_RETRIES 5
29 #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
31 #define MAX_BUF PAGE_SIZE
34 * If we are told to probe a host, we will return 0 if the host is not
35 * present, 1 if the host is present, and will return an identifying
36 * string at *arg, if arg is non null, filling to the length stored at
40 static int ioctl_probe(struct Scsi_Host
*host
, void __user
*buffer
)
42 unsigned int len
, slen
;
44 int temp
= host
->hostt
->present
;
47 if (get_user(len
, (unsigned int __user
*) buffer
))
50 if (host
->hostt
->info
)
51 string
= host
->hostt
->info(host
);
53 string
= host
->hostt
->name
;
55 slen
= strlen(string
);
58 if (copy_to_user(buffer
, string
, len
))
67 * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
68 * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
70 * dev is the SCSI device struct ptr, *(int *) arg is the length of the
71 * input data, if any, not including the command string & counts,
72 * *((int *)arg + 1) is the output buffer size in bytes.
74 * *(char *) ((int *) arg)[2] the actual command byte.
76 * Note that if more than MAX_BUF bytes are requested to be transferred,
77 * the ioctl will fail with error EINVAL.
79 * This size *does not* include the initial lengths that were passed.
81 * The SCSI command is read from the memory location immediately after the
82 * length words, and the input data is right after the command. The SCSI
83 * routines know the command size based on the opcode decode.
85 * The output area is then filled in starting from the command byte.
88 static int ioctl_internal_command(struct scsi_device
*sdev
, char *cmd
,
89 int timeout
, int retries
)
91 struct scsi_request
*sreq
;
93 struct scsi_sense_hdr sshdr
;
95 SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd
));
97 sreq
= scsi_allocate_request(sdev
, GFP_KERNEL
);
99 printk(KERN_WARNING
"SCSI internal ioctl failed, no memory\n");
103 sreq
->sr_data_direction
= DMA_NONE
;
104 scsi_wait_req(sreq
, cmd
, NULL
, 0, timeout
, retries
);
106 SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", sreq
->sr_result
));
108 if ((driver_byte(sreq
->sr_result
) & DRIVER_SENSE
) &&
109 (scsi_request_normalize_sense(sreq
, &sshdr
))) {
110 switch (sshdr
.sense_key
) {
111 case ILLEGAL_REQUEST
:
112 if (cmd
[0] == ALLOW_MEDIUM_REMOVAL
)
115 printk(KERN_INFO
"ioctl_internal_command: "
116 "ILLEGAL REQUEST asc=0x%x ascq=0x%x\n",
117 sshdr
.asc
, sshdr
.ascq
);
119 case NOT_READY
: /* This happens if there is no disc in drive */
120 if (sdev
->removable
&& (cmd
[0] != TEST_UNIT_READY
)) {
121 printk(KERN_INFO
"Device not ready. Make sure"
122 " there is a disc in the drive.\n");
126 if (sdev
->removable
) {
128 sreq
->sr_result
= 0; /* This is no longer considered an error */
131 default: /* Fall through for non-removable media */
132 printk(KERN_INFO
"ioctl_internal_command: <%d %d %d "
133 "%d> return code = %x\n",
139 scsi_print_req_sense(" ", sreq
);
144 result
= sreq
->sr_result
;
145 SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
146 scsi_release_request(sreq
);
150 int scsi_set_medium_removal(struct scsi_device
*sdev
, char state
)
152 char scsi_cmd
[MAX_COMMAND_SIZE
];
155 if (!sdev
->removable
|| !sdev
->lockable
)
158 scsi_cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
165 ret
= ioctl_internal_command(sdev
, scsi_cmd
,
166 IOCTL_NORMAL_TIMEOUT
, NORMAL_RETRIES
);
168 sdev
->locked
= (state
== SCSI_REMOVAL_PREVENT
);
171 EXPORT_SYMBOL(scsi_set_medium_removal
);
174 * This interface is deprecated - users should use the scsi generic (sg)
175 * interface instead, as this is a more flexible approach to performing
176 * generic SCSI commands on a device.
178 * The structure that we are passed should look like:
181 * unsigned int inlen; [i] Length of data to be written to device
182 * unsigned int outlen; [i] Length of data to be read from device
183 * unsigned char cmd[x]; [i] SCSI command (6 <= x <= 12).
184 * [o] Data read from device starts here.
185 * [o] On error, sense buffer starts here.
186 * unsigned char wdata[y]; [i] Data written to device starts here.
189 * - The SCSI command length is determined by examining the 1st byte
190 * of the given command. There is no way to override this.
191 * - Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
192 * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
193 * accommodate the sense buffer when an error occurs.
194 * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
195 * old code will not be surprised.
196 * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
197 * a negative return and the Unix error code in 'errno'.
198 * If the SCSI command succeeds then 0 is returned.
199 * Positive numbers returned are the compacted SCSI error codes (4
200 * bytes in one int) where the lowest byte is the SCSI status.
201 * See the drivers/scsi/scsi.h file for more information on this.
204 #define OMAX_SB_LEN 16 /* Old sense buffer length */
206 int scsi_ioctl_send_command(struct scsi_device
*sdev
,
207 struct scsi_ioctl_command __user
*sic
)
210 unsigned char cmd
[MAX_COMMAND_SIZE
];
212 struct scsi_request
*sreq
;
213 unsigned char opcode
;
214 unsigned int inlen
, outlen
, cmdlen
;
215 unsigned int needed
, buf_needed
;
216 int timeout
, retries
, result
;
217 int data_direction
, gfp_mask
= GFP_KERNEL
;
222 if (sdev
->host
->unchecked_isa_dma
)
226 * Verify that we can read at least this much.
228 if (!access_ok(VERIFY_READ
, sic
, sizeof(Scsi_Ioctl_Command
)))
231 if(__get_user(inlen
, &sic
->inlen
))
234 if(__get_user(outlen
, &sic
->outlen
))
238 * We do not transfer more than MAX_BUF with this interface.
239 * If the user needs to transfer more data than this, they
240 * should use scsi_generics (sg) instead.
244 if (outlen
> MAX_BUF
)
248 if(get_user(opcode
, cmd_in
))
251 needed
= buf_needed
= (inlen
> outlen
? inlen
: outlen
);
253 buf_needed
= (buf_needed
+ 511) & ~511;
254 if (buf_needed
> MAX_BUF
)
255 buf_needed
= MAX_BUF
;
256 buf
= kmalloc(buf_needed
, gfp_mask
);
259 memset(buf
, 0, buf_needed
);
261 data_direction
= DMA_FROM_DEVICE
;
262 } else if (outlen
== 0 ) {
263 data_direction
= DMA_TO_DEVICE
;
266 * Can this ever happen?
268 data_direction
= DMA_BIDIRECTIONAL
;
273 data_direction
= DMA_NONE
;
277 * Obtain the command from the user's address space.
279 cmdlen
= COMMAND_SIZE(opcode
);
283 if (!access_ok(VERIFY_READ
, cmd_in
, cmdlen
+ inlen
))
286 if(__copy_from_user(cmd
, cmd_in
, cmdlen
))
290 * Obtain the data to be sent to the device (if any).
293 if(copy_from_user(buf
, cmd_in
+ cmdlen
, inlen
))
297 case SEND_DIAGNOSTIC
:
299 timeout
= FORMAT_UNIT_TIMEOUT
;
303 timeout
= START_STOP_TIMEOUT
;
304 retries
= NORMAL_RETRIES
;
307 timeout
= MOVE_MEDIUM_TIMEOUT
;
308 retries
= NORMAL_RETRIES
;
310 case READ_ELEMENT_STATUS
:
311 timeout
= READ_ELEMENT_STATUS_TIMEOUT
;
312 retries
= NORMAL_RETRIES
;
314 case READ_DEFECT_DATA
:
315 timeout
= READ_DEFECT_DATA_TIMEOUT
;
319 timeout
= IOCTL_NORMAL_TIMEOUT
;
320 retries
= NORMAL_RETRIES
;
324 sreq
= scsi_allocate_request(sdev
, GFP_KERNEL
);
330 sreq
->sr_data_direction
= data_direction
;
331 scsi_wait_req(sreq
, cmd
, buf
, needed
, timeout
, retries
);
334 * If there was an error condition, pass the info back to the user.
336 result
= sreq
->sr_result
;
338 int sb_len
= sizeof(sreq
->sr_sense_buffer
);
340 sb_len
= (sb_len
> OMAX_SB_LEN
) ? OMAX_SB_LEN
: sb_len
;
341 if (copy_to_user(cmd_in
, sreq
->sr_sense_buffer
, sb_len
))
344 if (copy_to_user(cmd_in
, buf
, outlen
))
348 scsi_release_request(sreq
);
353 EXPORT_SYMBOL(scsi_ioctl_send_command
);
356 * The scsi_ioctl_get_pci() function places into arg the value
357 * pci_dev::slot_name (8 characters) for the PCI device (if any).
358 * Returns: 0 on success
359 * -ENXIO if there isn't a PCI device pointer
360 * (could be because the SCSI driver hasn't been
361 * updated yet, or because it isn't a SCSI
363 * any copy_to_user() error on failure there
365 static int scsi_ioctl_get_pci(struct scsi_device
*sdev
, void __user
*arg
)
367 struct device
*dev
= scsi_get_device(sdev
->host
);
371 return copy_to_user(arg
, dev
->bus_id
, sizeof(dev
->bus_id
))? -EFAULT
: 0;
376 * the scsi_ioctl() function differs from most ioctls in that it does
377 * not take a major/minor number as the dev field. Rather, it takes
378 * a pointer to a scsi_devices[] element, a structure.
380 int scsi_ioctl(struct scsi_device
*sdev
, int cmd
, void __user
*arg
)
382 char scsi_cmd
[MAX_COMMAND_SIZE
];
384 /* No idea how this happens.... */
389 * If we are in the middle of error recovery, don't let anyone
390 * else try and use this device. Also, if error recovery fails, it
391 * may try and take the device offline, in which case all further
392 * access to the device is prohibited.
394 if (!scsi_block_when_processing_errors(sdev
))
397 /* Check for deprecated ioctls ... all the ioctls which don't
398 * follow the new unique numbering scheme are deprecated */
400 case SCSI_IOCTL_SEND_COMMAND
:
401 case SCSI_IOCTL_TEST_UNIT_READY
:
402 case SCSI_IOCTL_BENCHMARK_COMMAND
:
403 case SCSI_IOCTL_SYNC
:
404 case SCSI_IOCTL_START_UNIT
:
405 case SCSI_IOCTL_STOP_UNIT
:
406 printk(KERN_WARNING
"program %s is using a deprecated SCSI "
407 "ioctl, please convert it to SG_IO\n", current
->comm
);
414 case SCSI_IOCTL_GET_IDLUN
:
415 if (!access_ok(VERIFY_WRITE
, arg
, sizeof(struct scsi_idlun
)))
418 __put_user((sdev
->id
& 0xff)
419 + ((sdev
->lun
& 0xff) << 8)
420 + ((sdev
->channel
& 0xff) << 16)
421 + ((sdev
->host
->host_no
& 0xff) << 24),
422 &((struct scsi_idlun __user
*)arg
)->dev_id
);
423 __put_user(sdev
->host
->unique_id
,
424 &((struct scsi_idlun __user
*)arg
)->host_unique_id
);
426 case SCSI_IOCTL_GET_BUS_NUMBER
:
427 return put_user(sdev
->host
->host_no
, (int __user
*)arg
);
428 case SCSI_IOCTL_PROBE_HOST
:
429 return ioctl_probe(sdev
->host
, arg
);
430 case SCSI_IOCTL_SEND_COMMAND
:
431 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
433 return scsi_ioctl_send_command(sdev
, arg
);
434 case SCSI_IOCTL_DOORLOCK
:
435 return scsi_set_medium_removal(sdev
, SCSI_REMOVAL_PREVENT
);
436 case SCSI_IOCTL_DOORUNLOCK
:
437 return scsi_set_medium_removal(sdev
, SCSI_REMOVAL_ALLOW
);
438 case SCSI_IOCTL_TEST_UNIT_READY
:
439 return scsi_test_unit_ready(sdev
, IOCTL_NORMAL_TIMEOUT
,
441 case SCSI_IOCTL_START_UNIT
:
442 scsi_cmd
[0] = START_STOP
;
444 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
446 return ioctl_internal_command(sdev
, scsi_cmd
,
447 START_STOP_TIMEOUT
, NORMAL_RETRIES
);
448 case SCSI_IOCTL_STOP_UNIT
:
449 scsi_cmd
[0] = START_STOP
;
451 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
453 return ioctl_internal_command(sdev
, scsi_cmd
,
454 START_STOP_TIMEOUT
, NORMAL_RETRIES
);
455 case SCSI_IOCTL_GET_PCI
:
456 return scsi_ioctl_get_pci(sdev
, arg
);
458 if (sdev
->host
->hostt
->ioctl
)
459 return sdev
->host
->hostt
->ioctl(sdev
, cmd
, arg
);
463 EXPORT_SYMBOL(scsi_ioctl
);
466 * the scsi_nonblock_ioctl() function is designed for ioctls which may
467 * be executed even if the device is in recovery.
469 int scsi_nonblockable_ioctl(struct scsi_device
*sdev
, int cmd
,
470 void __user
*arg
, struct file
*filp
)
474 /* The first set of iocts may be executed even if we're doing
475 * error processing, as long as the device was opened
477 if (filp
&& filp
->f_flags
& O_NONBLOCK
) {
478 if (test_bit(SHOST_RECOVERY
,
479 &sdev
->host
->shost_state
))
481 } else if (!scsi_block_when_processing_errors(sdev
))
486 result
= get_user(val
, (int __user
*)arg
);
489 if (val
== SG_SCSI_RESET_NOTHING
)
492 case SG_SCSI_RESET_DEVICE
:
493 val
= SCSI_TRY_RESET_DEVICE
;
495 case SG_SCSI_RESET_BUS
:
496 val
= SCSI_TRY_RESET_BUS
;
498 case SG_SCSI_RESET_HOST
:
499 val
= SCSI_TRY_RESET_HOST
;
504 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
506 return (scsi_reset_provider(sdev
, val
) ==
511 EXPORT_SYMBOL(scsi_nonblockable_ioctl
);