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