Merge branch 'topic/snd_card_new-err' into topic/oxygen
[linux-2.6] / drivers / ide / ide-acpi.c
1 /*
2  * Provides ACPI support for IDE drives.
3  *
4  * Copyright (C) 2005 Intel Corp.
5  * Copyright (C) 2005 Randy Dunlap
6  * Copyright (C) 2006 SUSE Linux Products GmbH
7  * Copyright (C) 2006 Hannes Reinecke
8  */
9
10 #include <linux/ata.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <acpi/acpi.h>
16 #include <linux/ide.h>
17 #include <linux/pci.h>
18 #include <linux/dmi.h>
19
20 #include <acpi/acpi_bus.h>
21
22 #define REGS_PER_GTF            7
23 struct taskfile_array {
24         u8      tfa[REGS_PER_GTF];      /* regs. 0x1f1 - 0x1f7 */
25 };
26
27 struct GTM_buffer {
28         u32     PIO_speed0;
29         u32     DMA_speed0;
30         u32     PIO_speed1;
31         u32     DMA_speed1;
32         u32     GTM_flags;
33 };
34
35 struct ide_acpi_drive_link {
36         acpi_handle      obj_handle;
37         u8               idbuff[512];
38 };
39
40 struct ide_acpi_hwif_link {
41         ide_hwif_t                      *hwif;
42         acpi_handle                      obj_handle;
43         struct GTM_buffer                gtm;
44         struct ide_acpi_drive_link       master;
45         struct ide_acpi_drive_link       slave;
46 };
47
48 #undef DEBUGGING
49 /* note: adds function name and KERN_DEBUG */
50 #ifdef DEBUGGING
51 #define DEBPRINT(fmt, args...)  \
52                 printk(KERN_DEBUG "%s: " fmt, __func__, ## args)
53 #else
54 #define DEBPRINT(fmt, args...)  do {} while (0)
55 #endif  /* DEBUGGING */
56
57 static int ide_noacpi;
58 module_param_named(noacpi, ide_noacpi, bool, 0);
59 MODULE_PARM_DESC(noacpi, "disable IDE ACPI support");
60
61 static int ide_acpigtf;
62 module_param_named(acpigtf, ide_acpigtf, bool, 0);
63 MODULE_PARM_DESC(acpigtf, "enable IDE ACPI _GTF support");
64
65 static int ide_acpionboot;
66 module_param_named(acpionboot, ide_acpionboot, bool, 0);
67 MODULE_PARM_DESC(acpionboot, "call IDE ACPI methods on boot");
68
69 static bool ide_noacpi_psx;
70 static int no_acpi_psx(const struct dmi_system_id *id)
71 {
72         ide_noacpi_psx = true;
73         printk(KERN_NOTICE"%s detected - disable ACPI _PSx.\n", id->ident);
74         return 0;
75 }
76
77 static const struct dmi_system_id ide_acpi_dmi_table[] = {
78         /* Bug 9673. */
79         /* We should check if this is because ACPI NVS isn't save/restored. */
80         {
81                 .callback = no_acpi_psx,
82                 .ident    = "HP nx9005",
83                 .matches  = {
84                         DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies Ltd."),
85                         DMI_MATCH(DMI_BIOS_VERSION, "KAM1.60")
86                 },
87         },
88
89         { }     /* terminate list */
90 };
91
92 static int ide_acpi_blacklist(void)
93 {
94         static int done;
95         if (done)
96                 return 0;
97         done = 1;
98         dmi_check_system(ide_acpi_dmi_table);
99         return 0;
100 }
101
102 /**
103  * ide_get_dev_handle - finds acpi_handle and PCI device.function
104  * @dev: device to locate
105  * @handle: returned acpi_handle for @dev
106  * @pcidevfn: return PCI device.func for @dev
107  *
108  * Returns the ACPI object handle to the corresponding PCI device.
109  *
110  * Returns 0 on success, <0 on error.
111  */
112 static int ide_get_dev_handle(struct device *dev, acpi_handle *handle,
113                                acpi_integer *pcidevfn)
114 {
115         struct pci_dev *pdev = to_pci_dev(dev);
116         unsigned int bus, devnum, func;
117         acpi_integer addr;
118         acpi_handle dev_handle;
119         struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
120                                         .pointer = NULL};
121         acpi_status status;
122         struct acpi_device_info *dinfo = NULL;
123         int ret = -ENODEV;
124
125         bus = pdev->bus->number;
126         devnum = PCI_SLOT(pdev->devfn);
127         func = PCI_FUNC(pdev->devfn);
128         /* ACPI _ADR encoding for PCI bus: */
129         addr = (acpi_integer)(devnum << 16 | func);
130
131         DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
132
133         dev_handle = DEVICE_ACPI_HANDLE(dev);
134         if (!dev_handle) {
135                 DEBPRINT("no acpi handle for device\n");
136                 goto err;
137         }
138
139         status = acpi_get_object_info(dev_handle, &buffer);
140         if (ACPI_FAILURE(status)) {
141                 DEBPRINT("get_object_info for device failed\n");
142                 goto err;
143         }
144         dinfo = buffer.pointer;
145         if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
146             dinfo->address == addr) {
147                 *pcidevfn = addr;
148                 *handle = dev_handle;
149         } else {
150                 DEBPRINT("get_object_info for device has wrong "
151                         " address: %llu, should be %u\n",
152                         dinfo ? (unsigned long long)dinfo->address : -1ULL,
153                         (unsigned int)addr);
154                 goto err;
155         }
156
157         DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
158                  devnum, func, (unsigned long long)addr, *handle);
159         ret = 0;
160 err:
161         kfree(dinfo);
162         return ret;
163 }
164
165 /**
166  * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
167  * @hwif: device to locate
168  *
169  * Retrieves the object handle for a given hwif.
170  *
171  * Returns handle on success, 0 on error.
172  */
173 static acpi_handle ide_acpi_hwif_get_handle(ide_hwif_t *hwif)
174 {
175         struct device           *dev = hwif->gendev.parent;
176         acpi_handle             uninitialized_var(dev_handle);
177         acpi_integer            pcidevfn;
178         acpi_handle             chan_handle;
179         int                     err;
180
181         DEBPRINT("ENTER: device %s\n", hwif->name);
182
183         if (!dev) {
184                 DEBPRINT("no PCI device for %s\n", hwif->name);
185                 return NULL;
186         }
187
188         err = ide_get_dev_handle(dev, &dev_handle, &pcidevfn);
189         if (err < 0) {
190                 DEBPRINT("ide_get_dev_handle failed (%d)\n", err);
191                 return NULL;
192         }
193
194         /* get child objects of dev_handle == channel objects,
195          * + _their_ children == drive objects */
196         /* channel is hwif->channel */
197         chan_handle = acpi_get_child(dev_handle, hwif->channel);
198         DEBPRINT("chan adr=%d: handle=0x%p\n",
199                  hwif->channel, chan_handle);
200
201         return chan_handle;
202 }
203
204 /**
205  * ide_acpi_drive_get_handle - Get ACPI object handle for a given drive
206  * @drive: device to locate
207  *
208  * Retrieves the object handle of a given drive. According to the ACPI
209  * spec the drive is a child of the hwif.
210  *
211  * Returns handle on success, 0 on error.
212  */
213 static acpi_handle ide_acpi_drive_get_handle(ide_drive_t *drive)
214 {
215         ide_hwif_t      *hwif = drive->hwif;
216         int              port;
217         acpi_handle      drive_handle;
218
219         if (!hwif->acpidata)
220                 return NULL;
221
222         if (!hwif->acpidata->obj_handle)
223                 return NULL;
224
225         port = hwif->channel ? drive->dn - 2: drive->dn;
226
227         DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
228                  drive->name, hwif->channel, port);
229
230
231         /* TBD: could also check ACPI object VALID bits */
232         drive_handle = acpi_get_child(hwif->acpidata->obj_handle, port);
233         DEBPRINT("drive %s handle 0x%p\n", drive->name, drive_handle);
234
235         return drive_handle;
236 }
237
238 /**
239  * do_drive_get_GTF - get the drive bootup default taskfile settings
240  * @drive: the drive for which the taskfile settings should be retrieved
241  * @gtf_length: number of bytes of _GTF data returned at @gtf_address
242  * @gtf_address: buffer containing _GTF taskfile arrays
243  *
244  * The _GTF method has no input parameters.
245  * It returns a variable number of register set values (registers
246  * hex 1F1..1F7, taskfiles).
247  * The <variable number> is not known in advance, so have ACPI-CA
248  * allocate the buffer as needed and return it, then free it later.
249  *
250  * The returned @gtf_length and @gtf_address are only valid if the
251  * function return value is 0.
252  */
253 static int do_drive_get_GTF(ide_drive_t *drive,
254                      unsigned int *gtf_length, unsigned long *gtf_address,
255                      unsigned long *obj_loc)
256 {
257         acpi_status                     status;
258         struct acpi_buffer              output;
259         union acpi_object               *out_obj;
260         ide_hwif_t                      *hwif = drive->hwif;
261         struct device                   *dev = hwif->gendev.parent;
262         int                             err = -ENODEV;
263         int                             port;
264
265         *gtf_length = 0;
266         *gtf_address = 0UL;
267         *obj_loc = 0UL;
268
269         if (ide_noacpi)
270                 return 0;
271
272         if (!dev) {
273                 DEBPRINT("no PCI device for %s\n", hwif->name);
274                 goto out;
275         }
276
277         if (!hwif->acpidata) {
278                 DEBPRINT("no ACPI data for %s\n", hwif->name);
279                 goto out;
280         }
281
282         port = hwif->channel ? drive->dn - 2: drive->dn;
283
284         DEBPRINT("ENTER: %s at %s, port#: %d, hard_port#: %d\n",
285                  hwif->name, dev_name(dev), port, hwif->channel);
286
287         if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0) {
288                 DEBPRINT("%s drive %d:%d not present\n",
289                          hwif->name, hwif->channel, port);
290                 goto out;
291         }
292
293         /* Get this drive's _ADR info. if not already known. */
294         if (!drive->acpidata->obj_handle) {
295                 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
296                 if (!drive->acpidata->obj_handle) {
297                         DEBPRINT("No ACPI object found for %s\n",
298                                  drive->name);
299                         goto out;
300                 }
301         }
302
303         /* Setting up output buffer */
304         output.length = ACPI_ALLOCATE_BUFFER;
305         output.pointer = NULL;  /* ACPI-CA sets this; save/free it later */
306
307         /* _GTF has no input parameters */
308         err = -EIO;
309         status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
310                                       NULL, &output);
311         if (ACPI_FAILURE(status)) {
312                 printk(KERN_DEBUG
313                        "%s: Run _GTF error: status = 0x%x\n",
314                        __func__, status);
315                 goto out;
316         }
317
318         if (!output.length || !output.pointer) {
319                 DEBPRINT("Run _GTF: "
320                        "length or ptr is NULL (0x%llx, 0x%p)\n",
321                        (unsigned long long)output.length,
322                        output.pointer);
323                 goto out;
324         }
325
326         out_obj = output.pointer;
327         if (out_obj->type != ACPI_TYPE_BUFFER) {
328                 DEBPRINT("Run _GTF: error: "
329                        "expected object type of ACPI_TYPE_BUFFER, "
330                        "got 0x%x\n", out_obj->type);
331                 err = -ENOENT;
332                 kfree(output.pointer);
333                 goto out;
334         }
335
336         if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
337             out_obj->buffer.length % REGS_PER_GTF) {
338                 printk(KERN_ERR
339                        "%s: unexpected GTF length (%d) or addr (0x%p)\n",
340                        __func__, out_obj->buffer.length,
341                        out_obj->buffer.pointer);
342                 err = -ENOENT;
343                 kfree(output.pointer);
344                 goto out;
345         }
346
347         *gtf_length = out_obj->buffer.length;
348         *gtf_address = (unsigned long)out_obj->buffer.pointer;
349         *obj_loc = (unsigned long)out_obj;
350         DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
351                  *gtf_length, *gtf_address, *obj_loc);
352         err = 0;
353 out:
354         return err;
355 }
356
357 /**
358  * taskfile_load_raw - send taskfile registers to drive
359  * @drive: drive to which output is sent
360  * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
361  *
362  * Outputs IDE taskfile to the drive.
363  */
364 static int taskfile_load_raw(ide_drive_t *drive,
365                               const struct taskfile_array *gtf)
366 {
367         ide_task_t args;
368         int err = 0;
369
370         DEBPRINT("(0x1f1-1f7): hex: "
371                "%02x %02x %02x %02x %02x %02x %02x\n",
372                gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
373                gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
374
375         memset(&args, 0, sizeof(ide_task_t));
376
377         /* convert gtf to IDE Taskfile */
378         memcpy(&args.tf_array[7], &gtf->tfa, 7);
379         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
380
381         if (!ide_acpigtf) {
382                 DEBPRINT("_GTF execution disabled\n");
383                 return err;
384         }
385
386         err = ide_no_data_taskfile(drive, &args);
387         if (err)
388                 printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
389                        __func__, err);
390
391         return err;
392 }
393
394 /**
395  * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
396  * @drive: the drive to which the taskfile command should be sent
397  * @gtf_length: total number of bytes of _GTF taskfiles
398  * @gtf_address: location of _GTF taskfile arrays
399  *
400  * Write {gtf_address, length gtf_length} in groups of
401  * REGS_PER_GTF bytes.
402  */
403 static int do_drive_set_taskfiles(ide_drive_t *drive,
404                                   unsigned int gtf_length,
405                                   unsigned long gtf_address)
406 {
407         int                     rc = -ENODEV, err;
408         int                     gtf_count = gtf_length / REGS_PER_GTF;
409         int                     ix;
410         struct taskfile_array   *gtf;
411
412         if (ide_noacpi)
413                 return 0;
414
415         DEBPRINT("ENTER: %s, hard_port#: %d\n", drive->name, drive->dn);
416
417         if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
418                 goto out;
419
420         if (!gtf_count)         /* shouldn't be here */
421                 goto out;
422
423         DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
424                  gtf_length, gtf_length, gtf_count, gtf_address);
425
426         if (gtf_length % REGS_PER_GTF) {
427                 printk(KERN_ERR "%s: unexpected GTF length (%d)\n",
428                        __func__, gtf_length);
429                 goto out;
430         }
431
432         rc = 0;
433         for (ix = 0; ix < gtf_count; ix++) {
434                 gtf = (struct taskfile_array *)
435                         (gtf_address + ix * REGS_PER_GTF);
436
437                 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
438                 err = taskfile_load_raw(drive, gtf);
439                 if (err)
440                         rc = err;
441         }
442
443 out:
444         return rc;
445 }
446
447 /**
448  * ide_acpi_exec_tfs - get then write drive taskfile settings
449  * @drive: the drive for which the taskfile settings should be
450  *         written.
451  *
452  * According to the ACPI spec this should be called after _STM
453  * has been evaluated for the interface. Some ACPI vendors interpret
454  * that as a hard requirement and modify the taskfile according
455  * to the Identify Drive information passed down with _STM.
456  * So one should really make sure to call this only after _STM has
457  * been executed.
458  */
459 int ide_acpi_exec_tfs(ide_drive_t *drive)
460 {
461         int             ret;
462         unsigned int    gtf_length;
463         unsigned long   gtf_address;
464         unsigned long   obj_loc;
465
466         if (ide_noacpi)
467                 return 0;
468
469         DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
470
471         ret = do_drive_get_GTF(drive, &gtf_length, &gtf_address, &obj_loc);
472         if (ret < 0) {
473                 DEBPRINT("get_GTF error (%d)\n", ret);
474                 return ret;
475         }
476
477         DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
478
479         ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
480         kfree((void *)obj_loc);
481         if (ret < 0) {
482                 DEBPRINT("set_taskfiles error (%d)\n", ret);
483         }
484
485         DEBPRINT("ret=%d\n", ret);
486
487         return ret;
488 }
489
490 /**
491  * ide_acpi_get_timing - get the channel (controller) timings
492  * @hwif: target IDE interface (channel)
493  *
494  * This function executes the _GTM ACPI method for the target channel.
495  *
496  */
497 void ide_acpi_get_timing(ide_hwif_t *hwif)
498 {
499         acpi_status             status;
500         struct acpi_buffer      output;
501         union acpi_object       *out_obj;
502
503         if (ide_noacpi)
504                 return;
505
506         DEBPRINT("ENTER:\n");
507
508         if (!hwif->acpidata) {
509                 DEBPRINT("no ACPI data for %s\n", hwif->name);
510                 return;
511         }
512
513         /* Setting up output buffer for _GTM */
514         output.length = ACPI_ALLOCATE_BUFFER;
515         output.pointer = NULL;  /* ACPI-CA sets this; save/free it later */
516
517         /* _GTM has no input parameters */
518         status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
519                                       NULL, &output);
520
521         DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
522                  status, output.pointer,
523                  (unsigned long long)output.length);
524
525         if (ACPI_FAILURE(status)) {
526                 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
527                 return;
528         }
529
530         if (!output.length || !output.pointer) {
531                 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
532                        (unsigned long long)output.length,
533                        output.pointer);
534                 kfree(output.pointer);
535                 return;
536         }
537
538         out_obj = output.pointer;
539         if (out_obj->type != ACPI_TYPE_BUFFER) {
540                 kfree(output.pointer);
541                 DEBPRINT("Run _GTM: error: "
542                        "expected object type of ACPI_TYPE_BUFFER, "
543                        "got 0x%x\n", out_obj->type);
544                 return;
545         }
546
547         if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
548             out_obj->buffer.length != sizeof(struct GTM_buffer)) {
549                 kfree(output.pointer);
550                 printk(KERN_ERR
551                         "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
552                         "addr (0x%p)\n",
553                         __func__, out_obj->buffer.length,
554                         sizeof(struct GTM_buffer), out_obj->buffer.pointer);
555                 return;
556         }
557
558         memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
559                sizeof(struct GTM_buffer));
560
561         DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
562                  out_obj->buffer.pointer, out_obj->buffer.length,
563                  sizeof(struct GTM_buffer));
564
565         DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
566                  hwif->acpidata->gtm.PIO_speed0,
567                  hwif->acpidata->gtm.DMA_speed0,
568                  hwif->acpidata->gtm.PIO_speed1,
569                  hwif->acpidata->gtm.DMA_speed1,
570                  hwif->acpidata->gtm.GTM_flags);
571
572         kfree(output.pointer);
573 }
574
575 /**
576  * ide_acpi_push_timing - set the channel (controller) timings
577  * @hwif: target IDE interface (channel)
578  *
579  * This function executes the _STM ACPI method for the target channel.
580  *
581  * _STM requires Identify Drive data, which has to passed as an argument.
582  * Unfortunately drive->id is a mangled version which we can't readily
583  * use; hence we'll get the information afresh.
584  */
585 void ide_acpi_push_timing(ide_hwif_t *hwif)
586 {
587         acpi_status             status;
588         struct acpi_object_list input;
589         union acpi_object       in_params[3];
590         struct ide_acpi_drive_link      *master = &hwif->acpidata->master;
591         struct ide_acpi_drive_link      *slave = &hwif->acpidata->slave;
592
593         if (ide_noacpi)
594                 return;
595
596         DEBPRINT("ENTER:\n");
597
598         if (!hwif->acpidata) {
599                 DEBPRINT("no ACPI data for %s\n", hwif->name);
600                 return;
601         }
602
603         /* Give the GTM buffer + drive Identify data to the channel via the
604          * _STM method: */
605         /* setup input parameters buffer for _STM */
606         input.count = 3;
607         input.pointer = in_params;
608         in_params[0].type = ACPI_TYPE_BUFFER;
609         in_params[0].buffer.length = sizeof(struct GTM_buffer);
610         in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
611         in_params[1].type = ACPI_TYPE_BUFFER;
612         in_params[1].buffer.length = ATA_ID_WORDS * 2;
613         in_params[1].buffer.pointer = (u8 *)&master->idbuff;
614         in_params[2].type = ACPI_TYPE_BUFFER;
615         in_params[2].buffer.length = ATA_ID_WORDS * 2;
616         in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
617         /* Output buffer: _STM has no output */
618
619         status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
620                                       &input, NULL);
621
622         if (ACPI_FAILURE(status)) {
623                 DEBPRINT("Run _STM error: status = 0x%x\n", status);
624         }
625         DEBPRINT("_STM status: %d\n", status);
626 }
627
628 /**
629  * ide_acpi_set_state - set the channel power state
630  * @hwif: target IDE interface
631  * @on: state, on/off
632  *
633  * This function executes the _PS0/_PS3 ACPI method to set the power state.
634  * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
635  */
636 void ide_acpi_set_state(ide_hwif_t *hwif, int on)
637 {
638         ide_drive_t *drive;
639         int i;
640
641         if (ide_noacpi || ide_noacpi_psx)
642                 return;
643
644         DEBPRINT("ENTER:\n");
645
646         if (!hwif->acpidata) {
647                 DEBPRINT("no ACPI data for %s\n", hwif->name);
648                 return;
649         }
650         /* channel first and then drives for power on and verse versa for power off */
651         if (on)
652                 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
653
654         ide_port_for_each_dev(i, drive, hwif) {
655                 if (!drive->acpidata->obj_handle)
656                         drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
657
658                 if (drive->acpidata->obj_handle &&
659                     (drive->dev_flags & IDE_DFLAG_PRESENT)) {
660                         acpi_bus_set_power(drive->acpidata->obj_handle,
661                                 on? ACPI_STATE_D0: ACPI_STATE_D3);
662                 }
663         }
664         if (!on)
665                 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
666 }
667
668 /**
669  * ide_acpi_init - initialize the ACPI link for an IDE interface
670  * @hwif: target IDE interface (channel)
671  *
672  * The ACPI spec is not quite clear when the drive identify buffer
673  * should be obtained. Calling IDENTIFY DEVICE during shutdown
674  * is not the best of ideas as the drive might already being put to
675  * sleep. And obviously we can't call it during resume.
676  * So we get the information during startup; but this means that
677  * any changes during run-time will be lost after resume.
678  */
679 void ide_acpi_init(ide_hwif_t *hwif)
680 {
681         ide_acpi_blacklist();
682
683         hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
684         if (!hwif->acpidata)
685                 return;
686
687         hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
688         if (!hwif->acpidata->obj_handle) {
689                 DEBPRINT("no ACPI object for %s found\n", hwif->name);
690                 kfree(hwif->acpidata);
691                 hwif->acpidata = NULL;
692         }
693 }
694
695 void ide_acpi_port_init_devices(ide_hwif_t *hwif)
696 {
697         ide_drive_t *drive;
698         int i, err;
699
700         if (hwif->acpidata == NULL)
701                 return;
702
703         /*
704          * The ACPI spec mandates that we send information
705          * for both drives, regardless whether they are connected
706          * or not.
707          */
708         hwif->devices[0]->acpidata = &hwif->acpidata->master;
709         hwif->devices[1]->acpidata = &hwif->acpidata->slave;
710
711         /*
712          * Send IDENTIFY for each drive
713          */
714         ide_port_for_each_dev(i, drive, hwif) {
715                 memset(drive->acpidata, 0, sizeof(*drive->acpidata));
716
717                 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
718                         continue;
719
720                 err = taskfile_lib_get_identify(drive, drive->acpidata->idbuff);
721                 if (err)
722                         DEBPRINT("identify device %s failed (%d)\n",
723                                  drive->name, err);
724         }
725
726         if (!ide_acpionboot) {
727                 DEBPRINT("ACPI methods disabled on boot\n");
728                 return;
729         }
730
731         /* ACPI _PS0 before _STM */
732         ide_acpi_set_state(hwif, 1);
733         /*
734          * ACPI requires us to call _STM on startup
735          */
736         ide_acpi_get_timing(hwif);
737         ide_acpi_push_timing(hwif);
738
739         ide_port_for_each_dev(i, drive, hwif) {
740                 if (drive->dev_flags & IDE_DFLAG_PRESENT)
741                         /* Execute ACPI startup code */
742                         ide_acpi_exec_tfs(drive);
743         }
744 }