home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / drivers / scsi / sd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-01  |  27.4 KB  |  971 lines

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