blob: 93ff15e6a9c4347b9ccf536c42513a0920d48bf7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01002 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 */
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 Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#define IDEDISK_VERSION "1.18"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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 Vencf8b8972006-03-23 03:00:45 -080031#include <linux/mutex.h>
Richard Purdie2bfb6462006-03-31 02:31:16 -080032#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ide.h>
Bartlomiej Zolnierkiewicz3ceca722008-10-10 22:39:27 +020034#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
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 Heo870d6652008-08-25 19:47:25 +090042#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
Tejun Heo689d6fa2008-08-25 19:56:16 +090043#define IDE_DISK_MINORS (1 << PARTN_BITS)
Tejun Heo870d6652008-08-25 19:47:25 +090044#else
Tejun Heo3e1a7ff2008-08-25 19:56:17 +090045#define IDE_DISK_MINORS 0
Tejun Heo870d6652008-08-25 19:47:25 +090046#endif
47
Bartlomiej Zolnierkiewiczf8790482008-10-13 21:39:45 +020048#include "ide-disk.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Arjan van de Vencf8b8972006-03-23 03:00:45 -080050static DEFINE_MUTEX(idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref)
53
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020054static void ide_disk_release(struct kref *);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
57{
58 struct ide_disk_obj *idkp = NULL;
59
Arjan van de Vencf8b8972006-03-23 03:00:45 -080060 mutex_lock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 idkp = ide_disk_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020062 if (idkp) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020063 if (ide_device_get(idkp->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020064 idkp = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020065 else
66 kref_get(&idkp->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020067 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -080068 mutex_unlock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return idkp;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void ide_disk_put(struct ide_disk_obj *idkp)
73{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020074 ide_drive_t *drive = idkp->drive;
75
Arjan van de Vencf8b8972006-03-23 03:00:45 -080076 mutex_lock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 kref_put(&idkp->kref, ide_disk_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020078 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080079 mutex_unlock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +010082static const u8 ide_rw_cmds[] = {
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +020083 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 Zolnierkiewiczba76ae32008-01-25 22:17:16 +010095};
96
97static 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
106static 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 Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200114 index = 8;
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +0100115 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 Torvalds1da177e2005-04-16 15:20:36 -0700126/*
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 Zolnierkiewicz98416542008-04-26 17:36:37 +0200130static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
131 sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100134 u16 nsectors = (u16)rq->nr_sectors;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200135 u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
136 u8 dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100137 ide_task_t task;
138 struct ide_taskfile *tf = &task.tf;
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100139 ide_startstop_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Bartlomiej Zolnierkiewicz238e4f12007-10-19 00:30:07 +0200141 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 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 Zolnierkiewicz9e422372008-01-25 22:17:07 +0100153 memset(&task, 0, sizeof(task));
Bartlomiej Zolnierkiewicz9a410e72008-07-15 21:21:48 +0200154 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100155
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200156 if (drive->dev_flags & IDE_DFLAG_LBA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (lba48) {
Michael Richardsonc2f83112006-02-07 12:58:33 -0800158 pr_debug("%s: LBA=0x%012llx\n", drive->name,
159 (unsigned long long)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100161 tf->hob_nsect = (nsectors >> 8) & 0xff;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100162 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 Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100167
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100168 tf->nsect = nsectors & 0xff;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100169 tf->lbal = (u8) block;
170 tf->lbam = (u8)(block >> 8);
171 tf->lbah = (u8)(block >> 16);
Bartlomiej Zolnierkiewicz6dd9b832008-01-26 20:13:03 +0100172
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100173 task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 } else {
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100175 tf->nsect = nsectors & 0xff;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100176 tf->lbal = block;
177 tf->lbam = block >>= 8;
178 tf->lbah = block >>= 8;
179 tf->device = (block >> 8) & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200181
182 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 } else {
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200184 unsigned int sect, head, cyl, track;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 track = (int)block / drive->sect;
187 sect = (int)block % drive->sect + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 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 Zolnierkiewicz790d1232008-01-25 22:17:12 +0100193 tf->nsect = nsectors & 0xff;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100194 tf->lbal = sect;
195 tf->lbam = cyl;
196 tf->lbah = cyl >> 8;
197 tf->device = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +0100200 if (rq_data_dir(rq))
201 task.tf_flags |= IDE_TFLAG_WRITE;
202
203 ide_tf_set_cmd(drive, &task, dma);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100204 if (!dma)
205 hwif->data_phase = task.data_phase;
206 task.rq = rq;
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +0100207
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100208 rc = do_rw_taskfile(drive, &task);
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100209
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100210 if (rc == ide_stopped && dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* fallback to PIO */
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100212 task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +0100213 ide_tf_set_cmd(drive, &task, 0);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100214 hwif->data_phase = task.data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ide_init_sg_cmd(drive, rq);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100216 rc = do_rw_taskfile(drive, &task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100219 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
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 Zolnierkiewicz98416542008-04-26 17:36:37 +0200228static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
229 sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 ide_hwif_t *hwif = HWIF(drive);
232
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200233 BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
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 Purdie2bfb6462006-03-31 02:31:16 -0800241 ledtrig_ide_activity();
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
244 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
Michael Richardsonc2f83112006-02-07 12:58:33 -0800245 (unsigned long long)block, rq->nr_sectors,
246 (unsigned long)rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
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 Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100258static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100261 struct ide_taskfile *tf = &args.tf;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100262 u64 addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Create IDE/ATA command request structure */
265 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100266 if (lba48)
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200267 tf->command = ATA_CMD_READ_NATIVE_MAX_EXT;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100268 else
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200269 tf->command = ATA_CMD_READ_NATIVE_MAX;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100270 tf->device = ATA_LBA;
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100271 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100272 if (lba48)
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100273 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* submit command request */
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100275 ide_no_data_taskfile(drive, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* if OK, compute maximum address value */
Bartlomiej Zolnierkiewicza5016332008-01-25 22:17:17 +0100278 if ((tf->status & 0x01) == 0)
279 addr = ide_get_lba_addr(tf, lba48) + 1;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 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 Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100288static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100291 struct ide_taskfile *tf = &args.tf;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100292 u64 addr_set = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 addr_req--;
295 /* Create IDE/ATA command request structure */
296 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100297 tf->lbal = (addr_req >> 0) & 0xff;
298 tf->lbam = (addr_req >>= 8) & 0xff;
299 tf->lbah = (addr_req >>= 8) & 0xff;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100300 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 Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200304 tf->command = ATA_CMD_SET_MAX_EXT;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100305 } else {
306 tf->device = (addr_req >>= 8) & 0x0f;
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200307 tf->command = ATA_CMD_SET_MAX;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100308 }
309 tf->device |= ATA_LBA;
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100310 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100311 if (lba48)
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100312 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* submit command request */
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100314 ide_no_data_taskfile(drive, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* if OK, compute maximum address value */
Bartlomiej Zolnierkiewicza5016332008-01-25 22:17:17 +0100316 if ((tf->status & 0x01) == 0)
317 addr_set = ide_get_lba_addr(tf, lba48) + 1;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return addr_set;
320}
321
322static 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 Zolnierkiewiczb0244a02007-08-20 22:42:57 +0200330 * Some disks report total number of sectors instead of
331 * maximum sector address. We list them here.
332 */
333static const struct drive_list_entry hpa_list[] = {
334 { "ST340823A", NULL },
Jorge Juan Chico7062cdc2007-09-17 12:35:30 +0200335 { "ST320413A", NULL },
Mikko Rapelib152fcd2008-02-19 01:41:25 +0100336 { "ST310211A", NULL },
Bartlomiej Zolnierkiewiczb0244a02007-08-20 22:42:57 +0200337 { NULL, NULL }
338};
339
Arjan van de Ven858119e2006-01-14 13:20:43 -0800340static void idedisk_check_hpa(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 unsigned long long capacity, set_max;
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200343 int lba48 = ata_id_lba48_enabled(drive->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 capacity = drive->capacity64;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100346
347 set_max = idedisk_read_native_max_address(drive, lba48);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Bartlomiej Zolnierkiewiczb0244a02007-08-20 22:42:57 +0200349 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 Torvalds1da177e2005-04-16 15:20:36 -0700358 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 Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100368 set_max = idedisk_set_max_address(drive, set_max, lba48);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 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 Zolnierkiewicz98416542008-04-26 17:36:37 +0200377static void init_idedisk_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200379 u16 *id = drive->id;
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200380 int lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200382 if (ata_id_lba48_enabled(id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* drive speaks 48-bit LBA */
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200384 lba = 1;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200385 drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Bartlomiej Zolnierkiewicza02227c2008-10-10 22:39:31 +0200386 } else if (ata_id_has_lba(id) && ata_id_is_lba_capacity_ok(id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* drive speaks 28-bit LBA */
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200388 lba = 1;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200389 drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } else {
391 /* drive speaks boring old 28-bit CHS */
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200392 lba = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 drive->capacity64 = drive->cyl * drive->head * drive->sect;
394 }
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200395
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 }
Bartlomiej Zolnierkiewicz0a70c7f2008-10-17 18:09:09 +0200406
407 /* limit drive capacity to 137GB if LBA48 cannot be used */
408 if ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 &&
409 drive->capacity64 > 1ULL << 28) {
410 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
411 "%llu sectors (%llu MB)\n",
412 drive->name, (unsigned long long)drive->capacity64,
413 sectors_to_MB(drive->capacity64));
414 drive->capacity64 = 1ULL << 28;
415 }
416
417 if ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) &&
418 (drive->dev_flags & IDE_DFLAG_LBA48)) {
419 if (drive->capacity64 > 1ULL << 28) {
420 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode"
421 " will be used for accessing sectors "
422 "> %u\n", drive->name, 1 << 28);
423 } else
424 drive->dev_flags &= ~IDE_DFLAG_LBA48;
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Bartlomiej Zolnierkiewicz06b89512008-10-13 21:39:45 +0200428sector_t ide_disk_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Bartlomiej Zolnierkiewicz3c619ff2008-10-10 22:39:22 +0200430 return drive->capacity64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Jens Axboe165125e2007-07-24 09:28:11 +0200433static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 ide_drive_t *drive = q->queuedata;
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100436 ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100438 /* FIXME: map struct ide_taskfile on rq->cmd[] */
439 BUG_ON(task == NULL);
440
441 memset(task, 0, sizeof(*task));
Bartlomiej Zolnierkiewiczff2779b2008-10-10 22:39:31 +0200442 if (ata_id_flush_ext_enabled(drive->id) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 (drive->capacity64 >= (1UL << 28)))
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200444 task->tf.command = ATA_CMD_FLUSH_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 else
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200446 task->tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100447 task->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
448 IDE_TFLAG_DYN;
449 task->data_phase = TASKFILE_NO_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Bartlomiej Zolnierkiewicz813a0eb2008-01-25 22:17:10 +0100451 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200452 rq->cmd_flags |= REQ_SOFTBARRIER;
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100453 rq->special = task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200456ide_devset_get(multcount, mult_count);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * This is tightly woven into the driver->do_special can not touch.
460 * DON'T do it again until a total personality rewrite is committed.
461 */
462static int set_multcount(ide_drive_t *drive, int arg)
463{
FUJITA Tomonoridd470872008-07-15 21:21:43 +0200464 struct request *rq;
465 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200467 if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200468 return -EINVAL;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (drive->special.b.set_multmode)
471 return -EBUSY;
Bartlomiej Zolnierkiewicz852738f2008-01-26 20:13:12 +0100472
FUJITA Tomonoridd470872008-07-15 21:21:43 +0200473 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
474 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
Bartlomiej Zolnierkiewicz852738f2008-01-26 20:13:12 +0100475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 drive->mult_req = arg;
477 drive->special.b.set_multmode = 1;
FUJITA Tomonoridd470872008-07-15 21:21:43 +0200478 error = blk_execute_rq(drive->queue, NULL, rq, 0);
479 blk_put_request(rq);
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return (drive->mult_count == arg) ? 0 : -EIO;
482}
483
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200484ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static int set_nowerr(ide_drive_t *drive, int arg)
487{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200488 if (arg < 0 || arg > 1)
489 return -EINVAL;
490
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200491 if (arg)
492 drive->dev_flags |= IDE_DFLAG_NOWERR;
493 else
494 drive->dev_flags &= ~IDE_DFLAG_NOWERR;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return 0;
499}
500
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200501static int ide_do_setfeature(ide_drive_t *drive, u8 feature, u8 nsect)
502{
503 ide_task_t task;
504
505 memset(&task, 0, sizeof(task));
506 task.tf.feature = feature;
507 task.tf.nsect = nsect;
508 task.tf.command = ATA_CMD_SET_FEATURES;
509 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
510
511 return ide_no_data_taskfile(drive, &task);
512}
513
Tejun Heo3e087b52006-01-06 09:57:31 +0100514static void update_ordered(ide_drive_t *drive)
515{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200516 u16 *id = drive->id;
Tejun Heo3e087b52006-01-06 09:57:31 +0100517 unsigned ordered = QUEUE_ORDERED_NONE;
518 prepare_flush_fn *prep_fn = NULL;
Tejun Heo3e087b52006-01-06 09:57:31 +0100519
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200520 if (drive->dev_flags & IDE_DFLAG_WCACHE) {
Tejun Heo3e087b52006-01-06 09:57:31 +0100521 unsigned long long capacity;
522 int barrier;
523 /*
524 * We must avoid issuing commands a drive does not
525 * understand or we may crash it. We check flush cache
526 * is supported. We also check we have the LBA48 flush
527 * cache if the drive capacity is too large. By this
528 * time we have trimmed the drive capacity if LBA48 is
529 * not available so we don't need to recheck that.
530 */
Bartlomiej Zolnierkiewicz06b89512008-10-13 21:39:45 +0200531 capacity = ide_disk_capacity(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200532 barrier = ata_id_flush_enabled(id) &&
533 (drive->dev_flags & IDE_DFLAG_NOFLUSH) == 0 &&
534 ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 ||
535 capacity <= (1ULL << 28) ||
Bartlomiej Zolnierkiewiczff2779b2008-10-10 22:39:31 +0200536 ata_id_flush_ext_enabled(id));
Tejun Heo3e087b52006-01-06 09:57:31 +0100537
538 printk(KERN_INFO "%s: cache flushes %ssupported\n",
Jean Delvaref7ad8362006-02-03 03:04:57 -0800539 drive->name, barrier ? "" : "not ");
Tejun Heo3e087b52006-01-06 09:57:31 +0100540
541 if (barrier) {
542 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
543 prep_fn = idedisk_prepare_flush;
Tejun Heo3e087b52006-01-06 09:57:31 +0100544 }
545 } else
546 ordered = QUEUE_ORDERED_DRAIN;
547
548 blk_queue_ordered(drive->queue, ordered, prep_fn);
Tejun Heo3e087b52006-01-06 09:57:31 +0100549}
550
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200551ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200552
553static int set_wcache(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Tejun Heo3e087b52006-01-06 09:57:31 +0100555 int err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200557 if (arg < 0 || arg > 1)
558 return -EINVAL;
559
Bartlomiej Zolnierkiewicz4b58f172008-10-10 22:39:30 +0200560 if (ata_id_flush_enabled(drive->id)) {
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200561 err = ide_do_setfeature(drive,
562 arg ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF, 0);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200563 if (err == 0) {
564 if (arg)
565 drive->dev_flags |= IDE_DFLAG_WCACHE;
566 else
567 drive->dev_flags &= ~IDE_DFLAG_WCACHE;
568 }
Tejun Heo3e087b52006-01-06 09:57:31 +0100569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Tejun Heo3e087b52006-01-06 09:57:31 +0100571 update_ordered(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Tejun Heo3e087b52006-01-06 09:57:31 +0100573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200576static int do_idedisk_flushcache(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 ide_task_t args;
579
580 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewiczff2779b2008-10-10 22:39:31 +0200581 if (ata_id_flush_ext_enabled(drive->id))
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200582 args.tf.command = ATA_CMD_FLUSH_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 else
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200584 args.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100585 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100586 return ide_no_data_taskfile(drive, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200589ide_devset_get(acoustic, acoustic);
590
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200591static int set_acoustic(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200593 if (arg < 0 || arg > 254)
594 return -EINVAL;
595
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200596 ide_do_setfeature(drive,
597 arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 drive->acoustic = arg;
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602}
603
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200604ide_devset_get_flag(addressing, IDE_DFLAG_LBA48);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
607 * drive->addressing:
608 * 0: 28-bit
609 * 1: 48-bit
610 * 2: 48-bit capable doing 28-bit
611 */
Bartlomiej Zolnierkiewiczaa7687732008-10-10 22:39:33 +0200612static int set_addressing(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200614 if (arg < 0 || arg > 2)
615 return -EINVAL;
616
Bartlomiej Zolnierkiewicz35c13752008-10-13 21:39:36 +0200617 if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
618 ata_id_lba48_enabled(drive->id) == 0))
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200619 return -EIO;
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200620
Bartlomiej Zolnierkiewicz35c13752008-10-13 21:39:36 +0200621 if (arg == 2)
622 arg = 0;
623
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200624 if (arg)
625 drive->dev_flags |= IDE_DFLAG_LBA48;
626 else
627 drive->dev_flags &= ~IDE_DFLAG_LBA48;
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630}
631
Bartlomiej Zolnierkiewiczf8790482008-10-13 21:39:45 +0200632ide_ext_devset_rw(acoustic, acoustic);
633ide_ext_devset_rw(address, addressing);
634ide_ext_devset_rw(multcount, multcount);
635ide_ext_devset_rw(wcache, wcache);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200636
Bartlomiej Zolnierkiewiczf8790482008-10-13 21:39:45 +0200637ide_ext_devset_rw_sync(nowerr, nowerr);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200638
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200639static void idedisk_setup(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +0200641 struct ide_disk_obj *idkp = drive->driver_data;
Bartlomiej Zolnierkiewicz238e4f12007-10-19 00:30:07 +0200642 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200643 u16 *id = drive->id;
644 char *m = (char *)&id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 unsigned long long capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +0200647 ide_proc_register_driver(drive, idkp->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200649 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return;
651
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200652 if (drive->dev_flags & IDE_DFLAG_REMOVABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200654 * Removable disks (eg. SYQUEST); ignore 'WD' drives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200656 if (m[0] != 'W' || m[1] != 'D')
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200657 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Bartlomiej Zolnierkiewiczaa7687732008-10-10 22:39:33 +0200660 (void)set_addressing(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200662 if (drive->dev_flags & IDE_DFLAG_LBA48) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int max_s = 2048;
664
665 if (max_s > hwif->rqsize)
666 max_s = hwif->rqsize;
667
668 blk_queue_max_sectors(drive->queue, max_s);
669 }
670
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200671 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
672 drive->queue->max_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* calculate drive capacity, and select LBA if possible */
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200675 init_idedisk_capacity(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * if possible, give fdisk access to more of the drive,
679 * by correcting bios_cyls:
680 */
Bartlomiej Zolnierkiewicz06b89512008-10-13 21:39:45 +0200681 capacity = ide_disk_capacity(drive);
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200682
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200683 if ((drive->dev_flags & IDE_DFLAG_FORCED_GEOM) == 0) {
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200684 if (ata_id_lba48_enabled(drive->id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* compatibility */
686 drive->bios_sect = 63;
687 drive->bios_head = 255;
688 }
689
690 if (drive->bios_sect && drive->bios_head) {
691 unsigned int cap0 = capacity; /* truncate to 32 bits */
692 unsigned int cylsz, cyl;
693
694 if (cap0 != capacity)
695 drive->bios_cyl = 65535;
696 else {
697 cylsz = drive->bios_sect * drive->bios_head;
698 cyl = cap0 / cylsz;
699 if (cyl > 65535)
700 cyl = 65535;
701 if (cyl > drive->bios_cyl)
702 drive->bios_cyl = cyl;
703 }
704 }
705 }
706 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
707 drive->name, capacity, sectors_to_MB(capacity));
708
709 /* Only print cache size when it was specified */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200710 if (id[ATA_ID_BUF_SIZE])
711 printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100713 printk(KERN_CONT ", CHS=%d/%d/%d\n",
714 drive->bios_cyl, drive->bios_head, drive->bios_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* write cache enabled? */
Bartlomiej Zolnierkiewicz8a089c62008-10-10 22:39:20 +0200717 if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200718 drive->dev_flags |= IDE_DFLAG_WCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200720 set_wcache(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723static void ide_cacheflush_p(ide_drive_t *drive)
724{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200725 if (ata_id_flush_enabled(drive->id) == 0 ||
726 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return;
728
729 if (do_idedisk_flushcache(drive))
730 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
731}
732
Russell King4031bbe2006-01-06 11:41:00 +0000733static void ide_disk_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct ide_disk_obj *idkp = drive->driver_data;
736 struct gendisk *g = idkp->disk;
737
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200738 ide_proc_unregister_driver(drive, idkp->driver);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 del_gendisk(g);
741
Bartlomiej Zolnierkiewiczd36fef62005-12-15 02:19:20 +0100742 ide_cacheflush_p(drive);
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 ide_disk_put(idkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747static void ide_disk_release(struct kref *kref)
748{
749 struct ide_disk_obj *idkp = to_ide_disk(kref);
750 ide_drive_t *drive = idkp->drive;
751 struct gendisk *g = idkp->disk;
752
753 drive->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 g->private_data = NULL;
755 put_disk(g);
756 kfree(idkp);
757}
758
Russell King4031bbe2006-01-06 11:41:00 +0000759static int ide_disk_probe(ide_drive_t *drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Lee Trager0d2157f2007-06-08 15:14:30 +0200761/*
762 * On HPA drives the capacity needs to be
763 * reinitilized on resume otherwise the disk
764 * can not be used and a hard reset is required
765 */
766static void ide_disk_resume(ide_drive_t *drive)
767{
Bartlomiej Zolnierkiewiczf41891c2008-10-10 22:39:20 +0200768 if (ata_id_hpa_enabled(drive->id))
Lee Trager0d2157f2007-06-08 15:14:30 +0200769 init_idedisk_capacity(drive);
770}
771
Russell King4031bbe2006-01-06 11:41:00 +0000772static void ide_device_shutdown(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774#ifdef CONFIG_ALPHA
775 /* On Alpha, halt(8) doesn't actually turn the machine off,
776 it puts you into the sort of firmware monitor. Typically,
777 it's used to boot another kernel image, so it's not much
778 different from reboot(8). Therefore, we don't need to
779 spin down the disk in this case, especially since Alpha
780 firmware doesn't handle disks in standby mode properly.
781 On the other hand, it's reasonably safe to turn the power
782 off when the shutdown process reaches the firmware prompt,
783 as the firmware initialization takes rather long time -
784 at least 10 seconds, which should be sufficient for
785 the disk to expire its write cache. */
786 if (system_state != SYSTEM_POWER_OFF) {
787#else
788 if (system_state == SYSTEM_RESTART) {
789#endif
790 ide_cacheflush_p(drive);
791 return;
792 }
793
Bartlomiej Zolnierkiewiczd12faa22008-02-26 21:50:36 +0100794 printk(KERN_INFO "Shutdown: %s\n", drive->name);
795
Russell King4031bbe2006-01-06 11:41:00 +0000796 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static ide_driver_t idedisk_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +0100801 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200802 .name = "ide-disk",
803 .bus = &ide_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 },
Russell King4031bbe2006-01-06 11:41:00 +0000805 .probe = ide_disk_probe,
806 .remove = ide_disk_remove,
Lee Trager0d2157f2007-06-08 15:14:30 +0200807 .resume = ide_disk_resume,
Russell King4031bbe2006-01-06 11:41:00 +0000808 .shutdown = ide_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 .version = IDEDISK_VERSION,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 .do_request = ide_do_rw_disk,
811 .end_request = ide_end_request,
812 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200813#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz06b89512008-10-13 21:39:45 +0200814 .proc = ide_disk_proc,
815 .settings = ide_disk_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817};
818
Bartlomiej Zolnierkiewicz29ec6832008-01-26 20:12:59 +0100819static int idedisk_set_doorlock(ide_drive_t *drive, int on)
820{
821 ide_task_t task;
822
823 memset(&task, 0, sizeof(task));
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200824 task.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100825 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicz29ec6832008-01-26 20:12:59 +0100826
827 return ide_no_data_taskfile(drive, &task);
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static int idedisk_open(struct inode *inode, struct file *filp)
831{
832 struct gendisk *disk = inode->i_bdev->bd_disk;
833 struct ide_disk_obj *idkp;
834 ide_drive_t *drive;
835
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200836 idkp = ide_disk_get(disk);
837 if (idkp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return -ENXIO;
839
840 drive = idkp->drive;
841
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100842 idkp->openers++;
843
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200844 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 check_disk_change(inode->i_bdev);
846 /*
847 * Ignore the return code from door_lock,
848 * since the open() has already succeeded,
849 * and the door_lock is irrelevant at this point.
850 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200851 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) &&
852 idedisk_set_doorlock(drive, 1))
853 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 return 0;
856}
857
858static int idedisk_release(struct inode *inode, struct file *filp)
859{
860 struct gendisk *disk = inode->i_bdev->bd_disk;
861 struct ide_disk_obj *idkp = ide_disk_g(disk);
862 ide_drive_t *drive = idkp->drive;
863
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100864 if (idkp->openers == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ide_cacheflush_p(drive);
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100866
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200867 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
868 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) &&
869 idedisk_set_doorlock(drive, 0))
870 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100872
873 idkp->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 ide_disk_put(idkp);
876
877 return 0;
878}
879
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800880static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
881{
882 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
883 ide_drive_t *drive = idkp->drive;
884
885 geo->heads = drive->bios_head;
886 geo->sectors = drive->bios_sect;
887 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
888 return 0;
889}
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891static int idedisk_media_changed(struct gendisk *disk)
892{
893 struct ide_disk_obj *idkp = ide_disk_g(disk);
894 ide_drive_t *drive = idkp->drive;
895
896 /* do not scan partitions twice if this is a removable device */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200897 if (drive->dev_flags & IDE_DFLAG_ATTACH) {
898 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
900 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* if removable, always assume it was changed */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200903 return !!(drive->dev_flags & IDE_DFLAG_REMOVABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906static int idedisk_revalidate_disk(struct gendisk *disk)
907{
908 struct ide_disk_obj *idkp = ide_disk_g(disk);
Bartlomiej Zolnierkiewicz06b89512008-10-13 21:39:45 +0200909 set_capacity(disk, ide_disk_capacity(idkp->drive));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return 0;
911}
912
913static struct block_device_operations idedisk_ops = {
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200914 .owner = THIS_MODULE,
915 .open = idedisk_open,
916 .release = idedisk_release,
Bartlomiej Zolnierkiewiczf8790482008-10-13 21:39:45 +0200917 .ioctl = ide_disk_ioctl,
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200918 .getgeo = idedisk_getgeo,
919 .media_changed = idedisk_media_changed,
920 .revalidate_disk = idedisk_revalidate_disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921};
922
923MODULE_DESCRIPTION("ATA DISK Driver");
924
Russell King4031bbe2006-01-06 11:41:00 +0000925static int ide_disk_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 struct ide_disk_obj *idkp;
928 struct gendisk *g;
929
930 /* strstr("foo", "") is non-NULL */
931 if (!strstr("ide-disk", drive->driver_req))
932 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +0200933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (drive->media != ide_disk)
935 goto failed;
936
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800937 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!idkp)
939 goto failed;
940
Tejun Heo689d6fa2008-08-25 19:56:16 +0900941 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!g)
943 goto out_free_idkp;
944
945 ide_init_disk(g, drive);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 kref_init(&idkp->kref);
948
949 idkp->drive = drive;
950 idkp->driver = &idedisk_driver;
951 idkp->disk = g;
952
953 g->private_data = &idkp->driver;
954
955 drive->driver_data = idkp;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 idedisk_setup(drive);
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200958 if ((drive->dev_flags & IDE_DFLAG_LBA) == 0 &&
959 (drive->head == 0 || drive->head > 16)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
961 drive->name, drive->head);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200962 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 } else
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200964 drive->dev_flags |= IDE_DFLAG_ATTACH;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200965
Tejun Heof615b482008-08-25 19:47:24 +0900966 g->minors = IDE_DISK_MINORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 g->driverfs_dev = &drive->gendev;
Tejun Heo689d6fa2008-08-25 19:56:16 +0900968 g->flags |= GENHD_FL_EXT_DEVT;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200969 if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
970 g->flags = GENHD_FL_REMOVABLE;
Bartlomiej Zolnierkiewicz06b89512008-10-13 21:39:45 +0200971 set_capacity(g, ide_disk_capacity(drive));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 g->fops = &idedisk_ops;
973 add_disk(g);
974 return 0;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976out_free_idkp:
977 kfree(idkp);
978failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200979 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200982static void __exit idedisk_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200984 driver_unregister(&idedisk_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +0100987static int __init idedisk_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200989 return driver_register(&idedisk_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Kay Sievers263756e2005-12-12 18:03:44 +0100992MODULE_ALIAS("ide:*m-disk*");
Bartlomiej Zolnierkiewiczf8790482008-10-13 21:39:45 +0200993MODULE_ALIAS("ide-disk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994module_init(idedisk_init);
995module_exit(idedisk_exit);
996MODULE_LICENSE("GPL");