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