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 */
39 * sata_get_dev_handle - finds acpi_handle and PCI device.function
40 * @dev: device to locate
41 * @handle: returned acpi_handle for @dev
42 * @pcidevfn: return PCI device.func for @dev
44 * This function is somewhat SATA-specific. Or at least the
45 * PATA & SATA versions of this function are different,
46 * so it's not entirely generic code.
48 * Returns 0 on success, <0 on error.
50 static int sata_get_dev_handle(struct device
*dev
, acpi_handle
*handle
,
51 acpi_integer
*pcidevfn
)
53 struct pci_dev
*pci_dev
;
56 pci_dev
= to_pci_dev(dev
); /* NOTE: PCI-specific */
57 /* Please refer to the ACPI spec for the syntax of _ADR. */
58 addr
= (PCI_SLOT(pci_dev
->devfn
) << 16) | PCI_FUNC(pci_dev
->devfn
);
60 *handle
= acpi_get_child(DEVICE_ACPI_HANDLE(dev
->parent
), addr
);
67 * pata_get_dev_handle - finds acpi_handle and PCI device.function
68 * @dev: device to locate
69 * @handle: returned acpi_handle for @dev
70 * @pcidevfn: return PCI device.func for @dev
72 * The PATA and SATA versions of this function are different.
74 * Returns 0 on success, <0 on error.
76 static int pata_get_dev_handle(struct device
*dev
, acpi_handle
*handle
,
77 acpi_integer
*pcidevfn
)
79 unsigned int bus
, devnum
, func
;
81 acpi_handle dev_handle
, parent_handle
;
82 struct acpi_buffer buffer
= {.length
= ACPI_ALLOCATE_BUFFER
,
85 struct acpi_device_info
*dinfo
= NULL
;
87 struct pci_dev
*pdev
= to_pci_dev(dev
);
89 bus
= pdev
->bus
->number
;
90 devnum
= PCI_SLOT(pdev
->devfn
);
91 func
= PCI_FUNC(pdev
->devfn
);
93 dev_handle
= DEVICE_ACPI_HANDLE(dev
);
94 parent_handle
= DEVICE_ACPI_HANDLE(dev
->parent
);
96 status
= acpi_get_object_info(parent_handle
, &buffer
);
97 if (ACPI_FAILURE(status
))
100 dinfo
= buffer
.pointer
;
101 if (dinfo
&& (dinfo
->valid
& ACPI_VALID_ADR
) &&
102 dinfo
->address
== bus
) {
103 /* ACPI spec for _ADR for PCI bus: */
104 addr
= (acpi_integer
)(devnum
<< 16 | func
);
106 *handle
= dev_handle
;
119 struct walk_info
{ /* can be trimmed some */
121 struct acpi_device
*adev
;
123 acpi_integer pcidevfn
;
124 unsigned int drivenum
;
125 acpi_handle obj_handle
;
126 struct ata_port
*ataport
;
127 struct ata_device
*atadev
;
130 char basepath
[ACPI_PATHNAME_MAX
];
134 static acpi_status
get_devices(acpi_handle handle
,
135 u32 level
, void *context
, void **return_value
)
138 struct walk_info
*winfo
= context
;
139 struct acpi_buffer namebuf
= {ACPI_ALLOCATE_BUFFER
, NULL
};
141 struct acpi_buffer buffer
;
142 struct acpi_device_info
*dinfo
;
144 status
= acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &namebuf
);
147 pathname
= namebuf
.pointer
;
149 buffer
.length
= ACPI_ALLOCATE_BUFFER
;
150 buffer
.pointer
= NULL
;
151 status
= acpi_get_object_info(handle
, &buffer
);
152 if (ACPI_FAILURE(status
))
155 dinfo
= buffer
.pointer
;
157 /* find full device path name for pcidevfn */
158 if (dinfo
&& (dinfo
->valid
& ACPI_VALID_ADR
) &&
159 dinfo
->address
== winfo
->pcidevfn
) {
160 if (ata_msg_probe(winfo
->ataport
))
161 ata_dev_printk(winfo
->atadev
, KERN_DEBUG
,
162 ":%s: matches pcidevfn (0x%llx)\n",
163 pathname
, winfo
->pcidevfn
);
164 strlcpy(winfo
->basepath
, pathname
,
165 sizeof(winfo
->basepath
));
166 winfo
->basepath_len
= strlen(pathname
);
170 /* if basepath is not yet known, ignore this object */
171 if (!winfo
->basepath_len
)
174 /* if this object is in scope of basepath, maybe use it */
175 if (strncmp(pathname
, winfo
->basepath
,
176 winfo
->basepath_len
) == 0) {
177 if (!(dinfo
->valid
& ACPI_VALID_ADR
))
179 if (ata_msg_probe(winfo
->ataport
))
180 ata_dev_printk(winfo
->atadev
, KERN_DEBUG
,
181 "GOT ONE: (%s) root_port = 0x%llx,"
182 " port_num = 0x%llx\n", pathname
,
183 SATA_ROOT_PORT(dinfo
->address
),
184 SATA_PORT_NUMBER(dinfo
->address
));
186 if (SATA_PORT_NUMBER(dinfo
->address
) != NO_PORT_MULT
)
187 if (ata_msg_probe(winfo
->ataport
))
188 ata_dev_printk(winfo
->atadev
,
189 KERN_DEBUG
, "warning: don't"
190 " know how to handle SATA port"
192 if (SATA_ROOT_PORT(dinfo
->address
) ==
193 winfo
->ataport
->port_no
&&
194 SATA_PORT_NUMBER(dinfo
->address
) == NO_PORT_MULT
) {
195 if (ata_msg_probe(winfo
->ataport
))
196 ata_dev_printk(winfo
->atadev
,
198 "THIS ^^^^^ is the requested"
199 " SATA drive (handle = 0x%p)\n",
201 winfo
->sata_adr
= dinfo
->address
;
202 winfo
->obj_handle
= handle
;
214 /* Get the SATA drive _ADR object. */
215 static int get_sata_adr(struct device
*dev
, acpi_handle handle
,
216 acpi_integer pcidevfn
, unsigned int drive
,
218 struct ata_device
*atadev
, u32
*dev_adr
)
221 struct walk_info
*winfo
;
224 winfo
= kzalloc(sizeof(struct walk_info
), GFP_KERNEL
);
229 winfo
->atadev
= atadev
;
231 if (acpi_bus_get_device(handle
, &winfo
->adev
) < 0)
232 if (ata_msg_probe(ap
))
233 ata_dev_printk(winfo
->atadev
, KERN_DEBUG
,
234 "acpi_bus_get_device failed\n");
235 winfo
->handle
= handle
;
236 winfo
->pcidevfn
= pcidevfn
;
237 winfo
->drivenum
= drive
;
239 status
= acpi_get_devices(NULL
, get_devices
, winfo
, NULL
);
240 if (ACPI_FAILURE(status
)) {
241 if (ata_msg_probe(ap
))
242 ata_dev_printk(winfo
->atadev
, KERN_DEBUG
,
243 "%s: acpi_get_devices failed\n",
247 *dev_adr
= winfo
->sata_adr
;
248 atadev
->obj_handle
= winfo
->obj_handle
;
257 * do_drive_get_GTF - get the drive bootup default taskfile settings
258 * @ap: the ata_port for the drive
259 * @ix: target ata_device (drive) index
260 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
261 * @gtf_address: buffer containing _GTF taskfile arrays
263 * This applies to both PATA and SATA drives.
265 * The _GTF method has no input parameters.
266 * It returns a variable number of register set values (registers
267 * hex 1F1..1F7, taskfiles).
268 * The <variable number> is not known in advance, so have ACPI-CA
269 * allocate the buffer as needed and return it, then free it later.
271 * The returned @gtf_length and @gtf_address are only valid if the
272 * function return value is 0.
274 static int do_drive_get_GTF(struct ata_port
*ap
, int ix
,
275 unsigned int *gtf_length
, unsigned long *gtf_address
,
276 unsigned long *obj_loc
)
279 acpi_handle dev_handle
= NULL
;
280 acpi_handle chan_handle
, drive_handle
;
281 acpi_integer pcidevfn
= 0;
283 struct acpi_buffer output
;
284 union acpi_object
*out_obj
;
285 struct device
*dev
= ap
->host
->dev
;
286 struct ata_device
*atadev
= &ap
->device
[ix
];
296 if (ata_msg_probe(ap
))
297 ata_dev_printk(atadev
, KERN_DEBUG
,
298 "%s: ENTER: ap->id: %d, port#: %d\n",
299 __FUNCTION__
, ap
->id
, ap
->port_no
);
301 if (!ata_dev_enabled(atadev
) || (ap
->flags
& ATA_FLAG_DISABLED
)) {
302 if (ata_msg_probe(ap
))
303 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: ERR: "
304 "ata_dev_present: %d, PORT_DISABLED: %lu\n",
305 __FUNCTION__
, ata_dev_enabled(atadev
),
306 ap
->flags
& ATA_FLAG_DISABLED
);
310 /* Don't continue if device has no _ADR method.
311 * _GTF is intended for known motherboard devices. */
312 if (!(ap
->cbl
== ATA_CBL_SATA
)) {
313 err
= pata_get_dev_handle(dev
, &dev_handle
, &pcidevfn
);
315 if (ata_msg_probe(ap
))
316 ata_dev_printk(atadev
, KERN_DEBUG
,
317 "%s: pata_get_dev_handle failed (%d)\n",
322 err
= sata_get_dev_handle(dev
, &dev_handle
, &pcidevfn
);
324 if (ata_msg_probe(ap
))
325 ata_dev_printk(atadev
, KERN_DEBUG
,
326 "%s: sata_get_dev_handle failed (%d\n",
332 /* Get this drive's _ADR info. if not already known. */
333 if (!atadev
->obj_handle
) {
334 if (!(ap
->cbl
== ATA_CBL_SATA
)) {
335 /* get child objects of dev_handle == channel objects,
336 * + _their_ children == drive objects */
337 /* channel is ap->port_no */
338 chan_handle
= acpi_get_child(dev_handle
,
340 if (ata_msg_probe(ap
))
341 ata_dev_printk(atadev
, KERN_DEBUG
,
342 "%s: chan adr=%d: chan_handle=0x%p\n",
343 __FUNCTION__
, ap
->port_no
,
349 /* TBD: could also check ACPI object VALID bits */
350 drive_handle
= acpi_get_child(chan_handle
, ix
);
356 atadev
->obj_handle
= drive_handle
;
357 } else { /* for SATA mode */
358 dev_adr
= SATA_ADR_RSVD
;
359 err
= get_sata_adr(dev
, dev_handle
, pcidevfn
, 0,
360 ap
, atadev
, &dev_adr
);
362 if (err
< 0 || dev_adr
== SATA_ADR_RSVD
||
363 !atadev
->obj_handle
) {
364 if (ata_msg_probe(ap
))
365 ata_dev_printk(atadev
, KERN_DEBUG
,
366 "%s: get_sata/pata_adr failed: "
367 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
368 __FUNCTION__
, err
, dev_adr
,
374 /* Setting up output buffer */
375 output
.length
= ACPI_ALLOCATE_BUFFER
;
376 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
378 /* _GTF has no input parameters */
380 status
= acpi_evaluate_object(atadev
->obj_handle
, "_GTF",
382 if (ACPI_FAILURE(status
)) {
383 if (ata_msg_probe(ap
))
384 ata_dev_printk(atadev
, KERN_DEBUG
,
385 "%s: Run _GTF error: status = 0x%x\n",
386 __FUNCTION__
, status
);
390 if (!output
.length
|| !output
.pointer
) {
391 if (ata_msg_probe(ap
))
392 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: Run _GTF: "
393 "length or ptr is NULL (0x%llx, 0x%p)\n",
395 (unsigned long long)output
.length
,
397 kfree(output
.pointer
);
401 out_obj
= output
.pointer
;
402 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
403 kfree(output
.pointer
);
404 if (ata_msg_probe(ap
))
405 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: Run _GTF: "
406 "error: expected object type of "
407 " ACPI_TYPE_BUFFER, got 0x%x\n",
408 __FUNCTION__
, out_obj
->type
);
413 if (!out_obj
->buffer
.length
|| !out_obj
->buffer
.pointer
||
414 out_obj
->buffer
.length
% REGS_PER_GTF
) {
416 ata_dev_printk(atadev
, KERN_ERR
,
417 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
418 __FUNCTION__
, out_obj
->buffer
.length
,
419 out_obj
->buffer
.pointer
);
424 *gtf_length
= out_obj
->buffer
.length
;
425 *gtf_address
= (unsigned long)out_obj
->buffer
.pointer
;
426 *obj_loc
= (unsigned long)out_obj
;
427 if (ata_msg_probe(ap
))
428 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: returning "
429 "gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
430 __FUNCTION__
, *gtf_length
, *gtf_address
, *obj_loc
);
437 * taskfile_load_raw - send taskfile registers to host controller
438 * @ap: Port to which output is sent
439 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
441 * Outputs ATA taskfile to standard ATA host controller using MMIO
442 * or PIO as indicated by the ATA_FLAG_MMIO flag.
443 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
444 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
445 * hob_lbal, hob_lbam, and hob_lbah.
447 * This function waits for idle (!BUSY and !DRQ) after writing
448 * registers. If the control register has a new value, this
449 * function also waits for idle after writing control and before
450 * writing the remaining registers.
453 * Inherited from caller.
455 static void taskfile_load_raw(struct ata_port
*ap
,
456 struct ata_device
*atadev
,
457 const struct taskfile_array
*gtf
)
459 if (ata_msg_probe(ap
))
460 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: (0x1f1-1f7): hex: "
461 "%02x %02x %02x %02x %02x %02x %02x\n",
463 gtf
->tfa
[0], gtf
->tfa
[1], gtf
->tfa
[2],
464 gtf
->tfa
[3], gtf
->tfa
[4], gtf
->tfa
[5], gtf
->tfa
[6]);
466 if ((gtf
->tfa
[0] == 0) && (gtf
->tfa
[1] == 0) && (gtf
->tfa
[2] == 0)
467 && (gtf
->tfa
[3] == 0) && (gtf
->tfa
[4] == 0) && (gtf
->tfa
[5] == 0)
468 && (gtf
->tfa
[6] == 0))
471 if (ap
->ops
->qc_issue
) {
472 struct ata_taskfile tf
;
475 ata_tf_init(atadev
, &tf
);
477 /* convert gtf to tf */
478 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
; /* TBD */
479 tf
.protocol
= atadev
->class == ATA_DEV_ATAPI
?
480 ATA_PROT_ATAPI_NODATA
: ATA_PROT_NODATA
;
481 tf
.feature
= gtf
->tfa
[0]; /* 0x1f1 */
482 tf
.nsect
= gtf
->tfa
[1]; /* 0x1f2 */
483 tf
.lbal
= gtf
->tfa
[2]; /* 0x1f3 */
484 tf
.lbam
= gtf
->tfa
[3]; /* 0x1f4 */
485 tf
.lbah
= gtf
->tfa
[4]; /* 0x1f5 */
486 tf
.device
= gtf
->tfa
[5]; /* 0x1f6 */
487 tf
.command
= gtf
->tfa
[6]; /* 0x1f7 */
489 err
= ata_exec_internal(atadev
, &tf
, NULL
, DMA_NONE
, NULL
, 0);
490 if (err
&& ata_msg_probe(ap
))
491 ata_dev_printk(atadev
, KERN_ERR
,
492 "%s: ata_exec_internal failed: %u\n",
495 if (ata_msg_warn(ap
))
496 ata_dev_printk(atadev
, KERN_WARNING
,
497 "%s: SATA driver is missing qc_issue function"
503 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
504 * @ap: the ata_port for the drive
505 * @atadev: target ata_device
506 * @gtf_length: total number of bytes of _GTF taskfiles
507 * @gtf_address: location of _GTF taskfile arrays
509 * This applies to both PATA and SATA drives.
511 * Write {gtf_address, length gtf_length} in groups of
512 * REGS_PER_GTF bytes.
514 static int do_drive_set_taskfiles(struct ata_port
*ap
,
515 struct ata_device
*atadev
, unsigned int gtf_length
,
516 unsigned long gtf_address
)
519 int gtf_count
= gtf_length
/ REGS_PER_GTF
;
521 struct taskfile_array
*gtf
;
523 if (ata_msg_probe(ap
))
524 ata_dev_printk(atadev
, KERN_DEBUG
,
525 "%s: ENTER: ap->id: %d, port#: %d\n",
526 __FUNCTION__
, ap
->id
, ap
->port_no
);
528 if (noacpi
|| !(ap
->cbl
== ATA_CBL_SATA
))
531 if (!ata_dev_enabled(atadev
) || (ap
->flags
& ATA_FLAG_DISABLED
))
533 if (!gtf_count
) /* shouldn't be here */
536 if (gtf_length
% REGS_PER_GTF
) {
538 ata_dev_printk(atadev
, KERN_ERR
,
539 "%s: unexpected GTF length (%d)\n",
540 __FUNCTION__
, gtf_length
);
544 for (ix
= 0; ix
< gtf_count
; ix
++) {
545 gtf
= (struct taskfile_array
*)
546 (gtf_address
+ ix
* REGS_PER_GTF
);
548 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
549 taskfile_load_raw(ap
, atadev
, gtf
);
558 * ata_acpi_exec_tfs - get then write drive taskfile settings
559 * @ap: the ata_port for the drive
561 * This applies to both PATA and SATA drives.
563 int ata_acpi_exec_tfs(struct ata_port
*ap
)
567 unsigned int gtf_length
;
568 unsigned long gtf_address
;
569 unsigned long obj_loc
;
574 for (ix
= 0; ix
< ATA_MAX_DEVICES
; ix
++) {
575 if (!ata_dev_enabled(&ap
->device
[ix
]))
578 ret
= do_drive_get_GTF(ap
, ix
,
579 >f_length
, >f_address
, &obj_loc
);
581 if (ata_msg_probe(ap
))
582 ata_port_printk(ap
, KERN_DEBUG
,
583 "%s: get_GTF error (%d)\n",
588 ret
= do_drive_set_taskfiles(ap
, &ap
->device
[ix
],
589 gtf_length
, gtf_address
);
590 kfree((void *)obj_loc
);
592 if (ata_msg_probe(ap
))
593 ata_port_printk(ap
, KERN_DEBUG
,
594 "%s: set_taskfiles error (%d)\n",
604 * ata_acpi_push_id - send Identify data to drive
605 * @ap: the ata_port for the drive
608 * _SDD ACPI object: for SATA mode only
609 * Must be after Identify (Packet) Device -- uses its data
610 * ATM this function never returns a failure. It is an optional
611 * method and if it fails for whatever reason, we should still
614 int ata_acpi_push_id(struct ata_port
*ap
, unsigned int ix
)
617 acpi_integer pcidevfn
;
619 struct device
*dev
= ap
->host
->dev
;
620 struct ata_device
*atadev
= &ap
->device
[ix
];
623 struct acpi_object_list input
;
624 union acpi_object in_params
[1];
629 if (ata_msg_probe(ap
))
630 ata_dev_printk(atadev
, KERN_DEBUG
,
631 "%s: ap->id: %d, ix = %d, port#: %d\n",
632 __FUNCTION__
, ap
->id
, ix
, ap
->port_no
);
634 /* Don't continue if not a SATA device. */
635 if (!(ap
->cbl
== ATA_CBL_SATA
)) {
636 if (ata_msg_probe(ap
))
637 ata_dev_printk(atadev
, KERN_DEBUG
,
638 "%s: Not a SATA device\n", __FUNCTION__
);
642 /* Don't continue if device has no _ADR method.
643 * _SDD is intended for known motherboard devices. */
644 err
= sata_get_dev_handle(dev
, &handle
, &pcidevfn
);
646 if (ata_msg_probe(ap
))
647 ata_dev_printk(atadev
, KERN_DEBUG
,
648 "%s: sata_get_dev_handle failed (%d\n",
653 /* Get this drive's _ADR info, if not already known */
654 if (!atadev
->obj_handle
) {
655 dev_adr
= SATA_ADR_RSVD
;
656 err
= get_sata_adr(dev
, handle
, pcidevfn
, ix
, ap
, atadev
,
658 if (err
< 0 || dev_adr
== SATA_ADR_RSVD
||
659 !atadev
->obj_handle
) {
660 if (ata_msg_probe(ap
))
661 ata_dev_printk(atadev
, KERN_DEBUG
,
662 "%s: get_sata_adr failed: "
663 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
664 __FUNCTION__
, err
, dev_adr
,
670 /* Give the drive Identify data to the drive via the _SDD method */
671 /* _SDD: set up input parameters */
673 input
.pointer
= in_params
;
674 in_params
[0].type
= ACPI_TYPE_BUFFER
;
675 in_params
[0].buffer
.length
= sizeof(atadev
->id
[0] * ATA_ID_WORDS
);
676 in_params
[0].buffer
.pointer
= (u8
*)atadev
->id
;
677 /* Output buffer: _SDD has no output */
679 /* It's OK for _SDD to be missing too. */
680 swap_buf_le16(atadev
->id
, ATA_ID_WORDS
);
681 status
= acpi_evaluate_object(atadev
->obj_handle
, "_SDD", &input
, NULL
);
682 swap_buf_le16(atadev
->id
, ATA_ID_WORDS
);
684 err
= ACPI_FAILURE(status
) ? -EIO
: 0;
686 if (ata_msg_probe(ap
))
687 ata_dev_printk(atadev
, KERN_DEBUG
,
688 "ata%u(%u): %s _SDD error: status = 0x%x\n",
689 ap
->id
, ap
->device
->devno
,
690 __FUNCTION__
, status
);
693 /* always return success */