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
8 #include <linux/module.h>
11 #include <asm/uaccess.h>
12 #include <asm/system.h>
15 #include <linux/interrupt.h>
16 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
20 #include <linux/string.h>
22 #include <linux/blk.h>
25 #include <scsi/scsi_ioctl.h>
27 #define NORMAL_RETRIES 5
28 #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
29 #define FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
30 #define START_STOP_TIMEOUT (60 * HZ)
31 #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
32 #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
33 #define READ_DEFECT_DATA_TIMEOUT (60 * HZ ) /* ZIP-250 on parallel port takes as long! */
35 #define MAX_BUF PAGE_SIZE
37 #define max(a,b) (((a) > (b)) ? (a) : (b))
40 * If we are told to probe a host, we will return 0 if the host is not
41 * present, 1 if the host is present, and will return an identifying
42 * string at *arg, if arg is non null, filling to the length stored at
46 static int ioctl_probe(struct Scsi_Host
*host
, void *buffer
)
48 unsigned int len
, slen
;
50 int temp
= host
->hostt
->present
;
53 if (get_user(len
, (unsigned int *) buffer
))
56 if (host
->hostt
->info
)
57 string
= host
->hostt
->info(host
);
59 string
= host
->hostt
->name
;
61 slen
= strlen(string
);
64 if (copy_to_user(buffer
, string
, len
))
73 * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
74 * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
76 * dev is the SCSI device struct ptr, *(int *) arg is the length of the
77 * input data, if any, not including the command string & counts,
78 * *((int *)arg + 1) is the output buffer size in bytes.
80 * *(char *) ((int *) arg)[2] the actual command byte.
82 * Note that if more than MAX_BUF bytes are requested to be transfered,
83 * the ioctl will fail with error EINVAL. MAX_BUF can be increased in
84 * the future by increasing the size that scsi_malloc will accept.
86 * This size *does not* include the initial lengths that were passed.
88 * The SCSI command is read from the memory location immediately after the
89 * length words, and the input data is right after the command. The SCSI
90 * routines know the command size based on the opcode decode.
92 * The output area is then filled in starting from the command byte.
95 static int ioctl_internal_command(Scsi_Device
* dev
, char *cmd
,
96 int timeout
, int retries
)
103 SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", cmd
[0]));
104 SRpnt
= scsi_allocate_request(dev
);
106 SRpnt
->sr_data_direction
= SCSI_DATA_NONE
;
107 scsi_wait_req(SRpnt
, cmd
, NULL
, 0, timeout
, retries
);
109 SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", SRpnt
->sr_result
));
111 if (driver_byte(SRpnt
->sr_result
) != 0)
112 switch (SRpnt
->sr_sense_buffer
[2] & 0xf) {
113 case ILLEGAL_REQUEST
:
114 if (cmd
[0] == ALLOW_MEDIUM_REMOVAL
)
117 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
119 case NOT_READY
: /* This happens if there is no disc in drive */
120 if (dev
->removable
&& (cmd
[0] != TEST_UNIT_READY
)) {
121 printk(KERN_INFO
"Device not ready. Make sure there is a disc in the drive.\n");
125 if (dev
->removable
) {
127 SRpnt
->sr_result
= 0; /* This is no longer considered an error */
128 /* gag this error, VFS will log it anyway /axboe */
129 /* printk(KERN_INFO "Disc change detected.\n"); */
132 default: /* Fall through for non-removable media */
133 printk("SCSI error: host %d id %d lun %d return code = %x\n",
138 printk("\tSense class %x, sense error %x, extended sense %x\n",
139 sense_class(SRpnt
->sr_sense_buffer
[0]),
140 sense_error(SRpnt
->sr_sense_buffer
[0]),
141 SRpnt
->sr_sense_buffer
[2] & 0xf);
145 result
= SRpnt
->sr_result
;
147 SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
148 SDpnt
= SRpnt
->sr_device
;
149 scsi_release_request(SRpnt
);
156 * This interface is depreciated - users should use the scsi generic (sg)
157 * interface instead, as this is a more flexible approach to performing
158 * generic SCSI commands on a device.
160 * The structure that we are passed should look like:
163 * unsigned int inlen; [i] Length of data to be written to device
164 * unsigned int outlen; [i] Length of data to be read from device
165 * unsigned char cmd[x]; [i] SCSI command (6 <= x <= 12).
166 * [o] Data read from device starts here.
167 * [o] On error, sense buffer starts here.
168 * unsigned char wdata[y]; [i] Data written to device starts here.
171 * - The SCSI command length is determined by examining the 1st byte
172 * of the given command. There is no way to override this.
173 * - Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
174 * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
175 * accomodate the sense buffer when an error occurs.
176 * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
177 * old code will not be surprised.
178 * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
179 * a negative return and the Unix error code in 'errno'.
180 * If the SCSI command succeeds then 0 is returned.
181 * Positive numbers returned are the compacted SCSI error codes (4
182 * bytes in one int) where the lowest byte is the SCSI status.
183 * See the drivers/scsi/scsi.h file for more information on this.
186 #define OMAX_SB_LEN 16 /* Old sense buffer length */
188 int scsi_ioctl_send_command(Scsi_Device
* dev
, Scsi_Ioctl_Command
* sic
)
191 unsigned char cmd
[MAX_COMMAND_SIZE
];
195 unsigned char opcode
;
196 int inlen
, outlen
, cmdlen
;
197 int needed
, buf_needed
;
198 int timeout
, retries
, result
;
204 * Verify that we can read at least this much.
206 if (verify_area(VERIFY_READ
, sic
, sizeof(Scsi_Ioctl_Command
)))
209 __get_user(inlen
, &sic
->inlen
);
210 __get_user(outlen
, &sic
->outlen
);
213 * We do not transfer more than MAX_BUF with this interface.
214 * If the user needs to transfer more data than this, they
215 * should use scsi_generics (sg) instead.
219 if (outlen
> MAX_BUF
)
223 __get_user(opcode
, cmd_in
);
225 needed
= buf_needed
= (inlen
> outlen
? inlen
: outlen
);
227 buf_needed
= (buf_needed
+ 511) & ~511;
228 if (buf_needed
> MAX_BUF
)
229 buf_needed
= MAX_BUF
;
230 buf
= (char *) scsi_malloc(buf_needed
);
233 memset(buf
, 0, buf_needed
);
235 data_direction
= SCSI_DATA_READ
;
236 } else if (outlen
== 0 ) {
237 data_direction
= SCSI_DATA_WRITE
;
240 * Can this ever happen?
242 data_direction
= SCSI_DATA_UNKNOWN
;
247 data_direction
= SCSI_DATA_NONE
;
251 * Obtain the command from the user's address space.
253 cmdlen
= COMMAND_SIZE(opcode
);
255 if (verify_area(VERIFY_READ
, cmd_in
, cmdlen
+ inlen
))
258 __copy_from_user(cmd
, cmd_in
, cmdlen
);
261 * Obtain the data to be sent to the device (if any).
263 __copy_from_user(buf
, cmd_in
+ cmdlen
, inlen
);
266 * Set the lun field to the correct value.
268 cmd
[1] = (cmd
[1] & 0x1f) | (dev
->lun
<< 5);
272 timeout
= FORMAT_UNIT_TIMEOUT
;
276 timeout
= START_STOP_TIMEOUT
;
277 retries
= NORMAL_RETRIES
;
280 timeout
= MOVE_MEDIUM_TIMEOUT
;
281 retries
= NORMAL_RETRIES
;
283 case READ_ELEMENT_STATUS
:
284 timeout
= READ_ELEMENT_STATUS_TIMEOUT
;
285 retries
= NORMAL_RETRIES
;
287 case READ_DEFECT_DATA
:
288 timeout
= READ_DEFECT_DATA_TIMEOUT
;
292 timeout
= IOCTL_NORMAL_TIMEOUT
;
293 retries
= NORMAL_RETRIES
;
300 SRpnt
= scsi_allocate_request(dev
);
306 SRpnt
->sr_data_direction
= data_direction
;
307 scsi_wait_req(SRpnt
, cmd
, buf
, needed
, timeout
, retries
);
310 * If there was an error condition, pass the info back to the user.
312 if (SRpnt
->sr_result
) {
313 int sb_len
= sizeof(SRpnt
->sr_sense_buffer
);
315 sb_len
= (sb_len
> OMAX_SB_LEN
) ? OMAX_SB_LEN
: sb_len
;
316 if (copy_to_user(cmd_in
, SRpnt
->sr_sense_buffer
, sb_len
))
319 if (copy_to_user(cmd_in
, buf
, outlen
))
322 result
= SRpnt
->sr_result
;
324 SDpnt
= SRpnt
->sr_device
;
325 scsi_release_request(SRpnt
);
329 scsi_free(buf
, buf_needed
);
336 printk("scsi_ioctl : device %d. command = ", dev
->id
);
337 for (i
= 0; i
< cmdlen
; ++i
)
338 printk("%02x ", cmd
[i
]);
339 printk("\nbuffer =");
340 for (i
= 0; i
< 20; ++i
)
341 printk("%02x ", buf
[i
]);
343 printk("inlen = %d, outlen = %d, cmdlen = %d\n",
344 inlen
, outlen
, cmdlen
);
345 printk("buffer = %d, cmd_in = %d\n", buffer
, cmd_in
);
352 * the scsi_ioctl() function differs from most ioctls in that it does
353 * not take a major/minor number as the dev field. Rather, it takes
354 * a pointer to a scsi_devices[] element, a structure.
356 int scsi_ioctl(Scsi_Device
* dev
, int cmd
, void *arg
)
358 char scsi_cmd
[MAX_COMMAND_SIZE
];
360 /* No idea how this happens.... */
365 * If we are in the middle of error recovery, don't let anyone
366 * else try and use this device. Also, if error recovery fails, it
367 * may try and take the device offline, in which case all further
368 * access to the device is prohibited.
370 if (!scsi_block_when_processing_errors(dev
)) {
374 case SCSI_IOCTL_GET_IDLUN
:
375 if (verify_area(VERIFY_WRITE
, arg
, sizeof(Scsi_Idlun
)))
380 + (dev
->channel
<< 16)
381 + ((dev
->host
->host_no
& 0xff) << 24),
382 &((Scsi_Idlun
*) arg
)->dev_id
);
383 __put_user(dev
->host
->unique_id
, &((Scsi_Idlun
*) arg
)->host_unique_id
);
385 case SCSI_IOCTL_GET_BUS_NUMBER
:
386 return put_user(dev
->host
->host_no
, (int *) arg
);
387 case SCSI_IOCTL_TAGGED_ENABLE
:
388 if (!capable(CAP_SYS_ADMIN
))
390 if (!dev
->tagged_supported
)
392 dev
->tagged_queue
= 1;
393 dev
->current_tag
= 1;
395 case SCSI_IOCTL_TAGGED_DISABLE
:
396 if (!capable(CAP_SYS_ADMIN
))
398 if (!dev
->tagged_supported
)
400 dev
->tagged_queue
= 0;
401 dev
->current_tag
= 0;
403 case SCSI_IOCTL_PROBE_HOST
:
404 return ioctl_probe(dev
->host
, arg
);
405 case SCSI_IOCTL_SEND_COMMAND
:
406 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
408 return scsi_ioctl_send_command((Scsi_Device
*) dev
,
409 (Scsi_Ioctl_Command
*) arg
);
410 case SCSI_IOCTL_DOORLOCK
:
411 if (!dev
->removable
|| !dev
->lockable
)
413 scsi_cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
414 scsi_cmd
[1] = dev
->lun
<< 5;
415 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
416 scsi_cmd
[4] = SCSI_REMOVAL_PREVENT
;
417 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
418 IOCTL_NORMAL_TIMEOUT
, NORMAL_RETRIES
);
420 case SCSI_IOCTL_DOORUNLOCK
:
421 if (!dev
->removable
|| !dev
->lockable
)
423 scsi_cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
424 scsi_cmd
[1] = dev
->lun
<< 5;
425 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
426 scsi_cmd
[4] = SCSI_REMOVAL_ALLOW
;
427 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
428 IOCTL_NORMAL_TIMEOUT
, NORMAL_RETRIES
);
429 case SCSI_IOCTL_TEST_UNIT_READY
:
430 scsi_cmd
[0] = TEST_UNIT_READY
;
431 scsi_cmd
[1] = dev
->lun
<< 5;
432 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
434 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
435 IOCTL_NORMAL_TIMEOUT
, NORMAL_RETRIES
);
437 case SCSI_IOCTL_START_UNIT
:
438 scsi_cmd
[0] = START_STOP
;
439 scsi_cmd
[1] = dev
->lun
<< 5;
440 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
442 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
443 START_STOP_TIMEOUT
, NORMAL_RETRIES
);
445 case SCSI_IOCTL_STOP_UNIT
:
446 scsi_cmd
[0] = START_STOP
;
447 scsi_cmd
[1] = dev
->lun
<< 5;
448 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
450 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
451 START_STOP_TIMEOUT
, NORMAL_RETRIES
);
454 if (dev
->host
->hostt
->ioctl
)
455 return dev
->host
->hostt
->ioctl(dev
, cmd
, arg
);
462 * Just like scsi_ioctl, only callable from kernel space with no
463 * fs segment fiddling.
466 int kernel_scsi_ioctl(Scsi_Device
* dev
, int cmd
, void *arg
)
472 tmp
= scsi_ioctl(dev
, cmd
, arg
);
478 * Overrides for Emacs so that we almost follow Linus's tabbing style.
479 * Emacs will notice this stuff at the end of the file and automatically
480 * adjust the settings for this buffer only. This must remain at the end
482 * ---------------------------------------------------------------------------
485 * c-brace-imaginary-offset: 0
487 * c-argdecl-indent: 4
489 * c-continued-statement-offset: 4
490 * c-continued-brace-offset: 0
491 * indent-tabs-mode: nil