1219820Sjeff/*
2219820Sjeff * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3219820Sjeff * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
4219820Sjeff * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5219820Sjeff * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
6219820Sjeff * Copyright (c) 2005 PathScale, Inc. All rights reserved.
7219820Sjeff *
8219820Sjeff * This software is available to you under a choice of one of two
9219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
10219820Sjeff * General Public License (GPL) Version 2, available from the file
11219820Sjeff * COPYING in the main directory of this source tree, or the
12219820Sjeff * OpenIB.org BSD license below:
13219820Sjeff *
14219820Sjeff *     Redistribution and use in source and binary forms, with or
15219820Sjeff *     without modification, are permitted provided that the following
16219820Sjeff *     conditions are met:
17219820Sjeff *
18219820Sjeff *      - Redistributions of source code must retain the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer.
21219820Sjeff *
22219820Sjeff *      - Redistributions in binary form must reproduce the above
23219820Sjeff *        copyright notice, this list of conditions and the following
24219820Sjeff *        disclaimer in the documentation and/or other materials
25219820Sjeff *        provided with the distribution.
26219820Sjeff *
27219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34219820Sjeff * SOFTWARE.
35219820Sjeff */
36219820Sjeff
37219820Sjeff#ifndef UVERBS_H
38219820Sjeff#define UVERBS_H
39219820Sjeff
40219820Sjeff#include <linux/kref.h>
41219820Sjeff#include <linux/idr.h>
42219820Sjeff#include <linux/mutex.h>
43219820Sjeff#include <linux/completion.h>
44219820Sjeff
45219820Sjeff#include <rdma/ib_verbs.h>
46219820Sjeff#include <rdma/ib_umem.h>
47219820Sjeff#include <rdma/ib_user_verbs.h>
48219820Sjeff
49219820Sjeff/*
50219820Sjeff * Our lifetime rules for these structs are the following:
51219820Sjeff *
52219820Sjeff * struct ib_uverbs_device: One reference is held by the module and
53219820Sjeff * released in ib_uverbs_remove_one().  Another reference is taken by
54219820Sjeff * ib_uverbs_open() each time the character special file is opened,
55219820Sjeff * and released in ib_uverbs_release_file() when the file is released.
56219820Sjeff *
57219820Sjeff * struct ib_uverbs_file: One reference is held by the VFS and
58219820Sjeff * released when the file is closed.  Another reference is taken when
59219820Sjeff * an asynchronous event queue file is created and released when the
60219820Sjeff * event file is closed.
61219820Sjeff *
62219820Sjeff * struct ib_uverbs_event_file: One reference is held by the VFS and
63219820Sjeff * released when the file is closed.  For asynchronous event files,
64219820Sjeff * another reference is held by the corresponding main context file
65219820Sjeff * and released when that file is closed.  For completion event files,
66219820Sjeff * a reference is taken when a CQ is created that uses the file, and
67219820Sjeff * released when the CQ is destroyed.
68219820Sjeff */
69219820Sjeff
70219820Sjeffstruct ib_uverbs_device {
71219820Sjeff	struct kref				ref;
72219820Sjeff	struct completion			comp;
73219820Sjeff	int					devnum;
74219820Sjeff	struct cdev			       *cdev;
75219820Sjeff	struct device			       *dev;
76219820Sjeff	struct ib_device		       *ib_dev;
77219820Sjeff	int					num_comp_vectors;
78219820Sjeff};
79219820Sjeff
80219820Sjeffstruct ib_uverbs_event_file {
81219820Sjeff	struct kref				ref;
82219820Sjeff	struct file			       *filp;
83219820Sjeff	struct ib_uverbs_file		       *uverbs_file;
84219820Sjeff	spinlock_t				lock;
85219820Sjeff	wait_queue_head_t			poll_wait;
86219820Sjeff	struct fasync_struct		       *async_queue;
87219820Sjeff	struct list_head			event_list;
88219820Sjeff	int					is_async;
89219820Sjeff	int					is_closed;
90219820Sjeff};
91219820Sjeff
92219820Sjeffstruct ib_uverbs_file {
93219820Sjeff	struct kref				ref;
94219820Sjeff	struct mutex				mutex;
95219820Sjeff	struct ib_uverbs_device		       *device;
96219820Sjeff	struct ib_ucontext		       *ucontext;
97219820Sjeff	struct ib_event_handler			event_handler;
98219820Sjeff	struct ib_uverbs_event_file	       *async_file;
99219820Sjeff};
100219820Sjeff
101219820Sjeffstruct ib_uverbs_event {
102219820Sjeff	union {
103219820Sjeff		struct ib_uverbs_async_event_desc	async;
104219820Sjeff		struct ib_uverbs_comp_event_desc	comp;
105219820Sjeff	}					desc;
106219820Sjeff	struct list_head			list;
107219820Sjeff	struct list_head			obj_list;
108219820Sjeff	u32				       *counter;
109219820Sjeff};
110219820Sjeff
111219820Sjeffstruct ib_uverbs_mcast_entry {
112219820Sjeff	struct list_head	list;
113219820Sjeff	union ib_gid 		gid;
114219820Sjeff	u16 			lid;
115219820Sjeff};
116219820Sjeff
117219820Sjeffstruct ib_uevent_object {
118219820Sjeff	struct ib_uobject	uobject;
119219820Sjeff	struct list_head	event_list;
120219820Sjeff	u32			events_reported;
121219820Sjeff};
122219820Sjeff
123219820Sjeffstruct ib_uqp_object {
124219820Sjeff	struct ib_uevent_object	uevent;
125219820Sjeff	struct list_head 	mcast_list;
126219820Sjeff};
127219820Sjeff
128219820Sjeffstruct ib_ucq_object {
129219820Sjeff	struct ib_uobject	uobject;
130219820Sjeff	struct ib_uverbs_file  *uverbs_file;
131219820Sjeff	struct list_head	comp_list;
132219820Sjeff	struct list_head	async_list;
133219820Sjeff	u32			comp_events_reported;
134219820Sjeff	u32			async_events_reported;
135219820Sjeff};
136219820Sjeff
137219820Sjeffstruct ib_uxrcd_object {
138219820Sjeff	struct ib_uobject	uobject;
139219820Sjeff	struct list_head	xrc_reg_qp_list;
140219820Sjeff};
141219820Sjeff
142219820Sjeffextern spinlock_t ib_uverbs_idr_lock;
143219820Sjeffextern struct idr ib_uverbs_pd_idr;
144219820Sjeffextern struct idr ib_uverbs_mr_idr;
145219820Sjeffextern struct idr ib_uverbs_mw_idr;
146219820Sjeffextern struct idr ib_uverbs_ah_idr;
147219820Sjeffextern struct idr ib_uverbs_cq_idr;
148219820Sjeffextern struct idr ib_uverbs_qp_idr;
149219820Sjeffextern struct idr ib_uverbs_srq_idr;
150219820Sjeffextern struct idr ib_uverbs_xrc_domain_idr;
151219820Sjeff
152219820Sjeffvoid idr_remove_uobj(struct idr *idp, struct ib_uobject *uobj);
153219820Sjeff
154219820Sjeffstruct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
155219820Sjeff					int is_async, int *fd);
156219820Sjeffstruct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd);
157219820Sjeff
158219820Sjeffvoid ib_uverbs_release_ucq(struct ib_uverbs_file *file,
159219820Sjeff			   struct ib_uverbs_event_file *ev_file,
160219820Sjeff			   struct ib_ucq_object *uobj);
161219820Sjeffvoid ib_uverbs_release_uevent(struct ib_uverbs_file *file,
162219820Sjeff			      struct ib_uevent_object *uobj);
163219820Sjeff
164219820Sjeffvoid ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context);
165219820Sjeffvoid ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr);
166219820Sjeffvoid ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr);
167219820Sjeffvoid ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
168219820Sjeffvoid ib_uverbs_event_handler(struct ib_event_handler *handler,
169219820Sjeff			     struct ib_event *event);
170219820Sjeffvoid ib_uverbs_xrc_rcv_qp_event_handler(struct ib_event *event,
171219820Sjeff					void *context_ptr);
172219820Sjeffvoid ib_uverbs_dealloc_xrcd(struct ib_device *ib_dev,
173219820Sjeff			    struct ib_xrcd *xrcd);
174219820Sjeffint ib_uverbs_cleanup_xrc_rcv_qp(struct ib_uverbs_file *file,
175219820Sjeff				 struct ib_xrcd *xrcd, u32 qp_num);
176219820Sjeff
177219820Sjeff#define IB_UVERBS_DECLARE_CMD(name)					\
178219820Sjeff	ssize_t ib_uverbs_##name(struct ib_uverbs_file *file,		\
179219820Sjeff				 const char __user *buf, int in_len,	\
180219820Sjeff				 int out_len)
181219820Sjeff
182219820SjeffIB_UVERBS_DECLARE_CMD(get_context);
183219820SjeffIB_UVERBS_DECLARE_CMD(query_device);
184219820SjeffIB_UVERBS_DECLARE_CMD(query_port);
185219820SjeffIB_UVERBS_DECLARE_CMD(alloc_pd);
186219820SjeffIB_UVERBS_DECLARE_CMD(dealloc_pd);
187219820SjeffIB_UVERBS_DECLARE_CMD(reg_mr);
188219820SjeffIB_UVERBS_DECLARE_CMD(dereg_mr);
189219820SjeffIB_UVERBS_DECLARE_CMD(create_comp_channel);
190219820SjeffIB_UVERBS_DECLARE_CMD(create_cq);
191219820SjeffIB_UVERBS_DECLARE_CMD(resize_cq);
192219820SjeffIB_UVERBS_DECLARE_CMD(poll_cq);
193219820SjeffIB_UVERBS_DECLARE_CMD(req_notify_cq);
194219820SjeffIB_UVERBS_DECLARE_CMD(destroy_cq);
195219820SjeffIB_UVERBS_DECLARE_CMD(create_qp);
196219820SjeffIB_UVERBS_DECLARE_CMD(query_qp);
197219820SjeffIB_UVERBS_DECLARE_CMD(modify_qp);
198219820SjeffIB_UVERBS_DECLARE_CMD(destroy_qp);
199219820SjeffIB_UVERBS_DECLARE_CMD(post_send);
200219820SjeffIB_UVERBS_DECLARE_CMD(post_recv);
201219820SjeffIB_UVERBS_DECLARE_CMD(post_srq_recv);
202219820SjeffIB_UVERBS_DECLARE_CMD(create_ah);
203219820SjeffIB_UVERBS_DECLARE_CMD(destroy_ah);
204219820SjeffIB_UVERBS_DECLARE_CMD(attach_mcast);
205219820SjeffIB_UVERBS_DECLARE_CMD(detach_mcast);
206219820SjeffIB_UVERBS_DECLARE_CMD(create_srq);
207219820SjeffIB_UVERBS_DECLARE_CMD(modify_srq);
208219820SjeffIB_UVERBS_DECLARE_CMD(query_srq);
209219820SjeffIB_UVERBS_DECLARE_CMD(destroy_srq);
210219820SjeffIB_UVERBS_DECLARE_CMD(create_xrc_srq);
211219820SjeffIB_UVERBS_DECLARE_CMD(open_xrc_domain);
212219820SjeffIB_UVERBS_DECLARE_CMD(close_xrc_domain);
213219820SjeffIB_UVERBS_DECLARE_CMD(create_xrc_rcv_qp);
214219820SjeffIB_UVERBS_DECLARE_CMD(modify_xrc_rcv_qp);
215219820SjeffIB_UVERBS_DECLARE_CMD(query_xrc_rcv_qp);
216219820SjeffIB_UVERBS_DECLARE_CMD(reg_xrc_rcv_qp);
217219820SjeffIB_UVERBS_DECLARE_CMD(unreg_xrc_rcv_qp);
218219820Sjeff
219219820Sjeff
220219820Sjeff#endif /* UVERBS_H */
221