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 extern int sd_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  53 
  54 static sd_init_onedisk(int);
  55 
  56 static void requeue_sd_request (Scsi_Cmnd * SCpnt);
  57 
  58 static int sd_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60         int target;
  61         target =  DEVICE_NR(MINOR(inode->i_rdev));
  62 
  63         if(target >= NR_SD || !rscsi_disks[target].device)
  64           return -ENODEV;   /* No such device */
  65         
  66 /* Make sure that only one process can do a check_change_disk at one time.
  67  This is also used to lock out further access when the partition table is being re-read. */
  68 
  69         while (rscsi_disks[target].device->busy);
  70 
  71         if(rscsi_disks[target].device->removable) {
  72           check_disk_change(inode->i_rdev);
  73 
  74           if(!rscsi_disks[target].device->access_count)
  75             sd_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
  76         };
  77         rscsi_disks[target].device->access_count++;
  78         return 0;
  79 }
  80 
  81 static void sd_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
  82 {
  83         int target;
  84         sync_dev(inode->i_rdev);
  85 
  86         target =  DEVICE_NR(MINOR(inode->i_rdev));
  87 
  88         rscsi_disks[target].device->access_count--;
  89 
  90         if(rscsi_disks[target].device->removable) {
  91           if(!rscsi_disks[target].device->access_count)
  92             sd_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
  93         };
  94 }
  95 
  96 static void sd_geninit(void);
  97 
  98 static struct file_operations sd_fops = {
  99         NULL,                   /* lseek - default */
 100         block_read,             /* read - general block-dev read */
 101         block_write,            /* write - general block-dev write */
 102         NULL,                   /* readdir - bad */
 103         NULL,                   /* select */
 104         sd_ioctl,               /* ioctl */
 105         NULL,                   /* mmap */
 106         sd_open,                /* open code */
 107         sd_release,             /* release */
 108         block_fsync             /* fsync */
 109 };
 110 
 111 static struct gendisk sd_gendisk = {
 112         MAJOR_NR,               /* Major number */
 113         "sd",           /* Major name */
 114         4,              /* Bits to shift to get real from partition */
 115         1 << 4,         /* Number of partitions per real */
 116         0,              /* maximum number of real */
 117         sd_geninit,     /* init function */
 118         NULL,           /* hd struct */
 119         NULL,   /* block sizes */
 120         0,              /* number */
 121         NULL,   /* internal */
 122         NULL            /* next */
 123 };
 124 
 125 static void sd_geninit (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 126 {
 127         int i;
 128 
 129         for (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           if (sugestion(result) == SUGGEST_REMAP) {
 249 #ifdef REMAP
 250 /*
 251         Not yet implemented.  A read will fail after being remapped,
 252         a write will call the strategy routine again.
 253 */
 254             if rscsi_disks[DEVICE_NR(SCpnt->request.dev)].remap
 255               {
 256                 result = 0;
 257               }
 258             else
 259               
 260 #endif
 261             }
 262 
 263           if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
 264             if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
 265               /* detected disc change.  set a bit and quietly refuse    */
 266               /* further access.                                        */
 267               
 268               rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->changed = 1;
 269               end_scsi_request(SCpnt, 0, this_count);
 270               requeue_sd_request(SCpnt);
 271               return;
 272             }
 273           }
 274           
 275 
 276 /*      If we had an ILLEGAL REQUEST returned, then we may have
 277 performed an unsupported command.  The only thing this should be would
 278 be a ten byte read where only a six byte read was supportted.  Also,
 279 on a system where READ CAPACITY failed, we mave have read past the end
 280 of the  disk. 
 281 */
 282 
 283           if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
 284             if (rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten) {
 285               rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten = 0;
 286               requeue_sd_request(SCpnt);
 287               result = 0;
 288             } else {
 289             }
 290           }
 291         }  /* driver byte != 0 */
 292         if (result) {
 293                 printk("SCSI disk error : host %d id %d lun %d return code = %x\n",
 294                        rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->host_no,
 295                        rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->id,
 296                        rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->lun, result);
 297 
 298                 if (driver_byte(result) & DRIVER_SENSE)
 299                         print_sense("sd", SCpnt);
 300                 end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
 301                 requeue_sd_request(SCpnt);
 302                 return;
 303         }
 304 }
 305 
 306 /*
 307         requeue_sd_request() is the request handler function for the sd driver.
 308         Its function in life is to take block device requests, and translate
 309         them to SCSI commands.
 310 */
 311 
 312 static void do_sd_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 313 {
 314   Scsi_Cmnd * SCpnt = NULL;
 315   struct request * req = NULL;
 316   int flag = 0;
 317   while (1==1){
 318     cli();
 319     if (CURRENT != NULL && CURRENT->dev == -1) {
 320       sti();
 321       return;
 322     };
 323 
 324     INIT_SCSI_REQUEST;
 325 
 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   char spintime;
 672   int the_result, retries;
 673   Scsi_Cmnd * SCpnt;
 674 
 675   /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is considered
 676      a fatal error, and many devices report such an error just after a scsi
 677      bus reset. */
 678 
 679   SCpnt = allocate_device(NULL, rscsi_disks[i].device->index, 1);
 680   buffer = (unsigned char *) scsi_malloc(512);
 681 
 682   spintime = 0;
 683 
 684   /* Spin up drives, as required.  Only do this at boot time */
 685   if (current == task[0]){
 686     do{
 687       cmd[0] = TEST_UNIT_READY;
 688       cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
 689       memset ((void *) &cmd[2], 0, 8);
 690       SCpnt->request.dev = 0xffff;  /* Mark as really busy again */
 691       SCpnt->sense_buffer[0] = 0;
 692       SCpnt->sense_buffer[2] = 0;
 693       
 694       scsi_do_cmd (SCpnt,
 695                    (void *) cmd, (void *) buffer,
 696                    512, sd_init_done,  SD_TIMEOUT,
 697                    MAX_RETRIES);
 698       
 699       while(SCpnt->request.dev != 0xfffe);
 700       
 701       the_result = SCpnt->result;
 702       
 703       /* Look for non-removable devices that return NOT_READY.  Issue command
 704          to spin up drive for these cases. */
 705       if(the_result && !rscsi_disks[i].device->removable && 
 706          SCpnt->sense_buffer[2] == NOT_READY) {
 707         int time1;
 708         if(!spintime){
 709           cmd[0] = START_STOP;
 710           cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
 711           cmd[1] |= 1;  /* Return immediately */
 712           memset ((void *) &cmd[2], 0, 8);
 713           cmd[4] = 1; /* Start spin cycle */
 714           SCpnt->request.dev = 0xffff;  /* Mark as really busy again */
 715           SCpnt->sense_buffer[0] = 0;
 716           SCpnt->sense_buffer[2] = 0;
 717           
 718           scsi_do_cmd (SCpnt,
 719                        (void *) cmd, (void *) buffer,
 720                        512, sd_init_done,  SD_TIMEOUT,
 721                        MAX_RETRIES);
 722           
 723           while(SCpnt->request.dev != 0xfffe);
 724 
 725           spintime = jiffies;
 726         };
 727 
 728         time1 = jiffies;
 729         while(jiffies < time1 + 100); /* Wait 1 second for next try */
 730       };
 731     } while(the_result && spintime && spintime+1500 < jiffies);
 732   };  /* current == task[0] */
 733 
 734 
 735   retries = 3;
 736   do {
 737     cmd[0] = READ_CAPACITY;
 738     cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
 739     memset ((void *) &cmd[2], 0, 8);
 740     memset ((void *) buffer, 0, 8);
 741     SCpnt->request.dev = 0xffff;  /* Mark as really busy again */
 742     SCpnt->sense_buffer[0] = 0;
 743     SCpnt->sense_buffer[2] = 0;
 744     
 745     scsi_do_cmd (SCpnt,
 746                  (void *) cmd, (void *) buffer,
 747                  8, sd_init_done,  SD_TIMEOUT,
 748                  MAX_RETRIES);
 749     
 750     if (current == task[0])
 751       while(SCpnt->request.dev != 0xfffe);
 752     else
 753       if (SCpnt->request.dev != 0xfffe){
 754         SCpnt->request.waiting = current;
 755         current->state = TASK_UNINTERRUPTIBLE;
 756         while (SCpnt->request.dev != 0xfffe) schedule();
 757       };
 758     
 759     the_result = SCpnt->result;
 760     retries--;
 761 
 762   } while(the_result && retries);
 763 
 764   SCpnt->request.dev = -1;  /* Mark as not busy */
 765 
 766   wake_up(&scsi_devices[SCpnt->index].device_wait); 
 767 
 768   /* Wake up a process waiting for device*/
 769 
 770   /*
 771    *    The SCSI standard says "READ CAPACITY is necessary for self confuring software"
 772    *    While not mandatory, support of READ CAPACITY is strongly encouraged.
 773    *    We used to die if we couldn't successfully do a READ CAPACITY.
 774    *    But, now we go on about our way.  The side effects of this are
 775    *
 776    *    1.  We can't know block size with certainty.  I have said "512 bytes is it"
 777    *            as this is most common.
 778    *
 779    *    2.  Recovery from when some one attempts to read past the end of the raw device will
 780    *        be slower.
 781    */
 782 
 783   if (the_result)
 784     {
 785       printk ("sd%d : READ CAPACITY failed.\n"
 786               "sd%d : status = %x, message = %02x, host = %02x, driver = %02x \n",
 787               i,i,
 788               rscsi_disks[i].device->host_no, rscsi_disks[i].device->id,
 789               rscsi_disks[i].device->lun,
 790               status_byte(the_result),
 791               msg_byte(the_result),
 792               host_byte(the_result),
 793               driver_byte(the_result)
 794               );
 795       if (driver_byte(the_result)  & DRIVER_SENSE)
 796         printk("sd%d : extended sense code = %1x \n", i, SCpnt->sense_buffer[2] & 0xf);
 797       else
 798         printk("sd%d : sense not available. \n", i);
 799 
 800       printk("sd%d : block size assumed to be 512 bytes, disk size 1GB.  \n", i);
 801       rscsi_disks[i].capacity = 0x1fffff;
 802       rscsi_disks[i].sector_size = 512;
 803 
 804       /* Set dirty bit for removable devices if not ready - sometimes drives
 805          will not report this properly. */
 806       if(rscsi_disks[i].device->removable && 
 807          SCpnt->sense_buffer[2] == NOT_READY)
 808         rscsi_disks[i].device->changed = 1;
 809 
 810     }
 811   else
 812     {
 813       rscsi_disks[i].capacity = (buffer[0] << 24) |
 814         (buffer[1] << 16) |
 815           (buffer[2] << 8) |
 816             buffer[3];
 817 
 818       rscsi_disks[i].sector_size = (buffer[4] << 24) |
 819         (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
 820 
 821       if (rscsi_disks[i].sector_size != 512 &&
 822           rscsi_disks[i].sector_size != 1024 &&
 823           rscsi_disks[i].sector_size != 256)
 824         {
 825           printk ("sd%d : unsupported sector size %d.\n",
 826                   i, rscsi_disks[i].sector_size);
 827           if(rscsi_disks[i].device->removable){
 828             rscsi_disks[i].capacity = 0;
 829           } else {
 830             printk ("scsi : deleting disk entry.\n");
 831             for  (j=i;  j < NR_SD - 1;)
 832               rscsi_disks[j] = rscsi_disks[++j];
 833             --i;
 834             --NR_SD;
 835             scsi_free(buffer, 512);
 836             return i;
 837           };
 838         }
 839       if(rscsi_disks[i].sector_size == 1024)
 840         rscsi_disks[i].capacity <<= 1;  /* Change this into 512 byte sectors */
 841       if(rscsi_disks[i].sector_size == 256)
 842         rscsi_disks[i].capacity >>= 1;  /* Change this into 512 byte sectors */
 843     }
 844 
 845   rscsi_disks[i].ten = 1;
 846   rscsi_disks[i].remap = 1;
 847   scsi_free(buffer, 512);
 848   return i;
 849 }
 850 
 851 /*
 852         The sd_init() function looks at all SCSI drives present, determines
 853         their size, and reads partition table entries for them.
 854 */
 855 
 856 unsigned long sd_init(unsigned long memory_start, unsigned long memory_end)
     /* [previous][next][first][last][top][bottom][index][help] */
 857 {
 858         int i;
 859 
 860         if (register_blkdev(MAJOR_NR,"sd",&sd_fops)) {
 861                 printk("Unable to get major %d for SCSI disk\n",MAJOR_NR);
 862                 return memory_start;
 863         }
 864         if (MAX_SD == 0) return memory_start;
 865 
 866         sd_sizes = (int *) memory_start;
 867         memory_start += (MAX_SD << 4) * sizeof(int);
 868         memset(sd_sizes, 0, (MAX_SD << 4) * sizeof(int));
 869 
 870         sd_blocksizes = (int *) memory_start;
 871         memory_start += (MAX_SD << 4) * sizeof(int);
 872         for(i=0;i<(MAX_SD << 4);i++) sd_blocksizes[i] = 1024;
 873         blksize_size[MAJOR_NR] = sd_blocksizes;
 874 
 875         sd = (struct hd_struct *) memory_start;
 876         memory_start += (MAX_SD << 4) * sizeof(struct hd_struct);
 877 
 878         sd_gendisk.max_nr = MAX_SD;
 879         sd_gendisk.part = sd;
 880         sd_gendisk.sizes = sd_sizes;
 881         sd_gendisk.real_devices = (void *) rscsi_disks;
 882 
 883         for (i = 0; i < NR_SD; ++i)
 884           i = sd_init_onedisk(i);
 885 
 886         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
 887 
 888         /* If our host adapter is capable of scatter-gather, then we increase
 889            the read-ahead to 8 blocks (16 sectors).  If not, we use
 890            a two block (4 sector) read ahead. */
 891         if(scsi_hosts[rscsi_disks[0].device->host_no].sg_tablesize)
 892           read_ahead[MAJOR_NR] = 16;  /* 16 sector read-ahead */
 893         else
 894           read_ahead[MAJOR_NR] = 4;  /* 4 sector read-ahead */
 895         
 896         sd_gendisk.next = gendisk_head;
 897         gendisk_head = &sd_gendisk;
 898         return memory_start;
 899 }
 900 
 901 unsigned long sd_init1(unsigned long mem_start, unsigned long mem_end){
     /* [previous][next][first][last][top][bottom][index][help] */
 902   rscsi_disks = (Scsi_Disk *) mem_start;
 903   mem_start += MAX_SD * sizeof(Scsi_Disk);
 904   return mem_start;
 905 };
 906 
 907 void sd_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 908   rscsi_disks[NR_SD++].device = SDp;
 909   if(NR_SD > MAX_SD) panic ("scsi_devices corrupt (sd)");
 910 };
 911 
 912 #define DEVICE_BUSY rscsi_disks[target].device->busy
 913 #define USAGE rscsi_disks[target].device->access_count
 914 #define CAPACITY rscsi_disks[target].capacity
 915 #define MAYBE_REINIT  sd_init_onedisk(target)
 916 #define GENDISK_STRUCT sd_gendisk
 917 
 918 /* This routine is called to flush all partitions and partition tables
 919    for a changed scsi disk, and then re-read the new partition table.
 920    If we are revalidating a disk because of a media change, then we
 921    enter with usage == 0.  If we are using an ioctl, we automatically have
 922    usage == 1 (we need an open channel to use an ioctl :-), so this
 923    is our limit.
 924  */
 925 int revalidate_scsidisk(int dev, int maxusage){
     /* [previous][next][first][last][top][bottom][index][help] */
 926           int target, major;
 927           struct gendisk * gdev;
 928           int max_p;
 929           int start;
 930           int i;
 931 
 932           target =  DEVICE_NR(MINOR(dev));
 933           gdev = &GENDISK_STRUCT;
 934 
 935           cli();
 936           if (DEVICE_BUSY || USAGE > maxusage) {
 937             sti();
 938             printk("Device busy for revalidation (usage=%d)\n", USAGE);
 939             return -EBUSY;
 940           };
 941           DEVICE_BUSY = 1;
 942           sti();
 943 
 944           max_p = gdev->max_p;
 945           start = target << gdev->minor_shift;
 946           major = MAJOR_NR << 8;
 947 
 948           for (i=max_p - 1; i >=0 ; i--) {
 949             sync_dev(major | start | i);
 950             invalidate_inodes(major | start | i);
 951             invalidate_buffers(major | start | i);
 952             gdev->part[start+i].start_sect = 0;
 953             gdev->part[start+i].nr_sects = 0;
 954           };
 955 
 956 #ifdef MAYBE_REINIT
 957           MAYBE_REINIT;
 958 #endif
 959 
 960           gdev->part[start].nr_sects = CAPACITY;
 961           resetup_one_dev(gdev, target);
 962 
 963           DEVICE_BUSY = 0;
 964           return 0;
 965 }

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