This source file includes following definitions.
- CMOS_READ
- read_timer
- hd_setup
- dump_status
- check_status
- controller_busy
- status_ok
- controller_ready
- hd_out
- fixstring
- identify_intr
- set_multmode_intr
- drive_busy
- reset_controller
- reset_hd
- unexpected_hd_interrupt
- bad_rw_intr
- wait_DRQ
- read_intr
- multwrite
- multwrite_intr
- write_intr
- recal_intr
- hd_times_out
- do_special_op
- hd_request
- do_hd_request
- hd_ioctl
- hd_open
- hd_release
- hd_interrupt
- hd_geninit
- hd_init
- revalidate_hddisk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #define DEFAULT_MULT_COUNT 0
23 #define DEFAULT_UNMASK_INTR 0
24
25 #include <asm/irq.h>
26 #include <linux/errno.h>
27 #include <linux/signal.h>
28 #include <linux/sched.h>
29 #include <linux/timer.h>
30 #include <linux/fs.h>
31 #include <linux/kernel.h>
32 #include <linux/hdreg.h>
33 #include <linux/genhd.h>
34 #include <linux/config.h>
35 #include <linux/malloc.h>
36 #include <linux/string.h>
37
38 #define REALLY_SLOW_IO
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/segment.h>
42
43 #define MAJOR_NR HD_MAJOR
44 #include "blk.h"
45
46 #define HD_IRQ 14
47
48 static int revalidate_hddisk(int, int);
49
50 static inline unsigned char CMOS_READ(unsigned char addr)
51 {
52 outb_p(addr,0x70);
53 return inb_p(0x71);
54 }
55
56 #define HD_DELAY 0
57
58 #define MAX_ERRORS 16
59 #define RESET_FREQ 8
60 #define RECAL_FREQ 4
61 #define MAX_HD 2
62
63 #define STAT_OK (READY_STAT|SEEK_STAT)
64 #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
65
66 static void recal_intr(void);
67 static void bad_rw_intr(void);
68
69 static char recalibrate[MAX_HD] = { 0, };
70 static char special_op[MAX_HD] = { 0, };
71 static int access_count[MAX_HD] = {0, };
72 static char busy[MAX_HD] = {0, };
73 static struct wait_queue * busy_wait = NULL;
74
75 static int reset = 0;
76 static int hd_error = 0;
77
78
79
80
81 struct hd_i_struct {
82 unsigned int head,sect,cyl,wpcom,lzone,ctl;
83 };
84 static struct hd_driveid *hd_ident_info[MAX_HD] = {0, };
85
86 #ifdef HD_TYPE
87 static struct hd_i_struct hd_info[] = { HD_TYPE };
88 struct hd_i_struct bios_info[] = { HD_TYPE };
89 static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
90 #else
91 static struct hd_i_struct hd_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
92 struct hd_i_struct bios_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
93 static int NR_HD = 0;
94 #endif
95
96 static struct hd_struct hd[MAX_HD<<6]={{0,0},};
97 static int hd_sizes[MAX_HD<<6] = {0, };
98 static int hd_blocksizes[MAX_HD<<6] = {0, };
99
100 #if (HD_DELAY > 0)
101 unsigned long last_req;
102
103 unsigned long read_timer(void)
104 {
105 unsigned long t, flags;
106 int i;
107
108 save_flags(flags);
109 cli();
110 t = jiffies * 11932;
111 outb_p(0, 0x43);
112 i = inb_p(0x40);
113 i |= inb(0x40) << 8;
114 restore_flags(flags);
115 return(t - i);
116 }
117 #endif
118
119 void hd_setup(char *str, int *ints)
120 {
121 int hdind = 0;
122
123 if (ints[0] != 3)
124 return;
125 if (bios_info[0].head != 0)
126 hdind=1;
127 bios_info[hdind].head = hd_info[hdind].head = ints[2];
128 bios_info[hdind].sect = hd_info[hdind].sect = ints[3];
129 bios_info[hdind].cyl = hd_info[hdind].cyl = ints[1];
130 bios_info[hdind].wpcom = hd_info[hdind].wpcom = 0;
131 bios_info[hdind].lzone = hd_info[hdind].lzone = ints[1];
132 bios_info[hdind].ctl = hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
133 NR_HD = hdind+1;
134 }
135
136 static void dump_status (char *msg, unsigned int stat)
137 {
138 unsigned long flags;
139 char devc;
140
141 devc = CURRENT ? 'a' + DEVICE_NR(CURRENT->dev) : '?';
142 save_flags (flags);
143 sti();
144 printk("hd%c: %s: status=0x%02x { ", devc, msg, stat & 0xff);
145 if (stat & BUSY_STAT) printk("Busy ");
146 if (stat & READY_STAT) printk("DriveReady ");
147 if (stat & WRERR_STAT) printk("WriteFault ");
148 if (stat & SEEK_STAT) printk("SeekComplete ");
149 if (stat & DRQ_STAT) printk("DataRequest ");
150 if (stat & ECC_STAT) printk("CorrectedError ");
151 if (stat & INDEX_STAT) printk("Index ");
152 if (stat & ERR_STAT) printk("Error ");
153 printk("}\n");
154 if ((stat & ERR_STAT) == 0) {
155 hd_error = 0;
156 } else {
157 hd_error = inb(HD_ERROR);
158 printk("hd%c: %s: error=0x%02x { ", devc, msg, hd_error & 0xff);
159 if (hd_error & BBD_ERR) printk("BadSector ");
160 if (hd_error & ECC_ERR) printk("UncorrectableError ");
161 if (hd_error & ID_ERR) printk("SectorIdNotFound ");
162 if (hd_error & ABRT_ERR) printk("DriveStatusError ");
163 if (hd_error & TRK0_ERR) printk("TrackZeroNotFound ");
164 if (hd_error & MARK_ERR) printk("AddrMarkNotFound ");
165 printk("}");
166 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
167 printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
168 inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
169 if (CURRENT)
170 printk(", sector=%ld", CURRENT->sector);
171 }
172 printk("\n");
173 }
174 restore_flags (flags);
175 }
176
177 void check_status(void)
178 {
179 int i = inb_p(HD_STATUS);
180
181 if (!OK_STATUS(i)) {
182 dump_status("check_status", i);
183 bad_rw_intr();
184 }
185 }
186
187 static int controller_busy(void)
188 {
189 int retries = 100000;
190 unsigned char status;
191
192 do {
193 status = inb_p(HD_STATUS);
194 } while ((status & BUSY_STAT) && --retries);
195 return status;
196 }
197
198 static int status_ok(void)
199 {
200 unsigned char status = inb_p(HD_STATUS);
201
202 if (status & BUSY_STAT)
203 return 1;
204 if (status & WRERR_STAT)
205 return 0;
206 if (!(status & READY_STAT))
207 return 0;
208 if (!(status & SEEK_STAT))
209 return 0;
210 return 1;
211 }
212
213 static int controller_ready(unsigned int drive, unsigned int head)
214 {
215 int retry = 100;
216
217 do {
218 if (controller_busy() & BUSY_STAT)
219 return 0;
220 outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
221 if (status_ok())
222 return 1;
223 } while (--retry);
224 return 0;
225 }
226
227 static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
228 unsigned int head,unsigned int cyl,unsigned int cmd,
229 void (*intr_addr)(void))
230 {
231 unsigned short port;
232
233 #if (HD_DELAY > 0)
234 while (read_timer() - last_req < HD_DELAY)
235 ;
236 #endif
237 if (reset)
238 return;
239 if (!controller_ready(drive, head)) {
240 reset = 1;
241 return;
242 }
243 SET_INTR(intr_addr);
244 outb_p(hd_info[drive].ctl,HD_CMD);
245 port=HD_DATA;
246 outb_p(hd_info[drive].wpcom>>2,++port);
247 outb_p(nsect,++port);
248 outb_p(sect,++port);
249 outb_p(cyl,++port);
250 outb_p(cyl>>8,++port);
251 outb_p(0xA0|(drive<<4)|head,++port);
252 outb_p(cmd,++port);
253 }
254
255 static void hd_request (void);
256 static unsigned int identified [MAX_HD] = {0,};
257 static unsigned int unmask_intr [MAX_HD] = {0,};
258 static unsigned int max_mult [MAX_HD] = {0,};
259 static unsigned int mult_req [MAX_HD] = {0,};
260 static unsigned int mult_count [MAX_HD] = {0,};
261 static struct request WCURRENT;
262
263 static void fixstring (unsigned char *s, int bytecount)
264 {
265 unsigned char *p, *end = &s[bytecount &= ~1];
266
267
268 for (p = end ; p != s;) {
269 unsigned short *pp = (unsigned short *) (p -= 2);
270 *pp = (*pp >> 8) | (*pp << 8);
271 }
272
273
274 while (s != end && *s == ' ')
275 ++s;
276
277
278 while (s != end && *s) {
279 if (*s++ != ' ' || (s != end && *s && *s != ' '))
280 *p++ = *(s-1);
281 }
282
283
284 while (p != end)
285 *p++ = '\0';
286 }
287
288 static void identify_intr(void)
289 {
290 unsigned int dev = DEVICE_NR(CURRENT->dev);
291 unsigned short stat = inb_p(HD_STATUS);
292 struct hd_driveid *id = hd_ident_info[dev];
293
294 if (unmask_intr[dev])
295 sti();
296 if (stat & (BUSY_STAT|ERR_STAT)) {
297 printk (" hd%c: non-IDE device, %dMB, CHS=%d/%d/%d\n", dev+'a',
298 hd_info[dev].cyl*hd_info[dev].head*hd_info[dev].sect / 2048,
299 hd_info[dev].cyl, hd_info[dev].head, hd_info[dev].sect);
300 if (id != NULL) {
301 hd_ident_info[dev] = NULL;
302 kfree_s (id, 512);
303 }
304 } else {
305 insw(HD_DATA, id, 256);
306 max_mult[dev] = id->max_multsect;
307 if ((id->field_valid&1) && id->cur_cyls && id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
308
309
310
311
312
313
314
315 hd_info[dev].cyl = id->cur_cyls;
316 hd_info[dev].head = id->cur_heads;
317 hd_info[dev].sect = id->cur_sectors;
318 }
319 fixstring (id->serial_no, sizeof(id->serial_no));
320 fixstring (id->fw_rev, sizeof(id->fw_rev));
321 fixstring (id->model, sizeof(id->model));
322 printk (" hd%c: %.40s, %dMB w/%dKB Cache, CHS=%d/%d/%d, MaxMult=%d\n",
323 dev+'a', id->model, id->cyls*id->heads*id->sectors/2048,
324 id->buf_size/2, bios_info[dev].cyl, bios_info[dev].head,
325 bios_info[dev].sect, id->max_multsect);
326
327
328
329
330
331 if (!strncmp(id->model, "QUANTUM", 7))
332 special_op[dev] = recalibrate[dev] = 1;
333 }
334 #if (HD_DELAY > 0)
335 last_req = read_timer();
336 #endif
337 hd_request();
338 return;
339 }
340
341 static void set_multmode_intr(void)
342 {
343 unsigned int dev = DEVICE_NR(CURRENT->dev), stat = inb_p(HD_STATUS);
344
345 if (unmask_intr[dev])
346 sti();
347 if (stat & (BUSY_STAT|ERR_STAT)) {
348 mult_req[dev] = mult_count[dev] = 0;
349 dump_status("set multmode failed", stat);
350 } else {
351 if ((mult_count[dev] = mult_req[dev]))
352 printk (" hd%c: enabled %d-sector multiple mode\n",
353 dev+'a', mult_count[dev]);
354 else
355 printk (" hd%c: disabled multiple mode\n", dev+'a');
356 }
357 #if (HD_DELAY > 0)
358 last_req = read_timer();
359 #endif
360 hd_request();
361 return;
362 }
363
364 static int drive_busy(void)
365 {
366 unsigned int i;
367 unsigned char c;
368
369 for (i = 0; i < 500000 ; i++) {
370 c = inb_p(HD_STATUS);
371 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
372 return 0;
373 }
374 dump_status("reset timed out", c);
375 return 1;
376 }
377
378 static void reset_controller(void)
379 {
380 int i;
381
382 outb_p(4,HD_CMD);
383 for(i = 0; i < 1000; i++) nop();
384 outb_p(hd_info[0].ctl & 0x0f,HD_CMD);
385 for(i = 0; i < 1000; i++) nop();
386 if (drive_busy())
387 printk("hd: controller still busy\n");
388 else if ((hd_error = inb(HD_ERROR)) != 1)
389 printk("hd: controller reset failed: %02x\n",hd_error);
390 }
391
392 static void reset_hd(void)
393 {
394 static int i;
395
396 repeat:
397 if (reset) {
398 reset = 0;
399 i = -1;
400 reset_controller();
401 } else {
402 check_status();
403 if (reset)
404 goto repeat;
405 }
406 if (++i < NR_HD) {
407 special_op[i] = recalibrate[i] = 1;
408 if (unmask_intr[i]) {
409 unmask_intr[i] = DEFAULT_UNMASK_INTR;
410 printk("hd%c: reset irq-unmasking to %d\n",i+'a',
411 DEFAULT_UNMASK_INTR);
412 }
413 if (mult_req[i] || mult_count[i]) {
414 mult_count[i] = 0;
415 mult_req[i] = DEFAULT_MULT_COUNT;
416 printk("hd%c: reset multiple mode to %d\n",i+'a',
417 DEFAULT_MULT_COUNT);
418 }
419 hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
420 hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
421 if (reset)
422 goto repeat;
423 } else
424 hd_request();
425 }
426
427
428
429
430
431
432
433
434
435
436 void unexpected_hd_interrupt(void)
437 {
438 unsigned int stat = inb_p(HD_STATUS);
439
440 if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
441 dump_status ("unexpected interrupt", stat);
442 SET_TIMER;
443 }
444 }
445
446
447
448
449
450
451 static void bad_rw_intr(void)
452 {
453 int dev;
454
455 if (!CURRENT)
456 return;
457 dev = DEVICE_NR(CURRENT->dev);
458 if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
459 end_request(0);
460 special_op[dev] = recalibrate[dev] = 1;
461 } else if (CURRENT->errors % RESET_FREQ == 0)
462 reset = 1;
463 else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
464 special_op[dev] = recalibrate[dev] = 1;
465
466 }
467
468 static inline int wait_DRQ(void)
469 {
470 int retries = 100000, stat;
471
472 while (--retries > 0)
473 if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)
474 return 0;
475 dump_status("wait_DRQ", stat);
476 return -1;
477 }
478
479 static void read_intr(void)
480 {
481 unsigned int dev = DEVICE_NR(CURRENT->dev);
482 int i, retries = 100000, msect = mult_count[dev], nsect;
483
484 if (unmask_intr[dev])
485 sti();
486 do {
487 i = (unsigned) inb_p(HD_STATUS);
488 if (i & BUSY_STAT)
489 continue;
490 if (!OK_STATUS(i))
491 break;
492 if (i & DRQ_STAT)
493 goto ok_to_read;
494 } while (--retries > 0);
495 dump_status("read_intr", i);
496 bad_rw_intr();
497 hd_request();
498 return;
499 ok_to_read:
500 if (msect) {
501 if ((nsect = CURRENT->current_nr_sectors) > msect)
502 nsect = msect;
503 msect -= nsect;
504 } else
505 nsect = 1;
506 insw(HD_DATA,CURRENT->buffer,nsect<<8);
507 CURRENT->sector += nsect;
508 CURRENT->buffer += nsect<<9;
509 CURRENT->errors = 0;
510 i = (CURRENT->nr_sectors -= nsect);
511
512 #ifdef DEBUG
513 printk("hd%c: read: sectors(%ld-%ld), remaining=%ld, buffer=0x%08lx\n",
514 dev+'a', CURRENT->sector, CURRENT->sector+nsect,
515 CURRENT->nr_sectors, (unsigned long) CURRENT->buffer+(nsect<<9));
516 #endif
517 if ((CURRENT->current_nr_sectors -= nsect) <= 0)
518 end_request(1);
519 if (i > 0) {
520 if (msect)
521 goto ok_to_read;
522 SET_INTR(&read_intr);
523 return;
524 }
525 (void) inb_p(HD_STATUS);
526 #if (HD_DELAY > 0)
527 last_req = read_timer();
528 #endif
529 if (CURRENT)
530 hd_request();
531 return;
532 }
533
534 static inline void multwrite (unsigned int dev)
535 {
536 unsigned int mcount = mult_count[dev];
537
538 while (mcount--) {
539 outsw(HD_DATA,WCURRENT.buffer,256);
540 if (!--WCURRENT.nr_sectors)
541 return;
542 WCURRENT.buffer += 512;
543 if (!--WCURRENT.current_nr_sectors) {
544 WCURRENT.bh = WCURRENT.bh->b_reqnext;
545 if (WCURRENT.bh == NULL)
546 panic("buffer list corrupted\n");
547 WCURRENT.current_nr_sectors = WCURRENT.bh->b_size>>9;
548 WCURRENT.buffer = WCURRENT.bh->b_data;
549 }
550 }
551 }
552
553 static void multwrite_intr(void)
554 {
555 int i;
556 unsigned int dev = DEVICE_NR(WCURRENT.dev);
557
558 if (unmask_intr[dev])
559 sti();
560 if (OK_STATUS(i=inb_p(HD_STATUS))) {
561 if (i & DRQ_STAT) {
562 if (WCURRENT.nr_sectors) {
563 multwrite(dev);
564 SET_INTR(&multwrite_intr);
565 return;
566 }
567 } else {
568 if (!WCURRENT.nr_sectors) {
569 for (i = CURRENT->nr_sectors; i > 0;){
570 i -= CURRENT->current_nr_sectors;
571 end_request(1);
572 }
573 #if (HD_DELAY > 0)
574 last_req = read_timer();
575 #endif
576 if (CURRENT)
577 hd_request();
578 return;
579 }
580 }
581 }
582 dump_status("multwrite_intr", i);
583 bad_rw_intr();
584 hd_request();
585 }
586
587 static void write_intr(void)
588 {
589 int i;
590 int retries = 100000;
591
592 if (unmask_intr[DEVICE_NR(WCURRENT.dev)])
593 sti();
594 do {
595 i = (unsigned) inb_p(HD_STATUS);
596 if (i & BUSY_STAT)
597 continue;
598 if (!OK_STATUS(i))
599 break;
600 if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
601 goto ok_to_write;
602 } while (--retries > 0);
603 dump_status("write_intr", i);
604 bad_rw_intr();
605 hd_request();
606 return;
607 ok_to_write:
608 CURRENT->sector++;
609 i = --CURRENT->nr_sectors;
610 --CURRENT->current_nr_sectors;
611 CURRENT->buffer += 512;
612 if (!i || (CURRENT->bh && !SUBSECTOR(i)))
613 end_request(1);
614 if (i > 0) {
615 SET_INTR(&write_intr);
616 outsw(HD_DATA,CURRENT->buffer,256);
617 sti();
618 } else {
619 #if (HD_DELAY > 0)
620 last_req = read_timer();
621 #endif
622 hd_request();
623 }
624 return;
625 }
626
627 static void recal_intr(void)
628 {
629 check_status();
630 #if (HD_DELAY > 0)
631 last_req = read_timer();
632 #endif
633 hd_request();
634 }
635
636
637
638
639
640 static void hd_times_out(void)
641 {
642 unsigned int dev;
643
644 DEVICE_INTR = NULL;
645 if (!CURRENT)
646 return;
647 disable_irq(HD_IRQ);
648 sti();
649 reset = 1;
650 dev = DEVICE_NR(CURRENT->dev);
651 printk("hd%c: timeout\n", dev+'a');
652 if (++CURRENT->errors >= MAX_ERRORS) {
653 #ifdef DEBUG
654 printk("hd%c: too many errors\n", dev+'a');
655 #endif
656 end_request(0);
657 }
658 cli();
659 hd_request();
660 enable_irq(HD_IRQ);
661 }
662
663 int do_special_op (unsigned int dev)
664 {
665 if (recalibrate[dev]) {
666 recalibrate[dev] = 0;
667 hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
668 return reset;
669 }
670 if (!identified[dev]) {
671 identified[dev] = 1;
672 unmask_intr[dev] = DEFAULT_UNMASK_INTR;
673 mult_req[dev] = DEFAULT_MULT_COUNT;
674 hd_out(dev,0,0,0,0,WIN_IDENTIFY,&identify_intr);
675 return reset;
676 }
677 if (mult_req[dev] != mult_count[dev]) {
678 hd_out(dev,mult_req[dev],0,0,0,WIN_SETMULT,&set_multmode_intr);
679 return reset;
680 }
681 if (hd_info[dev].head > 16) {
682 printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
683 end_request(0);
684 }
685 special_op[dev] = 0;
686 return 1;
687 }
688
689
690
691
692
693
694
695
696
697
698
699
700
701 static void hd_request(void)
702 {
703 unsigned int dev, block, nsect, sec, track, head, cyl;
704
705 if (CURRENT && CURRENT->dev < 0) return;
706 if (DEVICE_INTR)
707 return;
708 repeat:
709 timer_active &= ~(1<<HD_TIMER);
710 sti();
711 INIT_REQUEST;
712 if (reset) {
713 cli();
714 reset_hd();
715 return;
716 }
717 dev = MINOR(CURRENT->dev);
718 block = CURRENT->sector;
719 nsect = CURRENT->nr_sectors;
720 if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {
721 #ifdef DEBUG
722 if (dev >= (NR_HD<<6))
723 printk("hd: bad minor number: device=0x%04x\n", CURRENT->dev);
724 else
725 printk("hd%c: bad access: block=%d, count=%d\n",
726 (CURRENT->dev>>6)+'a', block, nsect);
727 #endif
728 end_request(0);
729 goto repeat;
730 }
731 block += hd[dev].start_sect;
732 dev >>= 6;
733 if (special_op[dev]) {
734 if (do_special_op(dev))
735 goto repeat;
736 return;
737 }
738 sec = block % hd_info[dev].sect + 1;
739 track = block / hd_info[dev].sect;
740 head = track % hd_info[dev].head;
741 cyl = track / hd_info[dev].head;
742 #ifdef DEBUG
743 printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
744 dev+'a', (CURRENT->cmd == READ)?"read":"writ",
745 cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);
746 #endif
747 if (!unmask_intr[dev])
748 cli();
749 if (CURRENT->cmd == READ) {
750 unsigned int cmd = mult_count[dev] > 1 ? WIN_MULTREAD : WIN_READ;
751 hd_out(dev,nsect,sec,head,cyl,cmd,&read_intr);
752 if (reset)
753 goto repeat;
754 return;
755 }
756 if (CURRENT->cmd == WRITE) {
757 if (mult_count[dev])
758 hd_out(dev,nsect,sec,head,cyl,WIN_MULTWRITE,&multwrite_intr);
759 else
760 hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
761 if (reset)
762 goto repeat;
763 if (wait_DRQ()) {
764 bad_rw_intr();
765 goto repeat;
766 }
767 if (mult_count[dev]) {
768 WCURRENT = *CURRENT;
769 multwrite(dev);
770 } else
771 outsw(HD_DATA,CURRENT->buffer,256);
772 return;
773 }
774 panic("unknown hd-command");
775 }
776
777 static void do_hd_request (void)
778 {
779 disable_irq(HD_IRQ);
780 hd_request();
781 enable_irq(HD_IRQ);
782 }
783
784 static int hd_ioctl(struct inode * inode, struct file * file,
785 unsigned int cmd, unsigned long arg)
786 {
787 struct hd_geometry *loc = (struct hd_geometry *) arg;
788 int dev, err;
789 unsigned long flags;
790
791 if ((!inode) || (!inode->i_rdev))
792 return -EINVAL;
793 dev = DEVICE_NR(inode->i_rdev);
794 if (dev >= NR_HD)
795 return -EINVAL;
796 switch (cmd) {
797 case HDIO_GETGEO:
798 if (!loc) return -EINVAL;
799 err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
800 if (err)
801 return err;
802 put_fs_byte(bios_info[dev].head,
803 (char *) &loc->heads);
804 put_fs_byte(bios_info[dev].sect,
805 (char *) &loc->sectors);
806 put_fs_word(bios_info[dev].cyl,
807 (short *) &loc->cylinders);
808 put_fs_long(hd[MINOR(inode->i_rdev)].start_sect,
809 (long *) &loc->start);
810 return 0;
811 case BLKRASET:
812 if(!suser()) return -EACCES;
813 if(arg > 0xff) return -EINVAL;
814 read_ahead[MAJOR(inode->i_rdev)] = arg;
815 return 0;
816 case BLKRAGET:
817 if (!arg) return -EINVAL;
818 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
819 if (err)
820 return err;
821 put_fs_long(read_ahead[MAJOR(inode->i_rdev)],(long *) arg);
822 return 0;
823 case BLKGETSIZE:
824 if (!arg) return -EINVAL;
825 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
826 if (err)
827 return err;
828 put_fs_long(hd[MINOR(inode->i_rdev)].nr_sects, (long *) arg);
829 return 0;
830 case BLKFLSBUF:
831 if(!suser()) return -EACCES;
832 fsync_dev(inode->i_rdev);
833 invalidate_buffers(inode->i_rdev);
834 return 0;
835
836 case BLKRRPART:
837 return revalidate_hddisk(inode->i_rdev, 1);
838
839 case HDIO_SETUNMASKINTR:
840 printk("hd: obsolete syscall: HDIO_SETUNMASKINTR\n");
841 if (!arg) return -EINVAL;
842 err = verify_area(VERIFY_READ, (long *) arg, sizeof(long));
843 if (err)
844 return err;
845 arg = get_fs_long((long *) arg);
846
847 case HDIO_SET_UNMASKINTR:
848 if (!suser()) return -EACCES;
849 if ((arg > 1) || (MINOR(inode->i_rdev) & 0x3F))
850 return -EINVAL;
851 unmask_intr[dev] = arg;
852 return 0;
853
854 case HDIO_GET_UNMASKINTR:
855 if (!arg) return -EINVAL;
856 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
857 if (err)
858 return err;
859 put_fs_long(unmask_intr[dev], (long *) arg);
860 return 0;
861
862 case HDIO_GET_MULTCOUNT:
863 if (!arg) return -EINVAL;
864 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
865 if (err)
866 return err;
867 put_fs_long(mult_count[dev], (long *) arg);
868 return 0;
869
870 case HDIO_SETMULTCOUNT:
871 printk("hd: obsolete syscall: HDIO_SETMULTCOUNT\n");
872 if (!arg) return -EINVAL;
873 err = verify_area(VERIFY_READ, (long *) arg, sizeof(long));
874 if (err)
875 return err;
876 arg = get_fs_long((long *) arg);
877
878 case HDIO_SET_MULTCOUNT:
879 if (!suser()) return -EACCES;
880 if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
881 save_flags(flags);
882 cli();
883 if (arg > max_mult[dev])
884 err = -EINVAL;
885 else if (mult_req[dev] != mult_count[dev]) {
886 special_op[dev] = 1;
887 err = -EBUSY;
888 } else {
889 mult_req[dev] = arg;
890 special_op[dev] = 1;
891 err = 0;
892 }
893 restore_flags(flags);
894 return err;
895
896 case HDIO_GET_IDENTITY:
897 if (!arg) return -EINVAL;
898 if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
899 if (hd_ident_info[dev] == NULL) return -ENOMSG;
900 err = verify_area(VERIFY_WRITE, (char *) arg, sizeof(struct hd_driveid));
901 if (err)
902 return err;
903 memcpy_tofs((char *)arg, (char *) hd_ident_info[dev], sizeof(struct hd_driveid));
904 return 0;
905
906 RO_IOCTLS(inode->i_rdev,arg);
907 default:
908 return -EINVAL;
909 }
910 }
911
912 static int hd_open(struct inode * inode, struct file * filp)
913 {
914 int target;
915 target = DEVICE_NR(inode->i_rdev);
916
917 while (busy[target])
918 sleep_on(&busy_wait);
919 access_count[target]++;
920 return 0;
921 }
922
923
924
925
926
927 static void hd_release(struct inode * inode, struct file * file)
928 {
929 int target;
930 sync_dev(inode->i_rdev);
931
932 target = DEVICE_NR(inode->i_rdev);
933 access_count[target]--;
934
935 }
936
937 static void hd_geninit(void);
938
939 static struct gendisk hd_gendisk = {
940 MAJOR_NR,
941 "hd",
942 6,
943 1 << 6,
944 MAX_HD,
945 hd_geninit,
946 hd,
947 hd_sizes,
948 0,
949 (void *) bios_info,
950 NULL
951 };
952
953 static void hd_interrupt(int unused)
954 {
955 void (*handler)(void) = DEVICE_INTR;
956
957 DEVICE_INTR = NULL;
958 timer_active &= ~(1<<HD_TIMER);
959 if (!handler)
960 handler = unexpected_hd_interrupt;
961 handler();
962 sti();
963 }
964
965
966
967
968
969
970
971
972
973
974 static void hd_geninit(void)
975 {
976 int drive, i;
977 extern struct drive_info drive_info;
978 unsigned char *BIOS = (unsigned char *) &drive_info;
979 int cmos_disks;
980
981 if (!NR_HD) {
982 for (drive=0 ; drive<2 ; drive++) {
983 bios_info[drive].cyl = hd_info[drive].cyl = *(unsigned short *) BIOS;
984 bios_info[drive].head = hd_info[drive].head = *(2+BIOS);
985 bios_info[drive].wpcom = hd_info[drive].wpcom = *(unsigned short *) (5+BIOS);
986 bios_info[drive].ctl = hd_info[drive].ctl = *(8+BIOS);
987 bios_info[drive].lzone = hd_info[drive].lzone = *(unsigned short *) (12+BIOS);
988 bios_info[drive].sect = hd_info[drive].sect = *(14+BIOS);
989 #ifdef does_not_work_for_everybody_with_scsi_but_helps_ibm_vp
990 if (hd_info[drive].cyl && NR_HD == drive)
991 NR_HD++;
992 #endif
993 BIOS += 16;
994 }
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 if ((cmos_disks = CMOS_READ(0x12)) & 0xf0)
1019 if (cmos_disks & 0x0f)
1020 NR_HD = 2;
1021 else
1022 NR_HD = 1;
1023 }
1024 i = NR_HD;
1025 while (i-- > 0) {
1026
1027
1028
1029
1030
1031
1032
1033
1034 hd[i<<6].nr_sects = bios_info[i].head *
1035 bios_info[i].sect * bios_info[i].cyl;
1036 hd_ident_info[i] = (struct hd_driveid *) kmalloc(512,GFP_KERNEL);
1037 special_op[i] = 1;
1038 }
1039 if (NR_HD) {
1040 if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd")) {
1041 printk("hd: unable to get IRQ%d for the harddisk driver\n",HD_IRQ);
1042 NR_HD = 0;
1043 }
1044 }
1045 hd_gendisk.nr_real = NR_HD;
1046
1047 for(i=0;i<(MAX_HD << 6);i++) hd_blocksizes[i] = 1024;
1048 blksize_size[MAJOR_NR] = hd_blocksizes;
1049 }
1050
1051 static struct file_operations hd_fops = {
1052 NULL,
1053 block_read,
1054 block_write,
1055 NULL,
1056 NULL,
1057 hd_ioctl,
1058 NULL,
1059 hd_open,
1060 hd_release,
1061 block_fsync
1062 };
1063
1064 unsigned long hd_init(unsigned long mem_start, unsigned long mem_end)
1065 {
1066 if (register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
1067 printk("hd: unable to get major %d for harddisk\n",MAJOR_NR);
1068 return mem_start;
1069 }
1070 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1071 read_ahead[MAJOR_NR] = 8;
1072 hd_gendisk.next = gendisk_head;
1073 gendisk_head = &hd_gendisk;
1074 timer_table[HD_TIMER].fn = hd_times_out;
1075 return mem_start;
1076 }
1077
1078 #define DEVICE_BUSY busy[target]
1079 #define USAGE access_count[target]
1080 #define CAPACITY (bios_info[target].head*bios_info[target].sect*bios_info[target].cyl)
1081
1082
1083 #undef MAYBE_REINIT
1084 #define GENDISK_STRUCT hd_gendisk
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 static int revalidate_hddisk(int dev, int maxusage)
1095 {
1096 int target, major;
1097 struct gendisk * gdev;
1098 int max_p;
1099 int start;
1100 int i;
1101 long flags;
1102
1103 target = DEVICE_NR(dev);
1104 gdev = &GENDISK_STRUCT;
1105
1106 save_flags(flags);
1107 cli();
1108 if (DEVICE_BUSY || USAGE > maxusage) {
1109 restore_flags(flags);
1110 return -EBUSY;
1111 };
1112 DEVICE_BUSY = 1;
1113 restore_flags(flags);
1114
1115 max_p = gdev->max_p;
1116 start = target << gdev->minor_shift;
1117 major = MAJOR_NR << 8;
1118
1119 for (i=max_p - 1; i >=0 ; i--) {
1120 sync_dev(major | start | i);
1121 invalidate_inodes(major | start | i);
1122 invalidate_buffers(major | start | i);
1123 gdev->part[start+i].start_sect = 0;
1124 gdev->part[start+i].nr_sects = 0;
1125 };
1126
1127 #ifdef MAYBE_REINIT
1128 MAYBE_REINIT;
1129 #endif
1130
1131 gdev->part[start].nr_sects = CAPACITY;
1132 resetup_one_dev(gdev, target);
1133
1134 DEVICE_BUSY = 0;
1135 wake_up(&busy_wait);
1136 return 0;
1137 }
1138