root/drivers/block/hd.c

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

DEFINITIONS

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

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

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