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;
88 * The ATA spec tells large drives to return
89 * C/H/S = 16383/16/63 independent of their size.
90 * Some drives can be jumpered to use 15 heads instead of 16.
91 * Some drives can be jumpered to use 4092 cyls instead of 16383.
93 if ((id->cyls == 16383
94 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
96 (id->heads == 15 || id->heads == 16) &&
97 (id->lba_capacity >= 16383*63*id->heads))
100 lba_sects = id->lba_capacity;
101 chs_sects = id->cyls * id->heads * id->sectors;
103 /* perform a rough sanity check on lba_sects: within 10% is OK */
104 if ((lba_sects - chs_sects) < chs_sects/10)
107 /* some drives have the word order reversed */
108 head = ((lba_sects >> 16) & 0xffff);
109 tail = (lba_sects & 0xffff);
110 lba_sects = (head | (tail << 16));
111 if ((lba_sects - chs_sects) < chs_sects/10) {
112 id->lba_capacity = lba_sects;
113 return 1; /* lba_capacity is (now) good */
116 return 0; /* lba_capacity value may be bad */
120 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
121 * using LBA if supported, or CHS otherwise, to address sectors.
123 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
125 ide_hwif_t *hwif = HWIF(drive);
126 unsigned int dma = drive->using_dma;
127 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
128 task_ioreg_t command = WIN_NOP;
129 ata_nsector_t nsectors;
131 nsectors.all = (u16) rq->nr_sectors;
133 if (hwif->no_lba48_dma && lba48 && dma) {
134 if (block + rq->nr_sectors > 1ULL << 28)
141 ide_init_sg_cmd(drive, rq);
142 ide_map_sg(drive, rq);
146 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
148 /* FIXME: SELECT_MASK(drive, 0) ? */
150 if (drive->select.b.lba) {
152 task_ioreg_t tasklets[10];
154 pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
158 tasklets[2] = nsectors.b.low;
159 tasklets[3] = nsectors.b.high;
160 tasklets[4] = (task_ioreg_t) block;
161 tasklets[5] = (task_ioreg_t) (block>>8);
162 tasklets[6] = (task_ioreg_t) (block>>16);
163 tasklets[7] = (task_ioreg_t) (block>>24);
164 if (sizeof(block) == 4) {
165 tasklets[8] = (task_ioreg_t) 0;
166 tasklets[9] = (task_ioreg_t) 0;
168 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
169 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
172 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
173 drive->name, tasklets[3], tasklets[2],
174 tasklets[9], tasklets[8], tasklets[7],
175 tasklets[6], tasklets[5], tasklets[4]);
177 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
178 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
179 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
180 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
181 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
183 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
184 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
185 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
186 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
187 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
188 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
190 hwif->OUTB(0x00, IDE_FEATURE_REG);
191 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
192 hwif->OUTB(block, IDE_SECTOR_REG);
193 hwif->OUTB(block>>=8, IDE_LCYL_REG);
194 hwif->OUTB(block>>=8, IDE_HCYL_REG);
195 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
198 unsigned int sect,head,cyl,track;
199 track = (int)block / drive->sect;
200 sect = (int)block % drive->sect + 1;
201 hwif->OUTB(sect, IDE_SECTOR_REG);
202 head = track % drive->head;
203 cyl = track / drive->head;
205 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
207 hwif->OUTB(0x00, IDE_FEATURE_REG);
208 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
209 hwif->OUTB(cyl, IDE_LCYL_REG);
210 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
211 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
215 if (!hwif->dma_setup(drive)) {
216 if (rq_data_dir(rq)) {
217 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
219 command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
221 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
223 command = lba48 ? WIN_READ_EXT: WIN_READ;
225 hwif->dma_exec_cmd(drive, command);
226 hwif->dma_start(drive);
229 /* fallback to PIO */
230 ide_init_sg_cmd(drive, rq);
233 if (rq_data_dir(rq) == READ) {
235 if (drive->mult_count) {
236 hwif->data_phase = TASKFILE_MULTI_IN;
237 command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
239 hwif->data_phase = TASKFILE_IN;
240 command = lba48 ? WIN_READ_EXT : WIN_READ;
243 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
246 if (drive->mult_count) {
247 hwif->data_phase = TASKFILE_MULTI_OUT;
248 command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
250 hwif->data_phase = TASKFILE_OUT;
251 command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
254 /* FIXME: ->OUTBSYNC ? */
255 hwif->OUTB(command, IDE_COMMAND_REG);
257 return pre_task_out_intr(drive, rq);
262 * 268435455 == 137439 MB or 28bit limit
263 * 320173056 == 163929 MB or 48bit addressing
264 * 1073741822 == 549756 MB or 48bit addressing fake drive
267 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
269 ide_hwif_t *hwif = HWIF(drive);
271 BUG_ON(drive->blocked);
273 if (!blk_fs_request(rq)) {
274 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
275 ide_end_request(drive, 0, 0);
279 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
280 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
281 block, rq->nr_sectors, (unsigned long)rq->buffer);
284 hwif->rw_disk(drive, rq);
286 return __ide_do_rw_disk(drive, rq, block);
290 * Queries for true maximum capacity of the drive.
291 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
293 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
296 unsigned long addr = 0;
298 /* Create IDE/ATA command request structure */
299 memset(&args, 0, sizeof(ide_task_t));
300 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
301 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
302 args.command_type = IDE_DRIVE_TASK_NO_DATA;
303 args.handler = &task_no_data_intr;
304 /* submit command request */
305 ide_raw_taskfile(drive, &args, NULL);
307 /* if OK, compute maximum address value */
308 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
309 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
310 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
311 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
312 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
313 addr++; /* since the return value is (maxlba - 1), we add 1 */
318 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
321 unsigned long long addr = 0;
323 /* Create IDE/ATA command request structure */
324 memset(&args, 0, sizeof(ide_task_t));
326 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
327 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
328 args.command_type = IDE_DRIVE_TASK_NO_DATA;
329 args.handler = &task_no_data_intr;
330 /* submit command request */
331 ide_raw_taskfile(drive, &args, NULL);
333 /* if OK, compute maximum address value */
334 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
335 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
336 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
337 args.hobRegister[IDE_SECTOR_OFFSET];
338 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
339 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
340 (args.tfRegister[IDE_SECTOR_OFFSET]);
341 addr = ((__u64)high << 24) | low;
342 addr++; /* since the return value is (maxlba - 1), we add 1 */
348 * Sets maximum virtual LBA address of the drive.
349 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
351 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
354 unsigned long addr_set = 0;
357 /* Create IDE/ATA command request structure */
358 memset(&args, 0, sizeof(ide_task_t));
359 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
360 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
361 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
362 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
363 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
364 args.command_type = IDE_DRIVE_TASK_NO_DATA;
365 args.handler = &task_no_data_intr;
366 /* submit command request */
367 ide_raw_taskfile(drive, &args, NULL);
368 /* if OK, read new maximum address value */
369 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
370 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
371 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
372 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
373 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
379 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
382 unsigned long long addr_set = 0;
385 /* Create IDE/ATA command request structure */
386 memset(&args, 0, sizeof(ide_task_t));
387 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
388 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
389 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
390 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
391 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
392 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
393 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
394 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
395 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
396 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
397 args.command_type = IDE_DRIVE_TASK_NO_DATA;
398 args.handler = &task_no_data_intr;
399 /* submit command request */
400 ide_raw_taskfile(drive, &args, NULL);
401 /* if OK, compute maximum address value */
402 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
403 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
404 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
405 args.hobRegister[IDE_SECTOR_OFFSET];
406 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
407 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
408 (args.tfRegister[IDE_SECTOR_OFFSET]);
409 addr_set = ((__u64)high << 24) | low;
415 static unsigned long long sectors_to_MB(unsigned long long n)
417 n <<= 9; /* make it bytes */
418 do_div(n, 1000000); /* make it MB */
423 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
424 * so on non-buggy drives we need test only one.
425 * However, we should also check whether these fields are valid.
427 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
429 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
435 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
437 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
438 && id->lba_capacity_2;
441 static inline void idedisk_check_hpa(ide_drive_t *drive)
443 unsigned long long capacity, set_max;
444 int lba48 = idedisk_supports_lba48(drive->id);
446 capacity = drive->capacity64;
448 set_max = idedisk_read_native_max_address_ext(drive);
450 set_max = idedisk_read_native_max_address(drive);
452 if (set_max <= capacity)
455 printk(KERN_INFO "%s: Host Protected Area detected.\n"
456 "\tcurrent capacity is %llu sectors (%llu MB)\n"
457 "\tnative capacity is %llu sectors (%llu MB)\n",
459 capacity, sectors_to_MB(capacity),
460 set_max, sectors_to_MB(set_max));
463 set_max = idedisk_set_max_address_ext(drive, set_max);
465 set_max = idedisk_set_max_address(drive, set_max);
467 drive->capacity64 = set_max;
468 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
474 * Compute drive->capacity, the full capacity of the drive
475 * Called with drive->id != NULL.
477 * To compute capacity, this uses either of
479 * 1. CHS value set by user (whatever user sets will be trusted)
480 * 2. LBA value from target drive (require new ATA feature)
481 * 3. LBA value from system BIOS (new one is OK, old one may break)
482 * 4. CHS value from system BIOS (traditional style)
484 * in above order (i.e., if value of higher priority is available,
485 * reset will be ignored).
487 static void init_idedisk_capacity (ide_drive_t *drive)
489 struct hd_driveid *id = drive->id;
491 * If this drive supports the Host Protected Area feature set,
492 * then we may need to change our opinion about the drive's capacity.
494 int hpa = idedisk_supports_hpa(id);
496 if (idedisk_supports_lba48(id)) {
497 /* drive speaks 48-bit LBA */
498 drive->select.b.lba = 1;
499 drive->capacity64 = id->lba_capacity_2;
501 idedisk_check_hpa(drive);
502 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
503 /* drive speaks 28-bit LBA */
504 drive->select.b.lba = 1;
505 drive->capacity64 = id->lba_capacity;
507 idedisk_check_hpa(drive);
509 /* drive speaks boring old 28-bit CHS */
510 drive->capacity64 = drive->cyl * drive->head * drive->sect;
514 static sector_t idedisk_capacity (ide_drive_t *drive)
516 return drive->capacity64 - drive->sect0;
519 #define IS_PDC4030_DRIVE 0
521 static ide_startstop_t idedisk_special (ide_drive_t *drive)
523 special_t *s = &drive->special;
525 if (s->b.set_geometry) {
526 s->b.set_geometry = 0;
527 if (!IS_PDC4030_DRIVE) {
529 memset(&args, 0, sizeof(ide_task_t));
530 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
531 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
532 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
533 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
534 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
535 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
536 args.command_type = IDE_DRIVE_TASK_NO_DATA;
537 args.handler = &set_geometry_intr;
538 do_rw_taskfile(drive, &args);
540 } else if (s->b.recalibrate) {
541 s->b.recalibrate = 0;
542 if (!IS_PDC4030_DRIVE) {
544 memset(&args, 0, sizeof(ide_task_t));
545 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
546 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
547 args.command_type = IDE_DRIVE_TASK_NO_DATA;
548 args.handler = &recal_intr;
549 do_rw_taskfile(drive, &args);
551 } else if (s->b.set_multmode) {
552 s->b.set_multmode = 0;
553 if (drive->mult_req > drive->id->max_multsect)
554 drive->mult_req = drive->id->max_multsect;
555 if (!IS_PDC4030_DRIVE) {
557 memset(&args, 0, sizeof(ide_task_t));
558 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
559 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
560 args.command_type = IDE_DRIVE_TASK_NO_DATA;
561 args.handler = &set_multmode_intr;
562 do_rw_taskfile(drive, &args);
565 int special = s->all;
567 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
570 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
573 static void idedisk_pre_reset (ide_drive_t *drive)
575 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
577 drive->special.all = 0;
578 drive->special.b.set_geometry = legacy;
579 drive->special.b.recalibrate = legacy;
580 if (OK_TO_RESET_CONTROLLER)
581 drive->mult_count = 0;
582 if (!drive->keep_settings && !drive->using_dma)
584 if (drive->mult_req != drive->mult_count)
585 drive->special.b.set_multmode = 1;
588 #ifdef CONFIG_PROC_FS
590 static int smart_enable(ide_drive_t *drive)
594 memset(&args, 0, sizeof(ide_task_t));
595 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
596 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
597 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
598 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
599 args.command_type = IDE_DRIVE_TASK_NO_DATA;
600 args.handler = &task_no_data_intr;
601 return ide_raw_taskfile(drive, &args, NULL);
604 static int get_smart_values(ide_drive_t *drive, u8 *buf)
608 memset(&args, 0, sizeof(ide_task_t));
609 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
610 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
611 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
612 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
613 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
614 args.command_type = IDE_DRIVE_TASK_IN;
615 args.data_phase = TASKFILE_IN;
616 args.handler = &task_in_intr;
617 (void) smart_enable(drive);
618 return ide_raw_taskfile(drive, &args, buf);
621 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
624 memset(&args, 0, sizeof(ide_task_t));
625 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
626 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
627 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
628 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
629 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
630 args.command_type = IDE_DRIVE_TASK_IN;
631 args.data_phase = TASKFILE_IN;
632 args.handler = &task_in_intr;
633 (void) smart_enable(drive);
634 return ide_raw_taskfile(drive, &args, buf);
637 static int proc_idedisk_read_cache
638 (char *page, char **start, off_t off, int count, int *eof, void *data)
640 ide_drive_t *drive = (ide_drive_t *) data;
645 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
647 len = sprintf(out,"(none)\n");
648 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
651 static int proc_idedisk_read_smart_thresholds
652 (char *page, char **start, off_t off, int count, int *eof, void *data)
654 ide_drive_t *drive = (ide_drive_t *)data;
657 if (!get_smart_thresholds(drive, page)) {
658 unsigned short *val = (unsigned short *) page;
659 char *out = ((char *)val) + (SECTOR_WORDS * 4);
662 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
664 } while (i < (SECTOR_WORDS * 2));
667 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
670 static int proc_idedisk_read_smart_values
671 (char *page, char **start, off_t off, int count, int *eof, void *data)
673 ide_drive_t *drive = (ide_drive_t *)data;
676 if (!get_smart_values(drive, page)) {
677 unsigned short *val = (unsigned short *) page;
678 char *out = ((char *)val) + (SECTOR_WORDS * 4);
681 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
683 } while (i < (SECTOR_WORDS * 2));
686 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
689 static ide_proc_entry_t idedisk_proc[] = {
690 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
691 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
692 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
693 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
694 { NULL, 0, NULL, NULL }
699 #define idedisk_proc NULL
701 #endif /* CONFIG_PROC_FS */
703 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
704 sector_t *error_sector)
706 ide_drive_t *drive = q->queuedata;
713 rq = blk_get_request(q, WRITE, __GFP_WAIT);
715 memset(rq->cmd, 0, sizeof(rq->cmd));
717 if (ide_id_has_flush_cache_ext(drive->id) &&
718 (drive->capacity64 >= (1UL << 28)))
719 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
721 rq->cmd[0] = WIN_FLUSH_CACHE;
724 rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
725 rq->buffer = rq->cmd;
727 ret = blk_execute_rq(q, disk, rq);
730 * if we failed and caller wants error offset, get it
732 if (ret && error_sector)
733 *error_sector = ide_get_error_location(drive, rq->cmd);
740 * This is tightly woven into the driver->do_special can not touch.
741 * DON'T do it again until a total personality rewrite is committed.
743 static int set_multcount(ide_drive_t *drive, int arg)
747 if (drive->special.b.set_multmode)
749 ide_init_drive_cmd (&rq);
750 rq.flags = REQ_DRIVE_CMD;
751 drive->mult_req = arg;
752 drive->special.b.set_multmode = 1;
753 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
754 return (drive->mult_count == arg) ? 0 : -EIO;
757 static int set_nowerr(ide_drive_t *drive, int arg)
759 if (ide_spin_wait_hwgroup(drive))
762 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
763 spin_unlock_irq(&ide_lock);
767 static int write_cache(ide_drive_t *drive, int arg)
772 if (!ide_id_has_flush_cache(drive->id))
775 memset(&args, 0, sizeof(ide_task_t));
776 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
777 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
778 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
779 args.command_type = IDE_DRIVE_TASK_NO_DATA;
780 args.handler = &task_no_data_intr;
782 err = ide_raw_taskfile(drive, &args, NULL);
790 static int do_idedisk_flushcache (ide_drive_t *drive)
794 memset(&args, 0, sizeof(ide_task_t));
795 if (ide_id_has_flush_cache_ext(drive->id))
796 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
798 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
799 args.command_type = IDE_DRIVE_TASK_NO_DATA;
800 args.handler = &task_no_data_intr;
801 return ide_raw_taskfile(drive, &args, NULL);
804 static int set_acoustic (ide_drive_t *drive, int arg)
808 memset(&args, 0, sizeof(ide_task_t));
809 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
811 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
812 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
813 args.command_type = IDE_DRIVE_TASK_NO_DATA;
814 args.handler = &task_no_data_intr;
815 ide_raw_taskfile(drive, &args, NULL);
816 drive->acoustic = arg;
824 * 2: 48-bit capable doing 28-bit
826 static int set_lba_addressing(ide_drive_t *drive, int arg)
828 drive->addressing = 0;
830 if (HWIF(drive)->no_lba48)
833 if (!idedisk_supports_lba48(drive->id))
835 drive->addressing = arg;
839 static void idedisk_add_settings(ide_drive_t *drive)
841 struct hd_driveid *id = drive->id;
843 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
844 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
845 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
846 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
847 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
848 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);
849 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
850 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
851 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
852 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
853 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
854 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
858 * Power Management state machine. This one is rather trivial for now,
859 * we should probably add more, like switching back to PIO on suspend
860 * to help some BIOSes, re-do the door locking on resume, etc...
864 idedisk_pm_flush_cache = ide_pm_state_start_suspend,
867 idedisk_pm_idle = ide_pm_state_start_resume,
868 idedisk_pm_restore_dma,
871 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
873 switch (rq->pm->pm_step) {
874 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
875 if (rq->pm->pm_state == 4)
876 rq->pm->pm_step = ide_pm_state_completed;
878 rq->pm->pm_step = idedisk_pm_standby;
880 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
881 rq->pm->pm_step = ide_pm_state_completed;
883 case idedisk_pm_idle: /* Resume step 1 (idle) complete */
884 rq->pm->pm_step = idedisk_pm_restore_dma;
889 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
891 ide_task_t *args = rq->special;
893 memset(args, 0, sizeof(*args));
895 switch (rq->pm->pm_step) {
896 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) */
897 /* Not supported? Switch to next step now. */
898 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
899 idedisk_complete_power_step(drive, rq, 0, 0);
902 if (ide_id_has_flush_cache_ext(drive->id))
903 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
905 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
906 args->command_type = IDE_DRIVE_TASK_NO_DATA;
907 args->handler = &task_no_data_intr;
908 return do_rw_taskfile(drive, args);
910 case idedisk_pm_standby: /* Suspend step 2 (standby) */
911 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
912 args->command_type = IDE_DRIVE_TASK_NO_DATA;
913 args->handler = &task_no_data_intr;
914 return do_rw_taskfile(drive, args);
916 case idedisk_pm_idle: /* Resume step 1 (idle) */
917 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
918 args->command_type = IDE_DRIVE_TASK_NO_DATA;
919 args->handler = task_no_data_intr;
920 return do_rw_taskfile(drive, args);
922 case idedisk_pm_restore_dma: /* Resume step 2 (restore DMA) */
924 * Right now, all we do is call hwif->ide_dma_check(drive),
925 * we could be smarter and check for current xfer_speed
926 * in struct drive etc...
927 * Also, this step could be implemented as a generic helper
928 * as most subdrivers will use it
930 if ((drive->id->capability & 1) == 0)
932 if (HWIF(drive)->ide_dma_check == NULL)
934 HWIF(drive)->ide_dma_check(drive);
937 rq->pm->pm_step = ide_pm_state_completed;
941 static void idedisk_setup (ide_drive_t *drive)
943 struct hd_driveid *id = drive->id;
944 unsigned long long capacity;
947 idedisk_add_settings(drive);
949 if (drive->id_read == 0)
953 * CompactFlash cards and their brethern look just like hard drives
954 * to us, but they are removable and don't have a doorlock mechanism.
956 if (drive->removable && !(drive->is_flash)) {
958 * Removable disks (eg. SYQUEST); ignore 'WD' drives
960 if (id->model[0] != 'W' || id->model[1] != 'D') {
961 drive->doorlocking = 1;
965 (void)set_lba_addressing(drive, 1);
967 if (drive->addressing == 1) {
968 ide_hwif_t *hwif = HWIF(drive);
971 if (max_s > hwif->rqsize)
972 max_s = hwif->rqsize;
974 blk_queue_max_sectors(drive->queue, max_s);
977 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
979 /* Extract geometry if we did not already have one for the drive */
980 if (!drive->cyl || !drive->head || !drive->sect) {
981 drive->cyl = drive->bios_cyl = id->cyls;
982 drive->head = drive->bios_head = id->heads;
983 drive->sect = drive->bios_sect = id->sectors;
986 /* Handle logical geometry translation by the drive */
987 if ((id->field_valid & 1) && id->cur_cyls &&
988 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
989 drive->cyl = id->cur_cyls;
990 drive->head = id->cur_heads;
991 drive->sect = id->cur_sectors;
994 /* Use physical geometry if what we have still makes no sense */
995 if (drive->head > 16 && id->heads && id->heads <= 16) {
996 drive->cyl = id->cyls;
997 drive->head = id->heads;
998 drive->sect = id->sectors;
1001 /* calculate drive capacity, and select LBA if possible */
1002 init_idedisk_capacity (drive);
1004 /* limit drive capacity to 137GB if LBA48 cannot be used */
1005 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1006 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
1007 "%llu sectors (%llu MB)\n",
1008 drive->name, (unsigned long long)drive->capacity64,
1009 sectors_to_MB(drive->capacity64));
1010 drive->capacity64 = 1ULL << 28;
1013 if (drive->hwif->no_lba48_dma && drive->addressing) {
1014 if (drive->capacity64 > 1ULL << 28) {
1015 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
1016 " be used for accessing sectors > %u\n",
1017 drive->name, 1 << 28);
1019 drive->addressing = 0;
1023 * if possible, give fdisk access to more of the drive,
1024 * by correcting bios_cyls:
1026 capacity = idedisk_capacity (drive);
1027 if (!drive->forced_geom) {
1029 if (idedisk_supports_lba48(drive->id)) {
1031 drive->bios_sect = 63;
1032 drive->bios_head = 255;
1035 if (drive->bios_sect && drive->bios_head) {
1036 unsigned int cap0 = capacity; /* truncate to 32 bits */
1037 unsigned int cylsz, cyl;
1039 if (cap0 != capacity)
1040 drive->bios_cyl = 65535;
1042 cylsz = drive->bios_sect * drive->bios_head;
1046 if (cyl > drive->bios_cyl)
1047 drive->bios_cyl = cyl;
1051 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1052 drive->name, capacity, sectors_to_MB(capacity));
1054 /* Only print cache size when it was specified */
1056 printk (" w/%dKiB Cache", id->buf_size/2);
1058 printk(", CHS=%d/%d/%d",
1059 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1060 if (drive->using_dma)
1061 ide_dma_verbose(drive);
1064 drive->mult_count = 0;
1065 if (id->max_multsect) {
1066 #ifdef CONFIG_IDEDISK_MULTI_MODE
1067 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1068 id->multsect_valid = id->multsect ? 1 : 0;
1069 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1070 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1071 #else /* original, pre IDE-NFG, per request of AC */
1072 drive->mult_req = INITIAL_MULT_COUNT;
1073 if (drive->mult_req > id->max_multsect)
1074 drive->mult_req = id->max_multsect;
1075 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1076 drive->special.b.set_multmode = 1;
1077 #endif /* CONFIG_IDEDISK_MULTI_MODE */
1079 drive->no_io_32bit = id->dword_io ? 1 : 0;
1081 /* write cache enabled? */
1082 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1085 write_cache(drive, 1);
1088 * We must avoid issuing commands a drive does not understand
1089 * or we may crash it. We check flush cache is supported. We also
1090 * check we have the LBA48 flush cache if the drive capacity is
1091 * too large. By this time we have trimmed the drive capacity if
1092 * LBA48 is not available so we don't need to recheck that.
1095 if (ide_id_has_flush_cache(id))
1097 if (drive->addressing == 1) {
1098 /* Can't issue the correct flush ? */
1099 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1103 printk(KERN_DEBUG "%s: cache flushes %ssupported\n",
1104 drive->name, barrier ? "" : "not ");
1106 blk_queue_ordered(drive->queue, 1);
1107 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1111 static void ide_cacheflush_p(ide_drive_t *drive)
1113 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1116 if (do_idedisk_flushcache(drive))
1117 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1120 static int idedisk_cleanup (ide_drive_t *drive)
1122 struct gendisk *g = drive->disk;
1123 ide_cacheflush_p(drive);
1124 if (ide_unregister_subdriver(drive))
1127 drive->devfs_name[0] = '\0';
1132 static int idedisk_attach(ide_drive_t *drive);
1134 static void ide_device_shutdown(struct device *dev)
1136 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1139 /* On Alpha, halt(8) doesn't actually turn the machine off,
1140 it puts you into the sort of firmware monitor. Typically,
1141 it's used to boot another kernel image, so it's not much
1142 different from reboot(8). Therefore, we don't need to
1143 spin down the disk in this case, especially since Alpha
1144 firmware doesn't handle disks in standby mode properly.
1145 On the other hand, it's reasonably safe to turn the power
1146 off when the shutdown process reaches the firmware prompt,
1147 as the firmware initialization takes rather long time -
1148 at least 10 seconds, which should be sufficient for
1149 the disk to expire its write cache. */
1150 if (system_state != SYSTEM_POWER_OFF) {
1152 if (system_state == SYSTEM_RESTART) {
1154 ide_cacheflush_p(drive);
1158 printk("Shutdown: %s\n", drive->name);
1159 dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1163 * IDE subdriver functions, registered with ide.c
1165 static ide_driver_t idedisk_driver = {
1166 .owner = THIS_MODULE,
1168 .shutdown = ide_device_shutdown,
1171 .version = IDEDISK_VERSION,
1174 .supports_dsc_overlap = 0,
1175 .cleanup = idedisk_cleanup,
1176 .do_request = ide_do_rw_disk,
1177 .pre_reset = idedisk_pre_reset,
1178 .capacity = idedisk_capacity,
1179 .special = idedisk_special,
1180 .proc = idedisk_proc,
1181 .attach = idedisk_attach,
1182 .drives = LIST_HEAD_INIT(idedisk_driver.drives),
1183 .start_power_step = idedisk_start_power_step,
1184 .complete_power_step = idedisk_complete_power_step,
1187 static int idedisk_open(struct inode *inode, struct file *filp)
1189 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1191 if (drive->removable && drive->usage == 1) {
1193 memset(&args, 0, sizeof(ide_task_t));
1194 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1195 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1196 args.handler = &task_no_data_intr;
1197 check_disk_change(inode->i_bdev);
1199 * Ignore the return code from door_lock,
1200 * since the open() has already succeeded,
1201 * and the door_lock is irrelevant at this point.
1203 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1204 drive->doorlocking = 0;
1209 static int idedisk_release(struct inode *inode, struct file *filp)
1211 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1212 if (drive->usage == 1)
1213 ide_cacheflush_p(drive);
1214 if (drive->removable && drive->usage == 1) {
1216 memset(&args, 0, sizeof(ide_task_t));
1217 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1218 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1219 args.handler = &task_no_data_intr;
1220 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1221 drive->doorlocking = 0;
1227 static int idedisk_ioctl(struct inode *inode, struct file *file,
1228 unsigned int cmd, unsigned long arg)
1230 struct block_device *bdev = inode->i_bdev;
1231 return generic_ide_ioctl(file, bdev, cmd, arg);
1234 static int idedisk_media_changed(struct gendisk *disk)
1236 ide_drive_t *drive = disk->private_data;
1238 /* do not scan partitions twice if this is a removable device */
1239 if (drive->attach) {
1243 /* if removable, always assume it was changed */
1244 return drive->removable;
1247 static int idedisk_revalidate_disk(struct gendisk *disk)
1249 ide_drive_t *drive = disk->private_data;
1250 set_capacity(disk, idedisk_capacity(drive));
1254 static struct block_device_operations idedisk_ops = {
1255 .owner = THIS_MODULE,
1256 .open = idedisk_open,
1257 .release = idedisk_release,
1258 .ioctl = idedisk_ioctl,
1259 .media_changed = idedisk_media_changed,
1260 .revalidate_disk= idedisk_revalidate_disk
1263 MODULE_DESCRIPTION("ATA DISK Driver");
1265 static int idedisk_attach(ide_drive_t *drive)
1267 struct gendisk *g = drive->disk;
1269 /* strstr("foo", "") is non-NULL */
1270 if (!strstr("ide-disk", drive->driver_req))
1272 if (!drive->present)
1274 if (drive->media != ide_disk)
1277 if (ide_register_subdriver(drive, &idedisk_driver)) {
1278 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1281 DRIVER(drive)->busy++;
1282 idedisk_setup(drive);
1283 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1284 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1285 drive->name, drive->head);
1289 DRIVER(drive)->busy--;
1290 g->minors = 1 << PARTN_BITS;
1291 strcpy(g->devfs_name, drive->devfs_name);
1292 g->driverfs_dev = &drive->gendev;
1293 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1294 set_capacity(g, idedisk_capacity(drive));
1295 g->fops = &idedisk_ops;
1302 static void __exit idedisk_exit (void)
1304 ide_unregister_driver(&idedisk_driver);
1307 static int idedisk_init (void)
1309 return ide_register_driver(&idedisk_driver);
1312 module_init(idedisk_init);
1313 module_exit(idedisk_exit);
1314 MODULE_LICENSE("GPL");