1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
11 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
13 #include <linux/module.h>
14 #include <linux/mempool.h>
18 #include <rdma/ib_cm.h>
19 #include <rdma/ib_verbs.h>
21 MODULE_DESCRIPTION("RDMA Transport Server");
22 MODULE_LICENSE("GPL");
24 /* Must be power of 2, see mask from mr->page_size in ib_sg_to_pages() */
25 #define DEFAULT_MAX_CHUNK_SIZE (128 << 10)
26 #define DEFAULT_SESS_QUEUE_DEPTH 512
27 #define MAX_HDR_SIZE PAGE_SIZE
29 /* We guarantee to serve 10 paths at least */
30 #define CHUNK_POOL_SZ 10
32 static struct rtrs_rdma_dev_pd dev_pd;
33 static mempool_t *chunk_pool;
34 struct class *rtrs_dev_class;
35 static struct rtrs_srv_ib_ctx ib_ctx;
37 static int __read_mostly max_chunk_size = DEFAULT_MAX_CHUNK_SIZE;
38 static int __read_mostly sess_queue_depth = DEFAULT_SESS_QUEUE_DEPTH;
40 static bool always_invalidate = true;
41 module_param(always_invalidate, bool, 0444);
42 MODULE_PARM_DESC(always_invalidate,
43 "Invalidate memory registration for contiguous memory regions before accessing.");
45 module_param_named(max_chunk_size, max_chunk_size, int, 0444);
46 MODULE_PARM_DESC(max_chunk_size,
47 "Max size for each IO request, when change the unit is in byte (default: "
48 __stringify(DEFAULT_MAX_CHUNK_SIZE) "KB)");
50 module_param_named(sess_queue_depth, sess_queue_depth, int, 0444);
51 MODULE_PARM_DESC(sess_queue_depth,
52 "Number of buffers for pending I/O requests to allocate per session. Maximum: "
53 __stringify(MAX_SESS_QUEUE_DEPTH) " (default: "
54 __stringify(DEFAULT_SESS_QUEUE_DEPTH) ")");
56 static cpumask_t cq_affinity_mask = { CPU_BITS_ALL };
58 static struct workqueue_struct *rtrs_wq;
60 static inline struct rtrs_srv_con *to_srv_con(struct rtrs_con *c)
62 return container_of(c, struct rtrs_srv_con, c);
65 static inline struct rtrs_srv_sess *to_srv_sess(struct rtrs_sess *s)
67 return container_of(s, struct rtrs_srv_sess, s);
70 static bool __rtrs_srv_change_state(struct rtrs_srv_sess *sess,
71 enum rtrs_srv_state new_state)
73 enum rtrs_srv_state old_state;
76 lockdep_assert_held(&sess->state_lock);
77 old_state = sess->state;
79 case RTRS_SRV_CONNECTED:
81 case RTRS_SRV_CONNECTING:
88 case RTRS_SRV_CLOSING:
90 case RTRS_SRV_CONNECTING:
91 case RTRS_SRV_CONNECTED:
100 case RTRS_SRV_CLOSING:
111 sess->state = new_state;
116 static bool rtrs_srv_change_state(struct rtrs_srv_sess *sess,
117 enum rtrs_srv_state new_state)
121 spin_lock_irq(&sess->state_lock);
122 changed = __rtrs_srv_change_state(sess, new_state);
123 spin_unlock_irq(&sess->state_lock);
128 static void free_id(struct rtrs_srv_op *id)
135 static void rtrs_srv_free_ops_ids(struct rtrs_srv_sess *sess)
137 struct rtrs_srv *srv = sess->srv;
140 WARN_ON(atomic_read(&sess->ids_inflight));
142 for (i = 0; i < srv->queue_depth; i++)
143 free_id(sess->ops_ids[i]);
144 kfree(sess->ops_ids);
145 sess->ops_ids = NULL;
149 static void rtrs_srv_rdma_done(struct ib_cq *cq, struct ib_wc *wc);
151 static struct ib_cqe io_comp_cqe = {
152 .done = rtrs_srv_rdma_done
155 static int rtrs_srv_alloc_ops_ids(struct rtrs_srv_sess *sess)
157 struct rtrs_srv *srv = sess->srv;
158 struct rtrs_srv_op *id;
161 sess->ops_ids = kcalloc(srv->queue_depth, sizeof(*sess->ops_ids),
166 for (i = 0; i < srv->queue_depth; ++i) {
167 id = kzalloc(sizeof(*id), GFP_KERNEL);
171 sess->ops_ids[i] = id;
173 init_waitqueue_head(&sess->ids_waitq);
174 atomic_set(&sess->ids_inflight, 0);
179 rtrs_srv_free_ops_ids(sess);
183 static inline void rtrs_srv_get_ops_ids(struct rtrs_srv_sess *sess)
185 atomic_inc(&sess->ids_inflight);
188 static inline void rtrs_srv_put_ops_ids(struct rtrs_srv_sess *sess)
190 if (atomic_dec_and_test(&sess->ids_inflight))
191 wake_up(&sess->ids_waitq);
194 static void rtrs_srv_wait_ops_ids(struct rtrs_srv_sess *sess)
196 wait_event(sess->ids_waitq, !atomic_read(&sess->ids_inflight));
200 static void rtrs_srv_reg_mr_done(struct ib_cq *cq, struct ib_wc *wc)
202 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
203 struct rtrs_sess *s = con->c.sess;
204 struct rtrs_srv_sess *sess = to_srv_sess(s);
206 if (unlikely(wc->status != IB_WC_SUCCESS)) {
207 rtrs_err(s, "REG MR failed: %s\n",
208 ib_wc_status_msg(wc->status));
214 static struct ib_cqe local_reg_cqe = {
215 .done = rtrs_srv_reg_mr_done
218 static int rdma_write_sg(struct rtrs_srv_op *id)
220 struct rtrs_sess *s = id->con->c.sess;
221 struct rtrs_srv_sess *sess = to_srv_sess(s);
222 dma_addr_t dma_addr = sess->dma_addr[id->msg_id];
223 struct rtrs_srv_mr *srv_mr;
224 struct rtrs_srv *srv = sess->srv;
225 struct ib_send_wr inv_wr;
226 struct ib_rdma_wr imm_wr;
227 struct ib_rdma_wr *wr = NULL;
228 enum ib_send_flags flags;
233 struct ib_reg_wr rwr;
234 struct ib_sge *plist;
237 sg_cnt = le16_to_cpu(id->rd_msg->sg_cnt);
238 need_inval = le16_to_cpu(id->rd_msg->flags) & RTRS_MSG_NEED_INVAL_F;
239 if (unlikely(sg_cnt != 1))
246 plist->addr = dma_addr + offset;
247 plist->length = le32_to_cpu(id->rd_msg->desc[0].len);
249 /* WR will fail with length error
252 if (unlikely(plist->length == 0)) {
253 rtrs_err(s, "Invalid RDMA-Write sg list length 0\n");
257 plist->lkey = sess->s.dev->ib_pd->local_dma_lkey;
258 offset += plist->length;
260 wr->wr.sg_list = plist;
262 wr->remote_addr = le64_to_cpu(id->rd_msg->desc[0].addr);
263 wr->rkey = le32_to_cpu(id->rd_msg->desc[0].key);
267 /* Only one key is actually used */
268 WARN_ON_ONCE(rkey != wr->rkey);
270 wr->wr.opcode = IB_WR_RDMA_WRITE;
271 wr->wr.wr_cqe = &io_comp_cqe;
272 wr->wr.ex.imm_data = 0;
273 wr->wr.send_flags = 0;
275 if (need_inval && always_invalidate) {
276 wr->wr.next = &rwr.wr;
277 rwr.wr.next = &inv_wr;
278 inv_wr.next = &imm_wr.wr;
279 } else if (always_invalidate) {
280 wr->wr.next = &rwr.wr;
281 rwr.wr.next = &imm_wr.wr;
282 } else if (need_inval) {
283 wr->wr.next = &inv_wr;
284 inv_wr.next = &imm_wr.wr;
286 wr->wr.next = &imm_wr.wr;
289 * From time to time we have to post signaled sends,
290 * or send queue will fill up and only QP reset can help.
292 flags = (atomic_inc_return(&id->con->wr_cnt) % srv->queue_depth) ?
293 0 : IB_SEND_SIGNALED;
296 inv_wr.sg_list = NULL;
298 inv_wr.opcode = IB_WR_SEND_WITH_INV;
299 inv_wr.wr_cqe = &io_comp_cqe;
300 inv_wr.send_flags = 0;
301 inv_wr.ex.invalidate_rkey = rkey;
304 imm_wr.wr.next = NULL;
305 if (always_invalidate) {
306 struct rtrs_msg_rkey_rsp *msg;
308 srv_mr = &sess->mrs[id->msg_id];
309 rwr.wr.opcode = IB_WR_REG_MR;
310 rwr.wr.wr_cqe = &local_reg_cqe;
313 rwr.wr.send_flags = 0;
314 rwr.key = srv_mr->mr->rkey;
315 rwr.access = (IB_ACCESS_LOCAL_WRITE |
316 IB_ACCESS_REMOTE_WRITE);
317 msg = srv_mr->iu->buf;
318 msg->buf_id = cpu_to_le16(id->msg_id);
319 msg->type = cpu_to_le16(RTRS_MSG_RKEY_RSP);
320 msg->rkey = cpu_to_le32(srv_mr->mr->rkey);
322 list.addr = srv_mr->iu->dma_addr;
323 list.length = sizeof(*msg);
324 list.lkey = sess->s.dev->ib_pd->local_dma_lkey;
325 imm_wr.wr.sg_list = &list;
326 imm_wr.wr.num_sge = 1;
327 imm_wr.wr.opcode = IB_WR_SEND_WITH_IMM;
328 ib_dma_sync_single_for_device(sess->s.dev->ib_dev,
329 srv_mr->iu->dma_addr,
330 srv_mr->iu->size, DMA_TO_DEVICE);
332 imm_wr.wr.sg_list = NULL;
333 imm_wr.wr.num_sge = 0;
334 imm_wr.wr.opcode = IB_WR_RDMA_WRITE_WITH_IMM;
336 imm_wr.wr.send_flags = flags;
337 imm_wr.wr.ex.imm_data = cpu_to_be32(rtrs_to_io_rsp_imm(id->msg_id,
340 imm_wr.wr.wr_cqe = &io_comp_cqe;
341 ib_dma_sync_single_for_device(sess->s.dev->ib_dev, dma_addr,
342 offset, DMA_BIDIRECTIONAL);
344 err = ib_post_send(id->con->c.qp, &id->tx_wr.wr, NULL);
347 "Posting RDMA-Write-Request to QP failed, err: %d\n",
354 * send_io_resp_imm() - respond to client with empty IMM on failed READ/WRITE
355 * requests or on successful WRITE request.
356 * @con: the connection to send back result
357 * @id: the id associated with the IO
358 * @errno: the error number of the IO.
360 * Return 0 on success, errno otherwise.
362 static int send_io_resp_imm(struct rtrs_srv_con *con, struct rtrs_srv_op *id,
365 struct rtrs_sess *s = con->c.sess;
366 struct rtrs_srv_sess *sess = to_srv_sess(s);
367 struct ib_send_wr inv_wr, *wr = NULL;
368 struct ib_rdma_wr imm_wr;
369 struct ib_reg_wr rwr;
370 struct rtrs_srv *srv = sess->srv;
371 struct rtrs_srv_mr *srv_mr;
372 bool need_inval = false;
373 enum ib_send_flags flags;
377 if (id->dir == READ) {
378 struct rtrs_msg_rdma_read *rd_msg = id->rd_msg;
381 need_inval = le16_to_cpu(rd_msg->flags) &
382 RTRS_MSG_NEED_INVAL_F;
383 sg_cnt = le16_to_cpu(rd_msg->sg_cnt);
386 if (likely(sg_cnt)) {
387 inv_wr.wr_cqe = &io_comp_cqe;
388 inv_wr.sg_list = NULL;
390 inv_wr.opcode = IB_WR_SEND_WITH_INV;
391 inv_wr.send_flags = 0;
392 /* Only one key is actually used */
393 inv_wr.ex.invalidate_rkey =
394 le32_to_cpu(rd_msg->desc[0].key);
402 if (need_inval && always_invalidate) {
404 inv_wr.next = &rwr.wr;
405 rwr.wr.next = &imm_wr.wr;
406 } else if (always_invalidate) {
408 rwr.wr.next = &imm_wr.wr;
409 } else if (need_inval) {
411 inv_wr.next = &imm_wr.wr;
416 * From time to time we have to post signalled sends,
417 * or send queue will fill up and only QP reset can help.
419 flags = (atomic_inc_return(&con->wr_cnt) % srv->queue_depth) ?
420 0 : IB_SEND_SIGNALED;
421 imm = rtrs_to_io_rsp_imm(id->msg_id, errno, need_inval);
422 imm_wr.wr.next = NULL;
423 if (always_invalidate) {
425 struct rtrs_msg_rkey_rsp *msg;
427 srv_mr = &sess->mrs[id->msg_id];
428 rwr.wr.next = &imm_wr.wr;
429 rwr.wr.opcode = IB_WR_REG_MR;
430 rwr.wr.wr_cqe = &local_reg_cqe;
432 rwr.wr.send_flags = 0;
434 rwr.key = srv_mr->mr->rkey;
435 rwr.access = (IB_ACCESS_LOCAL_WRITE |
436 IB_ACCESS_REMOTE_WRITE);
437 msg = srv_mr->iu->buf;
438 msg->buf_id = cpu_to_le16(id->msg_id);
439 msg->type = cpu_to_le16(RTRS_MSG_RKEY_RSP);
440 msg->rkey = cpu_to_le32(srv_mr->mr->rkey);
442 list.addr = srv_mr->iu->dma_addr;
443 list.length = sizeof(*msg);
444 list.lkey = sess->s.dev->ib_pd->local_dma_lkey;
445 imm_wr.wr.sg_list = &list;
446 imm_wr.wr.num_sge = 1;
447 imm_wr.wr.opcode = IB_WR_SEND_WITH_IMM;
448 ib_dma_sync_single_for_device(sess->s.dev->ib_dev,
449 srv_mr->iu->dma_addr,
450 srv_mr->iu->size, DMA_TO_DEVICE);
452 imm_wr.wr.sg_list = NULL;
453 imm_wr.wr.num_sge = 0;
454 imm_wr.wr.opcode = IB_WR_RDMA_WRITE_WITH_IMM;
456 imm_wr.wr.send_flags = flags;
457 imm_wr.wr.wr_cqe = &io_comp_cqe;
459 imm_wr.wr.ex.imm_data = cpu_to_be32(imm);
461 err = ib_post_send(id->con->c.qp, wr, NULL);
463 rtrs_err_rl(s, "Posting RDMA-Reply to QP failed, err: %d\n",
469 void close_sess(struct rtrs_srv_sess *sess)
471 if (rtrs_srv_change_state(sess, RTRS_SRV_CLOSING))
472 queue_work(rtrs_wq, &sess->close_work);
473 WARN_ON(sess->state != RTRS_SRV_CLOSING);
476 static inline const char *rtrs_srv_state_str(enum rtrs_srv_state state)
479 case RTRS_SRV_CONNECTING:
480 return "RTRS_SRV_CONNECTING";
481 case RTRS_SRV_CONNECTED:
482 return "RTRS_SRV_CONNECTED";
483 case RTRS_SRV_CLOSING:
484 return "RTRS_SRV_CLOSING";
485 case RTRS_SRV_CLOSED:
486 return "RTRS_SRV_CLOSED";
493 * rtrs_srv_resp_rdma() - Finish an RDMA request
495 * @id: Internal RTRS operation identifier
496 * @status: Response Code sent to the other side for this operation.
497 * 0 = success, <=0 error
500 * Finish a RDMA operation. A message is sent to the client and the
501 * corresponding memory areas will be released.
503 bool rtrs_srv_resp_rdma(struct rtrs_srv_op *id, int status)
505 struct rtrs_srv_sess *sess;
506 struct rtrs_srv_con *con;
515 sess = to_srv_sess(s);
519 if (unlikely(sess->state != RTRS_SRV_CONNECTED)) {
521 "Sending I/O response failed, session %s is disconnected, sess state %s\n",
522 kobject_name(&sess->kobj),
523 rtrs_srv_state_str(sess->state));
526 if (always_invalidate) {
527 struct rtrs_srv_mr *mr = &sess->mrs[id->msg_id];
529 ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey));
531 if (unlikely(atomic_sub_return(1,
532 &con->sq_wr_avail) < 0)) {
533 rtrs_err(s, "IB send queue full: sess=%s cid=%d\n",
534 kobject_name(&sess->kobj),
536 atomic_add(1, &con->sq_wr_avail);
537 spin_lock(&con->rsp_wr_wait_lock);
538 list_add_tail(&id->wait_list, &con->rsp_wr_wait_list);
539 spin_unlock(&con->rsp_wr_wait_lock);
543 if (status || id->dir == WRITE || !id->rd_msg->sg_cnt)
544 err = send_io_resp_imm(con, id, status);
546 err = rdma_write_sg(id);
549 rtrs_err_rl(s, "IO response failed: %d: sess=%s\n", err,
550 kobject_name(&sess->kobj));
554 rtrs_srv_put_ops_ids(sess);
557 EXPORT_SYMBOL(rtrs_srv_resp_rdma);
560 * rtrs_srv_set_sess_priv() - Set private pointer in rtrs_srv.
561 * @srv: Session pointer
562 * @priv: The private pointer that is associated with the session.
564 void rtrs_srv_set_sess_priv(struct rtrs_srv *srv, void *priv)
568 EXPORT_SYMBOL(rtrs_srv_set_sess_priv);
570 static void unmap_cont_bufs(struct rtrs_srv_sess *sess)
574 for (i = 0; i < sess->mrs_num; i++) {
575 struct rtrs_srv_mr *srv_mr;
577 srv_mr = &sess->mrs[i];
578 rtrs_iu_free(srv_mr->iu, sess->s.dev->ib_dev, 1);
579 ib_dereg_mr(srv_mr->mr);
580 ib_dma_unmap_sg(sess->s.dev->ib_dev, srv_mr->sgt.sgl,
581 srv_mr->sgt.nents, DMA_BIDIRECTIONAL);
582 sg_free_table(&srv_mr->sgt);
587 static int map_cont_bufs(struct rtrs_srv_sess *sess)
589 struct rtrs_srv *srv = sess->srv;
590 struct rtrs_sess *ss = &sess->s;
591 int i, mri, err, mrs_num;
592 unsigned int chunk_bits;
593 int chunks_per_mr = 1;
596 * Here we map queue_depth chunks to MR. Firstly we have to
597 * figure out how many chunks can we map per MR.
599 if (always_invalidate) {
601 * in order to do invalidate for each chunks of memory, we needs
602 * more memory regions.
604 mrs_num = srv->queue_depth;
607 sess->s.dev->ib_dev->attrs.max_fast_reg_page_list_len;
608 mrs_num = DIV_ROUND_UP(srv->queue_depth, chunks_per_mr);
609 chunks_per_mr = DIV_ROUND_UP(srv->queue_depth, mrs_num);
612 sess->mrs = kcalloc(mrs_num, sizeof(*sess->mrs), GFP_KERNEL);
616 sess->mrs_num = mrs_num;
618 for (mri = 0; mri < mrs_num; mri++) {
619 struct rtrs_srv_mr *srv_mr = &sess->mrs[mri];
620 struct sg_table *sgt = &srv_mr->sgt;
621 struct scatterlist *s;
625 chunks = chunks_per_mr * mri;
626 if (!always_invalidate)
627 chunks_per_mr = min_t(int, chunks_per_mr,
628 srv->queue_depth - chunks);
630 err = sg_alloc_table(sgt, chunks_per_mr, GFP_KERNEL);
634 for_each_sg(sgt->sgl, s, chunks_per_mr, i)
635 sg_set_page(s, srv->chunks[chunks + i],
638 nr = ib_dma_map_sg(sess->s.dev->ib_dev, sgt->sgl,
639 sgt->nents, DMA_BIDIRECTIONAL);
640 if (nr < sgt->nents) {
641 err = nr < 0 ? nr : -EINVAL;
644 mr = ib_alloc_mr(sess->s.dev->ib_pd, IB_MR_TYPE_MEM_REG,
650 nr = ib_map_mr_sg(mr, sgt->sgl, sgt->nents,
651 NULL, max_chunk_size);
652 if (nr < 0 || nr < sgt->nents) {
653 err = nr < 0 ? nr : -EINVAL;
657 if (always_invalidate) {
658 srv_mr->iu = rtrs_iu_alloc(1,
659 sizeof(struct rtrs_msg_rkey_rsp),
660 GFP_KERNEL, sess->s.dev->ib_dev,
661 DMA_TO_DEVICE, rtrs_srv_rdma_done);
664 rtrs_err(ss, "rtrs_iu_alloc(), err: %d\n", err);
668 /* Eventually dma addr for each chunk can be cached */
669 for_each_sg(sgt->sgl, s, sgt->orig_nents, i)
670 sess->dma_addr[chunks + i] = sg_dma_address(s);
672 ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
678 srv_mr = &sess->mrs[mri];
681 rtrs_iu_free(srv_mr->iu, sess->s.dev->ib_dev, 1);
685 ib_dma_unmap_sg(sess->s.dev->ib_dev, sgt->sgl,
686 sgt->nents, DMA_BIDIRECTIONAL);
695 chunk_bits = ilog2(srv->queue_depth - 1) + 1;
696 sess->mem_bits = (MAX_IMM_PAYL_BITS - chunk_bits);
701 static void rtrs_srv_hb_err_handler(struct rtrs_con *c)
703 close_sess(to_srv_sess(c->sess));
706 static void rtrs_srv_init_hb(struct rtrs_srv_sess *sess)
708 rtrs_init_hb(&sess->s, &io_comp_cqe,
711 rtrs_srv_hb_err_handler,
715 static void rtrs_srv_start_hb(struct rtrs_srv_sess *sess)
717 rtrs_start_hb(&sess->s);
720 static void rtrs_srv_stop_hb(struct rtrs_srv_sess *sess)
722 rtrs_stop_hb(&sess->s);
725 static void rtrs_srv_info_rsp_done(struct ib_cq *cq, struct ib_wc *wc)
727 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
728 struct rtrs_sess *s = con->c.sess;
729 struct rtrs_srv_sess *sess = to_srv_sess(s);
732 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
733 rtrs_iu_free(iu, sess->s.dev->ib_dev, 1);
735 if (unlikely(wc->status != IB_WC_SUCCESS)) {
736 rtrs_err(s, "Sess info response send failed: %s\n",
737 ib_wc_status_msg(wc->status));
741 WARN_ON(wc->opcode != IB_WC_SEND);
744 static void rtrs_srv_sess_up(struct rtrs_srv_sess *sess)
746 struct rtrs_srv *srv = sess->srv;
747 struct rtrs_srv_ctx *ctx = srv->ctx;
750 mutex_lock(&srv->paths_ev_mutex);
751 up = ++srv->paths_up;
753 ctx->ops.link_ev(srv, RTRS_SRV_LINK_EV_CONNECTED, NULL);
754 mutex_unlock(&srv->paths_ev_mutex);
756 /* Mark session as established */
757 sess->established = true;
760 static void rtrs_srv_sess_down(struct rtrs_srv_sess *sess)
762 struct rtrs_srv *srv = sess->srv;
763 struct rtrs_srv_ctx *ctx = srv->ctx;
765 if (!sess->established)
768 sess->established = false;
769 mutex_lock(&srv->paths_ev_mutex);
770 WARN_ON(!srv->paths_up);
771 if (--srv->paths_up == 0)
772 ctx->ops.link_ev(srv, RTRS_SRV_LINK_EV_DISCONNECTED, srv->priv);
773 mutex_unlock(&srv->paths_ev_mutex);
776 static int post_recv_sess(struct rtrs_srv_sess *sess);
778 static int process_info_req(struct rtrs_srv_con *con,
779 struct rtrs_msg_info_req *msg)
781 struct rtrs_sess *s = con->c.sess;
782 struct rtrs_srv_sess *sess = to_srv_sess(s);
783 struct ib_send_wr *reg_wr = NULL;
784 struct rtrs_msg_info_rsp *rsp;
785 struct rtrs_iu *tx_iu;
786 struct ib_reg_wr *rwr;
790 err = post_recv_sess(sess);
792 rtrs_err(s, "post_recv_sess(), err: %d\n", err);
795 rwr = kcalloc(sess->mrs_num, sizeof(*rwr), GFP_KERNEL);
798 strlcpy(sess->s.sessname, msg->sessname, sizeof(sess->s.sessname));
800 tx_sz = sizeof(*rsp);
801 tx_sz += sizeof(rsp->desc[0]) * sess->mrs_num;
802 tx_iu = rtrs_iu_alloc(1, tx_sz, GFP_KERNEL, sess->s.dev->ib_dev,
803 DMA_TO_DEVICE, rtrs_srv_info_rsp_done);
804 if (unlikely(!tx_iu)) {
810 rsp->type = cpu_to_le16(RTRS_MSG_INFO_RSP);
811 rsp->sg_cnt = cpu_to_le16(sess->mrs_num);
813 for (mri = 0; mri < sess->mrs_num; mri++) {
814 struct ib_mr *mr = sess->mrs[mri].mr;
816 rsp->desc[mri].addr = cpu_to_le64(mr->iova);
817 rsp->desc[mri].key = cpu_to_le32(mr->rkey);
818 rsp->desc[mri].len = cpu_to_le32(mr->length);
821 * Fill in reg MR request and chain them *backwards*
823 rwr[mri].wr.next = mri ? &rwr[mri - 1].wr : NULL;
824 rwr[mri].wr.opcode = IB_WR_REG_MR;
825 rwr[mri].wr.wr_cqe = &local_reg_cqe;
826 rwr[mri].wr.num_sge = 0;
827 rwr[mri].wr.send_flags = 0;
829 rwr[mri].key = mr->rkey;
830 rwr[mri].access = (IB_ACCESS_LOCAL_WRITE |
831 IB_ACCESS_REMOTE_WRITE);
832 reg_wr = &rwr[mri].wr;
835 err = rtrs_srv_create_sess_files(sess);
838 kobject_get(&sess->kobj);
839 get_device(&sess->srv->dev);
840 rtrs_srv_change_state(sess, RTRS_SRV_CONNECTED);
841 rtrs_srv_start_hb(sess);
844 * We do not account number of established connections at the current
845 * moment, we rely on the client, which should send info request when
846 * all connections are successfully established. Thus, simply notify
847 * listener with a proper event if we are the first path.
849 rtrs_srv_sess_up(sess);
851 ib_dma_sync_single_for_device(sess->s.dev->ib_dev, tx_iu->dma_addr,
852 tx_iu->size, DMA_TO_DEVICE);
854 /* Send info response */
855 err = rtrs_iu_post_send(&con->c, tx_iu, tx_sz, reg_wr);
857 rtrs_err(s, "rtrs_iu_post_send(), err: %d\n", err);
859 rtrs_iu_free(tx_iu, sess->s.dev->ib_dev, 1);
867 static void rtrs_srv_info_req_done(struct ib_cq *cq, struct ib_wc *wc)
869 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
870 struct rtrs_sess *s = con->c.sess;
871 struct rtrs_srv_sess *sess = to_srv_sess(s);
872 struct rtrs_msg_info_req *msg;
878 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
879 if (unlikely(wc->status != IB_WC_SUCCESS)) {
880 rtrs_err(s, "Sess info request receive failed: %s\n",
881 ib_wc_status_msg(wc->status));
884 WARN_ON(wc->opcode != IB_WC_RECV);
886 if (unlikely(wc->byte_len < sizeof(*msg))) {
887 rtrs_err(s, "Sess info request is malformed: size %d\n",
891 ib_dma_sync_single_for_cpu(sess->s.dev->ib_dev, iu->dma_addr,
892 iu->size, DMA_FROM_DEVICE);
894 if (unlikely(le16_to_cpu(msg->type) != RTRS_MSG_INFO_REQ)) {
895 rtrs_err(s, "Sess info request is malformed: type %d\n",
896 le16_to_cpu(msg->type));
899 err = process_info_req(con, msg);
904 rtrs_iu_free(iu, sess->s.dev->ib_dev, 1);
911 static int post_recv_info_req(struct rtrs_srv_con *con)
913 struct rtrs_sess *s = con->c.sess;
914 struct rtrs_srv_sess *sess = to_srv_sess(s);
915 struct rtrs_iu *rx_iu;
918 rx_iu = rtrs_iu_alloc(1, sizeof(struct rtrs_msg_info_req),
919 GFP_KERNEL, sess->s.dev->ib_dev,
920 DMA_FROM_DEVICE, rtrs_srv_info_req_done);
921 if (unlikely(!rx_iu))
923 /* Prepare for getting info response */
924 err = rtrs_iu_post_recv(&con->c, rx_iu);
926 rtrs_err(s, "rtrs_iu_post_recv(), err: %d\n", err);
927 rtrs_iu_free(rx_iu, sess->s.dev->ib_dev, 1);
934 static int post_recv_io(struct rtrs_srv_con *con, size_t q_size)
938 for (i = 0; i < q_size; i++) {
939 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe);
947 static int post_recv_sess(struct rtrs_srv_sess *sess)
949 struct rtrs_srv *srv = sess->srv;
950 struct rtrs_sess *s = &sess->s;
954 for (cid = 0; cid < sess->s.con_num; cid++) {
956 q_size = SERVICE_CON_QUEUE_DEPTH;
958 q_size = srv->queue_depth;
960 err = post_recv_io(to_srv_con(sess->s.con[cid]), q_size);
962 rtrs_err(s, "post_recv_io(), err: %d\n", err);
970 static void process_read(struct rtrs_srv_con *con,
971 struct rtrs_msg_rdma_read *msg,
974 struct rtrs_sess *s = con->c.sess;
975 struct rtrs_srv_sess *sess = to_srv_sess(s);
976 struct rtrs_srv *srv = sess->srv;
977 struct rtrs_srv_ctx *ctx = srv->ctx;
978 struct rtrs_srv_op *id;
980 size_t usr_len, data_len;
984 if (unlikely(sess->state != RTRS_SRV_CONNECTED)) {
986 "Processing read request failed, session is disconnected, sess state %s\n",
987 rtrs_srv_state_str(sess->state));
990 if (unlikely(msg->sg_cnt != 1 && msg->sg_cnt != 0)) {
992 "Processing read request failed, invalid message\n");
995 rtrs_srv_get_ops_ids(sess);
996 rtrs_srv_update_rdma_stats(sess->stats, off, READ);
997 id = sess->ops_ids[buf_id];
1000 id->msg_id = buf_id;
1002 usr_len = le16_to_cpu(msg->usr_len);
1003 data_len = off - usr_len;
1004 data = page_address(srv->chunks[buf_id]);
1005 ret = ctx->ops.rdma_ev(srv->priv, id, READ, data, data_len,
1006 data + data_len, usr_len);
1008 if (unlikely(ret)) {
1010 "Processing read request failed, user module cb reported for msg_id %d, err: %d\n",
1018 ret = send_io_resp_imm(con, id, ret);
1021 "Sending err msg for failed RDMA-Write-Req failed, msg_id %d, err: %d\n",
1025 rtrs_srv_put_ops_ids(sess);
1028 static void process_write(struct rtrs_srv_con *con,
1029 struct rtrs_msg_rdma_write *req,
1030 u32 buf_id, u32 off)
1032 struct rtrs_sess *s = con->c.sess;
1033 struct rtrs_srv_sess *sess = to_srv_sess(s);
1034 struct rtrs_srv *srv = sess->srv;
1035 struct rtrs_srv_ctx *ctx = srv->ctx;
1036 struct rtrs_srv_op *id;
1038 size_t data_len, usr_len;
1042 if (unlikely(sess->state != RTRS_SRV_CONNECTED)) {
1044 "Processing write request failed, session is disconnected, sess state %s\n",
1045 rtrs_srv_state_str(sess->state));
1048 rtrs_srv_get_ops_ids(sess);
1049 rtrs_srv_update_rdma_stats(sess->stats, off, WRITE);
1050 id = sess->ops_ids[buf_id];
1053 id->msg_id = buf_id;
1055 usr_len = le16_to_cpu(req->usr_len);
1056 data_len = off - usr_len;
1057 data = page_address(srv->chunks[buf_id]);
1058 ret = ctx->ops.rdma_ev(srv->priv, id, WRITE, data, data_len,
1059 data + data_len, usr_len);
1060 if (unlikely(ret)) {
1062 "Processing write request failed, user module callback reports err: %d\n",
1070 ret = send_io_resp_imm(con, id, ret);
1073 "Processing write request failed, sending I/O response failed, msg_id %d, err: %d\n",
1077 rtrs_srv_put_ops_ids(sess);
1080 static void process_io_req(struct rtrs_srv_con *con, void *msg,
1083 struct rtrs_sess *s = con->c.sess;
1084 struct rtrs_srv_sess *sess = to_srv_sess(s);
1085 struct rtrs_msg_rdma_hdr *hdr;
1088 ib_dma_sync_single_for_cpu(sess->s.dev->ib_dev, sess->dma_addr[id],
1089 max_chunk_size, DMA_BIDIRECTIONAL);
1091 type = le16_to_cpu(hdr->type);
1094 case RTRS_MSG_WRITE:
1095 process_write(con, msg, id, off);
1098 process_read(con, msg, id, off);
1102 "Processing I/O request failed, unknown message type received: 0x%02x\n",
1113 static void rtrs_srv_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1115 struct rtrs_srv_mr *mr =
1116 container_of(wc->wr_cqe, typeof(*mr), inv_cqe);
1117 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
1118 struct rtrs_sess *s = con->c.sess;
1119 struct rtrs_srv_sess *sess = to_srv_sess(s);
1120 struct rtrs_srv *srv = sess->srv;
1124 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1125 rtrs_err(s, "Failed IB_WR_LOCAL_INV: %s\n",
1126 ib_wc_status_msg(wc->status));
1129 msg_id = mr->msg_id;
1131 data = page_address(srv->chunks[msg_id]) + off;
1132 process_io_req(con, data, msg_id, off);
1135 static int rtrs_srv_inv_rkey(struct rtrs_srv_con *con,
1136 struct rtrs_srv_mr *mr)
1138 struct ib_send_wr wr = {
1139 .opcode = IB_WR_LOCAL_INV,
1140 .wr_cqe = &mr->inv_cqe,
1141 .send_flags = IB_SEND_SIGNALED,
1142 .ex.invalidate_rkey = mr->mr->rkey,
1144 mr->inv_cqe.done = rtrs_srv_inv_rkey_done;
1146 return ib_post_send(con->c.qp, &wr, NULL);
1149 static void rtrs_rdma_process_wr_wait_list(struct rtrs_srv_con *con)
1151 spin_lock(&con->rsp_wr_wait_lock);
1152 while (!list_empty(&con->rsp_wr_wait_list)) {
1153 struct rtrs_srv_op *id;
1156 id = list_entry(con->rsp_wr_wait_list.next,
1157 struct rtrs_srv_op, wait_list);
1158 list_del(&id->wait_list);
1160 spin_unlock(&con->rsp_wr_wait_lock);
1161 ret = rtrs_srv_resp_rdma(id, id->status);
1162 spin_lock(&con->rsp_wr_wait_lock);
1165 list_add(&id->wait_list, &con->rsp_wr_wait_list);
1169 spin_unlock(&con->rsp_wr_wait_lock);
1172 static void rtrs_srv_rdma_done(struct ib_cq *cq, struct ib_wc *wc)
1174 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
1175 struct rtrs_sess *s = con->c.sess;
1176 struct rtrs_srv_sess *sess = to_srv_sess(s);
1177 struct rtrs_srv *srv = sess->srv;
1178 u32 imm_type, imm_payload;
1181 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1182 if (wc->status != IB_WC_WR_FLUSH_ERR) {
1184 "%s (wr_cqe: %p, type: %d, vendor_err: 0x%x, len: %u)\n",
1185 ib_wc_status_msg(wc->status), wc->wr_cqe,
1186 wc->opcode, wc->vendor_err, wc->byte_len);
1192 switch (wc->opcode) {
1193 case IB_WC_RECV_RDMA_WITH_IMM:
1195 * post_recv() RDMA write completions of IO reqs (read/write)
1198 if (WARN_ON(wc->wr_cqe != &io_comp_cqe))
1200 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe);
1201 if (unlikely(err)) {
1202 rtrs_err(s, "rtrs_post_recv(), err: %d\n", err);
1206 rtrs_from_imm(be32_to_cpu(wc->ex.imm_data),
1207 &imm_type, &imm_payload);
1208 if (likely(imm_type == RTRS_IO_REQ_IMM)) {
1212 msg_id = imm_payload >> sess->mem_bits;
1213 off = imm_payload & ((1 << sess->mem_bits) - 1);
1214 if (unlikely(msg_id >= srv->queue_depth ||
1215 off >= max_chunk_size)) {
1216 rtrs_err(s, "Wrong msg_id %u, off %u\n",
1221 if (always_invalidate) {
1222 struct rtrs_srv_mr *mr = &sess->mrs[msg_id];
1225 mr->msg_id = msg_id;
1226 err = rtrs_srv_inv_rkey(con, mr);
1227 if (unlikely(err)) {
1228 rtrs_err(s, "rtrs_post_recv(), err: %d\n",
1234 data = page_address(srv->chunks[msg_id]) + off;
1235 process_io_req(con, data, msg_id, off);
1237 } else if (imm_type == RTRS_HB_MSG_IMM) {
1238 WARN_ON(con->c.cid);
1239 rtrs_send_hb_ack(&sess->s);
1240 } else if (imm_type == RTRS_HB_ACK_IMM) {
1241 WARN_ON(con->c.cid);
1242 sess->s.hb_missed_cnt = 0;
1244 rtrs_wrn(s, "Unknown IMM type %u\n", imm_type);
1247 case IB_WC_RDMA_WRITE:
1250 * post_send() RDMA write completions of IO reqs (read/write)
1252 atomic_add(srv->queue_depth, &con->sq_wr_avail);
1254 if (unlikely(!list_empty_careful(&con->rsp_wr_wait_list)))
1255 rtrs_rdma_process_wr_wait_list(con);
1259 rtrs_wrn(s, "Unexpected WC type: %d\n", wc->opcode);
1265 * rtrs_srv_get_sess_name() - Get rtrs_srv peer hostname.
1267 * @sessname: Sessname buffer
1268 * @len: Length of sessname buffer
1270 int rtrs_srv_get_sess_name(struct rtrs_srv *srv, char *sessname, size_t len)
1272 struct rtrs_srv_sess *sess;
1273 int err = -ENOTCONN;
1275 mutex_lock(&srv->paths_mutex);
1276 list_for_each_entry(sess, &srv->paths_list, s.entry) {
1277 if (sess->state != RTRS_SRV_CONNECTED)
1279 strlcpy(sessname, sess->s.sessname,
1280 min_t(size_t, sizeof(sess->s.sessname), len));
1284 mutex_unlock(&srv->paths_mutex);
1288 EXPORT_SYMBOL(rtrs_srv_get_sess_name);
1291 * rtrs_srv_get_sess_qdepth() - Get rtrs_srv qdepth.
1294 int rtrs_srv_get_queue_depth(struct rtrs_srv *srv)
1296 return srv->queue_depth;
1298 EXPORT_SYMBOL(rtrs_srv_get_queue_depth);
1300 static int find_next_bit_ring(struct rtrs_srv_sess *sess)
1302 struct ib_device *ib_dev = sess->s.dev->ib_dev;
1305 v = cpumask_next(sess->cur_cq_vector, &cq_affinity_mask);
1306 if (v >= nr_cpu_ids || v >= ib_dev->num_comp_vectors)
1307 v = cpumask_first(&cq_affinity_mask);
1311 static int rtrs_srv_get_next_cq_vector(struct rtrs_srv_sess *sess)
1313 sess->cur_cq_vector = find_next_bit_ring(sess);
1315 return sess->cur_cq_vector;
1318 static void rtrs_srv_dev_release(struct device *dev)
1320 struct rtrs_srv *srv = container_of(dev, struct rtrs_srv, dev);
1325 static void free_srv(struct rtrs_srv *srv)
1329 WARN_ON(refcount_read(&srv->refcount));
1330 for (i = 0; i < srv->queue_depth; i++)
1331 mempool_free(srv->chunks[i], chunk_pool);
1333 mutex_destroy(&srv->paths_mutex);
1334 mutex_destroy(&srv->paths_ev_mutex);
1335 /* last put to release the srv structure */
1336 put_device(&srv->dev);
1339 static struct rtrs_srv *get_or_create_srv(struct rtrs_srv_ctx *ctx,
1340 const uuid_t *paths_uuid,
1343 struct rtrs_srv *srv;
1346 mutex_lock(&ctx->srv_mutex);
1347 list_for_each_entry(srv, &ctx->srv_list, ctx_list) {
1348 if (uuid_equal(&srv->paths_uuid, paths_uuid) &&
1349 refcount_inc_not_zero(&srv->refcount)) {
1350 mutex_unlock(&ctx->srv_mutex);
1354 mutex_unlock(&ctx->srv_mutex);
1356 * If this request is not the first connection request from the
1357 * client for this session then fail and return error.
1360 return ERR_PTR(-ENXIO);
1362 /* need to allocate a new srv */
1363 srv = kzalloc(sizeof(*srv), GFP_KERNEL);
1365 return ERR_PTR(-ENOMEM);
1367 INIT_LIST_HEAD(&srv->paths_list);
1368 mutex_init(&srv->paths_mutex);
1369 mutex_init(&srv->paths_ev_mutex);
1370 uuid_copy(&srv->paths_uuid, paths_uuid);
1371 srv->queue_depth = sess_queue_depth;
1373 device_initialize(&srv->dev);
1374 srv->dev.release = rtrs_srv_dev_release;
1376 srv->chunks = kcalloc(srv->queue_depth, sizeof(*srv->chunks),
1381 for (i = 0; i < srv->queue_depth; i++) {
1382 srv->chunks[i] = mempool_alloc(chunk_pool, GFP_KERNEL);
1383 if (!srv->chunks[i])
1384 goto err_free_chunks;
1386 refcount_set(&srv->refcount, 1);
1387 mutex_lock(&ctx->srv_mutex);
1388 list_add(&srv->ctx_list, &ctx->srv_list);
1389 mutex_unlock(&ctx->srv_mutex);
1395 mempool_free(srv->chunks[i], chunk_pool);
1400 return ERR_PTR(-ENOMEM);
1403 static void put_srv(struct rtrs_srv *srv)
1405 if (refcount_dec_and_test(&srv->refcount)) {
1406 struct rtrs_srv_ctx *ctx = srv->ctx;
1408 WARN_ON(srv->dev.kobj.state_in_sysfs);
1410 mutex_lock(&ctx->srv_mutex);
1411 list_del(&srv->ctx_list);
1412 mutex_unlock(&ctx->srv_mutex);
1417 static void __add_path_to_srv(struct rtrs_srv *srv,
1418 struct rtrs_srv_sess *sess)
1420 list_add_tail(&sess->s.entry, &srv->paths_list);
1422 WARN_ON(srv->paths_num >= MAX_PATHS_NUM);
1425 static void del_path_from_srv(struct rtrs_srv_sess *sess)
1427 struct rtrs_srv *srv = sess->srv;
1432 mutex_lock(&srv->paths_mutex);
1433 list_del(&sess->s.entry);
1434 WARN_ON(!srv->paths_num);
1436 mutex_unlock(&srv->paths_mutex);
1439 /* return true if addresses are the same, error other wise */
1440 static int sockaddr_cmp(const struct sockaddr *a, const struct sockaddr *b)
1442 switch (a->sa_family) {
1444 return memcmp(&((struct sockaddr_ib *)a)->sib_addr,
1445 &((struct sockaddr_ib *)b)->sib_addr,
1446 sizeof(struct ib_addr)) &&
1447 (b->sa_family == AF_IB);
1449 return memcmp(&((struct sockaddr_in *)a)->sin_addr,
1450 &((struct sockaddr_in *)b)->sin_addr,
1451 sizeof(struct in_addr)) &&
1452 (b->sa_family == AF_INET);
1454 return memcmp(&((struct sockaddr_in6 *)a)->sin6_addr,
1455 &((struct sockaddr_in6 *)b)->sin6_addr,
1456 sizeof(struct in6_addr)) &&
1457 (b->sa_family == AF_INET6);
1463 static bool __is_path_w_addr_exists(struct rtrs_srv *srv,
1464 struct rdma_addr *addr)
1466 struct rtrs_srv_sess *sess;
1468 list_for_each_entry(sess, &srv->paths_list, s.entry)
1469 if (!sockaddr_cmp((struct sockaddr *)&sess->s.dst_addr,
1470 (struct sockaddr *)&addr->dst_addr) &&
1471 !sockaddr_cmp((struct sockaddr *)&sess->s.src_addr,
1472 (struct sockaddr *)&addr->src_addr))
1478 static void free_sess(struct rtrs_srv_sess *sess)
1480 if (sess->kobj.state_in_sysfs) {
1481 kobject_del(&sess->kobj);
1482 kobject_put(&sess->kobj);
1488 static void rtrs_srv_close_work(struct work_struct *work)
1490 struct rtrs_srv_sess *sess;
1491 struct rtrs_srv_con *con;
1494 sess = container_of(work, typeof(*sess), close_work);
1496 rtrs_srv_destroy_sess_files(sess);
1497 rtrs_srv_stop_hb(sess);
1499 for (i = 0; i < sess->s.con_num; i++) {
1500 if (!sess->s.con[i])
1502 con = to_srv_con(sess->s.con[i]);
1503 rdma_disconnect(con->c.cm_id);
1504 ib_drain_qp(con->c.qp);
1506 /* Wait for all inflights */
1507 rtrs_srv_wait_ops_ids(sess);
1509 /* Notify upper layer if we are the last path */
1510 rtrs_srv_sess_down(sess);
1512 unmap_cont_bufs(sess);
1513 rtrs_srv_free_ops_ids(sess);
1515 for (i = 0; i < sess->s.con_num; i++) {
1516 if (!sess->s.con[i])
1518 con = to_srv_con(sess->s.con[i]);
1519 rtrs_cq_qp_destroy(&con->c);
1520 rdma_destroy_id(con->c.cm_id);
1523 rtrs_ib_dev_put(sess->s.dev);
1525 del_path_from_srv(sess);
1528 rtrs_srv_change_state(sess, RTRS_SRV_CLOSED);
1530 kfree(sess->dma_addr);
1535 static int rtrs_rdma_do_accept(struct rtrs_srv_sess *sess,
1536 struct rdma_cm_id *cm_id)
1538 struct rtrs_srv *srv = sess->srv;
1539 struct rtrs_msg_conn_rsp msg;
1540 struct rdma_conn_param param;
1543 param = (struct rdma_conn_param) {
1544 .rnr_retry_count = 7,
1545 .private_data = &msg,
1546 .private_data_len = sizeof(msg),
1549 msg = (struct rtrs_msg_conn_rsp) {
1550 .magic = cpu_to_le16(RTRS_MAGIC),
1551 .version = cpu_to_le16(RTRS_PROTO_VER),
1552 .queue_depth = cpu_to_le16(srv->queue_depth),
1553 .max_io_size = cpu_to_le32(max_chunk_size - MAX_HDR_SIZE),
1554 .max_hdr_size = cpu_to_le32(MAX_HDR_SIZE),
1557 if (always_invalidate)
1558 msg.flags = cpu_to_le32(RTRS_MSG_NEW_RKEY_F);
1560 err = rdma_accept(cm_id, ¶m);
1562 pr_err("rdma_accept(), err: %d\n", err);
1567 static int rtrs_rdma_do_reject(struct rdma_cm_id *cm_id, int errno)
1569 struct rtrs_msg_conn_rsp msg;
1572 msg = (struct rtrs_msg_conn_rsp) {
1573 .magic = cpu_to_le16(RTRS_MAGIC),
1574 .version = cpu_to_le16(RTRS_PROTO_VER),
1575 .errno = cpu_to_le16(errno),
1578 err = rdma_reject(cm_id, &msg, sizeof(msg), IB_CM_REJ_CONSUMER_DEFINED);
1580 pr_err("rdma_reject(), err: %d\n", err);
1582 /* Bounce errno back */
1586 static struct rtrs_srv_sess *
1587 __find_sess(struct rtrs_srv *srv, const uuid_t *sess_uuid)
1589 struct rtrs_srv_sess *sess;
1591 list_for_each_entry(sess, &srv->paths_list, s.entry) {
1592 if (uuid_equal(&sess->s.uuid, sess_uuid))
1599 static int create_con(struct rtrs_srv_sess *sess,
1600 struct rdma_cm_id *cm_id,
1603 struct rtrs_srv *srv = sess->srv;
1604 struct rtrs_sess *s = &sess->s;
1605 struct rtrs_srv_con *con;
1607 u32 cq_size, wr_queue_size;
1610 con = kzalloc(sizeof(*con), GFP_KERNEL);
1616 spin_lock_init(&con->rsp_wr_wait_lock);
1617 INIT_LIST_HEAD(&con->rsp_wr_wait_list);
1618 con->c.cm_id = cm_id;
1619 con->c.sess = &sess->s;
1621 atomic_set(&con->wr_cnt, 1);
1623 if (con->c.cid == 0) {
1625 * All receive and all send (each requiring invalidate)
1626 * + 2 for drain and heartbeat
1628 wr_queue_size = SERVICE_CON_QUEUE_DEPTH * 3 + 2;
1629 cq_size = wr_queue_size;
1632 * If we have all receive requests posted and
1633 * all write requests posted and each read request
1634 * requires an invalidate request + drain
1635 * and qp gets into error state.
1637 cq_size = srv->queue_depth * 3 + 1;
1639 * In theory we might have queue_depth * 32
1640 * outstanding requests if an unsafe global key is used
1641 * and we have queue_depth read requests each consisting
1642 * of 32 different addresses. div 3 for mlx5.
1644 wr_queue_size = sess->s.dev->ib_dev->attrs.max_qp_wr / 3;
1646 atomic_set(&con->sq_wr_avail, wr_queue_size);
1647 cq_vector = rtrs_srv_get_next_cq_vector(sess);
1649 /* TODO: SOFTIRQ can be faster, but be careful with softirq context */
1650 err = rtrs_cq_qp_create(&sess->s, &con->c, 1, cq_vector, cq_size,
1651 wr_queue_size, wr_queue_size,
1654 rtrs_err(s, "rtrs_cq_qp_create(), err: %d\n", err);
1657 if (con->c.cid == 0) {
1658 err = post_recv_info_req(con);
1662 WARN_ON(sess->s.con[cid]);
1663 sess->s.con[cid] = &con->c;
1666 * Change context from server to current connection. The other
1667 * way is to use cm_id->qp->qp_context, which does not work on OFED.
1669 cm_id->context = &con->c;
1674 rtrs_cq_qp_destroy(&con->c);
1682 static struct rtrs_srv_sess *__alloc_sess(struct rtrs_srv *srv,
1683 struct rdma_cm_id *cm_id,
1684 unsigned int con_num,
1685 unsigned int recon_cnt,
1688 struct rtrs_srv_sess *sess;
1691 struct rtrs_addr path;
1693 if (srv->paths_num >= MAX_PATHS_NUM) {
1697 if (__is_path_w_addr_exists(srv, &cm_id->route.addr)) {
1699 pr_err("Path with same addr exists\n");
1702 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
1706 sess->stats = kzalloc(sizeof(*sess->stats), GFP_KERNEL);
1710 sess->stats->sess = sess;
1712 sess->dma_addr = kcalloc(srv->queue_depth, sizeof(*sess->dma_addr),
1714 if (!sess->dma_addr)
1715 goto err_free_stats;
1717 sess->s.con = kcalloc(con_num, sizeof(*sess->s.con), GFP_KERNEL);
1719 goto err_free_dma_addr;
1721 sess->state = RTRS_SRV_CONNECTING;
1723 sess->cur_cq_vector = -1;
1724 sess->s.dst_addr = cm_id->route.addr.dst_addr;
1725 sess->s.src_addr = cm_id->route.addr.src_addr;
1727 /* temporary until receiving session-name from client */
1728 path.src = &sess->s.src_addr;
1729 path.dst = &sess->s.dst_addr;
1730 rtrs_addr_to_str(&path, str, sizeof(str));
1731 strlcpy(sess->s.sessname, str, sizeof(sess->s.sessname));
1733 sess->s.con_num = con_num;
1734 sess->s.recon_cnt = recon_cnt;
1735 uuid_copy(&sess->s.uuid, uuid);
1736 spin_lock_init(&sess->state_lock);
1737 INIT_WORK(&sess->close_work, rtrs_srv_close_work);
1738 rtrs_srv_init_hb(sess);
1740 sess->s.dev = rtrs_ib_dev_find_or_add(cm_id->device, &dev_pd);
1745 err = map_cont_bufs(sess);
1749 err = rtrs_srv_alloc_ops_ids(sess);
1751 goto err_unmap_bufs;
1753 __add_path_to_srv(srv, sess);
1758 unmap_cont_bufs(sess);
1760 rtrs_ib_dev_put(sess->s.dev);
1764 kfree(sess->dma_addr);
1770 return ERR_PTR(err);
1773 static int rtrs_rdma_connect(struct rdma_cm_id *cm_id,
1774 const struct rtrs_msg_conn_req *msg,
1777 struct rtrs_srv_ctx *ctx = cm_id->context;
1778 struct rtrs_srv_sess *sess;
1779 struct rtrs_srv *srv;
1781 u16 version, con_num, cid;
1785 if (len < sizeof(*msg)) {
1786 pr_err("Invalid RTRS connection request\n");
1787 goto reject_w_econnreset;
1789 if (le16_to_cpu(msg->magic) != RTRS_MAGIC) {
1790 pr_err("Invalid RTRS magic\n");
1791 goto reject_w_econnreset;
1793 version = le16_to_cpu(msg->version);
1794 if (version >> 8 != RTRS_PROTO_VER_MAJOR) {
1795 pr_err("Unsupported major RTRS version: %d, expected %d\n",
1796 version >> 8, RTRS_PROTO_VER_MAJOR);
1797 goto reject_w_econnreset;
1799 con_num = le16_to_cpu(msg->cid_num);
1800 if (con_num > 4096) {
1802 pr_err("Too many connections requested: %d\n", con_num);
1803 goto reject_w_econnreset;
1805 cid = le16_to_cpu(msg->cid);
1806 if (cid >= con_num) {
1808 pr_err("Incorrect cid: %d >= %d\n", cid, con_num);
1809 goto reject_w_econnreset;
1811 recon_cnt = le16_to_cpu(msg->recon_cnt);
1812 srv = get_or_create_srv(ctx, &msg->paths_uuid, msg->first_conn);
1817 mutex_lock(&srv->paths_mutex);
1818 sess = __find_sess(srv, &msg->sess_uuid);
1820 struct rtrs_sess *s = &sess->s;
1822 /* Session already holds a reference */
1825 if (sess->state != RTRS_SRV_CONNECTING) {
1826 rtrs_err(s, "Session in wrong state: %s\n",
1827 rtrs_srv_state_str(sess->state));
1828 mutex_unlock(&srv->paths_mutex);
1829 goto reject_w_econnreset;
1834 if (con_num != s->con_num || cid >= s->con_num) {
1835 rtrs_err(s, "Incorrect request: %d, %d\n",
1837 mutex_unlock(&srv->paths_mutex);
1838 goto reject_w_econnreset;
1841 rtrs_err(s, "Connection already exists: %d\n",
1843 mutex_unlock(&srv->paths_mutex);
1844 goto reject_w_econnreset;
1847 sess = __alloc_sess(srv, cm_id, con_num, recon_cnt,
1850 mutex_unlock(&srv->paths_mutex);
1852 err = PTR_ERR(sess);
1856 err = create_con(sess, cm_id, cid);
1858 (void)rtrs_rdma_do_reject(cm_id, err);
1860 * Since session has other connections we follow normal way
1861 * through workqueue, but still return an error to tell cma.c
1862 * to call rdma_destroy_id() for current connection.
1864 goto close_and_return_err;
1866 err = rtrs_rdma_do_accept(sess, cm_id);
1868 (void)rtrs_rdma_do_reject(cm_id, err);
1870 * Since current connection was successfully added to the
1871 * session we follow normal way through workqueue to close the
1872 * session, thus return 0 to tell cma.c we call
1873 * rdma_destroy_id() ourselves.
1876 goto close_and_return_err;
1878 mutex_unlock(&srv->paths_mutex);
1883 return rtrs_rdma_do_reject(cm_id, err);
1885 reject_w_econnreset:
1886 return rtrs_rdma_do_reject(cm_id, -ECONNRESET);
1888 close_and_return_err:
1889 mutex_unlock(&srv->paths_mutex);
1895 static int rtrs_srv_rdma_cm_handler(struct rdma_cm_id *cm_id,
1896 struct rdma_cm_event *ev)
1898 struct rtrs_srv_sess *sess = NULL;
1899 struct rtrs_sess *s = NULL;
1901 if (ev->event != RDMA_CM_EVENT_CONNECT_REQUEST) {
1902 struct rtrs_con *c = cm_id->context;
1905 sess = to_srv_sess(s);
1908 switch (ev->event) {
1909 case RDMA_CM_EVENT_CONNECT_REQUEST:
1911 * In case of error cma.c will destroy cm_id,
1912 * see cma_process_remove()
1914 return rtrs_rdma_connect(cm_id, ev->param.conn.private_data,
1915 ev->param.conn.private_data_len);
1916 case RDMA_CM_EVENT_ESTABLISHED:
1919 case RDMA_CM_EVENT_REJECTED:
1920 case RDMA_CM_EVENT_CONNECT_ERROR:
1921 case RDMA_CM_EVENT_UNREACHABLE:
1922 rtrs_err(s, "CM error (CM event: %s, err: %d)\n",
1923 rdma_event_msg(ev->event), ev->status);
1925 case RDMA_CM_EVENT_DISCONNECTED:
1926 case RDMA_CM_EVENT_ADDR_CHANGE:
1927 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1928 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1932 pr_err("Ignoring unexpected CM event %s, err %d\n",
1933 rdma_event_msg(ev->event), ev->status);
1940 static struct rdma_cm_id *rtrs_srv_cm_init(struct rtrs_srv_ctx *ctx,
1941 struct sockaddr *addr,
1942 enum rdma_ucm_port_space ps)
1944 struct rdma_cm_id *cm_id;
1947 cm_id = rdma_create_id(&init_net, rtrs_srv_rdma_cm_handler,
1948 ctx, ps, IB_QPT_RC);
1949 if (IS_ERR(cm_id)) {
1950 ret = PTR_ERR(cm_id);
1951 pr_err("Creating id for RDMA connection failed, err: %d\n",
1955 ret = rdma_bind_addr(cm_id, addr);
1957 pr_err("Binding RDMA address failed, err: %d\n", ret);
1960 ret = rdma_listen(cm_id, 64);
1962 pr_err("Listening on RDMA connection failed, err: %d\n",
1970 rdma_destroy_id(cm_id);
1973 return ERR_PTR(ret);
1976 static int rtrs_srv_rdma_init(struct rtrs_srv_ctx *ctx, u16 port)
1978 struct sockaddr_in6 sin = {
1979 .sin6_family = AF_INET6,
1980 .sin6_addr = IN6ADDR_ANY_INIT,
1981 .sin6_port = htons(port),
1983 struct sockaddr_ib sib = {
1984 .sib_family = AF_IB,
1985 .sib_sid = cpu_to_be64(RDMA_IB_IP_PS_IB | port),
1986 .sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL),
1987 .sib_pkey = cpu_to_be16(0xffff),
1989 struct rdma_cm_id *cm_ip, *cm_ib;
1993 * We accept both IPoIB and IB connections, so we need to keep
1994 * two cm id's, one for each socket type and port space.
1995 * If the cm initialization of one of the id's fails, we abort
1998 cm_ip = rtrs_srv_cm_init(ctx, (struct sockaddr *)&sin, RDMA_PS_TCP);
2000 return PTR_ERR(cm_ip);
2002 cm_ib = rtrs_srv_cm_init(ctx, (struct sockaddr *)&sib, RDMA_PS_IB);
2003 if (IS_ERR(cm_ib)) {
2004 ret = PTR_ERR(cm_ib);
2008 ctx->cm_id_ip = cm_ip;
2009 ctx->cm_id_ib = cm_ib;
2014 rdma_destroy_id(cm_ip);
2019 static struct rtrs_srv_ctx *alloc_srv_ctx(struct rtrs_srv_ops *ops)
2021 struct rtrs_srv_ctx *ctx;
2023 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2028 mutex_init(&ctx->srv_mutex);
2029 INIT_LIST_HEAD(&ctx->srv_list);
2034 static void free_srv_ctx(struct rtrs_srv_ctx *ctx)
2036 WARN_ON(!list_empty(&ctx->srv_list));
2037 mutex_destroy(&ctx->srv_mutex);
2041 static int rtrs_srv_add_one(struct ib_device *device)
2043 struct rtrs_srv_ctx *ctx;
2046 mutex_lock(&ib_ctx.ib_dev_mutex);
2047 if (ib_ctx.ib_dev_count)
2051 * Since our CM IDs are NOT bound to any ib device we will create them
2054 ctx = ib_ctx.srv_ctx;
2055 ret = rtrs_srv_rdma_init(ctx, ib_ctx.port);
2058 * We errored out here.
2059 * According to the ib code, if we encounter an error here then the
2060 * error code is ignored, and no more calls to our ops are made.
2062 pr_err("Failed to initialize RDMA connection");
2068 * Keep a track on the number of ib devices added
2070 ib_ctx.ib_dev_count++;
2073 mutex_unlock(&ib_ctx.ib_dev_mutex);
2077 static void rtrs_srv_remove_one(struct ib_device *device, void *client_data)
2079 struct rtrs_srv_ctx *ctx;
2081 mutex_lock(&ib_ctx.ib_dev_mutex);
2082 ib_ctx.ib_dev_count--;
2084 if (ib_ctx.ib_dev_count)
2088 * Since our CM IDs are NOT bound to any ib device we will remove them
2089 * only once, when the last device is removed
2091 ctx = ib_ctx.srv_ctx;
2092 rdma_destroy_id(ctx->cm_id_ip);
2093 rdma_destroy_id(ctx->cm_id_ib);
2096 mutex_unlock(&ib_ctx.ib_dev_mutex);
2099 static struct ib_client rtrs_srv_client = {
2100 .name = "rtrs_server",
2101 .add = rtrs_srv_add_one,
2102 .remove = rtrs_srv_remove_one
2106 * rtrs_srv_open() - open RTRS server context
2107 * @ops: callback functions
2108 * @port: port to listen on
2110 * Creates server context with specified callbacks.
2112 * Return a valid pointer on success otherwise PTR_ERR.
2114 struct rtrs_srv_ctx *rtrs_srv_open(struct rtrs_srv_ops *ops, u16 port)
2116 struct rtrs_srv_ctx *ctx;
2119 ctx = alloc_srv_ctx(ops);
2121 return ERR_PTR(-ENOMEM);
2123 mutex_init(&ib_ctx.ib_dev_mutex);
2124 ib_ctx.srv_ctx = ctx;
2127 err = ib_register_client(&rtrs_srv_client);
2130 return ERR_PTR(err);
2135 EXPORT_SYMBOL(rtrs_srv_open);
2137 static void close_sessions(struct rtrs_srv *srv)
2139 struct rtrs_srv_sess *sess;
2141 mutex_lock(&srv->paths_mutex);
2142 list_for_each_entry(sess, &srv->paths_list, s.entry)
2144 mutex_unlock(&srv->paths_mutex);
2147 static void close_ctx(struct rtrs_srv_ctx *ctx)
2149 struct rtrs_srv *srv;
2151 mutex_lock(&ctx->srv_mutex);
2152 list_for_each_entry(srv, &ctx->srv_list, ctx_list)
2153 close_sessions(srv);
2154 mutex_unlock(&ctx->srv_mutex);
2155 flush_workqueue(rtrs_wq);
2159 * rtrs_srv_close() - close RTRS server context
2160 * @ctx: pointer to server context
2162 * Closes RTRS server context with all client sessions.
2164 void rtrs_srv_close(struct rtrs_srv_ctx *ctx)
2166 ib_unregister_client(&rtrs_srv_client);
2167 mutex_destroy(&ib_ctx.ib_dev_mutex);
2171 EXPORT_SYMBOL(rtrs_srv_close);
2173 static int check_module_params(void)
2175 if (sess_queue_depth < 1 || sess_queue_depth > MAX_SESS_QUEUE_DEPTH) {
2176 pr_err("Invalid sess_queue_depth value %d, has to be >= %d, <= %d.\n",
2177 sess_queue_depth, 1, MAX_SESS_QUEUE_DEPTH);
2180 if (max_chunk_size < 4096 || !is_power_of_2(max_chunk_size)) {
2181 pr_err("Invalid max_chunk_size value %d, has to be >= %d and should be power of two.\n",
2182 max_chunk_size, 4096);
2187 * Check if IB immediate data size is enough to hold the mem_id and the
2188 * offset inside the memory chunk
2190 if ((ilog2(sess_queue_depth - 1) + 1) +
2191 (ilog2(max_chunk_size - 1) + 1) > MAX_IMM_PAYL_BITS) {
2192 pr_err("RDMA immediate size (%db) not enough to encode %d buffers of size %dB. Reduce 'sess_queue_depth' or 'max_chunk_size' parameters.\n",
2193 MAX_IMM_PAYL_BITS, sess_queue_depth, max_chunk_size);
2200 static int __init rtrs_server_init(void)
2204 pr_info("Loading module %s, proto %s: (max_chunk_size: %d (pure IO %ld, headers %ld) , sess_queue_depth: %d, always_invalidate: %d)\n",
2205 KBUILD_MODNAME, RTRS_PROTO_VER_STRING,
2206 max_chunk_size, max_chunk_size - MAX_HDR_SIZE, MAX_HDR_SIZE,
2207 sess_queue_depth, always_invalidate);
2209 rtrs_rdma_dev_pd_init(0, &dev_pd);
2211 err = check_module_params();
2213 pr_err("Failed to load module, invalid module parameters, err: %d\n",
2217 chunk_pool = mempool_create_page_pool(sess_queue_depth * CHUNK_POOL_SZ,
2218 get_order(max_chunk_size));
2221 rtrs_dev_class = class_create(THIS_MODULE, "rtrs-server");
2222 if (IS_ERR(rtrs_dev_class)) {
2223 err = PTR_ERR(rtrs_dev_class);
2224 goto out_chunk_pool;
2226 rtrs_wq = alloc_workqueue("rtrs_server_wq", 0, 0);
2235 class_destroy(rtrs_dev_class);
2237 mempool_destroy(chunk_pool);
2242 static void __exit rtrs_server_exit(void)
2244 destroy_workqueue(rtrs_wq);
2245 class_destroy(rtrs_dev_class);
2246 mempool_destroy(chunk_pool);
2247 rtrs_rdma_dev_pd_deinit(&dev_pd);
2250 module_init(rtrs_server_init);
2251 module_exit(rtrs_server_exit);