Merge branch 'odp_fixes' into rdma.git for-next
[linux-2.6-block.git] / drivers / infiniband / core / core_priv.h
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
48 /* Total number of ports combined across all struct ib_devices's */
49 #define RDMA_MAX_PORTS 8192
50
51 struct pkey_index_qp_list {
52         struct list_head    pkey_index_list;
53         u16                 pkey_index;
54         /* Lock to hold while iterating the qp_list. */
55         spinlock_t          qp_list_lock;
56         struct list_head    qp_list;
57 };
58
59 /**
60  * struct rdma_dev_net - rdma net namespace metadata for a net
61  * @nl_sock:    Pointer to netlink socket
62  * @net:        Pointer to owner net namespace
63  * @id:         xarray id to identify the net namespace.
64  */
65 struct rdma_dev_net {
66         struct sock *nl_sock;
67         possible_net_t net;
68         u32 id;
69 };
70
71 extern const struct attribute_group ib_dev_attr_group;
72 extern bool ib_devices_shared_netns;
73 extern unsigned int rdma_dev_net_id;
74
75 static inline struct rdma_dev_net *rdma_net_to_dev_net(struct net *net)
76 {
77         return net_generic(net, rdma_dev_net_id);
78 }
79
80 int ib_device_register_sysfs(struct ib_device *device);
81 void ib_device_unregister_sysfs(struct ib_device *device);
82 int ib_device_rename(struct ib_device *ibdev, const char *name);
83 int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim);
84
85 typedef void (*roce_netdev_callback)(struct ib_device *device, u8 port,
86               struct net_device *idev, void *cookie);
87
88 typedef bool (*roce_netdev_filter)(struct ib_device *device, u8 port,
89                                    struct net_device *idev, void *cookie);
90
91 struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
92                                         unsigned int port);
93
94 void ib_enum_roce_netdev(struct ib_device *ib_dev,
95                          roce_netdev_filter filter,
96                          void *filter_cookie,
97                          roce_netdev_callback cb,
98                          void *cookie);
99 void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
100                               void *filter_cookie,
101                               roce_netdev_callback cb,
102                               void *cookie);
103
104 typedef int (*nldev_callback)(struct ib_device *device,
105                               struct sk_buff *skb,
106                               struct netlink_callback *cb,
107                               unsigned int idx);
108
109 int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
110                      struct netlink_callback *cb);
111
112 struct ib_client_nl_info {
113         struct sk_buff *nl_msg;
114         struct device *cdev;
115         unsigned int port;
116         u64 abi;
117 };
118 int ib_get_client_nl_info(struct ib_device *ibdev, const char *client_name,
119                           struct ib_client_nl_info *res);
120
121 enum ib_cache_gid_default_mode {
122         IB_CACHE_GID_DEFAULT_MODE_SET,
123         IB_CACHE_GID_DEFAULT_MODE_DELETE
124 };
125
126 int ib_cache_gid_parse_type_str(const char *buf);
127
128 const char *ib_cache_gid_type_str(enum ib_gid_type gid_type);
129
130 void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
131                                   struct net_device *ndev,
132                                   unsigned long gid_type_mask,
133                                   enum ib_cache_gid_default_mode mode);
134
135 int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
136                      union ib_gid *gid, struct ib_gid_attr *attr);
137
138 int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
139                      union ib_gid *gid, struct ib_gid_attr *attr);
140
141 int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
142                                      struct net_device *ndev);
143
144 int roce_gid_mgmt_init(void);
145 void roce_gid_mgmt_cleanup(void);
146
147 unsigned long roce_gid_type_mask_support(struct ib_device *ib_dev, u8 port);
148
149 int ib_cache_setup_one(struct ib_device *device);
150 void ib_cache_cleanup_one(struct ib_device *device);
151 void ib_cache_release_one(struct ib_device *device);
152
153 #ifdef CONFIG_CGROUP_RDMA
154 void ib_device_register_rdmacg(struct ib_device *device);
155 void ib_device_unregister_rdmacg(struct ib_device *device);
156
157 int ib_rdmacg_try_charge(struct ib_rdmacg_object *cg_obj,
158                          struct ib_device *device,
159                          enum rdmacg_resource_type resource_index);
160
161 void ib_rdmacg_uncharge(struct ib_rdmacg_object *cg_obj,
162                         struct ib_device *device,
163                         enum rdmacg_resource_type resource_index);
164 #else
165 static inline void ib_device_register_rdmacg(struct ib_device *device)
166 {
167 }
168
169 static inline void ib_device_unregister_rdmacg(struct ib_device *device)
170 {
171 }
172
173 static inline int ib_rdmacg_try_charge(struct ib_rdmacg_object *cg_obj,
174                                        struct ib_device *device,
175                                        enum rdmacg_resource_type resource_index)
176 {
177         return 0;
178 }
179
180 static inline void ib_rdmacg_uncharge(struct ib_rdmacg_object *cg_obj,
181                                       struct ib_device *device,
182                                       enum rdmacg_resource_type resource_index)
183 {
184 }
185 #endif
186
187 static inline bool rdma_is_upper_dev_rcu(struct net_device *dev,
188                                          struct net_device *upper)
189 {
190         return netdev_has_upper_dev_all_rcu(dev, upper);
191 }
192
193 int addr_init(void);
194 void addr_cleanup(void);
195
196 int ib_mad_init(void);
197 void ib_mad_cleanup(void);
198
199 int ib_sa_init(void);
200 void ib_sa_cleanup(void);
201
202 void rdma_nl_exit(void);
203
204 int ib_nl_handle_resolve_resp(struct sk_buff *skb,
205                               struct nlmsghdr *nlh,
206                               struct netlink_ext_ack *extack);
207 int ib_nl_handle_set_timeout(struct sk_buff *skb,
208                              struct nlmsghdr *nlh,
209                              struct netlink_ext_ack *extack);
210 int ib_nl_handle_ip_res_resp(struct sk_buff *skb,
211                              struct nlmsghdr *nlh,
212                              struct netlink_ext_ack *extack);
213
214 int ib_get_cached_subnet_prefix(struct ib_device *device,
215                                 u8                port_num,
216                                 u64              *sn_pfx);
217
218 #ifdef CONFIG_SECURITY_INFINIBAND
219 void ib_security_release_port_pkey_list(struct ib_device *device);
220
221 void ib_security_cache_change(struct ib_device *device,
222                               u8 port_num,
223                               u64 subnet_prefix);
224
225 int ib_security_modify_qp(struct ib_qp *qp,
226                           struct ib_qp_attr *qp_attr,
227                           int qp_attr_mask,
228                           struct ib_udata *udata);
229
230 int ib_create_qp_security(struct ib_qp *qp, struct ib_device *dev);
231 void ib_destroy_qp_security_begin(struct ib_qp_security *sec);
232 void ib_destroy_qp_security_abort(struct ib_qp_security *sec);
233 void ib_destroy_qp_security_end(struct ib_qp_security *sec);
234 int ib_open_shared_qp_security(struct ib_qp *qp, struct ib_device *dev);
235 void ib_close_shared_qp_security(struct ib_qp_security *sec);
236 int ib_mad_agent_security_setup(struct ib_mad_agent *agent,
237                                 enum ib_qp_type qp_type);
238 void ib_mad_agent_security_cleanup(struct ib_mad_agent *agent);
239 int ib_mad_enforce_security(struct ib_mad_agent_private *map, u16 pkey_index);
240 void ib_mad_agent_security_change(void);
241 #else
242 static inline void ib_security_release_port_pkey_list(struct ib_device *device)
243 {
244 }
245
246 static inline void ib_security_cache_change(struct ib_device *device,
247                                             u8 port_num,
248                                             u64 subnet_prefix)
249 {
250 }
251
252 static inline int ib_security_modify_qp(struct ib_qp *qp,
253                                         struct ib_qp_attr *qp_attr,
254                                         int qp_attr_mask,
255                                         struct ib_udata *udata)
256 {
257         return qp->device->ops.modify_qp(qp->real_qp,
258                                          qp_attr,
259                                          qp_attr_mask,
260                                          udata);
261 }
262
263 static inline int ib_create_qp_security(struct ib_qp *qp,
264                                         struct ib_device *dev)
265 {
266         return 0;
267 }
268
269 static inline void ib_destroy_qp_security_begin(struct ib_qp_security *sec)
270 {
271 }
272
273 static inline void ib_destroy_qp_security_abort(struct ib_qp_security *sec)
274 {
275 }
276
277 static inline void ib_destroy_qp_security_end(struct ib_qp_security *sec)
278 {
279 }
280
281 static inline int ib_open_shared_qp_security(struct ib_qp *qp,
282                                              struct ib_device *dev)
283 {
284         return 0;
285 }
286
287 static inline void ib_close_shared_qp_security(struct ib_qp_security *sec)
288 {
289 }
290
291 static inline int ib_mad_agent_security_setup(struct ib_mad_agent *agent,
292                                               enum ib_qp_type qp_type)
293 {
294         return 0;
295 }
296
297 static inline void ib_mad_agent_security_cleanup(struct ib_mad_agent *agent)
298 {
299 }
300
301 static inline int ib_mad_enforce_security(struct ib_mad_agent_private *map,
302                                           u16 pkey_index)
303 {
304         return 0;
305 }
306
307 static inline void ib_mad_agent_security_change(void)
308 {
309 }
310 #endif
311
312 struct ib_device *ib_device_get_by_index(const struct net *net, u32 index);
313
314 /* RDMA device netlink */
315 void nldev_init(void);
316 void nldev_exit(void);
317
318 static inline struct ib_qp *_ib_create_qp(struct ib_device *dev,
319                                           struct ib_pd *pd,
320                                           struct ib_qp_init_attr *attr,
321                                           struct ib_udata *udata,
322                                           struct ib_uobject *uobj)
323 {
324         enum ib_qp_type qp_type = attr->qp_type;
325         struct ib_qp *qp;
326         bool is_xrc;
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          * We don't track XRC QPs for now, because they don't have PD
341          * and more importantly they are created internaly by driver,
342          * see mlx5 create_dev_resources() as an example.
343          */
344         is_xrc = qp_type == IB_QPT_XRC_INI || qp_type == IB_QPT_XRC_TGT;
345         if ((qp_type < IB_QPT_MAX && !is_xrc) || qp_type == IB_QPT_DRIVER) {
346                 qp->res.type = RDMA_RESTRACK_QP;
347                 if (uobj)
348                         rdma_restrack_uadd(&qp->res);
349                 else
350                         rdma_restrack_kadd(&qp->res);
351         } else
352                 qp->res.valid = false;
353
354         return qp;
355 }
356
357 struct rdma_dev_addr;
358 int rdma_resolve_ip_route(struct sockaddr *src_addr,
359                           const struct sockaddr *dst_addr,
360                           struct rdma_dev_addr *addr);
361
362 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
363                                  const union ib_gid *dgid,
364                                  u8 *dmac, const struct ib_gid_attr *sgid_attr,
365                                  int *hoplimit);
366 void rdma_copy_src_l2_addr(struct rdma_dev_addr *dev_addr,
367                            const struct net_device *dev);
368
369 struct sa_path_rec;
370 int roce_resolve_route_from_path(struct sa_path_rec *rec,
371                                  const struct ib_gid_attr *attr);
372
373 struct net_device *rdma_read_gid_attr_ndev_rcu(const struct ib_gid_attr *attr);
374
375 void ib_free_port_attrs(struct ib_core_device *coredev);
376 int ib_setup_port_attrs(struct ib_core_device *coredev);
377
378 int rdma_compatdev_set(u8 enable);
379
380 int ib_port_register_module_stat(struct ib_device *device, u8 port_num,
381                                  struct kobject *kobj, struct kobj_type *ktype,
382                                  const char *name);
383 void ib_port_unregister_module_stat(struct kobject *kobj);
384
385 int ib_device_set_netns_put(struct sk_buff *skb,
386                             struct ib_device *dev, u32 ns_fd);
387
388 int rdma_nl_net_init(struct rdma_dev_net *rnet);
389 void rdma_nl_net_exit(struct rdma_dev_net *rnet);
390 #endif /* _CORE_PRIV_H */