blob: 9e69a5308693de59abd5c596244033af43f7b3a8 [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
Tejun Heo110f66d2009-09-16 04:17:28 +090023unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
Tejun Heo3264a8d2007-12-15 15:05:06 +090024module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
Tejun Heofa5b5612009-09-16 04:17:02 +090025MODULE_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 +090026
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070027#define NO_PORT_MULT 0xffff
Jeff Garzik2dcb4072007-10-19 06:42:56 -040028#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070029
30#define REGS_PER_GTF 7
Tejun Heo4700c4b2007-05-15 03:28:16 +090031struct ata_acpi_gtf {
32 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
33} __packed;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070034
Tejun Heo398e0782007-12-15 15:05:03 +090035static void ata_acpi_clear_gtf(struct ata_device *dev)
36{
37 kfree(dev->gtf_cache);
38 dev->gtf_cache = NULL;
39}
40
Matthew Garrett30dcf762012-06-25 16:13:04 +080041/**
Matthew Garrett30dcf762012-06-25 16:13:04 +080042 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
Aaron Luf1bc1e42013-08-23 10:17:54 +080043 * @dev: the acpi_handle returned will correspond to this device
Matthew Garrett30dcf762012-06-25 16:13:04 +080044 *
45 * Returns the acpi_handle for the ACPI namespace object corresponding to
46 * the ata_device passed into the function, or NULL if no such object exists
Aaron Luf1bc1e42013-08-23 10:17:54 +080047 * or ACPI is disabled for this device due to consecutive errors.
Matthew Garrett30dcf762012-06-25 16:13:04 +080048 */
49acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
Matthew Garrett6b66d952012-06-25 16:13:03 +080050{
Aaron Luf1bc1e42013-08-23 10:17:54 +080051 return dev->flags & ATA_DFLAG_ACPI_DISABLED ?
52 NULL : ACPI_HANDLE(&dev->tdev);
Matthew Garrett6b66d952012-06-25 16:13:03 +080053}
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070054
Holger Macht664d0802008-06-03 20:27:59 +020055/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
56static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
57{
58 if (dev)
59 dev->flags |= ATA_DFLAG_DETACH;
60 else {
61 struct ata_link *tlink;
62 struct ata_device *tdev;
63
Tejun Heo1eca4362008-11-03 20:03:17 +090064 ata_for_each_link(tlink, ap, EDGE)
65 ata_for_each_dev(tdev, tlink, ALL)
Holger Macht664d0802008-06-03 20:27:59 +020066 tdev->flags |= ATA_DFLAG_DETACH;
67 }
68
69 ata_port_schedule_eh(ap);
70}
71
72/**
73 * ata_acpi_handle_hotplug - ACPI event handler backend
74 * @ap: ATA port ACPI event occurred
75 * @dev: ATA device ACPI event occurred (can be NULL)
76 * @event: ACPI event which occurred
Holger Macht664d0802008-06-03 20:27:59 +020077 *
78 * All ACPI bay / device realted events end up in this function. If
79 * the event is port-wide @dev is NULL. If the event is specific to a
80 * device, @dev points to it.
81 *
82 * Hotplug (as opposed to unplug) notification is always handled as
83 * port-wide while unplug only kills the target device on device-wide
84 * event.
85 *
86 * LOCKING:
87 * ACPI notify handler context. May sleep.
88 */
89static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
Shaohua Lif730ae12008-08-28 10:05:45 +080090 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +010091{
Holger Macht664d0802008-06-03 20:27:59 +020092 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo233f1122008-03-12 14:24:43 +090093 int wait = 0;
94 unsigned long flags;
Zhang Rui3c1e3892008-07-11 09:42:03 -040095
Holger Macht664d0802008-06-03 20:27:59 +020096 spin_lock_irqsave(ap->lock, flags);
Shaohua Lif730ae12008-08-28 10:05:45 +080097 /*
98 * When dock driver calls into the routine, it will always use
99 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
100 * ACPI_NOTIFY_EJECT_REQUEST for remove
101 */
Tejun Heo233f1122008-03-12 14:24:43 +0900102 switch (event) {
103 case ACPI_NOTIFY_BUS_CHECK:
104 case ACPI_NOTIFY_DEVICE_CHECK:
105 ata_ehi_push_desc(ehi, "ACPI event");
Jeff Garzikc85665f2008-05-19 17:56:10 -0400106
Shaohua Lif730ae12008-08-28 10:05:45 +0800107 ata_ehi_hotplugged(ehi);
108 ata_port_freeze(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200109 break;
110 case ACPI_NOTIFY_EJECT_REQUEST:
111 ata_ehi_push_desc(ehi, "ACPI event");
112
Holger Macht664d0802008-06-03 20:27:59 +0200113 ata_acpi_detach_device(ap, dev);
114 wait = 1;
115 break;
Matthew Garrett237d8442007-10-03 01:24:16 +0100116 }
117
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100118 spin_unlock_irqrestore(ap->lock, flags);
119
Aaron Luf1bc1e42013-08-23 10:17:54 +0800120 if (wait)
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100121 ata_port_wait_eh(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200122}
123
124static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
125{
126 struct ata_device *dev = data;
127
Shaohua Lif730ae12008-08-28 10:05:45 +0800128 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
Holger Macht664d0802008-06-03 20:27:59 +0200129}
130
131static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
132{
133 struct ata_port *ap = data;
134
Shaohua Lif730ae12008-08-28 10:05:45 +0800135 ata_acpi_handle_hotplug(ap, NULL, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100136}
137
Shaohua Li1253f7a2008-08-28 10:06:16 +0800138static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
139 u32 event)
140{
141 struct kobject *kobj = NULL;
142 char event_string[20];
143 char *envp[] = { event_string, NULL };
144
145 if (dev) {
146 if (dev->sdev)
147 kobj = &dev->sdev->sdev_gendev.kobj;
148 } else
149 kobj = &ap->dev->kobj;
150
151 if (kobj) {
152 snprintf(event_string, 20, "BAY_EVENT=%d", event);
153 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
154 }
155}
156
157static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
158{
159 ata_acpi_uevent(data, NULL, event);
160}
161
162static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
163{
164 struct ata_device *dev = data;
165 ata_acpi_uevent(dev->link->ap, dev, event);
166}
167
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400168static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800169 .handler = ata_acpi_dev_notify_dock,
170 .uevent = ata_acpi_dev_uevent,
171};
172
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400173static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800174 .handler = ata_acpi_ap_notify_dock,
175 .uevent = ata_acpi_ap_uevent,
176};
177
Aaron Luf1bc1e42013-08-23 10:17:54 +0800178/* bind acpi handle to pata port */
179void ata_acpi_bind_port(struct ata_port *ap)
Aaron Lu44521522013-06-20 09:38:34 +0800180{
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100181 struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev);
Aaron Lu44521522013-06-20 09:38:34 +0800182
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100183 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_companion)
Aaron Luf1bc1e42013-08-23 10:17:54 +0800184 return;
Aaron Lu44521522013-06-20 09:38:34 +0800185
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100186 acpi_preset_companion(&ap->tdev, host_companion, ap->port_no);
Aaron Lu44521522013-06-20 09:38:34 +0800187
Aaron Luf1bc1e42013-08-23 10:17:54 +0800188 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
189 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
Aaron Lu44521522013-06-20 09:38:34 +0800190
Aaron Luf1bc1e42013-08-23 10:17:54 +0800191 /* we might be on a docking station */
192 register_hotplug_dock_device(ACPI_HANDLE(&ap->tdev),
193 &ata_acpi_ap_dock_ops, ap, NULL, NULL);
194}
Aaron Lu44521522013-06-20 09:38:34 +0800195
Aaron Luf1bc1e42013-08-23 10:17:54 +0800196void ata_acpi_bind_dev(struct ata_device *dev)
197{
198 struct ata_port *ap = dev->link->ap;
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100199 struct acpi_device *port_companion = ACPI_COMPANION(&ap->tdev);
200 struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev);
201 struct acpi_device *parent;
Aaron Luf1bc1e42013-08-23 10:17:54 +0800202 u64 adr;
203
204 /*
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100205 * For both sata/pata devices, host companion device is required.
206 * For pata device, port companion device is also required.
Aaron Luf1bc1e42013-08-23 10:17:54 +0800207 */
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100208 if (libata_noacpi || !host_companion ||
209 (!(ap->flags & ATA_FLAG_ACPI_SATA) && !port_companion))
Aaron Luf1bc1e42013-08-23 10:17:54 +0800210 return;
211
212 if (ap->flags & ATA_FLAG_ACPI_SATA) {
213 if (!sata_pmp_attached(ap))
214 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
215 else
216 adr = SATA_ADR(ap->port_no, dev->link->pmp);
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100217 parent = host_companion;
Aaron Luf1bc1e42013-08-23 10:17:54 +0800218 } else {
219 adr = dev->devno;
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100220 parent = port_companion;
Aaron Lu44521522013-06-20 09:38:34 +0800221 }
Aaron Luf1bc1e42013-08-23 10:17:54 +0800222
Rafael J. Wysocki9c5ad362013-11-28 23:58:28 +0100223 acpi_preset_companion(&dev->tdev, parent, adr);
Aaron Luf1bc1e42013-08-23 10:17:54 +0800224
225 register_hotplug_dock_device(ata_dev_acpi_handle(dev),
226 &ata_acpi_dev_dock_ops, dev, NULL, NULL);
Aaron Lu44521522013-06-20 09:38:34 +0800227}
228
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700229/**
Tejun Heo562f0c22007-12-15 15:05:01 +0900230 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
231 * @host: target ATA host
232 *
233 * This function is called during driver detach after the whole host
234 * is shut down.
235 *
236 * LOCKING:
237 * EH context.
238 */
239void ata_acpi_dissociate(struct ata_host *host)
240{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900241 int i;
242
243 /* Restore initial _GTM values so that driver which attaches
244 * afterward can use them too.
245 */
246 for (i = 0; i < host->n_ports; i++) {
247 struct ata_port *ap = host->ports[i];
248 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
249
Aaron Luf1bc1e42013-08-23 10:17:54 +0800250 if (ACPI_HANDLE(&ap->tdev) && gtm)
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900251 ata_acpi_stm(ap, gtm);
252 }
Tejun Heo562f0c22007-12-15 15:05:01 +0900253}
254
Aaron Luf1bc1e42013-08-23 10:17:54 +0800255/**
256 * ata_acpi_gtm - execute _GTM
257 * @ap: target ATA port
258 * @gtm: out parameter for _GTM result
259 *
260 * Evaluate _GTM and store the result in @gtm.
261 *
262 * LOCKING:
263 * EH context.
264 *
265 * RETURNS:
266 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
267 */
268int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
Tejun Heo64578a32007-05-15 03:28:16 +0900269{
270 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
271 union acpi_object *out_obj;
272 acpi_status status;
273 int rc = 0;
Aaron Luf1bc1e42013-08-23 10:17:54 +0800274 acpi_handle handle = ACPI_HANDLE(&ap->tdev);
275
276 if (!handle)
277 return -EINVAL;
Tejun Heo64578a32007-05-15 03:28:16 +0900278
Aaron Lud66af4d2013-04-27 09:33:07 +0800279 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
Tejun Heo64578a32007-05-15 03:28:16 +0900280
281 rc = -ENOENT;
282 if (status == AE_NOT_FOUND)
283 goto out_free;
284
285 rc = -EINVAL;
286 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700287 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
288 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900289 goto out_free;
290 }
291
292 out_obj = output.pointer;
293 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700294 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
295 out_obj->type);
Tejun Heo64578a32007-05-15 03:28:16 +0900296
297 goto out_free;
298 }
299
300 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700301 ata_port_err(ap, "_GTM returned invalid length %d\n",
302 out_obj->buffer.length);
Tejun Heo64578a32007-05-15 03:28:16 +0900303 goto out_free;
304 }
305
306 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
307 rc = 0;
308 out_free:
309 kfree(output.pointer);
310 return rc;
311}
312
Alan Coxbadff032007-10-04 21:28:18 +0100313EXPORT_SYMBOL_GPL(ata_acpi_gtm);
314
Tejun Heo64578a32007-05-15 03:28:16 +0900315/**
316 * ata_acpi_stm - execute _STM
317 * @ap: target ATA port
318 * @stm: timing parameter to _STM
319 *
320 * Evaluate _STM with timing parameter @stm.
321 *
322 * LOCKING:
323 * EH context.
324 *
325 * RETURNS:
326 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
327 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900328int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a32007-05-15 03:28:16 +0900329{
330 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900331 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a32007-05-15 03:28:16 +0900332 struct acpi_object_list input;
333 union acpi_object in_params[3];
334
335 in_params[0].type = ACPI_TYPE_BUFFER;
336 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900337 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a32007-05-15 03:28:16 +0900338 /* Buffers for id may need byteswapping ? */
339 in_params[1].type = ACPI_TYPE_BUFFER;
340 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900341 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900342 in_params[2].type = ACPI_TYPE_BUFFER;
343 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900344 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900345
346 input.count = 3;
347 input.pointer = in_params;
348
Aaron Luf1bc1e42013-08-23 10:17:54 +0800349 status = acpi_evaluate_object(ACPI_HANDLE(&ap->tdev), "_STM",
350 &input, NULL);
Tejun Heo64578a32007-05-15 03:28:16 +0900351
352 if (status == AE_NOT_FOUND)
353 return -ENOENT;
354 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700355 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
356 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900357 return -EINVAL;
358 }
359 return 0;
360}
361
Alan Coxbadff032007-10-04 21:28:18 +0100362EXPORT_SYMBOL_GPL(ata_acpi_stm);
363
Tejun Heo64578a32007-05-15 03:28:16 +0900364/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900365 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900366 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900367 * @gtf: output parameter for buffer containing _GTF taskfile arrays
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700368 *
369 * This applies to both PATA and SATA drives.
370 *
371 * The _GTF method has no input parameters.
372 * It returns a variable number of register set values (registers
373 * hex 1F1..1F7, taskfiles).
374 * The <variable number> is not known in advance, so have ACPI-CA
375 * allocate the buffer as needed and return it, then free it later.
376 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900377 * LOCKING:
378 * EH context.
379 *
380 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900381 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
382 * if _GTF is invalid.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700383 */
Tejun Heo398e0782007-12-15 15:05:03 +0900384static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700385{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900386 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900387 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900388 struct acpi_buffer output;
389 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900390 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700391
Tejun Heo398e0782007-12-15 15:05:03 +0900392 /* if _GTF is cached, use the cached value */
393 if (dev->gtf_cache) {
394 out_obj = dev->gtf_cache;
395 goto done;
396 }
397
Tejun Heo4700c4b2007-05-15 03:28:16 +0900398 /* set up output buffer */
399 output.length = ACPI_ALLOCATE_BUFFER;
400 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700401
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700402 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700403 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
404 __func__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700405
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700406 /* _GTF has no input parameters */
Matthew Garrett30dcf762012-06-25 16:13:04 +0800407 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
408 &output);
Tejun Heo398e0782007-12-15 15:05:03 +0900409 out_obj = dev->gtf_cache = output.pointer;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900410
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700411 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900412 if (status != AE_NOT_FOUND) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700413 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
414 status);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900415 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900416 }
417 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700418 }
419
420 if (!output.length || !output.pointer) {
421 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700422 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
423 __func__,
424 (unsigned long long)output.length,
425 output.pointer);
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
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700430 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700431 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
432 out_obj->type);
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 Heo4700c4b2007-05-15 03:28:16 +0900437 if (out_obj->buffer.length % REGS_PER_GTF) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700438 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
439 out_obj->buffer.length);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900440 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900441 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700442 }
443
Tejun Heo398e0782007-12-15 15:05:03 +0900444 done:
Tejun Heo4700c4b2007-05-15 03:28:16 +0900445 rc = out_obj->buffer.length / REGS_PER_GTF;
Tejun Heo398e0782007-12-15 15:05:03 +0900446 if (gtf) {
447 *gtf = (void *)out_obj->buffer.pointer;
448 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700449 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
450 __func__, *gtf, rc);
Tejun Heo398e0782007-12-15 15:05:03 +0900451 }
Tejun Heo4700c4b2007-05-15 03:28:16 +0900452 return rc;
453
454 out_free:
Tejun Heo398e0782007-12-15 15:05:03 +0900455 ata_acpi_clear_gtf(dev);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900456 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700457}
458
Tejun Heo7c77fa42007-12-18 16:33:03 +0900459/**
460 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
461 * @dev: target device
462 * @gtm: GTM parameter to use
463 *
464 * Determine xfermask for @dev from @gtm.
465 *
466 * LOCKING:
467 * None.
468 *
469 * RETURNS:
470 * Determined xfermask.
471 */
472unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
473 const struct ata_acpi_gtm *gtm)
474{
Tejun Heoa0f79b92007-12-18 16:33:05 +0900475 unsigned long xfer_mask = 0;
476 unsigned int type;
477 int unit;
478 u8 mode;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900479
480 /* we always use the 0 slot for crap hardware */
481 unit = dev->devno;
482 if (!(gtm->flags & 0x10))
483 unit = 0;
484
Tejun Heoa0f79b92007-12-18 16:33:05 +0900485 /* PIO */
486 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
487 xfer_mask |= ata_xfer_mode2mask(mode);
Tejun Heo7c77fa42007-12-18 16:33:03 +0900488
489 /* See if we have MWDMA or UDMA data. We don't bother with
490 * MWDMA if UDMA is available as this means the BIOS set UDMA
491 * and our error changedown if it works is UDMA to PIO anyway.
492 */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900493 if (!(gtm->flags & (1 << (2 * unit))))
494 type = ATA_SHIFT_MWDMA;
495 else
496 type = ATA_SHIFT_UDMA;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900497
Tejun Heoa0f79b92007-12-18 16:33:05 +0900498 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
499 xfer_mask |= ata_xfer_mode2mask(mode);
500
501 return xfer_mask;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900502}
503EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
504
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700505/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400506 * ata_acpi_cbl_80wire - Check for 80 wire cable
507 * @ap: Port to check
Tejun Heo021ee9a2007-12-18 16:33:06 +0900508 * @gtm: GTM data to use
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400509 *
Tejun Heo021ee9a2007-12-18 16:33:06 +0900510 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400511 */
Tejun Heo021ee9a2007-12-18 16:33:06 +0900512int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400513{
Tejun Heo021ee9a2007-12-18 16:33:06 +0900514 struct ata_device *dev;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400515
Tejun Heo1eca4362008-11-03 20:03:17 +0900516 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo021ee9a2007-12-18 16:33:06 +0900517 unsigned long xfer_mask, udma_mask;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400518
Tejun Heo021ee9a2007-12-18 16:33:06 +0900519 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
520 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
521
522 if (udma_mask & ~ATA_UDMA_MASK_40C)
523 return 1;
524 }
525
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400526 return 0;
527}
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400528EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
529
Tejun Heo3264a8d2007-12-15 15:05:06 +0900530static void ata_acpi_gtf_to_tf(struct ata_device *dev,
531 const struct ata_acpi_gtf *gtf,
532 struct ata_taskfile *tf)
533{
534 ata_tf_init(dev, tf);
535
536 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
537 tf->protocol = ATA_PROT_NODATA;
538 tf->feature = gtf->tf[0]; /* 0x1f1 */
539 tf->nsect = gtf->tf[1]; /* 0x1f2 */
540 tf->lbal = gtf->tf[2]; /* 0x1f3 */
541 tf->lbam = gtf->tf[3]; /* 0x1f4 */
542 tf->lbah = gtf->tf[4]; /* 0x1f5 */
543 tf->device = gtf->tf[5]; /* 0x1f6 */
544 tf->command = gtf->tf[6]; /* 0x1f7 */
545}
546
Tejun Heo110f66d2009-09-16 04:17:28 +0900547static int ata_acpi_filter_tf(struct ata_device *dev,
548 const struct ata_taskfile *tf,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900549 const struct ata_taskfile *ptf)
550{
Tejun Heo110f66d2009-09-16 04:17:28 +0900551 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900552 /* libata doesn't use ACPI to configure transfer mode.
553 * It will only confuse device configuration. Skip.
554 */
555 if (tf->command == ATA_CMD_SET_FEATURES &&
556 tf->feature == SETFEATURES_XFER)
557 return 1;
558 }
559
Tejun Heo110f66d2009-09-16 04:17:28 +0900560 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900561 /* BIOS writers, sorry but we don't wanna lock
562 * features unless the user explicitly said so.
563 */
564
565 /* DEVICE CONFIGURATION FREEZE LOCK */
566 if (tf->command == ATA_CMD_CONF_OVERLAY &&
567 tf->feature == ATA_DCO_FREEZE_LOCK)
568 return 1;
569
570 /* SECURITY FREEZE LOCK */
571 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
572 return 1;
573
574 /* SET MAX LOCK and SET MAX FREEZE LOCK */
575 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
576 tf->command == ATA_CMD_SET_MAX &&
577 (tf->feature == ATA_SET_MAX_LOCK ||
578 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
579 return 1;
580 }
581
Tejun Heofa5b5612009-09-16 04:17:02 +0900582 if (tf->command == ATA_CMD_SET_FEATURES &&
583 tf->feature == SETFEATURES_SATA_ENABLE) {
Tejun Heob3449912008-07-06 23:15:03 +0900584 /* inhibit enabling DIPM */
Tejun Heo110f66d2009-09-16 04:17:28 +0900585 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
Tejun Heob3449912008-07-06 23:15:03 +0900586 tf->nsect == SATA_DIPM)
587 return 1;
Tejun Heofa5b5612009-09-16 04:17:02 +0900588
589 /* inhibit FPDMA non-zero offset */
Tejun Heo110f66d2009-09-16 04:17:28 +0900590 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900591 (tf->nsect == SATA_FPDMA_OFFSET ||
592 tf->nsect == SATA_FPDMA_IN_ORDER))
593 return 1;
594
595 /* inhibit FPDMA auto activation */
Tejun Heo110f66d2009-09-16 04:17:28 +0900596 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900597 tf->nsect == SATA_FPDMA_AA)
598 return 1;
Tejun Heob3449912008-07-06 23:15:03 +0900599 }
600
Tejun Heo3264a8d2007-12-15 15:05:06 +0900601 return 0;
602}
603
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400604/**
Tejun Heo0e8634b2007-12-15 15:05:05 +0900605 * ata_acpi_run_tf - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900606 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700607 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
608 *
Sergei Shtylyov3696df32011-02-04 22:04:17 +0300609 * Outputs ATA taskfile to standard ATA host controller.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700610 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
611 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
612 * hob_lbal, hob_lbam, and hob_lbah.
613 *
614 * This function waits for idle (!BUSY and !DRQ) after writing
615 * registers. If the control register has a new value, this
616 * function also waits for idle after writing control and before
617 * writing the remaining registers.
618 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900619 * LOCKING:
620 * EH context.
621 *
622 * RETURNS:
Tejun Heo3264a8d2007-12-15 15:05:06 +0900623 * 1 if command is executed successfully. 0 if ignored, rejected or
624 * filtered out, -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700625 */
Tejun Heo0e8634b2007-12-15 15:05:05 +0900626static int ata_acpi_run_tf(struct ata_device *dev,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900627 const struct ata_acpi_gtf *gtf,
628 const struct ata_acpi_gtf *prev_gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700629{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900630 struct ata_taskfile *pptf = NULL;
631 struct ata_taskfile tf, ptf, rtf;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900632 unsigned int err_mask;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900633 const char *level;
Robert Hancock65211482009-07-14 20:43:39 -0600634 const char *descr;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900635 char msg[60];
636 int rc;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500637
Tejun Heo4700c4b2007-05-15 03:28:16 +0900638 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
639 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
640 && (gtf->tf[6] == 0))
641 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700642
Tejun Heo3264a8d2007-12-15 15:05:06 +0900643 ata_acpi_gtf_to_tf(dev, gtf, &tf);
644 if (prev_gtf) {
645 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
646 pptf = &ptf;
647 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700648
Tejun Heo110f66d2009-09-16 04:17:28 +0900649 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900650 rtf = tf;
651 err_mask = ata_exec_internal(dev, &rtf, NULL,
652 DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700653
Tejun Heo3264a8d2007-12-15 15:05:06 +0900654 switch (err_mask) {
655 case 0:
656 level = KERN_DEBUG;
657 snprintf(msg, sizeof(msg), "succeeded");
658 rc = 1;
659 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900660
Tejun Heo3264a8d2007-12-15 15:05:06 +0900661 case AC_ERR_DEV:
662 level = KERN_INFO;
663 snprintf(msg, sizeof(msg),
664 "rejected by device (Stat=0x%02x Err=0x%02x)",
665 rtf.command, rtf.feature);
666 rc = 0;
667 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900668
Tejun Heo3264a8d2007-12-15 15:05:06 +0900669 default:
670 level = KERN_ERR;
671 snprintf(msg, sizeof(msg),
672 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
673 err_mask, rtf.command, rtf.feature);
674 rc = -EIO;
675 break;
676 }
677 } else {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900678 level = KERN_INFO;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900679 snprintf(msg, sizeof(msg), "filtered out");
Tejun Heo0e8634b2007-12-15 15:05:05 +0900680 rc = 0;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900681 }
Robert Hancock65211482009-07-14 20:43:39 -0600682 descr = ata_get_cmd_descript(tf.command);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900683
Tejun Heo0e8634b2007-12-15 15:05:05 +0900684 ata_dev_printk(dev, level,
Robert Hancock65211482009-07-14 20:43:39 -0600685 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
Tejun Heo0e8634b2007-12-15 15:05:05 +0900686 tf.command, tf.feature, tf.nsect, tf.lbal,
Robert Hancock65211482009-07-14 20:43:39 -0600687 tf.lbam, tf.lbah, tf.device,
688 (descr ? descr : "unknown"), msg);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900689
690 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700691}
692
693/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700694 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900695 * @dev: target ATA device
Martin Olsson98a17082009-04-22 18:21:29 +0200696 * @nr_executed: out parameter for the number of executed commands
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700697 *
Martin Olsson98a17082009-04-22 18:21:29 +0200698 * Evaluate _GTF and execute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900699 *
700 * LOCKING:
701 * EH context.
702 *
703 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900704 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
705 * -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700706 */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900707static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700708{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900709 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
Tejun Heo67465442007-05-15 03:28:16 +0900710 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700711
Tejun Heo67465442007-05-15 03:28:16 +0900712 /* get taskfiles */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900713 rc = ata_dev_get_GTF(dev, &gtf);
714 if (rc < 0)
715 return rc;
716 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700717
Tejun Heo67465442007-05-15 03:28:16 +0900718 /* execute them */
Tejun Heo3264a8d2007-12-15 15:05:06 +0900719 for (i = 0; i < gtf_count; i++, gtf++) {
720 rc = ata_acpi_run_tf(dev, gtf, pgtf);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900721 if (rc < 0)
722 break;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900723 if (rc) {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900724 (*nr_executed)++;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900725 pgtf = gtf;
726 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700727 }
728
Tejun Heo398e0782007-12-15 15:05:03 +0900729 ata_acpi_clear_gtf(dev);
Tejun Heo67465442007-05-15 03:28:16 +0900730
Tejun Heo0e8634b2007-12-15 15:05:05 +0900731 if (rc < 0)
732 return rc;
733 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700734}
735
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700736/**
737 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900738 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700739 *
740 * _SDD ACPI object: for SATA mode only
741 * Must be after Identify (Packet) Device -- uses its data
742 * ATM this function never returns a failure. It is an optional
743 * method and if it fails for whatever reason, we should still
744 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900745 *
746 * LOCKING:
747 * EH context.
748 *
749 * RETURNS:
Tejun Heof2406772009-11-18 22:24:21 +0900750 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700751 */
Tejun Heo67465442007-05-15 03:28:16 +0900752static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700753{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900754 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900755 acpi_status status;
756 struct acpi_object_list input;
757 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700758
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700759 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700760 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
761 __func__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700762
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700763 /* Give the drive Identify data to the drive via the _SDD method */
764 /* _SDD: set up input parameters */
765 input.count = 1;
766 input.pointer = in_params;
767 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900768 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
769 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700770 /* Output buffer: _SDD has no output */
771
772 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900773 swap_buf_le16(dev->id, ATA_ID_WORDS);
Matthew Garrett30dcf762012-06-25 16:13:04 +0800774 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
775 NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900776 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700777
Tejun Heof2406772009-11-18 22:24:21 +0900778 if (status == AE_NOT_FOUND)
779 return -ENOENT;
780
781 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700782 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
Tejun Heof2406772009-11-18 22:24:21 +0900783 return -EIO;
784 }
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700785
Tejun Heof2406772009-11-18 22:24:21 +0900786 return 0;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700787}
788
Tejun Heo67465442007-05-15 03:28:16 +0900789/**
Tejun Heo64578a32007-05-15 03:28:16 +0900790 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
791 * @ap: target ATA port
792 *
793 * This function is called when @ap is about to be suspended. All
794 * devices are already put to sleep but the port_suspend() callback
795 * hasn't been executed yet. Error return from this function aborts
796 * suspend.
797 *
798 * LOCKING:
799 * EH context.
800 *
801 * RETURNS:
802 * 0 on success, -errno on failure.
803 */
804int ata_acpi_on_suspend(struct ata_port *ap)
805{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900806 /* nada */
807 return 0;
Tejun Heo64578a32007-05-15 03:28:16 +0900808}
809
810/**
Tejun Heo67465442007-05-15 03:28:16 +0900811 * ata_acpi_on_resume - ATA ACPI hook called on resume
812 * @ap: target ATA port
813 *
814 * This function is called when @ap is resumed - right after port
815 * itself is resumed but before any EH action is taken.
816 *
817 * LOCKING:
818 * EH context.
819 */
820void ata_acpi_on_resume(struct ata_port *ap)
821{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900822 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Tejun Heof58229f2007-08-06 18:36:23 +0900823 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700824
Aaron Luf1bc1e42013-08-23 10:17:54 +0800825 if (ACPI_HANDLE(&ap->tdev) && gtm) {
Tejun Heo398e0782007-12-15 15:05:03 +0900826 /* _GTM valid */
827
828 /* restore timing parameters */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900829 ata_acpi_stm(ap, gtm);
Tejun Heo64578a32007-05-15 03:28:16 +0900830
Tejun Heo398e0782007-12-15 15:05:03 +0900831 /* _GTF should immediately follow _STM so that it can
832 * use values set by _STM. Cache _GTF result and
833 * schedule _GTF.
834 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900835 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900836 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800837 if (ata_dev_enabled(dev) &&
838 ata_dev_get_GTF(dev, NULL) >= 0)
Tejun Heo398e0782007-12-15 15:05:03 +0900839 dev->flags |= ATA_DFLAG_ACPI_PENDING;
840 }
841 } else {
842 /* SATA _GTF needs to be evaulated after _SDD and
843 * there's no reason to evaluate IDE _GTF early
844 * without _STM. Clear cache and schedule _GTF.
845 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900846 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900847 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800848 if (ata_dev_enabled(dev))
849 dev->flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo398e0782007-12-15 15:05:03 +0900850 }
851 }
Tejun Heo67465442007-05-15 03:28:16 +0900852}
853
Aaron Lua7ff60d2013-01-25 14:29:35 +0800854static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
Shaohua Libd3adca2007-11-02 09:32:38 +0800855{
Aaron Lu21334202013-01-15 17:21:01 +0800856 int d_max_in = ACPI_STATE_D3_COLD;
Aaron Lua7ff60d2013-01-25 14:29:35 +0800857 if (!runtime)
858 goto out;
Aaron Lu21334202013-01-15 17:21:01 +0800859
860 /*
861 * For ATAPI, runtime D3 cold is only allowed
862 * for ZPODD in zero power ready state
863 */
864 if (dev->class == ATA_DEV_ATAPI &&
865 !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
866 d_max_in = ACPI_STATE_D3_HOT;
867
Aaron Lua7ff60d2013-01-25 14:29:35 +0800868out:
Aaron Luf1bc1e42013-08-23 10:17:54 +0800869 return acpi_pm_device_sleep_state(&dev->tdev, NULL, d_max_in);
Aaron Lu21334202013-01-15 17:21:01 +0800870}
871
Aaron Lua7ff60d2013-01-25 14:29:35 +0800872static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
873{
874 bool runtime = PMSG_IS_AUTO(state);
Shaohua Libd3adca2007-11-02 09:32:38 +0800875 struct ata_device *dev;
Lin Mingfebe53b2012-06-25 16:13:05 +0800876 acpi_handle handle;
Lin Ming3bd46602012-06-25 16:13:06 +0800877 int acpi_state;
Shaohua Libd3adca2007-11-02 09:32:38 +0800878
Tejun Heo1eca4362008-11-03 20:03:17 +0900879 ata_for_each_dev(dev, &ap->link, ENABLED) {
Lin Mingfebe53b2012-06-25 16:13:05 +0800880 handle = ata_dev_acpi_handle(dev);
Lin Ming3bd46602012-06-25 16:13:06 +0800881 if (!handle)
882 continue;
883
Aaron Lua7ff60d2013-01-25 14:29:35 +0800884 if (!(state.event & PM_EVENT_RESUME)) {
885 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
886 if (acpi_state == ACPI_STATE_D0)
887 continue;
888 if (runtime && zpodd_dev_enabled(dev) &&
889 acpi_state == ACPI_STATE_D3_COLD)
890 zpodd_enable_run_wake(dev);
891 acpi_bus_set_power(handle, acpi_state);
Lin Ming3bd46602012-06-25 16:13:06 +0800892 } else {
Aaron Lua7ff60d2013-01-25 14:29:35 +0800893 if (runtime && zpodd_dev_enabled(dev))
894 zpodd_disable_run_wake(dev);
Lin Ming3bd46602012-06-25 16:13:06 +0800895 acpi_bus_set_power(handle, ACPI_STATE_D0);
896 }
Shaohua Libd3adca2007-11-02 09:32:38 +0800897 }
Aaron Lua7ff60d2013-01-25 14:29:35 +0800898}
Lin Mingfebe53b2012-06-25 16:13:05 +0800899
Aaron Lua7ff60d2013-01-25 14:29:35 +0800900/* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
901static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
902{
903 struct ata_device *dev;
904 acpi_handle port_handle;
905
Aaron Luf1bc1e42013-08-23 10:17:54 +0800906 port_handle = ACPI_HANDLE(&ap->tdev);
Aaron Lua7ff60d2013-01-25 14:29:35 +0800907 if (!port_handle)
908 return;
909
910 /* channel first and then drives for power on and vica versa
911 for power off */
912 if (state.event & PM_EVENT_RESUME)
913 acpi_bus_set_power(port_handle, ACPI_STATE_D0);
914
915 ata_for_each_dev(dev, &ap->link, ENABLED) {
916 acpi_handle dev_handle = ata_dev_acpi_handle(dev);
917 if (!dev_handle)
918 continue;
919
920 acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +0200921 ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
Aaron Lua7ff60d2013-01-25 14:29:35 +0800922 }
923
924 if (!(state.event & PM_EVENT_RESUME))
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +0200925 acpi_bus_set_power(port_handle, ACPI_STATE_D3_COLD);
Aaron Lua7ff60d2013-01-25 14:29:35 +0800926}
927
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700928/**
929 * ata_acpi_set_state - set the port power state
930 * @ap: target ATA port
931 * @state: state, on/off
932 *
Aaron Lua7ff60d2013-01-25 14:29:35 +0800933 * This function sets a proper ACPI D state for the device on
934 * system and runtime PM operations.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700935 */
936void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
937{
Aaron Lua7ff60d2013-01-25 14:29:35 +0800938 if (ap->flags & ATA_FLAG_ACPI_SATA)
939 sata_acpi_set_state(ap, state);
940 else
941 pata_acpi_set_state(ap, state);
Shaohua Libd3adca2007-11-02 09:32:38 +0800942}
943
944/**
Tejun Heo67465442007-05-15 03:28:16 +0900945 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
946 * @dev: target ATA device
947 *
948 * This function is called when @dev is about to be configured.
949 * IDENTIFY data might have been modified after this hook is run.
950 *
951 * LOCKING:
952 * EH context.
953 *
954 * RETURNS:
955 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
956 * -errno on failure.
957 */
958int ata_acpi_on_devcfg(struct ata_device *dev)
959{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900960 struct ata_port *ap = dev->link->ap;
961 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900962 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
Tejun Heo66fa7f22007-12-15 15:05:04 +0900963 int nr_executed = 0;
Tejun Heo67465442007-05-15 03:28:16 +0900964 int rc;
965
Matthew Garrett30dcf762012-06-25 16:13:04 +0800966 if (!ata_dev_acpi_handle(dev))
Tejun Heo67465442007-05-15 03:28:16 +0900967 return 0;
968
969 /* do we need to do _GTF? */
970 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
971 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
972 return 0;
973
974 /* do _SDD if SATA */
975 if (acpi_sata) {
976 rc = ata_acpi_push_id(dev);
Tejun Heof2406772009-11-18 22:24:21 +0900977 if (rc && rc != -ENOENT)
Tejun Heo67465442007-05-15 03:28:16 +0900978 goto acpi_err;
979 }
980
981 /* do _GTF */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900982 rc = ata_acpi_exec_tfs(dev, &nr_executed);
983 if (rc)
Tejun Heo67465442007-05-15 03:28:16 +0900984 goto acpi_err;
985
986 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
987
988 /* refresh IDENTIFY page if any _GTF command has been executed */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900989 if (nr_executed) {
Tejun Heo67465442007-05-15 03:28:16 +0900990 rc = ata_dev_reread_id(dev, 0);
991 if (rc < 0) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700992 ata_dev_err(dev,
993 "failed to IDENTIFY after ACPI commands\n");
Tejun Heo67465442007-05-15 03:28:16 +0900994 return rc;
995 }
996 }
997
998 return 0;
999
1000 acpi_err:
Tejun Heo66fa7f22007-12-15 15:05:04 +09001001 /* ignore evaluation failure if we can continue safely */
1002 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1003 return 0;
Tejun Heo67465442007-05-15 03:28:16 +09001004
Tejun Heo66fa7f22007-12-15 15:05:04 +09001005 /* fail and let EH retry once more for unknown IO errors */
1006 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1007 dev->flags |= ATA_DFLAG_ACPI_FAILED;
1008 return rc;
Tejun Heo67465442007-05-15 03:28:16 +09001009 }
Tejun Heo66fa7f22007-12-15 15:05:04 +09001010
Aaron Lu0d0cdb02012-11-26 13:55:25 +08001011 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
Joe Perchesa9a79df2011-04-15 15:51:59 -07001012 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
Tejun Heo66fa7f22007-12-15 15:05:04 +09001013
1014 /* We can safely continue if no _GTF command has been executed
1015 * and port is not frozen.
1016 */
1017 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1018 return 0;
1019
Tejun Heo67465442007-05-15 03:28:16 +09001020 return rc;
1021}
Tejun Heo562f0c22007-12-15 15:05:01 +09001022
1023/**
1024 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1025 * @dev: target ATA device
1026 *
1027 * This function is called when @dev is about to be disabled.
1028 *
1029 * LOCKING:
1030 * EH context.
1031 */
1032void ata_acpi_on_disable(struct ata_device *dev)
1033{
Tejun Heo398e0782007-12-15 15:05:03 +09001034 ata_acpi_clear_gtf(dev);
Tejun Heo562f0c22007-12-15 15:05:01 +09001035}