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

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

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