drm/i915: do not update cursor in crtc mode set
[deliverable/linux.git] / drivers / ata / libata-acpi.c
CommitLineData
11ef697b
KCA
1/*
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
3264a8d8 9#include <linux/module.h>
11ef697b
KCA
10#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>
5a0e3ad6 18#include <linux/slab.h>
3bd46600 19#include <linux/pm_runtime.h>
237d8440 20#include <scsi/scsi_device.h>
11ef697b
KCA
21#include "libata.h"
22
23#include <acpi/acpi_bus.h>
11ef697b 24
110f66d2 25unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
3264a8d8 26module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
fa5b561c 27MODULE_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)");
3264a8d8 28
11ef697b 29#define NO_PORT_MULT 0xffff
2dcb407e 30#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
11ef697b
KCA
31
32#define REGS_PER_GTF 7
4700c4bc
TH
33struct ata_acpi_gtf {
34 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
35} __packed;
11ef697b 36
ca426635
AC
37/*
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}
11ef697b 44
398e0782
TH
45static void ata_acpi_clear_gtf(struct ata_device *dev)
46{
47 kfree(dev->gtf_cache);
48 dev->gtf_cache = NULL;
49}
50
30dcf76a
MG
51/**
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)
6b66d958
MG
59{
60 if (ap->flags & ATA_FLAG_ACPI_SATA)
61 return NULL;
62
d66af4df
AL
63 return ap->scsi_host ?
64 DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev) : NULL;
6b66d958 65}
30dcf76a 66EXPORT_SYMBOL(ata_ap_acpi_handle);
6b66d958 67
30dcf76a
MG
68/**
69 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
70 * @dev: the acpi_device returned will correspond to this port
71 *
72 * Returns the acpi_handle for the ACPI namespace object corresponding to
73 * the ata_device passed into the function, or NULL if no such object exists
74 */
75acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
6b66d958
MG
76{
77 acpi_integer adr;
78 struct ata_port *ap = dev->link->ap;
79
19ccee76 80 if (libata_noacpi || dev->flags & ATA_DFLAG_ACPI_DISABLED)
0d0cdb02
AL
81 return NULL;
82
6b66d958
MG
83 if (ap->flags & ATA_FLAG_ACPI_SATA) {
84 if (!sata_pmp_attached(ap))
85 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
86 else
87 adr = SATA_ADR(ap->port_no, dev->link->pmp);
88 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
89 } else
30dcf76a 90 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
11ef697b 91}
30dcf76a 92EXPORT_SYMBOL(ata_dev_acpi_handle);
11ef697b 93
664d080c
HM
94/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
95static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
96{
97 if (dev)
98 dev->flags |= ATA_DFLAG_DETACH;
99 else {
100 struct ata_link *tlink;
101 struct ata_device *tdev;
102
1eca4365
TH
103 ata_for_each_link(tlink, ap, EDGE)
104 ata_for_each_dev(tdev, tlink, ALL)
664d080c
HM
105 tdev->flags |= ATA_DFLAG_DETACH;
106 }
107
108 ata_port_schedule_eh(ap);
109}
110
111/**
112 * ata_acpi_handle_hotplug - ACPI event handler backend
113 * @ap: ATA port ACPI event occurred
114 * @dev: ATA device ACPI event occurred (can be NULL)
115 * @event: ACPI event which occurred
664d080c
HM
116 *
117 * All ACPI bay / device realted events end up in this function. If
118 * the event is port-wide @dev is NULL. If the event is specific to a
119 * device, @dev points to it.
120 *
121 * Hotplug (as opposed to unplug) notification is always handled as
122 * port-wide while unplug only kills the target device on device-wide
123 * event.
124 *
125 * LOCKING:
126 * ACPI notify handler context. May sleep.
127 */
128static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
f730ae18 129 u32 event)
237d8440 130{
664d080c 131 struct ata_eh_info *ehi = &ap->link.eh_info;
233f1120
TH
132 int wait = 0;
133 unsigned long flags;
3c1e3896 134
664d080c 135 spin_lock_irqsave(ap->lock, flags);
f730ae18
SL
136 /*
137 * When dock driver calls into the routine, it will always use
138 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
139 * ACPI_NOTIFY_EJECT_REQUEST for remove
140 */
233f1120
TH
141 switch (event) {
142 case ACPI_NOTIFY_BUS_CHECK:
143 case ACPI_NOTIFY_DEVICE_CHECK:
144 ata_ehi_push_desc(ehi, "ACPI event");
664d080c 145
f730ae18
SL
146 ata_ehi_hotplugged(ehi);
147 ata_port_freeze(ap);
664d080c
HM
148 break;
149 case ACPI_NOTIFY_EJECT_REQUEST:
150 ata_ehi_push_desc(ehi, "ACPI event");
151
664d080c
HM
152 ata_acpi_detach_device(ap, dev);
153 wait = 1;
154 break;
237d8440
MG
155 }
156
ae6c23c4
MG
157 spin_unlock_irqrestore(ap->lock, flags);
158
44521527 159 if (wait) {
ae6c23c4 160 ata_port_wait_eh(ap);
44521527
AL
161 flush_work(&ap->hotplug_task.work);
162 }
664d080c
HM
163}
164
165static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
166{
167 struct ata_device *dev = data;
168
f730ae18 169 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
664d080c
HM
170}
171
172static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
173{
174 struct ata_port *ap = data;
175
f730ae18 176 ata_acpi_handle_hotplug(ap, NULL, event);
237d8440
MG
177}
178
1253f7aa
SL
179static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
180 u32 event)
181{
182 struct kobject *kobj = NULL;
183 char event_string[20];
184 char *envp[] = { event_string, NULL };
185
186 if (dev) {
187 if (dev->sdev)
188 kobj = &dev->sdev->sdev_gendev.kobj;
189 } else
190 kobj = &ap->dev->kobj;
191
192 if (kobj) {
193 snprintf(event_string, 20, "BAY_EVENT=%d", event);
194 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
195 }
196}
197
198static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
199{
200 ata_acpi_uevent(data, NULL, event);
201}
202
203static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
204{
205 struct ata_device *dev = data;
206 ata_acpi_uevent(dev->link->ap, dev, event);
207}
208
9c8b04be 209static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
1253f7aa
SL
210 .handler = ata_acpi_dev_notify_dock,
211 .uevent = ata_acpi_dev_uevent,
212};
213
9c8b04be 214static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
1253f7aa
SL
215 .handler = ata_acpi_ap_notify_dock,
216 .uevent = ata_acpi_ap_uevent,
217};
218
44521527
AL
219void ata_acpi_hotplug_init(struct ata_host *host)
220{
221 int i;
222
223 for (i = 0; i < host->n_ports; i++) {
224 struct ata_port *ap = host->ports[i];
225 acpi_handle handle;
226 struct ata_device *dev;
227
228 if (!ap)
229 continue;
230
231 handle = ata_ap_acpi_handle(ap);
232 if (handle) {
233 /* we might be on a docking station */
234 register_hotplug_dock_device(handle,
235 &ata_acpi_ap_dock_ops, ap,
236 NULL, NULL);
237 }
238
239 ata_for_each_dev(dev, &ap->link, ALL) {
240 handle = ata_dev_acpi_handle(dev);
241 if (!handle)
242 continue;
243
244 /* we might be on a docking station */
245 register_hotplug_dock_device(handle,
246 &ata_acpi_dev_dock_ops,
247 dev, NULL, NULL);
248 }
249 }
250}
251
562f0c2d
TH
252/**
253 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
254 * @host: target ATA host
255 *
256 * This function is called during driver detach after the whole host
257 * is shut down.
258 *
259 * LOCKING:
260 * EH context.
261 */
262void ata_acpi_dissociate(struct ata_host *host)
263{
c05e6ff0
TH
264 int i;
265
266 /* Restore initial _GTM values so that driver which attaches
267 * afterward can use them too.
268 */
269 for (i = 0; i < host->n_ports; i++) {
270 struct ata_port *ap = host->ports[i];
271 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
272
30dcf76a 273 if (ata_ap_acpi_handle(ap) && gtm)
c05e6ff0
TH
274 ata_acpi_stm(ap, gtm);
275 }
562f0c2d
TH
276}
277
d66af4df
AL
278static int __ata_acpi_gtm(struct ata_port *ap, acpi_handle handle,
279 struct ata_acpi_gtm *gtm)
64578a3d
TH
280{
281 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
282 union acpi_object *out_obj;
283 acpi_status status;
284 int rc = 0;
285
d66af4df 286 status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
64578a3d
TH
287
288 rc = -ENOENT;
289 if (status == AE_NOT_FOUND)
290 goto out_free;
291
292 rc = -EINVAL;
293 if (ACPI_FAILURE(status)) {
a9a79dfe
JP
294 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
295 status);
64578a3d
TH
296 goto out_free;
297 }
298
299 out_obj = output.pointer;
300 if (out_obj->type != ACPI_TYPE_BUFFER) {
a9a79dfe
JP
301 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
302 out_obj->type);
64578a3d
TH
303
304 goto out_free;
305 }
306
307 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
a9a79dfe
JP
308 ata_port_err(ap, "_GTM returned invalid length %d\n",
309 out_obj->buffer.length);
64578a3d
TH
310 goto out_free;
311 }
312
313 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
314 rc = 0;
315 out_free:
316 kfree(output.pointer);
317 return rc;
318}
319
d66af4df
AL
320/**
321 * ata_acpi_gtm - execute _GTM
322 * @ap: target ATA port
323 * @gtm: out parameter for _GTM result
324 *
325 * Evaluate _GTM and store the result in @gtm.
326 *
327 * LOCKING:
328 * EH context.
329 *
330 * RETURNS:
331 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
332 */
333int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
334{
335 if (ata_ap_acpi_handle(ap))
336 return __ata_acpi_gtm(ap, ata_ap_acpi_handle(ap), gtm);
337 else
338 return -EINVAL;
339}
340
badff03d
AC
341EXPORT_SYMBOL_GPL(ata_acpi_gtm);
342
64578a3d
TH
343/**
344 * ata_acpi_stm - execute _STM
345 * @ap: target ATA port
346 * @stm: timing parameter to _STM
347 *
348 * Evaluate _STM with timing parameter @stm.
349 *
350 * LOCKING:
351 * EH context.
352 *
353 * RETURNS:
354 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
355 */
0d02f0b2 356int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
64578a3d
TH
357{
358 acpi_status status;
0d02f0b2 359 struct ata_acpi_gtm stm_buf = *stm;
64578a3d
TH
360 struct acpi_object_list input;
361 union acpi_object in_params[3];
362
363 in_params[0].type = ACPI_TYPE_BUFFER;
364 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
0d02f0b2 365 in_params[0].buffer.pointer = (u8 *)&stm_buf;
64578a3d
TH
366 /* Buffers for id may need byteswapping ? */
367 in_params[1].type = ACPI_TYPE_BUFFER;
368 in_params[1].buffer.length = 512;
9af5c9c9 369 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
64578a3d
TH
370 in_params[2].type = ACPI_TYPE_BUFFER;
371 in_params[2].buffer.length = 512;
9af5c9c9 372 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
64578a3d
TH
373
374 input.count = 3;
375 input.pointer = in_params;
376
30dcf76a
MG
377 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
378 NULL);
64578a3d
TH
379
380 if (status == AE_NOT_FOUND)
381 return -ENOENT;
382 if (ACPI_FAILURE(status)) {
a9a79dfe
JP
383 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
384 status);
64578a3d
TH
385 return -EINVAL;
386 }
387 return 0;
388}
389
badff03d
AC
390EXPORT_SYMBOL_GPL(ata_acpi_stm);
391
11ef697b 392/**
4700c4bc 393 * ata_dev_get_GTF - get the drive bootup default taskfile settings
3a32a8e9 394 * @dev: target ATA device
4700c4bc 395 * @gtf: output parameter for buffer containing _GTF taskfile arrays
11ef697b
KCA
396 *
397 * This applies to both PATA and SATA drives.
398 *
399 * The _GTF method has no input parameters.
400 * It returns a variable number of register set values (registers
401 * hex 1F1..1F7, taskfiles).
402 * The <variable number> is not known in advance, so have ACPI-CA
403 * allocate the buffer as needed and return it, then free it later.
404 *
4700c4bc
TH
405 * LOCKING:
406 * EH context.
407 *
408 * RETURNS:
66fa7f21
TH
409 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
410 * if _GTF is invalid.
11ef697b 411 */
398e0782 412static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
11ef697b 413{
9af5c9c9 414 struct ata_port *ap = dev->link->ap;
3a32a8e9 415 acpi_status status;
3a32a8e9
TH
416 struct acpi_buffer output;
417 union acpi_object *out_obj;
4700c4bc 418 int rc = 0;
11ef697b 419
398e0782
TH
420 /* if _GTF is cached, use the cached value */
421 if (dev->gtf_cache) {
422 out_obj = dev->gtf_cache;
423 goto done;
424 }
425
4700c4bc
TH
426 /* set up output buffer */
427 output.length = ACPI_ALLOCATE_BUFFER;
428 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
11ef697b 429
11ef697b 430 if (ata_msg_probe(ap))
a9a79dfe
JP
431 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
432 __func__, ap->port_no);
11ef697b 433
11ef697b 434 /* _GTF has no input parameters */
30dcf76a
MG
435 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
436 &output);
398e0782 437 out_obj = dev->gtf_cache = output.pointer;
4700c4bc 438
11ef697b 439 if (ACPI_FAILURE(status)) {
4700c4bc 440 if (status != AE_NOT_FOUND) {
a9a79dfe
JP
441 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
442 status);
66fa7f21 443 rc = -EINVAL;
4700c4bc
TH
444 }
445 goto out_free;
11ef697b
KCA
446 }
447
448 if (!output.length || !output.pointer) {
449 if (ata_msg_probe(ap))
a9a79dfe
JP
450 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
451 __func__,
452 (unsigned long long)output.length,
453 output.pointer);
66fa7f21 454 rc = -EINVAL;
4700c4bc 455 goto out_free;
11ef697b
KCA
456 }
457
11ef697b 458 if (out_obj->type != ACPI_TYPE_BUFFER) {
a9a79dfe
JP
459 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
460 out_obj->type);
66fa7f21 461 rc = -EINVAL;
4700c4bc 462 goto out_free;
11ef697b
KCA
463 }
464
4700c4bc 465 if (out_obj->buffer.length % REGS_PER_GTF) {
a9a79dfe
JP
466 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
467 out_obj->buffer.length);
66fa7f21 468 rc = -EINVAL;
4700c4bc 469 goto out_free;
11ef697b
KCA
470 }
471
398e0782 472 done:
4700c4bc 473 rc = out_obj->buffer.length / REGS_PER_GTF;
398e0782
TH
474 if (gtf) {
475 *gtf = (void *)out_obj->buffer.pointer;
476 if (ata_msg_probe(ap))
a9a79dfe
JP
477 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
478 __func__, *gtf, rc);
398e0782 479 }
4700c4bc
TH
480 return rc;
481
482 out_free:
398e0782 483 ata_acpi_clear_gtf(dev);
4700c4bc 484 return rc;
11ef697b
KCA
485}
486
7c77fa4d
TH
487/**
488 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
489 * @dev: target device
490 * @gtm: GTM parameter to use
491 *
492 * Determine xfermask for @dev from @gtm.
493 *
494 * LOCKING:
495 * None.
496 *
497 * RETURNS:
498 * Determined xfermask.
499 */
500unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
501 const struct ata_acpi_gtm *gtm)
502{
a0f79b92
TH
503 unsigned long xfer_mask = 0;
504 unsigned int type;
505 int unit;
506 u8 mode;
7c77fa4d
TH
507
508 /* we always use the 0 slot for crap hardware */
509 unit = dev->devno;
510 if (!(gtm->flags & 0x10))
511 unit = 0;
512
a0f79b92
TH
513 /* PIO */
514 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
515 xfer_mask |= ata_xfer_mode2mask(mode);
7c77fa4d
TH
516
517 /* See if we have MWDMA or UDMA data. We don't bother with
518 * MWDMA if UDMA is available as this means the BIOS set UDMA
519 * and our error changedown if it works is UDMA to PIO anyway.
520 */
a0f79b92
TH
521 if (!(gtm->flags & (1 << (2 * unit))))
522 type = ATA_SHIFT_MWDMA;
523 else
524 type = ATA_SHIFT_UDMA;
525
526 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
527 xfer_mask |= ata_xfer_mode2mask(mode);
7c77fa4d 528
a0f79b92 529 return xfer_mask;
7c77fa4d
TH
530}
531EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
532
e1ddb4b6
AC
533/**
534 * ata_acpi_cbl_80wire - Check for 80 wire cable
535 * @ap: Port to check
021ee9a6 536 * @gtm: GTM data to use
e1ddb4b6 537 *
021ee9a6 538 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
e1ddb4b6 539 */
021ee9a6 540int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
e1ddb4b6 541{
021ee9a6
TH
542 struct ata_device *dev;
543
1eca4365 544 ata_for_each_dev(dev, &ap->link, ENABLED) {
021ee9a6
TH
545 unsigned long xfer_mask, udma_mask;
546
021ee9a6
TH
547 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
548 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
549
550 if (udma_mask & ~ATA_UDMA_MASK_40C)
551 return 1;
552 }
553
e1ddb4b6
AC
554 return 0;
555}
e1ddb4b6
AC
556EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
557
3264a8d8
TH
558static void ata_acpi_gtf_to_tf(struct ata_device *dev,
559 const struct ata_acpi_gtf *gtf,
560 struct ata_taskfile *tf)
561{
562 ata_tf_init(dev, tf);
563
564 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
565 tf->protocol = ATA_PROT_NODATA;
566 tf->feature = gtf->tf[0]; /* 0x1f1 */
567 tf->nsect = gtf->tf[1]; /* 0x1f2 */
568 tf->lbal = gtf->tf[2]; /* 0x1f3 */
569 tf->lbam = gtf->tf[3]; /* 0x1f4 */
570 tf->lbah = gtf->tf[4]; /* 0x1f5 */
571 tf->device = gtf->tf[5]; /* 0x1f6 */
572 tf->command = gtf->tf[6]; /* 0x1f7 */
573}
574
110f66d2
TH
575static int ata_acpi_filter_tf(struct ata_device *dev,
576 const struct ata_taskfile *tf,
3264a8d8
TH
577 const struct ata_taskfile *ptf)
578{
110f66d2 579 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
3264a8d8
TH
580 /* libata doesn't use ACPI to configure transfer mode.
581 * It will only confuse device configuration. Skip.
582 */
583 if (tf->command == ATA_CMD_SET_FEATURES &&
584 tf->feature == SETFEATURES_XFER)
585 return 1;
586 }
587
110f66d2 588 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
3264a8d8
TH
589 /* BIOS writers, sorry but we don't wanna lock
590 * features unless the user explicitly said so.
591 */
592
593 /* DEVICE CONFIGURATION FREEZE LOCK */
594 if (tf->command == ATA_CMD_CONF_OVERLAY &&
595 tf->feature == ATA_DCO_FREEZE_LOCK)
596 return 1;
597
598 /* SECURITY FREEZE LOCK */
599 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
600 return 1;
601
602 /* SET MAX LOCK and SET MAX FREEZE LOCK */
603 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
604 tf->command == ATA_CMD_SET_MAX &&
605 (tf->feature == ATA_SET_MAX_LOCK ||
606 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
607 return 1;
608 }
609
fa5b561c
TH
610 if (tf->command == ATA_CMD_SET_FEATURES &&
611 tf->feature == SETFEATURES_SATA_ENABLE) {
b344991a 612 /* inhibit enabling DIPM */
110f66d2 613 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
b344991a
TH
614 tf->nsect == SATA_DIPM)
615 return 1;
fa5b561c
TH
616
617 /* inhibit FPDMA non-zero offset */
110f66d2 618 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
fa5b561c
TH
619 (tf->nsect == SATA_FPDMA_OFFSET ||
620 tf->nsect == SATA_FPDMA_IN_ORDER))
621 return 1;
622
623 /* inhibit FPDMA auto activation */
110f66d2 624 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
fa5b561c
TH
625 tf->nsect == SATA_FPDMA_AA)
626 return 1;
b344991a
TH
627 }
628
3264a8d8
TH
629 return 0;
630}
631
11ef697b 632/**
0e8634bf 633 * ata_acpi_run_tf - send taskfile registers to host controller
3a32a8e9 634 * @dev: target ATA device
11ef697b
KCA
635 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
636 *
3696df30 637 * Outputs ATA taskfile to standard ATA host controller.
11ef697b
KCA
638 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
639 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
640 * hob_lbal, hob_lbam, and hob_lbah.
641 *
642 * This function waits for idle (!BUSY and !DRQ) after writing
643 * registers. If the control register has a new value, this
644 * function also waits for idle after writing control and before
645 * writing the remaining registers.
646 *
4700c4bc
TH
647 * LOCKING:
648 * EH context.
649 *
650 * RETURNS:
3264a8d8
TH
651 * 1 if command is executed successfully. 0 if ignored, rejected or
652 * filtered out, -errno on other errors.
11ef697b 653 */
0e8634bf 654static int ata_acpi_run_tf(struct ata_device *dev,
3264a8d8
TH
655 const struct ata_acpi_gtf *gtf,
656 const struct ata_acpi_gtf *prev_gtf)
11ef697b 657{
3264a8d8
TH
658 struct ata_taskfile *pptf = NULL;
659 struct ata_taskfile tf, ptf, rtf;
4700c4bc 660 unsigned int err_mask;
0e8634bf 661 const char *level;
6521148c 662 const char *descr;
0e8634bf
TH
663 char msg[60];
664 int rc;
fc16c25f 665
4700c4bc
TH
666 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
667 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
668 && (gtf->tf[6] == 0))
669 return 0;
11ef697b 670
3264a8d8
TH
671 ata_acpi_gtf_to_tf(dev, gtf, &tf);
672 if (prev_gtf) {
673 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
674 pptf = &ptf;
675 }
676
110f66d2 677 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
3264a8d8
TH
678 rtf = tf;
679 err_mask = ata_exec_internal(dev, &rtf, NULL,
680 DMA_NONE, NULL, 0, 0);
681
682 switch (err_mask) {
683 case 0:
684 level = KERN_DEBUG;
685 snprintf(msg, sizeof(msg), "succeeded");
686 rc = 1;
687 break;
688
689 case AC_ERR_DEV:
690 level = KERN_INFO;
691 snprintf(msg, sizeof(msg),
692 "rejected by device (Stat=0x%02x Err=0x%02x)",
693 rtf.command, rtf.feature);
694 rc = 0;
695 break;
696
697 default:
698 level = KERN_ERR;
699 snprintf(msg, sizeof(msg),
700 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
701 err_mask, rtf.command, rtf.feature);
702 rc = -EIO;
703 break;
704 }
705 } else {
0e8634bf 706 level = KERN_INFO;
3264a8d8 707 snprintf(msg, sizeof(msg), "filtered out");
0e8634bf 708 rc = 0;
4700c4bc 709 }
6521148c 710 descr = ata_get_cmd_descript(tf.command);
4700c4bc 711
0e8634bf 712 ata_dev_printk(dev, level,
6521148c 713 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
0e8634bf 714 tf.command, tf.feature, tf.nsect, tf.lbal,
6521148c
RH
715 tf.lbam, tf.lbah, tf.device,
716 (descr ? descr : "unknown"), msg);
0e8634bf
TH
717
718 return rc;
11ef697b
KCA
719}
720
11ef697b
KCA
721/**
722 * ata_acpi_exec_tfs - get then write drive taskfile settings
6746544c 723 * @dev: target ATA device
98a1708d 724 * @nr_executed: out parameter for the number of executed commands
11ef697b 725 *
98a1708d 726 * Evaluate _GTF and execute returned taskfiles.
69b16a5f
TH
727 *
728 * LOCKING:
729 * EH context.
730 *
731 * RETURNS:
66fa7f21
TH
732 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
733 * -errno on other errors.
11ef697b 734 */
66fa7f21 735static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
11ef697b 736{
3264a8d8 737 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
6746544c
TH
738 int gtf_count, i, rc;
739
740 /* get taskfiles */
66fa7f21
TH
741 rc = ata_dev_get_GTF(dev, &gtf);
742 if (rc < 0)
743 return rc;
744 gtf_count = rc;
6746544c
TH
745
746 /* execute them */
3264a8d8
TH
747 for (i = 0; i < gtf_count; i++, gtf++) {
748 rc = ata_acpi_run_tf(dev, gtf, pgtf);
0e8634bf
TH
749 if (rc < 0)
750 break;
3264a8d8 751 if (rc) {
0e8634bf 752 (*nr_executed)++;
3264a8d8
TH
753 pgtf = gtf;
754 }
11ef697b
KCA
755 }
756
398e0782 757 ata_acpi_clear_gtf(dev);
6746544c 758
0e8634bf
TH
759 if (rc < 0)
760 return rc;
761 return 0;
11ef697b
KCA
762}
763
7ea1fbc2
KCA
764/**
765 * ata_acpi_push_id - send Identify data to drive
3a32a8e9 766 * @dev: target ATA device
7ea1fbc2
KCA
767 *
768 * _SDD ACPI object: for SATA mode only
769 * Must be after Identify (Packet) Device -- uses its data
770 * ATM this function never returns a failure. It is an optional
771 * method and if it fails for whatever reason, we should still
772 * just keep going.
69b16a5f
TH
773 *
774 * LOCKING:
775 * EH context.
776 *
777 * RETURNS:
f2406770 778 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
7ea1fbc2 779 */
6746544c 780static int ata_acpi_push_id(struct ata_device *dev)
7ea1fbc2 781{
9af5c9c9 782 struct ata_port *ap = dev->link->ap;
3a32a8e9
TH
783 acpi_status status;
784 struct acpi_object_list input;
785 union acpi_object in_params[1];
7ea1fbc2 786
7ea1fbc2 787 if (ata_msg_probe(ap))
a9a79dfe
JP
788 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
789 __func__, dev->devno, ap->port_no);
7ea1fbc2 790
7ea1fbc2
KCA
791 /* Give the drive Identify data to the drive via the _SDD method */
792 /* _SDD: set up input parameters */
793 input.count = 1;
794 input.pointer = in_params;
795 in_params[0].type = ACPI_TYPE_BUFFER;
3a32a8e9
TH
796 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
797 in_params[0].buffer.pointer = (u8 *)dev->id;
7ea1fbc2
KCA
798 /* Output buffer: _SDD has no output */
799
800 /* It's OK for _SDD to be missing too. */
3a32a8e9 801 swap_buf_le16(dev->id, ATA_ID_WORDS);
30dcf76a
MG
802 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
803 NULL);
3a32a8e9 804 swap_buf_le16(dev->id, ATA_ID_WORDS);
7ea1fbc2 805
f2406770
TH
806 if (status == AE_NOT_FOUND)
807 return -ENOENT;
808
809 if (ACPI_FAILURE(status)) {
a9a79dfe 810 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
f2406770
TH
811 return -EIO;
812 }
7ea1fbc2 813
f2406770 814 return 0;
6746544c
TH
815}
816
64578a3d
TH
817/**
818 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
819 * @ap: target ATA port
820 *
821 * This function is called when @ap is about to be suspended. All
822 * devices are already put to sleep but the port_suspend() callback
823 * hasn't been executed yet. Error return from this function aborts
824 * suspend.
825 *
826 * LOCKING:
827 * EH context.
828 *
829 * RETURNS:
830 * 0 on success, -errno on failure.
831 */
832int ata_acpi_on_suspend(struct ata_port *ap)
833{
c05e6ff0
TH
834 /* nada */
835 return 0;
64578a3d
TH
836}
837
6746544c
TH
838/**
839 * ata_acpi_on_resume - ATA ACPI hook called on resume
840 * @ap: target ATA port
841 *
842 * This function is called when @ap is resumed - right after port
843 * itself is resumed but before any EH action is taken.
844 *
845 * LOCKING:
846 * EH context.
847 */
848void ata_acpi_on_resume(struct ata_port *ap)
849{
c05e6ff0 850 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
f58229f8 851 struct ata_device *dev;
6746544c 852
30dcf76a 853 if (ata_ap_acpi_handle(ap) && gtm) {
398e0782
TH
854 /* _GTM valid */
855
856 /* restore timing parameters */
c05e6ff0 857 ata_acpi_stm(ap, gtm);
64578a3d 858
398e0782
TH
859 /* _GTF should immediately follow _STM so that it can
860 * use values set by _STM. Cache _GTF result and
861 * schedule _GTF.
862 */
1eca4365 863 ata_for_each_dev(dev, &ap->link, ALL) {
398e0782 864 ata_acpi_clear_gtf(dev);
48feb3c4
SL
865 if (ata_dev_enabled(dev) &&
866 ata_dev_get_GTF(dev, NULL) >= 0)
398e0782
TH
867 dev->flags |= ATA_DFLAG_ACPI_PENDING;
868 }
869 } else {
870 /* SATA _GTF needs to be evaulated after _SDD and
871 * there's no reason to evaluate IDE _GTF early
872 * without _STM. Clear cache and schedule _GTF.
873 */
1eca4365 874 ata_for_each_dev(dev, &ap->link, ALL) {
398e0782 875 ata_acpi_clear_gtf(dev);
48feb3c4
SL
876 if (ata_dev_enabled(dev))
877 dev->flags |= ATA_DFLAG_ACPI_PENDING;
398e0782
TH
878 }
879 }
7ea1fbc2
KCA
880}
881
a7ff60db 882static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
21334205
AL
883{
884 int d_max_in = ACPI_STATE_D3_COLD;
a7ff60db
AL
885 if (!runtime)
886 goto out;
21334205
AL
887
888 /*
889 * For ATAPI, runtime D3 cold is only allowed
890 * for ZPODD in zero power ready state
891 */
892 if (dev->class == ATA_DEV_ATAPI &&
893 !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
894 d_max_in = ACPI_STATE_D3_HOT;
895
a7ff60db 896out:
21334205
AL
897 return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev,
898 NULL, d_max_in);
899}
900
a7ff60db 901static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
bd3adca5 902{
a7ff60db 903 bool runtime = PMSG_IS_AUTO(state);
bd3adca5 904 struct ata_device *dev;
febe53ba 905 acpi_handle handle;
3bd46600 906 int acpi_state;
bd3adca5 907
1eca4365 908 ata_for_each_dev(dev, &ap->link, ENABLED) {
febe53ba 909 handle = ata_dev_acpi_handle(dev);
3bd46600
LM
910 if (!handle)
911 continue;
912
a7ff60db
AL
913 if (!(state.event & PM_EVENT_RESUME)) {
914 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
21334205
AL
915 if (acpi_state == ACPI_STATE_D0)
916 continue;
a7ff60db 917 if (runtime && zpodd_dev_enabled(dev) &&
21334205
AL
918 acpi_state == ACPI_STATE_D3_COLD)
919 zpodd_enable_run_wake(dev);
920 acpi_bus_set_power(handle, acpi_state);
3bd46600 921 } else {
a7ff60db 922 if (runtime && zpodd_dev_enabled(dev))
21334205 923 zpodd_disable_run_wake(dev);
3bd46600
LM
924 acpi_bus_set_power(handle, ACPI_STATE_D0);
925 }
bd3adca5 926 }
a7ff60db 927}
febe53ba 928
a7ff60db
AL
929/* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
930static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
931{
932 struct ata_device *dev;
933 acpi_handle port_handle;
934
935 port_handle = ata_ap_acpi_handle(ap);
936 if (!port_handle)
937 return;
938
939 /* channel first and then drives for power on and vica versa
940 for power off */
941 if (state.event & PM_EVENT_RESUME)
942 acpi_bus_set_power(port_handle, ACPI_STATE_D0);
febe53ba 943
a7ff60db
AL
944 ata_for_each_dev(dev, &ap->link, ENABLED) {
945 acpi_handle dev_handle = ata_dev_acpi_handle(dev);
946 if (!dev_handle)
947 continue;
948
949 acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
950 ACPI_STATE_D0 : ACPI_STATE_D3);
951 }
952
953 if (!(state.event & PM_EVENT_RESUME))
954 acpi_bus_set_power(port_handle, ACPI_STATE_D3);
955}
956
957/**
958 * ata_acpi_set_state - set the port power state
959 * @ap: target ATA port
960 * @state: state, on/off
961 *
962 * This function sets a proper ACPI D state for the device on
963 * system and runtime PM operations.
964 */
965void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
966{
967 if (ap->flags & ATA_FLAG_ACPI_SATA)
968 sata_acpi_set_state(ap, state);
969 else
970 pata_acpi_set_state(ap, state);
bd3adca5
SL
971}
972
6746544c
TH
973/**
974 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
975 * @dev: target ATA device
976 *
977 * This function is called when @dev is about to be configured.
978 * IDENTIFY data might have been modified after this hook is run.
979 *
980 * LOCKING:
981 * EH context.
982 *
983 * RETURNS:
984 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
985 * -errno on failure.
986 */
987int ata_acpi_on_devcfg(struct ata_device *dev)
988{
9af5c9c9
TH
989 struct ata_port *ap = dev->link->ap;
990 struct ata_eh_context *ehc = &ap->link.eh_context;
6746544c 991 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
66fa7f21 992 int nr_executed = 0;
6746544c
TH
993 int rc;
994
30dcf76a 995 if (!ata_dev_acpi_handle(dev))
6746544c
TH
996 return 0;
997
998 /* do we need to do _GTF? */
999 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
1000 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
1001 return 0;
1002
1003 /* do _SDD if SATA */
1004 if (acpi_sata) {
1005 rc = ata_acpi_push_id(dev);
f2406770 1006 if (rc && rc != -ENOENT)
6746544c
TH
1007 goto acpi_err;
1008 }
1009
1010 /* do _GTF */
66fa7f21
TH
1011 rc = ata_acpi_exec_tfs(dev, &nr_executed);
1012 if (rc)
6746544c
TH
1013 goto acpi_err;
1014
1015 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
1016
1017 /* refresh IDENTIFY page if any _GTF command has been executed */
66fa7f21 1018 if (nr_executed) {
6746544c
TH
1019 rc = ata_dev_reread_id(dev, 0);
1020 if (rc < 0) {
a9a79dfe
JP
1021 ata_dev_err(dev,
1022 "failed to IDENTIFY after ACPI commands\n");
6746544c
TH
1023 return rc;
1024 }
1025 }
1026
1027 return 0;
1028
1029 acpi_err:
66fa7f21
TH
1030 /* ignore evaluation failure if we can continue safely */
1031 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1032 return 0;
6746544c 1033
66fa7f21
TH
1034 /* fail and let EH retry once more for unknown IO errors */
1035 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1036 dev->flags |= ATA_DFLAG_ACPI_FAILED;
1037 return rc;
6746544c 1038 }
66fa7f21 1039
0d0cdb02 1040 dev->flags |= ATA_DFLAG_ACPI_DISABLED;
a9a79dfe 1041 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
66fa7f21
TH
1042
1043 /* We can safely continue if no _GTF command has been executed
1044 * and port is not frozen.
1045 */
1046 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1047 return 0;
1048
6746544c
TH
1049 return rc;
1050}
562f0c2d
TH
1051
1052/**
1053 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1054 * @dev: target ATA device
1055 *
1056 * This function is called when @dev is about to be disabled.
1057 *
1058 * LOCKING:
1059 * EH context.
1060 */
1061void ata_acpi_on_disable(struct ata_device *dev)
1062{
398e0782 1063 ata_acpi_clear_gtf(dev);
562f0c2d 1064}
6b66d958
MG
1065
1066static int compat_pci_ata(struct ata_port *ap)
1067{
1068 struct device *dev = ap->tdev.parent;
1069 struct pci_dev *pdev;
1070
1071 if (!is_pci_dev(dev))
1072 return 0;
1073
1074 pdev = to_pci_dev(dev);
1075
1076 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1077 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1078 return 0;
1079
1080 return 1;
1081}
1082
1083static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1084{
19ccee76 1085 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA)
6b66d958
MG
1086 return -ENODEV;
1087
1088 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1089 ap->port_no);
1090
1091 if (!*handle)
1092 return -ENODEV;
1093
d66af4df 1094 if (__ata_acpi_gtm(ap, *handle, &ap->__acpi_init_gtm) == 0)
83400917
AL
1095 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1096
6b66d958
MG
1097 return 0;
1098}
1099
1100static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1101 acpi_handle *handle)
1102{
1103 struct ata_device *ata_dev;
1104
60817a68
AL
1105 if (ap->flags & ATA_FLAG_ACPI_SATA) {
1106 if (!sata_pmp_attached(ap))
1107 ata_dev = &ap->link.device[sdev->id];
1108 else
1109 ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id];
1110 }
1111 else {
6b66d958 1112 ata_dev = &ap->link.device[sdev->id];
60817a68 1113 }
6b66d958 1114
30dcf76a 1115 *handle = ata_dev_acpi_handle(ata_dev);
6b66d958
MG
1116
1117 if (!*handle)
1118 return -ENODEV;
1119
1120 return 0;
1121}
1122
1123static int is_ata_port(const struct device *dev)
1124{
1125 return dev->type == &ata_port_type;
1126}
1127
1128static struct ata_port *dev_to_ata_port(struct device *dev)
1129{
1130 while (!is_ata_port(dev)) {
1131 if (!dev->parent)
1132 return NULL;
1133 dev = dev->parent;
1134 }
1135 return to_ata_port(dev);
1136}
1137
1138static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1139{
1140 struct ata_port *ap = dev_to_ata_port(dev);
1141
1142 if (!ap)
1143 return -ENODEV;
1144
1145 if (!compat_pci_ata(ap))
1146 return -ENODEV;
1147
1148 if (scsi_is_host_device(dev))
1149 return ata_acpi_bind_host(ap, handle);
1150 else if (scsi_is_sdev_device(dev)) {
1151 struct scsi_device *sdev = to_scsi_device(dev);
1152
1153 return ata_acpi_bind_device(ap, sdev, handle);
1154 } else
1155 return -ENODEV;
1156}
1157
6b66d958 1158static struct acpi_bus_type ata_acpi_bus = {
53540098 1159 .name = "ATA",
6b66d958
MG
1160 .find_device = ata_acpi_find_device,
1161};
1162
1163int ata_acpi_register(void)
1164{
1165 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1166}
1167
1168void ata_acpi_unregister(void)
1169{
1170 scsi_unregister_acpi_bus_type(&ata_acpi_bus);
1171}
This page took 0.948291 seconds and 5 git commands to generate.