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

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