162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * PowerNV OPAL Sensor-groups interface 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright 2017 IBM Corp. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#define pr_fmt(fmt) "opal-sensor-groups: " fmt 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <linux/of.h> 1162306a36Sopenharmony_ci#include <linux/kobject.h> 1262306a36Sopenharmony_ci#include <linux/slab.h> 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <asm/opal.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_cistatic DEFINE_MUTEX(sg_mutex); 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_cistatic struct kobject *sg_kobj; 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_cistruct sg_attr { 2162306a36Sopenharmony_ci u32 handle; 2262306a36Sopenharmony_ci struct kobj_attribute attr; 2362306a36Sopenharmony_ci}; 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_cistatic struct sensor_group { 2662306a36Sopenharmony_ci char name[20]; 2762306a36Sopenharmony_ci struct attribute_group sg; 2862306a36Sopenharmony_ci struct sg_attr *sgattrs; 2962306a36Sopenharmony_ci} *sgs; 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ciint sensor_group_enable(u32 handle, bool enable) 3262306a36Sopenharmony_ci{ 3362306a36Sopenharmony_ci struct opal_msg msg; 3462306a36Sopenharmony_ci int token, ret; 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci token = opal_async_get_token_interruptible(); 3762306a36Sopenharmony_ci if (token < 0) 3862306a36Sopenharmony_ci return token; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci ret = opal_sensor_group_enable(handle, token, enable); 4162306a36Sopenharmony_ci if (ret == OPAL_ASYNC_COMPLETION) { 4262306a36Sopenharmony_ci ret = opal_async_wait_response(token, &msg); 4362306a36Sopenharmony_ci if (ret) { 4462306a36Sopenharmony_ci pr_devel("Failed to wait for the async response\n"); 4562306a36Sopenharmony_ci ret = -EIO; 4662306a36Sopenharmony_ci goto out; 4762306a36Sopenharmony_ci } 4862306a36Sopenharmony_ci ret = opal_error_code(opal_get_async_rc(msg)); 4962306a36Sopenharmony_ci } else { 5062306a36Sopenharmony_ci ret = opal_error_code(ret); 5162306a36Sopenharmony_ci } 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ciout: 5462306a36Sopenharmony_ci opal_async_release_token(token); 5562306a36Sopenharmony_ci return ret; 5662306a36Sopenharmony_ci} 5762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(sensor_group_enable); 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cistatic ssize_t sg_store(struct kobject *kobj, struct kobj_attribute *attr, 6062306a36Sopenharmony_ci const char *buf, size_t count) 6162306a36Sopenharmony_ci{ 6262306a36Sopenharmony_ci struct sg_attr *sattr = container_of(attr, struct sg_attr, attr); 6362306a36Sopenharmony_ci struct opal_msg msg; 6462306a36Sopenharmony_ci u32 data; 6562306a36Sopenharmony_ci int ret, token; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci ret = kstrtoint(buf, 0, &data); 6862306a36Sopenharmony_ci if (ret) 6962306a36Sopenharmony_ci return ret; 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci if (data != 1) 7262306a36Sopenharmony_ci return -EINVAL; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci token = opal_async_get_token_interruptible(); 7562306a36Sopenharmony_ci if (token < 0) { 7662306a36Sopenharmony_ci pr_devel("Failed to get token\n"); 7762306a36Sopenharmony_ci return token; 7862306a36Sopenharmony_ci } 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci ret = mutex_lock_interruptible(&sg_mutex); 8162306a36Sopenharmony_ci if (ret) 8262306a36Sopenharmony_ci goto out_token; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci ret = opal_sensor_group_clear(sattr->handle, token); 8562306a36Sopenharmony_ci switch (ret) { 8662306a36Sopenharmony_ci case OPAL_ASYNC_COMPLETION: 8762306a36Sopenharmony_ci ret = opal_async_wait_response(token, &msg); 8862306a36Sopenharmony_ci if (ret) { 8962306a36Sopenharmony_ci pr_devel("Failed to wait for the async response\n"); 9062306a36Sopenharmony_ci ret = -EIO; 9162306a36Sopenharmony_ci goto out; 9262306a36Sopenharmony_ci } 9362306a36Sopenharmony_ci ret = opal_error_code(opal_get_async_rc(msg)); 9462306a36Sopenharmony_ci if (!ret) 9562306a36Sopenharmony_ci ret = count; 9662306a36Sopenharmony_ci break; 9762306a36Sopenharmony_ci case OPAL_SUCCESS: 9862306a36Sopenharmony_ci ret = count; 9962306a36Sopenharmony_ci break; 10062306a36Sopenharmony_ci default: 10162306a36Sopenharmony_ci ret = opal_error_code(ret); 10262306a36Sopenharmony_ci } 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ciout: 10562306a36Sopenharmony_ci mutex_unlock(&sg_mutex); 10662306a36Sopenharmony_ciout_token: 10762306a36Sopenharmony_ci opal_async_release_token(token); 10862306a36Sopenharmony_ci return ret; 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_cistatic struct sg_ops_info { 11262306a36Sopenharmony_ci int opal_no; 11362306a36Sopenharmony_ci const char *attr_name; 11462306a36Sopenharmony_ci ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, 11562306a36Sopenharmony_ci const char *buf, size_t count); 11662306a36Sopenharmony_ci} ops_info[] = { 11762306a36Sopenharmony_ci { OPAL_SENSOR_GROUP_CLEAR, "clear", sg_store }, 11862306a36Sopenharmony_ci}; 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_cistatic void add_attr(int handle, struct sg_attr *attr, int index) 12162306a36Sopenharmony_ci{ 12262306a36Sopenharmony_ci attr->handle = handle; 12362306a36Sopenharmony_ci sysfs_attr_init(&attr->attr.attr); 12462306a36Sopenharmony_ci attr->attr.attr.name = ops_info[index].attr_name; 12562306a36Sopenharmony_ci attr->attr.attr.mode = 0220; 12662306a36Sopenharmony_ci attr->attr.store = ops_info[index].store; 12762306a36Sopenharmony_ci} 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_cistatic int __init add_attr_group(const __be32 *ops, int len, struct sensor_group *sg, 13062306a36Sopenharmony_ci u32 handle) 13162306a36Sopenharmony_ci{ 13262306a36Sopenharmony_ci int i, j; 13362306a36Sopenharmony_ci int count = 0; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci for (i = 0; i < len; i++) 13662306a36Sopenharmony_ci for (j = 0; j < ARRAY_SIZE(ops_info); j++) 13762306a36Sopenharmony_ci if (be32_to_cpu(ops[i]) == ops_info[j].opal_no) { 13862306a36Sopenharmony_ci add_attr(handle, &sg->sgattrs[count], j); 13962306a36Sopenharmony_ci sg->sg.attrs[count] = 14062306a36Sopenharmony_ci &sg->sgattrs[count].attr.attr; 14162306a36Sopenharmony_ci count++; 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci return sysfs_create_group(sg_kobj, &sg->sg); 14562306a36Sopenharmony_ci} 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_cistatic int __init get_nr_attrs(const __be32 *ops, int len) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci int i, j; 15062306a36Sopenharmony_ci int nr_attrs = 0; 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci for (i = 0; i < len; i++) 15362306a36Sopenharmony_ci for (j = 0; j < ARRAY_SIZE(ops_info); j++) 15462306a36Sopenharmony_ci if (be32_to_cpu(ops[i]) == ops_info[j].opal_no) 15562306a36Sopenharmony_ci nr_attrs++; 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci return nr_attrs; 15862306a36Sopenharmony_ci} 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_civoid __init opal_sensor_groups_init(void) 16162306a36Sopenharmony_ci{ 16262306a36Sopenharmony_ci struct device_node *sg, *node; 16362306a36Sopenharmony_ci int i = 0; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci sg = of_find_compatible_node(NULL, NULL, "ibm,opal-sensor-group"); 16662306a36Sopenharmony_ci if (!sg) { 16762306a36Sopenharmony_ci pr_devel("Sensor groups node not found\n"); 16862306a36Sopenharmony_ci return; 16962306a36Sopenharmony_ci } 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci sgs = kcalloc(of_get_child_count(sg), sizeof(*sgs), GFP_KERNEL); 17262306a36Sopenharmony_ci if (!sgs) 17362306a36Sopenharmony_ci goto out_sg_put; 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci sg_kobj = kobject_create_and_add("sensor_groups", opal_kobj); 17662306a36Sopenharmony_ci if (!sg_kobj) { 17762306a36Sopenharmony_ci pr_warn("Failed to create sensor group kobject\n"); 17862306a36Sopenharmony_ci goto out_sgs; 17962306a36Sopenharmony_ci } 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci for_each_child_of_node(sg, node) { 18262306a36Sopenharmony_ci const __be32 *ops; 18362306a36Sopenharmony_ci u32 sgid, len, nr_attrs, chipid; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci ops = of_get_property(node, "ops", &len); 18662306a36Sopenharmony_ci if (!ops) 18762306a36Sopenharmony_ci continue; 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci nr_attrs = get_nr_attrs(ops, len); 19062306a36Sopenharmony_ci if (!nr_attrs) 19162306a36Sopenharmony_ci continue; 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci sgs[i].sgattrs = kcalloc(nr_attrs, sizeof(*sgs[i].sgattrs), 19462306a36Sopenharmony_ci GFP_KERNEL); 19562306a36Sopenharmony_ci if (!sgs[i].sgattrs) 19662306a36Sopenharmony_ci goto out_sgs_sgattrs; 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci sgs[i].sg.attrs = kcalloc(nr_attrs + 1, 19962306a36Sopenharmony_ci sizeof(*sgs[i].sg.attrs), 20062306a36Sopenharmony_ci GFP_KERNEL); 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci if (!sgs[i].sg.attrs) { 20362306a36Sopenharmony_ci kfree(sgs[i].sgattrs); 20462306a36Sopenharmony_ci goto out_sgs_sgattrs; 20562306a36Sopenharmony_ci } 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci if (of_property_read_u32(node, "sensor-group-id", &sgid)) { 20862306a36Sopenharmony_ci pr_warn("sensor-group-id property not found\n"); 20962306a36Sopenharmony_ci goto out_sgs_sgattrs; 21062306a36Sopenharmony_ci } 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci if (!of_property_read_u32(node, "ibm,chip-id", &chipid)) 21362306a36Sopenharmony_ci sprintf(sgs[i].name, "%pOFn%d", node, chipid); 21462306a36Sopenharmony_ci else 21562306a36Sopenharmony_ci sprintf(sgs[i].name, "%pOFn", node); 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci sgs[i].sg.name = sgs[i].name; 21862306a36Sopenharmony_ci if (add_attr_group(ops, len, &sgs[i], sgid)) { 21962306a36Sopenharmony_ci pr_warn("Failed to create sensor attribute group %s\n", 22062306a36Sopenharmony_ci sgs[i].sg.name); 22162306a36Sopenharmony_ci goto out_sgs_sgattrs; 22262306a36Sopenharmony_ci } 22362306a36Sopenharmony_ci i++; 22462306a36Sopenharmony_ci } 22562306a36Sopenharmony_ci of_node_put(sg); 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci return; 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ciout_sgs_sgattrs: 23062306a36Sopenharmony_ci while (--i >= 0) { 23162306a36Sopenharmony_ci kfree(sgs[i].sgattrs); 23262306a36Sopenharmony_ci kfree(sgs[i].sg.attrs); 23362306a36Sopenharmony_ci } 23462306a36Sopenharmony_ci kobject_put(sg_kobj); 23562306a36Sopenharmony_ci of_node_put(node); 23662306a36Sopenharmony_ciout_sgs: 23762306a36Sopenharmony_ci kfree(sgs); 23862306a36Sopenharmony_ciout_sg_put: 23962306a36Sopenharmony_ci of_node_put(sg); 24062306a36Sopenharmony_ci} 241