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