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