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 #define NORMAL_RETRIES 5
27 #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
28 #define FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
29 #define START_STOP_TIMEOUT (60 * HZ)
30 #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
31 #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
32 #define READ_DEFECT_DATA_TIMEOUT (60 * HZ ) /* ZIP-250 on parallel port takes as long! */
34 #define MAX_BUF PAGE_SIZE
37 * If we are told to probe a host, we will return 0 if the host is not
38 * present, 1 if the host is present, and will return an identifying
39 * string at *arg, if arg is non null, filling to the length stored at
43 static int ioctl_probe(struct Scsi_Host *host, void *buffer)
45 unsigned int len, slen;
47 int temp = host->hostt->present;
50 if (get_user(len, (unsigned int *) buffer))
53 if (host->hostt->info)
54 string = host->hostt->info(host);
56 string = host->hostt->name;
58 slen = strlen(string);
61 if (copy_to_user(buffer, string, len))
70 * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
71 * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
73 * dev is the SCSI device struct ptr, *(int *) arg is the length of the
74 * input data, if any, not including the command string & counts,
75 * *((int *)arg + 1) is the output buffer size in bytes.
77 * *(char *) ((int *) arg)[2] the actual command byte.
79 * Note that if more than MAX_BUF bytes are requested to be transferred,
80 * the ioctl will fail with error EINVAL.
82 * This size *does not* include the initial lengths that were passed.
84 * The SCSI command is read from the memory location immediately after the
85 * length words, and the input data is right after the command. The SCSI
86 * routines know the command size based on the opcode decode.
88 * The output area is then filled in starting from the command byte.
91 static int ioctl_internal_command(Scsi_Device * dev, char *cmd,
92 int timeout, int retries)
99 SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", cmd[0]));
100 if (NULL == (SRpnt = scsi_allocate_request(dev))) {
101 printk("SCSI internal ioctl failed, no memory\n");
105 SRpnt->sr_data_direction = SCSI_DATA_NONE;
106 scsi_wait_req(SRpnt, cmd, NULL, 0, timeout, retries);
108 SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", SRpnt->sr_result));
110 if (driver_byte(SRpnt->sr_result) != 0)
111 switch (SRpnt->sr_sense_buffer[2] & 0xf) {
112 case ILLEGAL_REQUEST:
113 if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
116 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
118 case NOT_READY: /* This happens if there is no disc in drive */
119 if (dev->removable && (cmd[0] != TEST_UNIT_READY)) {
120 printk(KERN_INFO "Device not ready. Make sure there is a disc in the drive.\n");
124 if (dev->removable) {
126 SRpnt->sr_result = 0; /* This is no longer considered an error */
127 /* gag this error, VFS will log it anyway /axboe */
128 /* printk(KERN_INFO "Disc change detected.\n"); */
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(SRpnt->sr_sense_buffer[0]),
139 sense_error(SRpnt->sr_sense_buffer[0]),
140 SRpnt->sr_sense_buffer[2] & 0xf);
144 result = SRpnt->sr_result;
146 SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
147 SDpnt = SRpnt->sr_device;
148 scsi_release_request(SRpnt);
154 int scsi_set_medium_removal(Scsi_Device *dev, char state)
156 char scsi_cmd[MAX_COMMAND_SIZE];
159 if (!dev->removable || !dev->lockable)
162 scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
169 ret = ioctl_internal_command(dev, scsi_cmd, IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
172 dev->locked = state == SCSI_REMOVAL_PREVENT;
178 * This interface is deprecated - users should use the scsi generic (sg)
179 * interface instead, as this is a more flexible approach to performing
180 * generic SCSI commands on a device.
182 * The structure that we are passed should look like:
185 * unsigned int inlen; [i] Length of data to be written to device
186 * unsigned int outlen; [i] Length of data to be read from device
187 * unsigned char cmd[x]; [i] SCSI command (6 <= x <= 12).
188 * [o] Data read from device starts here.
189 * [o] On error, sense buffer starts here.
190 * unsigned char wdata[y]; [i] Data written to device starts here.
193 * - The SCSI command length is determined by examining the 1st byte
194 * of the given command. There is no way to override this.
195 * - Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
196 * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
197 * accommodate the sense buffer when an error occurs.
198 * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
199 * old code will not be surprised.
200 * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
201 * a negative return and the Unix error code in 'errno'.
202 * If the SCSI command succeeds then 0 is returned.
203 * Positive numbers returned are the compacted SCSI error codes (4
204 * bytes in one int) where the lowest byte is the SCSI status.
205 * See the drivers/scsi/scsi.h file for more information on this.
208 #define OMAX_SB_LEN 16 /* Old sense buffer length */
210 int scsi_ioctl_send_command(Scsi_Device * dev, Scsi_Ioctl_Command * sic)
213 unsigned char cmd[MAX_COMMAND_SIZE];
217 unsigned char opcode;
218 unsigned int inlen, outlen, cmdlen;
219 unsigned int needed, buf_needed;
220 int timeout, retries, result;
221 int data_direction, gfp_mask = GFP_KERNEL;
229 if (dev->host->unchecked_isa_dma)
233 * Verify that we can read at least this much.
235 if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
239 foo = __get_user(inlen, &sic->inlen);
243 foo = __get_user(outlen, &sic->outlen);
247 if(__get_user(inlen, &sic->inlen))
250 if(__get_user(outlen, &sic->outlen))
255 * We do not transfer more than MAX_BUF with this interface.
256 * If the user needs to transfer more data than this, they
257 * should use scsi_generics (sg) instead.
261 if (outlen > MAX_BUF)
265 if(get_user(opcode, cmd_in))
268 needed = buf_needed = (inlen > outlen ? inlen : outlen);
270 buf_needed = (buf_needed + 511) & ~511;
271 if (buf_needed > MAX_BUF)
272 buf_needed = MAX_BUF;
273 buf = (char *) kmalloc(buf_needed, gfp_mask);
276 memset(buf, 0, buf_needed);
278 data_direction = SCSI_DATA_READ;
279 } else if (outlen == 0 ) {
280 data_direction = SCSI_DATA_WRITE;
283 * Can this ever happen?
285 data_direction = SCSI_DATA_UNKNOWN;
290 data_direction = SCSI_DATA_NONE;
294 * Obtain the command from the user's address space.
296 cmdlen = COMMAND_SIZE(opcode);
300 if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
303 if(__copy_from_user(cmd, cmd_in, cmdlen))
307 * Obtain the data to be sent to the device (if any).
310 if(copy_from_user(buf, cmd_in + cmdlen, inlen))
314 case SEND_DIAGNOSTIC:
316 timeout = FORMAT_UNIT_TIMEOUT;
320 timeout = START_STOP_TIMEOUT;
321 retries = NORMAL_RETRIES;
324 timeout = MOVE_MEDIUM_TIMEOUT;
325 retries = NORMAL_RETRIES;
327 case READ_ELEMENT_STATUS:
328 timeout = READ_ELEMENT_STATUS_TIMEOUT;
329 retries = NORMAL_RETRIES;
331 case READ_DEFECT_DATA:
332 timeout = READ_DEFECT_DATA_TIMEOUT;
336 timeout = IOCTL_NORMAL_TIMEOUT;
337 retries = NORMAL_RETRIES;
344 SRpnt = scsi_allocate_request(dev);
351 SRpnt->sr_data_direction = data_direction;
352 scsi_wait_req(SRpnt, cmd, buf, needed, timeout, retries);
355 * If there was an error condition, pass the info back to the user.
358 result = SRpnt->sr_result;
360 if (SRpnt->sr_result) {
361 int sb_len = sizeof(SRpnt->sr_sense_buffer);
363 sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
364 if (copy_to_user(cmd_in, SRpnt->sr_sense_buffer, sb_len))
367 if (copy_to_user(cmd_in, buf, outlen))
371 SDpnt = SRpnt->sr_device;
372 scsi_release_request(SRpnt);
384 printk("scsi_ioctl : device %d. command = ", dev->id);
385 for (i = 0; i < cmdlen; ++i)
386 printk("%02x ", cmd[i]);
387 printk("\nbuffer =");
388 for (i = 0; i < 20; ++i)
389 printk("%02x ", buf[i]);
391 printk("inlen = %d, outlen = %d, cmdlen = %d\n",
392 inlen, outlen, cmdlen);
393 printk("buffer = %d, cmd_in = %d\n", buffer, cmd_in);
400 * The scsi_ioctl_get_pci() function places into arg the value
401 * pci_dev::slot_name (8 characters) for the PCI device (if any).
402 * Returns: 0 on success
403 * -ENXIO if there isn't a PCI device pointer
404 * (could be because the SCSI driver hasn't been
405 * updated yet, or because it isn't a SCSI
407 * any copy_to_user() error on failure there
410 scsi_ioctl_get_pci(Scsi_Device * sdev, void *arg)
412 struct device *dev = scsi_get_device(sdev->host);
414 if (!dev) return -ENXIO;
415 return copy_to_user(arg, dev->bus_id,
416 sizeof(dev->bus_id));
421 * the scsi_ioctl() function differs from most ioctls in that it does
422 * not take a major/minor number as the dev field. Rather, it takes
423 * a pointer to a scsi_devices[] element, a structure.
425 int scsi_ioctl(Scsi_Device * dev, int cmd, void *arg)
427 char scsi_cmd[MAX_COMMAND_SIZE];
429 /* No idea how this happens.... */
434 * If we are in the middle of error recovery, don't let anyone
435 * else try and use this device. Also, if error recovery fails, it
436 * may try and take the device offline, in which case all further
437 * access to the device is prohibited.
439 if (!scsi_block_when_processing_errors(dev)) {
444 case SCSI_IOCTL_GET_IDLUN:
445 if (verify_area(VERIFY_WRITE, arg, sizeof(Scsi_Idlun)))
448 __put_user((dev->id & 0xff)
449 + ((dev->lun & 0xff) << 8)
450 + ((dev->channel & 0xff) << 16)
451 + ((dev->host->host_no & 0xff) << 24),
452 &((Scsi_Idlun *) arg)->dev_id);
453 __put_user(dev->host->unique_id, &((Scsi_Idlun *) arg)->host_unique_id);
455 case SCSI_IOCTL_GET_BUS_NUMBER:
456 return put_user(dev->host->host_no, (int *) arg);
458 * The next two ioctls either need to go or need to be changed to
459 * pass tagged queueing changes through the low level drivers.
460 * Simply enabling or disabling tagged queueing without the knowledge
461 * of the low level driver is a *BAD* thing.
463 * Oct. 10, 2002 - Doug Ledford <dledford@redhat.com>
465 case SCSI_IOCTL_TAGGED_ENABLE:
466 if (!capable(CAP_SYS_ADMIN))
468 if (!dev->tagged_supported)
470 dev->tagged_queue = 1;
471 dev->current_tag = 1;
473 case SCSI_IOCTL_TAGGED_DISABLE:
474 if (!capable(CAP_SYS_ADMIN))
476 if (!dev->tagged_supported)
478 dev->tagged_queue = 0;
479 dev->current_tag = 0;
481 case SCSI_IOCTL_PROBE_HOST:
482 return ioctl_probe(dev->host, arg);
483 case SCSI_IOCTL_SEND_COMMAND:
484 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
486 return scsi_ioctl_send_command((Scsi_Device *) dev,
487 (Scsi_Ioctl_Command *) arg);
488 case SCSI_IOCTL_DOORLOCK:
489 return scsi_set_medium_removal(dev, SCSI_REMOVAL_PREVENT);
490 case SCSI_IOCTL_DOORUNLOCK:
491 return scsi_set_medium_removal(dev, SCSI_REMOVAL_ALLOW);
492 case SCSI_IOCTL_TEST_UNIT_READY:
493 scsi_cmd[0] = TEST_UNIT_READY;
495 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
497 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
498 IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
500 case SCSI_IOCTL_START_UNIT:
501 scsi_cmd[0] = START_STOP;
503 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
505 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
506 START_STOP_TIMEOUT, NORMAL_RETRIES);
508 case SCSI_IOCTL_STOP_UNIT:
509 scsi_cmd[0] = START_STOP;
511 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
513 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
514 START_STOP_TIMEOUT, NORMAL_RETRIES);
516 case SCSI_IOCTL_GET_PCI:
517 return scsi_ioctl_get_pci(dev, arg);
520 if (dev->host->hostt->ioctl)
521 return dev->host->hostt->ioctl(dev, cmd, arg);
528 * Just like scsi_ioctl, only callable from kernel space with no
529 * fs segment fiddling.
532 int kernel_scsi_ioctl(Scsi_Device * dev, int cmd, void *arg)
538 tmp = scsi_ioctl(dev, cmd, arg);