2 * Copyright (C) 2017, Microsoft Corporation.
4 * Author(s): Long Li <longli@microsoft.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/highmem.h>
18 #include "smbdirect.h"
19 #include "cifs_debug.h"
21 static struct smbd_response *get_empty_queue_buffer(
22 struct smbd_connection *info);
23 static struct smbd_response *get_receive_buffer(
24 struct smbd_connection *info);
25 static void put_receive_buffer(
26 struct smbd_connection *info,
27 struct smbd_response *response);
28 static int allocate_receive_buffers(struct smbd_connection *info, int num_buf);
29 static void destroy_receive_buffers(struct smbd_connection *info);
31 static void put_empty_packet(
32 struct smbd_connection *info, struct smbd_response *response);
33 static void enqueue_reassembly(
34 struct smbd_connection *info,
35 struct smbd_response *response, int data_length);
36 static struct smbd_response *_get_first_reassembly(
37 struct smbd_connection *info);
39 static int smbd_post_recv(
40 struct smbd_connection *info,
41 struct smbd_response *response);
43 static int smbd_post_send_empty(struct smbd_connection *info);
45 /* SMBD version number */
46 #define SMBD_V1 0x0100
48 /* Port numbers for SMBD transport */
50 #define SMBD_PORT 5445
52 /* Address lookup and resolve timeout in ms */
53 #define RDMA_RESOLVE_TIMEOUT 5000
55 /* SMBD negotiation timeout in seconds */
56 #define SMBD_NEGOTIATE_TIMEOUT 120
58 /* SMBD minimum receive size and fragmented sized defined in [MS-SMBD] */
59 #define SMBD_MIN_RECEIVE_SIZE 128
60 #define SMBD_MIN_FRAGMENTED_SIZE 131072
63 * Default maximum number of RDMA read/write outstanding on this connection
64 * This value is possibly decreased during QP creation on hardware limit
66 #define SMBD_CM_RESPONDER_RESOURCES 32
68 /* Maximum number of retries on data transfer operations */
69 #define SMBD_CM_RETRY 6
70 /* No need to retry on Receiver Not Ready since SMBD manages credits */
71 #define SMBD_CM_RNR_RETRY 0
74 * User configurable initial values per SMBD transport connection
75 * as defined in [MS-SMBD] 3.1.1.1
76 * Those may change after a SMBD negotiation
78 /* The local peer's maximum number of credits to grant to the peer */
79 int smbd_receive_credit_max = 255;
81 /* The remote peer's credit request of local peer */
82 int smbd_send_credit_target = 255;
84 /* The maximum single message size can be sent to remote peer */
85 int smbd_max_send_size = 1364;
87 /* The maximum fragmented upper-layer payload receive size supported */
88 int smbd_max_fragmented_recv_size = 1024 * 1024;
90 /* The maximum single-message size which can be received */
91 int smbd_max_receive_size = 8192;
93 /* The timeout to initiate send of a keepalive message on idle */
94 int smbd_keep_alive_interval = 120;
97 * User configurable initial values for RDMA transport
98 * The actual values used may be lower and are limited to hardware capabilities
100 /* Default maximum number of SGEs in a RDMA write/read */
101 int smbd_max_frmr_depth = 2048;
103 /* If payload is less than this byte, use RDMA send/recv not read/write */
104 int rdma_readwrite_threshold = 4096;
106 /* Transport logging functions
107 * Logging are defined as classes. They can be OR'ed to define the actual
108 * logging level via module parameter smbd_logging_class
109 * e.g. cifs.smbd_logging_class=0xa0 will log all log_rdma_recv() and
112 #define LOG_OUTGOING 0x1
113 #define LOG_INCOMING 0x2
115 #define LOG_WRITE 0x8
116 #define LOG_RDMA_SEND 0x10
117 #define LOG_RDMA_RECV 0x20
118 #define LOG_KEEP_ALIVE 0x40
119 #define LOG_RDMA_EVENT 0x80
120 #define LOG_RDMA_MR 0x100
121 static unsigned int smbd_logging_class;
122 module_param(smbd_logging_class, uint, 0644);
123 MODULE_PARM_DESC(smbd_logging_class,
124 "Logging class for SMBD transport 0x0 to 0x100");
128 static unsigned int smbd_logging_level = ERR;
129 module_param(smbd_logging_level, uint, 0644);
130 MODULE_PARM_DESC(smbd_logging_level,
131 "Logging level for SMBD transport, 0 (default): error, 1: info");
133 #define log_rdma(level, class, fmt, args...) \
135 if (level <= smbd_logging_level || class & smbd_logging_class) \
136 cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
139 #define log_outgoing(level, fmt, args...) \
140 log_rdma(level, LOG_OUTGOING, fmt, ##args)
141 #define log_incoming(level, fmt, args...) \
142 log_rdma(level, LOG_INCOMING, fmt, ##args)
143 #define log_read(level, fmt, args...) log_rdma(level, LOG_READ, fmt, ##args)
144 #define log_write(level, fmt, args...) log_rdma(level, LOG_WRITE, fmt, ##args)
145 #define log_rdma_send(level, fmt, args...) \
146 log_rdma(level, LOG_RDMA_SEND, fmt, ##args)
147 #define log_rdma_recv(level, fmt, args...) \
148 log_rdma(level, LOG_RDMA_RECV, fmt, ##args)
149 #define log_keep_alive(level, fmt, args...) \
150 log_rdma(level, LOG_KEEP_ALIVE, fmt, ##args)
151 #define log_rdma_event(level, fmt, args...) \
152 log_rdma(level, LOG_RDMA_EVENT, fmt, ##args)
153 #define log_rdma_mr(level, fmt, args...) \
154 log_rdma(level, LOG_RDMA_MR, fmt, ##args)
157 * Destroy the transport and related RDMA and memory resources
158 * Need to go through all the pending counters and make sure on one is using
159 * the transport while it is destroyed
161 static void smbd_destroy_rdma_work(struct work_struct *work)
163 struct smbd_response *response;
164 struct smbd_connection *info =
165 container_of(work, struct smbd_connection, destroy_work);
168 log_rdma_event(INFO, "destroying qp\n");
169 ib_drain_qp(info->id->qp);
170 rdma_destroy_qp(info->id);
172 /* Unblock all I/O waiting on the send queue */
173 wake_up_interruptible_all(&info->wait_send_queue);
175 log_rdma_event(INFO, "cancelling idle timer\n");
176 cancel_delayed_work_sync(&info->idle_timer_work);
177 log_rdma_event(INFO, "cancelling send immediate work\n");
178 cancel_delayed_work_sync(&info->send_immediate_work);
180 log_rdma_event(INFO, "wait for all recv to finish\n");
181 wake_up_interruptible(&info->wait_reassembly_queue);
182 wait_event(info->wait_smbd_recv_pending,
183 info->smbd_recv_pending == 0);
185 log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
186 wait_event(info->wait_send_pending,
187 atomic_read(&info->send_pending) == 0);
188 wait_event(info->wait_send_payload_pending,
189 atomic_read(&info->send_payload_pending) == 0);
191 /* It's not posssible for upper layer to get to reassembly */
192 log_rdma_event(INFO, "drain the reassembly queue\n");
194 spin_lock_irqsave(&info->reassembly_queue_lock, flags);
195 response = _get_first_reassembly(info);
197 list_del(&response->list);
198 spin_unlock_irqrestore(
199 &info->reassembly_queue_lock, flags);
200 put_receive_buffer(info, response);
203 spin_unlock_irqrestore(&info->reassembly_queue_lock, flags);
204 info->reassembly_data_length = 0;
206 log_rdma_event(INFO, "free receive buffers\n");
207 wait_event(info->wait_receive_queues,
208 info->count_receive_queue + info->count_empty_packet_queue
209 == info->receive_credit_max);
210 destroy_receive_buffers(info);
212 ib_free_cq(info->send_cq);
213 ib_free_cq(info->recv_cq);
214 ib_dealloc_pd(info->pd);
215 rdma_destroy_id(info->id);
218 mempool_destroy(info->request_mempool);
219 kmem_cache_destroy(info->request_cache);
221 mempool_destroy(info->response_mempool);
222 kmem_cache_destroy(info->response_cache);
224 info->transport_status = SMBD_DESTROYED;
225 wake_up_all(&info->wait_destroy);
228 static int smbd_process_disconnected(struct smbd_connection *info)
230 schedule_work(&info->destroy_work);
234 static void smbd_disconnect_rdma_work(struct work_struct *work)
236 struct smbd_connection *info =
237 container_of(work, struct smbd_connection, disconnect_work);
239 if (info->transport_status == SMBD_CONNECTED) {
240 info->transport_status = SMBD_DISCONNECTING;
241 rdma_disconnect(info->id);
245 static void smbd_disconnect_rdma_connection(struct smbd_connection *info)
247 queue_work(info->workqueue, &info->disconnect_work);
250 /* Upcall from RDMA CM */
251 static int smbd_conn_upcall(
252 struct rdma_cm_id *id, struct rdma_cm_event *event)
254 struct smbd_connection *info = id->context;
256 log_rdma_event(INFO, "event=%d status=%d\n",
257 event->event, event->status);
259 switch (event->event) {
260 case RDMA_CM_EVENT_ADDR_RESOLVED:
261 case RDMA_CM_EVENT_ROUTE_RESOLVED:
263 complete(&info->ri_done);
266 case RDMA_CM_EVENT_ADDR_ERROR:
267 info->ri_rc = -EHOSTUNREACH;
268 complete(&info->ri_done);
271 case RDMA_CM_EVENT_ROUTE_ERROR:
272 info->ri_rc = -ENETUNREACH;
273 complete(&info->ri_done);
276 case RDMA_CM_EVENT_ESTABLISHED:
277 log_rdma_event(INFO, "connected event=%d\n", event->event);
278 info->transport_status = SMBD_CONNECTED;
279 wake_up_interruptible(&info->conn_wait);
282 case RDMA_CM_EVENT_CONNECT_ERROR:
283 case RDMA_CM_EVENT_UNREACHABLE:
284 case RDMA_CM_EVENT_REJECTED:
285 log_rdma_event(INFO, "connecting failed event=%d\n", event->event);
286 info->transport_status = SMBD_DISCONNECTED;
287 wake_up_interruptible(&info->conn_wait);
290 case RDMA_CM_EVENT_DEVICE_REMOVAL:
291 case RDMA_CM_EVENT_DISCONNECTED:
292 /* This happenes when we fail the negotiation */
293 if (info->transport_status == SMBD_NEGOTIATE_FAILED) {
294 info->transport_status = SMBD_DISCONNECTED;
295 wake_up(&info->conn_wait);
299 info->transport_status = SMBD_DISCONNECTED;
300 smbd_process_disconnected(info);
310 /* Upcall from RDMA QP */
312 smbd_qp_async_error_upcall(struct ib_event *event, void *context)
314 struct smbd_connection *info = context;
316 log_rdma_event(ERR, "%s on device %s info %p\n",
317 ib_event_msg(event->event), event->device->name, info);
319 switch (event->event) {
320 case IB_EVENT_CQ_ERR:
321 case IB_EVENT_QP_FATAL:
322 smbd_disconnect_rdma_connection(info);
329 static inline void *smbd_request_payload(struct smbd_request *request)
331 return (void *)request->packet;
334 static inline void *smbd_response_payload(struct smbd_response *response)
336 return (void *)response->packet;
339 /* Called when a RDMA send is done */
340 static void send_done(struct ib_cq *cq, struct ib_wc *wc)
343 struct smbd_request *request =
344 container_of(wc->wr_cqe, struct smbd_request, cqe);
346 log_rdma_send(INFO, "smbd_request %p completed wc->status=%d\n",
347 request, wc->status);
349 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
350 log_rdma_send(ERR, "wc->status=%d wc->opcode=%d\n",
351 wc->status, wc->opcode);
352 smbd_disconnect_rdma_connection(request->info);
355 for (i = 0; i < request->num_sge; i++)
356 ib_dma_unmap_single(request->info->id->device,
357 request->sge[i].addr,
358 request->sge[i].length,
361 if (request->has_payload) {
362 if (atomic_dec_and_test(&request->info->send_payload_pending))
363 wake_up(&request->info->wait_send_payload_pending);
365 if (atomic_dec_and_test(&request->info->send_pending))
366 wake_up(&request->info->wait_send_pending);
369 mempool_free(request, request->info->request_mempool);
372 static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
374 log_rdma_event(INFO, "resp message min_version %u max_version %u "
375 "negotiated_version %u credits_requested %u "
376 "credits_granted %u status %u max_readwrite_size %u "
377 "preferred_send_size %u max_receive_size %u "
378 "max_fragmented_size %u\n",
379 resp->min_version, resp->max_version, resp->negotiated_version,
380 resp->credits_requested, resp->credits_granted, resp->status,
381 resp->max_readwrite_size, resp->preferred_send_size,
382 resp->max_receive_size, resp->max_fragmented_size);
386 * Process a negotiation response message, according to [MS-SMBD]3.1.5.7
387 * response, packet_length: the negotiation response message
388 * return value: true if negotiation is a success, false if failed
390 static bool process_negotiation_response(
391 struct smbd_response *response, int packet_length)
393 struct smbd_connection *info = response->info;
394 struct smbd_negotiate_resp *packet = smbd_response_payload(response);
396 if (packet_length < sizeof(struct smbd_negotiate_resp)) {
398 "error: packet_length=%d\n", packet_length);
402 if (le16_to_cpu(packet->negotiated_version) != SMBD_V1) {
403 log_rdma_event(ERR, "error: negotiated_version=%x\n",
404 le16_to_cpu(packet->negotiated_version));
407 info->protocol = le16_to_cpu(packet->negotiated_version);
409 if (packet->credits_requested == 0) {
410 log_rdma_event(ERR, "error: credits_requested==0\n");
413 info->receive_credit_target = le16_to_cpu(packet->credits_requested);
415 if (packet->credits_granted == 0) {
416 log_rdma_event(ERR, "error: credits_granted==0\n");
419 atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
421 atomic_set(&info->receive_credits, 0);
423 if (le32_to_cpu(packet->preferred_send_size) > info->max_receive_size) {
424 log_rdma_event(ERR, "error: preferred_send_size=%d\n",
425 le32_to_cpu(packet->preferred_send_size));
428 info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
430 if (le32_to_cpu(packet->max_receive_size) < SMBD_MIN_RECEIVE_SIZE) {
431 log_rdma_event(ERR, "error: max_receive_size=%d\n",
432 le32_to_cpu(packet->max_receive_size));
435 info->max_send_size = min_t(int, info->max_send_size,
436 le32_to_cpu(packet->max_receive_size));
438 if (le32_to_cpu(packet->max_fragmented_size) <
439 SMBD_MIN_FRAGMENTED_SIZE) {
440 log_rdma_event(ERR, "error: max_fragmented_size=%d\n",
441 le32_to_cpu(packet->max_fragmented_size));
444 info->max_fragmented_send_size =
445 le32_to_cpu(packet->max_fragmented_size);
451 * Check and schedule to send an immediate packet
452 * This is used to extend credtis to remote peer to keep the transport busy
454 static void check_and_send_immediate(struct smbd_connection *info)
456 if (info->transport_status != SMBD_CONNECTED)
459 info->send_immediate = true;
462 * Promptly send a packet if our peer is running low on receive
465 if (atomic_read(&info->receive_credits) <
466 info->receive_credit_target - 1)
468 info->workqueue, &info->send_immediate_work, 0);
471 static void smbd_post_send_credits(struct work_struct *work)
474 int use_receive_queue = 1;
476 struct smbd_response *response;
477 struct smbd_connection *info =
478 container_of(work, struct smbd_connection,
479 post_send_credits_work);
481 if (info->transport_status != SMBD_CONNECTED) {
482 wake_up(&info->wait_receive_queues);
486 if (info->receive_credit_target >
487 atomic_read(&info->receive_credits)) {
489 if (use_receive_queue)
490 response = get_receive_buffer(info);
492 response = get_empty_queue_buffer(info);
494 /* now switch to emtpy packet queue */
495 if (use_receive_queue) {
496 use_receive_queue = 0;
502 response->type = SMBD_TRANSFER_DATA;
503 response->first_segment = false;
504 rc = smbd_post_recv(info, response);
507 "post_recv failed rc=%d\n", rc);
508 put_receive_buffer(info, response);
516 spin_lock(&info->lock_new_credits_offered);
517 info->new_credits_offered += ret;
518 spin_unlock(&info->lock_new_credits_offered);
520 atomic_add(ret, &info->receive_credits);
522 /* Check if we can post new receive and grant credits to peer */
523 check_and_send_immediate(info);
526 static void smbd_recv_done_work(struct work_struct *work)
528 struct smbd_connection *info =
529 container_of(work, struct smbd_connection, recv_done_work);
532 * We may have new send credits granted from remote peer
533 * If any sender is blcoked on lack of credets, unblock it
535 if (atomic_read(&info->send_credits))
536 wake_up_interruptible(&info->wait_send_queue);
539 * Check if we need to send something to remote peer to
540 * grant more credits or respond to KEEP_ALIVE packet
542 check_and_send_immediate(info);
545 /* Called from softirq, when recv is done */
546 static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
548 struct smbd_data_transfer *data_transfer;
549 struct smbd_response *response =
550 container_of(wc->wr_cqe, struct smbd_response, cqe);
551 struct smbd_connection *info = response->info;
554 log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d "
555 "byte_len=%d pkey_index=%x\n",
556 response, response->type, wc->status, wc->opcode,
557 wc->byte_len, wc->pkey_index);
559 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
560 log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
561 wc->status, wc->opcode);
562 smbd_disconnect_rdma_connection(info);
566 ib_dma_sync_single_for_cpu(
569 response->sge.length,
572 switch (response->type) {
573 /* SMBD negotiation response */
574 case SMBD_NEGOTIATE_RESP:
575 dump_smbd_negotiate_resp(smbd_response_payload(response));
576 info->full_packet_received = true;
577 info->negotiate_done =
578 process_negotiation_response(response, wc->byte_len);
579 complete(&info->negotiate_completion);
582 /* SMBD data transfer packet */
583 case SMBD_TRANSFER_DATA:
584 data_transfer = smbd_response_payload(response);
585 data_length = le32_to_cpu(data_transfer->data_length);
588 * If this is a packet with data playload place the data in
589 * reassembly queue and wake up the reading thread
592 if (info->full_packet_received)
593 response->first_segment = true;
595 if (le32_to_cpu(data_transfer->remaining_data_length))
596 info->full_packet_received = false;
598 info->full_packet_received = true;
605 put_empty_packet(info, response);
608 wake_up_interruptible(&info->wait_reassembly_queue);
610 atomic_dec(&info->receive_credits);
611 info->receive_credit_target =
612 le16_to_cpu(data_transfer->credits_requested);
613 atomic_add(le16_to_cpu(data_transfer->credits_granted),
614 &info->send_credits);
616 log_incoming(INFO, "data flags %d data_offset %d "
617 "data_length %d remaining_data_length %d\n",
618 le16_to_cpu(data_transfer->flags),
619 le32_to_cpu(data_transfer->data_offset),
620 le32_to_cpu(data_transfer->data_length),
621 le32_to_cpu(data_transfer->remaining_data_length));
623 /* Send a KEEP_ALIVE response right away if requested */
624 info->keep_alive_requested = KEEP_ALIVE_NONE;
625 if (le16_to_cpu(data_transfer->flags) &
626 SMB_DIRECT_RESPONSE_REQUESTED) {
627 info->keep_alive_requested = KEEP_ALIVE_PENDING;
630 queue_work(info->workqueue, &info->recv_done_work);
635 "unexpected response type=%d\n", response->type);
639 put_receive_buffer(info, response);
642 static struct rdma_cm_id *smbd_create_id(
643 struct smbd_connection *info,
644 struct sockaddr *dstaddr, int port)
646 struct rdma_cm_id *id;
650 id = rdma_create_id(&init_net, smbd_conn_upcall, info,
651 RDMA_PS_TCP, IB_QPT_RC);
654 log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
658 if (dstaddr->sa_family == AF_INET6)
659 sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
661 sport = &((struct sockaddr_in *)dstaddr)->sin_port;
663 *sport = htons(port);
665 init_completion(&info->ri_done);
666 info->ri_rc = -ETIMEDOUT;
668 rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
669 RDMA_RESOLVE_TIMEOUT);
671 log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
674 wait_for_completion_interruptible_timeout(
675 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
678 log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
682 info->ri_rc = -ETIMEDOUT;
683 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
685 log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
688 wait_for_completion_interruptible_timeout(
689 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
692 log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
704 * Test if FRWR (Fast Registration Work Requests) is supported on the device
705 * This implementation requries FRWR on RDMA read/write
706 * return value: true if it is supported
708 static bool frwr_is_supported(struct ib_device_attr *attrs)
710 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
712 if (attrs->max_fast_reg_page_list_len == 0)
717 static int smbd_ia_open(
718 struct smbd_connection *info,
719 struct sockaddr *dstaddr, int port)
723 info->id = smbd_create_id(info, dstaddr, port);
724 if (IS_ERR(info->id)) {
725 rc = PTR_ERR(info->id);
729 if (!frwr_is_supported(&info->id->device->attrs)) {
731 "Fast Registration Work Requests "
732 "(FRWR) is not supported\n");
734 "Device capability flags = %llx "
735 "max_fast_reg_page_list_len = %u\n",
736 info->id->device->attrs.device_cap_flags,
737 info->id->device->attrs.max_fast_reg_page_list_len);
738 rc = -EPROTONOSUPPORT;
742 info->pd = ib_alloc_pd(info->id->device, 0);
743 if (IS_ERR(info->pd)) {
744 rc = PTR_ERR(info->pd);
745 log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
752 rdma_destroy_id(info->id);
760 * Send a negotiation request message to the peer
761 * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
762 * After negotiation, the transport is connected and ready for
763 * carrying upper layer SMB payload
765 static int smbd_post_send_negotiate_req(struct smbd_connection *info)
767 struct ib_send_wr send_wr, *send_wr_fail;
769 struct smbd_request *request;
770 struct smbd_negotiate_req *packet;
772 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
776 request->info = info;
778 packet = smbd_request_payload(request);
779 packet->min_version = cpu_to_le16(SMBD_V1);
780 packet->max_version = cpu_to_le16(SMBD_V1);
781 packet->reserved = 0;
782 packet->credits_requested = cpu_to_le16(info->send_credit_target);
783 packet->preferred_send_size = cpu_to_le32(info->max_send_size);
784 packet->max_receive_size = cpu_to_le32(info->max_receive_size);
785 packet->max_fragmented_size =
786 cpu_to_le32(info->max_fragmented_recv_size);
788 request->num_sge = 1;
789 request->sge[0].addr = ib_dma_map_single(
790 info->id->device, (void *)packet,
791 sizeof(*packet), DMA_TO_DEVICE);
792 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
794 goto dma_mapping_failed;
797 request->sge[0].length = sizeof(*packet);
798 request->sge[0].lkey = info->pd->local_dma_lkey;
800 ib_dma_sync_single_for_device(
801 info->id->device, request->sge[0].addr,
802 request->sge[0].length, DMA_TO_DEVICE);
804 request->cqe.done = send_done;
807 send_wr.wr_cqe = &request->cqe;
808 send_wr.sg_list = request->sge;
809 send_wr.num_sge = request->num_sge;
810 send_wr.opcode = IB_WR_SEND;
811 send_wr.send_flags = IB_SEND_SIGNALED;
813 log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
814 request->sge[0].addr,
815 request->sge[0].length, request->sge[0].lkey);
817 request->has_payload = false;
818 atomic_inc(&info->send_pending);
819 rc = ib_post_send(info->id->qp, &send_wr, &send_wr_fail);
823 /* if we reach here, post send failed */
824 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
825 atomic_dec(&info->send_pending);
826 ib_dma_unmap_single(info->id->device, request->sge[0].addr,
827 request->sge[0].length, DMA_TO_DEVICE);
830 mempool_free(request, info->request_mempool);
835 * Extend the credits to remote peer
836 * This implements [MS-SMBD] 3.1.5.9
837 * The idea is that we should extend credits to remote peer as quickly as
838 * it's allowed, to maintain data flow. We allocate as much receive
839 * buffer as possible, and extend the receive credits to remote peer
840 * return value: the new credtis being granted.
842 static int manage_credits_prior_sending(struct smbd_connection *info)
846 spin_lock(&info->lock_new_credits_offered);
847 new_credits = info->new_credits_offered;
848 info->new_credits_offered = 0;
849 spin_unlock(&info->lock_new_credits_offered);
855 * Check if we need to send a KEEP_ALIVE message
856 * The idle connection timer triggers a KEEP_ALIVE message when expires
857 * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
860 * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
863 static int manage_keep_alive_before_sending(struct smbd_connection *info)
865 if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
866 info->keep_alive_requested = KEEP_ALIVE_SENT;
873 * Build and prepare the SMBD packet header
874 * This function waits for avaialbe send credits and build a SMBD packet
875 * header. The caller then optional append payload to the packet after
878 * size: the size of the payload
879 * remaining_data_length: remaining data to send if this is part of a
882 * request_out: the request allocated from this function
883 * return values: 0 on success, otherwise actual error code returned
885 static int smbd_create_header(struct smbd_connection *info,
886 int size, int remaining_data_length,
887 struct smbd_request **request_out)
889 struct smbd_request *request;
890 struct smbd_data_transfer *packet;
894 /* Wait for send credits. A SMBD packet needs one credit */
895 rc = wait_event_interruptible(info->wait_send_queue,
896 atomic_read(&info->send_credits) > 0 ||
897 info->transport_status != SMBD_CONNECTED);
901 if (info->transport_status != SMBD_CONNECTED) {
902 log_outgoing(ERR, "disconnected not sending\n");
905 atomic_dec(&info->send_credits);
907 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
913 request->info = info;
915 /* Fill in the packet header */
916 packet = smbd_request_payload(request);
917 packet->credits_requested = cpu_to_le16(info->send_credit_target);
918 packet->credits_granted =
919 cpu_to_le16(manage_credits_prior_sending(info));
920 info->send_immediate = false;
923 if (manage_keep_alive_before_sending(info))
924 packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
926 packet->reserved = 0;
928 packet->data_offset = 0;
930 packet->data_offset = cpu_to_le32(24);
931 packet->data_length = cpu_to_le32(size);
932 packet->remaining_data_length = cpu_to_le32(remaining_data_length);
935 log_outgoing(INFO, "credits_requested=%d credits_granted=%d "
936 "data_offset=%d data_length=%d remaining_data_length=%d\n",
937 le16_to_cpu(packet->credits_requested),
938 le16_to_cpu(packet->credits_granted),
939 le32_to_cpu(packet->data_offset),
940 le32_to_cpu(packet->data_length),
941 le32_to_cpu(packet->remaining_data_length));
943 /* Map the packet to DMA */
944 header_length = sizeof(struct smbd_data_transfer);
945 /* If this is a packet without payload, don't send padding */
947 header_length = offsetof(struct smbd_data_transfer, padding);
949 request->num_sge = 1;
950 request->sge[0].addr = ib_dma_map_single(info->id->device,
954 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
955 mempool_free(request, info->request_mempool);
960 request->sge[0].length = header_length;
961 request->sge[0].lkey = info->pd->local_dma_lkey;
963 *request_out = request;
967 atomic_inc(&info->send_credits);
971 static void smbd_destroy_header(struct smbd_connection *info,
972 struct smbd_request *request)
975 ib_dma_unmap_single(info->id->device,
976 request->sge[0].addr,
977 request->sge[0].length,
979 mempool_free(request, info->request_mempool);
980 atomic_inc(&info->send_credits);
983 /* Post the send request */
984 static int smbd_post_send(struct smbd_connection *info,
985 struct smbd_request *request, bool has_payload)
987 struct ib_send_wr send_wr, *send_wr_fail;
990 for (i = 0; i < request->num_sge; i++) {
992 "rdma_request sge[%d] addr=%llu legnth=%u\n",
993 i, request->sge[0].addr, request->sge[0].length);
994 ib_dma_sync_single_for_device(
996 request->sge[i].addr,
997 request->sge[i].length,
1001 request->cqe.done = send_done;
1003 send_wr.next = NULL;
1004 send_wr.wr_cqe = &request->cqe;
1005 send_wr.sg_list = request->sge;
1006 send_wr.num_sge = request->num_sge;
1007 send_wr.opcode = IB_WR_SEND;
1008 send_wr.send_flags = IB_SEND_SIGNALED;
1011 request->has_payload = true;
1012 atomic_inc(&info->send_payload_pending);
1014 request->has_payload = false;
1015 atomic_inc(&info->send_pending);
1018 rc = ib_post_send(info->id->qp, &send_wr, &send_wr_fail);
1020 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
1022 if (atomic_dec_and_test(&info->send_payload_pending))
1023 wake_up(&info->wait_send_payload_pending);
1025 if (atomic_dec_and_test(&info->send_pending))
1026 wake_up(&info->wait_send_pending);
1029 /* Reset timer for idle connection after packet is sent */
1030 mod_delayed_work(info->workqueue, &info->idle_timer_work,
1031 info->keep_alive_interval*HZ);
1036 static int smbd_post_send_sgl(struct smbd_connection *info,
1037 struct scatterlist *sgl, int data_length, int remaining_data_length)
1041 struct smbd_request *request;
1042 struct scatterlist *sg;
1044 rc = smbd_create_header(
1045 info, data_length, remaining_data_length, &request);
1049 num_sgs = sgl ? sg_nents(sgl) : 0;
1050 for_each_sg(sgl, sg, num_sgs, i) {
1051 request->sge[i+1].addr =
1052 ib_dma_map_page(info->id->device, sg_page(sg),
1053 sg->offset, sg->length, DMA_BIDIRECTIONAL);
1054 if (ib_dma_mapping_error(
1055 info->id->device, request->sge[i+1].addr)) {
1057 request->sge[i+1].addr = 0;
1058 goto dma_mapping_failure;
1060 request->sge[i+1].length = sg->length;
1061 request->sge[i+1].lkey = info->pd->local_dma_lkey;
1065 rc = smbd_post_send(info, request, data_length);
1069 dma_mapping_failure:
1070 for (i = 1; i < request->num_sge; i++)
1071 if (request->sge[i].addr)
1072 ib_dma_unmap_single(info->id->device,
1073 request->sge[i].addr,
1074 request->sge[i].length,
1076 smbd_destroy_header(info, request);
1081 * Send an empty message
1082 * Empty message is used to extend credits to peer to for keep live
1083 * while there is no upper layer payload to send at the time
1085 static int smbd_post_send_empty(struct smbd_connection *info)
1087 info->count_send_empty++;
1088 return smbd_post_send_sgl(info, NULL, 0, 0);
1092 * Post a receive request to the transport
1093 * The remote peer can only send data when a receive request is posted
1094 * The interaction is controlled by send/receive credit system
1096 static int smbd_post_recv(
1097 struct smbd_connection *info, struct smbd_response *response)
1099 struct ib_recv_wr recv_wr, *recv_wr_fail = NULL;
1102 response->sge.addr = ib_dma_map_single(
1103 info->id->device, response->packet,
1104 info->max_receive_size, DMA_FROM_DEVICE);
1105 if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1108 response->sge.length = info->max_receive_size;
1109 response->sge.lkey = info->pd->local_dma_lkey;
1111 response->cqe.done = recv_done;
1113 recv_wr.wr_cqe = &response->cqe;
1114 recv_wr.next = NULL;
1115 recv_wr.sg_list = &response->sge;
1116 recv_wr.num_sge = 1;
1118 rc = ib_post_recv(info->id->qp, &recv_wr, &recv_wr_fail);
1120 ib_dma_unmap_single(info->id->device, response->sge.addr,
1121 response->sge.length, DMA_FROM_DEVICE);
1123 log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1129 /* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1130 static int smbd_negotiate(struct smbd_connection *info)
1133 struct smbd_response *response = get_receive_buffer(info);
1135 response->type = SMBD_NEGOTIATE_RESP;
1136 rc = smbd_post_recv(info, response);
1137 log_rdma_event(INFO,
1138 "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x "
1140 rc, response->sge.addr,
1141 response->sge.length, response->sge.lkey);
1145 init_completion(&info->negotiate_completion);
1146 info->negotiate_done = false;
1147 rc = smbd_post_send_negotiate_req(info);
1151 rc = wait_for_completion_interruptible_timeout(
1152 &info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1153 log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1155 if (info->negotiate_done)
1160 else if (rc == -ERESTARTSYS)
1168 static void put_empty_packet(
1169 struct smbd_connection *info, struct smbd_response *response)
1171 spin_lock(&info->empty_packet_queue_lock);
1172 list_add_tail(&response->list, &info->empty_packet_queue);
1173 info->count_empty_packet_queue++;
1174 spin_unlock(&info->empty_packet_queue_lock);
1176 queue_work(info->workqueue, &info->post_send_credits_work);
1180 * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1181 * This is a queue for reassembling upper layer payload and present to upper
1182 * layer. All the inncoming payload go to the reassembly queue, regardless of
1183 * if reassembly is required. The uuper layer code reads from the queue for all
1184 * incoming payloads.
1185 * Put a received packet to the reassembly queue
1186 * response: the packet received
1187 * data_length: the size of payload in this packet
1189 static void enqueue_reassembly(
1190 struct smbd_connection *info,
1191 struct smbd_response *response,
1194 spin_lock(&info->reassembly_queue_lock);
1195 list_add_tail(&response->list, &info->reassembly_queue);
1196 info->reassembly_queue_length++;
1198 * Make sure reassembly_data_length is updated after list and
1199 * reassembly_queue_length are updated. On the dequeue side
1200 * reassembly_data_length is checked without a lock to determine
1201 * if reassembly_queue_length and list is up to date
1204 info->reassembly_data_length += data_length;
1205 spin_unlock(&info->reassembly_queue_lock);
1206 info->count_reassembly_queue++;
1207 info->count_enqueue_reassembly_queue++;
1211 * Get the first entry at the front of reassembly queue
1212 * Caller is responsible for locking
1213 * return value: the first entry if any, NULL if queue is empty
1215 static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1217 struct smbd_response *ret = NULL;
1219 if (!list_empty(&info->reassembly_queue)) {
1220 ret = list_first_entry(
1221 &info->reassembly_queue,
1222 struct smbd_response, list);
1227 static struct smbd_response *get_empty_queue_buffer(
1228 struct smbd_connection *info)
1230 struct smbd_response *ret = NULL;
1231 unsigned long flags;
1233 spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1234 if (!list_empty(&info->empty_packet_queue)) {
1235 ret = list_first_entry(
1236 &info->empty_packet_queue,
1237 struct smbd_response, list);
1238 list_del(&ret->list);
1239 info->count_empty_packet_queue--;
1241 spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1247 * Get a receive buffer
1248 * For each remote send, we need to post a receive. The receive buffers are
1249 * pre-allocated in advance.
1250 * return value: the receive buffer, NULL if none is available
1252 static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1254 struct smbd_response *ret = NULL;
1255 unsigned long flags;
1257 spin_lock_irqsave(&info->receive_queue_lock, flags);
1258 if (!list_empty(&info->receive_queue)) {
1259 ret = list_first_entry(
1260 &info->receive_queue,
1261 struct smbd_response, list);
1262 list_del(&ret->list);
1263 info->count_receive_queue--;
1264 info->count_get_receive_buffer++;
1266 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1272 * Return a receive buffer
1273 * Upon returning of a receive buffer, we can post new receive and extend
1274 * more receive credits to remote peer. This is done immediately after a
1275 * receive buffer is returned.
1277 static void put_receive_buffer(
1278 struct smbd_connection *info, struct smbd_response *response)
1280 unsigned long flags;
1282 ib_dma_unmap_single(info->id->device, response->sge.addr,
1283 response->sge.length, DMA_FROM_DEVICE);
1285 spin_lock_irqsave(&info->receive_queue_lock, flags);
1286 list_add_tail(&response->list, &info->receive_queue);
1287 info->count_receive_queue++;
1288 info->count_put_receive_buffer++;
1289 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1291 queue_work(info->workqueue, &info->post_send_credits_work);
1294 /* Preallocate all receive buffer on transport establishment */
1295 static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1298 struct smbd_response *response;
1300 INIT_LIST_HEAD(&info->reassembly_queue);
1301 spin_lock_init(&info->reassembly_queue_lock);
1302 info->reassembly_data_length = 0;
1303 info->reassembly_queue_length = 0;
1305 INIT_LIST_HEAD(&info->receive_queue);
1306 spin_lock_init(&info->receive_queue_lock);
1307 info->count_receive_queue = 0;
1309 INIT_LIST_HEAD(&info->empty_packet_queue);
1310 spin_lock_init(&info->empty_packet_queue_lock);
1311 info->count_empty_packet_queue = 0;
1313 init_waitqueue_head(&info->wait_receive_queues);
1315 for (i = 0; i < num_buf; i++) {
1316 response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1318 goto allocate_failed;
1320 response->info = info;
1321 list_add_tail(&response->list, &info->receive_queue);
1322 info->count_receive_queue++;
1328 while (!list_empty(&info->receive_queue)) {
1329 response = list_first_entry(
1330 &info->receive_queue,
1331 struct smbd_response, list);
1332 list_del(&response->list);
1333 info->count_receive_queue--;
1335 mempool_free(response, info->response_mempool);
1340 static void destroy_receive_buffers(struct smbd_connection *info)
1342 struct smbd_response *response;
1344 while ((response = get_receive_buffer(info)))
1345 mempool_free(response, info->response_mempool);
1347 while ((response = get_empty_queue_buffer(info)))
1348 mempool_free(response, info->response_mempool);
1352 * Check and send an immediate or keep alive packet
1353 * The condition to send those packets are defined in [MS-SMBD] 3.1.1.1
1354 * Connection.KeepaliveRequested and Connection.SendImmediate
1355 * The idea is to extend credits to server as soon as it becomes available
1357 static void send_immediate_work(struct work_struct *work)
1359 struct smbd_connection *info = container_of(
1360 work, struct smbd_connection,
1361 send_immediate_work.work);
1363 if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
1364 info->send_immediate) {
1365 log_keep_alive(INFO, "send an empty message\n");
1366 smbd_post_send_empty(info);
1370 /* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1371 static void idle_connection_timer(struct work_struct *work)
1373 struct smbd_connection *info = container_of(
1374 work, struct smbd_connection,
1375 idle_timer_work.work);
1377 if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1379 "error status info->keep_alive_requested=%d\n",
1380 info->keep_alive_requested);
1381 smbd_disconnect_rdma_connection(info);
1385 log_keep_alive(INFO, "about to send an empty idle message\n");
1386 smbd_post_send_empty(info);
1388 /* Setup the next idle timeout work */
1389 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1390 info->keep_alive_interval*HZ);
1393 /* Destroy this SMBD connection, called from upper layer */
1394 void smbd_destroy(struct smbd_connection *info)
1396 log_rdma_event(INFO, "destroying rdma session\n");
1398 /* Kick off the disconnection process */
1399 smbd_disconnect_rdma_connection(info);
1401 log_rdma_event(INFO, "wait for transport being destroyed\n");
1402 wait_event(info->wait_destroy,
1403 info->transport_status == SMBD_DESTROYED);
1405 destroy_workqueue(info->workqueue);
1410 * Reconnect this SMBD connection, called from upper layer
1411 * return value: 0 on success, or actual error code
1413 int smbd_reconnect(struct TCP_Server_Info *server)
1415 log_rdma_event(INFO, "reconnecting rdma session\n");
1417 if (!server->smbd_conn) {
1418 log_rdma_event(ERR, "rdma session already destroyed\n");
1423 * This is possible if transport is disconnected and we haven't received
1424 * notification from RDMA, but upper layer has detected timeout
1426 if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1427 log_rdma_event(INFO, "disconnecting transport\n");
1428 smbd_disconnect_rdma_connection(server->smbd_conn);
1431 /* wait until the transport is destroyed */
1432 wait_event(server->smbd_conn->wait_destroy,
1433 server->smbd_conn->transport_status == SMBD_DESTROYED);
1435 destroy_workqueue(server->smbd_conn->workqueue);
1436 kfree(server->smbd_conn);
1438 log_rdma_event(INFO, "creating rdma session\n");
1439 server->smbd_conn = smbd_get_connection(
1440 server, (struct sockaddr *) &server->dstaddr);
1442 return server->smbd_conn ? 0 : -ENOENT;
1445 static void destroy_caches_and_workqueue(struct smbd_connection *info)
1447 destroy_receive_buffers(info);
1448 destroy_workqueue(info->workqueue);
1449 mempool_destroy(info->response_mempool);
1450 kmem_cache_destroy(info->response_cache);
1451 mempool_destroy(info->request_mempool);
1452 kmem_cache_destroy(info->request_cache);
1455 #define MAX_NAME_LEN 80
1456 static int allocate_caches_and_workqueue(struct smbd_connection *info)
1458 char name[MAX_NAME_LEN];
1461 snprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
1462 info->request_cache =
1465 sizeof(struct smbd_request) +
1466 sizeof(struct smbd_data_transfer),
1467 0, SLAB_HWCACHE_ALIGN, NULL);
1468 if (!info->request_cache)
1471 info->request_mempool =
1472 mempool_create(info->send_credit_target, mempool_alloc_slab,
1473 mempool_free_slab, info->request_cache);
1474 if (!info->request_mempool)
1477 snprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
1478 info->response_cache =
1481 sizeof(struct smbd_response) +
1482 info->max_receive_size,
1483 0, SLAB_HWCACHE_ALIGN, NULL);
1484 if (!info->response_cache)
1487 info->response_mempool =
1488 mempool_create(info->receive_credit_max, mempool_alloc_slab,
1489 mempool_free_slab, info->response_cache);
1490 if (!info->response_mempool)
1493 snprintf(name, MAX_NAME_LEN, "smbd_%p", info);
1494 info->workqueue = create_workqueue(name);
1495 if (!info->workqueue)
1498 rc = allocate_receive_buffers(info, info->receive_credit_max);
1500 log_rdma_event(ERR, "failed to allocate receive buffers\n");
1507 destroy_workqueue(info->workqueue);
1509 mempool_destroy(info->response_mempool);
1511 kmem_cache_destroy(info->response_cache);
1513 mempool_destroy(info->request_mempool);
1515 kmem_cache_destroy(info->request_cache);
1519 /* Create a SMBD connection, called by upper layer */
1520 struct smbd_connection *_smbd_get_connection(
1521 struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1524 struct smbd_connection *info;
1525 struct rdma_conn_param conn_param;
1526 struct ib_qp_init_attr qp_attr;
1527 struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
1529 info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1533 info->transport_status = SMBD_CONNECTING;
1534 rc = smbd_ia_open(info, dstaddr, port);
1536 log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1537 goto create_id_failed;
1540 if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1541 smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1543 "consider lowering send_credit_target = %d. "
1544 "Possible CQE overrun, device "
1545 "reporting max_cpe %d max_qp_wr %d\n",
1546 smbd_send_credit_target,
1547 info->id->device->attrs.max_cqe,
1548 info->id->device->attrs.max_qp_wr);
1552 if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1553 smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1555 "consider lowering receive_credit_max = %d. "
1556 "Possible CQE overrun, device "
1557 "reporting max_cpe %d max_qp_wr %d\n",
1558 smbd_receive_credit_max,
1559 info->id->device->attrs.max_cqe,
1560 info->id->device->attrs.max_qp_wr);
1564 info->receive_credit_max = smbd_receive_credit_max;
1565 info->send_credit_target = smbd_send_credit_target;
1566 info->max_send_size = smbd_max_send_size;
1567 info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1568 info->max_receive_size = smbd_max_receive_size;
1569 info->keep_alive_interval = smbd_keep_alive_interval;
1571 if (info->id->device->attrs.max_sge < SMBDIRECT_MAX_SGE) {
1572 log_rdma_event(ERR, "warning: device max_sge = %d too small\n",
1573 info->id->device->attrs.max_sge);
1574 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1577 info->send_cq = NULL;
1578 info->recv_cq = NULL;
1579 info->send_cq = ib_alloc_cq(info->id->device, info,
1580 info->send_credit_target, 0, IB_POLL_SOFTIRQ);
1581 if (IS_ERR(info->send_cq)) {
1582 info->send_cq = NULL;
1583 goto alloc_cq_failed;
1586 info->recv_cq = ib_alloc_cq(info->id->device, info,
1587 info->receive_credit_max, 0, IB_POLL_SOFTIRQ);
1588 if (IS_ERR(info->recv_cq)) {
1589 info->recv_cq = NULL;
1590 goto alloc_cq_failed;
1593 memset(&qp_attr, 0, sizeof(qp_attr));
1594 qp_attr.event_handler = smbd_qp_async_error_upcall;
1595 qp_attr.qp_context = info;
1596 qp_attr.cap.max_send_wr = info->send_credit_target;
1597 qp_attr.cap.max_recv_wr = info->receive_credit_max;
1598 qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
1599 qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
1600 qp_attr.cap.max_inline_data = 0;
1601 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1602 qp_attr.qp_type = IB_QPT_RC;
1603 qp_attr.send_cq = info->send_cq;
1604 qp_attr.recv_cq = info->recv_cq;
1605 qp_attr.port_num = ~0;
1607 rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1609 log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1610 goto create_qp_failed;
1613 memset(&conn_param, 0, sizeof(conn_param));
1614 conn_param.initiator_depth = 0;
1616 conn_param.retry_count = SMBD_CM_RETRY;
1617 conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1618 conn_param.flow_control = 0;
1619 init_waitqueue_head(&info->wait_destroy);
1621 log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1622 &addr_in->sin_addr, port);
1624 init_waitqueue_head(&info->conn_wait);
1625 rc = rdma_connect(info->id, &conn_param);
1627 log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1628 goto rdma_connect_failed;
1631 wait_event_interruptible(
1632 info->conn_wait, info->transport_status != SMBD_CONNECTING);
1634 if (info->transport_status != SMBD_CONNECTED) {
1635 log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1636 goto rdma_connect_failed;
1639 log_rdma_event(INFO, "rdma_connect connected\n");
1641 rc = allocate_caches_and_workqueue(info);
1643 log_rdma_event(ERR, "cache allocation failed\n");
1644 goto allocate_cache_failed;
1647 init_waitqueue_head(&info->wait_send_queue);
1648 init_waitqueue_head(&info->wait_reassembly_queue);
1650 INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
1651 INIT_DELAYED_WORK(&info->send_immediate_work, send_immediate_work);
1652 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1653 info->keep_alive_interval*HZ);
1655 init_waitqueue_head(&info->wait_smbd_recv_pending);
1656 info->smbd_recv_pending = 0;
1658 init_waitqueue_head(&info->wait_send_pending);
1659 atomic_set(&info->send_pending, 0);
1661 init_waitqueue_head(&info->wait_send_payload_pending);
1662 atomic_set(&info->send_payload_pending, 0);
1664 INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
1665 INIT_WORK(&info->destroy_work, smbd_destroy_rdma_work);
1666 INIT_WORK(&info->recv_done_work, smbd_recv_done_work);
1667 INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1668 info->new_credits_offered = 0;
1669 spin_lock_init(&info->lock_new_credits_offered);
1671 rc = smbd_negotiate(info);
1673 log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1674 goto negotiation_failed;
1680 cancel_delayed_work_sync(&info->idle_timer_work);
1681 destroy_caches_and_workqueue(info);
1682 info->transport_status = SMBD_NEGOTIATE_FAILED;
1683 init_waitqueue_head(&info->conn_wait);
1684 rdma_disconnect(info->id);
1685 wait_event(info->conn_wait,
1686 info->transport_status == SMBD_DISCONNECTED);
1688 allocate_cache_failed:
1689 rdma_connect_failed:
1690 rdma_destroy_qp(info->id);
1695 ib_free_cq(info->send_cq);
1697 ib_free_cq(info->recv_cq);
1700 ib_dealloc_pd(info->pd);
1701 rdma_destroy_id(info->id);
1708 struct smbd_connection *smbd_get_connection(
1709 struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1711 struct smbd_connection *ret;
1712 int port = SMBD_PORT;
1715 ret = _smbd_get_connection(server, dstaddr, port);
1717 /* Try SMB_PORT if SMBD_PORT doesn't work */
1718 if (!ret && port == SMBD_PORT) {
1726 * Receive data from receive reassembly queue
1727 * All the incoming data packets are placed in reassembly queue
1728 * buf: the buffer to read data into
1729 * size: the length of data to read
1730 * return value: actual data read
1731 * Note: this implementation copies the data from reassebmly queue to receive
1732 * buffers used by upper layer. This is not the optimal code path. A better way
1733 * to do it is to not have upper layer allocate its receive buffers but rather
1734 * borrow the buffer from reassembly queue, and return it after data is
1735 * consumed. But this will require more changes to upper layer code, and also
1736 * need to consider packet boundaries while they still being reassembled.
1738 int smbd_recv_buf(struct smbd_connection *info, char *buf, unsigned int size)
1740 struct smbd_response *response;
1741 struct smbd_data_transfer *data_transfer;
1742 int to_copy, to_read, data_read, offset;
1743 u32 data_length, remaining_data_length, data_offset;
1745 unsigned long flags;
1748 if (info->transport_status != SMBD_CONNECTED) {
1749 log_read(ERR, "disconnected\n");
1754 * No need to hold the reassembly queue lock all the time as we are
1755 * the only one reading from the front of the queue. The transport
1756 * may add more entries to the back of the queue at the same time
1758 log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1759 info->reassembly_data_length);
1760 if (info->reassembly_data_length >= size) {
1762 int queue_removed = 0;
1765 * Need to make sure reassembly_data_length is read before
1766 * reading reassembly_queue_length and calling
1767 * _get_first_reassembly. This call is lock free
1768 * as we never read at the end of the queue which are being
1769 * updated in SOFTIRQ as more data is received
1772 queue_length = info->reassembly_queue_length;
1775 offset = info->first_entry_offset;
1776 while (data_read < size) {
1777 response = _get_first_reassembly(info);
1778 data_transfer = smbd_response_payload(response);
1779 data_length = le32_to_cpu(data_transfer->data_length);
1780 remaining_data_length =
1782 data_transfer->remaining_data_length);
1783 data_offset = le32_to_cpu(data_transfer->data_offset);
1786 * The upper layer expects RFC1002 length at the
1787 * beginning of the payload. Return it to indicate
1788 * the total length of the packet. This minimize the
1789 * change to upper layer packet processing logic. This
1790 * will be eventually remove when an intermediate
1791 * transport layer is added
1793 if (response->first_segment && size == 4) {
1794 unsigned int rfc1002_len =
1795 data_length + remaining_data_length;
1796 *((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1798 response->first_segment = false;
1799 log_read(INFO, "returning rfc1002 length %d\n",
1801 goto read_rfc1002_done;
1804 to_copy = min_t(int, data_length - offset, to_read);
1807 (char *)data_transfer + data_offset + offset,
1810 /* move on to the next buffer? */
1811 if (to_copy == data_length - offset) {
1814 * No need to lock if we are not at the
1819 &info->reassembly_queue_lock,
1821 list_del(&response->list);
1824 spin_unlock_irqrestore(
1825 &info->reassembly_queue_lock,
1828 info->count_reassembly_queue--;
1829 info->count_dequeue_reassembly_queue++;
1830 put_receive_buffer(info, response);
1832 log_read(INFO, "put_receive_buffer offset=0\n");
1837 data_read += to_copy;
1839 log_read(INFO, "_get_first_reassembly memcpy %d bytes "
1840 "data_transfer_length-offset=%d after that "
1841 "to_read=%d data_read=%d offset=%d\n",
1842 to_copy, data_length - offset,
1843 to_read, data_read, offset);
1846 spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1847 info->reassembly_data_length -= data_read;
1848 info->reassembly_queue_length -= queue_removed;
1849 spin_unlock_irqrestore(&info->reassembly_queue_lock, flags);
1851 info->first_entry_offset = offset;
1852 log_read(INFO, "returning to thread data_read=%d "
1853 "reassembly_data_length=%d first_entry_offset=%d\n",
1854 data_read, info->reassembly_data_length,
1855 info->first_entry_offset);
1860 log_read(INFO, "wait_event on more data\n");
1861 rc = wait_event_interruptible(
1862 info->wait_reassembly_queue,
1863 info->reassembly_data_length >= size ||
1864 info->transport_status != SMBD_CONNECTED);
1865 /* Don't return any data if interrupted */
1873 * Receive a page from receive reassembly queue
1874 * page: the page to read data into
1875 * to_read: the length of data to read
1876 * return value: actual data read
1878 int smbd_recv_page(struct smbd_connection *info,
1879 struct page *page, unsigned int to_read)
1884 /* make sure we have the page ready for read */
1885 ret = wait_event_interruptible(
1886 info->wait_reassembly_queue,
1887 info->reassembly_data_length >= to_read ||
1888 info->transport_status != SMBD_CONNECTED);
1892 /* now we can read from reassembly queue and not sleep */
1893 to_address = kmap_atomic(page);
1895 log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
1896 page, to_address, to_read);
1898 ret = smbd_recv_buf(info, to_address, to_read);
1899 kunmap_atomic(to_address);
1905 * Receive data from transport
1906 * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
1907 * return: total bytes read, or 0. SMB Direct will not do partial read.
1909 int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
1913 unsigned int to_read;
1916 info->smbd_recv_pending++;
1918 switch (msg->msg_iter.type) {
1919 case READ | ITER_KVEC:
1920 buf = msg->msg_iter.kvec->iov_base;
1921 to_read = msg->msg_iter.kvec->iov_len;
1922 rc = smbd_recv_buf(info, buf, to_read);
1925 case READ | ITER_BVEC:
1926 page = msg->msg_iter.bvec->bv_page;
1927 to_read = msg->msg_iter.bvec->bv_len;
1928 rc = smbd_recv_page(info, page, to_read);
1932 /* It's a bug in upper layer to get there */
1933 cifs_dbg(VFS, "CIFS: invalid msg type %d\n",
1934 msg->msg_iter.type);
1938 info->smbd_recv_pending--;
1939 wake_up(&info->wait_smbd_recv_pending);
1941 /* SMBDirect will read it all or nothing */
1943 msg->msg_iter.count = 0;