OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [block/] [hd.c] - Blame information for rev 1765

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1622 jcastillo
/*
2
 *  linux/arch/arm/drivers/block/hd.c
3
 *   [ origional file: linux/drivers/block/hd.c ]
4
 *
5
 *  Copyright (C) 1991, 1992  Linus Torvalds
6
 *  Modified 1995  Russell King for ARM processor.
7
 */
8
 
9
/*
10
 * This is the low-level hd interrupt support. It traverses the
11
 * request-list, using interrupts to jump between functions. As
12
 * all the functions are called within interrupts, we may not
13
 * sleep. Special care is recommended.
14
 *
15
 *  modified by Drew Eckhardt to check nr of hd's from the CMOS.
16
 *
17
 *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
18
 *  in the early extended-partition checks and added DM partitions
19
 *
20
 *  IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
21
 *  and general streamlining by Mark Lord.
22
 */
23
 
24
#define DEFAULT_MULT_COUNT  0   /* set to 0 to disable multiple mode at boot */
25
#define DEFAULT_UNMASK_INTR 0   /* set to 0 to *NOT* unmask irq's more often */
26
 
27
#include <asm/irq.h>
28
#include <linux/errno.h>
29
#include <linux/signal.h>
30
#include <linux/sched.h>
31
#include <linux/timer.h>
32
#include <linux/fs.h>
33
#include <linux/kernel.h>
34
#include <linux/hdreg.h>
35
#include <linux/genhd.h>
36
#include <linux/malloc.h>
37
#include <linux/string.h>
38
#include <linux/ioport.h>
39
 
40
#define REALLY_SLOW_IO
41
#include <asm/system.h>
42
#include <asm/io.h>
43
#include <asm/segment.h>
44
 
45
#define MAJOR_NR HD_MAJOR
46
#include <linux/blk.h>
47
 
48
#undef  HD_IRQ
49
#define HD_IRQ 11
50
 
51
static int revalidate_hddisk(kdev_t, int);
52
 
53
#define HD_DELAY        0
54
 
55
#define MAX_ERRORS     16       /* Max read/write errors/sector */
56
#define RESET_FREQ      8       /* Reset controller every 8th retry */
57
#define RECAL_FREQ      4       /* Recalibrate every 4th retry */
58
#define MAX_HD          2
59
 
60
#define STAT_OK         (READY_STAT|SEEK_STAT)
61
#define OK_STATUS(s)    (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
62
 
63
static void recal_intr(void);
64
static void bad_rw_intr(void);
65
 
66
static char recalibrate[MAX_HD];
67
static char special_op[MAX_HD];
68
static int access_count[MAX_HD];
69
static char busy[MAX_HD];
70
static struct wait_queue * busy_wait;
71
 
72
static int reset;
73
static int hd_error;
74
 
75
/*
76
 *  This struct defines the HD's and their types.
77
 */
78
struct hd_i_struct {
79
        unsigned int head,sect,cyl,wpcom,lzone,ctl;
80
        };
81
static struct hd_driveid *hd_ident_info[MAX_HD];
82
 
83
#ifdef HD_TYPE
84
static struct hd_i_struct hd_info[] = { HD_TYPE };
85
struct hd_i_struct bios_info[] = { HD_TYPE };
86
static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
87
#else
88
static struct hd_i_struct hd_info[MAX_HD];
89
struct hd_i_struct bios_info[MAX_HD];
90
static int NR_HD;
91
#endif
92
 
93
static struct hd_struct hd[MAX_HD<<6];
94
static int hd_sizes[MAX_HD<<6];
95
static int hd_blocksizes[MAX_HD<<6];
96
static int hd_hardsectsizes[MAX_HD<<6];
97
 
98
#if (HD_DELAY > 0)
99
unsigned long last_req;
100
 
101
unsigned long read_timer(void)
102
{
103
        unsigned long t, flags;
104
        int i;
105
 
106
        save_flags_cli (flags);
107
        t = jiffies * 11932;
108
        outb_p(0, 0x43);
109
        i = inb_p(0x40);
110
        i |= inb(0x40) << 8;
111
        restore_flags(flags);
112
        return(t - i);
113
}
114
#endif
115
 
116
void hd_setup(char *str, int *ints)
117
{
118
        int hdind = 0;
119
 
120
        if (ints[0] != 3)
121
                return;
122
        if (bios_info[0].head != 0)
123
                hdind=1;
124
        bios_info[hdind].head  = hd_info[hdind].head = ints[2];
125
        bios_info[hdind].sect  = hd_info[hdind].sect = ints[3];
126
        bios_info[hdind].cyl   = hd_info[hdind].cyl = ints[1];
127
        bios_info[hdind].wpcom = hd_info[hdind].wpcom = 0;
128
        bios_info[hdind].lzone = hd_info[hdind].lzone = ints[1];
129
        bios_info[hdind].ctl   = hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
130
        NR_HD = hdind+1;
131
}
132
 
133
static void dump_status (const char *msg, unsigned int stat)
134
{
135
        unsigned long flags;
136
        char devc;
137
 
138
        devc = CURRENT ? 'a' + DEVICE_NR(CURRENT->rq_dev) : '?';
139
        save_flags (flags);
140
        sti();
141
        printk("hd%c: %s: status=0x%02x { ", devc, msg, stat & 0xff);
142
        if (stat & BUSY_STAT)   printk("Busy ");
143
        if (stat & READY_STAT)  printk("DriveReady ");
144
        if (stat & WRERR_STAT)  printk("WriteFault ");
145
        if (stat & SEEK_STAT)   printk("SeekComplete ");
146
        if (stat & DRQ_STAT)    printk("DataRequest ");
147
        if (stat & ECC_STAT)    printk("CorrectedError ");
148
        if (stat & INDEX_STAT)  printk("Index ");
149
        if (stat & ERR_STAT)    printk("Error ");
150
        printk("}\n");
151
        if ((stat & ERR_STAT) == 0) {
152
                hd_error = 0;
153
        } else {
154
                hd_error = inb(HD_ERROR);
155
                printk("hd%c: %s: error=0x%02x { ", devc, msg, hd_error & 0xff);
156
                if (hd_error & BBD_ERR)         printk("BadSector ");
157
                if (hd_error & ECC_ERR)         printk("UncorrectableError ");
158
                if (hd_error & ID_ERR)          printk("SectorIdNotFound ");
159
                if (hd_error & ABRT_ERR)        printk("DriveStatusError ");
160
                if (hd_error & TRK0_ERR)        printk("TrackZeroNotFound ");
161
                if (hd_error & MARK_ERR)        printk("AddrMarkNotFound ");
162
                printk("}");
163
                if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
164
                        printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
165
                                inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
166
                        if (CURRENT)
167
                                printk(", sector=%ld", CURRENT->sector);
168
                }
169
                printk("\n");
170
        }
171
        restore_flags (flags);
172
}
173
 
174
void check_status(void)
175
{
176
        int i = inb_p(HD_STATUS);
177
 
178
        if (!OK_STATUS(i)) {
179
                dump_status("check_status", i);
180
                bad_rw_intr();
181
        }
182
}
183
 
184
static int controller_busy(void)
185
{
186
        int retries = 100000;
187
        unsigned char status;
188
 
189
        do {
190
                status = inb_p(HD_STATUS);
191
        } while ((status & BUSY_STAT) && --retries);
192
        return status;
193
}
194
 
195
static int status_ok(void)
196
{
197
        unsigned char status = inb_p(HD_STATUS);
198
 
199
        if (status & BUSY_STAT)
200
                return 1;       /* Ancient, but does it make sense??? */
201
        if (status & WRERR_STAT)
202
                return 0;
203
        if (!(status & READY_STAT))
204
                return 0;
205
        if (!(status & SEEK_STAT))
206
                return 0;
207
        return 1;
208
}
209
 
210
static int controller_ready(unsigned int drive, unsigned int head)
211
{
212
        int retry = 100;
213
 
214
        do {
215
                if (controller_busy() & BUSY_STAT)
216
                        return 0;
217
                outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
218
                if (status_ok())
219
                        return 1;
220
        } while (--retry);
221
        return 0;
222
}
223
 
224
static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
225
                unsigned int head,unsigned int cyl,unsigned int cmd,
226
                void (*intr_addr)(void))
227
{
228
        unsigned short port;
229
 
230
#if (HD_DELAY > 0)
231
        while (read_timer() - last_req < HD_DELAY)
232
                /* nothing */;
233
#endif
234
        if (reset)
235
                return;
236
        if (!controller_ready(drive, head)) {
237
                reset = 1;
238
                return;
239
        }
240
        SET_INTR(intr_addr);
241
        outb_p(hd_info[drive].ctl,HD_CMD);
242
        port=HD_DATA;
243
        outb_p(hd_info[drive].wpcom>>2,++port);
244
        outb_p(nsect,++port);
245
        outb_p(sect,++port);
246
        outb_p(cyl,++port);
247
        outb_p(cyl>>8,++port);
248
        outb_p(0xA0|(drive<<4)|head,++port);
249
        outb_p(cmd,++port);
250
}
251
 
252
static void hd_request (void);
253
static unsigned int identified  [MAX_HD]; /* 1 = drive ID already displayed   */
254
static unsigned int unmask_intr [MAX_HD]; /* 1 = unmask IRQs during I/O       */
255
static unsigned int max_mult    [MAX_HD]; /* max sectors for MultMode         */
256
static unsigned int mult_req    [MAX_HD]; /* requested MultMode count         */
257
static unsigned int mult_count  [MAX_HD]; /* currently enabled MultMode count */
258
static struct request WCURRENT;
259
 
260
static void fixstring (unsigned char *s, int bytecount)
261
{
262
        unsigned char *p, *end = &s[bytecount &= ~1];   /* bytecount must be even */
263
 
264
        /* convert from big-endian to little-endian */
265
        for (p = end ; p != s;) {
266
                unsigned short *pp = (unsigned short *) (p -= 2);
267
                *pp = (*pp >> 8) | (*pp << 8);
268
        }
269
 
270
        /* strip leading blanks */
271
        while (s != end && *s == ' ')
272
                ++s;
273
 
274
        /* compress internal blanks and strip trailing blanks */
275
        while (s != end && *s) {
276
                if (*s++ != ' ' || (s != end && *s && *s != ' '))
277
                        *p++ = *(s-1);
278
        }
279
 
280
        /* wipe out trailing garbage */
281
        while (p != end)
282
                *p++ = '\0';
283
}
284
 
285
static void identify_intr(void)
286
{
287
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev);
288
        unsigned short stat = inb_p(HD_STATUS);
289
        struct hd_driveid *id = hd_ident_info[dev];
290
 
291
        if (unmask_intr[dev])
292
                sti();
293
        if (stat & (BUSY_STAT|ERR_STAT)) {
294
                printk ("  hd%c: non-IDE device, %dMB, CHS=%d/%d/%d\n", dev+'a',
295
                        hd_info[dev].cyl*hd_info[dev].head*hd_info[dev].sect / 2048,
296
                        hd_info[dev].cyl, hd_info[dev].head, hd_info[dev].sect);
297
                if (id != NULL) {
298
                        hd_ident_info[dev] = NULL;
299
                        kfree_s (id, 512);
300
                }
301
        } else {
302
                insw(HD_DATA, id, 256); /* get ID info */
303
                max_mult[dev] = id->max_multsect;
304
                if ((id->field_valid&1) && id->cur_cyls && id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
305
                        /*
306
                         * Extract the physical drive geometry for our use.
307
                         * Note that we purposely do *not* update the bios_info.
308
                         * This way, programs that use it (like fdisk) will
309
                         * still have the same logical view as the BIOS does,
310
                         * which keeps the partition table from being screwed.
311
                         */
312
                        hd_info[dev].cyl  = id->cur_cyls;
313
                        hd_info[dev].head = id->cur_heads;
314
                        hd_info[dev].sect = id->cur_sectors;
315
                }
316
                fixstring (id->serial_no, sizeof(id->serial_no));
317
                fixstring (id->fw_rev, sizeof(id->fw_rev));
318
                fixstring (id->model, sizeof(id->model));
319
                printk ("  hd%c: %.40s, %dMB w/%dKB Cache, CHS=%d/%d/%d, MaxMult=%d\n",
320
                        dev+'a', id->model, id->cyls*id->heads*id->sectors/2048,
321
                        id->buf_size/2, bios_info[dev].cyl, bios_info[dev].head,
322
                        bios_info[dev].sect, id->max_multsect);
323
                /*
324
                 * Early model Quantum drives go weird at this point,
325
                 *   but doing a recalibrate seems to "fix" them.
326
                 * (Doing a full reset confuses some other model Quantums)
327
                 */
328
                if (!strncmp(id->model, "QUANTUM", 7))
329
                        special_op[dev] = recalibrate[dev] = 1;
330
        }
331
#if (HD_DELAY > 0)
332
        last_req = read_timer();
333
#endif
334
        hd_request();
335
        return;
336
}
337
 
338
static void set_multmode_intr(void)
339
{
340
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev), stat = inb_p(HD_STATUS);
341
 
342
        if (unmask_intr[dev])
343
                sti();
344
        if (stat & (BUSY_STAT|ERR_STAT)) {
345
                mult_req[dev] = mult_count[dev] = 0;
346
                dump_status("set multmode failed", stat);
347
        } else {
348
                if ((mult_count[dev] = mult_req[dev]))
349
                        printk ("  hd%c: enabled %d-sector multiple mode\n",
350
                                dev+'a', mult_count[dev]);
351
                else
352
                        printk ("  hd%c: disabled multiple mode\n", dev+'a');
353
        }
354
#if (HD_DELAY > 0)
355
        last_req = read_timer();
356
#endif
357
        hd_request();
358
        return;
359
}
360
 
361
static int drive_busy(void)
362
{
363
        unsigned int i;
364
        unsigned char c;
365
 
366
        for (i = 0; i < 500000 ; i++) {
367
                c = inb_p(HD_STATUS);
368
                if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
369
                        return 0;
370
        }
371
        dump_status("reset timed out", c);
372
        return 1;
373
}
374
 
375
static void reset_controller(void)
376
{
377
        int     i;
378
 
379
        outb_p(4,HD_CMD);
380
        for(i = 0; i < 1000; i++) barrier();
381
        outb_p(hd_info[0].ctl & 0x0f,HD_CMD);
382
        for(i = 0; i < 1000; i++) barrier();
383
        if (drive_busy())
384
                printk("hd: controller still busy\n");
385
        else if ((hd_error = inb(HD_ERROR)) != 1)
386
                printk("hd: controller reset failed: %02x\n",hd_error);
387
}
388
 
389
static void reset_hd(void)
390
{
391
        static int i;
392
 
393
repeat:
394
        if (reset) {
395
                reset = 0;
396
                i = -1;
397
                reset_controller();
398
        } else {
399
                check_status();
400
                if (reset)
401
                        goto repeat;
402
        }
403
        if (++i < NR_HD) {
404
                special_op[i] = recalibrate[i] = 1;
405
                if (unmask_intr[i]) {
406
                        unmask_intr[i] = DEFAULT_UNMASK_INTR;
407
                        printk("hd%c: reset irq-unmasking to %d\n",i+'a',
408
                                DEFAULT_UNMASK_INTR);
409
                }
410
                if (mult_req[i] || mult_count[i]) {
411
                        mult_count[i] = 0;
412
                        mult_req[i] = DEFAULT_MULT_COUNT;
413
                        printk("hd%c: reset multiple mode to %d\n",i+'a',
414
                                DEFAULT_MULT_COUNT);
415
                }
416
                hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
417
                        hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
418
                if (reset)
419
                        goto repeat;
420
        } else
421
                hd_request();
422
}
423
 
424
/*
425
 * Ok, don't know what to do with the unexpected interrupts: on some machines
426
 * doing a reset and a retry seems to result in an eternal loop. Right now I
427
 * ignore it, and just set the timeout.
428
 *
429
 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
430
 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
431
 * "good", we just ignore the interrupt completely.
432
 */
433
void unexpected_hd_interrupt(void)
434
{
435
        unsigned int stat = inb_p(HD_STATUS);
436
 
437
        if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
438
                dump_status ("unexpected interrupt", stat);
439
                SET_TIMER;
440
        }
441
}
442
 
443
/*
444
 * bad_rw_intr() now tries to be a bit smarter and does things
445
 * according to the error returned by the controller.
446
 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
447
 */
448
static void bad_rw_intr(void)
449
{
450
        int dev;
451
 
452
        if (!CURRENT)
453
                return;
454
        dev = DEVICE_NR(CURRENT->rq_dev);
455
        if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
456
                end_request(0);
457
                special_op[dev] = recalibrate[dev] = 1;
458
        } else if (CURRENT->errors % RESET_FREQ == 0)
459
                reset = 1;
460
        else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
461
                special_op[dev] = recalibrate[dev] = 1;
462
        /* Otherwise just retry */
463
}
464
 
465
static inline int wait_DRQ(void)
466
{
467
        int retries = 100000, stat;
468
 
469
        while (--retries > 0)
470
                if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)
471
                        return 0;
472
        dump_status("wait_DRQ", stat);
473
        return -1;
474
}
475
 
476
static void read_intr(void)
477
{
478
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev);
479
        int i, retries = 100000, msect = mult_count[dev], nsect;
480
 
481
        if (unmask_intr[dev])
482
                sti();                  /* permit other IRQs during xfer */
483
        do {
484
                i = (unsigned) inb_p(HD_STATUS);
485
                if (i & BUSY_STAT)
486
                        continue;
487
                if (!OK_STATUS(i))
488
                        break;
489
                if (i & DRQ_STAT)
490
                        goto ok_to_read;
491
        } while (--retries > 0);
492
        dump_status("read_intr", i);
493
        bad_rw_intr();
494
        hd_request();
495
        return;
496
ok_to_read:
497
        if (msect) {
498
                if ((nsect = CURRENT->current_nr_sectors) > msect)
499
                        nsect = msect;
500
                msect -= nsect;
501
        } else
502
                nsect = 1;
503
        insw(HD_DATA,CURRENT->buffer,nsect<<8);
504
        CURRENT->sector += nsect;
505
        CURRENT->buffer += nsect<<9;
506
        CURRENT->errors = 0;
507
        i = (CURRENT->nr_sectors -= nsect);
508
 
509
#ifdef DEBUG
510
        printk("hd%c: read: sectors(%ld-%ld), remaining=%ld, buffer=0x%08lx\n",
511
                dev+'a', CURRENT->sector, CURRENT->sector+nsect,
512
                CURRENT->nr_sectors, (unsigned long) CURRENT->buffer+(nsect<<9));
513
#endif
514
        if ((CURRENT->current_nr_sectors -= nsect) <= 0)
515
                end_request(1);
516
        if (i > 0) {
517
                if (msect)
518
                        goto ok_to_read;
519
                SET_INTR(&read_intr);
520
                return;
521
        }
522
        (void) inb_p(HD_STATUS);
523
#if (HD_DELAY > 0)
524
        last_req = read_timer();
525
#endif
526
        if (CURRENT)
527
                hd_request();
528
        return;
529
}
530
 
531
static inline void multwrite (unsigned int dev)
532
{
533
        unsigned int mcount = mult_count[dev];
534
 
535
        while (mcount--) {
536
                outsw(HD_DATA,WCURRENT.buffer,256);
537
                if (!--WCURRENT.nr_sectors)
538
                        return;
539
                WCURRENT.buffer += 512;
540
                if (!--WCURRENT.current_nr_sectors) {
541
                        WCURRENT.bh = WCURRENT.bh->b_reqnext;
542
                        if (WCURRENT.bh == NULL)
543
                                panic("buffer list corrupted\n");
544
                        WCURRENT.current_nr_sectors = WCURRENT.bh->b_size>>9;
545
                        WCURRENT.buffer             = WCURRENT.bh->b_data;
546
                }
547
        }
548
}
549
 
550
static void multwrite_intr(void)
551
{
552
        int i;
553
        unsigned int dev = DEVICE_NR(WCURRENT.rq_dev);
554
 
555
        if (unmask_intr[dev])
556
                sti();
557
        if (OK_STATUS(i=inb_p(HD_STATUS))) {
558
                if (i & DRQ_STAT) {
559
                        if (WCURRENT.nr_sectors) {
560
                                multwrite(dev);
561
                                SET_INTR(&multwrite_intr);
562
                                return;
563
                        }
564
                } else {
565
                        if (!WCURRENT.nr_sectors) {     /* all done? */
566
                                for (i = CURRENT->nr_sectors; i > 0;){
567
                                        i -= CURRENT->current_nr_sectors;
568
                                        end_request(1);
569
                                }
570
#if (HD_DELAY > 0)
571
                                last_req = read_timer();
572
#endif
573
                                if (CURRENT)
574
                                        hd_request();
575
                                return;
576
                        }
577
                }
578
        }
579
        dump_status("multwrite_intr", i);
580
        bad_rw_intr();
581
        hd_request();
582
}
583
 
584
static void write_intr(void)
585
{
586
        int i;
587
        int retries = 100000;
588
 
589
        if (unmask_intr[DEVICE_NR(WCURRENT.rq_dev)])
590
                sti();
591
        do {
592
                i = (unsigned) inb_p(HD_STATUS);
593
                if (i & BUSY_STAT)
594
                        continue;
595
                if (!OK_STATUS(i))
596
                        break;
597
                if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
598
                        goto ok_to_write;
599
        } while (--retries > 0);
600
        dump_status("write_intr", i);
601
        bad_rw_intr();
602
        hd_request();
603
        return;
604
ok_to_write:
605
        CURRENT->sector++;
606
        i = --CURRENT->nr_sectors;
607
        --CURRENT->current_nr_sectors;
608
        CURRENT->buffer += 512;
609
        if (!i || (CURRENT->bh && !SUBSECTOR(i)))
610
                end_request(1);
611
        if (i > 0) {
612
                SET_INTR(&write_intr);
613
                outsw(HD_DATA,CURRENT->buffer,256);
614
                sti();
615
        } else {
616
#if (HD_DELAY > 0)
617
                last_req = read_timer();
618
#endif
619
                hd_request();
620
        }
621
        return;
622
}
623
 
624
static void recal_intr(void)
625
{
626
        check_status();
627
#if (HD_DELAY > 0)
628
        last_req = read_timer();
629
#endif
630
        hd_request();
631
}
632
 
633
/*
634
 * This is another of the error-routines I don't know what to do with. The
635
 * best idea seems to just set reset, and start all over again.
636
 */
637
static void hd_times_out(void)
638
{
639
        unsigned int dev;
640
 
641
        DEVICE_INTR = NULL;
642
        if (!CURRENT)
643
                return;
644
        disable_irq(HD_IRQ);
645
        sti();
646
        reset = 1;
647
        dev = DEVICE_NR(CURRENT->rq_dev);
648
        printk("hd%c: timeout\n", dev+'a');
649
        if (++CURRENT->errors >= MAX_ERRORS) {
650
#ifdef DEBUG
651
                printk("hd%c: too many errors\n", dev+'a');
652
#endif
653
                end_request(0);
654
        }
655
        cli();
656
        hd_request();
657
        enable_irq(HD_IRQ);
658
}
659
 
660
int do_special_op (unsigned int dev)
661
{
662
        if (recalibrate[dev]) {
663
                recalibrate[dev] = 0;
664
                hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
665
                return reset;
666
        }
667
        if (!identified[dev]) {
668
                identified[dev]  = 1;
669
                unmask_intr[dev] = DEFAULT_UNMASK_INTR;
670
                mult_req[dev]    = DEFAULT_MULT_COUNT;
671
                hd_out(dev,0,0,0,0,WIN_IDENTIFY,&identify_intr);
672
                return reset;
673
        }
674
        if (mult_req[dev] != mult_count[dev]) {
675
                hd_out(dev,mult_req[dev],0,0,0,WIN_SETMULT,&set_multmode_intr);
676
                return reset;
677
        }
678
        if (hd_info[dev].head > 16) {
679
                printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
680
                end_request(0);
681
        }
682
        special_op[dev] = 0;
683
        return 1;
684
}
685
 
686
/*
687
 * The driver enables interrupts as much as possible.  In order to do this,
688
 * (a) the device-interrupt is disabled before entering hd_request(),
689
 * and (b) the timeout-interrupt is disabled before the sti().
690
 *
691
 * Interrupts are still masked (by default) whenever we are exchanging
692
 * data/cmds with a drive, because some drives seem to have very poor
693
 * tolerance for latency during I/O.  For devices which don't suffer from
694
 * that problem (most don't), the unmask_intr[] flag can be set to unmask
695
 * other interrupts during data/cmd transfers (by defining DEFAULT_UNMASK_INTR
696
 * to 1, or by using "hdparm -u1 /dev/hd?" from the shell).
697
 */
698
static void hd_request(void)
699
{
700
        unsigned int dev, block, nsect, sec, track, head, cyl;
701
 
702
        if (CURRENT && CURRENT->rq_status == RQ_INACTIVE) return;
703
        if (DEVICE_INTR)
704
                return;
705
repeat:
706
        timer_active &= ~(1<<HD_TIMER);
707
        sti();
708
        INIT_REQUEST;
709
        if (reset) {
710
                cli();
711
                reset_hd();
712
                return;
713
        }
714
        dev = MINOR(CURRENT->rq_dev);
715
        block = CURRENT->sector;
716
        nsect = CURRENT->nr_sectors;
717
        if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {
718
#ifdef DEBUG
719
                if (dev >= (NR_HD<<6))
720
                        printk("hd: bad minor number: device=%s\n",
721
                               kdevname(CURRENT->rq_dev));
722
                else
723
                        printk("hd%c: bad access: block=%d, count=%d\n",
724
                                (MINOR(CURRENT->rq_dev)>>6)+'a', block, nsect);
725
#endif
726
                end_request(0);
727
                goto repeat;
728
        }
729
        block += hd[dev].start_sect;
730
        dev >>= 6;
731
        if (special_op[dev]) {
732
                if (do_special_op(dev))
733
                        goto repeat;
734
                return;
735
        }
736
        sec   = block % hd_info[dev].sect + 1;
737
        track = block / hd_info[dev].sect;
738
        head  = track % hd_info[dev].head;
739
        cyl   = track / hd_info[dev].head;
740
#ifdef DEBUG
741
        printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
742
                dev+'a', (CURRENT->cmd == READ)?"read":"writ",
743
                cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);
744
#endif
745
        if (!unmask_intr[dev])
746
                cli();
747
        if (CURRENT->cmd == READ) {
748
                unsigned int cmd = mult_count[dev] > 1 ? WIN_MULTREAD : WIN_READ;
749
                hd_out(dev,nsect,sec,head,cyl,cmd,&read_intr);
750
                if (reset)
751
                        goto repeat;
752
                return;
753
        }
754
        if (CURRENT->cmd == WRITE) {
755
                if (mult_count[dev])
756
                        hd_out(dev,nsect,sec,head,cyl,WIN_MULTWRITE,&multwrite_intr);
757
                else
758
                        hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
759
                if (reset)
760
                        goto repeat;
761
                if (wait_DRQ()) {
762
                        bad_rw_intr();
763
                        goto repeat;
764
                }
765
                if (mult_count[dev]) {
766
                        WCURRENT = *CURRENT;
767
                        multwrite(dev);
768
                } else
769
                        outsw(HD_DATA,CURRENT->buffer,256);
770
                return;
771
        }
772
        panic("unknown hd-command");
773
}
774
 
775
static void do_hd_request (void)
776
{
777
        disable_irq(HD_IRQ);
778
        hd_request();
779
        enable_irq(HD_IRQ);
780
}
781
 
782
static int hd_ioctl(struct inode * inode, struct file * file,
783
        unsigned int cmd, unsigned long arg)
784
{
785
        struct hd_geometry *loc = (struct hd_geometry *) arg;
786
        int dev, err;
787
        unsigned long flags;
788
 
789
        if ((!inode) || !(inode->i_rdev))
790
                return -EINVAL;
791
        dev = DEVICE_NR(inode->i_rdev);
792
        if (dev >= NR_HD)
793
                return -EINVAL;
794
        switch (cmd) {
795
                case HDIO_GETGEO:
796
                        if (!loc)  return -EINVAL;
797
                        err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
798
                        if (err)
799
                                return err;
800
                        put_user(bios_info[dev].head,
801
                                (char *) &loc->heads);
802
                        put_user(bios_info[dev].sect,
803
                                (char *) &loc->sectors);
804
                        put_user(bios_info[dev].cyl,
805
                                (short *) &loc->cylinders);
806
                        put_user(hd[MINOR(inode->i_rdev)].start_sect,
807
                                (long *) &loc->start);
808
                        return 0;
809
                case BLKRASET:
810
                        if(!suser())  return -EACCES;
811
                        if(arg > 0xff) return -EINVAL;
812
                        read_ahead[MAJOR(inode->i_rdev)] = arg;
813
                        return 0;
814
                case BLKRAGET:
815
                        if (!arg)  return -EINVAL;
816
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
817
                        if (err)
818
                                return err;
819
                        put_user(read_ahead[MAJOR(inode->i_rdev)],(long *) arg);
820
                        return 0;
821
                case BLKGETSIZE:   /* Return device size */
822
                        if (!arg)  return -EINVAL;
823
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
824
                        if (err)
825
                                return err;
826
                        put_user(hd[MINOR(inode->i_rdev)].nr_sects, (long *) arg);
827
                        return 0;
828
                case BLKFLSBUF:
829
                        if(!suser())  return -EACCES;
830
                        fsync_dev(inode->i_rdev);
831
                        invalidate_buffers(inode->i_rdev);
832
                        return 0;
833
 
834
                case BLKRRPART: /* Re-read partition tables */
835
                        return revalidate_hddisk(inode->i_rdev, 1);
836
 
837
                case HDIO_SET_UNMASKINTR:
838
                        if (!suser()) return -EACCES;
839
                        if ((arg > 1) || (MINOR(inode->i_rdev) & 0x3F))
840
                                return -EINVAL;
841
                        unmask_intr[dev] = arg;
842
                        return 0;
843
 
844
                case HDIO_GET_UNMASKINTR:
845
                        if (!arg)  return -EINVAL;
846
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
847
                        if (err)
848
                                return err;
849
                        put_user(unmask_intr[dev], (long *) arg);
850
                        return 0;
851
 
852
                case HDIO_GET_MULTCOUNT:
853
                        if (!arg)  return -EINVAL;
854
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
855
                        if (err)
856
                                return err;
857
                        put_user(mult_count[dev], (long *) arg);
858
                        return 0;
859
 
860
                case HDIO_SET_MULTCOUNT:
861
                        if (!suser()) return -EACCES;
862
                        if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
863
                        save_flags(flags);
864
                        cli();  /* a prior request might still be in progress */
865
                        if (arg > max_mult[dev])
866
                                err = -EINVAL;  /* out of range for device */
867
                        else if (mult_req[dev] != mult_count[dev]) {
868
                                special_op[dev] = 1;
869
                                err = -EBUSY;   /* busy, try again */
870
                        } else {
871
                                mult_req[dev] = arg;
872
                                special_op[dev] = 1;
873
                                err = 0;
874
                        }
875
                        restore_flags(flags);
876
                        return err;
877
 
878
                case HDIO_GET_IDENTITY:
879
                        if (!arg)  return -EINVAL;
880
                        if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
881
                        if (hd_ident_info[dev] == NULL)  return -ENOMSG;
882
                        err = verify_area(VERIFY_WRITE, (char *) arg, sizeof(struct hd_driveid));
883
                        if (err)
884
                                return err;
885
                        memcpy_tofs((char *)arg, (char *) hd_ident_info[dev], sizeof(struct hd_driveid));
886
                        return 0;
887
 
888
                RO_IOCTLS(inode->i_rdev,arg);
889
                default:
890
                        return -EINVAL;
891
        }
892
}
893
 
894
static int hd_open(struct inode * inode, struct file * filp)
895
{
896
        int target;
897
        target =  DEVICE_NR(inode->i_rdev);
898
 
899
        if (target >= NR_HD)
900
                return -ENODEV;
901
        while (busy[target])
902
                sleep_on(&busy_wait);
903
        access_count[target]++;
904
        return 0;
905
}
906
 
907
/*
908
 * Releasing a block device means we sync() it, so that it can safely
909
 * be forgotten about...
910
 */
911
static void hd_release(struct inode * inode, struct file * file)
912
{
913
        int target;
914
        sync_dev(inode->i_rdev);
915
 
916
        target =  DEVICE_NR(inode->i_rdev);
917
        access_count[target]--;
918
 
919
}
920
 
921
static void hd_geninit(struct gendisk *);
922
 
923
static struct gendisk hd_gendisk = {
924
        MAJOR_NR,       /* Major number */
925
        "hd",           /* Major name */
926
        6,              /* Bits to shift to get real from partition */
927
        1 << 6,         /* Number of partitions per real */
928
        MAX_HD,         /* maximum number of real */
929
        hd_geninit,     /* init function */
930
        hd,             /* hd struct */
931
        hd_sizes,       /* block sizes */
932
        0,               /* number */
933
        (void *) bios_info,     /* internal */
934
        NULL            /* next */
935
};
936
 
937
static void hd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
938
{
939
        void (*handler)(void) = DEVICE_INTR;
940
 
941
        DEVICE_INTR = NULL;
942
        timer_active &= ~(1<<HD_TIMER);
943
        if (!handler)
944
                handler = unexpected_hd_interrupt;
945
        handler();
946
        sti();
947
}
948
 
949
/*
950
 * Since we find out the physical drive geometry, we don't touch that.
951
 * We only alter the logical disk geometry that is passed to user programs.
952
 * [as per PC Linux].
953
 */
954
void hd_set_geometry (kdev_t dev, unsigned char secspertrack, unsigned char heads,
955
                unsigned long discsize, unsigned int secsize)
956
{
957
    int minor = MINOR(dev);
958
    int drv = minor >> 6;
959
 
960
    if (bios_info[drv].cyl == 1) {
961
        bios_info[drv].cyl = discsize / (secspertrack * heads * secsize);
962
        bios_info[drv].head = heads;
963
        bios_info[drv].wpcom = -1;
964
        bios_info[drv].ctl = 8;
965
        bios_info[drv].lzone = bios_info[drv].cyl - 1;
966
        bios_info[drv].sect = secspertrack;
967
    }
968
    hd[minor].start_sect = 0;
969
    hd[minor].nr_sects = discsize / secsize;
970
}
971
 
972
/*
973
 * This is the harddisk IRQ description. The SA_INTERRUPT in sa_flags
974
 * means we run the IRQ-handler with interrupts disabled: this is bad for
975
 * interrupt latency, but anything else has led to problems on some
976
 * machines...
977
 *
978
 * We enable interrupts in some of the routines after making sure it's
979
 * safe.
980
 */
981
static void hd_geninit(struct gendisk *ignored)
982
{
983
        int i;
984
 
985
        if (!NR_HD) {
986
                int drive;
987
                extern int number_ide_drives;
988
                /*
989
                 * Default settings
990
                 *
991
                 * If we don't know anything about the drive, then set it
992
                 * so that we have enough to read the boot sector of the
993
                 * ADFS drive.  This means that you *MUST* specify the
994
                 * drive parameters of *all* drives if you have one IDE
995
                 * drive that is not ADFS formatted.
996
                 */
997
                for (drive=0 ; drive<2 ; drive++) {
998
                        bios_info[drive].cyl   = hd_info[drive].cyl = 1;
999
                        bios_info[drive].head  = hd_info[drive].head = 1;
1000
                        bios_info[drive].wpcom = hd_info[drive].wpcom = -1;
1001
                        bios_info[drive].ctl   = hd_info[drive].ctl = 8;
1002
                        bios_info[drive].lzone = hd_info[drive].lzone = 1;
1003
                        bios_info[drive].sect  = hd_info[drive].sect = 17;
1004
                }
1005
 
1006
                /*
1007
                 * We only set this to the one that the host OS gave us
1008
                 * if the user has not defined any types.
1009
                 */
1010
                NR_HD = number_ide_drives;
1011
        }
1012
 
1013
        i = NR_HD;
1014
        while (i-- > 0) {
1015
                /*
1016
                 * The newer E-IDE BIOSs handle drives larger than 1024
1017
                 * cylinders by increasing the number of logical heads
1018
                 * to keep the number of logical cylinders below the
1019
                 * sacred INT13 limit of 1024 (10 bits).  If that is
1020
                 * what's happening here, we'll find out and correct
1021
                 * it later when "identifying" the drive.
1022
                 */
1023
                hd[i<<6].nr_sects = bios_info[i].head *
1024
                                bios_info[i].sect * bios_info[i].cyl;
1025
                hd_ident_info[i] = (struct hd_driveid *) kmalloc(512,GFP_KERNEL);
1026
                special_op[i] = 1;
1027
        }
1028
        if (NR_HD) {
1029
                if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd", NULL)) {
1030
                        printk("hd: unable to get IRQ%d for the harddisk driver\n",HD_IRQ);
1031
                        NR_HD = 0;
1032
                } else {
1033
                        request_region(HD_DATA, 8, "hd");
1034
                        request_region(HD_CMD, 1, "hd(cmd)");
1035
                }
1036
        }
1037
        hd_gendisk.nr_real = NR_HD;
1038
 
1039
        for (i = 0; i < (MAX_HD << 6); i++) {
1040
                hd_blocksizes[i] = 1024;
1041
                hd_hardsectsizes[i] = 512;
1042
        }
1043
        blksize_size[MAJOR_NR] = hd_blocksizes;
1044
        hardsect_size[MAJOR_NR] = hd_hardsectsizes;
1045
}
1046
 
1047
static struct file_operations hd_fops = {
1048
        NULL,                   /* lseek - default */
1049
        block_read,             /* read - general block-dev read */
1050
        block_write,            /* write - general block-dev write */
1051
        NULL,                   /* readdir - bad */
1052
        NULL,                   /* select */
1053
        hd_ioctl,               /* ioctl */
1054
        NULL,                   /* mmap */
1055
        hd_open,                /* open */
1056
        hd_release,             /* release */
1057
        block_fsync             /* fsync */
1058
};
1059
 
1060
int hd_init(void)
1061
{
1062
        if (register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
1063
                printk("hd: unable to get major %d for harddisk\n",MAJOR_NR);
1064
                return -1;
1065
        }
1066
        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1067
        read_ahead[MAJOR_NR] = 8;               /* 8 sector (4kB) read-ahead */
1068
        hd_gendisk.next = gendisk_head;
1069
        gendisk_head = &hd_gendisk;
1070
        timer_table[HD_TIMER].fn = hd_times_out;
1071
        return 0;
1072
}
1073
 
1074
#define DEVICE_BUSY busy[target]
1075
#define USAGE access_count[target]
1076
#define CAPACITY (bios_info[target].head*bios_info[target].sect*bios_info[target].cyl)
1077
/* We assume that the the bios parameters do not change, so the disk capacity
1078
   will not change */
1079
#undef MAYBE_REINIT
1080
#define GENDISK_STRUCT hd_gendisk
1081
 
1082
/*
1083
 * This routine is called to flush all partitions and partition tables
1084
 * for a changed scsi disk, and then re-read the new partition table.
1085
 * If we are revalidating a disk because of a media change, then we
1086
 * enter with usage == 0.  If we are using an ioctl, we automatically have
1087
 * usage == 1 (we need an open channel to use an ioctl :-), so this
1088
 * is our limit.
1089
 */
1090
static int revalidate_hddisk(kdev_t dev, int maxusage)
1091
{
1092
        int target;
1093
        struct gendisk * gdev;
1094
        int max_p;
1095
        int start;
1096
        int i;
1097
        long flags;
1098
 
1099
        target = DEVICE_NR(dev);
1100
        gdev = &GENDISK_STRUCT;
1101
 
1102
        save_flags_cli (flags);
1103
        if (DEVICE_BUSY || USAGE > maxusage) {
1104
                restore_flags(flags);
1105
                return -EBUSY;
1106
        };
1107
        DEVICE_BUSY = 1;
1108
        restore_flags(flags);
1109
 
1110
        max_p = gdev->max_p;
1111
        start = target << gdev->minor_shift;
1112
 
1113
        for (i=max_p - 1; i >=0 ; i--) {
1114
                int minor = start + i;
1115
                kdev_t devi = MKDEV(MAJOR_NR, minor);
1116
                sync_dev(devi);
1117
                invalidate_inodes(devi);
1118
                invalidate_buffers(devi);
1119
                gdev->part[minor].start_sect = 0;
1120
                gdev->part[minor].nr_sects = 0;
1121
        };
1122
 
1123
#ifdef MAYBE_REINIT
1124
        MAYBE_REINIT;
1125
#endif
1126
 
1127
        gdev->part[start].nr_sects = CAPACITY;
1128
        resetup_one_dev(gdev, target);
1129
 
1130
        DEVICE_BUSY = 0;
1131
        wake_up(&busy_wait);
1132
        return 0;
1133
}
1134
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.