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, CHS=%d/%d/%d\n", dev+'a',
 309                         hd_info[dev].cyl, hd_info[dev].head, hd_info[dev].sect);
 310                 if (id != NULL) {
 311                         hd_ident_info[dev] = NULL;
 312                         kfree_s (id, 512);
 313                 }
 314         } else {
 315                 insw(HD_DATA, id, 256); /* get ID info */
 316                 max_mult[dev] = id->max_multsect;
 317                 if ((id->field_valid&1) && id->cur_cyls && id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
 318                         /*
 319                          * Extract the physical drive geometry for our use.
 320                          * Note that we purposely do *not* update the bios_info.
 321                          * This way, programs that use it (like fdisk) will 
 322                          * still have the same logical view as the BIOS does,
 323                          * which keeps the partition table from being screwed.
 324                          */
 325                         hd_info[dev].cyl  = id->cur_cyls;
 326                         hd_info[dev].head = id->cur_heads;
 327                         hd_info[dev].sect = id->cur_sectors; 
 328                 }
 329                 fixstring (id->serial_no, sizeof(id->serial_no));
 330                 fixstring (id->fw_rev, sizeof(id->fw_rev));
 331                 fixstring (id->model, sizeof(id->model));
 332                 printk ("  hd%c: %.40s, %dMB w/%dKB Cache, CHS=%d/%d/%d, MaxMult=%d\n",
 333                         dev+'a', id->model, id->cyls*id->heads*id->sectors/2048,
 334                         id->buf_size/2, hd_info[dev].cyl, hd_info[dev].head,
 335                         hd_info[dev].sect, id->max_multsect);
 336                 /*
 337                  * Early model Quantum drives go weird at this point,
 338                  *   but doing a recalibrate seems to "fix" them.
 339                  * (Doing a full reset confuses some other model Quantums)
 340                  */
 341                 if (!strncmp(id->model, "QUANTUM", 7))
 342                         special_op[dev] = recalibrate[dev] = 1;
 343         }
 344 #if (HD_DELAY > 0)
 345         last_req = read_timer();
 346 #endif
 347         hd_request();
 348         return;
 349 }
 350 
 351 static void set_multmode_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 352 {
 353         unsigned int dev = DEVICE_NR(CURRENT->dev), stat = inb_p(HD_STATUS);
 354 
 355         if (unmask_intr[dev])
 356                 sti();
 357         if (stat & (BUSY_STAT|ERR_STAT)) {
 358                 mult_req[dev] = mult_count[dev] = 0;
 359                 dump_status("set multmode failed", stat);
 360         } else {
 361                 if ((mult_count[dev] = mult_req[dev]))
 362                         printk ("  hd%c: enabled %d-sector multiple mode\n",
 363                                 dev+'a', mult_count[dev]);
 364                 else
 365                         printk ("  hd%c: disabled multiple mode\n", dev+'a');
 366         }
 367 #if (HD_DELAY > 0)
 368         last_req = read_timer();
 369 #endif
 370         hd_request();
 371         return;
 372 }
 373 
 374 static int drive_busy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 375 {
 376         unsigned int i;
 377         unsigned char c;
 378 
 379         for (i = 0; i < 500000 ; i++) {
 380                 c = inb_p(HD_STATUS);
 381                 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
 382                         return 0;
 383         }
 384         dump_status("reset timed out", c);
 385         return 1;
 386 }
 387 
 388 static void reset_controller(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 389 {
 390         int     i;
 391 
 392         outb_p(4,HD_CMD);
 393         for(i = 0; i < 1000; i++) nop();
 394         outb_p(hd_info[0].ctl & 0x0f,HD_CMD);
 395         for(i = 0; i < 1000; i++) nop();
 396         if (drive_busy())
 397                 printk("hd: controller still busy\n");
 398         else if ((hd_error = inb(HD_ERROR)) != 1)
 399                 printk("hd: controller reset failed: %02x\n",hd_error);
 400 }
 401 
 402 static void reset_hd(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 403 {
 404         static int i;
 405 
 406 repeat:
 407         if (reset) {
 408                 reset = 0;
 409                 i = -1;
 410                 reset_controller();
 411         } else {
 412                 check_status();
 413                 if (reset)
 414                         goto repeat;
 415         }
 416         if (++i < NR_HD) {
 417                 special_op[i] = recalibrate[i] = 1;
 418                 if (unmask_intr[i]) {
 419                         unmask_intr[i] = DEFAULT_UNMASK_INTR;
 420                         printk("hd%c: reset irq-unmasking to %d\n",i+'a',
 421                                 DEFAULT_UNMASK_INTR);
 422                 }
 423                 if (mult_req[i] || mult_count[i]) {
 424                         mult_count[i] = 0;
 425                         mult_req[i] = DEFAULT_MULT_COUNT;
 426                         printk("hd%c: reset multiple mode to %d\n",i+'a',
 427                                 DEFAULT_MULT_COUNT);
 428                 }
 429                 hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
 430                         hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
 431                 if (reset)
 432                         goto repeat;
 433         } else
 434                 hd_request();
 435 }
 436 
 437 /*
 438  * Ok, don't know what to do with the unexpected interrupts: on some machines
 439  * doing a reset and a retry seems to result in an eternal loop. Right now I
 440  * ignore it, and just set the timeout.
 441  *
 442  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
 443  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
 444  * "good", we just ignore the interrupt completely.
 445  */
 446 void unexpected_hd_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 447 {
 448         unsigned int stat = inb_p(HD_STATUS);
 449 
 450         if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
 451                 dump_status ("unexpected interrupt", stat);
 452                 SET_TIMER;
 453         }
 454 }
 455 
 456 /*
 457  * bad_rw_intr() now tries to be a bit smarter and does things
 458  * according to the error returned by the controller.
 459  * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
 460  */
 461 static void bad_rw_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 462 {
 463         int dev;
 464 
 465         if (!CURRENT)
 466                 return;
 467         dev = DEVICE_NR(CURRENT->dev);
 468         if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
 469                 end_request(0);
 470                 special_op[dev] = recalibrate[dev] = 1;
 471         } else if (CURRENT->errors % RESET_FREQ == 0)
 472                 reset = 1;
 473         else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
 474                 special_op[dev] = recalibrate[dev] = 1;
 475         /* Otherwise just retry */
 476 }
 477 
 478 static inline int wait_DRQ(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 479 {
 480         int retries = 100000, stat;
 481 
 482         while (--retries > 0)
 483                 if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)
 484                         return 0;
 485         dump_status("wait_DRQ", stat);
 486         return -1;
 487 }
 488 
 489 static void read_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 490 {
 491         unsigned int dev = DEVICE_NR(CURRENT->dev);
 492         int i, retries = 100000, msect = mult_count[dev], nsect;
 493 
 494         if (unmask_intr[dev])
 495                 sti();                  /* permit other IRQs during xfer */
 496         do {
 497                 i = (unsigned) inb_p(HD_STATUS);
 498                 if (i & BUSY_STAT)
 499                         continue;
 500                 if (!OK_STATUS(i))
 501                         break;
 502                 if (i & DRQ_STAT)
 503                         goto ok_to_read;
 504         } while (--retries > 0);
 505         dump_status("read_intr", i);
 506         bad_rw_intr();
 507         hd_request();
 508         return;
 509 ok_to_read:
 510         if (msect) {
 511                 if ((nsect = CURRENT->current_nr_sectors) > msect)
 512                         nsect = msect;
 513                 msect -= nsect;
 514         } else
 515                 nsect = 1;
 516         insw(HD_DATA,CURRENT->buffer,nsect<<8);
 517         CURRENT->sector += nsect;
 518         CURRENT->buffer += nsect<<9;
 519         CURRENT->errors = 0;
 520         i = (CURRENT->nr_sectors -= nsect);
 521 
 522 #ifdef DEBUG
 523         printk("hd%c: read: sectors(%ld-%ld), remaining=%ld, buffer=0x%08lx\n",
 524                 dev+'a', CURRENT->sector, CURRENT->sector+nsect,
 525                 CURRENT->nr_sectors, (unsigned long) CURRENT->buffer+(nsect<<9));
 526 #endif
 527         if ((CURRENT->current_nr_sectors -= nsect) <= 0)
 528                 end_request(1);
 529         if (i > 0) {
 530                 if (msect)
 531                         goto ok_to_read;
 532                 SET_INTR(&read_intr);
 533                 return;
 534         }
 535         (void) inb_p(HD_STATUS);
 536 #if (HD_DELAY > 0)
 537         last_req = read_timer();
 538 #endif
 539         if (CURRENT)
 540                 hd_request();
 541         return;
 542 }
 543 
 544 static inline void multwrite (unsigned int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 545 {
 546         unsigned int mcount = mult_count[dev];
 547 
 548         while (mcount--) {
 549                 outsw(HD_DATA,WCURRENT.buffer,256);
 550                 if (!--WCURRENT.nr_sectors)
 551                         return;
 552                 WCURRENT.buffer += 512;
 553                 if (!--WCURRENT.current_nr_sectors) {
 554                         WCURRENT.bh = WCURRENT.bh->b_reqnext;
 555                         if (WCURRENT.bh == NULL)
 556                                 panic("buffer list corrupted\n");
 557                         WCURRENT.current_nr_sectors = WCURRENT.bh->b_size>>9;
 558                         WCURRENT.buffer             = WCURRENT.bh->b_data;
 559                 }
 560         }
 561 }
 562 
 563 static void multwrite_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 564 {
 565         int i;
 566         unsigned int dev = DEVICE_NR(WCURRENT.dev);
 567 
 568         if (unmask_intr[dev])
 569                 sti();
 570         if (OK_STATUS(i=inb_p(HD_STATUS))) {
 571                 if (i & DRQ_STAT) {
 572                         if (WCURRENT.nr_sectors) {
 573                                 multwrite(dev);
 574                                 SET_INTR(&multwrite_intr);
 575                                 return;
 576                         }
 577                 } else {
 578                         if (!WCURRENT.nr_sectors) {     /* all done? */
 579                                 for (i = CURRENT->nr_sectors; i > 0;){
 580                                         i -= CURRENT->current_nr_sectors;
 581                                         end_request(1);
 582                                 }
 583 #if (HD_DELAY > 0)
 584                                 last_req = read_timer();
 585 #endif
 586                                 if (CURRENT)
 587                                         hd_request();
 588                                 return;
 589                         }
 590                 }
 591         }
 592         dump_status("multwrite_intr", i);
 593         bad_rw_intr();
 594         hd_request();
 595 }
 596 
 597 static void write_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 598 {
 599         int i;
 600         int retries = 100000;
 601 
 602         if (unmask_intr[DEVICE_NR(WCURRENT.dev)])
 603                 sti();
 604         do {
 605                 i = (unsigned) inb_p(HD_STATUS);
 606                 if (i & BUSY_STAT)
 607                         continue;
 608                 if (!OK_STATUS(i))
 609                         break;
 610                 if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
 611                         goto ok_to_write;
 612         } while (--retries > 0);
 613         dump_status("write_intr", i);
 614         bad_rw_intr();
 615         hd_request();
 616         return;
 617 ok_to_write:
 618         CURRENT->sector++;
 619         i = --CURRENT->nr_sectors;
 620         --CURRENT->current_nr_sectors;
 621         CURRENT->buffer += 512;
 622         if (!i || (CURRENT->bh && !SUBSECTOR(i)))
 623                 end_request(1);
 624         if (i > 0) {
 625                 SET_INTR(&write_intr);
 626                 outsw(HD_DATA,CURRENT->buffer,256);
 627                 sti();
 628         } else {
 629 #if (HD_DELAY > 0)
 630                 last_req = read_timer();
 631 #endif
 632                 hd_request();
 633         }
 634         return;
 635 }
 636 
 637 static void recal_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 638 {
 639         check_status();
 640 #if (HD_DELAY > 0)
 641         last_req = read_timer();
 642 #endif
 643         hd_request();
 644 }
 645 
 646 /*
 647  * This is another of the error-routines I don't know what to do with. The
 648  * best idea seems to just set reset, and start all over again.
 649  */
 650 static void hd_times_out(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 651 {
 652         unsigned int dev;
 653 
 654         DEVICE_INTR = NULL;
 655         if (!CURRENT)
 656                 return;
 657         disable_irq(HD_IRQ);
 658         sti();
 659         reset = 1;
 660         dev = DEVICE_NR(CURRENT->dev);
 661         printk("hd%c: timeout\n", dev+'a');
 662         if (++CURRENT->errors >= MAX_ERRORS) {
 663 #ifdef DEBUG
 664                 printk("hd%c: too many errors\n", dev+'a');
 665 #endif
 666                 end_request(0);
 667         }
 668         cli();
 669         hd_request();
 670         enable_irq(HD_IRQ);
 671 }
 672 
 673 int do_special_op (unsigned int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 674 {
 675         if (recalibrate[dev]) {
 676                 recalibrate[dev] = 0;
 677                 hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
 678                 return reset;
 679         }
 680         if (!identified[dev]) {
 681                 identified[dev]  = 1;
 682                 unmask_intr[dev] = DEFAULT_UNMASK_INTR;
 683                 mult_req[dev]    = DEFAULT_MULT_COUNT;
 684                 hd_out(dev,0,0,0,0,WIN_IDENTIFY,&identify_intr);
 685                 return reset;
 686         }
 687         if (mult_req[dev] != mult_count[dev]) {
 688                 hd_out(dev,mult_req[dev],0,0,0,WIN_SETMULT,&set_multmode_intr);
 689                 return reset;
 690         }
 691         if (hd_info[dev].head > 16) {
 692                 printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
 693                 end_request(0);
 694         }
 695         special_op[dev] = 0;
 696         return 1;
 697 }
 698 
 699 /*
 700  * The driver enables interrupts as much as possible.  In order to do this,
 701  * (a) the device-interrupt is disabled before entering hd_request(),
 702  * and (b) the timeout-interrupt is disabled before the sti().
 703  *
 704  * Interrupts are still masked (by default) whenever we are exchanging
 705  * data/cmds with a drive, because some drives seem to have very poor
 706  * tolerance for latency during I/O.  For devices which don't suffer from
 707  * that problem (most don't), the unmask_intr[] flag can be set to unmask
 708  * other interrupts during data/cmd transfers (by defining DEFAULT_UNMASK_INTR
 709  * to 1, or by using "hdparm -u1 /dev/hd?" from the shell).
 710  */
 711 static void hd_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 712 {
 713         unsigned int dev, block, nsect, sec, track, head, cyl;
 714 
 715         if (CURRENT && CURRENT->dev < 0) return;
 716         if (DEVICE_INTR)
 717                 return;
 718 repeat:
 719         timer_active &= ~(1<<HD_TIMER);
 720         sti();
 721         INIT_REQUEST;
 722         if (reset) {
 723                 cli();
 724                 reset_hd();
 725                 return;
 726         }
 727         dev = MINOR(CURRENT->dev);
 728         block = CURRENT->sector;
 729         nsect = CURRENT->nr_sectors;
 730         if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {
 731 #ifdef DEBUG
 732                 if (dev >= (NR_HD<<6))
 733                         printk("hd: bad minor number: device=0x%04x\n", CURRENT->dev);
 734                 else
 735                         printk("hd%c: bad access: block=%d, count=%d\n",
 736                                 (CURRENT->dev>>6)+'a', block, nsect);
 737 #endif
 738                 end_request(0);
 739                 goto repeat;
 740         }
 741         block += hd[dev].start_sect;
 742         dev >>= 6;
 743         if (special_op[dev]) {
 744                 if (do_special_op(dev))
 745                         goto repeat;
 746                 return;
 747         }
 748         sec   = block % hd_info[dev].sect + 1;
 749         track = block / hd_info[dev].sect;
 750         head  = track % hd_info[dev].head;
 751         cyl   = track / hd_info[dev].head;
 752 #ifdef DEBUG
 753         printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
 754                 dev+'a', (CURRENT->cmd == READ)?"read":"writ",
 755                 cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);
 756 #endif
 757         if (!unmask_intr[dev])
 758                 cli();
 759         if (CURRENT->cmd == READ) {
 760                 unsigned int cmd = mult_count[dev] > 1 ? WIN_MULTREAD : WIN_READ;
 761                 hd_out(dev,nsect,sec,head,cyl,cmd,&read_intr);
 762                 if (reset)
 763                         goto repeat;
 764                 return;
 765         }
 766         if (CURRENT->cmd == WRITE) {
 767                 if (mult_count[dev])
 768                         hd_out(dev,nsect,sec,head,cyl,WIN_MULTWRITE,&multwrite_intr);
 769                 else
 770                         hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
 771                 if (reset)
 772                         goto repeat;
 773                 if (wait_DRQ()) {
 774                         bad_rw_intr();
 775                         goto repeat;
 776                 }
 777                 if (mult_count[dev]) {
 778                         WCURRENT = *CURRENT;
 779                         multwrite(dev);
 780                 } else
 781                         outsw(HD_DATA,CURRENT->buffer,256);
 782                 return;
 783         }
 784         panic("unknown hd-command");
 785 }
 786 
 787 static void do_hd_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 788 {
 789         disable_irq(HD_IRQ);
 790         hd_request();
 791         enable_irq(HD_IRQ);
 792 }
 793 
 794 static int hd_ioctl(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 795         unsigned int cmd, unsigned long arg)
 796 {
 797         struct hd_geometry *loc = (struct hd_geometry *) arg;
 798         int dev, err;
 799 
 800         if ((!inode) || (!inode->i_rdev))
 801                 return -EINVAL;
 802         dev = DEVICE_NR(inode->i_rdev);
 803         if (dev >= NR_HD)
 804                 return -EINVAL;
 805         switch (cmd) {
 806                 case HDIO_GETGEO:
 807                         if (!loc)  return -EINVAL;
 808                         err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
 809                         if (err)
 810                                 return err;
 811                         put_fs_byte(bios_info[dev].head,
 812                                 (char *) &loc->heads);
 813                         put_fs_byte(bios_info[dev].sect,
 814                                 (char *) &loc->sectors);
 815                         put_fs_word(bios_info[dev].cyl,
 816                                 (short *) &loc->cylinders);
 817                         put_fs_long(hd[MINOR(inode->i_rdev)].start_sect,
 818                                 (long *) &loc->start);
 819                         return 0;
 820                 case BLKRASET:
 821                         if(!suser())  return -EACCES;
 822                         if(arg > 0xff) return -EINVAL;
 823                         read_ahead[MAJOR(inode->i_rdev)] = arg;
 824                         return 0;
 825                 case BLKRAGET:
 826                         if (!arg)  return -EINVAL;
 827                         err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
 828                         if (err)
 829                                 return err;
 830                         put_fs_long(read_ahead[MAJOR(inode->i_rdev)],(long *) arg);
 831                         return 0;
 832                 case BLKGETSIZE:   /* Return device size */
 833                         if (!arg)  return -EINVAL;
 834                         err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
 835                         if (err)
 836                                 return err;
 837                         put_fs_long(hd[MINOR(inode->i_rdev)].nr_sects, (long *) arg);
 838                         return 0;
 839                 case BLKFLSBUF:
 840                         if(!suser())  return -EACCES;
 841                         fsync_dev(inode->i_rdev);
 842                         invalidate_buffers(inode->i_rdev);
 843                         return 0;
 844 
 845                 case BLKRRPART: /* Re-read partition tables */
 846                         return revalidate_hddisk(inode->i_rdev, 1);
 847 
 848                 case HDIO_SETUNMASKINTR:
 849                         if (!suser()) return -EACCES;
 850                         if (!arg)  return -EINVAL;
 851                         if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
 852                         err = verify_area(VERIFY_READ, (long *) arg, sizeof(long));
 853                         if (err)
 854                                 return err;
 855                         unmask_intr[dev] = get_fs_long((long *) arg);
 856                         return 0;
 857 
 858                 case HDIO_GETUNMASKINTR:
 859                         if (!arg)  return -EINVAL;
 860                         err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
 861                         if (err)
 862                                 return err;
 863                         put_fs_long(unmask_intr[dev], (long *) arg);
 864                         return 0;
 865 
 866                 case HDIO_GETMULTCOUNT:
 867                         if (!arg)  return -EINVAL;
 868                         err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
 869                         if (err)
 870                                 return err;
 871                         put_fs_long(mult_count[dev], (long *) arg);
 872                         return 0;
 873 
 874                 case HDIO_SETMULTCOUNT:
 875                 {
 876                         unsigned long flags;
 877                         if (!suser()) return -EACCES;
 878                         if (!arg)  return -EINVAL;
 879                         if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
 880                         err = verify_area(VERIFY_READ, (long *) arg, sizeof(long));
 881                         if (err)
 882                                 return err;
 883                         arg = get_fs_long((long *) arg);
 884                         save_flags(flags);
 885                         cli();  /* a prior request might still be in progress */
 886                         if (arg > max_mult[dev])
 887                                 err = -EINVAL;  /* out of range for device */
 888                         else if (mult_req[dev] != mult_count[dev]) {
 889                                 special_op[dev] = 1;
 890                                 err = -EBUSY;   /* busy, try again */
 891                         } else {
 892                                 mult_req[dev] = arg;
 893                                 special_op[dev] = 1;
 894                                 err = 0;
 895                         }
 896                         restore_flags(flags);
 897                         return err;
 898                 }
 899                 case HDIO_GETIDENTITY:
 900                         if (!arg)  return -EINVAL;
 901                         if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
 902                         if (hd_ident_info[dev] == NULL)  return -ENOMSG;
 903                         err = verify_area(VERIFY_WRITE, (char *) arg, sizeof(struct hd_driveid));
 904                         if (err)
 905                                 return err;
 906                         memcpy_tofs((char *)arg, (char *) hd_ident_info[dev], sizeof(struct hd_driveid));
 907                         return 0;
 908 
 909                 RO_IOCTLS(inode->i_rdev,arg);
 910                 default:
 911                         return -EINVAL;
 912         }
 913 }
 914 
 915 static int hd_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 916 {
 917         int target;
 918         target =  DEVICE_NR(inode->i_rdev);
 919 
 920         while (busy[target])
 921                 sleep_on(&busy_wait);
 922         access_count[target]++;
 923         return 0;
 924 }
 925 
 926 /*
 927  * Releasing a block device means we sync() it, so that it can safely
 928  * be forgotten about...
 929  */
 930 static void hd_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
 931 {
 932         int target;
 933         sync_dev(inode->i_rdev);
 934 
 935         target =  DEVICE_NR(inode->i_rdev);
 936         access_count[target]--;
 937 
 938 }
 939 
 940 static void hd_geninit(void);
 941 
 942 static struct gendisk hd_gendisk = {
 943         MAJOR_NR,       /* Major number */      
 944         "hd",           /* Major name */
 945         6,              /* Bits to shift to get real from partition */
 946         1 << 6,         /* Number of partitions per real */
 947         MAX_HD,         /* maximum number of real */
 948         hd_geninit,     /* init function */
 949         hd,             /* hd struct */
 950         hd_sizes,       /* block sizes */
 951         0,              /* number */
 952         (void *) bios_info,     /* internal */
 953         NULL            /* next */
 954 };
 955         
 956 static void hd_interrupt(int unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 957 {
 958         void (*handler)(void) = DEVICE_INTR;
 959 
 960         DEVICE_INTR = NULL;
 961         timer_active &= ~(1<<HD_TIMER);
 962         if (!handler)
 963                 handler = unexpected_hd_interrupt;
 964         handler();
 965         sti();
 966 }
 967 
 968 /*
 969  * This is the harddisk IRQ description. The SA_INTERRUPT in sa_flags
 970  * means we run the IRQ-handler with interrupts disabled: this is bad for
 971  * interrupt latency, but anything else has led to problems on some
 972  * machines...
 973  *
 974  * We enable interrupts in some of the routines after making sure it's
 975  * safe.
 976  */
 977 static void hd_geninit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 978 {
 979         int drive, i;
 980         extern struct drive_info drive_info;
 981         unsigned char *BIOS = (unsigned char *) &drive_info;
 982         int cmos_disks;
 983 
 984         if (!NR_HD) {      
 985                 for (drive=0 ; drive<2 ; drive++) {
 986                         bios_info[drive].cyl   = hd_info[drive].cyl = *(unsigned short *) BIOS;
 987                         bios_info[drive].head  = hd_info[drive].head = *(2+BIOS);
 988                         bios_info[drive].wpcom = hd_info[drive].wpcom = *(unsigned short *) (5+BIOS);
 989                         bios_info[drive].ctl   = hd_info[drive].ctl = *(8+BIOS);
 990                         bios_info[drive].lzone = hd_info[drive].lzone = *(unsigned short *) (12+BIOS);
 991                         bios_info[drive].sect  = hd_info[drive].sect = *(14+BIOS);
 992 #ifdef does_not_work_for_everybody_with_scsi_but_helps_ibm_vp
 993                         if (hd_info[drive].cyl && NR_HD == drive)
 994                                 NR_HD++;
 995 #endif
 996                         BIOS += 16;
 997                 }
 998 
 999         /*
1000                 We query CMOS about hard disks : it could be that 
1001                 we have a SCSI/ESDI/etc controller that is BIOS
1002                 compatible with ST-506, and thus showing up in our
1003                 BIOS table, but not register compatible, and therefore
1004                 not present in CMOS.
1005 
1006                 Furthermore, we will assume that our ST-506 drives
1007                 <if any> are the primary drives in the system, and 
1008                 the ones reflected as drive 1 or 2.
1009 
1010                 The first drive is stored in the high nibble of CMOS
1011                 byte 0x12, the second in the low nibble.  This will be
1012                 either a 4 bit drive type or 0xf indicating use byte 0x19 
1013                 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.
1014 
1015                 Needless to say, a non-zero value means we have 
1016                 an AT controller hard disk for that drive.
1017 
1018                 
1019         */
1020 
1021                 if ((cmos_disks = CMOS_READ(0x12)) & 0xf0)
1022                         if (cmos_disks & 0x0f)
1023                                 NR_HD = 2;
1024                         else
1025                                 NR_HD = 1;
1026         }
1027         i = NR_HD;
1028         while (i-- > 0) {
1029                 /*
1030                  * The newer E-IDE BIOSs handle drives larger than 1024
1031                  * cylinders by increasing the number of logical heads
1032                  * to keep the number of logical cylinders below the
1033                  * sacred INT13 limit of 1024 (10 bits).  If that is
1034                  * what's happening here, we'll find out and correct
1035                  * it later when "identifying" the drive.
1036                  */
1037                 hd[i<<6].nr_sects = bios_info[i].head *
1038                                 bios_info[i].sect * bios_info[i].cyl;
1039                 hd_ident_info[i] = (struct hd_driveid *) kmalloc(512,GFP_KERNEL);
1040                 special_op[i] = 1;
1041         }
1042         if (NR_HD) {
1043                 if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd")) {
1044                         printk("hd: unable to get IRQ%d for the harddisk driver\n",HD_IRQ);
1045                         NR_HD = 0;
1046                 }
1047         }
1048         hd_gendisk.nr_real = NR_HD;
1049 
1050         for(i=0;i<(MAX_HD << 6);i++) hd_blocksizes[i] = 1024;
1051         blksize_size[MAJOR_NR] = hd_blocksizes;
1052 }
1053 
1054 static struct file_operations hd_fops = {
1055         NULL,                   /* lseek - default */
1056         block_read,             /* read - general block-dev read */
1057         block_write,            /* write - general block-dev write */
1058         NULL,                   /* readdir - bad */
1059         NULL,                   /* select */
1060         hd_ioctl,               /* ioctl */
1061         NULL,                   /* mmap */
1062         hd_open,                /* open */
1063         hd_release,             /* release */
1064         block_fsync             /* fsync */
1065 };
1066 
1067 unsigned long hd_init(unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1068 {
1069         if (register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
1070                 printk("hd: unable to get major %d for harddisk\n",MAJOR_NR);
1071                 return mem_start;
1072         }
1073         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1074         read_ahead[MAJOR_NR] = 8;               /* 8 sector (4kB) read-ahead */
1075         hd_gendisk.next = gendisk_head;
1076         gendisk_head = &hd_gendisk;
1077         timer_table[HD_TIMER].fn = hd_times_out;
1078         return mem_start;
1079 }
1080 
1081 #define DEVICE_BUSY busy[target]
1082 #define USAGE access_count[target]
1083 #define CAPACITY (bios_info[target].head*bios_info[target].sect*bios_info[target].cyl)
1084 /* We assume that the the bios parameters do not change, so the disk capacity
1085    will not change */
1086 #undef MAYBE_REINIT
1087 #define GENDISK_STRUCT hd_gendisk
1088 
1089 /*
1090  * This routine is called to flush all partitions and partition tables
1091  * for a changed scsi disk, and then re-read the new partition table.
1092  * If we are revalidating a disk because of a media change, then we
1093  * enter with usage == 0.  If we are using an ioctl, we automatically have
1094  * usage == 1 (we need an open channel to use an ioctl :-), so this
1095  * is our limit.
1096  */
1097 static int revalidate_hddisk(int dev, int maxusage)
     /* [previous][next][first][last][top][bottom][index][help] */
1098 {
1099         int target, major;
1100         struct gendisk * gdev;
1101         int max_p;
1102         int start;
1103         int i;
1104         long flags;
1105 
1106         target =  DEVICE_NR(dev);
1107         gdev = &GENDISK_STRUCT;
1108 
1109         save_flags(flags);
1110         cli();
1111         if (DEVICE_BUSY || USAGE > maxusage) {
1112                 restore_flags(flags);
1113                 return -EBUSY;
1114         };
1115         DEVICE_BUSY = 1;
1116         restore_flags(flags);
1117 
1118         max_p = gdev->max_p;
1119         start = target << gdev->minor_shift;
1120         major = MAJOR_NR << 8;
1121 
1122         for (i=max_p - 1; i >=0 ; i--) {
1123                 sync_dev(major | start | i);
1124                 invalidate_inodes(major | start | i);
1125                 invalidate_buffers(major | start | i);
1126                 gdev->part[start+i].start_sect = 0;
1127                 gdev->part[start+i].nr_sects = 0;
1128         };
1129 
1130 #ifdef MAYBE_REINIT
1131         MAYBE_REINIT;
1132 #endif
1133 
1134         gdev->part[start].nr_sects = CAPACITY;
1135         resetup_one_dev(gdev, target);
1136 
1137         DEVICE_BUSY = 0;
1138         wake_up(&busy_wait);
1139         return 0;
1140 }
1141 

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