root/kernel/blk_drv/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_init1
  12. sd_attach
  13. revalidate_scsidisk

   1 /*
   2  *      sd.c Copyright (C) 1992 Drew Eckhardt 
   3  *      Linux scsi disk driver by
   4  *              Drew Eckhardt 
   5  *
   6  *      <drew@colorado.edu>
   7  *
   8  *       Modified by Eric Youngdale eric@tantalus.nrl.navy.mil to
   9  *       add scatter-gather, multiple outstanding request, and other
  10  *       enhancements.
  11  */
  12 
  13 #include <linux/fs.h>
  14 #include <linux/kernel.h>
  15 #include <linux/sched.h>
  16 #include <linux/string.h>
  17 #include <linux/errno.h>
  18 #include <asm/system.h>
  19 
  20 
  21 #define MAJOR_NR 8
  22 
  23 #include "../blk.h"
  24 #include "scsi.h"
  25 #include "hosts.h"
  26 #include "sd.h"
  27 #include "scsi_ioctl.h"
  28 #include "constants.h"
  29 
  30 #include <linux/genhd.h>
  31 
  32 /*
  33 static const char RCSid[] = "$Header:";
  34 */
  35 
  36 #define MAX_RETRIES 5
  37 
  38 /*
  39  *      Time out in seconds
  40  */
  41 
  42 #define SD_TIMEOUT 300
  43 
  44 struct hd_struct * sd;
  45 
  46 int NR_SD=0;
  47 int MAX_SD=0;
  48 Scsi_Disk * rscsi_disks;
  49 static int * sd_sizes;
  50 static int * sd_blocksizes;
  51 
  52 /* used to re-read partitions. */
  53 extern void resetup_one_dev(struct gendisk *, unsigned int);
  54 
  55 extern int sd_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  56 
  57 static sd_init_onedisk(int);
  58 
  59 static void requeue_sd_request (Scsi_Cmnd * SCpnt);
  60 
  61 static int sd_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63         int target;
  64         target =  DEVICE_NR(MINOR(inode->i_rdev));
  65 
  66         if(target >= NR_SD || !rscsi_disks[target].device)
  67           return -ENODEV;   /* No such device */
  68         
  69 /* Make sure that only one process can do a check_change_disk at one time.
  70  This is also used to lock out further access when the partition table is being re-read. */
  71 
  72         while (rscsi_disks[target].device->busy);
  73 
  74         if(rscsi_disks[target].device->removable) {
  75           check_disk_change(inode->i_rdev);
  76 
  77           if(!rscsi_disks[target].device->access_count)
  78             sd_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
  79         };
  80         rscsi_disks[target].device->access_count++;
  81         return 0;
  82 }
  83 
  84 static void sd_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86         int target;
  87         sync_dev(inode->i_rdev);
  88 
  89         target =  DEVICE_NR(MINOR(inode->i_rdev));
  90 
  91         rscsi_disks[target].device->access_count--;
  92 
  93         if(rscsi_disks[target].device->removable) {
  94           if(!rscsi_disks[target].device->access_count)
  95             sd_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
  96         };
  97 }
  98 
  99 static void sd_geninit(void);
 100 
 101 static struct file_operations sd_fops = {
 102         NULL,                   /* lseek - default */
 103         block_read,             /* read - general block-dev read */
 104         block_write,            /* write - general block-dev write */
 105         NULL,                   /* readdir - bad */
 106         NULL,                   /* select */
 107         sd_ioctl,               /* ioctl */
 108         NULL,                   /* mmap */
 109         sd_open,                /* open code */
 110         sd_release,             /* release */
 111         block_fsync             /* fsync */
 112 };
 113 
 114 static struct gendisk sd_gendisk = {
 115         MAJOR_NR,               /* Major number */
 116         "sd",           /* Major name */
 117         4,              /* Bits to shift to get real from partition */
 118         1 << 4,         /* Number of partitions per real */
 119         0,              /* maximum number of real */
 120         sd_geninit,     /* init function */
 121         NULL,           /* hd struct */
 122         NULL,   /* block sizes */
 123         0,              /* number */
 124         NULL,   /* internal */
 125         NULL            /* next */
 126 };
 127 
 128 static void sd_geninit (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130         for (int i = 0; i < NR_SD; ++i)
 131                 sd[i << 4].nr_sects = rscsi_disks[i].capacity;
 132         sd_gendisk.nr_real = NR_SD;
 133 }
 134 
 135 /*
 136         rw_intr is the interrupt routine for the device driver.  It will
 137         be notified on the end of a SCSI read / write, and
 138         will take on of several actions based on success or failure.
 139 */
 140 
 141 static void rw_intr (Scsi_Cmnd *SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 142 {
 143   int result = SCpnt->result;
 144   int this_count = SCpnt->bufflen >> 9;
 145 
 146 #ifdef DEBUG
 147   printk("sd%d : rw_intr(%d, %x)\n", MINOR(SCpnt->request.dev), SCpnt->host, result);
 148 #endif
 149 
 150 /*
 151   First case : we assume that the command succeeded.  One of two things will
 152   happen here.  Either we will be finished, or there will be more
 153   sectors that we were unable to read last time.
 154 */
 155 
 156   if (!result) {
 157 
 158 #ifdef DEBUG
 159     printk("sd%d : %d sectors remain.\n", MINOR(SCpnt->request.dev), SCpnt->request.nr_sectors);
 160     printk("use_sg is %d\n ",SCpnt->use_sg);
 161 #endif
 162     if (SCpnt->use_sg) {
 163       struct scatterlist * sgpnt;
 164       int i;
 165       sgpnt = (struct scatterlist *) SCpnt->buffer;
 166       for(i=0; i<SCpnt->use_sg; i++) {
 167 #ifdef DEBUG
 168         printk(":%x %x %d\n",sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
 169 #endif
 170         if (sgpnt[i].alt_address) {
 171           if (SCpnt->request.cmd == READ)
 172             memcpy(sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
 173           scsi_free(sgpnt[i].address, sgpnt[i].length);
 174         };
 175       };
 176       scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
 177     } else {
 178       if (SCpnt->buffer != SCpnt->request.buffer) {
 179 #ifdef DEBUG
 180         printk("nosg: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
 181                    SCpnt->bufflen);
 182 #endif  
 183           if (SCpnt->request.cmd == READ)
 184             memcpy(SCpnt->request.buffer, SCpnt->buffer,
 185                    SCpnt->bufflen);
 186           scsi_free(SCpnt->buffer, SCpnt->bufflen);
 187       };
 188     };
 189 /*
 190  *      If multiple sectors are requested in one buffer, then
 191  *      they will have been finished off by the first command.  If
 192  *      not, then we have a multi-buffer command.
 193  */
 194     if (SCpnt->request.nr_sectors > this_count)
 195       {
 196         SCpnt->request.errors = 0;
 197         
 198         if (!SCpnt->request.bh)
 199           {
 200 #ifdef DEBUG
 201             printk("sd%d : handling page request, no buffer\n",
 202                    MINOR(SCpnt->request.dev));
 203 #endif
 204 /*
 205   The SCpnt->request.nr_sectors field is always done in 512 byte sectors,
 206   even if this really isn't the case.
 207 */
 208             printk("sd.c: linked page request. (%x %x)",
 209                   SCpnt->request.sector, this_count);
 210             panic("Aiiiiiiiiiiiieeeeeeeee");
 211           }
 212       }
 213     end_scsi_request(SCpnt, 1, this_count);
 214     requeue_sd_request(SCpnt);
 215     return;
 216   }
 217 
 218 /* Free up any indirection buffers we allocated for DMA purposes. */
 219     if (SCpnt->use_sg) {
 220       struct scatterlist * sgpnt;
 221       int i;
 222       sgpnt = (struct scatterlist *) SCpnt->buffer;
 223       for(i=0; i<SCpnt->use_sg; i++) {
 224 #ifdef DEBUG
 225         printk("err: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
 226                    SCpnt->bufflen);
 227 #endif
 228         if (sgpnt[i].alt_address) {
 229           scsi_free(sgpnt[i].address, sgpnt[i].length);
 230         };
 231       };
 232       scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
 233     } else {
 234 #ifdef DEBUG
 235       printk("nosgerr: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
 236                    SCpnt->bufflen);
 237 #endif
 238       if (SCpnt->buffer != SCpnt->request.buffer)
 239         scsi_free(SCpnt->buffer, SCpnt->bufflen);
 240     };
 241 
 242 /*
 243         Now, if we were good little boys and girls, Santa left us a request
 244         sense buffer.  We can extract information from this, so we
 245         can choose a block to remap, etc.
 246 */
 247 
 248         if (driver_byte(result) != 0) {
 249           if (sugestion(result) == SUGGEST_REMAP) {
 250 #ifdef REMAP
 251 /*
 252         Not yet implemented.  A read will fail after being remapped,
 253         a write will call the strategy routine again.
 254 */
 255             if rscsi_disks[DEVICE_NR(SCpnt->request.dev)].remap
 256               {
 257                 result = 0;
 258               }
 259             else
 260               
 261 #endif
 262             }
 263 
 264           if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
 265             if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
 266               /* detected disc change.  set a bit and quietly refuse    */
 267               /* further access.                                        */
 268               
 269               rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->changed = 1;
 270               end_scsi_request(SCpnt, 0, this_count);
 271               requeue_sd_request(SCpnt);
 272               return;
 273             }
 274           }
 275           
 276 
 277 /*      If we had an ILLEGAL REQUEST returned, then we may have
 278 performed an unsupported command.  The only thing this should be would
 279 be a ten byte read where only a six byte read was supportted.  Also,
 280 on a system where READ CAPACITY failed, we mave have read past the end
 281 of the  disk. 
 282 */
 283 
 284           if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
 285             if (rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten) {
 286               rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten = 0;
 287               requeue_sd_request(SCpnt);
 288               result = 0;
 289             } else {
 290             }
 291           }
 292         }  /* driver byte != 0 */
 293         if (result) {
 294                 printk("SCSI disk error : host %d id %d lun %d return code = %x\n",
 295                        rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->host_no,
 296                        rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->id,
 297                        rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->lun, result);
 298 
 299                 if (driver_byte(result) & DRIVER_SENSE)
 300                         print_sense("sd", SCpnt);
 301                 end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
 302                 requeue_sd_request(SCpnt);
 303                 return;
 304         }
 305 }
 306 
 307 /*
 308         requeue_sd_request() is the request handler function for the sd driver.
 309         Its function in life is to take block device requests, and translate
 310         them to SCSI commands.
 311 */
 312 
 313 static void do_sd_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 314 {
 315   Scsi_Cmnd * SCpnt = NULL;
 316   struct request * req = NULL;
 317   int flag = 0;
 318   while (1==1){
 319     cli();
 320     if (CURRENT != NULL && CURRENT->dev == -1) {
 321       sti();
 322       return;
 323     };
 324 
 325     INIT_SCSI_REQUEST;
 326 
 327 /* We have to be careful here.  allocate_device will get a free pointer, but
 328    there is no guarantee that it is queueable.  In normal usage, we want to
 329    call this, because other types of devices may have the host all tied up,
 330    and we want to make sure that we have at least one request pending for this
 331    type of device.   We can also come through here while servicing an
 332    interrupt, because of the need to start another command.  If we call
 333    allocate_device more than once, then the system can wedge if the command
 334    is not queueable.  The request_queueable function is safe because it checks
 335    to make sure that the host is able to take another command before it returns
 336    a pointer.  */
 337 
 338     if (flag++ == 0)
 339       SCpnt = allocate_device(&CURRENT,
 340                               rscsi_disks[DEVICE_NR(MINOR(CURRENT->dev))].device->index, 0); 
 341     else SCpnt = NULL;
 342     sti();
 343 
 344 /* This is a performance enhancement.  We dig down into the request list and
 345    try and find a queueable request (i.e. device not busy, and host able to
 346    accept another command.  If we find one, then we queue it. This can
 347    make a big difference on systems with more than one disk drive.  We want
 348    to have the interrupts off when monkeying with the request list, because
 349    otherwise the kernel might try and slip in a request inbetween somewhere. */
 350 
 351     if (!SCpnt && NR_SD > 1){
 352       struct request *req1;
 353       req1 = NULL;
 354       cli();
 355       req = CURRENT;
 356       while(req){
 357         SCpnt = request_queueable(req,
 358                                   rscsi_disks[DEVICE_NR(MINOR(req->dev))].device->index);
 359         if(SCpnt) break;
 360         req1 = req;
 361         req = req->next;
 362       };
 363       if (SCpnt) {
 364         if (req == CURRENT) 
 365           CURRENT = CURRENT->next;
 366         else
 367           req1->next = req->next;
 368       };
 369       sti();
 370     };
 371     
 372     if (!SCpnt) return; /* Could not find anything to do */
 373     
 374     wake_up(&wait_for_request);
 375     
 376     /* Queue command */
 377     requeue_sd_request(SCpnt);
 378   };  /* While */
 379 }    
 380 
 381 static void requeue_sd_request (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 382 {
 383         int dev, block, this_count;
 384         unsigned char cmd[10];
 385         char * buff;
 386 
 387 repeat:
 388 
 389         if(SCpnt->request.dev <= 0) {
 390           do_sd_request();
 391           return;
 392         }
 393 
 394         dev =  MINOR(SCpnt->request.dev);
 395         block = SCpnt->request.sector;
 396         this_count = 0;
 397 
 398 #ifdef DEBUG
 399         printk("Doing sd request, dev = %d, block = %d\n", dev, block);
 400 #endif
 401 
 402         if (dev >= (NR_SD << 4) || block + SCpnt->request.nr_sectors > sd[dev].nr_sects)
 403                 {
 404                 end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 405                 goto repeat;
 406                 }
 407 
 408         block += sd[dev].start_sect;
 409         dev = DEVICE_NR(dev);
 410 
 411         if (rscsi_disks[dev].device->changed)
 412                 {
 413 /*
 414  * quietly refuse to do anything to a changed disc until the changed bit has been reset
 415  */
 416                 /* printk("SCSI disk has been changed.  Prohibiting further I/O.\n");   */
 417                 end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 418                 goto repeat;
 419                 }
 420 
 421 #ifdef DEBUG
 422         printk("sd%d : real dev = /dev/sd%d, block = %d\n", MINOR(SCpnt->request.dev), dev, block);
 423 #endif
 424 
 425         switch (SCpnt->request.cmd)
 426                 {
 427                 case WRITE :
 428                         if (!rscsi_disks[dev].device->writeable)
 429                                 {
 430                                 end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 431                                 goto repeat;
 432                                 }
 433                         cmd[0] = WRITE_6;
 434                         break;
 435                 case READ :
 436                         cmd[0] = READ_6;
 437                         break;
 438                 default :
 439                         printk ("Unknown sd command %d\n", SCpnt->request.cmd);
 440                         panic("");
 441                       }
 442 
 443         SCpnt->this_count = 0;
 444 
 445         if (!SCpnt->request.bh || 
 446             (SCpnt->request.nr_sectors == SCpnt->request.current_nr_sectors)) {
 447 
 448           /* case of page request (i.e. raw device), or unlinked buffer */
 449           this_count = SCpnt->request.nr_sectors;
 450           buff = SCpnt->request.buffer;
 451           SCpnt->use_sg = 0;
 452 
 453         } else if (scsi_hosts[SCpnt->host].sg_tablesize == 0 ||
 454                    (need_isa_buffer && 
 455                     dma_free_sectors < 10)) {
 456 
 457           /* Case of host adapter that cannot scatter-gather.  We also
 458            come here if we are running low on DMA buffer memory.  We set
 459            a threshold higher than that we would need for this request so
 460            we leave room for other requests.  Even though we would not need
 461            it all, we need to be conservative, because if we run low enough
 462            we have no choice but to panic. */
 463 
 464           if (scsi_hosts[SCpnt->host].sg_tablesize != 0 &&
 465               need_isa_buffer && 
 466               dma_free_sectors < 10)
 467             printk("Warning: SCSI DMA buffer space running low.  Using non scatter-gather I/O.\n");
 468 
 469           this_count = SCpnt->request.current_nr_sectors;
 470           buff = SCpnt->request.buffer;
 471           SCpnt->use_sg = 0;
 472 
 473         } else {
 474 
 475           /* Scatter-gather capable host adapter */
 476           struct buffer_head * bh;
 477           struct scatterlist * sgpnt;
 478           int count, this_count_max;
 479           bh = SCpnt->request.bh;
 480           this_count = 0;
 481           this_count_max = (rscsi_disks[dev].ten ? 0xffff : 0xff);
 482           count = 0;
 483           while(bh && count < scsi_hosts[SCpnt->host].sg_tablesize) {
 484             if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
 485             this_count += (bh->b_size >> 9);
 486             count++;
 487             bh = bh->b_reqnext;
 488           };
 489           SCpnt->use_sg = count;  /* Number of chains */
 490           count = 512;/* scsi_malloc can only allocate in chunks of 512 bytes*/
 491           while( count < (SCpnt->use_sg * sizeof(struct scatterlist))) 
 492             count = count << 1;
 493           SCpnt->sglist_len = count;
 494           sgpnt = (struct scatterlist * ) scsi_malloc(count);
 495           if (!sgpnt) {
 496             printk("Warning - running *really* short on DMA buffers\n");
 497             SCpnt->use_sg = 0;  /* No memory left - bail out */
 498             this_count = SCpnt->request.current_nr_sectors;
 499             buff = SCpnt->request.buffer;
 500           } else {
 501             buff = (char *) sgpnt;
 502             count = 0;
 503             bh = SCpnt->request.bh;
 504             for(count = 0, bh = SCpnt->request.bh; count < SCpnt->use_sg; 
 505                 count++, bh = bh->b_reqnext) {
 506               sgpnt[count].address = bh->b_data;
 507               sgpnt[count].alt_address = NULL;
 508               sgpnt[count].length = bh->b_size;
 509               if (((int) sgpnt[count].address) + sgpnt[count].length > 
 510                   ISA_DMA_THRESHOLD & (scsi_hosts[SCpnt->host].unchecked_isa_dma)) {
 511                 sgpnt[count].alt_address = sgpnt[count].address;
 512                 /* We try and avoid exhausting the DMA pool, since it is easier
 513                    to control usage here.  In other places we might have a more
 514                    pressing need, and we would be screwed if we ran out */
 515                 if(dma_free_sectors < (bh->b_size >> 9) + 5) {
 516                   sgpnt[count].address = NULL;
 517                 } else {
 518                   sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
 519                 };
 520 /* If we start running low on DMA buffers, we abort the scatter-gather
 521    operation, and free all of the memory we have allocated.  We want to
 522    ensure that all scsi operations are able to do at least a non-scatter/gather
 523    operation */
 524                 if(sgpnt[count].address == NULL){ /* Out of dma memory */
 525                   printk("Warning: Running low on SCSI DMA buffers");
 526                   /* Try switching back to a non scatter-gather operation. */
 527                   while(--count >= 0){
 528                     if(sgpnt[count].alt_address) 
 529                       scsi_free(sgpnt[count].address, sgpnt[count].length);
 530                   };
 531                   this_count = SCpnt->request.current_nr_sectors;
 532                   buff = SCpnt->request.buffer;
 533                   SCpnt->use_sg = 0;
 534                   scsi_free(buff, SCpnt->sglist_len);
 535                   break;
 536                 };
 537 
 538                 if (SCpnt->request.cmd == WRITE)
 539                   memcpy(sgpnt[count].address, sgpnt[count].alt_address, 
 540                          sgpnt[count].length);
 541               };
 542             }; /* for loop */
 543           };  /* Able to malloc sgpnt */
 544         };  /* Host adapter capable of scatter-gather */
 545 
 546 /* Now handle the possibility of DMA to addresses > 16Mb */
 547 
 548         if(SCpnt->use_sg == 0){
 549           if (((int) buff) + (this_count << 9) > ISA_DMA_THRESHOLD && 
 550             (scsi_hosts[SCpnt->host].unchecked_isa_dma)) {
 551             buff = (char *) scsi_malloc(this_count << 9);
 552             if(buff == NULL) panic("Ran out of DMA buffers.");
 553             if (SCpnt->request.cmd == WRITE)
 554               memcpy(buff, (char *)SCpnt->request.buffer, this_count << 9);
 555           };
 556         };
 557 
 558 #ifdef DEBUG
 559         printk("sd%d : %s %d/%d 512 byte blocks.\n", MINOR(SCpnt->request.dev),
 560                 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
 561                 this_count, SCpnt->request.nr_sectors);
 562 #endif
 563 
 564         cmd[1] = (SCpnt->lun << 5) & 0xe0;
 565 
 566         if (rscsi_disks[dev].sector_size == 1024){
 567           if(block & 1) panic("sd.c:Bad block number requested");
 568           if(this_count & 1) panic("sd.c:Bad block number requested");
 569           block = block >> 1;
 570           this_count = this_count >> 1;
 571         };
 572 
 573         if (rscsi_disks[dev].sector_size == 256){
 574           block = block << 1;
 575           this_count = this_count << 1;
 576         };
 577 
 578         if (((this_count > 0xff) ||  (block > 0x1fffff)) && rscsi_disks[dev].ten)
 579                 {
 580                 if (this_count > 0xffff)
 581                         this_count = 0xffff;
 582 
 583                 cmd[0] += READ_10 - READ_6 ;
 584                 cmd[2] = (unsigned char) (block >> 24) & 0xff;
 585                 cmd[3] = (unsigned char) (block >> 16) & 0xff;
 586                 cmd[4] = (unsigned char) (block >> 8) & 0xff;
 587                 cmd[5] = (unsigned char) block & 0xff;
 588                 cmd[6] = cmd[9] = 0;
 589                 cmd[7] = (unsigned char) (this_count >> 8) & 0xff;
 590                 cmd[8] = (unsigned char) this_count & 0xff;
 591                 }
 592         else
 593                 {
 594                 if (this_count > 0xff)
 595                         this_count = 0xff;
 596 
 597                 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
 598                 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
 599                 cmd[3] = (unsigned char) block & 0xff;
 600                 cmd[4] = (unsigned char) this_count;
 601                 cmd[5] = 0;
 602                 }
 603 
 604 /*
 605  * We shouldn't disconnect in the middle of a sector, so with a dumb 
 606  * host adapter, it's safe to assume that we can at least transfer 
 607  * this many bytes between each connect / disconnect.  
 608  */
 609 
 610         SCpnt->transfersize = rscsi_disks[dev].sector_size;
 611         SCpnt->underflow = this_count << 9; 
 612 
 613         scsi_do_cmd (SCpnt, (void *) cmd, buff, 
 614                      this_count * rscsi_disks[dev].sector_size,
 615                      rw_intr, SD_TIMEOUT, MAX_RETRIES);
 616 }
 617 
 618 int check_scsidisk_media_change(int full_dev, int flag){
     /* [previous][next][first][last][top][bottom][index][help] */
 619         int retval;
 620         int target;
 621         struct inode inode;
 622 
 623         target =  DEVICE_NR(MINOR(full_dev));
 624 
 625         if (target >= NR_SD) {
 626                 printk("SCSI disk request error: invalid device.\n");
 627                 return 0;
 628         };
 629 
 630         if(!rscsi_disks[target].device->removable) return 0;
 631 
 632         inode.i_rdev = full_dev;  /* This is all we really need here */
 633         retval = sd_ioctl(&inode, NULL, SCSI_IOCTL_TEST_UNIT_READY, 0);
 634 
 635         if(retval){ /* Unable to test, unit probably not ready.  This usually
 636                      means there is no disc in the drive.  Mark as changed,
 637                      and we will figure it out later once the drive is
 638                      available again.  */
 639 
 640           rscsi_disks[target].device->changed = 1;
 641           return 1; /* This will force a flush, if called from
 642                        check_disk_change */
 643         };
 644 
 645         retval = rscsi_disks[target].device->changed;
 646         if(!flag) rscsi_disks[target].device->changed = 0;
 647         return retval;
 648 }
 649 
 650 static void sd_init_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 651 {
 652   struct request * req;
 653   struct task_struct * p;
 654   
 655   req = &SCpnt->request;
 656   req->dev = 0xfffe; /* Busy, but indicate request done */
 657   
 658   if ((p = req->waiting) != NULL) {
 659     req->waiting = NULL;
 660     p->state = TASK_RUNNING;
 661     if (p->counter > current->counter)
 662       need_resched = 1;
 663   }
 664 }
 665 
 666 static int sd_init_onedisk(int i)
     /* [previous][next][first][last][top][bottom][index][help] */
 667 {
 668   int j = 0;
 669   unsigned char cmd[10];
 670   unsigned char *buffer;
 671   int the_result, retries;
 672   Scsi_Cmnd * SCpnt;
 673 
 674   /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is considered
 675      a fatal error, and many devices report such an error just after a scsi
 676      bus reset. */
 677 
 678   SCpnt = allocate_device(NULL, rscsi_disks[i].device->index, 1);
 679   buffer = (unsigned char *) scsi_malloc(512);
 680 
 681   retries = 3;
 682   do {
 683     cmd[0] = READ_CAPACITY;
 684     cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
 685     memset ((void *) &cmd[2], 0, 8);
 686     memset ((void *) buffer, 0, 8);
 687     SCpnt->request.dev = 0xffff;  /* Mark as really busy again */
 688     SCpnt->sense_buffer[0] = 0;
 689     SCpnt->sense_buffer[2] = 0;
 690     
 691     scsi_do_cmd (SCpnt,
 692                  (void *) cmd, (void *) buffer,
 693                  8, sd_init_done,  SD_TIMEOUT,
 694                  MAX_RETRIES);
 695     
 696     if (current == task[0])
 697       while(SCpnt->request.dev != 0xfffe);
 698     else
 699       if (SCpnt->request.dev != 0xfffe){
 700         SCpnt->request.waiting = current;
 701         current->state = TASK_UNINTERRUPTIBLE;
 702         while (SCpnt->request.dev != 0xfffe) schedule();
 703       };
 704     
 705     the_result = SCpnt->result;
 706     retries--;
 707 
 708   } while(the_result && retries);
 709 
 710   SCpnt->request.dev = -1;  /* Mark as not busy */
 711 
 712   wake_up(&scsi_devices[SCpnt->index].device_wait); 
 713 
 714   /* Wake up a process waiting for device*/
 715 
 716   /*
 717    *    The SCSI standard says "READ CAPACITY is necessary for self confuring software"
 718    *    While not mandatory, support of READ CAPACITY is strongly encouraged.
 719    *    We used to die if we couldn't successfully do a READ CAPACITY.
 720    *    But, now we go on about our way.  The side effects of this are
 721    *
 722    *    1.  We can't know block size with certainty.  I have said "512 bytes is it"
 723    *            as this is most common.
 724    *
 725    *    2.  Recovery from when some one attempts to read past the end of the raw device will
 726    *        be slower.
 727    */
 728 
 729   if (the_result)
 730     {
 731       printk ("sd%d : READ CAPACITY failed.\n"
 732               "sd%d : status = %x, message = %02x, host = %02x, driver = %02x \n",
 733               i,i,
 734               rscsi_disks[i].device->host_no, rscsi_disks[i].device->id,
 735               rscsi_disks[i].device->lun,
 736               status_byte(the_result),
 737               msg_byte(the_result),
 738               host_byte(the_result),
 739               driver_byte(the_result)
 740               );
 741       if (driver_byte(the_result)  & DRIVER_SENSE)
 742         printk("sd%d : extended sense code = %1x \n", i, SCpnt->sense_buffer[2] & 0xf);
 743       else
 744         printk("sd%d : sense not available. \n", i);
 745 
 746       printk("sd%d : block size assumed to be 512 bytes, disk size 1GB.  \n", i);
 747       rscsi_disks[i].capacity = 0x1fffff;
 748       rscsi_disks[i].sector_size = 512;
 749 
 750       /* Set dirty bit for removable devices if not ready - sometimes drives
 751          will not report this properly. */
 752       if(rscsi_disks[i].device->removable && 
 753          SCpnt->sense_buffer[2] == NOT_READY)
 754         rscsi_disks[i].device->changed = 1;
 755 
 756     }
 757   else
 758     {
 759       rscsi_disks[i].capacity = (buffer[0] << 24) |
 760         (buffer[1] << 16) |
 761           (buffer[2] << 8) |
 762             buffer[3];
 763 
 764       rscsi_disks[i].sector_size = (buffer[4] << 24) |
 765         (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
 766 
 767       if (rscsi_disks[i].sector_size != 512 &&
 768           rscsi_disks[i].sector_size != 1024 &&
 769           rscsi_disks[i].sector_size != 256)
 770         {
 771           printk ("sd%d : unsupported sector size %d.\n",
 772                   i, rscsi_disks[i].sector_size);
 773           if(rscsi_disks[i].device->removable){
 774             rscsi_disks[i].capacity = 0;
 775           } else {
 776             printk ("scsi : deleting disk entry.\n");
 777             for  (j=i;  j < NR_SD - 1;)
 778               rscsi_disks[j] = rscsi_disks[++j];
 779             --i;
 780             --NR_SD;
 781             scsi_free(buffer, 512);
 782             return i;
 783           };
 784         }
 785       if(rscsi_disks[i].sector_size == 1024)
 786         rscsi_disks[i].capacity <<= 1;  /* Change this into 512 byte sectors */
 787       if(rscsi_disks[i].sector_size == 256)
 788         rscsi_disks[i].capacity >>= 1;  /* Change this into 512 byte sectors */
 789     }
 790 
 791   rscsi_disks[i].ten = 1;
 792   rscsi_disks[i].remap = 1;
 793   scsi_free(buffer, 512);
 794   return i;
 795 }
 796 
 797 /*
 798         The sd_init() function looks at all SCSI drives present, determines
 799         their size, and reads partition table entries for them.
 800 */
 801 
 802 unsigned long sd_init(unsigned long memory_start, unsigned long memory_end)
     /* [previous][next][first][last][top][bottom][index][help] */
 803 {
 804         int i;
 805 
 806         if (register_blkdev(MAJOR_NR,"sd",&sd_fops)) {
 807                 printk("Unable to get major %d for SCSI disk\n",MAJOR_NR);
 808                 return memory_start;
 809         }
 810         if (MAX_SD == 0) return memory_start;
 811 
 812         sd_sizes = (int *) memory_start;
 813         memory_start += (MAX_SD << 4) * sizeof(int);
 814         memset(sd_sizes, 0, (MAX_SD << 4) * sizeof(int));
 815 
 816         sd_blocksizes = (int *) memory_start;
 817         memory_start += (MAX_SD << 4) * sizeof(int);
 818         for(i=0;i<(MAX_SD << 4);i++) sd_blocksizes[i] = 1024;
 819         blksize_size[MAJOR_NR] = sd_blocksizes;
 820 
 821         sd = (struct hd_struct *) memory_start;
 822         memory_start += (MAX_SD << 4) * sizeof(struct hd_struct);
 823 
 824         sd_gendisk.max_nr = MAX_SD;
 825         sd_gendisk.part = sd;
 826         sd_gendisk.sizes = sd_sizes;
 827         sd_gendisk.real_devices = (void *) rscsi_disks;
 828 
 829         for (i = 0; i < NR_SD; ++i)
 830           i = sd_init_onedisk(i);
 831 
 832         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
 833 
 834         /* If our host adapter is capable of scatter-gather, then we increase
 835            the read-ahead to 8 blocks (16 sectors).  If not, we use
 836            a two block (4 sector) read ahead. */
 837         if(scsi_hosts[rscsi_disks[0].device->host_no].sg_tablesize)
 838           read_ahead[MAJOR_NR] = 16;  /* 16 sector read-ahead */
 839         else
 840           read_ahead[MAJOR_NR] = 4;  /* 4 sector read-ahead */
 841         
 842         sd_gendisk.next = gendisk_head;
 843         gendisk_head = &sd_gendisk;
 844         return memory_start;
 845 }
 846 
 847 unsigned long sd_init1(unsigned long mem_start, unsigned long mem_end){
     /* [previous][next][first][last][top][bottom][index][help] */
 848   rscsi_disks = (Scsi_Disk *) mem_start;
 849   mem_start += MAX_SD * sizeof(Scsi_Disk);
 850   return mem_start;
 851 };
 852 
 853 void sd_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 854   rscsi_disks[NR_SD++].device = SDp;
 855   if(NR_SD > MAX_SD) panic ("scsi_devices corrupt (sd)");
 856 };
 857 
 858 #define DEVICE_BUSY rscsi_disks[target].device->busy
 859 #define USAGE rscsi_disks[target].device->access_count
 860 #define CAPACITY rscsi_disks[target].capacity
 861 #define MAYBE_REINIT  sd_init_onedisk(target)
 862 #define GENDISK_STRUCT sd_gendisk
 863 
 864 /* This routine is called to flush all partitions and partition tables
 865    for a changed scsi disk, and then re-read the new partition table.
 866    If we are revalidating a disk because of a media change, then we
 867    enter with usage == 0.  If we are using an ioctl, we automatically have
 868    usage == 1 (we need an open channel to use an ioctl :-), so this
 869    is our limit.
 870  */
 871 int revalidate_scsidisk(int dev, int maxusage){
     /* [previous][next][first][last][top][bottom][index][help] */
 872           int target, major;
 873           struct gendisk * gdev;
 874           int max_p;
 875           int start;
 876           int i;
 877 
 878           target =  DEVICE_NR(MINOR(dev));
 879           gdev = &GENDISK_STRUCT;
 880 
 881           cli();
 882           if (DEVICE_BUSY || USAGE > maxusage) {
 883             sti();
 884             printk("Device busy for revalidation (usage=%d)\n", USAGE);
 885             return -EBUSY;
 886           };
 887           DEVICE_BUSY = 1;
 888           sti();
 889 
 890           max_p = gdev->max_p;
 891           start = target << gdev->minor_shift;
 892           major = MAJOR_NR << 8;
 893 
 894           for (i=max_p - 1; i >=0 ; i--) {
 895             sync_dev(major | start | i);
 896             invalidate_inodes(major | start | i);
 897             invalidate_buffers(major | start | i);
 898             gdev->part[start+i].start_sect = 0;
 899             gdev->part[start+i].nr_sects = 0;
 900           };
 901 
 902 #ifdef MAYBE_REINIT
 903           MAYBE_REINIT;
 904 #endif
 905 
 906           gdev->part[start].nr_sects = CAPACITY;
 907           resetup_one_dev(gdev, target);
 908 
 909           DEVICE_BUSY = 0;
 910           return 0;
 911 }

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