blob: 1a5834a5d597dab650d448835cf1ffb06fe4e9fa [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Long Li03bee012017-11-07 01:54:56 -07002/*
3 * Copyright (C) 2017, Microsoft Corporation.
4 *
5 * Author(s): Long Li <longli@microsoft.com>
Long Li03bee012017-11-07 01:54:56 -07006 */
Long Lif1981862017-11-04 18:17:24 -07007#include <linux/module.h>
Long Lif64b78f2017-11-22 17:38:40 -07008#include <linux/highmem.h>
Long Li03bee012017-11-07 01:54:56 -07009#include "smbdirect.h"
Long Lif1981862017-11-04 18:17:24 -070010#include "cifs_debug.h"
Long Lib6903bc2018-05-30 12:48:00 -070011#include "cifsproto.h"
Paulo Alcantara35e2cc12018-06-15 10:22:44 -030012#include "smb2proto.h"
Long Lif1981862017-11-04 18:17:24 -070013
14static struct smbd_response *get_empty_queue_buffer(
15 struct smbd_connection *info);
16static struct smbd_response *get_receive_buffer(
17 struct smbd_connection *info);
18static void put_receive_buffer(
19 struct smbd_connection *info,
20 struct smbd_response *response);
21static int allocate_receive_buffers(struct smbd_connection *info, int num_buf);
22static void destroy_receive_buffers(struct smbd_connection *info);
23
24static void put_empty_packet(
25 struct smbd_connection *info, struct smbd_response *response);
26static void enqueue_reassembly(
27 struct smbd_connection *info,
28 struct smbd_response *response, int data_length);
29static struct smbd_response *_get_first_reassembly(
30 struct smbd_connection *info);
31
32static int smbd_post_recv(
33 struct smbd_connection *info,
34 struct smbd_response *response);
35
36static int smbd_post_send_empty(struct smbd_connection *info);
Long Lid649e1b2017-11-22 17:38:42 -070037static int smbd_post_send_data(
38 struct smbd_connection *info,
39 struct kvec *iov, int n_vec, int remaining_data_length);
40static int smbd_post_send_page(struct smbd_connection *info,
41 struct page *page, unsigned long offset,
42 size_t size, int remaining_data_length);
Long Li03bee012017-11-07 01:54:56 -070043
Long Lic7398582017-11-22 17:38:44 -070044static void destroy_mr_list(struct smbd_connection *info);
45static int allocate_mr_list(struct smbd_connection *info);
46
Long Li03bee012017-11-07 01:54:56 -070047/* SMBD version number */
48#define SMBD_V1 0x0100
49
50/* Port numbers for SMBD transport */
51#define SMB_PORT 445
52#define SMBD_PORT 5445
53
54/* Address lookup and resolve timeout in ms */
55#define RDMA_RESOLVE_TIMEOUT 5000
56
57/* SMBD negotiation timeout in seconds */
58#define SMBD_NEGOTIATE_TIMEOUT 120
59
60/* SMBD minimum receive size and fragmented sized defined in [MS-SMBD] */
61#define SMBD_MIN_RECEIVE_SIZE 128
62#define SMBD_MIN_FRAGMENTED_SIZE 131072
63
64/*
65 * Default maximum number of RDMA read/write outstanding on this connection
66 * This value is possibly decreased during QP creation on hardware limit
67 */
68#define SMBD_CM_RESPONDER_RESOURCES 32
69
70/* Maximum number of retries on data transfer operations */
71#define SMBD_CM_RETRY 6
72/* No need to retry on Receiver Not Ready since SMBD manages credits */
73#define SMBD_CM_RNR_RETRY 0
74
75/*
76 * User configurable initial values per SMBD transport connection
77 * as defined in [MS-SMBD] 3.1.1.1
78 * Those may change after a SMBD negotiation
79 */
80/* The local peer's maximum number of credits to grant to the peer */
81int smbd_receive_credit_max = 255;
82
83/* The remote peer's credit request of local peer */
84int smbd_send_credit_target = 255;
85
86/* The maximum single message size can be sent to remote peer */
87int smbd_max_send_size = 1364;
88
89/* The maximum fragmented upper-layer payload receive size supported */
90int smbd_max_fragmented_recv_size = 1024 * 1024;
91
92/* The maximum single-message size which can be received */
93int smbd_max_receive_size = 8192;
94
95/* The timeout to initiate send of a keepalive message on idle */
96int smbd_keep_alive_interval = 120;
97
98/*
99 * User configurable initial values for RDMA transport
100 * The actual values used may be lower and are limited to hardware capabilities
101 */
102/* Default maximum number of SGEs in a RDMA write/read */
103int smbd_max_frmr_depth = 2048;
104
105/* If payload is less than this byte, use RDMA send/recv not read/write */
106int rdma_readwrite_threshold = 4096;
Long Lif1981862017-11-04 18:17:24 -0700107
108/* Transport logging functions
109 * Logging are defined as classes. They can be OR'ed to define the actual
110 * logging level via module parameter smbd_logging_class
111 * e.g. cifs.smbd_logging_class=0xa0 will log all log_rdma_recv() and
112 * log_rdma_event()
113 */
114#define LOG_OUTGOING 0x1
115#define LOG_INCOMING 0x2
116#define LOG_READ 0x4
117#define LOG_WRITE 0x8
118#define LOG_RDMA_SEND 0x10
119#define LOG_RDMA_RECV 0x20
120#define LOG_KEEP_ALIVE 0x40
121#define LOG_RDMA_EVENT 0x80
122#define LOG_RDMA_MR 0x100
123static unsigned int smbd_logging_class;
124module_param(smbd_logging_class, uint, 0644);
125MODULE_PARM_DESC(smbd_logging_class,
126 "Logging class for SMBD transport 0x0 to 0x100");
127
128#define ERR 0x0
129#define INFO 0x1
130static unsigned int smbd_logging_level = ERR;
131module_param(smbd_logging_level, uint, 0644);
132MODULE_PARM_DESC(smbd_logging_level,
133 "Logging level for SMBD transport, 0 (default): error, 1: info");
134
135#define log_rdma(level, class, fmt, args...) \
136do { \
137 if (level <= smbd_logging_level || class & smbd_logging_class) \
138 cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
139} while (0)
140
141#define log_outgoing(level, fmt, args...) \
142 log_rdma(level, LOG_OUTGOING, fmt, ##args)
143#define log_incoming(level, fmt, args...) \
144 log_rdma(level, LOG_INCOMING, fmt, ##args)
145#define log_read(level, fmt, args...) log_rdma(level, LOG_READ, fmt, ##args)
146#define log_write(level, fmt, args...) log_rdma(level, LOG_WRITE, fmt, ##args)
147#define log_rdma_send(level, fmt, args...) \
148 log_rdma(level, LOG_RDMA_SEND, fmt, ##args)
149#define log_rdma_recv(level, fmt, args...) \
150 log_rdma(level, LOG_RDMA_RECV, fmt, ##args)
151#define log_keep_alive(level, fmt, args...) \
152 log_rdma(level, LOG_KEEP_ALIVE, fmt, ##args)
153#define log_rdma_event(level, fmt, args...) \
154 log_rdma(level, LOG_RDMA_EVENT, fmt, ##args)
155#define log_rdma_mr(level, fmt, args...) \
156 log_rdma(level, LOG_RDMA_MR, fmt, ##args)
157
Long Lif1981862017-11-04 18:17:24 -0700158static void smbd_disconnect_rdma_work(struct work_struct *work)
159{
160 struct smbd_connection *info =
161 container_of(work, struct smbd_connection, disconnect_work);
162
163 if (info->transport_status == SMBD_CONNECTED) {
164 info->transport_status = SMBD_DISCONNECTING;
165 rdma_disconnect(info->id);
166 }
167}
168
169static void smbd_disconnect_rdma_connection(struct smbd_connection *info)
170{
171 queue_work(info->workqueue, &info->disconnect_work);
172}
173
174/* Upcall from RDMA CM */
175static int smbd_conn_upcall(
176 struct rdma_cm_id *id, struct rdma_cm_event *event)
177{
178 struct smbd_connection *info = id->context;
179
180 log_rdma_event(INFO, "event=%d status=%d\n",
181 event->event, event->status);
182
183 switch (event->event) {
184 case RDMA_CM_EVENT_ADDR_RESOLVED:
185 case RDMA_CM_EVENT_ROUTE_RESOLVED:
186 info->ri_rc = 0;
187 complete(&info->ri_done);
188 break;
189
190 case RDMA_CM_EVENT_ADDR_ERROR:
191 info->ri_rc = -EHOSTUNREACH;
192 complete(&info->ri_done);
193 break;
194
195 case RDMA_CM_EVENT_ROUTE_ERROR:
196 info->ri_rc = -ENETUNREACH;
197 complete(&info->ri_done);
198 break;
199
200 case RDMA_CM_EVENT_ESTABLISHED:
201 log_rdma_event(INFO, "connected event=%d\n", event->event);
202 info->transport_status = SMBD_CONNECTED;
203 wake_up_interruptible(&info->conn_wait);
204 break;
205
206 case RDMA_CM_EVENT_CONNECT_ERROR:
207 case RDMA_CM_EVENT_UNREACHABLE:
208 case RDMA_CM_EVENT_REJECTED:
209 log_rdma_event(INFO, "connecting failed event=%d\n", event->event);
210 info->transport_status = SMBD_DISCONNECTED;
211 wake_up_interruptible(&info->conn_wait);
212 break;
213
214 case RDMA_CM_EVENT_DEVICE_REMOVAL:
215 case RDMA_CM_EVENT_DISCONNECTED:
216 /* This happenes when we fail the negotiation */
217 if (info->transport_status == SMBD_NEGOTIATE_FAILED) {
218 info->transport_status = SMBD_DISCONNECTED;
219 wake_up(&info->conn_wait);
220 break;
221 }
222
223 info->transport_status = SMBD_DISCONNECTED;
Long Lie8b3bfe2019-04-05 21:36:31 +0000224 wake_up_interruptible(&info->disconn_wait);
Long Li050b8c32019-04-04 11:35:42 -0500225 wake_up_interruptible(&info->wait_reassembly_queue);
226 wake_up_interruptible_all(&info->wait_send_queue);
Long Lif1981862017-11-04 18:17:24 -0700227 break;
228
229 default:
230 break;
231 }
232
233 return 0;
234}
235
236/* Upcall from RDMA QP */
237static void
238smbd_qp_async_error_upcall(struct ib_event *event, void *context)
239{
240 struct smbd_connection *info = context;
241
242 log_rdma_event(ERR, "%s on device %s info %p\n",
243 ib_event_msg(event->event), event->device->name, info);
244
245 switch (event->event) {
246 case IB_EVENT_CQ_ERR:
247 case IB_EVENT_QP_FATAL:
248 smbd_disconnect_rdma_connection(info);
249
250 default:
251 break;
252 }
253}
254
255static inline void *smbd_request_payload(struct smbd_request *request)
256{
257 return (void *)request->packet;
258}
259
260static inline void *smbd_response_payload(struct smbd_response *response)
261{
262 return (void *)response->packet;
263}
264
265/* Called when a RDMA send is done */
266static void send_done(struct ib_cq *cq, struct ib_wc *wc)
267{
268 int i;
269 struct smbd_request *request =
270 container_of(wc->wr_cqe, struct smbd_request, cqe);
271
272 log_rdma_send(INFO, "smbd_request %p completed wc->status=%d\n",
273 request, wc->status);
274
275 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
276 log_rdma_send(ERR, "wc->status=%d wc->opcode=%d\n",
277 wc->status, wc->opcode);
278 smbd_disconnect_rdma_connection(request->info);
279 }
280
281 for (i = 0; i < request->num_sge; i++)
282 ib_dma_unmap_single(request->info->id->device,
283 request->sge[i].addr,
284 request->sge[i].length,
285 DMA_TO_DEVICE);
286
Long Li072a14e2020-03-30 11:04:06 -0700287 if (atomic_dec_and_test(&request->info->send_pending))
288 wake_up(&request->info->wait_send_pending);
289
Long Li3ffbe782020-03-30 11:04:07 -0700290 wake_up(&request->info->wait_post_send);
Long Lif1981862017-11-04 18:17:24 -0700291
292 mempool_free(request, request->info->request_mempool);
293}
294
295static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
296{
297 log_rdma_event(INFO, "resp message min_version %u max_version %u "
298 "negotiated_version %u credits_requested %u "
299 "credits_granted %u status %u max_readwrite_size %u "
300 "preferred_send_size %u max_receive_size %u "
301 "max_fragmented_size %u\n",
302 resp->min_version, resp->max_version, resp->negotiated_version,
303 resp->credits_requested, resp->credits_granted, resp->status,
304 resp->max_readwrite_size, resp->preferred_send_size,
305 resp->max_receive_size, resp->max_fragmented_size);
306}
307
308/*
309 * Process a negotiation response message, according to [MS-SMBD]3.1.5.7
310 * response, packet_length: the negotiation response message
311 * return value: true if negotiation is a success, false if failed
312 */
313static bool process_negotiation_response(
314 struct smbd_response *response, int packet_length)
315{
316 struct smbd_connection *info = response->info;
317 struct smbd_negotiate_resp *packet = smbd_response_payload(response);
318
319 if (packet_length < sizeof(struct smbd_negotiate_resp)) {
320 log_rdma_event(ERR,
321 "error: packet_length=%d\n", packet_length);
322 return false;
323 }
324
325 if (le16_to_cpu(packet->negotiated_version) != SMBD_V1) {
326 log_rdma_event(ERR, "error: negotiated_version=%x\n",
327 le16_to_cpu(packet->negotiated_version));
328 return false;
329 }
330 info->protocol = le16_to_cpu(packet->negotiated_version);
331
332 if (packet->credits_requested == 0) {
333 log_rdma_event(ERR, "error: credits_requested==0\n");
334 return false;
335 }
336 info->receive_credit_target = le16_to_cpu(packet->credits_requested);
337
338 if (packet->credits_granted == 0) {
339 log_rdma_event(ERR, "error: credits_granted==0\n");
340 return false;
341 }
342 atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
343
344 atomic_set(&info->receive_credits, 0);
345
346 if (le32_to_cpu(packet->preferred_send_size) > info->max_receive_size) {
347 log_rdma_event(ERR, "error: preferred_send_size=%d\n",
348 le32_to_cpu(packet->preferred_send_size));
349 return false;
350 }
351 info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
352
353 if (le32_to_cpu(packet->max_receive_size) < SMBD_MIN_RECEIVE_SIZE) {
354 log_rdma_event(ERR, "error: max_receive_size=%d\n",
355 le32_to_cpu(packet->max_receive_size));
356 return false;
357 }
358 info->max_send_size = min_t(int, info->max_send_size,
359 le32_to_cpu(packet->max_receive_size));
360
361 if (le32_to_cpu(packet->max_fragmented_size) <
362 SMBD_MIN_FRAGMENTED_SIZE) {
363 log_rdma_event(ERR, "error: max_fragmented_size=%d\n",
364 le32_to_cpu(packet->max_fragmented_size));
365 return false;
366 }
367 info->max_fragmented_send_size =
368 le32_to_cpu(packet->max_fragmented_size);
Long Lic7398582017-11-22 17:38:44 -0700369 info->rdma_readwrite_threshold =
370 rdma_readwrite_threshold > info->max_fragmented_send_size ?
371 info->max_fragmented_send_size :
372 rdma_readwrite_threshold;
373
374
375 info->max_readwrite_size = min_t(u32,
376 le32_to_cpu(packet->max_readwrite_size),
377 info->max_frmr_depth * PAGE_SIZE);
378 info->max_frmr_depth = info->max_readwrite_size / PAGE_SIZE;
Long Lif1981862017-11-04 18:17:24 -0700379
380 return true;
381}
382
Long Lif1981862017-11-04 18:17:24 -0700383static void smbd_post_send_credits(struct work_struct *work)
384{
385 int ret = 0;
386 int use_receive_queue = 1;
387 int rc;
388 struct smbd_response *response;
389 struct smbd_connection *info =
390 container_of(work, struct smbd_connection,
391 post_send_credits_work);
392
393 if (info->transport_status != SMBD_CONNECTED) {
394 wake_up(&info->wait_receive_queues);
395 return;
396 }
397
398 if (info->receive_credit_target >
399 atomic_read(&info->receive_credits)) {
400 while (true) {
401 if (use_receive_queue)
402 response = get_receive_buffer(info);
403 else
404 response = get_empty_queue_buffer(info);
405 if (!response) {
406 /* now switch to emtpy packet queue */
407 if (use_receive_queue) {
408 use_receive_queue = 0;
409 continue;
410 } else
411 break;
412 }
413
414 response->type = SMBD_TRANSFER_DATA;
415 response->first_segment = false;
416 rc = smbd_post_recv(info, response);
417 if (rc) {
418 log_rdma_recv(ERR,
419 "post_recv failed rc=%d\n", rc);
420 put_receive_buffer(info, response);
421 break;
422 }
423
424 ret++;
425 }
426 }
427
428 spin_lock(&info->lock_new_credits_offered);
429 info->new_credits_offered += ret;
430 spin_unlock(&info->lock_new_credits_offered);
431
Long Li044b5412020-03-31 20:59:23 -0700432 /* Promptly send an immediate packet as defined in [MS-SMBD] 3.1.1.1 */
433 info->send_immediate = true;
434 if (atomic_read(&info->receive_credits) <
435 info->receive_credit_target - 1) {
436 if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
437 info->send_immediate) {
438 log_keep_alive(INFO, "send an empty message\n");
439 smbd_post_send_empty(info);
440 }
441 }
Long Lif1981862017-11-04 18:17:24 -0700442}
443
Long Lif1981862017-11-04 18:17:24 -0700444/* Called from softirq, when recv is done */
445static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
446{
447 struct smbd_data_transfer *data_transfer;
448 struct smbd_response *response =
449 container_of(wc->wr_cqe, struct smbd_response, cqe);
450 struct smbd_connection *info = response->info;
451 int data_length = 0;
452
453 log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d "
454 "byte_len=%d pkey_index=%x\n",
455 response, response->type, wc->status, wc->opcode,
456 wc->byte_len, wc->pkey_index);
457
458 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
459 log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
460 wc->status, wc->opcode);
461 smbd_disconnect_rdma_connection(info);
462 goto error;
463 }
464
465 ib_dma_sync_single_for_cpu(
466 wc->qp->device,
467 response->sge.addr,
468 response->sge.length,
469 DMA_FROM_DEVICE);
470
471 switch (response->type) {
472 /* SMBD negotiation response */
473 case SMBD_NEGOTIATE_RESP:
474 dump_smbd_negotiate_resp(smbd_response_payload(response));
475 info->full_packet_received = true;
476 info->negotiate_done =
477 process_negotiation_response(response, wc->byte_len);
478 complete(&info->negotiate_completion);
479 break;
480
481 /* SMBD data transfer packet */
482 case SMBD_TRANSFER_DATA:
483 data_transfer = smbd_response_payload(response);
484 data_length = le32_to_cpu(data_transfer->data_length);
485
486 /*
487 * If this is a packet with data playload place the data in
488 * reassembly queue and wake up the reading thread
489 */
490 if (data_length) {
491 if (info->full_packet_received)
492 response->first_segment = true;
493
494 if (le32_to_cpu(data_transfer->remaining_data_length))
495 info->full_packet_received = false;
496 else
497 info->full_packet_received = true;
498
499 enqueue_reassembly(
500 info,
501 response,
502 data_length);
503 } else
504 put_empty_packet(info, response);
505
506 if (data_length)
507 wake_up_interruptible(&info->wait_reassembly_queue);
508
509 atomic_dec(&info->receive_credits);
510 info->receive_credit_target =
511 le16_to_cpu(data_transfer->credits_requested);
Long Li4ebb8792020-03-26 22:33:01 -0700512 if (le16_to_cpu(data_transfer->credits_granted)) {
513 atomic_add(le16_to_cpu(data_transfer->credits_granted),
514 &info->send_credits);
515 /*
516 * We have new send credits granted from remote peer
517 * If any sender is waiting for credits, unblock it
518 */
519 wake_up_interruptible(&info->wait_send_queue);
520 }
Long Lif1981862017-11-04 18:17:24 -0700521
522 log_incoming(INFO, "data flags %d data_offset %d "
523 "data_length %d remaining_data_length %d\n",
524 le16_to_cpu(data_transfer->flags),
525 le32_to_cpu(data_transfer->data_offset),
526 le32_to_cpu(data_transfer->data_length),
527 le32_to_cpu(data_transfer->remaining_data_length));
528
529 /* Send a KEEP_ALIVE response right away if requested */
530 info->keep_alive_requested = KEEP_ALIVE_NONE;
531 if (le16_to_cpu(data_transfer->flags) &
532 SMB_DIRECT_RESPONSE_REQUESTED) {
533 info->keep_alive_requested = KEEP_ALIVE_PENDING;
534 }
535
Long Lif1981862017-11-04 18:17:24 -0700536 return;
537
538 default:
539 log_rdma_recv(ERR,
540 "unexpected response type=%d\n", response->type);
541 }
542
543error:
544 put_receive_buffer(info, response);
545}
546
547static struct rdma_cm_id *smbd_create_id(
548 struct smbd_connection *info,
549 struct sockaddr *dstaddr, int port)
550{
551 struct rdma_cm_id *id;
552 int rc;
553 __be16 *sport;
554
555 id = rdma_create_id(&init_net, smbd_conn_upcall, info,
556 RDMA_PS_TCP, IB_QPT_RC);
557 if (IS_ERR(id)) {
558 rc = PTR_ERR(id);
559 log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
560 return id;
561 }
562
563 if (dstaddr->sa_family == AF_INET6)
564 sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
565 else
566 sport = &((struct sockaddr_in *)dstaddr)->sin_port;
567
568 *sport = htons(port);
569
570 init_completion(&info->ri_done);
571 info->ri_rc = -ETIMEDOUT;
572
573 rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
574 RDMA_RESOLVE_TIMEOUT);
575 if (rc) {
576 log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
577 goto out;
578 }
579 wait_for_completion_interruptible_timeout(
580 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
581 rc = info->ri_rc;
582 if (rc) {
583 log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
584 goto out;
585 }
586
587 info->ri_rc = -ETIMEDOUT;
588 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
589 if (rc) {
590 log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
591 goto out;
592 }
593 wait_for_completion_interruptible_timeout(
594 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
595 rc = info->ri_rc;
596 if (rc) {
597 log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
598 goto out;
599 }
600
601 return id;
602
603out:
604 rdma_destroy_id(id);
605 return ERR_PTR(rc);
606}
607
608/*
609 * Test if FRWR (Fast Registration Work Requests) is supported on the device
610 * This implementation requries FRWR on RDMA read/write
611 * return value: true if it is supported
612 */
613static bool frwr_is_supported(struct ib_device_attr *attrs)
614{
615 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
616 return false;
617 if (attrs->max_fast_reg_page_list_len == 0)
618 return false;
619 return true;
620}
621
622static int smbd_ia_open(
623 struct smbd_connection *info,
624 struct sockaddr *dstaddr, int port)
625{
626 int rc;
627
628 info->id = smbd_create_id(info, dstaddr, port);
629 if (IS_ERR(info->id)) {
630 rc = PTR_ERR(info->id);
631 goto out1;
632 }
633
634 if (!frwr_is_supported(&info->id->device->attrs)) {
635 log_rdma_event(ERR,
636 "Fast Registration Work Requests "
637 "(FRWR) is not supported\n");
638 log_rdma_event(ERR,
639 "Device capability flags = %llx "
640 "max_fast_reg_page_list_len = %u\n",
641 info->id->device->attrs.device_cap_flags,
642 info->id->device->attrs.max_fast_reg_page_list_len);
643 rc = -EPROTONOSUPPORT;
644 goto out2;
645 }
Long Lic7398582017-11-22 17:38:44 -0700646 info->max_frmr_depth = min_t(int,
647 smbd_max_frmr_depth,
648 info->id->device->attrs.max_fast_reg_page_list_len);
649 info->mr_type = IB_MR_TYPE_MEM_REG;
650 if (info->id->device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
651 info->mr_type = IB_MR_TYPE_SG_GAPS;
Long Lif1981862017-11-04 18:17:24 -0700652
653 info->pd = ib_alloc_pd(info->id->device, 0);
654 if (IS_ERR(info->pd)) {
655 rc = PTR_ERR(info->pd);
656 log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
657 goto out2;
658 }
659
660 return 0;
661
662out2:
663 rdma_destroy_id(info->id);
664 info->id = NULL;
665
666out1:
667 return rc;
668}
669
670/*
671 * Send a negotiation request message to the peer
672 * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
673 * After negotiation, the transport is connected and ready for
674 * carrying upper layer SMB payload
675 */
676static int smbd_post_send_negotiate_req(struct smbd_connection *info)
677{
Bart Van Assche73930592018-07-18 09:25:25 -0700678 struct ib_send_wr send_wr;
Long Lif1981862017-11-04 18:17:24 -0700679 int rc = -ENOMEM;
680 struct smbd_request *request;
681 struct smbd_negotiate_req *packet;
682
683 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
684 if (!request)
685 return rc;
686
687 request->info = info;
688
689 packet = smbd_request_payload(request);
690 packet->min_version = cpu_to_le16(SMBD_V1);
691 packet->max_version = cpu_to_le16(SMBD_V1);
692 packet->reserved = 0;
693 packet->credits_requested = cpu_to_le16(info->send_credit_target);
694 packet->preferred_send_size = cpu_to_le32(info->max_send_size);
695 packet->max_receive_size = cpu_to_le32(info->max_receive_size);
696 packet->max_fragmented_size =
697 cpu_to_le32(info->max_fragmented_recv_size);
698
699 request->num_sge = 1;
700 request->sge[0].addr = ib_dma_map_single(
701 info->id->device, (void *)packet,
702 sizeof(*packet), DMA_TO_DEVICE);
703 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
704 rc = -EIO;
705 goto dma_mapping_failed;
706 }
707
708 request->sge[0].length = sizeof(*packet);
709 request->sge[0].lkey = info->pd->local_dma_lkey;
710
711 ib_dma_sync_single_for_device(
712 info->id->device, request->sge[0].addr,
713 request->sge[0].length, DMA_TO_DEVICE);
714
715 request->cqe.done = send_done;
716
717 send_wr.next = NULL;
718 send_wr.wr_cqe = &request->cqe;
719 send_wr.sg_list = request->sge;
720 send_wr.num_sge = request->num_sge;
721 send_wr.opcode = IB_WR_SEND;
722 send_wr.send_flags = IB_SEND_SIGNALED;
723
724 log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
725 request->sge[0].addr,
726 request->sge[0].length, request->sge[0].lkey);
727
Long Lif1981862017-11-04 18:17:24 -0700728 atomic_inc(&info->send_pending);
Bart Van Assche73930592018-07-18 09:25:25 -0700729 rc = ib_post_send(info->id->qp, &send_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -0700730 if (!rc)
731 return 0;
732
733 /* if we reach here, post send failed */
734 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
735 atomic_dec(&info->send_pending);
736 ib_dma_unmap_single(info->id->device, request->sge[0].addr,
737 request->sge[0].length, DMA_TO_DEVICE);
738
Long Li21a4e142018-03-30 15:16:36 -0700739 smbd_disconnect_rdma_connection(info);
740
Long Lif1981862017-11-04 18:17:24 -0700741dma_mapping_failed:
742 mempool_free(request, info->request_mempool);
743 return rc;
744}
745
746/*
747 * Extend the credits to remote peer
748 * This implements [MS-SMBD] 3.1.5.9
749 * The idea is that we should extend credits to remote peer as quickly as
750 * it's allowed, to maintain data flow. We allocate as much receive
751 * buffer as possible, and extend the receive credits to remote peer
752 * return value: the new credtis being granted.
753 */
754static int manage_credits_prior_sending(struct smbd_connection *info)
755{
756 int new_credits;
757
758 spin_lock(&info->lock_new_credits_offered);
759 new_credits = info->new_credits_offered;
760 info->new_credits_offered = 0;
761 spin_unlock(&info->lock_new_credits_offered);
762
763 return new_credits;
764}
765
766/*
767 * Check if we need to send a KEEP_ALIVE message
768 * The idle connection timer triggers a KEEP_ALIVE message when expires
769 * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
770 * back a response.
771 * return value:
772 * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
773 * 0: otherwise
774 */
775static int manage_keep_alive_before_sending(struct smbd_connection *info)
776{
777 if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
778 info->keep_alive_requested = KEEP_ALIVE_SENT;
779 return 1;
780 }
781 return 0;
782}
783
Long Lif1981862017-11-04 18:17:24 -0700784/* Post the send request */
785static int smbd_post_send(struct smbd_connection *info,
Long Li072a14e2020-03-30 11:04:06 -0700786 struct smbd_request *request)
Long Lif1981862017-11-04 18:17:24 -0700787{
Bart Van Assche73930592018-07-18 09:25:25 -0700788 struct ib_send_wr send_wr;
Long Lif1981862017-11-04 18:17:24 -0700789 int rc, i;
790
791 for (i = 0; i < request->num_sge; i++) {
792 log_rdma_send(INFO,
Colin Ian Kingac65cb62018-02-09 12:14:15 +0000793 "rdma_request sge[%d] addr=%llu length=%u\n",
Long Liff30b892018-04-17 12:17:10 -0700794 i, request->sge[i].addr, request->sge[i].length);
Long Lif1981862017-11-04 18:17:24 -0700795 ib_dma_sync_single_for_device(
796 info->id->device,
797 request->sge[i].addr,
798 request->sge[i].length,
799 DMA_TO_DEVICE);
800 }
801
802 request->cqe.done = send_done;
803
804 send_wr.next = NULL;
805 send_wr.wr_cqe = &request->cqe;
806 send_wr.sg_list = request->sge;
807 send_wr.num_sge = request->num_sge;
808 send_wr.opcode = IB_WR_SEND;
809 send_wr.send_flags = IB_SEND_SIGNALED;
810
Bart Van Assche73930592018-07-18 09:25:25 -0700811 rc = ib_post_send(info->id->qp, &send_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -0700812 if (rc) {
813 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
Long Li21a4e142018-03-30 15:16:36 -0700814 smbd_disconnect_rdma_connection(info);
Long Li62fdf672019-04-05 21:36:33 +0000815 rc = -EAGAIN;
Long Lif1981862017-11-04 18:17:24 -0700816 } else
817 /* Reset timer for idle connection after packet is sent */
818 mod_delayed_work(info->workqueue, &info->idle_timer_work,
819 info->keep_alive_interval*HZ);
820
821 return rc;
822}
823
824static int smbd_post_send_sgl(struct smbd_connection *info,
825 struct scatterlist *sgl, int data_length, int remaining_data_length)
826{
827 int num_sgs;
828 int i, rc;
Long Lif1b7b862020-04-02 13:56:48 -0700829 int header_length;
Long Lif1981862017-11-04 18:17:24 -0700830 struct smbd_request *request;
Long Lif1b7b862020-04-02 13:56:48 -0700831 struct smbd_data_transfer *packet;
832 int new_credits;
Long Lif1981862017-11-04 18:17:24 -0700833 struct scatterlist *sg;
834
Long Lif1b7b862020-04-02 13:56:48 -0700835wait_credit:
836 /* Wait for send credits. A SMBD packet needs one credit */
837 rc = wait_event_interruptible(info->wait_send_queue,
838 atomic_read(&info->send_credits) > 0 ||
839 info->transport_status != SMBD_CONNECTED);
Long Lif1981862017-11-04 18:17:24 -0700840 if (rc)
Long Lif1b7b862020-04-02 13:56:48 -0700841 goto err_wait_credit;
Long Lif1981862017-11-04 18:17:24 -0700842
Long Lif1b7b862020-04-02 13:56:48 -0700843 if (info->transport_status != SMBD_CONNECTED) {
844 log_outgoing(ERR, "disconnected not sending on wait_credit\n");
845 rc = -EAGAIN;
846 goto err_wait_credit;
847 }
848 if (unlikely(atomic_dec_return(&info->send_credits) < 0)) {
849 atomic_inc(&info->send_credits);
850 goto wait_credit;
851 }
852
853wait_send_queue:
854 wait_event(info->wait_post_send,
855 atomic_read(&info->send_pending) < info->send_credit_target ||
856 info->transport_status != SMBD_CONNECTED);
857
858 if (info->transport_status != SMBD_CONNECTED) {
859 log_outgoing(ERR, "disconnected not sending on wait_send_queue\n");
860 rc = -EAGAIN;
861 goto err_wait_send_queue;
862 }
863
864 if (unlikely(atomic_inc_return(&info->send_pending) >
865 info->send_credit_target)) {
866 atomic_dec(&info->send_pending);
867 goto wait_send_queue;
868 }
869
870 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
871 if (!request) {
872 rc = -ENOMEM;
873 goto err_alloc;
874 }
875
876 request->info = info;
877
878 /* Fill in the packet header */
879 packet = smbd_request_payload(request);
880 packet->credits_requested = cpu_to_le16(info->send_credit_target);
881
882 new_credits = manage_credits_prior_sending(info);
883 atomic_add(new_credits, &info->receive_credits);
884 packet->credits_granted = cpu_to_le16(new_credits);
885
886 info->send_immediate = false;
887
888 packet->flags = 0;
889 if (manage_keep_alive_before_sending(info))
890 packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
891
892 packet->reserved = 0;
893 if (!data_length)
894 packet->data_offset = 0;
895 else
896 packet->data_offset = cpu_to_le32(24);
897 packet->data_length = cpu_to_le32(data_length);
898 packet->remaining_data_length = cpu_to_le32(remaining_data_length);
899 packet->padding = 0;
900
901 log_outgoing(INFO, "credits_requested=%d credits_granted=%d "
902 "data_offset=%d data_length=%d remaining_data_length=%d\n",
903 le16_to_cpu(packet->credits_requested),
904 le16_to_cpu(packet->credits_granted),
905 le32_to_cpu(packet->data_offset),
906 le32_to_cpu(packet->data_length),
907 le32_to_cpu(packet->remaining_data_length));
908
909 /* Map the packet to DMA */
910 header_length = sizeof(struct smbd_data_transfer);
911 /* If this is a packet without payload, don't send padding */
912 if (!data_length)
913 header_length = offsetof(struct smbd_data_transfer, padding);
914
915 request->num_sge = 1;
916 request->sge[0].addr = ib_dma_map_single(info->id->device,
917 (void *)packet,
918 header_length,
919 DMA_TO_DEVICE);
920 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
921 rc = -EIO;
922 request->sge[0].addr = 0;
923 goto err_dma;
924 }
925
926 request->sge[0].length = header_length;
927 request->sge[0].lkey = info->pd->local_dma_lkey;
928
929 /* Fill in the packet data payload */
Long Lif1981862017-11-04 18:17:24 -0700930 num_sgs = sgl ? sg_nents(sgl) : 0;
931 for_each_sg(sgl, sg, num_sgs, i) {
932 request->sge[i+1].addr =
933 ib_dma_map_page(info->id->device, sg_page(sg),
Long Li7f46d232019-05-13 21:01:29 -0700934 sg->offset, sg->length, DMA_TO_DEVICE);
Long Lif1981862017-11-04 18:17:24 -0700935 if (ib_dma_mapping_error(
936 info->id->device, request->sge[i+1].addr)) {
937 rc = -EIO;
938 request->sge[i+1].addr = 0;
Long Lif1b7b862020-04-02 13:56:48 -0700939 goto err_dma;
Long Lif1981862017-11-04 18:17:24 -0700940 }
941 request->sge[i+1].length = sg->length;
942 request->sge[i+1].lkey = info->pd->local_dma_lkey;
943 request->num_sge++;
944 }
945
Long Li072a14e2020-03-30 11:04:06 -0700946 rc = smbd_post_send(info, request);
Long Lif1981862017-11-04 18:17:24 -0700947 if (!rc)
948 return 0;
949
Long Lif1b7b862020-04-02 13:56:48 -0700950err_dma:
951 for (i = 0; i < request->num_sge; i++)
Long Lif1981862017-11-04 18:17:24 -0700952 if (request->sge[i].addr)
953 ib_dma_unmap_single(info->id->device,
954 request->sge[i].addr,
955 request->sge[i].length,
956 DMA_TO_DEVICE);
Long Lif1b7b862020-04-02 13:56:48 -0700957 mempool_free(request, info->request_mempool);
958
959 /* roll back receive credits and credits to be offered */
960 spin_lock(&info->lock_new_credits_offered);
961 info->new_credits_offered += new_credits;
962 spin_unlock(&info->lock_new_credits_offered);
963 atomic_sub(new_credits, &info->receive_credits);
964
965err_alloc:
966 if (atomic_dec_and_test(&info->send_pending))
967 wake_up(&info->wait_send_pending);
968
969err_wait_send_queue:
970 /* roll back send credits and pending */
971 atomic_inc(&info->send_credits);
972
973err_wait_credit:
Long Lif1981862017-11-04 18:17:24 -0700974 return rc;
975}
976
977/*
Long Lid649e1b2017-11-22 17:38:42 -0700978 * Send a page
979 * page: the page to send
980 * offset: offset in the page to send
981 * size: length in the page to send
982 * remaining_data_length: remaining data to send in this payload
983 */
984static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
985 unsigned long offset, size_t size, int remaining_data_length)
986{
987 struct scatterlist sgl;
988
989 sg_init_table(&sgl, 1);
990 sg_set_page(&sgl, page, size, offset);
991
992 return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
993}
994
995/*
Long Lif1981862017-11-04 18:17:24 -0700996 * Send an empty message
997 * Empty message is used to extend credits to peer to for keep live
998 * while there is no upper layer payload to send at the time
999 */
1000static int smbd_post_send_empty(struct smbd_connection *info)
1001{
1002 info->count_send_empty++;
1003 return smbd_post_send_sgl(info, NULL, 0, 0);
1004}
1005
1006/*
Long Lid649e1b2017-11-22 17:38:42 -07001007 * Send a data buffer
1008 * iov: the iov array describing the data buffers
1009 * n_vec: number of iov array
1010 * remaining_data_length: remaining data to send following this packet
1011 * in segmented SMBD packet
1012 */
1013static int smbd_post_send_data(
1014 struct smbd_connection *info, struct kvec *iov, int n_vec,
1015 int remaining_data_length)
1016{
1017 int i;
1018 u32 data_length = 0;
1019 struct scatterlist sgl[SMBDIRECT_MAX_SGE];
1020
1021 if (n_vec > SMBDIRECT_MAX_SGE) {
1022 cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
Long Li37941ea2019-10-16 13:51:52 -07001023 return -EINVAL;
Long Lid649e1b2017-11-22 17:38:42 -07001024 }
1025
1026 sg_init_table(sgl, n_vec);
1027 for (i = 0; i < n_vec; i++) {
1028 data_length += iov[i].iov_len;
1029 sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
1030 }
1031
1032 return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
1033}
1034
1035/*
Long Lif1981862017-11-04 18:17:24 -07001036 * Post a receive request to the transport
1037 * The remote peer can only send data when a receive request is posted
1038 * The interaction is controlled by send/receive credit system
1039 */
1040static int smbd_post_recv(
1041 struct smbd_connection *info, struct smbd_response *response)
1042{
Bart Van Assche73930592018-07-18 09:25:25 -07001043 struct ib_recv_wr recv_wr;
Long Lif1981862017-11-04 18:17:24 -07001044 int rc = -EIO;
1045
1046 response->sge.addr = ib_dma_map_single(
1047 info->id->device, response->packet,
1048 info->max_receive_size, DMA_FROM_DEVICE);
1049 if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1050 return rc;
1051
1052 response->sge.length = info->max_receive_size;
1053 response->sge.lkey = info->pd->local_dma_lkey;
1054
1055 response->cqe.done = recv_done;
1056
1057 recv_wr.wr_cqe = &response->cqe;
1058 recv_wr.next = NULL;
1059 recv_wr.sg_list = &response->sge;
1060 recv_wr.num_sge = 1;
1061
Bart Van Assche73930592018-07-18 09:25:25 -07001062 rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -07001063 if (rc) {
1064 ib_dma_unmap_single(info->id->device, response->sge.addr,
1065 response->sge.length, DMA_FROM_DEVICE);
Long Li21a4e142018-03-30 15:16:36 -07001066 smbd_disconnect_rdma_connection(info);
Long Lif1981862017-11-04 18:17:24 -07001067 log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1068 }
1069
1070 return rc;
1071}
1072
1073/* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1074static int smbd_negotiate(struct smbd_connection *info)
1075{
1076 int rc;
1077 struct smbd_response *response = get_receive_buffer(info);
1078
1079 response->type = SMBD_NEGOTIATE_RESP;
1080 rc = smbd_post_recv(info, response);
1081 log_rdma_event(INFO,
1082 "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x "
1083 "iov.lkey=%x\n",
1084 rc, response->sge.addr,
1085 response->sge.length, response->sge.lkey);
1086 if (rc)
1087 return rc;
1088
1089 init_completion(&info->negotiate_completion);
1090 info->negotiate_done = false;
1091 rc = smbd_post_send_negotiate_req(info);
1092 if (rc)
1093 return rc;
1094
1095 rc = wait_for_completion_interruptible_timeout(
1096 &info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1097 log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1098
1099 if (info->negotiate_done)
1100 return 0;
1101
1102 if (rc == 0)
1103 rc = -ETIMEDOUT;
1104 else if (rc == -ERESTARTSYS)
1105 rc = -EINTR;
1106 else
1107 rc = -ENOTCONN;
1108
1109 return rc;
1110}
1111
1112static void put_empty_packet(
1113 struct smbd_connection *info, struct smbd_response *response)
1114{
1115 spin_lock(&info->empty_packet_queue_lock);
1116 list_add_tail(&response->list, &info->empty_packet_queue);
1117 info->count_empty_packet_queue++;
1118 spin_unlock(&info->empty_packet_queue_lock);
1119
1120 queue_work(info->workqueue, &info->post_send_credits_work);
1121}
1122
1123/*
1124 * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1125 * This is a queue for reassembling upper layer payload and present to upper
1126 * layer. All the inncoming payload go to the reassembly queue, regardless of
1127 * if reassembly is required. The uuper layer code reads from the queue for all
1128 * incoming payloads.
1129 * Put a received packet to the reassembly queue
1130 * response: the packet received
1131 * data_length: the size of payload in this packet
1132 */
1133static void enqueue_reassembly(
1134 struct smbd_connection *info,
1135 struct smbd_response *response,
1136 int data_length)
1137{
1138 spin_lock(&info->reassembly_queue_lock);
1139 list_add_tail(&response->list, &info->reassembly_queue);
1140 info->reassembly_queue_length++;
1141 /*
1142 * Make sure reassembly_data_length is updated after list and
1143 * reassembly_queue_length are updated. On the dequeue side
1144 * reassembly_data_length is checked without a lock to determine
1145 * if reassembly_queue_length and list is up to date
1146 */
1147 virt_wmb();
1148 info->reassembly_data_length += data_length;
1149 spin_unlock(&info->reassembly_queue_lock);
1150 info->count_reassembly_queue++;
1151 info->count_enqueue_reassembly_queue++;
1152}
1153
1154/*
1155 * Get the first entry at the front of reassembly queue
1156 * Caller is responsible for locking
1157 * return value: the first entry if any, NULL if queue is empty
1158 */
1159static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1160{
1161 struct smbd_response *ret = NULL;
1162
1163 if (!list_empty(&info->reassembly_queue)) {
1164 ret = list_first_entry(
1165 &info->reassembly_queue,
1166 struct smbd_response, list);
1167 }
1168 return ret;
1169}
1170
1171static struct smbd_response *get_empty_queue_buffer(
1172 struct smbd_connection *info)
1173{
1174 struct smbd_response *ret = NULL;
1175 unsigned long flags;
1176
1177 spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1178 if (!list_empty(&info->empty_packet_queue)) {
1179 ret = list_first_entry(
1180 &info->empty_packet_queue,
1181 struct smbd_response, list);
1182 list_del(&ret->list);
1183 info->count_empty_packet_queue--;
1184 }
1185 spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1186
1187 return ret;
1188}
1189
1190/*
1191 * Get a receive buffer
1192 * For each remote send, we need to post a receive. The receive buffers are
1193 * pre-allocated in advance.
1194 * return value: the receive buffer, NULL if none is available
1195 */
1196static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1197{
1198 struct smbd_response *ret = NULL;
1199 unsigned long flags;
1200
1201 spin_lock_irqsave(&info->receive_queue_lock, flags);
1202 if (!list_empty(&info->receive_queue)) {
1203 ret = list_first_entry(
1204 &info->receive_queue,
1205 struct smbd_response, list);
1206 list_del(&ret->list);
1207 info->count_receive_queue--;
1208 info->count_get_receive_buffer++;
1209 }
1210 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1211
1212 return ret;
1213}
1214
1215/*
1216 * Return a receive buffer
1217 * Upon returning of a receive buffer, we can post new receive and extend
1218 * more receive credits to remote peer. This is done immediately after a
1219 * receive buffer is returned.
1220 */
1221static void put_receive_buffer(
1222 struct smbd_connection *info, struct smbd_response *response)
1223{
1224 unsigned long flags;
1225
1226 ib_dma_unmap_single(info->id->device, response->sge.addr,
1227 response->sge.length, DMA_FROM_DEVICE);
1228
1229 spin_lock_irqsave(&info->receive_queue_lock, flags);
1230 list_add_tail(&response->list, &info->receive_queue);
1231 info->count_receive_queue++;
1232 info->count_put_receive_buffer++;
1233 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1234
1235 queue_work(info->workqueue, &info->post_send_credits_work);
1236}
1237
1238/* Preallocate all receive buffer on transport establishment */
1239static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1240{
1241 int i;
1242 struct smbd_response *response;
1243
1244 INIT_LIST_HEAD(&info->reassembly_queue);
1245 spin_lock_init(&info->reassembly_queue_lock);
1246 info->reassembly_data_length = 0;
1247 info->reassembly_queue_length = 0;
1248
1249 INIT_LIST_HEAD(&info->receive_queue);
1250 spin_lock_init(&info->receive_queue_lock);
1251 info->count_receive_queue = 0;
1252
1253 INIT_LIST_HEAD(&info->empty_packet_queue);
1254 spin_lock_init(&info->empty_packet_queue_lock);
1255 info->count_empty_packet_queue = 0;
1256
1257 init_waitqueue_head(&info->wait_receive_queues);
1258
1259 for (i = 0; i < num_buf; i++) {
1260 response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1261 if (!response)
1262 goto allocate_failed;
1263
1264 response->info = info;
1265 list_add_tail(&response->list, &info->receive_queue);
1266 info->count_receive_queue++;
1267 }
1268
1269 return 0;
1270
1271allocate_failed:
1272 while (!list_empty(&info->receive_queue)) {
1273 response = list_first_entry(
1274 &info->receive_queue,
1275 struct smbd_response, list);
1276 list_del(&response->list);
1277 info->count_receive_queue--;
1278
1279 mempool_free(response, info->response_mempool);
1280 }
1281 return -ENOMEM;
1282}
1283
1284static void destroy_receive_buffers(struct smbd_connection *info)
1285{
1286 struct smbd_response *response;
1287
1288 while ((response = get_receive_buffer(info)))
1289 mempool_free(response, info->response_mempool);
1290
1291 while ((response = get_empty_queue_buffer(info)))
1292 mempool_free(response, info->response_mempool);
1293}
1294
Long Lif1981862017-11-04 18:17:24 -07001295/* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1296static void idle_connection_timer(struct work_struct *work)
1297{
1298 struct smbd_connection *info = container_of(
1299 work, struct smbd_connection,
1300 idle_timer_work.work);
1301
1302 if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1303 log_keep_alive(ERR,
1304 "error status info->keep_alive_requested=%d\n",
1305 info->keep_alive_requested);
1306 smbd_disconnect_rdma_connection(info);
1307 return;
1308 }
1309
1310 log_keep_alive(INFO, "about to send an empty idle message\n");
1311 smbd_post_send_empty(info);
1312
1313 /* Setup the next idle timeout work */
1314 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1315 info->keep_alive_interval*HZ);
1316}
1317
Long Li050b8c32019-04-04 11:35:42 -05001318/*
1319 * Destroy the transport and related RDMA and memory resources
1320 * Need to go through all the pending counters and make sure on one is using
1321 * the transport while it is destroyed
1322 */
1323void smbd_destroy(struct TCP_Server_Info *server)
Long Li8ef130f2017-11-22 17:38:37 -07001324{
Long Li050b8c32019-04-04 11:35:42 -05001325 struct smbd_connection *info = server->smbd_conn;
1326 struct smbd_response *response;
1327 unsigned long flags;
1328
1329 if (!info) {
1330 log_rdma_event(INFO, "rdma session already destroyed\n");
1331 return;
1332 }
1333
Long Li8ef130f2017-11-22 17:38:37 -07001334 log_rdma_event(INFO, "destroying rdma session\n");
Long Li050b8c32019-04-04 11:35:42 -05001335 if (info->transport_status != SMBD_DISCONNECTED) {
1336 rdma_disconnect(server->smbd_conn->id);
1337 log_rdma_event(INFO, "wait for transport being disconnected\n");
Long Lie8b3bfe2019-04-05 21:36:31 +00001338 wait_event_interruptible(
Long Li050b8c32019-04-04 11:35:42 -05001339 info->disconn_wait,
1340 info->transport_status == SMBD_DISCONNECTED);
1341 }
Long Li8ef130f2017-11-22 17:38:37 -07001342
Long Li050b8c32019-04-04 11:35:42 -05001343 log_rdma_event(INFO, "destroying qp\n");
1344 ib_drain_qp(info->id->qp);
1345 rdma_destroy_qp(info->id);
Long Li8ef130f2017-11-22 17:38:37 -07001346
Long Li050b8c32019-04-04 11:35:42 -05001347 log_rdma_event(INFO, "cancelling idle timer\n");
1348 cancel_delayed_work_sync(&info->idle_timer_work);
Long Li050b8c32019-04-04 11:35:42 -05001349
1350 log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
1351 wait_event(info->wait_send_pending,
1352 atomic_read(&info->send_pending) == 0);
Long Li050b8c32019-04-04 11:35:42 -05001353
1354 /* It's not posssible for upper layer to get to reassembly */
1355 log_rdma_event(INFO, "drain the reassembly queue\n");
1356 do {
1357 spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1358 response = _get_first_reassembly(info);
1359 if (response) {
1360 list_del(&response->list);
1361 spin_unlock_irqrestore(
1362 &info->reassembly_queue_lock, flags);
1363 put_receive_buffer(info, response);
1364 } else
1365 spin_unlock_irqrestore(
1366 &info->reassembly_queue_lock, flags);
1367 } while (response);
1368 info->reassembly_data_length = 0;
1369
1370 log_rdma_event(INFO, "free receive buffers\n");
1371 wait_event(info->wait_receive_queues,
1372 info->count_receive_queue + info->count_empty_packet_queue
1373 == info->receive_credit_max);
1374 destroy_receive_buffers(info);
1375
1376 /*
1377 * For performance reasons, memory registration and deregistration
1378 * are not locked by srv_mutex. It is possible some processes are
1379 * blocked on transport srv_mutex while holding memory registration.
1380 * Release the transport srv_mutex to allow them to hit the failure
1381 * path when sending data, and then release memory registartions.
1382 */
1383 log_rdma_event(INFO, "freeing mr list\n");
1384 wake_up_interruptible_all(&info->wait_mr);
1385 while (atomic_read(&info->mr_used_count)) {
1386 mutex_unlock(&server->srv_mutex);
1387 msleep(1000);
1388 mutex_lock(&server->srv_mutex);
1389 }
1390 destroy_mr_list(info);
1391
1392 ib_free_cq(info->send_cq);
1393 ib_free_cq(info->recv_cq);
1394 ib_dealloc_pd(info->pd);
1395 rdma_destroy_id(info->id);
1396
1397 /* free mempools */
1398 mempool_destroy(info->request_mempool);
1399 kmem_cache_destroy(info->request_cache);
1400
1401 mempool_destroy(info->response_mempool);
1402 kmem_cache_destroy(info->response_cache);
1403
1404 info->transport_status = SMBD_DESTROYED;
Long Li8ef130f2017-11-22 17:38:37 -07001405
1406 destroy_workqueue(info->workqueue);
Long Lid63cdba2019-10-16 13:51:53 -07001407 log_rdma_event(INFO, "rdma session destroyed\n");
Long Li8ef130f2017-11-22 17:38:37 -07001408 kfree(info);
1409}
1410
Long Liad57b8e2017-11-22 17:38:35 -07001411/*
1412 * Reconnect this SMBD connection, called from upper layer
1413 * return value: 0 on success, or actual error code
1414 */
1415int smbd_reconnect(struct TCP_Server_Info *server)
1416{
1417 log_rdma_event(INFO, "reconnecting rdma session\n");
1418
1419 if (!server->smbd_conn) {
Long Li48f238a2018-03-30 15:16:35 -07001420 log_rdma_event(INFO, "rdma session already destroyed\n");
1421 goto create_conn;
Long Liad57b8e2017-11-22 17:38:35 -07001422 }
1423
1424 /*
1425 * This is possible if transport is disconnected and we haven't received
1426 * notification from RDMA, but upper layer has detected timeout
1427 */
1428 if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1429 log_rdma_event(INFO, "disconnecting transport\n");
Long Li050b8c32019-04-04 11:35:42 -05001430 smbd_destroy(server);
Long Liad57b8e2017-11-22 17:38:35 -07001431 }
1432
Long Li48f238a2018-03-30 15:16:35 -07001433create_conn:
Long Liad57b8e2017-11-22 17:38:35 -07001434 log_rdma_event(INFO, "creating rdma session\n");
1435 server->smbd_conn = smbd_get_connection(
1436 server, (struct sockaddr *) &server->dstaddr);
Long Lid63cdba2019-10-16 13:51:53 -07001437
1438 if (server->smbd_conn)
1439 cifs_dbg(VFS, "RDMA transport re-established\n");
Long Liad57b8e2017-11-22 17:38:35 -07001440
1441 return server->smbd_conn ? 0 : -ENOENT;
1442}
1443
Long Lif1981862017-11-04 18:17:24 -07001444static void destroy_caches_and_workqueue(struct smbd_connection *info)
1445{
1446 destroy_receive_buffers(info);
1447 destroy_workqueue(info->workqueue);
1448 mempool_destroy(info->response_mempool);
1449 kmem_cache_destroy(info->response_cache);
1450 mempool_destroy(info->request_mempool);
1451 kmem_cache_destroy(info->request_cache);
1452}
1453
1454#define MAX_NAME_LEN 80
1455static int allocate_caches_and_workqueue(struct smbd_connection *info)
1456{
1457 char name[MAX_NAME_LEN];
1458 int rc;
1459
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001460 scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001461 info->request_cache =
1462 kmem_cache_create(
1463 name,
1464 sizeof(struct smbd_request) +
1465 sizeof(struct smbd_data_transfer),
1466 0, SLAB_HWCACHE_ALIGN, NULL);
1467 if (!info->request_cache)
1468 return -ENOMEM;
1469
1470 info->request_mempool =
1471 mempool_create(info->send_credit_target, mempool_alloc_slab,
1472 mempool_free_slab, info->request_cache);
1473 if (!info->request_mempool)
1474 goto out1;
1475
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001476 scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001477 info->response_cache =
1478 kmem_cache_create(
1479 name,
1480 sizeof(struct smbd_response) +
1481 info->max_receive_size,
1482 0, SLAB_HWCACHE_ALIGN, NULL);
1483 if (!info->response_cache)
1484 goto out2;
1485
1486 info->response_mempool =
1487 mempool_create(info->receive_credit_max, mempool_alloc_slab,
1488 mempool_free_slab, info->response_cache);
1489 if (!info->response_mempool)
1490 goto out3;
1491
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001492 scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001493 info->workqueue = create_workqueue(name);
1494 if (!info->workqueue)
1495 goto out4;
1496
1497 rc = allocate_receive_buffers(info, info->receive_credit_max);
1498 if (rc) {
1499 log_rdma_event(ERR, "failed to allocate receive buffers\n");
1500 goto out5;
1501 }
1502
1503 return 0;
1504
1505out5:
1506 destroy_workqueue(info->workqueue);
1507out4:
1508 mempool_destroy(info->response_mempool);
1509out3:
1510 kmem_cache_destroy(info->response_cache);
1511out2:
1512 mempool_destroy(info->request_mempool);
1513out1:
1514 kmem_cache_destroy(info->request_cache);
1515 return -ENOMEM;
1516}
1517
1518/* Create a SMBD connection, called by upper layer */
kbuild test robot90844322017-12-18 21:30:06 +08001519static struct smbd_connection *_smbd_get_connection(
Long Lif1981862017-11-04 18:17:24 -07001520 struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1521{
1522 int rc;
1523 struct smbd_connection *info;
1524 struct rdma_conn_param conn_param;
1525 struct ib_qp_init_attr qp_attr;
1526 struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
Long Lic7398582017-11-22 17:38:44 -07001527 struct ib_port_immutable port_immutable;
1528 u32 ird_ord_hdr[2];
Long Lif1981862017-11-04 18:17:24 -07001529
1530 info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1531 if (!info)
1532 return NULL;
1533
1534 info->transport_status = SMBD_CONNECTING;
1535 rc = smbd_ia_open(info, dstaddr, port);
1536 if (rc) {
1537 log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1538 goto create_id_failed;
1539 }
1540
1541 if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1542 smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1543 log_rdma_event(ERR,
1544 "consider lowering send_credit_target = %d. "
1545 "Possible CQE overrun, device "
1546 "reporting max_cpe %d max_qp_wr %d\n",
1547 smbd_send_credit_target,
1548 info->id->device->attrs.max_cqe,
1549 info->id->device->attrs.max_qp_wr);
1550 goto config_failed;
1551 }
1552
1553 if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1554 smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1555 log_rdma_event(ERR,
1556 "consider lowering receive_credit_max = %d. "
1557 "Possible CQE overrun, device "
1558 "reporting max_cpe %d max_qp_wr %d\n",
1559 smbd_receive_credit_max,
1560 info->id->device->attrs.max_cqe,
1561 info->id->device->attrs.max_qp_wr);
1562 goto config_failed;
1563 }
1564
1565 info->receive_credit_max = smbd_receive_credit_max;
1566 info->send_credit_target = smbd_send_credit_target;
1567 info->max_send_size = smbd_max_send_size;
1568 info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1569 info->max_receive_size = smbd_max_receive_size;
1570 info->keep_alive_interval = smbd_keep_alive_interval;
1571
Steve Wise33023fb2018-06-18 08:05:26 -07001572 if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
1573 log_rdma_event(ERR,
1574 "warning: device max_send_sge = %d too small\n",
1575 info->id->device->attrs.max_send_sge);
1576 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1577 }
1578 if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
1579 log_rdma_event(ERR,
1580 "warning: device max_recv_sge = %d too small\n",
1581 info->id->device->attrs.max_recv_sge);
Long Lif1981862017-11-04 18:17:24 -07001582 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1583 }
1584
1585 info->send_cq = NULL;
1586 info->recv_cq = NULL;
Chuck Lever20cf4e02019-07-29 13:22:09 -04001587 info->send_cq =
1588 ib_alloc_cq_any(info->id->device, info,
1589 info->send_credit_target, IB_POLL_SOFTIRQ);
Long Lif1981862017-11-04 18:17:24 -07001590 if (IS_ERR(info->send_cq)) {
1591 info->send_cq = NULL;
1592 goto alloc_cq_failed;
1593 }
1594
Chuck Lever20cf4e02019-07-29 13:22:09 -04001595 info->recv_cq =
1596 ib_alloc_cq_any(info->id->device, info,
1597 info->receive_credit_max, IB_POLL_SOFTIRQ);
Long Lif1981862017-11-04 18:17:24 -07001598 if (IS_ERR(info->recv_cq)) {
1599 info->recv_cq = NULL;
1600 goto alloc_cq_failed;
1601 }
1602
1603 memset(&qp_attr, 0, sizeof(qp_attr));
1604 qp_attr.event_handler = smbd_qp_async_error_upcall;
1605 qp_attr.qp_context = info;
1606 qp_attr.cap.max_send_wr = info->send_credit_target;
1607 qp_attr.cap.max_recv_wr = info->receive_credit_max;
1608 qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
1609 qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
1610 qp_attr.cap.max_inline_data = 0;
1611 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1612 qp_attr.qp_type = IB_QPT_RC;
1613 qp_attr.send_cq = info->send_cq;
1614 qp_attr.recv_cq = info->recv_cq;
1615 qp_attr.port_num = ~0;
1616
1617 rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1618 if (rc) {
1619 log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1620 goto create_qp_failed;
1621 }
1622
1623 memset(&conn_param, 0, sizeof(conn_param));
1624 conn_param.initiator_depth = 0;
1625
Long Lic7398582017-11-22 17:38:44 -07001626 conn_param.responder_resources =
1627 info->id->device->attrs.max_qp_rd_atom
1628 < SMBD_CM_RESPONDER_RESOURCES ?
1629 info->id->device->attrs.max_qp_rd_atom :
1630 SMBD_CM_RESPONDER_RESOURCES;
1631 info->responder_resources = conn_param.responder_resources;
1632 log_rdma_mr(INFO, "responder_resources=%d\n",
1633 info->responder_resources);
1634
1635 /* Need to send IRD/ORD in private data for iWARP */
Kamal Heib3023a1e2018-12-10 21:09:48 +02001636 info->id->device->ops.get_port_immutable(
Long Lic7398582017-11-22 17:38:44 -07001637 info->id->device, info->id->port_num, &port_immutable);
1638 if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1639 ird_ord_hdr[0] = info->responder_resources;
1640 ird_ord_hdr[1] = 1;
1641 conn_param.private_data = ird_ord_hdr;
1642 conn_param.private_data_len = sizeof(ird_ord_hdr);
1643 } else {
1644 conn_param.private_data = NULL;
1645 conn_param.private_data_len = 0;
1646 }
1647
Long Lif1981862017-11-04 18:17:24 -07001648 conn_param.retry_count = SMBD_CM_RETRY;
1649 conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1650 conn_param.flow_control = 0;
Long Lif1981862017-11-04 18:17:24 -07001651
1652 log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1653 &addr_in->sin_addr, port);
1654
1655 init_waitqueue_head(&info->conn_wait);
Long Li050b8c32019-04-04 11:35:42 -05001656 init_waitqueue_head(&info->disconn_wait);
1657 init_waitqueue_head(&info->wait_reassembly_queue);
Long Lif1981862017-11-04 18:17:24 -07001658 rc = rdma_connect(info->id, &conn_param);
1659 if (rc) {
1660 log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1661 goto rdma_connect_failed;
1662 }
1663
1664 wait_event_interruptible(
1665 info->conn_wait, info->transport_status != SMBD_CONNECTING);
1666
1667 if (info->transport_status != SMBD_CONNECTED) {
1668 log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1669 goto rdma_connect_failed;
1670 }
1671
1672 log_rdma_event(INFO, "rdma_connect connected\n");
1673
1674 rc = allocate_caches_and_workqueue(info);
1675 if (rc) {
1676 log_rdma_event(ERR, "cache allocation failed\n");
1677 goto allocate_cache_failed;
1678 }
1679
1680 init_waitqueue_head(&info->wait_send_queue);
Long Lif1981862017-11-04 18:17:24 -07001681 INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
Long Lif1981862017-11-04 18:17:24 -07001682 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1683 info->keep_alive_interval*HZ);
1684
1685 init_waitqueue_head(&info->wait_send_pending);
1686 atomic_set(&info->send_pending, 0);
1687
Long Li3ffbe782020-03-30 11:04:07 -07001688 init_waitqueue_head(&info->wait_post_send);
Long Lif1981862017-11-04 18:17:24 -07001689
1690 INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
Long Lif1981862017-11-04 18:17:24 -07001691 INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1692 info->new_credits_offered = 0;
1693 spin_lock_init(&info->lock_new_credits_offered);
1694
1695 rc = smbd_negotiate(info);
1696 if (rc) {
1697 log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1698 goto negotiation_failed;
1699 }
1700
Long Lic7398582017-11-22 17:38:44 -07001701 rc = allocate_mr_list(info);
1702 if (rc) {
1703 log_rdma_mr(ERR, "memory registration allocation failed\n");
1704 goto allocate_mr_failed;
1705 }
1706
Long Lif1981862017-11-04 18:17:24 -07001707 return info;
1708
Long Lic7398582017-11-22 17:38:44 -07001709allocate_mr_failed:
1710 /* At this point, need to a full transport shutdown */
Long Li050b8c32019-04-04 11:35:42 -05001711 smbd_destroy(server);
Long Lic7398582017-11-22 17:38:44 -07001712 return NULL;
1713
Long Lif1981862017-11-04 18:17:24 -07001714negotiation_failed:
1715 cancel_delayed_work_sync(&info->idle_timer_work);
1716 destroy_caches_and_workqueue(info);
1717 info->transport_status = SMBD_NEGOTIATE_FAILED;
1718 init_waitqueue_head(&info->conn_wait);
1719 rdma_disconnect(info->id);
1720 wait_event(info->conn_wait,
1721 info->transport_status == SMBD_DISCONNECTED);
1722
1723allocate_cache_failed:
1724rdma_connect_failed:
1725 rdma_destroy_qp(info->id);
1726
1727create_qp_failed:
1728alloc_cq_failed:
1729 if (info->send_cq)
1730 ib_free_cq(info->send_cq);
1731 if (info->recv_cq)
1732 ib_free_cq(info->recv_cq);
1733
1734config_failed:
1735 ib_dealloc_pd(info->pd);
1736 rdma_destroy_id(info->id);
1737
1738create_id_failed:
1739 kfree(info);
1740 return NULL;
1741}
Long Li399f9532017-11-17 17:26:52 -08001742
1743struct smbd_connection *smbd_get_connection(
1744 struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1745{
1746 struct smbd_connection *ret;
1747 int port = SMBD_PORT;
1748
1749try_again:
1750 ret = _smbd_get_connection(server, dstaddr, port);
1751
1752 /* Try SMB_PORT if SMBD_PORT doesn't work */
1753 if (!ret && port == SMBD_PORT) {
1754 port = SMB_PORT;
1755 goto try_again;
1756 }
1757 return ret;
1758}
Long Lif64b78f2017-11-22 17:38:40 -07001759
1760/*
1761 * Receive data from receive reassembly queue
1762 * All the incoming data packets are placed in reassembly queue
1763 * buf: the buffer to read data into
1764 * size: the length of data to read
1765 * return value: actual data read
1766 * Note: this implementation copies the data from reassebmly queue to receive
1767 * buffers used by upper layer. This is not the optimal code path. A better way
1768 * to do it is to not have upper layer allocate its receive buffers but rather
1769 * borrow the buffer from reassembly queue, and return it after data is
1770 * consumed. But this will require more changes to upper layer code, and also
1771 * need to consider packet boundaries while they still being reassembled.
1772 */
Steve French2026b062018-01-24 23:07:41 -06001773static int smbd_recv_buf(struct smbd_connection *info, char *buf,
1774 unsigned int size)
Long Lif64b78f2017-11-22 17:38:40 -07001775{
1776 struct smbd_response *response;
1777 struct smbd_data_transfer *data_transfer;
1778 int to_copy, to_read, data_read, offset;
1779 u32 data_length, remaining_data_length, data_offset;
1780 int rc;
Long Lif64b78f2017-11-22 17:38:40 -07001781
1782again:
Long Lif64b78f2017-11-22 17:38:40 -07001783 /*
1784 * No need to hold the reassembly queue lock all the time as we are
1785 * the only one reading from the front of the queue. The transport
1786 * may add more entries to the back of the queue at the same time
1787 */
1788 log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1789 info->reassembly_data_length);
1790 if (info->reassembly_data_length >= size) {
1791 int queue_length;
1792 int queue_removed = 0;
1793
1794 /*
1795 * Need to make sure reassembly_data_length is read before
1796 * reading reassembly_queue_length and calling
1797 * _get_first_reassembly. This call is lock free
1798 * as we never read at the end of the queue which are being
1799 * updated in SOFTIRQ as more data is received
1800 */
1801 virt_rmb();
1802 queue_length = info->reassembly_queue_length;
1803 data_read = 0;
1804 to_read = size;
1805 offset = info->first_entry_offset;
1806 while (data_read < size) {
1807 response = _get_first_reassembly(info);
1808 data_transfer = smbd_response_payload(response);
1809 data_length = le32_to_cpu(data_transfer->data_length);
1810 remaining_data_length =
1811 le32_to_cpu(
1812 data_transfer->remaining_data_length);
1813 data_offset = le32_to_cpu(data_transfer->data_offset);
1814
1815 /*
1816 * The upper layer expects RFC1002 length at the
1817 * beginning of the payload. Return it to indicate
1818 * the total length of the packet. This minimize the
1819 * change to upper layer packet processing logic. This
1820 * will be eventually remove when an intermediate
1821 * transport layer is added
1822 */
1823 if (response->first_segment && size == 4) {
1824 unsigned int rfc1002_len =
1825 data_length + remaining_data_length;
1826 *((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1827 data_read = 4;
1828 response->first_segment = false;
1829 log_read(INFO, "returning rfc1002 length %d\n",
1830 rfc1002_len);
1831 goto read_rfc1002_done;
1832 }
1833
1834 to_copy = min_t(int, data_length - offset, to_read);
1835 memcpy(
1836 buf + data_read,
1837 (char *)data_transfer + data_offset + offset,
1838 to_copy);
1839
1840 /* move on to the next buffer? */
1841 if (to_copy == data_length - offset) {
1842 queue_length--;
1843 /*
1844 * No need to lock if we are not at the
1845 * end of the queue
1846 */
Steve Frenchf9de1512018-02-03 19:45:07 -06001847 if (queue_length)
1848 list_del(&response->list);
1849 else {
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001850 spin_lock_irq(
1851 &info->reassembly_queue_lock);
Steve Frenchf9de1512018-02-03 19:45:07 -06001852 list_del(&response->list);
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001853 spin_unlock_irq(
1854 &info->reassembly_queue_lock);
Steve Frenchf9de1512018-02-03 19:45:07 -06001855 }
1856 queue_removed++;
Long Lif64b78f2017-11-22 17:38:40 -07001857 info->count_reassembly_queue--;
1858 info->count_dequeue_reassembly_queue++;
1859 put_receive_buffer(info, response);
1860 offset = 0;
1861 log_read(INFO, "put_receive_buffer offset=0\n");
1862 } else
1863 offset += to_copy;
1864
1865 to_read -= to_copy;
1866 data_read += to_copy;
1867
1868 log_read(INFO, "_get_first_reassembly memcpy %d bytes "
1869 "data_transfer_length-offset=%d after that "
1870 "to_read=%d data_read=%d offset=%d\n",
1871 to_copy, data_length - offset,
1872 to_read, data_read, offset);
1873 }
1874
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001875 spin_lock_irq(&info->reassembly_queue_lock);
Long Lif64b78f2017-11-22 17:38:40 -07001876 info->reassembly_data_length -= data_read;
1877 info->reassembly_queue_length -= queue_removed;
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001878 spin_unlock_irq(&info->reassembly_queue_lock);
Long Lif64b78f2017-11-22 17:38:40 -07001879
1880 info->first_entry_offset = offset;
1881 log_read(INFO, "returning to thread data_read=%d "
1882 "reassembly_data_length=%d first_entry_offset=%d\n",
1883 data_read, info->reassembly_data_length,
1884 info->first_entry_offset);
1885read_rfc1002_done:
1886 return data_read;
1887 }
1888
1889 log_read(INFO, "wait_event on more data\n");
1890 rc = wait_event_interruptible(
1891 info->wait_reassembly_queue,
1892 info->reassembly_data_length >= size ||
1893 info->transport_status != SMBD_CONNECTED);
1894 /* Don't return any data if interrupted */
1895 if (rc)
Long Li98e0d402019-04-05 21:36:32 +00001896 return rc;
Long Lif64b78f2017-11-22 17:38:40 -07001897
Long Lie8b3bfe2019-04-05 21:36:31 +00001898 if (info->transport_status != SMBD_CONNECTED) {
1899 log_read(ERR, "disconnected\n");
Long Liacd46802019-10-16 13:51:54 -07001900 return -ECONNABORTED;
Long Lie8b3bfe2019-04-05 21:36:31 +00001901 }
1902
Long Lif64b78f2017-11-22 17:38:40 -07001903 goto again;
1904}
1905
1906/*
1907 * Receive a page from receive reassembly queue
1908 * page: the page to read data into
1909 * to_read: the length of data to read
1910 * return value: actual data read
1911 */
Steve French2026b062018-01-24 23:07:41 -06001912static int smbd_recv_page(struct smbd_connection *info,
Long Li6509f502018-05-30 12:48:01 -07001913 struct page *page, unsigned int page_offset,
1914 unsigned int to_read)
Long Lif64b78f2017-11-22 17:38:40 -07001915{
1916 int ret;
1917 char *to_address;
Long Li6509f502018-05-30 12:48:01 -07001918 void *page_address;
Long Lif64b78f2017-11-22 17:38:40 -07001919
1920 /* make sure we have the page ready for read */
1921 ret = wait_event_interruptible(
1922 info->wait_reassembly_queue,
1923 info->reassembly_data_length >= to_read ||
1924 info->transport_status != SMBD_CONNECTED);
1925 if (ret)
Long Li6509f502018-05-30 12:48:01 -07001926 return ret;
Long Lif64b78f2017-11-22 17:38:40 -07001927
1928 /* now we can read from reassembly queue and not sleep */
Long Li6509f502018-05-30 12:48:01 -07001929 page_address = kmap_atomic(page);
1930 to_address = (char *) page_address + page_offset;
Long Lif64b78f2017-11-22 17:38:40 -07001931
1932 log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
1933 page, to_address, to_read);
1934
1935 ret = smbd_recv_buf(info, to_address, to_read);
Long Li6509f502018-05-30 12:48:01 -07001936 kunmap_atomic(page_address);
Long Lif64b78f2017-11-22 17:38:40 -07001937
1938 return ret;
1939}
1940
1941/*
1942 * Receive data from transport
1943 * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
1944 * return: total bytes read, or 0. SMB Direct will not do partial read.
1945 */
1946int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
1947{
1948 char *buf;
1949 struct page *page;
Long Li6509f502018-05-30 12:48:01 -07001950 unsigned int to_read, page_offset;
Long Lif64b78f2017-11-22 17:38:40 -07001951 int rc;
1952
David Howells00e23702018-10-22 13:07:28 +01001953 if (iov_iter_rw(&msg->msg_iter) == WRITE) {
1954 /* It's a bug in upper layer to get there */
1955 cifs_dbg(VFS, "CIFS: invalid msg iter dir %u\n",
1956 iov_iter_rw(&msg->msg_iter));
1957 rc = -EINVAL;
1958 goto out;
1959 }
1960
1961 switch (iov_iter_type(&msg->msg_iter)) {
1962 case ITER_KVEC:
Long Lif64b78f2017-11-22 17:38:40 -07001963 buf = msg->msg_iter.kvec->iov_base;
1964 to_read = msg->msg_iter.kvec->iov_len;
1965 rc = smbd_recv_buf(info, buf, to_read);
1966 break;
1967
David Howells00e23702018-10-22 13:07:28 +01001968 case ITER_BVEC:
Long Lif64b78f2017-11-22 17:38:40 -07001969 page = msg->msg_iter.bvec->bv_page;
Long Li6509f502018-05-30 12:48:01 -07001970 page_offset = msg->msg_iter.bvec->bv_offset;
Long Lif64b78f2017-11-22 17:38:40 -07001971 to_read = msg->msg_iter.bvec->bv_len;
Long Li6509f502018-05-30 12:48:01 -07001972 rc = smbd_recv_page(info, page, page_offset, to_read);
Long Lif64b78f2017-11-22 17:38:40 -07001973 break;
1974
1975 default:
1976 /* It's a bug in upper layer to get there */
1977 cifs_dbg(VFS, "CIFS: invalid msg type %d\n",
David Howells00e23702018-10-22 13:07:28 +01001978 iov_iter_type(&msg->msg_iter));
Long Li6509f502018-05-30 12:48:01 -07001979 rc = -EINVAL;
Long Lif64b78f2017-11-22 17:38:40 -07001980 }
1981
David Howells00e23702018-10-22 13:07:28 +01001982out:
Long Lif64b78f2017-11-22 17:38:40 -07001983 /* SMBDirect will read it all or nothing */
1984 if (rc > 0)
1985 msg->msg_iter.count = 0;
1986 return rc;
1987}
Long Lid649e1b2017-11-22 17:38:42 -07001988
1989/*
1990 * Send data to transport
1991 * Each rqst is transported as a SMBDirect payload
1992 * rqst: the data to write
1993 * return value: 0 if successfully write, otherwise error code
1994 */
Long Li4739f232019-04-15 14:49:17 -07001995int smbd_send(struct TCP_Server_Info *server,
1996 int num_rqst, struct smb_rqst *rqst_array)
Long Lid649e1b2017-11-22 17:38:42 -07001997{
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +10001998 struct smbd_connection *info = server->smbd_conn;
Long Lid649e1b2017-11-22 17:38:42 -07001999 struct kvec vec;
2000 int nvecs;
2001 int size;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -03002002 unsigned int buflen, remaining_data_length;
Long Lid649e1b2017-11-22 17:38:42 -07002003 int start, i, j;
2004 int max_iov_size =
2005 info->max_send_size - sizeof(struct smbd_data_transfer);
Long Li8bcda1d2018-04-17 12:17:07 -07002006 struct kvec *iov;
Long Lid649e1b2017-11-22 17:38:42 -07002007 int rc;
Long Li4739f232019-04-15 14:49:17 -07002008 struct smb_rqst *rqst;
2009 int rqst_idx;
Long Lid649e1b2017-11-22 17:38:42 -07002010
Long Lid649e1b2017-11-22 17:38:42 -07002011 if (info->transport_status != SMBD_CONNECTED) {
Long Li62fdf672019-04-05 21:36:33 +00002012 rc = -EAGAIN;
Long Lid649e1b2017-11-22 17:38:42 -07002013 goto done;
2014 }
2015
2016 /*
Long Lib6903bc2018-05-30 12:48:00 -07002017 * Add in the page array if there is one. The caller needs to set
2018 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
2019 * ends at page boundary
2020 */
Long Li4739f232019-04-15 14:49:17 -07002021 remaining_data_length = 0;
2022 for (i = 0; i < num_rqst; i++)
2023 remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
Long Lid649e1b2017-11-22 17:38:42 -07002024
Long Lif7950cb2020-03-26 19:42:24 -07002025 if (remaining_data_length > info->max_fragmented_send_size) {
Long Lid649e1b2017-11-22 17:38:42 -07002026 log_write(ERR, "payload size %d > max size %d\n",
Long Li4739f232019-04-15 14:49:17 -07002027 remaining_data_length, info->max_fragmented_send_size);
Long Lid649e1b2017-11-22 17:38:42 -07002028 rc = -EINVAL;
2029 goto done;
2030 }
2031
Long Li7f46d232019-05-13 21:01:29 -07002032 log_write(INFO, "num_rqst=%d total length=%u\n",
2033 num_rqst, remaining_data_length);
Paulo Alcantara35e2cc12018-06-15 10:22:44 -03002034
Long Li7f46d232019-05-13 21:01:29 -07002035 rqst_idx = 0;
Long Li4739f232019-04-15 14:49:17 -07002036next_rqst:
2037 rqst = &rqst_array[rqst_idx];
2038 iov = rqst->rq_iov;
2039
2040 cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
2041 rqst_idx, smb_rqst_len(server, rqst));
2042 for (i = 0; i < rqst->rq_nvec; i++)
Long Liff30b892018-04-17 12:17:10 -07002043 dump_smb(iov[i].iov_base, iov[i].iov_len);
2044
Long Lid649e1b2017-11-22 17:38:42 -07002045
Long Li4739f232019-04-15 14:49:17 -07002046 log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d "
2047 "rq_tailsz=%d buflen=%lu\n",
2048 rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
2049 rqst->rq_tailsz, smb_rqst_len(server, rqst));
Long Lid649e1b2017-11-22 17:38:42 -07002050
Long Li4739f232019-04-15 14:49:17 -07002051 start = i = 0;
Long Lid649e1b2017-11-22 17:38:42 -07002052 buflen = 0;
2053 while (true) {
2054 buflen += iov[i].iov_len;
2055 if (buflen > max_iov_size) {
2056 if (i > start) {
2057 remaining_data_length -=
2058 (buflen-iov[i].iov_len);
2059 log_write(INFO, "sending iov[] from start=%d "
2060 "i=%d nvecs=%d "
2061 "remaining_data_length=%d\n",
2062 start, i, i-start,
2063 remaining_data_length);
2064 rc = smbd_post_send_data(
2065 info, &iov[start], i-start,
2066 remaining_data_length);
2067 if (rc)
2068 goto done;
2069 } else {
2070 /* iov[start] is too big, break it */
2071 nvecs = (buflen+max_iov_size-1)/max_iov_size;
2072 log_write(INFO, "iov[%d] iov_base=%p buflen=%d"
2073 " break to %d vectors\n",
2074 start, iov[start].iov_base,
2075 buflen, nvecs);
2076 for (j = 0; j < nvecs; j++) {
2077 vec.iov_base =
2078 (char *)iov[start].iov_base +
2079 j*max_iov_size;
2080 vec.iov_len = max_iov_size;
2081 if (j == nvecs-1)
2082 vec.iov_len =
2083 buflen -
2084 max_iov_size*(nvecs-1);
2085 remaining_data_length -= vec.iov_len;
2086 log_write(INFO,
2087 "sending vec j=%d iov_base=%p"
2088 " iov_len=%zu "
2089 "remaining_data_length=%d\n",
2090 j, vec.iov_base, vec.iov_len,
2091 remaining_data_length);
2092 rc = smbd_post_send_data(
2093 info, &vec, 1,
2094 remaining_data_length);
2095 if (rc)
2096 goto done;
2097 }
2098 i++;
Long Li4739f232019-04-15 14:49:17 -07002099 if (i == rqst->rq_nvec)
Long Liab60ee72018-04-17 12:17:05 -07002100 break;
Long Lid649e1b2017-11-22 17:38:42 -07002101 }
2102 start = i;
2103 buflen = 0;
2104 } else {
2105 i++;
Long Li4739f232019-04-15 14:49:17 -07002106 if (i == rqst->rq_nvec) {
Long Lid649e1b2017-11-22 17:38:42 -07002107 /* send out all remaining vecs */
2108 remaining_data_length -= buflen;
2109 log_write(INFO,
2110 "sending iov[] from start=%d i=%d "
2111 "nvecs=%d remaining_data_length=%d\n",
2112 start, i, i-start,
2113 remaining_data_length);
2114 rc = smbd_post_send_data(info, &iov[start],
2115 i-start, remaining_data_length);
2116 if (rc)
2117 goto done;
2118 break;
2119 }
2120 }
2121 log_write(INFO, "looping i=%d buflen=%d\n", i, buflen);
2122 }
2123
2124 /* now sending pages if there are any */
2125 for (i = 0; i < rqst->rq_npages; i++) {
Long Lib6903bc2018-05-30 12:48:00 -07002126 unsigned int offset;
2127
2128 rqst_page_get_length(rqst, i, &buflen, &offset);
Long Lid649e1b2017-11-22 17:38:42 -07002129 nvecs = (buflen + max_iov_size - 1) / max_iov_size;
2130 log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
2131 buflen, nvecs);
2132 for (j = 0; j < nvecs; j++) {
2133 size = max_iov_size;
2134 if (j == nvecs-1)
2135 size = buflen - j*max_iov_size;
2136 remaining_data_length -= size;
2137 log_write(INFO, "sending pages i=%d offset=%d size=%d"
2138 " remaining_data_length=%d\n",
Long Lib6903bc2018-05-30 12:48:00 -07002139 i, j*max_iov_size+offset, size,
2140 remaining_data_length);
Long Lid649e1b2017-11-22 17:38:42 -07002141 rc = smbd_post_send_page(
Long Lib6903bc2018-05-30 12:48:00 -07002142 info, rqst->rq_pages[i],
2143 j*max_iov_size + offset,
Long Lid649e1b2017-11-22 17:38:42 -07002144 size, remaining_data_length);
2145 if (rc)
2146 goto done;
2147 }
2148 }
2149
Long Li4739f232019-04-15 14:49:17 -07002150 rqst_idx++;
2151 if (rqst_idx < num_rqst)
2152 goto next_rqst;
2153
Long Lid649e1b2017-11-22 17:38:42 -07002154done:
2155 /*
2156 * As an optimization, we don't wait for individual I/O to finish
2157 * before sending the next one.
2158 * Send them all and wait for pending send count to get to 0
2159 * that means all the I/Os have been out and we are good to return
2160 */
2161
Long Li072a14e2020-03-30 11:04:06 -07002162 wait_event(info->wait_send_pending,
2163 atomic_read(&info->send_pending) == 0);
Long Lid649e1b2017-11-22 17:38:42 -07002164
Long Lid649e1b2017-11-22 17:38:42 -07002165 return rc;
2166}
Long Lic7398582017-11-22 17:38:44 -07002167
2168static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
2169{
2170 struct smbd_mr *mr;
2171 struct ib_cqe *cqe;
2172
2173 if (wc->status) {
2174 log_rdma_mr(ERR, "status=%d\n", wc->status);
2175 cqe = wc->wr_cqe;
2176 mr = container_of(cqe, struct smbd_mr, cqe);
2177 smbd_disconnect_rdma_connection(mr->conn);
2178 }
2179}
2180
2181/*
2182 * The work queue function that recovers MRs
2183 * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
2184 * again. Both calls are slow, so finish them in a workqueue. This will not
2185 * block I/O path.
2186 * There is one workqueue that recovers MRs, there is no need to lock as the
2187 * I/O requests calling smbd_register_mr will never update the links in the
2188 * mr_list.
2189 */
2190static void smbd_mr_recovery_work(struct work_struct *work)
2191{
2192 struct smbd_connection *info =
2193 container_of(work, struct smbd_connection, mr_recovery_work);
2194 struct smbd_mr *smbdirect_mr;
2195 int rc;
2196
2197 list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
Long Lic21ce582019-10-16 13:51:55 -07002198 if (smbdirect_mr->state == MR_ERROR) {
Long Lic7398582017-11-22 17:38:44 -07002199
Long Li7cf20bc2018-05-30 12:48:02 -07002200 /* recover this MR entry */
2201 rc = ib_dereg_mr(smbdirect_mr->mr);
2202 if (rc) {
2203 log_rdma_mr(ERR,
2204 "ib_dereg_mr failed rc=%x\n",
2205 rc);
2206 smbd_disconnect_rdma_connection(info);
2207 continue;
2208 }
2209
2210 smbdirect_mr->mr = ib_alloc_mr(
2211 info->pd, info->mr_type,
2212 info->max_frmr_depth);
2213 if (IS_ERR(smbdirect_mr->mr)) {
2214 log_rdma_mr(ERR,
2215 "ib_alloc_mr failed mr_type=%x "
2216 "max_frmr_depth=%x\n",
2217 info->mr_type,
2218 info->max_frmr_depth);
2219 smbd_disconnect_rdma_connection(info);
2220 continue;
2221 }
Long Liff526d82018-09-20 21:18:39 +00002222 } else
2223 /* This MR is being used, don't recover it */
2224 continue;
Long Li7cf20bc2018-05-30 12:48:02 -07002225
Long Liff526d82018-09-20 21:18:39 +00002226 smbdirect_mr->state = MR_READY;
Long Lic7398582017-11-22 17:38:44 -07002227
Long Liff526d82018-09-20 21:18:39 +00002228 /* smbdirect_mr->state is updated by this function
2229 * and is read and updated by I/O issuing CPUs trying
2230 * to get a MR, the call to atomic_inc_return
2231 * implicates a memory barrier and guarantees this
2232 * value is updated before waking up any calls to
2233 * get_mr() from the I/O issuing CPUs
2234 */
2235 if (atomic_inc_return(&info->mr_ready_count) == 1)
2236 wake_up_interruptible(&info->wait_mr);
Long Lic7398582017-11-22 17:38:44 -07002237 }
2238}
2239
2240static void destroy_mr_list(struct smbd_connection *info)
2241{
2242 struct smbd_mr *mr, *tmp;
2243
2244 cancel_work_sync(&info->mr_recovery_work);
2245 list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
2246 if (mr->state == MR_INVALIDATED)
2247 ib_dma_unmap_sg(info->id->device, mr->sgl,
2248 mr->sgl_count, mr->dir);
2249 ib_dereg_mr(mr->mr);
2250 kfree(mr->sgl);
2251 kfree(mr);
2252 }
2253}
2254
2255/*
2256 * Allocate MRs used for RDMA read/write
2257 * The number of MRs will not exceed hardware capability in responder_resources
2258 * All MRs are kept in mr_list. The MR can be recovered after it's used
2259 * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
2260 * as MRs are used and recovered for I/O, but the list links will not change
2261 */
2262static int allocate_mr_list(struct smbd_connection *info)
2263{
2264 int i;
2265 struct smbd_mr *smbdirect_mr, *tmp;
2266
2267 INIT_LIST_HEAD(&info->mr_list);
2268 init_waitqueue_head(&info->wait_mr);
2269 spin_lock_init(&info->mr_list_lock);
2270 atomic_set(&info->mr_ready_count, 0);
2271 atomic_set(&info->mr_used_count, 0);
2272 init_waitqueue_head(&info->wait_for_mr_cleanup);
2273 /* Allocate more MRs (2x) than hardware responder_resources */
2274 for (i = 0; i < info->responder_resources * 2; i++) {
2275 smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
2276 if (!smbdirect_mr)
2277 goto out;
2278 smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
2279 info->max_frmr_depth);
2280 if (IS_ERR(smbdirect_mr->mr)) {
2281 log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x "
2282 "max_frmr_depth=%x\n",
2283 info->mr_type, info->max_frmr_depth);
2284 goto out;
2285 }
2286 smbdirect_mr->sgl = kcalloc(
2287 info->max_frmr_depth,
2288 sizeof(struct scatterlist),
2289 GFP_KERNEL);
2290 if (!smbdirect_mr->sgl) {
2291 log_rdma_mr(ERR, "failed to allocate sgl\n");
2292 ib_dereg_mr(smbdirect_mr->mr);
2293 goto out;
2294 }
2295 smbdirect_mr->state = MR_READY;
2296 smbdirect_mr->conn = info;
2297
2298 list_add_tail(&smbdirect_mr->list, &info->mr_list);
2299 atomic_inc(&info->mr_ready_count);
2300 }
2301 INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
2302 return 0;
2303
2304out:
2305 kfree(smbdirect_mr);
2306
2307 list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
2308 ib_dereg_mr(smbdirect_mr->mr);
2309 kfree(smbdirect_mr->sgl);
2310 kfree(smbdirect_mr);
2311 }
2312 return -ENOMEM;
2313}
2314
2315/*
2316 * Get a MR from mr_list. This function waits until there is at least one
2317 * MR available in the list. It may access the list while the
2318 * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
2319 * as they never modify the same places. However, there may be several CPUs
2320 * issueing I/O trying to get MR at the same time, mr_list_lock is used to
2321 * protect this situation.
2322 */
2323static struct smbd_mr *get_mr(struct smbd_connection *info)
2324{
2325 struct smbd_mr *ret;
2326 int rc;
2327again:
2328 rc = wait_event_interruptible(info->wait_mr,
2329 atomic_read(&info->mr_ready_count) ||
2330 info->transport_status != SMBD_CONNECTED);
2331 if (rc) {
2332 log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
2333 return NULL;
2334 }
2335
2336 if (info->transport_status != SMBD_CONNECTED) {
2337 log_rdma_mr(ERR, "info->transport_status=%x\n",
2338 info->transport_status);
2339 return NULL;
2340 }
2341
2342 spin_lock(&info->mr_list_lock);
2343 list_for_each_entry(ret, &info->mr_list, list) {
2344 if (ret->state == MR_READY) {
2345 ret->state = MR_REGISTERED;
2346 spin_unlock(&info->mr_list_lock);
2347 atomic_dec(&info->mr_ready_count);
2348 atomic_inc(&info->mr_used_count);
2349 return ret;
2350 }
2351 }
2352
2353 spin_unlock(&info->mr_list_lock);
2354 /*
2355 * It is possible that we could fail to get MR because other processes may
2356 * try to acquire a MR at the same time. If this is the case, retry it.
2357 */
2358 goto again;
2359}
2360
2361/*
2362 * Register memory for RDMA read/write
2363 * pages[]: the list of pages to register memory with
2364 * num_pages: the number of pages to register
2365 * tailsz: if non-zero, the bytes to register in the last page
2366 * writing: true if this is a RDMA write (SMB read), false for RDMA read
2367 * need_invalidate: true if this MR needs to be locally invalidated after I/O
2368 * return value: the MR registered, NULL if failed.
2369 */
2370struct smbd_mr *smbd_register_mr(
2371 struct smbd_connection *info, struct page *pages[], int num_pages,
Long Li7cf20bc2018-05-30 12:48:02 -07002372 int offset, int tailsz, bool writing, bool need_invalidate)
Long Lic7398582017-11-22 17:38:44 -07002373{
2374 struct smbd_mr *smbdirect_mr;
2375 int rc, i;
2376 enum dma_data_direction dir;
2377 struct ib_reg_wr *reg_wr;
Long Lic7398582017-11-22 17:38:44 -07002378
2379 if (num_pages > info->max_frmr_depth) {
2380 log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
2381 num_pages, info->max_frmr_depth);
2382 return NULL;
2383 }
2384
2385 smbdirect_mr = get_mr(info);
2386 if (!smbdirect_mr) {
2387 log_rdma_mr(ERR, "get_mr returning NULL\n");
2388 return NULL;
2389 }
2390 smbdirect_mr->need_invalidate = need_invalidate;
2391 smbdirect_mr->sgl_count = num_pages;
2392 sg_init_table(smbdirect_mr->sgl, num_pages);
2393
Long Li7cf20bc2018-05-30 12:48:02 -07002394 log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
2395 num_pages, offset, tailsz);
Long Lic7398582017-11-22 17:38:44 -07002396
Long Li7cf20bc2018-05-30 12:48:02 -07002397 if (num_pages == 1) {
2398 sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
2399 goto skip_multiple_pages;
2400 }
2401
2402 /* We have at least two pages to register */
2403 sg_set_page(
2404 &smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
2405 i = 1;
2406 while (i < num_pages - 1) {
2407 sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
2408 i++;
2409 }
Long Lic7398582017-11-22 17:38:44 -07002410 sg_set_page(&smbdirect_mr->sgl[i], pages[i],
2411 tailsz ? tailsz : PAGE_SIZE, 0);
2412
Long Li7cf20bc2018-05-30 12:48:02 -07002413skip_multiple_pages:
Long Lic7398582017-11-22 17:38:44 -07002414 dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2415 smbdirect_mr->dir = dir;
2416 rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
2417 if (!rc) {
Long Li7cf20bc2018-05-30 12:48:02 -07002418 log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
Long Lic7398582017-11-22 17:38:44 -07002419 num_pages, dir, rc);
2420 goto dma_map_error;
2421 }
2422
2423 rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
2424 NULL, PAGE_SIZE);
2425 if (rc != num_pages) {
Long Li7cf20bc2018-05-30 12:48:02 -07002426 log_rdma_mr(ERR,
2427 "ib_map_mr_sg failed rc = %d num_pages = %x\n",
Long Lic7398582017-11-22 17:38:44 -07002428 rc, num_pages);
2429 goto map_mr_error;
2430 }
2431
2432 ib_update_fast_reg_key(smbdirect_mr->mr,
2433 ib_inc_rkey(smbdirect_mr->mr->rkey));
2434 reg_wr = &smbdirect_mr->wr;
2435 reg_wr->wr.opcode = IB_WR_REG_MR;
2436 smbdirect_mr->cqe.done = register_mr_done;
2437 reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
2438 reg_wr->wr.num_sge = 0;
2439 reg_wr->wr.send_flags = IB_SEND_SIGNALED;
2440 reg_wr->mr = smbdirect_mr->mr;
2441 reg_wr->key = smbdirect_mr->mr->rkey;
2442 reg_wr->access = writing ?
2443 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2444 IB_ACCESS_REMOTE_READ;
2445
2446 /*
2447 * There is no need for waiting for complemtion on ib_post_send
2448 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
2449 * on the next ib_post_send when we actaully send I/O to remote peer
2450 */
Bart Van Assche73930592018-07-18 09:25:25 -07002451 rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
Long Lic7398582017-11-22 17:38:44 -07002452 if (!rc)
2453 return smbdirect_mr;
2454
2455 log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
2456 rc, reg_wr->key);
2457
2458 /* If all failed, attempt to recover this MR by setting it MR_ERROR*/
2459map_mr_error:
2460 ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
2461 smbdirect_mr->sgl_count, smbdirect_mr->dir);
2462
2463dma_map_error:
2464 smbdirect_mr->state = MR_ERROR;
2465 if (atomic_dec_and_test(&info->mr_used_count))
2466 wake_up(&info->wait_for_mr_cleanup);
2467
Long Li21a4e142018-03-30 15:16:36 -07002468 smbd_disconnect_rdma_connection(info);
2469
Long Lic7398582017-11-22 17:38:44 -07002470 return NULL;
2471}
2472
2473static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
2474{
2475 struct smbd_mr *smbdirect_mr;
2476 struct ib_cqe *cqe;
2477
2478 cqe = wc->wr_cqe;
2479 smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
2480 smbdirect_mr->state = MR_INVALIDATED;
2481 if (wc->status != IB_WC_SUCCESS) {
2482 log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
2483 smbdirect_mr->state = MR_ERROR;
2484 }
2485 complete(&smbdirect_mr->invalidate_done);
2486}
2487
2488/*
2489 * Deregister a MR after I/O is done
2490 * This function may wait if remote invalidation is not used
2491 * and we have to locally invalidate the buffer to prevent data is being
2492 * modified by remote peer after upper layer consumes it
2493 */
2494int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
2495{
Bart Van Assche73930592018-07-18 09:25:25 -07002496 struct ib_send_wr *wr;
Long Lic7398582017-11-22 17:38:44 -07002497 struct smbd_connection *info = smbdirect_mr->conn;
2498 int rc = 0;
2499
2500 if (smbdirect_mr->need_invalidate) {
2501 /* Need to finish local invalidation before returning */
2502 wr = &smbdirect_mr->inv_wr;
2503 wr->opcode = IB_WR_LOCAL_INV;
2504 smbdirect_mr->cqe.done = local_inv_done;
2505 wr->wr_cqe = &smbdirect_mr->cqe;
2506 wr->num_sge = 0;
2507 wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
2508 wr->send_flags = IB_SEND_SIGNALED;
2509
2510 init_completion(&smbdirect_mr->invalidate_done);
Bart Van Assche73930592018-07-18 09:25:25 -07002511 rc = ib_post_send(info->id->qp, wr, NULL);
Long Lic7398582017-11-22 17:38:44 -07002512 if (rc) {
2513 log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
2514 smbd_disconnect_rdma_connection(info);
2515 goto done;
2516 }
2517 wait_for_completion(&smbdirect_mr->invalidate_done);
2518 smbdirect_mr->need_invalidate = false;
2519 } else
2520 /*
2521 * For remote invalidation, just set it to MR_INVALIDATED
2522 * and defer to mr_recovery_work to recover the MR for next use
2523 */
2524 smbdirect_mr->state = MR_INVALIDATED;
2525
Long Lic21ce582019-10-16 13:51:55 -07002526 if (smbdirect_mr->state == MR_INVALIDATED) {
2527 ib_dma_unmap_sg(
2528 info->id->device, smbdirect_mr->sgl,
2529 smbdirect_mr->sgl_count,
2530 smbdirect_mr->dir);
2531 smbdirect_mr->state = MR_READY;
2532 if (atomic_inc_return(&info->mr_ready_count) == 1)
2533 wake_up_interruptible(&info->wait_mr);
2534 } else
2535 /*
2536 * Schedule the work to do MR recovery for future I/Os MR
2537 * recovery is slow and don't want it to block current I/O
2538 */
2539 queue_work(info->workqueue, &info->mr_recovery_work);
Long Lic7398582017-11-22 17:38:44 -07002540
2541done:
2542 if (atomic_dec_and_test(&info->mr_used_count))
2543 wake_up(&info->wait_for_mr_cleanup);
2544
2545 return rc;
2546}