Linux 5.7.6
[linux/fpc-iii.git] / fs / cifs / smbdirect.c
blob1a5834a5d597dab650d448835cf1ffb06fe4e9fa
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2017, Microsoft Corporation.
5 * Author(s): Long Li <longli@microsoft.com>
6 */
7 #include <linux/module.h>
8 #include <linux/highmem.h>
9 #include "smbdirect.h"
10 #include "cifs_debug.h"
11 #include "cifsproto.h"
12 #include "smb2proto.h"
14 static struct smbd_response *get_empty_queue_buffer(
15 struct smbd_connection *info);
16 static struct smbd_response *get_receive_buffer(
17 struct smbd_connection *info);
18 static void put_receive_buffer(
19 struct smbd_connection *info,
20 struct smbd_response *response);
21 static int allocate_receive_buffers(struct smbd_connection *info, int num_buf);
22 static void destroy_receive_buffers(struct smbd_connection *info);
24 static void put_empty_packet(
25 struct smbd_connection *info, struct smbd_response *response);
26 static void enqueue_reassembly(
27 struct smbd_connection *info,
28 struct smbd_response *response, int data_length);
29 static struct smbd_response *_get_first_reassembly(
30 struct smbd_connection *info);
32 static int smbd_post_recv(
33 struct smbd_connection *info,
34 struct smbd_response *response);
36 static int smbd_post_send_empty(struct smbd_connection *info);
37 static int smbd_post_send_data(
38 struct smbd_connection *info,
39 struct kvec *iov, int n_vec, int remaining_data_length);
40 static int smbd_post_send_page(struct smbd_connection *info,
41 struct page *page, unsigned long offset,
42 size_t size, int remaining_data_length);
44 static void destroy_mr_list(struct smbd_connection *info);
45 static int allocate_mr_list(struct smbd_connection *info);
47 /* SMBD version number */
48 #define SMBD_V1 0x0100
50 /* Port numbers for SMBD transport */
51 #define SMB_PORT 445
52 #define SMBD_PORT 5445
54 /* Address lookup and resolve timeout in ms */
55 #define RDMA_RESOLVE_TIMEOUT 5000
57 /* SMBD negotiation timeout in seconds */
58 #define SMBD_NEGOTIATE_TIMEOUT 120
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
65 * Default maximum number of RDMA read/write outstanding on this connection
66 * This value is possibly decreased during QP creation on hardware limit
68 #define SMBD_CM_RESPONDER_RESOURCES 32
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
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
80 /* The local peer's maximum number of credits to grant to the peer */
81 int smbd_receive_credit_max = 255;
83 /* The remote peer's credit request of local peer */
84 int smbd_send_credit_target = 255;
86 /* The maximum single message size can be sent to remote peer */
87 int smbd_max_send_size = 1364;
89 /* The maximum fragmented upper-layer payload receive size supported */
90 int smbd_max_fragmented_recv_size = 1024 * 1024;
92 /* The maximum single-message size which can be received */
93 int smbd_max_receive_size = 8192;
95 /* The timeout to initiate send of a keepalive message on idle */
96 int smbd_keep_alive_interval = 120;
99 * User configurable initial values for RDMA transport
100 * The actual values used may be lower and are limited to hardware capabilities
102 /* Default maximum number of SGEs in a RDMA write/read */
103 int smbd_max_frmr_depth = 2048;
105 /* If payload is less than this byte, use RDMA send/recv not read/write */
106 int rdma_readwrite_threshold = 4096;
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()
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
123 static unsigned int smbd_logging_class;
124 module_param(smbd_logging_class, uint, 0644);
125 MODULE_PARM_DESC(smbd_logging_class,
126 "Logging class for SMBD transport 0x0 to 0x100");
128 #define ERR 0x0
129 #define INFO 0x1
130 static unsigned int smbd_logging_level = ERR;
131 module_param(smbd_logging_level, uint, 0644);
132 MODULE_PARM_DESC(smbd_logging_level,
133 "Logging level for SMBD transport, 0 (default): error, 1: info");
135 #define log_rdma(level, class, fmt, args...) \
136 do { \
137 if (level <= smbd_logging_level || class & smbd_logging_class) \
138 cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
139 } while (0)
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)
158 static void smbd_disconnect_rdma_work(struct work_struct *work)
160 struct smbd_connection *info =
161 container_of(work, struct smbd_connection, disconnect_work);
163 if (info->transport_status == SMBD_CONNECTED) {
164 info->transport_status = SMBD_DISCONNECTING;
165 rdma_disconnect(info->id);
169 static void smbd_disconnect_rdma_connection(struct smbd_connection *info)
171 queue_work(info->workqueue, &info->disconnect_work);
174 /* Upcall from RDMA CM */
175 static int smbd_conn_upcall(
176 struct rdma_cm_id *id, struct rdma_cm_event *event)
178 struct smbd_connection *info = id->context;
180 log_rdma_event(INFO, "event=%d status=%d\n",
181 event->event, event->status);
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;
190 case RDMA_CM_EVENT_ADDR_ERROR:
191 info->ri_rc = -EHOSTUNREACH;
192 complete(&info->ri_done);
193 break;
195 case RDMA_CM_EVENT_ROUTE_ERROR:
196 info->ri_rc = -ENETUNREACH;
197 complete(&info->ri_done);
198 break;
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;
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;
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;
223 info->transport_status = SMBD_DISCONNECTED;
224 wake_up_interruptible(&info->disconn_wait);
225 wake_up_interruptible(&info->wait_reassembly_queue);
226 wake_up_interruptible_all(&info->wait_send_queue);
227 break;
229 default:
230 break;
233 return 0;
236 /* Upcall from RDMA QP */
237 static void
238 smbd_qp_async_error_upcall(struct ib_event *event, void *context)
240 struct smbd_connection *info = context;
242 log_rdma_event(ERR, "%s on device %s info %p\n",
243 ib_event_msg(event->event), event->device->name, info);
245 switch (event->event) {
246 case IB_EVENT_CQ_ERR:
247 case IB_EVENT_QP_FATAL:
248 smbd_disconnect_rdma_connection(info);
250 default:
251 break;
255 static inline void *smbd_request_payload(struct smbd_request *request)
257 return (void *)request->packet;
260 static inline void *smbd_response_payload(struct smbd_response *response)
262 return (void *)response->packet;
265 /* Called when a RDMA send is done */
266 static void send_done(struct ib_cq *cq, struct ib_wc *wc)
268 int i;
269 struct smbd_request *request =
270 container_of(wc->wr_cqe, struct smbd_request, cqe);
272 log_rdma_send(INFO, "smbd_request %p completed wc->status=%d\n",
273 request, wc->status);
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);
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);
287 if (atomic_dec_and_test(&request->info->send_pending))
288 wake_up(&request->info->wait_send_pending);
290 wake_up(&request->info->wait_post_send);
292 mempool_free(request, request->info->request_mempool);
295 static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
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);
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
313 static bool process_negotiation_response(
314 struct smbd_response *response, int packet_length)
316 struct smbd_connection *info = response->info;
317 struct smbd_negotiate_resp *packet = smbd_response_payload(response);
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;
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;
330 info->protocol = le16_to_cpu(packet->negotiated_version);
332 if (packet->credits_requested == 0) {
333 log_rdma_event(ERR, "error: credits_requested==0\n");
334 return false;
336 info->receive_credit_target = le16_to_cpu(packet->credits_requested);
338 if (packet->credits_granted == 0) {
339 log_rdma_event(ERR, "error: credits_granted==0\n");
340 return false;
342 atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
344 atomic_set(&info->receive_credits, 0);
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;
351 info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
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;
358 info->max_send_size = min_t(int, info->max_send_size,
359 le32_to_cpu(packet->max_receive_size));
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;
367 info->max_fragmented_send_size =
368 le32_to_cpu(packet->max_fragmented_size);
369 info->rdma_readwrite_threshold =
370 rdma_readwrite_threshold > info->max_fragmented_send_size ?
371 info->max_fragmented_send_size :
372 rdma_readwrite_threshold;
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;
380 return true;
383 static void smbd_post_send_credits(struct work_struct *work)
385 int ret = 0;
386 int use_receive_queue = 1;
387 int rc;
388 struct smbd_response *response;
389 struct smbd_connection *info =
390 container_of(work, struct smbd_connection,
391 post_send_credits_work);
393 if (info->transport_status != SMBD_CONNECTED) {
394 wake_up(&info->wait_receive_queues);
395 return;
398 if (info->receive_credit_target >
399 atomic_read(&info->receive_credits)) {
400 while (true) {
401 if (use_receive_queue)
402 response = get_receive_buffer(info);
403 else
404 response = get_empty_queue_buffer(info);
405 if (!response) {
406 /* now switch to emtpy packet queue */
407 if (use_receive_queue) {
408 use_receive_queue = 0;
409 continue;
410 } else
411 break;
414 response->type = SMBD_TRANSFER_DATA;
415 response->first_segment = false;
416 rc = smbd_post_recv(info, response);
417 if (rc) {
418 log_rdma_recv(ERR,
419 "post_recv failed rc=%d\n", rc);
420 put_receive_buffer(info, response);
421 break;
424 ret++;
428 spin_lock(&info->lock_new_credits_offered);
429 info->new_credits_offered += ret;
430 spin_unlock(&info->lock_new_credits_offered);
432 /* Promptly send an immediate packet as defined in [MS-SMBD] 3.1.1.1 */
433 info->send_immediate = true;
434 if (atomic_read(&info->receive_credits) <
435 info->receive_credit_target - 1) {
436 if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
437 info->send_immediate) {
438 log_keep_alive(INFO, "send an empty message\n");
439 smbd_post_send_empty(info);
444 /* Called from softirq, when recv is done */
445 static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
447 struct smbd_data_transfer *data_transfer;
448 struct smbd_response *response =
449 container_of(wc->wr_cqe, struct smbd_response, cqe);
450 struct smbd_connection *info = response->info;
451 int data_length = 0;
453 log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d "
454 "byte_len=%d pkey_index=%x\n",
455 response, response->type, wc->status, wc->opcode,
456 wc->byte_len, wc->pkey_index);
458 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
459 log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
460 wc->status, wc->opcode);
461 smbd_disconnect_rdma_connection(info);
462 goto error;
465 ib_dma_sync_single_for_cpu(
466 wc->qp->device,
467 response->sge.addr,
468 response->sge.length,
469 DMA_FROM_DEVICE);
471 switch (response->type) {
472 /* SMBD negotiation response */
473 case SMBD_NEGOTIATE_RESP:
474 dump_smbd_negotiate_resp(smbd_response_payload(response));
475 info->full_packet_received = true;
476 info->negotiate_done =
477 process_negotiation_response(response, wc->byte_len);
478 complete(&info->negotiate_completion);
479 break;
481 /* SMBD data transfer packet */
482 case SMBD_TRANSFER_DATA:
483 data_transfer = smbd_response_payload(response);
484 data_length = le32_to_cpu(data_transfer->data_length);
487 * If this is a packet with data playload place the data in
488 * reassembly queue and wake up the reading thread
490 if (data_length) {
491 if (info->full_packet_received)
492 response->first_segment = true;
494 if (le32_to_cpu(data_transfer->remaining_data_length))
495 info->full_packet_received = false;
496 else
497 info->full_packet_received = true;
499 enqueue_reassembly(
500 info,
501 response,
502 data_length);
503 } else
504 put_empty_packet(info, response);
506 if (data_length)
507 wake_up_interruptible(&info->wait_reassembly_queue);
509 atomic_dec(&info->receive_credits);
510 info->receive_credit_target =
511 le16_to_cpu(data_transfer->credits_requested);
512 if (le16_to_cpu(data_transfer->credits_granted)) {
513 atomic_add(le16_to_cpu(data_transfer->credits_granted),
514 &info->send_credits);
516 * We have new send credits granted from remote peer
517 * If any sender is waiting for credits, unblock it
519 wake_up_interruptible(&info->wait_send_queue);
522 log_incoming(INFO, "data flags %d data_offset %d "
523 "data_length %d remaining_data_length %d\n",
524 le16_to_cpu(data_transfer->flags),
525 le32_to_cpu(data_transfer->data_offset),
526 le32_to_cpu(data_transfer->data_length),
527 le32_to_cpu(data_transfer->remaining_data_length));
529 /* Send a KEEP_ALIVE response right away if requested */
530 info->keep_alive_requested = KEEP_ALIVE_NONE;
531 if (le16_to_cpu(data_transfer->flags) &
532 SMB_DIRECT_RESPONSE_REQUESTED) {
533 info->keep_alive_requested = KEEP_ALIVE_PENDING;
536 return;
538 default:
539 log_rdma_recv(ERR,
540 "unexpected response type=%d\n", response->type);
543 error:
544 put_receive_buffer(info, response);
547 static struct rdma_cm_id *smbd_create_id(
548 struct smbd_connection *info,
549 struct sockaddr *dstaddr, int port)
551 struct rdma_cm_id *id;
552 int rc;
553 __be16 *sport;
555 id = rdma_create_id(&init_net, smbd_conn_upcall, info,
556 RDMA_PS_TCP, IB_QPT_RC);
557 if (IS_ERR(id)) {
558 rc = PTR_ERR(id);
559 log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
560 return id;
563 if (dstaddr->sa_family == AF_INET6)
564 sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
565 else
566 sport = &((struct sockaddr_in *)dstaddr)->sin_port;
568 *sport = htons(port);
570 init_completion(&info->ri_done);
571 info->ri_rc = -ETIMEDOUT;
573 rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
574 RDMA_RESOLVE_TIMEOUT);
575 if (rc) {
576 log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
577 goto out;
579 wait_for_completion_interruptible_timeout(
580 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
581 rc = info->ri_rc;
582 if (rc) {
583 log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
584 goto out;
587 info->ri_rc = -ETIMEDOUT;
588 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
589 if (rc) {
590 log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
591 goto out;
593 wait_for_completion_interruptible_timeout(
594 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
595 rc = info->ri_rc;
596 if (rc) {
597 log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
598 goto out;
601 return id;
603 out:
604 rdma_destroy_id(id);
605 return ERR_PTR(rc);
609 * Test if FRWR (Fast Registration Work Requests) is supported on the device
610 * This implementation requries FRWR on RDMA read/write
611 * return value: true if it is supported
613 static bool frwr_is_supported(struct ib_device_attr *attrs)
615 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
616 return false;
617 if (attrs->max_fast_reg_page_list_len == 0)
618 return false;
619 return true;
622 static int smbd_ia_open(
623 struct smbd_connection *info,
624 struct sockaddr *dstaddr, int port)
626 int rc;
628 info->id = smbd_create_id(info, dstaddr, port);
629 if (IS_ERR(info->id)) {
630 rc = PTR_ERR(info->id);
631 goto out1;
634 if (!frwr_is_supported(&info->id->device->attrs)) {
635 log_rdma_event(ERR,
636 "Fast Registration Work Requests "
637 "(FRWR) is not supported\n");
638 log_rdma_event(ERR,
639 "Device capability flags = %llx "
640 "max_fast_reg_page_list_len = %u\n",
641 info->id->device->attrs.device_cap_flags,
642 info->id->device->attrs.max_fast_reg_page_list_len);
643 rc = -EPROTONOSUPPORT;
644 goto out2;
646 info->max_frmr_depth = min_t(int,
647 smbd_max_frmr_depth,
648 info->id->device->attrs.max_fast_reg_page_list_len);
649 info->mr_type = IB_MR_TYPE_MEM_REG;
650 if (info->id->device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
651 info->mr_type = IB_MR_TYPE_SG_GAPS;
653 info->pd = ib_alloc_pd(info->id->device, 0);
654 if (IS_ERR(info->pd)) {
655 rc = PTR_ERR(info->pd);
656 log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
657 goto out2;
660 return 0;
662 out2:
663 rdma_destroy_id(info->id);
664 info->id = NULL;
666 out1:
667 return rc;
671 * Send a negotiation request message to the peer
672 * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
673 * After negotiation, the transport is connected and ready for
674 * carrying upper layer SMB payload
676 static int smbd_post_send_negotiate_req(struct smbd_connection *info)
678 struct ib_send_wr send_wr;
679 int rc = -ENOMEM;
680 struct smbd_request *request;
681 struct smbd_negotiate_req *packet;
683 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
684 if (!request)
685 return rc;
687 request->info = info;
689 packet = smbd_request_payload(request);
690 packet->min_version = cpu_to_le16(SMBD_V1);
691 packet->max_version = cpu_to_le16(SMBD_V1);
692 packet->reserved = 0;
693 packet->credits_requested = cpu_to_le16(info->send_credit_target);
694 packet->preferred_send_size = cpu_to_le32(info->max_send_size);
695 packet->max_receive_size = cpu_to_le32(info->max_receive_size);
696 packet->max_fragmented_size =
697 cpu_to_le32(info->max_fragmented_recv_size);
699 request->num_sge = 1;
700 request->sge[0].addr = ib_dma_map_single(
701 info->id->device, (void *)packet,
702 sizeof(*packet), DMA_TO_DEVICE);
703 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
704 rc = -EIO;
705 goto dma_mapping_failed;
708 request->sge[0].length = sizeof(*packet);
709 request->sge[0].lkey = info->pd->local_dma_lkey;
711 ib_dma_sync_single_for_device(
712 info->id->device, request->sge[0].addr,
713 request->sge[0].length, DMA_TO_DEVICE);
715 request->cqe.done = send_done;
717 send_wr.next = NULL;
718 send_wr.wr_cqe = &request->cqe;
719 send_wr.sg_list = request->sge;
720 send_wr.num_sge = request->num_sge;
721 send_wr.opcode = IB_WR_SEND;
722 send_wr.send_flags = IB_SEND_SIGNALED;
724 log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
725 request->sge[0].addr,
726 request->sge[0].length, request->sge[0].lkey);
728 atomic_inc(&info->send_pending);
729 rc = ib_post_send(info->id->qp, &send_wr, NULL);
730 if (!rc)
731 return 0;
733 /* if we reach here, post send failed */
734 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
735 atomic_dec(&info->send_pending);
736 ib_dma_unmap_single(info->id->device, request->sge[0].addr,
737 request->sge[0].length, DMA_TO_DEVICE);
739 smbd_disconnect_rdma_connection(info);
741 dma_mapping_failed:
742 mempool_free(request, info->request_mempool);
743 return rc;
747 * Extend the credits to remote peer
748 * This implements [MS-SMBD] 3.1.5.9
749 * The idea is that we should extend credits to remote peer as quickly as
750 * it's allowed, to maintain data flow. We allocate as much receive
751 * buffer as possible, and extend the receive credits to remote peer
752 * return value: the new credtis being granted.
754 static int manage_credits_prior_sending(struct smbd_connection *info)
756 int new_credits;
758 spin_lock(&info->lock_new_credits_offered);
759 new_credits = info->new_credits_offered;
760 info->new_credits_offered = 0;
761 spin_unlock(&info->lock_new_credits_offered);
763 return new_credits;
767 * Check if we need to send a KEEP_ALIVE message
768 * The idle connection timer triggers a KEEP_ALIVE message when expires
769 * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
770 * back a response.
771 * return value:
772 * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
773 * 0: otherwise
775 static int manage_keep_alive_before_sending(struct smbd_connection *info)
777 if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
778 info->keep_alive_requested = KEEP_ALIVE_SENT;
779 return 1;
781 return 0;
784 /* Post the send request */
785 static int smbd_post_send(struct smbd_connection *info,
786 struct smbd_request *request)
788 struct ib_send_wr send_wr;
789 int rc, i;
791 for (i = 0; i < request->num_sge; i++) {
792 log_rdma_send(INFO,
793 "rdma_request sge[%d] addr=%llu length=%u\n",
794 i, request->sge[i].addr, request->sge[i].length);
795 ib_dma_sync_single_for_device(
796 info->id->device,
797 request->sge[i].addr,
798 request->sge[i].length,
799 DMA_TO_DEVICE);
802 request->cqe.done = send_done;
804 send_wr.next = NULL;
805 send_wr.wr_cqe = &request->cqe;
806 send_wr.sg_list = request->sge;
807 send_wr.num_sge = request->num_sge;
808 send_wr.opcode = IB_WR_SEND;
809 send_wr.send_flags = IB_SEND_SIGNALED;
811 rc = ib_post_send(info->id->qp, &send_wr, NULL);
812 if (rc) {
813 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
814 smbd_disconnect_rdma_connection(info);
815 rc = -EAGAIN;
816 } else
817 /* Reset timer for idle connection after packet is sent */
818 mod_delayed_work(info->workqueue, &info->idle_timer_work,
819 info->keep_alive_interval*HZ);
821 return rc;
824 static int smbd_post_send_sgl(struct smbd_connection *info,
825 struct scatterlist *sgl, int data_length, int remaining_data_length)
827 int num_sgs;
828 int i, rc;
829 int header_length;
830 struct smbd_request *request;
831 struct smbd_data_transfer *packet;
832 int new_credits;
833 struct scatterlist *sg;
835 wait_credit:
836 /* Wait for send credits. A SMBD packet needs one credit */
837 rc = wait_event_interruptible(info->wait_send_queue,
838 atomic_read(&info->send_credits) > 0 ||
839 info->transport_status != SMBD_CONNECTED);
840 if (rc)
841 goto err_wait_credit;
843 if (info->transport_status != SMBD_CONNECTED) {
844 log_outgoing(ERR, "disconnected not sending on wait_credit\n");
845 rc = -EAGAIN;
846 goto err_wait_credit;
848 if (unlikely(atomic_dec_return(&info->send_credits) < 0)) {
849 atomic_inc(&info->send_credits);
850 goto wait_credit;
853 wait_send_queue:
854 wait_event(info->wait_post_send,
855 atomic_read(&info->send_pending) < info->send_credit_target ||
856 info->transport_status != SMBD_CONNECTED);
858 if (info->transport_status != SMBD_CONNECTED) {
859 log_outgoing(ERR, "disconnected not sending on wait_send_queue\n");
860 rc = -EAGAIN;
861 goto err_wait_send_queue;
864 if (unlikely(atomic_inc_return(&info->send_pending) >
865 info->send_credit_target)) {
866 atomic_dec(&info->send_pending);
867 goto wait_send_queue;
870 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
871 if (!request) {
872 rc = -ENOMEM;
873 goto err_alloc;
876 request->info = info;
878 /* Fill in the packet header */
879 packet = smbd_request_payload(request);
880 packet->credits_requested = cpu_to_le16(info->send_credit_target);
882 new_credits = manage_credits_prior_sending(info);
883 atomic_add(new_credits, &info->receive_credits);
884 packet->credits_granted = cpu_to_le16(new_credits);
886 info->send_immediate = false;
888 packet->flags = 0;
889 if (manage_keep_alive_before_sending(info))
890 packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
892 packet->reserved = 0;
893 if (!data_length)
894 packet->data_offset = 0;
895 else
896 packet->data_offset = cpu_to_le32(24);
897 packet->data_length = cpu_to_le32(data_length);
898 packet->remaining_data_length = cpu_to_le32(remaining_data_length);
899 packet->padding = 0;
901 log_outgoing(INFO, "credits_requested=%d credits_granted=%d "
902 "data_offset=%d data_length=%d remaining_data_length=%d\n",
903 le16_to_cpu(packet->credits_requested),
904 le16_to_cpu(packet->credits_granted),
905 le32_to_cpu(packet->data_offset),
906 le32_to_cpu(packet->data_length),
907 le32_to_cpu(packet->remaining_data_length));
909 /* Map the packet to DMA */
910 header_length = sizeof(struct smbd_data_transfer);
911 /* If this is a packet without payload, don't send padding */
912 if (!data_length)
913 header_length = offsetof(struct smbd_data_transfer, padding);
915 request->num_sge = 1;
916 request->sge[0].addr = ib_dma_map_single(info->id->device,
917 (void *)packet,
918 header_length,
919 DMA_TO_DEVICE);
920 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
921 rc = -EIO;
922 request->sge[0].addr = 0;
923 goto err_dma;
926 request->sge[0].length = header_length;
927 request->sge[0].lkey = info->pd->local_dma_lkey;
929 /* Fill in the packet data payload */
930 num_sgs = sgl ? sg_nents(sgl) : 0;
931 for_each_sg(sgl, sg, num_sgs, i) {
932 request->sge[i+1].addr =
933 ib_dma_map_page(info->id->device, sg_page(sg),
934 sg->offset, sg->length, DMA_TO_DEVICE);
935 if (ib_dma_mapping_error(
936 info->id->device, request->sge[i+1].addr)) {
937 rc = -EIO;
938 request->sge[i+1].addr = 0;
939 goto err_dma;
941 request->sge[i+1].length = sg->length;
942 request->sge[i+1].lkey = info->pd->local_dma_lkey;
943 request->num_sge++;
946 rc = smbd_post_send(info, request);
947 if (!rc)
948 return 0;
950 err_dma:
951 for (i = 0; i < request->num_sge; i++)
952 if (request->sge[i].addr)
953 ib_dma_unmap_single(info->id->device,
954 request->sge[i].addr,
955 request->sge[i].length,
956 DMA_TO_DEVICE);
957 mempool_free(request, info->request_mempool);
959 /* roll back receive credits and credits to be offered */
960 spin_lock(&info->lock_new_credits_offered);
961 info->new_credits_offered += new_credits;
962 spin_unlock(&info->lock_new_credits_offered);
963 atomic_sub(new_credits, &info->receive_credits);
965 err_alloc:
966 if (atomic_dec_and_test(&info->send_pending))
967 wake_up(&info->wait_send_pending);
969 err_wait_send_queue:
970 /* roll back send credits and pending */
971 atomic_inc(&info->send_credits);
973 err_wait_credit:
974 return rc;
978 * Send a page
979 * page: the page to send
980 * offset: offset in the page to send
981 * size: length in the page to send
982 * remaining_data_length: remaining data to send in this payload
984 static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
985 unsigned long offset, size_t size, int remaining_data_length)
987 struct scatterlist sgl;
989 sg_init_table(&sgl, 1);
990 sg_set_page(&sgl, page, size, offset);
992 return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
996 * Send an empty message
997 * Empty message is used to extend credits to peer to for keep live
998 * while there is no upper layer payload to send at the time
1000 static int smbd_post_send_empty(struct smbd_connection *info)
1002 info->count_send_empty++;
1003 return smbd_post_send_sgl(info, NULL, 0, 0);
1007 * Send a data buffer
1008 * iov: the iov array describing the data buffers
1009 * n_vec: number of iov array
1010 * remaining_data_length: remaining data to send following this packet
1011 * in segmented SMBD packet
1013 static int smbd_post_send_data(
1014 struct smbd_connection *info, struct kvec *iov, int n_vec,
1015 int remaining_data_length)
1017 int i;
1018 u32 data_length = 0;
1019 struct scatterlist sgl[SMBDIRECT_MAX_SGE];
1021 if (n_vec > SMBDIRECT_MAX_SGE) {
1022 cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
1023 return -EINVAL;
1026 sg_init_table(sgl, n_vec);
1027 for (i = 0; i < n_vec; i++) {
1028 data_length += iov[i].iov_len;
1029 sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
1032 return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
1036 * Post a receive request to the transport
1037 * The remote peer can only send data when a receive request is posted
1038 * The interaction is controlled by send/receive credit system
1040 static int smbd_post_recv(
1041 struct smbd_connection *info, struct smbd_response *response)
1043 struct ib_recv_wr recv_wr;
1044 int rc = -EIO;
1046 response->sge.addr = ib_dma_map_single(
1047 info->id->device, response->packet,
1048 info->max_receive_size, DMA_FROM_DEVICE);
1049 if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1050 return rc;
1052 response->sge.length = info->max_receive_size;
1053 response->sge.lkey = info->pd->local_dma_lkey;
1055 response->cqe.done = recv_done;
1057 recv_wr.wr_cqe = &response->cqe;
1058 recv_wr.next = NULL;
1059 recv_wr.sg_list = &response->sge;
1060 recv_wr.num_sge = 1;
1062 rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
1063 if (rc) {
1064 ib_dma_unmap_single(info->id->device, response->sge.addr,
1065 response->sge.length, DMA_FROM_DEVICE);
1066 smbd_disconnect_rdma_connection(info);
1067 log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1070 return rc;
1073 /* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1074 static int smbd_negotiate(struct smbd_connection *info)
1076 int rc;
1077 struct smbd_response *response = get_receive_buffer(info);
1079 response->type = SMBD_NEGOTIATE_RESP;
1080 rc = smbd_post_recv(info, response);
1081 log_rdma_event(INFO,
1082 "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x "
1083 "iov.lkey=%x\n",
1084 rc, response->sge.addr,
1085 response->sge.length, response->sge.lkey);
1086 if (rc)
1087 return rc;
1089 init_completion(&info->negotiate_completion);
1090 info->negotiate_done = false;
1091 rc = smbd_post_send_negotiate_req(info);
1092 if (rc)
1093 return rc;
1095 rc = wait_for_completion_interruptible_timeout(
1096 &info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1097 log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1099 if (info->negotiate_done)
1100 return 0;
1102 if (rc == 0)
1103 rc = -ETIMEDOUT;
1104 else if (rc == -ERESTARTSYS)
1105 rc = -EINTR;
1106 else
1107 rc = -ENOTCONN;
1109 return rc;
1112 static void put_empty_packet(
1113 struct smbd_connection *info, struct smbd_response *response)
1115 spin_lock(&info->empty_packet_queue_lock);
1116 list_add_tail(&response->list, &info->empty_packet_queue);
1117 info->count_empty_packet_queue++;
1118 spin_unlock(&info->empty_packet_queue_lock);
1120 queue_work(info->workqueue, &info->post_send_credits_work);
1124 * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1125 * This is a queue for reassembling upper layer payload and present to upper
1126 * layer. All the inncoming payload go to the reassembly queue, regardless of
1127 * if reassembly is required. The uuper layer code reads from the queue for all
1128 * incoming payloads.
1129 * Put a received packet to the reassembly queue
1130 * response: the packet received
1131 * data_length: the size of payload in this packet
1133 static void enqueue_reassembly(
1134 struct smbd_connection *info,
1135 struct smbd_response *response,
1136 int data_length)
1138 spin_lock(&info->reassembly_queue_lock);
1139 list_add_tail(&response->list, &info->reassembly_queue);
1140 info->reassembly_queue_length++;
1142 * Make sure reassembly_data_length is updated after list and
1143 * reassembly_queue_length are updated. On the dequeue side
1144 * reassembly_data_length is checked without a lock to determine
1145 * if reassembly_queue_length and list is up to date
1147 virt_wmb();
1148 info->reassembly_data_length += data_length;
1149 spin_unlock(&info->reassembly_queue_lock);
1150 info->count_reassembly_queue++;
1151 info->count_enqueue_reassembly_queue++;
1155 * Get the first entry at the front of reassembly queue
1156 * Caller is responsible for locking
1157 * return value: the first entry if any, NULL if queue is empty
1159 static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1161 struct smbd_response *ret = NULL;
1163 if (!list_empty(&info->reassembly_queue)) {
1164 ret = list_first_entry(
1165 &info->reassembly_queue,
1166 struct smbd_response, list);
1168 return ret;
1171 static struct smbd_response *get_empty_queue_buffer(
1172 struct smbd_connection *info)
1174 struct smbd_response *ret = NULL;
1175 unsigned long flags;
1177 spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1178 if (!list_empty(&info->empty_packet_queue)) {
1179 ret = list_first_entry(
1180 &info->empty_packet_queue,
1181 struct smbd_response, list);
1182 list_del(&ret->list);
1183 info->count_empty_packet_queue--;
1185 spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1187 return ret;
1191 * Get a receive buffer
1192 * For each remote send, we need to post a receive. The receive buffers are
1193 * pre-allocated in advance.
1194 * return value: the receive buffer, NULL if none is available
1196 static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1198 struct smbd_response *ret = NULL;
1199 unsigned long flags;
1201 spin_lock_irqsave(&info->receive_queue_lock, flags);
1202 if (!list_empty(&info->receive_queue)) {
1203 ret = list_first_entry(
1204 &info->receive_queue,
1205 struct smbd_response, list);
1206 list_del(&ret->list);
1207 info->count_receive_queue--;
1208 info->count_get_receive_buffer++;
1210 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1212 return ret;
1216 * Return a receive buffer
1217 * Upon returning of a receive buffer, we can post new receive and extend
1218 * more receive credits to remote peer. This is done immediately after a
1219 * receive buffer is returned.
1221 static void put_receive_buffer(
1222 struct smbd_connection *info, struct smbd_response *response)
1224 unsigned long flags;
1226 ib_dma_unmap_single(info->id->device, response->sge.addr,
1227 response->sge.length, DMA_FROM_DEVICE);
1229 spin_lock_irqsave(&info->receive_queue_lock, flags);
1230 list_add_tail(&response->list, &info->receive_queue);
1231 info->count_receive_queue++;
1232 info->count_put_receive_buffer++;
1233 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1235 queue_work(info->workqueue, &info->post_send_credits_work);
1238 /* Preallocate all receive buffer on transport establishment */
1239 static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1241 int i;
1242 struct smbd_response *response;
1244 INIT_LIST_HEAD(&info->reassembly_queue);
1245 spin_lock_init(&info->reassembly_queue_lock);
1246 info->reassembly_data_length = 0;
1247 info->reassembly_queue_length = 0;
1249 INIT_LIST_HEAD(&info->receive_queue);
1250 spin_lock_init(&info->receive_queue_lock);
1251 info->count_receive_queue = 0;
1253 INIT_LIST_HEAD(&info->empty_packet_queue);
1254 spin_lock_init(&info->empty_packet_queue_lock);
1255 info->count_empty_packet_queue = 0;
1257 init_waitqueue_head(&info->wait_receive_queues);
1259 for (i = 0; i < num_buf; i++) {
1260 response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1261 if (!response)
1262 goto allocate_failed;
1264 response->info = info;
1265 list_add_tail(&response->list, &info->receive_queue);
1266 info->count_receive_queue++;
1269 return 0;
1271 allocate_failed:
1272 while (!list_empty(&info->receive_queue)) {
1273 response = list_first_entry(
1274 &info->receive_queue,
1275 struct smbd_response, list);
1276 list_del(&response->list);
1277 info->count_receive_queue--;
1279 mempool_free(response, info->response_mempool);
1281 return -ENOMEM;
1284 static void destroy_receive_buffers(struct smbd_connection *info)
1286 struct smbd_response *response;
1288 while ((response = get_receive_buffer(info)))
1289 mempool_free(response, info->response_mempool);
1291 while ((response = get_empty_queue_buffer(info)))
1292 mempool_free(response, info->response_mempool);
1295 /* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1296 static void idle_connection_timer(struct work_struct *work)
1298 struct smbd_connection *info = container_of(
1299 work, struct smbd_connection,
1300 idle_timer_work.work);
1302 if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1303 log_keep_alive(ERR,
1304 "error status info->keep_alive_requested=%d\n",
1305 info->keep_alive_requested);
1306 smbd_disconnect_rdma_connection(info);
1307 return;
1310 log_keep_alive(INFO, "about to send an empty idle message\n");
1311 smbd_post_send_empty(info);
1313 /* Setup the next idle timeout work */
1314 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1315 info->keep_alive_interval*HZ);
1319 * Destroy the transport and related RDMA and memory resources
1320 * Need to go through all the pending counters and make sure on one is using
1321 * the transport while it is destroyed
1323 void smbd_destroy(struct TCP_Server_Info *server)
1325 struct smbd_connection *info = server->smbd_conn;
1326 struct smbd_response *response;
1327 unsigned long flags;
1329 if (!info) {
1330 log_rdma_event(INFO, "rdma session already destroyed\n");
1331 return;
1334 log_rdma_event(INFO, "destroying rdma session\n");
1335 if (info->transport_status != SMBD_DISCONNECTED) {
1336 rdma_disconnect(server->smbd_conn->id);
1337 log_rdma_event(INFO, "wait for transport being disconnected\n");
1338 wait_event_interruptible(
1339 info->disconn_wait,
1340 info->transport_status == SMBD_DISCONNECTED);
1343 log_rdma_event(INFO, "destroying qp\n");
1344 ib_drain_qp(info->id->qp);
1345 rdma_destroy_qp(info->id);
1347 log_rdma_event(INFO, "cancelling idle timer\n");
1348 cancel_delayed_work_sync(&info->idle_timer_work);
1350 log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
1351 wait_event(info->wait_send_pending,
1352 atomic_read(&info->send_pending) == 0);
1354 /* It's not posssible for upper layer to get to reassembly */
1355 log_rdma_event(INFO, "drain the reassembly queue\n");
1356 do {
1357 spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1358 response = _get_first_reassembly(info);
1359 if (response) {
1360 list_del(&response->list);
1361 spin_unlock_irqrestore(
1362 &info->reassembly_queue_lock, flags);
1363 put_receive_buffer(info, response);
1364 } else
1365 spin_unlock_irqrestore(
1366 &info->reassembly_queue_lock, flags);
1367 } while (response);
1368 info->reassembly_data_length = 0;
1370 log_rdma_event(INFO, "free receive buffers\n");
1371 wait_event(info->wait_receive_queues,
1372 info->count_receive_queue + info->count_empty_packet_queue
1373 == info->receive_credit_max);
1374 destroy_receive_buffers(info);
1377 * For performance reasons, memory registration and deregistration
1378 * are not locked by srv_mutex. It is possible some processes are
1379 * blocked on transport srv_mutex while holding memory registration.
1380 * Release the transport srv_mutex to allow them to hit the failure
1381 * path when sending data, and then release memory registartions.
1383 log_rdma_event(INFO, "freeing mr list\n");
1384 wake_up_interruptible_all(&info->wait_mr);
1385 while (atomic_read(&info->mr_used_count)) {
1386 mutex_unlock(&server->srv_mutex);
1387 msleep(1000);
1388 mutex_lock(&server->srv_mutex);
1390 destroy_mr_list(info);
1392 ib_free_cq(info->send_cq);
1393 ib_free_cq(info->recv_cq);
1394 ib_dealloc_pd(info->pd);
1395 rdma_destroy_id(info->id);
1397 /* free mempools */
1398 mempool_destroy(info->request_mempool);
1399 kmem_cache_destroy(info->request_cache);
1401 mempool_destroy(info->response_mempool);
1402 kmem_cache_destroy(info->response_cache);
1404 info->transport_status = SMBD_DESTROYED;
1406 destroy_workqueue(info->workqueue);
1407 log_rdma_event(INFO, "rdma session destroyed\n");
1408 kfree(info);
1412 * Reconnect this SMBD connection, called from upper layer
1413 * return value: 0 on success, or actual error code
1415 int smbd_reconnect(struct TCP_Server_Info *server)
1417 log_rdma_event(INFO, "reconnecting rdma session\n");
1419 if (!server->smbd_conn) {
1420 log_rdma_event(INFO, "rdma session already destroyed\n");
1421 goto create_conn;
1425 * This is possible if transport is disconnected and we haven't received
1426 * notification from RDMA, but upper layer has detected timeout
1428 if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1429 log_rdma_event(INFO, "disconnecting transport\n");
1430 smbd_destroy(server);
1433 create_conn:
1434 log_rdma_event(INFO, "creating rdma session\n");
1435 server->smbd_conn = smbd_get_connection(
1436 server, (struct sockaddr *) &server->dstaddr);
1438 if (server->smbd_conn)
1439 cifs_dbg(VFS, "RDMA transport re-established\n");
1441 return server->smbd_conn ? 0 : -ENOENT;
1444 static void destroy_caches_and_workqueue(struct smbd_connection *info)
1446 destroy_receive_buffers(info);
1447 destroy_workqueue(info->workqueue);
1448 mempool_destroy(info->response_mempool);
1449 kmem_cache_destroy(info->response_cache);
1450 mempool_destroy(info->request_mempool);
1451 kmem_cache_destroy(info->request_cache);
1454 #define MAX_NAME_LEN 80
1455 static int allocate_caches_and_workqueue(struct smbd_connection *info)
1457 char name[MAX_NAME_LEN];
1458 int rc;
1460 scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
1461 info->request_cache =
1462 kmem_cache_create(
1463 name,
1464 sizeof(struct smbd_request) +
1465 sizeof(struct smbd_data_transfer),
1466 0, SLAB_HWCACHE_ALIGN, NULL);
1467 if (!info->request_cache)
1468 return -ENOMEM;
1470 info->request_mempool =
1471 mempool_create(info->send_credit_target, mempool_alloc_slab,
1472 mempool_free_slab, info->request_cache);
1473 if (!info->request_mempool)
1474 goto out1;
1476 scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
1477 info->response_cache =
1478 kmem_cache_create(
1479 name,
1480 sizeof(struct smbd_response) +
1481 info->max_receive_size,
1482 0, SLAB_HWCACHE_ALIGN, NULL);
1483 if (!info->response_cache)
1484 goto out2;
1486 info->response_mempool =
1487 mempool_create(info->receive_credit_max, mempool_alloc_slab,
1488 mempool_free_slab, info->response_cache);
1489 if (!info->response_mempool)
1490 goto out3;
1492 scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
1493 info->workqueue = create_workqueue(name);
1494 if (!info->workqueue)
1495 goto out4;
1497 rc = allocate_receive_buffers(info, info->receive_credit_max);
1498 if (rc) {
1499 log_rdma_event(ERR, "failed to allocate receive buffers\n");
1500 goto out5;
1503 return 0;
1505 out5:
1506 destroy_workqueue(info->workqueue);
1507 out4:
1508 mempool_destroy(info->response_mempool);
1509 out3:
1510 kmem_cache_destroy(info->response_cache);
1511 out2:
1512 mempool_destroy(info->request_mempool);
1513 out1:
1514 kmem_cache_destroy(info->request_cache);
1515 return -ENOMEM;
1518 /* Create a SMBD connection, called by upper layer */
1519 static struct smbd_connection *_smbd_get_connection(
1520 struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1522 int rc;
1523 struct smbd_connection *info;
1524 struct rdma_conn_param conn_param;
1525 struct ib_qp_init_attr qp_attr;
1526 struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
1527 struct ib_port_immutable port_immutable;
1528 u32 ird_ord_hdr[2];
1530 info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1531 if (!info)
1532 return NULL;
1534 info->transport_status = SMBD_CONNECTING;
1535 rc = smbd_ia_open(info, dstaddr, port);
1536 if (rc) {
1537 log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1538 goto create_id_failed;
1541 if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1542 smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1543 log_rdma_event(ERR,
1544 "consider lowering send_credit_target = %d. "
1545 "Possible CQE overrun, device "
1546 "reporting max_cpe %d max_qp_wr %d\n",
1547 smbd_send_credit_target,
1548 info->id->device->attrs.max_cqe,
1549 info->id->device->attrs.max_qp_wr);
1550 goto config_failed;
1553 if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1554 smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1555 log_rdma_event(ERR,
1556 "consider lowering receive_credit_max = %d. "
1557 "Possible CQE overrun, device "
1558 "reporting max_cpe %d max_qp_wr %d\n",
1559 smbd_receive_credit_max,
1560 info->id->device->attrs.max_cqe,
1561 info->id->device->attrs.max_qp_wr);
1562 goto config_failed;
1565 info->receive_credit_max = smbd_receive_credit_max;
1566 info->send_credit_target = smbd_send_credit_target;
1567 info->max_send_size = smbd_max_send_size;
1568 info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1569 info->max_receive_size = smbd_max_receive_size;
1570 info->keep_alive_interval = smbd_keep_alive_interval;
1572 if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
1573 log_rdma_event(ERR,
1574 "warning: device max_send_sge = %d too small\n",
1575 info->id->device->attrs.max_send_sge);
1576 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1578 if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
1579 log_rdma_event(ERR,
1580 "warning: device max_recv_sge = %d too small\n",
1581 info->id->device->attrs.max_recv_sge);
1582 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1585 info->send_cq = NULL;
1586 info->recv_cq = NULL;
1587 info->send_cq =
1588 ib_alloc_cq_any(info->id->device, info,
1589 info->send_credit_target, IB_POLL_SOFTIRQ);
1590 if (IS_ERR(info->send_cq)) {
1591 info->send_cq = NULL;
1592 goto alloc_cq_failed;
1595 info->recv_cq =
1596 ib_alloc_cq_any(info->id->device, info,
1597 info->receive_credit_max, IB_POLL_SOFTIRQ);
1598 if (IS_ERR(info->recv_cq)) {
1599 info->recv_cq = NULL;
1600 goto alloc_cq_failed;
1603 memset(&qp_attr, 0, sizeof(qp_attr));
1604 qp_attr.event_handler = smbd_qp_async_error_upcall;
1605 qp_attr.qp_context = info;
1606 qp_attr.cap.max_send_wr = info->send_credit_target;
1607 qp_attr.cap.max_recv_wr = info->receive_credit_max;
1608 qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
1609 qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
1610 qp_attr.cap.max_inline_data = 0;
1611 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1612 qp_attr.qp_type = IB_QPT_RC;
1613 qp_attr.send_cq = info->send_cq;
1614 qp_attr.recv_cq = info->recv_cq;
1615 qp_attr.port_num = ~0;
1617 rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1618 if (rc) {
1619 log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1620 goto create_qp_failed;
1623 memset(&conn_param, 0, sizeof(conn_param));
1624 conn_param.initiator_depth = 0;
1626 conn_param.responder_resources =
1627 info->id->device->attrs.max_qp_rd_atom
1628 < SMBD_CM_RESPONDER_RESOURCES ?
1629 info->id->device->attrs.max_qp_rd_atom :
1630 SMBD_CM_RESPONDER_RESOURCES;
1631 info->responder_resources = conn_param.responder_resources;
1632 log_rdma_mr(INFO, "responder_resources=%d\n",
1633 info->responder_resources);
1635 /* Need to send IRD/ORD in private data for iWARP */
1636 info->id->device->ops.get_port_immutable(
1637 info->id->device, info->id->port_num, &port_immutable);
1638 if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1639 ird_ord_hdr[0] = info->responder_resources;
1640 ird_ord_hdr[1] = 1;
1641 conn_param.private_data = ird_ord_hdr;
1642 conn_param.private_data_len = sizeof(ird_ord_hdr);
1643 } else {
1644 conn_param.private_data = NULL;
1645 conn_param.private_data_len = 0;
1648 conn_param.retry_count = SMBD_CM_RETRY;
1649 conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1650 conn_param.flow_control = 0;
1652 log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1653 &addr_in->sin_addr, port);
1655 init_waitqueue_head(&info->conn_wait);
1656 init_waitqueue_head(&info->disconn_wait);
1657 init_waitqueue_head(&info->wait_reassembly_queue);
1658 rc = rdma_connect(info->id, &conn_param);
1659 if (rc) {
1660 log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1661 goto rdma_connect_failed;
1664 wait_event_interruptible(
1665 info->conn_wait, info->transport_status != SMBD_CONNECTING);
1667 if (info->transport_status != SMBD_CONNECTED) {
1668 log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1669 goto rdma_connect_failed;
1672 log_rdma_event(INFO, "rdma_connect connected\n");
1674 rc = allocate_caches_and_workqueue(info);
1675 if (rc) {
1676 log_rdma_event(ERR, "cache allocation failed\n");
1677 goto allocate_cache_failed;
1680 init_waitqueue_head(&info->wait_send_queue);
1681 INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
1682 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1683 info->keep_alive_interval*HZ);
1685 init_waitqueue_head(&info->wait_send_pending);
1686 atomic_set(&info->send_pending, 0);
1688 init_waitqueue_head(&info->wait_post_send);
1690 INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
1691 INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1692 info->new_credits_offered = 0;
1693 spin_lock_init(&info->lock_new_credits_offered);
1695 rc = smbd_negotiate(info);
1696 if (rc) {
1697 log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1698 goto negotiation_failed;
1701 rc = allocate_mr_list(info);
1702 if (rc) {
1703 log_rdma_mr(ERR, "memory registration allocation failed\n");
1704 goto allocate_mr_failed;
1707 return info;
1709 allocate_mr_failed:
1710 /* At this point, need to a full transport shutdown */
1711 smbd_destroy(server);
1712 return NULL;
1714 negotiation_failed:
1715 cancel_delayed_work_sync(&info->idle_timer_work);
1716 destroy_caches_and_workqueue(info);
1717 info->transport_status = SMBD_NEGOTIATE_FAILED;
1718 init_waitqueue_head(&info->conn_wait);
1719 rdma_disconnect(info->id);
1720 wait_event(info->conn_wait,
1721 info->transport_status == SMBD_DISCONNECTED);
1723 allocate_cache_failed:
1724 rdma_connect_failed:
1725 rdma_destroy_qp(info->id);
1727 create_qp_failed:
1728 alloc_cq_failed:
1729 if (info->send_cq)
1730 ib_free_cq(info->send_cq);
1731 if (info->recv_cq)
1732 ib_free_cq(info->recv_cq);
1734 config_failed:
1735 ib_dealloc_pd(info->pd);
1736 rdma_destroy_id(info->id);
1738 create_id_failed:
1739 kfree(info);
1740 return NULL;
1743 struct smbd_connection *smbd_get_connection(
1744 struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1746 struct smbd_connection *ret;
1747 int port = SMBD_PORT;
1749 try_again:
1750 ret = _smbd_get_connection(server, dstaddr, port);
1752 /* Try SMB_PORT if SMBD_PORT doesn't work */
1753 if (!ret && port == SMBD_PORT) {
1754 port = SMB_PORT;
1755 goto try_again;
1757 return ret;
1761 * Receive data from receive reassembly queue
1762 * All the incoming data packets are placed in reassembly queue
1763 * buf: the buffer to read data into
1764 * size: the length of data to read
1765 * return value: actual data read
1766 * Note: this implementation copies the data from reassebmly queue to receive
1767 * buffers used by upper layer. This is not the optimal code path. A better way
1768 * to do it is to not have upper layer allocate its receive buffers but rather
1769 * borrow the buffer from reassembly queue, and return it after data is
1770 * consumed. But this will require more changes to upper layer code, and also
1771 * need to consider packet boundaries while they still being reassembled.
1773 static int smbd_recv_buf(struct smbd_connection *info, char *buf,
1774 unsigned int size)
1776 struct smbd_response *response;
1777 struct smbd_data_transfer *data_transfer;
1778 int to_copy, to_read, data_read, offset;
1779 u32 data_length, remaining_data_length, data_offset;
1780 int rc;
1782 again:
1784 * No need to hold the reassembly queue lock all the time as we are
1785 * the only one reading from the front of the queue. The transport
1786 * may add more entries to the back of the queue at the same time
1788 log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1789 info->reassembly_data_length);
1790 if (info->reassembly_data_length >= size) {
1791 int queue_length;
1792 int queue_removed = 0;
1795 * Need to make sure reassembly_data_length is read before
1796 * reading reassembly_queue_length and calling
1797 * _get_first_reassembly. This call is lock free
1798 * as we never read at the end of the queue which are being
1799 * updated in SOFTIRQ as more data is received
1801 virt_rmb();
1802 queue_length = info->reassembly_queue_length;
1803 data_read = 0;
1804 to_read = size;
1805 offset = info->first_entry_offset;
1806 while (data_read < size) {
1807 response = _get_first_reassembly(info);
1808 data_transfer = smbd_response_payload(response);
1809 data_length = le32_to_cpu(data_transfer->data_length);
1810 remaining_data_length =
1811 le32_to_cpu(
1812 data_transfer->remaining_data_length);
1813 data_offset = le32_to_cpu(data_transfer->data_offset);
1816 * The upper layer expects RFC1002 length at the
1817 * beginning of the payload. Return it to indicate
1818 * the total length of the packet. This minimize the
1819 * change to upper layer packet processing logic. This
1820 * will be eventually remove when an intermediate
1821 * transport layer is added
1823 if (response->first_segment && size == 4) {
1824 unsigned int rfc1002_len =
1825 data_length + remaining_data_length;
1826 *((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1827 data_read = 4;
1828 response->first_segment = false;
1829 log_read(INFO, "returning rfc1002 length %d\n",
1830 rfc1002_len);
1831 goto read_rfc1002_done;
1834 to_copy = min_t(int, data_length - offset, to_read);
1835 memcpy(
1836 buf + data_read,
1837 (char *)data_transfer + data_offset + offset,
1838 to_copy);
1840 /* move on to the next buffer? */
1841 if (to_copy == data_length - offset) {
1842 queue_length--;
1844 * No need to lock if we are not at the
1845 * end of the queue
1847 if (queue_length)
1848 list_del(&response->list);
1849 else {
1850 spin_lock_irq(
1851 &info->reassembly_queue_lock);
1852 list_del(&response->list);
1853 spin_unlock_irq(
1854 &info->reassembly_queue_lock);
1856 queue_removed++;
1857 info->count_reassembly_queue--;
1858 info->count_dequeue_reassembly_queue++;
1859 put_receive_buffer(info, response);
1860 offset = 0;
1861 log_read(INFO, "put_receive_buffer offset=0\n");
1862 } else
1863 offset += to_copy;
1865 to_read -= to_copy;
1866 data_read += to_copy;
1868 log_read(INFO, "_get_first_reassembly memcpy %d bytes "
1869 "data_transfer_length-offset=%d after that "
1870 "to_read=%d data_read=%d offset=%d\n",
1871 to_copy, data_length - offset,
1872 to_read, data_read, offset);
1875 spin_lock_irq(&info->reassembly_queue_lock);
1876 info->reassembly_data_length -= data_read;
1877 info->reassembly_queue_length -= queue_removed;
1878 spin_unlock_irq(&info->reassembly_queue_lock);
1880 info->first_entry_offset = offset;
1881 log_read(INFO, "returning to thread data_read=%d "
1882 "reassembly_data_length=%d first_entry_offset=%d\n",
1883 data_read, info->reassembly_data_length,
1884 info->first_entry_offset);
1885 read_rfc1002_done:
1886 return data_read;
1889 log_read(INFO, "wait_event on more data\n");
1890 rc = wait_event_interruptible(
1891 info->wait_reassembly_queue,
1892 info->reassembly_data_length >= size ||
1893 info->transport_status != SMBD_CONNECTED);
1894 /* Don't return any data if interrupted */
1895 if (rc)
1896 return rc;
1898 if (info->transport_status != SMBD_CONNECTED) {
1899 log_read(ERR, "disconnected\n");
1900 return -ECONNABORTED;
1903 goto again;
1907 * Receive a page from receive reassembly queue
1908 * page: the page to read data into
1909 * to_read: the length of data to read
1910 * return value: actual data read
1912 static int smbd_recv_page(struct smbd_connection *info,
1913 struct page *page, unsigned int page_offset,
1914 unsigned int to_read)
1916 int ret;
1917 char *to_address;
1918 void *page_address;
1920 /* make sure we have the page ready for read */
1921 ret = wait_event_interruptible(
1922 info->wait_reassembly_queue,
1923 info->reassembly_data_length >= to_read ||
1924 info->transport_status != SMBD_CONNECTED);
1925 if (ret)
1926 return ret;
1928 /* now we can read from reassembly queue and not sleep */
1929 page_address = kmap_atomic(page);
1930 to_address = (char *) page_address + page_offset;
1932 log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
1933 page, to_address, to_read);
1935 ret = smbd_recv_buf(info, to_address, to_read);
1936 kunmap_atomic(page_address);
1938 return ret;
1942 * Receive data from transport
1943 * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
1944 * return: total bytes read, or 0. SMB Direct will not do partial read.
1946 int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
1948 char *buf;
1949 struct page *page;
1950 unsigned int to_read, page_offset;
1951 int rc;
1953 if (iov_iter_rw(&msg->msg_iter) == WRITE) {
1954 /* It's a bug in upper layer to get there */
1955 cifs_dbg(VFS, "CIFS: invalid msg iter dir %u\n",
1956 iov_iter_rw(&msg->msg_iter));
1957 rc = -EINVAL;
1958 goto out;
1961 switch (iov_iter_type(&msg->msg_iter)) {
1962 case ITER_KVEC:
1963 buf = msg->msg_iter.kvec->iov_base;
1964 to_read = msg->msg_iter.kvec->iov_len;
1965 rc = smbd_recv_buf(info, buf, to_read);
1966 break;
1968 case ITER_BVEC:
1969 page = msg->msg_iter.bvec->bv_page;
1970 page_offset = msg->msg_iter.bvec->bv_offset;
1971 to_read = msg->msg_iter.bvec->bv_len;
1972 rc = smbd_recv_page(info, page, page_offset, to_read);
1973 break;
1975 default:
1976 /* It's a bug in upper layer to get there */
1977 cifs_dbg(VFS, "CIFS: invalid msg type %d\n",
1978 iov_iter_type(&msg->msg_iter));
1979 rc = -EINVAL;
1982 out:
1983 /* SMBDirect will read it all or nothing */
1984 if (rc > 0)
1985 msg->msg_iter.count = 0;
1986 return rc;
1990 * Send data to transport
1991 * Each rqst is transported as a SMBDirect payload
1992 * rqst: the data to write
1993 * return value: 0 if successfully write, otherwise error code
1995 int smbd_send(struct TCP_Server_Info *server,
1996 int num_rqst, struct smb_rqst *rqst_array)
1998 struct smbd_connection *info = server->smbd_conn;
1999 struct kvec vec;
2000 int nvecs;
2001 int size;
2002 unsigned int buflen, remaining_data_length;
2003 int start, i, j;
2004 int max_iov_size =
2005 info->max_send_size - sizeof(struct smbd_data_transfer);
2006 struct kvec *iov;
2007 int rc;
2008 struct smb_rqst *rqst;
2009 int rqst_idx;
2011 if (info->transport_status != SMBD_CONNECTED) {
2012 rc = -EAGAIN;
2013 goto done;
2017 * Add in the page array if there is one. The caller needs to set
2018 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
2019 * ends at page boundary
2021 remaining_data_length = 0;
2022 for (i = 0; i < num_rqst; i++)
2023 remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
2025 if (remaining_data_length > info->max_fragmented_send_size) {
2026 log_write(ERR, "payload size %d > max size %d\n",
2027 remaining_data_length, info->max_fragmented_send_size);
2028 rc = -EINVAL;
2029 goto done;
2032 log_write(INFO, "num_rqst=%d total length=%u\n",
2033 num_rqst, remaining_data_length);
2035 rqst_idx = 0;
2036 next_rqst:
2037 rqst = &rqst_array[rqst_idx];
2038 iov = rqst->rq_iov;
2040 cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
2041 rqst_idx, smb_rqst_len(server, rqst));
2042 for (i = 0; i < rqst->rq_nvec; i++)
2043 dump_smb(iov[i].iov_base, iov[i].iov_len);
2046 log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d "
2047 "rq_tailsz=%d buflen=%lu\n",
2048 rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
2049 rqst->rq_tailsz, smb_rqst_len(server, rqst));
2051 start = i = 0;
2052 buflen = 0;
2053 while (true) {
2054 buflen += iov[i].iov_len;
2055 if (buflen > max_iov_size) {
2056 if (i > start) {
2057 remaining_data_length -=
2058 (buflen-iov[i].iov_len);
2059 log_write(INFO, "sending iov[] from start=%d "
2060 "i=%d nvecs=%d "
2061 "remaining_data_length=%d\n",
2062 start, i, i-start,
2063 remaining_data_length);
2064 rc = smbd_post_send_data(
2065 info, &iov[start], i-start,
2066 remaining_data_length);
2067 if (rc)
2068 goto done;
2069 } else {
2070 /* iov[start] is too big, break it */
2071 nvecs = (buflen+max_iov_size-1)/max_iov_size;
2072 log_write(INFO, "iov[%d] iov_base=%p buflen=%d"
2073 " break to %d vectors\n",
2074 start, iov[start].iov_base,
2075 buflen, nvecs);
2076 for (j = 0; j < nvecs; j++) {
2077 vec.iov_base =
2078 (char *)iov[start].iov_base +
2079 j*max_iov_size;
2080 vec.iov_len = max_iov_size;
2081 if (j == nvecs-1)
2082 vec.iov_len =
2083 buflen -
2084 max_iov_size*(nvecs-1);
2085 remaining_data_length -= vec.iov_len;
2086 log_write(INFO,
2087 "sending vec j=%d iov_base=%p"
2088 " iov_len=%zu "
2089 "remaining_data_length=%d\n",
2090 j, vec.iov_base, vec.iov_len,
2091 remaining_data_length);
2092 rc = smbd_post_send_data(
2093 info, &vec, 1,
2094 remaining_data_length);
2095 if (rc)
2096 goto done;
2098 i++;
2099 if (i == rqst->rq_nvec)
2100 break;
2102 start = i;
2103 buflen = 0;
2104 } else {
2105 i++;
2106 if (i == rqst->rq_nvec) {
2107 /* send out all remaining vecs */
2108 remaining_data_length -= buflen;
2109 log_write(INFO,
2110 "sending iov[] from start=%d i=%d "
2111 "nvecs=%d remaining_data_length=%d\n",
2112 start, i, i-start,
2113 remaining_data_length);
2114 rc = smbd_post_send_data(info, &iov[start],
2115 i-start, remaining_data_length);
2116 if (rc)
2117 goto done;
2118 break;
2121 log_write(INFO, "looping i=%d buflen=%d\n", i, buflen);
2124 /* now sending pages if there are any */
2125 for (i = 0; i < rqst->rq_npages; i++) {
2126 unsigned int offset;
2128 rqst_page_get_length(rqst, i, &buflen, &offset);
2129 nvecs = (buflen + max_iov_size - 1) / max_iov_size;
2130 log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
2131 buflen, nvecs);
2132 for (j = 0; j < nvecs; j++) {
2133 size = max_iov_size;
2134 if (j == nvecs-1)
2135 size = buflen - j*max_iov_size;
2136 remaining_data_length -= size;
2137 log_write(INFO, "sending pages i=%d offset=%d size=%d"
2138 " remaining_data_length=%d\n",
2139 i, j*max_iov_size+offset, size,
2140 remaining_data_length);
2141 rc = smbd_post_send_page(
2142 info, rqst->rq_pages[i],
2143 j*max_iov_size + offset,
2144 size, remaining_data_length);
2145 if (rc)
2146 goto done;
2150 rqst_idx++;
2151 if (rqst_idx < num_rqst)
2152 goto next_rqst;
2154 done:
2156 * As an optimization, we don't wait for individual I/O to finish
2157 * before sending the next one.
2158 * Send them all and wait for pending send count to get to 0
2159 * that means all the I/Os have been out and we are good to return
2162 wait_event(info->wait_send_pending,
2163 atomic_read(&info->send_pending) == 0);
2165 return rc;
2168 static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
2170 struct smbd_mr *mr;
2171 struct ib_cqe *cqe;
2173 if (wc->status) {
2174 log_rdma_mr(ERR, "status=%d\n", wc->status);
2175 cqe = wc->wr_cqe;
2176 mr = container_of(cqe, struct smbd_mr, cqe);
2177 smbd_disconnect_rdma_connection(mr->conn);
2182 * The work queue function that recovers MRs
2183 * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
2184 * again. Both calls are slow, so finish them in a workqueue. This will not
2185 * block I/O path.
2186 * There is one workqueue that recovers MRs, there is no need to lock as the
2187 * I/O requests calling smbd_register_mr will never update the links in the
2188 * mr_list.
2190 static void smbd_mr_recovery_work(struct work_struct *work)
2192 struct smbd_connection *info =
2193 container_of(work, struct smbd_connection, mr_recovery_work);
2194 struct smbd_mr *smbdirect_mr;
2195 int rc;
2197 list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
2198 if (smbdirect_mr->state == MR_ERROR) {
2200 /* recover this MR entry */
2201 rc = ib_dereg_mr(smbdirect_mr->mr);
2202 if (rc) {
2203 log_rdma_mr(ERR,
2204 "ib_dereg_mr failed rc=%x\n",
2205 rc);
2206 smbd_disconnect_rdma_connection(info);
2207 continue;
2210 smbdirect_mr->mr = ib_alloc_mr(
2211 info->pd, info->mr_type,
2212 info->max_frmr_depth);
2213 if (IS_ERR(smbdirect_mr->mr)) {
2214 log_rdma_mr(ERR,
2215 "ib_alloc_mr failed mr_type=%x "
2216 "max_frmr_depth=%x\n",
2217 info->mr_type,
2218 info->max_frmr_depth);
2219 smbd_disconnect_rdma_connection(info);
2220 continue;
2222 } else
2223 /* This MR is being used, don't recover it */
2224 continue;
2226 smbdirect_mr->state = MR_READY;
2228 /* smbdirect_mr->state is updated by this function
2229 * and is read and updated by I/O issuing CPUs trying
2230 * to get a MR, the call to atomic_inc_return
2231 * implicates a memory barrier and guarantees this
2232 * value is updated before waking up any calls to
2233 * get_mr() from the I/O issuing CPUs
2235 if (atomic_inc_return(&info->mr_ready_count) == 1)
2236 wake_up_interruptible(&info->wait_mr);
2240 static void destroy_mr_list(struct smbd_connection *info)
2242 struct smbd_mr *mr, *tmp;
2244 cancel_work_sync(&info->mr_recovery_work);
2245 list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
2246 if (mr->state == MR_INVALIDATED)
2247 ib_dma_unmap_sg(info->id->device, mr->sgl,
2248 mr->sgl_count, mr->dir);
2249 ib_dereg_mr(mr->mr);
2250 kfree(mr->sgl);
2251 kfree(mr);
2256 * Allocate MRs used for RDMA read/write
2257 * The number of MRs will not exceed hardware capability in responder_resources
2258 * All MRs are kept in mr_list. The MR can be recovered after it's used
2259 * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
2260 * as MRs are used and recovered for I/O, but the list links will not change
2262 static int allocate_mr_list(struct smbd_connection *info)
2264 int i;
2265 struct smbd_mr *smbdirect_mr, *tmp;
2267 INIT_LIST_HEAD(&info->mr_list);
2268 init_waitqueue_head(&info->wait_mr);
2269 spin_lock_init(&info->mr_list_lock);
2270 atomic_set(&info->mr_ready_count, 0);
2271 atomic_set(&info->mr_used_count, 0);
2272 init_waitqueue_head(&info->wait_for_mr_cleanup);
2273 /* Allocate more MRs (2x) than hardware responder_resources */
2274 for (i = 0; i < info->responder_resources * 2; i++) {
2275 smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
2276 if (!smbdirect_mr)
2277 goto out;
2278 smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
2279 info->max_frmr_depth);
2280 if (IS_ERR(smbdirect_mr->mr)) {
2281 log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x "
2282 "max_frmr_depth=%x\n",
2283 info->mr_type, info->max_frmr_depth);
2284 goto out;
2286 smbdirect_mr->sgl = kcalloc(
2287 info->max_frmr_depth,
2288 sizeof(struct scatterlist),
2289 GFP_KERNEL);
2290 if (!smbdirect_mr->sgl) {
2291 log_rdma_mr(ERR, "failed to allocate sgl\n");
2292 ib_dereg_mr(smbdirect_mr->mr);
2293 goto out;
2295 smbdirect_mr->state = MR_READY;
2296 smbdirect_mr->conn = info;
2298 list_add_tail(&smbdirect_mr->list, &info->mr_list);
2299 atomic_inc(&info->mr_ready_count);
2301 INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
2302 return 0;
2304 out:
2305 kfree(smbdirect_mr);
2307 list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
2308 ib_dereg_mr(smbdirect_mr->mr);
2309 kfree(smbdirect_mr->sgl);
2310 kfree(smbdirect_mr);
2312 return -ENOMEM;
2316 * Get a MR from mr_list. This function waits until there is at least one
2317 * MR available in the list. It may access the list while the
2318 * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
2319 * as they never modify the same places. However, there may be several CPUs
2320 * issueing I/O trying to get MR at the same time, mr_list_lock is used to
2321 * protect this situation.
2323 static struct smbd_mr *get_mr(struct smbd_connection *info)
2325 struct smbd_mr *ret;
2326 int rc;
2327 again:
2328 rc = wait_event_interruptible(info->wait_mr,
2329 atomic_read(&info->mr_ready_count) ||
2330 info->transport_status != SMBD_CONNECTED);
2331 if (rc) {
2332 log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
2333 return NULL;
2336 if (info->transport_status != SMBD_CONNECTED) {
2337 log_rdma_mr(ERR, "info->transport_status=%x\n",
2338 info->transport_status);
2339 return NULL;
2342 spin_lock(&info->mr_list_lock);
2343 list_for_each_entry(ret, &info->mr_list, list) {
2344 if (ret->state == MR_READY) {
2345 ret->state = MR_REGISTERED;
2346 spin_unlock(&info->mr_list_lock);
2347 atomic_dec(&info->mr_ready_count);
2348 atomic_inc(&info->mr_used_count);
2349 return ret;
2353 spin_unlock(&info->mr_list_lock);
2355 * It is possible that we could fail to get MR because other processes may
2356 * try to acquire a MR at the same time. If this is the case, retry it.
2358 goto again;
2362 * Register memory for RDMA read/write
2363 * pages[]: the list of pages to register memory with
2364 * num_pages: the number of pages to register
2365 * tailsz: if non-zero, the bytes to register in the last page
2366 * writing: true if this is a RDMA write (SMB read), false for RDMA read
2367 * need_invalidate: true if this MR needs to be locally invalidated after I/O
2368 * return value: the MR registered, NULL if failed.
2370 struct smbd_mr *smbd_register_mr(
2371 struct smbd_connection *info, struct page *pages[], int num_pages,
2372 int offset, int tailsz, bool writing, bool need_invalidate)
2374 struct smbd_mr *smbdirect_mr;
2375 int rc, i;
2376 enum dma_data_direction dir;
2377 struct ib_reg_wr *reg_wr;
2379 if (num_pages > info->max_frmr_depth) {
2380 log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
2381 num_pages, info->max_frmr_depth);
2382 return NULL;
2385 smbdirect_mr = get_mr(info);
2386 if (!smbdirect_mr) {
2387 log_rdma_mr(ERR, "get_mr returning NULL\n");
2388 return NULL;
2390 smbdirect_mr->need_invalidate = need_invalidate;
2391 smbdirect_mr->sgl_count = num_pages;
2392 sg_init_table(smbdirect_mr->sgl, num_pages);
2394 log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
2395 num_pages, offset, tailsz);
2397 if (num_pages == 1) {
2398 sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
2399 goto skip_multiple_pages;
2402 /* We have at least two pages to register */
2403 sg_set_page(
2404 &smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
2405 i = 1;
2406 while (i < num_pages - 1) {
2407 sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
2408 i++;
2410 sg_set_page(&smbdirect_mr->sgl[i], pages[i],
2411 tailsz ? tailsz : PAGE_SIZE, 0);
2413 skip_multiple_pages:
2414 dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2415 smbdirect_mr->dir = dir;
2416 rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
2417 if (!rc) {
2418 log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
2419 num_pages, dir, rc);
2420 goto dma_map_error;
2423 rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
2424 NULL, PAGE_SIZE);
2425 if (rc != num_pages) {
2426 log_rdma_mr(ERR,
2427 "ib_map_mr_sg failed rc = %d num_pages = %x\n",
2428 rc, num_pages);
2429 goto map_mr_error;
2432 ib_update_fast_reg_key(smbdirect_mr->mr,
2433 ib_inc_rkey(smbdirect_mr->mr->rkey));
2434 reg_wr = &smbdirect_mr->wr;
2435 reg_wr->wr.opcode = IB_WR_REG_MR;
2436 smbdirect_mr->cqe.done = register_mr_done;
2437 reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
2438 reg_wr->wr.num_sge = 0;
2439 reg_wr->wr.send_flags = IB_SEND_SIGNALED;
2440 reg_wr->mr = smbdirect_mr->mr;
2441 reg_wr->key = smbdirect_mr->mr->rkey;
2442 reg_wr->access = writing ?
2443 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2444 IB_ACCESS_REMOTE_READ;
2447 * There is no need for waiting for complemtion on ib_post_send
2448 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
2449 * on the next ib_post_send when we actaully send I/O to remote peer
2451 rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
2452 if (!rc)
2453 return smbdirect_mr;
2455 log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
2456 rc, reg_wr->key);
2458 /* If all failed, attempt to recover this MR by setting it MR_ERROR*/
2459 map_mr_error:
2460 ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
2461 smbdirect_mr->sgl_count, smbdirect_mr->dir);
2463 dma_map_error:
2464 smbdirect_mr->state = MR_ERROR;
2465 if (atomic_dec_and_test(&info->mr_used_count))
2466 wake_up(&info->wait_for_mr_cleanup);
2468 smbd_disconnect_rdma_connection(info);
2470 return NULL;
2473 static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
2475 struct smbd_mr *smbdirect_mr;
2476 struct ib_cqe *cqe;
2478 cqe = wc->wr_cqe;
2479 smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
2480 smbdirect_mr->state = MR_INVALIDATED;
2481 if (wc->status != IB_WC_SUCCESS) {
2482 log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
2483 smbdirect_mr->state = MR_ERROR;
2485 complete(&smbdirect_mr->invalidate_done);
2489 * Deregister a MR after I/O is done
2490 * This function may wait if remote invalidation is not used
2491 * and we have to locally invalidate the buffer to prevent data is being
2492 * modified by remote peer after upper layer consumes it
2494 int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
2496 struct ib_send_wr *wr;
2497 struct smbd_connection *info = smbdirect_mr->conn;
2498 int rc = 0;
2500 if (smbdirect_mr->need_invalidate) {
2501 /* Need to finish local invalidation before returning */
2502 wr = &smbdirect_mr->inv_wr;
2503 wr->opcode = IB_WR_LOCAL_INV;
2504 smbdirect_mr->cqe.done = local_inv_done;
2505 wr->wr_cqe = &smbdirect_mr->cqe;
2506 wr->num_sge = 0;
2507 wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
2508 wr->send_flags = IB_SEND_SIGNALED;
2510 init_completion(&smbdirect_mr->invalidate_done);
2511 rc = ib_post_send(info->id->qp, wr, NULL);
2512 if (rc) {
2513 log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
2514 smbd_disconnect_rdma_connection(info);
2515 goto done;
2517 wait_for_completion(&smbdirect_mr->invalidate_done);
2518 smbdirect_mr->need_invalidate = false;
2519 } else
2521 * For remote invalidation, just set it to MR_INVALIDATED
2522 * and defer to mr_recovery_work to recover the MR for next use
2524 smbdirect_mr->state = MR_INVALIDATED;
2526 if (smbdirect_mr->state == MR_INVALIDATED) {
2527 ib_dma_unmap_sg(
2528 info->id->device, smbdirect_mr->sgl,
2529 smbdirect_mr->sgl_count,
2530 smbdirect_mr->dir);
2531 smbdirect_mr->state = MR_READY;
2532 if (atomic_inc_return(&info->mr_ready_count) == 1)
2533 wake_up_interruptible(&info->wait_mr);
2534 } else
2536 * Schedule the work to do MR recovery for future I/Os MR
2537 * recovery is slow and don't want it to block current I/O
2539 queue_work(info->workqueue, &info->mr_recovery_work);
2541 done:
2542 if (atomic_dec_and_test(&info->mr_used_count))
2543 wake_up(&info->wait_for_mr_cleanup);
2545 return rc;