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