blob: cf4e7020adacde5e69881a21adb0c578d31d7a3e [file] [log] [blame]
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -07001/*
2 * libata-acpi.c
3 * Provides ACPI support for PATA/SATA.
4 *
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
7 */
8
Tejun Heo3264a8d2007-12-15 15:05:06 +09009#include <linux/module.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070010#include <linux/ata.h>
11#include <linux/delay.h>
12#include <linux/device.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/acpi.h>
16#include <linux/libata.h>
17#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Lin Ming3bd46602012-06-25 16:13:06 +080019#include <linux/pm_runtime.h>
Matthew Garrett237d8442007-10-03 01:24:16 +010020#include <scsi/scsi_device.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070021#include "libata.h"
22
23#include <acpi/acpi_bus.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070024
Tejun Heo110f66d2009-09-16 04:17:28 +090025unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
Tejun Heo3264a8d2007-12-15 15:05:06 +090026module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
Tejun Heofa5b5612009-09-16 04:17:02 +090027MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock, 0x4=DIPM, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)");
Tejun Heo3264a8d2007-12-15 15:05:06 +090028
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070029#define NO_PORT_MULT 0xffff
Jeff Garzik2dcb4072007-10-19 06:42:56 -040030#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070031
32#define REGS_PER_GTF 7
Tejun Heo4700c4b2007-05-15 03:28:16 +090033struct ata_acpi_gtf {
34 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
35} __packed;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070036
Alan Coxca426632007-03-08 23:13:50 +000037/*
38 * Helper - belongs in the PCI layer somewhere eventually
39 */
40static int is_pci_dev(struct device *dev)
41{
42 return (dev->bus == &pci_bus_type);
43}
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070044
Tejun Heo398e0782007-12-15 15:05:03 +090045static void ata_acpi_clear_gtf(struct ata_device *dev)
46{
47 kfree(dev->gtf_cache);
48 dev->gtf_cache = NULL;
49}
50
Matthew Garrett30dcf762012-06-25 16:13:04 +080051/**
52 * ata_ap_acpi_handle - provide the acpi_handle for an ata_port
53 * @ap: the acpi_handle returned will correspond to this port
54 *
55 * Returns the acpi_handle for the ACPI namespace object corresponding to
56 * the ata_port passed into the function, or NULL if no such object exists
57 */
58acpi_handle ata_ap_acpi_handle(struct ata_port *ap)
Matthew Garrett6b66d952012-06-25 16:13:03 +080059{
60 if (ap->flags & ATA_FLAG_ACPI_SATA)
61 return NULL;
62
Aaron Lud66af4d2013-04-27 09:33:07 +080063 return ap->scsi_host ?
64 DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev) : NULL;
Matthew Garrett6b66d952012-06-25 16:13:03 +080065}
Matthew Garrett30dcf762012-06-25 16:13:04 +080066EXPORT_SYMBOL(ata_ap_acpi_handle);
Matthew Garrett6b66d952012-06-25 16:13:03 +080067
Matthew Garrett30dcf762012-06-25 16:13:04 +080068/**
69 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
70 * @dev: the acpi_device returned will correspond to this port
71 *
72 * Returns the acpi_handle for the ACPI namespace object corresponding to
73 * the ata_device passed into the function, or NULL if no such object exists
74 */
75acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
Matthew Garrett6b66d952012-06-25 16:13:03 +080076{
77 acpi_integer adr;
78 struct ata_port *ap = dev->link->ap;
79
Lv Zheng19ccee72013-04-27 09:37:53 +080080 if (libata_noacpi || dev->flags & ATA_DFLAG_ACPI_DISABLED)
Aaron Lu0d0cdb02012-11-26 13:55:25 +080081 return NULL;
82
Matthew Garrett6b66d952012-06-25 16:13:03 +080083 if (ap->flags & ATA_FLAG_ACPI_SATA) {
84 if (!sata_pmp_attached(ap))
85 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
86 else
87 adr = SATA_ADR(ap->port_no, dev->link->pmp);
88 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
89 } else
Matthew Garrett30dcf762012-06-25 16:13:04 +080090 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
Matthew Garrett6b66d952012-06-25 16:13:03 +080091}
Matthew Garrett30dcf762012-06-25 16:13:04 +080092EXPORT_SYMBOL(ata_dev_acpi_handle);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070093
Holger Macht664d0802008-06-03 20:27:59 +020094/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
95static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
96{
97 if (dev)
98 dev->flags |= ATA_DFLAG_DETACH;
99 else {
100 struct ata_link *tlink;
101 struct ata_device *tdev;
102
Tejun Heo1eca4362008-11-03 20:03:17 +0900103 ata_for_each_link(tlink, ap, EDGE)
104 ata_for_each_dev(tdev, tlink, ALL)
Holger Macht664d0802008-06-03 20:27:59 +0200105 tdev->flags |= ATA_DFLAG_DETACH;
106 }
107
108 ata_port_schedule_eh(ap);
109}
110
111/**
112 * ata_acpi_handle_hotplug - ACPI event handler backend
113 * @ap: ATA port ACPI event occurred
114 * @dev: ATA device ACPI event occurred (can be NULL)
115 * @event: ACPI event which occurred
Holger Macht664d0802008-06-03 20:27:59 +0200116 *
117 * All ACPI bay / device realted events end up in this function. If
118 * the event is port-wide @dev is NULL. If the event is specific to a
119 * device, @dev points to it.
120 *
121 * Hotplug (as opposed to unplug) notification is always handled as
122 * port-wide while unplug only kills the target device on device-wide
123 * event.
124 *
125 * LOCKING:
126 * ACPI notify handler context. May sleep.
127 */
128static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
Shaohua Lif730ae12008-08-28 10:05:45 +0800129 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +0100130{
Holger Macht664d0802008-06-03 20:27:59 +0200131 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo233f1122008-03-12 14:24:43 +0900132 int wait = 0;
133 unsigned long flags;
Zhang Rui3c1e3892008-07-11 09:42:03 -0400134
Holger Macht664d0802008-06-03 20:27:59 +0200135 spin_lock_irqsave(ap->lock, flags);
Shaohua Lif730ae12008-08-28 10:05:45 +0800136 /*
137 * When dock driver calls into the routine, it will always use
138 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
139 * ACPI_NOTIFY_EJECT_REQUEST for remove
140 */
Tejun Heo233f1122008-03-12 14:24:43 +0900141 switch (event) {
142 case ACPI_NOTIFY_BUS_CHECK:
143 case ACPI_NOTIFY_DEVICE_CHECK:
144 ata_ehi_push_desc(ehi, "ACPI event");
Jeff Garzikc85665f2008-05-19 17:56:10 -0400145
Shaohua Lif730ae12008-08-28 10:05:45 +0800146 ata_ehi_hotplugged(ehi);
147 ata_port_freeze(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200148 break;
149 case ACPI_NOTIFY_EJECT_REQUEST:
150 ata_ehi_push_desc(ehi, "ACPI event");
151
Holger Macht664d0802008-06-03 20:27:59 +0200152 ata_acpi_detach_device(ap, dev);
153 wait = 1;
154 break;
Matthew Garrett237d8442007-10-03 01:24:16 +0100155 }
156
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100157 spin_unlock_irqrestore(ap->lock, flags);
158
Aaron Lu44521522013-06-20 09:38:34 +0800159 if (wait) {
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100160 ata_port_wait_eh(ap);
Aaron Lu44521522013-06-20 09:38:34 +0800161 flush_work(&ap->hotplug_task.work);
162 }
Holger Macht664d0802008-06-03 20:27:59 +0200163}
164
165static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
166{
167 struct ata_device *dev = data;
168
Shaohua Lif730ae12008-08-28 10:05:45 +0800169 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
Holger Macht664d0802008-06-03 20:27:59 +0200170}
171
172static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
173{
174 struct ata_port *ap = data;
175
Shaohua Lif730ae12008-08-28 10:05:45 +0800176 ata_acpi_handle_hotplug(ap, NULL, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100177}
178
Shaohua Li1253f7a2008-08-28 10:06:16 +0800179static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
180 u32 event)
181{
182 struct kobject *kobj = NULL;
183 char event_string[20];
184 char *envp[] = { event_string, NULL };
185
186 if (dev) {
187 if (dev->sdev)
188 kobj = &dev->sdev->sdev_gendev.kobj;
189 } else
190 kobj = &ap->dev->kobj;
191
192 if (kobj) {
193 snprintf(event_string, 20, "BAY_EVENT=%d", event);
194 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
195 }
196}
197
198static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
199{
200 ata_acpi_uevent(data, NULL, event);
201}
202
203static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
204{
205 struct ata_device *dev = data;
206 ata_acpi_uevent(dev->link->ap, dev, event);
207}
208
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400209static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800210 .handler = ata_acpi_dev_notify_dock,
211 .uevent = ata_acpi_dev_uevent,
212};
213
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400214static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800215 .handler = ata_acpi_ap_notify_dock,
216 .uevent = ata_acpi_ap_uevent,
217};
218
Aaron Lu44521522013-06-20 09:38:34 +0800219void ata_acpi_hotplug_init(struct ata_host *host)
220{
221 int i;
222
223 for (i = 0; i < host->n_ports; i++) {
224 struct ata_port *ap = host->ports[i];
225 acpi_handle handle;
226 struct ata_device *dev;
227
228 if (!ap)
229 continue;
230
231 handle = ata_ap_acpi_handle(ap);
232 if (handle) {
233 /* we might be on a docking station */
234 register_hotplug_dock_device(handle,
235 &ata_acpi_ap_dock_ops, ap,
236 NULL, NULL);
237 }
238
239 ata_for_each_dev(dev, &ap->link, ALL) {
240 handle = ata_dev_acpi_handle(dev);
241 if (!handle)
242 continue;
243
244 /* we might be on a docking station */
245 register_hotplug_dock_device(handle,
246 &ata_acpi_dev_dock_ops,
247 dev, NULL, NULL);
248 }
249 }
250}
251
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700252/**
Tejun Heo562f0c22007-12-15 15:05:01 +0900253 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
254 * @host: target ATA host
255 *
256 * This function is called during driver detach after the whole host
257 * is shut down.
258 *
259 * LOCKING:
260 * EH context.
261 */
262void ata_acpi_dissociate(struct ata_host *host)
263{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900264 int i;
265
266 /* Restore initial _GTM values so that driver which attaches
267 * afterward can use them too.
268 */
269 for (i = 0; i < host->n_ports; i++) {
270 struct ata_port *ap = host->ports[i];
271 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
272
Matthew Garrett30dcf762012-06-25 16:13:04 +0800273 if (ata_ap_acpi_handle(ap) && gtm)
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900274 ata_acpi_stm(ap, gtm);
275 }
Tejun Heo562f0c22007-12-15 15:05:01 +0900276}
277
Aaron Lud66af4d2013-04-27 09:33:07 +0800278static int __ata_acpi_gtm(struct ata_port *ap, acpi_handle handle,
279 struct ata_acpi_gtm *gtm)
Tejun Heo64578a32007-05-15 03:28:16 +0900280{
281 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
282 union acpi_object *out_obj;
283 acpi_status status;
284 int rc = 0;
285
Aaron Lud66af4d2013-04-27 09:33:07 +0800286 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
Tejun Heo64578a32007-05-15 03:28:16 +0900287
288 rc = -ENOENT;
289 if (status == AE_NOT_FOUND)
290 goto out_free;
291
292 rc = -EINVAL;
293 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700294 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
295 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900296 goto out_free;
297 }
298
299 out_obj = output.pointer;
300 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700301 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
302 out_obj->type);
Tejun Heo64578a32007-05-15 03:28:16 +0900303
304 goto out_free;
305 }
306
307 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700308 ata_port_err(ap, "_GTM returned invalid length %d\n",
309 out_obj->buffer.length);
Tejun Heo64578a32007-05-15 03:28:16 +0900310 goto out_free;
311 }
312
313 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
314 rc = 0;
315 out_free:
316 kfree(output.pointer);
317 return rc;
318}
319
Aaron Lud66af4d2013-04-27 09:33:07 +0800320/**
321 * ata_acpi_gtm - execute _GTM
322 * @ap: target ATA port
323 * @gtm: out parameter for _GTM result
324 *
325 * Evaluate _GTM and store the result in @gtm.
326 *
327 * LOCKING:
328 * EH context.
329 *
330 * RETURNS:
331 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
332 */
333int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
334{
335 if (ata_ap_acpi_handle(ap))
336 return __ata_acpi_gtm(ap, ata_ap_acpi_handle(ap), gtm);
337 else
338 return -EINVAL;
339}
340
Alan Coxbadff032007-10-04 21:28:18 +0100341EXPORT_SYMBOL_GPL(ata_acpi_gtm);
342
Tejun Heo64578a32007-05-15 03:28:16 +0900343/**
344 * ata_acpi_stm - execute _STM
345 * @ap: target ATA port
346 * @stm: timing parameter to _STM
347 *
348 * Evaluate _STM with timing parameter @stm.
349 *
350 * LOCKING:
351 * EH context.
352 *
353 * RETURNS:
354 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
355 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900356int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a32007-05-15 03:28:16 +0900357{
358 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900359 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a32007-05-15 03:28:16 +0900360 struct acpi_object_list input;
361 union acpi_object in_params[3];
362
363 in_params[0].type = ACPI_TYPE_BUFFER;
364 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900365 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a32007-05-15 03:28:16 +0900366 /* Buffers for id may need byteswapping ? */
367 in_params[1].type = ACPI_TYPE_BUFFER;
368 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900369 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900370 in_params[2].type = ACPI_TYPE_BUFFER;
371 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900372 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900373
374 input.count = 3;
375 input.pointer = in_params;
376
Matthew Garrett30dcf762012-06-25 16:13:04 +0800377 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
378 NULL);
Tejun Heo64578a32007-05-15 03:28:16 +0900379
380 if (status == AE_NOT_FOUND)
381 return -ENOENT;
382 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700383 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
384 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900385 return -EINVAL;
386 }
387 return 0;
388}
389
Alan Coxbadff032007-10-04 21:28:18 +0100390EXPORT_SYMBOL_GPL(ata_acpi_stm);
391
Tejun Heo64578a32007-05-15 03:28:16 +0900392/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900393 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900394 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900395 * @gtf: output parameter for buffer containing _GTF taskfile arrays
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700396 *
397 * This applies to both PATA and SATA drives.
398 *
399 * The _GTF method has no input parameters.
400 * It returns a variable number of register set values (registers
401 * hex 1F1..1F7, taskfiles).
402 * The <variable number> is not known in advance, so have ACPI-CA
403 * allocate the buffer as needed and return it, then free it later.
404 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900405 * LOCKING:
406 * EH context.
407 *
408 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900409 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
410 * if _GTF is invalid.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700411 */
Tejun Heo398e0782007-12-15 15:05:03 +0900412static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700413{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900414 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900415 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900416 struct acpi_buffer output;
417 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900418 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700419
Tejun Heo398e0782007-12-15 15:05:03 +0900420 /* if _GTF is cached, use the cached value */
421 if (dev->gtf_cache) {
422 out_obj = dev->gtf_cache;
423 goto done;
424 }
425
Tejun Heo4700c4b2007-05-15 03:28:16 +0900426 /* set up output buffer */
427 output.length = ACPI_ALLOCATE_BUFFER;
428 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700429
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700430 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700431 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
432 __func__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700433
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700434 /* _GTF has no input parameters */
Matthew Garrett30dcf762012-06-25 16:13:04 +0800435 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
436 &output);
Tejun Heo398e0782007-12-15 15:05:03 +0900437 out_obj = dev->gtf_cache = output.pointer;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900438
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700439 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900440 if (status != AE_NOT_FOUND) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700441 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
442 status);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900443 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900444 }
445 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700446 }
447
448 if (!output.length || !output.pointer) {
449 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700450 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
451 __func__,
452 (unsigned long long)output.length,
453 output.pointer);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900454 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900455 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700456 }
457
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700458 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700459 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
460 out_obj->type);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900461 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900462 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700463 }
464
Tejun Heo4700c4b2007-05-15 03:28:16 +0900465 if (out_obj->buffer.length % REGS_PER_GTF) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700466 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
467 out_obj->buffer.length);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900468 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900469 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700470 }
471
Tejun Heo398e0782007-12-15 15:05:03 +0900472 done:
Tejun Heo4700c4b2007-05-15 03:28:16 +0900473 rc = out_obj->buffer.length / REGS_PER_GTF;
Tejun Heo398e0782007-12-15 15:05:03 +0900474 if (gtf) {
475 *gtf = (void *)out_obj->buffer.pointer;
476 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700477 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
478 __func__, *gtf, rc);
Tejun Heo398e0782007-12-15 15:05:03 +0900479 }
Tejun Heo4700c4b2007-05-15 03:28:16 +0900480 return rc;
481
482 out_free:
Tejun Heo398e0782007-12-15 15:05:03 +0900483 ata_acpi_clear_gtf(dev);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900484 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700485}
486
Tejun Heo7c77fa42007-12-18 16:33:03 +0900487/**
488 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
489 * @dev: target device
490 * @gtm: GTM parameter to use
491 *
492 * Determine xfermask for @dev from @gtm.
493 *
494 * LOCKING:
495 * None.
496 *
497 * RETURNS:
498 * Determined xfermask.
499 */
500unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
501 const struct ata_acpi_gtm *gtm)
502{
Tejun Heoa0f79b92007-12-18 16:33:05 +0900503 unsigned long xfer_mask = 0;
504 unsigned int type;
505 int unit;
506 u8 mode;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900507
508 /* we always use the 0 slot for crap hardware */
509 unit = dev->devno;
510 if (!(gtm->flags & 0x10))
511 unit = 0;
512
Tejun Heoa0f79b92007-12-18 16:33:05 +0900513 /* PIO */
514 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
515 xfer_mask |= ata_xfer_mode2mask(mode);
Tejun Heo7c77fa42007-12-18 16:33:03 +0900516
517 /* See if we have MWDMA or UDMA data. We don't bother with
518 * MWDMA if UDMA is available as this means the BIOS set UDMA
519 * and our error changedown if it works is UDMA to PIO anyway.
520 */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900521 if (!(gtm->flags & (1 << (2 * unit))))
522 type = ATA_SHIFT_MWDMA;
523 else
524 type = ATA_SHIFT_UDMA;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900525
Tejun Heoa0f79b92007-12-18 16:33:05 +0900526 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
527 xfer_mask |= ata_xfer_mode2mask(mode);
528
529 return xfer_mask;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900530}
531EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
532
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700533/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400534 * ata_acpi_cbl_80wire - Check for 80 wire cable
535 * @ap: Port to check
Tejun Heo021ee9a2007-12-18 16:33:06 +0900536 * @gtm: GTM data to use
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400537 *
Tejun Heo021ee9a2007-12-18 16:33:06 +0900538 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400539 */
Tejun Heo021ee9a2007-12-18 16:33:06 +0900540int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400541{
Tejun Heo021ee9a2007-12-18 16:33:06 +0900542 struct ata_device *dev;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400543
Tejun Heo1eca4362008-11-03 20:03:17 +0900544 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo021ee9a2007-12-18 16:33:06 +0900545 unsigned long xfer_mask, udma_mask;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400546
Tejun Heo021ee9a2007-12-18 16:33:06 +0900547 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
548 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
549
550 if (udma_mask & ~ATA_UDMA_MASK_40C)
551 return 1;
552 }
553
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400554 return 0;
555}
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400556EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
557
Tejun Heo3264a8d2007-12-15 15:05:06 +0900558static void ata_acpi_gtf_to_tf(struct ata_device *dev,
559 const struct ata_acpi_gtf *gtf,
560 struct ata_taskfile *tf)
561{
562 ata_tf_init(dev, tf);
563
564 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
565 tf->protocol = ATA_PROT_NODATA;
566 tf->feature = gtf->tf[0]; /* 0x1f1 */
567 tf->nsect = gtf->tf[1]; /* 0x1f2 */
568 tf->lbal = gtf->tf[2]; /* 0x1f3 */
569 tf->lbam = gtf->tf[3]; /* 0x1f4 */
570 tf->lbah = gtf->tf[4]; /* 0x1f5 */
571 tf->device = gtf->tf[5]; /* 0x1f6 */
572 tf->command = gtf->tf[6]; /* 0x1f7 */
573}
574
Tejun Heo110f66d2009-09-16 04:17:28 +0900575static int ata_acpi_filter_tf(struct ata_device *dev,
576 const struct ata_taskfile *tf,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900577 const struct ata_taskfile *ptf)
578{
Tejun Heo110f66d2009-09-16 04:17:28 +0900579 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900580 /* libata doesn't use ACPI to configure transfer mode.
581 * It will only confuse device configuration. Skip.
582 */
583 if (tf->command == ATA_CMD_SET_FEATURES &&
584 tf->feature == SETFEATURES_XFER)
585 return 1;
586 }
587
Tejun Heo110f66d2009-09-16 04:17:28 +0900588 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900589 /* BIOS writers, sorry but we don't wanna lock
590 * features unless the user explicitly said so.
591 */
592
593 /* DEVICE CONFIGURATION FREEZE LOCK */
594 if (tf->command == ATA_CMD_CONF_OVERLAY &&
595 tf->feature == ATA_DCO_FREEZE_LOCK)
596 return 1;
597
598 /* SECURITY FREEZE LOCK */
599 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
600 return 1;
601
602 /* SET MAX LOCK and SET MAX FREEZE LOCK */
603 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
604 tf->command == ATA_CMD_SET_MAX &&
605 (tf->feature == ATA_SET_MAX_LOCK ||
606 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
607 return 1;
608 }
609
Tejun Heofa5b5612009-09-16 04:17:02 +0900610 if (tf->command == ATA_CMD_SET_FEATURES &&
611 tf->feature == SETFEATURES_SATA_ENABLE) {
Tejun Heob3449912008-07-06 23:15:03 +0900612 /* inhibit enabling DIPM */
Tejun Heo110f66d2009-09-16 04:17:28 +0900613 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
Tejun Heob3449912008-07-06 23:15:03 +0900614 tf->nsect == SATA_DIPM)
615 return 1;
Tejun Heofa5b5612009-09-16 04:17:02 +0900616
617 /* inhibit FPDMA non-zero offset */
Tejun Heo110f66d2009-09-16 04:17:28 +0900618 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900619 (tf->nsect == SATA_FPDMA_OFFSET ||
620 tf->nsect == SATA_FPDMA_IN_ORDER))
621 return 1;
622
623 /* inhibit FPDMA auto activation */
Tejun Heo110f66d2009-09-16 04:17:28 +0900624 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900625 tf->nsect == SATA_FPDMA_AA)
626 return 1;
Tejun Heob3449912008-07-06 23:15:03 +0900627 }
628
Tejun Heo3264a8d2007-12-15 15:05:06 +0900629 return 0;
630}
631
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400632/**
Tejun Heo0e8634b2007-12-15 15:05:05 +0900633 * ata_acpi_run_tf - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900634 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700635 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
636 *
Sergei Shtylyov3696df32011-02-04 22:04:17 +0300637 * Outputs ATA taskfile to standard ATA host controller.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700638 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
639 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
640 * hob_lbal, hob_lbam, and hob_lbah.
641 *
642 * This function waits for idle (!BUSY and !DRQ) after writing
643 * registers. If the control register has a new value, this
644 * function also waits for idle after writing control and before
645 * writing the remaining registers.
646 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900647 * LOCKING:
648 * EH context.
649 *
650 * RETURNS:
Tejun Heo3264a8d2007-12-15 15:05:06 +0900651 * 1 if command is executed successfully. 0 if ignored, rejected or
652 * filtered out, -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700653 */
Tejun Heo0e8634b2007-12-15 15:05:05 +0900654static int ata_acpi_run_tf(struct ata_device *dev,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900655 const struct ata_acpi_gtf *gtf,
656 const struct ata_acpi_gtf *prev_gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700657{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900658 struct ata_taskfile *pptf = NULL;
659 struct ata_taskfile tf, ptf, rtf;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900660 unsigned int err_mask;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900661 const char *level;
Robert Hancock65211482009-07-14 20:43:39 -0600662 const char *descr;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900663 char msg[60];
664 int rc;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500665
Tejun Heo4700c4b2007-05-15 03:28:16 +0900666 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
667 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
668 && (gtf->tf[6] == 0))
669 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700670
Tejun Heo3264a8d2007-12-15 15:05:06 +0900671 ata_acpi_gtf_to_tf(dev, gtf, &tf);
672 if (prev_gtf) {
673 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
674 pptf = &ptf;
675 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700676
Tejun Heo110f66d2009-09-16 04:17:28 +0900677 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900678 rtf = tf;
679 err_mask = ata_exec_internal(dev, &rtf, NULL,
680 DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700681
Tejun Heo3264a8d2007-12-15 15:05:06 +0900682 switch (err_mask) {
683 case 0:
684 level = KERN_DEBUG;
685 snprintf(msg, sizeof(msg), "succeeded");
686 rc = 1;
687 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900688
Tejun Heo3264a8d2007-12-15 15:05:06 +0900689 case AC_ERR_DEV:
690 level = KERN_INFO;
691 snprintf(msg, sizeof(msg),
692 "rejected by device (Stat=0x%02x Err=0x%02x)",
693 rtf.command, rtf.feature);
694 rc = 0;
695 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900696
Tejun Heo3264a8d2007-12-15 15:05:06 +0900697 default:
698 level = KERN_ERR;
699 snprintf(msg, sizeof(msg),
700 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
701 err_mask, rtf.command, rtf.feature);
702 rc = -EIO;
703 break;
704 }
705 } else {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900706 level = KERN_INFO;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900707 snprintf(msg, sizeof(msg), "filtered out");
Tejun Heo0e8634b2007-12-15 15:05:05 +0900708 rc = 0;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900709 }
Robert Hancock65211482009-07-14 20:43:39 -0600710 descr = ata_get_cmd_descript(tf.command);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900711
Tejun Heo0e8634b2007-12-15 15:05:05 +0900712 ata_dev_printk(dev, level,
Robert Hancock65211482009-07-14 20:43:39 -0600713 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
Tejun Heo0e8634b2007-12-15 15:05:05 +0900714 tf.command, tf.feature, tf.nsect, tf.lbal,
Robert Hancock65211482009-07-14 20:43:39 -0600715 tf.lbam, tf.lbah, tf.device,
716 (descr ? descr : "unknown"), msg);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900717
718 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700719}
720
721/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700722 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900723 * @dev: target ATA device
Martin Olsson98a17082009-04-22 18:21:29 +0200724 * @nr_executed: out parameter for the number of executed commands
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700725 *
Martin Olsson98a17082009-04-22 18:21:29 +0200726 * Evaluate _GTF and execute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900727 *
728 * LOCKING:
729 * EH context.
730 *
731 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900732 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
733 * -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700734 */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900735static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700736{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900737 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
Tejun Heo67465442007-05-15 03:28:16 +0900738 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700739
Tejun Heo67465442007-05-15 03:28:16 +0900740 /* get taskfiles */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900741 rc = ata_dev_get_GTF(dev, &gtf);
742 if (rc < 0)
743 return rc;
744 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700745
Tejun Heo67465442007-05-15 03:28:16 +0900746 /* execute them */
Tejun Heo3264a8d2007-12-15 15:05:06 +0900747 for (i = 0; i < gtf_count; i++, gtf++) {
748 rc = ata_acpi_run_tf(dev, gtf, pgtf);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900749 if (rc < 0)
750 break;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900751 if (rc) {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900752 (*nr_executed)++;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900753 pgtf = gtf;
754 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700755 }
756
Tejun Heo398e0782007-12-15 15:05:03 +0900757 ata_acpi_clear_gtf(dev);
Tejun Heo67465442007-05-15 03:28:16 +0900758
Tejun Heo0e8634b2007-12-15 15:05:05 +0900759 if (rc < 0)
760 return rc;
761 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700762}
763
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700764/**
765 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900766 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700767 *
768 * _SDD ACPI object: for SATA mode only
769 * Must be after Identify (Packet) Device -- uses its data
770 * ATM this function never returns a failure. It is an optional
771 * method and if it fails for whatever reason, we should still
772 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900773 *
774 * LOCKING:
775 * EH context.
776 *
777 * RETURNS:
Tejun Heof2406772009-11-18 22:24:21 +0900778 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700779 */
Tejun Heo67465442007-05-15 03:28:16 +0900780static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700781{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900782 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900783 acpi_status status;
784 struct acpi_object_list input;
785 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700786
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700787 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700788 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
789 __func__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700790
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700791 /* Give the drive Identify data to the drive via the _SDD method */
792 /* _SDD: set up input parameters */
793 input.count = 1;
794 input.pointer = in_params;
795 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900796 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
797 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700798 /* Output buffer: _SDD has no output */
799
800 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900801 swap_buf_le16(dev->id, ATA_ID_WORDS);
Matthew Garrett30dcf762012-06-25 16:13:04 +0800802 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
803 NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900804 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700805
Tejun Heof2406772009-11-18 22:24:21 +0900806 if (status == AE_NOT_FOUND)
807 return -ENOENT;
808
809 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700810 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
Tejun Heof2406772009-11-18 22:24:21 +0900811 return -EIO;
812 }
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700813
Tejun Heof2406772009-11-18 22:24:21 +0900814 return 0;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700815}
816
Tejun Heo67465442007-05-15 03:28:16 +0900817/**
Tejun Heo64578a32007-05-15 03:28:16 +0900818 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
819 * @ap: target ATA port
820 *
821 * This function is called when @ap is about to be suspended. All
822 * devices are already put to sleep but the port_suspend() callback
823 * hasn't been executed yet. Error return from this function aborts
824 * suspend.
825 *
826 * LOCKING:
827 * EH context.
828 *
829 * RETURNS:
830 * 0 on success, -errno on failure.
831 */
832int ata_acpi_on_suspend(struct ata_port *ap)
833{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900834 /* nada */
835 return 0;
Tejun Heo64578a32007-05-15 03:28:16 +0900836}
837
838/**
Tejun Heo67465442007-05-15 03:28:16 +0900839 * ata_acpi_on_resume - ATA ACPI hook called on resume
840 * @ap: target ATA port
841 *
842 * This function is called when @ap is resumed - right after port
843 * itself is resumed but before any EH action is taken.
844 *
845 * LOCKING:
846 * EH context.
847 */
848void ata_acpi_on_resume(struct ata_port *ap)
849{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900850 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Tejun Heof58229f2007-08-06 18:36:23 +0900851 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700852
Matthew Garrett30dcf762012-06-25 16:13:04 +0800853 if (ata_ap_acpi_handle(ap) && gtm) {
Tejun Heo398e0782007-12-15 15:05:03 +0900854 /* _GTM valid */
855
856 /* restore timing parameters */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900857 ata_acpi_stm(ap, gtm);
Tejun Heo64578a32007-05-15 03:28:16 +0900858
Tejun Heo398e0782007-12-15 15:05:03 +0900859 /* _GTF should immediately follow _STM so that it can
860 * use values set by _STM. Cache _GTF result and
861 * schedule _GTF.
862 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900863 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900864 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800865 if (ata_dev_enabled(dev) &&
866 ata_dev_get_GTF(dev, NULL) >= 0)
Tejun Heo398e0782007-12-15 15:05:03 +0900867 dev->flags |= ATA_DFLAG_ACPI_PENDING;
868 }
869 } else {
870 /* SATA _GTF needs to be evaulated after _SDD and
871 * there's no reason to evaluate IDE _GTF early
872 * without _STM. Clear cache and schedule _GTF.
873 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900874 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900875 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800876 if (ata_dev_enabled(dev))
877 dev->flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo398e0782007-12-15 15:05:03 +0900878 }
879 }
Tejun Heo67465442007-05-15 03:28:16 +0900880}
881
Aaron Lua7ff60d2013-01-25 14:29:35 +0800882static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
Shaohua Libd3adca2007-11-02 09:32:38 +0800883{
Aaron Lu21334202013-01-15 17:21:01 +0800884 int d_max_in = ACPI_STATE_D3_COLD;
Aaron Lua7ff60d2013-01-25 14:29:35 +0800885 if (!runtime)
886 goto out;
Aaron Lu21334202013-01-15 17:21:01 +0800887
888 /*
889 * For ATAPI, runtime D3 cold is only allowed
890 * for ZPODD in zero power ready state
891 */
892 if (dev->class == ATA_DEV_ATAPI &&
893 !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
894 d_max_in = ACPI_STATE_D3_HOT;
895
Aaron Lua7ff60d2013-01-25 14:29:35 +0800896out:
Aaron Lu21334202013-01-15 17:21:01 +0800897 return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev,
898 NULL, d_max_in);
899}
900
Aaron Lua7ff60d2013-01-25 14:29:35 +0800901static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
902{
903 bool runtime = PMSG_IS_AUTO(state);
Shaohua Libd3adca2007-11-02 09:32:38 +0800904 struct ata_device *dev;
Lin Mingfebe53b2012-06-25 16:13:05 +0800905 acpi_handle handle;
Lin Ming3bd46602012-06-25 16:13:06 +0800906 int acpi_state;
Shaohua Libd3adca2007-11-02 09:32:38 +0800907
Tejun Heo1eca4362008-11-03 20:03:17 +0900908 ata_for_each_dev(dev, &ap->link, ENABLED) {
Lin Mingfebe53b2012-06-25 16:13:05 +0800909 handle = ata_dev_acpi_handle(dev);
Lin Ming3bd46602012-06-25 16:13:06 +0800910 if (!handle)
911 continue;
912
Aaron Lua7ff60d2013-01-25 14:29:35 +0800913 if (!(state.event & PM_EVENT_RESUME)) {
914 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
915 if (acpi_state == ACPI_STATE_D0)
916 continue;
917 if (runtime && zpodd_dev_enabled(dev) &&
918 acpi_state == ACPI_STATE_D3_COLD)
919 zpodd_enable_run_wake(dev);
920 acpi_bus_set_power(handle, acpi_state);
Lin Ming3bd46602012-06-25 16:13:06 +0800921 } else {
Aaron Lua7ff60d2013-01-25 14:29:35 +0800922 if (runtime && zpodd_dev_enabled(dev))
923 zpodd_disable_run_wake(dev);
Lin Ming3bd46602012-06-25 16:13:06 +0800924 acpi_bus_set_power(handle, ACPI_STATE_D0);
925 }
Shaohua Libd3adca2007-11-02 09:32:38 +0800926 }
Aaron Lua7ff60d2013-01-25 14:29:35 +0800927}
Lin Mingfebe53b2012-06-25 16:13:05 +0800928
Aaron Lua7ff60d2013-01-25 14:29:35 +0800929/* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
930static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
931{
932 struct ata_device *dev;
933 acpi_handle port_handle;
934
935 port_handle = ata_ap_acpi_handle(ap);
936 if (!port_handle)
937 return;
938
939 /* channel first and then drives for power on and vica versa
940 for power off */
941 if (state.event & PM_EVENT_RESUME)
942 acpi_bus_set_power(port_handle, ACPI_STATE_D0);
943
944 ata_for_each_dev(dev, &ap->link, ENABLED) {
945 acpi_handle dev_handle = ata_dev_acpi_handle(dev);
946 if (!dev_handle)
947 continue;
948
949 acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
950 ACPI_STATE_D0 : ACPI_STATE_D3);
951 }
952
953 if (!(state.event & PM_EVENT_RESUME))
954 acpi_bus_set_power(port_handle, ACPI_STATE_D3);
955}
956
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700957/**
958 * ata_acpi_set_state - set the port power state
959 * @ap: target ATA port
960 * @state: state, on/off
961 *
Aaron Lua7ff60d2013-01-25 14:29:35 +0800962 * This function sets a proper ACPI D state for the device on
963 * system and runtime PM operations.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700964 */
965void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
966{
Aaron Lua7ff60d2013-01-25 14:29:35 +0800967 if (ap->flags & ATA_FLAG_ACPI_SATA)
968 sata_acpi_set_state(ap, state);
969 else
970 pata_acpi_set_state(ap, state);
Shaohua Libd3adca2007-11-02 09:32:38 +0800971}
972
973/**
Tejun Heo67465442007-05-15 03:28:16 +0900974 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
975 * @dev: target ATA device
976 *
977 * This function is called when @dev is about to be configured.
978 * IDENTIFY data might have been modified after this hook is run.
979 *
980 * LOCKING:
981 * EH context.
982 *
983 * RETURNS:
984 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
985 * -errno on failure.
986 */
987int ata_acpi_on_devcfg(struct ata_device *dev)
988{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900989 struct ata_port *ap = dev->link->ap;
990 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900991 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
Tejun Heo66fa7f22007-12-15 15:05:04 +0900992 int nr_executed = 0;
Tejun Heo67465442007-05-15 03:28:16 +0900993 int rc;
994
Matthew Garrett30dcf762012-06-25 16:13:04 +0800995 if (!ata_dev_acpi_handle(dev))
Tejun Heo67465442007-05-15 03:28:16 +0900996 return 0;
997
998 /* do we need to do _GTF? */
999 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
1000 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
1001 return 0;
1002
1003 /* do _SDD if SATA */
1004 if (acpi_sata) {
1005 rc = ata_acpi_push_id(dev);
Tejun Heof2406772009-11-18 22:24:21 +09001006 if (rc && rc != -ENOENT)
Tejun Heo67465442007-05-15 03:28:16 +09001007 goto acpi_err;
1008 }
1009
1010 /* do _GTF */
Tejun Heo66fa7f22007-12-15 15:05:04 +09001011 rc = ata_acpi_exec_tfs(dev, &nr_executed);
1012 if (rc)
Tejun Heo67465442007-05-15 03:28:16 +09001013 goto acpi_err;
1014
1015 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
1016
1017 /* refresh IDENTIFY page if any _GTF command has been executed */
Tejun Heo66fa7f22007-12-15 15:05:04 +09001018 if (nr_executed) {
Tejun Heo67465442007-05-15 03:28:16 +09001019 rc = ata_dev_reread_id(dev, 0);
1020 if (rc < 0) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001021 ata_dev_err(dev,
1022 "failed to IDENTIFY after ACPI commands\n");
Tejun Heo67465442007-05-15 03:28:16 +09001023 return rc;
1024 }
1025 }
1026
1027 return 0;
1028
1029 acpi_err:
Tejun Heo66fa7f22007-12-15 15:05:04 +09001030 /* ignore evaluation failure if we can continue safely */
1031 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1032 return 0;
Tejun Heo67465442007-05-15 03:28:16 +09001033
Tejun Heo66fa7f22007-12-15 15:05:04 +09001034 /* fail and let EH retry once more for unknown IO errors */
1035 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1036 dev->flags |= ATA_DFLAG_ACPI_FAILED;
1037 return rc;
Tejun Heo67465442007-05-15 03:28:16 +09001038 }
Tejun Heo66fa7f22007-12-15 15:05:04 +09001039
Aaron Lu0d0cdb02012-11-26 13:55:25 +08001040 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
Joe Perchesa9a79df2011-04-15 15:51:59 -07001041 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
Tejun Heo66fa7f22007-12-15 15:05:04 +09001042
1043 /* We can safely continue if no _GTF command has been executed
1044 * and port is not frozen.
1045 */
1046 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1047 return 0;
1048
Tejun Heo67465442007-05-15 03:28:16 +09001049 return rc;
1050}
Tejun Heo562f0c22007-12-15 15:05:01 +09001051
1052/**
1053 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1054 * @dev: target ATA device
1055 *
1056 * This function is called when @dev is about to be disabled.
1057 *
1058 * LOCKING:
1059 * EH context.
1060 */
1061void ata_acpi_on_disable(struct ata_device *dev)
1062{
Tejun Heo398e0782007-12-15 15:05:03 +09001063 ata_acpi_clear_gtf(dev);
Tejun Heo562f0c22007-12-15 15:05:01 +09001064}
Matthew Garrett6b66d952012-06-25 16:13:03 +08001065
1066static int compat_pci_ata(struct ata_port *ap)
1067{
1068 struct device *dev = ap->tdev.parent;
1069 struct pci_dev *pdev;
1070
1071 if (!is_pci_dev(dev))
1072 return 0;
1073
1074 pdev = to_pci_dev(dev);
1075
1076 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1077 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1078 return 0;
1079
1080 return 1;
1081}
1082
1083static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1084{
Lv Zheng19ccee72013-04-27 09:37:53 +08001085 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA)
Matthew Garrett6b66d952012-06-25 16:13:03 +08001086 return -ENODEV;
1087
1088 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1089 ap->port_no);
1090
1091 if (!*handle)
1092 return -ENODEV;
1093
Aaron Lud66af4d2013-04-27 09:33:07 +08001094 if (__ata_acpi_gtm(ap, *handle, &ap->__acpi_init_gtm) == 0)
Aaron Lu83400912012-08-15 17:08:12 +08001095 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1096
Matthew Garrett6b66d952012-06-25 16:13:03 +08001097 return 0;
1098}
1099
1100static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1101 acpi_handle *handle)
1102{
1103 struct ata_device *ata_dev;
1104
Aaron Lu60817a62012-10-09 15:37:48 +08001105 if (ap->flags & ATA_FLAG_ACPI_SATA) {
1106 if (!sata_pmp_attached(ap))
1107 ata_dev = &ap->link.device[sdev->id];
1108 else
1109 ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id];
1110 }
1111 else {
Matthew Garrett6b66d952012-06-25 16:13:03 +08001112 ata_dev = &ap->link.device[sdev->id];
Aaron Lu60817a62012-10-09 15:37:48 +08001113 }
Matthew Garrett6b66d952012-06-25 16:13:03 +08001114
Matthew Garrett30dcf762012-06-25 16:13:04 +08001115 *handle = ata_dev_acpi_handle(ata_dev);
Matthew Garrett6b66d952012-06-25 16:13:03 +08001116
1117 if (!*handle)
1118 return -ENODEV;
1119
1120 return 0;
1121}
1122
1123static int is_ata_port(const struct device *dev)
1124{
1125 return dev->type == &ata_port_type;
1126}
1127
1128static struct ata_port *dev_to_ata_port(struct device *dev)
1129{
1130 while (!is_ata_port(dev)) {
1131 if (!dev->parent)
1132 return NULL;
1133 dev = dev->parent;
1134 }
1135 return to_ata_port(dev);
1136}
1137
1138static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1139{
1140 struct ata_port *ap = dev_to_ata_port(dev);
1141
1142 if (!ap)
1143 return -ENODEV;
1144
1145 if (!compat_pci_ata(ap))
1146 return -ENODEV;
1147
1148 if (scsi_is_host_device(dev))
1149 return ata_acpi_bind_host(ap, handle);
1150 else if (scsi_is_sdev_device(dev)) {
1151 struct scsi_device *sdev = to_scsi_device(dev);
1152
1153 return ata_acpi_bind_device(ap, sdev, handle);
1154 } else
1155 return -ENODEV;
1156}
1157
Matthew Garrett6b66d952012-06-25 16:13:03 +08001158static struct acpi_bus_type ata_acpi_bus = {
Rafael J. Wysocki53540092013-03-03 22:35:20 +01001159 .name = "ATA",
Matthew Garrett6b66d952012-06-25 16:13:03 +08001160 .find_device = ata_acpi_find_device,
1161};
1162
1163int ata_acpi_register(void)
1164{
1165 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1166}
1167
1168void ata_acpi_unregister(void)
1169{
1170 scsi_unregister_acpi_bus_type(&ata_acpi_bus);
1171}