root/drivers/scsi/sd.c

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

DEFINITIONS

This source file includes following definitions.
  1. sd_open
  2. sd_release
  3. sd_geninit
  4. rw_intr
  5. do_sd_request
  6. requeue_sd_request
  7. check_scsidisk_media_change
  8. sd_init_done
  9. sd_init_onedisk
  10. sd_init
  11. sd_finish
  12. sd_detect
  13. sd_attach
  14. revalidate_scsidisk
  15. fop_revalidate_scsidisk
  16. sd_detach
  17. init_module
  18. cleanup_module

   1 /*
   2  *      sd.c Copyright (C) 1992 Drew Eckhardt 
   3  *           Copyright (C) 1993, 1994, 1995 Eric Youngdale
   4  *
   5  *      Linux scsi disk driver
   6  *              Initial versions: Drew Eckhardt 
   7  *              Subsequent revisions: Eric Youngdale
   8  *
   9  *      <drew@colorado.edu>
  10  *
  11  *       Modified by Eric Youngdale ericy@cais.com to
  12  *       add scatter-gather, multiple outstanding request, and other
  13  *       enhancements.
  14  *
  15  *       Modified by Eric Youngdale eric@aib.com to support loadable
  16  *       low-level scsi drivers.
  17  */
  18 
  19 #include <linux/module.h>
  20 #ifdef MODULE
  21 /*
  22  * This is a variable in scsi.c that is set when we are processing something
  23  * after boot time.  By definition, this is true when we are a loadable module
  24  * ourselves.
  25  */
  26 #define MODULE_FLAG 1
  27 #else
  28 #define MODULE_FLAG scsi_loadable_module_flag
  29 #endif /* MODULE */
  30 
  31 #include <linux/fs.h>
  32 #include <linux/kernel.h>
  33 #include <linux/sched.h>
  34 #include <linux/mm.h>
  35 #include <linux/string.h>
  36 #include <linux/errno.h>
  37 #include <asm/system.h>
  38 
  39 #define MAJOR_NR SCSI_DISK_MAJOR
  40 #include <linux/blk.h>
  41 #include "scsi.h"
  42 #include "hosts.h"
  43 #include "sd.h"
  44 #include <scsi/scsi_ioctl.h>
  45 #include "constants.h"
  46 
  47 #include <linux/genhd.h>
  48 
  49 /*
  50  *  static const char RCSid[] = "$Header:";
  51  */
  52 
  53 #define MAX_RETRIES 5
  54 
  55 /*
  56  *  Time out in seconds for disks and Magneto-opticals (which are slower).
  57  */
  58 
  59 #define SD_TIMEOUT (15 * HZ)
  60 #define SD_MOD_TIMEOUT (15 * HZ)
  61 
  62 #define CLUSTERABLE_DEVICE(SC) (SC->host->use_clustering && \
  63                                 SC->device->type != TYPE_MOD)
  64 
  65 struct hd_struct * sd;
  66 
  67 Scsi_Disk * rscsi_disks = NULL;
  68 static int * sd_sizes;
  69 static int * sd_blocksizes;
  70 static int * sd_hardsizes;              /* Hardware sector size */
  71 
  72 extern int sd_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  73 
  74 static int check_scsidisk_media_change(kdev_t);
  75 static int fop_revalidate_scsidisk(kdev_t);
  76 
  77 static sd_init_onedisk(int);
  78 
  79 static void requeue_sd_request (Scsi_Cmnd * SCpnt);
  80 
  81 static int sd_init(void);
  82 static void sd_finish(void);
  83 static int sd_attach(Scsi_Device *);
  84 static int sd_detect(Scsi_Device *);
  85 static void sd_detach(Scsi_Device *);
  86 
  87 struct Scsi_Device_Template sd_template = 
  88 { NULL, "disk", "sd", NULL, TYPE_DISK, 
  89       SCSI_DISK_MAJOR, 0, 0, 0, 1,
  90       sd_detect, sd_init,
  91       sd_finish, sd_attach, sd_detach
  92 };
  93 
  94 static int sd_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96     int target;
  97     target =  DEVICE_NR(inode->i_rdev);
  98     
  99     if(target >= sd_template.dev_max || !rscsi_disks[target].device)
 100         return -ENXIO;   /* No such device */
 101     
 102     /* 
 103      * Make sure that only one process can do a check_change_disk at one time.
 104      * This is also used to lock out further access when the partition table 
 105      * is being re-read. 
 106      */
 107     
 108     while (rscsi_disks[target].device->busy)
 109     barrier();   
 110     if(rscsi_disks[target].device->removable) {
 111         check_disk_change(inode->i_rdev);
 112         
 113         /*
 114          * If the drive is empty, just let the open fail.
 115          */
 116         if ( !rscsi_disks[target].ready ) {
 117             return -ENXIO;
 118         }
 119 
 120         /*
 121          * Similarly, if the device has the write protect tab set,
 122          * have the open fail if the user expects to be able to write
 123          * to the thing.
 124          */
 125         if ( (rscsi_disks[target].write_prot) && (filp->f_mode & 2) ) { 
 126             return -EROFS;
 127         }
 128 
 129         if(!rscsi_disks[target].device->access_count)
 130             sd_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
 131     }
 132 
 133     /*
 134      * See if we are requesting a non-existent partition.  Do this
 135      * after checking for disk change.
 136      */
 137     if(sd_sizes[MINOR(inode->i_rdev)] == 0)
 138         return -ENXIO;
 139     
 140     rscsi_disks[target].device->access_count++;
 141     if (rscsi_disks[target].device->host->hostt->usage_count)
 142         (*rscsi_disks[target].device->host->hostt->usage_count)++;
 143     if(sd_template.usage_count) (*sd_template.usage_count)++;
 144     return 0;
 145 }
 146 
 147 static void sd_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
 148 {
 149     int target;
 150     fsync_dev(inode->i_rdev);
 151     
 152     target =  DEVICE_NR(inode->i_rdev);
 153     
 154     rscsi_disks[target].device->access_count--;
 155     if (rscsi_disks[target].device->host->hostt->usage_count)
 156         (*rscsi_disks[target].device->host->hostt->usage_count)--;
 157     if(sd_template.usage_count) (*sd_template.usage_count)--;
 158     
 159     if(rscsi_disks[target].device->removable) {
 160         if(!rscsi_disks[target].device->access_count)
 161             sd_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
 162     }
 163 }
 164 
 165 static void sd_geninit(struct gendisk *);
 166 
 167 static struct file_operations sd_fops = {
 168     NULL,                        /* lseek - default */
 169     block_read,                  /* read - general block-dev read */
 170     block_write,                 /* write - general block-dev write */
 171     NULL,                        /* readdir - bad */
 172     NULL,                        /* select */
 173     sd_ioctl,                    /* ioctl */
 174     NULL,                        /* mmap */
 175     sd_open,                     /* open code */
 176     sd_release,                  /* release */
 177     block_fsync,                 /* fsync */
 178     NULL,                        /* fasync */
 179     check_scsidisk_media_change, /* Disk change */
 180     fop_revalidate_scsidisk      /* revalidate */
 181 };
 182 
 183 static struct gendisk sd_gendisk = {
 184     MAJOR_NR,                    /* Major number */
 185     "sd",                        /* Major name */
 186     4,                           /* Bits to shift to get real from partition */
 187     1 << 4,                      /* Number of partitions per real */
 188     0,                           /* maximum number of real */
 189     sd_geninit,                  /* init function */
 190     NULL,                        /* hd struct */
 191     NULL,                        /* block sizes */
 192     0,                           /* number */
 193     NULL,                        /* internal */
 194     NULL                         /* next */
 195 };
 196 
 197 static void sd_geninit (struct gendisk *ignored)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199     int i;
 200     
 201     for (i = 0; i < sd_template.dev_max; ++i)
 202         if(rscsi_disks[i].device) 
 203             sd[i << 4].nr_sects = rscsi_disks[i].capacity;
 204 #if 0
 205     /* No longer needed - we keep track of this as we attach/detach */
 206     sd_gendisk.nr_real = sd_template.dev_max;
 207 #endif
 208 }
 209 
 210 /*
 211  * rw_intr is the interrupt routine for the device driver.  It will
 212  * be notified on the end of a SCSI read / write, and
 213  * will take on of several actions based on success or failure.
 214  */
 215 
 216 static void rw_intr (Scsi_Cmnd *SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 217 {
 218     int result = SCpnt->result;
 219     int this_count = SCpnt->bufflen >> 9;
 220     
 221 #ifdef DEBUG
 222     printk("sd%c : rw_intr(%d, %d)\n", 'a' + MINOR(SCpnt->request.rq_dev), 
 223            SCpnt->host->host_no, result);
 224 #endif
 225     
 226     /*
 227      * First case : we assume that the command succeeded.  One of two things 
 228      * will happen here.  Either we will be finished, or there will be more
 229      * sectors that we were unable to read last time.
 230      */
 231 
 232     if (!result) {
 233         
 234 #ifdef DEBUG
 235         printk("sd%c : %d sectors remain.\n", 'a' + MINOR(SCpnt->request.rq_dev),
 236                SCpnt->request.nr_sectors);
 237         printk("use_sg is %d\n ",SCpnt->use_sg);
 238 #endif
 239         if (SCpnt->use_sg) {
 240             struct scatterlist * sgpnt;
 241             int i;
 242             sgpnt = (struct scatterlist *) SCpnt->buffer;
 243             for(i=0; i<SCpnt->use_sg; i++) {
 244 #ifdef DEBUG
 245                 printk(":%x %x %d\n",sgpnt[i].alt_address, sgpnt[i].address, 
 246                        sgpnt[i].length);
 247 #endif
 248                 if (sgpnt[i].alt_address) {
 249                     if (SCpnt->request.cmd == READ)
 250                         memcpy(sgpnt[i].alt_address, sgpnt[i].address, 
 251                                sgpnt[i].length);
 252                     scsi_free(sgpnt[i].address, sgpnt[i].length);
 253                 }
 254             }
 255 
 256             /* Free list of scatter-gather pointers */
 257             scsi_free(SCpnt->buffer, SCpnt->sglist_len);  
 258         } else {
 259             if (SCpnt->buffer != SCpnt->request.buffer) {
 260 #ifdef DEBUG
 261                 printk("nosg: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
 262                        SCpnt->bufflen);
 263 #endif  
 264                 if (SCpnt->request.cmd == READ)
 265                     memcpy(SCpnt->request.buffer, SCpnt->buffer,
 266                            SCpnt->bufflen);
 267                 scsi_free(SCpnt->buffer, SCpnt->bufflen);
 268             }
 269         }
 270         /*
 271          * If multiple sectors are requested in one buffer, then
 272          * they will have been finished off by the first command.
 273          * If not, then we have a multi-buffer command.
 274          */
 275         if (SCpnt->request.nr_sectors > this_count)
 276         {
 277             SCpnt->request.errors = 0;
 278             
 279             if (!SCpnt->request.bh)
 280             {
 281 #ifdef DEBUG
 282                 printk("sd%c : handling page request, no buffer\n",
 283                        'a' + MINOR(SCpnt->request.rq_dev));
 284 #endif
 285                 /*
 286                  * The SCpnt->request.nr_sectors field is always done in 
 287                  * 512 byte sectors, even if this really isn't the case.
 288                  */
 289                 panic("sd.c: linked page request (%lx %x)",
 290                       SCpnt->request.sector, this_count);
 291             }
 292         }
 293         SCpnt = end_scsi_request(SCpnt, 1, this_count);
 294         requeue_sd_request(SCpnt);
 295         return;
 296     }
 297     
 298     /* Free up any indirection buffers we allocated for DMA purposes. */
 299     if (SCpnt->use_sg) {
 300         struct scatterlist * sgpnt;
 301         int i;
 302         sgpnt = (struct scatterlist *) SCpnt->buffer;
 303         for(i=0; i<SCpnt->use_sg; i++) {
 304 #ifdef DEBUG
 305             printk("err: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
 306                    SCpnt->bufflen);
 307 #endif
 308             if (sgpnt[i].alt_address) {
 309                 scsi_free(sgpnt[i].address, sgpnt[i].length);
 310             }
 311         }
 312         scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
 313     } else {
 314 #ifdef DEBUG
 315         printk("nosgerr: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
 316                SCpnt->bufflen);
 317 #endif
 318         if (SCpnt->buffer != SCpnt->request.buffer)
 319             scsi_free(SCpnt->buffer, SCpnt->bufflen);
 320     }
 321     
 322     /*
 323      * Now, if we were good little boys and girls, Santa left us a request
 324      * sense buffer.  We can extract information from this, so we
 325      * can choose a block to remap, etc.
 326      */
 327 
 328     if (driver_byte(result) != 0) {
 329         if (suggestion(result) == SUGGEST_REMAP) {
 330 #ifdef REMAP
 331             /*
 332              * Not yet implemented.  A read will fail after being remapped,
 333              * a write will call the strategy routine again.
 334              */
 335             if rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].remap
 336             {
 337                 result = 0;
 338             }
 339             else
 340 #endif
 341         }
 342         
 343         if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
 344             if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
 345                 if(rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->removable) {
 346                     /* detected disc change.  set a bit and quietly refuse
 347                      * further access.
 348                      */  
 349                     rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->changed = 1;
 350                     SCpnt = end_scsi_request(SCpnt, 0, this_count);
 351                     requeue_sd_request(SCpnt);
 352                     return;
 353                 }
 354                 else
 355                 {
 356                     /*
 357                      * Must have been a power glitch, or a bus reset.
 358                      * Could not have been a media change, so we just retry
 359                      * the request and see what happens.
 360                      */
 361                     requeue_sd_request(SCpnt);
 362                     return;
 363                 }
 364             }
 365         }
 366         
 367         
 368         /* If we had an ILLEGAL REQUEST returned, then we may have
 369          * performed an unsupported command.  The only thing this should be 
 370          * would be a ten byte read where only a six byte read was supported.
 371          * Also, on a system where READ CAPACITY failed, we have have read 
 372          * past the end of the disk. 
 373          */
 374 
 375         if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
 376             if (rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].ten) {
 377                 rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].ten = 0;
 378                 requeue_sd_request(SCpnt);
 379                 result = 0;
 380             } else {
 381                 /* ???? */
 382             }
 383         }
 384     }  /* driver byte != 0 */
 385     if (result) {
 386         printk("SCSI disk error : host %d channel %d id %d lun %d return code = %x\n",
 387                rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->host->host_no,
 388                rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->channel,
 389            rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->id,
 390              rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->lun, result);
 391         
 392         if (driver_byte(result) & DRIVER_SENSE)
 393             print_sense("sd", SCpnt);
 394         SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
 395         requeue_sd_request(SCpnt);
 396         return;
 397     }
 398 }
 399 
 400 /*
 401  * requeue_sd_request() is the request handler function for the sd driver.
 402  * Its function in life is to take block device requests, and translate
 403  * them to SCSI commands.
 404  */
 405 
 406 static void do_sd_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 407 {
 408     Scsi_Cmnd * SCpnt = NULL;
 409     Scsi_Device * SDev;
 410     struct request * req = NULL;
 411     unsigned long flags;
 412     int flag = 0;
 413     
 414     save_flags(flags);
 415     while (1==1){
 416         cli();
 417         if (CURRENT != NULL && CURRENT->rq_status == RQ_INACTIVE) {
 418             restore_flags(flags);
 419             return;
 420         }
 421         
 422         INIT_SCSI_REQUEST;
 423         SDev = rscsi_disks[DEVICE_NR(CURRENT->rq_dev)].device;
 424         
 425         /*
 426          * I am not sure where the best place to do this is.  We need
 427          * to hook in a place where we are likely to come if in user
 428          * space.
 429          */
 430         if( SDev->was_reset )
 431         {
 432             /*
 433              * We need to relock the door, but we might
 434              * be in an interrupt handler.  Only do this
 435              * from user space, since we do not want to
 436              * sleep from an interrupt.
 437              */
 438             if( SDev->removable && !intr_count )
 439             {
 440                 scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, 0);
 441             }
 442             SDev->was_reset = 0;
 443         }
 444                 
 445         /* We have to be careful here. allocate_device will get a free pointer,
 446          * but there is no guarantee that it is queueable.  In normal usage, 
 447          * we want to call this, because other types of devices may have the 
 448          * host all tied up, and we want to make sure that we have at least 
 449          * one request pending for this type of device. We can also come 
 450          * through here while servicing an interrupt, because of the need to 
 451          * start another command. If we call allocate_device more than once, 
 452          * then the system can wedge if the command is not queueable. The 
 453          * request_queueable function is safe because it checks to make sure 
 454          * that the host is able to take another command before it returns
 455          * a pointer.  
 456          */
 457 
 458         if (flag++ == 0)
 459             SCpnt = allocate_device(&CURRENT,
 460                                     rscsi_disks[DEVICE_NR(CURRENT->rq_dev)].device, 0); 
 461         else SCpnt = NULL;
 462         
 463         /*
 464          * The following restore_flags leads to latency problems.  FIXME.
 465          * Using a "sti()" gets rid of the latency problems but causes
 466          * race conditions and crashes.
 467          */
 468         restore_flags(flags);
 469 
 470         /* This is a performance enhancement. We dig down into the request 
 471          * list and try to find a queueable request (i.e. device not busy, 
 472          * and host able to accept another command. If we find one, then we 
 473          * queue it. This can make a big difference on systems with more than 
 474          * one disk drive.  We want to have the interrupts off when monkeying 
 475          * with the request list, because otherwise the kernel might try to 
 476          * slip in a request in between somewhere. 
 477          */
 478 
 479         if (!SCpnt && sd_template.nr_dev > 1){
 480             struct request *req1;
 481             req1 = NULL;
 482             cli();
 483             req = CURRENT;
 484             while(req){
 485                 SCpnt = request_queueable(req, rscsi_disks[DEVICE_NR(req->rq_dev)].device);
 486                 if(SCpnt) break;
 487                 req1 = req;
 488                 req = req->next;
 489             }
 490             if (SCpnt && req->rq_status == RQ_INACTIVE) {
 491                 if (req == CURRENT) 
 492                     CURRENT = CURRENT->next;
 493                 else
 494                     req1->next = req->next;
 495             }
 496             restore_flags(flags);
 497         }
 498         
 499         if (!SCpnt) return; /* Could not find anything to do */
 500         
 501         /* Queue command */
 502         requeue_sd_request(SCpnt);
 503     }  /* While */
 504 }    
 505 
 506 static void requeue_sd_request (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 507 {
 508     int dev, devm, block, this_count;
 509     unsigned char cmd[10];
 510     int bounce_size, contiguous;
 511     int max_sg;
 512     struct buffer_head * bh, *bhp;
 513     char * buff, *bounce_buffer;
 514     
 515  repeat:
 516     
 517     if(!SCpnt || SCpnt->request.rq_status == RQ_INACTIVE) {
 518         do_sd_request();
 519         return;
 520     }
 521     
 522     devm =  MINOR(SCpnt->request.rq_dev);
 523     dev = DEVICE_NR(SCpnt->request.rq_dev);
 524 
 525     block = SCpnt->request.sector;
 526     this_count = 0;
 527 
 528 #ifdef DEBUG
 529     printk("Doing sd request, dev = %d, block = %d\n", devm, block);
 530 #endif
 531     
 532     if (devm >= (sd_template.dev_max << 4) || 
 533         !rscsi_disks[dev].device ||
 534         block + SCpnt->request.nr_sectors > sd[devm].nr_sects)
 535     {
 536         SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 537         goto repeat;
 538     }
 539     
 540     block += sd[devm].start_sect;
 541     
 542     if (rscsi_disks[dev].device->changed)
 543     {
 544         /*
 545          * quietly refuse to do anything to a changed disc until the changed 
 546          * bit has been reset
 547          */
 548         /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
 549         SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 550         goto repeat;
 551     }
 552     
 553 #ifdef DEBUG
 554     printk("sd%c : real dev = /dev/sd%c, block = %d\n", 
 555            'a' + devm, dev, block);
 556 #endif
 557     
 558     /*
 559      * If we have a 1K hardware sectorsize, prevent access to single
 560      * 512 byte sectors.  In theory we could handle this - in fact
 561      * the scsi cdrom driver must be able to handle this because
 562      * we typically use 1K blocksizes, and cdroms typically have
 563      * 2K hardware sectorsizes.  Of course, things are simpler
 564      * with the cdrom, since it is read-only.  For performance
 565      * reasons, the filesystems should be able to handle this
 566      * and not force the scsi disk driver to use bounce buffers
 567      * for this.
 568      */
 569     if (rscsi_disks[dev].sector_size == 1024)
 570         if((block & 1) || (SCpnt->request.nr_sectors & 1)) {
 571             printk("sd.c:Bad block number requested");
 572             SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 573             goto repeat;
 574         }
 575     
 576     switch (SCpnt->request.cmd)
 577     {
 578     case WRITE :
 579         if (!rscsi_disks[dev].device->writeable)
 580         {
 581             SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 582             goto repeat;
 583         }
 584         cmd[0] = WRITE_6;
 585         break;
 586     case READ :
 587         cmd[0] = READ_6;
 588         break;
 589     default :
 590         panic ("Unknown sd command %d\n", SCpnt->request.cmd);
 591     }
 592     
 593     SCpnt->this_count = 0;
 594     
 595     /* If the host adapter can deal with very large scatter-gather
 596      * requests, it is a waste of time to cluster 
 597      */
 598     contiguous = (!CLUSTERABLE_DEVICE(SCpnt) ? 0 :1);
 599     bounce_buffer = NULL;
 600     bounce_size = (SCpnt->request.nr_sectors << 9);
 601     
 602     /* First see if we need a bounce buffer for this request. If we do, make 
 603      * sure that we can allocate a buffer. Do not waste space by allocating 
 604      * a bounce buffer if we are straddling the 16Mb line 
 605      */ 
 606     if (contiguous && SCpnt->request.bh &&
 607         ((long) SCpnt->request.bh->b_data) 
 608         + (SCpnt->request.nr_sectors << 9) - 1 > ISA_DMA_THRESHOLD 
 609         && SCpnt->host->unchecked_isa_dma) {
 610         if(((long) SCpnt->request.bh->b_data) > ISA_DMA_THRESHOLD)
 611             bounce_buffer = (char *) scsi_malloc(bounce_size);
 612         if(!bounce_buffer) contiguous = 0;
 613     }
 614     
 615     if(contiguous && SCpnt->request.bh && SCpnt->request.bh->b_reqnext)
 616         for(bh = SCpnt->request.bh, bhp = bh->b_reqnext; bhp; bh = bhp, 
 617             bhp = bhp->b_reqnext) {
 618             if(!CONTIGUOUS_BUFFERS(bh,bhp)) { 
 619                 if(bounce_buffer) scsi_free(bounce_buffer, bounce_size);
 620                 contiguous = 0;
 621                 break;
 622             } 
 623         }
 624     if (!SCpnt->request.bh || contiguous) {
 625         
 626         /* case of page request (i.e. raw device), or unlinked buffer */
 627         this_count = SCpnt->request.nr_sectors;
 628         buff = SCpnt->request.buffer;
 629         SCpnt->use_sg = 0;
 630         
 631     } else if (SCpnt->host->sg_tablesize == 0 ||
 632                (need_isa_buffer && dma_free_sectors <= 10)) {
 633         
 634         /* Case of host adapter that cannot scatter-gather.  We also
 635          * come here if we are running low on DMA buffer memory.  We set
 636          * a threshold higher than that we would need for this request so
 637          * we leave room for other requests.  Even though we would not need
 638          * it all, we need to be conservative, because if we run low enough
 639          * we have no choice but to panic. 
 640          */
 641         if (SCpnt->host->sg_tablesize != 0 &&
 642             need_isa_buffer && 
 643             dma_free_sectors <= 10)
 644             printk("Warning: SCSI DMA buffer space running low.  Using non scatter-gather I/O.\n");
 645         
 646         this_count = SCpnt->request.current_nr_sectors;
 647         buff = SCpnt->request.buffer;
 648         SCpnt->use_sg = 0;
 649         
 650     } else {
 651         
 652         /* Scatter-gather capable host adapter */
 653         struct scatterlist * sgpnt;
 654         int count, this_count_max;
 655         int counted;
 656         
 657         bh = SCpnt->request.bh;
 658         this_count = 0;
 659         this_count_max = (rscsi_disks[dev].ten ? 0xffff : 0xff);
 660         count = 0;
 661         bhp = NULL;
 662         while(bh) {
 663             if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
 664             if(!bhp || !CONTIGUOUS_BUFFERS(bhp,bh) ||
 665                !CLUSTERABLE_DEVICE(SCpnt) ||
 666                (SCpnt->host->unchecked_isa_dma &&
 667                 ((unsigned long) bh->b_data-1) == ISA_DMA_THRESHOLD)) {
 668                 if (count < SCpnt->host->sg_tablesize) count++;
 669                 else break;
 670             }
 671             this_count += (bh->b_size >> 9);
 672             bhp = bh;
 673             bh = bh->b_reqnext;
 674         }
 675 #if 0
 676         if(SCpnt->host->unchecked_isa_dma &&
 677            ((unsigned int) SCpnt->request.bh->b_data-1) == ISA_DMA_THRESHOLD) count--;
 678 #endif
 679         SCpnt->use_sg = count;  /* Number of chains */
 680         count = 512;/* scsi_malloc can only allocate in chunks of 512 bytes */
 681         while( count < (SCpnt->use_sg * sizeof(struct scatterlist))) 
 682             count = count << 1;
 683         SCpnt->sglist_len = count;
 684         max_sg = count / sizeof(struct scatterlist);
 685         if(SCpnt->host->sg_tablesize < max_sg) 
 686             max_sg = SCpnt->host->sg_tablesize;
 687         sgpnt = (struct scatterlist * ) scsi_malloc(count);
 688         if (!sgpnt) {
 689             printk("Warning - running *really* short on DMA buffers\n");
 690             SCpnt->use_sg = 0;    /* No memory left - bail out */
 691             this_count = SCpnt->request.current_nr_sectors;
 692             buff = SCpnt->request.buffer;
 693         } else {
 694             memset(sgpnt, 0, count);  /* Zero so it is easy to fill, but only
 695                                        * if memory is available 
 696                                        */
 697             buff = (char *) sgpnt;
 698             counted = 0;
 699             for(count = 0, bh = SCpnt->request.bh, bhp = bh->b_reqnext;
 700                 count < SCpnt->use_sg && bh; 
 701                 count++, bh = bhp) {
 702                 
 703                 bhp = bh->b_reqnext;
 704                 
 705                 if(!sgpnt[count].address) sgpnt[count].address = bh->b_data;
 706                 sgpnt[count].length += bh->b_size;
 707                 counted += bh->b_size >> 9;
 708                 
 709                 if (((long) sgpnt[count].address) + sgpnt[count].length - 1 > 
 710                     ISA_DMA_THRESHOLD && (SCpnt->host->unchecked_isa_dma) &&
 711                     !sgpnt[count].alt_address) {
 712                     sgpnt[count].alt_address = sgpnt[count].address;
 713                     /* We try to avoid exhausting the DMA pool, since it is 
 714                      * easier to control usage here. In other places we might 
 715                      * have a more pressing need, and we would be screwed if 
 716                      * we ran out */
 717                     if(dma_free_sectors < (sgpnt[count].length >> 9) + 10) {
 718                         sgpnt[count].address = NULL;
 719                     } else {
 720                         sgpnt[count].address = 
 721                             (char *) scsi_malloc(sgpnt[count].length);
 722                     }
 723                     /* If we start running low on DMA buffers, we abort the 
 724                      * scatter-gather operation, and free all of the memory 
 725                      * we have allocated.  We want to ensure that all scsi 
 726                      * operations are able to do at least a non-scatter/gather
 727                      * operation */
 728                     if(sgpnt[count].address == NULL){ /* Out of dma memory */
 729 #if 0
 730                         printk("Warning: Running low on SCSI DMA buffers");
 731                         /* Try switching back to a non s-g operation. */
 732                         while(--count >= 0){
 733                             if(sgpnt[count].alt_address) 
 734                                 scsi_free(sgpnt[count].address, 
 735                                           sgpnt[count].length);
 736                         }
 737                         this_count = SCpnt->request.current_nr_sectors;
 738                         buff = SCpnt->request.buffer;
 739                         SCpnt->use_sg = 0;
 740                         scsi_free(sgpnt, SCpnt->sglist_len);
 741 #endif
 742                         SCpnt->use_sg = count;
 743                         this_count = counted -= bh->b_size >> 9;
 744                         break;
 745                     }               
 746                 }
 747                 
 748                 /* Only cluster buffers if we know that we can supply DMA 
 749                  * buffers large enough to satisfy the request. Do not cluster
 750                  * a new request if this would mean that we suddenly need to 
 751                  * start using DMA bounce buffers */
 752                 if(bhp && CONTIGUOUS_BUFFERS(bh,bhp) 
 753                    && CLUSTERABLE_DEVICE(SCpnt)) {
 754                     char * tmp;
 755                     
 756                     if (((long) sgpnt[count].address) + sgpnt[count].length +
 757                         bhp->b_size - 1 > ISA_DMA_THRESHOLD && 
 758                         (SCpnt->host->unchecked_isa_dma) &&
 759                         !sgpnt[count].alt_address) continue;
 760                     
 761                     if(!sgpnt[count].alt_address) {count--; continue; }
 762                     if(dma_free_sectors > 10)
 763                         tmp = (char *) scsi_malloc(sgpnt[count].length 
 764                                                    + bhp->b_size);
 765                     else {
 766                         tmp = NULL;
 767                         max_sg = SCpnt->use_sg;
 768                     }
 769                     if(tmp){
 770                         scsi_free(sgpnt[count].address, sgpnt[count].length);
 771                         sgpnt[count].address = tmp;
 772                         count--;
 773                         continue;
 774                     }
 775                     
 776                     /* If we are allowed another sg chain, then increment 
 777                      * counter so we can insert it.  Otherwise we will end 
 778                      up truncating */
 779                     
 780                     if (SCpnt->use_sg < max_sg) SCpnt->use_sg++;
 781                 }  /* contiguous buffers */
 782             } /* for loop */
 783             
 784             /* This is actually how many we are going to transfer */
 785             this_count = counted; 
 786             
 787             if(count < SCpnt->use_sg || SCpnt->use_sg 
 788                > SCpnt->host->sg_tablesize){
 789                 bh = SCpnt->request.bh;
 790                 printk("Use sg, count %d %x %d\n", 
 791                        SCpnt->use_sg, count, dma_free_sectors);
 792                 printk("maxsg = %x, counted = %d this_count = %d\n", 
 793                        max_sg, counted, this_count);
 794                 while(bh){
 795                     printk("[%p %lx] ", bh->b_data, bh->b_size);
 796                     bh = bh->b_reqnext;
 797                 }
 798                 if(SCpnt->use_sg < 16)
 799                     for(count=0; count<SCpnt->use_sg; count++)
 800                         printk("{%d:%p %p %d}  ", count,
 801                                sgpnt[count].address,
 802                                sgpnt[count].alt_address,
 803                                sgpnt[count].length);
 804                 panic("Ooops");
 805             }
 806             
 807             if (SCpnt->request.cmd == WRITE)
 808                 for(count=0; count<SCpnt->use_sg; count++)
 809                     if(sgpnt[count].alt_address)
 810                         memcpy(sgpnt[count].address, sgpnt[count].alt_address, 
 811                                sgpnt[count].length);
 812         }  /* Able to malloc sgpnt */
 813     }  /* Host adapter capable of scatter-gather */
 814     
 815     /* Now handle the possibility of DMA to addresses > 16Mb */
 816     
 817     if(SCpnt->use_sg == 0){
 818         if (((long) buff) + (this_count << 9) - 1 > ISA_DMA_THRESHOLD && 
 819             (SCpnt->host->unchecked_isa_dma)) {
 820             if(bounce_buffer)
 821                 buff = bounce_buffer;
 822             else
 823                 buff = (char *) scsi_malloc(this_count << 9);
 824             if(buff == NULL) {  /* Try backing off a bit if we are low on mem*/
 825                 this_count = SCpnt->request.current_nr_sectors;
 826                 buff = (char *) scsi_malloc(this_count << 9);
 827                 if(!buff) panic("Ran out of DMA buffers.");
 828             }
 829             if (SCpnt->request.cmd == WRITE)
 830                 memcpy(buff, (char *)SCpnt->request.buffer, this_count << 9);
 831         }
 832     }
 833 #ifdef DEBUG
 834     printk("sd%c : %s %d/%d 512 byte blocks.\n", 
 835            'a' + devm,
 836            (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
 837            this_count, SCpnt->request.nr_sectors);
 838 #endif
 839     
 840     cmd[1] = (SCpnt->lun << 5) & 0xe0;
 841     
 842     if (rscsi_disks[dev].sector_size == 1024){
 843         if(block & 1) panic("sd.c:Bad block number requested");
 844         if(this_count & 1) panic("sd.c:Bad block number requested");
 845         block = block >> 1;
 846         this_count = this_count >> 1;
 847     }
 848     
 849     if (rscsi_disks[dev].sector_size == 256){
 850         block = block << 1;
 851         this_count = this_count << 1;
 852     }
 853     
 854     if (((this_count > 0xff) ||  (block > 0x1fffff)) && rscsi_disks[dev].ten)
 855     {
 856         if (this_count > 0xffff)
 857             this_count = 0xffff;
 858         
 859         cmd[0] += READ_10 - READ_6 ;
 860         cmd[2] = (unsigned char) (block >> 24) & 0xff;
 861         cmd[3] = (unsigned char) (block >> 16) & 0xff;
 862         cmd[4] = (unsigned char) (block >> 8) & 0xff;
 863         cmd[5] = (unsigned char) block & 0xff;
 864         cmd[6] = cmd[9] = 0;
 865         cmd[7] = (unsigned char) (this_count >> 8) & 0xff;
 866         cmd[8] = (unsigned char) this_count & 0xff;
 867     }
 868     else
 869     {
 870         if (this_count > 0xff)
 871             this_count = 0xff;
 872         
 873         cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
 874         cmd[2] = (unsigned char) ((block >> 8) & 0xff);
 875         cmd[3] = (unsigned char) block & 0xff;
 876         cmd[4] = (unsigned char) this_count;
 877         cmd[5] = 0;
 878     }
 879     
 880     /*
 881      * We shouldn't disconnect in the middle of a sector, so with a dumb 
 882      * host adapter, it's safe to assume that we can at least transfer 
 883      * this many bytes between each connect / disconnect.  
 884      */
 885     
 886     SCpnt->transfersize = rscsi_disks[dev].sector_size;
 887     SCpnt->underflow = this_count << 9; 
 888     scsi_do_cmd (SCpnt, (void *) cmd, buff, 
 889                  this_count * rscsi_disks[dev].sector_size,
 890                  rw_intr, 
 891                  (SCpnt->device->type == TYPE_DISK ? 
 892                   SD_TIMEOUT : SD_MOD_TIMEOUT),
 893                  MAX_RETRIES);
 894 }
 895 
 896 static int check_scsidisk_media_change(kdev_t full_dev){
     /* [previous][next][first][last][top][bottom][index][help] */
 897     int retval;
 898     int target;
 899     struct inode inode;
 900     int flag = 0;
 901     
 902     target =  DEVICE_NR(full_dev);
 903     
 904     if (target >= sd_template.dev_max ||
 905         !rscsi_disks[target].device) {
 906         printk("SCSI disk request error: invalid device.\n");
 907         return 0;
 908     }
 909     
 910     if(!rscsi_disks[target].device->removable) return 0;
 911     
 912     inode.i_rdev = full_dev;  /* This is all we really need here */
 913     retval = sd_ioctl(&inode, NULL, SCSI_IOCTL_TEST_UNIT_READY, 0);
 914     
 915     if(retval){ /* Unable to test, unit probably not ready.  This usually
 916                  * means there is no disc in the drive.  Mark as changed,
 917                  * and we will figure it out later once the drive is
 918                  * available again.  */
 919         
 920         rscsi_disks[target].ready = 0;
 921         rscsi_disks[target].device->changed = 1;
 922         return 1; /* This will force a flush, if called from
 923                    * check_disk_change */
 924     }
 925     
 926     /* 
 927      * for removable scsi disk ( FLOPTICAL ) we have to recognise the
 928      * presence of disk in the drive. This is kept in the Scsi_Disk
 929      * struct and tested at open !  Daniel Roche ( dan@lectra.fr ) 
 930      */
 931     
 932     rscsi_disks[target].ready = 1;      /* FLOPTICAL */
 933 
 934     retval = rscsi_disks[target].device->changed;
 935     if(!flag) rscsi_disks[target].device->changed = 0;
 936     return retval;
 937 }
 938 
 939 static void sd_init_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 940 {
 941     struct request * req;
 942     
 943     req = &SCpnt->request;
 944     req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
 945     
 946     if (req->sem != NULL) {
 947         up(req->sem);
 948     }
 949 }
 950 
 951 static int sd_init_onedisk(int i)
     /* [previous][next][first][last][top][bottom][index][help] */
 952 {
 953     unsigned char cmd[10];
 954     unsigned char *buffer;
 955     unsigned long spintime;
 956     int the_result, retries;
 957     Scsi_Cmnd * SCpnt;
 958     
 959     /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is 
 960      * considered a fatal error, and many devices report such an error 
 961      * just after a scsi bus reset. 
 962      */
 963     
 964     SCpnt = allocate_device(NULL, rscsi_disks[i].device, 1);
 965     buffer = (unsigned char *) scsi_malloc(512);
 966     
 967     spintime = 0;
 968     
 969     /* Spin up drives, as required.  Only do this at boot time */
 970     if (!MODULE_FLAG){
 971         do{
 972             retries = 0;
 973             while(retries < 3)
 974             {
 975                 cmd[0] = TEST_UNIT_READY;
 976                 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
 977                 memset ((void *) &cmd[2], 0, 8);
 978                 SCpnt->cmd_len = 0;
 979                 SCpnt->sense_buffer[0] = 0;
 980                 SCpnt->sense_buffer[2] = 0;
 981 
 982                 {
 983                     struct semaphore sem = MUTEX_LOCKED;
 984                     /* Mark as really busy again */
 985                     SCpnt->request.rq_status = RQ_SCSI_BUSY;
 986                     SCpnt->request.sem = &sem;
 987                     scsi_do_cmd (SCpnt,
 988                                  (void *) cmd, (void *) buffer,
 989                                  512, sd_init_done,  SD_TIMEOUT,
 990                                  MAX_RETRIES);
 991                     down(&sem);
 992                 }
 993 
 994                 the_result = SCpnt->result;
 995                 retries++;
 996                 if(   the_result == 0
 997                    || SCpnt->sense_buffer[2] != UNIT_ATTENTION)
 998                     break;
 999             }
1000             
1001             /* Look for non-removable devices that return NOT_READY.  
1002              * Issue command to spin up drive for these cases. */
1003             if(the_result && !rscsi_disks[i].device->removable && 
1004                SCpnt->sense_buffer[2] == NOT_READY) {
1005                 int time1;
1006                 if(!spintime){
1007                     printk( "sd%c: Spinning up disk...", 'a' + i );
1008                     cmd[0] = START_STOP;
1009                     cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
1010                     cmd[1] |= 1;  /* Return immediately */
1011                     memset ((void *) &cmd[2], 0, 8);
1012                     cmd[4] = 1; /* Start spin cycle */
1013                     SCpnt->cmd_len = 0;
1014                     SCpnt->sense_buffer[0] = 0;
1015                     SCpnt->sense_buffer[2] = 0;
1016                     
1017                     {
1018                         struct semaphore sem = MUTEX_LOCKED;
1019                         /* Mark as really busy again */
1020                         SCpnt->request.rq_status = RQ_SCSI_BUSY; 
1021                         SCpnt->request.sem = &sem;
1022                         scsi_do_cmd (SCpnt,
1023                                      (void *) cmd, (void *) buffer,
1024                                      512, sd_init_done,  SD_TIMEOUT,
1025                                      MAX_RETRIES);
1026                         down(&sem);
1027                     }
1028                     
1029                     spintime = jiffies;
1030                 }
1031                 
1032                 time1 = jiffies;
1033                 while(jiffies < time1 + HZ); /* Wait 1 second for next try */
1034                 printk( "." );
1035             }
1036         } while(the_result && spintime && spintime+100*HZ > jiffies);
1037         if (spintime) {
1038             if (the_result)
1039                 printk( "not responding...\n" );
1040             else
1041                 printk( "ready\n" );
1042         }
1043     }  /* !MODULE_FLAG */
1044     
1045     retries = 3;
1046     do {
1047         cmd[0] = READ_CAPACITY;
1048         cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
1049         memset ((void *) &cmd[2], 0, 8);
1050         memset ((void *) buffer, 0, 8);
1051         SCpnt->cmd_len = 0;
1052         SCpnt->sense_buffer[0] = 0;
1053         SCpnt->sense_buffer[2] = 0;
1054 
1055         {
1056             struct semaphore sem = MUTEX_LOCKED;
1057             /* Mark as really busy again */
1058             SCpnt->request.rq_status = RQ_SCSI_BUSY;
1059             SCpnt->request.sem = &sem;
1060             scsi_do_cmd (SCpnt,
1061                          (void *) cmd, (void *) buffer,
1062                          8, sd_init_done,  SD_TIMEOUT,
1063                          MAX_RETRIES);
1064             down(&sem); /* sleep until it is ready */
1065         }
1066         
1067         the_result = SCpnt->result;
1068         retries--;
1069         
1070     } while(the_result && retries);
1071     
1072     SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1073     
1074     wake_up(&SCpnt->device->device_wait); 
1075     
1076     /* Wake up a process waiting for device */
1077     
1078     /*
1079      * The SCSI standard says: 
1080      * "READ CAPACITY is necessary for self configuring software"
1081      *  While not mandatory, support of READ CAPACITY is strongly encouraged.
1082      *  We used to die if we couldn't successfully do a READ CAPACITY.
1083      *  But, now we go on about our way.  The side effects of this are
1084      *
1085      *  1. We can't know block size with certainty. I have said "512 bytes 
1086      *     is it" as this is most common.
1087      *
1088      *  2. Recovery from when some one attempts to read past the end of the 
1089      *     raw device will be slower.
1090      */
1091     
1092     if (the_result)
1093     {
1094         printk ("sd%c : READ CAPACITY failed.\n"
1095                 "sd%c : status = %x, message = %02x, host = %d, driver = %02x \n",
1096                 'a' + i, 'a' + i,
1097                 status_byte(the_result),
1098                 msg_byte(the_result),
1099                 host_byte(the_result),
1100                 driver_byte(the_result)
1101                 );
1102         if (driver_byte(the_result)  & DRIVER_SENSE)
1103             printk("sd%c : extended sense code = %1x \n", 
1104                    'a' + i, SCpnt->sense_buffer[2] & 0xf);
1105         else
1106             printk("sd%c : sense not available. \n", 'a' + i);
1107         
1108         printk("sd%c : block size assumed to be 512 bytes, disk size 1GB.  \n",
1109                'a' + i);
1110         rscsi_disks[i].capacity = 0x1fffff;
1111         rscsi_disks[i].sector_size = 512;
1112         
1113         /* Set dirty bit for removable devices if not ready - sometimes drives
1114          * will not report this properly. */
1115         if(rscsi_disks[i].device->removable && 
1116            SCpnt->sense_buffer[2] == NOT_READY)
1117             rscsi_disks[i].device->changed = 1;
1118         
1119     }
1120     else
1121     {
1122         /*
1123          * FLOPTICAL , if read_capa is ok , drive is assumed to be ready 
1124          */
1125         rscsi_disks[i].ready = 1;
1126 
1127         rscsi_disks[i].capacity = 1 + ((buffer[0] << 24) |
1128                                        (buffer[1] << 16) |
1129                                        (buffer[2] << 8) |
1130                                        buffer[3]);
1131         
1132         rscsi_disks[i].sector_size = (buffer[4] << 24) |
1133             (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1134 
1135         if (rscsi_disks[i].sector_size == 0) {
1136           rscsi_disks[i].sector_size = 512;
1137           printk("sd%c : sector size 0 reported, assuming 512.\n", 'a' + i);
1138         }
1139  
1140         
1141         if (rscsi_disks[i].sector_size != 512 &&
1142             rscsi_disks[i].sector_size != 1024 &&
1143             rscsi_disks[i].sector_size != 256)
1144         {
1145             printk ("sd%c : unsupported sector size %d.\n",
1146                     'a' + i, rscsi_disks[i].sector_size);
1147             if(rscsi_disks[i].device->removable){
1148                 rscsi_disks[i].capacity = 0;
1149             } else {
1150                 printk ("scsi : deleting disk entry.\n");
1151                 rscsi_disks[i].device = NULL;
1152                 sd_template.nr_dev--;
1153                 return i;
1154             }
1155         }
1156     {
1157         /*
1158          * The msdos fs needs to know the hardware sector size
1159          * So I have created this table. See ll_rw_blk.c
1160          * Jacques Gelinas (Jacques@solucorp.qc.ca)
1161          */
1162         int m, mb;
1163         int sz_quot, sz_rem;
1164         int hard_sector = rscsi_disks[i].sector_size;
1165         /* There are 16 minors allocated for each major device */
1166         for (m=i<<4; m<((i+1)<<4); m++){
1167             sd_hardsizes[m] = hard_sector;
1168         }
1169         mb = rscsi_disks[i].capacity / 1024 * hard_sector / 1024;
1170         /* sz = div(m/100, 10);  this seems to not be in the libr */
1171         m = (mb + 50) / 100;
1172         sz_quot = m / 10;
1173         sz_rem = m - (10 * sz_quot);
1174         printk ("SCSI device sd%c: hdwr sector= %d bytes."
1175                " Sectors= %d [%d MB] [%d.%1d GB]\n",
1176                 i+'a', hard_sector, rscsi_disks[i].capacity, 
1177                 mb, sz_quot, sz_rem);
1178     }
1179         if(rscsi_disks[i].sector_size == 1024)
1180             rscsi_disks[i].capacity <<= 1;  /* Change into 512 byte sectors */
1181         if(rscsi_disks[i].sector_size == 256)
1182             rscsi_disks[i].capacity >>= 1;  /* Change into 512 byte sectors */
1183     }
1184     
1185 
1186     /*
1187      * Unless otherwise specified, this is not write protected.
1188      */
1189     rscsi_disks[i].write_prot = 0;
1190     if ( rscsi_disks[i].device->removable && rscsi_disks[i].ready ) {
1191         /* FLOPTICAL */
1192 
1193         /* 
1194          *      for removable scsi disk ( FLOPTICAL ) we have to recognise  
1195          * the Write Protect Flag. This flag is kept in the Scsi_Disk struct
1196          * and tested at open !
1197          * Daniel Roche ( dan@lectra.fr )
1198          */
1199         
1200         memset ((void *) &cmd[0], 0, 8);
1201         cmd[0] = MODE_SENSE;
1202         cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
1203         cmd[2] = 1;      /* page code 1 ?? */
1204         cmd[4] = 12;
1205         SCpnt->cmd_len = 0;
1206         SCpnt->sense_buffer[0] = 0;
1207         SCpnt->sense_buffer[2] = 0;
1208 
1209         /* same code as READCAPA !! */
1210         {
1211             struct semaphore sem = MUTEX_LOCKED;
1212             SCpnt->request.rq_status = RQ_SCSI_BUSY;  /* Mark as really busy again */
1213             SCpnt->request.sem = &sem;
1214             scsi_do_cmd (SCpnt,
1215                          (void *) cmd, (void *) buffer,
1216                          512, sd_init_done,  SD_TIMEOUT,
1217                          MAX_RETRIES);
1218             down(&sem);
1219         }
1220         
1221         the_result = SCpnt->result;
1222         SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1223         wake_up(&SCpnt->device->device_wait); 
1224         
1225         if ( the_result ) {
1226             printk ("sd%c: test WP failed, assume Write Protected\n",i+'a');
1227             rscsi_disks[i].write_prot = 1;
1228         } else {
1229             rscsi_disks[i].write_prot = ((buffer[2] & 0x80) != 0);
1230             printk ("sd%c: Write Protect is %s\n",i+'a',
1231                     rscsi_disks[i].write_prot ? "on" : "off");
1232         }
1233         
1234     }   /* check for write protect */
1235  
1236     rscsi_disks[i].ten = 1;
1237     rscsi_disks[i].remap = 1;
1238     scsi_free(buffer, 512);
1239     return i;
1240 }
1241 
1242 /*
1243  * The sd_init() function looks at all SCSI drives present, determines
1244  * their size, and reads partition table entries for them.
1245  */
1246 
1247 static int sd_registered = 0;
1248 
1249 static int sd_init()
     /* [previous][next][first][last][top][bottom][index][help] */
1250 {
1251     int i;
1252     
1253     if (sd_template.dev_noticed == 0) return 0;
1254     
1255     if(!sd_registered) {
1256           if (register_blkdev(MAJOR_NR,"sd",&sd_fops)) {
1257               printk("Unable to get major %d for SCSI disk\n",MAJOR_NR);
1258               return 1;
1259           }
1260           sd_registered++;
1261       }
1262     
1263     /* We do not support attaching loadable devices yet. */
1264     if(rscsi_disks) return 0;
1265     
1266     sd_template.dev_max = sd_template.dev_noticed + SD_EXTRA_DEVS;
1267     
1268     rscsi_disks = (Scsi_Disk *) 
1269         scsi_init_malloc(sd_template.dev_max * sizeof(Scsi_Disk), GFP_ATOMIC);
1270     memset(rscsi_disks, 0, sd_template.dev_max * sizeof(Scsi_Disk));
1271     
1272     sd_sizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * 
1273                                         sizeof(int), GFP_ATOMIC);
1274     memset(sd_sizes, 0, (sd_template.dev_max << 4) * sizeof(int));
1275     
1276     sd_blocksizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * 
1277                                              sizeof(int), GFP_ATOMIC);
1278     
1279     sd_hardsizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * 
1280                                             sizeof(int), GFP_ATOMIC);
1281     
1282     for(i=0;i<(sd_template.dev_max << 4);i++){
1283         sd_blocksizes[i] = 1024;
1284         sd_hardsizes[i] = 512;
1285     }
1286     blksize_size[MAJOR_NR] = sd_blocksizes;
1287     hardsect_size[MAJOR_NR] = sd_hardsizes;
1288     sd = (struct hd_struct *) scsi_init_malloc((sd_template.dev_max << 4) *
1289                                                sizeof(struct hd_struct),
1290                                                GFP_ATOMIC);
1291     
1292     
1293     sd_gendisk.max_nr = sd_template.dev_max;
1294     sd_gendisk.part = sd;
1295     sd_gendisk.sizes = sd_sizes;
1296     sd_gendisk.real_devices = (void *) rscsi_disks;
1297     return 0;
1298 }
1299 
1300 static void sd_finish()
     /* [previous][next][first][last][top][bottom][index][help] */
1301 {
1302     int i;
1303 
1304     blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1305     
1306     sd_gendisk.next = gendisk_head;
1307     gendisk_head = &sd_gendisk;
1308     
1309     for (i = 0; i < sd_template.dev_max; ++i)
1310         if (!rscsi_disks[i].capacity && 
1311             rscsi_disks[i].device)
1312         {
1313             if (MODULE_FLAG
1314                 && !rscsi_disks[i].has_part_table) {
1315                 sd_sizes[i << 4] = rscsi_disks[i].capacity;
1316                 /* revalidate does sd_init_onedisk via MAYBE_REINIT*/
1317                 revalidate_scsidisk(MKDEV(MAJOR_NR, i << 4), 0);
1318             }
1319             else
1320                 i=sd_init_onedisk(i);
1321             rscsi_disks[i].has_part_table = 1;
1322         }
1323     
1324     /* If our host adapter is capable of scatter-gather, then we increase
1325      * the read-ahead to 16 blocks (32 sectors).  If not, we use
1326      * a two block (4 sector) read ahead. 
1327      */
1328     if(rscsi_disks[0].device && rscsi_disks[0].device->host->sg_tablesize)
1329         read_ahead[MAJOR_NR] = 120;  /* 120 sector read-ahead */
1330     else
1331         read_ahead[MAJOR_NR] = 4;  /* 4 sector read-ahead */
1332 
1333     return;
1334 }
1335 
1336 static int sd_detect(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
1337     if(SDp->type != TYPE_DISK && SDp->type != TYPE_MOD) return 0;
1338     
1339     printk("Detected scsi disk sd%c at scsi%d, channel %d, id %d, lun %d\n", 
1340            'a'+ (sd_template.dev_noticed++),
1341            SDp->host->host_no, SDp->channel, SDp->id, SDp->lun); 
1342     
1343     return 1;
1344 }
1345 
1346 static int sd_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
1347     Scsi_Disk * dpnt;
1348     int i;
1349     
1350     if(SDp->type != TYPE_DISK && SDp->type != TYPE_MOD) return 0;
1351     
1352     if(sd_template.nr_dev >= sd_template.dev_max) {
1353         SDp->attached--;
1354         return 1;
1355     }
1356     
1357     for(dpnt = rscsi_disks, i=0; i<sd_template.dev_max; i++, dpnt++) 
1358         if(!dpnt->device) break;
1359     
1360     if(i >= sd_template.dev_max) panic ("scsi_devices corrupt (sd)");
1361     
1362     SDp->scsi_request_fn = do_sd_request;
1363     rscsi_disks[i].device = SDp;
1364     rscsi_disks[i].has_part_table = 0;
1365     sd_template.nr_dev++;
1366     sd_gendisk.nr_real++;
1367     return 0;
1368 }
1369 
1370 #define DEVICE_BUSY rscsi_disks[target].device->busy
1371 #define USAGE rscsi_disks[target].device->access_count
1372 #define CAPACITY rscsi_disks[target].capacity
1373 #define MAYBE_REINIT  sd_init_onedisk(target)
1374 #define GENDISK_STRUCT sd_gendisk
1375 
1376 /* This routine is called to flush all partitions and partition tables
1377  * for a changed scsi disk, and then re-read the new partition table.
1378  * If we are revalidating a disk because of a media change, then we
1379  * enter with usage == 0.  If we are using an ioctl, we automatically have
1380  * usage == 1 (we need an open channel to use an ioctl :-), so this
1381  * is our limit.
1382  */
1383 int revalidate_scsidisk(kdev_t dev, int maxusage){
     /* [previous][next][first][last][top][bottom][index][help] */
1384     int target;
1385     struct gendisk * gdev;
1386     unsigned long flags;
1387     int max_p;
1388     int start;
1389     int i;
1390     
1391     target =  DEVICE_NR(dev);
1392     gdev = &GENDISK_STRUCT;
1393     
1394     save_flags(flags);
1395     cli();
1396     if (DEVICE_BUSY || USAGE > maxusage) {
1397         restore_flags(flags);
1398         printk("Device busy for revalidation (usage=%d)\n", USAGE);
1399         return -EBUSY;
1400     }
1401     DEVICE_BUSY = 1;
1402     restore_flags(flags);
1403     
1404     max_p = gdev->max_p;
1405     start = target << gdev->minor_shift;
1406     
1407     for (i=max_p - 1; i >=0 ; i--) {
1408         int minor = start+i;
1409         kdev_t devi = MKDEV(MAJOR_NR, minor);
1410         sync_dev(devi);
1411         invalidate_inodes(devi);
1412         invalidate_buffers(devi);
1413         gdev->part[minor].start_sect = 0;
1414         gdev->part[minor].nr_sects = 0;
1415         /*
1416          * Reset the blocksize for everything so that we can read
1417          * the partition table.
1418          */
1419         blksize_size[MAJOR_NR][minor] = 1024;
1420     }
1421     
1422 #ifdef MAYBE_REINIT
1423     MAYBE_REINIT;
1424 #endif
1425     
1426     gdev->part[start].nr_sects = CAPACITY;
1427     resetup_one_dev(gdev, target);
1428     
1429     DEVICE_BUSY = 0;
1430     return 0;
1431 }
1432 
1433 static int fop_revalidate_scsidisk(kdev_t dev){
     /* [previous][next][first][last][top][bottom][index][help] */
1434     return revalidate_scsidisk(dev, 0);
1435 }
1436 
1437 
1438 static void sd_detach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
1439 {
1440     Scsi_Disk * dpnt;
1441     int i;
1442     int max_p;
1443     int start;
1444     
1445     for(dpnt = rscsi_disks, i=0; i<sd_template.dev_max; i++, dpnt++) 
1446         if(dpnt->device == SDp) {
1447             
1448             /* If we are disconnecting a disk driver, sync and invalidate 
1449              * everything */
1450             max_p = sd_gendisk.max_p;
1451             start = i << sd_gendisk.minor_shift;
1452             
1453             for (i=max_p - 1; i >=0 ; i--) {
1454                 int minor = start+i;
1455                 kdev_t devi = MKDEV(MAJOR_NR, minor);
1456                 sync_dev(devi);
1457                 invalidate_inodes(devi);
1458                 invalidate_buffers(devi);
1459                 sd_gendisk.part[minor].start_sect = 0;
1460                 sd_gendisk.part[minor].nr_sects = 0;
1461                 sd_sizes[minor] = 0;
1462             }
1463             
1464             dpnt->has_part_table = 0;
1465             dpnt->device = NULL;
1466             dpnt->capacity = 0;
1467             SDp->attached--;
1468             sd_template.dev_noticed--;
1469             sd_template.nr_dev--;
1470             sd_gendisk.nr_real--;
1471             return;
1472         }
1473     return;
1474 }
1475 
1476 #ifdef MODULE
1477 
1478 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
1479     sd_template.usage_count = &mod_use_count_;
1480     return scsi_register_module(MODULE_SCSI_DEV, &sd_template);
1481 }
1482 
1483 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
1484 {
1485     struct gendisk * prev_sdgd;
1486     struct gendisk * sdgd;
1487     
1488     scsi_unregister_module(MODULE_SCSI_DEV, &sd_template);
1489     unregister_blkdev(SCSI_DISK_MAJOR, "sd");
1490     sd_registered--;
1491     if( rscsi_disks != NULL )
1492     {
1493         scsi_init_free((char *) rscsi_disks,
1494                        (sd_template.dev_noticed + SD_EXTRA_DEVS) 
1495                        * sizeof(Scsi_Disk));
1496         
1497         scsi_init_free((char *) sd_sizes, sd_template.dev_max * sizeof(int));
1498         scsi_init_free((char *) sd_blocksizes, sd_template.dev_max * sizeof(int));
1499         scsi_init_free((char *) sd_hardsizes, sd_template.dev_max * sizeof(int));
1500         scsi_init_free((char *) sd, 
1501                        (sd_template.dev_max << 4) * sizeof(struct hd_struct));
1502         /*
1503          * Now remove sd_gendisk from the linked list
1504          */
1505         sdgd = gendisk_head;
1506         prev_sdgd = NULL;
1507         while(sdgd != &sd_gendisk)
1508         {
1509             prev_sdgd = sdgd;
1510             sdgd = sdgd->next;
1511         }
1512         
1513         if(sdgd != &sd_gendisk)
1514             printk("sd_gendisk not in disk chain.\n");
1515         else {
1516             if(prev_sdgd != NULL)
1517                 prev_sdgd->next = sdgd->next;
1518             else
1519                 gendisk_head = sdgd->next;
1520         }
1521     }
1522     
1523     blksize_size[MAJOR_NR] = NULL;
1524     blk_dev[MAJOR_NR].request_fn = NULL;
1525     blk_size[MAJOR_NR] = NULL;  
1526     hardsect_size[MAJOR_NR] = NULL;
1527     read_ahead[MAJOR_NR] = 0;
1528     sd_template.dev_max = 0;
1529 }
1530 #endif /* MODULE */
1531 
1532 /*
1533  * Overrides for Emacs so that we almost follow Linus's tabbing style.
1534  * Emacs will notice this stuff at the end of the file and automatically
1535  * adjust the settings for this buffer only.  This must remain at the end
1536  * of the file.
1537  * ---------------------------------------------------------------------------
1538  * Local variables:
1539  * c-indent-level: 4
1540  * c-brace-imaginary-offset: 0
1541  * c-brace-offset: -4
1542  * c-argdecl-indent: 4
1543  * c-label-offset: -4
1544  * c-continued-statement-offset: 4
1545  * c-continued-brace-offset: 0
1546  * indent-tabs-mode: nil
1547  * tab-width: 8
1548  * End:
1549  */

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