uverbs_std_types_dm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include "rdma_core.h"
  33. #include "uverbs.h"
  34. #include <rdma/uverbs_std_types.h>
  35. static int uverbs_free_dm(struct ib_uobject *uobject,
  36. enum rdma_remove_reason why,
  37. struct uverbs_attr_bundle *attrs)
  38. {
  39. struct ib_dm *dm = uobject->object;
  40. int ret;
  41. ret = ib_destroy_usecnt(&dm->usecnt, why, uobject);
  42. if (ret)
  43. return ret;
  44. return dm->device->ops.dealloc_dm(dm, attrs);
  45. }
  46. static int UVERBS_HANDLER(UVERBS_METHOD_DM_ALLOC)(
  47. struct uverbs_attr_bundle *attrs)
  48. {
  49. struct ib_dm_alloc_attr attr = {};
  50. struct ib_uobject *uobj =
  51. uverbs_attr_get(attrs, UVERBS_ATTR_ALLOC_DM_HANDLE)
  52. ->obj_attr.uobject;
  53. struct ib_device *ib_dev = attrs->context->device;
  54. struct ib_dm *dm;
  55. int ret;
  56. if (!ib_dev->ops.alloc_dm)
  57. return -EOPNOTSUPP;
  58. ret = uverbs_copy_from(&attr.length, attrs,
  59. UVERBS_ATTR_ALLOC_DM_LENGTH);
  60. if (ret)
  61. return ret;
  62. ret = uverbs_copy_from(&attr.alignment, attrs,
  63. UVERBS_ATTR_ALLOC_DM_ALIGNMENT);
  64. if (ret)
  65. return ret;
  66. dm = ib_dev->ops.alloc_dm(ib_dev, attrs->context, &attr, attrs);
  67. if (IS_ERR(dm))
  68. return PTR_ERR(dm);
  69. dm->device = ib_dev;
  70. dm->length = attr.length;
  71. dm->uobject = uobj;
  72. atomic_set(&dm->usecnt, 0);
  73. uobj->object = dm;
  74. return 0;
  75. }
  76. DECLARE_UVERBS_NAMED_METHOD(
  77. UVERBS_METHOD_DM_ALLOC,
  78. UVERBS_ATTR_IDR(UVERBS_ATTR_ALLOC_DM_HANDLE,
  79. UVERBS_OBJECT_DM,
  80. UVERBS_ACCESS_NEW,
  81. UA_MANDATORY),
  82. UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_LENGTH,
  83. UVERBS_ATTR_TYPE(u64),
  84. UA_MANDATORY),
  85. UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_ALIGNMENT,
  86. UVERBS_ATTR_TYPE(u32),
  87. UA_MANDATORY));
  88. DECLARE_UVERBS_NAMED_METHOD_DESTROY(
  89. UVERBS_METHOD_DM_FREE,
  90. UVERBS_ATTR_IDR(UVERBS_ATTR_FREE_DM_HANDLE,
  91. UVERBS_OBJECT_DM,
  92. UVERBS_ACCESS_DESTROY,
  93. UA_MANDATORY));
  94. DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_DM,
  95. UVERBS_TYPE_ALLOC_IDR(uverbs_free_dm),
  96. &UVERBS_METHOD(UVERBS_METHOD_DM_ALLOC),
  97. &UVERBS_METHOD(UVERBS_METHOD_DM_FREE));
  98. const struct uapi_definition uverbs_def_obj_dm[] = {
  99. UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_DM,
  100. UAPI_DEF_OBJ_NEEDS_FN(dealloc_dm)),
  101. {}
  102. };