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")
90 static int ide_acpi_blacklist(void)
96 dmi_check_system(ide_acpi_dmi_table);
101 * ide_get_dev_handle - finds acpi_handle and PCI device.function
102 * @dev: device to locate
103 * @handle: returned acpi_handle for @dev
104 * @pcidevfn: return PCI device.func for @dev
106 * Returns the ACPI object handle to the corresponding PCI device.
108 * Returns 0 on success, <0 on error.
110 static int ide_get_dev_handle(struct device *dev, acpi_handle *handle,
111 acpi_integer *pcidevfn)
113 struct pci_dev *pdev = to_pci_dev(dev);
114 unsigned int bus, devnum, func;
116 acpi_handle dev_handle;
117 struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
120 struct acpi_device_info *dinfo = NULL;
123 bus = pdev->bus->number;
124 devnum = PCI_SLOT(pdev->devfn);
125 func = PCI_FUNC(pdev->devfn);
126 /* ACPI _ADR encoding for PCI bus: */
127 addr = (acpi_integer)(devnum << 16 | func);
129 DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
131 dev_handle = DEVICE_ACPI_HANDLE(dev);
133 DEBPRINT("no acpi handle for device\n");
137 status = acpi_get_object_info(dev_handle, &buffer);
138 if (ACPI_FAILURE(status)) {
139 DEBPRINT("get_object_info for device failed\n");
142 dinfo = buffer.pointer;
143 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
144 dinfo->address == addr) {
146 *handle = dev_handle;
148 DEBPRINT("get_object_info for device has wrong "
149 " address: %llu, should be %u\n",
150 dinfo ? (unsigned long long)dinfo->address : -1ULL,
155 DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
156 devnum, func, (unsigned long long)addr, *handle);
164 * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
165 * @hwif: device to locate
167 * Retrieves the object handle for a given hwif.
169 * Returns handle on success, 0 on error.
171 static acpi_handle ide_acpi_hwif_get_handle(ide_hwif_t *hwif)
173 struct device *dev = hwif->gendev.parent;
174 acpi_handle dev_handle;
175 acpi_integer pcidevfn;
176 acpi_handle chan_handle;
179 DEBPRINT("ENTER: device %s\n", hwif->name);
182 DEBPRINT("no PCI device for %s\n", hwif->name);
186 err = ide_get_dev_handle(dev, &dev_handle, &pcidevfn);
188 DEBPRINT("ide_get_dev_handle failed (%d)\n", err);
192 /* get child objects of dev_handle == channel objects,
193 * + _their_ children == drive objects */
194 /* channel is hwif->channel */
195 chan_handle = acpi_get_child(dev_handle, hwif->channel);
196 DEBPRINT("chan adr=%d: handle=0x%p\n",
197 hwif->channel, chan_handle);
203 * ide_acpi_drive_get_handle - Get ACPI object handle for a given drive
204 * @drive: device to locate
206 * Retrieves the object handle of a given drive. According to the ACPI
207 * spec the drive is a child of the hwif.
209 * Returns handle on success, 0 on error.
211 static acpi_handle ide_acpi_drive_get_handle(ide_drive_t *drive)
213 ide_hwif_t *hwif = HWIF(drive);
215 acpi_handle drive_handle;
220 if (!hwif->acpidata->obj_handle)
223 port = hwif->channel ? drive->dn - 2: drive->dn;
225 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
226 drive->name, hwif->channel, port);
229 /* TBD: could also check ACPI object VALID bits */
230 drive_handle = acpi_get_child(hwif->acpidata->obj_handle, port);
231 DEBPRINT("drive %s handle 0x%p\n", drive->name, drive_handle);
237 * do_drive_get_GTF - get the drive bootup default taskfile settings
238 * @drive: the drive for which the taskfile settings should be retrieved
239 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
240 * @gtf_address: buffer containing _GTF taskfile arrays
242 * The _GTF method has no input parameters.
243 * It returns a variable number of register set values (registers
244 * hex 1F1..1F7, taskfiles).
245 * The <variable number> is not known in advance, so have ACPI-CA
246 * allocate the buffer as needed and return it, then free it later.
248 * The returned @gtf_length and @gtf_address are only valid if the
249 * function return value is 0.
251 static int do_drive_get_GTF(ide_drive_t *drive,
252 unsigned int *gtf_length, unsigned long *gtf_address,
253 unsigned long *obj_loc)
256 struct acpi_buffer output;
257 union acpi_object *out_obj;
258 ide_hwif_t *hwif = HWIF(drive);
259 struct device *dev = hwif->gendev.parent;
271 DEBPRINT("no PCI device for %s\n", hwif->name);
275 if (!hwif->acpidata) {
276 DEBPRINT("no ACPI data for %s\n", hwif->name);
280 port = hwif->channel ? drive->dn - 2: drive->dn;
282 if (!drive->acpidata) {
284 drive->acpidata = &hwif->acpidata->master;
285 hwif->acpidata->master.drive = drive;
287 drive->acpidata = &hwif->acpidata->slave;
288 hwif->acpidata->slave.drive = drive;
292 DEBPRINT("ENTER: %s at %s, port#: %d, hard_port#: %d\n",
293 hwif->name, dev->bus_id, port, hwif->channel);
295 if (!drive->present) {
296 DEBPRINT("%s drive %d:%d not present\n",
297 hwif->name, hwif->channel, port);
301 /* Get this drive's _ADR info. if not already known. */
302 if (!drive->acpidata->obj_handle) {
303 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
304 if (!drive->acpidata->obj_handle) {
305 DEBPRINT("No ACPI object found for %s\n",
311 /* Setting up output buffer */
312 output.length = ACPI_ALLOCATE_BUFFER;
313 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
315 /* _GTF has no input parameters */
317 status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
319 if (ACPI_FAILURE(status)) {
321 "%s: Run _GTF error: status = 0x%x\n",
322 __FUNCTION__, status);
326 if (!output.length || !output.pointer) {
327 DEBPRINT("Run _GTF: "
328 "length or ptr is NULL (0x%llx, 0x%p)\n",
329 (unsigned long long)output.length,
334 out_obj = output.pointer;
335 if (out_obj->type != ACPI_TYPE_BUFFER) {
336 DEBPRINT("Run _GTF: error: "
337 "expected object type of ACPI_TYPE_BUFFER, "
338 "got 0x%x\n", out_obj->type);
340 kfree(output.pointer);
344 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
345 out_obj->buffer.length % REGS_PER_GTF) {
347 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
348 __FUNCTION__, out_obj->buffer.length,
349 out_obj->buffer.pointer);
351 kfree(output.pointer);
355 *gtf_length = out_obj->buffer.length;
356 *gtf_address = (unsigned long)out_obj->buffer.pointer;
357 *obj_loc = (unsigned long)out_obj;
358 DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
359 *gtf_length, *gtf_address, *obj_loc);
366 * taskfile_load_raw - send taskfile registers to drive
367 * @drive: drive to which output is sent
368 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
370 * Outputs IDE taskfile to the drive.
372 static int taskfile_load_raw(ide_drive_t *drive,
373 const struct taskfile_array *gtf)
378 DEBPRINT("(0x1f1-1f7): hex: "
379 "%02x %02x %02x %02x %02x %02x %02x\n",
380 gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
381 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
383 memset(&args, 0, sizeof(ide_task_t));
384 args.command_type = IDE_DRIVE_TASK_NO_DATA;
385 args.data_phase = TASKFILE_NO_DATA;
386 args.handler = &task_no_data_intr;
388 /* convert gtf to IDE Taskfile */
389 args.tfRegister[1] = gtf->tfa[0]; /* 0x1f1 */
390 args.tfRegister[2] = gtf->tfa[1]; /* 0x1f2 */
391 args.tfRegister[3] = gtf->tfa[2]; /* 0x1f3 */
392 args.tfRegister[4] = gtf->tfa[3]; /* 0x1f4 */
393 args.tfRegister[5] = gtf->tfa[4]; /* 0x1f5 */
394 args.tfRegister[6] = gtf->tfa[5]; /* 0x1f6 */
395 args.tfRegister[7] = gtf->tfa[6]; /* 0x1f7 */
398 DEBPRINT("_GTF execution disabled\n");
402 err = ide_raw_taskfile(drive, &args, NULL);
404 printk(KERN_ERR "%s: ide_raw_taskfile failed: %u\n",
411 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
412 * @drive: the drive to which the taskfile command should be sent
413 * @gtf_length: total number of bytes of _GTF taskfiles
414 * @gtf_address: location of _GTF taskfile arrays
416 * Write {gtf_address, length gtf_length} in groups of
417 * REGS_PER_GTF bytes.
419 static int do_drive_set_taskfiles(ide_drive_t *drive,
420 unsigned int gtf_length,
421 unsigned long gtf_address)
423 int rc = -ENODEV, err;
424 int gtf_count = gtf_length / REGS_PER_GTF;
426 struct taskfile_array *gtf;
431 DEBPRINT("ENTER: %s, hard_port#: %d\n", drive->name, drive->dn);
435 if (!gtf_count) /* shouldn't be here */
438 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
439 gtf_length, gtf_length, gtf_count, gtf_address);
441 if (gtf_length % REGS_PER_GTF) {
442 printk(KERN_ERR "%s: unexpected GTF length (%d)\n",
443 __FUNCTION__, gtf_length);
448 for (ix = 0; ix < gtf_count; ix++) {
449 gtf = (struct taskfile_array *)
450 (gtf_address + ix * REGS_PER_GTF);
452 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
453 err = taskfile_load_raw(drive, gtf);
463 * ide_acpi_exec_tfs - get then write drive taskfile settings
464 * @drive: the drive for which the taskfile settings should be
467 * According to the ACPI spec this should be called after _STM
468 * has been evaluated for the interface. Some ACPI vendors interpret
469 * that as a hard requirement and modify the taskfile according
470 * to the Identify Drive information passed down with _STM.
471 * So one should really make sure to call this only after _STM has
474 int ide_acpi_exec_tfs(ide_drive_t *drive)
477 unsigned int gtf_length;
478 unsigned long gtf_address;
479 unsigned long obj_loc;
484 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
486 ret = do_drive_get_GTF(drive, >f_length, >f_address, &obj_loc);
488 DEBPRINT("get_GTF error (%d)\n", ret);
492 DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
494 ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
495 kfree((void *)obj_loc);
497 DEBPRINT("set_taskfiles error (%d)\n", ret);
500 DEBPRINT("ret=%d\n", ret);
504 EXPORT_SYMBOL_GPL(ide_acpi_exec_tfs);
507 * ide_acpi_get_timing - get the channel (controller) timings
508 * @hwif: target IDE interface (channel)
510 * This function executes the _GTM ACPI method for the target channel.
513 void ide_acpi_get_timing(ide_hwif_t *hwif)
516 struct acpi_buffer output;
517 union acpi_object *out_obj;
522 DEBPRINT("ENTER:\n");
524 if (!hwif->acpidata) {
525 DEBPRINT("no ACPI data for %s\n", hwif->name);
529 /* Setting up output buffer for _GTM */
530 output.length = ACPI_ALLOCATE_BUFFER;
531 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
533 /* _GTM has no input parameters */
534 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
537 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
538 status, output.pointer,
539 (unsigned long long)output.length);
541 if (ACPI_FAILURE(status)) {
542 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
546 if (!output.length || !output.pointer) {
547 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
548 (unsigned long long)output.length,
550 kfree(output.pointer);
554 out_obj = output.pointer;
555 if (out_obj->type != ACPI_TYPE_BUFFER) {
556 kfree(output.pointer);
557 DEBPRINT("Run _GTM: error: "
558 "expected object type of ACPI_TYPE_BUFFER, "
559 "got 0x%x\n", out_obj->type);
563 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
564 out_obj->buffer.length != sizeof(struct GTM_buffer)) {
565 kfree(output.pointer);
567 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
569 __FUNCTION__, out_obj->buffer.length,
570 sizeof(struct GTM_buffer), out_obj->buffer.pointer);
574 memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
575 sizeof(struct GTM_buffer));
577 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
578 out_obj->buffer.pointer, out_obj->buffer.length,
579 sizeof(struct GTM_buffer));
581 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
582 hwif->acpidata->gtm.PIO_speed0,
583 hwif->acpidata->gtm.DMA_speed0,
584 hwif->acpidata->gtm.PIO_speed1,
585 hwif->acpidata->gtm.DMA_speed1,
586 hwif->acpidata->gtm.GTM_flags);
588 kfree(output.pointer);
590 EXPORT_SYMBOL_GPL(ide_acpi_get_timing);
593 * ide_acpi_push_timing - set the channel (controller) timings
594 * @hwif: target IDE interface (channel)
596 * This function executes the _STM ACPI method for the target channel.
598 * _STM requires Identify Drive data, which has to passed as an argument.
599 * Unfortunately hd_driveid is a mangled version which we can't readily
600 * use; hence we'll get the information afresh.
602 void ide_acpi_push_timing(ide_hwif_t *hwif)
605 struct acpi_object_list input;
606 union acpi_object in_params[3];
607 struct ide_acpi_drive_link *master = &hwif->acpidata->master;
608 struct ide_acpi_drive_link *slave = &hwif->acpidata->slave;
613 DEBPRINT("ENTER:\n");
615 if (!hwif->acpidata) {
616 DEBPRINT("no ACPI data for %s\n", hwif->name);
620 /* Give the GTM buffer + drive Identify data to the channel via the
622 /* setup input parameters buffer for _STM */
624 input.pointer = in_params;
625 in_params[0].type = ACPI_TYPE_BUFFER;
626 in_params[0].buffer.length = sizeof(struct GTM_buffer);
627 in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
628 in_params[1].type = ACPI_TYPE_BUFFER;
629 in_params[1].buffer.length = sizeof(struct hd_driveid);
630 in_params[1].buffer.pointer = (u8 *)&master->idbuff;
631 in_params[2].type = ACPI_TYPE_BUFFER;
632 in_params[2].buffer.length = sizeof(struct hd_driveid);
633 in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
634 /* Output buffer: _STM has no output */
636 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
639 if (ACPI_FAILURE(status)) {
640 DEBPRINT("Run _STM error: status = 0x%x\n", status);
642 DEBPRINT("_STM status: %d\n", status);
644 EXPORT_SYMBOL_GPL(ide_acpi_push_timing);
647 * ide_acpi_set_state - set the channel power state
648 * @hwif: target IDE interface
651 * This function executes the _PS0/_PS3 ACPI method to set the power state.
652 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
654 void ide_acpi_set_state(ide_hwif_t *hwif, int on)
658 if (ide_noacpi || ide_noacpi_psx)
661 DEBPRINT("ENTER:\n");
663 if (!hwif->acpidata) {
664 DEBPRINT("no ACPI data for %s\n", hwif->name);
667 /* channel first and then drives for power on and verse versa for power off */
669 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
670 for (unit = 0; unit < MAX_DRIVES; ++unit) {
671 ide_drive_t *drive = &hwif->drives[unit];
673 if (!drive->acpidata->obj_handle)
674 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
676 if (drive->acpidata->obj_handle && drive->present) {
677 acpi_bus_set_power(drive->acpidata->obj_handle,
678 on? ACPI_STATE_D0: ACPI_STATE_D3);
682 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
686 * ide_acpi_init - initialize the ACPI link for an IDE interface
687 * @hwif: target IDE interface (channel)
689 * The ACPI spec is not quite clear when the drive identify buffer
690 * should be obtained. Calling IDENTIFY DEVICE during shutdown
691 * is not the best of ideas as the drive might already being put to
692 * sleep. And obviously we can't call it during resume.
693 * So we get the information during startup; but this means that
694 * any changes during run-time will be lost after resume.
696 void ide_acpi_init(ide_hwif_t *hwif)
700 struct ide_acpi_drive_link *master;
701 struct ide_acpi_drive_link *slave;
703 ide_acpi_blacklist();
705 hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
709 hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
710 if (!hwif->acpidata->obj_handle) {
711 DEBPRINT("no ACPI object for %s found\n", hwif->name);
712 kfree(hwif->acpidata);
713 hwif->acpidata = NULL;
718 * The ACPI spec mandates that we send information
719 * for both drives, regardless whether they are connected
722 hwif->acpidata->master.drive = &hwif->drives[0];
723 hwif->drives[0].acpidata = &hwif->acpidata->master;
724 master = &hwif->acpidata->master;
726 hwif->acpidata->slave.drive = &hwif->drives[1];
727 hwif->drives[1].acpidata = &hwif->acpidata->slave;
728 slave = &hwif->acpidata->slave;
732 * Send IDENTIFY for each drive
734 if (master->drive->present) {
735 err = taskfile_lib_get_identify(master->drive, master->idbuff);
737 DEBPRINT("identify device %s failed (%d)\n",
738 master->drive->name, err);
742 if (slave->drive->present) {
743 err = taskfile_lib_get_identify(slave->drive, slave->idbuff);
745 DEBPRINT("identify device %s failed (%d)\n",
746 slave->drive->name, err);
750 if (ide_noacpionboot) {
751 DEBPRINT("ACPI methods disabled on boot\n");
755 /* ACPI _PS0 before _STM */
756 ide_acpi_set_state(hwif, 1);
758 * ACPI requires us to call _STM on startup
760 ide_acpi_get_timing(hwif);
761 ide_acpi_push_timing(hwif);
763 for (unit = 0; unit < MAX_DRIVES; ++unit) {
764 ide_drive_t *drive = &hwif->drives[unit];
766 if (drive->present) {
767 /* Execute ACPI startup code */
768 ide_acpi_exec_tfs(drive);
772 EXPORT_SYMBOL_GPL(ide_acpi_init);