Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 3 | * |
| 4 | * This is the low-level hd interrupt support. It traverses the |
| 5 | * request-list, using interrupts to jump between functions. As |
| 6 | * all the functions are called within interrupts, we may not |
| 7 | * sleep. Special care is recommended. |
| 8 | * |
| 9 | * modified by Drew Eckhardt to check nr of hd's from the CMOS. |
| 10 | * |
| 11 | * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug |
| 12 | * in the early extended-partition checks and added DM partitions |
| 13 | * |
| 14 | * IRQ-unmask, drive-id, multiple-mode, support for ">16 heads", |
| 15 | * and general streamlining by Mark Lord. |
| 16 | * |
| 17 | * Removed 99% of above. Use Mark's ide driver for those options. |
| 18 | * This is now a lightweight ST-506 driver. (Paul Gortmaker) |
| 19 | * |
| 20 | * Modified 1995 Russell King for ARM processor. |
| 21 | * |
| 22 | * Bugfix: max_sectors must be <= 255 or the wheels tend to come |
| 23 | * off in a hurry once you queue things up - Paul G. 02/2001 |
| 24 | */ |
| 25 | |
| 26 | /* Uncomment the following if you want verbose error reports. */ |
| 27 | /* #define VERBOSE_ERRORS */ |
| 28 | |
| 29 | #include <linux/blkdev.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/signal.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/timer.h> |
| 34 | #include <linux/fs.h> |
| 35 | #include <linux/kernel.h> |
| 36 | #include <linux/genhd.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/string.h> |
| 39 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/init.h> |
| 41 | #include <linux/blkpg.h> |
Bartlomiej Zolnierkiewicz | f26b3d7 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 42 | #include <linux/ata.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | |
Bartlomiej Zolnierkiewicz | 4fe6e30 | 2009-04-01 21:42:25 +0200 | [diff] [blame] | 45 | #define HD_IRQ 14 |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define REALLY_SLOW_IO |
| 48 | #include <asm/system.h> |
| 49 | #include <asm/io.h> |
| 50 | #include <asm/uaccess.h> |
| 51 | |
| 52 | #ifdef __arm__ |
| 53 | #undef HD_IRQ |
| 54 | #endif |
| 55 | #include <asm/irq.h> |
| 56 | #ifdef __arm__ |
| 57 | #define HD_IRQ IRQ_HARDDISK |
| 58 | #endif |
| 59 | |
| 60 | /* Hd controller regster ports */ |
| 61 | |
| 62 | #define HD_DATA 0x1f0 /* _CTL when writing */ |
| 63 | #define HD_ERROR 0x1f1 /* see err-bits */ |
| 64 | #define HD_NSECTOR 0x1f2 /* nr of sectors to read/write */ |
| 65 | #define HD_SECTOR 0x1f3 /* starting sector */ |
| 66 | #define HD_LCYL 0x1f4 /* starting cylinder */ |
| 67 | #define HD_HCYL 0x1f5 /* high byte of starting cyl */ |
| 68 | #define HD_CURRENT 0x1f6 /* 101dhhhh , d=drive, hhhh=head */ |
| 69 | #define HD_STATUS 0x1f7 /* see status-bits */ |
| 70 | #define HD_FEATURE HD_ERROR /* same io address, read=error, write=feature */ |
| 71 | #define HD_PRECOMP HD_FEATURE /* obsolete use of this port - predates IDE */ |
| 72 | #define HD_COMMAND HD_STATUS /* same io address, read=status, write=cmd */ |
| 73 | |
| 74 | #define HD_CMD 0x3f6 /* used for resets */ |
| 75 | #define HD_ALTSTATUS 0x3f6 /* same as HD_STATUS but doesn't clear irq */ |
| 76 | |
| 77 | /* Bits of HD_STATUS */ |
| 78 | #define ERR_STAT 0x01 |
| 79 | #define INDEX_STAT 0x02 |
| 80 | #define ECC_STAT 0x04 /* Corrected error */ |
| 81 | #define DRQ_STAT 0x08 |
| 82 | #define SEEK_STAT 0x10 |
| 83 | #define SERVICE_STAT SEEK_STAT |
| 84 | #define WRERR_STAT 0x20 |
| 85 | #define READY_STAT 0x40 |
| 86 | #define BUSY_STAT 0x80 |
| 87 | |
| 88 | /* Bits for HD_ERROR */ |
| 89 | #define MARK_ERR 0x01 /* Bad address mark */ |
| 90 | #define TRK0_ERR 0x02 /* couldn't find track 0 */ |
| 91 | #define ABRT_ERR 0x04 /* Command aborted */ |
| 92 | #define MCR_ERR 0x08 /* media change request */ |
| 93 | #define ID_ERR 0x10 /* ID field not found */ |
| 94 | #define MC_ERR 0x20 /* media changed */ |
| 95 | #define ECC_ERR 0x40 /* Uncorrectable ECC error */ |
| 96 | #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */ |
| 97 | #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */ |
| 98 | |
| 99 | static DEFINE_SPINLOCK(hd_lock); |
| 100 | static struct request_queue *hd_queue; |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 101 | static struct request *hd_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | #define TIMEOUT_VALUE (6*HZ) |
| 104 | #define HD_DELAY 0 |
| 105 | |
| 106 | #define MAX_ERRORS 16 /* Max read/write errors/sector */ |
| 107 | #define RESET_FREQ 8 /* Reset controller every 8th retry */ |
| 108 | #define RECAL_FREQ 4 /* Recalibrate every 4th retry */ |
| 109 | #define MAX_HD 2 |
| 110 | |
| 111 | #define STAT_OK (READY_STAT|SEEK_STAT) |
| 112 | #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK) |
| 113 | |
| 114 | static void recal_intr(void); |
| 115 | static void bad_rw_intr(void); |
| 116 | |
| 117 | static int reset; |
| 118 | static int hd_error; |
| 119 | |
| 120 | /* |
| 121 | * This struct defines the HD's and their types. |
| 122 | */ |
| 123 | struct hd_i_struct { |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 124 | unsigned int head, sect, cyl, wpcom, lzone, ctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | int unit; |
| 126 | int recalibrate; |
| 127 | int special_op; |
| 128 | }; |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | #ifdef HD_TYPE |
| 131 | static struct hd_i_struct hd_info[] = { HD_TYPE }; |
Andi Drebes | ecea573 | 2007-07-09 23:17:57 +0200 | [diff] [blame] | 132 | static int NR_HD = ARRAY_SIZE(hd_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | #else |
| 134 | static struct hd_i_struct hd_info[MAX_HD]; |
| 135 | static int NR_HD; |
| 136 | #endif |
| 137 | |
| 138 | static struct gendisk *hd_gendisk[MAX_HD]; |
| 139 | |
| 140 | static struct timer_list device_timer; |
| 141 | |
| 142 | #define TIMEOUT_VALUE (6*HZ) |
| 143 | |
| 144 | #define SET_TIMER \ |
| 145 | do { \ |
| 146 | mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \ |
| 147 | } while (0) |
| 148 | |
| 149 | static void (*do_hd)(void) = NULL; |
| 150 | #define SET_HANDLER(x) \ |
| 151 | if ((do_hd = (x)) != NULL) \ |
| 152 | SET_TIMER; \ |
| 153 | else \ |
| 154 | del_timer(&device_timer); |
| 155 | |
| 156 | |
| 157 | #if (HD_DELAY > 0) |
Ingo Molnar | 306e440 | 2005-06-30 02:58:55 -0700 | [diff] [blame] | 158 | |
| 159 | #include <asm/i8253.h> |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | unsigned long last_req; |
| 162 | |
| 163 | unsigned long read_timer(void) |
| 164 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | unsigned long t, flags; |
| 166 | int i; |
| 167 | |
| 168 | spin_lock_irqsave(&i8253_lock, flags); |
| 169 | t = jiffies * 11932; |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 170 | outb_p(0, 0x43); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | i = inb_p(0x40); |
| 172 | i |= inb(0x40) << 8; |
| 173 | spin_unlock_irqrestore(&i8253_lock, flags); |
| 174 | return(t - i); |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | static void __init hd_setup(char *str, int *ints) |
| 179 | { |
| 180 | int hdind = 0; |
| 181 | |
| 182 | if (ints[0] != 3) |
| 183 | return; |
| 184 | if (hd_info[0].head != 0) |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 185 | hdind = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | hd_info[hdind].head = ints[2]; |
| 187 | hd_info[hdind].sect = ints[3]; |
| 188 | hd_info[hdind].cyl = ints[1]; |
| 189 | hd_info[hdind].wpcom = 0; |
| 190 | hd_info[hdind].lzone = ints[1]; |
| 191 | hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0); |
| 192 | NR_HD = hdind+1; |
| 193 | } |
| 194 | |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 195 | static bool hd_end_request(int err, unsigned int bytes) |
| 196 | { |
| 197 | if (__blk_end_request(hd_req, err, bytes)) |
| 198 | return true; |
| 199 | hd_req = NULL; |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | static bool hd_end_request_cur(int err) |
| 204 | { |
| 205 | return hd_end_request(err, blk_rq_cur_bytes(hd_req)); |
| 206 | } |
| 207 | |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 208 | static void dump_status(const char *msg, unsigned int stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | char *name = "hd?"; |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 211 | if (hd_req) |
| 212 | name = hd_req->rq_disk->disk_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | #ifdef VERBOSE_ERRORS |
| 215 | printk("%s: %s: status=0x%02x { ", name, msg, stat & 0xff); |
| 216 | if (stat & BUSY_STAT) printk("Busy "); |
| 217 | if (stat & READY_STAT) printk("DriveReady "); |
| 218 | if (stat & WRERR_STAT) printk("WriteFault "); |
| 219 | if (stat & SEEK_STAT) printk("SeekComplete "); |
| 220 | if (stat & DRQ_STAT) printk("DataRequest "); |
| 221 | if (stat & ECC_STAT) printk("CorrectedError "); |
| 222 | if (stat & INDEX_STAT) printk("Index "); |
| 223 | if (stat & ERR_STAT) printk("Error "); |
| 224 | printk("}\n"); |
| 225 | if ((stat & ERR_STAT) == 0) { |
| 226 | hd_error = 0; |
| 227 | } else { |
| 228 | hd_error = inb(HD_ERROR); |
| 229 | printk("%s: %s: error=0x%02x { ", name, msg, hd_error & 0xff); |
| 230 | if (hd_error & BBD_ERR) printk("BadSector "); |
| 231 | if (hd_error & ECC_ERR) printk("UncorrectableError "); |
| 232 | if (hd_error & ID_ERR) printk("SectorIdNotFound "); |
| 233 | if (hd_error & ABRT_ERR) printk("DriveStatusError "); |
| 234 | if (hd_error & TRK0_ERR) printk("TrackZeroNotFound "); |
| 235 | if (hd_error & MARK_ERR) printk("AddrMarkNotFound "); |
| 236 | printk("}"); |
| 237 | if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) { |
| 238 | printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL), |
| 239 | inb(HD_CURRENT) & 0xf, inb(HD_SECTOR)); |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 240 | if (hd_req) |
| 241 | printk(", sector=%ld", blk_rq_pos(hd_req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | printk("\n"); |
| 244 | } |
| 245 | #else |
| 246 | printk("%s: %s: status=0x%02x.\n", name, msg, stat & 0xff); |
| 247 | if ((stat & ERR_STAT) == 0) { |
| 248 | hd_error = 0; |
| 249 | } else { |
| 250 | hd_error = inb(HD_ERROR); |
| 251 | printk("%s: %s: error=0x%02x.\n", name, msg, hd_error & 0xff); |
| 252 | } |
| 253 | #endif |
| 254 | } |
| 255 | |
| 256 | static void check_status(void) |
| 257 | { |
| 258 | int i = inb_p(HD_STATUS); |
| 259 | |
| 260 | if (!OK_STATUS(i)) { |
| 261 | dump_status("check_status", i); |
| 262 | bad_rw_intr(); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static int controller_busy(void) |
| 267 | { |
| 268 | int retries = 100000; |
| 269 | unsigned char status; |
| 270 | |
| 271 | do { |
| 272 | status = inb_p(HD_STATUS); |
| 273 | } while ((status & BUSY_STAT) && --retries); |
| 274 | return status; |
| 275 | } |
| 276 | |
| 277 | static int status_ok(void) |
| 278 | { |
| 279 | unsigned char status = inb_p(HD_STATUS); |
| 280 | |
| 281 | if (status & BUSY_STAT) |
| 282 | return 1; /* Ancient, but does it make sense??? */ |
| 283 | if (status & WRERR_STAT) |
| 284 | return 0; |
| 285 | if (!(status & READY_STAT)) |
| 286 | return 0; |
| 287 | if (!(status & SEEK_STAT)) |
| 288 | return 0; |
| 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | static int controller_ready(unsigned int drive, unsigned int head) |
| 293 | { |
| 294 | int retry = 100; |
| 295 | |
| 296 | do { |
| 297 | if (controller_busy() & BUSY_STAT) |
| 298 | return 0; |
| 299 | outb_p(0xA0 | (drive<<4) | head, HD_CURRENT); |
| 300 | if (status_ok()) |
| 301 | return 1; |
| 302 | } while (--retry); |
| 303 | return 0; |
| 304 | } |
| 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | static void hd_out(struct hd_i_struct *disk, |
| 307 | unsigned int nsect, |
| 308 | unsigned int sect, |
| 309 | unsigned int head, |
| 310 | unsigned int cyl, |
| 311 | unsigned int cmd, |
| 312 | void (*intr_addr)(void)) |
| 313 | { |
| 314 | unsigned short port; |
| 315 | |
| 316 | #if (HD_DELAY > 0) |
| 317 | while (read_timer() - last_req < HD_DELAY) |
| 318 | /* nothing */; |
| 319 | #endif |
| 320 | if (reset) |
| 321 | return; |
| 322 | if (!controller_ready(disk->unit, head)) { |
| 323 | reset = 1; |
| 324 | return; |
| 325 | } |
| 326 | SET_HANDLER(intr_addr); |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 327 | outb_p(disk->ctl, HD_CMD); |
| 328 | port = HD_DATA; |
| 329 | outb_p(disk->wpcom >> 2, ++port); |
| 330 | outb_p(nsect, ++port); |
| 331 | outb_p(sect, ++port); |
| 332 | outb_p(cyl, ++port); |
| 333 | outb_p(cyl >> 8, ++port); |
| 334 | outb_p(0xA0 | (disk->unit << 4) | head, ++port); |
| 335 | outb_p(cmd, ++port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static void hd_request (void); |
| 339 | |
| 340 | static int drive_busy(void) |
| 341 | { |
| 342 | unsigned int i; |
| 343 | unsigned char c; |
| 344 | |
| 345 | for (i = 0; i < 500000 ; i++) { |
| 346 | c = inb_p(HD_STATUS); |
| 347 | if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK) |
| 348 | return 0; |
| 349 | } |
| 350 | dump_status("reset timed out", c); |
| 351 | return 1; |
| 352 | } |
| 353 | |
| 354 | static void reset_controller(void) |
| 355 | { |
| 356 | int i; |
| 357 | |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 358 | outb_p(4, HD_CMD); |
| 359 | for (i = 0; i < 1000; i++) barrier(); |
| 360 | outb_p(hd_info[0].ctl & 0x0f, HD_CMD); |
| 361 | for (i = 0; i < 1000; i++) barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | if (drive_busy()) |
| 363 | printk("hd: controller still busy\n"); |
| 364 | else if ((hd_error = inb(HD_ERROR)) != 1) |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 365 | printk("hd: controller reset failed: %02x\n", hd_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static void reset_hd(void) |
| 369 | { |
| 370 | static int i; |
| 371 | |
| 372 | repeat: |
| 373 | if (reset) { |
| 374 | reset = 0; |
| 375 | i = -1; |
| 376 | reset_controller(); |
| 377 | } else { |
| 378 | check_status(); |
| 379 | if (reset) |
| 380 | goto repeat; |
| 381 | } |
| 382 | if (++i < NR_HD) { |
| 383 | struct hd_i_struct *disk = &hd_info[i]; |
| 384 | disk->special_op = disk->recalibrate = 1; |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 385 | hd_out(disk, disk->sect, disk->sect, disk->head-1, |
Bartlomiej Zolnierkiewicz | f26b3d7 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 386 | disk->cyl, ATA_CMD_INIT_DEV_PARAMS, &reset_hd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (reset) |
| 388 | goto repeat; |
| 389 | } else |
| 390 | hd_request(); |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Ok, don't know what to do with the unexpected interrupts: on some machines |
| 395 | * doing a reset and a retry seems to result in an eternal loop. Right now I |
| 396 | * ignore it, and just set the timeout. |
| 397 | * |
| 398 | * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the |
| 399 | * drive enters "idle", "standby", or "sleep" mode, so if the status looks |
| 400 | * "good", we just ignore the interrupt completely. |
| 401 | */ |
| 402 | static void unexpected_hd_interrupt(void) |
| 403 | { |
| 404 | unsigned int stat = inb_p(HD_STATUS); |
| 405 | |
| 406 | if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) { |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 407 | dump_status("unexpected interrupt", stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | SET_TIMER; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * bad_rw_intr() now tries to be a bit smarter and does things |
| 414 | * according to the error returned by the controller. |
| 415 | * -Mika Liljeberg (liljeber@cs.Helsinki.FI) |
| 416 | */ |
| 417 | static void bad_rw_intr(void) |
| 418 | { |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 419 | struct request *req = hd_req; |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | if (req != NULL) { |
| 422 | struct hd_i_struct *disk = req->rq_disk->private_data; |
| 423 | if (++req->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) { |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 424 | hd_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | disk->special_op = disk->recalibrate = 1; |
| 426 | } else if (req->errors % RESET_FREQ == 0) |
| 427 | reset = 1; |
| 428 | else if ((hd_error & TRK0_ERR) || req->errors % RECAL_FREQ == 0) |
| 429 | disk->special_op = disk->recalibrate = 1; |
| 430 | /* Otherwise just retry */ |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | static inline int wait_DRQ(void) |
| 435 | { |
Andrew Morton | b004223 | 2008-02-06 02:57:49 +0100 | [diff] [blame] | 436 | int retries; |
| 437 | int stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Andrew Morton | b004223 | 2008-02-06 02:57:49 +0100 | [diff] [blame] | 439 | for (retries = 0; retries < 100000; retries++) { |
| 440 | stat = inb_p(HD_STATUS); |
| 441 | if (stat & DRQ_STAT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return 0; |
Andrew Morton | b004223 | 2008-02-06 02:57:49 +0100 | [diff] [blame] | 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | dump_status("wait_DRQ", stat); |
| 445 | return -1; |
| 446 | } |
| 447 | |
| 448 | static void read_intr(void) |
| 449 | { |
| 450 | struct request *req; |
| 451 | int i, retries = 100000; |
| 452 | |
| 453 | do { |
| 454 | i = (unsigned) inb_p(HD_STATUS); |
| 455 | if (i & BUSY_STAT) |
| 456 | continue; |
| 457 | if (!OK_STATUS(i)) |
| 458 | break; |
| 459 | if (i & DRQ_STAT) |
| 460 | goto ok_to_read; |
| 461 | } while (--retries > 0); |
| 462 | dump_status("read_intr", i); |
| 463 | bad_rw_intr(); |
| 464 | hd_request(); |
| 465 | return; |
Tejun Heo | e091eb6 | 2009-04-28 13:06:11 +0900 | [diff] [blame] | 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | ok_to_read: |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 468 | req = hd_req; |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 469 | insw(HD_DATA, req->buffer, 256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | #ifdef DEBUG |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 471 | printk("%s: read: sector %ld, remaining = %u, buffer=%p\n", |
| 472 | req->rq_disk->disk_name, blk_rq_pos(req) + 1, |
| 473 | blk_rq_sectors(req) - 1, req->buffer+512); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | #endif |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 475 | if (hd_end_request(0, 512)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | SET_HANDLER(&read_intr); |
| 477 | return; |
| 478 | } |
Tejun Heo | e091eb6 | 2009-04-28 13:06:11 +0900 | [diff] [blame] | 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | (void) inb_p(HD_STATUS); |
| 481 | #if (HD_DELAY > 0) |
| 482 | last_req = read_timer(); |
| 483 | #endif |
Tejun Heo | e091eb6 | 2009-04-28 13:06:11 +0900 | [diff] [blame] | 484 | hd_request(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static void write_intr(void) |
| 488 | { |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 489 | struct request *req = hd_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | int i; |
| 491 | int retries = 100000; |
| 492 | |
| 493 | do { |
| 494 | i = (unsigned) inb_p(HD_STATUS); |
| 495 | if (i & BUSY_STAT) |
| 496 | continue; |
| 497 | if (!OK_STATUS(i)) |
| 498 | break; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 499 | if ((blk_rq_sectors(req) <= 1) || (i & DRQ_STAT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | goto ok_to_write; |
| 501 | } while (--retries > 0); |
| 502 | dump_status("write_intr", i); |
| 503 | bad_rw_intr(); |
| 504 | hd_request(); |
| 505 | return; |
Tejun Heo | e091eb6 | 2009-04-28 13:06:11 +0900 | [diff] [blame] | 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | ok_to_write: |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 508 | if (hd_end_request(0, 512)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | SET_HANDLER(&write_intr); |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 510 | outsw(HD_DATA, req->buffer, 256); |
Tejun Heo | e091eb6 | 2009-04-28 13:06:11 +0900 | [diff] [blame] | 511 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
Tejun Heo | e091eb6 | 2009-04-28 13:06:11 +0900 | [diff] [blame] | 513 | |
| 514 | #if (HD_DELAY > 0) |
| 515 | last_req = read_timer(); |
| 516 | #endif |
| 517 | hd_request(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static void recal_intr(void) |
| 521 | { |
| 522 | check_status(); |
| 523 | #if (HD_DELAY > 0) |
| 524 | last_req = read_timer(); |
| 525 | #endif |
| 526 | hd_request(); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * This is another of the error-routines I don't know what to do with. The |
| 531 | * best idea seems to just set reset, and start all over again. |
| 532 | */ |
| 533 | static void hd_times_out(unsigned long dummy) |
| 534 | { |
| 535 | char *name; |
| 536 | |
| 537 | do_hd = NULL; |
| 538 | |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 539 | if (!hd_req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return; |
| 541 | |
Tejun Heo | e93b9fb | 2009-04-28 12:38:33 +0900 | [diff] [blame] | 542 | spin_lock_irq(hd_queue->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | reset = 1; |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 544 | name = hd_req->rq_disk->disk_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | printk("%s: timeout\n", name); |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 546 | if (++hd_req->errors >= MAX_ERRORS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | #ifdef DEBUG |
| 548 | printk("%s: too many errors\n", name); |
| 549 | #endif |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 550 | hd_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | hd_request(); |
Tejun Heo | e93b9fb | 2009-04-28 12:38:33 +0900 | [diff] [blame] | 553 | spin_unlock_irq(hd_queue->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static int do_special_op(struct hd_i_struct *disk, struct request *req) |
| 557 | { |
| 558 | if (disk->recalibrate) { |
| 559 | disk->recalibrate = 0; |
Bartlomiej Zolnierkiewicz | f26b3d7 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 560 | hd_out(disk, disk->sect, 0, 0, 0, ATA_CMD_RESTORE, &recal_intr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return reset; |
| 562 | } |
| 563 | if (disk->head > 16) { |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 564 | printk("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name); |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 565 | hd_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } |
| 567 | disk->special_op = 0; |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * The driver enables interrupts as much as possible. In order to do this, |
| 573 | * (a) the device-interrupt is disabled before entering hd_request(), |
| 574 | * and (b) the timeout-interrupt is disabled before the sti(). |
| 575 | * |
| 576 | * Interrupts are still masked (by default) whenever we are exchanging |
| 577 | * data/cmds with a drive, because some drives seem to have very poor |
| 578 | * tolerance for latency during I/O. The IDE driver has support to unmask |
| 579 | * interrupts for non-broken hardware, so use that driver if required. |
| 580 | */ |
| 581 | static void hd_request(void) |
| 582 | { |
| 583 | unsigned int block, nsect, sec, track, head, cyl; |
| 584 | struct hd_i_struct *disk; |
| 585 | struct request *req; |
| 586 | |
| 587 | if (do_hd) |
| 588 | return; |
| 589 | repeat: |
| 590 | del_timer(&device_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 592 | if (!hd_req) { |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 593 | hd_req = blk_fetch_request(hd_queue); |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 594 | if (!hd_req) { |
| 595 | do_hd = NULL; |
| 596 | return; |
| 597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 599 | req = hd_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 601 | if (reset) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | reset_hd(); |
| 603 | return; |
| 604 | } |
| 605 | disk = req->rq_disk->private_data; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 606 | block = blk_rq_pos(req); |
| 607 | nsect = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | if (block >= get_capacity(req->rq_disk) || |
| 609 | ((block+nsect) > get_capacity(req->rq_disk))) { |
| 610 | printk("%s: bad access: block=%d, count=%d\n", |
| 611 | req->rq_disk->disk_name, block, nsect); |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 612 | hd_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | goto repeat; |
| 614 | } |
| 615 | |
| 616 | if (disk->special_op) { |
| 617 | if (do_special_op(disk, req)) |
| 618 | goto repeat; |
| 619 | return; |
| 620 | } |
| 621 | sec = block % disk->sect + 1; |
| 622 | track = block / disk->sect; |
| 623 | head = track % disk->head; |
| 624 | cyl = track / disk->head; |
| 625 | #ifdef DEBUG |
| 626 | printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n", |
Boaz Harrosh | e654bc4 | 2007-06-20 13:53:23 +0200 | [diff] [blame] | 627 | req->rq_disk->disk_name, |
| 628 | req_data_dir(req) == READ ? "read" : "writ", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | cyl, head, sec, nsect, req->buffer); |
| 630 | #endif |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 631 | if (blk_fs_request(req)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | switch (rq_data_dir(req)) { |
| 633 | case READ: |
Bartlomiej Zolnierkiewicz | f26b3d7 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 634 | hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ, |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 635 | &read_intr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | if (reset) |
| 637 | goto repeat; |
| 638 | break; |
| 639 | case WRITE: |
Bartlomiej Zolnierkiewicz | f26b3d7 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 640 | hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_WRITE, |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 641 | &write_intr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | if (reset) |
| 643 | goto repeat; |
| 644 | if (wait_DRQ()) { |
| 645 | bad_rw_intr(); |
| 646 | goto repeat; |
| 647 | } |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 648 | outsw(HD_DATA, req->buffer, 256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | break; |
| 650 | default: |
| 651 | printk("unknown hd-command\n"); |
Tejun Heo | 8a12c4a | 2009-05-08 11:54:02 +0900 | [diff] [blame] | 652 | hd_end_request_cur(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 658 | static void do_hd_request(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | hd_request(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 663 | static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 665 | struct hd_i_struct *disk = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 667 | geo->heads = disk->head; |
| 668 | geo->sectors = disk->sect; |
| 669 | geo->cylinders = disk->cyl; |
| 670 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Releasing a block device means we sync() it, so that it can safely |
| 675 | * be forgotten about... |
| 676 | */ |
| 677 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 678 | static irqreturn_t hd_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
| 680 | void (*handler)(void) = do_hd; |
| 681 | |
Tejun Heo | e93b9fb | 2009-04-28 12:38:33 +0900 | [diff] [blame] | 682 | spin_lock(hd_queue->queue_lock); |
| 683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | do_hd = NULL; |
| 685 | del_timer(&device_timer); |
| 686 | if (!handler) |
| 687 | handler = unexpected_hd_interrupt; |
| 688 | handler(); |
Tejun Heo | e93b9fb | 2009-04-28 12:38:33 +0900 | [diff] [blame] | 689 | |
| 690 | spin_unlock(hd_queue->queue_lock); |
| 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | return IRQ_HANDLED; |
| 693 | } |
| 694 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 695 | static const struct block_device_operations hd_fops = { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 696 | .getgeo = hd_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | /* |
Thomas Gleixner | 362537b | 2006-07-01 19:29:34 -0700 | [diff] [blame] | 700 | * This is the hard disk IRQ description. The IRQF_DISABLED in sa_flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | * means we run the IRQ-handler with interrupts disabled: this is bad for |
| 702 | * interrupt latency, but anything else has led to problems on some |
| 703 | * machines. |
| 704 | * |
| 705 | * We enable interrupts in some of the routines after making sure it's |
| 706 | * safe. |
| 707 | */ |
| 708 | |
| 709 | static int __init hd_init(void) |
| 710 | { |
| 711 | int drive; |
| 712 | |
Christoph Hellwig | ddeb9c3 | 2009-06-16 22:07:32 +0200 | [diff] [blame] | 713 | if (register_blkdev(HD_MAJOR, "hd")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | return -1; |
| 715 | |
| 716 | hd_queue = blk_init_queue(do_hd_request, &hd_lock); |
| 717 | if (!hd_queue) { |
Christoph Hellwig | ddeb9c3 | 2009-06-16 22:07:32 +0200 | [diff] [blame] | 718 | unregister_blkdev(HD_MAJOR, "hd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return -ENOMEM; |
| 720 | } |
| 721 | |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 722 | blk_queue_max_hw_sectors(hd_queue, 255); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | init_timer(&device_timer); |
| 724 | device_timer.function = hd_times_out; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 725 | blk_queue_logical_block_size(hd_queue, 512); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (!NR_HD) { |
H. Peter Anvin | 48dd643 | 2007-07-11 12:18:27 -0700 | [diff] [blame] | 728 | /* |
| 729 | * We don't know anything about the drive. This means |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | * that you *MUST* specify the drive parameters to the |
| 731 | * kernel yourself. |
H. Peter Anvin | 48dd643 | 2007-07-11 12:18:27 -0700 | [diff] [blame] | 732 | * |
| 733 | * If we were on an i386, we used to read this info from |
| 734 | * the BIOS or CMOS. This doesn't work all that well, |
| 735 | * since this assumes that this is a primary or secondary |
| 736 | * drive, and if we're using this legacy driver, it's |
| 737 | * probably an auxilliary controller added to recover |
| 738 | * legacy data off an ST-506 drive. Either way, it's |
| 739 | * definitely safest to have the user explicitly specify |
| 740 | * the information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | */ |
| 742 | printk("hd: no drives specified - use hd=cyl,head,sectors" |
| 743 | " on kernel command line\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | goto out; |
H. Peter Anvin | 48dd643 | 2007-07-11 12:18:27 -0700 | [diff] [blame] | 745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 747 | for (drive = 0 ; drive < NR_HD ; drive++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | struct gendisk *disk = alloc_disk(64); |
| 749 | struct hd_i_struct *p = &hd_info[drive]; |
| 750 | if (!disk) |
| 751 | goto Enomem; |
Christoph Hellwig | ddeb9c3 | 2009-06-16 22:07:32 +0200 | [diff] [blame] | 752 | disk->major = HD_MAJOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | disk->first_minor = drive << 6; |
| 754 | disk->fops = &hd_fops; |
| 755 | sprintf(disk->disk_name, "hd%c", 'a'+drive); |
| 756 | disk->private_data = p; |
| 757 | set_capacity(disk, p->head * p->sect * p->cyl); |
| 758 | disk->queue = hd_queue; |
| 759 | p->unit = drive; |
| 760 | hd_gendisk[drive] = disk; |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 761 | printk("%s: %luMB, CHS=%d/%d/%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | disk->disk_name, (unsigned long)get_capacity(disk)/2048, |
| 763 | p->cyl, p->head, p->sect); |
| 764 | } |
| 765 | |
Thomas Gleixner | 362537b | 2006-07-01 19:29:34 -0700 | [diff] [blame] | 766 | if (request_irq(HD_IRQ, hd_interrupt, IRQF_DISABLED, "hd", NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | printk("hd: unable to get IRQ%d for the hard disk driver\n", |
| 768 | HD_IRQ); |
| 769 | goto out1; |
| 770 | } |
| 771 | if (!request_region(HD_DATA, 8, "hd")) { |
| 772 | printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA); |
| 773 | goto out2; |
| 774 | } |
| 775 | if (!request_region(HD_CMD, 1, "hd(cmd)")) { |
| 776 | printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD); |
| 777 | goto out3; |
| 778 | } |
| 779 | |
| 780 | /* Let them fly */ |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 781 | for (drive = 0; drive < NR_HD; drive++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | add_disk(hd_gendisk[drive]); |
| 783 | |
| 784 | return 0; |
| 785 | |
| 786 | out3: |
| 787 | release_region(HD_DATA, 8); |
| 788 | out2: |
| 789 | free_irq(HD_IRQ, NULL); |
| 790 | out1: |
| 791 | for (drive = 0; drive < NR_HD; drive++) |
| 792 | put_disk(hd_gendisk[drive]); |
| 793 | NR_HD = 0; |
| 794 | out: |
| 795 | del_timer(&device_timer); |
Christoph Hellwig | ddeb9c3 | 2009-06-16 22:07:32 +0200 | [diff] [blame] | 796 | unregister_blkdev(HD_MAJOR, "hd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | blk_cleanup_queue(hd_queue); |
| 798 | return -1; |
| 799 | Enomem: |
| 800 | while (drive--) |
| 801 | put_disk(hd_gendisk[drive]); |
| 802 | goto out; |
| 803 | } |
| 804 | |
Paolo Ciarrocchi | ec29782 | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 805 | static int __init parse_hd_setup(char *line) |
| 806 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | int ints[6]; |
| 808 | |
| 809 | (void) get_options(line, ARRAY_SIZE(ints), ints); |
| 810 | hd_setup(NULL, ints); |
| 811 | |
| 812 | return 1; |
| 813 | } |
| 814 | __setup("hd=", parse_hd_setup); |
| 815 | |
Adrian Bunk | 01c22bf | 2008-07-16 20:33:47 +0200 | [diff] [blame] | 816 | late_initcall(hd_init); |