Merge with Linux 2.5.73.
[linux-2.6/linux-mips.git] / drivers / scsi / scsi_ioctl.c
blob6458c99bab15be9740554ad64dec2aa28030bab4
1 /*
2 * Changes:
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
6 */
7 #include <linux/module.h>
9 #include <asm/io.h>
10 #include <asm/uaccess.h>
11 #include <asm/system.h>
12 #include <asm/page.h>
14 #include <linux/interrupt.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/string.h>
21 #include <linux/blk.h>
22 #include "scsi.h"
23 #include "hosts.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
42 * (int *) arg
45 static int ioctl_probe(struct Scsi_Host *host, void *buffer)
47 unsigned int len, slen;
48 const char *string;
49 int temp = host->hostt->present;
51 if (temp && buffer) {
52 if (get_user(len, (unsigned int *) buffer))
53 return -EFAULT;
55 if (host->hostt->info)
56 string = host->hostt->info(host);
57 else
58 string = host->hostt->name;
59 if (string) {
60 slen = strlen(string);
61 if (len > slen)
62 len = slen + 1;
63 if (copy_to_user(buffer, string, len))
64 return -EFAULT;
67 return temp;
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;
97 int result;
99 SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd));
101 sreq = scsi_allocate_request(sdev);
102 if (!sreq) {
103 printk("SCSI internal ioctl failed, no memory\n");
104 return -ENOMEM;
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)
116 sdev->lockable = 0;
117 else
118 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
119 break;
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");
123 break;
125 case UNIT_ATTENTION:
126 if (sdev->removable) {
127 sdev->changed = 1;
128 sreq->sr_result = 0; /* This is no longer considered an error */
129 break;
131 default: /* Fall through for non-removable media */
132 printk("SCSI error: host %d id %d lun %d return code = %x\n",
133 sdev->host->host_no,
134 sdev->id,
135 sdev->lun,
136 sreq->sr_result);
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);
148 return result;
151 int scsi_set_medium_removal(struct scsi_device *sdev, char state)
153 char scsi_cmd[MAX_COMMAND_SIZE];
154 int ret;
156 if (!sdev->removable || !sdev->lockable)
157 return 0;
159 scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
160 scsi_cmd[1] = 0;
161 scsi_cmd[2] = 0;
162 scsi_cmd[3] = 0;
163 scsi_cmd[4] = state;
164 scsi_cmd[5] = 0;
166 ret = ioctl_internal_command(sdev, scsi_cmd,
167 IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
168 if (ret == 0)
169 sdev->locked = (state == SCSI_REMOVAL_PREVENT);
170 return ret;
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:
180 * struct sdata {
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.
187 * };
188 * Notes:
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)
209 char *buf;
210 unsigned char cmd[MAX_COMMAND_SIZE];
211 char *cmd_in;
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;
219 if (!sic)
220 return -EINVAL;
222 if (sdev->host->unchecked_isa_dma)
223 gfp_mask |= GFP_DMA;
226 * Verify that we can read at least this much.
228 if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
229 return -EFAULT;
231 if(__get_user(inlen, &sic->inlen))
232 return -EFAULT;
234 if(__get_user(outlen, &sic->outlen))
235 return -EFAULT;
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.
242 if (inlen > MAX_BUF)
243 return -EINVAL;
244 if (outlen > MAX_BUF)
245 return -EINVAL;
247 cmd_in = sic->data;
248 if(get_user(opcode, cmd_in))
249 return -EFAULT;
251 needed = buf_needed = (inlen > outlen ? inlen : outlen);
252 if (buf_needed) {
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);
257 if (!buf)
258 return -ENOMEM;
259 memset(buf, 0, buf_needed);
260 if (inlen == 0) {
261 data_direction = SCSI_DATA_READ;
262 } else if (outlen == 0 ) {
263 data_direction = SCSI_DATA_WRITE;
264 } else {
266 * Can this ever happen?
268 data_direction = SCSI_DATA_UNKNOWN;
271 } else {
272 buf = NULL;
273 data_direction = SCSI_DATA_NONE;
277 * Obtain the command from the user's address space.
279 cmdlen = COMMAND_SIZE(opcode);
281 result = -EFAULT;
283 if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
284 goto error;
286 if(__copy_from_user(cmd, cmd_in, cmdlen))
287 goto error;
290 * Obtain the data to be sent to the device (if any).
293 if(copy_from_user(buf, cmd_in + cmdlen, inlen))
294 goto error;
296 switch (opcode) {
297 case SEND_DIAGNOSTIC:
298 case FORMAT_UNIT:
299 timeout = FORMAT_UNIT_TIMEOUT;
300 retries = 1;
301 break;
302 case START_STOP:
303 timeout = START_STOP_TIMEOUT;
304 retries = NORMAL_RETRIES;
305 break;
306 case MOVE_MEDIUM:
307 timeout = MOVE_MEDIUM_TIMEOUT;
308 retries = NORMAL_RETRIES;
309 break;
310 case READ_ELEMENT_STATUS:
311 timeout = READ_ELEMENT_STATUS_TIMEOUT;
312 retries = NORMAL_RETRIES;
313 break;
314 case READ_DEFECT_DATA:
315 timeout = READ_DEFECT_DATA_TIMEOUT;
316 retries = 1;
317 break;
318 default:
319 timeout = IOCTL_NORMAL_TIMEOUT;
320 retries = NORMAL_RETRIES;
321 break;
324 sreq = scsi_allocate_request(sdev);
325 if (sreq) {
326 result = -EINTR;
327 goto error;
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;
337 if (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))
342 result = -EFAULT;
343 } else {
344 if (copy_to_user(cmd_in, buf, outlen))
345 result = -EFAULT;
348 scsi_release_request(sreq);
349 error:
350 kfree(buf);
351 return result;
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
361 * device)
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);
368 if (!dev)
369 return -ENXIO;
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.... */
384 if (!sdev)
385 return -ENXIO;
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))
394 return -ENODEV;
396 switch (cmd) {
397 case SCSI_IOCTL_GET_IDLUN:
398 if (verify_area(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
399 return -EFAULT;
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);
408 return 0;
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))
421 return -EACCES;
422 if (!sdev->tagged_supported)
423 return -EINVAL;
424 sdev->tagged_queue = 1;
425 sdev->current_tag = 1;
426 return 0;
427 case SCSI_IOCTL_TAGGED_DISABLE:
428 if (!capable(CAP_SYS_ADMIN))
429 return -EACCES;
430 if (!sdev->tagged_supported)
431 return -EINVAL;
432 sdev->tagged_queue = 0;
433 sdev->current_tag = 0;
434 return 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))
439 return -EACCES;
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;
448 scsi_cmd[1] = 0;
449 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
450 scsi_cmd[4] = 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;
455 scsi_cmd[1] = 0;
456 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
457 scsi_cmd[4] = 1;
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;
462 scsi_cmd[1] = 0;
463 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
464 scsi_cmd[4] = 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);
469 default:
470 if (sdev->host->hostt->ioctl)
471 return sdev->host->hostt->ioctl(sdev, cmd, arg);
473 return -EINVAL;
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)
483 mm_segment_t oldfs;
484 int tmp;
485 oldfs = get_fs();
486 set_fs(get_ds());
487 tmp = scsi_ioctl(sdev, cmd, arg);
488 set_fs(oldfs);
489 return tmp;