📄 libata-acpi.c
字号:
/* * libata-acpi.c * Provides ACPI support for PATA/SATA. * * Copyright (C) 2006 Intel Corp. * Copyright (C) 2006 Randy Dunlap */#include <linux/module.h>#include <linux/ata.h>#include <linux/delay.h>#include <linux/device.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/acpi.h>#include <linux/libata.h>#include <linux/pci.h>#include <scsi/scsi_device.h>#include "libata.h"#include <acpi/acpi_bus.h>#include <acpi/acnames.h>#include <acpi/acnamesp.h>#include <acpi/acparser.h>#include <acpi/acexcep.h>#include <acpi/acmacros.h>#include <acpi/actypes.h>enum { ATA_ACPI_FILTER_SETXFER = 1 << 0, ATA_ACPI_FILTER_LOCK = 1 << 1, ATA_ACPI_FILTER_DEFAULT = ATA_ACPI_FILTER_SETXFER | ATA_ACPI_FILTER_LOCK,};static unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock)");#define NO_PORT_MULT 0xffff#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))#define REGS_PER_GTF 7struct ata_acpi_gtf { u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */} __packed;/* * Helper - belongs in the PCI layer somewhere eventually */static int is_pci_dev(struct device *dev){ return (dev->bus == &pci_bus_type);}static void ata_acpi_clear_gtf(struct ata_device *dev){ kfree(dev->gtf_cache); dev->gtf_cache = NULL;}/** * ata_acpi_associate_sata_port - associate SATA port with ACPI objects * @ap: target SATA port * * Look up ACPI objects associated with @ap and initialize acpi_handle * fields of @ap, the port and devices accordingly. * * LOCKING: * EH context. * * RETURNS: * 0 on success, -errno on failure. */void ata_acpi_associate_sata_port(struct ata_port *ap){ WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA)); if (!ap->nr_pmp_links) { acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT); ap->link.device->acpi_handle = acpi_get_child(ap->host->acpi_handle, adr); } else { struct ata_link *link; ap->link.device->acpi_handle = NULL; ata_port_for_each_link(link, ap) { acpi_integer adr = SATA_ADR(ap->port_no, link->pmp); link->device->acpi_handle = acpi_get_child(ap->host->acpi_handle, adr); } }}static void ata_acpi_associate_ide_port(struct ata_port *ap){ int max_devices, i; ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no); if (!ap->acpi_handle) return; max_devices = 1; if (ap->flags & ATA_FLAG_SLAVE_POSS) max_devices++; for (i = 0; i < max_devices; i++) { struct ata_device *dev = &ap->link.device[i]; dev->acpi_handle = acpi_get_child(ap->acpi_handle, i); } if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0) ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;}static void ata_acpi_handle_hotplug(struct ata_port *ap, struct kobject *kobj, u32 event){ char event_string[12]; char *envp[] = { event_string, NULL }; struct ata_eh_info *ehi = &ap->link.eh_info; if (event == 0 || event == 1) { unsigned long flags; spin_lock_irqsave(ap->lock, flags); ata_ehi_clear_desc(ehi); ata_ehi_push_desc(ehi, "ACPI event"); ata_ehi_hotplugged(ehi); ata_port_freeze(ap); spin_unlock_irqrestore(ap->lock, flags); } if (kobj) { sprintf(event_string, "BAY_EVENT=%d", event); kobject_uevent_env(kobj, KOBJ_CHANGE, envp); }}static void ata_acpi_dev_notify(acpi_handle handle, u32 event, void *data){ struct ata_device *dev = data; struct kobject *kobj = NULL; if (dev->sdev) kobj = &dev->sdev->sdev_gendev.kobj; ata_acpi_handle_hotplug(dev->link->ap, kobj, event);}static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data){ struct ata_port *ap = data; ata_acpi_handle_hotplug(ap, &ap->dev->kobj, event);}/** * ata_acpi_associate - associate ATA host with ACPI objects * @host: target ATA host * * Look up ACPI objects associated with @host and initialize * acpi_handle fields of @host, its ports and devices accordingly. * * LOCKING: * EH context. * * RETURNS: * 0 on success, -errno on failure. */void ata_acpi_associate(struct ata_host *host){ int i, j; if (!is_pci_dev(host->dev) || libata_noacpi) return; host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev); if (!host->acpi_handle) return; for (i = 0; i < host->n_ports; i++) { struct ata_port *ap = host->ports[i]; if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA) ata_acpi_associate_sata_port(ap); else ata_acpi_associate_ide_port(ap); if (ap->acpi_handle) acpi_install_notify_handler (ap->acpi_handle, ACPI_SYSTEM_NOTIFY, ata_acpi_ap_notify, ap); for (j = 0; j < ata_link_max_devices(&ap->link); j++) { struct ata_device *dev = &ap->link.device[j]; if (dev->acpi_handle) acpi_install_notify_handler (dev->acpi_handle, ACPI_SYSTEM_NOTIFY, ata_acpi_dev_notify, dev); } }}/** * ata_acpi_dissociate - dissociate ATA host from ACPI objects * @host: target ATA host * * This function is called during driver detach after the whole host * is shut down. * * LOCKING: * EH context. */void ata_acpi_dissociate(struct ata_host *host){ int i; /* Restore initial _GTM values so that driver which attaches * afterward can use them too. */ for (i = 0; i < host->n_ports; i++) { struct ata_port *ap = host->ports[i]; const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap); if (ap->acpi_handle && gtm) ata_acpi_stm(ap, gtm); }}/** * ata_acpi_gtm - execute _GTM * @ap: target ATA port * @gtm: out parameter for _GTM result * * Evaluate _GTM and store the result in @gtm. * * LOCKING: * EH context. * * RETURNS: * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure. */int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm){ struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER }; union acpi_object *out_obj; acpi_status status; int rc = 0; status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output); rc = -ENOENT; if (status == AE_NOT_FOUND) goto out_free; rc = -EINVAL; if (ACPI_FAILURE(status)) { ata_port_printk(ap, KERN_ERR, "ACPI get timing mode failed (AE 0x%x)\n", status); goto out_free; } out_obj = output.pointer; if (out_obj->type != ACPI_TYPE_BUFFER) { ata_port_printk(ap, KERN_WARNING, "_GTM returned unexpected object type 0x%x\n", out_obj->type); goto out_free; } if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) { ata_port_printk(ap, KERN_ERR, "_GTM returned invalid length %d\n", out_obj->buffer.length); goto out_free; } memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm)); rc = 0; out_free: kfree(output.pointer); return rc;}EXPORT_SYMBOL_GPL(ata_acpi_gtm);/** * ata_acpi_stm - execute _STM * @ap: target ATA port * @stm: timing parameter to _STM * * Evaluate _STM with timing parameter @stm. * * LOCKING: * EH context. * * RETURNS: * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure. */int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm){ acpi_status status; struct ata_acpi_gtm stm_buf = *stm; struct acpi_object_list input; union acpi_object in_params[3]; in_params[0].type = ACPI_TYPE_BUFFER; in_params[0].buffer.length = sizeof(struct ata_acpi_gtm); in_params[0].buffer.pointer = (u8 *)&stm_buf; /* Buffers for id may need byteswapping ? */ in_params[1].type = ACPI_TYPE_BUFFER; in_params[1].buffer.length = 512; in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id; in_params[2].type = ACPI_TYPE_BUFFER; in_params[2].buffer.length = 512; in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id; input.count = 3; input.pointer = in_params; status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL); if (status == AE_NOT_FOUND) return -ENOENT; if (ACPI_FAILURE(status)) { ata_port_printk(ap, KERN_ERR, "ACPI set timing mode failed (status=0x%x)\n", status); return -EINVAL; } return 0;}EXPORT_SYMBOL_GPL(ata_acpi_stm);/** * ata_dev_get_GTF - get the drive bootup default taskfile settings * @dev: target ATA device * @gtf: output parameter for buffer containing _GTF taskfile arrays * * This applies to both PATA and SATA drives. * * The _GTF method has no input parameters. * It returns a variable number of register set values (registers * hex 1F1..1F7, taskfiles). * The <variable number> is not known in advance, so have ACPI-CA * allocate the buffer as needed and return it, then free it later. * * LOCKING: * EH context. * * RETURNS: * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL * if _GTF is invalid. */static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf){ struct ata_port *ap = dev->link->ap; acpi_status status; struct acpi_buffer output; union acpi_object *out_obj; int rc = 0; /* if _GTF is cached, use the cached value */ if (dev->gtf_cache) { out_obj = dev->gtf_cache; goto done; } /* set up output buffer */ output.length = ACPI_ALLOCATE_BUFFER; output.pointer = NULL; /* ACPI-CA sets this; save/free it later */ if (ata_msg_probe(ap)) ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n", __FUNCTION__, ap->port_no); /* _GTF has no input parameters */ status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output); out_obj = dev->gtf_cache = output.pointer; if (ACPI_FAILURE(status)) { if (status != AE_NOT_FOUND) { ata_dev_printk(dev, KERN_WARNING, "_GTF evaluation failed (AE 0x%x)\n", status); rc = -EINVAL; } goto out_free; } if (!output.length || !output.pointer) { if (ata_msg_probe(ap)) ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: " "length or ptr is NULL (0x%llx, 0x%p)\n", __FUNCTION__, (unsigned long long)output.length, output.pointer); rc = -EINVAL; goto out_free; } if (out_obj->type != ACPI_TYPE_BUFFER) { ata_dev_printk(dev, KERN_WARNING, "_GTF unexpected object type 0x%x\n", out_obj->type); rc = -EINVAL; goto out_free; } if (out_obj->buffer.length % REGS_PER_GTF) { ata_dev_printk(dev, KERN_WARNING, "unexpected _GTF length (%d)\n", out_obj->buffer.length); rc = -EINVAL; goto out_free; } done: rc = out_obj->buffer.length / REGS_PER_GTF; if (gtf) { *gtf = (void *)out_obj->buffer.pointer; if (ata_msg_probe(ap)) ata_dev_printk(dev, KERN_DEBUG, "%s: returning gtf=%p, gtf_count=%d\n", __FUNCTION__, *gtf, rc); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -