File Index
Files and directories
Sign in
FILE

libata-acpi.c

File preview with syntax highlighting.

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