uverbs_std_types_async_fd.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2019, Mellanox Technologies inc. All rights reserved.
  4. */
  5. #include <rdma/uverbs_std_types.h>
  6. #include <rdma/uverbs_ioctl.h>
  7. #include "rdma_core.h"
  8. #include "uverbs.h"
  9. static int UVERBS_HANDLER(UVERBS_METHOD_ASYNC_EVENT_ALLOC)(
  10. struct uverbs_attr_bundle *attrs)
  11. {
  12. struct ib_uobject *uobj =
  13. uverbs_attr_get_uobject(attrs, UVERBS_METHOD_ASYNC_EVENT_ALLOC);
  14. ib_uverbs_init_async_event_file(
  15. container_of(uobj, struct ib_uverbs_async_event_file, uobj));
  16. return 0;
  17. }
  18. static int uverbs_async_event_destroy_uobj(struct ib_uobject *uobj,
  19. enum rdma_remove_reason why)
  20. {
  21. struct ib_uverbs_async_event_file *event_file =
  22. container_of(uobj, struct ib_uverbs_async_event_file, uobj);
  23. ib_unregister_event_handler(&event_file->event_handler);
  24. if (why == RDMA_REMOVE_DRIVER_REMOVE)
  25. ib_uverbs_async_handler(event_file, 0, IB_EVENT_DEVICE_FATAL,
  26. NULL, NULL);
  27. return 0;
  28. }
  29. int uverbs_async_event_release(struct inode *inode, struct file *filp)
  30. {
  31. struct ib_uverbs_async_event_file *event_file;
  32. struct ib_uobject *uobj = filp->private_data;
  33. int ret;
  34. if (!uobj)
  35. return uverbs_uobject_fd_release(inode, filp);
  36. event_file =
  37. container_of(uobj, struct ib_uverbs_async_event_file, uobj);
  38. /*
  39. * The async event FD has to deliver IB_EVENT_DEVICE_FATAL even after
  40. * disassociation, so cleaning the event list must only happen after
  41. * release. The user knows it has reached the end of the event stream
  42. * when it sees IB_EVENT_DEVICE_FATAL.
  43. */
  44. uverbs_uobject_get(uobj);
  45. ret = uverbs_uobject_fd_release(inode, filp);
  46. ib_uverbs_free_event_queue(&event_file->ev_queue);
  47. uverbs_uobject_put(uobj);
  48. return ret;
  49. }
  50. DECLARE_UVERBS_NAMED_METHOD(
  51. UVERBS_METHOD_ASYNC_EVENT_ALLOC,
  52. UVERBS_ATTR_FD(UVERBS_ATTR_ASYNC_EVENT_ALLOC_FD_HANDLE,
  53. UVERBS_OBJECT_ASYNC_EVENT,
  54. UVERBS_ACCESS_NEW,
  55. UA_MANDATORY));
  56. DECLARE_UVERBS_NAMED_OBJECT(
  57. UVERBS_OBJECT_ASYNC_EVENT,
  58. UVERBS_TYPE_ALLOC_FD(sizeof(struct ib_uverbs_async_event_file),
  59. uverbs_async_event_destroy_uobj,
  60. &uverbs_async_event_fops,
  61. "[infinibandevent]",
  62. O_RDONLY),
  63. &UVERBS_METHOD(UVERBS_METHOD_ASYNC_EVENT_ALLOC));
  64. const struct uapi_definition uverbs_def_obj_async_fd[] = {
  65. UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_ASYNC_EVENT),
  66. {}
  67. };