uverbs_std_types_counters.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include "rdma_core.h"
  34. #include "uverbs.h"
  35. #include <rdma/uverbs_std_types.h>
  36. static int uverbs_free_counters(struct ib_uobject *uobject,
  37. enum rdma_remove_reason why,
  38. struct uverbs_attr_bundle *attrs)
  39. {
  40. struct ib_counters *counters = uobject->object;
  41. int ret;
  42. ret = ib_destroy_usecnt(&counters->usecnt, why, uobject);
  43. if (ret)
  44. return ret;
  45. ret = counters->device->ops.destroy_counters(counters);
  46. if (ret)
  47. return ret;
  48. kfree(counters);
  49. return 0;
  50. }
  51. static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_CREATE)(
  52. struct uverbs_attr_bundle *attrs)
  53. {
  54. struct ib_uobject *uobj = uverbs_attr_get_uobject(
  55. attrs, UVERBS_ATTR_CREATE_COUNTERS_HANDLE);
  56. struct ib_device *ib_dev = attrs->context->device;
  57. struct ib_counters *counters;
  58. int ret;
  59. /*
  60. * This check should be removed once the infrastructure
  61. * have the ability to remove methods from parse tree once
  62. * such condition is met.
  63. */
  64. if (!ib_dev->ops.create_counters)
  65. return -EOPNOTSUPP;
  66. counters = rdma_zalloc_drv_obj(ib_dev, ib_counters);
  67. if (!counters)
  68. return -ENOMEM;
  69. counters->device = ib_dev;
  70. counters->uobject = uobj;
  71. uobj->object = counters;
  72. atomic_set(&counters->usecnt, 0);
  73. ret = ib_dev->ops.create_counters(counters, attrs);
  74. if (ret)
  75. kfree(counters);
  76. return ret;
  77. }
  78. static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_READ)(
  79. struct uverbs_attr_bundle *attrs)
  80. {
  81. struct ib_counters_read_attr read_attr = {};
  82. const struct uverbs_attr *uattr;
  83. struct ib_counters *counters =
  84. uverbs_attr_get_obj(attrs, UVERBS_ATTR_READ_COUNTERS_HANDLE);
  85. int ret;
  86. if (!counters->device->ops.read_counters)
  87. return -EOPNOTSUPP;
  88. if (!atomic_read(&counters->usecnt))
  89. return -EINVAL;
  90. ret = uverbs_get_flags32(&read_attr.flags, attrs,
  91. UVERBS_ATTR_READ_COUNTERS_FLAGS,
  92. IB_UVERBS_READ_COUNTERS_PREFER_CACHED);
  93. if (ret)
  94. return ret;
  95. uattr = uverbs_attr_get(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF);
  96. read_attr.ncounters = uattr->ptr_attr.len / sizeof(u64);
  97. read_attr.counters_buff = uverbs_zalloc(
  98. attrs, array_size(read_attr.ncounters, sizeof(u64)));
  99. if (IS_ERR(read_attr.counters_buff))
  100. return PTR_ERR(read_attr.counters_buff);
  101. ret = counters->device->ops.read_counters(counters, &read_attr, attrs);
  102. if (ret)
  103. return ret;
  104. return uverbs_copy_to(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF,
  105. read_attr.counters_buff,
  106. read_attr.ncounters * sizeof(u64));
  107. }
  108. DECLARE_UVERBS_NAMED_METHOD(
  109. UVERBS_METHOD_COUNTERS_CREATE,
  110. UVERBS_ATTR_IDR(UVERBS_ATTR_CREATE_COUNTERS_HANDLE,
  111. UVERBS_OBJECT_COUNTERS,
  112. UVERBS_ACCESS_NEW,
  113. UA_MANDATORY));
  114. DECLARE_UVERBS_NAMED_METHOD_DESTROY(
  115. UVERBS_METHOD_COUNTERS_DESTROY,
  116. UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_COUNTERS_HANDLE,
  117. UVERBS_OBJECT_COUNTERS,
  118. UVERBS_ACCESS_DESTROY,
  119. UA_MANDATORY));
  120. DECLARE_UVERBS_NAMED_METHOD(
  121. UVERBS_METHOD_COUNTERS_READ,
  122. UVERBS_ATTR_IDR(UVERBS_ATTR_READ_COUNTERS_HANDLE,
  123. UVERBS_OBJECT_COUNTERS,
  124. UVERBS_ACCESS_READ,
  125. UA_MANDATORY),
  126. UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_READ_COUNTERS_BUFF,
  127. UVERBS_ATTR_MIN_SIZE(0),
  128. UA_MANDATORY),
  129. UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_READ_COUNTERS_FLAGS,
  130. enum ib_uverbs_read_counters_flags));
  131. DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_COUNTERS,
  132. UVERBS_TYPE_ALLOC_IDR(uverbs_free_counters),
  133. &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_CREATE),
  134. &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_DESTROY),
  135. &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_READ));
  136. const struct uapi_definition uverbs_def_obj_counters[] = {
  137. UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_COUNTERS,
  138. UAPI_DEF_OBJ_NEEDS_FN(destroy_counters)),
  139. {}
  140. };