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 standby, do flush on ATA < ATA6
44 #define IDEDISK_VERSION "1.18"
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/string.h>
51 #include <linux/kernel.h>
52 #include <linux/timer.h>
54 #include <linux/interrupt.h>
55 #include <linux/major.h>
56 #include <linux/errno.h>
57 #include <linux/genhd.h>
58 #include <linux/slab.h>
59 #include <linux/delay.h>
60 #include <linux/mutex.h>
61 #include <linux/leds.h>
65 #include <linux/ide.h>
67 #include <asm/byteorder.h>
69 #include <asm/uaccess.h>
71 #include <asm/div64.h>
78 unsigned int openers; /* protected by BKL for now */
81 static DEFINE_MUTEX(idedisk_ref_mutex);
83 #define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref)
85 #define ide_disk_g(disk) \
86 container_of((disk)->private_data, struct ide_disk_obj, driver)
88 static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
90 struct ide_disk_obj *idkp = NULL;
92 mutex_lock(&idedisk_ref_mutex);
93 idkp = ide_disk_g(disk);
95 kref_get(&idkp->kref);
96 mutex_unlock(&idedisk_ref_mutex);
100 static void ide_disk_release(struct kref *);
102 static void ide_disk_put(struct ide_disk_obj *idkp)
104 mutex_lock(&idedisk_ref_mutex);
105 kref_put(&idkp->kref, ide_disk_release);
106 mutex_unlock(&idedisk_ref_mutex);
110 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
111 * value for this drive (from its reported identification information).
113 * Returns: 1 if lba_capacity looks sensible
116 * It is called only once for each drive.
118 static int lba_capacity_is_ok (struct hd_driveid *id)
120 unsigned long lba_sects, chs_sects, head, tail;
122 /* No non-LBA info .. so valid! */
127 * The ATA spec tells large drives to return
128 * C/H/S = 16383/16/63 independent of their size.
129 * Some drives can be jumpered to use 15 heads instead of 16.
130 * Some drives can be jumpered to use 4092 cyls instead of 16383.
132 if ((id->cyls == 16383
133 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
135 (id->heads == 15 || id->heads == 16) &&
136 (id->lba_capacity >= 16383*63*id->heads))
139 lba_sects = id->lba_capacity;
140 chs_sects = id->cyls * id->heads * id->sectors;
142 /* perform a rough sanity check on lba_sects: within 10% is OK */
143 if ((lba_sects - chs_sects) < chs_sects/10)
146 /* some drives have the word order reversed */
147 head = ((lba_sects >> 16) & 0xffff);
148 tail = (lba_sects & 0xffff);
149 lba_sects = (head | (tail << 16));
150 if ((lba_sects - chs_sects) < chs_sects/10) {
151 id->lba_capacity = lba_sects;
152 return 1; /* lba_capacity is (now) good */
155 return 0; /* lba_capacity value may be bad */
159 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
160 * using LBA if supported, or CHS otherwise, to address sectors.
162 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
164 ide_hwif_t *hwif = HWIF(drive);
165 unsigned int dma = drive->using_dma;
166 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
167 task_ioreg_t command = WIN_NOP;
168 ata_nsector_t nsectors;
170 nsectors.all = (u16) rq->nr_sectors;
172 if (hwif->no_lba48_dma && lba48 && dma) {
173 if (block + rq->nr_sectors > 1ULL << 28)
180 ide_init_sg_cmd(drive, rq);
181 ide_map_sg(drive, rq);
185 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
187 /* FIXME: SELECT_MASK(drive, 0) ? */
189 if (drive->select.b.lba) {
191 task_ioreg_t tasklets[10];
193 pr_debug("%s: LBA=0x%012llx\n", drive->name,
194 (unsigned long long)block);
198 tasklets[2] = nsectors.b.low;
199 tasklets[3] = nsectors.b.high;
200 tasklets[4] = (task_ioreg_t) block;
201 tasklets[5] = (task_ioreg_t) (block>>8);
202 tasklets[6] = (task_ioreg_t) (block>>16);
203 tasklets[7] = (task_ioreg_t) (block>>24);
204 if (sizeof(block) == 4) {
205 tasklets[8] = (task_ioreg_t) 0;
206 tasklets[9] = (task_ioreg_t) 0;
208 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
209 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
212 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
213 drive->name, tasklets[3], tasklets[2],
214 tasklets[9], tasklets[8], tasklets[7],
215 tasklets[6], tasklets[5], tasklets[4]);
217 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
218 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
219 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
220 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
221 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
223 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
224 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
225 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
226 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
227 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
228 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
230 hwif->OUTB(0x00, IDE_FEATURE_REG);
231 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
232 hwif->OUTB(block, IDE_SECTOR_REG);
233 hwif->OUTB(block>>=8, IDE_LCYL_REG);
234 hwif->OUTB(block>>=8, IDE_HCYL_REG);
235 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
238 unsigned int sect,head,cyl,track;
239 track = (int)block / drive->sect;
240 sect = (int)block % drive->sect + 1;
241 hwif->OUTB(sect, IDE_SECTOR_REG);
242 head = track % drive->head;
243 cyl = track / drive->head;
245 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
247 hwif->OUTB(0x00, IDE_FEATURE_REG);
248 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
249 hwif->OUTB(cyl, IDE_LCYL_REG);
250 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
251 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
255 if (!hwif->dma_setup(drive)) {
256 if (rq_data_dir(rq)) {
257 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
259 command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
261 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
263 command = lba48 ? WIN_READ_EXT: WIN_READ;
265 hwif->dma_exec_cmd(drive, command);
266 hwif->dma_start(drive);
269 /* fallback to PIO */
270 ide_init_sg_cmd(drive, rq);
273 if (rq_data_dir(rq) == READ) {
275 if (drive->mult_count) {
276 hwif->data_phase = TASKFILE_MULTI_IN;
277 command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
279 hwif->data_phase = TASKFILE_IN;
280 command = lba48 ? WIN_READ_EXT : WIN_READ;
283 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
286 if (drive->mult_count) {
287 hwif->data_phase = TASKFILE_MULTI_OUT;
288 command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
290 hwif->data_phase = TASKFILE_OUT;
291 command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
294 /* FIXME: ->OUTBSYNC ? */
295 hwif->OUTB(command, IDE_COMMAND_REG);
297 return pre_task_out_intr(drive, rq);
302 * 268435455 == 137439 MB or 28bit limit
303 * 320173056 == 163929 MB or 48bit addressing
304 * 1073741822 == 549756 MB or 48bit addressing fake drive
307 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
309 ide_hwif_t *hwif = HWIF(drive);
311 BUG_ON(drive->blocked);
313 if (!blk_fs_request(rq)) {
314 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
315 ide_end_request(drive, 0, 0);
319 ledtrig_ide_activity();
321 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
322 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
323 (unsigned long long)block, rq->nr_sectors,
324 (unsigned long)rq->buffer);
327 hwif->rw_disk(drive, rq);
329 return __ide_do_rw_disk(drive, rq, block);
333 * Queries for true maximum capacity of the drive.
334 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
336 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
339 unsigned long addr = 0;
341 /* Create IDE/ATA command request structure */
342 memset(&args, 0, sizeof(ide_task_t));
343 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
344 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
345 args.command_type = IDE_DRIVE_TASK_NO_DATA;
346 args.handler = &task_no_data_intr;
347 /* submit command request */
348 ide_raw_taskfile(drive, &args, NULL);
350 /* if OK, compute maximum address value */
351 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
352 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
353 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
354 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
355 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
356 addr++; /* since the return value is (maxlba - 1), we add 1 */
361 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
364 unsigned long long addr = 0;
366 /* Create IDE/ATA command request structure */
367 memset(&args, 0, sizeof(ide_task_t));
369 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
370 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
371 args.command_type = IDE_DRIVE_TASK_NO_DATA;
372 args.handler = &task_no_data_intr;
373 /* submit command request */
374 ide_raw_taskfile(drive, &args, NULL);
376 /* if OK, compute maximum address value */
377 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
378 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
379 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
380 args.hobRegister[IDE_SECTOR_OFFSET];
381 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
382 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
383 (args.tfRegister[IDE_SECTOR_OFFSET]);
384 addr = ((__u64)high << 24) | low;
385 addr++; /* since the return value is (maxlba - 1), we add 1 */
391 * Sets maximum virtual LBA address of the drive.
392 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
394 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
397 unsigned long addr_set = 0;
400 /* Create IDE/ATA command request structure */
401 memset(&args, 0, sizeof(ide_task_t));
402 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
403 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
404 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
405 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
406 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
407 args.command_type = IDE_DRIVE_TASK_NO_DATA;
408 args.handler = &task_no_data_intr;
409 /* submit command request */
410 ide_raw_taskfile(drive, &args, NULL);
411 /* if OK, read new maximum address value */
412 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
413 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
414 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
415 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
416 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
422 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
425 unsigned long long addr_set = 0;
428 /* Create IDE/ATA command request structure */
429 memset(&args, 0, sizeof(ide_task_t));
430 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
431 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
432 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
433 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
434 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
435 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
436 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
437 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
438 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
439 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
440 args.command_type = IDE_DRIVE_TASK_NO_DATA;
441 args.handler = &task_no_data_intr;
442 /* submit command request */
443 ide_raw_taskfile(drive, &args, NULL);
444 /* if OK, compute maximum address value */
445 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
446 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
447 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
448 args.hobRegister[IDE_SECTOR_OFFSET];
449 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
450 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
451 (args.tfRegister[IDE_SECTOR_OFFSET]);
452 addr_set = ((__u64)high << 24) | low;
458 static unsigned long long sectors_to_MB(unsigned long long n)
460 n <<= 9; /* make it bytes */
461 do_div(n, 1000000); /* make it MB */
466 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
467 * so on non-buggy drives we need test only one.
468 * However, we should also check whether these fields are valid.
470 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
472 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
478 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
480 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
481 && id->lba_capacity_2;
484 static void idedisk_check_hpa(ide_drive_t *drive)
486 unsigned long long capacity, set_max;
487 int lba48 = idedisk_supports_lba48(drive->id);
489 capacity = drive->capacity64;
491 set_max = idedisk_read_native_max_address_ext(drive);
493 set_max = idedisk_read_native_max_address(drive);
495 if (set_max <= capacity)
498 printk(KERN_INFO "%s: Host Protected Area detected.\n"
499 "\tcurrent capacity is %llu sectors (%llu MB)\n"
500 "\tnative capacity is %llu sectors (%llu MB)\n",
502 capacity, sectors_to_MB(capacity),
503 set_max, sectors_to_MB(set_max));
506 set_max = idedisk_set_max_address_ext(drive, set_max);
508 set_max = idedisk_set_max_address(drive, set_max);
510 drive->capacity64 = set_max;
511 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
517 * Compute drive->capacity, the full capacity of the drive
518 * Called with drive->id != NULL.
520 * To compute capacity, this uses either of
522 * 1. CHS value set by user (whatever user sets will be trusted)
523 * 2. LBA value from target drive (require new ATA feature)
524 * 3. LBA value from system BIOS (new one is OK, old one may break)
525 * 4. CHS value from system BIOS (traditional style)
527 * in above order (i.e., if value of higher priority is available,
528 * reset will be ignored).
530 static void init_idedisk_capacity (ide_drive_t *drive)
532 struct hd_driveid *id = drive->id;
534 * If this drive supports the Host Protected Area feature set,
535 * then we may need to change our opinion about the drive's capacity.
537 int hpa = idedisk_supports_hpa(id);
539 if (idedisk_supports_lba48(id)) {
540 /* drive speaks 48-bit LBA */
541 drive->select.b.lba = 1;
542 drive->capacity64 = id->lba_capacity_2;
544 idedisk_check_hpa(drive);
545 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
546 /* drive speaks 28-bit LBA */
547 drive->select.b.lba = 1;
548 drive->capacity64 = id->lba_capacity;
550 idedisk_check_hpa(drive);
552 /* drive speaks boring old 28-bit CHS */
553 drive->capacity64 = drive->cyl * drive->head * drive->sect;
557 static sector_t idedisk_capacity (ide_drive_t *drive)
559 return drive->capacity64 - drive->sect0;
562 #ifdef CONFIG_IDE_PROC_FS
563 static int smart_enable(ide_drive_t *drive)
567 memset(&args, 0, sizeof(ide_task_t));
568 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
569 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
570 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
571 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
572 args.command_type = IDE_DRIVE_TASK_NO_DATA;
573 args.handler = &task_no_data_intr;
574 return ide_raw_taskfile(drive, &args, NULL);
577 static int get_smart_values(ide_drive_t *drive, u8 *buf)
581 memset(&args, 0, sizeof(ide_task_t));
582 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
583 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
584 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
585 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
586 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
587 args.command_type = IDE_DRIVE_TASK_IN;
588 args.data_phase = TASKFILE_IN;
589 args.handler = &task_in_intr;
590 (void) smart_enable(drive);
591 return ide_raw_taskfile(drive, &args, buf);
594 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
597 memset(&args, 0, sizeof(ide_task_t));
598 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
599 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
600 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
601 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
602 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
603 args.command_type = IDE_DRIVE_TASK_IN;
604 args.data_phase = TASKFILE_IN;
605 args.handler = &task_in_intr;
606 (void) smart_enable(drive);
607 return ide_raw_taskfile(drive, &args, buf);
610 static int proc_idedisk_read_cache
611 (char *page, char **start, off_t off, int count, int *eof, void *data)
613 ide_drive_t *drive = (ide_drive_t *) data;
618 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
620 len = sprintf(out,"(none)\n");
621 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
624 static int proc_idedisk_read_capacity
625 (char *page, char **start, off_t off, int count, int *eof, void *data)
627 ide_drive_t*drive = (ide_drive_t *)data;
630 len = sprintf(page,"%llu\n", (long long)idedisk_capacity(drive));
631 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
634 static int proc_idedisk_read_smart_thresholds
635 (char *page, char **start, off_t off, int count, int *eof, void *data)
637 ide_drive_t *drive = (ide_drive_t *)data;
640 if (!get_smart_thresholds(drive, page)) {
641 unsigned short *val = (unsigned short *) page;
642 char *out = ((char *)val) + (SECTOR_WORDS * 4);
645 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
647 } while (i < (SECTOR_WORDS * 2));
650 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
653 static int proc_idedisk_read_smart_values
654 (char *page, char **start, off_t off, int count, int *eof, void *data)
656 ide_drive_t *drive = (ide_drive_t *)data;
659 if (!get_smart_values(drive, page)) {
660 unsigned short *val = (unsigned short *) page;
661 char *out = ((char *)val) + (SECTOR_WORDS * 4);
664 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
666 } while (i < (SECTOR_WORDS * 2));
669 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
672 static ide_proc_entry_t idedisk_proc[] = {
673 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
674 { "capacity", S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL },
675 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
676 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
677 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
678 { NULL, 0, NULL, NULL }
680 #endif /* CONFIG_IDE_PROC_FS */
682 static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
684 ide_drive_t *drive = q->queuedata;
686 memset(rq->cmd, 0, sizeof(rq->cmd));
688 if (ide_id_has_flush_cache_ext(drive->id) &&
689 (drive->capacity64 >= (1UL << 28)))
690 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
692 rq->cmd[0] = WIN_FLUSH_CACHE;
695 rq->cmd_type = REQ_TYPE_ATA_TASK;
696 rq->cmd_flags |= REQ_SOFTBARRIER;
697 rq->buffer = rq->cmd;
700 static int idedisk_issue_flush(struct request_queue *q, struct gendisk *disk,
701 sector_t *error_sector)
703 ide_drive_t *drive = q->queuedata;
710 rq = blk_get_request(q, WRITE, __GFP_WAIT);
712 idedisk_prepare_flush(q, rq);
714 ret = blk_execute_rq(q, disk, rq, 0);
717 * if we failed and caller wants error offset, get it
719 if (ret && error_sector)
720 *error_sector = ide_get_error_location(drive, rq->cmd);
727 * This is tightly woven into the driver->do_special can not touch.
728 * DON'T do it again until a total personality rewrite is committed.
730 static int set_multcount(ide_drive_t *drive, int arg)
734 if (arg < 0 || arg > drive->id->max_multsect)
737 if (drive->special.b.set_multmode)
739 ide_init_drive_cmd (&rq);
740 rq.cmd_type = REQ_TYPE_ATA_CMD;
741 drive->mult_req = arg;
742 drive->special.b.set_multmode = 1;
743 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
744 return (drive->mult_count == arg) ? 0 : -EIO;
747 static int set_nowerr(ide_drive_t *drive, int arg)
749 if (arg < 0 || arg > 1)
752 if (ide_spin_wait_hwgroup(drive))
755 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
756 spin_unlock_irq(&ide_lock);
760 static void update_ordered(ide_drive_t *drive)
762 struct hd_driveid *id = drive->id;
763 unsigned ordered = QUEUE_ORDERED_NONE;
764 prepare_flush_fn *prep_fn = NULL;
765 issue_flush_fn *issue_fn = NULL;
768 unsigned long long capacity;
771 * We must avoid issuing commands a drive does not
772 * understand or we may crash it. We check flush cache
773 * is supported. We also check we have the LBA48 flush
774 * cache if the drive capacity is too large. By this
775 * time we have trimmed the drive capacity if LBA48 is
776 * not available so we don't need to recheck that.
778 capacity = idedisk_capacity(drive);
779 barrier = ide_id_has_flush_cache(id) && !drive->noflush &&
780 (drive->addressing == 0 || capacity <= (1ULL << 28) ||
781 ide_id_has_flush_cache_ext(id));
783 printk(KERN_INFO "%s: cache flushes %ssupported\n",
784 drive->name, barrier ? "" : "not ");
787 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
788 prep_fn = idedisk_prepare_flush;
789 issue_fn = idedisk_issue_flush;
792 ordered = QUEUE_ORDERED_DRAIN;
794 blk_queue_ordered(drive->queue, ordered, prep_fn);
795 blk_queue_issue_flush_fn(drive->queue, issue_fn);
798 static int write_cache(ide_drive_t *drive, int arg)
803 if (arg < 0 || arg > 1)
806 if (ide_id_has_flush_cache(drive->id)) {
807 memset(&args, 0, sizeof(ide_task_t));
808 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
809 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
810 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
811 args.command_type = IDE_DRIVE_TASK_NO_DATA;
812 args.handler = &task_no_data_intr;
813 err = ide_raw_taskfile(drive, &args, NULL);
818 update_ordered(drive);
823 static int do_idedisk_flushcache (ide_drive_t *drive)
827 memset(&args, 0, sizeof(ide_task_t));
828 if (ide_id_has_flush_cache_ext(drive->id))
829 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
831 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
832 args.command_type = IDE_DRIVE_TASK_NO_DATA;
833 args.handler = &task_no_data_intr;
834 return ide_raw_taskfile(drive, &args, NULL);
837 static int set_acoustic (ide_drive_t *drive, int arg)
841 if (arg < 0 || arg > 254)
844 memset(&args, 0, sizeof(ide_task_t));
845 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
847 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
848 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
849 args.command_type = IDE_DRIVE_TASK_NO_DATA;
850 args.handler = &task_no_data_intr;
851 ide_raw_taskfile(drive, &args, NULL);
852 drive->acoustic = arg;
860 * 2: 48-bit capable doing 28-bit
862 static int set_lba_addressing(ide_drive_t *drive, int arg)
864 if (arg < 0 || arg > 2)
867 drive->addressing = 0;
869 if (HWIF(drive)->no_lba48)
872 if (!idedisk_supports_lba48(drive->id))
874 drive->addressing = arg;
878 #ifdef CONFIG_IDE_PROC_FS
879 static void idedisk_add_settings(ide_drive_t *drive)
881 struct hd_driveid *id = drive->id;
883 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
884 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
885 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
886 ide_add_setting(drive, "address", SETTING_RW, TYPE_BYTE, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
887 ide_add_setting(drive, "bswap", SETTING_READ, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
888 ide_add_setting(drive, "multcount", SETTING_RW, TYPE_BYTE, 0, id->max_multsect, 1, 1, &drive->mult_count, set_multcount);
889 ide_add_setting(drive, "nowerr", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
890 ide_add_setting(drive, "lun", SETTING_RW, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
891 ide_add_setting(drive, "wcache", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
892 ide_add_setting(drive, "acoustic", SETTING_RW, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
893 ide_add_setting(drive, "failures", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
894 ide_add_setting(drive, "max_failures", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
897 static inline void idedisk_add_settings(ide_drive_t *drive) { ; }
900 static void idedisk_setup (ide_drive_t *drive)
902 struct hd_driveid *id = drive->id;
903 unsigned long long capacity;
905 idedisk_add_settings(drive);
907 if (drive->id_read == 0)
910 if (drive->removable) {
912 * Removable disks (eg. SYQUEST); ignore 'WD' drives
914 if (id->model[0] != 'W' || id->model[1] != 'D') {
915 drive->doorlocking = 1;
919 (void)set_lba_addressing(drive, 1);
921 if (drive->addressing == 1) {
922 ide_hwif_t *hwif = HWIF(drive);
925 if (max_s > hwif->rqsize)
926 max_s = hwif->rqsize;
928 blk_queue_max_sectors(drive->queue, max_s);
931 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
933 /* calculate drive capacity, and select LBA if possible */
934 init_idedisk_capacity (drive);
936 /* limit drive capacity to 137GB if LBA48 cannot be used */
937 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
938 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
939 "%llu sectors (%llu MB)\n",
940 drive->name, (unsigned long long)drive->capacity64,
941 sectors_to_MB(drive->capacity64));
942 drive->capacity64 = 1ULL << 28;
945 if (drive->hwif->no_lba48_dma && drive->addressing) {
946 if (drive->capacity64 > 1ULL << 28) {
947 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
948 " be used for accessing sectors > %u\n",
949 drive->name, 1 << 28);
951 drive->addressing = 0;
955 * if possible, give fdisk access to more of the drive,
956 * by correcting bios_cyls:
958 capacity = idedisk_capacity (drive);
959 if (!drive->forced_geom) {
961 if (idedisk_supports_lba48(drive->id)) {
963 drive->bios_sect = 63;
964 drive->bios_head = 255;
967 if (drive->bios_sect && drive->bios_head) {
968 unsigned int cap0 = capacity; /* truncate to 32 bits */
969 unsigned int cylsz, cyl;
971 if (cap0 != capacity)
972 drive->bios_cyl = 65535;
974 cylsz = drive->bios_sect * drive->bios_head;
978 if (cyl > drive->bios_cyl)
979 drive->bios_cyl = cyl;
983 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
984 drive->name, capacity, sectors_to_MB(capacity));
986 /* Only print cache size when it was specified */
988 printk (" w/%dKiB Cache", id->buf_size/2);
990 printk(", CHS=%d/%d/%d",
991 drive->bios_cyl, drive->bios_head, drive->bios_sect);
992 if (drive->using_dma)
993 ide_dma_verbose(drive);
996 /* write cache enabled? */
997 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1000 write_cache(drive, 1);
1003 static void ide_cacheflush_p(ide_drive_t *drive)
1005 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1008 if (do_idedisk_flushcache(drive))
1009 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1012 static void ide_disk_remove(ide_drive_t *drive)
1014 struct ide_disk_obj *idkp = drive->driver_data;
1015 struct gendisk *g = idkp->disk;
1017 ide_proc_unregister_driver(drive, idkp->driver);
1021 ide_cacheflush_p(drive);
1026 static void ide_disk_release(struct kref *kref)
1028 struct ide_disk_obj *idkp = to_ide_disk(kref);
1029 ide_drive_t *drive = idkp->drive;
1030 struct gendisk *g = idkp->disk;
1032 drive->driver_data = NULL;
1033 g->private_data = NULL;
1038 static int ide_disk_probe(ide_drive_t *drive);
1041 * On HPA drives the capacity needs to be
1042 * reinitilized on resume otherwise the disk
1043 * can not be used and a hard reset is required
1045 static void ide_disk_resume(ide_drive_t *drive)
1047 if (idedisk_supports_hpa(drive->id))
1048 init_idedisk_capacity(drive);
1051 static void ide_device_shutdown(ide_drive_t *drive)
1054 /* On Alpha, halt(8) doesn't actually turn the machine off,
1055 it puts you into the sort of firmware monitor. Typically,
1056 it's used to boot another kernel image, so it's not much
1057 different from reboot(8). Therefore, we don't need to
1058 spin down the disk in this case, especially since Alpha
1059 firmware doesn't handle disks in standby mode properly.
1060 On the other hand, it's reasonably safe to turn the power
1061 off when the shutdown process reaches the firmware prompt,
1062 as the firmware initialization takes rather long time -
1063 at least 10 seconds, which should be sufficient for
1064 the disk to expire its write cache. */
1065 if (system_state != SYSTEM_POWER_OFF) {
1067 if (system_state == SYSTEM_RESTART) {
1069 ide_cacheflush_p(drive);
1073 printk("Shutdown: %s\n", drive->name);
1074 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
1077 static ide_driver_t idedisk_driver = {
1079 .owner = THIS_MODULE,
1081 .bus = &ide_bus_type,
1083 .probe = ide_disk_probe,
1084 .remove = ide_disk_remove,
1085 .resume = ide_disk_resume,
1086 .shutdown = ide_device_shutdown,
1087 .version = IDEDISK_VERSION,
1089 .supports_dsc_overlap = 0,
1090 .do_request = ide_do_rw_disk,
1091 .end_request = ide_end_request,
1092 .error = __ide_error,
1093 .abort = __ide_abort,
1094 #ifdef CONFIG_IDE_PROC_FS
1095 .proc = idedisk_proc,
1099 static int idedisk_open(struct inode *inode, struct file *filp)
1101 struct gendisk *disk = inode->i_bdev->bd_disk;
1102 struct ide_disk_obj *idkp;
1105 if (!(idkp = ide_disk_get(disk)))
1108 drive = idkp->drive;
1112 if (drive->removable && idkp->openers == 1) {
1114 memset(&args, 0, sizeof(ide_task_t));
1115 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1116 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1117 args.handler = &task_no_data_intr;
1118 check_disk_change(inode->i_bdev);
1120 * Ignore the return code from door_lock,
1121 * since the open() has already succeeded,
1122 * and the door_lock is irrelevant at this point.
1124 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1125 drive->doorlocking = 0;
1130 static int idedisk_release(struct inode *inode, struct file *filp)
1132 struct gendisk *disk = inode->i_bdev->bd_disk;
1133 struct ide_disk_obj *idkp = ide_disk_g(disk);
1134 ide_drive_t *drive = idkp->drive;
1136 if (idkp->openers == 1)
1137 ide_cacheflush_p(drive);
1139 if (drive->removable && idkp->openers == 1) {
1141 memset(&args, 0, sizeof(ide_task_t));
1142 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1143 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1144 args.handler = &task_no_data_intr;
1145 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1146 drive->doorlocking = 0;
1156 static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1158 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1159 ide_drive_t *drive = idkp->drive;
1161 geo->heads = drive->bios_head;
1162 geo->sectors = drive->bios_sect;
1163 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1167 static int idedisk_ioctl(struct inode *inode, struct file *file,
1168 unsigned int cmd, unsigned long arg)
1170 unsigned long flags;
1171 struct block_device *bdev = inode->i_bdev;
1172 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1173 ide_drive_t *drive = idkp->drive;
1174 int err, (*setfunc)(ide_drive_t *, int);
1178 case HDIO_GET_ADDRESS: val = &drive->addressing; goto read_val;
1179 case HDIO_GET_MULTCOUNT: val = &drive->mult_count; goto read_val;
1180 case HDIO_GET_NOWERR: val = &drive->nowerr; goto read_val;
1181 case HDIO_GET_WCACHE: val = &drive->wcache; goto read_val;
1182 case HDIO_GET_ACOUSTIC: val = &drive->acoustic; goto read_val;
1183 case HDIO_SET_ADDRESS: setfunc = set_lba_addressing; goto set_val;
1184 case HDIO_SET_MULTCOUNT: setfunc = set_multcount; goto set_val;
1185 case HDIO_SET_NOWERR: setfunc = set_nowerr; goto set_val;
1186 case HDIO_SET_WCACHE: setfunc = write_cache; goto set_val;
1187 case HDIO_SET_ACOUSTIC: setfunc = set_acoustic; goto set_val;
1190 return generic_ide_ioctl(drive, file, bdev, cmd, arg);
1193 mutex_lock(&ide_setting_mtx);
1194 spin_lock_irqsave(&ide_lock, flags);
1196 spin_unlock_irqrestore(&ide_lock, flags);
1197 mutex_unlock(&ide_setting_mtx);
1198 return err >= 0 ? put_user(err, (long __user *)arg) : err;
1201 if (bdev != bdev->bd_contains)
1204 if (!capable(CAP_SYS_ADMIN))
1207 mutex_lock(&ide_setting_mtx);
1208 err = setfunc(drive, arg);
1209 mutex_unlock(&ide_setting_mtx);
1215 static int idedisk_media_changed(struct gendisk *disk)
1217 struct ide_disk_obj *idkp = ide_disk_g(disk);
1218 ide_drive_t *drive = idkp->drive;
1220 /* do not scan partitions twice if this is a removable device */
1221 if (drive->attach) {
1225 /* if removable, always assume it was changed */
1226 return drive->removable;
1229 static int idedisk_revalidate_disk(struct gendisk *disk)
1231 struct ide_disk_obj *idkp = ide_disk_g(disk);
1232 set_capacity(disk, idedisk_capacity(idkp->drive));
1236 static struct block_device_operations idedisk_ops = {
1237 .owner = THIS_MODULE,
1238 .open = idedisk_open,
1239 .release = idedisk_release,
1240 .ioctl = idedisk_ioctl,
1241 .getgeo = idedisk_getgeo,
1242 .media_changed = idedisk_media_changed,
1243 .revalidate_disk= idedisk_revalidate_disk
1246 MODULE_DESCRIPTION("ATA DISK Driver");
1248 static int ide_disk_probe(ide_drive_t *drive)
1250 struct ide_disk_obj *idkp;
1253 /* strstr("foo", "") is non-NULL */
1254 if (!strstr("ide-disk", drive->driver_req))
1256 if (!drive->present)
1258 if (drive->media != ide_disk)
1261 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
1265 g = alloc_disk_node(1 << PARTN_BITS,
1266 hwif_to_node(drive->hwif));
1270 ide_init_disk(g, drive);
1272 ide_proc_register_driver(drive, &idedisk_driver);
1274 kref_init(&idkp->kref);
1276 idkp->drive = drive;
1277 idkp->driver = &idedisk_driver;
1280 g->private_data = &idkp->driver;
1282 drive->driver_data = idkp;
1284 idedisk_setup(drive);
1285 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1286 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1287 drive->name, drive->head);
1292 g->minors = 1 << PARTN_BITS;
1293 g->driverfs_dev = &drive->gendev;
1294 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1295 set_capacity(g, idedisk_capacity(drive));
1296 g->fops = &idedisk_ops;
1306 static void __exit idedisk_exit (void)
1308 driver_unregister(&idedisk_driver.gen_driver);
1311 static int __init idedisk_init(void)
1313 return driver_register(&idedisk_driver.gen_driver);
1316 MODULE_ALIAS("ide:*m-disk*");
1317 module_init(idedisk_init);
1318 module_exit(idedisk_exit);
1319 MODULE_LICENSE("GPL");