5fc4213437a5f42330635316de0f2a70308341d1
[safe/jmp/linux-2.6] / drivers / ide / ide-disk.c
1 /*
2  *  Copyright (C) 1994-1998        Linus Torvalds & authors (see below)
3  *  Copyright (C) 1998-2002        Linux ATA Development
4  *                                    Andre Hedrick <andre@linux-ide.org>
5  *  Copyright (C) 2003             Red Hat <alan@redhat.com>
6  *  Copyright (C) 2003-2005, 2007  Bartlomiej Zolnierkiewicz
7  */
8
9 /*
10  *  Mostly written by Mark Lord <mlord@pobox.com>
11  *                and Gadi Oxman <gadio@netvision.net.il>
12  *                and Andre Hedrick <andre@linux-ide.org>
13  *
14  * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
15  */
16
17 #define IDEDISK_VERSION "1.18"
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/timer.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/major.h>
27 #include <linux/errno.h>
28 #include <linux/genhd.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/mutex.h>
32 #include <linux/leds.h>
33 #include <linux/ide.h>
34 #include <linux/hdreg.h>
35
36 #include <asm/byteorder.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
40 #include <asm/div64.h>
41
42 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
43 #define IDE_DISK_MINORS         (1 << PARTN_BITS)
44 #else
45 #define IDE_DISK_MINORS         0
46 #endif
47
48 struct ide_disk_obj {
49         ide_drive_t     *drive;
50         ide_driver_t    *driver;
51         struct gendisk  *disk;
52         struct kref     kref;
53         unsigned int    openers;        /* protected by BKL for now */
54 };
55
56 static DEFINE_MUTEX(idedisk_ref_mutex);
57
58 #define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref)
59
60 #define ide_disk_g(disk) \
61         container_of((disk)->private_data, struct ide_disk_obj, driver)
62
63 static void ide_disk_release(struct kref *);
64
65 static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
66 {
67         struct ide_disk_obj *idkp = NULL;
68
69         mutex_lock(&idedisk_ref_mutex);
70         idkp = ide_disk_g(disk);
71         if (idkp) {
72                 if (ide_device_get(idkp->drive))
73                         idkp = NULL;
74                 else
75                         kref_get(&idkp->kref);
76         }
77         mutex_unlock(&idedisk_ref_mutex);
78         return idkp;
79 }
80
81 static void ide_disk_put(struct ide_disk_obj *idkp)
82 {
83         ide_drive_t *drive = idkp->drive;
84
85         mutex_lock(&idedisk_ref_mutex);
86         kref_put(&idkp->kref, ide_disk_release);
87         ide_device_put(drive);
88         mutex_unlock(&idedisk_ref_mutex);
89 }
90
91 /*
92  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
93  * value for this drive (from its reported identification information).
94  *
95  * Returns:     1 if lba_capacity looks sensible
96  *              0 otherwise
97  *
98  * It is called only once for each drive.
99  */
100 static int lba_capacity_is_ok(u16 *id)
101 {
102         unsigned long lba_sects, chs_sects, head, tail;
103
104         /* No non-LBA info .. so valid! */
105         if (id[ATA_ID_CYLS] == 0)
106                 return 1;
107
108         lba_sects = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
109
110         /*
111          * The ATA spec tells large drives to return
112          * C/H/S = 16383/16/63 independent of their size.
113          * Some drives can be jumpered to use 15 heads instead of 16.
114          * Some drives can be jumpered to use 4092 cyls instead of 16383.
115          */
116         if ((id[ATA_ID_CYLS] == 16383 ||
117              (id[ATA_ID_CYLS] == 4092 && id[ATA_ID_CUR_CYLS] == 16383)) &&
118             id[ATA_ID_SECTORS] == 63 &&
119             (id[ATA_ID_HEADS] == 15 || id[ATA_ID_HEADS] == 16) &&
120             (lba_sects >= 16383 * 63 * id[ATA_ID_HEADS]))
121                 return 1;
122
123         chs_sects = id[ATA_ID_CYLS] * id[ATA_ID_HEADS] * id[ATA_ID_SECTORS];
124
125         /* perform a rough sanity check on lba_sects:  within 10% is OK */
126         if ((lba_sects - chs_sects) < chs_sects/10)
127                 return 1;
128
129         /* some drives have the word order reversed */
130         head = ((lba_sects >> 16) & 0xffff);
131         tail = (lba_sects & 0xffff);
132         lba_sects = (head | (tail << 16));
133         if ((lba_sects - chs_sects) < chs_sects/10) {
134                 *(__le32 *)&id[ATA_ID_LBA_CAPACITY] = __cpu_to_le32(lba_sects);
135                 return 1;       /* lba_capacity is (now) good */
136         }
137
138         return 0;       /* lba_capacity value may be bad */
139 }
140
141 static const u8 ide_rw_cmds[] = {
142         ATA_CMD_READ_MULTI,
143         ATA_CMD_WRITE_MULTI,
144         ATA_CMD_READ_MULTI_EXT,
145         ATA_CMD_WRITE_MULTI_EXT,
146         ATA_CMD_PIO_READ,
147         ATA_CMD_PIO_WRITE,
148         ATA_CMD_PIO_READ_EXT,
149         ATA_CMD_PIO_WRITE_EXT,
150         ATA_CMD_READ,
151         ATA_CMD_WRITE,
152         ATA_CMD_READ_EXT,
153         ATA_CMD_WRITE_EXT,
154 };
155
156 static const u8 ide_data_phases[] = {
157         TASKFILE_MULTI_IN,
158         TASKFILE_MULTI_OUT,
159         TASKFILE_IN,
160         TASKFILE_OUT,
161         TASKFILE_IN_DMA,
162         TASKFILE_OUT_DMA,
163 };
164
165 static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma)
166 {
167         u8 index, lba48, write;
168
169         lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
170         write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
171
172         if (dma)
173                 index = 8;
174         else
175                 index = drive->mult_count ? 0 : 4;
176
177         task->tf.command = ide_rw_cmds[index + lba48 + write];
178
179         if (dma)
180                 index = 8; /* fixup index */
181
182         task->data_phase = ide_data_phases[index / 2 + write];
183 }
184
185 /*
186  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
187  * using LBA if supported, or CHS otherwise, to address sectors.
188  */
189 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
190                                         sector_t block)
191 {
192         ide_hwif_t *hwif        = HWIF(drive);
193         unsigned int dma        = drive->using_dma;
194         u16 nsectors            = (u16)rq->nr_sectors;
195         u8 lba48                = (drive->addressing == 1) ? 1 : 0;
196         ide_task_t              task;
197         struct ide_taskfile     *tf = &task.tf;
198         ide_startstop_t         rc;
199
200         if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
201                 if (block + rq->nr_sectors > 1ULL << 28)
202                         dma = 0;
203                 else
204                         lba48 = 0;
205         }
206
207         if (!dma) {
208                 ide_init_sg_cmd(drive, rq);
209                 ide_map_sg(drive, rq);
210         }
211
212         memset(&task, 0, sizeof(task));
213         task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
214
215         if (drive->select.b.lba) {
216                 if (lba48) {
217                         pr_debug("%s: LBA=0x%012llx\n", drive->name,
218                                         (unsigned long long)block);
219
220                         tf->hob_nsect = (nsectors >> 8) & 0xff;
221                         tf->hob_lbal  = (u8)(block >> 24);
222                         if (sizeof(block) != 4) {
223                                 tf->hob_lbam = (u8)((u64)block >> 32);
224                                 tf->hob_lbah = (u8)((u64)block >> 40);
225                         }
226
227                         tf->nsect  = nsectors & 0xff;
228                         tf->lbal   = (u8) block;
229                         tf->lbam   = (u8)(block >>  8);
230                         tf->lbah   = (u8)(block >> 16);
231
232                         task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
233                 } else {
234                         tf->nsect  = nsectors & 0xff;
235                         tf->lbal   = block;
236                         tf->lbam   = block >>= 8;
237                         tf->lbah   = block >>= 8;
238                         tf->device = (block >> 8) & 0xf;
239                 }
240         } else {
241                 unsigned int sect, head, cyl, track;
242
243                 track = (int)block / drive->sect;
244                 sect  = (int)block % drive->sect + 1;
245                 head  = track % drive->head;
246                 cyl   = track / drive->head;
247
248                 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
249
250                 tf->nsect  = nsectors & 0xff;
251                 tf->lbal   = sect;
252                 tf->lbam   = cyl;
253                 tf->lbah   = cyl >> 8;
254                 tf->device = head;
255         }
256
257         if (rq_data_dir(rq))
258                 task.tf_flags |= IDE_TFLAG_WRITE;
259
260         ide_tf_set_cmd(drive, &task, dma);
261         if (!dma)
262                 hwif->data_phase = task.data_phase;
263         task.rq = rq;
264
265         rc = do_rw_taskfile(drive, &task);
266
267         if (rc == ide_stopped && dma) {
268                 /* fallback to PIO */
269                 task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
270                 ide_tf_set_cmd(drive, &task, 0);
271                 hwif->data_phase = task.data_phase;
272                 ide_init_sg_cmd(drive, rq);
273                 rc = do_rw_taskfile(drive, &task);
274         }
275
276         return rc;
277 }
278
279 /*
280  * 268435455  == 137439 MB or 28bit limit
281  * 320173056  == 163929 MB or 48bit addressing
282  * 1073741822 == 549756 MB or 48bit addressing fake drive
283  */
284
285 static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
286                                       sector_t block)
287 {
288         ide_hwif_t *hwif = HWIF(drive);
289
290         BUG_ON(drive->blocked);
291
292         if (!blk_fs_request(rq)) {
293                 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
294                 ide_end_request(drive, 0, 0);
295                 return ide_stopped;
296         }
297
298         ledtrig_ide_activity();
299
300         pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
301                  drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
302                  (unsigned long long)block, rq->nr_sectors,
303                  (unsigned long)rq->buffer);
304
305         if (hwif->rw_disk)
306                 hwif->rw_disk(drive, rq);
307
308         return __ide_do_rw_disk(drive, rq, block);
309 }
310
311 /*
312  * Queries for true maximum capacity of the drive.
313  * Returns maximum LBA address (> 0) of the drive, 0 if failed.
314  */
315 static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
316 {
317         ide_task_t args;
318         struct ide_taskfile *tf = &args.tf;
319         u64 addr = 0;
320
321         /* Create IDE/ATA command request structure */
322         memset(&args, 0, sizeof(ide_task_t));
323         if (lba48)
324                 tf->command = ATA_CMD_READ_NATIVE_MAX_EXT;
325         else
326                 tf->command = ATA_CMD_READ_NATIVE_MAX;
327         tf->device  = ATA_LBA;
328         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
329         if (lba48)
330                 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
331         /* submit command request */
332         ide_no_data_taskfile(drive, &args);
333
334         /* if OK, compute maximum address value */
335         if ((tf->status & 0x01) == 0)
336                 addr = ide_get_lba_addr(tf, lba48) + 1;
337
338         return addr;
339 }
340
341 /*
342  * Sets maximum virtual LBA address of the drive.
343  * Returns new maximum virtual LBA address (> 0) or 0 on failure.
344  */
345 static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
346 {
347         ide_task_t args;
348         struct ide_taskfile *tf = &args.tf;
349         u64 addr_set = 0;
350
351         addr_req--;
352         /* Create IDE/ATA command request structure */
353         memset(&args, 0, sizeof(ide_task_t));
354         tf->lbal     = (addr_req >>  0) & 0xff;
355         tf->lbam     = (addr_req >>= 8) & 0xff;
356         tf->lbah     = (addr_req >>= 8) & 0xff;
357         if (lba48) {
358                 tf->hob_lbal = (addr_req >>= 8) & 0xff;
359                 tf->hob_lbam = (addr_req >>= 8) & 0xff;
360                 tf->hob_lbah = (addr_req >>= 8) & 0xff;
361                 tf->command  = ATA_CMD_SET_MAX_EXT;
362         } else {
363                 tf->device   = (addr_req >>= 8) & 0x0f;
364                 tf->command  = ATA_CMD_SET_MAX;
365         }
366         tf->device |= ATA_LBA;
367         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
368         if (lba48)
369                 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
370         /* submit command request */
371         ide_no_data_taskfile(drive, &args);
372         /* if OK, compute maximum address value */
373         if ((tf->status & 0x01) == 0)
374                 addr_set = ide_get_lba_addr(tf, lba48) + 1;
375
376         return addr_set;
377 }
378
379 static unsigned long long sectors_to_MB(unsigned long long n)
380 {
381         n <<= 9;                /* make it bytes */
382         do_div(n, 1000000);     /* make it MB */
383         return n;
384 }
385
386 /*
387  * The same here.
388  */
389 static inline int idedisk_supports_lba48(const u16 *id)
390 {
391         return (id[ATA_ID_COMMAND_SET_2] & 0x0400) &&
392                (id[ATA_ID_CFS_ENABLE_2] & 0x0400) &&
393                ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
394 }
395
396 /*
397  * Some disks report total number of sectors instead of
398  * maximum sector address.  We list them here.
399  */
400 static const struct drive_list_entry hpa_list[] = {
401         { "ST340823A",  NULL },
402         { "ST320413A",  NULL },
403         { "ST310211A",  NULL },
404         { NULL,         NULL }
405 };
406
407 static void idedisk_check_hpa(ide_drive_t *drive)
408 {
409         unsigned long long capacity, set_max;
410         int lba48 = idedisk_supports_lba48(drive->id);
411
412         capacity = drive->capacity64;
413
414         set_max = idedisk_read_native_max_address(drive, lba48);
415
416         if (ide_in_drive_list(drive->id, hpa_list)) {
417                 /*
418                  * Since we are inclusive wrt to firmware revisions do this
419                  * extra check and apply the workaround only when needed.
420                  */
421                 if (set_max == capacity + 1)
422                         set_max--;
423         }
424
425         if (set_max <= capacity)
426                 return;
427
428         printk(KERN_INFO "%s: Host Protected Area detected.\n"
429                          "\tcurrent capacity is %llu sectors (%llu MB)\n"
430                          "\tnative  capacity is %llu sectors (%llu MB)\n",
431                          drive->name,
432                          capacity, sectors_to_MB(capacity),
433                          set_max, sectors_to_MB(set_max));
434
435         set_max = idedisk_set_max_address(drive, set_max, lba48);
436
437         if (set_max) {
438                 drive->capacity64 = set_max;
439                 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
440                                  drive->name);
441         }
442 }
443
444 static void init_idedisk_capacity(ide_drive_t *drive)
445 {
446         u16 *id = drive->id;
447         /*
448          * If this drive supports the Host Protected Area feature set,
449          * then we may need to change our opinion about the drive's capacity.
450          */
451         int hpa = ata_id_hpa_enabled(id);
452
453         if (idedisk_supports_lba48(id)) {
454                 /* drive speaks 48-bit LBA */
455                 drive->select.b.lba = 1;
456                 drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
457                 if (hpa)
458                         idedisk_check_hpa(drive);
459         } else if (ata_id_has_lba(id) && lba_capacity_is_ok(id)) {
460                 /* drive speaks 28-bit LBA */
461                 drive->select.b.lba = 1;
462                 drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
463                 if (hpa)
464                         idedisk_check_hpa(drive);
465         } else {
466                 /* drive speaks boring old 28-bit CHS */
467                 drive->capacity64 = drive->cyl * drive->head * drive->sect;
468         }
469 }
470
471 static sector_t idedisk_capacity(ide_drive_t *drive)
472 {
473         return drive->capacity64;
474 }
475
476 #ifdef CONFIG_IDE_PROC_FS
477 static int smart_enable(ide_drive_t *drive)
478 {
479         ide_task_t args;
480         struct ide_taskfile *tf = &args.tf;
481
482         memset(&args, 0, sizeof(ide_task_t));
483         tf->feature = ATA_SMART_ENABLE;
484         tf->lbam    = ATA_SMART_LBAM_PASS;
485         tf->lbah    = ATA_SMART_LBAH_PASS;
486         tf->command = ATA_CMD_SMART;
487         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
488         return ide_no_data_taskfile(drive, &args);
489 }
490
491 static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd)
492 {
493         ide_task_t args;
494         struct ide_taskfile *tf = &args.tf;
495
496         memset(&args, 0, sizeof(ide_task_t));
497         tf->feature = sub_cmd;
498         tf->nsect   = 0x01;
499         tf->lbam    = ATA_SMART_LBAM_PASS;
500         tf->lbah    = ATA_SMART_LBAH_PASS;
501         tf->command = ATA_CMD_SMART;
502         args.tf_flags   = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
503         args.data_phase = TASKFILE_IN;
504         (void) smart_enable(drive);
505         return ide_raw_taskfile(drive, &args, buf, 1);
506 }
507
508 static int proc_idedisk_read_cache
509         (char *page, char **start, off_t off, int count, int *eof, void *data)
510 {
511         ide_drive_t     *drive = (ide_drive_t *) data;
512         char            *out = page;
513         int             len;
514
515         if (drive->id_read)
516                 len = sprintf(out, "%i\n", drive->id[ATA_ID_BUF_SIZE] / 2);
517         else
518                 len = sprintf(out, "(none)\n");
519
520         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
521 }
522
523 static int proc_idedisk_read_capacity
524         (char *page, char **start, off_t off, int count, int *eof, void *data)
525 {
526         ide_drive_t*drive = (ide_drive_t *)data;
527         int len;
528
529         len = sprintf(page, "%llu\n", (long long)idedisk_capacity(drive));
530
531         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
532 }
533
534 static int proc_idedisk_read_smart(char *page, char **start, off_t off,
535                                    int count, int *eof, void *data, u8 sub_cmd)
536 {
537         ide_drive_t     *drive = (ide_drive_t *)data;
538         int             len = 0, i = 0;
539
540         if (get_smart_data(drive, page, sub_cmd) == 0) {
541                 unsigned short *val = (unsigned short *) page;
542                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
543                 page = out;
544                 do {
545                         out += sprintf(out, "%04x%c", le16_to_cpu(*val),
546                                        (++i & 7) ? ' ' : '\n');
547                         val += 1;
548                 } while (i < (SECTOR_WORDS * 2));
549                 len = out - page;
550         }
551
552         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
553 }
554
555 static int proc_idedisk_read_sv
556         (char *page, char **start, off_t off, int count, int *eof, void *data)
557 {
558         return proc_idedisk_read_smart(page, start, off, count, eof, data,
559                                        ATA_SMART_READ_VALUES);
560 }
561
562 static int proc_idedisk_read_st
563         (char *page, char **start, off_t off, int count, int *eof, void *data)
564 {
565         return proc_idedisk_read_smart(page, start, off, count, eof, data,
566                                        ATA_SMART_READ_THRESHOLDS);
567 }
568
569 static ide_proc_entry_t idedisk_proc[] = {
570         { "cache",        S_IFREG|S_IRUGO, proc_idedisk_read_cache,    NULL },
571         { "capacity",     S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL },
572         { "geometry",     S_IFREG|S_IRUGO, proc_ide_read_geometry,     NULL },
573         { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_sv,       NULL },
574         { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_st,   NULL },
575         { NULL, 0, NULL, NULL }
576 };
577 #endif  /* CONFIG_IDE_PROC_FS */
578
579 static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
580 {
581         ide_drive_t *drive = q->queuedata;
582         ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC);
583
584         /* FIXME: map struct ide_taskfile on rq->cmd[] */
585         BUG_ON(task == NULL);
586
587         memset(task, 0, sizeof(*task));
588         if (ide_id_has_flush_cache_ext(drive->id) &&
589             (drive->capacity64 >= (1UL << 28)))
590                 task->tf.command = ATA_CMD_FLUSH_EXT;
591         else
592                 task->tf.command = ATA_CMD_FLUSH;
593         task->tf_flags   = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
594                            IDE_TFLAG_DYN;
595         task->data_phase = TASKFILE_NO_DATA;
596
597         rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
598         rq->cmd_flags |= REQ_SOFTBARRIER;
599         rq->special = task;
600 }
601
602 /*
603  * This is tightly woven into the driver->do_special can not touch.
604  * DON'T do it again until a total personality rewrite is committed.
605  */
606 static int set_multcount(ide_drive_t *drive, int arg)
607 {
608         struct request *rq;
609         int error;
610
611         if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
612                 return -EINVAL;
613
614         if (drive->special.b.set_multmode)
615                 return -EBUSY;
616
617         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
618         rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
619
620         drive->mult_req = arg;
621         drive->special.b.set_multmode = 1;
622         error = blk_execute_rq(drive->queue, NULL, rq, 0);
623         blk_put_request(rq);
624
625         return (drive->mult_count == arg) ? 0 : -EIO;
626 }
627
628 static int set_nowerr(ide_drive_t *drive, int arg)
629 {
630         if (arg < 0 || arg > 1)
631                 return -EINVAL;
632
633         if (ide_spin_wait_hwgroup(drive))
634                 return -EBUSY;
635         drive->nowerr = arg;
636         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
637         spin_unlock_irq(&ide_lock);
638         return 0;
639 }
640
641 static void update_ordered(ide_drive_t *drive)
642 {
643         u16 *id = drive->id;
644         unsigned ordered = QUEUE_ORDERED_NONE;
645         prepare_flush_fn *prep_fn = NULL;
646
647         if (drive->wcache) {
648                 unsigned long long capacity;
649                 int barrier;
650                 /*
651                  * We must avoid issuing commands a drive does not
652                  * understand or we may crash it. We check flush cache
653                  * is supported. We also check we have the LBA48 flush
654                  * cache if the drive capacity is too large. By this
655                  * time we have trimmed the drive capacity if LBA48 is
656                  * not available so we don't need to recheck that.
657                  */
658                 capacity = idedisk_capacity(drive);
659                 barrier = ide_id_has_flush_cache(id) && !drive->noflush &&
660                         (drive->addressing == 0 || capacity <= (1ULL << 28) ||
661                          ide_id_has_flush_cache_ext(id));
662
663                 printk(KERN_INFO "%s: cache flushes %ssupported\n",
664                        drive->name, barrier ? "" : "not ");
665
666                 if (barrier) {
667                         ordered = QUEUE_ORDERED_DRAIN_FLUSH;
668                         prep_fn = idedisk_prepare_flush;
669                 }
670         } else
671                 ordered = QUEUE_ORDERED_DRAIN;
672
673         blk_queue_ordered(drive->queue, ordered, prep_fn);
674 }
675
676 static int write_cache(ide_drive_t *drive, int arg)
677 {
678         ide_task_t args;
679         int err = 1;
680
681         if (arg < 0 || arg > 1)
682                 return -EINVAL;
683
684         if (ide_id_has_flush_cache(drive->id)) {
685                 memset(&args, 0, sizeof(ide_task_t));
686                 args.tf.feature = arg ?
687                         SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
688                 args.tf.command = ATA_CMD_SET_FEATURES;
689                 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
690                 err = ide_no_data_taskfile(drive, &args);
691                 if (err == 0)
692                         drive->wcache = arg;
693         }
694
695         update_ordered(drive);
696
697         return err;
698 }
699
700 static int do_idedisk_flushcache(ide_drive_t *drive)
701 {
702         ide_task_t args;
703
704         memset(&args, 0, sizeof(ide_task_t));
705         if (ide_id_has_flush_cache_ext(drive->id))
706                 args.tf.command = ATA_CMD_FLUSH_EXT;
707         else
708                 args.tf.command = ATA_CMD_FLUSH;
709         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
710         return ide_no_data_taskfile(drive, &args);
711 }
712
713 static int set_acoustic(ide_drive_t *drive, int arg)
714 {
715         ide_task_t args;
716
717         if (arg < 0 || arg > 254)
718                 return -EINVAL;
719
720         memset(&args, 0, sizeof(ide_task_t));
721         args.tf.feature = arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF;
722         args.tf.nsect   = arg;
723         args.tf.command = ATA_CMD_SET_FEATURES;
724         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
725         ide_no_data_taskfile(drive, &args);
726         drive->acoustic = arg;
727         return 0;
728 }
729
730 /*
731  * drive->addressing:
732  *      0: 28-bit
733  *      1: 48-bit
734  *      2: 48-bit capable doing 28-bit
735  */
736 static int set_lba_addressing(ide_drive_t *drive, int arg)
737 {
738         if (arg < 0 || arg > 2)
739                 return -EINVAL;
740
741         drive->addressing =  0;
742
743         if (drive->hwif->host_flags & IDE_HFLAG_NO_LBA48)
744                 return 0;
745
746         if (!idedisk_supports_lba48(drive->id))
747                 return -EIO;
748         drive->addressing = arg;
749         return 0;
750 }
751
752 #ifdef CONFIG_IDE_PROC_FS
753 static void idedisk_add_settings(ide_drive_t *drive)
754 {
755         ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 65535, 1, 1,
756                         &drive->bios_cyl, NULL);
757         ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
758                         &drive->bios_head, NULL);
759         ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
760                         &drive->bios_sect, NULL);
761         ide_add_setting(drive, "address", SETTING_RW, TYPE_BYTE, 0, 2, 1, 1,
762                         &drive->addressing, set_lba_addressing);
763         ide_add_setting(drive, "multcount", SETTING_RW, TYPE_BYTE, 0,
764                         drive->id[ATA_ID_MAX_MULTSECT] & 0xff, 1, 1,
765                         &drive->mult_count, set_multcount);
766         ide_add_setting(drive, "nowerr", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1,
767                         &drive->nowerr, set_nowerr);
768         ide_add_setting(drive, "lun", SETTING_RW, TYPE_INT, 0, 7, 1, 1,
769                         &drive->lun, NULL);
770         ide_add_setting(drive, "wcache", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1,
771                         &drive->wcache, write_cache);
772         ide_add_setting(drive, "acoustic", SETTING_RW, TYPE_BYTE, 0, 254, 1, 1,
773                         &drive->acoustic, set_acoustic);
774         ide_add_setting(drive, "failures", SETTING_RW, TYPE_INT, 0, 65535, 1, 1,
775                         &drive->failures, NULL);
776         ide_add_setting(drive, "max_failures", SETTING_RW, TYPE_INT, 0, 65535,
777                         1, 1, &drive->max_failures, NULL);
778 }
779 #else
780 static inline void idedisk_add_settings(ide_drive_t *drive) { ; }
781 #endif
782
783 static void idedisk_setup(ide_drive_t *drive)
784 {
785         struct ide_disk_obj *idkp = drive->driver_data;
786         ide_hwif_t *hwif = drive->hwif;
787         u16 *id = drive->id;
788         char *m = (char *)&id[ATA_ID_PROD];
789         unsigned long long capacity;
790
791         ide_proc_register_driver(drive, idkp->driver);
792         idedisk_add_settings(drive);
793
794         if (drive->id_read == 0)
795                 return;
796
797         if (drive->removable) {
798                 /*
799                  * Removable disks (eg. SYQUEST); ignore 'WD' drives
800                  */
801                 if (m[0] != 'W' || m[1] != 'D')
802                         drive->doorlocking = 1;
803         }
804
805         (void)set_lba_addressing(drive, 1);
806
807         if (drive->addressing == 1) {
808                 int max_s = 2048;
809
810                 if (max_s > hwif->rqsize)
811                         max_s = hwif->rqsize;
812
813                 blk_queue_max_sectors(drive->queue, max_s);
814         }
815
816         printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
817                          drive->queue->max_sectors / 2);
818
819         /* calculate drive capacity, and select LBA if possible */
820         init_idedisk_capacity(drive);
821
822         /* limit drive capacity to 137GB if LBA48 cannot be used */
823         if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
824                 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
825                        "%llu sectors (%llu MB)\n",
826                        drive->name, (unsigned long long)drive->capacity64,
827                        sectors_to_MB(drive->capacity64));
828                 drive->capacity64 = 1ULL << 28;
829         }
830
831         if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && drive->addressing) {
832                 if (drive->capacity64 > 1ULL << 28) {
833                         printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode"
834                                          " will be used for accessing sectors "
835                                          "> %u\n", drive->name, 1 << 28);
836                 } else
837                         drive->addressing = 0;
838         }
839
840         /*
841          * if possible, give fdisk access to more of the drive,
842          * by correcting bios_cyls:
843          */
844         capacity = idedisk_capacity(drive);
845
846         if (!drive->forced_geom) {
847
848                 if (idedisk_supports_lba48(drive->id)) {
849                         /* compatibility */
850                         drive->bios_sect = 63;
851                         drive->bios_head = 255;
852                 }
853
854                 if (drive->bios_sect && drive->bios_head) {
855                         unsigned int cap0 = capacity; /* truncate to 32 bits */
856                         unsigned int cylsz, cyl;
857
858                         if (cap0 != capacity)
859                                 drive->bios_cyl = 65535;
860                         else {
861                                 cylsz = drive->bios_sect * drive->bios_head;
862                                 cyl = cap0 / cylsz;
863                                 if (cyl > 65535)
864                                         cyl = 65535;
865                                 if (cyl > drive->bios_cyl)
866                                         drive->bios_cyl = cyl;
867                         }
868                 }
869         }
870         printk(KERN_INFO "%s: %llu sectors (%llu MB)",
871                          drive->name, capacity, sectors_to_MB(capacity));
872
873         /* Only print cache size when it was specified */
874         if (id[ATA_ID_BUF_SIZE])
875                 printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2);
876
877         printk(KERN_CONT ", CHS=%d/%d/%d\n",
878                          drive->bios_cyl, drive->bios_head, drive->bios_sect);
879
880         /* write cache enabled? */
881         if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
882                 drive->wcache = 1;
883
884         write_cache(drive, 1);
885 }
886
887 static void ide_cacheflush_p(ide_drive_t *drive)
888 {
889         if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
890                 return;
891
892         if (do_idedisk_flushcache(drive))
893                 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
894 }
895
896 static void ide_disk_remove(ide_drive_t *drive)
897 {
898         struct ide_disk_obj *idkp = drive->driver_data;
899         struct gendisk *g = idkp->disk;
900
901         ide_proc_unregister_driver(drive, idkp->driver);
902
903         del_gendisk(g);
904
905         ide_cacheflush_p(drive);
906
907         ide_disk_put(idkp);
908 }
909
910 static void ide_disk_release(struct kref *kref)
911 {
912         struct ide_disk_obj *idkp = to_ide_disk(kref);
913         ide_drive_t *drive = idkp->drive;
914         struct gendisk *g = idkp->disk;
915
916         drive->driver_data = NULL;
917         g->private_data = NULL;
918         put_disk(g);
919         kfree(idkp);
920 }
921
922 static int ide_disk_probe(ide_drive_t *drive);
923
924 /*
925  * On HPA drives the capacity needs to be
926  * reinitilized on resume otherwise the disk
927  * can not be used and a hard reset is required
928  */
929 static void ide_disk_resume(ide_drive_t *drive)
930 {
931         if (ata_id_hpa_enabled(drive->id))
932                 init_idedisk_capacity(drive);
933 }
934
935 static void ide_device_shutdown(ide_drive_t *drive)
936 {
937 #ifdef  CONFIG_ALPHA
938         /* On Alpha, halt(8) doesn't actually turn the machine off,
939            it puts you into the sort of firmware monitor. Typically,
940            it's used to boot another kernel image, so it's not much
941            different from reboot(8). Therefore, we don't need to
942            spin down the disk in this case, especially since Alpha
943            firmware doesn't handle disks in standby mode properly.
944            On the other hand, it's reasonably safe to turn the power
945            off when the shutdown process reaches the firmware prompt,
946            as the firmware initialization takes rather long time -
947            at least 10 seconds, which should be sufficient for
948            the disk to expire its write cache. */
949         if (system_state != SYSTEM_POWER_OFF) {
950 #else
951         if (system_state == SYSTEM_RESTART) {
952 #endif
953                 ide_cacheflush_p(drive);
954                 return;
955         }
956
957         printk(KERN_INFO "Shutdown: %s\n", drive->name);
958
959         drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
960 }
961
962 static ide_driver_t idedisk_driver = {
963         .gen_driver = {
964                 .owner          = THIS_MODULE,
965                 .name           = "ide-disk",
966                 .bus            = &ide_bus_type,
967         },
968         .probe                  = ide_disk_probe,
969         .remove                 = ide_disk_remove,
970         .resume                 = ide_disk_resume,
971         .shutdown               = ide_device_shutdown,
972         .version                = IDEDISK_VERSION,
973         .media                  = ide_disk,
974         .supports_dsc_overlap   = 0,
975         .do_request             = ide_do_rw_disk,
976         .end_request            = ide_end_request,
977         .error                  = __ide_error,
978 #ifdef CONFIG_IDE_PROC_FS
979         .proc                   = idedisk_proc,
980 #endif
981 };
982
983 static int idedisk_set_doorlock(ide_drive_t *drive, int on)
984 {
985         ide_task_t task;
986
987         memset(&task, 0, sizeof(task));
988         task.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
989         task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
990
991         return ide_no_data_taskfile(drive, &task);
992 }
993
994 static int idedisk_open(struct inode *inode, struct file *filp)
995 {
996         struct gendisk *disk = inode->i_bdev->bd_disk;
997         struct ide_disk_obj *idkp;
998         ide_drive_t *drive;
999
1000         idkp = ide_disk_get(disk);
1001         if (idkp == NULL)
1002                 return -ENXIO;
1003
1004         drive = idkp->drive;
1005
1006         idkp->openers++;
1007
1008         if (drive->removable && idkp->openers == 1) {
1009                 check_disk_change(inode->i_bdev);
1010                 /*
1011                  * Ignore the return code from door_lock,
1012                  * since the open() has already succeeded,
1013                  * and the door_lock is irrelevant at this point.
1014                  */
1015                 if (drive->doorlocking && idedisk_set_doorlock(drive, 1))
1016                         drive->doorlocking = 0;
1017         }
1018         return 0;
1019 }
1020
1021 static int idedisk_release(struct inode *inode, struct file *filp)
1022 {
1023         struct gendisk *disk = inode->i_bdev->bd_disk;
1024         struct ide_disk_obj *idkp = ide_disk_g(disk);
1025         ide_drive_t *drive = idkp->drive;
1026
1027         if (idkp->openers == 1)
1028                 ide_cacheflush_p(drive);
1029
1030         if (drive->removable && idkp->openers == 1) {
1031                 if (drive->doorlocking && idedisk_set_doorlock(drive, 0))
1032                         drive->doorlocking = 0;
1033         }
1034
1035         idkp->openers--;
1036
1037         ide_disk_put(idkp);
1038
1039         return 0;
1040 }
1041
1042 static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1043 {
1044         struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1045         ide_drive_t *drive = idkp->drive;
1046
1047         geo->heads = drive->bios_head;
1048         geo->sectors = drive->bios_sect;
1049         geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1050         return 0;
1051 }
1052
1053 static int idedisk_ioctl(struct inode *inode, struct file *file,
1054                         unsigned int cmd, unsigned long arg)
1055 {
1056         unsigned long flags;
1057         struct block_device *bdev = inode->i_bdev;
1058         struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1059         ide_drive_t *drive = idkp->drive;
1060         int err, (*setfunc)(ide_drive_t *, int);
1061         u8 *val;
1062
1063         switch (cmd) {
1064         case HDIO_GET_ADDRESS:   val = &drive->addressing;      goto read_val;
1065         case HDIO_GET_MULTCOUNT: val = &drive->mult_count;      goto read_val;
1066         case HDIO_GET_NOWERR:    val = &drive->nowerr;          goto read_val;
1067         case HDIO_GET_WCACHE:    val = &drive->wcache;          goto read_val;
1068         case HDIO_GET_ACOUSTIC:  val = &drive->acoustic;        goto read_val;
1069         case HDIO_SET_ADDRESS:   setfunc = set_lba_addressing;  goto set_val;
1070         case HDIO_SET_MULTCOUNT: setfunc = set_multcount;       goto set_val;
1071         case HDIO_SET_NOWERR:    setfunc = set_nowerr;          goto set_val;
1072         case HDIO_SET_WCACHE:    setfunc = write_cache;         goto set_val;
1073         case HDIO_SET_ACOUSTIC:  setfunc = set_acoustic;        goto set_val;
1074         }
1075
1076         return generic_ide_ioctl(drive, file, bdev, cmd, arg);
1077
1078 read_val:
1079         mutex_lock(&ide_setting_mtx);
1080         spin_lock_irqsave(&ide_lock, flags);
1081         err = *val;
1082         spin_unlock_irqrestore(&ide_lock, flags);
1083         mutex_unlock(&ide_setting_mtx);
1084         return err >= 0 ? put_user(err, (long __user *)arg) : err;
1085
1086 set_val:
1087         if (bdev != bdev->bd_contains)
1088                 err = -EINVAL;
1089         else {
1090                 if (!capable(CAP_SYS_ADMIN))
1091                         err = -EACCES;
1092                 else {
1093                         mutex_lock(&ide_setting_mtx);
1094                         err = setfunc(drive, arg);
1095                         mutex_unlock(&ide_setting_mtx);
1096                 }
1097         }
1098         return err;
1099 }
1100
1101 static int idedisk_media_changed(struct gendisk *disk)
1102 {
1103         struct ide_disk_obj *idkp = ide_disk_g(disk);
1104         ide_drive_t *drive = idkp->drive;
1105
1106         /* do not scan partitions twice if this is a removable device */
1107         if (drive->attach) {
1108                 drive->attach = 0;
1109                 return 0;
1110         }
1111         /* if removable, always assume it was changed */
1112         return drive->removable;
1113 }
1114
1115 static int idedisk_revalidate_disk(struct gendisk *disk)
1116 {
1117         struct ide_disk_obj *idkp = ide_disk_g(disk);
1118         set_capacity(disk, idedisk_capacity(idkp->drive));
1119         return 0;
1120 }
1121
1122 static struct block_device_operations idedisk_ops = {
1123         .owner                  = THIS_MODULE,
1124         .open                   = idedisk_open,
1125         .release                = idedisk_release,
1126         .ioctl                  = idedisk_ioctl,
1127         .getgeo                 = idedisk_getgeo,
1128         .media_changed          = idedisk_media_changed,
1129         .revalidate_disk        = idedisk_revalidate_disk
1130 };
1131
1132 MODULE_DESCRIPTION("ATA DISK Driver");
1133
1134 static int ide_disk_probe(ide_drive_t *drive)
1135 {
1136         struct ide_disk_obj *idkp;
1137         struct gendisk *g;
1138
1139         /* strstr("foo", "") is non-NULL */
1140         if (!strstr("ide-disk", drive->driver_req))
1141                 goto failed;
1142
1143         if (drive->media != ide_disk)
1144                 goto failed;
1145
1146         idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
1147         if (!idkp)
1148                 goto failed;
1149
1150         g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
1151         if (!g)
1152                 goto out_free_idkp;
1153
1154         ide_init_disk(g, drive);
1155
1156         kref_init(&idkp->kref);
1157
1158         idkp->drive = drive;
1159         idkp->driver = &idedisk_driver;
1160         idkp->disk = g;
1161
1162         g->private_data = &idkp->driver;
1163
1164         drive->driver_data = idkp;
1165
1166         idedisk_setup(drive);
1167         if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1168                 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1169                         drive->name, drive->head);
1170                 drive->attach = 0;
1171         } else
1172                 drive->attach = 1;
1173
1174         g->minors = IDE_DISK_MINORS;
1175         g->driverfs_dev = &drive->gendev;
1176         g->flags |= GENHD_FL_EXT_DEVT;
1177         if (drive->removable)
1178                 g->flags |= GENHD_FL_REMOVABLE;
1179         set_capacity(g, idedisk_capacity(drive));
1180         g->fops = &idedisk_ops;
1181         add_disk(g);
1182         return 0;
1183
1184 out_free_idkp:
1185         kfree(idkp);
1186 failed:
1187         return -ENODEV;
1188 }
1189
1190 static void __exit idedisk_exit(void)
1191 {
1192         driver_unregister(&idedisk_driver.gen_driver);
1193 }
1194
1195 static int __init idedisk_init(void)
1196 {
1197         return driver_register(&idedisk_driver.gen_driver);
1198 }
1199
1200 MODULE_ALIAS("ide:*m-disk*");
1201 module_init(idedisk_init);
1202 module_exit(idedisk_exit);
1203 MODULE_LICENSE("GPL");