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