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