Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *   Copyright (C) 2017, Microsoft Corporation.
   4 *
   5 *   Author(s): Long Li <longli@microsoft.com>
   6 */
   7#include <linux/module.h>
   8#include <linux/highmem.h>
   9#include "smbdirect.h"
  10#include "cifs_debug.h"
  11#include "cifsproto.h"
  12#include "smb2proto.h"
  13
  14static struct smbd_response *get_empty_queue_buffer(
  15		struct smbd_connection *info);
  16static struct smbd_response *get_receive_buffer(
  17		struct smbd_connection *info);
  18static void put_receive_buffer(
  19		struct smbd_connection *info,
  20		struct smbd_response *response);
  21static int allocate_receive_buffers(struct smbd_connection *info, int num_buf);
  22static void destroy_receive_buffers(struct smbd_connection *info);
  23
  24static void put_empty_packet(
  25		struct smbd_connection *info, struct smbd_response *response);
  26static void enqueue_reassembly(
  27		struct smbd_connection *info,
  28		struct smbd_response *response, int data_length);
  29static struct smbd_response *_get_first_reassembly(
  30		struct smbd_connection *info);
  31
  32static int smbd_post_recv(
  33		struct smbd_connection *info,
  34		struct smbd_response *response);
  35
  36static int smbd_post_send_empty(struct smbd_connection *info);
  37static int smbd_post_send_data(
  38		struct smbd_connection *info,
  39		struct kvec *iov, int n_vec, int remaining_data_length);
  40static int smbd_post_send_page(struct smbd_connection *info,
  41		struct page *page, unsigned long offset,
  42		size_t size, int remaining_data_length);
  43
  44static void destroy_mr_list(struct smbd_connection *info);
  45static int allocate_mr_list(struct smbd_connection *info);
  46
  47/* SMBD version number */
  48#define SMBD_V1	0x0100
  49
  50/* Port numbers for SMBD transport */
  51#define SMB_PORT	445
  52#define SMBD_PORT	5445
  53
  54/* Address lookup and resolve timeout in ms */
  55#define RDMA_RESOLVE_TIMEOUT	5000
  56
  57/* SMBD negotiation timeout in seconds */
  58#define SMBD_NEGOTIATE_TIMEOUT	120
  59
  60/* SMBD minimum receive size and fragmented sized defined in [MS-SMBD] */
  61#define SMBD_MIN_RECEIVE_SIZE		128
  62#define SMBD_MIN_FRAGMENTED_SIZE	131072
  63
  64/*
  65 * Default maximum number of RDMA read/write outstanding on this connection
  66 * This value is possibly decreased during QP creation on hardware limit
  67 */
  68#define SMBD_CM_RESPONDER_RESOURCES	32
  69
  70/* Maximum number of retries on data transfer operations */
  71#define SMBD_CM_RETRY			6
  72/* No need to retry on Receiver Not Ready since SMBD manages credits */
  73#define SMBD_CM_RNR_RETRY		0
  74
  75/*
  76 * User configurable initial values per SMBD transport connection
  77 * as defined in [MS-SMBD] 3.1.1.1
  78 * Those may change after a SMBD negotiation
  79 */
  80/* The local peer's maximum number of credits to grant to the peer */
  81int smbd_receive_credit_max = 255;
  82
  83/* The remote peer's credit request of local peer */
  84int smbd_send_credit_target = 255;
  85
  86/* The maximum single message size can be sent to remote peer */
  87int smbd_max_send_size = 1364;
  88
  89/*  The maximum fragmented upper-layer payload receive size supported */
  90int smbd_max_fragmented_recv_size = 1024 * 1024;
  91
  92/*  The maximum single-message size which can be received */
  93int smbd_max_receive_size = 1364;
  94
  95/* The timeout to initiate send of a keepalive message on idle */
  96int smbd_keep_alive_interval = 120;
  97
  98/*
  99 * User configurable initial values for RDMA transport
 100 * The actual values used may be lower and are limited to hardware capabilities
 101 */
 102/* Default maximum number of pages in a single RDMA write/read */
 103int smbd_max_frmr_depth = 2048;
 104
 105/* If payload is less than this byte, use RDMA send/recv not read/write */
 106int rdma_readwrite_threshold = 4096;
 107
 108/* Transport logging functions
 109 * Logging are defined as classes. They can be OR'ed to define the actual
 110 * logging level via module parameter smbd_logging_class
 111 * e.g. cifs.smbd_logging_class=0xa0 will log all log_rdma_recv() and
 112 * log_rdma_event()
 113 */
 114#define LOG_OUTGOING			0x1
 115#define LOG_INCOMING			0x2
 116#define LOG_READ			0x4
 117#define LOG_WRITE			0x8
 118#define LOG_RDMA_SEND			0x10
 119#define LOG_RDMA_RECV			0x20
 120#define LOG_KEEP_ALIVE			0x40
 121#define LOG_RDMA_EVENT			0x80
 122#define LOG_RDMA_MR			0x100
 123static unsigned int smbd_logging_class;
 124module_param(smbd_logging_class, uint, 0644);
 125MODULE_PARM_DESC(smbd_logging_class,
 126	"Logging class for SMBD transport 0x0 to 0x100");
 127
 128#define ERR		0x0
 129#define INFO		0x1
 130static unsigned int smbd_logging_level = ERR;
 131module_param(smbd_logging_level, uint, 0644);
 132MODULE_PARM_DESC(smbd_logging_level,
 133	"Logging level for SMBD transport, 0 (default): error, 1: info");
 134
 135#define log_rdma(level, class, fmt, args...)				\
 136do {									\
 137	if (level <= smbd_logging_level || class & smbd_logging_class)	\
 138		cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
 139} while (0)
 140
 141#define log_outgoing(level, fmt, args...) \
 142		log_rdma(level, LOG_OUTGOING, fmt, ##args)
 143#define log_incoming(level, fmt, args...) \
 144		log_rdma(level, LOG_INCOMING, fmt, ##args)
 145#define log_read(level, fmt, args...)	log_rdma(level, LOG_READ, fmt, ##args)
 146#define log_write(level, fmt, args...)	log_rdma(level, LOG_WRITE, fmt, ##args)
 147#define log_rdma_send(level, fmt, args...) \
 148		log_rdma(level, LOG_RDMA_SEND, fmt, ##args)
 149#define log_rdma_recv(level, fmt, args...) \
 150		log_rdma(level, LOG_RDMA_RECV, fmt, ##args)
 151#define log_keep_alive(level, fmt, args...) \
 152		log_rdma(level, LOG_KEEP_ALIVE, fmt, ##args)
 153#define log_rdma_event(level, fmt, args...) \
 154		log_rdma(level, LOG_RDMA_EVENT, fmt, ##args)
 155#define log_rdma_mr(level, fmt, args...) \
 156		log_rdma(level, LOG_RDMA_MR, fmt, ##args)
 157
 158static void smbd_disconnect_rdma_work(struct work_struct *work)
 159{
 160	struct smbd_connection *info =
 161		container_of(work, struct smbd_connection, disconnect_work);
 162
 163	if (info->transport_status == SMBD_CONNECTED) {
 164		info->transport_status = SMBD_DISCONNECTING;
 165		rdma_disconnect(info->id);
 166	}
 167}
 168
 169static void smbd_disconnect_rdma_connection(struct smbd_connection *info)
 170{
 171	queue_work(info->workqueue, &info->disconnect_work);
 172}
 173
 174/* Upcall from RDMA CM */
 175static int smbd_conn_upcall(
 176		struct rdma_cm_id *id, struct rdma_cm_event *event)
 177{
 178	struct smbd_connection *info = id->context;
 179
 180	log_rdma_event(INFO, "event=%d status=%d\n",
 181		event->event, event->status);
 182
 183	switch (event->event) {
 184	case RDMA_CM_EVENT_ADDR_RESOLVED:
 185	case RDMA_CM_EVENT_ROUTE_RESOLVED:
 186		info->ri_rc = 0;
 187		complete(&info->ri_done);
 188		break;
 189
 190	case RDMA_CM_EVENT_ADDR_ERROR:
 191		info->ri_rc = -EHOSTUNREACH;
 192		complete(&info->ri_done);
 193		break;
 194
 195	case RDMA_CM_EVENT_ROUTE_ERROR:
 196		info->ri_rc = -ENETUNREACH;
 197		complete(&info->ri_done);
 198		break;
 199
 200	case RDMA_CM_EVENT_ESTABLISHED:
 201		log_rdma_event(INFO, "connected event=%d\n", event->event);
 202		info->transport_status = SMBD_CONNECTED;
 203		wake_up_interruptible(&info->conn_wait);
 204		break;
 205
 206	case RDMA_CM_EVENT_CONNECT_ERROR:
 207	case RDMA_CM_EVENT_UNREACHABLE:
 208	case RDMA_CM_EVENT_REJECTED:
 209		log_rdma_event(INFO, "connecting failed event=%d\n", event->event);
 210		info->transport_status = SMBD_DISCONNECTED;
 211		wake_up_interruptible(&info->conn_wait);
 212		break;
 213
 214	case RDMA_CM_EVENT_DEVICE_REMOVAL:
 215	case RDMA_CM_EVENT_DISCONNECTED:
 216		/* This happenes when we fail the negotiation */
 217		if (info->transport_status == SMBD_NEGOTIATE_FAILED) {
 218			info->transport_status = SMBD_DISCONNECTED;
 219			wake_up(&info->conn_wait);
 220			break;
 221		}
 222
 223		info->transport_status = SMBD_DISCONNECTED;
 224		wake_up_interruptible(&info->disconn_wait);
 225		wake_up_interruptible(&info->wait_reassembly_queue);
 226		wake_up_interruptible_all(&info->wait_send_queue);
 227		break;
 228
 229	default:
 230		break;
 231	}
 232
 233	return 0;
 234}
 235
 236/* Upcall from RDMA QP */
 237static void
 238smbd_qp_async_error_upcall(struct ib_event *event, void *context)
 239{
 240	struct smbd_connection *info = context;
 241
 242	log_rdma_event(ERR, "%s on device %s info %p\n",
 243		ib_event_msg(event->event), event->device->name, info);
 244
 245	switch (event->event) {
 246	case IB_EVENT_CQ_ERR:
 247	case IB_EVENT_QP_FATAL:
 248		smbd_disconnect_rdma_connection(info);
 249		break;
 250
 251	default:
 252		break;
 253	}
 254}
 255
 256static inline void *smbd_request_payload(struct smbd_request *request)
 257{
 258	return (void *)request->packet;
 259}
 260
 261static inline void *smbd_response_payload(struct smbd_response *response)
 262{
 263	return (void *)response->packet;
 264}
 265
 266/* Called when a RDMA send is done */
 267static void send_done(struct ib_cq *cq, struct ib_wc *wc)
 268{
 269	int i;
 270	struct smbd_request *request =
 271		container_of(wc->wr_cqe, struct smbd_request, cqe);
 272
 273	log_rdma_send(INFO, "smbd_request 0x%p completed wc->status=%d\n",
 274		request, wc->status);
 275
 276	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
 277		log_rdma_send(ERR, "wc->status=%d wc->opcode=%d\n",
 278			wc->status, wc->opcode);
 279		smbd_disconnect_rdma_connection(request->info);
 280	}
 281
 282	for (i = 0; i < request->num_sge; i++)
 283		ib_dma_unmap_single(request->info->id->device,
 284			request->sge[i].addr,
 285			request->sge[i].length,
 286			DMA_TO_DEVICE);
 287
 288	if (atomic_dec_and_test(&request->info->send_pending))
 289		wake_up(&request->info->wait_send_pending);
 290
 291	wake_up(&request->info->wait_post_send);
 
 
 
 292
 293	mempool_free(request, request->info->request_mempool);
 294}
 295
 296static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
 297{
 298	log_rdma_event(INFO, "resp message min_version %u max_version %u negotiated_version %u credits_requested %u credits_granted %u status %u max_readwrite_size %u preferred_send_size %u max_receive_size %u max_fragmented_size %u\n",
 299		       resp->min_version, resp->max_version,
 300		       resp->negotiated_version, resp->credits_requested,
 301		       resp->credits_granted, resp->status,
 302		       resp->max_readwrite_size, resp->preferred_send_size,
 303		       resp->max_receive_size, resp->max_fragmented_size);
 
 
 
 304}
 305
 306/*
 307 * Process a negotiation response message, according to [MS-SMBD]3.1.5.7
 308 * response, packet_length: the negotiation response message
 309 * return value: true if negotiation is a success, false if failed
 310 */
 311static bool process_negotiation_response(
 312		struct smbd_response *response, int packet_length)
 313{
 314	struct smbd_connection *info = response->info;
 315	struct smbd_negotiate_resp *packet = smbd_response_payload(response);
 316
 317	if (packet_length < sizeof(struct smbd_negotiate_resp)) {
 318		log_rdma_event(ERR,
 319			"error: packet_length=%d\n", packet_length);
 320		return false;
 321	}
 322
 323	if (le16_to_cpu(packet->negotiated_version) != SMBD_V1) {
 324		log_rdma_event(ERR, "error: negotiated_version=%x\n",
 325			le16_to_cpu(packet->negotiated_version));
 326		return false;
 327	}
 328	info->protocol = le16_to_cpu(packet->negotiated_version);
 329
 330	if (packet->credits_requested == 0) {
 331		log_rdma_event(ERR, "error: credits_requested==0\n");
 332		return false;
 333	}
 334	info->receive_credit_target = le16_to_cpu(packet->credits_requested);
 335
 336	if (packet->credits_granted == 0) {
 337		log_rdma_event(ERR, "error: credits_granted==0\n");
 338		return false;
 339	}
 340	atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
 341
 342	atomic_set(&info->receive_credits, 0);
 343
 344	if (le32_to_cpu(packet->preferred_send_size) > info->max_receive_size) {
 345		log_rdma_event(ERR, "error: preferred_send_size=%d\n",
 346			le32_to_cpu(packet->preferred_send_size));
 347		return false;
 348	}
 349	info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
 350
 351	if (le32_to_cpu(packet->max_receive_size) < SMBD_MIN_RECEIVE_SIZE) {
 352		log_rdma_event(ERR, "error: max_receive_size=%d\n",
 353			le32_to_cpu(packet->max_receive_size));
 354		return false;
 355	}
 356	info->max_send_size = min_t(int, info->max_send_size,
 357					le32_to_cpu(packet->max_receive_size));
 358
 359	if (le32_to_cpu(packet->max_fragmented_size) <
 360			SMBD_MIN_FRAGMENTED_SIZE) {
 361		log_rdma_event(ERR, "error: max_fragmented_size=%d\n",
 362			le32_to_cpu(packet->max_fragmented_size));
 363		return false;
 364	}
 365	info->max_fragmented_send_size =
 366		le32_to_cpu(packet->max_fragmented_size);
 367	info->rdma_readwrite_threshold =
 368		rdma_readwrite_threshold > info->max_fragmented_send_size ?
 369		info->max_fragmented_send_size :
 370		rdma_readwrite_threshold;
 371
 372
 373	info->max_readwrite_size = min_t(u32,
 374			le32_to_cpu(packet->max_readwrite_size),
 375			info->max_frmr_depth * PAGE_SIZE);
 376	info->max_frmr_depth = info->max_readwrite_size / PAGE_SIZE;
 377
 378	return true;
 379}
 380
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 381static void smbd_post_send_credits(struct work_struct *work)
 382{
 383	int ret = 0;
 384	int use_receive_queue = 1;
 385	int rc;
 386	struct smbd_response *response;
 387	struct smbd_connection *info =
 388		container_of(work, struct smbd_connection,
 389			post_send_credits_work);
 390
 391	if (info->transport_status != SMBD_CONNECTED) {
 392		wake_up(&info->wait_receive_queues);
 393		return;
 394	}
 395
 396	if (info->receive_credit_target >
 397		atomic_read(&info->receive_credits)) {
 398		while (true) {
 399			if (use_receive_queue)
 400				response = get_receive_buffer(info);
 401			else
 402				response = get_empty_queue_buffer(info);
 403			if (!response) {
 404				/* now switch to emtpy packet queue */
 405				if (use_receive_queue) {
 406					use_receive_queue = 0;
 407					continue;
 408				} else
 409					break;
 410			}
 411
 412			response->type = SMBD_TRANSFER_DATA;
 413			response->first_segment = false;
 414			rc = smbd_post_recv(info, response);
 415			if (rc) {
 416				log_rdma_recv(ERR,
 417					"post_recv failed rc=%d\n", rc);
 418				put_receive_buffer(info, response);
 419				break;
 420			}
 421
 422			ret++;
 423		}
 424	}
 425
 426	spin_lock(&info->lock_new_credits_offered);
 427	info->new_credits_offered += ret;
 428	spin_unlock(&info->lock_new_credits_offered);
 429
 430	/* Promptly send an immediate packet as defined in [MS-SMBD] 3.1.1.1 */
 431	info->send_immediate = true;
 432	if (atomic_read(&info->receive_credits) <
 433		info->receive_credit_target - 1) {
 434		if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
 435		    info->send_immediate) {
 436			log_keep_alive(INFO, "send an empty message\n");
 437			smbd_post_send_empty(info);
 438		}
 439	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 440}
 441
 442/* Called from softirq, when recv is done */
 443static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 444{
 445	struct smbd_data_transfer *data_transfer;
 446	struct smbd_response *response =
 447		container_of(wc->wr_cqe, struct smbd_response, cqe);
 448	struct smbd_connection *info = response->info;
 449	int data_length = 0;
 450
 451	log_rdma_recv(INFO, "response=0x%p type=%d wc status=%d wc opcode %d byte_len=%d pkey_index=%u\n",
 452		      response, response->type, wc->status, wc->opcode,
 453		      wc->byte_len, wc->pkey_index);
 
 454
 455	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
 456		log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
 457			wc->status, wc->opcode);
 458		smbd_disconnect_rdma_connection(info);
 459		goto error;
 460	}
 461
 462	ib_dma_sync_single_for_cpu(
 463		wc->qp->device,
 464		response->sge.addr,
 465		response->sge.length,
 466		DMA_FROM_DEVICE);
 467
 468	switch (response->type) {
 469	/* SMBD negotiation response */
 470	case SMBD_NEGOTIATE_RESP:
 471		dump_smbd_negotiate_resp(smbd_response_payload(response));
 472		info->full_packet_received = true;
 473		info->negotiate_done =
 474			process_negotiation_response(response, wc->byte_len);
 475		complete(&info->negotiate_completion);
 476		break;
 477
 478	/* SMBD data transfer packet */
 479	case SMBD_TRANSFER_DATA:
 480		data_transfer = smbd_response_payload(response);
 481		data_length = le32_to_cpu(data_transfer->data_length);
 482
 483		/*
 484		 * If this is a packet with data playload place the data in
 485		 * reassembly queue and wake up the reading thread
 486		 */
 487		if (data_length) {
 488			if (info->full_packet_received)
 489				response->first_segment = true;
 490
 491			if (le32_to_cpu(data_transfer->remaining_data_length))
 492				info->full_packet_received = false;
 493			else
 494				info->full_packet_received = true;
 495
 496			enqueue_reassembly(
 497				info,
 498				response,
 499				data_length);
 500		} else
 501			put_empty_packet(info, response);
 502
 503		if (data_length)
 504			wake_up_interruptible(&info->wait_reassembly_queue);
 505
 506		atomic_dec(&info->receive_credits);
 507		info->receive_credit_target =
 508			le16_to_cpu(data_transfer->credits_requested);
 509		if (le16_to_cpu(data_transfer->credits_granted)) {
 510			atomic_add(le16_to_cpu(data_transfer->credits_granted),
 511				&info->send_credits);
 512			/*
 513			 * We have new send credits granted from remote peer
 514			 * If any sender is waiting for credits, unblock it
 515			 */
 516			wake_up_interruptible(&info->wait_send_queue);
 517		}
 518
 519		log_incoming(INFO, "data flags %d data_offset %d data_length %d remaining_data_length %d\n",
 520			     le16_to_cpu(data_transfer->flags),
 521			     le32_to_cpu(data_transfer->data_offset),
 522			     le32_to_cpu(data_transfer->data_length),
 523			     le32_to_cpu(data_transfer->remaining_data_length));
 
 524
 525		/* Send a KEEP_ALIVE response right away if requested */
 526		info->keep_alive_requested = KEEP_ALIVE_NONE;
 527		if (le16_to_cpu(data_transfer->flags) &
 528				SMB_DIRECT_RESPONSE_REQUESTED) {
 529			info->keep_alive_requested = KEEP_ALIVE_PENDING;
 530		}
 531
 
 532		return;
 533
 534	default:
 535		log_rdma_recv(ERR,
 536			"unexpected response type=%d\n", response->type);
 537	}
 538
 539error:
 540	put_receive_buffer(info, response);
 541}
 542
 543static struct rdma_cm_id *smbd_create_id(
 544		struct smbd_connection *info,
 545		struct sockaddr *dstaddr, int port)
 546{
 547	struct rdma_cm_id *id;
 548	int rc;
 549	__be16 *sport;
 550
 551	id = rdma_create_id(&init_net, smbd_conn_upcall, info,
 552		RDMA_PS_TCP, IB_QPT_RC);
 553	if (IS_ERR(id)) {
 554		rc = PTR_ERR(id);
 555		log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
 556		return id;
 557	}
 558
 559	if (dstaddr->sa_family == AF_INET6)
 560		sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
 561	else
 562		sport = &((struct sockaddr_in *)dstaddr)->sin_port;
 563
 564	*sport = htons(port);
 565
 566	init_completion(&info->ri_done);
 567	info->ri_rc = -ETIMEDOUT;
 568
 569	rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
 570		RDMA_RESOLVE_TIMEOUT);
 571	if (rc) {
 572		log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
 573		goto out;
 574	}
 575	rc = wait_for_completion_interruptible_timeout(
 576		&info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
 577	/* e.g. if interrupted returns -ERESTARTSYS */
 578	if (rc < 0) {
 579		log_rdma_event(ERR, "rdma_resolve_addr timeout rc: %i\n", rc);
 580		goto out;
 581	}
 582	rc = info->ri_rc;
 583	if (rc) {
 584		log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
 585		goto out;
 586	}
 587
 588	info->ri_rc = -ETIMEDOUT;
 589	rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
 590	if (rc) {
 591		log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
 592		goto out;
 593	}
 594	rc = wait_for_completion_interruptible_timeout(
 595		&info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
 596	/* e.g. if interrupted returns -ERESTARTSYS */
 597	if (rc < 0)  {
 598		log_rdma_event(ERR, "rdma_resolve_addr timeout rc: %i\n", rc);
 599		goto out;
 600	}
 601	rc = info->ri_rc;
 602	if (rc) {
 603		log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
 604		goto out;
 605	}
 606
 607	return id;
 608
 609out:
 610	rdma_destroy_id(id);
 611	return ERR_PTR(rc);
 612}
 613
 614/*
 615 * Test if FRWR (Fast Registration Work Requests) is supported on the device
 616 * This implementation requries FRWR on RDMA read/write
 617 * return value: true if it is supported
 618 */
 619static bool frwr_is_supported(struct ib_device_attr *attrs)
 620{
 621	if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
 622		return false;
 623	if (attrs->max_fast_reg_page_list_len == 0)
 624		return false;
 625	return true;
 626}
 627
 628static int smbd_ia_open(
 629		struct smbd_connection *info,
 630		struct sockaddr *dstaddr, int port)
 631{
 632	int rc;
 633
 634	info->id = smbd_create_id(info, dstaddr, port);
 635	if (IS_ERR(info->id)) {
 636		rc = PTR_ERR(info->id);
 637		goto out1;
 638	}
 639
 640	if (!frwr_is_supported(&info->id->device->attrs)) {
 641		log_rdma_event(ERR, "Fast Registration Work Requests (FRWR) is not supported\n");
 642		log_rdma_event(ERR, "Device capability flags = %llx max_fast_reg_page_list_len = %u\n",
 643			       info->id->device->attrs.device_cap_flags,
 644			       info->id->device->attrs.max_fast_reg_page_list_len);
 
 
 
 
 645		rc = -EPROTONOSUPPORT;
 646		goto out2;
 647	}
 648	info->max_frmr_depth = min_t(int,
 649		smbd_max_frmr_depth,
 650		info->id->device->attrs.max_fast_reg_page_list_len);
 651	info->mr_type = IB_MR_TYPE_MEM_REG;
 652	if (info->id->device->attrs.kernel_cap_flags & IBK_SG_GAPS_REG)
 653		info->mr_type = IB_MR_TYPE_SG_GAPS;
 654
 655	info->pd = ib_alloc_pd(info->id->device, 0);
 656	if (IS_ERR(info->pd)) {
 657		rc = PTR_ERR(info->pd);
 658		log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
 659		goto out2;
 660	}
 661
 662	return 0;
 663
 664out2:
 665	rdma_destroy_id(info->id);
 666	info->id = NULL;
 667
 668out1:
 669	return rc;
 670}
 671
 672/*
 673 * Send a negotiation request message to the peer
 674 * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
 675 * After negotiation, the transport is connected and ready for
 676 * carrying upper layer SMB payload
 677 */
 678static int smbd_post_send_negotiate_req(struct smbd_connection *info)
 679{
 680	struct ib_send_wr send_wr;
 681	int rc = -ENOMEM;
 682	struct smbd_request *request;
 683	struct smbd_negotiate_req *packet;
 684
 685	request = mempool_alloc(info->request_mempool, GFP_KERNEL);
 686	if (!request)
 687		return rc;
 688
 689	request->info = info;
 690
 691	packet = smbd_request_payload(request);
 692	packet->min_version = cpu_to_le16(SMBD_V1);
 693	packet->max_version = cpu_to_le16(SMBD_V1);
 694	packet->reserved = 0;
 695	packet->credits_requested = cpu_to_le16(info->send_credit_target);
 696	packet->preferred_send_size = cpu_to_le32(info->max_send_size);
 697	packet->max_receive_size = cpu_to_le32(info->max_receive_size);
 698	packet->max_fragmented_size =
 699		cpu_to_le32(info->max_fragmented_recv_size);
 700
 701	request->num_sge = 1;
 702	request->sge[0].addr = ib_dma_map_single(
 703				info->id->device, (void *)packet,
 704				sizeof(*packet), DMA_TO_DEVICE);
 705	if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
 706		rc = -EIO;
 707		goto dma_mapping_failed;
 708	}
 709
 710	request->sge[0].length = sizeof(*packet);
 711	request->sge[0].lkey = info->pd->local_dma_lkey;
 712
 713	ib_dma_sync_single_for_device(
 714		info->id->device, request->sge[0].addr,
 715		request->sge[0].length, DMA_TO_DEVICE);
 716
 717	request->cqe.done = send_done;
 718
 719	send_wr.next = NULL;
 720	send_wr.wr_cqe = &request->cqe;
 721	send_wr.sg_list = request->sge;
 722	send_wr.num_sge = request->num_sge;
 723	send_wr.opcode = IB_WR_SEND;
 724	send_wr.send_flags = IB_SEND_SIGNALED;
 725
 726	log_rdma_send(INFO, "sge addr=0x%llx length=%u lkey=0x%x\n",
 727		request->sge[0].addr,
 728		request->sge[0].length, request->sge[0].lkey);
 729
 
 730	atomic_inc(&info->send_pending);
 731	rc = ib_post_send(info->id->qp, &send_wr, NULL);
 732	if (!rc)
 733		return 0;
 734
 735	/* if we reach here, post send failed */
 736	log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
 737	atomic_dec(&info->send_pending);
 738	ib_dma_unmap_single(info->id->device, request->sge[0].addr,
 739		request->sge[0].length, DMA_TO_DEVICE);
 740
 741	smbd_disconnect_rdma_connection(info);
 742
 743dma_mapping_failed:
 744	mempool_free(request, info->request_mempool);
 745	return rc;
 746}
 747
 748/*
 749 * Extend the credits to remote peer
 750 * This implements [MS-SMBD] 3.1.5.9
 751 * The idea is that we should extend credits to remote peer as quickly as
 752 * it's allowed, to maintain data flow. We allocate as much receive
 753 * buffer as possible, and extend the receive credits to remote peer
 754 * return value: the new credtis being granted.
 755 */
 756static int manage_credits_prior_sending(struct smbd_connection *info)
 757{
 758	int new_credits;
 759
 760	spin_lock(&info->lock_new_credits_offered);
 761	new_credits = info->new_credits_offered;
 762	info->new_credits_offered = 0;
 763	spin_unlock(&info->lock_new_credits_offered);
 764
 765	return new_credits;
 766}
 767
 768/*
 769 * Check if we need to send a KEEP_ALIVE message
 770 * The idle connection timer triggers a KEEP_ALIVE message when expires
 771 * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
 772 * back a response.
 773 * return value:
 774 * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
 775 * 0: otherwise
 776 */
 777static int manage_keep_alive_before_sending(struct smbd_connection *info)
 778{
 779	if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
 780		info->keep_alive_requested = KEEP_ALIVE_SENT;
 781		return 1;
 782	}
 783	return 0;
 784}
 785
 786/* Post the send request */
 787static int smbd_post_send(struct smbd_connection *info,
 788		struct smbd_request *request)
 789{
 790	struct ib_send_wr send_wr;
 791	int rc, i;
 792
 793	for (i = 0; i < request->num_sge; i++) {
 794		log_rdma_send(INFO,
 795			"rdma_request sge[%d] addr=0x%llx length=%u\n",
 796			i, request->sge[i].addr, request->sge[i].length);
 797		ib_dma_sync_single_for_device(
 798			info->id->device,
 799			request->sge[i].addr,
 800			request->sge[i].length,
 801			DMA_TO_DEVICE);
 802	}
 803
 804	request->cqe.done = send_done;
 805
 806	send_wr.next = NULL;
 807	send_wr.wr_cqe = &request->cqe;
 808	send_wr.sg_list = request->sge;
 809	send_wr.num_sge = request->num_sge;
 810	send_wr.opcode = IB_WR_SEND;
 811	send_wr.send_flags = IB_SEND_SIGNALED;
 812
 813	rc = ib_post_send(info->id->qp, &send_wr, NULL);
 814	if (rc) {
 815		log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
 816		smbd_disconnect_rdma_connection(info);
 817		rc = -EAGAIN;
 818	} else
 819		/* Reset timer for idle connection after packet is sent */
 820		mod_delayed_work(info->workqueue, &info->idle_timer_work,
 821			info->keep_alive_interval*HZ);
 822
 823	return rc;
 824}
 825
 826static int smbd_post_send_sgl(struct smbd_connection *info,
 827	struct scatterlist *sgl, int data_length, int remaining_data_length)
 828{
 829	int num_sgs;
 830	int i, rc;
 831	int header_length;
 832	struct smbd_request *request;
 833	struct smbd_data_transfer *packet;
 834	int new_credits;
 835	struct scatterlist *sg;
 836
 837wait_credit:
 838	/* Wait for send credits. A SMBD packet needs one credit */
 839	rc = wait_event_interruptible(info->wait_send_queue,
 840		atomic_read(&info->send_credits) > 0 ||
 841		info->transport_status != SMBD_CONNECTED);
 842	if (rc)
 843		goto err_wait_credit;
 844
 845	if (info->transport_status != SMBD_CONNECTED) {
 846		log_outgoing(ERR, "disconnected not sending on wait_credit\n");
 847		rc = -EAGAIN;
 848		goto err_wait_credit;
 849	}
 850	if (unlikely(atomic_dec_return(&info->send_credits) < 0)) {
 851		atomic_inc(&info->send_credits);
 852		goto wait_credit;
 853	}
 854
 855wait_send_queue:
 856	wait_event(info->wait_post_send,
 857		atomic_read(&info->send_pending) < info->send_credit_target ||
 858		info->transport_status != SMBD_CONNECTED);
 859
 860	if (info->transport_status != SMBD_CONNECTED) {
 861		log_outgoing(ERR, "disconnected not sending on wait_send_queue\n");
 862		rc = -EAGAIN;
 863		goto err_wait_send_queue;
 864	}
 865
 866	if (unlikely(atomic_inc_return(&info->send_pending) >
 867				info->send_credit_target)) {
 868		atomic_dec(&info->send_pending);
 869		goto wait_send_queue;
 870	}
 
 871
 872	request = mempool_alloc(info->request_mempool, GFP_KERNEL);
 873	if (!request) {
 874		rc = -ENOMEM;
 875		goto err_alloc;
 876	}
 877
 878	request->info = info;
 879
 880	/* Fill in the packet header */
 881	packet = smbd_request_payload(request);
 882	packet->credits_requested = cpu_to_le16(info->send_credit_target);
 883
 884	new_credits = manage_credits_prior_sending(info);
 885	atomic_add(new_credits, &info->receive_credits);
 886	packet->credits_granted = cpu_to_le16(new_credits);
 887
 888	info->send_immediate = false;
 889
 890	packet->flags = 0;
 891	if (manage_keep_alive_before_sending(info))
 892		packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
 893
 894	packet->reserved = 0;
 895	if (!data_length)
 896		packet->data_offset = 0;
 897	else
 898		packet->data_offset = cpu_to_le32(24);
 899	packet->data_length = cpu_to_le32(data_length);
 900	packet->remaining_data_length = cpu_to_le32(remaining_data_length);
 901	packet->padding = 0;
 902
 903	log_outgoing(INFO, "credits_requested=%d credits_granted=%d data_offset=%d data_length=%d remaining_data_length=%d\n",
 904		     le16_to_cpu(packet->credits_requested),
 905		     le16_to_cpu(packet->credits_granted),
 906		     le32_to_cpu(packet->data_offset),
 907		     le32_to_cpu(packet->data_length),
 908		     le32_to_cpu(packet->remaining_data_length));
 
 909
 910	/* Map the packet to DMA */
 911	header_length = sizeof(struct smbd_data_transfer);
 912	/* If this is a packet without payload, don't send padding */
 913	if (!data_length)
 914		header_length = offsetof(struct smbd_data_transfer, padding);
 915
 916	request->num_sge = 1;
 917	request->sge[0].addr = ib_dma_map_single(info->id->device,
 918						 (void *)packet,
 919						 header_length,
 920						 DMA_TO_DEVICE);
 921	if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
 
 922		rc = -EIO;
 923		request->sge[0].addr = 0;
 924		goto err_dma;
 925	}
 926
 927	request->sge[0].length = header_length;
 928	request->sge[0].lkey = info->pd->local_dma_lkey;
 929
 930	/* Fill in the packet data payload */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 931	num_sgs = sgl ? sg_nents(sgl) : 0;
 932	for_each_sg(sgl, sg, num_sgs, i) {
 933		request->sge[i+1].addr =
 934			ib_dma_map_page(info->id->device, sg_page(sg),
 935			       sg->offset, sg->length, DMA_TO_DEVICE);
 936		if (ib_dma_mapping_error(
 937				info->id->device, request->sge[i+1].addr)) {
 938			rc = -EIO;
 939			request->sge[i+1].addr = 0;
 940			goto err_dma;
 941		}
 942		request->sge[i+1].length = sg->length;
 943		request->sge[i+1].lkey = info->pd->local_dma_lkey;
 944		request->num_sge++;
 945	}
 946
 947	rc = smbd_post_send(info, request);
 948	if (!rc)
 949		return 0;
 950
 951err_dma:
 952	for (i = 0; i < request->num_sge; i++)
 953		if (request->sge[i].addr)
 954			ib_dma_unmap_single(info->id->device,
 955					    request->sge[i].addr,
 956					    request->sge[i].length,
 957					    DMA_TO_DEVICE);
 958	mempool_free(request, info->request_mempool);
 959
 960	/* roll back receive credits and credits to be offered */
 961	spin_lock(&info->lock_new_credits_offered);
 962	info->new_credits_offered += new_credits;
 963	spin_unlock(&info->lock_new_credits_offered);
 964	atomic_sub(new_credits, &info->receive_credits);
 965
 966err_alloc:
 967	if (atomic_dec_and_test(&info->send_pending))
 968		wake_up(&info->wait_send_pending);
 969
 970err_wait_send_queue:
 971	/* roll back send credits and pending */
 972	atomic_inc(&info->send_credits);
 973
 974err_wait_credit:
 975	return rc;
 976}
 977
 978/*
 979 * Send a page
 980 * page: the page to send
 981 * offset: offset in the page to send
 982 * size: length in the page to send
 983 * remaining_data_length: remaining data to send in this payload
 984 */
 985static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
 986		unsigned long offset, size_t size, int remaining_data_length)
 987{
 988	struct scatterlist sgl;
 989
 990	sg_init_table(&sgl, 1);
 991	sg_set_page(&sgl, page, size, offset);
 992
 993	return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
 994}
 995
 996/*
 997 * Send an empty message
 998 * Empty message is used to extend credits to peer to for keep live
 999 * while there is no upper layer payload to send at the time
1000 */
1001static int smbd_post_send_empty(struct smbd_connection *info)
1002{
1003	info->count_send_empty++;
1004	return smbd_post_send_sgl(info, NULL, 0, 0);
1005}
1006
1007/*
1008 * Send a data buffer
1009 * iov: the iov array describing the data buffers
1010 * n_vec: number of iov array
1011 * remaining_data_length: remaining data to send following this packet
1012 * in segmented SMBD packet
1013 */
1014static int smbd_post_send_data(
1015	struct smbd_connection *info, struct kvec *iov, int n_vec,
1016	int remaining_data_length)
1017{
1018	int i;
1019	u32 data_length = 0;
1020	struct scatterlist sgl[SMBDIRECT_MAX_SEND_SGE - 1];
1021
1022	if (n_vec > SMBDIRECT_MAX_SEND_SGE - 1) {
1023		cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
1024		return -EINVAL;
1025	}
1026
1027	sg_init_table(sgl, n_vec);
1028	for (i = 0; i < n_vec; i++) {
1029		data_length += iov[i].iov_len;
1030		sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
1031	}
1032
1033	return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
1034}
1035
1036/*
1037 * Post a receive request to the transport
1038 * The remote peer can only send data when a receive request is posted
1039 * The interaction is controlled by send/receive credit system
1040 */
1041static int smbd_post_recv(
1042		struct smbd_connection *info, struct smbd_response *response)
1043{
1044	struct ib_recv_wr recv_wr;
1045	int rc = -EIO;
1046
1047	response->sge.addr = ib_dma_map_single(
1048				info->id->device, response->packet,
1049				info->max_receive_size, DMA_FROM_DEVICE);
1050	if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1051		return rc;
1052
1053	response->sge.length = info->max_receive_size;
1054	response->sge.lkey = info->pd->local_dma_lkey;
1055
1056	response->cqe.done = recv_done;
1057
1058	recv_wr.wr_cqe = &response->cqe;
1059	recv_wr.next = NULL;
1060	recv_wr.sg_list = &response->sge;
1061	recv_wr.num_sge = 1;
1062
1063	rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
1064	if (rc) {
1065		ib_dma_unmap_single(info->id->device, response->sge.addr,
1066				    response->sge.length, DMA_FROM_DEVICE);
1067		smbd_disconnect_rdma_connection(info);
1068		log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1069	}
1070
1071	return rc;
1072}
1073
1074/* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1075static int smbd_negotiate(struct smbd_connection *info)
1076{
1077	int rc;
1078	struct smbd_response *response = get_receive_buffer(info);
1079
1080	response->type = SMBD_NEGOTIATE_RESP;
1081	rc = smbd_post_recv(info, response);
1082	log_rdma_event(INFO, "smbd_post_recv rc=%d iov.addr=0x%llx iov.length=%u iov.lkey=0x%x\n",
1083		       rc, response->sge.addr,
1084		       response->sge.length, response->sge.lkey);
 
 
1085	if (rc)
1086		return rc;
1087
1088	init_completion(&info->negotiate_completion);
1089	info->negotiate_done = false;
1090	rc = smbd_post_send_negotiate_req(info);
1091	if (rc)
1092		return rc;
1093
1094	rc = wait_for_completion_interruptible_timeout(
1095		&info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1096	log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1097
1098	if (info->negotiate_done)
1099		return 0;
1100
1101	if (rc == 0)
1102		rc = -ETIMEDOUT;
1103	else if (rc == -ERESTARTSYS)
1104		rc = -EINTR;
1105	else
1106		rc = -ENOTCONN;
1107
1108	return rc;
1109}
1110
1111static void put_empty_packet(
1112		struct smbd_connection *info, struct smbd_response *response)
1113{
1114	spin_lock(&info->empty_packet_queue_lock);
1115	list_add_tail(&response->list, &info->empty_packet_queue);
1116	info->count_empty_packet_queue++;
1117	spin_unlock(&info->empty_packet_queue_lock);
1118
1119	queue_work(info->workqueue, &info->post_send_credits_work);
1120}
1121
1122/*
1123 * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1124 * This is a queue for reassembling upper layer payload and present to upper
1125 * layer. All the inncoming payload go to the reassembly queue, regardless of
1126 * if reassembly is required. The uuper layer code reads from the queue for all
1127 * incoming payloads.
1128 * Put a received packet to the reassembly queue
1129 * response: the packet received
1130 * data_length: the size of payload in this packet
1131 */
1132static void enqueue_reassembly(
1133	struct smbd_connection *info,
1134	struct smbd_response *response,
1135	int data_length)
1136{
1137	spin_lock(&info->reassembly_queue_lock);
1138	list_add_tail(&response->list, &info->reassembly_queue);
1139	info->reassembly_queue_length++;
1140	/*
1141	 * Make sure reassembly_data_length is updated after list and
1142	 * reassembly_queue_length are updated. On the dequeue side
1143	 * reassembly_data_length is checked without a lock to determine
1144	 * if reassembly_queue_length and list is up to date
1145	 */
1146	virt_wmb();
1147	info->reassembly_data_length += data_length;
1148	spin_unlock(&info->reassembly_queue_lock);
1149	info->count_reassembly_queue++;
1150	info->count_enqueue_reassembly_queue++;
1151}
1152
1153/*
1154 * Get the first entry at the front of reassembly queue
1155 * Caller is responsible for locking
1156 * return value: the first entry if any, NULL if queue is empty
1157 */
1158static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1159{
1160	struct smbd_response *ret = NULL;
1161
1162	if (!list_empty(&info->reassembly_queue)) {
1163		ret = list_first_entry(
1164			&info->reassembly_queue,
1165			struct smbd_response, list);
1166	}
1167	return ret;
1168}
1169
1170static struct smbd_response *get_empty_queue_buffer(
1171		struct smbd_connection *info)
1172{
1173	struct smbd_response *ret = NULL;
1174	unsigned long flags;
1175
1176	spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1177	if (!list_empty(&info->empty_packet_queue)) {
1178		ret = list_first_entry(
1179			&info->empty_packet_queue,
1180			struct smbd_response, list);
1181		list_del(&ret->list);
1182		info->count_empty_packet_queue--;
1183	}
1184	spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1185
1186	return ret;
1187}
1188
1189/*
1190 * Get a receive buffer
1191 * For each remote send, we need to post a receive. The receive buffers are
1192 * pre-allocated in advance.
1193 * return value: the receive buffer, NULL if none is available
1194 */
1195static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1196{
1197	struct smbd_response *ret = NULL;
1198	unsigned long flags;
1199
1200	spin_lock_irqsave(&info->receive_queue_lock, flags);
1201	if (!list_empty(&info->receive_queue)) {
1202		ret = list_first_entry(
1203			&info->receive_queue,
1204			struct smbd_response, list);
1205		list_del(&ret->list);
1206		info->count_receive_queue--;
1207		info->count_get_receive_buffer++;
1208	}
1209	spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1210
1211	return ret;
1212}
1213
1214/*
1215 * Return a receive buffer
1216 * Upon returning of a receive buffer, we can post new receive and extend
1217 * more receive credits to remote peer. This is done immediately after a
1218 * receive buffer is returned.
1219 */
1220static void put_receive_buffer(
1221	struct smbd_connection *info, struct smbd_response *response)
1222{
1223	unsigned long flags;
1224
1225	ib_dma_unmap_single(info->id->device, response->sge.addr,
1226		response->sge.length, DMA_FROM_DEVICE);
1227
1228	spin_lock_irqsave(&info->receive_queue_lock, flags);
1229	list_add_tail(&response->list, &info->receive_queue);
1230	info->count_receive_queue++;
1231	info->count_put_receive_buffer++;
1232	spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1233
1234	queue_work(info->workqueue, &info->post_send_credits_work);
1235}
1236
1237/* Preallocate all receive buffer on transport establishment */
1238static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1239{
1240	int i;
1241	struct smbd_response *response;
1242
1243	INIT_LIST_HEAD(&info->reassembly_queue);
1244	spin_lock_init(&info->reassembly_queue_lock);
1245	info->reassembly_data_length = 0;
1246	info->reassembly_queue_length = 0;
1247
1248	INIT_LIST_HEAD(&info->receive_queue);
1249	spin_lock_init(&info->receive_queue_lock);
1250	info->count_receive_queue = 0;
1251
1252	INIT_LIST_HEAD(&info->empty_packet_queue);
1253	spin_lock_init(&info->empty_packet_queue_lock);
1254	info->count_empty_packet_queue = 0;
1255
1256	init_waitqueue_head(&info->wait_receive_queues);
1257
1258	for (i = 0; i < num_buf; i++) {
1259		response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1260		if (!response)
1261			goto allocate_failed;
1262
1263		response->info = info;
1264		list_add_tail(&response->list, &info->receive_queue);
1265		info->count_receive_queue++;
1266	}
1267
1268	return 0;
1269
1270allocate_failed:
1271	while (!list_empty(&info->receive_queue)) {
1272		response = list_first_entry(
1273				&info->receive_queue,
1274				struct smbd_response, list);
1275		list_del(&response->list);
1276		info->count_receive_queue--;
1277
1278		mempool_free(response, info->response_mempool);
1279	}
1280	return -ENOMEM;
1281}
1282
1283static void destroy_receive_buffers(struct smbd_connection *info)
1284{
1285	struct smbd_response *response;
1286
1287	while ((response = get_receive_buffer(info)))
1288		mempool_free(response, info->response_mempool);
1289
1290	while ((response = get_empty_queue_buffer(info)))
1291		mempool_free(response, info->response_mempool);
1292}
1293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1294/* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1295static void idle_connection_timer(struct work_struct *work)
1296{
1297	struct smbd_connection *info = container_of(
1298					work, struct smbd_connection,
1299					idle_timer_work.work);
1300
1301	if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1302		log_keep_alive(ERR,
1303			"error status info->keep_alive_requested=%d\n",
1304			info->keep_alive_requested);
1305		smbd_disconnect_rdma_connection(info);
1306		return;
1307	}
1308
1309	log_keep_alive(INFO, "about to send an empty idle message\n");
1310	smbd_post_send_empty(info);
1311
1312	/* Setup the next idle timeout work */
1313	queue_delayed_work(info->workqueue, &info->idle_timer_work,
1314			info->keep_alive_interval*HZ);
1315}
1316
1317/*
1318 * Destroy the transport and related RDMA and memory resources
1319 * Need to go through all the pending counters and make sure on one is using
1320 * the transport while it is destroyed
1321 */
1322void smbd_destroy(struct TCP_Server_Info *server)
1323{
1324	struct smbd_connection *info = server->smbd_conn;
1325	struct smbd_response *response;
1326	unsigned long flags;
1327
1328	if (!info) {
1329		log_rdma_event(INFO, "rdma session already destroyed\n");
1330		return;
1331	}
1332
1333	log_rdma_event(INFO, "destroying rdma session\n");
1334	if (info->transport_status != SMBD_DISCONNECTED) {
1335		rdma_disconnect(server->smbd_conn->id);
1336		log_rdma_event(INFO, "wait for transport being disconnected\n");
1337		wait_event_interruptible(
1338			info->disconn_wait,
1339			info->transport_status == SMBD_DISCONNECTED);
1340	}
1341
1342	log_rdma_event(INFO, "destroying qp\n");
1343	ib_drain_qp(info->id->qp);
1344	rdma_destroy_qp(info->id);
1345
1346	log_rdma_event(INFO, "cancelling idle timer\n");
1347	cancel_delayed_work_sync(&info->idle_timer_work);
 
 
1348
1349	log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
1350	wait_event(info->wait_send_pending,
1351		atomic_read(&info->send_pending) == 0);
 
 
1352
1353	/* It's not possible for upper layer to get to reassembly */
1354	log_rdma_event(INFO, "drain the reassembly queue\n");
1355	do {
1356		spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1357		response = _get_first_reassembly(info);
1358		if (response) {
1359			list_del(&response->list);
1360			spin_unlock_irqrestore(
1361				&info->reassembly_queue_lock, flags);
1362			put_receive_buffer(info, response);
1363		} else
1364			spin_unlock_irqrestore(
1365				&info->reassembly_queue_lock, flags);
1366	} while (response);
1367	info->reassembly_data_length = 0;
1368
1369	log_rdma_event(INFO, "free receive buffers\n");
1370	wait_event(info->wait_receive_queues,
1371		info->count_receive_queue + info->count_empty_packet_queue
1372			== info->receive_credit_max);
1373	destroy_receive_buffers(info);
1374
1375	/*
1376	 * For performance reasons, memory registration and deregistration
1377	 * are not locked by srv_mutex. It is possible some processes are
1378	 * blocked on transport srv_mutex while holding memory registration.
1379	 * Release the transport srv_mutex to allow them to hit the failure
1380	 * path when sending data, and then release memory registartions.
1381	 */
1382	log_rdma_event(INFO, "freeing mr list\n");
1383	wake_up_interruptible_all(&info->wait_mr);
1384	while (atomic_read(&info->mr_used_count)) {
1385		cifs_server_unlock(server);
1386		msleep(1000);
1387		cifs_server_lock(server);
1388	}
1389	destroy_mr_list(info);
1390
1391	ib_free_cq(info->send_cq);
1392	ib_free_cq(info->recv_cq);
1393	ib_dealloc_pd(info->pd);
1394	rdma_destroy_id(info->id);
1395
1396	/* free mempools */
1397	mempool_destroy(info->request_mempool);
1398	kmem_cache_destroy(info->request_cache);
1399
1400	mempool_destroy(info->response_mempool);
1401	kmem_cache_destroy(info->response_cache);
1402
1403	info->transport_status = SMBD_DESTROYED;
1404
1405	destroy_workqueue(info->workqueue);
1406	log_rdma_event(INFO,  "rdma session destroyed\n");
1407	kfree(info);
1408	server->smbd_conn = NULL;
1409}
1410
1411/*
1412 * Reconnect this SMBD connection, called from upper layer
1413 * return value: 0 on success, or actual error code
1414 */
1415int smbd_reconnect(struct TCP_Server_Info *server)
1416{
1417	log_rdma_event(INFO, "reconnecting rdma session\n");
1418
1419	if (!server->smbd_conn) {
1420		log_rdma_event(INFO, "rdma session already destroyed\n");
1421		goto create_conn;
1422	}
1423
1424	/*
1425	 * This is possible if transport is disconnected and we haven't received
1426	 * notification from RDMA, but upper layer has detected timeout
1427	 */
1428	if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1429		log_rdma_event(INFO, "disconnecting transport\n");
1430		smbd_destroy(server);
1431	}
1432
1433create_conn:
1434	log_rdma_event(INFO, "creating rdma session\n");
1435	server->smbd_conn = smbd_get_connection(
1436		server, (struct sockaddr *) &server->dstaddr);
1437
1438	if (server->smbd_conn)
1439		cifs_dbg(VFS, "RDMA transport re-established\n");
1440
1441	return server->smbd_conn ? 0 : -ENOENT;
1442}
1443
1444static void destroy_caches_and_workqueue(struct smbd_connection *info)
1445{
1446	destroy_receive_buffers(info);
1447	destroy_workqueue(info->workqueue);
1448	mempool_destroy(info->response_mempool);
1449	kmem_cache_destroy(info->response_cache);
1450	mempool_destroy(info->request_mempool);
1451	kmem_cache_destroy(info->request_cache);
1452}
1453
1454#define MAX_NAME_LEN	80
1455static int allocate_caches_and_workqueue(struct smbd_connection *info)
1456{
1457	char name[MAX_NAME_LEN];
1458	int rc;
1459
1460	scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
1461	info->request_cache =
1462		kmem_cache_create(
1463			name,
1464			sizeof(struct smbd_request) +
1465				sizeof(struct smbd_data_transfer),
1466			0, SLAB_HWCACHE_ALIGN, NULL);
1467	if (!info->request_cache)
1468		return -ENOMEM;
1469
1470	info->request_mempool =
1471		mempool_create(info->send_credit_target, mempool_alloc_slab,
1472			mempool_free_slab, info->request_cache);
1473	if (!info->request_mempool)
1474		goto out1;
1475
1476	scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
1477	info->response_cache =
1478		kmem_cache_create(
1479			name,
1480			sizeof(struct smbd_response) +
1481				info->max_receive_size,
1482			0, SLAB_HWCACHE_ALIGN, NULL);
1483	if (!info->response_cache)
1484		goto out2;
1485
1486	info->response_mempool =
1487		mempool_create(info->receive_credit_max, mempool_alloc_slab,
1488		       mempool_free_slab, info->response_cache);
1489	if (!info->response_mempool)
1490		goto out3;
1491
1492	scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
1493	info->workqueue = create_workqueue(name);
1494	if (!info->workqueue)
1495		goto out4;
1496
1497	rc = allocate_receive_buffers(info, info->receive_credit_max);
1498	if (rc) {
1499		log_rdma_event(ERR, "failed to allocate receive buffers\n");
1500		goto out5;
1501	}
1502
1503	return 0;
1504
1505out5:
1506	destroy_workqueue(info->workqueue);
1507out4:
1508	mempool_destroy(info->response_mempool);
1509out3:
1510	kmem_cache_destroy(info->response_cache);
1511out2:
1512	mempool_destroy(info->request_mempool);
1513out1:
1514	kmem_cache_destroy(info->request_cache);
1515	return -ENOMEM;
1516}
1517
1518/* Create a SMBD connection, called by upper layer */
1519static struct smbd_connection *_smbd_get_connection(
1520	struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1521{
1522	int rc;
1523	struct smbd_connection *info;
1524	struct rdma_conn_param conn_param;
1525	struct ib_qp_init_attr qp_attr;
1526	struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
1527	struct ib_port_immutable port_immutable;
1528	u32 ird_ord_hdr[2];
1529
1530	info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1531	if (!info)
1532		return NULL;
1533
1534	info->transport_status = SMBD_CONNECTING;
1535	rc = smbd_ia_open(info, dstaddr, port);
1536	if (rc) {
1537		log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1538		goto create_id_failed;
1539	}
1540
1541	if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1542	    smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1543		log_rdma_event(ERR, "consider lowering send_credit_target = %d. Possible CQE overrun, device reporting max_cqe %d max_qp_wr %d\n",
1544			       smbd_send_credit_target,
1545			       info->id->device->attrs.max_cqe,
1546			       info->id->device->attrs.max_qp_wr);
 
 
 
1547		goto config_failed;
1548	}
1549
1550	if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1551	    smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1552		log_rdma_event(ERR, "consider lowering receive_credit_max = %d. Possible CQE overrun, device reporting max_cqe %d max_qp_wr %d\n",
1553			       smbd_receive_credit_max,
1554			       info->id->device->attrs.max_cqe,
1555			       info->id->device->attrs.max_qp_wr);
 
 
 
1556		goto config_failed;
1557	}
1558
1559	info->receive_credit_max = smbd_receive_credit_max;
1560	info->send_credit_target = smbd_send_credit_target;
1561	info->max_send_size = smbd_max_send_size;
1562	info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1563	info->max_receive_size = smbd_max_receive_size;
1564	info->keep_alive_interval = smbd_keep_alive_interval;
1565
1566	if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SEND_SGE ||
1567	    info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_RECV_SGE) {
1568		log_rdma_event(ERR,
1569			"device %.*s max_send_sge/max_recv_sge = %d/%d too small\n",
1570			IB_DEVICE_NAME_MAX,
1571			info->id->device->name,
1572			info->id->device->attrs.max_send_sge,
 
 
 
1573			info->id->device->attrs.max_recv_sge);
1574		goto config_failed;
1575	}
1576
1577	info->send_cq = NULL;
1578	info->recv_cq = NULL;
1579	info->send_cq =
1580		ib_alloc_cq_any(info->id->device, info,
1581				info->send_credit_target, IB_POLL_SOFTIRQ);
1582	if (IS_ERR(info->send_cq)) {
1583		info->send_cq = NULL;
1584		goto alloc_cq_failed;
1585	}
1586
1587	info->recv_cq =
1588		ib_alloc_cq_any(info->id->device, info,
1589				info->receive_credit_max, IB_POLL_SOFTIRQ);
1590	if (IS_ERR(info->recv_cq)) {
1591		info->recv_cq = NULL;
1592		goto alloc_cq_failed;
1593	}
1594
1595	memset(&qp_attr, 0, sizeof(qp_attr));
1596	qp_attr.event_handler = smbd_qp_async_error_upcall;
1597	qp_attr.qp_context = info;
1598	qp_attr.cap.max_send_wr = info->send_credit_target;
1599	qp_attr.cap.max_recv_wr = info->receive_credit_max;
1600	qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SEND_SGE;
1601	qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_RECV_SGE;
1602	qp_attr.cap.max_inline_data = 0;
1603	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1604	qp_attr.qp_type = IB_QPT_RC;
1605	qp_attr.send_cq = info->send_cq;
1606	qp_attr.recv_cq = info->recv_cq;
1607	qp_attr.port_num = ~0;
1608
1609	rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1610	if (rc) {
1611		log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1612		goto create_qp_failed;
1613	}
1614
1615	memset(&conn_param, 0, sizeof(conn_param));
1616	conn_param.initiator_depth = 0;
1617
1618	conn_param.responder_resources =
1619		info->id->device->attrs.max_qp_rd_atom
1620			< SMBD_CM_RESPONDER_RESOURCES ?
1621		info->id->device->attrs.max_qp_rd_atom :
1622		SMBD_CM_RESPONDER_RESOURCES;
1623	info->responder_resources = conn_param.responder_resources;
1624	log_rdma_mr(INFO, "responder_resources=%d\n",
1625		info->responder_resources);
1626
1627	/* Need to send IRD/ORD in private data for iWARP */
1628	info->id->device->ops.get_port_immutable(
1629		info->id->device, info->id->port_num, &port_immutable);
1630	if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1631		ird_ord_hdr[0] = info->responder_resources;
1632		ird_ord_hdr[1] = 1;
1633		conn_param.private_data = ird_ord_hdr;
1634		conn_param.private_data_len = sizeof(ird_ord_hdr);
1635	} else {
1636		conn_param.private_data = NULL;
1637		conn_param.private_data_len = 0;
1638	}
1639
1640	conn_param.retry_count = SMBD_CM_RETRY;
1641	conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1642	conn_param.flow_control = 0;
1643
1644	log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1645		&addr_in->sin_addr, port);
1646
1647	init_waitqueue_head(&info->conn_wait);
1648	init_waitqueue_head(&info->disconn_wait);
1649	init_waitqueue_head(&info->wait_reassembly_queue);
1650	rc = rdma_connect(info->id, &conn_param);
1651	if (rc) {
1652		log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1653		goto rdma_connect_failed;
1654	}
1655
1656	wait_event_interruptible(
1657		info->conn_wait, info->transport_status != SMBD_CONNECTING);
1658
1659	if (info->transport_status != SMBD_CONNECTED) {
1660		log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1661		goto rdma_connect_failed;
1662	}
1663
1664	log_rdma_event(INFO, "rdma_connect connected\n");
1665
1666	rc = allocate_caches_and_workqueue(info);
1667	if (rc) {
1668		log_rdma_event(ERR, "cache allocation failed\n");
1669		goto allocate_cache_failed;
1670	}
1671
1672	init_waitqueue_head(&info->wait_send_queue);
1673	INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
 
1674	queue_delayed_work(info->workqueue, &info->idle_timer_work,
1675		info->keep_alive_interval*HZ);
1676
1677	init_waitqueue_head(&info->wait_send_pending);
1678	atomic_set(&info->send_pending, 0);
1679
1680	init_waitqueue_head(&info->wait_post_send);
 
1681
1682	INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
 
1683	INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1684	info->new_credits_offered = 0;
1685	spin_lock_init(&info->lock_new_credits_offered);
1686
1687	rc = smbd_negotiate(info);
1688	if (rc) {
1689		log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1690		goto negotiation_failed;
1691	}
1692
1693	rc = allocate_mr_list(info);
1694	if (rc) {
1695		log_rdma_mr(ERR, "memory registration allocation failed\n");
1696		goto allocate_mr_failed;
1697	}
1698
1699	return info;
1700
1701allocate_mr_failed:
1702	/* At this point, need to a full transport shutdown */
1703	smbd_destroy(server);
1704	return NULL;
1705
1706negotiation_failed:
1707	cancel_delayed_work_sync(&info->idle_timer_work);
1708	destroy_caches_and_workqueue(info);
1709	info->transport_status = SMBD_NEGOTIATE_FAILED;
1710	init_waitqueue_head(&info->conn_wait);
1711	rdma_disconnect(info->id);
1712	wait_event(info->conn_wait,
1713		info->transport_status == SMBD_DISCONNECTED);
1714
1715allocate_cache_failed:
1716rdma_connect_failed:
1717	rdma_destroy_qp(info->id);
1718
1719create_qp_failed:
1720alloc_cq_failed:
1721	if (info->send_cq)
1722		ib_free_cq(info->send_cq);
1723	if (info->recv_cq)
1724		ib_free_cq(info->recv_cq);
1725
1726config_failed:
1727	ib_dealloc_pd(info->pd);
1728	rdma_destroy_id(info->id);
1729
1730create_id_failed:
1731	kfree(info);
1732	return NULL;
1733}
1734
1735struct smbd_connection *smbd_get_connection(
1736	struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1737{
1738	struct smbd_connection *ret;
1739	int port = SMBD_PORT;
1740
1741try_again:
1742	ret = _smbd_get_connection(server, dstaddr, port);
1743
1744	/* Try SMB_PORT if SMBD_PORT doesn't work */
1745	if (!ret && port == SMBD_PORT) {
1746		port = SMB_PORT;
1747		goto try_again;
1748	}
1749	return ret;
1750}
1751
1752/*
1753 * Receive data from receive reassembly queue
1754 * All the incoming data packets are placed in reassembly queue
1755 * buf: the buffer to read data into
1756 * size: the length of data to read
1757 * return value: actual data read
1758 * Note: this implementation copies the data from reassebmly queue to receive
1759 * buffers used by upper layer. This is not the optimal code path. A better way
1760 * to do it is to not have upper layer allocate its receive buffers but rather
1761 * borrow the buffer from reassembly queue, and return it after data is
1762 * consumed. But this will require more changes to upper layer code, and also
1763 * need to consider packet boundaries while they still being reassembled.
1764 */
1765static int smbd_recv_buf(struct smbd_connection *info, char *buf,
1766		unsigned int size)
1767{
1768	struct smbd_response *response;
1769	struct smbd_data_transfer *data_transfer;
1770	int to_copy, to_read, data_read, offset;
1771	u32 data_length, remaining_data_length, data_offset;
1772	int rc;
1773
1774again:
1775	/*
1776	 * No need to hold the reassembly queue lock all the time as we are
1777	 * the only one reading from the front of the queue. The transport
1778	 * may add more entries to the back of the queue at the same time
1779	 */
1780	log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1781		info->reassembly_data_length);
1782	if (info->reassembly_data_length >= size) {
1783		int queue_length;
1784		int queue_removed = 0;
1785
1786		/*
1787		 * Need to make sure reassembly_data_length is read before
1788		 * reading reassembly_queue_length and calling
1789		 * _get_first_reassembly. This call is lock free
1790		 * as we never read at the end of the queue which are being
1791		 * updated in SOFTIRQ as more data is received
1792		 */
1793		virt_rmb();
1794		queue_length = info->reassembly_queue_length;
1795		data_read = 0;
1796		to_read = size;
1797		offset = info->first_entry_offset;
1798		while (data_read < size) {
1799			response = _get_first_reassembly(info);
1800			data_transfer = smbd_response_payload(response);
1801			data_length = le32_to_cpu(data_transfer->data_length);
1802			remaining_data_length =
1803				le32_to_cpu(
1804					data_transfer->remaining_data_length);
1805			data_offset = le32_to_cpu(data_transfer->data_offset);
1806
1807			/*
1808			 * The upper layer expects RFC1002 length at the
1809			 * beginning of the payload. Return it to indicate
1810			 * the total length of the packet. This minimize the
1811			 * change to upper layer packet processing logic. This
1812			 * will be eventually remove when an intermediate
1813			 * transport layer is added
1814			 */
1815			if (response->first_segment && size == 4) {
1816				unsigned int rfc1002_len =
1817					data_length + remaining_data_length;
1818				*((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1819				data_read = 4;
1820				response->first_segment = false;
1821				log_read(INFO, "returning rfc1002 length %d\n",
1822					rfc1002_len);
1823				goto read_rfc1002_done;
1824			}
1825
1826			to_copy = min_t(int, data_length - offset, to_read);
1827			memcpy(
1828				buf + data_read,
1829				(char *)data_transfer + data_offset + offset,
1830				to_copy);
1831
1832			/* move on to the next buffer? */
1833			if (to_copy == data_length - offset) {
1834				queue_length--;
1835				/*
1836				 * No need to lock if we are not at the
1837				 * end of the queue
1838				 */
1839				if (queue_length)
1840					list_del(&response->list);
1841				else {
1842					spin_lock_irq(
1843						&info->reassembly_queue_lock);
1844					list_del(&response->list);
1845					spin_unlock_irq(
1846						&info->reassembly_queue_lock);
1847				}
1848				queue_removed++;
1849				info->count_reassembly_queue--;
1850				info->count_dequeue_reassembly_queue++;
1851				put_receive_buffer(info, response);
1852				offset = 0;
1853				log_read(INFO, "put_receive_buffer offset=0\n");
1854			} else
1855				offset += to_copy;
1856
1857			to_read -= to_copy;
1858			data_read += to_copy;
1859
1860			log_read(INFO, "_get_first_reassembly memcpy %d bytes data_transfer_length-offset=%d after that to_read=%d data_read=%d offset=%d\n",
1861				 to_copy, data_length - offset,
1862				 to_read, data_read, offset);
 
 
1863		}
1864
1865		spin_lock_irq(&info->reassembly_queue_lock);
1866		info->reassembly_data_length -= data_read;
1867		info->reassembly_queue_length -= queue_removed;
1868		spin_unlock_irq(&info->reassembly_queue_lock);
1869
1870		info->first_entry_offset = offset;
1871		log_read(INFO, "returning to thread data_read=%d reassembly_data_length=%d first_entry_offset=%d\n",
1872			 data_read, info->reassembly_data_length,
1873			 info->first_entry_offset);
 
1874read_rfc1002_done:
1875		return data_read;
1876	}
1877
1878	log_read(INFO, "wait_event on more data\n");
1879	rc = wait_event_interruptible(
1880		info->wait_reassembly_queue,
1881		info->reassembly_data_length >= size ||
1882			info->transport_status != SMBD_CONNECTED);
1883	/* Don't return any data if interrupted */
1884	if (rc)
1885		return rc;
1886
1887	if (info->transport_status != SMBD_CONNECTED) {
1888		log_read(ERR, "disconnected\n");
1889		return -ECONNABORTED;
1890	}
1891
1892	goto again;
1893}
1894
1895/*
1896 * Receive a page from receive reassembly queue
1897 * page: the page to read data into
1898 * to_read: the length of data to read
1899 * return value: actual data read
1900 */
1901static int smbd_recv_page(struct smbd_connection *info,
1902		struct page *page, unsigned int page_offset,
1903		unsigned int to_read)
1904{
1905	int ret;
1906	char *to_address;
1907	void *page_address;
1908
1909	/* make sure we have the page ready for read */
1910	ret = wait_event_interruptible(
1911		info->wait_reassembly_queue,
1912		info->reassembly_data_length >= to_read ||
1913			info->transport_status != SMBD_CONNECTED);
1914	if (ret)
1915		return ret;
1916
1917	/* now we can read from reassembly queue and not sleep */
1918	page_address = kmap_atomic(page);
1919	to_address = (char *) page_address + page_offset;
1920
1921	log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
1922		page, to_address, to_read);
1923
1924	ret = smbd_recv_buf(info, to_address, to_read);
1925	kunmap_atomic(page_address);
1926
1927	return ret;
1928}
1929
1930/*
1931 * Receive data from transport
1932 * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
1933 * return: total bytes read, or 0. SMB Direct will not do partial read.
1934 */
1935int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
1936{
1937	char *buf;
1938	struct page *page;
1939	unsigned int to_read, page_offset;
1940	int rc;
1941
1942	if (iov_iter_rw(&msg->msg_iter) == WRITE) {
1943		/* It's a bug in upper layer to get there */
1944		cifs_dbg(VFS, "Invalid msg iter dir %u\n",
1945			 iov_iter_rw(&msg->msg_iter));
1946		rc = -EINVAL;
1947		goto out;
1948	}
1949
1950	switch (iov_iter_type(&msg->msg_iter)) {
1951	case ITER_KVEC:
1952		buf = msg->msg_iter.kvec->iov_base;
1953		to_read = msg->msg_iter.kvec->iov_len;
1954		rc = smbd_recv_buf(info, buf, to_read);
1955		break;
1956
1957	case ITER_BVEC:
1958		page = msg->msg_iter.bvec->bv_page;
1959		page_offset = msg->msg_iter.bvec->bv_offset;
1960		to_read = msg->msg_iter.bvec->bv_len;
1961		rc = smbd_recv_page(info, page, page_offset, to_read);
1962		break;
1963
1964	default:
1965		/* It's a bug in upper layer to get there */
1966		cifs_dbg(VFS, "Invalid msg type %d\n",
1967			 iov_iter_type(&msg->msg_iter));
1968		rc = -EINVAL;
1969	}
1970
1971out:
1972	/* SMBDirect will read it all or nothing */
1973	if (rc > 0)
1974		msg->msg_iter.count = 0;
1975	return rc;
1976}
1977
1978/*
1979 * Send data to transport
1980 * Each rqst is transported as a SMBDirect payload
1981 * rqst: the data to write
1982 * return value: 0 if successfully write, otherwise error code
1983 */
1984int smbd_send(struct TCP_Server_Info *server,
1985	int num_rqst, struct smb_rqst *rqst_array)
1986{
1987	struct smbd_connection *info = server->smbd_conn;
1988	struct kvec vecs[SMBDIRECT_MAX_SEND_SGE - 1];
1989	int nvecs;
1990	int size;
1991	unsigned int buflen, remaining_data_length;
1992	unsigned int offset, remaining_vec_data_length;
1993	int start, i, j;
1994	int max_iov_size =
1995		info->max_send_size - sizeof(struct smbd_data_transfer);
1996	struct kvec *iov;
1997	int rc;
1998	struct smb_rqst *rqst;
1999	int rqst_idx;
2000
2001	if (info->transport_status != SMBD_CONNECTED)
2002		return -EAGAIN;
 
 
2003
2004	/*
2005	 * Add in the page array if there is one. The caller needs to set
2006	 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
2007	 * ends at page boundary
2008	 */
2009	remaining_data_length = 0;
2010	for (i = 0; i < num_rqst; i++)
2011		remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
2012
2013	if (unlikely(remaining_data_length > info->max_fragmented_send_size)) {
2014		/* assertion: payload never exceeds negotiated maximum */
2015		log_write(ERR, "payload size %d > max size %d\n",
2016			remaining_data_length, info->max_fragmented_send_size);
2017		return -EINVAL;
 
2018	}
2019
2020	log_write(INFO, "num_rqst=%d total length=%u\n",
2021			num_rqst, remaining_data_length);
2022
2023	rqst_idx = 0;
2024	do {
2025		rqst = &rqst_array[rqst_idx];
2026		iov = rqst->rq_iov;
2027
2028		cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
2029			rqst_idx, smb_rqst_len(server, rqst));
2030		remaining_vec_data_length = 0;
2031		for (i = 0; i < rqst->rq_nvec; i++) {
2032			remaining_vec_data_length += iov[i].iov_len;
2033			dump_smb(iov[i].iov_base, iov[i].iov_len);
2034		}
2035
2036		log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d rq_tailsz=%d buflen=%lu\n",
2037			  rqst_idx, rqst->rq_nvec,
2038			  rqst->rq_npages, rqst->rq_pagesz,
2039			  rqst->rq_tailsz, smb_rqst_len(server, rqst));
2040
2041		start = 0;
2042		offset = 0;
2043		do {
2044			buflen = 0;
2045			i = start;
2046			j = 0;
2047			while (i < rqst->rq_nvec &&
2048				j < SMBDIRECT_MAX_SEND_SGE - 1 &&
2049				buflen < max_iov_size) {
2050
2051				vecs[j].iov_base = iov[i].iov_base + offset;
2052				if (buflen + iov[i].iov_len > max_iov_size) {
2053					vecs[j].iov_len =
2054						max_iov_size - iov[i].iov_len;
2055					buflen = max_iov_size;
2056					offset = vecs[j].iov_len;
2057				} else {
2058					vecs[j].iov_len =
2059						iov[i].iov_len - offset;
2060					buflen += vecs[j].iov_len;
2061					offset = 0;
2062					++i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2063				}
2064				++j;
 
 
2065			}
2066
2067			remaining_vec_data_length -= buflen;
2068			remaining_data_length -= buflen;
2069			log_write(INFO, "sending %s iov[%d] from start=%d nvecs=%d remaining_data_length=%d\n",
2070					remaining_vec_data_length > 0 ?
2071						"partial" : "complete",
2072					rqst->rq_nvec, start, j,
2073					remaining_data_length);
2074
2075			start = i;
2076			rc = smbd_post_send_data(info, vecs, j, remaining_data_length);
2077			if (rc)
2078				goto done;
2079		} while (remaining_vec_data_length > 0);
2080
2081		/* now sending pages if there are any */
2082		for (i = 0; i < rqst->rq_npages; i++) {
2083			rqst_page_get_length(rqst, i, &buflen, &offset);
2084			nvecs = (buflen + max_iov_size - 1) / max_iov_size;
2085			log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
2086				buflen, nvecs);
2087			for (j = 0; j < nvecs; j++) {
2088				size = min_t(unsigned int, max_iov_size, remaining_data_length);
2089				remaining_data_length -= size;
2090				log_write(INFO, "sending pages i=%d offset=%d size=%d remaining_data_length=%d\n",
2091					  i, j * max_iov_size + offset, size,
2092					  remaining_data_length);
2093				rc = smbd_post_send_page(
2094					info, rqst->rq_pages[i],
2095					j*max_iov_size + offset,
2096					size, remaining_data_length);
2097				if (rc)
2098					goto done;
 
2099			}
2100		}
2101	} while (++rqst_idx < num_rqst);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2102
2103done:
2104	/*
2105	 * As an optimization, we don't wait for individual I/O to finish
2106	 * before sending the next one.
2107	 * Send them all and wait for pending send count to get to 0
2108	 * that means all the I/Os have been out and we are good to return
2109	 */
2110
2111	wait_event(info->wait_send_pending,
2112		atomic_read(&info->send_pending) == 0);
2113
2114	return rc;
2115}
2116
2117static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
2118{
2119	struct smbd_mr *mr;
2120	struct ib_cqe *cqe;
2121
2122	if (wc->status) {
2123		log_rdma_mr(ERR, "status=%d\n", wc->status);
2124		cqe = wc->wr_cqe;
2125		mr = container_of(cqe, struct smbd_mr, cqe);
2126		smbd_disconnect_rdma_connection(mr->conn);
2127	}
2128}
2129
2130/*
2131 * The work queue function that recovers MRs
2132 * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
2133 * again. Both calls are slow, so finish them in a workqueue. This will not
2134 * block I/O path.
2135 * There is one workqueue that recovers MRs, there is no need to lock as the
2136 * I/O requests calling smbd_register_mr will never update the links in the
2137 * mr_list.
2138 */
2139static void smbd_mr_recovery_work(struct work_struct *work)
2140{
2141	struct smbd_connection *info =
2142		container_of(work, struct smbd_connection, mr_recovery_work);
2143	struct smbd_mr *smbdirect_mr;
2144	int rc;
2145
2146	list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
2147		if (smbdirect_mr->state == MR_ERROR) {
 
 
 
 
 
2148
2149			/* recover this MR entry */
2150			rc = ib_dereg_mr(smbdirect_mr->mr);
2151			if (rc) {
2152				log_rdma_mr(ERR,
2153					"ib_dereg_mr failed rc=%x\n",
2154					rc);
2155				smbd_disconnect_rdma_connection(info);
2156				continue;
2157			}
2158
2159			smbdirect_mr->mr = ib_alloc_mr(
2160				info->pd, info->mr_type,
2161				info->max_frmr_depth);
2162			if (IS_ERR(smbdirect_mr->mr)) {
2163				log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x max_frmr_depth=%x\n",
2164					    info->mr_type,
2165					    info->max_frmr_depth);
 
 
2166				smbd_disconnect_rdma_connection(info);
2167				continue;
2168			}
2169		} else
2170			/* This MR is being used, don't recover it */
2171			continue;
2172
2173		smbdirect_mr->state = MR_READY;
2174
2175		/* smbdirect_mr->state is updated by this function
2176		 * and is read and updated by I/O issuing CPUs trying
2177		 * to get a MR, the call to atomic_inc_return
2178		 * implicates a memory barrier and guarantees this
2179		 * value is updated before waking up any calls to
2180		 * get_mr() from the I/O issuing CPUs
2181		 */
2182		if (atomic_inc_return(&info->mr_ready_count) == 1)
2183			wake_up_interruptible(&info->wait_mr);
2184	}
2185}
2186
2187static void destroy_mr_list(struct smbd_connection *info)
2188{
2189	struct smbd_mr *mr, *tmp;
2190
2191	cancel_work_sync(&info->mr_recovery_work);
2192	list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
2193		if (mr->state == MR_INVALIDATED)
2194			ib_dma_unmap_sg(info->id->device, mr->sgl,
2195				mr->sgl_count, mr->dir);
2196		ib_dereg_mr(mr->mr);
2197		kfree(mr->sgl);
2198		kfree(mr);
2199	}
2200}
2201
2202/*
2203 * Allocate MRs used for RDMA read/write
2204 * The number of MRs will not exceed hardware capability in responder_resources
2205 * All MRs are kept in mr_list. The MR can be recovered after it's used
2206 * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
2207 * as MRs are used and recovered for I/O, but the list links will not change
2208 */
2209static int allocate_mr_list(struct smbd_connection *info)
2210{
2211	int i;
2212	struct smbd_mr *smbdirect_mr, *tmp;
2213
2214	INIT_LIST_HEAD(&info->mr_list);
2215	init_waitqueue_head(&info->wait_mr);
2216	spin_lock_init(&info->mr_list_lock);
2217	atomic_set(&info->mr_ready_count, 0);
2218	atomic_set(&info->mr_used_count, 0);
2219	init_waitqueue_head(&info->wait_for_mr_cleanup);
2220	/* Allocate more MRs (2x) than hardware responder_resources */
2221	for (i = 0; i < info->responder_resources * 2; i++) {
2222		smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
2223		if (!smbdirect_mr)
2224			goto out;
2225		smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
2226					info->max_frmr_depth);
2227		if (IS_ERR(smbdirect_mr->mr)) {
2228			log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x max_frmr_depth=%x\n",
2229				    info->mr_type, info->max_frmr_depth);
 
2230			goto out;
2231		}
2232		smbdirect_mr->sgl = kcalloc(
2233					info->max_frmr_depth,
2234					sizeof(struct scatterlist),
2235					GFP_KERNEL);
2236		if (!smbdirect_mr->sgl) {
2237			log_rdma_mr(ERR, "failed to allocate sgl\n");
2238			ib_dereg_mr(smbdirect_mr->mr);
2239			goto out;
2240		}
2241		smbdirect_mr->state = MR_READY;
2242		smbdirect_mr->conn = info;
2243
2244		list_add_tail(&smbdirect_mr->list, &info->mr_list);
2245		atomic_inc(&info->mr_ready_count);
2246	}
2247	INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
2248	return 0;
2249
2250out:
2251	kfree(smbdirect_mr);
2252
2253	list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
2254		ib_dereg_mr(smbdirect_mr->mr);
2255		kfree(smbdirect_mr->sgl);
2256		kfree(smbdirect_mr);
2257	}
2258	return -ENOMEM;
2259}
2260
2261/*
2262 * Get a MR from mr_list. This function waits until there is at least one
2263 * MR available in the list. It may access the list while the
2264 * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
2265 * as they never modify the same places. However, there may be several CPUs
2266 * issueing I/O trying to get MR at the same time, mr_list_lock is used to
2267 * protect this situation.
2268 */
2269static struct smbd_mr *get_mr(struct smbd_connection *info)
2270{
2271	struct smbd_mr *ret;
2272	int rc;
2273again:
2274	rc = wait_event_interruptible(info->wait_mr,
2275		atomic_read(&info->mr_ready_count) ||
2276		info->transport_status != SMBD_CONNECTED);
2277	if (rc) {
2278		log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
2279		return NULL;
2280	}
2281
2282	if (info->transport_status != SMBD_CONNECTED) {
2283		log_rdma_mr(ERR, "info->transport_status=%x\n",
2284			info->transport_status);
2285		return NULL;
2286	}
2287
2288	spin_lock(&info->mr_list_lock);
2289	list_for_each_entry(ret, &info->mr_list, list) {
2290		if (ret->state == MR_READY) {
2291			ret->state = MR_REGISTERED;
2292			spin_unlock(&info->mr_list_lock);
2293			atomic_dec(&info->mr_ready_count);
2294			atomic_inc(&info->mr_used_count);
2295			return ret;
2296		}
2297	}
2298
2299	spin_unlock(&info->mr_list_lock);
2300	/*
2301	 * It is possible that we could fail to get MR because other processes may
2302	 * try to acquire a MR at the same time. If this is the case, retry it.
2303	 */
2304	goto again;
2305}
2306
2307/*
2308 * Register memory for RDMA read/write
2309 * pages[]: the list of pages to register memory with
2310 * num_pages: the number of pages to register
2311 * tailsz: if non-zero, the bytes to register in the last page
2312 * writing: true if this is a RDMA write (SMB read), false for RDMA read
2313 * need_invalidate: true if this MR needs to be locally invalidated after I/O
2314 * return value: the MR registered, NULL if failed.
2315 */
2316struct smbd_mr *smbd_register_mr(
2317	struct smbd_connection *info, struct page *pages[], int num_pages,
2318	int offset, int tailsz, bool writing, bool need_invalidate)
2319{
2320	struct smbd_mr *smbdirect_mr;
2321	int rc, i;
2322	enum dma_data_direction dir;
2323	struct ib_reg_wr *reg_wr;
2324
2325	if (num_pages > info->max_frmr_depth) {
2326		log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
2327			num_pages, info->max_frmr_depth);
2328		return NULL;
2329	}
2330
2331	smbdirect_mr = get_mr(info);
2332	if (!smbdirect_mr) {
2333		log_rdma_mr(ERR, "get_mr returning NULL\n");
2334		return NULL;
2335	}
2336	smbdirect_mr->need_invalidate = need_invalidate;
2337	smbdirect_mr->sgl_count = num_pages;
2338	sg_init_table(smbdirect_mr->sgl, num_pages);
2339
2340	log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
2341			num_pages, offset, tailsz);
2342
2343	if (num_pages == 1) {
2344		sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
2345		goto skip_multiple_pages;
2346	}
2347
2348	/* We have at least two pages to register */
2349	sg_set_page(
2350		&smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
2351	i = 1;
2352	while (i < num_pages - 1) {
2353		sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
2354		i++;
2355	}
2356	sg_set_page(&smbdirect_mr->sgl[i], pages[i],
2357		tailsz ? tailsz : PAGE_SIZE, 0);
2358
2359skip_multiple_pages:
2360	dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2361	smbdirect_mr->dir = dir;
2362	rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
2363	if (!rc) {
2364		log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
2365			num_pages, dir, rc);
2366		goto dma_map_error;
2367	}
2368
2369	rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
2370		NULL, PAGE_SIZE);
2371	if (rc != num_pages) {
2372		log_rdma_mr(ERR,
2373			"ib_map_mr_sg failed rc = %d num_pages = %x\n",
2374			rc, num_pages);
2375		goto map_mr_error;
2376	}
2377
2378	ib_update_fast_reg_key(smbdirect_mr->mr,
2379		ib_inc_rkey(smbdirect_mr->mr->rkey));
2380	reg_wr = &smbdirect_mr->wr;
2381	reg_wr->wr.opcode = IB_WR_REG_MR;
2382	smbdirect_mr->cqe.done = register_mr_done;
2383	reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
2384	reg_wr->wr.num_sge = 0;
2385	reg_wr->wr.send_flags = IB_SEND_SIGNALED;
2386	reg_wr->mr = smbdirect_mr->mr;
2387	reg_wr->key = smbdirect_mr->mr->rkey;
2388	reg_wr->access = writing ?
2389			IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2390			IB_ACCESS_REMOTE_READ;
2391
2392	/*
2393	 * There is no need for waiting for complemtion on ib_post_send
2394	 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
2395	 * on the next ib_post_send when we actaully send I/O to remote peer
2396	 */
2397	rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
2398	if (!rc)
2399		return smbdirect_mr;
2400
2401	log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
2402		rc, reg_wr->key);
2403
2404	/* If all failed, attempt to recover this MR by setting it MR_ERROR*/
2405map_mr_error:
2406	ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
2407		smbdirect_mr->sgl_count, smbdirect_mr->dir);
2408
2409dma_map_error:
2410	smbdirect_mr->state = MR_ERROR;
2411	if (atomic_dec_and_test(&info->mr_used_count))
2412		wake_up(&info->wait_for_mr_cleanup);
2413
2414	smbd_disconnect_rdma_connection(info);
2415
2416	return NULL;
2417}
2418
2419static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
2420{
2421	struct smbd_mr *smbdirect_mr;
2422	struct ib_cqe *cqe;
2423
2424	cqe = wc->wr_cqe;
2425	smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
2426	smbdirect_mr->state = MR_INVALIDATED;
2427	if (wc->status != IB_WC_SUCCESS) {
2428		log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
2429		smbdirect_mr->state = MR_ERROR;
2430	}
2431	complete(&smbdirect_mr->invalidate_done);
2432}
2433
2434/*
2435 * Deregister a MR after I/O is done
2436 * This function may wait if remote invalidation is not used
2437 * and we have to locally invalidate the buffer to prevent data is being
2438 * modified by remote peer after upper layer consumes it
2439 */
2440int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
2441{
2442	struct ib_send_wr *wr;
2443	struct smbd_connection *info = smbdirect_mr->conn;
2444	int rc = 0;
2445
2446	if (smbdirect_mr->need_invalidate) {
2447		/* Need to finish local invalidation before returning */
2448		wr = &smbdirect_mr->inv_wr;
2449		wr->opcode = IB_WR_LOCAL_INV;
2450		smbdirect_mr->cqe.done = local_inv_done;
2451		wr->wr_cqe = &smbdirect_mr->cqe;
2452		wr->num_sge = 0;
2453		wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
2454		wr->send_flags = IB_SEND_SIGNALED;
2455
2456		init_completion(&smbdirect_mr->invalidate_done);
2457		rc = ib_post_send(info->id->qp, wr, NULL);
2458		if (rc) {
2459			log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
2460			smbd_disconnect_rdma_connection(info);
2461			goto done;
2462		}
2463		wait_for_completion(&smbdirect_mr->invalidate_done);
2464		smbdirect_mr->need_invalidate = false;
2465	} else
2466		/*
2467		 * For remote invalidation, just set it to MR_INVALIDATED
2468		 * and defer to mr_recovery_work to recover the MR for next use
2469		 */
2470		smbdirect_mr->state = MR_INVALIDATED;
2471
2472	if (smbdirect_mr->state == MR_INVALIDATED) {
2473		ib_dma_unmap_sg(
2474			info->id->device, smbdirect_mr->sgl,
2475			smbdirect_mr->sgl_count,
2476			smbdirect_mr->dir);
2477		smbdirect_mr->state = MR_READY;
2478		if (atomic_inc_return(&info->mr_ready_count) == 1)
2479			wake_up_interruptible(&info->wait_mr);
2480	} else
2481		/*
2482		 * Schedule the work to do MR recovery for future I/Os MR
2483		 * recovery is slow and don't want it to block current I/O
2484		 */
2485		queue_work(info->workqueue, &info->mr_recovery_work);
2486
2487done:
2488	if (atomic_dec_and_test(&info->mr_used_count))
2489		wake_up(&info->wait_for_mr_cleanup);
2490
2491	return rc;
2492}
v5.4
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *   Copyright (C) 2017, Microsoft Corporation.
   4 *
   5 *   Author(s): Long Li <longli@microsoft.com>
   6 */
   7#include <linux/module.h>
   8#include <linux/highmem.h>
   9#include "smbdirect.h"
  10#include "cifs_debug.h"
  11#include "cifsproto.h"
  12#include "smb2proto.h"
  13
  14static struct smbd_response *get_empty_queue_buffer(
  15		struct smbd_connection *info);
  16static struct smbd_response *get_receive_buffer(
  17		struct smbd_connection *info);
  18static void put_receive_buffer(
  19		struct smbd_connection *info,
  20		struct smbd_response *response);
  21static int allocate_receive_buffers(struct smbd_connection *info, int num_buf);
  22static void destroy_receive_buffers(struct smbd_connection *info);
  23
  24static void put_empty_packet(
  25		struct smbd_connection *info, struct smbd_response *response);
  26static void enqueue_reassembly(
  27		struct smbd_connection *info,
  28		struct smbd_response *response, int data_length);
  29static struct smbd_response *_get_first_reassembly(
  30		struct smbd_connection *info);
  31
  32static int smbd_post_recv(
  33		struct smbd_connection *info,
  34		struct smbd_response *response);
  35
  36static int smbd_post_send_empty(struct smbd_connection *info);
  37static int smbd_post_send_data(
  38		struct smbd_connection *info,
  39		struct kvec *iov, int n_vec, int remaining_data_length);
  40static int smbd_post_send_page(struct smbd_connection *info,
  41		struct page *page, unsigned long offset,
  42		size_t size, int remaining_data_length);
  43
  44static void destroy_mr_list(struct smbd_connection *info);
  45static int allocate_mr_list(struct smbd_connection *info);
  46
  47/* SMBD version number */
  48#define SMBD_V1	0x0100
  49
  50/* Port numbers for SMBD transport */
  51#define SMB_PORT	445
  52#define SMBD_PORT	5445
  53
  54/* Address lookup and resolve timeout in ms */
  55#define RDMA_RESOLVE_TIMEOUT	5000
  56
  57/* SMBD negotiation timeout in seconds */
  58#define SMBD_NEGOTIATE_TIMEOUT	120
  59
  60/* SMBD minimum receive size and fragmented sized defined in [MS-SMBD] */
  61#define SMBD_MIN_RECEIVE_SIZE		128
  62#define SMBD_MIN_FRAGMENTED_SIZE	131072
  63
  64/*
  65 * Default maximum number of RDMA read/write outstanding on this connection
  66 * This value is possibly decreased during QP creation on hardware limit
  67 */
  68#define SMBD_CM_RESPONDER_RESOURCES	32
  69
  70/* Maximum number of retries on data transfer operations */
  71#define SMBD_CM_RETRY			6
  72/* No need to retry on Receiver Not Ready since SMBD manages credits */
  73#define SMBD_CM_RNR_RETRY		0
  74
  75/*
  76 * User configurable initial values per SMBD transport connection
  77 * as defined in [MS-SMBD] 3.1.1.1
  78 * Those may change after a SMBD negotiation
  79 */
  80/* The local peer's maximum number of credits to grant to the peer */
  81int smbd_receive_credit_max = 255;
  82
  83/* The remote peer's credit request of local peer */
  84int smbd_send_credit_target = 255;
  85
  86/* The maximum single message size can be sent to remote peer */
  87int smbd_max_send_size = 1364;
  88
  89/*  The maximum fragmented upper-layer payload receive size supported */
  90int smbd_max_fragmented_recv_size = 1024 * 1024;
  91
  92/*  The maximum single-message size which can be received */
  93int smbd_max_receive_size = 8192;
  94
  95/* The timeout to initiate send of a keepalive message on idle */
  96int smbd_keep_alive_interval = 120;
  97
  98/*
  99 * User configurable initial values for RDMA transport
 100 * The actual values used may be lower and are limited to hardware capabilities
 101 */
 102/* Default maximum number of SGEs in a RDMA write/read */
 103int smbd_max_frmr_depth = 2048;
 104
 105/* If payload is less than this byte, use RDMA send/recv not read/write */
 106int rdma_readwrite_threshold = 4096;
 107
 108/* Transport logging functions
 109 * Logging are defined as classes. They can be OR'ed to define the actual
 110 * logging level via module parameter smbd_logging_class
 111 * e.g. cifs.smbd_logging_class=0xa0 will log all log_rdma_recv() and
 112 * log_rdma_event()
 113 */
 114#define LOG_OUTGOING			0x1
 115#define LOG_INCOMING			0x2
 116#define LOG_READ			0x4
 117#define LOG_WRITE			0x8
 118#define LOG_RDMA_SEND			0x10
 119#define LOG_RDMA_RECV			0x20
 120#define LOG_KEEP_ALIVE			0x40
 121#define LOG_RDMA_EVENT			0x80
 122#define LOG_RDMA_MR			0x100
 123static unsigned int smbd_logging_class;
 124module_param(smbd_logging_class, uint, 0644);
 125MODULE_PARM_DESC(smbd_logging_class,
 126	"Logging class for SMBD transport 0x0 to 0x100");
 127
 128#define ERR		0x0
 129#define INFO		0x1
 130static unsigned int smbd_logging_level = ERR;
 131module_param(smbd_logging_level, uint, 0644);
 132MODULE_PARM_DESC(smbd_logging_level,
 133	"Logging level for SMBD transport, 0 (default): error, 1: info");
 134
 135#define log_rdma(level, class, fmt, args...)				\
 136do {									\
 137	if (level <= smbd_logging_level || class & smbd_logging_class)	\
 138		cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
 139} while (0)
 140
 141#define log_outgoing(level, fmt, args...) \
 142		log_rdma(level, LOG_OUTGOING, fmt, ##args)
 143#define log_incoming(level, fmt, args...) \
 144		log_rdma(level, LOG_INCOMING, fmt, ##args)
 145#define log_read(level, fmt, args...)	log_rdma(level, LOG_READ, fmt, ##args)
 146#define log_write(level, fmt, args...)	log_rdma(level, LOG_WRITE, fmt, ##args)
 147#define log_rdma_send(level, fmt, args...) \
 148		log_rdma(level, LOG_RDMA_SEND, fmt, ##args)
 149#define log_rdma_recv(level, fmt, args...) \
 150		log_rdma(level, LOG_RDMA_RECV, fmt, ##args)
 151#define log_keep_alive(level, fmt, args...) \
 152		log_rdma(level, LOG_KEEP_ALIVE, fmt, ##args)
 153#define log_rdma_event(level, fmt, args...) \
 154		log_rdma(level, LOG_RDMA_EVENT, fmt, ##args)
 155#define log_rdma_mr(level, fmt, args...) \
 156		log_rdma(level, LOG_RDMA_MR, fmt, ##args)
 157
 158static void smbd_disconnect_rdma_work(struct work_struct *work)
 159{
 160	struct smbd_connection *info =
 161		container_of(work, struct smbd_connection, disconnect_work);
 162
 163	if (info->transport_status == SMBD_CONNECTED) {
 164		info->transport_status = SMBD_DISCONNECTING;
 165		rdma_disconnect(info->id);
 166	}
 167}
 168
 169static void smbd_disconnect_rdma_connection(struct smbd_connection *info)
 170{
 171	queue_work(info->workqueue, &info->disconnect_work);
 172}
 173
 174/* Upcall from RDMA CM */
 175static int smbd_conn_upcall(
 176		struct rdma_cm_id *id, struct rdma_cm_event *event)
 177{
 178	struct smbd_connection *info = id->context;
 179
 180	log_rdma_event(INFO, "event=%d status=%d\n",
 181		event->event, event->status);
 182
 183	switch (event->event) {
 184	case RDMA_CM_EVENT_ADDR_RESOLVED:
 185	case RDMA_CM_EVENT_ROUTE_RESOLVED:
 186		info->ri_rc = 0;
 187		complete(&info->ri_done);
 188		break;
 189
 190	case RDMA_CM_EVENT_ADDR_ERROR:
 191		info->ri_rc = -EHOSTUNREACH;
 192		complete(&info->ri_done);
 193		break;
 194
 195	case RDMA_CM_EVENT_ROUTE_ERROR:
 196		info->ri_rc = -ENETUNREACH;
 197		complete(&info->ri_done);
 198		break;
 199
 200	case RDMA_CM_EVENT_ESTABLISHED:
 201		log_rdma_event(INFO, "connected event=%d\n", event->event);
 202		info->transport_status = SMBD_CONNECTED;
 203		wake_up_interruptible(&info->conn_wait);
 204		break;
 205
 206	case RDMA_CM_EVENT_CONNECT_ERROR:
 207	case RDMA_CM_EVENT_UNREACHABLE:
 208	case RDMA_CM_EVENT_REJECTED:
 209		log_rdma_event(INFO, "connecting failed event=%d\n", event->event);
 210		info->transport_status = SMBD_DISCONNECTED;
 211		wake_up_interruptible(&info->conn_wait);
 212		break;
 213
 214	case RDMA_CM_EVENT_DEVICE_REMOVAL:
 215	case RDMA_CM_EVENT_DISCONNECTED:
 216		/* This happenes when we fail the negotiation */
 217		if (info->transport_status == SMBD_NEGOTIATE_FAILED) {
 218			info->transport_status = SMBD_DISCONNECTED;
 219			wake_up(&info->conn_wait);
 220			break;
 221		}
 222
 223		info->transport_status = SMBD_DISCONNECTED;
 224		wake_up_interruptible(&info->disconn_wait);
 225		wake_up_interruptible(&info->wait_reassembly_queue);
 226		wake_up_interruptible_all(&info->wait_send_queue);
 227		break;
 228
 229	default:
 230		break;
 231	}
 232
 233	return 0;
 234}
 235
 236/* Upcall from RDMA QP */
 237static void
 238smbd_qp_async_error_upcall(struct ib_event *event, void *context)
 239{
 240	struct smbd_connection *info = context;
 241
 242	log_rdma_event(ERR, "%s on device %s info %p\n",
 243		ib_event_msg(event->event), event->device->name, info);
 244
 245	switch (event->event) {
 246	case IB_EVENT_CQ_ERR:
 247	case IB_EVENT_QP_FATAL:
 248		smbd_disconnect_rdma_connection(info);
 
 249
 250	default:
 251		break;
 252	}
 253}
 254
 255static inline void *smbd_request_payload(struct smbd_request *request)
 256{
 257	return (void *)request->packet;
 258}
 259
 260static inline void *smbd_response_payload(struct smbd_response *response)
 261{
 262	return (void *)response->packet;
 263}
 264
 265/* Called when a RDMA send is done */
 266static void send_done(struct ib_cq *cq, struct ib_wc *wc)
 267{
 268	int i;
 269	struct smbd_request *request =
 270		container_of(wc->wr_cqe, struct smbd_request, cqe);
 271
 272	log_rdma_send(INFO, "smbd_request %p completed wc->status=%d\n",
 273		request, wc->status);
 274
 275	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
 276		log_rdma_send(ERR, "wc->status=%d wc->opcode=%d\n",
 277			wc->status, wc->opcode);
 278		smbd_disconnect_rdma_connection(request->info);
 279	}
 280
 281	for (i = 0; i < request->num_sge; i++)
 282		ib_dma_unmap_single(request->info->id->device,
 283			request->sge[i].addr,
 284			request->sge[i].length,
 285			DMA_TO_DEVICE);
 286
 287	if (request->has_payload) {
 288		if (atomic_dec_and_test(&request->info->send_payload_pending))
 289			wake_up(&request->info->wait_send_payload_pending);
 290	} else {
 291		if (atomic_dec_and_test(&request->info->send_pending))
 292			wake_up(&request->info->wait_send_pending);
 293	}
 294
 295	mempool_free(request, request->info->request_mempool);
 296}
 297
 298static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
 299{
 300	log_rdma_event(INFO, "resp message min_version %u max_version %u "
 301		"negotiated_version %u credits_requested %u "
 302		"credits_granted %u status %u max_readwrite_size %u "
 303		"preferred_send_size %u max_receive_size %u "
 304		"max_fragmented_size %u\n",
 305		resp->min_version, resp->max_version, resp->negotiated_version,
 306		resp->credits_requested, resp->credits_granted, resp->status,
 307		resp->max_readwrite_size, resp->preferred_send_size,
 308		resp->max_receive_size, resp->max_fragmented_size);
 309}
 310
 311/*
 312 * Process a negotiation response message, according to [MS-SMBD]3.1.5.7
 313 * response, packet_length: the negotiation response message
 314 * return value: true if negotiation is a success, false if failed
 315 */
 316static bool process_negotiation_response(
 317		struct smbd_response *response, int packet_length)
 318{
 319	struct smbd_connection *info = response->info;
 320	struct smbd_negotiate_resp *packet = smbd_response_payload(response);
 321
 322	if (packet_length < sizeof(struct smbd_negotiate_resp)) {
 323		log_rdma_event(ERR,
 324			"error: packet_length=%d\n", packet_length);
 325		return false;
 326	}
 327
 328	if (le16_to_cpu(packet->negotiated_version) != SMBD_V1) {
 329		log_rdma_event(ERR, "error: negotiated_version=%x\n",
 330			le16_to_cpu(packet->negotiated_version));
 331		return false;
 332	}
 333	info->protocol = le16_to_cpu(packet->negotiated_version);
 334
 335	if (packet->credits_requested == 0) {
 336		log_rdma_event(ERR, "error: credits_requested==0\n");
 337		return false;
 338	}
 339	info->receive_credit_target = le16_to_cpu(packet->credits_requested);
 340
 341	if (packet->credits_granted == 0) {
 342		log_rdma_event(ERR, "error: credits_granted==0\n");
 343		return false;
 344	}
 345	atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
 346
 347	atomic_set(&info->receive_credits, 0);
 348
 349	if (le32_to_cpu(packet->preferred_send_size) > info->max_receive_size) {
 350		log_rdma_event(ERR, "error: preferred_send_size=%d\n",
 351			le32_to_cpu(packet->preferred_send_size));
 352		return false;
 353	}
 354	info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
 355
 356	if (le32_to_cpu(packet->max_receive_size) < SMBD_MIN_RECEIVE_SIZE) {
 357		log_rdma_event(ERR, "error: max_receive_size=%d\n",
 358			le32_to_cpu(packet->max_receive_size));
 359		return false;
 360	}
 361	info->max_send_size = min_t(int, info->max_send_size,
 362					le32_to_cpu(packet->max_receive_size));
 363
 364	if (le32_to_cpu(packet->max_fragmented_size) <
 365			SMBD_MIN_FRAGMENTED_SIZE) {
 366		log_rdma_event(ERR, "error: max_fragmented_size=%d\n",
 367			le32_to_cpu(packet->max_fragmented_size));
 368		return false;
 369	}
 370	info->max_fragmented_send_size =
 371		le32_to_cpu(packet->max_fragmented_size);
 372	info->rdma_readwrite_threshold =
 373		rdma_readwrite_threshold > info->max_fragmented_send_size ?
 374		info->max_fragmented_send_size :
 375		rdma_readwrite_threshold;
 376
 377
 378	info->max_readwrite_size = min_t(u32,
 379			le32_to_cpu(packet->max_readwrite_size),
 380			info->max_frmr_depth * PAGE_SIZE);
 381	info->max_frmr_depth = info->max_readwrite_size / PAGE_SIZE;
 382
 383	return true;
 384}
 385
 386/*
 387 * Check and schedule to send an immediate packet
 388 * This is used to extend credtis to remote peer to keep the transport busy
 389 */
 390static void check_and_send_immediate(struct smbd_connection *info)
 391{
 392	if (info->transport_status != SMBD_CONNECTED)
 393		return;
 394
 395	info->send_immediate = true;
 396
 397	/*
 398	 * Promptly send a packet if our peer is running low on receive
 399	 * credits
 400	 */
 401	if (atomic_read(&info->receive_credits) <
 402		info->receive_credit_target - 1)
 403		queue_delayed_work(
 404			info->workqueue, &info->send_immediate_work, 0);
 405}
 406
 407static void smbd_post_send_credits(struct work_struct *work)
 408{
 409	int ret = 0;
 410	int use_receive_queue = 1;
 411	int rc;
 412	struct smbd_response *response;
 413	struct smbd_connection *info =
 414		container_of(work, struct smbd_connection,
 415			post_send_credits_work);
 416
 417	if (info->transport_status != SMBD_CONNECTED) {
 418		wake_up(&info->wait_receive_queues);
 419		return;
 420	}
 421
 422	if (info->receive_credit_target >
 423		atomic_read(&info->receive_credits)) {
 424		while (true) {
 425			if (use_receive_queue)
 426				response = get_receive_buffer(info);
 427			else
 428				response = get_empty_queue_buffer(info);
 429			if (!response) {
 430				/* now switch to emtpy packet queue */
 431				if (use_receive_queue) {
 432					use_receive_queue = 0;
 433					continue;
 434				} else
 435					break;
 436			}
 437
 438			response->type = SMBD_TRANSFER_DATA;
 439			response->first_segment = false;
 440			rc = smbd_post_recv(info, response);
 441			if (rc) {
 442				log_rdma_recv(ERR,
 443					"post_recv failed rc=%d\n", rc);
 444				put_receive_buffer(info, response);
 445				break;
 446			}
 447
 448			ret++;
 449		}
 450	}
 451
 452	spin_lock(&info->lock_new_credits_offered);
 453	info->new_credits_offered += ret;
 454	spin_unlock(&info->lock_new_credits_offered);
 455
 456	atomic_add(ret, &info->receive_credits);
 457
 458	/* Check if we can post new receive and grant credits to peer */
 459	check_and_send_immediate(info);
 460}
 461
 462static void smbd_recv_done_work(struct work_struct *work)
 463{
 464	struct smbd_connection *info =
 465		container_of(work, struct smbd_connection, recv_done_work);
 466
 467	/*
 468	 * We may have new send credits granted from remote peer
 469	 * If any sender is blcoked on lack of credets, unblock it
 470	 */
 471	if (atomic_read(&info->send_credits))
 472		wake_up_interruptible(&info->wait_send_queue);
 473
 474	/*
 475	 * Check if we need to send something to remote peer to
 476	 * grant more credits or respond to KEEP_ALIVE packet
 477	 */
 478	check_and_send_immediate(info);
 479}
 480
 481/* Called from softirq, when recv is done */
 482static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
 483{
 484	struct smbd_data_transfer *data_transfer;
 485	struct smbd_response *response =
 486		container_of(wc->wr_cqe, struct smbd_response, cqe);
 487	struct smbd_connection *info = response->info;
 488	int data_length = 0;
 489
 490	log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d "
 491		      "byte_len=%d pkey_index=%x\n",
 492		response, response->type, wc->status, wc->opcode,
 493		wc->byte_len, wc->pkey_index);
 494
 495	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
 496		log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
 497			wc->status, wc->opcode);
 498		smbd_disconnect_rdma_connection(info);
 499		goto error;
 500	}
 501
 502	ib_dma_sync_single_for_cpu(
 503		wc->qp->device,
 504		response->sge.addr,
 505		response->sge.length,
 506		DMA_FROM_DEVICE);
 507
 508	switch (response->type) {
 509	/* SMBD negotiation response */
 510	case SMBD_NEGOTIATE_RESP:
 511		dump_smbd_negotiate_resp(smbd_response_payload(response));
 512		info->full_packet_received = true;
 513		info->negotiate_done =
 514			process_negotiation_response(response, wc->byte_len);
 515		complete(&info->negotiate_completion);
 516		break;
 517
 518	/* SMBD data transfer packet */
 519	case SMBD_TRANSFER_DATA:
 520		data_transfer = smbd_response_payload(response);
 521		data_length = le32_to_cpu(data_transfer->data_length);
 522
 523		/*
 524		 * If this is a packet with data playload place the data in
 525		 * reassembly queue and wake up the reading thread
 526		 */
 527		if (data_length) {
 528			if (info->full_packet_received)
 529				response->first_segment = true;
 530
 531			if (le32_to_cpu(data_transfer->remaining_data_length))
 532				info->full_packet_received = false;
 533			else
 534				info->full_packet_received = true;
 535
 536			enqueue_reassembly(
 537				info,
 538				response,
 539				data_length);
 540		} else
 541			put_empty_packet(info, response);
 542
 543		if (data_length)
 544			wake_up_interruptible(&info->wait_reassembly_queue);
 545
 546		atomic_dec(&info->receive_credits);
 547		info->receive_credit_target =
 548			le16_to_cpu(data_transfer->credits_requested);
 549		atomic_add(le16_to_cpu(data_transfer->credits_granted),
 550			&info->send_credits);
 
 
 
 
 
 
 
 551
 552		log_incoming(INFO, "data flags %d data_offset %d "
 553			"data_length %d remaining_data_length %d\n",
 554			le16_to_cpu(data_transfer->flags),
 555			le32_to_cpu(data_transfer->data_offset),
 556			le32_to_cpu(data_transfer->data_length),
 557			le32_to_cpu(data_transfer->remaining_data_length));
 558
 559		/* Send a KEEP_ALIVE response right away if requested */
 560		info->keep_alive_requested = KEEP_ALIVE_NONE;
 561		if (le16_to_cpu(data_transfer->flags) &
 562				SMB_DIRECT_RESPONSE_REQUESTED) {
 563			info->keep_alive_requested = KEEP_ALIVE_PENDING;
 564		}
 565
 566		queue_work(info->workqueue, &info->recv_done_work);
 567		return;
 568
 569	default:
 570		log_rdma_recv(ERR,
 571			"unexpected response type=%d\n", response->type);
 572	}
 573
 574error:
 575	put_receive_buffer(info, response);
 576}
 577
 578static struct rdma_cm_id *smbd_create_id(
 579		struct smbd_connection *info,
 580		struct sockaddr *dstaddr, int port)
 581{
 582	struct rdma_cm_id *id;
 583	int rc;
 584	__be16 *sport;
 585
 586	id = rdma_create_id(&init_net, smbd_conn_upcall, info,
 587		RDMA_PS_TCP, IB_QPT_RC);
 588	if (IS_ERR(id)) {
 589		rc = PTR_ERR(id);
 590		log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
 591		return id;
 592	}
 593
 594	if (dstaddr->sa_family == AF_INET6)
 595		sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
 596	else
 597		sport = &((struct sockaddr_in *)dstaddr)->sin_port;
 598
 599	*sport = htons(port);
 600
 601	init_completion(&info->ri_done);
 602	info->ri_rc = -ETIMEDOUT;
 603
 604	rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
 605		RDMA_RESOLVE_TIMEOUT);
 606	if (rc) {
 607		log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
 608		goto out;
 609	}
 610	wait_for_completion_interruptible_timeout(
 611		&info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
 
 
 
 
 
 612	rc = info->ri_rc;
 613	if (rc) {
 614		log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
 615		goto out;
 616	}
 617
 618	info->ri_rc = -ETIMEDOUT;
 619	rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
 620	if (rc) {
 621		log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
 622		goto out;
 623	}
 624	wait_for_completion_interruptible_timeout(
 625		&info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
 
 
 
 
 
 626	rc = info->ri_rc;
 627	if (rc) {
 628		log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
 629		goto out;
 630	}
 631
 632	return id;
 633
 634out:
 635	rdma_destroy_id(id);
 636	return ERR_PTR(rc);
 637}
 638
 639/*
 640 * Test if FRWR (Fast Registration Work Requests) is supported on the device
 641 * This implementation requries FRWR on RDMA read/write
 642 * return value: true if it is supported
 643 */
 644static bool frwr_is_supported(struct ib_device_attr *attrs)
 645{
 646	if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
 647		return false;
 648	if (attrs->max_fast_reg_page_list_len == 0)
 649		return false;
 650	return true;
 651}
 652
 653static int smbd_ia_open(
 654		struct smbd_connection *info,
 655		struct sockaddr *dstaddr, int port)
 656{
 657	int rc;
 658
 659	info->id = smbd_create_id(info, dstaddr, port);
 660	if (IS_ERR(info->id)) {
 661		rc = PTR_ERR(info->id);
 662		goto out1;
 663	}
 664
 665	if (!frwr_is_supported(&info->id->device->attrs)) {
 666		log_rdma_event(ERR,
 667			"Fast Registration Work Requests "
 668			"(FRWR) is not supported\n");
 669		log_rdma_event(ERR,
 670			"Device capability flags = %llx "
 671			"max_fast_reg_page_list_len = %u\n",
 672			info->id->device->attrs.device_cap_flags,
 673			info->id->device->attrs.max_fast_reg_page_list_len);
 674		rc = -EPROTONOSUPPORT;
 675		goto out2;
 676	}
 677	info->max_frmr_depth = min_t(int,
 678		smbd_max_frmr_depth,
 679		info->id->device->attrs.max_fast_reg_page_list_len);
 680	info->mr_type = IB_MR_TYPE_MEM_REG;
 681	if (info->id->device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
 682		info->mr_type = IB_MR_TYPE_SG_GAPS;
 683
 684	info->pd = ib_alloc_pd(info->id->device, 0);
 685	if (IS_ERR(info->pd)) {
 686		rc = PTR_ERR(info->pd);
 687		log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
 688		goto out2;
 689	}
 690
 691	return 0;
 692
 693out2:
 694	rdma_destroy_id(info->id);
 695	info->id = NULL;
 696
 697out1:
 698	return rc;
 699}
 700
 701/*
 702 * Send a negotiation request message to the peer
 703 * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
 704 * After negotiation, the transport is connected and ready for
 705 * carrying upper layer SMB payload
 706 */
 707static int smbd_post_send_negotiate_req(struct smbd_connection *info)
 708{
 709	struct ib_send_wr send_wr;
 710	int rc = -ENOMEM;
 711	struct smbd_request *request;
 712	struct smbd_negotiate_req *packet;
 713
 714	request = mempool_alloc(info->request_mempool, GFP_KERNEL);
 715	if (!request)
 716		return rc;
 717
 718	request->info = info;
 719
 720	packet = smbd_request_payload(request);
 721	packet->min_version = cpu_to_le16(SMBD_V1);
 722	packet->max_version = cpu_to_le16(SMBD_V1);
 723	packet->reserved = 0;
 724	packet->credits_requested = cpu_to_le16(info->send_credit_target);
 725	packet->preferred_send_size = cpu_to_le32(info->max_send_size);
 726	packet->max_receive_size = cpu_to_le32(info->max_receive_size);
 727	packet->max_fragmented_size =
 728		cpu_to_le32(info->max_fragmented_recv_size);
 729
 730	request->num_sge = 1;
 731	request->sge[0].addr = ib_dma_map_single(
 732				info->id->device, (void *)packet,
 733				sizeof(*packet), DMA_TO_DEVICE);
 734	if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
 735		rc = -EIO;
 736		goto dma_mapping_failed;
 737	}
 738
 739	request->sge[0].length = sizeof(*packet);
 740	request->sge[0].lkey = info->pd->local_dma_lkey;
 741
 742	ib_dma_sync_single_for_device(
 743		info->id->device, request->sge[0].addr,
 744		request->sge[0].length, DMA_TO_DEVICE);
 745
 746	request->cqe.done = send_done;
 747
 748	send_wr.next = NULL;
 749	send_wr.wr_cqe = &request->cqe;
 750	send_wr.sg_list = request->sge;
 751	send_wr.num_sge = request->num_sge;
 752	send_wr.opcode = IB_WR_SEND;
 753	send_wr.send_flags = IB_SEND_SIGNALED;
 754
 755	log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
 756		request->sge[0].addr,
 757		request->sge[0].length, request->sge[0].lkey);
 758
 759	request->has_payload = false;
 760	atomic_inc(&info->send_pending);
 761	rc = ib_post_send(info->id->qp, &send_wr, NULL);
 762	if (!rc)
 763		return 0;
 764
 765	/* if we reach here, post send failed */
 766	log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
 767	atomic_dec(&info->send_pending);
 768	ib_dma_unmap_single(info->id->device, request->sge[0].addr,
 769		request->sge[0].length, DMA_TO_DEVICE);
 770
 771	smbd_disconnect_rdma_connection(info);
 772
 773dma_mapping_failed:
 774	mempool_free(request, info->request_mempool);
 775	return rc;
 776}
 777
 778/*
 779 * Extend the credits to remote peer
 780 * This implements [MS-SMBD] 3.1.5.9
 781 * The idea is that we should extend credits to remote peer as quickly as
 782 * it's allowed, to maintain data flow. We allocate as much receive
 783 * buffer as possible, and extend the receive credits to remote peer
 784 * return value: the new credtis being granted.
 785 */
 786static int manage_credits_prior_sending(struct smbd_connection *info)
 787{
 788	int new_credits;
 789
 790	spin_lock(&info->lock_new_credits_offered);
 791	new_credits = info->new_credits_offered;
 792	info->new_credits_offered = 0;
 793	spin_unlock(&info->lock_new_credits_offered);
 794
 795	return new_credits;
 796}
 797
 798/*
 799 * Check if we need to send a KEEP_ALIVE message
 800 * The idle connection timer triggers a KEEP_ALIVE message when expires
 801 * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
 802 * back a response.
 803 * return value:
 804 * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
 805 * 0: otherwise
 806 */
 807static int manage_keep_alive_before_sending(struct smbd_connection *info)
 808{
 809	if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
 810		info->keep_alive_requested = KEEP_ALIVE_SENT;
 811		return 1;
 812	}
 813	return 0;
 814}
 815
 816/*
 817 * Build and prepare the SMBD packet header
 818 * This function waits for avaialbe send credits and build a SMBD packet
 819 * header. The caller then optional append payload to the packet after
 820 * the header
 821 * intput values
 822 * size: the size of the payload
 823 * remaining_data_length: remaining data to send if this is part of a
 824 * fragmented packet
 825 * output values
 826 * request_out: the request allocated from this function
 827 * return values: 0 on success, otherwise actual error code returned
 828 */
 829static int smbd_create_header(struct smbd_connection *info,
 830		int size, int remaining_data_length,
 831		struct smbd_request **request_out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 832{
 
 
 
 833	struct smbd_request *request;
 834	struct smbd_data_transfer *packet;
 835	int header_length;
 836	int rc;
 837
 
 838	/* Wait for send credits. A SMBD packet needs one credit */
 839	rc = wait_event_interruptible(info->wait_send_queue,
 840		atomic_read(&info->send_credits) > 0 ||
 841		info->transport_status != SMBD_CONNECTED);
 842	if (rc)
 843		return rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 844
 845	if (info->transport_status != SMBD_CONNECTED) {
 846		log_outgoing(ERR, "disconnected not sending\n");
 847		return -EAGAIN;
 
 
 
 
 
 
 
 848	}
 849	atomic_dec(&info->send_credits);
 850
 851	request = mempool_alloc(info->request_mempool, GFP_KERNEL);
 852	if (!request) {
 853		rc = -ENOMEM;
 854		goto err;
 855	}
 856
 857	request->info = info;
 858
 859	/* Fill in the packet header */
 860	packet = smbd_request_payload(request);
 861	packet->credits_requested = cpu_to_le16(info->send_credit_target);
 862	packet->credits_granted =
 863		cpu_to_le16(manage_credits_prior_sending(info));
 
 
 
 864	info->send_immediate = false;
 865
 866	packet->flags = 0;
 867	if (manage_keep_alive_before_sending(info))
 868		packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
 869
 870	packet->reserved = 0;
 871	if (!size)
 872		packet->data_offset = 0;
 873	else
 874		packet->data_offset = cpu_to_le32(24);
 875	packet->data_length = cpu_to_le32(size);
 876	packet->remaining_data_length = cpu_to_le32(remaining_data_length);
 877	packet->padding = 0;
 878
 879	log_outgoing(INFO, "credits_requested=%d credits_granted=%d "
 880		"data_offset=%d data_length=%d remaining_data_length=%d\n",
 881		le16_to_cpu(packet->credits_requested),
 882		le16_to_cpu(packet->credits_granted),
 883		le32_to_cpu(packet->data_offset),
 884		le32_to_cpu(packet->data_length),
 885		le32_to_cpu(packet->remaining_data_length));
 886
 887	/* Map the packet to DMA */
 888	header_length = sizeof(struct smbd_data_transfer);
 889	/* If this is a packet without payload, don't send padding */
 890	if (!size)
 891		header_length = offsetof(struct smbd_data_transfer, padding);
 892
 893	request->num_sge = 1;
 894	request->sge[0].addr = ib_dma_map_single(info->id->device,
 895						 (void *)packet,
 896						 header_length,
 897						 DMA_TO_DEVICE);
 898	if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
 899		mempool_free(request, info->request_mempool);
 900		rc = -EIO;
 901		goto err;
 
 902	}
 903
 904	request->sge[0].length = header_length;
 905	request->sge[0].lkey = info->pd->local_dma_lkey;
 906
 907	*request_out = request;
 908	return 0;
 909
 910err:
 911	atomic_inc(&info->send_credits);
 912	return rc;
 913}
 914
 915static void smbd_destroy_header(struct smbd_connection *info,
 916		struct smbd_request *request)
 917{
 918
 919	ib_dma_unmap_single(info->id->device,
 920			    request->sge[0].addr,
 921			    request->sge[0].length,
 922			    DMA_TO_DEVICE);
 923	mempool_free(request, info->request_mempool);
 924	atomic_inc(&info->send_credits);
 925}
 926
 927/* Post the send request */
 928static int smbd_post_send(struct smbd_connection *info,
 929		struct smbd_request *request, bool has_payload)
 930{
 931	struct ib_send_wr send_wr;
 932	int rc, i;
 933
 934	for (i = 0; i < request->num_sge; i++) {
 935		log_rdma_send(INFO,
 936			"rdma_request sge[%d] addr=%llu length=%u\n",
 937			i, request->sge[i].addr, request->sge[i].length);
 938		ib_dma_sync_single_for_device(
 939			info->id->device,
 940			request->sge[i].addr,
 941			request->sge[i].length,
 942			DMA_TO_DEVICE);
 943	}
 944
 945	request->cqe.done = send_done;
 946
 947	send_wr.next = NULL;
 948	send_wr.wr_cqe = &request->cqe;
 949	send_wr.sg_list = request->sge;
 950	send_wr.num_sge = request->num_sge;
 951	send_wr.opcode = IB_WR_SEND;
 952	send_wr.send_flags = IB_SEND_SIGNALED;
 953
 954	if (has_payload) {
 955		request->has_payload = true;
 956		atomic_inc(&info->send_payload_pending);
 957	} else {
 958		request->has_payload = false;
 959		atomic_inc(&info->send_pending);
 960	}
 961
 962	rc = ib_post_send(info->id->qp, &send_wr, NULL);
 963	if (rc) {
 964		log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
 965		if (has_payload) {
 966			if (atomic_dec_and_test(&info->send_payload_pending))
 967				wake_up(&info->wait_send_payload_pending);
 968		} else {
 969			if (atomic_dec_and_test(&info->send_pending))
 970				wake_up(&info->wait_send_pending);
 971		}
 972		smbd_disconnect_rdma_connection(info);
 973		rc = -EAGAIN;
 974	} else
 975		/* Reset timer for idle connection after packet is sent */
 976		mod_delayed_work(info->workqueue, &info->idle_timer_work,
 977			info->keep_alive_interval*HZ);
 978
 979	return rc;
 980}
 981
 982static int smbd_post_send_sgl(struct smbd_connection *info,
 983	struct scatterlist *sgl, int data_length, int remaining_data_length)
 984{
 985	int num_sgs;
 986	int i, rc;
 987	struct smbd_request *request;
 988	struct scatterlist *sg;
 989
 990	rc = smbd_create_header(
 991		info, data_length, remaining_data_length, &request);
 992	if (rc)
 993		return rc;
 994
 995	num_sgs = sgl ? sg_nents(sgl) : 0;
 996	for_each_sg(sgl, sg, num_sgs, i) {
 997		request->sge[i+1].addr =
 998			ib_dma_map_page(info->id->device, sg_page(sg),
 999			       sg->offset, sg->length, DMA_TO_DEVICE);
1000		if (ib_dma_mapping_error(
1001				info->id->device, request->sge[i+1].addr)) {
1002			rc = -EIO;
1003			request->sge[i+1].addr = 0;
1004			goto dma_mapping_failure;
1005		}
1006		request->sge[i+1].length = sg->length;
1007		request->sge[i+1].lkey = info->pd->local_dma_lkey;
1008		request->num_sge++;
1009	}
1010
1011	rc = smbd_post_send(info, request, data_length);
1012	if (!rc)
1013		return 0;
1014
1015dma_mapping_failure:
1016	for (i = 1; i < request->num_sge; i++)
1017		if (request->sge[i].addr)
1018			ib_dma_unmap_single(info->id->device,
1019					    request->sge[i].addr,
1020					    request->sge[i].length,
1021					    DMA_TO_DEVICE);
1022	smbd_destroy_header(info, request);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1023	return rc;
1024}
1025
1026/*
1027 * Send a page
1028 * page: the page to send
1029 * offset: offset in the page to send
1030 * size: length in the page to send
1031 * remaining_data_length: remaining data to send in this payload
1032 */
1033static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
1034		unsigned long offset, size_t size, int remaining_data_length)
1035{
1036	struct scatterlist sgl;
1037
1038	sg_init_table(&sgl, 1);
1039	sg_set_page(&sgl, page, size, offset);
1040
1041	return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
1042}
1043
1044/*
1045 * Send an empty message
1046 * Empty message is used to extend credits to peer to for keep live
1047 * while there is no upper layer payload to send at the time
1048 */
1049static int smbd_post_send_empty(struct smbd_connection *info)
1050{
1051	info->count_send_empty++;
1052	return smbd_post_send_sgl(info, NULL, 0, 0);
1053}
1054
1055/*
1056 * Send a data buffer
1057 * iov: the iov array describing the data buffers
1058 * n_vec: number of iov array
1059 * remaining_data_length: remaining data to send following this packet
1060 * in segmented SMBD packet
1061 */
1062static int smbd_post_send_data(
1063	struct smbd_connection *info, struct kvec *iov, int n_vec,
1064	int remaining_data_length)
1065{
1066	int i;
1067	u32 data_length = 0;
1068	struct scatterlist sgl[SMBDIRECT_MAX_SGE];
1069
1070	if (n_vec > SMBDIRECT_MAX_SGE) {
1071		cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
1072		return -ENOMEM;
1073	}
1074
1075	sg_init_table(sgl, n_vec);
1076	for (i = 0; i < n_vec; i++) {
1077		data_length += iov[i].iov_len;
1078		sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
1079	}
1080
1081	return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
1082}
1083
1084/*
1085 * Post a receive request to the transport
1086 * The remote peer can only send data when a receive request is posted
1087 * The interaction is controlled by send/receive credit system
1088 */
1089static int smbd_post_recv(
1090		struct smbd_connection *info, struct smbd_response *response)
1091{
1092	struct ib_recv_wr recv_wr;
1093	int rc = -EIO;
1094
1095	response->sge.addr = ib_dma_map_single(
1096				info->id->device, response->packet,
1097				info->max_receive_size, DMA_FROM_DEVICE);
1098	if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1099		return rc;
1100
1101	response->sge.length = info->max_receive_size;
1102	response->sge.lkey = info->pd->local_dma_lkey;
1103
1104	response->cqe.done = recv_done;
1105
1106	recv_wr.wr_cqe = &response->cqe;
1107	recv_wr.next = NULL;
1108	recv_wr.sg_list = &response->sge;
1109	recv_wr.num_sge = 1;
1110
1111	rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
1112	if (rc) {
1113		ib_dma_unmap_single(info->id->device, response->sge.addr,
1114				    response->sge.length, DMA_FROM_DEVICE);
1115		smbd_disconnect_rdma_connection(info);
1116		log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1117	}
1118
1119	return rc;
1120}
1121
1122/* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1123static int smbd_negotiate(struct smbd_connection *info)
1124{
1125	int rc;
1126	struct smbd_response *response = get_receive_buffer(info);
1127
1128	response->type = SMBD_NEGOTIATE_RESP;
1129	rc = smbd_post_recv(info, response);
1130	log_rdma_event(INFO,
1131		"smbd_post_recv rc=%d iov.addr=%llx iov.length=%x "
1132		"iov.lkey=%x\n",
1133		rc, response->sge.addr,
1134		response->sge.length, response->sge.lkey);
1135	if (rc)
1136		return rc;
1137
1138	init_completion(&info->negotiate_completion);
1139	info->negotiate_done = false;
1140	rc = smbd_post_send_negotiate_req(info);
1141	if (rc)
1142		return rc;
1143
1144	rc = wait_for_completion_interruptible_timeout(
1145		&info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1146	log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1147
1148	if (info->negotiate_done)
1149		return 0;
1150
1151	if (rc == 0)
1152		rc = -ETIMEDOUT;
1153	else if (rc == -ERESTARTSYS)
1154		rc = -EINTR;
1155	else
1156		rc = -ENOTCONN;
1157
1158	return rc;
1159}
1160
1161static void put_empty_packet(
1162		struct smbd_connection *info, struct smbd_response *response)
1163{
1164	spin_lock(&info->empty_packet_queue_lock);
1165	list_add_tail(&response->list, &info->empty_packet_queue);
1166	info->count_empty_packet_queue++;
1167	spin_unlock(&info->empty_packet_queue_lock);
1168
1169	queue_work(info->workqueue, &info->post_send_credits_work);
1170}
1171
1172/*
1173 * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1174 * This is a queue for reassembling upper layer payload and present to upper
1175 * layer. All the inncoming payload go to the reassembly queue, regardless of
1176 * if reassembly is required. The uuper layer code reads from the queue for all
1177 * incoming payloads.
1178 * Put a received packet to the reassembly queue
1179 * response: the packet received
1180 * data_length: the size of payload in this packet
1181 */
1182static void enqueue_reassembly(
1183	struct smbd_connection *info,
1184	struct smbd_response *response,
1185	int data_length)
1186{
1187	spin_lock(&info->reassembly_queue_lock);
1188	list_add_tail(&response->list, &info->reassembly_queue);
1189	info->reassembly_queue_length++;
1190	/*
1191	 * Make sure reassembly_data_length is updated after list and
1192	 * reassembly_queue_length are updated. On the dequeue side
1193	 * reassembly_data_length is checked without a lock to determine
1194	 * if reassembly_queue_length and list is up to date
1195	 */
1196	virt_wmb();
1197	info->reassembly_data_length += data_length;
1198	spin_unlock(&info->reassembly_queue_lock);
1199	info->count_reassembly_queue++;
1200	info->count_enqueue_reassembly_queue++;
1201}
1202
1203/*
1204 * Get the first entry at the front of reassembly queue
1205 * Caller is responsible for locking
1206 * return value: the first entry if any, NULL if queue is empty
1207 */
1208static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1209{
1210	struct smbd_response *ret = NULL;
1211
1212	if (!list_empty(&info->reassembly_queue)) {
1213		ret = list_first_entry(
1214			&info->reassembly_queue,
1215			struct smbd_response, list);
1216	}
1217	return ret;
1218}
1219
1220static struct smbd_response *get_empty_queue_buffer(
1221		struct smbd_connection *info)
1222{
1223	struct smbd_response *ret = NULL;
1224	unsigned long flags;
1225
1226	spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1227	if (!list_empty(&info->empty_packet_queue)) {
1228		ret = list_first_entry(
1229			&info->empty_packet_queue,
1230			struct smbd_response, list);
1231		list_del(&ret->list);
1232		info->count_empty_packet_queue--;
1233	}
1234	spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1235
1236	return ret;
1237}
1238
1239/*
1240 * Get a receive buffer
1241 * For each remote send, we need to post a receive. The receive buffers are
1242 * pre-allocated in advance.
1243 * return value: the receive buffer, NULL if none is available
1244 */
1245static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1246{
1247	struct smbd_response *ret = NULL;
1248	unsigned long flags;
1249
1250	spin_lock_irqsave(&info->receive_queue_lock, flags);
1251	if (!list_empty(&info->receive_queue)) {
1252		ret = list_first_entry(
1253			&info->receive_queue,
1254			struct smbd_response, list);
1255		list_del(&ret->list);
1256		info->count_receive_queue--;
1257		info->count_get_receive_buffer++;
1258	}
1259	spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1260
1261	return ret;
1262}
1263
1264/*
1265 * Return a receive buffer
1266 * Upon returning of a receive buffer, we can post new receive and extend
1267 * more receive credits to remote peer. This is done immediately after a
1268 * receive buffer is returned.
1269 */
1270static void put_receive_buffer(
1271	struct smbd_connection *info, struct smbd_response *response)
1272{
1273	unsigned long flags;
1274
1275	ib_dma_unmap_single(info->id->device, response->sge.addr,
1276		response->sge.length, DMA_FROM_DEVICE);
1277
1278	spin_lock_irqsave(&info->receive_queue_lock, flags);
1279	list_add_tail(&response->list, &info->receive_queue);
1280	info->count_receive_queue++;
1281	info->count_put_receive_buffer++;
1282	spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1283
1284	queue_work(info->workqueue, &info->post_send_credits_work);
1285}
1286
1287/* Preallocate all receive buffer on transport establishment */
1288static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1289{
1290	int i;
1291	struct smbd_response *response;
1292
1293	INIT_LIST_HEAD(&info->reassembly_queue);
1294	spin_lock_init(&info->reassembly_queue_lock);
1295	info->reassembly_data_length = 0;
1296	info->reassembly_queue_length = 0;
1297
1298	INIT_LIST_HEAD(&info->receive_queue);
1299	spin_lock_init(&info->receive_queue_lock);
1300	info->count_receive_queue = 0;
1301
1302	INIT_LIST_HEAD(&info->empty_packet_queue);
1303	spin_lock_init(&info->empty_packet_queue_lock);
1304	info->count_empty_packet_queue = 0;
1305
1306	init_waitqueue_head(&info->wait_receive_queues);
1307
1308	for (i = 0; i < num_buf; i++) {
1309		response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1310		if (!response)
1311			goto allocate_failed;
1312
1313		response->info = info;
1314		list_add_tail(&response->list, &info->receive_queue);
1315		info->count_receive_queue++;
1316	}
1317
1318	return 0;
1319
1320allocate_failed:
1321	while (!list_empty(&info->receive_queue)) {
1322		response = list_first_entry(
1323				&info->receive_queue,
1324				struct smbd_response, list);
1325		list_del(&response->list);
1326		info->count_receive_queue--;
1327
1328		mempool_free(response, info->response_mempool);
1329	}
1330	return -ENOMEM;
1331}
1332
1333static void destroy_receive_buffers(struct smbd_connection *info)
1334{
1335	struct smbd_response *response;
1336
1337	while ((response = get_receive_buffer(info)))
1338		mempool_free(response, info->response_mempool);
1339
1340	while ((response = get_empty_queue_buffer(info)))
1341		mempool_free(response, info->response_mempool);
1342}
1343
1344/*
1345 * Check and send an immediate or keep alive packet
1346 * The condition to send those packets are defined in [MS-SMBD] 3.1.1.1
1347 * Connection.KeepaliveRequested and Connection.SendImmediate
1348 * The idea is to extend credits to server as soon as it becomes available
1349 */
1350static void send_immediate_work(struct work_struct *work)
1351{
1352	struct smbd_connection *info = container_of(
1353					work, struct smbd_connection,
1354					send_immediate_work.work);
1355
1356	if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
1357	    info->send_immediate) {
1358		log_keep_alive(INFO, "send an empty message\n");
1359		smbd_post_send_empty(info);
1360	}
1361}
1362
1363/* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1364static void idle_connection_timer(struct work_struct *work)
1365{
1366	struct smbd_connection *info = container_of(
1367					work, struct smbd_connection,
1368					idle_timer_work.work);
1369
1370	if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1371		log_keep_alive(ERR,
1372			"error status info->keep_alive_requested=%d\n",
1373			info->keep_alive_requested);
1374		smbd_disconnect_rdma_connection(info);
1375		return;
1376	}
1377
1378	log_keep_alive(INFO, "about to send an empty idle message\n");
1379	smbd_post_send_empty(info);
1380
1381	/* Setup the next idle timeout work */
1382	queue_delayed_work(info->workqueue, &info->idle_timer_work,
1383			info->keep_alive_interval*HZ);
1384}
1385
1386/*
1387 * Destroy the transport and related RDMA and memory resources
1388 * Need to go through all the pending counters and make sure on one is using
1389 * the transport while it is destroyed
1390 */
1391void smbd_destroy(struct TCP_Server_Info *server)
1392{
1393	struct smbd_connection *info = server->smbd_conn;
1394	struct smbd_response *response;
1395	unsigned long flags;
1396
1397	if (!info) {
1398		log_rdma_event(INFO, "rdma session already destroyed\n");
1399		return;
1400	}
1401
1402	log_rdma_event(INFO, "destroying rdma session\n");
1403	if (info->transport_status != SMBD_DISCONNECTED) {
1404		rdma_disconnect(server->smbd_conn->id);
1405		log_rdma_event(INFO, "wait for transport being disconnected\n");
1406		wait_event_interruptible(
1407			info->disconn_wait,
1408			info->transport_status == SMBD_DISCONNECTED);
1409	}
1410
1411	log_rdma_event(INFO, "destroying qp\n");
1412	ib_drain_qp(info->id->qp);
1413	rdma_destroy_qp(info->id);
1414
1415	log_rdma_event(INFO, "cancelling idle timer\n");
1416	cancel_delayed_work_sync(&info->idle_timer_work);
1417	log_rdma_event(INFO, "cancelling send immediate work\n");
1418	cancel_delayed_work_sync(&info->send_immediate_work);
1419
1420	log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
1421	wait_event(info->wait_send_pending,
1422		atomic_read(&info->send_pending) == 0);
1423	wait_event(info->wait_send_payload_pending,
1424		atomic_read(&info->send_payload_pending) == 0);
1425
1426	/* It's not posssible for upper layer to get to reassembly */
1427	log_rdma_event(INFO, "drain the reassembly queue\n");
1428	do {
1429		spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1430		response = _get_first_reassembly(info);
1431		if (response) {
1432			list_del(&response->list);
1433			spin_unlock_irqrestore(
1434				&info->reassembly_queue_lock, flags);
1435			put_receive_buffer(info, response);
1436		} else
1437			spin_unlock_irqrestore(
1438				&info->reassembly_queue_lock, flags);
1439	} while (response);
1440	info->reassembly_data_length = 0;
1441
1442	log_rdma_event(INFO, "free receive buffers\n");
1443	wait_event(info->wait_receive_queues,
1444		info->count_receive_queue + info->count_empty_packet_queue
1445			== info->receive_credit_max);
1446	destroy_receive_buffers(info);
1447
1448	/*
1449	 * For performance reasons, memory registration and deregistration
1450	 * are not locked by srv_mutex. It is possible some processes are
1451	 * blocked on transport srv_mutex while holding memory registration.
1452	 * Release the transport srv_mutex to allow them to hit the failure
1453	 * path when sending data, and then release memory registartions.
1454	 */
1455	log_rdma_event(INFO, "freeing mr list\n");
1456	wake_up_interruptible_all(&info->wait_mr);
1457	while (atomic_read(&info->mr_used_count)) {
1458		mutex_unlock(&server->srv_mutex);
1459		msleep(1000);
1460		mutex_lock(&server->srv_mutex);
1461	}
1462	destroy_mr_list(info);
1463
1464	ib_free_cq(info->send_cq);
1465	ib_free_cq(info->recv_cq);
1466	ib_dealloc_pd(info->pd);
1467	rdma_destroy_id(info->id);
1468
1469	/* free mempools */
1470	mempool_destroy(info->request_mempool);
1471	kmem_cache_destroy(info->request_cache);
1472
1473	mempool_destroy(info->response_mempool);
1474	kmem_cache_destroy(info->response_cache);
1475
1476	info->transport_status = SMBD_DESTROYED;
1477
1478	destroy_workqueue(info->workqueue);
 
1479	kfree(info);
 
1480}
1481
1482/*
1483 * Reconnect this SMBD connection, called from upper layer
1484 * return value: 0 on success, or actual error code
1485 */
1486int smbd_reconnect(struct TCP_Server_Info *server)
1487{
1488	log_rdma_event(INFO, "reconnecting rdma session\n");
1489
1490	if (!server->smbd_conn) {
1491		log_rdma_event(INFO, "rdma session already destroyed\n");
1492		goto create_conn;
1493	}
1494
1495	/*
1496	 * This is possible if transport is disconnected and we haven't received
1497	 * notification from RDMA, but upper layer has detected timeout
1498	 */
1499	if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1500		log_rdma_event(INFO, "disconnecting transport\n");
1501		smbd_destroy(server);
1502	}
1503
1504create_conn:
1505	log_rdma_event(INFO, "creating rdma session\n");
1506	server->smbd_conn = smbd_get_connection(
1507		server, (struct sockaddr *) &server->dstaddr);
1508	log_rdma_event(INFO, "created rdma session info=%p\n",
1509		server->smbd_conn);
 
1510
1511	return server->smbd_conn ? 0 : -ENOENT;
1512}
1513
1514static void destroy_caches_and_workqueue(struct smbd_connection *info)
1515{
1516	destroy_receive_buffers(info);
1517	destroy_workqueue(info->workqueue);
1518	mempool_destroy(info->response_mempool);
1519	kmem_cache_destroy(info->response_cache);
1520	mempool_destroy(info->request_mempool);
1521	kmem_cache_destroy(info->request_cache);
1522}
1523
1524#define MAX_NAME_LEN	80
1525static int allocate_caches_and_workqueue(struct smbd_connection *info)
1526{
1527	char name[MAX_NAME_LEN];
1528	int rc;
1529
1530	scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
1531	info->request_cache =
1532		kmem_cache_create(
1533			name,
1534			sizeof(struct smbd_request) +
1535				sizeof(struct smbd_data_transfer),
1536			0, SLAB_HWCACHE_ALIGN, NULL);
1537	if (!info->request_cache)
1538		return -ENOMEM;
1539
1540	info->request_mempool =
1541		mempool_create(info->send_credit_target, mempool_alloc_slab,
1542			mempool_free_slab, info->request_cache);
1543	if (!info->request_mempool)
1544		goto out1;
1545
1546	scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
1547	info->response_cache =
1548		kmem_cache_create(
1549			name,
1550			sizeof(struct smbd_response) +
1551				info->max_receive_size,
1552			0, SLAB_HWCACHE_ALIGN, NULL);
1553	if (!info->response_cache)
1554		goto out2;
1555
1556	info->response_mempool =
1557		mempool_create(info->receive_credit_max, mempool_alloc_slab,
1558		       mempool_free_slab, info->response_cache);
1559	if (!info->response_mempool)
1560		goto out3;
1561
1562	scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
1563	info->workqueue = create_workqueue(name);
1564	if (!info->workqueue)
1565		goto out4;
1566
1567	rc = allocate_receive_buffers(info, info->receive_credit_max);
1568	if (rc) {
1569		log_rdma_event(ERR, "failed to allocate receive buffers\n");
1570		goto out5;
1571	}
1572
1573	return 0;
1574
1575out5:
1576	destroy_workqueue(info->workqueue);
1577out4:
1578	mempool_destroy(info->response_mempool);
1579out3:
1580	kmem_cache_destroy(info->response_cache);
1581out2:
1582	mempool_destroy(info->request_mempool);
1583out1:
1584	kmem_cache_destroy(info->request_cache);
1585	return -ENOMEM;
1586}
1587
1588/* Create a SMBD connection, called by upper layer */
1589static struct smbd_connection *_smbd_get_connection(
1590	struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1591{
1592	int rc;
1593	struct smbd_connection *info;
1594	struct rdma_conn_param conn_param;
1595	struct ib_qp_init_attr qp_attr;
1596	struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
1597	struct ib_port_immutable port_immutable;
1598	u32 ird_ord_hdr[2];
1599
1600	info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1601	if (!info)
1602		return NULL;
1603
1604	info->transport_status = SMBD_CONNECTING;
1605	rc = smbd_ia_open(info, dstaddr, port);
1606	if (rc) {
1607		log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1608		goto create_id_failed;
1609	}
1610
1611	if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1612	    smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1613		log_rdma_event(ERR,
1614			"consider lowering send_credit_target = %d. "
1615			"Possible CQE overrun, device "
1616			"reporting max_cpe %d max_qp_wr %d\n",
1617			smbd_send_credit_target,
1618			info->id->device->attrs.max_cqe,
1619			info->id->device->attrs.max_qp_wr);
1620		goto config_failed;
1621	}
1622
1623	if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1624	    smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1625		log_rdma_event(ERR,
1626			"consider lowering receive_credit_max = %d. "
1627			"Possible CQE overrun, device "
1628			"reporting max_cpe %d max_qp_wr %d\n",
1629			smbd_receive_credit_max,
1630			info->id->device->attrs.max_cqe,
1631			info->id->device->attrs.max_qp_wr);
1632		goto config_failed;
1633	}
1634
1635	info->receive_credit_max = smbd_receive_credit_max;
1636	info->send_credit_target = smbd_send_credit_target;
1637	info->max_send_size = smbd_max_send_size;
1638	info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1639	info->max_receive_size = smbd_max_receive_size;
1640	info->keep_alive_interval = smbd_keep_alive_interval;
1641
1642	if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
 
1643		log_rdma_event(ERR,
1644			"warning: device max_send_sge = %d too small\n",
1645			info->id->device->attrs.max_send_sge);
1646		log_rdma_event(ERR, "Queue Pair creation may fail\n");
1647	}
1648	if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
1649		log_rdma_event(ERR,
1650			"warning: device max_recv_sge = %d too small\n",
1651			info->id->device->attrs.max_recv_sge);
1652		log_rdma_event(ERR, "Queue Pair creation may fail\n");
1653	}
1654
1655	info->send_cq = NULL;
1656	info->recv_cq = NULL;
1657	info->send_cq =
1658		ib_alloc_cq_any(info->id->device, info,
1659				info->send_credit_target, IB_POLL_SOFTIRQ);
1660	if (IS_ERR(info->send_cq)) {
1661		info->send_cq = NULL;
1662		goto alloc_cq_failed;
1663	}
1664
1665	info->recv_cq =
1666		ib_alloc_cq_any(info->id->device, info,
1667				info->receive_credit_max, IB_POLL_SOFTIRQ);
1668	if (IS_ERR(info->recv_cq)) {
1669		info->recv_cq = NULL;
1670		goto alloc_cq_failed;
1671	}
1672
1673	memset(&qp_attr, 0, sizeof(qp_attr));
1674	qp_attr.event_handler = smbd_qp_async_error_upcall;
1675	qp_attr.qp_context = info;
1676	qp_attr.cap.max_send_wr = info->send_credit_target;
1677	qp_attr.cap.max_recv_wr = info->receive_credit_max;
1678	qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
1679	qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
1680	qp_attr.cap.max_inline_data = 0;
1681	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1682	qp_attr.qp_type = IB_QPT_RC;
1683	qp_attr.send_cq = info->send_cq;
1684	qp_attr.recv_cq = info->recv_cq;
1685	qp_attr.port_num = ~0;
1686
1687	rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1688	if (rc) {
1689		log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1690		goto create_qp_failed;
1691	}
1692
1693	memset(&conn_param, 0, sizeof(conn_param));
1694	conn_param.initiator_depth = 0;
1695
1696	conn_param.responder_resources =
1697		info->id->device->attrs.max_qp_rd_atom
1698			< SMBD_CM_RESPONDER_RESOURCES ?
1699		info->id->device->attrs.max_qp_rd_atom :
1700		SMBD_CM_RESPONDER_RESOURCES;
1701	info->responder_resources = conn_param.responder_resources;
1702	log_rdma_mr(INFO, "responder_resources=%d\n",
1703		info->responder_resources);
1704
1705	/* Need to send IRD/ORD in private data for iWARP */
1706	info->id->device->ops.get_port_immutable(
1707		info->id->device, info->id->port_num, &port_immutable);
1708	if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1709		ird_ord_hdr[0] = info->responder_resources;
1710		ird_ord_hdr[1] = 1;
1711		conn_param.private_data = ird_ord_hdr;
1712		conn_param.private_data_len = sizeof(ird_ord_hdr);
1713	} else {
1714		conn_param.private_data = NULL;
1715		conn_param.private_data_len = 0;
1716	}
1717
1718	conn_param.retry_count = SMBD_CM_RETRY;
1719	conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1720	conn_param.flow_control = 0;
1721
1722	log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1723		&addr_in->sin_addr, port);
1724
1725	init_waitqueue_head(&info->conn_wait);
1726	init_waitqueue_head(&info->disconn_wait);
1727	init_waitqueue_head(&info->wait_reassembly_queue);
1728	rc = rdma_connect(info->id, &conn_param);
1729	if (rc) {
1730		log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1731		goto rdma_connect_failed;
1732	}
1733
1734	wait_event_interruptible(
1735		info->conn_wait, info->transport_status != SMBD_CONNECTING);
1736
1737	if (info->transport_status != SMBD_CONNECTED) {
1738		log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1739		goto rdma_connect_failed;
1740	}
1741
1742	log_rdma_event(INFO, "rdma_connect connected\n");
1743
1744	rc = allocate_caches_and_workqueue(info);
1745	if (rc) {
1746		log_rdma_event(ERR, "cache allocation failed\n");
1747		goto allocate_cache_failed;
1748	}
1749
1750	init_waitqueue_head(&info->wait_send_queue);
1751	INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
1752	INIT_DELAYED_WORK(&info->send_immediate_work, send_immediate_work);
1753	queue_delayed_work(info->workqueue, &info->idle_timer_work,
1754		info->keep_alive_interval*HZ);
1755
1756	init_waitqueue_head(&info->wait_send_pending);
1757	atomic_set(&info->send_pending, 0);
1758
1759	init_waitqueue_head(&info->wait_send_payload_pending);
1760	atomic_set(&info->send_payload_pending, 0);
1761
1762	INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
1763	INIT_WORK(&info->recv_done_work, smbd_recv_done_work);
1764	INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1765	info->new_credits_offered = 0;
1766	spin_lock_init(&info->lock_new_credits_offered);
1767
1768	rc = smbd_negotiate(info);
1769	if (rc) {
1770		log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1771		goto negotiation_failed;
1772	}
1773
1774	rc = allocate_mr_list(info);
1775	if (rc) {
1776		log_rdma_mr(ERR, "memory registration allocation failed\n");
1777		goto allocate_mr_failed;
1778	}
1779
1780	return info;
1781
1782allocate_mr_failed:
1783	/* At this point, need to a full transport shutdown */
1784	smbd_destroy(server);
1785	return NULL;
1786
1787negotiation_failed:
1788	cancel_delayed_work_sync(&info->idle_timer_work);
1789	destroy_caches_and_workqueue(info);
1790	info->transport_status = SMBD_NEGOTIATE_FAILED;
1791	init_waitqueue_head(&info->conn_wait);
1792	rdma_disconnect(info->id);
1793	wait_event(info->conn_wait,
1794		info->transport_status == SMBD_DISCONNECTED);
1795
1796allocate_cache_failed:
1797rdma_connect_failed:
1798	rdma_destroy_qp(info->id);
1799
1800create_qp_failed:
1801alloc_cq_failed:
1802	if (info->send_cq)
1803		ib_free_cq(info->send_cq);
1804	if (info->recv_cq)
1805		ib_free_cq(info->recv_cq);
1806
1807config_failed:
1808	ib_dealloc_pd(info->pd);
1809	rdma_destroy_id(info->id);
1810
1811create_id_failed:
1812	kfree(info);
1813	return NULL;
1814}
1815
1816struct smbd_connection *smbd_get_connection(
1817	struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1818{
1819	struct smbd_connection *ret;
1820	int port = SMBD_PORT;
1821
1822try_again:
1823	ret = _smbd_get_connection(server, dstaddr, port);
1824
1825	/* Try SMB_PORT if SMBD_PORT doesn't work */
1826	if (!ret && port == SMBD_PORT) {
1827		port = SMB_PORT;
1828		goto try_again;
1829	}
1830	return ret;
1831}
1832
1833/*
1834 * Receive data from receive reassembly queue
1835 * All the incoming data packets are placed in reassembly queue
1836 * buf: the buffer to read data into
1837 * size: the length of data to read
1838 * return value: actual data read
1839 * Note: this implementation copies the data from reassebmly queue to receive
1840 * buffers used by upper layer. This is not the optimal code path. A better way
1841 * to do it is to not have upper layer allocate its receive buffers but rather
1842 * borrow the buffer from reassembly queue, and return it after data is
1843 * consumed. But this will require more changes to upper layer code, and also
1844 * need to consider packet boundaries while they still being reassembled.
1845 */
1846static int smbd_recv_buf(struct smbd_connection *info, char *buf,
1847		unsigned int size)
1848{
1849	struct smbd_response *response;
1850	struct smbd_data_transfer *data_transfer;
1851	int to_copy, to_read, data_read, offset;
1852	u32 data_length, remaining_data_length, data_offset;
1853	int rc;
1854
1855again:
1856	/*
1857	 * No need to hold the reassembly queue lock all the time as we are
1858	 * the only one reading from the front of the queue. The transport
1859	 * may add more entries to the back of the queue at the same time
1860	 */
1861	log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1862		info->reassembly_data_length);
1863	if (info->reassembly_data_length >= size) {
1864		int queue_length;
1865		int queue_removed = 0;
1866
1867		/*
1868		 * Need to make sure reassembly_data_length is read before
1869		 * reading reassembly_queue_length and calling
1870		 * _get_first_reassembly. This call is lock free
1871		 * as we never read at the end of the queue which are being
1872		 * updated in SOFTIRQ as more data is received
1873		 */
1874		virt_rmb();
1875		queue_length = info->reassembly_queue_length;
1876		data_read = 0;
1877		to_read = size;
1878		offset = info->first_entry_offset;
1879		while (data_read < size) {
1880			response = _get_first_reassembly(info);
1881			data_transfer = smbd_response_payload(response);
1882			data_length = le32_to_cpu(data_transfer->data_length);
1883			remaining_data_length =
1884				le32_to_cpu(
1885					data_transfer->remaining_data_length);
1886			data_offset = le32_to_cpu(data_transfer->data_offset);
1887
1888			/*
1889			 * The upper layer expects RFC1002 length at the
1890			 * beginning of the payload. Return it to indicate
1891			 * the total length of the packet. This minimize the
1892			 * change to upper layer packet processing logic. This
1893			 * will be eventually remove when an intermediate
1894			 * transport layer is added
1895			 */
1896			if (response->first_segment && size == 4) {
1897				unsigned int rfc1002_len =
1898					data_length + remaining_data_length;
1899				*((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1900				data_read = 4;
1901				response->first_segment = false;
1902				log_read(INFO, "returning rfc1002 length %d\n",
1903					rfc1002_len);
1904				goto read_rfc1002_done;
1905			}
1906
1907			to_copy = min_t(int, data_length - offset, to_read);
1908			memcpy(
1909				buf + data_read,
1910				(char *)data_transfer + data_offset + offset,
1911				to_copy);
1912
1913			/* move on to the next buffer? */
1914			if (to_copy == data_length - offset) {
1915				queue_length--;
1916				/*
1917				 * No need to lock if we are not at the
1918				 * end of the queue
1919				 */
1920				if (queue_length)
1921					list_del(&response->list);
1922				else {
1923					spin_lock_irq(
1924						&info->reassembly_queue_lock);
1925					list_del(&response->list);
1926					spin_unlock_irq(
1927						&info->reassembly_queue_lock);
1928				}
1929				queue_removed++;
1930				info->count_reassembly_queue--;
1931				info->count_dequeue_reassembly_queue++;
1932				put_receive_buffer(info, response);
1933				offset = 0;
1934				log_read(INFO, "put_receive_buffer offset=0\n");
1935			} else
1936				offset += to_copy;
1937
1938			to_read -= to_copy;
1939			data_read += to_copy;
1940
1941			log_read(INFO, "_get_first_reassembly memcpy %d bytes "
1942				"data_transfer_length-offset=%d after that "
1943				"to_read=%d data_read=%d offset=%d\n",
1944				to_copy, data_length - offset,
1945				to_read, data_read, offset);
1946		}
1947
1948		spin_lock_irq(&info->reassembly_queue_lock);
1949		info->reassembly_data_length -= data_read;
1950		info->reassembly_queue_length -= queue_removed;
1951		spin_unlock_irq(&info->reassembly_queue_lock);
1952
1953		info->first_entry_offset = offset;
1954		log_read(INFO, "returning to thread data_read=%d "
1955			"reassembly_data_length=%d first_entry_offset=%d\n",
1956			data_read, info->reassembly_data_length,
1957			info->first_entry_offset);
1958read_rfc1002_done:
1959		return data_read;
1960	}
1961
1962	log_read(INFO, "wait_event on more data\n");
1963	rc = wait_event_interruptible(
1964		info->wait_reassembly_queue,
1965		info->reassembly_data_length >= size ||
1966			info->transport_status != SMBD_CONNECTED);
1967	/* Don't return any data if interrupted */
1968	if (rc)
1969		return rc;
1970
1971	if (info->transport_status != SMBD_CONNECTED) {
1972		log_read(ERR, "disconnected\n");
1973		return 0;
1974	}
1975
1976	goto again;
1977}
1978
1979/*
1980 * Receive a page from receive reassembly queue
1981 * page: the page to read data into
1982 * to_read: the length of data to read
1983 * return value: actual data read
1984 */
1985static int smbd_recv_page(struct smbd_connection *info,
1986		struct page *page, unsigned int page_offset,
1987		unsigned int to_read)
1988{
1989	int ret;
1990	char *to_address;
1991	void *page_address;
1992
1993	/* make sure we have the page ready for read */
1994	ret = wait_event_interruptible(
1995		info->wait_reassembly_queue,
1996		info->reassembly_data_length >= to_read ||
1997			info->transport_status != SMBD_CONNECTED);
1998	if (ret)
1999		return ret;
2000
2001	/* now we can read from reassembly queue and not sleep */
2002	page_address = kmap_atomic(page);
2003	to_address = (char *) page_address + page_offset;
2004
2005	log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
2006		page, to_address, to_read);
2007
2008	ret = smbd_recv_buf(info, to_address, to_read);
2009	kunmap_atomic(page_address);
2010
2011	return ret;
2012}
2013
2014/*
2015 * Receive data from transport
2016 * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
2017 * return: total bytes read, or 0. SMB Direct will not do partial read.
2018 */
2019int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
2020{
2021	char *buf;
2022	struct page *page;
2023	unsigned int to_read, page_offset;
2024	int rc;
2025
2026	if (iov_iter_rw(&msg->msg_iter) == WRITE) {
2027		/* It's a bug in upper layer to get there */
2028		cifs_dbg(VFS, "CIFS: invalid msg iter dir %u\n",
2029			 iov_iter_rw(&msg->msg_iter));
2030		rc = -EINVAL;
2031		goto out;
2032	}
2033
2034	switch (iov_iter_type(&msg->msg_iter)) {
2035	case ITER_KVEC:
2036		buf = msg->msg_iter.kvec->iov_base;
2037		to_read = msg->msg_iter.kvec->iov_len;
2038		rc = smbd_recv_buf(info, buf, to_read);
2039		break;
2040
2041	case ITER_BVEC:
2042		page = msg->msg_iter.bvec->bv_page;
2043		page_offset = msg->msg_iter.bvec->bv_offset;
2044		to_read = msg->msg_iter.bvec->bv_len;
2045		rc = smbd_recv_page(info, page, page_offset, to_read);
2046		break;
2047
2048	default:
2049		/* It's a bug in upper layer to get there */
2050		cifs_dbg(VFS, "CIFS: invalid msg type %d\n",
2051			 iov_iter_type(&msg->msg_iter));
2052		rc = -EINVAL;
2053	}
2054
2055out:
2056	/* SMBDirect will read it all or nothing */
2057	if (rc > 0)
2058		msg->msg_iter.count = 0;
2059	return rc;
2060}
2061
2062/*
2063 * Send data to transport
2064 * Each rqst is transported as a SMBDirect payload
2065 * rqst: the data to write
2066 * return value: 0 if successfully write, otherwise error code
2067 */
2068int smbd_send(struct TCP_Server_Info *server,
2069	int num_rqst, struct smb_rqst *rqst_array)
2070{
2071	struct smbd_connection *info = server->smbd_conn;
2072	struct kvec vec;
2073	int nvecs;
2074	int size;
2075	unsigned int buflen, remaining_data_length;
 
2076	int start, i, j;
2077	int max_iov_size =
2078		info->max_send_size - sizeof(struct smbd_data_transfer);
2079	struct kvec *iov;
2080	int rc;
2081	struct smb_rqst *rqst;
2082	int rqst_idx;
2083
2084	if (info->transport_status != SMBD_CONNECTED) {
2085		rc = -EAGAIN;
2086		goto done;
2087	}
2088
2089	/*
2090	 * Add in the page array if there is one. The caller needs to set
2091	 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
2092	 * ends at page boundary
2093	 */
2094	remaining_data_length = 0;
2095	for (i = 0; i < num_rqst; i++)
2096		remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
2097
2098	if (remaining_data_length + sizeof(struct smbd_data_transfer) >
2099		info->max_fragmented_send_size) {
2100		log_write(ERR, "payload size %d > max size %d\n",
2101			remaining_data_length, info->max_fragmented_send_size);
2102		rc = -EINVAL;
2103		goto done;
2104	}
2105
2106	log_write(INFO, "num_rqst=%d total length=%u\n",
2107			num_rqst, remaining_data_length);
2108
2109	rqst_idx = 0;
2110next_rqst:
2111	rqst = &rqst_array[rqst_idx];
2112	iov = rqst->rq_iov;
2113
2114	cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
2115		rqst_idx, smb_rqst_len(server, rqst));
2116	for (i = 0; i < rqst->rq_nvec; i++)
2117		dump_smb(iov[i].iov_base, iov[i].iov_len);
2118
2119
2120	log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d "
2121		"rq_tailsz=%d buflen=%lu\n",
2122		rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
2123		rqst->rq_tailsz, smb_rqst_len(server, rqst));
2124
2125	start = i = 0;
2126	buflen = 0;
2127	while (true) {
2128		buflen += iov[i].iov_len;
2129		if (buflen > max_iov_size) {
2130			if (i > start) {
2131				remaining_data_length -=
2132					(buflen-iov[i].iov_len);
2133				log_write(INFO, "sending iov[] from start=%d "
2134					"i=%d nvecs=%d "
2135					"remaining_data_length=%d\n",
2136					start, i, i-start,
2137					remaining_data_length);
2138				rc = smbd_post_send_data(
2139					info, &iov[start], i-start,
2140					remaining_data_length);
2141				if (rc)
2142					goto done;
2143			} else {
2144				/* iov[start] is too big, break it */
2145				nvecs = (buflen+max_iov_size-1)/max_iov_size;
2146				log_write(INFO, "iov[%d] iov_base=%p buflen=%d"
2147					" break to %d vectors\n",
2148					start, iov[start].iov_base,
2149					buflen, nvecs);
2150				for (j = 0; j < nvecs; j++) {
2151					vec.iov_base =
2152						(char *)iov[start].iov_base +
2153						j*max_iov_size;
2154					vec.iov_len = max_iov_size;
2155					if (j == nvecs-1)
2156						vec.iov_len =
2157							buflen -
2158							max_iov_size*(nvecs-1);
2159					remaining_data_length -= vec.iov_len;
2160					log_write(INFO,
2161						"sending vec j=%d iov_base=%p"
2162						" iov_len=%zu "
2163						"remaining_data_length=%d\n",
2164						j, vec.iov_base, vec.iov_len,
2165						remaining_data_length);
2166					rc = smbd_post_send_data(
2167						info, &vec, 1,
2168						remaining_data_length);
2169					if (rc)
2170						goto done;
2171				}
2172				i++;
2173				if (i == rqst->rq_nvec)
2174					break;
2175			}
 
 
 
 
 
 
 
 
 
2176			start = i;
2177			buflen = 0;
2178		} else {
2179			i++;
2180			if (i == rqst->rq_nvec) {
2181				/* send out all remaining vecs */
2182				remaining_data_length -= buflen;
2183				log_write(INFO,
2184					"sending iov[] from start=%d i=%d "
2185					"nvecs=%d remaining_data_length=%d\n",
2186					start, i, i-start,
2187					remaining_data_length);
2188				rc = smbd_post_send_data(info, &iov[start],
2189					i-start, remaining_data_length);
 
 
 
 
 
 
 
 
2190				if (rc)
2191					goto done;
2192				break;
2193			}
2194		}
2195		log_write(INFO, "looping i=%d buflen=%d\n", i, buflen);
2196	}
2197
2198	/* now sending pages if there are any */
2199	for (i = 0; i < rqst->rq_npages; i++) {
2200		unsigned int offset;
2201
2202		rqst_page_get_length(rqst, i, &buflen, &offset);
2203		nvecs = (buflen + max_iov_size - 1) / max_iov_size;
2204		log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
2205			buflen, nvecs);
2206		for (j = 0; j < nvecs; j++) {
2207			size = max_iov_size;
2208			if (j == nvecs-1)
2209				size = buflen - j*max_iov_size;
2210			remaining_data_length -= size;
2211			log_write(INFO, "sending pages i=%d offset=%d size=%d"
2212				" remaining_data_length=%d\n",
2213				i, j*max_iov_size+offset, size,
2214				remaining_data_length);
2215			rc = smbd_post_send_page(
2216				info, rqst->rq_pages[i],
2217				j*max_iov_size + offset,
2218				size, remaining_data_length);
2219			if (rc)
2220				goto done;
2221		}
2222	}
2223
2224	rqst_idx++;
2225	if (rqst_idx < num_rqst)
2226		goto next_rqst;
2227
2228done:
2229	/*
2230	 * As an optimization, we don't wait for individual I/O to finish
2231	 * before sending the next one.
2232	 * Send them all and wait for pending send count to get to 0
2233	 * that means all the I/Os have been out and we are good to return
2234	 */
2235
2236	wait_event(info->wait_send_payload_pending,
2237		atomic_read(&info->send_payload_pending) == 0);
2238
2239	return rc;
2240}
2241
2242static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
2243{
2244	struct smbd_mr *mr;
2245	struct ib_cqe *cqe;
2246
2247	if (wc->status) {
2248		log_rdma_mr(ERR, "status=%d\n", wc->status);
2249		cqe = wc->wr_cqe;
2250		mr = container_of(cqe, struct smbd_mr, cqe);
2251		smbd_disconnect_rdma_connection(mr->conn);
2252	}
2253}
2254
2255/*
2256 * The work queue function that recovers MRs
2257 * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
2258 * again. Both calls are slow, so finish them in a workqueue. This will not
2259 * block I/O path.
2260 * There is one workqueue that recovers MRs, there is no need to lock as the
2261 * I/O requests calling smbd_register_mr will never update the links in the
2262 * mr_list.
2263 */
2264static void smbd_mr_recovery_work(struct work_struct *work)
2265{
2266	struct smbd_connection *info =
2267		container_of(work, struct smbd_connection, mr_recovery_work);
2268	struct smbd_mr *smbdirect_mr;
2269	int rc;
2270
2271	list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
2272		if (smbdirect_mr->state == MR_INVALIDATED)
2273			ib_dma_unmap_sg(
2274				info->id->device, smbdirect_mr->sgl,
2275				smbdirect_mr->sgl_count,
2276				smbdirect_mr->dir);
2277		else if (smbdirect_mr->state == MR_ERROR) {
2278
2279			/* recover this MR entry */
2280			rc = ib_dereg_mr(smbdirect_mr->mr);
2281			if (rc) {
2282				log_rdma_mr(ERR,
2283					"ib_dereg_mr failed rc=%x\n",
2284					rc);
2285				smbd_disconnect_rdma_connection(info);
2286				continue;
2287			}
2288
2289			smbdirect_mr->mr = ib_alloc_mr(
2290				info->pd, info->mr_type,
2291				info->max_frmr_depth);
2292			if (IS_ERR(smbdirect_mr->mr)) {
2293				log_rdma_mr(ERR,
2294					"ib_alloc_mr failed mr_type=%x "
2295					"max_frmr_depth=%x\n",
2296					info->mr_type,
2297					info->max_frmr_depth);
2298				smbd_disconnect_rdma_connection(info);
2299				continue;
2300			}
2301		} else
2302			/* This MR is being used, don't recover it */
2303			continue;
2304
2305		smbdirect_mr->state = MR_READY;
2306
2307		/* smbdirect_mr->state is updated by this function
2308		 * and is read and updated by I/O issuing CPUs trying
2309		 * to get a MR, the call to atomic_inc_return
2310		 * implicates a memory barrier and guarantees this
2311		 * value is updated before waking up any calls to
2312		 * get_mr() from the I/O issuing CPUs
2313		 */
2314		if (atomic_inc_return(&info->mr_ready_count) == 1)
2315			wake_up_interruptible(&info->wait_mr);
2316	}
2317}
2318
2319static void destroy_mr_list(struct smbd_connection *info)
2320{
2321	struct smbd_mr *mr, *tmp;
2322
2323	cancel_work_sync(&info->mr_recovery_work);
2324	list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
2325		if (mr->state == MR_INVALIDATED)
2326			ib_dma_unmap_sg(info->id->device, mr->sgl,
2327				mr->sgl_count, mr->dir);
2328		ib_dereg_mr(mr->mr);
2329		kfree(mr->sgl);
2330		kfree(mr);
2331	}
2332}
2333
2334/*
2335 * Allocate MRs used for RDMA read/write
2336 * The number of MRs will not exceed hardware capability in responder_resources
2337 * All MRs are kept in mr_list. The MR can be recovered after it's used
2338 * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
2339 * as MRs are used and recovered for I/O, but the list links will not change
2340 */
2341static int allocate_mr_list(struct smbd_connection *info)
2342{
2343	int i;
2344	struct smbd_mr *smbdirect_mr, *tmp;
2345
2346	INIT_LIST_HEAD(&info->mr_list);
2347	init_waitqueue_head(&info->wait_mr);
2348	spin_lock_init(&info->mr_list_lock);
2349	atomic_set(&info->mr_ready_count, 0);
2350	atomic_set(&info->mr_used_count, 0);
2351	init_waitqueue_head(&info->wait_for_mr_cleanup);
2352	/* Allocate more MRs (2x) than hardware responder_resources */
2353	for (i = 0; i < info->responder_resources * 2; i++) {
2354		smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
2355		if (!smbdirect_mr)
2356			goto out;
2357		smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
2358					info->max_frmr_depth);
2359		if (IS_ERR(smbdirect_mr->mr)) {
2360			log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x "
2361				"max_frmr_depth=%x\n",
2362				info->mr_type, info->max_frmr_depth);
2363			goto out;
2364		}
2365		smbdirect_mr->sgl = kcalloc(
2366					info->max_frmr_depth,
2367					sizeof(struct scatterlist),
2368					GFP_KERNEL);
2369		if (!smbdirect_mr->sgl) {
2370			log_rdma_mr(ERR, "failed to allocate sgl\n");
2371			ib_dereg_mr(smbdirect_mr->mr);
2372			goto out;
2373		}
2374		smbdirect_mr->state = MR_READY;
2375		smbdirect_mr->conn = info;
2376
2377		list_add_tail(&smbdirect_mr->list, &info->mr_list);
2378		atomic_inc(&info->mr_ready_count);
2379	}
2380	INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
2381	return 0;
2382
2383out:
2384	kfree(smbdirect_mr);
2385
2386	list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
2387		ib_dereg_mr(smbdirect_mr->mr);
2388		kfree(smbdirect_mr->sgl);
2389		kfree(smbdirect_mr);
2390	}
2391	return -ENOMEM;
2392}
2393
2394/*
2395 * Get a MR from mr_list. This function waits until there is at least one
2396 * MR available in the list. It may access the list while the
2397 * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
2398 * as they never modify the same places. However, there may be several CPUs
2399 * issueing I/O trying to get MR at the same time, mr_list_lock is used to
2400 * protect this situation.
2401 */
2402static struct smbd_mr *get_mr(struct smbd_connection *info)
2403{
2404	struct smbd_mr *ret;
2405	int rc;
2406again:
2407	rc = wait_event_interruptible(info->wait_mr,
2408		atomic_read(&info->mr_ready_count) ||
2409		info->transport_status != SMBD_CONNECTED);
2410	if (rc) {
2411		log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
2412		return NULL;
2413	}
2414
2415	if (info->transport_status != SMBD_CONNECTED) {
2416		log_rdma_mr(ERR, "info->transport_status=%x\n",
2417			info->transport_status);
2418		return NULL;
2419	}
2420
2421	spin_lock(&info->mr_list_lock);
2422	list_for_each_entry(ret, &info->mr_list, list) {
2423		if (ret->state == MR_READY) {
2424			ret->state = MR_REGISTERED;
2425			spin_unlock(&info->mr_list_lock);
2426			atomic_dec(&info->mr_ready_count);
2427			atomic_inc(&info->mr_used_count);
2428			return ret;
2429		}
2430	}
2431
2432	spin_unlock(&info->mr_list_lock);
2433	/*
2434	 * It is possible that we could fail to get MR because other processes may
2435	 * try to acquire a MR at the same time. If this is the case, retry it.
2436	 */
2437	goto again;
2438}
2439
2440/*
2441 * Register memory for RDMA read/write
2442 * pages[]: the list of pages to register memory with
2443 * num_pages: the number of pages to register
2444 * tailsz: if non-zero, the bytes to register in the last page
2445 * writing: true if this is a RDMA write (SMB read), false for RDMA read
2446 * need_invalidate: true if this MR needs to be locally invalidated after I/O
2447 * return value: the MR registered, NULL if failed.
2448 */
2449struct smbd_mr *smbd_register_mr(
2450	struct smbd_connection *info, struct page *pages[], int num_pages,
2451	int offset, int tailsz, bool writing, bool need_invalidate)
2452{
2453	struct smbd_mr *smbdirect_mr;
2454	int rc, i;
2455	enum dma_data_direction dir;
2456	struct ib_reg_wr *reg_wr;
2457
2458	if (num_pages > info->max_frmr_depth) {
2459		log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
2460			num_pages, info->max_frmr_depth);
2461		return NULL;
2462	}
2463
2464	smbdirect_mr = get_mr(info);
2465	if (!smbdirect_mr) {
2466		log_rdma_mr(ERR, "get_mr returning NULL\n");
2467		return NULL;
2468	}
2469	smbdirect_mr->need_invalidate = need_invalidate;
2470	smbdirect_mr->sgl_count = num_pages;
2471	sg_init_table(smbdirect_mr->sgl, num_pages);
2472
2473	log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
2474			num_pages, offset, tailsz);
2475
2476	if (num_pages == 1) {
2477		sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
2478		goto skip_multiple_pages;
2479	}
2480
2481	/* We have at least two pages to register */
2482	sg_set_page(
2483		&smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
2484	i = 1;
2485	while (i < num_pages - 1) {
2486		sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
2487		i++;
2488	}
2489	sg_set_page(&smbdirect_mr->sgl[i], pages[i],
2490		tailsz ? tailsz : PAGE_SIZE, 0);
2491
2492skip_multiple_pages:
2493	dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2494	smbdirect_mr->dir = dir;
2495	rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
2496	if (!rc) {
2497		log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
2498			num_pages, dir, rc);
2499		goto dma_map_error;
2500	}
2501
2502	rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
2503		NULL, PAGE_SIZE);
2504	if (rc != num_pages) {
2505		log_rdma_mr(ERR,
2506			"ib_map_mr_sg failed rc = %d num_pages = %x\n",
2507			rc, num_pages);
2508		goto map_mr_error;
2509	}
2510
2511	ib_update_fast_reg_key(smbdirect_mr->mr,
2512		ib_inc_rkey(smbdirect_mr->mr->rkey));
2513	reg_wr = &smbdirect_mr->wr;
2514	reg_wr->wr.opcode = IB_WR_REG_MR;
2515	smbdirect_mr->cqe.done = register_mr_done;
2516	reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
2517	reg_wr->wr.num_sge = 0;
2518	reg_wr->wr.send_flags = IB_SEND_SIGNALED;
2519	reg_wr->mr = smbdirect_mr->mr;
2520	reg_wr->key = smbdirect_mr->mr->rkey;
2521	reg_wr->access = writing ?
2522			IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2523			IB_ACCESS_REMOTE_READ;
2524
2525	/*
2526	 * There is no need for waiting for complemtion on ib_post_send
2527	 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
2528	 * on the next ib_post_send when we actaully send I/O to remote peer
2529	 */
2530	rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
2531	if (!rc)
2532		return smbdirect_mr;
2533
2534	log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
2535		rc, reg_wr->key);
2536
2537	/* If all failed, attempt to recover this MR by setting it MR_ERROR*/
2538map_mr_error:
2539	ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
2540		smbdirect_mr->sgl_count, smbdirect_mr->dir);
2541
2542dma_map_error:
2543	smbdirect_mr->state = MR_ERROR;
2544	if (atomic_dec_and_test(&info->mr_used_count))
2545		wake_up(&info->wait_for_mr_cleanup);
2546
2547	smbd_disconnect_rdma_connection(info);
2548
2549	return NULL;
2550}
2551
2552static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
2553{
2554	struct smbd_mr *smbdirect_mr;
2555	struct ib_cqe *cqe;
2556
2557	cqe = wc->wr_cqe;
2558	smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
2559	smbdirect_mr->state = MR_INVALIDATED;
2560	if (wc->status != IB_WC_SUCCESS) {
2561		log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
2562		smbdirect_mr->state = MR_ERROR;
2563	}
2564	complete(&smbdirect_mr->invalidate_done);
2565}
2566
2567/*
2568 * Deregister a MR after I/O is done
2569 * This function may wait if remote invalidation is not used
2570 * and we have to locally invalidate the buffer to prevent data is being
2571 * modified by remote peer after upper layer consumes it
2572 */
2573int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
2574{
2575	struct ib_send_wr *wr;
2576	struct smbd_connection *info = smbdirect_mr->conn;
2577	int rc = 0;
2578
2579	if (smbdirect_mr->need_invalidate) {
2580		/* Need to finish local invalidation before returning */
2581		wr = &smbdirect_mr->inv_wr;
2582		wr->opcode = IB_WR_LOCAL_INV;
2583		smbdirect_mr->cqe.done = local_inv_done;
2584		wr->wr_cqe = &smbdirect_mr->cqe;
2585		wr->num_sge = 0;
2586		wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
2587		wr->send_flags = IB_SEND_SIGNALED;
2588
2589		init_completion(&smbdirect_mr->invalidate_done);
2590		rc = ib_post_send(info->id->qp, wr, NULL);
2591		if (rc) {
2592			log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
2593			smbd_disconnect_rdma_connection(info);
2594			goto done;
2595		}
2596		wait_for_completion(&smbdirect_mr->invalidate_done);
2597		smbdirect_mr->need_invalidate = false;
2598	} else
2599		/*
2600		 * For remote invalidation, just set it to MR_INVALIDATED
2601		 * and defer to mr_recovery_work to recover the MR for next use
2602		 */
2603		smbdirect_mr->state = MR_INVALIDATED;
2604
2605	/*
2606	 * Schedule the work to do MR recovery for future I/Os
2607	 * MR recovery is slow and we don't want it to block the current I/O
2608	 */
2609	queue_work(info->workqueue, &info->mr_recovery_work);
 
 
 
 
 
 
 
 
 
2610
2611done:
2612	if (atomic_dec_and_test(&info->mr_used_count))
2613		wake_up(&info->wait_for_mr_cleanup);
2614
2615	return rc;
2616}