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