ide: remove IDE_AFLAG_NO_DOORLOCKING
[deliverable/linux.git] / drivers / ide / ide-disk.c
CommitLineData
1da177e4 1/*
59bca8cc
BZ
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
1da177e4
LT
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.
1da177e4
LT
15 */
16
17#define IDEDISK_VERSION "1.18"
18
1da177e4
LT
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>
cf8b8975 31#include <linux/mutex.h>
2bfb646c 32#include <linux/leds.h>
1da177e4 33#include <linux/ide.h>
3ceca727 34#include <linux/hdreg.h>
1da177e4
LT
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
870d6656 42#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
689d6fac 43#define IDE_DISK_MINORS (1 << PARTN_BITS)
870d6656 44#else
3e1a7ff8 45#define IDE_DISK_MINORS 0
870d6656
TH
46#endif
47
f8790489 48#include "ide-disk.h"
1da177e4 49
cf8b8975 50static DEFINE_MUTEX(idedisk_ref_mutex);
1da177e4 51
08da591e
BZ
52static void ide_disk_release(struct kref *);
53
1da177e4
LT
54static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
55{
56 struct ide_disk_obj *idkp = NULL;
57
cf8b8975 58 mutex_lock(&idedisk_ref_mutex);
d7e74759 59 idkp = ide_drv_g(disk, ide_disk_obj);
08da591e 60 if (idkp) {
d3e33ff5 61 if (ide_device_get(idkp->drive))
08da591e 62 idkp = NULL;
d3e33ff5
BZ
63 else
64 kref_get(&idkp->kref);
08da591e 65 }
cf8b8975 66 mutex_unlock(&idedisk_ref_mutex);
1da177e4
LT
67 return idkp;
68}
69
1da177e4
LT
70static void ide_disk_put(struct ide_disk_obj *idkp)
71{
d3e33ff5
BZ
72 ide_drive_t *drive = idkp->drive;
73
cf8b8975 74 mutex_lock(&idedisk_ref_mutex);
1da177e4 75 kref_put(&idkp->kref, ide_disk_release);
d3e33ff5 76 ide_device_put(drive);
cf8b8975 77 mutex_unlock(&idedisk_ref_mutex);
1da177e4
LT
78}
79
ba76ae38 80static const u8 ide_rw_cmds[] = {
aaaade3f
BZ
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,
ba76ae38
BZ
93};
94
95static 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
104static 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)
ba4b2e60 112 index = 8;
ba76ae38
BZ
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
1da177e4
LT
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 */
98416549
BZ
128static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
129 sector_t block)
1da177e4
LT
130{
131 ide_hwif_t *hwif = HWIF(drive);
790d1239 132 u16 nsectors = (u16)rq->nr_sectors;
97100fc8
BZ
133 u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
134 u8 dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
9e42237f
BZ
135 ide_task_t task;
136 struct ide_taskfile *tf = &task.tf;
f6e29e35 137 ide_startstop_t rc;
1da177e4 138
238e4f14 139 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
1da177e4
LT
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
9e42237f 151 memset(&task, 0, sizeof(task));
9a410e79 152 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
2bd06b23 153
d1d76714 154 if (drive->dev_flags & IDE_DFLAG_LBA) {
1da177e4 155 if (lba48) {
c2f8311d
MR
156 pr_debug("%s: LBA=0x%012llx\n", drive->name,
157 (unsigned long long)block);
1da177e4 158
790d1239 159 tf->hob_nsect = (nsectors >> 8) & 0xff;
2bd06b23
BZ
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);
1da177e4 164 }
2bd06b23 165
790d1239 166 tf->nsect = nsectors & 0xff;
2bd06b23
BZ
167 tf->lbal = (u8) block;
168 tf->lbam = (u8)(block >> 8);
169 tf->lbah = (u8)(block >> 16);
6dd9b837 170
657cc1a8 171 task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
1da177e4 172 } else {
790d1239 173 tf->nsect = nsectors & 0xff;
2bd06b23
BZ
174 tf->lbal = block;
175 tf->lbam = block >>= 8;
176 tf->lbah = block >>= 8;
177 tf->device = (block >> 8) & 0xf;
1da177e4 178 }
d1d76714
BZ
179
180 tf->device |= ATA_LBA;
1da177e4 181 } else {
98416549
BZ
182 unsigned int sect, head, cyl, track;
183
1da177e4
LT
184 track = (int)block / drive->sect;
185 sect = (int)block % drive->sect + 1;
1da177e4
LT
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
790d1239 191 tf->nsect = nsectors & 0xff;
2bd06b23
BZ
192 tf->lbal = sect;
193 tf->lbam = cyl;
194 tf->lbah = cyl >> 8;
195 tf->device = head;
1da177e4
LT
196 }
197
ba76ae38
BZ
198 if (rq_data_dir(rq))
199 task.tf_flags |= IDE_TFLAG_WRITE;
200
201 ide_tf_set_cmd(drive, &task, dma);
f6e29e35
BZ
202 if (!dma)
203 hwif->data_phase = task.data_phase;
204 task.rq = rq;
ba76ae38 205
f6e29e35 206 rc = do_rw_taskfile(drive, &task);
2bd06b23 207
f6e29e35 208 if (rc == ide_stopped && dma) {
1da177e4 209 /* fallback to PIO */
f6e29e35 210 task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
ba76ae38 211 ide_tf_set_cmd(drive, &task, 0);
f6e29e35 212 hwif->data_phase = task.data_phase;
1da177e4 213 ide_init_sg_cmd(drive, rq);
f6e29e35 214 rc = do_rw_taskfile(drive, &task);
1da177e4
LT
215 }
216
f6e29e35 217 return rc;
1da177e4
LT
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
98416549
BZ
226static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
227 sector_t block)
1da177e4
LT
228{
229 ide_hwif_t *hwif = HWIF(drive);
230
97100fc8 231 BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
1da177e4
LT
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
2bfb646c
RP
239 ledtrig_ide_activity();
240
1da177e4
LT
241 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
242 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
c2f8311d
MR
243 (unsigned long long)block, rq->nr_sectors,
244 (unsigned long)rq->buffer);
1da177e4
LT
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 */
7a3b7512 256static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
1da177e4
LT
257{
258 ide_task_t args;
650d841d 259 struct ide_taskfile *tf = &args.tf;
7a3b7512 260 u64 addr = 0;
1da177e4
LT
261
262 /* Create IDE/ATA command request structure */
263 memset(&args, 0, sizeof(ide_task_t));
7a3b7512 264 if (lba48)
aaaade3f 265 tf->command = ATA_CMD_READ_NATIVE_MAX_EXT;
7a3b7512 266 else
aaaade3f 267 tf->command = ATA_CMD_READ_NATIVE_MAX;
650d841d 268 tf->device = ATA_LBA;
657cc1a8 269 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
a3bbb9d8 270 if (lba48)
657cc1a8 271 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
1da177e4 272 /* submit command request */
9a3c49be 273 ide_no_data_taskfile(drive, &args);
1da177e4 274
1da177e4 275 /* if OK, compute maximum address value */
a501633c
BZ
276 if ((tf->status & 0x01) == 0)
277 addr = ide_get_lba_addr(tf, lba48) + 1;
650d841d 278
1da177e4
LT
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 */
7a3b7512 286static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
1da177e4
LT
287{
288 ide_task_t args;
650d841d 289 struct ide_taskfile *tf = &args.tf;
7a3b7512 290 u64 addr_set = 0;
1da177e4
LT
291
292 addr_req--;
293 /* Create IDE/ATA command request structure */
294 memset(&args, 0, sizeof(ide_task_t));
650d841d
BZ
295 tf->lbal = (addr_req >> 0) & 0xff;
296 tf->lbam = (addr_req >>= 8) & 0xff;
297 tf->lbah = (addr_req >>= 8) & 0xff;
7a3b7512
BZ
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;
aaaade3f 302 tf->command = ATA_CMD_SET_MAX_EXT;
7a3b7512
BZ
303 } else {
304 tf->device = (addr_req >>= 8) & 0x0f;
aaaade3f 305 tf->command = ATA_CMD_SET_MAX;
7a3b7512
BZ
306 }
307 tf->device |= ATA_LBA;
657cc1a8 308 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
a3bbb9d8 309 if (lba48)
657cc1a8 310 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
1da177e4 311 /* submit command request */
9a3c49be 312 ide_no_data_taskfile(drive, &args);
1da177e4 313 /* if OK, compute maximum address value */
a501633c
BZ
314 if ((tf->status & 0x01) == 0)
315 addr_set = ide_get_lba_addr(tf, lba48) + 1;
650d841d 316
1da177e4
LT
317 return addr_set;
318}
319
320static 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
b0244a00
BZ
327/*
328 * Some disks report total number of sectors instead of
329 * maximum sector address. We list them here.
330 */
331static const struct drive_list_entry hpa_list[] = {
332 { "ST340823A", NULL },
7062cdc5 333 { "ST320413A", NULL },
b152fcd3 334 { "ST310211A", NULL },
b0244a00
BZ
335 { NULL, NULL }
336};
337
858119e1 338static void idedisk_check_hpa(ide_drive_t *drive)
1da177e4
LT
339{
340 unsigned long long capacity, set_max;
942dcd85 341 int lba48 = ata_id_lba48_enabled(drive->id);
1da177e4
LT
342
343 capacity = drive->capacity64;
7a3b7512
BZ
344
345 set_max = idedisk_read_native_max_address(drive, lba48);
1da177e4 346
b0244a00
BZ
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
1da177e4
LT
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
7a3b7512
BZ
366 set_max = idedisk_set_max_address(drive, set_max, lba48);
367
1da177e4
LT
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
98416549 375static void init_idedisk_capacity(ide_drive_t *drive)
1da177e4 376{
4dde4492 377 u16 *id = drive->id;
d1d76714 378 int lba;
1da177e4 379
942dcd85 380 if (ata_id_lba48_enabled(id)) {
1da177e4 381 /* drive speaks 48-bit LBA */
d1d76714 382 lba = 1;
48fb2688 383 drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
a02227c9 384 } else if (ata_id_has_lba(id) && ata_id_is_lba_capacity_ok(id)) {
1da177e4 385 /* drive speaks 28-bit LBA */
d1d76714 386 lba = 1;
48fb2688 387 drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1da177e4
LT
388 } else {
389 /* drive speaks boring old 28-bit CHS */
d1d76714 390 lba = 0;
1da177e4
LT
391 drive->capacity64 = drive->cyl * drive->head * drive->sect;
392 }
d1d76714
BZ
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 }
0a70c7f6
BZ
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 }
1da177e4
LT
424}
425
06b89518 426sector_t ide_disk_capacity(ide_drive_t *drive)
1da177e4 427{
3c619ffd 428 return drive->capacity64;
1da177e4
LT
429}
430
165125e1 431static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
1da177e4
LT
432{
433 ide_drive_t *drive = q->queuedata;
395d8ef5 434 ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC);
1da177e4 435
395d8ef5
BZ
436 /* FIXME: map struct ide_taskfile on rq->cmd[] */
437 BUG_ON(task == NULL);
438
439 memset(task, 0, sizeof(*task));
ff2779b5 440 if (ata_id_flush_ext_enabled(drive->id) &&
1da177e4 441 (drive->capacity64 >= (1UL << 28)))
aaaade3f 442 task->tf.command = ATA_CMD_FLUSH_EXT;
1da177e4 443 else
aaaade3f 444 task->tf.command = ATA_CMD_FLUSH;
395d8ef5
BZ
445 task->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
446 IDE_TFLAG_DYN;
447 task->data_phase = TASKFILE_NO_DATA;
1da177e4 448
813a0eb2 449 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
4aff5e23 450 rq->cmd_flags |= REQ_SOFTBARRIER;
395d8ef5 451 rq->special = task;
1da177e4
LT
452}
453
8185d5aa
BZ
454ide_devset_get(multcount, mult_count);
455
1da177e4
LT
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 */
460static int set_multcount(ide_drive_t *drive, int arg)
461{
dd47087b
FT
462 struct request *rq;
463 int error;
1da177e4 464
48fb2688 465 if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
1497943e
BZ
466 return -EINVAL;
467
1da177e4
LT
468 if (drive->special.b.set_multmode)
469 return -EBUSY;
852738f3 470
dd47087b
FT
471 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
472 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
852738f3 473
1da177e4
LT
474 drive->mult_req = arg;
475 drive->special.b.set_multmode = 1;
dd47087b
FT
476 error = blk_execute_rq(drive->queue, NULL, rq, 0);
477 blk_put_request(rq);
98416549 478
1da177e4
LT
479 return (drive->mult_count == arg) ? 0 : -EIO;
480}
481
97100fc8 482ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR);
8185d5aa 483
1da177e4
LT
484static int set_nowerr(ide_drive_t *drive, int arg)
485{
1497943e
BZ
486 if (arg < 0 || arg > 1)
487 return -EINVAL;
488
97100fc8
BZ
489 if (arg)
490 drive->dev_flags |= IDE_DFLAG_NOWERR;
491 else
492 drive->dev_flags &= ~IDE_DFLAG_NOWERR;
493
1da177e4 494 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
97100fc8 495
1da177e4
LT
496 return 0;
497}
498
be3c096e
BZ
499static 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
3e087b57
TH
512static void update_ordered(ide_drive_t *drive)
513{
4dde4492 514 u16 *id = drive->id;
3e087b57
TH
515 unsigned ordered = QUEUE_ORDERED_NONE;
516 prepare_flush_fn *prep_fn = NULL;
3e087b57 517
97100fc8 518 if (drive->dev_flags & IDE_DFLAG_WCACHE) {
3e087b57
TH
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 */
06b89518 529 capacity = ide_disk_capacity(drive);
97100fc8
BZ
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) ||
ff2779b5 534 ata_id_flush_ext_enabled(id));
3e087b57
TH
535
536 printk(KERN_INFO "%s: cache flushes %ssupported\n",
f7ad836c 537 drive->name, barrier ? "" : "not ");
3e087b57
TH
538
539 if (barrier) {
540 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
541 prep_fn = idedisk_prepare_flush;
3e087b57
TH
542 }
543 } else
544 ordered = QUEUE_ORDERED_DRAIN;
545
546 blk_queue_ordered(drive->queue, ordered, prep_fn);
3e087b57
TH
547}
548
97100fc8 549ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
8185d5aa
BZ
550
551static int set_wcache(ide_drive_t *drive, int arg)
1da177e4 552{
3e087b57 553 int err = 1;
1da177e4 554
1497943e
BZ
555 if (arg < 0 || arg > 1)
556 return -EINVAL;
557
4b58f17d 558 if (ata_id_flush_enabled(drive->id)) {
be3c096e
BZ
559 err = ide_do_setfeature(drive,
560 arg ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF, 0);
97100fc8
BZ
561 if (err == 0) {
562 if (arg)
563 drive->dev_flags |= IDE_DFLAG_WCACHE;
564 else
565 drive->dev_flags &= ~IDE_DFLAG_WCACHE;
566 }
3e087b57 567 }
1da177e4 568
3e087b57 569 update_ordered(drive);
1da177e4 570
3e087b57 571 return err;
1da177e4
LT
572}
573
98416549 574static int do_idedisk_flushcache(ide_drive_t *drive)
1da177e4
LT
575{
576 ide_task_t args;
577
578 memset(&args, 0, sizeof(ide_task_t));
ff2779b5 579 if (ata_id_flush_ext_enabled(drive->id))
aaaade3f 580 args.tf.command = ATA_CMD_FLUSH_EXT;
1da177e4 581 else
aaaade3f 582 args.tf.command = ATA_CMD_FLUSH;
657cc1a8 583 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
9a3c49be 584 return ide_no_data_taskfile(drive, &args);
1da177e4
LT
585}
586
8185d5aa
BZ
587ide_devset_get(acoustic, acoustic);
588
98416549 589static int set_acoustic(ide_drive_t *drive, int arg)
1da177e4 590{
1497943e
BZ
591 if (arg < 0 || arg > 254)
592 return -EINVAL;
593
be3c096e
BZ
594 ide_do_setfeature(drive,
595 arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg);
596
1da177e4 597 drive->acoustic = arg;
be3c096e 598
1da177e4
LT
599 return 0;
600}
601
97100fc8 602ide_devset_get_flag(addressing, IDE_DFLAG_LBA48);
8185d5aa 603
1da177e4
LT
604/*
605 * drive->addressing:
606 * 0: 28-bit
607 * 1: 48-bit
608 * 2: 48-bit capable doing 28-bit
609 */
aa768773 610static int set_addressing(ide_drive_t *drive, int arg)
1da177e4 611{
1497943e
BZ
612 if (arg < 0 || arg > 2)
613 return -EINVAL;
614
35c13753
BZ
615 if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
616 ata_id_lba48_enabled(drive->id) == 0))
98416549 617 return -EIO;
942dcd85 618
35c13753
BZ
619 if (arg == 2)
620 arg = 0;
621
97100fc8
BZ
622 if (arg)
623 drive->dev_flags |= IDE_DFLAG_LBA48;
624 else
625 drive->dev_flags &= ~IDE_DFLAG_LBA48;
942dcd85 626
1da177e4
LT
627 return 0;
628}
629
f8790489
BZ
630ide_ext_devset_rw(acoustic, acoustic);
631ide_ext_devset_rw(address, addressing);
632ide_ext_devset_rw(multcount, multcount);
633ide_ext_devset_rw(wcache, wcache);
92f1f8fd 634
f8790489 635ide_ext_devset_rw_sync(nowerr, nowerr);
92f1f8fd 636
98416549 637static void idedisk_setup(ide_drive_t *drive)
1da177e4 638{
1e874f44 639 struct ide_disk_obj *idkp = drive->driver_data;
238e4f14 640 ide_hwif_t *hwif = drive->hwif;
4dde4492
BZ
641 u16 *id = drive->id;
642 char *m = (char *)&id[ATA_ID_PROD];
1da177e4 643 unsigned long long capacity;
1da177e4 644
1e874f44 645 ide_proc_register_driver(drive, idkp->driver);
1da177e4 646
97100fc8 647 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0)
1da177e4
LT
648 return;
649
97100fc8 650 if (drive->dev_flags & IDE_DFLAG_REMOVABLE) {
1da177e4 651 /*
98416549 652 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1da177e4 653 */
4dde4492 654 if (m[0] != 'W' || m[1] != 'D')
97100fc8 655 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
1da177e4
LT
656 }
657
aa768773 658 (void)set_addressing(drive, 1);
1da177e4 659
97100fc8 660 if (drive->dev_flags & IDE_DFLAG_LBA48) {
1da177e4
LT
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
98416549
BZ
669 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
670 drive->queue->max_sectors / 2);
1da177e4
LT
671
672 /* calculate drive capacity, and select LBA if possible */
98416549 673 init_idedisk_capacity(drive);
1da177e4 674
1da177e4
LT
675 /*
676 * if possible, give fdisk access to more of the drive,
677 * by correcting bios_cyls:
678 */
06b89518 679 capacity = ide_disk_capacity(drive);
98416549 680
97100fc8 681 if ((drive->dev_flags & IDE_DFLAG_FORCED_GEOM) == 0) {
942dcd85 682 if (ata_id_lba48_enabled(drive->id)) {
1da177e4
LT
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 */
4dde4492
BZ
708 if (id[ATA_ID_BUF_SIZE])
709 printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2);
1da177e4 710
3ab7efe8
BZ
711 printk(KERN_CONT ", CHS=%d/%d/%d\n",
712 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1da177e4 713
1da177e4 714 /* write cache enabled? */
8a089c66 715 if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
97100fc8 716 drive->dev_flags |= IDE_DFLAG_WCACHE;
1da177e4 717
8185d5aa 718 set_wcache(drive, 1);
ae9f9f07
BZ
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;
1da177e4
LT
727}
728
729static void ide_cacheflush_p(ide_drive_t *drive)
730{
97100fc8
BZ
731 if (ata_id_flush_enabled(drive->id) == 0 ||
732 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0)
1da177e4
LT
733 return;
734
735 if (do_idedisk_flushcache(drive))
736 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
737}
738
4031bbe4 739static void ide_disk_remove(ide_drive_t *drive)
1da177e4
LT
740{
741 struct ide_disk_obj *idkp = drive->driver_data;
742 struct gendisk *g = idkp->disk;
743
7662d046 744 ide_proc_unregister_driver(drive, idkp->driver);
8604affd 745
1da177e4
LT
746 del_gendisk(g);
747
d36fef6f
BZ
748 ide_cacheflush_p(drive);
749
1da177e4 750 ide_disk_put(idkp);
1da177e4
LT
751}
752
753static void ide_disk_release(struct kref *kref)
754{
d7e74759 755 struct ide_disk_obj *idkp = to_ide_drv(kref, ide_disk_obj);
1da177e4
LT
756 ide_drive_t *drive = idkp->drive;
757 struct gendisk *g = idkp->disk;
758
759 drive->driver_data = NULL;
1da177e4
LT
760 g->private_data = NULL;
761 put_disk(g);
762 kfree(idkp);
763}
764
4031bbe4 765static int ide_disk_probe(ide_drive_t *drive);
1da177e4 766
0d2157f7
LT
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 */
772static void ide_disk_resume(ide_drive_t *drive)
773{
f41891c1 774 if (ata_id_hpa_enabled(drive->id))
0d2157f7
LT
775 init_idedisk_capacity(drive);
776}
777
4031bbe4 778static void ide_device_shutdown(ide_drive_t *drive)
1da177e4 779{
1da177e4
LT
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
d12faa27
BZ
800 printk(KERN_INFO "Shutdown: %s\n", drive->name);
801
4031bbe4 802 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
1da177e4
LT
803}
804
1da177e4 805static ide_driver_t idedisk_driver = {
1da177e4 806 .gen_driver = {
4ef3b8f4 807 .owner = THIS_MODULE,
8604affd
BZ
808 .name = "ide-disk",
809 .bus = &ide_bus_type,
1da177e4 810 },
4031bbe4
RK
811 .probe = ide_disk_probe,
812 .remove = ide_disk_remove,
0d2157f7 813 .resume = ide_disk_resume,
4031bbe4 814 .shutdown = ide_device_shutdown,
1da177e4 815 .version = IDEDISK_VERSION,
1da177e4
LT
816 .do_request = ide_do_rw_disk,
817 .end_request = ide_end_request,
818 .error = __ide_error,
7662d046 819#ifdef CONFIG_IDE_PROC_FS
06b89518
BZ
820 .proc = ide_disk_proc,
821 .settings = ide_disk_settings,
7662d046 822#endif
1da177e4
LT
823};
824
29ec683f
BZ
825static int idedisk_set_doorlock(ide_drive_t *drive, int on)
826{
827 ide_task_t task;
81ee1bb5
BZ
828 int ret;
829
830 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
831 return 0;
29ec683f
BZ
832
833 memset(&task, 0, sizeof(task));
aaaade3f 834 task.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
657cc1a8 835 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
29ec683f 836
81ee1bb5
BZ
837 ret = ide_no_data_taskfile(drive, &task);
838
839 if (ret)
840 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
841
842 return ret;
29ec683f
BZ
843}
844
1da177e4
LT
845static 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
98416549
BZ
851 idkp = ide_disk_get(disk);
852 if (idkp == NULL)
1da177e4
LT
853 return -ENXIO;
854
855 drive = idkp->drive;
856
c94964a4
BZ
857 idkp->openers++;
858
97100fc8 859 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
1da177e4
LT
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 */
81ee1bb5 865 idedisk_set_doorlock(drive, 1);
099ed4c2 866 check_disk_change(inode->i_bdev);
1da177e4
LT
867 }
868 return 0;
869}
870
871static int idedisk_release(struct inode *inode, struct file *filp)
872{
873 struct gendisk *disk = inode->i_bdev->bd_disk;
d7e74759 874 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
1da177e4
LT
875 ide_drive_t *drive = idkp->drive;
876
c94964a4 877 if (idkp->openers == 1)
1da177e4 878 ide_cacheflush_p(drive);
c94964a4 879
81ee1bb5
BZ
880 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1)
881 idedisk_set_doorlock(drive, 0);
c94964a4
BZ
882
883 idkp->openers--;
1da177e4
LT
884
885 ide_disk_put(idkp);
886
887 return 0;
888}
889
a885c8c4
CH
890static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
891{
d7e74759 892 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
a885c8c4
CH
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
1da177e4
LT
901static int idedisk_media_changed(struct gendisk *disk)
902{
d7e74759 903 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
1da177e4
LT
904 ide_drive_t *drive = idkp->drive;
905
906 /* do not scan partitions twice if this is a removable device */
97100fc8
BZ
907 if (drive->dev_flags & IDE_DFLAG_ATTACH) {
908 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
1da177e4
LT
909 return 0;
910 }
97100fc8 911
1da177e4 912 /* if removable, always assume it was changed */
97100fc8 913 return !!(drive->dev_flags & IDE_DFLAG_REMOVABLE);
1da177e4
LT
914}
915
916static int idedisk_revalidate_disk(struct gendisk *disk)
917{
d7e74759 918 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
06b89518 919 set_capacity(disk, ide_disk_capacity(idkp->drive));
1da177e4
LT
920 return 0;
921}
922
923static struct block_device_operations idedisk_ops = {
98416549
BZ
924 .owner = THIS_MODULE,
925 .open = idedisk_open,
926 .release = idedisk_release,
f8790489 927 .ioctl = ide_disk_ioctl,
98416549
BZ
928 .getgeo = idedisk_getgeo,
929 .media_changed = idedisk_media_changed,
930 .revalidate_disk = idedisk_revalidate_disk
1da177e4
LT
931};
932
933MODULE_DESCRIPTION("ATA DISK Driver");
934
4031bbe4 935static int ide_disk_probe(ide_drive_t *drive)
1da177e4
LT
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;
2a924662 943
1da177e4
LT
944 if (drive->media != ide_disk)
945 goto failed;
946
f5e3c2fa 947 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
1da177e4
LT
948 if (!idkp)
949 goto failed;
950
689d6fac 951 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
1da177e4
LT
952 if (!g)
953 goto out_free_idkp;
954
955 ide_init_disk(g, drive);
956
1da177e4
LT
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
1da177e4 967 idedisk_setup(drive);
ae9f9f07
BZ
968
969 set_capacity(g, ide_disk_capacity(drive));
8604affd 970
f615b48c 971 g->minors = IDE_DISK_MINORS;
1da177e4 972 g->driverfs_dev = &drive->gendev;
689d6fac 973 g->flags |= GENHD_FL_EXT_DEVT;
97100fc8
BZ
974 if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
975 g->flags = GENHD_FL_REMOVABLE;
1da177e4
LT
976 g->fops = &idedisk_ops;
977 add_disk(g);
978 return 0;
979
1da177e4
LT
980out_free_idkp:
981 kfree(idkp);
982failed:
8604affd 983 return -ENODEV;
1da177e4
LT
984}
985
98416549 986static void __exit idedisk_exit(void)
1da177e4 987{
8604affd 988 driver_unregister(&idedisk_driver.gen_driver);
1da177e4
LT
989}
990
17514e8a 991static int __init idedisk_init(void)
1da177e4 992{
8604affd 993 return driver_register(&idedisk_driver.gen_driver);
1da177e4
LT
994}
995
263756ec 996MODULE_ALIAS("ide:*m-disk*");
f8790489 997MODULE_ALIAS("ide-disk");
1da177e4
LT
998module_init(idedisk_init);
999module_exit(idedisk_exit);
1000MODULE_LICENSE("GPL");
This page took 0.418872 seconds and 5 git commands to generate.