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->link.device->acpi_handle =
48 acpi_get_child(ap->host->acpi_handle, adr);
51 static void ata_acpi_associate_ide_port(struct ata_port *ap)
55 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
60 if (ap->flags & ATA_FLAG_SLAVE_POSS)
63 for (i = 0; i < max_devices; i++) {
64 struct ata_device *dev = &ap->link.device[i];
66 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
71 * ata_acpi_associate - associate ATA host with ACPI objects
72 * @host: target ATA host
74 * Look up ACPI objects associated with @host and initialize
75 * acpi_handle fields of @host, its ports and devices accordingly.
81 * 0 on success, -errno on failure.
83 void ata_acpi_associate(struct ata_host *host)
87 if (!is_pci_dev(host->dev) || libata_noacpi)
90 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
91 if (!host->acpi_handle)
94 for (i = 0; i < host->n_ports; i++) {
95 struct ata_port *ap = host->ports[i];
97 if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
98 ata_acpi_associate_sata_port(ap);
100 ata_acpi_associate_ide_port(ap);
105 * ata_acpi_gtm - execute _GTM
106 * @ap: target ATA port
107 * @gtm: out parameter for _GTM result
109 * Evaluate _GTM and store the result in @gtm.
115 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
117 static int ata_acpi_gtm(const struct ata_port *ap, struct ata_acpi_gtm *gtm)
119 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
120 union acpi_object *out_obj;
124 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
127 if (status == AE_NOT_FOUND)
131 if (ACPI_FAILURE(status)) {
132 ata_port_printk(ap, KERN_ERR,
133 "ACPI get timing mode failed (AE 0x%x)\n",
138 out_obj = output.pointer;
139 if (out_obj->type != ACPI_TYPE_BUFFER) {
140 ata_port_printk(ap, KERN_WARNING,
141 "_GTM returned unexpected object type 0x%x\n",
147 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
148 ata_port_printk(ap, KERN_ERR,
149 "_GTM returned invalid length %d\n",
150 out_obj->buffer.length);
154 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
157 kfree(output.pointer);
162 * ata_acpi_stm - execute _STM
163 * @ap: target ATA port
164 * @stm: timing parameter to _STM
166 * Evaluate _STM with timing parameter @stm.
172 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
174 static int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm)
177 struct acpi_object_list input;
178 union acpi_object in_params[3];
180 in_params[0].type = ACPI_TYPE_BUFFER;
181 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
182 in_params[0].buffer.pointer = (u8 *)stm;
183 /* Buffers for id may need byteswapping ? */
184 in_params[1].type = ACPI_TYPE_BUFFER;
185 in_params[1].buffer.length = 512;
186 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
187 in_params[2].type = ACPI_TYPE_BUFFER;
188 in_params[2].buffer.length = 512;
189 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
192 input.pointer = in_params;
194 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
196 if (status == AE_NOT_FOUND)
198 if (ACPI_FAILURE(status)) {
199 ata_port_printk(ap, KERN_ERR,
200 "ACPI set timing mode failed (status=0x%x)\n", status);
207 * ata_dev_get_GTF - get the drive bootup default taskfile settings
208 * @dev: target ATA device
209 * @gtf: output parameter for buffer containing _GTF taskfile arrays
210 * @ptr_to_free: pointer which should be freed
212 * This applies to both PATA and SATA drives.
214 * The _GTF method has no input parameters.
215 * It returns a variable number of register set values (registers
216 * hex 1F1..1F7, taskfiles).
217 * The <variable number> is not known in advance, so have ACPI-CA
218 * allocate the buffer as needed and return it, then free it later.
224 * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
225 * contain valid data. -errno on other errors.
227 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
230 struct ata_port *ap = dev->link->ap;
232 struct acpi_buffer output;
233 union acpi_object *out_obj;
236 /* set up output buffer */
237 output.length = ACPI_ALLOCATE_BUFFER;
238 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
240 if (ata_msg_probe(ap))
241 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
242 __FUNCTION__, ap->port_no);
244 /* _GTF has no input parameters */
245 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
247 if (ACPI_FAILURE(status)) {
248 if (status != AE_NOT_FOUND) {
249 ata_dev_printk(dev, KERN_WARNING,
250 "_GTF evaluation failed (AE 0x%x)\n",
257 if (!output.length || !output.pointer) {
258 if (ata_msg_probe(ap))
259 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
260 "length or ptr is NULL (0x%llx, 0x%p)\n",
262 (unsigned long long)output.length,
267 out_obj = output.pointer;
268 if (out_obj->type != ACPI_TYPE_BUFFER) {
269 ata_dev_printk(dev, KERN_WARNING,
270 "_GTF unexpected object type 0x%x\n",
276 if (out_obj->buffer.length % REGS_PER_GTF) {
277 ata_dev_printk(dev, KERN_WARNING,
278 "unexpected _GTF length (%d)\n",
279 out_obj->buffer.length);
284 *ptr_to_free = out_obj;
285 *gtf = (void *)out_obj->buffer.pointer;
286 rc = out_obj->buffer.length / REGS_PER_GTF;
288 if (ata_msg_probe(ap))
289 ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
290 "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
291 __FUNCTION__, *gtf, rc, *ptr_to_free);
295 kfree(output.pointer);
300 * taskfile_load_raw - send taskfile registers to host controller
301 * @dev: target ATA device
302 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
304 * Outputs ATA taskfile to standard ATA host controller using MMIO
305 * or PIO as indicated by the ATA_FLAG_MMIO flag.
306 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
307 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
308 * hob_lbal, hob_lbam, and hob_lbah.
310 * This function waits for idle (!BUSY and !DRQ) after writing
311 * registers. If the control register has a new value, this
312 * function also waits for idle after writing control and before
313 * writing the remaining registers.
319 * 0 on success, -errno on failure.
321 static int taskfile_load_raw(struct ata_device *dev,
322 const struct ata_acpi_gtf *gtf)
324 struct ata_port *ap = dev->link->ap;
325 struct ata_taskfile tf, rtf;
326 unsigned int err_mask;
328 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
329 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
330 && (gtf->tf[6] == 0))
333 ata_tf_init(dev, &tf);
335 /* convert gtf to tf */
336 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
337 tf.protocol = ATA_PROT_NODATA;
338 tf.feature = gtf->tf[0]; /* 0x1f1 */
339 tf.nsect = gtf->tf[1]; /* 0x1f2 */
340 tf.lbal = gtf->tf[2]; /* 0x1f3 */
341 tf.lbam = gtf->tf[3]; /* 0x1f4 */
342 tf.lbah = gtf->tf[4]; /* 0x1f5 */
343 tf.device = gtf->tf[5]; /* 0x1f6 */
344 tf.command = gtf->tf[6]; /* 0x1f7 */
346 if (ata_msg_probe(ap))
347 ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
348 "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
349 tf.command, tf.feature, tf.nsect,
350 tf.lbal, tf.lbam, tf.lbah, tf.device);
353 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
355 ata_dev_printk(dev, KERN_ERR,
356 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
357 "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
358 tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
359 tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
367 * ata_acpi_exec_tfs - get then write drive taskfile settings
368 * @dev: target ATA device
370 * Evaluate _GTF and excute returned taskfiles.
376 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
377 * doesn't contain valid data. -errno on other errors.
379 static int ata_acpi_exec_tfs(struct ata_device *dev)
381 struct ata_acpi_gtf *gtf = NULL;
382 void *ptr_to_free = NULL;
383 int gtf_count, i, rc;
386 rc = ata_dev_get_GTF(dev, >f, &ptr_to_free);
392 for (i = 0, rc = 0; i < gtf_count; i++) {
395 /* ACPI errors are eventually ignored. Run till the
396 * end even after errors.
398 tmp = taskfile_load_raw(dev, gtf++);
411 * ata_acpi_push_id - send Identify data to drive
412 * @dev: target ATA device
414 * _SDD ACPI object: for SATA mode only
415 * Must be after Identify (Packet) Device -- uses its data
416 * ATM this function never returns a failure. It is an optional
417 * method and if it fails for whatever reason, we should still
424 * 0 on success, -errno on failure.
426 static int ata_acpi_push_id(struct ata_device *dev)
428 struct ata_port *ap = dev->link->ap;
431 struct acpi_object_list input;
432 union acpi_object in_params[1];
434 if (ata_msg_probe(ap))
435 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
436 __FUNCTION__, dev->devno, ap->port_no);
438 /* Give the drive Identify data to the drive via the _SDD method */
439 /* _SDD: set up input parameters */
441 input.pointer = in_params;
442 in_params[0].type = ACPI_TYPE_BUFFER;
443 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
444 in_params[0].buffer.pointer = (u8 *)dev->id;
445 /* Output buffer: _SDD has no output */
447 /* It's OK for _SDD to be missing too. */
448 swap_buf_le16(dev->id, ATA_ID_WORDS);
449 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
450 swap_buf_le16(dev->id, ATA_ID_WORDS);
452 err = ACPI_FAILURE(status) ? -EIO : 0;
454 ata_dev_printk(dev, KERN_WARNING,
455 "ACPI _SDD failed (AE 0x%x)\n", status);
461 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
462 * @ap: target ATA port
464 * This function is called when @ap is about to be suspended. All
465 * devices are already put to sleep but the port_suspend() callback
466 * hasn't been executed yet. Error return from this function aborts
473 * 0 on success, -errno on failure.
475 int ata_acpi_on_suspend(struct ata_port *ap)
480 /* proceed iff per-port acpi_handle is valid */
481 if (!ap->acpi_handle)
483 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
485 /* store timing parameters */
486 rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
488 spin_lock_irqsave(ap->lock, flags);
490 ap->pflags |= ATA_PFLAG_GTM_VALID;
492 ap->pflags &= ~ATA_PFLAG_GTM_VALID;
493 spin_unlock_irqrestore(ap->lock, flags);
501 * ata_acpi_on_resume - ATA ACPI hook called on resume
502 * @ap: target ATA port
504 * This function is called when @ap is resumed - right after port
505 * itself is resumed but before any EH action is taken.
510 void ata_acpi_on_resume(struct ata_port *ap)
512 struct ata_device *dev;
514 if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
515 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
517 /* restore timing parameters */
518 ata_acpi_stm(ap, &ap->acpi_gtm);
522 ata_link_for_each_dev(dev, &ap->link)
523 dev->flags |= ATA_DFLAG_ACPI_PENDING;
527 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
528 * @dev: target ATA device
530 * This function is called when @dev is about to be configured.
531 * IDENTIFY data might have been modified after this hook is run.
537 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
540 int ata_acpi_on_devcfg(struct ata_device *dev)
542 struct ata_port *ap = dev->link->ap;
543 struct ata_eh_context *ehc = &ap->link.eh_context;
544 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
547 if (!dev->acpi_handle)
550 /* do we need to do _GTF? */
551 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
552 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
555 /* do _SDD if SATA */
557 rc = ata_acpi_push_id(dev);
563 rc = ata_acpi_exec_tfs(dev);
567 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
569 /* refresh IDENTIFY page if any _GTF command has been executed */
571 rc = ata_dev_reread_id(dev, 0);
573 ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
574 "after ACPI commands\n");
582 /* let EH retry on the first failure, disable ACPI on the second */
583 if (dev->flags & ATA_DFLAG_ACPI_FAILED) {
584 ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the "
585 "second time, disabling (errno=%d)\n", rc);
587 dev->acpi_handle = NULL;
589 /* if port is working, request IDENTIFY reload and continue */
590 if (!(ap->pflags & ATA_PFLAG_FROZEN))
593 dev->flags |= ATA_DFLAG_ACPI_FAILED;