root/drivers/scsi/scsi_ioctl.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ioctl_probe
  2. scsi_ioctl_done
  3. ioctl_internal_command
  4. ioctl_command
  5. scsi_ioctl
  6. kernel_scsi_ioctl

   1 /*
   2  * Don't import our own symbols, as this would severely mess up our
   3  * symbol tables.
   4  */
   5 #define _SCSI_SYMS_VER_
   6 #define __NO_VERSION__
   7 #include <linux/module.h>
   8 
   9 #include <asm/io.h>
  10 #include <asm/segment.h>
  11 #include <asm/system.h>
  12 
  13 #include <linux/errno.h>
  14 #include <linux/kernel.h>
  15 #include <linux/sched.h>
  16 #include <linux/mm.h>
  17 #include <linux/string.h>
  18 
  19 #include <linux/blk.h>
  20 #include "scsi.h"
  21 #include "hosts.h"
  22 #include "scsi_ioctl.h"
  23 
  24 #define MAX_RETRIES 5   
  25 #define MAX_TIMEOUT 900
  26 #define MAX_BUF 4096
  27 
  28 #define max(a,b) (((a) > (b)) ? (a) : (b))
  29 
  30 /*
  31  * If we are told to probe a host, we will return 0 if  the host is not
  32  * present, 1 if the host is present, and will return an identifying
  33  * string at *arg, if arg is non null, filling to the length stored at
  34  * (int *) arg
  35  */
  36 
  37 static int ioctl_probe(struct Scsi_Host * host, void *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39     int temp, result;
  40     unsigned int len,slen;
  41     const char * string;
  42     
  43     if ((temp = host->hostt->present) && buffer) {
  44         result = verify_area(VERIFY_READ, buffer, sizeof(long));
  45         if (result) return result;
  46 
  47         len = get_user ((unsigned int *) buffer);
  48         if(host->hostt->info)
  49             string = host->hostt->info(host);
  50         else 
  51             string = host->hostt->name;
  52         if(string) {
  53             slen = strlen(string);
  54             if (len > slen)
  55                 len = slen + 1;
  56             result = verify_area(VERIFY_WRITE, buffer, len);
  57             if (result) return result;
  58 
  59             memcpy_tofs (buffer, string, len);
  60         }
  61     }
  62     return temp;
  63 }
  64 
  65 /*
  66  * 
  67  * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
  68  * The MAX_TIMEOUT and MAX_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 no more than MAX_BUF data bytes will be transfered.  Since
  77  * SCSI block device size is 512 bytes, I figured 1K was good.
  78  * but (WDE) changed it to 8192 to handle large bad track buffers.
  79  * ERY: I changed this to a dynamic allocation using scsi_malloc - we were
  80  * getting a kernel stack overflow which was crashing the system when we
  81  * were using 8192 bytes.
  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 void scsi_ioctl_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94     struct request * req;
  95     
  96     req = &SCpnt->request;
  97     req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
  98     
  99     if (req->sem != NULL) {
 100         up(req->sem);
 101     }
 102 }   
 103 
 104 static int ioctl_internal_command(Scsi_Device *dev, char * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106     int result;
 107     Scsi_Cmnd * SCpnt;
 108     
 109     SCpnt = allocate_device(NULL, dev, 1);
 110     {
 111         struct semaphore sem = MUTEX_LOCKED;
 112         SCpnt->request.sem = &sem;
 113         scsi_do_cmd(SCpnt,  cmd, NULL,  0,
 114                     scsi_ioctl_done,  MAX_TIMEOUT,
 115                     MAX_RETRIES);
 116         down(&sem);
 117     }
 118     
 119     if(driver_byte(SCpnt->result) != 0)
 120         switch(SCpnt->sense_buffer[2] & 0xf) {
 121         case ILLEGAL_REQUEST:
 122             if(cmd[0] == ALLOW_MEDIUM_REMOVAL) dev->lockable = 0;
 123             else printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
 124             break;
 125         case NOT_READY: /* This happens if there is no disc in drive */
 126             if(dev->removable){
 127                 printk(KERN_INFO "Device not ready.  Make sure there is a disc in the drive.\n");
 128                 break;
 129             };
 130         case UNIT_ATTENTION:
 131             if (dev->removable){
 132                 dev->changed = 1;
 133                 SCpnt->result = 0; /* This is no longer considered an error */
 134                 printk(KERN_INFO "Disc change detected.\n");
 135                 break;
 136             };
 137         default: /* Fall through for non-removable media */
 138             printk("SCSI error: host %d id %d lun %d return code = %x\n",
 139                    dev->host->host_no,
 140                    dev->id,
 141                    dev->lun,
 142                    SCpnt->result);
 143             printk("\tSense class %x, sense error %x, extended sense %x\n",
 144                    sense_class(SCpnt->sense_buffer[0]),
 145                    sense_error(SCpnt->sense_buffer[0]),
 146                    SCpnt->sense_buffer[2] & 0xf);
 147             
 148         };
 149     
 150     result = SCpnt->result;
 151     SCpnt->request.rq_status = RQ_INACTIVE;
 152 
 153     if(SCpnt->device->scsi_request_fn)
 154         (*SCpnt->device->scsi_request_fn)();
 155 
 156     wake_up(&SCpnt->device->device_wait);
 157     return result;
 158 }
 159 
 160 /*
 161  * This interface is depreciated - users should use the scsi generics
 162  * interface instead, as this is a more flexible approach to performing
 163  * generic SCSI commands on a device.
 164  */
 165 static int ioctl_command(Scsi_Device *dev, void *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167     char * buf;
 168     char cmd[12];
 169     char * cmd_in;
 170     Scsi_Cmnd * SCpnt;
 171     unsigned char opcode;
 172     int inlen, outlen, cmdlen;
 173     int needed, buf_needed;
 174     int result;
 175     
 176     if (!buffer)
 177         return -EINVAL;
 178     
 179 
 180     /*
 181      * Verify that we can read at least this much.
 182      */
 183     result = verify_area(VERIFY_READ, buffer, 2*sizeof(long) + 1);
 184     if (result) return result;
 185 
 186     /*
 187      * The structure that we are passed should look like:
 188      *
 189      * struct sdata{
 190      *  int inlen;
 191      *  int outlen;
 192      *  char cmd[];  # However many bytes are used for cmd.
 193      *  char data[];
 194      */
 195     inlen = get_user((unsigned int *) buffer);
 196     outlen = get_user( ((unsigned int *) buffer) + 1);
 197     
 198     /*
 199      * We do not transfer more than MAX_BUF with this interface.
 200      * If the user needs to transfer more data than this, they
 201      * should use scsi_generics instead.
 202      */
 203     if( inlen > MAX_BUF ) inlen = MAX_BUF;
 204     if( outlen > MAX_BUF ) outlen = MAX_BUF;
 205 
 206     cmd_in = (char *) ( ((int *)buffer) + 2);
 207     opcode = get_user(cmd_in); 
 208     
 209     needed = buf_needed = (inlen > outlen ? inlen : outlen);
 210     if(buf_needed){
 211         buf_needed = (buf_needed + 511) & ~511;
 212         if (buf_needed > MAX_BUF) buf_needed = MAX_BUF;
 213         buf = (char *) scsi_malloc(buf_needed);
 214         if (!buf) return -ENOMEM;
 215         memset(buf, 0, buf_needed);
 216     } else
 217         buf = NULL;
 218     
 219     /*
 220      * Obtain the command from the user's address space.
 221      */
 222     cmdlen = COMMAND_SIZE(opcode);
 223 
 224     result = verify_area(VERIFY_READ, cmd_in, 
 225                          cmdlen + inlen > MAX_BUF ? MAX_BUF : inlen);
 226     if (result) return result;
 227 
 228     memcpy_fromfs ((void *) cmd,  cmd_in,  cmdlen);
 229     
 230     /*
 231      * Obtain the data to be sent to the device (if any).
 232      */
 233     memcpy_fromfs ((void *) buf,  
 234                    (void *) (cmd_in + cmdlen), 
 235                    inlen);
 236     
 237     /*
 238      * Set the lun field to the correct value.
 239      */
 240     cmd[1] = ( cmd[1] & 0x1f ) | (dev->lun << 5);
 241     
 242 #ifndef DEBUG_NO_CMD
 243     
 244     SCpnt = allocate_device(NULL, dev, 1);
 245 
 246     {
 247         struct semaphore sem = MUTEX_LOCKED;
 248         SCpnt->request.sem = &sem;
 249         scsi_do_cmd(SCpnt,  cmd,  buf, needed,  scsi_ioctl_done,  MAX_TIMEOUT, 
 250                     MAX_RETRIES);
 251         down(&sem);
 252     }
 253     
 254     /* 
 255      * If there was an error condition, pass the info back to the user. 
 256      */
 257     if(SCpnt->result) {
 258         result = verify_area(VERIFY_WRITE, 
 259                              cmd_in, 
 260                              sizeof(SCpnt->sense_buffer));
 261         if (result) return result;
 262         memcpy_tofs((void *) cmd_in,  
 263                     SCpnt->sense_buffer, 
 264                     sizeof(SCpnt->sense_buffer));
 265     } else {
 266         result = verify_area(VERIFY_WRITE, cmd_in, outlen);
 267         if (result) return result;
 268         memcpy_tofs ((void *) cmd_in,  buf,  outlen);
 269     }
 270     result = SCpnt->result;
 271 
 272     SCpnt->request.rq_status = RQ_INACTIVE;
 273 
 274     if (buf) scsi_free(buf, buf_needed);
 275     
 276     if(SCpnt->device->scsi_request_fn)
 277         (*SCpnt->device->scsi_request_fn)();
 278     
 279     wake_up(&SCpnt->device->device_wait);
 280     return result;
 281 #else
 282     {
 283         int i;
 284         printk("scsi_ioctl : device %d.  command = ", dev->id);
 285         for (i = 0; i < 12; ++i)
 286             printk("%02x ", cmd[i]);
 287         printk("\nbuffer =");
 288         for (i = 0; i < 20; ++i)
 289             printk("%02x ", buf[i]);
 290         printk("\n");
 291         printk("inlen = %d, outlen = %d, cmdlen = %d\n",
 292                inlen, outlen, cmdlen);
 293         printk("buffer = %d, cmd_in = %d\n", buffer, cmd_in);
 294     }
 295     return 0;
 296 #endif
 297 }
 298 
 299 /*
 300  * the scsi_ioctl() function differs from most ioctls in that it does
 301  * not take a major/minor number as the dev filed.  Rather, it takes
 302  * a pointer to a scsi_devices[] element, a structure. 
 303  */
 304 int scsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 305 {
 306     int result;
 307     char scsi_cmd[12];
 308     
 309     /* No idea how this happens.... */
 310     if (!dev) return -ENXIO;
 311     
 312     switch (cmd) {
 313     case SCSI_IOCTL_GET_IDLUN:
 314         result = verify_area(VERIFY_WRITE, (void *) arg, 2*sizeof(long));
 315         if (result) return result;
 316 
 317         put_user(dev->id 
 318                  + (dev->lun << 8) 
 319                  + (dev->channel << 16)
 320                  + ((dev->host->hostt->proc_dir->low_ino & 0xff) << 24),
 321                     (unsigned long *) arg);
 322         put_user( dev->host->unique_id, (unsigned long *) arg+1);
 323         return 0;
 324     case SCSI_IOCTL_TAGGED_ENABLE:
 325         if(!suser())  return -EACCES;
 326         if(!dev->tagged_supported) return -EINVAL;
 327         dev->tagged_queue = 1;
 328         dev->current_tag = 1;
 329         break;
 330     case SCSI_IOCTL_TAGGED_DISABLE:
 331         if(!suser())  return -EACCES;
 332         if(!dev->tagged_supported) return -EINVAL;
 333         dev->tagged_queue = 0;
 334         dev->current_tag = 0;
 335         break;
 336     case SCSI_IOCTL_PROBE_HOST:
 337         return ioctl_probe(dev->host, arg);
 338     case SCSI_IOCTL_SEND_COMMAND:
 339         if(!suser())  return -EACCES;
 340         return ioctl_command((Scsi_Device *) dev, arg);
 341     case SCSI_IOCTL_DOORLOCK:
 342         if (!dev->removable || !dev->lockable) return 0;
 343         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
 344         scsi_cmd[1] = dev->lun << 5;
 345         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 346         scsi_cmd[4] = SCSI_REMOVAL_PREVENT;
 347         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 348         break;
 349     case SCSI_IOCTL_DOORUNLOCK:
 350         if (!dev->removable || !dev->lockable) return 0;
 351         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
 352         scsi_cmd[1] = dev->lun << 5;
 353         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 354         scsi_cmd[4] = SCSI_REMOVAL_ALLOW;
 355         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 356     case SCSI_IOCTL_TEST_UNIT_READY:
 357         scsi_cmd[0] = TEST_UNIT_READY;
 358         scsi_cmd[1] = dev->lun << 5;
 359         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 360         scsi_cmd[4] = 0;
 361         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 362         break;
 363     default :           
 364         return -EINVAL;
 365     }
 366     return -EINVAL;
 367 }
 368 
 369 /*
 370  * Just like scsi_ioctl, only callable from kernel space with no 
 371  * fs segment fiddling.
 372  */
 373 
 374 int kernel_scsi_ioctl (Scsi_Device *dev, int cmd, void *arg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 375     unsigned long oldfs;
 376     int tmp;
 377     oldfs = get_fs();
 378     set_fs(get_ds());
 379     tmp = scsi_ioctl (dev, cmd, arg);
 380     set_fs(oldfs);
 381     return tmp;
 382 }
 383 
 384 /*
 385  * Overrides for Emacs so that we almost follow Linus's tabbing style.
 386  * Emacs will notice this stuff at the end of the file and automatically
 387  * adjust the settings for this buffer only.  This must remain at the end
 388  * of the file.
 389  * ---------------------------------------------------------------------------
 390  * Local variables:
 391  * c-indent-level: 4
 392  * c-brace-imaginary-offset: 0
 393  * c-brace-offset: -4
 394  * c-argdecl-indent: 4
 395  * c-label-offset: -4
 396  * c-continued-statement-offset: 4
 397  * c-continued-brace-offset: 0
 398  * indent-tabs-mode: nil
 399  * tab-width: 8
 400  * End:
 401  */

/* [previous][next][first][last][top][bottom][index][help] */