Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   Copyright (C) 2017, Microsoft Corporation.
0004  *
0005  *   Author(s): Long Li <longli@microsoft.com>
0006  */
0007 #include <linux/module.h>
0008 #include <linux/highmem.h>
0009 #include "smbdirect.h"
0010 #include "cifs_debug.h"
0011 #include "cifsproto.h"
0012 #include "smb2proto.h"
0013 
0014 static struct smbd_response *get_empty_queue_buffer(
0015         struct smbd_connection *info);
0016 static struct smbd_response *get_receive_buffer(
0017         struct smbd_connection *info);
0018 static void put_receive_buffer(
0019         struct smbd_connection *info,
0020         struct smbd_response *response);
0021 static int allocate_receive_buffers(struct smbd_connection *info, int num_buf);
0022 static void destroy_receive_buffers(struct smbd_connection *info);
0023 
0024 static void put_empty_packet(
0025         struct smbd_connection *info, struct smbd_response *response);
0026 static void enqueue_reassembly(
0027         struct smbd_connection *info,
0028         struct smbd_response *response, int data_length);
0029 static struct smbd_response *_get_first_reassembly(
0030         struct smbd_connection *info);
0031 
0032 static int smbd_post_recv(
0033         struct smbd_connection *info,
0034         struct smbd_response *response);
0035 
0036 static int smbd_post_send_empty(struct smbd_connection *info);
0037 static int smbd_post_send_data(
0038         struct smbd_connection *info,
0039         struct kvec *iov, int n_vec, int remaining_data_length);
0040 static int smbd_post_send_page(struct smbd_connection *info,
0041         struct page *page, unsigned long offset,
0042         size_t size, int remaining_data_length);
0043 
0044 static void destroy_mr_list(struct smbd_connection *info);
0045 static int allocate_mr_list(struct smbd_connection *info);
0046 
0047 /* SMBD version number */
0048 #define SMBD_V1 0x0100
0049 
0050 /* Port numbers for SMBD transport */
0051 #define SMB_PORT    445
0052 #define SMBD_PORT   5445
0053 
0054 /* Address lookup and resolve timeout in ms */
0055 #define RDMA_RESOLVE_TIMEOUT    5000
0056 
0057 /* SMBD negotiation timeout in seconds */
0058 #define SMBD_NEGOTIATE_TIMEOUT  120
0059 
0060 /* SMBD minimum receive size and fragmented sized defined in [MS-SMBD] */
0061 #define SMBD_MIN_RECEIVE_SIZE       128
0062 #define SMBD_MIN_FRAGMENTED_SIZE    131072
0063 
0064 /*
0065  * Default maximum number of RDMA read/write outstanding on this connection
0066  * This value is possibly decreased during QP creation on hardware limit
0067  */
0068 #define SMBD_CM_RESPONDER_RESOURCES 32
0069 
0070 /* Maximum number of retries on data transfer operations */
0071 #define SMBD_CM_RETRY           6
0072 /* No need to retry on Receiver Not Ready since SMBD manages credits */
0073 #define SMBD_CM_RNR_RETRY       0
0074 
0075 /*
0076  * User configurable initial values per SMBD transport connection
0077  * as defined in [MS-SMBD] 3.1.1.1
0078  * Those may change after a SMBD negotiation
0079  */
0080 /* The local peer's maximum number of credits to grant to the peer */
0081 int smbd_receive_credit_max = 255;
0082 
0083 /* The remote peer's credit request of local peer */
0084 int smbd_send_credit_target = 255;
0085 
0086 /* The maximum single message size can be sent to remote peer */
0087 int smbd_max_send_size = 1364;
0088 
0089 /*  The maximum fragmented upper-layer payload receive size supported */
0090 int smbd_max_fragmented_recv_size = 1024 * 1024;
0091 
0092 /*  The maximum single-message size which can be received */
0093 int smbd_max_receive_size = 8192;
0094 
0095 /* The timeout to initiate send of a keepalive message on idle */
0096 int smbd_keep_alive_interval = 120;
0097 
0098 /*
0099  * User configurable initial values for RDMA transport
0100  * The actual values used may be lower and are limited to hardware capabilities
0101  */
0102 /* Default maximum number of SGEs in a RDMA write/read */
0103 int smbd_max_frmr_depth = 2048;
0104 
0105 /* If payload is less than this byte, use RDMA send/recv not read/write */
0106 int rdma_readwrite_threshold = 4096;
0107 
0108 /* Transport logging functions
0109  * Logging are defined as classes. They can be OR'ed to define the actual
0110  * logging level via module parameter smbd_logging_class
0111  * e.g. cifs.smbd_logging_class=0xa0 will log all log_rdma_recv() and
0112  * log_rdma_event()
0113  */
0114 #define LOG_OUTGOING            0x1
0115 #define LOG_INCOMING            0x2
0116 #define LOG_READ            0x4
0117 #define LOG_WRITE           0x8
0118 #define LOG_RDMA_SEND           0x10
0119 #define LOG_RDMA_RECV           0x20
0120 #define LOG_KEEP_ALIVE          0x40
0121 #define LOG_RDMA_EVENT          0x80
0122 #define LOG_RDMA_MR         0x100
0123 static unsigned int smbd_logging_class;
0124 module_param(smbd_logging_class, uint, 0644);
0125 MODULE_PARM_DESC(smbd_logging_class,
0126     "Logging class for SMBD transport 0x0 to 0x100");
0127 
0128 #define ERR     0x0
0129 #define INFO        0x1
0130 static unsigned int smbd_logging_level = ERR;
0131 module_param(smbd_logging_level, uint, 0644);
0132 MODULE_PARM_DESC(smbd_logging_level,
0133     "Logging level for SMBD transport, 0 (default): error, 1: info");
0134 
0135 #define log_rdma(level, class, fmt, args...)                \
0136 do {                                    \
0137     if (level <= smbd_logging_level || class & smbd_logging_class)  \
0138         cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
0139 } while (0)
0140 
0141 #define log_outgoing(level, fmt, args...) \
0142         log_rdma(level, LOG_OUTGOING, fmt, ##args)
0143 #define log_incoming(level, fmt, args...) \
0144         log_rdma(level, LOG_INCOMING, fmt, ##args)
0145 #define log_read(level, fmt, args...)   log_rdma(level, LOG_READ, fmt, ##args)
0146 #define log_write(level, fmt, args...)  log_rdma(level, LOG_WRITE, fmt, ##args)
0147 #define log_rdma_send(level, fmt, args...) \
0148         log_rdma(level, LOG_RDMA_SEND, fmt, ##args)
0149 #define log_rdma_recv(level, fmt, args...) \
0150         log_rdma(level, LOG_RDMA_RECV, fmt, ##args)
0151 #define log_keep_alive(level, fmt, args...) \
0152         log_rdma(level, LOG_KEEP_ALIVE, fmt, ##args)
0153 #define log_rdma_event(level, fmt, args...) \
0154         log_rdma(level, LOG_RDMA_EVENT, fmt, ##args)
0155 #define log_rdma_mr(level, fmt, args...) \
0156         log_rdma(level, LOG_RDMA_MR, fmt, ##args)
0157 
0158 static void smbd_disconnect_rdma_work(struct work_struct *work)
0159 {
0160     struct smbd_connection *info =
0161         container_of(work, struct smbd_connection, disconnect_work);
0162 
0163     if (info->transport_status == SMBD_CONNECTED) {
0164         info->transport_status = SMBD_DISCONNECTING;
0165         rdma_disconnect(info->id);
0166     }
0167 }
0168 
0169 static void smbd_disconnect_rdma_connection(struct smbd_connection *info)
0170 {
0171     queue_work(info->workqueue, &info->disconnect_work);
0172 }
0173 
0174 /* Upcall from RDMA CM */
0175 static int smbd_conn_upcall(
0176         struct rdma_cm_id *id, struct rdma_cm_event *event)
0177 {
0178     struct smbd_connection *info = id->context;
0179 
0180     log_rdma_event(INFO, "event=%d status=%d\n",
0181         event->event, event->status);
0182 
0183     switch (event->event) {
0184     case RDMA_CM_EVENT_ADDR_RESOLVED:
0185     case RDMA_CM_EVENT_ROUTE_RESOLVED:
0186         info->ri_rc = 0;
0187         complete(&info->ri_done);
0188         break;
0189 
0190     case RDMA_CM_EVENT_ADDR_ERROR:
0191         info->ri_rc = -EHOSTUNREACH;
0192         complete(&info->ri_done);
0193         break;
0194 
0195     case RDMA_CM_EVENT_ROUTE_ERROR:
0196         info->ri_rc = -ENETUNREACH;
0197         complete(&info->ri_done);
0198         break;
0199 
0200     case RDMA_CM_EVENT_ESTABLISHED:
0201         log_rdma_event(INFO, "connected event=%d\n", event->event);
0202         info->transport_status = SMBD_CONNECTED;
0203         wake_up_interruptible(&info->conn_wait);
0204         break;
0205 
0206     case RDMA_CM_EVENT_CONNECT_ERROR:
0207     case RDMA_CM_EVENT_UNREACHABLE:
0208     case RDMA_CM_EVENT_REJECTED:
0209         log_rdma_event(INFO, "connecting failed event=%d\n", event->event);
0210         info->transport_status = SMBD_DISCONNECTED;
0211         wake_up_interruptible(&info->conn_wait);
0212         break;
0213 
0214     case RDMA_CM_EVENT_DEVICE_REMOVAL:
0215     case RDMA_CM_EVENT_DISCONNECTED:
0216         /* This happenes when we fail the negotiation */
0217         if (info->transport_status == SMBD_NEGOTIATE_FAILED) {
0218             info->transport_status = SMBD_DISCONNECTED;
0219             wake_up(&info->conn_wait);
0220             break;
0221         }
0222 
0223         info->transport_status = SMBD_DISCONNECTED;
0224         wake_up_interruptible(&info->disconn_wait);
0225         wake_up_interruptible(&info->wait_reassembly_queue);
0226         wake_up_interruptible_all(&info->wait_send_queue);
0227         break;
0228 
0229     default:
0230         break;
0231     }
0232 
0233     return 0;
0234 }
0235 
0236 /* Upcall from RDMA QP */
0237 static void
0238 smbd_qp_async_error_upcall(struct ib_event *event, void *context)
0239 {
0240     struct smbd_connection *info = context;
0241 
0242     log_rdma_event(ERR, "%s on device %s info %p\n",
0243         ib_event_msg(event->event), event->device->name, info);
0244 
0245     switch (event->event) {
0246     case IB_EVENT_CQ_ERR:
0247     case IB_EVENT_QP_FATAL:
0248         smbd_disconnect_rdma_connection(info);
0249         break;
0250 
0251     default:
0252         break;
0253     }
0254 }
0255 
0256 static inline void *smbd_request_payload(struct smbd_request *request)
0257 {
0258     return (void *)request->packet;
0259 }
0260 
0261 static inline void *smbd_response_payload(struct smbd_response *response)
0262 {
0263     return (void *)response->packet;
0264 }
0265 
0266 /* Called when a RDMA send is done */
0267 static void send_done(struct ib_cq *cq, struct ib_wc *wc)
0268 {
0269     int i;
0270     struct smbd_request *request =
0271         container_of(wc->wr_cqe, struct smbd_request, cqe);
0272 
0273     log_rdma_send(INFO, "smbd_request %p completed wc->status=%d\n",
0274         request, wc->status);
0275 
0276     if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
0277         log_rdma_send(ERR, "wc->status=%d wc->opcode=%d\n",
0278             wc->status, wc->opcode);
0279         smbd_disconnect_rdma_connection(request->info);
0280     }
0281 
0282     for (i = 0; i < request->num_sge; i++)
0283         ib_dma_unmap_single(request->info->id->device,
0284             request->sge[i].addr,
0285             request->sge[i].length,
0286             DMA_TO_DEVICE);
0287 
0288     if (atomic_dec_and_test(&request->info->send_pending))
0289         wake_up(&request->info->wait_send_pending);
0290 
0291     wake_up(&request->info->wait_post_send);
0292 
0293     mempool_free(request, request->info->request_mempool);
0294 }
0295 
0296 static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
0297 {
0298     log_rdma_event(INFO, "resp message min_version %u max_version %u negotiated_version %u credits_requested %u credits_granted %u status %u max_readwrite_size %u preferred_send_size %u max_receive_size %u max_fragmented_size %u\n",
0299                resp->min_version, resp->max_version,
0300                resp->negotiated_version, resp->credits_requested,
0301                resp->credits_granted, resp->status,
0302                resp->max_readwrite_size, resp->preferred_send_size,
0303                resp->max_receive_size, resp->max_fragmented_size);
0304 }
0305 
0306 /*
0307  * Process a negotiation response message, according to [MS-SMBD]3.1.5.7
0308  * response, packet_length: the negotiation response message
0309  * return value: true if negotiation is a success, false if failed
0310  */
0311 static bool process_negotiation_response(
0312         struct smbd_response *response, int packet_length)
0313 {
0314     struct smbd_connection *info = response->info;
0315     struct smbd_negotiate_resp *packet = smbd_response_payload(response);
0316 
0317     if (packet_length < sizeof(struct smbd_negotiate_resp)) {
0318         log_rdma_event(ERR,
0319             "error: packet_length=%d\n", packet_length);
0320         return false;
0321     }
0322 
0323     if (le16_to_cpu(packet->negotiated_version) != SMBD_V1) {
0324         log_rdma_event(ERR, "error: negotiated_version=%x\n",
0325             le16_to_cpu(packet->negotiated_version));
0326         return false;
0327     }
0328     info->protocol = le16_to_cpu(packet->negotiated_version);
0329 
0330     if (packet->credits_requested == 0) {
0331         log_rdma_event(ERR, "error: credits_requested==0\n");
0332         return false;
0333     }
0334     info->receive_credit_target = le16_to_cpu(packet->credits_requested);
0335 
0336     if (packet->credits_granted == 0) {
0337         log_rdma_event(ERR, "error: credits_granted==0\n");
0338         return false;
0339     }
0340     atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
0341 
0342     atomic_set(&info->receive_credits, 0);
0343 
0344     if (le32_to_cpu(packet->preferred_send_size) > info->max_receive_size) {
0345         log_rdma_event(ERR, "error: preferred_send_size=%d\n",
0346             le32_to_cpu(packet->preferred_send_size));
0347         return false;
0348     }
0349     info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
0350 
0351     if (le32_to_cpu(packet->max_receive_size) < SMBD_MIN_RECEIVE_SIZE) {
0352         log_rdma_event(ERR, "error: max_receive_size=%d\n",
0353             le32_to_cpu(packet->max_receive_size));
0354         return false;
0355     }
0356     info->max_send_size = min_t(int, info->max_send_size,
0357                     le32_to_cpu(packet->max_receive_size));
0358 
0359     if (le32_to_cpu(packet->max_fragmented_size) <
0360             SMBD_MIN_FRAGMENTED_SIZE) {
0361         log_rdma_event(ERR, "error: max_fragmented_size=%d\n",
0362             le32_to_cpu(packet->max_fragmented_size));
0363         return false;
0364     }
0365     info->max_fragmented_send_size =
0366         le32_to_cpu(packet->max_fragmented_size);
0367     info->rdma_readwrite_threshold =
0368         rdma_readwrite_threshold > info->max_fragmented_send_size ?
0369         info->max_fragmented_send_size :
0370         rdma_readwrite_threshold;
0371 
0372 
0373     info->max_readwrite_size = min_t(u32,
0374             le32_to_cpu(packet->max_readwrite_size),
0375             info->max_frmr_depth * PAGE_SIZE);
0376     info->max_frmr_depth = info->max_readwrite_size / PAGE_SIZE;
0377 
0378     return true;
0379 }
0380 
0381 static void smbd_post_send_credits(struct work_struct *work)
0382 {
0383     int ret = 0;
0384     int use_receive_queue = 1;
0385     int rc;
0386     struct smbd_response *response;
0387     struct smbd_connection *info =
0388         container_of(work, struct smbd_connection,
0389             post_send_credits_work);
0390 
0391     if (info->transport_status != SMBD_CONNECTED) {
0392         wake_up(&info->wait_receive_queues);
0393         return;
0394     }
0395 
0396     if (info->receive_credit_target >
0397         atomic_read(&info->receive_credits)) {
0398         while (true) {
0399             if (use_receive_queue)
0400                 response = get_receive_buffer(info);
0401             else
0402                 response = get_empty_queue_buffer(info);
0403             if (!response) {
0404                 /* now switch to emtpy packet queue */
0405                 if (use_receive_queue) {
0406                     use_receive_queue = 0;
0407                     continue;
0408                 } else
0409                     break;
0410             }
0411 
0412             response->type = SMBD_TRANSFER_DATA;
0413             response->first_segment = false;
0414             rc = smbd_post_recv(info, response);
0415             if (rc) {
0416                 log_rdma_recv(ERR,
0417                     "post_recv failed rc=%d\n", rc);
0418                 put_receive_buffer(info, response);
0419                 break;
0420             }
0421 
0422             ret++;
0423         }
0424     }
0425 
0426     spin_lock(&info->lock_new_credits_offered);
0427     info->new_credits_offered += ret;
0428     spin_unlock(&info->lock_new_credits_offered);
0429 
0430     /* Promptly send an immediate packet as defined in [MS-SMBD] 3.1.1.1 */
0431     info->send_immediate = true;
0432     if (atomic_read(&info->receive_credits) <
0433         info->receive_credit_target - 1) {
0434         if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
0435             info->send_immediate) {
0436             log_keep_alive(INFO, "send an empty message\n");
0437             smbd_post_send_empty(info);
0438         }
0439     }
0440 }
0441 
0442 /* Called from softirq, when recv is done */
0443 static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
0444 {
0445     struct smbd_data_transfer *data_transfer;
0446     struct smbd_response *response =
0447         container_of(wc->wr_cqe, struct smbd_response, cqe);
0448     struct smbd_connection *info = response->info;
0449     int data_length = 0;
0450 
0451     log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d byte_len=%d pkey_index=%x\n",
0452               response, response->type, wc->status, wc->opcode,
0453               wc->byte_len, wc->pkey_index);
0454 
0455     if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
0456         log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
0457             wc->status, wc->opcode);
0458         smbd_disconnect_rdma_connection(info);
0459         goto error;
0460     }
0461 
0462     ib_dma_sync_single_for_cpu(
0463         wc->qp->device,
0464         response->sge.addr,
0465         response->sge.length,
0466         DMA_FROM_DEVICE);
0467 
0468     switch (response->type) {
0469     /* SMBD negotiation response */
0470     case SMBD_NEGOTIATE_RESP:
0471         dump_smbd_negotiate_resp(smbd_response_payload(response));
0472         info->full_packet_received = true;
0473         info->negotiate_done =
0474             process_negotiation_response(response, wc->byte_len);
0475         complete(&info->negotiate_completion);
0476         break;
0477 
0478     /* SMBD data transfer packet */
0479     case SMBD_TRANSFER_DATA:
0480         data_transfer = smbd_response_payload(response);
0481         data_length = le32_to_cpu(data_transfer->data_length);
0482 
0483         /*
0484          * If this is a packet with data playload place the data in
0485          * reassembly queue and wake up the reading thread
0486          */
0487         if (data_length) {
0488             if (info->full_packet_received)
0489                 response->first_segment = true;
0490 
0491             if (le32_to_cpu(data_transfer->remaining_data_length))
0492                 info->full_packet_received = false;
0493             else
0494                 info->full_packet_received = true;
0495 
0496             enqueue_reassembly(
0497                 info,
0498                 response,
0499                 data_length);
0500         } else
0501             put_empty_packet(info, response);
0502 
0503         if (data_length)
0504             wake_up_interruptible(&info->wait_reassembly_queue);
0505 
0506         atomic_dec(&info->receive_credits);
0507         info->receive_credit_target =
0508             le16_to_cpu(data_transfer->credits_requested);
0509         if (le16_to_cpu(data_transfer->credits_granted)) {
0510             atomic_add(le16_to_cpu(data_transfer->credits_granted),
0511                 &info->send_credits);
0512             /*
0513              * We have new send credits granted from remote peer
0514              * If any sender is waiting for credits, unblock it
0515              */
0516             wake_up_interruptible(&info->wait_send_queue);
0517         }
0518 
0519         log_incoming(INFO, "data flags %d data_offset %d data_length %d remaining_data_length %d\n",
0520                  le16_to_cpu(data_transfer->flags),
0521                  le32_to_cpu(data_transfer->data_offset),
0522                  le32_to_cpu(data_transfer->data_length),
0523                  le32_to_cpu(data_transfer->remaining_data_length));
0524 
0525         /* Send a KEEP_ALIVE response right away if requested */
0526         info->keep_alive_requested = KEEP_ALIVE_NONE;
0527         if (le16_to_cpu(data_transfer->flags) &
0528                 SMB_DIRECT_RESPONSE_REQUESTED) {
0529             info->keep_alive_requested = KEEP_ALIVE_PENDING;
0530         }
0531 
0532         return;
0533 
0534     default:
0535         log_rdma_recv(ERR,
0536             "unexpected response type=%d\n", response->type);
0537     }
0538 
0539 error:
0540     put_receive_buffer(info, response);
0541 }
0542 
0543 static struct rdma_cm_id *smbd_create_id(
0544         struct smbd_connection *info,
0545         struct sockaddr *dstaddr, int port)
0546 {
0547     struct rdma_cm_id *id;
0548     int rc;
0549     __be16 *sport;
0550 
0551     id = rdma_create_id(&init_net, smbd_conn_upcall, info,
0552         RDMA_PS_TCP, IB_QPT_RC);
0553     if (IS_ERR(id)) {
0554         rc = PTR_ERR(id);
0555         log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
0556         return id;
0557     }
0558 
0559     if (dstaddr->sa_family == AF_INET6)
0560         sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
0561     else
0562         sport = &((struct sockaddr_in *)dstaddr)->sin_port;
0563 
0564     *sport = htons(port);
0565 
0566     init_completion(&info->ri_done);
0567     info->ri_rc = -ETIMEDOUT;
0568 
0569     rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
0570         RDMA_RESOLVE_TIMEOUT);
0571     if (rc) {
0572         log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
0573         goto out;
0574     }
0575     rc = wait_for_completion_interruptible_timeout(
0576         &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
0577     /* e.g. if interrupted returns -ERESTARTSYS */
0578     if (rc < 0) {
0579         log_rdma_event(ERR, "rdma_resolve_addr timeout rc: %i\n", rc);
0580         goto out;
0581     }
0582     rc = info->ri_rc;
0583     if (rc) {
0584         log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
0585         goto out;
0586     }
0587 
0588     info->ri_rc = -ETIMEDOUT;
0589     rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
0590     if (rc) {
0591         log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
0592         goto out;
0593     }
0594     rc = wait_for_completion_interruptible_timeout(
0595         &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
0596     /* e.g. if interrupted returns -ERESTARTSYS */
0597     if (rc < 0)  {
0598         log_rdma_event(ERR, "rdma_resolve_addr timeout rc: %i\n", rc);
0599         goto out;
0600     }
0601     rc = info->ri_rc;
0602     if (rc) {
0603         log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
0604         goto out;
0605     }
0606 
0607     return id;
0608 
0609 out:
0610     rdma_destroy_id(id);
0611     return ERR_PTR(rc);
0612 }
0613 
0614 /*
0615  * Test if FRWR (Fast Registration Work Requests) is supported on the device
0616  * This implementation requries FRWR on RDMA read/write
0617  * return value: true if it is supported
0618  */
0619 static bool frwr_is_supported(struct ib_device_attr *attrs)
0620 {
0621     if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
0622         return false;
0623     if (attrs->max_fast_reg_page_list_len == 0)
0624         return false;
0625     return true;
0626 }
0627 
0628 static int smbd_ia_open(
0629         struct smbd_connection *info,
0630         struct sockaddr *dstaddr, int port)
0631 {
0632     int rc;
0633 
0634     info->id = smbd_create_id(info, dstaddr, port);
0635     if (IS_ERR(info->id)) {
0636         rc = PTR_ERR(info->id);
0637         goto out1;
0638     }
0639 
0640     if (!frwr_is_supported(&info->id->device->attrs)) {
0641         log_rdma_event(ERR, "Fast Registration Work Requests (FRWR) is not supported\n");
0642         log_rdma_event(ERR, "Device capability flags = %llx max_fast_reg_page_list_len = %u\n",
0643                    info->id->device->attrs.device_cap_flags,
0644                    info->id->device->attrs.max_fast_reg_page_list_len);
0645         rc = -EPROTONOSUPPORT;
0646         goto out2;
0647     }
0648     info->max_frmr_depth = min_t(int,
0649         smbd_max_frmr_depth,
0650         info->id->device->attrs.max_fast_reg_page_list_len);
0651     info->mr_type = IB_MR_TYPE_MEM_REG;
0652     if (info->id->device->attrs.kernel_cap_flags & IBK_SG_GAPS_REG)
0653         info->mr_type = IB_MR_TYPE_SG_GAPS;
0654 
0655     info->pd = ib_alloc_pd(info->id->device, 0);
0656     if (IS_ERR(info->pd)) {
0657         rc = PTR_ERR(info->pd);
0658         log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
0659         goto out2;
0660     }
0661 
0662     return 0;
0663 
0664 out2:
0665     rdma_destroy_id(info->id);
0666     info->id = NULL;
0667 
0668 out1:
0669     return rc;
0670 }
0671 
0672 /*
0673  * Send a negotiation request message to the peer
0674  * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
0675  * After negotiation, the transport is connected and ready for
0676  * carrying upper layer SMB payload
0677  */
0678 static int smbd_post_send_negotiate_req(struct smbd_connection *info)
0679 {
0680     struct ib_send_wr send_wr;
0681     int rc = -ENOMEM;
0682     struct smbd_request *request;
0683     struct smbd_negotiate_req *packet;
0684 
0685     request = mempool_alloc(info->request_mempool, GFP_KERNEL);
0686     if (!request)
0687         return rc;
0688 
0689     request->info = info;
0690 
0691     packet = smbd_request_payload(request);
0692     packet->min_version = cpu_to_le16(SMBD_V1);
0693     packet->max_version = cpu_to_le16(SMBD_V1);
0694     packet->reserved = 0;
0695     packet->credits_requested = cpu_to_le16(info->send_credit_target);
0696     packet->preferred_send_size = cpu_to_le32(info->max_send_size);
0697     packet->max_receive_size = cpu_to_le32(info->max_receive_size);
0698     packet->max_fragmented_size =
0699         cpu_to_le32(info->max_fragmented_recv_size);
0700 
0701     request->num_sge = 1;
0702     request->sge[0].addr = ib_dma_map_single(
0703                 info->id->device, (void *)packet,
0704                 sizeof(*packet), DMA_TO_DEVICE);
0705     if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
0706         rc = -EIO;
0707         goto dma_mapping_failed;
0708     }
0709 
0710     request->sge[0].length = sizeof(*packet);
0711     request->sge[0].lkey = info->pd->local_dma_lkey;
0712 
0713     ib_dma_sync_single_for_device(
0714         info->id->device, request->sge[0].addr,
0715         request->sge[0].length, DMA_TO_DEVICE);
0716 
0717     request->cqe.done = send_done;
0718 
0719     send_wr.next = NULL;
0720     send_wr.wr_cqe = &request->cqe;
0721     send_wr.sg_list = request->sge;
0722     send_wr.num_sge = request->num_sge;
0723     send_wr.opcode = IB_WR_SEND;
0724     send_wr.send_flags = IB_SEND_SIGNALED;
0725 
0726     log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
0727         request->sge[0].addr,
0728         request->sge[0].length, request->sge[0].lkey);
0729 
0730     atomic_inc(&info->send_pending);
0731     rc = ib_post_send(info->id->qp, &send_wr, NULL);
0732     if (!rc)
0733         return 0;
0734 
0735     /* if we reach here, post send failed */
0736     log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
0737     atomic_dec(&info->send_pending);
0738     ib_dma_unmap_single(info->id->device, request->sge[0].addr,
0739         request->sge[0].length, DMA_TO_DEVICE);
0740 
0741     smbd_disconnect_rdma_connection(info);
0742 
0743 dma_mapping_failed:
0744     mempool_free(request, info->request_mempool);
0745     return rc;
0746 }
0747 
0748 /*
0749  * Extend the credits to remote peer
0750  * This implements [MS-SMBD] 3.1.5.9
0751  * The idea is that we should extend credits to remote peer as quickly as
0752  * it's allowed, to maintain data flow. We allocate as much receive
0753  * buffer as possible, and extend the receive credits to remote peer
0754  * return value: the new credtis being granted.
0755  */
0756 static int manage_credits_prior_sending(struct smbd_connection *info)
0757 {
0758     int new_credits;
0759 
0760     spin_lock(&info->lock_new_credits_offered);
0761     new_credits = info->new_credits_offered;
0762     info->new_credits_offered = 0;
0763     spin_unlock(&info->lock_new_credits_offered);
0764 
0765     return new_credits;
0766 }
0767 
0768 /*
0769  * Check if we need to send a KEEP_ALIVE message
0770  * The idle connection timer triggers a KEEP_ALIVE message when expires
0771  * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
0772  * back a response.
0773  * return value:
0774  * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
0775  * 0: otherwise
0776  */
0777 static int manage_keep_alive_before_sending(struct smbd_connection *info)
0778 {
0779     if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
0780         info->keep_alive_requested = KEEP_ALIVE_SENT;
0781         return 1;
0782     }
0783     return 0;
0784 }
0785 
0786 /* Post the send request */
0787 static int smbd_post_send(struct smbd_connection *info,
0788         struct smbd_request *request)
0789 {
0790     struct ib_send_wr send_wr;
0791     int rc, i;
0792 
0793     for (i = 0; i < request->num_sge; i++) {
0794         log_rdma_send(INFO,
0795             "rdma_request sge[%d] addr=%llu length=%u\n",
0796             i, request->sge[i].addr, request->sge[i].length);
0797         ib_dma_sync_single_for_device(
0798             info->id->device,
0799             request->sge[i].addr,
0800             request->sge[i].length,
0801             DMA_TO_DEVICE);
0802     }
0803 
0804     request->cqe.done = send_done;
0805 
0806     send_wr.next = NULL;
0807     send_wr.wr_cqe = &request->cqe;
0808     send_wr.sg_list = request->sge;
0809     send_wr.num_sge = request->num_sge;
0810     send_wr.opcode = IB_WR_SEND;
0811     send_wr.send_flags = IB_SEND_SIGNALED;
0812 
0813     rc = ib_post_send(info->id->qp, &send_wr, NULL);
0814     if (rc) {
0815         log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
0816         smbd_disconnect_rdma_connection(info);
0817         rc = -EAGAIN;
0818     } else
0819         /* Reset timer for idle connection after packet is sent */
0820         mod_delayed_work(info->workqueue, &info->idle_timer_work,
0821             info->keep_alive_interval*HZ);
0822 
0823     return rc;
0824 }
0825 
0826 static int smbd_post_send_sgl(struct smbd_connection *info,
0827     struct scatterlist *sgl, int data_length, int remaining_data_length)
0828 {
0829     int num_sgs;
0830     int i, rc;
0831     int header_length;
0832     struct smbd_request *request;
0833     struct smbd_data_transfer *packet;
0834     int new_credits;
0835     struct scatterlist *sg;
0836 
0837 wait_credit:
0838     /* Wait for send credits. A SMBD packet needs one credit */
0839     rc = wait_event_interruptible(info->wait_send_queue,
0840         atomic_read(&info->send_credits) > 0 ||
0841         info->transport_status != SMBD_CONNECTED);
0842     if (rc)
0843         goto err_wait_credit;
0844 
0845     if (info->transport_status != SMBD_CONNECTED) {
0846         log_outgoing(ERR, "disconnected not sending on wait_credit\n");
0847         rc = -EAGAIN;
0848         goto err_wait_credit;
0849     }
0850     if (unlikely(atomic_dec_return(&info->send_credits) < 0)) {
0851         atomic_inc(&info->send_credits);
0852         goto wait_credit;
0853     }
0854 
0855 wait_send_queue:
0856     wait_event(info->wait_post_send,
0857         atomic_read(&info->send_pending) < info->send_credit_target ||
0858         info->transport_status != SMBD_CONNECTED);
0859 
0860     if (info->transport_status != SMBD_CONNECTED) {
0861         log_outgoing(ERR, "disconnected not sending on wait_send_queue\n");
0862         rc = -EAGAIN;
0863         goto err_wait_send_queue;
0864     }
0865 
0866     if (unlikely(atomic_inc_return(&info->send_pending) >
0867                 info->send_credit_target)) {
0868         atomic_dec(&info->send_pending);
0869         goto wait_send_queue;
0870     }
0871 
0872     request = mempool_alloc(info->request_mempool, GFP_KERNEL);
0873     if (!request) {
0874         rc = -ENOMEM;
0875         goto err_alloc;
0876     }
0877 
0878     request->info = info;
0879 
0880     /* Fill in the packet header */
0881     packet = smbd_request_payload(request);
0882     packet->credits_requested = cpu_to_le16(info->send_credit_target);
0883 
0884     new_credits = manage_credits_prior_sending(info);
0885     atomic_add(new_credits, &info->receive_credits);
0886     packet->credits_granted = cpu_to_le16(new_credits);
0887 
0888     info->send_immediate = false;
0889 
0890     packet->flags = 0;
0891     if (manage_keep_alive_before_sending(info))
0892         packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
0893 
0894     packet->reserved = 0;
0895     if (!data_length)
0896         packet->data_offset = 0;
0897     else
0898         packet->data_offset = cpu_to_le32(24);
0899     packet->data_length = cpu_to_le32(data_length);
0900     packet->remaining_data_length = cpu_to_le32(remaining_data_length);
0901     packet->padding = 0;
0902 
0903     log_outgoing(INFO, "credits_requested=%d credits_granted=%d data_offset=%d data_length=%d remaining_data_length=%d\n",
0904              le16_to_cpu(packet->credits_requested),
0905              le16_to_cpu(packet->credits_granted),
0906              le32_to_cpu(packet->data_offset),
0907              le32_to_cpu(packet->data_length),
0908              le32_to_cpu(packet->remaining_data_length));
0909 
0910     /* Map the packet to DMA */
0911     header_length = sizeof(struct smbd_data_transfer);
0912     /* If this is a packet without payload, don't send padding */
0913     if (!data_length)
0914         header_length = offsetof(struct smbd_data_transfer, padding);
0915 
0916     request->num_sge = 1;
0917     request->sge[0].addr = ib_dma_map_single(info->id->device,
0918                          (void *)packet,
0919                          header_length,
0920                          DMA_TO_DEVICE);
0921     if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
0922         rc = -EIO;
0923         request->sge[0].addr = 0;
0924         goto err_dma;
0925     }
0926 
0927     request->sge[0].length = header_length;
0928     request->sge[0].lkey = info->pd->local_dma_lkey;
0929 
0930     /* Fill in the packet data payload */
0931     num_sgs = sgl ? sg_nents(sgl) : 0;
0932     for_each_sg(sgl, sg, num_sgs, i) {
0933         request->sge[i+1].addr =
0934             ib_dma_map_page(info->id->device, sg_page(sg),
0935                    sg->offset, sg->length, DMA_TO_DEVICE);
0936         if (ib_dma_mapping_error(
0937                 info->id->device, request->sge[i+1].addr)) {
0938             rc = -EIO;
0939             request->sge[i+1].addr = 0;
0940             goto err_dma;
0941         }
0942         request->sge[i+1].length = sg->length;
0943         request->sge[i+1].lkey = info->pd->local_dma_lkey;
0944         request->num_sge++;
0945     }
0946 
0947     rc = smbd_post_send(info, request);
0948     if (!rc)
0949         return 0;
0950 
0951 err_dma:
0952     for (i = 0; i < request->num_sge; i++)
0953         if (request->sge[i].addr)
0954             ib_dma_unmap_single(info->id->device,
0955                         request->sge[i].addr,
0956                         request->sge[i].length,
0957                         DMA_TO_DEVICE);
0958     mempool_free(request, info->request_mempool);
0959 
0960     /* roll back receive credits and credits to be offered */
0961     spin_lock(&info->lock_new_credits_offered);
0962     info->new_credits_offered += new_credits;
0963     spin_unlock(&info->lock_new_credits_offered);
0964     atomic_sub(new_credits, &info->receive_credits);
0965 
0966 err_alloc:
0967     if (atomic_dec_and_test(&info->send_pending))
0968         wake_up(&info->wait_send_pending);
0969 
0970 err_wait_send_queue:
0971     /* roll back send credits and pending */
0972     atomic_inc(&info->send_credits);
0973 
0974 err_wait_credit:
0975     return rc;
0976 }
0977 
0978 /*
0979  * Send a page
0980  * page: the page to send
0981  * offset: offset in the page to send
0982  * size: length in the page to send
0983  * remaining_data_length: remaining data to send in this payload
0984  */
0985 static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
0986         unsigned long offset, size_t size, int remaining_data_length)
0987 {
0988     struct scatterlist sgl;
0989 
0990     sg_init_table(&sgl, 1);
0991     sg_set_page(&sgl, page, size, offset);
0992 
0993     return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
0994 }
0995 
0996 /*
0997  * Send an empty message
0998  * Empty message is used to extend credits to peer to for keep live
0999  * while there is no upper layer payload to send at the time
1000  */
1001 static int smbd_post_send_empty(struct smbd_connection *info)
1002 {
1003     info->count_send_empty++;
1004     return smbd_post_send_sgl(info, NULL, 0, 0);
1005 }
1006 
1007 /*
1008  * Send a data buffer
1009  * iov: the iov array describing the data buffers
1010  * n_vec: number of iov array
1011  * remaining_data_length: remaining data to send following this packet
1012  * in segmented SMBD packet
1013  */
1014 static int smbd_post_send_data(
1015     struct smbd_connection *info, struct kvec *iov, int n_vec,
1016     int remaining_data_length)
1017 {
1018     int i;
1019     u32 data_length = 0;
1020     struct scatterlist sgl[SMBDIRECT_MAX_SGE];
1021 
1022     if (n_vec > SMBDIRECT_MAX_SGE) {
1023         cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
1024         return -EINVAL;
1025     }
1026 
1027     sg_init_table(sgl, n_vec);
1028     for (i = 0; i < n_vec; i++) {
1029         data_length += iov[i].iov_len;
1030         sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
1031     }
1032 
1033     return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
1034 }
1035 
1036 /*
1037  * Post a receive request to the transport
1038  * The remote peer can only send data when a receive request is posted
1039  * The interaction is controlled by send/receive credit system
1040  */
1041 static int smbd_post_recv(
1042         struct smbd_connection *info, struct smbd_response *response)
1043 {
1044     struct ib_recv_wr recv_wr;
1045     int rc = -EIO;
1046 
1047     response->sge.addr = ib_dma_map_single(
1048                 info->id->device, response->packet,
1049                 info->max_receive_size, DMA_FROM_DEVICE);
1050     if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1051         return rc;
1052 
1053     response->sge.length = info->max_receive_size;
1054     response->sge.lkey = info->pd->local_dma_lkey;
1055 
1056     response->cqe.done = recv_done;
1057 
1058     recv_wr.wr_cqe = &response->cqe;
1059     recv_wr.next = NULL;
1060     recv_wr.sg_list = &response->sge;
1061     recv_wr.num_sge = 1;
1062 
1063     rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
1064     if (rc) {
1065         ib_dma_unmap_single(info->id->device, response->sge.addr,
1066                     response->sge.length, DMA_FROM_DEVICE);
1067         smbd_disconnect_rdma_connection(info);
1068         log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1069     }
1070 
1071     return rc;
1072 }
1073 
1074 /* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1075 static int smbd_negotiate(struct smbd_connection *info)
1076 {
1077     int rc;
1078     struct smbd_response *response = get_receive_buffer(info);
1079 
1080     response->type = SMBD_NEGOTIATE_RESP;
1081     rc = smbd_post_recv(info, response);
1082     log_rdma_event(INFO, "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x iov.lkey=%x\n",
1083                rc, response->sge.addr,
1084                response->sge.length, response->sge.lkey);
1085     if (rc)
1086         return rc;
1087 
1088     init_completion(&info->negotiate_completion);
1089     info->negotiate_done = false;
1090     rc = smbd_post_send_negotiate_req(info);
1091     if (rc)
1092         return rc;
1093 
1094     rc = wait_for_completion_interruptible_timeout(
1095         &info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1096     log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1097 
1098     if (info->negotiate_done)
1099         return 0;
1100 
1101     if (rc == 0)
1102         rc = -ETIMEDOUT;
1103     else if (rc == -ERESTARTSYS)
1104         rc = -EINTR;
1105     else
1106         rc = -ENOTCONN;
1107 
1108     return rc;
1109 }
1110 
1111 static void put_empty_packet(
1112         struct smbd_connection *info, struct smbd_response *response)
1113 {
1114     spin_lock(&info->empty_packet_queue_lock);
1115     list_add_tail(&response->list, &info->empty_packet_queue);
1116     info->count_empty_packet_queue++;
1117     spin_unlock(&info->empty_packet_queue_lock);
1118 
1119     queue_work(info->workqueue, &info->post_send_credits_work);
1120 }
1121 
1122 /*
1123  * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1124  * This is a queue for reassembling upper layer payload and present to upper
1125  * layer. All the inncoming payload go to the reassembly queue, regardless of
1126  * if reassembly is required. The uuper layer code reads from the queue for all
1127  * incoming payloads.
1128  * Put a received packet to the reassembly queue
1129  * response: the packet received
1130  * data_length: the size of payload in this packet
1131  */
1132 static void enqueue_reassembly(
1133     struct smbd_connection *info,
1134     struct smbd_response *response,
1135     int data_length)
1136 {
1137     spin_lock(&info->reassembly_queue_lock);
1138     list_add_tail(&response->list, &info->reassembly_queue);
1139     info->reassembly_queue_length++;
1140     /*
1141      * Make sure reassembly_data_length is updated after list and
1142      * reassembly_queue_length are updated. On the dequeue side
1143      * reassembly_data_length is checked without a lock to determine
1144      * if reassembly_queue_length and list is up to date
1145      */
1146     virt_wmb();
1147     info->reassembly_data_length += data_length;
1148     spin_unlock(&info->reassembly_queue_lock);
1149     info->count_reassembly_queue++;
1150     info->count_enqueue_reassembly_queue++;
1151 }
1152 
1153 /*
1154  * Get the first entry at the front of reassembly queue
1155  * Caller is responsible for locking
1156  * return value: the first entry if any, NULL if queue is empty
1157  */
1158 static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1159 {
1160     struct smbd_response *ret = NULL;
1161 
1162     if (!list_empty(&info->reassembly_queue)) {
1163         ret = list_first_entry(
1164             &info->reassembly_queue,
1165             struct smbd_response, list);
1166     }
1167     return ret;
1168 }
1169 
1170 static struct smbd_response *get_empty_queue_buffer(
1171         struct smbd_connection *info)
1172 {
1173     struct smbd_response *ret = NULL;
1174     unsigned long flags;
1175 
1176     spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1177     if (!list_empty(&info->empty_packet_queue)) {
1178         ret = list_first_entry(
1179             &info->empty_packet_queue,
1180             struct smbd_response, list);
1181         list_del(&ret->list);
1182         info->count_empty_packet_queue--;
1183     }
1184     spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1185 
1186     return ret;
1187 }
1188 
1189 /*
1190  * Get a receive buffer
1191  * For each remote send, we need to post a receive. The receive buffers are
1192  * pre-allocated in advance.
1193  * return value: the receive buffer, NULL if none is available
1194  */
1195 static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1196 {
1197     struct smbd_response *ret = NULL;
1198     unsigned long flags;
1199 
1200     spin_lock_irqsave(&info->receive_queue_lock, flags);
1201     if (!list_empty(&info->receive_queue)) {
1202         ret = list_first_entry(
1203             &info->receive_queue,
1204             struct smbd_response, list);
1205         list_del(&ret->list);
1206         info->count_receive_queue--;
1207         info->count_get_receive_buffer++;
1208     }
1209     spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1210 
1211     return ret;
1212 }
1213 
1214 /*
1215  * Return a receive buffer
1216  * Upon returning of a receive buffer, we can post new receive and extend
1217  * more receive credits to remote peer. This is done immediately after a
1218  * receive buffer is returned.
1219  */
1220 static void put_receive_buffer(
1221     struct smbd_connection *info, struct smbd_response *response)
1222 {
1223     unsigned long flags;
1224 
1225     ib_dma_unmap_single(info->id->device, response->sge.addr,
1226         response->sge.length, DMA_FROM_DEVICE);
1227 
1228     spin_lock_irqsave(&info->receive_queue_lock, flags);
1229     list_add_tail(&response->list, &info->receive_queue);
1230     info->count_receive_queue++;
1231     info->count_put_receive_buffer++;
1232     spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1233 
1234     queue_work(info->workqueue, &info->post_send_credits_work);
1235 }
1236 
1237 /* Preallocate all receive buffer on transport establishment */
1238 static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1239 {
1240     int i;
1241     struct smbd_response *response;
1242 
1243     INIT_LIST_HEAD(&info->reassembly_queue);
1244     spin_lock_init(&info->reassembly_queue_lock);
1245     info->reassembly_data_length = 0;
1246     info->reassembly_queue_length = 0;
1247 
1248     INIT_LIST_HEAD(&info->receive_queue);
1249     spin_lock_init(&info->receive_queue_lock);
1250     info->count_receive_queue = 0;
1251 
1252     INIT_LIST_HEAD(&info->empty_packet_queue);
1253     spin_lock_init(&info->empty_packet_queue_lock);
1254     info->count_empty_packet_queue = 0;
1255 
1256     init_waitqueue_head(&info->wait_receive_queues);
1257 
1258     for (i = 0; i < num_buf; i++) {
1259         response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1260         if (!response)
1261             goto allocate_failed;
1262 
1263         response->info = info;
1264         list_add_tail(&response->list, &info->receive_queue);
1265         info->count_receive_queue++;
1266     }
1267 
1268     return 0;
1269 
1270 allocate_failed:
1271     while (!list_empty(&info->receive_queue)) {
1272         response = list_first_entry(
1273                 &info->receive_queue,
1274                 struct smbd_response, list);
1275         list_del(&response->list);
1276         info->count_receive_queue--;
1277 
1278         mempool_free(response, info->response_mempool);
1279     }
1280     return -ENOMEM;
1281 }
1282 
1283 static void destroy_receive_buffers(struct smbd_connection *info)
1284 {
1285     struct smbd_response *response;
1286 
1287     while ((response = get_receive_buffer(info)))
1288         mempool_free(response, info->response_mempool);
1289 
1290     while ((response = get_empty_queue_buffer(info)))
1291         mempool_free(response, info->response_mempool);
1292 }
1293 
1294 /* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1295 static void idle_connection_timer(struct work_struct *work)
1296 {
1297     struct smbd_connection *info = container_of(
1298                     work, struct smbd_connection,
1299                     idle_timer_work.work);
1300 
1301     if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1302         log_keep_alive(ERR,
1303             "error status info->keep_alive_requested=%d\n",
1304             info->keep_alive_requested);
1305         smbd_disconnect_rdma_connection(info);
1306         return;
1307     }
1308 
1309     log_keep_alive(INFO, "about to send an empty idle message\n");
1310     smbd_post_send_empty(info);
1311 
1312     /* Setup the next idle timeout work */
1313     queue_delayed_work(info->workqueue, &info->idle_timer_work,
1314             info->keep_alive_interval*HZ);
1315 }
1316 
1317 /*
1318  * Destroy the transport and related RDMA and memory resources
1319  * Need to go through all the pending counters and make sure on one is using
1320  * the transport while it is destroyed
1321  */
1322 void smbd_destroy(struct TCP_Server_Info *server)
1323 {
1324     struct smbd_connection *info = server->smbd_conn;
1325     struct smbd_response *response;
1326     unsigned long flags;
1327 
1328     if (!info) {
1329         log_rdma_event(INFO, "rdma session already destroyed\n");
1330         return;
1331     }
1332 
1333     log_rdma_event(INFO, "destroying rdma session\n");
1334     if (info->transport_status != SMBD_DISCONNECTED) {
1335         rdma_disconnect(server->smbd_conn->id);
1336         log_rdma_event(INFO, "wait for transport being disconnected\n");
1337         wait_event_interruptible(
1338             info->disconn_wait,
1339             info->transport_status == SMBD_DISCONNECTED);
1340     }
1341 
1342     log_rdma_event(INFO, "destroying qp\n");
1343     ib_drain_qp(info->id->qp);
1344     rdma_destroy_qp(info->id);
1345 
1346     log_rdma_event(INFO, "cancelling idle timer\n");
1347     cancel_delayed_work_sync(&info->idle_timer_work);
1348 
1349     log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
1350     wait_event(info->wait_send_pending,
1351         atomic_read(&info->send_pending) == 0);
1352 
1353     /* It's not possible for upper layer to get to reassembly */
1354     log_rdma_event(INFO, "drain the reassembly queue\n");
1355     do {
1356         spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1357         response = _get_first_reassembly(info);
1358         if (response) {
1359             list_del(&response->list);
1360             spin_unlock_irqrestore(
1361                 &info->reassembly_queue_lock, flags);
1362             put_receive_buffer(info, response);
1363         } else
1364             spin_unlock_irqrestore(
1365                 &info->reassembly_queue_lock, flags);
1366     } while (response);
1367     info->reassembly_data_length = 0;
1368 
1369     log_rdma_event(INFO, "free receive buffers\n");
1370     wait_event(info->wait_receive_queues,
1371         info->count_receive_queue + info->count_empty_packet_queue
1372             == info->receive_credit_max);
1373     destroy_receive_buffers(info);
1374 
1375     /*
1376      * For performance reasons, memory registration and deregistration
1377      * are not locked by srv_mutex. It is possible some processes are
1378      * blocked on transport srv_mutex while holding memory registration.
1379      * Release the transport srv_mutex to allow them to hit the failure
1380      * path when sending data, and then release memory registartions.
1381      */
1382     log_rdma_event(INFO, "freeing mr list\n");
1383     wake_up_interruptible_all(&info->wait_mr);
1384     while (atomic_read(&info->mr_used_count)) {
1385         cifs_server_unlock(server);
1386         msleep(1000);
1387         cifs_server_lock(server);
1388     }
1389     destroy_mr_list(info);
1390 
1391     ib_free_cq(info->send_cq);
1392     ib_free_cq(info->recv_cq);
1393     ib_dealloc_pd(info->pd);
1394     rdma_destroy_id(info->id);
1395 
1396     /* free mempools */
1397     mempool_destroy(info->request_mempool);
1398     kmem_cache_destroy(info->request_cache);
1399 
1400     mempool_destroy(info->response_mempool);
1401     kmem_cache_destroy(info->response_cache);
1402 
1403     info->transport_status = SMBD_DESTROYED;
1404 
1405     destroy_workqueue(info->workqueue);
1406     log_rdma_event(INFO,  "rdma session destroyed\n");
1407     kfree(info);
1408 }
1409 
1410 /*
1411  * Reconnect this SMBD connection, called from upper layer
1412  * return value: 0 on success, or actual error code
1413  */
1414 int smbd_reconnect(struct TCP_Server_Info *server)
1415 {
1416     log_rdma_event(INFO, "reconnecting rdma session\n");
1417 
1418     if (!server->smbd_conn) {
1419         log_rdma_event(INFO, "rdma session already destroyed\n");
1420         goto create_conn;
1421     }
1422 
1423     /*
1424      * This is possible if transport is disconnected and we haven't received
1425      * notification from RDMA, but upper layer has detected timeout
1426      */
1427     if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1428         log_rdma_event(INFO, "disconnecting transport\n");
1429         smbd_destroy(server);
1430     }
1431 
1432 create_conn:
1433     log_rdma_event(INFO, "creating rdma session\n");
1434     server->smbd_conn = smbd_get_connection(
1435         server, (struct sockaddr *) &server->dstaddr);
1436 
1437     if (server->smbd_conn)
1438         cifs_dbg(VFS, "RDMA transport re-established\n");
1439 
1440     return server->smbd_conn ? 0 : -ENOENT;
1441 }
1442 
1443 static void destroy_caches_and_workqueue(struct smbd_connection *info)
1444 {
1445     destroy_receive_buffers(info);
1446     destroy_workqueue(info->workqueue);
1447     mempool_destroy(info->response_mempool);
1448     kmem_cache_destroy(info->response_cache);
1449     mempool_destroy(info->request_mempool);
1450     kmem_cache_destroy(info->request_cache);
1451 }
1452 
1453 #define MAX_NAME_LEN    80
1454 static int allocate_caches_and_workqueue(struct smbd_connection *info)
1455 {
1456     char name[MAX_NAME_LEN];
1457     int rc;
1458 
1459     scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
1460     info->request_cache =
1461         kmem_cache_create(
1462             name,
1463             sizeof(struct smbd_request) +
1464                 sizeof(struct smbd_data_transfer),
1465             0, SLAB_HWCACHE_ALIGN, NULL);
1466     if (!info->request_cache)
1467         return -ENOMEM;
1468 
1469     info->request_mempool =
1470         mempool_create(info->send_credit_target, mempool_alloc_slab,
1471             mempool_free_slab, info->request_cache);
1472     if (!info->request_mempool)
1473         goto out1;
1474 
1475     scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
1476     info->response_cache =
1477         kmem_cache_create(
1478             name,
1479             sizeof(struct smbd_response) +
1480                 info->max_receive_size,
1481             0, SLAB_HWCACHE_ALIGN, NULL);
1482     if (!info->response_cache)
1483         goto out2;
1484 
1485     info->response_mempool =
1486         mempool_create(info->receive_credit_max, mempool_alloc_slab,
1487                mempool_free_slab, info->response_cache);
1488     if (!info->response_mempool)
1489         goto out3;
1490 
1491     scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
1492     info->workqueue = create_workqueue(name);
1493     if (!info->workqueue)
1494         goto out4;
1495 
1496     rc = allocate_receive_buffers(info, info->receive_credit_max);
1497     if (rc) {
1498         log_rdma_event(ERR, "failed to allocate receive buffers\n");
1499         goto out5;
1500     }
1501 
1502     return 0;
1503 
1504 out5:
1505     destroy_workqueue(info->workqueue);
1506 out4:
1507     mempool_destroy(info->response_mempool);
1508 out3:
1509     kmem_cache_destroy(info->response_cache);
1510 out2:
1511     mempool_destroy(info->request_mempool);
1512 out1:
1513     kmem_cache_destroy(info->request_cache);
1514     return -ENOMEM;
1515 }
1516 
1517 /* Create a SMBD connection, called by upper layer */
1518 static struct smbd_connection *_smbd_get_connection(
1519     struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1520 {
1521     int rc;
1522     struct smbd_connection *info;
1523     struct rdma_conn_param conn_param;
1524     struct ib_qp_init_attr qp_attr;
1525     struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
1526     struct ib_port_immutable port_immutable;
1527     u32 ird_ord_hdr[2];
1528 
1529     info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1530     if (!info)
1531         return NULL;
1532 
1533     info->transport_status = SMBD_CONNECTING;
1534     rc = smbd_ia_open(info, dstaddr, port);
1535     if (rc) {
1536         log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1537         goto create_id_failed;
1538     }
1539 
1540     if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1541         smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1542         log_rdma_event(ERR, "consider lowering send_credit_target = %d. Possible CQE overrun, device reporting max_cpe %d max_qp_wr %d\n",
1543                    smbd_send_credit_target,
1544                    info->id->device->attrs.max_cqe,
1545                    info->id->device->attrs.max_qp_wr);
1546         goto config_failed;
1547     }
1548 
1549     if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1550         smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1551         log_rdma_event(ERR, "consider lowering receive_credit_max = %d. Possible CQE overrun, device reporting max_cpe %d max_qp_wr %d\n",
1552                    smbd_receive_credit_max,
1553                    info->id->device->attrs.max_cqe,
1554                    info->id->device->attrs.max_qp_wr);
1555         goto config_failed;
1556     }
1557 
1558     info->receive_credit_max = smbd_receive_credit_max;
1559     info->send_credit_target = smbd_send_credit_target;
1560     info->max_send_size = smbd_max_send_size;
1561     info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1562     info->max_receive_size = smbd_max_receive_size;
1563     info->keep_alive_interval = smbd_keep_alive_interval;
1564 
1565     if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
1566         log_rdma_event(ERR,
1567             "warning: device max_send_sge = %d too small\n",
1568             info->id->device->attrs.max_send_sge);
1569         log_rdma_event(ERR, "Queue Pair creation may fail\n");
1570     }
1571     if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
1572         log_rdma_event(ERR,
1573             "warning: device max_recv_sge = %d too small\n",
1574             info->id->device->attrs.max_recv_sge);
1575         log_rdma_event(ERR, "Queue Pair creation may fail\n");
1576     }
1577 
1578     info->send_cq = NULL;
1579     info->recv_cq = NULL;
1580     info->send_cq =
1581         ib_alloc_cq_any(info->id->device, info,
1582                 info->send_credit_target, IB_POLL_SOFTIRQ);
1583     if (IS_ERR(info->send_cq)) {
1584         info->send_cq = NULL;
1585         goto alloc_cq_failed;
1586     }
1587 
1588     info->recv_cq =
1589         ib_alloc_cq_any(info->id->device, info,
1590                 info->receive_credit_max, IB_POLL_SOFTIRQ);
1591     if (IS_ERR(info->recv_cq)) {
1592         info->recv_cq = NULL;
1593         goto alloc_cq_failed;
1594     }
1595 
1596     memset(&qp_attr, 0, sizeof(qp_attr));
1597     qp_attr.event_handler = smbd_qp_async_error_upcall;
1598     qp_attr.qp_context = info;
1599     qp_attr.cap.max_send_wr = info->send_credit_target;
1600     qp_attr.cap.max_recv_wr = info->receive_credit_max;
1601     qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
1602     qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
1603     qp_attr.cap.max_inline_data = 0;
1604     qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1605     qp_attr.qp_type = IB_QPT_RC;
1606     qp_attr.send_cq = info->send_cq;
1607     qp_attr.recv_cq = info->recv_cq;
1608     qp_attr.port_num = ~0;
1609 
1610     rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1611     if (rc) {
1612         log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1613         goto create_qp_failed;
1614     }
1615 
1616     memset(&conn_param, 0, sizeof(conn_param));
1617     conn_param.initiator_depth = 0;
1618 
1619     conn_param.responder_resources =
1620         info->id->device->attrs.max_qp_rd_atom
1621             < SMBD_CM_RESPONDER_RESOURCES ?
1622         info->id->device->attrs.max_qp_rd_atom :
1623         SMBD_CM_RESPONDER_RESOURCES;
1624     info->responder_resources = conn_param.responder_resources;
1625     log_rdma_mr(INFO, "responder_resources=%d\n",
1626         info->responder_resources);
1627 
1628     /* Need to send IRD/ORD in private data for iWARP */
1629     info->id->device->ops.get_port_immutable(
1630         info->id->device, info->id->port_num, &port_immutable);
1631     if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1632         ird_ord_hdr[0] = info->responder_resources;
1633         ird_ord_hdr[1] = 1;
1634         conn_param.private_data = ird_ord_hdr;
1635         conn_param.private_data_len = sizeof(ird_ord_hdr);
1636     } else {
1637         conn_param.private_data = NULL;
1638         conn_param.private_data_len = 0;
1639     }
1640 
1641     conn_param.retry_count = SMBD_CM_RETRY;
1642     conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1643     conn_param.flow_control = 0;
1644 
1645     log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1646         &addr_in->sin_addr, port);
1647 
1648     init_waitqueue_head(&info->conn_wait);
1649     init_waitqueue_head(&info->disconn_wait);
1650     init_waitqueue_head(&info->wait_reassembly_queue);
1651     rc = rdma_connect(info->id, &conn_param);
1652     if (rc) {
1653         log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1654         goto rdma_connect_failed;
1655     }
1656 
1657     wait_event_interruptible(
1658         info->conn_wait, info->transport_status != SMBD_CONNECTING);
1659 
1660     if (info->transport_status != SMBD_CONNECTED) {
1661         log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1662         goto rdma_connect_failed;
1663     }
1664 
1665     log_rdma_event(INFO, "rdma_connect connected\n");
1666 
1667     rc = allocate_caches_and_workqueue(info);
1668     if (rc) {
1669         log_rdma_event(ERR, "cache allocation failed\n");
1670         goto allocate_cache_failed;
1671     }
1672 
1673     init_waitqueue_head(&info->wait_send_queue);
1674     INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
1675     queue_delayed_work(info->workqueue, &info->idle_timer_work,
1676         info->keep_alive_interval*HZ);
1677 
1678     init_waitqueue_head(&info->wait_send_pending);
1679     atomic_set(&info->send_pending, 0);
1680 
1681     init_waitqueue_head(&info->wait_post_send);
1682 
1683     INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
1684     INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1685     info->new_credits_offered = 0;
1686     spin_lock_init(&info->lock_new_credits_offered);
1687 
1688     rc = smbd_negotiate(info);
1689     if (rc) {
1690         log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1691         goto negotiation_failed;
1692     }
1693 
1694     rc = allocate_mr_list(info);
1695     if (rc) {
1696         log_rdma_mr(ERR, "memory registration allocation failed\n");
1697         goto allocate_mr_failed;
1698     }
1699 
1700     return info;
1701 
1702 allocate_mr_failed:
1703     /* At this point, need to a full transport shutdown */
1704     smbd_destroy(server);
1705     return NULL;
1706 
1707 negotiation_failed:
1708     cancel_delayed_work_sync(&info->idle_timer_work);
1709     destroy_caches_and_workqueue(info);
1710     info->transport_status = SMBD_NEGOTIATE_FAILED;
1711     init_waitqueue_head(&info->conn_wait);
1712     rdma_disconnect(info->id);
1713     wait_event(info->conn_wait,
1714         info->transport_status == SMBD_DISCONNECTED);
1715 
1716 allocate_cache_failed:
1717 rdma_connect_failed:
1718     rdma_destroy_qp(info->id);
1719 
1720 create_qp_failed:
1721 alloc_cq_failed:
1722     if (info->send_cq)
1723         ib_free_cq(info->send_cq);
1724     if (info->recv_cq)
1725         ib_free_cq(info->recv_cq);
1726 
1727 config_failed:
1728     ib_dealloc_pd(info->pd);
1729     rdma_destroy_id(info->id);
1730 
1731 create_id_failed:
1732     kfree(info);
1733     return NULL;
1734 }
1735 
1736 struct smbd_connection *smbd_get_connection(
1737     struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1738 {
1739     struct smbd_connection *ret;
1740     int port = SMBD_PORT;
1741 
1742 try_again:
1743     ret = _smbd_get_connection(server, dstaddr, port);
1744 
1745     /* Try SMB_PORT if SMBD_PORT doesn't work */
1746     if (!ret && port == SMBD_PORT) {
1747         port = SMB_PORT;
1748         goto try_again;
1749     }
1750     return ret;
1751 }
1752 
1753 /*
1754  * Receive data from receive reassembly queue
1755  * All the incoming data packets are placed in reassembly queue
1756  * buf: the buffer to read data into
1757  * size: the length of data to read
1758  * return value: actual data read
1759  * Note: this implementation copies the data from reassebmly queue to receive
1760  * buffers used by upper layer. This is not the optimal code path. A better way
1761  * to do it is to not have upper layer allocate its receive buffers but rather
1762  * borrow the buffer from reassembly queue, and return it after data is
1763  * consumed. But this will require more changes to upper layer code, and also
1764  * need to consider packet boundaries while they still being reassembled.
1765  */
1766 static int smbd_recv_buf(struct smbd_connection *info, char *buf,
1767         unsigned int size)
1768 {
1769     struct smbd_response *response;
1770     struct smbd_data_transfer *data_transfer;
1771     int to_copy, to_read, data_read, offset;
1772     u32 data_length, remaining_data_length, data_offset;
1773     int rc;
1774 
1775 again:
1776     /*
1777      * No need to hold the reassembly queue lock all the time as we are
1778      * the only one reading from the front of the queue. The transport
1779      * may add more entries to the back of the queue at the same time
1780      */
1781     log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1782         info->reassembly_data_length);
1783     if (info->reassembly_data_length >= size) {
1784         int queue_length;
1785         int queue_removed = 0;
1786 
1787         /*
1788          * Need to make sure reassembly_data_length is read before
1789          * reading reassembly_queue_length and calling
1790          * _get_first_reassembly. This call is lock free
1791          * as we never read at the end of the queue which are being
1792          * updated in SOFTIRQ as more data is received
1793          */
1794         virt_rmb();
1795         queue_length = info->reassembly_queue_length;
1796         data_read = 0;
1797         to_read = size;
1798         offset = info->first_entry_offset;
1799         while (data_read < size) {
1800             response = _get_first_reassembly(info);
1801             data_transfer = smbd_response_payload(response);
1802             data_length = le32_to_cpu(data_transfer->data_length);
1803             remaining_data_length =
1804                 le32_to_cpu(
1805                     data_transfer->remaining_data_length);
1806             data_offset = le32_to_cpu(data_transfer->data_offset);
1807 
1808             /*
1809              * The upper layer expects RFC1002 length at the
1810              * beginning of the payload. Return it to indicate
1811              * the total length of the packet. This minimize the
1812              * change to upper layer packet processing logic. This
1813              * will be eventually remove when an intermediate
1814              * transport layer is added
1815              */
1816             if (response->first_segment && size == 4) {
1817                 unsigned int rfc1002_len =
1818                     data_length + remaining_data_length;
1819                 *((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1820                 data_read = 4;
1821                 response->first_segment = false;
1822                 log_read(INFO, "returning rfc1002 length %d\n",
1823                     rfc1002_len);
1824                 goto read_rfc1002_done;
1825             }
1826 
1827             to_copy = min_t(int, data_length - offset, to_read);
1828             memcpy(
1829                 buf + data_read,
1830                 (char *)data_transfer + data_offset + offset,
1831                 to_copy);
1832 
1833             /* move on to the next buffer? */
1834             if (to_copy == data_length - offset) {
1835                 queue_length--;
1836                 /*
1837                  * No need to lock if we are not at the
1838                  * end of the queue
1839                  */
1840                 if (queue_length)
1841                     list_del(&response->list);
1842                 else {
1843                     spin_lock_irq(
1844                         &info->reassembly_queue_lock);
1845                     list_del(&response->list);
1846                     spin_unlock_irq(
1847                         &info->reassembly_queue_lock);
1848                 }
1849                 queue_removed++;
1850                 info->count_reassembly_queue--;
1851                 info->count_dequeue_reassembly_queue++;
1852                 put_receive_buffer(info, response);
1853                 offset = 0;
1854                 log_read(INFO, "put_receive_buffer offset=0\n");
1855             } else
1856                 offset += to_copy;
1857 
1858             to_read -= to_copy;
1859             data_read += to_copy;
1860 
1861             log_read(INFO, "_get_first_reassembly memcpy %d bytes data_transfer_length-offset=%d after that to_read=%d data_read=%d offset=%d\n",
1862                  to_copy, data_length - offset,
1863                  to_read, data_read, offset);
1864         }
1865 
1866         spin_lock_irq(&info->reassembly_queue_lock);
1867         info->reassembly_data_length -= data_read;
1868         info->reassembly_queue_length -= queue_removed;
1869         spin_unlock_irq(&info->reassembly_queue_lock);
1870 
1871         info->first_entry_offset = offset;
1872         log_read(INFO, "returning to thread data_read=%d reassembly_data_length=%d first_entry_offset=%d\n",
1873              data_read, info->reassembly_data_length,
1874              info->first_entry_offset);
1875 read_rfc1002_done:
1876         return data_read;
1877     }
1878 
1879     log_read(INFO, "wait_event on more data\n");
1880     rc = wait_event_interruptible(
1881         info->wait_reassembly_queue,
1882         info->reassembly_data_length >= size ||
1883             info->transport_status != SMBD_CONNECTED);
1884     /* Don't return any data if interrupted */
1885     if (rc)
1886         return rc;
1887 
1888     if (info->transport_status != SMBD_CONNECTED) {
1889         log_read(ERR, "disconnected\n");
1890         return -ECONNABORTED;
1891     }
1892 
1893     goto again;
1894 }
1895 
1896 /*
1897  * Receive a page from receive reassembly queue
1898  * page: the page to read data into
1899  * to_read: the length of data to read
1900  * return value: actual data read
1901  */
1902 static int smbd_recv_page(struct smbd_connection *info,
1903         struct page *page, unsigned int page_offset,
1904         unsigned int to_read)
1905 {
1906     int ret;
1907     char *to_address;
1908     void *page_address;
1909 
1910     /* make sure we have the page ready for read */
1911     ret = wait_event_interruptible(
1912         info->wait_reassembly_queue,
1913         info->reassembly_data_length >= to_read ||
1914             info->transport_status != SMBD_CONNECTED);
1915     if (ret)
1916         return ret;
1917 
1918     /* now we can read from reassembly queue and not sleep */
1919     page_address = kmap_atomic(page);
1920     to_address = (char *) page_address + page_offset;
1921 
1922     log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
1923         page, to_address, to_read);
1924 
1925     ret = smbd_recv_buf(info, to_address, to_read);
1926     kunmap_atomic(page_address);
1927 
1928     return ret;
1929 }
1930 
1931 /*
1932  * Receive data from transport
1933  * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
1934  * return: total bytes read, or 0. SMB Direct will not do partial read.
1935  */
1936 int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
1937 {
1938     char *buf;
1939     struct page *page;
1940     unsigned int to_read, page_offset;
1941     int rc;
1942 
1943     if (iov_iter_rw(&msg->msg_iter) == WRITE) {
1944         /* It's a bug in upper layer to get there */
1945         cifs_dbg(VFS, "Invalid msg iter dir %u\n",
1946              iov_iter_rw(&msg->msg_iter));
1947         rc = -EINVAL;
1948         goto out;
1949     }
1950 
1951     switch (iov_iter_type(&msg->msg_iter)) {
1952     case ITER_KVEC:
1953         buf = msg->msg_iter.kvec->iov_base;
1954         to_read = msg->msg_iter.kvec->iov_len;
1955         rc = smbd_recv_buf(info, buf, to_read);
1956         break;
1957 
1958     case ITER_BVEC:
1959         page = msg->msg_iter.bvec->bv_page;
1960         page_offset = msg->msg_iter.bvec->bv_offset;
1961         to_read = msg->msg_iter.bvec->bv_len;
1962         rc = smbd_recv_page(info, page, page_offset, to_read);
1963         break;
1964 
1965     default:
1966         /* It's a bug in upper layer to get there */
1967         cifs_dbg(VFS, "Invalid msg type %d\n",
1968              iov_iter_type(&msg->msg_iter));
1969         rc = -EINVAL;
1970     }
1971 
1972 out:
1973     /* SMBDirect will read it all or nothing */
1974     if (rc > 0)
1975         msg->msg_iter.count = 0;
1976     return rc;
1977 }
1978 
1979 /*
1980  * Send data to transport
1981  * Each rqst is transported as a SMBDirect payload
1982  * rqst: the data to write
1983  * return value: 0 if successfully write, otherwise error code
1984  */
1985 int smbd_send(struct TCP_Server_Info *server,
1986     int num_rqst, struct smb_rqst *rqst_array)
1987 {
1988     struct smbd_connection *info = server->smbd_conn;
1989     struct kvec vec;
1990     int nvecs;
1991     int size;
1992     unsigned int buflen, remaining_data_length;
1993     int start, i, j;
1994     int max_iov_size =
1995         info->max_send_size - sizeof(struct smbd_data_transfer);
1996     struct kvec *iov;
1997     int rc;
1998     struct smb_rqst *rqst;
1999     int rqst_idx;
2000 
2001     if (info->transport_status != SMBD_CONNECTED) {
2002         rc = -EAGAIN;
2003         goto done;
2004     }
2005 
2006     /*
2007      * Add in the page array if there is one. The caller needs to set
2008      * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
2009      * ends at page boundary
2010      */
2011     remaining_data_length = 0;
2012     for (i = 0; i < num_rqst; i++)
2013         remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
2014 
2015     if (remaining_data_length > info->max_fragmented_send_size) {
2016         log_write(ERR, "payload size %d > max size %d\n",
2017             remaining_data_length, info->max_fragmented_send_size);
2018         rc = -EINVAL;
2019         goto done;
2020     }
2021 
2022     log_write(INFO, "num_rqst=%d total length=%u\n",
2023             num_rqst, remaining_data_length);
2024 
2025     rqst_idx = 0;
2026 next_rqst:
2027     rqst = &rqst_array[rqst_idx];
2028     iov = rqst->rq_iov;
2029 
2030     cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
2031         rqst_idx, smb_rqst_len(server, rqst));
2032     for (i = 0; i < rqst->rq_nvec; i++)
2033         dump_smb(iov[i].iov_base, iov[i].iov_len);
2034 
2035 
2036     log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d rq_tailsz=%d buflen=%lu\n",
2037           rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
2038           rqst->rq_tailsz, smb_rqst_len(server, rqst));
2039 
2040     start = i = 0;
2041     buflen = 0;
2042     while (true) {
2043         buflen += iov[i].iov_len;
2044         if (buflen > max_iov_size) {
2045             if (i > start) {
2046                 remaining_data_length -=
2047                     (buflen-iov[i].iov_len);
2048                 log_write(INFO, "sending iov[] from start=%d i=%d nvecs=%d remaining_data_length=%d\n",
2049                       start, i, i - start,
2050                       remaining_data_length);
2051                 rc = smbd_post_send_data(
2052                     info, &iov[start], i-start,
2053                     remaining_data_length);
2054                 if (rc)
2055                     goto done;
2056             } else {
2057                 /* iov[start] is too big, break it */
2058                 nvecs = (buflen+max_iov_size-1)/max_iov_size;
2059                 log_write(INFO, "iov[%d] iov_base=%p buflen=%d break to %d vectors\n",
2060                       start, iov[start].iov_base,
2061                       buflen, nvecs);
2062                 for (j = 0; j < nvecs; j++) {
2063                     vec.iov_base =
2064                         (char *)iov[start].iov_base +
2065                         j*max_iov_size;
2066                     vec.iov_len = max_iov_size;
2067                     if (j == nvecs-1)
2068                         vec.iov_len =
2069                             buflen -
2070                             max_iov_size*(nvecs-1);
2071                     remaining_data_length -= vec.iov_len;
2072                     log_write(INFO,
2073                         "sending vec j=%d iov_base=%p iov_len=%zu remaining_data_length=%d\n",
2074                           j, vec.iov_base, vec.iov_len,
2075                           remaining_data_length);
2076                     rc = smbd_post_send_data(
2077                         info, &vec, 1,
2078                         remaining_data_length);
2079                     if (rc)
2080                         goto done;
2081                 }
2082                 i++;
2083                 if (i == rqst->rq_nvec)
2084                     break;
2085             }
2086             start = i;
2087             buflen = 0;
2088         } else {
2089             i++;
2090             if (i == rqst->rq_nvec) {
2091                 /* send out all remaining vecs */
2092                 remaining_data_length -= buflen;
2093                 log_write(INFO, "sending iov[] from start=%d i=%d nvecs=%d remaining_data_length=%d\n",
2094                       start, i, i - start,
2095                       remaining_data_length);
2096                 rc = smbd_post_send_data(info, &iov[start],
2097                     i-start, remaining_data_length);
2098                 if (rc)
2099                     goto done;
2100                 break;
2101             }
2102         }
2103         log_write(INFO, "looping i=%d buflen=%d\n", i, buflen);
2104     }
2105 
2106     /* now sending pages if there are any */
2107     for (i = 0; i < rqst->rq_npages; i++) {
2108         unsigned int offset;
2109 
2110         rqst_page_get_length(rqst, i, &buflen, &offset);
2111         nvecs = (buflen + max_iov_size - 1) / max_iov_size;
2112         log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
2113             buflen, nvecs);
2114         for (j = 0; j < nvecs; j++) {
2115             size = max_iov_size;
2116             if (j == nvecs-1)
2117                 size = buflen - j*max_iov_size;
2118             remaining_data_length -= size;
2119             log_write(INFO, "sending pages i=%d offset=%d size=%d remaining_data_length=%d\n",
2120                   i, j * max_iov_size + offset, size,
2121                   remaining_data_length);
2122             rc = smbd_post_send_page(
2123                 info, rqst->rq_pages[i],
2124                 j*max_iov_size + offset,
2125                 size, remaining_data_length);
2126             if (rc)
2127                 goto done;
2128         }
2129     }
2130 
2131     rqst_idx++;
2132     if (rqst_idx < num_rqst)
2133         goto next_rqst;
2134 
2135 done:
2136     /*
2137      * As an optimization, we don't wait for individual I/O to finish
2138      * before sending the next one.
2139      * Send them all and wait for pending send count to get to 0
2140      * that means all the I/Os have been out and we are good to return
2141      */
2142 
2143     wait_event(info->wait_send_pending,
2144         atomic_read(&info->send_pending) == 0);
2145 
2146     return rc;
2147 }
2148 
2149 static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
2150 {
2151     struct smbd_mr *mr;
2152     struct ib_cqe *cqe;
2153 
2154     if (wc->status) {
2155         log_rdma_mr(ERR, "status=%d\n", wc->status);
2156         cqe = wc->wr_cqe;
2157         mr = container_of(cqe, struct smbd_mr, cqe);
2158         smbd_disconnect_rdma_connection(mr->conn);
2159     }
2160 }
2161 
2162 /*
2163  * The work queue function that recovers MRs
2164  * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
2165  * again. Both calls are slow, so finish them in a workqueue. This will not
2166  * block I/O path.
2167  * There is one workqueue that recovers MRs, there is no need to lock as the
2168  * I/O requests calling smbd_register_mr will never update the links in the
2169  * mr_list.
2170  */
2171 static void smbd_mr_recovery_work(struct work_struct *work)
2172 {
2173     struct smbd_connection *info =
2174         container_of(work, struct smbd_connection, mr_recovery_work);
2175     struct smbd_mr *smbdirect_mr;
2176     int rc;
2177 
2178     list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
2179         if (smbdirect_mr->state == MR_ERROR) {
2180 
2181             /* recover this MR entry */
2182             rc = ib_dereg_mr(smbdirect_mr->mr);
2183             if (rc) {
2184                 log_rdma_mr(ERR,
2185                     "ib_dereg_mr failed rc=%x\n",
2186                     rc);
2187                 smbd_disconnect_rdma_connection(info);
2188                 continue;
2189             }
2190 
2191             smbdirect_mr->mr = ib_alloc_mr(
2192                 info->pd, info->mr_type,
2193                 info->max_frmr_depth);
2194             if (IS_ERR(smbdirect_mr->mr)) {
2195                 log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x max_frmr_depth=%x\n",
2196                         info->mr_type,
2197                         info->max_frmr_depth);
2198                 smbd_disconnect_rdma_connection(info);
2199                 continue;
2200             }
2201         } else
2202             /* This MR is being used, don't recover it */
2203             continue;
2204 
2205         smbdirect_mr->state = MR_READY;
2206 
2207         /* smbdirect_mr->state is updated by this function
2208          * and is read and updated by I/O issuing CPUs trying
2209          * to get a MR, the call to atomic_inc_return
2210          * implicates a memory barrier and guarantees this
2211          * value is updated before waking up any calls to
2212          * get_mr() from the I/O issuing CPUs
2213          */
2214         if (atomic_inc_return(&info->mr_ready_count) == 1)
2215             wake_up_interruptible(&info->wait_mr);
2216     }
2217 }
2218 
2219 static void destroy_mr_list(struct smbd_connection *info)
2220 {
2221     struct smbd_mr *mr, *tmp;
2222 
2223     cancel_work_sync(&info->mr_recovery_work);
2224     list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
2225         if (mr->state == MR_INVALIDATED)
2226             ib_dma_unmap_sg(info->id->device, mr->sgl,
2227                 mr->sgl_count, mr->dir);
2228         ib_dereg_mr(mr->mr);
2229         kfree(mr->sgl);
2230         kfree(mr);
2231     }
2232 }
2233 
2234 /*
2235  * Allocate MRs used for RDMA read/write
2236  * The number of MRs will not exceed hardware capability in responder_resources
2237  * All MRs are kept in mr_list. The MR can be recovered after it's used
2238  * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
2239  * as MRs are used and recovered for I/O, but the list links will not change
2240  */
2241 static int allocate_mr_list(struct smbd_connection *info)
2242 {
2243     int i;
2244     struct smbd_mr *smbdirect_mr, *tmp;
2245 
2246     INIT_LIST_HEAD(&info->mr_list);
2247     init_waitqueue_head(&info->wait_mr);
2248     spin_lock_init(&info->mr_list_lock);
2249     atomic_set(&info->mr_ready_count, 0);
2250     atomic_set(&info->mr_used_count, 0);
2251     init_waitqueue_head(&info->wait_for_mr_cleanup);
2252     /* Allocate more MRs (2x) than hardware responder_resources */
2253     for (i = 0; i < info->responder_resources * 2; i++) {
2254         smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
2255         if (!smbdirect_mr)
2256             goto out;
2257         smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
2258                     info->max_frmr_depth);
2259         if (IS_ERR(smbdirect_mr->mr)) {
2260             log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x max_frmr_depth=%x\n",
2261                     info->mr_type, info->max_frmr_depth);
2262             goto out;
2263         }
2264         smbdirect_mr->sgl = kcalloc(
2265                     info->max_frmr_depth,
2266                     sizeof(struct scatterlist),
2267                     GFP_KERNEL);
2268         if (!smbdirect_mr->sgl) {
2269             log_rdma_mr(ERR, "failed to allocate sgl\n");
2270             ib_dereg_mr(smbdirect_mr->mr);
2271             goto out;
2272         }
2273         smbdirect_mr->state = MR_READY;
2274         smbdirect_mr->conn = info;
2275 
2276         list_add_tail(&smbdirect_mr->list, &info->mr_list);
2277         atomic_inc(&info->mr_ready_count);
2278     }
2279     INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
2280     return 0;
2281 
2282 out:
2283     kfree(smbdirect_mr);
2284 
2285     list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
2286         ib_dereg_mr(smbdirect_mr->mr);
2287         kfree(smbdirect_mr->sgl);
2288         kfree(smbdirect_mr);
2289     }
2290     return -ENOMEM;
2291 }
2292 
2293 /*
2294  * Get a MR from mr_list. This function waits until there is at least one
2295  * MR available in the list. It may access the list while the
2296  * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
2297  * as they never modify the same places. However, there may be several CPUs
2298  * issueing I/O trying to get MR at the same time, mr_list_lock is used to
2299  * protect this situation.
2300  */
2301 static struct smbd_mr *get_mr(struct smbd_connection *info)
2302 {
2303     struct smbd_mr *ret;
2304     int rc;
2305 again:
2306     rc = wait_event_interruptible(info->wait_mr,
2307         atomic_read(&info->mr_ready_count) ||
2308         info->transport_status != SMBD_CONNECTED);
2309     if (rc) {
2310         log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
2311         return NULL;
2312     }
2313 
2314     if (info->transport_status != SMBD_CONNECTED) {
2315         log_rdma_mr(ERR, "info->transport_status=%x\n",
2316             info->transport_status);
2317         return NULL;
2318     }
2319 
2320     spin_lock(&info->mr_list_lock);
2321     list_for_each_entry(ret, &info->mr_list, list) {
2322         if (ret->state == MR_READY) {
2323             ret->state = MR_REGISTERED;
2324             spin_unlock(&info->mr_list_lock);
2325             atomic_dec(&info->mr_ready_count);
2326             atomic_inc(&info->mr_used_count);
2327             return ret;
2328         }
2329     }
2330 
2331     spin_unlock(&info->mr_list_lock);
2332     /*
2333      * It is possible that we could fail to get MR because other processes may
2334      * try to acquire a MR at the same time. If this is the case, retry it.
2335      */
2336     goto again;
2337 }
2338 
2339 /*
2340  * Register memory for RDMA read/write
2341  * pages[]: the list of pages to register memory with
2342  * num_pages: the number of pages to register
2343  * tailsz: if non-zero, the bytes to register in the last page
2344  * writing: true if this is a RDMA write (SMB read), false for RDMA read
2345  * need_invalidate: true if this MR needs to be locally invalidated after I/O
2346  * return value: the MR registered, NULL if failed.
2347  */
2348 struct smbd_mr *smbd_register_mr(
2349     struct smbd_connection *info, struct page *pages[], int num_pages,
2350     int offset, int tailsz, bool writing, bool need_invalidate)
2351 {
2352     struct smbd_mr *smbdirect_mr;
2353     int rc, i;
2354     enum dma_data_direction dir;
2355     struct ib_reg_wr *reg_wr;
2356 
2357     if (num_pages > info->max_frmr_depth) {
2358         log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
2359             num_pages, info->max_frmr_depth);
2360         return NULL;
2361     }
2362 
2363     smbdirect_mr = get_mr(info);
2364     if (!smbdirect_mr) {
2365         log_rdma_mr(ERR, "get_mr returning NULL\n");
2366         return NULL;
2367     }
2368     smbdirect_mr->need_invalidate = need_invalidate;
2369     smbdirect_mr->sgl_count = num_pages;
2370     sg_init_table(smbdirect_mr->sgl, num_pages);
2371 
2372     log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
2373             num_pages, offset, tailsz);
2374 
2375     if (num_pages == 1) {
2376         sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
2377         goto skip_multiple_pages;
2378     }
2379 
2380     /* We have at least two pages to register */
2381     sg_set_page(
2382         &smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
2383     i = 1;
2384     while (i < num_pages - 1) {
2385         sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
2386         i++;
2387     }
2388     sg_set_page(&smbdirect_mr->sgl[i], pages[i],
2389         tailsz ? tailsz : PAGE_SIZE, 0);
2390 
2391 skip_multiple_pages:
2392     dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2393     smbdirect_mr->dir = dir;
2394     rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
2395     if (!rc) {
2396         log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
2397             num_pages, dir, rc);
2398         goto dma_map_error;
2399     }
2400 
2401     rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
2402         NULL, PAGE_SIZE);
2403     if (rc != num_pages) {
2404         log_rdma_mr(ERR,
2405             "ib_map_mr_sg failed rc = %d num_pages = %x\n",
2406             rc, num_pages);
2407         goto map_mr_error;
2408     }
2409 
2410     ib_update_fast_reg_key(smbdirect_mr->mr,
2411         ib_inc_rkey(smbdirect_mr->mr->rkey));
2412     reg_wr = &smbdirect_mr->wr;
2413     reg_wr->wr.opcode = IB_WR_REG_MR;
2414     smbdirect_mr->cqe.done = register_mr_done;
2415     reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
2416     reg_wr->wr.num_sge = 0;
2417     reg_wr->wr.send_flags = IB_SEND_SIGNALED;
2418     reg_wr->mr = smbdirect_mr->mr;
2419     reg_wr->key = smbdirect_mr->mr->rkey;
2420     reg_wr->access = writing ?
2421             IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2422             IB_ACCESS_REMOTE_READ;
2423 
2424     /*
2425      * There is no need for waiting for complemtion on ib_post_send
2426      * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
2427      * on the next ib_post_send when we actaully send I/O to remote peer
2428      */
2429     rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
2430     if (!rc)
2431         return smbdirect_mr;
2432 
2433     log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
2434         rc, reg_wr->key);
2435 
2436     /* If all failed, attempt to recover this MR by setting it MR_ERROR*/
2437 map_mr_error:
2438     ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
2439         smbdirect_mr->sgl_count, smbdirect_mr->dir);
2440 
2441 dma_map_error:
2442     smbdirect_mr->state = MR_ERROR;
2443     if (atomic_dec_and_test(&info->mr_used_count))
2444         wake_up(&info->wait_for_mr_cleanup);
2445 
2446     smbd_disconnect_rdma_connection(info);
2447 
2448     return NULL;
2449 }
2450 
2451 static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
2452 {
2453     struct smbd_mr *smbdirect_mr;
2454     struct ib_cqe *cqe;
2455 
2456     cqe = wc->wr_cqe;
2457     smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
2458     smbdirect_mr->state = MR_INVALIDATED;
2459     if (wc->status != IB_WC_SUCCESS) {
2460         log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
2461         smbdirect_mr->state = MR_ERROR;
2462     }
2463     complete(&smbdirect_mr->invalidate_done);
2464 }
2465 
2466 /*
2467  * Deregister a MR after I/O is done
2468  * This function may wait if remote invalidation is not used
2469  * and we have to locally invalidate the buffer to prevent data is being
2470  * modified by remote peer after upper layer consumes it
2471  */
2472 int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
2473 {
2474     struct ib_send_wr *wr;
2475     struct smbd_connection *info = smbdirect_mr->conn;
2476     int rc = 0;
2477 
2478     if (smbdirect_mr->need_invalidate) {
2479         /* Need to finish local invalidation before returning */
2480         wr = &smbdirect_mr->inv_wr;
2481         wr->opcode = IB_WR_LOCAL_INV;
2482         smbdirect_mr->cqe.done = local_inv_done;
2483         wr->wr_cqe = &smbdirect_mr->cqe;
2484         wr->num_sge = 0;
2485         wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
2486         wr->send_flags = IB_SEND_SIGNALED;
2487 
2488         init_completion(&smbdirect_mr->invalidate_done);
2489         rc = ib_post_send(info->id->qp, wr, NULL);
2490         if (rc) {
2491             log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
2492             smbd_disconnect_rdma_connection(info);
2493             goto done;
2494         }
2495         wait_for_completion(&smbdirect_mr->invalidate_done);
2496         smbdirect_mr->need_invalidate = false;
2497     } else
2498         /*
2499          * For remote invalidation, just set it to MR_INVALIDATED
2500          * and defer to mr_recovery_work to recover the MR for next use
2501          */
2502         smbdirect_mr->state = MR_INVALIDATED;
2503 
2504     if (smbdirect_mr->state == MR_INVALIDATED) {
2505         ib_dma_unmap_sg(
2506             info->id->device, smbdirect_mr->sgl,
2507             smbdirect_mr->sgl_count,
2508             smbdirect_mr->dir);
2509         smbdirect_mr->state = MR_READY;
2510         if (atomic_inc_return(&info->mr_ready_count) == 1)
2511             wake_up_interruptible(&info->wait_mr);
2512     } else
2513         /*
2514          * Schedule the work to do MR recovery for future I/Os MR
2515          * recovery is slow and don't want it to block current I/O
2516          */
2517         queue_work(info->workqueue, &info->mr_recovery_work);
2518 
2519 done:
2520     if (atomic_dec_and_test(&info->mr_used_count))
2521         wake_up(&info->wait_for_mr_cleanup);
2522 
2523     return rc;
2524 }