blob: c059f78ad944dc18a3372f63a49a14b4b0179441 [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 Heofafbae82007-05-15 03:28:16 +090047 ap->device->acpi_handle = acpi_get_child(ap->host->acpi_handle, adr);
48}
Alan Coxca426632007-03-08 23:13:50 +000049
Tejun Heofafbae82007-05-15 03:28:16 +090050static void ata_acpi_associate_ide_port(struct ata_port *ap)
51{
52 int max_devices, i;
53
54 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
55 if (!ap->acpi_handle)
56 return;
57
58 max_devices = 1;
59 if (ap->flags & ATA_FLAG_SLAVE_POSS)
60 max_devices++;
61
62 for (i = 0; i < max_devices; i++) {
63 struct ata_device *dev = &ap->device[i];
64
65 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
66 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070067}
68
69/**
Tejun Heofafbae82007-05-15 03:28:16 +090070 * ata_acpi_associate - associate ATA host with ACPI objects
71 * @host: target ATA host
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070072 *
Tejun Heofafbae82007-05-15 03:28:16 +090073 * Look up ACPI objects associated with @host and initialize
74 * acpi_handle fields of @host, its ports and devices accordingly.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070075 *
Tejun Heofafbae82007-05-15 03:28:16 +090076 * LOCKING:
77 * EH context.
78 *
79 * RETURNS:
80 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070081 */
Tejun Heofafbae82007-05-15 03:28:16 +090082void ata_acpi_associate(struct ata_host *host)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070083{
Tejun Heofafbae82007-05-15 03:28:16 +090084 int i;
Alan Coxca426632007-03-08 23:13:50 +000085
Tejun Heofafbae82007-05-15 03:28:16 +090086 if (!is_pci_dev(host->dev) || libata_noacpi)
87 return;
Alan Coxca426632007-03-08 23:13:50 +000088
Tejun Heofafbae82007-05-15 03:28:16 +090089 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
90 if (!host->acpi_handle)
91 return;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070092
Tejun Heofafbae82007-05-15 03:28:16 +090093 for (i = 0; i < host->n_ports; i++) {
94 struct ata_port *ap = host->ports[i];
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070095
Tejun Heofafbae82007-05-15 03:28:16 +090096 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);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700100 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700101}
102
103/**
Tejun Heo64578a32007-05-15 03:28:16 +0900104 * ata_acpi_gtm - execute _GTM
105 * @ap: target ATA port
106 * @gtm: out parameter for _GTM result
107 *
108 * Evaluate _GTM and store the result in @gtm.
109 *
110 * LOCKING:
111 * EH context.
112 *
113 * RETURNS:
114 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
115 */
116static int ata_acpi_gtm(const struct ata_port *ap, struct ata_acpi_gtm *gtm)
117{
118 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
119 union acpi_object *out_obj;
120 acpi_status status;
121 int rc = 0;
122
123 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
124
125 rc = -ENOENT;
126 if (status == AE_NOT_FOUND)
127 goto out_free;
128
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;
135 }
136
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);
142
143 goto out_free;
144 }
145
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;
151 }
152
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;
158}
159
160/**
161 * ata_acpi_stm - execute _STM
162 * @ap: target ATA port
163 * @stm: timing parameter to _STM
164 *
165 * Evaluate _STM with timing parameter @stm.
166 *
167 * LOCKING:
168 * EH context.
169 *
170 * RETURNS:
171 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
172 */
173static int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm)
174{
175 acpi_status status;
176 struct acpi_object_list input;
177 union acpi_object in_params[3];
178
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;
189
190 input.count = 3;
191 input.pointer = in_params;
192
193 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
194
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;
201 }
202 return 0;
203}
204
205/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900206 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900207 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900208 * @gtf: output parameter for buffer containing _GTF taskfile arrays
209 * @ptr_to_free: pointer which should be freed
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700210 *
211 * This applies to both PATA and SATA drives.
212 *
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.
218 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900219 * LOCKING:
220 * EH context.
221 *
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.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700225 */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900226static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
227 void **ptr_to_free)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700228{
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900229 struct ata_port *ap = dev->ap;
230 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900231 struct acpi_buffer output;
232 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900233 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700234
Tejun Heo4700c4b2007-05-15 03:28:16 +0900235 /* set up output buffer */
236 output.length = ACPI_ALLOCATE_BUFFER;
237 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700238
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700239 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900240 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
Tejun Heo878d4fe2007-02-21 16:36:33 +0900241 __FUNCTION__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700242
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700243 /* _GTF has no input parameters */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900244 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
245
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700246 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900247 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;
252 }
253 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700254 }
255
256 if (!output.length || !output.pointer) {
257 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900258 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700259 "length or ptr is NULL (0x%llx, 0x%p)\n",
260 __FUNCTION__,
261 (unsigned long long)output.length,
262 output.pointer);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900263 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700264 }
265
266 out_obj = output.pointer;
267 if (out_obj->type != ACPI_TYPE_BUFFER) {
Tejun Heo69b16a52007-05-15 03:28:16 +0900268 ata_dev_printk(dev, KERN_WARNING,
269 "_GTF unexpected object type 0x%x\n",
270 out_obj->type);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900271 rc = -EINVAL;
272 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700273 }
274
Tejun Heo4700c4b2007-05-15 03:28:16 +0900275 if (out_obj->buffer.length % REGS_PER_GTF) {
Tejun Heo69b16a52007-05-15 03:28:16 +0900276 ata_dev_printk(dev, KERN_WARNING,
277 "unexpected _GTF length (%d)\n",
278 out_obj->buffer.length);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900279 rc = -EINVAL;
280 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700281 }
282
Tejun Heo4700c4b2007-05-15 03:28:16 +0900283 *ptr_to_free = out_obj;
284 *gtf = (void *)out_obj->buffer.pointer;
285 rc = out_obj->buffer.length / REGS_PER_GTF;
286
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700287 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900288 ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
Tejun Heo4700c4b2007-05-15 03:28:16 +0900289 "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
290 __FUNCTION__, *gtf, rc, *ptr_to_free);
291 return rc;
292
293 out_free:
294 kfree(output.pointer);
295 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700296}
297
298/**
299 * taskfile_load_raw - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900300 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700301 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
302 *
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.
308 *
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.
313 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900314 * LOCKING:
315 * EH context.
316 *
317 * RETURNS:
318 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700319 */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900320static int taskfile_load_raw(struct ata_device *dev,
321 const struct ata_acpi_gtf *gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700322{
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900323 struct ata_port *ap = dev->ap;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900324 struct ata_taskfile tf, rtf;
325 unsigned int err_mask;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500326
Tejun Heo4700c4b2007-05-15 03:28:16 +0900327 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;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700331
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900332 ata_tf_init(dev, &tf);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700333
Jeff Garzikfc16c252007-02-24 21:05:01 -0500334 /* convert gtf to tf */
335 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
Tejun Heo48be6b12007-04-23 02:06:46 +0900336 tf.protocol = ATA_PROT_NODATA;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900337 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 */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700344
Tejun Heo4700c4b2007-05-15 03:28:16 +0900345 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);
350
351 rtf = tf;
352 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
353 if (err_mask) {
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900354 ata_dev_printk(dev, KERN_ERR,
Tejun Heo4700c4b2007-05-15 03:28:16 +0900355 "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;
360 }
361
362 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700363}
364
365/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700366 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900367 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700368 *
Tejun Heo67465442007-05-15 03:28:16 +0900369 * Evaluate _GTF and excute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900370 *
371 * LOCKING:
372 * EH context.
373 *
374 * RETURNS:
Tejun Heo67465442007-05-15 03:28:16 +0900375 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
376 * doesn't contain valid data. -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700377 */
Tejun Heo67465442007-05-15 03:28:16 +0900378static int ata_acpi_exec_tfs(struct ata_device *dev)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700379{
Tejun Heo67465442007-05-15 03:28:16 +0900380 struct ata_acpi_gtf *gtf = NULL;
381 void *ptr_to_free = NULL;
382 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700383
Tejun Heo67465442007-05-15 03:28:16 +0900384 /* get taskfiles */
385 rc = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
386 if (rc < 0)
387 return rc;
388 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700389
Tejun Heo67465442007-05-15 03:28:16 +0900390 /* execute them */
391 for (i = 0, rc = 0; i < gtf_count; i++) {
392 int tmp;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900393
Tejun Heo67465442007-05-15 03:28:16 +0900394 /* ACPI errors are eventually ignored. Run till the
395 * end even after errors.
396 */
397 tmp = taskfile_load_raw(dev, gtf++);
398 if (!rc)
399 rc = tmp;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700400 }
401
Tejun Heo67465442007-05-15 03:28:16 +0900402 kfree(ptr_to_free);
403
404 if (rc == 0)
405 return gtf_count;
406 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700407}
408
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700409/**
410 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900411 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700412 *
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.
Tejun Heo69b16a52007-05-15 03:28:16 +0900418 *
419 * LOCKING:
420 * EH context.
421 *
422 * RETURNS:
423 * 0 on success, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700424 */
Tejun Heo67465442007-05-15 03:28:16 +0900425static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700426{
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900427 struct ata_port *ap = dev->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900428 int err;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900429 acpi_status status;
430 struct acpi_object_list input;
431 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700432
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700433 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900434 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
435 __FUNCTION__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700436
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700437 /* 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;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900442 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
443 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700444 /* Output buffer: _SDD has no output */
445
446 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900447 swap_buf_le16(dev->id, ATA_ID_WORDS);
Tejun Heofafbae82007-05-15 03:28:16 +0900448 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900449 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700450
451 err = ACPI_FAILURE(status) ? -EIO : 0;
Tejun Heo69b16a52007-05-15 03:28:16 +0900452 if (err < 0)
453 ata_dev_printk(dev, KERN_WARNING,
454 "ACPI _SDD failed (AE 0x%x)\n", status);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700455
Tejun Heo67465442007-05-15 03:28:16 +0900456 return err;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700457}
458
Tejun Heo67465442007-05-15 03:28:16 +0900459/**
Tejun Heo64578a32007-05-15 03:28:16 +0900460 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
461 * @ap: target ATA port
462 *
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.
467 *
468 * LOCKING:
469 * EH context.
470 *
471 * RETURNS:
472 * 0 on success, -errno on failure.
473 */
474int ata_acpi_on_suspend(struct ata_port *ap)
475{
476 unsigned long flags;
477 int rc;
478
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);
483
484 /* store timing parameters */
485 rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
486
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);
493
494 if (rc == -ENOENT)
495 rc = 0;
496 return rc;
497}
498
499/**
Tejun Heo67465442007-05-15 03:28:16 +0900500 * ata_acpi_on_resume - ATA ACPI hook called on resume
501 * @ap: target ATA port
502 *
503 * This function is called when @ap is resumed - right after port
504 * itself is resumed but before any EH action is taken.
505 *
506 * LOCKING:
507 * EH context.
508 */
509void ata_acpi_on_resume(struct ata_port *ap)
510{
511 int i;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700512
Tejun Heo64578a32007-05-15 03:28:16 +0900513 if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
514 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
515
516 /* restore timing parameters */
517 ata_acpi_stm(ap, &ap->acpi_gtm);
518 }
519
Tejun Heo67465442007-05-15 03:28:16 +0900520 /* schedule _GTF */
521 for (i = 0; i < ATA_MAX_DEVICES; i++)
522 ap->device[i].flags |= ATA_DFLAG_ACPI_PENDING;
523}
524
525/**
526 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
527 * @dev: target ATA device
528 *
529 * This function is called when @dev is about to be configured.
530 * IDENTIFY data might have been modified after this hook is run.
531 *
532 * LOCKING:
533 * EH context.
534 *
535 * RETURNS:
536 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
537 * -errno on failure.
538 */
539int ata_acpi_on_devcfg(struct ata_device *dev)
540{
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;
545
Tejun Heo67465442007-05-15 03:28:16 +0900546 if (!dev->acpi_handle)
547 return 0;
548
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;
553
554 /* do _SDD if SATA */
555 if (acpi_sata) {
556 rc = ata_acpi_push_id(dev);
557 if (rc)
558 goto acpi_err;
559 }
560
561 /* do _GTF */
562 rc = ata_acpi_exec_tfs(dev);
563 if (rc < 0)
564 goto acpi_err;
565
566 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
567
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;
575 }
576 }
577
578 return 0;
579
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);
585
586 dev->acpi_handle = NULL;
587
588 /* if port is working, request IDENTIFY reload and continue */
589 if (!(ap->pflags & ATA_PFLAG_FROZEN))
590 rc = 1;
591 }
592 dev->flags |= ATA_DFLAG_ACPI_FAILED;
593 return rc;
594}