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