[IRDA]: Avoid a label defined but not used warning in irda_init()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ata / libata-acpi.c
blobc059f78ad944dc18a3372f63a49a14b4b0179441
1 /*
2 * libata-acpi.c
3 * Provides ACPI support for PATA/SATA.
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
7 */
9 #include <linux/ata.h>
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>
17 #include "libata.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
31 struct ata_acpi_gtf {
32 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
33 } __packed;
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)
52 int max_devices, i;
54 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
55 if (!ap->acpi_handle)
56 return;
58 max_devices = 1;
59 if (ap->flags & ATA_FLAG_SLAVE_POSS)
60 max_devices++;
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);
69 /**
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.
76 * LOCKING:
77 * EH context.
79 * RETURNS:
80 * 0 on success, -errno on failure.
82 void ata_acpi_associate(struct ata_host *host)
84 int i;
86 if (!is_pci_dev(host->dev) || libata_noacpi)
87 return;
89 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
90 if (!host->acpi_handle)
91 return;
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);
98 else
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.
110 * LOCKING:
111 * EH context.
113 * RETURNS:
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;
120 acpi_status status;
121 int rc = 0;
123 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
125 rc = -ENOENT;
126 if (status == AE_NOT_FOUND)
127 goto out_free;
129 rc = -EINVAL;
130 if (ACPI_FAILURE(status)) {
131 ata_port_printk(ap, KERN_ERR,
132 "ACPI get timing mode failed (AE 0x%x)\n",
133 status);
134 goto out_free;
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",
141 out_obj->type);
143 goto out_free;
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);
150 goto out_free;
153 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
154 rc = 0;
155 out_free:
156 kfree(output.pointer);
157 return rc;
161 * ata_acpi_stm - execute _STM
162 * @ap: target ATA port
163 * @stm: timing parameter to _STM
165 * Evaluate _STM with timing parameter @stm.
167 * LOCKING:
168 * EH context.
170 * RETURNS:
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)
175 acpi_status status;
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;
190 input.count = 3;
191 input.pointer = in_params;
193 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
195 if (status == AE_NOT_FOUND)
196 return -ENOENT;
197 if (ACPI_FAILURE(status)) {
198 ata_port_printk(ap, KERN_ERR,
199 "ACPI set timing mode failed (status=0x%x)\n", status);
200 return -EINVAL;
202 return 0;
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.
219 * LOCKING:
220 * EH context.
222 * RETURNS:
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,
227 void **ptr_to_free)
229 struct ata_port *ap = dev->ap;
230 acpi_status status;
231 struct acpi_buffer output;
232 union acpi_object *out_obj;
233 int rc = 0;
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",
250 status);
251 rc = -EIO;
253 goto out_free;
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",
260 __FUNCTION__,
261 (unsigned long long)output.length,
262 output.pointer);
263 goto out_free;
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",
270 out_obj->type);
271 rc = -EINVAL;
272 goto out_free;
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);
279 rc = -EINVAL;
280 goto out_free;
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);
291 return rc;
293 out_free:
294 kfree(output.pointer);
295 return rc;
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.
314 * LOCKING:
315 * EH context.
317 * RETURNS:
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))
330 return 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);
351 rtf = tf;
352 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
353 if (err_mask) {
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);
359 return -EIO;
362 return 0;
366 * ata_acpi_exec_tfs - get then write drive taskfile settings
367 * @dev: target ATA device
369 * Evaluate _GTF and excute returned taskfiles.
371 * LOCKING:
372 * EH context.
374 * RETURNS:
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;
384 /* get taskfiles */
385 rc = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
386 if (rc < 0)
387 return rc;
388 gtf_count = rc;
390 /* execute them */
391 for (i = 0, rc = 0; i < gtf_count; i++) {
392 int tmp;
394 /* ACPI errors are eventually ignored. Run till the
395 * end even after errors.
397 tmp = taskfile_load_raw(dev, gtf++);
398 if (!rc)
399 rc = tmp;
402 kfree(ptr_to_free);
404 if (rc == 0)
405 return gtf_count;
406 return rc;
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
417 * just keep going.
419 * LOCKING:
420 * EH context.
422 * RETURNS:
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;
428 int err;
429 acpi_status status;
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 */
439 input.count = 1;
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;
452 if (err < 0)
453 ata_dev_printk(dev, KERN_WARNING,
454 "ACPI _SDD failed (AE 0x%x)\n", status);
456 return err;
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
466 * suspend.
468 * LOCKING:
469 * EH context.
471 * RETURNS:
472 * 0 on success, -errno on failure.
474 int ata_acpi_on_suspend(struct ata_port *ap)
476 unsigned long flags;
477 int rc;
479 /* proceed iff per-port acpi_handle is valid */
480 if (!ap->acpi_handle)
481 return 0;
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);
488 if (rc == 0)
489 ap->pflags |= ATA_PFLAG_GTM_VALID;
490 else
491 ap->pflags &= ~ATA_PFLAG_GTM_VALID;
492 spin_unlock_irqrestore(ap->lock, flags);
494 if (rc == -ENOENT)
495 rc = 0;
496 return rc;
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.
506 * LOCKING:
507 * EH context.
509 void ata_acpi_on_resume(struct ata_port *ap)
511 int i;
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);
520 /* schedule _GTF */
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.
532 * LOCKING:
533 * EH context.
535 * RETURNS:
536 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
537 * -errno on failure.
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;
544 int rc;
546 if (!dev->acpi_handle)
547 return 0;
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)))
552 return 0;
554 /* do _SDD if SATA */
555 if (acpi_sata) {
556 rc = ata_acpi_push_id(dev);
557 if (rc)
558 goto acpi_err;
561 /* do _GTF */
562 rc = ata_acpi_exec_tfs(dev);
563 if (rc < 0)
564 goto acpi_err;
566 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
568 /* refresh IDENTIFY page if any _GTF command has been executed */
569 if (rc > 0) {
570 rc = ata_dev_reread_id(dev, 0);
571 if (rc < 0) {
572 ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
573 "after ACPI commands\n");
574 return rc;
578 return 0;
580 acpi_err:
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))
590 rc = 1;
592 dev->flags |= ATA_DFLAG_ACPI_FAILED;
593 return rc;