1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * RDMA Transport Layer
4 *
5 * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6 * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7 * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8 */
9
10 #undef pr_fmt
11 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
12
13 #include <linux/module.h>
14 #include <linux/rculist.h>
15 #include <linux/random.h>
16
17 #include "rtrs-clt.h"
18 #include "rtrs-log.h"
19
20 #define RTRS_CONNECT_TIMEOUT_MS 30000
21 /*
22 * Wait a bit before trying to reconnect after a failure
23 * in order to give server time to finish clean up which
24 * leads to "false positives" failed reconnect attempts
25 */
26 #define RTRS_RECONNECT_BACKOFF 1000
27 /*
28 * Wait for additional random time between 0 and 8 seconds
29 * before starting to reconnect to avoid clients reconnecting
30 * all at once in case of a major network outage
31 */
32 #define RTRS_RECONNECT_SEED 8
33
34 #define FIRST_CONN 0x01
35 /* limit to 128 * 4k = 512k max IO */
36 #define RTRS_MAX_SEGMENTS 128
37
38 MODULE_DESCRIPTION("RDMA Transport Client");
39 MODULE_LICENSE("GPL");
40
41 static const struct rtrs_rdma_dev_pd_ops dev_pd_ops;
42 static struct rtrs_rdma_dev_pd dev_pd = {
43 .ops = &dev_pd_ops
44 };
45
46 static struct workqueue_struct *rtrs_wq;
47 static struct class *rtrs_clt_dev_class;
48
rtrs_clt_is_connected(const struct rtrs_clt_sess * clt)49 static inline bool rtrs_clt_is_connected(const struct rtrs_clt_sess *clt)
50 {
51 struct rtrs_clt_path *clt_path;
52 bool connected = false;
53
54 rcu_read_lock();
55 list_for_each_entry_rcu(clt_path, &clt->paths_list, s.entry)
56 connected |= READ_ONCE(clt_path->state) == RTRS_CLT_CONNECTED;
57 rcu_read_unlock();
58
59 return connected;
60 }
61
62 static struct rtrs_permit *
__rtrs_get_permit(struct rtrs_clt_sess * clt,enum rtrs_clt_con_type con_type)63 __rtrs_get_permit(struct rtrs_clt_sess *clt, enum rtrs_clt_con_type con_type)
64 {
65 size_t max_depth = clt->queue_depth;
66 struct rtrs_permit *permit;
67 int bit;
68
69 /*
70 * Adapted from null_blk get_tag(). Callers from different cpus may
71 * grab the same bit, since find_first_zero_bit is not atomic.
72 * But then the test_and_set_bit_lock will fail for all the
73 * callers but one, so that they will loop again.
74 * This way an explicit spinlock is not required.
75 */
76 do {
77 bit = find_first_zero_bit(clt->permits_map, max_depth);
78 if (bit >= max_depth)
79 return NULL;
80 } while (test_and_set_bit_lock(bit, clt->permits_map));
81
82 permit = get_permit(clt, bit);
83 WARN_ON(permit->mem_id != bit);
84 permit->cpu_id = raw_smp_processor_id();
85 permit->con_type = con_type;
86
87 return permit;
88 }
89
__rtrs_put_permit(struct rtrs_clt_sess * clt,struct rtrs_permit * permit)90 static inline void __rtrs_put_permit(struct rtrs_clt_sess *clt,
91 struct rtrs_permit *permit)
92 {
93 clear_bit_unlock(permit->mem_id, clt->permits_map);
94 }
95
96 /**
97 * rtrs_clt_get_permit() - allocates permit for future RDMA operation
98 * @clt: Current session
99 * @con_type: Type of connection to use with the permit
100 * @can_wait: Wait type
101 *
102 * Description:
103 * Allocates permit for the following RDMA operation. Permit is used
104 * to preallocate all resources and to propagate memory pressure
105 * up earlier.
106 *
107 * Context:
108 * Can sleep if @wait == RTRS_PERMIT_WAIT
109 */
rtrs_clt_get_permit(struct rtrs_clt_sess * clt,enum rtrs_clt_con_type con_type,enum wait_type can_wait)110 struct rtrs_permit *rtrs_clt_get_permit(struct rtrs_clt_sess *clt,
111 enum rtrs_clt_con_type con_type,
112 enum wait_type can_wait)
113 {
114 struct rtrs_permit *permit;
115 DEFINE_WAIT(wait);
116
117 permit = __rtrs_get_permit(clt, con_type);
118 if (permit || !can_wait)
119 return permit;
120
121 do {
122 prepare_to_wait(&clt->permits_wait, &wait,
123 TASK_UNINTERRUPTIBLE);
124 permit = __rtrs_get_permit(clt, con_type);
125 if (permit)
126 break;
127
128 io_schedule();
129 } while (1);
130
131 finish_wait(&clt->permits_wait, &wait);
132
133 return permit;
134 }
135 EXPORT_SYMBOL(rtrs_clt_get_permit);
136
137 /**
138 * rtrs_clt_put_permit() - puts allocated permit
139 * @clt: Current session
140 * @permit: Permit to be freed
141 *
142 * Context:
143 * Does not matter
144 */
rtrs_clt_put_permit(struct rtrs_clt_sess * clt,struct rtrs_permit * permit)145 void rtrs_clt_put_permit(struct rtrs_clt_sess *clt,
146 struct rtrs_permit *permit)
147 {
148 if (WARN_ON(!test_bit(permit->mem_id, clt->permits_map)))
149 return;
150
151 __rtrs_put_permit(clt, permit);
152
153 /*
154 * rtrs_clt_get_permit() adds itself to the &clt->permits_wait list
155 * before calling schedule(). So if rtrs_clt_get_permit() is sleeping
156 * it must have added itself to &clt->permits_wait before
157 * __rtrs_put_permit() finished.
158 * Hence it is safe to guard wake_up() with a waitqueue_active() test.
159 */
160 if (waitqueue_active(&clt->permits_wait))
161 wake_up(&clt->permits_wait);
162 }
163 EXPORT_SYMBOL(rtrs_clt_put_permit);
164
165 /**
166 * rtrs_permit_to_clt_con() - returns RDMA connection pointer by the permit
167 * @clt_path: client path pointer
168 * @permit: permit for the allocation of the RDMA buffer
169 * Note:
170 * IO connection starts from 1.
171 * 0 connection is for user messages.
172 */
173 static
rtrs_permit_to_clt_con(struct rtrs_clt_path * clt_path,struct rtrs_permit * permit)174 struct rtrs_clt_con *rtrs_permit_to_clt_con(struct rtrs_clt_path *clt_path,
175 struct rtrs_permit *permit)
176 {
177 int id = 0;
178
179 if (permit->con_type == RTRS_IO_CON)
180 id = (permit->cpu_id % (clt_path->s.irq_con_num - 1)) + 1;
181
182 return to_clt_con(clt_path->s.con[id]);
183 }
184
185 /**
186 * rtrs_clt_change_state() - change the session state through session state
187 * machine.
188 *
189 * @clt_path: client path to change the state of.
190 * @new_state: state to change to.
191 *
192 * returns true if sess's state is changed to new state, otherwise return false.
193 *
194 * Locks:
195 * state_wq lock must be hold.
196 */
rtrs_clt_change_state(struct rtrs_clt_path * clt_path,enum rtrs_clt_state new_state)197 static bool rtrs_clt_change_state(struct rtrs_clt_path *clt_path,
198 enum rtrs_clt_state new_state)
199 {
200 enum rtrs_clt_state old_state;
201 bool changed = false;
202
203 lockdep_assert_held(&clt_path->state_wq.lock);
204
205 old_state = clt_path->state;
206 switch (new_state) {
207 case RTRS_CLT_CONNECTING:
208 switch (old_state) {
209 case RTRS_CLT_RECONNECTING:
210 changed = true;
211 fallthrough;
212 default:
213 break;
214 }
215 break;
216 case RTRS_CLT_RECONNECTING:
217 switch (old_state) {
218 case RTRS_CLT_CONNECTED:
219 case RTRS_CLT_CONNECTING_ERR:
220 case RTRS_CLT_CLOSED:
221 changed = true;
222 fallthrough;
223 default:
224 break;
225 }
226 break;
227 case RTRS_CLT_CONNECTED:
228 switch (old_state) {
229 case RTRS_CLT_CONNECTING:
230 changed = true;
231 fallthrough;
232 default:
233 break;
234 }
235 break;
236 case RTRS_CLT_CONNECTING_ERR:
237 switch (old_state) {
238 case RTRS_CLT_CONNECTING:
239 changed = true;
240 fallthrough;
241 default:
242 break;
243 }
244 break;
245 case RTRS_CLT_CLOSING:
246 switch (old_state) {
247 case RTRS_CLT_CONNECTING:
248 case RTRS_CLT_CONNECTING_ERR:
249 case RTRS_CLT_RECONNECTING:
250 case RTRS_CLT_CONNECTED:
251 changed = true;
252 fallthrough;
253 default:
254 break;
255 }
256 break;
257 case RTRS_CLT_CLOSED:
258 switch (old_state) {
259 case RTRS_CLT_CLOSING:
260 changed = true;
261 fallthrough;
262 default:
263 break;
264 }
265 break;
266 case RTRS_CLT_DEAD:
267 switch (old_state) {
268 case RTRS_CLT_CLOSED:
269 changed = true;
270 fallthrough;
271 default:
272 break;
273 }
274 break;
275 default:
276 break;
277 }
278 if (changed) {
279 clt_path->state = new_state;
280 wake_up_locked(&clt_path->state_wq);
281 }
282
283 return changed;
284 }
285
rtrs_clt_change_state_from_to(struct rtrs_clt_path * clt_path,enum rtrs_clt_state old_state,enum rtrs_clt_state new_state)286 static bool rtrs_clt_change_state_from_to(struct rtrs_clt_path *clt_path,
287 enum rtrs_clt_state old_state,
288 enum rtrs_clt_state new_state)
289 {
290 bool changed = false;
291
292 spin_lock_irq(&clt_path->state_wq.lock);
293 if (clt_path->state == old_state)
294 changed = rtrs_clt_change_state(clt_path, new_state);
295 spin_unlock_irq(&clt_path->state_wq.lock);
296
297 return changed;
298 }
299
300 static void rtrs_clt_stop_and_destroy_conns(struct rtrs_clt_path *clt_path);
rtrs_rdma_error_recovery(struct rtrs_clt_con * con)301 static void rtrs_rdma_error_recovery(struct rtrs_clt_con *con)
302 {
303 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
304
305 if (rtrs_clt_change_state_from_to(clt_path,
306 RTRS_CLT_CONNECTED,
307 RTRS_CLT_RECONNECTING)) {
308 queue_work(rtrs_wq, &clt_path->err_recovery_work);
309 } else {
310 /*
311 * Error can happen just on establishing new connection,
312 * so notify waiter with error state, waiter is responsible
313 * for cleaning the rest and reconnect if needed.
314 */
315 rtrs_clt_change_state_from_to(clt_path,
316 RTRS_CLT_CONNECTING,
317 RTRS_CLT_CONNECTING_ERR);
318 }
319 }
320
rtrs_clt_fast_reg_done(struct ib_cq * cq,struct ib_wc * wc)321 static void rtrs_clt_fast_reg_done(struct ib_cq *cq, struct ib_wc *wc)
322 {
323 struct rtrs_clt_con *con = to_clt_con(wc->qp->qp_context);
324
325 if (wc->status != IB_WC_SUCCESS) {
326 rtrs_err(con->c.path, "Failed IB_WR_REG_MR: %s\n",
327 ib_wc_status_msg(wc->status));
328 rtrs_rdma_error_recovery(con);
329 }
330 }
331
332 static struct ib_cqe fast_reg_cqe = {
333 .done = rtrs_clt_fast_reg_done
334 };
335
336 static void complete_rdma_req(struct rtrs_clt_io_req *req, int errno,
337 bool notify, bool can_wait);
338
rtrs_clt_inv_rkey_done(struct ib_cq * cq,struct ib_wc * wc)339 static void rtrs_clt_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
340 {
341 struct rtrs_clt_io_req *req =
342 container_of(wc->wr_cqe, typeof(*req), inv_cqe);
343 struct rtrs_clt_con *con = to_clt_con(wc->qp->qp_context);
344
345 if (wc->status != IB_WC_SUCCESS) {
346 rtrs_err(con->c.path, "Failed IB_WR_LOCAL_INV: %s\n",
347 ib_wc_status_msg(wc->status));
348 rtrs_rdma_error_recovery(con);
349 }
350 req->need_inv = false;
351 if (req->need_inv_comp)
352 complete(&req->inv_comp);
353 else
354 /* Complete request from INV callback */
355 complete_rdma_req(req, req->inv_errno, true, false);
356 }
357
rtrs_inv_rkey(struct rtrs_clt_io_req * req)358 static int rtrs_inv_rkey(struct rtrs_clt_io_req *req)
359 {
360 struct rtrs_clt_con *con = req->con;
361 struct ib_send_wr wr = {
362 .opcode = IB_WR_LOCAL_INV,
363 .wr_cqe = &req->inv_cqe,
364 .send_flags = IB_SEND_SIGNALED,
365 .ex.invalidate_rkey = req->mr->rkey,
366 };
367 req->inv_cqe.done = rtrs_clt_inv_rkey_done;
368
369 return ib_post_send(con->c.qp, &wr, NULL);
370 }
371
complete_rdma_req(struct rtrs_clt_io_req * req,int errno,bool notify,bool can_wait)372 static void complete_rdma_req(struct rtrs_clt_io_req *req, int errno,
373 bool notify, bool can_wait)
374 {
375 struct rtrs_clt_con *con = req->con;
376 struct rtrs_clt_path *clt_path;
377 int err;
378
379 if (WARN_ON(!req->in_use))
380 return;
381 if (WARN_ON(!req->con))
382 return;
383 clt_path = to_clt_path(con->c.path);
384
385 if (req->sg_cnt) {
386 if (req->dir == DMA_FROM_DEVICE && req->need_inv) {
387 /*
388 * We are here to invalidate read requests
389 * ourselves. In normal scenario server should
390 * send INV for all read requests, but
391 * we are here, thus two things could happen:
392 *
393 * 1. this is failover, when errno != 0
394 * and can_wait == 1,
395 *
396 * 2. something totally bad happened and
397 * server forgot to send INV, so we
398 * should do that ourselves.
399 */
400
401 if (can_wait) {
402 req->need_inv_comp = true;
403 } else {
404 /* This should be IO path, so always notify */
405 WARN_ON(!notify);
406 /* Save errno for INV callback */
407 req->inv_errno = errno;
408 }
409
410 refcount_inc(&req->ref);
411 err = rtrs_inv_rkey(req);
412 if (err) {
413 rtrs_err(con->c.path, "Send INV WR key=%#x: %d\n",
414 req->mr->rkey, err);
415 } else if (can_wait) {
416 wait_for_completion(&req->inv_comp);
417 } else {
418 /*
419 * Something went wrong, so request will be
420 * completed from INV callback.
421 */
422 WARN_ON_ONCE(1);
423
424 return;
425 }
426 if (!refcount_dec_and_test(&req->ref))
427 return;
428 }
429 ib_dma_unmap_sg(clt_path->s.dev->ib_dev, req->sglist,
430 req->sg_cnt, req->dir);
431 }
432 if (!refcount_dec_and_test(&req->ref))
433 return;
434 if (req->mp_policy == MP_POLICY_MIN_INFLIGHT)
435 atomic_dec(&clt_path->stats->inflight);
436
437 req->in_use = false;
438 req->con = NULL;
439
440 if (errno) {
441 rtrs_err_rl(con->c.path, "IO request failed: error=%d path=%s [%s:%u] notify=%d\n",
442 errno, kobject_name(&clt_path->kobj), clt_path->hca_name,
443 clt_path->hca_port, notify);
444 }
445
446 if (notify)
447 req->conf(req->priv, errno);
448 }
449
rtrs_post_send_rdma(struct rtrs_clt_con * con,struct rtrs_clt_io_req * req,struct rtrs_rbuf * rbuf,u32 off,u32 imm,struct ib_send_wr * wr)450 static int rtrs_post_send_rdma(struct rtrs_clt_con *con,
451 struct rtrs_clt_io_req *req,
452 struct rtrs_rbuf *rbuf, u32 off,
453 u32 imm, struct ib_send_wr *wr)
454 {
455 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
456 enum ib_send_flags flags;
457 struct ib_sge sge;
458
459 if (!req->sg_size) {
460 rtrs_wrn(con->c.path,
461 "Doing RDMA Write failed, no data supplied\n");
462 return -EINVAL;
463 }
464
465 /* user data and user message in the first list element */
466 sge.addr = req->iu->dma_addr;
467 sge.length = req->sg_size;
468 sge.lkey = clt_path->s.dev->ib_pd->local_dma_lkey;
469
470 /*
471 * From time to time we have to post signalled sends,
472 * or send queue will fill up and only QP reset can help.
473 */
474 flags = atomic_inc_return(&con->c.wr_cnt) % clt_path->s.signal_interval ?
475 0 : IB_SEND_SIGNALED;
476
477 ib_dma_sync_single_for_device(clt_path->s.dev->ib_dev,
478 req->iu->dma_addr,
479 req->sg_size, DMA_TO_DEVICE);
480
481 return rtrs_iu_post_rdma_write_imm(&con->c, req->iu, &sge, 1,
482 rbuf->rkey, rbuf->addr + off,
483 imm, flags, wr, NULL);
484 }
485
process_io_rsp(struct rtrs_clt_path * clt_path,u32 msg_id,s16 errno,bool w_inval)486 static void process_io_rsp(struct rtrs_clt_path *clt_path, u32 msg_id,
487 s16 errno, bool w_inval)
488 {
489 struct rtrs_clt_io_req *req;
490
491 if (WARN_ON(msg_id >= clt_path->queue_depth))
492 return;
493
494 req = &clt_path->reqs[msg_id];
495 /* Drop need_inv if server responded with send with invalidation */
496 req->need_inv &= !w_inval;
497 complete_rdma_req(req, errno, true, false);
498 }
499
rtrs_clt_recv_done(struct rtrs_clt_con * con,struct ib_wc * wc)500 static void rtrs_clt_recv_done(struct rtrs_clt_con *con, struct ib_wc *wc)
501 {
502 struct rtrs_iu *iu;
503 int err;
504 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
505
506 WARN_ON((clt_path->flags & RTRS_MSG_NEW_RKEY_F) == 0);
507 iu = container_of(wc->wr_cqe, struct rtrs_iu,
508 cqe);
509 err = rtrs_iu_post_recv(&con->c, iu);
510 if (err) {
511 rtrs_err(con->c.path, "post iu failed %d\n", err);
512 rtrs_rdma_error_recovery(con);
513 }
514 }
515
rtrs_clt_rkey_rsp_done(struct rtrs_clt_con * con,struct ib_wc * wc)516 static void rtrs_clt_rkey_rsp_done(struct rtrs_clt_con *con, struct ib_wc *wc)
517 {
518 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
519 struct rtrs_msg_rkey_rsp *msg;
520 u32 imm_type, imm_payload;
521 bool w_inval = false;
522 struct rtrs_iu *iu;
523 u32 buf_id;
524 int err;
525
526 WARN_ON((clt_path->flags & RTRS_MSG_NEW_RKEY_F) == 0);
527
528 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
529
530 if (wc->byte_len < sizeof(*msg)) {
531 rtrs_err(con->c.path, "rkey response is malformed: size %d\n",
532 wc->byte_len);
533 goto out;
534 }
535 ib_dma_sync_single_for_cpu(clt_path->s.dev->ib_dev, iu->dma_addr,
536 iu->size, DMA_FROM_DEVICE);
537 msg = iu->buf;
538 if (le16_to_cpu(msg->type) != RTRS_MSG_RKEY_RSP) {
539 rtrs_err(clt_path->clt,
540 "rkey response is malformed: type %d\n",
541 le16_to_cpu(msg->type));
542 goto out;
543 }
544 buf_id = le16_to_cpu(msg->buf_id);
545 if (WARN_ON(buf_id >= clt_path->queue_depth))
546 goto out;
547
548 rtrs_from_imm(be32_to_cpu(wc->ex.imm_data), &imm_type, &imm_payload);
549 if (imm_type == RTRS_IO_RSP_IMM ||
550 imm_type == RTRS_IO_RSP_W_INV_IMM) {
551 u32 msg_id;
552
553 w_inval = (imm_type == RTRS_IO_RSP_W_INV_IMM);
554 rtrs_from_io_rsp_imm(imm_payload, &msg_id, &err);
555
556 if (WARN_ON(buf_id != msg_id))
557 goto out;
558 clt_path->rbufs[buf_id].rkey = le32_to_cpu(msg->rkey);
559 process_io_rsp(clt_path, msg_id, err, w_inval);
560 }
561 ib_dma_sync_single_for_device(clt_path->s.dev->ib_dev, iu->dma_addr,
562 iu->size, DMA_FROM_DEVICE);
563 return rtrs_clt_recv_done(con, wc);
564 out:
565 rtrs_rdma_error_recovery(con);
566 }
567
568 static void rtrs_clt_rdma_done(struct ib_cq *cq, struct ib_wc *wc);
569
570 static struct ib_cqe io_comp_cqe = {
571 .done = rtrs_clt_rdma_done
572 };
573
574 /*
575 * Post x2 empty WRs: first is for this RDMA with IMM,
576 * second is for RECV with INV, which happened earlier.
577 */
rtrs_post_recv_empty_x2(struct rtrs_con * con,struct ib_cqe * cqe)578 static int rtrs_post_recv_empty_x2(struct rtrs_con *con, struct ib_cqe *cqe)
579 {
580 struct ib_recv_wr wr_arr[2], *wr;
581 int i;
582
583 memset(wr_arr, 0, sizeof(wr_arr));
584 for (i = 0; i < ARRAY_SIZE(wr_arr); i++) {
585 wr = &wr_arr[i];
586 wr->wr_cqe = cqe;
587 if (i)
588 /* Chain backwards */
589 wr->next = &wr_arr[i - 1];
590 }
591
592 return ib_post_recv(con->qp, wr, NULL);
593 }
594
rtrs_clt_rdma_done(struct ib_cq * cq,struct ib_wc * wc)595 static void rtrs_clt_rdma_done(struct ib_cq *cq, struct ib_wc *wc)
596 {
597 struct rtrs_clt_con *con = to_clt_con(wc->qp->qp_context);
598 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
599 u32 imm_type, imm_payload;
600 bool w_inval = false;
601 int err;
602
603 if (wc->status != IB_WC_SUCCESS) {
604 if (wc->status != IB_WC_WR_FLUSH_ERR) {
605 rtrs_err(clt_path->clt, "RDMA failed: %s\n",
606 ib_wc_status_msg(wc->status));
607 rtrs_rdma_error_recovery(con);
608 }
609 return;
610 }
611 rtrs_clt_update_wc_stats(con);
612
613 switch (wc->opcode) {
614 case IB_WC_RECV_RDMA_WITH_IMM:
615 /*
616 * post_recv() RDMA write completions of IO reqs (read/write)
617 * and hb
618 */
619 if (WARN_ON(wc->wr_cqe->done != rtrs_clt_rdma_done))
620 return;
621 rtrs_from_imm(be32_to_cpu(wc->ex.imm_data),
622 &imm_type, &imm_payload);
623 if (imm_type == RTRS_IO_RSP_IMM ||
624 imm_type == RTRS_IO_RSP_W_INV_IMM) {
625 u32 msg_id;
626
627 w_inval = (imm_type == RTRS_IO_RSP_W_INV_IMM);
628 rtrs_from_io_rsp_imm(imm_payload, &msg_id, &err);
629
630 process_io_rsp(clt_path, msg_id, err, w_inval);
631 } else if (imm_type == RTRS_HB_MSG_IMM) {
632 WARN_ON(con->c.cid);
633 rtrs_send_hb_ack(&clt_path->s);
634 if (clt_path->flags & RTRS_MSG_NEW_RKEY_F)
635 return rtrs_clt_recv_done(con, wc);
636 } else if (imm_type == RTRS_HB_ACK_IMM) {
637 WARN_ON(con->c.cid);
638 clt_path->s.hb_missed_cnt = 0;
639 clt_path->s.hb_cur_latency =
640 ktime_sub(ktime_get(), clt_path->s.hb_last_sent);
641 if (clt_path->flags & RTRS_MSG_NEW_RKEY_F)
642 return rtrs_clt_recv_done(con, wc);
643 } else {
644 rtrs_wrn(con->c.path, "Unknown IMM type %u\n",
645 imm_type);
646 }
647 if (w_inval)
648 /*
649 * Post x2 empty WRs: first is for this RDMA with IMM,
650 * second is for RECV with INV, which happened earlier.
651 */
652 err = rtrs_post_recv_empty_x2(&con->c, &io_comp_cqe);
653 else
654 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe);
655 if (err) {
656 rtrs_err(con->c.path, "rtrs_post_recv_empty(): %d\n",
657 err);
658 rtrs_rdma_error_recovery(con);
659 }
660 break;
661 case IB_WC_RECV:
662 /*
663 * Key invalidations from server side
664 */
665 WARN_ON(!(wc->wc_flags & IB_WC_WITH_INVALIDATE ||
666 wc->wc_flags & IB_WC_WITH_IMM));
667 WARN_ON(wc->wr_cqe->done != rtrs_clt_rdma_done);
668 if (clt_path->flags & RTRS_MSG_NEW_RKEY_F) {
669 if (wc->wc_flags & IB_WC_WITH_INVALIDATE)
670 return rtrs_clt_recv_done(con, wc);
671
672 return rtrs_clt_rkey_rsp_done(con, wc);
673 }
674 break;
675 case IB_WC_RDMA_WRITE:
676 /*
677 * post_send() RDMA write completions of IO reqs (read/write)
678 * and hb.
679 */
680 break;
681
682 default:
683 rtrs_wrn(clt_path->clt, "Unexpected WC type: %d\n", wc->opcode);
684 return;
685 }
686 }
687
post_recv_io(struct rtrs_clt_con * con,size_t q_size)688 static int post_recv_io(struct rtrs_clt_con *con, size_t q_size)
689 {
690 int err, i;
691 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
692
693 for (i = 0; i < q_size; i++) {
694 if (clt_path->flags & RTRS_MSG_NEW_RKEY_F) {
695 struct rtrs_iu *iu = &con->rsp_ius[i];
696
697 err = rtrs_iu_post_recv(&con->c, iu);
698 } else {
699 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe);
700 }
701 if (err)
702 return err;
703 }
704
705 return 0;
706 }
707
post_recv_path(struct rtrs_clt_path * clt_path)708 static int post_recv_path(struct rtrs_clt_path *clt_path)
709 {
710 size_t q_size = 0;
711 int err, cid;
712
713 for (cid = 0; cid < clt_path->s.con_num; cid++) {
714 if (cid == 0)
715 q_size = SERVICE_CON_QUEUE_DEPTH;
716 else
717 q_size = clt_path->queue_depth;
718
719 /*
720 * x2 for RDMA read responses + FR key invalidations,
721 * RDMA writes do not require any FR registrations.
722 */
723 q_size *= 2;
724
725 err = post_recv_io(to_clt_con(clt_path->s.con[cid]), q_size);
726 if (err) {
727 rtrs_err(clt_path->clt, "post_recv_io(), err: %d\n",
728 err);
729 return err;
730 }
731 }
732
733 return 0;
734 }
735
736 struct path_it {
737 int i;
738 struct list_head skip_list;
739 struct rtrs_clt_sess *clt;
740 struct rtrs_clt_path *(*next_path)(struct path_it *it);
741 };
742
743 /*
744 * rtrs_clt_get_next_path_or_null - get clt path from the list or return NULL
745 * @head: the head for the list.
746 * @clt_path: The element to take the next clt_path from.
747 *
748 * Next clt path returned in round-robin fashion, i.e. head will be skipped,
749 * but if list is observed as empty, NULL will be returned.
750 *
751 * This function may safely run concurrently with the _rcu list-mutation
752 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
753 */
754 static inline struct rtrs_clt_path *
rtrs_clt_get_next_path_or_null(struct list_head * head,struct rtrs_clt_path * clt_path)755 rtrs_clt_get_next_path_or_null(struct list_head *head, struct rtrs_clt_path *clt_path)
756 {
757 return list_next_or_null_rcu(head, &clt_path->s.entry, typeof(*clt_path), s.entry) ?:
758 list_next_or_null_rcu(head,
759 READ_ONCE((&clt_path->s.entry)->next),
760 typeof(*clt_path), s.entry);
761 }
762
763 /**
764 * get_next_path_rr() - Returns path in round-robin fashion.
765 * @it: the path pointer
766 *
767 * Related to @MP_POLICY_RR
768 *
769 * Locks:
770 * rcu_read_lock() must be hold.
771 */
get_next_path_rr(struct path_it * it)772 static struct rtrs_clt_path *get_next_path_rr(struct path_it *it)
773 {
774 struct rtrs_clt_path __rcu **ppcpu_path;
775 struct rtrs_clt_path *path;
776 struct rtrs_clt_sess *clt;
777
778 clt = it->clt;
779
780 /*
781 * Here we use two RCU objects: @paths_list and @pcpu_path
782 * pointer. See rtrs_clt_remove_path_from_arr() for details
783 * how that is handled.
784 */
785
786 ppcpu_path = this_cpu_ptr(clt->pcpu_path);
787 path = rcu_dereference(*ppcpu_path);
788 if (!path)
789 path = list_first_or_null_rcu(&clt->paths_list,
790 typeof(*path), s.entry);
791 else
792 path = rtrs_clt_get_next_path_or_null(&clt->paths_list, path);
793
794 rcu_assign_pointer(*ppcpu_path, path);
795
796 return path;
797 }
798
799 /**
800 * get_next_path_min_inflight() - Returns path with minimal inflight count.
801 * @it: the path pointer
802 *
803 * Related to @MP_POLICY_MIN_INFLIGHT
804 *
805 * Locks:
806 * rcu_read_lock() must be hold.
807 */
get_next_path_min_inflight(struct path_it * it)808 static struct rtrs_clt_path *get_next_path_min_inflight(struct path_it *it)
809 {
810 struct rtrs_clt_path *min_path = NULL;
811 struct rtrs_clt_sess *clt = it->clt;
812 struct rtrs_clt_path *clt_path;
813 int min_inflight = INT_MAX;
814 int inflight;
815
816 list_for_each_entry_rcu(clt_path, &clt->paths_list, s.entry) {
817 if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED)
818 continue;
819
820 if (!list_empty(raw_cpu_ptr(clt_path->mp_skip_entry)))
821 continue;
822
823 inflight = atomic_read(&clt_path->stats->inflight);
824
825 if (inflight < min_inflight) {
826 min_inflight = inflight;
827 min_path = clt_path;
828 }
829 }
830
831 /*
832 * add the path to the skip list, so that next time we can get
833 * a different one
834 */
835 if (min_path)
836 list_add(raw_cpu_ptr(min_path->mp_skip_entry), &it->skip_list);
837
838 return min_path;
839 }
840
841 /**
842 * get_next_path_min_latency() - Returns path with minimal latency.
843 * @it: the path pointer
844 *
845 * Return: a path with the lowest latency or NULL if all paths are tried
846 *
847 * Locks:
848 * rcu_read_lock() must be hold.
849 *
850 * Related to @MP_POLICY_MIN_LATENCY
851 *
852 * This DOES skip an already-tried path.
853 * There is a skip-list to skip a path if the path has tried but failed.
854 * It will try the minimum latency path and then the second minimum latency
855 * path and so on. Finally it will return NULL if all paths are tried.
856 * Therefore the caller MUST check the returned
857 * path is NULL and trigger the IO error.
858 */
get_next_path_min_latency(struct path_it * it)859 static struct rtrs_clt_path *get_next_path_min_latency(struct path_it *it)
860 {
861 struct rtrs_clt_path *min_path = NULL;
862 struct rtrs_clt_sess *clt = it->clt;
863 struct rtrs_clt_path *clt_path;
864 ktime_t min_latency = KTIME_MAX;
865 ktime_t latency;
866
867 list_for_each_entry_rcu(clt_path, &clt->paths_list, s.entry) {
868 if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED)
869 continue;
870
871 if (!list_empty(raw_cpu_ptr(clt_path->mp_skip_entry)))
872 continue;
873
874 latency = clt_path->s.hb_cur_latency;
875
876 if (latency < min_latency) {
877 min_latency = latency;
878 min_path = clt_path;
879 }
880 }
881
882 /*
883 * add the path to the skip list, so that next time we can get
884 * a different one
885 */
886 if (min_path)
887 list_add(raw_cpu_ptr(min_path->mp_skip_entry), &it->skip_list);
888
889 return min_path;
890 }
891
path_it_init(struct path_it * it,struct rtrs_clt_sess * clt)892 static inline void path_it_init(struct path_it *it, struct rtrs_clt_sess *clt)
893 {
894 INIT_LIST_HEAD(&it->skip_list);
895 it->clt = clt;
896 it->i = 0;
897
898 if (clt->mp_policy == MP_POLICY_RR)
899 it->next_path = get_next_path_rr;
900 else if (clt->mp_policy == MP_POLICY_MIN_INFLIGHT)
901 it->next_path = get_next_path_min_inflight;
902 else
903 it->next_path = get_next_path_min_latency;
904 }
905
path_it_deinit(struct path_it * it)906 static inline void path_it_deinit(struct path_it *it)
907 {
908 struct list_head *skip, *tmp;
909 /*
910 * The skip_list is used only for the MIN_INFLIGHT and MIN_LATENCY policies.
911 * We need to remove paths from it, so that next IO can insert
912 * paths (->mp_skip_entry) into a skip_list again.
913 */
914 list_for_each_safe(skip, tmp, &it->skip_list)
915 list_del_init(skip);
916 }
917
918 /**
919 * rtrs_clt_init_req() - Initialize an rtrs_clt_io_req holding information
920 * about an inflight IO.
921 * The user buffer holding user control message (not data) is copied into
922 * the corresponding buffer of rtrs_iu (req->iu->buf), which later on will
923 * also hold the control message of rtrs.
924 * @req: an io request holding information about IO.
925 * @clt_path: client path
926 * @conf: conformation callback function to notify upper layer.
927 * @permit: permit for allocation of RDMA remote buffer
928 * @priv: private pointer
929 * @vec: kernel vector containing control message
930 * @usr_len: length of the user message
931 * @sg: scater list for IO data
932 * @sg_cnt: number of scater list entries
933 * @data_len: length of the IO data
934 * @dir: direction of the IO.
935 */
rtrs_clt_init_req(struct rtrs_clt_io_req * req,struct rtrs_clt_path * clt_path,void (* conf)(void * priv,int errno),struct rtrs_permit * permit,void * priv,const struct kvec * vec,size_t usr_len,struct scatterlist * sg,size_t sg_cnt,size_t data_len,int dir)936 static void rtrs_clt_init_req(struct rtrs_clt_io_req *req,
937 struct rtrs_clt_path *clt_path,
938 void (*conf)(void *priv, int errno),
939 struct rtrs_permit *permit, void *priv,
940 const struct kvec *vec, size_t usr_len,
941 struct scatterlist *sg, size_t sg_cnt,
942 size_t data_len, int dir)
943 {
944 struct iov_iter iter;
945 size_t len;
946
947 req->permit = permit;
948 req->in_use = true;
949 req->usr_len = usr_len;
950 req->data_len = data_len;
951 req->sglist = sg;
952 req->sg_cnt = sg_cnt;
953 req->priv = priv;
954 req->dir = dir;
955 req->con = rtrs_permit_to_clt_con(clt_path, permit);
956 req->conf = conf;
957 req->need_inv = false;
958 req->need_inv_comp = false;
959 req->inv_errno = 0;
960 refcount_set(&req->ref, 1);
961 req->mp_policy = clt_path->clt->mp_policy;
962
963 iov_iter_kvec(&iter, READ, vec, 1, usr_len);
964 len = _copy_from_iter(req->iu->buf, usr_len, &iter);
965 WARN_ON(len != usr_len);
966
967 reinit_completion(&req->inv_comp);
968 }
969
970 static struct rtrs_clt_io_req *
rtrs_clt_get_req(struct rtrs_clt_path * clt_path,void (* conf)(void * priv,int errno),struct rtrs_permit * permit,void * priv,const struct kvec * vec,size_t usr_len,struct scatterlist * sg,size_t sg_cnt,size_t data_len,int dir)971 rtrs_clt_get_req(struct rtrs_clt_path *clt_path,
972 void (*conf)(void *priv, int errno),
973 struct rtrs_permit *permit, void *priv,
974 const struct kvec *vec, size_t usr_len,
975 struct scatterlist *sg, size_t sg_cnt,
976 size_t data_len, int dir)
977 {
978 struct rtrs_clt_io_req *req;
979
980 req = &clt_path->reqs[permit->mem_id];
981 rtrs_clt_init_req(req, clt_path, conf, permit, priv, vec, usr_len,
982 sg, sg_cnt, data_len, dir);
983 return req;
984 }
985
986 static struct rtrs_clt_io_req *
rtrs_clt_get_copy_req(struct rtrs_clt_path * alive_path,struct rtrs_clt_io_req * fail_req)987 rtrs_clt_get_copy_req(struct rtrs_clt_path *alive_path,
988 struct rtrs_clt_io_req *fail_req)
989 {
990 struct rtrs_clt_io_req *req;
991 struct kvec vec = {
992 .iov_base = fail_req->iu->buf,
993 .iov_len = fail_req->usr_len
994 };
995
996 req = &alive_path->reqs[fail_req->permit->mem_id];
997 rtrs_clt_init_req(req, alive_path, fail_req->conf, fail_req->permit,
998 fail_req->priv, &vec, fail_req->usr_len,
999 fail_req->sglist, fail_req->sg_cnt,
1000 fail_req->data_len, fail_req->dir);
1001 return req;
1002 }
1003
rtrs_post_rdma_write_sg(struct rtrs_clt_con * con,struct rtrs_clt_io_req * req,struct rtrs_rbuf * rbuf,bool fr_en,u32 count,u32 size,u32 imm,struct ib_send_wr * wr,struct ib_send_wr * tail)1004 static int rtrs_post_rdma_write_sg(struct rtrs_clt_con *con,
1005 struct rtrs_clt_io_req *req,
1006 struct rtrs_rbuf *rbuf, bool fr_en,
1007 u32 count, u32 size, u32 imm,
1008 struct ib_send_wr *wr,
1009 struct ib_send_wr *tail)
1010 {
1011 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
1012 struct ib_sge *sge = req->sge;
1013 enum ib_send_flags flags;
1014 struct scatterlist *sg;
1015 size_t num_sge;
1016 int i;
1017 struct ib_send_wr *ptail = NULL;
1018
1019 if (fr_en) {
1020 i = 0;
1021 sge[i].addr = req->mr->iova;
1022 sge[i].length = req->mr->length;
1023 sge[i].lkey = req->mr->lkey;
1024 i++;
1025 num_sge = 2;
1026 ptail = tail;
1027 } else {
1028 for_each_sg(req->sglist, sg, count, i) {
1029 sge[i].addr = sg_dma_address(sg);
1030 sge[i].length = sg_dma_len(sg);
1031 sge[i].lkey = clt_path->s.dev->ib_pd->local_dma_lkey;
1032 }
1033 num_sge = 1 + count;
1034 }
1035 sge[i].addr = req->iu->dma_addr;
1036 sge[i].length = size;
1037 sge[i].lkey = clt_path->s.dev->ib_pd->local_dma_lkey;
1038
1039 /*
1040 * From time to time we have to post signalled sends,
1041 * or send queue will fill up and only QP reset can help.
1042 */
1043 flags = atomic_inc_return(&con->c.wr_cnt) % clt_path->s.signal_interval ?
1044 0 : IB_SEND_SIGNALED;
1045
1046 ib_dma_sync_single_for_device(clt_path->s.dev->ib_dev,
1047 req->iu->dma_addr,
1048 size, DMA_TO_DEVICE);
1049
1050 return rtrs_iu_post_rdma_write_imm(&con->c, req->iu, sge, num_sge,
1051 rbuf->rkey, rbuf->addr, imm,
1052 flags, wr, ptail);
1053 }
1054
rtrs_map_sg_fr(struct rtrs_clt_io_req * req,size_t count)1055 static int rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count)
1056 {
1057 int nr;
1058
1059 /* Align the MR to a 4K page size to match the block virt boundary */
1060 nr = ib_map_mr_sg(req->mr, req->sglist, count, NULL, SZ_4K);
1061 if (nr < 0)
1062 return nr;
1063 if (nr < req->sg_cnt)
1064 return -EINVAL;
1065 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1066
1067 return nr;
1068 }
1069
rtrs_clt_write_req(struct rtrs_clt_io_req * req)1070 static int rtrs_clt_write_req(struct rtrs_clt_io_req *req)
1071 {
1072 struct rtrs_clt_con *con = req->con;
1073 struct rtrs_path *s = con->c.path;
1074 struct rtrs_clt_path *clt_path = to_clt_path(s);
1075 struct rtrs_msg_rdma_write *msg;
1076
1077 struct rtrs_rbuf *rbuf;
1078 int ret, count = 0;
1079 u32 imm, buf_id;
1080 struct ib_reg_wr rwr;
1081 struct ib_send_wr inv_wr;
1082 struct ib_send_wr *wr = NULL;
1083 bool fr_en = false;
1084
1085 const size_t tsize = sizeof(*msg) + req->data_len + req->usr_len;
1086
1087 if (tsize > clt_path->chunk_size) {
1088 rtrs_wrn(s, "Write request failed, size too big %zu > %d\n",
1089 tsize, clt_path->chunk_size);
1090 return -EMSGSIZE;
1091 }
1092 if (req->sg_cnt) {
1093 count = ib_dma_map_sg(clt_path->s.dev->ib_dev, req->sglist,
1094 req->sg_cnt, req->dir);
1095 if (!count) {
1096 rtrs_wrn(s, "Write request failed, map failed\n");
1097 return -EINVAL;
1098 }
1099 }
1100 /* put rtrs msg after sg and user message */
1101 msg = req->iu->buf + req->usr_len;
1102 msg->type = cpu_to_le16(RTRS_MSG_WRITE);
1103 msg->usr_len = cpu_to_le16(req->usr_len);
1104
1105 /* rtrs message on server side will be after user data and message */
1106 imm = req->permit->mem_off + req->data_len + req->usr_len;
1107 imm = rtrs_to_io_req_imm(imm);
1108 buf_id = req->permit->mem_id;
1109 req->sg_size = tsize;
1110 rbuf = &clt_path->rbufs[buf_id];
1111
1112 if (count) {
1113 ret = rtrs_map_sg_fr(req, count);
1114 if (ret < 0) {
1115 rtrs_err_rl(s,
1116 "Write request failed, failed to map fast reg. data, err: %d\n",
1117 ret);
1118 ib_dma_unmap_sg(clt_path->s.dev->ib_dev, req->sglist,
1119 req->sg_cnt, req->dir);
1120 return ret;
1121 }
1122 inv_wr = (struct ib_send_wr) {
1123 .opcode = IB_WR_LOCAL_INV,
1124 .wr_cqe = &req->inv_cqe,
1125 .send_flags = IB_SEND_SIGNALED,
1126 .ex.invalidate_rkey = req->mr->rkey,
1127 };
1128 req->inv_cqe.done = rtrs_clt_inv_rkey_done;
1129 rwr = (struct ib_reg_wr) {
1130 .wr.opcode = IB_WR_REG_MR,
1131 .wr.wr_cqe = &fast_reg_cqe,
1132 .mr = req->mr,
1133 .key = req->mr->rkey,
1134 .access = (IB_ACCESS_LOCAL_WRITE),
1135 };
1136 wr = &rwr.wr;
1137 fr_en = true;
1138 refcount_inc(&req->ref);
1139 }
1140 /*
1141 * Update stats now, after request is successfully sent it is not
1142 * safe anymore to touch it.
1143 */
1144 rtrs_clt_update_all_stats(req, WRITE);
1145
1146 ret = rtrs_post_rdma_write_sg(req->con, req, rbuf, fr_en, count,
1147 req->usr_len + sizeof(*msg),
1148 imm, wr, &inv_wr);
1149 if (ret) {
1150 rtrs_err_rl(s,
1151 "Write request failed: error=%d path=%s [%s:%u]\n",
1152 ret, kobject_name(&clt_path->kobj), clt_path->hca_name,
1153 clt_path->hca_port);
1154 if (req->mp_policy == MP_POLICY_MIN_INFLIGHT)
1155 atomic_dec(&clt_path->stats->inflight);
1156 if (req->sg_cnt)
1157 ib_dma_unmap_sg(clt_path->s.dev->ib_dev, req->sglist,
1158 req->sg_cnt, req->dir);
1159 }
1160
1161 return ret;
1162 }
1163
rtrs_clt_read_req(struct rtrs_clt_io_req * req)1164 static int rtrs_clt_read_req(struct rtrs_clt_io_req *req)
1165 {
1166 struct rtrs_clt_con *con = req->con;
1167 struct rtrs_path *s = con->c.path;
1168 struct rtrs_clt_path *clt_path = to_clt_path(s);
1169 struct rtrs_msg_rdma_read *msg;
1170 struct rtrs_ib_dev *dev = clt_path->s.dev;
1171
1172 struct ib_reg_wr rwr;
1173 struct ib_send_wr *wr = NULL;
1174
1175 int ret, count = 0;
1176 u32 imm, buf_id;
1177
1178 const size_t tsize = sizeof(*msg) + req->data_len + req->usr_len;
1179
1180 if (tsize > clt_path->chunk_size) {
1181 rtrs_wrn(s,
1182 "Read request failed, message size is %zu, bigger than CHUNK_SIZE %d\n",
1183 tsize, clt_path->chunk_size);
1184 return -EMSGSIZE;
1185 }
1186
1187 if (req->sg_cnt) {
1188 count = ib_dma_map_sg(dev->ib_dev, req->sglist, req->sg_cnt,
1189 req->dir);
1190 if (!count) {
1191 rtrs_wrn(s,
1192 "Read request failed, dma map failed\n");
1193 return -EINVAL;
1194 }
1195 }
1196 /* put our message into req->buf after user message*/
1197 msg = req->iu->buf + req->usr_len;
1198 msg->type = cpu_to_le16(RTRS_MSG_READ);
1199 msg->usr_len = cpu_to_le16(req->usr_len);
1200
1201 if (count) {
1202 ret = rtrs_map_sg_fr(req, count);
1203 if (ret < 0) {
1204 rtrs_err_rl(s,
1205 "Read request failed, failed to map fast reg. data, err: %d\n",
1206 ret);
1207 ib_dma_unmap_sg(dev->ib_dev, req->sglist, req->sg_cnt,
1208 req->dir);
1209 return ret;
1210 }
1211 rwr = (struct ib_reg_wr) {
1212 .wr.opcode = IB_WR_REG_MR,
1213 .wr.wr_cqe = &fast_reg_cqe,
1214 .mr = req->mr,
1215 .key = req->mr->rkey,
1216 .access = (IB_ACCESS_LOCAL_WRITE |
1217 IB_ACCESS_REMOTE_WRITE),
1218 };
1219 wr = &rwr.wr;
1220
1221 msg->sg_cnt = cpu_to_le16(1);
1222 msg->flags = cpu_to_le16(RTRS_MSG_NEED_INVAL_F);
1223
1224 msg->desc[0].addr = cpu_to_le64(req->mr->iova);
1225 msg->desc[0].key = cpu_to_le32(req->mr->rkey);
1226 msg->desc[0].len = cpu_to_le32(req->mr->length);
1227
1228 /* Further invalidation is required */
1229 req->need_inv = !!RTRS_MSG_NEED_INVAL_F;
1230
1231 } else {
1232 msg->sg_cnt = 0;
1233 msg->flags = 0;
1234 }
1235 /*
1236 * rtrs message will be after the space reserved for disk data and
1237 * user message
1238 */
1239 imm = req->permit->mem_off + req->data_len + req->usr_len;
1240 imm = rtrs_to_io_req_imm(imm);
1241 buf_id = req->permit->mem_id;
1242
1243 req->sg_size = sizeof(*msg);
1244 req->sg_size += le16_to_cpu(msg->sg_cnt) * sizeof(struct rtrs_sg_desc);
1245 req->sg_size += req->usr_len;
1246
1247 /*
1248 * Update stats now, after request is successfully sent it is not
1249 * safe anymore to touch it.
1250 */
1251 rtrs_clt_update_all_stats(req, READ);
1252
1253 ret = rtrs_post_send_rdma(req->con, req, &clt_path->rbufs[buf_id],
1254 req->data_len, imm, wr);
1255 if (ret) {
1256 rtrs_err_rl(s,
1257 "Read request failed: error=%d path=%s [%s:%u]\n",
1258 ret, kobject_name(&clt_path->kobj), clt_path->hca_name,
1259 clt_path->hca_port);
1260 if (req->mp_policy == MP_POLICY_MIN_INFLIGHT)
1261 atomic_dec(&clt_path->stats->inflight);
1262 req->need_inv = false;
1263 if (req->sg_cnt)
1264 ib_dma_unmap_sg(dev->ib_dev, req->sglist,
1265 req->sg_cnt, req->dir);
1266 }
1267
1268 return ret;
1269 }
1270
1271 /**
1272 * rtrs_clt_failover_req() - Try to find an active path for a failed request
1273 * @clt: clt context
1274 * @fail_req: a failed io request.
1275 */
rtrs_clt_failover_req(struct rtrs_clt_sess * clt,struct rtrs_clt_io_req * fail_req)1276 static int rtrs_clt_failover_req(struct rtrs_clt_sess *clt,
1277 struct rtrs_clt_io_req *fail_req)
1278 {
1279 struct rtrs_clt_path *alive_path;
1280 struct rtrs_clt_io_req *req;
1281 int err = -ECONNABORTED;
1282 struct path_it it;
1283
1284 rcu_read_lock();
1285 for (path_it_init(&it, clt);
1286 (alive_path = it.next_path(&it)) && it.i < it.clt->paths_num;
1287 it.i++) {
1288 if (READ_ONCE(alive_path->state) != RTRS_CLT_CONNECTED)
1289 continue;
1290 req = rtrs_clt_get_copy_req(alive_path, fail_req);
1291 if (req->dir == DMA_TO_DEVICE)
1292 err = rtrs_clt_write_req(req);
1293 else
1294 err = rtrs_clt_read_req(req);
1295 if (err) {
1296 req->in_use = false;
1297 continue;
1298 }
1299 /* Success path */
1300 rtrs_clt_inc_failover_cnt(alive_path->stats);
1301 break;
1302 }
1303 path_it_deinit(&it);
1304 rcu_read_unlock();
1305
1306 return err;
1307 }
1308
fail_all_outstanding_reqs(struct rtrs_clt_path * clt_path)1309 static void fail_all_outstanding_reqs(struct rtrs_clt_path *clt_path)
1310 {
1311 struct rtrs_clt_sess *clt = clt_path->clt;
1312 struct rtrs_clt_io_req *req;
1313 int i, err;
1314
1315 if (!clt_path->reqs)
1316 return;
1317 for (i = 0; i < clt_path->queue_depth; ++i) {
1318 req = &clt_path->reqs[i];
1319 if (!req->in_use)
1320 continue;
1321
1322 /*
1323 * Safely (without notification) complete failed request.
1324 * After completion this request is still useble and can
1325 * be failovered to another path.
1326 */
1327 complete_rdma_req(req, -ECONNABORTED, false, true);
1328
1329 err = rtrs_clt_failover_req(clt, req);
1330 if (err)
1331 /* Failover failed, notify anyway */
1332 req->conf(req->priv, err);
1333 }
1334 }
1335
free_path_reqs(struct rtrs_clt_path * clt_path)1336 static void free_path_reqs(struct rtrs_clt_path *clt_path)
1337 {
1338 struct rtrs_clt_io_req *req;
1339 int i;
1340
1341 if (!clt_path->reqs)
1342 return;
1343 for (i = 0; i < clt_path->queue_depth; ++i) {
1344 req = &clt_path->reqs[i];
1345 if (req->mr)
1346 ib_dereg_mr(req->mr);
1347 kfree(req->sge);
1348 rtrs_iu_free(req->iu, clt_path->s.dev->ib_dev, 1);
1349 }
1350 kfree(clt_path->reqs);
1351 clt_path->reqs = NULL;
1352 }
1353
alloc_path_reqs(struct rtrs_clt_path * clt_path)1354 static int alloc_path_reqs(struct rtrs_clt_path *clt_path)
1355 {
1356 struct rtrs_clt_io_req *req;
1357 int i, err = -ENOMEM;
1358
1359 clt_path->reqs = kcalloc(clt_path->queue_depth,
1360 sizeof(*clt_path->reqs),
1361 GFP_KERNEL);
1362 if (!clt_path->reqs)
1363 return -ENOMEM;
1364
1365 for (i = 0; i < clt_path->queue_depth; ++i) {
1366 req = &clt_path->reqs[i];
1367 req->iu = rtrs_iu_alloc(1, clt_path->max_hdr_size, GFP_KERNEL,
1368 clt_path->s.dev->ib_dev,
1369 DMA_TO_DEVICE,
1370 rtrs_clt_rdma_done);
1371 if (!req->iu)
1372 goto out;
1373
1374 req->sge = kcalloc(2, sizeof(*req->sge), GFP_KERNEL);
1375 if (!req->sge)
1376 goto out;
1377
1378 req->mr = ib_alloc_mr(clt_path->s.dev->ib_pd,
1379 IB_MR_TYPE_MEM_REG,
1380 clt_path->max_pages_per_mr);
1381 if (IS_ERR(req->mr)) {
1382 err = PTR_ERR(req->mr);
1383 req->mr = NULL;
1384 pr_err("Failed to alloc clt_path->max_pages_per_mr %d\n",
1385 clt_path->max_pages_per_mr);
1386 goto out;
1387 }
1388
1389 init_completion(&req->inv_comp);
1390 }
1391
1392 return 0;
1393
1394 out:
1395 free_path_reqs(clt_path);
1396
1397 return err;
1398 }
1399
alloc_permits(struct rtrs_clt_sess * clt)1400 static int alloc_permits(struct rtrs_clt_sess *clt)
1401 {
1402 unsigned int chunk_bits;
1403 int err, i;
1404
1405 clt->permits_map = kcalloc(BITS_TO_LONGS(clt->queue_depth),
1406 sizeof(long), GFP_KERNEL);
1407 if (!clt->permits_map) {
1408 err = -ENOMEM;
1409 goto out_err;
1410 }
1411 clt->permits = kcalloc(clt->queue_depth, permit_size(clt), GFP_KERNEL);
1412 if (!clt->permits) {
1413 err = -ENOMEM;
1414 goto err_map;
1415 }
1416 chunk_bits = ilog2(clt->queue_depth - 1) + 1;
1417 for (i = 0; i < clt->queue_depth; i++) {
1418 struct rtrs_permit *permit;
1419
1420 permit = get_permit(clt, i);
1421 permit->mem_id = i;
1422 permit->mem_off = i << (MAX_IMM_PAYL_BITS - chunk_bits);
1423 }
1424
1425 return 0;
1426
1427 err_map:
1428 kfree(clt->permits_map);
1429 clt->permits_map = NULL;
1430 out_err:
1431 return err;
1432 }
1433
free_permits(struct rtrs_clt_sess * clt)1434 static void free_permits(struct rtrs_clt_sess *clt)
1435 {
1436 if (clt->permits_map) {
1437 size_t sz = clt->queue_depth;
1438
1439 wait_event(clt->permits_wait,
1440 find_first_bit(clt->permits_map, sz) >= sz);
1441 }
1442 kfree(clt->permits_map);
1443 clt->permits_map = NULL;
1444 kfree(clt->permits);
1445 clt->permits = NULL;
1446 }
1447
query_fast_reg_mode(struct rtrs_clt_path * clt_path)1448 static void query_fast_reg_mode(struct rtrs_clt_path *clt_path)
1449 {
1450 struct ib_device *ib_dev;
1451 u64 max_pages_per_mr;
1452 int mr_page_shift;
1453
1454 ib_dev = clt_path->s.dev->ib_dev;
1455
1456 /*
1457 * Use the smallest page size supported by the HCA, down to a
1458 * minimum of 4096 bytes. We're unlikely to build large sglists
1459 * out of smaller entries.
1460 */
1461 mr_page_shift = max(12, ffs(ib_dev->attrs.page_size_cap) - 1);
1462 max_pages_per_mr = ib_dev->attrs.max_mr_size;
1463 do_div(max_pages_per_mr, (1ull << mr_page_shift));
1464 clt_path->max_pages_per_mr =
1465 min3(clt_path->max_pages_per_mr, (u32)max_pages_per_mr,
1466 ib_dev->attrs.max_fast_reg_page_list_len);
1467 clt_path->clt->max_segments =
1468 min(clt_path->max_pages_per_mr, clt_path->clt->max_segments);
1469 }
1470
rtrs_clt_change_state_get_old(struct rtrs_clt_path * clt_path,enum rtrs_clt_state new_state,enum rtrs_clt_state * old_state)1471 static bool rtrs_clt_change_state_get_old(struct rtrs_clt_path *clt_path,
1472 enum rtrs_clt_state new_state,
1473 enum rtrs_clt_state *old_state)
1474 {
1475 bool changed;
1476
1477 spin_lock_irq(&clt_path->state_wq.lock);
1478 if (old_state)
1479 *old_state = clt_path->state;
1480 changed = rtrs_clt_change_state(clt_path, new_state);
1481 spin_unlock_irq(&clt_path->state_wq.lock);
1482
1483 return changed;
1484 }
1485
rtrs_clt_hb_err_handler(struct rtrs_con * c)1486 static void rtrs_clt_hb_err_handler(struct rtrs_con *c)
1487 {
1488 struct rtrs_clt_con *con = container_of(c, typeof(*con), c);
1489
1490 rtrs_rdma_error_recovery(con);
1491 }
1492
rtrs_clt_init_hb(struct rtrs_clt_path * clt_path)1493 static void rtrs_clt_init_hb(struct rtrs_clt_path *clt_path)
1494 {
1495 rtrs_init_hb(&clt_path->s, &io_comp_cqe,
1496 RTRS_HB_INTERVAL_MS,
1497 RTRS_HB_MISSED_MAX,
1498 rtrs_clt_hb_err_handler,
1499 rtrs_wq);
1500 }
1501
1502 static void rtrs_clt_reconnect_work(struct work_struct *work);
1503 static void rtrs_clt_close_work(struct work_struct *work);
1504
rtrs_clt_err_recovery_work(struct work_struct * work)1505 static void rtrs_clt_err_recovery_work(struct work_struct *work)
1506 {
1507 struct rtrs_clt_path *clt_path;
1508 struct rtrs_clt_sess *clt;
1509 int delay_ms;
1510
1511 clt_path = container_of(work, struct rtrs_clt_path, err_recovery_work);
1512 clt = clt_path->clt;
1513 delay_ms = clt->reconnect_delay_sec * 1000;
1514 rtrs_clt_stop_and_destroy_conns(clt_path);
1515 queue_delayed_work(rtrs_wq, &clt_path->reconnect_dwork,
1516 msecs_to_jiffies(delay_ms +
1517 prandom_u32() %
1518 RTRS_RECONNECT_SEED));
1519 }
1520
alloc_path(struct rtrs_clt_sess * clt,const struct rtrs_addr * path,size_t con_num,u32 nr_poll_queues)1521 static struct rtrs_clt_path *alloc_path(struct rtrs_clt_sess *clt,
1522 const struct rtrs_addr *path,
1523 size_t con_num, u32 nr_poll_queues)
1524 {
1525 struct rtrs_clt_path *clt_path;
1526 int err = -ENOMEM;
1527 int cpu;
1528 size_t total_con;
1529
1530 clt_path = kzalloc(sizeof(*clt_path), GFP_KERNEL);
1531 if (!clt_path)
1532 goto err;
1533
1534 /*
1535 * irqmode and poll
1536 * +1: Extra connection for user messages
1537 */
1538 total_con = con_num + nr_poll_queues + 1;
1539 clt_path->s.con = kcalloc(total_con, sizeof(*clt_path->s.con),
1540 GFP_KERNEL);
1541 if (!clt_path->s.con)
1542 goto err_free_path;
1543
1544 clt_path->s.con_num = total_con;
1545 clt_path->s.irq_con_num = con_num + 1;
1546
1547 clt_path->stats = kzalloc(sizeof(*clt_path->stats), GFP_KERNEL);
1548 if (!clt_path->stats)
1549 goto err_free_con;
1550
1551 mutex_init(&clt_path->init_mutex);
1552 uuid_gen(&clt_path->s.uuid);
1553 memcpy(&clt_path->s.dst_addr, path->dst,
1554 rdma_addr_size((struct sockaddr *)path->dst));
1555
1556 /*
1557 * rdma_resolve_addr() passes src_addr to cma_bind_addr, which
1558 * checks the sa_family to be non-zero. If user passed src_addr=NULL
1559 * the sess->src_addr will contain only zeros, which is then fine.
1560 */
1561 if (path->src)
1562 memcpy(&clt_path->s.src_addr, path->src,
1563 rdma_addr_size((struct sockaddr *)path->src));
1564 strscpy(clt_path->s.sessname, clt->sessname,
1565 sizeof(clt_path->s.sessname));
1566 clt_path->clt = clt;
1567 clt_path->max_pages_per_mr = RTRS_MAX_SEGMENTS;
1568 init_waitqueue_head(&clt_path->state_wq);
1569 clt_path->state = RTRS_CLT_CONNECTING;
1570 atomic_set(&clt_path->connected_cnt, 0);
1571 INIT_WORK(&clt_path->close_work, rtrs_clt_close_work);
1572 INIT_WORK(&clt_path->err_recovery_work, rtrs_clt_err_recovery_work);
1573 INIT_DELAYED_WORK(&clt_path->reconnect_dwork, rtrs_clt_reconnect_work);
1574 rtrs_clt_init_hb(clt_path);
1575
1576 clt_path->mp_skip_entry = alloc_percpu(typeof(*clt_path->mp_skip_entry));
1577 if (!clt_path->mp_skip_entry)
1578 goto err_free_stats;
1579
1580 for_each_possible_cpu(cpu)
1581 INIT_LIST_HEAD(per_cpu_ptr(clt_path->mp_skip_entry, cpu));
1582
1583 err = rtrs_clt_init_stats(clt_path->stats);
1584 if (err)
1585 goto err_free_percpu;
1586
1587 return clt_path;
1588
1589 err_free_percpu:
1590 free_percpu(clt_path->mp_skip_entry);
1591 err_free_stats:
1592 kfree(clt_path->stats);
1593 err_free_con:
1594 kfree(clt_path->s.con);
1595 err_free_path:
1596 kfree(clt_path);
1597 err:
1598 return ERR_PTR(err);
1599 }
1600
free_path(struct rtrs_clt_path * clt_path)1601 void free_path(struct rtrs_clt_path *clt_path)
1602 {
1603 free_percpu(clt_path->mp_skip_entry);
1604 mutex_destroy(&clt_path->init_mutex);
1605 kfree(clt_path->s.con);
1606 kfree(clt_path->rbufs);
1607 kfree(clt_path);
1608 }
1609
create_con(struct rtrs_clt_path * clt_path,unsigned int cid)1610 static int create_con(struct rtrs_clt_path *clt_path, unsigned int cid)
1611 {
1612 struct rtrs_clt_con *con;
1613
1614 con = kzalloc(sizeof(*con), GFP_KERNEL);
1615 if (!con)
1616 return -ENOMEM;
1617
1618 /* Map first two connections to the first CPU */
1619 con->cpu = (cid ? cid - 1 : 0) % nr_cpu_ids;
1620 con->c.cid = cid;
1621 con->c.path = &clt_path->s;
1622 /* Align with srv, init as 1 */
1623 atomic_set(&con->c.wr_cnt, 1);
1624 mutex_init(&con->con_mutex);
1625
1626 clt_path->s.con[cid] = &con->c;
1627
1628 return 0;
1629 }
1630
destroy_con(struct rtrs_clt_con * con)1631 static void destroy_con(struct rtrs_clt_con *con)
1632 {
1633 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
1634
1635 clt_path->s.con[con->c.cid] = NULL;
1636 mutex_destroy(&con->con_mutex);
1637 kfree(con);
1638 }
1639
create_con_cq_qp(struct rtrs_clt_con * con)1640 static int create_con_cq_qp(struct rtrs_clt_con *con)
1641 {
1642 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
1643 u32 max_send_wr, max_recv_wr, cq_num, max_send_sge, wr_limit;
1644 int err, cq_vector;
1645 struct rtrs_msg_rkey_rsp *rsp;
1646
1647 lockdep_assert_held(&con->con_mutex);
1648 if (con->c.cid == 0) {
1649 max_send_sge = 1;
1650 /* We must be the first here */
1651 if (WARN_ON(clt_path->s.dev))
1652 return -EINVAL;
1653
1654 /*
1655 * The whole session uses device from user connection.
1656 * Be careful not to close user connection before ib dev
1657 * is gracefully put.
1658 */
1659 clt_path->s.dev = rtrs_ib_dev_find_or_add(con->c.cm_id->device,
1660 &dev_pd);
1661 if (!clt_path->s.dev) {
1662 rtrs_wrn(clt_path->clt,
1663 "rtrs_ib_dev_find_get_or_add(): no memory\n");
1664 return -ENOMEM;
1665 }
1666 clt_path->s.dev_ref = 1;
1667 query_fast_reg_mode(clt_path);
1668 wr_limit = clt_path->s.dev->ib_dev->attrs.max_qp_wr;
1669 /*
1670 * Two (request + registration) completion for send
1671 * Two for recv if always_invalidate is set on server
1672 * or one for recv.
1673 * + 2 for drain and heartbeat
1674 * in case qp gets into error state.
1675 */
1676 max_send_wr =
1677 min_t(int, wr_limit, SERVICE_CON_QUEUE_DEPTH * 2 + 2);
1678 max_recv_wr = max_send_wr;
1679 } else {
1680 /*
1681 * Here we assume that session members are correctly set.
1682 * This is always true if user connection (cid == 0) is
1683 * established first.
1684 */
1685 if (WARN_ON(!clt_path->s.dev))
1686 return -EINVAL;
1687 if (WARN_ON(!clt_path->queue_depth))
1688 return -EINVAL;
1689
1690 wr_limit = clt_path->s.dev->ib_dev->attrs.max_qp_wr;
1691 /* Shared between connections */
1692 clt_path->s.dev_ref++;
1693 max_send_wr = min_t(int, wr_limit,
1694 /* QD * (REQ + RSP + FR REGS or INVS) + drain */
1695 clt_path->queue_depth * 3 + 1);
1696 max_recv_wr = min_t(int, wr_limit,
1697 clt_path->queue_depth * 3 + 1);
1698 max_send_sge = 2;
1699 }
1700 atomic_set(&con->c.sq_wr_avail, max_send_wr);
1701 cq_num = max_send_wr + max_recv_wr;
1702 /* alloc iu to recv new rkey reply when server reports flags set */
1703 if (clt_path->flags & RTRS_MSG_NEW_RKEY_F || con->c.cid == 0) {
1704 con->rsp_ius = rtrs_iu_alloc(cq_num, sizeof(*rsp),
1705 GFP_KERNEL,
1706 clt_path->s.dev->ib_dev,
1707 DMA_FROM_DEVICE,
1708 rtrs_clt_rdma_done);
1709 if (!con->rsp_ius)
1710 return -ENOMEM;
1711 con->queue_num = cq_num;
1712 }
1713 cq_num = max_send_wr + max_recv_wr;
1714 cq_vector = con->cpu % clt_path->s.dev->ib_dev->num_comp_vectors;
1715 if (con->c.cid >= clt_path->s.irq_con_num)
1716 err = rtrs_cq_qp_create(&clt_path->s, &con->c, max_send_sge,
1717 cq_vector, cq_num, max_send_wr,
1718 max_recv_wr, IB_POLL_DIRECT);
1719 else
1720 err = rtrs_cq_qp_create(&clt_path->s, &con->c, max_send_sge,
1721 cq_vector, cq_num, max_send_wr,
1722 max_recv_wr, IB_POLL_SOFTIRQ);
1723 /*
1724 * In case of error we do not bother to clean previous allocations,
1725 * since destroy_con_cq_qp() must be called.
1726 */
1727 return err;
1728 }
1729
destroy_con_cq_qp(struct rtrs_clt_con * con)1730 static void destroy_con_cq_qp(struct rtrs_clt_con *con)
1731 {
1732 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
1733
1734 /*
1735 * Be careful here: destroy_con_cq_qp() can be called even
1736 * create_con_cq_qp() failed, see comments there.
1737 */
1738 lockdep_assert_held(&con->con_mutex);
1739 rtrs_cq_qp_destroy(&con->c);
1740 if (con->rsp_ius) {
1741 rtrs_iu_free(con->rsp_ius, clt_path->s.dev->ib_dev,
1742 con->queue_num);
1743 con->rsp_ius = NULL;
1744 con->queue_num = 0;
1745 }
1746 if (clt_path->s.dev_ref && !--clt_path->s.dev_ref) {
1747 rtrs_ib_dev_put(clt_path->s.dev);
1748 clt_path->s.dev = NULL;
1749 }
1750 }
1751
stop_cm(struct rtrs_clt_con * con)1752 static void stop_cm(struct rtrs_clt_con *con)
1753 {
1754 rdma_disconnect(con->c.cm_id);
1755 if (con->c.qp)
1756 ib_drain_qp(con->c.qp);
1757 }
1758
destroy_cm(struct rtrs_clt_con * con)1759 static void destroy_cm(struct rtrs_clt_con *con)
1760 {
1761 rdma_destroy_id(con->c.cm_id);
1762 con->c.cm_id = NULL;
1763 }
1764
rtrs_rdma_addr_resolved(struct rtrs_clt_con * con)1765 static int rtrs_rdma_addr_resolved(struct rtrs_clt_con *con)
1766 {
1767 struct rtrs_path *s = con->c.path;
1768 int err;
1769
1770 mutex_lock(&con->con_mutex);
1771 err = create_con_cq_qp(con);
1772 mutex_unlock(&con->con_mutex);
1773 if (err) {
1774 rtrs_err(s, "create_con_cq_qp(), err: %d\n", err);
1775 return err;
1776 }
1777 err = rdma_resolve_route(con->c.cm_id, RTRS_CONNECT_TIMEOUT_MS);
1778 if (err)
1779 rtrs_err(s, "Resolving route failed, err: %d\n", err);
1780
1781 return err;
1782 }
1783
rtrs_rdma_route_resolved(struct rtrs_clt_con * con)1784 static int rtrs_rdma_route_resolved(struct rtrs_clt_con *con)
1785 {
1786 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
1787 struct rtrs_clt_sess *clt = clt_path->clt;
1788 struct rtrs_msg_conn_req msg;
1789 struct rdma_conn_param param;
1790
1791 int err;
1792
1793 param = (struct rdma_conn_param) {
1794 .retry_count = 7,
1795 .rnr_retry_count = 7,
1796 .private_data = &msg,
1797 .private_data_len = sizeof(msg),
1798 };
1799
1800 msg = (struct rtrs_msg_conn_req) {
1801 .magic = cpu_to_le16(RTRS_MAGIC),
1802 .version = cpu_to_le16(RTRS_PROTO_VER),
1803 .cid = cpu_to_le16(con->c.cid),
1804 .cid_num = cpu_to_le16(clt_path->s.con_num),
1805 .recon_cnt = cpu_to_le16(clt_path->s.recon_cnt),
1806 };
1807 msg.first_conn = clt_path->for_new_clt ? FIRST_CONN : 0;
1808 uuid_copy(&msg.sess_uuid, &clt_path->s.uuid);
1809 uuid_copy(&msg.paths_uuid, &clt->paths_uuid);
1810
1811 err = rdma_connect_locked(con->c.cm_id, ¶m);
1812 if (err)
1813 rtrs_err(clt, "rdma_connect_locked(): %d\n", err);
1814
1815 return err;
1816 }
1817
rtrs_rdma_conn_established(struct rtrs_clt_con * con,struct rdma_cm_event * ev)1818 static int rtrs_rdma_conn_established(struct rtrs_clt_con *con,
1819 struct rdma_cm_event *ev)
1820 {
1821 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
1822 struct rtrs_clt_sess *clt = clt_path->clt;
1823 const struct rtrs_msg_conn_rsp *msg;
1824 u16 version, queue_depth;
1825 int errno;
1826 u8 len;
1827
1828 msg = ev->param.conn.private_data;
1829 len = ev->param.conn.private_data_len;
1830 if (len < sizeof(*msg)) {
1831 rtrs_err(clt, "Invalid RTRS connection response\n");
1832 return -ECONNRESET;
1833 }
1834 if (le16_to_cpu(msg->magic) != RTRS_MAGIC) {
1835 rtrs_err(clt, "Invalid RTRS magic\n");
1836 return -ECONNRESET;
1837 }
1838 version = le16_to_cpu(msg->version);
1839 if (version >> 8 != RTRS_PROTO_VER_MAJOR) {
1840 rtrs_err(clt, "Unsupported major RTRS version: %d, expected %d\n",
1841 version >> 8, RTRS_PROTO_VER_MAJOR);
1842 return -ECONNRESET;
1843 }
1844 errno = le16_to_cpu(msg->errno);
1845 if (errno) {
1846 rtrs_err(clt, "Invalid RTRS message: errno %d\n",
1847 errno);
1848 return -ECONNRESET;
1849 }
1850 if (con->c.cid == 0) {
1851 queue_depth = le16_to_cpu(msg->queue_depth);
1852
1853 if (clt_path->queue_depth > 0 && queue_depth != clt_path->queue_depth) {
1854 rtrs_err(clt, "Error: queue depth changed\n");
1855
1856 /*
1857 * Stop any more reconnection attempts
1858 */
1859 clt_path->reconnect_attempts = -1;
1860 rtrs_err(clt,
1861 "Disabling auto-reconnect. Trigger a manual reconnect after issue is resolved\n");
1862 return -ECONNRESET;
1863 }
1864
1865 if (!clt_path->rbufs) {
1866 clt_path->rbufs = kcalloc(queue_depth,
1867 sizeof(*clt_path->rbufs),
1868 GFP_KERNEL);
1869 if (!clt_path->rbufs)
1870 return -ENOMEM;
1871 }
1872 clt_path->queue_depth = queue_depth;
1873 clt_path->s.signal_interval = min_not_zero(queue_depth,
1874 (unsigned short) SERVICE_CON_QUEUE_DEPTH);
1875 clt_path->max_hdr_size = le32_to_cpu(msg->max_hdr_size);
1876 clt_path->max_io_size = le32_to_cpu(msg->max_io_size);
1877 clt_path->flags = le32_to_cpu(msg->flags);
1878 clt_path->chunk_size = clt_path->max_io_size + clt_path->max_hdr_size;
1879
1880 /*
1881 * Global IO size is always a minimum.
1882 * If while a reconnection server sends us a value a bit
1883 * higher - client does not care and uses cached minimum.
1884 *
1885 * Since we can have several sessions (paths) restablishing
1886 * connections in parallel, use lock.
1887 */
1888 mutex_lock(&clt->paths_mutex);
1889 clt->queue_depth = clt_path->queue_depth;
1890 clt->max_io_size = min_not_zero(clt_path->max_io_size,
1891 clt->max_io_size);
1892 mutex_unlock(&clt->paths_mutex);
1893
1894 /*
1895 * Cache the hca_port and hca_name for sysfs
1896 */
1897 clt_path->hca_port = con->c.cm_id->port_num;
1898 scnprintf(clt_path->hca_name, sizeof(clt_path->hca_name),
1899 clt_path->s.dev->ib_dev->name);
1900 clt_path->s.src_addr = con->c.cm_id->route.addr.src_addr;
1901 /* set for_new_clt, to allow future reconnect on any path */
1902 clt_path->for_new_clt = 1;
1903 }
1904
1905 return 0;
1906 }
1907
flag_success_on_conn(struct rtrs_clt_con * con)1908 static inline void flag_success_on_conn(struct rtrs_clt_con *con)
1909 {
1910 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
1911
1912 atomic_inc(&clt_path->connected_cnt);
1913 con->cm_err = 1;
1914 }
1915
rtrs_rdma_conn_rejected(struct rtrs_clt_con * con,struct rdma_cm_event * ev)1916 static int rtrs_rdma_conn_rejected(struct rtrs_clt_con *con,
1917 struct rdma_cm_event *ev)
1918 {
1919 struct rtrs_path *s = con->c.path;
1920 const struct rtrs_msg_conn_rsp *msg;
1921 const char *rej_msg;
1922 int status, errno;
1923 u8 data_len;
1924
1925 status = ev->status;
1926 rej_msg = rdma_reject_msg(con->c.cm_id, status);
1927 msg = rdma_consumer_reject_data(con->c.cm_id, ev, &data_len);
1928
1929 if (msg && data_len >= sizeof(*msg)) {
1930 errno = (int16_t)le16_to_cpu(msg->errno);
1931 if (errno == -EBUSY)
1932 rtrs_err(s,
1933 "Previous session is still exists on the server, please reconnect later\n");
1934 else
1935 rtrs_err(s,
1936 "Connect rejected: status %d (%s), rtrs errno %d\n",
1937 status, rej_msg, errno);
1938 } else {
1939 rtrs_err(s,
1940 "Connect rejected but with malformed message: status %d (%s)\n",
1941 status, rej_msg);
1942 }
1943
1944 return -ECONNRESET;
1945 }
1946
rtrs_clt_close_conns(struct rtrs_clt_path * clt_path,bool wait)1947 void rtrs_clt_close_conns(struct rtrs_clt_path *clt_path, bool wait)
1948 {
1949 if (rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_CLOSING, NULL))
1950 queue_work(rtrs_wq, &clt_path->close_work);
1951 if (wait)
1952 flush_work(&clt_path->close_work);
1953 }
1954
flag_error_on_conn(struct rtrs_clt_con * con,int cm_err)1955 static inline void flag_error_on_conn(struct rtrs_clt_con *con, int cm_err)
1956 {
1957 if (con->cm_err == 1) {
1958 struct rtrs_clt_path *clt_path;
1959
1960 clt_path = to_clt_path(con->c.path);
1961 if (atomic_dec_and_test(&clt_path->connected_cnt))
1962
1963 wake_up(&clt_path->state_wq);
1964 }
1965 con->cm_err = cm_err;
1966 }
1967
rtrs_clt_rdma_cm_handler(struct rdma_cm_id * cm_id,struct rdma_cm_event * ev)1968 static int rtrs_clt_rdma_cm_handler(struct rdma_cm_id *cm_id,
1969 struct rdma_cm_event *ev)
1970 {
1971 struct rtrs_clt_con *con = cm_id->context;
1972 struct rtrs_path *s = con->c.path;
1973 struct rtrs_clt_path *clt_path = to_clt_path(s);
1974 int cm_err = 0;
1975
1976 switch (ev->event) {
1977 case RDMA_CM_EVENT_ADDR_RESOLVED:
1978 cm_err = rtrs_rdma_addr_resolved(con);
1979 break;
1980 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1981 cm_err = rtrs_rdma_route_resolved(con);
1982 break;
1983 case RDMA_CM_EVENT_ESTABLISHED:
1984 cm_err = rtrs_rdma_conn_established(con, ev);
1985 if (!cm_err) {
1986 /*
1987 * Report success and wake up. Here we abuse state_wq,
1988 * i.e. wake up without state change, but we set cm_err.
1989 */
1990 flag_success_on_conn(con);
1991 wake_up(&clt_path->state_wq);
1992 return 0;
1993 }
1994 break;
1995 case RDMA_CM_EVENT_REJECTED:
1996 cm_err = rtrs_rdma_conn_rejected(con, ev);
1997 break;
1998 case RDMA_CM_EVENT_DISCONNECTED:
1999 /* No message for disconnecting */
2000 cm_err = -ECONNRESET;
2001 break;
2002 case RDMA_CM_EVENT_CONNECT_ERROR:
2003 case RDMA_CM_EVENT_UNREACHABLE:
2004 case RDMA_CM_EVENT_ADDR_CHANGE:
2005 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2006 rtrs_wrn(s, "CM error (CM event: %s, err: %d)\n",
2007 rdma_event_msg(ev->event), ev->status);
2008 cm_err = -ECONNRESET;
2009 break;
2010 case RDMA_CM_EVENT_ADDR_ERROR:
2011 case RDMA_CM_EVENT_ROUTE_ERROR:
2012 rtrs_wrn(s, "CM error (CM event: %s, err: %d)\n",
2013 rdma_event_msg(ev->event), ev->status);
2014 cm_err = -EHOSTUNREACH;
2015 break;
2016 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2017 /*
2018 * Device removal is a special case. Queue close and return 0.
2019 */
2020 rtrs_clt_close_conns(clt_path, false);
2021 return 0;
2022 default:
2023 rtrs_err(s, "Unexpected RDMA CM error (CM event: %s, err: %d)\n",
2024 rdma_event_msg(ev->event), ev->status);
2025 cm_err = -ECONNRESET;
2026 break;
2027 }
2028
2029 if (cm_err) {
2030 /*
2031 * cm error makes sense only on connection establishing,
2032 * in other cases we rely on normal procedure of reconnecting.
2033 */
2034 flag_error_on_conn(con, cm_err);
2035 rtrs_rdma_error_recovery(con);
2036 }
2037
2038 return 0;
2039 }
2040
create_cm(struct rtrs_clt_con * con)2041 static int create_cm(struct rtrs_clt_con *con)
2042 {
2043 struct rtrs_path *s = con->c.path;
2044 struct rtrs_clt_path *clt_path = to_clt_path(s);
2045 struct rdma_cm_id *cm_id;
2046 int err;
2047
2048 cm_id = rdma_create_id(&init_net, rtrs_clt_rdma_cm_handler, con,
2049 clt_path->s.dst_addr.ss_family == AF_IB ?
2050 RDMA_PS_IB : RDMA_PS_TCP, IB_QPT_RC);
2051 if (IS_ERR(cm_id)) {
2052 err = PTR_ERR(cm_id);
2053 rtrs_err(s, "Failed to create CM ID, err: %d\n", err);
2054
2055 return err;
2056 }
2057 con->c.cm_id = cm_id;
2058 con->cm_err = 0;
2059 /* allow the port to be reused */
2060 err = rdma_set_reuseaddr(cm_id, 1);
2061 if (err != 0) {
2062 rtrs_err(s, "Set address reuse failed, err: %d\n", err);
2063 goto destroy_cm;
2064 }
2065 err = rdma_resolve_addr(cm_id, (struct sockaddr *)&clt_path->s.src_addr,
2066 (struct sockaddr *)&clt_path->s.dst_addr,
2067 RTRS_CONNECT_TIMEOUT_MS);
2068 if (err) {
2069 rtrs_err(s, "Failed to resolve address, err: %d\n", err);
2070 goto destroy_cm;
2071 }
2072 /*
2073 * Combine connection status and session events. This is needed
2074 * for waiting two possible cases: cm_err has something meaningful
2075 * or session state was really changed to error by device removal.
2076 */
2077 err = wait_event_interruptible_timeout(
2078 clt_path->state_wq,
2079 con->cm_err || clt_path->state != RTRS_CLT_CONNECTING,
2080 msecs_to_jiffies(RTRS_CONNECT_TIMEOUT_MS));
2081 if (err == 0 || err == -ERESTARTSYS) {
2082 if (err == 0)
2083 err = -ETIMEDOUT;
2084 /* Timedout or interrupted */
2085 goto errr;
2086 }
2087 if (con->cm_err < 0) {
2088 err = con->cm_err;
2089 goto errr;
2090 }
2091 if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTING) {
2092 /* Device removal */
2093 err = -ECONNABORTED;
2094 goto errr;
2095 }
2096
2097 return 0;
2098
2099 errr:
2100 stop_cm(con);
2101 mutex_lock(&con->con_mutex);
2102 destroy_con_cq_qp(con);
2103 mutex_unlock(&con->con_mutex);
2104 destroy_cm:
2105 destroy_cm(con);
2106
2107 return err;
2108 }
2109
rtrs_clt_path_up(struct rtrs_clt_path * clt_path)2110 static void rtrs_clt_path_up(struct rtrs_clt_path *clt_path)
2111 {
2112 struct rtrs_clt_sess *clt = clt_path->clt;
2113 int up;
2114
2115 /*
2116 * We can fire RECONNECTED event only when all paths were
2117 * connected on rtrs_clt_open(), then each was disconnected
2118 * and the first one connected again. That's why this nasty
2119 * game with counter value.
2120 */
2121
2122 mutex_lock(&clt->paths_ev_mutex);
2123 up = ++clt->paths_up;
2124 /*
2125 * Here it is safe to access paths num directly since up counter
2126 * is greater than MAX_PATHS_NUM only while rtrs_clt_open() is
2127 * in progress, thus paths removals are impossible.
2128 */
2129 if (up > MAX_PATHS_NUM && up == MAX_PATHS_NUM + clt->paths_num)
2130 clt->paths_up = clt->paths_num;
2131 else if (up == 1)
2132 clt->link_ev(clt->priv, RTRS_CLT_LINK_EV_RECONNECTED);
2133 mutex_unlock(&clt->paths_ev_mutex);
2134
2135 /* Mark session as established */
2136 clt_path->established = true;
2137 clt_path->reconnect_attempts = 0;
2138 clt_path->stats->reconnects.successful_cnt++;
2139 }
2140
rtrs_clt_path_down(struct rtrs_clt_path * clt_path)2141 static void rtrs_clt_path_down(struct rtrs_clt_path *clt_path)
2142 {
2143 struct rtrs_clt_sess *clt = clt_path->clt;
2144
2145 if (!clt_path->established)
2146 return;
2147
2148 clt_path->established = false;
2149 mutex_lock(&clt->paths_ev_mutex);
2150 WARN_ON(!clt->paths_up);
2151 if (--clt->paths_up == 0)
2152 clt->link_ev(clt->priv, RTRS_CLT_LINK_EV_DISCONNECTED);
2153 mutex_unlock(&clt->paths_ev_mutex);
2154 }
2155
rtrs_clt_stop_and_destroy_conns(struct rtrs_clt_path * clt_path)2156 static void rtrs_clt_stop_and_destroy_conns(struct rtrs_clt_path *clt_path)
2157 {
2158 struct rtrs_clt_con *con;
2159 unsigned int cid;
2160
2161 WARN_ON(READ_ONCE(clt_path->state) == RTRS_CLT_CONNECTED);
2162
2163 /*
2164 * Possible race with rtrs_clt_open(), when DEVICE_REMOVAL comes
2165 * exactly in between. Start destroying after it finishes.
2166 */
2167 mutex_lock(&clt_path->init_mutex);
2168 mutex_unlock(&clt_path->init_mutex);
2169
2170 /*
2171 * All IO paths must observe !CONNECTED state before we
2172 * free everything.
2173 */
2174 synchronize_rcu();
2175
2176 rtrs_stop_hb(&clt_path->s);
2177
2178 /*
2179 * The order it utterly crucial: firstly disconnect and complete all
2180 * rdma requests with error (thus set in_use=false for requests),
2181 * then fail outstanding requests checking in_use for each, and
2182 * eventually notify upper layer about session disconnection.
2183 */
2184
2185 for (cid = 0; cid < clt_path->s.con_num; cid++) {
2186 if (!clt_path->s.con[cid])
2187 break;
2188 con = to_clt_con(clt_path->s.con[cid]);
2189 stop_cm(con);
2190 }
2191 fail_all_outstanding_reqs(clt_path);
2192 free_path_reqs(clt_path);
2193 rtrs_clt_path_down(clt_path);
2194
2195 /*
2196 * Wait for graceful shutdown, namely when peer side invokes
2197 * rdma_disconnect(). 'connected_cnt' is decremented only on
2198 * CM events, thus if other side had crashed and hb has detected
2199 * something is wrong, here we will stuck for exactly timeout ms,
2200 * since CM does not fire anything. That is fine, we are not in
2201 * hurry.
2202 */
2203 wait_event_timeout(clt_path->state_wq,
2204 !atomic_read(&clt_path->connected_cnt),
2205 msecs_to_jiffies(RTRS_CONNECT_TIMEOUT_MS));
2206
2207 for (cid = 0; cid < clt_path->s.con_num; cid++) {
2208 if (!clt_path->s.con[cid])
2209 break;
2210 con = to_clt_con(clt_path->s.con[cid]);
2211 mutex_lock(&con->con_mutex);
2212 destroy_con_cq_qp(con);
2213 mutex_unlock(&con->con_mutex);
2214 destroy_cm(con);
2215 destroy_con(con);
2216 }
2217 }
2218
xchg_paths(struct rtrs_clt_path __rcu ** rcu_ppcpu_path,struct rtrs_clt_path * clt_path,struct rtrs_clt_path * next)2219 static inline bool xchg_paths(struct rtrs_clt_path __rcu **rcu_ppcpu_path,
2220 struct rtrs_clt_path *clt_path,
2221 struct rtrs_clt_path *next)
2222 {
2223 struct rtrs_clt_path **ppcpu_path;
2224
2225 /* Call cmpxchg() without sparse warnings */
2226 ppcpu_path = (typeof(ppcpu_path))rcu_ppcpu_path;
2227 return clt_path == cmpxchg(ppcpu_path, clt_path, next);
2228 }
2229
rtrs_clt_remove_path_from_arr(struct rtrs_clt_path * clt_path)2230 static void rtrs_clt_remove_path_from_arr(struct rtrs_clt_path *clt_path)
2231 {
2232 struct rtrs_clt_sess *clt = clt_path->clt;
2233 struct rtrs_clt_path *next;
2234 bool wait_for_grace = false;
2235 int cpu;
2236
2237 mutex_lock(&clt->paths_mutex);
2238 list_del_rcu(&clt_path->s.entry);
2239
2240 /* Make sure everybody observes path removal. */
2241 synchronize_rcu();
2242
2243 /*
2244 * At this point nobody sees @sess in the list, but still we have
2245 * dangling pointer @pcpu_path which _can_ point to @sess. Since
2246 * nobody can observe @sess in the list, we guarantee that IO path
2247 * will not assign @sess to @pcpu_path, i.e. @pcpu_path can be equal
2248 * to @sess, but can never again become @sess.
2249 */
2250
2251 /*
2252 * Decrement paths number only after grace period, because
2253 * caller of do_each_path() must firstly observe list without
2254 * path and only then decremented paths number.
2255 *
2256 * Otherwise there can be the following situation:
2257 * o Two paths exist and IO is coming.
2258 * o One path is removed:
2259 * CPU#0 CPU#1
2260 * do_each_path(): rtrs_clt_remove_path_from_arr():
2261 * path = get_next_path()
2262 * ^^^ list_del_rcu(path)
2263 * [!CONNECTED path] clt->paths_num--
2264 * ^^^^^^^^^
2265 * load clt->paths_num from 2 to 1
2266 * ^^^^^^^^^
2267 * sees 1
2268 *
2269 * path is observed as !CONNECTED, but do_each_path() loop
2270 * ends, because expression i < clt->paths_num is false.
2271 */
2272 clt->paths_num--;
2273
2274 /*
2275 * Get @next connection from current @sess which is going to be
2276 * removed. If @sess is the last element, then @next is NULL.
2277 */
2278 rcu_read_lock();
2279 next = rtrs_clt_get_next_path_or_null(&clt->paths_list, clt_path);
2280 rcu_read_unlock();
2281
2282 /*
2283 * @pcpu paths can still point to the path which is going to be
2284 * removed, so change the pointer manually.
2285 */
2286 for_each_possible_cpu(cpu) {
2287 struct rtrs_clt_path __rcu **ppcpu_path;
2288
2289 ppcpu_path = per_cpu_ptr(clt->pcpu_path, cpu);
2290 if (rcu_dereference_protected(*ppcpu_path,
2291 lockdep_is_held(&clt->paths_mutex)) != clt_path)
2292 /*
2293 * synchronize_rcu() was called just after deleting
2294 * entry from the list, thus IO code path cannot
2295 * change pointer back to the pointer which is going
2296 * to be removed, we are safe here.
2297 */
2298 continue;
2299
2300 /*
2301 * We race with IO code path, which also changes pointer,
2302 * thus we have to be careful not to overwrite it.
2303 */
2304 if (xchg_paths(ppcpu_path, clt_path, next))
2305 /*
2306 * @ppcpu_path was successfully replaced with @next,
2307 * that means that someone could also pick up the
2308 * @sess and dereferencing it right now, so wait for
2309 * a grace period is required.
2310 */
2311 wait_for_grace = true;
2312 }
2313 if (wait_for_grace)
2314 synchronize_rcu();
2315
2316 mutex_unlock(&clt->paths_mutex);
2317 }
2318
rtrs_clt_add_path_to_arr(struct rtrs_clt_path * clt_path)2319 static void rtrs_clt_add_path_to_arr(struct rtrs_clt_path *clt_path)
2320 {
2321 struct rtrs_clt_sess *clt = clt_path->clt;
2322
2323 mutex_lock(&clt->paths_mutex);
2324 clt->paths_num++;
2325
2326 list_add_tail_rcu(&clt_path->s.entry, &clt->paths_list);
2327 mutex_unlock(&clt->paths_mutex);
2328 }
2329
rtrs_clt_close_work(struct work_struct * work)2330 static void rtrs_clt_close_work(struct work_struct *work)
2331 {
2332 struct rtrs_clt_path *clt_path;
2333
2334 clt_path = container_of(work, struct rtrs_clt_path, close_work);
2335
2336 cancel_work_sync(&clt_path->err_recovery_work);
2337 cancel_delayed_work_sync(&clt_path->reconnect_dwork);
2338 rtrs_clt_stop_and_destroy_conns(clt_path);
2339 rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_CLOSED, NULL);
2340 }
2341
init_conns(struct rtrs_clt_path * clt_path)2342 static int init_conns(struct rtrs_clt_path *clt_path)
2343 {
2344 unsigned int cid;
2345 int err;
2346
2347 /*
2348 * On every new session connections increase reconnect counter
2349 * to avoid clashes with previous sessions not yet closed
2350 * sessions on a server side.
2351 */
2352 clt_path->s.recon_cnt++;
2353
2354 /* Establish all RDMA connections */
2355 for (cid = 0; cid < clt_path->s.con_num; cid++) {
2356 err = create_con(clt_path, cid);
2357 if (err)
2358 goto destroy;
2359
2360 err = create_cm(to_clt_con(clt_path->s.con[cid]));
2361 if (err) {
2362 destroy_con(to_clt_con(clt_path->s.con[cid]));
2363 goto destroy;
2364 }
2365 }
2366 err = alloc_path_reqs(clt_path);
2367 if (err)
2368 goto destroy;
2369
2370 rtrs_start_hb(&clt_path->s);
2371
2372 return 0;
2373
2374 destroy:
2375 while (cid--) {
2376 struct rtrs_clt_con *con = to_clt_con(clt_path->s.con[cid]);
2377
2378 stop_cm(con);
2379
2380 mutex_lock(&con->con_mutex);
2381 destroy_con_cq_qp(con);
2382 mutex_unlock(&con->con_mutex);
2383 destroy_cm(con);
2384 destroy_con(con);
2385 }
2386 /*
2387 * If we've never taken async path and got an error, say,
2388 * doing rdma_resolve_addr(), switch to CONNECTION_ERR state
2389 * manually to keep reconnecting.
2390 */
2391 rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_CONNECTING_ERR, NULL);
2392
2393 return err;
2394 }
2395
rtrs_clt_info_req_done(struct ib_cq * cq,struct ib_wc * wc)2396 static void rtrs_clt_info_req_done(struct ib_cq *cq, struct ib_wc *wc)
2397 {
2398 struct rtrs_clt_con *con = to_clt_con(wc->qp->qp_context);
2399 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
2400 struct rtrs_iu *iu;
2401
2402 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
2403 rtrs_iu_free(iu, clt_path->s.dev->ib_dev, 1);
2404
2405 if (wc->status != IB_WC_SUCCESS) {
2406 rtrs_err(clt_path->clt, "Path info request send failed: %s\n",
2407 ib_wc_status_msg(wc->status));
2408 rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_CONNECTING_ERR, NULL);
2409 return;
2410 }
2411
2412 rtrs_clt_update_wc_stats(con);
2413 }
2414
process_info_rsp(struct rtrs_clt_path * clt_path,const struct rtrs_msg_info_rsp * msg)2415 static int process_info_rsp(struct rtrs_clt_path *clt_path,
2416 const struct rtrs_msg_info_rsp *msg)
2417 {
2418 unsigned int sg_cnt, total_len;
2419 int i, sgi;
2420
2421 sg_cnt = le16_to_cpu(msg->sg_cnt);
2422 if (!sg_cnt || (clt_path->queue_depth % sg_cnt)) {
2423 rtrs_err(clt_path->clt,
2424 "Incorrect sg_cnt %d, is not multiple\n",
2425 sg_cnt);
2426 return -EINVAL;
2427 }
2428
2429 /*
2430 * Check if IB immediate data size is enough to hold the mem_id and
2431 * the offset inside the memory chunk.
2432 */
2433 if ((ilog2(sg_cnt - 1) + 1) + (ilog2(clt_path->chunk_size - 1) + 1) >
2434 MAX_IMM_PAYL_BITS) {
2435 rtrs_err(clt_path->clt,
2436 "RDMA immediate size (%db) not enough to encode %d buffers of size %dB\n",
2437 MAX_IMM_PAYL_BITS, sg_cnt, clt_path->chunk_size);
2438 return -EINVAL;
2439 }
2440 total_len = 0;
2441 for (sgi = 0, i = 0; sgi < sg_cnt && i < clt_path->queue_depth; sgi++) {
2442 const struct rtrs_sg_desc *desc = &msg->desc[sgi];
2443 u32 len, rkey;
2444 u64 addr;
2445
2446 addr = le64_to_cpu(desc->addr);
2447 rkey = le32_to_cpu(desc->key);
2448 len = le32_to_cpu(desc->len);
2449
2450 total_len += len;
2451
2452 if (!len || (len % clt_path->chunk_size)) {
2453 rtrs_err(clt_path->clt, "Incorrect [%d].len %d\n",
2454 sgi,
2455 len);
2456 return -EINVAL;
2457 }
2458 for ( ; len && i < clt_path->queue_depth; i++) {
2459 clt_path->rbufs[i].addr = addr;
2460 clt_path->rbufs[i].rkey = rkey;
2461
2462 len -= clt_path->chunk_size;
2463 addr += clt_path->chunk_size;
2464 }
2465 }
2466 /* Sanity check */
2467 if (sgi != sg_cnt || i != clt_path->queue_depth) {
2468 rtrs_err(clt_path->clt,
2469 "Incorrect sg vector, not fully mapped\n");
2470 return -EINVAL;
2471 }
2472 if (total_len != clt_path->chunk_size * clt_path->queue_depth) {
2473 rtrs_err(clt_path->clt, "Incorrect total_len %d\n", total_len);
2474 return -EINVAL;
2475 }
2476
2477 return 0;
2478 }
2479
rtrs_clt_info_rsp_done(struct ib_cq * cq,struct ib_wc * wc)2480 static void rtrs_clt_info_rsp_done(struct ib_cq *cq, struct ib_wc *wc)
2481 {
2482 struct rtrs_clt_con *con = to_clt_con(wc->qp->qp_context);
2483 struct rtrs_clt_path *clt_path = to_clt_path(con->c.path);
2484 struct rtrs_msg_info_rsp *msg;
2485 enum rtrs_clt_state state;
2486 struct rtrs_iu *iu;
2487 size_t rx_sz;
2488 int err;
2489
2490 state = RTRS_CLT_CONNECTING_ERR;
2491
2492 WARN_ON(con->c.cid);
2493 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
2494 if (wc->status != IB_WC_SUCCESS) {
2495 rtrs_err(clt_path->clt, "Path info response recv failed: %s\n",
2496 ib_wc_status_msg(wc->status));
2497 goto out;
2498 }
2499 WARN_ON(wc->opcode != IB_WC_RECV);
2500
2501 if (wc->byte_len < sizeof(*msg)) {
2502 rtrs_err(clt_path->clt, "Path info response is malformed: size %d\n",
2503 wc->byte_len);
2504 goto out;
2505 }
2506 ib_dma_sync_single_for_cpu(clt_path->s.dev->ib_dev, iu->dma_addr,
2507 iu->size, DMA_FROM_DEVICE);
2508 msg = iu->buf;
2509 if (le16_to_cpu(msg->type) != RTRS_MSG_INFO_RSP) {
2510 rtrs_err(clt_path->clt, "Path info response is malformed: type %d\n",
2511 le16_to_cpu(msg->type));
2512 goto out;
2513 }
2514 rx_sz = sizeof(*msg);
2515 rx_sz += sizeof(msg->desc[0]) * le16_to_cpu(msg->sg_cnt);
2516 if (wc->byte_len < rx_sz) {
2517 rtrs_err(clt_path->clt, "Path info response is malformed: size %d\n",
2518 wc->byte_len);
2519 goto out;
2520 }
2521 err = process_info_rsp(clt_path, msg);
2522 if (err)
2523 goto out;
2524
2525 err = post_recv_path(clt_path);
2526 if (err)
2527 goto out;
2528
2529 state = RTRS_CLT_CONNECTED;
2530
2531 out:
2532 rtrs_clt_update_wc_stats(con);
2533 rtrs_iu_free(iu, clt_path->s.dev->ib_dev, 1);
2534 rtrs_clt_change_state_get_old(clt_path, state, NULL);
2535 }
2536
rtrs_send_path_info(struct rtrs_clt_path * clt_path)2537 static int rtrs_send_path_info(struct rtrs_clt_path *clt_path)
2538 {
2539 struct rtrs_clt_con *usr_con = to_clt_con(clt_path->s.con[0]);
2540 struct rtrs_msg_info_req *msg;
2541 struct rtrs_iu *tx_iu, *rx_iu;
2542 size_t rx_sz;
2543 int err;
2544
2545 rx_sz = sizeof(struct rtrs_msg_info_rsp);
2546 rx_sz += sizeof(struct rtrs_sg_desc) * clt_path->queue_depth;
2547
2548 tx_iu = rtrs_iu_alloc(1, sizeof(struct rtrs_msg_info_req), GFP_KERNEL,
2549 clt_path->s.dev->ib_dev, DMA_TO_DEVICE,
2550 rtrs_clt_info_req_done);
2551 rx_iu = rtrs_iu_alloc(1, rx_sz, GFP_KERNEL, clt_path->s.dev->ib_dev,
2552 DMA_FROM_DEVICE, rtrs_clt_info_rsp_done);
2553 if (!tx_iu || !rx_iu) {
2554 err = -ENOMEM;
2555 goto out;
2556 }
2557 /* Prepare for getting info response */
2558 err = rtrs_iu_post_recv(&usr_con->c, rx_iu);
2559 if (err) {
2560 rtrs_err(clt_path->clt, "rtrs_iu_post_recv(), err: %d\n", err);
2561 goto out;
2562 }
2563 rx_iu = NULL;
2564
2565 msg = tx_iu->buf;
2566 msg->type = cpu_to_le16(RTRS_MSG_INFO_REQ);
2567 memcpy(msg->pathname, clt_path->s.sessname, sizeof(msg->pathname));
2568
2569 ib_dma_sync_single_for_device(clt_path->s.dev->ib_dev,
2570 tx_iu->dma_addr,
2571 tx_iu->size, DMA_TO_DEVICE);
2572
2573 /* Send info request */
2574 err = rtrs_iu_post_send(&usr_con->c, tx_iu, sizeof(*msg), NULL);
2575 if (err) {
2576 rtrs_err(clt_path->clt, "rtrs_iu_post_send(), err: %d\n", err);
2577 goto out;
2578 }
2579 tx_iu = NULL;
2580
2581 /* Wait for state change */
2582 wait_event_interruptible_timeout(clt_path->state_wq,
2583 clt_path->state != RTRS_CLT_CONNECTING,
2584 msecs_to_jiffies(
2585 RTRS_CONNECT_TIMEOUT_MS));
2586 if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED) {
2587 if (READ_ONCE(clt_path->state) == RTRS_CLT_CONNECTING_ERR)
2588 err = -ECONNRESET;
2589 else
2590 err = -ETIMEDOUT;
2591 }
2592
2593 out:
2594 if (tx_iu)
2595 rtrs_iu_free(tx_iu, clt_path->s.dev->ib_dev, 1);
2596 if (rx_iu)
2597 rtrs_iu_free(rx_iu, clt_path->s.dev->ib_dev, 1);
2598 if (err)
2599 /* If we've never taken async path because of malloc problems */
2600 rtrs_clt_change_state_get_old(clt_path,
2601 RTRS_CLT_CONNECTING_ERR, NULL);
2602
2603 return err;
2604 }
2605
2606 /**
2607 * init_path() - establishes all path connections and does handshake
2608 * @clt_path: client path.
2609 * In case of error full close or reconnect procedure should be taken,
2610 * because reconnect or close async works can be started.
2611 */
init_path(struct rtrs_clt_path * clt_path)2612 static int init_path(struct rtrs_clt_path *clt_path)
2613 {
2614 int err;
2615 char str[NAME_MAX];
2616 struct rtrs_addr path = {
2617 .src = &clt_path->s.src_addr,
2618 .dst = &clt_path->s.dst_addr,
2619 };
2620
2621 rtrs_addr_to_str(&path, str, sizeof(str));
2622
2623 mutex_lock(&clt_path->init_mutex);
2624 err = init_conns(clt_path);
2625 if (err) {
2626 rtrs_err(clt_path->clt,
2627 "init_conns() failed: err=%d path=%s [%s:%u]\n", err,
2628 str, clt_path->hca_name, clt_path->hca_port);
2629 goto out;
2630 }
2631 err = rtrs_send_path_info(clt_path);
2632 if (err) {
2633 rtrs_err(clt_path->clt,
2634 "rtrs_send_path_info() failed: err=%d path=%s [%s:%u]\n",
2635 err, str, clt_path->hca_name, clt_path->hca_port);
2636 goto out;
2637 }
2638 rtrs_clt_path_up(clt_path);
2639 out:
2640 mutex_unlock(&clt_path->init_mutex);
2641
2642 return err;
2643 }
2644
rtrs_clt_reconnect_work(struct work_struct * work)2645 static void rtrs_clt_reconnect_work(struct work_struct *work)
2646 {
2647 struct rtrs_clt_path *clt_path;
2648 struct rtrs_clt_sess *clt;
2649 int err;
2650
2651 clt_path = container_of(to_delayed_work(work), struct rtrs_clt_path,
2652 reconnect_dwork);
2653 clt = clt_path->clt;
2654
2655 if (READ_ONCE(clt_path->state) != RTRS_CLT_RECONNECTING)
2656 return;
2657
2658 if (clt_path->reconnect_attempts >= clt->max_reconnect_attempts) {
2659 /* Close a path completely if max attempts is reached */
2660 rtrs_clt_close_conns(clt_path, false);
2661 return;
2662 }
2663 clt_path->reconnect_attempts++;
2664
2665 msleep(RTRS_RECONNECT_BACKOFF);
2666 if (rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_CONNECTING, NULL)) {
2667 err = init_path(clt_path);
2668 if (err)
2669 goto reconnect_again;
2670 }
2671
2672 return;
2673
2674 reconnect_again:
2675 if (rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_RECONNECTING, NULL)) {
2676 clt_path->stats->reconnects.fail_cnt++;
2677 queue_work(rtrs_wq, &clt_path->err_recovery_work);
2678 }
2679 }
2680
rtrs_clt_dev_release(struct device * dev)2681 static void rtrs_clt_dev_release(struct device *dev)
2682 {
2683 struct rtrs_clt_sess *clt = container_of(dev, struct rtrs_clt_sess,
2684 dev);
2685
2686 mutex_destroy(&clt->paths_ev_mutex);
2687 mutex_destroy(&clt->paths_mutex);
2688 kfree(clt);
2689 }
2690
alloc_clt(const char * sessname,size_t paths_num,u16 port,size_t pdu_sz,void * priv,void (* link_ev)(void * priv,enum rtrs_clt_link_ev ev),unsigned int reconnect_delay_sec,unsigned int max_reconnect_attempts)2691 static struct rtrs_clt_sess *alloc_clt(const char *sessname, size_t paths_num,
2692 u16 port, size_t pdu_sz, void *priv,
2693 void (*link_ev)(void *priv,
2694 enum rtrs_clt_link_ev ev),
2695 unsigned int reconnect_delay_sec,
2696 unsigned int max_reconnect_attempts)
2697 {
2698 struct rtrs_clt_sess *clt;
2699 int err;
2700
2701 if (!paths_num || paths_num > MAX_PATHS_NUM)
2702 return ERR_PTR(-EINVAL);
2703
2704 if (strlen(sessname) >= sizeof(clt->sessname))
2705 return ERR_PTR(-EINVAL);
2706
2707 clt = kzalloc(sizeof(*clt), GFP_KERNEL);
2708 if (!clt)
2709 return ERR_PTR(-ENOMEM);
2710
2711 clt->pcpu_path = alloc_percpu(typeof(*clt->pcpu_path));
2712 if (!clt->pcpu_path) {
2713 kfree(clt);
2714 return ERR_PTR(-ENOMEM);
2715 }
2716
2717 clt->dev.class = rtrs_clt_dev_class;
2718 clt->dev.release = rtrs_clt_dev_release;
2719 uuid_gen(&clt->paths_uuid);
2720 INIT_LIST_HEAD_RCU(&clt->paths_list);
2721 clt->paths_num = paths_num;
2722 clt->paths_up = MAX_PATHS_NUM;
2723 clt->port = port;
2724 clt->pdu_sz = pdu_sz;
2725 clt->max_segments = RTRS_MAX_SEGMENTS;
2726 clt->reconnect_delay_sec = reconnect_delay_sec;
2727 clt->max_reconnect_attempts = max_reconnect_attempts;
2728 clt->priv = priv;
2729 clt->link_ev = link_ev;
2730 clt->mp_policy = MP_POLICY_MIN_INFLIGHT;
2731 strscpy(clt->sessname, sessname, sizeof(clt->sessname));
2732 init_waitqueue_head(&clt->permits_wait);
2733 mutex_init(&clt->paths_ev_mutex);
2734 mutex_init(&clt->paths_mutex);
2735 device_initialize(&clt->dev);
2736
2737 err = dev_set_name(&clt->dev, "%s", sessname);
2738 if (err)
2739 goto err_put;
2740
2741 /*
2742 * Suppress user space notification until
2743 * sysfs files are created
2744 */
2745 dev_set_uevent_suppress(&clt->dev, true);
2746 err = device_add(&clt->dev);
2747 if (err)
2748 goto err_put;
2749
2750 clt->kobj_paths = kobject_create_and_add("paths", &clt->dev.kobj);
2751 if (!clt->kobj_paths) {
2752 err = -ENOMEM;
2753 goto err_del;
2754 }
2755 err = rtrs_clt_create_sysfs_root_files(clt);
2756 if (err) {
2757 kobject_del(clt->kobj_paths);
2758 kobject_put(clt->kobj_paths);
2759 goto err_del;
2760 }
2761 dev_set_uevent_suppress(&clt->dev, false);
2762 kobject_uevent(&clt->dev.kobj, KOBJ_ADD);
2763
2764 return clt;
2765 err_del:
2766 device_del(&clt->dev);
2767 err_put:
2768 free_percpu(clt->pcpu_path);
2769 put_device(&clt->dev);
2770 return ERR_PTR(err);
2771 }
2772
free_clt(struct rtrs_clt_sess * clt)2773 static void free_clt(struct rtrs_clt_sess *clt)
2774 {
2775 free_percpu(clt->pcpu_path);
2776
2777 /*
2778 * release callback will free clt and destroy mutexes in last put
2779 */
2780 device_unregister(&clt->dev);
2781 }
2782
2783 /**
2784 * rtrs_clt_open() - Open a path to an RTRS server
2785 * @ops: holds the link event callback and the private pointer.
2786 * @pathname: name of the path to an RTRS server
2787 * @paths: Paths to be established defined by their src and dst addresses
2788 * @paths_num: Number of elements in the @paths array
2789 * @port: port to be used by the RTRS session
2790 * @pdu_sz: Size of extra payload which can be accessed after permit allocation.
2791 * @reconnect_delay_sec: time between reconnect tries
2792 * @max_reconnect_attempts: Number of times to reconnect on error before giving
2793 * up, 0 for * disabled, -1 for forever
2794 * @nr_poll_queues: number of polling mode connection using IB_POLL_DIRECT flag
2795 *
2796 * Starts session establishment with the rtrs_server. The function can block
2797 * up to ~2000ms before it returns.
2798 *
2799 * Return a valid pointer on success otherwise PTR_ERR.
2800 */
rtrs_clt_open(struct rtrs_clt_ops * ops,const char * pathname,const struct rtrs_addr * paths,size_t paths_num,u16 port,size_t pdu_sz,u8 reconnect_delay_sec,s16 max_reconnect_attempts,u32 nr_poll_queues)2801 struct rtrs_clt_sess *rtrs_clt_open(struct rtrs_clt_ops *ops,
2802 const char *pathname,
2803 const struct rtrs_addr *paths,
2804 size_t paths_num, u16 port,
2805 size_t pdu_sz, u8 reconnect_delay_sec,
2806 s16 max_reconnect_attempts, u32 nr_poll_queues)
2807 {
2808 struct rtrs_clt_path *clt_path, *tmp;
2809 struct rtrs_clt_sess *clt;
2810 int err, i;
2811
2812 if (strchr(pathname, '/') || strchr(pathname, '.')) {
2813 pr_err("pathname cannot contain / and .\n");
2814 err = -EINVAL;
2815 goto out;
2816 }
2817
2818 clt = alloc_clt(pathname, paths_num, port, pdu_sz, ops->priv,
2819 ops->link_ev,
2820 reconnect_delay_sec,
2821 max_reconnect_attempts);
2822 if (IS_ERR(clt)) {
2823 err = PTR_ERR(clt);
2824 goto out;
2825 }
2826 for (i = 0; i < paths_num; i++) {
2827 struct rtrs_clt_path *clt_path;
2828
2829 clt_path = alloc_path(clt, &paths[i], nr_cpu_ids,
2830 nr_poll_queues);
2831 if (IS_ERR(clt_path)) {
2832 err = PTR_ERR(clt_path);
2833 goto close_all_path;
2834 }
2835 if (!i)
2836 clt_path->for_new_clt = 1;
2837 list_add_tail_rcu(&clt_path->s.entry, &clt->paths_list);
2838
2839 err = init_path(clt_path);
2840 if (err) {
2841 list_del_rcu(&clt_path->s.entry);
2842 rtrs_clt_close_conns(clt_path, true);
2843 free_percpu(clt_path->stats->pcpu_stats);
2844 kfree(clt_path->stats);
2845 free_path(clt_path);
2846 goto close_all_path;
2847 }
2848
2849 err = rtrs_clt_create_path_files(clt_path);
2850 if (err) {
2851 list_del_rcu(&clt_path->s.entry);
2852 rtrs_clt_close_conns(clt_path, true);
2853 free_percpu(clt_path->stats->pcpu_stats);
2854 kfree(clt_path->stats);
2855 free_path(clt_path);
2856 goto close_all_path;
2857 }
2858 }
2859 err = alloc_permits(clt);
2860 if (err)
2861 goto close_all_path;
2862
2863 return clt;
2864
2865 close_all_path:
2866 list_for_each_entry_safe(clt_path, tmp, &clt->paths_list, s.entry) {
2867 rtrs_clt_destroy_path_files(clt_path, NULL);
2868 rtrs_clt_close_conns(clt_path, true);
2869 kobject_put(&clt_path->kobj);
2870 }
2871 rtrs_clt_destroy_sysfs_root(clt);
2872 free_clt(clt);
2873
2874 out:
2875 return ERR_PTR(err);
2876 }
2877 EXPORT_SYMBOL(rtrs_clt_open);
2878
2879 /**
2880 * rtrs_clt_close() - Close a path
2881 * @clt: Session handle. Session is freed upon return.
2882 */
rtrs_clt_close(struct rtrs_clt_sess * clt)2883 void rtrs_clt_close(struct rtrs_clt_sess *clt)
2884 {
2885 struct rtrs_clt_path *clt_path, *tmp;
2886
2887 /* Firstly forbid sysfs access */
2888 rtrs_clt_destroy_sysfs_root(clt);
2889
2890 /* Now it is safe to iterate over all paths without locks */
2891 list_for_each_entry_safe(clt_path, tmp, &clt->paths_list, s.entry) {
2892 rtrs_clt_close_conns(clt_path, true);
2893 rtrs_clt_destroy_path_files(clt_path, NULL);
2894 kobject_put(&clt_path->kobj);
2895 }
2896 free_permits(clt);
2897 free_clt(clt);
2898 }
2899 EXPORT_SYMBOL(rtrs_clt_close);
2900
rtrs_clt_reconnect_from_sysfs(struct rtrs_clt_path * clt_path)2901 int rtrs_clt_reconnect_from_sysfs(struct rtrs_clt_path *clt_path)
2902 {
2903 enum rtrs_clt_state old_state;
2904 int err = -EBUSY;
2905 bool changed;
2906
2907 changed = rtrs_clt_change_state_get_old(clt_path,
2908 RTRS_CLT_RECONNECTING,
2909 &old_state);
2910 if (changed) {
2911 clt_path->reconnect_attempts = 0;
2912 rtrs_clt_stop_and_destroy_conns(clt_path);
2913 queue_delayed_work(rtrs_wq, &clt_path->reconnect_dwork, 0);
2914 }
2915 if (changed || old_state == RTRS_CLT_RECONNECTING) {
2916 /*
2917 * flush_delayed_work() queues pending work for immediate
2918 * execution, so do the flush if we have queued something
2919 * right now or work is pending.
2920 */
2921 flush_delayed_work(&clt_path->reconnect_dwork);
2922 err = (READ_ONCE(clt_path->state) ==
2923 RTRS_CLT_CONNECTED ? 0 : -ENOTCONN);
2924 }
2925
2926 return err;
2927 }
2928
rtrs_clt_remove_path_from_sysfs(struct rtrs_clt_path * clt_path,const struct attribute * sysfs_self)2929 int rtrs_clt_remove_path_from_sysfs(struct rtrs_clt_path *clt_path,
2930 const struct attribute *sysfs_self)
2931 {
2932 enum rtrs_clt_state old_state;
2933 bool changed;
2934
2935 /*
2936 * Continue stopping path till state was changed to DEAD or
2937 * state was observed as DEAD:
2938 * 1. State was changed to DEAD - we were fast and nobody
2939 * invoked rtrs_clt_reconnect(), which can again start
2940 * reconnecting.
2941 * 2. State was observed as DEAD - we have someone in parallel
2942 * removing the path.
2943 */
2944 do {
2945 rtrs_clt_close_conns(clt_path, true);
2946 changed = rtrs_clt_change_state_get_old(clt_path,
2947 RTRS_CLT_DEAD,
2948 &old_state);
2949 } while (!changed && old_state != RTRS_CLT_DEAD);
2950
2951 if (changed) {
2952 rtrs_clt_remove_path_from_arr(clt_path);
2953 rtrs_clt_destroy_path_files(clt_path, sysfs_self);
2954 kobject_put(&clt_path->kobj);
2955 }
2956
2957 return 0;
2958 }
2959
rtrs_clt_set_max_reconnect_attempts(struct rtrs_clt_sess * clt,int value)2960 void rtrs_clt_set_max_reconnect_attempts(struct rtrs_clt_sess *clt, int value)
2961 {
2962 clt->max_reconnect_attempts = (unsigned int)value;
2963 }
2964
rtrs_clt_get_max_reconnect_attempts(const struct rtrs_clt_sess * clt)2965 int rtrs_clt_get_max_reconnect_attempts(const struct rtrs_clt_sess *clt)
2966 {
2967 return (int)clt->max_reconnect_attempts;
2968 }
2969
2970 /**
2971 * rtrs_clt_request() - Request data transfer to/from server via RDMA.
2972 *
2973 * @dir: READ/WRITE
2974 * @ops: callback function to be called as confirmation, and the pointer.
2975 * @clt: Session
2976 * @permit: Preallocated permit
2977 * @vec: Message that is sent to server together with the request.
2978 * Sum of len of all @vec elements limited to <= IO_MSG_SIZE.
2979 * Since the msg is copied internally it can be allocated on stack.
2980 * @nr: Number of elements in @vec.
2981 * @data_len: length of data sent to/from server
2982 * @sg: Pages to be sent/received to/from server.
2983 * @sg_cnt: Number of elements in the @sg
2984 *
2985 * Return:
2986 * 0: Success
2987 * <0: Error
2988 *
2989 * On dir=READ rtrs client will request a data transfer from Server to client.
2990 * The data that the server will respond with will be stored in @sg when
2991 * the user receives an %RTRS_CLT_RDMA_EV_RDMA_REQUEST_WRITE_COMPL event.
2992 * On dir=WRITE rtrs client will rdma write data in sg to server side.
2993 */
rtrs_clt_request(int dir,struct rtrs_clt_req_ops * ops,struct rtrs_clt_sess * clt,struct rtrs_permit * permit,const struct kvec * vec,size_t nr,size_t data_len,struct scatterlist * sg,unsigned int sg_cnt)2994 int rtrs_clt_request(int dir, struct rtrs_clt_req_ops *ops,
2995 struct rtrs_clt_sess *clt, struct rtrs_permit *permit,
2996 const struct kvec *vec, size_t nr, size_t data_len,
2997 struct scatterlist *sg, unsigned int sg_cnt)
2998 {
2999 struct rtrs_clt_io_req *req;
3000 struct rtrs_clt_path *clt_path;
3001
3002 enum dma_data_direction dma_dir;
3003 int err = -ECONNABORTED, i;
3004 size_t usr_len, hdr_len;
3005 struct path_it it;
3006
3007 /* Get kvec length */
3008 for (i = 0, usr_len = 0; i < nr; i++)
3009 usr_len += vec[i].iov_len;
3010
3011 if (dir == READ) {
3012 hdr_len = sizeof(struct rtrs_msg_rdma_read) +
3013 sg_cnt * sizeof(struct rtrs_sg_desc);
3014 dma_dir = DMA_FROM_DEVICE;
3015 } else {
3016 hdr_len = sizeof(struct rtrs_msg_rdma_write);
3017 dma_dir = DMA_TO_DEVICE;
3018 }
3019
3020 rcu_read_lock();
3021 for (path_it_init(&it, clt);
3022 (clt_path = it.next_path(&it)) && it.i < it.clt->paths_num; it.i++) {
3023 if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED)
3024 continue;
3025
3026 if (usr_len + hdr_len > clt_path->max_hdr_size) {
3027 rtrs_wrn_rl(clt_path->clt,
3028 "%s request failed, user message size is %zu and header length %zu, but max size is %u\n",
3029 dir == READ ? "Read" : "Write",
3030 usr_len, hdr_len, clt_path->max_hdr_size);
3031 err = -EMSGSIZE;
3032 break;
3033 }
3034 req = rtrs_clt_get_req(clt_path, ops->conf_fn, permit, ops->priv,
3035 vec, usr_len, sg, sg_cnt, data_len,
3036 dma_dir);
3037 if (dir == READ)
3038 err = rtrs_clt_read_req(req);
3039 else
3040 err = rtrs_clt_write_req(req);
3041 if (err) {
3042 req->in_use = false;
3043 continue;
3044 }
3045 /* Success path */
3046 break;
3047 }
3048 path_it_deinit(&it);
3049 rcu_read_unlock();
3050
3051 return err;
3052 }
3053 EXPORT_SYMBOL(rtrs_clt_request);
3054
rtrs_clt_rdma_cq_direct(struct rtrs_clt_sess * clt,unsigned int index)3055 int rtrs_clt_rdma_cq_direct(struct rtrs_clt_sess *clt, unsigned int index)
3056 {
3057 /* If no path, return -1 for block layer not to try again */
3058 int cnt = -1;
3059 struct rtrs_con *con;
3060 struct rtrs_clt_path *clt_path;
3061 struct path_it it;
3062
3063 rcu_read_lock();
3064 for (path_it_init(&it, clt);
3065 (clt_path = it.next_path(&it)) && it.i < it.clt->paths_num; it.i++) {
3066 if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED)
3067 continue;
3068
3069 con = clt_path->s.con[index + 1];
3070 cnt = ib_process_cq_direct(con->cq, -1);
3071 if (cnt)
3072 break;
3073 }
3074 path_it_deinit(&it);
3075 rcu_read_unlock();
3076
3077 return cnt;
3078 }
3079 EXPORT_SYMBOL(rtrs_clt_rdma_cq_direct);
3080
3081 /**
3082 * rtrs_clt_query() - queries RTRS session attributes
3083 *@clt: session pointer
3084 *@attr: query results for session attributes.
3085 * Returns:
3086 * 0 on success
3087 * -ECOMM no connection to the server
3088 */
rtrs_clt_query(struct rtrs_clt_sess * clt,struct rtrs_attrs * attr)3089 int rtrs_clt_query(struct rtrs_clt_sess *clt, struct rtrs_attrs *attr)
3090 {
3091 if (!rtrs_clt_is_connected(clt))
3092 return -ECOMM;
3093
3094 attr->queue_depth = clt->queue_depth;
3095 attr->max_segments = clt->max_segments;
3096 /* Cap max_io_size to min of remote buffer size and the fr pages */
3097 attr->max_io_size = min_t(int, clt->max_io_size,
3098 clt->max_segments * SZ_4K);
3099
3100 return 0;
3101 }
3102 EXPORT_SYMBOL(rtrs_clt_query);
3103
rtrs_clt_create_path_from_sysfs(struct rtrs_clt_sess * clt,struct rtrs_addr * addr)3104 int rtrs_clt_create_path_from_sysfs(struct rtrs_clt_sess *clt,
3105 struct rtrs_addr *addr)
3106 {
3107 struct rtrs_clt_path *clt_path;
3108 int err;
3109
3110 clt_path = alloc_path(clt, addr, nr_cpu_ids, 0);
3111 if (IS_ERR(clt_path))
3112 return PTR_ERR(clt_path);
3113
3114 mutex_lock(&clt->paths_mutex);
3115 if (clt->paths_num == 0) {
3116 /*
3117 * When all the paths are removed for a session,
3118 * the addition of the first path is like a new session for
3119 * the storage server
3120 */
3121 clt_path->for_new_clt = 1;
3122 }
3123
3124 mutex_unlock(&clt->paths_mutex);
3125
3126 /*
3127 * It is totally safe to add path in CONNECTING state: coming
3128 * IO will never grab it. Also it is very important to add
3129 * path before init, since init fires LINK_CONNECTED event.
3130 */
3131 rtrs_clt_add_path_to_arr(clt_path);
3132
3133 err = init_path(clt_path);
3134 if (err)
3135 goto close_path;
3136
3137 err = rtrs_clt_create_path_files(clt_path);
3138 if (err)
3139 goto close_path;
3140
3141 return 0;
3142
3143 close_path:
3144 rtrs_clt_remove_path_from_arr(clt_path);
3145 rtrs_clt_close_conns(clt_path, true);
3146 free_percpu(clt_path->stats->pcpu_stats);
3147 kfree(clt_path->stats);
3148 free_path(clt_path);
3149
3150 return err;
3151 }
3152
rtrs_clt_ib_dev_init(struct rtrs_ib_dev * dev)3153 static int rtrs_clt_ib_dev_init(struct rtrs_ib_dev *dev)
3154 {
3155 if (!(dev->ib_dev->attrs.device_cap_flags &
3156 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
3157 pr_err("Memory registrations not supported.\n");
3158 return -ENOTSUPP;
3159 }
3160
3161 return 0;
3162 }
3163
3164 static const struct rtrs_rdma_dev_pd_ops dev_pd_ops = {
3165 .init = rtrs_clt_ib_dev_init
3166 };
3167
rtrs_client_init(void)3168 static int __init rtrs_client_init(void)
3169 {
3170 rtrs_rdma_dev_pd_init(0, &dev_pd);
3171
3172 rtrs_clt_dev_class = class_create(THIS_MODULE, "rtrs-client");
3173 if (IS_ERR(rtrs_clt_dev_class)) {
3174 pr_err("Failed to create rtrs-client dev class\n");
3175 return PTR_ERR(rtrs_clt_dev_class);
3176 }
3177 rtrs_wq = alloc_workqueue("rtrs_client_wq", 0, 0);
3178 if (!rtrs_wq) {
3179 class_destroy(rtrs_clt_dev_class);
3180 return -ENOMEM;
3181 }
3182
3183 return 0;
3184 }
3185
rtrs_client_exit(void)3186 static void __exit rtrs_client_exit(void)
3187 {
3188 destroy_workqueue(rtrs_wq);
3189 class_destroy(rtrs_clt_dev_class);
3190 rtrs_rdma_dev_pd_deinit(&dev_pd);
3191 }
3192
3193 module_init(rtrs_client_init);
3194 module_exit(rtrs_client_exit);
3195