3 * Provides ACPI support for PATA/SATA.
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/acpi.h>
15 #include <linux/libata.h>
16 #include <linux/pci.h>
19 #include <acpi/acpi_bus.h>
20 #include <acpi/acnames.h>
21 #include <acpi/acnamesp.h>
22 #include <acpi/acparser.h>
23 #include <acpi/acexcep.h>
24 #include <acpi/acmacros.h>
25 #include <acpi/actypes.h>
27 #define SATA_ROOT_PORT(x) (((x) >> 16) & 0xffff)
28 #define SATA_PORT_NUMBER(x) ((x) & 0xffff) /* or NO_PORT_MULT */
29 #define NO_PORT_MULT 0xffff
30 #define SATA_ADR_RSVD 0xffffffff
32 #define REGS_PER_GTF 7
33 struct taskfile_array
{
34 u8 tfa
[REGS_PER_GTF
]; /* regs. 0x1f1 - 0x1f7 */
38 * Helper - belongs in the PCI layer somewhere eventually
40 static int is_pci_dev(struct device
*dev
)
42 return (dev
->bus
== &pci_bus_type
);
46 * sata_get_dev_handle - finds acpi_handle and PCI device.function
47 * @dev: device to locate
48 * @handle: returned acpi_handle for @dev
49 * @pcidevfn: return PCI device.func for @dev
51 * This function is somewhat SATA-specific. Or at least the
52 * PATA & SATA versions of this function are different,
53 * so it's not entirely generic code.
55 * Returns 0 on success, <0 on error.
57 static int sata_get_dev_handle(struct device
*dev
, acpi_handle
*handle
,
58 acpi_integer
*pcidevfn
)
60 struct pci_dev
*pci_dev
;
66 pci_dev
= to_pci_dev(dev
); /* NOTE: PCI-specific */
67 /* Please refer to the ACPI spec for the syntax of _ADR. */
68 addr
= (PCI_SLOT(pci_dev
->devfn
) << 16) | PCI_FUNC(pci_dev
->devfn
);
70 *handle
= acpi_get_child(DEVICE_ACPI_HANDLE(dev
->parent
), addr
);
77 * pata_get_dev_handle - finds acpi_handle and PCI device.function
78 * @dev: device to locate
79 * @handle: returned acpi_handle for @dev
80 * @pcidevfn: return PCI device.func for @dev
82 * The PATA and SATA versions of this function are different.
84 * Returns 0 on success, <0 on error.
86 static int pata_get_dev_handle(struct device
*dev
, acpi_handle
*handle
,
87 acpi_integer
*pcidevfn
)
89 unsigned int bus
, devnum
, func
;
91 acpi_handle dev_handle
, parent_handle
;
92 struct acpi_buffer buffer
= {.length
= ACPI_ALLOCATE_BUFFER
,
95 struct acpi_device_info
*dinfo
= NULL
;
102 pdev
= to_pci_dev(dev
);
104 bus
= pdev
->bus
->number
;
105 devnum
= PCI_SLOT(pdev
->devfn
);
106 func
= PCI_FUNC(pdev
->devfn
);
108 dev_handle
= DEVICE_ACPI_HANDLE(dev
);
109 parent_handle
= DEVICE_ACPI_HANDLE(dev
->parent
);
111 status
= acpi_get_object_info(parent_handle
, &buffer
);
112 if (ACPI_FAILURE(status
))
115 dinfo
= buffer
.pointer
;
116 if (dinfo
&& (dinfo
->valid
& ACPI_VALID_ADR
) &&
117 dinfo
->address
== bus
) {
118 /* ACPI spec for _ADR for PCI bus: */
119 addr
= (acpi_integer
)(devnum
<< 16 | func
);
121 *handle
= dev_handle
;
134 struct walk_info
{ /* can be trimmed some */
136 struct acpi_device
*adev
;
138 acpi_integer pcidevfn
;
139 unsigned int drivenum
;
140 acpi_handle obj_handle
;
141 struct ata_port
*ataport
;
142 struct ata_device
*atadev
;
145 char basepath
[ACPI_PATHNAME_MAX
];
149 static acpi_status
get_devices(acpi_handle handle
,
150 u32 level
, void *context
, void **return_value
)
153 struct walk_info
*winfo
= context
;
154 struct acpi_buffer namebuf
= {ACPI_ALLOCATE_BUFFER
, NULL
};
156 struct acpi_buffer buffer
;
157 struct acpi_device_info
*dinfo
;
159 status
= acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &namebuf
);
162 pathname
= namebuf
.pointer
;
164 buffer
.length
= ACPI_ALLOCATE_BUFFER
;
165 buffer
.pointer
= NULL
;
166 status
= acpi_get_object_info(handle
, &buffer
);
167 if (ACPI_FAILURE(status
))
170 dinfo
= buffer
.pointer
;
172 /* find full device path name for pcidevfn */
173 if (dinfo
&& (dinfo
->valid
& ACPI_VALID_ADR
) &&
174 dinfo
->address
== winfo
->pcidevfn
) {
175 if (ata_msg_probe(winfo
->ataport
))
176 ata_dev_printk(winfo
->atadev
, KERN_DEBUG
,
177 ":%s: matches pcidevfn (0x%llx)\n",
178 pathname
, winfo
->pcidevfn
);
179 strlcpy(winfo
->basepath
, pathname
,
180 sizeof(winfo
->basepath
));
181 winfo
->basepath_len
= strlen(pathname
);
185 /* if basepath is not yet known, ignore this object */
186 if (!winfo
->basepath_len
)
189 /* if this object is in scope of basepath, maybe use it */
190 if (strncmp(pathname
, winfo
->basepath
,
191 winfo
->basepath_len
) == 0) {
192 if (!(dinfo
->valid
& ACPI_VALID_ADR
))
194 if (ata_msg_probe(winfo
->ataport
))
195 ata_dev_printk(winfo
->atadev
, KERN_DEBUG
,
196 "GOT ONE: (%s) root_port = 0x%llx,"
197 " port_num = 0x%llx\n", pathname
,
198 SATA_ROOT_PORT(dinfo
->address
),
199 SATA_PORT_NUMBER(dinfo
->address
));
201 if (SATA_PORT_NUMBER(dinfo
->address
) != NO_PORT_MULT
)
202 if (ata_msg_probe(winfo
->ataport
))
203 ata_dev_printk(winfo
->atadev
,
204 KERN_DEBUG
, "warning: don't"
205 " know how to handle SATA port"
207 if (SATA_ROOT_PORT(dinfo
->address
) ==
208 winfo
->ataport
->port_no
&&
209 SATA_PORT_NUMBER(dinfo
->address
) == NO_PORT_MULT
) {
210 if (ata_msg_probe(winfo
->ataport
))
211 ata_dev_printk(winfo
->atadev
,
213 "THIS ^^^^^ is the requested"
214 " SATA drive (handle = 0x%p)\n",
216 winfo
->sata_adr
= dinfo
->address
;
217 winfo
->obj_handle
= handle
;
229 /* Get the SATA drive _ADR object. */
230 static int get_sata_adr(struct device
*dev
, acpi_handle handle
,
231 acpi_integer pcidevfn
, unsigned int drive
,
233 struct ata_device
*atadev
, u32
*dev_adr
)
236 struct walk_info
*winfo
;
239 winfo
= kzalloc(sizeof(struct walk_info
), GFP_KERNEL
);
244 winfo
->atadev
= atadev
;
246 if (acpi_bus_get_device(handle
, &winfo
->adev
) < 0)
247 if (ata_msg_probe(ap
))
248 ata_dev_printk(winfo
->atadev
, KERN_DEBUG
,
249 "acpi_bus_get_device failed\n");
250 winfo
->handle
= handle
;
251 winfo
->pcidevfn
= pcidevfn
;
252 winfo
->drivenum
= drive
;
254 status
= acpi_get_devices(NULL
, get_devices
, winfo
, NULL
);
255 if (ACPI_FAILURE(status
)) {
256 if (ata_msg_probe(ap
))
257 ata_dev_printk(winfo
->atadev
, KERN_DEBUG
,
258 "%s: acpi_get_devices failed\n",
262 *dev_adr
= winfo
->sata_adr
;
263 atadev
->obj_handle
= winfo
->obj_handle
;
272 * do_drive_get_GTF - get the drive bootup default taskfile settings
273 * @dev: target ATA device
274 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
275 * @gtf_address: buffer containing _GTF taskfile arrays
277 * This applies to both PATA and SATA drives.
279 * The _GTF method has no input parameters.
280 * It returns a variable number of register set values (registers
281 * hex 1F1..1F7, taskfiles).
282 * The <variable number> is not known in advance, so have ACPI-CA
283 * allocate the buffer as needed and return it, then free it later.
285 * The returned @gtf_length and @gtf_address are only valid if the
286 * function return value is 0.
288 static int do_drive_get_GTF(struct ata_device
*dev
, unsigned int *gtf_length
,
289 unsigned long *gtf_address
, unsigned long *obj_loc
)
291 struct ata_port
*ap
= dev
->ap
;
293 acpi_handle dev_handle
= NULL
;
294 acpi_handle chan_handle
, drive_handle
;
295 acpi_integer pcidevfn
= 0;
297 struct acpi_buffer output
;
298 union acpi_object
*out_obj
;
299 struct device
*gdev
= ap
->host
->dev
;
309 if (ata_msg_probe(ap
))
310 ata_dev_printk(dev
, KERN_DEBUG
, "%s: ENTER: port#: %d\n",
311 __FUNCTION__
, ap
->port_no
);
313 if (!ata_dev_enabled(dev
) || (ap
->flags
& ATA_FLAG_DISABLED
)) {
314 if (ata_msg_probe(ap
))
315 ata_dev_printk(dev
, KERN_DEBUG
, "%s: ERR: "
316 "ata_dev_present: %d, PORT_DISABLED: %lu\n",
317 __FUNCTION__
, ata_dev_enabled(dev
),
318 ap
->flags
& ATA_FLAG_DISABLED
);
322 /* Don't continue if device has no _ADR method.
323 * _GTF is intended for known motherboard devices. */
324 if (!(ap
->flags
& ATA_FLAG_ACPI_SATA
)) {
325 err
= pata_get_dev_handle(gdev
, &dev_handle
, &pcidevfn
);
327 if (ata_msg_probe(ap
))
328 ata_dev_printk(dev
, KERN_DEBUG
,
329 "%s: pata_get_dev_handle failed (%d)\n",
334 err
= sata_get_dev_handle(gdev
, &dev_handle
, &pcidevfn
);
336 if (ata_msg_probe(ap
))
337 ata_dev_printk(dev
, KERN_DEBUG
,
338 "%s: sata_get_dev_handle failed (%d\n",
344 /* Get this drive's _ADR info. if not already known. */
345 if (!dev
->obj_handle
) {
346 if (!(ap
->flags
& ATA_FLAG_ACPI_SATA
)) {
347 /* get child objects of dev_handle == channel objects,
348 * + _their_ children == drive objects */
349 /* channel is ap->port_no */
350 chan_handle
= acpi_get_child(dev_handle
,
352 if (ata_msg_probe(ap
))
353 ata_dev_printk(dev
, KERN_DEBUG
,
354 "%s: chan adr=%d: chan_handle=0x%p\n",
355 __FUNCTION__
, ap
->port_no
,
361 /* TBD: could also check ACPI object VALID bits */
362 drive_handle
= acpi_get_child(chan_handle
, dev
->devno
);
367 dev_adr
= dev
->devno
;
368 dev
->obj_handle
= drive_handle
;
369 } else { /* for SATA mode */
370 dev_adr
= SATA_ADR_RSVD
;
371 err
= get_sata_adr(gdev
, dev_handle
, pcidevfn
, 0,
374 if (err
< 0 || dev_adr
== SATA_ADR_RSVD
||
376 if (ata_msg_probe(ap
))
377 ata_dev_printk(dev
, KERN_DEBUG
,
378 "%s: get_sata/pata_adr failed: "
379 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
380 __FUNCTION__
, err
, dev_adr
,
386 /* Setting up output buffer */
387 output
.length
= ACPI_ALLOCATE_BUFFER
;
388 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
390 /* _GTF has no input parameters */
392 status
= acpi_evaluate_object(dev
->obj_handle
, "_GTF",
394 if (ACPI_FAILURE(status
)) {
395 if (ata_msg_probe(ap
))
396 ata_dev_printk(dev
, KERN_DEBUG
,
397 "%s: Run _GTF error: status = 0x%x\n",
398 __FUNCTION__
, status
);
402 if (!output
.length
|| !output
.pointer
) {
403 if (ata_msg_probe(ap
))
404 ata_dev_printk(dev
, KERN_DEBUG
, "%s: Run _GTF: "
405 "length or ptr is NULL (0x%llx, 0x%p)\n",
407 (unsigned long long)output
.length
,
409 kfree(output
.pointer
);
413 out_obj
= output
.pointer
;
414 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
415 kfree(output
.pointer
);
416 if (ata_msg_probe(ap
))
417 ata_dev_printk(dev
, KERN_DEBUG
, "%s: Run _GTF: "
418 "error: expected object type of "
419 " ACPI_TYPE_BUFFER, got 0x%x\n",
420 __FUNCTION__
, out_obj
->type
);
425 if (!out_obj
->buffer
.length
|| !out_obj
->buffer
.pointer
||
426 out_obj
->buffer
.length
% REGS_PER_GTF
) {
428 ata_dev_printk(dev
, KERN_ERR
,
429 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
430 __FUNCTION__
, out_obj
->buffer
.length
,
431 out_obj
->buffer
.pointer
);
436 *gtf_length
= out_obj
->buffer
.length
;
437 *gtf_address
= (unsigned long)out_obj
->buffer
.pointer
;
438 *obj_loc
= (unsigned long)out_obj
;
439 if (ata_msg_probe(ap
))
440 ata_dev_printk(dev
, KERN_DEBUG
, "%s: returning "
441 "gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
442 __FUNCTION__
, *gtf_length
, *gtf_address
, *obj_loc
);
449 * taskfile_load_raw - send taskfile registers to host controller
450 * @dev: target ATA device
451 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
453 * Outputs ATA taskfile to standard ATA host controller using MMIO
454 * or PIO as indicated by the ATA_FLAG_MMIO flag.
455 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
456 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
457 * hob_lbal, hob_lbam, and hob_lbah.
459 * This function waits for idle (!BUSY and !DRQ) after writing
460 * registers. If the control register has a new value, this
461 * function also waits for idle after writing control and before
462 * writing the remaining registers.
465 * Inherited from caller.
467 static void taskfile_load_raw(struct ata_device
*dev
,
468 const struct taskfile_array
*gtf
)
470 struct ata_port
*ap
= dev
->ap
;
471 struct ata_taskfile tf
;
474 if (ata_msg_probe(ap
))
475 ata_dev_printk(dev
, KERN_DEBUG
, "%s: (0x1f1-1f7): hex: "
476 "%02x %02x %02x %02x %02x %02x %02x\n",
478 gtf
->tfa
[0], gtf
->tfa
[1], gtf
->tfa
[2],
479 gtf
->tfa
[3], gtf
->tfa
[4], gtf
->tfa
[5], gtf
->tfa
[6]);
481 if ((gtf
->tfa
[0] == 0) && (gtf
->tfa
[1] == 0) && (gtf
->tfa
[2] == 0)
482 && (gtf
->tfa
[3] == 0) && (gtf
->tfa
[4] == 0) && (gtf
->tfa
[5] == 0)
483 && (gtf
->tfa
[6] == 0))
486 ata_tf_init(dev
, &tf
);
488 /* convert gtf to tf */
489 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
; /* TBD */
490 tf
.protocol
= ATA_PROT_NODATA
;
491 tf
.feature
= gtf
->tfa
[0]; /* 0x1f1 */
492 tf
.nsect
= gtf
->tfa
[1]; /* 0x1f2 */
493 tf
.lbal
= gtf
->tfa
[2]; /* 0x1f3 */
494 tf
.lbam
= gtf
->tfa
[3]; /* 0x1f4 */
495 tf
.lbah
= gtf
->tfa
[4]; /* 0x1f5 */
496 tf
.device
= gtf
->tfa
[5]; /* 0x1f6 */
497 tf
.command
= gtf
->tfa
[6]; /* 0x1f7 */
499 err
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0);
500 if (err
&& ata_msg_probe(ap
))
501 ata_dev_printk(dev
, KERN_ERR
,
502 "%s: ata_exec_internal failed: %u\n",
507 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
508 * @dev: target ATA device
509 * @gtf_length: total number of bytes of _GTF taskfiles
510 * @gtf_address: location of _GTF taskfile arrays
512 * This applies to both PATA and SATA drives.
514 * Write {gtf_address, length gtf_length} in groups of
515 * REGS_PER_GTF bytes.
517 static int do_drive_set_taskfiles(struct ata_device
*dev
,
518 unsigned int gtf_length
,
519 unsigned long gtf_address
)
521 struct ata_port
*ap
= dev
->ap
;
523 int gtf_count
= gtf_length
/ REGS_PER_GTF
;
525 struct taskfile_array
*gtf
;
527 if (ata_msg_probe(ap
))
528 ata_dev_printk(dev
, KERN_DEBUG
, "%s: ENTER: port#: %d\n",
529 __FUNCTION__
, ap
->port_no
);
531 if (libata_noacpi
|| !(ap
->flags
& ATA_FLAG_ACPI_SATA
))
534 if (!ata_dev_enabled(dev
) || (ap
->flags
& ATA_FLAG_DISABLED
))
536 if (!gtf_count
) /* shouldn't be here */
539 if (gtf_length
% REGS_PER_GTF
) {
541 ata_dev_printk(dev
, KERN_ERR
,
542 "%s: unexpected GTF length (%d)\n",
543 __FUNCTION__
, gtf_length
);
547 for (ix
= 0; ix
< gtf_count
; ix
++) {
548 gtf
= (struct taskfile_array
*)
549 (gtf_address
+ ix
* REGS_PER_GTF
);
551 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
552 taskfile_load_raw(dev
, gtf
);
561 * ata_acpi_exec_tfs - get then write drive taskfile settings
562 * @ap: the ata_port for the drive
564 * This applies to both PATA and SATA drives.
566 int ata_acpi_exec_tfs(struct ata_port
*ap
)
570 unsigned int gtf_length
;
571 unsigned long gtf_address
;
572 unsigned long obj_loc
;
577 * TBD - implement PATA support. For now,
578 * we should not run GTF on PATA devices since some
579 * PATA require execution of GTM/STM before GTF.
581 if (!(ap
->flags
& ATA_FLAG_ACPI_SATA
))
584 for (ix
= 0; ix
< ATA_MAX_DEVICES
; ix
++) {
585 struct ata_device
*dev
= &ap
->device
[ix
];
587 if (!ata_dev_enabled(dev
))
590 ret
= do_drive_get_GTF(dev
, >f_length
, >f_address
,
593 if (ata_msg_probe(ap
))
594 ata_port_printk(ap
, KERN_DEBUG
,
595 "%s: get_GTF error (%d)\n",
600 ret
= do_drive_set_taskfiles(dev
, gtf_length
, gtf_address
);
601 kfree((void *)obj_loc
);
603 if (ata_msg_probe(ap
))
604 ata_port_printk(ap
, KERN_DEBUG
,
605 "%s: set_taskfiles error (%d)\n",
615 * ata_acpi_push_id - send Identify data to drive
616 * @dev: target ATA device
618 * _SDD ACPI object: for SATA mode only
619 * Must be after Identify (Packet) Device -- uses its data
620 * ATM this function never returns a failure. It is an optional
621 * method and if it fails for whatever reason, we should still
624 int ata_acpi_push_id(struct ata_device
*dev
)
626 struct ata_port
*ap
= dev
->ap
;
628 acpi_integer pcidevfn
;
630 struct device
*gdev
= ap
->host
->dev
;
633 struct acpi_object_list input
;
634 union acpi_object in_params
[1];
639 if (ata_msg_probe(ap
))
640 ata_dev_printk(dev
, KERN_DEBUG
, "%s: ix = %d, port#: %d\n",
641 __FUNCTION__
, dev
->devno
, ap
->port_no
);
643 /* Don't continue if not a SATA device. */
644 if (!(ap
->flags
& ATA_FLAG_ACPI_SATA
)) {
645 if (ata_msg_probe(ap
))
646 ata_dev_printk(dev
, KERN_DEBUG
,
647 "%s: Not a SATA device\n", __FUNCTION__
);
651 /* Don't continue if device has no _ADR method.
652 * _SDD is intended for known motherboard devices. */
653 err
= sata_get_dev_handle(gdev
, &handle
, &pcidevfn
);
655 if (ata_msg_probe(ap
))
656 ata_dev_printk(dev
, KERN_DEBUG
,
657 "%s: sata_get_dev_handle failed (%d\n",
662 /* Get this drive's _ADR info, if not already known */
663 if (!dev
->obj_handle
) {
664 dev_adr
= SATA_ADR_RSVD
;
665 err
= get_sata_adr(gdev
, handle
, pcidevfn
, dev
->devno
, ap
, dev
,
667 if (err
< 0 || dev_adr
== SATA_ADR_RSVD
||
669 if (ata_msg_probe(ap
))
670 ata_dev_printk(dev
, KERN_DEBUG
,
671 "%s: get_sata_adr failed: "
672 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
673 __FUNCTION__
, err
, dev_adr
,
679 /* Give the drive Identify data to the drive via the _SDD method */
680 /* _SDD: set up input parameters */
682 input
.pointer
= in_params
;
683 in_params
[0].type
= ACPI_TYPE_BUFFER
;
684 in_params
[0].buffer
.length
= sizeof(dev
->id
[0]) * ATA_ID_WORDS
;
685 in_params
[0].buffer
.pointer
= (u8
*)dev
->id
;
686 /* Output buffer: _SDD has no output */
688 /* It's OK for _SDD to be missing too. */
689 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
690 status
= acpi_evaluate_object(dev
->obj_handle
, "_SDD", &input
, NULL
);
691 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
693 err
= ACPI_FAILURE(status
) ? -EIO
: 0;
695 if (ata_msg_probe(ap
))
696 ata_dev_printk(dev
, KERN_DEBUG
,
697 "%s _SDD error: status = 0x%x\n",
698 __FUNCTION__
, status
);
701 /* always return success */