3 * Provides ACPI support for IDE drives.
5 * Copyright (C) 2005 Intel Corp.
6 * Copyright (C) 2005 Randy Dunlap
7 * Copyright (C) 2006 SUSE Linux Products GmbH
8 * Copyright (C) 2006 Hannes Reinecke
11 #include <linux/ata.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <acpi/acpi.h>
17 #include <linux/ide.h>
18 #include <linux/pci.h>
19 #include <linux/dmi.h>
21 #include <acpi/acpi_bus.h>
22 #include <acpi/acnames.h>
23 #include <acpi/acnamesp.h>
24 #include <acpi/acparser.h>
25 #include <acpi/acexcep.h>
26 #include <acpi/acmacros.h>
27 #include <acpi/actypes.h>
29 #define REGS_PER_GTF 7
30 struct taskfile_array {
31 u8 tfa[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
42 struct ide_acpi_drive_link {
44 acpi_handle obj_handle;
48 struct ide_acpi_hwif_link {
50 acpi_handle obj_handle;
51 struct GTM_buffer gtm;
52 struct ide_acpi_drive_link master;
53 struct ide_acpi_drive_link slave;
57 /* note: adds function name and KERN_DEBUG */
59 #define DEBPRINT(fmt, args...) \
60 printk(KERN_DEBUG "%s: " fmt, __FUNCTION__, ## args)
62 #define DEBPRINT(fmt, args...) do {} while (0)
63 #endif /* DEBUGGING */
65 extern int ide_noacpi;
66 extern int ide_noacpitfs;
67 extern int ide_noacpionboot;
69 static bool ide_noacpi_psx;
70 static int no_acpi_psx(const struct dmi_system_id *id)
72 ide_noacpi_psx = true;
73 printk(KERN_NOTICE"%s detected - disable ACPI _PSx.\n", id->ident);
77 static const struct dmi_system_id ide_acpi_dmi_table[] = {
79 /* We should check if this is because ACPI NVS isn't save/restored. */
81 .callback = no_acpi_psx,
84 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies Ltd."),
85 DMI_MATCH(DMI_BIOS_VERSION, "KAM1.60")
89 { } /* terminate list */
92 static int ide_acpi_blacklist(void)
98 dmi_check_system(ide_acpi_dmi_table);
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
108 * Returns the ACPI object handle to the corresponding PCI device.
110 * Returns 0 on success, <0 on error.
112 static int ide_get_dev_handle(struct device *dev, acpi_handle *handle,
113 acpi_integer *pcidevfn)
115 struct pci_dev *pdev = to_pci_dev(dev);
116 unsigned int bus, devnum, func;
118 acpi_handle dev_handle;
119 struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
122 struct acpi_device_info *dinfo = NULL;
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);
131 DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
133 dev_handle = DEVICE_ACPI_HANDLE(dev);
135 DEBPRINT("no acpi handle for device\n");
139 status = acpi_get_object_info(dev_handle, &buffer);
140 if (ACPI_FAILURE(status)) {
141 DEBPRINT("get_object_info for device failed\n");
144 dinfo = buffer.pointer;
145 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
146 dinfo->address == addr) {
148 *handle = dev_handle;
150 DEBPRINT("get_object_info for device has wrong "
151 " address: %llu, should be %u\n",
152 dinfo ? (unsigned long long)dinfo->address : -1ULL,
157 DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
158 devnum, func, (unsigned long long)addr, *handle);
166 * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
167 * @hwif: device to locate
169 * Retrieves the object handle for a given hwif.
171 * Returns handle on success, 0 on error.
173 static acpi_handle ide_acpi_hwif_get_handle(ide_hwif_t *hwif)
175 struct device *dev = hwif->gendev.parent;
176 acpi_handle dev_handle;
177 acpi_integer pcidevfn;
178 acpi_handle chan_handle;
181 DEBPRINT("ENTER: device %s\n", hwif->name);
184 DEBPRINT("no PCI device for %s\n", hwif->name);
188 err = ide_get_dev_handle(dev, &dev_handle, &pcidevfn);
190 DEBPRINT("ide_get_dev_handle failed (%d)\n", err);
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);
205 * ide_acpi_drive_get_handle - Get ACPI object handle for a given drive
206 * @drive: device to locate
208 * Retrieves the object handle of a given drive. According to the ACPI
209 * spec the drive is a child of the hwif.
211 * Returns handle on success, 0 on error.
213 static acpi_handle ide_acpi_drive_get_handle(ide_drive_t *drive)
215 ide_hwif_t *hwif = HWIF(drive);
217 acpi_handle drive_handle;
222 if (!hwif->acpidata->obj_handle)
225 port = hwif->channel ? drive->dn - 2: drive->dn;
227 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
228 drive->name, hwif->channel, port);
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);
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
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.
250 * The returned @gtf_length and @gtf_address are only valid if the
251 * function return value is 0.
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)
258 struct acpi_buffer output;
259 union acpi_object *out_obj;
260 ide_hwif_t *hwif = HWIF(drive);
261 struct device *dev = hwif->gendev.parent;
273 DEBPRINT("no PCI device for %s\n", hwif->name);
277 if (!hwif->acpidata) {
278 DEBPRINT("no ACPI data for %s\n", hwif->name);
282 port = hwif->channel ? drive->dn - 2: drive->dn;
284 if (!drive->acpidata) {
286 drive->acpidata = &hwif->acpidata->master;
287 hwif->acpidata->master.drive = drive;
289 drive->acpidata = &hwif->acpidata->slave;
290 hwif->acpidata->slave.drive = drive;
294 DEBPRINT("ENTER: %s at %s, port#: %d, hard_port#: %d\n",
295 hwif->name, dev->bus_id, port, hwif->channel);
297 if (!drive->present) {
298 DEBPRINT("%s drive %d:%d not present\n",
299 hwif->name, hwif->channel, port);
303 /* Get this drive's _ADR info. if not already known. */
304 if (!drive->acpidata->obj_handle) {
305 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
306 if (!drive->acpidata->obj_handle) {
307 DEBPRINT("No ACPI object found for %s\n",
313 /* Setting up output buffer */
314 output.length = ACPI_ALLOCATE_BUFFER;
315 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
317 /* _GTF has no input parameters */
319 status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
321 if (ACPI_FAILURE(status)) {
323 "%s: Run _GTF error: status = 0x%x\n",
324 __FUNCTION__, status);
328 if (!output.length || !output.pointer) {
329 DEBPRINT("Run _GTF: "
330 "length or ptr is NULL (0x%llx, 0x%p)\n",
331 (unsigned long long)output.length,
336 out_obj = output.pointer;
337 if (out_obj->type != ACPI_TYPE_BUFFER) {
338 DEBPRINT("Run _GTF: error: "
339 "expected object type of ACPI_TYPE_BUFFER, "
340 "got 0x%x\n", out_obj->type);
342 kfree(output.pointer);
346 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
347 out_obj->buffer.length % REGS_PER_GTF) {
349 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
350 __FUNCTION__, out_obj->buffer.length,
351 out_obj->buffer.pointer);
353 kfree(output.pointer);
357 *gtf_length = out_obj->buffer.length;
358 *gtf_address = (unsigned long)out_obj->buffer.pointer;
359 *obj_loc = (unsigned long)out_obj;
360 DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
361 *gtf_length, *gtf_address, *obj_loc);
368 * taskfile_load_raw - send taskfile registers to drive
369 * @drive: drive to which output is sent
370 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
372 * Outputs IDE taskfile to the drive.
374 static int taskfile_load_raw(ide_drive_t *drive,
375 const struct taskfile_array *gtf)
380 DEBPRINT("(0x1f1-1f7): hex: "
381 "%02x %02x %02x %02x %02x %02x %02x\n",
382 gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
383 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
385 memset(&args, 0, sizeof(ide_task_t));
386 args.command_type = IDE_DRIVE_TASK_NO_DATA;
387 args.data_phase = TASKFILE_NO_DATA;
388 args.handler = &task_no_data_intr;
390 /* convert gtf to IDE Taskfile */
391 args.tfRegister[1] = gtf->tfa[0]; /* 0x1f1 */
392 args.tfRegister[2] = gtf->tfa[1]; /* 0x1f2 */
393 args.tfRegister[3] = gtf->tfa[2]; /* 0x1f3 */
394 args.tfRegister[4] = gtf->tfa[3]; /* 0x1f4 */
395 args.tfRegister[5] = gtf->tfa[4]; /* 0x1f5 */
396 args.tfRegister[6] = gtf->tfa[5]; /* 0x1f6 */
397 args.tfRegister[7] = gtf->tfa[6]; /* 0x1f7 */
400 DEBPRINT("_GTF execution disabled\n");
404 err = ide_raw_taskfile(drive, &args, NULL);
406 printk(KERN_ERR "%s: ide_raw_taskfile failed: %u\n",
413 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
414 * @drive: the drive to which the taskfile command should be sent
415 * @gtf_length: total number of bytes of _GTF taskfiles
416 * @gtf_address: location of _GTF taskfile arrays
418 * Write {gtf_address, length gtf_length} in groups of
419 * REGS_PER_GTF bytes.
421 static int do_drive_set_taskfiles(ide_drive_t *drive,
422 unsigned int gtf_length,
423 unsigned long gtf_address)
425 int rc = -ENODEV, err;
426 int gtf_count = gtf_length / REGS_PER_GTF;
428 struct taskfile_array *gtf;
433 DEBPRINT("ENTER: %s, hard_port#: %d\n", drive->name, drive->dn);
437 if (!gtf_count) /* shouldn't be here */
440 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
441 gtf_length, gtf_length, gtf_count, gtf_address);
443 if (gtf_length % REGS_PER_GTF) {
444 printk(KERN_ERR "%s: unexpected GTF length (%d)\n",
445 __FUNCTION__, gtf_length);
450 for (ix = 0; ix < gtf_count; ix++) {
451 gtf = (struct taskfile_array *)
452 (gtf_address + ix * REGS_PER_GTF);
454 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
455 err = taskfile_load_raw(drive, gtf);
465 * ide_acpi_exec_tfs - get then write drive taskfile settings
466 * @drive: the drive for which the taskfile settings should be
469 * According to the ACPI spec this should be called after _STM
470 * has been evaluated for the interface. Some ACPI vendors interpret
471 * that as a hard requirement and modify the taskfile according
472 * to the Identify Drive information passed down with _STM.
473 * So one should really make sure to call this only after _STM has
476 int ide_acpi_exec_tfs(ide_drive_t *drive)
479 unsigned int gtf_length;
480 unsigned long gtf_address;
481 unsigned long obj_loc;
486 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
488 ret = do_drive_get_GTF(drive, >f_length, >f_address, &obj_loc);
490 DEBPRINT("get_GTF error (%d)\n", ret);
494 DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
496 ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
497 kfree((void *)obj_loc);
499 DEBPRINT("set_taskfiles error (%d)\n", ret);
502 DEBPRINT("ret=%d\n", ret);
506 EXPORT_SYMBOL_GPL(ide_acpi_exec_tfs);
509 * ide_acpi_get_timing - get the channel (controller) timings
510 * @hwif: target IDE interface (channel)
512 * This function executes the _GTM ACPI method for the target channel.
515 void ide_acpi_get_timing(ide_hwif_t *hwif)
518 struct acpi_buffer output;
519 union acpi_object *out_obj;
524 DEBPRINT("ENTER:\n");
526 if (!hwif->acpidata) {
527 DEBPRINT("no ACPI data for %s\n", hwif->name);
531 /* Setting up output buffer for _GTM */
532 output.length = ACPI_ALLOCATE_BUFFER;
533 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
535 /* _GTM has no input parameters */
536 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
539 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
540 status, output.pointer,
541 (unsigned long long)output.length);
543 if (ACPI_FAILURE(status)) {
544 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
548 if (!output.length || !output.pointer) {
549 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
550 (unsigned long long)output.length,
552 kfree(output.pointer);
556 out_obj = output.pointer;
557 if (out_obj->type != ACPI_TYPE_BUFFER) {
558 kfree(output.pointer);
559 DEBPRINT("Run _GTM: error: "
560 "expected object type of ACPI_TYPE_BUFFER, "
561 "got 0x%x\n", out_obj->type);
565 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
566 out_obj->buffer.length != sizeof(struct GTM_buffer)) {
567 kfree(output.pointer);
569 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
571 __FUNCTION__, out_obj->buffer.length,
572 sizeof(struct GTM_buffer), out_obj->buffer.pointer);
576 memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
577 sizeof(struct GTM_buffer));
579 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
580 out_obj->buffer.pointer, out_obj->buffer.length,
581 sizeof(struct GTM_buffer));
583 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
584 hwif->acpidata->gtm.PIO_speed0,
585 hwif->acpidata->gtm.DMA_speed0,
586 hwif->acpidata->gtm.PIO_speed1,
587 hwif->acpidata->gtm.DMA_speed1,
588 hwif->acpidata->gtm.GTM_flags);
590 kfree(output.pointer);
592 EXPORT_SYMBOL_GPL(ide_acpi_get_timing);
595 * ide_acpi_push_timing - set the channel (controller) timings
596 * @hwif: target IDE interface (channel)
598 * This function executes the _STM ACPI method for the target channel.
600 * _STM requires Identify Drive data, which has to passed as an argument.
601 * Unfortunately hd_driveid is a mangled version which we can't readily
602 * use; hence we'll get the information afresh.
604 void ide_acpi_push_timing(ide_hwif_t *hwif)
607 struct acpi_object_list input;
608 union acpi_object in_params[3];
609 struct ide_acpi_drive_link *master = &hwif->acpidata->master;
610 struct ide_acpi_drive_link *slave = &hwif->acpidata->slave;
615 DEBPRINT("ENTER:\n");
617 if (!hwif->acpidata) {
618 DEBPRINT("no ACPI data for %s\n", hwif->name);
622 /* Give the GTM buffer + drive Identify data to the channel via the
624 /* setup input parameters buffer for _STM */
626 input.pointer = in_params;
627 in_params[0].type = ACPI_TYPE_BUFFER;
628 in_params[0].buffer.length = sizeof(struct GTM_buffer);
629 in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
630 in_params[1].type = ACPI_TYPE_BUFFER;
631 in_params[1].buffer.length = sizeof(struct hd_driveid);
632 in_params[1].buffer.pointer = (u8 *)&master->idbuff;
633 in_params[2].type = ACPI_TYPE_BUFFER;
634 in_params[2].buffer.length = sizeof(struct hd_driveid);
635 in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
636 /* Output buffer: _STM has no output */
638 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
641 if (ACPI_FAILURE(status)) {
642 DEBPRINT("Run _STM error: status = 0x%x\n", status);
644 DEBPRINT("_STM status: %d\n", status);
646 EXPORT_SYMBOL_GPL(ide_acpi_push_timing);
649 * ide_acpi_set_state - set the channel power state
650 * @hwif: target IDE interface
653 * This function executes the _PS0/_PS3 ACPI method to set the power state.
654 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
656 void ide_acpi_set_state(ide_hwif_t *hwif, int on)
660 if (ide_noacpi || ide_noacpi_psx)
663 DEBPRINT("ENTER:\n");
665 if (!hwif->acpidata) {
666 DEBPRINT("no ACPI data for %s\n", hwif->name);
669 /* channel first and then drives for power on and verse versa for power off */
671 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
672 for (unit = 0; unit < MAX_DRIVES; ++unit) {
673 ide_drive_t *drive = &hwif->drives[unit];
675 if (!drive->acpidata->obj_handle)
676 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
678 if (drive->acpidata->obj_handle && drive->present) {
679 acpi_bus_set_power(drive->acpidata->obj_handle,
680 on? ACPI_STATE_D0: ACPI_STATE_D3);
684 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
688 * ide_acpi_init - initialize the ACPI link for an IDE interface
689 * @hwif: target IDE interface (channel)
691 * The ACPI spec is not quite clear when the drive identify buffer
692 * should be obtained. Calling IDENTIFY DEVICE during shutdown
693 * is not the best of ideas as the drive might already being put to
694 * sleep. And obviously we can't call it during resume.
695 * So we get the information during startup; but this means that
696 * any changes during run-time will be lost after resume.
698 void ide_acpi_init(ide_hwif_t *hwif)
702 struct ide_acpi_drive_link *master;
703 struct ide_acpi_drive_link *slave;
705 ide_acpi_blacklist();
707 hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
711 hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
712 if (!hwif->acpidata->obj_handle) {
713 DEBPRINT("no ACPI object for %s found\n", hwif->name);
714 kfree(hwif->acpidata);
715 hwif->acpidata = NULL;
720 * The ACPI spec mandates that we send information
721 * for both drives, regardless whether they are connected
724 hwif->acpidata->master.drive = &hwif->drives[0];
725 hwif->drives[0].acpidata = &hwif->acpidata->master;
726 master = &hwif->acpidata->master;
728 hwif->acpidata->slave.drive = &hwif->drives[1];
729 hwif->drives[1].acpidata = &hwif->acpidata->slave;
730 slave = &hwif->acpidata->slave;
734 * Send IDENTIFY for each drive
736 if (master->drive->present) {
737 err = taskfile_lib_get_identify(master->drive, master->idbuff);
739 DEBPRINT("identify device %s failed (%d)\n",
740 master->drive->name, err);
744 if (slave->drive->present) {
745 err = taskfile_lib_get_identify(slave->drive, slave->idbuff);
747 DEBPRINT("identify device %s failed (%d)\n",
748 slave->drive->name, err);
752 if (ide_noacpionboot) {
753 DEBPRINT("ACPI methods disabled on boot\n");
757 /* ACPI _PS0 before _STM */
758 ide_acpi_set_state(hwif, 1);
760 * ACPI requires us to call _STM on startup
762 ide_acpi_get_timing(hwif);
763 ide_acpi_push_timing(hwif);
765 for (unit = 0; unit < MAX_DRIVES; ++unit) {
766 ide_drive_t *drive = &hwif->drives[unit];
768 if (drive->present) {
769 /* Execute ACPI startup code */
770 ide_acpi_exec_tfs(drive);
774 EXPORT_SYMBOL_GPL(ide_acpi_init);