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