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