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