13e032c0eSJason Gunthorpe // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
23e032c0eSJason Gunthorpe /*
33e032c0eSJason Gunthorpe  * Copyright (c) 2019, Mellanox Technologies inc.  All rights reserved.
43e032c0eSJason Gunthorpe  */
53e032c0eSJason Gunthorpe 
63e032c0eSJason Gunthorpe #include <rdma/uverbs_std_types.h>
73e032c0eSJason Gunthorpe #include <rdma/uverbs_ioctl.h>
83e032c0eSJason Gunthorpe #include "rdma_core.h"
93e032c0eSJason Gunthorpe #include "uverbs.h"
103e032c0eSJason Gunthorpe 
UVERBS_HANDLER(UVERBS_METHOD_ASYNC_EVENT_ALLOC)11d680e88eSJason Gunthorpe static int UVERBS_HANDLER(UVERBS_METHOD_ASYNC_EVENT_ALLOC)(
12d680e88eSJason Gunthorpe 	struct uverbs_attr_bundle *attrs)
13d680e88eSJason Gunthorpe {
14d680e88eSJason Gunthorpe 	struct ib_uobject *uobj =
15d680e88eSJason Gunthorpe 		uverbs_attr_get_uobject(attrs, UVERBS_METHOD_ASYNC_EVENT_ALLOC);
16d680e88eSJason Gunthorpe 
17d680e88eSJason Gunthorpe 	ib_uverbs_init_async_event_file(
18d680e88eSJason Gunthorpe 		container_of(uobj, struct ib_uverbs_async_event_file, uobj));
19d680e88eSJason Gunthorpe 	return 0;
20d680e88eSJason Gunthorpe }
21d680e88eSJason Gunthorpe 
uverbs_async_event_destroy_uobj(struct ib_uobject * uobj,enum rdma_remove_reason why)22*c5633a72SLeon Romanovsky static void uverbs_async_event_destroy_uobj(struct ib_uobject *uobj,
233e032c0eSJason Gunthorpe 					    enum rdma_remove_reason why)
243e032c0eSJason Gunthorpe {
253e032c0eSJason Gunthorpe 	struct ib_uverbs_async_event_file *event_file =
263e032c0eSJason Gunthorpe 		container_of(uobj, struct ib_uverbs_async_event_file, uobj);
273e032c0eSJason Gunthorpe 
283e032c0eSJason Gunthorpe 	ib_unregister_event_handler(&event_file->event_handler);
29ccfdbaa5SJason Gunthorpe 
30ccfdbaa5SJason Gunthorpe 	if (why == RDMA_REMOVE_DRIVER_REMOVE)
31ccfdbaa5SJason Gunthorpe 		ib_uverbs_async_handler(event_file, 0, IB_EVENT_DEVICE_FATAL,
32ccfdbaa5SJason Gunthorpe 					NULL, NULL);
333e032c0eSJason Gunthorpe }
343e032c0eSJason Gunthorpe 
uverbs_async_event_release(struct inode * inode,struct file * filp)35c485b19dSJason Gunthorpe int uverbs_async_event_release(struct inode *inode, struct file *filp)
36c485b19dSJason Gunthorpe {
37c485b19dSJason Gunthorpe 	struct ib_uverbs_async_event_file *event_file;
38c485b19dSJason Gunthorpe 	struct ib_uobject *uobj = filp->private_data;
39c485b19dSJason Gunthorpe 	int ret;
40c485b19dSJason Gunthorpe 
41c485b19dSJason Gunthorpe 	if (!uobj)
42c485b19dSJason Gunthorpe 		return uverbs_uobject_fd_release(inode, filp);
43c485b19dSJason Gunthorpe 
44c485b19dSJason Gunthorpe 	event_file =
45c485b19dSJason Gunthorpe 		container_of(uobj, struct ib_uverbs_async_event_file, uobj);
46c485b19dSJason Gunthorpe 
47c485b19dSJason Gunthorpe 	/*
48c485b19dSJason Gunthorpe 	 * The async event FD has to deliver IB_EVENT_DEVICE_FATAL even after
49c485b19dSJason Gunthorpe 	 * disassociation, so cleaning the event list must only happen after
50c485b19dSJason Gunthorpe 	 * release. The user knows it has reached the end of the event stream
51c485b19dSJason Gunthorpe 	 * when it sees IB_EVENT_DEVICE_FATAL.
52c485b19dSJason Gunthorpe 	 */
53c485b19dSJason Gunthorpe 	uverbs_uobject_get(uobj);
54c485b19dSJason Gunthorpe 	ret = uverbs_uobject_fd_release(inode, filp);
55c485b19dSJason Gunthorpe 	ib_uverbs_free_event_queue(&event_file->ev_queue);
56c485b19dSJason Gunthorpe 	uverbs_uobject_put(uobj);
57c485b19dSJason Gunthorpe 	return ret;
58c485b19dSJason Gunthorpe }
59c485b19dSJason Gunthorpe 
60d680e88eSJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD(
61d680e88eSJason Gunthorpe 	UVERBS_METHOD_ASYNC_EVENT_ALLOC,
62d680e88eSJason Gunthorpe 	UVERBS_ATTR_FD(UVERBS_ATTR_ASYNC_EVENT_ALLOC_FD_HANDLE,
63d680e88eSJason Gunthorpe 		       UVERBS_OBJECT_ASYNC_EVENT,
64d680e88eSJason Gunthorpe 		       UVERBS_ACCESS_NEW,
65d680e88eSJason Gunthorpe 		       UA_MANDATORY));
66d680e88eSJason Gunthorpe 
673e032c0eSJason Gunthorpe DECLARE_UVERBS_NAMED_OBJECT(
683e032c0eSJason Gunthorpe 	UVERBS_OBJECT_ASYNC_EVENT,
693e032c0eSJason Gunthorpe 	UVERBS_TYPE_ALLOC_FD(sizeof(struct ib_uverbs_async_event_file),
703e032c0eSJason Gunthorpe 			     uverbs_async_event_destroy_uobj,
713e032c0eSJason Gunthorpe 			     &uverbs_async_event_fops,
723e032c0eSJason Gunthorpe 			     "[infinibandevent]",
73d680e88eSJason Gunthorpe 			     O_RDONLY),
74d680e88eSJason Gunthorpe 	&UVERBS_METHOD(UVERBS_METHOD_ASYNC_EVENT_ALLOC));
753e032c0eSJason Gunthorpe 
763e032c0eSJason Gunthorpe const struct uapi_definition uverbs_def_obj_async_fd[] = {
773e032c0eSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_ASYNC_EVENT),
783e032c0eSJason Gunthorpe 	{}
793e032c0eSJason Gunthorpe };
80