1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017, Microsoft Corporation.
5 * Author(s): Long Li <longli@microsoft.com>
7 #include <linux/module.h>
8 #include <linux/highmem.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 */
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
114 #define LOG_OUTGOING 0x1
115 #define LOG_INCOMING 0x2
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");
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...) \
137 if (level <= smbd_logging_level || class & smbd_logging_class) \
138 cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
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:
187 complete(&info->ri_done);
190 case RDMA_CM_EVENT_ADDR_ERROR:
191 info->ri_rc = -EHOSTUNREACH;
192 complete(&info->ri_done);
195 case RDMA_CM_EVENT_ROUTE_ERROR:
196 info->ri_rc = -ENETUNREACH;
197 complete(&info->ri_done);
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);
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);
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);
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);
236 /* Upcall from RDMA QP */
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);
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)
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,
287 if (atomic_dec_and_test(&request->info->send_pending))
288 wake_up(&request->info->wait_send_pending);
291 mempool_free(request, request->info->request_mempool);
294 static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
296 log_rdma_event(INFO, "resp message min_version %u max_version %u "
297 "negotiated_version %u credits_requested %u "
298 "credits_granted %u status %u max_readwrite_size %u "
299 "preferred_send_size %u max_receive_size %u "
300 "max_fragmented_size %u\n",
301 resp->min_version, resp->max_version, resp->negotiated_version,
302 resp->credits_requested, resp->credits_granted, resp->status,
303 resp->max_readwrite_size, resp->preferred_send_size,
304 resp->max_receive_size, resp->max_fragmented_size);
308 * Process a negotiation response message, according to [MS-SMBD]3.1.5.7
309 * response, packet_length: the negotiation response message
310 * return value: true if negotiation is a success, false if failed
312 static bool process_negotiation_response(
313 struct smbd_response *response, int packet_length)
315 struct smbd_connection *info = response->info;
316 struct smbd_negotiate_resp *packet = smbd_response_payload(response);
318 if (packet_length < sizeof(struct smbd_negotiate_resp)) {
320 "error: packet_length=%d\n", packet_length);
324 if (le16_to_cpu(packet->negotiated_version) != SMBD_V1) {
325 log_rdma_event(ERR, "error: negotiated_version=%x\n",
326 le16_to_cpu(packet->negotiated_version));
329 info->protocol = le16_to_cpu(packet->negotiated_version);
331 if (packet->credits_requested == 0) {
332 log_rdma_event(ERR, "error: credits_requested==0\n");
335 info->receive_credit_target = le16_to_cpu(packet->credits_requested);
337 if (packet->credits_granted == 0) {
338 log_rdma_event(ERR, "error: credits_granted==0\n");
341 atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
343 atomic_set(&info->receive_credits, 0);
345 if (le32_to_cpu(packet->preferred_send_size) > info->max_receive_size) {
346 log_rdma_event(ERR, "error: preferred_send_size=%d\n",
347 le32_to_cpu(packet->preferred_send_size));
350 info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
352 if (le32_to_cpu(packet->max_receive_size) < SMBD_MIN_RECEIVE_SIZE) {
353 log_rdma_event(ERR, "error: max_receive_size=%d\n",
354 le32_to_cpu(packet->max_receive_size));
357 info->max_send_size = min_t(int, info->max_send_size,
358 le32_to_cpu(packet->max_receive_size));
360 if (le32_to_cpu(packet->max_fragmented_size) <
361 SMBD_MIN_FRAGMENTED_SIZE) {
362 log_rdma_event(ERR, "error: max_fragmented_size=%d\n",
363 le32_to_cpu(packet->max_fragmented_size));
366 info->max_fragmented_send_size =
367 le32_to_cpu(packet->max_fragmented_size);
368 info->rdma_readwrite_threshold =
369 rdma_readwrite_threshold > info->max_fragmented_send_size ?
370 info->max_fragmented_send_size :
371 rdma_readwrite_threshold;
374 info->max_readwrite_size = min_t(u32,
375 le32_to_cpu(packet->max_readwrite_size),
376 info->max_frmr_depth * PAGE_SIZE);
377 info->max_frmr_depth = info->max_readwrite_size / PAGE_SIZE;
383 * Check and schedule to send an immediate packet
384 * This is used to extend credtis to remote peer to keep the transport busy
386 static void check_and_send_immediate(struct smbd_connection *info)
388 if (info->transport_status != SMBD_CONNECTED)
391 info->send_immediate = true;
394 * Promptly send a packet if our peer is running low on receive
397 if (atomic_read(&info->receive_credits) <
398 info->receive_credit_target - 1)
400 info->workqueue, &info->send_immediate_work, 0);
403 static void smbd_post_send_credits(struct work_struct *work)
406 int use_receive_queue = 1;
408 struct smbd_response *response;
409 struct smbd_connection *info =
410 container_of(work, struct smbd_connection,
411 post_send_credits_work);
413 if (info->transport_status != SMBD_CONNECTED) {
414 wake_up(&info->wait_receive_queues);
418 if (info->receive_credit_target >
419 atomic_read(&info->receive_credits)) {
421 if (use_receive_queue)
422 response = get_receive_buffer(info);
424 response = get_empty_queue_buffer(info);
426 /* now switch to emtpy packet queue */
427 if (use_receive_queue) {
428 use_receive_queue = 0;
434 response->type = SMBD_TRANSFER_DATA;
435 response->first_segment = false;
436 rc = smbd_post_recv(info, response);
439 "post_recv failed rc=%d\n", rc);
440 put_receive_buffer(info, response);
448 spin_lock(&info->lock_new_credits_offered);
449 info->new_credits_offered += ret;
450 spin_unlock(&info->lock_new_credits_offered);
452 atomic_add(ret, &info->receive_credits);
454 /* Check if we can post new receive and grant credits to peer */
455 check_and_send_immediate(info);
458 /* Called from softirq, when recv is done */
459 static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
461 struct smbd_data_transfer *data_transfer;
462 struct smbd_response *response =
463 container_of(wc->wr_cqe, struct smbd_response, cqe);
464 struct smbd_connection *info = response->info;
467 log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d "
468 "byte_len=%d pkey_index=%x\n",
469 response, response->type, wc->status, wc->opcode,
470 wc->byte_len, wc->pkey_index);
472 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
473 log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
474 wc->status, wc->opcode);
475 smbd_disconnect_rdma_connection(info);
479 ib_dma_sync_single_for_cpu(
482 response->sge.length,
485 switch (response->type) {
486 /* SMBD negotiation response */
487 case SMBD_NEGOTIATE_RESP:
488 dump_smbd_negotiate_resp(smbd_response_payload(response));
489 info->full_packet_received = true;
490 info->negotiate_done =
491 process_negotiation_response(response, wc->byte_len);
492 complete(&info->negotiate_completion);
495 /* SMBD data transfer packet */
496 case SMBD_TRANSFER_DATA:
497 data_transfer = smbd_response_payload(response);
498 data_length = le32_to_cpu(data_transfer->data_length);
501 * If this is a packet with data playload place the data in
502 * reassembly queue and wake up the reading thread
505 if (info->full_packet_received)
506 response->first_segment = true;
508 if (le32_to_cpu(data_transfer->remaining_data_length))
509 info->full_packet_received = false;
511 info->full_packet_received = true;
518 put_empty_packet(info, response);
521 wake_up_interruptible(&info->wait_reassembly_queue);
523 atomic_dec(&info->receive_credits);
524 info->receive_credit_target =
525 le16_to_cpu(data_transfer->credits_requested);
526 if (le16_to_cpu(data_transfer->credits_granted)) {
527 atomic_add(le16_to_cpu(data_transfer->credits_granted),
528 &info->send_credits);
530 * We have new send credits granted from remote peer
531 * If any sender is waiting for credits, unblock it
533 wake_up_interruptible(&info->wait_send_queue);
536 log_incoming(INFO, "data flags %d data_offset %d "
537 "data_length %d remaining_data_length %d\n",
538 le16_to_cpu(data_transfer->flags),
539 le32_to_cpu(data_transfer->data_offset),
540 le32_to_cpu(data_transfer->data_length),
541 le32_to_cpu(data_transfer->remaining_data_length));
543 /* Send a KEEP_ALIVE response right away if requested */
544 info->keep_alive_requested = KEEP_ALIVE_NONE;
545 if (le16_to_cpu(data_transfer->flags) &
546 SMB_DIRECT_RESPONSE_REQUESTED) {
547 info->keep_alive_requested = KEEP_ALIVE_PENDING;
551 * Check if we need to send something to remote peer to
552 * grant more credits or respond to KEEP_ALIVE packet
554 check_and_send_immediate(info);
560 "unexpected response type=%d\n", response->type);
564 put_receive_buffer(info, response);
567 static struct rdma_cm_id *smbd_create_id(
568 struct smbd_connection *info,
569 struct sockaddr *dstaddr, int port)
571 struct rdma_cm_id *id;
575 id = rdma_create_id(&init_net, smbd_conn_upcall, info,
576 RDMA_PS_TCP, IB_QPT_RC);
579 log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
583 if (dstaddr->sa_family == AF_INET6)
584 sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
586 sport = &((struct sockaddr_in *)dstaddr)->sin_port;
588 *sport = htons(port);
590 init_completion(&info->ri_done);
591 info->ri_rc = -ETIMEDOUT;
593 rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
594 RDMA_RESOLVE_TIMEOUT);
596 log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
599 wait_for_completion_interruptible_timeout(
600 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
603 log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
607 info->ri_rc = -ETIMEDOUT;
608 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
610 log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
613 wait_for_completion_interruptible_timeout(
614 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
617 log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
629 * Test if FRWR (Fast Registration Work Requests) is supported on the device
630 * This implementation requries FRWR on RDMA read/write
631 * return value: true if it is supported
633 static bool frwr_is_supported(struct ib_device_attr *attrs)
635 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
637 if (attrs->max_fast_reg_page_list_len == 0)
642 static int smbd_ia_open(
643 struct smbd_connection *info,
644 struct sockaddr *dstaddr, int port)
648 info->id = smbd_create_id(info, dstaddr, port);
649 if (IS_ERR(info->id)) {
650 rc = PTR_ERR(info->id);
654 if (!frwr_is_supported(&info->id->device->attrs)) {
656 "Fast Registration Work Requests "
657 "(FRWR) is not supported\n");
659 "Device capability flags = %llx "
660 "max_fast_reg_page_list_len = %u\n",
661 info->id->device->attrs.device_cap_flags,
662 info->id->device->attrs.max_fast_reg_page_list_len);
663 rc = -EPROTONOSUPPORT;
666 info->max_frmr_depth = min_t(int,
668 info->id->device->attrs.max_fast_reg_page_list_len);
669 info->mr_type = IB_MR_TYPE_MEM_REG;
670 if (info->id->device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
671 info->mr_type = IB_MR_TYPE_SG_GAPS;
673 info->pd = ib_alloc_pd(info->id->device, 0);
674 if (IS_ERR(info->pd)) {
675 rc = PTR_ERR(info->pd);
676 log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
683 rdma_destroy_id(info->id);
691 * Send a negotiation request message to the peer
692 * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
693 * After negotiation, the transport is connected and ready for
694 * carrying upper layer SMB payload
696 static int smbd_post_send_negotiate_req(struct smbd_connection *info)
698 struct ib_send_wr send_wr;
700 struct smbd_request *request;
701 struct smbd_negotiate_req *packet;
703 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
707 request->info = info;
709 packet = smbd_request_payload(request);
710 packet->min_version = cpu_to_le16(SMBD_V1);
711 packet->max_version = cpu_to_le16(SMBD_V1);
712 packet->reserved = 0;
713 packet->credits_requested = cpu_to_le16(info->send_credit_target);
714 packet->preferred_send_size = cpu_to_le32(info->max_send_size);
715 packet->max_receive_size = cpu_to_le32(info->max_receive_size);
716 packet->max_fragmented_size =
717 cpu_to_le32(info->max_fragmented_recv_size);
719 request->num_sge = 1;
720 request->sge[0].addr = ib_dma_map_single(
721 info->id->device, (void *)packet,
722 sizeof(*packet), DMA_TO_DEVICE);
723 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
725 goto dma_mapping_failed;
728 request->sge[0].length = sizeof(*packet);
729 request->sge[0].lkey = info->pd->local_dma_lkey;
731 ib_dma_sync_single_for_device(
732 info->id->device, request->sge[0].addr,
733 request->sge[0].length, DMA_TO_DEVICE);
735 request->cqe.done = send_done;
738 send_wr.wr_cqe = &request->cqe;
739 send_wr.sg_list = request->sge;
740 send_wr.num_sge = request->num_sge;
741 send_wr.opcode = IB_WR_SEND;
742 send_wr.send_flags = IB_SEND_SIGNALED;
744 log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
745 request->sge[0].addr,
746 request->sge[0].length, request->sge[0].lkey);
748 atomic_inc(&info->send_pending);
749 rc = ib_post_send(info->id->qp, &send_wr, NULL);
753 /* if we reach here, post send failed */
754 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
755 atomic_dec(&info->send_pending);
756 ib_dma_unmap_single(info->id->device, request->sge[0].addr,
757 request->sge[0].length, DMA_TO_DEVICE);
759 smbd_disconnect_rdma_connection(info);
762 mempool_free(request, info->request_mempool);
767 * Extend the credits to remote peer
768 * This implements [MS-SMBD] 3.1.5.9
769 * The idea is that we should extend credits to remote peer as quickly as
770 * it's allowed, to maintain data flow. We allocate as much receive
771 * buffer as possible, and extend the receive credits to remote peer
772 * return value: the new credtis being granted.
774 static int manage_credits_prior_sending(struct smbd_connection *info)
778 spin_lock(&info->lock_new_credits_offered);
779 new_credits = info->new_credits_offered;
780 info->new_credits_offered = 0;
781 spin_unlock(&info->lock_new_credits_offered);
787 * Check if we need to send a KEEP_ALIVE message
788 * The idle connection timer triggers a KEEP_ALIVE message when expires
789 * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
792 * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
795 static int manage_keep_alive_before_sending(struct smbd_connection *info)
797 if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
798 info->keep_alive_requested = KEEP_ALIVE_SENT;
805 * Build and prepare the SMBD packet header
806 * This function waits for avaialbe send credits and build a SMBD packet
807 * header. The caller then optional append payload to the packet after
810 * size: the size of the payload
811 * remaining_data_length: remaining data to send if this is part of a
814 * request_out: the request allocated from this function
815 * return values: 0 on success, otherwise actual error code returned
817 static int smbd_create_header(struct smbd_connection *info,
818 int size, int remaining_data_length,
819 struct smbd_request **request_out)
821 struct smbd_request *request;
822 struct smbd_data_transfer *packet;
826 /* Wait for send credits. A SMBD packet needs one credit */
827 rc = wait_event_interruptible(info->wait_send_queue,
828 atomic_read(&info->send_credits) > 0 ||
829 info->transport_status != SMBD_CONNECTED);
833 if (info->transport_status != SMBD_CONNECTED) {
834 log_outgoing(ERR, "disconnected not sending\n");
837 atomic_dec(&info->send_credits);
839 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
845 request->info = info;
847 /* Fill in the packet header */
848 packet = smbd_request_payload(request);
849 packet->credits_requested = cpu_to_le16(info->send_credit_target);
850 packet->credits_granted =
851 cpu_to_le16(manage_credits_prior_sending(info));
852 info->send_immediate = false;
855 if (manage_keep_alive_before_sending(info))
856 packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
858 packet->reserved = 0;
860 packet->data_offset = 0;
862 packet->data_offset = cpu_to_le32(24);
863 packet->data_length = cpu_to_le32(size);
864 packet->remaining_data_length = cpu_to_le32(remaining_data_length);
867 log_outgoing(INFO, "credits_requested=%d credits_granted=%d "
868 "data_offset=%d data_length=%d remaining_data_length=%d\n",
869 le16_to_cpu(packet->credits_requested),
870 le16_to_cpu(packet->credits_granted),
871 le32_to_cpu(packet->data_offset),
872 le32_to_cpu(packet->data_length),
873 le32_to_cpu(packet->remaining_data_length));
875 /* Map the packet to DMA */
876 header_length = sizeof(struct smbd_data_transfer);
877 /* If this is a packet without payload, don't send padding */
879 header_length = offsetof(struct smbd_data_transfer, padding);
881 request->num_sge = 1;
882 request->sge[0].addr = ib_dma_map_single(info->id->device,
886 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
887 mempool_free(request, info->request_mempool);
892 request->sge[0].length = header_length;
893 request->sge[0].lkey = info->pd->local_dma_lkey;
895 *request_out = request;
899 atomic_inc(&info->send_credits);
903 static void smbd_destroy_header(struct smbd_connection *info,
904 struct smbd_request *request)
907 ib_dma_unmap_single(info->id->device,
908 request->sge[0].addr,
909 request->sge[0].length,
911 mempool_free(request, info->request_mempool);
912 atomic_inc(&info->send_credits);
915 /* Post the send request */
916 static int smbd_post_send(struct smbd_connection *info,
917 struct smbd_request *request)
919 struct ib_send_wr send_wr;
922 for (i = 0; i < request->num_sge; i++) {
924 "rdma_request sge[%d] addr=%llu length=%u\n",
925 i, request->sge[i].addr, request->sge[i].length);
926 ib_dma_sync_single_for_device(
928 request->sge[i].addr,
929 request->sge[i].length,
933 request->cqe.done = send_done;
936 send_wr.wr_cqe = &request->cqe;
937 send_wr.sg_list = request->sge;
938 send_wr.num_sge = request->num_sge;
939 send_wr.opcode = IB_WR_SEND;
940 send_wr.send_flags = IB_SEND_SIGNALED;
942 atomic_inc(&info->send_pending);
944 rc = ib_post_send(info->id->qp, &send_wr, NULL);
946 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
947 if (atomic_dec_and_test(&info->send_pending))
948 wake_up(&info->wait_send_pending);
949 smbd_disconnect_rdma_connection(info);
952 /* Reset timer for idle connection after packet is sent */
953 mod_delayed_work(info->workqueue, &info->idle_timer_work,
954 info->keep_alive_interval*HZ);
959 static int smbd_post_send_sgl(struct smbd_connection *info,
960 struct scatterlist *sgl, int data_length, int remaining_data_length)
964 struct smbd_request *request;
965 struct scatterlist *sg;
967 rc = smbd_create_header(
968 info, data_length, remaining_data_length, &request);
972 num_sgs = sgl ? sg_nents(sgl) : 0;
973 for_each_sg(sgl, sg, num_sgs, i) {
974 request->sge[i+1].addr =
975 ib_dma_map_page(info->id->device, sg_page(sg),
976 sg->offset, sg->length, DMA_TO_DEVICE);
977 if (ib_dma_mapping_error(
978 info->id->device, request->sge[i+1].addr)) {
980 request->sge[i+1].addr = 0;
981 goto dma_mapping_failure;
983 request->sge[i+1].length = sg->length;
984 request->sge[i+1].lkey = info->pd->local_dma_lkey;
988 rc = smbd_post_send(info, request);
993 for (i = 1; i < request->num_sge; i++)
994 if (request->sge[i].addr)
995 ib_dma_unmap_single(info->id->device,
996 request->sge[i].addr,
997 request->sge[i].length,
999 smbd_destroy_header(info, request);
1005 * page: the page to send
1006 * offset: offset in the page to send
1007 * size: length in the page to send
1008 * remaining_data_length: remaining data to send in this payload
1010 static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
1011 unsigned long offset, size_t size, int remaining_data_length)
1013 struct scatterlist sgl;
1015 sg_init_table(&sgl, 1);
1016 sg_set_page(&sgl, page, size, offset);
1018 return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
1022 * Send an empty message
1023 * Empty message is used to extend credits to peer to for keep live
1024 * while there is no upper layer payload to send at the time
1026 static int smbd_post_send_empty(struct smbd_connection *info)
1028 info->count_send_empty++;
1029 return smbd_post_send_sgl(info, NULL, 0, 0);
1033 * Send a data buffer
1034 * iov: the iov array describing the data buffers
1035 * n_vec: number of iov array
1036 * remaining_data_length: remaining data to send following this packet
1037 * in segmented SMBD packet
1039 static int smbd_post_send_data(
1040 struct smbd_connection *info, struct kvec *iov, int n_vec,
1041 int remaining_data_length)
1044 u32 data_length = 0;
1045 struct scatterlist sgl[SMBDIRECT_MAX_SGE];
1047 if (n_vec > SMBDIRECT_MAX_SGE) {
1048 cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
1052 sg_init_table(sgl, n_vec);
1053 for (i = 0; i < n_vec; i++) {
1054 data_length += iov[i].iov_len;
1055 sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
1058 return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
1062 * Post a receive request to the transport
1063 * The remote peer can only send data when a receive request is posted
1064 * The interaction is controlled by send/receive credit system
1066 static int smbd_post_recv(
1067 struct smbd_connection *info, struct smbd_response *response)
1069 struct ib_recv_wr recv_wr;
1072 response->sge.addr = ib_dma_map_single(
1073 info->id->device, response->packet,
1074 info->max_receive_size, DMA_FROM_DEVICE);
1075 if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1078 response->sge.length = info->max_receive_size;
1079 response->sge.lkey = info->pd->local_dma_lkey;
1081 response->cqe.done = recv_done;
1083 recv_wr.wr_cqe = &response->cqe;
1084 recv_wr.next = NULL;
1085 recv_wr.sg_list = &response->sge;
1086 recv_wr.num_sge = 1;
1088 rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
1090 ib_dma_unmap_single(info->id->device, response->sge.addr,
1091 response->sge.length, DMA_FROM_DEVICE);
1092 smbd_disconnect_rdma_connection(info);
1093 log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1099 /* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1100 static int smbd_negotiate(struct smbd_connection *info)
1103 struct smbd_response *response = get_receive_buffer(info);
1105 response->type = SMBD_NEGOTIATE_RESP;
1106 rc = smbd_post_recv(info, response);
1107 log_rdma_event(INFO,
1108 "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x "
1110 rc, response->sge.addr,
1111 response->sge.length, response->sge.lkey);
1115 init_completion(&info->negotiate_completion);
1116 info->negotiate_done = false;
1117 rc = smbd_post_send_negotiate_req(info);
1121 rc = wait_for_completion_interruptible_timeout(
1122 &info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1123 log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1125 if (info->negotiate_done)
1130 else if (rc == -ERESTARTSYS)
1138 static void put_empty_packet(
1139 struct smbd_connection *info, struct smbd_response *response)
1141 spin_lock(&info->empty_packet_queue_lock);
1142 list_add_tail(&response->list, &info->empty_packet_queue);
1143 info->count_empty_packet_queue++;
1144 spin_unlock(&info->empty_packet_queue_lock);
1146 queue_work(info->workqueue, &info->post_send_credits_work);
1150 * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1151 * This is a queue for reassembling upper layer payload and present to upper
1152 * layer. All the inncoming payload go to the reassembly queue, regardless of
1153 * if reassembly is required. The uuper layer code reads from the queue for all
1154 * incoming payloads.
1155 * Put a received packet to the reassembly queue
1156 * response: the packet received
1157 * data_length: the size of payload in this packet
1159 static void enqueue_reassembly(
1160 struct smbd_connection *info,
1161 struct smbd_response *response,
1164 spin_lock(&info->reassembly_queue_lock);
1165 list_add_tail(&response->list, &info->reassembly_queue);
1166 info->reassembly_queue_length++;
1168 * Make sure reassembly_data_length is updated after list and
1169 * reassembly_queue_length are updated. On the dequeue side
1170 * reassembly_data_length is checked without a lock to determine
1171 * if reassembly_queue_length and list is up to date
1174 info->reassembly_data_length += data_length;
1175 spin_unlock(&info->reassembly_queue_lock);
1176 info->count_reassembly_queue++;
1177 info->count_enqueue_reassembly_queue++;
1181 * Get the first entry at the front of reassembly queue
1182 * Caller is responsible for locking
1183 * return value: the first entry if any, NULL if queue is empty
1185 static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1187 struct smbd_response *ret = NULL;
1189 if (!list_empty(&info->reassembly_queue)) {
1190 ret = list_first_entry(
1191 &info->reassembly_queue,
1192 struct smbd_response, list);
1197 static struct smbd_response *get_empty_queue_buffer(
1198 struct smbd_connection *info)
1200 struct smbd_response *ret = NULL;
1201 unsigned long flags;
1203 spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1204 if (!list_empty(&info->empty_packet_queue)) {
1205 ret = list_first_entry(
1206 &info->empty_packet_queue,
1207 struct smbd_response, list);
1208 list_del(&ret->list);
1209 info->count_empty_packet_queue--;
1211 spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1217 * Get a receive buffer
1218 * For each remote send, we need to post a receive. The receive buffers are
1219 * pre-allocated in advance.
1220 * return value: the receive buffer, NULL if none is available
1222 static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1224 struct smbd_response *ret = NULL;
1225 unsigned long flags;
1227 spin_lock_irqsave(&info->receive_queue_lock, flags);
1228 if (!list_empty(&info->receive_queue)) {
1229 ret = list_first_entry(
1230 &info->receive_queue,
1231 struct smbd_response, list);
1232 list_del(&ret->list);
1233 info->count_receive_queue--;
1234 info->count_get_receive_buffer++;
1236 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1242 * Return a receive buffer
1243 * Upon returning of a receive buffer, we can post new receive and extend
1244 * more receive credits to remote peer. This is done immediately after a
1245 * receive buffer is returned.
1247 static void put_receive_buffer(
1248 struct smbd_connection *info, struct smbd_response *response)
1250 unsigned long flags;
1252 ib_dma_unmap_single(info->id->device, response->sge.addr,
1253 response->sge.length, DMA_FROM_DEVICE);
1255 spin_lock_irqsave(&info->receive_queue_lock, flags);
1256 list_add_tail(&response->list, &info->receive_queue);
1257 info->count_receive_queue++;
1258 info->count_put_receive_buffer++;
1259 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1261 queue_work(info->workqueue, &info->post_send_credits_work);
1264 /* Preallocate all receive buffer on transport establishment */
1265 static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1268 struct smbd_response *response;
1270 INIT_LIST_HEAD(&info->reassembly_queue);
1271 spin_lock_init(&info->reassembly_queue_lock);
1272 info->reassembly_data_length = 0;
1273 info->reassembly_queue_length = 0;
1275 INIT_LIST_HEAD(&info->receive_queue);
1276 spin_lock_init(&info->receive_queue_lock);
1277 info->count_receive_queue = 0;
1279 INIT_LIST_HEAD(&info->empty_packet_queue);
1280 spin_lock_init(&info->empty_packet_queue_lock);
1281 info->count_empty_packet_queue = 0;
1283 init_waitqueue_head(&info->wait_receive_queues);
1285 for (i = 0; i < num_buf; i++) {
1286 response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1288 goto allocate_failed;
1290 response->info = info;
1291 list_add_tail(&response->list, &info->receive_queue);
1292 info->count_receive_queue++;
1298 while (!list_empty(&info->receive_queue)) {
1299 response = list_first_entry(
1300 &info->receive_queue,
1301 struct smbd_response, list);
1302 list_del(&response->list);
1303 info->count_receive_queue--;
1305 mempool_free(response, info->response_mempool);
1310 static void destroy_receive_buffers(struct smbd_connection *info)
1312 struct smbd_response *response;
1314 while ((response = get_receive_buffer(info)))
1315 mempool_free(response, info->response_mempool);
1317 while ((response = get_empty_queue_buffer(info)))
1318 mempool_free(response, info->response_mempool);
1322 * Check and send an immediate or keep alive packet
1323 * The condition to send those packets are defined in [MS-SMBD] 3.1.1.1
1324 * Connection.KeepaliveRequested and Connection.SendImmediate
1325 * The idea is to extend credits to server as soon as it becomes available
1327 static void send_immediate_work(struct work_struct *work)
1329 struct smbd_connection *info = container_of(
1330 work, struct smbd_connection,
1331 send_immediate_work.work);
1333 if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
1334 info->send_immediate) {
1335 log_keep_alive(INFO, "send an empty message\n");
1336 smbd_post_send_empty(info);
1340 /* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1341 static void idle_connection_timer(struct work_struct *work)
1343 struct smbd_connection *info = container_of(
1344 work, struct smbd_connection,
1345 idle_timer_work.work);
1347 if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1349 "error status info->keep_alive_requested=%d\n",
1350 info->keep_alive_requested);
1351 smbd_disconnect_rdma_connection(info);
1355 log_keep_alive(INFO, "about to send an empty idle message\n");
1356 smbd_post_send_empty(info);
1358 /* Setup the next idle timeout work */
1359 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1360 info->keep_alive_interval*HZ);
1364 * Destroy the transport and related RDMA and memory resources
1365 * Need to go through all the pending counters and make sure on one is using
1366 * the transport while it is destroyed
1368 void smbd_destroy(struct TCP_Server_Info *server)
1370 struct smbd_connection *info = server->smbd_conn;
1371 struct smbd_response *response;
1372 unsigned long flags;
1375 log_rdma_event(INFO, "rdma session already destroyed\n");
1379 log_rdma_event(INFO, "destroying rdma session\n");
1380 if (info->transport_status != SMBD_DISCONNECTED) {
1381 rdma_disconnect(server->smbd_conn->id);
1382 log_rdma_event(INFO, "wait for transport being disconnected\n");
1383 wait_event_interruptible(
1385 info->transport_status == SMBD_DISCONNECTED);
1388 log_rdma_event(INFO, "destroying qp\n");
1389 ib_drain_qp(info->id->qp);
1390 rdma_destroy_qp(info->id);
1392 log_rdma_event(INFO, "cancelling idle timer\n");
1393 cancel_delayed_work_sync(&info->idle_timer_work);
1394 log_rdma_event(INFO, "cancelling send immediate work\n");
1395 cancel_delayed_work_sync(&info->send_immediate_work);
1397 log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
1398 wait_event(info->wait_send_pending,
1399 atomic_read(&info->send_pending) == 0);
1401 /* It's not posssible for upper layer to get to reassembly */
1402 log_rdma_event(INFO, "drain the reassembly queue\n");
1404 spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1405 response = _get_first_reassembly(info);
1407 list_del(&response->list);
1408 spin_unlock_irqrestore(
1409 &info->reassembly_queue_lock, flags);
1410 put_receive_buffer(info, response);
1412 spin_unlock_irqrestore(
1413 &info->reassembly_queue_lock, flags);
1415 info->reassembly_data_length = 0;
1417 log_rdma_event(INFO, "free receive buffers\n");
1418 wait_event(info->wait_receive_queues,
1419 info->count_receive_queue + info->count_empty_packet_queue
1420 == info->receive_credit_max);
1421 destroy_receive_buffers(info);
1424 * For performance reasons, memory registration and deregistration
1425 * are not locked by srv_mutex. It is possible some processes are
1426 * blocked on transport srv_mutex while holding memory registration.
1427 * Release the transport srv_mutex to allow them to hit the failure
1428 * path when sending data, and then release memory registartions.
1430 log_rdma_event(INFO, "freeing mr list\n");
1431 wake_up_interruptible_all(&info->wait_mr);
1432 while (atomic_read(&info->mr_used_count)) {
1433 mutex_unlock(&server->srv_mutex);
1435 mutex_lock(&server->srv_mutex);
1437 destroy_mr_list(info);
1439 ib_free_cq(info->send_cq);
1440 ib_free_cq(info->recv_cq);
1441 ib_dealloc_pd(info->pd);
1442 rdma_destroy_id(info->id);
1445 mempool_destroy(info->request_mempool);
1446 kmem_cache_destroy(info->request_cache);
1448 mempool_destroy(info->response_mempool);
1449 kmem_cache_destroy(info->response_cache);
1451 info->transport_status = SMBD_DESTROYED;
1453 destroy_workqueue(info->workqueue);
1454 log_rdma_event(INFO, "rdma session destroyed\n");
1459 * Reconnect this SMBD connection, called from upper layer
1460 * return value: 0 on success, or actual error code
1462 int smbd_reconnect(struct TCP_Server_Info *server)
1464 log_rdma_event(INFO, "reconnecting rdma session\n");
1466 if (!server->smbd_conn) {
1467 log_rdma_event(INFO, "rdma session already destroyed\n");
1472 * This is possible if transport is disconnected and we haven't received
1473 * notification from RDMA, but upper layer has detected timeout
1475 if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1476 log_rdma_event(INFO, "disconnecting transport\n");
1477 smbd_destroy(server);
1481 log_rdma_event(INFO, "creating rdma session\n");
1482 server->smbd_conn = smbd_get_connection(
1483 server, (struct sockaddr *) &server->dstaddr);
1485 if (server->smbd_conn)
1486 cifs_dbg(VFS, "RDMA transport re-established\n");
1488 return server->smbd_conn ? 0 : -ENOENT;
1491 static void destroy_caches_and_workqueue(struct smbd_connection *info)
1493 destroy_receive_buffers(info);
1494 destroy_workqueue(info->workqueue);
1495 mempool_destroy(info->response_mempool);
1496 kmem_cache_destroy(info->response_cache);
1497 mempool_destroy(info->request_mempool);
1498 kmem_cache_destroy(info->request_cache);
1501 #define MAX_NAME_LEN 80
1502 static int allocate_caches_and_workqueue(struct smbd_connection *info)
1504 char name[MAX_NAME_LEN];
1507 scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
1508 info->request_cache =
1511 sizeof(struct smbd_request) +
1512 sizeof(struct smbd_data_transfer),
1513 0, SLAB_HWCACHE_ALIGN, NULL);
1514 if (!info->request_cache)
1517 info->request_mempool =
1518 mempool_create(info->send_credit_target, mempool_alloc_slab,
1519 mempool_free_slab, info->request_cache);
1520 if (!info->request_mempool)
1523 scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
1524 info->response_cache =
1527 sizeof(struct smbd_response) +
1528 info->max_receive_size,
1529 0, SLAB_HWCACHE_ALIGN, NULL);
1530 if (!info->response_cache)
1533 info->response_mempool =
1534 mempool_create(info->receive_credit_max, mempool_alloc_slab,
1535 mempool_free_slab, info->response_cache);
1536 if (!info->response_mempool)
1539 scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
1540 info->workqueue = create_workqueue(name);
1541 if (!info->workqueue)
1544 rc = allocate_receive_buffers(info, info->receive_credit_max);
1546 log_rdma_event(ERR, "failed to allocate receive buffers\n");
1553 destroy_workqueue(info->workqueue);
1555 mempool_destroy(info->response_mempool);
1557 kmem_cache_destroy(info->response_cache);
1559 mempool_destroy(info->request_mempool);
1561 kmem_cache_destroy(info->request_cache);
1565 /* Create a SMBD connection, called by upper layer */
1566 static struct smbd_connection *_smbd_get_connection(
1567 struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1570 struct smbd_connection *info;
1571 struct rdma_conn_param conn_param;
1572 struct ib_qp_init_attr qp_attr;
1573 struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
1574 struct ib_port_immutable port_immutable;
1577 info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1581 info->transport_status = SMBD_CONNECTING;
1582 rc = smbd_ia_open(info, dstaddr, port);
1584 log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1585 goto create_id_failed;
1588 if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1589 smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1591 "consider lowering send_credit_target = %d. "
1592 "Possible CQE overrun, device "
1593 "reporting max_cpe %d max_qp_wr %d\n",
1594 smbd_send_credit_target,
1595 info->id->device->attrs.max_cqe,
1596 info->id->device->attrs.max_qp_wr);
1600 if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1601 smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1603 "consider lowering receive_credit_max = %d. "
1604 "Possible CQE overrun, device "
1605 "reporting max_cpe %d max_qp_wr %d\n",
1606 smbd_receive_credit_max,
1607 info->id->device->attrs.max_cqe,
1608 info->id->device->attrs.max_qp_wr);
1612 info->receive_credit_max = smbd_receive_credit_max;
1613 info->send_credit_target = smbd_send_credit_target;
1614 info->max_send_size = smbd_max_send_size;
1615 info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1616 info->max_receive_size = smbd_max_receive_size;
1617 info->keep_alive_interval = smbd_keep_alive_interval;
1619 if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
1621 "warning: device max_send_sge = %d too small\n",
1622 info->id->device->attrs.max_send_sge);
1623 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1625 if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
1627 "warning: device max_recv_sge = %d too small\n",
1628 info->id->device->attrs.max_recv_sge);
1629 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1632 info->send_cq = NULL;
1633 info->recv_cq = NULL;
1635 ib_alloc_cq_any(info->id->device, info,
1636 info->send_credit_target, IB_POLL_SOFTIRQ);
1637 if (IS_ERR(info->send_cq)) {
1638 info->send_cq = NULL;
1639 goto alloc_cq_failed;
1643 ib_alloc_cq_any(info->id->device, info,
1644 info->receive_credit_max, IB_POLL_SOFTIRQ);
1645 if (IS_ERR(info->recv_cq)) {
1646 info->recv_cq = NULL;
1647 goto alloc_cq_failed;
1650 memset(&qp_attr, 0, sizeof(qp_attr));
1651 qp_attr.event_handler = smbd_qp_async_error_upcall;
1652 qp_attr.qp_context = info;
1653 qp_attr.cap.max_send_wr = info->send_credit_target;
1654 qp_attr.cap.max_recv_wr = info->receive_credit_max;
1655 qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
1656 qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
1657 qp_attr.cap.max_inline_data = 0;
1658 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1659 qp_attr.qp_type = IB_QPT_RC;
1660 qp_attr.send_cq = info->send_cq;
1661 qp_attr.recv_cq = info->recv_cq;
1662 qp_attr.port_num = ~0;
1664 rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1666 log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1667 goto create_qp_failed;
1670 memset(&conn_param, 0, sizeof(conn_param));
1671 conn_param.initiator_depth = 0;
1673 conn_param.responder_resources =
1674 info->id->device->attrs.max_qp_rd_atom
1675 < SMBD_CM_RESPONDER_RESOURCES ?
1676 info->id->device->attrs.max_qp_rd_atom :
1677 SMBD_CM_RESPONDER_RESOURCES;
1678 info->responder_resources = conn_param.responder_resources;
1679 log_rdma_mr(INFO, "responder_resources=%d\n",
1680 info->responder_resources);
1682 /* Need to send IRD/ORD in private data for iWARP */
1683 info->id->device->ops.get_port_immutable(
1684 info->id->device, info->id->port_num, &port_immutable);
1685 if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1686 ird_ord_hdr[0] = info->responder_resources;
1688 conn_param.private_data = ird_ord_hdr;
1689 conn_param.private_data_len = sizeof(ird_ord_hdr);
1691 conn_param.private_data = NULL;
1692 conn_param.private_data_len = 0;
1695 conn_param.retry_count = SMBD_CM_RETRY;
1696 conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1697 conn_param.flow_control = 0;
1699 log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1700 &addr_in->sin_addr, port);
1702 init_waitqueue_head(&info->conn_wait);
1703 init_waitqueue_head(&info->disconn_wait);
1704 init_waitqueue_head(&info->wait_reassembly_queue);
1705 rc = rdma_connect(info->id, &conn_param);
1707 log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1708 goto rdma_connect_failed;
1711 wait_event_interruptible(
1712 info->conn_wait, info->transport_status != SMBD_CONNECTING);
1714 if (info->transport_status != SMBD_CONNECTED) {
1715 log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1716 goto rdma_connect_failed;
1719 log_rdma_event(INFO, "rdma_connect connected\n");
1721 rc = allocate_caches_and_workqueue(info);
1723 log_rdma_event(ERR, "cache allocation failed\n");
1724 goto allocate_cache_failed;
1727 init_waitqueue_head(&info->wait_send_queue);
1728 INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
1729 INIT_DELAYED_WORK(&info->send_immediate_work, send_immediate_work);
1730 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1731 info->keep_alive_interval*HZ);
1733 init_waitqueue_head(&info->wait_send_pending);
1734 atomic_set(&info->send_pending, 0);
1737 INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
1738 INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1739 info->new_credits_offered = 0;
1740 spin_lock_init(&info->lock_new_credits_offered);
1742 rc = smbd_negotiate(info);
1744 log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1745 goto negotiation_failed;
1748 rc = allocate_mr_list(info);
1750 log_rdma_mr(ERR, "memory registration allocation failed\n");
1751 goto allocate_mr_failed;
1757 /* At this point, need to a full transport shutdown */
1758 smbd_destroy(server);
1762 cancel_delayed_work_sync(&info->idle_timer_work);
1763 destroy_caches_and_workqueue(info);
1764 info->transport_status = SMBD_NEGOTIATE_FAILED;
1765 init_waitqueue_head(&info->conn_wait);
1766 rdma_disconnect(info->id);
1767 wait_event(info->conn_wait,
1768 info->transport_status == SMBD_DISCONNECTED);
1770 allocate_cache_failed:
1771 rdma_connect_failed:
1772 rdma_destroy_qp(info->id);
1777 ib_free_cq(info->send_cq);
1779 ib_free_cq(info->recv_cq);
1782 ib_dealloc_pd(info->pd);
1783 rdma_destroy_id(info->id);
1790 struct smbd_connection *smbd_get_connection(
1791 struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1793 struct smbd_connection *ret;
1794 int port = SMBD_PORT;
1797 ret = _smbd_get_connection(server, dstaddr, port);
1799 /* Try SMB_PORT if SMBD_PORT doesn't work */
1800 if (!ret && port == SMBD_PORT) {
1808 * Receive data from receive reassembly queue
1809 * All the incoming data packets are placed in reassembly queue
1810 * buf: the buffer to read data into
1811 * size: the length of data to read
1812 * return value: actual data read
1813 * Note: this implementation copies the data from reassebmly queue to receive
1814 * buffers used by upper layer. This is not the optimal code path. A better way
1815 * to do it is to not have upper layer allocate its receive buffers but rather
1816 * borrow the buffer from reassembly queue, and return it after data is
1817 * consumed. But this will require more changes to upper layer code, and also
1818 * need to consider packet boundaries while they still being reassembled.
1820 static int smbd_recv_buf(struct smbd_connection *info, char *buf,
1823 struct smbd_response *response;
1824 struct smbd_data_transfer *data_transfer;
1825 int to_copy, to_read, data_read, offset;
1826 u32 data_length, remaining_data_length, data_offset;
1831 * No need to hold the reassembly queue lock all the time as we are
1832 * the only one reading from the front of the queue. The transport
1833 * may add more entries to the back of the queue at the same time
1835 log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1836 info->reassembly_data_length);
1837 if (info->reassembly_data_length >= size) {
1839 int queue_removed = 0;
1842 * Need to make sure reassembly_data_length is read before
1843 * reading reassembly_queue_length and calling
1844 * _get_first_reassembly. This call is lock free
1845 * as we never read at the end of the queue which are being
1846 * updated in SOFTIRQ as more data is received
1849 queue_length = info->reassembly_queue_length;
1852 offset = info->first_entry_offset;
1853 while (data_read < size) {
1854 response = _get_first_reassembly(info);
1855 data_transfer = smbd_response_payload(response);
1856 data_length = le32_to_cpu(data_transfer->data_length);
1857 remaining_data_length =
1859 data_transfer->remaining_data_length);
1860 data_offset = le32_to_cpu(data_transfer->data_offset);
1863 * The upper layer expects RFC1002 length at the
1864 * beginning of the payload. Return it to indicate
1865 * the total length of the packet. This minimize the
1866 * change to upper layer packet processing logic. This
1867 * will be eventually remove when an intermediate
1868 * transport layer is added
1870 if (response->first_segment && size == 4) {
1871 unsigned int rfc1002_len =
1872 data_length + remaining_data_length;
1873 *((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1875 response->first_segment = false;
1876 log_read(INFO, "returning rfc1002 length %d\n",
1878 goto read_rfc1002_done;
1881 to_copy = min_t(int, data_length - offset, to_read);
1884 (char *)data_transfer + data_offset + offset,
1887 /* move on to the next buffer? */
1888 if (to_copy == data_length - offset) {
1891 * No need to lock if we are not at the
1895 list_del(&response->list);
1898 &info->reassembly_queue_lock);
1899 list_del(&response->list);
1901 &info->reassembly_queue_lock);
1904 info->count_reassembly_queue--;
1905 info->count_dequeue_reassembly_queue++;
1906 put_receive_buffer(info, response);
1908 log_read(INFO, "put_receive_buffer offset=0\n");
1913 data_read += to_copy;
1915 log_read(INFO, "_get_first_reassembly memcpy %d bytes "
1916 "data_transfer_length-offset=%d after that "
1917 "to_read=%d data_read=%d offset=%d\n",
1918 to_copy, data_length - offset,
1919 to_read, data_read, offset);
1922 spin_lock_irq(&info->reassembly_queue_lock);
1923 info->reassembly_data_length -= data_read;
1924 info->reassembly_queue_length -= queue_removed;
1925 spin_unlock_irq(&info->reassembly_queue_lock);
1927 info->first_entry_offset = offset;
1928 log_read(INFO, "returning to thread data_read=%d "
1929 "reassembly_data_length=%d first_entry_offset=%d\n",
1930 data_read, info->reassembly_data_length,
1931 info->first_entry_offset);
1936 log_read(INFO, "wait_event on more data\n");
1937 rc = wait_event_interruptible(
1938 info->wait_reassembly_queue,
1939 info->reassembly_data_length >= size ||
1940 info->transport_status != SMBD_CONNECTED);
1941 /* Don't return any data if interrupted */
1945 if (info->transport_status != SMBD_CONNECTED) {
1946 log_read(ERR, "disconnected\n");
1947 return -ECONNABORTED;
1954 * Receive a page from receive reassembly queue
1955 * page: the page to read data into
1956 * to_read: the length of data to read
1957 * return value: actual data read
1959 static int smbd_recv_page(struct smbd_connection *info,
1960 struct page *page, unsigned int page_offset,
1961 unsigned int to_read)
1967 /* make sure we have the page ready for read */
1968 ret = wait_event_interruptible(
1969 info->wait_reassembly_queue,
1970 info->reassembly_data_length >= to_read ||
1971 info->transport_status != SMBD_CONNECTED);
1975 /* now we can read from reassembly queue and not sleep */
1976 page_address = kmap_atomic(page);
1977 to_address = (char *) page_address + page_offset;
1979 log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
1980 page, to_address, to_read);
1982 ret = smbd_recv_buf(info, to_address, to_read);
1983 kunmap_atomic(page_address);
1989 * Receive data from transport
1990 * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
1991 * return: total bytes read, or 0. SMB Direct will not do partial read.
1993 int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
1997 unsigned int to_read, page_offset;
2000 if (iov_iter_rw(&msg->msg_iter) == WRITE) {
2001 /* It's a bug in upper layer to get there */
2002 cifs_dbg(VFS, "CIFS: invalid msg iter dir %u\n",
2003 iov_iter_rw(&msg->msg_iter));
2008 switch (iov_iter_type(&msg->msg_iter)) {
2010 buf = msg->msg_iter.kvec->iov_base;
2011 to_read = msg->msg_iter.kvec->iov_len;
2012 rc = smbd_recv_buf(info, buf, to_read);
2016 page = msg->msg_iter.bvec->bv_page;
2017 page_offset = msg->msg_iter.bvec->bv_offset;
2018 to_read = msg->msg_iter.bvec->bv_len;
2019 rc = smbd_recv_page(info, page, page_offset, to_read);
2023 /* It's a bug in upper layer to get there */
2024 cifs_dbg(VFS, "CIFS: invalid msg type %d\n",
2025 iov_iter_type(&msg->msg_iter));
2030 /* SMBDirect will read it all or nothing */
2032 msg->msg_iter.count = 0;
2037 * Send data to transport
2038 * Each rqst is transported as a SMBDirect payload
2039 * rqst: the data to write
2040 * return value: 0 if successfully write, otherwise error code
2042 int smbd_send(struct TCP_Server_Info *server,
2043 int num_rqst, struct smb_rqst *rqst_array)
2045 struct smbd_connection *info = server->smbd_conn;
2049 unsigned int buflen, remaining_data_length;
2052 info->max_send_size - sizeof(struct smbd_data_transfer);
2055 struct smb_rqst *rqst;
2058 if (info->transport_status != SMBD_CONNECTED) {
2064 * Add in the page array if there is one. The caller needs to set
2065 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
2066 * ends at page boundary
2068 remaining_data_length = 0;
2069 for (i = 0; i < num_rqst; i++)
2070 remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
2072 if (remaining_data_length > info->max_fragmented_send_size) {
2073 log_write(ERR, "payload size %d > max size %d\n",
2074 remaining_data_length, info->max_fragmented_send_size);
2079 log_write(INFO, "num_rqst=%d total length=%u\n",
2080 num_rqst, remaining_data_length);
2084 rqst = &rqst_array[rqst_idx];
2087 cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
2088 rqst_idx, smb_rqst_len(server, rqst));
2089 for (i = 0; i < rqst->rq_nvec; i++)
2090 dump_smb(iov[i].iov_base, iov[i].iov_len);
2093 log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d "
2094 "rq_tailsz=%d buflen=%lu\n",
2095 rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
2096 rqst->rq_tailsz, smb_rqst_len(server, rqst));
2101 buflen += iov[i].iov_len;
2102 if (buflen > max_iov_size) {
2104 remaining_data_length -=
2105 (buflen-iov[i].iov_len);
2106 log_write(INFO, "sending iov[] from start=%d "
2108 "remaining_data_length=%d\n",
2110 remaining_data_length);
2111 rc = smbd_post_send_data(
2112 info, &iov[start], i-start,
2113 remaining_data_length);
2117 /* iov[start] is too big, break it */
2118 nvecs = (buflen+max_iov_size-1)/max_iov_size;
2119 log_write(INFO, "iov[%d] iov_base=%p buflen=%d"
2120 " break to %d vectors\n",
2121 start, iov[start].iov_base,
2123 for (j = 0; j < nvecs; j++) {
2125 (char *)iov[start].iov_base +
2127 vec.iov_len = max_iov_size;
2131 max_iov_size*(nvecs-1);
2132 remaining_data_length -= vec.iov_len;
2134 "sending vec j=%d iov_base=%p"
2136 "remaining_data_length=%d\n",
2137 j, vec.iov_base, vec.iov_len,
2138 remaining_data_length);
2139 rc = smbd_post_send_data(
2141 remaining_data_length);
2146 if (i == rqst->rq_nvec)
2153 if (i == rqst->rq_nvec) {
2154 /* send out all remaining vecs */
2155 remaining_data_length -= buflen;
2157 "sending iov[] from start=%d i=%d "
2158 "nvecs=%d remaining_data_length=%d\n",
2160 remaining_data_length);
2161 rc = smbd_post_send_data(info, &iov[start],
2162 i-start, remaining_data_length);
2168 log_write(INFO, "looping i=%d buflen=%d\n", i, buflen);
2171 /* now sending pages if there are any */
2172 for (i = 0; i < rqst->rq_npages; i++) {
2173 unsigned int offset;
2175 rqst_page_get_length(rqst, i, &buflen, &offset);
2176 nvecs = (buflen + max_iov_size - 1) / max_iov_size;
2177 log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
2179 for (j = 0; j < nvecs; j++) {
2180 size = max_iov_size;
2182 size = buflen - j*max_iov_size;
2183 remaining_data_length -= size;
2184 log_write(INFO, "sending pages i=%d offset=%d size=%d"
2185 " remaining_data_length=%d\n",
2186 i, j*max_iov_size+offset, size,
2187 remaining_data_length);
2188 rc = smbd_post_send_page(
2189 info, rqst->rq_pages[i],
2190 j*max_iov_size + offset,
2191 size, remaining_data_length);
2198 if (rqst_idx < num_rqst)
2203 * As an optimization, we don't wait for individual I/O to finish
2204 * before sending the next one.
2205 * Send them all and wait for pending send count to get to 0
2206 * that means all the I/Os have been out and we are good to return
2209 wait_event(info->wait_send_pending,
2210 atomic_read(&info->send_pending) == 0);
2215 static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
2221 log_rdma_mr(ERR, "status=%d\n", wc->status);
2223 mr = container_of(cqe, struct smbd_mr, cqe);
2224 smbd_disconnect_rdma_connection(mr->conn);
2229 * The work queue function that recovers MRs
2230 * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
2231 * again. Both calls are slow, so finish them in a workqueue. This will not
2233 * There is one workqueue that recovers MRs, there is no need to lock as the
2234 * I/O requests calling smbd_register_mr will never update the links in the
2237 static void smbd_mr_recovery_work(struct work_struct *work)
2239 struct smbd_connection *info =
2240 container_of(work, struct smbd_connection, mr_recovery_work);
2241 struct smbd_mr *smbdirect_mr;
2244 list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
2245 if (smbdirect_mr->state == MR_ERROR) {
2247 /* recover this MR entry */
2248 rc = ib_dereg_mr(smbdirect_mr->mr);
2251 "ib_dereg_mr failed rc=%x\n",
2253 smbd_disconnect_rdma_connection(info);
2257 smbdirect_mr->mr = ib_alloc_mr(
2258 info->pd, info->mr_type,
2259 info->max_frmr_depth);
2260 if (IS_ERR(smbdirect_mr->mr)) {
2262 "ib_alloc_mr failed mr_type=%x "
2263 "max_frmr_depth=%x\n",
2265 info->max_frmr_depth);
2266 smbd_disconnect_rdma_connection(info);
2270 /* This MR is being used, don't recover it */
2273 smbdirect_mr->state = MR_READY;
2275 /* smbdirect_mr->state is updated by this function
2276 * and is read and updated by I/O issuing CPUs trying
2277 * to get a MR, the call to atomic_inc_return
2278 * implicates a memory barrier and guarantees this
2279 * value is updated before waking up any calls to
2280 * get_mr() from the I/O issuing CPUs
2282 if (atomic_inc_return(&info->mr_ready_count) == 1)
2283 wake_up_interruptible(&info->wait_mr);
2287 static void destroy_mr_list(struct smbd_connection *info)
2289 struct smbd_mr *mr, *tmp;
2291 cancel_work_sync(&info->mr_recovery_work);
2292 list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
2293 if (mr->state == MR_INVALIDATED)
2294 ib_dma_unmap_sg(info->id->device, mr->sgl,
2295 mr->sgl_count, mr->dir);
2296 ib_dereg_mr(mr->mr);
2303 * Allocate MRs used for RDMA read/write
2304 * The number of MRs will not exceed hardware capability in responder_resources
2305 * All MRs are kept in mr_list. The MR can be recovered after it's used
2306 * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
2307 * as MRs are used and recovered for I/O, but the list links will not change
2309 static int allocate_mr_list(struct smbd_connection *info)
2312 struct smbd_mr *smbdirect_mr, *tmp;
2314 INIT_LIST_HEAD(&info->mr_list);
2315 init_waitqueue_head(&info->wait_mr);
2316 spin_lock_init(&info->mr_list_lock);
2317 atomic_set(&info->mr_ready_count, 0);
2318 atomic_set(&info->mr_used_count, 0);
2319 init_waitqueue_head(&info->wait_for_mr_cleanup);
2320 /* Allocate more MRs (2x) than hardware responder_resources */
2321 for (i = 0; i < info->responder_resources * 2; i++) {
2322 smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
2325 smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
2326 info->max_frmr_depth);
2327 if (IS_ERR(smbdirect_mr->mr)) {
2328 log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x "
2329 "max_frmr_depth=%x\n",
2330 info->mr_type, info->max_frmr_depth);
2333 smbdirect_mr->sgl = kcalloc(
2334 info->max_frmr_depth,
2335 sizeof(struct scatterlist),
2337 if (!smbdirect_mr->sgl) {
2338 log_rdma_mr(ERR, "failed to allocate sgl\n");
2339 ib_dereg_mr(smbdirect_mr->mr);
2342 smbdirect_mr->state = MR_READY;
2343 smbdirect_mr->conn = info;
2345 list_add_tail(&smbdirect_mr->list, &info->mr_list);
2346 atomic_inc(&info->mr_ready_count);
2348 INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
2352 kfree(smbdirect_mr);
2354 list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
2355 ib_dereg_mr(smbdirect_mr->mr);
2356 kfree(smbdirect_mr->sgl);
2357 kfree(smbdirect_mr);
2363 * Get a MR from mr_list. This function waits until there is at least one
2364 * MR available in the list. It may access the list while the
2365 * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
2366 * as they never modify the same places. However, there may be several CPUs
2367 * issueing I/O trying to get MR at the same time, mr_list_lock is used to
2368 * protect this situation.
2370 static struct smbd_mr *get_mr(struct smbd_connection *info)
2372 struct smbd_mr *ret;
2375 rc = wait_event_interruptible(info->wait_mr,
2376 atomic_read(&info->mr_ready_count) ||
2377 info->transport_status != SMBD_CONNECTED);
2379 log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
2383 if (info->transport_status != SMBD_CONNECTED) {
2384 log_rdma_mr(ERR, "info->transport_status=%x\n",
2385 info->transport_status);
2389 spin_lock(&info->mr_list_lock);
2390 list_for_each_entry(ret, &info->mr_list, list) {
2391 if (ret->state == MR_READY) {
2392 ret->state = MR_REGISTERED;
2393 spin_unlock(&info->mr_list_lock);
2394 atomic_dec(&info->mr_ready_count);
2395 atomic_inc(&info->mr_used_count);
2400 spin_unlock(&info->mr_list_lock);
2402 * It is possible that we could fail to get MR because other processes may
2403 * try to acquire a MR at the same time. If this is the case, retry it.
2409 * Register memory for RDMA read/write
2410 * pages[]: the list of pages to register memory with
2411 * num_pages: the number of pages to register
2412 * tailsz: if non-zero, the bytes to register in the last page
2413 * writing: true if this is a RDMA write (SMB read), false for RDMA read
2414 * need_invalidate: true if this MR needs to be locally invalidated after I/O
2415 * return value: the MR registered, NULL if failed.
2417 struct smbd_mr *smbd_register_mr(
2418 struct smbd_connection *info, struct page *pages[], int num_pages,
2419 int offset, int tailsz, bool writing, bool need_invalidate)
2421 struct smbd_mr *smbdirect_mr;
2423 enum dma_data_direction dir;
2424 struct ib_reg_wr *reg_wr;
2426 if (num_pages > info->max_frmr_depth) {
2427 log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
2428 num_pages, info->max_frmr_depth);
2432 smbdirect_mr = get_mr(info);
2433 if (!smbdirect_mr) {
2434 log_rdma_mr(ERR, "get_mr returning NULL\n");
2437 smbdirect_mr->need_invalidate = need_invalidate;
2438 smbdirect_mr->sgl_count = num_pages;
2439 sg_init_table(smbdirect_mr->sgl, num_pages);
2441 log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
2442 num_pages, offset, tailsz);
2444 if (num_pages == 1) {
2445 sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
2446 goto skip_multiple_pages;
2449 /* We have at least two pages to register */
2451 &smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
2453 while (i < num_pages - 1) {
2454 sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
2457 sg_set_page(&smbdirect_mr->sgl[i], pages[i],
2458 tailsz ? tailsz : PAGE_SIZE, 0);
2460 skip_multiple_pages:
2461 dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2462 smbdirect_mr->dir = dir;
2463 rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
2465 log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
2466 num_pages, dir, rc);
2470 rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
2472 if (rc != num_pages) {
2474 "ib_map_mr_sg failed rc = %d num_pages = %x\n",
2479 ib_update_fast_reg_key(smbdirect_mr->mr,
2480 ib_inc_rkey(smbdirect_mr->mr->rkey));
2481 reg_wr = &smbdirect_mr->wr;
2482 reg_wr->wr.opcode = IB_WR_REG_MR;
2483 smbdirect_mr->cqe.done = register_mr_done;
2484 reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
2485 reg_wr->wr.num_sge = 0;
2486 reg_wr->wr.send_flags = IB_SEND_SIGNALED;
2487 reg_wr->mr = smbdirect_mr->mr;
2488 reg_wr->key = smbdirect_mr->mr->rkey;
2489 reg_wr->access = writing ?
2490 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2491 IB_ACCESS_REMOTE_READ;
2494 * There is no need for waiting for complemtion on ib_post_send
2495 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
2496 * on the next ib_post_send when we actaully send I/O to remote peer
2498 rc = ib_post_send(info->id->qp, ®_wr->wr, NULL);
2500 return smbdirect_mr;
2502 log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
2505 /* If all failed, attempt to recover this MR by setting it MR_ERROR*/
2507 ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
2508 smbdirect_mr->sgl_count, smbdirect_mr->dir);
2511 smbdirect_mr->state = MR_ERROR;
2512 if (atomic_dec_and_test(&info->mr_used_count))
2513 wake_up(&info->wait_for_mr_cleanup);
2515 smbd_disconnect_rdma_connection(info);
2520 static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
2522 struct smbd_mr *smbdirect_mr;
2526 smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
2527 smbdirect_mr->state = MR_INVALIDATED;
2528 if (wc->status != IB_WC_SUCCESS) {
2529 log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
2530 smbdirect_mr->state = MR_ERROR;
2532 complete(&smbdirect_mr->invalidate_done);
2536 * Deregister a MR after I/O is done
2537 * This function may wait if remote invalidation is not used
2538 * and we have to locally invalidate the buffer to prevent data is being
2539 * modified by remote peer after upper layer consumes it
2541 int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
2543 struct ib_send_wr *wr;
2544 struct smbd_connection *info = smbdirect_mr->conn;
2547 if (smbdirect_mr->need_invalidate) {
2548 /* Need to finish local invalidation before returning */
2549 wr = &smbdirect_mr->inv_wr;
2550 wr->opcode = IB_WR_LOCAL_INV;
2551 smbdirect_mr->cqe.done = local_inv_done;
2552 wr->wr_cqe = &smbdirect_mr->cqe;
2554 wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
2555 wr->send_flags = IB_SEND_SIGNALED;
2557 init_completion(&smbdirect_mr->invalidate_done);
2558 rc = ib_post_send(info->id->qp, wr, NULL);
2560 log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
2561 smbd_disconnect_rdma_connection(info);
2564 wait_for_completion(&smbdirect_mr->invalidate_done);
2565 smbdirect_mr->need_invalidate = false;
2568 * For remote invalidation, just set it to MR_INVALIDATED
2569 * and defer to mr_recovery_work to recover the MR for next use
2571 smbdirect_mr->state = MR_INVALIDATED;
2573 if (smbdirect_mr->state == MR_INVALIDATED) {
2575 info->id->device, smbdirect_mr->sgl,
2576 smbdirect_mr->sgl_count,
2578 smbdirect_mr->state = MR_READY;
2579 if (atomic_inc_return(&info->mr_ready_count) == 1)
2580 wake_up_interruptible(&info->wait_mr);
2583 * Schedule the work to do MR recovery for future I/Os MR
2584 * recovery is slow and don't want it to block current I/O
2586 queue_work(info->workqueue, &info->mr_recovery_work);
2589 if (atomic_dec_and_test(&info->mr_used_count))
2590 wake_up(&info->wait_for_mr_cleanup);