Blame view

fs/cifs/smbdirect.c 71.3 KB
c942fddf8   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
03bee01d6   Long Li   CIFS: SMBD: Add S...
2
3
4
5
  /*
   *   Copyright (C) 2017, Microsoft Corporation.
   *
   *   Author(s): Long Li <longli@microsoft.com>
03bee01d6   Long Li   CIFS: SMBD: Add S...
6
   */
f198186aa   Long Li   CIFS: SMBD: Estab...
7
  #include <linux/module.h>
f64b78fd1   Long Li   CIFS: SMBD: Imple...
8
  #include <linux/highmem.h>
03bee01d6   Long Li   CIFS: SMBD: Add S...
9
  #include "smbdirect.h"
f198186aa   Long Li   CIFS: SMBD: Estab...
10
  #include "cifs_debug.h"
b6903bcf0   Long Li   CIFS: SMBD: Suppo...
11
  #include "cifsproto.h"
35e2cc1ba   Paulo Alcantara   cifs: Use correct...
12
  #include "smb2proto.h"
f198186aa   Long Li   CIFS: SMBD: Estab...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  
  static struct smbd_response *get_empty_queue_buffer(
  		struct smbd_connection *info);
  static struct smbd_response *get_receive_buffer(
  		struct smbd_connection *info);
  static void put_receive_buffer(
  		struct smbd_connection *info,
  		struct smbd_response *response);
  static int allocate_receive_buffers(struct smbd_connection *info, int num_buf);
  static void destroy_receive_buffers(struct smbd_connection *info);
  
  static void put_empty_packet(
  		struct smbd_connection *info, struct smbd_response *response);
  static void enqueue_reassembly(
  		struct smbd_connection *info,
  		struct smbd_response *response, int data_length);
  static struct smbd_response *_get_first_reassembly(
  		struct smbd_connection *info);
  
  static int smbd_post_recv(
  		struct smbd_connection *info,
  		struct smbd_response *response);
  
  static int smbd_post_send_empty(struct smbd_connection *info);
d649e1bba   Long Li   CIFS: SMBD: Imple...
37
38
39
40
41
42
  static int smbd_post_send_data(
  		struct smbd_connection *info,
  		struct kvec *iov, int n_vec, int remaining_data_length);
  static int smbd_post_send_page(struct smbd_connection *info,
  		struct page *page, unsigned long offset,
  		size_t size, int remaining_data_length);
03bee01d6   Long Li   CIFS: SMBD: Add S...
43

c73985833   Long Li   CIFS: SMBD: Imple...
44
45
  static void destroy_mr_list(struct smbd_connection *info);
  static int allocate_mr_list(struct smbd_connection *info);
03bee01d6   Long Li   CIFS: SMBD: Add S...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  /* SMBD version number */
  #define SMBD_V1	0x0100
  
  /* Port numbers for SMBD transport */
  #define SMB_PORT	445
  #define SMBD_PORT	5445
  
  /* Address lookup and resolve timeout in ms */
  #define RDMA_RESOLVE_TIMEOUT	5000
  
  /* SMBD negotiation timeout in seconds */
  #define SMBD_NEGOTIATE_TIMEOUT	120
  
  /* SMBD minimum receive size and fragmented sized defined in [MS-SMBD] */
  #define SMBD_MIN_RECEIVE_SIZE		128
  #define SMBD_MIN_FRAGMENTED_SIZE	131072
  
  /*
   * Default maximum number of RDMA read/write outstanding on this connection
   * This value is possibly decreased during QP creation on hardware limit
   */
  #define SMBD_CM_RESPONDER_RESOURCES	32
  
  /* Maximum number of retries on data transfer operations */
  #define SMBD_CM_RETRY			6
  /* No need to retry on Receiver Not Ready since SMBD manages credits */
  #define SMBD_CM_RNR_RETRY		0
  
  /*
   * User configurable initial values per SMBD transport connection
   * as defined in [MS-SMBD] 3.1.1.1
   * Those may change after a SMBD negotiation
   */
  /* The local peer's maximum number of credits to grant to the peer */
  int smbd_receive_credit_max = 255;
  
  /* The remote peer's credit request of local peer */
  int smbd_send_credit_target = 255;
  
  /* The maximum single message size can be sent to remote peer */
  int smbd_max_send_size = 1364;
  
  /*  The maximum fragmented upper-layer payload receive size supported */
  int smbd_max_fragmented_recv_size = 1024 * 1024;
  
  /*  The maximum single-message size which can be received */
  int smbd_max_receive_size = 8192;
  
  /* The timeout to initiate send of a keepalive message on idle */
  int smbd_keep_alive_interval = 120;
  
  /*
   * User configurable initial values for RDMA transport
   * The actual values used may be lower and are limited to hardware capabilities
   */
  /* Default maximum number of SGEs in a RDMA write/read */
  int smbd_max_frmr_depth = 2048;
  
  /* If payload is less than this byte, use RDMA send/recv not read/write */
  int rdma_readwrite_threshold = 4096;
f198186aa   Long Li   CIFS: SMBD: Estab...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  
  /* Transport logging functions
   * Logging are defined as classes. They can be OR'ed to define the actual
   * logging level via module parameter smbd_logging_class
   * e.g. cifs.smbd_logging_class=0xa0 will log all log_rdma_recv() and
   * log_rdma_event()
   */
  #define LOG_OUTGOING			0x1
  #define LOG_INCOMING			0x2
  #define LOG_READ			0x4
  #define LOG_WRITE			0x8
  #define LOG_RDMA_SEND			0x10
  #define LOG_RDMA_RECV			0x20
  #define LOG_KEEP_ALIVE			0x40
  #define LOG_RDMA_EVENT			0x80
  #define LOG_RDMA_MR			0x100
  static unsigned int smbd_logging_class;
  module_param(smbd_logging_class, uint, 0644);
  MODULE_PARM_DESC(smbd_logging_class,
  	"Logging class for SMBD transport 0x0 to 0x100");
  
  #define ERR		0x0
  #define INFO		0x1
  static unsigned int smbd_logging_level = ERR;
  module_param(smbd_logging_level, uint, 0644);
  MODULE_PARM_DESC(smbd_logging_level,
  	"Logging level for SMBD transport, 0 (default): error, 1: info");
  
  #define log_rdma(level, class, fmt, args...)				\
  do {									\
  	if (level <= smbd_logging_level || class & smbd_logging_class)	\
  		cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
  } while (0)
  
  #define log_outgoing(level, fmt, args...) \
  		log_rdma(level, LOG_OUTGOING, fmt, ##args)
  #define log_incoming(level, fmt, args...) \
  		log_rdma(level, LOG_INCOMING, fmt, ##args)
  #define log_read(level, fmt, args...)	log_rdma(level, LOG_READ, fmt, ##args)
  #define log_write(level, fmt, args...)	log_rdma(level, LOG_WRITE, fmt, ##args)
  #define log_rdma_send(level, fmt, args...) \
  		log_rdma(level, LOG_RDMA_SEND, fmt, ##args)
  #define log_rdma_recv(level, fmt, args...) \
  		log_rdma(level, LOG_RDMA_RECV, fmt, ##args)
  #define log_keep_alive(level, fmt, args...) \
  		log_rdma(level, LOG_KEEP_ALIVE, fmt, ##args)
  #define log_rdma_event(level, fmt, args...) \
  		log_rdma(level, LOG_RDMA_EVENT, fmt, ##args)
  #define log_rdma_mr(level, fmt, args...) \
  		log_rdma(level, LOG_RDMA_MR, fmt, ##args)
f198186aa   Long Li   CIFS: SMBD: Estab...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  static void smbd_disconnect_rdma_work(struct work_struct *work)
  {
  	struct smbd_connection *info =
  		container_of(work, struct smbd_connection, disconnect_work);
  
  	if (info->transport_status == SMBD_CONNECTED) {
  		info->transport_status = SMBD_DISCONNECTING;
  		rdma_disconnect(info->id);
  	}
  }
  
  static void smbd_disconnect_rdma_connection(struct smbd_connection *info)
  {
  	queue_work(info->workqueue, &info->disconnect_work);
  }
  
  /* Upcall from RDMA CM */
  static int smbd_conn_upcall(
  		struct rdma_cm_id *id, struct rdma_cm_event *event)
  {
  	struct smbd_connection *info = id->context;
  
  	log_rdma_event(INFO, "event=%d status=%d
  ",
  		event->event, event->status);
  
  	switch (event->event) {
  	case RDMA_CM_EVENT_ADDR_RESOLVED:
  	case RDMA_CM_EVENT_ROUTE_RESOLVED:
  		info->ri_rc = 0;
  		complete(&info->ri_done);
  		break;
  
  	case RDMA_CM_EVENT_ADDR_ERROR:
  		info->ri_rc = -EHOSTUNREACH;
  		complete(&info->ri_done);
  		break;
  
  	case RDMA_CM_EVENT_ROUTE_ERROR:
  		info->ri_rc = -ENETUNREACH;
  		complete(&info->ri_done);
  		break;
  
  	case RDMA_CM_EVENT_ESTABLISHED:
  		log_rdma_event(INFO, "connected event=%d
  ", event->event);
  		info->transport_status = SMBD_CONNECTED;
  		wake_up_interruptible(&info->conn_wait);
  		break;
  
  	case RDMA_CM_EVENT_CONNECT_ERROR:
  	case RDMA_CM_EVENT_UNREACHABLE:
  	case RDMA_CM_EVENT_REJECTED:
  		log_rdma_event(INFO, "connecting failed event=%d
  ", event->event);
  		info->transport_status = SMBD_DISCONNECTED;
  		wake_up_interruptible(&info->conn_wait);
  		break;
  
  	case RDMA_CM_EVENT_DEVICE_REMOVAL:
  	case RDMA_CM_EVENT_DISCONNECTED:
  		/* This happenes when we fail the negotiation */
  		if (info->transport_status == SMBD_NEGOTIATE_FAILED) {
  			info->transport_status = SMBD_DISCONNECTED;
  			wake_up(&info->conn_wait);
  			break;
  		}
  
  		info->transport_status = SMBD_DISCONNECTED;
e8b3bfe9b   Long Li   cifs: smbd: Don't...
225
  		wake_up_interruptible(&info->disconn_wait);
050b8c374   Long Li   smbd: Make upper ...
226
227
  		wake_up_interruptible(&info->wait_reassembly_queue);
  		wake_up_interruptible_all(&info->wait_send_queue);
f198186aa   Long Li   CIFS: SMBD: Estab...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  		break;
  
  	default:
  		break;
  	}
  
  	return 0;
  }
  
  /* Upcall from RDMA QP */
  static void
  smbd_qp_async_error_upcall(struct ib_event *event, void *context)
  {
  	struct smbd_connection *info = context;
  
  	log_rdma_event(ERR, "%s on device %s info %p
  ",
  		ib_event_msg(event->event), event->device->name, info);
  
  	switch (event->event) {
  	case IB_EVENT_CQ_ERR:
  	case IB_EVENT_QP_FATAL:
  		smbd_disconnect_rdma_connection(info);
  
  	default:
  		break;
  	}
  }
  
  static inline void *smbd_request_payload(struct smbd_request *request)
  {
  	return (void *)request->packet;
  }
  
  static inline void *smbd_response_payload(struct smbd_response *response)
  {
  	return (void *)response->packet;
  }
  
  /* Called when a RDMA send is done */
  static void send_done(struct ib_cq *cq, struct ib_wc *wc)
  {
  	int i;
  	struct smbd_request *request =
  		container_of(wc->wr_cqe, struct smbd_request, cqe);
  
  	log_rdma_send(INFO, "smbd_request %p completed wc->status=%d
  ",
  		request, wc->status);
  
  	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
  		log_rdma_send(ERR, "wc->status=%d wc->opcode=%d
  ",
  			wc->status, wc->opcode);
  		smbd_disconnect_rdma_connection(request->info);
  	}
  
  	for (i = 0; i < request->num_sge; i++)
  		ib_dma_unmap_single(request->info->id->device,
  			request->sge[i].addr,
  			request->sge[i].length,
  			DMA_TO_DEVICE);
072a14ec6   Long Li   cifs: smbd: Merge...
290
291
  	if (atomic_dec_and_test(&request->info->send_pending))
  		wake_up(&request->info->wait_send_pending);
3ffbe78af   Long Li   cifs: smbd: Check...
292
  	wake_up(&request->info->wait_post_send);
f198186aa   Long Li   CIFS: SMBD: Estab...
293
294
295
296
297
298
  
  	mempool_free(request, request->info->request_mempool);
  }
  
  static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
  {
a0a3036b8   Joe Perches   cifs: Standardize...
299
300
301
302
303
304
305
  	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
  ",
  		       resp->min_version, resp->max_version,
  		       resp->negotiated_version, resp->credits_requested,
  		       resp->credits_granted, resp->status,
  		       resp->max_readwrite_size, resp->preferred_send_size,
  		       resp->max_receive_size, resp->max_fragmented_size);
f198186aa   Long Li   CIFS: SMBD: Estab...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  }
  
  /*
   * Process a negotiation response message, according to [MS-SMBD]3.1.5.7
   * response, packet_length: the negotiation response message
   * return value: true if negotiation is a success, false if failed
   */
  static bool process_negotiation_response(
  		struct smbd_response *response, int packet_length)
  {
  	struct smbd_connection *info = response->info;
  	struct smbd_negotiate_resp *packet = smbd_response_payload(response);
  
  	if (packet_length < sizeof(struct smbd_negotiate_resp)) {
  		log_rdma_event(ERR,
  			"error: packet_length=%d
  ", packet_length);
  		return false;
  	}
  
  	if (le16_to_cpu(packet->negotiated_version) != SMBD_V1) {
  		log_rdma_event(ERR, "error: negotiated_version=%x
  ",
  			le16_to_cpu(packet->negotiated_version));
  		return false;
  	}
  	info->protocol = le16_to_cpu(packet->negotiated_version);
  
  	if (packet->credits_requested == 0) {
  		log_rdma_event(ERR, "error: credits_requested==0
  ");
  		return false;
  	}
  	info->receive_credit_target = le16_to_cpu(packet->credits_requested);
  
  	if (packet->credits_granted == 0) {
  		log_rdma_event(ERR, "error: credits_granted==0
  ");
  		return false;
  	}
  	atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
  
  	atomic_set(&info->receive_credits, 0);
  
  	if (le32_to_cpu(packet->preferred_send_size) > info->max_receive_size) {
  		log_rdma_event(ERR, "error: preferred_send_size=%d
  ",
  			le32_to_cpu(packet->preferred_send_size));
  		return false;
  	}
  	info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
  
  	if (le32_to_cpu(packet->max_receive_size) < SMBD_MIN_RECEIVE_SIZE) {
  		log_rdma_event(ERR, "error: max_receive_size=%d
  ",
  			le32_to_cpu(packet->max_receive_size));
  		return false;
  	}
  	info->max_send_size = min_t(int, info->max_send_size,
  					le32_to_cpu(packet->max_receive_size));
  
  	if (le32_to_cpu(packet->max_fragmented_size) <
  			SMBD_MIN_FRAGMENTED_SIZE) {
  		log_rdma_event(ERR, "error: max_fragmented_size=%d
  ",
  			le32_to_cpu(packet->max_fragmented_size));
  		return false;
  	}
  	info->max_fragmented_send_size =
  		le32_to_cpu(packet->max_fragmented_size);
c73985833   Long Li   CIFS: SMBD: Imple...
376
377
378
379
380
381
382
383
384
385
  	info->rdma_readwrite_threshold =
  		rdma_readwrite_threshold > info->max_fragmented_send_size ?
  		info->max_fragmented_send_size :
  		rdma_readwrite_threshold;
  
  
  	info->max_readwrite_size = min_t(u32,
  			le32_to_cpu(packet->max_readwrite_size),
  			info->max_frmr_depth * PAGE_SIZE);
  	info->max_frmr_depth = info->max_readwrite_size / PAGE_SIZE;
f198186aa   Long Li   CIFS: SMBD: Estab...
386
387
388
  
  	return true;
  }
f198186aa   Long Li   CIFS: SMBD: Estab...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
  static void smbd_post_send_credits(struct work_struct *work)
  {
  	int ret = 0;
  	int use_receive_queue = 1;
  	int rc;
  	struct smbd_response *response;
  	struct smbd_connection *info =
  		container_of(work, struct smbd_connection,
  			post_send_credits_work);
  
  	if (info->transport_status != SMBD_CONNECTED) {
  		wake_up(&info->wait_receive_queues);
  		return;
  	}
  
  	if (info->receive_credit_target >
  		atomic_read(&info->receive_credits)) {
  		while (true) {
  			if (use_receive_queue)
  				response = get_receive_buffer(info);
  			else
  				response = get_empty_queue_buffer(info);
  			if (!response) {
  				/* now switch to emtpy packet queue */
  				if (use_receive_queue) {
  					use_receive_queue = 0;
  					continue;
  				} else
  					break;
  			}
  
  			response->type = SMBD_TRANSFER_DATA;
  			response->first_segment = false;
  			rc = smbd_post_recv(info, response);
  			if (rc) {
  				log_rdma_recv(ERR,
  					"post_recv failed rc=%d
  ", rc);
  				put_receive_buffer(info, response);
  				break;
  			}
  
  			ret++;
  		}
  	}
  
  	spin_lock(&info->lock_new_credits_offered);
  	info->new_credits_offered += ret;
  	spin_unlock(&info->lock_new_credits_offered);
044b541c1   Long Li   cifs: smbd: Do no...
438
439
440
441
442
443
444
445
446
447
448
  	/* Promptly send an immediate packet as defined in [MS-SMBD] 3.1.1.1 */
  	info->send_immediate = true;
  	if (atomic_read(&info->receive_credits) <
  		info->receive_credit_target - 1) {
  		if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
  		    info->send_immediate) {
  			log_keep_alive(INFO, "send an empty message
  ");
  			smbd_post_send_empty(info);
  		}
  	}
f198186aa   Long Li   CIFS: SMBD: Estab...
449
  }
f198186aa   Long Li   CIFS: SMBD: Estab...
450
451
452
453
454
455
456
457
  /* Called from softirq, when recv is done */
  static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
  {
  	struct smbd_data_transfer *data_transfer;
  	struct smbd_response *response =
  		container_of(wc->wr_cqe, struct smbd_response, cqe);
  	struct smbd_connection *info = response->info;
  	int data_length = 0;
a0a3036b8   Joe Perches   cifs: Standardize...
458
459
460
461
  	log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d byte_len=%d pkey_index=%x
  ",
  		      response, response->type, wc->status, wc->opcode,
  		      wc->byte_len, wc->pkey_index);
f198186aa   Long Li   CIFS: SMBD: Estab...
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  
  	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
  		log_rdma_recv(INFO, "wc->status=%d opcode=%d
  ",
  			wc->status, wc->opcode);
  		smbd_disconnect_rdma_connection(info);
  		goto error;
  	}
  
  	ib_dma_sync_single_for_cpu(
  		wc->qp->device,
  		response->sge.addr,
  		response->sge.length,
  		DMA_FROM_DEVICE);
  
  	switch (response->type) {
  	/* SMBD negotiation response */
  	case SMBD_NEGOTIATE_RESP:
  		dump_smbd_negotiate_resp(smbd_response_payload(response));
  		info->full_packet_received = true;
  		info->negotiate_done =
  			process_negotiation_response(response, wc->byte_len);
  		complete(&info->negotiate_completion);
  		break;
  
  	/* SMBD data transfer packet */
  	case SMBD_TRANSFER_DATA:
  		data_transfer = smbd_response_payload(response);
  		data_length = le32_to_cpu(data_transfer->data_length);
  
  		/*
  		 * If this is a packet with data playload place the data in
  		 * reassembly queue and wake up the reading thread
  		 */
  		if (data_length) {
  			if (info->full_packet_received)
  				response->first_segment = true;
  
  			if (le32_to_cpu(data_transfer->remaining_data_length))
  				info->full_packet_received = false;
  			else
  				info->full_packet_received = true;
  
  			enqueue_reassembly(
  				info,
  				response,
  				data_length);
  		} else
  			put_empty_packet(info, response);
  
  		if (data_length)
  			wake_up_interruptible(&info->wait_reassembly_queue);
  
  		atomic_dec(&info->receive_credits);
  		info->receive_credit_target =
  			le16_to_cpu(data_transfer->credits_requested);
4ebb8795a   Long Li   cifs: smbd: Check...
518
519
520
521
522
523
524
525
526
  		if (le16_to_cpu(data_transfer->credits_granted)) {
  			atomic_add(le16_to_cpu(data_transfer->credits_granted),
  				&info->send_credits);
  			/*
  			 * We have new send credits granted from remote peer
  			 * If any sender is waiting for credits, unblock it
  			 */
  			wake_up_interruptible(&info->wait_send_queue);
  		}
f198186aa   Long Li   CIFS: SMBD: Estab...
527

a0a3036b8   Joe Perches   cifs: Standardize...
528
529
530
531
532
533
  		log_incoming(INFO, "data flags %d data_offset %d data_length %d remaining_data_length %d
  ",
  			     le16_to_cpu(data_transfer->flags),
  			     le32_to_cpu(data_transfer->data_offset),
  			     le32_to_cpu(data_transfer->data_length),
  			     le32_to_cpu(data_transfer->remaining_data_length));
f198186aa   Long Li   CIFS: SMBD: Estab...
534
535
536
537
538
539
540
  
  		/* Send a KEEP_ALIVE response right away if requested */
  		info->keep_alive_requested = KEEP_ALIVE_NONE;
  		if (le16_to_cpu(data_transfer->flags) &
  				SMB_DIRECT_RESPONSE_REQUESTED) {
  			info->keep_alive_requested = KEEP_ALIVE_PENDING;
  		}
f198186aa   Long Li   CIFS: SMBD: Estab...
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  		return;
  
  	default:
  		log_rdma_recv(ERR,
  			"unexpected response type=%d
  ", response->type);
  	}
  
  error:
  	put_receive_buffer(info, response);
  }
  
  static struct rdma_cm_id *smbd_create_id(
  		struct smbd_connection *info,
  		struct sockaddr *dstaddr, int port)
  {
  	struct rdma_cm_id *id;
  	int rc;
  	__be16 *sport;
  
  	id = rdma_create_id(&init_net, smbd_conn_upcall, info,
  		RDMA_PS_TCP, IB_QPT_RC);
  	if (IS_ERR(id)) {
  		rc = PTR_ERR(id);
  		log_rdma_event(ERR, "rdma_create_id() failed %i
  ", rc);
  		return id;
  	}
  
  	if (dstaddr->sa_family == AF_INET6)
  		sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
  	else
  		sport = &((struct sockaddr_in *)dstaddr)->sin_port;
  
  	*sport = htons(port);
  
  	init_completion(&info->ri_done);
  	info->ri_rc = -ETIMEDOUT;
  
  	rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
  		RDMA_RESOLVE_TIMEOUT);
  	if (rc) {
  		log_rdma_event(ERR, "rdma_resolve_addr() failed %i
  ", rc);
  		goto out;
  	}
  	wait_for_completion_interruptible_timeout(
  		&info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
  	rc = info->ri_rc;
  	if (rc) {
  		log_rdma_event(ERR, "rdma_resolve_addr() completed %i
  ", rc);
  		goto out;
  	}
  
  	info->ri_rc = -ETIMEDOUT;
  	rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
  	if (rc) {
  		log_rdma_event(ERR, "rdma_resolve_route() failed %i
  ", rc);
  		goto out;
  	}
  	wait_for_completion_interruptible_timeout(
  		&info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
  	rc = info->ri_rc;
  	if (rc) {
  		log_rdma_event(ERR, "rdma_resolve_route() completed %i
  ", rc);
  		goto out;
  	}
  
  	return id;
  
  out:
  	rdma_destroy_id(id);
  	return ERR_PTR(rc);
  }
  
  /*
   * Test if FRWR (Fast Registration Work Requests) is supported on the device
   * This implementation requries FRWR on RDMA read/write
   * return value: true if it is supported
   */
  static bool frwr_is_supported(struct ib_device_attr *attrs)
  {
  	if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
  		return false;
  	if (attrs->max_fast_reg_page_list_len == 0)
  		return false;
  	return true;
  }
  
  static int smbd_ia_open(
  		struct smbd_connection *info,
  		struct sockaddr *dstaddr, int port)
  {
  	int rc;
  
  	info->id = smbd_create_id(info, dstaddr, port);
  	if (IS_ERR(info->id)) {
  		rc = PTR_ERR(info->id);
  		goto out1;
  	}
  
  	if (!frwr_is_supported(&info->id->device->attrs)) {
a0a3036b8   Joe Perches   cifs: Standardize...
646
647
648
649
650
651
  		log_rdma_event(ERR, "Fast Registration Work Requests (FRWR) is not supported
  ");
  		log_rdma_event(ERR, "Device capability flags = %llx max_fast_reg_page_list_len = %u
  ",
  			       info->id->device->attrs.device_cap_flags,
  			       info->id->device->attrs.max_fast_reg_page_list_len);
f198186aa   Long Li   CIFS: SMBD: Estab...
652
653
654
  		rc = -EPROTONOSUPPORT;
  		goto out2;
  	}
c73985833   Long Li   CIFS: SMBD: Imple...
655
656
657
658
659
660
  	info->max_frmr_depth = min_t(int,
  		smbd_max_frmr_depth,
  		info->id->device->attrs.max_fast_reg_page_list_len);
  	info->mr_type = IB_MR_TYPE_MEM_REG;
  	if (info->id->device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
  		info->mr_type = IB_MR_TYPE_SG_GAPS;
f198186aa   Long Li   CIFS: SMBD: Estab...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
  
  	info->pd = ib_alloc_pd(info->id->device, 0);
  	if (IS_ERR(info->pd)) {
  		rc = PTR_ERR(info->pd);
  		log_rdma_event(ERR, "ib_alloc_pd() returned %d
  ", rc);
  		goto out2;
  	}
  
  	return 0;
  
  out2:
  	rdma_destroy_id(info->id);
  	info->id = NULL;
  
  out1:
  	return rc;
  }
  
  /*
   * Send a negotiation request message to the peer
   * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
   * After negotiation, the transport is connected and ready for
   * carrying upper layer SMB payload
   */
  static int smbd_post_send_negotiate_req(struct smbd_connection *info)
  {
739305950   Bart Van Assche   fs/cifs: Simplify...
688
  	struct ib_send_wr send_wr;
f198186aa   Long Li   CIFS: SMBD: Estab...
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
  	int rc = -ENOMEM;
  	struct smbd_request *request;
  	struct smbd_negotiate_req *packet;
  
  	request = mempool_alloc(info->request_mempool, GFP_KERNEL);
  	if (!request)
  		return rc;
  
  	request->info = info;
  
  	packet = smbd_request_payload(request);
  	packet->min_version = cpu_to_le16(SMBD_V1);
  	packet->max_version = cpu_to_le16(SMBD_V1);
  	packet->reserved = 0;
  	packet->credits_requested = cpu_to_le16(info->send_credit_target);
  	packet->preferred_send_size = cpu_to_le32(info->max_send_size);
  	packet->max_receive_size = cpu_to_le32(info->max_receive_size);
  	packet->max_fragmented_size =
  		cpu_to_le32(info->max_fragmented_recv_size);
  
  	request->num_sge = 1;
  	request->sge[0].addr = ib_dma_map_single(
  				info->id->device, (void *)packet,
  				sizeof(*packet), DMA_TO_DEVICE);
  	if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
  		rc = -EIO;
  		goto dma_mapping_failed;
  	}
  
  	request->sge[0].length = sizeof(*packet);
  	request->sge[0].lkey = info->pd->local_dma_lkey;
  
  	ib_dma_sync_single_for_device(
  		info->id->device, request->sge[0].addr,
  		request->sge[0].length, DMA_TO_DEVICE);
  
  	request->cqe.done = send_done;
  
  	send_wr.next = NULL;
  	send_wr.wr_cqe = &request->cqe;
  	send_wr.sg_list = request->sge;
  	send_wr.num_sge = request->num_sge;
  	send_wr.opcode = IB_WR_SEND;
  	send_wr.send_flags = IB_SEND_SIGNALED;
  
  	log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x
  ",
  		request->sge[0].addr,
  		request->sge[0].length, request->sge[0].lkey);
f198186aa   Long Li   CIFS: SMBD: Estab...
738
  	atomic_inc(&info->send_pending);
739305950   Bart Van Assche   fs/cifs: Simplify...
739
  	rc = ib_post_send(info->id->qp, &send_wr, NULL);
f198186aa   Long Li   CIFS: SMBD: Estab...
740
741
742
743
744
745
746
747
748
  	if (!rc)
  		return 0;
  
  	/* if we reach here, post send failed */
  	log_rdma_send(ERR, "ib_post_send failed rc=%d
  ", rc);
  	atomic_dec(&info->send_pending);
  	ib_dma_unmap_single(info->id->device, request->sge[0].addr,
  		request->sge[0].length, DMA_TO_DEVICE);
21a4e14aa   Long Li   cifs: smbd: disco...
749
  	smbd_disconnect_rdma_connection(info);
f198186aa   Long Li   CIFS: SMBD: Estab...
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
  dma_mapping_failed:
  	mempool_free(request, info->request_mempool);
  	return rc;
  }
  
  /*
   * Extend the credits to remote peer
   * This implements [MS-SMBD] 3.1.5.9
   * The idea is that we should extend credits to remote peer as quickly as
   * it's allowed, to maintain data flow. We allocate as much receive
   * buffer as possible, and extend the receive credits to remote peer
   * return value: the new credtis being granted.
   */
  static int manage_credits_prior_sending(struct smbd_connection *info)
  {
  	int new_credits;
  
  	spin_lock(&info->lock_new_credits_offered);
  	new_credits = info->new_credits_offered;
  	info->new_credits_offered = 0;
  	spin_unlock(&info->lock_new_credits_offered);
  
  	return new_credits;
  }
  
  /*
   * Check if we need to send a KEEP_ALIVE message
   * The idle connection timer triggers a KEEP_ALIVE message when expires
   * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
   * back a response.
   * return value:
   * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
   * 0: otherwise
   */
  static int manage_keep_alive_before_sending(struct smbd_connection *info)
  {
  	if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
  		info->keep_alive_requested = KEEP_ALIVE_SENT;
  		return 1;
  	}
  	return 0;
  }
f1b7b862b   Long Li   cifs: smbd: Prope...
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
  /* Post the send request */
  static int smbd_post_send(struct smbd_connection *info,
  		struct smbd_request *request)
  {
  	struct ib_send_wr send_wr;
  	int rc, i;
  
  	for (i = 0; i < request->num_sge; i++) {
  		log_rdma_send(INFO,
  			"rdma_request sge[%d] addr=%llu length=%u
  ",
  			i, request->sge[i].addr, request->sge[i].length);
  		ib_dma_sync_single_for_device(
  			info->id->device,
  			request->sge[i].addr,
  			request->sge[i].length,
  			DMA_TO_DEVICE);
  	}
  
  	request->cqe.done = send_done;
  
  	send_wr.next = NULL;
  	send_wr.wr_cqe = &request->cqe;
  	send_wr.sg_list = request->sge;
  	send_wr.num_sge = request->num_sge;
  	send_wr.opcode = IB_WR_SEND;
  	send_wr.send_flags = IB_SEND_SIGNALED;
  
  	rc = ib_post_send(info->id->qp, &send_wr, NULL);
  	if (rc) {
  		log_rdma_send(ERR, "ib_post_send failed rc=%d
  ", rc);
  		smbd_disconnect_rdma_connection(info);
  		rc = -EAGAIN;
  	} else
  		/* Reset timer for idle connection after packet is sent */
  		mod_delayed_work(info->workqueue, &info->idle_timer_work,
  			info->keep_alive_interval*HZ);
  
  	return rc;
  }
  
  static int smbd_post_send_sgl(struct smbd_connection *info,
  	struct scatterlist *sgl, int data_length, int remaining_data_length)
f198186aa   Long Li   CIFS: SMBD: Estab...
836
  {
f1b7b862b   Long Li   cifs: smbd: Prope...
837
838
839
  	int num_sgs;
  	int i, rc;
  	int header_length;
f198186aa   Long Li   CIFS: SMBD: Estab...
840
841
  	struct smbd_request *request;
  	struct smbd_data_transfer *packet;
d4e5160d1   Long Li   cifs: smbd: Updat...
842
  	int new_credits;
f1b7b862b   Long Li   cifs: smbd: Prope...
843
  	struct scatterlist *sg;
f198186aa   Long Li   CIFS: SMBD: Estab...
844

f1b7b862b   Long Li   cifs: smbd: Prope...
845
  wait_credit:
f198186aa   Long Li   CIFS: SMBD: Estab...
846
847
848
849
850
  	/* Wait for send credits. A SMBD packet needs one credit */
  	rc = wait_event_interruptible(info->wait_send_queue,
  		atomic_read(&info->send_credits) > 0 ||
  		info->transport_status != SMBD_CONNECTED);
  	if (rc)
f1b7b862b   Long Li   cifs: smbd: Prope...
851
  		goto err_wait_credit;
f198186aa   Long Li   CIFS: SMBD: Estab...
852
853
  
  	if (info->transport_status != SMBD_CONNECTED) {
f1b7b862b   Long Li   cifs: smbd: Prope...
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
  		log_outgoing(ERR, "disconnected not sending on wait_credit
  ");
  		rc = -EAGAIN;
  		goto err_wait_credit;
  	}
  	if (unlikely(atomic_dec_return(&info->send_credits) < 0)) {
  		atomic_inc(&info->send_credits);
  		goto wait_credit;
  	}
  
  wait_send_queue:
  	wait_event(info->wait_post_send,
  		atomic_read(&info->send_pending) < info->send_credit_target ||
  		info->transport_status != SMBD_CONNECTED);
  
  	if (info->transport_status != SMBD_CONNECTED) {
  		log_outgoing(ERR, "disconnected not sending on wait_send_queue
  ");
  		rc = -EAGAIN;
  		goto err_wait_send_queue;
  	}
  
  	if (unlikely(atomic_inc_return(&info->send_pending) >
  				info->send_credit_target)) {
  		atomic_dec(&info->send_pending);
  		goto wait_send_queue;
f198186aa   Long Li   CIFS: SMBD: Estab...
880
  	}
f198186aa   Long Li   CIFS: SMBD: Estab...
881
882
883
884
  
  	request = mempool_alloc(info->request_mempool, GFP_KERNEL);
  	if (!request) {
  		rc = -ENOMEM;
d4e5160d1   Long Li   cifs: smbd: Updat...
885
  		goto err_alloc;
f198186aa   Long Li   CIFS: SMBD: Estab...
886
887
888
889
890
891
892
  	}
  
  	request->info = info;
  
  	/* Fill in the packet header */
  	packet = smbd_request_payload(request);
  	packet->credits_requested = cpu_to_le16(info->send_credit_target);
d4e5160d1   Long Li   cifs: smbd: Updat...
893
894
895
896
  
  	new_credits = manage_credits_prior_sending(info);
  	atomic_add(new_credits, &info->receive_credits);
  	packet->credits_granted = cpu_to_le16(new_credits);
f198186aa   Long Li   CIFS: SMBD: Estab...
897
898
899
900
901
902
903
  	info->send_immediate = false;
  
  	packet->flags = 0;
  	if (manage_keep_alive_before_sending(info))
  		packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
  
  	packet->reserved = 0;
f1b7b862b   Long Li   cifs: smbd: Prope...
904
  	if (!data_length)
f198186aa   Long Li   CIFS: SMBD: Estab...
905
906
907
  		packet->data_offset = 0;
  	else
  		packet->data_offset = cpu_to_le32(24);
f1b7b862b   Long Li   cifs: smbd: Prope...
908
  	packet->data_length = cpu_to_le32(data_length);
f198186aa   Long Li   CIFS: SMBD: Estab...
909
910
  	packet->remaining_data_length = cpu_to_le32(remaining_data_length);
  	packet->padding = 0;
a0a3036b8   Joe Perches   cifs: Standardize...
911
912
913
914
915
916
917
  	log_outgoing(INFO, "credits_requested=%d credits_granted=%d data_offset=%d data_length=%d remaining_data_length=%d
  ",
  		     le16_to_cpu(packet->credits_requested),
  		     le16_to_cpu(packet->credits_granted),
  		     le32_to_cpu(packet->data_offset),
  		     le32_to_cpu(packet->data_length),
  		     le32_to_cpu(packet->remaining_data_length));
f198186aa   Long Li   CIFS: SMBD: Estab...
918
919
920
921
  
  	/* Map the packet to DMA */
  	header_length = sizeof(struct smbd_data_transfer);
  	/* If this is a packet without payload, don't send padding */
f1b7b862b   Long Li   cifs: smbd: Prope...
922
  	if (!data_length)
f198186aa   Long Li   CIFS: SMBD: Estab...
923
924
925
926
927
928
  		header_length = offsetof(struct smbd_data_transfer, padding);
  
  	request->num_sge = 1;
  	request->sge[0].addr = ib_dma_map_single(info->id->device,
  						 (void *)packet,
  						 header_length,
7f46d23e1   Long Li   cifs:smbd Use the...
929
  						 DMA_TO_DEVICE);
f198186aa   Long Li   CIFS: SMBD: Estab...
930
  	if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
f198186aa   Long Li   CIFS: SMBD: Estab...
931
  		rc = -EIO;
f1b7b862b   Long Li   cifs: smbd: Prope...
932
  		request->sge[0].addr = 0;
d4e5160d1   Long Li   cifs: smbd: Updat...
933
  		goto err_dma;
f198186aa   Long Li   CIFS: SMBD: Estab...
934
935
936
937
  	}
  
  	request->sge[0].length = header_length;
  	request->sge[0].lkey = info->pd->local_dma_lkey;
f1b7b862b   Long Li   cifs: smbd: Prope...
938
  	/* Fill in the packet data payload */
f198186aa   Long Li   CIFS: SMBD: Estab...
939
940
941
942
  	num_sgs = sgl ? sg_nents(sgl) : 0;
  	for_each_sg(sgl, sg, num_sgs, i) {
  		request->sge[i+1].addr =
  			ib_dma_map_page(info->id->device, sg_page(sg),
7f46d23e1   Long Li   cifs:smbd Use the...
943
  			       sg->offset, sg->length, DMA_TO_DEVICE);
f198186aa   Long Li   CIFS: SMBD: Estab...
944
945
946
947
  		if (ib_dma_mapping_error(
  				info->id->device, request->sge[i+1].addr)) {
  			rc = -EIO;
  			request->sge[i+1].addr = 0;
f1b7b862b   Long Li   cifs: smbd: Prope...
948
  			goto err_dma;
f198186aa   Long Li   CIFS: SMBD: Estab...
949
950
951
952
953
  		}
  		request->sge[i+1].length = sg->length;
  		request->sge[i+1].lkey = info->pd->local_dma_lkey;
  		request->num_sge++;
  	}
072a14ec6   Long Li   cifs: smbd: Merge...
954
  	rc = smbd_post_send(info, request);
f198186aa   Long Li   CIFS: SMBD: Estab...
955
956
  	if (!rc)
  		return 0;
f1b7b862b   Long Li   cifs: smbd: Prope...
957
958
  err_dma:
  	for (i = 0; i < request->num_sge; i++)
f198186aa   Long Li   CIFS: SMBD: Estab...
959
960
961
962
963
  		if (request->sge[i].addr)
  			ib_dma_unmap_single(info->id->device,
  					    request->sge[i].addr,
  					    request->sge[i].length,
  					    DMA_TO_DEVICE);
f1b7b862b   Long Li   cifs: smbd: Prope...
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
  	mempool_free(request, info->request_mempool);
  
  	/* roll back receive credits and credits to be offered */
  	spin_lock(&info->lock_new_credits_offered);
  	info->new_credits_offered += new_credits;
  	spin_unlock(&info->lock_new_credits_offered);
  	atomic_sub(new_credits, &info->receive_credits);
  
  err_alloc:
  	if (atomic_dec_and_test(&info->send_pending))
  		wake_up(&info->wait_send_pending);
  
  err_wait_send_queue:
  	/* roll back send credits and pending */
  	atomic_inc(&info->send_credits);
  
  err_wait_credit:
f198186aa   Long Li   CIFS: SMBD: Estab...
981
982
983
984
  	return rc;
  }
  
  /*
d649e1bba   Long Li   CIFS: SMBD: Imple...
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
   * Send a page
   * page: the page to send
   * offset: offset in the page to send
   * size: length in the page to send
   * remaining_data_length: remaining data to send in this payload
   */
  static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
  		unsigned long offset, size_t size, int remaining_data_length)
  {
  	struct scatterlist sgl;
  
  	sg_init_table(&sgl, 1);
  	sg_set_page(&sgl, page, size, offset);
  
  	return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
  }
  
  /*
f198186aa   Long Li   CIFS: SMBD: Estab...
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
   * Send an empty message
   * Empty message is used to extend credits to peer to for keep live
   * while there is no upper layer payload to send at the time
   */
  static int smbd_post_send_empty(struct smbd_connection *info)
  {
  	info->count_send_empty++;
  	return smbd_post_send_sgl(info, NULL, 0, 0);
  }
  
  /*
d649e1bba   Long Li   CIFS: SMBD: Imple...
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
   * Send a data buffer
   * iov: the iov array describing the data buffers
   * n_vec: number of iov array
   * remaining_data_length: remaining data to send following this packet
   * in segmented SMBD packet
   */
  static int smbd_post_send_data(
  	struct smbd_connection *info, struct kvec *iov, int n_vec,
  	int remaining_data_length)
  {
  	int i;
  	u32 data_length = 0;
  	struct scatterlist sgl[SMBDIRECT_MAX_SGE];
  
  	if (n_vec > SMBDIRECT_MAX_SGE) {
  		cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d
  ", n_vec);
37941ea17   Long Li   cifs: smbd: Retur...
1031
  		return -EINVAL;
d649e1bba   Long Li   CIFS: SMBD: Imple...
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  	}
  
  	sg_init_table(sgl, n_vec);
  	for (i = 0; i < n_vec; i++) {
  		data_length += iov[i].iov_len;
  		sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
  	}
  
  	return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
  }
  
  /*
f198186aa   Long Li   CIFS: SMBD: Estab...
1044
1045
1046
1047
1048
1049
1050
   * Post a receive request to the transport
   * The remote peer can only send data when a receive request is posted
   * The interaction is controlled by send/receive credit system
   */
  static int smbd_post_recv(
  		struct smbd_connection *info, struct smbd_response *response)
  {
739305950   Bart Van Assche   fs/cifs: Simplify...
1051
  	struct ib_recv_wr recv_wr;
f198186aa   Long Li   CIFS: SMBD: Estab...
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
  	int rc = -EIO;
  
  	response->sge.addr = ib_dma_map_single(
  				info->id->device, response->packet,
  				info->max_receive_size, DMA_FROM_DEVICE);
  	if (ib_dma_mapping_error(info->id->device, response->sge.addr))
  		return rc;
  
  	response->sge.length = info->max_receive_size;
  	response->sge.lkey = info->pd->local_dma_lkey;
  
  	response->cqe.done = recv_done;
  
  	recv_wr.wr_cqe = &response->cqe;
  	recv_wr.next = NULL;
  	recv_wr.sg_list = &response->sge;
  	recv_wr.num_sge = 1;
739305950   Bart Van Assche   fs/cifs: Simplify...
1069
  	rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
f198186aa   Long Li   CIFS: SMBD: Estab...
1070
1071
1072
  	if (rc) {
  		ib_dma_unmap_single(info->id->device, response->sge.addr,
  				    response->sge.length, DMA_FROM_DEVICE);
21a4e14aa   Long Li   cifs: smbd: disco...
1073
  		smbd_disconnect_rdma_connection(info);
f198186aa   Long Li   CIFS: SMBD: Estab...
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
  		log_rdma_recv(ERR, "ib_post_recv failed rc=%d
  ", rc);
  	}
  
  	return rc;
  }
  
  /* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
  static int smbd_negotiate(struct smbd_connection *info)
  {
  	int rc;
  	struct smbd_response *response = get_receive_buffer(info);
  
  	response->type = SMBD_NEGOTIATE_RESP;
  	rc = smbd_post_recv(info, response);
a0a3036b8   Joe Perches   cifs: Standardize...
1089
1090
1091
1092
  	log_rdma_event(INFO, "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x iov.lkey=%x
  ",
  		       rc, response->sge.addr,
  		       response->sge.length, response->sge.lkey);
f198186aa   Long Li   CIFS: SMBD: Estab...
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
  	if (rc)
  		return rc;
  
  	init_completion(&info->negotiate_completion);
  	info->negotiate_done = false;
  	rc = smbd_post_send_negotiate_req(info);
  	if (rc)
  		return rc;
  
  	rc = wait_for_completion_interruptible_timeout(
  		&info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
  	log_rdma_event(INFO, "wait_for_completion_timeout rc=%d
  ", rc);
  
  	if (info->negotiate_done)
  		return 0;
  
  	if (rc == 0)
  		rc = -ETIMEDOUT;
  	else if (rc == -ERESTARTSYS)
  		rc = -EINTR;
  	else
  		rc = -ENOTCONN;
  
  	return rc;
  }
  
  static void put_empty_packet(
  		struct smbd_connection *info, struct smbd_response *response)
  {
  	spin_lock(&info->empty_packet_queue_lock);
  	list_add_tail(&response->list, &info->empty_packet_queue);
  	info->count_empty_packet_queue++;
  	spin_unlock(&info->empty_packet_queue_lock);
  
  	queue_work(info->workqueue, &info->post_send_credits_work);
  }
  
  /*
   * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
   * This is a queue for reassembling upper layer payload and present to upper
   * layer. All the inncoming payload go to the reassembly queue, regardless of
   * if reassembly is required. The uuper layer code reads from the queue for all
   * incoming payloads.
   * Put a received packet to the reassembly queue
   * response: the packet received
   * data_length: the size of payload in this packet
   */
  static void enqueue_reassembly(
  	struct smbd_connection *info,
  	struct smbd_response *response,
  	int data_length)
  {
  	spin_lock(&info->reassembly_queue_lock);
  	list_add_tail(&response->list, &info->reassembly_queue);
  	info->reassembly_queue_length++;
  	/*
  	 * Make sure reassembly_data_length is updated after list and
  	 * reassembly_queue_length are updated. On the dequeue side
  	 * reassembly_data_length is checked without a lock to determine
  	 * if reassembly_queue_length and list is up to date
  	 */
  	virt_wmb();
  	info->reassembly_data_length += data_length;
  	spin_unlock(&info->reassembly_queue_lock);
  	info->count_reassembly_queue++;
  	info->count_enqueue_reassembly_queue++;
  }
  
  /*
   * Get the first entry at the front of reassembly queue
   * Caller is responsible for locking
   * return value: the first entry if any, NULL if queue is empty
   */
  static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
  {
  	struct smbd_response *ret = NULL;
  
  	if (!list_empty(&info->reassembly_queue)) {
  		ret = list_first_entry(
  			&info->reassembly_queue,
  			struct smbd_response, list);
  	}
  	return ret;
  }
  
  static struct smbd_response *get_empty_queue_buffer(
  		struct smbd_connection *info)
  {
  	struct smbd_response *ret = NULL;
  	unsigned long flags;
  
  	spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
  	if (!list_empty(&info->empty_packet_queue)) {
  		ret = list_first_entry(
  			&info->empty_packet_queue,
  			struct smbd_response, list);
  		list_del(&ret->list);
  		info->count_empty_packet_queue--;
  	}
  	spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
  
  	return ret;
  }
  
  /*
   * Get a receive buffer
   * For each remote send, we need to post a receive. The receive buffers are
   * pre-allocated in advance.
   * return value: the receive buffer, NULL if none is available
   */
  static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
  {
  	struct smbd_response *ret = NULL;
  	unsigned long flags;
  
  	spin_lock_irqsave(&info->receive_queue_lock, flags);
  	if (!list_empty(&info->receive_queue)) {
  		ret = list_first_entry(
  			&info->receive_queue,
  			struct smbd_response, list);
  		list_del(&ret->list);
  		info->count_receive_queue--;
  		info->count_get_receive_buffer++;
  	}
  	spin_unlock_irqrestore(&info->receive_queue_lock, flags);
  
  	return ret;
  }
  
  /*
   * Return a receive buffer
   * Upon returning of a receive buffer, we can post new receive and extend
   * more receive credits to remote peer. This is done immediately after a
   * receive buffer is returned.
   */
  static void put_receive_buffer(
  	struct smbd_connection *info, struct smbd_response *response)
  {
  	unsigned long flags;
  
  	ib_dma_unmap_single(info->id->device, response->sge.addr,
  		response->sge.length, DMA_FROM_DEVICE);
  
  	spin_lock_irqsave(&info->receive_queue_lock, flags);
  	list_add_tail(&response->list, &info->receive_queue);
  	info->count_receive_queue++;
  	info->count_put_receive_buffer++;
  	spin_unlock_irqrestore(&info->receive_queue_lock, flags);
  
  	queue_work(info->workqueue, &info->post_send_credits_work);
  }
  
  /* Preallocate all receive buffer on transport establishment */
  static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
  {
  	int i;
  	struct smbd_response *response;
  
  	INIT_LIST_HEAD(&info->reassembly_queue);
  	spin_lock_init(&info->reassembly_queue_lock);
  	info->reassembly_data_length = 0;
  	info->reassembly_queue_length = 0;
  
  	INIT_LIST_HEAD(&info->receive_queue);
  	spin_lock_init(&info->receive_queue_lock);
  	info->count_receive_queue = 0;
  
  	INIT_LIST_HEAD(&info->empty_packet_queue);
  	spin_lock_init(&info->empty_packet_queue_lock);
  	info->count_empty_packet_queue = 0;
  
  	init_waitqueue_head(&info->wait_receive_queues);
  
  	for (i = 0; i < num_buf; i++) {
  		response = mempool_alloc(info->response_mempool, GFP_KERNEL);
  		if (!response)
  			goto allocate_failed;
  
  		response->info = info;
  		list_add_tail(&response->list, &info->receive_queue);
  		info->count_receive_queue++;
  	}
  
  	return 0;
  
  allocate_failed:
  	while (!list_empty(&info->receive_queue)) {
  		response = list_first_entry(
  				&info->receive_queue,
  				struct smbd_response, list);
  		list_del(&response->list);
  		info->count_receive_queue--;
  
  		mempool_free(response, info->response_mempool);
  	}
  	return -ENOMEM;
  }
  
  static void destroy_receive_buffers(struct smbd_connection *info)
  {
  	struct smbd_response *response;
  
  	while ((response = get_receive_buffer(info)))
  		mempool_free(response, info->response_mempool);
  
  	while ((response = get_empty_queue_buffer(info)))
  		mempool_free(response, info->response_mempool);
  }
f198186aa   Long Li   CIFS: SMBD: Estab...
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
  /* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
  static void idle_connection_timer(struct work_struct *work)
  {
  	struct smbd_connection *info = container_of(
  					work, struct smbd_connection,
  					idle_timer_work.work);
  
  	if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
  		log_keep_alive(ERR,
  			"error status info->keep_alive_requested=%d
  ",
  			info->keep_alive_requested);
  		smbd_disconnect_rdma_connection(info);
  		return;
  	}
  
  	log_keep_alive(INFO, "about to send an empty idle message
  ");
  	smbd_post_send_empty(info);
  
  	/* Setup the next idle timeout work */
  	queue_delayed_work(info->workqueue, &info->idle_timer_work,
  			info->keep_alive_interval*HZ);
  }
050b8c374   Long Li   smbd: Make upper ...
1326
1327
1328
1329
1330
1331
  /*
   * Destroy the transport and related RDMA and memory resources
   * Need to go through all the pending counters and make sure on one is using
   * the transport while it is destroyed
   */
  void smbd_destroy(struct TCP_Server_Info *server)
8ef130f9e   Long Li   CIFS: SMBD: Imple...
1332
  {
050b8c374   Long Li   smbd: Make upper ...
1333
1334
1335
1336
1337
1338
1339
1340
1341
  	struct smbd_connection *info = server->smbd_conn;
  	struct smbd_response *response;
  	unsigned long flags;
  
  	if (!info) {
  		log_rdma_event(INFO, "rdma session already destroyed
  ");
  		return;
  	}
8ef130f9e   Long Li   CIFS: SMBD: Imple...
1342
1343
  	log_rdma_event(INFO, "destroying rdma session
  ");
050b8c374   Long Li   smbd: Make upper ...
1344
1345
1346
1347
  	if (info->transport_status != SMBD_DISCONNECTED) {
  		rdma_disconnect(server->smbd_conn->id);
  		log_rdma_event(INFO, "wait for transport being disconnected
  ");
e8b3bfe9b   Long Li   cifs: smbd: Don't...
1348
  		wait_event_interruptible(
050b8c374   Long Li   smbd: Make upper ...
1349
1350
1351
  			info->disconn_wait,
  			info->transport_status == SMBD_DISCONNECTED);
  	}
8ef130f9e   Long Li   CIFS: SMBD: Imple...
1352

050b8c374   Long Li   smbd: Make upper ...
1353
1354
1355
1356
1357
1358
1359
1360
  	log_rdma_event(INFO, "destroying qp
  ");
  	ib_drain_qp(info->id->qp);
  	rdma_destroy_qp(info->id);
  
  	log_rdma_event(INFO, "cancelling idle timer
  ");
  	cancel_delayed_work_sync(&info->idle_timer_work);
8ef130f9e   Long Li   CIFS: SMBD: Imple...
1361

050b8c374   Long Li   smbd: Make upper ...
1362
1363
1364
1365
  	log_rdma_event(INFO, "wait for all send posted to IB to finish
  ");
  	wait_event(info->wait_send_pending,
  		atomic_read(&info->send_pending) == 0);
050b8c374   Long Li   smbd: Make upper ...
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
  
  	/* It's not posssible for upper layer to get to reassembly */
  	log_rdma_event(INFO, "drain the reassembly queue
  ");
  	do {
  		spin_lock_irqsave(&info->reassembly_queue_lock, flags);
  		response = _get_first_reassembly(info);
  		if (response) {
  			list_del(&response->list);
  			spin_unlock_irqrestore(
  				&info->reassembly_queue_lock, flags);
  			put_receive_buffer(info, response);
  		} else
  			spin_unlock_irqrestore(
  				&info->reassembly_queue_lock, flags);
  	} while (response);
  	info->reassembly_data_length = 0;
  
  	log_rdma_event(INFO, "free receive buffers
  ");
  	wait_event(info->wait_receive_queues,
  		info->count_receive_queue + info->count_empty_packet_queue
  			== info->receive_credit_max);
  	destroy_receive_buffers(info);
  
  	/*
  	 * For performance reasons, memory registration and deregistration
  	 * are not locked by srv_mutex. It is possible some processes are
  	 * blocked on transport srv_mutex while holding memory registration.
  	 * Release the transport srv_mutex to allow them to hit the failure
  	 * path when sending data, and then release memory registartions.
  	 */
  	log_rdma_event(INFO, "freeing mr list
  ");
  	wake_up_interruptible_all(&info->wait_mr);
  	while (atomic_read(&info->mr_used_count)) {
  		mutex_unlock(&server->srv_mutex);
  		msleep(1000);
  		mutex_lock(&server->srv_mutex);
  	}
  	destroy_mr_list(info);
  
  	ib_free_cq(info->send_cq);
  	ib_free_cq(info->recv_cq);
  	ib_dealloc_pd(info->pd);
  	rdma_destroy_id(info->id);
  
  	/* free mempools */
  	mempool_destroy(info->request_mempool);
  	kmem_cache_destroy(info->request_cache);
  
  	mempool_destroy(info->response_mempool);
  	kmem_cache_destroy(info->response_cache);
  
  	info->transport_status = SMBD_DESTROYED;
8ef130f9e   Long Li   CIFS: SMBD: Imple...
1421
1422
  
  	destroy_workqueue(info->workqueue);
d63cdbae6   Long Li   cifs: smbd: Add m...
1423
1424
  	log_rdma_event(INFO,  "rdma session destroyed
  ");
8ef130f9e   Long Li   CIFS: SMBD: Imple...
1425
1426
  	kfree(info);
  }
ad57b8e17   Long Li   CIFS: SMBD: Imple...
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
  /*
   * Reconnect this SMBD connection, called from upper layer
   * return value: 0 on success, or actual error code
   */
  int smbd_reconnect(struct TCP_Server_Info *server)
  {
  	log_rdma_event(INFO, "reconnecting rdma session
  ");
  
  	if (!server->smbd_conn) {
48f238a79   Long Li   cifs: smbd: avoid...
1437
1438
1439
  		log_rdma_event(INFO, "rdma session already destroyed
  ");
  		goto create_conn;
ad57b8e17   Long Li   CIFS: SMBD: Imple...
1440
1441
1442
1443
1444
1445
1446
1447
1448
  	}
  
  	/*
  	 * This is possible if transport is disconnected and we haven't received
  	 * notification from RDMA, but upper layer has detected timeout
  	 */
  	if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
  		log_rdma_event(INFO, "disconnecting transport
  ");
050b8c374   Long Li   smbd: Make upper ...
1449
  		smbd_destroy(server);
ad57b8e17   Long Li   CIFS: SMBD: Imple...
1450
  	}
48f238a79   Long Li   cifs: smbd: avoid...
1451
  create_conn:
ad57b8e17   Long Li   CIFS: SMBD: Imple...
1452
1453
1454
1455
  	log_rdma_event(INFO, "creating rdma session
  ");
  	server->smbd_conn = smbd_get_connection(
  		server, (struct sockaddr *) &server->dstaddr);
d63cdbae6   Long Li   cifs: smbd: Add m...
1456
1457
1458
1459
  
  	if (server->smbd_conn)
  		cifs_dbg(VFS, "RDMA transport re-established
  ");
ad57b8e17   Long Li   CIFS: SMBD: Imple...
1460
1461
1462
  
  	return server->smbd_conn ? 0 : -ENOENT;
  }
f198186aa   Long Li   CIFS: SMBD: Estab...
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
  static void destroy_caches_and_workqueue(struct smbd_connection *info)
  {
  	destroy_receive_buffers(info);
  	destroy_workqueue(info->workqueue);
  	mempool_destroy(info->response_mempool);
  	kmem_cache_destroy(info->response_cache);
  	mempool_destroy(info->request_mempool);
  	kmem_cache_destroy(info->request_cache);
  }
  
  #define MAX_NAME_LEN	80
  static int allocate_caches_and_workqueue(struct smbd_connection *info)
  {
  	char name[MAX_NAME_LEN];
  	int rc;
74ea5f983   Ronnie Sahlberg   cifs: replace snp...
1478
  	scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
f198186aa   Long Li   CIFS: SMBD: Estab...
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
  	info->request_cache =
  		kmem_cache_create(
  			name,
  			sizeof(struct smbd_request) +
  				sizeof(struct smbd_data_transfer),
  			0, SLAB_HWCACHE_ALIGN, NULL);
  	if (!info->request_cache)
  		return -ENOMEM;
  
  	info->request_mempool =
  		mempool_create(info->send_credit_target, mempool_alloc_slab,
  			mempool_free_slab, info->request_cache);
  	if (!info->request_mempool)
  		goto out1;
74ea5f983   Ronnie Sahlberg   cifs: replace snp...
1493
  	scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
f198186aa   Long Li   CIFS: SMBD: Estab...
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
  	info->response_cache =
  		kmem_cache_create(
  			name,
  			sizeof(struct smbd_response) +
  				info->max_receive_size,
  			0, SLAB_HWCACHE_ALIGN, NULL);
  	if (!info->response_cache)
  		goto out2;
  
  	info->response_mempool =
  		mempool_create(info->receive_credit_max, mempool_alloc_slab,
  		       mempool_free_slab, info->response_cache);
  	if (!info->response_mempool)
  		goto out3;
74ea5f983   Ronnie Sahlberg   cifs: replace snp...
1508
  	scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
f198186aa   Long Li   CIFS: SMBD: Estab...
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
  	info->workqueue = create_workqueue(name);
  	if (!info->workqueue)
  		goto out4;
  
  	rc = allocate_receive_buffers(info, info->receive_credit_max);
  	if (rc) {
  		log_rdma_event(ERR, "failed to allocate receive buffers
  ");
  		goto out5;
  	}
  
  	return 0;
  
  out5:
  	destroy_workqueue(info->workqueue);
  out4:
  	mempool_destroy(info->response_mempool);
  out3:
  	kmem_cache_destroy(info->response_cache);
  out2:
  	mempool_destroy(info->request_mempool);
  out1:
  	kmem_cache_destroy(info->request_cache);
  	return -ENOMEM;
  }
  
  /* Create a SMBD connection, called by upper layer */
9084432c3   kbuild test robot   CIFS: SMBD: _smbd...
1536
  static struct smbd_connection *_smbd_get_connection(
f198186aa   Long Li   CIFS: SMBD: Estab...
1537
1538
1539
1540
1541
1542
1543
  	struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
  {
  	int rc;
  	struct smbd_connection *info;
  	struct rdma_conn_param conn_param;
  	struct ib_qp_init_attr qp_attr;
  	struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
c73985833   Long Li   CIFS: SMBD: Imple...
1544
1545
  	struct ib_port_immutable port_immutable;
  	u32 ird_ord_hdr[2];
f198186aa   Long Li   CIFS: SMBD: Estab...
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
  
  	info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
  	if (!info)
  		return NULL;
  
  	info->transport_status = SMBD_CONNECTING;
  	rc = smbd_ia_open(info, dstaddr, port);
  	if (rc) {
  		log_rdma_event(INFO, "smbd_ia_open rc=%d
  ", rc);
  		goto create_id_failed;
  	}
  
  	if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
  	    smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
a0a3036b8   Joe Perches   cifs: Standardize...
1561
1562
1563
1564
1565
  		log_rdma_event(ERR, "consider lowering send_credit_target = %d. Possible CQE overrun, device reporting max_cpe %d max_qp_wr %d
  ",
  			       smbd_send_credit_target,
  			       info->id->device->attrs.max_cqe,
  			       info->id->device->attrs.max_qp_wr);
f198186aa   Long Li   CIFS: SMBD: Estab...
1566
1567
1568
1569
1570
  		goto config_failed;
  	}
  
  	if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
  	    smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
a0a3036b8   Joe Perches   cifs: Standardize...
1571
1572
1573
1574
1575
  		log_rdma_event(ERR, "consider lowering receive_credit_max = %d. Possible CQE overrun, device reporting max_cpe %d max_qp_wr %d
  ",
  			       smbd_receive_credit_max,
  			       info->id->device->attrs.max_cqe,
  			       info->id->device->attrs.max_qp_wr);
f198186aa   Long Li   CIFS: SMBD: Estab...
1576
1577
1578
1579
1580
1581
1582
1583
1584
  		goto config_failed;
  	}
  
  	info->receive_credit_max = smbd_receive_credit_max;
  	info->send_credit_target = smbd_send_credit_target;
  	info->max_send_size = smbd_max_send_size;
  	info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
  	info->max_receive_size = smbd_max_receive_size;
  	info->keep_alive_interval = smbd_keep_alive_interval;
33023fb85   Steve Wise   IB/core: add max_...
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
  	if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
  		log_rdma_event(ERR,
  			"warning: device max_send_sge = %d too small
  ",
  			info->id->device->attrs.max_send_sge);
  		log_rdma_event(ERR, "Queue Pair creation may fail
  ");
  	}
  	if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
  		log_rdma_event(ERR,
  			"warning: device max_recv_sge = %d too small
  ",
  			info->id->device->attrs.max_recv_sge);
f198186aa   Long Li   CIFS: SMBD: Estab...
1598
1599
1600
1601
1602
1603
  		log_rdma_event(ERR, "Queue Pair creation may fail
  ");
  	}
  
  	info->send_cq = NULL;
  	info->recv_cq = NULL;
20cf4e026   Chuck Lever   rdma: Enable ib_a...
1604
1605
1606
  	info->send_cq =
  		ib_alloc_cq_any(info->id->device, info,
  				info->send_credit_target, IB_POLL_SOFTIRQ);
f198186aa   Long Li   CIFS: SMBD: Estab...
1607
1608
1609
1610
  	if (IS_ERR(info->send_cq)) {
  		info->send_cq = NULL;
  		goto alloc_cq_failed;
  	}
20cf4e026   Chuck Lever   rdma: Enable ib_a...
1611
1612
1613
  	info->recv_cq =
  		ib_alloc_cq_any(info->id->device, info,
  				info->receive_credit_max, IB_POLL_SOFTIRQ);
f198186aa   Long Li   CIFS: SMBD: Estab...
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
  	if (IS_ERR(info->recv_cq)) {
  		info->recv_cq = NULL;
  		goto alloc_cq_failed;
  	}
  
  	memset(&qp_attr, 0, sizeof(qp_attr));
  	qp_attr.event_handler = smbd_qp_async_error_upcall;
  	qp_attr.qp_context = info;
  	qp_attr.cap.max_send_wr = info->send_credit_target;
  	qp_attr.cap.max_recv_wr = info->receive_credit_max;
  	qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
  	qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
  	qp_attr.cap.max_inline_data = 0;
  	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  	qp_attr.qp_type = IB_QPT_RC;
  	qp_attr.send_cq = info->send_cq;
  	qp_attr.recv_cq = info->recv_cq;
  	qp_attr.port_num = ~0;
  
  	rc = rdma_create_qp(info->id, info->pd, &qp_attr);
  	if (rc) {
  		log_rdma_event(ERR, "rdma_create_qp failed %i
  ", rc);
  		goto create_qp_failed;
  	}
  
  	memset(&conn_param, 0, sizeof(conn_param));
  	conn_param.initiator_depth = 0;
c73985833   Long Li   CIFS: SMBD: Imple...
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
  	conn_param.responder_resources =
  		info->id->device->attrs.max_qp_rd_atom
  			< SMBD_CM_RESPONDER_RESOURCES ?
  		info->id->device->attrs.max_qp_rd_atom :
  		SMBD_CM_RESPONDER_RESOURCES;
  	info->responder_resources = conn_param.responder_resources;
  	log_rdma_mr(INFO, "responder_resources=%d
  ",
  		info->responder_resources);
  
  	/* Need to send IRD/ORD in private data for iWARP */
3023a1e93   Kamal Heib   RDMA: Start use i...
1653
  	info->id->device->ops.get_port_immutable(
c73985833   Long Li   CIFS: SMBD: Imple...
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
  		info->id->device, info->id->port_num, &port_immutable);
  	if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
  		ird_ord_hdr[0] = info->responder_resources;
  		ird_ord_hdr[1] = 1;
  		conn_param.private_data = ird_ord_hdr;
  		conn_param.private_data_len = sizeof(ird_ord_hdr);
  	} else {
  		conn_param.private_data = NULL;
  		conn_param.private_data_len = 0;
  	}
f198186aa   Long Li   CIFS: SMBD: Estab...
1664
1665
1666
  	conn_param.retry_count = SMBD_CM_RETRY;
  	conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
  	conn_param.flow_control = 0;
f198186aa   Long Li   CIFS: SMBD: Estab...
1667
1668
1669
1670
1671
1672
  
  	log_rdma_event(INFO, "connecting to IP %pI4 port %d
  ",
  		&addr_in->sin_addr, port);
  
  	init_waitqueue_head(&info->conn_wait);
050b8c374   Long Li   smbd: Make upper ...
1673
1674
  	init_waitqueue_head(&info->disconn_wait);
  	init_waitqueue_head(&info->wait_reassembly_queue);
f198186aa   Long Li   CIFS: SMBD: Estab...
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
  	rc = rdma_connect(info->id, &conn_param);
  	if (rc) {
  		log_rdma_event(ERR, "rdma_connect() failed with %i
  ", rc);
  		goto rdma_connect_failed;
  	}
  
  	wait_event_interruptible(
  		info->conn_wait, info->transport_status != SMBD_CONNECTING);
  
  	if (info->transport_status != SMBD_CONNECTED) {
  		log_rdma_event(ERR, "rdma_connect failed port=%d
  ", port);
  		goto rdma_connect_failed;
  	}
  
  	log_rdma_event(INFO, "rdma_connect connected
  ");
  
  	rc = allocate_caches_and_workqueue(info);
  	if (rc) {
  		log_rdma_event(ERR, "cache allocation failed
  ");
  		goto allocate_cache_failed;
  	}
  
  	init_waitqueue_head(&info->wait_send_queue);
f198186aa   Long Li   CIFS: SMBD: Estab...
1702
  	INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
f198186aa   Long Li   CIFS: SMBD: Estab...
1703
1704
1705
1706
1707
  	queue_delayed_work(info->workqueue, &info->idle_timer_work,
  		info->keep_alive_interval*HZ);
  
  	init_waitqueue_head(&info->wait_send_pending);
  	atomic_set(&info->send_pending, 0);
3ffbe78af   Long Li   cifs: smbd: Check...
1708
  	init_waitqueue_head(&info->wait_post_send);
f198186aa   Long Li   CIFS: SMBD: Estab...
1709
1710
  
  	INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
f198186aa   Long Li   CIFS: SMBD: Estab...
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
  	INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
  	info->new_credits_offered = 0;
  	spin_lock_init(&info->lock_new_credits_offered);
  
  	rc = smbd_negotiate(info);
  	if (rc) {
  		log_rdma_event(ERR, "smbd_negotiate rc=%d
  ", rc);
  		goto negotiation_failed;
  	}
c73985833   Long Li   CIFS: SMBD: Imple...
1721
1722
1723
1724
1725
1726
  	rc = allocate_mr_list(info);
  	if (rc) {
  		log_rdma_mr(ERR, "memory registration allocation failed
  ");
  		goto allocate_mr_failed;
  	}
f198186aa   Long Li   CIFS: SMBD: Estab...
1727
  	return info;
c73985833   Long Li   CIFS: SMBD: Imple...
1728
1729
  allocate_mr_failed:
  	/* At this point, need to a full transport shutdown */
050b8c374   Long Li   smbd: Make upper ...
1730
  	smbd_destroy(server);
c73985833   Long Li   CIFS: SMBD: Imple...
1731
  	return NULL;
f198186aa   Long Li   CIFS: SMBD: Estab...
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
  negotiation_failed:
  	cancel_delayed_work_sync(&info->idle_timer_work);
  	destroy_caches_and_workqueue(info);
  	info->transport_status = SMBD_NEGOTIATE_FAILED;
  	init_waitqueue_head(&info->conn_wait);
  	rdma_disconnect(info->id);
  	wait_event(info->conn_wait,
  		info->transport_status == SMBD_DISCONNECTED);
  
  allocate_cache_failed:
  rdma_connect_failed:
  	rdma_destroy_qp(info->id);
  
  create_qp_failed:
  alloc_cq_failed:
  	if (info->send_cq)
  		ib_free_cq(info->send_cq);
  	if (info->recv_cq)
  		ib_free_cq(info->recv_cq);
  
  config_failed:
  	ib_dealloc_pd(info->pd);
  	rdma_destroy_id(info->id);
  
  create_id_failed:
  	kfree(info);
  	return NULL;
  }
399f9539d   Long Li   CIFS: SMBD: Imple...
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
  
  struct smbd_connection *smbd_get_connection(
  	struct TCP_Server_Info *server, struct sockaddr *dstaddr)
  {
  	struct smbd_connection *ret;
  	int port = SMBD_PORT;
  
  try_again:
  	ret = _smbd_get_connection(server, dstaddr, port);
  
  	/* Try SMB_PORT if SMBD_PORT doesn't work */
  	if (!ret && port == SMBD_PORT) {
  		port = SMB_PORT;
  		goto try_again;
  	}
  	return ret;
  }
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
  
  /*
   * Receive data from receive reassembly queue
   * All the incoming data packets are placed in reassembly queue
   * buf: the buffer to read data into
   * size: the length of data to read
   * return value: actual data read
   * Note: this implementation copies the data from reassebmly queue to receive
   * buffers used by upper layer. This is not the optimal code path. A better way
   * to do it is to not have upper layer allocate its receive buffers but rather
   * borrow the buffer from reassembly queue, and return it after data is
   * consumed. But this will require more changes to upper layer code, and also
   * need to consider packet boundaries while they still being reassembled.
   */
2026b06e9   Steve French   Cleanup some mino...
1791
1792
  static int smbd_recv_buf(struct smbd_connection *info, char *buf,
  		unsigned int size)
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1793
1794
1795
1796
1797
1798
  {
  	struct smbd_response *response;
  	struct smbd_data_transfer *data_transfer;
  	int to_copy, to_read, data_read, offset;
  	u32 data_length, remaining_data_length, data_offset;
  	int rc;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1799
1800
  
  again:
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
  	/*
  	 * No need to hold the reassembly queue lock all the time as we are
  	 * the only one reading from the front of the queue. The transport
  	 * may add more entries to the back of the queue at the same time
  	 */
  	log_read(INFO, "size=%d info->reassembly_data_length=%d
  ", size,
  		info->reassembly_data_length);
  	if (info->reassembly_data_length >= size) {
  		int queue_length;
  		int queue_removed = 0;
  
  		/*
  		 * Need to make sure reassembly_data_length is read before
  		 * reading reassembly_queue_length and calling
  		 * _get_first_reassembly. This call is lock free
  		 * as we never read at the end of the queue which are being
  		 * updated in SOFTIRQ as more data is received
  		 */
  		virt_rmb();
  		queue_length = info->reassembly_queue_length;
  		data_read = 0;
  		to_read = size;
  		offset = info->first_entry_offset;
  		while (data_read < size) {
  			response = _get_first_reassembly(info);
  			data_transfer = smbd_response_payload(response);
  			data_length = le32_to_cpu(data_transfer->data_length);
  			remaining_data_length =
  				le32_to_cpu(
  					data_transfer->remaining_data_length);
  			data_offset = le32_to_cpu(data_transfer->data_offset);
  
  			/*
  			 * The upper layer expects RFC1002 length at the
  			 * beginning of the payload. Return it to indicate
  			 * the total length of the packet. This minimize the
  			 * change to upper layer packet processing logic. This
  			 * will be eventually remove when an intermediate
  			 * transport layer is added
  			 */
  			if (response->first_segment && size == 4) {
  				unsigned int rfc1002_len =
  					data_length + remaining_data_length;
  				*((__be32 *)buf) = cpu_to_be32(rfc1002_len);
  				data_read = 4;
  				response->first_segment = false;
  				log_read(INFO, "returning rfc1002 length %d
  ",
  					rfc1002_len);
  				goto read_rfc1002_done;
  			}
  
  			to_copy = min_t(int, data_length - offset, to_read);
  			memcpy(
  				buf + data_read,
  				(char *)data_transfer + data_offset + offset,
  				to_copy);
  
  			/* move on to the next buffer? */
  			if (to_copy == data_length - offset) {
  				queue_length--;
  				/*
  				 * No need to lock if we are not at the
  				 * end of the queue
  				 */
f9de151bf   Steve French   address lock imba...
1867
1868
1869
  				if (queue_length)
  					list_del(&response->list);
  				else {
e36c048a9   Arnd Bergmann   CIFS: SMBD: work ...
1870
1871
  					spin_lock_irq(
  						&info->reassembly_queue_lock);
f9de151bf   Steve French   address lock imba...
1872
  					list_del(&response->list);
e36c048a9   Arnd Bergmann   CIFS: SMBD: work ...
1873
1874
  					spin_unlock_irq(
  						&info->reassembly_queue_lock);
f9de151bf   Steve French   address lock imba...
1875
1876
  				}
  				queue_removed++;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
  				info->count_reassembly_queue--;
  				info->count_dequeue_reassembly_queue++;
  				put_receive_buffer(info, response);
  				offset = 0;
  				log_read(INFO, "put_receive_buffer offset=0
  ");
  			} else
  				offset += to_copy;
  
  			to_read -= to_copy;
  			data_read += to_copy;
a0a3036b8   Joe Perches   cifs: Standardize...
1888
1889
1890
1891
  			log_read(INFO, "_get_first_reassembly memcpy %d bytes data_transfer_length-offset=%d after that to_read=%d data_read=%d offset=%d
  ",
  				 to_copy, data_length - offset,
  				 to_read, data_read, offset);
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1892
  		}
e36c048a9   Arnd Bergmann   CIFS: SMBD: work ...
1893
  		spin_lock_irq(&info->reassembly_queue_lock);
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1894
1895
  		info->reassembly_data_length -= data_read;
  		info->reassembly_queue_length -= queue_removed;
e36c048a9   Arnd Bergmann   CIFS: SMBD: work ...
1896
  		spin_unlock_irq(&info->reassembly_queue_lock);
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1897
1898
  
  		info->first_entry_offset = offset;
a0a3036b8   Joe Perches   cifs: Standardize...
1899
1900
1901
1902
  		log_read(INFO, "returning to thread data_read=%d reassembly_data_length=%d first_entry_offset=%d
  ",
  			 data_read, info->reassembly_data_length,
  			 info->first_entry_offset);
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
  read_rfc1002_done:
  		return data_read;
  	}
  
  	log_read(INFO, "wait_event on more data
  ");
  	rc = wait_event_interruptible(
  		info->wait_reassembly_queue,
  		info->reassembly_data_length >= size ||
  			info->transport_status != SMBD_CONNECTED);
  	/* Don't return any data if interrupted */
  	if (rc)
98e0d4088   Long Li   cifs: smbd: Retur...
1915
  		return rc;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1916

e8b3bfe9b   Long Li   cifs: smbd: Don't...
1917
1918
1919
  	if (info->transport_status != SMBD_CONNECTED) {
  		log_read(ERR, "disconnected
  ");
acd4680e2   Long Li   cifs: smbd: Retur...
1920
  		return -ECONNABORTED;
e8b3bfe9b   Long Li   cifs: smbd: Don't...
1921
  	}
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1922
1923
1924
1925
1926
1927
1928
1929
1930
  	goto again;
  }
  
  /*
   * Receive a page from receive reassembly queue
   * page: the page to read data into
   * to_read: the length of data to read
   * return value: actual data read
   */
2026b06e9   Steve French   Cleanup some mino...
1931
  static int smbd_recv_page(struct smbd_connection *info,
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1932
1933
  		struct page *page, unsigned int page_offset,
  		unsigned int to_read)
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1934
1935
1936
  {
  	int ret;
  	char *to_address;
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1937
  	void *page_address;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1938
1939
1940
1941
1942
1943
1944
  
  	/* make sure we have the page ready for read */
  	ret = wait_event_interruptible(
  		info->wait_reassembly_queue,
  		info->reassembly_data_length >= to_read ||
  			info->transport_status != SMBD_CONNECTED);
  	if (ret)
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1945
  		return ret;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1946
1947
  
  	/* now we can read from reassembly queue and not sleep */
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1948
1949
  	page_address = kmap_atomic(page);
  	to_address = (char *) page_address + page_offset;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1950
1951
1952
1953
1954
1955
  
  	log_read(INFO, "reading from page=%p address=%p to_read=%d
  ",
  		page, to_address, to_read);
  
  	ret = smbd_recv_buf(info, to_address, to_read);
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1956
  	kunmap_atomic(page_address);
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
  
  	return ret;
  }
  
  /*
   * Receive data from transport
   * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
   * return: total bytes read, or 0. SMB Direct will not do partial read.
   */
  int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
  {
  	char *buf;
  	struct page *page;
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1970
  	unsigned int to_read, page_offset;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1971
  	int rc;
00e237074   David Howells   iov_iter: Use acc...
1972
1973
  	if (iov_iter_rw(&msg->msg_iter) == WRITE) {
  		/* It's a bug in upper layer to get there */
a0a3036b8   Joe Perches   cifs: Standardize...
1974
1975
  		cifs_dbg(VFS, "Invalid msg iter dir %u
  ",
00e237074   David Howells   iov_iter: Use acc...
1976
1977
1978
1979
1980
1981
1982
  			 iov_iter_rw(&msg->msg_iter));
  		rc = -EINVAL;
  		goto out;
  	}
  
  	switch (iov_iter_type(&msg->msg_iter)) {
  	case ITER_KVEC:
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1983
1984
1985
1986
  		buf = msg->msg_iter.kvec->iov_base;
  		to_read = msg->msg_iter.kvec->iov_len;
  		rc = smbd_recv_buf(info, buf, to_read);
  		break;
00e237074   David Howells   iov_iter: Use acc...
1987
  	case ITER_BVEC:
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1988
  		page = msg->msg_iter.bvec->bv_page;
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1989
  		page_offset = msg->msg_iter.bvec->bv_offset;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1990
  		to_read = msg->msg_iter.bvec->bv_len;
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1991
  		rc = smbd_recv_page(info, page, page_offset, to_read);
f64b78fd1   Long Li   CIFS: SMBD: Imple...
1992
1993
1994
1995
  		break;
  
  	default:
  		/* It's a bug in upper layer to get there */
a0a3036b8   Joe Perches   cifs: Standardize...
1996
1997
  		cifs_dbg(VFS, "Invalid msg type %d
  ",
00e237074   David Howells   iov_iter: Use acc...
1998
  			 iov_iter_type(&msg->msg_iter));
6509f50cd   Long Li   CIFS: SMBD: Suppo...
1999
  		rc = -EINVAL;
f64b78fd1   Long Li   CIFS: SMBD: Imple...
2000
  	}
00e237074   David Howells   iov_iter: Use acc...
2001
  out:
f64b78fd1   Long Li   CIFS: SMBD: Imple...
2002
2003
2004
2005
2006
  	/* SMBDirect will read it all or nothing */
  	if (rc > 0)
  		msg->msg_iter.count = 0;
  	return rc;
  }
d649e1bba   Long Li   CIFS: SMBD: Imple...
2007
2008
2009
2010
2011
2012
2013
  
  /*
   * Send data to transport
   * Each rqst is transported as a SMBDirect payload
   * rqst: the data to write
   * return value: 0 if successfully write, otherwise error code
   */
4739f2328   Long Li   cifs: smbd: take ...
2014
2015
  int smbd_send(struct TCP_Server_Info *server,
  	int num_rqst, struct smb_rqst *rqst_array)
d649e1bba   Long Li   CIFS: SMBD: Imple...
2016
  {
81f39f951   Ronnie Sahlberg   cifs: fix SMB1 br...
2017
  	struct smbd_connection *info = server->smbd_conn;
d649e1bba   Long Li   CIFS: SMBD: Imple...
2018
2019
2020
  	struct kvec vec;
  	int nvecs;
  	int size;
35e2cc1ba   Paulo Alcantara   cifs: Use correct...
2021
  	unsigned int buflen, remaining_data_length;
d649e1bba   Long Li   CIFS: SMBD: Imple...
2022
2023
2024
  	int start, i, j;
  	int max_iov_size =
  		info->max_send_size - sizeof(struct smbd_data_transfer);
8bcda1d2a   Long Li   cifs: smbd: Avoid...
2025
  	struct kvec *iov;
d649e1bba   Long Li   CIFS: SMBD: Imple...
2026
  	int rc;
4739f2328   Long Li   cifs: smbd: take ...
2027
2028
  	struct smb_rqst *rqst;
  	int rqst_idx;
d649e1bba   Long Li   CIFS: SMBD: Imple...
2029

d649e1bba   Long Li   CIFS: SMBD: Imple...
2030
  	if (info->transport_status != SMBD_CONNECTED) {
62fdf6707   Long Li   cifs: smbd: Indic...
2031
  		rc = -EAGAIN;
d649e1bba   Long Li   CIFS: SMBD: Imple...
2032
2033
2034
2035
  		goto done;
  	}
  
  	/*
b6903bcf0   Long Li   CIFS: SMBD: Suppo...
2036
2037
2038
2039
  	 * Add in the page array if there is one. The caller needs to set
  	 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
  	 * ends at page boundary
  	 */
4739f2328   Long Li   cifs: smbd: take ...
2040
2041
2042
  	remaining_data_length = 0;
  	for (i = 0; i < num_rqst; i++)
  		remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2043

f7950cb05   Long Li   cifs: smbd: Calcu...
2044
  	if (remaining_data_length > info->max_fragmented_send_size) {
d649e1bba   Long Li   CIFS: SMBD: Imple...
2045
2046
  		log_write(ERR, "payload size %d > max size %d
  ",
4739f2328   Long Li   cifs: smbd: take ...
2047
  			remaining_data_length, info->max_fragmented_send_size);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2048
2049
2050
  		rc = -EINVAL;
  		goto done;
  	}
7f46d23e1   Long Li   cifs:smbd Use the...
2051
2052
2053
  	log_write(INFO, "num_rqst=%d total length=%u
  ",
  			num_rqst, remaining_data_length);
4739f2328   Long Li   cifs: smbd: take ...
2054

7f46d23e1   Long Li   cifs:smbd Use the...
2055
  	rqst_idx = 0;
4739f2328   Long Li   cifs: smbd: take ...
2056
2057
2058
  next_rqst:
  	rqst = &rqst_array[rqst_idx];
  	iov = rqst->rq_iov;
35e2cc1ba   Paulo Alcantara   cifs: Use correct...
2059

4739f2328   Long Li   cifs: smbd: take ...
2060
2061
2062
2063
  	cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu
  ",
  		rqst_idx, smb_rqst_len(server, rqst));
  	for (i = 0; i < rqst->rq_nvec; i++)
ff30b89e0   Long Li   cifs: smbd: Dump ...
2064
  		dump_smb(iov[i].iov_base, iov[i].iov_len);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2065

a0a3036b8   Joe Perches   cifs: Standardize...
2066
2067
2068
2069
  	log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d rq_tailsz=%d buflen=%lu
  ",
  		  rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
  		  rqst->rq_tailsz, smb_rqst_len(server, rqst));
d649e1bba   Long Li   CIFS: SMBD: Imple...
2070

4739f2328   Long Li   cifs: smbd: take ...
2071
  	start = i = 0;
d649e1bba   Long Li   CIFS: SMBD: Imple...
2072
2073
2074
2075
2076
2077
2078
  	buflen = 0;
  	while (true) {
  		buflen += iov[i].iov_len;
  		if (buflen > max_iov_size) {
  			if (i > start) {
  				remaining_data_length -=
  					(buflen-iov[i].iov_len);
a0a3036b8   Joe Perches   cifs: Standardize...
2079
2080
2081
2082
  				log_write(INFO, "sending iov[] from start=%d i=%d nvecs=%d remaining_data_length=%d
  ",
  					  start, i, i - start,
  					  remaining_data_length);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2083
2084
2085
2086
2087
2088
2089
2090
  				rc = smbd_post_send_data(
  					info, &iov[start], i-start,
  					remaining_data_length);
  				if (rc)
  					goto done;
  			} else {
  				/* iov[start] is too big, break it */
  				nvecs = (buflen+max_iov_size-1)/max_iov_size;
a0a3036b8   Joe Perches   cifs: Standardize...
2091
2092
2093
2094
  				log_write(INFO, "iov[%d] iov_base=%p buflen=%d break to %d vectors
  ",
  					  start, iov[start].iov_base,
  					  buflen, nvecs);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
  				for (j = 0; j < nvecs; j++) {
  					vec.iov_base =
  						(char *)iov[start].iov_base +
  						j*max_iov_size;
  					vec.iov_len = max_iov_size;
  					if (j == nvecs-1)
  						vec.iov_len =
  							buflen -
  							max_iov_size*(nvecs-1);
  					remaining_data_length -= vec.iov_len;
  					log_write(INFO,
a0a3036b8   Joe Perches   cifs: Standardize...
2106
2107
2108
2109
  						"sending vec j=%d iov_base=%p iov_len=%zu remaining_data_length=%d
  ",
  						  j, vec.iov_base, vec.iov_len,
  						  remaining_data_length);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2110
2111
2112
2113
2114
2115
2116
  					rc = smbd_post_send_data(
  						info, &vec, 1,
  						remaining_data_length);
  					if (rc)
  						goto done;
  				}
  				i++;
4739f2328   Long Li   cifs: smbd: take ...
2117
  				if (i == rqst->rq_nvec)
ab60ee7bf   Long Li   cifs: smbd: Check...
2118
  					break;
d649e1bba   Long Li   CIFS: SMBD: Imple...
2119
2120
2121
2122
2123
  			}
  			start = i;
  			buflen = 0;
  		} else {
  			i++;
4739f2328   Long Li   cifs: smbd: take ...
2124
  			if (i == rqst->rq_nvec) {
d649e1bba   Long Li   CIFS: SMBD: Imple...
2125
2126
  				/* send out all remaining vecs */
  				remaining_data_length -= buflen;
a0a3036b8   Joe Perches   cifs: Standardize...
2127
2128
2129
2130
  				log_write(INFO, "sending iov[] from start=%d i=%d nvecs=%d remaining_data_length=%d
  ",
  					  start, i, i - start,
  					  remaining_data_length);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
  				rc = smbd_post_send_data(info, &iov[start],
  					i-start, remaining_data_length);
  				if (rc)
  					goto done;
  				break;
  			}
  		}
  		log_write(INFO, "looping i=%d buflen=%d
  ", i, buflen);
  	}
  
  	/* now sending pages if there are any */
  	for (i = 0; i < rqst->rq_npages; i++) {
b6903bcf0   Long Li   CIFS: SMBD: Suppo...
2144
2145
2146
  		unsigned int offset;
  
  		rqst_page_get_length(rqst, i, &buflen, &offset);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2147
2148
2149
2150
2151
2152
2153
2154
2155
  		nvecs = (buflen + max_iov_size - 1) / max_iov_size;
  		log_write(INFO, "sending pages buflen=%d nvecs=%d
  ",
  			buflen, nvecs);
  		for (j = 0; j < nvecs; j++) {
  			size = max_iov_size;
  			if (j == nvecs-1)
  				size = buflen - j*max_iov_size;
  			remaining_data_length -= size;
a0a3036b8   Joe Perches   cifs: Standardize...
2156
2157
2158
2159
  			log_write(INFO, "sending pages i=%d offset=%d size=%d remaining_data_length=%d
  ",
  				  i, j * max_iov_size + offset, size,
  				  remaining_data_length);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2160
  			rc = smbd_post_send_page(
b6903bcf0   Long Li   CIFS: SMBD: Suppo...
2161
2162
  				info, rqst->rq_pages[i],
  				j*max_iov_size + offset,
d649e1bba   Long Li   CIFS: SMBD: Imple...
2163
2164
2165
2166
2167
  				size, remaining_data_length);
  			if (rc)
  				goto done;
  		}
  	}
4739f2328   Long Li   cifs: smbd: take ...
2168
2169
2170
  	rqst_idx++;
  	if (rqst_idx < num_rqst)
  		goto next_rqst;
d649e1bba   Long Li   CIFS: SMBD: Imple...
2171
2172
2173
2174
2175
2176
2177
  done:
  	/*
  	 * As an optimization, we don't wait for individual I/O to finish
  	 * before sending the next one.
  	 * Send them all and wait for pending send count to get to 0
  	 * that means all the I/Os have been out and we are good to return
  	 */
072a14ec6   Long Li   cifs: smbd: Merge...
2178
2179
  	wait_event(info->wait_send_pending,
  		atomic_read(&info->send_pending) == 0);
d649e1bba   Long Li   CIFS: SMBD: Imple...
2180

d649e1bba   Long Li   CIFS: SMBD: Imple...
2181
2182
  	return rc;
  }
c73985833   Long Li   CIFS: SMBD: Imple...
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
  
  static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
  {
  	struct smbd_mr *mr;
  	struct ib_cqe *cqe;
  
  	if (wc->status) {
  		log_rdma_mr(ERR, "status=%d
  ", wc->status);
  		cqe = wc->wr_cqe;
  		mr = container_of(cqe, struct smbd_mr, cqe);
  		smbd_disconnect_rdma_connection(mr->conn);
  	}
  }
  
  /*
   * The work queue function that recovers MRs
   * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
   * again. Both calls are slow, so finish them in a workqueue. This will not
   * block I/O path.
   * There is one workqueue that recovers MRs, there is no need to lock as the
   * I/O requests calling smbd_register_mr will never update the links in the
   * mr_list.
   */
  static void smbd_mr_recovery_work(struct work_struct *work)
  {
  	struct smbd_connection *info =
  		container_of(work, struct smbd_connection, mr_recovery_work);
  	struct smbd_mr *smbdirect_mr;
  	int rc;
  
  	list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
c21ce58ea   Long Li   cifs: smbd: Only ...
2215
  		if (smbdirect_mr->state == MR_ERROR) {
c73985833   Long Li   CIFS: SMBD: Imple...
2216

7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
  			/* recover this MR entry */
  			rc = ib_dereg_mr(smbdirect_mr->mr);
  			if (rc) {
  				log_rdma_mr(ERR,
  					"ib_dereg_mr failed rc=%x
  ",
  					rc);
  				smbd_disconnect_rdma_connection(info);
  				continue;
  			}
  
  			smbdirect_mr->mr = ib_alloc_mr(
  				info->pd, info->mr_type,
  				info->max_frmr_depth);
  			if (IS_ERR(smbdirect_mr->mr)) {
a0a3036b8   Joe Perches   cifs: Standardize...
2232
2233
2234
2235
  				log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x max_frmr_depth=%x
  ",
  					    info->mr_type,
  					    info->max_frmr_depth);
7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2236
2237
2238
  				smbd_disconnect_rdma_connection(info);
  				continue;
  			}
ff526d860   Long Li   CIFS: SMBD: Do no...
2239
2240
2241
  		} else
  			/* This MR is being used, don't recover it */
  			continue;
7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2242

ff526d860   Long Li   CIFS: SMBD: Do no...
2243
  		smbdirect_mr->state = MR_READY;
c73985833   Long Li   CIFS: SMBD: Imple...
2244

ff526d860   Long Li   CIFS: SMBD: Do no...
2245
2246
2247
2248
2249
2250
2251
2252
2253
  		/* smbdirect_mr->state is updated by this function
  		 * and is read and updated by I/O issuing CPUs trying
  		 * to get a MR, the call to atomic_inc_return
  		 * implicates a memory barrier and guarantees this
  		 * value is updated before waking up any calls to
  		 * get_mr() from the I/O issuing CPUs
  		 */
  		if (atomic_inc_return(&info->mr_ready_count) == 1)
  			wake_up_interruptible(&info->wait_mr);
c73985833   Long Li   CIFS: SMBD: Imple...
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
  	}
  }
  
  static void destroy_mr_list(struct smbd_connection *info)
  {
  	struct smbd_mr *mr, *tmp;
  
  	cancel_work_sync(&info->mr_recovery_work);
  	list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
  		if (mr->state == MR_INVALIDATED)
  			ib_dma_unmap_sg(info->id->device, mr->sgl,
  				mr->sgl_count, mr->dir);
  		ib_dereg_mr(mr->mr);
  		kfree(mr->sgl);
  		kfree(mr);
  	}
  }
  
  /*
   * Allocate MRs used for RDMA read/write
   * The number of MRs will not exceed hardware capability in responder_resources
   * All MRs are kept in mr_list. The MR can be recovered after it's used
   * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
   * as MRs are used and recovered for I/O, but the list links will not change
   */
  static int allocate_mr_list(struct smbd_connection *info)
  {
  	int i;
  	struct smbd_mr *smbdirect_mr, *tmp;
  
  	INIT_LIST_HEAD(&info->mr_list);
  	init_waitqueue_head(&info->wait_mr);
  	spin_lock_init(&info->mr_list_lock);
  	atomic_set(&info->mr_ready_count, 0);
  	atomic_set(&info->mr_used_count, 0);
  	init_waitqueue_head(&info->wait_for_mr_cleanup);
  	/* Allocate more MRs (2x) than hardware responder_resources */
  	for (i = 0; i < info->responder_resources * 2; i++) {
  		smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
  		if (!smbdirect_mr)
  			goto out;
  		smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
  					info->max_frmr_depth);
  		if (IS_ERR(smbdirect_mr->mr)) {
a0a3036b8   Joe Perches   cifs: Standardize...
2298
2299
2300
  			log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x max_frmr_depth=%x
  ",
  				    info->mr_type, info->max_frmr_depth);
c73985833   Long Li   CIFS: SMBD: Imple...
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
  			goto out;
  		}
  		smbdirect_mr->sgl = kcalloc(
  					info->max_frmr_depth,
  					sizeof(struct scatterlist),
  					GFP_KERNEL);
  		if (!smbdirect_mr->sgl) {
  			log_rdma_mr(ERR, "failed to allocate sgl
  ");
  			ib_dereg_mr(smbdirect_mr->mr);
  			goto out;
  		}
  		smbdirect_mr->state = MR_READY;
  		smbdirect_mr->conn = info;
  
  		list_add_tail(&smbdirect_mr->list, &info->mr_list);
  		atomic_inc(&info->mr_ready_count);
  	}
  	INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
  	return 0;
  
  out:
  	kfree(smbdirect_mr);
  
  	list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
  		ib_dereg_mr(smbdirect_mr->mr);
  		kfree(smbdirect_mr->sgl);
  		kfree(smbdirect_mr);
  	}
  	return -ENOMEM;
  }
  
  /*
   * Get a MR from mr_list. This function waits until there is at least one
   * MR available in the list. It may access the list while the
   * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
   * as they never modify the same places. However, there may be several CPUs
   * issueing I/O trying to get MR at the same time, mr_list_lock is used to
   * protect this situation.
   */
  static struct smbd_mr *get_mr(struct smbd_connection *info)
  {
  	struct smbd_mr *ret;
  	int rc;
  again:
  	rc = wait_event_interruptible(info->wait_mr,
  		atomic_read(&info->mr_ready_count) ||
  		info->transport_status != SMBD_CONNECTED);
  	if (rc) {
  		log_rdma_mr(ERR, "wait_event_interruptible rc=%x
  ", rc);
  		return NULL;
  	}
  
  	if (info->transport_status != SMBD_CONNECTED) {
  		log_rdma_mr(ERR, "info->transport_status=%x
  ",
  			info->transport_status);
  		return NULL;
  	}
  
  	spin_lock(&info->mr_list_lock);
  	list_for_each_entry(ret, &info->mr_list, list) {
  		if (ret->state == MR_READY) {
  			ret->state = MR_REGISTERED;
  			spin_unlock(&info->mr_list_lock);
  			atomic_dec(&info->mr_ready_count);
  			atomic_inc(&info->mr_used_count);
  			return ret;
  		}
  	}
  
  	spin_unlock(&info->mr_list_lock);
  	/*
  	 * It is possible that we could fail to get MR because other processes may
  	 * try to acquire a MR at the same time. If this is the case, retry it.
  	 */
  	goto again;
  }
  
  /*
   * Register memory for RDMA read/write
   * pages[]: the list of pages to register memory with
   * num_pages: the number of pages to register
   * tailsz: if non-zero, the bytes to register in the last page
   * writing: true if this is a RDMA write (SMB read), false for RDMA read
   * need_invalidate: true if this MR needs to be locally invalidated after I/O
   * return value: the MR registered, NULL if failed.
   */
  struct smbd_mr *smbd_register_mr(
  	struct smbd_connection *info, struct page *pages[], int num_pages,
7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2392
  	int offset, int tailsz, bool writing, bool need_invalidate)
c73985833   Long Li   CIFS: SMBD: Imple...
2393
2394
2395
2396
2397
  {
  	struct smbd_mr *smbdirect_mr;
  	int rc, i;
  	enum dma_data_direction dir;
  	struct ib_reg_wr *reg_wr;
c73985833   Long Li   CIFS: SMBD: Imple...
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
  
  	if (num_pages > info->max_frmr_depth) {
  		log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d
  ",
  			num_pages, info->max_frmr_depth);
  		return NULL;
  	}
  
  	smbdirect_mr = get_mr(info);
  	if (!smbdirect_mr) {
  		log_rdma_mr(ERR, "get_mr returning NULL
  ");
  		return NULL;
  	}
  	smbdirect_mr->need_invalidate = need_invalidate;
  	smbdirect_mr->sgl_count = num_pages;
  	sg_init_table(smbdirect_mr->sgl, num_pages);
7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2415
2416
2417
  	log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x
  ",
  			num_pages, offset, tailsz);
c73985833   Long Li   CIFS: SMBD: Imple...
2418

7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
  	if (num_pages == 1) {
  		sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
  		goto skip_multiple_pages;
  	}
  
  	/* We have at least two pages to register */
  	sg_set_page(
  		&smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
  	i = 1;
  	while (i < num_pages - 1) {
  		sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
  		i++;
  	}
c73985833   Long Li   CIFS: SMBD: Imple...
2432
2433
  	sg_set_page(&smbdirect_mr->sgl[i], pages[i],
  		tailsz ? tailsz : PAGE_SIZE, 0);
7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2434
  skip_multiple_pages:
c73985833   Long Li   CIFS: SMBD: Imple...
2435
2436
2437
2438
  	dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  	smbdirect_mr->dir = dir;
  	rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
  	if (!rc) {
7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2439
2440
  		log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x
  ",
c73985833   Long Li   CIFS: SMBD: Imple...
2441
2442
2443
2444
2445
2446
2447
  			num_pages, dir, rc);
  		goto dma_map_error;
  	}
  
  	rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
  		NULL, PAGE_SIZE);
  	if (rc != num_pages) {
7cf20bce7   Long Li   CIFS: SMBD: Suppo...
2448
2449
2450
  		log_rdma_mr(ERR,
  			"ib_map_mr_sg failed rc = %d num_pages = %x
  ",
c73985833   Long Li   CIFS: SMBD: Imple...
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
  			rc, num_pages);
  		goto map_mr_error;
  	}
  
  	ib_update_fast_reg_key(smbdirect_mr->mr,
  		ib_inc_rkey(smbdirect_mr->mr->rkey));
  	reg_wr = &smbdirect_mr->wr;
  	reg_wr->wr.opcode = IB_WR_REG_MR;
  	smbdirect_mr->cqe.done = register_mr_done;
  	reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
  	reg_wr->wr.num_sge = 0;
  	reg_wr->wr.send_flags = IB_SEND_SIGNALED;
  	reg_wr->mr = smbdirect_mr->mr;
  	reg_wr->key = smbdirect_mr->mr->rkey;
  	reg_wr->access = writing ?
  			IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
  			IB_ACCESS_REMOTE_READ;
  
  	/*
  	 * There is no need for waiting for complemtion on ib_post_send
  	 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
  	 * on the next ib_post_send when we actaully send I/O to remote peer
  	 */
739305950   Bart Van Assche   fs/cifs: Simplify...
2474
  	rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
c73985833   Long Li   CIFS: SMBD: Imple...
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
  	if (!rc)
  		return smbdirect_mr;
  
  	log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x
  ",
  		rc, reg_wr->key);
  
  	/* If all failed, attempt to recover this MR by setting it MR_ERROR*/
  map_mr_error:
  	ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
  		smbdirect_mr->sgl_count, smbdirect_mr->dir);
  
  dma_map_error:
  	smbdirect_mr->state = MR_ERROR;
  	if (atomic_dec_and_test(&info->mr_used_count))
  		wake_up(&info->wait_for_mr_cleanup);
21a4e14aa   Long Li   cifs: smbd: disco...
2491
  	smbd_disconnect_rdma_connection(info);
c73985833   Long Li   CIFS: SMBD: Imple...
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
  	return NULL;
  }
  
  static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
  {
  	struct smbd_mr *smbdirect_mr;
  	struct ib_cqe *cqe;
  
  	cqe = wc->wr_cqe;
  	smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
  	smbdirect_mr->state = MR_INVALIDATED;
  	if (wc->status != IB_WC_SUCCESS) {
  		log_rdma_mr(ERR, "invalidate failed status=%x
  ", wc->status);
  		smbdirect_mr->state = MR_ERROR;
  	}
  	complete(&smbdirect_mr->invalidate_done);
  }
  
  /*
   * Deregister a MR after I/O is done
   * This function may wait if remote invalidation is not used
   * and we have to locally invalidate the buffer to prevent data is being
   * modified by remote peer after upper layer consumes it
   */
  int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
  {
739305950   Bart Van Assche   fs/cifs: Simplify...
2519
  	struct ib_send_wr *wr;
c73985833   Long Li   CIFS: SMBD: Imple...
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
  	struct smbd_connection *info = smbdirect_mr->conn;
  	int rc = 0;
  
  	if (smbdirect_mr->need_invalidate) {
  		/* Need to finish local invalidation before returning */
  		wr = &smbdirect_mr->inv_wr;
  		wr->opcode = IB_WR_LOCAL_INV;
  		smbdirect_mr->cqe.done = local_inv_done;
  		wr->wr_cqe = &smbdirect_mr->cqe;
  		wr->num_sge = 0;
  		wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
  		wr->send_flags = IB_SEND_SIGNALED;
  
  		init_completion(&smbdirect_mr->invalidate_done);
739305950   Bart Van Assche   fs/cifs: Simplify...
2534
  		rc = ib_post_send(info->id->qp, wr, NULL);
c73985833   Long Li   CIFS: SMBD: Imple...
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
  		if (rc) {
  			log_rdma_mr(ERR, "ib_post_send failed rc=%x
  ", rc);
  			smbd_disconnect_rdma_connection(info);
  			goto done;
  		}
  		wait_for_completion(&smbdirect_mr->invalidate_done);
  		smbdirect_mr->need_invalidate = false;
  	} else
  		/*
  		 * For remote invalidation, just set it to MR_INVALIDATED
  		 * and defer to mr_recovery_work to recover the MR for next use
  		 */
  		smbdirect_mr->state = MR_INVALIDATED;
c21ce58ea   Long Li   cifs: smbd: Only ...
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
  	if (smbdirect_mr->state == MR_INVALIDATED) {
  		ib_dma_unmap_sg(
  			info->id->device, smbdirect_mr->sgl,
  			smbdirect_mr->sgl_count,
  			smbdirect_mr->dir);
  		smbdirect_mr->state = MR_READY;
  		if (atomic_inc_return(&info->mr_ready_count) == 1)
  			wake_up_interruptible(&info->wait_mr);
  	} else
  		/*
  		 * Schedule the work to do MR recovery for future I/Os MR
  		 * recovery is slow and don't want it to block current I/O
  		 */
  		queue_work(info->workqueue, &info->mr_recovery_work);
c73985833   Long Li   CIFS: SMBD: Imple...
2563
2564
2565
2566
2567
2568
2569
  
  done:
  	if (atomic_dec_and_test(&info->mr_used_count))
  		wake_up(&info->wait_for_mr_cleanup);
  
  	return rc;
  }