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>
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
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
44 #define IDEDISK_VERSION "1.18"
46 #undef REALLY_SLOW_IO /* most systems can safely undef this */
50 #include <linux/config.h>
51 #include <linux/module.h>
52 #include <linux/types.h>
53 #include <linux/string.h>
54 #include <linux/kernel.h>
55 #include <linux/timer.h>
57 #include <linux/interrupt.h>
58 #include <linux/major.h>
59 #include <linux/errno.h>
60 #include <linux/genhd.h>
61 #include <linux/slab.h>
62 #include <linux/delay.h>
66 #include <linux/ide.h>
68 #include <asm/byteorder.h>
70 #include <asm/uaccess.h>
72 #include <asm/div64.h>
75 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
76 * value for this drive (from its reported identification information).
78 * Returns: 1 if lba_capacity looks sensible
81 * It is called only once for each drive.
83 static int lba_capacity_is_ok (struct hd_driveid *id)
85 unsigned long lba_sects, chs_sects, head, tail;
87 /* No non-LBA info .. so valid! */
92 * The ATA spec tells large drives to return
93 * C/H/S = 16383/16/63 independent of their size.
94 * Some drives can be jumpered to use 15 heads instead of 16.
95 * Some drives can be jumpered to use 4092 cyls instead of 16383.
97 if ((id->cyls == 16383
98 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
100 (id->heads == 15 || id->heads == 16) &&
101 (id->lba_capacity >= 16383*63*id->heads))
104 lba_sects = id->lba_capacity;
105 chs_sects = id->cyls * id->heads * id->sectors;
107 /* perform a rough sanity check on lba_sects: within 10% is OK */
108 if ((lba_sects - chs_sects) < chs_sects/10)
111 /* some drives have the word order reversed */
112 head = ((lba_sects >> 16) & 0xffff);
113 tail = (lba_sects & 0xffff);
114 lba_sects = (head | (tail << 16));
115 if ((lba_sects - chs_sects) < chs_sects/10) {
116 id->lba_capacity = lba_sects;
117 return 1; /* lba_capacity is (now) good */
120 return 0; /* lba_capacity value may be bad */
124 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
125 * using LBA if supported, or CHS otherwise, to address sectors.
126 * It also takes care of issuing special DRIVE_CMDs.
128 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
130 ide_hwif_t *hwif = HWIF(drive);
131 unsigned int dma = drive->using_dma;
132 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
133 task_ioreg_t command = WIN_NOP;
134 ata_nsector_t nsectors;
136 nsectors.all = (u16) rq->nr_sectors;
138 if (hwif->no_lba48_dma && lba48 && dma) {
139 if (rq->sector + rq->nr_sectors > 1ULL << 28)
144 ide_init_sg_cmd(drive, rq);
145 ide_map_sg(drive, rq);
149 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
151 /* FIXME: SELECT_MASK(drive, 0) ? */
153 if (drive->select.b.lba) {
154 if (drive->addressing == 1) {
155 task_ioreg_t tasklets[10];
157 pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
161 tasklets[2] = nsectors.b.low;
162 tasklets[3] = nsectors.b.high;
163 tasklets[4] = (task_ioreg_t) block;
164 tasklets[5] = (task_ioreg_t) (block>>8);
165 tasklets[6] = (task_ioreg_t) (block>>16);
166 tasklets[7] = (task_ioreg_t) (block>>24);
167 if (sizeof(block) == 4) {
168 tasklets[8] = (task_ioreg_t) 0;
169 tasklets[9] = (task_ioreg_t) 0;
171 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
172 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
175 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
176 drive->name, tasklets[3], tasklets[2],
177 tasklets[9], tasklets[8], tasklets[7],
178 tasklets[6], tasklets[5], tasklets[4]);
180 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
181 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
182 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
183 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
184 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
186 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
187 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
188 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
189 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
190 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
191 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
193 hwif->OUTB(0x00, IDE_FEATURE_REG);
194 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
195 hwif->OUTB(block, IDE_SECTOR_REG);
196 hwif->OUTB(block>>=8, IDE_LCYL_REG);
197 hwif->OUTB(block>>=8, IDE_HCYL_REG);
198 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
201 unsigned int sect,head,cyl,track;
202 track = (int)block / drive->sect;
203 sect = (int)block % drive->sect + 1;
204 hwif->OUTB(sect, IDE_SECTOR_REG);
205 head = track % drive->head;
206 cyl = track / drive->head;
208 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
210 hwif->OUTB(0x00, IDE_FEATURE_REG);
211 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
212 hwif->OUTB(cyl, IDE_LCYL_REG);
213 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
214 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
218 if (!hwif->ide_dma_setup(drive)) {
219 if (rq_data_dir(rq)) {
220 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
222 command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
224 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
226 command = lba48 ? WIN_READ_EXT: WIN_READ;
228 hwif->ide_dma_exec_cmd(drive, command);
229 hwif->ide_dma_start(drive);
232 /* fallback to PIO */
233 ide_init_sg_cmd(drive, rq);
236 if (rq_data_dir(rq) == READ) {
238 if (drive->mult_count) {
239 hwif->data_phase = TASKFILE_MULTI_IN;
240 command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
242 hwif->data_phase = TASKFILE_IN;
243 command = lba48 ? WIN_READ_EXT : WIN_READ;
246 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
249 if (drive->mult_count) {
250 hwif->data_phase = TASKFILE_MULTI_OUT;
251 command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
253 hwif->data_phase = TASKFILE_OUT;
254 command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
257 /* FIXME: ->OUTBSYNC ? */
258 hwif->OUTB(command, IDE_COMMAND_REG);
260 pre_task_out_intr(drive, rq);
264 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
267 * 268435455 == 137439 MB or 28bit limit
268 * 320173056 == 163929 MB or 48bit addressing
269 * 1073741822 == 549756 MB or 48bit addressing fake drive
272 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
274 ide_hwif_t *hwif = HWIF(drive);
276 BUG_ON(drive->blocked);
278 if (!blk_fs_request(rq)) {
279 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
280 ide_end_request(drive, 0, 0);
284 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
285 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
286 block, rq->nr_sectors, (unsigned long)rq->buffer);
289 return hwif->rw_disk(drive, rq, block);
291 return __ide_do_rw_disk(drive, rq, block);
294 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
296 ide_hwif_t *hwif = HWIF(drive);
300 local_irq_set(flags);
301 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
303 if (stat & BUSY_STAT)
306 if (stat & READY_STAT) printk("DriveReady ");
307 if (stat & WRERR_STAT) printk("DeviceFault ");
308 if (stat & SEEK_STAT) printk("SeekComplete ");
309 if (stat & DRQ_STAT) printk("DataRequest ");
310 if (stat & ECC_STAT) printk("CorrectedError ");
311 if (stat & INDEX_STAT) printk("Index ");
312 if (stat & ERR_STAT) printk("Error ");
316 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
317 err = hwif->INB(IDE_ERROR_REG);
318 printk("%s: %s: error=0x%02x", drive->name, msg, err);
320 if (err & ABRT_ERR) printk("DriveStatusError ");
322 printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
323 if (err & ECC_ERR) printk("UncorrectableError ");
324 if (err & ID_ERR) printk("SectorIdNotFound ");
325 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
326 if (err & MARK_ERR) printk("AddrMarkNotFound ");
328 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
329 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
330 if (drive->addressing == 1) {
332 u32 low = 0, high = 0;
333 low = ide_read_24(drive);
334 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
335 high = ide_read_24(drive);
336 sectors = ((__u64)high << 24) | low;
337 printk(", LBAsect=%llu, high=%d, low=%d",
338 (unsigned long long) sectors,
341 u8 cur = hwif->INB(IDE_SELECT_REG);
342 if (cur & 0x40) { /* using LBA? */
343 printk(", LBAsect=%ld", (unsigned long)
345 |(hwif->INB(IDE_HCYL_REG)<<16)
346 |(hwif->INB(IDE_LCYL_REG)<<8)
347 | hwif->INB(IDE_SECTOR_REG));
349 printk(", CHS=%d/%d/%d",
350 (hwif->INB(IDE_HCYL_REG)<<8) +
351 hwif->INB(IDE_LCYL_REG),
353 hwif->INB(IDE_SECTOR_REG));
356 if (HWGROUP(drive) && HWGROUP(drive)->rq)
357 printk(", sector=%llu",
358 (unsigned long long)HWGROUP(drive)->rq->sector);
364 unsigned char opcode = 0;
367 spin_lock(&ide_lock);
368 rq = HWGROUP(drive)->rq;
369 spin_unlock(&ide_lock);
372 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
373 char *args = rq->buffer;
378 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
379 ide_task_t *args = rq->special;
381 task_struct_t *tf = (task_struct_t *) args->tfRegister;
382 opcode = tf->command;
386 printk("ide: failed opcode was: ");
390 printk("0x%02x\n", opcode);
393 local_irq_restore(flags);
397 static ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
403 err = idedisk_dump_status(drive, msg, stat);
405 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
409 /* retry only "normal" I/O: */
410 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
412 ide_end_drive_cmd(drive, stat, err);
416 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
417 /* other bits are useless when BUSY */
418 rq->errors |= ERROR_RESET;
419 } else if (stat & ERR_STAT) {
420 /* err has different meaning on cdrom and tape */
421 if (err == ABRT_ERR) {
422 if (drive->select.b.lba &&
423 /* some newer drives don't support WIN_SPECIFY */
424 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
426 } else if ((err & BAD_CRC) == BAD_CRC) {
427 /* UDMA crc error, just retry the operation */
429 } else if (err & (BBD_ERR | ECC_ERR)) {
430 /* retries won't help these */
431 rq->errors = ERROR_MAX;
432 } else if (err & TRK0_ERR) {
433 /* help it find track zero */
434 rq->errors |= ERROR_RECAL;
437 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
438 try_to_flush_leftover_data(drive);
439 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
441 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
443 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
444 DRIVER(drive)->end_request(drive, 0, 0);
446 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
448 return ide_do_reset(drive);
450 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
451 drive->special.b.recalibrate = 1;
457 static ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
462 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
467 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
469 ide_end_drive_cmd(drive, BUSY_STAT, 0);
473 DRIVER(drive)->end_request(drive, 0, 0);
478 * Queries for true maximum capacity of the drive.
479 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
481 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
484 unsigned long addr = 0;
486 /* Create IDE/ATA command request structure */
487 memset(&args, 0, sizeof(ide_task_t));
488 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
489 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
490 args.command_type = IDE_DRIVE_TASK_NO_DATA;
491 args.handler = &task_no_data_intr;
492 /* submit command request */
493 ide_raw_taskfile(drive, &args, NULL);
495 /* if OK, compute maximum address value */
496 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
497 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
498 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
499 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
500 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
501 addr++; /* since the return value is (maxlba - 1), we add 1 */
506 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
509 unsigned long long addr = 0;
511 /* Create IDE/ATA command request structure */
512 memset(&args, 0, sizeof(ide_task_t));
514 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
515 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
516 args.command_type = IDE_DRIVE_TASK_NO_DATA;
517 args.handler = &task_no_data_intr;
518 /* submit command request */
519 ide_raw_taskfile(drive, &args, NULL);
521 /* if OK, compute maximum address value */
522 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
523 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
524 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
525 args.hobRegister[IDE_SECTOR_OFFSET];
526 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
527 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
528 (args.tfRegister[IDE_SECTOR_OFFSET]);
529 addr = ((__u64)high << 24) | low;
530 addr++; /* since the return value is (maxlba - 1), we add 1 */
536 * Sets maximum virtual LBA address of the drive.
537 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
539 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
542 unsigned long addr_set = 0;
545 /* Create IDE/ATA command request structure */
546 memset(&args, 0, sizeof(ide_task_t));
547 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
548 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
549 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
550 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
551 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
552 args.command_type = IDE_DRIVE_TASK_NO_DATA;
553 args.handler = &task_no_data_intr;
554 /* submit command request */
555 ide_raw_taskfile(drive, &args, NULL);
556 /* if OK, read new maximum address value */
557 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
558 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
559 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
560 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
561 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
567 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
570 unsigned long long addr_set = 0;
573 /* Create IDE/ATA command request structure */
574 memset(&args, 0, sizeof(ide_task_t));
575 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
576 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
577 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
578 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
579 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
580 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
581 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
582 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
583 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
584 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
585 args.command_type = IDE_DRIVE_TASK_NO_DATA;
586 args.handler = &task_no_data_intr;
587 /* submit command request */
588 ide_raw_taskfile(drive, &args, NULL);
589 /* if OK, compute maximum address value */
590 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
591 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
592 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
593 args.hobRegister[IDE_SECTOR_OFFSET];
594 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
595 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
596 (args.tfRegister[IDE_SECTOR_OFFSET]);
597 addr_set = ((__u64)high << 24) | low;
603 static unsigned long long sectors_to_MB(unsigned long long n)
605 n <<= 9; /* make it bytes */
606 do_div(n, 1000000); /* make it MB */
611 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
612 * so on non-buggy drives we need test only one.
613 * However, we should also check whether these fields are valid.
615 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
617 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
623 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
625 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
626 && id->lba_capacity_2;
629 static inline void idedisk_check_hpa(ide_drive_t *drive)
631 unsigned long long capacity, set_max = 0;
632 int lba48 = idedisk_supports_lba48(drive->id);
635 capacity = drive->capacity64;
637 set_max = idedisk_read_native_max_address_ext(drive);
638 if (set_max == 0) /* LBA28 or LBA48 failed */
639 set_max = idedisk_read_native_max_address(drive);
641 if (set_max <= capacity)
644 printk(KERN_INFO "%s: Host Protected Area detected.\n"
645 "\tcurrent capacity is %llu sectors (%llu MB)\n"
646 "\tnative capacity is %llu sectors (%llu MB)\n",
648 capacity, sectors_to_MB(capacity),
649 set_max, sectors_to_MB(set_max));
651 /* Some maxtor support LBA48 but do not accept LBA48 set max... */
652 if (lba48 || set_max < (1ULL << 28))
653 set_max = idedisk_set_max_address_ext(drive, set_max);
655 set_max = idedisk_set_max_address(drive, set_max);
657 drive->capacity64 = set_max;
658 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
664 * Compute drive->capacity, the full capacity of the drive
665 * Called with drive->id != NULL.
667 * To compute capacity, this uses either of
669 * 1. CHS value set by user (whatever user sets will be trusted)
670 * 2. LBA value from target drive (require new ATA feature)
671 * 3. LBA value from system BIOS (new one is OK, old one may break)
672 * 4. CHS value from system BIOS (traditional style)
674 * in above order (i.e., if value of higher priority is available,
675 * reset will be ignored).
677 static void init_idedisk_capacity (ide_drive_t *drive)
679 struct hd_driveid *id = drive->id;
681 * If this drive supports the Host Protected Area feature set,
682 * then we may need to change our opinion about the drive's capacity.
684 int hpa = idedisk_supports_hpa(id);
686 if (idedisk_supports_lba48(id)) {
687 /* drive speaks 48-bit LBA */
688 drive->select.b.lba = 1;
689 drive->capacity64 = id->lba_capacity_2;
691 idedisk_check_hpa(drive);
692 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
693 /* drive speaks 28-bit LBA */
694 drive->select.b.lba = 1;
695 drive->capacity64 = id->lba_capacity;
697 idedisk_check_hpa(drive);
699 /* drive speaks boring old 28-bit CHS */
700 drive->capacity64 = drive->cyl * drive->head * drive->sect;
704 static sector_t idedisk_capacity (ide_drive_t *drive)
706 return drive->capacity64 - drive->sect0;
709 #define IS_PDC4030_DRIVE 0
711 static ide_startstop_t idedisk_special (ide_drive_t *drive)
713 special_t *s = &drive->special;
715 if (s->b.set_geometry) {
716 s->b.set_geometry = 0;
717 if (!IS_PDC4030_DRIVE) {
719 memset(&args, 0, sizeof(ide_task_t));
720 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
721 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
722 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
723 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
724 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
725 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
726 args.command_type = IDE_DRIVE_TASK_NO_DATA;
727 args.handler = &set_geometry_intr;
728 do_rw_taskfile(drive, &args);
730 } else if (s->b.recalibrate) {
731 s->b.recalibrate = 0;
732 if (!IS_PDC4030_DRIVE) {
734 memset(&args, 0, sizeof(ide_task_t));
735 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
736 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
737 args.command_type = IDE_DRIVE_TASK_NO_DATA;
738 args.handler = &recal_intr;
739 do_rw_taskfile(drive, &args);
741 } else if (s->b.set_multmode) {
742 s->b.set_multmode = 0;
743 if (drive->mult_req > drive->id->max_multsect)
744 drive->mult_req = drive->id->max_multsect;
745 if (!IS_PDC4030_DRIVE) {
747 memset(&args, 0, sizeof(ide_task_t));
748 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
749 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
750 args.command_type = IDE_DRIVE_TASK_NO_DATA;
751 args.handler = &set_multmode_intr;
752 do_rw_taskfile(drive, &args);
755 int special = s->all;
757 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
760 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
763 static void idedisk_pre_reset (ide_drive_t *drive)
765 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
767 drive->special.all = 0;
768 drive->special.b.set_geometry = legacy;
769 drive->special.b.recalibrate = legacy;
770 if (OK_TO_RESET_CONTROLLER)
771 drive->mult_count = 0;
772 if (!drive->keep_settings && !drive->using_dma)
774 if (drive->mult_req != drive->mult_count)
775 drive->special.b.set_multmode = 1;
778 #ifdef CONFIG_PROC_FS
780 static int smart_enable(ide_drive_t *drive)
784 memset(&args, 0, sizeof(ide_task_t));
785 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
786 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
787 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
788 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
789 args.command_type = IDE_DRIVE_TASK_NO_DATA;
790 args.handler = &task_no_data_intr;
791 return ide_raw_taskfile(drive, &args, NULL);
794 static int get_smart_values(ide_drive_t *drive, u8 *buf)
798 memset(&args, 0, sizeof(ide_task_t));
799 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
800 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
801 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
802 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
803 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
804 args.command_type = IDE_DRIVE_TASK_IN;
805 args.data_phase = TASKFILE_IN;
806 args.handler = &task_in_intr;
807 (void) smart_enable(drive);
808 return ide_raw_taskfile(drive, &args, buf);
811 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
814 memset(&args, 0, sizeof(ide_task_t));
815 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
816 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
817 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
818 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
819 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
820 args.command_type = IDE_DRIVE_TASK_IN;
821 args.data_phase = TASKFILE_IN;
822 args.handler = &task_in_intr;
823 (void) smart_enable(drive);
824 return ide_raw_taskfile(drive, &args, buf);
827 static int proc_idedisk_read_cache
828 (char *page, char **start, off_t off, int count, int *eof, void *data)
835 drive = ide_drive_from_key(data);
836 if (drive && drive->id_read)
837 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
839 len = sprintf(out,"(none)\n");
841 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
844 static int proc_idedisk_read_smart_thresholds
845 (char *page, char **start, off_t off, int count, int *eof, void *data)
852 drive = ide_drive_from_key(data);
853 if (drive && !get_smart_thresholds(drive, page)) {
854 unsigned short *val = (unsigned short *) page;
855 char *out = ((char *)val) + (SECTOR_WORDS * 4);
858 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
860 } while (i < (SECTOR_WORDS * 2));
864 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
867 static int proc_idedisk_read_smart_values
868 (char *page, char **start, off_t off, int count, int *eof, void *data)
875 drive = ide_drive_from_key(data);
876 if (drive && !get_smart_values(drive, page)) {
877 unsigned short *val = (unsigned short *) page;
878 char *out = ((char *)val) + (SECTOR_WORDS * 4);
881 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
883 } while (i < (SECTOR_WORDS * 2));
887 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
890 static ide_proc_entry_t idedisk_proc[] = {
891 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
892 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
893 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
894 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
895 { NULL, 0, NULL, NULL }
900 #define idedisk_proc NULL
902 #endif /* CONFIG_PROC_FS */
904 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
905 sector_t *error_sector)
907 ide_drive_t *drive = q->queuedata;
914 rq = blk_get_request(q, WRITE, __GFP_WAIT);
916 memset(rq->cmd, 0, sizeof(rq->cmd));
918 if (ide_id_has_flush_cache_ext(drive->id) &&
919 (drive->capacity64 >= (1UL << 28)))
920 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
922 rq->cmd[0] = WIN_FLUSH_CACHE;
925 rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
926 rq->buffer = rq->cmd;
928 ret = blk_execute_rq(q, disk, rq);
931 * if we failed and caller wants error offset, get it
933 if (ret && error_sector)
934 *error_sector = ide_get_error_location(drive, rq->cmd);
941 * This is tightly woven into the driver->do_special can not touch.
942 * DON'T do it again until a total personality rewrite is committed.
944 static int set_multcount(ide_drive_t *drive, int arg)
948 if (drive->special.b.set_multmode)
950 ide_init_drive_cmd (&rq);
951 rq.flags = REQ_DRIVE_CMD;
952 drive->mult_req = arg;
953 drive->special.b.set_multmode = 1;
954 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
955 return (drive->mult_count == arg) ? 0 : -EIO;
958 static int set_nowerr(ide_drive_t *drive, int arg)
960 if (ide_spin_wait_hwgroup(drive))
963 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
964 spin_unlock_irq(&ide_lock);
968 static int write_cache(ide_drive_t *drive, int arg)
973 if (!ide_id_has_flush_cache(drive->id))
976 memset(&args, 0, sizeof(ide_task_t));
977 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
978 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
979 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
980 args.command_type = IDE_DRIVE_TASK_NO_DATA;
981 args.handler = &task_no_data_intr;
983 err = ide_raw_taskfile(drive, &args, NULL);
991 static int do_idedisk_flushcache (ide_drive_t *drive)
995 memset(&args, 0, sizeof(ide_task_t));
996 if (ide_id_has_flush_cache_ext(drive->id))
997 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
999 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1000 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1001 args.handler = &task_no_data_intr;
1002 return ide_raw_taskfile(drive, &args, NULL);
1005 static int set_acoustic (ide_drive_t *drive, int arg)
1009 memset(&args, 0, sizeof(ide_task_t));
1010 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
1011 SETFEATURES_DIS_AAM;
1012 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
1013 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1014 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1015 args.handler = &task_no_data_intr;
1016 ide_raw_taskfile(drive, &args, NULL);
1017 drive->acoustic = arg;
1022 * drive->addressing:
1025 * 2: 48-bit capable doing 28-bit
1027 static int set_lba_addressing(ide_drive_t *drive, int arg)
1029 drive->addressing = 0;
1031 if (HWIF(drive)->no_lba48)
1034 if (!idedisk_supports_lba48(drive->id))
1036 drive->addressing = arg;
1040 static void idedisk_add_settings(ide_drive_t *drive)
1042 struct hd_driveid *id = drive->id;
1044 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
1045 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1046 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1047 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
1048 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
1049 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);
1050 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
1051 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
1052 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
1053 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
1054 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
1055 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1059 * Power Management state machine. This one is rather trivial for now,
1060 * we should probably add more, like switching back to PIO on suspend
1061 * to help some BIOSes, re-do the door locking on resume, etc...
1065 idedisk_pm_flush_cache = ide_pm_state_start_suspend,
1068 idedisk_pm_idle = ide_pm_state_start_resume,
1069 idedisk_pm_restore_dma,
1072 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1074 switch (rq->pm->pm_step) {
1075 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
1076 if (rq->pm->pm_state == 4)
1077 rq->pm->pm_step = ide_pm_state_completed;
1079 rq->pm->pm_step = idedisk_pm_standby;
1081 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
1082 rq->pm->pm_step = ide_pm_state_completed;
1084 case idedisk_pm_idle: /* Resume step 1 (idle) complete */
1085 rq->pm->pm_step = idedisk_pm_restore_dma;
1090 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1092 ide_task_t *args = rq->special;
1094 memset(args, 0, sizeof(*args));
1096 switch (rq->pm->pm_step) {
1097 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) */
1098 /* Not supported? Switch to next step now. */
1099 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
1100 idedisk_complete_power_step(drive, rq, 0, 0);
1103 if (ide_id_has_flush_cache_ext(drive->id))
1104 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1106 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1107 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1108 args->handler = &task_no_data_intr;
1109 return do_rw_taskfile(drive, args);
1111 case idedisk_pm_standby: /* Suspend step 2 (standby) */
1112 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1113 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1114 args->handler = &task_no_data_intr;
1115 return do_rw_taskfile(drive, args);
1117 case idedisk_pm_idle: /* Resume step 1 (idle) */
1118 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
1119 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1120 args->handler = task_no_data_intr;
1121 return do_rw_taskfile(drive, args);
1123 case idedisk_pm_restore_dma: /* Resume step 2 (restore DMA) */
1125 * Right now, all we do is call hwif->ide_dma_check(drive),
1126 * we could be smarter and check for current xfer_speed
1127 * in struct drive etc...
1128 * Also, this step could be implemented as a generic helper
1129 * as most subdrivers will use it
1131 if ((drive->id->capability & 1) == 0)
1133 if (HWIF(drive)->ide_dma_check == NULL)
1135 HWIF(drive)->ide_dma_check(drive);
1138 rq->pm->pm_step = ide_pm_state_completed;
1142 static void idedisk_setup (ide_drive_t *drive)
1144 struct hd_driveid *id = drive->id;
1145 unsigned long long capacity;
1148 idedisk_add_settings(drive);
1150 if (drive->id_read == 0)
1154 * CompactFlash cards and their brethern look just like hard drives
1155 * to us, but they are removable and don't have a doorlock mechanism.
1157 if (drive->removable && !(drive->is_flash)) {
1159 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1161 if (id->model[0] != 'W' || id->model[1] != 'D') {
1162 drive->doorlocking = 1;
1166 (void)set_lba_addressing(drive, 1);
1168 if (drive->addressing == 1) {
1169 ide_hwif_t *hwif = HWIF(drive);
1172 if (max_s > hwif->rqsize)
1173 max_s = hwif->rqsize;
1175 blk_queue_max_sectors(drive->queue, max_s);
1178 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
1180 /* Extract geometry if we did not already have one for the drive */
1181 if (!drive->cyl || !drive->head || !drive->sect) {
1182 drive->cyl = drive->bios_cyl = id->cyls;
1183 drive->head = drive->bios_head = id->heads;
1184 drive->sect = drive->bios_sect = id->sectors;
1187 /* Handle logical geometry translation by the drive */
1188 if ((id->field_valid & 1) && id->cur_cyls &&
1189 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1190 drive->cyl = id->cur_cyls;
1191 drive->head = id->cur_heads;
1192 drive->sect = id->cur_sectors;
1195 /* Use physical geometry if what we have still makes no sense */
1196 if (drive->head > 16 && id->heads && id->heads <= 16) {
1197 drive->cyl = id->cyls;
1198 drive->head = id->heads;
1199 drive->sect = id->sectors;
1202 /* calculate drive capacity, and select LBA if possible */
1203 init_idedisk_capacity (drive);
1205 /* limit drive capacity to 137GB if LBA48 cannot be used */
1206 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1207 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
1208 "%llu sectors (%llu MB)\n",
1209 drive->name, (unsigned long long)drive->capacity64,
1210 sectors_to_MB(drive->capacity64));
1211 drive->capacity64 = 1ULL << 28;
1214 if (drive->hwif->no_lba48_dma && drive->addressing) {
1215 if (drive->capacity64 > 1ULL << 28) {
1216 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
1217 " be used for accessing sectors > %u\n",
1218 drive->name, 1 << 28);
1220 drive->addressing = 0;
1224 * if possible, give fdisk access to more of the drive,
1225 * by correcting bios_cyls:
1227 capacity = idedisk_capacity (drive);
1228 if (!drive->forced_geom) {
1230 if (idedisk_supports_lba48(drive->id)) {
1232 drive->bios_sect = 63;
1233 drive->bios_head = 255;
1236 if (drive->bios_sect && drive->bios_head) {
1237 unsigned int cap0 = capacity; /* truncate to 32 bits */
1238 unsigned int cylsz, cyl;
1240 if (cap0 != capacity)
1241 drive->bios_cyl = 65535;
1243 cylsz = drive->bios_sect * drive->bios_head;
1247 if (cyl > drive->bios_cyl)
1248 drive->bios_cyl = cyl;
1252 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1253 drive->name, capacity, sectors_to_MB(capacity));
1255 /* Only print cache size when it was specified */
1257 printk (" w/%dKiB Cache", id->buf_size/2);
1260 printk(", CHS=%d/%d/%d",
1261 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1262 if (drive->using_dma)
1263 ide_dma_verbose(drive);
1266 drive->mult_count = 0;
1267 if (id->max_multsect) {
1268 #ifdef CONFIG_IDEDISK_MULTI_MODE
1269 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1270 id->multsect_valid = id->multsect ? 1 : 0;
1271 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1272 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1273 #else /* original, pre IDE-NFG, per request of AC */
1274 drive->mult_req = INITIAL_MULT_COUNT;
1275 if (drive->mult_req > id->max_multsect)
1276 drive->mult_req = id->max_multsect;
1277 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1278 drive->special.b.set_multmode = 1;
1279 #endif /* CONFIG_IDEDISK_MULTI_MODE */
1281 drive->no_io_32bit = id->dword_io ? 1 : 0;
1283 /* write cache enabled? */
1284 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1287 write_cache(drive, 1);
1290 * We must avoid issuing commands a drive does not understand
1291 * or we may crash it. We check flush cache is supported. We also
1292 * check we have the LBA48 flush cache if the drive capacity is
1293 * too large. By this time we have trimmed the drive capacity if
1294 * LBA48 is not available so we don't need to recheck that.
1297 if (ide_id_has_flush_cache(id))
1299 if (drive->addressing == 1) {
1300 /* Can't issue the correct flush ? */
1301 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1305 printk(KERN_DEBUG "%s: cache flushes %ssupported\n",
1306 drive->name, barrier ? "" : "not ");
1308 blk_queue_ordered(drive->queue, 1);
1309 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1313 static void ide_cacheflush_p(ide_drive_t *drive)
1315 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1318 if (do_idedisk_flushcache(drive))
1319 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1322 static int idedisk_cleanup (ide_drive_t *drive)
1324 struct gendisk *g = drive->disk;
1325 ide_cacheflush_p(drive);
1326 if (ide_unregister_subdriver(drive))
1329 drive->devfs_name[0] = '\0';
1334 static int idedisk_attach(ide_drive_t *drive);
1336 static void ide_device_shutdown(struct device *dev)
1338 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1341 /* On Alpha, halt(8) doesn't actually turn the machine off,
1342 it puts you into the sort of firmware monitor. Typically,
1343 it's used to boot another kernel image, so it's not much
1344 different from reboot(8). Therefore, we don't need to
1345 spin down the disk in this case, especially since Alpha
1346 firmware doesn't handle disks in standby mode properly.
1347 On the other hand, it's reasonably safe to turn the power
1348 off when the shutdown process reaches the firmware prompt,
1349 as the firmware initialization takes rather long time -
1350 at least 10 seconds, which should be sufficient for
1351 the disk to expire its write cache. */
1352 if (system_state != SYSTEM_POWER_OFF) {
1354 if (system_state == SYSTEM_RESTART) {
1356 ide_cacheflush_p(drive);
1360 printk("Shutdown: %s\n", drive->name);
1361 dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1365 * IDE subdriver functions, registered with ide.c
1367 static ide_driver_t idedisk_driver = {
1368 .owner = THIS_MODULE,
1370 .shutdown = ide_device_shutdown,
1373 .version = IDEDISK_VERSION,
1376 .supports_dsc_overlap = 0,
1377 .cleanup = idedisk_cleanup,
1378 .do_request = ide_do_rw_disk,
1379 .sense = idedisk_dump_status,
1380 .error = idedisk_error,
1381 .abort = idedisk_abort,
1382 .pre_reset = idedisk_pre_reset,
1383 .capacity = idedisk_capacity,
1384 .special = idedisk_special,
1385 .proc = idedisk_proc,
1386 .attach = idedisk_attach,
1387 .drives = LIST_HEAD_INIT(idedisk_driver.drives),
1388 .start_power_step = idedisk_start_power_step,
1389 .complete_power_step = idedisk_complete_power_step,
1392 static int idedisk_open(struct inode *inode, struct file *filp)
1394 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1396 if (drive->removable && drive->usage == 1) {
1398 memset(&args, 0, sizeof(ide_task_t));
1399 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1400 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1401 args.handler = &task_no_data_intr;
1402 check_disk_change(inode->i_bdev);
1404 * Ignore the return code from door_lock,
1405 * since the open() has already succeeded,
1406 * and the door_lock is irrelevant at this point.
1408 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1409 drive->doorlocking = 0;
1414 static int idedisk_release(struct inode *inode, struct file *filp)
1416 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1417 if (drive->usage == 1)
1418 ide_cacheflush_p(drive);
1419 if (drive->removable && drive->usage == 1) {
1421 memset(&args, 0, sizeof(ide_task_t));
1422 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1423 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1424 args.handler = &task_no_data_intr;
1425 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1426 drive->doorlocking = 0;
1432 static int idedisk_ioctl(struct inode *inode, struct file *file,
1433 unsigned int cmd, unsigned long arg)
1435 struct block_device *bdev = inode->i_bdev;
1436 return generic_ide_ioctl(file, bdev, cmd, arg);
1439 static int idedisk_media_changed(struct gendisk *disk)
1441 ide_drive_t *drive = disk->private_data;
1443 /* do not scan partitions twice if this is a removable device */
1444 if (drive->attach) {
1448 /* if removable, always assume it was changed */
1449 return drive->removable;
1452 static int idedisk_revalidate_disk(struct gendisk *disk)
1454 ide_drive_t *drive = disk->private_data;
1455 set_capacity(disk, current_capacity(drive));
1459 static struct block_device_operations idedisk_ops = {
1460 .owner = THIS_MODULE,
1461 .open = idedisk_open,
1462 .release = idedisk_release,
1463 .ioctl = idedisk_ioctl,
1464 .media_changed = idedisk_media_changed,
1465 .revalidate_disk= idedisk_revalidate_disk
1468 MODULE_DESCRIPTION("ATA DISK Driver");
1470 static int idedisk_attach(ide_drive_t *drive)
1472 struct gendisk *g = drive->disk;
1474 /* strstr("foo", "") is non-NULL */
1475 if (!strstr("ide-disk", drive->driver_req))
1477 if (!drive->present)
1479 if (drive->media != ide_disk)
1482 if (ide_register_subdriver(drive, &idedisk_driver)) {
1483 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1486 DRIVER(drive)->busy++;
1487 idedisk_setup(drive);
1488 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1489 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1490 drive->name, drive->head);
1494 DRIVER(drive)->busy--;
1495 g->minors = 1 << PARTN_BITS;
1496 strcpy(g->devfs_name, drive->devfs_name);
1497 g->driverfs_dev = &drive->gendev;
1498 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1499 set_capacity(g, current_capacity(drive));
1500 g->fops = &idedisk_ops;
1507 static void __exit idedisk_exit (void)
1509 ide_unregister_driver(&idedisk_driver);
1512 static int idedisk_init (void)
1514 return ide_register_driver(&idedisk_driver);
1517 module_init(idedisk_init);
1518 module_exit(idedisk_exit);
1519 MODULE_LICENSE("GPL");