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>
10 #include <asm/uaccess.h>
11 #include <asm/system.h>
14 #include <linux/interrupt.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
19 #include <linux/string.h>
21 #include <linux/blk.h>
24 #include <scsi/scsi_ioctl.h>
26 #include "scsi_logging.h"
28 #define NORMAL_RETRIES 5
29 #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
30 #define FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
31 #define START_STOP_TIMEOUT (60 * HZ)
32 #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
33 #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
34 #define READ_DEFECT_DATA_TIMEOUT (60 * HZ ) /* ZIP-250 on parallel port takes as long! */
36 #define MAX_BUF PAGE_SIZE
39 * If we are told to probe a host, we will return 0 if the host is not
40 * present, 1 if the host is present, and will return an identifying
41 * string at *arg, if arg is non null, filling to the length stored at
45 static int ioctl_probe(struct Scsi_Host
*host
, void *buffer
)
47 unsigned int len
, slen
;
49 int temp
= host
->hostt
->present
;
52 if (get_user(len
, (unsigned int *) buffer
))
55 if (host
->hostt
->info
)
56 string
= host
->hostt
->info(host
);
58 string
= host
->hostt
->name
;
60 slen
= strlen(string
);
63 if (copy_to_user(buffer
, string
, len
))
72 * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
73 * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
75 * dev is the SCSI device struct ptr, *(int *) arg is the length of the
76 * input data, if any, not including the command string & counts,
77 * *((int *)arg + 1) is the output buffer size in bytes.
79 * *(char *) ((int *) arg)[2] the actual command byte.
81 * Note that if more than MAX_BUF bytes are requested to be transferred,
82 * the ioctl will fail with error EINVAL.
84 * This size *does not* include the initial lengths that were passed.
86 * The SCSI command is read from the memory location immediately after the
87 * length words, and the input data is right after the command. The SCSI
88 * routines know the command size based on the opcode decode.
90 * The output area is then filled in starting from the command byte.
93 static int ioctl_internal_command(struct scsi_device
*sdev
, char *cmd
,
94 int timeout
, int retries
)
96 struct scsi_request
*sreq
;
99 SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd
));
101 sreq
= scsi_allocate_request(sdev
);
103 printk("SCSI internal ioctl failed, no memory\n");
107 sreq
->sr_data_direction
= SCSI_DATA_NONE
;
108 scsi_wait_req(sreq
, cmd
, NULL
, 0, timeout
, retries
);
110 SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", sreq
->sr_result
));
112 if (driver_byte(sreq
->sr_result
)) {
113 switch (sreq
->sr_sense_buffer
[2] & 0xf) {
114 case ILLEGAL_REQUEST
:
115 if (cmd
[0] == ALLOW_MEDIUM_REMOVAL
)
118 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
120 case NOT_READY
: /* This happens if there is no disc in drive */
121 if (sdev
->removable
&& (cmd
[0] != TEST_UNIT_READY
)) {
122 printk(KERN_INFO
"Device not ready. Make sure 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("SCSI error: host %d id %d lun %d return code = %x\n",
137 printk("\tSense class %x, sense error %x, extended sense %x\n",
138 sense_class(sreq
->sr_sense_buffer
[0]),
139 sense_error(sreq
->sr_sense_buffer
[0]),
140 sreq
->sr_sense_buffer
[2] & 0xf);
145 result
= sreq
->sr_result
;
146 SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
147 scsi_release_request(sreq
);
151 int scsi_set_medium_removal(struct scsi_device
*sdev
, char state
)
153 char scsi_cmd
[MAX_COMMAND_SIZE
];
156 if (!sdev
->removable
|| !sdev
->lockable
)
159 scsi_cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
166 ret
= ioctl_internal_command(sdev
, scsi_cmd
,
167 IOCTL_NORMAL_TIMEOUT
, NORMAL_RETRIES
);
169 sdev
->locked
= (state
== SCSI_REMOVAL_PREVENT
);
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
*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 (verify_area(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
= SCSI_DATA_READ
;
262 } else if (outlen
== 0 ) {
263 data_direction
= SCSI_DATA_WRITE
;
266 * Can this ever happen?
268 data_direction
= SCSI_DATA_UNKNOWN
;
273 data_direction
= SCSI_DATA_NONE
;
277 * Obtain the command from the user's address space.
279 cmdlen
= COMMAND_SIZE(opcode
);
283 if (verify_area(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
);
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
);
355 * The scsi_ioctl_get_pci() function places into arg the value
356 * pci_dev::slot_name (8 characters) for the PCI device (if any).
357 * Returns: 0 on success
358 * -ENXIO if there isn't a PCI device pointer
359 * (could be because the SCSI driver hasn't been
360 * updated yet, or because it isn't a SCSI
362 * any copy_to_user() error on failure there
364 static int scsi_ioctl_get_pci(struct scsi_device
*sdev
, void *arg
)
366 struct device
*dev
= scsi_get_device(sdev
->host
);
370 return copy_to_user(arg
, dev
->bus_id
, sizeof(dev
->bus_id
));
375 * the scsi_ioctl() function differs from most ioctls in that it does
376 * not take a major/minor number as the dev field. Rather, it takes
377 * a pointer to a scsi_devices[] element, a structure.
379 int scsi_ioctl(struct scsi_device
*sdev
, int cmd
, void *arg
)
381 char scsi_cmd
[MAX_COMMAND_SIZE
];
383 /* No idea how this happens.... */
388 * If we are in the middle of error recovery, don't let anyone
389 * else try and use this device. Also, if error recovery fails, it
390 * may try and take the device offline, in which case all further
391 * access to the device is prohibited.
393 if (!scsi_block_when_processing_errors(sdev
))
397 case SCSI_IOCTL_GET_IDLUN
:
398 if (verify_area(VERIFY_WRITE
, arg
, sizeof(struct scsi_idlun
)))
401 __put_user((sdev
->id
& 0xff)
402 + ((sdev
->lun
& 0xff) << 8)
403 + ((sdev
->channel
& 0xff) << 16)
404 + ((sdev
->host
->host_no
& 0xff) << 24),
405 &((struct scsi_idlun
*)arg
)->dev_id
);
406 __put_user(sdev
->host
->unique_id
,
407 &((struct scsi_idlun
*)arg
)->host_unique_id
);
409 case SCSI_IOCTL_GET_BUS_NUMBER
:
410 return put_user(sdev
->host
->host_no
, (int *)arg
);
412 * The next two ioctls either need to go or need to be changed to
413 * pass tagged queueing changes through the low level drivers.
414 * Simply enabling or disabling tagged queueing without the knowledge
415 * of the low level driver is a *BAD* thing.
417 * Oct. 10, 2002 - Doug Ledford <dledford@redhat.com>
419 case SCSI_IOCTL_TAGGED_ENABLE
:
420 if (!capable(CAP_SYS_ADMIN
))
422 if (!sdev
->tagged_supported
)
424 sdev
->tagged_queue
= 1;
425 sdev
->current_tag
= 1;
427 case SCSI_IOCTL_TAGGED_DISABLE
:
428 if (!capable(CAP_SYS_ADMIN
))
430 if (!sdev
->tagged_supported
)
432 sdev
->tagged_queue
= 0;
433 sdev
->current_tag
= 0;
435 case SCSI_IOCTL_PROBE_HOST
:
436 return ioctl_probe(sdev
->host
, arg
);
437 case SCSI_IOCTL_SEND_COMMAND
:
438 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
440 return scsi_ioctl_send_command(sdev
,
441 (struct scsi_ioctl_command
*)arg
);
442 case SCSI_IOCTL_DOORLOCK
:
443 return scsi_set_medium_removal(sdev
, SCSI_REMOVAL_PREVENT
);
444 case SCSI_IOCTL_DOORUNLOCK
:
445 return scsi_set_medium_removal(sdev
, SCSI_REMOVAL_ALLOW
);
446 case SCSI_IOCTL_TEST_UNIT_READY
:
447 scsi_cmd
[0] = TEST_UNIT_READY
;
449 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
451 return ioctl_internal_command(sdev
, scsi_cmd
,
452 IOCTL_NORMAL_TIMEOUT
, NORMAL_RETRIES
);
453 case SCSI_IOCTL_START_UNIT
:
454 scsi_cmd
[0] = START_STOP
;
456 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
458 return ioctl_internal_command(sdev
, scsi_cmd
,
459 START_STOP_TIMEOUT
, NORMAL_RETRIES
);
460 case SCSI_IOCTL_STOP_UNIT
:
461 scsi_cmd
[0] = START_STOP
;
463 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
465 return ioctl_internal_command(sdev
, scsi_cmd
,
466 START_STOP_TIMEOUT
, NORMAL_RETRIES
);
467 case SCSI_IOCTL_GET_PCI
:
468 return scsi_ioctl_get_pci(sdev
, arg
);
470 if (sdev
->host
->hostt
->ioctl
)
471 return sdev
->host
->hostt
->ioctl(sdev
, cmd
, arg
);
477 * Just like scsi_ioctl, only callable from kernel space with no
478 * fs segment fiddling.
481 int kernel_scsi_ioctl(struct scsi_device
*sdev
, int cmd
, void *arg
)
487 tmp
= scsi_ioctl(sdev
, cmd
, arg
);