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>
21 #include <acpi/acnames.h>
22 #include <acpi/acnamesp.h>
23 #include <acpi/acparser.h>
24 #include <acpi/acexcep.h>
25 #include <acpi/acmacros.h>
26 #include <acpi/actypes.h>
28 #define REGS_PER_GTF 7
29 struct taskfile_array {
30 u8 tfa[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
41 struct ide_acpi_drive_link {
42 acpi_handle obj_handle;
46 struct ide_acpi_hwif_link {
48 acpi_handle obj_handle;
49 struct GTM_buffer gtm;
50 struct ide_acpi_drive_link master;
51 struct ide_acpi_drive_link slave;
55 /* note: adds function name and KERN_DEBUG */
57 #define DEBPRINT(fmt, args...) \
58 printk(KERN_DEBUG "%s: " fmt, __FUNCTION__, ## args)
60 #define DEBPRINT(fmt, args...) do {} while (0)
61 #endif /* DEBUGGING */
63 extern int ide_noacpi;
64 extern int ide_noacpitfs;
65 extern int ide_noacpionboot;
67 static bool ide_noacpi_psx;
68 static int no_acpi_psx(const struct dmi_system_id *id)
70 ide_noacpi_psx = true;
71 printk(KERN_NOTICE"%s detected - disable ACPI _PSx.\n", id->ident);
75 static const struct dmi_system_id ide_acpi_dmi_table[] = {
77 /* We should check if this is because ACPI NVS isn't save/restored. */
79 .callback = no_acpi_psx,
82 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies Ltd."),
83 DMI_MATCH(DMI_BIOS_VERSION, "KAM1.60")
87 { } /* terminate list */
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 uninitialized_var(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 DEBPRINT("ENTER: %s at %s, port#: %d, hard_port#: %d\n",
283 hwif->name, dev->bus_id, port, hwif->channel);
285 if (!drive->present) {
286 DEBPRINT("%s drive %d:%d not present\n",
287 hwif->name, hwif->channel, port);
291 /* Get this drive's _ADR info. if not already known. */
292 if (!drive->acpidata->obj_handle) {
293 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
294 if (!drive->acpidata->obj_handle) {
295 DEBPRINT("No ACPI object found for %s\n",
301 /* Setting up output buffer */
302 output.length = ACPI_ALLOCATE_BUFFER;
303 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
305 /* _GTF has no input parameters */
307 status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
309 if (ACPI_FAILURE(status)) {
311 "%s: Run _GTF error: status = 0x%x\n",
312 __FUNCTION__, status);
316 if (!output.length || !output.pointer) {
317 DEBPRINT("Run _GTF: "
318 "length or ptr is NULL (0x%llx, 0x%p)\n",
319 (unsigned long long)output.length,
324 out_obj = output.pointer;
325 if (out_obj->type != ACPI_TYPE_BUFFER) {
326 DEBPRINT("Run _GTF: error: "
327 "expected object type of ACPI_TYPE_BUFFER, "
328 "got 0x%x\n", out_obj->type);
330 kfree(output.pointer);
334 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
335 out_obj->buffer.length % REGS_PER_GTF) {
337 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
338 __FUNCTION__, out_obj->buffer.length,
339 out_obj->buffer.pointer);
341 kfree(output.pointer);
345 *gtf_length = out_obj->buffer.length;
346 *gtf_address = (unsigned long)out_obj->buffer.pointer;
347 *obj_loc = (unsigned long)out_obj;
348 DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
349 *gtf_length, *gtf_address, *obj_loc);
356 * taskfile_load_raw - send taskfile registers to drive
357 * @drive: drive to which output is sent
358 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
360 * Outputs IDE taskfile to the drive.
362 static int taskfile_load_raw(ide_drive_t *drive,
363 const struct taskfile_array *gtf)
368 DEBPRINT("(0x1f1-1f7): hex: "
369 "%02x %02x %02x %02x %02x %02x %02x\n",
370 gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
371 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
373 memset(&args, 0, sizeof(ide_task_t));
375 /* convert gtf to IDE Taskfile */
376 memcpy(&args.tf_array[7], >f->tfa, 7);
377 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
380 DEBPRINT("_GTF execution disabled\n");
384 err = ide_no_data_taskfile(drive, &args);
386 printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
393 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
394 * @drive: the drive to which the taskfile command should be sent
395 * @gtf_length: total number of bytes of _GTF taskfiles
396 * @gtf_address: location of _GTF taskfile arrays
398 * Write {gtf_address, length gtf_length} in groups of
399 * REGS_PER_GTF bytes.
401 static int do_drive_set_taskfiles(ide_drive_t *drive,
402 unsigned int gtf_length,
403 unsigned long gtf_address)
405 int rc = -ENODEV, err;
406 int gtf_count = gtf_length / REGS_PER_GTF;
408 struct taskfile_array *gtf;
413 DEBPRINT("ENTER: %s, hard_port#: %d\n", drive->name, drive->dn);
417 if (!gtf_count) /* shouldn't be here */
420 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
421 gtf_length, gtf_length, gtf_count, gtf_address);
423 if (gtf_length % REGS_PER_GTF) {
424 printk(KERN_ERR "%s: unexpected GTF length (%d)\n",
425 __FUNCTION__, gtf_length);
430 for (ix = 0; ix < gtf_count; ix++) {
431 gtf = (struct taskfile_array *)
432 (gtf_address + ix * REGS_PER_GTF);
434 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
435 err = taskfile_load_raw(drive, gtf);
445 * ide_acpi_exec_tfs - get then write drive taskfile settings
446 * @drive: the drive for which the taskfile settings should be
449 * According to the ACPI spec this should be called after _STM
450 * has been evaluated for the interface. Some ACPI vendors interpret
451 * that as a hard requirement and modify the taskfile according
452 * to the Identify Drive information passed down with _STM.
453 * So one should really make sure to call this only after _STM has
456 int ide_acpi_exec_tfs(ide_drive_t *drive)
459 unsigned int gtf_length;
460 unsigned long gtf_address;
461 unsigned long obj_loc;
466 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
468 ret = do_drive_get_GTF(drive, >f_length, >f_address, &obj_loc);
470 DEBPRINT("get_GTF error (%d)\n", ret);
474 DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
476 ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
477 kfree((void *)obj_loc);
479 DEBPRINT("set_taskfiles error (%d)\n", ret);
482 DEBPRINT("ret=%d\n", ret);
488 * ide_acpi_get_timing - get the channel (controller) timings
489 * @hwif: target IDE interface (channel)
491 * This function executes the _GTM ACPI method for the target channel.
494 void ide_acpi_get_timing(ide_hwif_t *hwif)
497 struct acpi_buffer output;
498 union acpi_object *out_obj;
503 DEBPRINT("ENTER:\n");
505 if (!hwif->acpidata) {
506 DEBPRINT("no ACPI data for %s\n", hwif->name);
510 /* Setting up output buffer for _GTM */
511 output.length = ACPI_ALLOCATE_BUFFER;
512 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
514 /* _GTM has no input parameters */
515 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
518 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
519 status, output.pointer,
520 (unsigned long long)output.length);
522 if (ACPI_FAILURE(status)) {
523 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
527 if (!output.length || !output.pointer) {
528 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
529 (unsigned long long)output.length,
531 kfree(output.pointer);
535 out_obj = output.pointer;
536 if (out_obj->type != ACPI_TYPE_BUFFER) {
537 kfree(output.pointer);
538 DEBPRINT("Run _GTM: error: "
539 "expected object type of ACPI_TYPE_BUFFER, "
540 "got 0x%x\n", out_obj->type);
544 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
545 out_obj->buffer.length != sizeof(struct GTM_buffer)) {
546 kfree(output.pointer);
548 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
550 __FUNCTION__, out_obj->buffer.length,
551 sizeof(struct GTM_buffer), out_obj->buffer.pointer);
555 memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
556 sizeof(struct GTM_buffer));
558 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
559 out_obj->buffer.pointer, out_obj->buffer.length,
560 sizeof(struct GTM_buffer));
562 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
563 hwif->acpidata->gtm.PIO_speed0,
564 hwif->acpidata->gtm.DMA_speed0,
565 hwif->acpidata->gtm.PIO_speed1,
566 hwif->acpidata->gtm.DMA_speed1,
567 hwif->acpidata->gtm.GTM_flags);
569 kfree(output.pointer);
573 * ide_acpi_push_timing - set the channel (controller) timings
574 * @hwif: target IDE interface (channel)
576 * This function executes the _STM ACPI method for the target channel.
578 * _STM requires Identify Drive data, which has to passed as an argument.
579 * Unfortunately hd_driveid is a mangled version which we can't readily
580 * use; hence we'll get the information afresh.
582 void ide_acpi_push_timing(ide_hwif_t *hwif)
585 struct acpi_object_list input;
586 union acpi_object in_params[3];
587 struct ide_acpi_drive_link *master = &hwif->acpidata->master;
588 struct ide_acpi_drive_link *slave = &hwif->acpidata->slave;
593 DEBPRINT("ENTER:\n");
595 if (!hwif->acpidata) {
596 DEBPRINT("no ACPI data for %s\n", hwif->name);
600 /* Give the GTM buffer + drive Identify data to the channel via the
602 /* setup input parameters buffer for _STM */
604 input.pointer = in_params;
605 in_params[0].type = ACPI_TYPE_BUFFER;
606 in_params[0].buffer.length = sizeof(struct GTM_buffer);
607 in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
608 in_params[1].type = ACPI_TYPE_BUFFER;
609 in_params[1].buffer.length = sizeof(struct hd_driveid);
610 in_params[1].buffer.pointer = (u8 *)&master->idbuff;
611 in_params[2].type = ACPI_TYPE_BUFFER;
612 in_params[2].buffer.length = sizeof(struct hd_driveid);
613 in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
614 /* Output buffer: _STM has no output */
616 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
619 if (ACPI_FAILURE(status)) {
620 DEBPRINT("Run _STM error: status = 0x%x\n", status);
622 DEBPRINT("_STM status: %d\n", status);
626 * ide_acpi_set_state - set the channel power state
627 * @hwif: target IDE interface
630 * This function executes the _PS0/_PS3 ACPI method to set the power state.
631 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
633 void ide_acpi_set_state(ide_hwif_t *hwif, int on)
637 if (ide_noacpi || ide_noacpi_psx)
640 DEBPRINT("ENTER:\n");
642 if (!hwif->acpidata) {
643 DEBPRINT("no ACPI data for %s\n", hwif->name);
646 /* channel first and then drives for power on and verse versa for power off */
648 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
649 for (unit = 0; unit < MAX_DRIVES; ++unit) {
650 ide_drive_t *drive = &hwif->drives[unit];
652 if (!drive->acpidata->obj_handle)
653 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
655 if (drive->acpidata->obj_handle && drive->present) {
656 acpi_bus_set_power(drive->acpidata->obj_handle,
657 on? ACPI_STATE_D0: ACPI_STATE_D3);
661 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
665 * ide_acpi_init - initialize the ACPI link for an IDE interface
666 * @hwif: target IDE interface (channel)
668 * The ACPI spec is not quite clear when the drive identify buffer
669 * should be obtained. Calling IDENTIFY DEVICE during shutdown
670 * is not the best of ideas as the drive might already being put to
671 * sleep. And obviously we can't call it during resume.
672 * So we get the information during startup; but this means that
673 * any changes during run-time will be lost after resume.
675 void ide_acpi_init(ide_hwif_t *hwif)
677 ide_acpi_blacklist();
679 hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
683 hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
684 if (!hwif->acpidata->obj_handle) {
685 DEBPRINT("no ACPI object for %s found\n", hwif->name);
686 kfree(hwif->acpidata);
687 hwif->acpidata = NULL;
691 void ide_acpi_port_init_devices(ide_hwif_t *hwif)
696 if (hwif->acpidata == NULL)
700 * The ACPI spec mandates that we send information
701 * for both drives, regardless whether they are connected
704 hwif->drives[0].acpidata = &hwif->acpidata->master;
705 hwif->drives[1].acpidata = &hwif->acpidata->slave;
708 * Send IDENTIFY for each drive
710 for (i = 0; i < MAX_DRIVES; i++) {
711 drive = &hwif->drives[i];
716 err = taskfile_lib_get_identify(drive, drive->acpidata->idbuff);
718 DEBPRINT("identify device %s failed (%d)\n",
722 if (ide_noacpionboot) {
723 DEBPRINT("ACPI methods disabled on boot\n");
727 /* ACPI _PS0 before _STM */
728 ide_acpi_set_state(hwif, 1);
730 * ACPI requires us to call _STM on startup
732 ide_acpi_get_timing(hwif);
733 ide_acpi_push_timing(hwif);
735 for (i = 0; i < MAX_DRIVES; i++) {
736 drive = &hwif->drives[i];
739 /* Execute ACPI startup code */
740 ide_acpi_exec_tfs(drive);