root/drivers/scsi/sg.c

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

DEFINITIONS

This source file includes following definitions.
  1. sg_ioctl
  2. sg_open
  3. sg_close
  4. sg_malloc
  5. sg_free
  6. sg_read
  7. sg_command_done
  8. sg_write
  9. sg_select
  10. sg_detect
  11. sg_init
  12. sg_attach
  13. sg_detach
  14. init_module
  15. cleanup_module

   1 /*
   2  *  History:
   3  *  Started: Aug 9 by Lawrence Foard (entropy@world.std.com), 
   4  *           to allow user process control of SCSI devices.
   5  *  Development Sponsored by Killy Corp. NY NY
   6  *   
   7  *  Borrows code from st driver.
   8  */
   9 #ifdef MODULE
  10 #include <linux/autoconf.h>
  11 #include <linux/module.h>
  12 #include <linux/version.h>
  13 #endif /* MODULE */
  14 
  15 #include <linux/fs.h>
  16 #include <linux/kernel.h>
  17 #include <linux/sched.h>
  18 #include <linux/string.h>
  19 #include <linux/mm.h>
  20 #include <linux/errno.h>
  21 #include <linux/mtio.h>
  22 #include <linux/ioctl.h>
  23 #include <linux/fcntl.h>
  24 #include <asm/io.h>
  25 #include <asm/segment.h>
  26 #include <asm/system.h>
  27 
  28 #include "../block/blk.h"
  29 #include "scsi.h"
  30 #include "hosts.h"
  31 #include "scsi_ioctl.h"
  32 #include "sg.h"
  33 
  34 static int sg_init(void);
  35 static int sg_attach(Scsi_Device *);
  36 static int sg_detect(Scsi_Device *);
  37 static void sg_detach(Scsi_Device *);
  38 
  39 
  40 struct Scsi_Device_Template sg_template = {NULL, NULL, "sg", NULL, 0xff, 
  41                                                SCSI_GENERIC_MAJOR, 0, 0, 0, 0,
  42                                                sg_detect, sg_init,
  43                                                NULL, sg_attach, sg_detach};
  44 
  45 #ifdef SG_BIG_BUFF
  46 static char *big_buff = NULL;
  47 static struct wait_queue *big_wait;   /* wait for buffer available */
  48 static int big_inuse=0;
  49 #endif
  50 
  51 struct scsi_generic
  52 {
  53     Scsi_Device *device;
  54     int users;   /* how many people have it open? */
  55     struct wait_queue *generic_wait; /* wait for device to be available */
  56     struct wait_queue *read_wait;    /* wait for response */
  57     struct wait_queue *write_wait;   /* wait for free buffer */
  58     int timeout; /* current default value for device */
  59     int buff_len; /* length of current buffer */
  60     char *buff;   /* the buffer */
  61     struct sg_header header; /* header of pending command */
  62     char exclude; /* opened for exclusive access */
  63     char pending;  /* don't accept writes now */
  64     char complete; /* command complete allow a read */
  65 };
  66 
  67 static struct scsi_generic *scsi_generics=NULL;
  68 static void sg_free(char *buff,int size);
  69 
  70 static int sg_ioctl(struct inode * inode,struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
  71                     unsigned int cmd_in, unsigned long arg)
  72 {
  73     int result;
  74     int dev = MINOR(inode->i_rdev);
  75     if ((dev<0) || (dev>=sg_template.dev_max))
  76         return -ENXIO;
  77     switch(cmd_in)
  78     {
  79     case SG_SET_TIMEOUT:
  80         result = verify_area(VERIFY_READ, (const void *)arg, sizeof(long));
  81         if (result) return result;
  82 
  83         scsi_generics[dev].timeout=get_user((int *) arg);
  84         return 0;
  85     case SG_GET_TIMEOUT:
  86         return scsi_generics[dev].timeout;
  87     default:
  88         return scsi_ioctl(scsi_generics[dev].device, cmd_in, (void *) arg);
  89     }
  90 }
  91 
  92 static int sg_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94     int dev=MINOR(inode->i_rdev);
  95     int flags=filp->f_flags;
  96     if (dev>=sg_template.dev_max || !scsi_generics[dev].device)
  97         return -ENXIO;
  98     if (O_RDWR!=(flags & O_ACCMODE))
  99         return -EACCES;
 100 
 101   /*
 102    * If we want exclusive access, then wait until the device is not
 103    * busy, and then set the flag to prevent anyone else from using it.
 104    */
 105     if (flags & O_EXCL)
 106     {
 107         while(scsi_generics[dev].users)
 108         {
 109             if (flags & O_NONBLOCK)
 110                 return -EBUSY;
 111             interruptible_sleep_on(&scsi_generics[dev].generic_wait);
 112             if (current->signal & ~current->blocked)
 113                 return -ERESTARTSYS;
 114         }
 115         scsi_generics[dev].exclude=1;
 116     }
 117     else
 118         /*
 119          * Wait until nobody has an exclusive open on
 120          * this device.
 121          */
 122         while(scsi_generics[dev].exclude)
 123         {
 124             if (flags & O_NONBLOCK)
 125                 return -EBUSY;
 126             interruptible_sleep_on(&scsi_generics[dev].generic_wait);
 127             if (current->signal & ~current->blocked)
 128                 return -ERESTARTSYS;
 129         }
 130 
 131     /*
 132      * OK, we should have grabbed the device.  Mark the thing so
 133      * that other processes know that we have it, and initialize the
 134      * state variables to known values.
 135      */
 136     if (!scsi_generics[dev].users 
 137         && scsi_generics[dev].pending
 138         && scsi_generics[dev].complete)
 139     {
 140         if (scsi_generics[dev].buff != NULL)
 141             sg_free(scsi_generics[dev].buff,scsi_generics[dev].buff_len);
 142         scsi_generics[dev].buff=NULL;
 143         scsi_generics[dev].pending=0;
 144     }
 145     if (!scsi_generics[dev].users)
 146         scsi_generics[dev].timeout=SG_DEFAULT_TIMEOUT;
 147     if (scsi_generics[dev].device->host->hostt->usage_count)
 148         (*scsi_generics[dev].device->host->hostt->usage_count)++;
 149     if(sg_template.usage_count) (*sg_template.usage_count)++;
 150     scsi_generics[dev].users++;
 151     return 0;
 152 }
 153 
 154 static void sg_close(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 155 {
 156     int dev=MINOR(inode->i_rdev);
 157     scsi_generics[dev].users--;
 158     if (scsi_generics[dev].device->host->hostt->usage_count)
 159         (*scsi_generics[dev].device->host->hostt->usage_count)--;
 160     if(sg_template.usage_count) (*sg_template.usage_count)--;
 161     scsi_generics[dev].exclude=0;
 162     wake_up(&scsi_generics[dev].generic_wait);
 163 }
 164 
 165 static char *sg_malloc(int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167     if (size<=4096)
 168         return (char *) scsi_malloc(size);
 169 #ifdef SG_BIG_BUFF
 170     if (size<=SG_BIG_BUFF)
 171     {
 172         while(big_inuse)
 173         {
 174             interruptible_sleep_on(&big_wait);
 175             if (current->signal & ~current->blocked)
 176                 return NULL;
 177         }
 178         big_inuse=1;
 179         return big_buff;
 180     }
 181 #endif   
 182     return NULL;
 183 }
 184 
 185 static void sg_free(char *buff,int size) 
     /* [previous][next][first][last][top][bottom][index][help] */
 186 {
 187 #ifdef SG_BIG_BUFF
 188     if (buff==big_buff)
 189     {
 190         big_inuse=0;
 191         wake_up(&big_wait);
 192         return;
 193     }
 194 #endif
 195     scsi_free(buff,size);
 196 }
 197 
 198 /*
 199  * Read back the results of a previous command.  We use the pending and
 200  * complete semaphores to tell us whether the buffer is available for us
 201  * and whether the command is actually done.
 202  */
 203 static int sg_read(struct inode *inode,struct file *filp,char *buf,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 204 {
 205     int dev=MINOR(inode->i_rdev);
 206     int i;
 207     struct scsi_generic *device=&scsi_generics[dev];
 208     if ((i=verify_area(VERIFY_WRITE,buf,count)))
 209         return i;
 210 
 211     /*
 212      * Wait until the command is actually done.
 213      */
 214     while(!device->pending || !device->complete)
 215     {
 216         if (filp->f_flags & O_NONBLOCK)
 217             return -EWOULDBLOCK;
 218         interruptible_sleep_on(&device->read_wait);
 219         if (current->signal & ~current->blocked)
 220             return -ERESTARTSYS;
 221     }
 222 
 223     /*
 224      * Now copy the result back to the user buffer.
 225      */
 226     device->header.pack_len=device->header.reply_len;
 227     device->header.result=0;
 228     if (count>=sizeof(struct sg_header))
 229     {
 230         memcpy_tofs(buf,&device->header,sizeof(struct sg_header));
 231         buf+=sizeof(struct sg_header);
 232         if (count>device->header.pack_len)
 233             count=device->header.pack_len;
 234         if (count > sizeof(struct sg_header)) {
 235             memcpy_tofs(buf,device->buff,count-sizeof(struct sg_header));
 236         }
 237     }
 238     else
 239         count=0;
 240     
 241     /*
 242      * Clean up, and release the device so that we can send another
 243      * command.
 244      */
 245     sg_free(device->buff,device->buff_len);
 246     device->buff = NULL;
 247     device->pending=0;
 248     wake_up(&device->write_wait);
 249     return count;
 250 }
 251 
 252 /*
 253  * This function is called by the interrupt handler when we
 254  * actually have a command that is complete.  Change the
 255  * flags to indicate that we have a result.
 256  */
 257 static void sg_command_done(Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 258 {
 259     int dev=SCpnt->request.dev;
 260     struct scsi_generic *device=&scsi_generics[dev];
 261     if (!device->pending)
 262     {
 263         printk("unexpected done for sg %d\n",dev);
 264         SCpnt->request.dev=-1;
 265         return;
 266     }
 267 
 268     /*
 269      * See if the command completed normally, or whether something went
 270      * wrong.
 271      */
 272     memcpy(device->header.sense_buffer, SCpnt->sense_buffer, sizeof(SCpnt->sense_buffer));
 273     if (SCpnt->sense_buffer[0])
 274     {
 275         device->header.result=EIO;
 276     }
 277     else
 278         device->header.result=SCpnt->result;
 279 
 280     /*
 281      * Now wake up the process that is waiting for the
 282      * result.
 283      */
 284     device->complete=1;
 285     SCpnt->request.dev=-1;
 286     wake_up(&scsi_generics[dev].read_wait);
 287 }
 288 
 289 #define SG_SEND 0
 290 #define SG_REC  1
 291 
 292 static int sg_write(struct inode *inode,struct file *filp,const char *buf,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 293 {
 294     int                   bsize,size,amt,i;
 295     unsigned char         cmnd[MAX_COMMAND_SIZE];
 296     int                   dev=MINOR(inode->i_rdev);
 297     struct scsi_generic   * device=&scsi_generics[dev];
 298     int                   direction;
 299     unsigned char         opcode;
 300     Scsi_Cmnd           * SCpnt;
 301     
 302     if ((i=verify_area(VERIFY_READ,buf,count)))
 303         return i;
 304     /*
 305      * The minimum scsi command length is 6 bytes.  If we get anything
 306      * less than this, it is clearly bogus.  
 307      */
 308     if (count<(sizeof(struct sg_header) + 6))
 309         return -EIO;
 310 
 311     /*
 312      * If we still have a result pending from a previous command,
 313      * wait until the result has been read by the user before sending
 314      * another command.
 315      */
 316     while(device->pending)
 317     {
 318         if (filp->f_flags & O_NONBLOCK)
 319             return -EWOULDBLOCK;
 320 #ifdef DEBUG
 321         printk("sg_write: sleeping on pending request\n");
 322 #endif     
 323         interruptible_sleep_on(&device->write_wait);
 324         if (current->signal & ~current->blocked)
 325             return -ERESTARTSYS;
 326     }
 327 
 328     /*
 329      * Mark the device flags for the new state.
 330      */
 331     device->pending=1;
 332     device->complete=0;
 333     memcpy_fromfs(&device->header,buf,sizeof(struct sg_header));
 334 
 335     /*
 336      * fix input size, and see if we are sending data.
 337      */
 338     device->header.pack_len=count;
 339     buf+=sizeof(struct sg_header);
 340     if( device->header.pack_len > device->header.reply_len )
 341     {
 342         bsize = device->header.pack_len;
 343         direction = SG_SEND;
 344     } else {
 345         bsize = device->header.reply_len;
 346         direction = SG_REC;
 347     }
 348     
 349     /*
 350      * Don't include the command header itself in the size.
 351      */
 352     bsize-=sizeof(struct sg_header);
 353 
 354     /*
 355      * Allocate a buffer that is large enough to hold the data
 356      * that has been requested.  Round up to an even number of sectors,
 357      * since scsi_malloc allocates in chunks of 512 bytes.
 358      */
 359     amt=bsize;
 360     if (!bsize)
 361         bsize++;
 362     bsize=(bsize+511) & ~511;
 363 
 364     /*
 365      * If we cannot allocate the buffer, report an error.
 366      */
 367     if ((bsize<0) || !(device->buff=sg_malloc(device->buff_len=bsize)))
 368     {
 369         device->pending=0;
 370         wake_up(&device->write_wait);
 371         return -ENOMEM;
 372     }
 373 
 374 #ifdef DEBUG
 375     printk("allocating device\n");
 376 #endif
 377 
 378     /*
 379      * Grab a device pointer for the device we want to talk to.  If we
 380      * don't want to block, just return with the appropriate message.
 381      */
 382     if (!(SCpnt=allocate_device(NULL,device->device, !(filp->f_flags & O_NONBLOCK))))
 383     {
 384         device->pending=0;
 385         wake_up(&device->write_wait);
 386         sg_free(device->buff,device->buff_len);
 387         device->buff = NULL;
 388         return -EWOULDBLOCK;
 389     } 
 390 #ifdef DEBUG
 391     printk("device allocated\n");
 392 #endif    
 393 
 394     /*
 395      * Now we need to grab the command itself from the user's buffer.
 396      */
 397     SCpnt->request.dev=dev;
 398     SCpnt->sense_buffer[0]=0;
 399     opcode = get_user(buf);
 400     size=COMMAND_SIZE(opcode);
 401     if (opcode >= 0xc0 && device->header.twelve_byte) size = 12;
 402     SCpnt->cmd_len = size;
 403 
 404     /*
 405      * If we are writing data, subtract off the size
 406      * of the command itself, to get the amount of actual data
 407      * that we need to send to the device.
 408      */
 409     if( direction == SG_SEND )
 410         amt -= size;
 411     
 412     /*
 413      * Verify that the user has actually passed enough bytes for this command.
 414      */
 415     if( count < (sizeof(struct sg_header) + size) )
 416     {
 417         device->pending=0;
 418         wake_up( &device->write_wait );
 419         sg_free( device->buff, device->buff_len );
 420         device->buff = NULL;
 421         return -EIO;
 422     }
 423     
 424     /*
 425      * Now copy the SCSI command from the user's address space.
 426      */
 427     memcpy_fromfs(cmnd,buf,size);
 428     buf+=size;
 429 
 430     /*
 431      * If we are writing data, copy the data we are writing.  The pack_len
 432      * field also includes the length of the header and the command,
 433      * so we need to subtract these off.
 434      */
 435     if( direction == SG_SEND )  memcpy_fromfs(device->buff,buf, amt);
 436     
 437     /*
 438      * Set the LUN field in the command structure.
 439      */
 440     cmnd[1]= (cmnd[1] & 0x1f) | (device->device->lun<<5);
 441 
 442 #ifdef DEBUG
 443     printk("do cmd\n");
 444 #endif
 445 
 446     /*
 447      * Now pass the actual command down to the low-level driver.  We
 448      * do not do any more here - when the interrupt arrives, we will
 449      * then do the post-processing.
 450      */
 451     scsi_do_cmd (SCpnt,(void *) cmnd,
 452                  (void *) device->buff,amt,
 453                  sg_command_done,device->timeout,SG_DEFAULT_RETRIES);
 454 
 455 #ifdef DEBUG
 456     printk("done cmd\n");
 457 #endif               
 458 
 459     return count;
 460 }
 461 
 462 static int sg_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 463 {
 464     int dev=MINOR(inode->i_rdev);
 465     int i;
 466     int r = 0;
 467     struct scsi_generic *device=&scsi_generics[dev];
 468 
 469     if (sel_type == SEL_IN) {
 470         if(device->pending && device->complete)
 471         {
 472             r = 1;
 473         } else {
 474             select_wait(&scsi_generics[dev].read_wait, wait);
 475         }
 476     }
 477     if (sel_type == SEL_OUT) {
 478         if(!device->pending){
 479             r = 1;
 480         }
 481         else
 482         {
 483             select_wait(&scsi_generics[dev].write_wait, wait);
 484         }
 485     }
 486 
 487     return(r);
 488 }
 489 
 490 static struct file_operations sg_fops = {
 491     NULL,            /* lseek */
 492     sg_read,         /* read */
 493     sg_write,        /* write */
 494     NULL,            /* readdir */
 495     sg_select,       /* select */
 496     sg_ioctl,        /* ioctl */
 497     NULL,            /* mmap */
 498     sg_open,         /* open */
 499     sg_close,        /* release */
 500     NULL             /* fsync */
 501 };
 502 
 503 
 504 static int sg_detect(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 505     ++sg_template.dev_noticed;
 506     return 1;
 507 }
 508 
 509 /* Driver initialization */
 510 static int sg_init()
     /* [previous][next][first][last][top][bottom][index][help] */
 511 {
 512     static int sg_registered = 0;
 513     
 514     if (sg_template.dev_noticed == 0) return 0;
 515     
 516     if(!sg_registered) {
 517         if (register_chrdev(SCSI_GENERIC_MAJOR,"sg",&sg_fops)) 
 518         {
 519             printk("Unable to get major %d for generic SCSI device\n",
 520                    SCSI_GENERIC_MAJOR);
 521             return 1;
 522         }
 523         sg_registered++;
 524     }
 525     
 526     /* If we have already been through here, return */
 527     if(scsi_generics) return 0;
 528     
 529 #ifdef DEBUG
 530     printk("sg: Init generic device.\n");
 531 #endif
 532     
 533 #ifdef SG_BIG_BUFF
 534     big_buff= (char *) scsi_init_malloc(SG_BIG_BUFF, GFP_ATOMIC | GFP_DMA);
 535 #endif
 536     
 537     scsi_generics = (struct scsi_generic *) 
 538         scsi_init_malloc((sg_template.dev_noticed + SG_EXTRA_DEVS) 
 539                          * sizeof(struct scsi_generic), GFP_ATOMIC);
 540     memset(scsi_generics, 0, (sg_template.dev_noticed + SG_EXTRA_DEVS)
 541            * sizeof(struct scsi_generic));
 542     
 543     sg_template.dev_max = sg_template.dev_noticed + SG_EXTRA_DEVS;
 544     return 0;
 545 }
 546 
 547 static int sg_attach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
 548 {
 549     struct scsi_generic * gpnt;
 550     int i;
 551     
 552     if(sg_template.nr_dev >= sg_template.dev_max) 
 553     {
 554         SDp->attached--;
 555         return 1;
 556     }
 557     
 558     for(gpnt = scsi_generics, i=0; i<sg_template.dev_max; i++, gpnt++) 
 559         if(!gpnt->device) break;
 560     
 561     if(i >= sg_template.dev_max) panic ("scsi_devices corrupt (sg)");
 562     
 563     scsi_generics[i].device=SDp;
 564     scsi_generics[i].users=0;
 565     scsi_generics[i].generic_wait=NULL;
 566     scsi_generics[i].read_wait=NULL;
 567     scsi_generics[i].write_wait=NULL;
 568     scsi_generics[i].buff=NULL;
 569     scsi_generics[i].exclude=0;
 570     scsi_generics[i].pending=0;
 571     scsi_generics[i].timeout=SG_DEFAULT_TIMEOUT;
 572     sg_template.nr_dev++;
 573     return 0;
 574 };
 575 
 576 
 577 
 578 static void sg_detach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
 579 {
 580     struct scsi_generic * gpnt;
 581     int i;
 582     
 583     for(gpnt = scsi_generics, i=0; i<sg_template.dev_max; i++, gpnt++) 
 584         if(gpnt->device == SDp) {
 585             gpnt->device = NULL;
 586             SDp->attached--;
 587             sg_template.nr_dev--;
 588             return;
 589         }
 590     return;
 591 }
 592 
 593 #ifdef MODULE
 594 char kernel_version[] = UTS_RELEASE;
 595 
 596 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 597     sg_template.usage_count = &mod_use_count_;
 598     return scsi_register_module(MODULE_SCSI_DEV, &sg_template);
 599 }
 600 
 601 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
 602 {
 603     if (MOD_IN_USE) {
 604         printk(KERN_INFO __FILE__ ": module is in use, remove rejected\n");
 605         return;
 606     }
 607     scsi_unregister_module(MODULE_SCSI_DEV, &sg_template);
 608     unregister_chrdev(SCSI_GENERIC_MAJOR, "sg");
 609     
 610     if(scsi_generics != NULL) {
 611         scsi_init_free((char *) scsi_generics,
 612                        (sg_template.dev_noticed + SG_EXTRA_DEVS) 
 613                        * sizeof(struct scsi_generic));
 614     }
 615     sg_template.dev_max = 0;
 616 #ifdef SG_BIG_BUFF
 617     if(big_buff != NULL)
 618         scsi_init_free(big_buff, SG_BIG_BUFF);
 619 #endif
 620 }
 621 #endif /* MODULE */
 622 
 623 /*
 624  * Overrides for Emacs so that we almost follow Linus's tabbing style.
 625  * Emacs will notice this stuff at the end of the file and automatically
 626  * adjust the settings for this buffer only.  This must remain at the end
 627  * of the file.
 628  * ---------------------------------------------------------------------------
 629  * Local variables:
 630  * c-indent-level: 4
 631  * c-brace-imaginary-offset: 0
 632  * c-brace-offset: -4
 633  * c-argdecl-indent: 4
 634  * c-label-offset: -4
 635  * c-continued-statement-offset: 4
 636  * c-continued-brace-offset: 0
 637  * indent-tabs-mode: nil
 638  * tab-width: 8
 639  * End:
 640  */

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