Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Bartlomiej Zolnierkiewicz | 59bca8c | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 2 | * Copyright (C) 1994-1998 Linus Torvalds & authors (see below) |
| 3 | * Copyright (C) 1998-2002 Linux ATA Development |
| 4 | * Andre Hedrick <andre@linux-ide.org> |
| 5 | * Copyright (C) 2003 Red Hat <alan@redhat.com> |
| 6 | * Copyright (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Mostly written by Mark Lord <mlord@pobox.com> |
| 11 | * and Gadi Oxman <gadio@netvision.net.il> |
| 12 | * and Andre Hedrick <andre@linux-ide.org> |
| 13 | * |
| 14 | * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #define IDEDISK_VERSION "1.18" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/timer.h> |
| 24 | #include <linux/mm.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/major.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/genhd.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/delay.h> |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Richard Purdie | 2bfb646 | 2006-03-31 02:31:16 -0800 | [diff] [blame] | 32 | #include <linux/leds.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/ide.h> |
Bartlomiej Zolnierkiewicz | 3ceca72 | 2008-10-10 22:39:27 +0200 | [diff] [blame] | 34 | #include <linux/hdreg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <asm/byteorder.h> |
| 37 | #include <asm/irq.h> |
| 38 | #include <asm/uaccess.h> |
| 39 | #include <asm/io.h> |
| 40 | #include <asm/div64.h> |
| 41 | |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 42 | #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT) |
Tejun Heo | 689d6fa | 2008-08-25 19:56:16 +0900 | [diff] [blame] | 43 | #define IDE_DISK_MINORS (1 << PARTN_BITS) |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 44 | #else |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 45 | #define IDE_DISK_MINORS 0 |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 46 | #endif |
| 47 | |
Bartlomiej Zolnierkiewicz | f879048 | 2008-10-13 21:39:45 +0200 | [diff] [blame^] | 48 | #include "ide-disk.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 50 | static DEFINE_MUTEX(idedisk_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | #define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref) |
| 53 | |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 54 | static void ide_disk_release(struct kref *); |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static struct ide_disk_obj *ide_disk_get(struct gendisk *disk) |
| 57 | { |
| 58 | struct ide_disk_obj *idkp = NULL; |
| 59 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 60 | mutex_lock(&idedisk_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | idkp = ide_disk_g(disk); |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 62 | if (idkp) { |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 63 | if (ide_device_get(idkp->drive)) |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 64 | idkp = NULL; |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 65 | else |
| 66 | kref_get(&idkp->kref); |
Bartlomiej Zolnierkiewicz | 08da591 | 2008-07-24 22:53:15 +0200 | [diff] [blame] | 67 | } |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 68 | mutex_unlock(&idedisk_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return idkp; |
| 70 | } |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static void ide_disk_put(struct ide_disk_obj *idkp) |
| 73 | { |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 74 | ide_drive_t *drive = idkp->drive; |
| 75 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 76 | mutex_lock(&idedisk_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | kref_put(&idkp->kref, ide_disk_release); |
Bartlomiej Zolnierkiewicz | d3e33ff | 2008-08-05 18:16:59 +0200 | [diff] [blame] | 78 | ide_device_put(drive); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 79 | mutex_unlock(&idedisk_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Bartlomiej Zolnierkiewicz | ba76ae3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 82 | static const u8 ide_rw_cmds[] = { |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 83 | ATA_CMD_READ_MULTI, |
| 84 | ATA_CMD_WRITE_MULTI, |
| 85 | ATA_CMD_READ_MULTI_EXT, |
| 86 | ATA_CMD_WRITE_MULTI_EXT, |
| 87 | ATA_CMD_PIO_READ, |
| 88 | ATA_CMD_PIO_WRITE, |
| 89 | ATA_CMD_PIO_READ_EXT, |
| 90 | ATA_CMD_PIO_WRITE_EXT, |
| 91 | ATA_CMD_READ, |
| 92 | ATA_CMD_WRITE, |
| 93 | ATA_CMD_READ_EXT, |
| 94 | ATA_CMD_WRITE_EXT, |
Bartlomiej Zolnierkiewicz | ba76ae3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static const u8 ide_data_phases[] = { |
| 98 | TASKFILE_MULTI_IN, |
| 99 | TASKFILE_MULTI_OUT, |
| 100 | TASKFILE_IN, |
| 101 | TASKFILE_OUT, |
| 102 | TASKFILE_IN_DMA, |
| 103 | TASKFILE_OUT_DMA, |
| 104 | }; |
| 105 | |
| 106 | static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma) |
| 107 | { |
| 108 | u8 index, lba48, write; |
| 109 | |
| 110 | lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0; |
| 111 | write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0; |
| 112 | |
| 113 | if (dma) |
Bartlomiej Zolnierkiewicz | ba4b2e6 | 2008-07-23 19:55:55 +0200 | [diff] [blame] | 114 | index = 8; |
Bartlomiej Zolnierkiewicz | ba76ae3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 115 | else |
| 116 | index = drive->mult_count ? 0 : 4; |
| 117 | |
| 118 | task->tf.command = ide_rw_cmds[index + lba48 + write]; |
| 119 | |
| 120 | if (dma) |
| 121 | index = 8; /* fixup index */ |
| 122 | |
| 123 | task->data_phase = ide_data_phases[index / 2 + write]; |
| 124 | } |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | /* |
| 127 | * __ide_do_rw_disk() issues READ and WRITE commands to a disk, |
| 128 | * using LBA if supported, or CHS otherwise, to address sectors. |
| 129 | */ |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 130 | static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, |
| 131 | sector_t block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | ide_hwif_t *hwif = HWIF(drive); |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 134 | u16 nsectors = (u16)rq->nr_sectors; |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 135 | u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48); |
| 136 | u8 dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA); |
Bartlomiej Zolnierkiewicz | 9e42237 | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 137 | ide_task_t task; |
| 138 | struct ide_taskfile *tf = &task.tf; |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 139 | ide_startstop_t rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Bartlomiej Zolnierkiewicz | 238e4f1 | 2007-10-19 00:30:07 +0200 | [diff] [blame] | 141 | if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | if (block + rq->nr_sectors > 1ULL << 28) |
| 143 | dma = 0; |
| 144 | else |
| 145 | lba48 = 0; |
| 146 | } |
| 147 | |
| 148 | if (!dma) { |
| 149 | ide_init_sg_cmd(drive, rq); |
| 150 | ide_map_sg(drive, rq); |
| 151 | } |
| 152 | |
Bartlomiej Zolnierkiewicz | 9e42237 | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 153 | memset(&task, 0, sizeof(task)); |
Bartlomiej Zolnierkiewicz | 9a410e7 | 2008-07-15 21:21:48 +0200 | [diff] [blame] | 154 | task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | 2bd06b2 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 155 | |
Bartlomiej Zolnierkiewicz | d1d7671 | 2008-10-13 21:39:38 +0200 | [diff] [blame] | 156 | if (drive->dev_flags & IDE_DFLAG_LBA) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (lba48) { |
Michael Richardson | c2f8311 | 2006-02-07 12:58:33 -0800 | [diff] [blame] | 158 | pr_debug("%s: LBA=0x%012llx\n", drive->name, |
| 159 | (unsigned long long)block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 161 | tf->hob_nsect = (nsectors >> 8) & 0xff; |
Bartlomiej Zolnierkiewicz | 2bd06b2 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 162 | tf->hob_lbal = (u8)(block >> 24); |
| 163 | if (sizeof(block) != 4) { |
| 164 | tf->hob_lbam = (u8)((u64)block >> 32); |
| 165 | tf->hob_lbah = (u8)((u64)block >> 40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Bartlomiej Zolnierkiewicz | 2bd06b2 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 167 | |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 168 | tf->nsect = nsectors & 0xff; |
Bartlomiej Zolnierkiewicz | 2bd06b2 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 169 | tf->lbal = (u8) block; |
| 170 | tf->lbam = (u8)(block >> 8); |
| 171 | tf->lbah = (u8)(block >> 16); |
Bartlomiej Zolnierkiewicz | 6dd9b83 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 172 | |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 173 | task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } else { |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 175 | tf->nsect = nsectors & 0xff; |
Bartlomiej Zolnierkiewicz | 2bd06b2 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 176 | tf->lbal = block; |
| 177 | tf->lbam = block >>= 8; |
| 178 | tf->lbah = block >>= 8; |
| 179 | tf->device = (block >> 8) & 0xf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
Bartlomiej Zolnierkiewicz | d1d7671 | 2008-10-13 21:39:38 +0200 | [diff] [blame] | 181 | |
| 182 | tf->device |= ATA_LBA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } else { |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 184 | unsigned int sect, head, cyl, track; |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | track = (int)block / drive->sect; |
| 187 | sect = (int)block % drive->sect + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | head = track % drive->head; |
| 189 | cyl = track / drive->head; |
| 190 | |
| 191 | pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect); |
| 192 | |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 193 | tf->nsect = nsectors & 0xff; |
Bartlomiej Zolnierkiewicz | 2bd06b2 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 194 | tf->lbal = sect; |
| 195 | tf->lbam = cyl; |
| 196 | tf->lbah = cyl >> 8; |
| 197 | tf->device = head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Bartlomiej Zolnierkiewicz | ba76ae3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 200 | if (rq_data_dir(rq)) |
| 201 | task.tf_flags |= IDE_TFLAG_WRITE; |
| 202 | |
| 203 | ide_tf_set_cmd(drive, &task, dma); |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 204 | if (!dma) |
| 205 | hwif->data_phase = task.data_phase; |
| 206 | task.rq = rq; |
Bartlomiej Zolnierkiewicz | ba76ae3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 207 | |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 208 | rc = do_rw_taskfile(drive, &task); |
Bartlomiej Zolnierkiewicz | 2bd06b2 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 209 | |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 210 | if (rc == ide_stopped && dma) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | /* fallback to PIO */ |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 212 | task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK; |
Bartlomiej Zolnierkiewicz | ba76ae3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 213 | ide_tf_set_cmd(drive, &task, 0); |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 214 | hwif->data_phase = task.data_phase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | ide_init_sg_cmd(drive, rq); |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 216 | rc = do_rw_taskfile(drive, &task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 219 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* |
| 223 | * 268435455 == 137439 MB or 28bit limit |
| 224 | * 320173056 == 163929 MB or 48bit addressing |
| 225 | * 1073741822 == 549756 MB or 48bit addressing fake drive |
| 226 | */ |
| 227 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 228 | static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq, |
| 229 | sector_t block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
| 231 | ide_hwif_t *hwif = HWIF(drive); |
| 232 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 233 | BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | if (!blk_fs_request(rq)) { |
| 236 | blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command"); |
| 237 | ide_end_request(drive, 0, 0); |
| 238 | return ide_stopped; |
| 239 | } |
| 240 | |
Richard Purdie | 2bfb646 | 2006-03-31 02:31:16 -0800 | [diff] [blame] | 241 | ledtrig_ide_activity(); |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n", |
| 244 | drive->name, rq_data_dir(rq) == READ ? "read" : "writ", |
Michael Richardson | c2f8311 | 2006-02-07 12:58:33 -0800 | [diff] [blame] | 245 | (unsigned long long)block, rq->nr_sectors, |
| 246 | (unsigned long)rq->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | if (hwif->rw_disk) |
| 249 | hwif->rw_disk(drive, rq); |
| 250 | |
| 251 | return __ide_do_rw_disk(drive, rq, block); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Queries for true maximum capacity of the drive. |
| 256 | * Returns maximum LBA address (> 0) of the drive, 0 if failed. |
| 257 | */ |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 258 | static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | ide_task_t args; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 261 | struct ide_taskfile *tf = &args.tf; |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 262 | u64 addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | /* Create IDE/ATA command request structure */ |
| 265 | memset(&args, 0, sizeof(ide_task_t)); |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 266 | if (lba48) |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 267 | tf->command = ATA_CMD_READ_NATIVE_MAX_EXT; |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 268 | else |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 269 | tf->command = ATA_CMD_READ_NATIVE_MAX; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 270 | tf->device = ATA_LBA; |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 271 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | a3bbb9d | 2008-01-25 22:17:10 +0100 | [diff] [blame] | 272 | if (lba48) |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 273 | args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | /* submit command request */ |
Bartlomiej Zolnierkiewicz | 9a3c49b | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 275 | ide_no_data_taskfile(drive, &args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | /* if OK, compute maximum address value */ |
Bartlomiej Zolnierkiewicz | a501633 | 2008-01-25 22:17:17 +0100 | [diff] [blame] | 278 | if ((tf->status & 0x01) == 0) |
| 279 | addr = ide_get_lba_addr(tf, lba48) + 1; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return addr; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Sets maximum virtual LBA address of the drive. |
| 286 | * Returns new maximum virtual LBA address (> 0) or 0 on failure. |
| 287 | */ |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 288 | static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | ide_task_t args; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 291 | struct ide_taskfile *tf = &args.tf; |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 292 | u64 addr_set = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | addr_req--; |
| 295 | /* Create IDE/ATA command request structure */ |
| 296 | memset(&args, 0, sizeof(ide_task_t)); |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 297 | tf->lbal = (addr_req >> 0) & 0xff; |
| 298 | tf->lbam = (addr_req >>= 8) & 0xff; |
| 299 | tf->lbah = (addr_req >>= 8) & 0xff; |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 300 | if (lba48) { |
| 301 | tf->hob_lbal = (addr_req >>= 8) & 0xff; |
| 302 | tf->hob_lbam = (addr_req >>= 8) & 0xff; |
| 303 | tf->hob_lbah = (addr_req >>= 8) & 0xff; |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 304 | tf->command = ATA_CMD_SET_MAX_EXT; |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 305 | } else { |
| 306 | tf->device = (addr_req >>= 8) & 0x0f; |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 307 | tf->command = ATA_CMD_SET_MAX; |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 308 | } |
| 309 | tf->device |= ATA_LBA; |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 310 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | a3bbb9d | 2008-01-25 22:17:10 +0100 | [diff] [blame] | 311 | if (lba48) |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 312 | args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | /* submit command request */ |
Bartlomiej Zolnierkiewicz | 9a3c49b | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 314 | ide_no_data_taskfile(drive, &args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | /* if OK, compute maximum address value */ |
Bartlomiej Zolnierkiewicz | a501633 | 2008-01-25 22:17:17 +0100 | [diff] [blame] | 316 | if ((tf->status & 0x01) == 0) |
| 317 | addr_set = ide_get_lba_addr(tf, lba48) + 1; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return addr_set; |
| 320 | } |
| 321 | |
| 322 | static unsigned long long sectors_to_MB(unsigned long long n) |
| 323 | { |
| 324 | n <<= 9; /* make it bytes */ |
| 325 | do_div(n, 1000000); /* make it MB */ |
| 326 | return n; |
| 327 | } |
| 328 | |
| 329 | /* |
Bartlomiej Zolnierkiewicz | b0244a0 | 2007-08-20 22:42:57 +0200 | [diff] [blame] | 330 | * Some disks report total number of sectors instead of |
| 331 | * maximum sector address. We list them here. |
| 332 | */ |
| 333 | static const struct drive_list_entry hpa_list[] = { |
| 334 | { "ST340823A", NULL }, |
Jorge Juan Chico | 7062cdc | 2007-09-17 12:35:30 +0200 | [diff] [blame] | 335 | { "ST320413A", NULL }, |
Mikko Rapeli | b152fcd | 2008-02-19 01:41:25 +0100 | [diff] [blame] | 336 | { "ST310211A", NULL }, |
Bartlomiej Zolnierkiewicz | b0244a0 | 2007-08-20 22:42:57 +0200 | [diff] [blame] | 337 | { NULL, NULL } |
| 338 | }; |
| 339 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 340 | static void idedisk_check_hpa(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | unsigned long long capacity, set_max; |
Bartlomiej Zolnierkiewicz | 942dcd8 | 2008-10-10 22:39:30 +0200 | [diff] [blame] | 343 | int lba48 = ata_id_lba48_enabled(drive->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | capacity = drive->capacity64; |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 346 | |
| 347 | set_max = idedisk_read_native_max_address(drive, lba48); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Bartlomiej Zolnierkiewicz | b0244a0 | 2007-08-20 22:42:57 +0200 | [diff] [blame] | 349 | if (ide_in_drive_list(drive->id, hpa_list)) { |
| 350 | /* |
| 351 | * Since we are inclusive wrt to firmware revisions do this |
| 352 | * extra check and apply the workaround only when needed. |
| 353 | */ |
| 354 | if (set_max == capacity + 1) |
| 355 | set_max--; |
| 356 | } |
| 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | if (set_max <= capacity) |
| 359 | return; |
| 360 | |
| 361 | printk(KERN_INFO "%s: Host Protected Area detected.\n" |
| 362 | "\tcurrent capacity is %llu sectors (%llu MB)\n" |
| 363 | "\tnative capacity is %llu sectors (%llu MB)\n", |
| 364 | drive->name, |
| 365 | capacity, sectors_to_MB(capacity), |
| 366 | set_max, sectors_to_MB(set_max)); |
| 367 | |
Bartlomiej Zolnierkiewicz | 7a3b751 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 368 | set_max = idedisk_set_max_address(drive, set_max, lba48); |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if (set_max) { |
| 371 | drive->capacity64 = set_max; |
| 372 | printk(KERN_INFO "%s: Host Protected Area disabled.\n", |
| 373 | drive->name); |
| 374 | } |
| 375 | } |
| 376 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 377 | static void init_idedisk_capacity(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 379 | u16 *id = drive->id; |
Bartlomiej Zolnierkiewicz | d1d7671 | 2008-10-13 21:39:38 +0200 | [diff] [blame] | 380 | int lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Bartlomiej Zolnierkiewicz | 942dcd8 | 2008-10-10 22:39:30 +0200 | [diff] [blame] | 382 | if (ata_id_lba48_enabled(id)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | /* drive speaks 48-bit LBA */ |
Bartlomiej Zolnierkiewicz | d1d7671 | 2008-10-13 21:39:38 +0200 | [diff] [blame] | 384 | lba = 1; |
Bartlomiej Zolnierkiewicz | 48fb268 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 385 | drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2); |
Bartlomiej Zolnierkiewicz | a02227c | 2008-10-10 22:39:31 +0200 | [diff] [blame] | 386 | } else if (ata_id_has_lba(id) && ata_id_is_lba_capacity_ok(id)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | /* drive speaks 28-bit LBA */ |
Bartlomiej Zolnierkiewicz | d1d7671 | 2008-10-13 21:39:38 +0200 | [diff] [blame] | 388 | lba = 1; |
Bartlomiej Zolnierkiewicz | 48fb268 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 389 | drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } else { |
| 391 | /* drive speaks boring old 28-bit CHS */ |
Bartlomiej Zolnierkiewicz | d1d7671 | 2008-10-13 21:39:38 +0200 | [diff] [blame] | 392 | lba = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | drive->capacity64 = drive->cyl * drive->head * drive->sect; |
| 394 | } |
Bartlomiej Zolnierkiewicz | d1d7671 | 2008-10-13 21:39:38 +0200 | [diff] [blame] | 395 | |
| 396 | if (lba) { |
| 397 | drive->dev_flags |= IDE_DFLAG_LBA; |
| 398 | |
| 399 | /* |
| 400 | * If this device supports the Host Protected Area feature set, |
| 401 | * then we may need to change our opinion about its capacity. |
| 402 | */ |
| 403 | if (ata_id_hpa_enabled(id)) |
| 404 | idedisk_check_hpa(drive); |
| 405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 408 | static sector_t idedisk_capacity(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
Bartlomiej Zolnierkiewicz | 3c619ff | 2008-10-10 22:39:22 +0200 | [diff] [blame] | 410 | return drive->capacity64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Bartlomiej Zolnierkiewicz | ecfd80e | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 413 | #ifdef CONFIG_IDE_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | static int smart_enable(ide_drive_t *drive) |
| 415 | { |
| 416 | ide_task_t args; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 417 | struct ide_taskfile *tf = &args.tf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | memset(&args, 0, sizeof(ide_task_t)); |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 420 | tf->feature = ATA_SMART_ENABLE; |
| 421 | tf->lbam = ATA_SMART_LBAM_PASS; |
| 422 | tf->lbah = ATA_SMART_LBAH_PASS; |
| 423 | tf->command = ATA_CMD_SMART; |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 424 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | 9a3c49b | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 425 | return ide_no_data_taskfile(drive, &args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Bartlomiej Zolnierkiewicz | 43e7c0c | 2007-10-20 00:32:37 +0200 | [diff] [blame] | 428 | static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
| 430 | ide_task_t args; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 431 | struct ide_taskfile *tf = &args.tf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | memset(&args, 0, sizeof(ide_task_t)); |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 434 | tf->feature = sub_cmd; |
| 435 | tf->nsect = 0x01; |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 436 | tf->lbam = ATA_SMART_LBAM_PASS; |
| 437 | tf->lbah = ATA_SMART_LBAH_PASS; |
| 438 | tf->command = ATA_CMD_SMART; |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 439 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 440 | args.data_phase = TASKFILE_IN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | (void) smart_enable(drive); |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 442 | return ide_raw_taskfile(drive, &args, buf, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static int proc_idedisk_read_cache |
| 446 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 447 | { |
| 448 | ide_drive_t *drive = (ide_drive_t *) data; |
| 449 | char *out = page; |
| 450 | int len; |
| 451 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 452 | if (drive->dev_flags & IDE_DFLAG_ID_READ) |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 453 | len = sprintf(out, "%i\n", drive->id[ATA_ID_BUF_SIZE] / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | else |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 455 | len = sprintf(out, "(none)\n"); |
| 456 | |
| 457 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static int proc_idedisk_read_capacity |
| 461 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 462 | { |
| 463 | ide_drive_t*drive = (ide_drive_t *)data; |
| 464 | int len; |
| 465 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 466 | len = sprintf(page, "%llu\n", (long long)idedisk_capacity(drive)); |
| 467 | |
| 468 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Bartlomiej Zolnierkiewicz | 799ee57 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 471 | static int proc_idedisk_read_smart(char *page, char **start, off_t off, |
| 472 | int count, int *eof, void *data, u8 sub_cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
| 474 | ide_drive_t *drive = (ide_drive_t *)data; |
| 475 | int len = 0, i = 0; |
| 476 | |
Bartlomiej Zolnierkiewicz | 799ee57 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 477 | if (get_smart_data(drive, page, sub_cmd) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | unsigned short *val = (unsigned short *) page; |
Bartlomiej Zolnierkiewicz | 151a670 | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 479 | char *out = (char *)val + SECTOR_SIZE; |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | page = out; |
| 482 | do { |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 483 | out += sprintf(out, "%04x%c", le16_to_cpu(*val), |
| 484 | (++i & 7) ? ' ' : '\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | val += 1; |
Bartlomiej Zolnierkiewicz | 151a670 | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 486 | } while (i < SECTOR_SIZE / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | len = out - page; |
| 488 | } |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 489 | |
| 490 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Bartlomiej Zolnierkiewicz | 799ee57 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 493 | static int proc_idedisk_read_sv |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 495 | { |
Bartlomiej Zolnierkiewicz | 799ee57 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 496 | return proc_idedisk_read_smart(page, start, off, count, eof, data, |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 497 | ATA_SMART_READ_VALUES); |
Bartlomiej Zolnierkiewicz | 799ee57 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Bartlomiej Zolnierkiewicz | 799ee57 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 500 | static int proc_idedisk_read_st |
| 501 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 502 | { |
| 503 | return proc_idedisk_read_smart(page, start, off, count, eof, data, |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 504 | ATA_SMART_READ_THRESHOLDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static ide_proc_entry_t idedisk_proc[] = { |
Bartlomiej Zolnierkiewicz | 799ee57 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 508 | { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL }, |
| 509 | { "capacity", S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL }, |
| 510 | { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL }, |
| 511 | { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_sv, NULL }, |
| 512 | { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_st, NULL }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { NULL, 0, NULL, NULL } |
| 514 | }; |
Bartlomiej Zolnierkiewicz | ecfd80e | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 515 | #endif /* CONFIG_IDE_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 517 | static void idedisk_prepare_flush(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
| 519 | ide_drive_t *drive = q->queuedata; |
Bartlomiej Zolnierkiewicz | 395d8ef | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 520 | ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Bartlomiej Zolnierkiewicz | 395d8ef | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 522 | /* FIXME: map struct ide_taskfile on rq->cmd[] */ |
| 523 | BUG_ON(task == NULL); |
| 524 | |
| 525 | memset(task, 0, sizeof(*task)); |
Bartlomiej Zolnierkiewicz | ff2779b | 2008-10-10 22:39:31 +0200 | [diff] [blame] | 526 | if (ata_id_flush_ext_enabled(drive->id) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | (drive->capacity64 >= (1UL << 28))) |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 528 | task->tf.command = ATA_CMD_FLUSH_EXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | else |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 530 | task->tf.command = ATA_CMD_FLUSH; |
Bartlomiej Zolnierkiewicz | 395d8ef | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 531 | task->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE | |
| 532 | IDE_TFLAG_DYN; |
| 533 | task->data_phase = TASKFILE_NO_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
Bartlomiej Zolnierkiewicz | 813a0eb | 2008-01-25 22:17:10 +0100 | [diff] [blame] | 535 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 536 | rq->cmd_flags |= REQ_SOFTBARRIER; |
Bartlomiej Zolnierkiewicz | 395d8ef | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 537 | rq->special = task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 540 | ide_devset_get(multcount, mult_count); |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | /* |
| 543 | * This is tightly woven into the driver->do_special can not touch. |
| 544 | * DON'T do it again until a total personality rewrite is committed. |
| 545 | */ |
| 546 | static int set_multcount(ide_drive_t *drive, int arg) |
| 547 | { |
FUJITA Tomonori | dd47087 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 548 | struct request *rq; |
| 549 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Bartlomiej Zolnierkiewicz | 48fb268 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 551 | if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff)) |
Bartlomiej Zolnierkiewicz | 1497943 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 552 | return -EINVAL; |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | if (drive->special.b.set_multmode) |
| 555 | return -EBUSY; |
Bartlomiej Zolnierkiewicz | 852738f | 2008-01-26 20:13:12 +0100 | [diff] [blame] | 556 | |
FUJITA Tomonori | dd47087 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 557 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 558 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; |
Bartlomiej Zolnierkiewicz | 852738f | 2008-01-26 20:13:12 +0100 | [diff] [blame] | 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | drive->mult_req = arg; |
| 561 | drive->special.b.set_multmode = 1; |
FUJITA Tomonori | dd47087 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 562 | error = blk_execute_rq(drive->queue, NULL, rq, 0); |
| 563 | blk_put_request(rq); |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | return (drive->mult_count == arg) ? 0 : -EIO; |
| 566 | } |
| 567 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 568 | ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | static int set_nowerr(ide_drive_t *drive, int arg) |
| 571 | { |
Bartlomiej Zolnierkiewicz | 1497943 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 572 | if (arg < 0 || arg > 1) |
| 573 | return -EINVAL; |
| 574 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 575 | if (arg) |
| 576 | drive->dev_flags |= IDE_DFLAG_NOWERR; |
| 577 | else |
| 578 | drive->dev_flags &= ~IDE_DFLAG_NOWERR; |
| 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT; |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
Bartlomiej Zolnierkiewicz | be3c096 | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 585 | static int ide_do_setfeature(ide_drive_t *drive, u8 feature, u8 nsect) |
| 586 | { |
| 587 | ide_task_t task; |
| 588 | |
| 589 | memset(&task, 0, sizeof(task)); |
| 590 | task.tf.feature = feature; |
| 591 | task.tf.nsect = nsect; |
| 592 | task.tf.command = ATA_CMD_SET_FEATURES; |
| 593 | task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
| 594 | |
| 595 | return ide_no_data_taskfile(drive, &task); |
| 596 | } |
| 597 | |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 598 | static void update_ordered(ide_drive_t *drive) |
| 599 | { |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 600 | u16 *id = drive->id; |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 601 | unsigned ordered = QUEUE_ORDERED_NONE; |
| 602 | prepare_flush_fn *prep_fn = NULL; |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 603 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 604 | if (drive->dev_flags & IDE_DFLAG_WCACHE) { |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 605 | unsigned long long capacity; |
| 606 | int barrier; |
| 607 | /* |
| 608 | * We must avoid issuing commands a drive does not |
| 609 | * understand or we may crash it. We check flush cache |
| 610 | * is supported. We also check we have the LBA48 flush |
| 611 | * cache if the drive capacity is too large. By this |
| 612 | * time we have trimmed the drive capacity if LBA48 is |
| 613 | * not available so we don't need to recheck that. |
| 614 | */ |
| 615 | capacity = idedisk_capacity(drive); |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 616 | barrier = ata_id_flush_enabled(id) && |
| 617 | (drive->dev_flags & IDE_DFLAG_NOFLUSH) == 0 && |
| 618 | ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 || |
| 619 | capacity <= (1ULL << 28) || |
Bartlomiej Zolnierkiewicz | ff2779b | 2008-10-10 22:39:31 +0200 | [diff] [blame] | 620 | ata_id_flush_ext_enabled(id)); |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 621 | |
| 622 | printk(KERN_INFO "%s: cache flushes %ssupported\n", |
Jean Delvare | f7ad836 | 2006-02-03 03:04:57 -0800 | [diff] [blame] | 623 | drive->name, barrier ? "" : "not "); |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 624 | |
| 625 | if (barrier) { |
| 626 | ordered = QUEUE_ORDERED_DRAIN_FLUSH; |
| 627 | prep_fn = idedisk_prepare_flush; |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 628 | } |
| 629 | } else |
| 630 | ordered = QUEUE_ORDERED_DRAIN; |
| 631 | |
| 632 | blk_queue_ordered(drive->queue, ordered, prep_fn); |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 635 | ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 636 | |
| 637 | static int set_wcache(ide_drive_t *drive, int arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 639 | int err = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
Bartlomiej Zolnierkiewicz | 1497943 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 641 | if (arg < 0 || arg > 1) |
| 642 | return -EINVAL; |
| 643 | |
Bartlomiej Zolnierkiewicz | 4b58f17 | 2008-10-10 22:39:30 +0200 | [diff] [blame] | 644 | if (ata_id_flush_enabled(drive->id)) { |
Bartlomiej Zolnierkiewicz | be3c096 | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 645 | err = ide_do_setfeature(drive, |
| 646 | arg ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF, 0); |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 647 | if (err == 0) { |
| 648 | if (arg) |
| 649 | drive->dev_flags |= IDE_DFLAG_WCACHE; |
| 650 | else |
| 651 | drive->dev_flags &= ~IDE_DFLAG_WCACHE; |
| 652 | } |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 655 | update_ordered(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Tejun Heo | 3e087b5 | 2006-01-06 09:57:31 +0100 | [diff] [blame] | 657 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 660 | static int do_idedisk_flushcache(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
| 662 | ide_task_t args; |
| 663 | |
| 664 | memset(&args, 0, sizeof(ide_task_t)); |
Bartlomiej Zolnierkiewicz | ff2779b | 2008-10-10 22:39:31 +0200 | [diff] [blame] | 665 | if (ata_id_flush_ext_enabled(drive->id)) |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 666 | args.tf.command = ATA_CMD_FLUSH_EXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | else |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 668 | args.tf.command = ATA_CMD_FLUSH; |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 669 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | 9a3c49b | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 670 | return ide_no_data_taskfile(drive, &args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 673 | ide_devset_get(acoustic, acoustic); |
| 674 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 675 | static int set_acoustic(ide_drive_t *drive, int arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
Bartlomiej Zolnierkiewicz | 1497943 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 677 | if (arg < 0 || arg > 254) |
| 678 | return -EINVAL; |
| 679 | |
Bartlomiej Zolnierkiewicz | be3c096 | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 680 | ide_do_setfeature(drive, |
| 681 | arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg); |
| 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | drive->acoustic = arg; |
Bartlomiej Zolnierkiewicz | be3c096 | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | return 0; |
| 686 | } |
| 687 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 688 | ide_devset_get_flag(addressing, IDE_DFLAG_LBA48); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | /* |
| 691 | * drive->addressing: |
| 692 | * 0: 28-bit |
| 693 | * 1: 48-bit |
| 694 | * 2: 48-bit capable doing 28-bit |
| 695 | */ |
Bartlomiej Zolnierkiewicz | aa768773 | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 696 | static int set_addressing(ide_drive_t *drive, int arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
Bartlomiej Zolnierkiewicz | 1497943 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 698 | if (arg < 0 || arg > 2) |
| 699 | return -EINVAL; |
| 700 | |
Bartlomiej Zolnierkiewicz | 35c1375 | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 701 | if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) || |
| 702 | ata_id_lba48_enabled(drive->id) == 0)) |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 703 | return -EIO; |
Bartlomiej Zolnierkiewicz | 942dcd8 | 2008-10-10 22:39:30 +0200 | [diff] [blame] | 704 | |
Bartlomiej Zolnierkiewicz | 35c1375 | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 705 | if (arg == 2) |
| 706 | arg = 0; |
| 707 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 708 | if (arg) |
| 709 | drive->dev_flags |= IDE_DFLAG_LBA48; |
| 710 | else |
| 711 | drive->dev_flags &= ~IDE_DFLAG_LBA48; |
Bartlomiej Zolnierkiewicz | 942dcd8 | 2008-10-10 22:39:30 +0200 | [diff] [blame] | 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
Bartlomiej Zolnierkiewicz | f879048 | 2008-10-13 21:39:45 +0200 | [diff] [blame^] | 716 | ide_ext_devset_rw(acoustic, acoustic); |
| 717 | ide_ext_devset_rw(address, addressing); |
| 718 | ide_ext_devset_rw(multcount, multcount); |
| 719 | ide_ext_devset_rw(wcache, wcache); |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 720 | |
Bartlomiej Zolnierkiewicz | f879048 | 2008-10-13 21:39:45 +0200 | [diff] [blame^] | 721 | ide_ext_devset_rw_sync(nowerr, nowerr); |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 722 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 723 | #ifdef CONFIG_IDE_PROC_FS |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 724 | ide_devset_rw_field(bios_cyl, bios_cyl); |
| 725 | ide_devset_rw_field(bios_head, bios_head); |
| 726 | ide_devset_rw_field(bios_sect, bios_sect); |
| 727 | ide_devset_rw_field(failures, failures); |
| 728 | ide_devset_rw_field(lun, lun); |
| 729 | ide_devset_rw_field(max_failures, max_failures); |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 730 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame] | 731 | static const struct ide_proc_devset idedisk_settings[] = { |
| 732 | IDE_PROC_DEVSET(acoustic, 0, 254), |
| 733 | IDE_PROC_DEVSET(address, 0, 2), |
| 734 | IDE_PROC_DEVSET(bios_cyl, 0, 65535), |
| 735 | IDE_PROC_DEVSET(bios_head, 0, 255), |
| 736 | IDE_PROC_DEVSET(bios_sect, 0, 63), |
| 737 | IDE_PROC_DEVSET(failures, 0, 65535), |
| 738 | IDE_PROC_DEVSET(lun, 0, 7), |
| 739 | IDE_PROC_DEVSET(max_failures, 0, 65535), |
| 740 | IDE_PROC_DEVSET(multcount, 0, 16), |
| 741 | IDE_PROC_DEVSET(nowerr, 0, 1), |
| 742 | IDE_PROC_DEVSET(wcache, 0, 1), |
| 743 | { 0 }, |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 744 | }; |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 745 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 747 | static void idedisk_setup(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
Bartlomiej Zolnierkiewicz | 1e874f4 | 2008-10-10 22:39:27 +0200 | [diff] [blame] | 749 | struct ide_disk_obj *idkp = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 238e4f1 | 2007-10-19 00:30:07 +0200 | [diff] [blame] | 750 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 751 | u16 *id = drive->id; |
| 752 | char *m = (char *)&id[ATA_ID_PROD]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | unsigned long long capacity; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Bartlomiej Zolnierkiewicz | 1e874f4 | 2008-10-10 22:39:27 +0200 | [diff] [blame] | 755 | ide_proc_register_driver(drive, idkp->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 757 | if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return; |
| 759 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 760 | if (drive->dev_flags & IDE_DFLAG_REMOVABLE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | /* |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 762 | * Removable disks (eg. SYQUEST); ignore 'WD' drives |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | */ |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 764 | if (m[0] != 'W' || m[1] != 'D') |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 765 | drive->dev_flags |= IDE_DFLAG_DOORLOCKING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Bartlomiej Zolnierkiewicz | aa768773 | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 768 | (void)set_addressing(drive, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 770 | if (drive->dev_flags & IDE_DFLAG_LBA48) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | int max_s = 2048; |
| 772 | |
| 773 | if (max_s > hwif->rqsize) |
| 774 | max_s = hwif->rqsize; |
| 775 | |
| 776 | blk_queue_max_sectors(drive->queue, max_s); |
| 777 | } |
| 778 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 779 | printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, |
| 780 | drive->queue->max_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | /* calculate drive capacity, and select LBA if possible */ |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 783 | init_idedisk_capacity(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
| 785 | /* limit drive capacity to 137GB if LBA48 cannot be used */ |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 786 | if ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 && |
| 787 | drive->capacity64 > 1ULL << 28) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | printk(KERN_WARNING "%s: cannot use LBA48 - full capacity " |
| 789 | "%llu sectors (%llu MB)\n", |
| 790 | drive->name, (unsigned long long)drive->capacity64, |
| 791 | sectors_to_MB(drive->capacity64)); |
| 792 | drive->capacity64 = 1ULL << 28; |
| 793 | } |
| 794 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 795 | if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && |
| 796 | (drive->dev_flags & IDE_DFLAG_LBA48)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | if (drive->capacity64 > 1ULL << 28) { |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 798 | printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode" |
| 799 | " will be used for accessing sectors " |
| 800 | "> %u\n", drive->name, 1 << 28); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } else |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 802 | drive->dev_flags &= ~IDE_DFLAG_LBA48; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
| 806 | * if possible, give fdisk access to more of the drive, |
| 807 | * by correcting bios_cyls: |
| 808 | */ |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 809 | capacity = idedisk_capacity(drive); |
| 810 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 811 | if ((drive->dev_flags & IDE_DFLAG_FORCED_GEOM) == 0) { |
Bartlomiej Zolnierkiewicz | 942dcd8 | 2008-10-10 22:39:30 +0200 | [diff] [blame] | 812 | if (ata_id_lba48_enabled(drive->id)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | /* compatibility */ |
| 814 | drive->bios_sect = 63; |
| 815 | drive->bios_head = 255; |
| 816 | } |
| 817 | |
| 818 | if (drive->bios_sect && drive->bios_head) { |
| 819 | unsigned int cap0 = capacity; /* truncate to 32 bits */ |
| 820 | unsigned int cylsz, cyl; |
| 821 | |
| 822 | if (cap0 != capacity) |
| 823 | drive->bios_cyl = 65535; |
| 824 | else { |
| 825 | cylsz = drive->bios_sect * drive->bios_head; |
| 826 | cyl = cap0 / cylsz; |
| 827 | if (cyl > 65535) |
| 828 | cyl = 65535; |
| 829 | if (cyl > drive->bios_cyl) |
| 830 | drive->bios_cyl = cyl; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | printk(KERN_INFO "%s: %llu sectors (%llu MB)", |
| 835 | drive->name, capacity, sectors_to_MB(capacity)); |
| 836 | |
| 837 | /* Only print cache size when it was specified */ |
Bartlomiej Zolnierkiewicz | 4dde449 | 2008-10-10 22:39:19 +0200 | [diff] [blame] | 838 | if (id[ATA_ID_BUF_SIZE]) |
| 839 | printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 841 | printk(KERN_CONT ", CHS=%d/%d/%d\n", |
| 842 | drive->bios_cyl, drive->bios_head, drive->bios_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | /* write cache enabled? */ |
Bartlomiej Zolnierkiewicz | 8a089c6 | 2008-10-10 22:39:20 +0200 | [diff] [blame] | 845 | if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id)) |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 846 | drive->dev_flags |= IDE_DFLAG_WCACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 848 | set_wcache(drive, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static void ide_cacheflush_p(ide_drive_t *drive) |
| 852 | { |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 853 | if (ata_id_flush_enabled(drive->id) == 0 || |
| 854 | (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | return; |
| 856 | |
| 857 | if (do_idedisk_flushcache(drive)) |
| 858 | printk(KERN_INFO "%s: wcache flush failed!\n", drive->name); |
| 859 | } |
| 860 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 861 | static void ide_disk_remove(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { |
| 863 | struct ide_disk_obj *idkp = drive->driver_data; |
| 864 | struct gendisk *g = idkp->disk; |
| 865 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 866 | ide_proc_unregister_driver(drive, idkp->driver); |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | del_gendisk(g); |
| 869 | |
Bartlomiej Zolnierkiewicz | d36fef6 | 2005-12-15 02:19:20 +0100 | [diff] [blame] | 870 | ide_cacheflush_p(drive); |
| 871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | ide_disk_put(idkp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static void ide_disk_release(struct kref *kref) |
| 876 | { |
| 877 | struct ide_disk_obj *idkp = to_ide_disk(kref); |
| 878 | ide_drive_t *drive = idkp->drive; |
| 879 | struct gendisk *g = idkp->disk; |
| 880 | |
| 881 | drive->driver_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | g->private_data = NULL; |
| 883 | put_disk(g); |
| 884 | kfree(idkp); |
| 885 | } |
| 886 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 887 | static int ide_disk_probe(ide_drive_t *drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Lee Trager | 0d2157f | 2007-06-08 15:14:30 +0200 | [diff] [blame] | 889 | /* |
| 890 | * On HPA drives the capacity needs to be |
| 891 | * reinitilized on resume otherwise the disk |
| 892 | * can not be used and a hard reset is required |
| 893 | */ |
| 894 | static void ide_disk_resume(ide_drive_t *drive) |
| 895 | { |
Bartlomiej Zolnierkiewicz | f41891c | 2008-10-10 22:39:20 +0200 | [diff] [blame] | 896 | if (ata_id_hpa_enabled(drive->id)) |
Lee Trager | 0d2157f | 2007-06-08 15:14:30 +0200 | [diff] [blame] | 897 | init_idedisk_capacity(drive); |
| 898 | } |
| 899 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 900 | static void ide_device_shutdown(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | #ifdef CONFIG_ALPHA |
| 903 | /* On Alpha, halt(8) doesn't actually turn the machine off, |
| 904 | it puts you into the sort of firmware monitor. Typically, |
| 905 | it's used to boot another kernel image, so it's not much |
| 906 | different from reboot(8). Therefore, we don't need to |
| 907 | spin down the disk in this case, especially since Alpha |
| 908 | firmware doesn't handle disks in standby mode properly. |
| 909 | On the other hand, it's reasonably safe to turn the power |
| 910 | off when the shutdown process reaches the firmware prompt, |
| 911 | as the firmware initialization takes rather long time - |
| 912 | at least 10 seconds, which should be sufficient for |
| 913 | the disk to expire its write cache. */ |
| 914 | if (system_state != SYSTEM_POWER_OFF) { |
| 915 | #else |
| 916 | if (system_state == SYSTEM_RESTART) { |
| 917 | #endif |
| 918 | ide_cacheflush_p(drive); |
| 919 | return; |
| 920 | } |
| 921 | |
Bartlomiej Zolnierkiewicz | d12faa2 | 2008-02-26 21:50:36 +0100 | [diff] [blame] | 922 | printk(KERN_INFO "Shutdown: %s\n", drive->name); |
| 923 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 924 | drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | static ide_driver_t idedisk_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | .gen_driver = { |
Laurent Riffard | 4ef3b8f | 2005-11-18 22:15:40 +0100 | [diff] [blame] | 929 | .owner = THIS_MODULE, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 930 | .name = "ide-disk", |
| 931 | .bus = &ide_bus_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | }, |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 933 | .probe = ide_disk_probe, |
| 934 | .remove = ide_disk_remove, |
Lee Trager | 0d2157f | 2007-06-08 15:14:30 +0200 | [diff] [blame] | 935 | .resume = ide_disk_resume, |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 936 | .shutdown = ide_device_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | .version = IDEDISK_VERSION, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | .do_request = ide_do_rw_disk, |
| 939 | .end_request = ide_end_request, |
| 940 | .error = __ide_error, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 941 | #ifdef CONFIG_IDE_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | .proc = idedisk_proc, |
Bartlomiej Zolnierkiewicz | 8185d5a | 2008-10-10 22:39:28 +0200 | [diff] [blame] | 943 | .settings = idedisk_settings, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 944 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | }; |
| 946 | |
Bartlomiej Zolnierkiewicz | 29ec683 | 2008-01-26 20:12:59 +0100 | [diff] [blame] | 947 | static int idedisk_set_doorlock(ide_drive_t *drive, int on) |
| 948 | { |
| 949 | ide_task_t task; |
| 950 | |
| 951 | memset(&task, 0, sizeof(task)); |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 952 | task.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK; |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 953 | task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | 29ec683 | 2008-01-26 20:12:59 +0100 | [diff] [blame] | 954 | |
| 955 | return ide_no_data_taskfile(drive, &task); |
| 956 | } |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | static int idedisk_open(struct inode *inode, struct file *filp) |
| 959 | { |
| 960 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 961 | struct ide_disk_obj *idkp; |
| 962 | ide_drive_t *drive; |
| 963 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 964 | idkp = ide_disk_get(disk); |
| 965 | if (idkp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return -ENXIO; |
| 967 | |
| 968 | drive = idkp->drive; |
| 969 | |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 970 | idkp->openers++; |
| 971 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 972 | if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | check_disk_change(inode->i_bdev); |
| 974 | /* |
| 975 | * Ignore the return code from door_lock, |
| 976 | * since the open() has already succeeded, |
| 977 | * and the door_lock is irrelevant at this point. |
| 978 | */ |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 979 | if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) && |
| 980 | idedisk_set_doorlock(drive, 1)) |
| 981 | drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | } |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | static int idedisk_release(struct inode *inode, struct file *filp) |
| 987 | { |
| 988 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 989 | struct ide_disk_obj *idkp = ide_disk_g(disk); |
| 990 | ide_drive_t *drive = idkp->drive; |
| 991 | |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 992 | if (idkp->openers == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | ide_cacheflush_p(drive); |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 994 | |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 995 | if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) { |
| 996 | if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) && |
| 997 | idedisk_set_doorlock(drive, 0)) |
| 998 | drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 1000 | |
| 1001 | idkp->openers--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
| 1003 | ide_disk_put(idkp); |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1008 | static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 1009 | { |
| 1010 | struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk); |
| 1011 | ide_drive_t *drive = idkp->drive; |
| 1012 | |
| 1013 | geo->heads = drive->bios_head; |
| 1014 | geo->sectors = drive->bios_sect; |
| 1015 | geo->cylinders = (u16)drive->bios_cyl; /* truncate */ |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | static int idedisk_media_changed(struct gendisk *disk) |
| 1020 | { |
| 1021 | struct ide_disk_obj *idkp = ide_disk_g(disk); |
| 1022 | ide_drive_t *drive = idkp->drive; |
| 1023 | |
| 1024 | /* do not scan partitions twice if this is a removable device */ |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1025 | if (drive->dev_flags & IDE_DFLAG_ATTACH) { |
| 1026 | drive->dev_flags &= ~IDE_DFLAG_ATTACH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | /* if removable, always assume it was changed */ |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1031 | return !!(drive->dev_flags & IDE_DFLAG_REMOVABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static int idedisk_revalidate_disk(struct gendisk *disk) |
| 1035 | { |
| 1036 | struct ide_disk_obj *idkp = ide_disk_g(disk); |
| 1037 | set_capacity(disk, idedisk_capacity(idkp->drive)); |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | static struct block_device_operations idedisk_ops = { |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 1042 | .owner = THIS_MODULE, |
| 1043 | .open = idedisk_open, |
| 1044 | .release = idedisk_release, |
Bartlomiej Zolnierkiewicz | f879048 | 2008-10-13 21:39:45 +0200 | [diff] [blame^] | 1045 | .ioctl = ide_disk_ioctl, |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 1046 | .getgeo = idedisk_getgeo, |
| 1047 | .media_changed = idedisk_media_changed, |
| 1048 | .revalidate_disk = idedisk_revalidate_disk |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | }; |
| 1050 | |
| 1051 | MODULE_DESCRIPTION("ATA DISK Driver"); |
| 1052 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1053 | static int ide_disk_probe(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { |
| 1055 | struct ide_disk_obj *idkp; |
| 1056 | struct gendisk *g; |
| 1057 | |
| 1058 | /* strstr("foo", "") is non-NULL */ |
| 1059 | if (!strstr("ide-disk", drive->driver_req)) |
| 1060 | goto failed; |
Bartlomiej Zolnierkiewicz | 2a92466 | 2008-10-10 22:39:24 +0200 | [diff] [blame] | 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (drive->media != ide_disk) |
| 1063 | goto failed; |
| 1064 | |
Deepak Saxena | f5e3c2f | 2005-11-07 01:01:25 -0800 | [diff] [blame] | 1065 | idkp = kzalloc(sizeof(*idkp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | if (!idkp) |
| 1067 | goto failed; |
| 1068 | |
Tejun Heo | 689d6fa | 2008-08-25 19:56:16 +0900 | [diff] [blame] | 1069 | g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | if (!g) |
| 1071 | goto out_free_idkp; |
| 1072 | |
| 1073 | ide_init_disk(g, drive); |
| 1074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | kref_init(&idkp->kref); |
| 1076 | |
| 1077 | idkp->drive = drive; |
| 1078 | idkp->driver = &idedisk_driver; |
| 1079 | idkp->disk = g; |
| 1080 | |
| 1081 | g->private_data = &idkp->driver; |
| 1082 | |
| 1083 | drive->driver_data = idkp; |
| 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | idedisk_setup(drive); |
Bartlomiej Zolnierkiewicz | d1d7671 | 2008-10-13 21:39:38 +0200 | [diff] [blame] | 1086 | if ((drive->dev_flags & IDE_DFLAG_LBA) == 0 && |
| 1087 | (drive->head == 0 || drive->head > 16)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n", |
| 1089 | drive->name, drive->head); |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1090 | drive->dev_flags &= ~IDE_DFLAG_ATTACH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } else |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1092 | drive->dev_flags |= IDE_DFLAG_ATTACH; |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1093 | |
Tejun Heo | f615b48 | 2008-08-25 19:47:24 +0900 | [diff] [blame] | 1094 | g->minors = IDE_DISK_MINORS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | g->driverfs_dev = &drive->gendev; |
Tejun Heo | 689d6fa | 2008-08-25 19:56:16 +0900 | [diff] [blame] | 1096 | g->flags |= GENHD_FL_EXT_DEVT; |
Bartlomiej Zolnierkiewicz | 97100fc | 2008-10-13 21:39:36 +0200 | [diff] [blame] | 1097 | if (drive->dev_flags & IDE_DFLAG_REMOVABLE) |
| 1098 | g->flags = GENHD_FL_REMOVABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | set_capacity(g, idedisk_capacity(drive)); |
| 1100 | g->fops = &idedisk_ops; |
| 1101 | add_disk(g); |
| 1102 | return 0; |
| 1103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | out_free_idkp: |
| 1105 | kfree(idkp); |
| 1106 | failed: |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1107 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Bartlomiej Zolnierkiewicz | 9841654 | 2008-04-26 17:36:37 +0200 | [diff] [blame] | 1110 | static void __exit idedisk_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1112 | driver_unregister(&idedisk_driver.gen_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Bartlomiej Zolnierkiewicz | 17514e8 | 2005-11-19 22:24:35 +0100 | [diff] [blame] | 1115 | static int __init idedisk_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1117 | return driver_register(&idedisk_driver.gen_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Kay Sievers | 263756e | 2005-12-12 18:03:44 +0100 | [diff] [blame] | 1120 | MODULE_ALIAS("ide:*m-disk*"); |
Bartlomiej Zolnierkiewicz | f879048 | 2008-10-13 21:39:45 +0200 | [diff] [blame^] | 1121 | MODULE_ALIAS("ide-disk"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | module_init(idedisk_init); |
| 1123 | module_exit(idedisk_exit); |
| 1124 | MODULE_LICENSE("GPL"); |