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 = cq->cq_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, imm_wr;
226 struct ib_rdma_wr *wr = NULL;
227 enum ib_send_flags flags;
232 struct ib_reg_wr rwr;
233 struct ib_sge *plist;
236 sg_cnt = le16_to_cpu(id->rd_msg->sg_cnt);
237 need_inval = le16_to_cpu(id->rd_msg->flags) & RTRS_MSG_NEED_INVAL_F;
238 if (unlikely(sg_cnt != 1))
245 plist->addr = dma_addr + offset;
246 plist->length = le32_to_cpu(id->rd_msg->desc[0].len);
248 /* WR will fail with length error
251 if (unlikely(plist->length == 0)) {
252 rtrs_err(s, "Invalid RDMA-Write sg list length 0\n");
256 plist->lkey = sess->s.dev->ib_pd->local_dma_lkey;
257 offset += plist->length;
259 wr->wr.sg_list = plist;
261 wr->remote_addr = le64_to_cpu(id->rd_msg->desc[0].addr);
262 wr->rkey = le32_to_cpu(id->rd_msg->desc[0].key);
266 /* Only one key is actually used */
267 WARN_ON_ONCE(rkey != wr->rkey);
269 wr->wr.opcode = IB_WR_RDMA_WRITE;
270 wr->wr.wr_cqe = &io_comp_cqe;
271 wr->wr.ex.imm_data = 0;
272 wr->wr.send_flags = 0;
274 if (need_inval && always_invalidate) {
275 wr->wr.next = &rwr.wr;
276 rwr.wr.next = &inv_wr;
277 inv_wr.next = &imm_wr;
278 } else if (always_invalidate) {
279 wr->wr.next = &rwr.wr;
280 rwr.wr.next = &imm_wr;
281 } else if (need_inval) {
282 wr->wr.next = &inv_wr;
283 inv_wr.next = &imm_wr;
285 wr->wr.next = &imm_wr;
288 * From time to time we have to post signaled sends,
289 * or send queue will fill up and only QP reset can help.
291 flags = (atomic_inc_return(&id->con->wr_cnt) % srv->queue_depth) ?
292 0 : IB_SEND_SIGNALED;
295 inv_wr.sg_list = NULL;
297 inv_wr.opcode = IB_WR_SEND_WITH_INV;
298 inv_wr.wr_cqe = &io_comp_cqe;
299 inv_wr.send_flags = 0;
300 inv_wr.ex.invalidate_rkey = rkey;
304 if (always_invalidate) {
305 struct rtrs_msg_rkey_rsp *msg;
307 srv_mr = &sess->mrs[id->msg_id];
308 rwr.wr.opcode = IB_WR_REG_MR;
309 rwr.wr.wr_cqe = &local_reg_cqe;
312 rwr.wr.send_flags = 0;
313 rwr.key = srv_mr->mr->rkey;
314 rwr.access = (IB_ACCESS_LOCAL_WRITE |
315 IB_ACCESS_REMOTE_WRITE);
316 msg = srv_mr->iu->buf;
317 msg->buf_id = cpu_to_le16(id->msg_id);
318 msg->type = cpu_to_le16(RTRS_MSG_RKEY_RSP);
319 msg->rkey = cpu_to_le32(srv_mr->mr->rkey);
321 list.addr = srv_mr->iu->dma_addr;
322 list.length = sizeof(*msg);
323 list.lkey = sess->s.dev->ib_pd->local_dma_lkey;
324 imm_wr.sg_list = &list;
326 imm_wr.opcode = IB_WR_SEND_WITH_IMM;
327 ib_dma_sync_single_for_device(sess->s.dev->ib_dev,
328 srv_mr->iu->dma_addr,
329 srv_mr->iu->size, DMA_TO_DEVICE);
331 imm_wr.sg_list = NULL;
333 imm_wr.opcode = IB_WR_RDMA_WRITE_WITH_IMM;
335 imm_wr.send_flags = flags;
336 imm_wr.ex.imm_data = cpu_to_be32(rtrs_to_io_rsp_imm(id->msg_id,
339 imm_wr.wr_cqe = &io_comp_cqe;
340 ib_dma_sync_single_for_device(sess->s.dev->ib_dev, dma_addr,
341 offset, DMA_BIDIRECTIONAL);
343 err = ib_post_send(id->con->c.qp, &id->tx_wr.wr, NULL);
346 "Posting RDMA-Write-Request to QP failed, err: %d\n",
353 * send_io_resp_imm() - respond to client with empty IMM on failed READ/WRITE
354 * requests or on successful WRITE request.
355 * @con: the connection to send back result
356 * @id: the id associated with the IO
357 * @errno: the error number of the IO.
359 * Return 0 on success, errno otherwise.
361 static int send_io_resp_imm(struct rtrs_srv_con *con, struct rtrs_srv_op *id,
364 struct rtrs_sess *s = con->c.sess;
365 struct rtrs_srv_sess *sess = to_srv_sess(s);
366 struct ib_send_wr inv_wr, imm_wr, *wr = NULL;
367 struct ib_reg_wr rwr;
368 struct rtrs_srv *srv = sess->srv;
369 struct rtrs_srv_mr *srv_mr;
370 bool need_inval = false;
371 enum ib_send_flags flags;
375 if (id->dir == READ) {
376 struct rtrs_msg_rdma_read *rd_msg = id->rd_msg;
379 need_inval = le16_to_cpu(rd_msg->flags) &
380 RTRS_MSG_NEED_INVAL_F;
381 sg_cnt = le16_to_cpu(rd_msg->sg_cnt);
384 if (likely(sg_cnt)) {
385 inv_wr.wr_cqe = &io_comp_cqe;
386 inv_wr.sg_list = NULL;
388 inv_wr.opcode = IB_WR_SEND_WITH_INV;
389 inv_wr.send_flags = 0;
390 /* Only one key is actually used */
391 inv_wr.ex.invalidate_rkey =
392 le32_to_cpu(rd_msg->desc[0].key);
400 if (need_inval && always_invalidate) {
402 inv_wr.next = &rwr.wr;
403 rwr.wr.next = &imm_wr;
404 } else if (always_invalidate) {
406 rwr.wr.next = &imm_wr;
407 } else if (need_inval) {
409 inv_wr.next = &imm_wr;
414 * From time to time we have to post signalled sends,
415 * or send queue will fill up and only QP reset can help.
417 flags = (atomic_inc_return(&con->wr_cnt) % srv->queue_depth) ?
418 0 : IB_SEND_SIGNALED;
419 imm = rtrs_to_io_rsp_imm(id->msg_id, errno, need_inval);
421 if (always_invalidate) {
423 struct rtrs_msg_rkey_rsp *msg;
425 srv_mr = &sess->mrs[id->msg_id];
426 rwr.wr.next = &imm_wr;
427 rwr.wr.opcode = IB_WR_REG_MR;
428 rwr.wr.wr_cqe = &local_reg_cqe;
430 rwr.wr.send_flags = 0;
432 rwr.key = srv_mr->mr->rkey;
433 rwr.access = (IB_ACCESS_LOCAL_WRITE |
434 IB_ACCESS_REMOTE_WRITE);
435 msg = srv_mr->iu->buf;
436 msg->buf_id = cpu_to_le16(id->msg_id);
437 msg->type = cpu_to_le16(RTRS_MSG_RKEY_RSP);
438 msg->rkey = cpu_to_le32(srv_mr->mr->rkey);
440 list.addr = srv_mr->iu->dma_addr;
441 list.length = sizeof(*msg);
442 list.lkey = sess->s.dev->ib_pd->local_dma_lkey;
443 imm_wr.sg_list = &list;
445 imm_wr.opcode = IB_WR_SEND_WITH_IMM;
446 ib_dma_sync_single_for_device(sess->s.dev->ib_dev,
447 srv_mr->iu->dma_addr,
448 srv_mr->iu->size, DMA_TO_DEVICE);
450 imm_wr.sg_list = NULL;
452 imm_wr.opcode = IB_WR_RDMA_WRITE_WITH_IMM;
454 imm_wr.send_flags = flags;
455 imm_wr.wr_cqe = &io_comp_cqe;
457 imm_wr.ex.imm_data = cpu_to_be32(imm);
459 err = ib_post_send(id->con->c.qp, wr, NULL);
461 rtrs_err_rl(s, "Posting RDMA-Reply to QP failed, err: %d\n",
467 void close_sess(struct rtrs_srv_sess *sess)
469 if (rtrs_srv_change_state(sess, RTRS_SRV_CLOSING))
470 queue_work(rtrs_wq, &sess->close_work);
471 WARN_ON(sess->state != RTRS_SRV_CLOSING);
474 static inline const char *rtrs_srv_state_str(enum rtrs_srv_state state)
477 case RTRS_SRV_CONNECTING:
478 return "RTRS_SRV_CONNECTING";
479 case RTRS_SRV_CONNECTED:
480 return "RTRS_SRV_CONNECTED";
481 case RTRS_SRV_CLOSING:
482 return "RTRS_SRV_CLOSING";
483 case RTRS_SRV_CLOSED:
484 return "RTRS_SRV_CLOSED";
491 * rtrs_srv_resp_rdma() - Finish an RDMA request
493 * @id: Internal RTRS operation identifier
494 * @status: Response Code sent to the other side for this operation.
495 * 0 = success, <=0 error
498 * Finish a RDMA operation. A message is sent to the client and the
499 * corresponding memory areas will be released.
501 bool rtrs_srv_resp_rdma(struct rtrs_srv_op *id, int status)
503 struct rtrs_srv_sess *sess;
504 struct rtrs_srv_con *con;
513 sess = to_srv_sess(s);
517 if (unlikely(sess->state != RTRS_SRV_CONNECTED)) {
519 "Sending I/O response failed, session is disconnected, sess state %s\n",
520 rtrs_srv_state_str(sess->state));
523 if (always_invalidate) {
524 struct rtrs_srv_mr *mr = &sess->mrs[id->msg_id];
526 ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey));
528 if (unlikely(atomic_sub_return(1,
529 &con->sq_wr_avail) < 0)) {
530 pr_err("IB send queue full\n");
531 atomic_add(1, &con->sq_wr_avail);
532 spin_lock(&con->rsp_wr_wait_lock);
533 list_add_tail(&id->wait_list, &con->rsp_wr_wait_list);
534 spin_unlock(&con->rsp_wr_wait_lock);
538 if (status || id->dir == WRITE || !id->rd_msg->sg_cnt)
539 err = send_io_resp_imm(con, id, status);
541 err = rdma_write_sg(id);
544 rtrs_err_rl(s, "IO response failed: %d\n", err);
548 rtrs_srv_put_ops_ids(sess);
551 EXPORT_SYMBOL(rtrs_srv_resp_rdma);
554 * rtrs_srv_set_sess_priv() - Set private pointer in rtrs_srv.
555 * @srv: Session pointer
556 * @priv: The private pointer that is associated with the session.
558 void rtrs_srv_set_sess_priv(struct rtrs_srv *srv, void *priv)
562 EXPORT_SYMBOL(rtrs_srv_set_sess_priv);
564 static void unmap_cont_bufs(struct rtrs_srv_sess *sess)
568 for (i = 0; i < sess->mrs_num; i++) {
569 struct rtrs_srv_mr *srv_mr;
571 srv_mr = &sess->mrs[i];
572 rtrs_iu_free(srv_mr->iu, sess->s.dev->ib_dev, 1);
573 ib_dereg_mr(srv_mr->mr);
574 ib_dma_unmap_sg(sess->s.dev->ib_dev, srv_mr->sgt.sgl,
575 srv_mr->sgt.nents, DMA_BIDIRECTIONAL);
576 sg_free_table(&srv_mr->sgt);
581 static int map_cont_bufs(struct rtrs_srv_sess *sess)
583 struct rtrs_srv *srv = sess->srv;
584 struct rtrs_sess *ss = &sess->s;
585 int i, mri, err, mrs_num;
586 unsigned int chunk_bits;
587 int chunks_per_mr = 1;
590 * Here we map queue_depth chunks to MR. Firstly we have to
591 * figure out how many chunks can we map per MR.
593 if (always_invalidate) {
595 * in order to do invalidate for each chunks of memory, we needs
596 * more memory regions.
598 mrs_num = srv->queue_depth;
601 sess->s.dev->ib_dev->attrs.max_fast_reg_page_list_len;
602 mrs_num = DIV_ROUND_UP(srv->queue_depth, chunks_per_mr);
603 chunks_per_mr = DIV_ROUND_UP(srv->queue_depth, mrs_num);
606 sess->mrs = kcalloc(mrs_num, sizeof(*sess->mrs), GFP_KERNEL);
610 sess->mrs_num = mrs_num;
612 for (mri = 0; mri < mrs_num; mri++) {
613 struct rtrs_srv_mr *srv_mr = &sess->mrs[mri];
614 struct sg_table *sgt = &srv_mr->sgt;
615 struct scatterlist *s;
619 chunks = chunks_per_mr * mri;
620 if (!always_invalidate)
621 chunks_per_mr = min_t(int, chunks_per_mr,
622 srv->queue_depth - chunks);
624 err = sg_alloc_table(sgt, chunks_per_mr, GFP_KERNEL);
628 for_each_sg(sgt->sgl, s, chunks_per_mr, i)
629 sg_set_page(s, srv->chunks[chunks + i],
632 nr = ib_dma_map_sg(sess->s.dev->ib_dev, sgt->sgl,
633 sgt->nents, DMA_BIDIRECTIONAL);
634 if (nr < sgt->nents) {
635 err = nr < 0 ? nr : -EINVAL;
638 mr = ib_alloc_mr(sess->s.dev->ib_pd, IB_MR_TYPE_MEM_REG,
644 nr = ib_map_mr_sg(mr, sgt->sgl, sgt->nents,
645 NULL, max_chunk_size);
646 if (nr < 0 || nr < sgt->nents) {
647 err = nr < 0 ? nr : -EINVAL;
651 if (always_invalidate) {
652 srv_mr->iu = rtrs_iu_alloc(1,
653 sizeof(struct rtrs_msg_rkey_rsp),
654 GFP_KERNEL, sess->s.dev->ib_dev,
655 DMA_TO_DEVICE, rtrs_srv_rdma_done);
658 rtrs_err(ss, "rtrs_iu_alloc(), err: %d\n", err);
662 /* Eventually dma addr for each chunk can be cached */
663 for_each_sg(sgt->sgl, s, sgt->orig_nents, i)
664 sess->dma_addr[chunks + i] = sg_dma_address(s);
666 ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
672 srv_mr = &sess->mrs[mri];
675 rtrs_iu_free(srv_mr->iu, sess->s.dev->ib_dev, 1);
679 ib_dma_unmap_sg(sess->s.dev->ib_dev, sgt->sgl,
680 sgt->nents, DMA_BIDIRECTIONAL);
689 chunk_bits = ilog2(srv->queue_depth - 1) + 1;
690 sess->mem_bits = (MAX_IMM_PAYL_BITS - chunk_bits);
695 static void rtrs_srv_hb_err_handler(struct rtrs_con *c)
697 close_sess(to_srv_sess(c->sess));
700 static void rtrs_srv_init_hb(struct rtrs_srv_sess *sess)
702 rtrs_init_hb(&sess->s, &io_comp_cqe,
705 rtrs_srv_hb_err_handler,
709 static void rtrs_srv_start_hb(struct rtrs_srv_sess *sess)
711 rtrs_start_hb(&sess->s);
714 static void rtrs_srv_stop_hb(struct rtrs_srv_sess *sess)
716 rtrs_stop_hb(&sess->s);
719 static void rtrs_srv_info_rsp_done(struct ib_cq *cq, struct ib_wc *wc)
721 struct rtrs_srv_con *con = cq->cq_context;
722 struct rtrs_sess *s = con->c.sess;
723 struct rtrs_srv_sess *sess = to_srv_sess(s);
726 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
727 rtrs_iu_free(iu, sess->s.dev->ib_dev, 1);
729 if (unlikely(wc->status != IB_WC_SUCCESS)) {
730 rtrs_err(s, "Sess info response send failed: %s\n",
731 ib_wc_status_msg(wc->status));
735 WARN_ON(wc->opcode != IB_WC_SEND);
738 static void rtrs_srv_sess_up(struct rtrs_srv_sess *sess)
740 struct rtrs_srv *srv = sess->srv;
741 struct rtrs_srv_ctx *ctx = srv->ctx;
744 mutex_lock(&srv->paths_ev_mutex);
745 up = ++srv->paths_up;
747 ctx->ops.link_ev(srv, RTRS_SRV_LINK_EV_CONNECTED, NULL);
748 mutex_unlock(&srv->paths_ev_mutex);
750 /* Mark session as established */
751 sess->established = true;
754 static void rtrs_srv_sess_down(struct rtrs_srv_sess *sess)
756 struct rtrs_srv *srv = sess->srv;
757 struct rtrs_srv_ctx *ctx = srv->ctx;
759 if (!sess->established)
762 sess->established = false;
763 mutex_lock(&srv->paths_ev_mutex);
764 WARN_ON(!srv->paths_up);
765 if (--srv->paths_up == 0)
766 ctx->ops.link_ev(srv, RTRS_SRV_LINK_EV_DISCONNECTED, srv->priv);
767 mutex_unlock(&srv->paths_ev_mutex);
770 static int post_recv_sess(struct rtrs_srv_sess *sess);
772 static int process_info_req(struct rtrs_srv_con *con,
773 struct rtrs_msg_info_req *msg)
775 struct rtrs_sess *s = con->c.sess;
776 struct rtrs_srv_sess *sess = to_srv_sess(s);
777 struct ib_send_wr *reg_wr = NULL;
778 struct rtrs_msg_info_rsp *rsp;
779 struct rtrs_iu *tx_iu;
780 struct ib_reg_wr *rwr;
784 err = post_recv_sess(sess);
786 rtrs_err(s, "post_recv_sess(), err: %d\n", err);
789 rwr = kcalloc(sess->mrs_num, sizeof(*rwr), GFP_KERNEL);
792 strlcpy(sess->s.sessname, msg->sessname, sizeof(sess->s.sessname));
794 tx_sz = sizeof(*rsp);
795 tx_sz += sizeof(rsp->desc[0]) * sess->mrs_num;
796 tx_iu = rtrs_iu_alloc(1, tx_sz, GFP_KERNEL, sess->s.dev->ib_dev,
797 DMA_TO_DEVICE, rtrs_srv_info_rsp_done);
798 if (unlikely(!tx_iu)) {
804 rsp->type = cpu_to_le16(RTRS_MSG_INFO_RSP);
805 rsp->sg_cnt = cpu_to_le16(sess->mrs_num);
807 for (mri = 0; mri < sess->mrs_num; mri++) {
808 struct ib_mr *mr = sess->mrs[mri].mr;
810 rsp->desc[mri].addr = cpu_to_le64(mr->iova);
811 rsp->desc[mri].key = cpu_to_le32(mr->rkey);
812 rsp->desc[mri].len = cpu_to_le32(mr->length);
815 * Fill in reg MR request and chain them *backwards*
817 rwr[mri].wr.next = mri ? &rwr[mri - 1].wr : NULL;
818 rwr[mri].wr.opcode = IB_WR_REG_MR;
819 rwr[mri].wr.wr_cqe = &local_reg_cqe;
820 rwr[mri].wr.num_sge = 0;
821 rwr[mri].wr.send_flags = mri ? 0 : IB_SEND_SIGNALED;
823 rwr[mri].key = mr->rkey;
824 rwr[mri].access = (IB_ACCESS_LOCAL_WRITE |
825 IB_ACCESS_REMOTE_WRITE);
826 reg_wr = &rwr[mri].wr;
829 err = rtrs_srv_create_sess_files(sess);
832 kobject_get(&sess->kobj);
833 get_device(&sess->srv->dev);
834 rtrs_srv_change_state(sess, RTRS_SRV_CONNECTED);
835 rtrs_srv_start_hb(sess);
838 * We do not account number of established connections at the current
839 * moment, we rely on the client, which should send info request when
840 * all connections are successfully established. Thus, simply notify
841 * listener with a proper event if we are the first path.
843 rtrs_srv_sess_up(sess);
845 ib_dma_sync_single_for_device(sess->s.dev->ib_dev, tx_iu->dma_addr,
846 tx_iu->size, DMA_TO_DEVICE);
848 /* Send info response */
849 err = rtrs_iu_post_send(&con->c, tx_iu, tx_sz, reg_wr);
851 rtrs_err(s, "rtrs_iu_post_send(), err: %d\n", err);
853 rtrs_iu_free(tx_iu, sess->s.dev->ib_dev, 1);
861 static void rtrs_srv_info_req_done(struct ib_cq *cq, struct ib_wc *wc)
863 struct rtrs_srv_con *con = cq->cq_context;
864 struct rtrs_sess *s = con->c.sess;
865 struct rtrs_srv_sess *sess = to_srv_sess(s);
866 struct rtrs_msg_info_req *msg;
872 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
873 if (unlikely(wc->status != IB_WC_SUCCESS)) {
874 rtrs_err(s, "Sess info request receive failed: %s\n",
875 ib_wc_status_msg(wc->status));
878 WARN_ON(wc->opcode != IB_WC_RECV);
880 if (unlikely(wc->byte_len < sizeof(*msg))) {
881 rtrs_err(s, "Sess info request is malformed: size %d\n",
885 ib_dma_sync_single_for_cpu(sess->s.dev->ib_dev, iu->dma_addr,
886 iu->size, DMA_FROM_DEVICE);
888 if (unlikely(le16_to_cpu(msg->type) != RTRS_MSG_INFO_REQ)) {
889 rtrs_err(s, "Sess info request is malformed: type %d\n",
890 le16_to_cpu(msg->type));
893 err = process_info_req(con, msg);
898 rtrs_iu_free(iu, sess->s.dev->ib_dev, 1);
905 static int post_recv_info_req(struct rtrs_srv_con *con)
907 struct rtrs_sess *s = con->c.sess;
908 struct rtrs_srv_sess *sess = to_srv_sess(s);
909 struct rtrs_iu *rx_iu;
912 rx_iu = rtrs_iu_alloc(1, sizeof(struct rtrs_msg_info_req),
913 GFP_KERNEL, sess->s.dev->ib_dev,
914 DMA_FROM_DEVICE, rtrs_srv_info_req_done);
915 if (unlikely(!rx_iu))
917 /* Prepare for getting info response */
918 err = rtrs_iu_post_recv(&con->c, rx_iu);
920 rtrs_err(s, "rtrs_iu_post_recv(), err: %d\n", err);
921 rtrs_iu_free(rx_iu, sess->s.dev->ib_dev, 1);
928 static int post_recv_io(struct rtrs_srv_con *con, size_t q_size)
932 for (i = 0; i < q_size; i++) {
933 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe);
941 static int post_recv_sess(struct rtrs_srv_sess *sess)
943 struct rtrs_srv *srv = sess->srv;
944 struct rtrs_sess *s = &sess->s;
948 for (cid = 0; cid < sess->s.con_num; cid++) {
950 q_size = SERVICE_CON_QUEUE_DEPTH;
952 q_size = srv->queue_depth;
954 err = post_recv_io(to_srv_con(sess->s.con[cid]), q_size);
956 rtrs_err(s, "post_recv_io(), err: %d\n", err);
964 static void process_read(struct rtrs_srv_con *con,
965 struct rtrs_msg_rdma_read *msg,
968 struct rtrs_sess *s = con->c.sess;
969 struct rtrs_srv_sess *sess = to_srv_sess(s);
970 struct rtrs_srv *srv = sess->srv;
971 struct rtrs_srv_ctx *ctx = srv->ctx;
972 struct rtrs_srv_op *id;
974 size_t usr_len, data_len;
978 if (unlikely(sess->state != RTRS_SRV_CONNECTED)) {
980 "Processing read request failed, session is disconnected, sess state %s\n",
981 rtrs_srv_state_str(sess->state));
984 if (unlikely(msg->sg_cnt != 1 && msg->sg_cnt != 0)) {
986 "Processing read request failed, invalid message\n");
989 rtrs_srv_get_ops_ids(sess);
990 rtrs_srv_update_rdma_stats(sess->stats, off, READ);
991 id = sess->ops_ids[buf_id];
996 usr_len = le16_to_cpu(msg->usr_len);
997 data_len = off - usr_len;
998 data = page_address(srv->chunks[buf_id]);
999 ret = ctx->ops.rdma_ev(srv, srv->priv, id, READ, data, data_len,
1000 data + data_len, usr_len);
1002 if (unlikely(ret)) {
1004 "Processing read request failed, user module cb reported for msg_id %d, err: %d\n",
1012 ret = send_io_resp_imm(con, id, ret);
1015 "Sending err msg for failed RDMA-Write-Req failed, msg_id %d, err: %d\n",
1019 rtrs_srv_put_ops_ids(sess);
1022 static void process_write(struct rtrs_srv_con *con,
1023 struct rtrs_msg_rdma_write *req,
1024 u32 buf_id, u32 off)
1026 struct rtrs_sess *s = con->c.sess;
1027 struct rtrs_srv_sess *sess = to_srv_sess(s);
1028 struct rtrs_srv *srv = sess->srv;
1029 struct rtrs_srv_ctx *ctx = srv->ctx;
1030 struct rtrs_srv_op *id;
1032 size_t data_len, usr_len;
1036 if (unlikely(sess->state != RTRS_SRV_CONNECTED)) {
1038 "Processing write request failed, session is disconnected, sess state %s\n",
1039 rtrs_srv_state_str(sess->state));
1042 rtrs_srv_get_ops_ids(sess);
1043 rtrs_srv_update_rdma_stats(sess->stats, off, WRITE);
1044 id = sess->ops_ids[buf_id];
1047 id->msg_id = buf_id;
1049 usr_len = le16_to_cpu(req->usr_len);
1050 data_len = off - usr_len;
1051 data = page_address(srv->chunks[buf_id]);
1052 ret = ctx->ops.rdma_ev(srv, srv->priv, id, WRITE, data, data_len,
1053 data + data_len, usr_len);
1054 if (unlikely(ret)) {
1056 "Processing write request failed, user module callback reports err: %d\n",
1064 ret = send_io_resp_imm(con, id, ret);
1067 "Processing write request failed, sending I/O response failed, msg_id %d, err: %d\n",
1071 rtrs_srv_put_ops_ids(sess);
1074 static void process_io_req(struct rtrs_srv_con *con, void *msg,
1077 struct rtrs_sess *s = con->c.sess;
1078 struct rtrs_srv_sess *sess = to_srv_sess(s);
1079 struct rtrs_msg_rdma_hdr *hdr;
1082 ib_dma_sync_single_for_cpu(sess->s.dev->ib_dev, sess->dma_addr[id],
1083 max_chunk_size, DMA_BIDIRECTIONAL);
1085 type = le16_to_cpu(hdr->type);
1088 case RTRS_MSG_WRITE:
1089 process_write(con, msg, id, off);
1092 process_read(con, msg, id, off);
1096 "Processing I/O request failed, unknown message type received: 0x%02x\n",
1107 static void rtrs_srv_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1109 struct rtrs_srv_mr *mr =
1110 container_of(wc->wr_cqe, typeof(*mr), inv_cqe);
1111 struct rtrs_srv_con *con = cq->cq_context;
1112 struct rtrs_sess *s = con->c.sess;
1113 struct rtrs_srv_sess *sess = to_srv_sess(s);
1114 struct rtrs_srv *srv = sess->srv;
1118 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1119 rtrs_err(s, "Failed IB_WR_LOCAL_INV: %s\n",
1120 ib_wc_status_msg(wc->status));
1123 msg_id = mr->msg_id;
1125 data = page_address(srv->chunks[msg_id]) + off;
1126 process_io_req(con, data, msg_id, off);
1129 static int rtrs_srv_inv_rkey(struct rtrs_srv_con *con,
1130 struct rtrs_srv_mr *mr)
1132 struct ib_send_wr wr = {
1133 .opcode = IB_WR_LOCAL_INV,
1134 .wr_cqe = &mr->inv_cqe,
1135 .send_flags = IB_SEND_SIGNALED,
1136 .ex.invalidate_rkey = mr->mr->rkey,
1138 mr->inv_cqe.done = rtrs_srv_inv_rkey_done;
1140 return ib_post_send(con->c.qp, &wr, NULL);
1143 static void rtrs_rdma_process_wr_wait_list(struct rtrs_srv_con *con)
1145 spin_lock(&con->rsp_wr_wait_lock);
1146 while (!list_empty(&con->rsp_wr_wait_list)) {
1147 struct rtrs_srv_op *id;
1150 id = list_entry(con->rsp_wr_wait_list.next,
1151 struct rtrs_srv_op, wait_list);
1152 list_del(&id->wait_list);
1154 spin_unlock(&con->rsp_wr_wait_lock);
1155 ret = rtrs_srv_resp_rdma(id, id->status);
1156 spin_lock(&con->rsp_wr_wait_lock);
1159 list_add(&id->wait_list, &con->rsp_wr_wait_list);
1163 spin_unlock(&con->rsp_wr_wait_lock);
1166 static void rtrs_srv_rdma_done(struct ib_cq *cq, struct ib_wc *wc)
1168 struct rtrs_srv_con *con = cq->cq_context;
1169 struct rtrs_sess *s = con->c.sess;
1170 struct rtrs_srv_sess *sess = to_srv_sess(s);
1171 struct rtrs_srv *srv = sess->srv;
1172 u32 imm_type, imm_payload;
1175 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1176 if (wc->status != IB_WC_WR_FLUSH_ERR) {
1178 "%s (wr_cqe: %p, type: %d, vendor_err: 0x%x, len: %u)\n",
1179 ib_wc_status_msg(wc->status), wc->wr_cqe,
1180 wc->opcode, wc->vendor_err, wc->byte_len);
1186 switch (wc->opcode) {
1187 case IB_WC_RECV_RDMA_WITH_IMM:
1189 * post_recv() RDMA write completions of IO reqs (read/write)
1192 if (WARN_ON(wc->wr_cqe != &io_comp_cqe))
1194 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe);
1195 if (unlikely(err)) {
1196 rtrs_err(s, "rtrs_post_recv(), err: %d\n", err);
1200 rtrs_from_imm(be32_to_cpu(wc->ex.imm_data),
1201 &imm_type, &imm_payload);
1202 if (likely(imm_type == RTRS_IO_REQ_IMM)) {
1206 msg_id = imm_payload >> sess->mem_bits;
1207 off = imm_payload & ((1 << sess->mem_bits) - 1);
1208 if (unlikely(msg_id >= srv->queue_depth ||
1209 off >= max_chunk_size)) {
1210 rtrs_err(s, "Wrong msg_id %u, off %u\n",
1215 if (always_invalidate) {
1216 struct rtrs_srv_mr *mr = &sess->mrs[msg_id];
1219 mr->msg_id = msg_id;
1220 err = rtrs_srv_inv_rkey(con, mr);
1221 if (unlikely(err)) {
1222 rtrs_err(s, "rtrs_post_recv(), err: %d\n",
1228 data = page_address(srv->chunks[msg_id]) + off;
1229 process_io_req(con, data, msg_id, off);
1231 } else if (imm_type == RTRS_HB_MSG_IMM) {
1232 WARN_ON(con->c.cid);
1233 rtrs_send_hb_ack(&sess->s);
1234 } else if (imm_type == RTRS_HB_ACK_IMM) {
1235 WARN_ON(con->c.cid);
1236 sess->s.hb_missed_cnt = 0;
1238 rtrs_wrn(s, "Unknown IMM type %u\n", imm_type);
1241 case IB_WC_RDMA_WRITE:
1244 * post_send() RDMA write completions of IO reqs (read/write)
1247 atomic_add(srv->queue_depth, &con->sq_wr_avail);
1249 if (unlikely(!list_empty_careful(&con->rsp_wr_wait_list)))
1250 rtrs_rdma_process_wr_wait_list(con);
1254 rtrs_wrn(s, "Unexpected WC type: %d\n", wc->opcode);
1260 * rtrs_srv_get_sess_name() - Get rtrs_srv peer hostname.
1262 * @sessname: Sessname buffer
1263 * @len: Length of sessname buffer
1265 int rtrs_srv_get_sess_name(struct rtrs_srv *srv, char *sessname, size_t len)
1267 struct rtrs_srv_sess *sess;
1268 int err = -ENOTCONN;
1270 mutex_lock(&srv->paths_mutex);
1271 list_for_each_entry(sess, &srv->paths_list, s.entry) {
1272 if (sess->state != RTRS_SRV_CONNECTED)
1274 strlcpy(sessname, sess->s.sessname,
1275 min_t(size_t, sizeof(sess->s.sessname), len));
1279 mutex_unlock(&srv->paths_mutex);
1283 EXPORT_SYMBOL(rtrs_srv_get_sess_name);
1286 * rtrs_srv_get_sess_qdepth() - Get rtrs_srv qdepth.
1289 int rtrs_srv_get_queue_depth(struct rtrs_srv *srv)
1291 return srv->queue_depth;
1293 EXPORT_SYMBOL(rtrs_srv_get_queue_depth);
1295 static int find_next_bit_ring(struct rtrs_srv_sess *sess)
1297 struct ib_device *ib_dev = sess->s.dev->ib_dev;
1300 v = cpumask_next(sess->cur_cq_vector, &cq_affinity_mask);
1301 if (v >= nr_cpu_ids || v >= ib_dev->num_comp_vectors)
1302 v = cpumask_first(&cq_affinity_mask);
1306 static int rtrs_srv_get_next_cq_vector(struct rtrs_srv_sess *sess)
1308 sess->cur_cq_vector = find_next_bit_ring(sess);
1310 return sess->cur_cq_vector;
1313 static void rtrs_srv_dev_release(struct device *dev)
1315 struct rtrs_srv *srv = container_of(dev, struct rtrs_srv, dev);
1320 static void free_srv(struct rtrs_srv *srv)
1324 WARN_ON(refcount_read(&srv->refcount));
1325 for (i = 0; i < srv->queue_depth; i++)
1326 mempool_free(srv->chunks[i], chunk_pool);
1328 mutex_destroy(&srv->paths_mutex);
1329 mutex_destroy(&srv->paths_ev_mutex);
1330 /* last put to release the srv structure */
1331 put_device(&srv->dev);
1334 static struct rtrs_srv *get_or_create_srv(struct rtrs_srv_ctx *ctx,
1335 const uuid_t *paths_uuid)
1337 struct rtrs_srv *srv;
1340 mutex_lock(&ctx->srv_mutex);
1341 list_for_each_entry(srv, &ctx->srv_list, ctx_list) {
1342 if (uuid_equal(&srv->paths_uuid, paths_uuid) &&
1343 refcount_inc_not_zero(&srv->refcount)) {
1344 mutex_unlock(&ctx->srv_mutex);
1349 /* need to allocate a new srv */
1350 srv = kzalloc(sizeof(*srv), GFP_KERNEL);
1352 mutex_unlock(&ctx->srv_mutex);
1356 INIT_LIST_HEAD(&srv->paths_list);
1357 mutex_init(&srv->paths_mutex);
1358 mutex_init(&srv->paths_ev_mutex);
1359 uuid_copy(&srv->paths_uuid, paths_uuid);
1360 srv->queue_depth = sess_queue_depth;
1362 device_initialize(&srv->dev);
1363 srv->dev.release = rtrs_srv_dev_release;
1364 list_add(&srv->ctx_list, &ctx->srv_list);
1365 mutex_unlock(&ctx->srv_mutex);
1367 srv->chunks = kcalloc(srv->queue_depth, sizeof(*srv->chunks),
1372 for (i = 0; i < srv->queue_depth; i++) {
1373 srv->chunks[i] = mempool_alloc(chunk_pool, GFP_KERNEL);
1374 if (!srv->chunks[i])
1375 goto err_free_chunks;
1377 refcount_set(&srv->refcount, 1);
1383 mempool_free(srv->chunks[i], chunk_pool);
1391 static void put_srv(struct rtrs_srv *srv)
1393 if (refcount_dec_and_test(&srv->refcount)) {
1394 struct rtrs_srv_ctx *ctx = srv->ctx;
1396 WARN_ON(srv->dev.kobj.state_in_sysfs);
1398 mutex_lock(&ctx->srv_mutex);
1399 list_del(&srv->ctx_list);
1400 mutex_unlock(&ctx->srv_mutex);
1405 static void __add_path_to_srv(struct rtrs_srv *srv,
1406 struct rtrs_srv_sess *sess)
1408 list_add_tail(&sess->s.entry, &srv->paths_list);
1410 WARN_ON(srv->paths_num >= MAX_PATHS_NUM);
1413 static void del_path_from_srv(struct rtrs_srv_sess *sess)
1415 struct rtrs_srv *srv = sess->srv;
1420 mutex_lock(&srv->paths_mutex);
1421 list_del(&sess->s.entry);
1422 WARN_ON(!srv->paths_num);
1424 mutex_unlock(&srv->paths_mutex);
1427 /* return true if addresses are the same, error other wise */
1428 static int sockaddr_cmp(const struct sockaddr *a, const struct sockaddr *b)
1430 switch (a->sa_family) {
1432 return memcmp(&((struct sockaddr_ib *)a)->sib_addr,
1433 &((struct sockaddr_ib *)b)->sib_addr,
1434 sizeof(struct ib_addr)) &&
1435 (b->sa_family == AF_IB);
1437 return memcmp(&((struct sockaddr_in *)a)->sin_addr,
1438 &((struct sockaddr_in *)b)->sin_addr,
1439 sizeof(struct in_addr)) &&
1440 (b->sa_family == AF_INET);
1442 return memcmp(&((struct sockaddr_in6 *)a)->sin6_addr,
1443 &((struct sockaddr_in6 *)b)->sin6_addr,
1444 sizeof(struct in6_addr)) &&
1445 (b->sa_family == AF_INET6);
1451 static bool __is_path_w_addr_exists(struct rtrs_srv *srv,
1452 struct rdma_addr *addr)
1454 struct rtrs_srv_sess *sess;
1456 list_for_each_entry(sess, &srv->paths_list, s.entry)
1457 if (!sockaddr_cmp((struct sockaddr *)&sess->s.dst_addr,
1458 (struct sockaddr *)&addr->dst_addr) &&
1459 !sockaddr_cmp((struct sockaddr *)&sess->s.src_addr,
1460 (struct sockaddr *)&addr->src_addr))
1466 static void free_sess(struct rtrs_srv_sess *sess)
1468 if (sess->kobj.state_in_sysfs)
1469 kobject_put(&sess->kobj);
1474 static void rtrs_srv_close_work(struct work_struct *work)
1476 struct rtrs_srv_sess *sess;
1477 struct rtrs_srv_con *con;
1480 sess = container_of(work, typeof(*sess), close_work);
1482 rtrs_srv_destroy_sess_files(sess);
1483 rtrs_srv_stop_hb(sess);
1485 for (i = 0; i < sess->s.con_num; i++) {
1486 if (!sess->s.con[i])
1488 con = to_srv_con(sess->s.con[i]);
1489 rdma_disconnect(con->c.cm_id);
1490 ib_drain_qp(con->c.qp);
1492 /* Wait for all inflights */
1493 rtrs_srv_wait_ops_ids(sess);
1495 /* Notify upper layer if we are the last path */
1496 rtrs_srv_sess_down(sess);
1498 unmap_cont_bufs(sess);
1499 rtrs_srv_free_ops_ids(sess);
1501 for (i = 0; i < sess->s.con_num; i++) {
1502 if (!sess->s.con[i])
1504 con = to_srv_con(sess->s.con[i]);
1505 rtrs_cq_qp_destroy(&con->c);
1506 rdma_destroy_id(con->c.cm_id);
1509 rtrs_ib_dev_put(sess->s.dev);
1511 del_path_from_srv(sess);
1514 rtrs_srv_change_state(sess, RTRS_SRV_CLOSED);
1516 kfree(sess->dma_addr);
1521 static int rtrs_rdma_do_accept(struct rtrs_srv_sess *sess,
1522 struct rdma_cm_id *cm_id)
1524 struct rtrs_srv *srv = sess->srv;
1525 struct rtrs_msg_conn_rsp msg;
1526 struct rdma_conn_param param;
1529 param = (struct rdma_conn_param) {
1530 .rnr_retry_count = 7,
1531 .private_data = &msg,
1532 .private_data_len = sizeof(msg),
1535 msg = (struct rtrs_msg_conn_rsp) {
1536 .magic = cpu_to_le16(RTRS_MAGIC),
1537 .version = cpu_to_le16(RTRS_PROTO_VER),
1538 .queue_depth = cpu_to_le16(srv->queue_depth),
1539 .max_io_size = cpu_to_le32(max_chunk_size - MAX_HDR_SIZE),
1540 .max_hdr_size = cpu_to_le32(MAX_HDR_SIZE),
1543 if (always_invalidate)
1544 msg.flags = cpu_to_le32(RTRS_MSG_NEW_RKEY_F);
1546 err = rdma_accept(cm_id, ¶m);
1548 pr_err("rdma_accept(), err: %d\n", err);
1553 static int rtrs_rdma_do_reject(struct rdma_cm_id *cm_id, int errno)
1555 struct rtrs_msg_conn_rsp msg;
1558 msg = (struct rtrs_msg_conn_rsp) {
1559 .magic = cpu_to_le16(RTRS_MAGIC),
1560 .version = cpu_to_le16(RTRS_PROTO_VER),
1561 .errno = cpu_to_le16(errno),
1564 err = rdma_reject(cm_id, &msg, sizeof(msg), IB_CM_REJ_CONSUMER_DEFINED);
1566 pr_err("rdma_reject(), err: %d\n", err);
1568 /* Bounce errno back */
1572 static struct rtrs_srv_sess *
1573 __find_sess(struct rtrs_srv *srv, const uuid_t *sess_uuid)
1575 struct rtrs_srv_sess *sess;
1577 list_for_each_entry(sess, &srv->paths_list, s.entry) {
1578 if (uuid_equal(&sess->s.uuid, sess_uuid))
1585 static int create_con(struct rtrs_srv_sess *sess,
1586 struct rdma_cm_id *cm_id,
1589 struct rtrs_srv *srv = sess->srv;
1590 struct rtrs_sess *s = &sess->s;
1591 struct rtrs_srv_con *con;
1593 u32 cq_size, wr_queue_size;
1596 con = kzalloc(sizeof(*con), GFP_KERNEL);
1602 spin_lock_init(&con->rsp_wr_wait_lock);
1603 INIT_LIST_HEAD(&con->rsp_wr_wait_list);
1604 con->c.cm_id = cm_id;
1605 con->c.sess = &sess->s;
1607 atomic_set(&con->wr_cnt, 0);
1609 if (con->c.cid == 0) {
1611 * All receive and all send (each requiring invalidate)
1612 * + 2 for drain and heartbeat
1614 wr_queue_size = SERVICE_CON_QUEUE_DEPTH * 3 + 2;
1615 cq_size = wr_queue_size;
1618 * If we have all receive requests posted and
1619 * all write requests posted and each read request
1620 * requires an invalidate request + drain
1621 * and qp gets into error state.
1623 cq_size = srv->queue_depth * 3 + 1;
1625 * In theory we might have queue_depth * 32
1626 * outstanding requests if an unsafe global key is used
1627 * and we have queue_depth read requests each consisting
1628 * of 32 different addresses. div 3 for mlx5.
1630 wr_queue_size = sess->s.dev->ib_dev->attrs.max_qp_wr / 3;
1632 atomic_set(&con->sq_wr_avail, wr_queue_size);
1633 cq_vector = rtrs_srv_get_next_cq_vector(sess);
1635 /* TODO: SOFTIRQ can be faster, but be careful with softirq context */
1636 err = rtrs_cq_qp_create(&sess->s, &con->c, 1, cq_vector, cq_size,
1637 wr_queue_size, wr_queue_size,
1640 rtrs_err(s, "rtrs_cq_qp_create(), err: %d\n", err);
1643 if (con->c.cid == 0) {
1644 err = post_recv_info_req(con);
1648 WARN_ON(sess->s.con[cid]);
1649 sess->s.con[cid] = &con->c;
1652 * Change context from server to current connection. The other
1653 * way is to use cm_id->qp->qp_context, which does not work on OFED.
1655 cm_id->context = &con->c;
1660 rtrs_cq_qp_destroy(&con->c);
1668 static struct rtrs_srv_sess *__alloc_sess(struct rtrs_srv *srv,
1669 struct rdma_cm_id *cm_id,
1670 unsigned int con_num,
1671 unsigned int recon_cnt,
1674 struct rtrs_srv_sess *sess;
1677 if (srv->paths_num >= MAX_PATHS_NUM) {
1681 if (__is_path_w_addr_exists(srv, &cm_id->route.addr)) {
1683 pr_err("Path with same addr exists\n");
1686 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
1690 sess->stats = kzalloc(sizeof(*sess->stats), GFP_KERNEL);
1694 sess->stats->sess = sess;
1696 sess->dma_addr = kcalloc(srv->queue_depth, sizeof(*sess->dma_addr),
1698 if (!sess->dma_addr)
1699 goto err_free_stats;
1701 sess->s.con = kcalloc(con_num, sizeof(*sess->s.con), GFP_KERNEL);
1703 goto err_free_dma_addr;
1705 sess->state = RTRS_SRV_CONNECTING;
1707 sess->cur_cq_vector = -1;
1708 sess->s.dst_addr = cm_id->route.addr.dst_addr;
1709 sess->s.src_addr = cm_id->route.addr.src_addr;
1710 sess->s.con_num = con_num;
1711 sess->s.recon_cnt = recon_cnt;
1712 uuid_copy(&sess->s.uuid, uuid);
1713 spin_lock_init(&sess->state_lock);
1714 INIT_WORK(&sess->close_work, rtrs_srv_close_work);
1715 rtrs_srv_init_hb(sess);
1717 sess->s.dev = rtrs_ib_dev_find_or_add(cm_id->device, &dev_pd);
1722 err = map_cont_bufs(sess);
1726 err = rtrs_srv_alloc_ops_ids(sess);
1728 goto err_unmap_bufs;
1730 __add_path_to_srv(srv, sess);
1735 unmap_cont_bufs(sess);
1737 rtrs_ib_dev_put(sess->s.dev);
1741 kfree(sess->dma_addr);
1747 return ERR_PTR(err);
1750 static int rtrs_rdma_connect(struct rdma_cm_id *cm_id,
1751 const struct rtrs_msg_conn_req *msg,
1754 struct rtrs_srv_ctx *ctx = cm_id->context;
1755 struct rtrs_srv_sess *sess;
1756 struct rtrs_srv *srv;
1758 u16 version, con_num, cid;
1762 if (len < sizeof(*msg)) {
1763 pr_err("Invalid RTRS connection request\n");
1764 goto reject_w_econnreset;
1766 if (le16_to_cpu(msg->magic) != RTRS_MAGIC) {
1767 pr_err("Invalid RTRS magic\n");
1768 goto reject_w_econnreset;
1770 version = le16_to_cpu(msg->version);
1771 if (version >> 8 != RTRS_PROTO_VER_MAJOR) {
1772 pr_err("Unsupported major RTRS version: %d, expected %d\n",
1773 version >> 8, RTRS_PROTO_VER_MAJOR);
1774 goto reject_w_econnreset;
1776 con_num = le16_to_cpu(msg->cid_num);
1777 if (con_num > 4096) {
1779 pr_err("Too many connections requested: %d\n", con_num);
1780 goto reject_w_econnreset;
1782 cid = le16_to_cpu(msg->cid);
1783 if (cid >= con_num) {
1785 pr_err("Incorrect cid: %d >= %d\n", cid, con_num);
1786 goto reject_w_econnreset;
1788 recon_cnt = le16_to_cpu(msg->recon_cnt);
1789 srv = get_or_create_srv(ctx, &msg->paths_uuid);
1791 * "refcount == 0" happens if a previous thread calls get_or_create_srv
1792 * allocate srv, but chunks of srv are not allocated yet.
1794 if (!srv || refcount_read(&srv->refcount) == 0) {
1798 mutex_lock(&srv->paths_mutex);
1799 sess = __find_sess(srv, &msg->sess_uuid);
1801 struct rtrs_sess *s = &sess->s;
1803 /* Session already holds a reference */
1806 if (sess->state != RTRS_SRV_CONNECTING) {
1807 rtrs_err(s, "Session in wrong state: %s\n",
1808 rtrs_srv_state_str(sess->state));
1809 mutex_unlock(&srv->paths_mutex);
1810 goto reject_w_econnreset;
1815 if (con_num != s->con_num || cid >= s->con_num) {
1816 rtrs_err(s, "Incorrect request: %d, %d\n",
1818 mutex_unlock(&srv->paths_mutex);
1819 goto reject_w_econnreset;
1822 rtrs_err(s, "Connection already exists: %d\n",
1824 mutex_unlock(&srv->paths_mutex);
1825 goto reject_w_econnreset;
1828 sess = __alloc_sess(srv, cm_id, con_num, recon_cnt,
1831 mutex_unlock(&srv->paths_mutex);
1833 err = PTR_ERR(sess);
1837 err = create_con(sess, cm_id, cid);
1839 (void)rtrs_rdma_do_reject(cm_id, err);
1841 * Since session has other connections we follow normal way
1842 * through workqueue, but still return an error to tell cma.c
1843 * to call rdma_destroy_id() for current connection.
1845 goto close_and_return_err;
1847 err = rtrs_rdma_do_accept(sess, cm_id);
1849 (void)rtrs_rdma_do_reject(cm_id, err);
1851 * Since current connection was successfully added to the
1852 * session we follow normal way through workqueue to close the
1853 * session, thus return 0 to tell cma.c we call
1854 * rdma_destroy_id() ourselves.
1857 goto close_and_return_err;
1859 mutex_unlock(&srv->paths_mutex);
1864 return rtrs_rdma_do_reject(cm_id, err);
1866 reject_w_econnreset:
1867 return rtrs_rdma_do_reject(cm_id, -ECONNRESET);
1869 close_and_return_err:
1870 mutex_unlock(&srv->paths_mutex);
1876 static int rtrs_srv_rdma_cm_handler(struct rdma_cm_id *cm_id,
1877 struct rdma_cm_event *ev)
1879 struct rtrs_srv_sess *sess = NULL;
1880 struct rtrs_sess *s = NULL;
1882 if (ev->event != RDMA_CM_EVENT_CONNECT_REQUEST) {
1883 struct rtrs_con *c = cm_id->context;
1886 sess = to_srv_sess(s);
1889 switch (ev->event) {
1890 case RDMA_CM_EVENT_CONNECT_REQUEST:
1892 * In case of error cma.c will destroy cm_id,
1893 * see cma_process_remove()
1895 return rtrs_rdma_connect(cm_id, ev->param.conn.private_data,
1896 ev->param.conn.private_data_len);
1897 case RDMA_CM_EVENT_ESTABLISHED:
1900 case RDMA_CM_EVENT_REJECTED:
1901 case RDMA_CM_EVENT_CONNECT_ERROR:
1902 case RDMA_CM_EVENT_UNREACHABLE:
1903 rtrs_err(s, "CM error (CM event: %s, err: %d)\n",
1904 rdma_event_msg(ev->event), ev->status);
1907 case RDMA_CM_EVENT_DISCONNECTED:
1908 case RDMA_CM_EVENT_ADDR_CHANGE:
1909 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1912 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1916 pr_err("Ignoring unexpected CM event %s, err %d\n",
1917 rdma_event_msg(ev->event), ev->status);
1924 static struct rdma_cm_id *rtrs_srv_cm_init(struct rtrs_srv_ctx *ctx,
1925 struct sockaddr *addr,
1926 enum rdma_ucm_port_space ps)
1928 struct rdma_cm_id *cm_id;
1931 cm_id = rdma_create_id(&init_net, rtrs_srv_rdma_cm_handler,
1932 ctx, ps, IB_QPT_RC);
1933 if (IS_ERR(cm_id)) {
1934 ret = PTR_ERR(cm_id);
1935 pr_err("Creating id for RDMA connection failed, err: %d\n",
1939 ret = rdma_bind_addr(cm_id, addr);
1941 pr_err("Binding RDMA address failed, err: %d\n", ret);
1944 ret = rdma_listen(cm_id, 64);
1946 pr_err("Listening on RDMA connection failed, err: %d\n",
1954 rdma_destroy_id(cm_id);
1957 return ERR_PTR(ret);
1960 static int rtrs_srv_rdma_init(struct rtrs_srv_ctx *ctx, u16 port)
1962 struct sockaddr_in6 sin = {
1963 .sin6_family = AF_INET6,
1964 .sin6_addr = IN6ADDR_ANY_INIT,
1965 .sin6_port = htons(port),
1967 struct sockaddr_ib sib = {
1968 .sib_family = AF_IB,
1969 .sib_sid = cpu_to_be64(RDMA_IB_IP_PS_IB | port),
1970 .sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL),
1971 .sib_pkey = cpu_to_be16(0xffff),
1973 struct rdma_cm_id *cm_ip, *cm_ib;
1977 * We accept both IPoIB and IB connections, so we need to keep
1978 * two cm id's, one for each socket type and port space.
1979 * If the cm initialization of one of the id's fails, we abort
1982 cm_ip = rtrs_srv_cm_init(ctx, (struct sockaddr *)&sin, RDMA_PS_TCP);
1984 return PTR_ERR(cm_ip);
1986 cm_ib = rtrs_srv_cm_init(ctx, (struct sockaddr *)&sib, RDMA_PS_IB);
1987 if (IS_ERR(cm_ib)) {
1988 ret = PTR_ERR(cm_ib);
1992 ctx->cm_id_ip = cm_ip;
1993 ctx->cm_id_ib = cm_ib;
1998 rdma_destroy_id(cm_ip);
2003 static struct rtrs_srv_ctx *alloc_srv_ctx(struct rtrs_srv_ops *ops)
2005 struct rtrs_srv_ctx *ctx;
2007 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2012 mutex_init(&ctx->srv_mutex);
2013 INIT_LIST_HEAD(&ctx->srv_list);
2018 static void free_srv_ctx(struct rtrs_srv_ctx *ctx)
2020 WARN_ON(!list_empty(&ctx->srv_list));
2021 mutex_destroy(&ctx->srv_mutex);
2025 static int rtrs_srv_add_one(struct ib_device *device)
2027 struct rtrs_srv_ctx *ctx;
2030 mutex_lock(&ib_ctx.ib_dev_mutex);
2031 if (ib_ctx.ib_dev_count)
2035 * Since our CM IDs are NOT bound to any ib device we will create them
2038 ctx = ib_ctx.srv_ctx;
2039 ret = rtrs_srv_rdma_init(ctx, ib_ctx.port);
2042 * We errored out here.
2043 * According to the ib code, if we encounter an error here then the
2044 * error code is ignored, and no more calls to our ops are made.
2046 pr_err("Failed to initialize RDMA connection");
2052 * Keep a track on the number of ib devices added
2054 ib_ctx.ib_dev_count++;
2057 mutex_unlock(&ib_ctx.ib_dev_mutex);
2061 static void rtrs_srv_remove_one(struct ib_device *device, void *client_data)
2063 struct rtrs_srv_ctx *ctx;
2065 mutex_lock(&ib_ctx.ib_dev_mutex);
2066 ib_ctx.ib_dev_count--;
2068 if (ib_ctx.ib_dev_count)
2072 * Since our CM IDs are NOT bound to any ib device we will remove them
2073 * only once, when the last device is removed
2075 ctx = ib_ctx.srv_ctx;
2076 rdma_destroy_id(ctx->cm_id_ip);
2077 rdma_destroy_id(ctx->cm_id_ib);
2080 mutex_unlock(&ib_ctx.ib_dev_mutex);
2083 static struct ib_client rtrs_srv_client = {
2084 .name = "rtrs_server",
2085 .add = rtrs_srv_add_one,
2086 .remove = rtrs_srv_remove_one
2090 * rtrs_srv_open() - open RTRS server context
2091 * @ops: callback functions
2092 * @port: port to listen on
2094 * Creates server context with specified callbacks.
2096 * Return a valid pointer on success otherwise PTR_ERR.
2098 struct rtrs_srv_ctx *rtrs_srv_open(struct rtrs_srv_ops *ops, u16 port)
2100 struct rtrs_srv_ctx *ctx;
2103 ctx = alloc_srv_ctx(ops);
2105 return ERR_PTR(-ENOMEM);
2107 mutex_init(&ib_ctx.ib_dev_mutex);
2108 ib_ctx.srv_ctx = ctx;
2111 err = ib_register_client(&rtrs_srv_client);
2114 return ERR_PTR(err);
2119 EXPORT_SYMBOL(rtrs_srv_open);
2121 static void close_sessions(struct rtrs_srv *srv)
2123 struct rtrs_srv_sess *sess;
2125 mutex_lock(&srv->paths_mutex);
2126 list_for_each_entry(sess, &srv->paths_list, s.entry)
2128 mutex_unlock(&srv->paths_mutex);
2131 static void close_ctx(struct rtrs_srv_ctx *ctx)
2133 struct rtrs_srv *srv;
2135 mutex_lock(&ctx->srv_mutex);
2136 list_for_each_entry(srv, &ctx->srv_list, ctx_list)
2137 close_sessions(srv);
2138 mutex_unlock(&ctx->srv_mutex);
2139 flush_workqueue(rtrs_wq);
2143 * rtrs_srv_close() - close RTRS server context
2144 * @ctx: pointer to server context
2146 * Closes RTRS server context with all client sessions.
2148 void rtrs_srv_close(struct rtrs_srv_ctx *ctx)
2150 ib_unregister_client(&rtrs_srv_client);
2151 mutex_destroy(&ib_ctx.ib_dev_mutex);
2155 EXPORT_SYMBOL(rtrs_srv_close);
2157 static int check_module_params(void)
2159 if (sess_queue_depth < 1 || sess_queue_depth > MAX_SESS_QUEUE_DEPTH) {
2160 pr_err("Invalid sess_queue_depth value %d, has to be >= %d, <= %d.\n",
2161 sess_queue_depth, 1, MAX_SESS_QUEUE_DEPTH);
2164 if (max_chunk_size < 4096 || !is_power_of_2(max_chunk_size)) {
2165 pr_err("Invalid max_chunk_size value %d, has to be >= %d and should be power of two.\n",
2166 max_chunk_size, 4096);
2171 * Check if IB immediate data size is enough to hold the mem_id and the
2172 * offset inside the memory chunk
2174 if ((ilog2(sess_queue_depth - 1) + 1) +
2175 (ilog2(max_chunk_size - 1) + 1) > MAX_IMM_PAYL_BITS) {
2176 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",
2177 MAX_IMM_PAYL_BITS, sess_queue_depth, max_chunk_size);
2184 static int __init rtrs_server_init(void)
2188 pr_info("Loading module %s, proto %s: (max_chunk_size: %d (pure IO %ld, headers %ld) , sess_queue_depth: %d, always_invalidate: %d)\n",
2189 KBUILD_MODNAME, RTRS_PROTO_VER_STRING,
2190 max_chunk_size, max_chunk_size - MAX_HDR_SIZE, MAX_HDR_SIZE,
2191 sess_queue_depth, always_invalidate);
2193 rtrs_rdma_dev_pd_init(0, &dev_pd);
2195 err = check_module_params();
2197 pr_err("Failed to load module, invalid module parameters, err: %d\n",
2201 chunk_pool = mempool_create_page_pool(sess_queue_depth * CHUNK_POOL_SZ,
2202 get_order(max_chunk_size));
2205 rtrs_dev_class = class_create(THIS_MODULE, "rtrs-server");
2206 if (IS_ERR(rtrs_dev_class)) {
2207 err = PTR_ERR(rtrs_dev_class);
2208 goto out_chunk_pool;
2210 rtrs_wq = alloc_workqueue("rtrs_server_wq", 0, 0);
2219 class_destroy(rtrs_dev_class);
2221 mempool_destroy(chunk_pool);
2226 static void __exit rtrs_server_exit(void)
2228 destroy_workqueue(rtrs_wq);
2229 class_destroy(rtrs_dev_class);
2230 mempool_destroy(chunk_pool);
2231 rtrs_rdma_dev_pd_deinit(&dev_pd);
2234 module_init(rtrs_server_init);
2235 module_exit(rtrs_server_exit);