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>
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
{
43 acpi_handle obj_handle
;
47 struct ide_acpi_hwif_link
{
49 acpi_handle obj_handle
;
50 struct GTM_buffer gtm
;
51 struct ide_acpi_drive_link master
;
52 struct ide_acpi_drive_link slave
;
56 /* note: adds function name and KERN_DEBUG */
58 #define DEBPRINT(fmt, args...) \
59 printk(KERN_DEBUG "%s: " fmt, __FUNCTION__, ## args)
61 #define DEBPRINT(fmt, args...) do {} while (0)
62 #endif /* DEBUGGING */
64 extern int ide_noacpi
;
65 extern int ide_noacpitfs
;
66 extern int ide_noacpionboot
;
69 * ide_get_dev_handle - finds acpi_handle and PCI device.function
70 * @dev: device to locate
71 * @handle: returned acpi_handle for @dev
72 * @pcidevfn: return PCI device.func for @dev
74 * Returns the ACPI object handle to the corresponding PCI device.
76 * Returns 0 on success, <0 on error.
78 static int ide_get_dev_handle(struct device
*dev
, acpi_handle
*handle
,
79 acpi_integer
*pcidevfn
)
81 struct pci_dev
*pdev
= to_pci_dev(dev
);
82 unsigned int bus
, devnum
, func
;
84 acpi_handle dev_handle
;
85 struct acpi_buffer buffer
= {.length
= ACPI_ALLOCATE_BUFFER
,
88 struct acpi_device_info
*dinfo
= NULL
;
91 bus
= pdev
->bus
->number
;
92 devnum
= PCI_SLOT(pdev
->devfn
);
93 func
= PCI_FUNC(pdev
->devfn
);
94 /* ACPI _ADR encoding for PCI bus: */
95 addr
= (acpi_integer
)(devnum
<< 16 | func
);
97 DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus
, devnum
, func
);
99 dev_handle
= DEVICE_ACPI_HANDLE(dev
);
101 DEBPRINT("no acpi handle for device\n");
105 status
= acpi_get_object_info(dev_handle
, &buffer
);
106 if (ACPI_FAILURE(status
)) {
107 DEBPRINT("get_object_info for device failed\n");
110 dinfo
= buffer
.pointer
;
111 if (dinfo
&& (dinfo
->valid
& ACPI_VALID_ADR
) &&
112 dinfo
->address
== addr
) {
114 *handle
= dev_handle
;
116 DEBPRINT("get_object_info for device has wrong "
117 " address: %llu, should be %u\n",
118 dinfo
? (unsigned long long)dinfo
->address
: -1ULL,
123 DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
124 devnum
, func
, (unsigned long long)addr
, *handle
);
132 * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
133 * @hwif: device to locate
135 * Retrieves the object handle for a given hwif.
137 * Returns handle on success, 0 on error.
139 static acpi_handle
ide_acpi_hwif_get_handle(ide_hwif_t
*hwif
)
141 struct device
*dev
= hwif
->gendev
.parent
;
142 acpi_handle dev_handle
;
143 acpi_integer pcidevfn
;
144 acpi_handle chan_handle
;
147 DEBPRINT("ENTER: device %s\n", hwif
->name
);
150 DEBPRINT("no PCI device for %s\n", hwif
->name
);
154 err
= ide_get_dev_handle(dev
, &dev_handle
, &pcidevfn
);
156 DEBPRINT("ide_get_dev_handle failed (%d)\n", err
);
160 /* get child objects of dev_handle == channel objects,
161 * + _their_ children == drive objects */
162 /* channel is hwif->channel */
163 chan_handle
= acpi_get_child(dev_handle
, hwif
->channel
);
164 DEBPRINT("chan adr=%d: handle=0x%p\n",
165 hwif
->channel
, chan_handle
);
171 * ide_acpi_drive_get_handle - Get ACPI object handle for a given drive
172 * @drive: device to locate
174 * Retrieves the object handle of a given drive. According to the ACPI
175 * spec the drive is a child of the hwif.
177 * Returns handle on success, 0 on error.
179 static acpi_handle
ide_acpi_drive_get_handle(ide_drive_t
*drive
)
181 ide_hwif_t
*hwif
= HWIF(drive
);
183 acpi_handle drive_handle
;
188 if (!hwif
->acpidata
->obj_handle
)
191 port
= hwif
->channel
? drive
->dn
- 2: drive
->dn
;
193 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
194 drive
->name
, hwif
->channel
, port
);
197 /* TBD: could also check ACPI object VALID bits */
198 drive_handle
= acpi_get_child(hwif
->acpidata
->obj_handle
, port
);
199 DEBPRINT("drive %s handle 0x%p\n", drive
->name
, drive_handle
);
205 * do_drive_get_GTF - get the drive bootup default taskfile settings
206 * @drive: the drive for which the taskfile settings should be retrieved
207 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
208 * @gtf_address: buffer containing _GTF taskfile arrays
210 * The _GTF method has no input parameters.
211 * It returns a variable number of register set values (registers
212 * hex 1F1..1F7, taskfiles).
213 * The <variable number> is not known in advance, so have ACPI-CA
214 * allocate the buffer as needed and return it, then free it later.
216 * The returned @gtf_length and @gtf_address are only valid if the
217 * function return value is 0.
219 static int do_drive_get_GTF(ide_drive_t
*drive
,
220 unsigned int *gtf_length
, unsigned long *gtf_address
,
221 unsigned long *obj_loc
)
224 struct acpi_buffer output
;
225 union acpi_object
*out_obj
;
226 ide_hwif_t
*hwif
= HWIF(drive
);
227 struct device
*dev
= hwif
->gendev
.parent
;
239 DEBPRINT("no PCI device for %s\n", hwif
->name
);
243 if (!hwif
->acpidata
) {
244 DEBPRINT("no ACPI data for %s\n", hwif
->name
);
248 port
= hwif
->channel
? drive
->dn
- 2: drive
->dn
;
250 if (!drive
->acpidata
) {
252 drive
->acpidata
= &hwif
->acpidata
->master
;
253 hwif
->acpidata
->master
.drive
= drive
;
255 drive
->acpidata
= &hwif
->acpidata
->slave
;
256 hwif
->acpidata
->slave
.drive
= drive
;
260 DEBPRINT("ENTER: %s at %s, port#: %d, hard_port#: %d\n",
261 hwif
->name
, dev
->bus_id
, port
, hwif
->channel
);
263 if (!drive
->present
) {
264 DEBPRINT("%s drive %d:%d not present\n",
265 hwif
->name
, hwif
->channel
, port
);
269 /* Get this drive's _ADR info. if not already known. */
270 if (!drive
->acpidata
->obj_handle
) {
271 drive
->acpidata
->obj_handle
= ide_acpi_drive_get_handle(drive
);
272 if (!drive
->acpidata
->obj_handle
) {
273 DEBPRINT("No ACPI object found for %s\n",
279 /* Setting up output buffer */
280 output
.length
= ACPI_ALLOCATE_BUFFER
;
281 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
283 /* _GTF has no input parameters */
285 status
= acpi_evaluate_object(drive
->acpidata
->obj_handle
, "_GTF",
287 if (ACPI_FAILURE(status
)) {
289 "%s: Run _GTF error: status = 0x%x\n",
290 __FUNCTION__
, status
);
294 if (!output
.length
|| !output
.pointer
) {
295 DEBPRINT("Run _GTF: "
296 "length or ptr is NULL (0x%llx, 0x%p)\n",
297 (unsigned long long)output
.length
,
302 out_obj
= output
.pointer
;
303 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
304 DEBPRINT("Run _GTF: error: "
305 "expected object type of ACPI_TYPE_BUFFER, "
306 "got 0x%x\n", out_obj
->type
);
308 kfree(output
.pointer
);
312 if (!out_obj
->buffer
.length
|| !out_obj
->buffer
.pointer
||
313 out_obj
->buffer
.length
% REGS_PER_GTF
) {
315 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
316 __FUNCTION__
, out_obj
->buffer
.length
,
317 out_obj
->buffer
.pointer
);
319 kfree(output
.pointer
);
323 *gtf_length
= out_obj
->buffer
.length
;
324 *gtf_address
= (unsigned long)out_obj
->buffer
.pointer
;
325 *obj_loc
= (unsigned long)out_obj
;
326 DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
327 *gtf_length
, *gtf_address
, *obj_loc
);
334 * taskfile_load_raw - send taskfile registers to drive
335 * @drive: drive to which output is sent
336 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
338 * Outputs IDE taskfile to the drive.
340 static int taskfile_load_raw(ide_drive_t
*drive
,
341 const struct taskfile_array
*gtf
)
346 DEBPRINT("(0x1f1-1f7): hex: "
347 "%02x %02x %02x %02x %02x %02x %02x\n",
348 gtf
->tfa
[0], gtf
->tfa
[1], gtf
->tfa
[2],
349 gtf
->tfa
[3], gtf
->tfa
[4], gtf
->tfa
[5], gtf
->tfa
[6]);
351 memset(&args
, 0, sizeof(ide_task_t
));
352 args
.command_type
= IDE_DRIVE_TASK_NO_DATA
;
353 args
.data_phase
= TASKFILE_IN
;
354 args
.handler
= &task_no_data_intr
;
356 /* convert gtf to IDE Taskfile */
357 args
.tfRegister
[1] = gtf
->tfa
[0]; /* 0x1f1 */
358 args
.tfRegister
[2] = gtf
->tfa
[1]; /* 0x1f2 */
359 args
.tfRegister
[3] = gtf
->tfa
[2]; /* 0x1f3 */
360 args
.tfRegister
[4] = gtf
->tfa
[3]; /* 0x1f4 */
361 args
.tfRegister
[5] = gtf
->tfa
[4]; /* 0x1f5 */
362 args
.tfRegister
[6] = gtf
->tfa
[5]; /* 0x1f6 */
363 args
.tfRegister
[7] = gtf
->tfa
[6]; /* 0x1f7 */
366 DEBPRINT("_GTF execution disabled\n");
370 err
= ide_raw_taskfile(drive
, &args
, NULL
);
372 printk(KERN_ERR
"%s: ide_raw_taskfile failed: %u\n",
379 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
380 * @drive: the drive to which the taskfile command should be sent
381 * @gtf_length: total number of bytes of _GTF taskfiles
382 * @gtf_address: location of _GTF taskfile arrays
384 * Write {gtf_address, length gtf_length} in groups of
385 * REGS_PER_GTF bytes.
387 static int do_drive_set_taskfiles(ide_drive_t
*drive
,
388 unsigned int gtf_length
,
389 unsigned long gtf_address
)
391 int rc
= -ENODEV
, err
;
392 int gtf_count
= gtf_length
/ REGS_PER_GTF
;
394 struct taskfile_array
*gtf
;
399 DEBPRINT("ENTER: %s, hard_port#: %d\n", drive
->name
, drive
->dn
);
403 if (!gtf_count
) /* shouldn't be here */
406 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
407 gtf_length
, gtf_length
, gtf_count
, gtf_address
);
409 if (gtf_length
% REGS_PER_GTF
) {
410 printk(KERN_ERR
"%s: unexpected GTF length (%d)\n",
411 __FUNCTION__
, gtf_length
);
416 for (ix
= 0; ix
< gtf_count
; ix
++) {
417 gtf
= (struct taskfile_array
*)
418 (gtf_address
+ ix
* REGS_PER_GTF
);
420 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
421 err
= taskfile_load_raw(drive
, gtf
);
431 * ide_acpi_exec_tfs - get then write drive taskfile settings
432 * @drive: the drive for which the taskfile settings should be
435 * According to the ACPI spec this should be called after _STM
436 * has been evaluated for the interface. Some ACPI vendors interpret
437 * that as a hard requirement and modify the taskfile according
438 * to the Identify Drive information passed down with _STM.
439 * So one should really make sure to call this only after _STM has
442 int ide_acpi_exec_tfs(ide_drive_t
*drive
)
445 unsigned int gtf_length
;
446 unsigned long gtf_address
;
447 unsigned long obj_loc
;
452 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive
->name
, drive
->dn
);
454 ret
= do_drive_get_GTF(drive
, >f_length
, >f_address
, &obj_loc
);
456 DEBPRINT("get_GTF error (%d)\n", ret
);
460 DEBPRINT("call set_taskfiles, drive=%s\n", drive
->name
);
462 ret
= do_drive_set_taskfiles(drive
, gtf_length
, gtf_address
);
463 kfree((void *)obj_loc
);
465 DEBPRINT("set_taskfiles error (%d)\n", ret
);
468 DEBPRINT("ret=%d\n", ret
);
472 EXPORT_SYMBOL_GPL(ide_acpi_exec_tfs
);
475 * ide_acpi_get_timing - get the channel (controller) timings
476 * @hwif: target IDE interface (channel)
478 * This function executes the _GTM ACPI method for the target channel.
481 void ide_acpi_get_timing(ide_hwif_t
*hwif
)
484 struct acpi_buffer output
;
485 union acpi_object
*out_obj
;
490 DEBPRINT("ENTER:\n");
492 if (!hwif
->acpidata
) {
493 DEBPRINT("no ACPI data for %s\n", hwif
->name
);
497 /* Setting up output buffer for _GTM */
498 output
.length
= ACPI_ALLOCATE_BUFFER
;
499 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
501 /* _GTM has no input parameters */
502 status
= acpi_evaluate_object(hwif
->acpidata
->obj_handle
, "_GTM",
505 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
506 status
, output
.pointer
,
507 (unsigned long long)output
.length
);
509 if (ACPI_FAILURE(status
)) {
510 DEBPRINT("Run _GTM error: status = 0x%x\n", status
);
514 if (!output
.length
|| !output
.pointer
) {
515 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
516 (unsigned long long)output
.length
,
518 kfree(output
.pointer
);
522 out_obj
= output
.pointer
;
523 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
524 kfree(output
.pointer
);
525 DEBPRINT("Run _GTM: error: "
526 "expected object type of ACPI_TYPE_BUFFER, "
527 "got 0x%x\n", out_obj
->type
);
531 if (!out_obj
->buffer
.length
|| !out_obj
->buffer
.pointer
||
532 out_obj
->buffer
.length
!= sizeof(struct GTM_buffer
)) {
533 kfree(output
.pointer
);
535 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
537 __FUNCTION__
, out_obj
->buffer
.length
,
538 sizeof(struct GTM_buffer
), out_obj
->buffer
.pointer
);
542 memcpy(&hwif
->acpidata
->gtm
, out_obj
->buffer
.pointer
,
543 sizeof(struct GTM_buffer
));
545 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
546 out_obj
->buffer
.pointer
, out_obj
->buffer
.length
,
547 sizeof(struct GTM_buffer
));
549 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
550 hwif
->acpidata
->gtm
.PIO_speed0
,
551 hwif
->acpidata
->gtm
.DMA_speed0
,
552 hwif
->acpidata
->gtm
.PIO_speed1
,
553 hwif
->acpidata
->gtm
.DMA_speed1
,
554 hwif
->acpidata
->gtm
.GTM_flags
);
556 kfree(output
.pointer
);
558 EXPORT_SYMBOL_GPL(ide_acpi_get_timing
);
561 * ide_acpi_push_timing - set the channel (controller) timings
562 * @hwif: target IDE interface (channel)
564 * This function executes the _STM ACPI method for the target channel.
566 * _STM requires Identify Drive data, which has to passed as an argument.
567 * Unfortunately hd_driveid is a mangled version which we can't readily
568 * use; hence we'll get the information afresh.
570 void ide_acpi_push_timing(ide_hwif_t
*hwif
)
573 struct acpi_object_list input
;
574 union acpi_object in_params
[3];
575 struct ide_acpi_drive_link
*master
= &hwif
->acpidata
->master
;
576 struct ide_acpi_drive_link
*slave
= &hwif
->acpidata
->slave
;
581 DEBPRINT("ENTER:\n");
583 if (!hwif
->acpidata
) {
584 DEBPRINT("no ACPI data for %s\n", hwif
->name
);
588 /* Give the GTM buffer + drive Identify data to the channel via the
590 /* setup input parameters buffer for _STM */
592 input
.pointer
= in_params
;
593 in_params
[0].type
= ACPI_TYPE_BUFFER
;
594 in_params
[0].buffer
.length
= sizeof(struct GTM_buffer
);
595 in_params
[0].buffer
.pointer
= (u8
*)&hwif
->acpidata
->gtm
;
596 in_params
[1].type
= ACPI_TYPE_BUFFER
;
597 in_params
[1].buffer
.length
= sizeof(struct hd_driveid
);
598 in_params
[1].buffer
.pointer
= (u8
*)&master
->idbuff
;
599 in_params
[2].type
= ACPI_TYPE_BUFFER
;
600 in_params
[2].buffer
.length
= sizeof(struct hd_driveid
);
601 in_params
[2].buffer
.pointer
= (u8
*)&slave
->idbuff
;
602 /* Output buffer: _STM has no output */
604 status
= acpi_evaluate_object(hwif
->acpidata
->obj_handle
, "_STM",
607 if (ACPI_FAILURE(status
)) {
608 DEBPRINT("Run _STM error: status = 0x%x\n", status
);
610 DEBPRINT("_STM status: %d\n", status
);
612 EXPORT_SYMBOL_GPL(ide_acpi_push_timing
);
615 * ide_acpi_set_state - set the channel power state
616 * @hwif: target IDE interface
619 * This function executes the _PS0/_PS3 ACPI method to set the power state.
620 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
622 void ide_acpi_set_state(ide_hwif_t
*hwif
, int on
)
629 DEBPRINT("ENTER:\n");
631 if (!hwif
->acpidata
) {
632 DEBPRINT("no ACPI data for %s\n", hwif
->name
);
635 /* channel first and then drives for power on and verse versa for power off */
637 acpi_bus_set_power(hwif
->acpidata
->obj_handle
, ACPI_STATE_D0
);
638 for (unit
= 0; unit
< MAX_DRIVES
; ++unit
) {
639 ide_drive_t
*drive
= &hwif
->drives
[unit
];
641 if (!drive
->acpidata
->obj_handle
)
642 drive
->acpidata
->obj_handle
= ide_acpi_drive_get_handle(drive
);
644 if (drive
->acpidata
->obj_handle
&& drive
->present
) {
645 acpi_bus_set_power(drive
->acpidata
->obj_handle
,
646 on
? ACPI_STATE_D0
: ACPI_STATE_D3
);
650 acpi_bus_set_power(hwif
->acpidata
->obj_handle
, ACPI_STATE_D3
);
654 * ide_acpi_init - initialize the ACPI link for an IDE interface
655 * @hwif: target IDE interface (channel)
657 * The ACPI spec is not quite clear when the drive identify buffer
658 * should be obtained. Calling IDENTIFY DEVICE during shutdown
659 * is not the best of ideas as the drive might already being put to
660 * sleep. And obviously we can't call it during resume.
661 * So we get the information during startup; but this means that
662 * any changes during run-time will be lost after resume.
664 void ide_acpi_init(ide_hwif_t
*hwif
)
668 struct ide_acpi_drive_link
*master
;
669 struct ide_acpi_drive_link
*slave
;
671 hwif
->acpidata
= kzalloc(sizeof(struct ide_acpi_hwif_link
), GFP_KERNEL
);
675 hwif
->acpidata
->obj_handle
= ide_acpi_hwif_get_handle(hwif
);
676 if (!hwif
->acpidata
->obj_handle
) {
677 DEBPRINT("no ACPI object for %s found\n", hwif
->name
);
678 kfree(hwif
->acpidata
);
679 hwif
->acpidata
= NULL
;
684 * The ACPI spec mandates that we send information
685 * for both drives, regardless whether they are connected
688 hwif
->acpidata
->master
.drive
= &hwif
->drives
[0];
689 hwif
->drives
[0].acpidata
= &hwif
->acpidata
->master
;
690 master
= &hwif
->acpidata
->master
;
692 hwif
->acpidata
->slave
.drive
= &hwif
->drives
[1];
693 hwif
->drives
[1].acpidata
= &hwif
->acpidata
->slave
;
694 slave
= &hwif
->acpidata
->slave
;
698 * Send IDENTIFY for each drive
700 if (master
->drive
->present
) {
701 err
= taskfile_lib_get_identify(master
->drive
, master
->idbuff
);
703 DEBPRINT("identify device %s failed (%d)\n",
704 master
->drive
->name
, err
);
708 if (slave
->drive
->present
) {
709 err
= taskfile_lib_get_identify(slave
->drive
, slave
->idbuff
);
711 DEBPRINT("identify device %s failed (%d)\n",
712 slave
->drive
->name
, err
);
716 if (ide_noacpionboot
) {
717 DEBPRINT("ACPI methods disabled on boot\n");
721 /* ACPI _PS0 before _STM */
722 ide_acpi_set_state(hwif
, 1);
724 * ACPI requires us to call _STM on startup
726 ide_acpi_get_timing(hwif
);
727 ide_acpi_push_timing(hwif
);
729 for (unit
= 0; unit
< MAX_DRIVES
; ++unit
) {
730 ide_drive_t
*drive
= &hwif
->drives
[unit
];
732 if (drive
->present
) {
733 /* Execute ACPI startup code */
734 ide_acpi_exec_tfs(drive
);
738 EXPORT_SYMBOL_GPL(ide_acpi_init
);