root/drivers/block/hd.c

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

DEFINITIONS

This source file includes following definitions.
  1. CMOS_READ
  2. read_timer
  3. hd_setup
  4. dump_status
  5. check_status
  6. controller_busy
  7. status_ok
  8. controller_ready
  9. hd_out
  10. fixstring
  11. identify_intr
  12. set_multmode_intr
  13. drive_busy
  14. reset_controller
  15. reset_hd
  16. unexpected_hd_interrupt
  17. bad_rw_intr
  18. wait_DRQ
  19. read_intr
  20. multwrite
  21. multwrite_intr
  22. write_intr
  23. recal_intr
  24. hd_times_out
  25. do_special_op
  26. hd_request
  27. do_hd_request
  28. hd_ioctl
  29. hd_open
  30. hd_release
  31. hd_interrupt
  32. hd_geninit
  33. hd_init
  34. revalidate_hddisk

   1 /*
   2  *  linux/kernel/hd.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * This is the low-level hd interrupt support. It traverses the
   9  * request-list, using interrupts to jump between functions. As
  10  * all the functions are called within interrupts, we may not
  11  * sleep. Special care is recommended.
  12  * 
  13  *  modified by Drew Eckhardt to check nr of hd's from the CMOS.
  14  *
  15  *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  16  *  in the early extended-partition checks and added DM partitions
  17  *
  18  *  IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
  19  *  and general streamlining by mlord@bnr.ca (Mark Lord).
  20  */
  21 
  22 #define DEFAULT_MULT_COUNT  0   /* set to 0 to disable multiple mode at boot */
  23 #define DEFAULT_UNMASK_INTR 0   /* set to 0 to *NOT* unmask irq's more often */
  24 
  25 #include <asm/irq.h>
  26 #include <linux/errno.h>
  27 #include <linux/signal.h>
  28 #include <linux/sched.h>
  29 #include <linux/timer.h>
  30 #include <linux/fs.h>
  31 #include <linux/kernel.h>
  32 #include <linux/hdreg.h>
  33 #include <linux/genhd.h>
  34 #include <linux/config.h>
  35 #include <linux/malloc.h>
  36 #include <linux/string.h>
  37 
  38 #define REALLY_SLOW_IO
  39 #include <asm/system.h>
  40 #include <asm/io.h>
  41 #include <asm/segment.h>
  42 
  43 #define MAJOR_NR HD_MAJOR
  44 #include "blk.h"
  45 
  46 #define HD_IRQ 14
  47 
  48 static int revalidate_hddisk(int, int);
  49 
  50 static inline unsigned char CMOS_READ(unsigned char addr)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52         outb_p(addr,0x70);
  53         return inb_p(0x71);
  54 }
  55 
  56 #define HD_DELAY        0
  57 
  58 #define MAX_ERRORS     16       /* Max read/write errors/sector */
  59 #define RESET_FREQ      8       /* Reset controller every 8th retry */
  60 #define RECAL_FREQ      4       /* Recalibrate every 4th retry */
  61 #define MAX_HD          2
  62 
  63 #define STAT_OK         (READY_STAT|SEEK_STAT)
  64 #define OK_STATUS(s)    (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
  65 
  66 static void recal_intr(void);
  67 static void bad_rw_intr(void);
  68 
  69 static char recalibrate[MAX_HD] = { 0, };
  70 static char special_op[MAX_HD] = { 0, };
  71 static int access_count[MAX_HD] = {0, };
  72 static char busy[MAX_HD] = {0, };
  73 static struct wait_queue * busy_wait = NULL;
  74 
  75 static int reset = 0;
  76 static int hd_error = 0;
  77 
  78 /*
  79  *  This struct defines the HD's and their types.
  80  */
  81 struct hd_i_struct {
  82         unsigned int head,sect,cyl,wpcom,lzone,ctl;
  83         };
  84 static struct hd_driveid *hd_ident_info[MAX_HD] = {0, };
  85         
  86 #ifdef HD_TYPE
  87 static struct hd_i_struct hd_info[] = { HD_TYPE };
  88 struct hd_i_struct bios_info[] = { HD_TYPE };
  89 static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
  90 #else
  91 static struct hd_i_struct hd_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
  92 struct hd_i_struct bios_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
  93 static int NR_HD = 0;
  94 #endif
  95 
  96 static struct hd_struct hd[MAX_HD<<6]={{0,0},};
  97 static int hd_sizes[MAX_HD<<6] = {0, };
  98 static int hd_blocksizes[MAX_HD<<6] = {0, };
  99 
 100 #if (HD_DELAY > 0)
 101 unsigned long last_req;
 102 
 103 unsigned long read_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105         unsigned long t, flags;
 106         int i;
 107 
 108         save_flags(flags);
 109         cli();
 110         t = jiffies * 11932;
 111         outb_p(0, 0x43);
 112         i = inb_p(0x40);
 113         i |= inb(0x40) << 8;
 114         restore_flags(flags);
 115         return(t - i);
 116 }
 117 #endif
 118 
 119 void hd_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         int hdind = 0;
 122 
 123         if (ints[0] != 3)
 124                 return;
 125         if (bios_info[0].head != 0)
 126                 hdind=1;
 127         bios_info[hdind].head  = hd_info[hdind].head = ints[2];
 128         bios_info[hdind].sect  = hd_info[hdind].sect = ints[3];
 129         bios_info[hdind].cyl   = hd_info[hdind].cyl = ints[1];
 130         bios_info[hdind].wpcom = hd_info[hdind].wpcom = 0;
 131         bios_info[hdind].lzone = hd_info[hdind].lzone = ints[1];
 132         bios_info[hdind].ctl   = hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
 133         NR_HD = hdind+1;
 134 }
 135 
 136 static void dump_status (char *msg, unsigned int stat)
     /* [previous][next][first][last][top][bottom][index][help] */
 137 {
 138         unsigned long flags;
 139         char devc;
 140 
 141         devc = CURRENT ? 'a' + DEVICE_NR(CURRENT->dev) : '?';
 142         save_flags (flags);
 143         sti();
 144         printk("hd%c: %s: status=0x%02x { ", devc, msg, stat & 0xff);
 145         if (stat & BUSY_STAT)   printk("Busy ");
 146         if (stat & READY_STAT)  printk("DriveReady ");
 147         if (stat & WRERR_STAT)  printk("WriteFault ");
 148         if (stat & SEEK_STAT)   printk("SeekComplete ");
 149         if (stat & DRQ_STAT)    printk("DataRequest ");
 150         if (stat & ECC_STAT)    printk("CorrectedError ");
 151         if (stat & INDEX_STAT)  printk("Index ");
 152         if (stat & ERR_STAT)    printk("Error ");
 153         printk("}\n");
 154         if ((stat & ERR_STAT) == 0) {
 155                 hd_error = 0;
 156         } else {
 157                 hd_error = inb(HD_ERROR);
 158                 printk("hd%c: %s: error=0x%02x { ", devc, msg, hd_error & 0xff);
 159                 if (hd_error & BBD_ERR)         printk("BadSector ");
 160                 if (hd_error & ECC_ERR)         printk("UncorrectableError ");
 161                 if (hd_error & ID_ERR)          printk("SectorIdNotFound ");
 162                 if (hd_error & ABRT_ERR)        printk("DriveStatusError ");
 163                 if (hd_error & TRK0_ERR)        printk("TrackZeroNotFound ");
 164                 if (hd_error & MARK_ERR)        printk("AddrMarkNotFound ");
 165                 printk("}");
 166                 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
 167                         printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
 168                                 inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
 169                         if (CURRENT)
 170                                 printk(", sector=%ld", CURRENT->sector);
 171                 }
 172                 printk("\n");
 173         }
 174         restore_flags (flags);
 175 }
 176 
 177 void check_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 178 {
 179         int i = inb_p(HD_STATUS);
 180 
 181         if (!OK_STATUS(i)) {
 182                 dump_status("check_status", i);
 183                 bad_rw_intr();
 184         }
 185 }
 186 
 187 static int controller_busy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 188 {
 189         int retries = 100000;
 190         unsigned char status;
 191 
 192         do {
 193                 status = inb_p(HD_STATUS);
 194         } while ((status & BUSY_STAT) && --retries);
 195         return status;
 196 }
 197 
 198 static int status_ok(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 199 {
 200         unsigned char status = inb_p(HD_STATUS);
 201 
 202         if (status & BUSY_STAT)
 203                 return 1;       /* Ancient, but does it make sense??? */
 204         if (status & WRERR_STAT)
 205                 return 0;
 206         if (!(status & READY_STAT))
 207                 return 0;
 208         if (!(status & SEEK_STAT))
 209                 return 0;
 210         return 1;
 211 }
 212 
 213 static int controller_ready(unsigned int drive, unsigned int head)
     /* [previous][next][first][last][top][bottom][index][help] */
 214 {
 215         int retry = 100;
 216 
 217         do {
 218                 if (controller_busy() & BUSY_STAT)
 219                         return 0;
 220                 outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
 221                 if (status_ok())
 222                         return 1;
 223         } while (--retry);
 224         return 0;
 225 }
 226 
 227 static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
     /* [previous][next][first][last][top][bottom][index][help] */
 228                 unsigned int head,unsigned int cyl,unsigned int cmd,
 229                 void (*intr_addr)(void))
 230 {
 231         unsigned short port;
 232 
 233 #if (HD_DELAY > 0)
 234         while (read_timer() - last_req < HD_DELAY)
 235                 /* nothing */;
 236 #endif
 237         if (reset)
 238                 return;
 239         if (!controller_ready(drive, head)) {
 240                 reset = 1;
 241                 return;
 242         }
 243         SET_INTR(intr_addr);
 244         outb_p(hd_info[drive].ctl,HD_CMD);
 245         port=HD_DATA;
 246         outb_p(hd_info[drive].wpcom>>2,++port);
 247         outb_p(nsect,++port);
 248         outb_p(sect,++port);
 249         outb_p(cyl,++port);
 250         outb_p(cyl>>8,++port);
 251         outb_p(0xA0|(drive<<4)|head,++port);
 252         outb_p(cmd,++port);
 253 }
 254 
 255 static void hd_request (void);
 256 static unsigned int identified  [MAX_HD] = {0,}; /* 1 = drive ID already displayed   */
 257 static unsigned int unmask_intr [MAX_HD] = {0,}; /* 1 = unmask IRQs during I/O       */
 258 static unsigned int max_mult    [MAX_HD] = {0,}; /* max sectors for MultMode         */
 259 static unsigned int mult_req    [MAX_HD] = {0,}; /* requested MultMode count         */
 260 static unsigned int mult_count  [MAX_HD] = {0,}; /* currently enabled MultMode count */
 261 static struct request WCURRENT;
 262 
 263 static void fixstring(unsigned char *s, int n)
     /* [previous][next][first][last][top][bottom][index][help] */
 264 {
 265         int i;
 266         unsigned short *ss = (unsigned short *) s;
 267 
 268         /* convert from big-endian to little-endian */
 269         for (i = n ; (i -= 2) >= 0 ; ss++)
 270                 *ss = (*ss >> 8) | (*ss << 8);
 271 
 272         /* "strnlen()" */
 273         for (i = 0 ; i < n ; i++) {
 274                 if (!s[i]) {
 275                         n = i;
 276                         break;
 277                 }
 278         }
 279 
 280         /* wipe out trailing spaces */
 281         while (n > 0) {
 282                 if (s[n-1] != ' ')
 283                         break;
 284                 n--;
 285                 s[n] = '\0';
 286         }
 287 
 288         /* wipe out leading spaces */
 289         if (*s == ' ') {
 290                 unsigned char *t = s;
 291                 while (n-- && *++s == ' ');
 292                 while (n-- >= 0) {
 293                         *t++ = *s;
 294                         *s++ = '\0';
 295                 }
 296         }
 297 }
 298 
 299 static void identify_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 300 {
 301         unsigned int dev = DEVICE_NR(CURRENT->dev);
 302         unsigned short stat = inb_p(HD_STATUS);
 303         struct hd_driveid *id = hd_ident_info[dev];
 304 
 305         if (unmask_intr[dev])
 306                 sti();
 307         if (stat & (BUSY_STAT|ERR_STAT)) {
 308                 printk ("  hd%c: non-IDE device, %dMB, CHS=%d/%d/%d\n", dev+'a',
 309                         hd_info[dev].cyl*hd_info[dev].head*hd_info[dev].sect / 2048,
 310                         hd_info[dev].cyl, hd_info[dev].head, hd_info[dev].sect);
 311                 if (id != NULL) {
 312                         hd_ident_info[dev] = NULL;
 313                         kfree_s (id, 512);
 314                 }
 315         } else {
 316                 insw(HD_DATA, id, 256); /* get ID info */
 317                 max_mult[dev] = id->max_multsect;
 318                 if ((id->field_valid&1) && id->cur_cyls && id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
 319                         /*
 320                          * Extract the physical drive geometry for our use.
 321                          * Note that we purposely do *not* update the bios_info.
 322                          * This way, programs that use it (like fdisk) will 
 323                          * still have the same logical view as the BIOS does,
 324                          * which keeps the partition table from being screwed.
 325                          */
 326                         hd_info[dev].cyl  = id->cur_cyls;
 327                         hd_info[dev].head = id->cur_heads;
 328                         hd_info[dev].sect = id->cur_sectors; 
 329                 }
 330                 fixstring (id->serial_no, sizeof(id->serial_no));
 331                 fixstring (id->fw_rev, sizeof(id->fw_rev));
 332                 fixstring (id->model, sizeof(id->model));
 333                 printk ("  hd%c: %.40s, %dMB w/%dKB Cache, CHS=%d/%d/%d, MaxMult=%d\n",
 334                         dev+'a', id->model, id->cyls*id->heads*id->sectors/2048,
 335                         id->buf_size/2, hd_info[dev].cyl, hd_info[dev].head,
 336                         hd_info[dev].sect, id->max_multsect);
 337                 /*
 338                  * Early model Quantum drives go weird at this point,
 339                  *   but doing a recalibrate seems to "fix" them.
 340                  * (Doing a full reset confuses some other model Quantums)
 341                  */
 342                 if (!strncmp(id->model, "QUANTUM", 7))
 343                         special_op[dev] = recalibrate[dev] = 1;
 344         }
 345 #if (HD_DELAY > 0)
 346         last_req = read_timer();
 347 #endif
 348         hd_request();
 349         return;
 350 }
 351 
 352 static void set_multmode_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 353 {
 354         unsigned int dev = DEVICE_NR(CURRENT->dev), stat = inb_p(HD_STATUS);
 355 
 356         if (unmask_intr[dev])
 357                 sti();
 358         if (stat & (BUSY_STAT|ERR_STAT)) {
 359                 mult_req[dev] = mult_count[dev] = 0;
 360                 dump_status("set multmode failed", stat);
 361         } else {
 362                 if ((mult_count[dev] = mult_req[dev]))
 363                         printk ("  hd%c: enabled %d-sector multiple mode\n",
 364                                 dev+'a', mult_count[dev]);
 365                 else
 366                         printk ("  hd%c: disabled multiple mode\n", dev+'a');
 367         }
 368 #if (HD_DELAY > 0)
 369         last_req = read_timer();
 370 #endif
 371         hd_request();
 372         return;
 373 }
 374 
 375 static int drive_busy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377         unsigned int i;
 378         unsigned char c;
 379 
 380         for (i = 0; i < 500000 ; i++) {
 381                 c = inb_p(HD_STATUS);
 382                 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
 383                         return 0;
 384         }
 385         dump_status("reset timed out", c);
 386         return 1;
 387 }
 388 
 389 static void reset_controller(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 390 {
 391         int     i;
 392 
 393         outb_p(4,HD_CMD);
 394         for(i = 0; i < 1000; i++) nop();
 395         outb_p(hd_info[0].ctl & 0x0f,HD_CMD);
 396         for(i = 0; i < 1000; i++) nop();
 397         if (drive_busy())
 398                 printk("hd: controller still busy\n");
 399         else if ((hd_error = inb(HD_ERROR)) != 1)
 400                 printk("hd: controller reset failed: %02x\n",hd_error);
 401 }
 402 
 403 static void reset_hd(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 404 {
 405         static int i;
 406 
 407 repeat:
 408         if (reset) {
 409                 reset = 0;
 410                 i = -1;
 411                 reset_controller();
 412         } else {
 413                 check_status();
 414                 if (reset)
 415                         goto repeat;
 416         }
 417         if (++i < NR_HD) {
 418                 special_op[i] = recalibrate[i] = 1;
 419                 if (unmask_intr[i]) {
 420                         unmask_intr[i] = DEFAULT_UNMASK_INTR;
 421                         printk("hd%c: reset irq-unmasking to %d\n",i+'a',
 422                                 DEFAULT_UNMASK_INTR);
 423                 }
 424                 if (mult_req[i] || mult_count[i]) {
 425                         mult_count[i] = 0;
 426                         mult_req[i] = DEFAULT_MULT_COUNT;
 427                         printk("hd%c: reset multiple mode to %d\n",i+'a',
 428                                 DEFAULT_MULT_COUNT);
 429                 }
 430                 hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
 431                         hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
 432                 if (reset)
 433                         goto repeat;
 434         } else
 435                 hd_request();
 436 }
 437 
 438 /*
 439  * Ok, don't know what to do with the unexpected interrupts: on some machines
 440  * doing a reset and a retry seems to result in an eternal loop. Right now I
 441  * ignore it, and just set the timeout.
 442  *
 443  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
 444  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
 445  * "good", we just ignore the interrupt completely.
 446  */
 447 void unexpected_hd_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 448 {
 449         unsigned int stat = inb_p(HD_STATUS);
 450 
 451         if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
 452                 dump_status ("unexpected interrupt", stat);
 453                 SET_TIMER;
 454         }
 455 }
 456 
 457 /*
 458  * bad_rw_intr() now tries to be a bit smarter and does things
 459  * according to the error returned by the controller.
 460  * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
 461  */
 462 static void bad_rw_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 463 {
 464         int dev;
 465 
 466         if (!CURRENT)
 467                 return;
 468         dev = DEVICE_NR(CURRENT->dev);
 469         if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
 470                 end_request(0);
 471                 special_op[dev] = recalibrate[dev] = 1;
 472         } else if (CURRENT->errors % RESET_FREQ == 0)
 473                 reset = 1;
 474         else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
 475                 special_op[dev] = recalibrate[dev] = 1;
 476         /* Otherwise just retry */
 477 }
 478 
 479 static inline int wait_DRQ(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 480 {
 481         int retries = 100000, stat;
 482 
 483         while (--retries > 0)
 484                 if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)
 485                         return 0;
 486         dump_status("wait_DRQ", stat);
 487         return -1;
 488 }
 489 
 490 static void read_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 491 {
 492         unsigned int dev = DEVICE_NR(CURRENT->dev);
 493         int i, retries = 100000, msect = mult_count[dev], nsect;
 494 
 495         if (unmask_intr[dev])
 496                 sti();                  /* permit other IRQs during xfer */
 497         do {
 498                 i = (unsigned) inb_p(HD_STATUS);
 499                 if (i & BUSY_STAT)
 500                         continue;
 501                 if (!OK_STATUS(i))
 502                         break;
 503                 if (i & DRQ_STAT)
 504                         goto ok_to_read;
 505         } while (--retries > 0);
 506         dump_status("read_intr", i);
 507         bad_rw_intr();
 508         hd_request();
 509         return;
 510 ok_to_read:
 511         if (msect) {
 512                 if ((nsect = CURRENT->current_nr_sectors) > msect)
 513                         nsect = msect;
 514                 msect -= nsect;
 515         } else
 516                 nsect = 1;
 517         insw(HD_DATA,CURRENT->buffer,nsect<<8);
 518         CURRENT->sector += nsect;
 519         CURRENT->buffer += nsect<<9;
 520         CURRENT->errors = 0;
 521         i = (CURRENT->nr_sectors -= nsect);
 522 
 523 #ifdef DEBUG
 524         printk("hd%c: read: sectors(%ld-%ld), remaining=%ld, buffer=0x%08lx\n",
 525                 dev+'a', CURRENT->sector, CURRENT->sector+nsect,
 526                 CURRENT->nr_sectors, (unsigned long) CURRENT->buffer+(nsect<<9));
 527 #endif
 528         if ((CURRENT->current_nr_sectors -= nsect) <= 0)
 529                 end_request(1);
 530         if (i > 0) {
 531                 if (msect)
 532                         goto ok_to_read;
 533                 SET_INTR(&read_intr);
 534                 return;
 535         }
 536         (void) inb_p(HD_STATUS);
 537 #if (HD_DELAY > 0)
 538         last_req = read_timer();
 539 #endif
 540         if (CURRENT)
 541                 hd_request();
 542         return;
 543 }
 544 
 545 static inline void multwrite (unsigned int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 546 {
 547         unsigned int mcount = mult_count[dev];
 548 
 549         while (mcount--) {
 550                 outsw(HD_DATA,WCURRENT.buffer,256);
 551                 if (!--WCURRENT.nr_sectors)
 552                         return;
 553                 WCURRENT.buffer += 512;
 554                 if (!--WCURRENT.current_nr_sectors) {
 555                         WCURRENT.bh = WCURRENT.bh->b_reqnext;
 556                         if (WCURRENT.bh == NULL)
 557                                 panic("buffer list corrupted\n");
 558                         WCURRENT.current_nr_sectors = WCURRENT.bh->b_size>>9;
 559                         WCURRENT.buffer             = WCURRENT.bh->b_data;
 560                 }
 561         }
 562 }
 563 
 564 static void multwrite_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 565 {
 566         int i;
 567         unsigned int dev = DEVICE_NR(WCURRENT.dev);
 568 
 569         if (unmask_intr[dev])
 570                 sti();
 571         if (OK_STATUS(i=inb_p(HD_STATUS))) {
 572                 if (i & DRQ_STAT) {
 573                         if (WCURRENT.nr_sectors) {
 574                                 multwrite(dev);
 575                                 SET_INTR(&multwrite_intr);
 576                                 return;
 577                         }
 578                 } else {
 579                         if (!WCURRENT.nr_sectors) {     /* all done? */
 580                                 for (i = CURRENT->nr_sectors; i > 0;){
 581                                         i -= CURRENT->current_nr_sectors;
 582                                         end_request(1);
 583                                 }
 584 #if (HD_DELAY > 0)
 585                                 last_req = read_timer();
 586 #endif
 587                                 if (CURRENT)
 588                                         hd_request();
 589                                 return;
 590                         }
 591                 }
 592         }
 593         dump_status("multwrite_intr", i);
 594         bad_rw_intr();
 595         hd_request();
 596 }
 597 
 598 static void write_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 599 {
 600         int i;
 601         int retries = 100000;
 602 
 603         if (unmask_intr[DEVICE_NR(WCURRENT.dev)])
 604                 sti();
 605         do {
 606                 i = (unsigned) inb_p(HD_STATUS);
 607                 if (i & BUSY_STAT)
 608                         continue;
 609                 if (!OK_STATUS(i))
 610                         break;
 611                 if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
 612                         goto ok_to_write;
 613         } while (--retries > 0);
 614         dump_status("write_intr", i);
 615         bad_rw_intr();
 616         hd_request();
 617         return;
 618 ok_to_write:
 619         CURRENT->sector++;
 620         i = --CURRENT->nr_sectors;
 621         --CURRENT->current_nr_sectors;
 622         CURRENT->buffer += 512;
 623         if (!i || (CURRENT->bh && !SUBSECTOR(i)))
 624                 end_request(1);
 625         if (i > 0) {
 626                 SET_INTR(&write_intr);
 627                 outsw(HD_DATA,CURRENT->buffer,256);
 628                 sti();
 629         } else {
 630 #if (HD_DELAY > 0)
 631                 last_req = read_timer();
 632 #endif
 633                 hd_request();
 634         }
 635         return;
 636 }
 637 
 638 static void recal_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 639 {
 640         check_status();
 641 #if (HD_DELAY > 0)
 642         last_req = read_timer();
 643 #endif
 644         hd_request();
 645 }
 646 
 647 /*
 648  * This is another of the error-routines I don't know what to do with. The
 649  * best idea seems to just set reset, and start all over again.
 650  */
 651 static void hd_times_out(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 652 {
 653         unsigned int dev;
 654 
 655         DEVICE_INTR = NULL;
 656         if (!CURRENT)
 657                 return;
 658         disable_irq(HD_IRQ);
 659         sti();
 660         reset = 1;
 661         dev = DEVICE_NR(CURRENT->dev);
 662         printk("hd%c: timeout\n", dev+'a');
 663         if (++CURRENT->errors >= MAX_ERRORS) {
 664 #ifdef DEBUG
 665                 printk("hd%c: too many errors\n", dev+'a');
 666 #endif
 667                 end_request(0);
 668         }
 669         cli();
 670         hd_request();
 671         enable_irq(HD_IRQ);
 672 }
 673 
 674 int do_special_op (unsigned int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 675 {
 676         if (recalibrate[dev]) {
 677                 recalibrate[dev] = 0;
 678                 hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
 679                 return reset;
 680         }
 681         if (!identified[dev]) {
 682                 identified[dev]  = 1;
 683                 unmask_intr[dev] = DEFAULT_UNMASK_INTR;
 684                 mult_req[dev]    = DEFAULT_MULT_COUNT;
 685                 hd_out(dev,0,0,0,0,WIN_IDENTIFY,&identify_intr);
 686                 return reset;
 687         }
 688         if (mult_req[dev] != mult_count[dev]) {
 689                 hd_out(dev,mult_req[dev],0,0,0,WIN_SETMULT,&set_multmode_intr);
 690                 return reset;
 691         }
 692         if (hd_info[dev].head > 16) {
 693                 printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
 694                 end_request(0);
 695         }
 696         special_op[dev] = 0;
 697         return 1;
 698 }
 699 
 700 /*
 701  * The driver enables interrupts as much as possible.  In order to do this,
 702  * (a) the device-interrupt is disabled before entering hd_request(),
 703  * and (b) the timeout-interrupt is disabled before the sti().
 704  *
 705  * Interrupts are still masked (by default) whenever we are exchanging
 706  * data/cmds with a drive, because some drives seem to have very poor
 707  * tolerance for latency during I/O.  For devices which don't suffer from
 708  * that problem (most don't), the unmask_intr[] flag can be set to unmask
 709  * other interrupts during data/cmd transfers (by defining DEFAULT_UNMASK_INTR
 710  * to 1, or by using "hdparm -u1 /dev/hd?" from the shell).
 711  */
 712 static void hd_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 713 {
 714         unsigned int dev, block, nsect, sec, track, head, cyl;
 715 
 716         if (CURRENT && CURRENT->dev < 0) return;
 717         if (DEVICE_INTR)
 718                 return;
 719 repeat:
 720         timer_active &= ~(1<<HD_TIMER);
 721         sti();
 722         INIT_REQUEST;
 723         if (reset) {
 724                 cli();
 725                 reset_hd();
 726                 return;
 727         }
 728         dev = MINOR(CURRENT->dev);
 729         block = CURRENT->sector;
 730         nsect = CURRENT->nr_sectors;
 731         if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {
 732 #ifdef DEBUG
 733                 if (dev >= (NR_HD<<6))
 734                         printk("hd: bad minor number: device=0x%04x\n", CURRENT->dev);
 735                 else
 736                         printk("hd%c: bad access: block=%d, count=%d\n",
 737                                 (CURRENT->dev>>6)+'a', block, nsect);
 738 #endif
 739                 end_request(0);
 740                 goto repeat;
 741         }
 742         block += hd[dev].start_sect;
 743         dev >>= 6;
 744         if (special_op[dev]) {
 745                 if (do_special_op(dev))
 746                         goto repeat;
 747                 return;
 748         }
 749         sec   = block % hd_info[dev].sect + 1;
 750         track = block / hd_info[dev].sect;
 751         head  = track % hd_info[dev].head;
 752         cyl   = track / hd_info[dev].head;
 753 #ifdef DEBUG
 754         printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
 755                 dev+'a', (CURRENT->cmd == READ)?"read":"writ",
 756                 cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);
 757 #endif
 758         if (!unmask_intr[dev])
 759                 cli();
 760         if (CURRENT->cmd == READ) {
 761                 unsigned int cmd = mult_count[dev] > 1 ? WIN_MULTREAD : WIN_READ;
 762                 hd_out(dev,nsect,sec,head,cyl,cmd,&read_intr);
 763                 if (reset)
 764                         goto repeat;
 765                 return;
 766         }
 767         if (CURRENT->cmd == WRITE) {
 768                 if (mult_count[dev])
 769                         hd_out(dev,nsect,sec,head,cyl,WIN_MULTWRITE,&multwrite_intr);
 770                 else
 771                         hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
 772                 if (reset)
 773                         goto repeat;
 774                 if (wait_DRQ()) {
 775                         bad_rw_intr();
 776                         goto repeat;
 777                 }
 778                 if (mult_count[dev]) {
 779                         WCURRENT = *CURRENT;
 780                         multwrite(dev);
 781                 } else
 782                         outsw(HD_DATA,CURRENT->buffer,256);
 783                 return;
 784         }
 785         panic("unknown hd-command");
 786 }
 787 
 788 static void do_hd_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 789 {
 790         disable_irq(HD_IRQ);
 791         hd_request();
 792         enable_irq(HD_IRQ);
 793 }
 794 
 795 static int hd_ioctl(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 796         unsigned int cmd, unsigned long arg)
 797 {
 798         struct hd_geometry *loc = (struct hd_geometry *) arg;
 799         int dev, err;
 800         unsigned long flags;
 801 
 802         if ((!inode) || (!inode->i_rdev))
 803                 return -EINVAL;
 804         dev = DEVICE_NR(inode->i_rdev);
 805         if (dev >= NR_HD)
 806                 return -EINVAL;
 807         switch (cmd) {
 808                 case HDIO_GETGEO:
 809                         if (!loc)  return -EINVAL;
 810                         err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
 811                         if (err)
 812                                 return err;
 813                         put_fs_byte(bios_info[dev].head,
 814                                 (char *) &loc->heads);
 815                         put_fs_byte(bios_info[dev].sect,
 816                                 (char *) &loc->sectors);
 817                         put_fs_word(bios_info[dev].cyl,
 818                                 (short *) &loc->cylinders);
 819                         put_fs_long(hd[MINOR(inode->i_rdev)].start_sect,
 820                                 (long *) &loc->start);
 821                         return 0;
 822                 case BLKRASET:
 823                         if(!suser())  return -EACCES;
 824                         if(arg > 0xff) return -EINVAL;
 825                         read_ahead[MAJOR(inode->i_rdev)] = arg;
 826                         return 0;
 827                 case BLKRAGET:
 828                         if (!arg)  return -EINVAL;
 829                         err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
 830                         if (err)
 831                                 return err;
 832                         put_fs_long(read_ahead[MAJOR(inode->i_rdev)],(long *) arg);
 833                         return 0;
 834                 case BLKGETSIZE:   /* Return device size */
 835                         if (!arg)  return -EINVAL;
 836                         err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
 837                         if (err)
 838                                 return err;
 839                         put_fs_long(hd[MINOR(inode->i_rdev)].nr_sects, (long *) arg);
 840                         return 0;
 841                 case BLKFLSBUF:
 842                         if(!suser())  return -EACCES;
 843                         fsync_dev(inode->i_rdev);
 844                         invalidate_buffers(inode->i_rdev);
 845                         return 0;
 846 
 847                 case BLKRRPART: /* Re-read partition tables */
 848                         return revalidate_hddisk(inode->i_rdev, 1);
 849 
 850                 case HDIO_SETUNMASKINTR:        /* obsolete */
 851                         printk("hd: obsolete syscall: HDIO_SETUNMASKINTR\n");
 852                         if (!arg)  return -EINVAL;
 853                         err = verify_area(VERIFY_READ, (long *) arg, sizeof(long));
 854                         if (err)
 855                                 return err;
 856                         arg = get_fs_long((long *) arg);
 857                         /* drop into HDIO_SET_UNMASKINTR */
 858                 case HDIO_SET_UNMASKINTR:
 859                         if (!suser()) return -EACCES;
 860                         if ((arg > 1) || (MINOR(inode->i_rdev) & 0x3F))
 861                                 return -EINVAL;
 862                         unmask_intr[dev] = arg;
 863                         return 0;
 864 
 865                 case HDIO_GET_UNMASKINTR:
 866                         if (!arg)  return -EINVAL;
 867                         err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
 868                         if (err)
 869                                 return err;
 870                         put_fs_long(unmask_intr[dev], (long *) arg);
 871                         return 0;
 872 
 873                 case HDIO_GET_MULTCOUNT:
 874                         if (!arg)  return -EINVAL;
 875                         err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
 876                         if (err)
 877                                 return err;
 878                         put_fs_long(mult_count[dev], (long *) arg);
 879                         return 0;
 880 
 881                 case HDIO_SETMULTCOUNT:         /* obsolete */
 882                         printk("hd: obsolete syscall: HDIO_SETMULTCOUNT\n");
 883                         if (!arg)  return -EINVAL;
 884                         err = verify_area(VERIFY_READ, (long *) arg, sizeof(long));
 885                         if (err)
 886                                 return err;
 887                         arg = get_fs_long((long *) arg);
 888                         /* drop into HDIO_SET_MULTCOUNT */
 889                 case HDIO_SET_MULTCOUNT:
 890                         if (!suser()) return -EACCES;
 891                         if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
 892                         save_flags(flags);
 893                         cli();  /* a prior request might still be in progress */
 894                         if (arg > max_mult[dev])
 895                                 err = -EINVAL;  /* out of range for device */
 896                         else if (mult_req[dev] != mult_count[dev]) {
 897                                 special_op[dev] = 1;
 898                                 err = -EBUSY;   /* busy, try again */
 899                         } else {
 900                                 mult_req[dev] = arg;
 901                                 special_op[dev] = 1;
 902                                 err = 0;
 903                         }
 904                         restore_flags(flags);
 905                         return err;
 906 
 907                 case HDIO_GET_IDENTITY:
 908                         if (!arg)  return -EINVAL;
 909                         if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
 910                         if (hd_ident_info[dev] == NULL)  return -ENOMSG;
 911                         err = verify_area(VERIFY_WRITE, (char *) arg, sizeof(struct hd_driveid));
 912                         if (err)
 913                                 return err;
 914                         memcpy_tofs((char *)arg, (char *) hd_ident_info[dev], sizeof(struct hd_driveid));
 915                         return 0;
 916 
 917                 RO_IOCTLS(inode->i_rdev,arg);
 918                 default:
 919                         return -EINVAL;
 920         }
 921 }
 922 
 923 static int hd_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 924 {
 925         int target;
 926         target =  DEVICE_NR(inode->i_rdev);
 927 
 928         while (busy[target])
 929                 sleep_on(&busy_wait);
 930         access_count[target]++;
 931         return 0;
 932 }
 933 
 934 /*
 935  * Releasing a block device means we sync() it, so that it can safely
 936  * be forgotten about...
 937  */
 938 static void hd_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
 939 {
 940         int target;
 941         sync_dev(inode->i_rdev);
 942 
 943         target =  DEVICE_NR(inode->i_rdev);
 944         access_count[target]--;
 945 
 946 }
 947 
 948 static void hd_geninit(void);
 949 
 950 static struct gendisk hd_gendisk = {
 951         MAJOR_NR,       /* Major number */      
 952         "hd",           /* Major name */
 953         6,              /* Bits to shift to get real from partition */
 954         1 << 6,         /* Number of partitions per real */
 955         MAX_HD,         /* maximum number of real */
 956         hd_geninit,     /* init function */
 957         hd,             /* hd struct */
 958         hd_sizes,       /* block sizes */
 959         0,              /* number */
 960         (void *) bios_info,     /* internal */
 961         NULL            /* next */
 962 };
 963         
 964 static void hd_interrupt(int unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 965 {
 966         void (*handler)(void) = DEVICE_INTR;
 967 
 968         DEVICE_INTR = NULL;
 969         timer_active &= ~(1<<HD_TIMER);
 970         if (!handler)
 971                 handler = unexpected_hd_interrupt;
 972         handler();
 973         sti();
 974 }
 975 
 976 /*
 977  * This is the harddisk IRQ description. The SA_INTERRUPT in sa_flags
 978  * means we run the IRQ-handler with interrupts disabled: this is bad for
 979  * interrupt latency, but anything else has led to problems on some
 980  * machines...
 981  *
 982  * We enable interrupts in some of the routines after making sure it's
 983  * safe.
 984  */
 985 static void hd_geninit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 986 {
 987         int drive, i;
 988         extern struct drive_info drive_info;
 989         unsigned char *BIOS = (unsigned char *) &drive_info;
 990         int cmos_disks;
 991 
 992         if (!NR_HD) {      
 993                 for (drive=0 ; drive<2 ; drive++) {
 994                         bios_info[drive].cyl   = hd_info[drive].cyl = *(unsigned short *) BIOS;
 995                         bios_info[drive].head  = hd_info[drive].head = *(2+BIOS);
 996                         bios_info[drive].wpcom = hd_info[drive].wpcom = *(unsigned short *) (5+BIOS);
 997                         bios_info[drive].ctl   = hd_info[drive].ctl = *(8+BIOS);
 998                         bios_info[drive].lzone = hd_info[drive].lzone = *(unsigned short *) (12+BIOS);
 999                         bios_info[drive].sect  = hd_info[drive].sect = *(14+BIOS);
1000 #ifdef does_not_work_for_everybody_with_scsi_but_helps_ibm_vp
1001                         if (hd_info[drive].cyl && NR_HD == drive)
1002                                 NR_HD++;
1003 #endif
1004                         BIOS += 16;
1005                 }
1006 
1007         /*
1008                 We query CMOS about hard disks : it could be that 
1009                 we have a SCSI/ESDI/etc controller that is BIOS
1010                 compatible with ST-506, and thus showing up in our
1011                 BIOS table, but not register compatible, and therefore
1012                 not present in CMOS.
1013 
1014                 Furthermore, we will assume that our ST-506 drives
1015                 <if any> are the primary drives in the system, and 
1016                 the ones reflected as drive 1 or 2.
1017 
1018                 The first drive is stored in the high nibble of CMOS
1019                 byte 0x12, the second in the low nibble.  This will be
1020                 either a 4 bit drive type or 0xf indicating use byte 0x19 
1021                 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.
1022 
1023                 Needless to say, a non-zero value means we have 
1024                 an AT controller hard disk for that drive.
1025 
1026                 
1027         */
1028 
1029                 if ((cmos_disks = CMOS_READ(0x12)) & 0xf0)
1030                         if (cmos_disks & 0x0f)
1031                                 NR_HD = 2;
1032                         else
1033                                 NR_HD = 1;
1034         }
1035         i = NR_HD;
1036         while (i-- > 0) {
1037                 /*
1038                  * The newer E-IDE BIOSs handle drives larger than 1024
1039                  * cylinders by increasing the number of logical heads
1040                  * to keep the number of logical cylinders below the
1041                  * sacred INT13 limit of 1024 (10 bits).  If that is
1042                  * what's happening here, we'll find out and correct
1043                  * it later when "identifying" the drive.
1044                  */
1045                 hd[i<<6].nr_sects = bios_info[i].head *
1046                                 bios_info[i].sect * bios_info[i].cyl;
1047                 hd_ident_info[i] = (struct hd_driveid *) kmalloc(512,GFP_KERNEL);
1048                 special_op[i] = 1;
1049         }
1050         if (NR_HD) {
1051                 if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd")) {
1052                         printk("hd: unable to get IRQ%d for the harddisk driver\n",HD_IRQ);
1053                         NR_HD = 0;
1054                 }
1055         }
1056         hd_gendisk.nr_real = NR_HD;
1057 
1058         for(i=0;i<(MAX_HD << 6);i++) hd_blocksizes[i] = 1024;
1059         blksize_size[MAJOR_NR] = hd_blocksizes;
1060 }
1061 
1062 static struct file_operations hd_fops = {
1063         NULL,                   /* lseek - default */
1064         block_read,             /* read - general block-dev read */
1065         block_write,            /* write - general block-dev write */
1066         NULL,                   /* readdir - bad */
1067         NULL,                   /* select */
1068         hd_ioctl,               /* ioctl */
1069         NULL,                   /* mmap */
1070         hd_open,                /* open */
1071         hd_release,             /* release */
1072         block_fsync             /* fsync */
1073 };
1074 
1075 unsigned long hd_init(unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1076 {
1077         if (register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
1078                 printk("hd: unable to get major %d for harddisk\n",MAJOR_NR);
1079                 return mem_start;
1080         }
1081         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1082         read_ahead[MAJOR_NR] = 8;               /* 8 sector (4kB) read-ahead */
1083         hd_gendisk.next = gendisk_head;
1084         gendisk_head = &hd_gendisk;
1085         timer_table[HD_TIMER].fn = hd_times_out;
1086         return mem_start;
1087 }
1088 
1089 #define DEVICE_BUSY busy[target]
1090 #define USAGE access_count[target]
1091 #define CAPACITY (bios_info[target].head*bios_info[target].sect*bios_info[target].cyl)
1092 /* We assume that the the bios parameters do not change, so the disk capacity
1093    will not change */
1094 #undef MAYBE_REINIT
1095 #define GENDISK_STRUCT hd_gendisk
1096 
1097 /*
1098  * This routine is called to flush all partitions and partition tables
1099  * for a changed scsi disk, and then re-read the new partition table.
1100  * If we are revalidating a disk because of a media change, then we
1101  * enter with usage == 0.  If we are using an ioctl, we automatically have
1102  * usage == 1 (we need an open channel to use an ioctl :-), so this
1103  * is our limit.
1104  */
1105 static int revalidate_hddisk(int dev, int maxusage)
     /* [previous][next][first][last][top][bottom][index][help] */
1106 {
1107         int target, major;
1108         struct gendisk * gdev;
1109         int max_p;
1110         int start;
1111         int i;
1112         long flags;
1113 
1114         target =  DEVICE_NR(dev);
1115         gdev = &GENDISK_STRUCT;
1116 
1117         save_flags(flags);
1118         cli();
1119         if (DEVICE_BUSY || USAGE > maxusage) {
1120                 restore_flags(flags);
1121                 return -EBUSY;
1122         };
1123         DEVICE_BUSY = 1;
1124         restore_flags(flags);
1125 
1126         max_p = gdev->max_p;
1127         start = target << gdev->minor_shift;
1128         major = MAJOR_NR << 8;
1129 
1130         for (i=max_p - 1; i >=0 ; i--) {
1131                 sync_dev(major | start | i);
1132                 invalidate_inodes(major | start | i);
1133                 invalidate_buffers(major | start | i);
1134                 gdev->part[start+i].start_sect = 0;
1135                 gdev->part[start+i].nr_sects = 0;
1136         };
1137 
1138 #ifdef MAYBE_REINIT
1139         MAYBE_REINIT;
1140 #endif
1141 
1142         gdev->part[start].nr_sects = CAPACITY;
1143         resetup_one_dev(gdev, target);
1144 
1145         DEVICE_BUSY = 0;
1146         wake_up(&busy_wait);
1147         return 0;
1148 }
1149 

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