blob: b3025a7d71e3b402b1012a0920f4e49dbd7af3ea [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 Garrett6b66d952012-06-25 16:13:03 +080050static acpi_handle ap_acpi_handle(struct ata_port *ap)
51{
52 if (ap->flags & ATA_FLAG_ACPI_SATA)
53 return NULL;
54
55 /*
56 * If acpi bind operation has already happened, we can get the handle
57 * for the port by checking the corresponding scsi_host device's
58 * firmware node, otherwise we will need to find out the handle from
59 * its parent's acpi node.
60 */
61 if (ap->scsi_host)
62 return DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev);
63 else
64 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev),
65 ap->port_no);
66}
67
68static acpi_handle dev_acpi_handle(struct ata_device *dev)
69{
70 acpi_integer adr;
71 struct ata_port *ap = dev->link->ap;
72
73 if (ap->flags & ATA_FLAG_ACPI_SATA) {
74 if (!sata_pmp_attached(ap))
75 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
76 else
77 adr = SATA_ADR(ap->port_no, dev->link->pmp);
78 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
79 } else
80 return acpi_get_child(ap_acpi_handle(ap), dev->devno);
81}
82
Tejun Heod0df8b5d2007-09-23 13:19:54 +090083/**
84 * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
85 * @ap: target SATA port
86 *
87 * Look up ACPI objects associated with @ap and initialize acpi_handle
88 * fields of @ap, the port and devices accordingly.
89 *
90 * LOCKING:
91 * EH context.
92 *
93 * RETURNS:
94 * 0 on success, -errno on failure.
95 */
96void ata_acpi_associate_sata_port(struct ata_port *ap)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070097{
Tejun Heod0df8b5d2007-09-23 13:19:54 +090098 WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070099
Tejun Heo071f44b2008-04-07 22:47:22 +0900100 if (!sata_pmp_attached(ap)) {
Lin Ming439913f2010-01-28 10:53:19 +0800101 u64 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
Tejun Heod0df8b5d2007-09-23 13:19:54 +0900102
103 ap->link.device->acpi_handle =
104 acpi_get_child(ap->host->acpi_handle, adr);
105 } else {
106 struct ata_link *link;
107
108 ap->link.device->acpi_handle = NULL;
109
Tejun Heo1eca4362008-11-03 20:03:17 +0900110 ata_for_each_link(link, ap, EDGE) {
Lin Ming439913f2010-01-28 10:53:19 +0800111 u64 adr = SATA_ADR(ap->port_no, link->pmp);
Tejun Heod0df8b5d2007-09-23 13:19:54 +0900112
113 link->device->acpi_handle =
114 acpi_get_child(ap->host->acpi_handle, adr);
115 }
116 }
Tejun Heofafbae82007-05-15 03:28:16 +0900117}
Alan Coxca426632007-03-08 23:13:50 +0000118
Tejun Heofafbae82007-05-15 03:28:16 +0900119static void ata_acpi_associate_ide_port(struct ata_port *ap)
120{
121 int max_devices, i;
122
123 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
124 if (!ap->acpi_handle)
125 return;
126
127 max_devices = 1;
128 if (ap->flags & ATA_FLAG_SLAVE_POSS)
129 max_devices++;
130
131 for (i = 0; i < max_devices; i++) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900132 struct ata_device *dev = &ap->link.device[i];
Tejun Heofafbae82007-05-15 03:28:16 +0900133
134 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
135 }
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900136
137 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
138 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700139}
140
Holger Macht664d0802008-06-03 20:27:59 +0200141/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
142static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
143{
144 if (dev)
145 dev->flags |= ATA_DFLAG_DETACH;
146 else {
147 struct ata_link *tlink;
148 struct ata_device *tdev;
149
Tejun Heo1eca4362008-11-03 20:03:17 +0900150 ata_for_each_link(tlink, ap, EDGE)
151 ata_for_each_dev(tdev, tlink, ALL)
Holger Macht664d0802008-06-03 20:27:59 +0200152 tdev->flags |= ATA_DFLAG_DETACH;
153 }
154
155 ata_port_schedule_eh(ap);
156}
157
158/**
159 * ata_acpi_handle_hotplug - ACPI event handler backend
160 * @ap: ATA port ACPI event occurred
161 * @dev: ATA device ACPI event occurred (can be NULL)
162 * @event: ACPI event which occurred
Holger Macht664d0802008-06-03 20:27:59 +0200163 *
164 * All ACPI bay / device realted events end up in this function. If
165 * the event is port-wide @dev is NULL. If the event is specific to a
166 * device, @dev points to it.
167 *
168 * Hotplug (as opposed to unplug) notification is always handled as
169 * port-wide while unplug only kills the target device on device-wide
170 * event.
171 *
172 * LOCKING:
173 * ACPI notify handler context. May sleep.
174 */
175static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
Shaohua Lif730ae12008-08-28 10:05:45 +0800176 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +0100177{
Holger Macht664d0802008-06-03 20:27:59 +0200178 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo233f1122008-03-12 14:24:43 +0900179 int wait = 0;
180 unsigned long flags;
Zhang Rui3c1e3892008-07-11 09:42:03 -0400181
Holger Macht664d0802008-06-03 20:27:59 +0200182 spin_lock_irqsave(ap->lock, flags);
Shaohua Lif730ae12008-08-28 10:05:45 +0800183 /*
184 * When dock driver calls into the routine, it will always use
185 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
186 * ACPI_NOTIFY_EJECT_REQUEST for remove
187 */
Tejun Heo233f1122008-03-12 14:24:43 +0900188 switch (event) {
189 case ACPI_NOTIFY_BUS_CHECK:
190 case ACPI_NOTIFY_DEVICE_CHECK:
191 ata_ehi_push_desc(ehi, "ACPI event");
Jeff Garzikc85665f2008-05-19 17:56:10 -0400192
Shaohua Lif730ae12008-08-28 10:05:45 +0800193 ata_ehi_hotplugged(ehi);
194 ata_port_freeze(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200195 break;
196 case ACPI_NOTIFY_EJECT_REQUEST:
197 ata_ehi_push_desc(ehi, "ACPI event");
198
Holger Macht664d0802008-06-03 20:27:59 +0200199 ata_acpi_detach_device(ap, dev);
200 wait = 1;
201 break;
Matthew Garrett237d8442007-10-03 01:24:16 +0100202 }
203
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100204 spin_unlock_irqrestore(ap->lock, flags);
205
Shaohua Lif730ae12008-08-28 10:05:45 +0800206 if (wait)
Matthew Garrettae6c23c2008-05-19 17:29:34 +0100207 ata_port_wait_eh(ap);
Holger Macht664d0802008-06-03 20:27:59 +0200208}
209
210static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
211{
212 struct ata_device *dev = data;
213
Shaohua Lif730ae12008-08-28 10:05:45 +0800214 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
Holger Macht664d0802008-06-03 20:27:59 +0200215}
216
217static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
218{
219 struct ata_port *ap = data;
220
Shaohua Lif730ae12008-08-28 10:05:45 +0800221 ata_acpi_handle_hotplug(ap, NULL, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100222}
223
Shaohua Li1253f7a2008-08-28 10:06:16 +0800224static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
225 u32 event)
226{
227 struct kobject *kobj = NULL;
228 char event_string[20];
229 char *envp[] = { event_string, NULL };
230
231 if (dev) {
232 if (dev->sdev)
233 kobj = &dev->sdev->sdev_gendev.kobj;
234 } else
235 kobj = &ap->dev->kobj;
236
237 if (kobj) {
238 snprintf(event_string, 20, "BAY_EVENT=%d", event);
239 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
240 }
241}
242
243static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
244{
245 ata_acpi_uevent(data, NULL, event);
246}
247
248static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
249{
250 struct ata_device *dev = data;
251 ata_acpi_uevent(dev->link->ap, dev, event);
252}
253
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400254static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800255 .handler = ata_acpi_dev_notify_dock,
256 .uevent = ata_acpi_dev_uevent,
257};
258
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400259static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800260 .handler = ata_acpi_ap_notify_dock,
261 .uevent = ata_acpi_ap_uevent,
262};
263
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700264/**
Tejun Heofafbae82007-05-15 03:28:16 +0900265 * ata_acpi_associate - associate ATA host with ACPI objects
266 * @host: target ATA host
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700267 *
Tejun Heofafbae82007-05-15 03:28:16 +0900268 * Look up ACPI objects associated with @host and initialize
269 * acpi_handle fields of @host, its ports and devices accordingly.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700270 *
Tejun Heofafbae82007-05-15 03:28:16 +0900271 * LOCKING:
272 * EH context.
273 *
274 * RETURNS:
275 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700276 */
Tejun Heofafbae82007-05-15 03:28:16 +0900277void ata_acpi_associate(struct ata_host *host)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700278{
Matthew Garrett237d8442007-10-03 01:24:16 +0100279 int i, j;
Alan Coxca426632007-03-08 23:13:50 +0000280
Tejun Heofafbae82007-05-15 03:28:16 +0900281 if (!is_pci_dev(host->dev) || libata_noacpi)
282 return;
Alan Coxca426632007-03-08 23:13:50 +0000283
Tejun Heofafbae82007-05-15 03:28:16 +0900284 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
285 if (!host->acpi_handle)
286 return;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700287
Tejun Heofafbae82007-05-15 03:28:16 +0900288 for (i = 0; i < host->n_ports; i++) {
289 struct ata_port *ap = host->ports[i];
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700290
Tejun Heofafbae82007-05-15 03:28:16 +0900291 if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
292 ata_acpi_associate_sata_port(ap);
293 else
294 ata_acpi_associate_ide_port(ap);
Matthew Garrett237d8442007-10-03 01:24:16 +0100295
Tejun Heo233f1122008-03-12 14:24:43 +0900296 if (ap->acpi_handle) {
Tejun Heo233f1122008-03-12 14:24:43 +0900297 /* we might be on a docking station */
298 register_hotplug_dock_device(ap->acpi_handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800299 &ata_acpi_ap_dock_ops, ap);
Tejun Heo233f1122008-03-12 14:24:43 +0900300 }
Matthew Garrett237d8442007-10-03 01:24:16 +0100301
302 for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
303 struct ata_device *dev = &ap->link.device[j];
304
Tejun Heo233f1122008-03-12 14:24:43 +0900305 if (dev->acpi_handle) {
Tejun Heo233f1122008-03-12 14:24:43 +0900306 /* we might be on a docking station */
307 register_hotplug_dock_device(dev->acpi_handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800308 &ata_acpi_dev_dock_ops, dev);
Tejun Heo233f1122008-03-12 14:24:43 +0900309 }
Matthew Garrett237d8442007-10-03 01:24:16 +0100310 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700311 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700312}
313
314/**
Tejun Heo562f0c22007-12-15 15:05:01 +0900315 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
316 * @host: target ATA host
317 *
318 * This function is called during driver detach after the whole host
319 * is shut down.
320 *
321 * LOCKING:
322 * EH context.
323 */
324void ata_acpi_dissociate(struct ata_host *host)
325{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900326 int i;
327
328 /* Restore initial _GTM values so that driver which attaches
329 * afterward can use them too.
330 */
331 for (i = 0; i < host->n_ports; i++) {
332 struct ata_port *ap = host->ports[i];
333 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
334
335 if (ap->acpi_handle && gtm)
336 ata_acpi_stm(ap, gtm);
337 }
Tejun Heo562f0c22007-12-15 15:05:01 +0900338}
339
340/**
Tejun Heo64578a32007-05-15 03:28:16 +0900341 * ata_acpi_gtm - execute _GTM
342 * @ap: target ATA port
343 * @gtm: out parameter for _GTM result
344 *
345 * Evaluate _GTM and store the result in @gtm.
346 *
347 * LOCKING:
348 * EH context.
349 *
350 * RETURNS:
351 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
352 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900353int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
Tejun Heo64578a32007-05-15 03:28:16 +0900354{
355 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
356 union acpi_object *out_obj;
357 acpi_status status;
358 int rc = 0;
359
360 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
361
362 rc = -ENOENT;
363 if (status == AE_NOT_FOUND)
364 goto out_free;
365
366 rc = -EINVAL;
367 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700368 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
369 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900370 goto out_free;
371 }
372
373 out_obj = output.pointer;
374 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700375 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
376 out_obj->type);
Tejun Heo64578a32007-05-15 03:28:16 +0900377
378 goto out_free;
379 }
380
381 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700382 ata_port_err(ap, "_GTM returned invalid length %d\n",
383 out_obj->buffer.length);
Tejun Heo64578a32007-05-15 03:28:16 +0900384 goto out_free;
385 }
386
387 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
388 rc = 0;
389 out_free:
390 kfree(output.pointer);
391 return rc;
392}
393
Alan Coxbadff032007-10-04 21:28:18 +0100394EXPORT_SYMBOL_GPL(ata_acpi_gtm);
395
Tejun Heo64578a32007-05-15 03:28:16 +0900396/**
397 * ata_acpi_stm - execute _STM
398 * @ap: target ATA port
399 * @stm: timing parameter to _STM
400 *
401 * Evaluate _STM with timing parameter @stm.
402 *
403 * LOCKING:
404 * EH context.
405 *
406 * RETURNS:
407 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
408 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900409int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a32007-05-15 03:28:16 +0900410{
411 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900412 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a32007-05-15 03:28:16 +0900413 struct acpi_object_list input;
414 union acpi_object in_params[3];
415
416 in_params[0].type = ACPI_TYPE_BUFFER;
417 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900418 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a32007-05-15 03:28:16 +0900419 /* Buffers for id may need byteswapping ? */
420 in_params[1].type = ACPI_TYPE_BUFFER;
421 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900422 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900423 in_params[2].type = ACPI_TYPE_BUFFER;
424 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900425 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900426
427 input.count = 3;
428 input.pointer = in_params;
429
430 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
431
432 if (status == AE_NOT_FOUND)
433 return -ENOENT;
434 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700435 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
436 status);
Tejun Heo64578a32007-05-15 03:28:16 +0900437 return -EINVAL;
438 }
439 return 0;
440}
441
Alan Coxbadff032007-10-04 21:28:18 +0100442EXPORT_SYMBOL_GPL(ata_acpi_stm);
443
Tejun Heo64578a32007-05-15 03:28:16 +0900444/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900445 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900446 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900447 * @gtf: output parameter for buffer containing _GTF taskfile arrays
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700448 *
449 * This applies to both PATA and SATA drives.
450 *
451 * The _GTF method has no input parameters.
452 * It returns a variable number of register set values (registers
453 * hex 1F1..1F7, taskfiles).
454 * The <variable number> is not known in advance, so have ACPI-CA
455 * allocate the buffer as needed and return it, then free it later.
456 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900457 * LOCKING:
458 * EH context.
459 *
460 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900461 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
462 * if _GTF is invalid.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700463 */
Tejun Heo398e0782007-12-15 15:05:03 +0900464static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700465{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900466 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900467 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900468 struct acpi_buffer output;
469 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900470 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700471
Tejun Heo398e0782007-12-15 15:05:03 +0900472 /* if _GTF is cached, use the cached value */
473 if (dev->gtf_cache) {
474 out_obj = dev->gtf_cache;
475 goto done;
476 }
477
Tejun Heo4700c4b2007-05-15 03:28:16 +0900478 /* set up output buffer */
479 output.length = ACPI_ALLOCATE_BUFFER;
480 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700481
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700482 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700483 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
484 __func__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700485
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700486 /* _GTF has no input parameters */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900487 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
Tejun Heo398e0782007-12-15 15:05:03 +0900488 out_obj = dev->gtf_cache = output.pointer;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900489
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700490 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900491 if (status != AE_NOT_FOUND) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700492 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
493 status);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900494 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900495 }
496 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700497 }
498
499 if (!output.length || !output.pointer) {
500 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700501 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
502 __func__,
503 (unsigned long long)output.length,
504 output.pointer);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900505 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900506 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700507 }
508
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700509 if (out_obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700510 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
511 out_obj->type);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900512 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900513 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700514 }
515
Tejun Heo4700c4b2007-05-15 03:28:16 +0900516 if (out_obj->buffer.length % REGS_PER_GTF) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700517 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
518 out_obj->buffer.length);
Tejun Heo66fa7f22007-12-15 15:05:04 +0900519 rc = -EINVAL;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900520 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700521 }
522
Tejun Heo398e0782007-12-15 15:05:03 +0900523 done:
Tejun Heo4700c4b2007-05-15 03:28:16 +0900524 rc = out_obj->buffer.length / REGS_PER_GTF;
Tejun Heo398e0782007-12-15 15:05:03 +0900525 if (gtf) {
526 *gtf = (void *)out_obj->buffer.pointer;
527 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700528 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
529 __func__, *gtf, rc);
Tejun Heo398e0782007-12-15 15:05:03 +0900530 }
Tejun Heo4700c4b2007-05-15 03:28:16 +0900531 return rc;
532
533 out_free:
Tejun Heo398e0782007-12-15 15:05:03 +0900534 ata_acpi_clear_gtf(dev);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900535 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700536}
537
Tejun Heo7c77fa42007-12-18 16:33:03 +0900538/**
539 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
540 * @dev: target device
541 * @gtm: GTM parameter to use
542 *
543 * Determine xfermask for @dev from @gtm.
544 *
545 * LOCKING:
546 * None.
547 *
548 * RETURNS:
549 * Determined xfermask.
550 */
551unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
552 const struct ata_acpi_gtm *gtm)
553{
Tejun Heoa0f79b92007-12-18 16:33:05 +0900554 unsigned long xfer_mask = 0;
555 unsigned int type;
556 int unit;
557 u8 mode;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900558
559 /* we always use the 0 slot for crap hardware */
560 unit = dev->devno;
561 if (!(gtm->flags & 0x10))
562 unit = 0;
563
Tejun Heoa0f79b92007-12-18 16:33:05 +0900564 /* PIO */
565 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
566 xfer_mask |= ata_xfer_mode2mask(mode);
Tejun Heo7c77fa42007-12-18 16:33:03 +0900567
568 /* See if we have MWDMA or UDMA data. We don't bother with
569 * MWDMA if UDMA is available as this means the BIOS set UDMA
570 * and our error changedown if it works is UDMA to PIO anyway.
571 */
Tejun Heoa0f79b92007-12-18 16:33:05 +0900572 if (!(gtm->flags & (1 << (2 * unit))))
573 type = ATA_SHIFT_MWDMA;
574 else
575 type = ATA_SHIFT_UDMA;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900576
Tejun Heoa0f79b92007-12-18 16:33:05 +0900577 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
578 xfer_mask |= ata_xfer_mode2mask(mode);
579
580 return xfer_mask;
Tejun Heo7c77fa42007-12-18 16:33:03 +0900581}
582EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
583
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700584/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400585 * ata_acpi_cbl_80wire - Check for 80 wire cable
586 * @ap: Port to check
Tejun Heo021ee9a2007-12-18 16:33:06 +0900587 * @gtm: GTM data to use
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400588 *
Tejun Heo021ee9a2007-12-18 16:33:06 +0900589 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400590 */
Tejun Heo021ee9a2007-12-18 16:33:06 +0900591int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400592{
Tejun Heo021ee9a2007-12-18 16:33:06 +0900593 struct ata_device *dev;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400594
Tejun Heo1eca4362008-11-03 20:03:17 +0900595 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo021ee9a2007-12-18 16:33:06 +0900596 unsigned long xfer_mask, udma_mask;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400597
Tejun Heo021ee9a2007-12-18 16:33:06 +0900598 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
599 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
600
601 if (udma_mask & ~ATA_UDMA_MASK_40C)
602 return 1;
603 }
604
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400605 return 0;
606}
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400607EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
608
Tejun Heo3264a8d2007-12-15 15:05:06 +0900609static void ata_acpi_gtf_to_tf(struct ata_device *dev,
610 const struct ata_acpi_gtf *gtf,
611 struct ata_taskfile *tf)
612{
613 ata_tf_init(dev, tf);
614
615 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
616 tf->protocol = ATA_PROT_NODATA;
617 tf->feature = gtf->tf[0]; /* 0x1f1 */
618 tf->nsect = gtf->tf[1]; /* 0x1f2 */
619 tf->lbal = gtf->tf[2]; /* 0x1f3 */
620 tf->lbam = gtf->tf[3]; /* 0x1f4 */
621 tf->lbah = gtf->tf[4]; /* 0x1f5 */
622 tf->device = gtf->tf[5]; /* 0x1f6 */
623 tf->command = gtf->tf[6]; /* 0x1f7 */
624}
625
Tejun Heo110f66d2009-09-16 04:17:28 +0900626static int ata_acpi_filter_tf(struct ata_device *dev,
627 const struct ata_taskfile *tf,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900628 const struct ata_taskfile *ptf)
629{
Tejun Heo110f66d2009-09-16 04:17:28 +0900630 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900631 /* libata doesn't use ACPI to configure transfer mode.
632 * It will only confuse device configuration. Skip.
633 */
634 if (tf->command == ATA_CMD_SET_FEATURES &&
635 tf->feature == SETFEATURES_XFER)
636 return 1;
637 }
638
Tejun Heo110f66d2009-09-16 04:17:28 +0900639 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900640 /* BIOS writers, sorry but we don't wanna lock
641 * features unless the user explicitly said so.
642 */
643
644 /* DEVICE CONFIGURATION FREEZE LOCK */
645 if (tf->command == ATA_CMD_CONF_OVERLAY &&
646 tf->feature == ATA_DCO_FREEZE_LOCK)
647 return 1;
648
649 /* SECURITY FREEZE LOCK */
650 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
651 return 1;
652
653 /* SET MAX LOCK and SET MAX FREEZE LOCK */
654 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
655 tf->command == ATA_CMD_SET_MAX &&
656 (tf->feature == ATA_SET_MAX_LOCK ||
657 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
658 return 1;
659 }
660
Tejun Heofa5b5612009-09-16 04:17:02 +0900661 if (tf->command == ATA_CMD_SET_FEATURES &&
662 tf->feature == SETFEATURES_SATA_ENABLE) {
Tejun Heob3449912008-07-06 23:15:03 +0900663 /* inhibit enabling DIPM */
Tejun Heo110f66d2009-09-16 04:17:28 +0900664 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
Tejun Heob3449912008-07-06 23:15:03 +0900665 tf->nsect == SATA_DIPM)
666 return 1;
Tejun Heofa5b5612009-09-16 04:17:02 +0900667
668 /* inhibit FPDMA non-zero offset */
Tejun Heo110f66d2009-09-16 04:17:28 +0900669 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900670 (tf->nsect == SATA_FPDMA_OFFSET ||
671 tf->nsect == SATA_FPDMA_IN_ORDER))
672 return 1;
673
674 /* inhibit FPDMA auto activation */
Tejun Heo110f66d2009-09-16 04:17:28 +0900675 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
Tejun Heofa5b5612009-09-16 04:17:02 +0900676 tf->nsect == SATA_FPDMA_AA)
677 return 1;
Tejun Heob3449912008-07-06 23:15:03 +0900678 }
679
Tejun Heo3264a8d2007-12-15 15:05:06 +0900680 return 0;
681}
682
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400683/**
Tejun Heo0e8634b2007-12-15 15:05:05 +0900684 * ata_acpi_run_tf - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900685 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700686 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
687 *
Sergei Shtylyov3696df32011-02-04 22:04:17 +0300688 * Outputs ATA taskfile to standard ATA host controller.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700689 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
690 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
691 * hob_lbal, hob_lbam, and hob_lbah.
692 *
693 * This function waits for idle (!BUSY and !DRQ) after writing
694 * registers. If the control register has a new value, this
695 * function also waits for idle after writing control and before
696 * writing the remaining registers.
697 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900698 * LOCKING:
699 * EH context.
700 *
701 * RETURNS:
Tejun Heo3264a8d2007-12-15 15:05:06 +0900702 * 1 if command is executed successfully. 0 if ignored, rejected or
703 * filtered out, -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700704 */
Tejun Heo0e8634b2007-12-15 15:05:05 +0900705static int ata_acpi_run_tf(struct ata_device *dev,
Tejun Heo3264a8d2007-12-15 15:05:06 +0900706 const struct ata_acpi_gtf *gtf,
707 const struct ata_acpi_gtf *prev_gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700708{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900709 struct ata_taskfile *pptf = NULL;
710 struct ata_taskfile tf, ptf, rtf;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900711 unsigned int err_mask;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900712 const char *level;
Robert Hancock65211482009-07-14 20:43:39 -0600713 const char *descr;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900714 char msg[60];
715 int rc;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500716
Tejun Heo4700c4b2007-05-15 03:28:16 +0900717 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
718 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
719 && (gtf->tf[6] == 0))
720 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700721
Tejun Heo3264a8d2007-12-15 15:05:06 +0900722 ata_acpi_gtf_to_tf(dev, gtf, &tf);
723 if (prev_gtf) {
724 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
725 pptf = &ptf;
726 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700727
Tejun Heo110f66d2009-09-16 04:17:28 +0900728 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
Tejun Heo3264a8d2007-12-15 15:05:06 +0900729 rtf = tf;
730 err_mask = ata_exec_internal(dev, &rtf, NULL,
731 DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700732
Tejun Heo3264a8d2007-12-15 15:05:06 +0900733 switch (err_mask) {
734 case 0:
735 level = KERN_DEBUG;
736 snprintf(msg, sizeof(msg), "succeeded");
737 rc = 1;
738 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900739
Tejun Heo3264a8d2007-12-15 15:05:06 +0900740 case AC_ERR_DEV:
741 level = KERN_INFO;
742 snprintf(msg, sizeof(msg),
743 "rejected by device (Stat=0x%02x Err=0x%02x)",
744 rtf.command, rtf.feature);
745 rc = 0;
746 break;
Tejun Heo0e8634b2007-12-15 15:05:05 +0900747
Tejun Heo3264a8d2007-12-15 15:05:06 +0900748 default:
749 level = KERN_ERR;
750 snprintf(msg, sizeof(msg),
751 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
752 err_mask, rtf.command, rtf.feature);
753 rc = -EIO;
754 break;
755 }
756 } else {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900757 level = KERN_INFO;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900758 snprintf(msg, sizeof(msg), "filtered out");
Tejun Heo0e8634b2007-12-15 15:05:05 +0900759 rc = 0;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900760 }
Robert Hancock65211482009-07-14 20:43:39 -0600761 descr = ata_get_cmd_descript(tf.command);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900762
Tejun Heo0e8634b2007-12-15 15:05:05 +0900763 ata_dev_printk(dev, level,
Robert Hancock65211482009-07-14 20:43:39 -0600764 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
Tejun Heo0e8634b2007-12-15 15:05:05 +0900765 tf.command, tf.feature, tf.nsect, tf.lbal,
Robert Hancock65211482009-07-14 20:43:39 -0600766 tf.lbam, tf.lbah, tf.device,
767 (descr ? descr : "unknown"), msg);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900768
769 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700770}
771
772/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700773 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900774 * @dev: target ATA device
Martin Olsson98a17082009-04-22 18:21:29 +0200775 * @nr_executed: out parameter for the number of executed commands
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700776 *
Martin Olsson98a17082009-04-22 18:21:29 +0200777 * Evaluate _GTF and execute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900778 *
779 * LOCKING:
780 * EH context.
781 *
782 * RETURNS:
Tejun Heo66fa7f22007-12-15 15:05:04 +0900783 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
784 * -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700785 */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900786static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700787{
Tejun Heo3264a8d2007-12-15 15:05:06 +0900788 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
Tejun Heo67465442007-05-15 03:28:16 +0900789 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700790
Tejun Heo67465442007-05-15 03:28:16 +0900791 /* get taskfiles */
Tejun Heo66fa7f22007-12-15 15:05:04 +0900792 rc = ata_dev_get_GTF(dev, &gtf);
793 if (rc < 0)
794 return rc;
795 gtf_count = rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700796
Tejun Heo67465442007-05-15 03:28:16 +0900797 /* execute them */
Tejun Heo3264a8d2007-12-15 15:05:06 +0900798 for (i = 0; i < gtf_count; i++, gtf++) {
799 rc = ata_acpi_run_tf(dev, gtf, pgtf);
Tejun Heo0e8634b2007-12-15 15:05:05 +0900800 if (rc < 0)
801 break;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900802 if (rc) {
Tejun Heo0e8634b2007-12-15 15:05:05 +0900803 (*nr_executed)++;
Tejun Heo3264a8d2007-12-15 15:05:06 +0900804 pgtf = gtf;
805 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700806 }
807
Tejun Heo398e0782007-12-15 15:05:03 +0900808 ata_acpi_clear_gtf(dev);
Tejun Heo67465442007-05-15 03:28:16 +0900809
Tejun Heo0e8634b2007-12-15 15:05:05 +0900810 if (rc < 0)
811 return rc;
812 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700813}
814
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700815/**
816 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900817 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700818 *
819 * _SDD ACPI object: for SATA mode only
820 * Must be after Identify (Packet) Device -- uses its data
821 * ATM this function never returns a failure. It is an optional
822 * method and if it fails for whatever reason, we should still
823 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900824 *
825 * LOCKING:
826 * EH context.
827 *
828 * RETURNS:
Tejun Heof2406772009-11-18 22:24:21 +0900829 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700830 */
Tejun Heo67465442007-05-15 03:28:16 +0900831static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700832{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900833 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900834 acpi_status status;
835 struct acpi_object_list input;
836 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700837
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700838 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700839 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
840 __func__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700841
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700842 /* Give the drive Identify data to the drive via the _SDD method */
843 /* _SDD: set up input parameters */
844 input.count = 1;
845 input.pointer = in_params;
846 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900847 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
848 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700849 /* Output buffer: _SDD has no output */
850
851 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900852 swap_buf_le16(dev->id, ATA_ID_WORDS);
Tejun Heofafbae82007-05-15 03:28:16 +0900853 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900854 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700855
Tejun Heof2406772009-11-18 22:24:21 +0900856 if (status == AE_NOT_FOUND)
857 return -ENOENT;
858
859 if (ACPI_FAILURE(status)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700860 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
Tejun Heof2406772009-11-18 22:24:21 +0900861 return -EIO;
862 }
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700863
Tejun Heof2406772009-11-18 22:24:21 +0900864 return 0;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700865}
866
Tejun Heo67465442007-05-15 03:28:16 +0900867/**
Tejun Heo64578a32007-05-15 03:28:16 +0900868 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
869 * @ap: target ATA port
870 *
871 * This function is called when @ap is about to be suspended. All
872 * devices are already put to sleep but the port_suspend() callback
873 * hasn't been executed yet. Error return from this function aborts
874 * suspend.
875 *
876 * LOCKING:
877 * EH context.
878 *
879 * RETURNS:
880 * 0 on success, -errno on failure.
881 */
882int ata_acpi_on_suspend(struct ata_port *ap)
883{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900884 /* nada */
885 return 0;
Tejun Heo64578a32007-05-15 03:28:16 +0900886}
887
888/**
Tejun Heo67465442007-05-15 03:28:16 +0900889 * ata_acpi_on_resume - ATA ACPI hook called on resume
890 * @ap: target ATA port
891 *
892 * This function is called when @ap is resumed - right after port
893 * itself is resumed but before any EH action is taken.
894 *
895 * LOCKING:
896 * EH context.
897 */
898void ata_acpi_on_resume(struct ata_port *ap)
899{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900900 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Tejun Heof58229f2007-08-06 18:36:23 +0900901 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700902
Tejun Heo398e0782007-12-15 15:05:03 +0900903 if (ap->acpi_handle && gtm) {
904 /* _GTM valid */
905
906 /* restore timing parameters */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900907 ata_acpi_stm(ap, gtm);
Tejun Heo64578a32007-05-15 03:28:16 +0900908
Tejun Heo398e0782007-12-15 15:05:03 +0900909 /* _GTF should immediately follow _STM so that it can
910 * use values set by _STM. Cache _GTF result and
911 * schedule _GTF.
912 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900913 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900914 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800915 if (ata_dev_enabled(dev) &&
916 ata_dev_get_GTF(dev, NULL) >= 0)
Tejun Heo398e0782007-12-15 15:05:03 +0900917 dev->flags |= ATA_DFLAG_ACPI_PENDING;
918 }
919 } else {
920 /* SATA _GTF needs to be evaulated after _SDD and
921 * there's no reason to evaluate IDE _GTF early
922 * without _STM. Clear cache and schedule _GTF.
923 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900924 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo398e0782007-12-15 15:05:03 +0900925 ata_acpi_clear_gtf(dev);
Shaohua Li48feb3c2008-03-25 16:50:45 +0800926 if (ata_dev_enabled(dev))
927 dev->flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo398e0782007-12-15 15:05:03 +0900928 }
929 }
Tejun Heo67465442007-05-15 03:28:16 +0900930}
931
932/**
Shaohua Libd3adca2007-11-02 09:32:38 +0800933 * ata_acpi_set_state - set the port power state
934 * @ap: target ATA port
935 * @state: state, on/off
936 *
937 * This function executes the _PS0/_PS3 ACPI method to set the power state.
938 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
939 */
940void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
941{
942 struct ata_device *dev;
943
944 if (!ap->acpi_handle || (ap->flags & ATA_FLAG_ACPI_SATA))
945 return;
946
947 /* channel first and then drives for power on and vica versa
948 for power off */
949 if (state.event == PM_EVENT_ON)
950 acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D0);
951
Tejun Heo1eca4362008-11-03 20:03:17 +0900952 ata_for_each_dev(dev, &ap->link, ENABLED) {
953 if (dev->acpi_handle)
Shaohua Libd3adca2007-11-02 09:32:38 +0800954 acpi_bus_set_power(dev->acpi_handle,
955 state.event == PM_EVENT_ON ?
956 ACPI_STATE_D0 : ACPI_STATE_D3);
957 }
958 if (state.event != PM_EVENT_ON)
959 acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D3);
960}
961
962/**
Tejun Heo67465442007-05-15 03:28:16 +0900963 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
964 * @dev: target ATA device
965 *
966 * This function is called when @dev is about to be configured.
967 * IDENTIFY data might have been modified after this hook is run.
968 *
969 * LOCKING:
970 * EH context.
971 *
972 * RETURNS:
973 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
974 * -errno on failure.
975 */
976int ata_acpi_on_devcfg(struct ata_device *dev)
977{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900978 struct ata_port *ap = dev->link->ap;
979 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900980 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
Tejun Heo66fa7f22007-12-15 15:05:04 +0900981 int nr_executed = 0;
Tejun Heo67465442007-05-15 03:28:16 +0900982 int rc;
983
Tejun Heo67465442007-05-15 03:28:16 +0900984 if (!dev->acpi_handle)
985 return 0;
986
987 /* do we need to do _GTF? */
988 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
989 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
990 return 0;
991
992 /* do _SDD if SATA */
993 if (acpi_sata) {
994 rc = ata_acpi_push_id(dev);
Tejun Heof2406772009-11-18 22:24:21 +0900995 if (rc && rc != -ENOENT)
Tejun Heo67465442007-05-15 03:28:16 +0900996 goto acpi_err;
997 }
998
999 /* do _GTF */
Tejun Heo66fa7f22007-12-15 15:05:04 +09001000 rc = ata_acpi_exec_tfs(dev, &nr_executed);
1001 if (rc)
Tejun Heo67465442007-05-15 03:28:16 +09001002 goto acpi_err;
1003
1004 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
1005
1006 /* refresh IDENTIFY page if any _GTF command has been executed */
Tejun Heo66fa7f22007-12-15 15:05:04 +09001007 if (nr_executed) {
Tejun Heo67465442007-05-15 03:28:16 +09001008 rc = ata_dev_reread_id(dev, 0);
1009 if (rc < 0) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001010 ata_dev_err(dev,
1011 "failed to IDENTIFY after ACPI commands\n");
Tejun Heo67465442007-05-15 03:28:16 +09001012 return rc;
1013 }
1014 }
1015
1016 return 0;
1017
1018 acpi_err:
Tejun Heo66fa7f22007-12-15 15:05:04 +09001019 /* ignore evaluation failure if we can continue safely */
1020 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1021 return 0;
Tejun Heo67465442007-05-15 03:28:16 +09001022
Tejun Heo66fa7f22007-12-15 15:05:04 +09001023 /* fail and let EH retry once more for unknown IO errors */
1024 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1025 dev->flags |= ATA_DFLAG_ACPI_FAILED;
1026 return rc;
Tejun Heo67465442007-05-15 03:28:16 +09001027 }
Tejun Heo66fa7f22007-12-15 15:05:04 +09001028
Joe Perchesa9a79df2011-04-15 15:51:59 -07001029 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
Tejun Heo66fa7f22007-12-15 15:05:04 +09001030 dev->acpi_handle = NULL;
1031
1032 /* We can safely continue if no _GTF command has been executed
1033 * and port is not frozen.
1034 */
1035 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1036 return 0;
1037
Tejun Heo67465442007-05-15 03:28:16 +09001038 return rc;
1039}
Tejun Heo562f0c22007-12-15 15:05:01 +09001040
1041/**
1042 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1043 * @dev: target ATA device
1044 *
1045 * This function is called when @dev is about to be disabled.
1046 *
1047 * LOCKING:
1048 * EH context.
1049 */
1050void ata_acpi_on_disable(struct ata_device *dev)
1051{
Tejun Heo398e0782007-12-15 15:05:03 +09001052 ata_acpi_clear_gtf(dev);
Tejun Heo562f0c22007-12-15 15:05:01 +09001053}
Matthew Garrett6b66d952012-06-25 16:13:03 +08001054
1055static int compat_pci_ata(struct ata_port *ap)
1056{
1057 struct device *dev = ap->tdev.parent;
1058 struct pci_dev *pdev;
1059
1060 if (!is_pci_dev(dev))
1061 return 0;
1062
1063 pdev = to_pci_dev(dev);
1064
1065 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1066 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1067 return 0;
1068
1069 return 1;
1070}
1071
1072static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1073{
1074 if (ap->flags & ATA_FLAG_ACPI_SATA)
1075 return -ENODEV;
1076
1077 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1078 ap->port_no);
1079
1080 if (!*handle)
1081 return -ENODEV;
1082
1083 return 0;
1084}
1085
1086static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1087 acpi_handle *handle)
1088{
1089 struct ata_device *ata_dev;
1090
1091 if (ap->flags & ATA_FLAG_ACPI_SATA)
1092 ata_dev = &ap->link.device[sdev->channel];
1093 else
1094 ata_dev = &ap->link.device[sdev->id];
1095
1096 *handle = dev_acpi_handle(ata_dev);
1097
1098 if (!*handle)
1099 return -ENODEV;
1100
1101 return 0;
1102}
1103
1104static int is_ata_port(const struct device *dev)
1105{
1106 return dev->type == &ata_port_type;
1107}
1108
1109static struct ata_port *dev_to_ata_port(struct device *dev)
1110{
1111 while (!is_ata_port(dev)) {
1112 if (!dev->parent)
1113 return NULL;
1114 dev = dev->parent;
1115 }
1116 return to_ata_port(dev);
1117}
1118
1119static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1120{
1121 struct ata_port *ap = dev_to_ata_port(dev);
1122
1123 if (!ap)
1124 return -ENODEV;
1125
1126 if (!compat_pci_ata(ap))
1127 return -ENODEV;
1128
1129 if (scsi_is_host_device(dev))
1130 return ata_acpi_bind_host(ap, handle);
1131 else if (scsi_is_sdev_device(dev)) {
1132 struct scsi_device *sdev = to_scsi_device(dev);
1133
1134 return ata_acpi_bind_device(ap, sdev, handle);
1135 } else
1136 return -ENODEV;
1137}
1138
1139static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1140{
1141 return -ENODEV;
1142}
1143
1144static struct acpi_bus_type ata_acpi_bus = {
1145 .find_bridge = ata_acpi_find_dummy,
1146 .find_device = ata_acpi_find_device,
1147};
1148
1149int ata_acpi_register(void)
1150{
1151 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1152}
1153
1154void ata_acpi_unregister(void)
1155{
1156 scsi_unregister_acpi_bus_type(&ata_acpi_bus);
1157}