clean
[linux-2.4.21-pre4.git] / drivers / scsi / scsi_ioctl.c
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 #define __NO_VERSION__
8 #include <linux/module.h>
9
10 #include <asm/io.h>
11 #include <asm/uaccess.h>
12 #include <asm/system.h>
13 #include <asm/page.h>
14
15 #include <linux/interrupt.h>
16 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/mm.h>
20 #include <linux/string.h>
21
22 #include <linux/blk.h>
23 #include "scsi.h"
24 #include "hosts.h"
25 #include <scsi/scsi_ioctl.h>
26
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! */
34
35 #define MAX_BUF PAGE_SIZE
36
37 /*
38  * If we are told to probe a host, we will return 0 if  the host is not
39  * present, 1 if the host is present, and will return an identifying
40  * string at *arg, if arg is non null, filling to the length stored at
41  * (int *) arg
42  */
43
44 static int ioctl_probe(struct Scsi_Host *host, void *buffer)
45 {
46         unsigned int len, slen;
47         const char *string;
48         int temp = host->hostt->present;
49
50         if (temp && buffer) {
51                 if (get_user(len, (unsigned int *) buffer))
52                         return -EFAULT;
53
54                 if (host->hostt->info)
55                         string = host->hostt->info(host);
56                 else
57                         string = host->hostt->name;
58                 if (string) {
59                         slen = strlen(string);
60                         if (len > slen)
61                                 len = slen + 1;
62                         if (copy_to_user(buffer, string, len))
63                                 return -EFAULT;
64                 }
65         }
66         return temp;
67 }
68
69 /*
70
71  * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
72  * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES  variables are used.  
73  * 
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.
77  * 
78  * *(char *) ((int *) arg)[2] the actual command byte.   
79  * 
80  * Note that if more than MAX_BUF bytes are requested to be transferred,
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.
83  * 
84  * This size *does not* include the initial lengths that were passed.
85  * 
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.  
89  * 
90  * The output area is then filled in starting from the command byte. 
91  */
92
93 static int ioctl_internal_command(Scsi_Device * dev, char *cmd,
94                                   int timeout, int retries)
95 {
96         int result;
97         Scsi_Request *SRpnt;
98         Scsi_Device *SDpnt;
99
100
101         SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", cmd[0]));
102         if (NULL == (SRpnt = scsi_allocate_request(dev))) {
103                 printk("SCSI internal ioctl failed, no memory\n");
104                 return -ENOMEM;
105         }
106
107         SRpnt->sr_data_direction = SCSI_DATA_NONE;
108         scsi_wait_req(SRpnt, cmd, NULL, 0, timeout, retries);
109
110         SCSI_LOG_IOCTL(2, printk("Ioctl returned  0x%x\n", SRpnt->sr_result));
111
112         if (driver_byte(SRpnt->sr_result) != 0)
113                 switch (SRpnt->sr_sense_buffer[2] & 0xf) {
114                 case ILLEGAL_REQUEST:
115                         if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
116                                 dev->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 (dev->removable && (cmd[0] != TEST_UNIT_READY)) {
122                                 //jackl
123                                 //printk(KERN_INFO "Device not ready.  Make sure there is a disc in the drive.\n");
124                                 break;
125                         }
126                 case UNIT_ATTENTION:
127                         if (dev->removable) {
128                                 dev->changed = 1;
129                                 SRpnt->sr_result = 0;   /* This is no longer considered an error */
130                                 /* gag this error, VFS will log it anyway /axboe */
131                                 /* printk(KERN_INFO "Disc change detected.\n"); */
132                                 break;
133                         };
134                 default:        /* Fall through for non-removable media */
135                         printk("SCSI error: host %d id %d lun %d return code = %x\n",
136                                dev->host->host_no,
137                                dev->id,
138                                dev->lun,
139                                SRpnt->sr_result);
140                         printk("\tSense class %x, sense error %x, extended sense %x\n",
141                                sense_class(SRpnt->sr_sense_buffer[0]),
142                                sense_error(SRpnt->sr_sense_buffer[0]),
143                                SRpnt->sr_sense_buffer[2] & 0xf);
144
145                 };
146
147         result = SRpnt->sr_result;
148
149         SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
150         SDpnt = SRpnt->sr_device;
151         scsi_release_request(SRpnt);
152         SRpnt = NULL;
153
154         return result;
155 }
156
157 /*
158  * This interface is depreciated - users should use the scsi generic (sg)
159  * interface instead, as this is a more flexible approach to performing
160  * generic SCSI commands on a device.
161  *
162  * The structure that we are passed should look like:
163  *
164  * struct sdata {
165  *  unsigned int inlen;      [i] Length of data to be written to device 
166  *  unsigned int outlen;     [i] Length of data to be read from device 
167  *  unsigned char cmd[x];    [i] SCSI command (6 <= x <= 12).
168  *                           [o] Data read from device starts here.
169  *                           [o] On error, sense buffer starts here.
170  *  unsigned char wdata[y];  [i] Data written to device starts here.
171  * };
172  * Notes:
173  *   -  The SCSI command length is determined by examining the 1st byte
174  *      of the given command. There is no way to override this.
175  *   -  Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
176  *   -  The length (x + y) must be at least OMAX_SB_LEN bytes long to
177  *      accomodate the sense buffer when an error occurs.
178  *      The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
179  *      old code will not be surprised.
180  *   -  If a Unix error occurs (e.g. ENOMEM) then the user will receive
181  *      a negative return and the Unix error code in 'errno'. 
182  *      If the SCSI command succeeds then 0 is returned.
183  *      Positive numbers returned are the compacted SCSI error codes (4 
184  *      bytes in one int) where the lowest byte is the SCSI status.
185  *      See the drivers/scsi/scsi.h file for more information on this.
186  *
187  */
188 #define OMAX_SB_LEN 16          /* Old sense buffer length */
189
190 int scsi_ioctl_send_command(Scsi_Device * dev, Scsi_Ioctl_Command * sic)
191 {
192         char *buf;
193         unsigned char cmd[MAX_COMMAND_SIZE];
194         char *cmd_in;
195         Scsi_Request *SRpnt;
196         Scsi_Device *SDpnt;
197         unsigned char opcode;
198         unsigned int inlen, outlen, cmdlen;
199         unsigned int needed, buf_needed;
200         int timeout, retries, result;
201         int data_direction;
202
203         if (!sic)
204                 return -EINVAL;
205         /*
206          * Verify that we can read at least this much.
207          */
208         if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
209                 return -EFAULT;
210
211         if(__get_user(inlen, &sic->inlen))
212                 return -EFAULT;
213                 
214         if(__get_user(outlen, &sic->outlen))
215                 return -EFAULT;
216
217         /*
218          * We do not transfer more than MAX_BUF with this interface.
219          * If the user needs to transfer more data than this, they
220          * should use scsi_generics (sg) instead.
221          */
222         if (inlen > MAX_BUF)
223                 return -EINVAL;
224         if (outlen > MAX_BUF)
225                 return -EINVAL;
226
227         cmd_in = sic->data;
228         if(get_user(opcode, cmd_in))
229                 return -EFAULT;
230
231         needed = buf_needed = (inlen > outlen ? inlen : outlen);
232         if (buf_needed) {
233                 buf_needed = (buf_needed + 511) & ~511;
234                 if (buf_needed > MAX_BUF)
235                         buf_needed = MAX_BUF;
236                 buf = (char *) scsi_malloc(buf_needed);
237                 if (!buf)
238                         return -ENOMEM;
239                 memset(buf, 0, buf_needed);
240                 if( inlen == 0 ) {
241                         data_direction = SCSI_DATA_READ;
242                 } else if (outlen == 0 ) {
243                         data_direction = SCSI_DATA_WRITE;
244                 } else {
245                         /*
246                          * Can this ever happen?
247                          */
248                         data_direction = SCSI_DATA_UNKNOWN;
249                 }
250
251         } else {
252                 buf = NULL;
253                 data_direction = SCSI_DATA_NONE;
254         }
255
256         /*
257          * Obtain the command from the user's address space.
258          */
259         cmdlen = COMMAND_SIZE(opcode);
260         
261         result = -EFAULT;
262
263         if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
264                 goto error;
265
266         if(__copy_from_user(cmd, cmd_in, cmdlen))
267                 goto error;
268
269         /*
270          * Obtain the data to be sent to the device (if any).
271          */
272
273         if(copy_from_user(buf, cmd_in + cmdlen, inlen))
274                 goto error;
275
276         /*
277          * Set the lun field to the correct value.
278          */
279         if (dev->scsi_level <= SCSI_2)
280                 cmd[1] = (cmd[1] & 0x1f) | (dev->lun << 5);
281
282         switch (opcode) {
283         case FORMAT_UNIT:
284                 timeout = FORMAT_UNIT_TIMEOUT;
285                 retries = 1;
286                 break;
287         case START_STOP:
288                 timeout = START_STOP_TIMEOUT;
289                 retries = NORMAL_RETRIES;
290                 break;
291         case MOVE_MEDIUM:
292                 timeout = MOVE_MEDIUM_TIMEOUT;
293                 retries = NORMAL_RETRIES;
294                 break;
295         case READ_ELEMENT_STATUS:
296                 timeout = READ_ELEMENT_STATUS_TIMEOUT;
297                 retries = NORMAL_RETRIES;
298                 break;
299         case READ_DEFECT_DATA:
300                 timeout = READ_DEFECT_DATA_TIMEOUT;
301                 retries = 1;
302                 break;
303         default:
304                 timeout = IOCTL_NORMAL_TIMEOUT;
305                 retries = NORMAL_RETRIES;
306                 break;
307         }
308
309 #ifndef DEBUG_NO_CMD
310
311
312         SRpnt = scsi_allocate_request(dev);
313         if( SRpnt == NULL )
314         {
315                 result = -EINTR;
316                 goto error;
317         }
318
319         SRpnt->sr_data_direction = data_direction;
320         scsi_wait_req(SRpnt, cmd, buf, needed, timeout, retries);
321
322         /* 
323          * If there was an error condition, pass the info back to the user. 
324          */
325
326         result = SRpnt->sr_result;
327
328         if (SRpnt->sr_result) {
329                 int sb_len = sizeof(SRpnt->sr_sense_buffer);
330
331                 sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
332                 if (copy_to_user(cmd_in, SRpnt->sr_sense_buffer, sb_len))
333                         result = -EFAULT;
334         } else {
335                 if (copy_to_user(cmd_in, buf, outlen))
336                         result = -EFAULT;
337         }       
338
339         SDpnt = SRpnt->sr_device;
340         scsi_release_request(SRpnt);
341         SRpnt = NULL;
342
343 error:
344         if (buf)
345                 scsi_free(buf, buf_needed);
346
347
348         return result;
349 #else
350         {
351                 int i;
352                 printk("scsi_ioctl : device %d.  command = ", dev->id);
353                 for (i = 0; i < cmdlen; ++i)
354                         printk("%02x ", cmd[i]);
355                 printk("\nbuffer =");
356                 for (i = 0; i < 20; ++i)
357                         printk("%02x ", buf[i]);
358                 printk("\n");
359                 printk("inlen = %d, outlen = %d, cmdlen = %d\n",
360                        inlen, outlen, cmdlen);
361                 printk("buffer = %d, cmd_in = %d\n", buffer, cmd_in);
362         }
363         return 0;
364 #endif
365 }
366
367 /*
368  * The scsi_ioctl_get_pci() function places into arg the value
369  * pci_dev::slot_name (8 characters) for the PCI device (if any).
370  * Returns: 0 on success
371  *          -ENXIO if there isn't a PCI device pointer
372  *                 (could be because the SCSI driver hasn't been
373  *                  updated yet, or because it isn't a SCSI
374  *                  device)
375  *          any copy_to_user() error on failure there
376  */
377 static int
378 scsi_ioctl_get_pci(Scsi_Device * dev, void *arg)
379 {
380
381         if (!dev->host->pci_dev)
382                 return -ENXIO;
383         if(copy_to_user(arg, dev->host->pci_dev->slot_name,
384                             sizeof(dev->host->pci_dev->slot_name)))
385                 return -EFAULT;
386         return 0;
387 }
388
389
390 /*
391  * the scsi_ioctl() function differs from most ioctls in that it does
392  * not take a major/minor number as the dev field.  Rather, it takes
393  * a pointer to a scsi_devices[] element, a structure. 
394  */
395 int scsi_ioctl(Scsi_Device * dev, int cmd, void *arg)
396 {
397         char scsi_cmd[MAX_COMMAND_SIZE];
398         char cmd_byte1;
399
400         /* No idea how this happens.... */
401         if (!dev)
402                 return -ENXIO;
403
404         /*
405          * If we are in the middle of error recovery, don't let anyone
406          * else try and use this device.  Also, if error recovery fails, it
407          * may try and take the device offline, in which case all further
408          * access to the device is prohibited.
409          */
410         if (!scsi_block_when_processing_errors(dev)) {
411                 return -ENODEV;
412         }
413         cmd_byte1 = (dev->scsi_level <= SCSI_2) ? (dev->lun << 5) : 0;
414
415         switch (cmd) {
416         case SCSI_IOCTL_GET_IDLUN:
417                 if (verify_area(VERIFY_WRITE, arg, sizeof(Scsi_Idlun)))
418                         return -EFAULT;
419
420                 __put_user((dev->id & 0xff)
421                          + ((dev->lun & 0xff) << 8)
422                          + ((dev->channel & 0xff) << 16)
423                          + ((dev->host->host_no & 0xff) << 24),
424                          &((Scsi_Idlun *) arg)->dev_id);
425                 __put_user(dev->host->unique_id, &((Scsi_Idlun *) arg)->host_unique_id);
426                 return 0;
427         case SCSI_IOCTL_GET_BUS_NUMBER:
428                 return put_user(dev->host->host_no, (int *) arg);
429         case SCSI_IOCTL_TAGGED_ENABLE:
430                 if (!capable(CAP_SYS_ADMIN))
431                         return -EACCES;
432                 if (!dev->tagged_supported)
433                         return -EINVAL;
434                 dev->tagged_queue = 1;
435                 dev->current_tag = 1;
436                 return 0;
437         case SCSI_IOCTL_TAGGED_DISABLE:
438                 if (!capable(CAP_SYS_ADMIN))
439                         return -EACCES;
440                 if (!dev->tagged_supported)
441                         return -EINVAL;
442                 dev->tagged_queue = 0;
443                 dev->current_tag = 0;
444                 return 0;
445         case SCSI_IOCTL_PROBE_HOST:
446                 return ioctl_probe(dev->host, arg);
447         case SCSI_IOCTL_SEND_COMMAND:
448                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
449                         return -EACCES;
450                 return scsi_ioctl_send_command((Scsi_Device *) dev,
451                                              (Scsi_Ioctl_Command *) arg);
452         case SCSI_IOCTL_DOORLOCK:
453                 if (!dev->removable || !dev->lockable)
454                         return 0;
455                 scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
456                 scsi_cmd[1] = cmd_byte1;
457                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
458                 scsi_cmd[4] = SCSI_REMOVAL_PREVENT;
459                 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
460                                    IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
461                 break;
462         case SCSI_IOCTL_DOORUNLOCK:
463                 if (!dev->removable || !dev->lockable)
464                         return 0;
465                 scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
466                 scsi_cmd[1] = cmd_byte1;
467                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
468                 scsi_cmd[4] = SCSI_REMOVAL_ALLOW;
469                 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
470                                    IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
471         case SCSI_IOCTL_TEST_UNIT_READY:
472                 scsi_cmd[0] = TEST_UNIT_READY;
473                 scsi_cmd[1] = cmd_byte1;
474                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
475                 scsi_cmd[4] = 0;
476                 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
477                                    IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
478                 break;
479         case SCSI_IOCTL_START_UNIT:
480                 scsi_cmd[0] = START_STOP;
481                 scsi_cmd[1] = cmd_byte1;
482                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
483                 scsi_cmd[4] = 1;
484                 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
485                                      START_STOP_TIMEOUT, NORMAL_RETRIES);
486                 break;
487         case SCSI_IOCTL_STOP_UNIT:
488                 scsi_cmd[0] = START_STOP;
489                 scsi_cmd[1] = cmd_byte1;
490                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
491                 scsi_cmd[4] = 0;
492                 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
493                                      START_STOP_TIMEOUT, NORMAL_RETRIES);
494                 break;
495         case SCSI_IOCTL_GET_PCI:
496                 return scsi_ioctl_get_pci(dev, arg);
497                 break;
498         default:
499                 if (dev->host->hostt->ioctl)
500                         return dev->host->hostt->ioctl(dev, cmd, arg);
501                 return -EINVAL;
502         }
503         return -EINVAL;
504 }
505
506 /*
507  * Just like scsi_ioctl, only callable from kernel space with no 
508  * fs segment fiddling.
509  */
510
511 int kernel_scsi_ioctl(Scsi_Device * dev, int cmd, void *arg)
512 {
513         mm_segment_t oldfs;
514         int tmp;
515         oldfs = get_fs();
516         set_fs(get_ds());
517         tmp = scsi_ioctl(dev, cmd, arg);
518         set_fs(oldfs);
519         return tmp;
520 }
521
522 /*
523  * Overrides for Emacs so that we almost follow Linus's tabbing style.
524  * Emacs will notice this stuff at the end of the file and automatically
525  * adjust the settings for this buffer only.  This must remain at the end
526  * of the file.
527  * ---------------------------------------------------------------------------
528  * Local variables:
529  * c-indent-level: 4
530  * c-brace-imaginary-offset: 0
531  * c-brace-offset: -4
532  * c-argdecl-indent: 4
533  * c-label-offset: -4
534  * c-continued-statement-offset: 4
535  * c-continued-brace-offset: 0
536  * indent-tabs-mode: nil
537  * tab-width: 8
538  * End:
539  */