1 /* 2 * Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved. 3 * Copyright (c) 2004 Infinicon Corporation. All rights reserved. 4 * Copyright (c) 2004 Intel Corporation. All rights reserved. 5 * Copyright (c) 2004 Topspin Corporation. All rights reserved. 6 * Copyright (c) 2004 Voltaire Corporation. All rights reserved. 7 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 8 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved. 9 * 10 * This software is available to you under a choice of one of two 11 * licenses. You may choose to be licensed under the terms of the GNU 12 * General Public License (GPL) Version 2, available from the file 13 * COPYING in the main directory of this source tree, or the 14 * OpenIB.org BSD license below: 15 * 16 * Redistribution and use in source and binary forms, with or 17 * without modification, are permitted provided that the following 18 * conditions are met: 19 * 20 * - Redistributions of source code must retain the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer. 23 * 24 * - Redistributions in binary form must reproduce the above 25 * copyright notice, this list of conditions and the following 26 * disclaimer in the documentation and/or other materials 27 * provided with the distribution. 28 * 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 30 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 32 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 33 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 34 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 35 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 36 * SOFTWARE. 37 */ 38 39 #include <linux/errno.h> 40 #include <linux/err.h> 41 #include <linux/export.h> 42 #include <linux/string.h> 43 #include <linux/slab.h> 44 #include <linux/in.h> 45 #include <linux/in6.h> 46 #include <net/addrconf.h> 47 #include <linux/security.h> 48 49 #include <rdma/ib_verbs.h> 50 #include <rdma/ib_cache.h> 51 #include <rdma/ib_addr.h> 52 #include <rdma/rw.h> 53 54 #include "core_priv.h" 55 #include <trace/events/rdma_core.h> 56 57 #include <trace/events/rdma_core.h> 58 59 static int ib_resolve_eth_dmac(struct ib_device *device, 60 struct rdma_ah_attr *ah_attr); 61 62 static const char * const ib_events[] = { 63 [IB_EVENT_CQ_ERR] = "CQ error", 64 [IB_EVENT_QP_FATAL] = "QP fatal error", 65 [IB_EVENT_QP_REQ_ERR] = "QP request error", 66 [IB_EVENT_QP_ACCESS_ERR] = "QP access error", 67 [IB_EVENT_COMM_EST] = "communication established", 68 [IB_EVENT_SQ_DRAINED] = "send queue drained", 69 [IB_EVENT_PATH_MIG] = "path migration successful", 70 [IB_EVENT_PATH_MIG_ERR] = "path migration error", 71 [IB_EVENT_DEVICE_FATAL] = "device fatal error", 72 [IB_EVENT_PORT_ACTIVE] = "port active", 73 [IB_EVENT_PORT_ERR] = "port error", 74 [IB_EVENT_LID_CHANGE] = "LID change", 75 [IB_EVENT_PKEY_CHANGE] = "P_key change", 76 [IB_EVENT_SM_CHANGE] = "SM change", 77 [IB_EVENT_SRQ_ERR] = "SRQ error", 78 [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached", 79 [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached", 80 [IB_EVENT_CLIENT_REREGISTER] = "client reregister", 81 [IB_EVENT_GID_CHANGE] = "GID changed", 82 }; 83 84 const char *__attribute_const__ ib_event_msg(enum ib_event_type event) 85 { 86 size_t index = event; 87 88 return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ? 89 ib_events[index] : "unrecognized event"; 90 } 91 EXPORT_SYMBOL(ib_event_msg); 92 93 static const char * const wc_statuses[] = { 94 [IB_WC_SUCCESS] = "success", 95 [IB_WC_LOC_LEN_ERR] = "local length error", 96 [IB_WC_LOC_QP_OP_ERR] = "local QP operation error", 97 [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error", 98 [IB_WC_LOC_PROT_ERR] = "local protection error", 99 [IB_WC_WR_FLUSH_ERR] = "WR flushed", 100 [IB_WC_MW_BIND_ERR] = "memory management operation error", 101 [IB_WC_BAD_RESP_ERR] = "bad response error", 102 [IB_WC_LOC_ACCESS_ERR] = "local access error", 103 [IB_WC_REM_INV_REQ_ERR] = "invalid request error", 104 [IB_WC_REM_ACCESS_ERR] = "remote access error", 105 [IB_WC_REM_OP_ERR] = "remote operation error", 106 [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded", 107 [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded", 108 [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error", 109 [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request", 110 [IB_WC_REM_ABORT_ERR] = "operation aborted", 111 [IB_WC_INV_EECN_ERR] = "invalid EE context number", 112 [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state", 113 [IB_WC_FATAL_ERR] = "fatal error", 114 [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error", 115 [IB_WC_GENERAL_ERR] = "general error", 116 }; 117 118 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status) 119 { 120 size_t index = status; 121 122 return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ? 123 wc_statuses[index] : "unrecognized status"; 124 } 125 EXPORT_SYMBOL(ib_wc_status_msg); 126 127 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate) 128 { 129 switch (rate) { 130 case IB_RATE_2_5_GBPS: return 1; 131 case IB_RATE_5_GBPS: return 2; 132 case IB_RATE_10_GBPS: return 4; 133 case IB_RATE_20_GBPS: return 8; 134 case IB_RATE_30_GBPS: return 12; 135 case IB_RATE_40_GBPS: return 16; 136 case IB_RATE_60_GBPS: return 24; 137 case IB_RATE_80_GBPS: return 32; 138 case IB_RATE_120_GBPS: return 48; 139 case IB_RATE_14_GBPS: return 6; 140 case IB_RATE_56_GBPS: return 22; 141 case IB_RATE_112_GBPS: return 45; 142 case IB_RATE_168_GBPS: return 67; 143 case IB_RATE_25_GBPS: return 10; 144 case IB_RATE_100_GBPS: return 40; 145 case IB_RATE_200_GBPS: return 80; 146 case IB_RATE_300_GBPS: return 120; 147 case IB_RATE_28_GBPS: return 11; 148 case IB_RATE_50_GBPS: return 20; 149 case IB_RATE_400_GBPS: return 160; 150 case IB_RATE_600_GBPS: return 240; 151 default: return -1; 152 } 153 } 154 EXPORT_SYMBOL(ib_rate_to_mult); 155 156 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult) 157 { 158 switch (mult) { 159 case 1: return IB_RATE_2_5_GBPS; 160 case 2: return IB_RATE_5_GBPS; 161 case 4: return IB_RATE_10_GBPS; 162 case 8: return IB_RATE_20_GBPS; 163 case 12: return IB_RATE_30_GBPS; 164 case 16: return IB_RATE_40_GBPS; 165 case 24: return IB_RATE_60_GBPS; 166 case 32: return IB_RATE_80_GBPS; 167 case 48: return IB_RATE_120_GBPS; 168 case 6: return IB_RATE_14_GBPS; 169 case 22: return IB_RATE_56_GBPS; 170 case 45: return IB_RATE_112_GBPS; 171 case 67: return IB_RATE_168_GBPS; 172 case 10: return IB_RATE_25_GBPS; 173 case 40: return IB_RATE_100_GBPS; 174 case 80: return IB_RATE_200_GBPS; 175 case 120: return IB_RATE_300_GBPS; 176 case 11: return IB_RATE_28_GBPS; 177 case 20: return IB_RATE_50_GBPS; 178 case 160: return IB_RATE_400_GBPS; 179 case 240: return IB_RATE_600_GBPS; 180 default: return IB_RATE_PORT_CURRENT; 181 } 182 } 183 EXPORT_SYMBOL(mult_to_ib_rate); 184 185 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate) 186 { 187 switch (rate) { 188 case IB_RATE_2_5_GBPS: return 2500; 189 case IB_RATE_5_GBPS: return 5000; 190 case IB_RATE_10_GBPS: return 10000; 191 case IB_RATE_20_GBPS: return 20000; 192 case IB_RATE_30_GBPS: return 30000; 193 case IB_RATE_40_GBPS: return 40000; 194 case IB_RATE_60_GBPS: return 60000; 195 case IB_RATE_80_GBPS: return 80000; 196 case IB_RATE_120_GBPS: return 120000; 197 case IB_RATE_14_GBPS: return 14062; 198 case IB_RATE_56_GBPS: return 56250; 199 case IB_RATE_112_GBPS: return 112500; 200 case IB_RATE_168_GBPS: return 168750; 201 case IB_RATE_25_GBPS: return 25781; 202 case IB_RATE_100_GBPS: return 103125; 203 case IB_RATE_200_GBPS: return 206250; 204 case IB_RATE_300_GBPS: return 309375; 205 case IB_RATE_28_GBPS: return 28125; 206 case IB_RATE_50_GBPS: return 53125; 207 case IB_RATE_400_GBPS: return 425000; 208 case IB_RATE_600_GBPS: return 637500; 209 default: return -1; 210 } 211 } 212 EXPORT_SYMBOL(ib_rate_to_mbps); 213 214 __attribute_const__ enum rdma_transport_type 215 rdma_node_get_transport(unsigned int node_type) 216 { 217 218 if (node_type == RDMA_NODE_USNIC) 219 return RDMA_TRANSPORT_USNIC; 220 if (node_type == RDMA_NODE_USNIC_UDP) 221 return RDMA_TRANSPORT_USNIC_UDP; 222 if (node_type == RDMA_NODE_RNIC) 223 return RDMA_TRANSPORT_IWARP; 224 if (node_type == RDMA_NODE_UNSPECIFIED) 225 return RDMA_TRANSPORT_UNSPECIFIED; 226 227 return RDMA_TRANSPORT_IB; 228 } 229 EXPORT_SYMBOL(rdma_node_get_transport); 230 231 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num) 232 { 233 enum rdma_transport_type lt; 234 if (device->ops.get_link_layer) 235 return device->ops.get_link_layer(device, port_num); 236 237 lt = rdma_node_get_transport(device->node_type); 238 if (lt == RDMA_TRANSPORT_IB) 239 return IB_LINK_LAYER_INFINIBAND; 240 241 return IB_LINK_LAYER_ETHERNET; 242 } 243 EXPORT_SYMBOL(rdma_port_get_link_layer); 244 245 /* Protection domains */ 246 247 /** 248 * ib_alloc_pd - Allocates an unused protection domain. 249 * @device: The device on which to allocate the protection domain. 250 * @flags: protection domain flags 251 * @caller: caller's build-time module name 252 * 253 * A protection domain object provides an association between QPs, shared 254 * receive queues, address handles, memory regions, and memory windows. 255 * 256 * Every PD has a local_dma_lkey which can be used as the lkey value for local 257 * memory operations. 258 */ 259 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags, 260 const char *caller) 261 { 262 struct ib_pd *pd; 263 int mr_access_flags = 0; 264 int ret; 265 266 pd = rdma_zalloc_drv_obj(device, ib_pd); 267 if (!pd) 268 return ERR_PTR(-ENOMEM); 269 270 pd->device = device; 271 pd->uobject = NULL; 272 pd->__internal_mr = NULL; 273 atomic_set(&pd->usecnt, 0); 274 pd->flags = flags; 275 276 pd->res.type = RDMA_RESTRACK_PD; 277 rdma_restrack_set_task(&pd->res, caller); 278 279 ret = device->ops.alloc_pd(pd, NULL); 280 if (ret) { 281 kfree(pd); 282 return ERR_PTR(ret); 283 } 284 rdma_restrack_kadd(&pd->res); 285 286 if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) 287 pd->local_dma_lkey = device->local_dma_lkey; 288 else 289 mr_access_flags |= IB_ACCESS_LOCAL_WRITE; 290 291 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) { 292 pr_warn("%s: enabling unsafe global rkey\n", caller); 293 mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE; 294 } 295 296 if (mr_access_flags) { 297 struct ib_mr *mr; 298 299 mr = pd->device->ops.get_dma_mr(pd, mr_access_flags); 300 if (IS_ERR(mr)) { 301 ib_dealloc_pd(pd); 302 return ERR_CAST(mr); 303 } 304 305 mr->device = pd->device; 306 mr->pd = pd; 307 mr->type = IB_MR_TYPE_DMA; 308 mr->uobject = NULL; 309 mr->need_inval = false; 310 311 pd->__internal_mr = mr; 312 313 if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) 314 pd->local_dma_lkey = pd->__internal_mr->lkey; 315 316 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) 317 pd->unsafe_global_rkey = pd->__internal_mr->rkey; 318 } 319 320 return pd; 321 } 322 EXPORT_SYMBOL(__ib_alloc_pd); 323 324 /** 325 * ib_dealloc_pd_user - Deallocates a protection domain. 326 * @pd: The protection domain to deallocate. 327 * @udata: Valid user data or NULL for kernel object 328 * 329 * It is an error to call this function while any resources in the pd still 330 * exist. The caller is responsible to synchronously destroy them and 331 * guarantee no new allocations will happen. 332 */ 333 void ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata) 334 { 335 int ret; 336 337 if (pd->__internal_mr) { 338 ret = pd->device->ops.dereg_mr(pd->__internal_mr, NULL); 339 WARN_ON(ret); 340 pd->__internal_mr = NULL; 341 } 342 343 /* uverbs manipulates usecnt with proper locking, while the kabi 344 requires the caller to guarantee we can't race here. */ 345 WARN_ON(atomic_read(&pd->usecnt)); 346 347 rdma_restrack_del(&pd->res); 348 pd->device->ops.dealloc_pd(pd, udata); 349 kfree(pd); 350 } 351 EXPORT_SYMBOL(ib_dealloc_pd_user); 352 353 /* Address handles */ 354 355 /** 356 * rdma_copy_ah_attr - Copy rdma ah attribute from source to destination. 357 * @dest: Pointer to destination ah_attr. Contents of the destination 358 * pointer is assumed to be invalid and attribute are overwritten. 359 * @src: Pointer to source ah_attr. 360 */ 361 void rdma_copy_ah_attr(struct rdma_ah_attr *dest, 362 const struct rdma_ah_attr *src) 363 { 364 *dest = *src; 365 if (dest->grh.sgid_attr) 366 rdma_hold_gid_attr(dest->grh.sgid_attr); 367 } 368 EXPORT_SYMBOL(rdma_copy_ah_attr); 369 370 /** 371 * rdma_replace_ah_attr - Replace valid ah_attr with new new one. 372 * @old: Pointer to existing ah_attr which needs to be replaced. 373 * old is assumed to be valid or zero'd 374 * @new: Pointer to the new ah_attr. 375 * 376 * rdma_replace_ah_attr() first releases any reference in the old ah_attr if 377 * old the ah_attr is valid; after that it copies the new attribute and holds 378 * the reference to the replaced ah_attr. 379 */ 380 void rdma_replace_ah_attr(struct rdma_ah_attr *old, 381 const struct rdma_ah_attr *new) 382 { 383 rdma_destroy_ah_attr(old); 384 *old = *new; 385 if (old->grh.sgid_attr) 386 rdma_hold_gid_attr(old->grh.sgid_attr); 387 } 388 EXPORT_SYMBOL(rdma_replace_ah_attr); 389 390 /** 391 * rdma_move_ah_attr - Move ah_attr pointed by source to destination. 392 * @dest: Pointer to destination ah_attr to copy to. 393 * dest is assumed to be valid or zero'd 394 * @src: Pointer to the new ah_attr. 395 * 396 * rdma_move_ah_attr() first releases any reference in the destination ah_attr 397 * if it is valid. This also transfers ownership of internal references from 398 * src to dest, making src invalid in the process. No new reference of the src 399 * ah_attr is taken. 400 */ 401 void rdma_move_ah_attr(struct rdma_ah_attr *dest, struct rdma_ah_attr *src) 402 { 403 rdma_destroy_ah_attr(dest); 404 *dest = *src; 405 src->grh.sgid_attr = NULL; 406 } 407 EXPORT_SYMBOL(rdma_move_ah_attr); 408 409 /* 410 * Validate that the rdma_ah_attr is valid for the device before passing it 411 * off to the driver. 412 */ 413 static int rdma_check_ah_attr(struct ib_device *device, 414 struct rdma_ah_attr *ah_attr) 415 { 416 if (!rdma_is_port_valid(device, ah_attr->port_num)) 417 return -EINVAL; 418 419 if ((rdma_is_grh_required(device, ah_attr->port_num) || 420 ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) && 421 !(ah_attr->ah_flags & IB_AH_GRH)) 422 return -EINVAL; 423 424 if (ah_attr->grh.sgid_attr) { 425 /* 426 * Make sure the passed sgid_attr is consistent with the 427 * parameters 428 */ 429 if (ah_attr->grh.sgid_attr->index != ah_attr->grh.sgid_index || 430 ah_attr->grh.sgid_attr->port_num != ah_attr->port_num) 431 return -EINVAL; 432 } 433 return 0; 434 } 435 436 /* 437 * If the ah requires a GRH then ensure that sgid_attr pointer is filled in. 438 * On success the caller is responsible to call rdma_unfill_sgid_attr(). 439 */ 440 static int rdma_fill_sgid_attr(struct ib_device *device, 441 struct rdma_ah_attr *ah_attr, 442 const struct ib_gid_attr **old_sgid_attr) 443 { 444 const struct ib_gid_attr *sgid_attr; 445 struct ib_global_route *grh; 446 int ret; 447 448 *old_sgid_attr = ah_attr->grh.sgid_attr; 449 450 ret = rdma_check_ah_attr(device, ah_attr); 451 if (ret) 452 return ret; 453 454 if (!(ah_attr->ah_flags & IB_AH_GRH)) 455 return 0; 456 457 grh = rdma_ah_retrieve_grh(ah_attr); 458 if (grh->sgid_attr) 459 return 0; 460 461 sgid_attr = 462 rdma_get_gid_attr(device, ah_attr->port_num, grh->sgid_index); 463 if (IS_ERR(sgid_attr)) 464 return PTR_ERR(sgid_attr); 465 466 /* Move ownerhip of the kref into the ah_attr */ 467 grh->sgid_attr = sgid_attr; 468 return 0; 469 } 470 471 static void rdma_unfill_sgid_attr(struct rdma_ah_attr *ah_attr, 472 const struct ib_gid_attr *old_sgid_attr) 473 { 474 /* 475 * Fill didn't change anything, the caller retains ownership of 476 * whatever it passed 477 */ 478 if (ah_attr->grh.sgid_attr == old_sgid_attr) 479 return; 480 481 /* 482 * Otherwise, we need to undo what rdma_fill_sgid_attr so the caller 483 * doesn't see any change in the rdma_ah_attr. If we get here 484 * old_sgid_attr is NULL. 485 */ 486 rdma_destroy_ah_attr(ah_attr); 487 } 488 489 static const struct ib_gid_attr * 490 rdma_update_sgid_attr(struct rdma_ah_attr *ah_attr, 491 const struct ib_gid_attr *old_attr) 492 { 493 if (old_attr) 494 rdma_put_gid_attr(old_attr); 495 if (ah_attr->ah_flags & IB_AH_GRH) { 496 rdma_hold_gid_attr(ah_attr->grh.sgid_attr); 497 return ah_attr->grh.sgid_attr; 498 } 499 return NULL; 500 } 501 502 static struct ib_ah *_rdma_create_ah(struct ib_pd *pd, 503 struct rdma_ah_attr *ah_attr, 504 u32 flags, 505 struct ib_udata *udata) 506 { 507 struct ib_device *device = pd->device; 508 struct ib_ah *ah; 509 int ret; 510 511 might_sleep_if(flags & RDMA_CREATE_AH_SLEEPABLE); 512 513 if (!device->ops.create_ah) 514 return ERR_PTR(-EOPNOTSUPP); 515 516 ah = rdma_zalloc_drv_obj_gfp( 517 device, ib_ah, 518 (flags & RDMA_CREATE_AH_SLEEPABLE) ? GFP_KERNEL : GFP_ATOMIC); 519 if (!ah) 520 return ERR_PTR(-ENOMEM); 521 522 ah->device = device; 523 ah->pd = pd; 524 ah->type = ah_attr->type; 525 ah->sgid_attr = rdma_update_sgid_attr(ah_attr, NULL); 526 527 ret = device->ops.create_ah(ah, ah_attr, flags, udata); 528 if (ret) { 529 kfree(ah); 530 return ERR_PTR(ret); 531 } 532 533 atomic_inc(&pd->usecnt); 534 return ah; 535 } 536 537 /** 538 * rdma_create_ah - Creates an address handle for the 539 * given address vector. 540 * @pd: The protection domain associated with the address handle. 541 * @ah_attr: The attributes of the address vector. 542 * @flags: Create address handle flags (see enum rdma_create_ah_flags). 543 * 544 * It returns 0 on success and returns appropriate error code on error. 545 * The address handle is used to reference a local or global destination 546 * in all UD QP post sends. 547 */ 548 struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr, 549 u32 flags) 550 { 551 const struct ib_gid_attr *old_sgid_attr; 552 struct ib_ah *ah; 553 int ret; 554 555 ret = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr); 556 if (ret) 557 return ERR_PTR(ret); 558 559 ah = _rdma_create_ah(pd, ah_attr, flags, NULL); 560 561 rdma_unfill_sgid_attr(ah_attr, old_sgid_attr); 562 return ah; 563 } 564 EXPORT_SYMBOL(rdma_create_ah); 565 566 /** 567 * rdma_create_user_ah - Creates an address handle for the 568 * given address vector. 569 * It resolves destination mac address for ah attribute of RoCE type. 570 * @pd: The protection domain associated with the address handle. 571 * @ah_attr: The attributes of the address vector. 572 * @udata: pointer to user's input output buffer information need by 573 * provider driver. 574 * 575 * It returns 0 on success and returns appropriate error code on error. 576 * The address handle is used to reference a local or global destination 577 * in all UD QP post sends. 578 */ 579 struct ib_ah *rdma_create_user_ah(struct ib_pd *pd, 580 struct rdma_ah_attr *ah_attr, 581 struct ib_udata *udata) 582 { 583 const struct ib_gid_attr *old_sgid_attr; 584 struct ib_ah *ah; 585 int err; 586 587 err = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr); 588 if (err) 589 return ERR_PTR(err); 590 591 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) { 592 err = ib_resolve_eth_dmac(pd->device, ah_attr); 593 if (err) { 594 ah = ERR_PTR(err); 595 goto out; 596 } 597 } 598 599 ah = _rdma_create_ah(pd, ah_attr, RDMA_CREATE_AH_SLEEPABLE, udata); 600 601 out: 602 rdma_unfill_sgid_attr(ah_attr, old_sgid_attr); 603 return ah; 604 } 605 EXPORT_SYMBOL(rdma_create_user_ah); 606 607 int ib_get_rdma_header_version(const union rdma_network_hdr *hdr) 608 { 609 const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh; 610 struct iphdr ip4h_checked; 611 const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh; 612 613 /* If it's IPv6, the version must be 6, otherwise, the first 614 * 20 bytes (before the IPv4 header) are garbled. 615 */ 616 if (ip6h->version != 6) 617 return (ip4h->version == 4) ? 4 : 0; 618 /* version may be 6 or 4 because the first 20 bytes could be garbled */ 619 620 /* RoCE v2 requires no options, thus header length 621 * must be 5 words 622 */ 623 if (ip4h->ihl != 5) 624 return 6; 625 626 /* Verify checksum. 627 * We can't write on scattered buffers so we need to copy to 628 * temp buffer. 629 */ 630 memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked)); 631 ip4h_checked.check = 0; 632 ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5); 633 /* if IPv4 header checksum is OK, believe it */ 634 if (ip4h->check == ip4h_checked.check) 635 return 4; 636 return 6; 637 } 638 EXPORT_SYMBOL(ib_get_rdma_header_version); 639 640 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device, 641 u8 port_num, 642 const struct ib_grh *grh) 643 { 644 int grh_version; 645 646 if (rdma_protocol_ib(device, port_num)) 647 return RDMA_NETWORK_IB; 648 649 grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh); 650 651 if (grh_version == 4) 652 return RDMA_NETWORK_IPV4; 653 654 if (grh->next_hdr == IPPROTO_UDP) 655 return RDMA_NETWORK_IPV6; 656 657 return RDMA_NETWORK_ROCE_V1; 658 } 659 660 struct find_gid_index_context { 661 u16 vlan_id; 662 enum ib_gid_type gid_type; 663 }; 664 665 static bool find_gid_index(const union ib_gid *gid, 666 const struct ib_gid_attr *gid_attr, 667 void *context) 668 { 669 struct find_gid_index_context *ctx = context; 670 u16 vlan_id = 0xffff; 671 int ret; 672 673 if (ctx->gid_type != gid_attr->gid_type) 674 return false; 675 676 ret = rdma_read_gid_l2_fields(gid_attr, &vlan_id, NULL); 677 if (ret) 678 return false; 679 680 return ctx->vlan_id == vlan_id; 681 } 682 683 static const struct ib_gid_attr * 684 get_sgid_attr_from_eth(struct ib_device *device, u8 port_num, 685 u16 vlan_id, const union ib_gid *sgid, 686 enum ib_gid_type gid_type) 687 { 688 struct find_gid_index_context context = {.vlan_id = vlan_id, 689 .gid_type = gid_type}; 690 691 return rdma_find_gid_by_filter(device, sgid, port_num, find_gid_index, 692 &context); 693 } 694 695 int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr, 696 enum rdma_network_type net_type, 697 union ib_gid *sgid, union ib_gid *dgid) 698 { 699 struct sockaddr_in src_in; 700 struct sockaddr_in dst_in; 701 __be32 src_saddr, dst_saddr; 702 703 if (!sgid || !dgid) 704 return -EINVAL; 705 706 if (net_type == RDMA_NETWORK_IPV4) { 707 memcpy(&src_in.sin_addr.s_addr, 708 &hdr->roce4grh.saddr, 4); 709 memcpy(&dst_in.sin_addr.s_addr, 710 &hdr->roce4grh.daddr, 4); 711 src_saddr = src_in.sin_addr.s_addr; 712 dst_saddr = dst_in.sin_addr.s_addr; 713 ipv6_addr_set_v4mapped(src_saddr, 714 (struct in6_addr *)sgid); 715 ipv6_addr_set_v4mapped(dst_saddr, 716 (struct in6_addr *)dgid); 717 return 0; 718 } else if (net_type == RDMA_NETWORK_IPV6 || 719 net_type == RDMA_NETWORK_IB) { 720 *dgid = hdr->ibgrh.dgid; 721 *sgid = hdr->ibgrh.sgid; 722 return 0; 723 } else { 724 return -EINVAL; 725 } 726 } 727 EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr); 728 729 /* Resolve destination mac address and hop limit for unicast destination 730 * GID entry, considering the source GID entry as well. 731 * ah_attribute must have have valid port_num, sgid_index. 732 */ 733 static int ib_resolve_unicast_gid_dmac(struct ib_device *device, 734 struct rdma_ah_attr *ah_attr) 735 { 736 struct ib_global_route *grh = rdma_ah_retrieve_grh(ah_attr); 737 const struct ib_gid_attr *sgid_attr = grh->sgid_attr; 738 int hop_limit = 0xff; 739 int ret = 0; 740 741 /* If destination is link local and source GID is RoCEv1, 742 * IP stack is not used. 743 */ 744 if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) && 745 sgid_attr->gid_type == IB_GID_TYPE_ROCE) { 746 rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw, 747 ah_attr->roce.dmac); 748 return ret; 749 } 750 751 ret = rdma_addr_find_l2_eth_by_grh(&sgid_attr->gid, &grh->dgid, 752 ah_attr->roce.dmac, 753 sgid_attr, &hop_limit); 754 755 grh->hop_limit = hop_limit; 756 return ret; 757 } 758 759 /* 760 * This function initializes address handle attributes from the incoming packet. 761 * Incoming packet has dgid of the receiver node on which this code is 762 * getting executed and, sgid contains the GID of the sender. 763 * 764 * When resolving mac address of destination, the arrived dgid is used 765 * as sgid and, sgid is used as dgid because sgid contains destinations 766 * GID whom to respond to. 767 * 768 * On success the caller is responsible to call rdma_destroy_ah_attr on the 769 * attr. 770 */ 771 int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num, 772 const struct ib_wc *wc, const struct ib_grh *grh, 773 struct rdma_ah_attr *ah_attr) 774 { 775 u32 flow_class; 776 int ret; 777 enum rdma_network_type net_type = RDMA_NETWORK_IB; 778 enum ib_gid_type gid_type = IB_GID_TYPE_IB; 779 const struct ib_gid_attr *sgid_attr; 780 int hoplimit = 0xff; 781 union ib_gid dgid; 782 union ib_gid sgid; 783 784 might_sleep(); 785 786 memset(ah_attr, 0, sizeof *ah_attr); 787 ah_attr->type = rdma_ah_find_type(device, port_num); 788 if (rdma_cap_eth_ah(device, port_num)) { 789 if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE) 790 net_type = wc->network_hdr_type; 791 else 792 net_type = ib_get_net_type_by_grh(device, port_num, grh); 793 gid_type = ib_network_to_gid_type(net_type); 794 } 795 ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type, 796 &sgid, &dgid); 797 if (ret) 798 return ret; 799 800 rdma_ah_set_sl(ah_attr, wc->sl); 801 rdma_ah_set_port_num(ah_attr, port_num); 802 803 if (rdma_protocol_roce(device, port_num)) { 804 u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ? 805 wc->vlan_id : 0xffff; 806 807 if (!(wc->wc_flags & IB_WC_GRH)) 808 return -EPROTOTYPE; 809 810 sgid_attr = get_sgid_attr_from_eth(device, port_num, 811 vlan_id, &dgid, 812 gid_type); 813 if (IS_ERR(sgid_attr)) 814 return PTR_ERR(sgid_attr); 815 816 flow_class = be32_to_cpu(grh->version_tclass_flow); 817 rdma_move_grh_sgid_attr(ah_attr, 818 &sgid, 819 flow_class & 0xFFFFF, 820 hoplimit, 821 (flow_class >> 20) & 0xFF, 822 sgid_attr); 823 824 ret = ib_resolve_unicast_gid_dmac(device, ah_attr); 825 if (ret) 826 rdma_destroy_ah_attr(ah_attr); 827 828 return ret; 829 } else { 830 rdma_ah_set_dlid(ah_attr, wc->slid); 831 rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits); 832 833 if ((wc->wc_flags & IB_WC_GRH) == 0) 834 return 0; 835 836 if (dgid.global.interface_id != 837 cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) { 838 sgid_attr = rdma_find_gid_by_port( 839 device, &dgid, IB_GID_TYPE_IB, port_num, NULL); 840 } else 841 sgid_attr = rdma_get_gid_attr(device, port_num, 0); 842 843 if (IS_ERR(sgid_attr)) 844 return PTR_ERR(sgid_attr); 845 flow_class = be32_to_cpu(grh->version_tclass_flow); 846 rdma_move_grh_sgid_attr(ah_attr, 847 &sgid, 848 flow_class & 0xFFFFF, 849 hoplimit, 850 (flow_class >> 20) & 0xFF, 851 sgid_attr); 852 853 return 0; 854 } 855 } 856 EXPORT_SYMBOL(ib_init_ah_attr_from_wc); 857 858 /** 859 * rdma_move_grh_sgid_attr - Sets the sgid attribute of GRH, taking ownership 860 * of the reference 861 * 862 * @attr: Pointer to AH attribute structure 863 * @dgid: Destination GID 864 * @flow_label: Flow label 865 * @hop_limit: Hop limit 866 * @traffic_class: traffic class 867 * @sgid_attr: Pointer to SGID attribute 868 * 869 * This takes ownership of the sgid_attr reference. The caller must ensure 870 * rdma_destroy_ah_attr() is called before destroying the rdma_ah_attr after 871 * calling this function. 872 */ 873 void rdma_move_grh_sgid_attr(struct rdma_ah_attr *attr, union ib_gid *dgid, 874 u32 flow_label, u8 hop_limit, u8 traffic_class, 875 const struct ib_gid_attr *sgid_attr) 876 { 877 rdma_ah_set_grh(attr, dgid, flow_label, sgid_attr->index, hop_limit, 878 traffic_class); 879 attr->grh.sgid_attr = sgid_attr; 880 } 881 EXPORT_SYMBOL(rdma_move_grh_sgid_attr); 882 883 /** 884 * rdma_destroy_ah_attr - Release reference to SGID attribute of 885 * ah attribute. 886 * @ah_attr: Pointer to ah attribute 887 * 888 * Release reference to the SGID attribute of the ah attribute if it is 889 * non NULL. It is safe to call this multiple times, and safe to call it on 890 * a zero initialized ah_attr. 891 */ 892 void rdma_destroy_ah_attr(struct rdma_ah_attr *ah_attr) 893 { 894 if (ah_attr->grh.sgid_attr) { 895 rdma_put_gid_attr(ah_attr->grh.sgid_attr); 896 ah_attr->grh.sgid_attr = NULL; 897 } 898 } 899 EXPORT_SYMBOL(rdma_destroy_ah_attr); 900 901 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc, 902 const struct ib_grh *grh, u8 port_num) 903 { 904 struct rdma_ah_attr ah_attr; 905 struct ib_ah *ah; 906 int ret; 907 908 ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr); 909 if (ret) 910 return ERR_PTR(ret); 911 912 ah = rdma_create_ah(pd, &ah_attr, RDMA_CREATE_AH_SLEEPABLE); 913 914 rdma_destroy_ah_attr(&ah_attr); 915 return ah; 916 } 917 EXPORT_SYMBOL(ib_create_ah_from_wc); 918 919 int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr) 920 { 921 const struct ib_gid_attr *old_sgid_attr; 922 int ret; 923 924 if (ah->type != ah_attr->type) 925 return -EINVAL; 926 927 ret = rdma_fill_sgid_attr(ah->device, ah_attr, &old_sgid_attr); 928 if (ret) 929 return ret; 930 931 ret = ah->device->ops.modify_ah ? 932 ah->device->ops.modify_ah(ah, ah_attr) : 933 -EOPNOTSUPP; 934 935 ah->sgid_attr = rdma_update_sgid_attr(ah_attr, ah->sgid_attr); 936 rdma_unfill_sgid_attr(ah_attr, old_sgid_attr); 937 return ret; 938 } 939 EXPORT_SYMBOL(rdma_modify_ah); 940 941 int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr) 942 { 943 ah_attr->grh.sgid_attr = NULL; 944 945 return ah->device->ops.query_ah ? 946 ah->device->ops.query_ah(ah, ah_attr) : 947 -EOPNOTSUPP; 948 } 949 EXPORT_SYMBOL(rdma_query_ah); 950 951 int rdma_destroy_ah_user(struct ib_ah *ah, u32 flags, struct ib_udata *udata) 952 { 953 const struct ib_gid_attr *sgid_attr = ah->sgid_attr; 954 struct ib_pd *pd; 955 956 might_sleep_if(flags & RDMA_DESTROY_AH_SLEEPABLE); 957 958 pd = ah->pd; 959 960 ah->device->ops.destroy_ah(ah, flags); 961 atomic_dec(&pd->usecnt); 962 if (sgid_attr) 963 rdma_put_gid_attr(sgid_attr); 964 965 kfree(ah); 966 return 0; 967 } 968 EXPORT_SYMBOL(rdma_destroy_ah_user); 969 970 /* Shared receive queues */ 971 972 struct ib_srq *ib_create_srq(struct ib_pd *pd, 973 struct ib_srq_init_attr *srq_init_attr) 974 { 975 struct ib_srq *srq; 976 int ret; 977 978 if (!pd->device->ops.create_srq) 979 return ERR_PTR(-EOPNOTSUPP); 980 981 srq = rdma_zalloc_drv_obj(pd->device, ib_srq); 982 if (!srq) 983 return ERR_PTR(-ENOMEM); 984 985 srq->device = pd->device; 986 srq->pd = pd; 987 srq->event_handler = srq_init_attr->event_handler; 988 srq->srq_context = srq_init_attr->srq_context; 989 srq->srq_type = srq_init_attr->srq_type; 990 991 if (ib_srq_has_cq(srq->srq_type)) { 992 srq->ext.cq = srq_init_attr->ext.cq; 993 atomic_inc(&srq->ext.cq->usecnt); 994 } 995 if (srq->srq_type == IB_SRQT_XRC) { 996 srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd; 997 atomic_inc(&srq->ext.xrc.xrcd->usecnt); 998 } 999 atomic_inc(&pd->usecnt); 1000 1001 ret = pd->device->ops.create_srq(srq, srq_init_attr, NULL); 1002 if (ret) { 1003 atomic_dec(&srq->pd->usecnt); 1004 if (srq->srq_type == IB_SRQT_XRC) 1005 atomic_dec(&srq->ext.xrc.xrcd->usecnt); 1006 if (ib_srq_has_cq(srq->srq_type)) 1007 atomic_dec(&srq->ext.cq->usecnt); 1008 kfree(srq); 1009 return ERR_PTR(ret); 1010 } 1011 1012 return srq; 1013 } 1014 EXPORT_SYMBOL(ib_create_srq); 1015 1016 int ib_modify_srq(struct ib_srq *srq, 1017 struct ib_srq_attr *srq_attr, 1018 enum ib_srq_attr_mask srq_attr_mask) 1019 { 1020 return srq->device->ops.modify_srq ? 1021 srq->device->ops.modify_srq(srq, srq_attr, srq_attr_mask, 1022 NULL) : -EOPNOTSUPP; 1023 } 1024 EXPORT_SYMBOL(ib_modify_srq); 1025 1026 int ib_query_srq(struct ib_srq *srq, 1027 struct ib_srq_attr *srq_attr) 1028 { 1029 return srq->device->ops.query_srq ? 1030 srq->device->ops.query_srq(srq, srq_attr) : -EOPNOTSUPP; 1031 } 1032 EXPORT_SYMBOL(ib_query_srq); 1033 1034 int ib_destroy_srq_user(struct ib_srq *srq, struct ib_udata *udata) 1035 { 1036 if (atomic_read(&srq->usecnt)) 1037 return -EBUSY; 1038 1039 srq->device->ops.destroy_srq(srq, udata); 1040 1041 atomic_dec(&srq->pd->usecnt); 1042 if (srq->srq_type == IB_SRQT_XRC) 1043 atomic_dec(&srq->ext.xrc.xrcd->usecnt); 1044 if (ib_srq_has_cq(srq->srq_type)) 1045 atomic_dec(&srq->ext.cq->usecnt); 1046 kfree(srq); 1047 1048 return 0; 1049 } 1050 EXPORT_SYMBOL(ib_destroy_srq_user); 1051 1052 /* Queue pairs */ 1053 1054 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context) 1055 { 1056 struct ib_qp *qp = context; 1057 unsigned long flags; 1058 1059 spin_lock_irqsave(&qp->device->qp_open_list_lock, flags); 1060 list_for_each_entry(event->element.qp, &qp->open_list, open_list) 1061 if (event->element.qp->event_handler) 1062 event->element.qp->event_handler(event, event->element.qp->qp_context); 1063 spin_unlock_irqrestore(&qp->device->qp_open_list_lock, flags); 1064 } 1065 1066 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp) 1067 { 1068 mutex_lock(&xrcd->tgt_qp_mutex); 1069 list_add(&qp->xrcd_list, &xrcd->tgt_qp_list); 1070 mutex_unlock(&xrcd->tgt_qp_mutex); 1071 } 1072 1073 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp, 1074 void (*event_handler)(struct ib_event *, void *), 1075 void *qp_context) 1076 { 1077 struct ib_qp *qp; 1078 unsigned long flags; 1079 int err; 1080 1081 qp = kzalloc(sizeof *qp, GFP_KERNEL); 1082 if (!qp) 1083 return ERR_PTR(-ENOMEM); 1084 1085 qp->real_qp = real_qp; 1086 err = ib_open_shared_qp_security(qp, real_qp->device); 1087 if (err) { 1088 kfree(qp); 1089 return ERR_PTR(err); 1090 } 1091 1092 qp->real_qp = real_qp; 1093 atomic_inc(&real_qp->usecnt); 1094 qp->device = real_qp->device; 1095 qp->event_handler = event_handler; 1096 qp->qp_context = qp_context; 1097 qp->qp_num = real_qp->qp_num; 1098 qp->qp_type = real_qp->qp_type; 1099 1100 spin_lock_irqsave(&real_qp->device->qp_open_list_lock, flags); 1101 list_add(&qp->open_list, &real_qp->open_list); 1102 spin_unlock_irqrestore(&real_qp->device->qp_open_list_lock, flags); 1103 1104 return qp; 1105 } 1106 1107 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd, 1108 struct ib_qp_open_attr *qp_open_attr) 1109 { 1110 struct ib_qp *qp, *real_qp; 1111 1112 if (qp_open_attr->qp_type != IB_QPT_XRC_TGT) 1113 return ERR_PTR(-EINVAL); 1114 1115 qp = ERR_PTR(-EINVAL); 1116 mutex_lock(&xrcd->tgt_qp_mutex); 1117 list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) { 1118 if (real_qp->qp_num == qp_open_attr->qp_num) { 1119 qp = __ib_open_qp(real_qp, qp_open_attr->event_handler, 1120 qp_open_attr->qp_context); 1121 break; 1122 } 1123 } 1124 mutex_unlock(&xrcd->tgt_qp_mutex); 1125 return qp; 1126 } 1127 EXPORT_SYMBOL(ib_open_qp); 1128 1129 static struct ib_qp *create_xrc_qp_user(struct ib_qp *qp, 1130 struct ib_qp_init_attr *qp_init_attr, 1131 struct ib_udata *udata) 1132 { 1133 struct ib_qp *real_qp = qp; 1134 1135 qp->event_handler = __ib_shared_qp_event_handler; 1136 qp->qp_context = qp; 1137 qp->pd = NULL; 1138 qp->send_cq = qp->recv_cq = NULL; 1139 qp->srq = NULL; 1140 qp->xrcd = qp_init_attr->xrcd; 1141 atomic_inc(&qp_init_attr->xrcd->usecnt); 1142 INIT_LIST_HEAD(&qp->open_list); 1143 1144 qp = __ib_open_qp(real_qp, qp_init_attr->event_handler, 1145 qp_init_attr->qp_context); 1146 if (IS_ERR(qp)) 1147 return qp; 1148 1149 __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp); 1150 return qp; 1151 } 1152 1153 struct ib_qp *ib_create_qp_user(struct ib_pd *pd, 1154 struct ib_qp_init_attr *qp_init_attr, 1155 struct ib_udata *udata) 1156 { 1157 struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device; 1158 struct ib_qp *qp; 1159 int ret; 1160 1161 if (qp_init_attr->rwq_ind_tbl && 1162 (qp_init_attr->recv_cq || 1163 qp_init_attr->srq || qp_init_attr->cap.max_recv_wr || 1164 qp_init_attr->cap.max_recv_sge)) 1165 return ERR_PTR(-EINVAL); 1166 1167 if ((qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN) && 1168 !(device->attrs.device_cap_flags & IB_DEVICE_INTEGRITY_HANDOVER)) 1169 return ERR_PTR(-EINVAL); 1170 1171 /* 1172 * If the callers is using the RDMA API calculate the resources 1173 * needed for the RDMA READ/WRITE operations. 1174 * 1175 * Note that these callers need to pass in a port number. 1176 */ 1177 if (qp_init_attr->cap.max_rdma_ctxs) 1178 rdma_rw_init_qp(device, qp_init_attr); 1179 1180 qp = _ib_create_qp(device, pd, qp_init_attr, NULL, NULL); 1181 if (IS_ERR(qp)) 1182 return qp; 1183 1184 ret = ib_create_qp_security(qp, device); 1185 if (ret) 1186 goto err; 1187 1188 if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) { 1189 struct ib_qp *xrc_qp = 1190 create_xrc_qp_user(qp, qp_init_attr, udata); 1191 1192 if (IS_ERR(xrc_qp)) { 1193 ret = PTR_ERR(xrc_qp); 1194 goto err; 1195 } 1196 return xrc_qp; 1197 } 1198 1199 qp->event_handler = qp_init_attr->event_handler; 1200 qp->qp_context = qp_init_attr->qp_context; 1201 if (qp_init_attr->qp_type == IB_QPT_XRC_INI) { 1202 qp->recv_cq = NULL; 1203 qp->srq = NULL; 1204 } else { 1205 qp->recv_cq = qp_init_attr->recv_cq; 1206 if (qp_init_attr->recv_cq) 1207 atomic_inc(&qp_init_attr->recv_cq->usecnt); 1208 qp->srq = qp_init_attr->srq; 1209 if (qp->srq) 1210 atomic_inc(&qp_init_attr->srq->usecnt); 1211 } 1212 1213 qp->send_cq = qp_init_attr->send_cq; 1214 qp->xrcd = NULL; 1215 1216 atomic_inc(&pd->usecnt); 1217 if (qp_init_attr->send_cq) 1218 atomic_inc(&qp_init_attr->send_cq->usecnt); 1219 if (qp_init_attr->rwq_ind_tbl) 1220 atomic_inc(&qp->rwq_ind_tbl->usecnt); 1221 1222 if (qp_init_attr->cap.max_rdma_ctxs) { 1223 ret = rdma_rw_init_mrs(qp, qp_init_attr); 1224 if (ret) 1225 goto err; 1226 } 1227 1228 /* 1229 * Note: all hw drivers guarantee that max_send_sge is lower than 1230 * the device RDMA WRITE SGE limit but not all hw drivers ensure that 1231 * max_send_sge <= max_sge_rd. 1232 */ 1233 qp->max_write_sge = qp_init_attr->cap.max_send_sge; 1234 qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge, 1235 device->attrs.max_sge_rd); 1236 if (qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN) 1237 qp->integrity_en = true; 1238 1239 return qp; 1240 1241 err: 1242 ib_destroy_qp(qp); 1243 return ERR_PTR(ret); 1244 1245 } 1246 EXPORT_SYMBOL(ib_create_qp_user); 1247 1248 static const struct { 1249 int valid; 1250 enum ib_qp_attr_mask req_param[IB_QPT_MAX]; 1251 enum ib_qp_attr_mask opt_param[IB_QPT_MAX]; 1252 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = { 1253 [IB_QPS_RESET] = { 1254 [IB_QPS_RESET] = { .valid = 1 }, 1255 [IB_QPS_INIT] = { 1256 .valid = 1, 1257 .req_param = { 1258 [IB_QPT_UD] = (IB_QP_PKEY_INDEX | 1259 IB_QP_PORT | 1260 IB_QP_QKEY), 1261 [IB_QPT_RAW_PACKET] = IB_QP_PORT, 1262 [IB_QPT_UC] = (IB_QP_PKEY_INDEX | 1263 IB_QP_PORT | 1264 IB_QP_ACCESS_FLAGS), 1265 [IB_QPT_RC] = (IB_QP_PKEY_INDEX | 1266 IB_QP_PORT | 1267 IB_QP_ACCESS_FLAGS), 1268 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX | 1269 IB_QP_PORT | 1270 IB_QP_ACCESS_FLAGS), 1271 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX | 1272 IB_QP_PORT | 1273 IB_QP_ACCESS_FLAGS), 1274 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | 1275 IB_QP_QKEY), 1276 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | 1277 IB_QP_QKEY), 1278 } 1279 }, 1280 }, 1281 [IB_QPS_INIT] = { 1282 [IB_QPS_RESET] = { .valid = 1 }, 1283 [IB_QPS_ERR] = { .valid = 1 }, 1284 [IB_QPS_INIT] = { 1285 .valid = 1, 1286 .opt_param = { 1287 [IB_QPT_UD] = (IB_QP_PKEY_INDEX | 1288 IB_QP_PORT | 1289 IB_QP_QKEY), 1290 [IB_QPT_UC] = (IB_QP_PKEY_INDEX | 1291 IB_QP_PORT | 1292 IB_QP_ACCESS_FLAGS), 1293 [IB_QPT_RC] = (IB_QP_PKEY_INDEX | 1294 IB_QP_PORT | 1295 IB_QP_ACCESS_FLAGS), 1296 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX | 1297 IB_QP_PORT | 1298 IB_QP_ACCESS_FLAGS), 1299 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX | 1300 IB_QP_PORT | 1301 IB_QP_ACCESS_FLAGS), 1302 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | 1303 IB_QP_QKEY), 1304 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | 1305 IB_QP_QKEY), 1306 } 1307 }, 1308 [IB_QPS_RTR] = { 1309 .valid = 1, 1310 .req_param = { 1311 [IB_QPT_UC] = (IB_QP_AV | 1312 IB_QP_PATH_MTU | 1313 IB_QP_DEST_QPN | 1314 IB_QP_RQ_PSN), 1315 [IB_QPT_RC] = (IB_QP_AV | 1316 IB_QP_PATH_MTU | 1317 IB_QP_DEST_QPN | 1318 IB_QP_RQ_PSN | 1319 IB_QP_MAX_DEST_RD_ATOMIC | 1320 IB_QP_MIN_RNR_TIMER), 1321 [IB_QPT_XRC_INI] = (IB_QP_AV | 1322 IB_QP_PATH_MTU | 1323 IB_QP_DEST_QPN | 1324 IB_QP_RQ_PSN), 1325 [IB_QPT_XRC_TGT] = (IB_QP_AV | 1326 IB_QP_PATH_MTU | 1327 IB_QP_DEST_QPN | 1328 IB_QP_RQ_PSN | 1329 IB_QP_MAX_DEST_RD_ATOMIC | 1330 IB_QP_MIN_RNR_TIMER), 1331 }, 1332 .opt_param = { 1333 [IB_QPT_UD] = (IB_QP_PKEY_INDEX | 1334 IB_QP_QKEY), 1335 [IB_QPT_UC] = (IB_QP_ALT_PATH | 1336 IB_QP_ACCESS_FLAGS | 1337 IB_QP_PKEY_INDEX), 1338 [IB_QPT_RC] = (IB_QP_ALT_PATH | 1339 IB_QP_ACCESS_FLAGS | 1340 IB_QP_PKEY_INDEX), 1341 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH | 1342 IB_QP_ACCESS_FLAGS | 1343 IB_QP_PKEY_INDEX), 1344 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH | 1345 IB_QP_ACCESS_FLAGS | 1346 IB_QP_PKEY_INDEX), 1347 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | 1348 IB_QP_QKEY), 1349 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | 1350 IB_QP_QKEY), 1351 }, 1352 }, 1353 }, 1354 [IB_QPS_RTR] = { 1355 [IB_QPS_RESET] = { .valid = 1 }, 1356 [IB_QPS_ERR] = { .valid = 1 }, 1357 [IB_QPS_RTS] = { 1358 .valid = 1, 1359 .req_param = { 1360 [IB_QPT_UD] = IB_QP_SQ_PSN, 1361 [IB_QPT_UC] = IB_QP_SQ_PSN, 1362 [IB_QPT_RC] = (IB_QP_TIMEOUT | 1363 IB_QP_RETRY_CNT | 1364 IB_QP_RNR_RETRY | 1365 IB_QP_SQ_PSN | 1366 IB_QP_MAX_QP_RD_ATOMIC), 1367 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT | 1368 IB_QP_RETRY_CNT | 1369 IB_QP_RNR_RETRY | 1370 IB_QP_SQ_PSN | 1371 IB_QP_MAX_QP_RD_ATOMIC), 1372 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT | 1373 IB_QP_SQ_PSN), 1374 [IB_QPT_SMI] = IB_QP_SQ_PSN, 1375 [IB_QPT_GSI] = IB_QP_SQ_PSN, 1376 }, 1377 .opt_param = { 1378 [IB_QPT_UD] = (IB_QP_CUR_STATE | 1379 IB_QP_QKEY), 1380 [IB_QPT_UC] = (IB_QP_CUR_STATE | 1381 IB_QP_ALT_PATH | 1382 IB_QP_ACCESS_FLAGS | 1383 IB_QP_PATH_MIG_STATE), 1384 [IB_QPT_RC] = (IB_QP_CUR_STATE | 1385 IB_QP_ALT_PATH | 1386 IB_QP_ACCESS_FLAGS | 1387 IB_QP_MIN_RNR_TIMER | 1388 IB_QP_PATH_MIG_STATE), 1389 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | 1390 IB_QP_ALT_PATH | 1391 IB_QP_ACCESS_FLAGS | 1392 IB_QP_PATH_MIG_STATE), 1393 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | 1394 IB_QP_ALT_PATH | 1395 IB_QP_ACCESS_FLAGS | 1396 IB_QP_MIN_RNR_TIMER | 1397 IB_QP_PATH_MIG_STATE), 1398 [IB_QPT_SMI] = (IB_QP_CUR_STATE | 1399 IB_QP_QKEY), 1400 [IB_QPT_GSI] = (IB_QP_CUR_STATE | 1401 IB_QP_QKEY), 1402 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT, 1403 } 1404 } 1405 }, 1406 [IB_QPS_RTS] = { 1407 [IB_QPS_RESET] = { .valid = 1 }, 1408 [IB_QPS_ERR] = { .valid = 1 }, 1409 [IB_QPS_RTS] = { 1410 .valid = 1, 1411 .opt_param = { 1412 [IB_QPT_UD] = (IB_QP_CUR_STATE | 1413 IB_QP_QKEY), 1414 [IB_QPT_UC] = (IB_QP_CUR_STATE | 1415 IB_QP_ACCESS_FLAGS | 1416 IB_QP_ALT_PATH | 1417 IB_QP_PATH_MIG_STATE), 1418 [IB_QPT_RC] = (IB_QP_CUR_STATE | 1419 IB_QP_ACCESS_FLAGS | 1420 IB_QP_ALT_PATH | 1421 IB_QP_PATH_MIG_STATE | 1422 IB_QP_MIN_RNR_TIMER), 1423 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | 1424 IB_QP_ACCESS_FLAGS | 1425 IB_QP_ALT_PATH | 1426 IB_QP_PATH_MIG_STATE), 1427 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | 1428 IB_QP_ACCESS_FLAGS | 1429 IB_QP_ALT_PATH | 1430 IB_QP_PATH_MIG_STATE | 1431 IB_QP_MIN_RNR_TIMER), 1432 [IB_QPT_SMI] = (IB_QP_CUR_STATE | 1433 IB_QP_QKEY), 1434 [IB_QPT_GSI] = (IB_QP_CUR_STATE | 1435 IB_QP_QKEY), 1436 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT, 1437 } 1438 }, 1439 [IB_QPS_SQD] = { 1440 .valid = 1, 1441 .opt_param = { 1442 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY, 1443 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY, 1444 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY, 1445 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY, 1446 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */ 1447 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY, 1448 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY 1449 } 1450 }, 1451 }, 1452 [IB_QPS_SQD] = { 1453 [IB_QPS_RESET] = { .valid = 1 }, 1454 [IB_QPS_ERR] = { .valid = 1 }, 1455 [IB_QPS_RTS] = { 1456 .valid = 1, 1457 .opt_param = { 1458 [IB_QPT_UD] = (IB_QP_CUR_STATE | 1459 IB_QP_QKEY), 1460 [IB_QPT_UC] = (IB_QP_CUR_STATE | 1461 IB_QP_ALT_PATH | 1462 IB_QP_ACCESS_FLAGS | 1463 IB_QP_PATH_MIG_STATE), 1464 [IB_QPT_RC] = (IB_QP_CUR_STATE | 1465 IB_QP_ALT_PATH | 1466 IB_QP_ACCESS_FLAGS | 1467 IB_QP_MIN_RNR_TIMER | 1468 IB_QP_PATH_MIG_STATE), 1469 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | 1470 IB_QP_ALT_PATH | 1471 IB_QP_ACCESS_FLAGS | 1472 IB_QP_PATH_MIG_STATE), 1473 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | 1474 IB_QP_ALT_PATH | 1475 IB_QP_ACCESS_FLAGS | 1476 IB_QP_MIN_RNR_TIMER | 1477 IB_QP_PATH_MIG_STATE), 1478 [IB_QPT_SMI] = (IB_QP_CUR_STATE | 1479 IB_QP_QKEY), 1480 [IB_QPT_GSI] = (IB_QP_CUR_STATE | 1481 IB_QP_QKEY), 1482 } 1483 }, 1484 [IB_QPS_SQD] = { 1485 .valid = 1, 1486 .opt_param = { 1487 [IB_QPT_UD] = (IB_QP_PKEY_INDEX | 1488 IB_QP_QKEY), 1489 [IB_QPT_UC] = (IB_QP_AV | 1490 IB_QP_ALT_PATH | 1491 IB_QP_ACCESS_FLAGS | 1492 IB_QP_PKEY_INDEX | 1493 IB_QP_PATH_MIG_STATE), 1494 [IB_QPT_RC] = (IB_QP_PORT | 1495 IB_QP_AV | 1496 IB_QP_TIMEOUT | 1497 IB_QP_RETRY_CNT | 1498 IB_QP_RNR_RETRY | 1499 IB_QP_MAX_QP_RD_ATOMIC | 1500 IB_QP_MAX_DEST_RD_ATOMIC | 1501 IB_QP_ALT_PATH | 1502 IB_QP_ACCESS_FLAGS | 1503 IB_QP_PKEY_INDEX | 1504 IB_QP_MIN_RNR_TIMER | 1505 IB_QP_PATH_MIG_STATE), 1506 [IB_QPT_XRC_INI] = (IB_QP_PORT | 1507 IB_QP_AV | 1508 IB_QP_TIMEOUT | 1509 IB_QP_RETRY_CNT | 1510 IB_QP_RNR_RETRY | 1511 IB_QP_MAX_QP_RD_ATOMIC | 1512 IB_QP_ALT_PATH | 1513 IB_QP_ACCESS_FLAGS | 1514 IB_QP_PKEY_INDEX | 1515 IB_QP_PATH_MIG_STATE), 1516 [IB_QPT_XRC_TGT] = (IB_QP_PORT | 1517 IB_QP_AV | 1518 IB_QP_TIMEOUT | 1519 IB_QP_MAX_DEST_RD_ATOMIC | 1520 IB_QP_ALT_PATH | 1521 IB_QP_ACCESS_FLAGS | 1522 IB_QP_PKEY_INDEX | 1523 IB_QP_MIN_RNR_TIMER | 1524 IB_QP_PATH_MIG_STATE), 1525 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | 1526 IB_QP_QKEY), 1527 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | 1528 IB_QP_QKEY), 1529 } 1530 } 1531 }, 1532 [IB_QPS_SQE] = { 1533 [IB_QPS_RESET] = { .valid = 1 }, 1534 [IB_QPS_ERR] = { .valid = 1 }, 1535 [IB_QPS_RTS] = { 1536 .valid = 1, 1537 .opt_param = { 1538 [IB_QPT_UD] = (IB_QP_CUR_STATE | 1539 IB_QP_QKEY), 1540 [IB_QPT_UC] = (IB_QP_CUR_STATE | 1541 IB_QP_ACCESS_FLAGS), 1542 [IB_QPT_SMI] = (IB_QP_CUR_STATE | 1543 IB_QP_QKEY), 1544 [IB_QPT_GSI] = (IB_QP_CUR_STATE | 1545 IB_QP_QKEY), 1546 } 1547 } 1548 }, 1549 [IB_QPS_ERR] = { 1550 [IB_QPS_RESET] = { .valid = 1 }, 1551 [IB_QPS_ERR] = { .valid = 1 } 1552 } 1553 }; 1554 1555 bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state, 1556 enum ib_qp_type type, enum ib_qp_attr_mask mask) 1557 { 1558 enum ib_qp_attr_mask req_param, opt_param; 1559 1560 if (mask & IB_QP_CUR_STATE && 1561 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS && 1562 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE) 1563 return false; 1564 1565 if (!qp_state_table[cur_state][next_state].valid) 1566 return false; 1567 1568 req_param = qp_state_table[cur_state][next_state].req_param[type]; 1569 opt_param = qp_state_table[cur_state][next_state].opt_param[type]; 1570 1571 if ((mask & req_param) != req_param) 1572 return false; 1573 1574 if (mask & ~(req_param | opt_param | IB_QP_STATE)) 1575 return false; 1576 1577 return true; 1578 } 1579 EXPORT_SYMBOL(ib_modify_qp_is_ok); 1580 1581 /** 1582 * ib_resolve_eth_dmac - Resolve destination mac address 1583 * @device: Device to consider 1584 * @ah_attr: address handle attribute which describes the 1585 * source and destination parameters 1586 * ib_resolve_eth_dmac() resolves destination mac address and L3 hop limit It 1587 * returns 0 on success or appropriate error code. It initializes the 1588 * necessary ah_attr fields when call is successful. 1589 */ 1590 static int ib_resolve_eth_dmac(struct ib_device *device, 1591 struct rdma_ah_attr *ah_attr) 1592 { 1593 int ret = 0; 1594 1595 if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) { 1596 if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) { 1597 __be32 addr = 0; 1598 1599 memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4); 1600 ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac); 1601 } else { 1602 ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw, 1603 (char *)ah_attr->roce.dmac); 1604 } 1605 } else { 1606 ret = ib_resolve_unicast_gid_dmac(device, ah_attr); 1607 } 1608 return ret; 1609 } 1610 1611 static bool is_qp_type_connected(const struct ib_qp *qp) 1612 { 1613 return (qp->qp_type == IB_QPT_UC || 1614 qp->qp_type == IB_QPT_RC || 1615 qp->qp_type == IB_QPT_XRC_INI || 1616 qp->qp_type == IB_QPT_XRC_TGT); 1617 } 1618 1619 /** 1620 * IB core internal function to perform QP attributes modification. 1621 */ 1622 static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr, 1623 int attr_mask, struct ib_udata *udata) 1624 { 1625 u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port; 1626 const struct ib_gid_attr *old_sgid_attr_av; 1627 const struct ib_gid_attr *old_sgid_attr_alt_av; 1628 int ret; 1629 1630 if (attr_mask & IB_QP_AV) { 1631 ret = rdma_fill_sgid_attr(qp->device, &attr->ah_attr, 1632 &old_sgid_attr_av); 1633 if (ret) 1634 return ret; 1635 } 1636 if (attr_mask & IB_QP_ALT_PATH) { 1637 /* 1638 * FIXME: This does not track the migration state, so if the 1639 * user loads a new alternate path after the HW has migrated 1640 * from primary->alternate we will keep the wrong 1641 * references. This is OK for IB because the reference 1642 * counting does not serve any functional purpose. 1643 */ 1644 ret = rdma_fill_sgid_attr(qp->device, &attr->alt_ah_attr, 1645 &old_sgid_attr_alt_av); 1646 if (ret) 1647 goto out_av; 1648 1649 /* 1650 * Today the core code can only handle alternate paths and APM 1651 * for IB. Ban them in roce mode. 1652 */ 1653 if (!(rdma_protocol_ib(qp->device, 1654 attr->alt_ah_attr.port_num) && 1655 rdma_protocol_ib(qp->device, port))) { 1656 ret = EINVAL; 1657 goto out; 1658 } 1659 } 1660 1661 /* 1662 * If the user provided the qp_attr then we have to resolve it. Kernel 1663 * users have to provide already resolved rdma_ah_attr's 1664 */ 1665 if (udata && (attr_mask & IB_QP_AV) && 1666 attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE && 1667 is_qp_type_connected(qp)) { 1668 ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr); 1669 if (ret) 1670 goto out; 1671 } 1672 1673 if (rdma_ib_or_roce(qp->device, port)) { 1674 if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) { 1675 dev_warn(&qp->device->dev, 1676 "%s rq_psn overflow, masking to 24 bits\n", 1677 __func__); 1678 attr->rq_psn &= 0xffffff; 1679 } 1680 1681 if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) { 1682 dev_warn(&qp->device->dev, 1683 " %s sq_psn overflow, masking to 24 bits\n", 1684 __func__); 1685 attr->sq_psn &= 0xffffff; 1686 } 1687 } 1688 1689 /* 1690 * Bind this qp to a counter automatically based on the rdma counter 1691 * rules. This only set in RST2INIT with port specified 1692 */ 1693 if (!qp->counter && (attr_mask & IB_QP_PORT) && 1694 ((attr_mask & IB_QP_STATE) && attr->qp_state == IB_QPS_INIT)) 1695 rdma_counter_bind_qp_auto(qp, attr->port_num); 1696 1697 ret = ib_security_modify_qp(qp, attr, attr_mask, udata); 1698 if (ret) 1699 goto out; 1700 1701 if (attr_mask & IB_QP_PORT) 1702 qp->port = attr->port_num; 1703 if (attr_mask & IB_QP_AV) 1704 qp->av_sgid_attr = 1705 rdma_update_sgid_attr(&attr->ah_attr, qp->av_sgid_attr); 1706 if (attr_mask & IB_QP_ALT_PATH) 1707 qp->alt_path_sgid_attr = rdma_update_sgid_attr( 1708 &attr->alt_ah_attr, qp->alt_path_sgid_attr); 1709 1710 out: 1711 if (attr_mask & IB_QP_ALT_PATH) 1712 rdma_unfill_sgid_attr(&attr->alt_ah_attr, old_sgid_attr_alt_av); 1713 out_av: 1714 if (attr_mask & IB_QP_AV) 1715 rdma_unfill_sgid_attr(&attr->ah_attr, old_sgid_attr_av); 1716 return ret; 1717 } 1718 1719 /** 1720 * ib_modify_qp_with_udata - Modifies the attributes for the specified QP. 1721 * @ib_qp: The QP to modify. 1722 * @attr: On input, specifies the QP attributes to modify. On output, 1723 * the current values of selected QP attributes are returned. 1724 * @attr_mask: A bit-mask used to specify which attributes of the QP 1725 * are being modified. 1726 * @udata: pointer to user's input output buffer information 1727 * are being modified. 1728 * It returns 0 on success and returns appropriate error code on error. 1729 */ 1730 int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr, 1731 int attr_mask, struct ib_udata *udata) 1732 { 1733 return _ib_modify_qp(ib_qp->real_qp, attr, attr_mask, udata); 1734 } 1735 EXPORT_SYMBOL(ib_modify_qp_with_udata); 1736 1737 int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width) 1738 { 1739 int rc; 1740 u32 netdev_speed; 1741 struct net_device *netdev; 1742 struct ethtool_link_ksettings lksettings; 1743 1744 if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET) 1745 return -EINVAL; 1746 1747 netdev = ib_device_get_netdev(dev, port_num); 1748 if (!netdev) 1749 return -ENODEV; 1750 1751 rtnl_lock(); 1752 rc = __ethtool_get_link_ksettings(netdev, &lksettings); 1753 rtnl_unlock(); 1754 1755 dev_put(netdev); 1756 1757 if (!rc) { 1758 netdev_speed = lksettings.base.speed; 1759 } else { 1760 netdev_speed = SPEED_1000; 1761 pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name, 1762 netdev_speed); 1763 } 1764 1765 if (netdev_speed <= SPEED_1000) { 1766 *width = IB_WIDTH_1X; 1767 *speed = IB_SPEED_SDR; 1768 } else if (netdev_speed <= SPEED_10000) { 1769 *width = IB_WIDTH_1X; 1770 *speed = IB_SPEED_FDR10; 1771 } else if (netdev_speed <= SPEED_20000) { 1772 *width = IB_WIDTH_4X; 1773 *speed = IB_SPEED_DDR; 1774 } else if (netdev_speed <= SPEED_25000) { 1775 *width = IB_WIDTH_1X; 1776 *speed = IB_SPEED_EDR; 1777 } else if (netdev_speed <= SPEED_40000) { 1778 *width = IB_WIDTH_4X; 1779 *speed = IB_SPEED_FDR10; 1780 } else { 1781 *width = IB_WIDTH_4X; 1782 *speed = IB_SPEED_EDR; 1783 } 1784 1785 return 0; 1786 } 1787 EXPORT_SYMBOL(ib_get_eth_speed); 1788 1789 int ib_modify_qp(struct ib_qp *qp, 1790 struct ib_qp_attr *qp_attr, 1791 int qp_attr_mask) 1792 { 1793 return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL); 1794 } 1795 EXPORT_SYMBOL(ib_modify_qp); 1796 1797 int ib_query_qp(struct ib_qp *qp, 1798 struct ib_qp_attr *qp_attr, 1799 int qp_attr_mask, 1800 struct ib_qp_init_attr *qp_init_attr) 1801 { 1802 qp_attr->ah_attr.grh.sgid_attr = NULL; 1803 qp_attr->alt_ah_attr.grh.sgid_attr = NULL; 1804 1805 return qp->device->ops.query_qp ? 1806 qp->device->ops.query_qp(qp->real_qp, qp_attr, qp_attr_mask, 1807 qp_init_attr) : -EOPNOTSUPP; 1808 } 1809 EXPORT_SYMBOL(ib_query_qp); 1810 1811 int ib_close_qp(struct ib_qp *qp) 1812 { 1813 struct ib_qp *real_qp; 1814 unsigned long flags; 1815 1816 real_qp = qp->real_qp; 1817 if (real_qp == qp) 1818 return -EINVAL; 1819 1820 spin_lock_irqsave(&real_qp->device->qp_open_list_lock, flags); 1821 list_del(&qp->open_list); 1822 spin_unlock_irqrestore(&real_qp->device->qp_open_list_lock, flags); 1823 1824 atomic_dec(&real_qp->usecnt); 1825 if (qp->qp_sec) 1826 ib_close_shared_qp_security(qp->qp_sec); 1827 kfree(qp); 1828 1829 return 0; 1830 } 1831 EXPORT_SYMBOL(ib_close_qp); 1832 1833 static int __ib_destroy_shared_qp(struct ib_qp *qp) 1834 { 1835 struct ib_xrcd *xrcd; 1836 struct ib_qp *real_qp; 1837 int ret; 1838 1839 real_qp = qp->real_qp; 1840 xrcd = real_qp->xrcd; 1841 1842 mutex_lock(&xrcd->tgt_qp_mutex); 1843 ib_close_qp(qp); 1844 if (atomic_read(&real_qp->usecnt) == 0) 1845 list_del(&real_qp->xrcd_list); 1846 else 1847 real_qp = NULL; 1848 mutex_unlock(&xrcd->tgt_qp_mutex); 1849 1850 if (real_qp) { 1851 ret = ib_destroy_qp(real_qp); 1852 if (!ret) 1853 atomic_dec(&xrcd->usecnt); 1854 else 1855 __ib_insert_xrcd_qp(xrcd, real_qp); 1856 } 1857 1858 return 0; 1859 } 1860 1861 int ib_destroy_qp_user(struct ib_qp *qp, struct ib_udata *udata) 1862 { 1863 const struct ib_gid_attr *alt_path_sgid_attr = qp->alt_path_sgid_attr; 1864 const struct ib_gid_attr *av_sgid_attr = qp->av_sgid_attr; 1865 struct ib_pd *pd; 1866 struct ib_cq *scq, *rcq; 1867 struct ib_srq *srq; 1868 struct ib_rwq_ind_table *ind_tbl; 1869 struct ib_qp_security *sec; 1870 int ret; 1871 1872 WARN_ON_ONCE(qp->mrs_used > 0); 1873 1874 if (atomic_read(&qp->usecnt)) 1875 return -EBUSY; 1876 1877 if (qp->real_qp != qp) 1878 return __ib_destroy_shared_qp(qp); 1879 1880 pd = qp->pd; 1881 scq = qp->send_cq; 1882 rcq = qp->recv_cq; 1883 srq = qp->srq; 1884 ind_tbl = qp->rwq_ind_tbl; 1885 sec = qp->qp_sec; 1886 if (sec) 1887 ib_destroy_qp_security_begin(sec); 1888 1889 if (!qp->uobject) 1890 rdma_rw_cleanup_mrs(qp); 1891 1892 rdma_counter_unbind_qp(qp, true); 1893 rdma_restrack_del(&qp->res); 1894 ret = qp->device->ops.destroy_qp(qp, udata); 1895 if (!ret) { 1896 if (alt_path_sgid_attr) 1897 rdma_put_gid_attr(alt_path_sgid_attr); 1898 if (av_sgid_attr) 1899 rdma_put_gid_attr(av_sgid_attr); 1900 if (pd) 1901 atomic_dec(&pd->usecnt); 1902 if (scq) 1903 atomic_dec(&scq->usecnt); 1904 if (rcq) 1905 atomic_dec(&rcq->usecnt); 1906 if (srq) 1907 atomic_dec(&srq->usecnt); 1908 if (ind_tbl) 1909 atomic_dec(&ind_tbl->usecnt); 1910 if (sec) 1911 ib_destroy_qp_security_end(sec); 1912 } else { 1913 if (sec) 1914 ib_destroy_qp_security_abort(sec); 1915 } 1916 1917 return ret; 1918 } 1919 EXPORT_SYMBOL(ib_destroy_qp_user); 1920 1921 /* Completion queues */ 1922 1923 struct ib_cq *__ib_create_cq(struct ib_device *device, 1924 ib_comp_handler comp_handler, 1925 void (*event_handler)(struct ib_event *, void *), 1926 void *cq_context, 1927 const struct ib_cq_init_attr *cq_attr, 1928 const char *caller) 1929 { 1930 struct ib_cq *cq; 1931 int ret; 1932 1933 cq = rdma_zalloc_drv_obj(device, ib_cq); 1934 if (!cq) 1935 return ERR_PTR(-ENOMEM); 1936 1937 cq->device = device; 1938 cq->uobject = NULL; 1939 cq->comp_handler = comp_handler; 1940 cq->event_handler = event_handler; 1941 cq->cq_context = cq_context; 1942 atomic_set(&cq->usecnt, 0); 1943 cq->res.type = RDMA_RESTRACK_CQ; 1944 rdma_restrack_set_task(&cq->res, caller); 1945 1946 ret = device->ops.create_cq(cq, cq_attr, NULL); 1947 if (ret) { 1948 kfree(cq); 1949 return ERR_PTR(ret); 1950 } 1951 1952 rdma_restrack_kadd(&cq->res); 1953 return cq; 1954 } 1955 EXPORT_SYMBOL(__ib_create_cq); 1956 1957 int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period) 1958 { 1959 return cq->device->ops.modify_cq ? 1960 cq->device->ops.modify_cq(cq, cq_count, 1961 cq_period) : -EOPNOTSUPP; 1962 } 1963 EXPORT_SYMBOL(rdma_set_cq_moderation); 1964 1965 int ib_destroy_cq_user(struct ib_cq *cq, struct ib_udata *udata) 1966 { 1967 if (atomic_read(&cq->usecnt)) 1968 return -EBUSY; 1969 1970 rdma_restrack_del(&cq->res); 1971 cq->device->ops.destroy_cq(cq, udata); 1972 kfree(cq); 1973 return 0; 1974 } 1975 EXPORT_SYMBOL(ib_destroy_cq_user); 1976 1977 int ib_resize_cq(struct ib_cq *cq, int cqe) 1978 { 1979 return cq->device->ops.resize_cq ? 1980 cq->device->ops.resize_cq(cq, cqe, NULL) : -EOPNOTSUPP; 1981 } 1982 EXPORT_SYMBOL(ib_resize_cq); 1983 1984 /* Memory regions */ 1985 1986 struct ib_mr *ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length, 1987 u64 virt_addr, int access_flags) 1988 { 1989 struct ib_mr *mr; 1990 1991 if (access_flags & IB_ACCESS_ON_DEMAND) { 1992 if (!(pd->device->attrs.device_cap_flags & 1993 IB_DEVICE_ON_DEMAND_PAGING)) { 1994 pr_debug("ODP support not available\n"); 1995 return ERR_PTR(-EINVAL); 1996 } 1997 } 1998 1999 mr = pd->device->ops.reg_user_mr(pd, start, length, virt_addr, 2000 access_flags, NULL); 2001 2002 if (IS_ERR(mr)) 2003 return mr; 2004 2005 mr->device = pd->device; 2006 mr->pd = pd; 2007 mr->dm = NULL; 2008 atomic_inc(&pd->usecnt); 2009 mr->res.type = RDMA_RESTRACK_MR; 2010 rdma_restrack_kadd(&mr->res); 2011 2012 return mr; 2013 } 2014 EXPORT_SYMBOL(ib_reg_user_mr); 2015 2016 int ib_advise_mr(struct ib_pd *pd, enum ib_uverbs_advise_mr_advice advice, 2017 u32 flags, struct ib_sge *sg_list, u32 num_sge) 2018 { 2019 if (!pd->device->ops.advise_mr) 2020 return -EOPNOTSUPP; 2021 2022 return pd->device->ops.advise_mr(pd, advice, flags, sg_list, num_sge, 2023 NULL); 2024 } 2025 EXPORT_SYMBOL(ib_advise_mr); 2026 2027 int ib_dereg_mr_user(struct ib_mr *mr, struct ib_udata *udata) 2028 { 2029 struct ib_pd *pd = mr->pd; 2030 struct ib_dm *dm = mr->dm; 2031 struct ib_sig_attrs *sig_attrs = mr->sig_attrs; 2032 int ret; 2033 2034 trace_mr_dereg(mr); 2035 rdma_restrack_del(&mr->res); 2036 ret = mr->device->ops.dereg_mr(mr, udata); 2037 if (!ret) { 2038 atomic_dec(&pd->usecnt); 2039 if (dm) 2040 atomic_dec(&dm->usecnt); 2041 kfree(sig_attrs); 2042 } 2043 2044 return ret; 2045 } 2046 EXPORT_SYMBOL(ib_dereg_mr_user); 2047 2048 /** 2049 * ib_alloc_mr_user() - Allocates a memory region 2050 * @pd: protection domain associated with the region 2051 * @mr_type: memory region type 2052 * @max_num_sg: maximum sg entries available for registration. 2053 * @udata: user data or null for kernel objects 2054 * 2055 * Notes: 2056 * Memory registeration page/sg lists must not exceed max_num_sg. 2057 * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed 2058 * max_num_sg * used_page_size. 2059 * 2060 */ 2061 struct ib_mr *ib_alloc_mr_user(struct ib_pd *pd, enum ib_mr_type mr_type, 2062 u32 max_num_sg, struct ib_udata *udata) 2063 { 2064 struct ib_mr *mr; 2065 2066 if (!pd->device->ops.alloc_mr) { 2067 mr = ERR_PTR(-EOPNOTSUPP); 2068 goto out; 2069 } 2070 2071 if (mr_type == IB_MR_TYPE_INTEGRITY) { 2072 WARN_ON_ONCE(1); 2073 mr = ERR_PTR(-EINVAL); 2074 goto out; 2075 } 2076 2077 mr = pd->device->ops.alloc_mr(pd, mr_type, max_num_sg, udata); 2078 if (!IS_ERR(mr)) { 2079 mr->device = pd->device; 2080 mr->pd = pd; 2081 mr->dm = NULL; 2082 mr->uobject = NULL; 2083 atomic_inc(&pd->usecnt); 2084 mr->need_inval = false; 2085 mr->res.type = RDMA_RESTRACK_MR; 2086 rdma_restrack_kadd(&mr->res); 2087 mr->type = mr_type; 2088 mr->sig_attrs = NULL; 2089 } 2090 2091 out: 2092 trace_mr_alloc(pd, mr_type, max_num_sg, mr); 2093 return mr; 2094 } 2095 EXPORT_SYMBOL(ib_alloc_mr_user); 2096 2097 /** 2098 * ib_alloc_mr_integrity() - Allocates an integrity memory region 2099 * @pd: protection domain associated with the region 2100 * @max_num_data_sg: maximum data sg entries available for registration 2101 * @max_num_meta_sg: maximum metadata sg entries available for 2102 * registration 2103 * 2104 * Notes: 2105 * Memory registration page/sg lists must not exceed max_num_sg, 2106 * also the integrity page/sg lists must not exceed max_num_meta_sg. 2107 * 2108 */ 2109 struct ib_mr *ib_alloc_mr_integrity(struct ib_pd *pd, 2110 u32 max_num_data_sg, 2111 u32 max_num_meta_sg) 2112 { 2113 struct ib_mr *mr; 2114 struct ib_sig_attrs *sig_attrs; 2115 2116 if (!pd->device->ops.alloc_mr_integrity || 2117 !pd->device->ops.map_mr_sg_pi) { 2118 mr = ERR_PTR(-EOPNOTSUPP); 2119 goto out; 2120 } 2121 2122 if (!max_num_meta_sg) { 2123 mr = ERR_PTR(-EINVAL); 2124 goto out; 2125 } 2126 2127 sig_attrs = kzalloc(sizeof(struct ib_sig_attrs), GFP_KERNEL); 2128 if (!sig_attrs) { 2129 mr = ERR_PTR(-ENOMEM); 2130 goto out; 2131 } 2132 2133 mr = pd->device->ops.alloc_mr_integrity(pd, max_num_data_sg, 2134 max_num_meta_sg); 2135 if (IS_ERR(mr)) { 2136 kfree(sig_attrs); 2137 goto out; 2138 } 2139 2140 mr->device = pd->device; 2141 mr->pd = pd; 2142 mr->dm = NULL; 2143 mr->uobject = NULL; 2144 atomic_inc(&pd->usecnt); 2145 mr->need_inval = false; 2146 mr->res.type = RDMA_RESTRACK_MR; 2147 rdma_restrack_kadd(&mr->res); 2148 mr->type = IB_MR_TYPE_INTEGRITY; 2149 mr->sig_attrs = sig_attrs; 2150 2151 out: 2152 trace_mr_integ_alloc(pd, max_num_data_sg, max_num_meta_sg, mr); 2153 return mr; 2154 } 2155 EXPORT_SYMBOL(ib_alloc_mr_integrity); 2156 2157 /* "Fast" memory regions */ 2158 2159 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd, 2160 int mr_access_flags, 2161 struct ib_fmr_attr *fmr_attr) 2162 { 2163 struct ib_fmr *fmr; 2164 2165 if (!pd->device->ops.alloc_fmr) 2166 return ERR_PTR(-EOPNOTSUPP); 2167 2168 fmr = pd->device->ops.alloc_fmr(pd, mr_access_flags, fmr_attr); 2169 if (!IS_ERR(fmr)) { 2170 fmr->device = pd->device; 2171 fmr->pd = pd; 2172 atomic_inc(&pd->usecnt); 2173 } 2174 2175 return fmr; 2176 } 2177 EXPORT_SYMBOL(ib_alloc_fmr); 2178 2179 int ib_unmap_fmr(struct list_head *fmr_list) 2180 { 2181 struct ib_fmr *fmr; 2182 2183 if (list_empty(fmr_list)) 2184 return 0; 2185 2186 fmr = list_entry(fmr_list->next, struct ib_fmr, list); 2187 return fmr->device->ops.unmap_fmr(fmr_list); 2188 } 2189 EXPORT_SYMBOL(ib_unmap_fmr); 2190 2191 int ib_dealloc_fmr(struct ib_fmr *fmr) 2192 { 2193 struct ib_pd *pd; 2194 int ret; 2195 2196 pd = fmr->pd; 2197 ret = fmr->device->ops.dealloc_fmr(fmr); 2198 if (!ret) 2199 atomic_dec(&pd->usecnt); 2200 2201 return ret; 2202 } 2203 EXPORT_SYMBOL(ib_dealloc_fmr); 2204 2205 /* Multicast groups */ 2206 2207 static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid) 2208 { 2209 struct ib_qp_init_attr init_attr = {}; 2210 struct ib_qp_attr attr = {}; 2211 int num_eth_ports = 0; 2212 int port; 2213 2214 /* If QP state >= init, it is assigned to a port and we can check this 2215 * port only. 2216 */ 2217 if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) { 2218 if (attr.qp_state >= IB_QPS_INIT) { 2219 if (rdma_port_get_link_layer(qp->device, attr.port_num) != 2220 IB_LINK_LAYER_INFINIBAND) 2221 return true; 2222 goto lid_check; 2223 } 2224 } 2225 2226 /* Can't get a quick answer, iterate over all ports */ 2227 for (port = 0; port < qp->device->phys_port_cnt; port++) 2228 if (rdma_port_get_link_layer(qp->device, port) != 2229 IB_LINK_LAYER_INFINIBAND) 2230 num_eth_ports++; 2231 2232 /* If we have at lease one Ethernet port, RoCE annex declares that 2233 * multicast LID should be ignored. We can't tell at this step if the 2234 * QP belongs to an IB or Ethernet port. 2235 */ 2236 if (num_eth_ports) 2237 return true; 2238 2239 /* If all the ports are IB, we can check according to IB spec. */ 2240 lid_check: 2241 return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) || 2242 lid == be16_to_cpu(IB_LID_PERMISSIVE)); 2243 } 2244 2245 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid) 2246 { 2247 int ret; 2248 2249 if (!qp->device->ops.attach_mcast) 2250 return -EOPNOTSUPP; 2251 2252 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) || 2253 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid)) 2254 return -EINVAL; 2255 2256 ret = qp->device->ops.attach_mcast(qp, gid, lid); 2257 if (!ret) 2258 atomic_inc(&qp->usecnt); 2259 return ret; 2260 } 2261 EXPORT_SYMBOL(ib_attach_mcast); 2262 2263 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid) 2264 { 2265 int ret; 2266 2267 if (!qp->device->ops.detach_mcast) 2268 return -EOPNOTSUPP; 2269 2270 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) || 2271 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid)) 2272 return -EINVAL; 2273 2274 ret = qp->device->ops.detach_mcast(qp, gid, lid); 2275 if (!ret) 2276 atomic_dec(&qp->usecnt); 2277 return ret; 2278 } 2279 EXPORT_SYMBOL(ib_detach_mcast); 2280 2281 struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller) 2282 { 2283 struct ib_xrcd *xrcd; 2284 2285 if (!device->ops.alloc_xrcd) 2286 return ERR_PTR(-EOPNOTSUPP); 2287 2288 xrcd = device->ops.alloc_xrcd(device, NULL); 2289 if (!IS_ERR(xrcd)) { 2290 xrcd->device = device; 2291 xrcd->inode = NULL; 2292 atomic_set(&xrcd->usecnt, 0); 2293 mutex_init(&xrcd->tgt_qp_mutex); 2294 INIT_LIST_HEAD(&xrcd->tgt_qp_list); 2295 } 2296 2297 return xrcd; 2298 } 2299 EXPORT_SYMBOL(__ib_alloc_xrcd); 2300 2301 int ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata) 2302 { 2303 struct ib_qp *qp; 2304 int ret; 2305 2306 if (atomic_read(&xrcd->usecnt)) 2307 return -EBUSY; 2308 2309 while (!list_empty(&xrcd->tgt_qp_list)) { 2310 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list); 2311 ret = ib_destroy_qp(qp); 2312 if (ret) 2313 return ret; 2314 } 2315 mutex_destroy(&xrcd->tgt_qp_mutex); 2316 2317 return xrcd->device->ops.dealloc_xrcd(xrcd, udata); 2318 } 2319 EXPORT_SYMBOL(ib_dealloc_xrcd); 2320 2321 /** 2322 * ib_create_wq - Creates a WQ associated with the specified protection 2323 * domain. 2324 * @pd: The protection domain associated with the WQ. 2325 * @wq_attr: A list of initial attributes required to create the 2326 * WQ. If WQ creation succeeds, then the attributes are updated to 2327 * the actual capabilities of the created WQ. 2328 * 2329 * wq_attr->max_wr and wq_attr->max_sge determine 2330 * the requested size of the WQ, and set to the actual values allocated 2331 * on return. 2332 * If ib_create_wq() succeeds, then max_wr and max_sge will always be 2333 * at least as large as the requested values. 2334 */ 2335 struct ib_wq *ib_create_wq(struct ib_pd *pd, 2336 struct ib_wq_init_attr *wq_attr) 2337 { 2338 struct ib_wq *wq; 2339 2340 if (!pd->device->ops.create_wq) 2341 return ERR_PTR(-EOPNOTSUPP); 2342 2343 wq = pd->device->ops.create_wq(pd, wq_attr, NULL); 2344 if (!IS_ERR(wq)) { 2345 wq->event_handler = wq_attr->event_handler; 2346 wq->wq_context = wq_attr->wq_context; 2347 wq->wq_type = wq_attr->wq_type; 2348 wq->cq = wq_attr->cq; 2349 wq->device = pd->device; 2350 wq->pd = pd; 2351 wq->uobject = NULL; 2352 atomic_inc(&pd->usecnt); 2353 atomic_inc(&wq_attr->cq->usecnt); 2354 atomic_set(&wq->usecnt, 0); 2355 } 2356 return wq; 2357 } 2358 EXPORT_SYMBOL(ib_create_wq); 2359 2360 /** 2361 * ib_destroy_wq - Destroys the specified user WQ. 2362 * @wq: The WQ to destroy. 2363 * @udata: Valid user data 2364 */ 2365 int ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata) 2366 { 2367 struct ib_cq *cq = wq->cq; 2368 struct ib_pd *pd = wq->pd; 2369 2370 if (atomic_read(&wq->usecnt)) 2371 return -EBUSY; 2372 2373 wq->device->ops.destroy_wq(wq, udata); 2374 atomic_dec(&pd->usecnt); 2375 atomic_dec(&cq->usecnt); 2376 2377 return 0; 2378 } 2379 EXPORT_SYMBOL(ib_destroy_wq); 2380 2381 /** 2382 * ib_modify_wq - Modifies the specified WQ. 2383 * @wq: The WQ to modify. 2384 * @wq_attr: On input, specifies the WQ attributes to modify. 2385 * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ 2386 * are being modified. 2387 * On output, the current values of selected WQ attributes are returned. 2388 */ 2389 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr, 2390 u32 wq_attr_mask) 2391 { 2392 int err; 2393 2394 if (!wq->device->ops.modify_wq) 2395 return -EOPNOTSUPP; 2396 2397 err = wq->device->ops.modify_wq(wq, wq_attr, wq_attr_mask, NULL); 2398 return err; 2399 } 2400 EXPORT_SYMBOL(ib_modify_wq); 2401 2402 /* 2403 * ib_create_rwq_ind_table - Creates a RQ Indirection Table. 2404 * @device: The device on which to create the rwq indirection table. 2405 * @ib_rwq_ind_table_init_attr: A list of initial attributes required to 2406 * create the Indirection Table. 2407 * 2408 * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less 2409 * than the created ib_rwq_ind_table object and the caller is responsible 2410 * for its memory allocation/free. 2411 */ 2412 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device, 2413 struct ib_rwq_ind_table_init_attr *init_attr) 2414 { 2415 struct ib_rwq_ind_table *rwq_ind_table; 2416 int i; 2417 u32 table_size; 2418 2419 if (!device->ops.create_rwq_ind_table) 2420 return ERR_PTR(-EOPNOTSUPP); 2421 2422 table_size = (1 << init_attr->log_ind_tbl_size); 2423 rwq_ind_table = device->ops.create_rwq_ind_table(device, 2424 init_attr, NULL); 2425 if (IS_ERR(rwq_ind_table)) 2426 return rwq_ind_table; 2427 2428 rwq_ind_table->ind_tbl = init_attr->ind_tbl; 2429 rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size; 2430 rwq_ind_table->device = device; 2431 rwq_ind_table->uobject = NULL; 2432 atomic_set(&rwq_ind_table->usecnt, 0); 2433 2434 for (i = 0; i < table_size; i++) 2435 atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt); 2436 2437 return rwq_ind_table; 2438 } 2439 EXPORT_SYMBOL(ib_create_rwq_ind_table); 2440 2441 /* 2442 * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table. 2443 * @wq_ind_table: The Indirection Table to destroy. 2444 */ 2445 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table) 2446 { 2447 int err, i; 2448 u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size); 2449 struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl; 2450 2451 if (atomic_read(&rwq_ind_table->usecnt)) 2452 return -EBUSY; 2453 2454 err = rwq_ind_table->device->ops.destroy_rwq_ind_table(rwq_ind_table); 2455 if (!err) { 2456 for (i = 0; i < table_size; i++) 2457 atomic_dec(&ind_tbl[i]->usecnt); 2458 } 2459 2460 return err; 2461 } 2462 EXPORT_SYMBOL(ib_destroy_rwq_ind_table); 2463 2464 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask, 2465 struct ib_mr_status *mr_status) 2466 { 2467 if (!mr->device->ops.check_mr_status) 2468 return -EOPNOTSUPP; 2469 2470 return mr->device->ops.check_mr_status(mr, check_mask, mr_status); 2471 } 2472 EXPORT_SYMBOL(ib_check_mr_status); 2473 2474 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port, 2475 int state) 2476 { 2477 if (!device->ops.set_vf_link_state) 2478 return -EOPNOTSUPP; 2479 2480 return device->ops.set_vf_link_state(device, vf, port, state); 2481 } 2482 EXPORT_SYMBOL(ib_set_vf_link_state); 2483 2484 int ib_get_vf_config(struct ib_device *device, int vf, u8 port, 2485 struct ifla_vf_info *info) 2486 { 2487 if (!device->ops.get_vf_config) 2488 return -EOPNOTSUPP; 2489 2490 return device->ops.get_vf_config(device, vf, port, info); 2491 } 2492 EXPORT_SYMBOL(ib_get_vf_config); 2493 2494 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port, 2495 struct ifla_vf_stats *stats) 2496 { 2497 if (!device->ops.get_vf_stats) 2498 return -EOPNOTSUPP; 2499 2500 return device->ops.get_vf_stats(device, vf, port, stats); 2501 } 2502 EXPORT_SYMBOL(ib_get_vf_stats); 2503 2504 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid, 2505 int type) 2506 { 2507 if (!device->ops.set_vf_guid) 2508 return -EOPNOTSUPP; 2509 2510 return device->ops.set_vf_guid(device, vf, port, guid, type); 2511 } 2512 EXPORT_SYMBOL(ib_set_vf_guid); 2513 2514 int ib_get_vf_guid(struct ib_device *device, int vf, u8 port, 2515 struct ifla_vf_guid *node_guid, 2516 struct ifla_vf_guid *port_guid) 2517 { 2518 if (!device->ops.get_vf_guid) 2519 return -EOPNOTSUPP; 2520 2521 return device->ops.get_vf_guid(device, vf, port, node_guid, port_guid); 2522 } 2523 EXPORT_SYMBOL(ib_get_vf_guid); 2524 /** 2525 * ib_map_mr_sg_pi() - Map the dma mapped SG lists for PI (protection 2526 * information) and set an appropriate memory region for registration. 2527 * @mr: memory region 2528 * @data_sg: dma mapped scatterlist for data 2529 * @data_sg_nents: number of entries in data_sg 2530 * @data_sg_offset: offset in bytes into data_sg 2531 * @meta_sg: dma mapped scatterlist for metadata 2532 * @meta_sg_nents: number of entries in meta_sg 2533 * @meta_sg_offset: offset in bytes into meta_sg 2534 * @page_size: page vector desired page size 2535 * 2536 * Constraints: 2537 * - The MR must be allocated with type IB_MR_TYPE_INTEGRITY. 2538 * 2539 * Return: 0 on success. 2540 * 2541 * After this completes successfully, the memory region 2542 * is ready for registration. 2543 */ 2544 int ib_map_mr_sg_pi(struct ib_mr *mr, struct scatterlist *data_sg, 2545 int data_sg_nents, unsigned int *data_sg_offset, 2546 struct scatterlist *meta_sg, int meta_sg_nents, 2547 unsigned int *meta_sg_offset, unsigned int page_size) 2548 { 2549 if (unlikely(!mr->device->ops.map_mr_sg_pi || 2550 WARN_ON_ONCE(mr->type != IB_MR_TYPE_INTEGRITY))) 2551 return -EOPNOTSUPP; 2552 2553 mr->page_size = page_size; 2554 2555 return mr->device->ops.map_mr_sg_pi(mr, data_sg, data_sg_nents, 2556 data_sg_offset, meta_sg, 2557 meta_sg_nents, meta_sg_offset); 2558 } 2559 EXPORT_SYMBOL(ib_map_mr_sg_pi); 2560 2561 /** 2562 * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list 2563 * and set it the memory region. 2564 * @mr: memory region 2565 * @sg: dma mapped scatterlist 2566 * @sg_nents: number of entries in sg 2567 * @sg_offset: offset in bytes into sg 2568 * @page_size: page vector desired page size 2569 * 2570 * Constraints: 2571 * - The first sg element is allowed to have an offset. 2572 * - Each sg element must either be aligned to page_size or virtually 2573 * contiguous to the previous element. In case an sg element has a 2574 * non-contiguous offset, the mapping prefix will not include it. 2575 * - The last sg element is allowed to have length less than page_size. 2576 * - If sg_nents total byte length exceeds the mr max_num_sge * page_size 2577 * then only max_num_sg entries will be mapped. 2578 * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these 2579 * constraints holds and the page_size argument is ignored. 2580 * 2581 * Returns the number of sg elements that were mapped to the memory region. 2582 * 2583 * After this completes successfully, the memory region 2584 * is ready for registration. 2585 */ 2586 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents, 2587 unsigned int *sg_offset, unsigned int page_size) 2588 { 2589 if (unlikely(!mr->device->ops.map_mr_sg)) 2590 return -EOPNOTSUPP; 2591 2592 mr->page_size = page_size; 2593 2594 return mr->device->ops.map_mr_sg(mr, sg, sg_nents, sg_offset); 2595 } 2596 EXPORT_SYMBOL(ib_map_mr_sg); 2597 2598 /** 2599 * ib_sg_to_pages() - Convert the largest prefix of a sg list 2600 * to a page vector 2601 * @mr: memory region 2602 * @sgl: dma mapped scatterlist 2603 * @sg_nents: number of entries in sg 2604 * @sg_offset_p: IN: start offset in bytes into sg 2605 * OUT: offset in bytes for element n of the sg of the first 2606 * byte that has not been processed where n is the return 2607 * value of this function. 2608 * @set_page: driver page assignment function pointer 2609 * 2610 * Core service helper for drivers to convert the largest 2611 * prefix of given sg list to a page vector. The sg list 2612 * prefix converted is the prefix that meet the requirements 2613 * of ib_map_mr_sg. 2614 * 2615 * Returns the number of sg elements that were assigned to 2616 * a page vector. 2617 */ 2618 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents, 2619 unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64)) 2620 { 2621 struct scatterlist *sg; 2622 u64 last_end_dma_addr = 0; 2623 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0; 2624 unsigned int last_page_off = 0; 2625 u64 page_mask = ~((u64)mr->page_size - 1); 2626 int i, ret; 2627 2628 if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0]))) 2629 return -EINVAL; 2630 2631 mr->iova = sg_dma_address(&sgl[0]) + sg_offset; 2632 mr->length = 0; 2633 2634 for_each_sg(sgl, sg, sg_nents, i) { 2635 u64 dma_addr = sg_dma_address(sg) + sg_offset; 2636 u64 prev_addr = dma_addr; 2637 unsigned int dma_len = sg_dma_len(sg) - sg_offset; 2638 u64 end_dma_addr = dma_addr + dma_len; 2639 u64 page_addr = dma_addr & page_mask; 2640 2641 /* 2642 * For the second and later elements, check whether either the 2643 * end of element i-1 or the start of element i is not aligned 2644 * on a page boundary. 2645 */ 2646 if (i && (last_page_off != 0 || page_addr != dma_addr)) { 2647 /* Stop mapping if there is a gap. */ 2648 if (last_end_dma_addr != dma_addr) 2649 break; 2650 2651 /* 2652 * Coalesce this element with the last. If it is small 2653 * enough just update mr->length. Otherwise start 2654 * mapping from the next page. 2655 */ 2656 goto next_page; 2657 } 2658 2659 do { 2660 ret = set_page(mr, page_addr); 2661 if (unlikely(ret < 0)) { 2662 sg_offset = prev_addr - sg_dma_address(sg); 2663 mr->length += prev_addr - dma_addr; 2664 if (sg_offset_p) 2665 *sg_offset_p = sg_offset; 2666 return i || sg_offset ? i : ret; 2667 } 2668 prev_addr = page_addr; 2669 next_page: 2670 page_addr += mr->page_size; 2671 } while (page_addr < end_dma_addr); 2672 2673 mr->length += dma_len; 2674 last_end_dma_addr = end_dma_addr; 2675 last_page_off = end_dma_addr & ~page_mask; 2676 2677 sg_offset = 0; 2678 } 2679 2680 if (sg_offset_p) 2681 *sg_offset_p = 0; 2682 return i; 2683 } 2684 EXPORT_SYMBOL(ib_sg_to_pages); 2685 2686 struct ib_drain_cqe { 2687 struct ib_cqe cqe; 2688 struct completion done; 2689 }; 2690 2691 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc) 2692 { 2693 struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe, 2694 cqe); 2695 2696 complete(&cqe->done); 2697 } 2698 2699 /* 2700 * Post a WR and block until its completion is reaped for the SQ. 2701 */ 2702 static void __ib_drain_sq(struct ib_qp *qp) 2703 { 2704 struct ib_cq *cq = qp->send_cq; 2705 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR }; 2706 struct ib_drain_cqe sdrain; 2707 struct ib_rdma_wr swr = { 2708 .wr = { 2709 .next = NULL, 2710 { .wr_cqe = &sdrain.cqe, }, 2711 .opcode = IB_WR_RDMA_WRITE, 2712 }, 2713 }; 2714 int ret; 2715 2716 ret = ib_modify_qp(qp, &attr, IB_QP_STATE); 2717 if (ret) { 2718 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret); 2719 return; 2720 } 2721 2722 sdrain.cqe.done = ib_drain_qp_done; 2723 init_completion(&sdrain.done); 2724 2725 ret = ib_post_send(qp, &swr.wr, NULL); 2726 if (ret) { 2727 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret); 2728 return; 2729 } 2730 2731 if (cq->poll_ctx == IB_POLL_DIRECT) 2732 while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0) 2733 ib_process_cq_direct(cq, -1); 2734 else 2735 wait_for_completion(&sdrain.done); 2736 } 2737 2738 /* 2739 * Post a WR and block until its completion is reaped for the RQ. 2740 */ 2741 static void __ib_drain_rq(struct ib_qp *qp) 2742 { 2743 struct ib_cq *cq = qp->recv_cq; 2744 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR }; 2745 struct ib_drain_cqe rdrain; 2746 struct ib_recv_wr rwr = {}; 2747 int ret; 2748 2749 ret = ib_modify_qp(qp, &attr, IB_QP_STATE); 2750 if (ret) { 2751 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret); 2752 return; 2753 } 2754 2755 rwr.wr_cqe = &rdrain.cqe; 2756 rdrain.cqe.done = ib_drain_qp_done; 2757 init_completion(&rdrain.done); 2758 2759 ret = ib_post_recv(qp, &rwr, NULL); 2760 if (ret) { 2761 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret); 2762 return; 2763 } 2764 2765 if (cq->poll_ctx == IB_POLL_DIRECT) 2766 while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0) 2767 ib_process_cq_direct(cq, -1); 2768 else 2769 wait_for_completion(&rdrain.done); 2770 } 2771 2772 /** 2773 * ib_drain_sq() - Block until all SQ CQEs have been consumed by the 2774 * application. 2775 * @qp: queue pair to drain 2776 * 2777 * If the device has a provider-specific drain function, then 2778 * call that. Otherwise call the generic drain function 2779 * __ib_drain_sq(). 2780 * 2781 * The caller must: 2782 * 2783 * ensure there is room in the CQ and SQ for the drain work request and 2784 * completion. 2785 * 2786 * allocate the CQ using ib_alloc_cq(). 2787 * 2788 * ensure that there are no other contexts that are posting WRs concurrently. 2789 * Otherwise the drain is not guaranteed. 2790 */ 2791 void ib_drain_sq(struct ib_qp *qp) 2792 { 2793 if (qp->device->ops.drain_sq) 2794 qp->device->ops.drain_sq(qp); 2795 else 2796 __ib_drain_sq(qp); 2797 trace_cq_drain_complete(qp->send_cq); 2798 } 2799 EXPORT_SYMBOL(ib_drain_sq); 2800 2801 /** 2802 * ib_drain_rq() - Block until all RQ CQEs have been consumed by the 2803 * application. 2804 * @qp: queue pair to drain 2805 * 2806 * If the device has a provider-specific drain function, then 2807 * call that. Otherwise call the generic drain function 2808 * __ib_drain_rq(). 2809 * 2810 * The caller must: 2811 * 2812 * ensure there is room in the CQ and RQ for the drain work request and 2813 * completion. 2814 * 2815 * allocate the CQ using ib_alloc_cq(). 2816 * 2817 * ensure that there are no other contexts that are posting WRs concurrently. 2818 * Otherwise the drain is not guaranteed. 2819 */ 2820 void ib_drain_rq(struct ib_qp *qp) 2821 { 2822 if (qp->device->ops.drain_rq) 2823 qp->device->ops.drain_rq(qp); 2824 else 2825 __ib_drain_rq(qp); 2826 trace_cq_drain_complete(qp->recv_cq); 2827 } 2828 EXPORT_SYMBOL(ib_drain_rq); 2829 2830 /** 2831 * ib_drain_qp() - Block until all CQEs have been consumed by the 2832 * application on both the RQ and SQ. 2833 * @qp: queue pair to drain 2834 * 2835 * The caller must: 2836 * 2837 * ensure there is room in the CQ(s), SQ, and RQ for drain work requests 2838 * and completions. 2839 * 2840 * allocate the CQs using ib_alloc_cq(). 2841 * 2842 * ensure that there are no other contexts that are posting WRs concurrently. 2843 * Otherwise the drain is not guaranteed. 2844 */ 2845 void ib_drain_qp(struct ib_qp *qp) 2846 { 2847 ib_drain_sq(qp); 2848 if (!qp->srq) 2849 ib_drain_rq(qp); 2850 } 2851 EXPORT_SYMBOL(ib_drain_qp); 2852 2853 struct net_device *rdma_alloc_netdev(struct ib_device *device, u8 port_num, 2854 enum rdma_netdev_t type, const char *name, 2855 unsigned char name_assign_type, 2856 void (*setup)(struct net_device *)) 2857 { 2858 struct rdma_netdev_alloc_params params; 2859 struct net_device *netdev; 2860 int rc; 2861 2862 if (!device->ops.rdma_netdev_get_params) 2863 return ERR_PTR(-EOPNOTSUPP); 2864 2865 rc = device->ops.rdma_netdev_get_params(device, port_num, type, 2866 ¶ms); 2867 if (rc) 2868 return ERR_PTR(rc); 2869 2870 netdev = alloc_netdev_mqs(params.sizeof_priv, name, name_assign_type, 2871 setup, params.txqs, params.rxqs); 2872 if (!netdev) 2873 return ERR_PTR(-ENOMEM); 2874 2875 return netdev; 2876 } 2877 EXPORT_SYMBOL(rdma_alloc_netdev); 2878 2879 int rdma_init_netdev(struct ib_device *device, u8 port_num, 2880 enum rdma_netdev_t type, const char *name, 2881 unsigned char name_assign_type, 2882 void (*setup)(struct net_device *), 2883 struct net_device *netdev) 2884 { 2885 struct rdma_netdev_alloc_params params; 2886 int rc; 2887 2888 if (!device->ops.rdma_netdev_get_params) 2889 return -EOPNOTSUPP; 2890 2891 rc = device->ops.rdma_netdev_get_params(device, port_num, type, 2892 ¶ms); 2893 if (rc) 2894 return rc; 2895 2896 return params.initialize_rdma_netdev(device, port_num, 2897 netdev, params.param); 2898 } 2899 EXPORT_SYMBOL(rdma_init_netdev); 2900 2901 void __rdma_block_iter_start(struct ib_block_iter *biter, 2902 struct scatterlist *sglist, unsigned int nents, 2903 unsigned long pgsz) 2904 { 2905 memset(biter, 0, sizeof(struct ib_block_iter)); 2906 biter->__sg = sglist; 2907 biter->__sg_nents = nents; 2908 2909 /* Driver provides best block size to use */ 2910 biter->__pg_bit = __fls(pgsz); 2911 } 2912 EXPORT_SYMBOL(__rdma_block_iter_start); 2913 2914 bool __rdma_block_iter_next(struct ib_block_iter *biter) 2915 { 2916 unsigned int block_offset; 2917 2918 if (!biter->__sg_nents || !biter->__sg) 2919 return false; 2920 2921 biter->__dma_addr = sg_dma_address(biter->__sg) + biter->__sg_advance; 2922 block_offset = biter->__dma_addr & (BIT_ULL(biter->__pg_bit) - 1); 2923 biter->__sg_advance += BIT_ULL(biter->__pg_bit) - block_offset; 2924 2925 if (biter->__sg_advance >= sg_dma_len(biter->__sg)) { 2926 biter->__sg_advance = 0; 2927 biter->__sg = sg_next(biter->__sg); 2928 biter->__sg_nents--; 2929 } 2930 2931 return true; 2932 } 2933 EXPORT_SYMBOL(__rdma_block_iter_next); 2934