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 * @ap: the ata_port for the drive
274 * @ix: target ata_device (drive) index
275 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
276 * @gtf_address: buffer containing _GTF taskfile arrays
278 * This applies to both PATA and SATA drives.
280 * The _GTF method has no input parameters.
281 * It returns a variable number of register set values (registers
282 * hex 1F1..1F7, taskfiles).
283 * The <variable number> is not known in advance, so have ACPI-CA
284 * allocate the buffer as needed and return it, then free it later.
286 * The returned @gtf_length and @gtf_address are only valid if the
287 * function return value is 0.
289 static int do_drive_get_GTF(struct ata_port
*ap
, int ix
,
290 unsigned int *gtf_length
, unsigned long *gtf_address
,
291 unsigned long *obj_loc
)
294 acpi_handle dev_handle
= NULL
;
295 acpi_handle chan_handle
, drive_handle
;
296 acpi_integer pcidevfn
= 0;
298 struct acpi_buffer output
;
299 union acpi_object
*out_obj
;
300 struct device
*dev
= ap
->host
->dev
;
301 struct ata_device
*atadev
= &ap
->device
[ix
];
311 if (ata_msg_probe(ap
))
312 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: ENTER: port#: %d\n",
313 __FUNCTION__
, ap
->port_no
);
315 if (!ata_dev_enabled(atadev
) || (ap
->flags
& ATA_FLAG_DISABLED
)) {
316 if (ata_msg_probe(ap
))
317 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: ERR: "
318 "ata_dev_present: %d, PORT_DISABLED: %lu\n",
319 __FUNCTION__
, ata_dev_enabled(atadev
),
320 ap
->flags
& ATA_FLAG_DISABLED
);
324 /* Don't continue if device has no _ADR method.
325 * _GTF is intended for known motherboard devices. */
326 if (!(ap
->cbl
== ATA_CBL_SATA
)) {
327 err
= pata_get_dev_handle(dev
, &dev_handle
, &pcidevfn
);
329 if (ata_msg_probe(ap
))
330 ata_dev_printk(atadev
, KERN_DEBUG
,
331 "%s: pata_get_dev_handle failed (%d)\n",
336 err
= sata_get_dev_handle(dev
, &dev_handle
, &pcidevfn
);
338 if (ata_msg_probe(ap
))
339 ata_dev_printk(atadev
, KERN_DEBUG
,
340 "%s: sata_get_dev_handle failed (%d\n",
346 /* Get this drive's _ADR info. if not already known. */
347 if (!atadev
->obj_handle
) {
348 if (!(ap
->cbl
== ATA_CBL_SATA
)) {
349 /* get child objects of dev_handle == channel objects,
350 * + _their_ children == drive objects */
351 /* channel is ap->port_no */
352 chan_handle
= acpi_get_child(dev_handle
,
354 if (ata_msg_probe(ap
))
355 ata_dev_printk(atadev
, KERN_DEBUG
,
356 "%s: chan adr=%d: chan_handle=0x%p\n",
357 __FUNCTION__
, ap
->port_no
,
363 /* TBD: could also check ACPI object VALID bits */
364 drive_handle
= acpi_get_child(chan_handle
, ix
);
370 atadev
->obj_handle
= drive_handle
;
371 } else { /* for SATA mode */
372 dev_adr
= SATA_ADR_RSVD
;
373 err
= get_sata_adr(dev
, dev_handle
, pcidevfn
, 0,
374 ap
, atadev
, &dev_adr
);
376 if (err
< 0 || dev_adr
== SATA_ADR_RSVD
||
377 !atadev
->obj_handle
) {
378 if (ata_msg_probe(ap
))
379 ata_dev_printk(atadev
, KERN_DEBUG
,
380 "%s: get_sata/pata_adr failed: "
381 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
382 __FUNCTION__
, err
, dev_adr
,
388 /* Setting up output buffer */
389 output
.length
= ACPI_ALLOCATE_BUFFER
;
390 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
392 /* _GTF has no input parameters */
394 status
= acpi_evaluate_object(atadev
->obj_handle
, "_GTF",
396 if (ACPI_FAILURE(status
)) {
397 if (ata_msg_probe(ap
))
398 ata_dev_printk(atadev
, KERN_DEBUG
,
399 "%s: Run _GTF error: status = 0x%x\n",
400 __FUNCTION__
, status
);
404 if (!output
.length
|| !output
.pointer
) {
405 if (ata_msg_probe(ap
))
406 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: Run _GTF: "
407 "length or ptr is NULL (0x%llx, 0x%p)\n",
409 (unsigned long long)output
.length
,
411 kfree(output
.pointer
);
415 out_obj
= output
.pointer
;
416 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
417 kfree(output
.pointer
);
418 if (ata_msg_probe(ap
))
419 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: Run _GTF: "
420 "error: expected object type of "
421 " ACPI_TYPE_BUFFER, got 0x%x\n",
422 __FUNCTION__
, out_obj
->type
);
427 if (!out_obj
->buffer
.length
|| !out_obj
->buffer
.pointer
||
428 out_obj
->buffer
.length
% REGS_PER_GTF
) {
430 ata_dev_printk(atadev
, KERN_ERR
,
431 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
432 __FUNCTION__
, out_obj
->buffer
.length
,
433 out_obj
->buffer
.pointer
);
438 *gtf_length
= out_obj
->buffer
.length
;
439 *gtf_address
= (unsigned long)out_obj
->buffer
.pointer
;
440 *obj_loc
= (unsigned long)out_obj
;
441 if (ata_msg_probe(ap
))
442 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: returning "
443 "gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
444 __FUNCTION__
, *gtf_length
, *gtf_address
, *obj_loc
);
451 * taskfile_load_raw - send taskfile registers to host controller
452 * @ap: Port to which output is sent
453 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
455 * Outputs ATA taskfile to standard ATA host controller using MMIO
456 * or PIO as indicated by the ATA_FLAG_MMIO flag.
457 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
458 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
459 * hob_lbal, hob_lbam, and hob_lbah.
461 * This function waits for idle (!BUSY and !DRQ) after writing
462 * registers. If the control register has a new value, this
463 * function also waits for idle after writing control and before
464 * writing the remaining registers.
467 * Inherited from caller.
469 static void taskfile_load_raw(struct ata_port
*ap
,
470 struct ata_device
*atadev
,
471 const struct taskfile_array
*gtf
)
473 struct ata_taskfile tf
;
476 if (ata_msg_probe(ap
))
477 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: (0x1f1-1f7): hex: "
478 "%02x %02x %02x %02x %02x %02x %02x\n",
480 gtf
->tfa
[0], gtf
->tfa
[1], gtf
->tfa
[2],
481 gtf
->tfa
[3], gtf
->tfa
[4], gtf
->tfa
[5], gtf
->tfa
[6]);
483 if ((gtf
->tfa
[0] == 0) && (gtf
->tfa
[1] == 0) && (gtf
->tfa
[2] == 0)
484 && (gtf
->tfa
[3] == 0) && (gtf
->tfa
[4] == 0) && (gtf
->tfa
[5] == 0)
485 && (gtf
->tfa
[6] == 0))
488 ata_tf_init(atadev
, &tf
);
490 /* convert gtf to tf */
491 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
; /* TBD */
492 tf
.protocol
= ATA_PROT_NODATA
;
493 tf
.feature
= gtf
->tfa
[0]; /* 0x1f1 */
494 tf
.nsect
= gtf
->tfa
[1]; /* 0x1f2 */
495 tf
.lbal
= gtf
->tfa
[2]; /* 0x1f3 */
496 tf
.lbam
= gtf
->tfa
[3]; /* 0x1f4 */
497 tf
.lbah
= gtf
->tfa
[4]; /* 0x1f5 */
498 tf
.device
= gtf
->tfa
[5]; /* 0x1f6 */
499 tf
.command
= gtf
->tfa
[6]; /* 0x1f7 */
501 err
= ata_exec_internal(atadev
, &tf
, NULL
, DMA_NONE
, NULL
, 0);
502 if (err
&& ata_msg_probe(ap
))
503 ata_dev_printk(atadev
, KERN_ERR
,
504 "%s: ata_exec_internal failed: %u\n",
509 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
510 * @ap: the ata_port for the drive
511 * @atadev: target ata_device
512 * @gtf_length: total number of bytes of _GTF taskfiles
513 * @gtf_address: location of _GTF taskfile arrays
515 * This applies to both PATA and SATA drives.
517 * Write {gtf_address, length gtf_length} in groups of
518 * REGS_PER_GTF bytes.
520 static int do_drive_set_taskfiles(struct ata_port
*ap
,
521 struct ata_device
*atadev
, unsigned int gtf_length
,
522 unsigned long gtf_address
)
525 int gtf_count
= gtf_length
/ REGS_PER_GTF
;
527 struct taskfile_array
*gtf
;
529 if (ata_msg_probe(ap
))
530 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: ENTER: port#: %d\n",
531 __FUNCTION__
, ap
->port_no
);
533 if (libata_noacpi
|| !(ap
->cbl
== ATA_CBL_SATA
))
536 if (!ata_dev_enabled(atadev
) || (ap
->flags
& ATA_FLAG_DISABLED
))
538 if (!gtf_count
) /* shouldn't be here */
541 if (gtf_length
% REGS_PER_GTF
) {
543 ata_dev_printk(atadev
, KERN_ERR
,
544 "%s: unexpected GTF length (%d)\n",
545 __FUNCTION__
, gtf_length
);
549 for (ix
= 0; ix
< gtf_count
; ix
++) {
550 gtf
= (struct taskfile_array
*)
551 (gtf_address
+ ix
* REGS_PER_GTF
);
553 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
554 taskfile_load_raw(ap
, atadev
, gtf
);
563 * ata_acpi_exec_tfs - get then write drive taskfile settings
564 * @ap: the ata_port for the drive
566 * This applies to both PATA and SATA drives.
568 int ata_acpi_exec_tfs(struct ata_port
*ap
)
572 unsigned int gtf_length
;
573 unsigned long gtf_address
;
574 unsigned long obj_loc
;
579 * TBD - implement PATA support. For now,
580 * we should not run GTF on PATA devices since some
581 * PATA require execution of GTM/STM before GTF.
583 if (!(ap
->cbl
== ATA_CBL_SATA
))
586 for (ix
= 0; ix
< ATA_MAX_DEVICES
; ix
++) {
587 if (!ata_dev_enabled(&ap
->device
[ix
]))
590 ret
= do_drive_get_GTF(ap
, ix
,
591 >f_length
, >f_address
, &obj_loc
);
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(ap
, &ap
->device
[ix
],
601 gtf_length
, gtf_address
);
602 kfree((void *)obj_loc
);
604 if (ata_msg_probe(ap
))
605 ata_port_printk(ap
, KERN_DEBUG
,
606 "%s: set_taskfiles error (%d)\n",
616 * ata_acpi_push_id - send Identify data to drive
617 * @ap: the ata_port for the drive
620 * _SDD ACPI object: for SATA mode only
621 * Must be after Identify (Packet) Device -- uses its data
622 * ATM this function never returns a failure. It is an optional
623 * method and if it fails for whatever reason, we should still
626 int ata_acpi_push_id(struct ata_port
*ap
, unsigned int ix
)
629 acpi_integer pcidevfn
;
631 struct device
*dev
= ap
->host
->dev
;
632 struct ata_device
*atadev
= &ap
->device
[ix
];
635 struct acpi_object_list input
;
636 union acpi_object in_params
[1];
641 if (ata_msg_probe(ap
))
642 ata_dev_printk(atadev
, KERN_DEBUG
, "%s: ix = %d, port#: %d\n",
643 __FUNCTION__
, ix
, ap
->port_no
);
645 /* Don't continue if not a SATA device. */
646 if (!(ap
->cbl
== ATA_CBL_SATA
)) {
647 if (ata_msg_probe(ap
))
648 ata_dev_printk(atadev
, KERN_DEBUG
,
649 "%s: Not a SATA device\n", __FUNCTION__
);
653 /* Don't continue if device has no _ADR method.
654 * _SDD is intended for known motherboard devices. */
655 err
= sata_get_dev_handle(dev
, &handle
, &pcidevfn
);
657 if (ata_msg_probe(ap
))
658 ata_dev_printk(atadev
, KERN_DEBUG
,
659 "%s: sata_get_dev_handle failed (%d\n",
664 /* Get this drive's _ADR info, if not already known */
665 if (!atadev
->obj_handle
) {
666 dev_adr
= SATA_ADR_RSVD
;
667 err
= get_sata_adr(dev
, handle
, pcidevfn
, ix
, ap
, atadev
,
669 if (err
< 0 || dev_adr
== SATA_ADR_RSVD
||
670 !atadev
->obj_handle
) {
671 if (ata_msg_probe(ap
))
672 ata_dev_printk(atadev
, KERN_DEBUG
,
673 "%s: get_sata_adr failed: "
674 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
675 __FUNCTION__
, err
, dev_adr
,
681 /* Give the drive Identify data to the drive via the _SDD method */
682 /* _SDD: set up input parameters */
684 input
.pointer
= in_params
;
685 in_params
[0].type
= ACPI_TYPE_BUFFER
;
686 in_params
[0].buffer
.length
= sizeof(atadev
->id
[0]) * ATA_ID_WORDS
;
687 in_params
[0].buffer
.pointer
= (u8
*)atadev
->id
;
688 /* Output buffer: _SDD has no output */
690 /* It's OK for _SDD to be missing too. */
691 swap_buf_le16(atadev
->id
, ATA_ID_WORDS
);
692 status
= acpi_evaluate_object(atadev
->obj_handle
, "_SDD", &input
, NULL
);
693 swap_buf_le16(atadev
->id
, ATA_ID_WORDS
);
695 err
= ACPI_FAILURE(status
) ? -EIO
: 0;
697 if (ata_msg_probe(ap
))
698 ata_dev_printk(atadev
, KERN_DEBUG
,
699 "%s _SDD error: status = 0x%x\n",
700 __FUNCTION__
, status
);
703 /* always return success */