2 * Provides ACPI support for IDE drives.
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
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>
20 #include <acpi/acpi_bus.h>
22 #define REGS_PER_GTF 7
32 struct ide_acpi_drive_link {
33 acpi_handle obj_handle;
37 struct ide_acpi_hwif_link {
39 acpi_handle obj_handle;
40 struct GTM_buffer gtm;
41 struct ide_acpi_drive_link master;
42 struct ide_acpi_drive_link slave;
46 /* note: adds function name and KERN_DEBUG */
48 #define DEBPRINT(fmt, args...) \
49 printk(KERN_DEBUG "%s: " fmt, __func__, ## args)
51 #define DEBPRINT(fmt, args...) do {} while (0)
52 #endif /* DEBUGGING */
54 static int ide_noacpi;
55 module_param_named(noacpi, ide_noacpi, bool, 0);
56 MODULE_PARM_DESC(noacpi, "disable IDE ACPI support");
58 static int ide_acpigtf;
59 module_param_named(acpigtf, ide_acpigtf, bool, 0);
60 MODULE_PARM_DESC(acpigtf, "enable IDE ACPI _GTF support");
62 static int ide_acpionboot;
63 module_param_named(acpionboot, ide_acpionboot, bool, 0);
64 MODULE_PARM_DESC(acpionboot, "call IDE ACPI methods on boot");
66 static bool ide_noacpi_psx;
67 static int no_acpi_psx(const struct dmi_system_id *id)
69 ide_noacpi_psx = true;
70 printk(KERN_NOTICE"%s detected - disable ACPI _PSx.\n", id->ident);
74 static const struct dmi_system_id ide_acpi_dmi_table[] = {
76 /* We should check if this is because ACPI NVS isn't save/restored. */
78 .callback = no_acpi_psx,
81 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies Ltd."),
82 DMI_MATCH(DMI_BIOS_VERSION, "KAM1.60")
86 { } /* terminate list */
89 int ide_acpi_init(void)
91 dmi_check_system(ide_acpi_dmi_table);
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
101 * Returns the ACPI object handle to the corresponding PCI device.
103 * Returns 0 on success, <0 on error.
105 static int ide_get_dev_handle(struct device *dev, acpi_handle *handle,
106 acpi_integer *pcidevfn)
108 struct pci_dev *pdev = to_pci_dev(dev);
109 unsigned int bus, devnum, func;
111 acpi_handle dev_handle;
112 struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
115 struct acpi_device_info *dinfo = NULL;
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);
124 DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
126 dev_handle = DEVICE_ACPI_HANDLE(dev);
128 DEBPRINT("no acpi handle for device\n");
132 status = acpi_get_object_info(dev_handle, &buffer);
133 if (ACPI_FAILURE(status)) {
134 DEBPRINT("get_object_info for device failed\n");
137 dinfo = buffer.pointer;
138 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
139 dinfo->address == addr) {
141 *handle = dev_handle;
143 DEBPRINT("get_object_info for device has wrong "
144 " address: %llu, should be %u\n",
145 dinfo ? (unsigned long long)dinfo->address : -1ULL,
150 DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
151 devnum, func, (unsigned long long)addr, *handle);
159 * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
160 * @hwif: device to locate
162 * Retrieves the object handle for a given hwif.
164 * Returns handle on success, 0 on error.
166 static acpi_handle ide_acpi_hwif_get_handle(ide_hwif_t *hwif)
168 struct device *dev = hwif->gendev.parent;
169 acpi_handle uninitialized_var(dev_handle);
170 acpi_integer pcidevfn;
171 acpi_handle chan_handle;
174 DEBPRINT("ENTER: device %s\n", hwif->name);
177 DEBPRINT("no PCI device for %s\n", hwif->name);
181 err = ide_get_dev_handle(dev, &dev_handle, &pcidevfn);
183 DEBPRINT("ide_get_dev_handle failed (%d)\n", err);
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);
198 * 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
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.
209 * The returned @gtf_length and @gtf_address are only valid if the
210 * function return value is 0.
212 static int do_drive_get_GTF(ide_drive_t *drive,
213 unsigned int *gtf_length, unsigned long *gtf_address,
214 unsigned long *obj_loc)
217 struct acpi_buffer output;
218 union acpi_object *out_obj;
225 if (!drive->acpidata->obj_handle) {
226 DEBPRINT("No ACPI object found for %s\n", drive->name);
230 /* Setting up output buffer */
231 output.length = ACPI_ALLOCATE_BUFFER;
232 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
234 /* _GTF has no input parameters */
236 status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
238 if (ACPI_FAILURE(status)) {
240 "%s: Run _GTF error: status = 0x%x\n",
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,
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);
259 kfree(output.pointer);
263 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
264 out_obj->buffer.length % REGS_PER_GTF) {
266 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
267 __func__, out_obj->buffer.length,
268 out_obj->buffer.pointer);
270 kfree(output.pointer);
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);
285 * 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
290 * Write {gtf_address, length gtf_length} in groups of
291 * REGS_PER_GTF bytes.
293 static int do_drive_set_taskfiles(ide_drive_t *drive,
294 unsigned int gtf_length,
295 unsigned long gtf_address)
298 int gtf_count = gtf_length / REGS_PER_GTF;
301 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
302 gtf_length, gtf_length, gtf_count, gtf_address);
304 /* send all taskfile registers (0x1f1-0x1f7) *in*that*order* */
305 for (ix = 0; ix < gtf_count; ix++) {
306 u8 *gtf = (u8 *)(gtf_address + ix * REGS_PER_GTF);
309 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]);
315 DEBPRINT("_GTF execution disabled\n");
319 /* convert GTF to taskfile */
320 memset(&cmd, 0, sizeof(cmd));
321 memcpy(&cmd.tf.feature, gtf, REGS_PER_GTF);
322 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
323 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
325 err = ide_no_data_taskfile(drive, &cmd);
327 printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
337 * ide_acpi_exec_tfs - get then write drive taskfile settings
338 * @drive: the drive for which the taskfile settings should be
341 * According to the ACPI spec this should be called after _STM
342 * has been evaluated for the interface. Some ACPI vendors interpret
343 * that as a hard requirement and modify the taskfile according
344 * to the Identify Drive information passed down with _STM.
345 * So one should really make sure to call this only after _STM has
348 int ide_acpi_exec_tfs(ide_drive_t *drive)
351 unsigned int gtf_length;
352 unsigned long gtf_address;
353 unsigned long obj_loc;
358 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
360 ret = do_drive_get_GTF(drive, >f_length, >f_address, &obj_loc);
362 DEBPRINT("get_GTF error (%d)\n", ret);
366 DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
368 ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
369 kfree((void *)obj_loc);
371 DEBPRINT("set_taskfiles error (%d)\n", ret);
374 DEBPRINT("ret=%d\n", ret);
380 * ide_acpi_get_timing - get the channel (controller) timings
381 * @hwif: target IDE interface (channel)
383 * This function executes the _GTM ACPI method for the target channel.
386 void ide_acpi_get_timing(ide_hwif_t *hwif)
389 struct acpi_buffer output;
390 union acpi_object *out_obj;
395 DEBPRINT("ENTER:\n");
397 if (!hwif->acpidata) {
398 DEBPRINT("no ACPI data for %s\n", hwif->name);
402 /* Setting up output buffer for _GTM */
403 output.length = ACPI_ALLOCATE_BUFFER;
404 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
406 /* _GTM has no input parameters */
407 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
410 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
411 status, output.pointer,
412 (unsigned long long)output.length);
414 if (ACPI_FAILURE(status)) {
415 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
419 if (!output.length || !output.pointer) {
420 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
421 (unsigned long long)output.length,
423 kfree(output.pointer);
427 out_obj = output.pointer;
428 if (out_obj->type != ACPI_TYPE_BUFFER) {
429 kfree(output.pointer);
430 DEBPRINT("Run _GTM: error: "
431 "expected object type of ACPI_TYPE_BUFFER, "
432 "got 0x%x\n", out_obj->type);
436 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
437 out_obj->buffer.length != sizeof(struct GTM_buffer)) {
438 kfree(output.pointer);
440 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
442 __func__, out_obj->buffer.length,
443 sizeof(struct GTM_buffer), out_obj->buffer.pointer);
447 memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
448 sizeof(struct GTM_buffer));
450 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
451 out_obj->buffer.pointer, out_obj->buffer.length,
452 sizeof(struct GTM_buffer));
454 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
455 hwif->acpidata->gtm.PIO_speed0,
456 hwif->acpidata->gtm.DMA_speed0,
457 hwif->acpidata->gtm.PIO_speed1,
458 hwif->acpidata->gtm.DMA_speed1,
459 hwif->acpidata->gtm.GTM_flags);
461 kfree(output.pointer);
465 * ide_acpi_push_timing - set the channel (controller) timings
466 * @hwif: target IDE interface (channel)
468 * This function executes the _STM ACPI method for the target channel.
470 * _STM requires Identify Drive data, which has to passed as an argument.
471 * Unfortunately drive->id is a mangled version which we can't readily
472 * use; hence we'll get the information afresh.
474 void ide_acpi_push_timing(ide_hwif_t *hwif)
477 struct acpi_object_list input;
478 union acpi_object in_params[3];
479 struct ide_acpi_drive_link *master = &hwif->acpidata->master;
480 struct ide_acpi_drive_link *slave = &hwif->acpidata->slave;
485 DEBPRINT("ENTER:\n");
487 if (!hwif->acpidata) {
488 DEBPRINT("no ACPI data for %s\n", hwif->name);
492 /* Give the GTM buffer + drive Identify data to the channel via the
494 /* setup input parameters buffer for _STM */
496 input.pointer = in_params;
497 in_params[0].type = ACPI_TYPE_BUFFER;
498 in_params[0].buffer.length = sizeof(struct GTM_buffer);
499 in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
500 in_params[1].type = ACPI_TYPE_BUFFER;
501 in_params[1].buffer.length = ATA_ID_WORDS * 2;
502 in_params[1].buffer.pointer = (u8 *)&master->idbuff;
503 in_params[2].type = ACPI_TYPE_BUFFER;
504 in_params[2].buffer.length = ATA_ID_WORDS * 2;
505 in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
506 /* Output buffer: _STM has no output */
508 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
511 if (ACPI_FAILURE(status)) {
512 DEBPRINT("Run _STM error: status = 0x%x\n", status);
514 DEBPRINT("_STM status: %d\n", status);
518 * ide_acpi_set_state - set the channel power state
519 * @hwif: target IDE interface
522 * This function executes the _PS0/_PS3 ACPI method to set the power state.
523 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
525 void ide_acpi_set_state(ide_hwif_t *hwif, int on)
530 if (ide_noacpi || ide_noacpi_psx)
533 DEBPRINT("ENTER:\n");
535 if (!hwif->acpidata) {
536 DEBPRINT("no ACPI data for %s\n", hwif->name);
540 /* channel first and then drives for power on and verse versa for power off */
542 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
544 ide_port_for_each_present_dev(i, drive, hwif) {
545 if (drive->acpidata->obj_handle)
546 acpi_bus_set_power(drive->acpidata->obj_handle,
547 on ? ACPI_STATE_D0 : ACPI_STATE_D3);
551 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
555 * ide_acpi_init_port - initialize the ACPI link for an IDE interface
556 * @hwif: target IDE interface (channel)
558 * The ACPI spec is not quite clear when the drive identify buffer
559 * should be obtained. Calling IDENTIFY DEVICE during shutdown
560 * is not the best of ideas as the drive might already being put to
561 * sleep. And obviously we can't call it during resume.
562 * So we get the information during startup; but this means that
563 * any changes during run-time will be lost after resume.
565 void ide_acpi_init_port(ide_hwif_t *hwif)
567 hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
571 hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
572 if (!hwif->acpidata->obj_handle) {
573 DEBPRINT("no ACPI object for %s found\n", hwif->name);
574 kfree(hwif->acpidata);
575 hwif->acpidata = NULL;
579 void ide_acpi_port_init_devices(ide_hwif_t *hwif)
584 if (hwif->acpidata == NULL)
588 * The ACPI spec mandates that we send information
589 * for both drives, regardless whether they are connected
592 hwif->devices[0]->acpidata = &hwif->acpidata->master;
593 hwif->devices[1]->acpidata = &hwif->acpidata->slave;
595 /* get _ADR info for each device */
596 ide_port_for_each_present_dev(i, drive, hwif) {
597 acpi_handle dev_handle;
599 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
600 drive->name, hwif->channel, drive->dn & 1);
602 /* TBD: could also check ACPI object VALID bits */
603 dev_handle = acpi_get_child(hwif->acpidata->obj_handle,
606 DEBPRINT("drive %s handle 0x%p\n", drive->name, dev_handle);
608 drive->acpidata->obj_handle = dev_handle;
611 /* send IDENTIFY for each device */
612 ide_port_for_each_present_dev(i, drive, hwif) {
613 err = taskfile_lib_get_identify(drive, drive->acpidata->idbuff);
615 DEBPRINT("identify device %s failed (%d)\n",
619 if (!ide_acpionboot) {
620 DEBPRINT("ACPI methods disabled on boot\n");
624 /* ACPI _PS0 before _STM */
625 ide_acpi_set_state(hwif, 1);
627 * ACPI requires us to call _STM on startup
629 ide_acpi_get_timing(hwif);
630 ide_acpi_push_timing(hwif);
632 ide_port_for_each_present_dev(i, drive, hwif) {
633 ide_acpi_exec_tfs(drive);