blob: f033d732f387277fbabe2fe7bb4123afe371a9d8 [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
46#undef REALLY_SLOW_IO /* most systems can safely undef this */
47
48//#define DEBUG
49
50#include <linux/config.h>
51#include <linux/module.h>
52#include <linux/types.h>
53#include <linux/string.h>
54#include <linux/kernel.h>
55#include <linux/timer.h>
56#include <linux/mm.h>
57#include <linux/interrupt.h>
58#include <linux/major.h>
59#include <linux/errno.h>
60#include <linux/genhd.h>
61#include <linux/slab.h>
62#include <linux/delay.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080063#include <linux/mutex.h>
Richard Purdie2bfb6462006-03-31 02:31:16 -080064#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#define _IDE_DISK
67
68#include <linux/ide.h>
69
70#include <asm/byteorder.h>
71#include <asm/irq.h>
72#include <asm/uaccess.h>
73#include <asm/io.h>
74#include <asm/div64.h>
75
76struct ide_disk_obj {
77 ide_drive_t *drive;
78 ide_driver_t *driver;
79 struct gendisk *disk;
80 struct kref kref;
81};
82
Arjan van de Vencf8b8972006-03-23 03:00:45 -080083static DEFINE_MUTEX(idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref)
86
87#define ide_disk_g(disk) \
88 container_of((disk)->private_data, struct ide_disk_obj, driver)
89
90static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
91{
92 struct ide_disk_obj *idkp = NULL;
93
Arjan van de Vencf8b8972006-03-23 03:00:45 -080094 mutex_lock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 idkp = ide_disk_g(disk);
96 if (idkp)
97 kref_get(&idkp->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080098 mutex_unlock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return idkp;
100}
101
102static void ide_disk_release(struct kref *);
103
104static void ide_disk_put(struct ide_disk_obj *idkp)
105{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800106 mutex_lock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 kref_put(&idkp->kref, ide_disk_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800108 mutex_unlock(&idedisk_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111/*
112 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
113 * value for this drive (from its reported identification information).
114 *
115 * Returns: 1 if lba_capacity looks sensible
116 * 0 otherwise
117 *
118 * It is called only once for each drive.
119 */
120static int lba_capacity_is_ok (struct hd_driveid *id)
121{
122 unsigned long lba_sects, chs_sects, head, tail;
123
Alan Cox6efd9362005-06-27 15:24:22 -0700124 /* No non-LBA info .. so valid! */
125 if (id->cyls == 0)
126 return 1;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /*
129 * The ATA spec tells large drives to return
130 * C/H/S = 16383/16/63 independent of their size.
131 * Some drives can be jumpered to use 15 heads instead of 16.
132 * Some drives can be jumpered to use 4092 cyls instead of 16383.
133 */
134 if ((id->cyls == 16383
135 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
136 id->sectors == 63 &&
137 (id->heads == 15 || id->heads == 16) &&
138 (id->lba_capacity >= 16383*63*id->heads))
139 return 1;
140
141 lba_sects = id->lba_capacity;
142 chs_sects = id->cyls * id->heads * id->sectors;
143
144 /* perform a rough sanity check on lba_sects: within 10% is OK */
145 if ((lba_sects - chs_sects) < chs_sects/10)
146 return 1;
147
148 /* some drives have the word order reversed */
149 head = ((lba_sects >> 16) & 0xffff);
150 tail = (lba_sects & 0xffff);
151 lba_sects = (head | (tail << 16));
152 if ((lba_sects - chs_sects) < chs_sects/10) {
153 id->lba_capacity = lba_sects;
154 return 1; /* lba_capacity is (now) good */
155 }
156
157 return 0; /* lba_capacity value may be bad */
158}
159
160/*
161 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
162 * using LBA if supported, or CHS otherwise, to address sectors.
163 */
164static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
165{
166 ide_hwif_t *hwif = HWIF(drive);
167 unsigned int dma = drive->using_dma;
168 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
169 task_ioreg_t command = WIN_NOP;
170 ata_nsector_t nsectors;
171
172 nsectors.all = (u16) rq->nr_sectors;
173
174 if (hwif->no_lba48_dma && lba48 && dma) {
175 if (block + rq->nr_sectors > 1ULL << 28)
176 dma = 0;
177 else
178 lba48 = 0;
179 }
180
181 if (!dma) {
182 ide_init_sg_cmd(drive, rq);
183 ide_map_sg(drive, rq);
184 }
185
186 if (IDE_CONTROL_REG)
187 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
188
189 /* FIXME: SELECT_MASK(drive, 0) ? */
190
191 if (drive->select.b.lba) {
192 if (lba48) {
193 task_ioreg_t tasklets[10];
194
Michael Richardsonc2f83112006-02-07 12:58:33 -0800195 pr_debug("%s: LBA=0x%012llx\n", drive->name,
196 (unsigned long long)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 tasklets[0] = 0;
199 tasklets[1] = 0;
200 tasklets[2] = nsectors.b.low;
201 tasklets[3] = nsectors.b.high;
202 tasklets[4] = (task_ioreg_t) block;
203 tasklets[5] = (task_ioreg_t) (block>>8);
204 tasklets[6] = (task_ioreg_t) (block>>16);
205 tasklets[7] = (task_ioreg_t) (block>>24);
206 if (sizeof(block) == 4) {
207 tasklets[8] = (task_ioreg_t) 0;
208 tasklets[9] = (task_ioreg_t) 0;
209 } else {
210 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
211 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
212 }
213#ifdef DEBUG
214 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
215 drive->name, tasklets[3], tasklets[2],
216 tasklets[9], tasklets[8], tasklets[7],
217 tasklets[6], tasklets[5], tasklets[4]);
218#endif
219 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
220 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
221 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
222 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
223 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
224
225 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
226 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
227 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
228 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
229 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
230 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
231 } else {
232 hwif->OUTB(0x00, IDE_FEATURE_REG);
233 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
234 hwif->OUTB(block, IDE_SECTOR_REG);
235 hwif->OUTB(block>>=8, IDE_LCYL_REG);
236 hwif->OUTB(block>>=8, IDE_HCYL_REG);
237 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
238 }
239 } else {
240 unsigned int sect,head,cyl,track;
241 track = (int)block / drive->sect;
242 sect = (int)block % drive->sect + 1;
243 hwif->OUTB(sect, IDE_SECTOR_REG);
244 head = track % drive->head;
245 cyl = track / drive->head;
246
247 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
248
249 hwif->OUTB(0x00, IDE_FEATURE_REG);
250 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
251 hwif->OUTB(cyl, IDE_LCYL_REG);
252 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
253 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
254 }
255
256 if (dma) {
257 if (!hwif->dma_setup(drive)) {
258 if (rq_data_dir(rq)) {
259 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
260 if (drive->vdma)
261 command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
262 } else {
263 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
264 if (drive->vdma)
265 command = lba48 ? WIN_READ_EXT: WIN_READ;
266 }
267 hwif->dma_exec_cmd(drive, command);
268 hwif->dma_start(drive);
269 return ide_started;
270 }
271 /* fallback to PIO */
272 ide_init_sg_cmd(drive, rq);
273 }
274
275 if (rq_data_dir(rq) == READ) {
276
277 if (drive->mult_count) {
278 hwif->data_phase = TASKFILE_MULTI_IN;
279 command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
280 } else {
281 hwif->data_phase = TASKFILE_IN;
282 command = lba48 ? WIN_READ_EXT : WIN_READ;
283 }
284
285 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
286 return ide_started;
287 } else {
288 if (drive->mult_count) {
289 hwif->data_phase = TASKFILE_MULTI_OUT;
290 command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
291 } else {
292 hwif->data_phase = TASKFILE_OUT;
293 command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
294 }
295
296 /* FIXME: ->OUTBSYNC ? */
297 hwif->OUTB(command, IDE_COMMAND_REG);
298
299 return pre_task_out_intr(drive, rq);
300 }
301}
302
303/*
304 * 268435455 == 137439 MB or 28bit limit
305 * 320173056 == 163929 MB or 48bit addressing
306 * 1073741822 == 549756 MB or 48bit addressing fake drive
307 */
308
309static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
310{
311 ide_hwif_t *hwif = HWIF(drive);
312
313 BUG_ON(drive->blocked);
314
315 if (!blk_fs_request(rq)) {
316 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
317 ide_end_request(drive, 0, 0);
318 return ide_stopped;
319 }
320
Richard Purdie2bfb6462006-03-31 02:31:16 -0800321 ledtrig_ide_activity();
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
324 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
Michael Richardsonc2f83112006-02-07 12:58:33 -0800325 (unsigned long long)block, rq->nr_sectors,
326 (unsigned long)rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (hwif->rw_disk)
329 hwif->rw_disk(drive, rq);
330
331 return __ide_do_rw_disk(drive, rq, block);
332}
333
334/*
335 * Queries for true maximum capacity of the drive.
336 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
337 */
338static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
339{
340 ide_task_t args;
341 unsigned long addr = 0;
342
343 /* Create IDE/ATA command request structure */
344 memset(&args, 0, sizeof(ide_task_t));
345 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
346 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
347 args.command_type = IDE_DRIVE_TASK_NO_DATA;
348 args.handler = &task_no_data_intr;
349 /* submit command request */
350 ide_raw_taskfile(drive, &args, NULL);
351
352 /* if OK, compute maximum address value */
353 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
354 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
355 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
356 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
357 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
358 addr++; /* since the return value is (maxlba - 1), we add 1 */
359 }
360 return addr;
361}
362
363static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
364{
365 ide_task_t args;
366 unsigned long long addr = 0;
367
368 /* Create IDE/ATA command request structure */
369 memset(&args, 0, sizeof(ide_task_t));
370
371 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
372 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
373 args.command_type = IDE_DRIVE_TASK_NO_DATA;
374 args.handler = &task_no_data_intr;
375 /* submit command request */
376 ide_raw_taskfile(drive, &args, NULL);
377
378 /* if OK, compute maximum address value */
379 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
380 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
381 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
382 args.hobRegister[IDE_SECTOR_OFFSET];
383 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
384 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
385 (args.tfRegister[IDE_SECTOR_OFFSET]);
386 addr = ((__u64)high << 24) | low;
387 addr++; /* since the return value is (maxlba - 1), we add 1 */
388 }
389 return addr;
390}
391
392/*
393 * Sets maximum virtual LBA address of the drive.
394 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
395 */
396static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
397{
398 ide_task_t args;
399 unsigned long addr_set = 0;
400
401 addr_req--;
402 /* Create IDE/ATA command request structure */
403 memset(&args, 0, sizeof(ide_task_t));
404 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
405 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
406 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
407 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
408 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
409 args.command_type = IDE_DRIVE_TASK_NO_DATA;
410 args.handler = &task_no_data_intr;
411 /* submit command request */
412 ide_raw_taskfile(drive, &args, NULL);
413 /* if OK, read new maximum address value */
414 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
415 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
416 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
417 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
418 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
419 addr_set++;
420 }
421 return addr_set;
422}
423
424static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
425{
426 ide_task_t args;
427 unsigned long long addr_set = 0;
428
429 addr_req--;
430 /* Create IDE/ATA command request structure */
431 memset(&args, 0, sizeof(ide_task_t));
432 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
433 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
434 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
435 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
436 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
437 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
438 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
439 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
440 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
441 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
442 args.command_type = IDE_DRIVE_TASK_NO_DATA;
443 args.handler = &task_no_data_intr;
444 /* submit command request */
445 ide_raw_taskfile(drive, &args, NULL);
446 /* if OK, compute maximum address value */
447 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
448 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
449 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
450 args.hobRegister[IDE_SECTOR_OFFSET];
451 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
452 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
453 (args.tfRegister[IDE_SECTOR_OFFSET]);
454 addr_set = ((__u64)high << 24) | low;
455 addr_set++;
456 }
457 return addr_set;
458}
459
460static unsigned long long sectors_to_MB(unsigned long long n)
461{
462 n <<= 9; /* make it bytes */
463 do_div(n, 1000000); /* make it MB */
464 return n;
465}
466
467/*
468 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
469 * so on non-buggy drives we need test only one.
470 * However, we should also check whether these fields are valid.
471 */
472static inline int idedisk_supports_hpa(const struct hd_driveid *id)
473{
474 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
475}
476
477/*
478 * The same here.
479 */
480static inline int idedisk_supports_lba48(const struct hd_driveid *id)
481{
482 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
483 && id->lba_capacity_2;
484}
485
Arjan van de Ven858119e2006-01-14 13:20:43 -0800486static void idedisk_check_hpa(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 unsigned long long capacity, set_max;
489 int lba48 = idedisk_supports_lba48(drive->id);
490
491 capacity = drive->capacity64;
492 if (lba48)
493 set_max = idedisk_read_native_max_address_ext(drive);
494 else
495 set_max = idedisk_read_native_max_address(drive);
496
497 if (set_max <= capacity)
498 return;
499
500 printk(KERN_INFO "%s: Host Protected Area detected.\n"
501 "\tcurrent capacity is %llu sectors (%llu MB)\n"
502 "\tnative capacity is %llu sectors (%llu MB)\n",
503 drive->name,
504 capacity, sectors_to_MB(capacity),
505 set_max, sectors_to_MB(set_max));
506
507 if (lba48)
508 set_max = idedisk_set_max_address_ext(drive, set_max);
509 else
510 set_max = idedisk_set_max_address(drive, set_max);
511 if (set_max) {
512 drive->capacity64 = set_max;
513 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
514 drive->name);
515 }
516}
517
518/*
519 * Compute drive->capacity, the full capacity of the drive
520 * Called with drive->id != NULL.
521 *
522 * To compute capacity, this uses either of
523 *
524 * 1. CHS value set by user (whatever user sets will be trusted)
525 * 2. LBA value from target drive (require new ATA feature)
526 * 3. LBA value from system BIOS (new one is OK, old one may break)
527 * 4. CHS value from system BIOS (traditional style)
528 *
529 * in above order (i.e., if value of higher priority is available,
530 * reset will be ignored).
531 */
532static void init_idedisk_capacity (ide_drive_t *drive)
533{
534 struct hd_driveid *id = drive->id;
535 /*
536 * If this drive supports the Host Protected Area feature set,
537 * then we may need to change our opinion about the drive's capacity.
538 */
539 int hpa = idedisk_supports_hpa(id);
540
541 if (idedisk_supports_lba48(id)) {
542 /* drive speaks 48-bit LBA */
543 drive->select.b.lba = 1;
544 drive->capacity64 = id->lba_capacity_2;
545 if (hpa)
546 idedisk_check_hpa(drive);
547 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
548 /* drive speaks 28-bit LBA */
549 drive->select.b.lba = 1;
550 drive->capacity64 = id->lba_capacity;
551 if (hpa)
552 idedisk_check_hpa(drive);
553 } else {
554 /* drive speaks boring old 28-bit CHS */
555 drive->capacity64 = drive->cyl * drive->head * drive->sect;
556 }
557}
558
559static sector_t idedisk_capacity (ide_drive_t *drive)
560{
561 return drive->capacity64 - drive->sect0;
562}
563
564#ifdef CONFIG_PROC_FS
565
566static int smart_enable(ide_drive_t *drive)
567{
568 ide_task_t args;
569
570 memset(&args, 0, sizeof(ide_task_t));
571 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
572 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
573 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
574 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
575 args.command_type = IDE_DRIVE_TASK_NO_DATA;
576 args.handler = &task_no_data_intr;
577 return ide_raw_taskfile(drive, &args, NULL);
578}
579
580static int get_smart_values(ide_drive_t *drive, u8 *buf)
581{
582 ide_task_t args;
583
584 memset(&args, 0, sizeof(ide_task_t));
585 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
586 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
587 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
588 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
589 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
590 args.command_type = IDE_DRIVE_TASK_IN;
591 args.data_phase = TASKFILE_IN;
592 args.handler = &task_in_intr;
593 (void) smart_enable(drive);
594 return ide_raw_taskfile(drive, &args, buf);
595}
596
597static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
598{
599 ide_task_t args;
600 memset(&args, 0, sizeof(ide_task_t));
601 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
602 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
603 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
604 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
605 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
606 args.command_type = IDE_DRIVE_TASK_IN;
607 args.data_phase = TASKFILE_IN;
608 args.handler = &task_in_intr;
609 (void) smart_enable(drive);
610 return ide_raw_taskfile(drive, &args, buf);
611}
612
613static int proc_idedisk_read_cache
614 (char *page, char **start, off_t off, int count, int *eof, void *data)
615{
616 ide_drive_t *drive = (ide_drive_t *) data;
617 char *out = page;
618 int len;
619
620 if (drive->id_read)
621 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
622 else
623 len = sprintf(out,"(none)\n");
624 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
625}
626
627static int proc_idedisk_read_capacity
628 (char *page, char **start, off_t off, int count, int *eof, void *data)
629{
630 ide_drive_t*drive = (ide_drive_t *)data;
631 int len;
632
633 len = sprintf(page,"%llu\n", (long long)idedisk_capacity(drive));
634 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
635}
636
637static int proc_idedisk_read_smart_thresholds
638 (char *page, char **start, off_t off, int count, int *eof, void *data)
639{
640 ide_drive_t *drive = (ide_drive_t *)data;
641 int len = 0, i = 0;
642
643 if (!get_smart_thresholds(drive, page)) {
644 unsigned short *val = (unsigned short *) page;
645 char *out = ((char *)val) + (SECTOR_WORDS * 4);
646 page = out;
647 do {
648 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
649 val += 1;
650 } while (i < (SECTOR_WORDS * 2));
651 len = out - page;
652 }
653 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
654}
655
656static int proc_idedisk_read_smart_values
657 (char *page, char **start, off_t off, int count, int *eof, void *data)
658{
659 ide_drive_t *drive = (ide_drive_t *)data;
660 int len = 0, i = 0;
661
662 if (!get_smart_values(drive, page)) {
663 unsigned short *val = (unsigned short *) page;
664 char *out = ((char *)val) + (SECTOR_WORDS * 4);
665 page = out;
666 do {
667 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
668 val += 1;
669 } while (i < (SECTOR_WORDS * 2));
670 len = out - page;
671 }
672 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
673}
674
675static ide_proc_entry_t idedisk_proc[] = {
676 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
677 { "capacity", S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL },
678 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
679 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
680 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
681 { NULL, 0, NULL, NULL }
682};
683
684#else
685
686#define idedisk_proc NULL
687
688#endif /* CONFIG_PROC_FS */
689
Tejun Heo3e087b52006-01-06 09:57:31 +0100690static void idedisk_prepare_flush(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 ide_drive_t *drive = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 memset(rq->cmd, 0, sizeof(rq->cmd));
695
696 if (ide_id_has_flush_cache_ext(drive->id) &&
697 (drive->capacity64 >= (1UL << 28)))
698 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
699 else
700 rq->cmd[0] = WIN_FLUSH_CACHE;
701
702
Tejun Heo3e087b52006-01-06 09:57:31 +0100703 rq->flags |= REQ_DRIVE_TASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rq->buffer = rq->cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
708 sector_t *error_sector)
709{
710 ide_drive_t *drive = q->queuedata;
711 struct request *rq;
712 int ret;
713
714 if (!drive->wcache)
715 return 0;
716
717 rq = blk_get_request(q, WRITE, __GFP_WAIT);
718
719 idedisk_prepare_flush(q, rq);
720
James Bottomley 994ca9a2005-06-20 14:11:09 +0200721 ret = blk_execute_rq(q, disk, rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * if we failed and caller wants error offset, get it
725 */
726 if (ret && error_sector)
727 *error_sector = ide_get_error_location(drive, rq->cmd);
728
729 blk_put_request(rq);
730 return ret;
731}
732
733/*
734 * This is tightly woven into the driver->do_special can not touch.
735 * DON'T do it again until a total personality rewrite is committed.
736 */
737static int set_multcount(ide_drive_t *drive, int arg)
738{
739 struct request rq;
740
741 if (drive->special.b.set_multmode)
742 return -EBUSY;
743 ide_init_drive_cmd (&rq);
744 rq.flags = REQ_DRIVE_CMD;
745 drive->mult_req = arg;
746 drive->special.b.set_multmode = 1;
747 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
748 return (drive->mult_count == arg) ? 0 : -EIO;
749}
750
751static int set_nowerr(ide_drive_t *drive, int arg)
752{
753 if (ide_spin_wait_hwgroup(drive))
754 return -EBUSY;
755 drive->nowerr = arg;
756 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
757 spin_unlock_irq(&ide_lock);
758 return 0;
759}
760
Tejun Heo3e087b52006-01-06 09:57:31 +0100761static void update_ordered(ide_drive_t *drive)
762{
763 struct hd_driveid *id = drive->id;
764 unsigned ordered = QUEUE_ORDERED_NONE;
765 prepare_flush_fn *prep_fn = NULL;
766 issue_flush_fn *issue_fn = NULL;
767
768 if (drive->wcache) {
769 unsigned long long capacity;
770 int barrier;
771 /*
772 * We must avoid issuing commands a drive does not
773 * understand or we may crash it. We check flush cache
774 * is supported. We also check we have the LBA48 flush
775 * cache if the drive capacity is too large. By this
776 * time we have trimmed the drive capacity if LBA48 is
777 * not available so we don't need to recheck that.
778 */
779 capacity = idedisk_capacity(drive);
780 barrier = ide_id_has_flush_cache(id) &&
781 (drive->addressing == 0 || capacity <= (1ULL << 28) ||
782 ide_id_has_flush_cache_ext(id));
783
784 printk(KERN_INFO "%s: cache flushes %ssupported\n",
Jean Delvaref7ad8362006-02-03 03:04:57 -0800785 drive->name, barrier ? "" : "not ");
Tejun Heo3e087b52006-01-06 09:57:31 +0100786
787 if (barrier) {
788 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
789 prep_fn = idedisk_prepare_flush;
790 issue_fn = idedisk_issue_flush;
791 }
792 } else
793 ordered = QUEUE_ORDERED_DRAIN;
794
795 blk_queue_ordered(drive->queue, ordered, prep_fn);
796 blk_queue_issue_flush_fn(drive->queue, issue_fn);
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static int write_cache(ide_drive_t *drive, int arg)
800{
801 ide_task_t args;
Tejun Heo3e087b52006-01-06 09:57:31 +0100802 int err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Tejun Heo3e087b52006-01-06 09:57:31 +0100804 if (ide_id_has_flush_cache(drive->id)) {
805 memset(&args, 0, sizeof(ide_task_t));
806 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
Tejun Heo3e087b52006-01-06 09:57:31 +0100808 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
809 args.command_type = IDE_DRIVE_TASK_NO_DATA;
810 args.handler = &task_no_data_intr;
811 err = ide_raw_taskfile(drive, &args, NULL);
812 if (err == 0)
813 drive->wcache = arg;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Tejun Heo3e087b52006-01-06 09:57:31 +0100816 update_ordered(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Tejun Heo3e087b52006-01-06 09:57:31 +0100818 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821static int do_idedisk_flushcache (ide_drive_t *drive)
822{
823 ide_task_t args;
824
825 memset(&args, 0, sizeof(ide_task_t));
826 if (ide_id_has_flush_cache_ext(drive->id))
827 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
828 else
829 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
830 args.command_type = IDE_DRIVE_TASK_NO_DATA;
831 args.handler = &task_no_data_intr;
832 return ide_raw_taskfile(drive, &args, NULL);
833}
834
835static int set_acoustic (ide_drive_t *drive, int arg)
836{
837 ide_task_t args;
838
839 memset(&args, 0, sizeof(ide_task_t));
840 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
841 SETFEATURES_DIS_AAM;
842 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
843 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
844 args.command_type = IDE_DRIVE_TASK_NO_DATA;
845 args.handler = &task_no_data_intr;
846 ide_raw_taskfile(drive, &args, NULL);
847 drive->acoustic = arg;
848 return 0;
849}
850
851/*
852 * drive->addressing:
853 * 0: 28-bit
854 * 1: 48-bit
855 * 2: 48-bit capable doing 28-bit
856 */
857static int set_lba_addressing(ide_drive_t *drive, int arg)
858{
859 drive->addressing = 0;
860
861 if (HWIF(drive)->no_lba48)
862 return 0;
863
864 if (!idedisk_supports_lba48(drive->id))
865 return -EIO;
866 drive->addressing = arg;
867 return 0;
868}
869
870static void idedisk_add_settings(ide_drive_t *drive)
871{
872 struct hd_driveid *id = drive->id;
873
874 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
875 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
876 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
877 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
878 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
879 ide_add_setting(drive, "multcount", id ? SETTING_RW : SETTING_READ, HDIO_GET_MULTCOUNT, HDIO_SET_MULTCOUNT, TYPE_BYTE, 0, id ? id->max_multsect : 0, 1, 1, &drive->mult_count, set_multcount);
880 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
881 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
882 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
883 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
884 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
885 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
886}
887
888static void idedisk_setup (ide_drive_t *drive)
889{
890 struct hd_driveid *id = drive->id;
891 unsigned long long capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 idedisk_add_settings(drive);
894
895 if (drive->id_read == 0)
896 return;
897
Richard Purdie98109332006-02-03 03:04:55 -0800898 if (drive->removable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * Removable disks (eg. SYQUEST); ignore 'WD' drives
901 */
902 if (id->model[0] != 'W' || id->model[1] != 'D') {
903 drive->doorlocking = 1;
904 }
905 }
906
907 (void)set_lba_addressing(drive, 1);
908
909 if (drive->addressing == 1) {
910 ide_hwif_t *hwif = HWIF(drive);
911 int max_s = 2048;
912
913 if (max_s > hwif->rqsize)
914 max_s = hwif->rqsize;
915
916 blk_queue_max_sectors(drive->queue, max_s);
917 }
918
919 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
920
921 /* calculate drive capacity, and select LBA if possible */
922 init_idedisk_capacity (drive);
923
924 /* limit drive capacity to 137GB if LBA48 cannot be used */
925 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
926 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
927 "%llu sectors (%llu MB)\n",
928 drive->name, (unsigned long long)drive->capacity64,
929 sectors_to_MB(drive->capacity64));
930 drive->capacity64 = 1ULL << 28;
931 }
932
933 if (drive->hwif->no_lba48_dma && drive->addressing) {
934 if (drive->capacity64 > 1ULL << 28) {
935 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
936 " be used for accessing sectors > %u\n",
937 drive->name, 1 << 28);
938 } else
939 drive->addressing = 0;
940 }
941
942 /*
943 * if possible, give fdisk access to more of the drive,
944 * by correcting bios_cyls:
945 */
946 capacity = idedisk_capacity (drive);
947 if (!drive->forced_geom) {
948
949 if (idedisk_supports_lba48(drive->id)) {
950 /* compatibility */
951 drive->bios_sect = 63;
952 drive->bios_head = 255;
953 }
954
955 if (drive->bios_sect && drive->bios_head) {
956 unsigned int cap0 = capacity; /* truncate to 32 bits */
957 unsigned int cylsz, cyl;
958
959 if (cap0 != capacity)
960 drive->bios_cyl = 65535;
961 else {
962 cylsz = drive->bios_sect * drive->bios_head;
963 cyl = cap0 / cylsz;
964 if (cyl > 65535)
965 cyl = 65535;
966 if (cyl > drive->bios_cyl)
967 drive->bios_cyl = cyl;
968 }
969 }
970 }
971 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
972 drive->name, capacity, sectors_to_MB(capacity));
973
974 /* Only print cache size when it was specified */
975 if (id->buf_size)
976 printk (" w/%dKiB Cache", id->buf_size/2);
977
978 printk(", CHS=%d/%d/%d",
979 drive->bios_cyl, drive->bios_head, drive->bios_sect);
980 if (drive->using_dma)
981 ide_dma_verbose(drive);
982 printk("\n");
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /* write cache enabled? */
985 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
986 drive->wcache = 1;
987
988 write_cache(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991static void ide_cacheflush_p(ide_drive_t *drive)
992{
993 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
994 return;
995
996 if (do_idedisk_flushcache(drive))
997 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
998}
999
Russell King4031bbe2006-01-06 11:41:00 +00001000static void ide_disk_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct ide_disk_obj *idkp = drive->driver_data;
1003 struct gendisk *g = idkp->disk;
1004
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001005 ide_unregister_subdriver(drive, idkp->driver);
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 del_gendisk(g);
1008
Bartlomiej Zolnierkiewiczd36fef62005-12-15 02:19:20 +01001009 ide_cacheflush_p(drive);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 ide_disk_put(idkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014static void ide_disk_release(struct kref *kref)
1015{
1016 struct ide_disk_obj *idkp = to_ide_disk(kref);
1017 ide_drive_t *drive = idkp->drive;
1018 struct gendisk *g = idkp->disk;
1019
1020 drive->driver_data = NULL;
1021 drive->devfs_name[0] = '\0';
1022 g->private_data = NULL;
1023 put_disk(g);
1024 kfree(idkp);
1025}
1026
Russell King4031bbe2006-01-06 11:41:00 +00001027static int ide_disk_probe(ide_drive_t *drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Russell King4031bbe2006-01-06 11:41:00 +00001029static void ide_device_shutdown(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031#ifdef CONFIG_ALPHA
1032 /* On Alpha, halt(8) doesn't actually turn the machine off,
1033 it puts you into the sort of firmware monitor. Typically,
1034 it's used to boot another kernel image, so it's not much
1035 different from reboot(8). Therefore, we don't need to
1036 spin down the disk in this case, especially since Alpha
1037 firmware doesn't handle disks in standby mode properly.
1038 On the other hand, it's reasonably safe to turn the power
1039 off when the shutdown process reaches the firmware prompt,
1040 as the firmware initialization takes rather long time -
1041 at least 10 seconds, which should be sufficient for
1042 the disk to expire its write cache. */
1043 if (system_state != SYSTEM_POWER_OFF) {
1044#else
1045 if (system_state == SYSTEM_RESTART) {
1046#endif
1047 ide_cacheflush_p(drive);
1048 return;
1049 }
1050
1051 printk("Shutdown: %s\n", drive->name);
Russell King4031bbe2006-01-06 11:41:00 +00001052 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055static ide_driver_t idedisk_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001057 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001058 .name = "ide-disk",
1059 .bus = &ide_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 },
Russell King4031bbe2006-01-06 11:41:00 +00001061 .probe = ide_disk_probe,
1062 .remove = ide_disk_remove,
1063 .shutdown = ide_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 .version = IDEDISK_VERSION,
1065 .media = ide_disk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 .do_request = ide_do_rw_disk,
1068 .end_request = ide_end_request,
1069 .error = __ide_error,
1070 .abort = __ide_abort,
1071 .proc = idedisk_proc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072};
1073
1074static int idedisk_open(struct inode *inode, struct file *filp)
1075{
1076 struct gendisk *disk = inode->i_bdev->bd_disk;
1077 struct ide_disk_obj *idkp;
1078 ide_drive_t *drive;
1079
1080 if (!(idkp = ide_disk_get(disk)))
1081 return -ENXIO;
1082
1083 drive = idkp->drive;
1084
1085 drive->usage++;
1086 if (drive->removable && drive->usage == 1) {
1087 ide_task_t args;
1088 memset(&args, 0, sizeof(ide_task_t));
1089 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1090 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1091 args.handler = &task_no_data_intr;
1092 check_disk_change(inode->i_bdev);
1093 /*
1094 * Ignore the return code from door_lock,
1095 * since the open() has already succeeded,
1096 * and the door_lock is irrelevant at this point.
1097 */
1098 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1099 drive->doorlocking = 0;
1100 }
1101 return 0;
1102}
1103
1104static int idedisk_release(struct inode *inode, struct file *filp)
1105{
1106 struct gendisk *disk = inode->i_bdev->bd_disk;
1107 struct ide_disk_obj *idkp = ide_disk_g(disk);
1108 ide_drive_t *drive = idkp->drive;
1109
1110 if (drive->usage == 1)
1111 ide_cacheflush_p(drive);
1112 if (drive->removable && drive->usage == 1) {
1113 ide_task_t args;
1114 memset(&args, 0, sizeof(ide_task_t));
1115 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1116 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1117 args.handler = &task_no_data_intr;
1118 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1119 drive->doorlocking = 0;
1120 }
1121 drive->usage--;
1122
1123 ide_disk_put(idkp);
1124
1125 return 0;
1126}
1127
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001128static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1129{
1130 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1131 ide_drive_t *drive = idkp->drive;
1132
1133 geo->heads = drive->bios_head;
1134 geo->sectors = drive->bios_sect;
1135 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1136 return 0;
1137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static int idedisk_ioctl(struct inode *inode, struct file *file,
1140 unsigned int cmd, unsigned long arg)
1141{
1142 struct block_device *bdev = inode->i_bdev;
1143 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1144 return generic_ide_ioctl(idkp->drive, file, bdev, cmd, arg);
1145}
1146
1147static int idedisk_media_changed(struct gendisk *disk)
1148{
1149 struct ide_disk_obj *idkp = ide_disk_g(disk);
1150 ide_drive_t *drive = idkp->drive;
1151
1152 /* do not scan partitions twice if this is a removable device */
1153 if (drive->attach) {
1154 drive->attach = 0;
1155 return 0;
1156 }
1157 /* if removable, always assume it was changed */
1158 return drive->removable;
1159}
1160
1161static int idedisk_revalidate_disk(struct gendisk *disk)
1162{
1163 struct ide_disk_obj *idkp = ide_disk_g(disk);
1164 set_capacity(disk, idedisk_capacity(idkp->drive));
1165 return 0;
1166}
1167
1168static struct block_device_operations idedisk_ops = {
1169 .owner = THIS_MODULE,
1170 .open = idedisk_open,
1171 .release = idedisk_release,
1172 .ioctl = idedisk_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001173 .getgeo = idedisk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 .media_changed = idedisk_media_changed,
1175 .revalidate_disk= idedisk_revalidate_disk
1176};
1177
1178MODULE_DESCRIPTION("ATA DISK Driver");
1179
Russell King4031bbe2006-01-06 11:41:00 +00001180static int ide_disk_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 struct ide_disk_obj *idkp;
1183 struct gendisk *g;
1184
1185 /* strstr("foo", "") is non-NULL */
1186 if (!strstr("ide-disk", drive->driver_req))
1187 goto failed;
1188 if (!drive->present)
1189 goto failed;
1190 if (drive->media != ide_disk)
1191 goto failed;
1192
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -08001193 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!idkp)
1195 goto failed;
1196
Christoph Lameter19460892005-06-23 00:08:19 -07001197 g = alloc_disk_node(1 << PARTN_BITS,
Christoph Lameter86b37862005-08-09 19:59:21 -07001198 hwif_to_node(drive->hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (!g)
1200 goto out_free_idkp;
1201
1202 ide_init_disk(g, drive);
1203
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001204 ide_register_subdriver(drive, &idedisk_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 kref_init(&idkp->kref);
1207
1208 idkp->drive = drive;
1209 idkp->driver = &idedisk_driver;
1210 idkp->disk = g;
1211
1212 g->private_data = &idkp->driver;
1213
1214 drive->driver_data = idkp;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 idedisk_setup(drive);
1217 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1218 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1219 drive->name, drive->head);
1220 drive->attach = 0;
1221 } else
1222 drive->attach = 1;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 g->minors = 1 << PARTN_BITS;
1225 strcpy(g->devfs_name, drive->devfs_name);
1226 g->driverfs_dev = &drive->gendev;
1227 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1228 set_capacity(g, idedisk_capacity(drive));
1229 g->fops = &idedisk_ops;
1230 add_disk(g);
1231 return 0;
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233out_free_idkp:
1234 kfree(idkp);
1235failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001236 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239static void __exit idedisk_exit (void)
1240{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001241 driver_unregister(&idedisk_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001244static int __init idedisk_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001246 return driver_register(&idedisk_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Kay Sievers263756e2005-12-12 18:03:44 +01001249MODULE_ALIAS("ide:*m-disk*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250module_init(idedisk_init);
1251module_exit(idedisk_exit);
1252MODULE_LICENSE("GPL");