blob: fb162cb3ebf5f7faeed702a537babc93e1064fe8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/ide-disk.c Version 1.18 Mar 05, 2003
3 *
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 * Copyright (C) 1998-2002 Linux ATA Development
6 * Andre Hedrick <andre@linux-ide.org>
7 * Copyright (C) 2003 Red Hat <alan@redhat.com>
8 */
9
10/*
11 * Mostly written by Mark Lord <mlord@pobox.com>
12 * and Gadi Oxman <gadio@netvision.net.il>
13 * and Andre Hedrick <andre@linux-ide.org>
14 *
15 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
16 *
17 * Version 1.00 move disk only code from ide.c to ide-disk.c
18 * support optional byte-swapping of all data
19 * Version 1.01 fix previous byte-swapping code
20 * Version 1.02 remove ", LBA" from drive identification msgs
21 * Version 1.03 fix display of id->buf_size for big-endian
22 * Version 1.04 add /proc configurable settings and S.M.A.R.T support
23 * Version 1.05 add capacity support for ATA3 >= 8GB
24 * Version 1.06 get boot-up messages to show full cyl count
25 * Version 1.07 disable door-locking if it fails
26 * Version 1.08 fixed CHS/LBA translations for ATA4 > 8GB,
27 * process of adding new ATA4 compliance.
28 * fixed problems in allowing fdisk to see
29 * the entire disk.
30 * Version 1.09 added increment of rq->sector in ide_multwrite
31 * added UDMA 3/4 reporting
32 * Version 1.10 request queue changes, Ultra DMA 100
33 * Version 1.11 added 48-bit lba
34 * Version 1.12 adding taskfile io access method
35 * Version 1.13 added standby and flush-cache for notifier
36 * Version 1.14 added acoustic-wcache
37 * Version 1.15 convert all calls to ide_raw_taskfile
38 * since args will return register content.
39 * Version 1.16 added suspend-resume-checkpower
Andreas Mohrd6e05ed2006-06-26 18:35:02 +020040 * Version 1.17 do flush on standby, do flush on ATA < ATA6
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * fix wcache setup.
42 */
43
44#define IDEDISK_VERSION "1.18"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046//#define DEBUG
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/module.h>
49#include <linux/types.h>
50#include <linux/string.h>
51#include <linux/kernel.h>
52#include <linux/timer.h>
53#include <linux/mm.h>
54#include <linux/interrupt.h>
55#include <linux/major.h>
56#include <linux/errno.h>
57#include <linux/genhd.h>
58#include <linux/slab.h>
59#include <linux/delay.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080060#include <linux/mutex.h>
Richard Purdie2bfb6462006-03-31 02:31:16 -080061#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define _IDE_DISK
64
65#include <linux/ide.h>
66
67#include <asm/byteorder.h>
68#include <asm/irq.h>
69#include <asm/uaccess.h>
70#include <asm/io.h>
71#include <asm/div64.h>
72
73struct ide_disk_obj {
74 ide_drive_t *drive;
75 ide_driver_t *driver;
76 struct gendisk *disk;
77 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +010078 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Arjan van de Vencf8b8972006-03-23 03:00:45 -080081static DEFINE_MUTEX(idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83#define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref)
84
85#define ide_disk_g(disk) \
86 container_of((disk)->private_data, struct ide_disk_obj, driver)
87
88static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
89{
90 struct ide_disk_obj *idkp = NULL;
91
Arjan van de Vencf8b8972006-03-23 03:00:45 -080092 mutex_lock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 idkp = ide_disk_g(disk);
94 if (idkp)
95 kref_get(&idkp->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080096 mutex_unlock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return idkp;
98}
99
100static void ide_disk_release(struct kref *);
101
102static void ide_disk_put(struct ide_disk_obj *idkp)
103{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800104 mutex_lock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 kref_put(&idkp->kref, ide_disk_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800106 mutex_unlock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
110 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
111 * value for this drive (from its reported identification information).
112 *
113 * Returns: 1 if lba_capacity looks sensible
114 * 0 otherwise
115 *
116 * It is called only once for each drive.
117 */
118static int lba_capacity_is_ok (struct hd_driveid *id)
119{
120 unsigned long lba_sects, chs_sects, head, tail;
121
Alan Cox6efd9362005-06-27 15:24:22 -0700122 /* No non-LBA info .. so valid! */
123 if (id->cyls == 0)
124 return 1;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * The ATA spec tells large drives to return
128 * C/H/S = 16383/16/63 independent of their size.
129 * Some drives can be jumpered to use 15 heads instead of 16.
130 * Some drives can be jumpered to use 4092 cyls instead of 16383.
131 */
132 if ((id->cyls == 16383
133 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
134 id->sectors == 63 &&
135 (id->heads == 15 || id->heads == 16) &&
136 (id->lba_capacity >= 16383*63*id->heads))
137 return 1;
138
139 lba_sects = id->lba_capacity;
140 chs_sects = id->cyls * id->heads * id->sectors;
141
142 /* perform a rough sanity check on lba_sects: within 10% is OK */
143 if ((lba_sects - chs_sects) < chs_sects/10)
144 return 1;
145
146 /* some drives have the word order reversed */
147 head = ((lba_sects >> 16) & 0xffff);
148 tail = (lba_sects & 0xffff);
149 lba_sects = (head | (tail << 16));
150 if ((lba_sects - chs_sects) < chs_sects/10) {
151 id->lba_capacity = lba_sects;
152 return 1; /* lba_capacity is (now) good */
153 }
154
155 return 0; /* lba_capacity value may be bad */
156}
157
158/*
159 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
160 * using LBA if supported, or CHS otherwise, to address sectors.
161 */
162static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
163{
164 ide_hwif_t *hwif = HWIF(drive);
165 unsigned int dma = drive->using_dma;
166 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
167 task_ioreg_t command = WIN_NOP;
168 ata_nsector_t nsectors;
169
170 nsectors.all = (u16) rq->nr_sectors;
171
172 if (hwif->no_lba48_dma && lba48 && dma) {
173 if (block + rq->nr_sectors > 1ULL << 28)
174 dma = 0;
175 else
176 lba48 = 0;
177 }
178
179 if (!dma) {
180 ide_init_sg_cmd(drive, rq);
181 ide_map_sg(drive, rq);
182 }
183
184 if (IDE_CONTROL_REG)
185 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
186
187 /* FIXME: SELECT_MASK(drive, 0) ? */
188
189 if (drive->select.b.lba) {
190 if (lba48) {
191 task_ioreg_t tasklets[10];
192
Michael Richardsonc2f83112006-02-07 12:58:33 -0800193 pr_debug("%s: LBA=0x%012llx\n", drive->name,
194 (unsigned long long)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 tasklets[0] = 0;
197 tasklets[1] = 0;
198 tasklets[2] = nsectors.b.low;
199 tasklets[3] = nsectors.b.high;
200 tasklets[4] = (task_ioreg_t) block;
201 tasklets[5] = (task_ioreg_t) (block>>8);
202 tasklets[6] = (task_ioreg_t) (block>>16);
203 tasklets[7] = (task_ioreg_t) (block>>24);
204 if (sizeof(block) == 4) {
205 tasklets[8] = (task_ioreg_t) 0;
206 tasklets[9] = (task_ioreg_t) 0;
207 } else {
208 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
209 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
210 }
211#ifdef DEBUG
212 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
213 drive->name, tasklets[3], tasklets[2],
214 tasklets[9], tasklets[8], tasklets[7],
215 tasklets[6], tasklets[5], tasklets[4]);
216#endif
217 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
218 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
219 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
220 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
221 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
222
223 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
224 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
225 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
226 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
227 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
228 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
229 } else {
230 hwif->OUTB(0x00, IDE_FEATURE_REG);
231 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
232 hwif->OUTB(block, IDE_SECTOR_REG);
233 hwif->OUTB(block>>=8, IDE_LCYL_REG);
234 hwif->OUTB(block>>=8, IDE_HCYL_REG);
235 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
236 }
237 } else {
238 unsigned int sect,head,cyl,track;
239 track = (int)block / drive->sect;
240 sect = (int)block % drive->sect + 1;
241 hwif->OUTB(sect, IDE_SECTOR_REG);
242 head = track % drive->head;
243 cyl = track / drive->head;
244
245 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
246
247 hwif->OUTB(0x00, IDE_FEATURE_REG);
248 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
249 hwif->OUTB(cyl, IDE_LCYL_REG);
250 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
251 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
252 }
253
254 if (dma) {
255 if (!hwif->dma_setup(drive)) {
256 if (rq_data_dir(rq)) {
257 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
258 if (drive->vdma)
259 command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
260 } else {
261 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
262 if (drive->vdma)
263 command = lba48 ? WIN_READ_EXT: WIN_READ;
264 }
265 hwif->dma_exec_cmd(drive, command);
266 hwif->dma_start(drive);
267 return ide_started;
268 }
269 /* fallback to PIO */
270 ide_init_sg_cmd(drive, rq);
271 }
272
273 if (rq_data_dir(rq) == READ) {
274
275 if (drive->mult_count) {
276 hwif->data_phase = TASKFILE_MULTI_IN;
277 command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
278 } else {
279 hwif->data_phase = TASKFILE_IN;
280 command = lba48 ? WIN_READ_EXT : WIN_READ;
281 }
282
283 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
284 return ide_started;
285 } else {
286 if (drive->mult_count) {
287 hwif->data_phase = TASKFILE_MULTI_OUT;
288 command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
289 } else {
290 hwif->data_phase = TASKFILE_OUT;
291 command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
292 }
293
294 /* FIXME: ->OUTBSYNC ? */
295 hwif->OUTB(command, IDE_COMMAND_REG);
296
297 return pre_task_out_intr(drive, rq);
298 }
299}
300
301/*
302 * 268435455 == 137439 MB or 28bit limit
303 * 320173056 == 163929 MB or 48bit addressing
304 * 1073741822 == 549756 MB or 48bit addressing fake drive
305 */
306
307static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
308{
309 ide_hwif_t *hwif = HWIF(drive);
310
311 BUG_ON(drive->blocked);
312
313 if (!blk_fs_request(rq)) {
314 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
315 ide_end_request(drive, 0, 0);
316 return ide_stopped;
317 }
318
Richard Purdie2bfb6462006-03-31 02:31:16 -0800319 ledtrig_ide_activity();
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
322 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
Michael Richardsonc2f83112006-02-07 12:58:33 -0800323 (unsigned long long)block, rq->nr_sectors,
324 (unsigned long)rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (hwif->rw_disk)
327 hwif->rw_disk(drive, rq);
328
329 return __ide_do_rw_disk(drive, rq, block);
330}
331
332/*
333 * Queries for true maximum capacity of the drive.
334 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
335 */
336static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
337{
338 ide_task_t args;
339 unsigned long addr = 0;
340
341 /* Create IDE/ATA command request structure */
342 memset(&args, 0, sizeof(ide_task_t));
343 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
344 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
345 args.command_type = IDE_DRIVE_TASK_NO_DATA;
346 args.handler = &task_no_data_intr;
347 /* submit command request */
348 ide_raw_taskfile(drive, &args, NULL);
349
350 /* if OK, compute maximum address value */
351 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
352 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
353 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
354 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
355 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
356 addr++; /* since the return value is (maxlba - 1), we add 1 */
357 }
358 return addr;
359}
360
361static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
362{
363 ide_task_t args;
364 unsigned long long addr = 0;
365
366 /* Create IDE/ATA command request structure */
367 memset(&args, 0, sizeof(ide_task_t));
368
369 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
370 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
371 args.command_type = IDE_DRIVE_TASK_NO_DATA;
372 args.handler = &task_no_data_intr;
373 /* submit command request */
374 ide_raw_taskfile(drive, &args, NULL);
375
376 /* if OK, compute maximum address value */
377 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
378 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
379 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
380 args.hobRegister[IDE_SECTOR_OFFSET];
381 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
382 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
383 (args.tfRegister[IDE_SECTOR_OFFSET]);
384 addr = ((__u64)high << 24) | low;
385 addr++; /* since the return value is (maxlba - 1), we add 1 */
386 }
387 return addr;
388}
389
390/*
391 * Sets maximum virtual LBA address of the drive.
392 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
393 */
394static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
395{
396 ide_task_t args;
397 unsigned long addr_set = 0;
398
399 addr_req--;
400 /* Create IDE/ATA command request structure */
401 memset(&args, 0, sizeof(ide_task_t));
402 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
403 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
404 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
405 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
406 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
407 args.command_type = IDE_DRIVE_TASK_NO_DATA;
408 args.handler = &task_no_data_intr;
409 /* submit command request */
410 ide_raw_taskfile(drive, &args, NULL);
411 /* if OK, read new maximum address value */
412 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
413 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
414 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
415 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
416 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
417 addr_set++;
418 }
419 return addr_set;
420}
421
422static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
423{
424 ide_task_t args;
425 unsigned long long addr_set = 0;
426
427 addr_req--;
428 /* Create IDE/ATA command request structure */
429 memset(&args, 0, sizeof(ide_task_t));
430 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
431 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
432 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
433 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
434 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
435 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
436 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
437 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
438 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
439 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
440 args.command_type = IDE_DRIVE_TASK_NO_DATA;
441 args.handler = &task_no_data_intr;
442 /* submit command request */
443 ide_raw_taskfile(drive, &args, NULL);
444 /* if OK, compute maximum address value */
445 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
446 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
447 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
448 args.hobRegister[IDE_SECTOR_OFFSET];
449 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
450 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
451 (args.tfRegister[IDE_SECTOR_OFFSET]);
452 addr_set = ((__u64)high << 24) | low;
453 addr_set++;
454 }
455 return addr_set;
456}
457
458static unsigned long long sectors_to_MB(unsigned long long n)
459{
460 n <<= 9; /* make it bytes */
461 do_div(n, 1000000); /* make it MB */
462 return n;
463}
464
465/*
466 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
467 * so on non-buggy drives we need test only one.
468 * However, we should also check whether these fields are valid.
469 */
470static inline int idedisk_supports_hpa(const struct hd_driveid *id)
471{
472 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
473}
474
475/*
476 * The same here.
477 */
478static inline int idedisk_supports_lba48(const struct hd_driveid *id)
479{
480 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
481 && id->lba_capacity_2;
482}
483
Arjan van de Ven858119e2006-01-14 13:20:43 -0800484static void idedisk_check_hpa(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 unsigned long long capacity, set_max;
487 int lba48 = idedisk_supports_lba48(drive->id);
488
489 capacity = drive->capacity64;
490 if (lba48)
491 set_max = idedisk_read_native_max_address_ext(drive);
492 else
493 set_max = idedisk_read_native_max_address(drive);
494
495 if (set_max <= capacity)
496 return;
497
498 printk(KERN_INFO "%s: Host Protected Area detected.\n"
499 "\tcurrent capacity is %llu sectors (%llu MB)\n"
500 "\tnative capacity is %llu sectors (%llu MB)\n",
501 drive->name,
502 capacity, sectors_to_MB(capacity),
503 set_max, sectors_to_MB(set_max));
504
505 if (lba48)
506 set_max = idedisk_set_max_address_ext(drive, set_max);
507 else
508 set_max = idedisk_set_max_address(drive, set_max);
509 if (set_max) {
510 drive->capacity64 = set_max;
511 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
512 drive->name);
513 }
514}
515
516/*
517 * Compute drive->capacity, the full capacity of the drive
518 * Called with drive->id != NULL.
519 *
520 * To compute capacity, this uses either of
521 *
522 * 1. CHS value set by user (whatever user sets will be trusted)
523 * 2. LBA value from target drive (require new ATA feature)
524 * 3. LBA value from system BIOS (new one is OK, old one may break)
525 * 4. CHS value from system BIOS (traditional style)
526 *
527 * in above order (i.e., if value of higher priority is available,
528 * reset will be ignored).
529 */
530static void init_idedisk_capacity (ide_drive_t *drive)
531{
532 struct hd_driveid *id = drive->id;
533 /*
534 * If this drive supports the Host Protected Area feature set,
535 * then we may need to change our opinion about the drive's capacity.
536 */
537 int hpa = idedisk_supports_hpa(id);
538
539 if (idedisk_supports_lba48(id)) {
540 /* drive speaks 48-bit LBA */
541 drive->select.b.lba = 1;
542 drive->capacity64 = id->lba_capacity_2;
543 if (hpa)
544 idedisk_check_hpa(drive);
545 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
546 /* drive speaks 28-bit LBA */
547 drive->select.b.lba = 1;
548 drive->capacity64 = id->lba_capacity;
549 if (hpa)
550 idedisk_check_hpa(drive);
551 } else {
552 /* drive speaks boring old 28-bit CHS */
553 drive->capacity64 = drive->cyl * drive->head * drive->sect;
554 }
555}
556
557static sector_t idedisk_capacity (ide_drive_t *drive)
558{
559 return drive->capacity64 - drive->sect0;
560}
561
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +0200562#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564static int smart_enable(ide_drive_t *drive)
565{
566 ide_task_t args;
567
568 memset(&args, 0, sizeof(ide_task_t));
569 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
570 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
571 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
572 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
573 args.command_type = IDE_DRIVE_TASK_NO_DATA;
574 args.handler = &task_no_data_intr;
575 return ide_raw_taskfile(drive, &args, NULL);
576}
577
578static int get_smart_values(ide_drive_t *drive, u8 *buf)
579{
580 ide_task_t args;
581
582 memset(&args, 0, sizeof(ide_task_t));
583 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
584 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
585 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
586 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
587 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
588 args.command_type = IDE_DRIVE_TASK_IN;
589 args.data_phase = TASKFILE_IN;
590 args.handler = &task_in_intr;
591 (void) smart_enable(drive);
592 return ide_raw_taskfile(drive, &args, buf);
593}
594
595static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
596{
597 ide_task_t args;
598 memset(&args, 0, sizeof(ide_task_t));
599 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
600 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
601 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
602 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
603 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
604 args.command_type = IDE_DRIVE_TASK_IN;
605 args.data_phase = TASKFILE_IN;
606 args.handler = &task_in_intr;
607 (void) smart_enable(drive);
608 return ide_raw_taskfile(drive, &args, buf);
609}
610
611static int proc_idedisk_read_cache
612 (char *page, char **start, off_t off, int count, int *eof, void *data)
613{
614 ide_drive_t *drive = (ide_drive_t *) data;
615 char *out = page;
616 int len;
617
618 if (drive->id_read)
619 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
620 else
621 len = sprintf(out,"(none)\n");
622 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
623}
624
625static int proc_idedisk_read_capacity
626 (char *page, char **start, off_t off, int count, int *eof, void *data)
627{
628 ide_drive_t*drive = (ide_drive_t *)data;
629 int len;
630
631 len = sprintf(page,"%llu\n", (long long)idedisk_capacity(drive));
632 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
633}
634
635static int proc_idedisk_read_smart_thresholds
636 (char *page, char **start, off_t off, int count, int *eof, void *data)
637{
638 ide_drive_t *drive = (ide_drive_t *)data;
639 int len = 0, i = 0;
640
641 if (!get_smart_thresholds(drive, page)) {
642 unsigned short *val = (unsigned short *) page;
643 char *out = ((char *)val) + (SECTOR_WORDS * 4);
644 page = out;
645 do {
646 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
647 val += 1;
648 } while (i < (SECTOR_WORDS * 2));
649 len = out - page;
650 }
651 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
652}
653
654static int proc_idedisk_read_smart_values
655 (char *page, char **start, off_t off, int count, int *eof, void *data)
656{
657 ide_drive_t *drive = (ide_drive_t *)data;
658 int len = 0, i = 0;
659
660 if (!get_smart_values(drive, page)) {
661 unsigned short *val = (unsigned short *) page;
662 char *out = ((char *)val) + (SECTOR_WORDS * 4);
663 page = out;
664 do {
665 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
666 val += 1;
667 } while (i < (SECTOR_WORDS * 2));
668 len = out - page;
669 }
670 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
671}
672
673static ide_proc_entry_t idedisk_proc[] = {
674 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
675 { "capacity", S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL },
676 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
677 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
678 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
679 { NULL, 0, NULL, NULL }
680};
681
682#else
683
684#define idedisk_proc NULL
685
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +0200686#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Tejun Heo3e087b52006-01-06 09:57:31 +0100688static void idedisk_prepare_flush(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 ide_drive_t *drive = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 memset(rq->cmd, 0, sizeof(rq->cmd));
693
694 if (ide_id_has_flush_cache_ext(drive->id) &&
695 (drive->capacity64 >= (1UL << 28)))
696 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
697 else
698 rq->cmd[0] = WIN_FLUSH_CACHE;
699
700
Jens Axboe4aff5e22006-08-10 08:44:47 +0200701 rq->cmd_type = REQ_TYPE_ATA_TASK;
702 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 rq->buffer = rq->cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
707 sector_t *error_sector)
708{
709 ide_drive_t *drive = q->queuedata;
710 struct request *rq;
711 int ret;
712
713 if (!drive->wcache)
714 return 0;
715
716 rq = blk_get_request(q, WRITE, __GFP_WAIT);
717
718 idedisk_prepare_flush(q, rq);
719
James Bottomley 994ca9a2005-06-20 14:11:09 +0200720 ret = blk_execute_rq(q, disk, rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /*
723 * if we failed and caller wants error offset, get it
724 */
725 if (ret && error_sector)
726 *error_sector = ide_get_error_location(drive, rq->cmd);
727
728 blk_put_request(rq);
729 return ret;
730}
731
732/*
733 * This is tightly woven into the driver->do_special can not touch.
734 * DON'T do it again until a total personality rewrite is committed.
735 */
736static int set_multcount(ide_drive_t *drive, int arg)
737{
738 struct request rq;
739
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200740 if (arg < 0 || arg > drive->id->max_multsect)
741 return -EINVAL;
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (drive->special.b.set_multmode)
744 return -EBUSY;
745 ide_init_drive_cmd (&rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200746 rq.cmd_type = REQ_TYPE_ATA_CMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 drive->mult_req = arg;
748 drive->special.b.set_multmode = 1;
749 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
750 return (drive->mult_count == arg) ? 0 : -EIO;
751}
752
753static int set_nowerr(ide_drive_t *drive, int arg)
754{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200755 if (arg < 0 || arg > 1)
756 return -EINVAL;
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (ide_spin_wait_hwgroup(drive))
759 return -EBUSY;
760 drive->nowerr = arg;
761 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
762 spin_unlock_irq(&ide_lock);
763 return 0;
764}
765
Tejun Heo3e087b52006-01-06 09:57:31 +0100766static void update_ordered(ide_drive_t *drive)
767{
768 struct hd_driveid *id = drive->id;
769 unsigned ordered = QUEUE_ORDERED_NONE;
770 prepare_flush_fn *prep_fn = NULL;
771 issue_flush_fn *issue_fn = NULL;
772
773 if (drive->wcache) {
774 unsigned long long capacity;
775 int barrier;
776 /*
777 * We must avoid issuing commands a drive does not
778 * understand or we may crash it. We check flush cache
779 * is supported. We also check we have the LBA48 flush
780 * cache if the drive capacity is too large. By this
781 * time we have trimmed the drive capacity if LBA48 is
782 * not available so we don't need to recheck that.
783 */
784 capacity = idedisk_capacity(drive);
Jens Axboe36193482006-07-28 08:54:59 +0200785 barrier = ide_id_has_flush_cache(id) && !drive->noflush &&
Tejun Heo3e087b52006-01-06 09:57:31 +0100786 (drive->addressing == 0 || capacity <= (1ULL << 28) ||
787 ide_id_has_flush_cache_ext(id));
788
789 printk(KERN_INFO "%s: cache flushes %ssupported\n",
Jean Delvaref7ad8362006-02-03 03:04:57 -0800790 drive->name, barrier ? "" : "not ");
Tejun Heo3e087b52006-01-06 09:57:31 +0100791
792 if (barrier) {
793 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
794 prep_fn = idedisk_prepare_flush;
795 issue_fn = idedisk_issue_flush;
796 }
797 } else
798 ordered = QUEUE_ORDERED_DRAIN;
799
800 blk_queue_ordered(drive->queue, ordered, prep_fn);
801 blk_queue_issue_flush_fn(drive->queue, issue_fn);
802}
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804static int write_cache(ide_drive_t *drive, int arg)
805{
806 ide_task_t args;
Tejun Heo3e087b52006-01-06 09:57:31 +0100807 int err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200809 if (arg < 0 || arg > 1)
810 return -EINVAL;
811
Tejun Heo3e087b52006-01-06 09:57:31 +0100812 if (ide_id_has_flush_cache(drive->id)) {
813 memset(&args, 0, sizeof(ide_task_t));
814 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
Tejun Heo3e087b52006-01-06 09:57:31 +0100816 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
817 args.command_type = IDE_DRIVE_TASK_NO_DATA;
818 args.handler = &task_no_data_intr;
819 err = ide_raw_taskfile(drive, &args, NULL);
820 if (err == 0)
821 drive->wcache = arg;
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Tejun Heo3e087b52006-01-06 09:57:31 +0100824 update_ordered(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Tejun Heo3e087b52006-01-06 09:57:31 +0100826 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829static int do_idedisk_flushcache (ide_drive_t *drive)
830{
831 ide_task_t args;
832
833 memset(&args, 0, sizeof(ide_task_t));
834 if (ide_id_has_flush_cache_ext(drive->id))
835 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
836 else
837 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
838 args.command_type = IDE_DRIVE_TASK_NO_DATA;
839 args.handler = &task_no_data_intr;
840 return ide_raw_taskfile(drive, &args, NULL);
841}
842
843static int set_acoustic (ide_drive_t *drive, int arg)
844{
845 ide_task_t args;
846
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200847 if (arg < 0 || arg > 254)
848 return -EINVAL;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 memset(&args, 0, sizeof(ide_task_t));
851 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
852 SETFEATURES_DIS_AAM;
853 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
854 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
855 args.command_type = IDE_DRIVE_TASK_NO_DATA;
856 args.handler = &task_no_data_intr;
857 ide_raw_taskfile(drive, &args, NULL);
858 drive->acoustic = arg;
859 return 0;
860}
861
862/*
863 * drive->addressing:
864 * 0: 28-bit
865 * 1: 48-bit
866 * 2: 48-bit capable doing 28-bit
867 */
868static int set_lba_addressing(ide_drive_t *drive, int arg)
869{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200870 if (arg < 0 || arg > 2)
871 return -EINVAL;
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 drive->addressing = 0;
874
875 if (HWIF(drive)->no_lba48)
876 return 0;
877
878 if (!idedisk_supports_lba48(drive->id))
879 return -EIO;
880 drive->addressing = arg;
881 return 0;
882}
883
884static void idedisk_add_settings(ide_drive_t *drive)
885{
886 struct hd_driveid *id = drive->id;
887
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200888 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
889 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
890 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
891 ide_add_setting(drive, "address", SETTING_RW, TYPE_BYTE, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
892 ide_add_setting(drive, "bswap", SETTING_READ, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
893 ide_add_setting(drive, "multcount", SETTING_RW, TYPE_BYTE, 0, id->max_multsect, 1, 1, &drive->mult_count, set_multcount);
894 ide_add_setting(drive, "nowerr", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
895 ide_add_setting(drive, "lun", SETTING_RW, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
896 ide_add_setting(drive, "wcache", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
897 ide_add_setting(drive, "acoustic", SETTING_RW, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
898 ide_add_setting(drive, "failures", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
899 ide_add_setting(drive, "max_failures", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static void idedisk_setup (ide_drive_t *drive)
903{
904 struct hd_driveid *id = drive->id;
905 unsigned long long capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 idedisk_add_settings(drive);
908
909 if (drive->id_read == 0)
910 return;
911
Richard Purdie98109332006-02-03 03:04:55 -0800912 if (drive->removable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * Removable disks (eg. SYQUEST); ignore 'WD' drives
915 */
916 if (id->model[0] != 'W' || id->model[1] != 'D') {
917 drive->doorlocking = 1;
918 }
919 }
920
921 (void)set_lba_addressing(drive, 1);
922
923 if (drive->addressing == 1) {
924 ide_hwif_t *hwif = HWIF(drive);
925 int max_s = 2048;
926
927 if (max_s > hwif->rqsize)
928 max_s = hwif->rqsize;
929
930 blk_queue_max_sectors(drive->queue, max_s);
931 }
932
933 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
934
935 /* calculate drive capacity, and select LBA if possible */
936 init_idedisk_capacity (drive);
937
938 /* limit drive capacity to 137GB if LBA48 cannot be used */
939 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
940 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
941 "%llu sectors (%llu MB)\n",
942 drive->name, (unsigned long long)drive->capacity64,
943 sectors_to_MB(drive->capacity64));
944 drive->capacity64 = 1ULL << 28;
945 }
946
947 if (drive->hwif->no_lba48_dma && drive->addressing) {
948 if (drive->capacity64 > 1ULL << 28) {
949 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
950 " be used for accessing sectors > %u\n",
951 drive->name, 1 << 28);
952 } else
953 drive->addressing = 0;
954 }
955
956 /*
957 * if possible, give fdisk access to more of the drive,
958 * by correcting bios_cyls:
959 */
960 capacity = idedisk_capacity (drive);
961 if (!drive->forced_geom) {
962
963 if (idedisk_supports_lba48(drive->id)) {
964 /* compatibility */
965 drive->bios_sect = 63;
966 drive->bios_head = 255;
967 }
968
969 if (drive->bios_sect && drive->bios_head) {
970 unsigned int cap0 = capacity; /* truncate to 32 bits */
971 unsigned int cylsz, cyl;
972
973 if (cap0 != capacity)
974 drive->bios_cyl = 65535;
975 else {
976 cylsz = drive->bios_sect * drive->bios_head;
977 cyl = cap0 / cylsz;
978 if (cyl > 65535)
979 cyl = 65535;
980 if (cyl > drive->bios_cyl)
981 drive->bios_cyl = cyl;
982 }
983 }
984 }
985 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
986 drive->name, capacity, sectors_to_MB(capacity));
987
988 /* Only print cache size when it was specified */
989 if (id->buf_size)
990 printk (" w/%dKiB Cache", id->buf_size/2);
991
992 printk(", CHS=%d/%d/%d",
993 drive->bios_cyl, drive->bios_head, drive->bios_sect);
994 if (drive->using_dma)
995 ide_dma_verbose(drive);
996 printk("\n");
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* write cache enabled? */
999 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1000 drive->wcache = 1;
1001
1002 write_cache(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005static void ide_cacheflush_p(ide_drive_t *drive)
1006{
1007 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1008 return;
1009
1010 if (do_idedisk_flushcache(drive))
1011 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1012}
1013
Russell King4031bbe2006-01-06 11:41:00 +00001014static void ide_disk_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 struct ide_disk_obj *idkp = drive->driver_data;
1017 struct gendisk *g = idkp->disk;
1018
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001019 ide_unregister_subdriver(drive, idkp->driver);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 del_gendisk(g);
1022
Bartlomiej Zolnierkiewiczd36fef62005-12-15 02:19:20 +01001023 ide_cacheflush_p(drive);
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 ide_disk_put(idkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028static void ide_disk_release(struct kref *kref)
1029{
1030 struct ide_disk_obj *idkp = to_ide_disk(kref);
1031 ide_drive_t *drive = idkp->drive;
1032 struct gendisk *g = idkp->disk;
1033
1034 drive->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 g->private_data = NULL;
1036 put_disk(g);
1037 kfree(idkp);
1038}
1039
Russell King4031bbe2006-01-06 11:41:00 +00001040static int ide_disk_probe(ide_drive_t *drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Russell King4031bbe2006-01-06 11:41:00 +00001042static void ide_device_shutdown(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044#ifdef CONFIG_ALPHA
1045 /* On Alpha, halt(8) doesn't actually turn the machine off,
1046 it puts you into the sort of firmware monitor. Typically,
1047 it's used to boot another kernel image, so it's not much
1048 different from reboot(8). Therefore, we don't need to
1049 spin down the disk in this case, especially since Alpha
1050 firmware doesn't handle disks in standby mode properly.
1051 On the other hand, it's reasonably safe to turn the power
1052 off when the shutdown process reaches the firmware prompt,
1053 as the firmware initialization takes rather long time -
1054 at least 10 seconds, which should be sufficient for
1055 the disk to expire its write cache. */
1056 if (system_state != SYSTEM_POWER_OFF) {
1057#else
1058 if (system_state == SYSTEM_RESTART) {
1059#endif
1060 ide_cacheflush_p(drive);
1061 return;
1062 }
1063
1064 printk("Shutdown: %s\n", drive->name);
Russell King4031bbe2006-01-06 11:41:00 +00001065 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068static ide_driver_t idedisk_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001070 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001071 .name = "ide-disk",
1072 .bus = &ide_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 },
Russell King4031bbe2006-01-06 11:41:00 +00001074 .probe = ide_disk_probe,
1075 .remove = ide_disk_remove,
1076 .shutdown = ide_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 .version = IDEDISK_VERSION,
1078 .media = ide_disk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 .do_request = ide_do_rw_disk,
1081 .end_request = ide_end_request,
1082 .error = __ide_error,
1083 .abort = __ide_abort,
1084 .proc = idedisk_proc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085};
1086
1087static int idedisk_open(struct inode *inode, struct file *filp)
1088{
1089 struct gendisk *disk = inode->i_bdev->bd_disk;
1090 struct ide_disk_obj *idkp;
1091 ide_drive_t *drive;
1092
1093 if (!(idkp = ide_disk_get(disk)))
1094 return -ENXIO;
1095
1096 drive = idkp->drive;
1097
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001098 idkp->openers++;
1099
1100 if (drive->removable && idkp->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ide_task_t args;
1102 memset(&args, 0, sizeof(ide_task_t));
1103 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1104 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1105 args.handler = &task_no_data_intr;
1106 check_disk_change(inode->i_bdev);
1107 /*
1108 * Ignore the return code from door_lock,
1109 * since the open() has already succeeded,
1110 * and the door_lock is irrelevant at this point.
1111 */
1112 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1113 drive->doorlocking = 0;
1114 }
1115 return 0;
1116}
1117
1118static int idedisk_release(struct inode *inode, struct file *filp)
1119{
1120 struct gendisk *disk = inode->i_bdev->bd_disk;
1121 struct ide_disk_obj *idkp = ide_disk_g(disk);
1122 ide_drive_t *drive = idkp->drive;
1123
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001124 if (idkp->openers == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ide_cacheflush_p(drive);
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001126
1127 if (drive->removable && idkp->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ide_task_t args;
1129 memset(&args, 0, sizeof(ide_task_t));
1130 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1131 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1132 args.handler = &task_no_data_intr;
1133 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1134 drive->doorlocking = 0;
1135 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001136
1137 idkp->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 ide_disk_put(idkp);
1140
1141 return 0;
1142}
1143
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001144static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1145{
1146 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1147 ide_drive_t *drive = idkp->drive;
1148
1149 geo->heads = drive->bios_head;
1150 geo->sectors = drive->bios_sect;
1151 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1152 return 0;
1153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static int idedisk_ioctl(struct inode *inode, struct file *file,
1156 unsigned int cmd, unsigned long arg)
1157{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001158 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct block_device *bdev = inode->i_bdev;
1160 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001161 ide_drive_t *drive = idkp->drive;
1162 int err, (*setfunc)(ide_drive_t *, int);
1163 u8 *val;
1164
1165 switch (cmd) {
1166 case HDIO_GET_ADDRESS: val = &drive->addressing; goto read_val;
1167 case HDIO_GET_MULTCOUNT: val = &drive->mult_count; goto read_val;
1168 case HDIO_GET_NOWERR: val = &drive->nowerr; goto read_val;
1169 case HDIO_GET_WCACHE: val = &drive->wcache; goto read_val;
1170 case HDIO_GET_ACOUSTIC: val = &drive->acoustic; goto read_val;
1171 case HDIO_SET_ADDRESS: setfunc = set_lba_addressing; goto set_val;
1172 case HDIO_SET_MULTCOUNT: setfunc = set_multcount; goto set_val;
1173 case HDIO_SET_NOWERR: setfunc = set_nowerr; goto set_val;
1174 case HDIO_SET_WCACHE: setfunc = write_cache; goto set_val;
1175 case HDIO_SET_ACOUSTIC: setfunc = set_acoustic; goto set_val;
1176 }
1177
1178 return generic_ide_ioctl(drive, file, bdev, cmd, arg);
1179
1180read_val:
1181 down(&ide_setting_sem);
1182 spin_lock_irqsave(&ide_lock, flags);
1183 err = *val;
1184 spin_unlock_irqrestore(&ide_lock, flags);
1185 up(&ide_setting_sem);
1186 return err >= 0 ? put_user(err, (long __user *)arg) : err;
1187
1188set_val:
1189 if (bdev != bdev->bd_contains)
1190 err = -EINVAL;
1191 else {
1192 if (!capable(CAP_SYS_ADMIN))
1193 err = -EACCES;
1194 else {
1195 down(&ide_setting_sem);
1196 err = setfunc(drive, arg);
1197 up(&ide_setting_sem);
1198 }
1199 }
1200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203static int idedisk_media_changed(struct gendisk *disk)
1204{
1205 struct ide_disk_obj *idkp = ide_disk_g(disk);
1206 ide_drive_t *drive = idkp->drive;
1207
1208 /* do not scan partitions twice if this is a removable device */
1209 if (drive->attach) {
1210 drive->attach = 0;
1211 return 0;
1212 }
1213 /* if removable, always assume it was changed */
1214 return drive->removable;
1215}
1216
1217static int idedisk_revalidate_disk(struct gendisk *disk)
1218{
1219 struct ide_disk_obj *idkp = ide_disk_g(disk);
1220 set_capacity(disk, idedisk_capacity(idkp->drive));
1221 return 0;
1222}
1223
1224static struct block_device_operations idedisk_ops = {
1225 .owner = THIS_MODULE,
1226 .open = idedisk_open,
1227 .release = idedisk_release,
1228 .ioctl = idedisk_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001229 .getgeo = idedisk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .media_changed = idedisk_media_changed,
1231 .revalidate_disk= idedisk_revalidate_disk
1232};
1233
1234MODULE_DESCRIPTION("ATA DISK Driver");
1235
Russell King4031bbe2006-01-06 11:41:00 +00001236static int ide_disk_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct ide_disk_obj *idkp;
1239 struct gendisk *g;
1240
1241 /* strstr("foo", "") is non-NULL */
1242 if (!strstr("ide-disk", drive->driver_req))
1243 goto failed;
1244 if (!drive->present)
1245 goto failed;
1246 if (drive->media != ide_disk)
1247 goto failed;
1248
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -08001249 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (!idkp)
1251 goto failed;
1252
Christoph Lameter19460892005-06-23 00:08:19 -07001253 g = alloc_disk_node(1 << PARTN_BITS,
Christoph Lameter86b37862005-08-09 19:59:21 -07001254 hwif_to_node(drive->hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (!g)
1256 goto out_free_idkp;
1257
1258 ide_init_disk(g, drive);
1259
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001260 ide_register_subdriver(drive, &idedisk_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 kref_init(&idkp->kref);
1263
1264 idkp->drive = drive;
1265 idkp->driver = &idedisk_driver;
1266 idkp->disk = g;
1267
1268 g->private_data = &idkp->driver;
1269
1270 drive->driver_data = idkp;
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 idedisk_setup(drive);
1273 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1274 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1275 drive->name, drive->head);
1276 drive->attach = 0;
1277 } else
1278 drive->attach = 1;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 g->minors = 1 << PARTN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 g->driverfs_dev = &drive->gendev;
1282 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1283 set_capacity(g, idedisk_capacity(drive));
1284 g->fops = &idedisk_ops;
1285 add_disk(g);
1286 return 0;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288out_free_idkp:
1289 kfree(idkp);
1290failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001291 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294static void __exit idedisk_exit (void)
1295{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001296 driver_unregister(&idedisk_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001299static int __init idedisk_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001301 return driver_register(&idedisk_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Kay Sievers263756e2005-12-12 18:03:44 +01001304MODULE_ALIAS("ide:*m-disk*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305module_init(idedisk_init);
1306module_exit(idedisk_exit);
1307MODULE_LICENSE("GPL");