verbs.c 60.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
 * Copyright (c) 2004 Infinicon Corporation.  All rights reserved.
 * Copyright (c) 2004 Intel Corporation.  All rights reserved.
 * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
 * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
7
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
8
 * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/errno.h>
#include <linux/err.h>
41
#include <linux/export.h>
42
#include <linux/string.h>
43
#include <linux/slab.h>
44 45 46
#include <linux/in.h>
#include <linux/in6.h>
#include <net/addrconf.h>
47
#include <linux/security.h>
L
Linus Torvalds 已提交
48

49 50
#include <rdma/ib_verbs.h>
#include <rdma/ib_cache.h>
51
#include <rdma/ib_addr.h>
52
#include <rdma/rw.h>
L
Linus Torvalds 已提交
53

54
#include "core_priv.h"
L
Linus Torvalds 已提交
55

56 57 58
static int ib_resolve_eth_dmac(struct ib_device *device,
			       struct rdma_ah_attr *ah_attr);

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static const char * const ib_events[] = {
	[IB_EVENT_CQ_ERR]		= "CQ error",
	[IB_EVENT_QP_FATAL]		= "QP fatal error",
	[IB_EVENT_QP_REQ_ERR]		= "QP request error",
	[IB_EVENT_QP_ACCESS_ERR]	= "QP access error",
	[IB_EVENT_COMM_EST]		= "communication established",
	[IB_EVENT_SQ_DRAINED]		= "send queue drained",
	[IB_EVENT_PATH_MIG]		= "path migration successful",
	[IB_EVENT_PATH_MIG_ERR]		= "path migration error",
	[IB_EVENT_DEVICE_FATAL]		= "device fatal error",
	[IB_EVENT_PORT_ACTIVE]		= "port active",
	[IB_EVENT_PORT_ERR]		= "port error",
	[IB_EVENT_LID_CHANGE]		= "LID change",
	[IB_EVENT_PKEY_CHANGE]		= "P_key change",
	[IB_EVENT_SM_CHANGE]		= "SM change",
	[IB_EVENT_SRQ_ERR]		= "SRQ error",
	[IB_EVENT_SRQ_LIMIT_REACHED]	= "SRQ limit reached",
	[IB_EVENT_QP_LAST_WQE_REACHED]	= "last WQE reached",
	[IB_EVENT_CLIENT_REREGISTER]	= "client reregister",
	[IB_EVENT_GID_CHANGE]		= "GID changed",
};

81
const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
	size_t index = event;

	return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
			ib_events[index] : "unrecognized event";
}
EXPORT_SYMBOL(ib_event_msg);

static const char * const wc_statuses[] = {
	[IB_WC_SUCCESS]			= "success",
	[IB_WC_LOC_LEN_ERR]		= "local length error",
	[IB_WC_LOC_QP_OP_ERR]		= "local QP operation error",
	[IB_WC_LOC_EEC_OP_ERR]		= "local EE context operation error",
	[IB_WC_LOC_PROT_ERR]		= "local protection error",
	[IB_WC_WR_FLUSH_ERR]		= "WR flushed",
	[IB_WC_MW_BIND_ERR]		= "memory management operation error",
	[IB_WC_BAD_RESP_ERR]		= "bad response error",
	[IB_WC_LOC_ACCESS_ERR]		= "local access error",
	[IB_WC_REM_INV_REQ_ERR]		= "invalid request error",
	[IB_WC_REM_ACCESS_ERR]		= "remote access error",
	[IB_WC_REM_OP_ERR]		= "remote operation error",
	[IB_WC_RETRY_EXC_ERR]		= "transport retry counter exceeded",
	[IB_WC_RNR_RETRY_EXC_ERR]	= "RNR retry counter exceeded",
	[IB_WC_LOC_RDD_VIOL_ERR]	= "local RDD violation error",
	[IB_WC_REM_INV_RD_REQ_ERR]	= "remote invalid RD request",
	[IB_WC_REM_ABORT_ERR]		= "operation aborted",
	[IB_WC_INV_EECN_ERR]		= "invalid EE context number",
	[IB_WC_INV_EEC_STATE_ERR]	= "invalid EE context state",
	[IB_WC_FATAL_ERR]		= "fatal error",
	[IB_WC_RESP_TIMEOUT_ERR]	= "response timeout error",
	[IB_WC_GENERAL_ERR]		= "general error",
};

115
const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
116 117 118 119 120 121 122 123
{
	size_t index = status;

	return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
			wc_statuses[index] : "unrecognized status";
}
EXPORT_SYMBOL(ib_wc_status_msg);

124
__attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
125 126
{
	switch (rate) {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	case IB_RATE_2_5_GBPS: return   1;
	case IB_RATE_5_GBPS:   return   2;
	case IB_RATE_10_GBPS:  return   4;
	case IB_RATE_20_GBPS:  return   8;
	case IB_RATE_30_GBPS:  return  12;
	case IB_RATE_40_GBPS:  return  16;
	case IB_RATE_60_GBPS:  return  24;
	case IB_RATE_80_GBPS:  return  32;
	case IB_RATE_120_GBPS: return  48;
	case IB_RATE_14_GBPS:  return   6;
	case IB_RATE_56_GBPS:  return  22;
	case IB_RATE_112_GBPS: return  45;
	case IB_RATE_168_GBPS: return  67;
	case IB_RATE_25_GBPS:  return  10;
	case IB_RATE_100_GBPS: return  40;
	case IB_RATE_200_GBPS: return  80;
	case IB_RATE_300_GBPS: return 120;
	default:	       return  -1;
145 146 147 148
	}
}
EXPORT_SYMBOL(ib_rate_to_mult);

149
__attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
150 151
{
	switch (mult) {
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	case 1:   return IB_RATE_2_5_GBPS;
	case 2:   return IB_RATE_5_GBPS;
	case 4:   return IB_RATE_10_GBPS;
	case 8:   return IB_RATE_20_GBPS;
	case 12:  return IB_RATE_30_GBPS;
	case 16:  return IB_RATE_40_GBPS;
	case 24:  return IB_RATE_60_GBPS;
	case 32:  return IB_RATE_80_GBPS;
	case 48:  return IB_RATE_120_GBPS;
	case 6:   return IB_RATE_14_GBPS;
	case 22:  return IB_RATE_56_GBPS;
	case 45:  return IB_RATE_112_GBPS;
	case 67:  return IB_RATE_168_GBPS;
	case 10:  return IB_RATE_25_GBPS;
	case 40:  return IB_RATE_100_GBPS;
	case 80:  return IB_RATE_200_GBPS;
	case 120: return IB_RATE_300_GBPS;
	default:  return IB_RATE_PORT_CURRENT;
170 171 172 173
	}
}
EXPORT_SYMBOL(mult_to_ib_rate);

174
__attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
{
	switch (rate) {
	case IB_RATE_2_5_GBPS: return 2500;
	case IB_RATE_5_GBPS:   return 5000;
	case IB_RATE_10_GBPS:  return 10000;
	case IB_RATE_20_GBPS:  return 20000;
	case IB_RATE_30_GBPS:  return 30000;
	case IB_RATE_40_GBPS:  return 40000;
	case IB_RATE_60_GBPS:  return 60000;
	case IB_RATE_80_GBPS:  return 80000;
	case IB_RATE_120_GBPS: return 120000;
	case IB_RATE_14_GBPS:  return 14062;
	case IB_RATE_56_GBPS:  return 56250;
	case IB_RATE_112_GBPS: return 112500;
	case IB_RATE_168_GBPS: return 168750;
	case IB_RATE_25_GBPS:  return 25781;
	case IB_RATE_100_GBPS: return 103125;
	case IB_RATE_200_GBPS: return 206250;
	case IB_RATE_300_GBPS: return 309375;
	default:	       return -1;
	}
}
EXPORT_SYMBOL(ib_rate_to_mbps);

199
__attribute_const__ enum rdma_transport_type
T
Tom Tucker 已提交
200 201
rdma_node_get_transport(enum rdma_node_type node_type)
{
202 203

	if (node_type == RDMA_NODE_USNIC)
204
		return RDMA_TRANSPORT_USNIC;
205
	if (node_type == RDMA_NODE_USNIC_UDP)
206
		return RDMA_TRANSPORT_USNIC_UDP;
207 208 209 210
	if (node_type == RDMA_NODE_RNIC)
		return RDMA_TRANSPORT_IWARP;

	return RDMA_TRANSPORT_IB;
T
Tom Tucker 已提交
211 212 213
}
EXPORT_SYMBOL(rdma_node_get_transport);

214 215
enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
{
216
	enum rdma_transport_type lt;
217 218 219
	if (device->get_link_layer)
		return device->get_link_layer(device, port_num);

220 221
	lt = rdma_node_get_transport(device->node_type);
	if (lt == RDMA_TRANSPORT_IB)
222
		return IB_LINK_LAYER_INFINIBAND;
223 224

	return IB_LINK_LAYER_ETHERNET;
225 226 227
}
EXPORT_SYMBOL(rdma_port_get_link_layer);

L
Linus Torvalds 已提交
228 229
/* Protection domains */

230 231 232 233 234 235 236 237 238 239
/**
 * ib_alloc_pd - Allocates an unused protection domain.
 * @device: The device on which to allocate the protection domain.
 *
 * A protection domain object provides an association between QPs, shared
 * receive queues, address handles, memory regions, and memory windows.
 *
 * Every PD has a local_dma_lkey which can be used as the lkey value for local
 * memory operations.
 */
240 241
struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
		const char *caller)
L
Linus Torvalds 已提交
242 243
{
	struct ib_pd *pd;
244
	int mr_access_flags = 0;
L
Linus Torvalds 已提交
245

246
	pd = device->alloc_pd(device, NULL, NULL);
247 248
	if (IS_ERR(pd))
		return pd;
L
Linus Torvalds 已提交
249

250 251
	pd->device = device;
	pd->uobject = NULL;
252
	pd->__internal_mr = NULL;
253
	atomic_set(&pd->usecnt, 0);
254
	pd->flags = flags;
L
Linus Torvalds 已提交
255

256
	if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
257
		pd->local_dma_lkey = device->local_dma_lkey;
258 259 260 261 262 263 264 265
	else
		mr_access_flags |= IB_ACCESS_LOCAL_WRITE;

	if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
		pr_warn("%s: enabling unsafe global rkey\n", caller);
		mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
	}

266 267 268 269
	pd->res.type = RDMA_RESTRACK_PD;
	pd->res.kern_name = caller;
	rdma_restrack_add(&pd->res);

270
	if (mr_access_flags) {
271 272
		struct ib_mr *mr;

273
		mr = pd->device->get_dma_mr(pd, mr_access_flags);
274 275
		if (IS_ERR(mr)) {
			ib_dealloc_pd(pd);
276
			return ERR_CAST(mr);
277
		}
L
Linus Torvalds 已提交
278

279 280 281 282 283
		mr->device	= pd->device;
		mr->pd		= pd;
		mr->uobject	= NULL;
		mr->need_inval	= false;

284
		pd->__internal_mr = mr;
285 286 287 288 289 290

		if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
			pd->local_dma_lkey = pd->__internal_mr->lkey;

		if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
			pd->unsafe_global_rkey = pd->__internal_mr->rkey;
L
Linus Torvalds 已提交
291
	}
292

L
Linus Torvalds 已提交
293 294
	return pd;
}
295
EXPORT_SYMBOL(__ib_alloc_pd);
L
Linus Torvalds 已提交
296

297 298 299 300 301 302 303 304 305
/**
 * ib_dealloc_pd - Deallocates a protection domain.
 * @pd: The protection domain to deallocate.
 *
 * It is an error to call this function while any resources in the pd still
 * exist.  The caller is responsible to synchronously destroy them and
 * guarantee no new allocations will happen.
 */
void ib_dealloc_pd(struct ib_pd *pd)
L
Linus Torvalds 已提交
306
{
307 308
	int ret;

309
	if (pd->__internal_mr) {
310
		ret = pd->device->dereg_mr(pd->__internal_mr);
311
		WARN_ON(ret);
312
		pd->__internal_mr = NULL;
313
	}
L
Linus Torvalds 已提交
314

315 316 317
	/* uverbs manipulates usecnt with proper locking, while the kabi
	   requires the caller to guarantee we can't race here. */
	WARN_ON(atomic_read(&pd->usecnt));
L
Linus Torvalds 已提交
318

319
	rdma_restrack_del(&pd->res);
320 321 322 323
	/* Making delalloc_pd a void return is a WIP, no driver should return
	   an error here. */
	ret = pd->device->dealloc_pd(pd);
	WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
L
Linus Torvalds 已提交
324 325 326 327 328
}
EXPORT_SYMBOL(ib_dealloc_pd);

/* Address handles */

329 330 331
static struct ib_ah *_rdma_create_ah(struct ib_pd *pd,
				     struct rdma_ah_attr *ah_attr,
				     struct ib_udata *udata)
L
Linus Torvalds 已提交
332 333 334
{
	struct ib_ah *ah;

335
	ah = pd->device->create_ah(pd, ah_attr, udata);
L
Linus Torvalds 已提交
336 337

	if (!IS_ERR(ah)) {
338 339 340
		ah->device  = pd->device;
		ah->pd      = pd;
		ah->uobject = NULL;
341
		ah->type    = ah_attr->type;
L
Linus Torvalds 已提交
342 343 344 345 346
		atomic_inc(&pd->usecnt);
	}

	return ah;
}
347 348 349 350 351

struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr)
{
	return _rdma_create_ah(pd, ah_attr, NULL);
}
352
EXPORT_SYMBOL(rdma_create_ah);
L
Linus Torvalds 已提交
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
/**
 * rdma_create_user_ah - Creates an address handle for the
 * given address vector.
 * It resolves destination mac address for ah attribute of RoCE type.
 * @pd: The protection domain associated with the address handle.
 * @ah_attr: The attributes of the address vector.
 * @udata: pointer to user's input output buffer information need by
 *         provider driver.
 *
 * It returns 0 on success and returns appropriate error code on error.
 * The address handle is used to reference a local or global destination
 * in all UD QP post sends.
 */
struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
				  struct rdma_ah_attr *ah_attr,
				  struct ib_udata *udata)
{
	int err;

	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
		err = ib_resolve_eth_dmac(pd->device, ah_attr);
		if (err)
			return ERR_PTR(err);
	}

	return _rdma_create_ah(pd, ah_attr, udata);
}
EXPORT_SYMBOL(rdma_create_user_ah);

383
int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
{
	const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
	struct iphdr ip4h_checked;
	const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;

	/* If it's IPv6, the version must be 6, otherwise, the first
	 * 20 bytes (before the IPv4 header) are garbled.
	 */
	if (ip6h->version != 6)
		return (ip4h->version == 4) ? 4 : 0;
	/* version may be 6 or 4 because the first 20 bytes could be garbled */

	/* RoCE v2 requires no options, thus header length
	 * must be 5 words
	 */
	if (ip4h->ihl != 5)
		return 6;

	/* Verify checksum.
	 * We can't write on scattered buffers so we need to copy to
	 * temp buffer.
	 */
	memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
	ip4h_checked.check = 0;
	ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
	/* if IPv4 header checksum is OK, believe it */
	if (ip4h->check == ip4h_checked.check)
		return 4;
	return 6;
}
414
EXPORT_SYMBOL(ib_get_rdma_header_version);
415 416 417 418 419 420 421 422 423 424

static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
						     u8 port_num,
						     const struct ib_grh *grh)
{
	int grh_version;

	if (rdma_protocol_ib(device, port_num))
		return RDMA_NETWORK_IB;

425
	grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
426 427 428 429 430 431 432 433 434 435

	if (grh_version == 4)
		return RDMA_NETWORK_IPV4;

	if (grh->next_hdr == IPPROTO_UDP)
		return RDMA_NETWORK_IPV6;

	return RDMA_NETWORK_ROCE_V1;
}

436 437
struct find_gid_index_context {
	u16 vlan_id;
438
	enum ib_gid_type gid_type;
439 440 441 442 443 444
};

static bool find_gid_index(const union ib_gid *gid,
			   const struct ib_gid_attr *gid_attr,
			   void *context)
{
445
	struct find_gid_index_context *ctx = context;
446

447 448 449
	if (ctx->gid_type != gid_attr->gid_type)
		return false;

450 451 452 453 454 455 456 457 458 459
	if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) ||
	    (is_vlan_dev(gid_attr->ndev) &&
	     vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id))
		return false;

	return true;
}

static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num,
				   u16 vlan_id, const union ib_gid *sgid,
460
				   enum ib_gid_type gid_type,
461 462
				   u16 *gid_index)
{
463 464
	struct find_gid_index_context context = {.vlan_id = vlan_id,
						 .gid_type = gid_type};
465 466 467 468 469

	return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index,
				     &context, gid_index);
}

470 471 472
int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
			      enum rdma_network_type net_type,
			      union ib_gid *sgid, union ib_gid *dgid)
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
{
	struct sockaddr_in  src_in;
	struct sockaddr_in  dst_in;
	__be32 src_saddr, dst_saddr;

	if (!sgid || !dgid)
		return -EINVAL;

	if (net_type == RDMA_NETWORK_IPV4) {
		memcpy(&src_in.sin_addr.s_addr,
		       &hdr->roce4grh.saddr, 4);
		memcpy(&dst_in.sin_addr.s_addr,
		       &hdr->roce4grh.daddr, 4);
		src_saddr = src_in.sin_addr.s_addr;
		dst_saddr = dst_in.sin_addr.s_addr;
		ipv6_addr_set_v4mapped(src_saddr,
				       (struct in6_addr *)sgid);
		ipv6_addr_set_v4mapped(dst_saddr,
				       (struct in6_addr *)dgid);
		return 0;
	} else if (net_type == RDMA_NETWORK_IPV6 ||
		   net_type == RDMA_NETWORK_IB) {
		*dgid = hdr->ibgrh.dgid;
		*sgid = hdr->ibgrh.sgid;
		return 0;
	} else {
		return -EINVAL;
	}
}
502
EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
503

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
/* Resolve destination mac address and hop limit for unicast destination
 * GID entry, considering the source GID entry as well.
 * ah_attribute must have have valid port_num, sgid_index.
 */
static int ib_resolve_unicast_gid_dmac(struct ib_device *device,
				       struct rdma_ah_attr *ah_attr)
{
	struct ib_gid_attr sgid_attr;
	struct ib_global_route *grh;
	int hop_limit = 0xff;
	union ib_gid sgid;
	int ret;

	grh = rdma_ah_retrieve_grh(ah_attr);

519 520
	ret = ib_get_cached_gid(device, rdma_ah_get_port_num(ah_attr),
				grh->sgid_index, &sgid, &sgid_attr);
521 522 523 524 525 526
	if (ret || !sgid_attr.ndev) {
		if (!ret)
			ret = -ENXIO;
		return ret;
	}

527 528 529 530 531 532 533 534 535 536
	/* If destination is link local and source GID is RoCEv1,
	 * IP stack is not used.
	 */
	if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) &&
	    sgid_attr.gid_type == IB_GID_TYPE_ROCE) {
		rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
				ah_attr->roce.dmac);
		goto done;
	}

537 538 539
	ret = rdma_addr_find_l2_eth_by_grh(&sgid, &grh->dgid,
					   ah_attr->roce.dmac,
					   sgid_attr.ndev, &hop_limit);
540
done:
541 542 543 544 545 546
	dev_put(sgid_attr.ndev);

	grh->hop_limit = hop_limit;
	return ret;
}

547
/*
548
 * This function initializes address handle attributes from the incoming packet.
549 550 551 552 553 554 555 556
 * Incoming packet has dgid of the receiver node on which this code is
 * getting executed and, sgid contains the GID of the sender.
 *
 * When resolving mac address of destination, the arrived dgid is used
 * as sgid and, sgid is used as dgid because sgid contains destinations
 * GID whom to respond to.
 *
 */
557 558 559
int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num,
			    const struct ib_wc *wc, const struct ib_grh *grh,
			    struct rdma_ah_attr *ah_attr)
560 561 562 563
{
	u32 flow_class;
	u16 gid_index;
	int ret;
564 565
	enum rdma_network_type net_type = RDMA_NETWORK_IB;
	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
566
	int hoplimit = 0xff;
567 568
	union ib_gid dgid;
	union ib_gid sgid;
569

570 571
	might_sleep();

S
Sean Hefty 已提交
572
	memset(ah_attr, 0, sizeof *ah_attr);
573
	ah_attr->type = rdma_ah_find_type(device, port_num);
574
	if (rdma_cap_eth_ah(device, port_num)) {
575 576 577 578 579 580
		if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
			net_type = wc->network_hdr_type;
		else
			net_type = ib_get_net_type_by_grh(device, port_num, grh);
		gid_type = ib_network_to_gid_type(net_type);
	}
581 582
	ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
					&sgid, &dgid);
583 584 585
	if (ret)
		return ret;

586 587 588
	rdma_ah_set_sl(ah_attr, wc->sl);
	rdma_ah_set_port_num(ah_attr, port_num);

589
	if (rdma_protocol_roce(device, port_num)) {
590 591 592
		u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
				wc->vlan_id : 0xffff;

593 594 595
		if (!(wc->wc_flags & IB_WC_GRH))
			return -EPROTOTYPE;

596 597 598
		ret = get_sgid_index_from_eth(device, port_num,
					      vlan_id, &dgid,
					      gid_type, &gid_index);
599 600
		if (ret)
			return ret;
601

602 603 604 605 606 607 608 609 610
		flow_class = be32_to_cpu(grh->version_tclass_flow);
		rdma_ah_set_grh(ah_attr, &sgid,
				flow_class & 0xFFFFF,
				(u8)gid_index, hoplimit,
				(flow_class >> 20) & 0xFF);
		return ib_resolve_unicast_gid_dmac(device, ah_attr);
	} else {
		rdma_ah_set_dlid(ah_attr, wc->slid);
		rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
611

612
		if (wc->wc_flags & IB_WC_GRH) {
613 614 615 616 617 618 619 620 621 622
			if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
				ret = ib_find_cached_gid_by_port(device, &dgid,
								 IB_GID_TYPE_IB,
								 port_num, NULL,
								 &gid_index);
				if (ret)
					return ret;
			} else {
				gid_index = 0;
			}
623

624 625 626 627 628 629 630
			flow_class = be32_to_cpu(grh->version_tclass_flow);
			rdma_ah_set_grh(ah_attr, &sgid,
					flow_class & 0xFFFFF,
					(u8)gid_index, hoplimit,
					(flow_class >> 20) & 0xFF);
		}
		return 0;
631
	}
S
Sean Hefty 已提交
632
}
633
EXPORT_SYMBOL(ib_init_ah_attr_from_wc);
S
Sean Hefty 已提交
634

635 636
struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
				   const struct ib_grh *grh, u8 port_num)
S
Sean Hefty 已提交
637
{
638
	struct rdma_ah_attr ah_attr;
S
Sean Hefty 已提交
639 640
	int ret;

641
	ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr);
S
Sean Hefty 已提交
642 643
	if (ret)
		return ERR_PTR(ret);
644

645
	return rdma_create_ah(pd, &ah_attr);
646 647 648
}
EXPORT_SYMBOL(ib_create_ah_from_wc);

649
int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
L
Linus Torvalds 已提交
650
{
651 652 653
	if (ah->type != ah_attr->type)
		return -EINVAL;

L
Linus Torvalds 已提交
654 655
	return ah->device->modify_ah ?
		ah->device->modify_ah(ah, ah_attr) :
656
		-EOPNOTSUPP;
L
Linus Torvalds 已提交
657
}
658
EXPORT_SYMBOL(rdma_modify_ah);
L
Linus Torvalds 已提交
659

660
int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
L
Linus Torvalds 已提交
661 662 663
{
	return ah->device->query_ah ?
		ah->device->query_ah(ah, ah_attr) :
664
		-EOPNOTSUPP;
L
Linus Torvalds 已提交
665
}
666
EXPORT_SYMBOL(rdma_query_ah);
L
Linus Torvalds 已提交
667

668
int rdma_destroy_ah(struct ib_ah *ah)
L
Linus Torvalds 已提交
669 670 671 672 673 674 675 676 677 678 679
{
	struct ib_pd *pd;
	int ret;

	pd = ah->pd;
	ret = ah->device->destroy_ah(ah);
	if (!ret)
		atomic_dec(&pd->usecnt);

	return ret;
}
680
EXPORT_SYMBOL(rdma_destroy_ah);
L
Linus Torvalds 已提交
681

682 683 684 685 686 687 688 689
/* Shared receive queues */

struct ib_srq *ib_create_srq(struct ib_pd *pd,
			     struct ib_srq_init_attr *srq_init_attr)
{
	struct ib_srq *srq;

	if (!pd->device->create_srq)
690
		return ERR_PTR(-EOPNOTSUPP);
691 692 693 694 695 696 697 698 699

	srq = pd->device->create_srq(pd, srq_init_attr, NULL);

	if (!IS_ERR(srq)) {
		srq->device    	   = pd->device;
		srq->pd        	   = pd;
		srq->uobject       = NULL;
		srq->event_handler = srq_init_attr->event_handler;
		srq->srq_context   = srq_init_attr->srq_context;
S
Sean Hefty 已提交
700
		srq->srq_type      = srq_init_attr->srq_type;
701 702 703 704
		if (ib_srq_has_cq(srq->srq_type)) {
			srq->ext.cq   = srq_init_attr->ext.cq;
			atomic_inc(&srq->ext.cq->usecnt);
		}
S
Sean Hefty 已提交
705 706 707 708
		if (srq->srq_type == IB_SRQT_XRC) {
			srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
			atomic_inc(&srq->ext.xrc.xrcd->usecnt);
		}
709 710 711 712 713 714 715 716 717 718 719 720
		atomic_inc(&pd->usecnt);
		atomic_set(&srq->usecnt, 0);
	}

	return srq;
}
EXPORT_SYMBOL(ib_create_srq);

int ib_modify_srq(struct ib_srq *srq,
		  struct ib_srq_attr *srq_attr,
		  enum ib_srq_attr_mask srq_attr_mask)
{
721 722
	return srq->device->modify_srq ?
		srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
723
		-EOPNOTSUPP;
724 725 726 727 728 729 730
}
EXPORT_SYMBOL(ib_modify_srq);

int ib_query_srq(struct ib_srq *srq,
		 struct ib_srq_attr *srq_attr)
{
	return srq->device->query_srq ?
731
		srq->device->query_srq(srq, srq_attr) : -EOPNOTSUPP;
732 733 734 735 736 737
}
EXPORT_SYMBOL(ib_query_srq);

int ib_destroy_srq(struct ib_srq *srq)
{
	struct ib_pd *pd;
S
Sean Hefty 已提交
738 739 740
	enum ib_srq_type srq_type;
	struct ib_xrcd *uninitialized_var(xrcd);
	struct ib_cq *uninitialized_var(cq);
741 742 743 744 745 746
	int ret;

	if (atomic_read(&srq->usecnt))
		return -EBUSY;

	pd = srq->pd;
S
Sean Hefty 已提交
747
	srq_type = srq->srq_type;
748 749 750
	if (ib_srq_has_cq(srq_type))
		cq = srq->ext.cq;
	if (srq_type == IB_SRQT_XRC)
S
Sean Hefty 已提交
751
		xrcd = srq->ext.xrc.xrcd;
752 753

	ret = srq->device->destroy_srq(srq);
S
Sean Hefty 已提交
754
	if (!ret) {
755
		atomic_dec(&pd->usecnt);
756
		if (srq_type == IB_SRQT_XRC)
S
Sean Hefty 已提交
757
			atomic_dec(&xrcd->usecnt);
758
		if (ib_srq_has_cq(srq_type))
S
Sean Hefty 已提交
759 760
			atomic_dec(&cq->usecnt);
	}
761 762 763 764 765

	return ret;
}
EXPORT_SYMBOL(ib_destroy_srq);

L
Linus Torvalds 已提交
766 767
/* Queue pairs */

768 769 770
static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
{
	struct ib_qp *qp = context;
771
	unsigned long flags;
772

773
	spin_lock_irqsave(&qp->device->event_handler_lock, flags);
774
	list_for_each_entry(event->element.qp, &qp->open_list, open_list)
775 776
		if (event->element.qp->event_handler)
			event->element.qp->event_handler(event, event->element.qp->qp_context);
777
	spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
778 779
}

780 781 782 783 784 785 786
static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
{
	mutex_lock(&xrcd->tgt_qp_mutex);
	list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
	mutex_unlock(&xrcd->tgt_qp_mutex);
}

787 788 789
static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
				  void (*event_handler)(struct ib_event *, void *),
				  void *qp_context)
790
{
791 792
	struct ib_qp *qp;
	unsigned long flags;
793
	int err;
794 795 796 797 798

	qp = kzalloc(sizeof *qp, GFP_KERNEL);
	if (!qp)
		return ERR_PTR(-ENOMEM);

799 800 801 802 803 804 805
	qp->real_qp = real_qp;
	err = ib_open_shared_qp_security(qp, real_qp->device);
	if (err) {
		kfree(qp);
		return ERR_PTR(err);
	}

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
	qp->real_qp = real_qp;
	atomic_inc(&real_qp->usecnt);
	qp->device = real_qp->device;
	qp->event_handler = event_handler;
	qp->qp_context = qp_context;
	qp->qp_num = real_qp->qp_num;
	qp->qp_type = real_qp->qp_type;

	spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
	list_add(&qp->open_list, &real_qp->open_list);
	spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);

	return qp;
}

struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
			 struct ib_qp_open_attr *qp_open_attr)
{
	struct ib_qp *qp, *real_qp;

	if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
		return ERR_PTR(-EINVAL);

	qp = ERR_PTR(-EINVAL);
830
	mutex_lock(&xrcd->tgt_qp_mutex);
831 832 833 834 835 836 837
	list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
		if (real_qp->qp_num == qp_open_attr->qp_num) {
			qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
					  qp_open_attr->qp_context);
			break;
		}
	}
838
	mutex_unlock(&xrcd->tgt_qp_mutex);
839
	return qp;
840
}
841
EXPORT_SYMBOL(ib_open_qp);
842

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp,
		struct ib_qp_init_attr *qp_init_attr)
{
	struct ib_qp *real_qp = qp;

	qp->event_handler = __ib_shared_qp_event_handler;
	qp->qp_context = qp;
	qp->pd = NULL;
	qp->send_cq = qp->recv_cq = NULL;
	qp->srq = NULL;
	qp->xrcd = qp_init_attr->xrcd;
	atomic_inc(&qp_init_attr->xrcd->usecnt);
	INIT_LIST_HEAD(&qp->open_list);

	qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
			  qp_init_attr->qp_context);
	if (!IS_ERR(qp))
		__ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
	else
		real_qp->device->destroy_qp(real_qp);
	return qp;
}

L
Linus Torvalds 已提交
866 867 868
struct ib_qp *ib_create_qp(struct ib_pd *pd,
			   struct ib_qp_init_attr *qp_init_attr)
{
869 870
	struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
	struct ib_qp *qp;
871 872
	int ret;

873 874 875 876 877 878
	if (qp_init_attr->rwq_ind_tbl &&
	    (qp_init_attr->recv_cq ||
	    qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
	    qp_init_attr->cap.max_recv_sge))
		return ERR_PTR(-EINVAL);

879 880 881 882 883 884 885 886
	/*
	 * If the callers is using the RDMA API calculate the resources
	 * needed for the RDMA READ/WRITE operations.
	 *
	 * Note that these callers need to pass in a port number.
	 */
	if (qp_init_attr->cap.max_rdma_ctxs)
		rdma_rw_init_qp(device, qp_init_attr);
L
Linus Torvalds 已提交
887

888
	qp = _ib_create_qp(device, pd, qp_init_attr, NULL, NULL);
889 890 891
	if (IS_ERR(qp))
		return qp;

892 893 894 895 896 897
	ret = ib_create_qp_security(qp, device);
	if (ret) {
		ib_destroy_qp(qp);
		return ERR_PTR(ret);
	}

898 899
	qp->real_qp    = qp;
	qp->qp_type    = qp_init_attr->qp_type;
900
	qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
901 902

	atomic_set(&qp->usecnt, 0);
903 904
	qp->mrs_used = 0;
	spin_lock_init(&qp->mr_lock);
905
	INIT_LIST_HEAD(&qp->rdma_mrs);
906
	INIT_LIST_HEAD(&qp->sig_mrs);
907
	qp->port = 0;
908

909 910 911 912 913 914 915 916 917 918
	if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
		return ib_create_xrc_qp(qp, qp_init_attr);

	qp->event_handler = qp_init_attr->event_handler;
	qp->qp_context = qp_init_attr->qp_context;
	if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
		qp->recv_cq = NULL;
		qp->srq = NULL;
	} else {
		qp->recv_cq = qp_init_attr->recv_cq;
919 920
		if (qp_init_attr->recv_cq)
			atomic_inc(&qp_init_attr->recv_cq->usecnt);
921 922 923
		qp->srq = qp_init_attr->srq;
		if (qp->srq)
			atomic_inc(&qp_init_attr->srq->usecnt);
L
Linus Torvalds 已提交
924 925
	}

926 927 928 929
	qp->send_cq = qp_init_attr->send_cq;
	qp->xrcd    = NULL;

	atomic_inc(&pd->usecnt);
930 931 932 933
	if (qp_init_attr->send_cq)
		atomic_inc(&qp_init_attr->send_cq->usecnt);
	if (qp_init_attr->rwq_ind_tbl)
		atomic_inc(&qp->rwq_ind_tbl->usecnt);
934 935 936 937 938 939

	if (qp_init_attr->cap.max_rdma_ctxs) {
		ret = rdma_rw_init_mrs(qp, qp_init_attr);
		if (ret) {
			pr_err("failed to init MR pool ret= %d\n", ret);
			ib_destroy_qp(qp);
940
			return ERR_PTR(ret);
941 942 943
		}
	}

944 945 946 947 948 949 950 951 952
	/*
	 * Note: all hw drivers guarantee that max_send_sge is lower than
	 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
	 * max_send_sge <= max_sge_rd.
	 */
	qp->max_write_sge = qp_init_attr->cap.max_send_sge;
	qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
				 device->attrs.max_sge_rd);

L
Linus Torvalds 已提交
953 954 955 956
	return qp;
}
EXPORT_SYMBOL(ib_create_qp);

957 958
static const struct {
	int			valid;
S
Sean Hefty 已提交
959 960
	enum ib_qp_attr_mask	req_param[IB_QPT_MAX];
	enum ib_qp_attr_mask	opt_param[IB_QPT_MAX];
961 962 963 964 965 966 967 968 969
} qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
	[IB_QPS_RESET] = {
		[IB_QPS_RESET] = { .valid = 1 },
		[IB_QPS_INIT]  = {
			.valid = 1,
			.req_param = {
				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_QKEY),
O
Or Gerlitz 已提交
970
				[IB_QPT_RAW_PACKET] = IB_QP_PORT,
971 972 973 974 975 976
				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_ACCESS_FLAGS),
				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_ACCESS_FLAGS),
S
Sean Hefty 已提交
977 978 979 980 981 982
				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_ACCESS_FLAGS),
				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_ACCESS_FLAGS),
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
						IB_QP_QKEY),
				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
						IB_QP_QKEY),
			}
		},
	},
	[IB_QPS_INIT]  = {
		[IB_QPS_RESET] = { .valid = 1 },
		[IB_QPS_ERR] =   { .valid = 1 },
		[IB_QPS_INIT]  = {
			.valid = 1,
			.opt_param = {
				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_QKEY),
				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_ACCESS_FLAGS),
				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_ACCESS_FLAGS),
S
Sean Hefty 已提交
1005 1006 1007 1008 1009 1010
				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_ACCESS_FLAGS),
				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
						IB_QP_PORT			|
						IB_QP_ACCESS_FLAGS),
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
						IB_QP_QKEY),
				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
						IB_QP_QKEY),
			}
		},
		[IB_QPS_RTR]   = {
			.valid = 1,
			.req_param = {
				[IB_QPT_UC]  = (IB_QP_AV			|
						IB_QP_PATH_MTU			|
						IB_QP_DEST_QPN			|
						IB_QP_RQ_PSN),
				[IB_QPT_RC]  = (IB_QP_AV			|
						IB_QP_PATH_MTU			|
						IB_QP_DEST_QPN			|
						IB_QP_RQ_PSN			|
						IB_QP_MAX_DEST_RD_ATOMIC	|
						IB_QP_MIN_RNR_TIMER),
S
Sean Hefty 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
				[IB_QPT_XRC_INI] = (IB_QP_AV			|
						IB_QP_PATH_MTU			|
						IB_QP_DEST_QPN			|
						IB_QP_RQ_PSN),
				[IB_QPT_XRC_TGT] = (IB_QP_AV			|
						IB_QP_PATH_MTU			|
						IB_QP_DEST_QPN			|
						IB_QP_RQ_PSN			|
						IB_QP_MAX_DEST_RD_ATOMIC	|
						IB_QP_MIN_RNR_TIMER),
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
			},
			.opt_param = {
				 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
						 IB_QP_QKEY),
				 [IB_QPT_UC]  = (IB_QP_ALT_PATH			|
						 IB_QP_ACCESS_FLAGS		|
						 IB_QP_PKEY_INDEX),
				 [IB_QPT_RC]  = (IB_QP_ALT_PATH			|
						 IB_QP_ACCESS_FLAGS		|
						 IB_QP_PKEY_INDEX),
S
Sean Hefty 已提交
1050 1051 1052 1053 1054 1055
				 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH		|
						 IB_QP_ACCESS_FLAGS		|
						 IB_QP_PKEY_INDEX),
				 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH		|
						 IB_QP_ACCESS_FLAGS		|
						 IB_QP_PKEY_INDEX),
1056 1057 1058 1059
				 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
						 IB_QP_QKEY),
				 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
						 IB_QP_QKEY),
1060
			 },
1061
		},
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	},
	[IB_QPS_RTR]   = {
		[IB_QPS_RESET] = { .valid = 1 },
		[IB_QPS_ERR] =   { .valid = 1 },
		[IB_QPS_RTS]   = {
			.valid = 1,
			.req_param = {
				[IB_QPT_UD]  = IB_QP_SQ_PSN,
				[IB_QPT_UC]  = IB_QP_SQ_PSN,
				[IB_QPT_RC]  = (IB_QP_TIMEOUT			|
						IB_QP_RETRY_CNT			|
						IB_QP_RNR_RETRY			|
						IB_QP_SQ_PSN			|
						IB_QP_MAX_QP_RD_ATOMIC),
S
Sean Hefty 已提交
1076 1077 1078 1079 1080 1081 1082
				[IB_QPT_XRC_INI] = (IB_QP_TIMEOUT		|
						IB_QP_RETRY_CNT			|
						IB_QP_RNR_RETRY			|
						IB_QP_SQ_PSN			|
						IB_QP_MAX_QP_RD_ATOMIC),
				[IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT		|
						IB_QP_SQ_PSN),
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
				[IB_QPT_SMI] = IB_QP_SQ_PSN,
				[IB_QPT_GSI] = IB_QP_SQ_PSN,
			},
			.opt_param = {
				 [IB_QPT_UD]  = (IB_QP_CUR_STATE		|
						 IB_QP_QKEY),
				 [IB_QPT_UC]  = (IB_QP_CUR_STATE		|
						 IB_QP_ALT_PATH			|
						 IB_QP_ACCESS_FLAGS		|
						 IB_QP_PATH_MIG_STATE),
				 [IB_QPT_RC]  = (IB_QP_CUR_STATE		|
						 IB_QP_ALT_PATH			|
						 IB_QP_ACCESS_FLAGS		|
						 IB_QP_MIN_RNR_TIMER		|
						 IB_QP_PATH_MIG_STATE),
S
Sean Hefty 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106
				 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
						 IB_QP_ALT_PATH			|
						 IB_QP_ACCESS_FLAGS		|
						 IB_QP_PATH_MIG_STATE),
				 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
						 IB_QP_ALT_PATH			|
						 IB_QP_ACCESS_FLAGS		|
						 IB_QP_MIN_RNR_TIMER		|
						 IB_QP_PATH_MIG_STATE),
1107 1108 1109 1110
				 [IB_QPT_SMI] = (IB_QP_CUR_STATE		|
						 IB_QP_QKEY),
				 [IB_QPT_GSI] = (IB_QP_CUR_STATE		|
						 IB_QP_QKEY),
1111
				 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
			 }
		}
	},
	[IB_QPS_RTS]   = {
		[IB_QPS_RESET] = { .valid = 1 },
		[IB_QPS_ERR] =   { .valid = 1 },
		[IB_QPS_RTS]   = {
			.valid = 1,
			.opt_param = {
				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
1123 1124
				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
						IB_QP_ACCESS_FLAGS		|
1125 1126
						IB_QP_ALT_PATH			|
						IB_QP_PATH_MIG_STATE),
1127 1128
				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
						IB_QP_ACCESS_FLAGS		|
1129 1130 1131
						IB_QP_ALT_PATH			|
						IB_QP_PATH_MIG_STATE		|
						IB_QP_MIN_RNR_TIMER),
S
Sean Hefty 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140
				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_ALT_PATH			|
						IB_QP_PATH_MIG_STATE),
				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_ALT_PATH			|
						IB_QP_PATH_MIG_STATE		|
						IB_QP_MIN_RNR_TIMER),
1141 1142 1143 1144
				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
1145
				[IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1146 1147 1148 1149 1150 1151 1152 1153
			}
		},
		[IB_QPS_SQD]   = {
			.valid = 1,
			.opt_param = {
				[IB_QPT_UD]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
				[IB_QPT_UC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
				[IB_QPT_RC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
S
Sean Hefty 已提交
1154 1155
				[IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
				[IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
				[IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
				[IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
			}
		},
	},
	[IB_QPS_SQD]   = {
		[IB_QPS_RESET] = { .valid = 1 },
		[IB_QPS_ERR] =   { .valid = 1 },
		[IB_QPS_RTS]   = {
			.valid = 1,
			.opt_param = {
				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
						IB_QP_ALT_PATH			|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_PATH_MIG_STATE),
				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
						IB_QP_ALT_PATH			|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_MIN_RNR_TIMER		|
						IB_QP_PATH_MIG_STATE),
S
Sean Hefty 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186
				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
						IB_QP_ALT_PATH			|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_PATH_MIG_STATE),
				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
						IB_QP_ALT_PATH			|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_MIN_RNR_TIMER		|
						IB_QP_PATH_MIG_STATE),
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
			}
		},
		[IB_QPS_SQD]   = {
			.valid = 1,
			.opt_param = {
				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
						IB_QP_QKEY),
				[IB_QPT_UC]  = (IB_QP_AV			|
						IB_QP_ALT_PATH			|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_PKEY_INDEX		|
						IB_QP_PATH_MIG_STATE),
				[IB_QPT_RC]  = (IB_QP_PORT			|
						IB_QP_AV			|
						IB_QP_TIMEOUT			|
						IB_QP_RETRY_CNT			|
						IB_QP_RNR_RETRY			|
						IB_QP_MAX_QP_RD_ATOMIC		|
						IB_QP_MAX_DEST_RD_ATOMIC	|
						IB_QP_ALT_PATH			|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_PKEY_INDEX		|
						IB_QP_MIN_RNR_TIMER		|
						IB_QP_PATH_MIG_STATE),
S
Sean Hefty 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
				[IB_QPT_XRC_INI] = (IB_QP_PORT			|
						IB_QP_AV			|
						IB_QP_TIMEOUT			|
						IB_QP_RETRY_CNT			|
						IB_QP_RNR_RETRY			|
						IB_QP_MAX_QP_RD_ATOMIC		|
						IB_QP_ALT_PATH			|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_PKEY_INDEX		|
						IB_QP_PATH_MIG_STATE),
				[IB_QPT_XRC_TGT] = (IB_QP_PORT			|
						IB_QP_AV			|
						IB_QP_TIMEOUT			|
						IB_QP_MAX_DEST_RD_ATOMIC	|
						IB_QP_ALT_PATH			|
						IB_QP_ACCESS_FLAGS		|
						IB_QP_PKEY_INDEX		|
						IB_QP_MIN_RNR_TIMER		|
						IB_QP_PATH_MIG_STATE),
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
						IB_QP_QKEY),
				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
						IB_QP_QKEY),
			}
		}
	},
	[IB_QPS_SQE]   = {
		[IB_QPS_RESET] = { .valid = 1 },
		[IB_QPS_ERR] =   { .valid = 1 },
		[IB_QPS_RTS]   = {
			.valid = 1,
			.opt_param = {
				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
						IB_QP_ACCESS_FLAGS),
				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
						IB_QP_QKEY),
			}
		}
	},
	[IB_QPS_ERR] = {
		[IB_QPS_RESET] = { .valid = 1 },
		[IB_QPS_ERR] =   { .valid = 1 }
	}
};

1264 1265 1266
bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
			enum ib_qp_type type, enum ib_qp_attr_mask mask,
			enum rdma_link_layer ll)
1267 1268 1269 1270 1271 1272
{
	enum ib_qp_attr_mask req_param, opt_param;

	if (mask & IB_QP_CUR_STATE  &&
	    cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1273
		return false;
1274 1275

	if (!qp_state_table[cur_state][next_state].valid)
1276
		return false;
1277 1278 1279 1280 1281

	req_param = qp_state_table[cur_state][next_state].req_param[type];
	opt_param = qp_state_table[cur_state][next_state].opt_param[type];

	if ((mask & req_param) != req_param)
1282
		return false;
1283 1284

	if (mask & ~(req_param | opt_param | IB_QP_STATE))
1285
		return false;
1286

1287
	return true;
1288 1289 1290
}
EXPORT_SYMBOL(ib_modify_qp_is_ok);

1291 1292
static int ib_resolve_eth_dmac(struct ib_device *device,
			       struct rdma_ah_attr *ah_attr)
1293 1294
{
	int           ret = 0;
1295
	struct ib_global_route *grh;
1296

1297
	if (!rdma_is_port_valid(device, rdma_ah_get_port_num(ah_attr)))
1298
		return -EINVAL;
1299

1300 1301
	grh = rdma_ah_retrieve_grh(ah_attr);

1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
		if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
			__be32 addr = 0;

			memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
			ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
		} else {
			ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
					(char *)ah_attr->roce.dmac);
		}
1312
	} else {
1313
		ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
1314 1315 1316 1317
	}
	return ret;
}

1318
/**
1319
 * IB core internal function to perform QP attributes modification.
1320
 */
1321 1322
static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
			 int attr_mask, struct ib_udata *udata)
L
Linus Torvalds 已提交
1323
{
1324
	u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1325
	int ret;
1326

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
	if (rdma_ib_or_roce(qp->device, port)) {
		if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
			pr_warn("%s: %s rq_psn overflow, masking to 24 bits\n",
				__func__, qp->device->name);
			attr->rq_psn &= 0xffffff;
		}

		if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
			pr_warn("%s: %s sq_psn overflow, masking to 24 bits\n",
				__func__, qp->device->name);
			attr->sq_psn &= 0xffffff;
		}
	}

1341 1342 1343 1344 1345
	ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
	if (!ret && (attr_mask & IB_QP_PORT))
		qp->port = attr->port_num;

	return ret;
1346
}
1347

1348 1349 1350 1351 1352 1353 1354 1355
static bool is_qp_type_connected(const struct ib_qp *qp)
{
	return (qp->qp_type == IB_QPT_UC ||
		qp->qp_type == IB_QPT_RC ||
		qp->qp_type == IB_QPT_XRC_INI ||
		qp->qp_type == IB_QPT_XRC_TGT);
}

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
/**
 * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
 * @ib_qp: The QP to modify.
 * @attr: On input, specifies the QP attributes to modify.  On output,
 *   the current values of selected QP attributes are returned.
 * @attr_mask: A bit-mask used to specify which attributes of the QP
 *   are being modified.
 * @udata: pointer to user's input output buffer information
 *   are being modified.
 * It returns 0 on success and returns appropriate error code on error.
 */
int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
			    int attr_mask, struct ib_udata *udata)
{
	struct ib_qp *qp = ib_qp->real_qp;
	int ret;

1373
	if (attr_mask & IB_QP_AV &&
1374 1375
	    attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
	    is_qp_type_connected(qp)) {
1376 1377 1378 1379 1380 1381
		ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
		if (ret)
			return ret;
	}
	return _ib_modify_qp(qp, attr, attr_mask, udata);
}
1382
EXPORT_SYMBOL(ib_modify_qp_with_udata);
1383

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
{
	int rc;
	u32 netdev_speed;
	struct net_device *netdev;
	struct ethtool_link_ksettings lksettings;

	if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
		return -EINVAL;

	if (!dev->get_netdev)
		return -EOPNOTSUPP;

	netdev = dev->get_netdev(dev, port_num);
	if (!netdev)
		return -ENODEV;

	rtnl_lock();
	rc = __ethtool_get_link_ksettings(netdev, &lksettings);
	rtnl_unlock();

	dev_put(netdev);

	if (!rc) {
		netdev_speed = lksettings.base.speed;
	} else {
		netdev_speed = SPEED_1000;
		pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
			netdev_speed);
	}

	if (netdev_speed <= SPEED_1000) {
		*width = IB_WIDTH_1X;
		*speed = IB_SPEED_SDR;
	} else if (netdev_speed <= SPEED_10000) {
		*width = IB_WIDTH_1X;
		*speed = IB_SPEED_FDR10;
	} else if (netdev_speed <= SPEED_20000) {
		*width = IB_WIDTH_4X;
		*speed = IB_SPEED_DDR;
	} else if (netdev_speed <= SPEED_25000) {
		*width = IB_WIDTH_1X;
		*speed = IB_SPEED_EDR;
	} else if (netdev_speed <= SPEED_40000) {
		*width = IB_WIDTH_4X;
		*speed = IB_SPEED_FDR10;
	} else {
		*width = IB_WIDTH_4X;
		*speed = IB_SPEED_EDR;
	}

	return 0;
}
EXPORT_SYMBOL(ib_get_eth_speed);

1439 1440 1441 1442
int ib_modify_qp(struct ib_qp *qp,
		 struct ib_qp_attr *qp_attr,
		 int qp_attr_mask)
{
1443
	return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452
}
EXPORT_SYMBOL(ib_modify_qp);

int ib_query_qp(struct ib_qp *qp,
		struct ib_qp_attr *qp_attr,
		int qp_attr_mask,
		struct ib_qp_init_attr *qp_init_attr)
{
	return qp->device->query_qp ?
1453
		qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
1454
		-EOPNOTSUPP;
L
Linus Torvalds 已提交
1455 1456 1457
}
EXPORT_SYMBOL(ib_query_qp);

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
int ib_close_qp(struct ib_qp *qp)
{
	struct ib_qp *real_qp;
	unsigned long flags;

	real_qp = qp->real_qp;
	if (real_qp == qp)
		return -EINVAL;

	spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
	list_del(&qp->open_list);
	spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);

	atomic_dec(&real_qp->usecnt);
1472 1473
	if (qp->qp_sec)
		ib_close_shared_qp_security(qp->qp_sec);
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
	kfree(qp);

	return 0;
}
EXPORT_SYMBOL(ib_close_qp);

static int __ib_destroy_shared_qp(struct ib_qp *qp)
{
	struct ib_xrcd *xrcd;
	struct ib_qp *real_qp;
	int ret;

	real_qp = qp->real_qp;
	xrcd = real_qp->xrcd;

	mutex_lock(&xrcd->tgt_qp_mutex);
	ib_close_qp(qp);
	if (atomic_read(&real_qp->usecnt) == 0)
		list_del(&real_qp->xrcd_list);
	else
		real_qp = NULL;
	mutex_unlock(&xrcd->tgt_qp_mutex);

	if (real_qp) {
		ret = ib_destroy_qp(real_qp);
		if (!ret)
			atomic_dec(&xrcd->usecnt);
		else
			__ib_insert_xrcd_qp(xrcd, real_qp);
	}

	return 0;
}

L
Linus Torvalds 已提交
1508 1509 1510 1511 1512
int ib_destroy_qp(struct ib_qp *qp)
{
	struct ib_pd *pd;
	struct ib_cq *scq, *rcq;
	struct ib_srq *srq;
1513
	struct ib_rwq_ind_table *ind_tbl;
1514
	struct ib_qp_security *sec;
L
Linus Torvalds 已提交
1515 1516
	int ret;

1517 1518
	WARN_ON_ONCE(qp->mrs_used > 0);

1519 1520 1521 1522 1523 1524
	if (atomic_read(&qp->usecnt))
		return -EBUSY;

	if (qp->real_qp != qp)
		return __ib_destroy_shared_qp(qp);

S
Sean Hefty 已提交
1525 1526 1527 1528
	pd   = qp->pd;
	scq  = qp->send_cq;
	rcq  = qp->recv_cq;
	srq  = qp->srq;
1529
	ind_tbl = qp->rwq_ind_tbl;
1530 1531 1532
	sec  = qp->qp_sec;
	if (sec)
		ib_destroy_qp_security_begin(sec);
L
Linus Torvalds 已提交
1533

1534 1535 1536
	if (!qp->uobject)
		rdma_rw_cleanup_mrs(qp);

1537
	rdma_restrack_del(&qp->res);
L
Linus Torvalds 已提交
1538 1539
	ret = qp->device->destroy_qp(qp);
	if (!ret) {
S
Sean Hefty 已提交
1540 1541 1542 1543 1544 1545
		if (pd)
			atomic_dec(&pd->usecnt);
		if (scq)
			atomic_dec(&scq->usecnt);
		if (rcq)
			atomic_dec(&rcq->usecnt);
L
Linus Torvalds 已提交
1546 1547
		if (srq)
			atomic_dec(&srq->usecnt);
1548 1549
		if (ind_tbl)
			atomic_dec(&ind_tbl->usecnt);
1550 1551 1552 1553 1554
		if (sec)
			ib_destroy_qp_security_end(sec);
	} else {
		if (sec)
			ib_destroy_qp_security_abort(sec);
L
Linus Torvalds 已提交
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
	}

	return ret;
}
EXPORT_SYMBOL(ib_destroy_qp);

/* Completion queues */

struct ib_cq *ib_create_cq(struct ib_device *device,
			   ib_comp_handler comp_handler,
			   void (*event_handler)(struct ib_event *, void *),
1566 1567
			   void *cq_context,
			   const struct ib_cq_init_attr *cq_attr)
L
Linus Torvalds 已提交
1568 1569 1570
{
	struct ib_cq *cq;

1571
	cq = device->create_cq(device, cq_attr, NULL, NULL);
L
Linus Torvalds 已提交
1572 1573 1574

	if (!IS_ERR(cq)) {
		cq->device        = device;
1575
		cq->uobject       = NULL;
L
Linus Torvalds 已提交
1576 1577 1578 1579
		cq->comp_handler  = comp_handler;
		cq->event_handler = event_handler;
		cq->cq_context    = cq_context;
		atomic_set(&cq->usecnt, 0);
1580 1581
		cq->res.type = RDMA_RESTRACK_CQ;
		rdma_restrack_add(&cq->res);
L
Linus Torvalds 已提交
1582 1583 1584 1585 1586 1587
	}

	return cq;
}
EXPORT_SYMBOL(ib_create_cq);

1588
int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
E
Eli Cohen 已提交
1589 1590
{
	return cq->device->modify_cq ?
1591
		cq->device->modify_cq(cq, cq_count, cq_period) : -EOPNOTSUPP;
E
Eli Cohen 已提交
1592
}
1593
EXPORT_SYMBOL(rdma_set_cq_moderation);
E
Eli Cohen 已提交
1594

L
Linus Torvalds 已提交
1595 1596 1597 1598 1599
int ib_destroy_cq(struct ib_cq *cq)
{
	if (atomic_read(&cq->usecnt))
		return -EBUSY;

1600
	rdma_restrack_del(&cq->res);
L
Linus Torvalds 已提交
1601 1602 1603 1604
	return cq->device->destroy_cq(cq);
}
EXPORT_SYMBOL(ib_destroy_cq);

R
Roland Dreier 已提交
1605
int ib_resize_cq(struct ib_cq *cq, int cqe)
L
Linus Torvalds 已提交
1606
{
1607
	return cq->device->resize_cq ?
1608
		cq->device->resize_cq(cq, cqe, NULL) : -EOPNOTSUPP;
L
Linus Torvalds 已提交
1609 1610 1611 1612 1613 1614 1615
}
EXPORT_SYMBOL(ib_resize_cq);

/* Memory regions */

int ib_dereg_mr(struct ib_mr *mr)
{
1616
	struct ib_pd *pd = mr->pd;
1617
	struct ib_dm *dm = mr->dm;
L
Linus Torvalds 已提交
1618 1619
	int ret;

1620
	rdma_restrack_del(&mr->res);
L
Linus Torvalds 已提交
1621
	ret = mr->device->dereg_mr(mr);
1622
	if (!ret) {
L
Linus Torvalds 已提交
1623
		atomic_dec(&pd->usecnt);
1624 1625 1626
		if (dm)
			atomic_dec(&dm->usecnt);
	}
L
Linus Torvalds 已提交
1627 1628 1629 1630 1631

	return ret;
}
EXPORT_SYMBOL(ib_dereg_mr);

S
Sagi Grimberg 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
/**
 * ib_alloc_mr() - Allocates a memory region
 * @pd:            protection domain associated with the region
 * @mr_type:       memory region type
 * @max_num_sg:    maximum sg entries available for registration.
 *
 * Notes:
 * Memory registeration page/sg lists must not exceed max_num_sg.
 * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
 * max_num_sg * used_page_size.
 *
 */
struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
			  enum ib_mr_type mr_type,
			  u32 max_num_sg)
1647 1648 1649
{
	struct ib_mr *mr;

1650
	if (!pd->device->alloc_mr)
1651
		return ERR_PTR(-EOPNOTSUPP);
1652

1653
	mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
1654 1655 1656
	if (!IS_ERR(mr)) {
		mr->device  = pd->device;
		mr->pd      = pd;
1657
		mr->dm      = NULL;
1658 1659
		mr->uobject = NULL;
		atomic_inc(&pd->usecnt);
1660
		mr->need_inval = false;
1661 1662
		mr->res.type = RDMA_RESTRACK_MR;
		rdma_restrack_add(&mr->res);
1663 1664 1665 1666
	}

	return mr;
}
1667
EXPORT_SYMBOL(ib_alloc_mr);
1668

L
Linus Torvalds 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677
/* "Fast" memory regions */

struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
			    int mr_access_flags,
			    struct ib_fmr_attr *fmr_attr)
{
	struct ib_fmr *fmr;

	if (!pd->device->alloc_fmr)
1678
		return ERR_PTR(-EOPNOTSUPP);
L
Linus Torvalds 已提交
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718

	fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
	if (!IS_ERR(fmr)) {
		fmr->device = pd->device;
		fmr->pd     = pd;
		atomic_inc(&pd->usecnt);
	}

	return fmr;
}
EXPORT_SYMBOL(ib_alloc_fmr);

int ib_unmap_fmr(struct list_head *fmr_list)
{
	struct ib_fmr *fmr;

	if (list_empty(fmr_list))
		return 0;

	fmr = list_entry(fmr_list->next, struct ib_fmr, list);
	return fmr->device->unmap_fmr(fmr_list);
}
EXPORT_SYMBOL(ib_unmap_fmr);

int ib_dealloc_fmr(struct ib_fmr *fmr)
{
	struct ib_pd *pd;
	int ret;

	pd = fmr->pd;
	ret = fmr->device->dealloc_fmr(fmr);
	if (!ret)
		atomic_dec(&pd->usecnt);

	return ret;
}
EXPORT_SYMBOL(ib_dealloc_fmr);

/* Multicast groups */

1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
{
	struct ib_qp_init_attr init_attr = {};
	struct ib_qp_attr attr = {};
	int num_eth_ports = 0;
	int port;

	/* If QP state >= init, it is assigned to a port and we can check this
	 * port only.
	 */
	if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
		if (attr.qp_state >= IB_QPS_INIT) {
A
Alex Estrin 已提交
1731
			if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
1732 1733 1734 1735 1736 1737 1738 1739
			    IB_LINK_LAYER_INFINIBAND)
				return true;
			goto lid_check;
		}
	}

	/* Can't get a quick answer, iterate over all ports */
	for (port = 0; port < qp->device->phys_port_cnt; port++)
A
Alex Estrin 已提交
1740
		if (rdma_port_get_link_layer(qp->device, port) !=
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
		    IB_LINK_LAYER_INFINIBAND)
			num_eth_ports++;

	/* If we have at lease one Ethernet port, RoCE annex declares that
	 * multicast LID should be ignored. We can't tell at this step if the
	 * QP belongs to an IB or Ethernet port.
	 */
	if (num_eth_ports)
		return true;

	/* If all the ports are IB, we can check according to IB spec. */
lid_check:
	return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
		 lid == be16_to_cpu(IB_LID_PERMISSIVE));
}

L
Linus Torvalds 已提交
1757 1758
int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
{
1759 1760
	int ret;

1761
	if (!qp->device->attach_mcast)
1762
		return -EOPNOTSUPP;
1763 1764 1765

	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
1766 1767
		return -EINVAL;

1768 1769 1770 1771
	ret = qp->device->attach_mcast(qp, gid, lid);
	if (!ret)
		atomic_inc(&qp->usecnt);
	return ret;
L
Linus Torvalds 已提交
1772 1773 1774 1775 1776
}
EXPORT_SYMBOL(ib_attach_mcast);

int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
{
1777 1778
	int ret;

1779
	if (!qp->device->detach_mcast)
1780
		return -EOPNOTSUPP;
1781 1782 1783

	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
1784 1785
		return -EINVAL;

1786 1787 1788 1789
	ret = qp->device->detach_mcast(qp, gid, lid);
	if (!ret)
		atomic_dec(&qp->usecnt);
	return ret;
L
Linus Torvalds 已提交
1790 1791
}
EXPORT_SYMBOL(ib_detach_mcast);
S
Sean Hefty 已提交
1792

1793
struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller)
S
Sean Hefty 已提交
1794 1795 1796 1797
{
	struct ib_xrcd *xrcd;

	if (!device->alloc_xrcd)
1798
		return ERR_PTR(-EOPNOTSUPP);
S
Sean Hefty 已提交
1799 1800 1801 1802

	xrcd = device->alloc_xrcd(device, NULL, NULL);
	if (!IS_ERR(xrcd)) {
		xrcd->device = device;
1803
		xrcd->inode = NULL;
S
Sean Hefty 已提交
1804
		atomic_set(&xrcd->usecnt, 0);
1805 1806
		mutex_init(&xrcd->tgt_qp_mutex);
		INIT_LIST_HEAD(&xrcd->tgt_qp_list);
S
Sean Hefty 已提交
1807 1808 1809 1810
	}

	return xrcd;
}
1811
EXPORT_SYMBOL(__ib_alloc_xrcd);
S
Sean Hefty 已提交
1812 1813 1814

int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
{
1815 1816 1817
	struct ib_qp *qp;
	int ret;

S
Sean Hefty 已提交
1818 1819 1820
	if (atomic_read(&xrcd->usecnt))
		return -EBUSY;

1821 1822 1823 1824 1825 1826 1827
	while (!list_empty(&xrcd->tgt_qp_list)) {
		qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
		ret = ib_destroy_qp(qp);
		if (ret)
			return ret;
	}

S
Sean Hefty 已提交
1828 1829 1830
	return xrcd->device->dealloc_xrcd(xrcd);
}
EXPORT_SYMBOL(ib_dealloc_xrcd);
1831

1832 1833 1834 1835
/**
 * ib_create_wq - Creates a WQ associated with the specified protection
 * domain.
 * @pd: The protection domain associated with the WQ.
1836
 * @wq_attr: A list of initial attributes required to create the
1837 1838 1839
 * WQ. If WQ creation succeeds, then the attributes are updated to
 * the actual capabilities of the created WQ.
 *
1840
 * wq_attr->max_wr and wq_attr->max_sge determine
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
 * the requested size of the WQ, and set to the actual values allocated
 * on return.
 * If ib_create_wq() succeeds, then max_wr and max_sge will always be
 * at least as large as the requested values.
 */
struct ib_wq *ib_create_wq(struct ib_pd *pd,
			   struct ib_wq_init_attr *wq_attr)
{
	struct ib_wq *wq;

	if (!pd->device->create_wq)
1852
		return ERR_PTR(-EOPNOTSUPP);
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906

	wq = pd->device->create_wq(pd, wq_attr, NULL);
	if (!IS_ERR(wq)) {
		wq->event_handler = wq_attr->event_handler;
		wq->wq_context = wq_attr->wq_context;
		wq->wq_type = wq_attr->wq_type;
		wq->cq = wq_attr->cq;
		wq->device = pd->device;
		wq->pd = pd;
		wq->uobject = NULL;
		atomic_inc(&pd->usecnt);
		atomic_inc(&wq_attr->cq->usecnt);
		atomic_set(&wq->usecnt, 0);
	}
	return wq;
}
EXPORT_SYMBOL(ib_create_wq);

/**
 * ib_destroy_wq - Destroys the specified WQ.
 * @wq: The WQ to destroy.
 */
int ib_destroy_wq(struct ib_wq *wq)
{
	int err;
	struct ib_cq *cq = wq->cq;
	struct ib_pd *pd = wq->pd;

	if (atomic_read(&wq->usecnt))
		return -EBUSY;

	err = wq->device->destroy_wq(wq);
	if (!err) {
		atomic_dec(&pd->usecnt);
		atomic_dec(&cq->usecnt);
	}
	return err;
}
EXPORT_SYMBOL(ib_destroy_wq);

/**
 * ib_modify_wq - Modifies the specified WQ.
 * @wq: The WQ to modify.
 * @wq_attr: On input, specifies the WQ attributes to modify.
 * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
 *   are being modified.
 * On output, the current values of selected WQ attributes are returned.
 */
int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
		 u32 wq_attr_mask)
{
	int err;

	if (!wq->device->modify_wq)
1907
		return -EOPNOTSUPP;
1908 1909 1910 1911 1912 1913

	err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
	return err;
}
EXPORT_SYMBOL(ib_modify_wq);

1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
/*
 * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
 * @device: The device on which to create the rwq indirection table.
 * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
 * create the Indirection Table.
 *
 * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
 *	than the created ib_rwq_ind_table object and the caller is responsible
 *	for its memory allocation/free.
 */
struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
						 struct ib_rwq_ind_table_init_attr *init_attr)
{
	struct ib_rwq_ind_table *rwq_ind_table;
	int i;
	u32 table_size;

	if (!device->create_rwq_ind_table)
1932
		return ERR_PTR(-EOPNOTSUPP);
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975

	table_size = (1 << init_attr->log_ind_tbl_size);
	rwq_ind_table = device->create_rwq_ind_table(device,
				init_attr, NULL);
	if (IS_ERR(rwq_ind_table))
		return rwq_ind_table;

	rwq_ind_table->ind_tbl = init_attr->ind_tbl;
	rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
	rwq_ind_table->device = device;
	rwq_ind_table->uobject = NULL;
	atomic_set(&rwq_ind_table->usecnt, 0);

	for (i = 0; i < table_size; i++)
		atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);

	return rwq_ind_table;
}
EXPORT_SYMBOL(ib_create_rwq_ind_table);

/*
 * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
 * @wq_ind_table: The Indirection Table to destroy.
*/
int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
{
	int err, i;
	u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
	struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;

	if (atomic_read(&rwq_ind_table->usecnt))
		return -EBUSY;

	err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
	if (!err) {
		for (i = 0; i < table_size; i++)
			atomic_dec(&ind_tbl[i]->usecnt);
	}

	return err;
}
EXPORT_SYMBOL(ib_destroy_rwq_ind_table);

1976 1977 1978 1979 1980 1981
struct ib_flow *ib_create_flow(struct ib_qp *qp,
			       struct ib_flow_attr *flow_attr,
			       int domain)
{
	struct ib_flow *flow_id;
	if (!qp->device->create_flow)
1982
		return ERR_PTR(-EOPNOTSUPP);
1983

1984
	flow_id = qp->device->create_flow(qp, flow_attr, domain, NULL);
M
Mark Bloch 已提交
1985
	if (!IS_ERR(flow_id)) {
1986
		atomic_inc(&qp->usecnt);
M
Mark Bloch 已提交
1987 1988
		flow_id->qp = qp;
	}
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
	return flow_id;
}
EXPORT_SYMBOL(ib_create_flow);

int ib_destroy_flow(struct ib_flow *flow_id)
{
	int err;
	struct ib_qp *qp = flow_id->qp;

	err = qp->device->destroy_flow(flow_id);
	if (!err)
		atomic_dec(&qp->usecnt);
	return err;
}
EXPORT_SYMBOL(ib_destroy_flow);
2004 2005 2006 2007 2008

int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
		       struct ib_mr_status *mr_status)
{
	return mr->device->check_mr_status ?
2009
		mr->device->check_mr_status(mr, check_mask, mr_status) : -EOPNOTSUPP;
2010 2011
}
EXPORT_SYMBOL(ib_check_mr_status);
2012

2013 2014 2015 2016
int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
			 int state)
{
	if (!device->set_vf_link_state)
2017
		return -EOPNOTSUPP;
2018 2019 2020 2021 2022 2023 2024 2025 2026

	return device->set_vf_link_state(device, vf, port, state);
}
EXPORT_SYMBOL(ib_set_vf_link_state);

int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
		     struct ifla_vf_info *info)
{
	if (!device->get_vf_config)
2027
		return -EOPNOTSUPP;
2028 2029 2030 2031 2032 2033 2034 2035 2036

	return device->get_vf_config(device, vf, port, info);
}
EXPORT_SYMBOL(ib_get_vf_config);

int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
		    struct ifla_vf_stats *stats)
{
	if (!device->get_vf_stats)
2037
		return -EOPNOTSUPP;
2038 2039 2040 2041 2042 2043 2044 2045 2046

	return device->get_vf_stats(device, vf, port, stats);
}
EXPORT_SYMBOL(ib_get_vf_stats);

int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
		   int type)
{
	if (!device->set_vf_guid)
2047
		return -EOPNOTSUPP;
2048 2049 2050 2051 2052

	return device->set_vf_guid(device, vf, port, guid, type);
}
EXPORT_SYMBOL(ib_set_vf_guid);

2053 2054 2055 2056 2057 2058
/**
 * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
 *     and set it the memory region.
 * @mr:            memory region
 * @sg:            dma mapped scatterlist
 * @sg_nents:      number of entries in sg
2059
 * @sg_offset:     offset in bytes into sg
2060 2061 2062 2063
 * @page_size:     page vector desired page size
 *
 * Constraints:
 * - The first sg element is allowed to have an offset.
2064 2065 2066
 * - Each sg element must either be aligned to page_size or virtually
 *   contiguous to the previous element. In case an sg element has a
 *   non-contiguous offset, the mapping prefix will not include it.
2067 2068 2069
 * - The last sg element is allowed to have length less than page_size.
 * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
 *   then only max_num_sg entries will be mapped.
2070
 * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
2071
 *   constraints holds and the page_size argument is ignored.
2072 2073 2074 2075 2076 2077
 *
 * Returns the number of sg elements that were mapped to the memory region.
 *
 * After this completes successfully, the  memory region
 * is ready for registration.
 */
2078
int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
2079
		 unsigned int *sg_offset, unsigned int page_size)
2080 2081
{
	if (unlikely(!mr->device->map_mr_sg))
2082
		return -EOPNOTSUPP;
2083 2084 2085

	mr->page_size = page_size;

2086
	return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
2087 2088 2089 2090 2091 2092 2093 2094 2095
}
EXPORT_SYMBOL(ib_map_mr_sg);

/**
 * ib_sg_to_pages() - Convert the largest prefix of a sg list
 *     to a page vector
 * @mr:            memory region
 * @sgl:           dma mapped scatterlist
 * @sg_nents:      number of entries in sg
2096 2097 2098 2099
 * @sg_offset_p:   IN:  start offset in bytes into sg
 *                 OUT: offset in bytes for element n of the sg of the first
 *                      byte that has not been processed where n is the return
 *                      value of this function.
2100 2101
 * @set_page:      driver page assignment function pointer
 *
B
Bart Van Assche 已提交
2102
 * Core service helper for drivers to convert the largest
2103 2104 2105 2106 2107 2108 2109
 * prefix of given sg list to a page vector. The sg list
 * prefix converted is the prefix that meet the requirements
 * of ib_map_mr_sg.
 *
 * Returns the number of sg elements that were assigned to
 * a page vector.
 */
2110
int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
2111
		unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
2112 2113
{
	struct scatterlist *sg;
2114
	u64 last_end_dma_addr = 0;
2115
	unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2116 2117
	unsigned int last_page_off = 0;
	u64 page_mask = ~((u64)mr->page_size - 1);
B
Bart Van Assche 已提交
2118
	int i, ret;
2119

2120 2121 2122
	if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
		return -EINVAL;

2123
	mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
2124 2125 2126
	mr->length = 0;

	for_each_sg(sgl, sg, sg_nents, i) {
2127
		u64 dma_addr = sg_dma_address(sg) + sg_offset;
2128
		u64 prev_addr = dma_addr;
2129
		unsigned int dma_len = sg_dma_len(sg) - sg_offset;
2130 2131 2132
		u64 end_dma_addr = dma_addr + dma_len;
		u64 page_addr = dma_addr & page_mask;

B
Bart Van Assche 已提交
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
		/*
		 * For the second and later elements, check whether either the
		 * end of element i-1 or the start of element i is not aligned
		 * on a page boundary.
		 */
		if (i && (last_page_off != 0 || page_addr != dma_addr)) {
			/* Stop mapping if there is a gap. */
			if (last_end_dma_addr != dma_addr)
				break;

			/*
			 * Coalesce this element with the last. If it is small
			 * enough just update mr->length. Otherwise start
			 * mapping from the next page.
			 */
			goto next_page;
2149 2150 2151
		}

		do {
B
Bart Van Assche 已提交
2152
			ret = set_page(mr, page_addr);
2153 2154 2155 2156 2157 2158 2159 2160
			if (unlikely(ret < 0)) {
				sg_offset = prev_addr - sg_dma_address(sg);
				mr->length += prev_addr - dma_addr;
				if (sg_offset_p)
					*sg_offset_p = sg_offset;
				return i || sg_offset ? i : ret;
			}
			prev_addr = page_addr;
B
Bart Van Assche 已提交
2161
next_page:
2162 2163 2164 2165 2166 2167
			page_addr += mr->page_size;
		} while (page_addr < end_dma_addr);

		mr->length += dma_len;
		last_end_dma_addr = end_dma_addr;
		last_page_off = end_dma_addr & ~page_mask;
2168 2169

		sg_offset = 0;
2170 2171
	}

2172 2173
	if (sg_offset_p)
		*sg_offset_p = 0;
2174 2175 2176
	return i;
}
EXPORT_SYMBOL(ib_sg_to_pages);
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195

struct ib_drain_cqe {
	struct ib_cqe cqe;
	struct completion done;
};

static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
{
	struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
						cqe);

	complete(&cqe->done);
}

/*
 * Post a WR and block until its completion is reaped for the SQ.
 */
static void __ib_drain_sq(struct ib_qp *qp)
{
2196
	struct ib_cq *cq = qp->send_cq;
2197 2198
	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
	struct ib_drain_cqe sdrain;
2199 2200 2201
	struct ib_send_wr *bad_swr;
	struct ib_rdma_wr swr = {
		.wr = {
2202 2203
			.next = NULL,
			{ .wr_cqe	= &sdrain.cqe, },
2204 2205 2206
			.opcode	= IB_WR_RDMA_WRITE,
		},
	};
2207 2208 2209 2210 2211 2212 2213 2214
	int ret;

	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
	if (ret) {
		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
		return;
	}

2215 2216 2217
	sdrain.cqe.done = ib_drain_qp_done;
	init_completion(&sdrain.done);

2218
	ret = ib_post_send(qp, &swr.wr, &bad_swr);
2219 2220 2221 2222 2223
	if (ret) {
		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
		return;
	}

2224 2225 2226 2227 2228
	if (cq->poll_ctx == IB_POLL_DIRECT)
		while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
			ib_process_cq_direct(cq, -1);
	else
		wait_for_completion(&sdrain.done);
2229 2230 2231 2232 2233 2234 2235
}

/*
 * Post a WR and block until its completion is reaped for the RQ.
 */
static void __ib_drain_rq(struct ib_qp *qp)
{
2236
	struct ib_cq *cq = qp->recv_cq;
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
	struct ib_drain_cqe rdrain;
	struct ib_recv_wr rwr = {}, *bad_rwr;
	int ret;

	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
	if (ret) {
		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
		return;
	}

2248 2249 2250 2251
	rwr.wr_cqe = &rdrain.cqe;
	rdrain.cqe.done = ib_drain_qp_done;
	init_completion(&rdrain.done);

2252 2253 2254 2255 2256 2257
	ret = ib_post_recv(qp, &rwr, &bad_rwr);
	if (ret) {
		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
		return;
	}

2258 2259 2260 2261 2262
	if (cq->poll_ctx == IB_POLL_DIRECT)
		while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
			ib_process_cq_direct(cq, -1);
	else
		wait_for_completion(&rdrain.done);
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
}

/**
 * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
 *		   application.
 * @qp:            queue pair to drain
 *
 * If the device has a provider-specific drain function, then
 * call that.  Otherwise call the generic drain function
 * __ib_drain_sq().
 *
 * The caller must:
 *
 * ensure there is room in the CQ and SQ for the drain work request and
 * completion.
 *
2279
 * allocate the CQ using ib_alloc_cq().
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
 *
 * ensure that there are no other contexts that are posting WRs concurrently.
 * Otherwise the drain is not guaranteed.
 */
void ib_drain_sq(struct ib_qp *qp)
{
	if (qp->device->drain_sq)
		qp->device->drain_sq(qp);
	else
		__ib_drain_sq(qp);
}
EXPORT_SYMBOL(ib_drain_sq);

/**
 * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
 *		   application.
 * @qp:            queue pair to drain
 *
 * If the device has a provider-specific drain function, then
 * call that.  Otherwise call the generic drain function
 * __ib_drain_rq().
 *
 * The caller must:
 *
 * ensure there is room in the CQ and RQ for the drain work request and
 * completion.
 *
2307
 * allocate the CQ using ib_alloc_cq().
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
 *
 * ensure that there are no other contexts that are posting WRs concurrently.
 * Otherwise the drain is not guaranteed.
 */
void ib_drain_rq(struct ib_qp *qp)
{
	if (qp->device->drain_rq)
		qp->device->drain_rq(qp);
	else
		__ib_drain_rq(qp);
}
EXPORT_SYMBOL(ib_drain_rq);

/**
 * ib_drain_qp() - Block until all CQEs have been consumed by the
 *		   application on both the RQ and SQ.
 * @qp:            queue pair to drain
 *
 * The caller must:
 *
 * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
 * and completions.
 *
2331
 * allocate the CQs using ib_alloc_cq().
2332 2333 2334 2335 2336 2337 2338
 *
 * ensure that there are no other contexts that are posting WRs concurrently.
 * Otherwise the drain is not guaranteed.
 */
void ib_drain_qp(struct ib_qp *qp)
{
	ib_drain_sq(qp);
2339 2340
	if (!qp->srq)
		ib_drain_rq(qp);
2341 2342
}
EXPORT_SYMBOL(ib_drain_qp);