]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/ata/libata-acpi.c
Merge tag 'fixes-for-3.13a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23...
[mirror_ubuntu-bionic-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
3264a8d8 9#include <linux/module.h>
11ef697b
KCA
10#include <linux/ata.h>
11#include <linux/delay.h>
12#include <linux/device.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/acpi.h>
16#include <linux/libata.h>
17#include <linux/pci.h>
5a0e3ad6 18#include <linux/slab.h>
3bd46600 19#include <linux/pm_runtime.h>
237d8440 20#include <scsi/scsi_device.h>
11ef697b
KCA
21#include "libata.h"
22
23#include <acpi/acpi_bus.h>
11ef697b 24
110f66d2 25unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
3264a8d8 26module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
fa5b561c 27MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock, 0x4=DIPM, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)");
3264a8d8 28
11ef697b 29#define NO_PORT_MULT 0xffff
2dcb407e 30#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
11ef697b
KCA
31
32#define REGS_PER_GTF 7
4700c4bc
TH
33struct ata_acpi_gtf {
34 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
35} __packed;
11ef697b 36
398e0782
TH
37static void ata_acpi_clear_gtf(struct ata_device *dev)
38{
39 kfree(dev->gtf_cache);
40 dev->gtf_cache = NULL;
41}
42
30dcf76a
MG
43/**
44 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
f1bc1e4c 45 * @dev: the acpi_handle returned will correspond to this device
30dcf76a
MG
46 *
47 * Returns the acpi_handle for the ACPI namespace object corresponding to
48 * the ata_device passed into the function, or NULL if no such object exists
f1bc1e4c 49 * or ACPI is disabled for this device due to consecutive errors.
30dcf76a
MG
50 */
51acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
6b66d958 52{
f1bc1e4c
AL
53 return dev->flags & ATA_DFLAG_ACPI_DISABLED ?
54 NULL : ACPI_HANDLE(&dev->tdev);
11ef697b
KCA
55}
56
664d080c
HM
57/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
58static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
59{
60 if (dev)
61 dev->flags |= ATA_DFLAG_DETACH;
62 else {
63 struct ata_link *tlink;
64 struct ata_device *tdev;
65
1eca4365
TH
66 ata_for_each_link(tlink, ap, EDGE)
67 ata_for_each_dev(tdev, tlink, ALL)
664d080c
HM
68 tdev->flags |= ATA_DFLAG_DETACH;
69 }
70
71 ata_port_schedule_eh(ap);
72}
73
74/**
75 * ata_acpi_handle_hotplug - ACPI event handler backend
76 * @ap: ATA port ACPI event occurred
77 * @dev: ATA device ACPI event occurred (can be NULL)
78 * @event: ACPI event which occurred
664d080c
HM
79 *
80 * All ACPI bay / device realted events end up in this function. If
81 * the event is port-wide @dev is NULL. If the event is specific to a
82 * device, @dev points to it.
83 *
84 * Hotplug (as opposed to unplug) notification is always handled as
85 * port-wide while unplug only kills the target device on device-wide
86 * event.
87 *
88 * LOCKING:
89 * ACPI notify handler context. May sleep.
90 */
91static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
f730ae18 92 u32 event)
237d8440 93{
664d080c 94 struct ata_eh_info *ehi = &ap->link.eh_info;
233f1120
TH
95 int wait = 0;
96 unsigned long flags;
3c1e3896 97
664d080c 98 spin_lock_irqsave(ap->lock, flags);
f730ae18
SL
99 /*
100 * When dock driver calls into the routine, it will always use
101 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
102 * ACPI_NOTIFY_EJECT_REQUEST for remove
103 */
233f1120
TH
104 switch (event) {
105 case ACPI_NOTIFY_BUS_CHECK:
106 case ACPI_NOTIFY_DEVICE_CHECK:
107 ata_ehi_push_desc(ehi, "ACPI event");
664d080c 108
f730ae18
SL
109 ata_ehi_hotplugged(ehi);
110 ata_port_freeze(ap);
664d080c
HM
111 break;
112 case ACPI_NOTIFY_EJECT_REQUEST:
113 ata_ehi_push_desc(ehi, "ACPI event");
114
664d080c
HM
115 ata_acpi_detach_device(ap, dev);
116 wait = 1;
117 break;
237d8440
MG
118 }
119
ae6c23c4
MG
120 spin_unlock_irqrestore(ap->lock, flags);
121
f1bc1e4c 122 if (wait)
ae6c23c4 123 ata_port_wait_eh(ap);
664d080c
HM
124}
125
126static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
127{
128 struct ata_device *dev = data;
129
f730ae18 130 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
664d080c
HM
131}
132
133static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
134{
135 struct ata_port *ap = data;
136
f730ae18 137 ata_acpi_handle_hotplug(ap, NULL, event);
237d8440
MG
138}
139
1253f7aa
SL
140static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
141 u32 event)
142{
143 struct kobject *kobj = NULL;
144 char event_string[20];
145 char *envp[] = { event_string, NULL };
146
147 if (dev) {
148 if (dev->sdev)
149 kobj = &dev->sdev->sdev_gendev.kobj;
150 } else
151 kobj = &ap->dev->kobj;
152
153 if (kobj) {
154 snprintf(event_string, 20, "BAY_EVENT=%d", event);
155 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
156 }
157}
158
159static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
160{
161 ata_acpi_uevent(data, NULL, event);
162}
163
164static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
165{
166 struct ata_device *dev = data;
167 ata_acpi_uevent(dev->link->ap, dev, event);
168}
169
9c8b04be 170static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
1253f7aa
SL
171 .handler = ata_acpi_dev_notify_dock,
172 .uevent = ata_acpi_dev_uevent,
173};
174
9c8b04be 175static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
1253f7aa
SL
176 .handler = ata_acpi_ap_notify_dock,
177 .uevent = ata_acpi_ap_uevent,
178};
179
f1bc1e4c
AL
180/* bind acpi handle to pata port */
181void ata_acpi_bind_port(struct ata_port *ap)
44521527 182{
f1bc1e4c 183 acpi_handle host_handle = ACPI_HANDLE(ap->host->dev);
44521527 184
f1bc1e4c
AL
185 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_handle)
186 return;
44521527 187
7b199811 188 acpi_preset_companion(&ap->tdev, host_handle, ap->port_no);
44521527 189
f1bc1e4c
AL
190 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
191 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
44521527 192
f1bc1e4c
AL
193 /* we might be on a docking station */
194 register_hotplug_dock_device(ACPI_HANDLE(&ap->tdev),
195 &ata_acpi_ap_dock_ops, ap, NULL, NULL);
196}
44521527 197
f1bc1e4c
AL
198void ata_acpi_bind_dev(struct ata_device *dev)
199{
200 struct ata_port *ap = dev->link->ap;
201 acpi_handle port_handle = ACPI_HANDLE(&ap->tdev);
202 acpi_handle host_handle = ACPI_HANDLE(ap->host->dev);
203 acpi_handle parent_handle;
204 u64 adr;
205
206 /*
207 * For both sata/pata devices, host handle is required.
208 * For pata device, port handle is also required.
209 */
210 if (libata_noacpi || !host_handle ||
211 (!(ap->flags & ATA_FLAG_ACPI_SATA) && !port_handle))
212 return;
213
214 if (ap->flags & ATA_FLAG_ACPI_SATA) {
215 if (!sata_pmp_attached(ap))
216 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
217 else
218 adr = SATA_ADR(ap->port_no, dev->link->pmp);
219 parent_handle = host_handle;
220 } else {
221 adr = dev->devno;
222 parent_handle = port_handle;
44521527 223 }
f1bc1e4c 224
7b199811 225 acpi_preset_companion(&dev->tdev, parent_handle, adr);
f1bc1e4c
AL
226
227 register_hotplug_dock_device(ata_dev_acpi_handle(dev),
228 &ata_acpi_dev_dock_ops, dev, NULL, NULL);
44521527
AL
229}
230
562f0c2d
TH
231/**
232 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
233 * @host: target ATA host
234 *
235 * This function is called during driver detach after the whole host
236 * is shut down.
237 *
238 * LOCKING:
239 * EH context.
240 */
241void ata_acpi_dissociate(struct ata_host *host)
242{
c05e6ff0
TH
243 int i;
244
245 /* Restore initial _GTM values so that driver which attaches
246 * afterward can use them too.
247 */
248 for (i = 0; i < host->n_ports; i++) {
249 struct ata_port *ap = host->ports[i];
250 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
251
f1bc1e4c 252 if (ACPI_HANDLE(&ap->tdev) && gtm)
c05e6ff0
TH
253 ata_acpi_stm(ap, gtm);
254 }
562f0c2d
TH
255}
256
f1bc1e4c
AL
257/**
258 * ata_acpi_gtm - execute _GTM
259 * @ap: target ATA port
260 * @gtm: out parameter for _GTM result
261 *
262 * Evaluate _GTM and store the result in @gtm.
263 *
264 * LOCKING:
265 * EH context.
266 *
267 * RETURNS:
268 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
269 */
270int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
64578a3d
TH
271{
272 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
273 union acpi_object *out_obj;
274 acpi_status status;
275 int rc = 0;
f1bc1e4c
AL
276 acpi_handle handle = ACPI_HANDLE(&ap->tdev);
277
278 if (!handle)
279 return -EINVAL;
64578a3d 280
d66af4df 281 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
64578a3d
TH
282
283 rc = -ENOENT;
284 if (status == AE_NOT_FOUND)
285 goto out_free;
286
287 rc = -EINVAL;
288 if (ACPI_FAILURE(status)) {
a9a79dfe
JP
289 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
290 status);
64578a3d
TH
291 goto out_free;
292 }
293
294 out_obj = output.pointer;
295 if (out_obj->type != ACPI_TYPE_BUFFER) {
a9a79dfe
JP
296 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
297 out_obj->type);
64578a3d
TH
298
299 goto out_free;
300 }
301
302 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
a9a79dfe
JP
303 ata_port_err(ap, "_GTM returned invalid length %d\n",
304 out_obj->buffer.length);
64578a3d
TH
305 goto out_free;
306 }
307
308 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
309 rc = 0;
310 out_free:
311 kfree(output.pointer);
312 return rc;
313}
314
badff03d
AC
315EXPORT_SYMBOL_GPL(ata_acpi_gtm);
316
64578a3d
TH
317/**
318 * ata_acpi_stm - execute _STM
319 * @ap: target ATA port
320 * @stm: timing parameter to _STM
321 *
322 * Evaluate _STM with timing parameter @stm.
323 *
324 * LOCKING:
325 * EH context.
326 *
327 * RETURNS:
328 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
329 */
0d02f0b2 330int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
64578a3d
TH
331{
332 acpi_status status;
0d02f0b2 333 struct ata_acpi_gtm stm_buf = *stm;
64578a3d
TH
334 struct acpi_object_list input;
335 union acpi_object in_params[3];
336
337 in_params[0].type = ACPI_TYPE_BUFFER;
338 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
0d02f0b2 339 in_params[0].buffer.pointer = (u8 *)&stm_buf;
64578a3d
TH
340 /* Buffers for id may need byteswapping ? */
341 in_params[1].type = ACPI_TYPE_BUFFER;
342 in_params[1].buffer.length = 512;
9af5c9c9 343 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
64578a3d
TH
344 in_params[2].type = ACPI_TYPE_BUFFER;
345 in_params[2].buffer.length = 512;
9af5c9c9 346 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
64578a3d
TH
347
348 input.count = 3;
349 input.pointer = in_params;
350
f1bc1e4c
AL
351 status = acpi_evaluate_object(ACPI_HANDLE(&ap->tdev), "_STM",
352 &input, NULL);
64578a3d
TH
353
354 if (status == AE_NOT_FOUND)
355 return -ENOENT;
356 if (ACPI_FAILURE(status)) {
a9a79dfe
JP
357 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
358 status);
64578a3d
TH
359 return -EINVAL;
360 }
361 return 0;
362}
363
badff03d
AC
364EXPORT_SYMBOL_GPL(ata_acpi_stm);
365
11ef697b 366/**
4700c4bc 367 * ata_dev_get_GTF - get the drive bootup default taskfile settings
3a32a8e9 368 * @dev: target ATA device
4700c4bc 369 * @gtf: output parameter for buffer containing _GTF taskfile arrays
11ef697b
KCA
370 *
371 * This applies to both PATA and SATA drives.
372 *
373 * The _GTF method has no input parameters.
374 * It returns a variable number of register set values (registers
375 * hex 1F1..1F7, taskfiles).
376 * The <variable number> is not known in advance, so have ACPI-CA
377 * allocate the buffer as needed and return it, then free it later.
378 *
4700c4bc
TH
379 * LOCKING:
380 * EH context.
381 *
382 * RETURNS:
66fa7f21
TH
383 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
384 * if _GTF is invalid.
11ef697b 385 */
398e0782 386static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
11ef697b 387{
9af5c9c9 388 struct ata_port *ap = dev->link->ap;
3a32a8e9 389 acpi_status status;
3a32a8e9
TH
390 struct acpi_buffer output;
391 union acpi_object *out_obj;
4700c4bc 392 int rc = 0;
11ef697b 393
398e0782
TH
394 /* if _GTF is cached, use the cached value */
395 if (dev->gtf_cache) {
396 out_obj = dev->gtf_cache;
397 goto done;
398 }
399
4700c4bc
TH
400 /* set up output buffer */
401 output.length = ACPI_ALLOCATE_BUFFER;
402 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
11ef697b 403
11ef697b 404 if (ata_msg_probe(ap))
a9a79dfe
JP
405 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
406 __func__, ap->port_no);
11ef697b 407
11ef697b 408 /* _GTF has no input parameters */
30dcf76a
MG
409 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
410 &output);
398e0782 411 out_obj = dev->gtf_cache = output.pointer;
4700c4bc 412
11ef697b 413 if (ACPI_FAILURE(status)) {
4700c4bc 414 if (status != AE_NOT_FOUND) {
a9a79dfe
JP
415 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
416 status);
66fa7f21 417 rc = -EINVAL;
4700c4bc
TH
418 }
419 goto out_free;
11ef697b
KCA
420 }
421
422 if (!output.length || !output.pointer) {
423 if (ata_msg_probe(ap))
a9a79dfe
JP
424 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
425 __func__,
426 (unsigned long long)output.length,
427 output.pointer);
66fa7f21 428 rc = -EINVAL;
4700c4bc 429 goto out_free;
11ef697b
KCA
430 }
431
11ef697b 432 if (out_obj->type != ACPI_TYPE_BUFFER) {
a9a79dfe
JP
433 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
434 out_obj->type);
66fa7f21 435 rc = -EINVAL;
4700c4bc 436 goto out_free;
11ef697b
KCA
437 }
438
4700c4bc 439 if (out_obj->buffer.length % REGS_PER_GTF) {
a9a79dfe
JP
440 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
441 out_obj->buffer.length);
66fa7f21 442 rc = -EINVAL;
4700c4bc 443 goto out_free;
11ef697b
KCA
444 }
445
398e0782 446 done:
4700c4bc 447 rc = out_obj->buffer.length / REGS_PER_GTF;
398e0782
TH
448 if (gtf) {
449 *gtf = (void *)out_obj->buffer.pointer;
450 if (ata_msg_probe(ap))
a9a79dfe
JP
451 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
452 __func__, *gtf, rc);
398e0782 453 }
4700c4bc
TH
454 return rc;
455
456 out_free:
398e0782 457 ata_acpi_clear_gtf(dev);
4700c4bc 458 return rc;
11ef697b
KCA
459}
460
7c77fa4d
TH
461/**
462 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
463 * @dev: target device
464 * @gtm: GTM parameter to use
465 *
466 * Determine xfermask for @dev from @gtm.
467 *
468 * LOCKING:
469 * None.
470 *
471 * RETURNS:
472 * Determined xfermask.
473 */
474unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
475 const struct ata_acpi_gtm *gtm)
476{
a0f79b92
TH
477 unsigned long xfer_mask = 0;
478 unsigned int type;
479 int unit;
480 u8 mode;
7c77fa4d
TH
481
482 /* we always use the 0 slot for crap hardware */
483 unit = dev->devno;
484 if (!(gtm->flags & 0x10))
485 unit = 0;
486
a0f79b92
TH
487 /* PIO */
488 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
489 xfer_mask |= ata_xfer_mode2mask(mode);
7c77fa4d
TH
490
491 /* See if we have MWDMA or UDMA data. We don't bother with
492 * MWDMA if UDMA is available as this means the BIOS set UDMA
493 * and our error changedown if it works is UDMA to PIO anyway.
494 */
a0f79b92
TH
495 if (!(gtm->flags & (1 << (2 * unit))))
496 type = ATA_SHIFT_MWDMA;
497 else
498 type = ATA_SHIFT_UDMA;
499
500 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
501 xfer_mask |= ata_xfer_mode2mask(mode);
7c77fa4d 502
a0f79b92 503 return xfer_mask;
7c77fa4d
TH
504}
505EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
506
e1ddb4b6
AC
507/**
508 * ata_acpi_cbl_80wire - Check for 80 wire cable
509 * @ap: Port to check
021ee9a6 510 * @gtm: GTM data to use
e1ddb4b6 511 *
021ee9a6 512 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
e1ddb4b6 513 */
021ee9a6 514int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
e1ddb4b6 515{
021ee9a6
TH
516 struct ata_device *dev;
517
1eca4365 518 ata_for_each_dev(dev, &ap->link, ENABLED) {
021ee9a6
TH
519 unsigned long xfer_mask, udma_mask;
520
021ee9a6
TH
521 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
522 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
523
524 if (udma_mask & ~ATA_UDMA_MASK_40C)
525 return 1;
526 }
527
e1ddb4b6
AC
528 return 0;
529}
e1ddb4b6
AC
530EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
531
3264a8d8
TH
532static void ata_acpi_gtf_to_tf(struct ata_device *dev,
533 const struct ata_acpi_gtf *gtf,
534 struct ata_taskfile *tf)
535{
536 ata_tf_init(dev, tf);
537
538 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
539 tf->protocol = ATA_PROT_NODATA;
540 tf->feature = gtf->tf[0]; /* 0x1f1 */
541 tf->nsect = gtf->tf[1]; /* 0x1f2 */
542 tf->lbal = gtf->tf[2]; /* 0x1f3 */
543 tf->lbam = gtf->tf[3]; /* 0x1f4 */
544 tf->lbah = gtf->tf[4]; /* 0x1f5 */
545 tf->device = gtf->tf[5]; /* 0x1f6 */
546 tf->command = gtf->tf[6]; /* 0x1f7 */
547}
548
110f66d2
TH
549static int ata_acpi_filter_tf(struct ata_device *dev,
550 const struct ata_taskfile *tf,
3264a8d8
TH
551 const struct ata_taskfile *ptf)
552{
110f66d2 553 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
3264a8d8
TH
554 /* libata doesn't use ACPI to configure transfer mode.
555 * It will only confuse device configuration. Skip.
556 */
557 if (tf->command == ATA_CMD_SET_FEATURES &&
558 tf->feature == SETFEATURES_XFER)
559 return 1;
560 }
561
110f66d2 562 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
3264a8d8
TH
563 /* BIOS writers, sorry but we don't wanna lock
564 * features unless the user explicitly said so.
565 */
566
567 /* DEVICE CONFIGURATION FREEZE LOCK */
568 if (tf->command == ATA_CMD_CONF_OVERLAY &&
569 tf->feature == ATA_DCO_FREEZE_LOCK)
570 return 1;
571
572 /* SECURITY FREEZE LOCK */
573 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
574 return 1;
575
576 /* SET MAX LOCK and SET MAX FREEZE LOCK */
577 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
578 tf->command == ATA_CMD_SET_MAX &&
579 (tf->feature == ATA_SET_MAX_LOCK ||
580 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
581 return 1;
582 }
583
fa5b561c
TH
584 if (tf->command == ATA_CMD_SET_FEATURES &&
585 tf->feature == SETFEATURES_SATA_ENABLE) {
b344991a 586 /* inhibit enabling DIPM */
110f66d2 587 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
b344991a
TH
588 tf->nsect == SATA_DIPM)
589 return 1;
fa5b561c
TH
590
591 /* inhibit FPDMA non-zero offset */
110f66d2 592 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
fa5b561c
TH
593 (tf->nsect == SATA_FPDMA_OFFSET ||
594 tf->nsect == SATA_FPDMA_IN_ORDER))
595 return 1;
596
597 /* inhibit FPDMA auto activation */
110f66d2 598 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
fa5b561c
TH
599 tf->nsect == SATA_FPDMA_AA)
600 return 1;
b344991a
TH
601 }
602
3264a8d8
TH
603 return 0;
604}
605
11ef697b 606/**
0e8634bf 607 * ata_acpi_run_tf - send taskfile registers to host controller
3a32a8e9 608 * @dev: target ATA device
11ef697b
KCA
609 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
610 *
3696df30 611 * Outputs ATA taskfile to standard ATA host controller.
11ef697b
KCA
612 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
613 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
614 * hob_lbal, hob_lbam, and hob_lbah.
615 *
616 * This function waits for idle (!BUSY and !DRQ) after writing
617 * registers. If the control register has a new value, this
618 * function also waits for idle after writing control and before
619 * writing the remaining registers.
620 *
4700c4bc
TH
621 * LOCKING:
622 * EH context.
623 *
624 * RETURNS:
3264a8d8
TH
625 * 1 if command is executed successfully. 0 if ignored, rejected or
626 * filtered out, -errno on other errors.
11ef697b 627 */
0e8634bf 628static int ata_acpi_run_tf(struct ata_device *dev,
3264a8d8
TH
629 const struct ata_acpi_gtf *gtf,
630 const struct ata_acpi_gtf *prev_gtf)
11ef697b 631{
3264a8d8
TH
632 struct ata_taskfile *pptf = NULL;
633 struct ata_taskfile tf, ptf, rtf;
4700c4bc 634 unsigned int err_mask;
0e8634bf 635 const char *level;
6521148c 636 const char *descr;
0e8634bf
TH
637 char msg[60];
638 int rc;
fc16c25f 639
4700c4bc
TH
640 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
641 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
642 && (gtf->tf[6] == 0))
643 return 0;
11ef697b 644
3264a8d8
TH
645 ata_acpi_gtf_to_tf(dev, gtf, &tf);
646 if (prev_gtf) {
647 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
648 pptf = &ptf;
649 }
650
110f66d2 651 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
3264a8d8
TH
652 rtf = tf;
653 err_mask = ata_exec_internal(dev, &rtf, NULL,
654 DMA_NONE, NULL, 0, 0);
655
656 switch (err_mask) {
657 case 0:
658 level = KERN_DEBUG;
659 snprintf(msg, sizeof(msg), "succeeded");
660 rc = 1;
661 break;
662
663 case AC_ERR_DEV:
664 level = KERN_INFO;
665 snprintf(msg, sizeof(msg),
666 "rejected by device (Stat=0x%02x Err=0x%02x)",
667 rtf.command, rtf.feature);
668 rc = 0;
669 break;
670
671 default:
672 level = KERN_ERR;
673 snprintf(msg, sizeof(msg),
674 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
675 err_mask, rtf.command, rtf.feature);
676 rc = -EIO;
677 break;
678 }
679 } else {
0e8634bf 680 level = KERN_INFO;
3264a8d8 681 snprintf(msg, sizeof(msg), "filtered out");
0e8634bf 682 rc = 0;
4700c4bc 683 }
6521148c 684 descr = ata_get_cmd_descript(tf.command);
4700c4bc 685
0e8634bf 686 ata_dev_printk(dev, level,
6521148c 687 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
0e8634bf 688 tf.command, tf.feature, tf.nsect, tf.lbal,
6521148c
RH
689 tf.lbam, tf.lbah, tf.device,
690 (descr ? descr : "unknown"), msg);
0e8634bf
TH
691
692 return rc;
11ef697b
KCA
693}
694
11ef697b
KCA
695/**
696 * ata_acpi_exec_tfs - get then write drive taskfile settings
6746544c 697 * @dev: target ATA device
98a1708d 698 * @nr_executed: out parameter for the number of executed commands
11ef697b 699 *
98a1708d 700 * Evaluate _GTF and execute returned taskfiles.
69b16a5f
TH
701 *
702 * LOCKING:
703 * EH context.
704 *
705 * RETURNS:
66fa7f21
TH
706 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
707 * -errno on other errors.
11ef697b 708 */
66fa7f21 709static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
11ef697b 710{
3264a8d8 711 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
6746544c
TH
712 int gtf_count, i, rc;
713
714 /* get taskfiles */
66fa7f21
TH
715 rc = ata_dev_get_GTF(dev, &gtf);
716 if (rc < 0)
717 return rc;
718 gtf_count = rc;
6746544c
TH
719
720 /* execute them */
3264a8d8
TH
721 for (i = 0; i < gtf_count; i++, gtf++) {
722 rc = ata_acpi_run_tf(dev, gtf, pgtf);
0e8634bf
TH
723 if (rc < 0)
724 break;
3264a8d8 725 if (rc) {
0e8634bf 726 (*nr_executed)++;
3264a8d8
TH
727 pgtf = gtf;
728 }
11ef697b
KCA
729 }
730
398e0782 731 ata_acpi_clear_gtf(dev);
6746544c 732
0e8634bf
TH
733 if (rc < 0)
734 return rc;
735 return 0;
11ef697b
KCA
736}
737
7ea1fbc2
KCA
738/**
739 * ata_acpi_push_id - send Identify data to drive
3a32a8e9 740 * @dev: target ATA device
7ea1fbc2
KCA
741 *
742 * _SDD ACPI object: for SATA mode only
743 * Must be after Identify (Packet) Device -- uses its data
744 * ATM this function never returns a failure. It is an optional
745 * method and if it fails for whatever reason, we should still
746 * just keep going.
69b16a5f
TH
747 *
748 * LOCKING:
749 * EH context.
750 *
751 * RETURNS:
f2406770 752 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
7ea1fbc2 753 */
6746544c 754static int ata_acpi_push_id(struct ata_device *dev)
7ea1fbc2 755{
9af5c9c9 756 struct ata_port *ap = dev->link->ap;
3a32a8e9
TH
757 acpi_status status;
758 struct acpi_object_list input;
759 union acpi_object in_params[1];
7ea1fbc2 760
7ea1fbc2 761 if (ata_msg_probe(ap))
a9a79dfe
JP
762 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
763 __func__, dev->devno, ap->port_no);
7ea1fbc2 764
7ea1fbc2
KCA
765 /* Give the drive Identify data to the drive via the _SDD method */
766 /* _SDD: set up input parameters */
767 input.count = 1;
768 input.pointer = in_params;
769 in_params[0].type = ACPI_TYPE_BUFFER;
3a32a8e9
TH
770 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
771 in_params[0].buffer.pointer = (u8 *)dev->id;
7ea1fbc2
KCA
772 /* Output buffer: _SDD has no output */
773
774 /* It's OK for _SDD to be missing too. */
3a32a8e9 775 swap_buf_le16(dev->id, ATA_ID_WORDS);
30dcf76a
MG
776 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
777 NULL);
3a32a8e9 778 swap_buf_le16(dev->id, ATA_ID_WORDS);
7ea1fbc2 779
f2406770
TH
780 if (status == AE_NOT_FOUND)
781 return -ENOENT;
782
783 if (ACPI_FAILURE(status)) {
a9a79dfe 784 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
f2406770
TH
785 return -EIO;
786 }
7ea1fbc2 787
f2406770 788 return 0;
6746544c
TH
789}
790
64578a3d
TH
791/**
792 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
793 * @ap: target ATA port
794 *
795 * This function is called when @ap is about to be suspended. All
796 * devices are already put to sleep but the port_suspend() callback
797 * hasn't been executed yet. Error return from this function aborts
798 * suspend.
799 *
800 * LOCKING:
801 * EH context.
802 *
803 * RETURNS:
804 * 0 on success, -errno on failure.
805 */
806int ata_acpi_on_suspend(struct ata_port *ap)
807{
c05e6ff0
TH
808 /* nada */
809 return 0;
64578a3d
TH
810}
811
6746544c
TH
812/**
813 * ata_acpi_on_resume - ATA ACPI hook called on resume
814 * @ap: target ATA port
815 *
816 * This function is called when @ap is resumed - right after port
817 * itself is resumed but before any EH action is taken.
818 *
819 * LOCKING:
820 * EH context.
821 */
822void ata_acpi_on_resume(struct ata_port *ap)
823{
c05e6ff0 824 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
f58229f8 825 struct ata_device *dev;
6746544c 826
f1bc1e4c 827 if (ACPI_HANDLE(&ap->tdev) && gtm) {
398e0782
TH
828 /* _GTM valid */
829
830 /* restore timing parameters */
c05e6ff0 831 ata_acpi_stm(ap, gtm);
64578a3d 832
398e0782
TH
833 /* _GTF should immediately follow _STM so that it can
834 * use values set by _STM. Cache _GTF result and
835 * schedule _GTF.
836 */
1eca4365 837 ata_for_each_dev(dev, &ap->link, ALL) {
398e0782 838 ata_acpi_clear_gtf(dev);
48feb3c4
SL
839 if (ata_dev_enabled(dev) &&
840 ata_dev_get_GTF(dev, NULL) >= 0)
398e0782
TH
841 dev->flags |= ATA_DFLAG_ACPI_PENDING;
842 }
843 } else {
844 /* SATA _GTF needs to be evaulated after _SDD and
845 * there's no reason to evaluate IDE _GTF early
846 * without _STM. Clear cache and schedule _GTF.
847 */
1eca4365 848 ata_for_each_dev(dev, &ap->link, ALL) {
398e0782 849 ata_acpi_clear_gtf(dev);
48feb3c4
SL
850 if (ata_dev_enabled(dev))
851 dev->flags |= ATA_DFLAG_ACPI_PENDING;
398e0782
TH
852 }
853 }
7ea1fbc2
KCA
854}
855
a7ff60db 856static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
21334205
AL
857{
858 int d_max_in = ACPI_STATE_D3_COLD;
a7ff60db
AL
859 if (!runtime)
860 goto out;
21334205
AL
861
862 /*
863 * For ATAPI, runtime D3 cold is only allowed
864 * for ZPODD in zero power ready state
865 */
866 if (dev->class == ATA_DEV_ATAPI &&
867 !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
868 d_max_in = ACPI_STATE_D3_HOT;
869
a7ff60db 870out:
f1bc1e4c 871 return acpi_pm_device_sleep_state(&dev->tdev, NULL, d_max_in);
21334205
AL
872}
873
a7ff60db 874static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
bd3adca5 875{
a7ff60db 876 bool runtime = PMSG_IS_AUTO(state);
bd3adca5 877 struct ata_device *dev;
febe53ba 878 acpi_handle handle;
3bd46600 879 int acpi_state;
bd3adca5 880
1eca4365 881 ata_for_each_dev(dev, &ap->link, ENABLED) {
febe53ba 882 handle = ata_dev_acpi_handle(dev);
3bd46600
LM
883 if (!handle)
884 continue;
885
a7ff60db
AL
886 if (!(state.event & PM_EVENT_RESUME)) {
887 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
21334205
AL
888 if (acpi_state == ACPI_STATE_D0)
889 continue;
a7ff60db 890 if (runtime && zpodd_dev_enabled(dev) &&
21334205
AL
891 acpi_state == ACPI_STATE_D3_COLD)
892 zpodd_enable_run_wake(dev);
893 acpi_bus_set_power(handle, acpi_state);
3bd46600 894 } else {
a7ff60db 895 if (runtime && zpodd_dev_enabled(dev))
21334205 896 zpodd_disable_run_wake(dev);
3bd46600
LM
897 acpi_bus_set_power(handle, ACPI_STATE_D0);
898 }
bd3adca5 899 }
a7ff60db 900}
febe53ba 901
a7ff60db
AL
902/* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
903static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
904{
905 struct ata_device *dev;
906 acpi_handle port_handle;
907
f1bc1e4c 908 port_handle = ACPI_HANDLE(&ap->tdev);
a7ff60db
AL
909 if (!port_handle)
910 return;
911
912 /* channel first and then drives for power on and vica versa
913 for power off */
914 if (state.event & PM_EVENT_RESUME)
915 acpi_bus_set_power(port_handle, ACPI_STATE_D0);
febe53ba 916
a7ff60db
AL
917 ata_for_each_dev(dev, &ap->link, ENABLED) {
918 acpi_handle dev_handle = ata_dev_acpi_handle(dev);
919 if (!dev_handle)
920 continue;
921
922 acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
8ad928d5 923 ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
a7ff60db
AL
924 }
925
926 if (!(state.event & PM_EVENT_RESUME))
8ad928d5 927 acpi_bus_set_power(port_handle, ACPI_STATE_D3_COLD);
a7ff60db
AL
928}
929
930/**
931 * ata_acpi_set_state - set the port power state
932 * @ap: target ATA port
933 * @state: state, on/off
934 *
935 * This function sets a proper ACPI D state for the device on
936 * system and runtime PM operations.
937 */
938void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
939{
940 if (ap->flags & ATA_FLAG_ACPI_SATA)
941 sata_acpi_set_state(ap, state);
942 else
943 pata_acpi_set_state(ap, state);
bd3adca5
SL
944}
945
6746544c
TH
946/**
947 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
948 * @dev: target ATA device
949 *
950 * This function is called when @dev is about to be configured.
951 * IDENTIFY data might have been modified after this hook is run.
952 *
953 * LOCKING:
954 * EH context.
955 *
956 * RETURNS:
957 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
958 * -errno on failure.
959 */
960int ata_acpi_on_devcfg(struct ata_device *dev)
961{
9af5c9c9
TH
962 struct ata_port *ap = dev->link->ap;
963 struct ata_eh_context *ehc = &ap->link.eh_context;
6746544c 964 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
66fa7f21 965 int nr_executed = 0;
6746544c
TH
966 int rc;
967
30dcf76a 968 if (!ata_dev_acpi_handle(dev))
6746544c
TH
969 return 0;
970
971 /* do we need to do _GTF? */
972 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
973 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
974 return 0;
975
976 /* do _SDD if SATA */
977 if (acpi_sata) {
978 rc = ata_acpi_push_id(dev);
f2406770 979 if (rc && rc != -ENOENT)
6746544c
TH
980 goto acpi_err;
981 }
982
983 /* do _GTF */
66fa7f21
TH
984 rc = ata_acpi_exec_tfs(dev, &nr_executed);
985 if (rc)
6746544c
TH
986 goto acpi_err;
987
988 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
989
990 /* refresh IDENTIFY page if any _GTF command has been executed */
66fa7f21 991 if (nr_executed) {
6746544c
TH
992 rc = ata_dev_reread_id(dev, 0);
993 if (rc < 0) {
a9a79dfe
JP
994 ata_dev_err(dev,
995 "failed to IDENTIFY after ACPI commands\n");
6746544c
TH
996 return rc;
997 }
998 }
999
1000 return 0;
1001
1002 acpi_err:
66fa7f21
TH
1003 /* ignore evaluation failure if we can continue safely */
1004 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1005 return 0;
6746544c 1006
66fa7f21
TH
1007 /* fail and let EH retry once more for unknown IO errors */
1008 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1009 dev->flags |= ATA_DFLAG_ACPI_FAILED;
1010 return rc;
6746544c 1011 }
66fa7f21 1012
0d0cdb02 1013 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
a9a79dfe 1014 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
66fa7f21
TH
1015
1016 /* We can safely continue if no _GTF command has been executed
1017 * and port is not frozen.
1018 */
1019 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1020 return 0;
1021
6746544c
TH
1022 return rc;
1023}
562f0c2d
TH
1024
1025/**
1026 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1027 * @dev: target ATA device
1028 *
1029 * This function is called when @dev is about to be disabled.
1030 *
1031 * LOCKING:
1032 * EH context.
1033 */
1034void ata_acpi_on_disable(struct ata_device *dev)
1035{
398e0782 1036 ata_acpi_clear_gtf(dev);
562f0c2d 1037}