uverbs_named_ioctl.h 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
  4. */
  5. #ifndef _UVERBS_NAMED_IOCTL_
  6. #define _UVERBS_NAMED_IOCTL_
  7. #include <rdma/uverbs_ioctl.h>
  8. #ifndef UVERBS_MODULE_NAME
  9. #error "Please #define UVERBS_MODULE_NAME before including rdma/uverbs_named_ioctl.h"
  10. #endif
  11. #define _UVERBS_PASTE(x, y) x ## y
  12. #define _UVERBS_NAME(x, y) _UVERBS_PASTE(x, y)
  13. #define UVERBS_METHOD(id) _UVERBS_NAME(UVERBS_MODULE_NAME, _method_##id)
  14. #define UVERBS_HANDLER(id) _UVERBS_NAME(UVERBS_MODULE_NAME, _handler_##id)
  15. #define UVERBS_OBJECT(id) _UVERBS_NAME(UVERBS_MODULE_NAME, _object_##id)
  16. /* These are static so they do not need to be qualified */
  17. #define UVERBS_METHOD_ATTRS(method_id) _method_attrs_##method_id
  18. #define UVERBS_OBJECT_METHODS(object_id) _UVERBS_NAME(_object_methods_##object_id, __LINE__)
  19. #define DECLARE_UVERBS_NAMED_METHOD(_method_id, ...) \
  20. static const struct uverbs_attr_def *const UVERBS_METHOD_ATTRS( \
  21. _method_id)[] = { __VA_ARGS__ }; \
  22. static const struct uverbs_method_def UVERBS_METHOD(_method_id) = { \
  23. .id = _method_id, \
  24. .handler = UVERBS_HANDLER(_method_id), \
  25. .num_attrs = ARRAY_SIZE(UVERBS_METHOD_ATTRS(_method_id)), \
  26. .attrs = &UVERBS_METHOD_ATTRS(_method_id), \
  27. }
  28. /* Create a standard destroy method using the default handler. The handle_attr
  29. * argument must be the attribute specifying the handle to destroy, the
  30. * default handler does not support any other attributes.
  31. */
  32. #define DECLARE_UVERBS_NAMED_METHOD_DESTROY(_method_id, _handle_attr) \
  33. static const struct uverbs_attr_def *const UVERBS_METHOD_ATTRS( \
  34. _method_id)[] = { _handle_attr }; \
  35. static const struct uverbs_method_def UVERBS_METHOD(_method_id) = { \
  36. .id = _method_id, \
  37. .handler = uverbs_destroy_def_handler, \
  38. .num_attrs = ARRAY_SIZE(UVERBS_METHOD_ATTRS(_method_id)), \
  39. .attrs = &UVERBS_METHOD_ATTRS(_method_id), \
  40. }
  41. #define DECLARE_UVERBS_NAMED_OBJECT(_object_id, _type_attrs, ...) \
  42. static const struct uverbs_method_def *const UVERBS_OBJECT_METHODS( \
  43. _object_id)[] = { __VA_ARGS__ }; \
  44. static const struct uverbs_object_def UVERBS_OBJECT(_object_id) = { \
  45. .id = _object_id, \
  46. .type_attrs = &_type_attrs, \
  47. .num_methods = ARRAY_SIZE(UVERBS_OBJECT_METHODS(_object_id)), \
  48. .methods = &UVERBS_OBJECT_METHODS(_object_id) \
  49. }
  50. /*
  51. * Declare global methods. These still have a unique object_id because we
  52. * identify all uapi methods with a (object,method) tuple. However, they have
  53. * no type pointer.
  54. */
  55. #define DECLARE_UVERBS_GLOBAL_METHODS(_object_id, ...) \
  56. static const struct uverbs_method_def *const UVERBS_OBJECT_METHODS( \
  57. _object_id)[] = { __VA_ARGS__ }; \
  58. static const struct uverbs_object_def UVERBS_OBJECT(_object_id) = { \
  59. .id = _object_id, \
  60. .num_methods = ARRAY_SIZE(UVERBS_OBJECT_METHODS(_object_id)), \
  61. .methods = &UVERBS_OBJECT_METHODS(_object_id) \
  62. }
  63. /* Used by drivers to declare a complete parsing tree for new methods
  64. */
  65. #define ADD_UVERBS_METHODS(_name, _object_id, ...) \
  66. static const struct uverbs_method_def *const UVERBS_OBJECT_METHODS( \
  67. _object_id)[] = { __VA_ARGS__ }; \
  68. static const struct uverbs_object_def _name = { \
  69. .id = _object_id, \
  70. .num_methods = ARRAY_SIZE(UVERBS_OBJECT_METHODS(_object_id)), \
  71. .methods = &UVERBS_OBJECT_METHODS(_object_id) \
  72. };
  73. /* Used by drivers to declare a complete parsing tree for a single method that
  74. * differs only in having additional driver specific attributes.
  75. */
  76. #define ADD_UVERBS_ATTRIBUTES_SIMPLE(_name, _object_id, _method_id, ...) \
  77. static const struct uverbs_attr_def *const UVERBS_METHOD_ATTRS( \
  78. _method_id)[] = { __VA_ARGS__ }; \
  79. static const struct uverbs_method_def UVERBS_METHOD(_method_id) = { \
  80. .id = _method_id, \
  81. .num_attrs = ARRAY_SIZE(UVERBS_METHOD_ATTRS(_method_id)), \
  82. .attrs = &UVERBS_METHOD_ATTRS(_method_id), \
  83. }; \
  84. ADD_UVERBS_METHODS(_name, _object_id, &UVERBS_METHOD(_method_id))
  85. #endif