#define pr_fmt(fmt) "papr-indices: " fmt
#include <linux/build_bug.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/lockdep.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/signal.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/string_helpers.h>
#include <linux/uaccess.h>
#include <asm/machdep.h>
#include <asm/rtas-work-area.h>
#include <asm/rtas.h>
#include <uapi/asm/papr-indices.h>
#include "papr-rtas-common.h"
#define RTAS_IBM_DYNAMIC_INDICE_NO_INDICATOR -3
struct rtas_get_indices_params {
u8 is_sensor;
u32 indice_type;
struct rtas_work_area *work_area;
u32 next;
s32 status;
};
static int rtas_ibm_get_indices(struct rtas_get_indices_params *params)
{
struct rtas_work_area *work_area = params->work_area;
const s32 token = rtas_function_token(RTAS_FN_IBM_GET_INDICES);
u32 rets;
s32 fwrc;
int ret;
if (token == RTAS_UNKNOWN_SERVICE)
return -ENOENT;
lockdep_assert_held(&rtas_ibm_get_indices_lock);
do {
fwrc = rtas_call(token, 5, 2, &rets, params->is_sensor,
params->indice_type,
rtas_work_area_phys(work_area),
rtas_work_area_size(work_area),
params->next);
} while (rtas_busy_delay(fwrc));
switch (fwrc) {
case RTAS_HARDWARE_ERROR:
ret = -EIO;
break;
case RTAS_INVALID_PARAMETER:
ret = -EINVAL;
break;
case RTAS_SEQ_START_OVER:
ret = -EAGAIN;
pr_info_ratelimited("Indices changed during retrieval, retrying\n");
params->next = 1;
break;
case RTAS_SEQ_MORE_DATA:
params->next = rets;
ret = 0;
break;
case RTAS_SEQ_COMPLETE:
params->next = 0;
ret = 0;
break;
default:
ret = -EIO;
pr_err_ratelimited("unexpected ibm,get-indices status %d\n", fwrc);
break;
}
params->status = fwrc;
return ret;
}
static void indices_sequence_begin(struct papr_rtas_sequence *seq)
{
struct rtas_get_indices_params *param;
param = (struct rtas_get_indices_params *)seq->params;
mutex_lock(&rtas_ibm_get_indices_lock);
param->work_area = rtas_work_area_alloc(RTAS_GET_INDICES_BUF_SIZE);
param->next = 1;
param->status = 0;
}
static void indices_sequence_end(struct papr_rtas_sequence *seq)
{
struct rtas_get_indices_params *param;
param = (struct rtas_get_indices_params *)seq->params;
rtas_work_area_free(param->work_area);
mutex_unlock(&rtas_ibm_get_indices_lock);
}
static const char *indices_sequence_fill_work_area(struct papr_rtas_sequence *seq,
size_t *len)
{
struct rtas_get_indices_params *p;
bool init_state;
p = (struct rtas_get_indices_params *)seq->params;
init_state = (p->next == 1) ? true : false;
if (papr_rtas_sequence_should_stop(seq, p->status, init_state))
return NULL;
if (papr_rtas_sequence_set_err(seq, rtas_ibm_get_indices(p)))
return NULL;
*len = RTAS_GET_INDICES_BUF_SIZE;
return rtas_work_area_raw_buf(p->work_area);
}
static ssize_t papr_indices_handle_read(struct file *file,
char __user *buf, size_t size, loff_t *off)
{
const struct papr_rtas_blob *blob = file->private_data;
if (!papr_rtas_blob_has_data(blob)) {
pr_err_once("handle without data\n");
return -EIO;
}
if (size < RTAS_GET_INDICES_BUF_SIZE) {
pr_err_once("Invalid buffer length %ld, expect %d\n",
size, RTAS_GET_INDICES_BUF_SIZE);
return -EINVAL;
} else if (size > RTAS_GET_INDICES_BUF_SIZE)
size = RTAS_GET_INDICES_BUF_SIZE;
return simple_read_from_buffer(buf, size, off, blob->data, blob->len);
}
static const struct file_operations papr_indices_handle_ops = {
.read = papr_indices_handle_read,
.llseek = papr_rtas_common_handle_seek,
.release = papr_rtas_common_handle_release,
};
static long papr_indices_create_handle(struct papr_indices_io_block __user *ubuf)
{
struct papr_rtas_sequence seq = {};
struct rtas_get_indices_params params = {};
int fd;
if (get_user(params.is_sensor, &ubuf->indices.is_sensor))
return -EFAULT;
if (get_user(params.indice_type, &ubuf->indices.indice_type))
return -EFAULT;
seq = (struct papr_rtas_sequence) {
.begin = indices_sequence_begin,
.end = indices_sequence_end,
.work = indices_sequence_fill_work_area,
};
seq.params = ¶ms;
fd = papr_rtas_setup_file_interface(&seq,
&papr_indices_handle_ops, "[papr-indices]");
return fd;
}
static struct rtas_work_area *
papr_dynamic_indice_buf_from_user(struct papr_indices_io_block __user *ubuf,
struct papr_indices_io_block *kbuf)
{
struct rtas_work_area *work_area;
u32 length;
__be32 len_be;
if (copy_from_user(kbuf, ubuf, sizeof(*kbuf)))
return ERR_PTR(-EFAULT);
if (!string_is_terminated(kbuf->dynamic_param.location_code_str,
ARRAY_SIZE(kbuf->dynamic_param.location_code_str)))
return ERR_PTR(-EINVAL);
length = strlen(kbuf->dynamic_param.location_code_str) + 1;
if (length > LOC_CODE_SIZE)
return ERR_PTR(-EINVAL);
len_be = cpu_to_be32(length);
work_area = rtas_work_area_alloc(LOC_CODE_SIZE + sizeof(u32));
memcpy(rtas_work_area_raw_buf(work_area), &len_be, sizeof(u32));
memcpy((rtas_work_area_raw_buf(work_area) + sizeof(u32)),
&kbuf->dynamic_param.location_code_str, length);
return work_area;
}
static long papr_dynamic_indicator_ioc_set(struct papr_indices_io_block __user *ubuf)
{
struct papr_indices_io_block kbuf;
struct rtas_work_area *work_area;
s32 fwrc, token, ret;
token = rtas_function_token(RTAS_FN_IBM_SET_DYNAMIC_INDICATOR);
if (token == RTAS_UNKNOWN_SERVICE)
return -ENOENT;
mutex_lock(&rtas_ibm_set_dynamic_indicator_lock);
work_area = papr_dynamic_indice_buf_from_user(ubuf, &kbuf);
if (IS_ERR(work_area)) {
ret = PTR_ERR(work_area);
goto out;
}
do {
fwrc = rtas_call(token, 3, 1, NULL,
kbuf.dynamic_param.token,
kbuf.dynamic_param.state,
rtas_work_area_phys(work_area));
} while (rtas_busy_delay(fwrc));
rtas_work_area_free(work_area);
switch (fwrc) {
case RTAS_SUCCESS:
ret = 0;
break;
case RTAS_IBM_DYNAMIC_INDICE_NO_INDICATOR:
ret = -EOPNOTSUPP;
break;
default:
pr_err("unexpected ibm,set-dynamic-indicator result %d\n",
fwrc);
fallthrough;
case RTAS_HARDWARE_ERROR:
ret = -EIO;
break;
}
out:
mutex_unlock(&rtas_ibm_set_dynamic_indicator_lock);
return ret;
}
static long papr_dynamic_sensor_ioc_get(struct papr_indices_io_block __user *ubuf)
{
struct papr_indices_io_block kbuf;
struct rtas_work_area *work_area;
s32 fwrc, token, ret;
u32 rets;
token = rtas_function_token(RTAS_FN_IBM_GET_DYNAMIC_SENSOR_STATE);
if (token == RTAS_UNKNOWN_SERVICE)
return -ENOENT;
mutex_lock(&rtas_ibm_get_dynamic_sensor_state_lock);
work_area = papr_dynamic_indice_buf_from_user(ubuf, &kbuf);
if (IS_ERR(work_area)) {
ret = PTR_ERR(work_area);
goto out;
}
do {
fwrc = rtas_call(token, 2, 2, &rets,
kbuf.dynamic_param.token,
rtas_work_area_phys(work_area));
} while (rtas_busy_delay(fwrc));
rtas_work_area_free(work_area);
switch (fwrc) {
case RTAS_SUCCESS:
if (put_user(rets, &ubuf->dynamic_param.state))
ret = -EFAULT;
else
ret = 0;
break;
case RTAS_IBM_DYNAMIC_INDICE_NO_INDICATOR:
ret = -EOPNOTSUPP;
break;
default:
pr_err("unexpected ibm,get-dynamic-sensor result %d\n",
fwrc);
fallthrough;
case RTAS_HARDWARE_ERROR:
ret = -EIO;
break;
}
out:
mutex_unlock(&rtas_ibm_get_dynamic_sensor_state_lock);
return ret;
}
static long papr_indices_dev_ioctl(struct file *filp, unsigned int ioctl,
unsigned long arg)
{
void __user *argp = (__force void __user *)arg;
long ret;
switch (ioctl) {
case PAPR_INDICES_IOC_GET:
ret = papr_indices_create_handle(argp);
break;
case PAPR_DYNAMIC_SENSOR_IOC_GET:
ret = papr_dynamic_sensor_ioc_get(argp);
break;
case PAPR_DYNAMIC_INDICATOR_IOC_SET:
if (filp->f_mode & FMODE_WRITE)
ret = papr_dynamic_indicator_ioc_set(argp);
else
ret = -EBADF;
break;
default:
ret = -ENOIOCTLCMD;
break;
}
return ret;
}
static const struct file_operations papr_indices_ops = {
.unlocked_ioctl = papr_indices_dev_ioctl,
};
static struct miscdevice papr_indices_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "papr-indices",
.fops = &papr_indices_ops,
};
static __init int papr_indices_init(void)
{
if (!rtas_function_implemented(RTAS_FN_IBM_GET_INDICES))
return -ENODEV;
if (!rtas_function_implemented(RTAS_FN_IBM_SET_DYNAMIC_INDICATOR))
return -ENODEV;
if (!rtas_function_implemented(RTAS_FN_IBM_GET_DYNAMIC_SENSOR_STATE))
return -ENODEV;
return misc_register(&papr_indices_dev);
}
machine_device_initcall(pseries, papr_indices_init);