#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/xarray.h>
#include <net/genetlink.h>
#include <drm/drm_ras.h>
#include "drm_ras_nl.h"
static DEFINE_XARRAY_ALLOC(drm_ras_xa);
struct drm_ras_ctx {
unsigned long restart;
};
int drm_ras_nl_list_nodes_dumpit(struct sk_buff *skb,
struct netlink_callback *cb)
{
const struct genl_info *info = genl_info_dump(cb);
struct drm_ras_ctx *ctx = (void *)cb->ctx;
struct drm_ras_node *node;
struct nlattr *hdr;
unsigned long id;
int ret;
xa_for_each_start(&drm_ras_xa, id, node, ctx->restart) {
hdr = genlmsg_iput(skb, info);
if (!hdr) {
ret = -EMSGSIZE;
break;
}
ret = nla_put_u32(skb, DRM_RAS_A_NODE_ATTRS_NODE_ID, node->id);
if (ret) {
genlmsg_cancel(skb, hdr);
break;
}
ret = nla_put_string(skb, DRM_RAS_A_NODE_ATTRS_DEVICE_NAME,
node->device_name);
if (ret) {
genlmsg_cancel(skb, hdr);
break;
}
ret = nla_put_string(skb, DRM_RAS_A_NODE_ATTRS_NODE_NAME,
node->node_name);
if (ret) {
genlmsg_cancel(skb, hdr);
break;
}
ret = nla_put_u32(skb, DRM_RAS_A_NODE_ATTRS_NODE_TYPE,
node->type);
if (ret) {
genlmsg_cancel(skb, hdr);
break;
}
genlmsg_end(skb, hdr);
}
if (ret == -EMSGSIZE)
ctx->restart = id;
return ret;
}
static int get_node_error_counter(u32 node_id, u32 error_id,
const char **name, u32 *value)
{
struct drm_ras_node *node;
node = xa_load(&drm_ras_xa, node_id);
if (!node || !node->query_error_counter)
return -ENOENT;
if (error_id < node->error_counter_range.first ||
error_id > node->error_counter_range.last)
return -EINVAL;
return node->query_error_counter(node, error_id, name, value);
}
static int get_node_error_threshold(u32 node_id, u32 error_id, const char **name, u32 *threshold)
{
struct drm_ras_node *node;
node = xa_load(&drm_ras_xa, node_id);
if (!node)
return -ENOENT;
if (!node->query_error_threshold)
return -EOPNOTSUPP;
if (error_id < node->error_counter_range.first || error_id > node->error_counter_range.last)
return -EINVAL;
return node->query_error_threshold(node, error_id, name, threshold);
}
static int set_node_error_threshold(u32 node_id, u32 error_id, u32 threshold)
{
struct drm_ras_node *node;
node = xa_load(&drm_ras_xa, node_id);
if (!node)
return -ENOENT;
if (!node->set_error_threshold)
return -EOPNOTSUPP;
if (error_id < node->error_counter_range.first || error_id > node->error_counter_range.last)
return -EINVAL;
return node->set_error_threshold(node, error_id, threshold);
}
static int msg_reply_value(struct sk_buff *msg, u32 error_id,
const char *error_name, u32 value)
{
int ret;
ret = nla_put_u32(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID, error_id);
if (ret)
return ret;
ret = nla_put_string(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_NAME,
error_name);
if (ret)
return ret;
return nla_put_u32(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_VALUE,
value);
}
static int msg_put_error_event_attrs(struct sk_buff *msg, struct drm_ras_node *node,
u32 error_id, const char *error_name, u32 value)
{
int ret;
ret = nla_put_string(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_DEVICE_NAME, node->device_name);
if (ret)
return ret;
ret = nla_put_u32(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_NODE_ID, node->id);
if (ret)
return ret;
ret = nla_put_string(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_NODE_NAME, node->node_name);
if (ret)
return ret;
ret = nla_put_u32(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_ERROR_ID, error_id);
if (ret)
return ret;
ret = nla_put_string(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_ERROR_NAME, error_name);
if (ret)
return ret;
return nla_put_u32(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_ERROR_VALUE, value);
}
static int msg_reply_threshold(struct sk_buff *msg, u32 error_id, const char *error_name,
u32 threshold)
{
int ret;
ret = nla_put_u32(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID, error_id);
if (ret)
return ret;
ret = nla_put_string(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_NAME, error_name);
if (ret)
return ret;
return nla_put_u32(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_THRESHOLD, threshold);
}
static int doit_reply_value(struct genl_info *info, u32 node_id,
u32 error_id)
{
struct sk_buff *msg;
struct nlattr *hdr;
const char *error_name;
u32 value;
int ret;
msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
hdr = genlmsg_iput(msg, info);
if (!hdr) {
ret = -EMSGSIZE;
goto free_msg;
}
ret = get_node_error_counter(node_id, error_id,
&error_name, &value);
if (ret)
goto cancel_msg;
ret = msg_reply_value(msg, error_id, error_name, value);
if (ret)
goto cancel_msg;
genlmsg_end(msg, hdr);
return genlmsg_reply(msg, info);
cancel_msg:
genlmsg_cancel(msg, hdr);
free_msg:
nlmsg_free(msg);
return ret;
}
static int doit_reply_threshold(struct genl_info *info, u32 node_id, u32 error_id)
{
const char *error_name;
struct sk_buff *msg;
struct nlattr *hdr;
u32 threshold;
int ret;
msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
hdr = genlmsg_iput(msg, info);
if (!hdr) {
ret = -EMSGSIZE;
goto free_msg;
}
ret = get_node_error_threshold(node_id, error_id, &error_name, &threshold);
if (ret)
goto cancel_msg;
ret = msg_reply_threshold(msg, error_id, error_name, threshold);
if (ret)
goto cancel_msg;
genlmsg_end(msg, hdr);
return genlmsg_reply(msg, info);
cancel_msg:
genlmsg_cancel(msg, hdr);
free_msg:
nlmsg_free(msg);
return ret;
}
int drm_ras_nl_error_event(struct drm_ras_node *node, u32 error_id, const char *error_name,
u32 value)
{
struct genl_info info;
struct sk_buff *msg;
struct nlattr *hdr;
int ret;
if (!node || !error_name)
return -EINVAL;
if (xa_load(&drm_ras_xa, node->id) != node)
return -ENOENT;
if (node->type != DRM_RAS_NODE_TYPE_ERROR_COUNTER)
return -EOPNOTSUPP;
if (error_id < node->error_counter_range.first ||
error_id > node->error_counter_range.last)
return -EINVAL;
genl_info_init_ntf(&info, &drm_ras_nl_family, DRM_RAS_CMD_ERROR_EVENT);
msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
hdr = genlmsg_iput(msg, &info);
if (!hdr) {
ret = -EMSGSIZE;
goto free_msg;
}
ret = msg_put_error_event_attrs(msg, node, error_id, error_name, value);
if (ret)
goto cancel_msg;
genlmsg_end(msg, hdr);
genlmsg_multicast_allns(&drm_ras_nl_family, msg, 0, DRM_RAS_NLGRP_ERROR_REPORT);
return 0;
cancel_msg:
genlmsg_cancel(msg, hdr);
free_msg:
nlmsg_free(msg);
return ret;
}
EXPORT_SYMBOL(drm_ras_nl_error_event);
int drm_ras_nl_get_error_counter_dumpit(struct sk_buff *skb,
struct netlink_callback *cb)
{
const struct genl_info *info = genl_info_dump(cb);
struct drm_ras_ctx *ctx = (void *)cb->ctx;
struct drm_ras_node *node;
struct nlattr *hdr;
const char *error_name;
u32 node_id, error_id, value;
int ret;
if (!info->attrs || GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID))
return -EINVAL;
node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
node = xa_load(&drm_ras_xa, node_id);
if (!node)
return -ENOENT;
for (error_id = max(node->error_counter_range.first, ctx->restart);
error_id <= node->error_counter_range.last;
error_id++) {
ret = get_node_error_counter(node_id, error_id,
&error_name, &value);
if (ret == -ENOENT)
continue;
if (ret)
return ret;
hdr = genlmsg_iput(skb, info);
if (!hdr) {
ret = -EMSGSIZE;
break;
}
ret = msg_reply_value(skb, error_id, error_name, value);
if (ret) {
genlmsg_cancel(skb, hdr);
break;
}
genlmsg_end(skb, hdr);
}
if (ret == -EMSGSIZE)
ctx->restart = error_id;
return ret;
}
int drm_ras_nl_get_error_counter_doit(struct sk_buff *skb,
struct genl_info *info)
{
u32 node_id, error_id;
if (!info->attrs ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID) ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID))
return -EINVAL;
node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
error_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID]);
return doit_reply_value(info, node_id, error_id);
}
int drm_ras_nl_clear_error_counter_doit(struct sk_buff *skb,
struct genl_info *info)
{
struct drm_ras_node *node;
u32 node_id, error_id;
if (!info->attrs ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID) ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID))
return -EINVAL;
node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
error_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID]);
node = xa_load(&drm_ras_xa, node_id);
if (!node || !node->clear_error_counter)
return -ENOENT;
if (error_id < node->error_counter_range.first ||
error_id > node->error_counter_range.last)
return -EINVAL;
return node->clear_error_counter(node, error_id);
}
int drm_ras_nl_get_error_threshold_doit(struct sk_buff *skb, struct genl_info *info)
{
u32 node_id, error_id;
if (!info->attrs ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID) ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID))
return -EINVAL;
node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
error_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID]);
return doit_reply_threshold(info, node_id, error_id);
}
int drm_ras_nl_set_error_threshold_doit(struct sk_buff *skb, struct genl_info *info)
{
u32 node_id, error_id, threshold;
if (!info->attrs ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID) ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID) ||
GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_THRESHOLD))
return -EINVAL;
node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
error_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID]);
threshold = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_THRESHOLD]);
return set_node_error_threshold(node_id, error_id, threshold);
}
int drm_ras_node_register(struct drm_ras_node *node)
{
if (!node->device_name || !node->node_name)
return -EINVAL;
if (node->type != DRM_RAS_NODE_TYPE_ERROR_COUNTER)
return -EINVAL;
if (node->type == DRM_RAS_NODE_TYPE_ERROR_COUNTER &&
(!node->error_counter_range.last || !node->query_error_counter))
return -EINVAL;
return xa_alloc(&drm_ras_xa, &node->id, node, xa_limit_32b, GFP_KERNEL);
}
EXPORT_SYMBOL(drm_ras_node_register);
void drm_ras_node_unregister(struct drm_ras_node *node)
{
xa_erase(&drm_ras_xa, node->id);
}
EXPORT_SYMBOL