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>
81 static DECLARE_MUTEX(idedisk_ref_sem);
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 down(&idedisk_ref_sem);
93 idkp = ide_disk_g(disk);
95 kref_get(&idkp->kref);
100 static void ide_disk_release(struct kref *);
102 static void ide_disk_put(struct ide_disk_obj *idkp)
104 down(&idedisk_ref_sem);
105 kref_put(&idkp->kref, ide_disk_release);
106 up(&idedisk_ref_sem);
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;
123 * The ATA spec tells large drives to return
124 * C/H/S = 16383/16/63 independent of their size.
125 * Some drives can be jumpered to use 15 heads instead of 16.
126 * Some drives can be jumpered to use 4092 cyls instead of 16383.
128 if ((id->cyls == 16383
129 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
131 (id->heads == 15 || id->heads == 16) &&
132 (id->lba_capacity >= 16383*63*id->heads))
135 lba_sects = id->lba_capacity;
136 chs_sects = id->cyls * id->heads * id->sectors;
138 /* perform a rough sanity check on lba_sects: within 10% is OK */
139 if ((lba_sects - chs_sects) < chs_sects/10)
142 /* some drives have the word order reversed */
143 head = ((lba_sects >> 16) & 0xffff);
144 tail = (lba_sects & 0xffff);
145 lba_sects = (head | (tail << 16));
146 if ((lba_sects - chs_sects) < chs_sects/10) {
147 id->lba_capacity = lba_sects;
148 return 1; /* lba_capacity is (now) good */
151 return 0; /* lba_capacity value may be bad */
155 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
156 * using LBA if supported, or CHS otherwise, to address sectors.
158 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
160 ide_hwif_t *hwif = HWIF(drive);
161 unsigned int dma = drive->using_dma;
162 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
163 task_ioreg_t command = WIN_NOP;
164 ata_nsector_t nsectors;
166 nsectors.all = (u16) rq->nr_sectors;
168 if (hwif->no_lba48_dma && lba48 && dma) {
169 if (block + rq->nr_sectors > 1ULL << 28)
176 ide_init_sg_cmd(drive, rq);
177 ide_map_sg(drive, rq);
181 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
183 /* FIXME: SELECT_MASK(drive, 0) ? */
185 if (drive->select.b.lba) {
187 task_ioreg_t tasklets[10];
189 pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
193 tasklets[2] = nsectors.b.low;
194 tasklets[3] = nsectors.b.high;
195 tasklets[4] = (task_ioreg_t) block;
196 tasklets[5] = (task_ioreg_t) (block>>8);
197 tasklets[6] = (task_ioreg_t) (block>>16);
198 tasklets[7] = (task_ioreg_t) (block>>24);
199 if (sizeof(block) == 4) {
200 tasklets[8] = (task_ioreg_t) 0;
201 tasklets[9] = (task_ioreg_t) 0;
203 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
204 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
207 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
208 drive->name, tasklets[3], tasklets[2],
209 tasklets[9], tasklets[8], tasklets[7],
210 tasklets[6], tasklets[5], tasklets[4]);
212 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
213 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
214 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
215 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
216 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
218 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
219 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
220 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
221 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
222 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
223 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
225 hwif->OUTB(0x00, IDE_FEATURE_REG);
226 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
227 hwif->OUTB(block, IDE_SECTOR_REG);
228 hwif->OUTB(block>>=8, IDE_LCYL_REG);
229 hwif->OUTB(block>>=8, IDE_HCYL_REG);
230 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
233 unsigned int sect,head,cyl,track;
234 track = (int)block / drive->sect;
235 sect = (int)block % drive->sect + 1;
236 hwif->OUTB(sect, IDE_SECTOR_REG);
237 head = track % drive->head;
238 cyl = track / drive->head;
240 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
242 hwif->OUTB(0x00, IDE_FEATURE_REG);
243 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
244 hwif->OUTB(cyl, IDE_LCYL_REG);
245 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
246 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
250 if (!hwif->dma_setup(drive)) {
251 if (rq_data_dir(rq)) {
252 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
254 command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
256 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
258 command = lba48 ? WIN_READ_EXT: WIN_READ;
260 hwif->dma_exec_cmd(drive, command);
261 hwif->dma_start(drive);
264 /* fallback to PIO */
265 ide_init_sg_cmd(drive, rq);
268 if (rq_data_dir(rq) == READ) {
270 if (drive->mult_count) {
271 hwif->data_phase = TASKFILE_MULTI_IN;
272 command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
274 hwif->data_phase = TASKFILE_IN;
275 command = lba48 ? WIN_READ_EXT : WIN_READ;
278 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
281 if (drive->mult_count) {
282 hwif->data_phase = TASKFILE_MULTI_OUT;
283 command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
285 hwif->data_phase = TASKFILE_OUT;
286 command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
289 /* FIXME: ->OUTBSYNC ? */
290 hwif->OUTB(command, IDE_COMMAND_REG);
292 return pre_task_out_intr(drive, rq);
297 * 268435455 == 137439 MB or 28bit limit
298 * 320173056 == 163929 MB or 48bit addressing
299 * 1073741822 == 549756 MB or 48bit addressing fake drive
302 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
304 ide_hwif_t *hwif = HWIF(drive);
306 BUG_ON(drive->blocked);
308 if (!blk_fs_request(rq)) {
309 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
310 ide_end_request(drive, 0, 0);
314 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
315 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
316 block, rq->nr_sectors, (unsigned long)rq->buffer);
319 hwif->rw_disk(drive, rq);
321 return __ide_do_rw_disk(drive, rq, block);
325 * Queries for true maximum capacity of the drive.
326 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
328 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
331 unsigned long addr = 0;
333 /* Create IDE/ATA command request structure */
334 memset(&args, 0, sizeof(ide_task_t));
335 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
336 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
337 args.command_type = IDE_DRIVE_TASK_NO_DATA;
338 args.handler = &task_no_data_intr;
339 /* submit command request */
340 ide_raw_taskfile(drive, &args, NULL);
342 /* if OK, compute maximum address value */
343 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
344 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
345 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
346 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
347 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
348 addr++; /* since the return value is (maxlba - 1), we add 1 */
353 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
356 unsigned long long addr = 0;
358 /* Create IDE/ATA command request structure */
359 memset(&args, 0, sizeof(ide_task_t));
361 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
362 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
363 args.command_type = IDE_DRIVE_TASK_NO_DATA;
364 args.handler = &task_no_data_intr;
365 /* submit command request */
366 ide_raw_taskfile(drive, &args, NULL);
368 /* if OK, compute maximum address value */
369 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
370 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
371 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
372 args.hobRegister[IDE_SECTOR_OFFSET];
373 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
374 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
375 (args.tfRegister[IDE_SECTOR_OFFSET]);
376 addr = ((__u64)high << 24) | low;
377 addr++; /* since the return value is (maxlba - 1), we add 1 */
383 * Sets maximum virtual LBA address of the drive.
384 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
386 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
389 unsigned long addr_set = 0;
392 /* Create IDE/ATA command request structure */
393 memset(&args, 0, sizeof(ide_task_t));
394 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
395 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
396 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
397 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
398 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
399 args.command_type = IDE_DRIVE_TASK_NO_DATA;
400 args.handler = &task_no_data_intr;
401 /* submit command request */
402 ide_raw_taskfile(drive, &args, NULL);
403 /* if OK, read new maximum address value */
404 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
405 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
406 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
407 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
408 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
414 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
417 unsigned long long addr_set = 0;
420 /* Create IDE/ATA command request structure */
421 memset(&args, 0, sizeof(ide_task_t));
422 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
423 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
424 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
425 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
426 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
427 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
428 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
429 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
430 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
431 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
432 args.command_type = IDE_DRIVE_TASK_NO_DATA;
433 args.handler = &task_no_data_intr;
434 /* submit command request */
435 ide_raw_taskfile(drive, &args, NULL);
436 /* if OK, compute maximum address value */
437 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
438 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
439 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
440 args.hobRegister[IDE_SECTOR_OFFSET];
441 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
442 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
443 (args.tfRegister[IDE_SECTOR_OFFSET]);
444 addr_set = ((__u64)high << 24) | low;
450 static unsigned long long sectors_to_MB(unsigned long long n)
452 n <<= 9; /* make it bytes */
453 do_div(n, 1000000); /* make it MB */
458 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
459 * so on non-buggy drives we need test only one.
460 * However, we should also check whether these fields are valid.
462 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
464 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
470 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
472 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
473 && id->lba_capacity_2;
476 static inline void idedisk_check_hpa(ide_drive_t *drive)
478 unsigned long long capacity, set_max;
479 int lba48 = idedisk_supports_lba48(drive->id);
481 capacity = drive->capacity64;
483 set_max = idedisk_read_native_max_address_ext(drive);
485 set_max = idedisk_read_native_max_address(drive);
487 if (set_max <= capacity)
490 printk(KERN_INFO "%s: Host Protected Area detected.\n"
491 "\tcurrent capacity is %llu sectors (%llu MB)\n"
492 "\tnative capacity is %llu sectors (%llu MB)\n",
494 capacity, sectors_to_MB(capacity),
495 set_max, sectors_to_MB(set_max));
498 set_max = idedisk_set_max_address_ext(drive, set_max);
500 set_max = idedisk_set_max_address(drive, set_max);
502 drive->capacity64 = set_max;
503 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
509 * Compute drive->capacity, the full capacity of the drive
510 * Called with drive->id != NULL.
512 * To compute capacity, this uses either of
514 * 1. CHS value set by user (whatever user sets will be trusted)
515 * 2. LBA value from target drive (require new ATA feature)
516 * 3. LBA value from system BIOS (new one is OK, old one may break)
517 * 4. CHS value from system BIOS (traditional style)
519 * in above order (i.e., if value of higher priority is available,
520 * reset will be ignored).
522 static void init_idedisk_capacity (ide_drive_t *drive)
524 struct hd_driveid *id = drive->id;
526 * If this drive supports the Host Protected Area feature set,
527 * then we may need to change our opinion about the drive's capacity.
529 int hpa = idedisk_supports_hpa(id);
531 if (idedisk_supports_lba48(id)) {
532 /* drive speaks 48-bit LBA */
533 drive->select.b.lba = 1;
534 drive->capacity64 = id->lba_capacity_2;
536 idedisk_check_hpa(drive);
537 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
538 /* drive speaks 28-bit LBA */
539 drive->select.b.lba = 1;
540 drive->capacity64 = id->lba_capacity;
542 idedisk_check_hpa(drive);
544 /* drive speaks boring old 28-bit CHS */
545 drive->capacity64 = drive->cyl * drive->head * drive->sect;
549 static sector_t idedisk_capacity (ide_drive_t *drive)
551 return drive->capacity64 - drive->sect0;
554 #ifdef CONFIG_PROC_FS
556 static int smart_enable(ide_drive_t *drive)
560 memset(&args, 0, sizeof(ide_task_t));
561 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
562 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
563 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
564 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
565 args.command_type = IDE_DRIVE_TASK_NO_DATA;
566 args.handler = &task_no_data_intr;
567 return ide_raw_taskfile(drive, &args, NULL);
570 static int get_smart_values(ide_drive_t *drive, u8 *buf)
574 memset(&args, 0, sizeof(ide_task_t));
575 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
576 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
577 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
578 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
579 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
580 args.command_type = IDE_DRIVE_TASK_IN;
581 args.data_phase = TASKFILE_IN;
582 args.handler = &task_in_intr;
583 (void) smart_enable(drive);
584 return ide_raw_taskfile(drive, &args, buf);
587 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
590 memset(&args, 0, sizeof(ide_task_t));
591 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
592 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
593 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
594 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
595 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
596 args.command_type = IDE_DRIVE_TASK_IN;
597 args.data_phase = TASKFILE_IN;
598 args.handler = &task_in_intr;
599 (void) smart_enable(drive);
600 return ide_raw_taskfile(drive, &args, buf);
603 static int proc_idedisk_read_cache
604 (char *page, char **start, off_t off, int count, int *eof, void *data)
606 ide_drive_t *drive = (ide_drive_t *) data;
611 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
613 len = sprintf(out,"(none)\n");
614 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
617 static int proc_idedisk_read_capacity
618 (char *page, char **start, off_t off, int count, int *eof, void *data)
620 ide_drive_t*drive = (ide_drive_t *)data;
623 len = sprintf(page,"%llu\n", (long long)idedisk_capacity(drive));
624 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
627 static int proc_idedisk_read_smart_thresholds
628 (char *page, char **start, off_t off, int count, int *eof, void *data)
630 ide_drive_t *drive = (ide_drive_t *)data;
633 if (!get_smart_thresholds(drive, page)) {
634 unsigned short *val = (unsigned short *) page;
635 char *out = ((char *)val) + (SECTOR_WORDS * 4);
638 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
640 } while (i < (SECTOR_WORDS * 2));
643 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
646 static int proc_idedisk_read_smart_values
647 (char *page, char **start, off_t off, int count, int *eof, void *data)
649 ide_drive_t *drive = (ide_drive_t *)data;
652 if (!get_smart_values(drive, page)) {
653 unsigned short *val = (unsigned short *) page;
654 char *out = ((char *)val) + (SECTOR_WORDS * 4);
657 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
659 } while (i < (SECTOR_WORDS * 2));
662 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
665 static ide_proc_entry_t idedisk_proc[] = {
666 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
667 { "capacity", S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL },
668 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
669 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
670 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
671 { NULL, 0, NULL, NULL }
676 #define idedisk_proc NULL
678 #endif /* CONFIG_PROC_FS */
680 static void idedisk_end_flush(request_queue_t *q, struct request *flush_rq)
682 ide_drive_t *drive = q->queuedata;
683 struct request *rq = flush_rq->end_io_data;
684 int good_sectors = rq->hard_nr_sectors;
688 if (flush_rq->errors & ABRT_ERR) {
689 printk(KERN_ERR "%s: barrier support doesn't work\n", drive->name);
690 blk_queue_ordered(drive->queue, QUEUE_ORDERED_NONE);
691 blk_queue_issue_flush_fn(drive->queue, NULL);
693 } else if (flush_rq->errors) {
695 if (blk_barrier_preflush(rq)) {
696 sector = ide_get_error_location(drive,flush_rq->buffer);
697 if ((sector >= rq->hard_sector) &&
698 (sector < rq->hard_sector + rq->hard_nr_sectors))
699 good_sectors = sector - rq->hard_sector;
703 if (flush_rq->errors)
704 printk(KERN_ERR "%s: failed barrier write: "
705 "sector=%Lx(good=%d/bad=%d)\n",
706 drive->name, (unsigned long long)rq->sector,
708 (int) (rq->hard_nr_sectors-good_sectors));
710 bad_sectors = rq->hard_nr_sectors - good_sectors;
713 __ide_end_request(drive, rq, 1, good_sectors);
715 __ide_end_request(drive, rq, 0, bad_sectors);
718 static int idedisk_prepare_flush(request_queue_t *q, struct request *rq)
720 ide_drive_t *drive = q->queuedata;
725 memset(rq->cmd, 0, sizeof(rq->cmd));
727 if (ide_id_has_flush_cache_ext(drive->id) &&
728 (drive->capacity64 >= (1UL << 28)))
729 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
731 rq->cmd[0] = WIN_FLUSH_CACHE;
734 rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
735 rq->buffer = rq->cmd;
739 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
740 sector_t *error_sector)
742 ide_drive_t *drive = q->queuedata;
749 rq = blk_get_request(q, WRITE, __GFP_WAIT);
751 idedisk_prepare_flush(q, rq);
753 ret = blk_execute_rq(q, disk, rq);
756 * if we failed and caller wants error offset, get it
758 if (ret && error_sector)
759 *error_sector = ide_get_error_location(drive, rq->cmd);
766 * This is tightly woven into the driver->do_special can not touch.
767 * DON'T do it again until a total personality rewrite is committed.
769 static int set_multcount(ide_drive_t *drive, int arg)
773 if (drive->special.b.set_multmode)
775 ide_init_drive_cmd (&rq);
776 rq.flags = REQ_DRIVE_CMD;
777 drive->mult_req = arg;
778 drive->special.b.set_multmode = 1;
779 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
780 return (drive->mult_count == arg) ? 0 : -EIO;
783 static int set_nowerr(ide_drive_t *drive, int arg)
785 if (ide_spin_wait_hwgroup(drive))
788 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
789 spin_unlock_irq(&ide_lock);
793 static int write_cache(ide_drive_t *drive, int arg)
798 if (!ide_id_has_flush_cache(drive->id))
801 memset(&args, 0, sizeof(ide_task_t));
802 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
803 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
804 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
805 args.command_type = IDE_DRIVE_TASK_NO_DATA;
806 args.handler = &task_no_data_intr;
808 err = ide_raw_taskfile(drive, &args, NULL);
816 static int do_idedisk_flushcache (ide_drive_t *drive)
820 memset(&args, 0, sizeof(ide_task_t));
821 if (ide_id_has_flush_cache_ext(drive->id))
822 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
824 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
825 args.command_type = IDE_DRIVE_TASK_NO_DATA;
826 args.handler = &task_no_data_intr;
827 return ide_raw_taskfile(drive, &args, NULL);
830 static int set_acoustic (ide_drive_t *drive, int arg)
834 memset(&args, 0, sizeof(ide_task_t));
835 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
837 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
838 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
839 args.command_type = IDE_DRIVE_TASK_NO_DATA;
840 args.handler = &task_no_data_intr;
841 ide_raw_taskfile(drive, &args, NULL);
842 drive->acoustic = arg;
850 * 2: 48-bit capable doing 28-bit
852 static int set_lba_addressing(ide_drive_t *drive, int arg)
854 drive->addressing = 0;
856 if (HWIF(drive)->no_lba48)
859 if (!idedisk_supports_lba48(drive->id))
861 drive->addressing = arg;
865 static void idedisk_add_settings(ide_drive_t *drive)
867 struct hd_driveid *id = drive->id;
869 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
870 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
871 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
872 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
873 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
874 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);
875 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
876 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
877 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
878 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
879 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
880 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
883 static void idedisk_setup (ide_drive_t *drive)
885 struct hd_driveid *id = drive->id;
886 unsigned long long capacity;
889 idedisk_add_settings(drive);
891 if (drive->id_read == 0)
895 * CompactFlash cards and their brethern look just like hard drives
896 * to us, but they are removable and don't have a doorlock mechanism.
898 if (drive->removable && !(drive->is_flash)) {
900 * Removable disks (eg. SYQUEST); ignore 'WD' drives
902 if (id->model[0] != 'W' || id->model[1] != 'D') {
903 drive->doorlocking = 1;
907 (void)set_lba_addressing(drive, 1);
909 if (drive->addressing == 1) {
910 ide_hwif_t *hwif = HWIF(drive);
913 if (max_s > hwif->rqsize)
914 max_s = hwif->rqsize;
916 blk_queue_max_sectors(drive->queue, max_s);
919 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
921 /* calculate drive capacity, and select LBA if possible */
922 init_idedisk_capacity (drive);
924 /* limit drive capacity to 137GB if LBA48 cannot be used */
925 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
926 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
927 "%llu sectors (%llu MB)\n",
928 drive->name, (unsigned long long)drive->capacity64,
929 sectors_to_MB(drive->capacity64));
930 drive->capacity64 = 1ULL << 28;
933 if (drive->hwif->no_lba48_dma && drive->addressing) {
934 if (drive->capacity64 > 1ULL << 28) {
935 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
936 " be used for accessing sectors > %u\n",
937 drive->name, 1 << 28);
939 drive->addressing = 0;
943 * if possible, give fdisk access to more of the drive,
944 * by correcting bios_cyls:
946 capacity = idedisk_capacity (drive);
947 if (!drive->forced_geom) {
949 if (idedisk_supports_lba48(drive->id)) {
951 drive->bios_sect = 63;
952 drive->bios_head = 255;
955 if (drive->bios_sect && drive->bios_head) {
956 unsigned int cap0 = capacity; /* truncate to 32 bits */
957 unsigned int cylsz, cyl;
959 if (cap0 != capacity)
960 drive->bios_cyl = 65535;
962 cylsz = drive->bios_sect * drive->bios_head;
966 if (cyl > drive->bios_cyl)
967 drive->bios_cyl = cyl;
971 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
972 drive->name, capacity, sectors_to_MB(capacity));
974 /* Only print cache size when it was specified */
976 printk (" w/%dKiB Cache", id->buf_size/2);
978 printk(", CHS=%d/%d/%d",
979 drive->bios_cyl, drive->bios_head, drive->bios_sect);
980 if (drive->using_dma)
981 ide_dma_verbose(drive);
984 drive->no_io_32bit = id->dword_io ? 1 : 0;
986 /* write cache enabled? */
987 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
990 write_cache(drive, 1);
993 * We must avoid issuing commands a drive does not understand
994 * or we may crash it. We check flush cache is supported. We also
995 * check we have the LBA48 flush cache if the drive capacity is
996 * too large. By this time we have trimmed the drive capacity if
997 * LBA48 is not available so we don't need to recheck that.
1000 if (ide_id_has_flush_cache(id))
1002 if (drive->addressing == 1) {
1003 /* Can't issue the correct flush ? */
1004 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1008 printk(KERN_INFO "%s: cache flushes %ssupported\n",
1009 drive->name, barrier ? "" : "not ");
1011 blk_queue_ordered(drive->queue, QUEUE_ORDERED_FLUSH);
1012 drive->queue->prepare_flush_fn = idedisk_prepare_flush;
1013 drive->queue->end_flush_fn = idedisk_end_flush;
1014 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1018 static void ide_cacheflush_p(ide_drive_t *drive)
1020 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1023 if (do_idedisk_flushcache(drive))
1024 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1027 static int idedisk_cleanup (ide_drive_t *drive)
1029 struct ide_disk_obj *idkp = drive->driver_data;
1030 struct gendisk *g = idkp->disk;
1032 ide_cacheflush_p(drive);
1033 if (ide_unregister_subdriver(drive))
1042 static void ide_disk_release(struct kref *kref)
1044 struct ide_disk_obj *idkp = to_ide_disk(kref);
1045 ide_drive_t *drive = idkp->drive;
1046 struct gendisk *g = idkp->disk;
1048 drive->driver_data = NULL;
1049 drive->devfs_name[0] = '\0';
1050 g->private_data = NULL;
1055 static int idedisk_attach(ide_drive_t *drive);
1057 static void ide_device_shutdown(struct device *dev)
1059 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1062 /* On Alpha, halt(8) doesn't actually turn the machine off,
1063 it puts you into the sort of firmware monitor. Typically,
1064 it's used to boot another kernel image, so it's not much
1065 different from reboot(8). Therefore, we don't need to
1066 spin down the disk in this case, especially since Alpha
1067 firmware doesn't handle disks in standby mode properly.
1068 On the other hand, it's reasonably safe to turn the power
1069 off when the shutdown process reaches the firmware prompt,
1070 as the firmware initialization takes rather long time -
1071 at least 10 seconds, which should be sufficient for
1072 the disk to expire its write cache. */
1073 if (system_state != SYSTEM_POWER_OFF) {
1075 if (system_state == SYSTEM_RESTART) {
1077 ide_cacheflush_p(drive);
1081 printk("Shutdown: %s\n", drive->name);
1082 dev->bus->suspend(dev, PMSG_SUSPEND);
1086 * IDE subdriver functions, registered with ide.c
1088 static ide_driver_t idedisk_driver = {
1089 .owner = THIS_MODULE,
1091 .shutdown = ide_device_shutdown,
1094 .version = IDEDISK_VERSION,
1097 .supports_dsc_overlap = 0,
1098 .cleanup = idedisk_cleanup,
1099 .do_request = ide_do_rw_disk,
1100 .end_request = ide_end_request,
1101 .error = __ide_error,
1102 .abort = __ide_abort,
1103 .proc = idedisk_proc,
1104 .attach = idedisk_attach,
1105 .drives = LIST_HEAD_INIT(idedisk_driver.drives),
1108 static int idedisk_open(struct inode *inode, struct file *filp)
1110 struct gendisk *disk = inode->i_bdev->bd_disk;
1111 struct ide_disk_obj *idkp;
1114 if (!(idkp = ide_disk_get(disk)))
1117 drive = idkp->drive;
1120 if (drive->removable && drive->usage == 1) {
1122 memset(&args, 0, sizeof(ide_task_t));
1123 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1124 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1125 args.handler = &task_no_data_intr;
1126 check_disk_change(inode->i_bdev);
1128 * Ignore the return code from door_lock,
1129 * since the open() has already succeeded,
1130 * and the door_lock is irrelevant at this point.
1132 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1133 drive->doorlocking = 0;
1138 static int idedisk_release(struct inode *inode, struct file *filp)
1140 struct gendisk *disk = inode->i_bdev->bd_disk;
1141 struct ide_disk_obj *idkp = ide_disk_g(disk);
1142 ide_drive_t *drive = idkp->drive;
1144 if (drive->usage == 1)
1145 ide_cacheflush_p(drive);
1146 if (drive->removable && drive->usage == 1) {
1148 memset(&args, 0, sizeof(ide_task_t));
1149 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1150 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1151 args.handler = &task_no_data_intr;
1152 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1153 drive->doorlocking = 0;
1162 static int idedisk_ioctl(struct inode *inode, struct file *file,
1163 unsigned int cmd, unsigned long arg)
1165 struct block_device *bdev = inode->i_bdev;
1166 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1167 return generic_ide_ioctl(idkp->drive, file, bdev, cmd, arg);
1170 static int idedisk_media_changed(struct gendisk *disk)
1172 struct ide_disk_obj *idkp = ide_disk_g(disk);
1173 ide_drive_t *drive = idkp->drive;
1175 /* do not scan partitions twice if this is a removable device */
1176 if (drive->attach) {
1180 /* if removable, always assume it was changed */
1181 return drive->removable;
1184 static int idedisk_revalidate_disk(struct gendisk *disk)
1186 struct ide_disk_obj *idkp = ide_disk_g(disk);
1187 set_capacity(disk, idedisk_capacity(idkp->drive));
1191 static struct block_device_operations idedisk_ops = {
1192 .owner = THIS_MODULE,
1193 .open = idedisk_open,
1194 .release = idedisk_release,
1195 .ioctl = idedisk_ioctl,
1196 .media_changed = idedisk_media_changed,
1197 .revalidate_disk= idedisk_revalidate_disk
1200 MODULE_DESCRIPTION("ATA DISK Driver");
1202 static int idedisk_attach(ide_drive_t *drive)
1204 struct ide_disk_obj *idkp;
1207 /* strstr("foo", "") is non-NULL */
1208 if (!strstr("ide-disk", drive->driver_req))
1210 if (!drive->present)
1212 if (drive->media != ide_disk)
1215 idkp = kmalloc(sizeof(*idkp), GFP_KERNEL);
1219 g = alloc_disk(1 << PARTN_BITS);
1223 ide_init_disk(g, drive);
1225 if (ide_register_subdriver(drive, &idedisk_driver)) {
1226 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1230 memset(idkp, 0, sizeof(*idkp));
1232 kref_init(&idkp->kref);
1234 idkp->drive = drive;
1235 idkp->driver = &idedisk_driver;
1238 g->private_data = &idkp->driver;
1240 drive->driver_data = idkp;
1242 DRIVER(drive)->busy++;
1243 idedisk_setup(drive);
1244 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1245 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1246 drive->name, drive->head);
1250 DRIVER(drive)->busy--;
1251 g->minors = 1 << PARTN_BITS;
1252 strcpy(g->devfs_name, drive->devfs_name);
1253 g->driverfs_dev = &drive->gendev;
1254 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1255 set_capacity(g, idedisk_capacity(drive));
1256 g->fops = &idedisk_ops;
1268 static void __exit idedisk_exit (void)
1270 ide_unregister_driver(&idedisk_driver);
1273 static int idedisk_init (void)
1275 return ide_register_driver(&idedisk_driver);
1278 module_init(idedisk_init);
1279 module_exit(idedisk_exit);
1280 MODULE_LICENSE("GPL");