32d8ee281d56b9f8db91861ec6c6da4c51953af8
[firefly-linux-kernel-4.4.55.git] / drivers / ide / ide.c
1 /*
2  *  Copyright (C) 1994-1998         Linus Torvalds & authors (see below)
3  *  Copyrifht (C) 2003-2005, 2007   Bartlomiej Zolnierkiewicz
4  */
5
6 /*
7  *  Mostly written by Mark Lord  <mlord@pobox.com>
8  *                and Gadi Oxman <gadio@netvision.net.il>
9  *                and Andre Hedrick <andre@linux-ide.org>
10  *
11  *  See linux/MAINTAINERS for address of current maintainer.
12  *
13  * This is the multiple IDE interface driver, as evolved from hd.c.
14  * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
15  *   (usually 14 & 15).
16  * There can be up to two drives per interface, as per the ATA-2 spec.
17  *
18  * ...
19  *
20  *  From hd.c:
21  *  |
22  *  | It traverses the request-list, using interrupts to jump between functions.
23  *  | As nearly all functions can be called within interrupts, we may not sleep.
24  *  | Special care is recommended.  Have Fun!
25  *  |
26  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
27  *  |
28  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
29  *  | in the early extended-partition checks and added DM partitions.
30  *  |
31  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
32  *  |
33  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
34  *  | and general streamlining by Mark Lord (mlord@pobox.com).
35  *
36  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
37  *
38  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
39  *      Delman Lee      (delman@ieee.org)               ("Mr. atdisk2")
40  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
41  *
42  *  This was a rewrite of just about everything from hd.c, though some original
43  *  code is still sprinkled about.  Think of it as a major evolution, with
44  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
45  */
46
47 #define _IDE_C                  /* Tell ide.h it's really us */
48
49 #include <linux/module.h>
50 #include <linux/types.h>
51 #include <linux/string.h>
52 #include <linux/kernel.h>
53 #include <linux/timer.h>
54 #include <linux/mm.h>
55 #include <linux/interrupt.h>
56 #include <linux/major.h>
57 #include <linux/errno.h>
58 #include <linux/genhd.h>
59 #include <linux/blkpg.h>
60 #include <linux/slab.h>
61 #include <linux/init.h>
62 #include <linux/pci.h>
63 #include <linux/delay.h>
64 #include <linux/ide.h>
65 #include <linux/completion.h>
66 #include <linux/reboot.h>
67 #include <linux/cdrom.h>
68 #include <linux/seq_file.h>
69 #include <linux/device.h>
70 #include <linux/bitops.h>
71
72 #include <asm/byteorder.h>
73 #include <asm/irq.h>
74 #include <asm/uaccess.h>
75 #include <asm/io.h>
76
77
78 /* default maximum number of failures */
79 #define IDE_DEFAULT_MAX_FAILURES        1
80
81 struct class *ide_port_class;
82
83 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
84                                         IDE2_MAJOR, IDE3_MAJOR,
85                                         IDE4_MAJOR, IDE5_MAJOR,
86                                         IDE6_MAJOR, IDE7_MAJOR,
87                                         IDE8_MAJOR, IDE9_MAJOR };
88
89 DEFINE_MUTEX(ide_cfg_mtx);
90
91 __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
92 EXPORT_SYMBOL(ide_lock);
93
94 ide_hwif_t ide_hwifs[MAX_HWIFS];        /* master data repository */
95
96 static void ide_port_init_devices_data(ide_hwif_t *);
97
98 /*
99  * Do not even *think* about calling this!
100  */
101 void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
102 {
103         /* bulk initialize hwif & drive info with zeros */
104         memset(hwif, 0, sizeof(ide_hwif_t));
105
106         /* fill in any non-zero initial values */
107         hwif->index     = index;
108         hwif->major     = ide_hwif_to_major[index];
109
110         hwif->name[0]   = 'i';
111         hwif->name[1]   = 'd';
112         hwif->name[2]   = 'e';
113         hwif->name[3]   = '0' + index;
114
115         hwif->bus_state = BUSSTATE_ON;
116
117         init_completion(&hwif->gendev_rel_comp);
118
119         default_hwif_iops(hwif);
120         default_hwif_transport(hwif);
121
122         ide_port_init_devices_data(hwif);
123 }
124
125 static void ide_port_init_devices_data(ide_hwif_t *hwif)
126 {
127         int unit;
128
129         for (unit = 0; unit < MAX_DRIVES; ++unit) {
130                 ide_drive_t *drive = &hwif->drives[unit];
131                 u8 j = (hwif->index * MAX_DRIVES) + unit;
132
133                 memset(drive, 0, sizeof(*drive));
134
135                 drive->media                    = ide_disk;
136                 drive->select.all               = (unit<<4)|0xa0;
137                 drive->hwif                     = hwif;
138                 drive->ready_stat               = READY_STAT;
139                 drive->bad_wstat                = BAD_W_STAT;
140                 drive->special.b.recalibrate    = 1;
141                 drive->special.b.set_geometry   = 1;
142                 drive->name[0]                  = 'h';
143                 drive->name[1]                  = 'd';
144                 drive->name[2]                  = 'a' + j;
145                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
146
147                 INIT_LIST_HEAD(&drive->list);
148                 init_completion(&drive->gendev_rel_comp);
149         }
150 }
151
152 void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
153 {
154         ide_hwgroup_t *hwgroup = hwif->hwgroup;
155
156         spin_lock_irq(&ide_lock);
157         /*
158          * Remove us from the hwgroup, and free
159          * the hwgroup if we were the only member
160          */
161         if (hwif->next == hwif) {
162                 BUG_ON(hwgroup->hwif != hwif);
163                 kfree(hwgroup);
164         } else {
165                 /* There is another interface in hwgroup.
166                  * Unlink us, and set hwgroup->drive and ->hwif to
167                  * something sane.
168                  */
169                 ide_hwif_t *g = hwgroup->hwif;
170
171                 while (g->next != hwif)
172                         g = g->next;
173                 g->next = hwif->next;
174                 if (hwgroup->hwif == hwif) {
175                         /* Chose a random hwif for hwgroup->hwif.
176                          * It's guaranteed that there are no drives
177                          * left in the hwgroup.
178                          */
179                         BUG_ON(hwgroup->drive != NULL);
180                         hwgroup->hwif = g;
181                 }
182                 BUG_ON(hwgroup->hwif == hwif);
183         }
184         spin_unlock_irq(&ide_lock);
185 }
186
187 /* Called with ide_lock held. */
188 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
189 {
190         int i;
191
192         for (i = 0; i < MAX_DRIVES; i++) {
193                 ide_drive_t *drive = &hwif->drives[i];
194
195                 if (drive->present) {
196                         spin_unlock_irq(&ide_lock);
197                         device_unregister(&drive->gendev);
198                         wait_for_completion(&drive->gendev_rel_comp);
199                         spin_lock_irq(&ide_lock);
200                 }
201         }
202 }
203
204 void ide_port_unregister_devices(ide_hwif_t *hwif)
205 {
206         mutex_lock(&ide_cfg_mtx);
207         spin_lock_irq(&ide_lock);
208         __ide_port_unregister_devices(hwif);
209         hwif->present = 0;
210         ide_port_init_devices_data(hwif);
211         spin_unlock_irq(&ide_lock);
212         mutex_unlock(&ide_cfg_mtx);
213 }
214 EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
215
216 /**
217  *      ide_unregister          -       free an IDE interface
218  *      @hwif: IDE interface
219  *
220  *      Perform the final unregister of an IDE interface. At the moment
221  *      we don't refcount interfaces so this will also get split up.
222  *
223  *      Locking:
224  *      The caller must not hold the IDE locks
225  *      The drive present/vanishing is not yet properly locked
226  *      Take care with the callbacks. These have been split to avoid
227  *      deadlocking the IDE layer. The shutdown callback is called
228  *      before we take the lock and free resources. It is up to the
229  *      caller to be sure there is no pending I/O here, and that
230  *      the interface will not be reopened (present/vanishing locking
231  *      isn't yet done BTW). After we commit to the final kill we
232  *      call the cleanup callback with the ide locks held.
233  *
234  *      Unregister restores the hwif structures to the default state.
235  *      This is raving bonkers.
236  */
237
238 void ide_unregister(ide_hwif_t *hwif)
239 {
240         ide_hwif_t *g;
241         ide_hwgroup_t *hwgroup;
242         int irq_count = 0;
243
244         BUG_ON(in_interrupt());
245         BUG_ON(irqs_disabled());
246
247         mutex_lock(&ide_cfg_mtx);
248
249         spin_lock_irq(&ide_lock);
250         if (hwif->present) {
251                 __ide_port_unregister_devices(hwif);
252                 hwif->present = 0;
253         }
254         spin_unlock_irq(&ide_lock);
255
256         ide_proc_unregister_port(hwif);
257
258         hwgroup = hwif->hwgroup;
259         /*
260          * free the irq if we were the only hwif using it
261          */
262         g = hwgroup->hwif;
263         do {
264                 if (g->irq == hwif->irq)
265                         ++irq_count;
266                 g = g->next;
267         } while (g != hwgroup->hwif);
268         if (irq_count == 1)
269                 free_irq(hwif->irq, hwgroup);
270
271         ide_remove_port_from_hwgroup(hwif);
272
273         device_unregister(hwif->portdev);
274         device_unregister(&hwif->gendev);
275         wait_for_completion(&hwif->gendev_rel_comp);
276
277         /*
278          * Remove us from the kernel's knowledge
279          */
280         blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
281         kfree(hwif->sg_table);
282         unregister_blkdev(hwif->major, hwif->name);
283
284         if (hwif->dma_base)
285                 ide_release_dma_engine(hwif);
286
287         spin_lock_irq(&ide_lock);
288         /* restore hwif data to pristine status */
289         ide_init_port_data(hwif, hwif->index);
290         spin_unlock_irq(&ide_lock);
291
292         mutex_unlock(&ide_cfg_mtx);
293 }
294
295 EXPORT_SYMBOL(ide_unregister);
296
297 void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
298 {
299         memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
300         hwif->irq = hw->irq;
301         hwif->chipset = hw->chipset;
302         hwif->gendev.parent = hw->dev;
303         hwif->ack_intr = hw->ack_intr;
304 }
305 EXPORT_SYMBOL_GPL(ide_init_port_hw);
306
307 /*
308  *      Locks for IDE setting functionality
309  */
310
311 DEFINE_MUTEX(ide_setting_mtx);
312
313 EXPORT_SYMBOL_GPL(ide_setting_mtx);
314
315 /**
316  *      ide_spin_wait_hwgroup   -       wait for group
317  *      @drive: drive in the group
318  *
319  *      Wait for an IDE device group to go non busy and then return
320  *      holding the ide_lock which guards the hwgroup->busy status
321  *      and right to use it.
322  */
323
324 int ide_spin_wait_hwgroup (ide_drive_t *drive)
325 {
326         ide_hwgroup_t *hwgroup = HWGROUP(drive);
327         unsigned long timeout = jiffies + (3 * HZ);
328
329         spin_lock_irq(&ide_lock);
330
331         while (hwgroup->busy) {
332                 unsigned long lflags;
333                 spin_unlock_irq(&ide_lock);
334                 local_irq_set(lflags);
335                 if (time_after(jiffies, timeout)) {
336                         local_irq_restore(lflags);
337                         printk(KERN_ERR "%s: channel busy\n", drive->name);
338                         return -EBUSY;
339                 }
340                 local_irq_restore(lflags);
341                 spin_lock_irq(&ide_lock);
342         }
343         return 0;
344 }
345
346 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
347
348 int set_io_32bit(ide_drive_t *drive, int arg)
349 {
350         if (drive->no_io_32bit)
351                 return -EPERM;
352
353         if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
354                 return -EINVAL;
355
356         if (ide_spin_wait_hwgroup(drive))
357                 return -EBUSY;
358
359         drive->io_32bit = arg;
360
361         spin_unlock_irq(&ide_lock);
362
363         return 0;
364 }
365
366 static int set_ksettings(ide_drive_t *drive, int arg)
367 {
368         if (arg < 0 || arg > 1)
369                 return -EINVAL;
370
371         if (ide_spin_wait_hwgroup(drive))
372                 return -EBUSY;
373         drive->keep_settings = arg;
374         spin_unlock_irq(&ide_lock);
375
376         return 0;
377 }
378
379 int set_using_dma(ide_drive_t *drive, int arg)
380 {
381 #ifdef CONFIG_BLK_DEV_IDEDMA
382         ide_hwif_t *hwif = drive->hwif;
383         int err = -EPERM;
384
385         if (arg < 0 || arg > 1)
386                 return -EINVAL;
387
388         if (!drive->id || !(drive->id->capability & 1))
389                 goto out;
390
391         if (hwif->dma_ops == NULL)
392                 goto out;
393
394         err = -EBUSY;
395         if (ide_spin_wait_hwgroup(drive))
396                 goto out;
397         /*
398          * set ->busy flag, unlock and let it ride
399          */
400         hwif->hwgroup->busy = 1;
401         spin_unlock_irq(&ide_lock);
402
403         err = 0;
404
405         if (arg) {
406                 if (ide_set_dma(drive))
407                         err = -EIO;
408         } else
409                 ide_dma_off(drive);
410
411         /*
412          * lock, clear ->busy flag and unlock before leaving
413          */
414         spin_lock_irq(&ide_lock);
415         hwif->hwgroup->busy = 0;
416         spin_unlock_irq(&ide_lock);
417 out:
418         return err;
419 #else
420         if (arg < 0 || arg > 1)
421                 return -EINVAL;
422
423         return -EPERM;
424 #endif
425 }
426
427 int set_pio_mode(ide_drive_t *drive, int arg)
428 {
429         struct request *rq;
430         ide_hwif_t *hwif = drive->hwif;
431         const struct ide_port_ops *port_ops = hwif->port_ops;
432
433         if (arg < 0 || arg > 255)
434                 return -EINVAL;
435
436         if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
437             (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
438                 return -ENOSYS;
439
440         if (drive->special.b.set_tune)
441                 return -EBUSY;
442
443         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
444         rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
445
446         drive->tune_req = (u8) arg;
447         drive->special.b.set_tune = 1;
448
449         blk_execute_rq(drive->queue, NULL, rq, 0);
450         blk_put_request(rq);
451
452         return 0;
453 }
454
455 static int set_unmaskirq(ide_drive_t *drive, int arg)
456 {
457         if (drive->no_unmask)
458                 return -EPERM;
459
460         if (arg < 0 || arg > 1)
461                 return -EINVAL;
462
463         if (ide_spin_wait_hwgroup(drive))
464                 return -EBUSY;
465         drive->unmask = arg;
466         spin_unlock_irq(&ide_lock);
467
468         return 0;
469 }
470
471 static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
472 {
473         ide_drive_t *drive = dev->driver_data;
474         ide_hwif_t *hwif = HWIF(drive);
475         struct request *rq;
476         struct request_pm_state rqpm;
477         ide_task_t args;
478         int ret;
479
480         /* Call ACPI _GTM only once */
481         if (!(drive->dn % 2))
482                 ide_acpi_get_timing(hwif);
483
484         memset(&rqpm, 0, sizeof(rqpm));
485         memset(&args, 0, sizeof(args));
486         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
487         rq->cmd_type = REQ_TYPE_PM_SUSPEND;
488         rq->special = &args;
489         rq->data = &rqpm;
490         rqpm.pm_step = ide_pm_state_start_suspend;
491         if (mesg.event == PM_EVENT_PRETHAW)
492                 mesg.event = PM_EVENT_FREEZE;
493         rqpm.pm_state = mesg.event;
494
495         ret = blk_execute_rq(drive->queue, NULL, rq, 0);
496         blk_put_request(rq);
497         /* only call ACPI _PS3 after both drivers are suspended */
498         if (!ret && (((drive->dn % 2) && hwif->drives[0].present
499                  && hwif->drives[1].present)
500                  || !hwif->drives[0].present
501                  || !hwif->drives[1].present))
502                 ide_acpi_set_state(hwif, 0);
503         return ret;
504 }
505
506 static int generic_ide_resume(struct device *dev)
507 {
508         ide_drive_t *drive = dev->driver_data;
509         ide_hwif_t *hwif = HWIF(drive);
510         struct request *rq;
511         struct request_pm_state rqpm;
512         ide_task_t args;
513         int err;
514
515         /* Call ACPI _STM only once */
516         if (!(drive->dn % 2)) {
517                 ide_acpi_set_state(hwif, 1);
518                 ide_acpi_push_timing(hwif);
519         }
520
521         ide_acpi_exec_tfs(drive);
522
523         memset(&rqpm, 0, sizeof(rqpm));
524         memset(&args, 0, sizeof(args));
525         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
526         rq->cmd_type = REQ_TYPE_PM_RESUME;
527         rq->cmd_flags |= REQ_PREEMPT;
528         rq->special = &args;
529         rq->data = &rqpm;
530         rqpm.pm_step = ide_pm_state_start_resume;
531         rqpm.pm_state = PM_EVENT_ON;
532
533         err = blk_execute_rq(drive->queue, NULL, rq, 1);
534         blk_put_request(rq);
535
536         if (err == 0 && dev->driver) {
537                 ide_driver_t *drv = to_ide_driver(dev->driver);
538
539                 if (drv->resume)
540                         drv->resume(drive);
541         }
542
543         return err;
544 }
545
546 int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
547                         unsigned int cmd, unsigned long arg)
548 {
549         unsigned long flags;
550         ide_driver_t *drv;
551         void __user *p = (void __user *)arg;
552         int err = 0, (*setfunc)(ide_drive_t *, int);
553         u8 *val;
554
555         switch (cmd) {
556         case HDIO_GET_32BIT:        val = &drive->io_32bit;      goto read_val;
557         case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
558         case HDIO_GET_UNMASKINTR:   val = &drive->unmask;        goto read_val;
559         case HDIO_GET_DMA:          val = &drive->using_dma;     goto read_val;
560         case HDIO_SET_32BIT:        setfunc = set_io_32bit;      goto set_val;
561         case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings;     goto set_val;
562         case HDIO_SET_PIO_MODE:     setfunc = set_pio_mode;      goto set_val;
563         case HDIO_SET_UNMASKINTR:   setfunc = set_unmaskirq;     goto set_val;
564         case HDIO_SET_DMA:          setfunc = set_using_dma;     goto set_val;
565         }
566
567         switch (cmd) {
568                 case HDIO_OBSOLETE_IDENTITY:
569                 case HDIO_GET_IDENTITY:
570                         if (bdev != bdev->bd_contains)
571                                 return -EINVAL;
572                         if (drive->id_read == 0)
573                                 return -ENOMSG;
574                         if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
575                                 return -EFAULT;
576                         return 0;
577
578                 case HDIO_GET_NICE:
579                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
580                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
581                                         drive->nice1 << IDE_NICE_1,
582                                         (long __user *) arg);
583 #ifdef CONFIG_IDE_TASK_IOCTL
584                 case HDIO_DRIVE_TASKFILE:
585                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
586                                 return -EACCES;
587                         switch(drive->media) {
588                                 case ide_disk:
589                                         return ide_taskfile_ioctl(drive, cmd, arg);
590                                 default:
591                                         return -ENOMSG;
592                         }
593 #endif /* CONFIG_IDE_TASK_IOCTL */
594
595                 case HDIO_DRIVE_CMD:
596                         if (!capable(CAP_SYS_RAWIO))
597                                 return -EACCES;
598                         return ide_cmd_ioctl(drive, cmd, arg);
599
600                 case HDIO_DRIVE_TASK:
601                         if (!capable(CAP_SYS_RAWIO))
602                                 return -EACCES;
603                         return ide_task_ioctl(drive, cmd, arg);
604                 case HDIO_SET_NICE:
605                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
606                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
607                                 return -EPERM;
608                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
609                         drv = *(ide_driver_t **)bdev->bd_disk->private_data;
610                         if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
611                                 drive->dsc_overlap = 0;
612                                 return -EPERM;
613                         }
614                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
615                         return 0;
616                 case HDIO_DRIVE_RESET:
617                         if (!capable(CAP_SYS_ADMIN))
618                                 return -EACCES;
619
620                         /*
621                          *      Abort the current command on the
622                          *      group if there is one, taking
623                          *      care not to allow anything else
624                          *      to be queued and to die on the
625                          *      spot if we miss one somehow
626                          */
627
628                         spin_lock_irqsave(&ide_lock, flags);
629
630                         if (HWGROUP(drive)->resetting) {
631                                 spin_unlock_irqrestore(&ide_lock, flags);
632                                 return -EBUSY;
633                         }
634
635                         ide_abort(drive, "drive reset");
636
637                         BUG_ON(HWGROUP(drive)->handler);
638
639                         /* Ensure nothing gets queued after we
640                            drop the lock. Reset will clear the busy */
641
642                         HWGROUP(drive)->busy = 1;
643                         spin_unlock_irqrestore(&ide_lock, flags);
644                         (void) ide_do_reset(drive);
645
646                         return 0;
647                 case HDIO_GET_BUSSTATE:
648                         if (!capable(CAP_SYS_ADMIN))
649                                 return -EACCES;
650                         if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
651                                 return -EFAULT;
652                         return 0;
653
654                 case HDIO_SET_BUSSTATE:
655                         if (!capable(CAP_SYS_ADMIN))
656                                 return -EACCES;
657                         return -EOPNOTSUPP;
658                 default:
659                         return -EINVAL;
660         }
661
662 read_val:
663         mutex_lock(&ide_setting_mtx);
664         spin_lock_irqsave(&ide_lock, flags);
665         err = *val;
666         spin_unlock_irqrestore(&ide_lock, flags);
667         mutex_unlock(&ide_setting_mtx);
668         return err >= 0 ? put_user(err, (long __user *)arg) : err;
669
670 set_val:
671         if (bdev != bdev->bd_contains)
672                 err = -EINVAL;
673         else {
674                 if (!capable(CAP_SYS_ADMIN))
675                         err = -EACCES;
676                 else {
677                         mutex_lock(&ide_setting_mtx);
678                         err = setfunc(drive, arg);
679                         mutex_unlock(&ide_setting_mtx);
680                 }
681         }
682         return err;
683 }
684
685 EXPORT_SYMBOL(generic_ide_ioctl);
686
687 static int ide_bus_match(struct device *dev, struct device_driver *drv)
688 {
689         return 1;
690 }
691
692 static char *media_string(ide_drive_t *drive)
693 {
694         switch (drive->media) {
695         case ide_disk:
696                 return "disk";
697         case ide_cdrom:
698                 return "cdrom";
699         case ide_tape:
700                 return "tape";
701         case ide_floppy:
702                 return "floppy";
703         case ide_optical:
704                 return "optical";
705         default:
706                 return "UNKNOWN";
707         }
708 }
709
710 static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
711 {
712         ide_drive_t *drive = to_ide_device(dev);
713         return sprintf(buf, "%s\n", media_string(drive));
714 }
715
716 static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
717 {
718         ide_drive_t *drive = to_ide_device(dev);
719         return sprintf(buf, "%s\n", drive->name);
720 }
721
722 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
723 {
724         ide_drive_t *drive = to_ide_device(dev);
725         return sprintf(buf, "ide:m-%s\n", media_string(drive));
726 }
727
728 static ssize_t model_show(struct device *dev, struct device_attribute *attr,
729                           char *buf)
730 {
731         ide_drive_t *drive = to_ide_device(dev);
732         return sprintf(buf, "%s\n", drive->id->model);
733 }
734
735 static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
736                              char *buf)
737 {
738         ide_drive_t *drive = to_ide_device(dev);
739         return sprintf(buf, "%s\n", drive->id->fw_rev);
740 }
741
742 static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
743                            char *buf)
744 {
745         ide_drive_t *drive = to_ide_device(dev);
746         return sprintf(buf, "%s\n", drive->id->serial_no);
747 }
748
749 static struct device_attribute ide_dev_attrs[] = {
750         __ATTR_RO(media),
751         __ATTR_RO(drivename),
752         __ATTR_RO(modalias),
753         __ATTR_RO(model),
754         __ATTR_RO(firmware),
755         __ATTR(serial, 0400, serial_show, NULL),
756         __ATTR_NULL
757 };
758
759 static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
760 {
761         ide_drive_t *drive = to_ide_device(dev);
762
763         add_uevent_var(env, "MEDIA=%s", media_string(drive));
764         add_uevent_var(env, "DRIVENAME=%s", drive->name);
765         add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
766         return 0;
767 }
768
769 static int generic_ide_probe(struct device *dev)
770 {
771         ide_drive_t *drive = to_ide_device(dev);
772         ide_driver_t *drv = to_ide_driver(dev->driver);
773
774         return drv->probe ? drv->probe(drive) : -ENODEV;
775 }
776
777 static int generic_ide_remove(struct device *dev)
778 {
779         ide_drive_t *drive = to_ide_device(dev);
780         ide_driver_t *drv = to_ide_driver(dev->driver);
781
782         if (drv->remove)
783                 drv->remove(drive);
784
785         return 0;
786 }
787
788 static void generic_ide_shutdown(struct device *dev)
789 {
790         ide_drive_t *drive = to_ide_device(dev);
791         ide_driver_t *drv = to_ide_driver(dev->driver);
792
793         if (dev->driver && drv->shutdown)
794                 drv->shutdown(drive);
795 }
796
797 struct bus_type ide_bus_type = {
798         .name           = "ide",
799         .match          = ide_bus_match,
800         .uevent         = ide_uevent,
801         .probe          = generic_ide_probe,
802         .remove         = generic_ide_remove,
803         .shutdown       = generic_ide_shutdown,
804         .dev_attrs      = ide_dev_attrs,
805         .suspend        = generic_ide_suspend,
806         .resume         = generic_ide_resume,
807 };
808
809 EXPORT_SYMBOL_GPL(ide_bus_type);
810
811 int ide_vlb_clk;
812 EXPORT_SYMBOL_GPL(ide_vlb_clk);
813
814 module_param_named(vlb_clock, ide_vlb_clk, int, 0);
815 MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
816
817 int ide_pci_clk;
818 EXPORT_SYMBOL_GPL(ide_pci_clk);
819
820 module_param_named(pci_clock, ide_pci_clk, int, 0);
821 MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
822
823 static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
824 {
825         int a, b, i, j = 1;
826         unsigned int *dev_param_mask = (unsigned int *)kp->arg;
827
828         if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
829             sscanf(s, "%d.%d", &a, &b) != 2)
830                 return -EINVAL;
831
832         i = a * MAX_DRIVES + b;
833
834         if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
835                 return -EINVAL;
836
837         if (j)
838                 *dev_param_mask |= (1 << i);
839         else
840                 *dev_param_mask &= (1 << i);
841
842         return 0;
843 }
844
845 static unsigned int ide_nodma;
846
847 module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
848 MODULE_PARM_DESC(nodma, "disallow DMA for a device");
849
850 static unsigned int ide_noflush;
851
852 module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
853 MODULE_PARM_DESC(noflush, "disable flush requests for a device");
854
855 static unsigned int ide_noprobe;
856
857 module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
858 MODULE_PARM_DESC(noprobe, "skip probing for a device");
859
860 static unsigned int ide_nowerr;
861
862 module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
863 MODULE_PARM_DESC(nowerr, "ignore the WRERR_STAT bit for a device");
864
865 static unsigned int ide_cdroms;
866
867 module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
868 MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
869
870 struct chs_geom {
871         unsigned int    cyl;
872         u8              head;
873         u8              sect;
874 };
875
876 static unsigned int ide_disks;
877 static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
878
879 static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
880 {
881         int a, b, c = 0, h = 0, s = 0, i, j = 1;
882
883         if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
884             sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
885                 return -EINVAL;
886
887         i = a * MAX_DRIVES + b;
888
889         if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
890                 return -EINVAL;
891
892         if (c > INT_MAX || h > 255 || s > 255)
893                 return -EINVAL;
894
895         if (j)
896                 ide_disks |= (1 << i);
897         else
898                 ide_disks &= (1 << i);
899
900         ide_disks_chs[i].cyl  = c;
901         ide_disks_chs[i].head = h;
902         ide_disks_chs[i].sect = s;
903
904         return 0;
905 }
906
907 module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
908 MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
909
910 static void ide_dev_apply_params(ide_drive_t *drive)
911 {
912         int i = drive->hwif->index * MAX_DRIVES + drive->select.b.unit;
913
914         if (ide_nodma & (1 << i)) {
915                 printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
916                 drive->nodma = 1;
917         }
918         if (ide_noflush & (1 << i)) {
919                 printk(KERN_INFO "ide: disabling flush requests for %s\n",
920                                  drive->name);
921                 drive->noflush = 1;
922         }
923         if (ide_noprobe & (1 << i)) {
924                 printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
925                 drive->noprobe = 1;
926         }
927         if (ide_nowerr & (1 << i)) {
928                 printk(KERN_INFO "ide: ignoring the WRERR_STAT bit for %s\n",
929                                  drive->name);
930                 drive->bad_wstat = BAD_R_STAT;
931         }
932         if (ide_cdroms & (1 << i)) {
933                 printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
934                 drive->present = 1;
935                 drive->media = ide_cdrom;
936                 /* an ATAPI device ignores DRDY */
937                 drive->ready_stat = 0;
938         }
939         if (ide_disks & (1 << i)) {
940                 drive->cyl  = drive->bios_cyl  = ide_disks_chs[i].cyl;
941                 drive->head = drive->bios_head = ide_disks_chs[i].head;
942                 drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
943                 drive->forced_geom = 1;
944                 printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
945                                  drive->name,
946                                  drive->cyl, drive->head, drive->sect);
947                 drive->present = 1;
948                 drive->media = ide_disk;
949                 drive->ready_stat = READY_STAT;
950         }
951 }
952
953 static unsigned int ide_ignore_cable;
954
955 static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
956 {
957         int i, j = 1;
958
959         if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
960                 return -EINVAL;
961
962         if (i >= MAX_HWIFS || j < 0 || j > 1)
963                 return -EINVAL;
964
965         if (j)
966                 ide_ignore_cable |= (1 << i);
967         else
968                 ide_ignore_cable &= (1 << i);
969
970         return 0;
971 }
972
973 module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
974 MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
975
976 void ide_port_apply_params(ide_hwif_t *hwif)
977 {
978         int i;
979
980         if (ide_ignore_cable & (1 << hwif->index)) {
981                 printk(KERN_INFO "ide: ignoring cable detection for %s\n",
982                                  hwif->name);
983                 hwif->cbl = ATA_CBL_PATA40_SHORT;
984         }
985
986         for (i = 0; i < MAX_DRIVES; i++)
987                 ide_dev_apply_params(&hwif->drives[i]);
988 }
989
990 /*
991  * This is gets invoked once during initialization, to set *everything* up
992  */
993 static int __init ide_init(void)
994 {
995         int ret;
996
997         printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
998
999         ret = bus_register(&ide_bus_type);
1000         if (ret < 0) {
1001                 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1002                 return ret;
1003         }
1004
1005         ide_port_class = class_create(THIS_MODULE, "ide_port");
1006         if (IS_ERR(ide_port_class)) {
1007                 ret = PTR_ERR(ide_port_class);
1008                 goto out_port_class;
1009         }
1010
1011         proc_ide_create();
1012
1013         return 0;
1014
1015 out_port_class:
1016         bus_unregister(&ide_bus_type);
1017
1018         return ret;
1019 }
1020
1021 static void __exit ide_exit(void)
1022 {
1023         proc_ide_destroy();
1024
1025         class_destroy(ide_port_class);
1026
1027         bus_unregister(&ide_bus_type);
1028 }
1029
1030 module_init(ide_init);
1031 module_exit(ide_exit);
1032
1033 MODULE_LICENSE("GPL");