blob: 6fc67f7efb224c64d076cf3679bd8878e8d278a1 [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 Lu83400912012-08-15 17:08:12 +080063 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), ap->port_no);
Matthew Garrett6b66d952012-06-25 16:13:03 +080064}
Matthew Garrett30dcf762012-06-25 16:13:04 +080065EXPORT_SYMBOL(ata_ap_acpi_handle);
Matthew Garrett6b66d952012-06-25 16:13:03 +080066
Matthew Garrett30dcf762012-06-25 16:13:04 +080067/**
68 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
69 * @dev: the acpi_device returned will correspond to this port
70 *
71 * Returns the acpi_handle for the ACPI namespace object corresponding to
72 * the ata_device passed into the function, or NULL if no such object exists
73 */
74acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
Matthew Garrett6b66d952012-06-25 16:13:03 +080075{
76 acpi_integer adr;
77 struct ata_port *ap = dev->link->ap;
78
Aaron Lu0d0cdb02012-11-26 13:55:25 +080079 if (dev->flags & ATA_DFLAG_ACPI_DISABLED)
80 return NULL;
81
Matthew Garrett6b66d952012-06-25 16:13:03 +080082 if (ap->flags & ATA_FLAG_ACPI_SATA) {
83 if (!sata_pmp_attached(ap))
84 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
85 else
86 adr = SATA_ADR(ap->port_no, dev->link->pmp);
87 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
88 } else
Matthew Garrett30dcf762012-06-25 16:13:04 +080089 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
Matthew Garrett6b66d952012-06-25 16:13:03 +080090}
Matthew Garrett30dcf762012-06-25 16:13:04 +080091EXPORT_SYMBOL(ata_dev_acpi_handle);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070092
Holger Macht664d0802008-06-03 20:27:59 +020093/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
94static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
95{
96 if (dev)
97 dev->flags |= ATA_DFLAG_DETACH;
98 else {
99 struct ata_link *tlink;
100 struct ata_device *tdev;
101
Tejun Heo1eca4362008-11-03 20:03:17 +0900102 ata_for_each_link(tlink, ap, EDGE)
103 ata_for_each_dev(tdev, tlink, ALL)
Holger Macht664d0802008-06-03 20:27:59 +0200104 tdev->flags |= ATA_DFLAG_DETACH;
105 }
106
107 ata_port_schedule_eh(ap);
108}
109
110/**
111 * ata_acpi_handle_hotplug - ACPI event handler backend
112 * @ap: ATA port ACPI event occurred
113 * @dev: ATA device ACPI event occurred (can be NULL)
114 * @event: ACPI event which occurred
Holger Macht664d0802008-06-03 20:27:59 +0200115 *
116 * All ACPI bay / device realted events end up in this function. If
117 * the event is port-wide @dev is NULL. If the event is specific to a
118 * device, @dev points to it.
119 *
120 * Hotplug (as opposed to unplug) notification is always handled as
121 * port-wide while unplug only kills the target device on device-wide
122 * event.
123 *
124 * LOCKING:
125 * ACPI notify handler context. May sleep.
126 */
127static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
Shaohua Lif730ae12008-08-28 10:05:45 +0800128 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +0100129{
Holger Macht664d0802008-06-03 20:27:59 +0200130 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo233f1122008-03-12 14:24:43 +0900131 int wait = 0;
132 unsigned long flags;
Zhang Rui3c1e3892008-07-11 09:42:03 -0400133
Holger Macht664d0802008-06-03 20:27:59 +0200134 spin_lock_irqsave(ap->lock, flags);
Shaohua Lif730ae12008-08-28 10:05:45 +0800135 /*
136 * When dock driver calls into the routine, it will always use
137 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
138 * ACPI_NOTIFY_EJECT_REQUEST for remove
139 */
Tejun Heo233f1122008-03-12 14:24:43 +0900140 switch (event) {
141 case ACPI_NOTIFY_BUS_CHECK:
142 case ACPI_NOTIFY_DEVICE_CHECK:
143 ata_ehi_push_desc(ehi, "ACPI event");
Jeff Garzikc85665f2008-05-19 17:56:10 -0400144
Shaohua Lif730ae12008-08-28 10:05:45 +0800145 ata_ehi_hotplugged(ehi);
146 ata_port_freeze(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200147 break;
148 case ACPI_NOTIFY_EJECT_REQUEST:
149 ata_ehi_push_desc(ehi, "ACPI event");
150
Holger Macht664d0802008-06-03 20:27:59 +0200151 ata_acpi_detach_device(ap, dev);
152 wait = 1;
153 break;
Matthew Garrett237d8442007-10-03 01:24:16 +0100154 }
155
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100156 spin_unlock_irqrestore(ap->lock, flags);
157
Shaohua Lif730ae12008-08-28 10:05:45 +0800158 if (wait)
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100159 ata_port_wait_eh(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200160}
161
162static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
163{
164 struct ata_device *dev = data;
165
Shaohua Lif730ae12008-08-28 10:05:45 +0800166 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
Holger Macht664d0802008-06-03 20:27:59 +0200167}
168
169static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
170{
171 struct ata_port *ap = data;
172
Shaohua Lif730ae12008-08-28 10:05:45 +0800173 ata_acpi_handle_hotplug(ap, NULL, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100174}
175
Shaohua Li1253f7a2008-08-28 10:06:16 +0800176static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
177 u32 event)
178{
179 struct kobject *kobj = NULL;
180 char event_string[20];
181 char *envp[] = { event_string, NULL };
182
183 if (dev) {
184 if (dev->sdev)
185 kobj = &dev->sdev->sdev_gendev.kobj;
186 } else
187 kobj = &ap->dev->kobj;
188
189 if (kobj) {
190 snprintf(event_string, 20, "BAY_EVENT=%d", event);
191 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
192 }
193}
194
195static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
196{
197 ata_acpi_uevent(data, NULL, event);
198}
199
200static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
201{
202 struct ata_device *dev = data;
203 ata_acpi_uevent(dev->link->ap, dev, event);
204}
205
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400206static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800207 .handler = ata_acpi_dev_notify_dock,
208 .uevent = ata_acpi_dev_uevent,
209};
210
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400211static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800212 .handler = ata_acpi_ap_notify_dock,
213 .uevent = ata_acpi_ap_uevent,
214};
215
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700216/**
Tejun Heo562f0c22007-12-15 15:05:01 +0900217 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
218 * @host: target ATA host
219 *
220 * This function is called during driver detach after the whole host
221 * is shut down.
222 *
223 * LOCKING:
224 * EH context.
225 */
226void ata_acpi_dissociate(struct ata_host *host)
227{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900228 int i;
229
230 /* Restore initial _GTM values so that driver which attaches
231 * afterward can use them too.
232 */
233 for (i = 0; i < host->n_ports; i++) {
234 struct ata_port *ap = host->ports[i];
235 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
236
Matthew Garrett30dcf762012-06-25 16:13:04 +0800237 if (ata_ap_acpi_handle(ap) && gtm)
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900238 ata_acpi_stm(ap, gtm);
239 }
Tejun Heo562f0c22007-12-15 15:05:01 +0900240}
241
242/**
Tejun Heo64578a32007-05-15 03:28:16 +0900243 * ata_acpi_gtm - execute _GTM
244 * @ap: target ATA port
245 * @gtm: out parameter for _GTM result
246 *
247 * Evaluate _GTM and store the result in @gtm.
248 *
249 * LOCKING:
250 * EH context.
251 *
252 * RETURNS:
253 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
254 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900255int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
Tejun Heo64578a32007-05-15 03:28:16 +0900256{
257 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
258 union acpi_object *out_obj;
259 acpi_status status;
260 int rc = 0;
261
Matthew Garrett30dcf762012-06-25 16:13:04 +0800262 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
263 &output);
Tejun Heo64578a32007-05-15 03:28:16 +0900264
265 rc = -ENOENT;
266 if (status == AE_NOT_FOUND)
267 goto out_free;
268
269 rc = -EINVAL;
270 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700271 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
272 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900273 goto out_free;
274 }
275
276 out_obj = output.pointer;
277 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700278 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
279 out_obj->type);
Tejun Heo64578a32007-05-15 03:28:16 +0900280
281 goto out_free;
282 }
283
284 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700285 ata_port_err(ap, "_GTM returned invalid length %d\n",
286 out_obj->buffer.length);
Tejun Heo64578a32007-05-15 03:28:16 +0900287 goto out_free;
288 }
289
290 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
291 rc = 0;
292 out_free:
293 kfree(output.pointer);
294 return rc;
295}
296
Alan Coxbadff032007-10-04 21:28:18 +0100297EXPORT_SYMBOL_GPL(ata_acpi_gtm);
298
Tejun Heo64578a32007-05-15 03:28:16 +0900299/**
300 * ata_acpi_stm - execute _STM
301 * @ap: target ATA port
302 * @stm: timing parameter to _STM
303 *
304 * Evaluate _STM with timing parameter @stm.
305 *
306 * LOCKING:
307 * EH context.
308 *
309 * RETURNS:
310 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
311 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900312int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a32007-05-15 03:28:16 +0900313{
314 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900315 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a32007-05-15 03:28:16 +0900316 struct acpi_object_list input;
317 union acpi_object in_params[3];
318
319 in_params[0].type = ACPI_TYPE_BUFFER;
320 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900321 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a32007-05-15 03:28:16 +0900322 /* Buffers for id may need byteswapping ? */
323 in_params[1].type = ACPI_TYPE_BUFFER;
324 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900325 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900326 in_params[2].type = ACPI_TYPE_BUFFER;
327 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900328 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900329
330 input.count = 3;
331 input.pointer = in_params;
332
Matthew Garrett30dcf762012-06-25 16:13:04 +0800333 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
334 NULL);
Tejun Heo64578a32007-05-15 03:28:16 +0900335
336 if (status == AE_NOT_FOUND)
337 return -ENOENT;
338 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700339 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
340 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900341 return -EINVAL;
342 }
343 return 0;
344}
345
Alan Coxbadff032007-10-04 21:28:18 +0100346EXPORT_SYMBOL_GPL(ata_acpi_stm);
347
Tejun Heo64578a32007-05-15 03:28:16 +0900348/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900349 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900350 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900351 * @gtf: output parameter for buffer containing _GTF taskfile arrays
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700352 *
353 * This applies to both PATA and SATA drives.
354 *
355 * The _GTF method has no input parameters.
356 * It returns a variable number of register set values (registers
357 * hex 1F1..1F7, taskfiles).
358 * The <variable number> is not known in advance, so have ACPI-CA
359 * allocate the buffer as needed and return it, then free it later.
360 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900361 * LOCKING:
362 * EH context.
363 *
364 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900365 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
366 * if _GTF is invalid.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700367 */
Tejun Heo398e0782007-12-15 15:05:03 +0900368static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700369{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900370 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900371 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900372 struct acpi_buffer output;
373 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900374 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700375
Tejun Heo398e0782007-12-15 15:05:03 +0900376 /* if _GTF is cached, use the cached value */
377 if (dev->gtf_cache) {
378 out_obj = dev->gtf_cache;
379 goto done;
380 }
381
Tejun Heo4700c4b2007-05-15 03:28:16 +0900382 /* set up output buffer */
383 output.length = ACPI_ALLOCATE_BUFFER;
384 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700385
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700386 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700387 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
388 __func__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700389
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700390 /* _GTF has no input parameters */
Matthew Garrett30dcf762012-06-25 16:13:04 +0800391 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
392 &output);
Tejun Heo398e0782007-12-15 15:05:03 +0900393 out_obj = dev->gtf_cache = output.pointer;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900394
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700395 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900396 if (status != AE_NOT_FOUND) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700397 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
398 status);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900399 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900400 }
401 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700402 }
403
404 if (!output.length || !output.pointer) {
405 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700406 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
407 __func__,
408 (unsigned long long)output.length,
409 output.pointer);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900410 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900411 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700412 }
413
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700414 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700415 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
416 out_obj->type);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900417 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900418 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700419 }
420
Tejun Heo4700c4b2007-05-15 03:28:16 +0900421 if (out_obj->buffer.length % REGS_PER_GTF) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700422 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
423 out_obj->buffer.length);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900424 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900425 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700426 }
427
Tejun Heo398e0782007-12-15 15:05:03 +0900428 done:
Tejun Heo4700c4b2007-05-15 03:28:16 +0900429 rc = out_obj->buffer.length / REGS_PER_GTF;
Tejun Heo398e0782007-12-15 15:05:03 +0900430 if (gtf) {
431 *gtf = (void *)out_obj->buffer.pointer;
432 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700433 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
434 __func__, *gtf, rc);
Tejun Heo398e0782007-12-15 15:05:03 +0900435 }
Tejun Heo4700c4b2007-05-15 03:28:16 +0900436 return rc;
437
438 out_free:
Tejun Heo398e0782007-12-15 15:05:03 +0900439 ata_acpi_clear_gtf(dev);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900440 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700441}
442
Tejun Heo7c77fa42007-12-18 16:33:03 +0900443/**
444 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
445 * @dev: target device
446 * @gtm: GTM parameter to use
447 *
448 * Determine xfermask for @dev from @gtm.
449 *
450 * LOCKING:
451 * None.
452 *
453 * RETURNS:
454 * Determined xfermask.
455 */
456unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
457 const struct ata_acpi_gtm *gtm)
458{
Tejun Heoa0f79b92007-12-18 16:33:05 +0900459 unsigned long xfer_mask = 0;
460 unsigned int type;
461 int unit;
462 u8 mode;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900463
464 /* we always use the 0 slot for crap hardware */
465 unit = dev->devno;
466 if (!(gtm->flags & 0x10))
467 unit = 0;
468
Tejun Heoa0f79b92007-12-18 16:33:05 +0900469 /* PIO */
470 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
471 xfer_mask |= ata_xfer_mode2mask(mode);
Tejun Heo7c77fa42007-12-18 16:33:03 +0900472
473 /* See if we have MWDMA or UDMA data. We don't bother with
474 * MWDMA if UDMA is available as this means the BIOS set UDMA
475 * and our error changedown if it works is UDMA to PIO anyway.
476 */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900477 if (!(gtm->flags & (1 << (2 * unit))))
478 type = ATA_SHIFT_MWDMA;
479 else
480 type = ATA_SHIFT_UDMA;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900481
Tejun Heoa0f79b92007-12-18 16:33:05 +0900482 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
483 xfer_mask |= ata_xfer_mode2mask(mode);
484
485 return xfer_mask;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900486}
487EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
488
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700489/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400490 * ata_acpi_cbl_80wire - Check for 80 wire cable
491 * @ap: Port to check
Tejun Heo021ee9a2007-12-18 16:33:06 +0900492 * @gtm: GTM data to use
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400493 *
Tejun Heo021ee9a2007-12-18 16:33:06 +0900494 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400495 */
Tejun Heo021ee9a2007-12-18 16:33:06 +0900496int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400497{
Tejun Heo021ee9a2007-12-18 16:33:06 +0900498 struct ata_device *dev;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400499
Tejun Heo1eca4362008-11-03 20:03:17 +0900500 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo021ee9a2007-12-18 16:33:06 +0900501 unsigned long xfer_mask, udma_mask;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400502
Tejun Heo021ee9a2007-12-18 16:33:06 +0900503 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
504 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
505
506 if (udma_mask & ~ATA_UDMA_MASK_40C)
507 return 1;
508 }
509
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400510 return 0;
511}
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400512EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
513
Tejun Heo3264a8d2007-12-15 15:05:06 +0900514static void ata_acpi_gtf_to_tf(struct ata_device *dev,
515 const struct ata_acpi_gtf *gtf,
516 struct ata_taskfile *tf)
517{
518 ata_tf_init(dev, tf);
519
520 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
521 tf->protocol = ATA_PROT_NODATA;
522 tf->feature = gtf->tf[0]; /* 0x1f1 */
523 tf->nsect = gtf->tf[1]; /* 0x1f2 */
524 tf->lbal = gtf->tf[2]; /* 0x1f3 */
525 tf->lbam = gtf->tf[3]; /* 0x1f4 */
526 tf->lbah = gtf->tf[4]; /* 0x1f5 */
527 tf->device = gtf->tf[5]; /* 0x1f6 */
528 tf->command = gtf->tf[6]; /* 0x1f7 */
529}
530
Tejun Heo110f66d2009-09-16 04:17:28 +0900531static int ata_acpi_filter_tf(struct ata_device *dev,
532 const struct ata_taskfile *tf,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900533 const struct ata_taskfile *ptf)
534{
Tejun Heo110f66d2009-09-16 04:17:28 +0900535 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900536 /* libata doesn't use ACPI to configure transfer mode.
537 * It will only confuse device configuration. Skip.
538 */
539 if (tf->command == ATA_CMD_SET_FEATURES &&
540 tf->feature == SETFEATURES_XFER)
541 return 1;
542 }
543
Tejun Heo110f66d2009-09-16 04:17:28 +0900544 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900545 /* BIOS writers, sorry but we don't wanna lock
546 * features unless the user explicitly said so.
547 */
548
549 /* DEVICE CONFIGURATION FREEZE LOCK */
550 if (tf->command == ATA_CMD_CONF_OVERLAY &&
551 tf->feature == ATA_DCO_FREEZE_LOCK)
552 return 1;
553
554 /* SECURITY FREEZE LOCK */
555 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
556 return 1;
557
558 /* SET MAX LOCK and SET MAX FREEZE LOCK */
559 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
560 tf->command == ATA_CMD_SET_MAX &&
561 (tf->feature == ATA_SET_MAX_LOCK ||
562 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
563 return 1;
564 }
565
Tejun Heofa5b5612009-09-16 04:17:02 +0900566 if (tf->command == ATA_CMD_SET_FEATURES &&
567 tf->feature == SETFEATURES_SATA_ENABLE) {
Tejun Heob3449912008-07-06 23:15:03 +0900568 /* inhibit enabling DIPM */
Tejun Heo110f66d2009-09-16 04:17:28 +0900569 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
Tejun Heob3449912008-07-06 23:15:03 +0900570 tf->nsect == SATA_DIPM)
571 return 1;
Tejun Heofa5b5612009-09-16 04:17:02 +0900572
573 /* inhibit FPDMA non-zero offset */
Tejun Heo110f66d2009-09-16 04:17:28 +0900574 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900575 (tf->nsect == SATA_FPDMA_OFFSET ||
576 tf->nsect == SATA_FPDMA_IN_ORDER))
577 return 1;
578
579 /* inhibit FPDMA auto activation */
Tejun Heo110f66d2009-09-16 04:17:28 +0900580 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900581 tf->nsect == SATA_FPDMA_AA)
582 return 1;
Tejun Heob3449912008-07-06 23:15:03 +0900583 }
584
Tejun Heo3264a8d2007-12-15 15:05:06 +0900585 return 0;
586}
587
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400588/**
Tejun Heo0e8634b2007-12-15 15:05:05 +0900589 * ata_acpi_run_tf - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900590 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700591 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
592 *
Sergei Shtylyov3696df32011-02-04 22:04:17 +0300593 * Outputs ATA taskfile to standard ATA host controller.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700594 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
595 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
596 * hob_lbal, hob_lbam, and hob_lbah.
597 *
598 * This function waits for idle (!BUSY and !DRQ) after writing
599 * registers. If the control register has a new value, this
600 * function also waits for idle after writing control and before
601 * writing the remaining registers.
602 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900603 * LOCKING:
604 * EH context.
605 *
606 * RETURNS:
Tejun Heo3264a8d2007-12-15 15:05:06 +0900607 * 1 if command is executed successfully. 0 if ignored, rejected or
608 * filtered out, -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700609 */
Tejun Heo0e8634b2007-12-15 15:05:05 +0900610static int ata_acpi_run_tf(struct ata_device *dev,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900611 const struct ata_acpi_gtf *gtf,
612 const struct ata_acpi_gtf *prev_gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700613{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900614 struct ata_taskfile *pptf = NULL;
615 struct ata_taskfile tf, ptf, rtf;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900616 unsigned int err_mask;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900617 const char *level;
Robert Hancock65211482009-07-14 20:43:39 -0600618 const char *descr;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900619 char msg[60];
620 int rc;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500621
Tejun Heo4700c4b2007-05-15 03:28:16 +0900622 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
623 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
624 && (gtf->tf[6] == 0))
625 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700626
Tejun Heo3264a8d2007-12-15 15:05:06 +0900627 ata_acpi_gtf_to_tf(dev, gtf, &tf);
628 if (prev_gtf) {
629 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
630 pptf = &ptf;
631 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700632
Tejun Heo110f66d2009-09-16 04:17:28 +0900633 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900634 rtf = tf;
635 err_mask = ata_exec_internal(dev, &rtf, NULL,
636 DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700637
Tejun Heo3264a8d2007-12-15 15:05:06 +0900638 switch (err_mask) {
639 case 0:
640 level = KERN_DEBUG;
641 snprintf(msg, sizeof(msg), "succeeded");
642 rc = 1;
643 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900644
Tejun Heo3264a8d2007-12-15 15:05:06 +0900645 case AC_ERR_DEV:
646 level = KERN_INFO;
647 snprintf(msg, sizeof(msg),
648 "rejected by device (Stat=0x%02x Err=0x%02x)",
649 rtf.command, rtf.feature);
650 rc = 0;
651 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900652
Tejun Heo3264a8d2007-12-15 15:05:06 +0900653 default:
654 level = KERN_ERR;
655 snprintf(msg, sizeof(msg),
656 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
657 err_mask, rtf.command, rtf.feature);
658 rc = -EIO;
659 break;
660 }
661 } else {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900662 level = KERN_INFO;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900663 snprintf(msg, sizeof(msg), "filtered out");
Tejun Heo0e8634b2007-12-15 15:05:05 +0900664 rc = 0;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900665 }
Robert Hancock65211482009-07-14 20:43:39 -0600666 descr = ata_get_cmd_descript(tf.command);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900667
Tejun Heo0e8634b2007-12-15 15:05:05 +0900668 ata_dev_printk(dev, level,
Robert Hancock65211482009-07-14 20:43:39 -0600669 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
Tejun Heo0e8634b2007-12-15 15:05:05 +0900670 tf.command, tf.feature, tf.nsect, tf.lbal,
Robert Hancock65211482009-07-14 20:43:39 -0600671 tf.lbam, tf.lbah, tf.device,
672 (descr ? descr : "unknown"), msg);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900673
674 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700675}
676
677/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700678 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900679 * @dev: target ATA device
Martin Olsson98a17082009-04-22 18:21:29 +0200680 * @nr_executed: out parameter for the number of executed commands
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700681 *
Martin Olsson98a17082009-04-22 18:21:29 +0200682 * Evaluate _GTF and execute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900683 *
684 * LOCKING:
685 * EH context.
686 *
687 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900688 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
689 * -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700690 */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900691static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700692{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900693 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
Tejun Heo67465442007-05-15 03:28:16 +0900694 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700695
Tejun Heo67465442007-05-15 03:28:16 +0900696 /* get taskfiles */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900697 rc = ata_dev_get_GTF(dev, &gtf);
698 if (rc < 0)
699 return rc;
700 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700701
Tejun Heo67465442007-05-15 03:28:16 +0900702 /* execute them */
Tejun Heo3264a8d2007-12-15 15:05:06 +0900703 for (i = 0; i < gtf_count; i++, gtf++) {
704 rc = ata_acpi_run_tf(dev, gtf, pgtf);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900705 if (rc < 0)
706 break;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900707 if (rc) {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900708 (*nr_executed)++;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900709 pgtf = gtf;
710 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700711 }
712
Tejun Heo398e0782007-12-15 15:05:03 +0900713 ata_acpi_clear_gtf(dev);
Tejun Heo67465442007-05-15 03:28:16 +0900714
Tejun Heo0e8634b2007-12-15 15:05:05 +0900715 if (rc < 0)
716 return rc;
717 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700718}
719
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700720/**
721 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900722 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700723 *
724 * _SDD ACPI object: for SATA mode only
725 * Must be after Identify (Packet) Device -- uses its data
726 * ATM this function never returns a failure. It is an optional
727 * method and if it fails for whatever reason, we should still
728 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900729 *
730 * LOCKING:
731 * EH context.
732 *
733 * RETURNS:
Tejun Heof2406772009-11-18 22:24:21 +0900734 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700735 */
Tejun Heo67465442007-05-15 03:28:16 +0900736static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700737{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900738 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900739 acpi_status status;
740 struct acpi_object_list input;
741 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700742
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700743 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700744 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
745 __func__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700746
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700747 /* Give the drive Identify data to the drive via the _SDD method */
748 /* _SDD: set up input parameters */
749 input.count = 1;
750 input.pointer = in_params;
751 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900752 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
753 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700754 /* Output buffer: _SDD has no output */
755
756 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900757 swap_buf_le16(dev->id, ATA_ID_WORDS);
Matthew Garrett30dcf762012-06-25 16:13:04 +0800758 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
759 NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900760 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700761
Tejun Heof2406772009-11-18 22:24:21 +0900762 if (status == AE_NOT_FOUND)
763 return -ENOENT;
764
765 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700766 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
Tejun Heof2406772009-11-18 22:24:21 +0900767 return -EIO;
768 }
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700769
Tejun Heof2406772009-11-18 22:24:21 +0900770 return 0;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700771}
772
Tejun Heo67465442007-05-15 03:28:16 +0900773/**
Tejun Heo64578a32007-05-15 03:28:16 +0900774 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
775 * @ap: target ATA port
776 *
777 * This function is called when @ap is about to be suspended. All
778 * devices are already put to sleep but the port_suspend() callback
779 * hasn't been executed yet. Error return from this function aborts
780 * suspend.
781 *
782 * LOCKING:
783 * EH context.
784 *
785 * RETURNS:
786 * 0 on success, -errno on failure.
787 */
788int ata_acpi_on_suspend(struct ata_port *ap)
789{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900790 /* nada */
791 return 0;
Tejun Heo64578a32007-05-15 03:28:16 +0900792}
793
794/**
Tejun Heo67465442007-05-15 03:28:16 +0900795 * ata_acpi_on_resume - ATA ACPI hook called on resume
796 * @ap: target ATA port
797 *
798 * This function is called when @ap is resumed - right after port
799 * itself is resumed but before any EH action is taken.
800 *
801 * LOCKING:
802 * EH context.
803 */
804void ata_acpi_on_resume(struct ata_port *ap)
805{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900806 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Tejun Heof58229f2007-08-06 18:36:23 +0900807 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700808
Matthew Garrett30dcf762012-06-25 16:13:04 +0800809 if (ata_ap_acpi_handle(ap) && gtm) {
Tejun Heo398e0782007-12-15 15:05:03 +0900810 /* _GTM valid */
811
812 /* restore timing parameters */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900813 ata_acpi_stm(ap, gtm);
Tejun Heo64578a32007-05-15 03:28:16 +0900814
Tejun Heo398e0782007-12-15 15:05:03 +0900815 /* _GTF should immediately follow _STM so that it can
816 * use values set by _STM. Cache _GTF result and
817 * schedule _GTF.
818 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900819 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900820 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800821 if (ata_dev_enabled(dev) &&
822 ata_dev_get_GTF(dev, NULL) >= 0)
Tejun Heo398e0782007-12-15 15:05:03 +0900823 dev->flags |= ATA_DFLAG_ACPI_PENDING;
824 }
825 } else {
826 /* SATA _GTF needs to be evaulated after _SDD and
827 * there's no reason to evaluate IDE _GTF early
828 * without _STM. Clear cache and schedule _GTF.
829 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900830 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900831 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800832 if (ata_dev_enabled(dev))
833 dev->flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo398e0782007-12-15 15:05:03 +0900834 }
835 }
Tejun Heo67465442007-05-15 03:28:16 +0900836}
837
838/**
Shaohua Libd3adca2007-11-02 09:32:38 +0800839 * ata_acpi_set_state - set the port power state
840 * @ap: target ATA port
841 * @state: state, on/off
842 *
843 * This function executes the _PS0/_PS3 ACPI method to set the power state.
844 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
845 */
846void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
847{
848 struct ata_device *dev;
Lin Mingfebe53b2012-06-25 16:13:05 +0800849 acpi_handle handle;
Lin Ming3bd46602012-06-25 16:13:06 +0800850 int acpi_state;
Shaohua Libd3adca2007-11-02 09:32:38 +0800851
852 /* channel first and then drives for power on and vica versa
853 for power off */
Lin Mingfebe53b2012-06-25 16:13:05 +0800854 handle = ata_ap_acpi_handle(ap);
855 if (handle && state.event == PM_EVENT_ON)
856 acpi_bus_set_power(handle, ACPI_STATE_D0);
Shaohua Libd3adca2007-11-02 09:32:38 +0800857
Tejun Heo1eca4362008-11-03 20:03:17 +0900858 ata_for_each_dev(dev, &ap->link, ENABLED) {
Lin Mingfebe53b2012-06-25 16:13:05 +0800859 handle = ata_dev_acpi_handle(dev);
Lin Ming3bd46602012-06-25 16:13:06 +0800860 if (!handle)
861 continue;
862
863 if (state.event != PM_EVENT_ON) {
864 acpi_state = acpi_pm_device_sleep_state(
Stephen Rothwell354b2ea2012-07-02 12:10:41 +1000865 &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
Lin Ming3bd46602012-06-25 16:13:06 +0800866 if (acpi_state > 0)
867 acpi_bus_set_power(handle, acpi_state);
868 /* TBD: need to check if it's runtime pm request */
869 acpi_pm_device_run_wake(
870 &dev->sdev->sdev_gendev, true);
871 } else {
872 /* Ditto */
873 acpi_pm_device_run_wake(
874 &dev->sdev->sdev_gendev, false);
875 acpi_bus_set_power(handle, ACPI_STATE_D0);
876 }
Shaohua Libd3adca2007-11-02 09:32:38 +0800877 }
Lin Mingfebe53b2012-06-25 16:13:05 +0800878
879 handle = ata_ap_acpi_handle(ap);
880 if (handle && state.event != PM_EVENT_ON)
881 acpi_bus_set_power(handle, ACPI_STATE_D3);
Shaohua Libd3adca2007-11-02 09:32:38 +0800882}
883
884/**
Tejun Heo67465442007-05-15 03:28:16 +0900885 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
886 * @dev: target ATA device
887 *
888 * This function is called when @dev is about to be configured.
889 * IDENTIFY data might have been modified after this hook is run.
890 *
891 * LOCKING:
892 * EH context.
893 *
894 * RETURNS:
895 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
896 * -errno on failure.
897 */
898int ata_acpi_on_devcfg(struct ata_device *dev)
899{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900900 struct ata_port *ap = dev->link->ap;
901 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900902 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
Tejun Heo66fa7f22007-12-15 15:05:04 +0900903 int nr_executed = 0;
Tejun Heo67465442007-05-15 03:28:16 +0900904 int rc;
905
Matthew Garrett30dcf762012-06-25 16:13:04 +0800906 if (!ata_dev_acpi_handle(dev))
Tejun Heo67465442007-05-15 03:28:16 +0900907 return 0;
908
909 /* do we need to do _GTF? */
910 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
911 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
912 return 0;
913
914 /* do _SDD if SATA */
915 if (acpi_sata) {
916 rc = ata_acpi_push_id(dev);
Tejun Heof2406772009-11-18 22:24:21 +0900917 if (rc && rc != -ENOENT)
Tejun Heo67465442007-05-15 03:28:16 +0900918 goto acpi_err;
919 }
920
921 /* do _GTF */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900922 rc = ata_acpi_exec_tfs(dev, &nr_executed);
923 if (rc)
Tejun Heo67465442007-05-15 03:28:16 +0900924 goto acpi_err;
925
926 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
927
928 /* refresh IDENTIFY page if any _GTF command has been executed */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900929 if (nr_executed) {
Tejun Heo67465442007-05-15 03:28:16 +0900930 rc = ata_dev_reread_id(dev, 0);
931 if (rc < 0) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700932 ata_dev_err(dev,
933 "failed to IDENTIFY after ACPI commands\n");
Tejun Heo67465442007-05-15 03:28:16 +0900934 return rc;
935 }
936 }
937
938 return 0;
939
940 acpi_err:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900941 /* ignore evaluation failure if we can continue safely */
942 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
943 return 0;
Tejun Heo67465442007-05-15 03:28:16 +0900944
Tejun Heo66fa7f22007-12-15 15:05:04 +0900945 /* fail and let EH retry once more for unknown IO errors */
946 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
947 dev->flags |= ATA_DFLAG_ACPI_FAILED;
948 return rc;
Tejun Heo67465442007-05-15 03:28:16 +0900949 }
Tejun Heo66fa7f22007-12-15 15:05:04 +0900950
Aaron Lu0d0cdb02012-11-26 13:55:25 +0800951 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700952 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
Tejun Heo66fa7f22007-12-15 15:05:04 +0900953
954 /* We can safely continue if no _GTF command has been executed
955 * and port is not frozen.
956 */
957 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
958 return 0;
959
Tejun Heo67465442007-05-15 03:28:16 +0900960 return rc;
961}
Tejun Heo562f0c22007-12-15 15:05:01 +0900962
963/**
964 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
965 * @dev: target ATA device
966 *
967 * This function is called when @dev is about to be disabled.
968 *
969 * LOCKING:
970 * EH context.
971 */
972void ata_acpi_on_disable(struct ata_device *dev)
973{
Tejun Heo398e0782007-12-15 15:05:03 +0900974 ata_acpi_clear_gtf(dev);
Tejun Heo562f0c22007-12-15 15:05:01 +0900975}
Matthew Garrett6b66d952012-06-25 16:13:03 +0800976
Lin Ming3bd46602012-06-25 16:13:06 +0800977static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
978{
979 struct ata_device *ata_dev = context;
980
981 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
982 pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
983 scsi_autopm_get_device(ata_dev->sdev);
984}
985
986static void ata_acpi_add_pm_notifier(struct ata_device *dev)
987{
988 struct acpi_device *acpi_dev;
989 acpi_handle handle;
990 acpi_status status;
991
992 handle = ata_dev_acpi_handle(dev);
993 if (!handle)
994 return;
995
996 status = acpi_bus_get_device(handle, &acpi_dev);
Aaron Lu166a2962012-06-25 16:13:09 +0800997 if (ACPI_FAILURE(status))
998 return;
999
1000 if (dev->sdev->can_power_off) {
Lin Ming3bd46602012-06-25 16:13:06 +08001001 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1002 ata_acpi_wake_dev, dev);
1003 device_set_run_wake(&dev->sdev->sdev_gendev, true);
1004 }
1005}
1006
1007static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
1008{
1009 struct acpi_device *acpi_dev;
1010 acpi_handle handle;
1011 acpi_status status;
1012
1013 handle = ata_dev_acpi_handle(dev);
1014 if (!handle)
1015 return;
1016
1017 status = acpi_bus_get_device(handle, &acpi_dev);
Aaron Lu166a2962012-06-25 16:13:09 +08001018 if (ACPI_FAILURE(status))
1019 return;
1020
1021 if (dev->sdev->can_power_off) {
Lin Ming3bd46602012-06-25 16:13:06 +08001022 device_set_run_wake(&dev->sdev->sdev_gendev, false);
1023 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1024 ata_acpi_wake_dev);
1025 }
1026}
1027
Lin Minga606dac32012-06-25 16:13:07 +08001028static void ata_acpi_register_power_resource(struct ata_device *dev)
1029{
1030 struct scsi_device *sdev = dev->sdev;
1031 acpi_handle handle;
Lin Minga606dac32012-06-25 16:13:07 +08001032
1033 handle = ata_dev_acpi_handle(dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001034 if (handle)
1035 acpi_dev_pm_remove_dependent(handle, &sdev->sdev_gendev);
Lin Minga606dac32012-06-25 16:13:07 +08001036}
1037
1038static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1039{
1040 struct scsi_device *sdev = dev->sdev;
1041 acpi_handle handle;
Lin Minga606dac32012-06-25 16:13:07 +08001042
1043 handle = ata_dev_acpi_handle(dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001044 if (handle)
1045 acpi_dev_pm_remove_dependent(handle, &sdev->sdev_gendev);
Lin Minga606dac32012-06-25 16:13:07 +08001046}
1047
Lin Ming3bd46602012-06-25 16:13:06 +08001048void ata_acpi_bind(struct ata_device *dev)
1049{
1050 ata_acpi_add_pm_notifier(dev);
Lin Minga606dac32012-06-25 16:13:07 +08001051 ata_acpi_register_power_resource(dev);
Lin Ming3bd46602012-06-25 16:13:06 +08001052}
1053
1054void ata_acpi_unbind(struct ata_device *dev)
1055{
1056 ata_acpi_remove_pm_notifier(dev);
Lin Minga606dac32012-06-25 16:13:07 +08001057 ata_acpi_unregister_power_resource(dev);
Lin Ming3bd46602012-06-25 16:13:06 +08001058}
1059
Matthew Garrett6b66d952012-06-25 16:13:03 +08001060static int compat_pci_ata(struct ata_port *ap)
1061{
1062 struct device *dev = ap->tdev.parent;
1063 struct pci_dev *pdev;
1064
1065 if (!is_pci_dev(dev))
1066 return 0;
1067
1068 pdev = to_pci_dev(dev);
1069
1070 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1071 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1072 return 0;
1073
1074 return 1;
1075}
1076
1077static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1078{
1079 if (ap->flags & ATA_FLAG_ACPI_SATA)
1080 return -ENODEV;
1081
1082 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1083 ap->port_no);
1084
1085 if (!*handle)
1086 return -ENODEV;
1087
Aaron Lu83400912012-08-15 17:08:12 +08001088 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
1089 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1090
Matthew Garrett6b66d952012-06-25 16:13:03 +08001091 return 0;
1092}
1093
1094static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1095 acpi_handle *handle)
1096{
1097 struct ata_device *ata_dev;
Aaron Lu166a2962012-06-25 16:13:09 +08001098 acpi_status status;
1099 struct acpi_device *acpi_dev;
1100 struct acpi_device_power_state *states;
Matthew Garrett6b66d952012-06-25 16:13:03 +08001101
Aaron Lu60817a62012-10-09 15:37:48 +08001102 if (ap->flags & ATA_FLAG_ACPI_SATA) {
1103 if (!sata_pmp_attached(ap))
1104 ata_dev = &ap->link.device[sdev->id];
1105 else
1106 ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id];
1107 }
1108 else {
Matthew Garrett6b66d952012-06-25 16:13:03 +08001109 ata_dev = &ap->link.device[sdev->id];
Aaron Lu60817a62012-10-09 15:37:48 +08001110 }
Matthew Garrett6b66d952012-06-25 16:13:03 +08001111
Matthew Garrett30dcf762012-06-25 16:13:04 +08001112 *handle = ata_dev_acpi_handle(ata_dev);
Matthew Garrett6b66d952012-06-25 16:13:03 +08001113
1114 if (!*handle)
1115 return -ENODEV;
1116
Aaron Lu166a2962012-06-25 16:13:09 +08001117 status = acpi_bus_get_device(*handle, &acpi_dev);
1118 if (ACPI_FAILURE(status))
1119 return 0;
1120
1121 /*
1122 * If firmware has _PS3 or _PR3 for this device,
1123 * and this ata ODD device support device attention,
1124 * it means this device can be powered off
1125 */
1126 states = acpi_dev->power.states;
1127 if ((states[ACPI_STATE_D3_HOT].flags.valid ||
1128 states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
1129 ata_dev->flags & ATA_DFLAG_DA)
1130 sdev->can_power_off = 1;
1131
Matthew Garrett6b66d952012-06-25 16:13:03 +08001132 return 0;
1133}
1134
1135static int is_ata_port(const struct device *dev)
1136{
1137 return dev->type == &ata_port_type;
1138}
1139
1140static struct ata_port *dev_to_ata_port(struct device *dev)
1141{
1142 while (!is_ata_port(dev)) {
1143 if (!dev->parent)
1144 return NULL;
1145 dev = dev->parent;
1146 }
1147 return to_ata_port(dev);
1148}
1149
1150static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1151{
1152 struct ata_port *ap = dev_to_ata_port(dev);
1153
1154 if (!ap)
1155 return -ENODEV;
1156
1157 if (!compat_pci_ata(ap))
1158 return -ENODEV;
1159
1160 if (scsi_is_host_device(dev))
1161 return ata_acpi_bind_host(ap, handle);
1162 else if (scsi_is_sdev_device(dev)) {
1163 struct scsi_device *sdev = to_scsi_device(dev);
1164
1165 return ata_acpi_bind_device(ap, sdev, handle);
1166 } else
1167 return -ENODEV;
1168}
1169
1170static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1171{
1172 return -ENODEV;
1173}
1174
1175static struct acpi_bus_type ata_acpi_bus = {
1176 .find_bridge = ata_acpi_find_dummy,
1177 .find_device = ata_acpi_find_device,
1178};
1179
1180int ata_acpi_register(void)
1181{
1182 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1183}
1184
1185void ata_acpi_unregister(void)
1186{
1187 scsi_unregister_acpi_bus_type(&ata_acpi_bus);
1188}