aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ide/ide.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ide/ide.c')
-rw-r--r--drivers/ide/ide.c342
1 files changed, 10 insertions, 332 deletions
diff --git a/drivers/ide/ide.c b/drivers/ide/ide.c
index 04f8f13cb9d7..258805da15c3 100644
--- a/drivers/ide/ide.c
+++ b/drivers/ide/ide.c
@@ -60,189 +60,8 @@
60#include <linux/completion.h> 60#include <linux/completion.h>
61#include <linux/device.h> 61#include <linux/device.h>
62 62
63
64/* default maximum number of failures */
65#define IDE_DEFAULT_MAX_FAILURES 1
66
67struct class *ide_port_class; 63struct class *ide_port_class;
68 64
69static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
70 IDE2_MAJOR, IDE3_MAJOR,
71 IDE4_MAJOR, IDE5_MAJOR,
72 IDE6_MAJOR, IDE7_MAJOR,
73 IDE8_MAJOR, IDE9_MAJOR };
74
75DEFINE_MUTEX(ide_cfg_mtx);
76
77__cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
78EXPORT_SYMBOL(ide_lock);
79
80static void ide_port_init_devices_data(ide_hwif_t *);
81
82/*
83 * Do not even *think* about calling this!
84 */
85void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
86{
87 /* bulk initialize hwif & drive info with zeros */
88 memset(hwif, 0, sizeof(ide_hwif_t));
89
90 /* fill in any non-zero initial values */
91 hwif->index = index;
92 hwif->major = ide_hwif_to_major[index];
93
94 hwif->name[0] = 'i';
95 hwif->name[1] = 'd';
96 hwif->name[2] = 'e';
97 hwif->name[3] = '0' + index;
98
99 init_completion(&hwif->gendev_rel_comp);
100
101 hwif->tp_ops = &default_tp_ops;
102
103 ide_port_init_devices_data(hwif);
104}
105
106static void ide_port_init_devices_data(ide_hwif_t *hwif)
107{
108 int unit;
109
110 for (unit = 0; unit < MAX_DRIVES; ++unit) {
111 ide_drive_t *drive = &hwif->drives[unit];
112 u8 j = (hwif->index * MAX_DRIVES) + unit;
113
114 memset(drive, 0, sizeof(*drive));
115
116 drive->media = ide_disk;
117 drive->select = (unit << 4) | ATA_DEVICE_OBS;
118 drive->hwif = hwif;
119 drive->ready_stat = ATA_DRDY;
120 drive->bad_wstat = BAD_W_STAT;
121 drive->special.b.recalibrate = 1;
122 drive->special.b.set_geometry = 1;
123 drive->name[0] = 'h';
124 drive->name[1] = 'd';
125 drive->name[2] = 'a' + j;
126 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
127
128 INIT_LIST_HEAD(&drive->list);
129 init_completion(&drive->gendev_rel_comp);
130 }
131}
132
133/* Called with ide_lock held. */
134static void __ide_port_unregister_devices(ide_hwif_t *hwif)
135{
136 int i;
137
138 for (i = 0; i < MAX_DRIVES; i++) {
139 ide_drive_t *drive = &hwif->drives[i];
140
141 if (drive->dev_flags & IDE_DFLAG_PRESENT) {
142 spin_unlock_irq(&ide_lock);
143 device_unregister(&drive->gendev);
144 wait_for_completion(&drive->gendev_rel_comp);
145 spin_lock_irq(&ide_lock);
146 }
147 }
148}
149
150void ide_port_unregister_devices(ide_hwif_t *hwif)
151{
152 mutex_lock(&ide_cfg_mtx);
153 spin_lock_irq(&ide_lock);
154 __ide_port_unregister_devices(hwif);
155 hwif->present = 0;
156 ide_port_init_devices_data(hwif);
157 spin_unlock_irq(&ide_lock);
158 mutex_unlock(&ide_cfg_mtx);
159}
160EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
161
162/**
163 * ide_unregister - free an IDE interface
164 * @hwif: IDE interface
165 *
166 * Perform the final unregister of an IDE interface. At the moment
167 * we don't refcount interfaces so this will also get split up.
168 *
169 * Locking:
170 * The caller must not hold the IDE locks
171 * The drive present/vanishing is not yet properly locked
172 * Take care with the callbacks. These have been split to avoid
173 * deadlocking the IDE layer. The shutdown callback is called
174 * before we take the lock and free resources. It is up to the
175 * caller to be sure there is no pending I/O here, and that
176 * the interface will not be reopened (present/vanishing locking
177 * isn't yet done BTW). After we commit to the final kill we
178 * call the cleanup callback with the ide locks held.
179 *
180 * Unregister restores the hwif structures to the default state.
181 * This is raving bonkers.
182 */
183
184void ide_unregister(ide_hwif_t *hwif)
185{
186 ide_hwif_t *g;
187 ide_hwgroup_t *hwgroup;
188 int irq_count = 0;
189
190 BUG_ON(in_interrupt());
191 BUG_ON(irqs_disabled());
192
193 mutex_lock(&ide_cfg_mtx);
194
195 spin_lock_irq(&ide_lock);
196 if (hwif->present) {
197 __ide_port_unregister_devices(hwif);
198 hwif->present = 0;
199 }
200 spin_unlock_irq(&ide_lock);
201
202 ide_proc_unregister_port(hwif);
203
204 hwgroup = hwif->hwgroup;
205 /*
206 * free the irq if we were the only hwif using it
207 */
208 g = hwgroup->hwif;
209 do {
210 if (g->irq == hwif->irq)
211 ++irq_count;
212 g = g->next;
213 } while (g != hwgroup->hwif);
214 if (irq_count == 1)
215 free_irq(hwif->irq, hwgroup);
216
217 ide_remove_port_from_hwgroup(hwif);
218
219 device_unregister(hwif->portdev);
220 device_unregister(&hwif->gendev);
221 wait_for_completion(&hwif->gendev_rel_comp);
222
223 /*
224 * Remove us from the kernel's knowledge
225 */
226 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
227 kfree(hwif->sg_table);
228 unregister_blkdev(hwif->major, hwif->name);
229
230 ide_release_dma_engine(hwif);
231
232 mutex_unlock(&ide_cfg_mtx);
233}
234
235void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
236{
237 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
238 hwif->irq = hw->irq;
239 hwif->chipset = hw->chipset;
240 hwif->dev = hw->dev;
241 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
242 hwif->ack_intr = hw->ack_intr;
243 hwif->config_data = hw->config;
244}
245
246/* 65/*
247 * Locks for IDE setting functionality 66 * Locks for IDE setting functionality
248 */ 67 */
@@ -354,9 +173,9 @@ static int set_pio_mode(ide_drive_t *drive, int arg)
354 unsigned long flags; 173 unsigned long flags;
355 174
356 /* take lock for IDE_DFLAG_[NO_]UNMASK/[NO_]IO_32BIT */ 175 /* take lock for IDE_DFLAG_[NO_]UNMASK/[NO_]IO_32BIT */
357 spin_lock_irqsave(&ide_lock, flags); 176 spin_lock_irqsave(&hwif->lock, flags);
358 port_ops->set_pio_mode(drive, arg); 177 port_ops->set_pio_mode(drive, arg);
359 spin_unlock_irqrestore(&ide_lock, flags); 178 spin_unlock_irqrestore(&hwif->lock, flags);
360 } else 179 } else
361 port_ops->set_pio_mode(drive, arg); 180 port_ops->set_pio_mode(drive, arg);
362 } else { 181 } else {
@@ -397,80 +216,6 @@ ide_ext_devset_rw_sync(unmaskirq, unmaskirq);
397ide_ext_devset_rw_sync(using_dma, using_dma); 216ide_ext_devset_rw_sync(using_dma, using_dma);
398__IDE_DEVSET(pio_mode, DS_SYNC, NULL, set_pio_mode); 217__IDE_DEVSET(pio_mode, DS_SYNC, NULL, set_pio_mode);
399 218
400static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
401{
402 ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive);
403 ide_hwif_t *hwif = HWIF(drive);
404 struct request *rq;
405 struct request_pm_state rqpm;
406 ide_task_t args;
407 int ret;
408
409 /* call ACPI _GTM only once */
410 if ((drive->dn & 1) == 0 || pair == NULL)
411 ide_acpi_get_timing(hwif);
412
413 memset(&rqpm, 0, sizeof(rqpm));
414 memset(&args, 0, sizeof(args));
415 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
416 rq->cmd_type = REQ_TYPE_PM_SUSPEND;
417 rq->special = &args;
418 rq->data = &rqpm;
419 rqpm.pm_step = IDE_PM_START_SUSPEND;
420 if (mesg.event == PM_EVENT_PRETHAW)
421 mesg.event = PM_EVENT_FREEZE;
422 rqpm.pm_state = mesg.event;
423
424 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
425 blk_put_request(rq);
426
427 /* call ACPI _PS3 only after both devices are suspended */
428 if (ret == 0 && ((drive->dn & 1) || pair == NULL))
429 ide_acpi_set_state(hwif, 0);
430
431 return ret;
432}
433
434static int generic_ide_resume(struct device *dev)
435{
436 ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive);
437 ide_hwif_t *hwif = HWIF(drive);
438 struct request *rq;
439 struct request_pm_state rqpm;
440 ide_task_t args;
441 int err;
442
443 /* call ACPI _PS0 / _STM only once */
444 if ((drive->dn & 1) == 0 || pair == NULL) {
445 ide_acpi_set_state(hwif, 1);
446 ide_acpi_push_timing(hwif);
447 }
448
449 ide_acpi_exec_tfs(drive);
450
451 memset(&rqpm, 0, sizeof(rqpm));
452 memset(&args, 0, sizeof(args));
453 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
454 rq->cmd_type = REQ_TYPE_PM_RESUME;
455 rq->cmd_flags |= REQ_PREEMPT;
456 rq->special = &args;
457 rq->data = &rqpm;
458 rqpm.pm_step = IDE_PM_START_RESUME;
459 rqpm.pm_state = PM_EVENT_ON;
460
461 err = blk_execute_rq(drive->queue, NULL, rq, 1);
462 blk_put_request(rq);
463
464 if (err == 0 && dev->driver) {
465 ide_driver_t *drv = to_ide_driver(dev->driver);
466
467 if (drv->resume)
468 drv->resume(drive);
469 }
470
471 return err;
472}
473
474/** 219/**
475 * ide_device_get - get an additional reference to a ide_drive_t 220 * ide_device_get - get an additional reference to a ide_drive_t
476 * @drive: device to get a reference to 221 * @drive: device to get a reference to
@@ -523,88 +268,20 @@ static int ide_bus_match(struct device *dev, struct device_driver *drv)
523 return 1; 268 return 1;
524} 269}
525 270
526static char *media_string(ide_drive_t *drive)
527{
528 switch (drive->media) {
529 case ide_disk:
530 return "disk";
531 case ide_cdrom:
532 return "cdrom";
533 case ide_tape:
534 return "tape";
535 case ide_floppy:
536 return "floppy";
537 case ide_optical:
538 return "optical";
539 default:
540 return "UNKNOWN";
541 }
542}
543
544static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
545{
546 ide_drive_t *drive = to_ide_device(dev);
547 return sprintf(buf, "%s\n", media_string(drive));
548}
549
550static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
551{
552 ide_drive_t *drive = to_ide_device(dev);
553 return sprintf(buf, "%s\n", drive->name);
554}
555
556static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
557{
558 ide_drive_t *drive = to_ide_device(dev);
559 return sprintf(buf, "ide:m-%s\n", media_string(drive));
560}
561
562static ssize_t model_show(struct device *dev, struct device_attribute *attr,
563 char *buf)
564{
565 ide_drive_t *drive = to_ide_device(dev);
566 return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_PROD]);
567}
568
569static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
570 char *buf)
571{
572 ide_drive_t *drive = to_ide_device(dev);
573 return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_FW_REV]);
574}
575
576static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
577 char *buf)
578{
579 ide_drive_t *drive = to_ide_device(dev);
580 return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_SERNO]);
581}
582
583static struct device_attribute ide_dev_attrs[] = {
584 __ATTR_RO(media),
585 __ATTR_RO(drivename),
586 __ATTR_RO(modalias),
587 __ATTR_RO(model),
588 __ATTR_RO(firmware),
589 __ATTR(serial, 0400, serial_show, NULL),
590 __ATTR(unload_heads, 0644, ide_park_show, ide_park_store),
591 __ATTR_NULL
592};
593
594static int ide_uevent(struct device *dev, struct kobj_uevent_env *env) 271static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
595{ 272{
596 ide_drive_t *drive = to_ide_device(dev); 273 ide_drive_t *drive = to_ide_device(dev);
597 274
598 add_uevent_var(env, "MEDIA=%s", media_string(drive)); 275 add_uevent_var(env, "MEDIA=%s", ide_media_string(drive));
599 add_uevent_var(env, "DRIVENAME=%s", drive->name); 276 add_uevent_var(env, "DRIVENAME=%s", drive->name);
600 add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive)); 277 add_uevent_var(env, "MODALIAS=ide:m-%s", ide_media_string(drive));
601 return 0; 278 return 0;
602} 279}
603 280
604static int generic_ide_probe(struct device *dev) 281static int generic_ide_probe(struct device *dev)
605{ 282{
606 ide_drive_t *drive = to_ide_device(dev); 283 ide_drive_t *drive = to_ide_device(dev);
607 ide_driver_t *drv = to_ide_driver(dev->driver); 284 struct ide_driver *drv = to_ide_driver(dev->driver);
608 285
609 return drv->probe ? drv->probe(drive) : -ENODEV; 286 return drv->probe ? drv->probe(drive) : -ENODEV;
610} 287}
@@ -612,7 +289,7 @@ static int generic_ide_probe(struct device *dev)
612static int generic_ide_remove(struct device *dev) 289static int generic_ide_remove(struct device *dev)
613{ 290{
614 ide_drive_t *drive = to_ide_device(dev); 291 ide_drive_t *drive = to_ide_device(dev);
615 ide_driver_t *drv = to_ide_driver(dev->driver); 292 struct ide_driver *drv = to_ide_driver(dev->driver);
616 293
617 if (drv->remove) 294 if (drv->remove)
618 drv->remove(drive); 295 drv->remove(drive);
@@ -623,7 +300,7 @@ static int generic_ide_remove(struct device *dev)
623static void generic_ide_shutdown(struct device *dev) 300static void generic_ide_shutdown(struct device *dev)
624{ 301{
625 ide_drive_t *drive = to_ide_device(dev); 302 ide_drive_t *drive = to_ide_device(dev);
626 ide_driver_t *drv = to_ide_driver(dev->driver); 303 struct ide_driver *drv = to_ide_driver(dev->driver);
627 304
628 if (dev->driver && drv->shutdown) 305 if (dev->driver && drv->shutdown)
629 drv->shutdown(drive); 306 drv->shutdown(drive);
@@ -811,6 +488,7 @@ MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
811 488
812void ide_port_apply_params(ide_hwif_t *hwif) 489void ide_port_apply_params(ide_hwif_t *hwif)
813{ 490{
491 ide_drive_t *drive;
814 int i; 492 int i;
815 493
816 if (ide_ignore_cable & (1 << hwif->index)) { 494 if (ide_ignore_cable & (1 << hwif->index)) {
@@ -819,8 +497,8 @@ void ide_port_apply_params(ide_hwif_t *hwif)
819 hwif->cbl = ATA_CBL_PATA40_SHORT; 497 hwif->cbl = ATA_CBL_PATA40_SHORT;
820 } 498 }
821 499
822 for (i = 0; i < MAX_DRIVES; i++) 500 ide_port_for_each_dev(i, drive, hwif)
823 ide_dev_apply_params(&hwif->drives[i], i); 501 ide_dev_apply_params(drive, i);
824} 502}
825 503
826/* 504/*