upgrade to linux 2.6.9-1.11_FC2
[linux-2.6.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 #include <linux/blkdev.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>
15 #include <asm/uaccess.h>
16
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_device.h>
19 #include <scsi/scsi_eh.h>
20 #include <scsi/scsi_host.h>
21 #include <scsi/scsi_ioctl.h>
22 #include <scsi/scsi_request.h>
23 #include <scsi/sg.h>
24
25 #include "scsi_logging.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 __user *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 __user *) 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.
82  * 
83  * This size *does not* include the initial lengths that were passed.
84  * 
85  * The SCSI command is read from the memory location immediately after the
86  * length words, and the input data is right after the command.  The SCSI
87  * routines know the command size based on the opcode decode.  
88  * 
89  * The output area is then filled in starting from the command byte. 
90  */
91
92 static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
93                                   int timeout, int retries)
94 {
95         struct scsi_request *sreq;
96         int result;
97
98         SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd));
99
100         sreq = scsi_allocate_request(sdev, GFP_KERNEL);
101         if (!sreq) {
102                 printk("SCSI internal ioctl failed, no memory\n");
103                 return -ENOMEM;
104         }
105
106         sreq->sr_data_direction = DMA_NONE;
107         scsi_wait_req(sreq, cmd, NULL, 0, timeout, retries);
108
109         SCSI_LOG_IOCTL(2, printk("Ioctl returned  0x%x\n", sreq->sr_result));
110
111         if (driver_byte(sreq->sr_result)) {
112                 switch (sreq->sr_sense_buffer[2] & 0xf) {
113                 case ILLEGAL_REQUEST:
114                         if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
115                                 sdev->lockable = 0;
116                         else
117                                 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
118                         break;
119                 case NOT_READY: /* This happens if there is no disc in drive */
120                         if (sdev->removable && (cmd[0] != TEST_UNIT_READY)) {
121                                 printk(KERN_INFO "Device not ready.  Make sure there is a disc in the drive.\n");
122                                 break;
123                         }
124                 case UNIT_ATTENTION:
125                         if (sdev->removable) {
126                                 sdev->changed = 1;
127                                 sreq->sr_result = 0;    /* This is no longer considered an error */
128                                 break;
129                         }
130                 default:        /* Fall through for non-removable media */
131                         printk("SCSI error: host %d id %d lun %d return code = %x\n",
132                                sdev->host->host_no,
133                                sdev->id,
134                                sdev->lun,
135                                sreq->sr_result);
136                         printk("\tSense class %x, sense error %x, extended sense %x\n",
137                                sense_class(sreq->sr_sense_buffer[0]),
138                                sense_error(sreq->sr_sense_buffer[0]),
139                                sreq->sr_sense_buffer[2] & 0xf);
140
141                 }
142         }
143
144         result = sreq->sr_result;
145         SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
146         scsi_release_request(sreq);
147         return result;
148 }
149
150 int scsi_set_medium_removal(struct scsi_device *sdev, char state)
151 {
152         char scsi_cmd[MAX_COMMAND_SIZE];
153         int ret;
154
155         if (!sdev->removable || !sdev->lockable)
156                return 0;
157
158         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
159         scsi_cmd[1] = 0;
160         scsi_cmd[2] = 0;
161         scsi_cmd[3] = 0;
162         scsi_cmd[4] = state;
163         scsi_cmd[5] = 0;
164
165         ret = ioctl_internal_command(sdev, scsi_cmd,
166                         IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
167         if (ret == 0)
168                 sdev->locked = (state == SCSI_REMOVAL_PREVENT);
169         return ret;
170 }
171
172 /*
173  * This interface is deprecated - users should use the scsi generic (sg)
174  * interface instead, as this is a more flexible approach to performing
175  * generic SCSI commands on a device.
176  *
177  * The structure that we are passed should look like:
178  *
179  * struct sdata {
180  *  unsigned int inlen;      [i] Length of data to be written to device 
181  *  unsigned int outlen;     [i] Length of data to be read from device 
182  *  unsigned char cmd[x];    [i] SCSI command (6 <= x <= 12).
183  *                           [o] Data read from device starts here.
184  *                           [o] On error, sense buffer starts here.
185  *  unsigned char wdata[y];  [i] Data written to device starts here.
186  * };
187  * Notes:
188  *   -  The SCSI command length is determined by examining the 1st byte
189  *      of the given command. There is no way to override this.
190  *   -  Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
191  *   -  The length (x + y) must be at least OMAX_SB_LEN bytes long to
192  *      accommodate the sense buffer when an error occurs.
193  *      The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
194  *      old code will not be surprised.
195  *   -  If a Unix error occurs (e.g. ENOMEM) then the user will receive
196  *      a negative return and the Unix error code in 'errno'. 
197  *      If the SCSI command succeeds then 0 is returned.
198  *      Positive numbers returned are the compacted SCSI error codes (4 
199  *      bytes in one int) where the lowest byte is the SCSI status.
200  *      See the drivers/scsi/scsi.h file for more information on this.
201  *
202  */
203 #define OMAX_SB_LEN 16          /* Old sense buffer length */
204
205 int scsi_ioctl_send_command(struct scsi_device *sdev,
206                             struct scsi_ioctl_command __user *sic)
207 {
208         char *buf;
209         unsigned char cmd[MAX_COMMAND_SIZE];
210         char __user *cmd_in;
211         struct scsi_request *sreq;
212         unsigned char opcode;
213         unsigned int inlen, outlen, cmdlen;
214         unsigned int needed, buf_needed;
215         int timeout, retries, result;
216         int data_direction, gfp_mask = GFP_KERNEL;
217
218         if (!sic)
219                 return -EINVAL;
220
221         if (sdev->host->unchecked_isa_dma)
222                 gfp_mask |= GFP_DMA;
223
224         /*
225          * Verify that we can read at least this much.
226          */
227         if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
228                 return -EFAULT;
229
230         if(__get_user(inlen, &sic->inlen))
231                 return -EFAULT;
232                 
233         if(__get_user(outlen, &sic->outlen))
234                 return -EFAULT;
235
236         /*
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.
240          */
241         if (inlen > MAX_BUF)
242                 return -EINVAL;
243         if (outlen > MAX_BUF)
244                 return -EINVAL;
245
246         cmd_in = sic->data;
247         if(get_user(opcode, cmd_in))
248                 return -EFAULT;
249
250         needed = buf_needed = (inlen > outlen ? inlen : outlen);
251         if (buf_needed) {
252                 buf_needed = (buf_needed + 511) & ~511;
253                 if (buf_needed > MAX_BUF)
254                         buf_needed = MAX_BUF;
255                 buf = kmalloc(buf_needed, gfp_mask);
256                 if (!buf)
257                         return -ENOMEM;
258                 memset(buf, 0, buf_needed);
259                 if (inlen == 0) {
260                         data_direction = DMA_FROM_DEVICE;
261                 } else if (outlen == 0 ) {
262                         data_direction = DMA_TO_DEVICE;
263                 } else {
264                         /*
265                          * Can this ever happen?
266                          */
267                         data_direction = DMA_BIDIRECTIONAL;
268                 }
269
270         } else {
271                 buf = NULL;
272                 data_direction = DMA_NONE;
273         }
274
275         /*
276          * Obtain the command from the user's address space.
277          */
278         cmdlen = COMMAND_SIZE(opcode);
279         
280         result = -EFAULT;
281
282         if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
283                 goto error;
284
285         if(__copy_from_user(cmd, cmd_in, cmdlen))
286                 goto error;
287
288         /*
289          * Obtain the data to be sent to the device (if any).
290          */
291
292         if(copy_from_user(buf, cmd_in + cmdlen, inlen))
293                 goto error;
294
295         switch (opcode) {
296         case SEND_DIAGNOSTIC:
297         case FORMAT_UNIT:
298                 timeout = FORMAT_UNIT_TIMEOUT;
299                 retries = 1;
300                 break;
301         case START_STOP:
302                 timeout = START_STOP_TIMEOUT;
303                 retries = NORMAL_RETRIES;
304                 break;
305         case MOVE_MEDIUM:
306                 timeout = MOVE_MEDIUM_TIMEOUT;
307                 retries = NORMAL_RETRIES;
308                 break;
309         case READ_ELEMENT_STATUS:
310                 timeout = READ_ELEMENT_STATUS_TIMEOUT;
311                 retries = NORMAL_RETRIES;
312                 break;
313         case READ_DEFECT_DATA:
314                 timeout = READ_DEFECT_DATA_TIMEOUT;
315                 retries = 1;
316                 break;
317         default:
318                 timeout = IOCTL_NORMAL_TIMEOUT;
319                 retries = NORMAL_RETRIES;
320                 break;
321         }
322
323         sreq = scsi_allocate_request(sdev, GFP_KERNEL);
324         if (!sreq) {
325                 result = -EINTR;
326                 goto error;
327         }
328
329         sreq->sr_data_direction = data_direction;
330         scsi_wait_req(sreq, cmd, buf, needed, timeout, retries);
331
332         /* 
333          * If there was an error condition, pass the info back to the user. 
334          */
335         result = sreq->sr_result;
336         if (result) {
337                 int sb_len = sizeof(sreq->sr_sense_buffer);
338
339                 sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
340                 if (copy_to_user(cmd_in, sreq->sr_sense_buffer, sb_len))
341                         result = -EFAULT;
342         } else {
343                 if (copy_to_user(cmd_in, buf, outlen))
344                         result = -EFAULT;
345         }       
346
347         scsi_release_request(sreq);
348 error:
349         kfree(buf);
350         return result;
351 }
352
353 /*
354  * The scsi_ioctl_get_pci() function places into arg the value
355  * pci_dev::slot_name (8 characters) for the PCI device (if any).
356  * Returns: 0 on success
357  *          -ENXIO if there isn't a PCI device pointer
358  *                 (could be because the SCSI driver hasn't been
359  *                  updated yet, or because it isn't a SCSI
360  *                  device)
361  *          any copy_to_user() error on failure there
362  */
363 static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
364 {
365         struct device *dev = scsi_get_device(sdev->host);
366
367         if (!dev)
368                 return -ENXIO;
369         return copy_to_user(arg, dev->bus_id, sizeof(dev->bus_id))? -EFAULT: 0;
370 }
371
372
373 /*
374  * the scsi_ioctl() function differs from most ioctls in that it does
375  * not take a major/minor number as the dev field.  Rather, it takes
376  * a pointer to a scsi_devices[] element, a structure. 
377  */
378 int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
379 {
380         char scsi_cmd[MAX_COMMAND_SIZE];
381
382         /* No idea how this happens.... */
383         if (!sdev)
384                 return -ENXIO;
385
386         /*
387          * If we are in the middle of error recovery, don't let anyone
388          * else try and use this device.  Also, if error recovery fails, it
389          * may try and take the device offline, in which case all further
390          * access to the device is prohibited.
391          */
392         if (!scsi_block_when_processing_errors(sdev))
393                 return -ENODEV;
394
395         /* Check for deprecated ioctls ... all the ioctls which don't
396          * follow the new unique numbering scheme are deprecated */
397         switch (cmd) {
398         case SCSI_IOCTL_SEND_COMMAND:
399         case SCSI_IOCTL_TEST_UNIT_READY:
400         case SCSI_IOCTL_BENCHMARK_COMMAND:
401         case SCSI_IOCTL_SYNC:
402         case SCSI_IOCTL_START_UNIT:
403         case SCSI_IOCTL_STOP_UNIT:
404                 printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
405                 break;
406         default:
407                 break;
408         }
409
410         switch (cmd) {
411         case SCSI_IOCTL_GET_IDLUN:
412                 if (verify_area(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
413                         return -EFAULT;
414
415                 __put_user((sdev->id & 0xff)
416                          + ((sdev->lun & 0xff) << 8)
417                          + ((sdev->channel & 0xff) << 16)
418                          + ((sdev->host->host_no & 0xff) << 24),
419                          &((struct scsi_idlun __user *)arg)->dev_id);
420                 __put_user(sdev->host->unique_id,
421                          &((struct scsi_idlun __user *)arg)->host_unique_id);
422                 return 0;
423         case SCSI_IOCTL_GET_BUS_NUMBER:
424                 return put_user(sdev->host->host_no, (int __user *)arg);
425         case SCSI_IOCTL_PROBE_HOST:
426                 return ioctl_probe(sdev->host, arg);
427         case SCSI_IOCTL_SEND_COMMAND:
428                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
429                         return -EACCES;
430                 return scsi_ioctl_send_command(sdev, arg);
431         case SCSI_IOCTL_DOORLOCK:
432                 return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
433         case SCSI_IOCTL_DOORUNLOCK:
434                 return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
435         case SCSI_IOCTL_TEST_UNIT_READY:
436                 return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
437                                             NORMAL_RETRIES);
438         case SCSI_IOCTL_START_UNIT:
439                 scsi_cmd[0] = START_STOP;
440                 scsi_cmd[1] = 0;
441                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
442                 scsi_cmd[4] = 1;
443                 return ioctl_internal_command(sdev, scsi_cmd,
444                                      START_STOP_TIMEOUT, NORMAL_RETRIES);
445         case SCSI_IOCTL_STOP_UNIT:
446                 scsi_cmd[0] = START_STOP;
447                 scsi_cmd[1] = 0;
448                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
449                 scsi_cmd[4] = 0;
450                 return ioctl_internal_command(sdev, scsi_cmd,
451                                      START_STOP_TIMEOUT, NORMAL_RETRIES);
452         case SCSI_IOCTL_GET_PCI:
453                 return scsi_ioctl_get_pci(sdev, arg);
454         default:
455                 if (sdev->host->hostt->ioctl)
456                         return sdev->host->hostt->ioctl(sdev, cmd, arg);
457         }
458         return -EINVAL;
459 }
460
461 /*
462  * the scsi_nonblock_ioctl() function is designed for ioctls which may
463  * be executed even if the device is in recovery.
464  */
465 int scsi_nonblockable_ioctl(struct scsi_device *sdev, int cmd,
466                             void __user *arg, struct file *filp)
467 {
468         int val, result;
469
470         /* The first set of iocts may be executed even if we're doing
471          * error processing, as long as the device was opened
472          * non-blocking */
473         if (filp && filp->f_flags & O_NONBLOCK) {
474                 if (test_bit(SHOST_RECOVERY,
475                              &sdev->host->shost_state))
476                         return -ENODEV;
477         } else if (!scsi_block_when_processing_errors(sdev))
478                 return -ENODEV;
479
480         switch (cmd) {
481         case SG_SCSI_RESET:
482                 result = get_user(val, (int __user *)arg);
483                 if (result)
484                         return result;
485                 if (val == SG_SCSI_RESET_NOTHING)
486                         return 0;
487                 switch (val) {
488                 case SG_SCSI_RESET_DEVICE:
489                         val = SCSI_TRY_RESET_DEVICE;
490                         break;
491                 case SG_SCSI_RESET_BUS:
492                         val = SCSI_TRY_RESET_BUS;
493                         break;
494                 case SG_SCSI_RESET_HOST:
495                         val = SCSI_TRY_RESET_HOST;
496                         break;
497                 default:
498                         return -EINVAL;
499                 }
500                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
501                         return -EACCES;
502                 return (scsi_reset_provider(sdev, val) ==
503                         SUCCESS) ? 0 : -EIO;
504         }
505         return -ENODEV;
506 }
507 EXPORT_SYMBOL(scsi_nonblockable_ioctl);