1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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>
40#include <linux/init.h>
41#include <linux/blkpg.h>
42#include <linux/ata.h>
43#include <linux/hdreg.h>
44
45#define HD_IRQ 14
46
47#define REALLY_SLOW_IO
48#include <asm/system.h>
49#include <asm/io.h>
50#include <asm/uaccess.h>
51
52#ifdef __arm__
53#undef HD_IRQ
54#endif
55#include <asm/irq.h>
56#ifdef __arm__
57#define HD_IRQ IRQ_HARDDISK
58#endif
59
60
61
62#define HD_DATA 0x1f0
63#define HD_ERROR 0x1f1
64#define HD_NSECTOR 0x1f2
65#define HD_SECTOR 0x1f3
66#define HD_LCYL 0x1f4
67#define HD_HCYL 0x1f5
68#define HD_CURRENT 0x1f6
69#define HD_STATUS 0x1f7
70#define HD_FEATURE HD_ERROR
71#define HD_PRECOMP HD_FEATURE
72#define HD_COMMAND HD_STATUS
73
74#define HD_CMD 0x3f6
75#define HD_ALTSTATUS 0x3f6
76
77
78#define ERR_STAT 0x01
79#define INDEX_STAT 0x02
80#define ECC_STAT 0x04
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
89#define MARK_ERR 0x01
90#define TRK0_ERR 0x02
91#define ABRT_ERR 0x04
92#define MCR_ERR 0x08
93#define ID_ERR 0x10
94#define MC_ERR 0x20
95#define ECC_ERR 0x40
96#define BBD_ERR 0x80
97#define ICRC_ERR 0x80
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
110#define RESET_FREQ 8
111#define RECAL_FREQ 4
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
125
126struct hd_i_struct {
127 unsigned int head, sect, cyl, wpcom, lzone, ctl;
128 int unit;
129 int recalibrate;
130 int special_op;
131};
132
133#ifdef HD_TYPE
134static struct hd_i_struct hd_info[] = { HD_TYPE };
135static int NR_HD = ARRAY_SIZE(hd_info);
136#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)
161
162#include <asm/i8253.h>
163
164unsigned long last_req;
165
166unsigned long read_timer(void)
167{
168 unsigned long t, flags;
169 int i;
170
171 spin_lock_irqsave(&i8253_lock, flags);
172 t = jiffies * 11932;
173 outb_p(0, 0x43);
174 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)
188 hdind = 1;
189 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
198static void dump_status(const char *msg, unsigned int stat)
199{
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)
231 printk(", sector=%ld", CURRENT->sector);
232 }
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;
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
296static 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 ;
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);
317 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);
326}
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
348 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();
352 if (drive_busy())
353 printk("hd: controller still busy\n");
354 else if ((hd_error = inb(HD_ERROR)) != 1)
355 printk("hd: controller reset failed: %02x\n", hd_error);
356}
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;
375 hd_out(disk, disk->sect, disk->sect, disk->head-1,
376 disk->cyl, ATA_CMD_INIT_DEV_PARAMS, &reset_hd);
377 if (reset)
378 goto repeat;
379 } else
380 hd_request();
381}
382
383
384
385
386
387
388
389
390
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)) {
397 dump_status("unexpected interrupt", stat);
398 SET_TIMER;
399 }
400}
401
402
403
404
405
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)) {
413 end_request(req, 0);
414 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
420 }
421}
422
423static inline int wait_DRQ(void)
424{
425 int retries;
426 int stat;
427
428 for (retries = 0; retries < 100000; retries++) {
429 stat = inb_p(HD_STATUS);
430 if (stat & DRQ_STAT)
431 return 0;
432 }
433 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;
455ok_to_read:
456 req = CURRENT;
457 insw(HD_DATA, req->buffer, 256);
458 req->sector++;
459 req->buffer += 512;
460 req->errors = 0;
461 i = --req->nr_sectors;
462 --req->current_nr_sectors;
463#ifdef DEBUG
464 printk("%s: read: sector %ld, remaining = %ld, buffer=%p\n",
465 req->rq_disk->disk_name, req->sector, req->nr_sectors,
466 req->buffer+512);
467#endif
468 if (req->current_nr_sectors <= 0)
469 end_request(req, 1);
470 if (i > 0) {
471 SET_HANDLER(&read_intr);
472 return;
473 }
474 (void) inb_p(HD_STATUS);
475#if (HD_DELAY > 0)
476 last_req = read_timer();
477#endif
478 if (elv_next_request(QUEUE))
479 hd_request();
480 return;
481}
482
483static void write_intr(void)
484{
485 struct request *req = CURRENT;
486 int i;
487 int retries = 100000;
488
489 do {
490 i = (unsigned) inb_p(HD_STATUS);
491 if (i & BUSY_STAT)
492 continue;
493 if (!OK_STATUS(i))
494 break;
495 if ((req->nr_sectors <= 1) || (i & DRQ_STAT))
496 goto ok_to_write;
497 } while (--retries > 0);
498 dump_status("write_intr", i);
499 bad_rw_intr();
500 hd_request();
501 return;
502ok_to_write:
503 req->sector++;
504 i = --req->nr_sectors;
505 --req->current_nr_sectors;
506 req->buffer += 512;
507 if (!i || (req->bio && req->current_nr_sectors <= 0))
508 end_request(req, 1);
509 if (i > 0) {
510 SET_HANDLER(&write_intr);
511 outsw(HD_DATA, req->buffer, 256);
512 } else {
513#if (HD_DELAY > 0)
514 last_req = read_timer();
515#endif
516 hd_request();
517 }
518 return;
519}
520
521static void recal_intr(void)
522{
523 check_status();
524#if (HD_DELAY > 0)
525 last_req = read_timer();
526#endif
527 hd_request();
528}
529
530
531
532
533
534static void hd_times_out(unsigned long dummy)
535{
536 char *name;
537
538 do_hd = NULL;
539
540 if (!CURRENT)
541 return;
542
543 spin_lock_irq(hd_queue->queue_lock);
544 reset = 1;
545 name = CURRENT->rq_disk->disk_name;
546 printk("%s: timeout\n", name);
547 if (++CURRENT->errors >= MAX_ERRORS) {
548#ifdef DEBUG
549 printk("%s: too many errors\n", name);
550#endif
551 end_request(CURRENT, 0);
552 }
553 hd_request();
554 spin_unlock_irq(hd_queue->queue_lock);
555}
556
557static int do_special_op(struct hd_i_struct *disk, struct request *req)
558{
559 if (disk->recalibrate) {
560 disk->recalibrate = 0;
561 hd_out(disk, disk->sect, 0, 0, 0, ATA_CMD_RESTORE, &recal_intr);
562 return reset;
563 }
564 if (disk->head > 16) {
565 printk("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);
566 end_request(req, 0);
567 }
568 disk->special_op = 0;
569 return 1;
570}
571
572
573
574
575
576
577
578
579
580
581
582static void hd_request(void)
583{
584 unsigned int block, nsect, sec, track, head, cyl;
585 struct hd_i_struct *disk;
586 struct request *req;
587
588 if (do_hd)
589 return;
590repeat:
591 del_timer(&device_timer);
592
593 req = CURRENT;
594 if (!req) {
595 do_hd = NULL;
596 return;
597 }
598
599 if (reset) {
600 reset_hd();
601 return;
602 }
603 disk = req->rq_disk->private_data;
604 block = req->sector;
605 nsect = req->nr_sectors;
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);
610 end_request(req, 0);
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",
625 req->rq_disk->disk_name,
626 req_data_dir(req) == READ ? "read" : "writ",
627 cyl, head, sec, nsect, req->buffer);
628#endif
629 if (blk_fs_request(req)) {
630 switch (rq_data_dir(req)) {
631 case READ:
632 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ,
633 &read_intr);
634 if (reset)
635 goto repeat;
636 break;
637 case WRITE:
638 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_WRITE,
639 &write_intr);
640 if (reset)
641 goto repeat;
642 if (wait_DRQ()) {
643 bad_rw_intr();
644 goto repeat;
645 }
646 outsw(HD_DATA, req->buffer, 256);
647 break;
648 default:
649 printk("unknown hd-command\n");
650 end_request(req, 0);
651 break;
652 }
653 }
654}
655
656static void do_hd_request(struct request_queue *q)
657{
658 hd_request();
659}
660
661static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
662{
663 struct hd_i_struct *disk = bdev->bd_disk->private_data;
664
665 geo->heads = disk->head;
666 geo->sectors = disk->sect;
667 geo->cylinders = disk->cyl;
668 return 0;
669}
670
671
672
673
674
675
676static irqreturn_t hd_interrupt(int irq, void *dev_id)
677{
678 void (*handler)(void) = do_hd;
679
680 spin_lock(hd_queue->queue_lock);
681
682 do_hd = NULL;
683 del_timer(&device_timer);
684 if (!handler)
685 handler = unexpected_hd_interrupt;
686 handler();
687
688 spin_unlock(hd_queue->queue_lock);
689
690 return IRQ_HANDLED;
691}
692
693static struct block_device_operations hd_fops = {
694 .getgeo = hd_getgeo,
695};
696
697
698
699
700
701
702
703
704
705
706
707static int __init hd_init(void)
708{
709 int drive;
710
711 if (register_blkdev(MAJOR_NR, "hd"))
712 return -1;
713
714 hd_queue = blk_init_queue(do_hd_request, &hd_lock);
715 if (!hd_queue) {
716 unregister_blkdev(MAJOR_NR, "hd");
717 return -ENOMEM;
718 }
719
720 blk_queue_max_sectors(hd_queue, 255);
721 init_timer(&device_timer);
722 device_timer.function = hd_times_out;
723 blk_queue_hardsect_size(hd_queue, 512);
724
725 if (!NR_HD) {
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740 printk("hd: no drives specified - use hd=cyl,head,sectors"
741 " on kernel command line\n");
742 goto out;
743 }
744
745 for (drive = 0 ; drive < NR_HD ; drive++) {
746 struct gendisk *disk = alloc_disk(64);
747 struct hd_i_struct *p = &hd_info[drive];
748 if (!disk)
749 goto Enomem;
750 disk->major = MAJOR_NR;
751 disk->first_minor = drive << 6;
752 disk->fops = &hd_fops;
753 sprintf(disk->disk_name, "hd%c", 'a'+drive);
754 disk->private_data = p;
755 set_capacity(disk, p->head * p->sect * p->cyl);
756 disk->queue = hd_queue;
757 p->unit = drive;
758 hd_gendisk[drive] = disk;
759 printk("%s: %luMB, CHS=%d/%d/%d\n",
760 disk->disk_name, (unsigned long)get_capacity(disk)/2048,
761 p->cyl, p->head, p->sect);
762 }
763
764 if (request_irq(HD_IRQ, hd_interrupt, IRQF_DISABLED, "hd", NULL)) {
765 printk("hd: unable to get IRQ%d for the hard disk driver\n",
766 HD_IRQ);
767 goto out1;
768 }
769 if (!request_region(HD_DATA, 8, "hd")) {
770 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
771 goto out2;
772 }
773 if (!request_region(HD_CMD, 1, "hd(cmd)")) {
774 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);
775 goto out3;
776 }
777
778
779 for (drive = 0; drive < NR_HD; drive++)
780 add_disk(hd_gendisk[drive]);
781
782 return 0;
783
784out3:
785 release_region(HD_DATA, 8);
786out2:
787 free_irq(HD_IRQ, NULL);
788out1:
789 for (drive = 0; drive < NR_HD; drive++)
790 put_disk(hd_gendisk[drive]);
791 NR_HD = 0;
792out:
793 del_timer(&device_timer);
794 unregister_blkdev(MAJOR_NR, "hd");
795 blk_cleanup_queue(hd_queue);
796 return -1;
797Enomem:
798 while (drive--)
799 put_disk(hd_gendisk[drive]);
800 goto out;
801}
802
803static int __init parse_hd_setup(char *line)
804{
805 int ints[6];
806
807 (void) get_options(line, ARRAY_SIZE(ints), ints);
808 hd_setup(NULL, ints);
809
810 return 1;
811}
812__setup("hd=", parse_hd_setup);
813
814late_initcall(hd_init);
815