blob: fa52bf3e02360ca3063e9d3557fe254c5c5dceeb [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
Long Lif1981862017-11-04 18:17:24 -0700453 /* Check if we can post new receive and grant credits to peer */
454 check_and_send_immediate(info);
455}
456
Long Lif1981862017-11-04 18:17:24 -0700457/* Called from softirq, when recv is done */
458static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
459{
460 struct smbd_data_transfer *data_transfer;
461 struct smbd_response *response =
462 container_of(wc->wr_cqe, struct smbd_response, cqe);
463 struct smbd_connection *info = response->info;
464 int data_length = 0;
465
466 log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d "
467 "byte_len=%d pkey_index=%x\n",
468 response, response->type, wc->status, wc->opcode,
469 wc->byte_len, wc->pkey_index);
470
471 if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
472 log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
473 wc->status, wc->opcode);
474 smbd_disconnect_rdma_connection(info);
475 goto error;
476 }
477
478 ib_dma_sync_single_for_cpu(
479 wc->qp->device,
480 response->sge.addr,
481 response->sge.length,
482 DMA_FROM_DEVICE);
483
484 switch (response->type) {
485 /* SMBD negotiation response */
486 case SMBD_NEGOTIATE_RESP:
487 dump_smbd_negotiate_resp(smbd_response_payload(response));
488 info->full_packet_received = true;
489 info->negotiate_done =
490 process_negotiation_response(response, wc->byte_len);
491 complete(&info->negotiate_completion);
492 break;
493
494 /* SMBD data transfer packet */
495 case SMBD_TRANSFER_DATA:
496 data_transfer = smbd_response_payload(response);
497 data_length = le32_to_cpu(data_transfer->data_length);
498
499 /*
500 * If this is a packet with data playload place the data in
501 * reassembly queue and wake up the reading thread
502 */
503 if (data_length) {
504 if (info->full_packet_received)
505 response->first_segment = true;
506
507 if (le32_to_cpu(data_transfer->remaining_data_length))
508 info->full_packet_received = false;
509 else
510 info->full_packet_received = true;
511
512 enqueue_reassembly(
513 info,
514 response,
515 data_length);
516 } else
517 put_empty_packet(info, response);
518
519 if (data_length)
520 wake_up_interruptible(&info->wait_reassembly_queue);
521
522 atomic_dec(&info->receive_credits);
523 info->receive_credit_target =
524 le16_to_cpu(data_transfer->credits_requested);
Long Li4ebb8792020-03-26 22:33:01 -0700525 if (le16_to_cpu(data_transfer->credits_granted)) {
526 atomic_add(le16_to_cpu(data_transfer->credits_granted),
527 &info->send_credits);
528 /*
529 * We have new send credits granted from remote peer
530 * If any sender is waiting for credits, unblock it
531 */
532 wake_up_interruptible(&info->wait_send_queue);
533 }
Long Lif1981862017-11-04 18:17:24 -0700534
535 log_incoming(INFO, "data flags %d data_offset %d "
536 "data_length %d remaining_data_length %d\n",
537 le16_to_cpu(data_transfer->flags),
538 le32_to_cpu(data_transfer->data_offset),
539 le32_to_cpu(data_transfer->data_length),
540 le32_to_cpu(data_transfer->remaining_data_length));
541
542 /* Send a KEEP_ALIVE response right away if requested */
543 info->keep_alive_requested = KEEP_ALIVE_NONE;
544 if (le16_to_cpu(data_transfer->flags) &
545 SMB_DIRECT_RESPONSE_REQUESTED) {
546 info->keep_alive_requested = KEEP_ALIVE_PENDING;
547 }
548
Long Li4ebb8792020-03-26 22:33:01 -0700549 /*
550 * Check if we need to send something to remote peer to
551 * grant more credits or respond to KEEP_ALIVE packet
552 */
553 check_and_send_immediate(info);
554
Long Lif1981862017-11-04 18:17:24 -0700555 return;
556
557 default:
558 log_rdma_recv(ERR,
559 "unexpected response type=%d\n", response->type);
560 }
561
562error:
563 put_receive_buffer(info, response);
564}
565
566static struct rdma_cm_id *smbd_create_id(
567 struct smbd_connection *info,
568 struct sockaddr *dstaddr, int port)
569{
570 struct rdma_cm_id *id;
571 int rc;
572 __be16 *sport;
573
574 id = rdma_create_id(&init_net, smbd_conn_upcall, info,
575 RDMA_PS_TCP, IB_QPT_RC);
576 if (IS_ERR(id)) {
577 rc = PTR_ERR(id);
578 log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
579 return id;
580 }
581
582 if (dstaddr->sa_family == AF_INET6)
583 sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
584 else
585 sport = &((struct sockaddr_in *)dstaddr)->sin_port;
586
587 *sport = htons(port);
588
589 init_completion(&info->ri_done);
590 info->ri_rc = -ETIMEDOUT;
591
592 rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
593 RDMA_RESOLVE_TIMEOUT);
594 if (rc) {
595 log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
596 goto out;
597 }
598 wait_for_completion_interruptible_timeout(
599 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
600 rc = info->ri_rc;
601 if (rc) {
602 log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
603 goto out;
604 }
605
606 info->ri_rc = -ETIMEDOUT;
607 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
608 if (rc) {
609 log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
610 goto out;
611 }
612 wait_for_completion_interruptible_timeout(
613 &info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
614 rc = info->ri_rc;
615 if (rc) {
616 log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
617 goto out;
618 }
619
620 return id;
621
622out:
623 rdma_destroy_id(id);
624 return ERR_PTR(rc);
625}
626
627/*
628 * Test if FRWR (Fast Registration Work Requests) is supported on the device
629 * This implementation requries FRWR on RDMA read/write
630 * return value: true if it is supported
631 */
632static bool frwr_is_supported(struct ib_device_attr *attrs)
633{
634 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
635 return false;
636 if (attrs->max_fast_reg_page_list_len == 0)
637 return false;
638 return true;
639}
640
641static int smbd_ia_open(
642 struct smbd_connection *info,
643 struct sockaddr *dstaddr, int port)
644{
645 int rc;
646
647 info->id = smbd_create_id(info, dstaddr, port);
648 if (IS_ERR(info->id)) {
649 rc = PTR_ERR(info->id);
650 goto out1;
651 }
652
653 if (!frwr_is_supported(&info->id->device->attrs)) {
654 log_rdma_event(ERR,
655 "Fast Registration Work Requests "
656 "(FRWR) is not supported\n");
657 log_rdma_event(ERR,
658 "Device capability flags = %llx "
659 "max_fast_reg_page_list_len = %u\n",
660 info->id->device->attrs.device_cap_flags,
661 info->id->device->attrs.max_fast_reg_page_list_len);
662 rc = -EPROTONOSUPPORT;
663 goto out2;
664 }
Long Lic7398582017-11-22 17:38:44 -0700665 info->max_frmr_depth = min_t(int,
666 smbd_max_frmr_depth,
667 info->id->device->attrs.max_fast_reg_page_list_len);
668 info->mr_type = IB_MR_TYPE_MEM_REG;
669 if (info->id->device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
670 info->mr_type = IB_MR_TYPE_SG_GAPS;
Long Lif1981862017-11-04 18:17:24 -0700671
672 info->pd = ib_alloc_pd(info->id->device, 0);
673 if (IS_ERR(info->pd)) {
674 rc = PTR_ERR(info->pd);
675 log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
676 goto out2;
677 }
678
679 return 0;
680
681out2:
682 rdma_destroy_id(info->id);
683 info->id = NULL;
684
685out1:
686 return rc;
687}
688
689/*
690 * Send a negotiation request message to the peer
691 * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
692 * After negotiation, the transport is connected and ready for
693 * carrying upper layer SMB payload
694 */
695static int smbd_post_send_negotiate_req(struct smbd_connection *info)
696{
Bart Van Assche73930592018-07-18 09:25:25 -0700697 struct ib_send_wr send_wr;
Long Lif1981862017-11-04 18:17:24 -0700698 int rc = -ENOMEM;
699 struct smbd_request *request;
700 struct smbd_negotiate_req *packet;
701
702 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
703 if (!request)
704 return rc;
705
706 request->info = info;
707
708 packet = smbd_request_payload(request);
709 packet->min_version = cpu_to_le16(SMBD_V1);
710 packet->max_version = cpu_to_le16(SMBD_V1);
711 packet->reserved = 0;
712 packet->credits_requested = cpu_to_le16(info->send_credit_target);
713 packet->preferred_send_size = cpu_to_le32(info->max_send_size);
714 packet->max_receive_size = cpu_to_le32(info->max_receive_size);
715 packet->max_fragmented_size =
716 cpu_to_le32(info->max_fragmented_recv_size);
717
718 request->num_sge = 1;
719 request->sge[0].addr = ib_dma_map_single(
720 info->id->device, (void *)packet,
721 sizeof(*packet), DMA_TO_DEVICE);
722 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
723 rc = -EIO;
724 goto dma_mapping_failed;
725 }
726
727 request->sge[0].length = sizeof(*packet);
728 request->sge[0].lkey = info->pd->local_dma_lkey;
729
730 ib_dma_sync_single_for_device(
731 info->id->device, request->sge[0].addr,
732 request->sge[0].length, DMA_TO_DEVICE);
733
734 request->cqe.done = send_done;
735
736 send_wr.next = NULL;
737 send_wr.wr_cqe = &request->cqe;
738 send_wr.sg_list = request->sge;
739 send_wr.num_sge = request->num_sge;
740 send_wr.opcode = IB_WR_SEND;
741 send_wr.send_flags = IB_SEND_SIGNALED;
742
743 log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
744 request->sge[0].addr,
745 request->sge[0].length, request->sge[0].lkey);
746
Long Lif1981862017-11-04 18:17:24 -0700747 atomic_inc(&info->send_pending);
Bart Van Assche73930592018-07-18 09:25:25 -0700748 rc = ib_post_send(info->id->qp, &send_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -0700749 if (!rc)
750 return 0;
751
752 /* if we reach here, post send failed */
753 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
754 atomic_dec(&info->send_pending);
755 ib_dma_unmap_single(info->id->device, request->sge[0].addr,
756 request->sge[0].length, DMA_TO_DEVICE);
757
Long Li21a4e142018-03-30 15:16:36 -0700758 smbd_disconnect_rdma_connection(info);
759
Long Lif1981862017-11-04 18:17:24 -0700760dma_mapping_failed:
761 mempool_free(request, info->request_mempool);
762 return rc;
763}
764
765/*
766 * Extend the credits to remote peer
767 * This implements [MS-SMBD] 3.1.5.9
768 * The idea is that we should extend credits to remote peer as quickly as
769 * it's allowed, to maintain data flow. We allocate as much receive
770 * buffer as possible, and extend the receive credits to remote peer
771 * return value: the new credtis being granted.
772 */
773static int manage_credits_prior_sending(struct smbd_connection *info)
774{
775 int new_credits;
776
777 spin_lock(&info->lock_new_credits_offered);
778 new_credits = info->new_credits_offered;
779 info->new_credits_offered = 0;
780 spin_unlock(&info->lock_new_credits_offered);
781
782 return new_credits;
783}
784
785/*
786 * Check if we need to send a KEEP_ALIVE message
787 * The idle connection timer triggers a KEEP_ALIVE message when expires
788 * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
789 * back a response.
790 * return value:
791 * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
792 * 0: otherwise
793 */
794static int manage_keep_alive_before_sending(struct smbd_connection *info)
795{
796 if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
797 info->keep_alive_requested = KEEP_ALIVE_SENT;
798 return 1;
799 }
800 return 0;
801}
802
803/*
804 * Build and prepare the SMBD packet header
805 * This function waits for avaialbe send credits and build a SMBD packet
806 * header. The caller then optional append payload to the packet after
807 * the header
808 * intput values
809 * size: the size of the payload
810 * remaining_data_length: remaining data to send if this is part of a
811 * fragmented packet
812 * output values
813 * request_out: the request allocated from this function
814 * return values: 0 on success, otherwise actual error code returned
815 */
816static int smbd_create_header(struct smbd_connection *info,
817 int size, int remaining_data_length,
818 struct smbd_request **request_out)
819{
820 struct smbd_request *request;
821 struct smbd_data_transfer *packet;
822 int header_length;
Long Lid4e51602020-04-02 13:42:06 -0700823 int new_credits;
Long Lif1981862017-11-04 18:17:24 -0700824 int rc;
825
826 /* Wait for send credits. A SMBD packet needs one credit */
827 rc = wait_event_interruptible(info->wait_send_queue,
828 atomic_read(&info->send_credits) > 0 ||
829 info->transport_status != SMBD_CONNECTED);
830 if (rc)
831 return rc;
832
833 if (info->transport_status != SMBD_CONNECTED) {
834 log_outgoing(ERR, "disconnected not sending\n");
Long Li62fdf672019-04-05 21:36:33 +0000835 return -EAGAIN;
Long Lif1981862017-11-04 18:17:24 -0700836 }
837 atomic_dec(&info->send_credits);
838
839 request = mempool_alloc(info->request_mempool, GFP_KERNEL);
840 if (!request) {
841 rc = -ENOMEM;
Long Lid4e51602020-04-02 13:42:06 -0700842 goto err_alloc;
Long Lif1981862017-11-04 18:17:24 -0700843 }
844
845 request->info = info;
846
847 /* Fill in the packet header */
848 packet = smbd_request_payload(request);
849 packet->credits_requested = cpu_to_le16(info->send_credit_target);
Long Lid4e51602020-04-02 13:42:06 -0700850
851 new_credits = manage_credits_prior_sending(info);
852 atomic_add(new_credits, &info->receive_credits);
853 packet->credits_granted = cpu_to_le16(new_credits);
854
Long Lif1981862017-11-04 18:17:24 -0700855 info->send_immediate = false;
856
857 packet->flags = 0;
858 if (manage_keep_alive_before_sending(info))
859 packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
860
861 packet->reserved = 0;
862 if (!size)
863 packet->data_offset = 0;
864 else
865 packet->data_offset = cpu_to_le32(24);
866 packet->data_length = cpu_to_le32(size);
867 packet->remaining_data_length = cpu_to_le32(remaining_data_length);
868 packet->padding = 0;
869
870 log_outgoing(INFO, "credits_requested=%d credits_granted=%d "
871 "data_offset=%d data_length=%d remaining_data_length=%d\n",
872 le16_to_cpu(packet->credits_requested),
873 le16_to_cpu(packet->credits_granted),
874 le32_to_cpu(packet->data_offset),
875 le32_to_cpu(packet->data_length),
876 le32_to_cpu(packet->remaining_data_length));
877
878 /* Map the packet to DMA */
879 header_length = sizeof(struct smbd_data_transfer);
880 /* If this is a packet without payload, don't send padding */
881 if (!size)
882 header_length = offsetof(struct smbd_data_transfer, padding);
883
884 request->num_sge = 1;
885 request->sge[0].addr = ib_dma_map_single(info->id->device,
886 (void *)packet,
887 header_length,
Long Li7f46d232019-05-13 21:01:29 -0700888 DMA_TO_DEVICE);
Long Lif1981862017-11-04 18:17:24 -0700889 if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
890 mempool_free(request, info->request_mempool);
891 rc = -EIO;
Long Lid4e51602020-04-02 13:42:06 -0700892 goto err_dma;
Long Lif1981862017-11-04 18:17:24 -0700893 }
894
895 request->sge[0].length = header_length;
896 request->sge[0].lkey = info->pd->local_dma_lkey;
897
898 *request_out = request;
899 return 0;
900
Long Lid4e51602020-04-02 13:42:06 -0700901err_dma:
902 /* roll back receive credits */
903 spin_lock(&info->lock_new_credits_offered);
904 info->new_credits_offered += new_credits;
905 spin_unlock(&info->lock_new_credits_offered);
906 atomic_sub(new_credits, &info->receive_credits);
907
908err_alloc:
909 /* roll back send credits */
Long Lif1981862017-11-04 18:17:24 -0700910 atomic_inc(&info->send_credits);
Long Lid4e51602020-04-02 13:42:06 -0700911
Long Lif1981862017-11-04 18:17:24 -0700912 return rc;
913}
914
915static void smbd_destroy_header(struct smbd_connection *info,
916 struct smbd_request *request)
917{
918
919 ib_dma_unmap_single(info->id->device,
920 request->sge[0].addr,
921 request->sge[0].length,
922 DMA_TO_DEVICE);
923 mempool_free(request, info->request_mempool);
924 atomic_inc(&info->send_credits);
925}
926
927/* Post the send request */
928static int smbd_post_send(struct smbd_connection *info,
Long Li072a14e2020-03-30 11:04:06 -0700929 struct smbd_request *request)
Long Lif1981862017-11-04 18:17:24 -0700930{
Bart Van Assche73930592018-07-18 09:25:25 -0700931 struct ib_send_wr send_wr;
Long Lif1981862017-11-04 18:17:24 -0700932 int rc, i;
933
934 for (i = 0; i < request->num_sge; i++) {
935 log_rdma_send(INFO,
Colin Ian Kingac65cb62018-02-09 12:14:15 +0000936 "rdma_request sge[%d] addr=%llu length=%u\n",
Long Liff30b892018-04-17 12:17:10 -0700937 i, request->sge[i].addr, request->sge[i].length);
Long Lif1981862017-11-04 18:17:24 -0700938 ib_dma_sync_single_for_device(
939 info->id->device,
940 request->sge[i].addr,
941 request->sge[i].length,
942 DMA_TO_DEVICE);
943 }
944
945 request->cqe.done = send_done;
946
947 send_wr.next = NULL;
948 send_wr.wr_cqe = &request->cqe;
949 send_wr.sg_list = request->sge;
950 send_wr.num_sge = request->num_sge;
951 send_wr.opcode = IB_WR_SEND;
952 send_wr.send_flags = IB_SEND_SIGNALED;
953
Long Li3ffbe782020-03-30 11:04:07 -0700954wait_sq:
955 wait_event(info->wait_post_send,
956 atomic_read(&info->send_pending) < info->send_credit_target);
957 if (unlikely(atomic_inc_return(&info->send_pending) >
958 info->send_credit_target)) {
959 atomic_dec(&info->send_pending);
960 goto wait_sq;
961 }
Long Lif1981862017-11-04 18:17:24 -0700962
Bart Van Assche73930592018-07-18 09:25:25 -0700963 rc = ib_post_send(info->id->qp, &send_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -0700964 if (rc) {
965 log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
Long Li072a14e2020-03-30 11:04:06 -0700966 if (atomic_dec_and_test(&info->send_pending))
967 wake_up(&info->wait_send_pending);
Long Li21a4e142018-03-30 15:16:36 -0700968 smbd_disconnect_rdma_connection(info);
Long Li62fdf672019-04-05 21:36:33 +0000969 rc = -EAGAIN;
Long Lif1981862017-11-04 18:17:24 -0700970 } else
971 /* Reset timer for idle connection after packet is sent */
972 mod_delayed_work(info->workqueue, &info->idle_timer_work,
973 info->keep_alive_interval*HZ);
974
975 return rc;
976}
977
978static int smbd_post_send_sgl(struct smbd_connection *info,
979 struct scatterlist *sgl, int data_length, int remaining_data_length)
980{
981 int num_sgs;
982 int i, rc;
983 struct smbd_request *request;
984 struct scatterlist *sg;
985
986 rc = smbd_create_header(
987 info, data_length, remaining_data_length, &request);
988 if (rc)
989 return rc;
990
991 num_sgs = sgl ? sg_nents(sgl) : 0;
992 for_each_sg(sgl, sg, num_sgs, i) {
993 request->sge[i+1].addr =
994 ib_dma_map_page(info->id->device, sg_page(sg),
Long Li7f46d232019-05-13 21:01:29 -0700995 sg->offset, sg->length, DMA_TO_DEVICE);
Long Lif1981862017-11-04 18:17:24 -0700996 if (ib_dma_mapping_error(
997 info->id->device, request->sge[i+1].addr)) {
998 rc = -EIO;
999 request->sge[i+1].addr = 0;
1000 goto dma_mapping_failure;
1001 }
1002 request->sge[i+1].length = sg->length;
1003 request->sge[i+1].lkey = info->pd->local_dma_lkey;
1004 request->num_sge++;
1005 }
1006
Long Li072a14e2020-03-30 11:04:06 -07001007 rc = smbd_post_send(info, request);
Long Lif1981862017-11-04 18:17:24 -07001008 if (!rc)
1009 return 0;
1010
1011dma_mapping_failure:
1012 for (i = 1; i < request->num_sge; i++)
1013 if (request->sge[i].addr)
1014 ib_dma_unmap_single(info->id->device,
1015 request->sge[i].addr,
1016 request->sge[i].length,
1017 DMA_TO_DEVICE);
1018 smbd_destroy_header(info, request);
1019 return rc;
1020}
1021
1022/*
Long Lid649e1b2017-11-22 17:38:42 -07001023 * Send a page
1024 * page: the page to send
1025 * offset: offset in the page to send
1026 * size: length in the page to send
1027 * remaining_data_length: remaining data to send in this payload
1028 */
1029static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
1030 unsigned long offset, size_t size, int remaining_data_length)
1031{
1032 struct scatterlist sgl;
1033
1034 sg_init_table(&sgl, 1);
1035 sg_set_page(&sgl, page, size, offset);
1036
1037 return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
1038}
1039
1040/*
Long Lif1981862017-11-04 18:17:24 -07001041 * Send an empty message
1042 * Empty message is used to extend credits to peer to for keep live
1043 * while there is no upper layer payload to send at the time
1044 */
1045static int smbd_post_send_empty(struct smbd_connection *info)
1046{
1047 info->count_send_empty++;
1048 return smbd_post_send_sgl(info, NULL, 0, 0);
1049}
1050
1051/*
Long Lid649e1b2017-11-22 17:38:42 -07001052 * Send a data buffer
1053 * iov: the iov array describing the data buffers
1054 * n_vec: number of iov array
1055 * remaining_data_length: remaining data to send following this packet
1056 * in segmented SMBD packet
1057 */
1058static int smbd_post_send_data(
1059 struct smbd_connection *info, struct kvec *iov, int n_vec,
1060 int remaining_data_length)
1061{
1062 int i;
1063 u32 data_length = 0;
1064 struct scatterlist sgl[SMBDIRECT_MAX_SGE];
1065
1066 if (n_vec > SMBDIRECT_MAX_SGE) {
1067 cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
Long Li37941ea2019-10-16 13:51:52 -07001068 return -EINVAL;
Long Lid649e1b2017-11-22 17:38:42 -07001069 }
1070
1071 sg_init_table(sgl, n_vec);
1072 for (i = 0; i < n_vec; i++) {
1073 data_length += iov[i].iov_len;
1074 sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
1075 }
1076
1077 return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
1078}
1079
1080/*
Long Lif1981862017-11-04 18:17:24 -07001081 * Post a receive request to the transport
1082 * The remote peer can only send data when a receive request is posted
1083 * The interaction is controlled by send/receive credit system
1084 */
1085static int smbd_post_recv(
1086 struct smbd_connection *info, struct smbd_response *response)
1087{
Bart Van Assche73930592018-07-18 09:25:25 -07001088 struct ib_recv_wr recv_wr;
Long Lif1981862017-11-04 18:17:24 -07001089 int rc = -EIO;
1090
1091 response->sge.addr = ib_dma_map_single(
1092 info->id->device, response->packet,
1093 info->max_receive_size, DMA_FROM_DEVICE);
1094 if (ib_dma_mapping_error(info->id->device, response->sge.addr))
1095 return rc;
1096
1097 response->sge.length = info->max_receive_size;
1098 response->sge.lkey = info->pd->local_dma_lkey;
1099
1100 response->cqe.done = recv_done;
1101
1102 recv_wr.wr_cqe = &response->cqe;
1103 recv_wr.next = NULL;
1104 recv_wr.sg_list = &response->sge;
1105 recv_wr.num_sge = 1;
1106
Bart Van Assche73930592018-07-18 09:25:25 -07001107 rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
Long Lif1981862017-11-04 18:17:24 -07001108 if (rc) {
1109 ib_dma_unmap_single(info->id->device, response->sge.addr,
1110 response->sge.length, DMA_FROM_DEVICE);
Long Li21a4e142018-03-30 15:16:36 -07001111 smbd_disconnect_rdma_connection(info);
Long Lif1981862017-11-04 18:17:24 -07001112 log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
1113 }
1114
1115 return rc;
1116}
1117
1118/* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
1119static int smbd_negotiate(struct smbd_connection *info)
1120{
1121 int rc;
1122 struct smbd_response *response = get_receive_buffer(info);
1123
1124 response->type = SMBD_NEGOTIATE_RESP;
1125 rc = smbd_post_recv(info, response);
1126 log_rdma_event(INFO,
1127 "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x "
1128 "iov.lkey=%x\n",
1129 rc, response->sge.addr,
1130 response->sge.length, response->sge.lkey);
1131 if (rc)
1132 return rc;
1133
1134 init_completion(&info->negotiate_completion);
1135 info->negotiate_done = false;
1136 rc = smbd_post_send_negotiate_req(info);
1137 if (rc)
1138 return rc;
1139
1140 rc = wait_for_completion_interruptible_timeout(
1141 &info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
1142 log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
1143
1144 if (info->negotiate_done)
1145 return 0;
1146
1147 if (rc == 0)
1148 rc = -ETIMEDOUT;
1149 else if (rc == -ERESTARTSYS)
1150 rc = -EINTR;
1151 else
1152 rc = -ENOTCONN;
1153
1154 return rc;
1155}
1156
1157static void put_empty_packet(
1158 struct smbd_connection *info, struct smbd_response *response)
1159{
1160 spin_lock(&info->empty_packet_queue_lock);
1161 list_add_tail(&response->list, &info->empty_packet_queue);
1162 info->count_empty_packet_queue++;
1163 spin_unlock(&info->empty_packet_queue_lock);
1164
1165 queue_work(info->workqueue, &info->post_send_credits_work);
1166}
1167
1168/*
1169 * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
1170 * This is a queue for reassembling upper layer payload and present to upper
1171 * layer. All the inncoming payload go to the reassembly queue, regardless of
1172 * if reassembly is required. The uuper layer code reads from the queue for all
1173 * incoming payloads.
1174 * Put a received packet to the reassembly queue
1175 * response: the packet received
1176 * data_length: the size of payload in this packet
1177 */
1178static void enqueue_reassembly(
1179 struct smbd_connection *info,
1180 struct smbd_response *response,
1181 int data_length)
1182{
1183 spin_lock(&info->reassembly_queue_lock);
1184 list_add_tail(&response->list, &info->reassembly_queue);
1185 info->reassembly_queue_length++;
1186 /*
1187 * Make sure reassembly_data_length is updated after list and
1188 * reassembly_queue_length are updated. On the dequeue side
1189 * reassembly_data_length is checked without a lock to determine
1190 * if reassembly_queue_length and list is up to date
1191 */
1192 virt_wmb();
1193 info->reassembly_data_length += data_length;
1194 spin_unlock(&info->reassembly_queue_lock);
1195 info->count_reassembly_queue++;
1196 info->count_enqueue_reassembly_queue++;
1197}
1198
1199/*
1200 * Get the first entry at the front of reassembly queue
1201 * Caller is responsible for locking
1202 * return value: the first entry if any, NULL if queue is empty
1203 */
1204static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
1205{
1206 struct smbd_response *ret = NULL;
1207
1208 if (!list_empty(&info->reassembly_queue)) {
1209 ret = list_first_entry(
1210 &info->reassembly_queue,
1211 struct smbd_response, list);
1212 }
1213 return ret;
1214}
1215
1216static struct smbd_response *get_empty_queue_buffer(
1217 struct smbd_connection *info)
1218{
1219 struct smbd_response *ret = NULL;
1220 unsigned long flags;
1221
1222 spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
1223 if (!list_empty(&info->empty_packet_queue)) {
1224 ret = list_first_entry(
1225 &info->empty_packet_queue,
1226 struct smbd_response, list);
1227 list_del(&ret->list);
1228 info->count_empty_packet_queue--;
1229 }
1230 spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
1231
1232 return ret;
1233}
1234
1235/*
1236 * Get a receive buffer
1237 * For each remote send, we need to post a receive. The receive buffers are
1238 * pre-allocated in advance.
1239 * return value: the receive buffer, NULL if none is available
1240 */
1241static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
1242{
1243 struct smbd_response *ret = NULL;
1244 unsigned long flags;
1245
1246 spin_lock_irqsave(&info->receive_queue_lock, flags);
1247 if (!list_empty(&info->receive_queue)) {
1248 ret = list_first_entry(
1249 &info->receive_queue,
1250 struct smbd_response, list);
1251 list_del(&ret->list);
1252 info->count_receive_queue--;
1253 info->count_get_receive_buffer++;
1254 }
1255 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1256
1257 return ret;
1258}
1259
1260/*
1261 * Return a receive buffer
1262 * Upon returning of a receive buffer, we can post new receive and extend
1263 * more receive credits to remote peer. This is done immediately after a
1264 * receive buffer is returned.
1265 */
1266static void put_receive_buffer(
1267 struct smbd_connection *info, struct smbd_response *response)
1268{
1269 unsigned long flags;
1270
1271 ib_dma_unmap_single(info->id->device, response->sge.addr,
1272 response->sge.length, DMA_FROM_DEVICE);
1273
1274 spin_lock_irqsave(&info->receive_queue_lock, flags);
1275 list_add_tail(&response->list, &info->receive_queue);
1276 info->count_receive_queue++;
1277 info->count_put_receive_buffer++;
1278 spin_unlock_irqrestore(&info->receive_queue_lock, flags);
1279
1280 queue_work(info->workqueue, &info->post_send_credits_work);
1281}
1282
1283/* Preallocate all receive buffer on transport establishment */
1284static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
1285{
1286 int i;
1287 struct smbd_response *response;
1288
1289 INIT_LIST_HEAD(&info->reassembly_queue);
1290 spin_lock_init(&info->reassembly_queue_lock);
1291 info->reassembly_data_length = 0;
1292 info->reassembly_queue_length = 0;
1293
1294 INIT_LIST_HEAD(&info->receive_queue);
1295 spin_lock_init(&info->receive_queue_lock);
1296 info->count_receive_queue = 0;
1297
1298 INIT_LIST_HEAD(&info->empty_packet_queue);
1299 spin_lock_init(&info->empty_packet_queue_lock);
1300 info->count_empty_packet_queue = 0;
1301
1302 init_waitqueue_head(&info->wait_receive_queues);
1303
1304 for (i = 0; i < num_buf; i++) {
1305 response = mempool_alloc(info->response_mempool, GFP_KERNEL);
1306 if (!response)
1307 goto allocate_failed;
1308
1309 response->info = info;
1310 list_add_tail(&response->list, &info->receive_queue);
1311 info->count_receive_queue++;
1312 }
1313
1314 return 0;
1315
1316allocate_failed:
1317 while (!list_empty(&info->receive_queue)) {
1318 response = list_first_entry(
1319 &info->receive_queue,
1320 struct smbd_response, list);
1321 list_del(&response->list);
1322 info->count_receive_queue--;
1323
1324 mempool_free(response, info->response_mempool);
1325 }
1326 return -ENOMEM;
1327}
1328
1329static void destroy_receive_buffers(struct smbd_connection *info)
1330{
1331 struct smbd_response *response;
1332
1333 while ((response = get_receive_buffer(info)))
1334 mempool_free(response, info->response_mempool);
1335
1336 while ((response = get_empty_queue_buffer(info)))
1337 mempool_free(response, info->response_mempool);
1338}
1339
1340/*
1341 * Check and send an immediate or keep alive packet
1342 * The condition to send those packets are defined in [MS-SMBD] 3.1.1.1
1343 * Connection.KeepaliveRequested and Connection.SendImmediate
1344 * The idea is to extend credits to server as soon as it becomes available
1345 */
1346static void send_immediate_work(struct work_struct *work)
1347{
1348 struct smbd_connection *info = container_of(
1349 work, struct smbd_connection,
1350 send_immediate_work.work);
1351
1352 if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
1353 info->send_immediate) {
1354 log_keep_alive(INFO, "send an empty message\n");
1355 smbd_post_send_empty(info);
1356 }
1357}
1358
1359/* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
1360static void idle_connection_timer(struct work_struct *work)
1361{
1362 struct smbd_connection *info = container_of(
1363 work, struct smbd_connection,
1364 idle_timer_work.work);
1365
1366 if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
1367 log_keep_alive(ERR,
1368 "error status info->keep_alive_requested=%d\n",
1369 info->keep_alive_requested);
1370 smbd_disconnect_rdma_connection(info);
1371 return;
1372 }
1373
1374 log_keep_alive(INFO, "about to send an empty idle message\n");
1375 smbd_post_send_empty(info);
1376
1377 /* Setup the next idle timeout work */
1378 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1379 info->keep_alive_interval*HZ);
1380}
1381
Long Li050b8c32019-04-04 11:35:42 -05001382/*
1383 * Destroy the transport and related RDMA and memory resources
1384 * Need to go through all the pending counters and make sure on one is using
1385 * the transport while it is destroyed
1386 */
1387void smbd_destroy(struct TCP_Server_Info *server)
Long Li8ef130f2017-11-22 17:38:37 -07001388{
Long Li050b8c32019-04-04 11:35:42 -05001389 struct smbd_connection *info = server->smbd_conn;
1390 struct smbd_response *response;
1391 unsigned long flags;
1392
1393 if (!info) {
1394 log_rdma_event(INFO, "rdma session already destroyed\n");
1395 return;
1396 }
1397
Long Li8ef130f2017-11-22 17:38:37 -07001398 log_rdma_event(INFO, "destroying rdma session\n");
Long Li050b8c32019-04-04 11:35:42 -05001399 if (info->transport_status != SMBD_DISCONNECTED) {
1400 rdma_disconnect(server->smbd_conn->id);
1401 log_rdma_event(INFO, "wait for transport being disconnected\n");
Long Lie8b3bfe2019-04-05 21:36:31 +00001402 wait_event_interruptible(
Long Li050b8c32019-04-04 11:35:42 -05001403 info->disconn_wait,
1404 info->transport_status == SMBD_DISCONNECTED);
1405 }
Long Li8ef130f2017-11-22 17:38:37 -07001406
Long Li050b8c32019-04-04 11:35:42 -05001407 log_rdma_event(INFO, "destroying qp\n");
1408 ib_drain_qp(info->id->qp);
1409 rdma_destroy_qp(info->id);
Long Li8ef130f2017-11-22 17:38:37 -07001410
Long Li050b8c32019-04-04 11:35:42 -05001411 log_rdma_event(INFO, "cancelling idle timer\n");
1412 cancel_delayed_work_sync(&info->idle_timer_work);
1413 log_rdma_event(INFO, "cancelling send immediate work\n");
1414 cancel_delayed_work_sync(&info->send_immediate_work);
1415
1416 log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
1417 wait_event(info->wait_send_pending,
1418 atomic_read(&info->send_pending) == 0);
Long Li050b8c32019-04-04 11:35:42 -05001419
1420 /* It's not posssible for upper layer to get to reassembly */
1421 log_rdma_event(INFO, "drain the reassembly queue\n");
1422 do {
1423 spin_lock_irqsave(&info->reassembly_queue_lock, flags);
1424 response = _get_first_reassembly(info);
1425 if (response) {
1426 list_del(&response->list);
1427 spin_unlock_irqrestore(
1428 &info->reassembly_queue_lock, flags);
1429 put_receive_buffer(info, response);
1430 } else
1431 spin_unlock_irqrestore(
1432 &info->reassembly_queue_lock, flags);
1433 } while (response);
1434 info->reassembly_data_length = 0;
1435
1436 log_rdma_event(INFO, "free receive buffers\n");
1437 wait_event(info->wait_receive_queues,
1438 info->count_receive_queue + info->count_empty_packet_queue
1439 == info->receive_credit_max);
1440 destroy_receive_buffers(info);
1441
1442 /*
1443 * For performance reasons, memory registration and deregistration
1444 * are not locked by srv_mutex. It is possible some processes are
1445 * blocked on transport srv_mutex while holding memory registration.
1446 * Release the transport srv_mutex to allow them to hit the failure
1447 * path when sending data, and then release memory registartions.
1448 */
1449 log_rdma_event(INFO, "freeing mr list\n");
1450 wake_up_interruptible_all(&info->wait_mr);
1451 while (atomic_read(&info->mr_used_count)) {
1452 mutex_unlock(&server->srv_mutex);
1453 msleep(1000);
1454 mutex_lock(&server->srv_mutex);
1455 }
1456 destroy_mr_list(info);
1457
1458 ib_free_cq(info->send_cq);
1459 ib_free_cq(info->recv_cq);
1460 ib_dealloc_pd(info->pd);
1461 rdma_destroy_id(info->id);
1462
1463 /* free mempools */
1464 mempool_destroy(info->request_mempool);
1465 kmem_cache_destroy(info->request_cache);
1466
1467 mempool_destroy(info->response_mempool);
1468 kmem_cache_destroy(info->response_cache);
1469
1470 info->transport_status = SMBD_DESTROYED;
Long Li8ef130f2017-11-22 17:38:37 -07001471
1472 destroy_workqueue(info->workqueue);
Long Lid63cdba2019-10-16 13:51:53 -07001473 log_rdma_event(INFO, "rdma session destroyed\n");
Long Li8ef130f2017-11-22 17:38:37 -07001474 kfree(info);
1475}
1476
Long Liad57b8e2017-11-22 17:38:35 -07001477/*
1478 * Reconnect this SMBD connection, called from upper layer
1479 * return value: 0 on success, or actual error code
1480 */
1481int smbd_reconnect(struct TCP_Server_Info *server)
1482{
1483 log_rdma_event(INFO, "reconnecting rdma session\n");
1484
1485 if (!server->smbd_conn) {
Long Li48f238a2018-03-30 15:16:35 -07001486 log_rdma_event(INFO, "rdma session already destroyed\n");
1487 goto create_conn;
Long Liad57b8e2017-11-22 17:38:35 -07001488 }
1489
1490 /*
1491 * This is possible if transport is disconnected and we haven't received
1492 * notification from RDMA, but upper layer has detected timeout
1493 */
1494 if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
1495 log_rdma_event(INFO, "disconnecting transport\n");
Long Li050b8c32019-04-04 11:35:42 -05001496 smbd_destroy(server);
Long Liad57b8e2017-11-22 17:38:35 -07001497 }
1498
Long Li48f238a2018-03-30 15:16:35 -07001499create_conn:
Long Liad57b8e2017-11-22 17:38:35 -07001500 log_rdma_event(INFO, "creating rdma session\n");
1501 server->smbd_conn = smbd_get_connection(
1502 server, (struct sockaddr *) &server->dstaddr);
Long Lid63cdba2019-10-16 13:51:53 -07001503
1504 if (server->smbd_conn)
1505 cifs_dbg(VFS, "RDMA transport re-established\n");
Long Liad57b8e2017-11-22 17:38:35 -07001506
1507 return server->smbd_conn ? 0 : -ENOENT;
1508}
1509
Long Lif1981862017-11-04 18:17:24 -07001510static void destroy_caches_and_workqueue(struct smbd_connection *info)
1511{
1512 destroy_receive_buffers(info);
1513 destroy_workqueue(info->workqueue);
1514 mempool_destroy(info->response_mempool);
1515 kmem_cache_destroy(info->response_cache);
1516 mempool_destroy(info->request_mempool);
1517 kmem_cache_destroy(info->request_cache);
1518}
1519
1520#define MAX_NAME_LEN 80
1521static int allocate_caches_and_workqueue(struct smbd_connection *info)
1522{
1523 char name[MAX_NAME_LEN];
1524 int rc;
1525
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001526 scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001527 info->request_cache =
1528 kmem_cache_create(
1529 name,
1530 sizeof(struct smbd_request) +
1531 sizeof(struct smbd_data_transfer),
1532 0, SLAB_HWCACHE_ALIGN, NULL);
1533 if (!info->request_cache)
1534 return -ENOMEM;
1535
1536 info->request_mempool =
1537 mempool_create(info->send_credit_target, mempool_alloc_slab,
1538 mempool_free_slab, info->request_cache);
1539 if (!info->request_mempool)
1540 goto out1;
1541
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001542 scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001543 info->response_cache =
1544 kmem_cache_create(
1545 name,
1546 sizeof(struct smbd_response) +
1547 info->max_receive_size,
1548 0, SLAB_HWCACHE_ALIGN, NULL);
1549 if (!info->response_cache)
1550 goto out2;
1551
1552 info->response_mempool =
1553 mempool_create(info->receive_credit_max, mempool_alloc_slab,
1554 mempool_free_slab, info->response_cache);
1555 if (!info->response_mempool)
1556 goto out3;
1557
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10001558 scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
Long Lif1981862017-11-04 18:17:24 -07001559 info->workqueue = create_workqueue(name);
1560 if (!info->workqueue)
1561 goto out4;
1562
1563 rc = allocate_receive_buffers(info, info->receive_credit_max);
1564 if (rc) {
1565 log_rdma_event(ERR, "failed to allocate receive buffers\n");
1566 goto out5;
1567 }
1568
1569 return 0;
1570
1571out5:
1572 destroy_workqueue(info->workqueue);
1573out4:
1574 mempool_destroy(info->response_mempool);
1575out3:
1576 kmem_cache_destroy(info->response_cache);
1577out2:
1578 mempool_destroy(info->request_mempool);
1579out1:
1580 kmem_cache_destroy(info->request_cache);
1581 return -ENOMEM;
1582}
1583
1584/* Create a SMBD connection, called by upper layer */
kbuild test robot90844322017-12-18 21:30:06 +08001585static struct smbd_connection *_smbd_get_connection(
Long Lif1981862017-11-04 18:17:24 -07001586 struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
1587{
1588 int rc;
1589 struct smbd_connection *info;
1590 struct rdma_conn_param conn_param;
1591 struct ib_qp_init_attr qp_attr;
1592 struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
Long Lic7398582017-11-22 17:38:44 -07001593 struct ib_port_immutable port_immutable;
1594 u32 ird_ord_hdr[2];
Long Lif1981862017-11-04 18:17:24 -07001595
1596 info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
1597 if (!info)
1598 return NULL;
1599
1600 info->transport_status = SMBD_CONNECTING;
1601 rc = smbd_ia_open(info, dstaddr, port);
1602 if (rc) {
1603 log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
1604 goto create_id_failed;
1605 }
1606
1607 if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
1608 smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
1609 log_rdma_event(ERR,
1610 "consider lowering send_credit_target = %d. "
1611 "Possible CQE overrun, device "
1612 "reporting max_cpe %d max_qp_wr %d\n",
1613 smbd_send_credit_target,
1614 info->id->device->attrs.max_cqe,
1615 info->id->device->attrs.max_qp_wr);
1616 goto config_failed;
1617 }
1618
1619 if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
1620 smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
1621 log_rdma_event(ERR,
1622 "consider lowering receive_credit_max = %d. "
1623 "Possible CQE overrun, device "
1624 "reporting max_cpe %d max_qp_wr %d\n",
1625 smbd_receive_credit_max,
1626 info->id->device->attrs.max_cqe,
1627 info->id->device->attrs.max_qp_wr);
1628 goto config_failed;
1629 }
1630
1631 info->receive_credit_max = smbd_receive_credit_max;
1632 info->send_credit_target = smbd_send_credit_target;
1633 info->max_send_size = smbd_max_send_size;
1634 info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
1635 info->max_receive_size = smbd_max_receive_size;
1636 info->keep_alive_interval = smbd_keep_alive_interval;
1637
Steve Wise33023fb2018-06-18 08:05:26 -07001638 if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
1639 log_rdma_event(ERR,
1640 "warning: device max_send_sge = %d too small\n",
1641 info->id->device->attrs.max_send_sge);
1642 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1643 }
1644 if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
1645 log_rdma_event(ERR,
1646 "warning: device max_recv_sge = %d too small\n",
1647 info->id->device->attrs.max_recv_sge);
Long Lif1981862017-11-04 18:17:24 -07001648 log_rdma_event(ERR, "Queue Pair creation may fail\n");
1649 }
1650
1651 info->send_cq = NULL;
1652 info->recv_cq = NULL;
Chuck Lever20cf4e02019-07-29 13:22:09 -04001653 info->send_cq =
1654 ib_alloc_cq_any(info->id->device, info,
1655 info->send_credit_target, IB_POLL_SOFTIRQ);
Long Lif1981862017-11-04 18:17:24 -07001656 if (IS_ERR(info->send_cq)) {
1657 info->send_cq = NULL;
1658 goto alloc_cq_failed;
1659 }
1660
Chuck Lever20cf4e02019-07-29 13:22:09 -04001661 info->recv_cq =
1662 ib_alloc_cq_any(info->id->device, info,
1663 info->receive_credit_max, IB_POLL_SOFTIRQ);
Long Lif1981862017-11-04 18:17:24 -07001664 if (IS_ERR(info->recv_cq)) {
1665 info->recv_cq = NULL;
1666 goto alloc_cq_failed;
1667 }
1668
1669 memset(&qp_attr, 0, sizeof(qp_attr));
1670 qp_attr.event_handler = smbd_qp_async_error_upcall;
1671 qp_attr.qp_context = info;
1672 qp_attr.cap.max_send_wr = info->send_credit_target;
1673 qp_attr.cap.max_recv_wr = info->receive_credit_max;
1674 qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
1675 qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
1676 qp_attr.cap.max_inline_data = 0;
1677 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1678 qp_attr.qp_type = IB_QPT_RC;
1679 qp_attr.send_cq = info->send_cq;
1680 qp_attr.recv_cq = info->recv_cq;
1681 qp_attr.port_num = ~0;
1682
1683 rc = rdma_create_qp(info->id, info->pd, &qp_attr);
1684 if (rc) {
1685 log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
1686 goto create_qp_failed;
1687 }
1688
1689 memset(&conn_param, 0, sizeof(conn_param));
1690 conn_param.initiator_depth = 0;
1691
Long Lic7398582017-11-22 17:38:44 -07001692 conn_param.responder_resources =
1693 info->id->device->attrs.max_qp_rd_atom
1694 < SMBD_CM_RESPONDER_RESOURCES ?
1695 info->id->device->attrs.max_qp_rd_atom :
1696 SMBD_CM_RESPONDER_RESOURCES;
1697 info->responder_resources = conn_param.responder_resources;
1698 log_rdma_mr(INFO, "responder_resources=%d\n",
1699 info->responder_resources);
1700
1701 /* Need to send IRD/ORD in private data for iWARP */
Kamal Heib3023a1e2018-12-10 21:09:48 +02001702 info->id->device->ops.get_port_immutable(
Long Lic7398582017-11-22 17:38:44 -07001703 info->id->device, info->id->port_num, &port_immutable);
1704 if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
1705 ird_ord_hdr[0] = info->responder_resources;
1706 ird_ord_hdr[1] = 1;
1707 conn_param.private_data = ird_ord_hdr;
1708 conn_param.private_data_len = sizeof(ird_ord_hdr);
1709 } else {
1710 conn_param.private_data = NULL;
1711 conn_param.private_data_len = 0;
1712 }
1713
Long Lif1981862017-11-04 18:17:24 -07001714 conn_param.retry_count = SMBD_CM_RETRY;
1715 conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
1716 conn_param.flow_control = 0;
Long Lif1981862017-11-04 18:17:24 -07001717
1718 log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
1719 &addr_in->sin_addr, port);
1720
1721 init_waitqueue_head(&info->conn_wait);
Long Li050b8c32019-04-04 11:35:42 -05001722 init_waitqueue_head(&info->disconn_wait);
1723 init_waitqueue_head(&info->wait_reassembly_queue);
Long Lif1981862017-11-04 18:17:24 -07001724 rc = rdma_connect(info->id, &conn_param);
1725 if (rc) {
1726 log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
1727 goto rdma_connect_failed;
1728 }
1729
1730 wait_event_interruptible(
1731 info->conn_wait, info->transport_status != SMBD_CONNECTING);
1732
1733 if (info->transport_status != SMBD_CONNECTED) {
1734 log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
1735 goto rdma_connect_failed;
1736 }
1737
1738 log_rdma_event(INFO, "rdma_connect connected\n");
1739
1740 rc = allocate_caches_and_workqueue(info);
1741 if (rc) {
1742 log_rdma_event(ERR, "cache allocation failed\n");
1743 goto allocate_cache_failed;
1744 }
1745
1746 init_waitqueue_head(&info->wait_send_queue);
Long Lif1981862017-11-04 18:17:24 -07001747 INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
1748 INIT_DELAYED_WORK(&info->send_immediate_work, send_immediate_work);
1749 queue_delayed_work(info->workqueue, &info->idle_timer_work,
1750 info->keep_alive_interval*HZ);
1751
1752 init_waitqueue_head(&info->wait_send_pending);
1753 atomic_set(&info->send_pending, 0);
1754
Long Li3ffbe782020-03-30 11:04:07 -07001755 init_waitqueue_head(&info->wait_post_send);
Long Lif1981862017-11-04 18:17:24 -07001756
1757 INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
Long Lif1981862017-11-04 18:17:24 -07001758 INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
1759 info->new_credits_offered = 0;
1760 spin_lock_init(&info->lock_new_credits_offered);
1761
1762 rc = smbd_negotiate(info);
1763 if (rc) {
1764 log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
1765 goto negotiation_failed;
1766 }
1767
Long Lic7398582017-11-22 17:38:44 -07001768 rc = allocate_mr_list(info);
1769 if (rc) {
1770 log_rdma_mr(ERR, "memory registration allocation failed\n");
1771 goto allocate_mr_failed;
1772 }
1773
Long Lif1981862017-11-04 18:17:24 -07001774 return info;
1775
Long Lic7398582017-11-22 17:38:44 -07001776allocate_mr_failed:
1777 /* At this point, need to a full transport shutdown */
Long Li050b8c32019-04-04 11:35:42 -05001778 smbd_destroy(server);
Long Lic7398582017-11-22 17:38:44 -07001779 return NULL;
1780
Long Lif1981862017-11-04 18:17:24 -07001781negotiation_failed:
1782 cancel_delayed_work_sync(&info->idle_timer_work);
1783 destroy_caches_and_workqueue(info);
1784 info->transport_status = SMBD_NEGOTIATE_FAILED;
1785 init_waitqueue_head(&info->conn_wait);
1786 rdma_disconnect(info->id);
1787 wait_event(info->conn_wait,
1788 info->transport_status == SMBD_DISCONNECTED);
1789
1790allocate_cache_failed:
1791rdma_connect_failed:
1792 rdma_destroy_qp(info->id);
1793
1794create_qp_failed:
1795alloc_cq_failed:
1796 if (info->send_cq)
1797 ib_free_cq(info->send_cq);
1798 if (info->recv_cq)
1799 ib_free_cq(info->recv_cq);
1800
1801config_failed:
1802 ib_dealloc_pd(info->pd);
1803 rdma_destroy_id(info->id);
1804
1805create_id_failed:
1806 kfree(info);
1807 return NULL;
1808}
Long Li399f9532017-11-17 17:26:52 -08001809
1810struct smbd_connection *smbd_get_connection(
1811 struct TCP_Server_Info *server, struct sockaddr *dstaddr)
1812{
1813 struct smbd_connection *ret;
1814 int port = SMBD_PORT;
1815
1816try_again:
1817 ret = _smbd_get_connection(server, dstaddr, port);
1818
1819 /* Try SMB_PORT if SMBD_PORT doesn't work */
1820 if (!ret && port == SMBD_PORT) {
1821 port = SMB_PORT;
1822 goto try_again;
1823 }
1824 return ret;
1825}
Long Lif64b78f2017-11-22 17:38:40 -07001826
1827/*
1828 * Receive data from receive reassembly queue
1829 * All the incoming data packets are placed in reassembly queue
1830 * buf: the buffer to read data into
1831 * size: the length of data to read
1832 * return value: actual data read
1833 * Note: this implementation copies the data from reassebmly queue to receive
1834 * buffers used by upper layer. This is not the optimal code path. A better way
1835 * to do it is to not have upper layer allocate its receive buffers but rather
1836 * borrow the buffer from reassembly queue, and return it after data is
1837 * consumed. But this will require more changes to upper layer code, and also
1838 * need to consider packet boundaries while they still being reassembled.
1839 */
Steve French2026b062018-01-24 23:07:41 -06001840static int smbd_recv_buf(struct smbd_connection *info, char *buf,
1841 unsigned int size)
Long Lif64b78f2017-11-22 17:38:40 -07001842{
1843 struct smbd_response *response;
1844 struct smbd_data_transfer *data_transfer;
1845 int to_copy, to_read, data_read, offset;
1846 u32 data_length, remaining_data_length, data_offset;
1847 int rc;
Long Lif64b78f2017-11-22 17:38:40 -07001848
1849again:
Long Lif64b78f2017-11-22 17:38:40 -07001850 /*
1851 * No need to hold the reassembly queue lock all the time as we are
1852 * the only one reading from the front of the queue. The transport
1853 * may add more entries to the back of the queue at the same time
1854 */
1855 log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
1856 info->reassembly_data_length);
1857 if (info->reassembly_data_length >= size) {
1858 int queue_length;
1859 int queue_removed = 0;
1860
1861 /*
1862 * Need to make sure reassembly_data_length is read before
1863 * reading reassembly_queue_length and calling
1864 * _get_first_reassembly. This call is lock free
1865 * as we never read at the end of the queue which are being
1866 * updated in SOFTIRQ as more data is received
1867 */
1868 virt_rmb();
1869 queue_length = info->reassembly_queue_length;
1870 data_read = 0;
1871 to_read = size;
1872 offset = info->first_entry_offset;
1873 while (data_read < size) {
1874 response = _get_first_reassembly(info);
1875 data_transfer = smbd_response_payload(response);
1876 data_length = le32_to_cpu(data_transfer->data_length);
1877 remaining_data_length =
1878 le32_to_cpu(
1879 data_transfer->remaining_data_length);
1880 data_offset = le32_to_cpu(data_transfer->data_offset);
1881
1882 /*
1883 * The upper layer expects RFC1002 length at the
1884 * beginning of the payload. Return it to indicate
1885 * the total length of the packet. This minimize the
1886 * change to upper layer packet processing logic. This
1887 * will be eventually remove when an intermediate
1888 * transport layer is added
1889 */
1890 if (response->first_segment && size == 4) {
1891 unsigned int rfc1002_len =
1892 data_length + remaining_data_length;
1893 *((__be32 *)buf) = cpu_to_be32(rfc1002_len);
1894 data_read = 4;
1895 response->first_segment = false;
1896 log_read(INFO, "returning rfc1002 length %d\n",
1897 rfc1002_len);
1898 goto read_rfc1002_done;
1899 }
1900
1901 to_copy = min_t(int, data_length - offset, to_read);
1902 memcpy(
1903 buf + data_read,
1904 (char *)data_transfer + data_offset + offset,
1905 to_copy);
1906
1907 /* move on to the next buffer? */
1908 if (to_copy == data_length - offset) {
1909 queue_length--;
1910 /*
1911 * No need to lock if we are not at the
1912 * end of the queue
1913 */
Steve Frenchf9de1512018-02-03 19:45:07 -06001914 if (queue_length)
1915 list_del(&response->list);
1916 else {
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001917 spin_lock_irq(
1918 &info->reassembly_queue_lock);
Steve Frenchf9de1512018-02-03 19:45:07 -06001919 list_del(&response->list);
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001920 spin_unlock_irq(
1921 &info->reassembly_queue_lock);
Steve Frenchf9de1512018-02-03 19:45:07 -06001922 }
1923 queue_removed++;
Long Lif64b78f2017-11-22 17:38:40 -07001924 info->count_reassembly_queue--;
1925 info->count_dequeue_reassembly_queue++;
1926 put_receive_buffer(info, response);
1927 offset = 0;
1928 log_read(INFO, "put_receive_buffer offset=0\n");
1929 } else
1930 offset += to_copy;
1931
1932 to_read -= to_copy;
1933 data_read += to_copy;
1934
1935 log_read(INFO, "_get_first_reassembly memcpy %d bytes "
1936 "data_transfer_length-offset=%d after that "
1937 "to_read=%d data_read=%d offset=%d\n",
1938 to_copy, data_length - offset,
1939 to_read, data_read, offset);
1940 }
1941
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001942 spin_lock_irq(&info->reassembly_queue_lock);
Long Lif64b78f2017-11-22 17:38:40 -07001943 info->reassembly_data_length -= data_read;
1944 info->reassembly_queue_length -= queue_removed;
Arnd Bergmanne36c0482018-01-10 21:51:05 +01001945 spin_unlock_irq(&info->reassembly_queue_lock);
Long Lif64b78f2017-11-22 17:38:40 -07001946
1947 info->first_entry_offset = offset;
1948 log_read(INFO, "returning to thread data_read=%d "
1949 "reassembly_data_length=%d first_entry_offset=%d\n",
1950 data_read, info->reassembly_data_length,
1951 info->first_entry_offset);
1952read_rfc1002_done:
1953 return data_read;
1954 }
1955
1956 log_read(INFO, "wait_event on more data\n");
1957 rc = wait_event_interruptible(
1958 info->wait_reassembly_queue,
1959 info->reassembly_data_length >= size ||
1960 info->transport_status != SMBD_CONNECTED);
1961 /* Don't return any data if interrupted */
1962 if (rc)
Long Li98e0d402019-04-05 21:36:32 +00001963 return rc;
Long Lif64b78f2017-11-22 17:38:40 -07001964
Long Lie8b3bfe2019-04-05 21:36:31 +00001965 if (info->transport_status != SMBD_CONNECTED) {
1966 log_read(ERR, "disconnected\n");
Long Liacd46802019-10-16 13:51:54 -07001967 return -ECONNABORTED;
Long Lie8b3bfe2019-04-05 21:36:31 +00001968 }
1969
Long Lif64b78f2017-11-22 17:38:40 -07001970 goto again;
1971}
1972
1973/*
1974 * Receive a page from receive reassembly queue
1975 * page: the page to read data into
1976 * to_read: the length of data to read
1977 * return value: actual data read
1978 */
Steve French2026b062018-01-24 23:07:41 -06001979static int smbd_recv_page(struct smbd_connection *info,
Long Li6509f502018-05-30 12:48:01 -07001980 struct page *page, unsigned int page_offset,
1981 unsigned int to_read)
Long Lif64b78f2017-11-22 17:38:40 -07001982{
1983 int ret;
1984 char *to_address;
Long Li6509f502018-05-30 12:48:01 -07001985 void *page_address;
Long Lif64b78f2017-11-22 17:38:40 -07001986
1987 /* make sure we have the page ready for read */
1988 ret = wait_event_interruptible(
1989 info->wait_reassembly_queue,
1990 info->reassembly_data_length >= to_read ||
1991 info->transport_status != SMBD_CONNECTED);
1992 if (ret)
Long Li6509f502018-05-30 12:48:01 -07001993 return ret;
Long Lif64b78f2017-11-22 17:38:40 -07001994
1995 /* now we can read from reassembly queue and not sleep */
Long Li6509f502018-05-30 12:48:01 -07001996 page_address = kmap_atomic(page);
1997 to_address = (char *) page_address + page_offset;
Long Lif64b78f2017-11-22 17:38:40 -07001998
1999 log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
2000 page, to_address, to_read);
2001
2002 ret = smbd_recv_buf(info, to_address, to_read);
Long Li6509f502018-05-30 12:48:01 -07002003 kunmap_atomic(page_address);
Long Lif64b78f2017-11-22 17:38:40 -07002004
2005 return ret;
2006}
2007
2008/*
2009 * Receive data from transport
2010 * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
2011 * return: total bytes read, or 0. SMB Direct will not do partial read.
2012 */
2013int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
2014{
2015 char *buf;
2016 struct page *page;
Long Li6509f502018-05-30 12:48:01 -07002017 unsigned int to_read, page_offset;
Long Lif64b78f2017-11-22 17:38:40 -07002018 int rc;
2019
David Howells00e23702018-10-22 13:07:28 +01002020 if (iov_iter_rw(&msg->msg_iter) == WRITE) {
2021 /* It's a bug in upper layer to get there */
2022 cifs_dbg(VFS, "CIFS: invalid msg iter dir %u\n",
2023 iov_iter_rw(&msg->msg_iter));
2024 rc = -EINVAL;
2025 goto out;
2026 }
2027
2028 switch (iov_iter_type(&msg->msg_iter)) {
2029 case ITER_KVEC:
Long Lif64b78f2017-11-22 17:38:40 -07002030 buf = msg->msg_iter.kvec->iov_base;
2031 to_read = msg->msg_iter.kvec->iov_len;
2032 rc = smbd_recv_buf(info, buf, to_read);
2033 break;
2034
David Howells00e23702018-10-22 13:07:28 +01002035 case ITER_BVEC:
Long Lif64b78f2017-11-22 17:38:40 -07002036 page = msg->msg_iter.bvec->bv_page;
Long Li6509f502018-05-30 12:48:01 -07002037 page_offset = msg->msg_iter.bvec->bv_offset;
Long Lif64b78f2017-11-22 17:38:40 -07002038 to_read = msg->msg_iter.bvec->bv_len;
Long Li6509f502018-05-30 12:48:01 -07002039 rc = smbd_recv_page(info, page, page_offset, to_read);
Long Lif64b78f2017-11-22 17:38:40 -07002040 break;
2041
2042 default:
2043 /* It's a bug in upper layer to get there */
2044 cifs_dbg(VFS, "CIFS: invalid msg type %d\n",
David Howells00e23702018-10-22 13:07:28 +01002045 iov_iter_type(&msg->msg_iter));
Long Li6509f502018-05-30 12:48:01 -07002046 rc = -EINVAL;
Long Lif64b78f2017-11-22 17:38:40 -07002047 }
2048
David Howells00e23702018-10-22 13:07:28 +01002049out:
Long Lif64b78f2017-11-22 17:38:40 -07002050 /* SMBDirect will read it all or nothing */
2051 if (rc > 0)
2052 msg->msg_iter.count = 0;
2053 return rc;
2054}
Long Lid649e1b2017-11-22 17:38:42 -07002055
2056/*
2057 * Send data to transport
2058 * Each rqst is transported as a SMBDirect payload
2059 * rqst: the data to write
2060 * return value: 0 if successfully write, otherwise error code
2061 */
Long Li4739f232019-04-15 14:49:17 -07002062int smbd_send(struct TCP_Server_Info *server,
2063 int num_rqst, struct smb_rqst *rqst_array)
Long Lid649e1b2017-11-22 17:38:42 -07002064{
Ronnie Sahlberg81f39f92018-06-28 10:47:14 +10002065 struct smbd_connection *info = server->smbd_conn;
Long Lid649e1b2017-11-22 17:38:42 -07002066 struct kvec vec;
2067 int nvecs;
2068 int size;
Paulo Alcantara35e2cc12018-06-15 10:22:44 -03002069 unsigned int buflen, remaining_data_length;
Long Lid649e1b2017-11-22 17:38:42 -07002070 int start, i, j;
2071 int max_iov_size =
2072 info->max_send_size - sizeof(struct smbd_data_transfer);
Long Li8bcda1d2018-04-17 12:17:07 -07002073 struct kvec *iov;
Long Lid649e1b2017-11-22 17:38:42 -07002074 int rc;
Long Li4739f232019-04-15 14:49:17 -07002075 struct smb_rqst *rqst;
2076 int rqst_idx;
Long Lid649e1b2017-11-22 17:38:42 -07002077
Long Lid649e1b2017-11-22 17:38:42 -07002078 if (info->transport_status != SMBD_CONNECTED) {
Long Li62fdf672019-04-05 21:36:33 +00002079 rc = -EAGAIN;
Long Lid649e1b2017-11-22 17:38:42 -07002080 goto done;
2081 }
2082
2083 /*
Long Lib6903bc2018-05-30 12:48:00 -07002084 * Add in the page array if there is one. The caller needs to set
2085 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
2086 * ends at page boundary
2087 */
Long Li4739f232019-04-15 14:49:17 -07002088 remaining_data_length = 0;
2089 for (i = 0; i < num_rqst; i++)
2090 remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
Long Lid649e1b2017-11-22 17:38:42 -07002091
Long Lif7950cb2020-03-26 19:42:24 -07002092 if (remaining_data_length > info->max_fragmented_send_size) {
Long Lid649e1b2017-11-22 17:38:42 -07002093 log_write(ERR, "payload size %d > max size %d\n",
Long Li4739f232019-04-15 14:49:17 -07002094 remaining_data_length, info->max_fragmented_send_size);
Long Lid649e1b2017-11-22 17:38:42 -07002095 rc = -EINVAL;
2096 goto done;
2097 }
2098
Long Li7f46d232019-05-13 21:01:29 -07002099 log_write(INFO, "num_rqst=%d total length=%u\n",
2100 num_rqst, remaining_data_length);
Paulo Alcantara35e2cc12018-06-15 10:22:44 -03002101
Long Li7f46d232019-05-13 21:01:29 -07002102 rqst_idx = 0;
Long Li4739f232019-04-15 14:49:17 -07002103next_rqst:
2104 rqst = &rqst_array[rqst_idx];
2105 iov = rqst->rq_iov;
2106
2107 cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
2108 rqst_idx, smb_rqst_len(server, rqst));
2109 for (i = 0; i < rqst->rq_nvec; i++)
Long Liff30b892018-04-17 12:17:10 -07002110 dump_smb(iov[i].iov_base, iov[i].iov_len);
2111
Long Lid649e1b2017-11-22 17:38:42 -07002112
Long Li4739f232019-04-15 14:49:17 -07002113 log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d "
2114 "rq_tailsz=%d buflen=%lu\n",
2115 rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
2116 rqst->rq_tailsz, smb_rqst_len(server, rqst));
Long Lid649e1b2017-11-22 17:38:42 -07002117
Long Li4739f232019-04-15 14:49:17 -07002118 start = i = 0;
Long Lid649e1b2017-11-22 17:38:42 -07002119 buflen = 0;
2120 while (true) {
2121 buflen += iov[i].iov_len;
2122 if (buflen > max_iov_size) {
2123 if (i > start) {
2124 remaining_data_length -=
2125 (buflen-iov[i].iov_len);
2126 log_write(INFO, "sending iov[] from start=%d "
2127 "i=%d nvecs=%d "
2128 "remaining_data_length=%d\n",
2129 start, i, i-start,
2130 remaining_data_length);
2131 rc = smbd_post_send_data(
2132 info, &iov[start], i-start,
2133 remaining_data_length);
2134 if (rc)
2135 goto done;
2136 } else {
2137 /* iov[start] is too big, break it */
2138 nvecs = (buflen+max_iov_size-1)/max_iov_size;
2139 log_write(INFO, "iov[%d] iov_base=%p buflen=%d"
2140 " break to %d vectors\n",
2141 start, iov[start].iov_base,
2142 buflen, nvecs);
2143 for (j = 0; j < nvecs; j++) {
2144 vec.iov_base =
2145 (char *)iov[start].iov_base +
2146 j*max_iov_size;
2147 vec.iov_len = max_iov_size;
2148 if (j == nvecs-1)
2149 vec.iov_len =
2150 buflen -
2151 max_iov_size*(nvecs-1);
2152 remaining_data_length -= vec.iov_len;
2153 log_write(INFO,
2154 "sending vec j=%d iov_base=%p"
2155 " iov_len=%zu "
2156 "remaining_data_length=%d\n",
2157 j, vec.iov_base, vec.iov_len,
2158 remaining_data_length);
2159 rc = smbd_post_send_data(
2160 info, &vec, 1,
2161 remaining_data_length);
2162 if (rc)
2163 goto done;
2164 }
2165 i++;
Long Li4739f232019-04-15 14:49:17 -07002166 if (i == rqst->rq_nvec)
Long Liab60ee72018-04-17 12:17:05 -07002167 break;
Long Lid649e1b2017-11-22 17:38:42 -07002168 }
2169 start = i;
2170 buflen = 0;
2171 } else {
2172 i++;
Long Li4739f232019-04-15 14:49:17 -07002173 if (i == rqst->rq_nvec) {
Long Lid649e1b2017-11-22 17:38:42 -07002174 /* send out all remaining vecs */
2175 remaining_data_length -= buflen;
2176 log_write(INFO,
2177 "sending iov[] from start=%d i=%d "
2178 "nvecs=%d remaining_data_length=%d\n",
2179 start, i, i-start,
2180 remaining_data_length);
2181 rc = smbd_post_send_data(info, &iov[start],
2182 i-start, remaining_data_length);
2183 if (rc)
2184 goto done;
2185 break;
2186 }
2187 }
2188 log_write(INFO, "looping i=%d buflen=%d\n", i, buflen);
2189 }
2190
2191 /* now sending pages if there are any */
2192 for (i = 0; i < rqst->rq_npages; i++) {
Long Lib6903bc2018-05-30 12:48:00 -07002193 unsigned int offset;
2194
2195 rqst_page_get_length(rqst, i, &buflen, &offset);
Long Lid649e1b2017-11-22 17:38:42 -07002196 nvecs = (buflen + max_iov_size - 1) / max_iov_size;
2197 log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
2198 buflen, nvecs);
2199 for (j = 0; j < nvecs; j++) {
2200 size = max_iov_size;
2201 if (j == nvecs-1)
2202 size = buflen - j*max_iov_size;
2203 remaining_data_length -= size;
2204 log_write(INFO, "sending pages i=%d offset=%d size=%d"
2205 " remaining_data_length=%d\n",
Long Lib6903bc2018-05-30 12:48:00 -07002206 i, j*max_iov_size+offset, size,
2207 remaining_data_length);
Long Lid649e1b2017-11-22 17:38:42 -07002208 rc = smbd_post_send_page(
Long Lib6903bc2018-05-30 12:48:00 -07002209 info, rqst->rq_pages[i],
2210 j*max_iov_size + offset,
Long Lid649e1b2017-11-22 17:38:42 -07002211 size, remaining_data_length);
2212 if (rc)
2213 goto done;
2214 }
2215 }
2216
Long Li4739f232019-04-15 14:49:17 -07002217 rqst_idx++;
2218 if (rqst_idx < num_rqst)
2219 goto next_rqst;
2220
Long Lid649e1b2017-11-22 17:38:42 -07002221done:
2222 /*
2223 * As an optimization, we don't wait for individual I/O to finish
2224 * before sending the next one.
2225 * Send them all and wait for pending send count to get to 0
2226 * that means all the I/Os have been out and we are good to return
2227 */
2228
Long Li072a14e2020-03-30 11:04:06 -07002229 wait_event(info->wait_send_pending,
2230 atomic_read(&info->send_pending) == 0);
Long Lid649e1b2017-11-22 17:38:42 -07002231
Long Lid649e1b2017-11-22 17:38:42 -07002232 return rc;
2233}
Long Lic7398582017-11-22 17:38:44 -07002234
2235static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
2236{
2237 struct smbd_mr *mr;
2238 struct ib_cqe *cqe;
2239
2240 if (wc->status) {
2241 log_rdma_mr(ERR, "status=%d\n", wc->status);
2242 cqe = wc->wr_cqe;
2243 mr = container_of(cqe, struct smbd_mr, cqe);
2244 smbd_disconnect_rdma_connection(mr->conn);
2245 }
2246}
2247
2248/*
2249 * The work queue function that recovers MRs
2250 * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
2251 * again. Both calls are slow, so finish them in a workqueue. This will not
2252 * block I/O path.
2253 * There is one workqueue that recovers MRs, there is no need to lock as the
2254 * I/O requests calling smbd_register_mr will never update the links in the
2255 * mr_list.
2256 */
2257static void smbd_mr_recovery_work(struct work_struct *work)
2258{
2259 struct smbd_connection *info =
2260 container_of(work, struct smbd_connection, mr_recovery_work);
2261 struct smbd_mr *smbdirect_mr;
2262 int rc;
2263
2264 list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
Long Lic21ce582019-10-16 13:51:55 -07002265 if (smbdirect_mr->state == MR_ERROR) {
Long Lic7398582017-11-22 17:38:44 -07002266
Long Li7cf20bc2018-05-30 12:48:02 -07002267 /* recover this MR entry */
2268 rc = ib_dereg_mr(smbdirect_mr->mr);
2269 if (rc) {
2270 log_rdma_mr(ERR,
2271 "ib_dereg_mr failed rc=%x\n",
2272 rc);
2273 smbd_disconnect_rdma_connection(info);
2274 continue;
2275 }
2276
2277 smbdirect_mr->mr = ib_alloc_mr(
2278 info->pd, info->mr_type,
2279 info->max_frmr_depth);
2280 if (IS_ERR(smbdirect_mr->mr)) {
2281 log_rdma_mr(ERR,
2282 "ib_alloc_mr failed mr_type=%x "
2283 "max_frmr_depth=%x\n",
2284 info->mr_type,
2285 info->max_frmr_depth);
2286 smbd_disconnect_rdma_connection(info);
2287 continue;
2288 }
Long Liff526d82018-09-20 21:18:39 +00002289 } else
2290 /* This MR is being used, don't recover it */
2291 continue;
Long Li7cf20bc2018-05-30 12:48:02 -07002292
Long Liff526d82018-09-20 21:18:39 +00002293 smbdirect_mr->state = MR_READY;
Long Lic7398582017-11-22 17:38:44 -07002294
Long Liff526d82018-09-20 21:18:39 +00002295 /* smbdirect_mr->state is updated by this function
2296 * and is read and updated by I/O issuing CPUs trying
2297 * to get a MR, the call to atomic_inc_return
2298 * implicates a memory barrier and guarantees this
2299 * value is updated before waking up any calls to
2300 * get_mr() from the I/O issuing CPUs
2301 */
2302 if (atomic_inc_return(&info->mr_ready_count) == 1)
2303 wake_up_interruptible(&info->wait_mr);
Long Lic7398582017-11-22 17:38:44 -07002304 }
2305}
2306
2307static void destroy_mr_list(struct smbd_connection *info)
2308{
2309 struct smbd_mr *mr, *tmp;
2310
2311 cancel_work_sync(&info->mr_recovery_work);
2312 list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
2313 if (mr->state == MR_INVALIDATED)
2314 ib_dma_unmap_sg(info->id->device, mr->sgl,
2315 mr->sgl_count, mr->dir);
2316 ib_dereg_mr(mr->mr);
2317 kfree(mr->sgl);
2318 kfree(mr);
2319 }
2320}
2321
2322/*
2323 * Allocate MRs used for RDMA read/write
2324 * The number of MRs will not exceed hardware capability in responder_resources
2325 * All MRs are kept in mr_list. The MR can be recovered after it's used
2326 * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
2327 * as MRs are used and recovered for I/O, but the list links will not change
2328 */
2329static int allocate_mr_list(struct smbd_connection *info)
2330{
2331 int i;
2332 struct smbd_mr *smbdirect_mr, *tmp;
2333
2334 INIT_LIST_HEAD(&info->mr_list);
2335 init_waitqueue_head(&info->wait_mr);
2336 spin_lock_init(&info->mr_list_lock);
2337 atomic_set(&info->mr_ready_count, 0);
2338 atomic_set(&info->mr_used_count, 0);
2339 init_waitqueue_head(&info->wait_for_mr_cleanup);
2340 /* Allocate more MRs (2x) than hardware responder_resources */
2341 for (i = 0; i < info->responder_resources * 2; i++) {
2342 smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
2343 if (!smbdirect_mr)
2344 goto out;
2345 smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
2346 info->max_frmr_depth);
2347 if (IS_ERR(smbdirect_mr->mr)) {
2348 log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x "
2349 "max_frmr_depth=%x\n",
2350 info->mr_type, info->max_frmr_depth);
2351 goto out;
2352 }
2353 smbdirect_mr->sgl = kcalloc(
2354 info->max_frmr_depth,
2355 sizeof(struct scatterlist),
2356 GFP_KERNEL);
2357 if (!smbdirect_mr->sgl) {
2358 log_rdma_mr(ERR, "failed to allocate sgl\n");
2359 ib_dereg_mr(smbdirect_mr->mr);
2360 goto out;
2361 }
2362 smbdirect_mr->state = MR_READY;
2363 smbdirect_mr->conn = info;
2364
2365 list_add_tail(&smbdirect_mr->list, &info->mr_list);
2366 atomic_inc(&info->mr_ready_count);
2367 }
2368 INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
2369 return 0;
2370
2371out:
2372 kfree(smbdirect_mr);
2373
2374 list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
2375 ib_dereg_mr(smbdirect_mr->mr);
2376 kfree(smbdirect_mr->sgl);
2377 kfree(smbdirect_mr);
2378 }
2379 return -ENOMEM;
2380}
2381
2382/*
2383 * Get a MR from mr_list. This function waits until there is at least one
2384 * MR available in the list. It may access the list while the
2385 * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
2386 * as they never modify the same places. However, there may be several CPUs
2387 * issueing I/O trying to get MR at the same time, mr_list_lock is used to
2388 * protect this situation.
2389 */
2390static struct smbd_mr *get_mr(struct smbd_connection *info)
2391{
2392 struct smbd_mr *ret;
2393 int rc;
2394again:
2395 rc = wait_event_interruptible(info->wait_mr,
2396 atomic_read(&info->mr_ready_count) ||
2397 info->transport_status != SMBD_CONNECTED);
2398 if (rc) {
2399 log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
2400 return NULL;
2401 }
2402
2403 if (info->transport_status != SMBD_CONNECTED) {
2404 log_rdma_mr(ERR, "info->transport_status=%x\n",
2405 info->transport_status);
2406 return NULL;
2407 }
2408
2409 spin_lock(&info->mr_list_lock);
2410 list_for_each_entry(ret, &info->mr_list, list) {
2411 if (ret->state == MR_READY) {
2412 ret->state = MR_REGISTERED;
2413 spin_unlock(&info->mr_list_lock);
2414 atomic_dec(&info->mr_ready_count);
2415 atomic_inc(&info->mr_used_count);
2416 return ret;
2417 }
2418 }
2419
2420 spin_unlock(&info->mr_list_lock);
2421 /*
2422 * It is possible that we could fail to get MR because other processes may
2423 * try to acquire a MR at the same time. If this is the case, retry it.
2424 */
2425 goto again;
2426}
2427
2428/*
2429 * Register memory for RDMA read/write
2430 * pages[]: the list of pages to register memory with
2431 * num_pages: the number of pages to register
2432 * tailsz: if non-zero, the bytes to register in the last page
2433 * writing: true if this is a RDMA write (SMB read), false for RDMA read
2434 * need_invalidate: true if this MR needs to be locally invalidated after I/O
2435 * return value: the MR registered, NULL if failed.
2436 */
2437struct smbd_mr *smbd_register_mr(
2438 struct smbd_connection *info, struct page *pages[], int num_pages,
Long Li7cf20bc2018-05-30 12:48:02 -07002439 int offset, int tailsz, bool writing, bool need_invalidate)
Long Lic7398582017-11-22 17:38:44 -07002440{
2441 struct smbd_mr *smbdirect_mr;
2442 int rc, i;
2443 enum dma_data_direction dir;
2444 struct ib_reg_wr *reg_wr;
Long Lic7398582017-11-22 17:38:44 -07002445
2446 if (num_pages > info->max_frmr_depth) {
2447 log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
2448 num_pages, info->max_frmr_depth);
2449 return NULL;
2450 }
2451
2452 smbdirect_mr = get_mr(info);
2453 if (!smbdirect_mr) {
2454 log_rdma_mr(ERR, "get_mr returning NULL\n");
2455 return NULL;
2456 }
2457 smbdirect_mr->need_invalidate = need_invalidate;
2458 smbdirect_mr->sgl_count = num_pages;
2459 sg_init_table(smbdirect_mr->sgl, num_pages);
2460
Long Li7cf20bc2018-05-30 12:48:02 -07002461 log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
2462 num_pages, offset, tailsz);
Long Lic7398582017-11-22 17:38:44 -07002463
Long Li7cf20bc2018-05-30 12:48:02 -07002464 if (num_pages == 1) {
2465 sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
2466 goto skip_multiple_pages;
2467 }
2468
2469 /* We have at least two pages to register */
2470 sg_set_page(
2471 &smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
2472 i = 1;
2473 while (i < num_pages - 1) {
2474 sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
2475 i++;
2476 }
Long Lic7398582017-11-22 17:38:44 -07002477 sg_set_page(&smbdirect_mr->sgl[i], pages[i],
2478 tailsz ? tailsz : PAGE_SIZE, 0);
2479
Long Li7cf20bc2018-05-30 12:48:02 -07002480skip_multiple_pages:
Long Lic7398582017-11-22 17:38:44 -07002481 dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2482 smbdirect_mr->dir = dir;
2483 rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
2484 if (!rc) {
Long Li7cf20bc2018-05-30 12:48:02 -07002485 log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
Long Lic7398582017-11-22 17:38:44 -07002486 num_pages, dir, rc);
2487 goto dma_map_error;
2488 }
2489
2490 rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
2491 NULL, PAGE_SIZE);
2492 if (rc != num_pages) {
Long Li7cf20bc2018-05-30 12:48:02 -07002493 log_rdma_mr(ERR,
2494 "ib_map_mr_sg failed rc = %d num_pages = %x\n",
Long Lic7398582017-11-22 17:38:44 -07002495 rc, num_pages);
2496 goto map_mr_error;
2497 }
2498
2499 ib_update_fast_reg_key(smbdirect_mr->mr,
2500 ib_inc_rkey(smbdirect_mr->mr->rkey));
2501 reg_wr = &smbdirect_mr->wr;
2502 reg_wr->wr.opcode = IB_WR_REG_MR;
2503 smbdirect_mr->cqe.done = register_mr_done;
2504 reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
2505 reg_wr->wr.num_sge = 0;
2506 reg_wr->wr.send_flags = IB_SEND_SIGNALED;
2507 reg_wr->mr = smbdirect_mr->mr;
2508 reg_wr->key = smbdirect_mr->mr->rkey;
2509 reg_wr->access = writing ?
2510 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2511 IB_ACCESS_REMOTE_READ;
2512
2513 /*
2514 * There is no need for waiting for complemtion on ib_post_send
2515 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
2516 * on the next ib_post_send when we actaully send I/O to remote peer
2517 */
Bart Van Assche73930592018-07-18 09:25:25 -07002518 rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
Long Lic7398582017-11-22 17:38:44 -07002519 if (!rc)
2520 return smbdirect_mr;
2521
2522 log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
2523 rc, reg_wr->key);
2524
2525 /* If all failed, attempt to recover this MR by setting it MR_ERROR*/
2526map_mr_error:
2527 ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
2528 smbdirect_mr->sgl_count, smbdirect_mr->dir);
2529
2530dma_map_error:
2531 smbdirect_mr->state = MR_ERROR;
2532 if (atomic_dec_and_test(&info->mr_used_count))
2533 wake_up(&info->wait_for_mr_cleanup);
2534
Long Li21a4e142018-03-30 15:16:36 -07002535 smbd_disconnect_rdma_connection(info);
2536
Long Lic7398582017-11-22 17:38:44 -07002537 return NULL;
2538}
2539
2540static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
2541{
2542 struct smbd_mr *smbdirect_mr;
2543 struct ib_cqe *cqe;
2544
2545 cqe = wc->wr_cqe;
2546 smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
2547 smbdirect_mr->state = MR_INVALIDATED;
2548 if (wc->status != IB_WC_SUCCESS) {
2549 log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
2550 smbdirect_mr->state = MR_ERROR;
2551 }
2552 complete(&smbdirect_mr->invalidate_done);
2553}
2554
2555/*
2556 * Deregister a MR after I/O is done
2557 * This function may wait if remote invalidation is not used
2558 * and we have to locally invalidate the buffer to prevent data is being
2559 * modified by remote peer after upper layer consumes it
2560 */
2561int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
2562{
Bart Van Assche73930592018-07-18 09:25:25 -07002563 struct ib_send_wr *wr;
Long Lic7398582017-11-22 17:38:44 -07002564 struct smbd_connection *info = smbdirect_mr->conn;
2565 int rc = 0;
2566
2567 if (smbdirect_mr->need_invalidate) {
2568 /* Need to finish local invalidation before returning */
2569 wr = &smbdirect_mr->inv_wr;
2570 wr->opcode = IB_WR_LOCAL_INV;
2571 smbdirect_mr->cqe.done = local_inv_done;
2572 wr->wr_cqe = &smbdirect_mr->cqe;
2573 wr->num_sge = 0;
2574 wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
2575 wr->send_flags = IB_SEND_SIGNALED;
2576
2577 init_completion(&smbdirect_mr->invalidate_done);
Bart Van Assche73930592018-07-18 09:25:25 -07002578 rc = ib_post_send(info->id->qp, wr, NULL);
Long Lic7398582017-11-22 17:38:44 -07002579 if (rc) {
2580 log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
2581 smbd_disconnect_rdma_connection(info);
2582 goto done;
2583 }
2584 wait_for_completion(&smbdirect_mr->invalidate_done);
2585 smbdirect_mr->need_invalidate = false;
2586 } else
2587 /*
2588 * For remote invalidation, just set it to MR_INVALIDATED
2589 * and defer to mr_recovery_work to recover the MR for next use
2590 */
2591 smbdirect_mr->state = MR_INVALIDATED;
2592
Long Lic21ce582019-10-16 13:51:55 -07002593 if (smbdirect_mr->state == MR_INVALIDATED) {
2594 ib_dma_unmap_sg(
2595 info->id->device, smbdirect_mr->sgl,
2596 smbdirect_mr->sgl_count,
2597 smbdirect_mr->dir);
2598 smbdirect_mr->state = MR_READY;
2599 if (atomic_inc_return(&info->mr_ready_count) == 1)
2600 wake_up_interruptible(&info->wait_mr);
2601 } else
2602 /*
2603 * Schedule the work to do MR recovery for future I/Os MR
2604 * recovery is slow and don't want it to block current I/O
2605 */
2606 queue_work(info->workqueue, &info->mr_recovery_work);
Long Lic7398582017-11-22 17:38:44 -07002607
2608done:
2609 if (atomic_dec_and_test(&info->mr_used_count))
2610 wake_up(&info->wait_for_mr_cleanup);
2611
2612 return rc;
2613}