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>
17 #include <rdma/ib_cm.h>
18 #include <rdma/ib_verbs.h>
19 #include "rtrs-srv-trace.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 static struct rtrs_rdma_dev_pd dev_pd;
30 const struct class rtrs_dev_class = {
31 .name = "rtrs-server",
33 static struct rtrs_srv_ib_ctx ib_ctx;
35 static int __read_mostly max_chunk_size = DEFAULT_MAX_CHUNK_SIZE;
36 static int __read_mostly sess_queue_depth = DEFAULT_SESS_QUEUE_DEPTH;
38 static bool always_invalidate = true;
39 module_param(always_invalidate, bool, 0444);
40 MODULE_PARM_DESC(always_invalidate,
41 "Invalidate memory registration for contiguous memory regions before accessing.");
43 module_param_named(max_chunk_size, max_chunk_size, int, 0444);
44 MODULE_PARM_DESC(max_chunk_size,
45 "Max size for each IO request, when change the unit is in byte (default: "
46 __stringify(DEFAULT_MAX_CHUNK_SIZE) "KB)");
48 module_param_named(sess_queue_depth, sess_queue_depth, int, 0444);
49 MODULE_PARM_DESC(sess_queue_depth,
50 "Number of buffers for pending I/O requests to allocate per session. Maximum: "
51 __stringify(MAX_SESS_QUEUE_DEPTH) " (default: "
52 __stringify(DEFAULT_SESS_QUEUE_DEPTH) ")");
54 static cpumask_t cq_affinity_mask = { CPU_BITS_ALL };
56 static struct workqueue_struct *rtrs_wq;
58 static inline struct rtrs_srv_con *to_srv_con(struct rtrs_con *c)
60 return container_of(c, struct rtrs_srv_con, c);
63 static bool rtrs_srv_change_state(struct rtrs_srv_path *srv_path,
64 enum rtrs_srv_state new_state)
66 enum rtrs_srv_state old_state;
70 spin_lock_irqsave(&srv_path->state_lock, flags);
71 old_state = srv_path->state;
73 case RTRS_SRV_CONNECTED:
74 if (old_state == RTRS_SRV_CONNECTING)
77 case RTRS_SRV_CLOSING:
78 if (old_state == RTRS_SRV_CONNECTING ||
79 old_state == RTRS_SRV_CONNECTED)
83 if (old_state == RTRS_SRV_CLOSING)
90 srv_path->state = new_state;
91 spin_unlock_irqrestore(&srv_path->state_lock, flags);
96 static void free_id(struct rtrs_srv_op *id)
103 static void rtrs_srv_free_ops_ids(struct rtrs_srv_path *srv_path)
105 struct rtrs_srv_sess *srv = srv_path->srv;
108 if (srv_path->ops_ids) {
109 for (i = 0; i < srv->queue_depth; i++)
110 free_id(srv_path->ops_ids[i]);
111 kfree(srv_path->ops_ids);
112 srv_path->ops_ids = NULL;
116 static void rtrs_srv_rdma_done(struct ib_cq *cq, struct ib_wc *wc);
118 static struct ib_cqe io_comp_cqe = {
119 .done = rtrs_srv_rdma_done
122 static inline void rtrs_srv_inflight_ref_release(struct percpu_ref *ref)
124 struct rtrs_srv_path *srv_path = container_of(ref,
125 struct rtrs_srv_path,
128 percpu_ref_exit(&srv_path->ids_inflight_ref);
129 complete(&srv_path->complete_done);
132 static int rtrs_srv_alloc_ops_ids(struct rtrs_srv_path *srv_path)
134 struct rtrs_srv_sess *srv = srv_path->srv;
135 struct rtrs_srv_op *id;
138 srv_path->ops_ids = kcalloc(srv->queue_depth,
139 sizeof(*srv_path->ops_ids),
141 if (!srv_path->ops_ids)
144 for (i = 0; i < srv->queue_depth; ++i) {
145 id = kzalloc(sizeof(*id), GFP_KERNEL);
149 srv_path->ops_ids[i] = id;
152 ret = percpu_ref_init(&srv_path->ids_inflight_ref,
153 rtrs_srv_inflight_ref_release, 0, GFP_KERNEL);
155 pr_err("Percpu reference init failed\n");
158 init_completion(&srv_path->complete_done);
163 rtrs_srv_free_ops_ids(srv_path);
167 static inline void rtrs_srv_get_ops_ids(struct rtrs_srv_path *srv_path)
169 percpu_ref_get(&srv_path->ids_inflight_ref);
172 static inline void rtrs_srv_put_ops_ids(struct rtrs_srv_path *srv_path)
174 percpu_ref_put(&srv_path->ids_inflight_ref);
177 static void rtrs_srv_reg_mr_done(struct ib_cq *cq, struct ib_wc *wc)
179 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
180 struct rtrs_path *s = con->c.path;
181 struct rtrs_srv_path *srv_path = to_srv_path(s);
183 if (wc->status != IB_WC_SUCCESS) {
184 rtrs_err(s, "REG MR failed: %s\n",
185 ib_wc_status_msg(wc->status));
186 close_path(srv_path);
191 static struct ib_cqe local_reg_cqe = {
192 .done = rtrs_srv_reg_mr_done
195 static int rdma_write_sg(struct rtrs_srv_op *id)
197 struct rtrs_path *s = id->con->c.path;
198 struct rtrs_srv_path *srv_path = to_srv_path(s);
199 dma_addr_t dma_addr = srv_path->dma_addr[id->msg_id];
200 struct rtrs_srv_mr *srv_mr;
201 struct ib_send_wr inv_wr;
202 struct ib_rdma_wr imm_wr;
203 struct ib_rdma_wr *wr = NULL;
204 enum ib_send_flags flags;
209 struct ib_reg_wr rwr;
210 struct ib_sge *plist;
213 sg_cnt = le16_to_cpu(id->rd_msg->sg_cnt);
214 need_inval = le16_to_cpu(id->rd_msg->flags) & RTRS_MSG_NEED_INVAL_F;
222 plist->addr = dma_addr + offset;
223 plist->length = le32_to_cpu(id->rd_msg->desc[0].len);
225 /* WR will fail with length error
228 if (plist->length == 0) {
229 rtrs_err(s, "Invalid RDMA-Write sg list length 0\n");
233 plist->lkey = srv_path->s.dev->ib_pd->local_dma_lkey;
234 offset += plist->length;
236 wr->wr.sg_list = plist;
238 wr->remote_addr = le64_to_cpu(id->rd_msg->desc[0].addr);
239 wr->rkey = le32_to_cpu(id->rd_msg->desc[0].key);
243 /* Only one key is actually used */
244 WARN_ON_ONCE(rkey != wr->rkey);
246 wr->wr.opcode = IB_WR_RDMA_WRITE;
247 wr->wr.wr_cqe = &io_comp_cqe;
248 wr->wr.ex.imm_data = 0;
249 wr->wr.send_flags = 0;
251 if (need_inval && always_invalidate) {
252 wr->wr.next = &rwr.wr;
253 rwr.wr.next = &inv_wr;
254 inv_wr.next = &imm_wr.wr;
255 } else if (always_invalidate) {
256 wr->wr.next = &rwr.wr;
257 rwr.wr.next = &imm_wr.wr;
258 } else if (need_inval) {
259 wr->wr.next = &inv_wr;
260 inv_wr.next = &imm_wr.wr;
262 wr->wr.next = &imm_wr.wr;
265 * From time to time we have to post signaled sends,
266 * or send queue will fill up and only QP reset can help.
268 flags = (atomic_inc_return(&id->con->c.wr_cnt) % s->signal_interval) ?
269 0 : IB_SEND_SIGNALED;
272 inv_wr.sg_list = NULL;
274 inv_wr.opcode = IB_WR_SEND_WITH_INV;
275 inv_wr.wr_cqe = &io_comp_cqe;
276 inv_wr.send_flags = 0;
277 inv_wr.ex.invalidate_rkey = rkey;
280 imm_wr.wr.next = NULL;
281 if (always_invalidate) {
282 struct rtrs_msg_rkey_rsp *msg;
284 srv_mr = &srv_path->mrs[id->msg_id];
285 rwr.wr.opcode = IB_WR_REG_MR;
286 rwr.wr.wr_cqe = &local_reg_cqe;
289 rwr.wr.send_flags = 0;
290 rwr.key = srv_mr->mr->rkey;
291 rwr.access = (IB_ACCESS_LOCAL_WRITE |
292 IB_ACCESS_REMOTE_WRITE);
293 msg = srv_mr->iu->buf;
294 msg->buf_id = cpu_to_le16(id->msg_id);
295 msg->type = cpu_to_le16(RTRS_MSG_RKEY_RSP);
296 msg->rkey = cpu_to_le32(srv_mr->mr->rkey);
298 list.addr = srv_mr->iu->dma_addr;
299 list.length = sizeof(*msg);
300 list.lkey = srv_path->s.dev->ib_pd->local_dma_lkey;
301 imm_wr.wr.sg_list = &list;
302 imm_wr.wr.num_sge = 1;
303 imm_wr.wr.opcode = IB_WR_SEND_WITH_IMM;
304 ib_dma_sync_single_for_device(srv_path->s.dev->ib_dev,
305 srv_mr->iu->dma_addr,
306 srv_mr->iu->size, DMA_TO_DEVICE);
308 imm_wr.wr.sg_list = NULL;
309 imm_wr.wr.num_sge = 0;
310 imm_wr.wr.opcode = IB_WR_RDMA_WRITE_WITH_IMM;
312 imm_wr.wr.send_flags = flags;
313 imm_wr.wr.ex.imm_data = cpu_to_be32(rtrs_to_io_rsp_imm(id->msg_id,
316 imm_wr.wr.wr_cqe = &io_comp_cqe;
317 ib_dma_sync_single_for_device(srv_path->s.dev->ib_dev, dma_addr,
318 offset, DMA_BIDIRECTIONAL);
320 err = ib_post_send(id->con->c.qp, &id->tx_wr.wr, NULL);
323 "Posting RDMA-Write-Request to QP failed, err: %d\n",
330 * send_io_resp_imm() - respond to client with empty IMM on failed READ/WRITE
331 * requests or on successful WRITE request.
332 * @con: the connection to send back result
333 * @id: the id associated with the IO
334 * @errno: the error number of the IO.
336 * Return 0 on success, errno otherwise.
338 static int send_io_resp_imm(struct rtrs_srv_con *con, struct rtrs_srv_op *id,
341 struct rtrs_path *s = con->c.path;
342 struct rtrs_srv_path *srv_path = to_srv_path(s);
343 struct ib_send_wr inv_wr, *wr = NULL;
344 struct ib_rdma_wr imm_wr;
345 struct ib_reg_wr rwr;
346 struct rtrs_srv_mr *srv_mr;
347 bool need_inval = false;
348 enum ib_send_flags flags;
352 if (id->dir == READ) {
353 struct rtrs_msg_rdma_read *rd_msg = id->rd_msg;
356 need_inval = le16_to_cpu(rd_msg->flags) &
357 RTRS_MSG_NEED_INVAL_F;
358 sg_cnt = le16_to_cpu(rd_msg->sg_cnt);
362 inv_wr.wr_cqe = &io_comp_cqe;
363 inv_wr.sg_list = NULL;
365 inv_wr.opcode = IB_WR_SEND_WITH_INV;
366 inv_wr.send_flags = 0;
367 /* Only one key is actually used */
368 inv_wr.ex.invalidate_rkey =
369 le32_to_cpu(rd_msg->desc[0].key);
377 trace_send_io_resp_imm(id, need_inval, always_invalidate, errno);
379 if (need_inval && always_invalidate) {
381 inv_wr.next = &rwr.wr;
382 rwr.wr.next = &imm_wr.wr;
383 } else if (always_invalidate) {
385 rwr.wr.next = &imm_wr.wr;
386 } else if (need_inval) {
388 inv_wr.next = &imm_wr.wr;
393 * From time to time we have to post signalled sends,
394 * or send queue will fill up and only QP reset can help.
396 flags = (atomic_inc_return(&con->c.wr_cnt) % s->signal_interval) ?
397 0 : IB_SEND_SIGNALED;
398 imm = rtrs_to_io_rsp_imm(id->msg_id, errno, need_inval);
399 imm_wr.wr.next = NULL;
400 if (always_invalidate) {
402 struct rtrs_msg_rkey_rsp *msg;
404 srv_mr = &srv_path->mrs[id->msg_id];
405 rwr.wr.next = &imm_wr.wr;
406 rwr.wr.opcode = IB_WR_REG_MR;
407 rwr.wr.wr_cqe = &local_reg_cqe;
409 rwr.wr.send_flags = 0;
411 rwr.key = srv_mr->mr->rkey;
412 rwr.access = (IB_ACCESS_LOCAL_WRITE |
413 IB_ACCESS_REMOTE_WRITE);
414 msg = srv_mr->iu->buf;
415 msg->buf_id = cpu_to_le16(id->msg_id);
416 msg->type = cpu_to_le16(RTRS_MSG_RKEY_RSP);
417 msg->rkey = cpu_to_le32(srv_mr->mr->rkey);
419 list.addr = srv_mr->iu->dma_addr;
420 list.length = sizeof(*msg);
421 list.lkey = srv_path->s.dev->ib_pd->local_dma_lkey;
422 imm_wr.wr.sg_list = &list;
423 imm_wr.wr.num_sge = 1;
424 imm_wr.wr.opcode = IB_WR_SEND_WITH_IMM;
425 ib_dma_sync_single_for_device(srv_path->s.dev->ib_dev,
426 srv_mr->iu->dma_addr,
427 srv_mr->iu->size, DMA_TO_DEVICE);
429 imm_wr.wr.sg_list = NULL;
430 imm_wr.wr.num_sge = 0;
431 imm_wr.wr.opcode = IB_WR_RDMA_WRITE_WITH_IMM;
433 imm_wr.wr.send_flags = flags;
434 imm_wr.wr.wr_cqe = &io_comp_cqe;
436 imm_wr.wr.ex.imm_data = cpu_to_be32(imm);
438 err = ib_post_send(id->con->c.qp, wr, NULL);
440 rtrs_err_rl(s, "Posting RDMA-Reply to QP failed, err: %d\n",
446 void close_path(struct rtrs_srv_path *srv_path)
448 if (rtrs_srv_change_state(srv_path, RTRS_SRV_CLOSING))
449 queue_work(rtrs_wq, &srv_path->close_work);
450 WARN_ON(srv_path->state != RTRS_SRV_CLOSING);
453 static inline const char *rtrs_srv_state_str(enum rtrs_srv_state state)
456 case RTRS_SRV_CONNECTING:
457 return "RTRS_SRV_CONNECTING";
458 case RTRS_SRV_CONNECTED:
459 return "RTRS_SRV_CONNECTED";
460 case RTRS_SRV_CLOSING:
461 return "RTRS_SRV_CLOSING";
462 case RTRS_SRV_CLOSED:
463 return "RTRS_SRV_CLOSED";
470 * rtrs_srv_resp_rdma() - Finish an RDMA request
472 * @id: Internal RTRS operation identifier
473 * @status: Response Code sent to the other side for this operation.
474 * 0 = success, <=0 error
477 * Finish a RDMA operation. A message is sent to the client and the
478 * corresponding memory areas will be released.
480 bool rtrs_srv_resp_rdma(struct rtrs_srv_op *id, int status)
482 struct rtrs_srv_path *srv_path;
483 struct rtrs_srv_con *con;
492 srv_path = to_srv_path(s);
496 if (srv_path->state != RTRS_SRV_CONNECTED) {
498 "Sending I/O response failed, server path %s is disconnected, path state %s\n",
499 kobject_name(&srv_path->kobj),
500 rtrs_srv_state_str(srv_path->state));
503 if (always_invalidate) {
504 struct rtrs_srv_mr *mr = &srv_path->mrs[id->msg_id];
506 ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey));
508 if (atomic_sub_return(1, &con->c.sq_wr_avail) < 0) {
509 rtrs_err(s, "IB send queue full: srv_path=%s cid=%d\n",
510 kobject_name(&srv_path->kobj),
512 atomic_add(1, &con->c.sq_wr_avail);
513 spin_lock(&con->rsp_wr_wait_lock);
514 list_add_tail(&id->wait_list, &con->rsp_wr_wait_list);
515 spin_unlock(&con->rsp_wr_wait_lock);
519 if (status || id->dir == WRITE || !id->rd_msg->sg_cnt)
520 err = send_io_resp_imm(con, id, status);
522 err = rdma_write_sg(id);
525 rtrs_err_rl(s, "IO response failed: %d: srv_path=%s\n", err,
526 kobject_name(&srv_path->kobj));
527 close_path(srv_path);
530 rtrs_srv_put_ops_ids(srv_path);
533 EXPORT_SYMBOL(rtrs_srv_resp_rdma);
536 * rtrs_srv_set_sess_priv() - Set private pointer in rtrs_srv.
537 * @srv: Session pointer
538 * @priv: The private pointer that is associated with the session.
540 void rtrs_srv_set_sess_priv(struct rtrs_srv_sess *srv, void *priv)
544 EXPORT_SYMBOL(rtrs_srv_set_sess_priv);
546 static void unmap_cont_bufs(struct rtrs_srv_path *srv_path)
550 for (i = 0; i < srv_path->mrs_num; i++) {
551 struct rtrs_srv_mr *srv_mr;
553 srv_mr = &srv_path->mrs[i];
555 if (always_invalidate)
556 rtrs_iu_free(srv_mr->iu, srv_path->s.dev->ib_dev, 1);
558 ib_dereg_mr(srv_mr->mr);
559 ib_dma_unmap_sg(srv_path->s.dev->ib_dev, srv_mr->sgt.sgl,
560 srv_mr->sgt.nents, DMA_BIDIRECTIONAL);
561 sg_free_table(&srv_mr->sgt);
563 kfree(srv_path->mrs);
566 static int map_cont_bufs(struct rtrs_srv_path *srv_path)
568 struct rtrs_srv_sess *srv = srv_path->srv;
569 struct rtrs_path *ss = &srv_path->s;
571 unsigned int chunk_bits;
572 int chunks_per_mr = 1;
574 struct sg_table *sgt;
577 * Here we map queue_depth chunks to MR. Firstly we have to
578 * figure out how many chunks can we map per MR.
580 if (always_invalidate) {
582 * in order to do invalidate for each chunks of memory, we needs
583 * more memory regions.
585 mrs_num = srv->queue_depth;
588 srv_path->s.dev->ib_dev->attrs.max_fast_reg_page_list_len;
589 mrs_num = DIV_ROUND_UP(srv->queue_depth, chunks_per_mr);
590 chunks_per_mr = DIV_ROUND_UP(srv->queue_depth, mrs_num);
593 srv_path->mrs = kcalloc(mrs_num, sizeof(*srv_path->mrs), GFP_KERNEL);
597 for (srv_path->mrs_num = 0; srv_path->mrs_num < mrs_num;
598 srv_path->mrs_num++) {
599 struct rtrs_srv_mr *srv_mr = &srv_path->mrs[srv_path->mrs_num];
600 struct scatterlist *s;
601 int nr, nr_sgt, chunks;
604 chunks = chunks_per_mr * srv_path->mrs_num;
605 if (!always_invalidate)
606 chunks_per_mr = min_t(int, chunks_per_mr,
607 srv->queue_depth - chunks);
609 err = sg_alloc_table(sgt, chunks_per_mr, GFP_KERNEL);
613 for_each_sg(sgt->sgl, s, chunks_per_mr, i)
614 sg_set_page(s, srv->chunks[chunks + i],
617 nr_sgt = ib_dma_map_sg(srv_path->s.dev->ib_dev, sgt->sgl,
618 sgt->nents, DMA_BIDIRECTIONAL);
623 mr = ib_alloc_mr(srv_path->s.dev->ib_pd, IB_MR_TYPE_MEM_REG,
629 nr = ib_map_mr_sg(mr, sgt->sgl, nr_sgt,
630 NULL, max_chunk_size);
632 err = nr < 0 ? nr : -EINVAL;
636 if (always_invalidate) {
637 srv_mr->iu = rtrs_iu_alloc(1,
638 sizeof(struct rtrs_msg_rkey_rsp),
639 GFP_KERNEL, srv_path->s.dev->ib_dev,
640 DMA_TO_DEVICE, rtrs_srv_rdma_done);
643 rtrs_err(ss, "rtrs_iu_alloc(), err: %d\n", err);
647 /* Eventually dma addr for each chunk can be cached */
648 for_each_sg(sgt->sgl, s, nr_sgt, i)
649 srv_path->dma_addr[chunks + i] = sg_dma_address(s);
651 ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
655 chunk_bits = ilog2(srv->queue_depth - 1) + 1;
656 srv_path->mem_bits = (MAX_IMM_PAYL_BITS - chunk_bits);
663 ib_dma_unmap_sg(srv_path->s.dev->ib_dev, sgt->sgl,
664 sgt->nents, DMA_BIDIRECTIONAL);
668 unmap_cont_bufs(srv_path);
673 static void rtrs_srv_hb_err_handler(struct rtrs_con *c)
675 close_path(to_srv_path(c->path));
678 static void rtrs_srv_init_hb(struct rtrs_srv_path *srv_path)
680 rtrs_init_hb(&srv_path->s, &io_comp_cqe,
683 rtrs_srv_hb_err_handler,
687 static void rtrs_srv_start_hb(struct rtrs_srv_path *srv_path)
689 rtrs_start_hb(&srv_path->s);
692 static void rtrs_srv_stop_hb(struct rtrs_srv_path *srv_path)
694 rtrs_stop_hb(&srv_path->s);
697 static void rtrs_srv_info_rsp_done(struct ib_cq *cq, struct ib_wc *wc)
699 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
700 struct rtrs_path *s = con->c.path;
701 struct rtrs_srv_path *srv_path = to_srv_path(s);
704 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
705 rtrs_iu_free(iu, srv_path->s.dev->ib_dev, 1);
707 if (wc->status != IB_WC_SUCCESS) {
708 rtrs_err(s, "Sess info response send failed: %s\n",
709 ib_wc_status_msg(wc->status));
710 close_path(srv_path);
713 WARN_ON(wc->opcode != IB_WC_SEND);
716 static int rtrs_srv_path_up(struct rtrs_srv_path *srv_path)
718 struct rtrs_srv_sess *srv = srv_path->srv;
719 struct rtrs_srv_ctx *ctx = srv->ctx;
722 mutex_lock(&srv->paths_ev_mutex);
723 up = ++srv->paths_up;
725 ret = ctx->ops.link_ev(srv, RTRS_SRV_LINK_EV_CONNECTED, NULL);
726 mutex_unlock(&srv->paths_ev_mutex);
728 /* Mark session as established */
730 srv_path->established = true;
735 static void rtrs_srv_path_down(struct rtrs_srv_path *srv_path)
737 struct rtrs_srv_sess *srv = srv_path->srv;
738 struct rtrs_srv_ctx *ctx = srv->ctx;
740 if (!srv_path->established)
743 srv_path->established = false;
744 mutex_lock(&srv->paths_ev_mutex);
745 WARN_ON(!srv->paths_up);
746 if (--srv->paths_up == 0)
747 ctx->ops.link_ev(srv, RTRS_SRV_LINK_EV_DISCONNECTED, srv->priv);
748 mutex_unlock(&srv->paths_ev_mutex);
751 static bool exist_pathname(struct rtrs_srv_ctx *ctx,
752 const char *pathname, const uuid_t *path_uuid)
754 struct rtrs_srv_sess *srv;
755 struct rtrs_srv_path *srv_path;
758 mutex_lock(&ctx->srv_mutex);
759 list_for_each_entry(srv, &ctx->srv_list, ctx_list) {
760 mutex_lock(&srv->paths_mutex);
762 /* when a client with same uuid and same sessname tried to add a path */
763 if (uuid_equal(&srv->paths_uuid, path_uuid)) {
764 mutex_unlock(&srv->paths_mutex);
768 list_for_each_entry(srv_path, &srv->paths_list, s.entry) {
769 if (strlen(srv_path->s.sessname) == strlen(pathname) &&
770 !strcmp(srv_path->s.sessname, pathname)) {
775 mutex_unlock(&srv->paths_mutex);
779 mutex_unlock(&ctx->srv_mutex);
783 static int post_recv_path(struct rtrs_srv_path *srv_path);
784 static int rtrs_rdma_do_reject(struct rdma_cm_id *cm_id, int errno);
786 static int process_info_req(struct rtrs_srv_con *con,
787 struct rtrs_msg_info_req *msg)
789 struct rtrs_path *s = con->c.path;
790 struct rtrs_srv_path *srv_path = to_srv_path(s);
791 struct ib_send_wr *reg_wr = NULL;
792 struct rtrs_msg_info_rsp *rsp;
793 struct rtrs_iu *tx_iu;
794 struct ib_reg_wr *rwr;
798 err = post_recv_path(srv_path);
800 rtrs_err(s, "post_recv_path(), err: %d\n", err);
804 if (strchr(msg->pathname, '/') || strchr(msg->pathname, '.')) {
805 rtrs_err(s, "pathname cannot contain / and .\n");
809 if (exist_pathname(srv_path->srv->ctx,
810 msg->pathname, &srv_path->srv->paths_uuid)) {
811 rtrs_err(s, "pathname is duplicated: %s\n", msg->pathname);
814 strscpy(srv_path->s.sessname, msg->pathname,
815 sizeof(srv_path->s.sessname));
817 rwr = kcalloc(srv_path->mrs_num, sizeof(*rwr), GFP_KERNEL);
821 tx_sz = sizeof(*rsp);
822 tx_sz += sizeof(rsp->desc[0]) * srv_path->mrs_num;
823 tx_iu = rtrs_iu_alloc(1, tx_sz, GFP_KERNEL, srv_path->s.dev->ib_dev,
824 DMA_TO_DEVICE, rtrs_srv_info_rsp_done);
831 rsp->type = cpu_to_le16(RTRS_MSG_INFO_RSP);
832 rsp->sg_cnt = cpu_to_le16(srv_path->mrs_num);
834 for (mri = 0; mri < srv_path->mrs_num; mri++) {
835 struct ib_mr *mr = srv_path->mrs[mri].mr;
837 rsp->desc[mri].addr = cpu_to_le64(mr->iova);
838 rsp->desc[mri].key = cpu_to_le32(mr->rkey);
839 rsp->desc[mri].len = cpu_to_le32(mr->length);
842 * Fill in reg MR request and chain them *backwards*
844 rwr[mri].wr.next = mri ? &rwr[mri - 1].wr : NULL;
845 rwr[mri].wr.opcode = IB_WR_REG_MR;
846 rwr[mri].wr.wr_cqe = &local_reg_cqe;
847 rwr[mri].wr.num_sge = 0;
848 rwr[mri].wr.send_flags = 0;
850 rwr[mri].key = mr->rkey;
851 rwr[mri].access = (IB_ACCESS_LOCAL_WRITE |
852 IB_ACCESS_REMOTE_WRITE);
853 reg_wr = &rwr[mri].wr;
856 err = rtrs_srv_create_path_files(srv_path);
859 kobject_get(&srv_path->kobj);
860 get_device(&srv_path->srv->dev);
861 err = rtrs_srv_change_state(srv_path, RTRS_SRV_CONNECTED);
863 rtrs_err(s, "rtrs_srv_change_state(), err: %d\n", err);
867 rtrs_srv_start_hb(srv_path);
870 * We do not account number of established connections at the current
871 * moment, we rely on the client, which should send info request when
872 * all connections are successfully established. Thus, simply notify
873 * listener with a proper event if we are the first path.
875 err = rtrs_srv_path_up(srv_path);
877 rtrs_err(s, "rtrs_srv_path_up(), err: %d\n", err);
881 ib_dma_sync_single_for_device(srv_path->s.dev->ib_dev,
883 tx_iu->size, DMA_TO_DEVICE);
885 /* Send info response */
886 err = rtrs_iu_post_send(&con->c, tx_iu, tx_sz, reg_wr);
888 rtrs_err(s, "rtrs_iu_post_send(), err: %d\n", err);
890 rtrs_iu_free(tx_iu, srv_path->s.dev->ib_dev, 1);
898 static void rtrs_srv_info_req_done(struct ib_cq *cq, struct ib_wc *wc)
900 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
901 struct rtrs_path *s = con->c.path;
902 struct rtrs_srv_path *srv_path = to_srv_path(s);
903 struct rtrs_msg_info_req *msg;
909 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe);
910 if (wc->status != IB_WC_SUCCESS) {
911 rtrs_err(s, "Sess info request receive failed: %s\n",
912 ib_wc_status_msg(wc->status));
915 WARN_ON(wc->opcode != IB_WC_RECV);
917 if (wc->byte_len < sizeof(*msg)) {
918 rtrs_err(s, "Sess info request is malformed: size %d\n",
922 ib_dma_sync_single_for_cpu(srv_path->s.dev->ib_dev, iu->dma_addr,
923 iu->size, DMA_FROM_DEVICE);
925 if (le16_to_cpu(msg->type) != RTRS_MSG_INFO_REQ) {
926 rtrs_err(s, "Sess info request is malformed: type %d\n",
927 le16_to_cpu(msg->type));
930 err = process_info_req(con, msg);
935 rtrs_iu_free(iu, srv_path->s.dev->ib_dev, 1);
938 close_path(srv_path);
942 static int post_recv_info_req(struct rtrs_srv_con *con)
944 struct rtrs_path *s = con->c.path;
945 struct rtrs_srv_path *srv_path = to_srv_path(s);
946 struct rtrs_iu *rx_iu;
949 rx_iu = rtrs_iu_alloc(1, sizeof(struct rtrs_msg_info_req),
950 GFP_KERNEL, srv_path->s.dev->ib_dev,
951 DMA_FROM_DEVICE, rtrs_srv_info_req_done);
954 /* Prepare for getting info response */
955 err = rtrs_iu_post_recv(&con->c, rx_iu);
957 rtrs_err(s, "rtrs_iu_post_recv(), err: %d\n", err);
958 rtrs_iu_free(rx_iu, srv_path->s.dev->ib_dev, 1);
965 static int post_recv_io(struct rtrs_srv_con *con, size_t q_size)
969 for (i = 0; i < q_size; i++) {
970 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe);
978 static int post_recv_path(struct rtrs_srv_path *srv_path)
980 struct rtrs_srv_sess *srv = srv_path->srv;
981 struct rtrs_path *s = &srv_path->s;
985 for (cid = 0; cid < srv_path->s.con_num; cid++) {
987 q_size = SERVICE_CON_QUEUE_DEPTH;
989 q_size = srv->queue_depth;
991 err = post_recv_io(to_srv_con(srv_path->s.con[cid]), q_size);
993 rtrs_err(s, "post_recv_io(), err: %d\n", err);
1001 static void process_read(struct rtrs_srv_con *con,
1002 struct rtrs_msg_rdma_read *msg,
1003 u32 buf_id, u32 off)
1005 struct rtrs_path *s = con->c.path;
1006 struct rtrs_srv_path *srv_path = to_srv_path(s);
1007 struct rtrs_srv_sess *srv = srv_path->srv;
1008 struct rtrs_srv_ctx *ctx = srv->ctx;
1009 struct rtrs_srv_op *id;
1011 size_t usr_len, data_len;
1015 if (srv_path->state != RTRS_SRV_CONNECTED) {
1017 "Processing read request failed, session is disconnected, sess state %s\n",
1018 rtrs_srv_state_str(srv_path->state));
1021 if (msg->sg_cnt != 1 && msg->sg_cnt != 0) {
1023 "Processing read request failed, invalid message\n");
1026 rtrs_srv_get_ops_ids(srv_path);
1027 rtrs_srv_update_rdma_stats(srv_path->stats, off, READ);
1028 id = srv_path->ops_ids[buf_id];
1031 id->msg_id = buf_id;
1033 usr_len = le16_to_cpu(msg->usr_len);
1034 data_len = off - usr_len;
1035 data = page_address(srv->chunks[buf_id]);
1036 ret = ctx->ops.rdma_ev(srv->priv, id, data, data_len,
1037 data + data_len, usr_len);
1041 "Processing read request failed, user module cb reported for msg_id %d, err: %d\n",
1049 ret = send_io_resp_imm(con, id, ret);
1052 "Sending err msg for failed RDMA-Write-Req failed, msg_id %d, err: %d\n",
1054 close_path(srv_path);
1056 rtrs_srv_put_ops_ids(srv_path);
1059 static void process_write(struct rtrs_srv_con *con,
1060 struct rtrs_msg_rdma_write *req,
1061 u32 buf_id, u32 off)
1063 struct rtrs_path *s = con->c.path;
1064 struct rtrs_srv_path *srv_path = to_srv_path(s);
1065 struct rtrs_srv_sess *srv = srv_path->srv;
1066 struct rtrs_srv_ctx *ctx = srv->ctx;
1067 struct rtrs_srv_op *id;
1069 size_t data_len, usr_len;
1073 if (srv_path->state != RTRS_SRV_CONNECTED) {
1075 "Processing write request failed, session is disconnected, sess state %s\n",
1076 rtrs_srv_state_str(srv_path->state));
1079 rtrs_srv_get_ops_ids(srv_path);
1080 rtrs_srv_update_rdma_stats(srv_path->stats, off, WRITE);
1081 id = srv_path->ops_ids[buf_id];
1084 id->msg_id = buf_id;
1086 usr_len = le16_to_cpu(req->usr_len);
1087 data_len = off - usr_len;
1088 data = page_address(srv->chunks[buf_id]);
1089 ret = ctx->ops.rdma_ev(srv->priv, id, data, data_len,
1090 data + data_len, usr_len);
1093 "Processing write request failed, user module callback reports err: %d\n",
1101 ret = send_io_resp_imm(con, id, ret);
1104 "Processing write request failed, sending I/O response failed, msg_id %d, err: %d\n",
1106 close_path(srv_path);
1108 rtrs_srv_put_ops_ids(srv_path);
1111 static void process_io_req(struct rtrs_srv_con *con, void *msg,
1114 struct rtrs_path *s = con->c.path;
1115 struct rtrs_srv_path *srv_path = to_srv_path(s);
1116 struct rtrs_msg_rdma_hdr *hdr;
1119 ib_dma_sync_single_for_cpu(srv_path->s.dev->ib_dev,
1120 srv_path->dma_addr[id],
1121 max_chunk_size, DMA_BIDIRECTIONAL);
1123 type = le16_to_cpu(hdr->type);
1126 case RTRS_MSG_WRITE:
1127 process_write(con, msg, id, off);
1130 process_read(con, msg, id, off);
1134 "Processing I/O request failed, unknown message type received: 0x%02x\n",
1142 close_path(srv_path);
1145 static void rtrs_srv_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1147 struct rtrs_srv_mr *mr =
1148 container_of(wc->wr_cqe, typeof(*mr), inv_cqe);
1149 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
1150 struct rtrs_path *s = con->c.path;
1151 struct rtrs_srv_path *srv_path = to_srv_path(s);
1152 struct rtrs_srv_sess *srv = srv_path->srv;
1156 if (wc->status != IB_WC_SUCCESS) {
1157 rtrs_err(s, "Failed IB_WR_LOCAL_INV: %s\n",
1158 ib_wc_status_msg(wc->status));
1159 close_path(srv_path);
1161 msg_id = mr->msg_id;
1163 data = page_address(srv->chunks[msg_id]) + off;
1164 process_io_req(con, data, msg_id, off);
1167 static int rtrs_srv_inv_rkey(struct rtrs_srv_con *con,
1168 struct rtrs_srv_mr *mr)
1170 struct ib_send_wr wr = {
1171 .opcode = IB_WR_LOCAL_INV,
1172 .wr_cqe = &mr->inv_cqe,
1173 .send_flags = IB_SEND_SIGNALED,
1174 .ex.invalidate_rkey = mr->mr->rkey,
1176 mr->inv_cqe.done = rtrs_srv_inv_rkey_done;
1178 return ib_post_send(con->c.qp, &wr, NULL);
1181 static void rtrs_rdma_process_wr_wait_list(struct rtrs_srv_con *con)
1183 spin_lock(&con->rsp_wr_wait_lock);
1184 while (!list_empty(&con->rsp_wr_wait_list)) {
1185 struct rtrs_srv_op *id;
1188 id = list_entry(con->rsp_wr_wait_list.next,
1189 struct rtrs_srv_op, wait_list);
1190 list_del(&id->wait_list);
1192 spin_unlock(&con->rsp_wr_wait_lock);
1193 ret = rtrs_srv_resp_rdma(id, id->status);
1194 spin_lock(&con->rsp_wr_wait_lock);
1197 list_add(&id->wait_list, &con->rsp_wr_wait_list);
1201 spin_unlock(&con->rsp_wr_wait_lock);
1204 static void rtrs_srv_rdma_done(struct ib_cq *cq, struct ib_wc *wc)
1206 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context);
1207 struct rtrs_path *s = con->c.path;
1208 struct rtrs_srv_path *srv_path = to_srv_path(s);
1209 struct rtrs_srv_sess *srv = srv_path->srv;
1210 u32 imm_type, imm_payload;
1213 if (wc->status != IB_WC_SUCCESS) {
1214 if (wc->status != IB_WC_WR_FLUSH_ERR) {
1216 "%s (wr_cqe: %p, type: %d, vendor_err: 0x%x, len: %u)\n",
1217 ib_wc_status_msg(wc->status), wc->wr_cqe,
1218 wc->opcode, wc->vendor_err, wc->byte_len);
1219 close_path(srv_path);
1224 switch (wc->opcode) {
1225 case IB_WC_RECV_RDMA_WITH_IMM:
1227 * post_recv() RDMA write completions of IO reqs (read/write)
1230 if (WARN_ON(wc->wr_cqe != &io_comp_cqe))
1232 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe);
1234 rtrs_err(s, "rtrs_post_recv(), err: %d\n", err);
1235 close_path(srv_path);
1238 rtrs_from_imm(be32_to_cpu(wc->ex.imm_data),
1239 &imm_type, &imm_payload);
1240 if (imm_type == RTRS_IO_REQ_IMM) {
1244 msg_id = imm_payload >> srv_path->mem_bits;
1245 off = imm_payload & ((1 << srv_path->mem_bits) - 1);
1246 if (msg_id >= srv->queue_depth || off >= max_chunk_size) {
1247 rtrs_err(s, "Wrong msg_id %u, off %u\n",
1249 close_path(srv_path);
1252 if (always_invalidate) {
1253 struct rtrs_srv_mr *mr = &srv_path->mrs[msg_id];
1256 mr->msg_id = msg_id;
1257 err = rtrs_srv_inv_rkey(con, mr);
1259 rtrs_err(s, "rtrs_post_recv(), err: %d\n",
1261 close_path(srv_path);
1265 data = page_address(srv->chunks[msg_id]) + off;
1266 process_io_req(con, data, msg_id, off);
1268 } else if (imm_type == RTRS_HB_MSG_IMM) {
1269 WARN_ON(con->c.cid);
1270 rtrs_send_hb_ack(&srv_path->s);
1271 } else if (imm_type == RTRS_HB_ACK_IMM) {
1272 WARN_ON(con->c.cid);
1273 srv_path->s.hb_missed_cnt = 0;
1275 rtrs_wrn(s, "Unknown IMM type %u\n", imm_type);
1278 case IB_WC_RDMA_WRITE:
1281 * post_send() RDMA write completions of IO reqs (read/write)
1284 atomic_add(s->signal_interval, &con->c.sq_wr_avail);
1286 if (!list_empty_careful(&con->rsp_wr_wait_list))
1287 rtrs_rdma_process_wr_wait_list(con);
1291 rtrs_wrn(s, "Unexpected WC type: %d\n", wc->opcode);
1297 * rtrs_srv_get_path_name() - Get rtrs_srv peer hostname.
1299 * @pathname: Pathname buffer
1300 * @len: Length of sessname buffer
1302 int rtrs_srv_get_path_name(struct rtrs_srv_sess *srv, char *pathname,
1305 struct rtrs_srv_path *srv_path;
1306 int err = -ENOTCONN;
1308 mutex_lock(&srv->paths_mutex);
1309 list_for_each_entry(srv_path, &srv->paths_list, s.entry) {
1310 if (srv_path->state != RTRS_SRV_CONNECTED)
1312 strscpy(pathname, srv_path->s.sessname,
1313 min_t(size_t, sizeof(srv_path->s.sessname), len));
1317 mutex_unlock(&srv->paths_mutex);
1321 EXPORT_SYMBOL(rtrs_srv_get_path_name);
1324 * rtrs_srv_get_queue_depth() - Get rtrs_srv qdepth.
1327 int rtrs_srv_get_queue_depth(struct rtrs_srv_sess *srv)
1329 return srv->queue_depth;
1331 EXPORT_SYMBOL(rtrs_srv_get_queue_depth);
1333 static int find_next_bit_ring(struct rtrs_srv_path *srv_path)
1335 struct ib_device *ib_dev = srv_path->s.dev->ib_dev;
1338 v = cpumask_next(srv_path->cur_cq_vector, &cq_affinity_mask);
1339 if (v >= nr_cpu_ids || v >= ib_dev->num_comp_vectors)
1340 v = cpumask_first(&cq_affinity_mask);
1344 static int rtrs_srv_get_next_cq_vector(struct rtrs_srv_path *srv_path)
1346 srv_path->cur_cq_vector = find_next_bit_ring(srv_path);
1348 return srv_path->cur_cq_vector;
1351 static void rtrs_srv_dev_release(struct device *dev)
1353 struct rtrs_srv_sess *srv = container_of(dev, struct rtrs_srv_sess,
1359 static void free_srv(struct rtrs_srv_sess *srv)
1363 WARN_ON(refcount_read(&srv->refcount));
1364 for (i = 0; i < srv->queue_depth; i++)
1365 __free_pages(srv->chunks[i], get_order(max_chunk_size));
1367 mutex_destroy(&srv->paths_mutex);
1368 mutex_destroy(&srv->paths_ev_mutex);
1369 /* last put to release the srv structure */
1370 put_device(&srv->dev);
1373 static struct rtrs_srv_sess *get_or_create_srv(struct rtrs_srv_ctx *ctx,
1374 const uuid_t *paths_uuid,
1377 struct rtrs_srv_sess *srv;
1380 mutex_lock(&ctx->srv_mutex);
1381 list_for_each_entry(srv, &ctx->srv_list, ctx_list) {
1382 if (uuid_equal(&srv->paths_uuid, paths_uuid) &&
1383 refcount_inc_not_zero(&srv->refcount)) {
1384 mutex_unlock(&ctx->srv_mutex);
1388 mutex_unlock(&ctx->srv_mutex);
1390 * If this request is not the first connection request from the
1391 * client for this session then fail and return error.
1394 pr_err_ratelimited("Error: Not the first connection request for this session\n");
1395 return ERR_PTR(-ENXIO);
1398 /* need to allocate a new srv */
1399 srv = kzalloc(sizeof(*srv), GFP_KERNEL);
1401 return ERR_PTR(-ENOMEM);
1403 INIT_LIST_HEAD(&srv->paths_list);
1404 mutex_init(&srv->paths_mutex);
1405 mutex_init(&srv->paths_ev_mutex);
1406 uuid_copy(&srv->paths_uuid, paths_uuid);
1407 srv->queue_depth = sess_queue_depth;
1409 device_initialize(&srv->dev);
1410 srv->dev.release = rtrs_srv_dev_release;
1412 srv->chunks = kcalloc(srv->queue_depth, sizeof(*srv->chunks),
1417 for (i = 0; i < srv->queue_depth; i++) {
1418 srv->chunks[i] = alloc_pages(GFP_KERNEL,
1419 get_order(max_chunk_size));
1420 if (!srv->chunks[i])
1421 goto err_free_chunks;
1423 refcount_set(&srv->refcount, 1);
1424 mutex_lock(&ctx->srv_mutex);
1425 list_add(&srv->ctx_list, &ctx->srv_list);
1426 mutex_unlock(&ctx->srv_mutex);
1432 __free_pages(srv->chunks[i], get_order(max_chunk_size));
1437 return ERR_PTR(-ENOMEM);
1440 static void put_srv(struct rtrs_srv_sess *srv)
1442 if (refcount_dec_and_test(&srv->refcount)) {
1443 struct rtrs_srv_ctx *ctx = srv->ctx;
1445 WARN_ON(srv->dev.kobj.state_in_sysfs);
1447 mutex_lock(&ctx->srv_mutex);
1448 list_del(&srv->ctx_list);
1449 mutex_unlock(&ctx->srv_mutex);
1454 static void __add_path_to_srv(struct rtrs_srv_sess *srv,
1455 struct rtrs_srv_path *srv_path)
1457 list_add_tail(&srv_path->s.entry, &srv->paths_list);
1459 WARN_ON(srv->paths_num >= MAX_PATHS_NUM);
1462 static void del_path_from_srv(struct rtrs_srv_path *srv_path)
1464 struct rtrs_srv_sess *srv = srv_path->srv;
1469 mutex_lock(&srv->paths_mutex);
1470 list_del(&srv_path->s.entry);
1471 WARN_ON(!srv->paths_num);
1473 mutex_unlock(&srv->paths_mutex);
1476 /* return true if addresses are the same, error other wise */
1477 static int sockaddr_cmp(const struct sockaddr *a, const struct sockaddr *b)
1479 switch (a->sa_family) {
1481 return memcmp(&((struct sockaddr_ib *)a)->sib_addr,
1482 &((struct sockaddr_ib *)b)->sib_addr,
1483 sizeof(struct ib_addr)) &&
1484 (b->sa_family == AF_IB);
1486 return memcmp(&((struct sockaddr_in *)a)->sin_addr,
1487 &((struct sockaddr_in *)b)->sin_addr,
1488 sizeof(struct in_addr)) &&
1489 (b->sa_family == AF_INET);
1491 return memcmp(&((struct sockaddr_in6 *)a)->sin6_addr,
1492 &((struct sockaddr_in6 *)b)->sin6_addr,
1493 sizeof(struct in6_addr)) &&
1494 (b->sa_family == AF_INET6);
1500 static bool __is_path_w_addr_exists(struct rtrs_srv_sess *srv,
1501 struct rdma_addr *addr)
1503 struct rtrs_srv_path *srv_path;
1505 list_for_each_entry(srv_path, &srv->paths_list, s.entry)
1506 if (!sockaddr_cmp((struct sockaddr *)&srv_path->s.dst_addr,
1507 (struct sockaddr *)&addr->dst_addr) &&
1508 !sockaddr_cmp((struct sockaddr *)&srv_path->s.src_addr,
1509 (struct sockaddr *)&addr->src_addr))
1515 static void free_path(struct rtrs_srv_path *srv_path)
1517 if (srv_path->kobj.state_in_sysfs) {
1518 kobject_del(&srv_path->kobj);
1519 kobject_put(&srv_path->kobj);
1521 free_percpu(srv_path->stats->rdma_stats);
1522 kfree(srv_path->stats);
1527 static void rtrs_srv_close_work(struct work_struct *work)
1529 struct rtrs_srv_path *srv_path;
1530 struct rtrs_srv_con *con;
1533 srv_path = container_of(work, typeof(*srv_path), close_work);
1535 rtrs_srv_stop_hb(srv_path);
1537 for (i = 0; i < srv_path->s.con_num; i++) {
1538 if (!srv_path->s.con[i])
1540 con = to_srv_con(srv_path->s.con[i]);
1541 rdma_disconnect(con->c.cm_id);
1542 ib_drain_qp(con->c.qp);
1546 * Degrade ref count to the usual model with a single shared
1549 percpu_ref_kill(&srv_path->ids_inflight_ref);
1551 /* Wait for all completion */
1552 wait_for_completion(&srv_path->complete_done);
1554 rtrs_srv_destroy_path_files(srv_path);
1556 /* Notify upper layer if we are the last path */
1557 rtrs_srv_path_down(srv_path);
1559 unmap_cont_bufs(srv_path);
1560 rtrs_srv_free_ops_ids(srv_path);
1562 for (i = 0; i < srv_path->s.con_num; i++) {
1563 if (!srv_path->s.con[i])
1565 con = to_srv_con(srv_path->s.con[i]);
1566 rtrs_cq_qp_destroy(&con->c);
1567 rdma_destroy_id(con->c.cm_id);
1570 rtrs_ib_dev_put(srv_path->s.dev);
1572 del_path_from_srv(srv_path);
1573 put_srv(srv_path->srv);
1574 srv_path->srv = NULL;
1575 rtrs_srv_change_state(srv_path, RTRS_SRV_CLOSED);
1577 kfree(srv_path->dma_addr);
1578 kfree(srv_path->s.con);
1579 free_path(srv_path);
1582 static int rtrs_rdma_do_accept(struct rtrs_srv_path *srv_path,
1583 struct rdma_cm_id *cm_id)
1585 struct rtrs_srv_sess *srv = srv_path->srv;
1586 struct rtrs_msg_conn_rsp msg;
1587 struct rdma_conn_param param;
1590 param = (struct rdma_conn_param) {
1591 .rnr_retry_count = 7,
1592 .private_data = &msg,
1593 .private_data_len = sizeof(msg),
1596 msg = (struct rtrs_msg_conn_rsp) {
1597 .magic = cpu_to_le16(RTRS_MAGIC),
1598 .version = cpu_to_le16(RTRS_PROTO_VER),
1599 .queue_depth = cpu_to_le16(srv->queue_depth),
1600 .max_io_size = cpu_to_le32(max_chunk_size - MAX_HDR_SIZE),
1601 .max_hdr_size = cpu_to_le32(MAX_HDR_SIZE),
1604 if (always_invalidate)
1605 msg.flags = cpu_to_le32(RTRS_MSG_NEW_RKEY_F);
1607 err = rdma_accept(cm_id, ¶m);
1609 pr_err("rdma_accept(), err: %d\n", err);
1614 static int rtrs_rdma_do_reject(struct rdma_cm_id *cm_id, int errno)
1616 struct rtrs_msg_conn_rsp msg;
1619 msg = (struct rtrs_msg_conn_rsp) {
1620 .magic = cpu_to_le16(RTRS_MAGIC),
1621 .version = cpu_to_le16(RTRS_PROTO_VER),
1622 .errno = cpu_to_le16(errno),
1625 err = rdma_reject(cm_id, &msg, sizeof(msg), IB_CM_REJ_CONSUMER_DEFINED);
1627 pr_err("rdma_reject(), err: %d\n", err);
1629 /* Bounce errno back */
1633 static struct rtrs_srv_path *
1634 __find_path(struct rtrs_srv_sess *srv, const uuid_t *sess_uuid)
1636 struct rtrs_srv_path *srv_path;
1638 list_for_each_entry(srv_path, &srv->paths_list, s.entry) {
1639 if (uuid_equal(&srv_path->s.uuid, sess_uuid))
1646 static int create_con(struct rtrs_srv_path *srv_path,
1647 struct rdma_cm_id *cm_id,
1650 struct rtrs_srv_sess *srv = srv_path->srv;
1651 struct rtrs_path *s = &srv_path->s;
1652 struct rtrs_srv_con *con;
1654 u32 cq_num, max_send_wr, max_recv_wr, wr_limit;
1657 con = kzalloc(sizeof(*con), GFP_KERNEL);
1663 spin_lock_init(&con->rsp_wr_wait_lock);
1664 INIT_LIST_HEAD(&con->rsp_wr_wait_list);
1665 con->c.cm_id = cm_id;
1666 con->c.path = &srv_path->s;
1668 atomic_set(&con->c.wr_cnt, 1);
1669 wr_limit = srv_path->s.dev->ib_dev->attrs.max_qp_wr;
1671 if (con->c.cid == 0) {
1673 * All receive and all send (each requiring invalidate)
1674 * + 2 for drain and heartbeat
1676 max_send_wr = min_t(int, wr_limit,
1677 SERVICE_CON_QUEUE_DEPTH * 2 + 2);
1678 max_recv_wr = max_send_wr;
1679 s->signal_interval = min_not_zero(srv->queue_depth,
1680 (size_t)SERVICE_CON_QUEUE_DEPTH);
1682 /* when always_invlaidate enalbed, we need linv+rinv+mr+imm */
1683 if (always_invalidate)
1685 min_t(int, wr_limit,
1686 srv->queue_depth * (1 + 4) + 1);
1689 min_t(int, wr_limit,
1690 srv->queue_depth * (1 + 2) + 1);
1692 max_recv_wr = srv->queue_depth + 1;
1694 cq_num = max_send_wr + max_recv_wr;
1695 atomic_set(&con->c.sq_wr_avail, max_send_wr);
1696 cq_vector = rtrs_srv_get_next_cq_vector(srv_path);
1698 /* TODO: SOFTIRQ can be faster, but be careful with softirq context */
1699 err = rtrs_cq_qp_create(&srv_path->s, &con->c, 1, cq_vector, cq_num,
1700 max_send_wr, max_recv_wr,
1703 rtrs_err(s, "rtrs_cq_qp_create(), err: %d\n", err);
1706 if (con->c.cid == 0) {
1707 err = post_recv_info_req(con);
1711 WARN_ON(srv_path->s.con[cid]);
1712 srv_path->s.con[cid] = &con->c;
1715 * Change context from server to current connection. The other
1716 * way is to use cm_id->qp->qp_context, which does not work on OFED.
1718 cm_id->context = &con->c;
1723 rtrs_cq_qp_destroy(&con->c);
1731 static struct rtrs_srv_path *__alloc_path(struct rtrs_srv_sess *srv,
1732 struct rdma_cm_id *cm_id,
1733 unsigned int con_num,
1734 unsigned int recon_cnt,
1737 struct rtrs_srv_path *srv_path;
1740 struct rtrs_addr path;
1742 if (srv->paths_num >= MAX_PATHS_NUM) {
1746 if (__is_path_w_addr_exists(srv, &cm_id->route.addr)) {
1748 pr_err("Path with same addr exists\n");
1751 srv_path = kzalloc(sizeof(*srv_path), GFP_KERNEL);
1755 srv_path->stats = kzalloc(sizeof(*srv_path->stats), GFP_KERNEL);
1756 if (!srv_path->stats)
1759 srv_path->stats->rdma_stats = alloc_percpu(struct rtrs_srv_stats_rdma_stats);
1760 if (!srv_path->stats->rdma_stats)
1761 goto err_free_stats;
1763 srv_path->stats->srv_path = srv_path;
1765 srv_path->dma_addr = kcalloc(srv->queue_depth,
1766 sizeof(*srv_path->dma_addr),
1768 if (!srv_path->dma_addr)
1769 goto err_free_percpu;
1771 srv_path->s.con = kcalloc(con_num, sizeof(*srv_path->s.con),
1773 if (!srv_path->s.con)
1774 goto err_free_dma_addr;
1776 srv_path->state = RTRS_SRV_CONNECTING;
1777 srv_path->srv = srv;
1778 srv_path->cur_cq_vector = -1;
1779 srv_path->s.dst_addr = cm_id->route.addr.dst_addr;
1780 srv_path->s.src_addr = cm_id->route.addr.src_addr;
1782 /* temporary until receiving session-name from client */
1783 path.src = &srv_path->s.src_addr;
1784 path.dst = &srv_path->s.dst_addr;
1785 rtrs_addr_to_str(&path, str, sizeof(str));
1786 strscpy(srv_path->s.sessname, str, sizeof(srv_path->s.sessname));
1788 srv_path->s.con_num = con_num;
1789 srv_path->s.irq_con_num = con_num;
1790 srv_path->s.recon_cnt = recon_cnt;
1791 uuid_copy(&srv_path->s.uuid, uuid);
1792 spin_lock_init(&srv_path->state_lock);
1793 INIT_WORK(&srv_path->close_work, rtrs_srv_close_work);
1794 rtrs_srv_init_hb(srv_path);
1796 srv_path->s.dev = rtrs_ib_dev_find_or_add(cm_id->device, &dev_pd);
1797 if (!srv_path->s.dev) {
1801 err = map_cont_bufs(srv_path);
1805 err = rtrs_srv_alloc_ops_ids(srv_path);
1807 goto err_unmap_bufs;
1809 __add_path_to_srv(srv, srv_path);
1814 unmap_cont_bufs(srv_path);
1816 rtrs_ib_dev_put(srv_path->s.dev);
1818 kfree(srv_path->s.con);
1820 kfree(srv_path->dma_addr);
1822 free_percpu(srv_path->stats->rdma_stats);
1824 kfree(srv_path->stats);
1828 return ERR_PTR(err);
1831 static int rtrs_rdma_connect(struct rdma_cm_id *cm_id,
1832 const struct rtrs_msg_conn_req *msg,
1835 struct rtrs_srv_ctx *ctx = cm_id->context;
1836 struct rtrs_srv_path *srv_path;
1837 struct rtrs_srv_sess *srv;
1839 u16 version, con_num, cid;
1841 int err = -ECONNRESET;
1843 if (len < sizeof(*msg)) {
1844 pr_err("Invalid RTRS connection request\n");
1847 if (le16_to_cpu(msg->magic) != RTRS_MAGIC) {
1848 pr_err("Invalid RTRS magic\n");
1851 version = le16_to_cpu(msg->version);
1852 if (version >> 8 != RTRS_PROTO_VER_MAJOR) {
1853 pr_err("Unsupported major RTRS version: %d, expected %d\n",
1854 version >> 8, RTRS_PROTO_VER_MAJOR);
1857 con_num = le16_to_cpu(msg->cid_num);
1858 if (con_num > 4096) {
1860 pr_err("Too many connections requested: %d\n", con_num);
1863 cid = le16_to_cpu(msg->cid);
1864 if (cid >= con_num) {
1866 pr_err("Incorrect cid: %d >= %d\n", cid, con_num);
1869 recon_cnt = le16_to_cpu(msg->recon_cnt);
1870 srv = get_or_create_srv(ctx, &msg->paths_uuid, msg->first_conn);
1873 pr_err("get_or_create_srv(), error %d\n", err);
1876 mutex_lock(&srv->paths_mutex);
1877 srv_path = __find_path(srv, &msg->sess_uuid);
1879 struct rtrs_path *s = &srv_path->s;
1881 /* Session already holds a reference */
1884 if (srv_path->state != RTRS_SRV_CONNECTING) {
1885 rtrs_err(s, "Session in wrong state: %s\n",
1886 rtrs_srv_state_str(srv_path->state));
1887 mutex_unlock(&srv->paths_mutex);
1893 if (con_num != s->con_num || cid >= s->con_num) {
1894 rtrs_err(s, "Incorrect request: %d, %d\n",
1896 mutex_unlock(&srv->paths_mutex);
1900 rtrs_err(s, "Connection already exists: %d\n",
1902 mutex_unlock(&srv->paths_mutex);
1906 srv_path = __alloc_path(srv, cm_id, con_num, recon_cnt,
1908 if (IS_ERR(srv_path)) {
1909 mutex_unlock(&srv->paths_mutex);
1911 err = PTR_ERR(srv_path);
1912 pr_err("RTRS server session allocation failed: %d\n", err);
1916 err = create_con(srv_path, cm_id, cid);
1918 rtrs_err((&srv_path->s), "create_con(), error %d\n", err);
1919 rtrs_rdma_do_reject(cm_id, err);
1921 * Since session has other connections we follow normal way
1922 * through workqueue, but still return an error to tell cma.c
1923 * to call rdma_destroy_id() for current connection.
1925 goto close_and_return_err;
1927 err = rtrs_rdma_do_accept(srv_path, cm_id);
1929 rtrs_err((&srv_path->s), "rtrs_rdma_do_accept(), error %d\n", err);
1930 rtrs_rdma_do_reject(cm_id, err);
1932 * Since current connection was successfully added to the
1933 * session we follow normal way through workqueue to close the
1934 * session, thus return 0 to tell cma.c we call
1935 * rdma_destroy_id() ourselves.
1938 goto close_and_return_err;
1940 mutex_unlock(&srv->paths_mutex);
1945 return rtrs_rdma_do_reject(cm_id, err);
1947 close_and_return_err:
1948 mutex_unlock(&srv->paths_mutex);
1949 close_path(srv_path);
1954 static int rtrs_srv_rdma_cm_handler(struct rdma_cm_id *cm_id,
1955 struct rdma_cm_event *ev)
1957 struct rtrs_srv_path *srv_path = NULL;
1958 struct rtrs_path *s = NULL;
1959 struct rtrs_con *c = NULL;
1961 if (ev->event == RDMA_CM_EVENT_CONNECT_REQUEST)
1963 * In case of error cma.c will destroy cm_id,
1964 * see cma_process_remove()
1966 return rtrs_rdma_connect(cm_id, ev->param.conn.private_data,
1967 ev->param.conn.private_data_len);
1971 srv_path = to_srv_path(s);
1973 switch (ev->event) {
1974 case RDMA_CM_EVENT_ESTABLISHED:
1977 case RDMA_CM_EVENT_REJECTED:
1978 case RDMA_CM_EVENT_CONNECT_ERROR:
1979 case RDMA_CM_EVENT_UNREACHABLE:
1980 rtrs_err(s, "CM error (CM event: %s, err: %d)\n",
1981 rdma_event_msg(ev->event), ev->status);
1983 case RDMA_CM_EVENT_DISCONNECTED:
1984 case RDMA_CM_EVENT_ADDR_CHANGE:
1985 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1986 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1987 close_path(srv_path);
1990 pr_err("Ignoring unexpected CM event %s, err %d\n",
1991 rdma_event_msg(ev->event), ev->status);
1998 static struct rdma_cm_id *rtrs_srv_cm_init(struct rtrs_srv_ctx *ctx,
1999 struct sockaddr *addr,
2000 enum rdma_ucm_port_space ps)
2002 struct rdma_cm_id *cm_id;
2005 cm_id = rdma_create_id(&init_net, rtrs_srv_rdma_cm_handler,
2006 ctx, ps, IB_QPT_RC);
2007 if (IS_ERR(cm_id)) {
2008 ret = PTR_ERR(cm_id);
2009 pr_err("Creating id for RDMA connection failed, err: %d\n",
2013 ret = rdma_bind_addr(cm_id, addr);
2015 pr_err("Binding RDMA address failed, err: %d\n", ret);
2018 ret = rdma_listen(cm_id, 64);
2020 pr_err("Listening on RDMA connection failed, err: %d\n",
2028 rdma_destroy_id(cm_id);
2031 return ERR_PTR(ret);
2034 static int rtrs_srv_rdma_init(struct rtrs_srv_ctx *ctx, u16 port)
2036 struct sockaddr_in6 sin = {
2037 .sin6_family = AF_INET6,
2038 .sin6_addr = IN6ADDR_ANY_INIT,
2039 .sin6_port = htons(port),
2041 struct sockaddr_ib sib = {
2042 .sib_family = AF_IB,
2043 .sib_sid = cpu_to_be64(RDMA_IB_IP_PS_IB | port),
2044 .sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL),
2045 .sib_pkey = cpu_to_be16(0xffff),
2047 struct rdma_cm_id *cm_ip, *cm_ib;
2051 * We accept both IPoIB and IB connections, so we need to keep
2052 * two cm id's, one for each socket type and port space.
2053 * If the cm initialization of one of the id's fails, we abort
2056 cm_ip = rtrs_srv_cm_init(ctx, (struct sockaddr *)&sin, RDMA_PS_TCP);
2058 return PTR_ERR(cm_ip);
2060 cm_ib = rtrs_srv_cm_init(ctx, (struct sockaddr *)&sib, RDMA_PS_IB);
2061 if (IS_ERR(cm_ib)) {
2062 ret = PTR_ERR(cm_ib);
2066 ctx->cm_id_ip = cm_ip;
2067 ctx->cm_id_ib = cm_ib;
2072 rdma_destroy_id(cm_ip);
2077 static struct rtrs_srv_ctx *alloc_srv_ctx(struct rtrs_srv_ops *ops)
2079 struct rtrs_srv_ctx *ctx;
2081 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2086 mutex_init(&ctx->srv_mutex);
2087 INIT_LIST_HEAD(&ctx->srv_list);
2092 static void free_srv_ctx(struct rtrs_srv_ctx *ctx)
2094 WARN_ON(!list_empty(&ctx->srv_list));
2095 mutex_destroy(&ctx->srv_mutex);
2099 static int rtrs_srv_add_one(struct ib_device *device)
2101 struct rtrs_srv_ctx *ctx;
2104 mutex_lock(&ib_ctx.ib_dev_mutex);
2105 if (ib_ctx.ib_dev_count)
2109 * Since our CM IDs are NOT bound to any ib device we will create them
2112 ctx = ib_ctx.srv_ctx;
2113 ret = rtrs_srv_rdma_init(ctx, ib_ctx.port);
2116 * We errored out here.
2117 * According to the ib code, if we encounter an error here then the
2118 * error code is ignored, and no more calls to our ops are made.
2120 pr_err("Failed to initialize RDMA connection");
2126 * Keep a track on the number of ib devices added
2128 ib_ctx.ib_dev_count++;
2131 mutex_unlock(&ib_ctx.ib_dev_mutex);
2135 static void rtrs_srv_remove_one(struct ib_device *device, void *client_data)
2137 struct rtrs_srv_ctx *ctx;
2139 mutex_lock(&ib_ctx.ib_dev_mutex);
2140 ib_ctx.ib_dev_count--;
2142 if (ib_ctx.ib_dev_count)
2146 * Since our CM IDs are NOT bound to any ib device we will remove them
2147 * only once, when the last device is removed
2149 ctx = ib_ctx.srv_ctx;
2150 rdma_destroy_id(ctx->cm_id_ip);
2151 rdma_destroy_id(ctx->cm_id_ib);
2154 mutex_unlock(&ib_ctx.ib_dev_mutex);
2157 static struct ib_client rtrs_srv_client = {
2158 .name = "rtrs_server",
2159 .add = rtrs_srv_add_one,
2160 .remove = rtrs_srv_remove_one
2164 * rtrs_srv_open() - open RTRS server context
2165 * @ops: callback functions
2166 * @port: port to listen on
2168 * Creates server context with specified callbacks.
2170 * Return a valid pointer on success otherwise PTR_ERR.
2172 struct rtrs_srv_ctx *rtrs_srv_open(struct rtrs_srv_ops *ops, u16 port)
2174 struct rtrs_srv_ctx *ctx;
2177 ctx = alloc_srv_ctx(ops);
2179 return ERR_PTR(-ENOMEM);
2181 mutex_init(&ib_ctx.ib_dev_mutex);
2182 ib_ctx.srv_ctx = ctx;
2185 err = ib_register_client(&rtrs_srv_client);
2188 return ERR_PTR(err);
2193 EXPORT_SYMBOL(rtrs_srv_open);
2195 static void close_paths(struct rtrs_srv_sess *srv)
2197 struct rtrs_srv_path *srv_path;
2199 mutex_lock(&srv->paths_mutex);
2200 list_for_each_entry(srv_path, &srv->paths_list, s.entry)
2201 close_path(srv_path);
2202 mutex_unlock(&srv->paths_mutex);
2205 static void close_ctx(struct rtrs_srv_ctx *ctx)
2207 struct rtrs_srv_sess *srv;
2209 mutex_lock(&ctx->srv_mutex);
2210 list_for_each_entry(srv, &ctx->srv_list, ctx_list)
2212 mutex_unlock(&ctx->srv_mutex);
2213 flush_workqueue(rtrs_wq);
2217 * rtrs_srv_close() - close RTRS server context
2218 * @ctx: pointer to server context
2220 * Closes RTRS server context with all client sessions.
2222 void rtrs_srv_close(struct rtrs_srv_ctx *ctx)
2224 ib_unregister_client(&rtrs_srv_client);
2225 mutex_destroy(&ib_ctx.ib_dev_mutex);
2229 EXPORT_SYMBOL(rtrs_srv_close);
2231 static int check_module_params(void)
2233 if (sess_queue_depth < 1 || sess_queue_depth > MAX_SESS_QUEUE_DEPTH) {
2234 pr_err("Invalid sess_queue_depth value %d, has to be >= %d, <= %d.\n",
2235 sess_queue_depth, 1, MAX_SESS_QUEUE_DEPTH);
2238 if (max_chunk_size < MIN_CHUNK_SIZE || !is_power_of_2(max_chunk_size)) {
2239 pr_err("Invalid max_chunk_size value %d, has to be >= %d and should be power of two.\n",
2240 max_chunk_size, MIN_CHUNK_SIZE);
2245 * Check if IB immediate data size is enough to hold the mem_id and the
2246 * offset inside the memory chunk
2248 if ((ilog2(sess_queue_depth - 1) + 1) +
2249 (ilog2(max_chunk_size - 1) + 1) > MAX_IMM_PAYL_BITS) {
2250 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",
2251 MAX_IMM_PAYL_BITS, sess_queue_depth, max_chunk_size);
2258 static int __init rtrs_server_init(void)
2262 pr_info("Loading module %s, proto %s: (max_chunk_size: %d (pure IO %ld, headers %ld) , sess_queue_depth: %d, always_invalidate: %d)\n",
2263 KBUILD_MODNAME, RTRS_PROTO_VER_STRING,
2264 max_chunk_size, max_chunk_size - MAX_HDR_SIZE, MAX_HDR_SIZE,
2265 sess_queue_depth, always_invalidate);
2267 rtrs_rdma_dev_pd_init(0, &dev_pd);
2269 err = check_module_params();
2271 pr_err("Failed to load module, invalid module parameters, err: %d\n",
2275 err = class_register(&rtrs_dev_class);
2279 rtrs_wq = alloc_workqueue("rtrs_server_wq", 0, 0);
2288 class_unregister(&rtrs_dev_class);
2293 static void __exit rtrs_server_exit(void)
2295 destroy_workqueue(rtrs_wq);
2296 class_unregister(&rtrs_dev_class);
2297 rtrs_rdma_dev_pd_deinit(&dev_pd);
2300 module_init(rtrs_server_init);
2301 module_exit(rtrs_server_exit);