blob: dae9d988de10c142e03c48e031e80397fb687b5f [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>
Alan Coxccd32e22008-11-02 21:40:08 +01005 * Copyright (C) 2003 Red Hat
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01006 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/types.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/timer.h>
21#include <linux/mm.h>
22#include <linux/interrupt.h>
23#include <linux/major.h>
24#include <linux/errno.h>
25#include <linux/genhd.h>
26#include <linux/slab.h>
27#include <linux/delay.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080028#include <linux/mutex.h>
Richard Purdie2bfb6462006-03-31 02:31:16 -080029#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ide.h>
31
32#include <asm/byteorder.h>
33#include <asm/irq.h>
34#include <asm/uaccess.h>
35#include <asm/io.h>
36#include <asm/div64.h>
37
Bartlomiej Zolnierkiewiczf8790482008-10-13 21:39:45 +020038#include "ide-disk.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +010040static const u8 ide_rw_cmds[] = {
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +020041 ATA_CMD_READ_MULTI,
42 ATA_CMD_WRITE_MULTI,
43 ATA_CMD_READ_MULTI_EXT,
44 ATA_CMD_WRITE_MULTI_EXT,
45 ATA_CMD_PIO_READ,
46 ATA_CMD_PIO_WRITE,
47 ATA_CMD_PIO_READ_EXT,
48 ATA_CMD_PIO_WRITE_EXT,
49 ATA_CMD_READ,
50 ATA_CMD_WRITE,
51 ATA_CMD_READ_EXT,
52 ATA_CMD_WRITE_EXT,
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +010053};
54
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010055static void ide_tf_set_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 dma)
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +010056{
57 u8 index, lba48, write;
58
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010059 lba48 = (cmd->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
60 write = (cmd->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +010061
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010062 if (dma) {
63 cmd->protocol = ATA_PROT_DMA;
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +020064 index = 8;
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010065 } else {
66 cmd->protocol = ATA_PROT_PIO;
67 if (drive->mult_count) {
68 cmd->tf_flags |= IDE_TFLAG_MULTI_PIO;
69 index = 0;
70 } else
71 index = 4;
72 }
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +010073
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010074 cmd->tf.command = ide_rw_cmds[index + lba48 + write];
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +010075}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
79 * using LBA if supported, or CHS otherwise, to address sectors.
80 */
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +020081static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
82 sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010084 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +010085 u16 nsectors = (u16)rq->nr_sectors;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +020086 u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
87 u8 dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010088 struct ide_cmd cmd;
89 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +010090 ide_startstop_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Bartlomiej Zolnierkiewicz238e4f12007-10-19 00:30:07 +020092 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (block + rq->nr_sectors > 1ULL << 28)
94 dma = 0;
95 else
96 lba48 = 0;
97 }
98
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010099 memset(&cmd, 0, sizeof(cmd));
100 cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100101
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100102 if (dma == 0) {
103 ide_init_sg_cmd(&cmd, nsectors);
104 ide_map_sg(drive, rq);
105 }
106
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200107 if (drive->dev_flags & IDE_DFLAG_LBA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (lba48) {
Michael Richardsonc2f83112006-02-07 12:58:33 -0800109 pr_debug("%s: LBA=0x%012llx\n", drive->name,
110 (unsigned long long)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100112 tf->hob_nsect = (nsectors >> 8) & 0xff;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100113 tf->hob_lbal = (u8)(block >> 24);
114 if (sizeof(block) != 4) {
115 tf->hob_lbam = (u8)((u64)block >> 32);
116 tf->hob_lbah = (u8)((u64)block >> 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100118
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100119 tf->nsect = nsectors & 0xff;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100120 tf->lbal = (u8) block;
121 tf->lbam = (u8)(block >> 8);
122 tf->lbah = (u8)(block >> 16);
Bartlomiej Zolnierkiewicz6dd9b832008-01-26 20:13:03 +0100123
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100124 cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 } else {
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100126 tf->nsect = nsectors & 0xff;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100127 tf->lbal = block;
128 tf->lbam = block >>= 8;
129 tf->lbah = block >>= 8;
130 tf->device = (block >> 8) & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200132
133 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 } else {
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200135 unsigned int sect, head, cyl, track;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 track = (int)block / drive->sect;
138 sect = (int)block % drive->sect + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 head = track % drive->head;
140 cyl = track / drive->head;
141
142 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
143
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100144 tf->nsect = nsectors & 0xff;
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100145 tf->lbal = sect;
146 tf->lbam = cyl;
147 tf->lbah = cyl >> 8;
148 tf->device = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100151 cmd.tf_flags |= IDE_TFLAG_FS;
152
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +0100153 if (rq_data_dir(rq))
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100154 cmd.tf_flags |= IDE_TFLAG_WRITE;
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +0100155
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100156 ide_tf_set_cmd(drive, &cmd, dma);
157 cmd.rq = rq;
Bartlomiej Zolnierkiewiczba76ae32008-01-25 22:17:16 +0100158
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100159 rc = do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicz2bd06b22008-01-25 22:17:06 +0100160
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100161 if (rc == ide_stopped && dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* fallback to PIO */
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100163 cmd.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
164 ide_tf_set_cmd(drive, &cmd, 0);
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100165 ide_init_sg_cmd(&cmd, nsectors);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100166 rc = do_rw_taskfile(drive, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100169 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * 268435455 == 137439 MB or 28bit limit
174 * 320173056 == 163929 MB or 48bit addressing
175 * 1073741822 == 549756 MB or 48bit addressing fake drive
176 */
177
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200178static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
179 sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100181 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200183 BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (!blk_fs_request(rq)) {
186 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
187 ide_end_request(drive, 0, 0);
188 return ide_stopped;
189 }
190
Richard Purdie2bfb6462006-03-31 02:31:16 -0800191 ledtrig_ide_activity();
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
194 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
Michael Richardsonc2f83112006-02-07 12:58:33 -0800195 (unsigned long long)block, rq->nr_sectors,
196 (unsigned long)rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (hwif->rw_disk)
199 hwif->rw_disk(drive, rq);
200
201 return __ide_do_rw_disk(drive, rq, block);
202}
203
204/*
205 * Queries for true maximum capacity of the drive.
206 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
207 */
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100208static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100210 struct ide_cmd cmd;
211 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100212 u64 addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100214 memset(&cmd, 0, sizeof(cmd));
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100215 if (lba48)
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200216 tf->command = ATA_CMD_READ_NATIVE_MAX_EXT;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100217 else
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200218 tf->command = ATA_CMD_READ_NATIVE_MAX;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100219 tf->device = ATA_LBA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100220
221 cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100222 if (lba48)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100223 cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
224
225 ide_no_data_taskfile(drive, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* if OK, compute maximum address value */
Bartlomiej Zolnierkiewicza5016332008-01-25 22:17:17 +0100228 if ((tf->status & 0x01) == 0)
229 addr = ide_get_lba_addr(tf, lba48) + 1;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return addr;
232}
233
234/*
235 * Sets maximum virtual LBA address of the drive.
236 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
237 */
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100238static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100240 struct ide_cmd cmd;
241 struct ide_taskfile *tf = &cmd.tf;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100242 u64 addr_set = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 addr_req--;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100245
246 memset(&cmd, 0, sizeof(cmd));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100247 tf->lbal = (addr_req >> 0) & 0xff;
248 tf->lbam = (addr_req >>= 8) & 0xff;
249 tf->lbah = (addr_req >>= 8) & 0xff;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100250 if (lba48) {
251 tf->hob_lbal = (addr_req >>= 8) & 0xff;
252 tf->hob_lbam = (addr_req >>= 8) & 0xff;
253 tf->hob_lbah = (addr_req >>= 8) & 0xff;
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200254 tf->command = ATA_CMD_SET_MAX_EXT;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100255 } else {
256 tf->device = (addr_req >>= 8) & 0x0f;
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200257 tf->command = ATA_CMD_SET_MAX;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100258 }
259 tf->device |= ATA_LBA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100260
261 cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100262 if (lba48)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100263 cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
264
265 ide_no_data_taskfile(drive, &cmd);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* if OK, compute maximum address value */
Bartlomiej Zolnierkiewicza5016332008-01-25 22:17:17 +0100268 if ((tf->status & 0x01) == 0)
269 addr_set = ide_get_lba_addr(tf, lba48) + 1;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return addr_set;
272}
273
274static unsigned long long sectors_to_MB(unsigned long long n)
275{
276 n <<= 9; /* make it bytes */
277 do_div(n, 1000000); /* make it MB */
278 return n;
279}
280
281/*
Bartlomiej Zolnierkiewiczb0244a02007-08-20 22:42:57 +0200282 * Some disks report total number of sectors instead of
283 * maximum sector address. We list them here.
284 */
285static const struct drive_list_entry hpa_list[] = {
286 { "ST340823A", NULL },
Jorge Juan Chico7062cdc2007-09-17 12:35:30 +0200287 { "ST320413A", NULL },
Mikko Rapelib152fcd2008-02-19 01:41:25 +0100288 { "ST310211A", NULL },
Bartlomiej Zolnierkiewiczb0244a02007-08-20 22:42:57 +0200289 { NULL, NULL }
290};
291
Arjan van de Ven858119e2006-01-14 13:20:43 -0800292static void idedisk_check_hpa(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 unsigned long long capacity, set_max;
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200295 int lba48 = ata_id_lba48_enabled(drive->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 capacity = drive->capacity64;
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100298
299 set_max = idedisk_read_native_max_address(drive, lba48);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Bartlomiej Zolnierkiewiczb0244a02007-08-20 22:42:57 +0200301 if (ide_in_drive_list(drive->id, hpa_list)) {
302 /*
303 * Since we are inclusive wrt to firmware revisions do this
304 * extra check and apply the workaround only when needed.
305 */
306 if (set_max == capacity + 1)
307 set_max--;
308 }
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (set_max <= capacity)
311 return;
312
313 printk(KERN_INFO "%s: Host Protected Area detected.\n"
314 "\tcurrent capacity is %llu sectors (%llu MB)\n"
315 "\tnative capacity is %llu sectors (%llu MB)\n",
316 drive->name,
317 capacity, sectors_to_MB(capacity),
318 set_max, sectors_to_MB(set_max));
319
Bartlomiej Zolnierkiewicz7a3b7512008-01-25 22:17:06 +0100320 set_max = idedisk_set_max_address(drive, set_max, lba48);
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (set_max) {
323 drive->capacity64 = set_max;
324 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
325 drive->name);
326 }
327}
328
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200329static int ide_disk_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200331 u16 *id = drive->id;
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200332 int lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200334 if (ata_id_lba48_enabled(id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* drive speaks 48-bit LBA */
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200336 lba = 1;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200337 drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Bartlomiej Zolnierkiewicza02227c92008-10-10 22:39:31 +0200338 } else if (ata_id_has_lba(id) && ata_id_is_lba_capacity_ok(id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* drive speaks 28-bit LBA */
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200340 lba = 1;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200341 drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 } else {
343 /* drive speaks boring old 28-bit CHS */
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200344 lba = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 drive->capacity64 = drive->cyl * drive->head * drive->sect;
346 }
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200347
348 if (lba) {
349 drive->dev_flags |= IDE_DFLAG_LBA;
350
351 /*
352 * If this device supports the Host Protected Area feature set,
353 * then we may need to change our opinion about its capacity.
354 */
355 if (ata_id_hpa_enabled(id))
356 idedisk_check_hpa(drive);
357 }
Bartlomiej Zolnierkiewicz0a70c7f2008-10-17 18:09:09 +0200358
359 /* limit drive capacity to 137GB if LBA48 cannot be used */
360 if ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 &&
361 drive->capacity64 > 1ULL << 28) {
362 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
363 "%llu sectors (%llu MB)\n",
364 drive->name, (unsigned long long)drive->capacity64,
365 sectors_to_MB(drive->capacity64));
366 drive->capacity64 = 1ULL << 28;
367 }
368
369 if ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) &&
370 (drive->dev_flags & IDE_DFLAG_LBA48)) {
371 if (drive->capacity64 > 1ULL << 28) {
372 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode"
373 " will be used for accessing sectors "
374 "> %u\n", drive->name, 1 << 28);
375 } else
376 drive->dev_flags &= ~IDE_DFLAG_LBA48;
377 }
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200378
379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Jens Axboe165125e2007-07-24 09:28:11 +0200382static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 ide_drive_t *drive = q->queuedata;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100385 struct ide_cmd *cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100387 /* FIXME: map struct ide_taskfile on rq->cmd[] */
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100388 BUG_ON(cmd == NULL);
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100389
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100390 memset(cmd, 0, sizeof(*cmd));
Bartlomiej Zolnierkiewiczff2779b2008-10-10 22:39:31 +0200391 if (ata_id_flush_ext_enabled(drive->id) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 (drive->capacity64 >= (1UL << 28)))
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100393 cmd->tf.command = ATA_CMD_FLUSH_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 else
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100395 cmd->tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100396 cmd->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
397 IDE_TFLAG_DYN;
398 cmd->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Bartlomiej Zolnierkiewicz813a0eb2008-01-25 22:17:10 +0100400 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200401 rq->cmd_flags |= REQ_SOFTBARRIER;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100402 rq->special = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200405ide_devset_get(multcount, mult_count);
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*
408 * This is tightly woven into the driver->do_special can not touch.
409 * DON'T do it again until a total personality rewrite is committed.
410 */
411static int set_multcount(ide_drive_t *drive, int arg)
412{
FUJITA Tomonoridd470872008-07-15 21:21:43 +0200413 struct request *rq;
414 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200416 if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200417 return -EINVAL;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (drive->special.b.set_multmode)
420 return -EBUSY;
Bartlomiej Zolnierkiewicz852738f2008-01-26 20:13:12 +0100421
FUJITA Tomonoridd470872008-07-15 21:21:43 +0200422 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
423 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
Bartlomiej Zolnierkiewicz852738f2008-01-26 20:13:12 +0100424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 drive->mult_req = arg;
426 drive->special.b.set_multmode = 1;
FUJITA Tomonoridd470872008-07-15 21:21:43 +0200427 error = blk_execute_rq(drive->queue, NULL, rq, 0);
428 blk_put_request(rq);
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return (drive->mult_count == arg) ? 0 : -EIO;
431}
432
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200433ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static int set_nowerr(ide_drive_t *drive, int arg)
436{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200437 if (arg < 0 || arg > 1)
438 return -EINVAL;
439
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200440 if (arg)
441 drive->dev_flags |= IDE_DFLAG_NOWERR;
442 else
443 drive->dev_flags &= ~IDE_DFLAG_NOWERR;
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448}
449
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200450static int ide_do_setfeature(ide_drive_t *drive, u8 feature, u8 nsect)
451{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100452 struct ide_cmd cmd;
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200453
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100454 memset(&cmd, 0, sizeof(cmd));
455 cmd.tf.feature = feature;
456 cmd.tf.nsect = nsect;
457 cmd.tf.command = ATA_CMD_SET_FEATURES;
458 cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200459
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100460 return ide_no_data_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200461}
462
Tejun Heo3e087b52006-01-06 09:57:31 +0100463static void update_ordered(ide_drive_t *drive)
464{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200465 u16 *id = drive->id;
Tejun Heo3e087b52006-01-06 09:57:31 +0100466 unsigned ordered = QUEUE_ORDERED_NONE;
467 prepare_flush_fn *prep_fn = NULL;
Tejun Heo3e087b52006-01-06 09:57:31 +0100468
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200469 if (drive->dev_flags & IDE_DFLAG_WCACHE) {
Tejun Heo3e087b52006-01-06 09:57:31 +0100470 unsigned long long capacity;
471 int barrier;
472 /*
473 * We must avoid issuing commands a drive does not
474 * understand or we may crash it. We check flush cache
475 * is supported. We also check we have the LBA48 flush
476 * cache if the drive capacity is too large. By this
477 * time we have trimmed the drive capacity if LBA48 is
478 * not available so we don't need to recheck that.
479 */
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200480 capacity = ide_gd_capacity(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200481 barrier = ata_id_flush_enabled(id) &&
482 (drive->dev_flags & IDE_DFLAG_NOFLUSH) == 0 &&
483 ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 ||
484 capacity <= (1ULL << 28) ||
Bartlomiej Zolnierkiewiczff2779b2008-10-10 22:39:31 +0200485 ata_id_flush_ext_enabled(id));
Tejun Heo3e087b52006-01-06 09:57:31 +0100486
487 printk(KERN_INFO "%s: cache flushes %ssupported\n",
Jean Delvaref7ad8362006-02-03 03:04:57 -0800488 drive->name, barrier ? "" : "not ");
Tejun Heo3e087b52006-01-06 09:57:31 +0100489
490 if (barrier) {
491 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
492 prep_fn = idedisk_prepare_flush;
Tejun Heo3e087b52006-01-06 09:57:31 +0100493 }
494 } else
495 ordered = QUEUE_ORDERED_DRAIN;
496
497 blk_queue_ordered(drive->queue, ordered, prep_fn);
Tejun Heo3e087b52006-01-06 09:57:31 +0100498}
499
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200500ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200501
502static int set_wcache(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Tejun Heo3e087b52006-01-06 09:57:31 +0100504 int err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200506 if (arg < 0 || arg > 1)
507 return -EINVAL;
508
Bartlomiej Zolnierkiewicz4b58f172008-10-10 22:39:30 +0200509 if (ata_id_flush_enabled(drive->id)) {
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200510 err = ide_do_setfeature(drive,
511 arg ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF, 0);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200512 if (err == 0) {
513 if (arg)
514 drive->dev_flags |= IDE_DFLAG_WCACHE;
515 else
516 drive->dev_flags &= ~IDE_DFLAG_WCACHE;
517 }
Tejun Heo3e087b52006-01-06 09:57:31 +0100518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Tejun Heo3e087b52006-01-06 09:57:31 +0100520 update_ordered(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Tejun Heo3e087b52006-01-06 09:57:31 +0100522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200525static int do_idedisk_flushcache(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100527 struct ide_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100529 memset(&cmd, 0, sizeof(cmd));
Bartlomiej Zolnierkiewiczff2779b2008-10-10 22:39:31 +0200530 if (ata_id_flush_ext_enabled(drive->id))
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100531 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 else
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100533 cmd.tf.command = ATA_CMD_FLUSH;
534 cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
535
536 return ide_no_data_taskfile(drive, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200539ide_devset_get(acoustic, acoustic);
540
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200541static int set_acoustic(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200543 if (arg < 0 || arg > 254)
544 return -EINVAL;
545
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200546 ide_do_setfeature(drive,
547 arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 drive->acoustic = arg;
Bartlomiej Zolnierkiewiczbe3c0962008-10-13 21:39:36 +0200550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200554ide_devset_get_flag(addressing, IDE_DFLAG_LBA48);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
557 * drive->addressing:
558 * 0: 28-bit
559 * 1: 48-bit
560 * 2: 48-bit capable doing 28-bit
561 */
Bartlomiej Zolnierkiewiczaa7687732008-10-10 22:39:33 +0200562static int set_addressing(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200564 if (arg < 0 || arg > 2)
565 return -EINVAL;
566
Bartlomiej Zolnierkiewicz35c13752008-10-13 21:39:36 +0200567 if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
568 ata_id_lba48_enabled(drive->id) == 0))
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200569 return -EIO;
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200570
Bartlomiej Zolnierkiewicz35c13752008-10-13 21:39:36 +0200571 if (arg == 2)
572 arg = 0;
573
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200574 if (arg)
575 drive->dev_flags |= IDE_DFLAG_LBA48;
576 else
577 drive->dev_flags &= ~IDE_DFLAG_LBA48;
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580}
581
Bartlomiej Zolnierkiewiczf8790482008-10-13 21:39:45 +0200582ide_ext_devset_rw(acoustic, acoustic);
583ide_ext_devset_rw(address, addressing);
584ide_ext_devset_rw(multcount, multcount);
585ide_ext_devset_rw(wcache, wcache);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200586
Bartlomiej Zolnierkiewiczf8790482008-10-13 21:39:45 +0200587ide_ext_devset_rw_sync(nowerr, nowerr);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200588
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200589static int ide_disk_check(ide_drive_t *drive, const char *s)
590{
591 return 1;
592}
593
594static void ide_disk_setup(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +0200596 struct ide_disk_obj *idkp = drive->driver_data;
Bartlomiej Zolnierkiewiczc77380d2008-10-23 23:22:09 +0200597 struct request_queue *q = drive->queue;
Bartlomiej Zolnierkiewicz238e4f12007-10-19 00:30:07 +0200598 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200599 u16 *id = drive->id;
600 char *m = (char *)&id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 unsigned long long capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +0200603 ide_proc_register_driver(drive, idkp->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200605 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return;
607
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200608 if (drive->dev_flags & IDE_DFLAG_REMOVABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200610 * Removable disks (eg. SYQUEST); ignore 'WD' drives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200612 if (m[0] != 'W' || m[1] != 'D')
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200613 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
Bartlomiej Zolnierkiewiczaa7687732008-10-10 22:39:33 +0200616 (void)set_addressing(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200618 if (drive->dev_flags & IDE_DFLAG_LBA48) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int max_s = 2048;
620
621 if (max_s > hwif->rqsize)
622 max_s = hwif->rqsize;
623
Bartlomiej Zolnierkiewiczc77380d2008-10-23 23:22:09 +0200624 blk_queue_max_sectors(q, max_s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200627 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
Bartlomiej Zolnierkiewiczc77380d2008-10-23 23:22:09 +0200628 q->max_sectors / 2);
629
Bartlomiej Zolnierkiewicz655772c2009-01-14 19:19:03 +0100630 if (ata_id_is_ssd(id))
Bartlomiej Zolnierkiewiczc77380d2008-10-23 23:22:09 +0200631 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* calculate drive capacity, and select LBA if possible */
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200634 ide_disk_get_capacity(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /*
637 * if possible, give fdisk access to more of the drive,
638 * by correcting bios_cyls:
639 */
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200640 capacity = ide_gd_capacity(drive);
Bartlomiej Zolnierkiewicz98416542008-04-26 17:36:37 +0200641
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200642 if ((drive->dev_flags & IDE_DFLAG_FORCED_GEOM) == 0) {
Bartlomiej Zolnierkiewicz942dcd82008-10-10 22:39:30 +0200643 if (ata_id_lba48_enabled(drive->id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* compatibility */
645 drive->bios_sect = 63;
646 drive->bios_head = 255;
647 }
648
649 if (drive->bios_sect && drive->bios_head) {
650 unsigned int cap0 = capacity; /* truncate to 32 bits */
651 unsigned int cylsz, cyl;
652
653 if (cap0 != capacity)
654 drive->bios_cyl = 65535;
655 else {
656 cylsz = drive->bios_sect * drive->bios_head;
657 cyl = cap0 / cylsz;
658 if (cyl > 65535)
659 cyl = 65535;
660 if (cyl > drive->bios_cyl)
661 drive->bios_cyl = cyl;
662 }
663 }
664 }
665 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
666 drive->name, capacity, sectors_to_MB(capacity));
667
668 /* Only print cache size when it was specified */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200669 if (id[ATA_ID_BUF_SIZE])
670 printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100672 printk(KERN_CONT ", CHS=%d/%d/%d\n",
673 drive->bios_cyl, drive->bios_head, drive->bios_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* write cache enabled? */
Bartlomiej Zolnierkiewicz8a089c62008-10-10 22:39:20 +0200676 if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200677 drive->dev_flags |= IDE_DFLAG_WCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200679 set_wcache(drive, 1);
Bartlomiej Zolnierkiewiczae9f9f02008-10-17 18:09:10 +0200680
681 if ((drive->dev_flags & IDE_DFLAG_LBA) == 0 &&
682 (drive->head == 0 || drive->head > 16)) {
683 printk(KERN_ERR "%s: invalid geometry: %d physical heads?\n",
684 drive->name, drive->head);
685 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
686 } else
687 drive->dev_flags |= IDE_DFLAG_ATTACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200690static void ide_disk_flush(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200692 if (ata_id_flush_enabled(drive->id) == 0 ||
693 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return;
695
696 if (do_idedisk_flushcache(drive))
697 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
698}
699
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200700static int ide_disk_init_media(ide_drive_t *drive, struct gendisk *disk)
701{
702 return 0;
703}
704
705static int ide_disk_set_doorlock(ide_drive_t *drive, struct gendisk *disk,
706 int on)
Bartlomiej Zolnierkiewicz29ec6832008-01-26 20:12:59 +0100707{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100708 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz81ee1bb2008-10-17 18:09:10 +0200709 int ret;
710
711 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
712 return 0;
Bartlomiej Zolnierkiewicz29ec6832008-01-26 20:12:59 +0100713
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100714 memset(&cmd, 0, sizeof(cmd));
715 cmd.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
716 cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicz29ec6832008-01-26 20:12:59 +0100717
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100718 ret = ide_no_data_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicz81ee1bb2008-10-17 18:09:10 +0200719
720 if (ret)
721 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
722
723 return ret;
Bartlomiej Zolnierkiewicz29ec6832008-01-26 20:12:59 +0100724}
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200725
726const struct ide_disk_ops ide_ata_disk_ops = {
727 .check = ide_disk_check,
728 .get_capacity = ide_disk_get_capacity,
729 .setup = ide_disk_setup,
730 .flush = ide_disk_flush,
731 .init_media = ide_disk_init_media,
732 .set_doorlock = ide_disk_set_doorlock,
733 .do_request = ide_do_rw_disk,
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200734 .ioctl = ide_disk_ioctl,
735};