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 NO_PORT_MULT 0xffff
28 #define SATA_ADR(root,pmp) (((root) << 16) | (pmp))
30 #define REGS_PER_GTF 7
32 u8 tf
[REGS_PER_GTF
]; /* regs. 0x1f1 - 0x1f7 */
36 * Helper - belongs in the PCI layer somewhere eventually
38 static int is_pci_dev(struct device
*dev
)
40 return (dev
->bus
== &pci_bus_type
);
43 static void ata_acpi_associate_sata_port(struct ata_port
*ap
)
45 acpi_integer adr
= SATA_ADR(ap
->port_no
, NO_PORT_MULT
);
47 ap
->device
->acpi_handle
= acpi_get_child(ap
->host
->acpi_handle
, adr
);
50 static void ata_acpi_associate_ide_port(struct ata_port
*ap
)
54 ap
->acpi_handle
= acpi_get_child(ap
->host
->acpi_handle
, ap
->port_no
);
59 if (ap
->flags
& ATA_FLAG_SLAVE_POSS
)
62 for (i
= 0; i
< max_devices
; i
++) {
63 struct ata_device
*dev
= &ap
->device
[i
];
65 dev
->acpi_handle
= acpi_get_child(ap
->acpi_handle
, i
);
70 * ata_acpi_associate - associate ATA host with ACPI objects
71 * @host: target ATA host
73 * Look up ACPI objects associated with @host and initialize
74 * acpi_handle fields of @host, its ports and devices accordingly.
80 * 0 on success, -errno on failure.
82 void ata_acpi_associate(struct ata_host
*host
)
86 if (!is_pci_dev(host
->dev
) || libata_noacpi
)
89 host
->acpi_handle
= DEVICE_ACPI_HANDLE(host
->dev
);
90 if (!host
->acpi_handle
)
93 for (i
= 0; i
< host
->n_ports
; i
++) {
94 struct ata_port
*ap
= host
->ports
[i
];
96 if (host
->ports
[0]->flags
& ATA_FLAG_ACPI_SATA
)
97 ata_acpi_associate_sata_port(ap
);
99 ata_acpi_associate_ide_port(ap
);
104 * ata_acpi_gtm - execute _GTM
105 * @ap: target ATA port
106 * @gtm: out parameter for _GTM result
108 * Evaluate _GTM and store the result in @gtm.
114 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
116 static int ata_acpi_gtm(const struct ata_port
*ap
, struct ata_acpi_gtm
*gtm
)
118 struct acpi_buffer output
= { .length
= ACPI_ALLOCATE_BUFFER
};
119 union acpi_object
*out_obj
;
123 status
= acpi_evaluate_object(ap
->acpi_handle
, "_GTM", NULL
, &output
);
126 if (status
== AE_NOT_FOUND
)
130 if (ACPI_FAILURE(status
)) {
131 ata_port_printk(ap
, KERN_ERR
,
132 "ACPI get timing mode failed (AE 0x%x)\n",
137 out_obj
= output
.pointer
;
138 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
139 ata_port_printk(ap
, KERN_WARNING
,
140 "_GTM returned unexpected object type 0x%x\n",
146 if (out_obj
->buffer
.length
!= sizeof(struct ata_acpi_gtm
)) {
147 ata_port_printk(ap
, KERN_ERR
,
148 "_GTM returned invalid length %d\n",
149 out_obj
->buffer
.length
);
153 memcpy(gtm
, out_obj
->buffer
.pointer
, sizeof(struct ata_acpi_gtm
));
156 kfree(output
.pointer
);
161 * ata_acpi_stm - execute _STM
162 * @ap: target ATA port
163 * @stm: timing parameter to _STM
165 * Evaluate _STM with timing parameter @stm.
171 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
173 static int ata_acpi_stm(const struct ata_port
*ap
, struct ata_acpi_gtm
*stm
)
176 struct acpi_object_list input
;
177 union acpi_object in_params
[3];
179 in_params
[0].type
= ACPI_TYPE_BUFFER
;
180 in_params
[0].buffer
.length
= sizeof(struct ata_acpi_gtm
);
181 in_params
[0].buffer
.pointer
= (u8
*)stm
;
182 /* Buffers for id may need byteswapping ? */
183 in_params
[1].type
= ACPI_TYPE_BUFFER
;
184 in_params
[1].buffer
.length
= 512;
185 in_params
[1].buffer
.pointer
= (u8
*)ap
->device
[0].id
;
186 in_params
[2].type
= ACPI_TYPE_BUFFER
;
187 in_params
[2].buffer
.length
= 512;
188 in_params
[2].buffer
.pointer
= (u8
*)ap
->device
[1].id
;
191 input
.pointer
= in_params
;
193 status
= acpi_evaluate_object(ap
->acpi_handle
, "_STM", &input
, NULL
);
195 if (status
== AE_NOT_FOUND
)
197 if (ACPI_FAILURE(status
)) {
198 ata_port_printk(ap
, KERN_ERR
,
199 "ACPI set timing mode failed (status=0x%x)\n", status
);
206 * ata_dev_get_GTF - get the drive bootup default taskfile settings
207 * @dev: target ATA device
208 * @gtf: output parameter for buffer containing _GTF taskfile arrays
209 * @ptr_to_free: pointer which should be freed
211 * This applies to both PATA and SATA drives.
213 * The _GTF method has no input parameters.
214 * It returns a variable number of register set values (registers
215 * hex 1F1..1F7, taskfiles).
216 * The <variable number> is not known in advance, so have ACPI-CA
217 * allocate the buffer as needed and return it, then free it later.
223 * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
224 * contain valid data. -errno on other errors.
226 static int ata_dev_get_GTF(struct ata_device
*dev
, struct ata_acpi_gtf
**gtf
,
229 struct ata_port
*ap
= dev
->ap
;
231 struct acpi_buffer output
;
232 union acpi_object
*out_obj
;
235 /* set up output buffer */
236 output
.length
= ACPI_ALLOCATE_BUFFER
;
237 output
.pointer
= NULL
; /* ACPI-CA sets this; save/free it later */
239 if (ata_msg_probe(ap
))
240 ata_dev_printk(dev
, KERN_DEBUG
, "%s: ENTER: port#: %d\n",
241 __FUNCTION__
, ap
->port_no
);
243 /* _GTF has no input parameters */
244 status
= acpi_evaluate_object(dev
->acpi_handle
, "_GTF", NULL
, &output
);
246 if (ACPI_FAILURE(status
)) {
247 if (status
!= AE_NOT_FOUND
) {
248 ata_dev_printk(dev
, KERN_WARNING
,
249 "_GTF evaluation failed (AE 0x%x)\n",
256 if (!output
.length
|| !output
.pointer
) {
257 if (ata_msg_probe(ap
))
258 ata_dev_printk(dev
, KERN_DEBUG
, "%s: Run _GTF: "
259 "length or ptr is NULL (0x%llx, 0x%p)\n",
261 (unsigned long long)output
.length
,
266 out_obj
= output
.pointer
;
267 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
268 ata_dev_printk(dev
, KERN_WARNING
,
269 "_GTF unexpected object type 0x%x\n",
275 if (out_obj
->buffer
.length
% REGS_PER_GTF
) {
276 ata_dev_printk(dev
, KERN_WARNING
,
277 "unexpected _GTF length (%d)\n",
278 out_obj
->buffer
.length
);
283 *ptr_to_free
= out_obj
;
284 *gtf
= (void *)out_obj
->buffer
.pointer
;
285 rc
= out_obj
->buffer
.length
/ REGS_PER_GTF
;
287 if (ata_msg_probe(ap
))
288 ata_dev_printk(dev
, KERN_DEBUG
, "%s: returning "
289 "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
290 __FUNCTION__
, *gtf
, rc
, *ptr_to_free
);
294 kfree(output
.pointer
);
299 * taskfile_load_raw - send taskfile registers to host controller
300 * @dev: target ATA device
301 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
303 * Outputs ATA taskfile to standard ATA host controller using MMIO
304 * or PIO as indicated by the ATA_FLAG_MMIO flag.
305 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
306 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
307 * hob_lbal, hob_lbam, and hob_lbah.
309 * This function waits for idle (!BUSY and !DRQ) after writing
310 * registers. If the control register has a new value, this
311 * function also waits for idle after writing control and before
312 * writing the remaining registers.
318 * 0 on success, -errno on failure.
320 static int taskfile_load_raw(struct ata_device
*dev
,
321 const struct ata_acpi_gtf
*gtf
)
323 struct ata_port
*ap
= dev
->ap
;
324 struct ata_taskfile tf
, rtf
;
325 unsigned int err_mask
;
327 if ((gtf
->tf
[0] == 0) && (gtf
->tf
[1] == 0) && (gtf
->tf
[2] == 0)
328 && (gtf
->tf
[3] == 0) && (gtf
->tf
[4] == 0) && (gtf
->tf
[5] == 0)
329 && (gtf
->tf
[6] == 0))
332 ata_tf_init(dev
, &tf
);
334 /* convert gtf to tf */
335 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
; /* TBD */
336 tf
.protocol
= ATA_PROT_NODATA
;
337 tf
.feature
= gtf
->tf
[0]; /* 0x1f1 */
338 tf
.nsect
= gtf
->tf
[1]; /* 0x1f2 */
339 tf
.lbal
= gtf
->tf
[2]; /* 0x1f3 */
340 tf
.lbam
= gtf
->tf
[3]; /* 0x1f4 */
341 tf
.lbah
= gtf
->tf
[4]; /* 0x1f5 */
342 tf
.device
= gtf
->tf
[5]; /* 0x1f6 */
343 tf
.command
= gtf
->tf
[6]; /* 0x1f7 */
345 if (ata_msg_probe(ap
))
346 ata_dev_printk(dev
, KERN_DEBUG
, "executing ACPI cmd "
347 "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
348 tf
.command
, tf
.feature
, tf
.nsect
,
349 tf
.lbal
, tf
.lbam
, tf
.lbah
, tf
.device
);
352 err_mask
= ata_exec_internal(dev
, &rtf
, NULL
, DMA_NONE
, NULL
, 0);
354 ata_dev_printk(dev
, KERN_ERR
,
355 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
356 "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
357 tf
.command
, tf
.feature
, tf
.nsect
, tf
.lbal
, tf
.lbam
,
358 tf
.lbah
, tf
.device
, err_mask
, rtf
.command
, rtf
.feature
);
366 * ata_acpi_exec_tfs - get then write drive taskfile settings
367 * @dev: target ATA device
369 * Evaluate _GTF and excute returned taskfiles.
375 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
376 * doesn't contain valid data. -errno on other errors.
378 static int ata_acpi_exec_tfs(struct ata_device
*dev
)
380 struct ata_acpi_gtf
*gtf
= NULL
;
381 void *ptr_to_free
= NULL
;
382 int gtf_count
, i
, rc
;
385 rc
= ata_dev_get_GTF(dev
, >f
, &ptr_to_free
);
391 for (i
= 0, rc
= 0; i
< gtf_count
; i
++) {
394 /* ACPI errors are eventually ignored. Run till the
395 * end even after errors.
397 tmp
= taskfile_load_raw(dev
, gtf
++);
410 * ata_acpi_push_id - send Identify data to drive
411 * @dev: target ATA device
413 * _SDD ACPI object: for SATA mode only
414 * Must be after Identify (Packet) Device -- uses its data
415 * ATM this function never returns a failure. It is an optional
416 * method and if it fails for whatever reason, we should still
423 * 0 on success, -errno on failure.
425 static int ata_acpi_push_id(struct ata_device
*dev
)
427 struct ata_port
*ap
= dev
->ap
;
430 struct acpi_object_list input
;
431 union acpi_object in_params
[1];
433 if (ata_msg_probe(ap
))
434 ata_dev_printk(dev
, KERN_DEBUG
, "%s: ix = %d, port#: %d\n",
435 __FUNCTION__
, dev
->devno
, ap
->port_no
);
437 /* Give the drive Identify data to the drive via the _SDD method */
438 /* _SDD: set up input parameters */
440 input
.pointer
= in_params
;
441 in_params
[0].type
= ACPI_TYPE_BUFFER
;
442 in_params
[0].buffer
.length
= sizeof(dev
->id
[0]) * ATA_ID_WORDS
;
443 in_params
[0].buffer
.pointer
= (u8
*)dev
->id
;
444 /* Output buffer: _SDD has no output */
446 /* It's OK for _SDD to be missing too. */
447 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
448 status
= acpi_evaluate_object(dev
->acpi_handle
, "_SDD", &input
, NULL
);
449 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
451 err
= ACPI_FAILURE(status
) ? -EIO
: 0;
453 ata_dev_printk(dev
, KERN_WARNING
,
454 "ACPI _SDD failed (AE 0x%x)\n", status
);
460 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
461 * @ap: target ATA port
463 * This function is called when @ap is about to be suspended. All
464 * devices are already put to sleep but the port_suspend() callback
465 * hasn't been executed yet. Error return from this function aborts
472 * 0 on success, -errno on failure.
474 int ata_acpi_on_suspend(struct ata_port
*ap
)
479 /* proceed iff per-port acpi_handle is valid */
480 if (!ap
->acpi_handle
)
482 BUG_ON(ap
->flags
& ATA_FLAG_ACPI_SATA
);
484 /* store timing parameters */
485 rc
= ata_acpi_gtm(ap
, &ap
->acpi_gtm
);
487 spin_lock_irqsave(ap
->lock
, flags
);
489 ap
->pflags
|= ATA_PFLAG_GTM_VALID
;
491 ap
->pflags
&= ~ATA_PFLAG_GTM_VALID
;
492 spin_unlock_irqrestore(ap
->lock
, flags
);
500 * ata_acpi_on_resume - ATA ACPI hook called on resume
501 * @ap: target ATA port
503 * This function is called when @ap is resumed - right after port
504 * itself is resumed but before any EH action is taken.
509 void ata_acpi_on_resume(struct ata_port
*ap
)
513 if (ap
->acpi_handle
&& (ap
->pflags
& ATA_PFLAG_GTM_VALID
)) {
514 BUG_ON(ap
->flags
& ATA_FLAG_ACPI_SATA
);
516 /* restore timing parameters */
517 ata_acpi_stm(ap
, &ap
->acpi_gtm
);
521 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
522 ap
->device
[i
].flags
|= ATA_DFLAG_ACPI_PENDING
;
526 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
527 * @dev: target ATA device
529 * This function is called when @dev is about to be configured.
530 * IDENTIFY data might have been modified after this hook is run.
536 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
539 int ata_acpi_on_devcfg(struct ata_device
*dev
)
541 struct ata_port
*ap
= dev
->ap
;
542 struct ata_eh_context
*ehc
= &ap
->eh_context
;
543 int acpi_sata
= ap
->flags
& ATA_FLAG_ACPI_SATA
;
546 if (!dev
->acpi_handle
)
549 /* do we need to do _GTF? */
550 if (!(dev
->flags
& ATA_DFLAG_ACPI_PENDING
) &&
551 !(acpi_sata
&& (ehc
->i
.flags
& ATA_EHI_DID_HARDRESET
)))
554 /* do _SDD if SATA */
556 rc
= ata_acpi_push_id(dev
);
562 rc
= ata_acpi_exec_tfs(dev
);
566 dev
->flags
&= ~ATA_DFLAG_ACPI_PENDING
;
568 /* refresh IDENTIFY page if any _GTF command has been executed */
570 rc
= ata_dev_reread_id(dev
, 0);
572 ata_dev_printk(dev
, KERN_ERR
, "failed to IDENTIFY "
573 "after ACPI commands\n");
581 /* let EH retry on the first failure, disable ACPI on the second */
582 if (dev
->flags
& ATA_DFLAG_ACPI_FAILED
) {
583 ata_dev_printk(dev
, KERN_WARNING
, "ACPI on devcfg failed the "
584 "second time, disabling (errno=%d)\n", rc
);
586 dev
->acpi_handle
= NULL
;
588 /* if port is working, request IDENTIFY reload and continue */
589 if (!(ap
->pflags
& ATA_PFLAG_FROZEN
))
592 dev
->flags
|= ATA_DFLAG_ACPI_FAILED
;