blob: c6f0101fd2538c7dfcbfef46794c03e90e3e96c4 [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>
Matthew Garrett237d8442007-10-03 01:24:16 +010019#include <scsi/scsi_device.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070020#include "libata.h"
21
22#include <acpi/acpi_bus.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070023
Tejun Heo110f66d2009-09-16 04:17:28 +090024unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
Tejun Heo3264a8d2007-12-15 15:05:06 +090025module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
Tejun Heofa5b5612009-09-16 04:17:02 +090026MODULE_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 +090027
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070028#define NO_PORT_MULT 0xffff
Jeff Garzik2dcb4072007-10-19 06:42:56 -040029#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070030
31#define REGS_PER_GTF 7
Tejun Heo4700c4b2007-05-15 03:28:16 +090032struct ata_acpi_gtf {
33 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
34} __packed;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070035
Alan Coxca426632007-03-08 23:13:50 +000036/*
37 * Helper - belongs in the PCI layer somewhere eventually
38 */
39static int is_pci_dev(struct device *dev)
40{
41 return (dev->bus == &pci_bus_type);
42}
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070043
Tejun Heo398e0782007-12-15 15:05:03 +090044static void ata_acpi_clear_gtf(struct ata_device *dev)
45{
46 kfree(dev->gtf_cache);
47 dev->gtf_cache = NULL;
48}
49
Matthew Garrett30dcf762012-06-25 16:13:04 +080050/**
51 * ata_ap_acpi_handle - provide the acpi_handle for an ata_port
52 * @ap: the acpi_handle returned will correspond to this port
53 *
54 * Returns the acpi_handle for the ACPI namespace object corresponding to
55 * the ata_port passed into the function, or NULL if no such object exists
56 */
57acpi_handle ata_ap_acpi_handle(struct ata_port *ap)
Matthew Garrett6b66d952012-06-25 16:13:03 +080058{
59 if (ap->flags & ATA_FLAG_ACPI_SATA)
60 return NULL;
61
62 /*
63 * If acpi bind operation has already happened, we can get the handle
64 * for the port by checking the corresponding scsi_host device's
65 * firmware node, otherwise we will need to find out the handle from
66 * its parent's acpi node.
67 */
68 if (ap->scsi_host)
69 return DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev);
70 else
71 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev),
72 ap->port_no);
73}
Matthew Garrett30dcf762012-06-25 16:13:04 +080074EXPORT_SYMBOL(ata_ap_acpi_handle);
Matthew Garrett6b66d952012-06-25 16:13:03 +080075
Matthew Garrett30dcf762012-06-25 16:13:04 +080076/**
77 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
78 * @dev: the acpi_device returned will correspond to this port
79 *
80 * Returns the acpi_handle for the ACPI namespace object corresponding to
81 * the ata_device passed into the function, or NULL if no such object exists
82 */
83acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
Matthew Garrett6b66d952012-06-25 16:13:03 +080084{
85 acpi_integer adr;
86 struct ata_port *ap = dev->link->ap;
87
88 if (ap->flags & ATA_FLAG_ACPI_SATA) {
89 if (!sata_pmp_attached(ap))
90 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
91 else
92 adr = SATA_ADR(ap->port_no, dev->link->pmp);
93 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
94 } else
Matthew Garrett30dcf762012-06-25 16:13:04 +080095 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
Matthew Garrett6b66d952012-06-25 16:13:03 +080096}
Matthew Garrett30dcf762012-06-25 16:13:04 +080097EXPORT_SYMBOL(ata_dev_acpi_handle);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070098
Holger Macht664d0802008-06-03 20:27:59 +020099/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
100static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
101{
102 if (dev)
103 dev->flags |= ATA_DFLAG_DETACH;
104 else {
105 struct ata_link *tlink;
106 struct ata_device *tdev;
107
Tejun Heo1eca4362008-11-03 20:03:17 +0900108 ata_for_each_link(tlink, ap, EDGE)
109 ata_for_each_dev(tdev, tlink, ALL)
Holger Macht664d0802008-06-03 20:27:59 +0200110 tdev->flags |= ATA_DFLAG_DETACH;
111 }
112
113 ata_port_schedule_eh(ap);
114}
115
116/**
117 * ata_acpi_handle_hotplug - ACPI event handler backend
118 * @ap: ATA port ACPI event occurred
119 * @dev: ATA device ACPI event occurred (can be NULL)
120 * @event: ACPI event which occurred
Holger Macht664d0802008-06-03 20:27:59 +0200121 *
122 * All ACPI bay / device realted events end up in this function. If
123 * the event is port-wide @dev is NULL. If the event is specific to a
124 * device, @dev points to it.
125 *
126 * Hotplug (as opposed to unplug) notification is always handled as
127 * port-wide while unplug only kills the target device on device-wide
128 * event.
129 *
130 * LOCKING:
131 * ACPI notify handler context. May sleep.
132 */
133static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
Shaohua Lif730ae12008-08-28 10:05:45 +0800134 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +0100135{
Holger Macht664d0802008-06-03 20:27:59 +0200136 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo233f1122008-03-12 14:24:43 +0900137 int wait = 0;
138 unsigned long flags;
Zhang Rui3c1e3892008-07-11 09:42:03 -0400139
Holger Macht664d0802008-06-03 20:27:59 +0200140 spin_lock_irqsave(ap->lock, flags);
Shaohua Lif730ae12008-08-28 10:05:45 +0800141 /*
142 * When dock driver calls into the routine, it will always use
143 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
144 * ACPI_NOTIFY_EJECT_REQUEST for remove
145 */
Tejun Heo233f1122008-03-12 14:24:43 +0900146 switch (event) {
147 case ACPI_NOTIFY_BUS_CHECK:
148 case ACPI_NOTIFY_DEVICE_CHECK:
149 ata_ehi_push_desc(ehi, "ACPI event");
Jeff Garzikc85665f2008-05-19 17:56:10 -0400150
Shaohua Lif730ae12008-08-28 10:05:45 +0800151 ata_ehi_hotplugged(ehi);
152 ata_port_freeze(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200153 break;
154 case ACPI_NOTIFY_EJECT_REQUEST:
155 ata_ehi_push_desc(ehi, "ACPI event");
156
Holger Macht664d0802008-06-03 20:27:59 +0200157 ata_acpi_detach_device(ap, dev);
158 wait = 1;
159 break;
Matthew Garrett237d8442007-10-03 01:24:16 +0100160 }
161
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100162 spin_unlock_irqrestore(ap->lock, flags);
163
Shaohua Lif730ae12008-08-28 10:05:45 +0800164 if (wait)
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100165 ata_port_wait_eh(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200166}
167
168static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
169{
170 struct ata_device *dev = data;
171
Shaohua Lif730ae12008-08-28 10:05:45 +0800172 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
Holger Macht664d0802008-06-03 20:27:59 +0200173}
174
175static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
176{
177 struct ata_port *ap = data;
178
Shaohua Lif730ae12008-08-28 10:05:45 +0800179 ata_acpi_handle_hotplug(ap, NULL, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100180}
181
Shaohua Li1253f7a2008-08-28 10:06:16 +0800182static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
183 u32 event)
184{
185 struct kobject *kobj = NULL;
186 char event_string[20];
187 char *envp[] = { event_string, NULL };
188
189 if (dev) {
190 if (dev->sdev)
191 kobj = &dev->sdev->sdev_gendev.kobj;
192 } else
193 kobj = &ap->dev->kobj;
194
195 if (kobj) {
196 snprintf(event_string, 20, "BAY_EVENT=%d", event);
197 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
198 }
199}
200
201static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
202{
203 ata_acpi_uevent(data, NULL, event);
204}
205
206static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
207{
208 struct ata_device *dev = data;
209 ata_acpi_uevent(dev->link->ap, dev, event);
210}
211
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400212static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800213 .handler = ata_acpi_dev_notify_dock,
214 .uevent = ata_acpi_dev_uevent,
215};
216
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400217static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800218 .handler = ata_acpi_ap_notify_dock,
219 .uevent = ata_acpi_ap_uevent,
220};
221
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700222/**
Tejun Heo562f0c22007-12-15 15:05:01 +0900223 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
224 * @host: target ATA host
225 *
226 * This function is called during driver detach after the whole host
227 * is shut down.
228 *
229 * LOCKING:
230 * EH context.
231 */
232void ata_acpi_dissociate(struct ata_host *host)
233{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900234 int i;
235
236 /* Restore initial _GTM values so that driver which attaches
237 * afterward can use them too.
238 */
239 for (i = 0; i < host->n_ports; i++) {
240 struct ata_port *ap = host->ports[i];
241 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
242
Matthew Garrett30dcf762012-06-25 16:13:04 +0800243 if (ata_ap_acpi_handle(ap) && gtm)
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900244 ata_acpi_stm(ap, gtm);
245 }
Tejun Heo562f0c22007-12-15 15:05:01 +0900246}
247
248/**
Tejun Heo64578a3de2007-05-15 03:28:16 +0900249 * ata_acpi_gtm - execute _GTM
250 * @ap: target ATA port
251 * @gtm: out parameter for _GTM result
252 *
253 * Evaluate _GTM and store the result in @gtm.
254 *
255 * LOCKING:
256 * EH context.
257 *
258 * RETURNS:
259 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
260 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900261int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
Tejun Heo64578a3de2007-05-15 03:28:16 +0900262{
263 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
264 union acpi_object *out_obj;
265 acpi_status status;
266 int rc = 0;
267
Matthew Garrett30dcf762012-06-25 16:13:04 +0800268 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
269 &output);
Tejun Heo64578a3de2007-05-15 03:28:16 +0900270
271 rc = -ENOENT;
272 if (status == AE_NOT_FOUND)
273 goto out_free;
274
275 rc = -EINVAL;
276 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700277 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
278 status);
Tejun Heo64578a3de2007-05-15 03:28:16 +0900279 goto out_free;
280 }
281
282 out_obj = output.pointer;
283 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700284 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
285 out_obj->type);
Tejun Heo64578a3de2007-05-15 03:28:16 +0900286
287 goto out_free;
288 }
289
290 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700291 ata_port_err(ap, "_GTM returned invalid length %d\n",
292 out_obj->buffer.length);
Tejun Heo64578a3de2007-05-15 03:28:16 +0900293 goto out_free;
294 }
295
296 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
297 rc = 0;
298 out_free:
299 kfree(output.pointer);
300 return rc;
301}
302
Alan Coxbadff032007-10-04 21:28:18 +0100303EXPORT_SYMBOL_GPL(ata_acpi_gtm);
304
Tejun Heo64578a3de2007-05-15 03:28:16 +0900305/**
306 * ata_acpi_stm - execute _STM
307 * @ap: target ATA port
308 * @stm: timing parameter to _STM
309 *
310 * Evaluate _STM with timing parameter @stm.
311 *
312 * LOCKING:
313 * EH context.
314 *
315 * RETURNS:
316 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
317 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900318int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a3de2007-05-15 03:28:16 +0900319{
320 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900321 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900322 struct acpi_object_list input;
323 union acpi_object in_params[3];
324
325 in_params[0].type = ACPI_TYPE_BUFFER;
326 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900327 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900328 /* Buffers for id may need byteswapping ? */
329 in_params[1].type = ACPI_TYPE_BUFFER;
330 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900331 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900332 in_params[2].type = ACPI_TYPE_BUFFER;
333 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900334 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900335
336 input.count = 3;
337 input.pointer = in_params;
338
Matthew Garrett30dcf762012-06-25 16:13:04 +0800339 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
340 NULL);
Tejun Heo64578a3de2007-05-15 03:28:16 +0900341
342 if (status == AE_NOT_FOUND)
343 return -ENOENT;
344 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700345 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
346 status);
Tejun Heo64578a3de2007-05-15 03:28:16 +0900347 return -EINVAL;
348 }
349 return 0;
350}
351
Alan Coxbadff032007-10-04 21:28:18 +0100352EXPORT_SYMBOL_GPL(ata_acpi_stm);
353
Tejun Heo64578a3de2007-05-15 03:28:16 +0900354/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900355 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900356 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900357 * @gtf: output parameter for buffer containing _GTF taskfile arrays
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700358 *
359 * This applies to both PATA and SATA drives.
360 *
361 * The _GTF method has no input parameters.
362 * It returns a variable number of register set values (registers
363 * hex 1F1..1F7, taskfiles).
364 * The <variable number> is not known in advance, so have ACPI-CA
365 * allocate the buffer as needed and return it, then free it later.
366 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900367 * LOCKING:
368 * EH context.
369 *
370 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900371 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
372 * if _GTF is invalid.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700373 */
Tejun Heo398e0782007-12-15 15:05:03 +0900374static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700375{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900376 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900377 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900378 struct acpi_buffer output;
379 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900380 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700381
Tejun Heo398e0782007-12-15 15:05:03 +0900382 /* if _GTF is cached, use the cached value */
383 if (dev->gtf_cache) {
384 out_obj = dev->gtf_cache;
385 goto done;
386 }
387
Tejun Heo4700c4b2007-05-15 03:28:16 +0900388 /* set up output buffer */
389 output.length = ACPI_ALLOCATE_BUFFER;
390 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700391
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700392 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700393 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
394 __func__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700395
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700396 /* _GTF has no input parameters */
Matthew Garrett30dcf762012-06-25 16:13:04 +0800397 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
398 &output);
Tejun Heo398e0782007-12-15 15:05:03 +0900399 out_obj = dev->gtf_cache = output.pointer;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900400
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700401 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900402 if (status != AE_NOT_FOUND) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700403 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
404 status);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900405 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900406 }
407 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700408 }
409
410 if (!output.length || !output.pointer) {
411 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700412 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
413 __func__,
414 (unsigned long long)output.length,
415 output.pointer);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900416 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900417 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700418 }
419
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700420 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700421 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
422 out_obj->type);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900423 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900424 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700425 }
426
Tejun Heo4700c4b2007-05-15 03:28:16 +0900427 if (out_obj->buffer.length % REGS_PER_GTF) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700428 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
429 out_obj->buffer.length);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900430 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900431 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700432 }
433
Tejun Heo398e0782007-12-15 15:05:03 +0900434 done:
Tejun Heo4700c4b2007-05-15 03:28:16 +0900435 rc = out_obj->buffer.length / REGS_PER_GTF;
Tejun Heo398e0782007-12-15 15:05:03 +0900436 if (gtf) {
437 *gtf = (void *)out_obj->buffer.pointer;
438 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700439 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
440 __func__, *gtf, rc);
Tejun Heo398e0782007-12-15 15:05:03 +0900441 }
Tejun Heo4700c4b2007-05-15 03:28:16 +0900442 return rc;
443
444 out_free:
Tejun Heo398e0782007-12-15 15:05:03 +0900445 ata_acpi_clear_gtf(dev);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900446 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700447}
448
Tejun Heo7c77fa42007-12-18 16:33:03 +0900449/**
450 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
451 * @dev: target device
452 * @gtm: GTM parameter to use
453 *
454 * Determine xfermask for @dev from @gtm.
455 *
456 * LOCKING:
457 * None.
458 *
459 * RETURNS:
460 * Determined xfermask.
461 */
462unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
463 const struct ata_acpi_gtm *gtm)
464{
Tejun Heoa0f79b92007-12-18 16:33:05 +0900465 unsigned long xfer_mask = 0;
466 unsigned int type;
467 int unit;
468 u8 mode;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900469
470 /* we always use the 0 slot for crap hardware */
471 unit = dev->devno;
472 if (!(gtm->flags & 0x10))
473 unit = 0;
474
Tejun Heoa0f79b92007-12-18 16:33:05 +0900475 /* PIO */
476 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
477 xfer_mask |= ata_xfer_mode2mask(mode);
Tejun Heo7c77fa42007-12-18 16:33:03 +0900478
479 /* See if we have MWDMA or UDMA data. We don't bother with
480 * MWDMA if UDMA is available as this means the BIOS set UDMA
481 * and our error changedown if it works is UDMA to PIO anyway.
482 */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900483 if (!(gtm->flags & (1 << (2 * unit))))
484 type = ATA_SHIFT_MWDMA;
485 else
486 type = ATA_SHIFT_UDMA;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900487
Tejun Heoa0f79b92007-12-18 16:33:05 +0900488 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
489 xfer_mask |= ata_xfer_mode2mask(mode);
490
491 return xfer_mask;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900492}
493EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
494
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700495/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400496 * ata_acpi_cbl_80wire - Check for 80 wire cable
497 * @ap: Port to check
Tejun Heo021ee9a2007-12-18 16:33:06 +0900498 * @gtm: GTM data to use
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400499 *
Tejun Heo021ee9a2007-12-18 16:33:06 +0900500 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400501 */
Tejun Heo021ee9a2007-12-18 16:33:06 +0900502int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400503{
Tejun Heo021ee9a2007-12-18 16:33:06 +0900504 struct ata_device *dev;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400505
Tejun Heo1eca4362008-11-03 20:03:17 +0900506 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo021ee9a2007-12-18 16:33:06 +0900507 unsigned long xfer_mask, udma_mask;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400508
Tejun Heo021ee9a2007-12-18 16:33:06 +0900509 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
510 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
511
512 if (udma_mask & ~ATA_UDMA_MASK_40C)
513 return 1;
514 }
515
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400516 return 0;
517}
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400518EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
519
Tejun Heo3264a8d2007-12-15 15:05:06 +0900520static void ata_acpi_gtf_to_tf(struct ata_device *dev,
521 const struct ata_acpi_gtf *gtf,
522 struct ata_taskfile *tf)
523{
524 ata_tf_init(dev, tf);
525
526 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
527 tf->protocol = ATA_PROT_NODATA;
528 tf->feature = gtf->tf[0]; /* 0x1f1 */
529 tf->nsect = gtf->tf[1]; /* 0x1f2 */
530 tf->lbal = gtf->tf[2]; /* 0x1f3 */
531 tf->lbam = gtf->tf[3]; /* 0x1f4 */
532 tf->lbah = gtf->tf[4]; /* 0x1f5 */
533 tf->device = gtf->tf[5]; /* 0x1f6 */
534 tf->command = gtf->tf[6]; /* 0x1f7 */
535}
536
Tejun Heo110f66d2009-09-16 04:17:28 +0900537static int ata_acpi_filter_tf(struct ata_device *dev,
538 const struct ata_taskfile *tf,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900539 const struct ata_taskfile *ptf)
540{
Tejun Heo110f66d2009-09-16 04:17:28 +0900541 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900542 /* libata doesn't use ACPI to configure transfer mode.
543 * It will only confuse device configuration. Skip.
544 */
545 if (tf->command == ATA_CMD_SET_FEATURES &&
546 tf->feature == SETFEATURES_XFER)
547 return 1;
548 }
549
Tejun Heo110f66d2009-09-16 04:17:28 +0900550 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900551 /* BIOS writers, sorry but we don't wanna lock
552 * features unless the user explicitly said so.
553 */
554
555 /* DEVICE CONFIGURATION FREEZE LOCK */
556 if (tf->command == ATA_CMD_CONF_OVERLAY &&
557 tf->feature == ATA_DCO_FREEZE_LOCK)
558 return 1;
559
560 /* SECURITY FREEZE LOCK */
561 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
562 return 1;
563
564 /* SET MAX LOCK and SET MAX FREEZE LOCK */
565 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
566 tf->command == ATA_CMD_SET_MAX &&
567 (tf->feature == ATA_SET_MAX_LOCK ||
568 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
569 return 1;
570 }
571
Tejun Heofa5b5612009-09-16 04:17:02 +0900572 if (tf->command == ATA_CMD_SET_FEATURES &&
573 tf->feature == SETFEATURES_SATA_ENABLE) {
Tejun Heob3449912008-07-06 23:15:03 +0900574 /* inhibit enabling DIPM */
Tejun Heo110f66d2009-09-16 04:17:28 +0900575 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
Tejun Heob3449912008-07-06 23:15:03 +0900576 tf->nsect == SATA_DIPM)
577 return 1;
Tejun Heofa5b5612009-09-16 04:17:02 +0900578
579 /* inhibit FPDMA non-zero offset */
Tejun Heo110f66d2009-09-16 04:17:28 +0900580 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900581 (tf->nsect == SATA_FPDMA_OFFSET ||
582 tf->nsect == SATA_FPDMA_IN_ORDER))
583 return 1;
584
585 /* inhibit FPDMA auto activation */
Tejun Heo110f66d2009-09-16 04:17:28 +0900586 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900587 tf->nsect == SATA_FPDMA_AA)
588 return 1;
Tejun Heob3449912008-07-06 23:15:03 +0900589 }
590
Tejun Heo3264a8d2007-12-15 15:05:06 +0900591 return 0;
592}
593
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400594/**
Tejun Heo0e8634b2007-12-15 15:05:05 +0900595 * ata_acpi_run_tf - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900596 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700597 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
598 *
Sergei Shtylyov3696df32011-02-04 22:04:17 +0300599 * Outputs ATA taskfile to standard ATA host controller.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700600 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
601 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
602 * hob_lbal, hob_lbam, and hob_lbah.
603 *
604 * This function waits for idle (!BUSY and !DRQ) after writing
605 * registers. If the control register has a new value, this
606 * function also waits for idle after writing control and before
607 * writing the remaining registers.
608 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900609 * LOCKING:
610 * EH context.
611 *
612 * RETURNS:
Tejun Heo3264a8d2007-12-15 15:05:06 +0900613 * 1 if command is executed successfully. 0 if ignored, rejected or
614 * filtered out, -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700615 */
Tejun Heo0e8634b2007-12-15 15:05:05 +0900616static int ata_acpi_run_tf(struct ata_device *dev,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900617 const struct ata_acpi_gtf *gtf,
618 const struct ata_acpi_gtf *prev_gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700619{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900620 struct ata_taskfile *pptf = NULL;
621 struct ata_taskfile tf, ptf, rtf;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900622 unsigned int err_mask;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900623 const char *level;
Robert Hancock65211482009-07-14 20:43:39 -0600624 const char *descr;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900625 char msg[60];
626 int rc;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500627
Tejun Heo4700c4b2007-05-15 03:28:16 +0900628 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
629 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
630 && (gtf->tf[6] == 0))
631 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700632
Tejun Heo3264a8d2007-12-15 15:05:06 +0900633 ata_acpi_gtf_to_tf(dev, gtf, &tf);
634 if (prev_gtf) {
635 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
636 pptf = &ptf;
637 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700638
Tejun Heo110f66d2009-09-16 04:17:28 +0900639 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900640 rtf = tf;
641 err_mask = ata_exec_internal(dev, &rtf, NULL,
642 DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700643
Tejun Heo3264a8d2007-12-15 15:05:06 +0900644 switch (err_mask) {
645 case 0:
646 level = KERN_DEBUG;
647 snprintf(msg, sizeof(msg), "succeeded");
648 rc = 1;
649 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900650
Tejun Heo3264a8d2007-12-15 15:05:06 +0900651 case AC_ERR_DEV:
652 level = KERN_INFO;
653 snprintf(msg, sizeof(msg),
654 "rejected by device (Stat=0x%02x Err=0x%02x)",
655 rtf.command, rtf.feature);
656 rc = 0;
657 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900658
Tejun Heo3264a8d2007-12-15 15:05:06 +0900659 default:
660 level = KERN_ERR;
661 snprintf(msg, sizeof(msg),
662 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
663 err_mask, rtf.command, rtf.feature);
664 rc = -EIO;
665 break;
666 }
667 } else {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900668 level = KERN_INFO;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900669 snprintf(msg, sizeof(msg), "filtered out");
Tejun Heo0e8634b2007-12-15 15:05:05 +0900670 rc = 0;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900671 }
Robert Hancock65211482009-07-14 20:43:39 -0600672 descr = ata_get_cmd_descript(tf.command);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900673
Tejun Heo0e8634b2007-12-15 15:05:05 +0900674 ata_dev_printk(dev, level,
Robert Hancock65211482009-07-14 20:43:39 -0600675 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
Tejun Heo0e8634b2007-12-15 15:05:05 +0900676 tf.command, tf.feature, tf.nsect, tf.lbal,
Robert Hancock65211482009-07-14 20:43:39 -0600677 tf.lbam, tf.lbah, tf.device,
678 (descr ? descr : "unknown"), msg);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900679
680 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700681}
682
683/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700684 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900685 * @dev: target ATA device
Martin Olsson98a17082009-04-22 18:21:29 +0200686 * @nr_executed: out parameter for the number of executed commands
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700687 *
Martin Olsson98a17082009-04-22 18:21:29 +0200688 * Evaluate _GTF and execute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900689 *
690 * LOCKING:
691 * EH context.
692 *
693 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900694 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
695 * -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700696 */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900697static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700698{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900699 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
Tejun Heo67465442007-05-15 03:28:16 +0900700 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700701
Tejun Heo67465442007-05-15 03:28:16 +0900702 /* get taskfiles */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900703 rc = ata_dev_get_GTF(dev, &gtf);
704 if (rc < 0)
705 return rc;
706 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700707
Tejun Heo67465442007-05-15 03:28:16 +0900708 /* execute them */
Tejun Heo3264a8d2007-12-15 15:05:06 +0900709 for (i = 0; i < gtf_count; i++, gtf++) {
710 rc = ata_acpi_run_tf(dev, gtf, pgtf);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900711 if (rc < 0)
712 break;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900713 if (rc) {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900714 (*nr_executed)++;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900715 pgtf = gtf;
716 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700717 }
718
Tejun Heo398e0782007-12-15 15:05:03 +0900719 ata_acpi_clear_gtf(dev);
Tejun Heo67465442007-05-15 03:28:16 +0900720
Tejun Heo0e8634b2007-12-15 15:05:05 +0900721 if (rc < 0)
722 return rc;
723 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700724}
725
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700726/**
727 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900728 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700729 *
730 * _SDD ACPI object: for SATA mode only
731 * Must be after Identify (Packet) Device -- uses its data
732 * ATM this function never returns a failure. It is an optional
733 * method and if it fails for whatever reason, we should still
734 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900735 *
736 * LOCKING:
737 * EH context.
738 *
739 * RETURNS:
Tejun Heof2406772009-11-18 22:24:21 +0900740 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700741 */
Tejun Heo67465442007-05-15 03:28:16 +0900742static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700743{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900744 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900745 acpi_status status;
746 struct acpi_object_list input;
747 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700748
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700749 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700750 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
751 __func__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700752
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700753 /* Give the drive Identify data to the drive via the _SDD method */
754 /* _SDD: set up input parameters */
755 input.count = 1;
756 input.pointer = in_params;
757 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900758 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
759 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700760 /* Output buffer: _SDD has no output */
761
762 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900763 swap_buf_le16(dev->id, ATA_ID_WORDS);
Matthew Garrett30dcf762012-06-25 16:13:04 +0800764 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
765 NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900766 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700767
Tejun Heof2406772009-11-18 22:24:21 +0900768 if (status == AE_NOT_FOUND)
769 return -ENOENT;
770
771 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700772 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
Tejun Heof2406772009-11-18 22:24:21 +0900773 return -EIO;
774 }
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700775
Tejun Heof2406772009-11-18 22:24:21 +0900776 return 0;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700777}
778
Tejun Heo67465442007-05-15 03:28:16 +0900779/**
Tejun Heo64578a3de2007-05-15 03:28:16 +0900780 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
781 * @ap: target ATA port
782 *
783 * This function is called when @ap is about to be suspended. All
784 * devices are already put to sleep but the port_suspend() callback
785 * hasn't been executed yet. Error return from this function aborts
786 * suspend.
787 *
788 * LOCKING:
789 * EH context.
790 *
791 * RETURNS:
792 * 0 on success, -errno on failure.
793 */
794int ata_acpi_on_suspend(struct ata_port *ap)
795{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900796 /* nada */
797 return 0;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900798}
799
800/**
Tejun Heo67465442007-05-15 03:28:16 +0900801 * ata_acpi_on_resume - ATA ACPI hook called on resume
802 * @ap: target ATA port
803 *
804 * This function is called when @ap is resumed - right after port
805 * itself is resumed but before any EH action is taken.
806 *
807 * LOCKING:
808 * EH context.
809 */
810void ata_acpi_on_resume(struct ata_port *ap)
811{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900812 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Tejun Heof58229f2007-08-06 18:36:23 +0900813 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700814
Matthew Garrett30dcf762012-06-25 16:13:04 +0800815 if (ata_ap_acpi_handle(ap) && gtm) {
Tejun Heo398e0782007-12-15 15:05:03 +0900816 /* _GTM valid */
817
818 /* restore timing parameters */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900819 ata_acpi_stm(ap, gtm);
Tejun Heo64578a3de2007-05-15 03:28:16 +0900820
Tejun Heo398e0782007-12-15 15:05:03 +0900821 /* _GTF should immediately follow _STM so that it can
822 * use values set by _STM. Cache _GTF result and
823 * schedule _GTF.
824 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900825 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900826 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800827 if (ata_dev_enabled(dev) &&
828 ata_dev_get_GTF(dev, NULL) >= 0)
Tejun Heo398e0782007-12-15 15:05:03 +0900829 dev->flags |= ATA_DFLAG_ACPI_PENDING;
830 }
831 } else {
832 /* SATA _GTF needs to be evaulated after _SDD and
833 * there's no reason to evaluate IDE _GTF early
834 * without _STM. Clear cache and schedule _GTF.
835 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900836 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900837 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800838 if (ata_dev_enabled(dev))
839 dev->flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo398e0782007-12-15 15:05:03 +0900840 }
841 }
Tejun Heo67465442007-05-15 03:28:16 +0900842}
843
844/**
Shaohua Libd3adca2007-11-02 09:32:38 +0800845 * ata_acpi_set_state - set the port power state
846 * @ap: target ATA port
847 * @state: state, on/off
848 *
849 * This function executes the _PS0/_PS3 ACPI method to set the power state.
850 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
851 */
852void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
853{
854 struct ata_device *dev;
855
Matthew Garrett30dcf762012-06-25 16:13:04 +0800856 if (!ata_ap_acpi_handle(ap) || (ap->flags & ATA_FLAG_ACPI_SATA))
Shaohua Libd3adca2007-11-02 09:32:38 +0800857 return;
858
859 /* channel first and then drives for power on and vica versa
860 for power off */
861 if (state.event == PM_EVENT_ON)
Matthew Garrett30dcf762012-06-25 16:13:04 +0800862 acpi_bus_set_power(ata_ap_acpi_handle(ap), ACPI_STATE_D0);
Shaohua Libd3adca2007-11-02 09:32:38 +0800863
Tejun Heo1eca4362008-11-03 20:03:17 +0900864 ata_for_each_dev(dev, &ap->link, ENABLED) {
Matthew Garrett30dcf762012-06-25 16:13:04 +0800865 if (ata_dev_acpi_handle(dev))
866 acpi_bus_set_power(ata_dev_acpi_handle(dev),
Shaohua Libd3adca2007-11-02 09:32:38 +0800867 state.event == PM_EVENT_ON ?
868 ACPI_STATE_D0 : ACPI_STATE_D3);
869 }
870 if (state.event != PM_EVENT_ON)
Matthew Garrett30dcf762012-06-25 16:13:04 +0800871 acpi_bus_set_power(ata_ap_acpi_handle(ap), ACPI_STATE_D3);
Shaohua Libd3adca2007-11-02 09:32:38 +0800872}
873
874/**
Tejun Heo67465442007-05-15 03:28:16 +0900875 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
876 * @dev: target ATA device
877 *
878 * This function is called when @dev is about to be configured.
879 * IDENTIFY data might have been modified after this hook is run.
880 *
881 * LOCKING:
882 * EH context.
883 *
884 * RETURNS:
885 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
886 * -errno on failure.
887 */
888int ata_acpi_on_devcfg(struct ata_device *dev)
889{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900890 struct ata_port *ap = dev->link->ap;
891 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900892 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
Tejun Heo66fa7f22007-12-15 15:05:04 +0900893 int nr_executed = 0;
Tejun Heo67465442007-05-15 03:28:16 +0900894 int rc;
895
Matthew Garrett30dcf762012-06-25 16:13:04 +0800896 if (!ata_dev_acpi_handle(dev))
Tejun Heo67465442007-05-15 03:28:16 +0900897 return 0;
898
899 /* do we need to do _GTF? */
900 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
901 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
902 return 0;
903
904 /* do _SDD if SATA */
905 if (acpi_sata) {
906 rc = ata_acpi_push_id(dev);
Tejun Heof2406772009-11-18 22:24:21 +0900907 if (rc && rc != -ENOENT)
Tejun Heo67465442007-05-15 03:28:16 +0900908 goto acpi_err;
909 }
910
911 /* do _GTF */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900912 rc = ata_acpi_exec_tfs(dev, &nr_executed);
913 if (rc)
Tejun Heo67465442007-05-15 03:28:16 +0900914 goto acpi_err;
915
916 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
917
918 /* refresh IDENTIFY page if any _GTF command has been executed */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900919 if (nr_executed) {
Tejun Heo67465442007-05-15 03:28:16 +0900920 rc = ata_dev_reread_id(dev, 0);
921 if (rc < 0) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700922 ata_dev_err(dev,
923 "failed to IDENTIFY after ACPI commands\n");
Tejun Heo67465442007-05-15 03:28:16 +0900924 return rc;
925 }
926 }
927
928 return 0;
929
930 acpi_err:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900931 /* ignore evaluation failure if we can continue safely */
932 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
933 return 0;
Tejun Heo67465442007-05-15 03:28:16 +0900934
Tejun Heo66fa7f22007-12-15 15:05:04 +0900935 /* fail and let EH retry once more for unknown IO errors */
936 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
937 dev->flags |= ATA_DFLAG_ACPI_FAILED;
938 return rc;
Tejun Heo67465442007-05-15 03:28:16 +0900939 }
Tejun Heo66fa7f22007-12-15 15:05:04 +0900940
Joe Perchesa9a79df2011-04-15 15:51:59 -0700941 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
Tejun Heo66fa7f22007-12-15 15:05:04 +0900942
943 /* We can safely continue if no _GTF command has been executed
944 * and port is not frozen.
945 */
946 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
947 return 0;
948
Tejun Heo67465442007-05-15 03:28:16 +0900949 return rc;
950}
Tejun Heo562f0c22007-12-15 15:05:01 +0900951
952/**
953 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
954 * @dev: target ATA device
955 *
956 * This function is called when @dev is about to be disabled.
957 *
958 * LOCKING:
959 * EH context.
960 */
961void ata_acpi_on_disable(struct ata_device *dev)
962{
Tejun Heo398e0782007-12-15 15:05:03 +0900963 ata_acpi_clear_gtf(dev);
Tejun Heo562f0c22007-12-15 15:05:01 +0900964}
Matthew Garrett6b66d952012-06-25 16:13:03 +0800965
966static int compat_pci_ata(struct ata_port *ap)
967{
968 struct device *dev = ap->tdev.parent;
969 struct pci_dev *pdev;
970
971 if (!is_pci_dev(dev))
972 return 0;
973
974 pdev = to_pci_dev(dev);
975
976 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
977 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
978 return 0;
979
980 return 1;
981}
982
983static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
984{
985 if (ap->flags & ATA_FLAG_ACPI_SATA)
986 return -ENODEV;
987
988 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
989 ap->port_no);
990
991 if (!*handle)
992 return -ENODEV;
993
994 return 0;
995}
996
997static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
998 acpi_handle *handle)
999{
1000 struct ata_device *ata_dev;
1001
1002 if (ap->flags & ATA_FLAG_ACPI_SATA)
1003 ata_dev = &ap->link.device[sdev->channel];
1004 else
1005 ata_dev = &ap->link.device[sdev->id];
1006
Matthew Garrett30dcf762012-06-25 16:13:04 +08001007 *handle = ata_dev_acpi_handle(ata_dev);
Matthew Garrett6b66d952012-06-25 16:13:03 +08001008
1009 if (!*handle)
1010 return -ENODEV;
1011
1012 return 0;
1013}
1014
1015static int is_ata_port(const struct device *dev)
1016{
1017 return dev->type == &ata_port_type;
1018}
1019
1020static struct ata_port *dev_to_ata_port(struct device *dev)
1021{
1022 while (!is_ata_port(dev)) {
1023 if (!dev->parent)
1024 return NULL;
1025 dev = dev->parent;
1026 }
1027 return to_ata_port(dev);
1028}
1029
1030static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1031{
1032 struct ata_port *ap = dev_to_ata_port(dev);
1033
1034 if (!ap)
1035 return -ENODEV;
1036
1037 if (!compat_pci_ata(ap))
1038 return -ENODEV;
1039
1040 if (scsi_is_host_device(dev))
1041 return ata_acpi_bind_host(ap, handle);
1042 else if (scsi_is_sdev_device(dev)) {
1043 struct scsi_device *sdev = to_scsi_device(dev);
1044
1045 return ata_acpi_bind_device(ap, sdev, handle);
1046 } else
1047 return -ENODEV;
1048}
1049
1050static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1051{
1052 return -ENODEV;
1053}
1054
1055static struct acpi_bus_type ata_acpi_bus = {
1056 .find_bridge = ata_acpi_find_dummy,
1057 .find_device = ata_acpi_find_device,
1058};
1059
1060int ata_acpi_register(void)
1061{
1062 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1063}
1064
1065void ata_acpi_unregister(void)
1066{
1067 scsi_unregister_acpi_bus_type(&ata_acpi_bus);
1068}