2 #include <linux/module.h>
5 #include <asm/uaccess.h>
6 #include <asm/system.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>
16 #include <linux/blk.h>
19 #include <scsi/scsi_ioctl.h>
21 #define NORMAL_RETRIES 5
22 #define NORMAL_TIMEOUT (10 * HZ)
23 #define FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
24 #define START_STOP_TIMEOUT (60 * HZ)
25 #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
26 #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
28 #define MAX_BUF PAGE_SIZE
30 #define max(a,b) (((a) > (b)) ? (a) : (b))
33 * If we are told to probe a host, we will return 0 if the host is not
34 * present, 1 if the host is present, and will return an identifying
35 * string at *arg, if arg is non null, filling to the length stored at
39 static int ioctl_probe(struct Scsi_Host
*host
, void *buffer
)
42 unsigned int len
, slen
;
45 if ((temp
= host
->hostt
->present
) && buffer
) {
46 result
= verify_area(VERIFY_READ
, buffer
, sizeof(long));
50 get_user(len
, (unsigned int *) buffer
);
51 if (host
->hostt
->info
)
52 string
= host
->hostt
->info(host
);
54 string
= host
->hostt
->name
;
56 slen
= strlen(string
);
59 result
= verify_area(VERIFY_WRITE
, buffer
, len
);
63 copy_to_user(buffer
, string
, len
);
71 * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
72 * The NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
74 * dev is the SCSI device struct ptr, *(int *) arg is the length of the
75 * input data, if any, not including the command string & counts,
76 * *((int *)arg + 1) is the output buffer size in bytes.
78 * *(char *) ((int *) arg)[2] the actual command byte.
80 * Note that if more than MAX_BUF bytes are requested to be transfered,
81 * the ioctl will fail with error EINVAL. MAX_BUF can be increased in
82 * the future by increasing the size that scsi_malloc will accept.
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 void scsi_ioctl_done(Scsi_Cmnd
* SCpnt
)
97 req
= &SCpnt
->request
;
98 req
->rq_status
= RQ_SCSI_DONE
; /* Busy, but indicate request done */
100 if (req
->sem
!= NULL
) {
105 static int ioctl_internal_command(Scsi_Device
* dev
, char *cmd
,
106 int timeout
, int retries
)
113 spin_lock_irqsave(&io_request_lock
, flags
);
115 SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", cmd
[0]));
116 SCpnt
= scsi_allocate_device(NULL
, dev
, 1);
118 DECLARE_MUTEX_LOCKED(sem
);
119 SCpnt
->request
.sem
= &sem
;
120 scsi_do_cmd(SCpnt
, cmd
, NULL
, 0, scsi_ioctl_done
, timeout
, retries
);
121 spin_unlock_irqrestore(&io_request_lock
, flags
);
123 spin_lock_irqsave(&io_request_lock
, flags
);
124 SCpnt
->request
.sem
= NULL
;
127 SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", SCpnt
->result
));
129 if (driver_byte(SCpnt
->result
) != 0)
130 switch (SCpnt
->sense_buffer
[2] & 0xf) {
131 case ILLEGAL_REQUEST
:
132 if (cmd
[0] == ALLOW_MEDIUM_REMOVAL
)
135 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
137 case NOT_READY
: /* This happens if there is no disc in drive */
138 if (dev
->removable
&& (cmd
[0] != TEST_UNIT_READY
)) {
139 printk(KERN_INFO
"Device not ready. Make sure there is a disc in the drive.\n");
143 if (dev
->removable
) {
145 SCpnt
->result
= 0; /* This is no longer considered an error */
146 /* gag this error, VFS will log it anyway /axboe */
147 /* printk(KERN_INFO "Disc change detected.\n"); */
150 default: /* Fall through for non-removable media */
151 printk("SCSI error: host %d id %d lun %d return code = %x\n",
156 printk("\tSense class %x, sense error %x, extended sense %x\n",
157 sense_class(SCpnt
->sense_buffer
[0]),
158 sense_error(SCpnt
->sense_buffer
[0]),
159 SCpnt
->sense_buffer
[2] & 0xf);
163 result
= SCpnt
->result
;
165 SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
166 SDpnt
= SCpnt
->device
;
167 scsi_release_command(SCpnt
);
170 if (!SDpnt
->was_reset
&& SDpnt
->scsi_request_fn
)
171 (*SDpnt
->scsi_request_fn
) ();
173 wake_up(&SDpnt
->device_wait
);
174 spin_unlock_irqrestore(&io_request_lock
, flags
);
179 * This interface is depreciated - users should use the scsi generic (sg)
180 * interface instead, as this is a more flexible approach to performing
181 * generic SCSI commands on a device.
183 * The structure that we are passed should look like:
186 * unsigned int inlen; [i] Length of data to be written to device
187 * unsigned int outlen; [i] Length of data to be read from device
188 * unsigned char cmd[x]; [i] SCSI command (6 <= x <= 12).
189 * [o] Data read from device starts here.
190 * [o] On error, sense buffer starts here.
191 * unsigned char wdata[y]; [i] Data written to device starts here.
194 * - The SCSI command length is determined by examining the 1st byte
195 * of the given command. There is no way to override this.
196 * - Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
197 * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
198 * accomodate the sense buffer when an error occurs.
199 * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
200 * old code will not be surprised.
201 * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
202 * a negative return and the Unix error code in 'errno'.
203 * If the SCSI command succeeds then 0 is returned.
204 * Positive numbers returned are the compacted SCSI error codes (4
205 * bytes in one int) where the lowest byte is the SCSI status.
206 * See the drivers/scsi/scsi.h file for more information on this.
209 #define OMAX_SB_LEN 16 /* Old sense buffer length */
211 int scsi_ioctl_send_command(Scsi_Device
* dev
, Scsi_Ioctl_Command
* sic
)
215 unsigned char cmd
[12];
219 unsigned char opcode
;
220 int inlen
, outlen
, cmdlen
;
221 int needed
, buf_needed
;
222 int timeout
, retries
, result
;
227 * Verify that we can read at least this much.
229 result
= verify_area(VERIFY_READ
, sic
, sizeof(Scsi_Ioctl_Command
));
233 get_user(inlen
, &sic
->inlen
);
234 get_user(outlen
, &sic
->outlen
);
237 * We do not transfer more than MAX_BUF with this interface.
238 * If the user needs to transfer more data than this, they
239 * should use scsi_generics (sg) instead.
243 if (outlen
> MAX_BUF
)
247 get_user(opcode
, cmd_in
);
249 needed
= buf_needed
= (inlen
> outlen
? inlen
: outlen
);
251 buf_needed
= (buf_needed
+ 511) & ~511;
252 if (buf_needed
> MAX_BUF
)
253 buf_needed
= MAX_BUF
;
254 spin_lock_irqsave(&io_request_lock
, flags
);
255 buf
= (char *) scsi_malloc(buf_needed
);
256 spin_unlock_irqrestore(&io_request_lock
, flags
);
259 memset(buf
, 0, buf_needed
);
264 * Obtain the command from the user's address space.
266 cmdlen
= COMMAND_SIZE(opcode
);
268 result
= verify_area(VERIFY_READ
, cmd_in
, cmdlen
+ inlen
);
272 copy_from_user(cmd
, cmd_in
, cmdlen
);
275 * Obtain the data to be sent to the device (if any).
277 copy_from_user(buf
, cmd_in
+ cmdlen
, inlen
);
280 * Set the lun field to the correct value.
282 cmd
[1] = (cmd
[1] & 0x1f) | (dev
->lun
<< 5);
286 timeout
= FORMAT_UNIT_TIMEOUT
;
290 timeout
= START_STOP_TIMEOUT
;
291 retries
= NORMAL_RETRIES
;
294 timeout
= MOVE_MEDIUM_TIMEOUT
;
295 retries
= NORMAL_RETRIES
;
297 case READ_ELEMENT_STATUS
:
298 timeout
= READ_ELEMENT_STATUS_TIMEOUT
;
299 retries
= NORMAL_RETRIES
;
302 timeout
= NORMAL_TIMEOUT
;
303 retries
= NORMAL_RETRIES
;
309 spin_lock_irqsave(&io_request_lock
, flags
);
311 SCpnt
= scsi_allocate_device(NULL
, dev
, 1);
314 DECLARE_MUTEX_LOCKED(sem
);
315 SCpnt
->request
.sem
= &sem
;
316 scsi_do_cmd(SCpnt
, cmd
, buf
, needed
, scsi_ioctl_done
,
318 spin_unlock_irqrestore(&io_request_lock
, flags
);
320 SCpnt
->request
.sem
= NULL
;
324 * If there was an error condition, pass the info back to the user.
327 int sb_len
= sizeof(SCpnt
->sense_buffer
);
329 sb_len
= (sb_len
> OMAX_SB_LEN
) ? OMAX_SB_LEN
: sb_len
;
330 result
= verify_area(VERIFY_WRITE
, cmd_in
, sb_len
);
333 copy_to_user(cmd_in
, SCpnt
->sense_buffer
, sb_len
);
335 result
= verify_area(VERIFY_WRITE
, cmd_in
, outlen
);
338 copy_to_user(cmd_in
, buf
, outlen
);
340 result
= SCpnt
->result
;
342 spin_lock_irqsave(&io_request_lock
, flags
);
344 wake_up(&SCpnt
->device
->device_wait
);
345 SDpnt
= SCpnt
->device
;
346 scsi_release_command(SCpnt
);
350 scsi_free(buf
, buf_needed
);
352 if (SDpnt
->scsi_request_fn
)
353 (*SDpnt
->scsi_request_fn
) ();
355 spin_unlock_irqrestore(&io_request_lock
, flags
);
360 printk("scsi_ioctl : device %d. command = ", dev
->id
);
361 for (i
= 0; i
< 12; ++i
)
362 printk("%02x ", cmd
[i
]);
363 printk("\nbuffer =");
364 for (i
= 0; i
< 20; ++i
)
365 printk("%02x ", buf
[i
]);
367 printk("inlen = %d, outlen = %d, cmdlen = %d\n",
368 inlen
, outlen
, cmdlen
);
369 printk("buffer = %d, cmd_in = %d\n", buffer
, cmd_in
);
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(Scsi_Device
* dev
, int cmd
, void *arg
)
385 /* No idea how this happens.... */
390 * If we are in the middle of error recovery, don't let anyone
391 * else try and use this device. Also, if error recovery fails, it
392 * may try and take the device offline, in which case all further
393 * access to the device is prohibited.
395 if (!scsi_block_when_processing_errors(dev
)) {
399 case SCSI_IOCTL_GET_IDLUN
:
400 result
= verify_area(VERIFY_WRITE
, arg
, sizeof(Scsi_Idlun
));
406 + (dev
->channel
<< 16)
407 + ((dev
->host
->hostt
->proc_dir
->low_ino
& 0xff) << 24),
408 &((Scsi_Idlun
*) arg
)->dev_id
);
409 put_user(dev
->host
->unique_id
, &((Scsi_Idlun
*) arg
)->host_unique_id
);
411 case SCSI_IOCTL_GET_BUS_NUMBER
:
412 result
= verify_area(VERIFY_WRITE
, (void *) arg
, sizeof(int));
415 put_user(dev
->host
->host_no
, (int *) arg
);
417 case SCSI_IOCTL_TAGGED_ENABLE
:
418 if (!capable(CAP_SYS_ADMIN
))
420 if (!dev
->tagged_supported
)
422 dev
->tagged_queue
= 1;
423 dev
->current_tag
= 1;
425 case SCSI_IOCTL_TAGGED_DISABLE
:
426 if (!capable(CAP_SYS_ADMIN
))
428 if (!dev
->tagged_supported
)
430 dev
->tagged_queue
= 0;
431 dev
->current_tag
= 0;
433 case SCSI_IOCTL_PROBE_HOST
:
434 return ioctl_probe(dev
->host
, arg
);
435 case SCSI_IOCTL_SEND_COMMAND
:
436 if (!capable(CAP_SYS_ADMIN
))
438 return scsi_ioctl_send_command((Scsi_Device
*) dev
,
439 (Scsi_Ioctl_Command
*) arg
);
440 case SCSI_IOCTL_DOORLOCK
:
441 if (!dev
->removable
|| !dev
->lockable
)
443 scsi_cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
444 scsi_cmd
[1] = dev
->lun
<< 5;
445 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
446 scsi_cmd
[4] = SCSI_REMOVAL_PREVENT
;
447 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
448 NORMAL_TIMEOUT
, NORMAL_RETRIES
);
450 case SCSI_IOCTL_DOORUNLOCK
:
451 if (!dev
->removable
|| !dev
->lockable
)
453 scsi_cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
454 scsi_cmd
[1] = dev
->lun
<< 5;
455 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
456 scsi_cmd
[4] = SCSI_REMOVAL_ALLOW
;
457 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
458 NORMAL_TIMEOUT
, NORMAL_RETRIES
);
459 case SCSI_IOCTL_TEST_UNIT_READY
:
460 scsi_cmd
[0] = TEST_UNIT_READY
;
461 scsi_cmd
[1] = dev
->lun
<< 5;
462 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
464 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
465 NORMAL_TIMEOUT
, NORMAL_RETRIES
);
467 case SCSI_IOCTL_START_UNIT
:
468 scsi_cmd
[0] = START_STOP
;
469 scsi_cmd
[1] = dev
->lun
<< 5;
470 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
472 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
473 START_STOP_TIMEOUT
, NORMAL_RETRIES
);
475 case SCSI_IOCTL_STOP_UNIT
:
476 scsi_cmd
[0] = START_STOP
;
477 scsi_cmd
[1] = dev
->lun
<< 5;
478 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
480 return ioctl_internal_command((Scsi_Device
*) dev
, scsi_cmd
,
481 START_STOP_TIMEOUT
, NORMAL_RETRIES
);
484 if (dev
->host
->hostt
->ioctl
)
485 return dev
->host
->hostt
->ioctl(dev
, cmd
, arg
);
492 * Just like scsi_ioctl, only callable from kernel space with no
493 * fs segment fiddling.
496 int kernel_scsi_ioctl(Scsi_Device
* dev
, int cmd
, void *arg
)
502 tmp
= scsi_ioctl(dev
, cmd
, arg
);
508 * Overrides for Emacs so that we almost follow Linus's tabbing style.
509 * Emacs will notice this stuff at the end of the file and automatically
510 * adjust the settings for this buffer only. This must remain at the end
512 * ---------------------------------------------------------------------------
515 * c-brace-imaginary-offset: 0
517 * c-argdecl-indent: 4
519 * c-continued-statement-offset: 4
520 * c-continued-brace-offset: 0
521 * indent-tabs-mode: nil