VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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
24 #include "scsi_logging.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 __user *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 __user *) 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(struct scsi_device *sdev, char *cmd,
92                                   int timeout, int retries)
93 {
94         struct scsi_request *sreq;
95         int result;
96
97         SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd));
98
99         sreq = scsi_allocate_request(sdev, GFP_KERNEL);
100         if (!sreq) {
101                 printk("SCSI internal ioctl failed, no memory\n");
102                 return -ENOMEM;
103         }
104
105         sreq->sr_data_direction = DMA_NONE;
106         scsi_wait_req(sreq, cmd, NULL, 0, timeout, retries);
107
108         SCSI_LOG_IOCTL(2, printk("Ioctl returned  0x%x\n", sreq->sr_result));
109
110         if (driver_byte(sreq->sr_result)) {
111                 switch (sreq->sr_sense_buffer[2] & 0xf) {
112                 case ILLEGAL_REQUEST:
113                         if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
114                                 sdev->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 (sdev->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 (sdev->removable) {
125                                 sdev->changed = 1;
126                                 sreq->sr_result = 0;    /* This is no longer considered an error */
127                                 break;
128                         }
129                 default:        /* Fall through for non-removable media */
130                         printk("SCSI error: host %d id %d lun %d return code = %x\n",
131                                sdev->host->host_no,
132                                sdev->id,
133                                sdev->lun,
134                                sreq->sr_result);
135                         printk("\tSense class %x, sense error %x, extended sense %x\n",
136                                sense_class(sreq->sr_sense_buffer[0]),
137                                sense_error(sreq->sr_sense_buffer[0]),
138                                sreq->sr_sense_buffer[2] & 0xf);
139
140                 }
141         }
142
143         result = sreq->sr_result;
144         SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
145         scsi_release_request(sreq);
146         return result;
147 }
148
149 int scsi_set_medium_removal(struct scsi_device *sdev, char state)
150 {
151         char scsi_cmd[MAX_COMMAND_SIZE];
152         int ret;
153
154         if (!sdev->removable || !sdev->lockable)
155                return 0;
156
157         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
158         scsi_cmd[1] = 0;
159         scsi_cmd[2] = 0;
160         scsi_cmd[3] = 0;
161         scsi_cmd[4] = state;
162         scsi_cmd[5] = 0;
163
164         ret = ioctl_internal_command(sdev, scsi_cmd,
165                         IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
166         if (ret == 0)
167                 sdev->locked = (state == SCSI_REMOVAL_PREVENT);
168         return ret;
169 }
170
171 /*
172  * This interface is deprecated - users should use the scsi generic (sg)
173  * interface instead, as this is a more flexible approach to performing
174  * generic SCSI commands on a device.
175  *
176  * The structure that we are passed should look like:
177  *
178  * struct sdata {
179  *  unsigned int inlen;      [i] Length of data to be written to device 
180  *  unsigned int outlen;     [i] Length of data to be read from device 
181  *  unsigned char cmd[x];    [i] SCSI command (6 <= x <= 12).
182  *                           [o] Data read from device starts here.
183  *                           [o] On error, sense buffer starts here.
184  *  unsigned char wdata[y];  [i] Data written to device starts here.
185  * };
186  * Notes:
187  *   -  The SCSI command length is determined by examining the 1st byte
188  *      of the given command. There is no way to override this.
189  *   -  Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
190  *   -  The length (x + y) must be at least OMAX_SB_LEN bytes long to
191  *      accommodate the sense buffer when an error occurs.
192  *      The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
193  *      old code will not be surprised.
194  *   -  If a Unix error occurs (e.g. ENOMEM) then the user will receive
195  *      a negative return and the Unix error code in 'errno'. 
196  *      If the SCSI command succeeds then 0 is returned.
197  *      Positive numbers returned are the compacted SCSI error codes (4 
198  *      bytes in one int) where the lowest byte is the SCSI status.
199  *      See the drivers/scsi/scsi.h file for more information on this.
200  *
201  */
202 #define OMAX_SB_LEN 16          /* Old sense buffer length */
203
204 int scsi_ioctl_send_command(struct scsi_device *sdev,
205                             struct scsi_ioctl_command __user *sic)
206 {
207         char *buf;
208         unsigned char cmd[MAX_COMMAND_SIZE];
209         char __user *cmd_in;
210         struct scsi_request *sreq;
211         unsigned char opcode;
212         unsigned int inlen, outlen, cmdlen;
213         unsigned int needed, buf_needed;
214         int timeout, retries, result;
215         int data_direction, gfp_mask = GFP_KERNEL;
216
217         if (!sic)
218                 return -EINVAL;
219
220         if (sdev->host->unchecked_isa_dma)
221                 gfp_mask |= GFP_DMA;
222
223         /*
224          * Verify that we can read at least this much.
225          */
226         if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
227                 return -EFAULT;
228
229         if(__get_user(inlen, &sic->inlen))
230                 return -EFAULT;
231                 
232         if(__get_user(outlen, &sic->outlen))
233                 return -EFAULT;
234
235         /*
236          * We do not transfer more than MAX_BUF with this interface.
237          * If the user needs to transfer more data than this, they
238          * should use scsi_generics (sg) instead.
239          */
240         if (inlen > MAX_BUF)
241                 return -EINVAL;
242         if (outlen > MAX_BUF)
243                 return -EINVAL;
244
245         cmd_in = sic->data;
246         if(get_user(opcode, cmd_in))
247                 return -EFAULT;
248
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                 buf = kmalloc(buf_needed, gfp_mask);
255                 if (!buf)
256                         return -ENOMEM;
257                 memset(buf, 0, buf_needed);
258                 if (inlen == 0) {
259                         data_direction = DMA_FROM_DEVICE;
260                 } else if (outlen == 0 ) {
261                         data_direction = DMA_TO_DEVICE;
262                 } else {
263                         /*
264                          * Can this ever happen?
265                          */
266                         data_direction = DMA_BIDIRECTIONAL;
267                 }
268
269         } else {
270                 buf = NULL;
271                 data_direction = DMA_NONE;
272         }
273
274         /*
275          * Obtain the command from the user's address space.
276          */
277         cmdlen = COMMAND_SIZE(opcode);
278         
279         result = -EFAULT;
280
281         if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
282                 goto error;
283
284         if(__copy_from_user(cmd, cmd_in, cmdlen))
285                 goto error;
286
287         /*
288          * Obtain the data to be sent to the device (if any).
289          */
290
291         if(copy_from_user(buf, cmd_in + cmdlen, inlen))
292                 goto error;
293
294         switch (opcode) {
295         case SEND_DIAGNOSTIC:
296         case FORMAT_UNIT:
297                 timeout = FORMAT_UNIT_TIMEOUT;
298                 retries = 1;
299                 break;
300         case START_STOP:
301                 timeout = START_STOP_TIMEOUT;
302                 retries = NORMAL_RETRIES;
303                 break;
304         case MOVE_MEDIUM:
305                 timeout = MOVE_MEDIUM_TIMEOUT;
306                 retries = NORMAL_RETRIES;
307                 break;
308         case READ_ELEMENT_STATUS:
309                 timeout = READ_ELEMENT_STATUS_TIMEOUT;
310                 retries = NORMAL_RETRIES;
311                 break;
312         case READ_DEFECT_DATA:
313                 timeout = READ_DEFECT_DATA_TIMEOUT;
314                 retries = 1;
315                 break;
316         default:
317                 timeout = IOCTL_NORMAL_TIMEOUT;
318                 retries = NORMAL_RETRIES;
319                 break;
320         }
321
322         sreq = scsi_allocate_request(sdev, GFP_KERNEL);
323         if (!sreq) {
324                 result = -EINTR;
325                 goto error;
326         }
327
328         sreq->sr_data_direction = data_direction;
329         scsi_wait_req(sreq, cmd, buf, needed, timeout, retries);
330
331         /* 
332          * If there was an error condition, pass the info back to the user. 
333          */
334         result = sreq->sr_result;
335         if (result) {
336                 int sb_len = sizeof(sreq->sr_sense_buffer);
337
338                 sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
339                 if (copy_to_user(cmd_in, sreq->sr_sense_buffer, sb_len))
340                         result = -EFAULT;
341         } else {
342                 if (copy_to_user(cmd_in, buf, outlen))
343                         result = -EFAULT;
344         }       
345
346         scsi_release_request(sreq);
347 error:
348         kfree(buf);
349         return result;
350 }
351
352 /*
353  * The scsi_ioctl_get_pci() function places into arg the value
354  * pci_dev::slot_name (8 characters) for the PCI device (if any).
355  * Returns: 0 on success
356  *          -ENXIO if there isn't a PCI device pointer
357  *                 (could be because the SCSI driver hasn't been
358  *                  updated yet, or because it isn't a SCSI
359  *                  device)
360  *          any copy_to_user() error on failure there
361  */
362 static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
363 {
364         struct device *dev = scsi_get_device(sdev->host);
365
366         if (!dev)
367                 return -ENXIO;
368         return copy_to_user(arg, dev->bus_id, sizeof(dev->bus_id))? -EFAULT: 0;
369 }
370
371
372 /*
373  * the scsi_ioctl() function differs from most ioctls in that it does
374  * not take a major/minor number as the dev field.  Rather, it takes
375  * a pointer to a scsi_devices[] element, a structure. 
376  */
377 int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
378 {
379         char scsi_cmd[MAX_COMMAND_SIZE];
380
381         /* No idea how this happens.... */
382         if (!sdev)
383                 return -ENXIO;
384
385         /*
386          * If we are in the middle of error recovery, don't let anyone
387          * else try and use this device.  Also, if error recovery fails, it
388          * may try and take the device offline, in which case all further
389          * access to the device is prohibited.
390          */
391         if (!scsi_block_when_processing_errors(sdev))
392                 return -ENODEV;
393
394         switch (cmd) {
395         case SCSI_IOCTL_GET_IDLUN:
396                 if (verify_area(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
397                         return -EFAULT;
398
399                 __put_user((sdev->id & 0xff)
400                          + ((sdev->lun & 0xff) << 8)
401                          + ((sdev->channel & 0xff) << 16)
402                          + ((sdev->host->host_no & 0xff) << 24),
403                          &((struct scsi_idlun __user *)arg)->dev_id);
404                 __put_user(sdev->host->unique_id,
405                          &((struct scsi_idlun __user *)arg)->host_unique_id);
406                 return 0;
407         case SCSI_IOCTL_GET_BUS_NUMBER:
408                 return put_user(sdev->host->host_no, (int __user *)arg);
409         case SCSI_IOCTL_PROBE_HOST:
410                 return ioctl_probe(sdev->host, arg);
411         case SCSI_IOCTL_SEND_COMMAND:
412                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
413                         return -EACCES;
414                 return scsi_ioctl_send_command(sdev, arg);
415         case SCSI_IOCTL_DOORLOCK:
416                 return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
417         case SCSI_IOCTL_DOORUNLOCK:
418                 return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
419         case SCSI_IOCTL_TEST_UNIT_READY:
420                 scsi_cmd[0] = TEST_UNIT_READY;
421                 scsi_cmd[1] = 0;
422                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
423                 scsi_cmd[4] = 0;
424                 return ioctl_internal_command(sdev, scsi_cmd,
425                                    IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
426         case SCSI_IOCTL_START_UNIT:
427                 scsi_cmd[0] = START_STOP;
428                 scsi_cmd[1] = 0;
429                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
430                 scsi_cmd[4] = 1;
431                 return ioctl_internal_command(sdev, scsi_cmd,
432                                      START_STOP_TIMEOUT, NORMAL_RETRIES);
433         case SCSI_IOCTL_STOP_UNIT:
434                 scsi_cmd[0] = START_STOP;
435                 scsi_cmd[1] = 0;
436                 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
437                 scsi_cmd[4] = 0;
438                 return ioctl_internal_command(sdev, scsi_cmd,
439                                      START_STOP_TIMEOUT, NORMAL_RETRIES);
440         case SCSI_IOCTL_GET_PCI:
441                 return scsi_ioctl_get_pci(sdev, arg);
442         default:
443                 if (sdev->host->hostt->ioctl)
444                         return sdev->host->hostt->ioctl(sdev, cmd, arg);
445         }
446         return -EINVAL;
447 }