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