* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / scsi / scsi_ioctl.c
blobd9abefc1b7a879f6d39b5f3ac6eec8e0693148ad
1 #define __NO_VERSION__
2 #include <linux/module.h>
4 #include <asm/io.h>
5 #include <asm/uaccess.h>
6 #include <asm/system.h>
7 #include <asm/page.h>
9 #include <linux/interrupt.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/string.h>
16 #include <linux/blk.h>
17 #include "scsi.h"
18 #include "hosts.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
36 * (int *) arg
39 static int ioctl_probe(struct Scsi_Host *host, void *buffer)
41 int temp, result;
42 unsigned int len, slen;
43 const char *string;
45 if ((temp = host->hostt->present) && buffer) {
46 result = verify_area(VERIFY_READ, buffer, sizeof(long));
47 if (result)
48 return result;
50 get_user(len, (unsigned int *) buffer);
51 if (host->hostt->info)
52 string = host->hostt->info(host);
53 else
54 string = host->hostt->name;
55 if (string) {
56 slen = strlen(string);
57 if (len > slen)
58 len = slen + 1;
59 result = verify_area(VERIFY_WRITE, buffer, len);
60 if (result)
61 return result;
63 copy_to_user(buffer, string, len);
66 return temp;
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)
95 struct request *req;
97 req = &SCpnt->request;
98 req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
100 if (req->sem != NULL) {
101 up(req->sem);
105 static int ioctl_internal_command(Scsi_Device * dev, char *cmd,
106 int timeout, int retries)
108 unsigned long flags;
109 int result;
110 Scsi_Cmnd *SCpnt;
111 Scsi_Device *SDpnt;
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);
122 down(&sem);
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)
133 dev->lockable = 0;
134 else
135 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
136 break;
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");
140 break;
142 case UNIT_ATTENTION:
143 if (dev->removable) {
144 dev->changed = 1;
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"); */
148 break;
150 default: /* Fall through for non-removable media */
151 printk("SCSI error: host %d id %d lun %d return code = %x\n",
152 dev->host->host_no,
153 dev->id,
154 dev->lun,
155 SCpnt->result);
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);
168 SCpnt = NULL;
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);
175 return result;
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:
185 * struct sdata {
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.
192 * };
193 * Notes:
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)
213 unsigned long flags;
214 char *buf;
215 unsigned char cmd[12];
216 char *cmd_in;
217 Scsi_Cmnd *SCpnt;
218 Scsi_Device *SDpnt;
219 unsigned char opcode;
220 int inlen, outlen, cmdlen;
221 int needed, buf_needed;
222 int timeout, retries, result;
224 if (!sic)
225 return -EINVAL;
227 * Verify that we can read at least this much.
229 result = verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command));
230 if (result)
231 return result;
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.
241 if (inlen > MAX_BUF)
242 return -EINVAL;
243 if (outlen > MAX_BUF)
244 return -EINVAL;
246 cmd_in = sic->data;
247 get_user(opcode, cmd_in);
249 needed = buf_needed = (inlen > outlen ? inlen : outlen);
250 if (buf_needed) {
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);
257 if (!buf)
258 return -ENOMEM;
259 memset(buf, 0, buf_needed);
260 } else
261 buf = NULL;
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);
269 if (result)
270 return result;
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);
284 switch (opcode) {
285 case FORMAT_UNIT:
286 timeout = FORMAT_UNIT_TIMEOUT;
287 retries = 1;
288 break;
289 case START_STOP:
290 timeout = START_STOP_TIMEOUT;
291 retries = NORMAL_RETRIES;
292 break;
293 case MOVE_MEDIUM:
294 timeout = MOVE_MEDIUM_TIMEOUT;
295 retries = NORMAL_RETRIES;
296 break;
297 case READ_ELEMENT_STATUS:
298 timeout = READ_ELEMENT_STATUS_TIMEOUT;
299 retries = NORMAL_RETRIES;
300 break;
301 default:
302 timeout = NORMAL_TIMEOUT;
303 retries = NORMAL_RETRIES;
304 break;
307 #ifndef DEBUG_NO_CMD
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,
317 timeout, retries);
318 spin_unlock_irqrestore(&io_request_lock, flags);
319 down(&sem);
320 SCpnt->request.sem = NULL;
324 * If there was an error condition, pass the info back to the user.
326 if (SCpnt->result) {
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);
331 if (result)
332 return result;
333 copy_to_user(cmd_in, SCpnt->sense_buffer, sb_len);
334 } else {
335 result = verify_area(VERIFY_WRITE, cmd_in, outlen);
336 if (result)
337 return result;
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);
347 SCpnt = NULL;
349 if (buf)
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);
356 return result;
357 #else
359 int i;
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]);
366 printk("\n");
367 printk("inlen = %d, outlen = %d, cmdlen = %d\n",
368 inlen, outlen, cmdlen);
369 printk("buffer = %d, cmd_in = %d\n", buffer, cmd_in);
371 return 0;
372 #endif
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)
382 int result;
383 char scsi_cmd[12];
385 /* No idea how this happens.... */
386 if (!dev)
387 return -ENXIO;
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)) {
396 return -ENODEV;
398 switch (cmd) {
399 case SCSI_IOCTL_GET_IDLUN:
400 result = verify_area(VERIFY_WRITE, arg, sizeof(Scsi_Idlun));
401 if (result)
402 return result;
404 put_user(dev->id
405 + (dev->lun << 8)
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);
410 return 0;
411 case SCSI_IOCTL_GET_BUS_NUMBER:
412 result = verify_area(VERIFY_WRITE, (void *) arg, sizeof(int));
413 if (result)
414 return result;
415 put_user(dev->host->host_no, (int *) arg);
416 return 0;
417 case SCSI_IOCTL_TAGGED_ENABLE:
418 if (!capable(CAP_SYS_ADMIN))
419 return -EACCES;
420 if (!dev->tagged_supported)
421 return -EINVAL;
422 dev->tagged_queue = 1;
423 dev->current_tag = 1;
424 return 0;
425 case SCSI_IOCTL_TAGGED_DISABLE:
426 if (!capable(CAP_SYS_ADMIN))
427 return -EACCES;
428 if (!dev->tagged_supported)
429 return -EINVAL;
430 dev->tagged_queue = 0;
431 dev->current_tag = 0;
432 return 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))
437 return -EACCES;
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)
442 return 0;
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);
449 break;
450 case SCSI_IOCTL_DOORUNLOCK:
451 if (!dev->removable || !dev->lockable)
452 return 0;
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;
463 scsi_cmd[4] = 0;
464 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
465 NORMAL_TIMEOUT, NORMAL_RETRIES);
466 break;
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;
471 scsi_cmd[4] = 1;
472 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
473 START_STOP_TIMEOUT, NORMAL_RETRIES);
474 break;
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;
479 scsi_cmd[4] = 0;
480 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
481 START_STOP_TIMEOUT, NORMAL_RETRIES);
482 break;
483 default:
484 if (dev->host->hostt->ioctl)
485 return dev->host->hostt->ioctl(dev, cmd, arg);
486 return -EINVAL;
488 return -EINVAL;
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)
498 mm_segment_t oldfs;
499 int tmp;
500 oldfs = get_fs();
501 set_fs(get_ds());
502 tmp = scsi_ioctl(dev, cmd, arg);
503 set_fs(oldfs);
504 return tmp;
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
511 * of the file.
512 * ---------------------------------------------------------------------------
513 * Local variables:
514 * c-indent-level: 4
515 * c-brace-imaginary-offset: 0
516 * c-brace-offset: -4
517 * c-argdecl-indent: 4
518 * c-label-offset: -4
519 * c-continued-statement-offset: 4
520 * c-continued-brace-offset: 0
521 * indent-tabs-mode: nil
522 * tab-width: 8
523 * End: