root/kernel/blk_drv/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

   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 "../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 200
  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(int dev, void *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30         int temp;
  31         int len;
  32         
  33         if ((temp = scsi_hosts[dev].present) && buffer) {
  34                 len = get_fs_long ((int *) buffer);
  35                 memcpy_tofs (buffer, scsi_hosts[dev].info(), len);
  36         }
  37         return temp;
  38 }
  39 
  40 /*
  41  * 
  42  * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
  43  * The MAX_TIMEOUT and MAX_RETRIES  variables are used.  
  44  * 
  45  * dev is the SCSI device struct ptr, *(int *) arg is the length of the
  46  * input data, if any, not including the command string & counts, 
  47  * *((int *)arg + 1) is the output buffer size in bytes.
  48  * 
  49  * *(char *) ((int *) arg)[2] the actual command byte.   
  50  * 
  51  * Note that no more than MAX_BUF data bytes will be transfered.  Since
  52  * SCSI block device size is 512 bytes, I figured 1K was good.
  53  * but (WDE) changed it to 8192 to handle large bad track buffers.
  54  * ERY: I changed this to a dynamic allocation using scsi_malloc - we were
  55  * getting a kernel stack overflow which was crashing the system when we
  56  * were using 8192 bytes.
  57  * 
  58  * This size *does not* include the initial lengths that were passed.
  59  * 
  60  * The SCSI command is read from the memory location immediately after the
  61  * length words, and the input data is right after the command.  The SCSI
  62  * routines know the command size based on the opcode decode.  
  63  * 
  64  * The output area is then filled in starting from the command byte. 
  65  */
  66 
  67 static void scsi_ioctl_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69   struct request * req;
  70   struct task_struct * p;
  71   
  72   req = &SCpnt->request;
  73   req->dev = 0xfffe; /* Busy, but indicate request done */
  74   
  75   if ((p = req->waiting) != NULL) {
  76     req->waiting = NULL;
  77     p->state = TASK_RUNNING;
  78     if (p->counter > current->counter)
  79       need_resched = 1;
  80   }
  81 }       
  82 
  83 static int ioctl_internal_command(Scsi_Device *dev, char * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
  84 {
  85         int host, result;
  86         Scsi_Cmnd * SCpnt;
  87 
  88         host = dev->host_no;
  89 
  90         SCpnt = allocate_device(NULL, dev->index, 1);
  91         scsi_do_cmd(SCpnt,  cmd, NULL,  0,
  92                         scsi_ioctl_done,  MAX_TIMEOUT,
  93                         MAX_RETRIES);
  94 
  95         if (SCpnt->request.dev != 0xfffe){
  96           SCpnt->request.waiting = current;
  97           current->state = TASK_UNINTERRUPTIBLE;
  98           while (SCpnt->request.dev != 0xfffe) schedule();
  99         };
 100 
 101         if(driver_byte(SCpnt->result) != 0)
 102           switch(SCpnt->sense_buffer[2] & 0xf) {
 103           case ILLEGAL_REQUEST:
 104             printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
 105             break;
 106           case NOT_READY: /* This happens if there is no disc in drive */
 107             if(dev->removable){
 108               printk("Device not ready.  Make sure there is a disc in the drive.\n");
 109               break;
 110             };
 111           case UNIT_ATTENTION:
 112             if (dev->removable){
 113               dev->changed = 1;
 114               SCpnt->result = 0; /* This is no longer considered an error */
 115               printk("Disc change detected.\n");
 116               break;
 117             };
 118           default: /* Fall through for non-removable media */
 119             printk("SCSI CD error: host %d id %d lun %d return code = %x\n",
 120                    dev->host_no,
 121                    dev->id,
 122                    dev->lun,
 123                    SCpnt->result);
 124             printk("\tSense class %x, sense error %x, extended sense %x\n",
 125                    sense_class(SCpnt->sense_buffer[0]),
 126                    sense_error(SCpnt->sense_buffer[0]),
 127                    SCpnt->sense_buffer[2] & 0xf);
 128 
 129           };
 130 
 131         result = SCpnt->result;
 132         SCpnt->request.dev = -1;  /* Mark as not busy */
 133         wake_up(&scsi_devices[SCpnt->index].device_wait);
 134         return result;
 135 }
 136 
 137 static int ioctl_command(Scsi_Device *dev, void *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139         char * buf;
 140         char cmd[10];
 141         char * cmd_in;
 142         Scsi_Cmnd * SCpnt;
 143         unsigned char opcode;
 144         int inlen, outlen, cmdlen, host;
 145         int needed;
 146         int result;
 147 
 148         if (!buffer)
 149                 return -EINVAL;
 150         
 151         inlen = get_fs_long((int *) buffer);
 152         outlen = get_fs_long( ((int *) buffer) + 1);
 153 
 154         cmd_in = (char *) ( ((int *)buffer) + 2);
 155         opcode = get_fs_byte(cmd_in); 
 156 
 157         needed = (inlen > outlen ? inlen : outlen);
 158         if(needed){
 159           needed = (needed + 511) & ~511;
 160           if (needed > MAX_BUF) needed = MAX_BUF;
 161           buf = scsi_malloc(needed);
 162           if (!buf) return -ENOMEM;
 163         } else
 164           buf = NULL;
 165 
 166         memcpy_fromfs ((void *) cmd,  cmd_in,  cmdlen = COMMAND_SIZE (opcode));
 167         memcpy_fromfs ((void *) buf,  (void *) (cmd_in + cmdlen),  inlen);
 168         host = dev->host_no;
 169 
 170 #ifndef DEBUG_NO_CMD
 171         
 172         SCpnt = allocate_device(NULL, dev->index, 1);
 173 
 174         scsi_do_cmd(SCpnt,  cmd,  buf,  ((outlen > MAX_BUF) ? 
 175                         MAX_BUF : outlen),  scsi_ioctl_done,  MAX_TIMEOUT, 
 176                         MAX_RETRIES);
 177 
 178         if (SCpnt->request.dev != 0xfffe){
 179           SCpnt->request.waiting = current;
 180           current->state = TASK_UNINTERRUPTIBLE;
 181           while (SCpnt->request.dev != 0xfffe) schedule();
 182         };
 183 
 184         verify_area(cmd_in, (outlen > MAX_BUF) ? MAX_BUF  : outlen);
 185         memcpy_tofs ((void *) cmd_in,  buf,  (outlen > MAX_BUF) ? MAX_BUF  : outlen);
 186         result = SCpnt->result;
 187         SCpnt->request.dev = -1;  /* Mark as not busy */
 188         if (buf) scsi_free(buf, needed);
 189         wake_up(&scsi_devices[SCpnt->index].device_wait);
 190         return result;
 191 #else
 192         {
 193         int i;
 194         printk("scsi_ioctl : device %d.  command = ", dev->id);
 195         for (i = 0; i < 10; ++i)
 196                 printk("%02x ", cmd[i]);
 197         printk("\r\nbuffer =");
 198         for (i = 0; i < 20; ++i)
 199                 printk("%02x ", buf[i]);
 200         printk("\r\n");
 201         printk("inlen = %d, outlen = %d, cmdlen = %d\n",
 202                 inlen, outlen, cmdlen);
 203         printk("buffer = %d, cmd_in = %d\n", buffer, cmd_in);
 204         }
 205         return 0;
 206 #endif
 207 }
 208 
 209         
 210 /*
 211         the scsi_ioctl() function differs from most ioctls in that it does
 212         not take a major/minor number as the dev filed.  Rather, it takes
 213         a pointer to a scsi_devices[] element, a structure. 
 214 */
 215 int scsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 216 {
 217         char scsi_cmd[10];
 218 
 219         if ((cmd != 0 && dev->index > NR_SCSI_DEVICES))
 220                 return -ENODEV;
 221         if ((cmd == 0 && dev->host_no > max_scsi_hosts))
 222                 return -ENODEV;
 223         
 224         switch (cmd) {
 225                 case SCSI_IOCTL_PROBE_HOST:
 226                         return ioctl_probe(dev->host_no, arg);
 227                 case SCSI_IOCTL_SEND_COMMAND:
 228                         return ioctl_command((Scsi_Device *) dev, arg);
 229                 case SCSI_IOCTL_DOORLOCK:
 230                         if (!dev->removable) return 0;
 231                         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
 232                         scsi_cmd[1] = dev->lun << 5;
 233                         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 234                         scsi_cmd[4] = SCSI_REMOVAL_PREVENT;
 235                         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 236                         break;
 237                 case SCSI_IOCTL_DOORUNLOCK:
 238                         if (!dev->removable) return 0;
 239                         scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
 240                         scsi_cmd[1] = dev->lun << 5;
 241                         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 242                         scsi_cmd[4] = SCSI_REMOVAL_ALLOW;
 243                         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 244                 case SCSI_IOCTL_TEST_UNIT_READY:
 245                         scsi_cmd[0] = TEST_UNIT_READY;
 246                         scsi_cmd[1] = dev->lun << 5;
 247                         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 248                         scsi_cmd[4] = 0;
 249                         return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd);
 250                         break;
 251                 default :                       
 252                         return -EINVAL;
 253         }
 254 }

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