1 /*
2  * Copyright (c) 2004 Topspin Communications.  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 
33 #ifndef _CORE_PRIV_H
34 #define _CORE_PRIV_H
35 
36 #include <linux/list.h>
37 #include <linux/spinlock.h>
38 #include <linux/cgroup_rdma.h>
39 #include <net/net_namespace.h>
40 #include <net/netns/generic.h>
41 
42 #include <rdma/ib_verbs.h>
43 #include <rdma/opa_addr.h>
44 #include <rdma/ib_mad.h>
45 #include <rdma/restrack.h>
46 #include "mad_priv.h"
47 #include "restrack.h"
48 
49 /* Total number of ports combined across all struct ib_devices's */
50 #define RDMA_MAX_PORTS 8192
51 
52 struct pkey_index_qp_list {
53 	struct list_head    pkey_index_list;
54 	u16                 pkey_index;
55 	/* Lock to hold while iterating the qp_list. */
56 	spinlock_t          qp_list_lock;
57 	struct list_head    qp_list;
58 };
59 
60 /**
61  * struct rdma_dev_net - rdma net namespace metadata for a net
62  * @nl_sock:	Pointer to netlink socket
63  * @net:	Pointer to owner net namespace
64  * @id:		xarray id to identify the net namespace.
65  */
66 struct rdma_dev_net {
67 	struct sock *nl_sock;
68 	possible_net_t net;
69 	u32 id;
70 };
71 
72 extern const struct attribute_group ib_dev_attr_group;
73 extern bool ib_devices_shared_netns;
74 extern unsigned int rdma_dev_net_id;
75 
76 static inline struct rdma_dev_net *rdma_net_to_dev_net(struct net *net)
77 {
78 	return net_generic(net, rdma_dev_net_id);
79 }
80 
81 int ib_device_register_sysfs(struct ib_device *device);
82 void ib_device_unregister_sysfs(struct ib_device *device);
83 int ib_device_rename(struct ib_device *ibdev, const char *name);
84 int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim);
85 
86 typedef void (*roce_netdev_callback)(struct ib_device *device, u8 port,
87 	      struct net_device *idev, void *cookie);
88 
89 typedef bool (*roce_netdev_filter)(struct ib_device *device, u8 port,
90 				   struct net_device *idev, void *cookie);
91 
92 struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
93 					unsigned int port);
94 
95 void ib_enum_roce_netdev(struct ib_device *ib_dev,
96 			 roce_netdev_filter filter,
97 			 void *filter_cookie,
98 			 roce_netdev_callback cb,
99 			 void *cookie);
100 void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
101 			      void *filter_cookie,
102 			      roce_netdev_callback cb,
103 			      void *cookie);
104 
105 typedef int (*nldev_callback)(struct ib_device *device,
106 			      struct sk_buff *skb,
107 			      struct netlink_callback *cb,
108 			      unsigned int idx);
109 
110 int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
111 		     struct netlink_callback *cb);
112 
113 struct ib_client_nl_info {
114 	struct sk_buff *nl_msg;
115 	struct device *cdev;
116 	unsigned int port;
117 	u64 abi;
118 };
119 int ib_get_client_nl_info(struct ib_device *ibdev, const char *client_name,
120 			  struct ib_client_nl_info *res);
121 
122 enum ib_cache_gid_default_mode {
123 	IB_CACHE_GID_DEFAULT_MODE_SET,
124 	IB_CACHE_GID_DEFAULT_MODE_DELETE
125 };
126 
127 int ib_cache_gid_parse_type_str(const char *buf);
128 
129 const char *ib_cache_gid_type_str(enum ib_gid_type gid_type);
130 
131 void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
132 				  struct net_device *ndev,
133 				  unsigned long gid_type_mask,
134 				  enum ib_cache_gid_default_mode mode);
135 
136 int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
137 		     union ib_gid *gid, struct ib_gid_attr *attr);
138 
139 int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
140 		     union ib_gid *gid, struct ib_gid_attr *attr);
141 
142 int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
143 				     struct net_device *ndev);
144 
145 int roce_gid_mgmt_init(void);
146 void roce_gid_mgmt_cleanup(void);
147 
148 unsigned long roce_gid_type_mask_support(struct ib_device *ib_dev, u8 port);
149 
150 int ib_cache_setup_one(struct ib_device *device);
151 void ib_cache_cleanup_one(struct ib_device *device);
152 void ib_cache_release_one(struct ib_device *device);
153 void ib_dispatch_event_clients(struct ib_event *event);
154 
155 #ifdef CONFIG_CGROUP_RDMA
156 void ib_device_register_rdmacg(struct ib_device *device);
157 void ib_device_unregister_rdmacg(struct ib_device *device);
158 
159 int ib_rdmacg_try_charge(struct ib_rdmacg_object *cg_obj,
160 			 struct ib_device *device,
161 			 enum rdmacg_resource_type resource_index);
162 
163 void ib_rdmacg_uncharge(struct ib_rdmacg_object *cg_obj,
164 			struct ib_device *device,
165 			enum rdmacg_resource_type resource_index);
166 #else
167 static inline void ib_device_register_rdmacg(struct ib_device *device)
168 {
169 }
170 
171 static inline void ib_device_unregister_rdmacg(struct ib_device *device)
172 {
173 }
174 
175 static inline int ib_rdmacg_try_charge(struct ib_rdmacg_object *cg_obj,
176 				       struct ib_device *device,
177 				       enum rdmacg_resource_type resource_index)
178 {
179 	return 0;
180 }
181 
182 static inline void ib_rdmacg_uncharge(struct ib_rdmacg_object *cg_obj,
183 				      struct ib_device *device,
184 				      enum rdmacg_resource_type resource_index)
185 {
186 }
187 #endif
188 
189 static inline bool rdma_is_upper_dev_rcu(struct net_device *dev,
190 					 struct net_device *upper)
191 {
192 	return netdev_has_upper_dev_all_rcu(dev, upper);
193 }
194 
195 int addr_init(void);
196 void addr_cleanup(void);
197 
198 int ib_mad_init(void);
199 void ib_mad_cleanup(void);
200 
201 int ib_sa_init(void);
202 void ib_sa_cleanup(void);
203 
204 void rdma_nl_init(void);
205 void rdma_nl_exit(void);
206 
207 int ib_nl_handle_resolve_resp(struct sk_buff *skb,
208 			      struct nlmsghdr *nlh,
209 			      struct netlink_ext_ack *extack);
210 int ib_nl_handle_set_timeout(struct sk_buff *skb,
211 			     struct nlmsghdr *nlh,
212 			     struct netlink_ext_ack *extack);
213 int ib_nl_handle_ip_res_resp(struct sk_buff *skb,
214 			     struct nlmsghdr *nlh,
215 			     struct netlink_ext_ack *extack);
216 
217 int ib_get_cached_subnet_prefix(struct ib_device *device,
218 				u8                port_num,
219 				u64              *sn_pfx);
220 
221 #ifdef CONFIG_SECURITY_INFINIBAND
222 void ib_security_release_port_pkey_list(struct ib_device *device);
223 
224 void ib_security_cache_change(struct ib_device *device,
225 			      u8 port_num,
226 			      u64 subnet_prefix);
227 
228 int ib_security_modify_qp(struct ib_qp *qp,
229 			  struct ib_qp_attr *qp_attr,
230 			  int qp_attr_mask,
231 			  struct ib_udata *udata);
232 
233 int ib_create_qp_security(struct ib_qp *qp, struct ib_device *dev);
234 void ib_destroy_qp_security_begin(struct ib_qp_security *sec);
235 void ib_destroy_qp_security_abort(struct ib_qp_security *sec);
236 void ib_destroy_qp_security_end(struct ib_qp_security *sec);
237 int ib_open_shared_qp_security(struct ib_qp *qp, struct ib_device *dev);
238 void ib_close_shared_qp_security(struct ib_qp_security *sec);
239 int ib_mad_agent_security_setup(struct ib_mad_agent *agent,
240 				enum ib_qp_type qp_type);
241 void ib_mad_agent_security_cleanup(struct ib_mad_agent *agent);
242 int ib_mad_enforce_security(struct ib_mad_agent_private *map, u16 pkey_index);
243 void ib_mad_agent_security_change(void);
244 #else
245 static inline void ib_security_release_port_pkey_list(struct ib_device *device)
246 {
247 }
248 
249 static inline void ib_security_cache_change(struct ib_device *device,
250 					    u8 port_num,
251 					    u64 subnet_prefix)
252 {
253 }
254 
255 static inline int ib_security_modify_qp(struct ib_qp *qp,
256 					struct ib_qp_attr *qp_attr,
257 					int qp_attr_mask,
258 					struct ib_udata *udata)
259 {
260 	return qp->device->ops.modify_qp(qp->real_qp,
261 					 qp_attr,
262 					 qp_attr_mask,
263 					 udata);
264 }
265 
266 static inline int ib_create_qp_security(struct ib_qp *qp,
267 					struct ib_device *dev)
268 {
269 	return 0;
270 }
271 
272 static inline void ib_destroy_qp_security_begin(struct ib_qp_security *sec)
273 {
274 }
275 
276 static inline void ib_destroy_qp_security_abort(struct ib_qp_security *sec)
277 {
278 }
279 
280 static inline void ib_destroy_qp_security_end(struct ib_qp_security *sec)
281 {
282 }
283 
284 static inline int ib_open_shared_qp_security(struct ib_qp *qp,
285 					     struct ib_device *dev)
286 {
287 	return 0;
288 }
289 
290 static inline void ib_close_shared_qp_security(struct ib_qp_security *sec)
291 {
292 }
293 
294 static inline int ib_mad_agent_security_setup(struct ib_mad_agent *agent,
295 					      enum ib_qp_type qp_type)
296 {
297 	return 0;
298 }
299 
300 static inline void ib_mad_agent_security_cleanup(struct ib_mad_agent *agent)
301 {
302 }
303 
304 static inline int ib_mad_enforce_security(struct ib_mad_agent_private *map,
305 					  u16 pkey_index)
306 {
307 	return 0;
308 }
309 
310 static inline void ib_mad_agent_security_change(void)
311 {
312 }
313 #endif
314 
315 struct ib_device *ib_device_get_by_index(const struct net *net, u32 index);
316 
317 /* RDMA device netlink */
318 void nldev_init(void);
319 void nldev_exit(void);
320 
321 static inline struct ib_qp *
322 _ib_create_qp(struct ib_device *dev, struct ib_pd *pd,
323 	      struct ib_qp_init_attr *attr, struct ib_udata *udata,
324 	      struct ib_uqp_object *uobj, const char *caller)
325 {
326 	struct ib_qp *qp;
327 
328 	if (!dev->ops.create_qp)
329 		return ERR_PTR(-EOPNOTSUPP);
330 
331 	qp = dev->ops.create_qp(pd, attr, udata);
332 	if (IS_ERR(qp))
333 		return qp;
334 
335 	qp->device = dev;
336 	qp->pd = pd;
337 	qp->uobject = uobj;
338 	qp->real_qp = qp;
339 
340 	qp->qp_type = attr->qp_type;
341 	qp->rwq_ind_tbl = attr->rwq_ind_tbl;
342 	qp->send_cq = attr->send_cq;
343 	qp->recv_cq = attr->recv_cq;
344 	qp->srq = attr->srq;
345 	qp->rwq_ind_tbl = attr->rwq_ind_tbl;
346 	qp->event_handler = attr->event_handler;
347 	qp->port = attr->port_num;
348 
349 	atomic_set(&qp->usecnt, 0);
350 	spin_lock_init(&qp->mr_lock);
351 	INIT_LIST_HEAD(&qp->rdma_mrs);
352 	INIT_LIST_HEAD(&qp->sig_mrs);
353 
354 	rdma_restrack_new(&qp->res, RDMA_RESTRACK_QP);
355 	WARN_ONCE(!udata && !caller, "Missing kernel QP owner");
356 	rdma_restrack_set_name(&qp->res, udata ? NULL : caller);
357 	rdma_restrack_add(&qp->res);
358 	return qp;
359 }
360 
361 struct rdma_dev_addr;
362 int rdma_resolve_ip_route(struct sockaddr *src_addr,
363 			  const struct sockaddr *dst_addr,
364 			  struct rdma_dev_addr *addr);
365 
366 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
367 				 const union ib_gid *dgid,
368 				 u8 *dmac, const struct ib_gid_attr *sgid_attr,
369 				 int *hoplimit);
370 void rdma_copy_src_l2_addr(struct rdma_dev_addr *dev_addr,
371 			   const struct net_device *dev);
372 
373 struct sa_path_rec;
374 int roce_resolve_route_from_path(struct sa_path_rec *rec,
375 				 const struct ib_gid_attr *attr);
376 
377 struct net_device *rdma_read_gid_attr_ndev_rcu(const struct ib_gid_attr *attr);
378 
379 void ib_free_port_attrs(struct ib_core_device *coredev);
380 int ib_setup_port_attrs(struct ib_core_device *coredev);
381 
382 int rdma_compatdev_set(u8 enable);
383 
384 int ib_port_register_module_stat(struct ib_device *device, u8 port_num,
385 				 struct kobject *kobj, struct kobj_type *ktype,
386 				 const char *name);
387 void ib_port_unregister_module_stat(struct kobject *kobj);
388 
389 int ib_device_set_netns_put(struct sk_buff *skb,
390 			    struct ib_device *dev, u32 ns_fd);
391 
392 int rdma_nl_net_init(struct rdma_dev_net *rnet);
393 void rdma_nl_net_exit(struct rdma_dev_net *rnet);
394 
395 struct rdma_umap_priv {
396 	struct vm_area_struct *vma;
397 	struct list_head list;
398 	struct rdma_user_mmap_entry *entry;
399 };
400 
401 void rdma_umap_priv_init(struct rdma_umap_priv *priv,
402 			 struct vm_area_struct *vma,
403 			 struct rdma_user_mmap_entry *entry);
404 
405 void ib_cq_pool_cleanup(struct ib_device *dev);
406 
407 #endif /* _CORE_PRIV_H */
408