]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/ide/ide-disk.c
ide: add /sys/bus/ide/devices/*/{model,firmware,serial} sysfs entries
[mirror_ubuntu-bionic-kernel.git] / drivers / ide / ide-disk.c
CommitLineData
1da177e4
LT
1/*
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
d6e05edc 40 * Version 1.17 do flush on standby, do flush on ATA < ATA6
1da177e4
LT
41 * fix wcache setup.
42 */
43
44#define IDEDISK_VERSION "1.18"
45
1da177e4
LT
46//#define DEBUG
47
1da177e4
LT
48#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>
cf8b8975 60#include <linux/mutex.h>
2bfb646c 61#include <linux/leds.h>
1da177e4
LT
62
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;
c94964a4 78 unsigned int openers; /* protected by BKL for now */
1da177e4
LT
79};
80
cf8b8975 81static DEFINE_MUTEX(idedisk_ref_mutex);
1da177e4
LT
82
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
cf8b8975 92 mutex_lock(&idedisk_ref_mutex);
1da177e4
LT
93 idkp = ide_disk_g(disk);
94 if (idkp)
95 kref_get(&idkp->kref);
cf8b8975 96 mutex_unlock(&idedisk_ref_mutex);
1da177e4
LT
97 return idkp;
98}
99
100static void ide_disk_release(struct kref *);
101
102static void ide_disk_put(struct ide_disk_obj *idkp)
103{
cf8b8975 104 mutex_lock(&idedisk_ref_mutex);
1da177e4 105 kref_put(&idkp->kref, ide_disk_release);
cf8b8975 106 mutex_unlock(&idedisk_ref_mutex);
1da177e4
LT
107}
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
6efd9360
AC
122 /* No non-LBA info .. so valid! */
123 if (id->cyls == 0)
124 return 1;
125
1da177e4
LT
126 /*
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
238e4f14 172 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
1da177e4
LT
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
c2f8311d
MR
193 pr_debug("%s: LBA=0x%012llx\n", drive->name,
194 (unsigned long long)block);
1da177e4
LT
195
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
2bfb646c
RP
319 ledtrig_ide_activity();
320
1da177e4
LT
321 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
322 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
c2f8311d
MR
323 (unsigned long long)block, rq->nr_sectors,
324 (unsigned long)rq->buffer);
1da177e4
LT
325
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
b0244a00
BZ
484/*
485 * Some disks report total number of sectors instead of
486 * maximum sector address. We list them here.
487 */
488static const struct drive_list_entry hpa_list[] = {
489 { "ST340823A", NULL },
7062cdc5 490 { "ST320413A", NULL },
b0244a00
BZ
491 { NULL, NULL }
492};
493
858119e1 494static void idedisk_check_hpa(ide_drive_t *drive)
1da177e4
LT
495{
496 unsigned long long capacity, set_max;
497 int lba48 = idedisk_supports_lba48(drive->id);
498
499 capacity = drive->capacity64;
500 if (lba48)
501 set_max = idedisk_read_native_max_address_ext(drive);
502 else
503 set_max = idedisk_read_native_max_address(drive);
504
b0244a00
BZ
505 if (ide_in_drive_list(drive->id, hpa_list)) {
506 /*
507 * Since we are inclusive wrt to firmware revisions do this
508 * extra check and apply the workaround only when needed.
509 */
510 if (set_max == capacity + 1)
511 set_max--;
512 }
513
1da177e4
LT
514 if (set_max <= capacity)
515 return;
516
517 printk(KERN_INFO "%s: Host Protected Area detected.\n"
518 "\tcurrent capacity is %llu sectors (%llu MB)\n"
519 "\tnative capacity is %llu sectors (%llu MB)\n",
520 drive->name,
521 capacity, sectors_to_MB(capacity),
522 set_max, sectors_to_MB(set_max));
523
524 if (lba48)
525 set_max = idedisk_set_max_address_ext(drive, set_max);
526 else
527 set_max = idedisk_set_max_address(drive, set_max);
528 if (set_max) {
529 drive->capacity64 = set_max;
530 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
531 drive->name);
532 }
533}
534
535/*
536 * Compute drive->capacity, the full capacity of the drive
537 * Called with drive->id != NULL.
538 *
539 * To compute capacity, this uses either of
540 *
541 * 1. CHS value set by user (whatever user sets will be trusted)
542 * 2. LBA value from target drive (require new ATA feature)
543 * 3. LBA value from system BIOS (new one is OK, old one may break)
544 * 4. CHS value from system BIOS (traditional style)
545 *
546 * in above order (i.e., if value of higher priority is available,
547 * reset will be ignored).
548 */
549static void init_idedisk_capacity (ide_drive_t *drive)
550{
551 struct hd_driveid *id = drive->id;
552 /*
553 * If this drive supports the Host Protected Area feature set,
554 * then we may need to change our opinion about the drive's capacity.
555 */
556 int hpa = idedisk_supports_hpa(id);
557
558 if (idedisk_supports_lba48(id)) {
559 /* drive speaks 48-bit LBA */
560 drive->select.b.lba = 1;
561 drive->capacity64 = id->lba_capacity_2;
562 if (hpa)
563 idedisk_check_hpa(drive);
564 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
565 /* drive speaks 28-bit LBA */
566 drive->select.b.lba = 1;
567 drive->capacity64 = id->lba_capacity;
568 if (hpa)
569 idedisk_check_hpa(drive);
570 } else {
571 /* drive speaks boring old 28-bit CHS */
572 drive->capacity64 = drive->cyl * drive->head * drive->sect;
573 }
574}
575
576static sector_t idedisk_capacity (ide_drive_t *drive)
577{
578 return drive->capacity64 - drive->sect0;
579}
580
ecfd80e4 581#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
582static int smart_enable(ide_drive_t *drive)
583{
584 ide_task_t args;
585
586 memset(&args, 0, sizeof(ide_task_t));
587 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
588 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
589 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
590 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
591 args.command_type = IDE_DRIVE_TASK_NO_DATA;
592 args.handler = &task_no_data_intr;
593 return ide_raw_taskfile(drive, &args, NULL);
594}
595
43e7c0c4 596static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd)
1da177e4
LT
597{
598 ide_task_t args;
599
600 memset(&args, 0, sizeof(ide_task_t));
43e7c0c4 601 args.tfRegister[IDE_FEATURE_OFFSET] = sub_cmd;
1da177e4
LT
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
43e7c0c4 643 if (get_smart_data(drive, page, SMART_READ_THRESHOLDS) == 0) {
1da177e4
LT
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
43e7c0c4 662 if (get_smart_data(drive, page, SMART_READ_VALUES) == 0) {
1da177e4
LT
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};
ecfd80e4 683#endif /* CONFIG_IDE_PROC_FS */
1da177e4 684
165125e1 685static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
1da177e4
LT
686{
687 ide_drive_t *drive = q->queuedata;
688
1da177e4
LT
689 memset(rq->cmd, 0, sizeof(rq->cmd));
690
691 if (ide_id_has_flush_cache_ext(drive->id) &&
692 (drive->capacity64 >= (1UL << 28)))
693 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
694 else
695 rq->cmd[0] = WIN_FLUSH_CACHE;
696
697
4aff5e23
JA
698 rq->cmd_type = REQ_TYPE_ATA_TASK;
699 rq->cmd_flags |= REQ_SOFTBARRIER;
1da177e4 700 rq->buffer = rq->cmd;
1da177e4
LT
701}
702
1da177e4
LT
703/*
704 * This is tightly woven into the driver->do_special can not touch.
705 * DON'T do it again until a total personality rewrite is committed.
706 */
707static int set_multcount(ide_drive_t *drive, int arg)
708{
709 struct request rq;
710
1497943e
BZ
711 if (arg < 0 || arg > drive->id->max_multsect)
712 return -EINVAL;
713
1da177e4
LT
714 if (drive->special.b.set_multmode)
715 return -EBUSY;
716 ide_init_drive_cmd (&rq);
4aff5e23 717 rq.cmd_type = REQ_TYPE_ATA_CMD;
1da177e4
LT
718 drive->mult_req = arg;
719 drive->special.b.set_multmode = 1;
720 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
721 return (drive->mult_count == arg) ? 0 : -EIO;
722}
723
724static int set_nowerr(ide_drive_t *drive, int arg)
725{
1497943e
BZ
726 if (arg < 0 || arg > 1)
727 return -EINVAL;
728
1da177e4
LT
729 if (ide_spin_wait_hwgroup(drive))
730 return -EBUSY;
731 drive->nowerr = arg;
732 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
733 spin_unlock_irq(&ide_lock);
734 return 0;
735}
736
3e087b57
TH
737static void update_ordered(ide_drive_t *drive)
738{
739 struct hd_driveid *id = drive->id;
740 unsigned ordered = QUEUE_ORDERED_NONE;
741 prepare_flush_fn *prep_fn = NULL;
3e087b57
TH
742
743 if (drive->wcache) {
744 unsigned long long capacity;
745 int barrier;
746 /*
747 * We must avoid issuing commands a drive does not
748 * understand or we may crash it. We check flush cache
749 * is supported. We also check we have the LBA48 flush
750 * cache if the drive capacity is too large. By this
751 * time we have trimmed the drive capacity if LBA48 is
752 * not available so we don't need to recheck that.
753 */
754 capacity = idedisk_capacity(drive);
36193484 755 barrier = ide_id_has_flush_cache(id) && !drive->noflush &&
3e087b57
TH
756 (drive->addressing == 0 || capacity <= (1ULL << 28) ||
757 ide_id_has_flush_cache_ext(id));
758
759 printk(KERN_INFO "%s: cache flushes %ssupported\n",
f7ad836c 760 drive->name, barrier ? "" : "not ");
3e087b57
TH
761
762 if (barrier) {
763 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
764 prep_fn = idedisk_prepare_flush;
3e087b57
TH
765 }
766 } else
767 ordered = QUEUE_ORDERED_DRAIN;
768
769 blk_queue_ordered(drive->queue, ordered, prep_fn);
3e087b57
TH
770}
771
1da177e4
LT
772static int write_cache(ide_drive_t *drive, int arg)
773{
774 ide_task_t args;
3e087b57 775 int err = 1;
1da177e4 776
1497943e
BZ
777 if (arg < 0 || arg > 1)
778 return -EINVAL;
779
3e087b57
TH
780 if (ide_id_has_flush_cache(drive->id)) {
781 memset(&args, 0, sizeof(ide_task_t));
782 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
1da177e4 783 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
3e087b57
TH
784 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
785 args.command_type = IDE_DRIVE_TASK_NO_DATA;
786 args.handler = &task_no_data_intr;
787 err = ide_raw_taskfile(drive, &args, NULL);
788 if (err == 0)
789 drive->wcache = arg;
790 }
1da177e4 791
3e087b57 792 update_ordered(drive);
1da177e4 793
3e087b57 794 return err;
1da177e4
LT
795}
796
797static int do_idedisk_flushcache (ide_drive_t *drive)
798{
799 ide_task_t args;
800
801 memset(&args, 0, sizeof(ide_task_t));
802 if (ide_id_has_flush_cache_ext(drive->id))
803 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
804 else
805 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
806 args.command_type = IDE_DRIVE_TASK_NO_DATA;
807 args.handler = &task_no_data_intr;
808 return ide_raw_taskfile(drive, &args, NULL);
809}
810
811static int set_acoustic (ide_drive_t *drive, int arg)
812{
813 ide_task_t args;
814
1497943e
BZ
815 if (arg < 0 || arg > 254)
816 return -EINVAL;
817
1da177e4
LT
818 memset(&args, 0, sizeof(ide_task_t));
819 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
820 SETFEATURES_DIS_AAM;
821 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
822 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
823 args.command_type = IDE_DRIVE_TASK_NO_DATA;
824 args.handler = &task_no_data_intr;
825 ide_raw_taskfile(drive, &args, NULL);
826 drive->acoustic = arg;
827 return 0;
828}
829
830/*
831 * drive->addressing:
832 * 0: 28-bit
833 * 1: 48-bit
834 * 2: 48-bit capable doing 28-bit
835 */
836static int set_lba_addressing(ide_drive_t *drive, int arg)
837{
1497943e
BZ
838 if (arg < 0 || arg > 2)
839 return -EINVAL;
840
1da177e4
LT
841 drive->addressing = 0;
842
238e4f14 843 if (drive->hwif->host_flags & IDE_HFLAG_NO_LBA48)
1da177e4
LT
844 return 0;
845
846 if (!idedisk_supports_lba48(drive->id))
847 return -EIO;
848 drive->addressing = arg;
849 return 0;
850}
851
7662d046 852#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
853static void idedisk_add_settings(ide_drive_t *drive)
854{
855 struct hd_driveid *id = drive->id;
856
1497943e
BZ
857 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
858 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
859 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
860 ide_add_setting(drive, "address", SETTING_RW, TYPE_BYTE, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
861 ide_add_setting(drive, "bswap", SETTING_READ, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
862 ide_add_setting(drive, "multcount", SETTING_RW, TYPE_BYTE, 0, id->max_multsect, 1, 1, &drive->mult_count, set_multcount);
863 ide_add_setting(drive, "nowerr", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
864 ide_add_setting(drive, "lun", SETTING_RW, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
865 ide_add_setting(drive, "wcache", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
866 ide_add_setting(drive, "acoustic", SETTING_RW, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
867 ide_add_setting(drive, "failures", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
868 ide_add_setting(drive, "max_failures", SETTING_RW, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1da177e4 869}
7662d046
BZ
870#else
871static inline void idedisk_add_settings(ide_drive_t *drive) { ; }
872#endif
1da177e4
LT
873
874static void idedisk_setup (ide_drive_t *drive)
875{
238e4f14 876 ide_hwif_t *hwif = drive->hwif;
1da177e4
LT
877 struct hd_driveid *id = drive->id;
878 unsigned long long capacity;
1da177e4
LT
879
880 idedisk_add_settings(drive);
881
882 if (drive->id_read == 0)
883 return;
884
98109337 885 if (drive->removable) {
1da177e4
LT
886 /*
887 * Removable disks (eg. SYQUEST); ignore 'WD' drives
888 */
889 if (id->model[0] != 'W' || id->model[1] != 'D') {
890 drive->doorlocking = 1;
891 }
892 }
893
894 (void)set_lba_addressing(drive, 1);
895
896 if (drive->addressing == 1) {
1da177e4
LT
897 int max_s = 2048;
898
899 if (max_s > hwif->rqsize)
900 max_s = hwif->rqsize;
901
902 blk_queue_max_sectors(drive->queue, max_s);
903 }
904
905 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
906
907 /* calculate drive capacity, and select LBA if possible */
908 init_idedisk_capacity (drive);
909
910 /* limit drive capacity to 137GB if LBA48 cannot be used */
911 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
912 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
913 "%llu sectors (%llu MB)\n",
914 drive->name, (unsigned long long)drive->capacity64,
915 sectors_to_MB(drive->capacity64));
916 drive->capacity64 = 1ULL << 28;
917 }
918
238e4f14 919 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && drive->addressing) {
1da177e4
LT
920 if (drive->capacity64 > 1ULL << 28) {
921 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
922 " be used for accessing sectors > %u\n",
923 drive->name, 1 << 28);
924 } else
925 drive->addressing = 0;
926 }
927
928 /*
929 * if possible, give fdisk access to more of the drive,
930 * by correcting bios_cyls:
931 */
932 capacity = idedisk_capacity (drive);
933 if (!drive->forced_geom) {
934
935 if (idedisk_supports_lba48(drive->id)) {
936 /* compatibility */
937 drive->bios_sect = 63;
938 drive->bios_head = 255;
939 }
940
941 if (drive->bios_sect && drive->bios_head) {
942 unsigned int cap0 = capacity; /* truncate to 32 bits */
943 unsigned int cylsz, cyl;
944
945 if (cap0 != capacity)
946 drive->bios_cyl = 65535;
947 else {
948 cylsz = drive->bios_sect * drive->bios_head;
949 cyl = cap0 / cylsz;
950 if (cyl > 65535)
951 cyl = 65535;
952 if (cyl > drive->bios_cyl)
953 drive->bios_cyl = cyl;
954 }
955 }
956 }
957 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
958 drive->name, capacity, sectors_to_MB(capacity));
959
960 /* Only print cache size when it was specified */
961 if (id->buf_size)
962 printk (" w/%dKiB Cache", id->buf_size/2);
963
964 printk(", CHS=%d/%d/%d",
965 drive->bios_cyl, drive->bios_head, drive->bios_sect);
966 if (drive->using_dma)
967 ide_dma_verbose(drive);
968 printk("\n");
969
1da177e4
LT
970 /* write cache enabled? */
971 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
972 drive->wcache = 1;
973
974 write_cache(drive, 1);
1da177e4
LT
975}
976
977static void ide_cacheflush_p(ide_drive_t *drive)
978{
979 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
980 return;
981
982 if (do_idedisk_flushcache(drive))
983 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
984}
985
4031bbe4 986static void ide_disk_remove(ide_drive_t *drive)
1da177e4
LT
987{
988 struct ide_disk_obj *idkp = drive->driver_data;
989 struct gendisk *g = idkp->disk;
990
7662d046 991 ide_proc_unregister_driver(drive, idkp->driver);
8604affd 992
1da177e4
LT
993 del_gendisk(g);
994
d36fef6f
BZ
995 ide_cacheflush_p(drive);
996
1da177e4 997 ide_disk_put(idkp);
1da177e4
LT
998}
999
1000static void ide_disk_release(struct kref *kref)
1001{
1002 struct ide_disk_obj *idkp = to_ide_disk(kref);
1003 ide_drive_t *drive = idkp->drive;
1004 struct gendisk *g = idkp->disk;
1005
1006 drive->driver_data = NULL;
1da177e4
LT
1007 g->private_data = NULL;
1008 put_disk(g);
1009 kfree(idkp);
1010}
1011
4031bbe4 1012static int ide_disk_probe(ide_drive_t *drive);
1da177e4 1013
0d2157f7
LT
1014/*
1015 * On HPA drives the capacity needs to be
1016 * reinitilized on resume otherwise the disk
1017 * can not be used and a hard reset is required
1018 */
1019static void ide_disk_resume(ide_drive_t *drive)
1020{
1021 if (idedisk_supports_hpa(drive->id))
1022 init_idedisk_capacity(drive);
1023}
1024
4031bbe4 1025static void ide_device_shutdown(ide_drive_t *drive)
1da177e4 1026{
1da177e4
LT
1027#ifdef CONFIG_ALPHA
1028 /* On Alpha, halt(8) doesn't actually turn the machine off,
1029 it puts you into the sort of firmware monitor. Typically,
1030 it's used to boot another kernel image, so it's not much
1031 different from reboot(8). Therefore, we don't need to
1032 spin down the disk in this case, especially since Alpha
1033 firmware doesn't handle disks in standby mode properly.
1034 On the other hand, it's reasonably safe to turn the power
1035 off when the shutdown process reaches the firmware prompt,
1036 as the firmware initialization takes rather long time -
1037 at least 10 seconds, which should be sufficient for
1038 the disk to expire its write cache. */
1039 if (system_state != SYSTEM_POWER_OFF) {
1040#else
1041 if (system_state == SYSTEM_RESTART) {
1042#endif
1043 ide_cacheflush_p(drive);
1044 return;
1045 }
1046
1047 printk("Shutdown: %s\n", drive->name);
4031bbe4 1048 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
1da177e4
LT
1049}
1050
1da177e4 1051static ide_driver_t idedisk_driver = {
1da177e4 1052 .gen_driver = {
4ef3b8f4 1053 .owner = THIS_MODULE,
8604affd
BZ
1054 .name = "ide-disk",
1055 .bus = &ide_bus_type,
1da177e4 1056 },
4031bbe4
RK
1057 .probe = ide_disk_probe,
1058 .remove = ide_disk_remove,
0d2157f7 1059 .resume = ide_disk_resume,
4031bbe4 1060 .shutdown = ide_device_shutdown,
1da177e4
LT
1061 .version = IDEDISK_VERSION,
1062 .media = ide_disk,
1da177e4 1063 .supports_dsc_overlap = 0,
1da177e4
LT
1064 .do_request = ide_do_rw_disk,
1065 .end_request = ide_end_request,
1066 .error = __ide_error,
1067 .abort = __ide_abort,
7662d046 1068#ifdef CONFIG_IDE_PROC_FS
1da177e4 1069 .proc = idedisk_proc,
7662d046 1070#endif
1da177e4
LT
1071};
1072
1073static int idedisk_open(struct inode *inode, struct file *filp)
1074{
1075 struct gendisk *disk = inode->i_bdev->bd_disk;
1076 struct ide_disk_obj *idkp;
1077 ide_drive_t *drive;
1078
1079 if (!(idkp = ide_disk_get(disk)))
1080 return -ENXIO;
1081
1082 drive = idkp->drive;
1083
c94964a4
BZ
1084 idkp->openers++;
1085
1086 if (drive->removable && idkp->openers == 1) {
1da177e4
LT
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
c94964a4 1110 if (idkp->openers == 1)
1da177e4 1111 ide_cacheflush_p(drive);
c94964a4
BZ
1112
1113 if (drive->removable && idkp->openers == 1) {
1da177e4
LT
1114 ide_task_t args;
1115 memset(&args, 0, sizeof(ide_task_t));
1116 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1117 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1118 args.handler = &task_no_data_intr;
1119 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1120 drive->doorlocking = 0;
1121 }
c94964a4
BZ
1122
1123 idkp->openers--;
1da177e4
LT
1124
1125 ide_disk_put(idkp);
1126
1127 return 0;
1128}
1129
a885c8c4
CH
1130static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1131{
1132 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1133 ide_drive_t *drive = idkp->drive;
1134
1135 geo->heads = drive->bios_head;
1136 geo->sectors = drive->bios_sect;
1137 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1138 return 0;
1139}
1140
1da177e4
LT
1141static int idedisk_ioctl(struct inode *inode, struct file *file,
1142 unsigned int cmd, unsigned long arg)
1143{
1497943e 1144 unsigned long flags;
1da177e4
LT
1145 struct block_device *bdev = inode->i_bdev;
1146 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1497943e
BZ
1147 ide_drive_t *drive = idkp->drive;
1148 int err, (*setfunc)(ide_drive_t *, int);
1149 u8 *val;
1150
1151 switch (cmd) {
1152 case HDIO_GET_ADDRESS: val = &drive->addressing; goto read_val;
1153 case HDIO_GET_MULTCOUNT: val = &drive->mult_count; goto read_val;
1154 case HDIO_GET_NOWERR: val = &drive->nowerr; goto read_val;
1155 case HDIO_GET_WCACHE: val = &drive->wcache; goto read_val;
1156 case HDIO_GET_ACOUSTIC: val = &drive->acoustic; goto read_val;
1157 case HDIO_SET_ADDRESS: setfunc = set_lba_addressing; goto set_val;
1158 case HDIO_SET_MULTCOUNT: setfunc = set_multcount; goto set_val;
1159 case HDIO_SET_NOWERR: setfunc = set_nowerr; goto set_val;
1160 case HDIO_SET_WCACHE: setfunc = write_cache; goto set_val;
1161 case HDIO_SET_ACOUSTIC: setfunc = set_acoustic; goto set_val;
1162 }
1163
1164 return generic_ide_ioctl(drive, file, bdev, cmd, arg);
1165
1166read_val:
f9383c42 1167 mutex_lock(&ide_setting_mtx);
1497943e
BZ
1168 spin_lock_irqsave(&ide_lock, flags);
1169 err = *val;
1170 spin_unlock_irqrestore(&ide_lock, flags);
f9383c42 1171 mutex_unlock(&ide_setting_mtx);
1497943e
BZ
1172 return err >= 0 ? put_user(err, (long __user *)arg) : err;
1173
1174set_val:
1175 if (bdev != bdev->bd_contains)
1176 err = -EINVAL;
1177 else {
1178 if (!capable(CAP_SYS_ADMIN))
1179 err = -EACCES;
1180 else {
f9383c42 1181 mutex_lock(&ide_setting_mtx);
1497943e 1182 err = setfunc(drive, arg);
f9383c42 1183 mutex_unlock(&ide_setting_mtx);
1497943e
BZ
1184 }
1185 }
1186 return err;
1da177e4
LT
1187}
1188
1189static int idedisk_media_changed(struct gendisk *disk)
1190{
1191 struct ide_disk_obj *idkp = ide_disk_g(disk);
1192 ide_drive_t *drive = idkp->drive;
1193
1194 /* do not scan partitions twice if this is a removable device */
1195 if (drive->attach) {
1196 drive->attach = 0;
1197 return 0;
1198 }
1199 /* if removable, always assume it was changed */
1200 return drive->removable;
1201}
1202
1203static int idedisk_revalidate_disk(struct gendisk *disk)
1204{
1205 struct ide_disk_obj *idkp = ide_disk_g(disk);
1206 set_capacity(disk, idedisk_capacity(idkp->drive));
1207 return 0;
1208}
1209
1210static struct block_device_operations idedisk_ops = {
1211 .owner = THIS_MODULE,
1212 .open = idedisk_open,
1213 .release = idedisk_release,
1214 .ioctl = idedisk_ioctl,
a885c8c4 1215 .getgeo = idedisk_getgeo,
1da177e4
LT
1216 .media_changed = idedisk_media_changed,
1217 .revalidate_disk= idedisk_revalidate_disk
1218};
1219
1220MODULE_DESCRIPTION("ATA DISK Driver");
1221
4031bbe4 1222static int ide_disk_probe(ide_drive_t *drive)
1da177e4
LT
1223{
1224 struct ide_disk_obj *idkp;
1225 struct gendisk *g;
1226
1227 /* strstr("foo", "") is non-NULL */
1228 if (!strstr("ide-disk", drive->driver_req))
1229 goto failed;
1230 if (!drive->present)
1231 goto failed;
1232 if (drive->media != ide_disk)
1233 goto failed;
1234
f5e3c2fa 1235 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
1da177e4
LT
1236 if (!idkp)
1237 goto failed;
1238
1946089a 1239 g = alloc_disk_node(1 << PARTN_BITS,
86b37860 1240 hwif_to_node(drive->hwif));
1da177e4
LT
1241 if (!g)
1242 goto out_free_idkp;
1243
1244 ide_init_disk(g, drive);
1245
7662d046 1246 ide_proc_register_driver(drive, &idedisk_driver);
1da177e4 1247
1da177e4
LT
1248 kref_init(&idkp->kref);
1249
1250 idkp->drive = drive;
1251 idkp->driver = &idedisk_driver;
1252 idkp->disk = g;
1253
1254 g->private_data = &idkp->driver;
1255
1256 drive->driver_data = idkp;
1257
1da177e4
LT
1258 idedisk_setup(drive);
1259 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1260 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1261 drive->name, drive->head);
1262 drive->attach = 0;
1263 } else
1264 drive->attach = 1;
8604affd 1265
1da177e4 1266 g->minors = 1 << PARTN_BITS;
1da177e4
LT
1267 g->driverfs_dev = &drive->gendev;
1268 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1269 set_capacity(g, idedisk_capacity(drive));
1270 g->fops = &idedisk_ops;
1271 add_disk(g);
1272 return 0;
1273
1da177e4
LT
1274out_free_idkp:
1275 kfree(idkp);
1276failed:
8604affd 1277 return -ENODEV;
1da177e4
LT
1278}
1279
1280static void __exit idedisk_exit (void)
1281{
8604affd 1282 driver_unregister(&idedisk_driver.gen_driver);
1da177e4
LT
1283}
1284
17514e8a 1285static int __init idedisk_init(void)
1da177e4 1286{
8604affd 1287 return driver_register(&idedisk_driver.gen_driver);
1da177e4
LT
1288}
1289
263756ec 1290MODULE_ALIAS("ide:*m-disk*");
1da177e4
LT
1291module_init(idedisk_init);
1292module_exit(idedisk_exit);
1293MODULE_LICENSE("GPL");