root/kernel/blk_drv/scsi/sd.c

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

DEFINITIONS

This source file includes following definitions.
  1. sd_release
  2. sd_geninit
  3. rw_intr
  4. do_sd_request
  5. sd_init_done
  6. sd_init

   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 
   9 #include <linux/config.h>
  10 
  11 #ifdef CONFIG_BLK_DEV_SD
  12 #include <linux/fs.h>
  13 #include <linux/kernel.h>
  14 #include <linux/sched.h>
  15 #include <linux/string.h>
  16 
  17 #include "scsi.h"
  18 #include "sd.h"
  19 
  20 #define MAJOR_NR 8
  21 
  22 #include "../blk.h"
  23 #include <linux/genhd.h>
  24 
  25 /*
  26 static const char RCSid[] = "$Header:";
  27 */
  28 
  29 #define MAX_RETRIES 5
  30 
  31 /*
  32  *      Time out in seconds
  33  */
  34 
  35 #define SD_TIMEOUT 200
  36 
  37 struct hd_struct sd[MAX_SD << 4];
  38                                 
  39 int NR_SD=0;
  40 Scsi_Disk rscsi_disks[MAX_SD];
  41 static int sd_sizes[MAX_SD << 4];
  42 static int this_count;
  43 static int the_result;
  44 
  45 static char sense_buffer[255];
  46 
  47 extern int sd_ioctl(struct inode *, struct file *, unsigned long, unsigned long);
  48 
  49 static void sd_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51         sync_dev(inode->i_rdev);
  52 }
  53 
  54 static struct gendisk sd_gendisk;
  55 
  56 static void sd_geninit (void) {
     /* [previous][next][first][last][top][bottom][index][help] */
  57         int i;
  58         for (i = 0; i < NR_SD; ++i)
  59                 sd_sizes[i << 4] = 
  60                 (sd[i << 4].nr_sects = rscsi_disks[i].capacity) >>
  61                 (BLOCK_SIZE_BITS - 9);
  62         sd_gendisk.nr_real = NR_SD;
  63 }
  64 
  65 static struct file_operations sd_fops = {
  66         NULL,                   /* lseek - default */
  67         block_read,             /* read - general block-dev read */
  68         block_write,            /* write - general block-dev write */
  69         NULL,                   /* readdir - bad */
  70         NULL,                   /* select */
  71         sd_ioctl,               /* ioctl */
  72         NULL,                   /* no special open code */
  73         sd_release              /* release */
  74 };
  75 
  76 static struct gendisk sd_gendisk = {
  77         MAJOR_NR,               /* Major number */      
  78         "sd",           /* Major name */
  79         4,              /* Bits to shift to get real from partition */
  80         1 << 4,         /* Number of partitions per real */
  81         MAX_SD,         /* maximum number of real */
  82         sd_geninit,     /* init function */
  83         sd,             /* hd struct */
  84         sd_sizes,       /* block sizes */
  85         0,              /* number */
  86         (void *) rscsi_disks,   /* internal */
  87         NULL            /* next */
  88 };
  89 
  90 /*
  91         rw_intr is the interrupt routine for the device driver.  It will
  92         be notified on the end of a SCSI read / write, and 
  93         will take on of several actions based on success or failure.
  94 */
  95 
  96 static void rw_intr (int host, int result)
     /* [previous][next][first][last][top][bottom][index][help] */
  97 {
  98         if (HOST != host)
  99                 panic ("sd.o : rw_intr() recieving interrupt for different host.");
 100 
 101 #ifdef DEBUG
 102         printk("sd%d : rw_intr(%d, %x)\n", MINOR(CURRENT->dev), host, result);
 103 #endif
 104 
 105 /*
 106         First case : we assume that the command succeeded.  One of two things will
 107         happen here.  Either we will be finished, or there will be more 
 108         sectors that we were unable to read last time.
 109 */
 110 
 111         if (!result) {
 112                 CURRENT->nr_sectors -= this_count;
 113 
 114 #ifdef DEBUG
 115                 printk("sd%d : %d sectors remain.\n", MINOR(CURRENT->dev), CURRENT->nr_sectors);
 116 #endif
 117 
 118 /*
 119  *      If multiple sectors are requested in one buffer, then
 120  *      they will have been finished off by the first command.  If
 121  *      not, then we have a multi-buffer command.
 122  */
 123                 if (CURRENT->nr_sectors)
 124                         {
 125                         CURRENT->sector += this_count;
 126                         CURRENT->errors = 0;
 127 
 128                         if (!CURRENT->bh)
 129                                 {
 130 #ifdef DEBUG
 131                                 printk("sd%d : handling page request, no buffer\n", 
 132                                         MINOR(CURRENT->dev));
 133 #endif
 134 
 135 /*
 136         The CURRENT->nr_sectors field is always done in 512 byte sectors,
 137         even if this really isn't the case.
 138 */
 139                                 (char *) CURRENT->buffer += this_count << 9;
 140                                 }
 141                         else
 142                                 {
 143 #ifdef DEBUG
 144                                 printk("sd%d :  handling linked buffer request\n", MINOR(CURRENT->dev));
 145 #endif
 146                                 end_request(1);
 147                                 }
 148                         }
 149                 else
 150                         end_request(1);
 151                 do_sd_request();                         
 152                 }
 153 
 154 /*
 155  *      Of course, the error handling code is a little Fubar down in scsi.c.  
 156  *      Version 2 of the drivers will fix that, and we will *really* recover 
 157  *      from errors.
 158  */
 159 
 160 /*
 161         Now, if we were good little boys and girls, Santa left us a request 
 162         sense buffer.  We can extract information from this, so we 
 163         can choose a block to remap, etc.
 164 */
 165 
 166         else if (driver_byte(result) & DRIVER_SENSE) {
 167                 if (sugestion(result) == SUGGEST_REMAP) {
 168 #ifdef REMAP
 169 /*
 170         Not yet implemented.  A read will fail after being remapped,
 171         a write will call the strategy routine again.
 172 */
 173 
 174                         if rscsi_disks[DEVICE_NR(CURRENT->dev)].remap
 175                                 {
 176                                 result = 0;
 177                                 }
 178                         else
 179                         
 180 #endif
 181                 }
 182 /*
 183         If we had an ILLEGAL REQUEST returned, then we may have performed
 184         an unsupported command.  The only thing this should be would be a  ten
 185         byte read where only a six byte read was supportted.  Also, on a 
 186         system where READ CAPACITY failed, we mave have read past the end of the
 187         disk.
 188 */
 189         
 190                 else if (sense_buffer[7] == ILLEGAL_REQUEST) {
 191                         if (rscsi_disks[DEVICE_NR(CURRENT->dev)].ten) {
 192                                 rscsi_disks[DEVICE_NR(CURRENT->dev)].ten = 0;
 193                                 do_sd_request();
 194                                 result = 0;
 195                         } else {
 196                         }
 197                 }
 198         }
 199         if (result) {
 200                 printk("SCSI disk error : host %d id %d lun %d return code = %03x\n", 
 201                        rscsi_disks[DEVICE_NR(CURRENT->dev)].device->host_no, 
 202                        rscsi_disks[DEVICE_NR(CURRENT->dev)].device->id,
 203                        rscsi_disks[DEVICE_NR(CURRENT->dev)].device->lun);
 204 
 205                 if (driver_byte(result) & DRIVER_SENSE) 
 206                         printk("\tSense class %x, sense error %x, extended sense %x\n",
 207                                 sense_class(sense_buffer[0]), 
 208                                 sense_error(sense_buffer[0]),
 209                                 sense_buffer[2] & 0xf);
 210 
 211                 end_request(0);
 212         }
 213 }
 214 
 215 /*
 216         do_sd_request() is the request handler function for the sd driver.  
 217         Its function in life is to take block device requests, and translate 
 218         them to SCSI commands.
 219 */
 220         
 221 static void do_sd_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223         int dev, block;
 224         unsigned char cmd[10];
 225 
 226 repeat:
 227         INIT_REQUEST;
 228         dev =  MINOR(CURRENT->dev);
 229         block = CURRENT->sector;
 230 
 231 #ifdef DEBUG
 232         printk("Doing sd request, dev = %d, block = %d\n", dev, block);
 233 #endif
 234 
 235         if (dev >= (NR_SD << 4) || block + CURRENT->nr_sectors > sd[dev].nr_sects) 
 236                 {
 237                 end_request(0); 
 238                 goto repeat;
 239                 }
 240         
 241         block += sd[dev].start_sect;
 242         dev = DEVICE_NR(dev);
 243 
 244 #ifdef DEBUG
 245         printk("sd%d : real dev = /dev/sd%d, block = %d\n", MINOR(CURRENT->dev), dev, block);
 246 #endif
 247 
 248 
 249         if (!CURRENT->bh)       
 250                 this_count = CURRENT->nr_sectors;
 251         else
 252                 this_count = (BLOCK_SIZE / 512);
 253 
 254 #ifdef DEBUG
 255         printk("sd%d : %s %d/%d 512 byte blocks.\n", MINOR(CURRENT->dev), 
 256                 (CURRENT->cmd == WRITE) ? "writing" : "reading",
 257                 this_count, CURRENT->nr_sectors);
 258 #endif
 259         
 260         switch (CURRENT->cmd)
 261                 {
 262                 case WRITE : 
 263                         if (!rscsi_disks[dev].device->writeable)
 264                                 {
 265                                 end_request(0);
 266                                 goto repeat;
 267                                 } 
 268                         cmd[0] = WRITE_6;
 269                         break;
 270                 case READ : 
 271                         cmd[0] = READ_6;
 272                         break;
 273                 default : 
 274                         printk ("Unknown sd command %d\r\n", CURRENT->cmd);
 275                         panic("");
 276                 }
 277 
 278         cmd[1] = (LUN << 5) & 0xe0;
 279 
 280         if (((this_count > 0xff) ||  (block > 0x1fffff)) && rscsi_disks[dev].ten) 
 281                 {
 282                 if (this_count > 0xffff)
 283                         this_count = 0xffff;
 284 
 285                 cmd[0] += READ_10 - READ_6 ;
 286                 cmd[2] = (unsigned char) (block >> 24) & 0xff;
 287                 cmd[3] = (unsigned char) (block >> 16) & 0xff;
 288                 cmd[4] = (unsigned char) (block >> 8) & 0xff;
 289                 cmd[5] = (unsigned char) block & 0xff;
 290                 cmd[6] = cmd[9] = 0;
 291                 cmd[7] = (unsigned char) (this_count >> 8) & 0xff;
 292                 cmd[8] = (unsigned char) this_count & 0xff;
 293                 }
 294         else
 295                 {
 296                 if (this_count > 0xff)
 297                         this_count = 0xff;
 298         
 299                 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
 300                 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
 301                 cmd[3] = (unsigned char) block & 0xff;
 302                 cmd[4] = (unsigned char) this_count;
 303                 cmd[5] = 0;
 304                 }
 305                         
 306         scsi_do_cmd (HOST, ID, (void *) cmd, CURRENT->buffer, this_count << 9, 
 307                      rw_intr, SD_TIMEOUT, sense_buffer, MAX_RETRIES);
 308 }
 309 
 310 static void sd_init_done (int host, int result)
     /* [previous][next][first][last][top][bottom][index][help] */
 311 {
 312         the_result = result;
 313 }
 314 
 315 /*
 316         The sd_init() function looks at all SCSI drives present, determines 
 317         their size, and reads partition table entries for them.
 318 */
 319 
 320 void sd_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {
 322         int i,j;
 323         unsigned char cmd[10];
 324         unsigned char buffer[513];
 325         int try_again;
 326 
 327         
 328         for (i = 0; i < NR_SD; ++i)
 329                 {
 330                 try_again=2;
 331                 cmd[0] = READ_CAPACITY;
 332                 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
 333                 memset ((void *) &cmd[2], 0, 8);        
 334 
 335 /*
 336  *      Super Kludge - since the midlevel error handling code doesn't work
 337  *      Version 2 will - it's under development 8^) 
 338  * 
 339  *      We manually retry
 340  */
 341         
 342 
 343                 do {
 344                 the_result = -1;
 345 #ifdef DEBUG
 346         printk("sd%d : READ CAPACITY\n ", i); 
 347 #endif
 348                 scsi_do_cmd (rscsi_disks[i].device->host_no , 
 349                                 rscsi_disks[i].device->id, 
 350                                 (void *) cmd, (void *) buffer,
 351                                  512, sd_init_done,  SD_TIMEOUT, sense_buffer,
 352                                  MAX_RETRIES);
 353 
 354                 while(the_result < 0);
 355                 } while (try_again  && the_result);
 356 /*
 357  *      The SCSI standard says "READ CAPACITY is necessary for self confuring software"
 358  *      While not mandatory, support of READ CAPACITY is strongly encouraged.
 359  *      We used to die if we couldn't successfully do a READ CAPACITY.  
 360  *      But, now we go on about our way.  The side effects of this are
 361  *
 362  *      1.  We can't know block size with certainty.  I have said "512 bytes is it"
 363  *              as this is most common.
 364  *
 365  *      2.  Recovery from when some one attempts to read past the end of the raw device will
 366  *          be slower.  
 367  */
 368         
 369                 if (the_result)
 370                         {
 371                         printk ("sd%d : READ CAPACITY failed.\n" 
 372                                 "sd%d : status = %x, message = %02x, host = %02x, driver = %02x \n",
 373                                 i,i,
 374                                 rscsi_disks[i].device->host_no, rscsi_disks[i].device->id,
 375                                 rscsi_disks[i].device->lun,
 376                                 status_byte(the_result),
 377                                 msg_byte(the_result),
 378                                 host_byte(the_result),
 379                                 driver_byte(the_result)
 380                                 );
 381                         if (driver_byte(the_result)  & DRIVER_SENSE)  
 382                                 printk("sd%d : extended sense code = %1x \n", i, sense_buffer[2] & 0xf);
 383                         else
 384                                 printk("sd%d : sense not available. \n", i);
 385 
 386                         printk("sd%d : block size assumed to be 512 bytes, disk size 1GB.  \n", i);
 387                         rscsi_disks[i].capacity = 0x1fffff;
 388                         rscsi_disks[i].sector_size = 512;
 389                         }
 390                 else
 391                         {
 392                         rscsi_disks[i].capacity = (buffer[0] << 24) |   
 393                                                   (buffer[1] << 16) |
 394                                                   (buffer[2] << 8) |
 395                                                   buffer[3];
 396 
 397                         if ((rscsi_disks[i].sector_size = (buffer[4] << 24) |
 398                                                      (buffer[5] << 16) |  
 399                                                      (buffer[6] << 8) | 
 400                                                      buffer[7]) != 512)
 401                                 {
 402                                 printk ("sd%d : unsupported sector size %d.\n", 
 403                                         i, rscsi_disks[i].sector_size);
 404                                 printk ("scsi : deleting disk entry.\n");
 405                                 for  (j=i;  j < NR_SD;)
 406                                         rscsi_disks[j] = rscsi_disks[++j];
 407                                 --i;
 408                                 continue;
 409                                 }
 410                         }
 411 
 412                 rscsi_disks[i].ten = 1;
 413                 rscsi_disks[i].remap = 1;
 414                 }
 415 
 416         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
 417         blk_size[MAJOR_NR] = sd_sizes;  
 418         blkdev_fops[MAJOR_NR] = &sd_fops; 
 419         sd_gendisk.next = gendisk_head;
 420         gendisk_head = &sd_gendisk;
 421 }       
 422 #endif

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