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