1 /* 2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. 3 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved. 4 * 5 * This software is available to you under a choice of one of two 6 * licenses. You may choose to be licensed under the terms of the GNU 7 * General Public License (GPL) Version 2, available from the file 8 * COPYING in the main directory of this source tree, or the 9 * OpenIB.org BSD license below: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 #define _GNU_SOURCE 34 #include <config.h> 35 36 #include <infiniband/endian.h> 37 #include <stdio.h> 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 #include <fcntl.h> 41 #include <unistd.h> 42 #include <stdlib.h> 43 #include <alloca.h> 44 #include <errno.h> 45 46 #include "ibverbs.h" 47 48 /* Hack to avoid GCC's -Wmissing-prototypes and the similar error from sparse 49 with these prototypes. Symbol versionining requires the goofy names, the 50 prototype must match the version in verbs.h. 51 */ 52 struct ibv_device **__ibv_get_device_list(int *num_devices); 53 void __ibv_free_device_list(struct ibv_device **list); 54 const char *__ibv_get_device_name(struct ibv_device *device); 55 __be64 __ibv_get_device_guid(struct ibv_device *device); 56 struct ibv_context *__ibv_open_device(struct ibv_device *device); 57 int __ibv_close_device(struct ibv_context *context); 58 int __ibv_get_async_event(struct ibv_context *context, 59 struct ibv_async_event *event); 60 void __ibv_ack_async_event(struct ibv_async_event *event); 61 62 static pthread_once_t device_list_once = PTHREAD_ONCE_INIT; 63 static int num_devices; 64 static struct ibv_device **device_list; 65 66 static void count_devices(void) 67 { 68 num_devices = ibverbs_init(&device_list); 69 } 70 71 struct ibv_device **__ibv_get_device_list(int *num) 72 { 73 struct ibv_device **l; 74 int i; 75 76 if (num) 77 *num = 0; 78 79 pthread_once(&device_list_once, count_devices); 80 81 if (num_devices < 0) { 82 errno = -num_devices; 83 return NULL; 84 } 85 86 l = calloc(num_devices + 1, sizeof (struct ibv_device *)); 87 if (!l) { 88 errno = ENOMEM; 89 return NULL; 90 } 91 92 for (i = 0; i < num_devices; ++i) 93 l[i] = device_list[i]; 94 if (num) 95 *num = num_devices; 96 97 return l; 98 } 99 default_symver(__ibv_get_device_list, ibv_get_device_list); 100 101 void __ibv_free_device_list(struct ibv_device **list) 102 { 103 free(list); 104 } 105 default_symver(__ibv_free_device_list, ibv_free_device_list); 106 107 const char *__ibv_get_device_name(struct ibv_device *device) 108 { 109 return device->name; 110 } 111 default_symver(__ibv_get_device_name, ibv_get_device_name); 112 113 __be64 __ibv_get_device_guid(struct ibv_device *device) 114 { 115 char attr[24]; 116 uint64_t guid = 0; 117 uint16_t parts[4]; 118 int i; 119 120 if (ibv_read_sysfs_file(device->ibdev_path, "node_guid", 121 attr, sizeof attr) < 0) 122 return 0; 123 124 if (sscanf(attr, "%hx:%hx:%hx:%hx", 125 parts, parts + 1, parts + 2, parts + 3) != 4) 126 return 0; 127 128 for (i = 0; i < 4; ++i) 129 guid = (guid << 16) | parts[i]; 130 131 return htobe64(guid); 132 } 133 default_symver(__ibv_get_device_guid, ibv_get_device_guid); 134 135 void verbs_init_cq(struct ibv_cq *cq, struct ibv_context *context, 136 struct ibv_comp_channel *channel, 137 void *cq_context) 138 { 139 cq->context = context; 140 cq->channel = channel; 141 142 if (cq->channel) { 143 pthread_mutex_lock(&context->mutex); 144 ++cq->channel->refcnt; 145 pthread_mutex_unlock(&context->mutex); 146 } 147 148 cq->cq_context = cq_context; 149 cq->comp_events_completed = 0; 150 cq->async_events_completed = 0; 151 pthread_mutex_init(&cq->mutex, NULL); 152 pthread_cond_init(&cq->cond, NULL); 153 } 154 155 static struct ibv_cq_ex * 156 __lib_ibv_create_cq_ex(struct ibv_context *context, 157 struct ibv_cq_init_attr_ex *cq_attr) 158 { 159 struct verbs_context *vctx = verbs_get_ctx(context); 160 struct ibv_cq_ex *cq; 161 162 if (cq_attr->wc_flags & ~IBV_CREATE_CQ_SUP_WC_FLAGS) { 163 errno = EOPNOTSUPP; 164 return NULL; 165 } 166 167 cq = vctx->priv->create_cq_ex(context, cq_attr); 168 169 if (cq) 170 verbs_init_cq(ibv_cq_ex_to_cq(cq), context, 171 cq_attr->channel, cq_attr->cq_context); 172 173 return cq; 174 } 175 176 struct ibv_context *__ibv_open_device(struct ibv_device *device) 177 { 178 struct verbs_device *verbs_device = verbs_get_device(device); 179 char *devpath; 180 int cmd_fd, ret; 181 struct ibv_context *context; 182 struct verbs_context *context_ex; 183 184 if (asprintf(&devpath, "/dev/%s", device->dev_name) < 0) 185 return NULL; 186 187 /* 188 * We'll only be doing writes, but we need O_RDWR in case the 189 * provider needs to mmap() the file. 190 */ 191 cmd_fd = open(devpath, O_RDWR | O_CLOEXEC); 192 free(devpath); 193 194 if (cmd_fd < 0) 195 return NULL; 196 197 if (!verbs_device->ops->init_context) { 198 context = verbs_device->ops->alloc_context(device, cmd_fd); 199 if (!context) 200 goto err; 201 } else { 202 struct verbs_ex_private *priv; 203 204 /* Library now allocates the context */ 205 context_ex = calloc(1, sizeof(*context_ex) + 206 verbs_device->size_of_context); 207 if (!context_ex) { 208 errno = ENOMEM; 209 goto err; 210 } 211 212 priv = calloc(1, sizeof(*priv)); 213 if (!priv) { 214 errno = ENOMEM; 215 free(context_ex); 216 goto err; 217 } 218 219 context_ex->priv = priv; 220 context_ex->context.abi_compat = __VERBS_ABI_IS_EXTENDED; 221 context_ex->sz = sizeof(*context_ex); 222 223 context = &context_ex->context; 224 ret = verbs_device->ops->init_context(verbs_device, context, cmd_fd); 225 if (ret) 226 goto verbs_err; 227 /* 228 * In order to maintain backward/forward binary compatibility 229 * with apps compiled against libibverbs-1.1.8 that use the 230 * flow steering addition, we need to set the two 231 * ABI_placeholder entries to match the driver set flow 232 * entries. This is because apps compiled against 233 * libibverbs-1.1.8 use an inline ibv_create_flow and 234 * ibv_destroy_flow function that looks in the placeholder 235 * spots for the proper entry points. For apps compiled 236 * against libibverbs-1.1.9 and later, the inline functions 237 * will be looking in the right place. 238 */ 239 context_ex->ABI_placeholder1 = (void (*)(void)) context_ex->ibv_create_flow; 240 context_ex->ABI_placeholder2 = (void (*)(void)) context_ex->ibv_destroy_flow; 241 242 if (context_ex->create_cq_ex) { 243 priv->create_cq_ex = context_ex->create_cq_ex; 244 context_ex->create_cq_ex = __lib_ibv_create_cq_ex; 245 } 246 } 247 248 context->device = device; 249 context->cmd_fd = cmd_fd; 250 pthread_mutex_init(&context->mutex, NULL); 251 252 return context; 253 254 verbs_err: 255 free(context_ex->priv); 256 free(context_ex); 257 err: 258 close(cmd_fd); 259 return NULL; 260 } 261 default_symver(__ibv_open_device, ibv_open_device); 262 263 int __ibv_close_device(struct ibv_context *context) 264 { 265 int async_fd = context->async_fd; 266 int cmd_fd = context->cmd_fd; 267 struct verbs_context *context_ex; 268 struct verbs_device *verbs_device = verbs_get_device(context->device); 269 270 context_ex = verbs_get_ctx(context); 271 if (context_ex) { 272 verbs_device->ops->uninit_context(verbs_device, context); 273 free(context_ex->priv); 274 free(context_ex); 275 } else { 276 verbs_device->ops->free_context(context); 277 } 278 279 close(async_fd); 280 close(cmd_fd); 281 282 return 0; 283 } 284 default_symver(__ibv_close_device, ibv_close_device); 285 286 int __ibv_get_async_event(struct ibv_context *context, 287 struct ibv_async_event *event) 288 { 289 struct ibv_kern_async_event ev; 290 291 if (read(context->async_fd, &ev, sizeof ev) != sizeof ev) 292 return -1; 293 294 event->event_type = ev.event_type; 295 296 switch (event->event_type) { 297 case IBV_EVENT_CQ_ERR: 298 event->element.cq = (void *) (uintptr_t) ev.element; 299 break; 300 301 case IBV_EVENT_QP_FATAL: 302 case IBV_EVENT_QP_REQ_ERR: 303 case IBV_EVENT_QP_ACCESS_ERR: 304 case IBV_EVENT_COMM_EST: 305 case IBV_EVENT_SQ_DRAINED: 306 case IBV_EVENT_PATH_MIG: 307 case IBV_EVENT_PATH_MIG_ERR: 308 case IBV_EVENT_QP_LAST_WQE_REACHED: 309 event->element.qp = (void *) (uintptr_t) ev.element; 310 break; 311 312 case IBV_EVENT_SRQ_ERR: 313 case IBV_EVENT_SRQ_LIMIT_REACHED: 314 event->element.srq = (void *) (uintptr_t) ev.element; 315 break; 316 317 case IBV_EVENT_WQ_FATAL: 318 event->element.wq = (void *) (uintptr_t) ev.element; 319 break; 320 default: 321 event->element.port_num = ev.element; 322 break; 323 } 324 325 if (context->ops.async_event) 326 context->ops.async_event(event); 327 328 return 0; 329 } 330 default_symver(__ibv_get_async_event, ibv_get_async_event); 331 332 void __ibv_ack_async_event(struct ibv_async_event *event) 333 { 334 switch (event->event_type) { 335 case IBV_EVENT_CQ_ERR: 336 { 337 struct ibv_cq *cq = event->element.cq; 338 339 pthread_mutex_lock(&cq->mutex); 340 ++cq->async_events_completed; 341 pthread_cond_signal(&cq->cond); 342 pthread_mutex_unlock(&cq->mutex); 343 344 return; 345 } 346 347 case IBV_EVENT_QP_FATAL: 348 case IBV_EVENT_QP_REQ_ERR: 349 case IBV_EVENT_QP_ACCESS_ERR: 350 case IBV_EVENT_COMM_EST: 351 case IBV_EVENT_SQ_DRAINED: 352 case IBV_EVENT_PATH_MIG: 353 case IBV_EVENT_PATH_MIG_ERR: 354 case IBV_EVENT_QP_LAST_WQE_REACHED: 355 { 356 struct ibv_qp *qp = event->element.qp; 357 358 pthread_mutex_lock(&qp->mutex); 359 ++qp->events_completed; 360 pthread_cond_signal(&qp->cond); 361 pthread_mutex_unlock(&qp->mutex); 362 363 return; 364 } 365 366 case IBV_EVENT_SRQ_ERR: 367 case IBV_EVENT_SRQ_LIMIT_REACHED: 368 { 369 struct ibv_srq *srq = event->element.srq; 370 371 pthread_mutex_lock(&srq->mutex); 372 ++srq->events_completed; 373 pthread_cond_signal(&srq->cond); 374 pthread_mutex_unlock(&srq->mutex); 375 376 return; 377 } 378 379 case IBV_EVENT_WQ_FATAL: 380 { 381 struct ibv_wq *wq = event->element.wq; 382 383 pthread_mutex_lock(&wq->mutex); 384 ++wq->events_completed; 385 pthread_cond_signal(&wq->cond); 386 pthread_mutex_unlock(&wq->mutex); 387 388 return; 389 } 390 391 default: 392 return; 393 } 394 } 395 default_symver(__ibv_ack_async_event, ibv_ack_async_event); 396