/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
/*
* Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
*/
#ifndef _UVERBS_IOCTL_
#define _UVERBS_IOCTL_
#include <rdma/uverbs_types.h>
#include <linux/uaccess.h>
#include <rdma/rdma_user_ioctl.h>
#include <rdma/ib_user_ioctl_verbs.h>
#include <rdma/ib_user_ioctl_cmds.h>
/*
* =======================================
* Verbs action specifications
* =======================================
*/
enum uverbs_attr_type {
UVERBS_ATTR_TYPE_NA,
UVERBS_ATTR_TYPE_PTR_IN,
UVERBS_ATTR_TYPE_PTR_OUT,
UVERBS_ATTR_TYPE_IDR,
UVERBS_ATTR_TYPE_FD,
UVERBS_ATTR_TYPE_RAW_FD,
UVERBS_ATTR_TYPE_ENUM_IN,
UVERBS_ATTR_TYPE_IDRS_ARRAY,
};
enum uverbs_obj_access {
UVERBS_ACCESS_READ,
UVERBS_ACCESS_WRITE,
UVERBS_ACCESS_NEW,
UVERBS_ACCESS_DESTROY
};
/* Specification of a single attribute inside the ioctl message */
/* good size 16 */
struct uverbs_attr_spec {
u8 type;
/*
* Support extending attributes by length. Allow the user to provide
* more bytes than ptr.len, but check that everything after is zero'd
* by the user.
*/
u8 zero_trailing:1;
/*
* Valid only for PTR_IN. Allocate and copy the data inside
* the parser
*/
u8 alloc_and_copy:1;
u8 mandatory:1;
/* True if this is from UVERBS_ATTR_UHW */
u8 is_udata:1;
union {
struct {
/* Current known size to kernel */
u16 len;
/* User isn't allowed to provide something < min_len */
u16 min_len;
} ptr;
struct {
/*
* higher bits mean the namespace and lower bits mean
* the type id within the namespace.
*/
u16 obj_type;
u8 access;
} obj;
struct {
u8 num_elems;
} enum_def;
} u;
/* This weird split lets us remove some padding */
union {
struct {
/*
* The enum attribute can select one of the attributes
* contained in the ids array. Currently only PTR_IN
* attributes are supported in the ids array.
*/
const struct uverbs_attr_spec *ids;
} enum_def;
struct {
/*
* higher bits mean the namespace and lower bits mean
* the type id within the namespace.
*/
u16 obj_type;
u16 min_len;
u16 max_len;
u8 access;
} objs_arr;
} u2;
};
/*
* Information about the API is loaded into a radix tree. For IOCTL we start
* with a tuple of:
* object_id, attr_id, method_id
*
* Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
* on the current kernel support this is compressed into 16 bit key for the
* radix tree. Since this compression is entirely internal to the kernel the
* below limits can be revised if the kernel gains additional data.
*
* With 64 leafs per node this is a 3 level radix tree.
*
* The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
* the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
*
* This also encodes the tables for the write() and write() extended commands
* using the coding
* OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
* OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
* ie the WRITE path is treated as a special method type in the ioctl
* framework.
*/
enum uapi_radix_data {
UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
UVERBS_API_ATTR_KEY_BITS = 6,
UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
UVERBS_API_METHOD_KEY_BITS = 5,
UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
UVERBS_API_METHOD_KEY_NUM_CORE = 22,
UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
UVERBS_API_METHOD_KEY_NUM_DRIVER =
(UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
UVERBS_API_METHOD_KEY_NUM_CORE,
UVERBS_API_METHOD_KEY_MASK = GENMASK(
UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
UVERBS_API_METHOD_KEY_SHIFT),
UVERBS_API_OBJ_KEY_BITS = 5,
UVERBS_API_OBJ_KEY_SHIFT =
UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
UVERBS_API_OBJ_KEY_NUM_CORE = 20,
UVERBS_API_OBJ_KEY_NUM_DRIVER =
(1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
/* This id guaranteed to not exist in the radix tree */
UVERBS_API_KEY_ERR = 0xFFFFFFFF,
};
static inline __attribute_const__ u32 uapi_key_obj(u32 id)
{
if (id & UVERBS_API_NS_FLAG) {
id &= ~UVERBS_API_NS_FLAG;
if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
return UVERBS_API_KEY_ERR;
id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
} else {
if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
return UVERBS_API_KEY_ERR;
}
return id << UVERBS_API_OBJ_KEY_SHIFT;
}
static inline __attribute_const__ bool uapi_key_is_object(u32 key)
{
return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
}
static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
{
if (id & UVERBS_API_NS_FLAG) {
id &= ~UVERBS_API_NS_FLAG;
if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
return UVERBS_API_KEY_ERR;
id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
} else {
id++;
if