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