blob: c7ef2d7ce0ef8d391c8dfe0cb46ccdd287a61c8d [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
383/*
384 * Check and schedule to send an immediate packet
385 * This is used to extend credtis to remote peer to keep the transport busy
386 */
387static void check_and_send_immediate(struct smbd_connection *info)
388{
389 if (info->transport_status != SMBD_CONNECTED)
390 return;
391
392 info->send_immediate = true;
393
394 /*
395 * Promptly send a packet if our peer is running low on receive
396 * credits
397 */
398 if (atomic_read(&info->receive_credits) <
399 info->receive_credit_target - 1)
400 queue_delayed_work(
401 info->workqueue, &info->send_immediate_work, 0);
402}
403
404static void smbd_post_send_credits(struct work_struct *work)
405{
406 int ret = 0;
407 int use_receive_queue = 1;
408 int rc;
409 struct smbd_response *response;
410 struct smbd_connection *info =
411 container_of(work, struct smbd_connection,
412 post_send_credits_work);
413
414 if (info->transport_status != SMBD_CONNECTED) {
415 wake_up(&info->wait_receive_queues);
416 return;
417 }
418
419 if (info->receive_credit_target >
420 atomic_read(&info->receive_credits)) {
421 while (true) {
422 if (use_receive_queue)
423 response = get_receive_buffer(info);
424 else
425 response = get_empty_queue_buffer(info);
426 if (!response) {
427 /* now switch to emtpy packet queue */
428 if (use_receive_queue) {
429 use_receive_queue = 0;
430 continue;
431 } else
432 break;
433 }
434
435 response->type = SMBD_TRANSFER_DATA;
436 response->first_segment = false;
437 rc = smbd_post_recv(info, response);
438 if (rc) {
439 log_rdma_recv(ERR,
440 "post_recv failed rc=%d\n", rc);
441 put_receive_buffer(info, response);
442 break;
443 }
444
445 ret++;
446 }
447 }
448
449 spin_lock(&info->lock_new_credits_offered);
450 info->new_credits_offered += ret;
451 spin_unlock(&info->lock_new_credits_offered);
452
453 atomic_add(ret, &info->receive_credits);
454
455 /* Check if we can post new receive and grant credits to peer */
456 check_and_send_immediate(info);
457}
458
Long Lif1981862017-11-04 18:17:24 -0700459/* Called from softirq, when recv is done */
460static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
461{
462 struct smbd_data_transfer *data_transfer;
463 struct smbd_response *response =
464 container_of(wc->wr_cqe, struct smbd_response, cqe);
465 struct smbd_connection *info = response->info;
466 int data_length = 0;
467
468 log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d "
469 "byte_len=%d pkey_index=%x\n",
470 response, response->type, wc->status, wc->opcode,
471 wc->byte_len, wc->pkey_index);
472
473 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
474 log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
475 wc->status, wc->opcode);
476 smbd_disconnect_rdma_connection(info);
477 goto error;
478 }
479
480 ib_dma_sync_single_for_cpu(
481 wc->qp->device,
482 response->sge.addr,
483 response->sge.length,
484 DMA_FROM_DEVICE);
485
486 switch (response->type) {
487 /* SMBD negotiation response */
488 case SMBD_NEGOTIATE_RESP:
489 dump_smbd_negotiate_resp(smbd_response_payload(response));
490 info->full_packet_received = true;
491 info->negotiate_done =
492 process_negotiation_response(response, wc->byte_len);
493 complete(&info->negotiate_completion);
494 break;
495
496 /* SMBD data transfer packet */
497 case SMBD_TRANSFER_DATA:
498 data_transfer = smbd_response_payload(response);
499 data_length = le32_to_cpu(data_transfer->data_length);
500
501 /*
502 * If this is a packet with data playload place the data in
503 * reassembly queue and wake up the reading thread
504 */
505 if (data_length) {
506 if (info->full_packet_received)
507 response->first_segment = true;
508
509 if (le32_to_cpu(data_transfer->remaining_data_length))
510 info->full_packet_received = false;
511 else
512 info->full_packet_received = true;
513
514 enqueue_reassembly(
515 info,
516 response,
517 data_length);
518 } else
519 put_empty_packet(info, response);
520
521 if (data_length)
522 wake_up_interruptible(&info->wait_reassembly_queue);
523
524 atomic_dec(&info->receive_credits);
525 info->receive_credit_target =
526 le16_to_cpu(data_transfer->credits_requested);
Long Li4ebb8792020-03-26 22:33:01 -0700527 if (le16_to_cpu(data_transfer->credits_granted)) {
528 atomic_add(le16_to_cpu(data_transfer->credits_granted),
529 &info->send_credits);
530 /*
531 * We have new send credits granted from remote peer
532 * If any sender is waiting for credits, unblock it
533 */
534 wake_up_interruptible(&info->wait_send_queue);
535 }
Long Lif1981862017-11-04 18:17:24 -0700536
537 log_incoming(INFO, "data flags %d data_offset %d "
538 "data_length %d remaining_data_length %d\n",
539 le16_to_cpu(data_transfer->flags),
540 le32_to_cpu(data_transfer->data_offset),
541 le32_to_cpu(data_transfer->data_length),
542 le32_to_cpu(data_transfer->remaining_data_length));
543
544 /* Send a KEEP_ALIVE response right away if requested */
545 info->keep_alive_requested = KEEP_ALIVE_NONE;
546 if (le16_to_cpu(data_transfer->flags) &
547 SMB_DIRECT_RESPONSE_REQUESTED) {
548 info->keep_alive_requested = KEEP_ALIVE_PENDING;
549 }
550
Long Li4ebb8792020-03-26 22:33:01 -0700551 /*
552 * Check if we need to send something to remote peer to
553 * grant more credits or respond to KEEP_ALIVE packet
554 */
555 check_and_send_immediate(info);
556
Long Lif1981862017-11-04 18:17:24 -0700557 return;
558
559 default:
560 log_rdma_recv(ERR,
561 "unexpected response type=%d\n", response->type);
562 }
563
564error:
565 put_receive_buffer(info, response);
566}
567
568static struct rdma_cm_id *smbd_create_id(
569 struct smbd_connection *info,
570 struct sockaddr *dstaddr, int port)
571{
572 struct rdma_cm_id *id;
573 int rc;
574 __be16 *sport;
575
576 id = rdma_create_id(&init_net, smbd_conn_upcall, info,
577 RDMA_PS_TCP, IB_QPT_RC);
578 if (IS_ERR(id)) {
579 rc = PTR_ERR(id);
580 log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
581 return id;
582 }
583
584 if (dstaddr->sa_family == AF_INET6)
585 sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
586 else
587 sport = &((struct sockaddr_in *)dstaddr)->sin_port;
588
589 *sport = htons(port);
590
591 init_completion(&info->ri_done);
592 info->ri_rc = -ETIMEDOUT;
593
594 rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
595 RDMA_RESOLVE_TIMEOUT);
596 if (rc) {
597 log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
598 goto out;
599 }
600 wait_for_completion_interruptible_timeout(
601 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
602 rc = info->ri_rc;
603 if (rc) {
604 log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
605 goto out;
606 }
607
608 info->ri_rc = -ETIMEDOUT;
609 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
610 if (rc) {
611 log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
612 goto out;
613 }
614 wait_for_completion_interruptible_timeout(
615 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
616 rc = info->ri_rc;
617 if (rc) {
618 log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
619 goto out;
620 }
621
622 return id;
623
624out:
625 rdma_destroy_id(id);
626 return ERR_PTR(rc);
627}
628
629/*
630 * Test if FRWR (Fast Registration Work Requests) is supported on the device
631 * This implementation requries FRWR on RDMA read/write
632 * return value: true if it is supported
633 */
634static bool frwr_is_supported(struct ib_device_attr *attrs)
635{
636 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
637 return false;
638 if (attrs->max_fast_reg_page_list_len == 0)
639 return false;
640 return true;
641}
642
643static int smbd_ia_open(
644 struct smbd_connection *info,
645 struct sockaddr *dstaddr, int port)
646{
647 int rc;
648
649 info->id = smbd_create_id(info, dstaddr, port);
650 if (IS_ERR(info->id)) {
651 rc = PTR_ERR(info->id);
652 goto out1;
653 }
654
655 if (!frwr_is_supported(&info->id->device->attrs)) {
656 log_rdma_event(ERR,
657 "Fast Registration Work Requests "
658 "(FRWR) is not supported\n");
659 log_rdma_event(ERR,
660 "Device capability flags = %llx "
661 "max_fast_reg_page_list_len = %u\n",
662 info->id->device->attrs.device_cap_flags,
663 info->id->device->attrs.max_fast_reg_page_list_len);
664 rc = -EPROTONOSUPPORT;
665 goto out2;
666 }
Long Lic7398582017-11-22 17:38:44 -0700667 info->max_frmr_depth = min_t(int,
668 smbd_max_frmr_depth,
669 info->id->device->attrs.max_fast_reg_page_list_len);
670 info->mr_type = IB_MR_TYPE_MEM_REG;
671 if (info->id->device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
672 info->mr_type = IB_MR_TYPE_SG_GAPS;
Long Lif1981862017-11-04 18:17:24 -0700673
674 info->pd = ib_alloc_pd(info->id->device, 0);
675 if (IS_ERR(info->pd)) {
676 rc = PTR_ERR(info->pd);
677 log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
678 goto out2;
679 }
680
681 return 0;
682
683out2:
684 rdma_destroy_id(info->id);
685 info->id = NULL;
686
687out1:
688 return rc;
689}
690
691/*
692 * Send a negotiation request message to the peer
693 * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
694 * After negotiation, the transport is connected and ready for
695 * carrying upper layer SMB payload
696 */
697static int smbd_post_send_negotiate_req(struct smbd_connection *info)
698{
Bart Van Assche73930592018-07-18 09:25:25 -0700699 struct ib_send_wr send_wr;
Long Lif1981862017-11-04 18:17:24 -0700700 int rc = -ENOMEM;
701 struct smbd_request *request;
702 struct smbd_negotiate_req *packet;
703
704 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
705 if (!request)
706 return rc;
707
708 request->info = info;
709
710 packet = smbd_request_payload(request);
711 packet->min_version = cpu_to_le16(SMBD_V1);
712 packet->max_version = cpu_to_le16(SMBD_V1);
713 packet->reserved = 0;
714 packet->credits_requested = cpu_to_le16(info->send_credit_target);
715 packet->preferred_send_size = cpu_to_le32(info->max_send_size);
716 packet->max_receive_size = cpu_to_le32(info->max_receive_size);
717 packet->max_fragmented_size =
718 cpu_to_le32(info->max_fragmented_recv_size);
719
720 request->num_sge = 1;
721 request->sge[0].addr = ib_dma_map_single(
722 info->id->device, (void *)packet,
723 sizeof(*packet), DMA_TO_DEVICE);
724 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
725 rc = -EIO;
726 goto dma_mapping_failed;
727 }
728
729 request->sge[0].length = sizeof(*packet);
730 request->sge[0].lkey = info->pd->local_dma_lkey;
731
732 ib_dma_sync_single_for_device(
733 info->id->device, request->sge[0].addr,
734 request->sge[0].length, DMA_TO_DEVICE);
735
736 request->cqe.done = send_done;
737
738 send_wr.next = NULL;
739 send_wr.wr_cqe = &request->cqe;
740 send_wr.sg_list = request->sge;
741 send_wr.num_sge = request->num_sge;
742 send_wr.opcode = IB_WR_SEND;
743 send_wr.send_flags = IB_SEND_SIGNALED;
744
745 log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
746 request->sge[0].addr,
747 request->sge[0].length, request->sge[0].lkey);
748
Long Lif1981862017-11-04 18:17:24 -0700749 atomic_inc(&info->send_pending);
Bart Van Assche73930592018-07-18 09:25:25 -0700750 rc = ib_post_send(info->id->qp, &send_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -0700751 if (!rc)
752 return 0;
753
754 /* if we reach here, post send failed */
755 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
756 atomic_dec(&info->send_pending);
757 ib_dma_unmap_single(info->id->device, request->sge[0].addr,
758 request->sge[0].length, DMA_TO_DEVICE);
759
Long Li21a4e142018-03-30 15:16:36 -0700760 smbd_disconnect_rdma_connection(info);
761
Long Lif1981862017-11-04 18:17:24 -0700762dma_mapping_failed:
763 mempool_free(request, info->request_mempool);
764 return rc;
765}
766
767/*
768 * Extend the credits to remote peer
769 * This implements [MS-SMBD] 3.1.5.9
770 * The idea is that we should extend credits to remote peer as quickly as
771 * it's allowed, to maintain data flow. We allocate as much receive
772 * buffer as possible, and extend the receive credits to remote peer
773 * return value: the new credtis being granted.
774 */
775static int manage_credits_prior_sending(struct smbd_connection *info)
776{
777 int new_credits;
778
779 spin_lock(&info->lock_new_credits_offered);
780 new_credits = info->new_credits_offered;
781 info->new_credits_offered = 0;
782 spin_unlock(&info->lock_new_credits_offered);
783
784 return new_credits;
785}
786
787/*
788 * Check if we need to send a KEEP_ALIVE message
789 * The idle connection timer triggers a KEEP_ALIVE message when expires
790 * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
791 * back a response.
792 * return value:
793 * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
794 * 0: otherwise
795 */
796static int manage_keep_alive_before_sending(struct smbd_connection *info)
797{
798 if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
799 info->keep_alive_requested = KEEP_ALIVE_SENT;
800 return 1;
801 }
802 return 0;
803}
804
805/*
806 * Build and prepare the SMBD packet header
807 * This function waits for avaialbe send credits and build a SMBD packet
808 * header. The caller then optional append payload to the packet after
809 * the header
810 * intput values
811 * size: the size of the payload
812 * remaining_data_length: remaining data to send if this is part of a
813 * fragmented packet
814 * output values
815 * request_out: the request allocated from this function
816 * return values: 0 on success, otherwise actual error code returned
817 */
818static int smbd_create_header(struct smbd_connection *info,
819 int size, int remaining_data_length,
820 struct smbd_request **request_out)
821{
822 struct smbd_request *request;
823 struct smbd_data_transfer *packet;
824 int header_length;
825 int rc;
826
827 /* Wait for send credits. A SMBD packet needs one credit */
828 rc = wait_event_interruptible(info->wait_send_queue,
829 atomic_read(&info->send_credits) > 0 ||
830 info->transport_status != SMBD_CONNECTED);
831 if (rc)
832 return rc;
833
834 if (info->transport_status != SMBD_CONNECTED) {
835 log_outgoing(ERR, "disconnected not sending\n");
Long Li62fdf672019-04-05 21:36:33 +0000836 return -EAGAIN;
Long Lif1981862017-11-04 18:17:24 -0700837 }
838 atomic_dec(&info->send_credits);
839
840 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
841 if (!request) {
842 rc = -ENOMEM;
843 goto err;
844 }
845
846 request->info = info;
847
848 /* Fill in the packet header */
849 packet = smbd_request_payload(request);
850 packet->credits_requested = cpu_to_le16(info->send_credit_target);
851 packet->credits_granted =
852 cpu_to_le16(manage_credits_prior_sending(info));
853 info->send_immediate = false;
854
855 packet->flags = 0;
856 if (manage_keep_alive_before_sending(info))
857 packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
858
859 packet->reserved = 0;
860 if (!size)
861 packet->data_offset = 0;
862 else
863 packet->data_offset = cpu_to_le32(24);
864 packet->data_length = cpu_to_le32(size);
865 packet->remaining_data_length = cpu_to_le32(remaining_data_length);
866 packet->padding = 0;
867
868 log_outgoing(INFO, "credits_requested=%d credits_granted=%d "
869 "data_offset=%d data_length=%d remaining_data_length=%d\n",
870 le16_to_cpu(packet->credits_requested),
871 le16_to_cpu(packet->credits_granted),
872 le32_to_cpu(packet->data_offset),
873 le32_to_cpu(packet->data_length),
874 le32_to_cpu(packet->remaining_data_length));
875
876 /* Map the packet to DMA */
877 header_length = sizeof(struct smbd_data_transfer);
878 /* If this is a packet without payload, don't send padding */
879 if (!size)
880 header_length = offsetof(struct smbd_data_transfer, padding);
881
882 request->num_sge = 1;
883 request->sge[0].addr = ib_dma_map_single(info->id->device,
884 (void *)packet,
885 header_length,
Long Li7f46d232019-05-13 21:01:29 -0700886 DMA_TO_DEVICE);
Long Lif1981862017-11-04 18:17:24 -0700887 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
888 mempool_free(request, info->request_mempool);
889 rc = -EIO;
890 goto err;
891 }
892
893 request->sge[0].length = header_length;
894 request->sge[0].lkey = info->pd->local_dma_lkey;
895
896 *request_out = request;
897 return 0;
898
899err:
900 atomic_inc(&info->send_credits);
901 return rc;
902}
903
904static void smbd_destroy_header(struct smbd_connection *info,
905 struct smbd_request *request)
906{
907
908 ib_dma_unmap_single(info->id->device,
909 request->sge[0].addr,
910 request->sge[0].length,
911 DMA_TO_DEVICE);
912 mempool_free(request, info->request_mempool);
913 atomic_inc(&info->send_credits);
914}
915
916/* Post the send request */
917static int smbd_post_send(struct smbd_connection *info,
Long Li072a14e2020-03-30 11:04:06 -0700918 struct smbd_request *request)
Long Lif1981862017-11-04 18:17:24 -0700919{
Bart Van Assche73930592018-07-18 09:25:25 -0700920 struct ib_send_wr send_wr;
Long Lif1981862017-11-04 18:17:24 -0700921 int rc, i;
922
923 for (i = 0; i < request->num_sge; i++) {
924 log_rdma_send(INFO,
Colin Ian Kingac65cb62018-02-09 12:14:15 +0000925 "rdma_request sge[%d] addr=%llu length=%u\n",
Long Liff30b892018-04-17 12:17:10 -0700926 i, request->sge[i].addr, request->sge[i].length);
Long Lif1981862017-11-04 18:17:24 -0700927 ib_dma_sync_single_for_device(
928 info->id->device,
929 request->sge[i].addr,
930 request->sge[i].length,
931 DMA_TO_DEVICE);
932 }
933
934 request->cqe.done = send_done;
935
936 send_wr.next = NULL;
937 send_wr.wr_cqe = &request->cqe;
938 send_wr.sg_list = request->sge;
939 send_wr.num_sge = request->num_sge;
940 send_wr.opcode = IB_WR_SEND;
941 send_wr.send_flags = IB_SEND_SIGNALED;
942
Long Li3ffbe782020-03-30 11:04:07 -0700943wait_sq:
944 wait_event(info->wait_post_send,
945 atomic_read(&info->send_pending) < info->send_credit_target);
946 if (unlikely(atomic_inc_return(&info->send_pending) >
947 info->send_credit_target)) {
948 atomic_dec(&info->send_pending);
949 goto wait_sq;
950 }
Long Lif1981862017-11-04 18:17:24 -0700951
Bart Van Assche73930592018-07-18 09:25:25 -0700952 rc = ib_post_send(info->id->qp, &send_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -0700953 if (rc) {
954 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
Long Li072a14e2020-03-30 11:04:06 -0700955 if (atomic_dec_and_test(&info->send_pending))
956 wake_up(&info->wait_send_pending);
Long Li21a4e142018-03-30 15:16:36 -0700957 smbd_disconnect_rdma_connection(info);
Long Li62fdf672019-04-05 21:36:33 +0000958 rc = -EAGAIN;
Long Lif1981862017-11-04 18:17:24 -0700959 } else
960 /* Reset timer for idle connection after packet is sent */
961 mod_delayed_work(info->workqueue, &info->idle_timer_work,
962 info->keep_alive_interval*HZ);
963
964 return rc;
965}
966
967static int smbd_post_send_sgl(struct smbd_connection *info,
968 struct scatterlist *sgl, int data_length, int remaining_data_length)
969{
970 int num_sgs;
971 int i, rc;
972 struct smbd_request *request;
973 struct scatterlist *sg;
974
975 rc = smbd_create_header(
976 info, data_length, remaining_data_length, &request);
977 if (rc)
978 return rc;
979
980 num_sgs = sgl ? sg_nents(sgl) : 0;
981 for_each_sg(sgl, sg, num_sgs, i) {
982 request->sge[i+1].addr =
983 ib_dma_map_page(info->id->device, sg_page(sg),
Long Li7f46d232019-05-13 21:01:29 -0700984 sg->offset, sg->length, DMA_TO_DEVICE);
Long Lif1981862017-11-04 18:17:24 -0700985 if (ib_dma_mapping_error(
986 info->id->device, request->sge[i+1].addr)) {
987 rc = -EIO;
988 request->sge[i+1].addr = 0;
989 goto dma_mapping_failure;
990 }
991 request->sge[i+1].length = sg->length;
992 request->sge[i+1].lkey = info->pd->local_dma_lkey;
993 request->num_sge++;
994 }
995
Long Li072a14e2020-03-30 11:04:06 -0700996 rc = smbd_post_send(info, request);
Long Lif1981862017-11-04 18:17:24 -0700997 if (!rc)
998 return 0;
999
1000dma_mapping_failure:
1001 for (i = 1; i < request->num_sge; i++)
1002 if (request->sge[i].addr)
1003 ib_dma_unmap_single(info->id->device,
1004 request->sge[i].addr,
1005 request->sge[i].length,
1006 DMA_TO_DEVICE);
1007 smbd_destroy_header(info, request);
1008 return rc;
1009}
1010
1011/*
Long Lid649e1b2017-11-22 17:38:42 -07001012 * Send a page
1013 * page: the page to send
1014 * offset: offset in the page to send
1015 * size: length in the page to send
1016 * remaining_data_length: remaining data to send in this payload
1017 */
1018static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
1019 unsigned long offset, size_t size, int remaining_data_length)
1020{
1021 struct scatterlist sgl;
1022
1023 sg_init_table(&sgl, 1);
1024 sg_set_page(&sgl, page, size, offset);
1025
1026 return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
1027}
1028
1029/*
Long Lif1981862017-11-04 18:17:24 -07001030 * Send an empty message
1031 * Empty message is used to extend credits to peer to for keep live
1032 * while there is no upper layer payload to send at the time
1033 */
1034static int smbd_post_send_empty(struct smbd_connection *info)
1035{
1036 info->count_send_empty++;
1037 return smbd_post_send_sgl(info, NULL, 0, 0);
1038}
1039
1040/*
Long Lid649e1b2017-11-22 17:38:42 -07001041 * Send a data buffer
1042 * iov: the iov array describing the data buffers
1043 * n_vec: number of iov array
1044 * remaining_data_length: remaining data to send following this packet
1045 * in segmented SMBD packet
1046 */
1047static int smbd_post_send_data(
1048 struct smbd_connection *info, struct kvec *iov, int n_vec,
1049 int remaining_data_length)
1050{
1051 int i;
1052 u32 data_length = 0;
1053 struct scatterlist sgl[SMBDIRECT_MAX_SGE];
1054
1055 if (n_vec > SMBDIRECT_MAX_SGE) {
1056 cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
Long Li37941ea2019-10-16 13:51:52 -07001057 return -EINVAL;
Long Lid649e1b2017-11-22 17:38:42 -07001058 }
1059
1060 sg_init_table(sgl, n_vec);
1061 for (i = 0; i < n_vec; i++) {
1062 data_length += iov[i].iov_len;
1063 sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
1064 }
1065
1066 return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
1067}
1068
1069/*
Long Lif1981862017-11-04 18:17:24 -07001070 * Post a receive request to the transport
1071 * The remote peer can only send data when a receive request is posted
1072 * The interaction is controlled by send/receive credit system
1073 */
1074static int smbd_post_recv(
1075 struct smbd_connection *info, struct smbd_response *response)
1076{
Bart Van Assche73930592018-07-18 09:25:25 -07001077 struct ib_recv_wr recv_wr;
Long Lif1981862017-11-04 18:17:24 -07001078 int rc = -EIO;
1079
1080 response->sge.addr = ib_dma_map_single(
1081 info->id->device, response->packet,
1082 info->max_receive_size, DMA_FROM_DEVICE);
1083 if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1084 return rc;
1085
1086 response->sge.length = info->max_receive_size;
1087 response->sge.lkey = info->pd->local_dma_lkey;
1088
1089 response->cqe.done = recv_done;
1090
1091 recv_wr.wr_cqe = &response->cqe;
1092 recv_wr.next = NULL;
1093 recv_wr.sg_list = &response->sge;
1094 recv_wr.num_sge = 1;
1095
Bart Van Assche73930592018-07-18 09:25:25 -07001096 rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -07001097 if (rc) {
1098 ib_dma_unmap_single(info->id->device, response->sge.addr,
1099 response->sge.length, DMA_FROM_DEVICE);
Long Li21a4e142018-03-30 15:16:36 -07001100 smbd_disconnect_rdma_connection(info);
Long Lif1981862017-11-04 18:17:24 -07001101 log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1102 }
1103
1104 return rc;
1105}
1106
1107/* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1108static int smbd_negotiate(struct smbd_connection *info)
1109{
1110 int rc;
1111 struct smbd_response *response = get_receive_buffer(info);
1112
1113 response->type = SMBD_NEGOTIATE_RESP;
1114 rc = smbd_post_recv(info, response);
1115 log_rdma_event(INFO,
1116 "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x "
1117 "iov.lkey=%x\n",
1118 rc, response->sge.addr,
1119 response->sge.length, response->sge.lkey);
1120 if (rc)
1121 return rc;
1122
1123 init_completion(&info->negotiate_completion);
1124 info->negotiate_done = false;
1125 rc = smbd_post_send_negotiate_req(info);
1126 if (rc)
1127 return rc;
1128
1129 rc = wait_for_completion_interruptible_timeout(
1130 &info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1131 log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1132
1133 if (info->negotiate_done)
1134 return 0;
1135
1136 if (rc == 0)
1137 rc = -ETIMEDOUT;
1138 else if (rc == -ERESTARTSYS)
1139 rc = -EINTR;
1140 else
1141 rc = -ENOTCONN;
1142
1143 return rc;
1144}
1145
1146static void put_empty_packet(
1147 struct smbd_connection *info, struct smbd_response *response)
1148{
1149 spin_lock(&info->empty_packet_queue_lock);
1150 list_add_tail(&response->list, &info->empty_packet_queue);
1151 info->count_empty_packet_queue++;
1152 spin_unlock(&info->empty_packet_queue_lock);
1153
1154 queue_work(info->workqueue, &info->post_send_credits_work);
1155}
1156
1157/*
1158 * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1159 * This is a queue for reassembling upper layer payload and present to upper
1160 * layer. All the inncoming payload go to the reassembly queue, regardless of
1161 * if reassembly is required. The uuper layer code reads from the queue for all
1162 * incoming payloads.
1163 * Put a received packet to the reassembly queue
1164 * response: the packet received
1165 * data_length: the size of payload in this packet
1166 */
1167static void enqueue_reassembly(
1168 struct smbd_connection *info,
1169 struct smbd_response *response,
1170 int data_length)
1171{
1172 spin_lock(&info->reassembly_queue_lock);
1173 list_add_tail(&response->list, &info->reassembly_queue);
1174 info->reassembly_queue_length++;
1175 /*
1176 * Make sure reassembly_data_length is updated after list and
1177 * reassembly_queue_length are updated. On the dequeue side
1178 * reassembly_data_length is checked without a lock to determine
1179 * if reassembly_queue_length and list is up to date
1180 */
1181 virt_wmb();
1182 info->reassembly_data_length += data_length;
1183 spin_unlock(&info->reassembly_queue_lock);
1184 info->count_reassembly_queue++;
1185 info->count_enqueue_reassembly_queue++;
1186}
1187
1188/*
1189 * Get the first entry at the front of reassembly queue
1190 * Caller is responsible for locking
1191 * return value: the first entry if any, NULL if queue is empty
1192 */
1193static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1194{
1195 struct smbd_response *ret = NULL;
1196
1197 if (!list_empty(&info->reassembly_queue)) {
1198 ret = list_first_entry(
1199 &info->reassembly_queue,
1200 struct smbd_response, list);
1201 }
1202 return ret;
1203}
1204
1205static struct smbd_response *get_empty_queue_buffer(
1206 struct smbd_connection *info)
1207{
1208 struct smbd_response *ret = NULL;
1209 unsigned long flags;
1210
1211 spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1212 if (!list_empty(&info->empty_packet_queue)) {
1213 ret = list_first_entry(
1214 &info->empty_packet_queue,
1215 struct smbd_response, list);
1216 list_del(&ret->list);
1217 info->count_empty_packet_queue--;
1218 }
1219 spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1220
1221 return ret;
1222}
1223
1224/*
1225 * Get a receive buffer
1226 * For each remote send, we need to post a receive. The receive buffers are
1227 * pre-allocated in advance.
1228 * return value: the receive buffer, NULL if none is available
1229 */
1230static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1231{
1232 struct smbd_response *ret = NULL;
1233 unsigned long flags;
1234
1235 spin_lock_irqsave(&info->receive_queue_lock, flags);
1236 if (!list_empty(&info->receive_queue)) {
1237 ret = list_first_entry(
1238 &info->receive_queue,
1239 struct smbd_response, list);
1240 list_del(&ret->list);
1241 info->count_receive_queue--;
1242 info->count_get_receive_buffer++;
1243 }
1244 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1245
1246 return ret;
1247}
1248
1249/*
1250 * Return a receive buffer
1251 * Upon returning of a receive buffer, we can post new receive and extend
1252 * more receive credits to remote peer. This is done immediately after a
1253 * receive buffer is returned.
1254 */
1255static void put_receive_buffer(
1256 struct smbd_connection *info, struct smbd_response *response)
1257{
1258 unsigned long flags;
1259
1260 ib_dma_unmap_single(info->id->device, response->sge.addr,
1261 response->sge.length, DMA_FROM_DEVICE);
1262
1263 spin_lock_irqsave(&info->receive_queue_lock, flags);
1264 list_add_tail(&response->list, &info->receive_queue);
1265 info->count_receive_queue++;
1266 info->count_put_receive_buffer++;
1267 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1268
1269 queue_work(info->workqueue, &info->post_send_credits_work);
1270}
1271
1272/* Preallocate all receive buffer on transport establishment */
1273static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1274{
1275 int i;
1276 struct smbd_response *response;
1277
1278 INIT_LIST_HEAD(&info->reassembly_queue);
1279 spin_lock_init(&info->reassembly_queue_lock);
1280 info->reassembly_data_length = 0;
1281 info->reassembly_queue_length = 0;
1282
1283 INIT_LIST_HEAD(&info->receive_queue);
1284 spin_lock_init(&info->receive_queue_lock);
1285 info->count_receive_queue = 0;
1286
1287 INIT_LIST_HEAD(&info->empty_packet_queue);
1288 spin_lock_init(&info->empty_packet_queue_lock);
1289 info->count_empty_packet_queue = 0;
1290
1291 init_waitqueue_head(&info->wait_receive_queues);
1292
1293 for (i = 0; i < num_buf; i++) {
1294 response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1295 if (!response)
1296 goto allocate_failed;
1297
1298 response->info = info;
1299 list_add_tail(&response->list, &info->receive_queue);
1300 info->count_receive_queue++;
1301 }
1302
1303 return 0;
1304
1305allocate_failed:
1306 while (!list_empty(&info->receive_queue)) {
1307 response = list_first_entry(
1308 &info->receive_queue,
1309 struct smbd_response, list);
1310 list_del(&response->list);
1311 info->count_receive_queue--;
1312
1313 mempool_free(response, info->response_mempool);
1314 }
1315 return -ENOMEM;
1316}
1317
1318static void destroy_receive_buffers(struct smbd_connection *info)
1319{
1320 struct smbd_response *response;
1321
1322 while ((response = get_receive_buffer(info)))
1323 mempool_free(response, info->response_mempool);
1324
1325 while ((response = get_empty_queue_buffer(info)))
1326 mempool_free(response, info->response_mempool);
1327}
1328
1329/*
1330 * Check and send an immediate or keep alive packet
1331 * The condition to send those packets are defined in [MS-SMBD] 3.1.1.1
1332 * Connection.KeepaliveRequested and Connection.SendImmediate
1333 * The idea is to extend credits to server as soon as it becomes available
1334 */
1335static void send_immediate_work(struct work_struct *work)
1336{
1337 struct smbd_connection *info = container_of(
1338 work, struct smbd_connection,
1339 send_immediate_work.work);
1340
1341 if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
1342 info->send_immediate) {
1343 log_keep_alive(INFO, "send an empty message\n");
1344 smbd_post_send_empty(info);
1345 }
1346}
1347
1348/* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1349static void idle_connection_timer(struct work_struct *work)
1350{
1351 struct smbd_connection *info = container_of(
1352 work, struct smbd_connection,
1353 idle_timer_work.work);
1354
1355 if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1356 log_keep_alive(ERR,
1357 "error status info->keep_alive_requested=%d\n",
1358 info->keep_alive_requested);
1359 smbd_disconnect_rdma_connection(info);
1360 return;
1361 }
1362
1363 log_keep_alive(INFO, "about to send an empty idle message\n");
1364 smbd_post_send_empty(info);
1365
1366 /* Setup the next idle timeout work */
1367 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1368 info->keep_alive_interval*HZ);
1369}
1370
Long Li050b8c32019-04-04 11:35:42 -05001371/*
1372 * Destroy the transport and related RDMA and memory resources
1373 * Need to go through all the pending counters and make sure on one is using
1374 * the transport while it is destroyed
1375 */
1376void smbd_destroy(struct TCP_Server_Info *server)
Long Li8ef130f2017-11-22 17:38:37 -07001377{
Long Li050b8c32019-04-04 11:35:42 -05001378 struct smbd_connection *info = server->smbd_conn;
1379 struct smbd_response *response;
1380 unsigned long flags;
1381
1382 if (!info) {
1383 log_rdma_event(INFO, "rdma session already destroyed\n");
1384 return;
1385 }
1386
Long Li8ef130f2017-11-22 17:38:37 -07001387 log_rdma_event(INFO, "destroying rdma session\n");
Long Li050b8c32019-04-04 11:35:42 -05001388 if (info->transport_status != SMBD_DISCONNECTED) {
1389 rdma_disconnect(server->smbd_conn->id);
1390 log_rdma_event(INFO, "wait for transport being disconnected\n");
Long Lie8b3bfe2019-04-05 21:36:31 +00001391 wait_event_interruptible(
Long Li050b8c32019-04-04 11:35:42 -05001392 info->disconn_wait,
1393 info->transport_status == SMBD_DISCONNECTED);
1394 }
Long Li8ef130f2017-11-22 17:38:37 -07001395
Long Li050b8c32019-04-04 11:35:42 -05001396 log_rdma_event(INFO, "destroying qp\n");
1397 ib_drain_qp(info->id->qp);
1398 rdma_destroy_qp(info->id);
Long Li8ef130f2017-11-22 17:38:37 -07001399
Long Li050b8c32019-04-04 11:35:42 -05001400 log_rdma_event(INFO, "cancelling idle timer\n");
1401 cancel_delayed_work_sync(&info->idle_timer_work);
1402 log_rdma_event(INFO, "cancelling send immediate work\n");
1403 cancel_delayed_work_sync(&info->send_immediate_work);
1404
1405 log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
1406 wait_event(info->wait_send_pending,
1407 atomic_read(&info->send_pending) == 0);
Long Li050b8c32019-04-04 11:35:42 -05001408
1409 /* It's not posssible for upper layer to get to reassembly */
1410 log_rdma_event(INFO, "drain the reassembly queue\n");
1411 do {
1412 spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1413 response = _get_first_reassembly(info);
1414 if (response) {
1415 list_del(&response->list);
1416 spin_unlock_irqrestore(
1417 &info->reassembly_queue_lock, flags);
1418 put_receive_buffer(info, response);
1419 } else
1420 spin_unlock_irqrestore(
1421 &info->reassembly_queue_lock, flags);
1422 } while (response);
1423 info->reassembly_data_length = 0;
1424
1425 log_rdma_event(INFO, "free receive buffers\n");
1426 wait_event(info->wait_receive_queues,
1427 info->count_receive_queue + info->count_empty_packet_queue
1428 == info->receive_credit_max);
1429 destroy_receive_buffers(info);
1430
1431 /*
1432 * For performance reasons, memory registration and deregistration
1433 * are not locked by srv_mutex. It is possible some processes are
1434 * blocked on transport srv_mutex while holding memory registration.
1435 * Release the transport srv_mutex to allow them to hit the failure
1436 * path when sending data, and then release memory registartions.
1437 */
1438 log_rdma_event(INFO, "freeing mr list\n");
1439 wake_up_interruptible_all(&info->wait_mr);
1440 while (atomic_read(&info->mr_used_count)) {
1441 mutex_unlock(&server->srv_mutex);
1442 msleep(1000);
1443 mutex_lock(&server->srv_mutex);
1444 }
1445 destroy_mr_list(info);
1446
1447 ib_free_cq(info->send_cq);
1448 ib_free_cq(info->recv_cq);
1449 ib_dealloc_pd(info->pd);
1450 rdma_destroy_id(info->id);
1451
1452 /* free mempools */
1453 mempool_destroy(info->request_mempool);
1454 kmem_cache_destroy(info->request_cache);
1455
1456 mempool_destroy(info->response_mempool);
1457 kmem_cache_destroy(info->response_cache);
1458
1459 info->transport_status = SMBD_DESTROYED;
Long Li8ef130f2017-11-22 17:38:37 -07001460
1461 destroy_workqueue(info->workqueue);
Long Lid63cdba2019-10-16 13:51:53 -07001462 log_rdma_event(INFO, "rdma session destroyed\n");
Long Li8ef130f2017-11-22 17:38:37 -07001463 kfree(info);
1464}
1465
Long Liad57b8e2017-11-22 17:38:35 -07001466/*
1467 * Reconnect this SMBD connection, called from upper layer
1468 * return value: 0 on success, or actual error code
1469 */
1470int smbd_reconnect(struct TCP_Server_Info *server)
1471{
1472 log_rdma_event(INFO, "reconnecting rdma session\n");
1473
1474 if (!server->smbd_conn) {
Long Li48f238a2018-03-30 15:16:35 -07001475 log_rdma_event(INFO, "rdma session already destroyed\n");
1476 goto create_conn;
Long Liad57b8e2017-11-22 17:38:35 -07001477 }
1478
1479 /*
1480 * This is possible if transport is disconnected and we haven't received
1481 * notification from RDMA, but upper layer has detected timeout
1482 */
1483 if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1484 log_rdma_event(INFO, "disconnecting transport\n");
Long Li050b8c32019-04-04 11:35:42 -05001485 smbd_destroy(server);
Long Liad57b8e2017-11-22 17:38:35 -07001486 }
1487
Long Li48f238a2018-03-30 15:16:35 -07001488create_conn:
Long Liad57b8e2017-11-22 17:38:35 -07001489 log_rdma_event(INFO, "creating rdma session\n");
1490 server->smbd_conn = smbd_get_connection(
1491 server, (struct sockaddr *) &server->dstaddr);
Long Lid63cdba2019-10-16 13:51:53 -07001492
1493 if (server->smbd_conn)
1494 cifs_dbg(VFS, "RDMA transport re-established\n");
Long Liad57b8e2017-11-22 17:38:35 -07001495
1496 return server->smbd_conn ? 0 : -ENOENT;
1497}
1498
Long Lif1981862017-11-04 18:17:24 -07001499static void destroy_caches_and_workqueue(struct smbd_connection *info)
1500{
1501 destroy_receive_buffers(info);
1502 destroy_workqueue(info->workqueue);
1503 mempool_destroy(info->response_mempool);
1504 kmem_cache_destroy(info->response_cache);
1505 mempool_destroy(info->request_mempool);
1506 kmem_cache_destroy(info->request_cache);
1507}
1508
1509#define MAX_NAME_LEN 80
1510static int allocate_caches_and_workqueue(struct smbd_connection *info)
1511{
1512 char name[MAX_NAME_LEN];
1513 int rc;
1514
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001515 scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001516 info->request_cache =
1517 kmem_cache_create(
1518 name,
1519 sizeof(struct smbd_request) +
1520 sizeof(struct smbd_data_transfer),
1521 0, SLAB_HWCACHE_ALIGN, NULL);
1522 if (!info->request_cache)
1523 return -ENOMEM;
1524
1525 info->request_mempool =
1526 mempool_create(info->send_credit_target, mempool_alloc_slab,
1527 mempool_free_slab, info->request_cache);
1528 if (!info->request_mempool)
1529 goto out1;
1530
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001531 scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001532 info->response_cache =
1533 kmem_cache_create(
1534 name,
1535 sizeof(struct smbd_response) +
1536 info->max_receive_size,
1537 0, SLAB_HWCACHE_ALIGN, NULL);
1538 if (!info->response_cache)
1539 goto out2;
1540
1541 info->response_mempool =
1542 mempool_create(info->receive_credit_max, mempool_alloc_slab,
1543 mempool_free_slab, info->response_cache);
1544 if (!info->response_mempool)
1545 goto out3;
1546
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001547 scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001548 info->workqueue = create_workqueue(name);
1549 if (!info->workqueue)
1550 goto out4;
1551
1552 rc = allocate_receive_buffers(info, info->receive_credit_max);
1553 if (rc) {
1554 log_rdma_event(ERR, "failed to allocate receive buffers\n");
1555 goto out5;
1556 }
1557
1558 return 0;
1559
1560out5:
1561 destroy_workqueue(info->workqueue);
1562out4:
1563 mempool_destroy(info->response_mempool);
1564out3:
1565 kmem_cache_destroy(info->response_cache);
1566out2:
1567 mempool_destroy(info->request_mempool);
1568out1:
1569 kmem_cache_destroy(info->request_cache);
1570 return -ENOMEM;
1571}
1572
1573/* Create a SMBD connection, called by upper layer */
kbuild test robot90844322017-12-18 21:30:06 +08001574static struct smbd_connection *_smbd_get_connection(
Long Lif1981862017-11-04 18:17:24 -07001575 struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1576{
1577 int rc;
1578 struct smbd_connection *info;
1579 struct rdma_conn_param conn_param;
1580 struct ib_qp_init_attr qp_attr;
1581 struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
Long Lic7398582017-11-22 17:38:44 -07001582 struct ib_port_immutable port_immutable;
1583 u32 ird_ord_hdr[2];
Long Lif1981862017-11-04 18:17:24 -07001584
1585 info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1586 if (!info)
1587 return NULL;
1588
1589 info->transport_status = SMBD_CONNECTING;
1590 rc = smbd_ia_open(info, dstaddr, port);
1591 if (rc) {
1592 log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1593 goto create_id_failed;
1594 }
1595
1596 if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1597 smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1598 log_rdma_event(ERR,
1599 "consider lowering send_credit_target = %d. "
1600 "Possible CQE overrun, device "
1601 "reporting max_cpe %d max_qp_wr %d\n",
1602 smbd_send_credit_target,
1603 info->id->device->attrs.max_cqe,
1604 info->id->device->attrs.max_qp_wr);
1605 goto config_failed;
1606 }
1607
1608 if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1609 smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1610 log_rdma_event(ERR,
1611 "consider lowering receive_credit_max = %d. "
1612 "Possible CQE overrun, device "
1613 "reporting max_cpe %d max_qp_wr %d\n",
1614 smbd_receive_credit_max,
1615 info->id->device->attrs.max_cqe,
1616 info->id->device->attrs.max_qp_wr);
1617 goto config_failed;
1618 }
1619
1620 info->receive_credit_max = smbd_receive_credit_max;
1621 info->send_credit_target = smbd_send_credit_target;
1622 info->max_send_size = smbd_max_send_size;
1623 info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1624 info->max_receive_size = smbd_max_receive_size;
1625 info->keep_alive_interval = smbd_keep_alive_interval;
1626
Steve Wise33023fb2018-06-18 08:05:26 -07001627 if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
1628 log_rdma_event(ERR,
1629 "warning: device max_send_sge = %d too small\n",
1630 info->id->device->attrs.max_send_sge);
1631 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1632 }
1633 if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
1634 log_rdma_event(ERR,
1635 "warning: device max_recv_sge = %d too small\n",
1636 info->id->device->attrs.max_recv_sge);
Long Lif1981862017-11-04 18:17:24 -07001637 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1638 }
1639
1640 info->send_cq = NULL;
1641 info->recv_cq = NULL;
Chuck Lever20cf4e02019-07-29 13:22:09 -04001642 info->send_cq =
1643 ib_alloc_cq_any(info->id->device, info,
1644 info->send_credit_target, IB_POLL_SOFTIRQ);
Long Lif1981862017-11-04 18:17:24 -07001645 if (IS_ERR(info->send_cq)) {
1646 info->send_cq = NULL;
1647 goto alloc_cq_failed;
1648 }
1649
Chuck Lever20cf4e02019-07-29 13:22:09 -04001650 info->recv_cq =
1651 ib_alloc_cq_any(info->id->device, info,
1652 info->receive_credit_max, IB_POLL_SOFTIRQ);
Long Lif1981862017-11-04 18:17:24 -07001653 if (IS_ERR(info->recv_cq)) {
1654 info->recv_cq = NULL;
1655 goto alloc_cq_failed;
1656 }
1657
1658 memset(&qp_attr, 0, sizeof(qp_attr));
1659 qp_attr.event_handler = smbd_qp_async_error_upcall;
1660 qp_attr.qp_context = info;
1661 qp_attr.cap.max_send_wr = info->send_credit_target;
1662 qp_attr.cap.max_recv_wr = info->receive_credit_max;
1663 qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
1664 qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
1665 qp_attr.cap.max_inline_data = 0;
1666 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1667 qp_attr.qp_type = IB_QPT_RC;
1668 qp_attr.send_cq = info->send_cq;
1669 qp_attr.recv_cq = info->recv_cq;
1670 qp_attr.port_num = ~0;
1671
1672 rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1673 if (rc) {
1674 log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1675 goto create_qp_failed;
1676 }
1677
1678 memset(&conn_param, 0, sizeof(conn_param));
1679 conn_param.initiator_depth = 0;
1680
Long Lic7398582017-11-22 17:38:44 -07001681 conn_param.responder_resources =
1682 info->id->device->attrs.max_qp_rd_atom
1683 < SMBD_CM_RESPONDER_RESOURCES ?
1684 info->id->device->attrs.max_qp_rd_atom :
1685 SMBD_CM_RESPONDER_RESOURCES;
1686 info->responder_resources = conn_param.responder_resources;
1687 log_rdma_mr(INFO, "responder_resources=%d\n",
1688 info->responder_resources);
1689
1690 /* Need to send IRD/ORD in private data for iWARP */
Kamal Heib3023a1e2018-12-10 21:09:48 +02001691 info->id->device->ops.get_port_immutable(
Long Lic7398582017-11-22 17:38:44 -07001692 info->id->device, info->id->port_num, &port_immutable);
1693 if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1694 ird_ord_hdr[0] = info->responder_resources;
1695 ird_ord_hdr[1] = 1;
1696 conn_param.private_data = ird_ord_hdr;
1697 conn_param.private_data_len = sizeof(ird_ord_hdr);
1698 } else {
1699 conn_param.private_data = NULL;
1700 conn_param.private_data_len = 0;
1701 }
1702
Long Lif1981862017-11-04 18:17:24 -07001703 conn_param.retry_count = SMBD_CM_RETRY;
1704 conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1705 conn_param.flow_control = 0;
Long Lif1981862017-11-04 18:17:24 -07001706
1707 log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1708 &addr_in->sin_addr, port);
1709
1710 init_waitqueue_head(&info->conn_wait);
Long Li050b8c32019-04-04 11:35:42 -05001711 init_waitqueue_head(&info->disconn_wait);
1712 init_waitqueue_head(&info->wait_reassembly_queue);
Long Lif1981862017-11-04 18:17:24 -07001713 rc = rdma_connect(info->id, &conn_param);
1714 if (rc) {
1715 log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1716 goto rdma_connect_failed;
1717 }
1718
1719 wait_event_interruptible(
1720 info->conn_wait, info->transport_status != SMBD_CONNECTING);
1721
1722 if (info->transport_status != SMBD_CONNECTED) {
1723 log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1724 goto rdma_connect_failed;
1725 }
1726
1727 log_rdma_event(INFO, "rdma_connect connected\n");
1728
1729 rc = allocate_caches_and_workqueue(info);
1730 if (rc) {
1731 log_rdma_event(ERR, "cache allocation failed\n");
1732 goto allocate_cache_failed;
1733 }
1734
1735 init_waitqueue_head(&info->wait_send_queue);
Long Lif1981862017-11-04 18:17:24 -07001736 INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
1737 INIT_DELAYED_WORK(&info->send_immediate_work, send_immediate_work);
1738 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1739 info->keep_alive_interval*HZ);
1740
1741 init_waitqueue_head(&info->wait_send_pending);
1742 atomic_set(&info->send_pending, 0);
1743
Long Li3ffbe782020-03-30 11:04:07 -07001744 init_waitqueue_head(&info->wait_post_send);
Long Lif1981862017-11-04 18:17:24 -07001745
1746 INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
Long Lif1981862017-11-04 18:17:24 -07001747 INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1748 info->new_credits_offered = 0;
1749 spin_lock_init(&info->lock_new_credits_offered);
1750
1751 rc = smbd_negotiate(info);
1752 if (rc) {
1753 log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1754 goto negotiation_failed;
1755 }
1756
Long Lic7398582017-11-22 17:38:44 -07001757 rc = allocate_mr_list(info);
1758 if (rc) {
1759 log_rdma_mr(ERR, "memory registration allocation failed\n");
1760 goto allocate_mr_failed;
1761 }
1762
Long Lif1981862017-11-04 18:17:24 -07001763 return info;
1764
Long Lic7398582017-11-22 17:38:44 -07001765allocate_mr_failed:
1766 /* At this point, need to a full transport shutdown */
Long Li050b8c32019-04-04 11:35:42 -05001767 smbd_destroy(server);
Long Lic7398582017-11-22 17:38:44 -07001768 return NULL;
1769
Long Lif1981862017-11-04 18:17:24 -07001770negotiation_failed:
1771 cancel_delayed_work_sync(&info->idle_timer_work);
1772 destroy_caches_and_workqueue(info);
1773 info->transport_status = SMBD_NEGOTIATE_FAILED;
1774 init_waitqueue_head(&info->conn_wait);
1775 rdma_disconnect(info->id);
1776 wait_event(info->conn_wait,
1777 info->transport_status == SMBD_DISCONNECTED);
1778
1779allocate_cache_failed:
1780rdma_connect_failed:
1781 rdma_destroy_qp(info->id);
1782
1783create_qp_failed:
1784alloc_cq_failed:
1785 if (info->send_cq)
1786 ib_free_cq(info->send_cq);
1787 if (info->recv_cq)
1788 ib_free_cq(info->recv_cq);
1789
1790config_failed:
1791 ib_dealloc_pd(info->pd);
1792 rdma_destroy_id(info->id);
1793
1794create_id_failed:
1795 kfree(info);
1796 return NULL;
1797}
Long Li399f9532017-11-17 17:26:52 -08001798
1799struct smbd_connection *smbd_get_connection(
1800 struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1801{
1802 struct smbd_connection *ret;
1803 int port = SMBD_PORT;
1804
1805try_again:
1806 ret = _smbd_get_connection(server, dstaddr, port);
1807
1808 /* Try SMB_PORT if SMBD_PORT doesn't work */
1809 if (!ret && port == SMBD_PORT) {
1810 port = SMB_PORT;
1811 goto try_again;
1812 }
1813 return ret;
1814}
Long Lif64b78f2017-11-22 17:38:40 -07001815
1816/*
1817 * Receive data from receive reassembly queue
1818 * All the incoming data packets are placed in reassembly queue
1819 * buf: the buffer to read data into
1820 * size: the length of data to read
1821 * return value: actual data read
1822 * Note: this implementation copies the data from reassebmly queue to receive
1823 * buffers used by upper layer. This is not the optimal code path. A better way
1824 * to do it is to not have upper layer allocate its receive buffers but rather
1825 * borrow the buffer from reassembly queue, and return it after data is
1826 * consumed. But this will require more changes to upper layer code, and also
1827 * need to consider packet boundaries while they still being reassembled.
1828 */
Steve French2026b062018-01-24 23:07:41 -06001829static int smbd_recv_buf(struct smbd_connection *info, char *buf,
1830 unsigned int size)
Long Lif64b78f2017-11-22 17:38:40 -07001831{
1832 struct smbd_response *response;
1833 struct smbd_data_transfer *data_transfer;
1834 int to_copy, to_read, data_read, offset;
1835 u32 data_length, remaining_data_length, data_offset;
1836 int rc;
Long Lif64b78f2017-11-22 17:38:40 -07001837
1838again:
Long Lif64b78f2017-11-22 17:38:40 -07001839 /*
1840 * No need to hold the reassembly queue lock all the time as we are
1841 * the only one reading from the front of the queue. The transport
1842 * may add more entries to the back of the queue at the same time
1843 */
1844 log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1845 info->reassembly_data_length);
1846 if (info->reassembly_data_length >= size) {
1847 int queue_length;
1848 int queue_removed = 0;
1849
1850 /*
1851 * Need to make sure reassembly_data_length is read before
1852 * reading reassembly_queue_length and calling
1853 * _get_first_reassembly. This call is lock free
1854 * as we never read at the end of the queue which are being
1855 * updated in SOFTIRQ as more data is received
1856 */
1857 virt_rmb();
1858 queue_length = info->reassembly_queue_length;
1859 data_read = 0;
1860 to_read = size;
1861 offset = info->first_entry_offset;
1862 while (data_read < size) {
1863 response = _get_first_reassembly(info);
1864 data_transfer = smbd_response_payload(response);
1865 data_length = le32_to_cpu(data_transfer->data_length);
1866 remaining_data_length =
1867 le32_to_cpu(
1868 data_transfer->remaining_data_length);
1869 data_offset = le32_to_cpu(data_transfer->data_offset);
1870
1871 /*
1872 * The upper layer expects RFC1002 length at the
1873 * beginning of the payload. Return it to indicate
1874 * the total length of the packet. This minimize the
1875 * change to upper layer packet processing logic. This
1876 * will be eventually remove when an intermediate
1877 * transport layer is added
1878 */
1879 if (response->first_segment && size == 4) {
1880 unsigned int rfc1002_len =
1881 data_length + remaining_data_length;
1882 *((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1883 data_read = 4;
1884 response->first_segment = false;
1885 log_read(INFO, "returning rfc1002 length %d\n",
1886 rfc1002_len);
1887 goto read_rfc1002_done;
1888 }
1889
1890 to_copy = min_t(int, data_length - offset, to_read);
1891 memcpy(
1892 buf + data_read,
1893 (char *)data_transfer + data_offset + offset,
1894 to_copy);
1895
1896 /* move on to the next buffer? */
1897 if (to_copy == data_length - offset) {
1898 queue_length--;
1899 /*
1900 * No need to lock if we are not at the
1901 * end of the queue
1902 */
Steve Frenchf9de1512018-02-03 19:45:07 -06001903 if (queue_length)
1904 list_del(&response->list);
1905 else {
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001906 spin_lock_irq(
1907 &info->reassembly_queue_lock);
Steve Frenchf9de1512018-02-03 19:45:07 -06001908 list_del(&response->list);
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001909 spin_unlock_irq(
1910 &info->reassembly_queue_lock);
Steve Frenchf9de1512018-02-03 19:45:07 -06001911 }
1912 queue_removed++;
Long Lif64b78f2017-11-22 17:38:40 -07001913 info->count_reassembly_queue--;
1914 info->count_dequeue_reassembly_queue++;
1915 put_receive_buffer(info, response);
1916 offset = 0;
1917 log_read(INFO, "put_receive_buffer offset=0\n");
1918 } else
1919 offset += to_copy;
1920
1921 to_read -= to_copy;
1922 data_read += to_copy;
1923
1924 log_read(INFO, "_get_first_reassembly memcpy %d bytes "
1925 "data_transfer_length-offset=%d after that "
1926 "to_read=%d data_read=%d offset=%d\n",
1927 to_copy, data_length - offset,
1928 to_read, data_read, offset);
1929 }
1930
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001931 spin_lock_irq(&info->reassembly_queue_lock);
Long Lif64b78f2017-11-22 17:38:40 -07001932 info->reassembly_data_length -= data_read;
1933 info->reassembly_queue_length -= queue_removed;
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001934 spin_unlock_irq(&info->reassembly_queue_lock);
Long Lif64b78f2017-11-22 17:38:40 -07001935
1936 info->first_entry_offset = offset;
1937 log_read(INFO, "returning to thread data_read=%d "
1938 "reassembly_data_length=%d first_entry_offset=%d\n",
1939 data_read, info->reassembly_data_length,
1940 info->first_entry_offset);
1941read_rfc1002_done:
1942 return data_read;
1943 }
1944
1945 log_read(INFO, "wait_event on more data\n");
1946 rc = wait_event_interruptible(
1947 info->wait_reassembly_queue,
1948 info->reassembly_data_length >= size ||
1949 info->transport_status != SMBD_CONNECTED);
1950 /* Don't return any data if interrupted */
1951 if (rc)
Long Li98e0d402019-04-05 21:36:32 +00001952 return rc;
Long Lif64b78f2017-11-22 17:38:40 -07001953
Long Lie8b3bfe2019-04-05 21:36:31 +00001954 if (info->transport_status != SMBD_CONNECTED) {
1955 log_read(ERR, "disconnected\n");
Long Liacd46802019-10-16 13:51:54 -07001956 return -ECONNABORTED;
Long Lie8b3bfe2019-04-05 21:36:31 +00001957 }
1958
Long Lif64b78f2017-11-22 17:38:40 -07001959 goto again;
1960}
1961
1962/*
1963 * Receive a page from receive reassembly queue
1964 * page: the page to read data into
1965 * to_read: the length of data to read
1966 * return value: actual data read
1967 */
Steve French2026b062018-01-24 23:07:41 -06001968static int smbd_recv_page(struct smbd_connection *info,
Long Li6509f502018-05-30 12:48:01 -07001969 struct page *page, unsigned int page_offset,
1970 unsigned int to_read)
Long Lif64b78f2017-11-22 17:38:40 -07001971{
1972 int ret;
1973 char *to_address;
Long Li6509f502018-05-30 12:48:01 -07001974 void *page_address;
Long Lif64b78f2017-11-22 17:38:40 -07001975
1976 /* make sure we have the page ready for read */
1977 ret = wait_event_interruptible(
1978 info->wait_reassembly_queue,
1979 info->reassembly_data_length >= to_read ||
1980 info->transport_status != SMBD_CONNECTED);
1981 if (ret)
Long Li6509f502018-05-30 12:48:01 -07001982 return ret;
Long Lif64b78f2017-11-22 17:38:40 -07001983
1984 /* now we can read from reassembly queue and not sleep */
Long Li6509f502018-05-30 12:48:01 -07001985 page_address = kmap_atomic(page);
1986 to_address = (char *) page_address + page_offset;
Long Lif64b78f2017-11-22 17:38:40 -07001987
1988 log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
1989 page, to_address, to_read);
1990
1991 ret = smbd_recv_buf(info, to_address, to_read);
Long Li6509f502018-05-30 12:48:01 -07001992 kunmap_atomic(page_address);
Long Lif64b78f2017-11-22 17:38:40 -07001993
1994 return ret;
1995}
1996
1997/*
1998 * Receive data from transport
1999 * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
2000 * return: total bytes read, or 0. SMB Direct will not do partial read.
2001 */
2002int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
2003{
2004 char *buf;
2005 struct page *page;
Long Li6509f502018-05-30 12:48:01 -07002006 unsigned int to_read, page_offset;
Long Lif64b78f2017-11-22 17:38:40 -07002007 int rc;
2008
David Howells00e23702018-10-22 13:07:28 +01002009 if (iov_iter_rw(&msg->msg_iter) == WRITE) {
2010 /* It's a bug in upper layer to get there */
2011 cifs_dbg(VFS, "CIFS: invalid msg iter dir %u\n",
2012 iov_iter_rw(&msg->msg_iter));
2013 rc = -EINVAL;
2014 goto out;
2015 }
2016
2017 switch (iov_iter_type(&msg->msg_iter)) {
2018 case ITER_KVEC:
Long Lif64b78f2017-11-22 17:38:40 -07002019 buf = msg->msg_iter.kvec->iov_base;
2020 to_read = msg->msg_iter.kvec->iov_len;
2021 rc = smbd_recv_buf(info, buf, to_read);
2022 break;
2023
David Howells00e23702018-10-22 13:07:28 +01002024 case ITER_BVEC:
Long Lif64b78f2017-11-22 17:38:40 -07002025 page = msg->msg_iter.bvec->bv_page;
Long Li6509f502018-05-30 12:48:01 -07002026 page_offset = msg->msg_iter.bvec->bv_offset;
Long Lif64b78f2017-11-22 17:38:40 -07002027 to_read = msg->msg_iter.bvec->bv_len;
Long Li6509f502018-05-30 12:48:01 -07002028 rc = smbd_recv_page(info, page, page_offset, to_read);
Long Lif64b78f2017-11-22 17:38:40 -07002029 break;
2030
2031 default:
2032 /* It's a bug in upper layer to get there */
2033 cifs_dbg(VFS, "CIFS: invalid msg type %d\n",
David Howells00e23702018-10-22 13:07:28 +01002034 iov_iter_type(&msg->msg_iter));
Long Li6509f502018-05-30 12:48:01 -07002035 rc = -EINVAL;
Long Lif64b78f2017-11-22 17:38:40 -07002036 }
2037
David Howells00e23702018-10-22 13:07:28 +01002038out:
Long Lif64b78f2017-11-22 17:38:40 -07002039 /* SMBDirect will read it all or nothing */
2040 if (rc > 0)
2041 msg->msg_iter.count = 0;
2042 return rc;
2043}
Long Lid649e1b2017-11-22 17:38:42 -07002044
2045/*
2046 * Send data to transport
2047 * Each rqst is transported as a SMBDirect payload
2048 * rqst: the data to write
2049 * return value: 0 if successfully write, otherwise error code
2050 */
Long Li4739f232019-04-15 14:49:17 -07002051int smbd_send(struct TCP_Server_Info *server,
2052 int num_rqst, struct smb_rqst *rqst_array)
Long Lid649e1b2017-11-22 17:38:42 -07002053{
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +10002054 struct smbd_connection *info = server->smbd_conn;
Long Lid649e1b2017-11-22 17:38:42 -07002055 struct kvec vec;
2056 int nvecs;
2057 int size;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -03002058 unsigned int buflen, remaining_data_length;
Long Lid649e1b2017-11-22 17:38:42 -07002059 int start, i, j;
2060 int max_iov_size =
2061 info->max_send_size - sizeof(struct smbd_data_transfer);
Long Li8bcda1d2018-04-17 12:17:07 -07002062 struct kvec *iov;
Long Lid649e1b2017-11-22 17:38:42 -07002063 int rc;
Long Li4739f232019-04-15 14:49:17 -07002064 struct smb_rqst *rqst;
2065 int rqst_idx;
Long Lid649e1b2017-11-22 17:38:42 -07002066
Long Lid649e1b2017-11-22 17:38:42 -07002067 if (info->transport_status != SMBD_CONNECTED) {
Long Li62fdf672019-04-05 21:36:33 +00002068 rc = -EAGAIN;
Long Lid649e1b2017-11-22 17:38:42 -07002069 goto done;
2070 }
2071
2072 /*
Long Lib6903bc2018-05-30 12:48:00 -07002073 * Add in the page array if there is one. The caller needs to set
2074 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
2075 * ends at page boundary
2076 */
Long Li4739f232019-04-15 14:49:17 -07002077 remaining_data_length = 0;
2078 for (i = 0; i < num_rqst; i++)
2079 remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
Long Lid649e1b2017-11-22 17:38:42 -07002080
Long Lif7950cb2020-03-26 19:42:24 -07002081 if (remaining_data_length > info->max_fragmented_send_size) {
Long Lid649e1b2017-11-22 17:38:42 -07002082 log_write(ERR, "payload size %d > max size %d\n",
Long Li4739f232019-04-15 14:49:17 -07002083 remaining_data_length, info->max_fragmented_send_size);
Long Lid649e1b2017-11-22 17:38:42 -07002084 rc = -EINVAL;
2085 goto done;
2086 }
2087
Long Li7f46d232019-05-13 21:01:29 -07002088 log_write(INFO, "num_rqst=%d total length=%u\n",
2089 num_rqst, remaining_data_length);
Paulo Alcantara35e2cc12018-06-15 10:22:44 -03002090
Long Li7f46d232019-05-13 21:01:29 -07002091 rqst_idx = 0;
Long Li4739f232019-04-15 14:49:17 -07002092next_rqst:
2093 rqst = &rqst_array[rqst_idx];
2094 iov = rqst->rq_iov;
2095
2096 cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
2097 rqst_idx, smb_rqst_len(server, rqst));
2098 for (i = 0; i < rqst->rq_nvec; i++)
Long Liff30b892018-04-17 12:17:10 -07002099 dump_smb(iov[i].iov_base, iov[i].iov_len);
2100
Long Lid649e1b2017-11-22 17:38:42 -07002101
Long Li4739f232019-04-15 14:49:17 -07002102 log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d "
2103 "rq_tailsz=%d buflen=%lu\n",
2104 rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
2105 rqst->rq_tailsz, smb_rqst_len(server, rqst));
Long Lid649e1b2017-11-22 17:38:42 -07002106
Long Li4739f232019-04-15 14:49:17 -07002107 start = i = 0;
Long Lid649e1b2017-11-22 17:38:42 -07002108 buflen = 0;
2109 while (true) {
2110 buflen += iov[i].iov_len;
2111 if (buflen > max_iov_size) {
2112 if (i > start) {
2113 remaining_data_length -=
2114 (buflen-iov[i].iov_len);
2115 log_write(INFO, "sending iov[] from start=%d "
2116 "i=%d nvecs=%d "
2117 "remaining_data_length=%d\n",
2118 start, i, i-start,
2119 remaining_data_length);
2120 rc = smbd_post_send_data(
2121 info, &iov[start], i-start,
2122 remaining_data_length);
2123 if (rc)
2124 goto done;
2125 } else {
2126 /* iov[start] is too big, break it */
2127 nvecs = (buflen+max_iov_size-1)/max_iov_size;
2128 log_write(INFO, "iov[%d] iov_base=%p buflen=%d"
2129 " break to %d vectors\n",
2130 start, iov[start].iov_base,
2131 buflen, nvecs);
2132 for (j = 0; j < nvecs; j++) {
2133 vec.iov_base =
2134 (char *)iov[start].iov_base +
2135 j*max_iov_size;
2136 vec.iov_len = max_iov_size;
2137 if (j == nvecs-1)
2138 vec.iov_len =
2139 buflen -
2140 max_iov_size*(nvecs-1);
2141 remaining_data_length -= vec.iov_len;
2142 log_write(INFO,
2143 "sending vec j=%d iov_base=%p"
2144 " iov_len=%zu "
2145 "remaining_data_length=%d\n",
2146 j, vec.iov_base, vec.iov_len,
2147 remaining_data_length);
2148 rc = smbd_post_send_data(
2149 info, &vec, 1,
2150 remaining_data_length);
2151 if (rc)
2152 goto done;
2153 }
2154 i++;
Long Li4739f232019-04-15 14:49:17 -07002155 if (i == rqst->rq_nvec)
Long Liab60ee72018-04-17 12:17:05 -07002156 break;
Long Lid649e1b2017-11-22 17:38:42 -07002157 }
2158 start = i;
2159 buflen = 0;
2160 } else {
2161 i++;
Long Li4739f232019-04-15 14:49:17 -07002162 if (i == rqst->rq_nvec) {
Long Lid649e1b2017-11-22 17:38:42 -07002163 /* send out all remaining vecs */
2164 remaining_data_length -= buflen;
2165 log_write(INFO,
2166 "sending iov[] from start=%d i=%d "
2167 "nvecs=%d remaining_data_length=%d\n",
2168 start, i, i-start,
2169 remaining_data_length);
2170 rc = smbd_post_send_data(info, &iov[start],
2171 i-start, remaining_data_length);
2172 if (rc)
2173 goto done;
2174 break;
2175 }
2176 }
2177 log_write(INFO, "looping i=%d buflen=%d\n", i, buflen);
2178 }
2179
2180 /* now sending pages if there are any */
2181 for (i = 0; i < rqst->rq_npages; i++) {
Long Lib6903bc2018-05-30 12:48:00 -07002182 unsigned int offset;
2183
2184 rqst_page_get_length(rqst, i, &buflen, &offset);
Long Lid649e1b2017-11-22 17:38:42 -07002185 nvecs = (buflen + max_iov_size - 1) / max_iov_size;
2186 log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
2187 buflen, nvecs);
2188 for (j = 0; j < nvecs; j++) {
2189 size = max_iov_size;
2190 if (j == nvecs-1)
2191 size = buflen - j*max_iov_size;
2192 remaining_data_length -= size;
2193 log_write(INFO, "sending pages i=%d offset=%d size=%d"
2194 " remaining_data_length=%d\n",
Long Lib6903bc2018-05-30 12:48:00 -07002195 i, j*max_iov_size+offset, size,
2196 remaining_data_length);
Long Lid649e1b2017-11-22 17:38:42 -07002197 rc = smbd_post_send_page(
Long Lib6903bc2018-05-30 12:48:00 -07002198 info, rqst->rq_pages[i],
2199 j*max_iov_size + offset,
Long Lid649e1b2017-11-22 17:38:42 -07002200 size, remaining_data_length);
2201 if (rc)
2202 goto done;
2203 }
2204 }
2205
Long Li4739f232019-04-15 14:49:17 -07002206 rqst_idx++;
2207 if (rqst_idx < num_rqst)
2208 goto next_rqst;
2209
Long Lid649e1b2017-11-22 17:38:42 -07002210done:
2211 /*
2212 * As an optimization, we don't wait for individual I/O to finish
2213 * before sending the next one.
2214 * Send them all and wait for pending send count to get to 0
2215 * that means all the I/Os have been out and we are good to return
2216 */
2217
Long Li072a14e2020-03-30 11:04:06 -07002218 wait_event(info->wait_send_pending,
2219 atomic_read(&info->send_pending) == 0);
Long Lid649e1b2017-11-22 17:38:42 -07002220
Long Lid649e1b2017-11-22 17:38:42 -07002221 return rc;
2222}
Long Lic7398582017-11-22 17:38:44 -07002223
2224static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
2225{
2226 struct smbd_mr *mr;
2227 struct ib_cqe *cqe;
2228
2229 if (wc->status) {
2230 log_rdma_mr(ERR, "status=%d\n", wc->status);
2231 cqe = wc->wr_cqe;
2232 mr = container_of(cqe, struct smbd_mr, cqe);
2233 smbd_disconnect_rdma_connection(mr->conn);
2234 }
2235}
2236
2237/*
2238 * The work queue function that recovers MRs
2239 * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
2240 * again. Both calls are slow, so finish them in a workqueue. This will not
2241 * block I/O path.
2242 * There is one workqueue that recovers MRs, there is no need to lock as the
2243 * I/O requests calling smbd_register_mr will never update the links in the
2244 * mr_list.
2245 */
2246static void smbd_mr_recovery_work(struct work_struct *work)
2247{
2248 struct smbd_connection *info =
2249 container_of(work, struct smbd_connection, mr_recovery_work);
2250 struct smbd_mr *smbdirect_mr;
2251 int rc;
2252
2253 list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
Long Lic21ce582019-10-16 13:51:55 -07002254 if (smbdirect_mr->state == MR_ERROR) {
Long Lic7398582017-11-22 17:38:44 -07002255
Long Li7cf20bc2018-05-30 12:48:02 -07002256 /* recover this MR entry */
2257 rc = ib_dereg_mr(smbdirect_mr->mr);
2258 if (rc) {
2259 log_rdma_mr(ERR,
2260 "ib_dereg_mr failed rc=%x\n",
2261 rc);
2262 smbd_disconnect_rdma_connection(info);
2263 continue;
2264 }
2265
2266 smbdirect_mr->mr = ib_alloc_mr(
2267 info->pd, info->mr_type,
2268 info->max_frmr_depth);
2269 if (IS_ERR(smbdirect_mr->mr)) {
2270 log_rdma_mr(ERR,
2271 "ib_alloc_mr failed mr_type=%x "
2272 "max_frmr_depth=%x\n",
2273 info->mr_type,
2274 info->max_frmr_depth);
2275 smbd_disconnect_rdma_connection(info);
2276 continue;
2277 }
Long Liff526d82018-09-20 21:18:39 +00002278 } else
2279 /* This MR is being used, don't recover it */
2280 continue;
Long Li7cf20bc2018-05-30 12:48:02 -07002281
Long Liff526d82018-09-20 21:18:39 +00002282 smbdirect_mr->state = MR_READY;
Long Lic7398582017-11-22 17:38:44 -07002283
Long Liff526d82018-09-20 21:18:39 +00002284 /* smbdirect_mr->state is updated by this function
2285 * and is read and updated by I/O issuing CPUs trying
2286 * to get a MR, the call to atomic_inc_return
2287 * implicates a memory barrier and guarantees this
2288 * value is updated before waking up any calls to
2289 * get_mr() from the I/O issuing CPUs
2290 */
2291 if (atomic_inc_return(&info->mr_ready_count) == 1)
2292 wake_up_interruptible(&info->wait_mr);
Long Lic7398582017-11-22 17:38:44 -07002293 }
2294}
2295
2296static void destroy_mr_list(struct smbd_connection *info)
2297{
2298 struct smbd_mr *mr, *tmp;
2299
2300 cancel_work_sync(&info->mr_recovery_work);
2301 list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
2302 if (mr->state == MR_INVALIDATED)
2303 ib_dma_unmap_sg(info->id->device, mr->sgl,
2304 mr->sgl_count, mr->dir);
2305 ib_dereg_mr(mr->mr);
2306 kfree(mr->sgl);
2307 kfree(mr);
2308 }
2309}
2310
2311/*
2312 * Allocate MRs used for RDMA read/write
2313 * The number of MRs will not exceed hardware capability in responder_resources
2314 * All MRs are kept in mr_list. The MR can be recovered after it's used
2315 * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
2316 * as MRs are used and recovered for I/O, but the list links will not change
2317 */
2318static int allocate_mr_list(struct smbd_connection *info)
2319{
2320 int i;
2321 struct smbd_mr *smbdirect_mr, *tmp;
2322
2323 INIT_LIST_HEAD(&info->mr_list);
2324 init_waitqueue_head(&info->wait_mr);
2325 spin_lock_init(&info->mr_list_lock);
2326 atomic_set(&info->mr_ready_count, 0);
2327 atomic_set(&info->mr_used_count, 0);
2328 init_waitqueue_head(&info->wait_for_mr_cleanup);
2329 /* Allocate more MRs (2x) than hardware responder_resources */
2330 for (i = 0; i < info->responder_resources * 2; i++) {
2331 smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
2332 if (!smbdirect_mr)
2333 goto out;
2334 smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
2335 info->max_frmr_depth);
2336 if (IS_ERR(smbdirect_mr->mr)) {
2337 log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x "
2338 "max_frmr_depth=%x\n",
2339 info->mr_type, info->max_frmr_depth);
2340 goto out;
2341 }
2342 smbdirect_mr->sgl = kcalloc(
2343 info->max_frmr_depth,
2344 sizeof(struct scatterlist),
2345 GFP_KERNEL);
2346 if (!smbdirect_mr->sgl) {
2347 log_rdma_mr(ERR, "failed to allocate sgl\n");
2348 ib_dereg_mr(smbdirect_mr->mr);
2349 goto out;
2350 }
2351 smbdirect_mr->state = MR_READY;
2352 smbdirect_mr->conn = info;
2353
2354 list_add_tail(&smbdirect_mr->list, &info->mr_list);
2355 atomic_inc(&info->mr_ready_count);
2356 }
2357 INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
2358 return 0;
2359
2360out:
2361 kfree(smbdirect_mr);
2362
2363 list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
2364 ib_dereg_mr(smbdirect_mr->mr);
2365 kfree(smbdirect_mr->sgl);
2366 kfree(smbdirect_mr);
2367 }
2368 return -ENOMEM;
2369}
2370
2371/*
2372 * Get a MR from mr_list. This function waits until there is at least one
2373 * MR available in the list. It may access the list while the
2374 * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
2375 * as they never modify the same places. However, there may be several CPUs
2376 * issueing I/O trying to get MR at the same time, mr_list_lock is used to
2377 * protect this situation.
2378 */
2379static struct smbd_mr *get_mr(struct smbd_connection *info)
2380{
2381 struct smbd_mr *ret;
2382 int rc;
2383again:
2384 rc = wait_event_interruptible(info->wait_mr,
2385 atomic_read(&info->mr_ready_count) ||
2386 info->transport_status != SMBD_CONNECTED);
2387 if (rc) {
2388 log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
2389 return NULL;
2390 }
2391
2392 if (info->transport_status != SMBD_CONNECTED) {
2393 log_rdma_mr(ERR, "info->transport_status=%x\n",
2394 info->transport_status);
2395 return NULL;
2396 }
2397
2398 spin_lock(&info->mr_list_lock);
2399 list_for_each_entry(ret, &info->mr_list, list) {
2400 if (ret->state == MR_READY) {
2401 ret->state = MR_REGISTERED;
2402 spin_unlock(&info->mr_list_lock);
2403 atomic_dec(&info->mr_ready_count);
2404 atomic_inc(&info->mr_used_count);
2405 return ret;
2406 }
2407 }
2408
2409 spin_unlock(&info->mr_list_lock);
2410 /*
2411 * It is possible that we could fail to get MR because other processes may
2412 * try to acquire a MR at the same time. If this is the case, retry it.
2413 */
2414 goto again;
2415}
2416
2417/*
2418 * Register memory for RDMA read/write
2419 * pages[]: the list of pages to register memory with
2420 * num_pages: the number of pages to register
2421 * tailsz: if non-zero, the bytes to register in the last page
2422 * writing: true if this is a RDMA write (SMB read), false for RDMA read
2423 * need_invalidate: true if this MR needs to be locally invalidated after I/O
2424 * return value: the MR registered, NULL if failed.
2425 */
2426struct smbd_mr *smbd_register_mr(
2427 struct smbd_connection *info, struct page *pages[], int num_pages,
Long Li7cf20bc2018-05-30 12:48:02 -07002428 int offset, int tailsz, bool writing, bool need_invalidate)
Long Lic7398582017-11-22 17:38:44 -07002429{
2430 struct smbd_mr *smbdirect_mr;
2431 int rc, i;
2432 enum dma_data_direction dir;
2433 struct ib_reg_wr *reg_wr;
Long Lic7398582017-11-22 17:38:44 -07002434
2435 if (num_pages > info->max_frmr_depth) {
2436 log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
2437 num_pages, info->max_frmr_depth);
2438 return NULL;
2439 }
2440
2441 smbdirect_mr = get_mr(info);
2442 if (!smbdirect_mr) {
2443 log_rdma_mr(ERR, "get_mr returning NULL\n");
2444 return NULL;
2445 }
2446 smbdirect_mr->need_invalidate = need_invalidate;
2447 smbdirect_mr->sgl_count = num_pages;
2448 sg_init_table(smbdirect_mr->sgl, num_pages);
2449
Long Li7cf20bc2018-05-30 12:48:02 -07002450 log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
2451 num_pages, offset, tailsz);
Long Lic7398582017-11-22 17:38:44 -07002452
Long Li7cf20bc2018-05-30 12:48:02 -07002453 if (num_pages == 1) {
2454 sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
2455 goto skip_multiple_pages;
2456 }
2457
2458 /* We have at least two pages to register */
2459 sg_set_page(
2460 &smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
2461 i = 1;
2462 while (i < num_pages - 1) {
2463 sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
2464 i++;
2465 }
Long Lic7398582017-11-22 17:38:44 -07002466 sg_set_page(&smbdirect_mr->sgl[i], pages[i],
2467 tailsz ? tailsz : PAGE_SIZE, 0);
2468
Long Li7cf20bc2018-05-30 12:48:02 -07002469skip_multiple_pages:
Long Lic7398582017-11-22 17:38:44 -07002470 dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2471 smbdirect_mr->dir = dir;
2472 rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
2473 if (!rc) {
Long Li7cf20bc2018-05-30 12:48:02 -07002474 log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
Long Lic7398582017-11-22 17:38:44 -07002475 num_pages, dir, rc);
2476 goto dma_map_error;
2477 }
2478
2479 rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
2480 NULL, PAGE_SIZE);
2481 if (rc != num_pages) {
Long Li7cf20bc2018-05-30 12:48:02 -07002482 log_rdma_mr(ERR,
2483 "ib_map_mr_sg failed rc = %d num_pages = %x\n",
Long Lic7398582017-11-22 17:38:44 -07002484 rc, num_pages);
2485 goto map_mr_error;
2486 }
2487
2488 ib_update_fast_reg_key(smbdirect_mr->mr,
2489 ib_inc_rkey(smbdirect_mr->mr->rkey));
2490 reg_wr = &smbdirect_mr->wr;
2491 reg_wr->wr.opcode = IB_WR_REG_MR;
2492 smbdirect_mr->cqe.done = register_mr_done;
2493 reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
2494 reg_wr->wr.num_sge = 0;
2495 reg_wr->wr.send_flags = IB_SEND_SIGNALED;
2496 reg_wr->mr = smbdirect_mr->mr;
2497 reg_wr->key = smbdirect_mr->mr->rkey;
2498 reg_wr->access = writing ?
2499 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2500 IB_ACCESS_REMOTE_READ;
2501
2502 /*
2503 * There is no need for waiting for complemtion on ib_post_send
2504 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
2505 * on the next ib_post_send when we actaully send I/O to remote peer
2506 */
Bart Van Assche73930592018-07-18 09:25:25 -07002507 rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
Long Lic7398582017-11-22 17:38:44 -07002508 if (!rc)
2509 return smbdirect_mr;
2510
2511 log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
2512 rc, reg_wr->key);
2513
2514 /* If all failed, attempt to recover this MR by setting it MR_ERROR*/
2515map_mr_error:
2516 ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
2517 smbdirect_mr->sgl_count, smbdirect_mr->dir);
2518
2519dma_map_error:
2520 smbdirect_mr->state = MR_ERROR;
2521 if (atomic_dec_and_test(&info->mr_used_count))
2522 wake_up(&info->wait_for_mr_cleanup);
2523
Long Li21a4e142018-03-30 15:16:36 -07002524 smbd_disconnect_rdma_connection(info);
2525
Long Lic7398582017-11-22 17:38:44 -07002526 return NULL;
2527}
2528
2529static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
2530{
2531 struct smbd_mr *smbdirect_mr;
2532 struct ib_cqe *cqe;
2533
2534 cqe = wc->wr_cqe;
2535 smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
2536 smbdirect_mr->state = MR_INVALIDATED;
2537 if (wc->status != IB_WC_SUCCESS) {
2538 log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
2539 smbdirect_mr->state = MR_ERROR;
2540 }
2541 complete(&smbdirect_mr->invalidate_done);
2542}
2543
2544/*
2545 * Deregister a MR after I/O is done
2546 * This function may wait if remote invalidation is not used
2547 * and we have to locally invalidate the buffer to prevent data is being
2548 * modified by remote peer after upper layer consumes it
2549 */
2550int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
2551{
Bart Van Assche73930592018-07-18 09:25:25 -07002552 struct ib_send_wr *wr;
Long Lic7398582017-11-22 17:38:44 -07002553 struct smbd_connection *info = smbdirect_mr->conn;
2554 int rc = 0;
2555
2556 if (smbdirect_mr->need_invalidate) {
2557 /* Need to finish local invalidation before returning */
2558 wr = &smbdirect_mr->inv_wr;
2559 wr->opcode = IB_WR_LOCAL_INV;
2560 smbdirect_mr->cqe.done = local_inv_done;
2561 wr->wr_cqe = &smbdirect_mr->cqe;
2562 wr->num_sge = 0;
2563 wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
2564 wr->send_flags = IB_SEND_SIGNALED;
2565
2566 init_completion(&smbdirect_mr->invalidate_done);
Bart Van Assche73930592018-07-18 09:25:25 -07002567 rc = ib_post_send(info->id->qp, wr, NULL);
Long Lic7398582017-11-22 17:38:44 -07002568 if (rc) {
2569 log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
2570 smbd_disconnect_rdma_connection(info);
2571 goto done;
2572 }
2573 wait_for_completion(&smbdirect_mr->invalidate_done);
2574 smbdirect_mr->need_invalidate = false;
2575 } else
2576 /*
2577 * For remote invalidation, just set it to MR_INVALIDATED
2578 * and defer to mr_recovery_work to recover the MR for next use
2579 */
2580 smbdirect_mr->state = MR_INVALIDATED;
2581
Long Lic21ce582019-10-16 13:51:55 -07002582 if (smbdirect_mr->state == MR_INVALIDATED) {
2583 ib_dma_unmap_sg(
2584 info->id->device, smbdirect_mr->sgl,
2585 smbdirect_mr->sgl_count,
2586 smbdirect_mr->dir);
2587 smbdirect_mr->state = MR_READY;
2588 if (atomic_inc_return(&info->mr_ready_count) == 1)
2589 wake_up_interruptible(&info->wait_mr);
2590 } else
2591 /*
2592 * Schedule the work to do MR recovery for future I/Os MR
2593 * recovery is slow and don't want it to block current I/O
2594 */
2595 queue_work(info->workqueue, &info->mr_recovery_work);
Long Lic7398582017-11-22 17:38:44 -07002596
2597done:
2598 if (atomic_dec_and_test(&info->mr_used_count))
2599 wake_up(&info->wait_for_mr_cleanup);
2600
2601 return rc;
2602}