More meth updates.
[linux-2.6/linux-mips.git] / drivers / ide / ide-disk.c
blob302d22d8dbeacc413c05b6e4b122161ab1f1c406
1 /*
2 * linux/drivers/ide/ide-disk.c Version 1.18 Mar 05, 2003
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 * Copyright (C) 1998-2002 Linux ATA Development
6 * Andre Hedrick <andre@linux-ide.org>
7 * Copyright (C) 2003 Red Hat <alan@redhat.com>
8 */
11 * Mostly written by Mark Lord <mlord@pobox.com>
12 * and Gadi Oxman <gadio@netvision.net.il>
13 * and Andre Hedrick <andre@linux-ide.org>
15 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
17 * Version 1.00 move disk only code from ide.c to ide-disk.c
18 * support optional byte-swapping of all data
19 * Version 1.01 fix previous byte-swapping code
20 * Version 1.02 remove ", LBA" from drive identification msgs
21 * Version 1.03 fix display of id->buf_size for big-endian
22 * Version 1.04 add /proc configurable settings and S.M.A.R.T support
23 * Version 1.05 add capacity support for ATA3 >= 8GB
24 * Version 1.06 get boot-up messages to show full cyl count
25 * Version 1.07 disable door-locking if it fails
26 * Version 1.08 fixed CHS/LBA translations for ATA4 > 8GB,
27 * process of adding new ATA4 compliance.
28 * fixed problems in allowing fdisk to see
29 * the entire disk.
30 * Version 1.09 added increment of rq->sector in ide_multwrite
31 * added UDMA 3/4 reporting
32 * Version 1.10 request queue changes, Ultra DMA 100
33 * Version 1.11 added 48-bit lba
34 * Version 1.12 adding taskfile io access method
35 * Version 1.13 added standby and flush-cache for notifier
36 * Version 1.14 added acoustic-wcache
37 * Version 1.15 convert all calls to ide_raw_taskfile
38 * since args will return register content.
39 * Version 1.16 added suspend-resume-checkpower
40 * Version 1.17 do flush on standy, do flush on ATA < ATA6
41 * fix wcache setup.
44 #define IDEDISK_VERSION "1.18"
46 #undef REALLY_SLOW_IO /* most systems can safely undef this */
48 #include <linux/config.h>
49 #include <linux/module.h>
50 #include <linux/types.h>
51 #include <linux/string.h>
52 #include <linux/kernel.h>
53 #include <linux/timer.h>
54 #include <linux/mm.h>
55 #include <linux/interrupt.h>
56 #include <linux/major.h>
57 #include <linux/errno.h>
58 #include <linux/genhd.h>
59 #include <linux/slab.h>
60 #include <linux/delay.h>
62 #define _IDE_DISK
64 #include <linux/ide.h>
66 #include <asm/byteorder.h>
67 #include <asm/irq.h>
68 #include <asm/uaccess.h>
69 #include <asm/io.h>
71 /* FIXME: some day we shouldn't need to look in here! */
73 #include "legacy/pdc4030.h"
76 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
77 * value for this drive (from its reported identification information).
79 * Returns: 1 if lba_capacity looks sensible
80 * 0 otherwise
82 * It is called only once for each drive.
84 static int lba_capacity_is_ok (struct hd_driveid *id)
86 unsigned long lba_sects, chs_sects, head, tail;
88 if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
89 printk("48-bit Drive: %llu \n", id->lba_capacity_2);
90 return 1;
94 * The ATA spec tells large drives to return
95 * C/H/S = 16383/16/63 independent of their size.
96 * Some drives can be jumpered to use 15 heads instead of 16.
97 * Some drives can be jumpered to use 4092 cyls instead of 16383.
99 if ((id->cyls == 16383
100 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
101 id->sectors == 63 &&
102 (id->heads == 15 || id->heads == 16) &&
103 (id->lba_capacity >= 16383*63*id->heads))
104 return 1;
106 lba_sects = id->lba_capacity;
107 chs_sects = id->cyls * id->heads * id->sectors;
109 /* perform a rough sanity check on lba_sects: within 10% is OK */
110 if ((lba_sects - chs_sects) < chs_sects/10)
111 return 1;
113 /* some drives have the word order reversed */
114 head = ((lba_sects >> 16) & 0xffff);
115 tail = (lba_sects & 0xffff);
116 lba_sects = (head | (tail << 16));
117 if ((lba_sects - chs_sects) < chs_sects/10) {
118 id->lba_capacity = lba_sects;
119 return 1; /* lba_capacity is (now) good */
122 return 0; /* lba_capacity value may be bad */
125 static int idedisk_start_tag(ide_drive_t *drive, struct request *rq)
127 unsigned long flags;
128 int ret = 1;
130 spin_lock_irqsave(&ide_lock, flags);
132 if (ata_pending_commands(drive) < drive->queue_depth)
133 ret = blk_queue_start_tag(&drive->queue, rq);
135 spin_unlock_irqrestore(&ide_lock, flags);
136 return ret;
139 #ifndef CONFIG_IDE_TASKFILE_IO
142 * read_intr() is the handler for disk read/multread interrupts
144 static ide_startstop_t read_intr (ide_drive_t *drive)
146 ide_hwif_t *hwif = HWIF(drive);
147 u32 i = 0, nsect = 0, msect = drive->mult_count;
148 struct request *rq;
149 unsigned long flags;
150 u8 stat;
151 char *to;
153 /* new way for dealing with premature shared PCI interrupts */
154 if (!OK_STAT(stat=hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
155 if (stat & (ERR_STAT|DRQ_STAT)) {
156 return DRIVER(drive)->error(drive, "read_intr", stat);
158 /* no data yet, so wait for another interrupt */
159 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
160 return ide_started;
163 read_next:
164 rq = HWGROUP(drive)->rq;
165 if (msect) {
166 if ((nsect = rq->current_nr_sectors) > msect)
167 nsect = msect;
168 msect -= nsect;
169 } else
170 nsect = 1;
171 to = ide_map_buffer(rq, &flags);
172 taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
173 #ifdef DEBUG
174 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
175 drive->name, rq->sector, rq->sector+nsect-1,
176 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
177 #endif
178 ide_unmap_buffer(rq, to, &flags);
179 rq->sector += nsect;
180 rq->errors = 0;
181 i = (rq->nr_sectors -= nsect);
182 if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
183 ide_end_request(drive, 1, rq->hard_cur_sectors);
185 * Another BH Page walker and DATA INTEGRITY Questioned on ERROR.
186 * If passed back up on multimode read, BAD DATA could be ACKED
187 * to FILE SYSTEMS above ...
189 if (i > 0) {
190 if (msect)
191 goto read_next;
192 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
193 return ide_started;
195 return ide_stopped;
199 * write_intr() is the handler for disk write interrupts
201 static ide_startstop_t write_intr (ide_drive_t *drive)
203 ide_hwgroup_t *hwgroup = HWGROUP(drive);
204 ide_hwif_t *hwif = HWIF(drive);
205 struct request *rq = hwgroup->rq;
206 u32 i = 0;
207 u8 stat;
209 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
210 DRIVE_READY, drive->bad_wstat)) {
211 printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n",
212 drive->name, rq->nr_sectors, stat);
213 } else {
214 #ifdef DEBUG
215 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
216 drive->name, rq->sector, (unsigned long) rq->buffer,
217 rq->nr_sectors-1);
218 #endif
219 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
220 rq->sector++;
221 rq->errors = 0;
222 i = --rq->nr_sectors;
223 --rq->current_nr_sectors;
224 if (((long)rq->current_nr_sectors) <= 0)
225 ide_end_request(drive, 1, rq->hard_cur_sectors);
226 if (i > 0) {
227 unsigned long flags;
228 char *to = ide_map_buffer(rq, &flags);
229 taskfile_output_data(drive, to, SECTOR_WORDS);
230 ide_unmap_buffer(rq, to, &flags);
231 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
232 return ide_started;
234 return ide_stopped;
236 /* the original code did this here (?) */
237 return ide_stopped;
239 return DRIVER(drive)->error(drive, "write_intr", stat);
243 * ide_multwrite() transfers a block of up to mcount sectors of data
244 * to a drive as part of a disk multiple-sector write operation.
246 * Returns 0 on success.
248 * Note that we may be called from two contexts - __ide_do_rw_disk() context
249 * and IRQ context. The IRQ can happen any time after we've output the
250 * full "mcount" number of sectors, so we must make sure we update the
251 * state _before_ we output the final part of the data!
253 * The update and return to BH is a BLOCK Layer Fakey to get more data
254 * to satisfy the hardware atomic segment. If the hardware atomic segment
255 * is shorter or smaller than the BH segment then we should be OKAY.
256 * This is only valid if we can rewind the rq->current_nr_sectors counter.
258 int ide_multwrite (ide_drive_t *drive, unsigned int mcount)
260 ide_hwgroup_t *hwgroup = HWGROUP(drive);
261 struct request *rq = &hwgroup->wrq;
263 do {
264 char *buffer;
265 int nsect = rq->current_nr_sectors;
266 unsigned long flags;
268 if (nsect > mcount)
269 nsect = mcount;
270 mcount -= nsect;
271 buffer = ide_map_buffer(rq, &flags);
273 rq->sector += nsect;
274 rq->nr_sectors -= nsect;
275 rq->current_nr_sectors -= nsect;
277 /* Do we move to the next bh after this? */
278 if (!rq->current_nr_sectors) {
279 struct bio *bio = rq->bio;
282 * only move to next bio, when we have processed
283 * all bvecs in this one.
285 if (++bio->bi_idx >= bio->bi_vcnt) {
286 bio->bi_idx = 0;
287 bio = bio->bi_next;
290 /* end early early we ran out of requests */
291 if (!bio) {
292 mcount = 0;
293 } else {
294 rq->bio = bio;
295 rq->current_nr_sectors = bio_iovec(bio)->bv_len >> 9;
296 rq->hard_cur_sectors = rq->current_nr_sectors;
301 * Ok, we're all setup for the interrupt
302 * re-entering us on the last transfer.
304 taskfile_output_data(drive, buffer, nsect<<7);
305 ide_unmap_buffer(rq, buffer, &flags);
306 } while (mcount);
308 return 0;
312 * multwrite_intr() is the handler for disk multwrite interrupts
314 static ide_startstop_t multwrite_intr (ide_drive_t *drive)
316 ide_hwgroup_t *hwgroup = HWGROUP(drive);
317 ide_hwif_t *hwif = HWIF(drive);
318 struct request *rq = &hwgroup->wrq;
319 u8 stat;
321 stat = hwif->INB(IDE_STATUS_REG);
322 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
323 if (stat & DRQ_STAT) {
325 * The drive wants data. Remember rq is the copy
326 * of the request
328 if (rq->nr_sectors) {
329 if (ide_multwrite(drive, drive->mult_count))
330 return ide_stopped;
331 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
332 return ide_started;
334 } else {
336 * If the copy has all the blocks completed then
337 * we can end the original request.
339 if (!rq->nr_sectors) { /* all done? */
340 rq = hwgroup->rq;
341 ide_end_request(drive, 1, rq->nr_sectors);
342 return ide_stopped;
345 /* the original code did this here (?) */
346 return ide_stopped;
348 return DRIVER(drive)->error(drive, "multwrite_intr", stat);
352 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
353 * using LBA if supported, or CHS otherwise, to address sectors.
354 * It also takes care of issuing special DRIVE_CMDs.
356 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
358 ide_hwif_t *hwif = HWIF(drive);
359 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
360 task_ioreg_t command = WIN_NOP;
361 ata_nsector_t nsectors;
363 nsectors.all = (u16) rq->nr_sectors;
365 if (drive->using_tcq && idedisk_start_tag(drive, rq)) {
366 if (!ata_pending_commands(drive))
367 BUG();
369 return ide_started;
372 if (IDE_CONTROL_REG)
373 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
375 if (drive->select.b.lba) {
376 if (drive->addressing == 1) {
377 task_ioreg_t tasklets[10];
379 if (blk_rq_tagged(rq)) {
380 tasklets[0] = nsectors.b.low;
381 tasklets[1] = nsectors.b.high;
382 tasklets[2] = rq->tag << 3;
383 tasklets[3] = 0;
384 } else {
385 tasklets[0] = 0;
386 tasklets[1] = 0;
387 tasklets[2] = nsectors.b.low;
388 tasklets[3] = nsectors.b.high;
391 tasklets[4] = (task_ioreg_t) block;
392 tasklets[5] = (task_ioreg_t) (block>>8);
393 tasklets[6] = (task_ioreg_t) (block>>16);
394 tasklets[7] = (task_ioreg_t) (block>>24);
395 if (sizeof(block) == 4) {
396 tasklets[8] = (task_ioreg_t) 0;
397 tasklets[9] = (task_ioreg_t) 0;
398 } else {
399 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
400 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
402 #ifdef DEBUG
403 printk("%s: %sing: LBAsect=%lu, sectors=%ld, "
404 "buffer=0x%08lx, LBAsect=0x%012lx\n",
405 drive->name,
406 rq_data_dir(rq)==READ?"read":"writ",
407 block,
408 rq->nr_sectors,
409 (unsigned long) rq->buffer,
410 block);
411 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
412 drive->name, tasklets[3], tasklets[2],
413 tasklets[9], tasklets[8], tasklets[7],
414 tasklets[6], tasklets[5], tasklets[4]);
415 #endif
416 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
417 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
418 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
419 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
420 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
422 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
423 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
424 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
425 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
426 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
427 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
428 } else {
429 #ifdef DEBUG
430 printk("%s: %sing: LBAsect=%llu, sectors=%ld, "
431 "buffer=0x%08lx\n",
432 drive->name,
433 rq_data_dir(rq)==READ?"read":"writ",
434 (unsigned long long)block, rq->nr_sectors,
435 (unsigned long) rq->buffer);
436 #endif
437 if (blk_rq_tagged(rq)) {
438 hwif->OUTB(nsectors.b.low, IDE_FEATURE_REG);
439 hwif->OUTB(rq->tag << 3, IDE_NSECTOR_REG);
440 } else {
441 hwif->OUTB(0x00, IDE_FEATURE_REG);
442 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
445 hwif->OUTB(block, IDE_SECTOR_REG);
446 hwif->OUTB(block>>=8, IDE_LCYL_REG);
447 hwif->OUTB(block>>=8, IDE_HCYL_REG);
448 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
450 } else {
451 unsigned int sect,head,cyl,track;
452 track = (int)block / drive->sect;
453 sect = (int)block % drive->sect + 1;
454 hwif->OUTB(sect, IDE_SECTOR_REG);
455 head = track % drive->head;
456 cyl = track / drive->head;
458 if (blk_rq_tagged(rq)) {
459 hwif->OUTB(nsectors.b.low, IDE_FEATURE_REG);
460 hwif->OUTB(rq->tag << 3, IDE_NSECTOR_REG);
461 } else {
462 hwif->OUTB(0x00, IDE_FEATURE_REG);
463 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
466 hwif->OUTB(cyl, IDE_LCYL_REG);
467 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
468 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
469 #ifdef DEBUG
470 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
471 drive->name, rq_data_dir(rq)==READ?"read":"writ", cyl,
472 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
473 #endif
476 if (rq_data_dir(rq) == READ) {
477 if (blk_rq_tagged(rq))
478 return hwif->ide_dma_queued_read(drive);
480 if (drive->using_dma && !hwif->ide_dma_read(drive))
481 return ide_started;
483 command = ((drive->mult_count) ?
484 ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
485 ((lba48) ? WIN_READ_EXT : WIN_READ));
486 ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
487 return ide_started;
488 } else if (rq_data_dir(rq) == WRITE) {
489 ide_startstop_t startstop;
491 if (blk_rq_tagged(rq))
492 return hwif->ide_dma_queued_write(drive);
494 if (drive->using_dma && !(HWIF(drive)->ide_dma_write(drive)))
495 return ide_started;
497 command = ((drive->mult_count) ?
498 ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
499 ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
500 hwif->OUTB(command, IDE_COMMAND_REG);
502 if (ide_wait_stat(&startstop, drive, DATA_READY,
503 drive->bad_wstat, WAIT_DRQ)) {
504 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
505 drive->name,
506 drive->mult_count ? "MULTWRITE" : "WRITE");
507 return startstop;
509 if (!drive->unmask)
510 local_irq_disable();
511 if (drive->mult_count) {
512 ide_hwgroup_t *hwgroup = HWGROUP(drive);
514 * Ugh.. this part looks ugly because we MUST set up
515 * the interrupt handler before outputting the first block
516 * of data to be written. If we hit an error (corrupted buffer list)
517 * in ide_multwrite(), then we need to remove the handler/timer
518 * before returning. Fortunately, this NEVER happens (right?).
520 * Except when you get an error it seems...
522 * MAJOR DATA INTEGRITY BUG !!! only if we error
524 hwgroup->wrq = *rq; /* scratchpad */
525 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
526 if (ide_multwrite(drive, drive->mult_count)) {
527 unsigned long flags;
528 spin_lock_irqsave(&ide_lock, flags);
529 hwgroup->handler = NULL;
530 del_timer(&hwgroup->timer);
531 spin_unlock_irqrestore(&ide_lock, flags);
532 return ide_stopped;
534 } else {
535 unsigned long flags;
536 char *to = ide_map_buffer(rq, &flags);
537 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
538 taskfile_output_data(drive, to, SECTOR_WORDS);
539 ide_unmap_buffer(rq, to, &flags);
541 return ide_started;
543 blk_dump_rq_flags(rq, "__ide_do_rw_disk - bad command");
544 ide_end_request(drive, 0, 0);
545 return ide_stopped;
547 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
549 #else /* CONFIG_IDE_TASKFILE_IO */
551 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
552 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
553 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
556 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
557 * using LBA if supported, or CHS otherwise, to address sectors.
558 * It also takes care of issuing special DRIVE_CMDs.
560 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
562 BUG_ON(drive->blocked);
563 if (!blk_fs_request(rq)) {
564 blk_dump_rq_flags(rq, "__ide_do_rw_disk - bad command");
565 ide_end_request(drive, 0, 0);
566 return ide_stopped;
570 * 268435455 == 137439 MB or 28bit limit
572 * need to add split taskfile operations based on 28bit threshold.
575 if (drive->using_tcq && idedisk_start_tag(drive, rq)) {
576 if (!ata_pending_commands(drive))
577 BUG();
579 return ide_started;
582 if (drive->addressing == 1) /* 48-bit LBA */
583 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
584 if (drive->select.b.lba) /* 28-bit LBA */
585 return lba_28_rw_disk(drive, rq, (unsigned long) block);
587 /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
588 return chs_rw_disk(drive, rq, (unsigned long) block);
590 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
592 static task_ioreg_t get_command (ide_drive_t *drive, int cmd)
594 int lba48bit = (drive->addressing == 1) ? 1 : 0;
596 if ((cmd == READ) && drive->using_tcq)
597 return lba48bit ? WIN_READDMA_QUEUED_EXT : WIN_READDMA_QUEUED;
598 if ((cmd == READ) && (drive->using_dma))
599 return (lba48bit) ? WIN_READDMA_EXT : WIN_READDMA;
600 else if ((cmd == READ) && (drive->mult_count))
601 return (lba48bit) ? WIN_MULTREAD_EXT : WIN_MULTREAD;
602 else if (cmd == READ)
603 return (lba48bit) ? WIN_READ_EXT : WIN_READ;
604 else if ((cmd == WRITE) && drive->using_tcq)
605 return lba48bit ? WIN_WRITEDMA_QUEUED_EXT : WIN_WRITEDMA_QUEUED;
606 else if ((cmd == WRITE) && (drive->using_dma))
607 return (lba48bit) ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
608 else if ((cmd == WRITE) && (drive->mult_count))
609 return (lba48bit) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
610 else if (cmd == WRITE)
611 return (lba48bit) ? WIN_WRITE_EXT : WIN_WRITE;
612 else
613 return WIN_NOP;
616 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
618 ide_task_t args;
619 int sectors;
620 ata_nsector_t nsectors;
621 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
622 unsigned int track = (block / drive->sect);
623 unsigned int sect = (block % drive->sect) + 1;
624 unsigned int head = (track % drive->head);
625 unsigned int cyl = (track / drive->head);
627 nsectors.all = (u16) rq->nr_sectors;
629 #ifdef DEBUG
630 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
631 printk("CHS=%d/%d/%d, ", cyl, head, sect);
632 printk("sectors=%ld, ", rq->nr_sectors);
633 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
634 #endif
636 memset(&args, 0, sizeof(ide_task_t));
638 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
640 if (blk_rq_tagged(rq)) {
641 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
642 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
643 } else
644 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
646 args.tfRegister[IDE_SECTOR_OFFSET] = sect;
647 args.tfRegister[IDE_LCYL_OFFSET] = cyl;
648 args.tfRegister[IDE_HCYL_OFFSET] = (cyl>>8);
649 args.tfRegister[IDE_SELECT_OFFSET] = head;
650 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
651 args.tfRegister[IDE_COMMAND_OFFSET] = command;
652 args.command_type = ide_cmd_type_parser(&args);
653 args.rq = (struct request *) rq;
654 rq->special = (ide_task_t *)&args;
655 return do_rw_taskfile(drive, &args);
658 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
660 ide_task_t args;
661 int sectors;
662 ata_nsector_t nsectors;
663 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
665 nsectors.all = (u16) rq->nr_sectors;
667 #ifdef DEBUG
668 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
669 printk("LBAsect=%lld, ", block);
670 printk("sectors=%ld, ", rq->nr_sectors);
671 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
672 #endif
674 memset(&args, 0, sizeof(ide_task_t));
676 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
678 if (blk_rq_tagged(rq)) {
679 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
680 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
681 } else
682 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
684 args.tfRegister[IDE_SECTOR_OFFSET] = block;
685 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8);
686 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8);
687 args.tfRegister[IDE_SELECT_OFFSET] = ((block>>8)&0x0f);
688 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
689 args.tfRegister[IDE_COMMAND_OFFSET] = command;
690 args.command_type = ide_cmd_type_parser(&args);
691 args.rq = (struct request *) rq;
692 rq->special = (ide_task_t *)&args;
693 return do_rw_taskfile(drive, &args);
697 * 268435455 == 137439 MB or 28bit limit
698 * 320173056 == 163929 MB or 48bit addressing
699 * 1073741822 == 549756 MB or 48bit addressing fake drive
702 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
704 ide_task_t args;
705 int sectors;
706 ata_nsector_t nsectors;
707 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
709 nsectors.all = (u16) rq->nr_sectors;
711 #ifdef DEBUG
712 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
713 printk("LBAsect=%lld, ", block);
714 printk("sectors=%ld, ", rq->nr_sectors);
715 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
716 #endif
718 memset(&args, 0, sizeof(ide_task_t));
720 sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
722 if (blk_rq_tagged(rq)) {
723 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
724 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
725 args.hobRegister[IDE_FEATURE_OFFSET_HOB] = sectors >> 8;
726 args.hobRegister[IDE_NSECTOR_OFFSET_HOB] = 0;
727 } else {
728 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
729 args.hobRegister[IDE_NSECTOR_OFFSET_HOB] = sectors >> 8;
732 args.tfRegister[IDE_SECTOR_OFFSET] = block; /* low lba */
733 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
734 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
735 args.tfRegister[IDE_SELECT_OFFSET] = drive->select.all;
736 args.tfRegister[IDE_COMMAND_OFFSET] = command;
737 args.hobRegister[IDE_SECTOR_OFFSET_HOB] = (block>>=8); /* low lba */
738 args.hobRegister[IDE_LCYL_OFFSET_HOB] = (block>>=8); /* mid lba */
739 args.hobRegister[IDE_HCYL_OFFSET_HOB] = (block>>=8); /* hi lba */
740 args.hobRegister[IDE_SELECT_OFFSET_HOB] = drive->select.all;
741 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
742 args.command_type = ide_cmd_type_parser(&args);
743 args.rq = (struct request *) rq;
744 rq->special = (ide_task_t *)&args;
745 return do_rw_taskfile(drive, &args);
748 #endif /* CONFIG_IDE_TASKFILE_IO */
750 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
752 ide_hwif_t *hwif = HWIF(drive);
754 if (hwif->rw_disk)
755 return hwif->rw_disk(drive, rq, block);
756 else
757 return __ide_do_rw_disk(drive, rq, block);
760 static int do_idedisk_flushcache(ide_drive_t *drive);
762 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
764 ide_hwif_t *hwif = HWIF(drive);
765 unsigned long flags;
766 u8 err = 0;
768 local_irq_set(flags);
769 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
770 #if FANCY_STATUS_DUMPS
771 printk(" { ");
772 if (stat & BUSY_STAT)
773 printk("Busy ");
774 else {
775 if (stat & READY_STAT) printk("DriveReady ");
776 if (stat & WRERR_STAT) printk("DeviceFault ");
777 if (stat & SEEK_STAT) printk("SeekComplete ");
778 if (stat & DRQ_STAT) printk("DataRequest ");
779 if (stat & ECC_STAT) printk("CorrectedError ");
780 if (stat & INDEX_STAT) printk("Index ");
781 if (stat & ERR_STAT) printk("Error ");
783 printk("}");
784 #endif /* FANCY_STATUS_DUMPS */
785 printk("\n");
786 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
787 err = hwif->INB(IDE_ERROR_REG);
788 printk("%s: %s: error=0x%02x", drive->name, msg, err);
789 #if FANCY_STATUS_DUMPS
790 printk(" { ");
791 if (err & ABRT_ERR) printk("DriveStatusError ");
792 if (err & ICRC_ERR)
793 printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
794 if (err & ECC_ERR) printk("UncorrectableError ");
795 if (err & ID_ERR) printk("SectorIdNotFound ");
796 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
797 if (err & MARK_ERR) printk("AddrMarkNotFound ");
798 printk("}");
799 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
800 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
801 if (drive->addressing == 1) {
802 __u64 sectors = 0;
803 u32 low = 0, high = 0;
804 low = ide_read_24(drive);
805 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
806 high = ide_read_24(drive);
807 sectors = ((__u64)high << 24) | low;
808 printk(", LBAsect=%llu, high=%d, low=%d",
809 (unsigned long long) sectors,
810 high, low);
811 } else {
812 u8 cur = hwif->INB(IDE_SELECT_REG);
813 if (cur & 0x40) { /* using LBA? */
814 printk(", LBAsect=%ld", (unsigned long)
815 ((cur&0xf)<<24)
816 |(hwif->INB(IDE_HCYL_REG)<<16)
817 |(hwif->INB(IDE_LCYL_REG)<<8)
818 | hwif->INB(IDE_SECTOR_REG));
819 } else {
820 printk(", CHS=%d/%d/%d",
821 (hwif->INB(IDE_HCYL_REG)<<8) +
822 hwif->INB(IDE_LCYL_REG),
823 cur & 0xf,
824 hwif->INB(IDE_SECTOR_REG));
827 if (HWGROUP(drive) && HWGROUP(drive)->rq)
828 printk(", sector=%llu",
829 (unsigned long long)HWGROUP(drive)->rq->sector);
832 #endif /* FANCY_STATUS_DUMPS */
833 printk("\n");
834 local_irq_restore(flags);
835 return err;
838 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
840 ide_hwif_t *hwif;
841 struct request *rq;
842 u8 err;
843 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
845 err = idedisk_dump_status(drive, msg, stat);
847 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
848 return ide_stopped;
850 hwif = HWIF(drive);
851 /* retry only "normal" I/O: */
852 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
853 rq->errors = 1;
854 ide_end_drive_cmd(drive, stat, err);
855 return ide_stopped;
857 #if 0
858 else if (rq->flags & REQ_DRIVE_TASKFILE) {
859 rq->errors = 1;
860 ide_end_taskfile(drive, stat, err);
861 return ide_stopped;
863 #endif
864 #ifdef CONFIG_IDE_TASKFILE_IO
865 /* make rq completion pointers new submission pointers */
866 blk_rq_prep_restart(rq);
867 #endif
869 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
870 /* other bits are useless when BUSY */
871 rq->errors |= ERROR_RESET;
872 } else if (stat & ERR_STAT) {
873 /* err has different meaning on cdrom and tape */
874 if (err == ABRT_ERR) {
875 if (drive->select.b.lba &&
876 /* some newer drives don't support WIN_SPECIFY */
877 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
878 return ide_stopped;
879 } else if ((err & BAD_CRC) == BAD_CRC) {
880 /* UDMA crc error, just retry the operation */
881 drive->crc_count++;
882 } else if (err & (BBD_ERR | ECC_ERR)) {
883 /* retries won't help these */
884 rq->errors = ERROR_MAX;
885 } else if (err & TRK0_ERR) {
886 /* help it find track zero */
887 rq->errors |= ERROR_RECAL;
890 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ) {
892 * try_to_flush_leftover_data() is invoked in response to
893 * a drive unexpectedly having its DRQ_STAT bit set. As
894 * an alternative to resetting the drive, this routine
895 * tries to clear the condition by read a sector's worth
896 * of data from the drive. Of course, this may not help
897 * if the drive is *waiting* for data from *us*.
899 while (i > 0) {
900 u32 buffer[16];
901 unsigned int wcount = (i > 16) ? 16 : i;
902 i -= wcount;
903 taskfile_input_data(drive, buffer, wcount);
906 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
907 /* force an abort */
908 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
910 if (rq->errors >= ERROR_MAX)
911 DRIVER(drive)->end_request(drive, 0, 0);
912 else {
913 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
914 ++rq->errors;
915 return ide_do_reset(drive);
917 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
918 drive->special.b.recalibrate = 1;
919 ++rq->errors;
921 return ide_stopped;
924 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
926 ide_hwif_t *hwif;
927 struct request *rq;
929 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
930 return ide_stopped;
932 hwif = HWIF(drive);
934 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
935 rq->errors = 1;
936 ide_end_drive_cmd(drive, BUSY_STAT, 0);
937 return ide_stopped;
940 DRIVER(drive)->end_request(drive, 0, 0);
941 return ide_stopped;
945 * Queries for true maximum capacity of the drive.
946 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
948 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
950 ide_task_t args;
951 unsigned long addr = 0;
953 /* Create IDE/ATA command request structure */
954 memset(&args, 0, sizeof(ide_task_t));
955 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
956 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
957 args.command_type = ide_cmd_type_parser(&args);
958 /* submit command request */
959 ide_raw_taskfile(drive, &args, NULL);
961 /* if OK, compute maximum address value */
962 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
963 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
964 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
965 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
966 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
968 addr++; /* since the return value is (maxlba - 1), we add 1 */
969 return addr;
972 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
974 ide_task_t args;
975 unsigned long long addr = 0;
977 /* Create IDE/ATA command request structure */
978 memset(&args, 0, sizeof(ide_task_t));
980 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
981 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
982 args.command_type = ide_cmd_type_parser(&args);
983 /* submit command request */
984 ide_raw_taskfile(drive, &args, NULL);
986 /* if OK, compute maximum address value */
987 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
988 u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
989 ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
990 (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
991 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
992 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
993 (args.tfRegister[IDE_SECTOR_OFFSET]);
994 addr = ((__u64)high << 24) | low;
996 addr++; /* since the return value is (maxlba - 1), we add 1 */
997 return addr;
1000 #ifdef CONFIG_IDEDISK_STROKE
1002 * Sets maximum virtual LBA address of the drive.
1003 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
1005 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
1007 ide_task_t args;
1008 unsigned long addr_set = 0;
1010 addr_req--;
1011 /* Create IDE/ATA command request structure */
1012 memset(&args, 0, sizeof(ide_task_t));
1013 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
1014 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
1015 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
1016 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
1017 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
1018 args.command_type = ide_cmd_type_parser(&args);
1019 /* submit command request */
1020 ide_raw_taskfile(drive, &args, NULL);
1021 /* if OK, read new maximum address value */
1022 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1023 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
1024 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
1025 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
1026 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
1028 addr_set++;
1029 return addr_set;
1032 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
1034 ide_task_t args;
1035 unsigned long long addr_set = 0;
1037 addr_req--;
1038 /* Create IDE/ATA command request structure */
1039 memset(&args, 0, sizeof(ide_task_t));
1040 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
1041 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
1042 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
1043 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
1044 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
1045 args.hobRegister[IDE_SECTOR_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1046 args.hobRegister[IDE_LCYL_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1047 args.hobRegister[IDE_HCYL_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1048 args.hobRegister[IDE_SELECT_OFFSET_HOB] = 0x40;
1049 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
1050 args.command_type = ide_cmd_type_parser(&args);
1051 /* submit command request */
1052 ide_raw_taskfile(drive, &args, NULL);
1053 /* if OK, compute maximum address value */
1054 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1055 u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
1056 ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
1057 (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
1058 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1059 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1060 (args.tfRegister[IDE_SECTOR_OFFSET]);
1061 addr_set = ((__u64)high << 24) | low;
1063 return addr_set;
1066 #endif /* CONFIG_IDEDISK_STROKE */
1069 * Tests if the drive supports Host Protected Area feature.
1070 * Returns true if supported, false otherwise.
1072 static inline int idedisk_supports_host_protected_area(ide_drive_t *drive)
1074 int flag = (drive->id->cfs_enable_1 & 0x0400) ? 1 : 0;
1075 if (flag)
1076 printk(KERN_INFO "%s: host protected area => %d\n", drive->name, flag);
1077 return flag;
1081 * Compute drive->capacity, the full capacity of the drive
1082 * Called with drive->id != NULL.
1084 * To compute capacity, this uses either of
1086 * 1. CHS value set by user (whatever user sets will be trusted)
1087 * 2. LBA value from target drive (require new ATA feature)
1088 * 3. LBA value from system BIOS (new one is OK, old one may break)
1089 * 4. CHS value from system BIOS (traditional style)
1091 * in above order (i.e., if value of higher priority is available,
1092 * reset will be ignored).
1094 #define IDE_STROKE_LIMIT (32000*1024*2)
1095 static void init_idedisk_capacity (ide_drive_t *drive)
1097 struct hd_driveid *id = drive->id;
1098 unsigned long capacity = drive->cyl * drive->head * drive->sect;
1099 unsigned long set_max = idedisk_read_native_max_address(drive);
1100 unsigned long long capacity_2 = capacity;
1101 unsigned long long set_max_ext;
1103 drive->capacity48 = 0;
1104 drive->select.b.lba = 0;
1106 (void) idedisk_supports_host_protected_area(drive);
1108 if (id->cfs_enable_2 & 0x0400) {
1109 capacity_2 = id->lba_capacity_2;
1110 drive->head = drive->bios_head = 255;
1111 drive->sect = drive->bios_sect = 63;
1112 drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
1113 drive->select.b.lba = 1;
1114 set_max_ext = idedisk_read_native_max_address_ext(drive);
1115 if (set_max_ext > capacity_2 && capacity_2 > IDE_STROKE_LIMIT) {
1116 #ifdef CONFIG_IDEDISK_STROKE
1117 set_max_ext = idedisk_read_native_max_address_ext(drive);
1118 set_max_ext = idedisk_set_max_address_ext(drive, set_max_ext);
1119 if (set_max_ext) {
1120 drive->capacity48 = capacity_2 = set_max_ext;
1121 drive->cyl = (unsigned int) set_max_ext / (drive->head * drive->sect);
1122 drive->select.b.lba = 1;
1123 drive->id->lba_capacity_2 = capacity_2;
1125 #else /* !CONFIG_IDEDISK_STROKE */
1126 printk(KERN_INFO "%s: setmax_ext LBA %llu, native %llu\n",
1127 drive->name, set_max_ext, capacity_2);
1128 #endif /* CONFIG_IDEDISK_STROKE */
1130 drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
1131 drive->bios_cyl = drive->cyl;
1132 drive->capacity48 = capacity_2;
1133 drive->capacity = (unsigned long) capacity_2;
1134 return;
1135 /* Determine capacity, and use LBA if the drive properly supports it */
1136 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1137 capacity = id->lba_capacity;
1138 drive->cyl = capacity / (drive->head * drive->sect);
1139 drive->select.b.lba = 1;
1142 if (set_max > capacity && capacity > IDE_STROKE_LIMIT) {
1143 #ifdef CONFIG_IDEDISK_STROKE
1144 set_max = idedisk_read_native_max_address(drive);
1145 set_max = idedisk_set_max_address(drive, set_max);
1146 if (set_max) {
1147 drive->capacity = capacity = set_max;
1148 drive->cyl = set_max / (drive->head * drive->sect);
1149 drive->select.b.lba = 1;
1150 drive->id->lba_capacity = capacity;
1152 #else /* !CONFIG_IDEDISK_STROKE */
1153 printk(KERN_INFO "%s: setmax LBA %lu, native %lu\n",
1154 drive->name, set_max, capacity);
1155 #endif /* CONFIG_IDEDISK_STROKE */
1158 drive->capacity = capacity;
1160 if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
1161 drive->capacity48 = id->lba_capacity_2;
1162 drive->head = 255;
1163 drive->sect = 63;
1164 drive->cyl = (unsigned long)(drive->capacity48) / (drive->head * drive->sect);
1168 static unsigned long idedisk_capacity (ide_drive_t *drive)
1170 if (drive->id->cfs_enable_2 & 0x0400)
1171 return (drive->capacity48 - drive->sect0);
1172 return (drive->capacity - drive->sect0);
1175 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1177 special_t *s = &drive->special;
1179 if (s->b.set_geometry) {
1180 s->b.set_geometry = 0;
1181 if (!IS_PDC4030_DRIVE) {
1182 ide_task_t args;
1183 memset(&args, 0, sizeof(ide_task_t));
1184 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1185 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
1186 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
1187 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
1188 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
1189 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1190 args.command_type = ide_cmd_type_parser(&args);
1191 do_rw_taskfile(drive, &args);
1193 } else if (s->b.recalibrate) {
1194 s->b.recalibrate = 0;
1195 if (!IS_PDC4030_DRIVE) {
1196 ide_task_t args;
1197 memset(&args, 0, sizeof(ide_task_t));
1198 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1199 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1200 args.command_type = ide_cmd_type_parser(&args);
1201 do_rw_taskfile(drive, &args);
1203 } else if (s->b.set_multmode) {
1204 s->b.set_multmode = 0;
1205 if (drive->mult_req > drive->id->max_multsect)
1206 drive->mult_req = drive->id->max_multsect;
1207 if (!IS_PDC4030_DRIVE) {
1208 ide_task_t args;
1209 memset(&args, 0, sizeof(ide_task_t));
1210 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1211 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1212 args.command_type = ide_cmd_type_parser(&args);
1213 do_rw_taskfile(drive, &args);
1215 } else if (s->all) {
1216 int special = s->all;
1217 s->all = 0;
1218 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1219 return ide_stopped;
1221 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1224 static void idedisk_pre_reset (ide_drive_t *drive)
1226 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1228 drive->special.all = 0;
1229 drive->special.b.set_geometry = legacy;
1230 drive->special.b.recalibrate = legacy;
1231 if (OK_TO_RESET_CONTROLLER)
1232 drive->mult_count = 0;
1233 if (!drive->keep_settings && !drive->using_dma)
1234 drive->mult_req = 0;
1235 if (drive->mult_req != drive->mult_count)
1236 drive->special.b.set_multmode = 1;
1239 #ifdef CONFIG_PROC_FS
1241 static int smart_enable(ide_drive_t *drive)
1243 ide_task_t args;
1245 memset(&args, 0, sizeof(ide_task_t));
1246 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
1247 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1248 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1249 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1250 args.command_type = ide_cmd_type_parser(&args);
1251 return ide_raw_taskfile(drive, &args, NULL);
1254 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1256 ide_task_t args;
1258 memset(&args, 0, sizeof(ide_task_t));
1259 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
1260 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1261 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1262 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1263 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1264 args.command_type = ide_cmd_type_parser(&args);
1265 (void) smart_enable(drive);
1266 return ide_raw_taskfile(drive, &args, buf);
1269 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1271 ide_task_t args;
1272 memset(&args, 0, sizeof(ide_task_t));
1273 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
1274 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1275 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1276 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1277 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1278 args.command_type = ide_cmd_type_parser(&args);
1279 (void) smart_enable(drive);
1280 return ide_raw_taskfile(drive, &args, buf);
1283 static int proc_idedisk_read_cache
1284 (char *page, char **start, off_t off, int count, int *eof, void *data)
1286 ide_drive_t *drive = (ide_drive_t *) data;
1287 char *out = page;
1288 int len;
1290 if (drive->id_read)
1291 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1292 else
1293 len = sprintf(out,"(none)\n");
1294 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1297 static int proc_idedisk_read_smart_thresholds
1298 (char *page, char **start, off_t off, int count, int *eof, void *data)
1300 ide_drive_t *drive = (ide_drive_t *)data;
1301 int len = 0, i = 0;
1303 if (!get_smart_thresholds(drive, page)) {
1304 unsigned short *val = (unsigned short *) page;
1305 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1306 page = out;
1307 do {
1308 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1309 val += 1;
1310 } while (i < (SECTOR_WORDS * 2));
1311 len = out - page;
1313 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1316 static int proc_idedisk_read_smart_values
1317 (char *page, char **start, off_t off, int count, int *eof, void *data)
1319 ide_drive_t *drive = (ide_drive_t *)data;
1320 int len = 0, i = 0;
1322 if (!get_smart_values(drive, page)) {
1323 unsigned short *val = (unsigned short *) page;
1324 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1325 page = out;
1326 do {
1327 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1328 val += 1;
1329 } while (i < (SECTOR_WORDS * 2));
1330 len = out - page;
1332 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1335 static ide_proc_entry_t idedisk_proc[] = {
1336 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
1337 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1338 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
1339 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
1340 { NULL, 0, NULL, NULL }
1343 #else
1345 #define idedisk_proc NULL
1347 #endif /* CONFIG_PROC_FS */
1350 * This is tightly woven into the driver->do_special can not touch.
1351 * DON'T do it again until a total personality rewrite is committed.
1353 static int set_multcount(ide_drive_t *drive, int arg)
1355 struct request rq;
1357 if (drive->special.b.set_multmode)
1358 return -EBUSY;
1359 ide_init_drive_cmd (&rq);
1360 rq.flags = REQ_DRIVE_CMD;
1361 drive->mult_req = arg;
1362 drive->special.b.set_multmode = 1;
1363 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1364 return (drive->mult_count == arg) ? 0 : -EIO;
1367 static int set_nowerr(ide_drive_t *drive, int arg)
1369 if (ide_spin_wait_hwgroup(drive))
1370 return -EBUSY;
1371 drive->nowerr = arg;
1372 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1373 spin_unlock_irq(&ide_lock);
1374 return 0;
1377 static int write_cache (ide_drive_t *drive, int arg)
1379 ide_task_t args;
1381 if (!(drive->id->cfs_enable_2 & 0x3000))
1382 return 1;
1384 memset(&args, 0, sizeof(ide_task_t));
1385 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
1386 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1387 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1388 args.command_type = ide_cmd_type_parser(&args);
1389 (void) ide_raw_taskfile(drive, &args, NULL);
1391 drive->wcache = arg;
1392 return 0;
1395 static int do_idedisk_flushcache (ide_drive_t *drive)
1397 ide_task_t args;
1399 memset(&args, 0, sizeof(ide_task_t));
1400 if (drive->id->cfs_enable_2 & 0x2400)
1401 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1402 else
1403 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1404 args.command_type = ide_cmd_type_parser(&args);
1405 return ide_raw_taskfile(drive, &args, NULL);
1408 static int set_acoustic (ide_drive_t *drive, int arg)
1410 ide_task_t args;
1412 memset(&args, 0, sizeof(ide_task_t));
1413 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
1414 SETFEATURES_DIS_AAM;
1415 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
1416 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1417 args.command_type = ide_cmd_type_parser(&args);
1418 ide_raw_taskfile(drive, &args, NULL);
1419 drive->acoustic = arg;
1420 return 0;
1423 #ifdef CONFIG_BLK_DEV_IDE_TCQ
1424 static int set_using_tcq(ide_drive_t *drive, int arg)
1426 ide_hwif_t *hwif = HWIF(drive);
1427 int ret;
1429 if (!drive->driver)
1430 return -EPERM;
1431 if (!hwif->ide_dma_queued_on || !hwif->ide_dma_queued_off)
1432 return -ENXIO;
1433 if (arg == drive->queue_depth && drive->using_tcq)
1434 return 0;
1437 * set depth, but check also id for max supported depth
1439 drive->queue_depth = arg ? arg : 1;
1440 if (drive->id) {
1441 if (drive->queue_depth > drive->id->queue_depth + 1)
1442 drive->queue_depth = drive->id->queue_depth + 1;
1445 if (arg)
1446 ret = HWIF(drive)->ide_dma_queued_on(drive);
1447 else
1448 ret = HWIF(drive)->ide_dma_queued_off(drive);
1450 return ret ? -EIO : 0;
1452 #endif
1454 static int probe_lba_addressing (ide_drive_t *drive, int arg)
1456 drive->addressing = 0;
1458 if (HWIF(drive)->addressing)
1459 return 0;
1461 if (!(drive->id->cfs_enable_2 & 0x0400))
1462 return -EIO;
1463 drive->addressing = arg;
1464 return 0;
1467 static int set_lba_addressing (ide_drive_t *drive, int arg)
1469 return probe_lba_addressing(drive, arg);
1472 static void idedisk_add_settings(ide_drive_t *drive)
1474 struct hd_driveid *id = drive->id;
1476 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
1477 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1478 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1479 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
1480 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
1481 ide_add_setting(drive, "multcount", id ? SETTING_RW : SETTING_READ, HDIO_GET_MULTCOUNT, HDIO_SET_MULTCOUNT, TYPE_BYTE, 0, id ? id->max_multsect : 0, 1, 1, &drive->mult_count, set_multcount);
1482 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
1483 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
1484 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
1485 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
1486 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
1487 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1488 #ifdef CONFIG_BLK_DEV_IDE_TCQ
1489 ide_add_setting(drive, "using_tcq", SETTING_RW, HDIO_GET_QDMA, HDIO_SET_QDMA, TYPE_BYTE, 0, IDE_MAX_TAG, 1, 1, &drive->using_tcq, set_using_tcq);
1490 #endif
1494 * Power Management state machine. This one is rather trivial for now,
1495 * we should probably add more, like switching back to PIO on suspend
1496 * to help some BIOSes, re-do the door locking on resume, etc...
1499 enum {
1500 idedisk_pm_flush_cache = ide_pm_state_start_suspend,
1501 idedisk_pm_standby,
1503 idedisk_pm_restore_dma = ide_pm_state_start_resume,
1506 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1508 switch (rq->pm->pm_step) {
1509 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
1510 if (rq->pm->pm_state == 4)
1511 rq->pm->pm_step = ide_pm_state_completed;
1512 else
1513 rq->pm->pm_step = idedisk_pm_standby;
1514 break;
1515 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
1516 rq->pm->pm_step = ide_pm_state_completed;
1517 break;
1521 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1523 ide_task_t *args = rq->special;
1525 memset(args, 0, sizeof(*args));
1527 switch (rq->pm->pm_step) {
1528 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) */
1529 /* Not supported? Switch to next step now. */
1530 if (!drive->wcache) {
1531 idedisk_complete_power_step(drive, rq, 0, 0);
1532 return ide_stopped;
1534 if (drive->id->cfs_enable_2 & 0x2400)
1535 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1536 else
1537 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1538 args->command_type = ide_cmd_type_parser(args);
1539 return do_rw_taskfile(drive, args);
1540 case idedisk_pm_standby: /* Suspend step 2 (standby) */
1541 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1542 args->command_type = ide_cmd_type_parser(args);
1543 return do_rw_taskfile(drive, args);
1545 case idedisk_pm_restore_dma: /* Resume step 1 (restore DMA) */
1547 * Right now, all we do is call hwif->ide_dma_check(drive),
1548 * we could be smarter and check for current xfer_speed
1549 * in struct drive etc...
1550 * Also, this step could be implemented as a generic helper
1551 * as most subdrivers will use it
1553 if ((drive->id->capability & 1) == 0)
1554 break;
1555 if (HWIF(drive)->ide_dma_check == NULL)
1556 break;
1557 HWIF(drive)->ide_dma_check(drive);
1558 break;
1560 rq->pm->pm_step = ide_pm_state_completed;
1561 return ide_stopped;
1564 static void idedisk_setup (ide_drive_t *drive)
1566 struct hd_driveid *id = drive->id;
1567 unsigned long capacity;
1569 idedisk_add_settings(drive);
1571 if (drive->id_read == 0)
1572 return;
1575 * CompactFlash cards and their brethern look just like hard drives
1576 * to us, but they are removable and don't have a doorlock mechanism.
1578 if (drive->removable && !(drive->is_flash)) {
1580 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1582 if (id->model[0] != 'W' || id->model[1] != 'D') {
1583 drive->doorlocking = 1;
1587 (void) probe_lba_addressing(drive, 1);
1589 if (drive->addressing == 1) {
1590 ide_hwif_t *hwif = HWIF(drive);
1591 int max_s = 2048;
1593 if (max_s > hwif->rqsize)
1594 max_s = hwif->rqsize;
1596 blk_queue_max_sectors(&drive->queue, max_s);
1599 printk("%s: max request size: %dKiB\n", drive->name, drive->queue.max_sectors / 2);
1601 /* Extract geometry if we did not already have one for the drive */
1602 if (!drive->cyl || !drive->head || !drive->sect) {
1603 drive->cyl = drive->bios_cyl = id->cyls;
1604 drive->head = drive->bios_head = id->heads;
1605 drive->sect = drive->bios_sect = id->sectors;
1608 /* Handle logical geometry translation by the drive */
1609 if ((id->field_valid & 1) && id->cur_cyls &&
1610 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1611 drive->cyl = id->cur_cyls;
1612 drive->head = id->cur_heads;
1613 drive->sect = id->cur_sectors;
1616 /* Use physical geometry if what we have still makes no sense */
1617 if (drive->head > 16 && id->heads && id->heads <= 16) {
1618 drive->cyl = id->cyls;
1619 drive->head = id->heads;
1620 drive->sect = id->sectors;
1623 /* calculate drive capacity, and select LBA if possible */
1624 init_idedisk_capacity (drive);
1627 * if possible, give fdisk access to more of the drive,
1628 * by correcting bios_cyls:
1630 capacity = idedisk_capacity (drive);
1631 if ((capacity >= (drive->bios_cyl * drive->bios_sect * drive->bios_head)) &&
1632 (!drive->forced_geom) && drive->bios_sect && drive->bios_head)
1633 drive->bios_cyl = (capacity / drive->bios_sect) / drive->bios_head;
1634 printk (KERN_INFO "%s: %ld sectors", drive->name, capacity);
1636 /* Give size in megabytes (MB), not mebibytes (MiB). */
1637 /* We compute the exact rounded value, avoiding overflow. */
1638 printk (" (%ld MB)", (capacity - capacity/625 + 974)/1950);
1640 /* Only print cache size when it was specified */
1641 if (id->buf_size)
1642 printk (" w/%dKiB Cache", id->buf_size/2);
1644 printk(", CHS=%d/%d/%d",
1645 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1646 if (drive->using_dma)
1647 (void) HWIF(drive)->ide_dma_verbose(drive);
1648 printk("\n");
1650 drive->mult_count = 0;
1651 if (id->max_multsect) {
1652 #ifdef CONFIG_IDEDISK_MULTI_MODE
1653 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1654 id->multsect_valid = id->multsect ? 1 : 0;
1655 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1656 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1657 #else /* original, pre IDE-NFG, per request of AC */
1658 drive->mult_req = INITIAL_MULT_COUNT;
1659 if (drive->mult_req > id->max_multsect)
1660 drive->mult_req = id->max_multsect;
1661 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1662 drive->special.b.set_multmode = 1;
1663 #endif /* CONFIG_IDEDISK_MULTI_MODE */
1665 drive->no_io_32bit = id->dword_io ? 1 : 0;
1666 if (drive->id->cfs_enable_2 & 0x3000)
1667 write_cache(drive, (id->cfs_enable_2 & 0x3000));
1670 static int idedisk_cleanup (ide_drive_t *drive)
1672 static int ide_cacheflush_p(ide_drive_t *drive);
1673 struct gendisk *g = drive->disk;
1674 ide_cacheflush_p(drive);
1675 if (ide_unregister_subdriver(drive))
1676 return 1;
1677 del_gendisk(g);
1678 g->fops = ide_fops;
1679 return 0;
1682 static int idedisk_attach(ide_drive_t *drive);
1685 * IDE subdriver functions, registered with ide.c
1687 static ide_driver_t idedisk_driver = {
1688 .owner = THIS_MODULE,
1689 .name = "ide-disk",
1690 .version = IDEDISK_VERSION,
1691 .media = ide_disk,
1692 .busy = 0,
1693 .supports_dma = 1,
1694 .supports_dsc_overlap = 0,
1695 .cleanup = idedisk_cleanup,
1696 .flushcache = do_idedisk_flushcache,
1697 .do_request = ide_do_rw_disk,
1698 .sense = idedisk_dump_status,
1699 .error = idedisk_error,
1700 .abort = idedisk_abort,
1701 .pre_reset = idedisk_pre_reset,
1702 .capacity = idedisk_capacity,
1703 .special = idedisk_special,
1704 .proc = idedisk_proc,
1705 .attach = idedisk_attach,
1706 .drives = LIST_HEAD_INIT(idedisk_driver.drives),
1707 .start_power_step = idedisk_start_power_step,
1708 .complete_power_step = idedisk_complete_power_step,
1709 .gen_driver = {
1710 .suspend = generic_ide_suspend,
1711 .resume = generic_ide_resume,
1715 static int idedisk_open(struct inode *inode, struct file *filp)
1717 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1718 drive->usage++;
1719 if (drive->removable && drive->usage == 1) {
1720 ide_task_t args;
1721 u8 cf;
1722 memset(&args, 0, sizeof(ide_task_t));
1723 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1724 args.command_type = ide_cmd_type_parser(&args);
1725 check_disk_change(inode->i_bdev);
1727 * Ignore the return code from door_lock,
1728 * since the open() has already succeeded,
1729 * and the door_lock is irrelevant at this point.
1731 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1732 drive->doorlocking = 0;
1733 drive->wcache = 0;
1734 /* Cache enabled ? */
1735 if (drive->id->csfo & 1)
1736 drive->wcache = 1;
1737 /* Cache command set available ? */
1738 if (drive->id->cfs_enable_1 & (1<<5))
1739 drive->wcache = 1;
1740 /* ATA6 cache extended commands */
1741 cf = drive->id->command_set_2 >> 24;
1742 if((cf & 0xC0) == 0x40 && (cf & 0x30) != 0)
1743 drive->wcache = 1;
1745 return 0;
1748 static int ide_cacheflush_p(ide_drive_t *drive)
1750 if(drive->wcache)
1752 if (do_idedisk_flushcache(drive))
1754 printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
1755 drive->name);
1756 return -EIO;
1758 return 1;
1760 return 0;
1763 static int idedisk_release(struct inode *inode, struct file *filp)
1765 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1766 if (drive->removable && drive->usage == 1) {
1767 ide_task_t args;
1768 memset(&args, 0, sizeof(ide_task_t));
1769 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1770 args.command_type = ide_cmd_type_parser(&args);
1771 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1772 drive->doorlocking = 0;
1774 ide_cacheflush_p(drive);
1775 drive->usage--;
1776 return 0;
1779 static int idedisk_ioctl(struct inode *inode, struct file *file,
1780 unsigned int cmd, unsigned long arg)
1782 struct block_device *bdev = inode->i_bdev;
1783 return generic_ide_ioctl(bdev, cmd, arg);
1786 static int idedisk_media_changed(struct gendisk *disk)
1788 ide_drive_t *drive = disk->private_data;
1789 /* if removable, always assume it was changed */
1790 return drive->removable;
1793 static int idedisk_revalidate_disk(struct gendisk *disk)
1795 ide_drive_t *drive = disk->private_data;
1796 set_capacity(disk, current_capacity(drive));
1797 return 0;
1800 static struct block_device_operations idedisk_ops = {
1801 .owner = THIS_MODULE,
1802 .open = idedisk_open,
1803 .release = idedisk_release,
1804 .ioctl = idedisk_ioctl,
1805 .media_changed = idedisk_media_changed,
1806 .revalidate_disk= idedisk_revalidate_disk
1809 MODULE_DESCRIPTION("ATA DISK Driver");
1811 static int idedisk_attach(ide_drive_t *drive)
1813 struct gendisk *g = drive->disk;
1815 /* strstr("foo", "") is non-NULL */
1816 if (!strstr("ide-disk", drive->driver_req))
1817 goto failed;
1818 if (!drive->present)
1819 goto failed;
1820 if (drive->media != ide_disk)
1821 goto failed;
1823 if (ide_register_subdriver (drive, &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1824 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1825 goto failed;
1827 DRIVER(drive)->busy++;
1828 idedisk_setup(drive);
1829 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1830 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1831 drive->name, drive->head);
1832 if ((drive->id->cfs_enable_2 & 0x3000) && drive->wcache)
1833 if (do_idedisk_flushcache(drive))
1834 printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
1835 drive->name);
1836 ide_unregister_subdriver(drive);
1837 DRIVER(drive)->busy--;
1838 goto failed;
1840 DRIVER(drive)->busy--;
1841 g->minors = 1 << PARTN_BITS;
1842 strcpy(g->devfs_name, drive->devfs_name);
1843 g->driverfs_dev = &drive->gendev;
1844 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1845 set_capacity(g, current_capacity(drive));
1846 g->fops = &idedisk_ops;
1847 add_disk(g);
1848 return 0;
1849 failed:
1850 return 1;
1853 static void __exit idedisk_exit (void)
1855 ide_unregister_driver(&idedisk_driver);
1858 static int idedisk_init (void)
1860 return ide_register_driver(&idedisk_driver);
1863 module_init(idedisk_init);
1864 module_exit(idedisk_exit);
1865 MODULE_LICENSE("GPL");