Add ia64 patch.
[linux-flexiantxendom0-3.2.10.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 #include <linux/module.h>
8
9 #include <asm/io.h>
10 #include <asm/uaccess.h>
11 #include <asm/system.h>
12 #include <asm/page.h>
13
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>
20
21 #include <linux/blk.h>
22 #include "scsi.h"
23 #include "hosts.h"
24 #include <scsi/scsi_ioctl.h>
25
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! */
33
34 #define MAX_BUF PAGE_SIZE
35
36 /*
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
40  * (int *) arg
41  */
42
43 static int ioctl_probe(struct Scsi_Host *host, void *buffer)
44 {
45         unsigned int len, slen;
46         const char *string;
47         int temp = host->hostt->present;
48
49         if (temp && buffer) {
50                 if (get_user(len, (unsigned int *) buffer))
51                         return -EFAULT;
52
53                 if (host->hostt->info)
54                         string = host->hostt->info(host);
55                 else
56                         string = host->hostt->name;
57                 if (string) {
58                         slen = strlen(string);
59                         if (len > slen)
60                                 len = slen + 1;
61                         if (copy_to_user(buffer, string, len))
62                                 return -EFAULT;
63                 }
64         }
65         return temp;
66 }
67
68 /*
69
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.  
72  * 
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.
76  * 
77  * *(char *) ((int *) arg)[2] the actual command byte.   
78  * 
79  * Note that if more than MAX_BUF bytes are requested to be transferred,
80  * the ioctl will fail with error EINVAL.
81  * 
82  * This size *does not* include the initial lengths that were passed.
83  * 
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.  
87  * 
88  * The output area is then filled in starting from the command byte. 
89  */
90
91 static int ioctl_internal_command(Scsi_Device * dev, char *cmd,
92                                   int timeout, int retries)
93 {
94         int result;
95         Scsi_Request *SRpnt;
96         Scsi_Device *SDpnt;
97
98
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");
102                 return -ENOMEM;
103         }
104
105         SRpnt->sr_data_direction = SCSI_DATA_NONE;
106         scsi_wait_req(SRpnt, cmd, NULL, 0, timeout, retries);
107
108         SCSI_LOG_IOCTL(2, printk("Ioctl returned  0x%x\n", SRpnt->sr_result));
109
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)
114                                 dev->lockable = 0;
115                         else
116                                 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
117                         break;
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");
121                                 break;
122                         }
123                 case UNIT_ATTENTION:
124                         if (dev->removable) {
125                                 dev->changed = 1;
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"); */
129                                 break;
130                         }
131                 default:        /* Fall through for non-removable media */
132                         printk("SCSI error: host %d id %d lun %d return code = %x\n",
133                                dev->host->host_no,
134                                dev->id,
135                                dev->lun,
136                                SRpnt->sr_result);
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);
141
142                 }
143
144         result = SRpnt->sr_result;
145
146         SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
147         SDpnt = SRpnt->sr_device;
148         scsi_release_request(SRpnt);
149         SRpnt = NULL;
150
151         return result;
152 }
153
154 int scsi_set_medium_removal(Scsi_Device *dev, char state)
155 {
156         char scsi_cmd[MAX_COMMAND_SIZE];
157         int ret;
158
159         if (!dev->removable || !dev->lockable)
160                return 0;
161
162         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
163         scsi_cmd[1] = 0;
164         scsi_cmd[2] = 0;
165         scsi_cmd[3] = 0;
166         scsi_cmd[4] = state;
167         scsi_cmd[5] = 0;
168
169         ret = ioctl_internal_command(dev, scsi_cmd, IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
170
171         if (ret == 0)
172                 dev->locked = state == SCSI_REMOVAL_PREVENT;
173
174         return ret;
175 }
176
177 /*
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.
181  *
182  * The structure that we are passed should look like:
183  *
184  * struct sdata {
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.
191  * };
192  * Notes:
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.
206  *
207  */
208 #define OMAX_SB_LEN 16          /* Old sense buffer length */
209
210 int scsi_ioctl_send_command(Scsi_Device * dev, Scsi_Ioctl_Command * sic)
211 {
212         char *buf;
213         unsigned char cmd[MAX_COMMAND_SIZE];
214         char *cmd_in;
215         Scsi_Request *SRpnt;
216         Scsi_Device *SDpnt;
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;
222 #if __GNUC__ < 3
223         int foo;
224 #endif
225
226         if (!sic)
227                 return -EINVAL;
228
229         if (dev->host->unchecked_isa_dma)
230                 gfp_mask |= GFP_DMA;
231
232         /*
233          * Verify that we can read at least this much.
234          */
235         if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
236                 return -EFAULT;
237
238 #if __GNUC__ < 3
239         foo = __get_user(inlen, &sic->inlen);
240         if (foo)
241                 return -EFAULT;
242
243         foo = __get_user(outlen, &sic->outlen);
244         if (foo)
245                 return -EFAULT;
246 #else
247         if(__get_user(inlen, &sic->inlen))
248                 return -EFAULT;
249                 
250         if(__get_user(outlen, &sic->outlen))
251                 return -EFAULT;
252 #endif
253
254         /*
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.
258          */
259         if (inlen > MAX_BUF)
260                 return -EINVAL;
261         if (outlen > MAX_BUF)
262                 return -EINVAL;
263
264         cmd_in = sic->data;
265         if(get_user(opcode, cmd_in))
266                 return -EFAULT;
267
268         needed = buf_needed = (inlen > outlen ? inlen : outlen);
269         if (buf_needed) {
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);
274                 if (!buf)
275                         return -ENOMEM;
276                 memset(buf, 0, buf_needed);
277                 if( inlen == 0 ) {
278                         data_direction = SCSI_DATA_READ;
279                 } else if (outlen == 0 ) {
280                         data_direction = SCSI_DATA_WRITE;
281                 } else {
282                         /*
283                          * Can this ever happen?
284                          */
285                         data_direction = SCSI_DATA_UNKNOWN;
286                 }
287
288         } else {
289                 buf = NULL;
290                 data_direction = SCSI_DATA_NONE;
291         }
292
293         /*
294          * Obtain the command from the user's address space.
295          */
296         cmdlen = COMMAND_SIZE(opcode);
297         
298         result = -EFAULT;
299
300         if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
301                 goto error;
302
303         if(__copy_from_user(cmd, cmd_in, cmdlen))
304                 goto error;
305
306         /*
307          * Obtain the data to be sent to the device (if any).
308          */
309
310         if(copy_from_user(buf, cmd_in + cmdlen, inlen))
311                 goto error;
312
313         switch (opcode) {
314         case SEND_DIAGNOSTIC:
315         case FORMAT_UNIT:
316                 timeout = FORMAT_UNIT_TIMEOUT;
317                 retries = 1;
318                 break;
319         case START_STOP:
320                 timeout = START_STOP_TIMEOUT;
321                 retries = NORMAL_RETRIES;
322                 break;
323         case MOVE_MEDIUM:
324                 timeout = MOVE_MEDIUM_TIMEOUT;
325                 retries = NORMAL_RETRIES;
326                 break;
327         case READ_ELEMENT_STATUS:
328                 timeout = READ_ELEMENT_STATUS_TIMEOUT;
329                 retries = NORMAL_RETRIES;
330                 break;
331         case READ_DEFECT_DATA:
332                 timeout = READ_DEFECT_DATA_TIMEOUT;
333                 retries = 1;
334                 break;
335         default:
336                 timeout = IOCTL_NORMAL_TIMEOUT;
337                 retries = NORMAL_RETRIES;
338                 break;
339         }
340
341 #ifndef DEBUG_NO_CMD
342
343
344         SRpnt = scsi_allocate_request(dev);
345         if( SRpnt == NULL )
346         {
347                 result = -EINTR;
348                 goto error;
349         }
350
351         SRpnt->sr_data_direction = data_direction;
352         scsi_wait_req(SRpnt, cmd, buf, needed, timeout, retries);
353
354         /* 
355          * If there was an error condition, pass the info back to the user. 
356          */
357
358         result = SRpnt->sr_result;
359
360         if (SRpnt->sr_result) {
361                 int sb_len = sizeof(SRpnt->sr_sense_buffer);
362
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))
365                         result = -EFAULT;
366         } else {
367                 if (copy_to_user(cmd_in, buf, outlen))
368                         result = -EFAULT;
369         }       
370
371         SDpnt = SRpnt->sr_device;
372         scsi_release_request(SRpnt);
373         SRpnt = NULL;
374
375 error:
376         if (buf)
377                 kfree(buf);
378
379
380         return result;
381 #else
382         {
383                 int i;
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]);
390                 printk("\n");
391                 printk("inlen = %d, outlen = %d, cmdlen = %d\n",
392                        inlen, outlen, cmdlen);
393                 printk("buffer = %d, cmd_in = %d\n", buffer, cmd_in);
394         }
395         return 0;
396 #endif
397 }
398
399 /*
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
406  *                  device)
407  *          any copy_to_user() error on failure there
408  */
409 static int
410 scsi_ioctl_get_pci(Scsi_Device * sdev, void *arg)
411 {
412         struct device *dev = scsi_get_device(sdev->host);
413
414         if (!dev) return -ENXIO;
415         return copy_to_user(arg, dev->bus_id,
416                             sizeof(dev->bus_id));
417 }
418
419
420 /*
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. 
424  */
425 int scsi_ioctl(Scsi_Device * dev, int cmd, void *arg)
426 {
427         char scsi_cmd[MAX_COMMAND_SIZE];
428
429         /* No idea how this happens.... */
430         if (!dev)
431                 return -ENXIO;
432
433         /*
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.
438          */
439         if (!scsi_block_when_processing_errors(dev)) {
440                 return -ENODEV;
441         }
442
443         switch (cmd) {
444         case SCSI_IOCTL_GET_IDLUN:
445                 if (verify_area(VERIFY_WRITE, arg, sizeof(Scsi_Idlun)))
446                         return -EFAULT;
447
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);
454                 return 0;
455         case SCSI_IOCTL_GET_BUS_NUMBER:
456                 return put_user(dev->host->host_no, (int *) arg);
457         /*
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.
462          *
463          * Oct. 10, 2002 - Doug Ledford <dledford@redhat.com>
464          */
465         case SCSI_IOCTL_TAGGED_ENABLE:
466                 if (!capable(CAP_SYS_ADMIN))
467                         return -EACCES;
468                 if (!dev->tagged_supported)
469                         return -EINVAL;
470                 dev->tagged_queue = 1;
471                 dev->current_tag = 1;
472                 return 0;
473         case SCSI_IOCTL_TAGGED_DISABLE:
474                 if (!capable(CAP_SYS_ADMIN))
475                         return -EACCES;
476                 if (!dev->tagged_supported)
477                         return -EINVAL;
478                 dev->tagged_queue = 0;
479                 dev->current_tag = 0;
480                 return 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))
485                         return -EACCES;
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;
494                 scsi_cmd[1] = 0;
495                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
496                 scsi_cmd[4] = 0;
497                 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
498                                    IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
499                 break;
500         case SCSI_IOCTL_START_UNIT:
501                 scsi_cmd[0] = START_STOP;
502                 scsi_cmd[1] = 0;
503                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
504                 scsi_cmd[4] = 1;
505                 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
506                                      START_STOP_TIMEOUT, NORMAL_RETRIES);
507                 break;
508         case SCSI_IOCTL_STOP_UNIT:
509                 scsi_cmd[0] = START_STOP;
510                 scsi_cmd[1] = 0;
511                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
512                 scsi_cmd[4] = 0;
513                 return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
514                                      START_STOP_TIMEOUT, NORMAL_RETRIES);
515                 break;
516         case SCSI_IOCTL_GET_PCI:
517                 return scsi_ioctl_get_pci(dev, arg);
518                 break;
519         default:
520                 if (dev->host->hostt->ioctl)
521                         return dev->host->hostt->ioctl(dev, cmd, arg);
522                 return -EINVAL;
523         }
524         return -EINVAL;
525 }
526
527 /*
528  * Just like scsi_ioctl, only callable from kernel space with no 
529  * fs segment fiddling.
530  */
531
532 int kernel_scsi_ioctl(Scsi_Device * dev, int cmd, void *arg)
533 {
534         mm_segment_t oldfs;
535         int tmp;
536         oldfs = get_fs();
537         set_fs(get_ds());
538         tmp = scsi_ioctl(dev, cmd, arg);
539         set_fs(oldfs);
540         return tmp;
541 }