]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/ata/libata-acpi.c
libata: add HDT722516DLA380 to NCQ blacklist
[mirror_ubuntu-focal-kernel.git] / drivers / ata / libata-acpi.c
CommitLineData
11ef697b
KCA
1/*
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>
237d8440 17#include <scsi/scsi_device.h>
11ef697b
KCA
18#include "libata.h"
19
20#include <acpi/acpi_bus.h>
21#include <acpi/acnames.h>
22#include <acpi/acnamesp.h>
23#include <acpi/acparser.h>
24#include <acpi/acexcep.h>
25#include <acpi/acmacros.h>
26#include <acpi/actypes.h>
27
11ef697b 28#define NO_PORT_MULT 0xffff
fafbae87 29#define SATA_ADR(root,pmp) (((root) << 16) | (pmp))
11ef697b
KCA
30
31#define REGS_PER_GTF 7
4700c4bc
TH
32struct ata_acpi_gtf {
33 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
34} __packed;
11ef697b 35
ca426635
AC
36/*
37 * Helper - belongs in the PCI layer somewhere eventually
38 */
39static int is_pci_dev(struct device *dev)
40{
41 return (dev->bus == &pci_bus_type);
42}
11ef697b 43
d0df8b5d
TH
44/**
45 * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
46 * @ap: target SATA port
47 *
48 * Look up ACPI objects associated with @ap and initialize acpi_handle
49 * fields of @ap, the port and devices accordingly.
50 *
51 * LOCKING:
52 * EH context.
53 *
54 * RETURNS:
55 * 0 on success, -errno on failure.
56 */
57void ata_acpi_associate_sata_port(struct ata_port *ap)
11ef697b 58{
d0df8b5d
TH
59 WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
60
61 if (!ap->nr_pmp_links) {
62 acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
63
64 ap->link.device->acpi_handle =
65 acpi_get_child(ap->host->acpi_handle, adr);
66 } else {
67 struct ata_link *link;
68
69 ap->link.device->acpi_handle = NULL;
70
71 ata_port_for_each_link(link, ap) {
72 acpi_integer adr = SATA_ADR(ap->port_no, link->pmp);
fafbae87 73
d0df8b5d
TH
74 link->device->acpi_handle =
75 acpi_get_child(ap->host->acpi_handle, adr);
76 }
77 }
11ef697b
KCA
78}
79
fafbae87 80static void ata_acpi_associate_ide_port(struct ata_port *ap)
11ef697b 81{
fafbae87 82 int max_devices, i;
11ef697b 83
fafbae87
TH
84 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
85 if (!ap->acpi_handle)
86 return;
11ef697b 87
fafbae87
TH
88 max_devices = 1;
89 if (ap->flags & ATA_FLAG_SLAVE_POSS)
90 max_devices++;
11ef697b 91
fafbae87 92 for (i = 0; i < max_devices; i++) {
9af5c9c9 93 struct ata_device *dev = &ap->link.device[i];
11ef697b 94
fafbae87 95 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
11ef697b 96 }
11ef697b
KCA
97}
98
237d8440
MG
99static void ata_acpi_handle_hotplug (struct ata_port *ap, struct kobject *kobj,
100 u32 event)
101{
102 char event_string[12];
103 char *envp[] = { event_string, NULL };
104 struct ata_eh_info *ehi = &ap->link.eh_info;
105
106 if (event == 0 || event == 1) {
107 unsigned long flags;
108 spin_lock_irqsave(ap->lock, flags);
109 ata_ehi_clear_desc(ehi);
110 ata_ehi_push_desc(ehi, "ACPI event");
111 ata_ehi_hotplugged(ehi);
112 ata_port_freeze(ap);
113 spin_unlock_irqrestore(ap->lock, flags);
114 }
115
116 if (kobj) {
117 sprintf(event_string, "BAY_EVENT=%d", event);
118 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
119 }
120}
121
122static void ata_acpi_dev_notify(acpi_handle handle, u32 event, void *data)
123{
124 struct ata_device *dev = data;
125 struct kobject *kobj = NULL;
126
127 if (dev->sdev)
128 kobj = &dev->sdev->sdev_gendev.kobj;
129
130 ata_acpi_handle_hotplug (dev->link->ap, kobj, event);
131}
132
133static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
134{
135 struct ata_port *ap = data;
136
137 ata_acpi_handle_hotplug (ap, &ap->dev->kobj, event);
138}
139
fafbae87
TH
140/**
141 * ata_acpi_associate - associate ATA host with ACPI objects
142 * @host: target ATA host
143 *
144 * Look up ACPI objects associated with @host and initialize
145 * acpi_handle fields of @host, its ports and devices accordingly.
146 *
147 * LOCKING:
148 * EH context.
149 *
150 * RETURNS:
151 * 0 on success, -errno on failure.
152 */
153void ata_acpi_associate(struct ata_host *host)
11ef697b 154{
237d8440 155 int i, j;
11ef697b 156
fafbae87
TH
157 if (!is_pci_dev(host->dev) || libata_noacpi)
158 return;
11ef697b 159
fafbae87
TH
160 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
161 if (!host->acpi_handle)
162 return;
11ef697b 163
fafbae87
TH
164 for (i = 0; i < host->n_ports; i++) {
165 struct ata_port *ap = host->ports[i];
166
167 if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
168 ata_acpi_associate_sata_port(ap);
169 else
170 ata_acpi_associate_ide_port(ap);
237d8440
MG
171
172 if (ap->acpi_handle)
173 acpi_install_notify_handler (ap->acpi_handle,
174 ACPI_SYSTEM_NOTIFY,
175 ata_acpi_ap_notify,
176 ap);
177
178 for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
179 struct ata_device *dev = &ap->link.device[j];
180
181 if (dev->acpi_handle)
182 acpi_install_notify_handler (dev->acpi_handle,
183 ACPI_SYSTEM_NOTIFY,
184 ata_acpi_dev_notify,
185 dev);
186 }
11ef697b 187 }
11ef697b
KCA
188}
189
64578a3d
TH
190/**
191 * ata_acpi_gtm - execute _GTM
192 * @ap: target ATA port
193 * @gtm: out parameter for _GTM result
194 *
195 * Evaluate _GTM and store the result in @gtm.
196 *
197 * LOCKING:
198 * EH context.
199 *
200 * RETURNS:
201 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
202 */
203static int ata_acpi_gtm(const struct ata_port *ap, struct ata_acpi_gtm *gtm)
204{
205 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
206 union acpi_object *out_obj;
207 acpi_status status;
208 int rc = 0;
209
210 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
211
212 rc = -ENOENT;
213 if (status == AE_NOT_FOUND)
214 goto out_free;
215
216 rc = -EINVAL;
217 if (ACPI_FAILURE(status)) {
218 ata_port_printk(ap, KERN_ERR,
219 "ACPI get timing mode failed (AE 0x%x)\n",
220 status);
221 goto out_free;
222 }
223
224 out_obj = output.pointer;
225 if (out_obj->type != ACPI_TYPE_BUFFER) {
226 ata_port_printk(ap, KERN_WARNING,
227 "_GTM returned unexpected object type 0x%x\n",
228 out_obj->type);
229
230 goto out_free;
231 }
232
233 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
234 ata_port_printk(ap, KERN_ERR,
235 "_GTM returned invalid length %d\n",
236 out_obj->buffer.length);
237 goto out_free;
238 }
239
240 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
241 rc = 0;
242 out_free:
243 kfree(output.pointer);
244 return rc;
245}
246
247/**
248 * ata_acpi_stm - execute _STM
249 * @ap: target ATA port
250 * @stm: timing parameter to _STM
251 *
252 * Evaluate _STM with timing parameter @stm.
253 *
254 * LOCKING:
255 * EH context.
256 *
257 * RETURNS:
258 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
259 */
260static int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm)
261{
262 acpi_status status;
263 struct acpi_object_list input;
264 union acpi_object in_params[3];
265
266 in_params[0].type = ACPI_TYPE_BUFFER;
267 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
268 in_params[0].buffer.pointer = (u8 *)stm;
269 /* Buffers for id may need byteswapping ? */
270 in_params[1].type = ACPI_TYPE_BUFFER;
271 in_params[1].buffer.length = 512;
9af5c9c9 272 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
64578a3d
TH
273 in_params[2].type = ACPI_TYPE_BUFFER;
274 in_params[2].buffer.length = 512;
9af5c9c9 275 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
64578a3d
TH
276
277 input.count = 3;
278 input.pointer = in_params;
279
280 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
281
282 if (status == AE_NOT_FOUND)
283 return -ENOENT;
284 if (ACPI_FAILURE(status)) {
285 ata_port_printk(ap, KERN_ERR,
286 "ACPI set timing mode failed (status=0x%x)\n", status);
287 return -EINVAL;
288 }
289 return 0;
290}
291
11ef697b 292/**
4700c4bc 293 * ata_dev_get_GTF - get the drive bootup default taskfile settings
3a32a8e9 294 * @dev: target ATA device
4700c4bc
TH
295 * @gtf: output parameter for buffer containing _GTF taskfile arrays
296 * @ptr_to_free: pointer which should be freed
11ef697b
KCA
297 *
298 * This applies to both PATA and SATA drives.
299 *
300 * The _GTF method has no input parameters.
301 * It returns a variable number of register set values (registers
302 * hex 1F1..1F7, taskfiles).
303 * The <variable number> is not known in advance, so have ACPI-CA
304 * allocate the buffer as needed and return it, then free it later.
305 *
4700c4bc
TH
306 * LOCKING:
307 * EH context.
308 *
309 * RETURNS:
310 * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
311 * contain valid data. -errno on other errors.
11ef697b 312 */
4700c4bc
TH
313static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
314 void **ptr_to_free)
11ef697b 315{
9af5c9c9 316 struct ata_port *ap = dev->link->ap;
3a32a8e9 317 acpi_status status;
3a32a8e9
TH
318 struct acpi_buffer output;
319 union acpi_object *out_obj;
4700c4bc 320 int rc = 0;
11ef697b 321
4700c4bc
TH
322 /* set up output buffer */
323 output.length = ACPI_ALLOCATE_BUFFER;
324 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
11ef697b 325
11ef697b 326 if (ata_msg_probe(ap))
3a32a8e9 327 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
878d4fed 328 __FUNCTION__, ap->port_no);
11ef697b 329
11ef697b 330 /* _GTF has no input parameters */
4700c4bc
TH
331 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
332
11ef697b 333 if (ACPI_FAILURE(status)) {
4700c4bc
TH
334 if (status != AE_NOT_FOUND) {
335 ata_dev_printk(dev, KERN_WARNING,
336 "_GTF evaluation failed (AE 0x%x)\n",
337 status);
338 rc = -EIO;
339 }
340 goto out_free;
11ef697b
KCA
341 }
342
343 if (!output.length || !output.pointer) {
344 if (ata_msg_probe(ap))
3a32a8e9 345 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
11ef697b
KCA
346 "length or ptr is NULL (0x%llx, 0x%p)\n",
347 __FUNCTION__,
348 (unsigned long long)output.length,
349 output.pointer);
4700c4bc 350 goto out_free;
11ef697b
KCA
351 }
352
353 out_obj = output.pointer;
354 if (out_obj->type != ACPI_TYPE_BUFFER) {
69b16a5f
TH
355 ata_dev_printk(dev, KERN_WARNING,
356 "_GTF unexpected object type 0x%x\n",
357 out_obj->type);
4700c4bc
TH
358 rc = -EINVAL;
359 goto out_free;
11ef697b
KCA
360 }
361
4700c4bc 362 if (out_obj->buffer.length % REGS_PER_GTF) {
69b16a5f
TH
363 ata_dev_printk(dev, KERN_WARNING,
364 "unexpected _GTF length (%d)\n",
365 out_obj->buffer.length);
4700c4bc
TH
366 rc = -EINVAL;
367 goto out_free;
11ef697b
KCA
368 }
369
4700c4bc
TH
370 *ptr_to_free = out_obj;
371 *gtf = (void *)out_obj->buffer.pointer;
372 rc = out_obj->buffer.length / REGS_PER_GTF;
373
11ef697b 374 if (ata_msg_probe(ap))
3a32a8e9 375 ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
4700c4bc
TH
376 "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
377 __FUNCTION__, *gtf, rc, *ptr_to_free);
378 return rc;
379
380 out_free:
381 kfree(output.pointer);
382 return rc;
11ef697b
KCA
383}
384
e1ddb4b6
AC
385/**
386 * ata_acpi_cbl_80wire - Check for 80 wire cable
387 * @ap: Port to check
388 *
389 * Return 1 if the ACPI mode data for this port indicates the BIOS selected
390 * an 80wire mode.
391 */
392
393int ata_acpi_cbl_80wire(struct ata_port *ap)
394{
395 struct ata_acpi_gtm gtm;
396 int valid = 0;
397
398 /* No _GTM data, no information */
399 if (ata_acpi_gtm(ap, &gtm) < 0)
400 return 0;
401
402 /* Split timing, DMA enabled */
403 if ((gtm.flags & 0x11) == 0x11 && gtm.drive[0].dma < 55)
404 valid |= 1;
405 if ((gtm.flags & 0x14) == 0x14 && gtm.drive[1].dma < 55)
406 valid |= 2;
407 /* Shared timing, DMA enabled */
408 if ((gtm.flags & 0x11) == 0x01 && gtm.drive[0].dma < 55)
409 valid |= 1;
410 if ((gtm.flags & 0x14) == 0x04 && gtm.drive[0].dma < 55)
411 valid |= 2;
412
413 /* Drive check */
414 if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
415 return 1;
416 if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
417 return 1;
418 return 0;
419}
420
421EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
422
11ef697b
KCA
423/**
424 * taskfile_load_raw - send taskfile registers to host controller
3a32a8e9 425 * @dev: target ATA device
11ef697b
KCA
426 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
427 *
428 * Outputs ATA taskfile to standard ATA host controller using MMIO
429 * or PIO as indicated by the ATA_FLAG_MMIO flag.
430 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
431 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
432 * hob_lbal, hob_lbam, and hob_lbah.
433 *
434 * This function waits for idle (!BUSY and !DRQ) after writing
435 * registers. If the control register has a new value, this
436 * function also waits for idle after writing control and before
437 * writing the remaining registers.
438 *
4700c4bc
TH
439 * LOCKING:
440 * EH context.
441 *
442 * RETURNS:
443 * 0 on success, -errno on failure.
11ef697b 444 */
4700c4bc
TH
445static int taskfile_load_raw(struct ata_device *dev,
446 const struct ata_acpi_gtf *gtf)
11ef697b 447{
9af5c9c9 448 struct ata_port *ap = dev->link->ap;
4700c4bc
TH
449 struct ata_taskfile tf, rtf;
450 unsigned int err_mask;
fc16c25f 451
4700c4bc
TH
452 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
453 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
454 && (gtf->tf[6] == 0))
455 return 0;
11ef697b 456
3a32a8e9 457 ata_tf_init(dev, &tf);
fc16c25f
JG
458
459 /* convert gtf to tf */
460 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
48be6b18 461 tf.protocol = ATA_PROT_NODATA;
4700c4bc
TH
462 tf.feature = gtf->tf[0]; /* 0x1f1 */
463 tf.nsect = gtf->tf[1]; /* 0x1f2 */
464 tf.lbal = gtf->tf[2]; /* 0x1f3 */
465 tf.lbam = gtf->tf[3]; /* 0x1f4 */
466 tf.lbah = gtf->tf[4]; /* 0x1f5 */
467 tf.device = gtf->tf[5]; /* 0x1f6 */
468 tf.command = gtf->tf[6]; /* 0x1f7 */
469
470 if (ata_msg_probe(ap))
471 ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
472 "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
473 tf.command, tf.feature, tf.nsect,
474 tf.lbal, tf.lbam, tf.lbah, tf.device);
475
476 rtf = tf;
477 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
478 if (err_mask) {
3a32a8e9 479 ata_dev_printk(dev, KERN_ERR,
4700c4bc
TH
480 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
481 "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
482 tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
483 tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
484 return -EIO;
485 }
486
487 return 0;
11ef697b
KCA
488}
489
11ef697b
KCA
490/**
491 * ata_acpi_exec_tfs - get then write drive taskfile settings
6746544c 492 * @dev: target ATA device
11ef697b 493 *
6746544c 494 * Evaluate _GTF and excute returned taskfiles.
69b16a5f
TH
495 *
496 * LOCKING:
497 * EH context.
498 *
499 * RETURNS:
6746544c
TH
500 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
501 * doesn't contain valid data. -errno on other errors.
11ef697b 502 */
6746544c 503static int ata_acpi_exec_tfs(struct ata_device *dev)
11ef697b 504{
6746544c
TH
505 struct ata_acpi_gtf *gtf = NULL;
506 void *ptr_to_free = NULL;
507 int gtf_count, i, rc;
508
509 /* get taskfiles */
510 rc = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
511 if (rc < 0)
512 return rc;
513 gtf_count = rc;
514
515 /* execute them */
516 for (i = 0, rc = 0; i < gtf_count; i++) {
517 int tmp;
518
519 /* ACPI errors are eventually ignored. Run till the
520 * end even after errors.
521 */
522 tmp = taskfile_load_raw(dev, gtf++);
523 if (!rc)
524 rc = tmp;
11ef697b
KCA
525 }
526
6746544c
TH
527 kfree(ptr_to_free);
528
529 if (rc == 0)
530 return gtf_count;
531 return rc;
11ef697b
KCA
532}
533
7ea1fbc2
KCA
534/**
535 * ata_acpi_push_id - send Identify data to drive
3a32a8e9 536 * @dev: target ATA device
7ea1fbc2
KCA
537 *
538 * _SDD ACPI object: for SATA mode only
539 * Must be after Identify (Packet) Device -- uses its data
540 * ATM this function never returns a failure. It is an optional
541 * method and if it fails for whatever reason, we should still
542 * just keep going.
69b16a5f
TH
543 *
544 * LOCKING:
545 * EH context.
546 *
547 * RETURNS:
548 * 0 on success, -errno on failure.
7ea1fbc2 549 */
6746544c 550static int ata_acpi_push_id(struct ata_device *dev)
7ea1fbc2 551{
9af5c9c9 552 struct ata_port *ap = dev->link->ap;
3a32a8e9 553 int err;
3a32a8e9
TH
554 acpi_status status;
555 struct acpi_object_list input;
556 union acpi_object in_params[1];
7ea1fbc2 557
7ea1fbc2 558 if (ata_msg_probe(ap))
3a32a8e9
TH
559 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
560 __FUNCTION__, dev->devno, ap->port_no);
7ea1fbc2 561
7ea1fbc2
KCA
562 /* Give the drive Identify data to the drive via the _SDD method */
563 /* _SDD: set up input parameters */
564 input.count = 1;
565 input.pointer = in_params;
566 in_params[0].type = ACPI_TYPE_BUFFER;
3a32a8e9
TH
567 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
568 in_params[0].buffer.pointer = (u8 *)dev->id;
7ea1fbc2
KCA
569 /* Output buffer: _SDD has no output */
570
571 /* It's OK for _SDD to be missing too. */
3a32a8e9 572 swap_buf_le16(dev->id, ATA_ID_WORDS);
fafbae87 573 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
3a32a8e9 574 swap_buf_le16(dev->id, ATA_ID_WORDS);
7ea1fbc2
KCA
575
576 err = ACPI_FAILURE(status) ? -EIO : 0;
69b16a5f
TH
577 if (err < 0)
578 ata_dev_printk(dev, KERN_WARNING,
579 "ACPI _SDD failed (AE 0x%x)\n", status);
7ea1fbc2 580
6746544c
TH
581 return err;
582}
583
64578a3d
TH
584/**
585 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
586 * @ap: target ATA port
587 *
588 * This function is called when @ap is about to be suspended. All
589 * devices are already put to sleep but the port_suspend() callback
590 * hasn't been executed yet. Error return from this function aborts
591 * suspend.
592 *
593 * LOCKING:
594 * EH context.
595 *
596 * RETURNS:
597 * 0 on success, -errno on failure.
598 */
599int ata_acpi_on_suspend(struct ata_port *ap)
600{
601 unsigned long flags;
602 int rc;
603
604 /* proceed iff per-port acpi_handle is valid */
605 if (!ap->acpi_handle)
606 return 0;
607 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
608
609 /* store timing parameters */
610 rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
611
612 spin_lock_irqsave(ap->lock, flags);
613 if (rc == 0)
614 ap->pflags |= ATA_PFLAG_GTM_VALID;
615 else
616 ap->pflags &= ~ATA_PFLAG_GTM_VALID;
617 spin_unlock_irqrestore(ap->lock, flags);
618
619 if (rc == -ENOENT)
620 rc = 0;
621 return rc;
622}
623
6746544c
TH
624/**
625 * ata_acpi_on_resume - ATA ACPI hook called on resume
626 * @ap: target ATA port
627 *
628 * This function is called when @ap is resumed - right after port
629 * itself is resumed but before any EH action is taken.
630 *
631 * LOCKING:
632 * EH context.
633 */
634void ata_acpi_on_resume(struct ata_port *ap)
635{
f58229f8 636 struct ata_device *dev;
6746544c 637
64578a3d
TH
638 if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
639 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
640
641 /* restore timing parameters */
642 ata_acpi_stm(ap, &ap->acpi_gtm);
643 }
644
6746544c 645 /* schedule _GTF */
f58229f8
TH
646 ata_link_for_each_dev(dev, &ap->link)
647 dev->flags |= ATA_DFLAG_ACPI_PENDING;
7ea1fbc2
KCA
648}
649
6746544c
TH
650/**
651 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
652 * @dev: target ATA device
653 *
654 * This function is called when @dev is about to be configured.
655 * IDENTIFY data might have been modified after this hook is run.
656 *
657 * LOCKING:
658 * EH context.
659 *
660 * RETURNS:
661 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
662 * -errno on failure.
663 */
664int ata_acpi_on_devcfg(struct ata_device *dev)
665{
9af5c9c9
TH
666 struct ata_port *ap = dev->link->ap;
667 struct ata_eh_context *ehc = &ap->link.eh_context;
6746544c
TH
668 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
669 int rc;
670
6746544c
TH
671 if (!dev->acpi_handle)
672 return 0;
673
674 /* do we need to do _GTF? */
675 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
676 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
677 return 0;
678
679 /* do _SDD if SATA */
680 if (acpi_sata) {
681 rc = ata_acpi_push_id(dev);
682 if (rc)
683 goto acpi_err;
684 }
685
686 /* do _GTF */
687 rc = ata_acpi_exec_tfs(dev);
688 if (rc < 0)
689 goto acpi_err;
690
691 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
692
693 /* refresh IDENTIFY page if any _GTF command has been executed */
694 if (rc > 0) {
695 rc = ata_dev_reread_id(dev, 0);
696 if (rc < 0) {
697 ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
698 "after ACPI commands\n");
699 return rc;
700 }
701 }
702
703 return 0;
704
705 acpi_err:
706 /* let EH retry on the first failure, disable ACPI on the second */
707 if (dev->flags & ATA_DFLAG_ACPI_FAILED) {
708 ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the "
709 "second time, disabling (errno=%d)\n", rc);
710
711 dev->acpi_handle = NULL;
712
713 /* if port is working, request IDENTIFY reload and continue */
714 if (!(ap->pflags & ATA_PFLAG_FROZEN))
715 rc = 1;
716 }
717 dev->flags |= ATA_DFLAG_ACPI_FAILED;
718 return rc;
719}