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