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