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

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