blob: a3b39940ce02d03e851b70518f450246f8a8c2c6 [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;
101
102#define MAJOR_NR HD_MAJOR
103#define QUEUE (hd_queue)
104#define CURRENT elv_next_request(hd_queue)
105
106#define TIMEOUT_VALUE (6*HZ)
107#define HD_DELAY 0
108
109#define MAX_ERRORS 16 /* Max read/write errors/sector */
110#define RESET_FREQ 8 /* Reset controller every 8th retry */
111#define RECAL_FREQ 4 /* Recalibrate every 4th retry */
112#define MAX_HD 2
113
114#define STAT_OK (READY_STAT|SEEK_STAT)
115#define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
116
117static void recal_intr(void);
118static void bad_rw_intr(void);
119
120static int reset;
121static int hd_error;
122
123/*
124 * This struct defines the HD's and their types.
125 */
126struct hd_i_struct {
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200127 unsigned int head, sect, cyl, wpcom, lzone, ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int unit;
129 int recalibrate;
130 int special_op;
131};
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#ifdef HD_TYPE
134static struct hd_i_struct hd_info[] = { HD_TYPE };
Andi Drebesecea5732007-07-09 23:17:57 +0200135static int NR_HD = ARRAY_SIZE(hd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#else
137static struct hd_i_struct hd_info[MAX_HD];
138static int NR_HD;
139#endif
140
141static struct gendisk *hd_gendisk[MAX_HD];
142
143static struct timer_list device_timer;
144
145#define TIMEOUT_VALUE (6*HZ)
146
147#define SET_TIMER \
148 do { \
149 mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
150 } while (0)
151
152static void (*do_hd)(void) = NULL;
153#define SET_HANDLER(x) \
154if ((do_hd = (x)) != NULL) \
155 SET_TIMER; \
156else \
157 del_timer(&device_timer);
158
159
160#if (HD_DELAY > 0)
Ingo Molnar306e4402005-06-30 02:58:55 -0700161
162#include <asm/i8253.h>
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164unsigned long last_req;
165
166unsigned long read_timer(void)
167{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned long t, flags;
169 int i;
170
171 spin_lock_irqsave(&i8253_lock, flags);
172 t = jiffies * 11932;
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200173 outb_p(0, 0x43);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 i = inb_p(0x40);
175 i |= inb(0x40) << 8;
176 spin_unlock_irqrestore(&i8253_lock, flags);
177 return(t - i);
178}
179#endif
180
181static void __init hd_setup(char *str, int *ints)
182{
183 int hdind = 0;
184
185 if (ints[0] != 3)
186 return;
187 if (hd_info[0].head != 0)
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200188 hdind = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 hd_info[hdind].head = ints[2];
190 hd_info[hdind].sect = ints[3];
191 hd_info[hdind].cyl = ints[1];
192 hd_info[hdind].wpcom = 0;
193 hd_info[hdind].lzone = ints[1];
194 hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
195 NR_HD = hdind+1;
196}
197
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200198static void dump_status(const char *msg, unsigned int stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 char *name = "hd?";
201 if (CURRENT)
202 name = CURRENT->rq_disk->disk_name;
203
204#ifdef VERBOSE_ERRORS
205 printk("%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
206 if (stat & BUSY_STAT) printk("Busy ");
207 if (stat & READY_STAT) printk("DriveReady ");
208 if (stat & WRERR_STAT) printk("WriteFault ");
209 if (stat & SEEK_STAT) printk("SeekComplete ");
210 if (stat & DRQ_STAT) printk("DataRequest ");
211 if (stat & ECC_STAT) printk("CorrectedError ");
212 if (stat & INDEX_STAT) printk("Index ");
213 if (stat & ERR_STAT) printk("Error ");
214 printk("}\n");
215 if ((stat & ERR_STAT) == 0) {
216 hd_error = 0;
217 } else {
218 hd_error = inb(HD_ERROR);
219 printk("%s: %s: error=0x%02x { ", name, msg, hd_error & 0xff);
220 if (hd_error & BBD_ERR) printk("BadSector ");
221 if (hd_error & ECC_ERR) printk("UncorrectableError ");
222 if (hd_error & ID_ERR) printk("SectorIdNotFound ");
223 if (hd_error & ABRT_ERR) printk("DriveStatusError ");
224 if (hd_error & TRK0_ERR) printk("TrackZeroNotFound ");
225 if (hd_error & MARK_ERR) printk("AddrMarkNotFound ");
226 printk("}");
227 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
228 printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
229 inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
230 if (CURRENT)
Tejun Heo83096eb2009-05-07 22:24:39 +0900231 printk(", sector=%ld", blk_rq_pos(CURRENT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 printk("\n");
234 }
235#else
236 printk("%s: %s: status=0x%02x.\n", name, msg, stat & 0xff);
237 if ((stat & ERR_STAT) == 0) {
238 hd_error = 0;
239 } else {
240 hd_error = inb(HD_ERROR);
241 printk("%s: %s: error=0x%02x.\n", name, msg, hd_error & 0xff);
242 }
243#endif
244}
245
246static void check_status(void)
247{
248 int i = inb_p(HD_STATUS);
249
250 if (!OK_STATUS(i)) {
251 dump_status("check_status", i);
252 bad_rw_intr();
253 }
254}
255
256static int controller_busy(void)
257{
258 int retries = 100000;
259 unsigned char status;
260
261 do {
262 status = inb_p(HD_STATUS);
263 } while ((status & BUSY_STAT) && --retries);
264 return status;
265}
266
267static int status_ok(void)
268{
269 unsigned char status = inb_p(HD_STATUS);
270
271 if (status & BUSY_STAT)
272 return 1; /* Ancient, but does it make sense??? */
273 if (status & WRERR_STAT)
274 return 0;
275 if (!(status & READY_STAT))
276 return 0;
277 if (!(status & SEEK_STAT))
278 return 0;
279 return 1;
280}
281
282static int controller_ready(unsigned int drive, unsigned int head)
283{
284 int retry = 100;
285
286 do {
287 if (controller_busy() & BUSY_STAT)
288 return 0;
289 outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
290 if (status_ok())
291 return 1;
292 } while (--retry);
293 return 0;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static void hd_out(struct hd_i_struct *disk,
297 unsigned int nsect,
298 unsigned int sect,
299 unsigned int head,
300 unsigned int cyl,
301 unsigned int cmd,
302 void (*intr_addr)(void))
303{
304 unsigned short port;
305
306#if (HD_DELAY > 0)
307 while (read_timer() - last_req < HD_DELAY)
308 /* nothing */;
309#endif
310 if (reset)
311 return;
312 if (!controller_ready(disk->unit, head)) {
313 reset = 1;
314 return;
315 }
316 SET_HANDLER(intr_addr);
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200317 outb_p(disk->ctl, HD_CMD);
318 port = HD_DATA;
319 outb_p(disk->wpcom >> 2, ++port);
320 outb_p(nsect, ++port);
321 outb_p(sect, ++port);
322 outb_p(cyl, ++port);
323 outb_p(cyl >> 8, ++port);
324 outb_p(0xA0 | (disk->unit << 4) | head, ++port);
325 outb_p(cmd, ++port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328static void hd_request (void);
329
330static int drive_busy(void)
331{
332 unsigned int i;
333 unsigned char c;
334
335 for (i = 0; i < 500000 ; i++) {
336 c = inb_p(HD_STATUS);
337 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
338 return 0;
339 }
340 dump_status("reset timed out", c);
341 return 1;
342}
343
344static void reset_controller(void)
345{
346 int i;
347
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200348 outb_p(4, HD_CMD);
349 for (i = 0; i < 1000; i++) barrier();
350 outb_p(hd_info[0].ctl & 0x0f, HD_CMD);
351 for (i = 0; i < 1000; i++) barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (drive_busy())
353 printk("hd: controller still busy\n");
354 else if ((hd_error = inb(HD_ERROR)) != 1)
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200355 printk("hd: controller reset failed: %02x\n", hd_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static void reset_hd(void)
359{
360 static int i;
361
362repeat:
363 if (reset) {
364 reset = 0;
365 i = -1;
366 reset_controller();
367 } else {
368 check_status();
369 if (reset)
370 goto repeat;
371 }
372 if (++i < NR_HD) {
373 struct hd_i_struct *disk = &hd_info[i];
374 disk->special_op = disk->recalibrate = 1;
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200375 hd_out(disk, disk->sect, disk->sect, disk->head-1,
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +0200376 disk->cyl, ATA_CMD_INIT_DEV_PARAMS, &reset_hd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (reset)
378 goto repeat;
379 } else
380 hd_request();
381}
382
383/*
384 * Ok, don't know what to do with the unexpected interrupts: on some machines
385 * doing a reset and a retry seems to result in an eternal loop. Right now I
386 * ignore it, and just set the timeout.
387 *
388 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
389 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
390 * "good", we just ignore the interrupt completely.
391 */
392static void unexpected_hd_interrupt(void)
393{
394 unsigned int stat = inb_p(HD_STATUS);
395
396 if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200397 dump_status("unexpected interrupt", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 SET_TIMER;
399 }
400}
401
402/*
403 * bad_rw_intr() now tries to be a bit smarter and does things
404 * according to the error returned by the controller.
405 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
406 */
407static void bad_rw_intr(void)
408{
409 struct request *req = CURRENT;
410 if (req != NULL) {
411 struct hd_i_struct *disk = req->rq_disk->private_data;
412 if (++req->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
Tejun Heof06d9a22009-04-23 11:05:19 +0900413 __blk_end_request_cur(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 disk->special_op = disk->recalibrate = 1;
415 } else if (req->errors % RESET_FREQ == 0)
416 reset = 1;
417 else if ((hd_error & TRK0_ERR) || req->errors % RECAL_FREQ == 0)
418 disk->special_op = disk->recalibrate = 1;
419 /* Otherwise just retry */
420 }
421}
422
423static inline int wait_DRQ(void)
424{
Andrew Mortonb0042232008-02-06 02:57:49 +0100425 int retries;
426 int stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Andrew Mortonb0042232008-02-06 02:57:49 +0100428 for (retries = 0; retries < 100000; retries++) {
429 stat = inb_p(HD_STATUS);
430 if (stat & DRQ_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
Andrew Mortonb0042232008-02-06 02:57:49 +0100432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 dump_status("wait_DRQ", stat);
434 return -1;
435}
436
437static void read_intr(void)
438{
439 struct request *req;
440 int i, retries = 100000;
441
442 do {
443 i = (unsigned) inb_p(HD_STATUS);
444 if (i & BUSY_STAT)
445 continue;
446 if (!OK_STATUS(i))
447 break;
448 if (i & DRQ_STAT)
449 goto ok_to_read;
450 } while (--retries > 0);
451 dump_status("read_intr", i);
452 bad_rw_intr();
453 hd_request();
454 return;
Tejun Heoe091eb62009-04-28 13:06:11 +0900455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456ok_to_read:
457 req = CURRENT;
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200458 insw(HD_DATA, req->buffer, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#ifdef DEBUG
Tejun Heo83096eb2009-05-07 22:24:39 +0900460 printk("%s: read: sector %ld, remaining = %u, buffer=%p\n",
461 req->rq_disk->disk_name, blk_rq_pos(req) + 1,
462 blk_rq_sectors(req) - 1, req->buffer+512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#endif
Tejun Heoe091eb62009-04-28 13:06:11 +0900464 if (__blk_end_request(req, 0, 512)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 SET_HANDLER(&read_intr);
466 return;
467 }
Tejun Heoe091eb62009-04-28 13:06:11 +0900468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 (void) inb_p(HD_STATUS);
470#if (HD_DELAY > 0)
471 last_req = read_timer();
472#endif
Tejun Heoe091eb62009-04-28 13:06:11 +0900473 hd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476static void write_intr(void)
477{
478 struct request *req = CURRENT;
479 int i;
480 int retries = 100000;
481
482 do {
483 i = (unsigned) inb_p(HD_STATUS);
484 if (i & BUSY_STAT)
485 continue;
486 if (!OK_STATUS(i))
487 break;
Tejun Heo83096eb2009-05-07 22:24:39 +0900488 if ((blk_rq_sectors(req) <= 1) || (i & DRQ_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto ok_to_write;
490 } while (--retries > 0);
491 dump_status("write_intr", i);
492 bad_rw_intr();
493 hd_request();
494 return;
Tejun Heoe091eb62009-04-28 13:06:11 +0900495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496ok_to_write:
Tejun Heoe091eb62009-04-28 13:06:11 +0900497 if (__blk_end_request(req, 0, 512)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 SET_HANDLER(&write_intr);
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200499 outsw(HD_DATA, req->buffer, 256);
Tejun Heoe091eb62009-04-28 13:06:11 +0900500 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Tejun Heoe091eb62009-04-28 13:06:11 +0900502
503#if (HD_DELAY > 0)
504 last_req = read_timer();
505#endif
506 hd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509static void recal_intr(void)
510{
511 check_status();
512#if (HD_DELAY > 0)
513 last_req = read_timer();
514#endif
515 hd_request();
516}
517
518/*
519 * This is another of the error-routines I don't know what to do with. The
520 * best idea seems to just set reset, and start all over again.
521 */
522static void hd_times_out(unsigned long dummy)
523{
524 char *name;
525
526 do_hd = NULL;
527
528 if (!CURRENT)
529 return;
530
Tejun Heoe93b9fb2009-04-28 12:38:33 +0900531 spin_lock_irq(hd_queue->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 reset = 1;
533 name = CURRENT->rq_disk->disk_name;
534 printk("%s: timeout\n", name);
535 if (++CURRENT->errors >= MAX_ERRORS) {
536#ifdef DEBUG
537 printk("%s: too many errors\n", name);
538#endif
Tejun Heof06d9a22009-04-23 11:05:19 +0900539 __blk_end_request_cur(CURRENT, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 hd_request();
Tejun Heoe93b9fb2009-04-28 12:38:33 +0900542 spin_unlock_irq(hd_queue->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static int do_special_op(struct hd_i_struct *disk, struct request *req)
546{
547 if (disk->recalibrate) {
548 disk->recalibrate = 0;
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +0200549 hd_out(disk, disk->sect, 0, 0, 0, ATA_CMD_RESTORE, &recal_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return reset;
551 }
552 if (disk->head > 16) {
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200553 printk("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);
Tejun Heof06d9a22009-04-23 11:05:19 +0900554 __blk_end_request_cur(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 disk->special_op = 0;
557 return 1;
558}
559
560/*
561 * The driver enables interrupts as much as possible. In order to do this,
562 * (a) the device-interrupt is disabled before entering hd_request(),
563 * and (b) the timeout-interrupt is disabled before the sti().
564 *
565 * Interrupts are still masked (by default) whenever we are exchanging
566 * data/cmds with a drive, because some drives seem to have very poor
567 * tolerance for latency during I/O. The IDE driver has support to unmask
568 * interrupts for non-broken hardware, so use that driver if required.
569 */
570static void hd_request(void)
571{
572 unsigned int block, nsect, sec, track, head, cyl;
573 struct hd_i_struct *disk;
574 struct request *req;
575
576 if (do_hd)
577 return;
578repeat:
579 del_timer(&device_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 req = CURRENT;
582 if (!req) {
583 do_hd = NULL;
584 return;
585 }
586
587 if (reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 reset_hd();
589 return;
590 }
591 disk = req->rq_disk->private_data;
Tejun Heo83096eb2009-05-07 22:24:39 +0900592 block = blk_rq_pos(req);
593 nsect = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (block >= get_capacity(req->rq_disk) ||
595 ((block+nsect) > get_capacity(req->rq_disk))) {
596 printk("%s: bad access: block=%d, count=%d\n",
597 req->rq_disk->disk_name, block, nsect);
Tejun Heof06d9a22009-04-23 11:05:19 +0900598 __blk_end_request_cur(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto repeat;
600 }
601
602 if (disk->special_op) {
603 if (do_special_op(disk, req))
604 goto repeat;
605 return;
606 }
607 sec = block % disk->sect + 1;
608 track = block / disk->sect;
609 head = track % disk->head;
610 cyl = track / disk->head;
611#ifdef DEBUG
612 printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",
Boaz Harroshe654bc42007-06-20 13:53:23 +0200613 req->rq_disk->disk_name,
614 req_data_dir(req) == READ ? "read" : "writ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 cyl, head, sec, nsect, req->buffer);
616#endif
Jens Axboe4aff5e22006-08-10 08:44:47 +0200617 if (blk_fs_request(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 switch (rq_data_dir(req)) {
619 case READ:
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +0200620 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ,
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200621 &read_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (reset)
623 goto repeat;
624 break;
625 case WRITE:
Bartlomiej Zolnierkiewiczf26b3d72008-10-10 22:39:21 +0200626 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_WRITE,
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200627 &write_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (reset)
629 goto repeat;
630 if (wait_DRQ()) {
631 bad_rw_intr();
632 goto repeat;
633 }
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200634 outsw(HD_DATA, req->buffer, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 break;
636 default:
637 printk("unknown hd-command\n");
Tejun Heof06d9a22009-04-23 11:05:19 +0900638 __blk_end_request_cur(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640 }
641 }
642}
643
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200644static void do_hd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 hd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800649static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800651 struct hd_i_struct *disk = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800653 geo->heads = disk->head;
654 geo->sectors = disk->sect;
655 geo->cylinders = disk->cyl;
656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659/*
660 * Releasing a block device means we sync() it, so that it can safely
661 * be forgotten about...
662 */
663
David Howells7d12e782006-10-05 14:55:46 +0100664static irqreturn_t hd_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 void (*handler)(void) = do_hd;
667
Tejun Heoe93b9fb2009-04-28 12:38:33 +0900668 spin_lock(hd_queue->queue_lock);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 do_hd = NULL;
671 del_timer(&device_timer);
672 if (!handler)
673 handler = unexpected_hd_interrupt;
674 handler();
Tejun Heoe93b9fb2009-04-28 12:38:33 +0900675
676 spin_unlock(hd_queue->queue_lock);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return IRQ_HANDLED;
679}
680
681static struct block_device_operations hd_fops = {
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800682 .getgeo = hd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683};
684
685/*
Thomas Gleixner362537b2006-07-01 19:29:34 -0700686 * This is the hard disk IRQ description. The IRQF_DISABLED in sa_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * means we run the IRQ-handler with interrupts disabled: this is bad for
688 * interrupt latency, but anything else has led to problems on some
689 * machines.
690 *
691 * We enable interrupts in some of the routines after making sure it's
692 * safe.
693 */
694
695static int __init hd_init(void)
696{
697 int drive;
698
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200699 if (register_blkdev(MAJOR_NR, "hd"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -1;
701
702 hd_queue = blk_init_queue(do_hd_request, &hd_lock);
703 if (!hd_queue) {
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200704 unregister_blkdev(MAJOR_NR, "hd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENOMEM;
706 }
707
708 blk_queue_max_sectors(hd_queue, 255);
709 init_timer(&device_timer);
710 device_timer.function = hd_times_out;
711 blk_queue_hardsect_size(hd_queue, 512);
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!NR_HD) {
H. Peter Anvin48dd6432007-07-11 12:18:27 -0700714 /*
715 * We don't know anything about the drive. This means
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * that you *MUST* specify the drive parameters to the
717 * kernel yourself.
H. Peter Anvin48dd6432007-07-11 12:18:27 -0700718 *
719 * If we were on an i386, we used to read this info from
720 * the BIOS or CMOS. This doesn't work all that well,
721 * since this assumes that this is a primary or secondary
722 * drive, and if we're using this legacy driver, it's
723 * probably an auxilliary controller added to recover
724 * legacy data off an ST-506 drive. Either way, it's
725 * definitely safest to have the user explicitly specify
726 * the information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
728 printk("hd: no drives specified - use hd=cyl,head,sectors"
729 " on kernel command line\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto out;
H. Peter Anvin48dd6432007-07-11 12:18:27 -0700731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200733 for (drive = 0 ; drive < NR_HD ; drive++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct gendisk *disk = alloc_disk(64);
735 struct hd_i_struct *p = &hd_info[drive];
736 if (!disk)
737 goto Enomem;
738 disk->major = MAJOR_NR;
739 disk->first_minor = drive << 6;
740 disk->fops = &hd_fops;
741 sprintf(disk->disk_name, "hd%c", 'a'+drive);
742 disk->private_data = p;
743 set_capacity(disk, p->head * p->sect * p->cyl);
744 disk->queue = hd_queue;
745 p->unit = drive;
746 hd_gendisk[drive] = disk;
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200747 printk("%s: %luMB, CHS=%d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 disk->disk_name, (unsigned long)get_capacity(disk)/2048,
749 p->cyl, p->head, p->sect);
750 }
751
Thomas Gleixner362537b2006-07-01 19:29:34 -0700752 if (request_irq(HD_IRQ, hd_interrupt, IRQF_DISABLED, "hd", NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 printk("hd: unable to get IRQ%d for the hard disk driver\n",
754 HD_IRQ);
755 goto out1;
756 }
757 if (!request_region(HD_DATA, 8, "hd")) {
758 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
759 goto out2;
760 }
761 if (!request_region(HD_CMD, 1, "hd(cmd)")) {
762 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);
763 goto out3;
764 }
765
766 /* Let them fly */
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200767 for (drive = 0; drive < NR_HD; drive++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 add_disk(hd_gendisk[drive]);
769
770 return 0;
771
772out3:
773 release_region(HD_DATA, 8);
774out2:
775 free_irq(HD_IRQ, NULL);
776out1:
777 for (drive = 0; drive < NR_HD; drive++)
778 put_disk(hd_gendisk[drive]);
779 NR_HD = 0;
780out:
781 del_timer(&device_timer);
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200782 unregister_blkdev(MAJOR_NR, "hd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 blk_cleanup_queue(hd_queue);
784 return -1;
785Enomem:
786 while (drive--)
787 put_disk(hd_gendisk[drive]);
788 goto out;
789}
790
Paolo Ciarrocchiec297822008-04-26 17:36:41 +0200791static int __init parse_hd_setup(char *line)
792{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 int ints[6];
794
795 (void) get_options(line, ARRAY_SIZE(ints), ints);
796 hd_setup(NULL, ints);
797
798 return 1;
799}
800__setup("hd=", parse_hd_setup);
801
Adrian Bunk01c22bf2008-07-16 20:33:47 +0200802late_initcall(hd_init);