blob: f65b3f369eb0ce795936045484cfdf9bb7367ac3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/blkpg.h>
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +020042#include <linux/ata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/hdreg.h>
44
Bartlomiej Zolnierkiewicz4fe6e302009-04-01 21:42:25 +020045#define HD_IRQ 14
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#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
99static DEFINE_SPINLOCK(hd_lock);
100static struct request_queue *hd_queue;
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900101static struct request *hd_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#define MAJOR_NR HD_MAJOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105#define TIMEOUT_VALUE (6*HZ)
106#define HD_DELAY 0
107
108#define MAX_ERRORS 16 /* Max read/write errors/sector */
109#define RESET_FREQ 8 /* Reset controller every 8th retry */
110#define RECAL_FREQ 4 /* Recalibrate every 4th retry */
111#define MAX_HD 2
112
113#define STAT_OK (READY_STAT|SEEK_STAT)
114#define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
115
116static void recal_intr(void);
117static void bad_rw_intr(void);
118
119static int reset;
120static int hd_error;
121
122/*
123 * This struct defines the HD's and their types.
124 */
125struct hd_i_struct {
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200126 unsigned int head, sect, cyl, wpcom, lzone, ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int unit;
128 int recalibrate;
129 int special_op;
130};
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#ifdef HD_TYPE
133static struct hd_i_struct hd_info[] = { HD_TYPE };
Andi Drebesecea5732007-07-09 23:17:57 +0200134static int NR_HD = ARRAY_SIZE(hd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#else
136static struct hd_i_struct hd_info[MAX_HD];
137static int NR_HD;
138#endif
139
140static struct gendisk *hd_gendisk[MAX_HD];
141
142static struct timer_list device_timer;
143
144#define TIMEOUT_VALUE (6*HZ)
145
146#define SET_TIMER \
147 do { \
148 mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
149 } while (0)
150
151static void (*do_hd)(void) = NULL;
152#define SET_HANDLER(x) \
153if ((do_hd = (x)) != NULL) \
154 SET_TIMER; \
155else \
156 del_timer(&device_timer);
157
158
159#if (HD_DELAY > 0)
Ingo Molnar306e4402005-06-30 02:58:55 -0700160
161#include <asm/i8253.h>
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163unsigned long last_req;
164
165unsigned long read_timer(void)
166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 unsigned long t, flags;
168 int i;
169
170 spin_lock_irqsave(&i8253_lock, flags);
171 t = jiffies * 11932;
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200172 outb_p(0, 0x43);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 i = inb_p(0x40);
174 i |= inb(0x40) << 8;
175 spin_unlock_irqrestore(&i8253_lock, flags);
176 return(t - i);
177}
178#endif
179
180static void __init hd_setup(char *str, int *ints)
181{
182 int hdind = 0;
183
184 if (ints[0] != 3)
185 return;
186 if (hd_info[0].head != 0)
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200187 hdind = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 hd_info[hdind].head = ints[2];
189 hd_info[hdind].sect = ints[3];
190 hd_info[hdind].cyl = ints[1];
191 hd_info[hdind].wpcom = 0;
192 hd_info[hdind].lzone = ints[1];
193 hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
194 NR_HD = hdind+1;
195}
196
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900197static bool hd_end_request(int err, unsigned int bytes)
198{
199 if (__blk_end_request(hd_req, err, bytes))
200 return true;
201 hd_req = NULL;
202 return false;
203}
204
205static bool hd_end_request_cur(int err)
206{
207 return hd_end_request(err, blk_rq_cur_bytes(hd_req));
208}
209
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200210static void dump_status(const char *msg, unsigned int stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 char *name = "hd?";
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900213 if (hd_req)
214 name = hd_req->rq_disk->disk_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216#ifdef VERBOSE_ERRORS
217 printk("%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
218 if (stat & BUSY_STAT) printk("Busy ");
219 if (stat & READY_STAT) printk("DriveReady ");
220 if (stat & WRERR_STAT) printk("WriteFault ");
221 if (stat & SEEK_STAT) printk("SeekComplete ");
222 if (stat & DRQ_STAT) printk("DataRequest ");
223 if (stat & ECC_STAT) printk("CorrectedError ");
224 if (stat & INDEX_STAT) printk("Index ");
225 if (stat & ERR_STAT) printk("Error ");
226 printk("}\n");
227 if ((stat & ERR_STAT) == 0) {
228 hd_error = 0;
229 } else {
230 hd_error = inb(HD_ERROR);
231 printk("%s: %s: error=0x%02x { ", name, msg, hd_error & 0xff);
232 if (hd_error & BBD_ERR) printk("BadSector ");
233 if (hd_error & ECC_ERR) printk("UncorrectableError ");
234 if (hd_error & ID_ERR) printk("SectorIdNotFound ");
235 if (hd_error & ABRT_ERR) printk("DriveStatusError ");
236 if (hd_error & TRK0_ERR) printk("TrackZeroNotFound ");
237 if (hd_error & MARK_ERR) printk("AddrMarkNotFound ");
238 printk("}");
239 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
240 printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
241 inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900242 if (hd_req)
243 printk(", sector=%ld", blk_rq_pos(hd_req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 printk("\n");
246 }
247#else
248 printk("%s: %s: status=0x%02x.\n", name, msg, stat & 0xff);
249 if ((stat & ERR_STAT) == 0) {
250 hd_error = 0;
251 } else {
252 hd_error = inb(HD_ERROR);
253 printk("%s: %s: error=0x%02x.\n", name, msg, hd_error & 0xff);
254 }
255#endif
256}
257
258static void check_status(void)
259{
260 int i = inb_p(HD_STATUS);
261
262 if (!OK_STATUS(i)) {
263 dump_status("check_status", i);
264 bad_rw_intr();
265 }
266}
267
268static int controller_busy(void)
269{
270 int retries = 100000;
271 unsigned char status;
272
273 do {
274 status = inb_p(HD_STATUS);
275 } while ((status & BUSY_STAT) && --retries);
276 return status;
277}
278
279static int status_ok(void)
280{
281 unsigned char status = inb_p(HD_STATUS);
282
283 if (status & BUSY_STAT)
284 return 1; /* Ancient, but does it make sense??? */
285 if (status & WRERR_STAT)
286 return 0;
287 if (!(status & READY_STAT))
288 return 0;
289 if (!(status & SEEK_STAT))
290 return 0;
291 return 1;
292}
293
294static int controller_ready(unsigned int drive, unsigned int head)
295{
296 int retry = 100;
297
298 do {
299 if (controller_busy() & BUSY_STAT)
300 return 0;
301 outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
302 if (status_ok())
303 return 1;
304 } while (--retry);
305 return 0;
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static void hd_out(struct hd_i_struct *disk,
309 unsigned int nsect,
310 unsigned int sect,
311 unsigned int head,
312 unsigned int cyl,
313 unsigned int cmd,
314 void (*intr_addr)(void))
315{
316 unsigned short port;
317
318#if (HD_DELAY > 0)
319 while (read_timer() - last_req < HD_DELAY)
320 /* nothing */;
321#endif
322 if (reset)
323 return;
324 if (!controller_ready(disk->unit, head)) {
325 reset = 1;
326 return;
327 }
328 SET_HANDLER(intr_addr);
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200329 outb_p(disk->ctl, HD_CMD);
330 port = HD_DATA;
331 outb_p(disk->wpcom >> 2, ++port);
332 outb_p(nsect, ++port);
333 outb_p(sect, ++port);
334 outb_p(cyl, ++port);
335 outb_p(cyl >> 8, ++port);
336 outb_p(0xA0 | (disk->unit << 4) | head, ++port);
337 outb_p(cmd, ++port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340static void hd_request (void);
341
342static int drive_busy(void)
343{
344 unsigned int i;
345 unsigned char c;
346
347 for (i = 0; i < 500000 ; i++) {
348 c = inb_p(HD_STATUS);
349 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
350 return 0;
351 }
352 dump_status("reset timed out", c);
353 return 1;
354}
355
356static void reset_controller(void)
357{
358 int i;
359
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200360 outb_p(4, HD_CMD);
361 for (i = 0; i < 1000; i++) barrier();
362 outb_p(hd_info[0].ctl & 0x0f, HD_CMD);
363 for (i = 0; i < 1000; i++) barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (drive_busy())
365 printk("hd: controller still busy\n");
366 else if ((hd_error = inb(HD_ERROR)) != 1)
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200367 printk("hd: controller reset failed: %02x\n", hd_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370static void reset_hd(void)
371{
372 static int i;
373
374repeat:
375 if (reset) {
376 reset = 0;
377 i = -1;
378 reset_controller();
379 } else {
380 check_status();
381 if (reset)
382 goto repeat;
383 }
384 if (++i < NR_HD) {
385 struct hd_i_struct *disk = &hd_info[i];
386 disk->special_op = disk->recalibrate = 1;
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200387 hd_out(disk, disk->sect, disk->sect, disk->head-1,
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +0200388 disk->cyl, ATA_CMD_INIT_DEV_PARAMS, &reset_hd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (reset)
390 goto repeat;
391 } else
392 hd_request();
393}
394
395/*
396 * Ok, don't know what to do with the unexpected interrupts: on some machines
397 * doing a reset and a retry seems to result in an eternal loop. Right now I
398 * ignore it, and just set the timeout.
399 *
400 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
401 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
402 * "good", we just ignore the interrupt completely.
403 */
404static void unexpected_hd_interrupt(void)
405{
406 unsigned int stat = inb_p(HD_STATUS);
407
408 if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200409 dump_status("unexpected interrupt", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 SET_TIMER;
411 }
412}
413
414/*
415 * bad_rw_intr() now tries to be a bit smarter and does things
416 * according to the error returned by the controller.
417 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
418 */
419static void bad_rw_intr(void)
420{
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900421 struct request *req = hd_req;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (req != NULL) {
424 struct hd_i_struct *disk = req->rq_disk->private_data;
425 if (++req->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900426 hd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 disk->special_op = disk->recalibrate = 1;
428 } else if (req->errors % RESET_FREQ == 0)
429 reset = 1;
430 else if ((hd_error & TRK0_ERR) || req->errors % RECAL_FREQ == 0)
431 disk->special_op = disk->recalibrate = 1;
432 /* Otherwise just retry */
433 }
434}
435
436static inline int wait_DRQ(void)
437{
Andrew Mortonb0042232008-02-06 02:57:49 +0100438 int retries;
439 int stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Andrew Mortonb0042232008-02-06 02:57:49 +0100441 for (retries = 0; retries < 100000; retries++) {
442 stat = inb_p(HD_STATUS);
443 if (stat & DRQ_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return 0;
Andrew Mortonb0042232008-02-06 02:57:49 +0100445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 dump_status("wait_DRQ", stat);
447 return -1;
448}
449
450static void read_intr(void)
451{
452 struct request *req;
453 int i, retries = 100000;
454
455 do {
456 i = (unsigned) inb_p(HD_STATUS);
457 if (i & BUSY_STAT)
458 continue;
459 if (!OK_STATUS(i))
460 break;
461 if (i & DRQ_STAT)
462 goto ok_to_read;
463 } while (--retries > 0);
464 dump_status("read_intr", i);
465 bad_rw_intr();
466 hd_request();
467 return;
Tejun Heoe091eb62009-04-28 13:06:11 +0900468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469ok_to_read:
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900470 req = hd_req;
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200471 insw(HD_DATA, req->buffer, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#ifdef DEBUG
Tejun Heo83096eb2009-05-07 22:24:39 +0900473 printk("%s: read: sector %ld, remaining = %u, buffer=%p\n",
474 req->rq_disk->disk_name, blk_rq_pos(req) + 1,
475 blk_rq_sectors(req) - 1, req->buffer+512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#endif
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900477 if (hd_end_request(0, 512)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 SET_HANDLER(&read_intr);
479 return;
480 }
Tejun Heoe091eb62009-04-28 13:06:11 +0900481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 (void) inb_p(HD_STATUS);
483#if (HD_DELAY > 0)
484 last_req = read_timer();
485#endif
Tejun Heoe091eb62009-04-28 13:06:11 +0900486 hd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489static void write_intr(void)
490{
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900491 struct request *req = hd_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int i;
493 int retries = 100000;
494
495 do {
496 i = (unsigned) inb_p(HD_STATUS);
497 if (i & BUSY_STAT)
498 continue;
499 if (!OK_STATUS(i))
500 break;
Tejun Heo83096eb2009-05-07 22:24:39 +0900501 if ((blk_rq_sectors(req) <= 1) || (i & DRQ_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto ok_to_write;
503 } while (--retries > 0);
504 dump_status("write_intr", i);
505 bad_rw_intr();
506 hd_request();
507 return;
Tejun Heoe091eb62009-04-28 13:06:11 +0900508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509ok_to_write:
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900510 if (hd_end_request(0, 512)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 SET_HANDLER(&write_intr);
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200512 outsw(HD_DATA, req->buffer, 256);
Tejun Heoe091eb62009-04-28 13:06:11 +0900513 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Tejun Heoe091eb62009-04-28 13:06:11 +0900515
516#if (HD_DELAY > 0)
517 last_req = read_timer();
518#endif
519 hd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522static void recal_intr(void)
523{
524 check_status();
525#if (HD_DELAY > 0)
526 last_req = read_timer();
527#endif
528 hd_request();
529}
530
531/*
532 * This is another of the error-routines I don't know what to do with. The
533 * best idea seems to just set reset, and start all over again.
534 */
535static void hd_times_out(unsigned long dummy)
536{
537 char *name;
538
539 do_hd = NULL;
540
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900541 if (!hd_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return;
543
Tejun Heoe93b9fb2009-04-28 12:38:33 +0900544 spin_lock_irq(hd_queue->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 reset = 1;
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900546 name = hd_req->rq_disk->disk_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 printk("%s: timeout\n", name);
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900548 if (++hd_req->errors >= MAX_ERRORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#ifdef DEBUG
550 printk("%s: too many errors\n", name);
551#endif
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900552 hd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 hd_request();
Tejun Heoe93b9fb2009-04-28 12:38:33 +0900555 spin_unlock_irq(hd_queue->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558static int do_special_op(struct hd_i_struct *disk, struct request *req)
559{
560 if (disk->recalibrate) {
561 disk->recalibrate = 0;
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +0200562 hd_out(disk, disk->sect, 0, 0, 0, ATA_CMD_RESTORE, &recal_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return reset;
564 }
565 if (disk->head > 16) {
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200566 printk("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900567 hd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 disk->special_op = 0;
570 return 1;
571}
572
573/*
574 * The driver enables interrupts as much as possible. In order to do this,
575 * (a) the device-interrupt is disabled before entering hd_request(),
576 * and (b) the timeout-interrupt is disabled before the sti().
577 *
578 * Interrupts are still masked (by default) whenever we are exchanging
579 * data/cmds with a drive, because some drives seem to have very poor
580 * tolerance for latency during I/O. The IDE driver has support to unmask
581 * interrupts for non-broken hardware, so use that driver if required.
582 */
583static void hd_request(void)
584{
585 unsigned int block, nsect, sec, track, head, cyl;
586 struct hd_i_struct *disk;
587 struct request *req;
588
589 if (do_hd)
590 return;
591repeat:
592 del_timer(&device_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900594 if (!hd_req) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900595 hd_req = blk_fetch_request(hd_queue);
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900596 if (!hd_req) {
597 do_hd = NULL;
598 return;
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900601 req = hd_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 reset_hd();
605 return;
606 }
607 disk = req->rq_disk->private_data;
Tejun Heo83096eb2009-05-07 22:24:39 +0900608 block = blk_rq_pos(req);
609 nsect = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (block >= get_capacity(req->rq_disk) ||
611 ((block+nsect) > get_capacity(req->rq_disk))) {
612 printk("%s: bad access: block=%d, count=%d\n",
613 req->rq_disk->disk_name, block, nsect);
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900614 hd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto repeat;
616 }
617
618 if (disk->special_op) {
619 if (do_special_op(disk, req))
620 goto repeat;
621 return;
622 }
623 sec = block % disk->sect + 1;
624 track = block / disk->sect;
625 head = track % disk->head;
626 cyl = track / disk->head;
627#ifdef DEBUG
628 printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",
Boaz Harroshe654bc42007-06-20 13:53:23 +0200629 req->rq_disk->disk_name,
630 req_data_dir(req) == READ ? "read" : "writ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 cyl, head, sec, nsect, req->buffer);
632#endif
Jens Axboe4aff5e22006-08-10 08:44:47 +0200633 if (blk_fs_request(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 switch (rq_data_dir(req)) {
635 case READ:
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +0200636 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ,
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200637 &read_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (reset)
639 goto repeat;
640 break;
641 case WRITE:
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +0200642 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_WRITE,
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200643 &write_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (reset)
645 goto repeat;
646 if (wait_DRQ()) {
647 bad_rw_intr();
648 goto repeat;
649 }
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200650 outsw(HD_DATA, req->buffer, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652 default:
653 printk("unknown hd-command\n");
Tejun Heo8a12c4a2009-05-08 11:54:02 +0900654 hd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 }
657 }
658}
659
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200660static void do_hd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 hd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800665static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800667 struct hd_i_struct *disk = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800669 geo->heads = disk->head;
670 geo->sectors = disk->sect;
671 geo->cylinders = disk->cyl;
672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
676 * Releasing a block device means we sync() it, so that it can safely
677 * be forgotten about...
678 */
679
David Howells7d12e782006-10-05 14:55:46 +0100680static irqreturn_t hd_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 void (*handler)(void) = do_hd;
683
Tejun Heoe93b9fb2009-04-28 12:38:33 +0900684 spin_lock(hd_queue->queue_lock);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 do_hd = NULL;
687 del_timer(&device_timer);
688 if (!handler)
689 handler = unexpected_hd_interrupt;
690 handler();
Tejun Heoe93b9fb2009-04-28 12:38:33 +0900691
692 spin_unlock(hd_queue->queue_lock);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return IRQ_HANDLED;
695}
696
697static struct block_device_operations hd_fops = {
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800698 .getgeo = hd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
701/*
Thomas Gleixner362537b2006-07-01 19:29:34 -0700702 * This is the hard disk IRQ description. The IRQF_DISABLED in sa_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * means we run the IRQ-handler with interrupts disabled: this is bad for
704 * interrupt latency, but anything else has led to problems on some
705 * machines.
706 *
707 * We enable interrupts in some of the routines after making sure it's
708 * safe.
709 */
710
711static int __init hd_init(void)
712{
713 int drive;
714
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200715 if (register_blkdev(MAJOR_NR, "hd"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return -1;
717
718 hd_queue = blk_init_queue(do_hd_request, &hd_lock);
719 if (!hd_queue) {
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200720 unregister_blkdev(MAJOR_NR, "hd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -ENOMEM;
722 }
723
724 blk_queue_max_sectors(hd_queue, 255);
725 init_timer(&device_timer);
726 device_timer.function = hd_times_out;
Martin K. Petersene1defc42009-05-22 17:17:49 -0400727 blk_queue_logical_block_size(hd_queue, 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!NR_HD) {
H. Peter Anvin48dd6432007-07-11 12:18:27 -0700730 /*
731 * We don't know anything about the drive. This means
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * that you *MUST* specify the drive parameters to the
733 * kernel yourself.
H. Peter Anvin48dd6432007-07-11 12:18:27 -0700734 *
735 * If we were on an i386, we used to read this info from
736 * the BIOS or CMOS. This doesn't work all that well,
737 * since this assumes that this is a primary or secondary
738 * drive, and if we're using this legacy driver, it's
739 * probably an auxilliary controller added to recover
740 * legacy data off an ST-506 drive. Either way, it's
741 * definitely safest to have the user explicitly specify
742 * the information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744 printk("hd: no drives specified - use hd=cyl,head,sectors"
745 " on kernel command line\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto out;
H. Peter Anvin48dd6432007-07-11 12:18:27 -0700747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200749 for (drive = 0 ; drive < NR_HD ; drive++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct gendisk *disk = alloc_disk(64);
751 struct hd_i_struct *p = &hd_info[drive];
752 if (!disk)
753 goto Enomem;
754 disk->major = MAJOR_NR;
755 disk->first_minor = drive << 6;
756 disk->fops = &hd_fops;
757 sprintf(disk->disk_name, "hd%c", 'a'+drive);
758 disk->private_data = p;
759 set_capacity(disk, p->head * p->sect * p->cyl);
760 disk->queue = hd_queue;
761 p->unit = drive;
762 hd_gendisk[drive] = disk;
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200763 printk("%s: %luMB, CHS=%d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 disk->disk_name, (unsigned long)get_capacity(disk)/2048,
765 p->cyl, p->head, p->sect);
766 }
767
Thomas Gleixner362537b2006-07-01 19:29:34 -0700768 if (request_irq(HD_IRQ, hd_interrupt, IRQF_DISABLED, "hd", NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 printk("hd: unable to get IRQ%d for the hard disk driver\n",
770 HD_IRQ);
771 goto out1;
772 }
773 if (!request_region(HD_DATA, 8, "hd")) {
774 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
775 goto out2;
776 }
777 if (!request_region(HD_CMD, 1, "hd(cmd)")) {
778 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);
779 goto out3;
780 }
781
782 /* Let them fly */
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200783 for (drive = 0; drive < NR_HD; drive++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 add_disk(hd_gendisk[drive]);
785
786 return 0;
787
788out3:
789 release_region(HD_DATA, 8);
790out2:
791 free_irq(HD_IRQ, NULL);
792out1:
793 for (drive = 0; drive < NR_HD; drive++)
794 put_disk(hd_gendisk[drive]);
795 NR_HD = 0;
796out:
797 del_timer(&device_timer);
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200798 unregister_blkdev(MAJOR_NR, "hd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 blk_cleanup_queue(hd_queue);
800 return -1;
801Enomem:
802 while (drive--)
803 put_disk(hd_gendisk[drive]);
804 goto out;
805}
806
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200807static int __init parse_hd_setup(char *line)
808{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int ints[6];
810
811 (void) get_options(line, ARRAY_SIZE(ints), ints);
812 hd_setup(NULL, ints);
813
814 return 1;
815}
816__setup("hd=", parse_hd_setup);
817
Adrian Bunk01c22bf2008-07-16 20:33:47 +0200818late_initcall(hd_init);