blob: 5b704f1ea90c901f446897ca21715b8f558bbd64 [file] [log] [blame]
Hannes Reineckee3a59b42007-02-07 18:19:37 +01001/*
Hannes Reineckee3a59b42007-02-07 18:19:37 +01002 * 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>
Shaohua Li90494892008-01-10 23:03:42 +010018#include <linux/dmi.h>
Hannes Reineckee3a59b42007-02-07 18:19:37 +010019
20#include <acpi/acpi_bus.h>
Hannes Reineckee3a59b42007-02-07 18:19:37 +010021
22#define REGS_PER_GTF 7
Hannes Reineckee3a59b42007-02-07 18:19:37 +010023
24struct GTM_buffer {
25 u32 PIO_speed0;
26 u32 DMA_speed0;
27 u32 PIO_speed1;
28 u32 DMA_speed1;
29 u32 GTM_flags;
30};
31
32struct ide_acpi_drive_link {
Hannes Reineckee3a59b42007-02-07 18:19:37 +010033 acpi_handle obj_handle;
34 u8 idbuff[512];
35};
36
37struct ide_acpi_hwif_link {
38 ide_hwif_t *hwif;
39 acpi_handle obj_handle;
40 struct GTM_buffer gtm;
41 struct ide_acpi_drive_link master;
42 struct ide_acpi_drive_link slave;
43};
44
45#undef DEBUGGING
46/* note: adds function name and KERN_DEBUG */
47#ifdef DEBUGGING
48#define DEBPRINT(fmt, args...) \
Harvey Harrisoneb639632008-04-26 22:25:20 +020049 printk(KERN_DEBUG "%s: " fmt, __func__, ## args)
Hannes Reineckee3a59b42007-02-07 18:19:37 +010050#else
51#define DEBPRINT(fmt, args...) do {} while (0)
52#endif /* DEBUGGING */
53
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +020054static int ide_noacpi;
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +020055module_param_named(noacpi, ide_noacpi, bool, 0);
56MODULE_PARM_DESC(noacpi, "disable IDE ACPI support");
57
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +020058static int ide_acpigtf;
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +020059module_param_named(acpigtf, ide_acpigtf, bool, 0);
60MODULE_PARM_DESC(acpigtf, "enable IDE ACPI _GTF support");
61
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +020062static int ide_acpionboot;
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +020063module_param_named(acpionboot, ide_acpionboot, bool, 0);
64MODULE_PARM_DESC(acpionboot, "call IDE ACPI methods on boot");
Hannes Reineckee3a59b42007-02-07 18:19:37 +010065
Shaohua Li90494892008-01-10 23:03:42 +010066static bool ide_noacpi_psx;
67static int no_acpi_psx(const struct dmi_system_id *id)
68{
69 ide_noacpi_psx = true;
70 printk(KERN_NOTICE"%s detected - disable ACPI _PSx.\n", id->ident);
71 return 0;
72}
73
74static const struct dmi_system_id ide_acpi_dmi_table[] = {
75 /* Bug 9673. */
76 /* We should check if this is because ACPI NVS isn't save/restored. */
77 {
78 .callback = no_acpi_psx,
79 .ident = "HP nx9005",
80 .matches = {
81 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies Ltd."),
82 DMI_MATCH(DMI_BIOS_VERSION, "KAM1.60")
83 },
84 },
Jeff Garzik7c48c562008-01-10 23:59:18 -050085
86 { } /* terminate list */
Shaohua Li90494892008-01-10 23:03:42 +010087};
88
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +010089int ide_acpi_init(void)
Shaohua Li90494892008-01-10 23:03:42 +010090{
Shaohua Li90494892008-01-10 23:03:42 +010091 dmi_check_system(ide_acpi_dmi_table);
92 return 0;
93}
94
Hannes Reineckee3a59b42007-02-07 18:19:37 +010095/**
96 * ide_get_dev_handle - finds acpi_handle and PCI device.function
97 * @dev: device to locate
98 * @handle: returned acpi_handle for @dev
99 * @pcidevfn: return PCI device.func for @dev
100 *
101 * Returns the ACPI object handle to the corresponding PCI device.
102 *
103 * Returns 0 on success, <0 on error.
104 */
105static int ide_get_dev_handle(struct device *dev, acpi_handle *handle,
106 acpi_integer *pcidevfn)
107{
108 struct pci_dev *pdev = to_pci_dev(dev);
109 unsigned int bus, devnum, func;
110 acpi_integer addr;
111 acpi_handle dev_handle;
112 struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
113 .pointer = NULL};
114 acpi_status status;
115 struct acpi_device_info *dinfo = NULL;
116 int ret = -ENODEV;
117
118 bus = pdev->bus->number;
119 devnum = PCI_SLOT(pdev->devfn);
120 func = PCI_FUNC(pdev->devfn);
121 /* ACPI _ADR encoding for PCI bus: */
122 addr = (acpi_integer)(devnum << 16 | func);
123
124 DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
125
126 dev_handle = DEVICE_ACPI_HANDLE(dev);
127 if (!dev_handle) {
128 DEBPRINT("no acpi handle for device\n");
129 goto err;
130 }
131
132 status = acpi_get_object_info(dev_handle, &buffer);
133 if (ACPI_FAILURE(status)) {
134 DEBPRINT("get_object_info for device failed\n");
135 goto err;
136 }
137 dinfo = buffer.pointer;
138 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
139 dinfo->address == addr) {
140 *pcidevfn = addr;
141 *handle = dev_handle;
142 } else {
143 DEBPRINT("get_object_info for device has wrong "
144 " address: %llu, should be %u\n",
145 dinfo ? (unsigned long long)dinfo->address : -1ULL,
146 (unsigned int)addr);
147 goto err;
148 }
149
150 DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
151 devnum, func, (unsigned long long)addr, *handle);
152 ret = 0;
153err:
154 kfree(dinfo);
155 return ret;
156}
157
158/**
159 * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
160 * @hwif: device to locate
161 *
162 * Retrieves the object handle for a given hwif.
163 *
164 * Returns handle on success, 0 on error.
165 */
166static acpi_handle ide_acpi_hwif_get_handle(ide_hwif_t *hwif)
167{
168 struct device *dev = hwif->gendev.parent;
Andrew Morton1dcfdf92008-02-06 02:57:49 +0100169 acpi_handle uninitialized_var(dev_handle);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100170 acpi_integer pcidevfn;
171 acpi_handle chan_handle;
172 int err;
173
174 DEBPRINT("ENTER: device %s\n", hwif->name);
175
176 if (!dev) {
177 DEBPRINT("no PCI device for %s\n", hwif->name);
178 return NULL;
179 }
180
181 err = ide_get_dev_handle(dev, &dev_handle, &pcidevfn);
182 if (err < 0) {
183 DEBPRINT("ide_get_dev_handle failed (%d)\n", err);
184 return NULL;
185 }
186
187 /* get child objects of dev_handle == channel objects,
188 * + _their_ children == drive objects */
189 /* channel is hwif->channel */
190 chan_handle = acpi_get_child(dev_handle, hwif->channel);
191 DEBPRINT("chan adr=%d: handle=0x%p\n",
192 hwif->channel, chan_handle);
193
194 return chan_handle;
195}
196
197/**
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100198 * do_drive_get_GTF - get the drive bootup default taskfile settings
199 * @drive: the drive for which the taskfile settings should be retrieved
200 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
201 * @gtf_address: buffer containing _GTF taskfile arrays
202 *
203 * The _GTF method has no input parameters.
204 * It returns a variable number of register set values (registers
205 * hex 1F1..1F7, taskfiles).
206 * The <variable number> is not known in advance, so have ACPI-CA
207 * allocate the buffer as needed and return it, then free it later.
208 *
209 * The returned @gtf_length and @gtf_address are only valid if the
210 * function return value is 0.
211 */
212static int do_drive_get_GTF(ide_drive_t *drive,
213 unsigned int *gtf_length, unsigned long *gtf_address,
214 unsigned long *obj_loc)
215{
216 acpi_status status;
217 struct acpi_buffer output;
218 union acpi_object *out_obj;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100219 int err = -ENODEV;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100220
221 *gtf_length = 0;
222 *gtf_address = 0UL;
223 *obj_loc = 0UL;
224
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100225 if (!drive->acpidata->obj_handle) {
Bartlomiej Zolnierkiewicz8cd3c602009-03-24 23:22:41 +0100226 DEBPRINT("No ACPI object found for %s\n", drive->name);
227 goto out;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100228 }
229
230 /* Setting up output buffer */
231 output.length = ACPI_ALLOCATE_BUFFER;
232 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
233
234 /* _GTF has no input parameters */
235 err = -EIO;
236 status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
237 NULL, &output);
238 if (ACPI_FAILURE(status)) {
239 printk(KERN_DEBUG
240 "%s: Run _GTF error: status = 0x%x\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200241 __func__, status);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100242 goto out;
243 }
244
245 if (!output.length || !output.pointer) {
246 DEBPRINT("Run _GTF: "
247 "length or ptr is NULL (0x%llx, 0x%p)\n",
248 (unsigned long long)output.length,
249 output.pointer);
250 goto out;
251 }
252
253 out_obj = output.pointer;
254 if (out_obj->type != ACPI_TYPE_BUFFER) {
255 DEBPRINT("Run _GTF: error: "
256 "expected object type of ACPI_TYPE_BUFFER, "
257 "got 0x%x\n", out_obj->type);
258 err = -ENOENT;
259 kfree(output.pointer);
260 goto out;
261 }
262
263 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
264 out_obj->buffer.length % REGS_PER_GTF) {
265 printk(KERN_ERR
266 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200267 __func__, out_obj->buffer.length,
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100268 out_obj->buffer.pointer);
269 err = -ENOENT;
270 kfree(output.pointer);
271 goto out;
272 }
273
274 *gtf_length = out_obj->buffer.length;
275 *gtf_address = (unsigned long)out_obj->buffer.pointer;
276 *obj_loc = (unsigned long)out_obj;
277 DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
278 *gtf_length, *gtf_address, *obj_loc);
279 err = 0;
280out:
281 return err;
282}
283
284/**
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100285 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
286 * @drive: the drive to which the taskfile command should be sent
287 * @gtf_length: total number of bytes of _GTF taskfiles
288 * @gtf_address: location of _GTF taskfile arrays
289 *
290 * Write {gtf_address, length gtf_length} in groups of
291 * REGS_PER_GTF bytes.
292 */
293static int do_drive_set_taskfiles(ide_drive_t *drive,
294 unsigned int gtf_length,
295 unsigned long gtf_address)
296{
Bartlomiej Zolnierkiewicz1f5892a2009-03-24 23:22:42 +0100297 int rc = 0, err;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100298 int gtf_count = gtf_length / REGS_PER_GTF;
299 int ix;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100300
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100301 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
302 gtf_length, gtf_length, gtf_count, gtf_address);
303
Bartlomiej Zolnierkiewiczb0b39142009-03-24 23:22:42 +0100304 /* send all taskfile registers (0x1f1-0x1f7) *in*that*order* */
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100305 for (ix = 0; ix < gtf_count; ix++) {
Bartlomiej Zolnierkiewiczb0b39142009-03-24 23:22:42 +0100306 u8 *gtf = (u8 *)(gtf_address + ix * REGS_PER_GTF);
307 ide_task_t task;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100308
Bartlomiej Zolnierkiewiczb0b39142009-03-24 23:22:42 +0100309 DEBPRINT("(0x1f1-1f7): "
310 "hex: %02x %02x %02x %02x %02x %02x %02x\n",
311 gtf[0], gtf[1], gtf[2],
312 gtf[3], gtf[4], gtf[5], gtf[6]);
313
314 if (!ide_acpigtf) {
315 DEBPRINT("_GTF execution disabled\n");
316 continue;
317 }
318
319 /* convert GTF to taskfile */
320 memset(&task, 0, sizeof(ide_task_t));
321 memcpy(&task.tf_array[7], gtf, REGS_PER_GTF);
322 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
323
324 err = ide_no_data_taskfile(drive, &task);
325 if (err) {
326 printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
327 __func__, err);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100328 rc = err;
Bartlomiej Zolnierkiewiczb0b39142009-03-24 23:22:42 +0100329 }
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100330 }
331
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100332 return rc;
333}
334
335/**
336 * ide_acpi_exec_tfs - get then write drive taskfile settings
337 * @drive: the drive for which the taskfile settings should be
338 * written.
339 *
340 * According to the ACPI spec this should be called after _STM
341 * has been evaluated for the interface. Some ACPI vendors interpret
342 * that as a hard requirement and modify the taskfile according
343 * to the Identify Drive information passed down with _STM.
344 * So one should really make sure to call this only after _STM has
345 * been executed.
346 */
347int ide_acpi_exec_tfs(ide_drive_t *drive)
348{
349 int ret;
350 unsigned int gtf_length;
351 unsigned long gtf_address;
352 unsigned long obj_loc;
353
354 if (ide_noacpi)
355 return 0;
356
357 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
358
359 ret = do_drive_get_GTF(drive, &gtf_length, &gtf_address, &obj_loc);
360 if (ret < 0) {
361 DEBPRINT("get_GTF error (%d)\n", ret);
362 return ret;
363 }
364
365 DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
366
367 ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
368 kfree((void *)obj_loc);
369 if (ret < 0) {
370 DEBPRINT("set_taskfiles error (%d)\n", ret);
371 }
372
373 DEBPRINT("ret=%d\n", ret);
374
375 return ret;
376}
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100377
378/**
379 * ide_acpi_get_timing - get the channel (controller) timings
380 * @hwif: target IDE interface (channel)
381 *
382 * This function executes the _GTM ACPI method for the target channel.
383 *
384 */
385void ide_acpi_get_timing(ide_hwif_t *hwif)
386{
387 acpi_status status;
388 struct acpi_buffer output;
389 union acpi_object *out_obj;
390
391 if (ide_noacpi)
392 return;
393
394 DEBPRINT("ENTER:\n");
395
396 if (!hwif->acpidata) {
397 DEBPRINT("no ACPI data for %s\n", hwif->name);
398 return;
399 }
400
401 /* Setting up output buffer for _GTM */
402 output.length = ACPI_ALLOCATE_BUFFER;
403 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
404
405 /* _GTM has no input parameters */
406 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
407 NULL, &output);
408
409 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
410 status, output.pointer,
411 (unsigned long long)output.length);
412
413 if (ACPI_FAILURE(status)) {
414 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
415 return;
416 }
417
418 if (!output.length || !output.pointer) {
419 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
420 (unsigned long long)output.length,
421 output.pointer);
422 kfree(output.pointer);
423 return;
424 }
425
426 out_obj = output.pointer;
427 if (out_obj->type != ACPI_TYPE_BUFFER) {
428 kfree(output.pointer);
429 DEBPRINT("Run _GTM: error: "
430 "expected object type of ACPI_TYPE_BUFFER, "
431 "got 0x%x\n", out_obj->type);
432 return;
433 }
434
435 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
436 out_obj->buffer.length != sizeof(struct GTM_buffer)) {
437 kfree(output.pointer);
438 printk(KERN_ERR
Andrew Morton1e8f34f2007-02-07 18:19:42 +0100439 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
440 "addr (0x%p)\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200441 __func__, out_obj->buffer.length,
Andrew Morton1e8f34f2007-02-07 18:19:42 +0100442 sizeof(struct GTM_buffer), out_obj->buffer.pointer);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100443 return;
444 }
445
446 memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
447 sizeof(struct GTM_buffer));
448
449 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
450 out_obj->buffer.pointer, out_obj->buffer.length,
451 sizeof(struct GTM_buffer));
452
453 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
454 hwif->acpidata->gtm.PIO_speed0,
455 hwif->acpidata->gtm.DMA_speed0,
456 hwif->acpidata->gtm.PIO_speed1,
457 hwif->acpidata->gtm.DMA_speed1,
458 hwif->acpidata->gtm.GTM_flags);
459
460 kfree(output.pointer);
461}
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100462
463/**
464 * ide_acpi_push_timing - set the channel (controller) timings
465 * @hwif: target IDE interface (channel)
466 *
467 * This function executes the _STM ACPI method for the target channel.
468 *
469 * _STM requires Identify Drive data, which has to passed as an argument.
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200470 * Unfortunately drive->id is a mangled version which we can't readily
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100471 * use; hence we'll get the information afresh.
472 */
473void ide_acpi_push_timing(ide_hwif_t *hwif)
474{
475 acpi_status status;
476 struct acpi_object_list input;
477 union acpi_object in_params[3];
478 struct ide_acpi_drive_link *master = &hwif->acpidata->master;
479 struct ide_acpi_drive_link *slave = &hwif->acpidata->slave;
480
481 if (ide_noacpi)
482 return;
483
484 DEBPRINT("ENTER:\n");
485
486 if (!hwif->acpidata) {
487 DEBPRINT("no ACPI data for %s\n", hwif->name);
488 return;
489 }
490
491 /* Give the GTM buffer + drive Identify data to the channel via the
492 * _STM method: */
493 /* setup input parameters buffer for _STM */
494 input.count = 3;
495 input.pointer = in_params;
496 in_params[0].type = ACPI_TYPE_BUFFER;
497 in_params[0].buffer.length = sizeof(struct GTM_buffer);
498 in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
499 in_params[1].type = ACPI_TYPE_BUFFER;
Bartlomiej Zolnierkiewicz0e63a582008-12-29 20:27:29 +0100500 in_params[1].buffer.length = ATA_ID_WORDS * 2;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100501 in_params[1].buffer.pointer = (u8 *)&master->idbuff;
502 in_params[2].type = ACPI_TYPE_BUFFER;
Bartlomiej Zolnierkiewicz0e63a582008-12-29 20:27:29 +0100503 in_params[2].buffer.length = ATA_ID_WORDS * 2;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100504 in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
505 /* Output buffer: _STM has no output */
506
507 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
508 &input, NULL);
509
510 if (ACPI_FAILURE(status)) {
511 DEBPRINT("Run _STM error: status = 0x%x\n", status);
512 }
513 DEBPRINT("_STM status: %d\n", status);
514}
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100515
516/**
Shaohua Li5e321322007-10-11 23:53:58 +0200517 * ide_acpi_set_state - set the channel power state
518 * @hwif: target IDE interface
519 * @on: state, on/off
520 *
521 * This function executes the _PS0/_PS3 ACPI method to set the power state.
522 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
523 */
524void ide_acpi_set_state(ide_hwif_t *hwif, int on)
525{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100526 ide_drive_t *drive;
527 int i;
Shaohua Li5e321322007-10-11 23:53:58 +0200528
Shaohua Li90494892008-01-10 23:03:42 +0100529 if (ide_noacpi || ide_noacpi_psx)
Shaohua Li5e321322007-10-11 23:53:58 +0200530 return;
531
532 DEBPRINT("ENTER:\n");
533
534 if (!hwif->acpidata) {
535 DEBPRINT("no ACPI data for %s\n", hwif->name);
536 return;
537 }
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100538
Shaohua Li5e321322007-10-11 23:53:58 +0200539 /* channel first and then drives for power on and verse versa for power off */
540 if (on)
541 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
Shaohua Li5e321322007-10-11 23:53:58 +0200542
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100543 ide_port_for_each_present_dev(i, drive, hwif) {
544 if (drive->acpidata->obj_handle)
Shaohua Li5e321322007-10-11 23:53:58 +0200545 acpi_bus_set_power(drive->acpidata->obj_handle,
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100546 on ? ACPI_STATE_D0 : ACPI_STATE_D3);
Shaohua Li5e321322007-10-11 23:53:58 +0200547 }
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100548
Shaohua Li5e321322007-10-11 23:53:58 +0200549 if (!on)
550 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
551}
Shaohua Li5e321322007-10-11 23:53:58 +0200552
553/**
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +0100554 * ide_acpi_init_port - initialize the ACPI link for an IDE interface
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100555 * @hwif: target IDE interface (channel)
556 *
557 * The ACPI spec is not quite clear when the drive identify buffer
558 * should be obtained. Calling IDENTIFY DEVICE during shutdown
559 * is not the best of ideas as the drive might already being put to
560 * sleep. And obviously we can't call it during resume.
561 * So we get the information during startup; but this means that
562 * any changes during run-time will be lost after resume.
563 */
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +0100564void ide_acpi_init_port(ide_hwif_t *hwif)
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100565{
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100566 hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
567 if (!hwif->acpidata)
568 return;
569
570 hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
571 if (!hwif->acpidata->obj_handle) {
572 DEBPRINT("no ACPI object for %s found\n", hwif->name);
573 kfree(hwif->acpidata);
574 hwif->acpidata = NULL;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100575 }
Bartlomiej Zolnierkiewiczeafd88a2008-02-02 19:56:43 +0100576}
577
578void ide_acpi_port_init_devices(ide_hwif_t *hwif)
579{
580 ide_drive_t *drive;
581 int i, err;
582
583 if (hwif->acpidata == NULL)
584 return;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100585
586 /*
587 * The ACPI spec mandates that we send information
588 * for both drives, regardless whether they are connected
589 * or not.
590 */
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100591 hwif->devices[0]->acpidata = &hwif->acpidata->master;
592 hwif->devices[1]->acpidata = &hwif->acpidata->slave;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100593
Bartlomiej Zolnierkiewicz8cd3c602009-03-24 23:22:41 +0100594 /* get _ADR info for each device */
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100595 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz8cd3c602009-03-24 23:22:41 +0100596 acpi_handle dev_handle;
597
Bartlomiej Zolnierkiewicz8cd3c602009-03-24 23:22:41 +0100598 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
599 drive->name, hwif->channel, drive->dn & 1);
600
601 /* TBD: could also check ACPI object VALID bits */
602 dev_handle = acpi_get_child(hwif->acpidata->obj_handle,
603 drive->dn & 1);
604
605 DEBPRINT("drive %s handle 0x%p\n", drive->name, dev_handle);
606
607 drive->acpidata->obj_handle = dev_handle;
608 }
609
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100610 /* send IDENTIFY for each device */
611 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczeafd88a2008-02-02 19:56:43 +0100612 err = taskfile_lib_get_identify(drive, drive->acpidata->idbuff);
613 if (err)
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100614 DEBPRINT("identify device %s failed (%d)\n",
Bartlomiej Zolnierkiewiczeafd88a2008-02-02 19:56:43 +0100615 drive->name, err);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100616 }
617
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +0200618 if (!ide_acpionboot) {
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100619 DEBPRINT("ACPI methods disabled on boot\n");
620 return;
621 }
622
Shaohua Li5e321322007-10-11 23:53:58 +0200623 /* ACPI _PS0 before _STM */
624 ide_acpi_set_state(hwif, 1);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100625 /*
626 * ACPI requires us to call _STM on startup
627 */
628 ide_acpi_get_timing(hwif);
629 ide_acpi_push_timing(hwif);
630
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100631 ide_port_for_each_present_dev(i, drive, hwif) {
632 ide_acpi_exec_tfs(drive);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100633 }
634}