blob: 339fa4f025f7518271b44757720b35eb9b5073eb [file] [log] [blame]
Namjae Jeon0626e662021-03-16 13:07:11 +09001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2017, Microsoft Corporation.
4 * Copyright (C) 2018, LG Electronics.
5 *
6 * Author(s): Long Li <longli@microsoft.com>,
7 * Hyunchul Lee <hyc.lee@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
18 */
19
20#define SUBMOD_NAME "smb_direct"
21
22#include <linux/kthread.h>
Namjae Jeon0626e662021-03-16 13:07:11 +090023#include <linux/list.h>
24#include <linux/mempool.h>
25#include <linux/highmem.h>
26#include <linux/scatterlist.h>
27#include <rdma/ib_verbs.h>
28#include <rdma/rdma_cm.h>
29#include <rdma/rw.h>
30
31#include "glob.h"
32#include "connection.h"
33#include "smb_common.h"
34#include "smbstatus.h"
Namjae Jeon0626e662021-03-16 13:07:11 +090035#include "transport_rdma.h"
36
37#define SMB_DIRECT_PORT 5445
38
39#define SMB_DIRECT_VERSION_LE cpu_to_le16(0x0100)
40
41/* SMB_DIRECT negotiation timeout in seconds */
42#define SMB_DIRECT_NEGOTIATE_TIMEOUT 120
43
44#define SMB_DIRECT_MAX_SEND_SGES 8
45#define SMB_DIRECT_MAX_RECV_SGES 1
46
47/*
48 * Default maximum number of RDMA read/write outstanding on this connection
49 * This value is possibly decreased during QP creation on hardware limit
50 */
51#define SMB_DIRECT_CM_INITIATOR_DEPTH 8
52
53/* Maximum number of retries on data transfer operations */
54#define SMB_DIRECT_CM_RETRY 6
55/* No need to retry on Receiver Not Ready since SMB_DIRECT manages credits */
56#define SMB_DIRECT_CM_RNR_RETRY 0
57
58/*
59 * User configurable initial values per SMB_DIRECT transport connection
Namjae Jeon92239582021-07-16 17:16:11 +090060 * as defined in [MS-SMBD] 3.1.1.1
Namjae Jeon0626e662021-03-16 13:07:11 +090061 * Those may change after a SMB_DIRECT negotiation
62 */
63/* The local peer's maximum number of credits to grant to the peer */
64static int smb_direct_receive_credit_max = 255;
65
66/* The remote peer's credit request of local peer */
67static int smb_direct_send_credit_target = 255;
68
69/* The maximum single message size can be sent to remote peer */
70static int smb_direct_max_send_size = 8192;
71
72/* The maximum fragmented upper-layer payload receive size supported */
73static int smb_direct_max_fragmented_recv_size = 1024 * 1024;
74
75/* The maximum single-message size which can be received */
76static int smb_direct_max_receive_size = 8192;
77
78static int smb_direct_max_read_write_size = 1024 * 1024;
79
80static int smb_direct_max_outstanding_rw_ops = 8;
81
Hyunchul Lee31928a02021-12-29 23:02:15 +090082static LIST_HEAD(smb_direct_device_list);
83static DEFINE_RWLOCK(smb_direct_device_lock);
84
85struct smb_direct_device {
86 struct ib_device *ib_dev;
87 struct list_head list;
88};
89
Namjae Jeon0626e662021-03-16 13:07:11 +090090static struct smb_direct_listener {
91 struct rdma_cm_id *cm_id;
92} smb_direct_listener;
93
Namjae Jeon0626e662021-03-16 13:07:11 +090094static struct workqueue_struct *smb_direct_wq;
95
96enum smb_direct_status {
97 SMB_DIRECT_CS_NEW = 0,
98 SMB_DIRECT_CS_CONNECTED,
99 SMB_DIRECT_CS_DISCONNECTING,
100 SMB_DIRECT_CS_DISCONNECTED,
101};
102
103struct smb_direct_transport {
104 struct ksmbd_transport transport;
105
106 enum smb_direct_status status;
107 bool full_packet_received;
108 wait_queue_head_t wait_status;
109
110 struct rdma_cm_id *cm_id;
111 struct ib_cq *send_cq;
112 struct ib_cq *recv_cq;
113 struct ib_pd *pd;
114 struct ib_qp *qp;
115
116 int max_send_size;
117 int max_recv_size;
118 int max_fragmented_send_size;
119 int max_fragmented_recv_size;
120 int max_rdma_rw_size;
121
122 spinlock_t reassembly_queue_lock;
123 struct list_head reassembly_queue;
124 int reassembly_data_length;
125 int reassembly_queue_length;
126 int first_entry_offset;
127 wait_queue_head_t wait_reassembly_queue;
128
129 spinlock_t receive_credit_lock;
130 int recv_credits;
131 int count_avail_recvmsg;
132 int recv_credit_max;
133 int recv_credit_target;
134
135 spinlock_t recvmsg_queue_lock;
136 struct list_head recvmsg_queue;
137
138 spinlock_t empty_recvmsg_queue_lock;
139 struct list_head empty_recvmsg_queue;
140
141 int send_credit_target;
142 atomic_t send_credits;
143 spinlock_t lock_new_recv_credits;
144 int new_recv_credits;
145 atomic_t rw_avail_ops;
146
147 wait_queue_head_t wait_send_credits;
148 wait_queue_head_t wait_rw_avail_ops;
149
150 mempool_t *sendmsg_mempool;
151 struct kmem_cache *sendmsg_cache;
152 mempool_t *recvmsg_mempool;
153 struct kmem_cache *recvmsg_cache;
154
155 wait_queue_head_t wait_send_payload_pending;
156 atomic_t send_payload_pending;
157 wait_queue_head_t wait_send_pending;
158 atomic_t send_pending;
159
160 struct delayed_work post_recv_credits_work;
161 struct work_struct send_immediate_work;
162 struct work_struct disconnect_work;
163
164 bool negotiation_requested;
165};
166
167#define KSMBD_TRANS(t) ((struct ksmbd_transport *)&((t)->transport))
Namjae Jeon0626e662021-03-16 13:07:11 +0900168
169enum {
170 SMB_DIRECT_MSG_NEGOTIATE_REQ = 0,
171 SMB_DIRECT_MSG_DATA_TRANSFER
172};
173
174static struct ksmbd_transport_ops ksmbd_smb_direct_transport_ops;
175
176struct smb_direct_send_ctx {
177 struct list_head msg_list;
178 int wr_cnt;
179 bool need_invalidate_rkey;
180 unsigned int remote_key;
181};
182
183struct smb_direct_sendmsg {
184 struct smb_direct_transport *transport;
185 struct ib_send_wr wr;
186 struct list_head list;
187 int num_sge;
188 struct ib_sge sge[SMB_DIRECT_MAX_SEND_SGES];
189 struct ib_cqe cqe;
190 u8 packet[];
191};
192
193struct smb_direct_recvmsg {
194 struct smb_direct_transport *transport;
195 struct list_head list;
196 int type;
197 struct ib_sge sge;
198 struct ib_cqe cqe;
199 bool first_segment;
200 u8 packet[];
201};
202
203struct smb_direct_rdma_rw_msg {
204 struct smb_direct_transport *t;
205 struct ib_cqe cqe;
206 struct completion *completion;
207 struct rdma_rw_ctx rw_ctx;
208 struct sg_table sgt;
209 struct scatterlist sg_list[0];
210};
211
Hyunchul Lee8ad8dc32021-06-25 07:02:07 +0900212static inline int get_buf_page_count(void *buf, int size)
213{
214 return DIV_ROUND_UP((uintptr_t)buf + size, PAGE_SIZE) -
215 (uintptr_t)buf / PAGE_SIZE;
216}
Namjae Jeon0626e662021-03-16 13:07:11 +0900217
218static void smb_direct_destroy_pools(struct smb_direct_transport *transport);
219static void smb_direct_post_recv_credits(struct work_struct *work);
220static int smb_direct_post_send_data(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900221 struct smb_direct_send_ctx *send_ctx,
222 struct kvec *iov, int niov,
223 int remaining_data_length);
Namjae Jeon0626e662021-03-16 13:07:11 +0900224
Namjae Jeon02d4b4a2021-06-25 13:43:01 +0900225static inline struct smb_direct_transport *
226smb_trans_direct_transfort(struct ksmbd_transport *t)
227{
228 return container_of(t, struct smb_direct_transport, transport);
229}
230
Namjae Jeon0626e662021-03-16 13:07:11 +0900231static inline void
232*smb_direct_recvmsg_payload(struct smb_direct_recvmsg *recvmsg)
233{
234 return (void *)recvmsg->packet;
235}
236
237static inline bool is_receive_credit_post_required(int receive_credits,
Namjae Jeon070fb212021-05-26 17:57:12 +0900238 int avail_recvmsg_count)
Namjae Jeon0626e662021-03-16 13:07:11 +0900239{
240 return receive_credits <= (smb_direct_receive_credit_max >> 3) &&
241 avail_recvmsg_count >= (receive_credits >> 2);
242}
243
244static struct
245smb_direct_recvmsg *get_free_recvmsg(struct smb_direct_transport *t)
246{
247 struct smb_direct_recvmsg *recvmsg = NULL;
248
249 spin_lock(&t->recvmsg_queue_lock);
250 if (!list_empty(&t->recvmsg_queue)) {
251 recvmsg = list_first_entry(&t->recvmsg_queue,
252 struct smb_direct_recvmsg,
253 list);
254 list_del(&recvmsg->list);
255 }
256 spin_unlock(&t->recvmsg_queue_lock);
257 return recvmsg;
258}
259
260static void put_recvmsg(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900261 struct smb_direct_recvmsg *recvmsg)
Namjae Jeon0626e662021-03-16 13:07:11 +0900262{
263 ib_dma_unmap_single(t->cm_id->device, recvmsg->sge.addr,
Namjae Jeon070fb212021-05-26 17:57:12 +0900264 recvmsg->sge.length, DMA_FROM_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +0900265
266 spin_lock(&t->recvmsg_queue_lock);
267 list_add(&recvmsg->list, &t->recvmsg_queue);
268 spin_unlock(&t->recvmsg_queue_lock);
Namjae Jeon0626e662021-03-16 13:07:11 +0900269}
270
271static struct
272smb_direct_recvmsg *get_empty_recvmsg(struct smb_direct_transport *t)
273{
274 struct smb_direct_recvmsg *recvmsg = NULL;
275
276 spin_lock(&t->empty_recvmsg_queue_lock);
277 if (!list_empty(&t->empty_recvmsg_queue)) {
Namjae Jeon64b39f42021-03-30 14:25:35 +0900278 recvmsg = list_first_entry(&t->empty_recvmsg_queue,
Namjae Jeon070fb212021-05-26 17:57:12 +0900279 struct smb_direct_recvmsg, list);
Namjae Jeon0626e662021-03-16 13:07:11 +0900280 list_del(&recvmsg->list);
281 }
282 spin_unlock(&t->empty_recvmsg_queue_lock);
283 return recvmsg;
284}
285
286static void put_empty_recvmsg(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900287 struct smb_direct_recvmsg *recvmsg)
Namjae Jeon0626e662021-03-16 13:07:11 +0900288{
289 ib_dma_unmap_single(t->cm_id->device, recvmsg->sge.addr,
Namjae Jeon070fb212021-05-26 17:57:12 +0900290 recvmsg->sge.length, DMA_FROM_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +0900291
292 spin_lock(&t->empty_recvmsg_queue_lock);
293 list_add_tail(&recvmsg->list, &t->empty_recvmsg_queue);
294 spin_unlock(&t->empty_recvmsg_queue_lock);
295}
296
297static void enqueue_reassembly(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900298 struct smb_direct_recvmsg *recvmsg,
299 int data_length)
Namjae Jeon0626e662021-03-16 13:07:11 +0900300{
301 spin_lock(&t->reassembly_queue_lock);
302 list_add_tail(&recvmsg->list, &t->reassembly_queue);
303 t->reassembly_queue_length++;
304 /*
305 * Make sure reassembly_data_length is updated after list and
306 * reassembly_queue_length are updated. On the dequeue side
307 * reassembly_data_length is checked without a lock to determine
308 * if reassembly_queue_length and list is up to date
309 */
310 virt_wmb();
311 t->reassembly_data_length += data_length;
312 spin_unlock(&t->reassembly_queue_lock);
Namjae Jeon0626e662021-03-16 13:07:11 +0900313}
314
Namjae Jeon64b39f42021-03-30 14:25:35 +0900315static struct smb_direct_recvmsg *get_first_reassembly(struct smb_direct_transport *t)
Namjae Jeon0626e662021-03-16 13:07:11 +0900316{
317 if (!list_empty(&t->reassembly_queue))
318 return list_first_entry(&t->reassembly_queue,
319 struct smb_direct_recvmsg, list);
320 else
321 return NULL;
322}
323
324static void smb_direct_disconnect_rdma_work(struct work_struct *work)
325{
326 struct smb_direct_transport *t =
327 container_of(work, struct smb_direct_transport,
328 disconnect_work);
329
330 if (t->status == SMB_DIRECT_CS_CONNECTED) {
331 t->status = SMB_DIRECT_CS_DISCONNECTING;
332 rdma_disconnect(t->cm_id);
333 }
334}
335
336static void
337smb_direct_disconnect_rdma_connection(struct smb_direct_transport *t)
338{
Hyunchul Lee323b1ea2021-08-12 10:23:08 +0900339 if (t->status == SMB_DIRECT_CS_CONNECTED)
340 queue_work(smb_direct_wq, &t->disconnect_work);
Namjae Jeon0626e662021-03-16 13:07:11 +0900341}
342
343static void smb_direct_send_immediate_work(struct work_struct *work)
344{
345 struct smb_direct_transport *t = container_of(work,
346 struct smb_direct_transport, send_immediate_work);
347
348 if (t->status != SMB_DIRECT_CS_CONNECTED)
349 return;
350
351 smb_direct_post_send_data(t, NULL, NULL, 0, 0);
352}
353
354static struct smb_direct_transport *alloc_transport(struct rdma_cm_id *cm_id)
355{
356 struct smb_direct_transport *t;
357 struct ksmbd_conn *conn;
358
359 t = kzalloc(sizeof(*t), GFP_KERNEL);
360 if (!t)
361 return NULL;
362
363 t->cm_id = cm_id;
364 cm_id->context = t;
365
366 t->status = SMB_DIRECT_CS_NEW;
367 init_waitqueue_head(&t->wait_status);
368
369 spin_lock_init(&t->reassembly_queue_lock);
370 INIT_LIST_HEAD(&t->reassembly_queue);
371 t->reassembly_data_length = 0;
372 t->reassembly_queue_length = 0;
373 init_waitqueue_head(&t->wait_reassembly_queue);
374 init_waitqueue_head(&t->wait_send_credits);
375 init_waitqueue_head(&t->wait_rw_avail_ops);
376
377 spin_lock_init(&t->receive_credit_lock);
378 spin_lock_init(&t->recvmsg_queue_lock);
379 INIT_LIST_HEAD(&t->recvmsg_queue);
380
381 spin_lock_init(&t->empty_recvmsg_queue_lock);
382 INIT_LIST_HEAD(&t->empty_recvmsg_queue);
383
384 init_waitqueue_head(&t->wait_send_payload_pending);
385 atomic_set(&t->send_payload_pending, 0);
386 init_waitqueue_head(&t->wait_send_pending);
387 atomic_set(&t->send_pending, 0);
388
389 spin_lock_init(&t->lock_new_recv_credits);
390
391 INIT_DELAYED_WORK(&t->post_recv_credits_work,
392 smb_direct_post_recv_credits);
393 INIT_WORK(&t->send_immediate_work, smb_direct_send_immediate_work);
394 INIT_WORK(&t->disconnect_work, smb_direct_disconnect_rdma_work);
395
396 conn = ksmbd_conn_alloc();
397 if (!conn)
398 goto err;
399 conn->transport = KSMBD_TRANS(t);
400 KSMBD_TRANS(t)->conn = conn;
401 KSMBD_TRANS(t)->ops = &ksmbd_smb_direct_transport_ops;
402 return t;
403err:
404 kfree(t);
405 return NULL;
406}
407
408static void free_transport(struct smb_direct_transport *t)
409{
410 struct smb_direct_recvmsg *recvmsg;
411
412 wake_up_interruptible(&t->wait_send_credits);
413
414 ksmbd_debug(RDMA, "wait for all send posted to IB to finish\n");
415 wait_event(t->wait_send_payload_pending,
Namjae Jeon070fb212021-05-26 17:57:12 +0900416 atomic_read(&t->send_payload_pending) == 0);
Namjae Jeon0626e662021-03-16 13:07:11 +0900417 wait_event(t->wait_send_pending,
Namjae Jeon070fb212021-05-26 17:57:12 +0900418 atomic_read(&t->send_pending) == 0);
Namjae Jeon0626e662021-03-16 13:07:11 +0900419
420 cancel_work_sync(&t->disconnect_work);
421 cancel_delayed_work_sync(&t->post_recv_credits_work);
422 cancel_work_sync(&t->send_immediate_work);
423
424 if (t->qp) {
425 ib_drain_qp(t->qp);
426 ib_destroy_qp(t->qp);
427 }
428
429 ksmbd_debug(RDMA, "drain the reassembly queue\n");
430 do {
431 spin_lock(&t->reassembly_queue_lock);
432 recvmsg = get_first_reassembly(t);
433 if (recvmsg) {
434 list_del(&recvmsg->list);
Namjae Jeon0626e662021-03-16 13:07:11 +0900435 spin_unlock(&t->reassembly_queue_lock);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900436 put_recvmsg(t, recvmsg);
437 } else {
438 spin_unlock(&t->reassembly_queue_lock);
439 }
Namjae Jeon0626e662021-03-16 13:07:11 +0900440 } while (recvmsg);
441 t->reassembly_data_length = 0;
442
443 if (t->send_cq)
444 ib_free_cq(t->send_cq);
445 if (t->recv_cq)
446 ib_free_cq(t->recv_cq);
447 if (t->pd)
448 ib_dealloc_pd(t->pd);
449 if (t->cm_id)
450 rdma_destroy_id(t->cm_id);
451
452 smb_direct_destroy_pools(t);
453 ksmbd_conn_free(KSMBD_TRANS(t)->conn);
454 kfree(t);
455}
456
457static struct smb_direct_sendmsg
458*smb_direct_alloc_sendmsg(struct smb_direct_transport *t)
459{
460 struct smb_direct_sendmsg *msg;
461
462 msg = mempool_alloc(t->sendmsg_mempool, GFP_KERNEL);
463 if (!msg)
464 return ERR_PTR(-ENOMEM);
465 msg->transport = t;
466 INIT_LIST_HEAD(&msg->list);
467 msg->num_sge = 0;
468 return msg;
469}
470
471static void smb_direct_free_sendmsg(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900472 struct smb_direct_sendmsg *msg)
Namjae Jeon0626e662021-03-16 13:07:11 +0900473{
474 int i;
475
476 if (msg->num_sge > 0) {
477 ib_dma_unmap_single(t->cm_id->device,
Namjae Jeon070fb212021-05-26 17:57:12 +0900478 msg->sge[0].addr, msg->sge[0].length,
479 DMA_TO_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +0900480 for (i = 1; i < msg->num_sge; i++)
481 ib_dma_unmap_page(t->cm_id->device,
Namjae Jeon070fb212021-05-26 17:57:12 +0900482 msg->sge[i].addr, msg->sge[i].length,
483 DMA_TO_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +0900484 }
485 mempool_free(msg, t->sendmsg_mempool);
486}
487
488static int smb_direct_check_recvmsg(struct smb_direct_recvmsg *recvmsg)
489{
490 switch (recvmsg->type) {
491 case SMB_DIRECT_MSG_DATA_TRANSFER: {
492 struct smb_direct_data_transfer *req =
Namjae Jeon64b39f42021-03-30 14:25:35 +0900493 (struct smb_direct_data_transfer *)recvmsg->packet;
494 struct smb2_hdr *hdr = (struct smb2_hdr *)(recvmsg->packet
Namjae Jeoncb451722021-11-03 08:08:44 +0900495 + le32_to_cpu(req->data_offset));
Namjae Jeon0626e662021-03-16 13:07:11 +0900496 ksmbd_debug(RDMA,
Namjae Jeon070fb212021-05-26 17:57:12 +0900497 "CreditGranted: %u, CreditRequested: %u, DataLength: %u, RemainingDataLength: %u, SMB: %x, Command: %u\n",
498 le16_to_cpu(req->credits_granted),
499 le16_to_cpu(req->credits_requested),
500 req->data_length, req->remaining_data_length,
501 hdr->ProtocolId, hdr->Command);
Namjae Jeon0626e662021-03-16 13:07:11 +0900502 break;
503 }
504 case SMB_DIRECT_MSG_NEGOTIATE_REQ: {
505 struct smb_direct_negotiate_req *req =
506 (struct smb_direct_negotiate_req *)recvmsg->packet;
507 ksmbd_debug(RDMA,
Namjae Jeon070fb212021-05-26 17:57:12 +0900508 "MinVersion: %u, MaxVersion: %u, CreditRequested: %u, MaxSendSize: %u, MaxRecvSize: %u, MaxFragmentedSize: %u\n",
509 le16_to_cpu(req->min_version),
510 le16_to_cpu(req->max_version),
511 le16_to_cpu(req->credits_requested),
512 le32_to_cpu(req->preferred_send_size),
513 le32_to_cpu(req->max_receive_size),
514 le32_to_cpu(req->max_fragmented_size));
Namjae Jeon0626e662021-03-16 13:07:11 +0900515 if (le16_to_cpu(req->min_version) > 0x0100 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900516 le16_to_cpu(req->max_version) < 0x0100)
Namjae Jeon0626e662021-03-16 13:07:11 +0900517 return -EOPNOTSUPP;
518 if (le16_to_cpu(req->credits_requested) <= 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900519 le32_to_cpu(req->max_receive_size) <= 128 ||
520 le32_to_cpu(req->max_fragmented_size) <=
521 128 * 1024)
Namjae Jeon0626e662021-03-16 13:07:11 +0900522 return -ECONNABORTED;
523
524 break;
525 }
526 default:
527 return -EINVAL;
528 }
529 return 0;
530}
531
532static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
533{
534 struct smb_direct_recvmsg *recvmsg;
535 struct smb_direct_transport *t;
536
537 recvmsg = container_of(wc->wr_cqe, struct smb_direct_recvmsg, cqe);
538 t = recvmsg->transport;
539
540 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
541 if (wc->status != IB_WC_WR_FLUSH_ERR) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900542 pr_err("Recv error. status='%s (%d)' opcode=%d\n",
543 ib_wc_status_msg(wc->status), wc->status,
544 wc->opcode);
Namjae Jeon0626e662021-03-16 13:07:11 +0900545 smb_direct_disconnect_rdma_connection(t);
546 }
547 put_empty_recvmsg(t, recvmsg);
548 return;
549 }
550
551 ksmbd_debug(RDMA, "Recv completed. status='%s (%d)', opcode=%d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900552 ib_wc_status_msg(wc->status), wc->status,
553 wc->opcode);
Namjae Jeon0626e662021-03-16 13:07:11 +0900554
555 ib_dma_sync_single_for_cpu(wc->qp->device, recvmsg->sge.addr,
Namjae Jeon070fb212021-05-26 17:57:12 +0900556 recvmsg->sge.length, DMA_FROM_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +0900557
558 switch (recvmsg->type) {
559 case SMB_DIRECT_MSG_NEGOTIATE_REQ:
Hyunchul Lee2ea086e2021-10-15 06:02:50 +0900560 if (wc->byte_len < sizeof(struct smb_direct_negotiate_req)) {
561 put_empty_recvmsg(t, recvmsg);
562 return;
563 }
Namjae Jeon0626e662021-03-16 13:07:11 +0900564 t->negotiation_requested = true;
565 t->full_packet_received = true;
566 wake_up_interruptible(&t->wait_status);
567 break;
568 case SMB_DIRECT_MSG_DATA_TRANSFER: {
569 struct smb_direct_data_transfer *data_transfer =
570 (struct smb_direct_data_transfer *)recvmsg->packet;
Hyunchul Lee2ea086e2021-10-15 06:02:50 +0900571 unsigned int data_length;
Namjae Jeon0626e662021-03-16 13:07:11 +0900572 int avail_recvmsg_count, receive_credits;
573
Hyunchul Lee2ea086e2021-10-15 06:02:50 +0900574 if (wc->byte_len <
575 offsetof(struct smb_direct_data_transfer, padding)) {
576 put_empty_recvmsg(t, recvmsg);
577 return;
578 }
579
580 data_length = le32_to_cpu(data_transfer->data_length);
Namjae Jeon0626e662021-03-16 13:07:11 +0900581 if (data_length) {
Hyunchul Lee2ea086e2021-10-15 06:02:50 +0900582 if (wc->byte_len < sizeof(struct smb_direct_data_transfer) +
583 (u64)data_length) {
584 put_empty_recvmsg(t, recvmsg);
585 return;
586 }
587
Namjae Jeon0626e662021-03-16 13:07:11 +0900588 if (t->full_packet_received)
589 recvmsg->first_segment = true;
590
591 if (le32_to_cpu(data_transfer->remaining_data_length))
592 t->full_packet_received = false;
593 else
594 t->full_packet_received = true;
595
Hyunchul Lee2ea086e2021-10-15 06:02:50 +0900596 enqueue_reassembly(t, recvmsg, (int)data_length);
Namjae Jeon0626e662021-03-16 13:07:11 +0900597 wake_up_interruptible(&t->wait_reassembly_queue);
598
599 spin_lock(&t->receive_credit_lock);
600 receive_credits = --(t->recv_credits);
601 avail_recvmsg_count = t->count_avail_recvmsg;
602 spin_unlock(&t->receive_credit_lock);
603 } else {
604 put_empty_recvmsg(t, recvmsg);
605
606 spin_lock(&t->receive_credit_lock);
607 receive_credits = --(t->recv_credits);
608 avail_recvmsg_count = ++(t->count_avail_recvmsg);
609 spin_unlock(&t->receive_credit_lock);
610 }
611
612 t->recv_credit_target =
613 le16_to_cpu(data_transfer->credits_requested);
614 atomic_add(le16_to_cpu(data_transfer->credits_granted),
Namjae Jeon070fb212021-05-26 17:57:12 +0900615 &t->send_credits);
Namjae Jeon0626e662021-03-16 13:07:11 +0900616
617 if (le16_to_cpu(data_transfer->flags) &
Namjae Jeon070fb212021-05-26 17:57:12 +0900618 SMB_DIRECT_RESPONSE_REQUESTED)
Namjae Jeon0626e662021-03-16 13:07:11 +0900619 queue_work(smb_direct_wq, &t->send_immediate_work);
620
621 if (atomic_read(&t->send_credits) > 0)
622 wake_up_interruptible(&t->wait_send_credits);
623
Namjae Jeon64b39f42021-03-30 14:25:35 +0900624 if (is_receive_credit_post_required(receive_credits, avail_recvmsg_count))
Namjae Jeon0626e662021-03-16 13:07:11 +0900625 mod_delayed_work(smb_direct_wq,
Namjae Jeon070fb212021-05-26 17:57:12 +0900626 &t->post_recv_credits_work, 0);
Namjae Jeon0626e662021-03-16 13:07:11 +0900627 break;
628 }
629 default:
630 break;
631 }
632}
633
634static int smb_direct_post_recv(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900635 struct smb_direct_recvmsg *recvmsg)
Namjae Jeon0626e662021-03-16 13:07:11 +0900636{
637 struct ib_recv_wr wr;
638 int ret;
639
640 recvmsg->sge.addr = ib_dma_map_single(t->cm_id->device,
Namjae Jeon070fb212021-05-26 17:57:12 +0900641 recvmsg->packet, t->max_recv_size,
642 DMA_FROM_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +0900643 ret = ib_dma_mapping_error(t->cm_id->device, recvmsg->sge.addr);
644 if (ret)
645 return ret;
646 recvmsg->sge.length = t->max_recv_size;
647 recvmsg->sge.lkey = t->pd->local_dma_lkey;
648 recvmsg->cqe.done = recv_done;
649
650 wr.wr_cqe = &recvmsg->cqe;
651 wr.next = NULL;
652 wr.sg_list = &recvmsg->sge;
653 wr.num_sge = 1;
654
655 ret = ib_post_recv(t->qp, &wr, NULL);
656 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900657 pr_err("Can't post recv: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +0900658 ib_dma_unmap_single(t->cm_id->device,
Namjae Jeon070fb212021-05-26 17:57:12 +0900659 recvmsg->sge.addr, recvmsg->sge.length,
660 DMA_FROM_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +0900661 smb_direct_disconnect_rdma_connection(t);
662 return ret;
663 }
664 return ret;
665}
666
667static int smb_direct_read(struct ksmbd_transport *t, char *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +0900668 unsigned int size)
Namjae Jeon0626e662021-03-16 13:07:11 +0900669{
670 struct smb_direct_recvmsg *recvmsg;
671 struct smb_direct_data_transfer *data_transfer;
672 int to_copy, to_read, data_read, offset;
673 u32 data_length, remaining_data_length, data_offset;
674 int rc;
Namjae Jeon02d4b4a2021-06-25 13:43:01 +0900675 struct smb_direct_transport *st = smb_trans_direct_transfort(t);
Namjae Jeon0626e662021-03-16 13:07:11 +0900676
677again:
678 if (st->status != SMB_DIRECT_CS_CONNECTED) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900679 pr_err("disconnected\n");
Namjae Jeon0626e662021-03-16 13:07:11 +0900680 return -ENOTCONN;
681 }
682
683 /*
684 * No need to hold the reassembly queue lock all the time as we are
685 * the only one reading from the front of the queue. The transport
686 * may add more entries to the back of the queue at the same time
687 */
688 if (st->reassembly_data_length >= size) {
689 int queue_length;
690 int queue_removed = 0;
691
692 /*
693 * Need to make sure reassembly_data_length is read before
694 * reading reassembly_queue_length and calling
695 * get_first_reassembly. This call is lock free
696 * as we never read at the end of the queue which are being
697 * updated in SOFTIRQ as more data is received
698 */
699 virt_rmb();
700 queue_length = st->reassembly_queue_length;
701 data_read = 0;
702 to_read = size;
703 offset = st->first_entry_offset;
704 while (data_read < size) {
705 recvmsg = get_first_reassembly(st);
706 data_transfer = smb_direct_recvmsg_payload(recvmsg);
707 data_length = le32_to_cpu(data_transfer->data_length);
708 remaining_data_length =
Namjae Jeon64b39f42021-03-30 14:25:35 +0900709 le32_to_cpu(data_transfer->remaining_data_length);
Namjae Jeon0626e662021-03-16 13:07:11 +0900710 data_offset = le32_to_cpu(data_transfer->data_offset);
711
712 /*
713 * The upper layer expects RFC1002 length at the
714 * beginning of the payload. Return it to indicate
715 * the total length of the packet. This minimize the
716 * change to upper layer packet processing logic. This
717 * will be eventually remove when an intermediate
718 * transport layer is added
719 */
720 if (recvmsg->first_segment && size == 4) {
721 unsigned int rfc1002_len =
722 data_length + remaining_data_length;
723 *((__be32 *)buf) = cpu_to_be32(rfc1002_len);
724 data_read = 4;
725 recvmsg->first_segment = false;
726 ksmbd_debug(RDMA,
Namjae Jeon070fb212021-05-26 17:57:12 +0900727 "returning rfc1002 length %d\n",
728 rfc1002_len);
Namjae Jeon0626e662021-03-16 13:07:11 +0900729 goto read_rfc1002_done;
730 }
731
732 to_copy = min_t(int, data_length - offset, to_read);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900733 memcpy(buf + data_read, (char *)data_transfer + data_offset + offset,
Namjae Jeon070fb212021-05-26 17:57:12 +0900734 to_copy);
Namjae Jeon0626e662021-03-16 13:07:11 +0900735
736 /* move on to the next buffer? */
737 if (to_copy == data_length - offset) {
738 queue_length--;
739 /*
740 * No need to lock if we are not at the
741 * end of the queue
742 */
Namjae Jeon64b39f42021-03-30 14:25:35 +0900743 if (queue_length) {
Namjae Jeon0626e662021-03-16 13:07:11 +0900744 list_del(&recvmsg->list);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900745 } else {
746 spin_lock_irq(&st->reassembly_queue_lock);
Namjae Jeon0626e662021-03-16 13:07:11 +0900747 list_del(&recvmsg->list);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900748 spin_unlock_irq(&st->reassembly_queue_lock);
Namjae Jeon0626e662021-03-16 13:07:11 +0900749 }
750 queue_removed++;
751 put_recvmsg(st, recvmsg);
752 offset = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900753 } else {
Namjae Jeon0626e662021-03-16 13:07:11 +0900754 offset += to_copy;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900755 }
Namjae Jeon0626e662021-03-16 13:07:11 +0900756
757 to_read -= to_copy;
758 data_read += to_copy;
759 }
760
761 spin_lock_irq(&st->reassembly_queue_lock);
762 st->reassembly_data_length -= data_read;
763 st->reassembly_queue_length -= queue_removed;
764 spin_unlock_irq(&st->reassembly_queue_lock);
765
766 spin_lock(&st->receive_credit_lock);
767 st->count_avail_recvmsg += queue_removed;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900768 if (is_receive_credit_post_required(st->recv_credits, st->count_avail_recvmsg)) {
Namjae Jeon0626e662021-03-16 13:07:11 +0900769 spin_unlock(&st->receive_credit_lock);
770 mod_delayed_work(smb_direct_wq,
Namjae Jeon070fb212021-05-26 17:57:12 +0900771 &st->post_recv_credits_work, 0);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900772 } else {
Namjae Jeon0626e662021-03-16 13:07:11 +0900773 spin_unlock(&st->receive_credit_lock);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900774 }
Namjae Jeon0626e662021-03-16 13:07:11 +0900775
776 st->first_entry_offset = offset;
777 ksmbd_debug(RDMA,
Namjae Jeon070fb212021-05-26 17:57:12 +0900778 "returning to thread data_read=%d reassembly_data_length=%d first_entry_offset=%d\n",
779 data_read, st->reassembly_data_length,
780 st->first_entry_offset);
Namjae Jeon0626e662021-03-16 13:07:11 +0900781read_rfc1002_done:
782 return data_read;
783 }
784
785 ksmbd_debug(RDMA, "wait_event on more data\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +0900786 rc = wait_event_interruptible(st->wait_reassembly_queue,
Namjae Jeon070fb212021-05-26 17:57:12 +0900787 st->reassembly_data_length >= size ||
788 st->status != SMB_DIRECT_CS_CONNECTED);
Namjae Jeon0626e662021-03-16 13:07:11 +0900789 if (rc)
790 return -EINTR;
791
792 goto again;
793}
794
795static void smb_direct_post_recv_credits(struct work_struct *work)
796{
797 struct smb_direct_transport *t = container_of(work,
798 struct smb_direct_transport, post_recv_credits_work.work);
799 struct smb_direct_recvmsg *recvmsg;
800 int receive_credits, credits = 0;
801 int ret;
802 int use_free = 1;
803
804 spin_lock(&t->receive_credit_lock);
805 receive_credits = t->recv_credits;
806 spin_unlock(&t->receive_credit_lock);
807
808 if (receive_credits < t->recv_credit_target) {
809 while (true) {
810 if (use_free)
811 recvmsg = get_free_recvmsg(t);
812 else
813 recvmsg = get_empty_recvmsg(t);
814 if (!recvmsg) {
815 if (use_free) {
816 use_free = 0;
817 continue;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900818 } else {
Namjae Jeon0626e662021-03-16 13:07:11 +0900819 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900820 }
Namjae Jeon0626e662021-03-16 13:07:11 +0900821 }
822
823 recvmsg->type = SMB_DIRECT_MSG_DATA_TRANSFER;
824 recvmsg->first_segment = false;
825
826 ret = smb_direct_post_recv(t, recvmsg);
827 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900828 pr_err("Can't post recv: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +0900829 put_recvmsg(t, recvmsg);
830 break;
831 }
832 credits++;
833 }
834 }
835
836 spin_lock(&t->receive_credit_lock);
837 t->recv_credits += credits;
838 t->count_avail_recvmsg -= credits;
839 spin_unlock(&t->receive_credit_lock);
840
841 spin_lock(&t->lock_new_recv_credits);
842 t->new_recv_credits += credits;
843 spin_unlock(&t->lock_new_recv_credits);
844
845 if (credits)
846 queue_work(smb_direct_wq, &t->send_immediate_work);
847}
848
849static void send_done(struct ib_cq *cq, struct ib_wc *wc)
850{
851 struct smb_direct_sendmsg *sendmsg, *sibling;
852 struct smb_direct_transport *t;
853 struct list_head *pos, *prev, *end;
854
855 sendmsg = container_of(wc->wr_cqe, struct smb_direct_sendmsg, cqe);
856 t = sendmsg->transport;
857
858 ksmbd_debug(RDMA, "Send completed. status='%s (%d)', opcode=%d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900859 ib_wc_status_msg(wc->status), wc->status,
860 wc->opcode);
Namjae Jeon0626e662021-03-16 13:07:11 +0900861
862 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900863 pr_err("Send error. status='%s (%d)', opcode=%d\n",
864 ib_wc_status_msg(wc->status), wc->status,
865 wc->opcode);
Namjae Jeon0626e662021-03-16 13:07:11 +0900866 smb_direct_disconnect_rdma_connection(t);
867 }
868
869 if (sendmsg->num_sge > 1) {
870 if (atomic_dec_and_test(&t->send_payload_pending))
871 wake_up(&t->wait_send_payload_pending);
872 } else {
873 if (atomic_dec_and_test(&t->send_pending))
874 wake_up(&t->wait_send_pending);
875 }
876
877 /* iterate and free the list of messages in reverse. the list's head
878 * is invalid.
879 */
880 for (pos = &sendmsg->list, prev = pos->prev, end = sendmsg->list.next;
Namjae Jeon070fb212021-05-26 17:57:12 +0900881 prev != end; pos = prev, prev = prev->prev) {
Namjae Jeon0626e662021-03-16 13:07:11 +0900882 sibling = container_of(pos, struct smb_direct_sendmsg, list);
883 smb_direct_free_sendmsg(t, sibling);
884 }
885
886 sibling = container_of(pos, struct smb_direct_sendmsg, list);
887 smb_direct_free_sendmsg(t, sibling);
888}
889
890static int manage_credits_prior_sending(struct smb_direct_transport *t)
891{
892 int new_credits;
893
894 spin_lock(&t->lock_new_recv_credits);
895 new_credits = t->new_recv_credits;
896 t->new_recv_credits = 0;
897 spin_unlock(&t->lock_new_recv_credits);
898
899 return new_credits;
900}
901
902static int smb_direct_post_send(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900903 struct ib_send_wr *wr)
Namjae Jeon0626e662021-03-16 13:07:11 +0900904{
905 int ret;
906
907 if (wr->num_sge > 1)
908 atomic_inc(&t->send_payload_pending);
909 else
910 atomic_inc(&t->send_pending);
911
912 ret = ib_post_send(t->qp, wr, NULL);
913 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900914 pr_err("failed to post send: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +0900915 if (wr->num_sge > 1) {
916 if (atomic_dec_and_test(&t->send_payload_pending))
917 wake_up(&t->wait_send_payload_pending);
918 } else {
919 if (atomic_dec_and_test(&t->send_pending))
920 wake_up(&t->wait_send_pending);
921 }
922 smb_direct_disconnect_rdma_connection(t);
923 }
924 return ret;
925}
926
927static void smb_direct_send_ctx_init(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900928 struct smb_direct_send_ctx *send_ctx,
929 bool need_invalidate_rkey,
930 unsigned int remote_key)
Namjae Jeon0626e662021-03-16 13:07:11 +0900931{
932 INIT_LIST_HEAD(&send_ctx->msg_list);
933 send_ctx->wr_cnt = 0;
934 send_ctx->need_invalidate_rkey = need_invalidate_rkey;
935 send_ctx->remote_key = remote_key;
936}
937
938static int smb_direct_flush_send_list(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900939 struct smb_direct_send_ctx *send_ctx,
940 bool is_last)
Namjae Jeon0626e662021-03-16 13:07:11 +0900941{
942 struct smb_direct_sendmsg *first, *last;
943 int ret;
944
945 if (list_empty(&send_ctx->msg_list))
946 return 0;
947
948 first = list_first_entry(&send_ctx->msg_list,
Namjae Jeon070fb212021-05-26 17:57:12 +0900949 struct smb_direct_sendmsg,
950 list);
Namjae Jeon0626e662021-03-16 13:07:11 +0900951 last = list_last_entry(&send_ctx->msg_list,
Namjae Jeon070fb212021-05-26 17:57:12 +0900952 struct smb_direct_sendmsg,
953 list);
Namjae Jeon0626e662021-03-16 13:07:11 +0900954
955 last->wr.send_flags = IB_SEND_SIGNALED;
956 last->wr.wr_cqe = &last->cqe;
957 if (is_last && send_ctx->need_invalidate_rkey) {
958 last->wr.opcode = IB_WR_SEND_WITH_INV;
959 last->wr.ex.invalidate_rkey = send_ctx->remote_key;
960 }
961
962 ret = smb_direct_post_send(t, &first->wr);
963 if (!ret) {
964 smb_direct_send_ctx_init(t, send_ctx,
Namjae Jeon070fb212021-05-26 17:57:12 +0900965 send_ctx->need_invalidate_rkey,
966 send_ctx->remote_key);
Namjae Jeon0626e662021-03-16 13:07:11 +0900967 } else {
968 atomic_add(send_ctx->wr_cnt, &t->send_credits);
969 wake_up(&t->wait_send_credits);
970 list_for_each_entry_safe(first, last, &send_ctx->msg_list,
Namjae Jeon070fb212021-05-26 17:57:12 +0900971 list) {
Namjae Jeon0626e662021-03-16 13:07:11 +0900972 smb_direct_free_sendmsg(t, first);
973 }
974 }
975 return ret;
976}
977
978static int wait_for_credits(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +0900979 wait_queue_head_t *waitq, atomic_t *credits)
Namjae Jeon0626e662021-03-16 13:07:11 +0900980{
981 int ret;
982
983 do {
984 if (atomic_dec_return(credits) >= 0)
985 return 0;
986
987 atomic_inc(credits);
988 ret = wait_event_interruptible(*waitq,
Namjae Jeon070fb212021-05-26 17:57:12 +0900989 atomic_read(credits) > 0 ||
990 t->status != SMB_DIRECT_CS_CONNECTED);
Namjae Jeon0626e662021-03-16 13:07:11 +0900991
992 if (t->status != SMB_DIRECT_CS_CONNECTED)
993 return -ENOTCONN;
994 else if (ret < 0)
995 return ret;
996 } while (true);
997}
998
999static int wait_for_send_credits(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001000 struct smb_direct_send_ctx *send_ctx)
Namjae Jeon0626e662021-03-16 13:07:11 +09001001{
1002 int ret;
1003
Namjae Jeon070fb212021-05-26 17:57:12 +09001004 if (send_ctx &&
1005 (send_ctx->wr_cnt >= 16 || atomic_read(&t->send_credits) <= 1)) {
Namjae Jeon0626e662021-03-16 13:07:11 +09001006 ret = smb_direct_flush_send_list(t, send_ctx, false);
1007 if (ret)
1008 return ret;
1009 }
1010
1011 return wait_for_credits(t, &t->wait_send_credits, &t->send_credits);
1012}
1013
1014static int smb_direct_create_header(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001015 int size, int remaining_data_length,
1016 struct smb_direct_sendmsg **sendmsg_out)
Namjae Jeon0626e662021-03-16 13:07:11 +09001017{
1018 struct smb_direct_sendmsg *sendmsg;
1019 struct smb_direct_data_transfer *packet;
1020 int header_length;
1021 int ret;
1022
1023 sendmsg = smb_direct_alloc_sendmsg(t);
Dan Carpenter8ef32962021-03-18 16:09:37 +03001024 if (IS_ERR(sendmsg))
1025 return PTR_ERR(sendmsg);
Namjae Jeon0626e662021-03-16 13:07:11 +09001026
1027 /* Fill in the packet header */
1028 packet = (struct smb_direct_data_transfer *)sendmsg->packet;
1029 packet->credits_requested = cpu_to_le16(t->send_credit_target);
1030 packet->credits_granted = cpu_to_le16(manage_credits_prior_sending(t));
1031
1032 packet->flags = 0;
1033 packet->reserved = 0;
1034 if (!size)
1035 packet->data_offset = 0;
1036 else
1037 packet->data_offset = cpu_to_le32(24);
1038 packet->data_length = cpu_to_le32(size);
1039 packet->remaining_data_length = cpu_to_le32(remaining_data_length);
1040 packet->padding = 0;
1041
1042 ksmbd_debug(RDMA,
Namjae Jeon070fb212021-05-26 17:57:12 +09001043 "credits_requested=%d credits_granted=%d data_offset=%d data_length=%d remaining_data_length=%d\n",
1044 le16_to_cpu(packet->credits_requested),
1045 le16_to_cpu(packet->credits_granted),
1046 le32_to_cpu(packet->data_offset),
1047 le32_to_cpu(packet->data_length),
1048 le32_to_cpu(packet->remaining_data_length));
Namjae Jeon0626e662021-03-16 13:07:11 +09001049
1050 /* Map the packet to DMA */
1051 header_length = sizeof(struct smb_direct_data_transfer);
1052 /* If this is a packet without payload, don't send padding */
1053 if (!size)
1054 header_length =
1055 offsetof(struct smb_direct_data_transfer, padding);
1056
1057 sendmsg->sge[0].addr = ib_dma_map_single(t->cm_id->device,
1058 (void *)packet,
1059 header_length,
1060 DMA_TO_DEVICE);
1061 ret = ib_dma_mapping_error(t->cm_id->device, sendmsg->sge[0].addr);
1062 if (ret) {
1063 smb_direct_free_sendmsg(t, sendmsg);
1064 return ret;
1065 }
1066
1067 sendmsg->num_sge = 1;
1068 sendmsg->sge[0].length = header_length;
1069 sendmsg->sge[0].lkey = t->pd->local_dma_lkey;
1070
1071 *sendmsg_out = sendmsg;
1072 return 0;
1073}
1074
Namjae Jeon64b39f42021-03-30 14:25:35 +09001075static int get_sg_list(void *buf, int size, struct scatterlist *sg_list, int nentries)
Namjae Jeon0626e662021-03-16 13:07:11 +09001076{
1077 bool high = is_vmalloc_addr(buf);
1078 struct page *page;
1079 int offset, len;
1080 int i = 0;
1081
Hyunchul Lee8ad8dc32021-06-25 07:02:07 +09001082 if (nentries < get_buf_page_count(buf, size))
Namjae Jeon0626e662021-03-16 13:07:11 +09001083 return -EINVAL;
1084
1085 offset = offset_in_page(buf);
1086 buf -= offset;
1087 while (size > 0) {
1088 len = min_t(int, PAGE_SIZE - offset, size);
1089 if (high)
1090 page = vmalloc_to_page(buf);
1091 else
1092 page = kmap_to_page(buf);
1093
1094 if (!sg_list)
1095 return -EINVAL;
1096 sg_set_page(sg_list, page, len, offset);
1097 sg_list = sg_next(sg_list);
1098
1099 buf += PAGE_SIZE;
1100 size -= len;
1101 offset = 0;
1102 i++;
1103 }
1104 return i;
1105}
1106
1107static int get_mapped_sg_list(struct ib_device *device, void *buf, int size,
Namjae Jeon070fb212021-05-26 17:57:12 +09001108 struct scatterlist *sg_list, int nentries,
1109 enum dma_data_direction dir)
Namjae Jeon0626e662021-03-16 13:07:11 +09001110{
1111 int npages;
1112
1113 npages = get_sg_list(buf, size, sg_list, nentries);
1114 if (npages <= 0)
1115 return -EINVAL;
1116 return ib_dma_map_sg(device, sg_list, npages, dir);
1117}
1118
1119static int post_sendmsg(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001120 struct smb_direct_send_ctx *send_ctx,
1121 struct smb_direct_sendmsg *msg)
Namjae Jeon0626e662021-03-16 13:07:11 +09001122{
1123 int i;
1124
1125 for (i = 0; i < msg->num_sge; i++)
1126 ib_dma_sync_single_for_device(t->cm_id->device,
Namjae Jeon070fb212021-05-26 17:57:12 +09001127 msg->sge[i].addr, msg->sge[i].length,
1128 DMA_TO_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +09001129
1130 msg->cqe.done = send_done;
1131 msg->wr.opcode = IB_WR_SEND;
1132 msg->wr.sg_list = &msg->sge[0];
1133 msg->wr.num_sge = msg->num_sge;
1134 msg->wr.next = NULL;
1135
1136 if (send_ctx) {
1137 msg->wr.wr_cqe = NULL;
1138 msg->wr.send_flags = 0;
1139 if (!list_empty(&send_ctx->msg_list)) {
1140 struct smb_direct_sendmsg *last;
1141
1142 last = list_last_entry(&send_ctx->msg_list,
1143 struct smb_direct_sendmsg,
1144 list);
1145 last->wr.next = &msg->wr;
1146 }
1147 list_add_tail(&msg->list, &send_ctx->msg_list);
1148 send_ctx->wr_cnt++;
1149 return 0;
1150 }
1151
1152 msg->wr.wr_cqe = &msg->cqe;
1153 msg->wr.send_flags = IB_SEND_SIGNALED;
1154 return smb_direct_post_send(t, &msg->wr);
1155}
1156
1157static int smb_direct_post_send_data(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001158 struct smb_direct_send_ctx *send_ctx,
1159 struct kvec *iov, int niov,
1160 int remaining_data_length)
Namjae Jeon0626e662021-03-16 13:07:11 +09001161{
1162 int i, j, ret;
1163 struct smb_direct_sendmsg *msg;
1164 int data_length;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001165 struct scatterlist sg[SMB_DIRECT_MAX_SEND_SGES - 1];
Namjae Jeon0626e662021-03-16 13:07:11 +09001166
1167 ret = wait_for_send_credits(t, send_ctx);
1168 if (ret)
1169 return ret;
1170
1171 data_length = 0;
1172 for (i = 0; i < niov; i++)
1173 data_length += iov[i].iov_len;
1174
1175 ret = smb_direct_create_header(t, data_length, remaining_data_length,
1176 &msg);
1177 if (ret) {
1178 atomic_inc(&t->send_credits);
1179 return ret;
1180 }
1181
1182 for (i = 0; i < niov; i++) {
1183 struct ib_sge *sge;
1184 int sg_cnt;
1185
Namjae Jeon64b39f42021-03-30 14:25:35 +09001186 sg_init_table(sg, SMB_DIRECT_MAX_SEND_SGES - 1);
Namjae Jeon0626e662021-03-16 13:07:11 +09001187 sg_cnt = get_mapped_sg_list(t->cm_id->device,
Namjae Jeon070fb212021-05-26 17:57:12 +09001188 iov[i].iov_base, iov[i].iov_len,
1189 sg, SMB_DIRECT_MAX_SEND_SGES - 1,
1190 DMA_TO_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +09001191 if (sg_cnt <= 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001192 pr_err("failed to map buffer\n");
Namjae Jeonbc3fcc92021-03-19 13:51:15 +09001193 ret = -ENOMEM;
Namjae Jeon0626e662021-03-16 13:07:11 +09001194 goto err;
Hyunchul Lee72d6cbb2021-08-30 13:27:43 +09001195 } else if (sg_cnt + msg->num_sge > SMB_DIRECT_MAX_SEND_SGES) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001196 pr_err("buffer not fitted into sges\n");
Namjae Jeon0626e662021-03-16 13:07:11 +09001197 ret = -E2BIG;
1198 ib_dma_unmap_sg(t->cm_id->device, sg, sg_cnt,
1199 DMA_TO_DEVICE);
1200 goto err;
1201 }
1202
1203 for (j = 0; j < sg_cnt; j++) {
1204 sge = &msg->sge[msg->num_sge];
1205 sge->addr = sg_dma_address(&sg[j]);
1206 sge->length = sg_dma_len(&sg[j]);
1207 sge->lkey = t->pd->local_dma_lkey;
1208 msg->num_sge++;
1209 }
1210 }
1211
1212 ret = post_sendmsg(t, send_ctx, msg);
1213 if (ret)
1214 goto err;
1215 return 0;
1216err:
1217 smb_direct_free_sendmsg(t, msg);
1218 atomic_inc(&t->send_credits);
1219 return ret;
1220}
1221
1222static int smb_direct_writev(struct ksmbd_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001223 struct kvec *iov, int niovs, int buflen,
1224 bool need_invalidate, unsigned int remote_key)
Namjae Jeon0626e662021-03-16 13:07:11 +09001225{
Namjae Jeon02d4b4a2021-06-25 13:43:01 +09001226 struct smb_direct_transport *st = smb_trans_direct_transfort(t);
Namjae Jeon0626e662021-03-16 13:07:11 +09001227 int remaining_data_length;
1228 int start, i, j;
1229 int max_iov_size = st->max_send_size -
1230 sizeof(struct smb_direct_data_transfer);
1231 int ret;
1232 struct kvec vec;
1233 struct smb_direct_send_ctx send_ctx;
1234
Namjae Jeonb8fc94c2021-07-07 14:56:44 +09001235 if (st->status != SMB_DIRECT_CS_CONNECTED)
1236 return -ENOTCONN;
Namjae Jeon0626e662021-03-16 13:07:11 +09001237
1238 //FIXME: skip RFC1002 header..
1239 buflen -= 4;
1240 iov[0].iov_base += 4;
1241 iov[0].iov_len -= 4;
1242
1243 remaining_data_length = buflen;
1244 ksmbd_debug(RDMA, "Sending smb (RDMA): smb_len=%u\n", buflen);
1245
1246 smb_direct_send_ctx_init(st, &send_ctx, need_invalidate, remote_key);
1247 start = i = 0;
1248 buflen = 0;
1249 while (true) {
1250 buflen += iov[i].iov_len;
1251 if (buflen > max_iov_size) {
1252 if (i > start) {
1253 remaining_data_length -=
Namjae Jeon64b39f42021-03-30 14:25:35 +09001254 (buflen - iov[i].iov_len);
Namjae Jeon0626e662021-03-16 13:07:11 +09001255 ret = smb_direct_post_send_data(st, &send_ctx,
Namjae Jeon070fb212021-05-26 17:57:12 +09001256 &iov[start], i - start,
1257 remaining_data_length);
Namjae Jeon0626e662021-03-16 13:07:11 +09001258 if (ret)
1259 goto done;
1260 } else {
1261 /* iov[start] is too big, break it */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001262 int nvec = (buflen + max_iov_size - 1) /
Namjae Jeon0626e662021-03-16 13:07:11 +09001263 max_iov_size;
1264
1265 for (j = 0; j < nvec; j++) {
1266 vec.iov_base =
1267 (char *)iov[start].iov_base +
Namjae Jeon64b39f42021-03-30 14:25:35 +09001268 j * max_iov_size;
Namjae Jeon0626e662021-03-16 13:07:11 +09001269 vec.iov_len =
1270 min_t(int, max_iov_size,
Namjae Jeon070fb212021-05-26 17:57:12 +09001271 buflen - max_iov_size * j);
Namjae Jeon0626e662021-03-16 13:07:11 +09001272 remaining_data_length -= vec.iov_len;
Namjae Jeon070fb212021-05-26 17:57:12 +09001273 ret = smb_direct_post_send_data(st, &send_ctx, &vec, 1,
1274 remaining_data_length);
Namjae Jeon0626e662021-03-16 13:07:11 +09001275 if (ret)
1276 goto done;
1277 }
1278 i++;
1279 if (i == niovs)
1280 break;
1281 }
1282 start = i;
1283 buflen = 0;
1284 } else {
1285 i++;
1286 if (i == niovs) {
1287 /* send out all remaining vecs */
1288 remaining_data_length -= buflen;
1289 ret = smb_direct_post_send_data(st, &send_ctx,
Namjae Jeon070fb212021-05-26 17:57:12 +09001290 &iov[start], i - start,
1291 remaining_data_length);
Namjae Jeon0626e662021-03-16 13:07:11 +09001292 if (ret)
1293 goto done;
1294 break;
1295 }
1296 }
1297 }
1298
1299done:
1300 ret = smb_direct_flush_send_list(st, &send_ctx, true);
1301
1302 /*
1303 * As an optimization, we don't wait for individual I/O to finish
1304 * before sending the next one.
1305 * Send them all and wait for pending send count to get to 0
1306 * that means all the I/Os have been out and we are good to return
1307 */
1308
1309 wait_event(st->wait_send_payload_pending,
Namjae Jeon070fb212021-05-26 17:57:12 +09001310 atomic_read(&st->send_payload_pending) == 0);
Namjae Jeon0626e662021-03-16 13:07:11 +09001311 return ret;
1312}
1313
1314static void read_write_done(struct ib_cq *cq, struct ib_wc *wc,
Namjae Jeon070fb212021-05-26 17:57:12 +09001315 enum dma_data_direction dir)
Namjae Jeon0626e662021-03-16 13:07:11 +09001316{
1317 struct smb_direct_rdma_rw_msg *msg = container_of(wc->wr_cqe,
Namjae Jeon070fb212021-05-26 17:57:12 +09001318 struct smb_direct_rdma_rw_msg, cqe);
Namjae Jeon0626e662021-03-16 13:07:11 +09001319 struct smb_direct_transport *t = msg->t;
1320
1321 if (wc->status != IB_WC_SUCCESS) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001322 pr_err("read/write error. opcode = %d, status = %s(%d)\n",
1323 wc->opcode, ib_wc_status_msg(wc->status), wc->status);
Namjae Jeon0626e662021-03-16 13:07:11 +09001324 smb_direct_disconnect_rdma_connection(t);
1325 }
1326
1327 if (atomic_inc_return(&t->rw_avail_ops) > 0)
1328 wake_up(&t->wait_rw_avail_ops);
1329
1330 rdma_rw_ctx_destroy(&msg->rw_ctx, t->qp, t->qp->port,
Namjae Jeon070fb212021-05-26 17:57:12 +09001331 msg->sg_list, msg->sgt.nents, dir);
Namjae Jeon0626e662021-03-16 13:07:11 +09001332 sg_free_table_chained(&msg->sgt, SG_CHUNK_SIZE);
1333 complete(msg->completion);
1334 kfree(msg);
1335}
1336
1337static void read_done(struct ib_cq *cq, struct ib_wc *wc)
1338{
1339 read_write_done(cq, wc, DMA_FROM_DEVICE);
1340}
1341
1342static void write_done(struct ib_cq *cq, struct ib_wc *wc)
1343{
1344 read_write_done(cq, wc, DMA_TO_DEVICE);
1345}
1346
1347static int smb_direct_rdma_xmit(struct smb_direct_transport *t, void *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09001348 int buf_len, u32 remote_key, u64 remote_offset,
1349 u32 remote_len, bool is_read)
Namjae Jeon0626e662021-03-16 13:07:11 +09001350{
1351 struct smb_direct_rdma_rw_msg *msg;
1352 int ret;
1353 DECLARE_COMPLETION_ONSTACK(completion);
1354 struct ib_send_wr *first_wr = NULL;
1355
1356 ret = wait_for_credits(t, &t->wait_rw_avail_ops, &t->rw_avail_ops);
1357 if (ret < 0)
1358 return ret;
1359
1360 /* TODO: mempool */
1361 msg = kmalloc(offsetof(struct smb_direct_rdma_rw_msg, sg_list) +
Namjae Jeon070fb212021-05-26 17:57:12 +09001362 sizeof(struct scatterlist) * SG_CHUNK_SIZE, GFP_KERNEL);
Namjae Jeon0626e662021-03-16 13:07:11 +09001363 if (!msg) {
1364 atomic_inc(&t->rw_avail_ops);
1365 return -ENOMEM;
1366 }
1367
1368 msg->sgt.sgl = &msg->sg_list[0];
1369 ret = sg_alloc_table_chained(&msg->sgt,
Hyunchul Lee8ad8dc32021-06-25 07:02:07 +09001370 get_buf_page_count(buf, buf_len),
Namjae Jeon070fb212021-05-26 17:57:12 +09001371 msg->sg_list, SG_CHUNK_SIZE);
Namjae Jeon0626e662021-03-16 13:07:11 +09001372 if (ret) {
1373 atomic_inc(&t->rw_avail_ops);
1374 kfree(msg);
1375 return -ENOMEM;
1376 }
1377
1378 ret = get_sg_list(buf, buf_len, msg->sgt.sgl, msg->sgt.orig_nents);
1379 if (ret <= 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001380 pr_err("failed to get pages\n");
Namjae Jeon0626e662021-03-16 13:07:11 +09001381 goto err;
1382 }
1383
1384 ret = rdma_rw_ctx_init(&msg->rw_ctx, t->qp, t->qp->port,
Hyunchul Lee8ad8dc32021-06-25 07:02:07 +09001385 msg->sg_list, get_buf_page_count(buf, buf_len),
Namjae Jeon070fb212021-05-26 17:57:12 +09001386 0, remote_offset, remote_key,
1387 is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +09001388 if (ret < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001389 pr_err("failed to init rdma_rw_ctx: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001390 goto err;
1391 }
1392
1393 msg->t = t;
1394 msg->cqe.done = is_read ? read_done : write_done;
1395 msg->completion = &completion;
1396 first_wr = rdma_rw_ctx_wrs(&msg->rw_ctx, t->qp, t->qp->port,
Namjae Jeon070fb212021-05-26 17:57:12 +09001397 &msg->cqe, NULL);
Namjae Jeon0626e662021-03-16 13:07:11 +09001398
1399 ret = ib_post_send(t->qp, first_wr, NULL);
1400 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001401 pr_err("failed to post send wr: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001402 goto err;
1403 }
1404
1405 wait_for_completion(&completion);
1406 return 0;
1407
1408err:
1409 atomic_inc(&t->rw_avail_ops);
1410 if (first_wr)
1411 rdma_rw_ctx_destroy(&msg->rw_ctx, t->qp, t->qp->port,
Namjae Jeon070fb212021-05-26 17:57:12 +09001412 msg->sg_list, msg->sgt.nents,
1413 is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
Namjae Jeon0626e662021-03-16 13:07:11 +09001414 sg_free_table_chained(&msg->sgt, SG_CHUNK_SIZE);
1415 kfree(msg);
1416 return ret;
Namjae Jeon0626e662021-03-16 13:07:11 +09001417}
1418
Namjae Jeon64b39f42021-03-30 14:25:35 +09001419static int smb_direct_rdma_write(struct ksmbd_transport *t, void *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09001420 unsigned int buflen, u32 remote_key,
1421 u64 remote_offset, u32 remote_len)
Namjae Jeon0626e662021-03-16 13:07:11 +09001422{
Namjae Jeon02d4b4a2021-06-25 13:43:01 +09001423 return smb_direct_rdma_xmit(smb_trans_direct_transfort(t), buf, buflen,
Namjae Jeon070fb212021-05-26 17:57:12 +09001424 remote_key, remote_offset,
1425 remote_len, false);
Namjae Jeon0626e662021-03-16 13:07:11 +09001426}
1427
Namjae Jeon64b39f42021-03-30 14:25:35 +09001428static int smb_direct_rdma_read(struct ksmbd_transport *t, void *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09001429 unsigned int buflen, u32 remote_key,
1430 u64 remote_offset, u32 remote_len)
Namjae Jeon0626e662021-03-16 13:07:11 +09001431{
Namjae Jeon02d4b4a2021-06-25 13:43:01 +09001432 return smb_direct_rdma_xmit(smb_trans_direct_transfort(t), buf, buflen,
Namjae Jeon070fb212021-05-26 17:57:12 +09001433 remote_key, remote_offset,
1434 remote_len, true);
Namjae Jeon0626e662021-03-16 13:07:11 +09001435}
1436
1437static void smb_direct_disconnect(struct ksmbd_transport *t)
1438{
Namjae Jeon02d4b4a2021-06-25 13:43:01 +09001439 struct smb_direct_transport *st = smb_trans_direct_transfort(t);
Namjae Jeon0626e662021-03-16 13:07:11 +09001440
1441 ksmbd_debug(RDMA, "Disconnecting cm_id=%p\n", st->cm_id);
1442
Hyunchul Lee323b1ea2021-08-12 10:23:08 +09001443 smb_direct_disconnect_rdma_work(&st->disconnect_work);
Namjae Jeon0626e662021-03-16 13:07:11 +09001444 wait_event_interruptible(st->wait_status,
Namjae Jeon070fb212021-05-26 17:57:12 +09001445 st->status == SMB_DIRECT_CS_DISCONNECTED);
Namjae Jeon0626e662021-03-16 13:07:11 +09001446 free_transport(st);
1447}
1448
1449static int smb_direct_cm_handler(struct rdma_cm_id *cm_id,
Namjae Jeon070fb212021-05-26 17:57:12 +09001450 struct rdma_cm_event *event)
Namjae Jeon0626e662021-03-16 13:07:11 +09001451{
1452 struct smb_direct_transport *t = cm_id->context;
1453
1454 ksmbd_debug(RDMA, "RDMA CM event. cm_id=%p event=%s (%d)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001455 cm_id, rdma_event_msg(event->event), event->event);
Namjae Jeon0626e662021-03-16 13:07:11 +09001456
1457 switch (event->event) {
1458 case RDMA_CM_EVENT_ESTABLISHED: {
1459 t->status = SMB_DIRECT_CS_CONNECTED;
1460 wake_up_interruptible(&t->wait_status);
1461 break;
1462 }
1463 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1464 case RDMA_CM_EVENT_DISCONNECTED: {
1465 t->status = SMB_DIRECT_CS_DISCONNECTED;
1466 wake_up_interruptible(&t->wait_status);
1467 wake_up_interruptible(&t->wait_reassembly_queue);
1468 wake_up(&t->wait_send_credits);
1469 break;
1470 }
1471 case RDMA_CM_EVENT_CONNECT_ERROR: {
1472 t->status = SMB_DIRECT_CS_DISCONNECTED;
1473 wake_up_interruptible(&t->wait_status);
1474 break;
1475 }
1476 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09001477 pr_err("Unexpected RDMA CM event. cm_id=%p, event=%s (%d)\n",
1478 cm_id, rdma_event_msg(event->event),
1479 event->event);
Namjae Jeon0626e662021-03-16 13:07:11 +09001480 break;
1481 }
1482 return 0;
1483}
1484
1485static void smb_direct_qpair_handler(struct ib_event *event, void *context)
1486{
1487 struct smb_direct_transport *t = context;
1488
1489 ksmbd_debug(RDMA, "Received QP event. cm_id=%p, event=%s (%d)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001490 t->cm_id, ib_event_msg(event->event), event->event);
Namjae Jeon0626e662021-03-16 13:07:11 +09001491
1492 switch (event->event) {
1493 case IB_EVENT_CQ_ERR:
1494 case IB_EVENT_QP_FATAL:
1495 smb_direct_disconnect_rdma_connection(t);
1496 break;
1497 default:
1498 break;
1499 }
1500}
1501
1502static int smb_direct_send_negotiate_response(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001503 int failed)
Namjae Jeon0626e662021-03-16 13:07:11 +09001504{
1505 struct smb_direct_sendmsg *sendmsg;
1506 struct smb_direct_negotiate_resp *resp;
1507 int ret;
1508
1509 sendmsg = smb_direct_alloc_sendmsg(t);
1510 if (IS_ERR(sendmsg))
1511 return -ENOMEM;
1512
1513 resp = (struct smb_direct_negotiate_resp *)sendmsg->packet;
1514 if (failed) {
1515 memset(resp, 0, sizeof(*resp));
1516 resp->min_version = cpu_to_le16(0x0100);
1517 resp->max_version = cpu_to_le16(0x0100);
1518 resp->status = STATUS_NOT_SUPPORTED;
1519 } else {
1520 resp->status = STATUS_SUCCESS;
1521 resp->min_version = SMB_DIRECT_VERSION_LE;
1522 resp->max_version = SMB_DIRECT_VERSION_LE;
1523 resp->negotiated_version = SMB_DIRECT_VERSION_LE;
1524 resp->reserved = 0;
1525 resp->credits_requested =
1526 cpu_to_le16(t->send_credit_target);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001527 resp->credits_granted = cpu_to_le16(manage_credits_prior_sending(t));
Namjae Jeon0626e662021-03-16 13:07:11 +09001528 resp->max_readwrite_size = cpu_to_le32(t->max_rdma_rw_size);
1529 resp->preferred_send_size = cpu_to_le32(t->max_send_size);
1530 resp->max_receive_size = cpu_to_le32(t->max_recv_size);
1531 resp->max_fragmented_size =
1532 cpu_to_le32(t->max_fragmented_recv_size);
1533 }
1534
1535 sendmsg->sge[0].addr = ib_dma_map_single(t->cm_id->device,
Namjae Jeon070fb212021-05-26 17:57:12 +09001536 (void *)resp, sizeof(*resp),
1537 DMA_TO_DEVICE);
1538 ret = ib_dma_mapping_error(t->cm_id->device, sendmsg->sge[0].addr);
Namjae Jeon0626e662021-03-16 13:07:11 +09001539 if (ret) {
1540 smb_direct_free_sendmsg(t, sendmsg);
1541 return ret;
1542 }
1543
1544 sendmsg->num_sge = 1;
1545 sendmsg->sge[0].length = sizeof(*resp);
1546 sendmsg->sge[0].lkey = t->pd->local_dma_lkey;
1547
1548 ret = post_sendmsg(t, NULL, sendmsg);
1549 if (ret) {
1550 smb_direct_free_sendmsg(t, sendmsg);
1551 return ret;
1552 }
1553
1554 wait_event(t->wait_send_pending,
Namjae Jeon070fb212021-05-26 17:57:12 +09001555 atomic_read(&t->send_pending) == 0);
Namjae Jeon0626e662021-03-16 13:07:11 +09001556 return 0;
1557}
1558
1559static int smb_direct_accept_client(struct smb_direct_transport *t)
1560{
1561 struct rdma_conn_param conn_param;
1562 struct ib_port_immutable port_immutable;
1563 u32 ird_ord_hdr[2];
1564 int ret;
1565
1566 memset(&conn_param, 0, sizeof(conn_param));
Namjae Jeon070fb212021-05-26 17:57:12 +09001567 conn_param.initiator_depth = min_t(u8, t->cm_id->device->attrs.max_qp_rd_atom,
1568 SMB_DIRECT_CM_INITIATOR_DEPTH);
Namjae Jeon0626e662021-03-16 13:07:11 +09001569 conn_param.responder_resources = 0;
1570
1571 t->cm_id->device->ops.get_port_immutable(t->cm_id->device,
Namjae Jeon070fb212021-05-26 17:57:12 +09001572 t->cm_id->port_num,
1573 &port_immutable);
Namjae Jeon0626e662021-03-16 13:07:11 +09001574 if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1575 ird_ord_hdr[0] = conn_param.responder_resources;
1576 ird_ord_hdr[1] = 1;
1577 conn_param.private_data = ird_ord_hdr;
1578 conn_param.private_data_len = sizeof(ird_ord_hdr);
1579 } else {
1580 conn_param.private_data = NULL;
1581 conn_param.private_data_len = 0;
1582 }
1583 conn_param.retry_count = SMB_DIRECT_CM_RETRY;
1584 conn_param.rnr_retry_count = SMB_DIRECT_CM_RNR_RETRY;
1585 conn_param.flow_control = 0;
1586
1587 ret = rdma_accept(t->cm_id, &conn_param);
1588 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001589 pr_err("error at rdma_accept: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001590 return ret;
1591 }
1592
1593 wait_event_interruptible(t->wait_status,
1594 t->status != SMB_DIRECT_CS_NEW);
1595 if (t->status != SMB_DIRECT_CS_CONNECTED)
1596 return -ENOTCONN;
1597 return 0;
1598}
1599
1600static int smb_direct_negotiate(struct smb_direct_transport *t)
1601{
1602 int ret;
1603 struct smb_direct_recvmsg *recvmsg;
1604 struct smb_direct_negotiate_req *req;
1605
1606 recvmsg = get_free_recvmsg(t);
1607 if (!recvmsg)
1608 return -ENOMEM;
1609 recvmsg->type = SMB_DIRECT_MSG_NEGOTIATE_REQ;
1610
1611 ret = smb_direct_post_recv(t, recvmsg);
1612 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001613 pr_err("Can't post recv: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001614 goto out;
1615 }
1616
1617 t->negotiation_requested = false;
1618 ret = smb_direct_accept_client(t);
1619 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001620 pr_err("Can't accept client\n");
Namjae Jeon0626e662021-03-16 13:07:11 +09001621 goto out;
1622 }
1623
1624 smb_direct_post_recv_credits(&t->post_recv_credits_work.work);
1625
1626 ksmbd_debug(RDMA, "Waiting for SMB_DIRECT negotiate request\n");
1627 ret = wait_event_interruptible_timeout(t->wait_status,
Namjae Jeon070fb212021-05-26 17:57:12 +09001628 t->negotiation_requested ||
1629 t->status == SMB_DIRECT_CS_DISCONNECTED,
1630 SMB_DIRECT_NEGOTIATE_TIMEOUT * HZ);
Namjae Jeon0626e662021-03-16 13:07:11 +09001631 if (ret <= 0 || t->status == SMB_DIRECT_CS_DISCONNECTED) {
1632 ret = ret < 0 ? ret : -ETIMEDOUT;
1633 goto out;
1634 }
1635
1636 ret = smb_direct_check_recvmsg(recvmsg);
1637 if (ret == -ECONNABORTED)
1638 goto out;
1639
1640 req = (struct smb_direct_negotiate_req *)recvmsg->packet;
1641 t->max_recv_size = min_t(int, t->max_recv_size,
Namjae Jeon070fb212021-05-26 17:57:12 +09001642 le32_to_cpu(req->preferred_send_size));
Namjae Jeon0626e662021-03-16 13:07:11 +09001643 t->max_send_size = min_t(int, t->max_send_size,
Namjae Jeon070fb212021-05-26 17:57:12 +09001644 le32_to_cpu(req->max_receive_size));
Namjae Jeon0626e662021-03-16 13:07:11 +09001645 t->max_fragmented_send_size =
1646 le32_to_cpu(req->max_fragmented_size);
1647
1648 ret = smb_direct_send_negotiate_response(t, ret);
1649out:
1650 if (recvmsg)
1651 put_recvmsg(t, recvmsg);
1652 return ret;
1653}
1654
1655static int smb_direct_init_params(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001656 struct ib_qp_cap *cap)
Namjae Jeon0626e662021-03-16 13:07:11 +09001657{
1658 struct ib_device *device = t->cm_id->device;
1659 int max_send_sges, max_pages, max_rw_wrs, max_send_wrs;
1660
1661 /* need 2 more sge. because a SMB_DIRECT header will be mapped,
1662 * and maybe a send buffer could be not page aligned.
1663 */
1664 t->max_send_size = smb_direct_max_send_size;
1665 max_send_sges = DIV_ROUND_UP(t->max_send_size, PAGE_SIZE) + 2;
1666 if (max_send_sges > SMB_DIRECT_MAX_SEND_SGES) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001667 pr_err("max_send_size %d is too large\n", t->max_send_size);
Namjae Jeon0626e662021-03-16 13:07:11 +09001668 return -EINVAL;
1669 }
1670
1671 /*
1672 * allow smb_direct_max_outstanding_rw_ops of in-flight RDMA
1673 * read/writes. HCA guarantees at least max_send_sge of sges for
1674 * a RDMA read/write work request, and if memory registration is used,
1675 * we need reg_mr, local_inv wrs for each read/write.
1676 */
1677 t->max_rdma_rw_size = smb_direct_max_read_write_size;
1678 max_pages = DIV_ROUND_UP(t->max_rdma_rw_size, PAGE_SIZE) + 1;
1679 max_rw_wrs = DIV_ROUND_UP(max_pages, SMB_DIRECT_MAX_SEND_SGES);
1680 max_rw_wrs += rdma_rw_mr_factor(device, t->cm_id->port_num,
1681 max_pages) * 2;
1682 max_rw_wrs *= smb_direct_max_outstanding_rw_ops;
1683
1684 max_send_wrs = smb_direct_send_credit_target + max_rw_wrs;
1685 if (max_send_wrs > device->attrs.max_cqe ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001686 max_send_wrs > device->attrs.max_qp_wr) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001687 pr_err("consider lowering send_credit_target = %d, or max_outstanding_rw_ops = %d\n",
1688 smb_direct_send_credit_target,
1689 smb_direct_max_outstanding_rw_ops);
1690 pr_err("Possible CQE overrun, device reporting max_cqe %d max_qp_wr %d\n",
1691 device->attrs.max_cqe, device->attrs.max_qp_wr);
Namjae Jeon0626e662021-03-16 13:07:11 +09001692 return -EINVAL;
1693 }
1694
1695 if (smb_direct_receive_credit_max > device->attrs.max_cqe ||
1696 smb_direct_receive_credit_max > device->attrs.max_qp_wr) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001697 pr_err("consider lowering receive_credit_max = %d\n",
1698 smb_direct_receive_credit_max);
1699 pr_err("Possible CQE overrun, device reporting max_cpe %d max_qp_wr %d\n",
1700 device->attrs.max_cqe, device->attrs.max_qp_wr);
Namjae Jeon0626e662021-03-16 13:07:11 +09001701 return -EINVAL;
1702 }
1703
1704 if (device->attrs.max_send_sge < SMB_DIRECT_MAX_SEND_SGES) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001705 pr_err("warning: device max_send_sge = %d too small\n",
1706 device->attrs.max_send_sge);
Namjae Jeon0626e662021-03-16 13:07:11 +09001707 return -EINVAL;
1708 }
1709 if (device->attrs.max_recv_sge < SMB_DIRECT_MAX_RECV_SGES) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001710 pr_err("warning: device max_recv_sge = %d too small\n",
1711 device->attrs.max_recv_sge);
Namjae Jeon0626e662021-03-16 13:07:11 +09001712 return -EINVAL;
1713 }
1714
1715 t->recv_credits = 0;
1716 t->count_avail_recvmsg = 0;
1717
1718 t->recv_credit_max = smb_direct_receive_credit_max;
1719 t->recv_credit_target = 10;
1720 t->new_recv_credits = 0;
1721
1722 t->send_credit_target = smb_direct_send_credit_target;
1723 atomic_set(&t->send_credits, 0);
1724 atomic_set(&t->rw_avail_ops, smb_direct_max_outstanding_rw_ops);
1725
1726 t->max_send_size = smb_direct_max_send_size;
1727 t->max_recv_size = smb_direct_max_receive_size;
1728 t->max_fragmented_recv_size = smb_direct_max_fragmented_recv_size;
1729
1730 cap->max_send_wr = max_send_wrs;
1731 cap->max_recv_wr = t->recv_credit_max;
1732 cap->max_send_sge = SMB_DIRECT_MAX_SEND_SGES;
1733 cap->max_recv_sge = SMB_DIRECT_MAX_RECV_SGES;
1734 cap->max_inline_data = 0;
1735 cap->max_rdma_ctxs = 0;
1736 return 0;
1737}
1738
1739static void smb_direct_destroy_pools(struct smb_direct_transport *t)
1740{
1741 struct smb_direct_recvmsg *recvmsg;
1742
1743 while ((recvmsg = get_free_recvmsg(t)))
1744 mempool_free(recvmsg, t->recvmsg_mempool);
1745 while ((recvmsg = get_empty_recvmsg(t)))
1746 mempool_free(recvmsg, t->recvmsg_mempool);
1747
1748 mempool_destroy(t->recvmsg_mempool);
1749 t->recvmsg_mempool = NULL;
1750
1751 kmem_cache_destroy(t->recvmsg_cache);
1752 t->recvmsg_cache = NULL;
1753
1754 mempool_destroy(t->sendmsg_mempool);
1755 t->sendmsg_mempool = NULL;
1756
1757 kmem_cache_destroy(t->sendmsg_cache);
1758 t->sendmsg_cache = NULL;
1759}
1760
1761static int smb_direct_create_pools(struct smb_direct_transport *t)
1762{
1763 char name[80];
1764 int i;
1765 struct smb_direct_recvmsg *recvmsg;
1766
1767 snprintf(name, sizeof(name), "smb_direct_rqst_pool_%p", t);
1768 t->sendmsg_cache = kmem_cache_create(name,
Namjae Jeon070fb212021-05-26 17:57:12 +09001769 sizeof(struct smb_direct_sendmsg) +
1770 sizeof(struct smb_direct_negotiate_resp),
1771 0, SLAB_HWCACHE_ALIGN, NULL);
Namjae Jeon0626e662021-03-16 13:07:11 +09001772 if (!t->sendmsg_cache)
1773 return -ENOMEM;
1774
1775 t->sendmsg_mempool = mempool_create(t->send_credit_target,
Namjae Jeon070fb212021-05-26 17:57:12 +09001776 mempool_alloc_slab, mempool_free_slab,
1777 t->sendmsg_cache);
Namjae Jeon0626e662021-03-16 13:07:11 +09001778 if (!t->sendmsg_mempool)
1779 goto err;
1780
1781 snprintf(name, sizeof(name), "smb_direct_resp_%p", t);
1782 t->recvmsg_cache = kmem_cache_create(name,
Namjae Jeon070fb212021-05-26 17:57:12 +09001783 sizeof(struct smb_direct_recvmsg) +
1784 t->max_recv_size,
1785 0, SLAB_HWCACHE_ALIGN, NULL);
Namjae Jeon0626e662021-03-16 13:07:11 +09001786 if (!t->recvmsg_cache)
1787 goto err;
1788
1789 t->recvmsg_mempool =
1790 mempool_create(t->recv_credit_max, mempool_alloc_slab,
Namjae Jeon070fb212021-05-26 17:57:12 +09001791 mempool_free_slab, t->recvmsg_cache);
Namjae Jeon0626e662021-03-16 13:07:11 +09001792 if (!t->recvmsg_mempool)
1793 goto err;
1794
1795 INIT_LIST_HEAD(&t->recvmsg_queue);
1796
1797 for (i = 0; i < t->recv_credit_max; i++) {
1798 recvmsg = mempool_alloc(t->recvmsg_mempool, GFP_KERNEL);
1799 if (!recvmsg)
1800 goto err;
1801 recvmsg->transport = t;
1802 list_add(&recvmsg->list, &t->recvmsg_queue);
1803 }
1804 t->count_avail_recvmsg = t->recv_credit_max;
1805
1806 return 0;
1807err:
1808 smb_direct_destroy_pools(t);
1809 return -ENOMEM;
1810}
1811
1812static int smb_direct_create_qpair(struct smb_direct_transport *t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001813 struct ib_qp_cap *cap)
Namjae Jeon0626e662021-03-16 13:07:11 +09001814{
1815 int ret;
1816 struct ib_qp_init_attr qp_attr;
1817
1818 t->pd = ib_alloc_pd(t->cm_id->device, 0);
1819 if (IS_ERR(t->pd)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001820 pr_err("Can't create RDMA PD\n");
Namjae Jeon0626e662021-03-16 13:07:11 +09001821 ret = PTR_ERR(t->pd);
1822 t->pd = NULL;
1823 return ret;
1824 }
1825
1826 t->send_cq = ib_alloc_cq(t->cm_id->device, t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001827 t->send_credit_target, 0, IB_POLL_WORKQUEUE);
Namjae Jeon0626e662021-03-16 13:07:11 +09001828 if (IS_ERR(t->send_cq)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001829 pr_err("Can't create RDMA send CQ\n");
Namjae Jeon0626e662021-03-16 13:07:11 +09001830 ret = PTR_ERR(t->send_cq);
1831 t->send_cq = NULL;
1832 goto err;
1833 }
1834
1835 t->recv_cq = ib_alloc_cq(t->cm_id->device, t,
Namjae Jeon070fb212021-05-26 17:57:12 +09001836 cap->max_send_wr + cap->max_rdma_ctxs,
1837 0, IB_POLL_WORKQUEUE);
Namjae Jeon0626e662021-03-16 13:07:11 +09001838 if (IS_ERR(t->recv_cq)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001839 pr_err("Can't create RDMA recv CQ\n");
Namjae Jeon0626e662021-03-16 13:07:11 +09001840 ret = PTR_ERR(t->recv_cq);
1841 t->recv_cq = NULL;
1842 goto err;
1843 }
1844
1845 memset(&qp_attr, 0, sizeof(qp_attr));
1846 qp_attr.event_handler = smb_direct_qpair_handler;
1847 qp_attr.qp_context = t;
1848 qp_attr.cap = *cap;
1849 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1850 qp_attr.qp_type = IB_QPT_RC;
1851 qp_attr.send_cq = t->send_cq;
1852 qp_attr.recv_cq = t->recv_cq;
1853 qp_attr.port_num = ~0;
1854
1855 ret = rdma_create_qp(t->cm_id, t->pd, &qp_attr);
1856 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001857 pr_err("Can't create RDMA QP: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001858 goto err;
1859 }
1860
1861 t->qp = t->cm_id->qp;
1862 t->cm_id->event_handler = smb_direct_cm_handler;
1863
1864 return 0;
1865err:
1866 if (t->qp) {
1867 ib_destroy_qp(t->qp);
1868 t->qp = NULL;
1869 }
1870 if (t->recv_cq) {
1871 ib_destroy_cq(t->recv_cq);
1872 t->recv_cq = NULL;
1873 }
1874 if (t->send_cq) {
1875 ib_destroy_cq(t->send_cq);
1876 t->send_cq = NULL;
1877 }
1878 if (t->pd) {
1879 ib_dealloc_pd(t->pd);
1880 t->pd = NULL;
1881 }
1882 return ret;
1883}
1884
1885static int smb_direct_prepare(struct ksmbd_transport *t)
1886{
Namjae Jeon02d4b4a2021-06-25 13:43:01 +09001887 struct smb_direct_transport *st = smb_trans_direct_transfort(t);
Namjae Jeon0626e662021-03-16 13:07:11 +09001888 int ret;
1889 struct ib_qp_cap qp_cap;
1890
1891 ret = smb_direct_init_params(st, &qp_cap);
1892 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001893 pr_err("Can't configure RDMA parameters\n");
Namjae Jeon0626e662021-03-16 13:07:11 +09001894 return ret;
1895 }
1896
1897 ret = smb_direct_create_pools(st);
1898 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001899 pr_err("Can't init RDMA pool: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001900 return ret;
1901 }
1902
1903 ret = smb_direct_create_qpair(st, &qp_cap);
1904 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001905 pr_err("Can't accept RDMA client: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001906 return ret;
1907 }
1908
1909 ret = smb_direct_negotiate(st);
1910 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001911 pr_err("Can't negotiate: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001912 return ret;
1913 }
1914
1915 st->status = SMB_DIRECT_CS_CONNECTED;
1916 return 0;
1917}
1918
1919static bool rdma_frwr_is_supported(struct ib_device_attr *attrs)
1920{
1921 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
1922 return false;
1923 if (attrs->max_fast_reg_page_list_len == 0)
1924 return false;
1925 return true;
1926}
1927
1928static int smb_direct_handle_connect_request(struct rdma_cm_id *new_cm_id)
1929{
1930 struct smb_direct_transport *t;
1931
1932 if (!rdma_frwr_is_supported(&new_cm_id->device->attrs)) {
1933 ksmbd_debug(RDMA,
Namjae Jeon070fb212021-05-26 17:57:12 +09001934 "Fast Registration Work Requests is not supported. device capabilities=%llx\n",
1935 new_cm_id->device->attrs.device_cap_flags);
Namjae Jeon0626e662021-03-16 13:07:11 +09001936 return -EPROTONOSUPPORT;
1937 }
1938
1939 t = alloc_transport(new_cm_id);
1940 if (!t)
1941 return -ENOMEM;
1942
1943 KSMBD_TRANS(t)->handler = kthread_run(ksmbd_conn_handler_loop,
Namjae Jeon070fb212021-05-26 17:57:12 +09001944 KSMBD_TRANS(t)->conn, "ksmbd:r%u",
1945 SMB_DIRECT_PORT);
Namjae Jeon0626e662021-03-16 13:07:11 +09001946 if (IS_ERR(KSMBD_TRANS(t)->handler)) {
1947 int ret = PTR_ERR(KSMBD_TRANS(t)->handler);
1948
Namjae Jeonbde16942021-06-28 15:23:19 +09001949 pr_err("Can't start thread\n");
Namjae Jeon0626e662021-03-16 13:07:11 +09001950 free_transport(t);
1951 return ret;
1952 }
1953
1954 return 0;
1955}
1956
1957static int smb_direct_listen_handler(struct rdma_cm_id *cm_id,
Namjae Jeon070fb212021-05-26 17:57:12 +09001958 struct rdma_cm_event *event)
Namjae Jeon0626e662021-03-16 13:07:11 +09001959{
1960 switch (event->event) {
1961 case RDMA_CM_EVENT_CONNECT_REQUEST: {
1962 int ret = smb_direct_handle_connect_request(cm_id);
1963
1964 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001965 pr_err("Can't create transport: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09001966 return ret;
1967 }
1968
1969 ksmbd_debug(RDMA, "Received connection request. cm_id=%p\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001970 cm_id);
Namjae Jeon0626e662021-03-16 13:07:11 +09001971 break;
1972 }
1973 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09001974 pr_err("Unexpected listen event. cm_id=%p, event=%s (%d)\n",
1975 cm_id, rdma_event_msg(event->event), event->event);
Namjae Jeon0626e662021-03-16 13:07:11 +09001976 break;
1977 }
1978 return 0;
1979}
1980
1981static int smb_direct_listen(int port)
1982{
1983 int ret;
1984 struct rdma_cm_id *cm_id;
1985 struct sockaddr_in sin = {
1986 .sin_family = AF_INET,
1987 .sin_addr.s_addr = htonl(INADDR_ANY),
1988 .sin_port = htons(port),
1989 };
1990
1991 cm_id = rdma_create_id(&init_net, smb_direct_listen_handler,
Namjae Jeon070fb212021-05-26 17:57:12 +09001992 &smb_direct_listener, RDMA_PS_TCP, IB_QPT_RC);
Namjae Jeon0626e662021-03-16 13:07:11 +09001993 if (IS_ERR(cm_id)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001994 pr_err("Can't create cm id: %ld\n", PTR_ERR(cm_id));
Namjae Jeon0626e662021-03-16 13:07:11 +09001995 return PTR_ERR(cm_id);
1996 }
1997
1998 ret = rdma_bind_addr(cm_id, (struct sockaddr *)&sin);
1999 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002000 pr_err("Can't bind: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09002001 goto err;
2002 }
2003
2004 smb_direct_listener.cm_id = cm_id;
2005
2006 ret = rdma_listen(cm_id, 10);
2007 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002008 pr_err("Can't listen: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09002009 goto err;
2010 }
2011 return 0;
2012err:
2013 smb_direct_listener.cm_id = NULL;
2014 rdma_destroy_id(cm_id);
2015 return ret;
2016}
2017
Hyunchul Lee31928a02021-12-29 23:02:15 +09002018static int smb_direct_ib_client_add(struct ib_device *ib_dev)
2019{
2020 struct smb_direct_device *smb_dev;
2021
2022 if (!ib_dev->ops.get_netdev ||
2023 !rdma_frwr_is_supported(&ib_dev->attrs))
2024 return 0;
2025
2026 smb_dev = kzalloc(sizeof(*smb_dev), GFP_KERNEL);
2027 if (!smb_dev)
2028 return -ENOMEM;
2029 smb_dev->ib_dev = ib_dev;
2030
2031 write_lock(&smb_direct_device_lock);
2032 list_add(&smb_dev->list, &smb_direct_device_list);
2033 write_unlock(&smb_direct_device_lock);
2034
2035 ksmbd_debug(RDMA, "ib device added: name %s\n", ib_dev->name);
2036 return 0;
2037}
2038
2039static void smb_direct_ib_client_remove(struct ib_device *ib_dev,
2040 void *client_data)
2041{
2042 struct smb_direct_device *smb_dev, *tmp;
2043
2044 write_lock(&smb_direct_device_lock);
2045 list_for_each_entry_safe(smb_dev, tmp, &smb_direct_device_list, list) {
2046 if (smb_dev->ib_dev == ib_dev) {
2047 list_del(&smb_dev->list);
2048 kfree(smb_dev);
2049 break;
2050 }
2051 }
2052 write_unlock(&smb_direct_device_lock);
2053}
2054
2055static struct ib_client smb_direct_ib_client = {
2056 .name = "ksmbd_smb_direct_ib",
2057 .add = smb_direct_ib_client_add,
2058 .remove = smb_direct_ib_client_remove,
2059};
2060
Namjae Jeon0626e662021-03-16 13:07:11 +09002061int ksmbd_rdma_init(void)
2062{
2063 int ret;
2064
2065 smb_direct_listener.cm_id = NULL;
2066
Hyunchul Lee31928a02021-12-29 23:02:15 +09002067 ret = ib_register_client(&smb_direct_ib_client);
2068 if (ret) {
2069 pr_err("failed to ib_register_client\n");
2070 return ret;
2071 }
2072
Namjae Jeon0626e662021-03-16 13:07:11 +09002073 /* When a client is running out of send credits, the credits are
2074 * granted by the server's sending a packet using this queue.
2075 * This avoids the situation that a clients cannot send packets
2076 * for lack of credits
2077 */
2078 smb_direct_wq = alloc_workqueue("ksmbd-smb_direct-wq",
Namjae Jeon070fb212021-05-26 17:57:12 +09002079 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
Namjae Jeon0626e662021-03-16 13:07:11 +09002080 if (!smb_direct_wq)
2081 return -ENOMEM;
2082
2083 ret = smb_direct_listen(SMB_DIRECT_PORT);
2084 if (ret) {
2085 destroy_workqueue(smb_direct_wq);
2086 smb_direct_wq = NULL;
Namjae Jeonbde16942021-06-28 15:23:19 +09002087 pr_err("Can't listen: %d\n", ret);
Namjae Jeon0626e662021-03-16 13:07:11 +09002088 return ret;
2089 }
2090
2091 ksmbd_debug(RDMA, "init RDMA listener. cm_id=%p\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002092 smb_direct_listener.cm_id);
Namjae Jeon0626e662021-03-16 13:07:11 +09002093 return 0;
2094}
2095
Hyunchul Lee31928a02021-12-29 23:02:15 +09002096void ksmbd_rdma_destroy(void)
Namjae Jeon0626e662021-03-16 13:07:11 +09002097{
Hyunchul Lee31928a02021-12-29 23:02:15 +09002098 if (!smb_direct_listener.cm_id)
2099 return;
2100
2101 ib_unregister_client(&smb_direct_ib_client);
2102 rdma_destroy_id(smb_direct_listener.cm_id);
2103
Namjae Jeon0626e662021-03-16 13:07:11 +09002104 smb_direct_listener.cm_id = NULL;
2105
2106 if (smb_direct_wq) {
Namjae Jeon0626e662021-03-16 13:07:11 +09002107 destroy_workqueue(smb_direct_wq);
2108 smb_direct_wq = NULL;
2109 }
Namjae Jeon0626e662021-03-16 13:07:11 +09002110}
2111
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09002112bool ksmbd_rdma_capable_netdev(struct net_device *netdev)
2113{
Hyunchul Lee31928a02021-12-29 23:02:15 +09002114 struct smb_direct_device *smb_dev;
2115 int i;
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09002116 bool rdma_capable = false;
2117
Hyunchul Lee31928a02021-12-29 23:02:15 +09002118 read_lock(&smb_direct_device_lock);
2119 list_for_each_entry(smb_dev, &smb_direct_device_list, list) {
2120 for (i = 0; i < smb_dev->ib_dev->phys_port_cnt; i++) {
2121 struct net_device *ndev;
2122
2123 ndev = smb_dev->ib_dev->ops.get_netdev(smb_dev->ib_dev,
2124 i + 1);
2125 if (!ndev)
2126 continue;
2127
2128 if (ndev == netdev) {
2129 dev_put(ndev);
2130 rdma_capable = true;
2131 goto out;
2132 }
2133 dev_put(ndev);
2134 }
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09002135 }
Hyunchul Lee31928a02021-12-29 23:02:15 +09002136out:
2137 read_unlock(&smb_direct_device_lock);
2138
2139 if (rdma_capable == false) {
2140 struct ib_device *ibdev;
2141
2142 ibdev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_UNKNOWN);
2143 if (ibdev) {
2144 if (rdma_frwr_is_supported(&ibdev->attrs))
2145 rdma_capable = true;
2146 ib_device_put(ibdev);
2147 }
2148 }
2149
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09002150 return rdma_capable;
2151}
2152
Namjae Jeon0626e662021-03-16 13:07:11 +09002153static struct ksmbd_transport_ops ksmbd_smb_direct_transport_ops = {
2154 .prepare = smb_direct_prepare,
2155 .disconnect = smb_direct_disconnect,
2156 .writev = smb_direct_writev,
2157 .read = smb_direct_read,
2158 .rdma_read = smb_direct_rdma_read,
2159 .rdma_write = smb_direct_rdma_write,
2160};