]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/infiniband/core/verbs.c
RDMA/core: Add trace points to follow MR allocation
[linux.git] / drivers / infiniband / core / verbs.c
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->event_handler_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->event_handler_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->event_handler_lock, flags);
1101         list_add(&qp->open_list, &real_qp->open_list);
1102         spin_unlock_irqrestore(&real_qp->device->event_handler_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         qp->qp_type    = qp_init_attr->qp_type;
1189         qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
1190
1191         atomic_set(&qp->usecnt, 0);
1192         qp->mrs_used = 0;
1193         spin_lock_init(&qp->mr_lock);
1194         INIT_LIST_HEAD(&qp->rdma_mrs);
1195         INIT_LIST_HEAD(&qp->sig_mrs);
1196         qp->port = 0;
1197
1198         if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
1199                 struct ib_qp *xrc_qp =
1200                         create_xrc_qp_user(qp, qp_init_attr, udata);
1201
1202                 if (IS_ERR(xrc_qp)) {
1203                         ret = PTR_ERR(xrc_qp);
1204                         goto err;
1205                 }
1206                 return xrc_qp;
1207         }
1208
1209         qp->event_handler = qp_init_attr->event_handler;
1210         qp->qp_context = qp_init_attr->qp_context;
1211         if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
1212                 qp->recv_cq = NULL;
1213                 qp->srq = NULL;
1214         } else {
1215                 qp->recv_cq = qp_init_attr->recv_cq;
1216                 if (qp_init_attr->recv_cq)
1217                         atomic_inc(&qp_init_attr->recv_cq->usecnt);
1218                 qp->srq = qp_init_attr->srq;
1219                 if (qp->srq)
1220                         atomic_inc(&qp_init_attr->srq->usecnt);
1221         }
1222
1223         qp->send_cq = qp_init_attr->send_cq;
1224         qp->xrcd    = NULL;
1225
1226         atomic_inc(&pd->usecnt);
1227         if (qp_init_attr->send_cq)
1228                 atomic_inc(&qp_init_attr->send_cq->usecnt);
1229         if (qp_init_attr->rwq_ind_tbl)
1230                 atomic_inc(&qp->rwq_ind_tbl->usecnt);
1231
1232         if (qp_init_attr->cap.max_rdma_ctxs) {
1233                 ret = rdma_rw_init_mrs(qp, qp_init_attr);
1234                 if (ret)
1235                         goto err;
1236         }
1237
1238         /*
1239          * Note: all hw drivers guarantee that max_send_sge is lower than
1240          * the device RDMA WRITE SGE limit but not all hw drivers ensure that
1241          * max_send_sge <= max_sge_rd.
1242          */
1243         qp->max_write_sge = qp_init_attr->cap.max_send_sge;
1244         qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
1245                                  device->attrs.max_sge_rd);
1246         if (qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN)
1247                 qp->integrity_en = true;
1248
1249         return qp;
1250
1251 err:
1252         ib_destroy_qp(qp);
1253         return ERR_PTR(ret);
1254
1255 }
1256 EXPORT_SYMBOL(ib_create_qp_user);
1257
1258 static const struct {
1259         int                     valid;
1260         enum ib_qp_attr_mask    req_param[IB_QPT_MAX];
1261         enum ib_qp_attr_mask    opt_param[IB_QPT_MAX];
1262 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
1263         [IB_QPS_RESET] = {
1264                 [IB_QPS_RESET] = { .valid = 1 },
1265                 [IB_QPS_INIT]  = {
1266                         .valid = 1,
1267                         .req_param = {
1268                                 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX                |
1269                                                 IB_QP_PORT                      |
1270                                                 IB_QP_QKEY),
1271                                 [IB_QPT_RAW_PACKET] = IB_QP_PORT,
1272                                 [IB_QPT_UC]  = (IB_QP_PKEY_INDEX                |
1273                                                 IB_QP_PORT                      |
1274                                                 IB_QP_ACCESS_FLAGS),
1275                                 [IB_QPT_RC]  = (IB_QP_PKEY_INDEX                |
1276                                                 IB_QP_PORT                      |
1277                                                 IB_QP_ACCESS_FLAGS),
1278                                 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX            |
1279                                                 IB_QP_PORT                      |
1280                                                 IB_QP_ACCESS_FLAGS),
1281                                 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX            |
1282                                                 IB_QP_PORT                      |
1283                                                 IB_QP_ACCESS_FLAGS),
1284                                 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX                |
1285                                                 IB_QP_QKEY),
1286                                 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX                |
1287                                                 IB_QP_QKEY),
1288                         }
1289                 },
1290         },
1291         [IB_QPS_INIT]  = {
1292                 [IB_QPS_RESET] = { .valid = 1 },
1293                 [IB_QPS_ERR] =   { .valid = 1 },
1294                 [IB_QPS_INIT]  = {
1295                         .valid = 1,
1296                         .opt_param = {
1297                                 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX                |
1298                                                 IB_QP_PORT                      |
1299                                                 IB_QP_QKEY),
1300                                 [IB_QPT_UC]  = (IB_QP_PKEY_INDEX                |
1301                                                 IB_QP_PORT                      |
1302                                                 IB_QP_ACCESS_FLAGS),
1303                                 [IB_QPT_RC]  = (IB_QP_PKEY_INDEX                |
1304                                                 IB_QP_PORT                      |
1305                                                 IB_QP_ACCESS_FLAGS),
1306                                 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX            |
1307                                                 IB_QP_PORT                      |
1308                                                 IB_QP_ACCESS_FLAGS),
1309                                 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX            |
1310                                                 IB_QP_PORT                      |
1311                                                 IB_QP_ACCESS_FLAGS),
1312                                 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX                |
1313                                                 IB_QP_QKEY),
1314                                 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX                |
1315                                                 IB_QP_QKEY),
1316                         }
1317                 },
1318                 [IB_QPS_RTR]   = {
1319                         .valid = 1,
1320                         .req_param = {
1321                                 [IB_QPT_UC]  = (IB_QP_AV                        |
1322                                                 IB_QP_PATH_MTU                  |
1323                                                 IB_QP_DEST_QPN                  |
1324                                                 IB_QP_RQ_PSN),
1325                                 [IB_QPT_RC]  = (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                                 [IB_QPT_XRC_INI] = (IB_QP_AV                    |
1332                                                 IB_QP_PATH_MTU                  |
1333                                                 IB_QP_DEST_QPN                  |
1334                                                 IB_QP_RQ_PSN),
1335                                 [IB_QPT_XRC_TGT] = (IB_QP_AV                    |
1336                                                 IB_QP_PATH_MTU                  |
1337                                                 IB_QP_DEST_QPN                  |
1338                                                 IB_QP_RQ_PSN                    |
1339                                                 IB_QP_MAX_DEST_RD_ATOMIC        |
1340                                                 IB_QP_MIN_RNR_TIMER),
1341                         },
1342                         .opt_param = {
1343                                  [IB_QPT_UD]  = (IB_QP_PKEY_INDEX               |
1344                                                  IB_QP_QKEY),
1345                                  [IB_QPT_UC]  = (IB_QP_ALT_PATH                 |
1346                                                  IB_QP_ACCESS_FLAGS             |
1347                                                  IB_QP_PKEY_INDEX),
1348                                  [IB_QPT_RC]  = (IB_QP_ALT_PATH                 |
1349                                                  IB_QP_ACCESS_FLAGS             |
1350                                                  IB_QP_PKEY_INDEX),
1351                                  [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH             |
1352                                                  IB_QP_ACCESS_FLAGS             |
1353                                                  IB_QP_PKEY_INDEX),
1354                                  [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH             |
1355                                                  IB_QP_ACCESS_FLAGS             |
1356                                                  IB_QP_PKEY_INDEX),
1357                                  [IB_QPT_SMI] = (IB_QP_PKEY_INDEX               |
1358                                                  IB_QP_QKEY),
1359                                  [IB_QPT_GSI] = (IB_QP_PKEY_INDEX               |
1360                                                  IB_QP_QKEY),
1361                          },
1362                 },
1363         },
1364         [IB_QPS_RTR]   = {
1365                 [IB_QPS_RESET] = { .valid = 1 },
1366                 [IB_QPS_ERR] =   { .valid = 1 },
1367                 [IB_QPS_RTS]   = {
1368                         .valid = 1,
1369                         .req_param = {
1370                                 [IB_QPT_UD]  = IB_QP_SQ_PSN,
1371                                 [IB_QPT_UC]  = IB_QP_SQ_PSN,
1372                                 [IB_QPT_RC]  = (IB_QP_TIMEOUT                   |
1373                                                 IB_QP_RETRY_CNT                 |
1374                                                 IB_QP_RNR_RETRY                 |
1375                                                 IB_QP_SQ_PSN                    |
1376                                                 IB_QP_MAX_QP_RD_ATOMIC),
1377                                 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT               |
1378                                                 IB_QP_RETRY_CNT                 |
1379                                                 IB_QP_RNR_RETRY                 |
1380                                                 IB_QP_SQ_PSN                    |
1381                                                 IB_QP_MAX_QP_RD_ATOMIC),
1382                                 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT               |
1383                                                 IB_QP_SQ_PSN),
1384                                 [IB_QPT_SMI] = IB_QP_SQ_PSN,
1385                                 [IB_QPT_GSI] = IB_QP_SQ_PSN,
1386                         },
1387                         .opt_param = {
1388                                  [IB_QPT_UD]  = (IB_QP_CUR_STATE                |
1389                                                  IB_QP_QKEY),
1390                                  [IB_QPT_UC]  = (IB_QP_CUR_STATE                |
1391                                                  IB_QP_ALT_PATH                 |
1392                                                  IB_QP_ACCESS_FLAGS             |
1393                                                  IB_QP_PATH_MIG_STATE),
1394                                  [IB_QPT_RC]  = (IB_QP_CUR_STATE                |
1395                                                  IB_QP_ALT_PATH                 |
1396                                                  IB_QP_ACCESS_FLAGS             |
1397                                                  IB_QP_MIN_RNR_TIMER            |
1398                                                  IB_QP_PATH_MIG_STATE),
1399                                  [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE            |
1400                                                  IB_QP_ALT_PATH                 |
1401                                                  IB_QP_ACCESS_FLAGS             |
1402                                                  IB_QP_PATH_MIG_STATE),
1403                                  [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE            |
1404                                                  IB_QP_ALT_PATH                 |
1405                                                  IB_QP_ACCESS_FLAGS             |
1406                                                  IB_QP_MIN_RNR_TIMER            |
1407                                                  IB_QP_PATH_MIG_STATE),
1408                                  [IB_QPT_SMI] = (IB_QP_CUR_STATE                |
1409                                                  IB_QP_QKEY),
1410                                  [IB_QPT_GSI] = (IB_QP_CUR_STATE                |
1411                                                  IB_QP_QKEY),
1412                                  [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1413                          }
1414                 }
1415         },
1416         [IB_QPS_RTS]   = {
1417                 [IB_QPS_RESET] = { .valid = 1 },
1418                 [IB_QPS_ERR] =   { .valid = 1 },
1419                 [IB_QPS_RTS]   = {
1420                         .valid = 1,
1421                         .opt_param = {
1422                                 [IB_QPT_UD]  = (IB_QP_CUR_STATE                 |
1423                                                 IB_QP_QKEY),
1424                                 [IB_QPT_UC]  = (IB_QP_CUR_STATE                 |
1425                                                 IB_QP_ACCESS_FLAGS              |
1426                                                 IB_QP_ALT_PATH                  |
1427                                                 IB_QP_PATH_MIG_STATE),
1428                                 [IB_QPT_RC]  = (IB_QP_CUR_STATE                 |
1429                                                 IB_QP_ACCESS_FLAGS              |
1430                                                 IB_QP_ALT_PATH                  |
1431                                                 IB_QP_PATH_MIG_STATE            |
1432                                                 IB_QP_MIN_RNR_TIMER),
1433                                 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE             |
1434                                                 IB_QP_ACCESS_FLAGS              |
1435                                                 IB_QP_ALT_PATH                  |
1436                                                 IB_QP_PATH_MIG_STATE),
1437                                 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE             |
1438                                                 IB_QP_ACCESS_FLAGS              |
1439                                                 IB_QP_ALT_PATH                  |
1440                                                 IB_QP_PATH_MIG_STATE            |
1441                                                 IB_QP_MIN_RNR_TIMER),
1442                                 [IB_QPT_SMI] = (IB_QP_CUR_STATE                 |
1443                                                 IB_QP_QKEY),
1444                                 [IB_QPT_GSI] = (IB_QP_CUR_STATE                 |
1445                                                 IB_QP_QKEY),
1446                                 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1447                         }
1448                 },
1449                 [IB_QPS_SQD]   = {
1450                         .valid = 1,
1451                         .opt_param = {
1452                                 [IB_QPT_UD]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1453                                 [IB_QPT_UC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1454                                 [IB_QPT_RC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1455                                 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1456                                 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1457                                 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1458                                 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1459                         }
1460                 },
1461         },
1462         [IB_QPS_SQD]   = {
1463                 [IB_QPS_RESET] = { .valid = 1 },
1464                 [IB_QPS_ERR] =   { .valid = 1 },
1465                 [IB_QPS_RTS]   = {
1466                         .valid = 1,
1467                         .opt_param = {
1468                                 [IB_QPT_UD]  = (IB_QP_CUR_STATE                 |
1469                                                 IB_QP_QKEY),
1470                                 [IB_QPT_UC]  = (IB_QP_CUR_STATE                 |
1471                                                 IB_QP_ALT_PATH                  |
1472                                                 IB_QP_ACCESS_FLAGS              |
1473                                                 IB_QP_PATH_MIG_STATE),
1474                                 [IB_QPT_RC]  = (IB_QP_CUR_STATE                 |
1475                                                 IB_QP_ALT_PATH                  |
1476                                                 IB_QP_ACCESS_FLAGS              |
1477                                                 IB_QP_MIN_RNR_TIMER             |
1478                                                 IB_QP_PATH_MIG_STATE),
1479                                 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE             |
1480                                                 IB_QP_ALT_PATH                  |
1481                                                 IB_QP_ACCESS_FLAGS              |
1482                                                 IB_QP_PATH_MIG_STATE),
1483                                 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE             |
1484                                                 IB_QP_ALT_PATH                  |
1485                                                 IB_QP_ACCESS_FLAGS              |
1486                                                 IB_QP_MIN_RNR_TIMER             |
1487                                                 IB_QP_PATH_MIG_STATE),
1488                                 [IB_QPT_SMI] = (IB_QP_CUR_STATE                 |
1489                                                 IB_QP_QKEY),
1490                                 [IB_QPT_GSI] = (IB_QP_CUR_STATE                 |
1491                                                 IB_QP_QKEY),
1492                         }
1493                 },
1494                 [IB_QPS_SQD]   = {
1495                         .valid = 1,
1496                         .opt_param = {
1497                                 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX                |
1498                                                 IB_QP_QKEY),
1499                                 [IB_QPT_UC]  = (IB_QP_AV                        |
1500                                                 IB_QP_ALT_PATH                  |
1501                                                 IB_QP_ACCESS_FLAGS              |
1502                                                 IB_QP_PKEY_INDEX                |
1503                                                 IB_QP_PATH_MIG_STATE),
1504                                 [IB_QPT_RC]  = (IB_QP_PORT                      |
1505                                                 IB_QP_AV                        |
1506                                                 IB_QP_TIMEOUT                   |
1507                                                 IB_QP_RETRY_CNT                 |
1508                                                 IB_QP_RNR_RETRY                 |
1509                                                 IB_QP_MAX_QP_RD_ATOMIC          |
1510                                                 IB_QP_MAX_DEST_RD_ATOMIC        |
1511                                                 IB_QP_ALT_PATH                  |
1512                                                 IB_QP_ACCESS_FLAGS              |
1513                                                 IB_QP_PKEY_INDEX                |
1514                                                 IB_QP_MIN_RNR_TIMER             |
1515                                                 IB_QP_PATH_MIG_STATE),
1516                                 [IB_QPT_XRC_INI] = (IB_QP_PORT                  |
1517                                                 IB_QP_AV                        |
1518                                                 IB_QP_TIMEOUT                   |
1519                                                 IB_QP_RETRY_CNT                 |
1520                                                 IB_QP_RNR_RETRY                 |
1521                                                 IB_QP_MAX_QP_RD_ATOMIC          |
1522                                                 IB_QP_ALT_PATH                  |
1523                                                 IB_QP_ACCESS_FLAGS              |
1524                                                 IB_QP_PKEY_INDEX                |
1525                                                 IB_QP_PATH_MIG_STATE),
1526                                 [IB_QPT_XRC_TGT] = (IB_QP_PORT                  |
1527                                                 IB_QP_AV                        |
1528                                                 IB_QP_TIMEOUT                   |
1529                                                 IB_QP_MAX_DEST_RD_ATOMIC        |
1530                                                 IB_QP_ALT_PATH                  |
1531                                                 IB_QP_ACCESS_FLAGS              |
1532                                                 IB_QP_PKEY_INDEX                |
1533                                                 IB_QP_MIN_RNR_TIMER             |
1534                                                 IB_QP_PATH_MIG_STATE),
1535                                 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX                |
1536                                                 IB_QP_QKEY),
1537                                 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX                |
1538                                                 IB_QP_QKEY),
1539                         }
1540                 }
1541         },
1542         [IB_QPS_SQE]   = {
1543                 [IB_QPS_RESET] = { .valid = 1 },
1544                 [IB_QPS_ERR] =   { .valid = 1 },
1545                 [IB_QPS_RTS]   = {
1546                         .valid = 1,
1547                         .opt_param = {
1548                                 [IB_QPT_UD]  = (IB_QP_CUR_STATE                 |
1549                                                 IB_QP_QKEY),
1550                                 [IB_QPT_UC]  = (IB_QP_CUR_STATE                 |
1551                                                 IB_QP_ACCESS_FLAGS),
1552                                 [IB_QPT_SMI] = (IB_QP_CUR_STATE                 |
1553                                                 IB_QP_QKEY),
1554                                 [IB_QPT_GSI] = (IB_QP_CUR_STATE                 |
1555                                                 IB_QP_QKEY),
1556                         }
1557                 }
1558         },
1559         [IB_QPS_ERR] = {
1560                 [IB_QPS_RESET] = { .valid = 1 },
1561                 [IB_QPS_ERR] =   { .valid = 1 }
1562         }
1563 };
1564
1565 bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1566                         enum ib_qp_type type, enum ib_qp_attr_mask mask)
1567 {
1568         enum ib_qp_attr_mask req_param, opt_param;
1569
1570         if (mask & IB_QP_CUR_STATE  &&
1571             cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1572             cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1573                 return false;
1574
1575         if (!qp_state_table[cur_state][next_state].valid)
1576                 return false;
1577
1578         req_param = qp_state_table[cur_state][next_state].req_param[type];
1579         opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1580
1581         if ((mask & req_param) != req_param)
1582                 return false;
1583
1584         if (mask & ~(req_param | opt_param | IB_QP_STATE))
1585                 return false;
1586
1587         return true;
1588 }
1589 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1590
1591 /**
1592  * ib_resolve_eth_dmac - Resolve destination mac address
1593  * @device:             Device to consider
1594  * @ah_attr:            address handle attribute which describes the
1595  *                      source and destination parameters
1596  * ib_resolve_eth_dmac() resolves destination mac address and L3 hop limit It
1597  * returns 0 on success or appropriate error code. It initializes the
1598  * necessary ah_attr fields when call is successful.
1599  */
1600 static int ib_resolve_eth_dmac(struct ib_device *device,
1601                                struct rdma_ah_attr *ah_attr)
1602 {
1603         int ret = 0;
1604
1605         if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1606                 if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1607                         __be32 addr = 0;
1608
1609                         memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
1610                         ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
1611                 } else {
1612                         ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
1613                                         (char *)ah_attr->roce.dmac);
1614                 }
1615         } else {
1616                 ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
1617         }
1618         return ret;
1619 }
1620
1621 static bool is_qp_type_connected(const struct ib_qp *qp)
1622 {
1623         return (qp->qp_type == IB_QPT_UC ||
1624                 qp->qp_type == IB_QPT_RC ||
1625                 qp->qp_type == IB_QPT_XRC_INI ||
1626                 qp->qp_type == IB_QPT_XRC_TGT);
1627 }
1628
1629 /**
1630  * IB core internal function to perform QP attributes modification.
1631  */
1632 static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
1633                          int attr_mask, struct ib_udata *udata)
1634 {
1635         u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1636         const struct ib_gid_attr *old_sgid_attr_av;
1637         const struct ib_gid_attr *old_sgid_attr_alt_av;
1638         int ret;
1639
1640         if (attr_mask & IB_QP_AV) {
1641                 ret = rdma_fill_sgid_attr(qp->device, &attr->ah_attr,
1642                                           &old_sgid_attr_av);
1643                 if (ret)
1644                         return ret;
1645         }
1646         if (attr_mask & IB_QP_ALT_PATH) {
1647                 /*
1648                  * FIXME: This does not track the migration state, so if the
1649                  * user loads a new alternate path after the HW has migrated
1650                  * from primary->alternate we will keep the wrong
1651                  * references. This is OK for IB because the reference
1652                  * counting does not serve any functional purpose.
1653                  */
1654                 ret = rdma_fill_sgid_attr(qp->device, &attr->alt_ah_attr,
1655                                           &old_sgid_attr_alt_av);
1656                 if (ret)
1657                         goto out_av;
1658
1659                 /*
1660                  * Today the core code can only handle alternate paths and APM
1661                  * for IB. Ban them in roce mode.
1662                  */
1663                 if (!(rdma_protocol_ib(qp->device,
1664                                        attr->alt_ah_attr.port_num) &&
1665                       rdma_protocol_ib(qp->device, port))) {
1666                         ret = EINVAL;
1667                         goto out;
1668                 }
1669         }
1670
1671         /*
1672          * If the user provided the qp_attr then we have to resolve it. Kernel
1673          * users have to provide already resolved rdma_ah_attr's
1674          */
1675         if (udata && (attr_mask & IB_QP_AV) &&
1676             attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
1677             is_qp_type_connected(qp)) {
1678                 ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
1679                 if (ret)
1680                         goto out;
1681         }
1682
1683         if (rdma_ib_or_roce(qp->device, port)) {
1684                 if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
1685                         dev_warn(&qp->device->dev,
1686                                  "%s rq_psn overflow, masking to 24 bits\n",
1687                                  __func__);
1688                         attr->rq_psn &= 0xffffff;
1689                 }
1690
1691                 if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
1692                         dev_warn(&qp->device->dev,
1693                                  " %s sq_psn overflow, masking to 24 bits\n",
1694                                  __func__);
1695                         attr->sq_psn &= 0xffffff;
1696                 }
1697         }
1698
1699         /*
1700          * Bind this qp to a counter automatically based on the rdma counter
1701          * rules. This only set in RST2INIT with port specified
1702          */
1703         if (!qp->counter && (attr_mask & IB_QP_PORT) &&
1704             ((attr_mask & IB_QP_STATE) && attr->qp_state == IB_QPS_INIT))
1705                 rdma_counter_bind_qp_auto(qp, attr->port_num);
1706
1707         ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
1708         if (ret)
1709                 goto out;
1710
1711         if (attr_mask & IB_QP_PORT)
1712                 qp->port = attr->port_num;
1713         if (attr_mask & IB_QP_AV)
1714                 qp->av_sgid_attr =
1715                         rdma_update_sgid_attr(&attr->ah_attr, qp->av_sgid_attr);
1716         if (attr_mask & IB_QP_ALT_PATH)
1717                 qp->alt_path_sgid_attr = rdma_update_sgid_attr(
1718                         &attr->alt_ah_attr, qp->alt_path_sgid_attr);
1719
1720 out:
1721         if (attr_mask & IB_QP_ALT_PATH)
1722                 rdma_unfill_sgid_attr(&attr->alt_ah_attr, old_sgid_attr_alt_av);
1723 out_av:
1724         if (attr_mask & IB_QP_AV)
1725                 rdma_unfill_sgid_attr(&attr->ah_attr, old_sgid_attr_av);
1726         return ret;
1727 }
1728
1729 /**
1730  * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
1731  * @ib_qp: The QP to modify.
1732  * @attr: On input, specifies the QP attributes to modify.  On output,
1733  *   the current values of selected QP attributes are returned.
1734  * @attr_mask: A bit-mask used to specify which attributes of the QP
1735  *   are being modified.
1736  * @udata: pointer to user's input output buffer information
1737  *   are being modified.
1738  * It returns 0 on success and returns appropriate error code on error.
1739  */
1740 int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
1741                             int attr_mask, struct ib_udata *udata)
1742 {
1743         return _ib_modify_qp(ib_qp->real_qp, attr, attr_mask, udata);
1744 }
1745 EXPORT_SYMBOL(ib_modify_qp_with_udata);
1746
1747 int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
1748 {
1749         int rc;
1750         u32 netdev_speed;
1751         struct net_device *netdev;
1752         struct ethtool_link_ksettings lksettings;
1753
1754         if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
1755                 return -EINVAL;
1756
1757         netdev = ib_device_get_netdev(dev, port_num);
1758         if (!netdev)
1759                 return -ENODEV;
1760
1761         rtnl_lock();
1762         rc = __ethtool_get_link_ksettings(netdev, &lksettings);
1763         rtnl_unlock();
1764
1765         dev_put(netdev);
1766
1767         if (!rc) {
1768                 netdev_speed = lksettings.base.speed;
1769         } else {
1770                 netdev_speed = SPEED_1000;
1771                 pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
1772                         netdev_speed);
1773         }
1774
1775         if (netdev_speed <= SPEED_1000) {
1776                 *width = IB_WIDTH_1X;
1777                 *speed = IB_SPEED_SDR;
1778         } else if (netdev_speed <= SPEED_10000) {
1779                 *width = IB_WIDTH_1X;
1780                 *speed = IB_SPEED_FDR10;
1781         } else if (netdev_speed <= SPEED_20000) {
1782                 *width = IB_WIDTH_4X;
1783                 *speed = IB_SPEED_DDR;
1784         } else if (netdev_speed <= SPEED_25000) {
1785                 *width = IB_WIDTH_1X;
1786                 *speed = IB_SPEED_EDR;
1787         } else if (netdev_speed <= SPEED_40000) {
1788                 *width = IB_WIDTH_4X;
1789                 *speed = IB_SPEED_FDR10;
1790         } else {
1791                 *width = IB_WIDTH_4X;
1792                 *speed = IB_SPEED_EDR;
1793         }
1794
1795         return 0;
1796 }
1797 EXPORT_SYMBOL(ib_get_eth_speed);
1798
1799 int ib_modify_qp(struct ib_qp *qp,
1800                  struct ib_qp_attr *qp_attr,
1801                  int qp_attr_mask)
1802 {
1803         return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
1804 }
1805 EXPORT_SYMBOL(ib_modify_qp);
1806
1807 int ib_query_qp(struct ib_qp *qp,
1808                 struct ib_qp_attr *qp_attr,
1809                 int qp_attr_mask,
1810                 struct ib_qp_init_attr *qp_init_attr)
1811 {
1812         qp_attr->ah_attr.grh.sgid_attr = NULL;
1813         qp_attr->alt_ah_attr.grh.sgid_attr = NULL;
1814
1815         return qp->device->ops.query_qp ?
1816                 qp->device->ops.query_qp(qp->real_qp, qp_attr, qp_attr_mask,
1817                                          qp_init_attr) : -EOPNOTSUPP;
1818 }
1819 EXPORT_SYMBOL(ib_query_qp);
1820
1821 int ib_close_qp(struct ib_qp *qp)
1822 {
1823         struct ib_qp *real_qp;
1824         unsigned long flags;
1825
1826         real_qp = qp->real_qp;
1827         if (real_qp == qp)
1828                 return -EINVAL;
1829
1830         spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1831         list_del(&qp->open_list);
1832         spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1833
1834         atomic_dec(&real_qp->usecnt);
1835         if (qp->qp_sec)
1836                 ib_close_shared_qp_security(qp->qp_sec);
1837         kfree(qp);
1838
1839         return 0;
1840 }
1841 EXPORT_SYMBOL(ib_close_qp);
1842
1843 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1844 {
1845         struct ib_xrcd *xrcd;
1846         struct ib_qp *real_qp;
1847         int ret;
1848
1849         real_qp = qp->real_qp;
1850         xrcd = real_qp->xrcd;
1851
1852         mutex_lock(&xrcd->tgt_qp_mutex);
1853         ib_close_qp(qp);
1854         if (atomic_read(&real_qp->usecnt) == 0)
1855                 list_del(&real_qp->xrcd_list);
1856         else
1857                 real_qp = NULL;
1858         mutex_unlock(&xrcd->tgt_qp_mutex);
1859
1860         if (real_qp) {
1861                 ret = ib_destroy_qp(real_qp);
1862                 if (!ret)
1863                         atomic_dec(&xrcd->usecnt);
1864                 else
1865                         __ib_insert_xrcd_qp(xrcd, real_qp);
1866         }
1867
1868         return 0;
1869 }
1870
1871 int ib_destroy_qp_user(struct ib_qp *qp, struct ib_udata *udata)
1872 {
1873         const struct ib_gid_attr *alt_path_sgid_attr = qp->alt_path_sgid_attr;
1874         const struct ib_gid_attr *av_sgid_attr = qp->av_sgid_attr;
1875         struct ib_pd *pd;
1876         struct ib_cq *scq, *rcq;
1877         struct ib_srq *srq;
1878         struct ib_rwq_ind_table *ind_tbl;
1879         struct ib_qp_security *sec;
1880         int ret;
1881
1882         WARN_ON_ONCE(qp->mrs_used > 0);
1883
1884         if (atomic_read(&qp->usecnt))
1885                 return -EBUSY;
1886
1887         if (qp->real_qp != qp)
1888                 return __ib_destroy_shared_qp(qp);
1889
1890         pd   = qp->pd;
1891         scq  = qp->send_cq;
1892         rcq  = qp->recv_cq;
1893         srq  = qp->srq;
1894         ind_tbl = qp->rwq_ind_tbl;
1895         sec  = qp->qp_sec;
1896         if (sec)
1897                 ib_destroy_qp_security_begin(sec);
1898
1899         if (!qp->uobject)
1900                 rdma_rw_cleanup_mrs(qp);
1901
1902         rdma_counter_unbind_qp(qp, true);
1903         rdma_restrack_del(&qp->res);
1904         ret = qp->device->ops.destroy_qp(qp, udata);
1905         if (!ret) {
1906                 if (alt_path_sgid_attr)
1907                         rdma_put_gid_attr(alt_path_sgid_attr);
1908                 if (av_sgid_attr)
1909                         rdma_put_gid_attr(av_sgid_attr);
1910                 if (pd)
1911                         atomic_dec(&pd->usecnt);
1912                 if (scq)
1913                         atomic_dec(&scq->usecnt);
1914                 if (rcq)
1915                         atomic_dec(&rcq->usecnt);
1916                 if (srq)
1917                         atomic_dec(&srq->usecnt);
1918                 if (ind_tbl)
1919                         atomic_dec(&ind_tbl->usecnt);
1920                 if (sec)
1921                         ib_destroy_qp_security_end(sec);
1922         } else {
1923                 if (sec)
1924                         ib_destroy_qp_security_abort(sec);
1925         }
1926
1927         return ret;
1928 }
1929 EXPORT_SYMBOL(ib_destroy_qp_user);
1930
1931 /* Completion queues */
1932
1933 struct ib_cq *__ib_create_cq(struct ib_device *device,
1934                              ib_comp_handler comp_handler,
1935                              void (*event_handler)(struct ib_event *, void *),
1936                              void *cq_context,
1937                              const struct ib_cq_init_attr *cq_attr,
1938                              const char *caller)
1939 {
1940         struct ib_cq *cq;
1941         int ret;
1942
1943         cq = rdma_zalloc_drv_obj(device, ib_cq);
1944         if (!cq)
1945                 return ERR_PTR(-ENOMEM);
1946
1947         cq->device = device;
1948         cq->uobject = NULL;
1949         cq->comp_handler = comp_handler;
1950         cq->event_handler = event_handler;
1951         cq->cq_context = cq_context;
1952         atomic_set(&cq->usecnt, 0);
1953         cq->res.type = RDMA_RESTRACK_CQ;
1954         rdma_restrack_set_task(&cq->res, caller);
1955
1956         ret = device->ops.create_cq(cq, cq_attr, NULL);
1957         if (ret) {
1958                 kfree(cq);
1959                 return ERR_PTR(ret);
1960         }
1961
1962         rdma_restrack_kadd(&cq->res);
1963         return cq;
1964 }
1965 EXPORT_SYMBOL(__ib_create_cq);
1966
1967 int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1968 {
1969         return cq->device->ops.modify_cq ?
1970                 cq->device->ops.modify_cq(cq, cq_count,
1971                                           cq_period) : -EOPNOTSUPP;
1972 }
1973 EXPORT_SYMBOL(rdma_set_cq_moderation);
1974
1975 int ib_destroy_cq_user(struct ib_cq *cq, struct ib_udata *udata)
1976 {
1977         if (atomic_read(&cq->usecnt))
1978                 return -EBUSY;
1979
1980         rdma_restrack_del(&cq->res);
1981         cq->device->ops.destroy_cq(cq, udata);
1982         kfree(cq);
1983         return 0;
1984 }
1985 EXPORT_SYMBOL(ib_destroy_cq_user);
1986
1987 int ib_resize_cq(struct ib_cq *cq, int cqe)
1988 {
1989         return cq->device->ops.resize_cq ?
1990                 cq->device->ops.resize_cq(cq, cqe, NULL) : -EOPNOTSUPP;
1991 }
1992 EXPORT_SYMBOL(ib_resize_cq);
1993
1994 /* Memory regions */
1995
1996 int ib_dereg_mr_user(struct ib_mr *mr, struct ib_udata *udata)
1997 {
1998         struct ib_pd *pd = mr->pd;
1999         struct ib_dm *dm = mr->dm;
2000         struct ib_sig_attrs *sig_attrs = mr->sig_attrs;
2001         int ret;
2002
2003         trace_mr_dereg(mr);
2004         rdma_restrack_del(&mr->res);
2005         ret = mr->device->ops.dereg_mr(mr, udata);
2006         if (!ret) {
2007                 atomic_dec(&pd->usecnt);
2008                 if (dm)
2009                         atomic_dec(&dm->usecnt);
2010                 kfree(sig_attrs);
2011         }
2012
2013         return ret;
2014 }
2015 EXPORT_SYMBOL(ib_dereg_mr_user);
2016
2017 /**
2018  * ib_alloc_mr_user() - Allocates a memory region
2019  * @pd:            protection domain associated with the region
2020  * @mr_type:       memory region type
2021  * @max_num_sg:    maximum sg entries available for registration.
2022  * @udata:         user data or null for kernel objects
2023  *
2024  * Notes:
2025  * Memory registeration page/sg lists must not exceed max_num_sg.
2026  * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
2027  * max_num_sg * used_page_size.
2028  *
2029  */
2030 struct ib_mr *ib_alloc_mr_user(struct ib_pd *pd, enum ib_mr_type mr_type,
2031                                u32 max_num_sg, struct ib_udata *udata)
2032 {
2033         struct ib_mr *mr;
2034
2035         if (!pd->device->ops.alloc_mr) {
2036                 mr = ERR_PTR(-EOPNOTSUPP);
2037                 goto out;
2038         }
2039
2040         if (mr_type == IB_MR_TYPE_INTEGRITY) {
2041                 WARN_ON_ONCE(1);
2042                 mr = ERR_PTR(-EINVAL);
2043                 goto out;
2044         }
2045
2046         mr = pd->device->ops.alloc_mr(pd, mr_type, max_num_sg, udata);
2047         if (!IS_ERR(mr)) {
2048                 mr->device  = pd->device;
2049                 mr->pd      = pd;
2050                 mr->dm      = NULL;
2051                 mr->uobject = NULL;
2052                 atomic_inc(&pd->usecnt);
2053                 mr->need_inval = false;
2054                 mr->res.type = RDMA_RESTRACK_MR;
2055                 rdma_restrack_kadd(&mr->res);
2056                 mr->type = mr_type;
2057                 mr->sig_attrs = NULL;
2058         }
2059
2060 out:
2061         trace_mr_alloc(pd, mr_type, max_num_sg, mr);
2062         return mr;
2063 }
2064 EXPORT_SYMBOL(ib_alloc_mr_user);
2065
2066 /**
2067  * ib_alloc_mr_integrity() - Allocates an integrity memory region
2068  * @pd:                      protection domain associated with the region
2069  * @max_num_data_sg:         maximum data sg entries available for registration
2070  * @max_num_meta_sg:         maximum metadata sg entries available for
2071  *                           registration
2072  *
2073  * Notes:
2074  * Memory registration page/sg lists must not exceed max_num_sg,
2075  * also the integrity page/sg lists must not exceed max_num_meta_sg.
2076  *
2077  */
2078 struct ib_mr *ib_alloc_mr_integrity(struct ib_pd *pd,
2079                                     u32 max_num_data_sg,
2080                                     u32 max_num_meta_sg)
2081 {
2082         struct ib_mr *mr;
2083         struct ib_sig_attrs *sig_attrs;
2084
2085         if (!pd->device->ops.alloc_mr_integrity ||
2086             !pd->device->ops.map_mr_sg_pi) {
2087                 mr = ERR_PTR(-EOPNOTSUPP);
2088                 goto out;
2089         }
2090
2091         if (!max_num_meta_sg) {
2092                 mr = ERR_PTR(-EINVAL);
2093                 goto out;
2094         }
2095
2096         sig_attrs = kzalloc(sizeof(struct ib_sig_attrs), GFP_KERNEL);
2097         if (!sig_attrs) {
2098                 mr = ERR_PTR(-ENOMEM);
2099                 goto out;
2100         }
2101
2102         mr = pd->device->ops.alloc_mr_integrity(pd, max_num_data_sg,
2103                                                 max_num_meta_sg);
2104         if (IS_ERR(mr)) {
2105                 kfree(sig_attrs);
2106                 goto out;
2107         }
2108
2109         mr->device = pd->device;
2110         mr->pd = pd;
2111         mr->dm = NULL;
2112         mr->uobject = NULL;
2113         atomic_inc(&pd->usecnt);
2114         mr->need_inval = false;
2115         mr->res.type = RDMA_RESTRACK_MR;
2116         rdma_restrack_kadd(&mr->res);
2117         mr->type = IB_MR_TYPE_INTEGRITY;
2118         mr->sig_attrs = sig_attrs;
2119
2120 out:
2121         trace_mr_integ_alloc(pd, max_num_data_sg, max_num_meta_sg, mr);
2122         return mr;
2123 }
2124 EXPORT_SYMBOL(ib_alloc_mr_integrity);
2125
2126 /* "Fast" memory regions */
2127
2128 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
2129                             int mr_access_flags,
2130                             struct ib_fmr_attr *fmr_attr)
2131 {
2132         struct ib_fmr *fmr;
2133
2134         if (!pd->device->ops.alloc_fmr)
2135                 return ERR_PTR(-EOPNOTSUPP);
2136
2137         fmr = pd->device->ops.alloc_fmr(pd, mr_access_flags, fmr_attr);
2138         if (!IS_ERR(fmr)) {
2139                 fmr->device = pd->device;
2140                 fmr->pd     = pd;
2141                 atomic_inc(&pd->usecnt);
2142         }
2143
2144         return fmr;
2145 }
2146 EXPORT_SYMBOL(ib_alloc_fmr);
2147
2148 int ib_unmap_fmr(struct list_head *fmr_list)
2149 {
2150         struct ib_fmr *fmr;
2151
2152         if (list_empty(fmr_list))
2153                 return 0;
2154
2155         fmr = list_entry(fmr_list->next, struct ib_fmr, list);
2156         return fmr->device->ops.unmap_fmr(fmr_list);
2157 }
2158 EXPORT_SYMBOL(ib_unmap_fmr);
2159
2160 int ib_dealloc_fmr(struct ib_fmr *fmr)
2161 {
2162         struct ib_pd *pd;
2163         int ret;
2164
2165         pd = fmr->pd;
2166         ret = fmr->device->ops.dealloc_fmr(fmr);
2167         if (!ret)
2168                 atomic_dec(&pd->usecnt);
2169
2170         return ret;
2171 }
2172 EXPORT_SYMBOL(ib_dealloc_fmr);
2173
2174 /* Multicast groups */
2175
2176 static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
2177 {
2178         struct ib_qp_init_attr init_attr = {};
2179         struct ib_qp_attr attr = {};
2180         int num_eth_ports = 0;
2181         int port;
2182
2183         /* If QP state >= init, it is assigned to a port and we can check this
2184          * port only.
2185          */
2186         if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
2187                 if (attr.qp_state >= IB_QPS_INIT) {
2188                         if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
2189                             IB_LINK_LAYER_INFINIBAND)
2190                                 return true;
2191                         goto lid_check;
2192                 }
2193         }
2194
2195         /* Can't get a quick answer, iterate over all ports */
2196         for (port = 0; port < qp->device->phys_port_cnt; port++)
2197                 if (rdma_port_get_link_layer(qp->device, port) !=
2198                     IB_LINK_LAYER_INFINIBAND)
2199                         num_eth_ports++;
2200
2201         /* If we have at lease one Ethernet port, RoCE annex declares that
2202          * multicast LID should be ignored. We can't tell at this step if the
2203          * QP belongs to an IB or Ethernet port.
2204          */
2205         if (num_eth_ports)
2206                 return true;
2207
2208         /* If all the ports are IB, we can check according to IB spec. */
2209 lid_check:
2210         return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
2211                  lid == be16_to_cpu(IB_LID_PERMISSIVE));
2212 }
2213
2214 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
2215 {
2216         int ret;
2217
2218         if (!qp->device->ops.attach_mcast)
2219                 return -EOPNOTSUPP;
2220
2221         if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2222             qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
2223                 return -EINVAL;
2224
2225         ret = qp->device->ops.attach_mcast(qp, gid, lid);
2226         if (!ret)
2227                 atomic_inc(&qp->usecnt);
2228         return ret;
2229 }
2230 EXPORT_SYMBOL(ib_attach_mcast);
2231
2232 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
2233 {
2234         int ret;
2235
2236         if (!qp->device->ops.detach_mcast)
2237                 return -EOPNOTSUPP;
2238
2239         if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2240             qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
2241                 return -EINVAL;
2242
2243         ret = qp->device->ops.detach_mcast(qp, gid, lid);
2244         if (!ret)
2245                 atomic_dec(&qp->usecnt);
2246         return ret;
2247 }
2248 EXPORT_SYMBOL(ib_detach_mcast);
2249
2250 struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller)
2251 {
2252         struct ib_xrcd *xrcd;
2253
2254         if (!device->ops.alloc_xrcd)
2255                 return ERR_PTR(-EOPNOTSUPP);
2256
2257         xrcd = device->ops.alloc_xrcd(device, NULL);
2258         if (!IS_ERR(xrcd)) {
2259                 xrcd->device = device;
2260                 xrcd->inode = NULL;
2261                 atomic_set(&xrcd->usecnt, 0);
2262                 mutex_init(&xrcd->tgt_qp_mutex);
2263                 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
2264         }
2265
2266         return xrcd;
2267 }
2268 EXPORT_SYMBOL(__ib_alloc_xrcd);
2269
2270 int ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
2271 {
2272         struct ib_qp *qp;
2273         int ret;
2274
2275         if (atomic_read(&xrcd->usecnt))
2276                 return -EBUSY;
2277
2278         while (!list_empty(&xrcd->tgt_qp_list)) {
2279                 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
2280                 ret = ib_destroy_qp(qp);
2281                 if (ret)
2282                         return ret;
2283         }
2284         mutex_destroy(&xrcd->tgt_qp_mutex);
2285
2286         return xrcd->device->ops.dealloc_xrcd(xrcd, udata);
2287 }
2288 EXPORT_SYMBOL(ib_dealloc_xrcd);
2289
2290 /**
2291  * ib_create_wq - Creates a WQ associated with the specified protection
2292  * domain.
2293  * @pd: The protection domain associated with the WQ.
2294  * @wq_attr: A list of initial attributes required to create the
2295  * WQ. If WQ creation succeeds, then the attributes are updated to
2296  * the actual capabilities of the created WQ.
2297  *
2298  * wq_attr->max_wr and wq_attr->max_sge determine
2299  * the requested size of the WQ, and set to the actual values allocated
2300  * on return.
2301  * If ib_create_wq() succeeds, then max_wr and max_sge will always be
2302  * at least as large as the requested values.
2303  */
2304 struct ib_wq *ib_create_wq(struct ib_pd *pd,
2305                            struct ib_wq_init_attr *wq_attr)
2306 {
2307         struct ib_wq *wq;
2308
2309         if (!pd->device->ops.create_wq)
2310                 return ERR_PTR(-EOPNOTSUPP);
2311
2312         wq = pd->device->ops.create_wq(pd, wq_attr, NULL);
2313         if (!IS_ERR(wq)) {
2314                 wq->event_handler = wq_attr->event_handler;
2315                 wq->wq_context = wq_attr->wq_context;
2316                 wq->wq_type = wq_attr->wq_type;
2317                 wq->cq = wq_attr->cq;
2318                 wq->device = pd->device;
2319                 wq->pd = pd;
2320                 wq->uobject = NULL;
2321                 atomic_inc(&pd->usecnt);
2322                 atomic_inc(&wq_attr->cq->usecnt);
2323                 atomic_set(&wq->usecnt, 0);
2324         }
2325         return wq;
2326 }
2327 EXPORT_SYMBOL(ib_create_wq);
2328
2329 /**
2330  * ib_destroy_wq - Destroys the specified user WQ.
2331  * @wq: The WQ to destroy.
2332  * @udata: Valid user data
2333  */
2334 int ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
2335 {
2336         struct ib_cq *cq = wq->cq;
2337         struct ib_pd *pd = wq->pd;
2338
2339         if (atomic_read(&wq->usecnt))
2340                 return -EBUSY;
2341
2342         wq->device->ops.destroy_wq(wq, udata);
2343         atomic_dec(&pd->usecnt);
2344         atomic_dec(&cq->usecnt);
2345
2346         return 0;
2347 }
2348 EXPORT_SYMBOL(ib_destroy_wq);
2349
2350 /**
2351  * ib_modify_wq - Modifies the specified WQ.
2352  * @wq: The WQ to modify.
2353  * @wq_attr: On input, specifies the WQ attributes to modify.
2354  * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
2355  *   are being modified.
2356  * On output, the current values of selected WQ attributes are returned.
2357  */
2358 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
2359                  u32 wq_attr_mask)
2360 {
2361         int err;
2362
2363         if (!wq->device->ops.modify_wq)
2364                 return -EOPNOTSUPP;
2365
2366         err = wq->device->ops.modify_wq(wq, wq_attr, wq_attr_mask, NULL);
2367         return err;
2368 }
2369 EXPORT_SYMBOL(ib_modify_wq);
2370
2371 /*
2372  * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
2373  * @device: The device on which to create the rwq indirection table.
2374  * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
2375  * create the Indirection Table.
2376  *
2377  * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
2378  *      than the created ib_rwq_ind_table object and the caller is responsible
2379  *      for its memory allocation/free.
2380  */
2381 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
2382                                                  struct ib_rwq_ind_table_init_attr *init_attr)
2383 {
2384         struct ib_rwq_ind_table *rwq_ind_table;
2385         int i;
2386         u32 table_size;
2387
2388         if (!device->ops.create_rwq_ind_table)
2389                 return ERR_PTR(-EOPNOTSUPP);
2390
2391         table_size = (1 << init_attr->log_ind_tbl_size);
2392         rwq_ind_table = device->ops.create_rwq_ind_table(device,
2393                                                          init_attr, NULL);
2394         if (IS_ERR(rwq_ind_table))
2395                 return rwq_ind_table;
2396
2397         rwq_ind_table->ind_tbl = init_attr->ind_tbl;
2398         rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
2399         rwq_ind_table->device = device;
2400         rwq_ind_table->uobject = NULL;
2401         atomic_set(&rwq_ind_table->usecnt, 0);
2402
2403         for (i = 0; i < table_size; i++)
2404                 atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
2405
2406         return rwq_ind_table;
2407 }
2408 EXPORT_SYMBOL(ib_create_rwq_ind_table);
2409
2410 /*
2411  * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
2412  * @wq_ind_table: The Indirection Table to destroy.
2413 */
2414 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
2415 {
2416         int err, i;
2417         u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
2418         struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
2419
2420         if (atomic_read(&rwq_ind_table->usecnt))
2421                 return -EBUSY;
2422
2423         err = rwq_ind_table->device->ops.destroy_rwq_ind_table(rwq_ind_table);
2424         if (!err) {
2425                 for (i = 0; i < table_size; i++)
2426                         atomic_dec(&ind_tbl[i]->usecnt);
2427         }
2428
2429         return err;
2430 }
2431 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
2432
2433 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
2434                        struct ib_mr_status *mr_status)
2435 {
2436         if (!mr->device->ops.check_mr_status)
2437                 return -EOPNOTSUPP;
2438
2439         return mr->device->ops.check_mr_status(mr, check_mask, mr_status);
2440 }
2441 EXPORT_SYMBOL(ib_check_mr_status);
2442
2443 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
2444                          int state)
2445 {
2446         if (!device->ops.set_vf_link_state)
2447                 return -EOPNOTSUPP;
2448
2449         return device->ops.set_vf_link_state(device, vf, port, state);
2450 }
2451 EXPORT_SYMBOL(ib_set_vf_link_state);
2452
2453 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
2454                      struct ifla_vf_info *info)
2455 {
2456         if (!device->ops.get_vf_config)
2457                 return -EOPNOTSUPP;
2458
2459         return device->ops.get_vf_config(device, vf, port, info);
2460 }
2461 EXPORT_SYMBOL(ib_get_vf_config);
2462
2463 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
2464                     struct ifla_vf_stats *stats)
2465 {
2466         if (!device->ops.get_vf_stats)
2467                 return -EOPNOTSUPP;
2468
2469         return device->ops.get_vf_stats(device, vf, port, stats);
2470 }
2471 EXPORT_SYMBOL(ib_get_vf_stats);
2472
2473 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
2474                    int type)
2475 {
2476         if (!device->ops.set_vf_guid)
2477                 return -EOPNOTSUPP;
2478
2479         return device->ops.set_vf_guid(device, vf, port, guid, type);
2480 }
2481 EXPORT_SYMBOL(ib_set_vf_guid);
2482
2483 int ib_get_vf_guid(struct ib_device *device, int vf, u8 port,
2484                    struct ifla_vf_guid *node_guid,
2485                    struct ifla_vf_guid *port_guid)
2486 {
2487         if (!device->ops.get_vf_guid)
2488                 return -EOPNOTSUPP;
2489
2490         return device->ops.get_vf_guid(device, vf, port, node_guid, port_guid);
2491 }
2492 EXPORT_SYMBOL(ib_get_vf_guid);
2493 /**
2494  * ib_map_mr_sg_pi() - Map the dma mapped SG lists for PI (protection
2495  *     information) and set an appropriate memory region for registration.
2496  * @mr:             memory region
2497  * @data_sg:        dma mapped scatterlist for data
2498  * @data_sg_nents:  number of entries in data_sg
2499  * @data_sg_offset: offset in bytes into data_sg
2500  * @meta_sg:        dma mapped scatterlist for metadata
2501  * @meta_sg_nents:  number of entries in meta_sg
2502  * @meta_sg_offset: offset in bytes into meta_sg
2503  * @page_size:      page vector desired page size
2504  *
2505  * Constraints:
2506  * - The MR must be allocated with type IB_MR_TYPE_INTEGRITY.
2507  *
2508  * Return: 0 on success.
2509  *
2510  * After this completes successfully, the  memory region
2511  * is ready for registration.
2512  */
2513 int ib_map_mr_sg_pi(struct ib_mr *mr, struct scatterlist *data_sg,
2514                     int data_sg_nents, unsigned int *data_sg_offset,
2515                     struct scatterlist *meta_sg, int meta_sg_nents,
2516                     unsigned int *meta_sg_offset, unsigned int page_size)
2517 {
2518         if (unlikely(!mr->device->ops.map_mr_sg_pi ||
2519                      WARN_ON_ONCE(mr->type != IB_MR_TYPE_INTEGRITY)))
2520                 return -EOPNOTSUPP;
2521
2522         mr->page_size = page_size;
2523
2524         return mr->device->ops.map_mr_sg_pi(mr, data_sg, data_sg_nents,
2525                                             data_sg_offset, meta_sg,
2526                                             meta_sg_nents, meta_sg_offset);
2527 }
2528 EXPORT_SYMBOL(ib_map_mr_sg_pi);
2529
2530 /**
2531  * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
2532  *     and set it the memory region.
2533  * @mr:            memory region
2534  * @sg:            dma mapped scatterlist
2535  * @sg_nents:      number of entries in sg
2536  * @sg_offset:     offset in bytes into sg
2537  * @page_size:     page vector desired page size
2538  *
2539  * Constraints:
2540  * - The first sg element is allowed to have an offset.
2541  * - Each sg element must either be aligned to page_size or virtually
2542  *   contiguous to the previous element. In case an sg element has a
2543  *   non-contiguous offset, the mapping prefix will not include it.
2544  * - The last sg element is allowed to have length less than page_size.
2545  * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
2546  *   then only max_num_sg entries will be mapped.
2547  * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
2548  *   constraints holds and the page_size argument is ignored.
2549  *
2550  * Returns the number of sg elements that were mapped to the memory region.
2551  *
2552  * After this completes successfully, the  memory region
2553  * is ready for registration.
2554  */
2555 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
2556                  unsigned int *sg_offset, unsigned int page_size)
2557 {
2558         if (unlikely(!mr->device->ops.map_mr_sg))
2559                 return -EOPNOTSUPP;
2560
2561         mr->page_size = page_size;
2562
2563         return mr->device->ops.map_mr_sg(mr, sg, sg_nents, sg_offset);
2564 }
2565 EXPORT_SYMBOL(ib_map_mr_sg);
2566
2567 /**
2568  * ib_sg_to_pages() - Convert the largest prefix of a sg list
2569  *     to a page vector
2570  * @mr:            memory region
2571  * @sgl:           dma mapped scatterlist
2572  * @sg_nents:      number of entries in sg
2573  * @sg_offset_p:   IN:  start offset in bytes into sg
2574  *                 OUT: offset in bytes for element n of the sg of the first
2575  *                      byte that has not been processed where n is the return
2576  *                      value of this function.
2577  * @set_page:      driver page assignment function pointer
2578  *
2579  * Core service helper for drivers to convert the largest
2580  * prefix of given sg list to a page vector. The sg list
2581  * prefix converted is the prefix that meet the requirements
2582  * of ib_map_mr_sg.
2583  *
2584  * Returns the number of sg elements that were assigned to
2585  * a page vector.
2586  */
2587 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
2588                 unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
2589 {
2590         struct scatterlist *sg;
2591         u64 last_end_dma_addr = 0;
2592         unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2593         unsigned int last_page_off = 0;
2594         u64 page_mask = ~((u64)mr->page_size - 1);
2595         int i, ret;
2596
2597         if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
2598                 return -EINVAL;
2599
2600         mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
2601         mr->length = 0;
2602
2603         for_each_sg(sgl, sg, sg_nents, i) {
2604                 u64 dma_addr = sg_dma_address(sg) + sg_offset;
2605                 u64 prev_addr = dma_addr;
2606                 unsigned int dma_len = sg_dma_len(sg) - sg_offset;
2607                 u64 end_dma_addr = dma_addr + dma_len;
2608                 u64 page_addr = dma_addr & page_mask;
2609
2610                 /*
2611                  * For the second and later elements, check whether either the
2612                  * end of element i-1 or the start of element i is not aligned
2613                  * on a page boundary.
2614                  */
2615                 if (i && (last_page_off != 0 || page_addr != dma_addr)) {
2616                         /* Stop mapping if there is a gap. */
2617                         if (last_end_dma_addr != dma_addr)
2618                                 break;
2619
2620                         /*
2621                          * Coalesce this element with the last. If it is small
2622                          * enough just update mr->length. Otherwise start
2623                          * mapping from the next page.
2624                          */
2625                         goto next_page;
2626                 }
2627
2628                 do {
2629                         ret = set_page(mr, page_addr);
2630                         if (unlikely(ret < 0)) {
2631                                 sg_offset = prev_addr - sg_dma_address(sg);
2632                                 mr->length += prev_addr - dma_addr;
2633                                 if (sg_offset_p)
2634                                         *sg_offset_p = sg_offset;
2635                                 return i || sg_offset ? i : ret;
2636                         }
2637                         prev_addr = page_addr;
2638 next_page:
2639                         page_addr += mr->page_size;
2640                 } while (page_addr < end_dma_addr);
2641
2642                 mr->length += dma_len;
2643                 last_end_dma_addr = end_dma_addr;
2644                 last_page_off = end_dma_addr & ~page_mask;
2645
2646                 sg_offset = 0;
2647         }
2648
2649         if (sg_offset_p)
2650                 *sg_offset_p = 0;
2651         return i;
2652 }
2653 EXPORT_SYMBOL(ib_sg_to_pages);
2654
2655 struct ib_drain_cqe {
2656         struct ib_cqe cqe;
2657         struct completion done;
2658 };
2659
2660 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
2661 {
2662         struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
2663                                                 cqe);
2664
2665         complete(&cqe->done);
2666 }
2667
2668 /*
2669  * Post a WR and block until its completion is reaped for the SQ.
2670  */
2671 static void __ib_drain_sq(struct ib_qp *qp)
2672 {
2673         struct ib_cq *cq = qp->send_cq;
2674         struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2675         struct ib_drain_cqe sdrain;
2676         struct ib_rdma_wr swr = {
2677                 .wr = {
2678                         .next = NULL,
2679                         { .wr_cqe       = &sdrain.cqe, },
2680                         .opcode = IB_WR_RDMA_WRITE,
2681                 },
2682         };
2683         int ret;
2684
2685         ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2686         if (ret) {
2687                 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2688                 return;
2689         }
2690
2691         sdrain.cqe.done = ib_drain_qp_done;
2692         init_completion(&sdrain.done);
2693
2694         ret = ib_post_send(qp, &swr.wr, NULL);
2695         if (ret) {
2696                 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2697                 return;
2698         }
2699
2700         if (cq->poll_ctx == IB_POLL_DIRECT)
2701                 while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2702                         ib_process_cq_direct(cq, -1);
2703         else
2704                 wait_for_completion(&sdrain.done);
2705 }
2706
2707 /*
2708  * Post a WR and block until its completion is reaped for the RQ.
2709  */
2710 static void __ib_drain_rq(struct ib_qp *qp)
2711 {
2712         struct ib_cq *cq = qp->recv_cq;
2713         struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2714         struct ib_drain_cqe rdrain;
2715         struct ib_recv_wr rwr = {};
2716         int ret;
2717
2718         ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2719         if (ret) {
2720                 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2721                 return;
2722         }
2723
2724         rwr.wr_cqe = &rdrain.cqe;
2725         rdrain.cqe.done = ib_drain_qp_done;
2726         init_completion(&rdrain.done);
2727
2728         ret = ib_post_recv(qp, &rwr, NULL);
2729         if (ret) {
2730                 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2731                 return;
2732         }
2733
2734         if (cq->poll_ctx == IB_POLL_DIRECT)
2735                 while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2736                         ib_process_cq_direct(cq, -1);
2737         else
2738                 wait_for_completion(&rdrain.done);
2739 }
2740
2741 /**
2742  * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2743  *                 application.
2744  * @qp:            queue pair to drain
2745  *
2746  * If the device has a provider-specific drain function, then
2747  * call that.  Otherwise call the generic drain function
2748  * __ib_drain_sq().
2749  *
2750  * The caller must:
2751  *
2752  * ensure there is room in the CQ and SQ for the drain work request and
2753  * completion.
2754  *
2755  * allocate the CQ using ib_alloc_cq().
2756  *
2757  * ensure that there are no other contexts that are posting WRs concurrently.
2758  * Otherwise the drain is not guaranteed.
2759  */
2760 void ib_drain_sq(struct ib_qp *qp)
2761 {
2762         if (qp->device->ops.drain_sq)
2763                 qp->device->ops.drain_sq(qp);
2764         else
2765                 __ib_drain_sq(qp);
2766         trace_cq_drain_complete(qp->send_cq);
2767 }
2768 EXPORT_SYMBOL(ib_drain_sq);
2769
2770 /**
2771  * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2772  *                 application.
2773  * @qp:            queue pair to drain
2774  *
2775  * If the device has a provider-specific drain function, then
2776  * call that.  Otherwise call the generic drain function
2777  * __ib_drain_rq().
2778  *
2779  * The caller must:
2780  *
2781  * ensure there is room in the CQ and RQ for the drain work request and
2782  * completion.
2783  *
2784  * allocate the CQ using ib_alloc_cq().
2785  *
2786  * ensure that there are no other contexts that are posting WRs concurrently.
2787  * Otherwise the drain is not guaranteed.
2788  */
2789 void ib_drain_rq(struct ib_qp *qp)
2790 {
2791         if (qp->device->ops.drain_rq)
2792                 qp->device->ops.drain_rq(qp);
2793         else
2794                 __ib_drain_rq(qp);
2795         trace_cq_drain_complete(qp->recv_cq);
2796 }
2797 EXPORT_SYMBOL(ib_drain_rq);
2798
2799 /**
2800  * ib_drain_qp() - Block until all CQEs have been consumed by the
2801  *                 application on both the RQ and SQ.
2802  * @qp:            queue pair to drain
2803  *
2804  * The caller must:
2805  *
2806  * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2807  * and completions.
2808  *
2809  * allocate the CQs using ib_alloc_cq().
2810  *
2811  * ensure that there are no other contexts that are posting WRs concurrently.
2812  * Otherwise the drain is not guaranteed.
2813  */
2814 void ib_drain_qp(struct ib_qp *qp)
2815 {
2816         ib_drain_sq(qp);
2817         if (!qp->srq)
2818                 ib_drain_rq(qp);
2819 }
2820 EXPORT_SYMBOL(ib_drain_qp);
2821
2822 struct net_device *rdma_alloc_netdev(struct ib_device *device, u8 port_num,
2823                                      enum rdma_netdev_t type, const char *name,
2824                                      unsigned char name_assign_type,
2825                                      void (*setup)(struct net_device *))
2826 {
2827         struct rdma_netdev_alloc_params params;
2828         struct net_device *netdev;
2829         int rc;
2830
2831         if (!device->ops.rdma_netdev_get_params)
2832                 return ERR_PTR(-EOPNOTSUPP);
2833
2834         rc = device->ops.rdma_netdev_get_params(device, port_num, type,
2835                                                 &params);
2836         if (rc)
2837                 return ERR_PTR(rc);
2838
2839         netdev = alloc_netdev_mqs(params.sizeof_priv, name, name_assign_type,
2840                                   setup, params.txqs, params.rxqs);
2841         if (!netdev)
2842                 return ERR_PTR(-ENOMEM);
2843
2844         return netdev;
2845 }
2846 EXPORT_SYMBOL(rdma_alloc_netdev);
2847
2848 int rdma_init_netdev(struct ib_device *device, u8 port_num,
2849                      enum rdma_netdev_t type, const char *name,
2850                      unsigned char name_assign_type,
2851                      void (*setup)(struct net_device *),
2852                      struct net_device *netdev)
2853 {
2854         struct rdma_netdev_alloc_params params;
2855         int rc;
2856
2857         if (!device->ops.rdma_netdev_get_params)
2858                 return -EOPNOTSUPP;
2859
2860         rc = device->ops.rdma_netdev_get_params(device, port_num, type,
2861                                                 &params);
2862         if (rc)
2863                 return rc;
2864
2865         return params.initialize_rdma_netdev(device, port_num,
2866                                              netdev, params.param);
2867 }
2868 EXPORT_SYMBOL(rdma_init_netdev);
2869
2870 void __rdma_block_iter_start(struct ib_block_iter *biter,
2871                              struct scatterlist *sglist, unsigned int nents,
2872                              unsigned long pgsz)
2873 {
2874         memset(biter, 0, sizeof(struct ib_block_iter));
2875         biter->__sg = sglist;
2876         biter->__sg_nents = nents;
2877
2878         /* Driver provides best block size to use */
2879         biter->__pg_bit = __fls(pgsz);
2880 }
2881 EXPORT_SYMBOL(__rdma_block_iter_start);
2882
2883 bool __rdma_block_iter_next(struct ib_block_iter *biter)
2884 {
2885         unsigned int block_offset;
2886
2887         if (!biter->__sg_nents || !biter->__sg)
2888                 return false;
2889
2890         biter->__dma_addr = sg_dma_address(biter->__sg) + biter->__sg_advance;
2891         block_offset = biter->__dma_addr & (BIT_ULL(biter->__pg_bit) - 1);
2892         biter->__sg_advance += BIT_ULL(biter->__pg_bit) - block_offset;
2893
2894         if (biter->__sg_advance >= sg_dma_len(biter->__sg)) {
2895                 biter->__sg_advance = 0;
2896                 biter->__sg = sg_next(biter->__sg);
2897                 biter->__sg_nents--;
2898         }
2899
2900         return true;
2901 }
2902 EXPORT_SYMBOL(__rdma_block_iter_next);