blob: ce5b213e2bc211efaeb4b803ef7dec40e7c021c9 [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
23struct taskfile_array {
24 u8 tfa[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
25};
26
27struct GTM_buffer {
28 u32 PIO_speed0;
29 u32 DMA_speed0;
30 u32 PIO_speed1;
31 u32 DMA_speed1;
32 u32 GTM_flags;
33};
34
35struct ide_acpi_drive_link {
Hannes Reineckee3a59b42007-02-07 18:19:37 +010036 acpi_handle obj_handle;
37 u8 idbuff[512];
38};
39
40struct 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...) \
Harvey Harrisoneb639632008-04-26 22:25:20 +020052 printk(KERN_DEBUG "%s: " fmt, __func__, ## args)
Hannes Reineckee3a59b42007-02-07 18:19:37 +010053#else
54#define DEBPRINT(fmt, args...) do {} while (0)
55#endif /* DEBUGGING */
56
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +020057static int ide_noacpi;
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +020058module_param_named(noacpi, ide_noacpi, bool, 0);
59MODULE_PARM_DESC(noacpi, "disable IDE ACPI support");
60
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +020061static int ide_acpigtf;
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +020062module_param_named(acpigtf, ide_acpigtf, bool, 0);
63MODULE_PARM_DESC(acpigtf, "enable IDE ACPI _GTF support");
64
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +020065static int ide_acpionboot;
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +020066module_param_named(acpionboot, ide_acpionboot, bool, 0);
67MODULE_PARM_DESC(acpionboot, "call IDE ACPI methods on boot");
Hannes Reineckee3a59b42007-02-07 18:19:37 +010068
Shaohua Li90494892008-01-10 23:03:42 +010069static bool ide_noacpi_psx;
70static 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
77static 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 },
Jeff Garzik7c48c562008-01-10 23:59:18 -050088
89 { } /* terminate list */
Shaohua Li90494892008-01-10 23:03:42 +010090};
91
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +010092int ide_acpi_init(void)
Shaohua Li90494892008-01-10 23:03:42 +010093{
Shaohua Li90494892008-01-10 23:03:42 +010094 dmi_check_system(ide_acpi_dmi_table);
95 return 0;
96}
97
Hannes Reineckee3a59b42007-02-07 18:19:37 +010098/**
99 * ide_get_dev_handle - finds acpi_handle and PCI device.function
100 * @dev: device to locate
101 * @handle: returned acpi_handle for @dev
102 * @pcidevfn: return PCI device.func for @dev
103 *
104 * Returns the ACPI object handle to the corresponding PCI device.
105 *
106 * Returns 0 on success, <0 on error.
107 */
108static int ide_get_dev_handle(struct device *dev, acpi_handle *handle,
109 acpi_integer *pcidevfn)
110{
111 struct pci_dev *pdev = to_pci_dev(dev);
112 unsigned int bus, devnum, func;
113 acpi_integer addr;
114 acpi_handle dev_handle;
115 struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
116 .pointer = NULL};
117 acpi_status status;
118 struct acpi_device_info *dinfo = NULL;
119 int ret = -ENODEV;
120
121 bus = pdev->bus->number;
122 devnum = PCI_SLOT(pdev->devfn);
123 func = PCI_FUNC(pdev->devfn);
124 /* ACPI _ADR encoding for PCI bus: */
125 addr = (acpi_integer)(devnum << 16 | func);
126
127 DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
128
129 dev_handle = DEVICE_ACPI_HANDLE(dev);
130 if (!dev_handle) {
131 DEBPRINT("no acpi handle for device\n");
132 goto err;
133 }
134
135 status = acpi_get_object_info(dev_handle, &buffer);
136 if (ACPI_FAILURE(status)) {
137 DEBPRINT("get_object_info for device failed\n");
138 goto err;
139 }
140 dinfo = buffer.pointer;
141 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
142 dinfo->address == addr) {
143 *pcidevfn = addr;
144 *handle = dev_handle;
145 } else {
146 DEBPRINT("get_object_info for device has wrong "
147 " address: %llu, should be %u\n",
148 dinfo ? (unsigned long long)dinfo->address : -1ULL,
149 (unsigned int)addr);
150 goto err;
151 }
152
153 DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
154 devnum, func, (unsigned long long)addr, *handle);
155 ret = 0;
156err:
157 kfree(dinfo);
158 return ret;
159}
160
161/**
162 * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
163 * @hwif: device to locate
164 *
165 * Retrieves the object handle for a given hwif.
166 *
167 * Returns handle on success, 0 on error.
168 */
169static acpi_handle ide_acpi_hwif_get_handle(ide_hwif_t *hwif)
170{
171 struct device *dev = hwif->gendev.parent;
Andrew Morton1dcfdf92008-02-06 02:57:49 +0100172 acpi_handle uninitialized_var(dev_handle);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100173 acpi_integer pcidevfn;
174 acpi_handle chan_handle;
175 int err;
176
177 DEBPRINT("ENTER: device %s\n", hwif->name);
178
179 if (!dev) {
180 DEBPRINT("no PCI device for %s\n", hwif->name);
181 return NULL;
182 }
183
184 err = ide_get_dev_handle(dev, &dev_handle, &pcidevfn);
185 if (err < 0) {
186 DEBPRINT("ide_get_dev_handle failed (%d)\n", err);
187 return NULL;
188 }
189
190 /* get child objects of dev_handle == channel objects,
191 * + _their_ children == drive objects */
192 /* channel is hwif->channel */
193 chan_handle = acpi_get_child(dev_handle, hwif->channel);
194 DEBPRINT("chan adr=%d: handle=0x%p\n",
195 hwif->channel, chan_handle);
196
197 return chan_handle;
198}
199
200/**
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100201 * do_drive_get_GTF - get the drive bootup default taskfile settings
202 * @drive: the drive for which the taskfile settings should be retrieved
203 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
204 * @gtf_address: buffer containing _GTF taskfile arrays
205 *
206 * The _GTF method has no input parameters.
207 * It returns a variable number of register set values (registers
208 * hex 1F1..1F7, taskfiles).
209 * The <variable number> is not known in advance, so have ACPI-CA
210 * allocate the buffer as needed and return it, then free it later.
211 *
212 * The returned @gtf_length and @gtf_address are only valid if the
213 * function return value is 0.
214 */
215static int do_drive_get_GTF(ide_drive_t *drive,
216 unsigned int *gtf_length, unsigned long *gtf_address,
217 unsigned long *obj_loc)
218{
219 acpi_status status;
220 struct acpi_buffer output;
221 union acpi_object *out_obj;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100222 int err = -ENODEV;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100223
224 *gtf_length = 0;
225 *gtf_address = 0UL;
226 *obj_loc = 0UL;
227
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100228 if (!drive->acpidata->obj_handle) {
Bartlomiej Zolnierkiewicz8cd3c602009-03-24 23:22:41 +0100229 DEBPRINT("No ACPI object found for %s\n", drive->name);
230 goto out;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100231 }
232
233 /* Setting up output buffer */
234 output.length = ACPI_ALLOCATE_BUFFER;
235 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
236
237 /* _GTF has no input parameters */
238 err = -EIO;
239 status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
240 NULL, &output);
241 if (ACPI_FAILURE(status)) {
242 printk(KERN_DEBUG
243 "%s: Run _GTF error: status = 0x%x\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200244 __func__, status);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100245 goto out;
246 }
247
248 if (!output.length || !output.pointer) {
249 DEBPRINT("Run _GTF: "
250 "length or ptr is NULL (0x%llx, 0x%p)\n",
251 (unsigned long long)output.length,
252 output.pointer);
253 goto out;
254 }
255
256 out_obj = output.pointer;
257 if (out_obj->type != ACPI_TYPE_BUFFER) {
258 DEBPRINT("Run _GTF: error: "
259 "expected object type of ACPI_TYPE_BUFFER, "
260 "got 0x%x\n", out_obj->type);
261 err = -ENOENT;
262 kfree(output.pointer);
263 goto out;
264 }
265
266 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
267 out_obj->buffer.length % REGS_PER_GTF) {
268 printk(KERN_ERR
269 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200270 __func__, out_obj->buffer.length,
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100271 out_obj->buffer.pointer);
272 err = -ENOENT;
273 kfree(output.pointer);
274 goto out;
275 }
276
277 *gtf_length = out_obj->buffer.length;
278 *gtf_address = (unsigned long)out_obj->buffer.pointer;
279 *obj_loc = (unsigned long)out_obj;
280 DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
281 *gtf_length, *gtf_address, *obj_loc);
282 err = 0;
283out:
284 return err;
285}
286
287/**
288 * taskfile_load_raw - send taskfile registers to drive
289 * @drive: drive to which output is sent
290 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
291 *
292 * Outputs IDE taskfile to the drive.
293 */
294static int taskfile_load_raw(ide_drive_t *drive,
295 const struct taskfile_array *gtf)
296{
297 ide_task_t args;
298 int err = 0;
299
300 DEBPRINT("(0x1f1-1f7): hex: "
301 "%02x %02x %02x %02x %02x %02x %02x\n",
302 gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
303 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
304
305 memset(&args, 0, sizeof(ide_task_t));
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100306
307 /* convert gtf to IDE Taskfile */
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100308 memcpy(&args.tf_array[7], &gtf->tfa, 7);
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100309 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100310
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +0200311 if (!ide_acpigtf) {
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100312 DEBPRINT("_GTF execution disabled\n");
313 return err;
314 }
315
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100316 err = ide_no_data_taskfile(drive, &args);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100317 if (err)
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100318 printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200319 __func__, err);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100320
321 return err;
322}
323
324/**
325 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
326 * @drive: the drive to which the taskfile command should be sent
327 * @gtf_length: total number of bytes of _GTF taskfiles
328 * @gtf_address: location of _GTF taskfile arrays
329 *
330 * Write {gtf_address, length gtf_length} in groups of
331 * REGS_PER_GTF bytes.
332 */
333static int do_drive_set_taskfiles(ide_drive_t *drive,
334 unsigned int gtf_length,
335 unsigned long gtf_address)
336{
Bartlomiej Zolnierkiewicz1f5892a2009-03-24 23:22:42 +0100337 int rc = 0, err;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100338 int gtf_count = gtf_length / REGS_PER_GTF;
339 int ix;
340 struct taskfile_array *gtf;
341
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100342 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
343 gtf_length, gtf_length, gtf_count, gtf_address);
344
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100345 for (ix = 0; ix < gtf_count; ix++) {
346 gtf = (struct taskfile_array *)
347 (gtf_address + ix * REGS_PER_GTF);
348
349 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
350 err = taskfile_load_raw(drive, gtf);
351 if (err)
352 rc = err;
353 }
354
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100355 return rc;
356}
357
358/**
359 * ide_acpi_exec_tfs - get then write drive taskfile settings
360 * @drive: the drive for which the taskfile settings should be
361 * written.
362 *
363 * According to the ACPI spec this should be called after _STM
364 * has been evaluated for the interface. Some ACPI vendors interpret
365 * that as a hard requirement and modify the taskfile according
366 * to the Identify Drive information passed down with _STM.
367 * So one should really make sure to call this only after _STM has
368 * been executed.
369 */
370int ide_acpi_exec_tfs(ide_drive_t *drive)
371{
372 int ret;
373 unsigned int gtf_length;
374 unsigned long gtf_address;
375 unsigned long obj_loc;
376
377 if (ide_noacpi)
378 return 0;
379
380 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
381
382 ret = do_drive_get_GTF(drive, &gtf_length, &gtf_address, &obj_loc);
383 if (ret < 0) {
384 DEBPRINT("get_GTF error (%d)\n", ret);
385 return ret;
386 }
387
388 DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
389
390 ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
391 kfree((void *)obj_loc);
392 if (ret < 0) {
393 DEBPRINT("set_taskfiles error (%d)\n", ret);
394 }
395
396 DEBPRINT("ret=%d\n", ret);
397
398 return ret;
399}
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100400
401/**
402 * ide_acpi_get_timing - get the channel (controller) timings
403 * @hwif: target IDE interface (channel)
404 *
405 * This function executes the _GTM ACPI method for the target channel.
406 *
407 */
408void ide_acpi_get_timing(ide_hwif_t *hwif)
409{
410 acpi_status status;
411 struct acpi_buffer output;
412 union acpi_object *out_obj;
413
414 if (ide_noacpi)
415 return;
416
417 DEBPRINT("ENTER:\n");
418
419 if (!hwif->acpidata) {
420 DEBPRINT("no ACPI data for %s\n", hwif->name);
421 return;
422 }
423
424 /* Setting up output buffer for _GTM */
425 output.length = ACPI_ALLOCATE_BUFFER;
426 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
427
428 /* _GTM has no input parameters */
429 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
430 NULL, &output);
431
432 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
433 status, output.pointer,
434 (unsigned long long)output.length);
435
436 if (ACPI_FAILURE(status)) {
437 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
438 return;
439 }
440
441 if (!output.length || !output.pointer) {
442 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
443 (unsigned long long)output.length,
444 output.pointer);
445 kfree(output.pointer);
446 return;
447 }
448
449 out_obj = output.pointer;
450 if (out_obj->type != ACPI_TYPE_BUFFER) {
451 kfree(output.pointer);
452 DEBPRINT("Run _GTM: error: "
453 "expected object type of ACPI_TYPE_BUFFER, "
454 "got 0x%x\n", out_obj->type);
455 return;
456 }
457
458 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
459 out_obj->buffer.length != sizeof(struct GTM_buffer)) {
460 kfree(output.pointer);
461 printk(KERN_ERR
Andrew Morton1e8f34f2007-02-07 18:19:42 +0100462 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
463 "addr (0x%p)\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200464 __func__, out_obj->buffer.length,
Andrew Morton1e8f34f2007-02-07 18:19:42 +0100465 sizeof(struct GTM_buffer), out_obj->buffer.pointer);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100466 return;
467 }
468
469 memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
470 sizeof(struct GTM_buffer));
471
472 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
473 out_obj->buffer.pointer, out_obj->buffer.length,
474 sizeof(struct GTM_buffer));
475
476 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
477 hwif->acpidata->gtm.PIO_speed0,
478 hwif->acpidata->gtm.DMA_speed0,
479 hwif->acpidata->gtm.PIO_speed1,
480 hwif->acpidata->gtm.DMA_speed1,
481 hwif->acpidata->gtm.GTM_flags);
482
483 kfree(output.pointer);
484}
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100485
486/**
487 * ide_acpi_push_timing - set the channel (controller) timings
488 * @hwif: target IDE interface (channel)
489 *
490 * This function executes the _STM ACPI method for the target channel.
491 *
492 * _STM requires Identify Drive data, which has to passed as an argument.
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200493 * Unfortunately drive->id is a mangled version which we can't readily
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100494 * use; hence we'll get the information afresh.
495 */
496void ide_acpi_push_timing(ide_hwif_t *hwif)
497{
498 acpi_status status;
499 struct acpi_object_list input;
500 union acpi_object in_params[3];
501 struct ide_acpi_drive_link *master = &hwif->acpidata->master;
502 struct ide_acpi_drive_link *slave = &hwif->acpidata->slave;
503
504 if (ide_noacpi)
505 return;
506
507 DEBPRINT("ENTER:\n");
508
509 if (!hwif->acpidata) {
510 DEBPRINT("no ACPI data for %s\n", hwif->name);
511 return;
512 }
513
514 /* Give the GTM buffer + drive Identify data to the channel via the
515 * _STM method: */
516 /* setup input parameters buffer for _STM */
517 input.count = 3;
518 input.pointer = in_params;
519 in_params[0].type = ACPI_TYPE_BUFFER;
520 in_params[0].buffer.length = sizeof(struct GTM_buffer);
521 in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
522 in_params[1].type = ACPI_TYPE_BUFFER;
Bartlomiej Zolnierkiewicz0e63a582008-12-29 20:27:29 +0100523 in_params[1].buffer.length = ATA_ID_WORDS * 2;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100524 in_params[1].buffer.pointer = (u8 *)&master->idbuff;
525 in_params[2].type = ACPI_TYPE_BUFFER;
Bartlomiej Zolnierkiewicz0e63a582008-12-29 20:27:29 +0100526 in_params[2].buffer.length = ATA_ID_WORDS * 2;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100527 in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
528 /* Output buffer: _STM has no output */
529
530 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
531 &input, NULL);
532
533 if (ACPI_FAILURE(status)) {
534 DEBPRINT("Run _STM error: status = 0x%x\n", status);
535 }
536 DEBPRINT("_STM status: %d\n", status);
537}
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100538
539/**
Shaohua Li5e321322007-10-11 23:53:58 +0200540 * ide_acpi_set_state - set the channel power state
541 * @hwif: target IDE interface
542 * @on: state, on/off
543 *
544 * This function executes the _PS0/_PS3 ACPI method to set the power state.
545 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
546 */
547void ide_acpi_set_state(ide_hwif_t *hwif, int on)
548{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100549 ide_drive_t *drive;
550 int i;
Shaohua Li5e321322007-10-11 23:53:58 +0200551
Shaohua Li90494892008-01-10 23:03:42 +0100552 if (ide_noacpi || ide_noacpi_psx)
Shaohua Li5e321322007-10-11 23:53:58 +0200553 return;
554
555 DEBPRINT("ENTER:\n");
556
557 if (!hwif->acpidata) {
558 DEBPRINT("no ACPI data for %s\n", hwif->name);
559 return;
560 }
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100561
Shaohua Li5e321322007-10-11 23:53:58 +0200562 /* channel first and then drives for power on and verse versa for power off */
563 if (on)
564 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
Shaohua Li5e321322007-10-11 23:53:58 +0200565
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100566 ide_port_for_each_present_dev(i, drive, hwif) {
567 if (drive->acpidata->obj_handle)
Shaohua Li5e321322007-10-11 23:53:58 +0200568 acpi_bus_set_power(drive->acpidata->obj_handle,
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100569 on ? ACPI_STATE_D0 : ACPI_STATE_D3);
Shaohua Li5e321322007-10-11 23:53:58 +0200570 }
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100571
Shaohua Li5e321322007-10-11 23:53:58 +0200572 if (!on)
573 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
574}
Shaohua Li5e321322007-10-11 23:53:58 +0200575
576/**
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +0100577 * ide_acpi_init_port - initialize the ACPI link for an IDE interface
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100578 * @hwif: target IDE interface (channel)
579 *
580 * The ACPI spec is not quite clear when the drive identify buffer
581 * should be obtained. Calling IDENTIFY DEVICE during shutdown
582 * is not the best of ideas as the drive might already being put to
583 * sleep. And obviously we can't call it during resume.
584 * So we get the information during startup; but this means that
585 * any changes during run-time will be lost after resume.
586 */
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +0100587void ide_acpi_init_port(ide_hwif_t *hwif)
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100588{
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100589 hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
590 if (!hwif->acpidata)
591 return;
592
593 hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
594 if (!hwif->acpidata->obj_handle) {
595 DEBPRINT("no ACPI object for %s found\n", hwif->name);
596 kfree(hwif->acpidata);
597 hwif->acpidata = NULL;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100598 }
Bartlomiej Zolnierkiewiczeafd88a2008-02-02 19:56:43 +0100599}
600
601void ide_acpi_port_init_devices(ide_hwif_t *hwif)
602{
603 ide_drive_t *drive;
604 int i, err;
605
606 if (hwif->acpidata == NULL)
607 return;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100608
609 /*
610 * The ACPI spec mandates that we send information
611 * for both drives, regardless whether they are connected
612 * or not.
613 */
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100614 hwif->devices[0]->acpidata = &hwif->acpidata->master;
615 hwif->devices[1]->acpidata = &hwif->acpidata->slave;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100616
Bartlomiej Zolnierkiewicz8cd3c602009-03-24 23:22:41 +0100617 /* get _ADR info for each device */
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100618 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz8cd3c602009-03-24 23:22:41 +0100619 acpi_handle dev_handle;
620
Bartlomiej Zolnierkiewicz8cd3c602009-03-24 23:22:41 +0100621 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
622 drive->name, hwif->channel, drive->dn & 1);
623
624 /* TBD: could also check ACPI object VALID bits */
625 dev_handle = acpi_get_child(hwif->acpidata->obj_handle,
626 drive->dn & 1);
627
628 DEBPRINT("drive %s handle 0x%p\n", drive->name, dev_handle);
629
630 drive->acpidata->obj_handle = dev_handle;
631 }
632
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100633 /* send IDENTIFY for each device */
634 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczeafd88a2008-02-02 19:56:43 +0100635 err = taskfile_lib_get_identify(drive, drive->acpidata->idbuff);
636 if (err)
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100637 DEBPRINT("identify device %s failed (%d)\n",
Bartlomiej Zolnierkiewiczeafd88a2008-02-02 19:56:43 +0100638 drive->name, err);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100639 }
640
Bartlomiej Zolnierkiewicz1dbfeb42008-04-27 15:38:30 +0200641 if (!ide_acpionboot) {
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100642 DEBPRINT("ACPI methods disabled on boot\n");
643 return;
644 }
645
Shaohua Li5e321322007-10-11 23:53:58 +0200646 /* ACPI _PS0 before _STM */
647 ide_acpi_set_state(hwif, 1);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100648 /*
649 * ACPI requires us to call _STM on startup
650 */
651 ide_acpi_get_timing(hwif);
652 ide_acpi_push_timing(hwif);
653
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100654 ide_port_for_each_present_dev(i, drive, hwif) {
655 ide_acpi_exec_tfs(drive);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100656 }
657}