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