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

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