blob: 87f2f395d79a1f9a9cd3dbc81675f0d71ac6f2cd [file] [log] [blame]
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -07001/*
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
Tejun Heo3264a8d2007-12-15 15:05:06 +09009#include <linux/module.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070010#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Lin Ming3bd46602012-06-25 16:13:06 +080019#include <linux/pm_runtime.h>
Matthew Garrett237d8442007-10-03 01:24:16 +010020#include <scsi/scsi_device.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070021#include "libata.h"
22
23#include <acpi/acpi_bus.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070024
Tejun Heo110f66d2009-09-16 04:17:28 +090025unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
Tejun Heo3264a8d2007-12-15 15:05:06 +090026module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
Tejun Heofa5b5612009-09-16 04:17:02 +090027MODULE_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)");
Tejun Heo3264a8d2007-12-15 15:05:06 +090028
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070029#define NO_PORT_MULT 0xffff
Jeff Garzik2dcb4072007-10-19 06:42:56 -040030#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070031
32#define REGS_PER_GTF 7
Tejun Heo4700c4b2007-05-15 03:28:16 +090033struct ata_acpi_gtf {
34 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
35} __packed;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070036
Alan Coxca426632007-03-08 23:13:50 +000037/*
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}
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070044
Tejun Heo398e0782007-12-15 15:05:03 +090045static void ata_acpi_clear_gtf(struct ata_device *dev)
46{
47 kfree(dev->gtf_cache);
48 dev->gtf_cache = NULL;
49}
50
Matthew Garrett30dcf762012-06-25 16:13:04 +080051/**
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)
Matthew Garrett6b66d952012-06-25 16:13:03 +080059{
60 if (ap->flags & ATA_FLAG_ACPI_SATA)
61 return NULL;
62
Aaron Lud66af4d2013-04-27 09:33:07 +080063 return ap->scsi_host ?
64 DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev) : NULL;
Matthew Garrett6b66d952012-06-25 16:13:03 +080065}
Matthew Garrett30dcf762012-06-25 16:13:04 +080066EXPORT_SYMBOL(ata_ap_acpi_handle);
Matthew Garrett6b66d952012-06-25 16:13:03 +080067
Matthew Garrett30dcf762012-06-25 16:13:04 +080068/**
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)
Matthew Garrett6b66d952012-06-25 16:13:03 +080076{
77 acpi_integer adr;
78 struct ata_port *ap = dev->link->ap;
79
Lv Zheng19ccee72013-04-27 09:37:53 +080080 if (libata_noacpi || dev->flags & ATA_DFLAG_ACPI_DISABLED)
Aaron Lu0d0cdb02012-11-26 13:55:25 +080081 return NULL;
82
Matthew Garrett6b66d952012-06-25 16:13:03 +080083 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
Matthew Garrett30dcf762012-06-25 16:13:04 +080090 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
Matthew Garrett6b66d952012-06-25 16:13:03 +080091}
Matthew Garrett30dcf762012-06-25 16:13:04 +080092EXPORT_SYMBOL(ata_dev_acpi_handle);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070093
Holger Macht664d0802008-06-03 20:27:59 +020094/* @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
Tejun Heo1eca4362008-11-03 20:03:17 +0900103 ata_for_each_link(tlink, ap, EDGE)
104 ata_for_each_dev(tdev, tlink, ALL)
Holger Macht664d0802008-06-03 20:27:59 +0200105 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
Holger Macht664d0802008-06-03 20:27:59 +0200116 *
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,
Shaohua Lif730ae12008-08-28 10:05:45 +0800129 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +0100130{
Holger Macht664d0802008-06-03 20:27:59 +0200131 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo233f1122008-03-12 14:24:43 +0900132 int wait = 0;
133 unsigned long flags;
Zhang Rui3c1e3892008-07-11 09:42:03 -0400134
Holger Macht664d0802008-06-03 20:27:59 +0200135 spin_lock_irqsave(ap->lock, flags);
Shaohua Lif730ae12008-08-28 10:05:45 +0800136 /*
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 */
Tejun Heo233f1122008-03-12 14:24:43 +0900141 switch (event) {
142 case ACPI_NOTIFY_BUS_CHECK:
143 case ACPI_NOTIFY_DEVICE_CHECK:
144 ata_ehi_push_desc(ehi, "ACPI event");
Jeff Garzikc85665f2008-05-19 17:56:10 -0400145
Shaohua Lif730ae12008-08-28 10:05:45 +0800146 ata_ehi_hotplugged(ehi);
147 ata_port_freeze(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200148 break;
149 case ACPI_NOTIFY_EJECT_REQUEST:
150 ata_ehi_push_desc(ehi, "ACPI event");
151
Holger Macht664d0802008-06-03 20:27:59 +0200152 ata_acpi_detach_device(ap, dev);
153 wait = 1;
154 break;
Matthew Garrett237d8442007-10-03 01:24:16 +0100155 }
156
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100157 spin_unlock_irqrestore(ap->lock, flags);
158
Shaohua Lif730ae12008-08-28 10:05:45 +0800159 if (wait)
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100160 ata_port_wait_eh(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200161}
162
163static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
164{
165 struct ata_device *dev = data;
166
Shaohua Lif730ae12008-08-28 10:05:45 +0800167 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
Holger Macht664d0802008-06-03 20:27:59 +0200168}
169
170static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
171{
172 struct ata_port *ap = data;
173
Shaohua Lif730ae12008-08-28 10:05:45 +0800174 ata_acpi_handle_hotplug(ap, NULL, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100175}
176
Shaohua Li1253f7a2008-08-28 10:06:16 +0800177static 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
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400207static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800208 .handler = ata_acpi_dev_notify_dock,
209 .uevent = ata_acpi_dev_uevent,
210};
211
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400212static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800213 .handler = ata_acpi_ap_notify_dock,
214 .uevent = ata_acpi_ap_uevent,
215};
216
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700217/**
Tejun Heo562f0c22007-12-15 15:05:01 +0900218 * 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{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900229 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
Matthew Garrett30dcf762012-06-25 16:13:04 +0800238 if (ata_ap_acpi_handle(ap) && gtm)
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900239 ata_acpi_stm(ap, gtm);
240 }
Tejun Heo562f0c22007-12-15 15:05:01 +0900241}
242
Aaron Lud66af4d2013-04-27 09:33:07 +0800243static int __ata_acpi_gtm(struct ata_port *ap, acpi_handle handle,
244 struct ata_acpi_gtm *gtm)
Tejun Heo64578a32007-05-15 03:28:16 +0900245{
246 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
247 union acpi_object *out_obj;
248 acpi_status status;
249 int rc = 0;
250
Aaron Lud66af4d2013-04-27 09:33:07 +0800251 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
Tejun Heo64578a32007-05-15 03:28:16 +0900252
253 rc = -ENOENT;
254 if (status == AE_NOT_FOUND)
255 goto out_free;
256
257 rc = -EINVAL;
258 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700259 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
260 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900261 goto out_free;
262 }
263
264 out_obj = output.pointer;
265 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700266 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
267 out_obj->type);
Tejun Heo64578a32007-05-15 03:28:16 +0900268
269 goto out_free;
270 }
271
272 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700273 ata_port_err(ap, "_GTM returned invalid length %d\n",
274 out_obj->buffer.length);
Tejun Heo64578a32007-05-15 03:28:16 +0900275 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
Aaron Lud66af4d2013-04-27 09:33:07 +0800285/**
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
Alan Coxbadff032007-10-04 21:28:18 +0100306EXPORT_SYMBOL_GPL(ata_acpi_gtm);
307
Tejun Heo64578a32007-05-15 03:28:16 +0900308/**
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 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900321int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a32007-05-15 03:28:16 +0900322{
323 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900324 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a32007-05-15 03:28:16 +0900325 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);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900330 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a32007-05-15 03:28:16 +0900331 /* Buffers for id may need byteswapping ? */
332 in_params[1].type = ACPI_TYPE_BUFFER;
333 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900334 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900335 in_params[2].type = ACPI_TYPE_BUFFER;
336 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900337 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900338
339 input.count = 3;
340 input.pointer = in_params;
341
Matthew Garrett30dcf762012-06-25 16:13:04 +0800342 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
343 NULL);
Tejun Heo64578a32007-05-15 03:28:16 +0900344
345 if (status == AE_NOT_FOUND)
346 return -ENOENT;
347 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700348 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
349 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900350 return -EINVAL;
351 }
352 return 0;
353}
354
Alan Coxbadff032007-10-04 21:28:18 +0100355EXPORT_SYMBOL_GPL(ata_acpi_stm);
356
Tejun Heo64578a32007-05-15 03:28:16 +0900357/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900358 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900359 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900360 * @gtf: output parameter for buffer containing _GTF taskfile arrays
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700361 *
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 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900370 * LOCKING:
371 * EH context.
372 *
373 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900374 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
375 * if _GTF is invalid.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700376 */
Tejun Heo398e0782007-12-15 15:05:03 +0900377static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700378{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900379 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900380 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900381 struct acpi_buffer output;
382 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900383 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700384
Tejun Heo398e0782007-12-15 15:05:03 +0900385 /* if _GTF is cached, use the cached value */
386 if (dev->gtf_cache) {
387 out_obj = dev->gtf_cache;
388 goto done;
389 }
390
Tejun Heo4700c4b2007-05-15 03:28:16 +0900391 /* set up output buffer */
392 output.length = ACPI_ALLOCATE_BUFFER;
393 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700394
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700395 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700396 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
397 __func__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700398
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700399 /* _GTF has no input parameters */
Matthew Garrett30dcf762012-06-25 16:13:04 +0800400 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
401 &output);
Tejun Heo398e0782007-12-15 15:05:03 +0900402 out_obj = dev->gtf_cache = output.pointer;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900403
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700404 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900405 if (status != AE_NOT_FOUND) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700406 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
407 status);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900408 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900409 }
410 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700411 }
412
413 if (!output.length || !output.pointer) {
414 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700415 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);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900419 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900420 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700421 }
422
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700423 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700424 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
425 out_obj->type);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900426 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900427 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700428 }
429
Tejun Heo4700c4b2007-05-15 03:28:16 +0900430 if (out_obj->buffer.length % REGS_PER_GTF) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700431 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
432 out_obj->buffer.length);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900433 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900434 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700435 }
436
Tejun Heo398e0782007-12-15 15:05:03 +0900437 done:
Tejun Heo4700c4b2007-05-15 03:28:16 +0900438 rc = out_obj->buffer.length / REGS_PER_GTF;
Tejun Heo398e0782007-12-15 15:05:03 +0900439 if (gtf) {
440 *gtf = (void *)out_obj->buffer.pointer;
441 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700442 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
443 __func__, *gtf, rc);
Tejun Heo398e0782007-12-15 15:05:03 +0900444 }
Tejun Heo4700c4b2007-05-15 03:28:16 +0900445 return rc;
446
447 out_free:
Tejun Heo398e0782007-12-15 15:05:03 +0900448 ata_acpi_clear_gtf(dev);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900449 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700450}
451
Tejun Heo7c77fa42007-12-18 16:33:03 +0900452/**
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{
Tejun Heoa0f79b92007-12-18 16:33:05 +0900468 unsigned long xfer_mask = 0;
469 unsigned int type;
470 int unit;
471 u8 mode;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900472
473 /* we always use the 0 slot for crap hardware */
474 unit = dev->devno;
475 if (!(gtm->flags & 0x10))
476 unit = 0;
477
Tejun Heoa0f79b92007-12-18 16:33:05 +0900478 /* PIO */
479 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
480 xfer_mask |= ata_xfer_mode2mask(mode);
Tejun Heo7c77fa42007-12-18 16:33:03 +0900481
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 */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900486 if (!(gtm->flags & (1 << (2 * unit))))
487 type = ATA_SHIFT_MWDMA;
488 else
489 type = ATA_SHIFT_UDMA;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900490
Tejun Heoa0f79b92007-12-18 16:33:05 +0900491 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
492 xfer_mask |= ata_xfer_mode2mask(mode);
493
494 return xfer_mask;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900495}
496EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
497
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700498/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400499 * ata_acpi_cbl_80wire - Check for 80 wire cable
500 * @ap: Port to check
Tejun Heo021ee9a2007-12-18 16:33:06 +0900501 * @gtm: GTM data to use
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400502 *
Tejun Heo021ee9a2007-12-18 16:33:06 +0900503 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400504 */
Tejun Heo021ee9a2007-12-18 16:33:06 +0900505int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400506{
Tejun Heo021ee9a2007-12-18 16:33:06 +0900507 struct ata_device *dev;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400508
Tejun Heo1eca4362008-11-03 20:03:17 +0900509 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo021ee9a2007-12-18 16:33:06 +0900510 unsigned long xfer_mask, udma_mask;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400511
Tejun Heo021ee9a2007-12-18 16:33:06 +0900512 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
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400519 return 0;
520}
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400521EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
522
Tejun Heo3264a8d2007-12-15 15:05:06 +0900523static 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
Tejun Heo110f66d2009-09-16 04:17:28 +0900540static int ata_acpi_filter_tf(struct ata_device *dev,
541 const struct ata_taskfile *tf,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900542 const struct ata_taskfile *ptf)
543{
Tejun Heo110f66d2009-09-16 04:17:28 +0900544 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900545 /* 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
Tejun Heo110f66d2009-09-16 04:17:28 +0900553 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900554 /* 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
Tejun Heofa5b5612009-09-16 04:17:02 +0900575 if (tf->command == ATA_CMD_SET_FEATURES &&
576 tf->feature == SETFEATURES_SATA_ENABLE) {
Tejun Heob3449912008-07-06 23:15:03 +0900577 /* inhibit enabling DIPM */
Tejun Heo110f66d2009-09-16 04:17:28 +0900578 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
Tejun Heob3449912008-07-06 23:15:03 +0900579 tf->nsect == SATA_DIPM)
580 return 1;
Tejun Heofa5b5612009-09-16 04:17:02 +0900581
582 /* inhibit FPDMA non-zero offset */
Tejun Heo110f66d2009-09-16 04:17:28 +0900583 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900584 (tf->nsect == SATA_FPDMA_OFFSET ||
585 tf->nsect == SATA_FPDMA_IN_ORDER))
586 return 1;
587
588 /* inhibit FPDMA auto activation */
Tejun Heo110f66d2009-09-16 04:17:28 +0900589 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900590 tf->nsect == SATA_FPDMA_AA)
591 return 1;
Tejun Heob3449912008-07-06 23:15:03 +0900592 }
593
Tejun Heo3264a8d2007-12-15 15:05:06 +0900594 return 0;
595}
596
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400597/**
Tejun Heo0e8634b2007-12-15 15:05:05 +0900598 * ata_acpi_run_tf - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900599 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700600 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
601 *
Sergei Shtylyov3696df32011-02-04 22:04:17 +0300602 * Outputs ATA taskfile to standard ATA host controller.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700603 * 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 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900612 * LOCKING:
613 * EH context.
614 *
615 * RETURNS:
Tejun Heo3264a8d2007-12-15 15:05:06 +0900616 * 1 if command is executed successfully. 0 if ignored, rejected or
617 * filtered out, -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700618 */
Tejun Heo0e8634b2007-12-15 15:05:05 +0900619static int ata_acpi_run_tf(struct ata_device *dev,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900620 const struct ata_acpi_gtf *gtf,
621 const struct ata_acpi_gtf *prev_gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700622{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900623 struct ata_taskfile *pptf = NULL;
624 struct ata_taskfile tf, ptf, rtf;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900625 unsigned int err_mask;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900626 const char *level;
Robert Hancock65211482009-07-14 20:43:39 -0600627 const char *descr;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900628 char msg[60];
629 int rc;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500630
Tejun Heo4700c4b2007-05-15 03:28:16 +0900631 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;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700635
Tejun Heo3264a8d2007-12-15 15:05:06 +0900636 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 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700641
Tejun Heo110f66d2009-09-16 04:17:28 +0900642 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900643 rtf = tf;
644 err_mask = ata_exec_internal(dev, &rtf, NULL,
645 DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700646
Tejun Heo3264a8d2007-12-15 15:05:06 +0900647 switch (err_mask) {
648 case 0:
649 level = KERN_DEBUG;
650 snprintf(msg, sizeof(msg), "succeeded");
651 rc = 1;
652 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900653
Tejun Heo3264a8d2007-12-15 15:05:06 +0900654 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;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900661
Tejun Heo3264a8d2007-12-15 15:05:06 +0900662 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 {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900671 level = KERN_INFO;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900672 snprintf(msg, sizeof(msg), "filtered out");
Tejun Heo0e8634b2007-12-15 15:05:05 +0900673 rc = 0;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900674 }
Robert Hancock65211482009-07-14 20:43:39 -0600675 descr = ata_get_cmd_descript(tf.command);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900676
Tejun Heo0e8634b2007-12-15 15:05:05 +0900677 ata_dev_printk(dev, level,
Robert Hancock65211482009-07-14 20:43:39 -0600678 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
Tejun Heo0e8634b2007-12-15 15:05:05 +0900679 tf.command, tf.feature, tf.nsect, tf.lbal,
Robert Hancock65211482009-07-14 20:43:39 -0600680 tf.lbam, tf.lbah, tf.device,
681 (descr ? descr : "unknown"), msg);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900682
683 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700684}
685
686/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700687 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900688 * @dev: target ATA device
Martin Olsson98a17082009-04-22 18:21:29 +0200689 * @nr_executed: out parameter for the number of executed commands
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700690 *
Martin Olsson98a17082009-04-22 18:21:29 +0200691 * Evaluate _GTF and execute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900692 *
693 * LOCKING:
694 * EH context.
695 *
696 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900697 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
698 * -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700699 */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900700static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700701{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900702 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
Tejun Heo67465442007-05-15 03:28:16 +0900703 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700704
Tejun Heo67465442007-05-15 03:28:16 +0900705 /* get taskfiles */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900706 rc = ata_dev_get_GTF(dev, &gtf);
707 if (rc < 0)
708 return rc;
709 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700710
Tejun Heo67465442007-05-15 03:28:16 +0900711 /* execute them */
Tejun Heo3264a8d2007-12-15 15:05:06 +0900712 for (i = 0; i < gtf_count; i++, gtf++) {
713 rc = ata_acpi_run_tf(dev, gtf, pgtf);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900714 if (rc < 0)
715 break;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900716 if (rc) {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900717 (*nr_executed)++;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900718 pgtf = gtf;
719 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700720 }
721
Tejun Heo398e0782007-12-15 15:05:03 +0900722 ata_acpi_clear_gtf(dev);
Tejun Heo67465442007-05-15 03:28:16 +0900723
Tejun Heo0e8634b2007-12-15 15:05:05 +0900724 if (rc < 0)
725 return rc;
726 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700727}
728
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700729/**
730 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900731 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700732 *
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.
Tejun Heo69b16a52007-05-15 03:28:16 +0900738 *
739 * LOCKING:
740 * EH context.
741 *
742 * RETURNS:
Tejun Heof2406772009-11-18 22:24:21 +0900743 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700744 */
Tejun Heo67465442007-05-15 03:28:16 +0900745static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700746{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900747 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900748 acpi_status status;
749 struct acpi_object_list input;
750 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700751
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700752 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700753 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
754 __func__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700755
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700756 /* 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;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900761 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
762 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700763 /* Output buffer: _SDD has no output */
764
765 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900766 swap_buf_le16(dev->id, ATA_ID_WORDS);
Matthew Garrett30dcf762012-06-25 16:13:04 +0800767 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
768 NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900769 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700770
Tejun Heof2406772009-11-18 22:24:21 +0900771 if (status == AE_NOT_FOUND)
772 return -ENOENT;
773
774 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700775 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
Tejun Heof2406772009-11-18 22:24:21 +0900776 return -EIO;
777 }
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700778
Tejun Heof2406772009-11-18 22:24:21 +0900779 return 0;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700780}
781
Tejun Heo67465442007-05-15 03:28:16 +0900782/**
Tejun Heo64578a32007-05-15 03:28:16 +0900783 * 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{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900799 /* nada */
800 return 0;
Tejun Heo64578a32007-05-15 03:28:16 +0900801}
802
803/**
Tejun Heo67465442007-05-15 03:28:16 +0900804 * 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{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900815 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Tejun Heof58229f2007-08-06 18:36:23 +0900816 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700817
Matthew Garrett30dcf762012-06-25 16:13:04 +0800818 if (ata_ap_acpi_handle(ap) && gtm) {
Tejun Heo398e0782007-12-15 15:05:03 +0900819 /* _GTM valid */
820
821 /* restore timing parameters */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900822 ata_acpi_stm(ap, gtm);
Tejun Heo64578a32007-05-15 03:28:16 +0900823
Tejun Heo398e0782007-12-15 15:05:03 +0900824 /* _GTF should immediately follow _STM so that it can
825 * use values set by _STM. Cache _GTF result and
826 * schedule _GTF.
827 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900828 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900829 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800830 if (ata_dev_enabled(dev) &&
831 ata_dev_get_GTF(dev, NULL) >= 0)
Tejun Heo398e0782007-12-15 15:05:03 +0900832 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 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900839 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900840 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800841 if (ata_dev_enabled(dev))
842 dev->flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo398e0782007-12-15 15:05:03 +0900843 }
844 }
Tejun Heo67465442007-05-15 03:28:16 +0900845}
846
Aaron Lua7ff60d2013-01-25 14:29:35 +0800847static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
Shaohua Libd3adca2007-11-02 09:32:38 +0800848{
Aaron Lu21334202013-01-15 17:21:01 +0800849 int d_max_in = ACPI_STATE_D3_COLD;
Aaron Lua7ff60d2013-01-25 14:29:35 +0800850 if (!runtime)
851 goto out;
Aaron Lu21334202013-01-15 17:21:01 +0800852
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
Aaron Lua7ff60d2013-01-25 14:29:35 +0800861out:
Aaron Lu21334202013-01-15 17:21:01 +0800862 return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev,
863 NULL, d_max_in);
864}
865
Aaron Lua7ff60d2013-01-25 14:29:35 +0800866static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
867{
868 bool runtime = PMSG_IS_AUTO(state);
Shaohua Libd3adca2007-11-02 09:32:38 +0800869 struct ata_device *dev;
Lin Mingfebe53b2012-06-25 16:13:05 +0800870 acpi_handle handle;
Lin Ming3bd46602012-06-25 16:13:06 +0800871 int acpi_state;
Shaohua Libd3adca2007-11-02 09:32:38 +0800872
Tejun Heo1eca4362008-11-03 20:03:17 +0900873 ata_for_each_dev(dev, &ap->link, ENABLED) {
Lin Mingfebe53b2012-06-25 16:13:05 +0800874 handle = ata_dev_acpi_handle(dev);
Lin Ming3bd46602012-06-25 16:13:06 +0800875 if (!handle)
876 continue;
877
Aaron Lua7ff60d2013-01-25 14:29:35 +0800878 if (!(state.event & PM_EVENT_RESUME)) {
879 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
880 if (acpi_state == ACPI_STATE_D0)
881 continue;
882 if (runtime && zpodd_dev_enabled(dev) &&
883 acpi_state == ACPI_STATE_D3_COLD)
884 zpodd_enable_run_wake(dev);
885 acpi_bus_set_power(handle, acpi_state);
Lin Ming3bd46602012-06-25 16:13:06 +0800886 } else {
Aaron Lua7ff60d2013-01-25 14:29:35 +0800887 if (runtime && zpodd_dev_enabled(dev))
888 zpodd_disable_run_wake(dev);
Lin Ming3bd46602012-06-25 16:13:06 +0800889 acpi_bus_set_power(handle, ACPI_STATE_D0);
890 }
Shaohua Libd3adca2007-11-02 09:32:38 +0800891 }
Aaron Lua7ff60d2013-01-25 14:29:35 +0800892}
Lin Mingfebe53b2012-06-25 16:13:05 +0800893
Aaron Lua7ff60d2013-01-25 14:29:35 +0800894/* 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);
908
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
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700922/**
923 * ata_acpi_set_state - set the port power state
924 * @ap: target ATA port
925 * @state: state, on/off
926 *
Aaron Lua7ff60d2013-01-25 14:29:35 +0800927 * This function sets a proper ACPI D state for the device on
928 * system and runtime PM operations.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700929 */
930void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
931{
Aaron Lua7ff60d2013-01-25 14:29:35 +0800932 if (ap->flags & ATA_FLAG_ACPI_SATA)
933 sata_acpi_set_state(ap, state);
934 else
935 pata_acpi_set_state(ap, state);
Shaohua Libd3adca2007-11-02 09:32:38 +0800936}
937
938/**
Tejun Heo67465442007-05-15 03:28:16 +0900939 * 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{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900954 struct ata_port *ap = dev->link->ap;
955 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900956 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
Tejun Heo66fa7f22007-12-15 15:05:04 +0900957 int nr_executed = 0;
Tejun Heo67465442007-05-15 03:28:16 +0900958 int rc;
959
Matthew Garrett30dcf762012-06-25 16:13:04 +0800960 if (!ata_dev_acpi_handle(dev))
Tejun Heo67465442007-05-15 03:28:16 +0900961 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);
Tejun Heof2406772009-11-18 22:24:21 +0900971 if (rc && rc != -ENOENT)
Tejun Heo67465442007-05-15 03:28:16 +0900972 goto acpi_err;
973 }
974
975 /* do _GTF */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900976 rc = ata_acpi_exec_tfs(dev, &nr_executed);
977 if (rc)
Tejun Heo67465442007-05-15 03:28:16 +0900978 goto acpi_err;
979
980 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
981
982 /* refresh IDENTIFY page if any _GTF command has been executed */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900983 if (nr_executed) {
Tejun Heo67465442007-05-15 03:28:16 +0900984 rc = ata_dev_reread_id(dev, 0);
985 if (rc < 0) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700986 ata_dev_err(dev,
987 "failed to IDENTIFY after ACPI commands\n");
Tejun Heo67465442007-05-15 03:28:16 +0900988 return rc;
989 }
990 }
991
992 return 0;
993
994 acpi_err:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900995 /* ignore evaluation failure if we can continue safely */
996 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
997 return 0;
Tejun Heo67465442007-05-15 03:28:16 +0900998
Tejun Heo66fa7f22007-12-15 15:05:04 +0900999 /* 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;
Tejun Heo67465442007-05-15 03:28:16 +09001003 }
Tejun Heo66fa7f22007-12-15 15:05:04 +09001004
Aaron Lu0d0cdb02012-11-26 13:55:25 +08001005 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
Joe Perchesa9a79df2011-04-15 15:51:59 -07001006 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
Tejun Heo66fa7f22007-12-15 15:05:04 +09001007
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
Tejun Heo67465442007-05-15 03:28:16 +09001014 return rc;
1015}
Tejun Heo562f0c22007-12-15 15:05:01 +09001016
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{
Tejun Heo398e0782007-12-15 15:05:03 +09001028 ata_acpi_clear_gtf(dev);
Tejun Heo562f0c22007-12-15 15:05:01 +09001029}
Matthew Garrett6b66d952012-06-25 16:13:03 +08001030
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{
Lv Zheng19ccee72013-04-27 09:37:53 +08001050 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA)
Matthew Garrett6b66d952012-06-25 16:13:03 +08001051 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
Aaron Lud66af4d2013-04-27 09:33:07 +08001059 if (__ata_acpi_gtm(ap, *handle, &ap->__acpi_init_gtm) == 0)
Aaron Lu83400912012-08-15 17:08:12 +08001060 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1061
Matthew Garrett6b66d952012-06-25 16:13:03 +08001062 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
Aaron Lu60817a62012-10-09 15:37:48 +08001070 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 {
Matthew Garrett6b66d952012-06-25 16:13:03 +08001077 ata_dev = &ap->link.device[sdev->id];
Aaron Lu60817a62012-10-09 15:37:48 +08001078 }
Matthew Garrett6b66d952012-06-25 16:13:03 +08001079
Matthew Garrett30dcf762012-06-25 16:13:04 +08001080 *handle = ata_dev_acpi_handle(ata_dev);
Matthew Garrett6b66d952012-06-25 16:13:03 +08001081
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
Matthew Garrett6b66d952012-06-25 16:13:03 +08001123static struct acpi_bus_type ata_acpi_bus = {
Rafael J. Wysocki53540092013-03-03 22:35:20 +01001124 .name = "ATA",
Matthew Garrett6b66d952012-06-25 16:13:03 +08001125 .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}