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