1 /* 2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005, 2006 Cisco Systems, Inc. All rights reserved. 4 * Copyright (c) 2005 PathScale, Inc. All rights reserved. 5 * 6 * This software is available to you under a choice of one of two 7 * licenses. You may choose to be licensed under the terms of the GNU 8 * General Public License (GPL) Version 2, available from the file 9 * COPYING in the main directory of this source tree, or the 10 * OpenIB.org BSD license below: 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer. 19 * 20 * - Redistributions in binary form must reproduce the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer in the documentation and/or other materials 23 * provided with the distribution. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 * SOFTWARE. 33 */ 34 35 #ifndef INFINIBAND_DRIVER_H 36 #define INFINIBAND_DRIVER_H 37 38 #include <stdbool.h> 39 #include <infiniband/verbs.h> 40 #include <infiniband/kern-abi.h> 41 42 #ifdef __cplusplus 43 # define BEGIN_C_DECLS extern "C" { 44 # define END_C_DECLS } 45 #else /* !__cplusplus */ 46 # define BEGIN_C_DECLS 47 # define END_C_DECLS 48 #endif /* __cplusplus */ 49 50 /* 51 * Extension that low-level drivers should add to their .so filename 52 * (probably via libtool "-release" option). For example a low-level 53 * driver named "libfoo" should build a plug-in named "libfoo-rdmav2.so". 54 */ 55 #define IBV_DEVICE_LIBRARY_EXTENSION rdmav2 56 57 struct verbs_device; 58 59 enum verbs_xrcd_mask { 60 VERBS_XRCD_HANDLE = 1 << 0, 61 VERBS_XRCD_RESERVED = 1 << 1 62 }; 63 64 struct verbs_xrcd { 65 struct ibv_xrcd xrcd; 66 uint32_t comp_mask; 67 uint32_t handle; 68 }; 69 70 enum verbs_srq_mask { 71 VERBS_SRQ_TYPE = 1 << 0, 72 VERBS_SRQ_XRCD = 1 << 1, 73 VERBS_SRQ_CQ = 1 << 2, 74 VERBS_SRQ_NUM = 1 << 3, 75 VERBS_SRQ_RESERVED = 1 << 4 76 }; 77 78 struct verbs_srq { 79 struct ibv_srq srq; 80 uint32_t comp_mask; 81 enum ibv_srq_type srq_type; 82 struct verbs_xrcd *xrcd; 83 struct ibv_cq *cq; 84 uint32_t srq_num; 85 }; 86 87 enum verbs_qp_mask { 88 VERBS_QP_XRCD = 1 << 0, 89 VERBS_QP_RESERVED = 1 << 1 90 }; 91 92 enum ibv_gid_type { 93 IBV_GID_TYPE_IB_ROCE_V1, 94 IBV_GID_TYPE_ROCE_V2, 95 }; 96 97 struct verbs_qp { 98 struct ibv_qp qp; 99 uint32_t comp_mask; 100 struct verbs_xrcd *xrcd; 101 }; 102 103 /* Must change the PRIVATE IBVERBS_PRIVATE_ symbol if this is changed */ 104 struct verbs_device_ops { 105 /* Old interface, do not use in new code. */ 106 struct ibv_context *(*alloc_context)(struct ibv_device *device, 107 int cmd_fd); 108 void (*free_context)(struct ibv_context *context); 109 110 /* New interface */ 111 int (*init_context)(struct verbs_device *device, 112 struct ibv_context *ctx, int cmd_fd); 113 void (*uninit_context)(struct verbs_device *device, 114 struct ibv_context *ctx); 115 }; 116 117 /* Must change the PRIVATE IBVERBS_PRIVATE_ symbol if this is changed */ 118 struct verbs_device { 119 struct ibv_device device; /* Must be first */ 120 const struct verbs_device_ops *ops; 121 size_t sz; 122 size_t size_of_context; 123 }; 124 125 static inline struct verbs_device * 126 verbs_get_device(const struct ibv_device *dev) 127 { 128 return container_of(dev, struct verbs_device, device); 129 } 130 131 typedef struct verbs_device *(*verbs_driver_init_func)(const char *uverbs_sys_path, 132 int abi_version); 133 void verbs_register_driver(const char *name, verbs_driver_init_func init_func); 134 int verbs_init_cq(struct ibv_cq *cq, struct ibv_context *context, 135 struct ibv_comp_channel *channel, 136 void *cq_context); 137 void verbs_cleanup_cq(struct ibv_cq *cq); 138 int ibv_init_wq(struct ibv_wq *wq); 139 void ibv_cleanup_wq(struct ibv_wq *wq); 140 141 int ibv_cmd_get_context(struct ibv_context *context, struct ibv_get_context *cmd, 142 size_t cmd_size, struct ibv_get_context_resp *resp, 143 size_t resp_size); 144 int ibv_cmd_query_device(struct ibv_context *context, 145 struct ibv_device_attr *device_attr, 146 uint64_t *raw_fw_ver, 147 struct ibv_query_device *cmd, size_t cmd_size); 148 int ibv_cmd_query_device_ex(struct ibv_context *context, 149 const struct ibv_query_device_ex_input *input, 150 struct ibv_device_attr_ex *attr, size_t attr_size, 151 uint64_t *raw_fw_ver, 152 struct ibv_query_device_ex *cmd, 153 size_t cmd_core_size, 154 size_t cmd_size, 155 struct ibv_query_device_resp_ex *resp, 156 size_t resp_core_size, 157 size_t resp_size); 158 int ibv_cmd_query_port(struct ibv_context *context, uint8_t port_num, 159 struct ibv_port_attr *port_attr, 160 struct ibv_query_port *cmd, size_t cmd_size); 161 int ibv_cmd_alloc_pd(struct ibv_context *context, struct ibv_pd *pd, 162 struct ibv_alloc_pd *cmd, size_t cmd_size, 163 struct ibv_alloc_pd_resp *resp, size_t resp_size); 164 int ibv_cmd_dealloc_pd(struct ibv_pd *pd); 165 int ibv_cmd_open_xrcd(struct ibv_context *context, struct verbs_xrcd *xrcd, 166 int vxrcd_size, 167 struct ibv_xrcd_init_attr *attr, 168 struct ibv_open_xrcd *cmd, size_t cmd_size, 169 struct ibv_open_xrcd_resp *resp, size_t resp_size); 170 int ibv_cmd_close_xrcd(struct verbs_xrcd *xrcd); 171 #define IBV_CMD_REG_MR_HAS_RESP_PARAMS 172 int ibv_cmd_reg_mr(struct ibv_pd *pd, void *addr, size_t length, 173 uint64_t hca_va, int access, 174 struct ibv_mr *mr, struct ibv_reg_mr *cmd, 175 size_t cmd_size, 176 struct ibv_reg_mr_resp *resp, size_t resp_size); 177 int ibv_cmd_rereg_mr(struct ibv_mr *mr, uint32_t flags, void *addr, 178 size_t length, uint64_t hca_va, int access, 179 struct ibv_pd *pd, struct ibv_rereg_mr *cmd, 180 size_t cmd_sz, struct ibv_rereg_mr_resp *resp, 181 size_t resp_sz); 182 int ibv_cmd_dereg_mr(struct ibv_mr *mr); 183 int ibv_cmd_alloc_mw(struct ibv_pd *pd, enum ibv_mw_type type, 184 struct ibv_mw *mw, struct ibv_alloc_mw *cmd, 185 size_t cmd_size, 186 struct ibv_alloc_mw_resp *resp, size_t resp_size); 187 int ibv_cmd_dealloc_mw(struct ibv_mw *mw, 188 struct ibv_dealloc_mw *cmd, size_t cmd_size); 189 int ibv_cmd_create_cq(struct ibv_context *context, int cqe, 190 struct ibv_comp_channel *channel, 191 int comp_vector, struct ibv_cq *cq, 192 struct ibv_create_cq *cmd, size_t cmd_size, 193 struct ibv_create_cq_resp *resp, size_t resp_size); 194 int ibv_cmd_create_cq_ex(struct ibv_context *context, 195 struct ibv_cq_init_attr_ex *cq_attr, 196 struct ibv_cq_ex *cq, 197 struct ibv_create_cq_ex *cmd, 198 size_t cmd_core_size, 199 size_t cmd_size, 200 struct ibv_create_cq_resp_ex *resp, 201 size_t resp_core_size, 202 size_t resp_size); 203 int ibv_cmd_poll_cq(struct ibv_cq *cq, int ne, struct ibv_wc *wc); 204 int ibv_cmd_req_notify_cq(struct ibv_cq *cq, int solicited_only); 205 #define IBV_CMD_RESIZE_CQ_HAS_RESP_PARAMS 206 int ibv_cmd_resize_cq(struct ibv_cq *cq, int cqe, 207 struct ibv_resize_cq *cmd, size_t cmd_size, 208 struct ibv_resize_cq_resp *resp, size_t resp_size); 209 int ibv_cmd_destroy_cq(struct ibv_cq *cq); 210 211 int ibv_cmd_create_srq(struct ibv_pd *pd, 212 struct ibv_srq *srq, struct ibv_srq_init_attr *attr, 213 struct ibv_create_srq *cmd, size_t cmd_size, 214 struct ibv_create_srq_resp *resp, size_t resp_size); 215 int ibv_cmd_create_srq_ex(struct ibv_context *context, 216 struct verbs_srq *srq, int vsrq_sz, 217 struct ibv_srq_init_attr_ex *attr_ex, 218 struct ibv_create_xsrq *cmd, size_t cmd_size, 219 struct ibv_create_srq_resp *resp, size_t resp_size); 220 int ibv_cmd_modify_srq(struct ibv_srq *srq, 221 struct ibv_srq_attr *srq_attr, 222 int srq_attr_mask, 223 struct ibv_modify_srq *cmd, size_t cmd_size); 224 int ibv_cmd_query_srq(struct ibv_srq *srq, 225 struct ibv_srq_attr *srq_attr, 226 struct ibv_query_srq *cmd, size_t cmd_size); 227 int ibv_cmd_destroy_srq(struct ibv_srq *srq); 228 229 int ibv_cmd_create_qp(struct ibv_pd *pd, 230 struct ibv_qp *qp, struct ibv_qp_init_attr *attr, 231 struct ibv_create_qp *cmd, size_t cmd_size, 232 struct ibv_create_qp_resp *resp, size_t resp_size); 233 int ibv_cmd_create_qp_ex(struct ibv_context *context, 234 struct verbs_qp *qp, int vqp_sz, 235 struct ibv_qp_init_attr_ex *attr_ex, 236 struct ibv_create_qp *cmd, size_t cmd_size, 237 struct ibv_create_qp_resp *resp, size_t resp_size); 238 int ibv_cmd_create_qp_ex2(struct ibv_context *context, 239 struct verbs_qp *qp, int vqp_sz, 240 struct ibv_qp_init_attr_ex *qp_attr, 241 struct ibv_create_qp_ex *cmd, 242 size_t cmd_core_size, 243 size_t cmd_size, 244 struct ibv_create_qp_resp_ex *resp, 245 size_t resp_core_size, 246 size_t resp_size); 247 int ibv_cmd_open_qp(struct ibv_context *context, 248 struct verbs_qp *qp, int vqp_sz, 249 struct ibv_qp_open_attr *attr, 250 struct ibv_open_qp *cmd, size_t cmd_size, 251 struct ibv_create_qp_resp *resp, size_t resp_size); 252 int ibv_cmd_query_qp(struct ibv_qp *qp, struct ibv_qp_attr *qp_attr, 253 int attr_mask, 254 struct ibv_qp_init_attr *qp_init_attr, 255 struct ibv_query_qp *cmd, size_t cmd_size); 256 int ibv_cmd_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr, 257 int attr_mask, 258 struct ibv_modify_qp *cmd, size_t cmd_size); 259 int ibv_cmd_modify_qp_ex(struct ibv_qp *qp, struct ibv_qp_attr *attr, 260 int attr_mask, struct ibv_modify_qp_ex *cmd, 261 size_t cmd_core_size, size_t cmd_size, 262 struct ibv_modify_qp_resp_ex *resp, 263 size_t resp_core_size, size_t resp_size); 264 int ibv_cmd_destroy_qp(struct ibv_qp *qp); 265 int ibv_cmd_post_send(struct ibv_qp *ibqp, struct ibv_send_wr *wr, 266 struct ibv_send_wr **bad_wr); 267 int ibv_cmd_post_recv(struct ibv_qp *ibqp, struct ibv_recv_wr *wr, 268 struct ibv_recv_wr **bad_wr); 269 int ibv_cmd_post_srq_recv(struct ibv_srq *srq, struct ibv_recv_wr *wr, 270 struct ibv_recv_wr **bad_wr); 271 int ibv_cmd_create_ah(struct ibv_pd *pd, struct ibv_ah *ah, 272 struct ibv_ah_attr *attr, 273 struct ibv_create_ah_resp *resp, 274 size_t resp_size); 275 int ibv_cmd_destroy_ah(struct ibv_ah *ah); 276 int ibv_cmd_attach_mcast(struct ibv_qp *qp, const union ibv_gid *gid, uint16_t lid); 277 int ibv_cmd_detach_mcast(struct ibv_qp *qp, const union ibv_gid *gid, uint16_t lid); 278 279 struct ibv_flow *ibv_cmd_create_flow(struct ibv_qp *qp, 280 struct ibv_flow_attr *flow_attr); 281 int ibv_cmd_destroy_flow(struct ibv_flow *flow_id); 282 int ibv_cmd_create_wq(struct ibv_context *context, 283 struct ibv_wq_init_attr *wq_init_attr, 284 struct ibv_wq *wq, 285 struct ibv_create_wq *cmd, 286 size_t cmd_core_size, 287 size_t cmd_size, 288 struct ibv_create_wq_resp *resp, 289 size_t resp_core_size, 290 size_t resp_size); 291 292 int ibv_cmd_modify_wq(struct ibv_wq *wq, struct ibv_wq_attr *attr, 293 struct ibv_modify_wq *cmd, size_t cmd_core_size, 294 size_t cmd_size); 295 int ibv_cmd_destroy_wq(struct ibv_wq *wq); 296 int ibv_cmd_create_rwq_ind_table(struct ibv_context *context, 297 struct ibv_rwq_ind_table_init_attr *init_attr, 298 struct ibv_rwq_ind_table *rwq_ind_table, 299 struct ibv_create_rwq_ind_table *cmd, 300 size_t cmd_core_size, 301 size_t cmd_size, 302 struct ibv_create_rwq_ind_table_resp *resp, 303 size_t resp_core_size, 304 size_t resp_size); 305 int ibv_cmd_destroy_rwq_ind_table(struct ibv_rwq_ind_table *rwq_ind_table); 306 int ibv_dontfork_range(void *base, size_t size); 307 int ibv_dofork_range(void *base, size_t size); 308 309 /* 310 * sysfs helper functions 311 */ 312 const char *ibv_get_sysfs_path(void); 313 314 int ibv_read_sysfs_file(const char *dir, const char *file, 315 char *buf, size_t size); 316 317 static inline int verbs_get_srq_num(struct ibv_srq *srq, uint32_t *srq_num) 318 { 319 struct verbs_srq *vsrq = container_of(srq, struct verbs_srq, srq); 320 if (vsrq->comp_mask & VERBS_SRQ_NUM) { 321 *srq_num = vsrq->srq_num; 322 return 0; 323 } 324 return ENOSYS; 325 } 326 327 static inline bool check_comp_mask(uint64_t input, uint64_t supported) 328 { 329 return (input & ~supported) == 0; 330 } 331 332 int ibv_query_gid_type(struct ibv_context *context, uint8_t port_num, 333 unsigned int index, enum ibv_gid_type *type); 334 #endif /* INFINIBAND_DRIVER_H */ 335