blob: f36284e3290d5def51bbcbf0390556ae61067248 [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
63 /*
64 * If acpi bind operation has already happened, we can get the handle
65 * for the port by checking the corresponding scsi_host device's
66 * firmware node, otherwise we will need to find out the handle from
67 * its parent's acpi node.
68 */
69 if (ap->scsi_host)
70 return DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev);
71 else
72 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev),
73 ap->port_no);
74}
Matthew Garrett30dcf762012-06-25 16:13:04 +080075EXPORT_SYMBOL(ata_ap_acpi_handle);
Matthew Garrett6b66d952012-06-25 16:13:03 +080076
Matthew Garrett30dcf762012-06-25 16:13:04 +080077/**
78 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
79 * @dev: the acpi_device returned will correspond to this port
80 *
81 * Returns the acpi_handle for the ACPI namespace object corresponding to
82 * the ata_device passed into the function, or NULL if no such object exists
83 */
84acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
Matthew Garrett6b66d952012-06-25 16:13:03 +080085{
86 acpi_integer adr;
87 struct ata_port *ap = dev->link->ap;
88
89 if (ap->flags & ATA_FLAG_ACPI_SATA) {
90 if (!sata_pmp_attached(ap))
91 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
92 else
93 adr = SATA_ADR(ap->port_no, dev->link->pmp);
94 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
95 } else
Matthew Garrett30dcf762012-06-25 16:13:04 +080096 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
Matthew Garrett6b66d952012-06-25 16:13:03 +080097}
Matthew Garrett30dcf762012-06-25 16:13:04 +080098EXPORT_SYMBOL(ata_dev_acpi_handle);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070099
Holger Macht664d0802008-06-03 20:27:59 +0200100/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
101static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
102{
103 if (dev)
104 dev->flags |= ATA_DFLAG_DETACH;
105 else {
106 struct ata_link *tlink;
107 struct ata_device *tdev;
108
Tejun Heo1eca4362008-11-03 20:03:17 +0900109 ata_for_each_link(tlink, ap, EDGE)
110 ata_for_each_dev(tdev, tlink, ALL)
Holger Macht664d0802008-06-03 20:27:59 +0200111 tdev->flags |= ATA_DFLAG_DETACH;
112 }
113
114 ata_port_schedule_eh(ap);
115}
116
117/**
118 * ata_acpi_handle_hotplug - ACPI event handler backend
119 * @ap: ATA port ACPI event occurred
120 * @dev: ATA device ACPI event occurred (can be NULL)
121 * @event: ACPI event which occurred
Holger Macht664d0802008-06-03 20:27:59 +0200122 *
123 * All ACPI bay / device realted events end up in this function. If
124 * the event is port-wide @dev is NULL. If the event is specific to a
125 * device, @dev points to it.
126 *
127 * Hotplug (as opposed to unplug) notification is always handled as
128 * port-wide while unplug only kills the target device on device-wide
129 * event.
130 *
131 * LOCKING:
132 * ACPI notify handler context. May sleep.
133 */
134static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
Shaohua Lif730ae12008-08-28 10:05:45 +0800135 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +0100136{
Holger Macht664d0802008-06-03 20:27:59 +0200137 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo233f1122008-03-12 14:24:43 +0900138 int wait = 0;
139 unsigned long flags;
Zhang Rui3c1e3892008-07-11 09:42:03 -0400140
Holger Macht664d0802008-06-03 20:27:59 +0200141 spin_lock_irqsave(ap->lock, flags);
Shaohua Lif730ae12008-08-28 10:05:45 +0800142 /*
143 * When dock driver calls into the routine, it will always use
144 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
145 * ACPI_NOTIFY_EJECT_REQUEST for remove
146 */
Tejun Heo233f1122008-03-12 14:24:43 +0900147 switch (event) {
148 case ACPI_NOTIFY_BUS_CHECK:
149 case ACPI_NOTIFY_DEVICE_CHECK:
150 ata_ehi_push_desc(ehi, "ACPI event");
Jeff Garzikc85665f2008-05-19 17:56:10 -0400151
Shaohua Lif730ae12008-08-28 10:05:45 +0800152 ata_ehi_hotplugged(ehi);
153 ata_port_freeze(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200154 break;
155 case ACPI_NOTIFY_EJECT_REQUEST:
156 ata_ehi_push_desc(ehi, "ACPI event");
157
Holger Macht664d0802008-06-03 20:27:59 +0200158 ata_acpi_detach_device(ap, dev);
159 wait = 1;
160 break;
Matthew Garrett237d8442007-10-03 01:24:16 +0100161 }
162
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100163 spin_unlock_irqrestore(ap->lock, flags);
164
Shaohua Lif730ae12008-08-28 10:05:45 +0800165 if (wait)
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100166 ata_port_wait_eh(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200167}
168
169static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
170{
171 struct ata_device *dev = data;
172
Shaohua Lif730ae12008-08-28 10:05:45 +0800173 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
Holger Macht664d0802008-06-03 20:27:59 +0200174}
175
176static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
177{
178 struct ata_port *ap = data;
179
Shaohua Lif730ae12008-08-28 10:05:45 +0800180 ata_acpi_handle_hotplug(ap, NULL, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100181}
182
Shaohua Li1253f7a2008-08-28 10:06:16 +0800183static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
184 u32 event)
185{
186 struct kobject *kobj = NULL;
187 char event_string[20];
188 char *envp[] = { event_string, NULL };
189
190 if (dev) {
191 if (dev->sdev)
192 kobj = &dev->sdev->sdev_gendev.kobj;
193 } else
194 kobj = &ap->dev->kobj;
195
196 if (kobj) {
197 snprintf(event_string, 20, "BAY_EVENT=%d", event);
198 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
199 }
200}
201
202static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
203{
204 ata_acpi_uevent(data, NULL, event);
205}
206
207static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
208{
209 struct ata_device *dev = data;
210 ata_acpi_uevent(dev->link->ap, dev, event);
211}
212
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400213static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800214 .handler = ata_acpi_dev_notify_dock,
215 .uevent = ata_acpi_dev_uevent,
216};
217
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400218static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800219 .handler = ata_acpi_ap_notify_dock,
220 .uevent = ata_acpi_ap_uevent,
221};
222
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700223/**
Tejun Heo562f0c22007-12-15 15:05:01 +0900224 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
225 * @host: target ATA host
226 *
227 * This function is called during driver detach after the whole host
228 * is shut down.
229 *
230 * LOCKING:
231 * EH context.
232 */
233void ata_acpi_dissociate(struct ata_host *host)
234{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900235 int i;
236
237 /* Restore initial _GTM values so that driver which attaches
238 * afterward can use them too.
239 */
240 for (i = 0; i < host->n_ports; i++) {
241 struct ata_port *ap = host->ports[i];
242 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
243
Matthew Garrett30dcf762012-06-25 16:13:04 +0800244 if (ata_ap_acpi_handle(ap) && gtm)
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900245 ata_acpi_stm(ap, gtm);
246 }
Tejun Heo562f0c22007-12-15 15:05:01 +0900247}
248
249/**
Tejun Heo64578a32007-05-15 03:28:16 +0900250 * ata_acpi_gtm - execute _GTM
251 * @ap: target ATA port
252 * @gtm: out parameter for _GTM result
253 *
254 * Evaluate _GTM and store the result in @gtm.
255 *
256 * LOCKING:
257 * EH context.
258 *
259 * RETURNS:
260 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
261 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900262int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
Tejun Heo64578a32007-05-15 03:28:16 +0900263{
264 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
265 union acpi_object *out_obj;
266 acpi_status status;
267 int rc = 0;
268
Matthew Garrett30dcf762012-06-25 16:13:04 +0800269 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
270 &output);
Tejun Heo64578a32007-05-15 03:28:16 +0900271
272 rc = -ENOENT;
273 if (status == AE_NOT_FOUND)
274 goto out_free;
275
276 rc = -EINVAL;
277 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700278 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
279 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900280 goto out_free;
281 }
282
283 out_obj = output.pointer;
284 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700285 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
286 out_obj->type);
Tejun Heo64578a32007-05-15 03:28:16 +0900287
288 goto out_free;
289 }
290
291 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700292 ata_port_err(ap, "_GTM returned invalid length %d\n",
293 out_obj->buffer.length);
Tejun Heo64578a32007-05-15 03:28:16 +0900294 goto out_free;
295 }
296
297 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
298 rc = 0;
299 out_free:
300 kfree(output.pointer);
301 return rc;
302}
303
Alan Coxbadff032007-10-04 21:28:18 +0100304EXPORT_SYMBOL_GPL(ata_acpi_gtm);
305
Tejun Heo64578a32007-05-15 03:28:16 +0900306/**
307 * ata_acpi_stm - execute _STM
308 * @ap: target ATA port
309 * @stm: timing parameter to _STM
310 *
311 * Evaluate _STM with timing parameter @stm.
312 *
313 * LOCKING:
314 * EH context.
315 *
316 * RETURNS:
317 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
318 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900319int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a32007-05-15 03:28:16 +0900320{
321 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900322 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a32007-05-15 03:28:16 +0900323 struct acpi_object_list input;
324 union acpi_object in_params[3];
325
326 in_params[0].type = ACPI_TYPE_BUFFER;
327 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900328 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a32007-05-15 03:28:16 +0900329 /* Buffers for id may need byteswapping ? */
330 in_params[1].type = ACPI_TYPE_BUFFER;
331 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900332 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900333 in_params[2].type = ACPI_TYPE_BUFFER;
334 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900335 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900336
337 input.count = 3;
338 input.pointer = in_params;
339
Matthew Garrett30dcf762012-06-25 16:13:04 +0800340 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
341 NULL);
Tejun Heo64578a32007-05-15 03:28:16 +0900342
343 if (status == AE_NOT_FOUND)
344 return -ENOENT;
345 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700346 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
347 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900348 return -EINVAL;
349 }
350 return 0;
351}
352
Alan Coxbadff032007-10-04 21:28:18 +0100353EXPORT_SYMBOL_GPL(ata_acpi_stm);
354
Tejun Heo64578a32007-05-15 03:28:16 +0900355/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900356 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900357 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900358 * @gtf: output parameter for buffer containing _GTF taskfile arrays
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700359 *
360 * This applies to both PATA and SATA drives.
361 *
362 * The _GTF method has no input parameters.
363 * It returns a variable number of register set values (registers
364 * hex 1F1..1F7, taskfiles).
365 * The <variable number> is not known in advance, so have ACPI-CA
366 * allocate the buffer as needed and return it, then free it later.
367 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900368 * LOCKING:
369 * EH context.
370 *
371 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900372 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
373 * if _GTF is invalid.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700374 */
Tejun Heo398e0782007-12-15 15:05:03 +0900375static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700376{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900377 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900378 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900379 struct acpi_buffer output;
380 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900381 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700382
Tejun Heo398e0782007-12-15 15:05:03 +0900383 /* if _GTF is cached, use the cached value */
384 if (dev->gtf_cache) {
385 out_obj = dev->gtf_cache;
386 goto done;
387 }
388
Tejun Heo4700c4b2007-05-15 03:28:16 +0900389 /* set up output buffer */
390 output.length = ACPI_ALLOCATE_BUFFER;
391 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700392
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700393 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700394 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
395 __func__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700396
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700397 /* _GTF has no input parameters */
Matthew Garrett30dcf762012-06-25 16:13:04 +0800398 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
399 &output);
Tejun Heo398e0782007-12-15 15:05:03 +0900400 out_obj = dev->gtf_cache = output.pointer;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900401
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700402 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900403 if (status != AE_NOT_FOUND) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700404 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
405 status);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900406 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900407 }
408 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700409 }
410
411 if (!output.length || !output.pointer) {
412 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700413 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
414 __func__,
415 (unsigned long long)output.length,
416 output.pointer);
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
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700421 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700422 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
423 out_obj->type);
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 Heo4700c4b2007-05-15 03:28:16 +0900428 if (out_obj->buffer.length % REGS_PER_GTF) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700429 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
430 out_obj->buffer.length);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900431 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900432 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700433 }
434
Tejun Heo398e0782007-12-15 15:05:03 +0900435 done:
Tejun Heo4700c4b2007-05-15 03:28:16 +0900436 rc = out_obj->buffer.length / REGS_PER_GTF;
Tejun Heo398e0782007-12-15 15:05:03 +0900437 if (gtf) {
438 *gtf = (void *)out_obj->buffer.pointer;
439 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700440 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
441 __func__, *gtf, rc);
Tejun Heo398e0782007-12-15 15:05:03 +0900442 }
Tejun Heo4700c4b2007-05-15 03:28:16 +0900443 return rc;
444
445 out_free:
Tejun Heo398e0782007-12-15 15:05:03 +0900446 ata_acpi_clear_gtf(dev);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900447 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700448}
449
Tejun Heo7c77fa42007-12-18 16:33:03 +0900450/**
451 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
452 * @dev: target device
453 * @gtm: GTM parameter to use
454 *
455 * Determine xfermask for @dev from @gtm.
456 *
457 * LOCKING:
458 * None.
459 *
460 * RETURNS:
461 * Determined xfermask.
462 */
463unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
464 const struct ata_acpi_gtm *gtm)
465{
Tejun Heoa0f79b92007-12-18 16:33:05 +0900466 unsigned long xfer_mask = 0;
467 unsigned int type;
468 int unit;
469 u8 mode;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900470
471 /* we always use the 0 slot for crap hardware */
472 unit = dev->devno;
473 if (!(gtm->flags & 0x10))
474 unit = 0;
475
Tejun Heoa0f79b92007-12-18 16:33:05 +0900476 /* PIO */
477 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
478 xfer_mask |= ata_xfer_mode2mask(mode);
Tejun Heo7c77fa42007-12-18 16:33:03 +0900479
480 /* See if we have MWDMA or UDMA data. We don't bother with
481 * MWDMA if UDMA is available as this means the BIOS set UDMA
482 * and our error changedown if it works is UDMA to PIO anyway.
483 */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900484 if (!(gtm->flags & (1 << (2 * unit))))
485 type = ATA_SHIFT_MWDMA;
486 else
487 type = ATA_SHIFT_UDMA;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900488
Tejun Heoa0f79b92007-12-18 16:33:05 +0900489 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
490 xfer_mask |= ata_xfer_mode2mask(mode);
491
492 return xfer_mask;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900493}
494EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
495
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700496/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400497 * ata_acpi_cbl_80wire - Check for 80 wire cable
498 * @ap: Port to check
Tejun Heo021ee9a2007-12-18 16:33:06 +0900499 * @gtm: GTM data to use
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400500 *
Tejun Heo021ee9a2007-12-18 16:33:06 +0900501 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400502 */
Tejun Heo021ee9a2007-12-18 16:33:06 +0900503int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400504{
Tejun Heo021ee9a2007-12-18 16:33:06 +0900505 struct ata_device *dev;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400506
Tejun Heo1eca4362008-11-03 20:03:17 +0900507 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo021ee9a2007-12-18 16:33:06 +0900508 unsigned long xfer_mask, udma_mask;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400509
Tejun Heo021ee9a2007-12-18 16:33:06 +0900510 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
511 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
512
513 if (udma_mask & ~ATA_UDMA_MASK_40C)
514 return 1;
515 }
516
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400517 return 0;
518}
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400519EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
520
Tejun Heo3264a8d2007-12-15 15:05:06 +0900521static void ata_acpi_gtf_to_tf(struct ata_device *dev,
522 const struct ata_acpi_gtf *gtf,
523 struct ata_taskfile *tf)
524{
525 ata_tf_init(dev, tf);
526
527 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
528 tf->protocol = ATA_PROT_NODATA;
529 tf->feature = gtf->tf[0]; /* 0x1f1 */
530 tf->nsect = gtf->tf[1]; /* 0x1f2 */
531 tf->lbal = gtf->tf[2]; /* 0x1f3 */
532 tf->lbam = gtf->tf[3]; /* 0x1f4 */
533 tf->lbah = gtf->tf[4]; /* 0x1f5 */
534 tf->device = gtf->tf[5]; /* 0x1f6 */
535 tf->command = gtf->tf[6]; /* 0x1f7 */
536}
537
Tejun Heo110f66d2009-09-16 04:17:28 +0900538static int ata_acpi_filter_tf(struct ata_device *dev,
539 const struct ata_taskfile *tf,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900540 const struct ata_taskfile *ptf)
541{
Tejun Heo110f66d2009-09-16 04:17:28 +0900542 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900543 /* libata doesn't use ACPI to configure transfer mode.
544 * It will only confuse device configuration. Skip.
545 */
546 if (tf->command == ATA_CMD_SET_FEATURES &&
547 tf->feature == SETFEATURES_XFER)
548 return 1;
549 }
550
Tejun Heo110f66d2009-09-16 04:17:28 +0900551 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900552 /* BIOS writers, sorry but we don't wanna lock
553 * features unless the user explicitly said so.
554 */
555
556 /* DEVICE CONFIGURATION FREEZE LOCK */
557 if (tf->command == ATA_CMD_CONF_OVERLAY &&
558 tf->feature == ATA_DCO_FREEZE_LOCK)
559 return 1;
560
561 /* SECURITY FREEZE LOCK */
562 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
563 return 1;
564
565 /* SET MAX LOCK and SET MAX FREEZE LOCK */
566 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
567 tf->command == ATA_CMD_SET_MAX &&
568 (tf->feature == ATA_SET_MAX_LOCK ||
569 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
570 return 1;
571 }
572
Tejun Heofa5b5612009-09-16 04:17:02 +0900573 if (tf->command == ATA_CMD_SET_FEATURES &&
574 tf->feature == SETFEATURES_SATA_ENABLE) {
Tejun Heob3449912008-07-06 23:15:03 +0900575 /* inhibit enabling DIPM */
Tejun Heo110f66d2009-09-16 04:17:28 +0900576 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
Tejun Heob3449912008-07-06 23:15:03 +0900577 tf->nsect == SATA_DIPM)
578 return 1;
Tejun Heofa5b5612009-09-16 04:17:02 +0900579
580 /* inhibit FPDMA non-zero offset */
Tejun Heo110f66d2009-09-16 04:17:28 +0900581 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900582 (tf->nsect == SATA_FPDMA_OFFSET ||
583 tf->nsect == SATA_FPDMA_IN_ORDER))
584 return 1;
585
586 /* inhibit FPDMA auto activation */
Tejun Heo110f66d2009-09-16 04:17:28 +0900587 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900588 tf->nsect == SATA_FPDMA_AA)
589 return 1;
Tejun Heob3449912008-07-06 23:15:03 +0900590 }
591
Tejun Heo3264a8d2007-12-15 15:05:06 +0900592 return 0;
593}
594
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400595/**
Tejun Heo0e8634b2007-12-15 15:05:05 +0900596 * ata_acpi_run_tf - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900597 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700598 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
599 *
Sergei Shtylyov3696df32011-02-04 22:04:17 +0300600 * Outputs ATA taskfile to standard ATA host controller.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700601 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
602 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
603 * hob_lbal, hob_lbam, and hob_lbah.
604 *
605 * This function waits for idle (!BUSY and !DRQ) after writing
606 * registers. If the control register has a new value, this
607 * function also waits for idle after writing control and before
608 * writing the remaining registers.
609 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900610 * LOCKING:
611 * EH context.
612 *
613 * RETURNS:
Tejun Heo3264a8d2007-12-15 15:05:06 +0900614 * 1 if command is executed successfully. 0 if ignored, rejected or
615 * filtered out, -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700616 */
Tejun Heo0e8634b2007-12-15 15:05:05 +0900617static int ata_acpi_run_tf(struct ata_device *dev,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900618 const struct ata_acpi_gtf *gtf,
619 const struct ata_acpi_gtf *prev_gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700620{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900621 struct ata_taskfile *pptf = NULL;
622 struct ata_taskfile tf, ptf, rtf;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900623 unsigned int err_mask;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900624 const char *level;
Robert Hancock65211482009-07-14 20:43:39 -0600625 const char *descr;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900626 char msg[60];
627 int rc;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500628
Tejun Heo4700c4b2007-05-15 03:28:16 +0900629 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
630 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
631 && (gtf->tf[6] == 0))
632 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700633
Tejun Heo3264a8d2007-12-15 15:05:06 +0900634 ata_acpi_gtf_to_tf(dev, gtf, &tf);
635 if (prev_gtf) {
636 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
637 pptf = &ptf;
638 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700639
Tejun Heo110f66d2009-09-16 04:17:28 +0900640 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900641 rtf = tf;
642 err_mask = ata_exec_internal(dev, &rtf, NULL,
643 DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700644
Tejun Heo3264a8d2007-12-15 15:05:06 +0900645 switch (err_mask) {
646 case 0:
647 level = KERN_DEBUG;
648 snprintf(msg, sizeof(msg), "succeeded");
649 rc = 1;
650 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900651
Tejun Heo3264a8d2007-12-15 15:05:06 +0900652 case AC_ERR_DEV:
653 level = KERN_INFO;
654 snprintf(msg, sizeof(msg),
655 "rejected by device (Stat=0x%02x Err=0x%02x)",
656 rtf.command, rtf.feature);
657 rc = 0;
658 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900659
Tejun Heo3264a8d2007-12-15 15:05:06 +0900660 default:
661 level = KERN_ERR;
662 snprintf(msg, sizeof(msg),
663 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
664 err_mask, rtf.command, rtf.feature);
665 rc = -EIO;
666 break;
667 }
668 } else {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900669 level = KERN_INFO;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900670 snprintf(msg, sizeof(msg), "filtered out");
Tejun Heo0e8634b2007-12-15 15:05:05 +0900671 rc = 0;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900672 }
Robert Hancock65211482009-07-14 20:43:39 -0600673 descr = ata_get_cmd_descript(tf.command);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900674
Tejun Heo0e8634b2007-12-15 15:05:05 +0900675 ata_dev_printk(dev, level,
Robert Hancock65211482009-07-14 20:43:39 -0600676 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
Tejun Heo0e8634b2007-12-15 15:05:05 +0900677 tf.command, tf.feature, tf.nsect, tf.lbal,
Robert Hancock65211482009-07-14 20:43:39 -0600678 tf.lbam, tf.lbah, tf.device,
679 (descr ? descr : "unknown"), msg);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900680
681 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700682}
683
684/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700685 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900686 * @dev: target ATA device
Martin Olsson98a17082009-04-22 18:21:29 +0200687 * @nr_executed: out parameter for the number of executed commands
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700688 *
Martin Olsson98a17082009-04-22 18:21:29 +0200689 * Evaluate _GTF and execute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900690 *
691 * LOCKING:
692 * EH context.
693 *
694 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900695 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
696 * -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700697 */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900698static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700699{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900700 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
Tejun Heo67465442007-05-15 03:28:16 +0900701 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700702
Tejun Heo67465442007-05-15 03:28:16 +0900703 /* get taskfiles */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900704 rc = ata_dev_get_GTF(dev, &gtf);
705 if (rc < 0)
706 return rc;
707 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700708
Tejun Heo67465442007-05-15 03:28:16 +0900709 /* execute them */
Tejun Heo3264a8d2007-12-15 15:05:06 +0900710 for (i = 0; i < gtf_count; i++, gtf++) {
711 rc = ata_acpi_run_tf(dev, gtf, pgtf);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900712 if (rc < 0)
713 break;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900714 if (rc) {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900715 (*nr_executed)++;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900716 pgtf = gtf;
717 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700718 }
719
Tejun Heo398e0782007-12-15 15:05:03 +0900720 ata_acpi_clear_gtf(dev);
Tejun Heo67465442007-05-15 03:28:16 +0900721
Tejun Heo0e8634b2007-12-15 15:05:05 +0900722 if (rc < 0)
723 return rc;
724 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700725}
726
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700727/**
728 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900729 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700730 *
731 * _SDD ACPI object: for SATA mode only
732 * Must be after Identify (Packet) Device -- uses its data
733 * ATM this function never returns a failure. It is an optional
734 * method and if it fails for whatever reason, we should still
735 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900736 *
737 * LOCKING:
738 * EH context.
739 *
740 * RETURNS:
Tejun Heof2406772009-11-18 22:24:21 +0900741 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700742 */
Tejun Heo67465442007-05-15 03:28:16 +0900743static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700744{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900745 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900746 acpi_status status;
747 struct acpi_object_list input;
748 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700749
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700750 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700751 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
752 __func__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700753
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700754 /* Give the drive Identify data to the drive via the _SDD method */
755 /* _SDD: set up input parameters */
756 input.count = 1;
757 input.pointer = in_params;
758 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900759 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
760 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700761 /* Output buffer: _SDD has no output */
762
763 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900764 swap_buf_le16(dev->id, ATA_ID_WORDS);
Matthew Garrett30dcf762012-06-25 16:13:04 +0800765 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
766 NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900767 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700768
Tejun Heof2406772009-11-18 22:24:21 +0900769 if (status == AE_NOT_FOUND)
770 return -ENOENT;
771
772 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700773 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
Tejun Heof2406772009-11-18 22:24:21 +0900774 return -EIO;
775 }
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700776
Tejun Heof2406772009-11-18 22:24:21 +0900777 return 0;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700778}
779
Tejun Heo67465442007-05-15 03:28:16 +0900780/**
Tejun Heo64578a32007-05-15 03:28:16 +0900781 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
782 * @ap: target ATA port
783 *
784 * This function is called when @ap is about to be suspended. All
785 * devices are already put to sleep but the port_suspend() callback
786 * hasn't been executed yet. Error return from this function aborts
787 * suspend.
788 *
789 * LOCKING:
790 * EH context.
791 *
792 * RETURNS:
793 * 0 on success, -errno on failure.
794 */
795int ata_acpi_on_suspend(struct ata_port *ap)
796{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900797 /* nada */
798 return 0;
Tejun Heo64578a32007-05-15 03:28:16 +0900799}
800
801/**
Tejun Heo67465442007-05-15 03:28:16 +0900802 * ata_acpi_on_resume - ATA ACPI hook called on resume
803 * @ap: target ATA port
804 *
805 * This function is called when @ap is resumed - right after port
806 * itself is resumed but before any EH action is taken.
807 *
808 * LOCKING:
809 * EH context.
810 */
811void ata_acpi_on_resume(struct ata_port *ap)
812{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900813 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Tejun Heof58229f2007-08-06 18:36:23 +0900814 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700815
Matthew Garrett30dcf762012-06-25 16:13:04 +0800816 if (ata_ap_acpi_handle(ap) && gtm) {
Tejun Heo398e0782007-12-15 15:05:03 +0900817 /* _GTM valid */
818
819 /* restore timing parameters */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900820 ata_acpi_stm(ap, gtm);
Tejun Heo64578a32007-05-15 03:28:16 +0900821
Tejun Heo398e0782007-12-15 15:05:03 +0900822 /* _GTF should immediately follow _STM so that it can
823 * use values set by _STM. Cache _GTF result and
824 * schedule _GTF.
825 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900826 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900827 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800828 if (ata_dev_enabled(dev) &&
829 ata_dev_get_GTF(dev, NULL) >= 0)
Tejun Heo398e0782007-12-15 15:05:03 +0900830 dev->flags |= ATA_DFLAG_ACPI_PENDING;
831 }
832 } else {
833 /* SATA _GTF needs to be evaulated after _SDD and
834 * there's no reason to evaluate IDE _GTF early
835 * without _STM. Clear cache and schedule _GTF.
836 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900837 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900838 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800839 if (ata_dev_enabled(dev))
840 dev->flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo398e0782007-12-15 15:05:03 +0900841 }
842 }
Tejun Heo67465442007-05-15 03:28:16 +0900843}
844
845/**
Shaohua Libd3adca2007-11-02 09:32:38 +0800846 * ata_acpi_set_state - set the port power state
847 * @ap: target ATA port
848 * @state: state, on/off
849 *
850 * This function executes the _PS0/_PS3 ACPI method to set the power state.
851 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
852 */
853void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
854{
855 struct ata_device *dev;
Lin Mingfebe53b2012-06-25 16:13:05 +0800856 acpi_handle handle;
Lin Ming3bd46602012-06-25 16:13:06 +0800857 int acpi_state;
Shaohua Libd3adca2007-11-02 09:32:38 +0800858
859 /* channel first and then drives for power on and vica versa
860 for power off */
Lin Mingfebe53b2012-06-25 16:13:05 +0800861 handle = ata_ap_acpi_handle(ap);
862 if (handle && state.event == PM_EVENT_ON)
863 acpi_bus_set_power(handle, ACPI_STATE_D0);
Shaohua Libd3adca2007-11-02 09:32:38 +0800864
Tejun Heo1eca4362008-11-03 20:03:17 +0900865 ata_for_each_dev(dev, &ap->link, ENABLED) {
Lin Mingfebe53b2012-06-25 16:13:05 +0800866 handle = ata_dev_acpi_handle(dev);
Lin Ming3bd46602012-06-25 16:13:06 +0800867 if (!handle)
868 continue;
869
870 if (state.event != PM_EVENT_ON) {
871 acpi_state = acpi_pm_device_sleep_state(
872 &dev->sdev->sdev_gendev, NULL);
873 if (acpi_state > 0)
874 acpi_bus_set_power(handle, acpi_state);
875 /* TBD: need to check if it's runtime pm request */
876 acpi_pm_device_run_wake(
877 &dev->sdev->sdev_gendev, true);
878 } else {
879 /* Ditto */
880 acpi_pm_device_run_wake(
881 &dev->sdev->sdev_gendev, false);
882 acpi_bus_set_power(handle, ACPI_STATE_D0);
883 }
Shaohua Libd3adca2007-11-02 09:32:38 +0800884 }
Lin Mingfebe53b2012-06-25 16:13:05 +0800885
886 handle = ata_ap_acpi_handle(ap);
887 if (handle && state.event != PM_EVENT_ON)
888 acpi_bus_set_power(handle, ACPI_STATE_D3);
Shaohua Libd3adca2007-11-02 09:32:38 +0800889}
890
891/**
Tejun Heo67465442007-05-15 03:28:16 +0900892 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
893 * @dev: target ATA device
894 *
895 * This function is called when @dev is about to be configured.
896 * IDENTIFY data might have been modified after this hook is run.
897 *
898 * LOCKING:
899 * EH context.
900 *
901 * RETURNS:
902 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
903 * -errno on failure.
904 */
905int ata_acpi_on_devcfg(struct ata_device *dev)
906{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900907 struct ata_port *ap = dev->link->ap;
908 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900909 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
Tejun Heo66fa7f22007-12-15 15:05:04 +0900910 int nr_executed = 0;
Tejun Heo67465442007-05-15 03:28:16 +0900911 int rc;
912
Matthew Garrett30dcf762012-06-25 16:13:04 +0800913 if (!ata_dev_acpi_handle(dev))
Tejun Heo67465442007-05-15 03:28:16 +0900914 return 0;
915
916 /* do we need to do _GTF? */
917 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
918 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
919 return 0;
920
921 /* do _SDD if SATA */
922 if (acpi_sata) {
923 rc = ata_acpi_push_id(dev);
Tejun Heof2406772009-11-18 22:24:21 +0900924 if (rc && rc != -ENOENT)
Tejun Heo67465442007-05-15 03:28:16 +0900925 goto acpi_err;
926 }
927
928 /* do _GTF */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900929 rc = ata_acpi_exec_tfs(dev, &nr_executed);
930 if (rc)
Tejun Heo67465442007-05-15 03:28:16 +0900931 goto acpi_err;
932
933 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
934
935 /* refresh IDENTIFY page if any _GTF command has been executed */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900936 if (nr_executed) {
Tejun Heo67465442007-05-15 03:28:16 +0900937 rc = ata_dev_reread_id(dev, 0);
938 if (rc < 0) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700939 ata_dev_err(dev,
940 "failed to IDENTIFY after ACPI commands\n");
Tejun Heo67465442007-05-15 03:28:16 +0900941 return rc;
942 }
943 }
944
945 return 0;
946
947 acpi_err:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900948 /* ignore evaluation failure if we can continue safely */
949 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
950 return 0;
Tejun Heo67465442007-05-15 03:28:16 +0900951
Tejun Heo66fa7f22007-12-15 15:05:04 +0900952 /* fail and let EH retry once more for unknown IO errors */
953 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
954 dev->flags |= ATA_DFLAG_ACPI_FAILED;
955 return rc;
Tejun Heo67465442007-05-15 03:28:16 +0900956 }
Tejun Heo66fa7f22007-12-15 15:05:04 +0900957
Joe Perchesa9a79df2011-04-15 15:51:59 -0700958 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
Tejun Heo66fa7f22007-12-15 15:05:04 +0900959
960 /* We can safely continue if no _GTF command has been executed
961 * and port is not frozen.
962 */
963 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
964 return 0;
965
Tejun Heo67465442007-05-15 03:28:16 +0900966 return rc;
967}
Tejun Heo562f0c22007-12-15 15:05:01 +0900968
969/**
970 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
971 * @dev: target ATA device
972 *
973 * This function is called when @dev is about to be disabled.
974 *
975 * LOCKING:
976 * EH context.
977 */
978void ata_acpi_on_disable(struct ata_device *dev)
979{
Tejun Heo398e0782007-12-15 15:05:03 +0900980 ata_acpi_clear_gtf(dev);
Tejun Heo562f0c22007-12-15 15:05:01 +0900981}
Matthew Garrett6b66d952012-06-25 16:13:03 +0800982
Lin Ming3bd46602012-06-25 16:13:06 +0800983static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
984{
985 struct ata_device *ata_dev = context;
986
987 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
988 pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
989 scsi_autopm_get_device(ata_dev->sdev);
990}
991
992static void ata_acpi_add_pm_notifier(struct ata_device *dev)
993{
994 struct acpi_device *acpi_dev;
995 acpi_handle handle;
996 acpi_status status;
997
998 handle = ata_dev_acpi_handle(dev);
999 if (!handle)
1000 return;
1001
1002 status = acpi_bus_get_device(handle, &acpi_dev);
1003 if (ACPI_SUCCESS(status)) {
1004 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1005 ata_acpi_wake_dev, dev);
1006 device_set_run_wake(&dev->sdev->sdev_gendev, true);
1007 }
1008}
1009
1010static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
1011{
1012 struct acpi_device *acpi_dev;
1013 acpi_handle handle;
1014 acpi_status status;
1015
1016 handle = ata_dev_acpi_handle(dev);
1017 if (!handle)
1018 return;
1019
1020 status = acpi_bus_get_device(handle, &acpi_dev);
1021 if (ACPI_SUCCESS(status)) {
1022 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;
1032 struct device *device;
1033
1034 handle = ata_dev_acpi_handle(dev);
1035 if (!handle)
1036 return;
1037
1038 device = &sdev->sdev_gendev;
1039
1040 acpi_power_resource_register_device(device, handle);
1041}
1042
1043static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1044{
1045 struct scsi_device *sdev = dev->sdev;
1046 acpi_handle handle;
1047 struct device *device;
1048
1049 handle = ata_dev_acpi_handle(dev);
1050 if (!handle)
1051 return;
1052
1053 device = &sdev->sdev_gendev;
1054
1055 acpi_power_resource_unregister_device(device, handle);
1056}
1057
Lin Ming3bd46602012-06-25 16:13:06 +08001058void ata_acpi_bind(struct ata_device *dev)
1059{
1060 ata_acpi_add_pm_notifier(dev);
Lin Minga606dac32012-06-25 16:13:07 +08001061 ata_acpi_register_power_resource(dev);
Lin Ming3bd46602012-06-25 16:13:06 +08001062}
1063
1064void ata_acpi_unbind(struct ata_device *dev)
1065{
1066 ata_acpi_remove_pm_notifier(dev);
Lin Minga606dac32012-06-25 16:13:07 +08001067 ata_acpi_unregister_power_resource(dev);
Lin Ming3bd46602012-06-25 16:13:06 +08001068}
1069
Matthew Garrett6b66d952012-06-25 16:13:03 +08001070static int compat_pci_ata(struct ata_port *ap)
1071{
1072 struct device *dev = ap->tdev.parent;
1073 struct pci_dev *pdev;
1074
1075 if (!is_pci_dev(dev))
1076 return 0;
1077
1078 pdev = to_pci_dev(dev);
1079
1080 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1081 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1082 return 0;
1083
1084 return 1;
1085}
1086
1087static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1088{
1089 if (ap->flags & ATA_FLAG_ACPI_SATA)
1090 return -ENODEV;
1091
1092 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1093 ap->port_no);
1094
1095 if (!*handle)
1096 return -ENODEV;
1097
1098 return 0;
1099}
1100
1101static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1102 acpi_handle *handle)
1103{
1104 struct ata_device *ata_dev;
1105
1106 if (ap->flags & ATA_FLAG_ACPI_SATA)
1107 ata_dev = &ap->link.device[sdev->channel];
1108 else
1109 ata_dev = &ap->link.device[sdev->id];
1110
Matthew Garrett30dcf762012-06-25 16:13:04 +08001111 *handle = ata_dev_acpi_handle(ata_dev);
Matthew Garrett6b66d952012-06-25 16:13:03 +08001112
1113 if (!*handle)
1114 return -ENODEV;
1115
1116 return 0;
1117}
1118
1119static int is_ata_port(const struct device *dev)
1120{
1121 return dev->type == &ata_port_type;
1122}
1123
1124static struct ata_port *dev_to_ata_port(struct device *dev)
1125{
1126 while (!is_ata_port(dev)) {
1127 if (!dev->parent)
1128 return NULL;
1129 dev = dev->parent;
1130 }
1131 return to_ata_port(dev);
1132}
1133
1134static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1135{
1136 struct ata_port *ap = dev_to_ata_port(dev);
1137
1138 if (!ap)
1139 return -ENODEV;
1140
1141 if (!compat_pci_ata(ap))
1142 return -ENODEV;
1143
1144 if (scsi_is_host_device(dev))
1145 return ata_acpi_bind_host(ap, handle);
1146 else if (scsi_is_sdev_device(dev)) {
1147 struct scsi_device *sdev = to_scsi_device(dev);
1148
1149 return ata_acpi_bind_device(ap, sdev, handle);
1150 } else
1151 return -ENODEV;
1152}
1153
1154static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1155{
1156 return -ENODEV;
1157}
1158
1159static struct acpi_bus_type ata_acpi_bus = {
1160 .find_bridge = ata_acpi_find_dummy,
1161 .find_device = ata_acpi_find_device,
1162};
1163
1164int ata_acpi_register(void)
1165{
1166 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1167}
1168
1169void ata_acpi_unregister(void)
1170{
1171 scsi_unregister_acpi_bus_type(&ata_acpi_bus);
1172}