blob: 0023ac4ff4966ee777412af6c4787686694679ed [file] [log] [blame]
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -07001/*
2 * libata-acpi.c
3 * Provides ACPI support for PATA/SATA.
4 *
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
7 */
8
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"
18
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>
26
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070027#define NO_PORT_MULT 0xffff
Tejun Heofafbae82007-05-15 03:28:16 +090028#define SATA_ADR(root,pmp) (((root) << 16) | (pmp))
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070029
30#define REGS_PER_GTF 7
Tejun Heo4700c4b2007-05-15 03:28:16 +090031struct ata_acpi_gtf {
32 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
33} __packed;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070034
Alan Coxca426632007-03-08 23:13:50 +000035/*
36 * Helper - belongs in the PCI layer somewhere eventually
37 */
38static int is_pci_dev(struct device *dev)
39{
40 return (dev->bus == &pci_bus_type);
41}
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070042
Tejun Heofafbae82007-05-15 03:28:16 +090043static void ata_acpi_associate_sata_port(struct ata_port *ap)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070044{
Tejun Heofafbae82007-05-15 03:28:16 +090045 acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070046
Tejun Heo9af5c9c2007-08-06 18:36:22 +090047 ap->link.device->acpi_handle =
48 acpi_get_child(ap->host->acpi_handle, adr);
Tejun Heofafbae82007-05-15 03:28:16 +090049}
Alan Coxca426632007-03-08 23:13:50 +000050
Tejun Heofafbae82007-05-15 03:28:16 +090051static void ata_acpi_associate_ide_port(struct ata_port *ap)
52{
53 int max_devices, i;
54
55 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
56 if (!ap->acpi_handle)
57 return;
58
59 max_devices = 1;
60 if (ap->flags & ATA_FLAG_SLAVE_POSS)
61 max_devices++;
62
63 for (i = 0; i < max_devices; i++) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +090064 struct ata_device *dev = &ap->link.device[i];
Tejun Heofafbae82007-05-15 03:28:16 +090065
66 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
67 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070068}
69
70/**
Tejun Heofafbae82007-05-15 03:28:16 +090071 * ata_acpi_associate - associate ATA host with ACPI objects
72 * @host: target ATA host
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070073 *
Tejun Heofafbae82007-05-15 03:28:16 +090074 * Look up ACPI objects associated with @host and initialize
75 * acpi_handle fields of @host, its ports and devices accordingly.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070076 *
Tejun Heofafbae82007-05-15 03:28:16 +090077 * LOCKING:
78 * EH context.
79 *
80 * RETURNS:
81 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070082 */
Tejun Heofafbae82007-05-15 03:28:16 +090083void ata_acpi_associate(struct ata_host *host)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070084{
Tejun Heofafbae82007-05-15 03:28:16 +090085 int i;
Alan Coxca426632007-03-08 23:13:50 +000086
Tejun Heofafbae82007-05-15 03:28:16 +090087 if (!is_pci_dev(host->dev) || libata_noacpi)
88 return;
Alan Coxca426632007-03-08 23:13:50 +000089
Tejun Heofafbae82007-05-15 03:28:16 +090090 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
91 if (!host->acpi_handle)
92 return;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070093
Tejun Heofafbae82007-05-15 03:28:16 +090094 for (i = 0; i < host->n_ports; i++) {
95 struct ata_port *ap = host->ports[i];
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070096
Tejun Heofafbae82007-05-15 03:28:16 +090097 if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
98 ata_acpi_associate_sata_port(ap);
99 else
100 ata_acpi_associate_ide_port(ap);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700101 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700102}
103
104/**
Tejun Heo64578a3de2007-05-15 03:28:16 +0900105 * ata_acpi_gtm - execute _GTM
106 * @ap: target ATA port
107 * @gtm: out parameter for _GTM result
108 *
109 * Evaluate _GTM and store the result in @gtm.
110 *
111 * LOCKING:
112 * EH context.
113 *
114 * RETURNS:
115 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
116 */
117static int ata_acpi_gtm(const struct ata_port *ap, struct ata_acpi_gtm *gtm)
118{
119 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
120 union acpi_object *out_obj;
121 acpi_status status;
122 int rc = 0;
123
124 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
125
126 rc = -ENOENT;
127 if (status == AE_NOT_FOUND)
128 goto out_free;
129
130 rc = -EINVAL;
131 if (ACPI_FAILURE(status)) {
132 ata_port_printk(ap, KERN_ERR,
133 "ACPI get timing mode failed (AE 0x%x)\n",
134 status);
135 goto out_free;
136 }
137
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",
142 out_obj->type);
143
144 goto out_free;
145 }
146
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);
151 goto out_free;
152 }
153
154 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
155 rc = 0;
156 out_free:
157 kfree(output.pointer);
158 return rc;
159}
160
161/**
162 * ata_acpi_stm - execute _STM
163 * @ap: target ATA port
164 * @stm: timing parameter to _STM
165 *
166 * Evaluate _STM with timing parameter @stm.
167 *
168 * LOCKING:
169 * EH context.
170 *
171 * RETURNS:
172 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
173 */
174static int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm)
175{
176 acpi_status status;
177 struct acpi_object_list input;
178 union acpi_object in_params[3];
179
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;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900186 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900187 in_params[2].type = ACPI_TYPE_BUFFER;
188 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900189 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900190
191 input.count = 3;
192 input.pointer = in_params;
193
194 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
195
196 if (status == AE_NOT_FOUND)
197 return -ENOENT;
198 if (ACPI_FAILURE(status)) {
199 ata_port_printk(ap, KERN_ERR,
200 "ACPI set timing mode failed (status=0x%x)\n", status);
201 return -EINVAL;
202 }
203 return 0;
204}
205
206/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900207 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900208 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900209 * @gtf: output parameter for buffer containing _GTF taskfile arrays
210 * @ptr_to_free: pointer which should be freed
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700211 *
212 * This applies to both PATA and SATA drives.
213 *
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.
219 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900220 * LOCKING:
221 * EH context.
222 *
223 * RETURNS:
224 * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
225 * contain valid data. -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700226 */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900227static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
228 void **ptr_to_free)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700229{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900230 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900231 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900232 struct acpi_buffer output;
233 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900234 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700235
Tejun Heo4700c4b2007-05-15 03:28:16 +0900236 /* set up output buffer */
237 output.length = ACPI_ALLOCATE_BUFFER;
238 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700239
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700240 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900241 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
Tejun Heo878d4fe2007-02-21 16:36:33 +0900242 __FUNCTION__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700243
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700244 /* _GTF has no input parameters */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900245 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
246
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700247 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900248 if (status != AE_NOT_FOUND) {
249 ata_dev_printk(dev, KERN_WARNING,
250 "_GTF evaluation failed (AE 0x%x)\n",
251 status);
252 rc = -EIO;
253 }
254 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700255 }
256
257 if (!output.length || !output.pointer) {
258 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900259 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700260 "length or ptr is NULL (0x%llx, 0x%p)\n",
261 __FUNCTION__,
262 (unsigned long long)output.length,
263 output.pointer);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900264 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700265 }
266
267 out_obj = output.pointer;
268 if (out_obj->type != ACPI_TYPE_BUFFER) {
Tejun Heo69b16a52007-05-15 03:28:16 +0900269 ata_dev_printk(dev, KERN_WARNING,
270 "_GTF unexpected object type 0x%x\n",
271 out_obj->type);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900272 rc = -EINVAL;
273 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700274 }
275
Tejun Heo4700c4b2007-05-15 03:28:16 +0900276 if (out_obj->buffer.length % REGS_PER_GTF) {
Tejun Heo69b16a52007-05-15 03:28:16 +0900277 ata_dev_printk(dev, KERN_WARNING,
278 "unexpected _GTF length (%d)\n",
279 out_obj->buffer.length);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900280 rc = -EINVAL;
281 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700282 }
283
Tejun Heo4700c4b2007-05-15 03:28:16 +0900284 *ptr_to_free = out_obj;
285 *gtf = (void *)out_obj->buffer.pointer;
286 rc = out_obj->buffer.length / REGS_PER_GTF;
287
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700288 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900289 ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
Tejun Heo4700c4b2007-05-15 03:28:16 +0900290 "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
291 __FUNCTION__, *gtf, rc, *ptr_to_free);
292 return rc;
293
294 out_free:
295 kfree(output.pointer);
296 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700297}
298
299/**
300 * taskfile_load_raw - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900301 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700302 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
303 *
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.
309 *
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.
314 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900315 * LOCKING:
316 * EH context.
317 *
318 * RETURNS:
319 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700320 */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900321static int taskfile_load_raw(struct ata_device *dev,
322 const struct ata_acpi_gtf *gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700323{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900324 struct ata_port *ap = dev->link->ap;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900325 struct ata_taskfile tf, rtf;
326 unsigned int err_mask;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500327
Tejun Heo4700c4b2007-05-15 03:28:16 +0900328 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))
331 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700332
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900333 ata_tf_init(dev, &tf);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700334
Jeff Garzikfc16c252007-02-24 21:05:01 -0500335 /* convert gtf to tf */
336 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
Tejun Heo48be6b12007-04-23 02:06:46 +0900337 tf.protocol = ATA_PROT_NODATA;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900338 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 */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700345
Tejun Heo4700c4b2007-05-15 03:28:16 +0900346 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);
351
352 rtf = tf;
353 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
354 if (err_mask) {
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900355 ata_dev_printk(dev, KERN_ERR,
Tejun Heo4700c4b2007-05-15 03:28:16 +0900356 "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);
360 return -EIO;
361 }
362
363 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700364}
365
366/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700367 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900368 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700369 *
Tejun Heo67465442007-05-15 03:28:16 +0900370 * Evaluate _GTF and excute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900371 *
372 * LOCKING:
373 * EH context.
374 *
375 * RETURNS:
Tejun Heo67465442007-05-15 03:28:16 +0900376 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
377 * doesn't contain valid data. -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700378 */
Tejun Heo67465442007-05-15 03:28:16 +0900379static int ata_acpi_exec_tfs(struct ata_device *dev)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700380{
Tejun Heo67465442007-05-15 03:28:16 +0900381 struct ata_acpi_gtf *gtf = NULL;
382 void *ptr_to_free = NULL;
383 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700384
Tejun Heo67465442007-05-15 03:28:16 +0900385 /* get taskfiles */
386 rc = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
387 if (rc < 0)
388 return rc;
389 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700390
Tejun Heo67465442007-05-15 03:28:16 +0900391 /* execute them */
392 for (i = 0, rc = 0; i < gtf_count; i++) {
393 int tmp;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900394
Tejun Heo67465442007-05-15 03:28:16 +0900395 /* ACPI errors are eventually ignored. Run till the
396 * end even after errors.
397 */
398 tmp = taskfile_load_raw(dev, gtf++);
399 if (!rc)
400 rc = tmp;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700401 }
402
Tejun Heo67465442007-05-15 03:28:16 +0900403 kfree(ptr_to_free);
404
405 if (rc == 0)
406 return gtf_count;
407 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700408}
409
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700410/**
411 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900412 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700413 *
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
418 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900419 *
420 * LOCKING:
421 * EH context.
422 *
423 * RETURNS:
424 * 0 on success, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700425 */
Tejun Heo67465442007-05-15 03:28:16 +0900426static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700427{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900428 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900429 int err;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900430 acpi_status status;
431 struct acpi_object_list input;
432 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700433
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700434 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900435 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
436 __FUNCTION__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700437
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700438 /* Give the drive Identify data to the drive via the _SDD method */
439 /* _SDD: set up input parameters */
440 input.count = 1;
441 input.pointer = in_params;
442 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900443 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
444 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700445 /* Output buffer: _SDD has no output */
446
447 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900448 swap_buf_le16(dev->id, ATA_ID_WORDS);
Tejun Heofafbae82007-05-15 03:28:16 +0900449 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900450 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700451
452 err = ACPI_FAILURE(status) ? -EIO : 0;
Tejun Heo69b16a52007-05-15 03:28:16 +0900453 if (err < 0)
454 ata_dev_printk(dev, KERN_WARNING,
455 "ACPI _SDD failed (AE 0x%x)\n", status);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700456
Tejun Heo67465442007-05-15 03:28:16 +0900457 return err;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700458}
459
Tejun Heo67465442007-05-15 03:28:16 +0900460/**
Tejun Heo64578a3de2007-05-15 03:28:16 +0900461 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
462 * @ap: target ATA port
463 *
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
467 * suspend.
468 *
469 * LOCKING:
470 * EH context.
471 *
472 * RETURNS:
473 * 0 on success, -errno on failure.
474 */
475int ata_acpi_on_suspend(struct ata_port *ap)
476{
477 unsigned long flags;
478 int rc;
479
480 /* proceed iff per-port acpi_handle is valid */
481 if (!ap->acpi_handle)
482 return 0;
483 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
484
485 /* store timing parameters */
486 rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
487
488 spin_lock_irqsave(ap->lock, flags);
489 if (rc == 0)
490 ap->pflags |= ATA_PFLAG_GTM_VALID;
491 else
492 ap->pflags &= ~ATA_PFLAG_GTM_VALID;
493 spin_unlock_irqrestore(ap->lock, flags);
494
495 if (rc == -ENOENT)
496 rc = 0;
497 return rc;
498}
499
500/**
Tejun Heo67465442007-05-15 03:28:16 +0900501 * ata_acpi_on_resume - ATA ACPI hook called on resume
502 * @ap: target ATA port
503 *
504 * This function is called when @ap is resumed - right after port
505 * itself is resumed but before any EH action is taken.
506 *
507 * LOCKING:
508 * EH context.
509 */
510void ata_acpi_on_resume(struct ata_port *ap)
511{
512 int i;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700513
Tejun Heo64578a3de2007-05-15 03:28:16 +0900514 if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
515 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
516
517 /* restore timing parameters */
518 ata_acpi_stm(ap, &ap->acpi_gtm);
519 }
520
Tejun Heo67465442007-05-15 03:28:16 +0900521 /* schedule _GTF */
522 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900523 ap->link.device[i].flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo67465442007-05-15 03:28:16 +0900524}
525
526/**
527 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
528 * @dev: target ATA device
529 *
530 * This function is called when @dev is about to be configured.
531 * IDENTIFY data might have been modified after this hook is run.
532 *
533 * LOCKING:
534 * EH context.
535 *
536 * RETURNS:
537 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
538 * -errno on failure.
539 */
540int ata_acpi_on_devcfg(struct ata_device *dev)
541{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900542 struct ata_port *ap = dev->link->ap;
543 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900544 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
545 int rc;
546
Tejun Heo67465442007-05-15 03:28:16 +0900547 if (!dev->acpi_handle)
548 return 0;
549
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)))
553 return 0;
554
555 /* do _SDD if SATA */
556 if (acpi_sata) {
557 rc = ata_acpi_push_id(dev);
558 if (rc)
559 goto acpi_err;
560 }
561
562 /* do _GTF */
563 rc = ata_acpi_exec_tfs(dev);
564 if (rc < 0)
565 goto acpi_err;
566
567 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
568
569 /* refresh IDENTIFY page if any _GTF command has been executed */
570 if (rc > 0) {
571 rc = ata_dev_reread_id(dev, 0);
572 if (rc < 0) {
573 ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
574 "after ACPI commands\n");
575 return rc;
576 }
577 }
578
579 return 0;
580
581 acpi_err:
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);
586
587 dev->acpi_handle = NULL;
588
589 /* if port is working, request IDENTIFY reload and continue */
590 if (!(ap->pflags & ATA_PFLAG_FROZEN))
591 rc = 1;
592 }
593 dev->flags |= ATA_DFLAG_ACPI_FAILED;
594 return rc;
595}