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

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