xref: /openbmc/linux/drivers/infiniband/core/verbs.c (revision 3cea7b4a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
31da177e4SLinus Torvalds  * Copyright (c) 2004 Infinicon Corporation.  All rights reserved.
41da177e4SLinus Torvalds  * Copyright (c) 2004 Intel Corporation.  All rights reserved.
51da177e4SLinus Torvalds  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
61da177e4SLinus Torvalds  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
72a1d9b7fSRoland Dreier  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
833b9b3eeSRoland Dreier  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * This software is available to you under a choice of one of two
111da177e4SLinus Torvalds  * licenses.  You may choose to be licensed under the terms of the GNU
121da177e4SLinus Torvalds  * General Public License (GPL) Version 2, available from the file
131da177e4SLinus Torvalds  * COPYING in the main directory of this source tree, or the
141da177e4SLinus Torvalds  * OpenIB.org BSD license below:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *     Redistribution and use in source and binary forms, with or
171da177e4SLinus Torvalds  *     without modification, are permitted provided that the following
181da177e4SLinus Torvalds  *     conditions are met:
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *      - Redistributions of source code must retain the above
211da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
221da177e4SLinus Torvalds  *        disclaimer.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *      - Redistributions in binary form must reproduce the above
251da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
261da177e4SLinus Torvalds  *        disclaimer in the documentation and/or other materials
271da177e4SLinus Torvalds  *        provided with the distribution.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
301da177e4SLinus Torvalds  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
311da177e4SLinus Torvalds  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
321da177e4SLinus Torvalds  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
331da177e4SLinus Torvalds  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
341da177e4SLinus Torvalds  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
351da177e4SLinus Torvalds  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
361da177e4SLinus Torvalds  * SOFTWARE.
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <linux/errno.h>
401da177e4SLinus Torvalds #include <linux/err.h>
41b108d976SPaul Gortmaker #include <linux/export.h>
428c65b4a6STim Schmielau #include <linux/string.h>
430e0ec7e0SSean Hefty #include <linux/slab.h>
44dbf727deSMatan Barak #include <linux/in.h>
45dbf727deSMatan Barak #include <linux/in6.h>
46dbf727deSMatan Barak #include <net/addrconf.h>
47d291f1a6SDaniel Jurgens #include <linux/security.h>
481da177e4SLinus Torvalds 
49a4d61e84SRoland Dreier #include <rdma/ib_verbs.h>
50a4d61e84SRoland Dreier #include <rdma/ib_cache.h>
51dd5f03beSMatan Barak #include <rdma/ib_addr.h>
52a060b562SChristoph Hellwig #include <rdma/rw.h>
5351aab126SMaor Gottlieb #include <rdma/lag.h>
541da177e4SLinus Torvalds 
55ed4c54e5SOr Gerlitz #include "core_priv.h"
56622db5b6SChuck Lever #include <trace/events/rdma_core.h>
571da177e4SLinus Torvalds 
58c0348eb0SParav Pandit static int ib_resolve_eth_dmac(struct ib_device *device,
59c0348eb0SParav Pandit 			       struct rdma_ah_attr *ah_attr);
60c0348eb0SParav Pandit 
612b1b5b60SSagi Grimberg static const char * const ib_events[] = {
622b1b5b60SSagi Grimberg 	[IB_EVENT_CQ_ERR]		= "CQ error",
632b1b5b60SSagi Grimberg 	[IB_EVENT_QP_FATAL]		= "QP fatal error",
642b1b5b60SSagi Grimberg 	[IB_EVENT_QP_REQ_ERR]		= "QP request error",
652b1b5b60SSagi Grimberg 	[IB_EVENT_QP_ACCESS_ERR]	= "QP access error",
662b1b5b60SSagi Grimberg 	[IB_EVENT_COMM_EST]		= "communication established",
672b1b5b60SSagi Grimberg 	[IB_EVENT_SQ_DRAINED]		= "send queue drained",
682b1b5b60SSagi Grimberg 	[IB_EVENT_PATH_MIG]		= "path migration successful",
692b1b5b60SSagi Grimberg 	[IB_EVENT_PATH_MIG_ERR]		= "path migration error",
702b1b5b60SSagi Grimberg 	[IB_EVENT_DEVICE_FATAL]		= "device fatal error",
712b1b5b60SSagi Grimberg 	[IB_EVENT_PORT_ACTIVE]		= "port active",
722b1b5b60SSagi Grimberg 	[IB_EVENT_PORT_ERR]		= "port error",
732b1b5b60SSagi Grimberg 	[IB_EVENT_LID_CHANGE]		= "LID change",
742b1b5b60SSagi Grimberg 	[IB_EVENT_PKEY_CHANGE]		= "P_key change",
752b1b5b60SSagi Grimberg 	[IB_EVENT_SM_CHANGE]		= "SM change",
762b1b5b60SSagi Grimberg 	[IB_EVENT_SRQ_ERR]		= "SRQ error",
772b1b5b60SSagi Grimberg 	[IB_EVENT_SRQ_LIMIT_REACHED]	= "SRQ limit reached",
782b1b5b60SSagi Grimberg 	[IB_EVENT_QP_LAST_WQE_REACHED]	= "last WQE reached",
792b1b5b60SSagi Grimberg 	[IB_EVENT_CLIENT_REREGISTER]	= "client reregister",
802b1b5b60SSagi Grimberg 	[IB_EVENT_GID_CHANGE]		= "GID changed",
812b1b5b60SSagi Grimberg };
822b1b5b60SSagi Grimberg 
83db7489e0SBart Van Assche const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
842b1b5b60SSagi Grimberg {
852b1b5b60SSagi Grimberg 	size_t index = event;
862b1b5b60SSagi Grimberg 
872b1b5b60SSagi Grimberg 	return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
882b1b5b60SSagi Grimberg 			ib_events[index] : "unrecognized event";
892b1b5b60SSagi Grimberg }
902b1b5b60SSagi Grimberg EXPORT_SYMBOL(ib_event_msg);
912b1b5b60SSagi Grimberg 
922b1b5b60SSagi Grimberg static const char * const wc_statuses[] = {
932b1b5b60SSagi Grimberg 	[IB_WC_SUCCESS]			= "success",
942b1b5b60SSagi Grimberg 	[IB_WC_LOC_LEN_ERR]		= "local length error",
952b1b5b60SSagi Grimberg 	[IB_WC_LOC_QP_OP_ERR]		= "local QP operation error",
962b1b5b60SSagi Grimberg 	[IB_WC_LOC_EEC_OP_ERR]		= "local EE context operation error",
972b1b5b60SSagi Grimberg 	[IB_WC_LOC_PROT_ERR]		= "local protection error",
982b1b5b60SSagi Grimberg 	[IB_WC_WR_FLUSH_ERR]		= "WR flushed",
997d8f3465SYixian Liu 	[IB_WC_MW_BIND_ERR]		= "memory bind operation error",
1002b1b5b60SSagi Grimberg 	[IB_WC_BAD_RESP_ERR]		= "bad response error",
1012b1b5b60SSagi Grimberg 	[IB_WC_LOC_ACCESS_ERR]		= "local access error",
1027d8f3465SYixian Liu 	[IB_WC_REM_INV_REQ_ERR]		= "remote invalid request error",
1032b1b5b60SSagi Grimberg 	[IB_WC_REM_ACCESS_ERR]		= "remote access error",
1042b1b5b60SSagi Grimberg 	[IB_WC_REM_OP_ERR]		= "remote operation error",
1052b1b5b60SSagi Grimberg 	[IB_WC_RETRY_EXC_ERR]		= "transport retry counter exceeded",
1062b1b5b60SSagi Grimberg 	[IB_WC_RNR_RETRY_EXC_ERR]	= "RNR retry counter exceeded",
1072b1b5b60SSagi Grimberg 	[IB_WC_LOC_RDD_VIOL_ERR]	= "local RDD violation error",
1082b1b5b60SSagi Grimberg 	[IB_WC_REM_INV_RD_REQ_ERR]	= "remote invalid RD request",
1092b1b5b60SSagi Grimberg 	[IB_WC_REM_ABORT_ERR]		= "operation aborted",
1102b1b5b60SSagi Grimberg 	[IB_WC_INV_EECN_ERR]		= "invalid EE context number",
1112b1b5b60SSagi Grimberg 	[IB_WC_INV_EEC_STATE_ERR]	= "invalid EE context state",
1122b1b5b60SSagi Grimberg 	[IB_WC_FATAL_ERR]		= "fatal error",
1132b1b5b60SSagi Grimberg 	[IB_WC_RESP_TIMEOUT_ERR]	= "response timeout error",
1142b1b5b60SSagi Grimberg 	[IB_WC_GENERAL_ERR]		= "general error",
1152b1b5b60SSagi Grimberg };
1162b1b5b60SSagi Grimberg 
117db7489e0SBart Van Assche const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
1182b1b5b60SSagi Grimberg {
1192b1b5b60SSagi Grimberg 	size_t index = status;
1202b1b5b60SSagi Grimberg 
1212b1b5b60SSagi Grimberg 	return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
1222b1b5b60SSagi Grimberg 			wc_statuses[index] : "unrecognized status";
1232b1b5b60SSagi Grimberg }
1242b1b5b60SSagi Grimberg EXPORT_SYMBOL(ib_wc_status_msg);
1252b1b5b60SSagi Grimberg 
1268385fd84SRoland Dreier __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
127bf6a9e31SJack Morgenstein {
128bf6a9e31SJack Morgenstein 	switch (rate) {
129bf6a9e31SJack Morgenstein 	case IB_RATE_2_5_GBPS: return   1;
130bf6a9e31SJack Morgenstein 	case IB_RATE_5_GBPS:   return   2;
131bf6a9e31SJack Morgenstein 	case IB_RATE_10_GBPS:  return   4;
132bf6a9e31SJack Morgenstein 	case IB_RATE_20_GBPS:  return   8;
133bf6a9e31SJack Morgenstein 	case IB_RATE_30_GBPS:  return  12;
134bf6a9e31SJack Morgenstein 	case IB_RATE_40_GBPS:  return  16;
135bf6a9e31SJack Morgenstein 	case IB_RATE_60_GBPS:  return  24;
136bf6a9e31SJack Morgenstein 	case IB_RATE_80_GBPS:  return  32;
137bf6a9e31SJack Morgenstein 	case IB_RATE_120_GBPS: return  48;
138e2dda368SHans Westgaard Ry 	case IB_RATE_14_GBPS:  return   6;
139e2dda368SHans Westgaard Ry 	case IB_RATE_56_GBPS:  return  22;
140e2dda368SHans Westgaard Ry 	case IB_RATE_112_GBPS: return  45;
141e2dda368SHans Westgaard Ry 	case IB_RATE_168_GBPS: return  67;
142e2dda368SHans Westgaard Ry 	case IB_RATE_25_GBPS:  return  10;
143e2dda368SHans Westgaard Ry 	case IB_RATE_100_GBPS: return  40;
144e2dda368SHans Westgaard Ry 	case IB_RATE_200_GBPS: return  80;
145e2dda368SHans Westgaard Ry 	case IB_RATE_300_GBPS: return 120;
146a5a5d199SMichael Guralnik 	case IB_RATE_28_GBPS:  return  11;
147a5a5d199SMichael Guralnik 	case IB_RATE_50_GBPS:  return  20;
148a5a5d199SMichael Guralnik 	case IB_RATE_400_GBPS: return 160;
149a5a5d199SMichael Guralnik 	case IB_RATE_600_GBPS: return 240;
150bf6a9e31SJack Morgenstein 	default:	       return  -1;
151bf6a9e31SJack Morgenstein 	}
152bf6a9e31SJack Morgenstein }
153bf6a9e31SJack Morgenstein EXPORT_SYMBOL(ib_rate_to_mult);
154bf6a9e31SJack Morgenstein 
1558385fd84SRoland Dreier __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
156bf6a9e31SJack Morgenstein {
157bf6a9e31SJack Morgenstein 	switch (mult) {
158bf6a9e31SJack Morgenstein 	case 1:   return IB_RATE_2_5_GBPS;
159bf6a9e31SJack Morgenstein 	case 2:   return IB_RATE_5_GBPS;
160bf6a9e31SJack Morgenstein 	case 4:   return IB_RATE_10_GBPS;
161bf6a9e31SJack Morgenstein 	case 8:   return IB_RATE_20_GBPS;
162bf6a9e31SJack Morgenstein 	case 12:  return IB_RATE_30_GBPS;
163bf6a9e31SJack Morgenstein 	case 16:  return IB_RATE_40_GBPS;
164bf6a9e31SJack Morgenstein 	case 24:  return IB_RATE_60_GBPS;
165bf6a9e31SJack Morgenstein 	case 32:  return IB_RATE_80_GBPS;
166bf6a9e31SJack Morgenstein 	case 48:  return IB_RATE_120_GBPS;
167e2dda368SHans Westgaard Ry 	case 6:   return IB_RATE_14_GBPS;
168e2dda368SHans Westgaard Ry 	case 22:  return IB_RATE_56_GBPS;
169e2dda368SHans Westgaard Ry 	case 45:  return IB_RATE_112_GBPS;
170e2dda368SHans Westgaard Ry 	case 67:  return IB_RATE_168_GBPS;
171e2dda368SHans Westgaard Ry 	case 10:  return IB_RATE_25_GBPS;
172e2dda368SHans Westgaard Ry 	case 40:  return IB_RATE_100_GBPS;
173e2dda368SHans Westgaard Ry 	case 80:  return IB_RATE_200_GBPS;
174e2dda368SHans Westgaard Ry 	case 120: return IB_RATE_300_GBPS;
175a5a5d199SMichael Guralnik 	case 11:  return IB_RATE_28_GBPS;
176a5a5d199SMichael Guralnik 	case 20:  return IB_RATE_50_GBPS;
177a5a5d199SMichael Guralnik 	case 160: return IB_RATE_400_GBPS;
178a5a5d199SMichael Guralnik 	case 240: return IB_RATE_600_GBPS;
179bf6a9e31SJack Morgenstein 	default:  return IB_RATE_PORT_CURRENT;
180bf6a9e31SJack Morgenstein 	}
181bf6a9e31SJack Morgenstein }
182bf6a9e31SJack Morgenstein EXPORT_SYMBOL(mult_to_ib_rate);
183bf6a9e31SJack Morgenstein 
1848385fd84SRoland Dreier __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
18571eeba16SMarcel Apfelbaum {
18671eeba16SMarcel Apfelbaum 	switch (rate) {
18771eeba16SMarcel Apfelbaum 	case IB_RATE_2_5_GBPS: return 2500;
18871eeba16SMarcel Apfelbaum 	case IB_RATE_5_GBPS:   return 5000;
18971eeba16SMarcel Apfelbaum 	case IB_RATE_10_GBPS:  return 10000;
19071eeba16SMarcel Apfelbaum 	case IB_RATE_20_GBPS:  return 20000;
19171eeba16SMarcel Apfelbaum 	case IB_RATE_30_GBPS:  return 30000;
19271eeba16SMarcel Apfelbaum 	case IB_RATE_40_GBPS:  return 40000;
19371eeba16SMarcel Apfelbaum 	case IB_RATE_60_GBPS:  return 60000;
19471eeba16SMarcel Apfelbaum 	case IB_RATE_80_GBPS:  return 80000;
19571eeba16SMarcel Apfelbaum 	case IB_RATE_120_GBPS: return 120000;
19671eeba16SMarcel Apfelbaum 	case IB_RATE_14_GBPS:  return 14062;
19771eeba16SMarcel Apfelbaum 	case IB_RATE_56_GBPS:  return 56250;
19871eeba16SMarcel Apfelbaum 	case IB_RATE_112_GBPS: return 112500;
19971eeba16SMarcel Apfelbaum 	case IB_RATE_168_GBPS: return 168750;
20071eeba16SMarcel Apfelbaum 	case IB_RATE_25_GBPS:  return 25781;
20171eeba16SMarcel Apfelbaum 	case IB_RATE_100_GBPS: return 103125;
20271eeba16SMarcel Apfelbaum 	case IB_RATE_200_GBPS: return 206250;
20371eeba16SMarcel Apfelbaum 	case IB_RATE_300_GBPS: return 309375;
204a5a5d199SMichael Guralnik 	case IB_RATE_28_GBPS:  return 28125;
205a5a5d199SMichael Guralnik 	case IB_RATE_50_GBPS:  return 53125;
206a5a5d199SMichael Guralnik 	case IB_RATE_400_GBPS: return 425000;
207a5a5d199SMichael Guralnik 	case IB_RATE_600_GBPS: return 637500;
20871eeba16SMarcel Apfelbaum 	default:	       return -1;
20971eeba16SMarcel Apfelbaum 	}
21071eeba16SMarcel Apfelbaum }
21171eeba16SMarcel Apfelbaum EXPORT_SYMBOL(ib_rate_to_mbps);
21271eeba16SMarcel Apfelbaum 
2138385fd84SRoland Dreier __attribute_const__ enum rdma_transport_type
2145d60c111SJason Gunthorpe rdma_node_get_transport(unsigned int node_type)
21507ebafbaSTom Tucker {
216cdc596d8SLeon Romanovsky 
217cdc596d8SLeon Romanovsky 	if (node_type == RDMA_NODE_USNIC)
2185db5765eSUpinder Malhi 		return RDMA_TRANSPORT_USNIC;
219cdc596d8SLeon Romanovsky 	if (node_type == RDMA_NODE_USNIC_UDP)
220248567f7SUpinder Malhi 		return RDMA_TRANSPORT_USNIC_UDP;
221cdc596d8SLeon Romanovsky 	if (node_type == RDMA_NODE_RNIC)
222cdc596d8SLeon Romanovsky 		return RDMA_TRANSPORT_IWARP;
223f95be3d2SGal Pressman 	if (node_type == RDMA_NODE_UNSPECIFIED)
224f95be3d2SGal Pressman 		return RDMA_TRANSPORT_UNSPECIFIED;
225cdc596d8SLeon Romanovsky 
226cdc596d8SLeon Romanovsky 	return RDMA_TRANSPORT_IB;
22707ebafbaSTom Tucker }
22807ebafbaSTom Tucker EXPORT_SYMBOL(rdma_node_get_transport);
22907ebafbaSTom Tucker 
2301fb7f897SMark Bloch enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device,
2311fb7f897SMark Bloch 					      u32 port_num)
232a3f5adafSEli Cohen {
23382901e3eSLeon Romanovsky 	enum rdma_transport_type lt;
2343023a1e9SKamal Heib 	if (device->ops.get_link_layer)
2353023a1e9SKamal Heib 		return device->ops.get_link_layer(device, port_num);
236a3f5adafSEli Cohen 
23782901e3eSLeon Romanovsky 	lt = rdma_node_get_transport(device->node_type);
23882901e3eSLeon Romanovsky 	if (lt == RDMA_TRANSPORT_IB)
239a3f5adafSEli Cohen 		return IB_LINK_LAYER_INFINIBAND;
24082901e3eSLeon Romanovsky 
241a3f5adafSEli Cohen 	return IB_LINK_LAYER_ETHERNET;
242a3f5adafSEli Cohen }
243a3f5adafSEli Cohen EXPORT_SYMBOL(rdma_port_get_link_layer);
244a3f5adafSEli Cohen 
2451da177e4SLinus Torvalds /* Protection domains */
2461da177e4SLinus Torvalds 
24796249d70SJason Gunthorpe /**
2482988ca08SMauro Carvalho Chehab  * __ib_alloc_pd - Allocates an unused protection domain.
24996249d70SJason Gunthorpe  * @device: The device on which to allocate the protection domain.
250094c88f3Srd.dunlab@gmail.com  * @flags: protection domain flags
251094c88f3Srd.dunlab@gmail.com  * @caller: caller's build-time module name
25296249d70SJason Gunthorpe  *
25396249d70SJason Gunthorpe  * A protection domain object provides an association between QPs, shared
25496249d70SJason Gunthorpe  * receive queues, address handles, memory regions, and memory windows.
25596249d70SJason Gunthorpe  *
25696249d70SJason Gunthorpe  * Every PD has a local_dma_lkey which can be used as the lkey value for local
25796249d70SJason Gunthorpe  * memory operations.
25896249d70SJason Gunthorpe  */
259ed082d36SChristoph Hellwig struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
260ed082d36SChristoph Hellwig 		const char *caller)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	struct ib_pd *pd;
263ed082d36SChristoph Hellwig 	int mr_access_flags = 0;
26421a428a0SLeon Romanovsky 	int ret;
2651da177e4SLinus Torvalds 
26621a428a0SLeon Romanovsky 	pd = rdma_zalloc_drv_obj(device, ib_pd);
26721a428a0SLeon Romanovsky 	if (!pd)
26821a428a0SLeon Romanovsky 		return ERR_PTR(-ENOMEM);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	pd->device = device;
271b5e81bf5SRoland Dreier 	pd->uobject = NULL;
27250d46335SChristoph Hellwig 	pd->__internal_mr = NULL;
2731da177e4SLinus Torvalds 	atomic_set(&pd->usecnt, 0);
274ed082d36SChristoph Hellwig 	pd->flags = flags;
27596249d70SJason Gunthorpe 
27613ef5539SLeon Romanovsky 	rdma_restrack_new(&pd->res, RDMA_RESTRACK_PD);
277b09c4d70SLeon Romanovsky 	rdma_restrack_set_name(&pd->res, caller);
27821a428a0SLeon Romanovsky 
279ff23dfa1SShamir Rabinovitch 	ret = device->ops.alloc_pd(pd, NULL);
28021a428a0SLeon Romanovsky 	if (ret) {
28113ef5539SLeon Romanovsky 		rdma_restrack_put(&pd->res);
28221a428a0SLeon Romanovsky 		kfree(pd);
28321a428a0SLeon Romanovsky 		return ERR_PTR(ret);
28421a428a0SLeon Romanovsky 	}
285c34a23c2SLeon Romanovsky 	rdma_restrack_add(&pd->res);
28621a428a0SLeon Romanovsky 
28786bee4c9SOr Gerlitz 	if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
28896249d70SJason Gunthorpe 		pd->local_dma_lkey = device->local_dma_lkey;
289ed082d36SChristoph Hellwig 	else
290ed082d36SChristoph Hellwig 		mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
291ed082d36SChristoph Hellwig 
292ed082d36SChristoph Hellwig 	if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
293ed082d36SChristoph Hellwig 		pr_warn("%s: enabling unsafe global rkey\n", caller);
294ed082d36SChristoph Hellwig 		mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
295ed082d36SChristoph Hellwig 	}
296ed082d36SChristoph Hellwig 
297ed082d36SChristoph Hellwig 	if (mr_access_flags) {
29896249d70SJason Gunthorpe 		struct ib_mr *mr;
29996249d70SJason Gunthorpe 
3003023a1e9SKamal Heib 		mr = pd->device->ops.get_dma_mr(pd, mr_access_flags);
30196249d70SJason Gunthorpe 		if (IS_ERR(mr)) {
30296249d70SJason Gunthorpe 			ib_dealloc_pd(pd);
3035ef990f0SChristoph Hellwig 			return ERR_CAST(mr);
3041da177e4SLinus Torvalds 		}
3051da177e4SLinus Torvalds 
3065ef990f0SChristoph Hellwig 		mr->device	= pd->device;
3075ef990f0SChristoph Hellwig 		mr->pd		= pd;
308a0bc099aSMax Gurtovoy 		mr->type        = IB_MR_TYPE_DMA;
3095ef990f0SChristoph Hellwig 		mr->uobject	= NULL;
3105ef990f0SChristoph Hellwig 		mr->need_inval	= false;
3115ef990f0SChristoph Hellwig 
31250d46335SChristoph Hellwig 		pd->__internal_mr = mr;
313ed082d36SChristoph Hellwig 
314ed082d36SChristoph Hellwig 		if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
31550d46335SChristoph Hellwig 			pd->local_dma_lkey = pd->__internal_mr->lkey;
316ed082d36SChristoph Hellwig 
317ed082d36SChristoph Hellwig 		if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
318ed082d36SChristoph Hellwig 			pd->unsafe_global_rkey = pd->__internal_mr->rkey;
31996249d70SJason Gunthorpe 	}
320ed082d36SChristoph Hellwig 
3211da177e4SLinus Torvalds 	return pd;
3221da177e4SLinus Torvalds }
323ed082d36SChristoph Hellwig EXPORT_SYMBOL(__ib_alloc_pd);
3241da177e4SLinus Torvalds 
3257dd78647SJason Gunthorpe /**
32691f57129SIsrael Rukshin  * ib_dealloc_pd_user - Deallocates a protection domain.
3277dd78647SJason Gunthorpe  * @pd: The protection domain to deallocate.
328c4367a26SShamir Rabinovitch  * @udata: Valid user data or NULL for kernel object
3297dd78647SJason Gunthorpe  *
3307dd78647SJason Gunthorpe  * It is an error to call this function while any resources in the pd still
3317dd78647SJason Gunthorpe  * exist.  The caller is responsible to synchronously destroy them and
3327dd78647SJason Gunthorpe  * guarantee no new allocations will happen.
3337dd78647SJason Gunthorpe  */
33491a7c58fSLeon Romanovsky int ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata)
3351da177e4SLinus Torvalds {
3367dd78647SJason Gunthorpe 	int ret;
3371da177e4SLinus Torvalds 
33850d46335SChristoph Hellwig 	if (pd->__internal_mr) {
339c4367a26SShamir Rabinovitch 		ret = pd->device->ops.dereg_mr(pd->__internal_mr, NULL);
3407dd78647SJason Gunthorpe 		WARN_ON(ret);
34150d46335SChristoph Hellwig 		pd->__internal_mr = NULL;
34296249d70SJason Gunthorpe 	}
34396249d70SJason Gunthorpe 
3447dd78647SJason Gunthorpe 	/* uverbs manipulates usecnt with proper locking, while the kabi
34526caea5fSWenpeng Liang 	 * requires the caller to guarantee we can't race here.
34626caea5fSWenpeng Liang 	 */
3477dd78647SJason Gunthorpe 	WARN_ON(atomic_read(&pd->usecnt));
3481da177e4SLinus Torvalds 
34991a7c58fSLeon Romanovsky 	ret = pd->device->ops.dealloc_pd(pd, udata);
35091a7c58fSLeon Romanovsky 	if (ret)
35191a7c58fSLeon Romanovsky 		return ret;
35291a7c58fSLeon Romanovsky 
3539d5f8c20SLeon Romanovsky 	rdma_restrack_del(&pd->res);
35421a428a0SLeon Romanovsky 	kfree(pd);
35591a7c58fSLeon Romanovsky 	return ret;
3561da177e4SLinus Torvalds }
357c4367a26SShamir Rabinovitch EXPORT_SYMBOL(ib_dealloc_pd_user);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /* Address handles */
3601da177e4SLinus Torvalds 
361d97099feSJason Gunthorpe /**
362d97099feSJason Gunthorpe  * rdma_copy_ah_attr - Copy rdma ah attribute from source to destination.
363d97099feSJason Gunthorpe  * @dest:       Pointer to destination ah_attr. Contents of the destination
364d97099feSJason Gunthorpe  *              pointer is assumed to be invalid and attribute are overwritten.
365d97099feSJason Gunthorpe  * @src:        Pointer to source ah_attr.
366d97099feSJason Gunthorpe  */
367d97099feSJason Gunthorpe void rdma_copy_ah_attr(struct rdma_ah_attr *dest,
368d97099feSJason Gunthorpe 		       const struct rdma_ah_attr *src)
369d97099feSJason Gunthorpe {
370d97099feSJason Gunthorpe 	*dest = *src;
371d97099feSJason Gunthorpe 	if (dest->grh.sgid_attr)
372d97099feSJason Gunthorpe 		rdma_hold_gid_attr(dest->grh.sgid_attr);
373d97099feSJason Gunthorpe }
374d97099feSJason Gunthorpe EXPORT_SYMBOL(rdma_copy_ah_attr);
375d97099feSJason Gunthorpe 
376d97099feSJason Gunthorpe /**
377d97099feSJason Gunthorpe  * rdma_replace_ah_attr - Replace valid ah_attr with new new one.
378d97099feSJason Gunthorpe  * @old:        Pointer to existing ah_attr which needs to be replaced.
379d97099feSJason Gunthorpe  *              old is assumed to be valid or zero'd
380d97099feSJason Gunthorpe  * @new:        Pointer to the new ah_attr.
381d97099feSJason Gunthorpe  *
382d97099feSJason Gunthorpe  * rdma_replace_ah_attr() first releases any reference in the old ah_attr if
383d97099feSJason Gunthorpe  * old the ah_attr is valid; after that it copies the new attribute and holds
384d97099feSJason Gunthorpe  * the reference to the replaced ah_attr.
385d97099feSJason Gunthorpe  */
386d97099feSJason Gunthorpe void rdma_replace_ah_attr(struct rdma_ah_attr *old,
387d97099feSJason Gunthorpe 			  const struct rdma_ah_attr *new)
388d97099feSJason Gunthorpe {
389d97099feSJason Gunthorpe 	rdma_destroy_ah_attr(old);
390d97099feSJason Gunthorpe 	*old = *new;
391d97099feSJason Gunthorpe 	if (old->grh.sgid_attr)
392d97099feSJason Gunthorpe 		rdma_hold_gid_attr(old->grh.sgid_attr);
393d97099feSJason Gunthorpe }
394d97099feSJason Gunthorpe EXPORT_SYMBOL(rdma_replace_ah_attr);
395d97099feSJason Gunthorpe 
396d97099feSJason Gunthorpe /**
397d97099feSJason Gunthorpe  * rdma_move_ah_attr - Move ah_attr pointed by source to destination.
398d97099feSJason Gunthorpe  * @dest:       Pointer to destination ah_attr to copy to.
399d97099feSJason Gunthorpe  *              dest is assumed to be valid or zero'd
400d97099feSJason Gunthorpe  * @src:        Pointer to the new ah_attr.
401d97099feSJason Gunthorpe  *
402d97099feSJason Gunthorpe  * rdma_move_ah_attr() first releases any reference in the destination ah_attr
403d97099feSJason Gunthorpe  * if it is valid. This also transfers ownership of internal references from
404d97099feSJason Gunthorpe  * src to dest, making src invalid in the process. No new reference of the src
405d97099feSJason Gunthorpe  * ah_attr is taken.
406d97099feSJason Gunthorpe  */
407d97099feSJason Gunthorpe void rdma_move_ah_attr(struct rdma_ah_attr *dest, struct rdma_ah_attr *src)
408d97099feSJason Gunthorpe {
409d97099feSJason Gunthorpe 	rdma_destroy_ah_attr(dest);
410d97099feSJason Gunthorpe 	*dest = *src;
411d97099feSJason Gunthorpe 	src->grh.sgid_attr = NULL;
412d97099feSJason Gunthorpe }
413d97099feSJason Gunthorpe EXPORT_SYMBOL(rdma_move_ah_attr);
414d97099feSJason Gunthorpe 
4158d9ec9adSJason Gunthorpe /*
4168d9ec9adSJason Gunthorpe  * Validate that the rdma_ah_attr is valid for the device before passing it
4178d9ec9adSJason Gunthorpe  * off to the driver.
4188d9ec9adSJason Gunthorpe  */
4198d9ec9adSJason Gunthorpe static int rdma_check_ah_attr(struct ib_device *device,
4208d9ec9adSJason Gunthorpe 			      struct rdma_ah_attr *ah_attr)
4218d9ec9adSJason Gunthorpe {
4228d9ec9adSJason Gunthorpe 	if (!rdma_is_port_valid(device, ah_attr->port_num))
4238d9ec9adSJason Gunthorpe 		return -EINVAL;
4248d9ec9adSJason Gunthorpe 
425b02289b3SArtemy Kovalyov 	if ((rdma_is_grh_required(device, ah_attr->port_num) ||
426b02289b3SArtemy Kovalyov 	     ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) &&
4278d9ec9adSJason Gunthorpe 	    !(ah_attr->ah_flags & IB_AH_GRH))
4288d9ec9adSJason Gunthorpe 		return -EINVAL;
4298d9ec9adSJason Gunthorpe 
4308d9ec9adSJason Gunthorpe 	if (ah_attr->grh.sgid_attr) {
4318d9ec9adSJason Gunthorpe 		/*
4328d9ec9adSJason Gunthorpe 		 * Make sure the passed sgid_attr is consistent with the
4338d9ec9adSJason Gunthorpe 		 * parameters
4348d9ec9adSJason Gunthorpe 		 */
4358d9ec9adSJason Gunthorpe 		if (ah_attr->grh.sgid_attr->index != ah_attr->grh.sgid_index ||
4368d9ec9adSJason Gunthorpe 		    ah_attr->grh.sgid_attr->port_num != ah_attr->port_num)
4378d9ec9adSJason Gunthorpe 			return -EINVAL;
4388d9ec9adSJason Gunthorpe 	}
4398d9ec9adSJason Gunthorpe 	return 0;
4408d9ec9adSJason Gunthorpe }
4418d9ec9adSJason Gunthorpe 
4428d9ec9adSJason Gunthorpe /*
4438d9ec9adSJason Gunthorpe  * If the ah requires a GRH then ensure that sgid_attr pointer is filled in.
4448d9ec9adSJason Gunthorpe  * On success the caller is responsible to call rdma_unfill_sgid_attr().
4458d9ec9adSJason Gunthorpe  */
4468d9ec9adSJason Gunthorpe static int rdma_fill_sgid_attr(struct ib_device *device,
4478d9ec9adSJason Gunthorpe 			       struct rdma_ah_attr *ah_attr,
4488d9ec9adSJason Gunthorpe 			       const struct ib_gid_attr **old_sgid_attr)
4498d9ec9adSJason Gunthorpe {
4508d9ec9adSJason Gunthorpe 	const struct ib_gid_attr *sgid_attr;
4518d9ec9adSJason Gunthorpe 	struct ib_global_route *grh;
4528d9ec9adSJason Gunthorpe 	int ret;
4538d9ec9adSJason Gunthorpe 
4548d9ec9adSJason Gunthorpe 	*old_sgid_attr = ah_attr->grh.sgid_attr;
4558d9ec9adSJason Gunthorpe 
4568d9ec9adSJason Gunthorpe 	ret = rdma_check_ah_attr(device, ah_attr);
4578d9ec9adSJason Gunthorpe 	if (ret)
4588d9ec9adSJason Gunthorpe 		return ret;
4598d9ec9adSJason Gunthorpe 
4608d9ec9adSJason Gunthorpe 	if (!(ah_attr->ah_flags & IB_AH_GRH))
4618d9ec9adSJason Gunthorpe 		return 0;
4628d9ec9adSJason Gunthorpe 
4638d9ec9adSJason Gunthorpe 	grh = rdma_ah_retrieve_grh(ah_attr);
4648d9ec9adSJason Gunthorpe 	if (grh->sgid_attr)
4658d9ec9adSJason Gunthorpe 		return 0;
4668d9ec9adSJason Gunthorpe 
4678d9ec9adSJason Gunthorpe 	sgid_attr =
4688d9ec9adSJason Gunthorpe 		rdma_get_gid_attr(device, ah_attr->port_num, grh->sgid_index);
4698d9ec9adSJason Gunthorpe 	if (IS_ERR(sgid_attr))
4708d9ec9adSJason Gunthorpe 		return PTR_ERR(sgid_attr);
4718d9ec9adSJason Gunthorpe 
4728d9ec9adSJason Gunthorpe 	/* Move ownerhip of the kref into the ah_attr */
4738d9ec9adSJason Gunthorpe 	grh->sgid_attr = sgid_attr;
4748d9ec9adSJason Gunthorpe 	return 0;
4758d9ec9adSJason Gunthorpe }
4768d9ec9adSJason Gunthorpe 
4778d9ec9adSJason Gunthorpe static void rdma_unfill_sgid_attr(struct rdma_ah_attr *ah_attr,
4788d9ec9adSJason Gunthorpe 				  const struct ib_gid_attr *old_sgid_attr)
4798d9ec9adSJason Gunthorpe {
4808d9ec9adSJason Gunthorpe 	/*
4818d9ec9adSJason Gunthorpe 	 * Fill didn't change anything, the caller retains ownership of
4828d9ec9adSJason Gunthorpe 	 * whatever it passed
4838d9ec9adSJason Gunthorpe 	 */
4848d9ec9adSJason Gunthorpe 	if (ah_attr->grh.sgid_attr == old_sgid_attr)
4858d9ec9adSJason Gunthorpe 		return;
4868d9ec9adSJason Gunthorpe 
4878d9ec9adSJason Gunthorpe 	/*
4888d9ec9adSJason Gunthorpe 	 * Otherwise, we need to undo what rdma_fill_sgid_attr so the caller
4898d9ec9adSJason Gunthorpe 	 * doesn't see any change in the rdma_ah_attr. If we get here
4908d9ec9adSJason Gunthorpe 	 * old_sgid_attr is NULL.
4918d9ec9adSJason Gunthorpe 	 */
4928d9ec9adSJason Gunthorpe 	rdma_destroy_ah_attr(ah_attr);
4938d9ec9adSJason Gunthorpe }
4948d9ec9adSJason Gunthorpe 
4951a1f460fSJason Gunthorpe static const struct ib_gid_attr *
4961a1f460fSJason Gunthorpe rdma_update_sgid_attr(struct rdma_ah_attr *ah_attr,
4971a1f460fSJason Gunthorpe 		      const struct ib_gid_attr *old_attr)
4981a1f460fSJason Gunthorpe {
4991a1f460fSJason Gunthorpe 	if (old_attr)
5001a1f460fSJason Gunthorpe 		rdma_put_gid_attr(old_attr);
5011a1f460fSJason Gunthorpe 	if (ah_attr->ah_flags & IB_AH_GRH) {
5021a1f460fSJason Gunthorpe 		rdma_hold_gid_attr(ah_attr->grh.sgid_attr);
5031a1f460fSJason Gunthorpe 		return ah_attr->grh.sgid_attr;
5041a1f460fSJason Gunthorpe 	}
5051a1f460fSJason Gunthorpe 	return NULL;
5061a1f460fSJason Gunthorpe }
5071a1f460fSJason Gunthorpe 
5085cda6587SParav Pandit static struct ib_ah *_rdma_create_ah(struct ib_pd *pd,
5095cda6587SParav Pandit 				     struct rdma_ah_attr *ah_attr,
510b090c4e3SGal Pressman 				     u32 flags,
51151aab126SMaor Gottlieb 				     struct ib_udata *udata,
51251aab126SMaor Gottlieb 				     struct net_device *xmit_slave)
5131da177e4SLinus Torvalds {
514fa5d010cSMaor Gottlieb 	struct rdma_ah_init_attr init_attr = {};
515d3456914SLeon Romanovsky 	struct ib_device *device = pd->device;
5161da177e4SLinus Torvalds 	struct ib_ah *ah;
517d3456914SLeon Romanovsky 	int ret;
5181da177e4SLinus Torvalds 
519b090c4e3SGal Pressman 	might_sleep_if(flags & RDMA_CREATE_AH_SLEEPABLE);
520b090c4e3SGal Pressman 
5218c030d78SGal Pressman 	if (!udata && !device->ops.create_ah)
5220584c47bSKamal Heib 		return ERR_PTR(-EOPNOTSUPP);
5230584c47bSKamal Heib 
524d3456914SLeon Romanovsky 	ah = rdma_zalloc_drv_obj_gfp(
525d3456914SLeon Romanovsky 		device, ib_ah,
526d3456914SLeon Romanovsky 		(flags & RDMA_CREATE_AH_SLEEPABLE) ? GFP_KERNEL : GFP_ATOMIC);
527d3456914SLeon Romanovsky 	if (!ah)
528d3456914SLeon Romanovsky 		return ERR_PTR(-ENOMEM);
5291da177e4SLinus Torvalds 
530d3456914SLeon Romanovsky 	ah->device = device;
5311da177e4SLinus Torvalds 	ah->pd = pd;
53244c58487SDasaratharaman Chandramouli 	ah->type = ah_attr->type;
5331a1f460fSJason Gunthorpe 	ah->sgid_attr = rdma_update_sgid_attr(ah_attr, NULL);
534fa5d010cSMaor Gottlieb 	init_attr.ah_attr = ah_attr;
535fa5d010cSMaor Gottlieb 	init_attr.flags = flags;
53651aab126SMaor Gottlieb 	init_attr.xmit_slave = xmit_slave;
5371a1f460fSJason Gunthorpe 
538676a80adSJason Gunthorpe 	if (udata)
539676a80adSJason Gunthorpe 		ret = device->ops.create_user_ah(ah, &init_attr, udata);
540676a80adSJason Gunthorpe 	else
541676a80adSJason Gunthorpe 		ret = device->ops.create_ah(ah, &init_attr, NULL);
542d3456914SLeon Romanovsky 	if (ret) {
543d3456914SLeon Romanovsky 		kfree(ah);
544d3456914SLeon Romanovsky 		return ERR_PTR(ret);
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
547d3456914SLeon Romanovsky 	atomic_inc(&pd->usecnt);
5481da177e4SLinus Torvalds 	return ah;
5491da177e4SLinus Torvalds }
5505cda6587SParav Pandit 
5518d9ec9adSJason Gunthorpe /**
5528d9ec9adSJason Gunthorpe  * rdma_create_ah - Creates an address handle for the
5538d9ec9adSJason Gunthorpe  * given address vector.
5548d9ec9adSJason Gunthorpe  * @pd: The protection domain associated with the address handle.
5558d9ec9adSJason Gunthorpe  * @ah_attr: The attributes of the address vector.
556b090c4e3SGal Pressman  * @flags: Create address handle flags (see enum rdma_create_ah_flags).
5578d9ec9adSJason Gunthorpe  *
5588d9ec9adSJason Gunthorpe  * It returns 0 on success and returns appropriate error code on error.
5598d9ec9adSJason Gunthorpe  * The address handle is used to reference a local or global destination
5608d9ec9adSJason Gunthorpe  * in all UD QP post sends.
5618d9ec9adSJason Gunthorpe  */
562b090c4e3SGal Pressman struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
563b090c4e3SGal Pressman 			     u32 flags)
5645cda6587SParav Pandit {
5658d9ec9adSJason Gunthorpe 	const struct ib_gid_attr *old_sgid_attr;
56651aab126SMaor Gottlieb 	struct net_device *slave;
5678d9ec9adSJason Gunthorpe 	struct ib_ah *ah;
5688d9ec9adSJason Gunthorpe 	int ret;
5698d9ec9adSJason Gunthorpe 
5708d9ec9adSJason Gunthorpe 	ret = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
5718d9ec9adSJason Gunthorpe 	if (ret)
5728d9ec9adSJason Gunthorpe 		return ERR_PTR(ret);
57351aab126SMaor Gottlieb 	slave = rdma_lag_get_ah_roce_slave(pd->device, ah_attr,
57451aab126SMaor Gottlieb 					   (flags & RDMA_CREATE_AH_SLEEPABLE) ?
57551aab126SMaor Gottlieb 					   GFP_KERNEL : GFP_ATOMIC);
57651aab126SMaor Gottlieb 	if (IS_ERR(slave)) {
57751aab126SMaor Gottlieb 		rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
57851aab126SMaor Gottlieb 		return (void *)slave;
57951aab126SMaor Gottlieb 	}
58051aab126SMaor Gottlieb 	ah = _rdma_create_ah(pd, ah_attr, flags, NULL, slave);
58151aab126SMaor Gottlieb 	rdma_lag_put_ah_roce_slave(slave);
5828d9ec9adSJason Gunthorpe 	rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
5838d9ec9adSJason Gunthorpe 	return ah;
5845cda6587SParav Pandit }
5850a18cfe4SDasaratharaman Chandramouli EXPORT_SYMBOL(rdma_create_ah);
5861da177e4SLinus Torvalds 
5875cda6587SParav Pandit /**
5885cda6587SParav Pandit  * rdma_create_user_ah - Creates an address handle for the
5895cda6587SParav Pandit  * given address vector.
5905cda6587SParav Pandit  * It resolves destination mac address for ah attribute of RoCE type.
5915cda6587SParav Pandit  * @pd: The protection domain associated with the address handle.
5925cda6587SParav Pandit  * @ah_attr: The attributes of the address vector.
5935cda6587SParav Pandit  * @udata: pointer to user's input output buffer information need by
5945cda6587SParav Pandit  *         provider driver.
5955cda6587SParav Pandit  *
5965cda6587SParav Pandit  * It returns 0 on success and returns appropriate error code on error.
5975cda6587SParav Pandit  * The address handle is used to reference a local or global destination
5985cda6587SParav Pandit  * in all UD QP post sends.
5995cda6587SParav Pandit  */
6005cda6587SParav Pandit struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
6015cda6587SParav Pandit 				  struct rdma_ah_attr *ah_attr,
6025cda6587SParav Pandit 				  struct ib_udata *udata)
6035cda6587SParav Pandit {
6048d9ec9adSJason Gunthorpe 	const struct ib_gid_attr *old_sgid_attr;
6058d9ec9adSJason Gunthorpe 	struct ib_ah *ah;
6065cda6587SParav Pandit 	int err;
6075cda6587SParav Pandit 
6088d9ec9adSJason Gunthorpe 	err = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
6098d9ec9adSJason Gunthorpe 	if (err)
6108d9ec9adSJason Gunthorpe 		return ERR_PTR(err);
6118d9ec9adSJason Gunthorpe 
6125cda6587SParav Pandit 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
6135cda6587SParav Pandit 		err = ib_resolve_eth_dmac(pd->device, ah_attr);
6148d9ec9adSJason Gunthorpe 		if (err) {
6158d9ec9adSJason Gunthorpe 			ah = ERR_PTR(err);
6168d9ec9adSJason Gunthorpe 			goto out;
6178d9ec9adSJason Gunthorpe 		}
6185cda6587SParav Pandit 	}
6195cda6587SParav Pandit 
62051aab126SMaor Gottlieb 	ah = _rdma_create_ah(pd, ah_attr, RDMA_CREATE_AH_SLEEPABLE,
62151aab126SMaor Gottlieb 			     udata, NULL);
6228d9ec9adSJason Gunthorpe 
6238d9ec9adSJason Gunthorpe out:
6248d9ec9adSJason Gunthorpe 	rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
6258d9ec9adSJason Gunthorpe 	return ah;
6265cda6587SParav Pandit }
6275cda6587SParav Pandit EXPORT_SYMBOL(rdma_create_user_ah);
6285cda6587SParav Pandit 
629850d8fd7SMoni Shoua int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
630c865f246SSomnath Kotur {
631c865f246SSomnath Kotur 	const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
632c865f246SSomnath Kotur 	struct iphdr ip4h_checked;
633c865f246SSomnath Kotur 	const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
634c865f246SSomnath Kotur 
635c865f246SSomnath Kotur 	/* If it's IPv6, the version must be 6, otherwise, the first
636c865f246SSomnath Kotur 	 * 20 bytes (before the IPv4 header) are garbled.
637c865f246SSomnath Kotur 	 */
638c865f246SSomnath Kotur 	if (ip6h->version != 6)
639c865f246SSomnath Kotur 		return (ip4h->version == 4) ? 4 : 0;
640c865f246SSomnath Kotur 	/* version may be 6 or 4 because the first 20 bytes could be garbled */
641c865f246SSomnath Kotur 
642c865f246SSomnath Kotur 	/* RoCE v2 requires no options, thus header length
643c865f246SSomnath Kotur 	 * must be 5 words
644c865f246SSomnath Kotur 	 */
645c865f246SSomnath Kotur 	if (ip4h->ihl != 5)
646c865f246SSomnath Kotur 		return 6;
647c865f246SSomnath Kotur 
648c865f246SSomnath Kotur 	/* Verify checksum.
649c865f246SSomnath Kotur 	 * We can't write on scattered buffers so we need to copy to
650c865f246SSomnath Kotur 	 * temp buffer.
651c865f246SSomnath Kotur 	 */
652c865f246SSomnath Kotur 	memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
653c865f246SSomnath Kotur 	ip4h_checked.check = 0;
654c865f246SSomnath Kotur 	ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
655c865f246SSomnath Kotur 	/* if IPv4 header checksum is OK, believe it */
656c865f246SSomnath Kotur 	if (ip4h->check == ip4h_checked.check)
657c865f246SSomnath Kotur 		return 4;
658c865f246SSomnath Kotur 	return 6;
659c865f246SSomnath Kotur }
660850d8fd7SMoni Shoua EXPORT_SYMBOL(ib_get_rdma_header_version);
661c865f246SSomnath Kotur 
662c865f246SSomnath Kotur static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
6631fb7f897SMark Bloch 						     u32 port_num,
664c865f246SSomnath Kotur 						     const struct ib_grh *grh)
665c865f246SSomnath Kotur {
666c865f246SSomnath Kotur 	int grh_version;
667c865f246SSomnath Kotur 
668c865f246SSomnath Kotur 	if (rdma_protocol_ib(device, port_num))
669c865f246SSomnath Kotur 		return RDMA_NETWORK_IB;
670c865f246SSomnath Kotur 
671850d8fd7SMoni Shoua 	grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
672c865f246SSomnath Kotur 
673c865f246SSomnath Kotur 	if (grh_version == 4)
674c865f246SSomnath Kotur 		return RDMA_NETWORK_IPV4;
675c865f246SSomnath Kotur 
676c865f246SSomnath Kotur 	if (grh->next_hdr == IPPROTO_UDP)
677c865f246SSomnath Kotur 		return RDMA_NETWORK_IPV6;
678c865f246SSomnath Kotur 
679c865f246SSomnath Kotur 	return RDMA_NETWORK_ROCE_V1;
680c865f246SSomnath Kotur }
681c865f246SSomnath Kotur 
682dbf727deSMatan Barak struct find_gid_index_context {
683dbf727deSMatan Barak 	u16 vlan_id;
684c865f246SSomnath Kotur 	enum ib_gid_type gid_type;
685dbf727deSMatan Barak };
686dbf727deSMatan Barak 
687dbf727deSMatan Barak static bool find_gid_index(const union ib_gid *gid,
688dbf727deSMatan Barak 			   const struct ib_gid_attr *gid_attr,
689dbf727deSMatan Barak 			   void *context)
690dbf727deSMatan Barak {
691b0dd0d33SParav Pandit 	struct find_gid_index_context *ctx = context;
692777a8b32SParav Pandit 	u16 vlan_id = 0xffff;
693777a8b32SParav Pandit 	int ret;
694dbf727deSMatan Barak 
695c865f246SSomnath Kotur 	if (ctx->gid_type != gid_attr->gid_type)
696c865f246SSomnath Kotur 		return false;
697c865f246SSomnath Kotur 
698777a8b32SParav Pandit 	ret = rdma_read_gid_l2_fields(gid_attr, &vlan_id, NULL);
699777a8b32SParav Pandit 	if (ret)
700dbf727deSMatan Barak 		return false;
701dbf727deSMatan Barak 
702777a8b32SParav Pandit 	return ctx->vlan_id == vlan_id;
703dbf727deSMatan Barak }
704dbf727deSMatan Barak 
705b7403217SParav Pandit static const struct ib_gid_attr *
7061fb7f897SMark Bloch get_sgid_attr_from_eth(struct ib_device *device, u32 port_num,
707dbf727deSMatan Barak 		       u16 vlan_id, const union ib_gid *sgid,
708b7403217SParav Pandit 		       enum ib_gid_type gid_type)
709dbf727deSMatan Barak {
710c865f246SSomnath Kotur 	struct find_gid_index_context context = {.vlan_id = vlan_id,
711c865f246SSomnath Kotur 						 .gid_type = gid_type};
712dbf727deSMatan Barak 
713b7403217SParav Pandit 	return rdma_find_gid_by_filter(device, sgid, port_num, find_gid_index,
714b7403217SParav Pandit 				       &context);
715dbf727deSMatan Barak }
716dbf727deSMatan Barak 
717850d8fd7SMoni Shoua int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
718c865f246SSomnath Kotur 			      enum rdma_network_type net_type,
719c865f246SSomnath Kotur 			      union ib_gid *sgid, union ib_gid *dgid)
720c865f246SSomnath Kotur {
721c865f246SSomnath Kotur 	struct sockaddr_in  src_in;
722c865f246SSomnath Kotur 	struct sockaddr_in  dst_in;
723c865f246SSomnath Kotur 	__be32 src_saddr, dst_saddr;
724c865f246SSomnath Kotur 
725c865f246SSomnath Kotur 	if (!sgid || !dgid)
726c865f246SSomnath Kotur 		return -EINVAL;
727c865f246SSomnath Kotur 
728c865f246SSomnath Kotur 	if (net_type == RDMA_NETWORK_IPV4) {
729c865f246SSomnath Kotur 		memcpy(&src_in.sin_addr.s_addr,
730c865f246SSomnath Kotur 		       &hdr->roce4grh.saddr, 4);
731c865f246SSomnath Kotur 		memcpy(&dst_in.sin_addr.s_addr,
732c865f246SSomnath Kotur 		       &hdr->roce4grh.daddr, 4);
733c865f246SSomnath Kotur 		src_saddr = src_in.sin_addr.s_addr;
734c865f246SSomnath Kotur 		dst_saddr = dst_in.sin_addr.s_addr;
735c865f246SSomnath Kotur 		ipv6_addr_set_v4mapped(src_saddr,
736c865f246SSomnath Kotur 				       (struct in6_addr *)sgid);
737c865f246SSomnath Kotur 		ipv6_addr_set_v4mapped(dst_saddr,
738c865f246SSomnath Kotur 				       (struct in6_addr *)dgid);
739c865f246SSomnath Kotur 		return 0;
740c865f246SSomnath Kotur 	} else if (net_type == RDMA_NETWORK_IPV6 ||
7411c15b4f2SAvihai Horon 		   net_type == RDMA_NETWORK_IB || RDMA_NETWORK_ROCE_V1) {
742c865f246SSomnath Kotur 		*dgid = hdr->ibgrh.dgid;
743c865f246SSomnath Kotur 		*sgid = hdr->ibgrh.sgid;
744c865f246SSomnath Kotur 		return 0;
745c865f246SSomnath Kotur 	} else {
746c865f246SSomnath Kotur 		return -EINVAL;
747c865f246SSomnath Kotur 	}
748c865f246SSomnath Kotur }
749850d8fd7SMoni Shoua EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
750c865f246SSomnath Kotur 
7511060f865SParav Pandit /* Resolve destination mac address and hop limit for unicast destination
7521060f865SParav Pandit  * GID entry, considering the source GID entry as well.
7531060f865SParav Pandit  * ah_attribute must have have valid port_num, sgid_index.
7541060f865SParav Pandit  */
7551060f865SParav Pandit static int ib_resolve_unicast_gid_dmac(struct ib_device *device,
7561060f865SParav Pandit 				       struct rdma_ah_attr *ah_attr)
7571060f865SParav Pandit {
758b7403217SParav Pandit 	struct ib_global_route *grh = rdma_ah_retrieve_grh(ah_attr);
759b7403217SParav Pandit 	const struct ib_gid_attr *sgid_attr = grh->sgid_attr;
7601060f865SParav Pandit 	int hop_limit = 0xff;
761b7403217SParav Pandit 	int ret = 0;
7621060f865SParav Pandit 
76356d0a7d9SParav Pandit 	/* If destination is link local and source GID is RoCEv1,
76456d0a7d9SParav Pandit 	 * IP stack is not used.
76556d0a7d9SParav Pandit 	 */
76656d0a7d9SParav Pandit 	if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) &&
767b7403217SParav Pandit 	    sgid_attr->gid_type == IB_GID_TYPE_ROCE) {
76856d0a7d9SParav Pandit 		rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
76956d0a7d9SParav Pandit 				ah_attr->roce.dmac);
770b7403217SParav Pandit 		return ret;
77156d0a7d9SParav Pandit 	}
77256d0a7d9SParav Pandit 
773b7403217SParav Pandit 	ret = rdma_addr_find_l2_eth_by_grh(&sgid_attr->gid, &grh->dgid,
7741060f865SParav Pandit 					   ah_attr->roce.dmac,
7750e9d2c19SParav Pandit 					   sgid_attr, &hop_limit);
7761060f865SParav Pandit 
7771060f865SParav Pandit 	grh->hop_limit = hop_limit;
7781060f865SParav Pandit 	return ret;
7791060f865SParav Pandit }
7801060f865SParav Pandit 
78128b5b3a2SGustavo A. R. Silva /*
782f6bdb142SParav Pandit  * This function initializes address handle attributes from the incoming packet.
78328b5b3a2SGustavo A. R. Silva  * Incoming packet has dgid of the receiver node on which this code is
78428b5b3a2SGustavo A. R. Silva  * getting executed and, sgid contains the GID of the sender.
78528b5b3a2SGustavo A. R. Silva  *
78628b5b3a2SGustavo A. R. Silva  * When resolving mac address of destination, the arrived dgid is used
78728b5b3a2SGustavo A. R. Silva  * as sgid and, sgid is used as dgid because sgid contains destinations
78828b5b3a2SGustavo A. R. Silva  * GID whom to respond to.
78928b5b3a2SGustavo A. R. Silva  *
790b7403217SParav Pandit  * On success the caller is responsible to call rdma_destroy_ah_attr on the
791b7403217SParav Pandit  * attr.
79228b5b3a2SGustavo A. R. Silva  */
7931fb7f897SMark Bloch int ib_init_ah_attr_from_wc(struct ib_device *device, u32 port_num,
79473cdaaeeSIra Weiny 			    const struct ib_wc *wc, const struct ib_grh *grh,
79590898850SDasaratharaman Chandramouli 			    struct rdma_ah_attr *ah_attr)
796513789edSHal Rosenstock {
797513789edSHal Rosenstock 	u32 flow_class;
798513789edSHal Rosenstock 	int ret;
799c865f246SSomnath Kotur 	enum rdma_network_type net_type = RDMA_NETWORK_IB;
800c865f246SSomnath Kotur 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
801b7403217SParav Pandit 	const struct ib_gid_attr *sgid_attr;
802c3efe750SMatan Barak 	int hoplimit = 0xff;
803c865f246SSomnath Kotur 	union ib_gid dgid;
804c865f246SSomnath Kotur 	union ib_gid sgid;
805513789edSHal Rosenstock 
80679364227SRoland Dreier 	might_sleep();
80779364227SRoland Dreier 
8084e00d694SSean Hefty 	memset(ah_attr, 0, sizeof *ah_attr);
80944c58487SDasaratharaman Chandramouli 	ah_attr->type = rdma_ah_find_type(device, port_num);
810227128fcSMichael Wang 	if (rdma_cap_eth_ah(device, port_num)) {
811c865f246SSomnath Kotur 		if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
812c865f246SSomnath Kotur 			net_type = wc->network_hdr_type;
813c865f246SSomnath Kotur 		else
814c865f246SSomnath Kotur 			net_type = ib_get_net_type_by_grh(device, port_num, grh);
815c865f246SSomnath Kotur 		gid_type = ib_network_to_gid_type(net_type);
816c865f246SSomnath Kotur 	}
817850d8fd7SMoni Shoua 	ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
818c865f246SSomnath Kotur 					&sgid, &dgid);
819c865f246SSomnath Kotur 	if (ret)
820c865f246SSomnath Kotur 		return ret;
821c865f246SSomnath Kotur 
8221060f865SParav Pandit 	rdma_ah_set_sl(ah_attr, wc->sl);
8231060f865SParav Pandit 	rdma_ah_set_port_num(ah_attr, port_num);
8241060f865SParav Pandit 
825c865f246SSomnath Kotur 	if (rdma_protocol_roce(device, port_num)) {
826dbf727deSMatan Barak 		u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
827dbf727deSMatan Barak 				wc->vlan_id : 0xffff;
828dbf727deSMatan Barak 
829dd5f03beSMatan Barak 		if (!(wc->wc_flags & IB_WC_GRH))
830dd5f03beSMatan Barak 			return -EPROTOTYPE;
831dd5f03beSMatan Barak 
832b7403217SParav Pandit 		sgid_attr = get_sgid_attr_from_eth(device, port_num,
8331060f865SParav Pandit 						   vlan_id, &dgid,
834b7403217SParav Pandit 						   gid_type);
835b7403217SParav Pandit 		if (IS_ERR(sgid_attr))
836b7403217SParav Pandit 			return PTR_ERR(sgid_attr);
83720029832SMatan Barak 
8381060f865SParav Pandit 		flow_class = be32_to_cpu(grh->version_tclass_flow);
839b7403217SParav Pandit 		rdma_move_grh_sgid_attr(ah_attr,
840b7403217SParav Pandit 					&sgid,
8411060f865SParav Pandit 					flow_class & 0xFFFFF,
842b7403217SParav Pandit 					hoplimit,
843b7403217SParav Pandit 					(flow_class >> 20) & 0xFF,
844b7403217SParav Pandit 					sgid_attr);
845b7403217SParav Pandit 
846b7403217SParav Pandit 		ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
847b7403217SParav Pandit 		if (ret)
848b7403217SParav Pandit 			rdma_destroy_ah_attr(ah_attr);
849b7403217SParav Pandit 
850b7403217SParav Pandit 		return ret;
8511060f865SParav Pandit 	} else {
852d8966fcdSDasaratharaman Chandramouli 		rdma_ah_set_dlid(ah_attr, wc->slid);
853d8966fcdSDasaratharaman Chandramouli 		rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
854513789edSHal Rosenstock 
855b7403217SParav Pandit 		if ((wc->wc_flags & IB_WC_GRH) == 0)
856b7403217SParav Pandit 			return 0;
857513789edSHal Rosenstock 
858b7403217SParav Pandit 		if (dgid.global.interface_id !=
859b7403217SParav Pandit 					cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
860b7403217SParav Pandit 			sgid_attr = rdma_find_gid_by_port(
861b7403217SParav Pandit 				device, &dgid, IB_GID_TYPE_IB, port_num, NULL);
862b7403217SParav Pandit 		} else
863b7403217SParav Pandit 			sgid_attr = rdma_get_gid_attr(device, port_num, 0);
864b7403217SParav Pandit 
865b7403217SParav Pandit 		if (IS_ERR(sgid_attr))
866b7403217SParav Pandit 			return PTR_ERR(sgid_attr);
867497677abSHal Rosenstock 		flow_class = be32_to_cpu(grh->version_tclass_flow);
868b7403217SParav Pandit 		rdma_move_grh_sgid_attr(ah_attr,
869b7403217SParav Pandit 					&sgid,
870d8966fcdSDasaratharaman Chandramouli 					flow_class & 0xFFFFF,
871b7403217SParav Pandit 					hoplimit,
872b7403217SParav Pandit 					(flow_class >> 20) & 0xFF,
873b7403217SParav Pandit 					sgid_attr);
874b7403217SParav Pandit 
8754e00d694SSean Hefty 		return 0;
8764e00d694SSean Hefty 	}
8771060f865SParav Pandit }
878f6bdb142SParav Pandit EXPORT_SYMBOL(ib_init_ah_attr_from_wc);
8794e00d694SSean Hefty 
8808d9ec9adSJason Gunthorpe /**
8818d9ec9adSJason Gunthorpe  * rdma_move_grh_sgid_attr - Sets the sgid attribute of GRH, taking ownership
8828d9ec9adSJason Gunthorpe  * of the reference
8838d9ec9adSJason Gunthorpe  *
8848d9ec9adSJason Gunthorpe  * @attr:	Pointer to AH attribute structure
8858d9ec9adSJason Gunthorpe  * @dgid:	Destination GID
8868d9ec9adSJason Gunthorpe  * @flow_label:	Flow label
8878d9ec9adSJason Gunthorpe  * @hop_limit:	Hop limit
8888d9ec9adSJason Gunthorpe  * @traffic_class: traffic class
8898d9ec9adSJason Gunthorpe  * @sgid_attr:	Pointer to SGID attribute
8908d9ec9adSJason Gunthorpe  *
8918d9ec9adSJason Gunthorpe  * This takes ownership of the sgid_attr reference. The caller must ensure
8928d9ec9adSJason Gunthorpe  * rdma_destroy_ah_attr() is called before destroying the rdma_ah_attr after
8938d9ec9adSJason Gunthorpe  * calling this function.
8948d9ec9adSJason Gunthorpe  */
8958d9ec9adSJason Gunthorpe void rdma_move_grh_sgid_attr(struct rdma_ah_attr *attr, union ib_gid *dgid,
8968d9ec9adSJason Gunthorpe 			     u32 flow_label, u8 hop_limit, u8 traffic_class,
8978d9ec9adSJason Gunthorpe 			     const struct ib_gid_attr *sgid_attr)
8988d9ec9adSJason Gunthorpe {
8998d9ec9adSJason Gunthorpe 	rdma_ah_set_grh(attr, dgid, flow_label, sgid_attr->index, hop_limit,
9008d9ec9adSJason Gunthorpe 			traffic_class);
9018d9ec9adSJason Gunthorpe 	attr->grh.sgid_attr = sgid_attr;
9028d9ec9adSJason Gunthorpe }
9038d9ec9adSJason Gunthorpe EXPORT_SYMBOL(rdma_move_grh_sgid_attr);
9048d9ec9adSJason Gunthorpe 
9058d9ec9adSJason Gunthorpe /**
9068d9ec9adSJason Gunthorpe  * rdma_destroy_ah_attr - Release reference to SGID attribute of
9078d9ec9adSJason Gunthorpe  * ah attribute.
9088d9ec9adSJason Gunthorpe  * @ah_attr: Pointer to ah attribute
9098d9ec9adSJason Gunthorpe  *
9108d9ec9adSJason Gunthorpe  * Release reference to the SGID attribute of the ah attribute if it is
9118d9ec9adSJason Gunthorpe  * non NULL. It is safe to call this multiple times, and safe to call it on
9128d9ec9adSJason Gunthorpe  * a zero initialized ah_attr.
9138d9ec9adSJason Gunthorpe  */
9148d9ec9adSJason Gunthorpe void rdma_destroy_ah_attr(struct rdma_ah_attr *ah_attr)
9158d9ec9adSJason Gunthorpe {
9168d9ec9adSJason Gunthorpe 	if (ah_attr->grh.sgid_attr) {
9178d9ec9adSJason Gunthorpe 		rdma_put_gid_attr(ah_attr->grh.sgid_attr);
9188d9ec9adSJason Gunthorpe 		ah_attr->grh.sgid_attr = NULL;
9198d9ec9adSJason Gunthorpe 	}
9208d9ec9adSJason Gunthorpe }
9218d9ec9adSJason Gunthorpe EXPORT_SYMBOL(rdma_destroy_ah_attr);
9228d9ec9adSJason Gunthorpe 
92373cdaaeeSIra Weiny struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
9241fb7f897SMark Bloch 				   const struct ib_grh *grh, u32 port_num)
9254e00d694SSean Hefty {
92690898850SDasaratharaman Chandramouli 	struct rdma_ah_attr ah_attr;
927b7403217SParav Pandit 	struct ib_ah *ah;
9284e00d694SSean Hefty 	int ret;
9294e00d694SSean Hefty 
930f6bdb142SParav Pandit 	ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr);
9314e00d694SSean Hefty 	if (ret)
9324e00d694SSean Hefty 		return ERR_PTR(ret);
933513789edSHal Rosenstock 
934b090c4e3SGal Pressman 	ah = rdma_create_ah(pd, &ah_attr, RDMA_CREATE_AH_SLEEPABLE);
935b7403217SParav Pandit 
936b7403217SParav Pandit 	rdma_destroy_ah_attr(&ah_attr);
937b7403217SParav Pandit 	return ah;
938513789edSHal Rosenstock }
939513789edSHal Rosenstock EXPORT_SYMBOL(ib_create_ah_from_wc);
940513789edSHal Rosenstock 
94167b985b6SDasaratharaman Chandramouli int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
9421da177e4SLinus Torvalds {
9438d9ec9adSJason Gunthorpe 	const struct ib_gid_attr *old_sgid_attr;
9448d9ec9adSJason Gunthorpe 	int ret;
9458d9ec9adSJason Gunthorpe 
94644c58487SDasaratharaman Chandramouli 	if (ah->type != ah_attr->type)
94744c58487SDasaratharaman Chandramouli 		return -EINVAL;
94844c58487SDasaratharaman Chandramouli 
9498d9ec9adSJason Gunthorpe 	ret = rdma_fill_sgid_attr(ah->device, ah_attr, &old_sgid_attr);
9508d9ec9adSJason Gunthorpe 	if (ret)
9518d9ec9adSJason Gunthorpe 		return ret;
9528d9ec9adSJason Gunthorpe 
9533023a1e9SKamal Heib 	ret = ah->device->ops.modify_ah ?
9543023a1e9SKamal Heib 		ah->device->ops.modify_ah(ah, ah_attr) :
95587915bf8SLeon Romanovsky 		-EOPNOTSUPP;
9568d9ec9adSJason Gunthorpe 
9571a1f460fSJason Gunthorpe 	ah->sgid_attr = rdma_update_sgid_attr(ah_attr, ah->sgid_attr);
9588d9ec9adSJason Gunthorpe 	rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
9598d9ec9adSJason Gunthorpe 	return ret;
9601da177e4SLinus Torvalds }
96167b985b6SDasaratharaman Chandramouli EXPORT_SYMBOL(rdma_modify_ah);
9621da177e4SLinus Torvalds 
963bfbfd661SDasaratharaman Chandramouli int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
9641da177e4SLinus Torvalds {
9658d9ec9adSJason Gunthorpe 	ah_attr->grh.sgid_attr = NULL;
9668d9ec9adSJason Gunthorpe 
9673023a1e9SKamal Heib 	return ah->device->ops.query_ah ?
9683023a1e9SKamal Heib 		ah->device->ops.query_ah(ah, ah_attr) :
96987915bf8SLeon Romanovsky 		-EOPNOTSUPP;
9701da177e4SLinus Torvalds }
971bfbfd661SDasaratharaman Chandramouli EXPORT_SYMBOL(rdma_query_ah);
9721da177e4SLinus Torvalds 
973c4367a26SShamir Rabinovitch int rdma_destroy_ah_user(struct ib_ah *ah, u32 flags, struct ib_udata *udata)
9741da177e4SLinus Torvalds {
9751a1f460fSJason Gunthorpe 	const struct ib_gid_attr *sgid_attr = ah->sgid_attr;
9761da177e4SLinus Torvalds 	struct ib_pd *pd;
9779a9ebf8cSLeon Romanovsky 	int ret;
9781da177e4SLinus Torvalds 
9792553ba21SGal Pressman 	might_sleep_if(flags & RDMA_DESTROY_AH_SLEEPABLE);
9802553ba21SGal Pressman 
9811da177e4SLinus Torvalds 	pd = ah->pd;
982d3456914SLeon Romanovsky 
9839a9ebf8cSLeon Romanovsky 	ret = ah->device->ops.destroy_ah(ah, flags);
9849a9ebf8cSLeon Romanovsky 	if (ret)
9859a9ebf8cSLeon Romanovsky 		return ret;
9869a9ebf8cSLeon Romanovsky 
9871da177e4SLinus Torvalds 	atomic_dec(&pd->usecnt);
9881a1f460fSJason Gunthorpe 	if (sgid_attr)
9891a1f460fSJason Gunthorpe 		rdma_put_gid_attr(sgid_attr);
9901da177e4SLinus Torvalds 
991d3456914SLeon Romanovsky 	kfree(ah);
9929a9ebf8cSLeon Romanovsky 	return ret;
9931da177e4SLinus Torvalds }
994c4367a26SShamir Rabinovitch EXPORT_SYMBOL(rdma_destroy_ah_user);
9951da177e4SLinus Torvalds 
996d41fcc67SRoland Dreier /* Shared receive queues */
997d41fcc67SRoland Dreier 
998b0810b03SJason Gunthorpe /**
999b0810b03SJason Gunthorpe  * ib_create_srq_user - Creates a SRQ associated with the specified protection
1000b0810b03SJason Gunthorpe  *   domain.
1001b0810b03SJason Gunthorpe  * @pd: The protection domain associated with the SRQ.
1002b0810b03SJason Gunthorpe  * @srq_init_attr: A list of initial attributes required to create the
1003b0810b03SJason Gunthorpe  *   SRQ.  If SRQ creation succeeds, then the attributes are updated to
1004b0810b03SJason Gunthorpe  *   the actual capabilities of the created SRQ.
100511708142SColton Lewis  * @uobject: uobject pointer if this is not a kernel SRQ
100611708142SColton Lewis  * @udata: udata pointer if this is not a kernel SRQ
1007b0810b03SJason Gunthorpe  *
1008b0810b03SJason Gunthorpe  * srq_attr->max_wr and srq_attr->max_sge are read the determine the
1009b0810b03SJason Gunthorpe  * requested size of the SRQ, and set to the actual values allocated
1010b0810b03SJason Gunthorpe  * on return.  If ib_create_srq() succeeds, then max_wr and max_sge
1011b0810b03SJason Gunthorpe  * will always be at least as large as the requested values.
1012b0810b03SJason Gunthorpe  */
1013b0810b03SJason Gunthorpe struct ib_srq *ib_create_srq_user(struct ib_pd *pd,
1014b0810b03SJason Gunthorpe 				  struct ib_srq_init_attr *srq_init_attr,
1015b0810b03SJason Gunthorpe 				  struct ib_usrq_object *uobject,
1016b0810b03SJason Gunthorpe 				  struct ib_udata *udata)
1017d41fcc67SRoland Dreier {
1018d41fcc67SRoland Dreier 	struct ib_srq *srq;
101968e326deSLeon Romanovsky 	int ret;
1020d41fcc67SRoland Dreier 
102168e326deSLeon Romanovsky 	srq = rdma_zalloc_drv_obj(pd->device, ib_srq);
102268e326deSLeon Romanovsky 	if (!srq)
102368e326deSLeon Romanovsky 		return ERR_PTR(-ENOMEM);
1024d41fcc67SRoland Dreier 
1025d41fcc67SRoland Dreier 	srq->device = pd->device;
1026d41fcc67SRoland Dreier 	srq->pd = pd;
1027d41fcc67SRoland Dreier 	srq->event_handler = srq_init_attr->event_handler;
1028d41fcc67SRoland Dreier 	srq->srq_context = srq_init_attr->srq_context;
102996104edaSSean Hefty 	srq->srq_type = srq_init_attr->srq_type;
1030b0810b03SJason Gunthorpe 	srq->uobject = uobject;
103168e326deSLeon Romanovsky 
10321a56ff6dSArtemy Kovalyov 	if (ib_srq_has_cq(srq->srq_type)) {
10331a56ff6dSArtemy Kovalyov 		srq->ext.cq = srq_init_attr->ext.cq;
10341a56ff6dSArtemy Kovalyov 		atomic_inc(&srq->ext.cq->usecnt);
10351a56ff6dSArtemy Kovalyov 	}
1036418d5130SSean Hefty 	if (srq->srq_type == IB_SRQT_XRC) {
1037418d5130SSean Hefty 		srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
1038418d5130SSean Hefty 		atomic_inc(&srq->ext.xrc.xrcd->usecnt);
1039418d5130SSean Hefty 	}
1040d41fcc67SRoland Dreier 	atomic_inc(&pd->usecnt);
104168e326deSLeon Romanovsky 
104248f8a70eSNeta Ostrovsky 	rdma_restrack_new(&srq->res, RDMA_RESTRACK_SRQ);
104348f8a70eSNeta Ostrovsky 	rdma_restrack_parent_name(&srq->res, &pd->res);
104448f8a70eSNeta Ostrovsky 
1045b0810b03SJason Gunthorpe 	ret = pd->device->ops.create_srq(srq, srq_init_attr, udata);
104668e326deSLeon Romanovsky 	if (ret) {
104748f8a70eSNeta Ostrovsky 		rdma_restrack_put(&srq->res);
104868e326deSLeon Romanovsky 		atomic_dec(&srq->pd->usecnt);
104968e326deSLeon Romanovsky 		if (srq->srq_type == IB_SRQT_XRC)
105068e326deSLeon Romanovsky 			atomic_dec(&srq->ext.xrc.xrcd->usecnt);
105168e326deSLeon Romanovsky 		if (ib_srq_has_cq(srq->srq_type))
105268e326deSLeon Romanovsky 			atomic_dec(&srq->ext.cq->usecnt);
105368e326deSLeon Romanovsky 		kfree(srq);
105468e326deSLeon Romanovsky 		return ERR_PTR(ret);
1055d41fcc67SRoland Dreier 	}
1056d41fcc67SRoland Dreier 
105748f8a70eSNeta Ostrovsky 	rdma_restrack_add(&srq->res);
105848f8a70eSNeta Ostrovsky 
1059d41fcc67SRoland Dreier 	return srq;
1060d41fcc67SRoland Dreier }
1061b0810b03SJason Gunthorpe EXPORT_SYMBOL(ib_create_srq_user);
1062d41fcc67SRoland Dreier 
1063d41fcc67SRoland Dreier int ib_modify_srq(struct ib_srq *srq,
1064d41fcc67SRoland Dreier 		  struct ib_srq_attr *srq_attr,
1065d41fcc67SRoland Dreier 		  enum ib_srq_attr_mask srq_attr_mask)
1066d41fcc67SRoland Dreier {
10673023a1e9SKamal Heib 	return srq->device->ops.modify_srq ?
10683023a1e9SKamal Heib 		srq->device->ops.modify_srq(srq, srq_attr, srq_attr_mask,
10693023a1e9SKamal Heib 					    NULL) : -EOPNOTSUPP;
1070d41fcc67SRoland Dreier }
1071d41fcc67SRoland Dreier EXPORT_SYMBOL(ib_modify_srq);
1072d41fcc67SRoland Dreier 
1073d41fcc67SRoland Dreier int ib_query_srq(struct ib_srq *srq,
1074d41fcc67SRoland Dreier 		 struct ib_srq_attr *srq_attr)
1075d41fcc67SRoland Dreier {
10763023a1e9SKamal Heib 	return srq->device->ops.query_srq ?
10773023a1e9SKamal Heib 		srq->device->ops.query_srq(srq, srq_attr) : -EOPNOTSUPP;
1078d41fcc67SRoland Dreier }
1079d41fcc67SRoland Dreier EXPORT_SYMBOL(ib_query_srq);
1080d41fcc67SRoland Dreier 
1081c4367a26SShamir Rabinovitch int ib_destroy_srq_user(struct ib_srq *srq, struct ib_udata *udata)
1082d41fcc67SRoland Dreier {
1083119181d1SLeon Romanovsky 	int ret;
1084119181d1SLeon Romanovsky 
1085d41fcc67SRoland Dreier 	if (atomic_read(&srq->usecnt))
1086d41fcc67SRoland Dreier 		return -EBUSY;
1087d41fcc67SRoland Dreier 
1088119181d1SLeon Romanovsky 	ret = srq->device->ops.destroy_srq(srq, udata);
1089119181d1SLeon Romanovsky 	if (ret)
1090119181d1SLeon Romanovsky 		return ret;
1091d41fcc67SRoland Dreier 
109268e326deSLeon Romanovsky 	atomic_dec(&srq->pd->usecnt);
109368e326deSLeon Romanovsky 	if (srq->srq_type == IB_SRQT_XRC)
109468e326deSLeon Romanovsky 		atomic_dec(&srq->ext.xrc.xrcd->usecnt);
109568e326deSLeon Romanovsky 	if (ib_srq_has_cq(srq->srq_type))
109668e326deSLeon Romanovsky 		atomic_dec(&srq->ext.cq->usecnt);
109748f8a70eSNeta Ostrovsky 	rdma_restrack_del(&srq->res);
109868e326deSLeon Romanovsky 	kfree(srq);
1099d41fcc67SRoland Dreier 
1100119181d1SLeon Romanovsky 	return ret;
1101d41fcc67SRoland Dreier }
1102c4367a26SShamir Rabinovitch EXPORT_SYMBOL(ib_destroy_srq_user);
1103d41fcc67SRoland Dreier 
11041da177e4SLinus Torvalds /* Queue pairs */
11051da177e4SLinus Torvalds 
11060e0ec7e0SSean Hefty static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
11070e0ec7e0SSean Hefty {
11080e0ec7e0SSean Hefty 	struct ib_qp *qp = context;
110973c40c61SYishai Hadas 	unsigned long flags;
11100e0ec7e0SSean Hefty 
111140adf686SParav Pandit 	spin_lock_irqsave(&qp->device->qp_open_list_lock, flags);
11120e0ec7e0SSean Hefty 	list_for_each_entry(event->element.qp, &qp->open_list, open_list)
1113eec9e29fSShlomo Pongratz 		if (event->element.qp->event_handler)
11140e0ec7e0SSean Hefty 			event->element.qp->event_handler(event, event->element.qp->qp_context);
111540adf686SParav Pandit 	spin_unlock_irqrestore(&qp->device->qp_open_list_lock, flags);
11160e0ec7e0SSean Hefty }
11170e0ec7e0SSean Hefty 
11180e0ec7e0SSean Hefty static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
11190e0ec7e0SSean Hefty 				  void (*event_handler)(struct ib_event *, void *),
11200e0ec7e0SSean Hefty 				  void *qp_context)
1121d3d72d90SSean Hefty {
11220e0ec7e0SSean Hefty 	struct ib_qp *qp;
11230e0ec7e0SSean Hefty 	unsigned long flags;
1124d291f1a6SDaniel Jurgens 	int err;
11250e0ec7e0SSean Hefty 
11260e0ec7e0SSean Hefty 	qp = kzalloc(sizeof *qp, GFP_KERNEL);
11270e0ec7e0SSean Hefty 	if (!qp)
11280e0ec7e0SSean Hefty 		return ERR_PTR(-ENOMEM);
11290e0ec7e0SSean Hefty 
11300e0ec7e0SSean Hefty 	qp->real_qp = real_qp;
1131d291f1a6SDaniel Jurgens 	err = ib_open_shared_qp_security(qp, real_qp->device);
1132d291f1a6SDaniel Jurgens 	if (err) {
1133d291f1a6SDaniel Jurgens 		kfree(qp);
1134d291f1a6SDaniel Jurgens 		return ERR_PTR(err);
1135d291f1a6SDaniel Jurgens 	}
1136d291f1a6SDaniel Jurgens 
1137d291f1a6SDaniel Jurgens 	qp->real_qp = real_qp;
11380e0ec7e0SSean Hefty 	atomic_inc(&real_qp->usecnt);
11390e0ec7e0SSean Hefty 	qp->device = real_qp->device;
11400e0ec7e0SSean Hefty 	qp->event_handler = event_handler;
11410e0ec7e0SSean Hefty 	qp->qp_context = qp_context;
11420e0ec7e0SSean Hefty 	qp->qp_num = real_qp->qp_num;
11430e0ec7e0SSean Hefty 	qp->qp_type = real_qp->qp_type;
11440e0ec7e0SSean Hefty 
114540adf686SParav Pandit 	spin_lock_irqsave(&real_qp->device->qp_open_list_lock, flags);
11460e0ec7e0SSean Hefty 	list_add(&qp->open_list, &real_qp->open_list);
114740adf686SParav Pandit 	spin_unlock_irqrestore(&real_qp->device->qp_open_list_lock, flags);
11480e0ec7e0SSean Hefty 
11490e0ec7e0SSean Hefty 	return qp;
1150d3d72d90SSean Hefty }
1151d3d72d90SSean Hefty 
11520e0ec7e0SSean Hefty struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
11530e0ec7e0SSean Hefty 			 struct ib_qp_open_attr *qp_open_attr)
11540e0ec7e0SSean Hefty {
11550e0ec7e0SSean Hefty 	struct ib_qp *qp, *real_qp;
11560e0ec7e0SSean Hefty 
11570e0ec7e0SSean Hefty 	if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
11580e0ec7e0SSean Hefty 		return ERR_PTR(-EINVAL);
11590e0ec7e0SSean Hefty 
11606f3ca6f4SMaor Gottlieb 	down_read(&xrcd->tgt_qps_rwsem);
11616f3ca6f4SMaor Gottlieb 	real_qp = xa_load(&xrcd->tgt_qps, qp_open_attr->qp_num);
11626f3ca6f4SMaor Gottlieb 	if (!real_qp) {
11636f3ca6f4SMaor Gottlieb 		up_read(&xrcd->tgt_qps_rwsem);
11646f3ca6f4SMaor Gottlieb 		return ERR_PTR(-EINVAL);
11656f3ca6f4SMaor Gottlieb 	}
11660e0ec7e0SSean Hefty 	qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
11670e0ec7e0SSean Hefty 			  qp_open_attr->qp_context);
11686f3ca6f4SMaor Gottlieb 	up_read(&xrcd->tgt_qps_rwsem);
11690e0ec7e0SSean Hefty 	return qp;
11700e0ec7e0SSean Hefty }
11710e0ec7e0SSean Hefty EXPORT_SYMBOL(ib_open_qp);
11720e0ec7e0SSean Hefty 
1173c4367a26SShamir Rabinovitch static struct ib_qp *create_xrc_qp_user(struct ib_qp *qp,
1174b72bfc96SJason Gunthorpe 					struct ib_qp_init_attr *qp_init_attr)
11751da177e4SLinus Torvalds {
117604c41bf3SChristoph Hellwig 	struct ib_qp *real_qp = qp;
11776f3ca6f4SMaor Gottlieb 	int err;
11781da177e4SLinus Torvalds 
11790e0ec7e0SSean Hefty 	qp->event_handler = __ib_shared_qp_event_handler;
11800e0ec7e0SSean Hefty 	qp->qp_context = qp;
1181b42b63cfSSean Hefty 	qp->pd = NULL;
1182b42b63cfSSean Hefty 	qp->send_cq = qp->recv_cq = NULL;
1183b42b63cfSSean Hefty 	qp->srq = NULL;
1184b42b63cfSSean Hefty 	qp->xrcd = qp_init_attr->xrcd;
1185b42b63cfSSean Hefty 	atomic_inc(&qp_init_attr->xrcd->usecnt);
11860e0ec7e0SSean Hefty 	INIT_LIST_HEAD(&qp->open_list);
11870e0ec7e0SSean Hefty 
11880e0ec7e0SSean Hefty 	qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
11890e0ec7e0SSean Hefty 			  qp_init_attr->qp_context);
1190535005caSYuval Avnery 	if (IS_ERR(qp))
1191535005caSYuval Avnery 		return qp;
1192535005caSYuval Avnery 
11936f3ca6f4SMaor Gottlieb 	err = xa_err(xa_store(&qp_init_attr->xrcd->tgt_qps, real_qp->qp_num,
11946f3ca6f4SMaor Gottlieb 			      real_qp, GFP_KERNEL));
11956f3ca6f4SMaor Gottlieb 	if (err) {
11966f3ca6f4SMaor Gottlieb 		ib_close_qp(qp);
11976f3ca6f4SMaor Gottlieb 		return ERR_PTR(err);
11986f3ca6f4SMaor Gottlieb 	}
119904c41bf3SChristoph Hellwig 	return qp;
120004c41bf3SChristoph Hellwig }
120104c41bf3SChristoph Hellwig 
1202b72bfc96SJason Gunthorpe /**
120366f57b87SLeon Romanovsky  * ib_create_named_qp - Creates a kernel QP associated with the specified protection
1204b72bfc96SJason Gunthorpe  *   domain.
1205b72bfc96SJason Gunthorpe  * @pd: The protection domain associated with the QP.
1206b72bfc96SJason Gunthorpe  * @qp_init_attr: A list of initial attributes required to create the
1207b72bfc96SJason Gunthorpe  *   QP.  If QP creation succeeds, then the attributes are updated to
1208b72bfc96SJason Gunthorpe  *   the actual capabilities of the created QP.
1209d1dec0caSLukas Bulwahn  * @caller: caller's build-time module name
1210b72bfc96SJason Gunthorpe  *
1211b72bfc96SJason Gunthorpe  * NOTE: for user qp use ib_create_qp_user with valid udata!
1212b72bfc96SJason Gunthorpe  */
121366f57b87SLeon Romanovsky struct ib_qp *ib_create_named_qp(struct ib_pd *pd,
121466f57b87SLeon Romanovsky 				 struct ib_qp_init_attr *qp_init_attr,
121566f57b87SLeon Romanovsky 				 const char *caller)
121604c41bf3SChristoph Hellwig {
121704c41bf3SChristoph Hellwig 	struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
121804c41bf3SChristoph Hellwig 	struct ib_qp *qp;
1219a060b562SChristoph Hellwig 	int ret;
1220a060b562SChristoph Hellwig 
1221a9017e23SYishai Hadas 	if (qp_init_attr->rwq_ind_tbl &&
1222a9017e23SYishai Hadas 	    (qp_init_attr->recv_cq ||
1223a9017e23SYishai Hadas 	    qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
1224a9017e23SYishai Hadas 	    qp_init_attr->cap.max_recv_sge))
1225a9017e23SYishai Hadas 		return ERR_PTR(-EINVAL);
1226a9017e23SYishai Hadas 
1227185eddc4SMax Gurtovoy 	if ((qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN) &&
1228185eddc4SMax Gurtovoy 	    !(device->attrs.device_cap_flags & IB_DEVICE_INTEGRITY_HANDOVER))
1229185eddc4SMax Gurtovoy 		return ERR_PTR(-EINVAL);
1230185eddc4SMax Gurtovoy 
1231a060b562SChristoph Hellwig 	/*
1232a060b562SChristoph Hellwig 	 * If the callers is using the RDMA API calculate the resources
1233a060b562SChristoph Hellwig 	 * needed for the RDMA READ/WRITE operations.
1234a060b562SChristoph Hellwig 	 *
1235a060b562SChristoph Hellwig 	 * Note that these callers need to pass in a port number.
1236a060b562SChristoph Hellwig 	 */
1237a060b562SChristoph Hellwig 	if (qp_init_attr->cap.max_rdma_ctxs)
1238a060b562SChristoph Hellwig 		rdma_rw_init_qp(device, qp_init_attr);
123904c41bf3SChristoph Hellwig 
124066f57b87SLeon Romanovsky 	qp = _ib_create_qp(device, pd, qp_init_attr, NULL, NULL, caller);
124104c41bf3SChristoph Hellwig 	if (IS_ERR(qp))
124204c41bf3SChristoph Hellwig 		return qp;
124304c41bf3SChristoph Hellwig 
1244d291f1a6SDaniel Jurgens 	ret = ib_create_qp_security(qp, device);
1245535005caSYuval Avnery 	if (ret)
1246535005caSYuval Avnery 		goto err;
1247d291f1a6SDaniel Jurgens 
1248535005caSYuval Avnery 	if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
1249c4367a26SShamir Rabinovitch 		struct ib_qp *xrc_qp =
1250b72bfc96SJason Gunthorpe 			create_xrc_qp_user(qp, qp_init_attr);
1251535005caSYuval Avnery 
1252535005caSYuval Avnery 		if (IS_ERR(xrc_qp)) {
1253535005caSYuval Avnery 			ret = PTR_ERR(xrc_qp);
1254535005caSYuval Avnery 			goto err;
1255535005caSYuval Avnery 		}
1256535005caSYuval Avnery 		return xrc_qp;
1257535005caSYuval Avnery 	}
125804c41bf3SChristoph Hellwig 
12591da177e4SLinus Torvalds 	qp->event_handler = qp_init_attr->event_handler;
12601da177e4SLinus Torvalds 	qp->qp_context = qp_init_attr->qp_context;
1261b42b63cfSSean Hefty 	if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
1262b42b63cfSSean Hefty 		qp->recv_cq = NULL;
1263b42b63cfSSean Hefty 		qp->srq = NULL;
1264b42b63cfSSean Hefty 	} else {
1265b42b63cfSSean Hefty 		qp->recv_cq = qp_init_attr->recv_cq;
1266a9017e23SYishai Hadas 		if (qp_init_attr->recv_cq)
1267b42b63cfSSean Hefty 			atomic_inc(&qp_init_attr->recv_cq->usecnt);
1268b42b63cfSSean Hefty 		qp->srq = qp_init_attr->srq;
1269b42b63cfSSean Hefty 		if (qp->srq)
1270b42b63cfSSean Hefty 			atomic_inc(&qp_init_attr->srq->usecnt);
1271b42b63cfSSean Hefty 	}
1272b42b63cfSSean Hefty 
12731da177e4SLinus Torvalds 	qp->send_cq = qp_init_attr->send_cq;
1274b42b63cfSSean Hefty 	qp->xrcd    = NULL;
1275b42b63cfSSean Hefty 
12761da177e4SLinus Torvalds 	atomic_inc(&pd->usecnt);
1277a9017e23SYishai Hadas 	if (qp_init_attr->send_cq)
12781da177e4SLinus Torvalds 		atomic_inc(&qp_init_attr->send_cq->usecnt);
1279a9017e23SYishai Hadas 	if (qp_init_attr->rwq_ind_tbl)
1280a9017e23SYishai Hadas 		atomic_inc(&qp->rwq_ind_tbl->usecnt);
1281a060b562SChristoph Hellwig 
1282a060b562SChristoph Hellwig 	if (qp_init_attr->cap.max_rdma_ctxs) {
1283a060b562SChristoph Hellwig 		ret = rdma_rw_init_mrs(qp, qp_init_attr);
1284535005caSYuval Avnery 		if (ret)
1285535005caSYuval Avnery 			goto err;
1286a060b562SChristoph Hellwig 	}
1287a060b562SChristoph Hellwig 
1288632bc3f6SBart Van Assche 	/*
1289632bc3f6SBart Van Assche 	 * Note: all hw drivers guarantee that max_send_sge is lower than
1290632bc3f6SBart Van Assche 	 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
1291632bc3f6SBart Van Assche 	 * max_send_sge <= max_sge_rd.
1292632bc3f6SBart Van Assche 	 */
1293632bc3f6SBart Van Assche 	qp->max_write_sge = qp_init_attr->cap.max_send_sge;
1294632bc3f6SBart Van Assche 	qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
1295632bc3f6SBart Van Assche 				 device->attrs.max_sge_rd);
1296185eddc4SMax Gurtovoy 	if (qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN)
1297185eddc4SMax Gurtovoy 		qp->integrity_en = true;
1298632bc3f6SBart Van Assche 
12991da177e4SLinus Torvalds 	return qp;
1300535005caSYuval Avnery 
1301535005caSYuval Avnery err:
1302535005caSYuval Avnery 	ib_destroy_qp(qp);
1303535005caSYuval Avnery 	return ERR_PTR(ret);
1304535005caSYuval Avnery 
13051da177e4SLinus Torvalds }
130666f57b87SLeon Romanovsky EXPORT_SYMBOL(ib_create_named_qp);
13071da177e4SLinus Torvalds 
13088a51866fSRoland Dreier static const struct {
13098a51866fSRoland Dreier 	int			valid;
1310b42b63cfSSean Hefty 	enum ib_qp_attr_mask	req_param[IB_QPT_MAX];
1311b42b63cfSSean Hefty 	enum ib_qp_attr_mask	opt_param[IB_QPT_MAX];
13128a51866fSRoland Dreier } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
13138a51866fSRoland Dreier 	[IB_QPS_RESET] = {
13148a51866fSRoland Dreier 		[IB_QPS_RESET] = { .valid = 1 },
13158a51866fSRoland Dreier 		[IB_QPS_INIT]  = {
13168a51866fSRoland Dreier 			.valid = 1,
13178a51866fSRoland Dreier 			.req_param = {
13188a51866fSRoland Dreier 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
13198a51866fSRoland Dreier 						IB_QP_PORT			|
13208a51866fSRoland Dreier 						IB_QP_QKEY),
1321c938a616SOr Gerlitz 				[IB_QPT_RAW_PACKET] = IB_QP_PORT,
13228a51866fSRoland Dreier 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
13238a51866fSRoland Dreier 						IB_QP_PORT			|
13248a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS),
13258a51866fSRoland Dreier 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
13268a51866fSRoland Dreier 						IB_QP_PORT			|
13278a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS),
1328b42b63cfSSean Hefty 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
1329b42b63cfSSean Hefty 						IB_QP_PORT			|
1330b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS),
1331b42b63cfSSean Hefty 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
1332b42b63cfSSean Hefty 						IB_QP_PORT			|
1333b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS),
13348a51866fSRoland Dreier 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
13358a51866fSRoland Dreier 						IB_QP_QKEY),
13368a51866fSRoland Dreier 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
13378a51866fSRoland Dreier 						IB_QP_QKEY),
13388a51866fSRoland Dreier 			}
13398a51866fSRoland Dreier 		},
13408a51866fSRoland Dreier 	},
13418a51866fSRoland Dreier 	[IB_QPS_INIT]  = {
13428a51866fSRoland Dreier 		[IB_QPS_RESET] = { .valid = 1 },
13438a51866fSRoland Dreier 		[IB_QPS_ERR] =   { .valid = 1 },
13448a51866fSRoland Dreier 		[IB_QPS_INIT]  = {
13458a51866fSRoland Dreier 			.valid = 1,
13468a51866fSRoland Dreier 			.opt_param = {
13478a51866fSRoland Dreier 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
13488a51866fSRoland Dreier 						IB_QP_PORT			|
13498a51866fSRoland Dreier 						IB_QP_QKEY),
13508a51866fSRoland Dreier 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
13518a51866fSRoland Dreier 						IB_QP_PORT			|
13528a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS),
13538a51866fSRoland Dreier 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
13548a51866fSRoland Dreier 						IB_QP_PORT			|
13558a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS),
1356b42b63cfSSean Hefty 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
1357b42b63cfSSean Hefty 						IB_QP_PORT			|
1358b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS),
1359b42b63cfSSean Hefty 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
1360b42b63cfSSean Hefty 						IB_QP_PORT			|
1361b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS),
13628a51866fSRoland Dreier 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
13638a51866fSRoland Dreier 						IB_QP_QKEY),
13648a51866fSRoland Dreier 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
13658a51866fSRoland Dreier 						IB_QP_QKEY),
13668a51866fSRoland Dreier 			}
13678a51866fSRoland Dreier 		},
13688a51866fSRoland Dreier 		[IB_QPS_RTR]   = {
13698a51866fSRoland Dreier 			.valid = 1,
13708a51866fSRoland Dreier 			.req_param = {
13718a51866fSRoland Dreier 				[IB_QPT_UC]  = (IB_QP_AV			|
13728a51866fSRoland Dreier 						IB_QP_PATH_MTU			|
13738a51866fSRoland Dreier 						IB_QP_DEST_QPN			|
13748a51866fSRoland Dreier 						IB_QP_RQ_PSN),
13758a51866fSRoland Dreier 				[IB_QPT_RC]  = (IB_QP_AV			|
13768a51866fSRoland Dreier 						IB_QP_PATH_MTU			|
13778a51866fSRoland Dreier 						IB_QP_DEST_QPN			|
13788a51866fSRoland Dreier 						IB_QP_RQ_PSN			|
13798a51866fSRoland Dreier 						IB_QP_MAX_DEST_RD_ATOMIC	|
13808a51866fSRoland Dreier 						IB_QP_MIN_RNR_TIMER),
1381b42b63cfSSean Hefty 				[IB_QPT_XRC_INI] = (IB_QP_AV			|
1382b42b63cfSSean Hefty 						IB_QP_PATH_MTU			|
1383b42b63cfSSean Hefty 						IB_QP_DEST_QPN			|
1384b42b63cfSSean Hefty 						IB_QP_RQ_PSN),
1385b42b63cfSSean Hefty 				[IB_QPT_XRC_TGT] = (IB_QP_AV			|
1386b42b63cfSSean Hefty 						IB_QP_PATH_MTU			|
1387b42b63cfSSean Hefty 						IB_QP_DEST_QPN			|
1388b42b63cfSSean Hefty 						IB_QP_RQ_PSN			|
1389b42b63cfSSean Hefty 						IB_QP_MAX_DEST_RD_ATOMIC	|
1390b42b63cfSSean Hefty 						IB_QP_MIN_RNR_TIMER),
13918a51866fSRoland Dreier 			},
13928a51866fSRoland Dreier 			.opt_param = {
13938a51866fSRoland Dreier 				 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
13948a51866fSRoland Dreier 						 IB_QP_QKEY),
13958a51866fSRoland Dreier 				 [IB_QPT_UC]  = (IB_QP_ALT_PATH			|
13968a51866fSRoland Dreier 						 IB_QP_ACCESS_FLAGS		|
13978a51866fSRoland Dreier 						 IB_QP_PKEY_INDEX),
13988a51866fSRoland Dreier 				 [IB_QPT_RC]  = (IB_QP_ALT_PATH			|
13998a51866fSRoland Dreier 						 IB_QP_ACCESS_FLAGS		|
14008a51866fSRoland Dreier 						 IB_QP_PKEY_INDEX),
1401b42b63cfSSean Hefty 				 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH		|
1402b42b63cfSSean Hefty 						 IB_QP_ACCESS_FLAGS		|
1403b42b63cfSSean Hefty 						 IB_QP_PKEY_INDEX),
1404b42b63cfSSean Hefty 				 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH		|
1405b42b63cfSSean Hefty 						 IB_QP_ACCESS_FLAGS		|
1406b42b63cfSSean Hefty 						 IB_QP_PKEY_INDEX),
14078a51866fSRoland Dreier 				 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
14088a51866fSRoland Dreier 						 IB_QP_QKEY),
14098a51866fSRoland Dreier 				 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
14108a51866fSRoland Dreier 						 IB_QP_QKEY),
1411dd5f03beSMatan Barak 			 },
1412dbf727deSMatan Barak 		},
14138a51866fSRoland Dreier 	},
14148a51866fSRoland Dreier 	[IB_QPS_RTR]   = {
14158a51866fSRoland Dreier 		[IB_QPS_RESET] = { .valid = 1 },
14168a51866fSRoland Dreier 		[IB_QPS_ERR] =   { .valid = 1 },
14178a51866fSRoland Dreier 		[IB_QPS_RTS]   = {
14188a51866fSRoland Dreier 			.valid = 1,
14198a51866fSRoland Dreier 			.req_param = {
14208a51866fSRoland Dreier 				[IB_QPT_UD]  = IB_QP_SQ_PSN,
14218a51866fSRoland Dreier 				[IB_QPT_UC]  = IB_QP_SQ_PSN,
14228a51866fSRoland Dreier 				[IB_QPT_RC]  = (IB_QP_TIMEOUT			|
14238a51866fSRoland Dreier 						IB_QP_RETRY_CNT			|
14248a51866fSRoland Dreier 						IB_QP_RNR_RETRY			|
14258a51866fSRoland Dreier 						IB_QP_SQ_PSN			|
14268a51866fSRoland Dreier 						IB_QP_MAX_QP_RD_ATOMIC),
1427b42b63cfSSean Hefty 				[IB_QPT_XRC_INI] = (IB_QP_TIMEOUT		|
1428b42b63cfSSean Hefty 						IB_QP_RETRY_CNT			|
1429b42b63cfSSean Hefty 						IB_QP_RNR_RETRY			|
1430b42b63cfSSean Hefty 						IB_QP_SQ_PSN			|
1431b42b63cfSSean Hefty 						IB_QP_MAX_QP_RD_ATOMIC),
1432b42b63cfSSean Hefty 				[IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT		|
1433b42b63cfSSean Hefty 						IB_QP_SQ_PSN),
14348a51866fSRoland Dreier 				[IB_QPT_SMI] = IB_QP_SQ_PSN,
14358a51866fSRoland Dreier 				[IB_QPT_GSI] = IB_QP_SQ_PSN,
14368a51866fSRoland Dreier 			},
14378a51866fSRoland Dreier 			.opt_param = {
14388a51866fSRoland Dreier 				 [IB_QPT_UD]  = (IB_QP_CUR_STATE		|
14398a51866fSRoland Dreier 						 IB_QP_QKEY),
14408a51866fSRoland Dreier 				 [IB_QPT_UC]  = (IB_QP_CUR_STATE		|
14418a51866fSRoland Dreier 						 IB_QP_ALT_PATH			|
14428a51866fSRoland Dreier 						 IB_QP_ACCESS_FLAGS		|
14438a51866fSRoland Dreier 						 IB_QP_PATH_MIG_STATE),
14448a51866fSRoland Dreier 				 [IB_QPT_RC]  = (IB_QP_CUR_STATE		|
14458a51866fSRoland Dreier 						 IB_QP_ALT_PATH			|
14468a51866fSRoland Dreier 						 IB_QP_ACCESS_FLAGS		|
14478a51866fSRoland Dreier 						 IB_QP_MIN_RNR_TIMER		|
14488a51866fSRoland Dreier 						 IB_QP_PATH_MIG_STATE),
1449b42b63cfSSean Hefty 				 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1450b42b63cfSSean Hefty 						 IB_QP_ALT_PATH			|
1451b42b63cfSSean Hefty 						 IB_QP_ACCESS_FLAGS		|
1452b42b63cfSSean Hefty 						 IB_QP_PATH_MIG_STATE),
1453b42b63cfSSean Hefty 				 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1454b42b63cfSSean Hefty 						 IB_QP_ALT_PATH			|
1455b42b63cfSSean Hefty 						 IB_QP_ACCESS_FLAGS		|
1456b42b63cfSSean Hefty 						 IB_QP_MIN_RNR_TIMER		|
1457b42b63cfSSean Hefty 						 IB_QP_PATH_MIG_STATE),
14588a51866fSRoland Dreier 				 [IB_QPT_SMI] = (IB_QP_CUR_STATE		|
14598a51866fSRoland Dreier 						 IB_QP_QKEY),
14608a51866fSRoland Dreier 				 [IB_QPT_GSI] = (IB_QP_CUR_STATE		|
14618a51866fSRoland Dreier 						 IB_QP_QKEY),
1462528e5a1bSBodong Wang 				 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
14638a51866fSRoland Dreier 			 }
14648a51866fSRoland Dreier 		}
14658a51866fSRoland Dreier 	},
14668a51866fSRoland Dreier 	[IB_QPS_RTS]   = {
14678a51866fSRoland Dreier 		[IB_QPS_RESET] = { .valid = 1 },
14688a51866fSRoland Dreier 		[IB_QPS_ERR] =   { .valid = 1 },
14698a51866fSRoland Dreier 		[IB_QPS_RTS]   = {
14708a51866fSRoland Dreier 			.valid = 1,
14718a51866fSRoland Dreier 			.opt_param = {
14728a51866fSRoland Dreier 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
14738a51866fSRoland Dreier 						IB_QP_QKEY),
14744546d31dSDotan Barak 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
14754546d31dSDotan Barak 						IB_QP_ACCESS_FLAGS		|
14768a51866fSRoland Dreier 						IB_QP_ALT_PATH			|
14778a51866fSRoland Dreier 						IB_QP_PATH_MIG_STATE),
14784546d31dSDotan Barak 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
14794546d31dSDotan Barak 						IB_QP_ACCESS_FLAGS		|
14808a51866fSRoland Dreier 						IB_QP_ALT_PATH			|
14818a51866fSRoland Dreier 						IB_QP_PATH_MIG_STATE		|
14828a51866fSRoland Dreier 						IB_QP_MIN_RNR_TIMER),
1483b42b63cfSSean Hefty 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1484b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS		|
1485b42b63cfSSean Hefty 						IB_QP_ALT_PATH			|
1486b42b63cfSSean Hefty 						IB_QP_PATH_MIG_STATE),
1487b42b63cfSSean Hefty 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1488b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS		|
1489b42b63cfSSean Hefty 						IB_QP_ALT_PATH			|
1490b42b63cfSSean Hefty 						IB_QP_PATH_MIG_STATE		|
1491b42b63cfSSean Hefty 						IB_QP_MIN_RNR_TIMER),
14928a51866fSRoland Dreier 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
14938a51866fSRoland Dreier 						IB_QP_QKEY),
14948a51866fSRoland Dreier 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
14958a51866fSRoland Dreier 						IB_QP_QKEY),
1496528e5a1bSBodong Wang 				[IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
14978a51866fSRoland Dreier 			}
14988a51866fSRoland Dreier 		},
14998a51866fSRoland Dreier 		[IB_QPS_SQD]   = {
15008a51866fSRoland Dreier 			.valid = 1,
15018a51866fSRoland Dreier 			.opt_param = {
15028a51866fSRoland Dreier 				[IB_QPT_UD]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
15038a51866fSRoland Dreier 				[IB_QPT_UC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
15048a51866fSRoland Dreier 				[IB_QPT_RC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1505b42b63cfSSean Hefty 				[IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1506b42b63cfSSean Hefty 				[IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
15078a51866fSRoland Dreier 				[IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
15088a51866fSRoland Dreier 				[IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
15098a51866fSRoland Dreier 			}
15108a51866fSRoland Dreier 		},
15118a51866fSRoland Dreier 	},
15128a51866fSRoland Dreier 	[IB_QPS_SQD]   = {
15138a51866fSRoland Dreier 		[IB_QPS_RESET] = { .valid = 1 },
15148a51866fSRoland Dreier 		[IB_QPS_ERR] =   { .valid = 1 },
15158a51866fSRoland Dreier 		[IB_QPS_RTS]   = {
15168a51866fSRoland Dreier 			.valid = 1,
15178a51866fSRoland Dreier 			.opt_param = {
15188a51866fSRoland Dreier 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
15198a51866fSRoland Dreier 						IB_QP_QKEY),
15208a51866fSRoland Dreier 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
15218a51866fSRoland Dreier 						IB_QP_ALT_PATH			|
15228a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS		|
15238a51866fSRoland Dreier 						IB_QP_PATH_MIG_STATE),
15248a51866fSRoland Dreier 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
15258a51866fSRoland Dreier 						IB_QP_ALT_PATH			|
15268a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS		|
15278a51866fSRoland Dreier 						IB_QP_MIN_RNR_TIMER		|
15288a51866fSRoland Dreier 						IB_QP_PATH_MIG_STATE),
1529b42b63cfSSean Hefty 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1530b42b63cfSSean Hefty 						IB_QP_ALT_PATH			|
1531b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS		|
1532b42b63cfSSean Hefty 						IB_QP_PATH_MIG_STATE),
1533b42b63cfSSean Hefty 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1534b42b63cfSSean Hefty 						IB_QP_ALT_PATH			|
1535b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS		|
1536b42b63cfSSean Hefty 						IB_QP_MIN_RNR_TIMER		|
1537b42b63cfSSean Hefty 						IB_QP_PATH_MIG_STATE),
15388a51866fSRoland Dreier 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
15398a51866fSRoland Dreier 						IB_QP_QKEY),
15408a51866fSRoland Dreier 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
15418a51866fSRoland Dreier 						IB_QP_QKEY),
15428a51866fSRoland Dreier 			}
15438a51866fSRoland Dreier 		},
15448a51866fSRoland Dreier 		[IB_QPS_SQD]   = {
15458a51866fSRoland Dreier 			.valid = 1,
15468a51866fSRoland Dreier 			.opt_param = {
15478a51866fSRoland Dreier 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
15488a51866fSRoland Dreier 						IB_QP_QKEY),
15498a51866fSRoland Dreier 				[IB_QPT_UC]  = (IB_QP_AV			|
15508a51866fSRoland Dreier 						IB_QP_ALT_PATH			|
15518a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS		|
15528a51866fSRoland Dreier 						IB_QP_PKEY_INDEX		|
15538a51866fSRoland Dreier 						IB_QP_PATH_MIG_STATE),
15548a51866fSRoland Dreier 				[IB_QPT_RC]  = (IB_QP_PORT			|
15558a51866fSRoland Dreier 						IB_QP_AV			|
15568a51866fSRoland Dreier 						IB_QP_TIMEOUT			|
15578a51866fSRoland Dreier 						IB_QP_RETRY_CNT			|
15588a51866fSRoland Dreier 						IB_QP_RNR_RETRY			|
15598a51866fSRoland Dreier 						IB_QP_MAX_QP_RD_ATOMIC		|
15608a51866fSRoland Dreier 						IB_QP_MAX_DEST_RD_ATOMIC	|
15618a51866fSRoland Dreier 						IB_QP_ALT_PATH			|
15628a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS		|
15638a51866fSRoland Dreier 						IB_QP_PKEY_INDEX		|
15648a51866fSRoland Dreier 						IB_QP_MIN_RNR_TIMER		|
15658a51866fSRoland Dreier 						IB_QP_PATH_MIG_STATE),
1566b42b63cfSSean Hefty 				[IB_QPT_XRC_INI] = (IB_QP_PORT			|
1567b42b63cfSSean Hefty 						IB_QP_AV			|
1568b42b63cfSSean Hefty 						IB_QP_TIMEOUT			|
1569b42b63cfSSean Hefty 						IB_QP_RETRY_CNT			|
1570b42b63cfSSean Hefty 						IB_QP_RNR_RETRY			|
1571b42b63cfSSean Hefty 						IB_QP_MAX_QP_RD_ATOMIC		|
1572b42b63cfSSean Hefty 						IB_QP_ALT_PATH			|
1573b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS		|
1574b42b63cfSSean Hefty 						IB_QP_PKEY_INDEX		|
1575b42b63cfSSean Hefty 						IB_QP_PATH_MIG_STATE),
1576b42b63cfSSean Hefty 				[IB_QPT_XRC_TGT] = (IB_QP_PORT			|
1577b42b63cfSSean Hefty 						IB_QP_AV			|
1578b42b63cfSSean Hefty 						IB_QP_TIMEOUT			|
1579b42b63cfSSean Hefty 						IB_QP_MAX_DEST_RD_ATOMIC	|
1580b42b63cfSSean Hefty 						IB_QP_ALT_PATH			|
1581b42b63cfSSean Hefty 						IB_QP_ACCESS_FLAGS		|
1582b42b63cfSSean Hefty 						IB_QP_PKEY_INDEX		|
1583b42b63cfSSean Hefty 						IB_QP_MIN_RNR_TIMER		|
1584b42b63cfSSean Hefty 						IB_QP_PATH_MIG_STATE),
15858a51866fSRoland Dreier 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
15868a51866fSRoland Dreier 						IB_QP_QKEY),
15878a51866fSRoland Dreier 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
15888a51866fSRoland Dreier 						IB_QP_QKEY),
15898a51866fSRoland Dreier 			}
15908a51866fSRoland Dreier 		}
15918a51866fSRoland Dreier 	},
15928a51866fSRoland Dreier 	[IB_QPS_SQE]   = {
15938a51866fSRoland Dreier 		[IB_QPS_RESET] = { .valid = 1 },
15948a51866fSRoland Dreier 		[IB_QPS_ERR] =   { .valid = 1 },
15958a51866fSRoland Dreier 		[IB_QPS_RTS]   = {
15968a51866fSRoland Dreier 			.valid = 1,
15978a51866fSRoland Dreier 			.opt_param = {
15988a51866fSRoland Dreier 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
15998a51866fSRoland Dreier 						IB_QP_QKEY),
16008a51866fSRoland Dreier 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
16018a51866fSRoland Dreier 						IB_QP_ACCESS_FLAGS),
16028a51866fSRoland Dreier 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
16038a51866fSRoland Dreier 						IB_QP_QKEY),
16048a51866fSRoland Dreier 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
16058a51866fSRoland Dreier 						IB_QP_QKEY),
16068a51866fSRoland Dreier 			}
16078a51866fSRoland Dreier 		}
16088a51866fSRoland Dreier 	},
16098a51866fSRoland Dreier 	[IB_QPS_ERR] = {
16108a51866fSRoland Dreier 		[IB_QPS_RESET] = { .valid = 1 },
16118a51866fSRoland Dreier 		[IB_QPS_ERR] =   { .valid = 1 }
16128a51866fSRoland Dreier 	}
16138a51866fSRoland Dreier };
16148a51866fSRoland Dreier 
161519b1f540SLeon Romanovsky bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1616d31131bbSKamal Heib 			enum ib_qp_type type, enum ib_qp_attr_mask mask)
16178a51866fSRoland Dreier {
16188a51866fSRoland Dreier 	enum ib_qp_attr_mask req_param, opt_param;
16198a51866fSRoland Dreier 
16208a51866fSRoland Dreier 	if (mask & IB_QP_CUR_STATE  &&
16218a51866fSRoland Dreier 	    cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
16228a51866fSRoland Dreier 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
162319b1f540SLeon Romanovsky 		return false;
16248a51866fSRoland Dreier 
16258a51866fSRoland Dreier 	if (!qp_state_table[cur_state][next_state].valid)
162619b1f540SLeon Romanovsky 		return false;
16278a51866fSRoland Dreier 
16288a51866fSRoland Dreier 	req_param = qp_state_table[cur_state][next_state].req_param[type];
16298a51866fSRoland Dreier 	opt_param = qp_state_table[cur_state][next_state].opt_param[type];
16308a51866fSRoland Dreier 
16318a51866fSRoland Dreier 	if ((mask & req_param) != req_param)
163219b1f540SLeon Romanovsky 		return false;
16338a51866fSRoland Dreier 
16348a51866fSRoland Dreier 	if (mask & ~(req_param | opt_param | IB_QP_STATE))
163519b1f540SLeon Romanovsky 		return false;
16368a51866fSRoland Dreier 
163719b1f540SLeon Romanovsky 	return true;
16388a51866fSRoland Dreier }
16398a51866fSRoland Dreier EXPORT_SYMBOL(ib_modify_qp_is_ok);
16408a51866fSRoland Dreier 
1641947c99ecSParav Pandit /**
1642947c99ecSParav Pandit  * ib_resolve_eth_dmac - Resolve destination mac address
1643947c99ecSParav Pandit  * @device:		Device to consider
1644947c99ecSParav Pandit  * @ah_attr:		address handle attribute which describes the
1645947c99ecSParav Pandit  *			source and destination parameters
1646947c99ecSParav Pandit  * ib_resolve_eth_dmac() resolves destination mac address and L3 hop limit It
1647947c99ecSParav Pandit  * returns 0 on success or appropriate error code. It initializes the
1648947c99ecSParav Pandit  * necessary ah_attr fields when call is successful.
1649947c99ecSParav Pandit  */
1650c0348eb0SParav Pandit static int ib_resolve_eth_dmac(struct ib_device *device,
165190898850SDasaratharaman Chandramouli 			       struct rdma_ah_attr *ah_attr)
1652ed4c54e5SOr Gerlitz {
1653ed4c54e5SOr Gerlitz 	int ret = 0;
1654d8966fcdSDasaratharaman Chandramouli 
16559636a56fSNoa Osherovich 	if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
16569636a56fSNoa Osherovich 		if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
16579636a56fSNoa Osherovich 			__be32 addr = 0;
16589636a56fSNoa Osherovich 
16599636a56fSNoa Osherovich 			memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
16609636a56fSNoa Osherovich 			ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
16619636a56fSNoa Osherovich 		} else {
16629636a56fSNoa Osherovich 			ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
16639636a56fSNoa Osherovich 					(char *)ah_attr->roce.dmac);
16649636a56fSNoa Osherovich 		}
1665ed4c54e5SOr Gerlitz 	} else {
16661060f865SParav Pandit 		ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
1667ed4c54e5SOr Gerlitz 	}
1668ed4c54e5SOr Gerlitz 	return ret;
1669ed4c54e5SOr Gerlitz }
1670ed4c54e5SOr Gerlitz 
16718d9ec9adSJason Gunthorpe static bool is_qp_type_connected(const struct ib_qp *qp)
16728d9ec9adSJason Gunthorpe {
16738d9ec9adSJason Gunthorpe 	return (qp->qp_type == IB_QPT_UC ||
16748d9ec9adSJason Gunthorpe 		qp->qp_type == IB_QPT_RC ||
16758d9ec9adSJason Gunthorpe 		qp->qp_type == IB_QPT_XRC_INI ||
16768d9ec9adSJason Gunthorpe 		qp->qp_type == IB_QPT_XRC_TGT);
16778d9ec9adSJason Gunthorpe }
16788d9ec9adSJason Gunthorpe 
16792988ca08SMauro Carvalho Chehab /*
1680b96ac05aSParav Pandit  * IB core internal function to perform QP attributes modification.
1681a512c2fbSParav Pandit  */
1682b96ac05aSParav Pandit static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
1683a512c2fbSParav Pandit 			 int attr_mask, struct ib_udata *udata)
1684a512c2fbSParav Pandit {
16851fb7f897SMark Bloch 	u32 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
16868d9ec9adSJason Gunthorpe 	const struct ib_gid_attr *old_sgid_attr_av;
16878d9ec9adSJason Gunthorpe 	const struct ib_gid_attr *old_sgid_attr_alt_av;
1688a512c2fbSParav Pandit 	int ret;
1689a512c2fbSParav Pandit 
169051aab126SMaor Gottlieb 	attr->xmit_slave = NULL;
16918d9ec9adSJason Gunthorpe 	if (attr_mask & IB_QP_AV) {
16928d9ec9adSJason Gunthorpe 		ret = rdma_fill_sgid_attr(qp->device, &attr->ah_attr,
16938d9ec9adSJason Gunthorpe 					  &old_sgid_attr_av);
16948d9ec9adSJason Gunthorpe 		if (ret)
16958d9ec9adSJason Gunthorpe 			return ret;
169651aab126SMaor Gottlieb 
169751aab126SMaor Gottlieb 		if (attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
169851aab126SMaor Gottlieb 		    is_qp_type_connected(qp)) {
169951aab126SMaor Gottlieb 			struct net_device *slave;
170051aab126SMaor Gottlieb 
170151aab126SMaor Gottlieb 			/*
170251aab126SMaor Gottlieb 			 * If the user provided the qp_attr then we have to
170351aab126SMaor Gottlieb 			 * resolve it. Kerne users have to provide already
170451aab126SMaor Gottlieb 			 * resolved rdma_ah_attr's.
170551aab126SMaor Gottlieb 			 */
170651aab126SMaor Gottlieb 			if (udata) {
170751aab126SMaor Gottlieb 				ret = ib_resolve_eth_dmac(qp->device,
170851aab126SMaor Gottlieb 							  &attr->ah_attr);
170951aab126SMaor Gottlieb 				if (ret)
171051aab126SMaor Gottlieb 					goto out_av;
171151aab126SMaor Gottlieb 			}
171251aab126SMaor Gottlieb 			slave = rdma_lag_get_ah_roce_slave(qp->device,
171351aab126SMaor Gottlieb 							   &attr->ah_attr,
171451aab126SMaor Gottlieb 							   GFP_KERNEL);
17155333499cSJing Xiangfeng 			if (IS_ERR(slave)) {
17165333499cSJing Xiangfeng 				ret = PTR_ERR(slave);
171751aab126SMaor Gottlieb 				goto out_av;
17185333499cSJing Xiangfeng 			}
171951aab126SMaor Gottlieb 			attr->xmit_slave = slave;
172051aab126SMaor Gottlieb 		}
17218d9ec9adSJason Gunthorpe 	}
17228d9ec9adSJason Gunthorpe 	if (attr_mask & IB_QP_ALT_PATH) {
17231a1f460fSJason Gunthorpe 		/*
17241a1f460fSJason Gunthorpe 		 * FIXME: This does not track the migration state, so if the
17251a1f460fSJason Gunthorpe 		 * user loads a new alternate path after the HW has migrated
17261a1f460fSJason Gunthorpe 		 * from primary->alternate we will keep the wrong
17271a1f460fSJason Gunthorpe 		 * references. This is OK for IB because the reference
17281a1f460fSJason Gunthorpe 		 * counting does not serve any functional purpose.
17291a1f460fSJason Gunthorpe 		 */
17308d9ec9adSJason Gunthorpe 		ret = rdma_fill_sgid_attr(qp->device, &attr->alt_ah_attr,
17318d9ec9adSJason Gunthorpe 					  &old_sgid_attr_alt_av);
17328d9ec9adSJason Gunthorpe 		if (ret)
17338d9ec9adSJason Gunthorpe 			goto out_av;
17347a5c938bSJason Gunthorpe 
17357a5c938bSJason Gunthorpe 		/*
17367a5c938bSJason Gunthorpe 		 * Today the core code can only handle alternate paths and APM
17377a5c938bSJason Gunthorpe 		 * for IB. Ban them in roce mode.
17387a5c938bSJason Gunthorpe 		 */
17397a5c938bSJason Gunthorpe 		if (!(rdma_protocol_ib(qp->device,
17407a5c938bSJason Gunthorpe 				       attr->alt_ah_attr.port_num) &&
17417a5c938bSJason Gunthorpe 		      rdma_protocol_ib(qp->device, port))) {
174247fda651SLi Heng 			ret = -EINVAL;
17437a5c938bSJason Gunthorpe 			goto out;
17447a5c938bSJason Gunthorpe 		}
17458d9ec9adSJason Gunthorpe 	}
17468d9ec9adSJason Gunthorpe 
1747727b7e9aSMajd Dibbiny 	if (rdma_ib_or_roce(qp->device, port)) {
1748727b7e9aSMajd Dibbiny 		if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
174943c7c851SJason Gunthorpe 			dev_warn(&qp->device->dev,
175043c7c851SJason Gunthorpe 				 "%s rq_psn overflow, masking to 24 bits\n",
175143c7c851SJason Gunthorpe 				 __func__);
1752727b7e9aSMajd Dibbiny 			attr->rq_psn &= 0xffffff;
1753727b7e9aSMajd Dibbiny 		}
1754727b7e9aSMajd Dibbiny 
1755727b7e9aSMajd Dibbiny 		if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
175643c7c851SJason Gunthorpe 			dev_warn(&qp->device->dev,
175743c7c851SJason Gunthorpe 				 " %s sq_psn overflow, masking to 24 bits\n",
175843c7c851SJason Gunthorpe 				 __func__);
1759727b7e9aSMajd Dibbiny 			attr->sq_psn &= 0xffffff;
1760727b7e9aSMajd Dibbiny 		}
1761727b7e9aSMajd Dibbiny 	}
1762727b7e9aSMajd Dibbiny 
176399fa331dSMark Zhang 	/*
176499fa331dSMark Zhang 	 * Bind this qp to a counter automatically based on the rdma counter
176599fa331dSMark Zhang 	 * rules. This only set in RST2INIT with port specified
176699fa331dSMark Zhang 	 */
176799fa331dSMark Zhang 	if (!qp->counter && (attr_mask & IB_QP_PORT) &&
176899fa331dSMark Zhang 	    ((attr_mask & IB_QP_STATE) && attr->qp_state == IB_QPS_INIT))
176999fa331dSMark Zhang 		rdma_counter_bind_qp_auto(qp, attr->port_num);
177099fa331dSMark Zhang 
1771498ca3c8SNoa Osherovich 	ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
17721a1f460fSJason Gunthorpe 	if (ret)
17731a1f460fSJason Gunthorpe 		goto out;
17741a1f460fSJason Gunthorpe 
17751a1f460fSJason Gunthorpe 	if (attr_mask & IB_QP_PORT)
1776498ca3c8SNoa Osherovich 		qp->port = attr->port_num;
17771a1f460fSJason Gunthorpe 	if (attr_mask & IB_QP_AV)
17781a1f460fSJason Gunthorpe 		qp->av_sgid_attr =
17791a1f460fSJason Gunthorpe 			rdma_update_sgid_attr(&attr->ah_attr, qp->av_sgid_attr);
17801a1f460fSJason Gunthorpe 	if (attr_mask & IB_QP_ALT_PATH)
17811a1f460fSJason Gunthorpe 		qp->alt_path_sgid_attr = rdma_update_sgid_attr(
17821a1f460fSJason Gunthorpe 			&attr->alt_ah_attr, qp->alt_path_sgid_attr);
1783498ca3c8SNoa Osherovich 
17848d9ec9adSJason Gunthorpe out:
17858d9ec9adSJason Gunthorpe 	if (attr_mask & IB_QP_ALT_PATH)
17868d9ec9adSJason Gunthorpe 		rdma_unfill_sgid_attr(&attr->alt_ah_attr, old_sgid_attr_alt_av);
17878d9ec9adSJason Gunthorpe out_av:
178851aab126SMaor Gottlieb 	if (attr_mask & IB_QP_AV) {
178951aab126SMaor Gottlieb 		rdma_lag_put_ah_roce_slave(attr->xmit_slave);
17908d9ec9adSJason Gunthorpe 		rdma_unfill_sgid_attr(&attr->ah_attr, old_sgid_attr_av);
179151aab126SMaor Gottlieb 	}
1792498ca3c8SNoa Osherovich 	return ret;
1793a512c2fbSParav Pandit }
1794b96ac05aSParav Pandit 
1795b96ac05aSParav Pandit /**
1796b96ac05aSParav Pandit  * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
1797b96ac05aSParav Pandit  * @ib_qp: The QP to modify.
1798b96ac05aSParav Pandit  * @attr: On input, specifies the QP attributes to modify.  On output,
1799b96ac05aSParav Pandit  *   the current values of selected QP attributes are returned.
1800b96ac05aSParav Pandit  * @attr_mask: A bit-mask used to specify which attributes of the QP
1801b96ac05aSParav Pandit  *   are being modified.
1802b96ac05aSParav Pandit  * @udata: pointer to user's input output buffer information
1803b96ac05aSParav Pandit  *   are being modified.
1804b96ac05aSParav Pandit  * It returns 0 on success and returns appropriate error code on error.
1805b96ac05aSParav Pandit  */
1806b96ac05aSParav Pandit int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
1807b96ac05aSParav Pandit 			    int attr_mask, struct ib_udata *udata)
1808b96ac05aSParav Pandit {
18098d9ec9adSJason Gunthorpe 	return _ib_modify_qp(ib_qp->real_qp, attr, attr_mask, udata);
1810b96ac05aSParav Pandit }
1811a512c2fbSParav Pandit EXPORT_SYMBOL(ib_modify_qp_with_udata);
1812a512c2fbSParav Pandit 
18131fb7f897SMark Bloch int ib_get_eth_speed(struct ib_device *dev, u32 port_num, u16 *speed, u8 *width)
1814d4186194SYuval Shaia {
1815d4186194SYuval Shaia 	int rc;
1816d4186194SYuval Shaia 	u32 netdev_speed;
1817d4186194SYuval Shaia 	struct net_device *netdev;
1818d4186194SYuval Shaia 	struct ethtool_link_ksettings lksettings;
1819d4186194SYuval Shaia 
1820d4186194SYuval Shaia 	if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
1821d4186194SYuval Shaia 		return -EINVAL;
1822d4186194SYuval Shaia 
1823c2261dd7SJason Gunthorpe 	netdev = ib_device_get_netdev(dev, port_num);
1824d4186194SYuval Shaia 	if (!netdev)
1825d4186194SYuval Shaia 		return -ENODEV;
1826d4186194SYuval Shaia 
1827d4186194SYuval Shaia 	rtnl_lock();
1828d4186194SYuval Shaia 	rc = __ethtool_get_link_ksettings(netdev, &lksettings);
1829d4186194SYuval Shaia 	rtnl_unlock();
1830d4186194SYuval Shaia 
1831d4186194SYuval Shaia 	dev_put(netdev);
1832d4186194SYuval Shaia 
183328b08657SKamal Heib 	if (!rc && lksettings.base.speed != (u32)SPEED_UNKNOWN) {
1834d4186194SYuval Shaia 		netdev_speed = lksettings.base.speed;
1835d4186194SYuval Shaia 	} else {
1836d4186194SYuval Shaia 		netdev_speed = SPEED_1000;
1837*3cea7b4aSWenpeng Liang 		pr_warn("%s speed is unknown, defaulting to %u\n", netdev->name,
1838d4186194SYuval Shaia 			netdev_speed);
1839d4186194SYuval Shaia 	}
1840d4186194SYuval Shaia 
1841d4186194SYuval Shaia 	if (netdev_speed <= SPEED_1000) {
1842d4186194SYuval Shaia 		*width = IB_WIDTH_1X;
1843d4186194SYuval Shaia 		*speed = IB_SPEED_SDR;
1844d4186194SYuval Shaia 	} else if (netdev_speed <= SPEED_10000) {
1845d4186194SYuval Shaia 		*width = IB_WIDTH_1X;
1846d4186194SYuval Shaia 		*speed = IB_SPEED_FDR10;
1847d4186194SYuval Shaia 	} else if (netdev_speed <= SPEED_20000) {
1848d4186194SYuval Shaia 		*width = IB_WIDTH_4X;
1849d4186194SYuval Shaia 		*speed = IB_SPEED_DDR;
1850d4186194SYuval Shaia 	} else if (netdev_speed <= SPEED_25000) {
1851d4186194SYuval Shaia 		*width = IB_WIDTH_1X;
1852d4186194SYuval Shaia 		*speed = IB_SPEED_EDR;
1853d4186194SYuval Shaia 	} else if (netdev_speed <= SPEED_40000) {
1854d4186194SYuval Shaia 		*width = IB_WIDTH_4X;
1855d4186194SYuval Shaia 		*speed = IB_SPEED_FDR10;
1856d4186194SYuval Shaia 	} else {
1857d4186194SYuval Shaia 		*width = IB_WIDTH_4X;
1858d4186194SYuval Shaia 		*speed = IB_SPEED_EDR;
1859d4186194SYuval Shaia 	}
1860d4186194SYuval Shaia 
1861d4186194SYuval Shaia 	return 0;
1862d4186194SYuval Shaia }
1863d4186194SYuval Shaia EXPORT_SYMBOL(ib_get_eth_speed);
1864d4186194SYuval Shaia 
18651da177e4SLinus Torvalds int ib_modify_qp(struct ib_qp *qp,
18661da177e4SLinus Torvalds 		 struct ib_qp_attr *qp_attr,
18671da177e4SLinus Torvalds 		 int qp_attr_mask)
18681da177e4SLinus Torvalds {
1869b96ac05aSParav Pandit 	return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
18701da177e4SLinus Torvalds }
18711da177e4SLinus Torvalds EXPORT_SYMBOL(ib_modify_qp);
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds int ib_query_qp(struct ib_qp *qp,
18741da177e4SLinus Torvalds 		struct ib_qp_attr *qp_attr,
18751da177e4SLinus Torvalds 		int qp_attr_mask,
18761da177e4SLinus Torvalds 		struct ib_qp_init_attr *qp_init_attr)
18771da177e4SLinus Torvalds {
18788d9ec9adSJason Gunthorpe 	qp_attr->ah_attr.grh.sgid_attr = NULL;
18798d9ec9adSJason Gunthorpe 	qp_attr->alt_ah_attr.grh.sgid_attr = NULL;
18808d9ec9adSJason Gunthorpe 
18813023a1e9SKamal Heib 	return qp->device->ops.query_qp ?
18823023a1e9SKamal Heib 		qp->device->ops.query_qp(qp->real_qp, qp_attr, qp_attr_mask,
18833023a1e9SKamal Heib 					 qp_init_attr) : -EOPNOTSUPP;
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds EXPORT_SYMBOL(ib_query_qp);
18861da177e4SLinus Torvalds 
18870e0ec7e0SSean Hefty int ib_close_qp(struct ib_qp *qp)
18880e0ec7e0SSean Hefty {
18890e0ec7e0SSean Hefty 	struct ib_qp *real_qp;
18900e0ec7e0SSean Hefty 	unsigned long flags;
18910e0ec7e0SSean Hefty 
18920e0ec7e0SSean Hefty 	real_qp = qp->real_qp;
18930e0ec7e0SSean Hefty 	if (real_qp == qp)
18940e0ec7e0SSean Hefty 		return -EINVAL;
18950e0ec7e0SSean Hefty 
189640adf686SParav Pandit 	spin_lock_irqsave(&real_qp->device->qp_open_list_lock, flags);
18970e0ec7e0SSean Hefty 	list_del(&qp->open_list);
189840adf686SParav Pandit 	spin_unlock_irqrestore(&real_qp->device->qp_open_list_lock, flags);
18990e0ec7e0SSean Hefty 
19000e0ec7e0SSean Hefty 	atomic_dec(&real_qp->usecnt);
19014a50881bSMoni Shoua 	if (qp->qp_sec)
1902d291f1a6SDaniel Jurgens 		ib_close_shared_qp_security(qp->qp_sec);
19030e0ec7e0SSean Hefty 	kfree(qp);
19040e0ec7e0SSean Hefty 
19050e0ec7e0SSean Hefty 	return 0;
19060e0ec7e0SSean Hefty }
19070e0ec7e0SSean Hefty EXPORT_SYMBOL(ib_close_qp);
19080e0ec7e0SSean Hefty 
19090e0ec7e0SSean Hefty static int __ib_destroy_shared_qp(struct ib_qp *qp)
19100e0ec7e0SSean Hefty {
19110e0ec7e0SSean Hefty 	struct ib_xrcd *xrcd;
19120e0ec7e0SSean Hefty 	struct ib_qp *real_qp;
19130e0ec7e0SSean Hefty 	int ret;
19140e0ec7e0SSean Hefty 
19150e0ec7e0SSean Hefty 	real_qp = qp->real_qp;
19160e0ec7e0SSean Hefty 	xrcd = real_qp->xrcd;
19176f3ca6f4SMaor Gottlieb 	down_write(&xrcd->tgt_qps_rwsem);
19180e0ec7e0SSean Hefty 	ib_close_qp(qp);
19190e0ec7e0SSean Hefty 	if (atomic_read(&real_qp->usecnt) == 0)
19206f3ca6f4SMaor Gottlieb 		xa_erase(&xrcd->tgt_qps, real_qp->qp_num);
19210e0ec7e0SSean Hefty 	else
19220e0ec7e0SSean Hefty 		real_qp = NULL;
19236f3ca6f4SMaor Gottlieb 	up_write(&xrcd->tgt_qps_rwsem);
19240e0ec7e0SSean Hefty 
19250e0ec7e0SSean Hefty 	if (real_qp) {
19260e0ec7e0SSean Hefty 		ret = ib_destroy_qp(real_qp);
19270e0ec7e0SSean Hefty 		if (!ret)
19280e0ec7e0SSean Hefty 			atomic_dec(&xrcd->usecnt);
19290e0ec7e0SSean Hefty 	}
19300e0ec7e0SSean Hefty 
19310e0ec7e0SSean Hefty 	return 0;
19320e0ec7e0SSean Hefty }
19330e0ec7e0SSean Hefty 
1934c4367a26SShamir Rabinovitch int ib_destroy_qp_user(struct ib_qp *qp, struct ib_udata *udata)
19351da177e4SLinus Torvalds {
19361a1f460fSJason Gunthorpe 	const struct ib_gid_attr *alt_path_sgid_attr = qp->alt_path_sgid_attr;
19371a1f460fSJason Gunthorpe 	const struct ib_gid_attr *av_sgid_attr = qp->av_sgid_attr;
19381da177e4SLinus Torvalds 	struct ib_pd *pd;
19391da177e4SLinus Torvalds 	struct ib_cq *scq, *rcq;
19401da177e4SLinus Torvalds 	struct ib_srq *srq;
1941a9017e23SYishai Hadas 	struct ib_rwq_ind_table *ind_tbl;
1942d291f1a6SDaniel Jurgens 	struct ib_qp_security *sec;
19431da177e4SLinus Torvalds 	int ret;
19441da177e4SLinus Torvalds 
1945fffb0383SChristoph Hellwig 	WARN_ON_ONCE(qp->mrs_used > 0);
1946fffb0383SChristoph Hellwig 
19470e0ec7e0SSean Hefty 	if (atomic_read(&qp->usecnt))
19480e0ec7e0SSean Hefty 		return -EBUSY;
19490e0ec7e0SSean Hefty 
19500e0ec7e0SSean Hefty 	if (qp->real_qp != qp)
19510e0ec7e0SSean Hefty 		return __ib_destroy_shared_qp(qp);
19520e0ec7e0SSean Hefty 
19531da177e4SLinus Torvalds 	pd   = qp->pd;
19541da177e4SLinus Torvalds 	scq  = qp->send_cq;
19551da177e4SLinus Torvalds 	rcq  = qp->recv_cq;
19561da177e4SLinus Torvalds 	srq  = qp->srq;
1957a9017e23SYishai Hadas 	ind_tbl = qp->rwq_ind_tbl;
1958d291f1a6SDaniel Jurgens 	sec  = qp->qp_sec;
1959d291f1a6SDaniel Jurgens 	if (sec)
1960d291f1a6SDaniel Jurgens 		ib_destroy_qp_security_begin(sec);
19611da177e4SLinus Torvalds 
1962a060b562SChristoph Hellwig 	if (!qp->uobject)
1963a060b562SChristoph Hellwig 		rdma_rw_cleanup_mrs(qp);
1964a060b562SChristoph Hellwig 
196599fa331dSMark Zhang 	rdma_counter_unbind_qp(qp, true);
196678a0cd64SLeon Romanovsky 	rdma_restrack_del(&qp->res);
1967c4367a26SShamir Rabinovitch 	ret = qp->device->ops.destroy_qp(qp, udata);
19681da177e4SLinus Torvalds 	if (!ret) {
19691a1f460fSJason Gunthorpe 		if (alt_path_sgid_attr)
19701a1f460fSJason Gunthorpe 			rdma_put_gid_attr(alt_path_sgid_attr);
19711a1f460fSJason Gunthorpe 		if (av_sgid_attr)
19721a1f460fSJason Gunthorpe 			rdma_put_gid_attr(av_sgid_attr);
1973b42b63cfSSean Hefty 		if (pd)
19741da177e4SLinus Torvalds 			atomic_dec(&pd->usecnt);
1975b42b63cfSSean Hefty 		if (scq)
19761da177e4SLinus Torvalds 			atomic_dec(&scq->usecnt);
1977b42b63cfSSean Hefty 		if (rcq)
19781da177e4SLinus Torvalds 			atomic_dec(&rcq->usecnt);
19791da177e4SLinus Torvalds 		if (srq)
19801da177e4SLinus Torvalds 			atomic_dec(&srq->usecnt);
1981a9017e23SYishai Hadas 		if (ind_tbl)
1982a9017e23SYishai Hadas 			atomic_dec(&ind_tbl->usecnt);
1983d291f1a6SDaniel Jurgens 		if (sec)
1984d291f1a6SDaniel Jurgens 			ib_destroy_qp_security_end(sec);
1985d291f1a6SDaniel Jurgens 	} else {
1986d291f1a6SDaniel Jurgens 		if (sec)
1987d291f1a6SDaniel Jurgens 			ib_destroy_qp_security_abort(sec);
19881da177e4SLinus Torvalds 	}
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	return ret;
19911da177e4SLinus Torvalds }
1992c4367a26SShamir Rabinovitch EXPORT_SYMBOL(ib_destroy_qp_user);
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds /* Completion queues */
19951da177e4SLinus Torvalds 
19967350cdd0SBharat Potnuri struct ib_cq *__ib_create_cq(struct ib_device *device,
19971da177e4SLinus Torvalds 			     ib_comp_handler comp_handler,
19981da177e4SLinus Torvalds 			     void (*event_handler)(struct ib_event *, void *),
19998e37210bSMatan Barak 			     void *cq_context,
20007350cdd0SBharat Potnuri 			     const struct ib_cq_init_attr *cq_attr,
20017350cdd0SBharat Potnuri 			     const char *caller)
20021da177e4SLinus Torvalds {
20031da177e4SLinus Torvalds 	struct ib_cq *cq;
2004e39afe3dSLeon Romanovsky 	int ret;
20051da177e4SLinus Torvalds 
2006e39afe3dSLeon Romanovsky 	cq = rdma_zalloc_drv_obj(device, ib_cq);
2007e39afe3dSLeon Romanovsky 	if (!cq)
2008e39afe3dSLeon Romanovsky 		return ERR_PTR(-ENOMEM);
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	cq->device = device;
2011b5e81bf5SRoland Dreier 	cq->uobject = NULL;
20121da177e4SLinus Torvalds 	cq->comp_handler = comp_handler;
20131da177e4SLinus Torvalds 	cq->event_handler = event_handler;
20141da177e4SLinus Torvalds 	cq->cq_context = cq_context;
20151da177e4SLinus Torvalds 	atomic_set(&cq->usecnt, 0);
201613ef5539SLeon Romanovsky 
201713ef5539SLeon Romanovsky 	rdma_restrack_new(&cq->res, RDMA_RESTRACK_CQ);
2018b09c4d70SLeon Romanovsky 	rdma_restrack_set_name(&cq->res, caller);
2019e39afe3dSLeon Romanovsky 
2020e39afe3dSLeon Romanovsky 	ret = device->ops.create_cq(cq, cq_attr, NULL);
2021e39afe3dSLeon Romanovsky 	if (ret) {
202213ef5539SLeon Romanovsky 		rdma_restrack_put(&cq->res);
2023e39afe3dSLeon Romanovsky 		kfree(cq);
2024e39afe3dSLeon Romanovsky 		return ERR_PTR(ret);
20251da177e4SLinus Torvalds 	}
20261da177e4SLinus Torvalds 
2027c34a23c2SLeon Romanovsky 	rdma_restrack_add(&cq->res);
20281da177e4SLinus Torvalds 	return cq;
20291da177e4SLinus Torvalds }
20307350cdd0SBharat Potnuri EXPORT_SYMBOL(__ib_create_cq);
20311da177e4SLinus Torvalds 
20324190b4e9SLeon Romanovsky int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
20332dd57162SEli Cohen {
20343446cbd2SYamin Friedman 	if (cq->shared)
20353446cbd2SYamin Friedman 		return -EOPNOTSUPP;
20363446cbd2SYamin Friedman 
20373023a1e9SKamal Heib 	return cq->device->ops.modify_cq ?
20383023a1e9SKamal Heib 		cq->device->ops.modify_cq(cq, cq_count,
20393023a1e9SKamal Heib 					  cq_period) : -EOPNOTSUPP;
20402dd57162SEli Cohen }
20414190b4e9SLeon Romanovsky EXPORT_SYMBOL(rdma_set_cq_moderation);
20422dd57162SEli Cohen 
2043c4367a26SShamir Rabinovitch int ib_destroy_cq_user(struct ib_cq *cq, struct ib_udata *udata)
20441da177e4SLinus Torvalds {
204543d781b9SLeon Romanovsky 	int ret;
204643d781b9SLeon Romanovsky 
20473446cbd2SYamin Friedman 	if (WARN_ON_ONCE(cq->shared))
20483446cbd2SYamin Friedman 		return -EOPNOTSUPP;
20493446cbd2SYamin Friedman 
20501da177e4SLinus Torvalds 	if (atomic_read(&cq->usecnt))
20511da177e4SLinus Torvalds 		return -EBUSY;
20521da177e4SLinus Torvalds 
205343d781b9SLeon Romanovsky 	ret = cq->device->ops.destroy_cq(cq, udata);
205443d781b9SLeon Romanovsky 	if (ret)
205543d781b9SLeon Romanovsky 		return ret;
205643d781b9SLeon Romanovsky 
205708f294a1SLeon Romanovsky 	rdma_restrack_del(&cq->res);
2058e39afe3dSLeon Romanovsky 	kfree(cq);
205943d781b9SLeon Romanovsky 	return ret;
20601da177e4SLinus Torvalds }
2061c4367a26SShamir Rabinovitch EXPORT_SYMBOL(ib_destroy_cq_user);
20621da177e4SLinus Torvalds 
2063a74cd4afSRoland Dreier int ib_resize_cq(struct ib_cq *cq, int cqe)
20641da177e4SLinus Torvalds {
20653446cbd2SYamin Friedman 	if (cq->shared)
20663446cbd2SYamin Friedman 		return -EOPNOTSUPP;
20673446cbd2SYamin Friedman 
20683023a1e9SKamal Heib 	return cq->device->ops.resize_cq ?
20693023a1e9SKamal Heib 		cq->device->ops.resize_cq(cq, cqe, NULL) : -EOPNOTSUPP;
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds EXPORT_SYMBOL(ib_resize_cq);
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds /* Memory regions */
20741da177e4SLinus Torvalds 
207533006bd4SMoni Shoua struct ib_mr *ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
207633006bd4SMoni Shoua 			     u64 virt_addr, int access_flags)
207733006bd4SMoni Shoua {
207833006bd4SMoni Shoua 	struct ib_mr *mr;
207933006bd4SMoni Shoua 
208033006bd4SMoni Shoua 	if (access_flags & IB_ACCESS_ON_DEMAND) {
208133006bd4SMoni Shoua 		if (!(pd->device->attrs.device_cap_flags &
208233006bd4SMoni Shoua 		      IB_DEVICE_ON_DEMAND_PAGING)) {
208333006bd4SMoni Shoua 			pr_debug("ODP support not available\n");
208433006bd4SMoni Shoua 			return ERR_PTR(-EINVAL);
208533006bd4SMoni Shoua 		}
208633006bd4SMoni Shoua 	}
208733006bd4SMoni Shoua 
208833006bd4SMoni Shoua 	mr = pd->device->ops.reg_user_mr(pd, start, length, virt_addr,
208933006bd4SMoni Shoua 					 access_flags, NULL);
209033006bd4SMoni Shoua 
209133006bd4SMoni Shoua 	if (IS_ERR(mr))
209233006bd4SMoni Shoua 		return mr;
209333006bd4SMoni Shoua 
209433006bd4SMoni Shoua 	mr->device = pd->device;
209533006bd4SMoni Shoua 	mr->pd = pd;
209633006bd4SMoni Shoua 	mr->dm = NULL;
209733006bd4SMoni Shoua 	atomic_inc(&pd->usecnt);
209813ef5539SLeon Romanovsky 
209913ef5539SLeon Romanovsky 	rdma_restrack_new(&mr->res, RDMA_RESTRACK_MR);
2100b09c4d70SLeon Romanovsky 	rdma_restrack_parent_name(&mr->res, &pd->res);
2101c34a23c2SLeon Romanovsky 	rdma_restrack_add(&mr->res);
210233006bd4SMoni Shoua 
210333006bd4SMoni Shoua 	return mr;
210433006bd4SMoni Shoua }
210533006bd4SMoni Shoua EXPORT_SYMBOL(ib_reg_user_mr);
210633006bd4SMoni Shoua 
210787d8069fSMoni Shoua int ib_advise_mr(struct ib_pd *pd, enum ib_uverbs_advise_mr_advice advice,
210887d8069fSMoni Shoua 		 u32 flags, struct ib_sge *sg_list, u32 num_sge)
210987d8069fSMoni Shoua {
211087d8069fSMoni Shoua 	if (!pd->device->ops.advise_mr)
211187d8069fSMoni Shoua 		return -EOPNOTSUPP;
211287d8069fSMoni Shoua 
2113d4d7f596SMaor Gottlieb 	if (!num_sge)
2114d4d7f596SMaor Gottlieb 		return 0;
2115d4d7f596SMaor Gottlieb 
211687d8069fSMoni Shoua 	return pd->device->ops.advise_mr(pd, advice, flags, sg_list, num_sge,
211787d8069fSMoni Shoua 					 NULL);
211887d8069fSMoni Shoua }
211987d8069fSMoni Shoua EXPORT_SYMBOL(ib_advise_mr);
212087d8069fSMoni Shoua 
2121c4367a26SShamir Rabinovitch int ib_dereg_mr_user(struct ib_mr *mr, struct ib_udata *udata)
21221da177e4SLinus Torvalds {
2123ab67ed8dSChristoph Hellwig 	struct ib_pd *pd = mr->pd;
2124be934ccaSAriel Levkovich 	struct ib_dm *dm = mr->dm;
21257c717d3aSMax Gurtovoy 	struct ib_sig_attrs *sig_attrs = mr->sig_attrs;
21261da177e4SLinus Torvalds 	int ret;
21271da177e4SLinus Torvalds 
2128622db5b6SChuck Lever 	trace_mr_dereg(mr);
2129fccec5b8SSteve Wise 	rdma_restrack_del(&mr->res);
2130c4367a26SShamir Rabinovitch 	ret = mr->device->ops.dereg_mr(mr, udata);
2131be934ccaSAriel Levkovich 	if (!ret) {
21321da177e4SLinus Torvalds 		atomic_dec(&pd->usecnt);
2133be934ccaSAriel Levkovich 		if (dm)
2134be934ccaSAriel Levkovich 			atomic_dec(&dm->usecnt);
21357c717d3aSMax Gurtovoy 		kfree(sig_attrs);
2136be934ccaSAriel Levkovich 	}
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	return ret;
21391da177e4SLinus Torvalds }
2140c4367a26SShamir Rabinovitch EXPORT_SYMBOL(ib_dereg_mr_user);
21411da177e4SLinus Torvalds 
21429bee178bSSagi Grimberg /**
2143b64b74b1SGal Pressman  * ib_alloc_mr() - Allocates a memory region
21449bee178bSSagi Grimberg  * @pd:            protection domain associated with the region
21459bee178bSSagi Grimberg  * @mr_type:       memory region type
21469bee178bSSagi Grimberg  * @max_num_sg:    maximum sg entries available for registration.
21479bee178bSSagi Grimberg  *
21489bee178bSSagi Grimberg  * Notes:
21499bee178bSSagi Grimberg  * Memory registeration page/sg lists must not exceed max_num_sg.
21509bee178bSSagi Grimberg  * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
21519bee178bSSagi Grimberg  * max_num_sg * used_page_size.
21529bee178bSSagi Grimberg  *
21539bee178bSSagi Grimberg  */
2154b64b74b1SGal Pressman struct ib_mr *ib_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
2155b64b74b1SGal Pressman 			  u32 max_num_sg)
215617cd3a2dSSagi Grimberg {
215717cd3a2dSSagi Grimberg 	struct ib_mr *mr;
215817cd3a2dSSagi Grimberg 
2159622db5b6SChuck Lever 	if (!pd->device->ops.alloc_mr) {
2160622db5b6SChuck Lever 		mr = ERR_PTR(-EOPNOTSUPP);
2161622db5b6SChuck Lever 		goto out;
2162622db5b6SChuck Lever 	}
216317cd3a2dSSagi Grimberg 
2164622db5b6SChuck Lever 	if (mr_type == IB_MR_TYPE_INTEGRITY) {
2165622db5b6SChuck Lever 		WARN_ON_ONCE(1);
2166622db5b6SChuck Lever 		mr = ERR_PTR(-EINVAL);
2167622db5b6SChuck Lever 		goto out;
2168622db5b6SChuck Lever 	}
216926bc7eaeSIsrael Rukshin 
217042a3b153SGal Pressman 	mr = pd->device->ops.alloc_mr(pd, mr_type, max_num_sg);
2171c5f42b21SGal Pressman 	if (IS_ERR(mr))
2172c5f42b21SGal Pressman 		goto out;
2173c5f42b21SGal Pressman 
217417cd3a2dSSagi Grimberg 	mr->device = pd->device;
217517cd3a2dSSagi Grimberg 	mr->pd = pd;
217654e7e48bSAriel Levkovich 	mr->dm = NULL;
217717cd3a2dSSagi Grimberg 	mr->uobject = NULL;
217817cd3a2dSSagi Grimberg 	atomic_inc(&pd->usecnt);
2179d4a85c30SSteve Wise 	mr->need_inval = false;
2180a0bc099aSMax Gurtovoy 	mr->type = mr_type;
21817c717d3aSMax Gurtovoy 	mr->sig_attrs = NULL;
218217cd3a2dSSagi Grimberg 
218313ef5539SLeon Romanovsky 	rdma_restrack_new(&mr->res, RDMA_RESTRACK_MR);
2184b09c4d70SLeon Romanovsky 	rdma_restrack_parent_name(&mr->res, &pd->res);
2185c34a23c2SLeon Romanovsky 	rdma_restrack_add(&mr->res);
2186622db5b6SChuck Lever out:
2187622db5b6SChuck Lever 	trace_mr_alloc(pd, mr_type, max_num_sg, mr);
218817cd3a2dSSagi Grimberg 	return mr;
218917cd3a2dSSagi Grimberg }
2190b64b74b1SGal Pressman EXPORT_SYMBOL(ib_alloc_mr);
219100f7ec36SSteve Wise 
219226bc7eaeSIsrael Rukshin /**
219326bc7eaeSIsrael Rukshin  * ib_alloc_mr_integrity() - Allocates an integrity memory region
219426bc7eaeSIsrael Rukshin  * @pd:                      protection domain associated with the region
219526bc7eaeSIsrael Rukshin  * @max_num_data_sg:         maximum data sg entries available for registration
219626bc7eaeSIsrael Rukshin  * @max_num_meta_sg:         maximum metadata sg entries available for
219726bc7eaeSIsrael Rukshin  *                           registration
219826bc7eaeSIsrael Rukshin  *
219926bc7eaeSIsrael Rukshin  * Notes:
220026bc7eaeSIsrael Rukshin  * Memory registration page/sg lists must not exceed max_num_sg,
220126bc7eaeSIsrael Rukshin  * also the integrity page/sg lists must not exceed max_num_meta_sg.
220226bc7eaeSIsrael Rukshin  *
220326bc7eaeSIsrael Rukshin  */
220426bc7eaeSIsrael Rukshin struct ib_mr *ib_alloc_mr_integrity(struct ib_pd *pd,
220526bc7eaeSIsrael Rukshin 				    u32 max_num_data_sg,
220626bc7eaeSIsrael Rukshin 				    u32 max_num_meta_sg)
220726bc7eaeSIsrael Rukshin {
220826bc7eaeSIsrael Rukshin 	struct ib_mr *mr;
22097c717d3aSMax Gurtovoy 	struct ib_sig_attrs *sig_attrs;
221026bc7eaeSIsrael Rukshin 
22112cdfcdd8SMax Gurtovoy 	if (!pd->device->ops.alloc_mr_integrity ||
2212622db5b6SChuck Lever 	    !pd->device->ops.map_mr_sg_pi) {
2213622db5b6SChuck Lever 		mr = ERR_PTR(-EOPNOTSUPP);
2214622db5b6SChuck Lever 		goto out;
2215622db5b6SChuck Lever 	}
221626bc7eaeSIsrael Rukshin 
2217622db5b6SChuck Lever 	if (!max_num_meta_sg) {
2218622db5b6SChuck Lever 		mr = ERR_PTR(-EINVAL);
2219622db5b6SChuck Lever 		goto out;
2220622db5b6SChuck Lever 	}
222126bc7eaeSIsrael Rukshin 
22227c717d3aSMax Gurtovoy 	sig_attrs = kzalloc(sizeof(struct ib_sig_attrs), GFP_KERNEL);
2223622db5b6SChuck Lever 	if (!sig_attrs) {
2224622db5b6SChuck Lever 		mr = ERR_PTR(-ENOMEM);
2225622db5b6SChuck Lever 		goto out;
2226622db5b6SChuck Lever 	}
22277c717d3aSMax Gurtovoy 
222826bc7eaeSIsrael Rukshin 	mr = pd->device->ops.alloc_mr_integrity(pd, max_num_data_sg,
222926bc7eaeSIsrael Rukshin 						max_num_meta_sg);
22307c717d3aSMax Gurtovoy 	if (IS_ERR(mr)) {
22317c717d3aSMax Gurtovoy 		kfree(sig_attrs);
2232622db5b6SChuck Lever 		goto out;
22337c717d3aSMax Gurtovoy 	}
223426bc7eaeSIsrael Rukshin 
223526bc7eaeSIsrael Rukshin 	mr->device = pd->device;
223626bc7eaeSIsrael Rukshin 	mr->pd = pd;
223726bc7eaeSIsrael Rukshin 	mr->dm = NULL;
223826bc7eaeSIsrael Rukshin 	mr->uobject = NULL;
223926bc7eaeSIsrael Rukshin 	atomic_inc(&pd->usecnt);
224026bc7eaeSIsrael Rukshin 	mr->need_inval = false;
224126bc7eaeSIsrael Rukshin 	mr->type = IB_MR_TYPE_INTEGRITY;
22427c717d3aSMax Gurtovoy 	mr->sig_attrs = sig_attrs;
224326bc7eaeSIsrael Rukshin 
224413ef5539SLeon Romanovsky 	rdma_restrack_new(&mr->res, RDMA_RESTRACK_MR);
2245b09c4d70SLeon Romanovsky 	rdma_restrack_parent_name(&mr->res, &pd->res);
2246c34a23c2SLeon Romanovsky 	rdma_restrack_add(&mr->res);
2247622db5b6SChuck Lever out:
2248622db5b6SChuck Lever 	trace_mr_integ_alloc(pd, max_num_data_sg, max_num_meta_sg, mr);
224926bc7eaeSIsrael Rukshin 	return mr;
225026bc7eaeSIsrael Rukshin }
225126bc7eaeSIsrael Rukshin EXPORT_SYMBOL(ib_alloc_mr_integrity);
225226bc7eaeSIsrael Rukshin 
22531da177e4SLinus Torvalds /* Multicast groups */
22541da177e4SLinus Torvalds 
225552363335SNoa Osherovich static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
225652363335SNoa Osherovich {
225752363335SNoa Osherovich 	struct ib_qp_init_attr init_attr = {};
225852363335SNoa Osherovich 	struct ib_qp_attr attr = {};
225952363335SNoa Osherovich 	int num_eth_ports = 0;
2260904f4f64SParav Pandit 	unsigned int port;
226152363335SNoa Osherovich 
226252363335SNoa Osherovich 	/* If QP state >= init, it is assigned to a port and we can check this
226352363335SNoa Osherovich 	 * port only.
226452363335SNoa Osherovich 	 */
226552363335SNoa Osherovich 	if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
226652363335SNoa Osherovich 		if (attr.qp_state >= IB_QPS_INIT) {
2267e6f9bc34SAlex Estrin 			if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
226852363335SNoa Osherovich 			    IB_LINK_LAYER_INFINIBAND)
226952363335SNoa Osherovich 				return true;
227052363335SNoa Osherovich 			goto lid_check;
227152363335SNoa Osherovich 		}
227252363335SNoa Osherovich 	}
227352363335SNoa Osherovich 
227452363335SNoa Osherovich 	/* Can't get a quick answer, iterate over all ports */
2275904f4f64SParav Pandit 	rdma_for_each_port(qp->device, port)
2276e6f9bc34SAlex Estrin 		if (rdma_port_get_link_layer(qp->device, port) !=
227752363335SNoa Osherovich 		    IB_LINK_LAYER_INFINIBAND)
227852363335SNoa Osherovich 			num_eth_ports++;
227952363335SNoa Osherovich 
228052363335SNoa Osherovich 	/* If we have at lease one Ethernet port, RoCE annex declares that
228152363335SNoa Osherovich 	 * multicast LID should be ignored. We can't tell at this step if the
228252363335SNoa Osherovich 	 * QP belongs to an IB or Ethernet port.
228352363335SNoa Osherovich 	 */
228452363335SNoa Osherovich 	if (num_eth_ports)
228552363335SNoa Osherovich 		return true;
228652363335SNoa Osherovich 
228752363335SNoa Osherovich 	/* If all the ports are IB, we can check according to IB spec. */
228852363335SNoa Osherovich lid_check:
228952363335SNoa Osherovich 	return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
229052363335SNoa Osherovich 		 lid == be16_to_cpu(IB_LID_PERMISSIVE));
229152363335SNoa Osherovich }
229252363335SNoa Osherovich 
22931da177e4SLinus Torvalds int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
22941da177e4SLinus Torvalds {
2295c3bccbfbSOr Gerlitz 	int ret;
2296c3bccbfbSOr Gerlitz 
22973023a1e9SKamal Heib 	if (!qp->device->ops.attach_mcast)
229887915bf8SLeon Romanovsky 		return -EOPNOTSUPP;
2299be1d325aSNoa Osherovich 
2300be1d325aSNoa Osherovich 	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2301be1d325aSNoa Osherovich 	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
23020c33aeedSJack Morgenstein 		return -EINVAL;
23030c33aeedSJack Morgenstein 
23043023a1e9SKamal Heib 	ret = qp->device->ops.attach_mcast(qp, gid, lid);
2305c3bccbfbSOr Gerlitz 	if (!ret)
2306c3bccbfbSOr Gerlitz 		atomic_inc(&qp->usecnt);
2307c3bccbfbSOr Gerlitz 	return ret;
23081da177e4SLinus Torvalds }
23091da177e4SLinus Torvalds EXPORT_SYMBOL(ib_attach_mcast);
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
23121da177e4SLinus Torvalds {
2313c3bccbfbSOr Gerlitz 	int ret;
2314c3bccbfbSOr Gerlitz 
23153023a1e9SKamal Heib 	if (!qp->device->ops.detach_mcast)
231687915bf8SLeon Romanovsky 		return -EOPNOTSUPP;
2317be1d325aSNoa Osherovich 
2318be1d325aSNoa Osherovich 	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2319be1d325aSNoa Osherovich 	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
23200c33aeedSJack Morgenstein 		return -EINVAL;
23210c33aeedSJack Morgenstein 
23223023a1e9SKamal Heib 	ret = qp->device->ops.detach_mcast(qp, gid, lid);
2323c3bccbfbSOr Gerlitz 	if (!ret)
2324c3bccbfbSOr Gerlitz 		atomic_dec(&qp->usecnt);
2325c3bccbfbSOr Gerlitz 	return ret;
23261da177e4SLinus Torvalds }
23271da177e4SLinus Torvalds EXPORT_SYMBOL(ib_detach_mcast);
232859991f94SSean Hefty 
2329b73efcb2SMaor Gottlieb /**
2330b73efcb2SMaor Gottlieb  * ib_alloc_xrcd_user - Allocates an XRC domain.
2331b73efcb2SMaor Gottlieb  * @device: The device on which to allocate the XRC domain.
2332b73efcb2SMaor Gottlieb  * @inode: inode to connect XRCD
2333b73efcb2SMaor Gottlieb  * @udata: Valid user data or NULL for kernel object
2334b73efcb2SMaor Gottlieb  */
2335b73efcb2SMaor Gottlieb struct ib_xrcd *ib_alloc_xrcd_user(struct ib_device *device,
2336b73efcb2SMaor Gottlieb 				   struct inode *inode, struct ib_udata *udata)
233759991f94SSean Hefty {
233859991f94SSean Hefty 	struct ib_xrcd *xrcd;
233928ad5f65SLeon Romanovsky 	int ret;
234059991f94SSean Hefty 
23413023a1e9SKamal Heib 	if (!device->ops.alloc_xrcd)
234287915bf8SLeon Romanovsky 		return ERR_PTR(-EOPNOTSUPP);
234359991f94SSean Hefty 
234428ad5f65SLeon Romanovsky 	xrcd = rdma_zalloc_drv_obj(device, ib_xrcd);
234528ad5f65SLeon Romanovsky 	if (!xrcd)
234628ad5f65SLeon Romanovsky 		return ERR_PTR(-ENOMEM);
234728ad5f65SLeon Romanovsky 
234859991f94SSean Hefty 	xrcd->device = device;
2349b73efcb2SMaor Gottlieb 	xrcd->inode = inode;
235059991f94SSean Hefty 	atomic_set(&xrcd->usecnt, 0);
23516f3ca6f4SMaor Gottlieb 	init_rwsem(&xrcd->tgt_qps_rwsem);
23526f3ca6f4SMaor Gottlieb 	xa_init(&xrcd->tgt_qps);
235359991f94SSean Hefty 
235428ad5f65SLeon Romanovsky 	ret = device->ops.alloc_xrcd(xrcd, udata);
235528ad5f65SLeon Romanovsky 	if (ret)
235628ad5f65SLeon Romanovsky 		goto err;
235759991f94SSean Hefty 	return xrcd;
235828ad5f65SLeon Romanovsky err:
235928ad5f65SLeon Romanovsky 	kfree(xrcd);
236028ad5f65SLeon Romanovsky 	return ERR_PTR(ret);
236159991f94SSean Hefty }
2362b73efcb2SMaor Gottlieb EXPORT_SYMBOL(ib_alloc_xrcd_user);
236359991f94SSean Hefty 
2364b73efcb2SMaor Gottlieb /**
2365b73efcb2SMaor Gottlieb  * ib_dealloc_xrcd_user - Deallocates an XRC domain.
2366b73efcb2SMaor Gottlieb  * @xrcd: The XRC domain to deallocate.
2367b73efcb2SMaor Gottlieb  * @udata: Valid user data or NULL for kernel object
2368b73efcb2SMaor Gottlieb  */
2369b73efcb2SMaor Gottlieb int ib_dealloc_xrcd_user(struct ib_xrcd *xrcd, struct ib_udata *udata)
237059991f94SSean Hefty {
2371d0c45c85SLeon Romanovsky 	int ret;
2372d0c45c85SLeon Romanovsky 
237359991f94SSean Hefty 	if (atomic_read(&xrcd->usecnt))
237459991f94SSean Hefty 		return -EBUSY;
237559991f94SSean Hefty 
23766f3ca6f4SMaor Gottlieb 	WARN_ON(!xa_empty(&xrcd->tgt_qps));
2377d0c45c85SLeon Romanovsky 	ret = xrcd->device->ops.dealloc_xrcd(xrcd, udata);
2378d0c45c85SLeon Romanovsky 	if (ret)
2379d0c45c85SLeon Romanovsky 		return ret;
238028ad5f65SLeon Romanovsky 	kfree(xrcd);
2381d0c45c85SLeon Romanovsky 	return ret;
238259991f94SSean Hefty }
2383b73efcb2SMaor Gottlieb EXPORT_SYMBOL(ib_dealloc_xrcd_user);
2384319a441dSHadar Hen Zion 
23855fd251c8SYishai Hadas /**
23865fd251c8SYishai Hadas  * ib_create_wq - Creates a WQ associated with the specified protection
23875fd251c8SYishai Hadas  * domain.
23885fd251c8SYishai Hadas  * @pd: The protection domain associated with the WQ.
23891f58621eSRandy Dunlap  * @wq_attr: A list of initial attributes required to create the
23905fd251c8SYishai Hadas  * WQ. If WQ creation succeeds, then the attributes are updated to
23915fd251c8SYishai Hadas  * the actual capabilities of the created WQ.
23925fd251c8SYishai Hadas  *
23931f58621eSRandy Dunlap  * wq_attr->max_wr and wq_attr->max_sge determine
23945fd251c8SYishai Hadas  * the requested size of the WQ, and set to the actual values allocated
23955fd251c8SYishai Hadas  * on return.
23965fd251c8SYishai Hadas  * If ib_create_wq() succeeds, then max_wr and max_sge will always be
23975fd251c8SYishai Hadas  * at least as large as the requested values.
23985fd251c8SYishai Hadas  */
23995fd251c8SYishai Hadas struct ib_wq *ib_create_wq(struct ib_pd *pd,
24005fd251c8SYishai Hadas 			   struct ib_wq_init_attr *wq_attr)
24015fd251c8SYishai Hadas {
24025fd251c8SYishai Hadas 	struct ib_wq *wq;
24035fd251c8SYishai Hadas 
24043023a1e9SKamal Heib 	if (!pd->device->ops.create_wq)
240587915bf8SLeon Romanovsky 		return ERR_PTR(-EOPNOTSUPP);
24065fd251c8SYishai Hadas 
24073023a1e9SKamal Heib 	wq = pd->device->ops.create_wq(pd, wq_attr, NULL);
24085fd251c8SYishai Hadas 	if (!IS_ERR(wq)) {
24095fd251c8SYishai Hadas 		wq->event_handler = wq_attr->event_handler;
24105fd251c8SYishai Hadas 		wq->wq_context = wq_attr->wq_context;
24115fd251c8SYishai Hadas 		wq->wq_type = wq_attr->wq_type;
24125fd251c8SYishai Hadas 		wq->cq = wq_attr->cq;
24135fd251c8SYishai Hadas 		wq->device = pd->device;
24145fd251c8SYishai Hadas 		wq->pd = pd;
24155fd251c8SYishai Hadas 		wq->uobject = NULL;
24165fd251c8SYishai Hadas 		atomic_inc(&pd->usecnt);
24175fd251c8SYishai Hadas 		atomic_inc(&wq_attr->cq->usecnt);
24185fd251c8SYishai Hadas 		atomic_set(&wq->usecnt, 0);
24195fd251c8SYishai Hadas 	}
24205fd251c8SYishai Hadas 	return wq;
24215fd251c8SYishai Hadas }
24225fd251c8SYishai Hadas EXPORT_SYMBOL(ib_create_wq);
24235fd251c8SYishai Hadas 
24245fd251c8SYishai Hadas /**
2425add53535SLeon Romanovsky  * ib_destroy_wq_user - Destroys the specified user WQ.
24265fd251c8SYishai Hadas  * @wq: The WQ to destroy.
2427c4367a26SShamir Rabinovitch  * @udata: Valid user data
24285fd251c8SYishai Hadas  */
2429add53535SLeon Romanovsky int ib_destroy_wq_user(struct ib_wq *wq, struct ib_udata *udata)
24305fd251c8SYishai Hadas {
24315fd251c8SYishai Hadas 	struct ib_cq *cq = wq->cq;
24325fd251c8SYishai Hadas 	struct ib_pd *pd = wq->pd;
2433add53535SLeon Romanovsky 	int ret;
24345fd251c8SYishai Hadas 
24355fd251c8SYishai Hadas 	if (atomic_read(&wq->usecnt))
24365fd251c8SYishai Hadas 		return -EBUSY;
24375fd251c8SYishai Hadas 
2438add53535SLeon Romanovsky 	ret = wq->device->ops.destroy_wq(wq, udata);
2439add53535SLeon Romanovsky 	if (ret)
2440add53535SLeon Romanovsky 		return ret;
2441add53535SLeon Romanovsky 
24425fd251c8SYishai Hadas 	atomic_dec(&pd->usecnt);
24435fd251c8SYishai Hadas 	atomic_dec(&cq->usecnt);
2444add53535SLeon Romanovsky 	return ret;
24455fd251c8SYishai Hadas }
2446add53535SLeon Romanovsky EXPORT_SYMBOL(ib_destroy_wq_user);
24475fd251c8SYishai Hadas 
24481b01d335SSagi Grimberg int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
24491b01d335SSagi Grimberg 		       struct ib_mr_status *mr_status)
24501b01d335SSagi Grimberg {
24513023a1e9SKamal Heib 	if (!mr->device->ops.check_mr_status)
24523023a1e9SKamal Heib 		return -EOPNOTSUPP;
24533023a1e9SKamal Heib 
24543023a1e9SKamal Heib 	return mr->device->ops.check_mr_status(mr, check_mask, mr_status);
24551b01d335SSagi Grimberg }
24561b01d335SSagi Grimberg EXPORT_SYMBOL(ib_check_mr_status);
24574c67e2bfSSagi Grimberg 
24581fb7f897SMark Bloch int ib_set_vf_link_state(struct ib_device *device, int vf, u32 port,
245950174a7fSEli Cohen 			 int state)
246050174a7fSEli Cohen {
24613023a1e9SKamal Heib 	if (!device->ops.set_vf_link_state)
246287915bf8SLeon Romanovsky 		return -EOPNOTSUPP;
246350174a7fSEli Cohen 
24643023a1e9SKamal Heib 	return device->ops.set_vf_link_state(device, vf, port, state);
246550174a7fSEli Cohen }
246650174a7fSEli Cohen EXPORT_SYMBOL(ib_set_vf_link_state);
246750174a7fSEli Cohen 
24681fb7f897SMark Bloch int ib_get_vf_config(struct ib_device *device, int vf, u32 port,
246950174a7fSEli Cohen 		     struct ifla_vf_info *info)
247050174a7fSEli Cohen {
24713023a1e9SKamal Heib 	if (!device->ops.get_vf_config)
247287915bf8SLeon Romanovsky 		return -EOPNOTSUPP;
247350174a7fSEli Cohen 
24743023a1e9SKamal Heib 	return device->ops.get_vf_config(device, vf, port, info);
247550174a7fSEli Cohen }
247650174a7fSEli Cohen EXPORT_SYMBOL(ib_get_vf_config);
247750174a7fSEli Cohen 
24781fb7f897SMark Bloch int ib_get_vf_stats(struct ib_device *device, int vf, u32 port,
247950174a7fSEli Cohen 		    struct ifla_vf_stats *stats)
248050174a7fSEli Cohen {
24813023a1e9SKamal Heib 	if (!device->ops.get_vf_stats)
248287915bf8SLeon Romanovsky 		return -EOPNOTSUPP;
248350174a7fSEli Cohen 
24843023a1e9SKamal Heib 	return device->ops.get_vf_stats(device, vf, port, stats);
248550174a7fSEli Cohen }
248650174a7fSEli Cohen EXPORT_SYMBOL(ib_get_vf_stats);
248750174a7fSEli Cohen 
24881fb7f897SMark Bloch int ib_set_vf_guid(struct ib_device *device, int vf, u32 port, u64 guid,
248950174a7fSEli Cohen 		   int type)
249050174a7fSEli Cohen {
24913023a1e9SKamal Heib 	if (!device->ops.set_vf_guid)
249287915bf8SLeon Romanovsky 		return -EOPNOTSUPP;
249350174a7fSEli Cohen 
24943023a1e9SKamal Heib 	return device->ops.set_vf_guid(device, vf, port, guid, type);
249550174a7fSEli Cohen }
249650174a7fSEli Cohen EXPORT_SYMBOL(ib_set_vf_guid);
249750174a7fSEli Cohen 
24981fb7f897SMark Bloch int ib_get_vf_guid(struct ib_device *device, int vf, u32 port,
2499bfcb3c5dSDanit Goldberg 		   struct ifla_vf_guid *node_guid,
2500bfcb3c5dSDanit Goldberg 		   struct ifla_vf_guid *port_guid)
2501bfcb3c5dSDanit Goldberg {
2502bfcb3c5dSDanit Goldberg 	if (!device->ops.get_vf_guid)
2503bfcb3c5dSDanit Goldberg 		return -EOPNOTSUPP;
2504bfcb3c5dSDanit Goldberg 
2505bfcb3c5dSDanit Goldberg 	return device->ops.get_vf_guid(device, vf, port, node_guid, port_guid);
2506bfcb3c5dSDanit Goldberg }
2507bfcb3c5dSDanit Goldberg EXPORT_SYMBOL(ib_get_vf_guid);
25084c67e2bfSSagi Grimberg /**
25092cdfcdd8SMax Gurtovoy  * ib_map_mr_sg_pi() - Map the dma mapped SG lists for PI (protection
25102cdfcdd8SMax Gurtovoy  *     information) and set an appropriate memory region for registration.
25112cdfcdd8SMax Gurtovoy  * @mr:             memory region
25122cdfcdd8SMax Gurtovoy  * @data_sg:        dma mapped scatterlist for data
25132cdfcdd8SMax Gurtovoy  * @data_sg_nents:  number of entries in data_sg
25142cdfcdd8SMax Gurtovoy  * @data_sg_offset: offset in bytes into data_sg
25152cdfcdd8SMax Gurtovoy  * @meta_sg:        dma mapped scatterlist for metadata
25162cdfcdd8SMax Gurtovoy  * @meta_sg_nents:  number of entries in meta_sg
25172cdfcdd8SMax Gurtovoy  * @meta_sg_offset: offset in bytes into meta_sg
25182cdfcdd8SMax Gurtovoy  * @page_size:      page vector desired page size
25192cdfcdd8SMax Gurtovoy  *
25202cdfcdd8SMax Gurtovoy  * Constraints:
25212cdfcdd8SMax Gurtovoy  * - The MR must be allocated with type IB_MR_TYPE_INTEGRITY.
25222cdfcdd8SMax Gurtovoy  *
25232cdfcdd8SMax Gurtovoy  * Return: 0 on success.
25242cdfcdd8SMax Gurtovoy  *
25252cdfcdd8SMax Gurtovoy  * After this completes successfully, the  memory region
25262cdfcdd8SMax Gurtovoy  * is ready for registration.
25272cdfcdd8SMax Gurtovoy  */
25282cdfcdd8SMax Gurtovoy int ib_map_mr_sg_pi(struct ib_mr *mr, struct scatterlist *data_sg,
25292cdfcdd8SMax Gurtovoy 		    int data_sg_nents, unsigned int *data_sg_offset,
25302cdfcdd8SMax Gurtovoy 		    struct scatterlist *meta_sg, int meta_sg_nents,
25312cdfcdd8SMax Gurtovoy 		    unsigned int *meta_sg_offset, unsigned int page_size)
25322cdfcdd8SMax Gurtovoy {
25332cdfcdd8SMax Gurtovoy 	if (unlikely(!mr->device->ops.map_mr_sg_pi ||
25342cdfcdd8SMax Gurtovoy 		     WARN_ON_ONCE(mr->type != IB_MR_TYPE_INTEGRITY)))
25352cdfcdd8SMax Gurtovoy 		return -EOPNOTSUPP;
25362cdfcdd8SMax Gurtovoy 
25372cdfcdd8SMax Gurtovoy 	mr->page_size = page_size;
25382cdfcdd8SMax Gurtovoy 
25392cdfcdd8SMax Gurtovoy 	return mr->device->ops.map_mr_sg_pi(mr, data_sg, data_sg_nents,
25402cdfcdd8SMax Gurtovoy 					    data_sg_offset, meta_sg,
25412cdfcdd8SMax Gurtovoy 					    meta_sg_nents, meta_sg_offset);
25422cdfcdd8SMax Gurtovoy }
25432cdfcdd8SMax Gurtovoy EXPORT_SYMBOL(ib_map_mr_sg_pi);
25442cdfcdd8SMax Gurtovoy 
25452cdfcdd8SMax Gurtovoy /**
25464c67e2bfSSagi Grimberg  * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
25474c67e2bfSSagi Grimberg  *     and set it the memory region.
25484c67e2bfSSagi Grimberg  * @mr:            memory region
25494c67e2bfSSagi Grimberg  * @sg:            dma mapped scatterlist
25504c67e2bfSSagi Grimberg  * @sg_nents:      number of entries in sg
2551ff2ba993SChristoph Hellwig  * @sg_offset:     offset in bytes into sg
25524c67e2bfSSagi Grimberg  * @page_size:     page vector desired page size
25534c67e2bfSSagi Grimberg  *
25544c67e2bfSSagi Grimberg  * Constraints:
2555255e636dSMauro Carvalho Chehab  *
25564c67e2bfSSagi Grimberg  * - The first sg element is allowed to have an offset.
255752746129SBart Van Assche  * - Each sg element must either be aligned to page_size or virtually
255852746129SBart Van Assche  *   contiguous to the previous element. In case an sg element has a
255952746129SBart Van Assche  *   non-contiguous offset, the mapping prefix will not include it.
25604c67e2bfSSagi Grimberg  * - The last sg element is allowed to have length less than page_size.
25614c67e2bfSSagi Grimberg  * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
25624c67e2bfSSagi Grimberg  *   then only max_num_sg entries will be mapped.
256352746129SBart Van Assche  * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
2564f5aa9159SSagi Grimberg  *   constraints holds and the page_size argument is ignored.
25654c67e2bfSSagi Grimberg  *
25664c67e2bfSSagi Grimberg  * Returns the number of sg elements that were mapped to the memory region.
25674c67e2bfSSagi Grimberg  *
25684c67e2bfSSagi Grimberg  * After this completes successfully, the  memory region
25694c67e2bfSSagi Grimberg  * is ready for registration.
25704c67e2bfSSagi Grimberg  */
2571ff2ba993SChristoph Hellwig int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
25729aa8b321SBart Van Assche 		 unsigned int *sg_offset, unsigned int page_size)
25734c67e2bfSSagi Grimberg {
25743023a1e9SKamal Heib 	if (unlikely(!mr->device->ops.map_mr_sg))
257587915bf8SLeon Romanovsky 		return -EOPNOTSUPP;
25764c67e2bfSSagi Grimberg 
25774c67e2bfSSagi Grimberg 	mr->page_size = page_size;
25784c67e2bfSSagi Grimberg 
25793023a1e9SKamal Heib 	return mr->device->ops.map_mr_sg(mr, sg, sg_nents, sg_offset);
25804c67e2bfSSagi Grimberg }
25814c67e2bfSSagi Grimberg EXPORT_SYMBOL(ib_map_mr_sg);
25824c67e2bfSSagi Grimberg 
25834c67e2bfSSagi Grimberg /**
25844c67e2bfSSagi Grimberg  * ib_sg_to_pages() - Convert the largest prefix of a sg list
25854c67e2bfSSagi Grimberg  *     to a page vector
25864c67e2bfSSagi Grimberg  * @mr:            memory region
25874c67e2bfSSagi Grimberg  * @sgl:           dma mapped scatterlist
25884c67e2bfSSagi Grimberg  * @sg_nents:      number of entries in sg
2589255e636dSMauro Carvalho Chehab  * @sg_offset_p:   ==== =======================================================
2590255e636dSMauro Carvalho Chehab  *                 IN   start offset in bytes into sg
2591255e636dSMauro Carvalho Chehab  *                 OUT  offset in bytes for element n of the sg of the first
25929aa8b321SBart Van Assche  *                      byte that has not been processed where n is the return
25939aa8b321SBart Van Assche  *                      value of this function.
2594255e636dSMauro Carvalho Chehab  *                 ==== =======================================================
25954c67e2bfSSagi Grimberg  * @set_page:      driver page assignment function pointer
25964c67e2bfSSagi Grimberg  *
25978f5ba10eSBart Van Assche  * Core service helper for drivers to convert the largest
25984c67e2bfSSagi Grimberg  * prefix of given sg list to a page vector. The sg list
25994c67e2bfSSagi Grimberg  * prefix converted is the prefix that meet the requirements
26004c67e2bfSSagi Grimberg  * of ib_map_mr_sg.
26014c67e2bfSSagi Grimberg  *
26024c67e2bfSSagi Grimberg  * Returns the number of sg elements that were assigned to
26034c67e2bfSSagi Grimberg  * a page vector.
26044c67e2bfSSagi Grimberg  */
2605ff2ba993SChristoph Hellwig int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
26069aa8b321SBart Van Assche 		unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
26074c67e2bfSSagi Grimberg {
26084c67e2bfSSagi Grimberg 	struct scatterlist *sg;
2609b6aeb980SBart Van Assche 	u64 last_end_dma_addr = 0;
26109aa8b321SBart Van Assche 	unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
26114c67e2bfSSagi Grimberg 	unsigned int last_page_off = 0;
26124c67e2bfSSagi Grimberg 	u64 page_mask = ~((u64)mr->page_size - 1);
26138f5ba10eSBart Van Assche 	int i, ret;
26144c67e2bfSSagi Grimberg 
26159aa8b321SBart Van Assche 	if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
26169aa8b321SBart Van Assche 		return -EINVAL;
26179aa8b321SBart Van Assche 
2618ff2ba993SChristoph Hellwig 	mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
26194c67e2bfSSagi Grimberg 	mr->length = 0;
26204c67e2bfSSagi Grimberg 
26214c67e2bfSSagi Grimberg 	for_each_sg(sgl, sg, sg_nents, i) {
2622ff2ba993SChristoph Hellwig 		u64 dma_addr = sg_dma_address(sg) + sg_offset;
26239aa8b321SBart Van Assche 		u64 prev_addr = dma_addr;
2624ff2ba993SChristoph Hellwig 		unsigned int dma_len = sg_dma_len(sg) - sg_offset;
26254c67e2bfSSagi Grimberg 		u64 end_dma_addr = dma_addr + dma_len;
26264c67e2bfSSagi Grimberg 		u64 page_addr = dma_addr & page_mask;
26274c67e2bfSSagi Grimberg 
26288f5ba10eSBart Van Assche 		/*
26298f5ba10eSBart Van Assche 		 * For the second and later elements, check whether either the
26308f5ba10eSBart Van Assche 		 * end of element i-1 or the start of element i is not aligned
26318f5ba10eSBart Van Assche 		 * on a page boundary.
26328f5ba10eSBart Van Assche 		 */
26338f5ba10eSBart Van Assche 		if (i && (last_page_off != 0 || page_addr != dma_addr)) {
26348f5ba10eSBart Van Assche 			/* Stop mapping if there is a gap. */
26358f5ba10eSBart Van Assche 			if (last_end_dma_addr != dma_addr)
26368f5ba10eSBart Van Assche 				break;
26374c67e2bfSSagi Grimberg 
26388f5ba10eSBart Van Assche 			/*
26398f5ba10eSBart Van Assche 			 * Coalesce this element with the last. If it is small
26408f5ba10eSBart Van Assche 			 * enough just update mr->length. Otherwise start
26418f5ba10eSBart Van Assche 			 * mapping from the next page.
26428f5ba10eSBart Van Assche 			 */
26438f5ba10eSBart Van Assche 			goto next_page;
26444c67e2bfSSagi Grimberg 		}
26454c67e2bfSSagi Grimberg 
26464c67e2bfSSagi Grimberg 		do {
26478f5ba10eSBart Van Assche 			ret = set_page(mr, page_addr);
26489aa8b321SBart Van Assche 			if (unlikely(ret < 0)) {
26499aa8b321SBart Van Assche 				sg_offset = prev_addr - sg_dma_address(sg);
26509aa8b321SBart Van Assche 				mr->length += prev_addr - dma_addr;
26519aa8b321SBart Van Assche 				if (sg_offset_p)
26529aa8b321SBart Van Assche 					*sg_offset_p = sg_offset;
26539aa8b321SBart Van Assche 				return i || sg_offset ? i : ret;
26549aa8b321SBart Van Assche 			}
26559aa8b321SBart Van Assche 			prev_addr = page_addr;
26568f5ba10eSBart Van Assche next_page:
26574c67e2bfSSagi Grimberg 			page_addr += mr->page_size;
26584c67e2bfSSagi Grimberg 		} while (page_addr < end_dma_addr);
26594c67e2bfSSagi Grimberg 
26604c67e2bfSSagi Grimberg 		mr->length += dma_len;
26614c67e2bfSSagi Grimberg 		last_end_dma_addr = end_dma_addr;
26624c67e2bfSSagi Grimberg 		last_page_off = end_dma_addr & ~page_mask;
2663ff2ba993SChristoph Hellwig 
2664ff2ba993SChristoph Hellwig 		sg_offset = 0;
26654c67e2bfSSagi Grimberg 	}
26664c67e2bfSSagi Grimberg 
26679aa8b321SBart Van Assche 	if (sg_offset_p)
26689aa8b321SBart Van Assche 		*sg_offset_p = 0;
26694c67e2bfSSagi Grimberg 	return i;
26704c67e2bfSSagi Grimberg }
26714c67e2bfSSagi Grimberg EXPORT_SYMBOL(ib_sg_to_pages);
2672765d6774SSteve Wise 
2673765d6774SSteve Wise struct ib_drain_cqe {
2674765d6774SSteve Wise 	struct ib_cqe cqe;
2675765d6774SSteve Wise 	struct completion done;
2676765d6774SSteve Wise };
2677765d6774SSteve Wise 
2678765d6774SSteve Wise static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
2679765d6774SSteve Wise {
2680765d6774SSteve Wise 	struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
2681765d6774SSteve Wise 						cqe);
2682765d6774SSteve Wise 
2683765d6774SSteve Wise 	complete(&cqe->done);
2684765d6774SSteve Wise }
2685765d6774SSteve Wise 
2686765d6774SSteve Wise /*
2687765d6774SSteve Wise  * Post a WR and block until its completion is reaped for the SQ.
2688765d6774SSteve Wise  */
2689765d6774SSteve Wise static void __ib_drain_sq(struct ib_qp *qp)
2690765d6774SSteve Wise {
2691f039f44fSBart Van Assche 	struct ib_cq *cq = qp->send_cq;
2692765d6774SSteve Wise 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2693765d6774SSteve Wise 	struct ib_drain_cqe sdrain;
2694a1ae7d03SBart Van Assche 	struct ib_rdma_wr swr = {
2695a1ae7d03SBart Van Assche 		.wr = {
26966ee68773SAndrew Morton 			.next = NULL,
26976ee68773SAndrew Morton 			{ .wr_cqe	= &sdrain.cqe, },
2698a1ae7d03SBart Van Assche 			.opcode	= IB_WR_RDMA_WRITE,
2699a1ae7d03SBart Van Assche 		},
2700a1ae7d03SBart Van Assche 	};
2701765d6774SSteve Wise 	int ret;
2702765d6774SSteve Wise 
2703765d6774SSteve Wise 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2704765d6774SSteve Wise 	if (ret) {
2705765d6774SSteve Wise 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2706765d6774SSteve Wise 		return;
2707765d6774SSteve Wise 	}
2708765d6774SSteve Wise 
2709aaebd377SMax Gurtovoy 	sdrain.cqe.done = ib_drain_qp_done;
2710aaebd377SMax Gurtovoy 	init_completion(&sdrain.done);
2711aaebd377SMax Gurtovoy 
27121fec77bfSBart Van Assche 	ret = ib_post_send(qp, &swr.wr, NULL);
2713765d6774SSteve Wise 	if (ret) {
2714765d6774SSteve Wise 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2715765d6774SSteve Wise 		return;
2716765d6774SSteve Wise 	}
2717765d6774SSteve Wise 
2718f039f44fSBart Van Assche 	if (cq->poll_ctx == IB_POLL_DIRECT)
2719f039f44fSBart Van Assche 		while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2720f039f44fSBart Van Assche 			ib_process_cq_direct(cq, -1);
2721f039f44fSBart Van Assche 	else
2722765d6774SSteve Wise 		wait_for_completion(&sdrain.done);
2723765d6774SSteve Wise }
2724765d6774SSteve Wise 
2725765d6774SSteve Wise /*
2726765d6774SSteve Wise  * Post a WR and block until its completion is reaped for the RQ.
2727765d6774SSteve Wise  */
2728765d6774SSteve Wise static void __ib_drain_rq(struct ib_qp *qp)
2729765d6774SSteve Wise {
2730f039f44fSBart Van Assche 	struct ib_cq *cq = qp->recv_cq;
2731765d6774SSteve Wise 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2732765d6774SSteve Wise 	struct ib_drain_cqe rdrain;
27331fec77bfSBart Van Assche 	struct ib_recv_wr rwr = {};
2734765d6774SSteve Wise 	int ret;
2735765d6774SSteve Wise 
2736765d6774SSteve Wise 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2737765d6774SSteve Wise 	if (ret) {
2738765d6774SSteve Wise 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2739765d6774SSteve Wise 		return;
2740765d6774SSteve Wise 	}
2741765d6774SSteve Wise 
2742aaebd377SMax Gurtovoy 	rwr.wr_cqe = &rdrain.cqe;
2743aaebd377SMax Gurtovoy 	rdrain.cqe.done = ib_drain_qp_done;
2744aaebd377SMax Gurtovoy 	init_completion(&rdrain.done);
2745aaebd377SMax Gurtovoy 
27461fec77bfSBart Van Assche 	ret = ib_post_recv(qp, &rwr, NULL);
2747765d6774SSteve Wise 	if (ret) {
2748765d6774SSteve Wise 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2749765d6774SSteve Wise 		return;
2750765d6774SSteve Wise 	}
2751765d6774SSteve Wise 
2752f039f44fSBart Van Assche 	if (cq->poll_ctx == IB_POLL_DIRECT)
2753f039f44fSBart Van Assche 		while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2754f039f44fSBart Van Assche 			ib_process_cq_direct(cq, -1);
2755f039f44fSBart Van Assche 	else
2756765d6774SSteve Wise 		wait_for_completion(&rdrain.done);
2757765d6774SSteve Wise }
2758765d6774SSteve Wise 
2759765d6774SSteve Wise /**
2760765d6774SSteve Wise  * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2761765d6774SSteve Wise  *		   application.
2762765d6774SSteve Wise  * @qp:            queue pair to drain
2763765d6774SSteve Wise  *
2764765d6774SSteve Wise  * If the device has a provider-specific drain function, then
2765765d6774SSteve Wise  * call that.  Otherwise call the generic drain function
2766765d6774SSteve Wise  * __ib_drain_sq().
2767765d6774SSteve Wise  *
2768765d6774SSteve Wise  * The caller must:
2769765d6774SSteve Wise  *
2770765d6774SSteve Wise  * ensure there is room in the CQ and SQ for the drain work request and
2771765d6774SSteve Wise  * completion.
2772765d6774SSteve Wise  *
2773f039f44fSBart Van Assche  * allocate the CQ using ib_alloc_cq().
2774765d6774SSteve Wise  *
2775765d6774SSteve Wise  * ensure that there are no other contexts that are posting WRs concurrently.
2776765d6774SSteve Wise  * Otherwise the drain is not guaranteed.
2777765d6774SSteve Wise  */
2778765d6774SSteve Wise void ib_drain_sq(struct ib_qp *qp)
2779765d6774SSteve Wise {
27803023a1e9SKamal Heib 	if (qp->device->ops.drain_sq)
27813023a1e9SKamal Heib 		qp->device->ops.drain_sq(qp);
2782765d6774SSteve Wise 	else
2783765d6774SSteve Wise 		__ib_drain_sq(qp);
27843e5901cbSChuck Lever 	trace_cq_drain_complete(qp->send_cq);
2785765d6774SSteve Wise }
2786765d6774SSteve Wise EXPORT_SYMBOL(ib_drain_sq);
2787765d6774SSteve Wise 
2788765d6774SSteve Wise /**
2789765d6774SSteve Wise  * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2790765d6774SSteve Wise  *		   application.
2791765d6774SSteve Wise  * @qp:            queue pair to drain
2792765d6774SSteve Wise  *
2793765d6774SSteve Wise  * If the device has a provider-specific drain function, then
2794765d6774SSteve Wise  * call that.  Otherwise call the generic drain function
2795765d6774SSteve Wise  * __ib_drain_rq().
2796765d6774SSteve Wise  *
2797765d6774SSteve Wise  * The caller must:
2798765d6774SSteve Wise  *
2799765d6774SSteve Wise  * ensure there is room in the CQ and RQ for the drain work request and
2800765d6774SSteve Wise  * completion.
2801765d6774SSteve Wise  *
2802f039f44fSBart Van Assche  * allocate the CQ using ib_alloc_cq().
2803765d6774SSteve Wise  *
2804765d6774SSteve Wise  * ensure that there are no other contexts that are posting WRs concurrently.
2805765d6774SSteve Wise  * Otherwise the drain is not guaranteed.
2806765d6774SSteve Wise  */
2807765d6774SSteve Wise void ib_drain_rq(struct ib_qp *qp)
2808765d6774SSteve Wise {
28093023a1e9SKamal Heib 	if (qp->device->ops.drain_rq)
28103023a1e9SKamal Heib 		qp->device->ops.drain_rq(qp);
2811765d6774SSteve Wise 	else
2812765d6774SSteve Wise 		__ib_drain_rq(qp);
28133e5901cbSChuck Lever 	trace_cq_drain_complete(qp->recv_cq);
2814765d6774SSteve Wise }
2815765d6774SSteve Wise EXPORT_SYMBOL(ib_drain_rq);
2816765d6774SSteve Wise 
2817765d6774SSteve Wise /**
2818765d6774SSteve Wise  * ib_drain_qp() - Block until all CQEs have been consumed by the
2819765d6774SSteve Wise  *		   application on both the RQ and SQ.
2820765d6774SSteve Wise  * @qp:            queue pair to drain
2821765d6774SSteve Wise  *
2822765d6774SSteve Wise  * The caller must:
2823765d6774SSteve Wise  *
2824765d6774SSteve Wise  * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2825765d6774SSteve Wise  * and completions.
2826765d6774SSteve Wise  *
2827f039f44fSBart Van Assche  * allocate the CQs using ib_alloc_cq().
2828765d6774SSteve Wise  *
2829765d6774SSteve Wise  * ensure that there are no other contexts that are posting WRs concurrently.
2830765d6774SSteve Wise  * Otherwise the drain is not guaranteed.
2831765d6774SSteve Wise  */
2832765d6774SSteve Wise void ib_drain_qp(struct ib_qp *qp)
2833765d6774SSteve Wise {
2834765d6774SSteve Wise 	ib_drain_sq(qp);
283542235f80SSagi Grimberg 	if (!qp->srq)
2836765d6774SSteve Wise 		ib_drain_rq(qp);
2837765d6774SSteve Wise }
2838765d6774SSteve Wise EXPORT_SYMBOL(ib_drain_qp);
2839f6a8a19bSDenis Drozdov 
28401fb7f897SMark Bloch struct net_device *rdma_alloc_netdev(struct ib_device *device, u32 port_num,
2841f6a8a19bSDenis Drozdov 				     enum rdma_netdev_t type, const char *name,
2842f6a8a19bSDenis Drozdov 				     unsigned char name_assign_type,
2843f6a8a19bSDenis Drozdov 				     void (*setup)(struct net_device *))
2844f6a8a19bSDenis Drozdov {
2845f6a8a19bSDenis Drozdov 	struct rdma_netdev_alloc_params params;
2846f6a8a19bSDenis Drozdov 	struct net_device *netdev;
2847f6a8a19bSDenis Drozdov 	int rc;
2848f6a8a19bSDenis Drozdov 
28493023a1e9SKamal Heib 	if (!device->ops.rdma_netdev_get_params)
2850f6a8a19bSDenis Drozdov 		return ERR_PTR(-EOPNOTSUPP);
2851f6a8a19bSDenis Drozdov 
28523023a1e9SKamal Heib 	rc = device->ops.rdma_netdev_get_params(device, port_num, type,
28533023a1e9SKamal Heib 						&params);
2854f6a8a19bSDenis Drozdov 	if (rc)
2855f6a8a19bSDenis Drozdov 		return ERR_PTR(rc);
2856f6a8a19bSDenis Drozdov 
2857f6a8a19bSDenis Drozdov 	netdev = alloc_netdev_mqs(params.sizeof_priv, name, name_assign_type,
2858f6a8a19bSDenis Drozdov 				  setup, params.txqs, params.rxqs);
2859f6a8a19bSDenis Drozdov 	if (!netdev)
2860f6a8a19bSDenis Drozdov 		return ERR_PTR(-ENOMEM);
2861f6a8a19bSDenis Drozdov 
2862f6a8a19bSDenis Drozdov 	return netdev;
2863f6a8a19bSDenis Drozdov }
2864f6a8a19bSDenis Drozdov EXPORT_SYMBOL(rdma_alloc_netdev);
28655d6b0cb3SDenis Drozdov 
28661fb7f897SMark Bloch int rdma_init_netdev(struct ib_device *device, u32 port_num,
28675d6b0cb3SDenis Drozdov 		     enum rdma_netdev_t type, const char *name,
28685d6b0cb3SDenis Drozdov 		     unsigned char name_assign_type,
28695d6b0cb3SDenis Drozdov 		     void (*setup)(struct net_device *),
28705d6b0cb3SDenis Drozdov 		     struct net_device *netdev)
28715d6b0cb3SDenis Drozdov {
28725d6b0cb3SDenis Drozdov 	struct rdma_netdev_alloc_params params;
28735d6b0cb3SDenis Drozdov 	int rc;
28745d6b0cb3SDenis Drozdov 
28753023a1e9SKamal Heib 	if (!device->ops.rdma_netdev_get_params)
28765d6b0cb3SDenis Drozdov 		return -EOPNOTSUPP;
28775d6b0cb3SDenis Drozdov 
28783023a1e9SKamal Heib 	rc = device->ops.rdma_netdev_get_params(device, port_num, type,
28793023a1e9SKamal Heib 						&params);
28805d6b0cb3SDenis Drozdov 	if (rc)
28815d6b0cb3SDenis Drozdov 		return rc;
28825d6b0cb3SDenis Drozdov 
28835d6b0cb3SDenis Drozdov 	return params.initialize_rdma_netdev(device, port_num,
28845d6b0cb3SDenis Drozdov 					     netdev, params.param);
28855d6b0cb3SDenis Drozdov }
28865d6b0cb3SDenis Drozdov EXPORT_SYMBOL(rdma_init_netdev);
2887a808273aSShiraz Saleem 
2888a808273aSShiraz Saleem void __rdma_block_iter_start(struct ib_block_iter *biter,
2889a808273aSShiraz Saleem 			     struct scatterlist *sglist, unsigned int nents,
2890a808273aSShiraz Saleem 			     unsigned long pgsz)
2891a808273aSShiraz Saleem {
2892a808273aSShiraz Saleem 	memset(biter, 0, sizeof(struct ib_block_iter));
2893a808273aSShiraz Saleem 	biter->__sg = sglist;
2894a808273aSShiraz Saleem 	biter->__sg_nents = nents;
2895a808273aSShiraz Saleem 
2896a808273aSShiraz Saleem 	/* Driver provides best block size to use */
2897a808273aSShiraz Saleem 	biter->__pg_bit = __fls(pgsz);
2898a808273aSShiraz Saleem }
2899a808273aSShiraz Saleem EXPORT_SYMBOL(__rdma_block_iter_start);
2900a808273aSShiraz Saleem 
2901a808273aSShiraz Saleem bool __rdma_block_iter_next(struct ib_block_iter *biter)
2902a808273aSShiraz Saleem {
2903a808273aSShiraz Saleem 	unsigned int block_offset;
2904a808273aSShiraz Saleem 
2905a808273aSShiraz Saleem 	if (!biter->__sg_nents || !biter->__sg)
2906a808273aSShiraz Saleem 		return false;
2907a808273aSShiraz Saleem 
2908a808273aSShiraz Saleem 	biter->__dma_addr = sg_dma_address(biter->__sg) + biter->__sg_advance;
2909a808273aSShiraz Saleem 	block_offset = biter->__dma_addr & (BIT_ULL(biter->__pg_bit) - 1);
2910a808273aSShiraz Saleem 	biter->__sg_advance += BIT_ULL(biter->__pg_bit) - block_offset;
2911a808273aSShiraz Saleem 
2912a808273aSShiraz Saleem 	if (biter->__sg_advance >= sg_dma_len(biter->__sg)) {
2913a808273aSShiraz Saleem 		biter->__sg_advance = 0;
2914a808273aSShiraz Saleem 		biter->__sg = sg_next(biter->__sg);
2915a808273aSShiraz Saleem 		biter->__sg_nents--;
2916a808273aSShiraz Saleem 	}
2917a808273aSShiraz Saleem 
2918a808273aSShiraz Saleem 	return true;
2919a808273aSShiraz Saleem }
2920a808273aSShiraz Saleem EXPORT_SYMBOL(__rdma_block_iter_next);
2921