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