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 #include <asm/io.h>
   2 #include <asm/segment.h>
   3 #include <asm/system.h>
   4 
   5 #include <linux/errno.h>
   6 #include <linux/kernel.h>
   7 #include <linux/sched.h>
   8 #include <linux/mm.h>
   9 #include <linux/string.h>
  10 
  11 #include "../block/blk.h"
  12 #include "scsi.h"
  13 #include "hosts.h"
  14 #include "scsi_ioctl.h"
  15 
  16 #define MAX_RETRIES 5   
  17 #define MAX_TIMEOUT 900
  18 #define MAX_BUF 4096
  19 
  20 #define max(a,b) (((a) > (b)) ? (a) : (b))
  21 
  22 /*
  23  * If we are told to probe a host, we will return 0 if  the host is not
  24  * present, 1 if the host is present, and will return an identifying
  25  * string at *arg, if arg is non null, filling to the length stored at
  26  * (int *) arg
  27  */
  28 
  29 static int ioctl_probe(struct Scsi_Host * host, void *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
  30 {
  31     int temp;
  32     unsigned int len,slen;
  33     const char * string;
  34     
  35     if ((temp = host->hostt->present) && buffer) {
  36         len = get_user ((unsigned int *) buffer);
  37         if(host->hostt->info)
  38             string = host->hostt->info(host);
  39         else 
  40             string = host->hostt->name;
  41         if(string) {
  42             slen = strlen(string);
  43             if (len > slen)
  44                 len = slen + 1;
  45             verify_area(VERIFY_WRITE, buffer, len);
  46             memcpy_tofs (buffer, string, len);
  47         }
  48     }
  49     return temp;
  50 }
  51 
  52 /*
  53  * 
  54  * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
  55  * The MAX_TIMEOUT and MAX_RETRIES  variables are used.  
  56  * 
  57  * dev is the SCSI device struct ptr, *(int *) arg is the length of the
  58  * input data, if any, not including the command string & counts, 
  59  * *((int *)arg + 1) is the output buffer size in bytes.
  60  * 
  61  * *(char *) ((int *) arg)[2] the actual command byte.   
  62  * 
  63  * Note that no more than MAX_BUF data bytes will be transfered.  Since
  64  * SCSI block device size is 512 bytes, I figured 1K was good.
  65  * but (WDE) changed it to 8192 to handle large bad track buffers.
  66  * ERY: I changed this to a dynamic allocation using scsi_malloc - we were
  67  * getting a kernel stack overflow which was crashing the system when we
  68  * were using 8192 bytes.
  69  * 
  70  * This size *does not* include the initial lengths that were passed.
  71  * 
  72  * The SCSI command is read from the memory location immediately after the
  73  * length words, and the input data is right after the command.  The SCSI
  74  * routines know the command size based on the opcode decode.  
  75  * 
  76  * The output area is then filled in starting from the command byte. 
  77  */
  78 
  79 static void scsi_ioctl_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81     struct request * req;
  82     
  83     req = &SCpnt->request;
  84     req->dev = 0xfffe; /* Busy, but indicate request done */
  85     
  86     if (req->sem != NULL) {
  87         up(req->sem);
  88     }
  89 }   
  90 
  91 static int ioctl_internal_command(Scsi_Device *dev, char * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
  92 {
  93     int result;
  94     Scsi_Cmnd * SCpnt;
  95     
  96     SCpnt = allocate_device(NULL, dev, 1);
  97     scsi_do_cmd(SCpnt,  cmd, NULL,  0,
  98                 scsi_ioctl_done,  MAX_TIMEOUT,
  99                 MAX_RETRIES);
 100     
 101     if (SCpnt->request.dev != 0xfffe){
 102         struct semaphore sem = MUTEX_LOCKED;
 103         SCpnt->request.sem = &sem;
 104         down(&sem);
 105         /* Hmm.. Have to ask about this one */
 106         while (SCpnt->request.dev != 0xfffe) schedule();
 107     };
 108     
 109     if(driver_byte(SCpnt->result) != 0)
 110         switch(SCpnt->sense_buffer[2] & 0xf) {
 111         case ILLEGAL_REQUEST:
 112             if(cmd[0] == ALLOW_MEDIUM_REMOVAL) dev->lockable = 0;
 113             else printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
 114             break;
 115         case NOT_READY: /* This happens if there is no disc in drive */
 116             if(dev->removable){
 117                 printk("Device not ready.  Make sure there is a disc in the drive.\n");
 118                 break;
 119             };
 120         case UNIT_ATTENTION:
 121             if (dev->removable){
 122                 dev->changed = 1;
 123                 SCpnt->result = 0; /* This is no longer considered an error */
 124                 printk("Disc change detected.\n");
 125                 break;
 126             };
 127         default: /* Fall through for non-removable media */
 128             printk("SCSI CD error: host %d id %d lun %d return code = %x\n",
 129                    dev->host->host_no,
 130                    dev->id,
 131                    dev->lun,
 132                    SCpnt->result);
 133             printk("\tSense class %x, sense error %x, extended sense %x\n",
 134                    sense_class(SCpnt->sense_buffer[0]),
 135                    sense_error(SCpnt->sense_buffer[0]),
 136                    SCpnt->sense_buffer[2] & 0xf);
 137             
 138         };
 139     
 140     result = SCpnt->result;
 141     SCpnt->request.dev = -1;
 142     wake_up(&SCpnt->device->device_wait);
 143     return result;
 144 }
 145 
 146 static int ioctl_command(Scsi_Device *dev, void *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148     char * buf;
 149     char cmd[12];
 150     char * cmd_in;
 151     Scsi_Cmnd * SCpnt;
 152     unsigned char opcode;
 153     int inlen, outlen, cmdlen;
 154     int needed, buf_needed;
 155     int result;
 156     
 157     if (!buffer)
 158         return -EINVAL;
 159     
 160         inlen = get_user((unsigned int *) buffer);
 161         outlen = get_user( ((unsigned int *) buffer) + 1);
 162   
 163         cmd_in = (char *) ( ((int *)buffer) + 2);
 164         opcode = get_user(cmd_in); 
 165     
 166     needed = buf_needed = (inlen > outlen ? inlen : outlen);
 167     if(buf_needed){
 168         buf_needed = (buf_needed + 511) & ~511;
 169         if (buf_needed > MAX_BUF) buf_needed = MAX_BUF;
 170         buf = (char *) scsi_malloc(buf_needed);
 171         if (!buf) return -ENOMEM;
 172         memset(buf, 0, buf_needed);
 173     } else
 174         buf = NULL;
 175     
 176     memcpy_fromfs ((void *) cmd,  cmd_in,  cmdlen = COMMAND_SIZE (opcode));
 177     memcpy_fromfs ((void *) buf,  (void *) (cmd_in + cmdlen), inlen > MAX_BUF ? MAX_BUF : inlen);
 178     
 179     cmd[1] = ( cmd[1] & 0x1f ) | (dev->lun << 5);
 180     
 181 #ifndef DEBUG_NO_CMD
 182     
 183     SCpnt = allocate_device(NULL, dev, 1);
 184     
 185     scsi_do_cmd(SCpnt,  cmd,  buf, needed,  scsi_ioctl_done,  MAX_TIMEOUT, 
 186                 MAX_RETRIES);
 187     
 188     if (SCpnt->request.dev != 0xfffe){
 189         struct semaphore sem = MUTEX_LOCKED;
 190         SCpnt->request.sem = &sem;
 191         down(&sem);
 192         /* Hmm.. Have to ask about this one */
 193         while (SCpnt->request.dev != 0xfffe) schedule();
 194     };
 195     
 196     
 197     /* If there was an error condition, pass the info back to the user. */
 198     if(SCpnt->result) {
 199         result = verify_area(VERIFY_WRITE, cmd_in, sizeof(SCpnt->sense_buffer));
 200         if (result)
 201             return result;
 202         memcpy_tofs((void *) cmd_in,  SCpnt->sense_buffer, sizeof(SCpnt->sense_buffer));
 203     } else {
 204         
 205         result = verify_area(VERIFY_WRITE, cmd_in, (outlen > MAX_BUF) ? MAX_BUF  : outlen);
 206         if (result)
 207             return result;
 208         memcpy_tofs ((void *) cmd_in,  buf,  (outlen > MAX_BUF) ? MAX_BUF  : outlen);
 209     };
 210     result = SCpnt->result;
 211     SCpnt->request.dev = -1;  /* Mark as not busy */
 212     if (buf) scsi_free(buf, buf_needed);
 213     
 214     if(SCpnt->device->scsi_request_fn)
 215         (*SCpnt->device->scsi_request_fn)();
 216     
 217     wake_up(&SCpnt->device->device_wait);
 218     return result;
 219 #else
 220     {
 221         int i;
 222         printk("scsi_ioctl : device %d.  command = ", dev->id);
 223         for (i = 0; i < 12; ++i)
 224             printk("%02x ", cmd[i]);
 225         printk("\nbuffer =");
 226         for (i = 0; i < 20; ++i)
 227             printk("%02x ", buf[i]);
 228         printk("\n");
 229         printk("inlen = %d, outlen = %d, cmdlen = %d\n",
 230                inlen, outlen, cmdlen);
 231         printk("buffer = %d, cmd_in = %d\n", buffer, cmd_in);
 232     }
 233     return 0;
 234 #endif
 235 }
 236 
 237 /*
 238  * the scsi_ioctl() function differs from most ioctls in that it does
 239  * not take a major/minor number as the dev filed.  Rather, it takes
 240  * a pointer to a scsi_devices[] element, a structure. 
 241  */
 242 int scsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 243 {
 244     char scsi_cmd[12];
 245     
 246     /* No idea how this happens.... */
 247     if (!dev) return -ENXIO;
 248     
 249     switch (cmd) {
 250     case SCSI_IOCTL_GET_IDLUN:
 251         verify_area(VERIFY_WRITE, (void *) arg, sizeof(int));
 252         put_user(dev->id + (dev->lun << 8) + (dev->host->host_no << 16) +
 253                     /* This has been added to support 
 254                      * multichannel HBAs, it might cause 
 255                      * problems with some software */ 
 256                     (dev->channel << 24), 
 257                     (unsigned long *) arg);
 258         return 0;
 259     case SCSI_IOCTL_TAGGED_ENABLE:
 260         if(!suser())  return -EACCES;
 261         if(!dev->tagged_supported) return -EINVAL;
 262         dev->tagged_queue = 1;
 263         dev->current_tag = 1;
 264         break;
 265     case SCSI_IOCTL_TAGGED_DISABLE:
 266         if(!suser())  return -EACCES;
 267         if(!dev->tagged_supported) return -EINVAL;
 268         dev->tagged_queue = 0;
 269         dev->current_tag = 0;
 270         break;
 271     case SCSI_IOCTL_PROBE_HOST:
 272         return ioctl_probe(dev->host, arg);
 273     case SCSI_IOCTL_SEND_COMMAND:
 274         if(!suser())  return -EACCES;
 275         return ioctl_command((Scsi_Device *) dev, arg);
 276     case SCSI_IOCTL_DOORLOCK:
 277         if (!dev->removable || !dev->lockable) return 0;
 278         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
 279         scsi_cmd[1] = dev->lun << 5;
 280         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 281         scsi_cmd[4] = SCSI_REMOVAL_PREVENT;
 282         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 283         break;
 284     case SCSI_IOCTL_DOORUNLOCK:
 285         if (!dev->removable || !dev->lockable) return 0;
 286         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
 287         scsi_cmd[1] = dev->lun << 5;
 288         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 289         scsi_cmd[4] = SCSI_REMOVAL_ALLOW;
 290         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 291     case SCSI_IOCTL_TEST_UNIT_READY:
 292         scsi_cmd[0] = TEST_UNIT_READY;
 293         scsi_cmd[1] = dev->lun << 5;
 294         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 295         scsi_cmd[4] = 0;
 296         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 297         break;
 298     default :           
 299         return -EINVAL;
 300     }
 301     return -EINVAL;
 302 }
 303 
 304 /*
 305  * Just like scsi_ioctl, only callable from kernel space with no 
 306  * fs segment fiddling.
 307  */
 308 
 309 int kernel_scsi_ioctl (Scsi_Device *dev, int cmd, void *arg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 310     unsigned long oldfs;
 311     int tmp;
 312     oldfs = get_fs();
 313     set_fs(get_ds());
 314     tmp = scsi_ioctl (dev, cmd, arg);
 315     set_fs(oldfs);
 316     return tmp;
 317 }
 318 
 319 /*
 320  * Overrides for Emacs so that we almost follow Linus's tabbing style.
 321  * Emacs will notice this stuff at the end of the file and automatically
 322  * adjust the settings for this buffer only.  This must remain at the end
 323  * of the file.
 324  * ---------------------------------------------------------------------------
 325  * Local variables:
 326  * c-indent-level: 4
 327  * c-brace-imaginary-offset: 0
 328  * c-brace-offset: -4
 329  * c-argdecl-indent: 4
 330  * c-label-offset: -4
 331  * c-continued-statement-offset: 4
 332  * c-continued-brace-offset: 0
 333  * indent-tabs-mode: nil
 334  * tab-width: 8
 335  * End:
 336  */

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