1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * RDMA Transport Layer 4 * 5 * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved. 6 * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved. 7 * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved. 8 */ 9 10 #undef pr_fmt 11 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt 12 13 #include <linux/module.h> 14 15 #include "rtrs-srv.h" 16 #include "rtrs-log.h" 17 #include <rdma/ib_cm.h> 18 #include <rdma/ib_verbs.h> 19 #include "rtrs-srv-trace.h" 20 21 MODULE_DESCRIPTION("RDMA Transport Server"); 22 MODULE_LICENSE("GPL"); 23 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 28 29 static struct rtrs_rdma_dev_pd dev_pd; 30 const struct class rtrs_dev_class = { 31 .name = "rtrs-server", 32 }; 33 static struct rtrs_srv_ib_ctx ib_ctx; 34 35 static int __read_mostly max_chunk_size = DEFAULT_MAX_CHUNK_SIZE; 36 static int __read_mostly sess_queue_depth = DEFAULT_SESS_QUEUE_DEPTH; 37 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."); 42 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)"); 47 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) ")"); 53 54 static cpumask_t cq_affinity_mask = { CPU_BITS_ALL }; 55 56 static struct workqueue_struct *rtrs_wq; 57 58 static inline struct rtrs_srv_con *to_srv_con(struct rtrs_con *c) 59 { 60 return container_of(c, struct rtrs_srv_con, c); 61 } 62 63 static bool rtrs_srv_change_state(struct rtrs_srv_path *srv_path, 64 enum rtrs_srv_state new_state) 65 { 66 enum rtrs_srv_state old_state; 67 bool changed = false; 68 unsigned long flags; 69 70 spin_lock_irqsave(&srv_path->state_lock, flags); 71 old_state = srv_path->state; 72 switch (new_state) { 73 case RTRS_SRV_CONNECTED: 74 if (old_state == RTRS_SRV_CONNECTING) 75 changed = true; 76 break; 77 case RTRS_SRV_CLOSING: 78 if (old_state == RTRS_SRV_CONNECTING || 79 old_state == RTRS_SRV_CONNECTED) 80 changed = true; 81 break; 82 case RTRS_SRV_CLOSED: 83 if (old_state == RTRS_SRV_CLOSING) 84 changed = true; 85 break; 86 default: 87 break; 88 } 89 if (changed) 90 srv_path->state = new_state; 91 spin_unlock_irqrestore(&srv_path->state_lock, flags); 92 93 return changed; 94 } 95 96 static void free_id(struct rtrs_srv_op *id) 97 { 98 if (!id) 99 return; 100 kfree(id); 101 } 102 103 static void rtrs_srv_free_ops_ids(struct rtrs_srv_path *srv_path) 104 { 105 struct rtrs_srv_sess *srv = srv_path->srv; 106 int i; 107 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; 113 } 114 } 115 116 static void rtrs_srv_rdma_done(struct ib_cq *cq, struct ib_wc *wc); 117 118 static struct ib_cqe io_comp_cqe = { 119 .done = rtrs_srv_rdma_done 120 }; 121 122 static inline void rtrs_srv_inflight_ref_release(struct percpu_ref *ref) 123 { 124 struct rtrs_srv_path *srv_path = container_of(ref, 125 struct rtrs_srv_path, 126 ids_inflight_ref); 127 128 percpu_ref_exit(&srv_path->ids_inflight_ref); 129 complete(&srv_path->complete_done); 130 } 131 132 static int rtrs_srv_alloc_ops_ids(struct rtrs_srv_path *srv_path) 133 { 134 struct rtrs_srv_sess *srv = srv_path->srv; 135 struct rtrs_srv_op *id; 136 int i, ret; 137 138 srv_path->ops_ids = kcalloc(srv->queue_depth, 139 sizeof(*srv_path->ops_ids), 140 GFP_KERNEL); 141 if (!srv_path->ops_ids) 142 goto err; 143 144 for (i = 0; i < srv->queue_depth; ++i) { 145 id = kzalloc(sizeof(*id), GFP_KERNEL); 146 if (!id) 147 goto err; 148 149 srv_path->ops_ids[i] = id; 150 } 151 152 ret = percpu_ref_init(&srv_path->ids_inflight_ref, 153 rtrs_srv_inflight_ref_release, 0, GFP_KERNEL); 154 if (ret) { 155 pr_err("Percpu reference init failed\n"); 156 goto err; 157 } 158 init_completion(&srv_path->complete_done); 159 160 return 0; 161 162 err: 163 rtrs_srv_free_ops_ids(srv_path); 164 return -ENOMEM; 165 } 166 167 static inline void rtrs_srv_get_ops_ids(struct rtrs_srv_path *srv_path) 168 { 169 percpu_ref_get(&srv_path->ids_inflight_ref); 170 } 171 172 static inline void rtrs_srv_put_ops_ids(struct rtrs_srv_path *srv_path) 173 { 174 percpu_ref_put(&srv_path->ids_inflight_ref); 175 } 176 177 static void rtrs_srv_reg_mr_done(struct ib_cq *cq, struct ib_wc *wc) 178 { 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); 182 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); 187 return; 188 } 189 } 190 191 static struct ib_cqe local_reg_cqe = { 192 .done = rtrs_srv_reg_mr_done 193 }; 194 195 static int rdma_write_sg(struct rtrs_srv_op *id) 196 { 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; 205 size_t sg_cnt; 206 int err, offset; 207 bool need_inval; 208 u32 rkey = 0; 209 struct ib_reg_wr rwr; 210 struct ib_sge *plist; 211 struct ib_sge list; 212 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; 215 if (sg_cnt != 1) 216 return -EINVAL; 217 218 offset = 0; 219 220 wr = &id->tx_wr; 221 plist = &id->tx_sg; 222 plist->addr = dma_addr + offset; 223 plist->length = le32_to_cpu(id->rd_msg->desc[0].len); 224 225 /* WR will fail with length error 226 * if this is 0 227 */ 228 if (plist->length == 0) { 229 rtrs_err(s, "Invalid RDMA-Write sg list length 0\n"); 230 return -EINVAL; 231 } 232 233 plist->lkey = srv_path->s.dev->ib_pd->local_dma_lkey; 234 offset += plist->length; 235 236 wr->wr.sg_list = plist; 237 wr->wr.num_sge = 1; 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); 240 if (rkey == 0) 241 rkey = wr->rkey; 242 else 243 /* Only one key is actually used */ 244 WARN_ON_ONCE(rkey != wr->rkey); 245 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; 250 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; 261 } else { 262 wr->wr.next = &imm_wr.wr; 263 } 264 /* 265 * From time to time we have to post signaled sends, 266 * or send queue will fill up and only QP reset can help. 267 */ 268 flags = (atomic_inc_return(&id->con->c.wr_cnt) % s->signal_interval) ? 269 0 : IB_SEND_SIGNALED; 270 271 if (need_inval) { 272 inv_wr.sg_list = NULL; 273 inv_wr.num_sge = 0; 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; 278 } 279 280 imm_wr.wr.next = NULL; 281 if (always_invalidate) { 282 struct rtrs_msg_rkey_rsp *msg; 283 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; 287 rwr.wr.num_sge = 0; 288 rwr.mr = srv_mr->mr; 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); 297 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); 307 } else { 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; 311 } 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, 314 0, need_inval)); 315 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); 319 320 err = ib_post_send(id->con->c.qp, &id->tx_wr.wr, NULL); 321 if (err) 322 rtrs_err(s, 323 "Posting RDMA-Write-Request to QP failed, err: %d\n", 324 err); 325 326 return err; 327 } 328 329 /** 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. 335 * 336 * Return 0 on success, errno otherwise. 337 */ 338 static int send_io_resp_imm(struct rtrs_srv_con *con, struct rtrs_srv_op *id, 339 int errno) 340 { 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; 349 u32 imm; 350 int err; 351 352 if (id->dir == READ) { 353 struct rtrs_msg_rdma_read *rd_msg = id->rd_msg; 354 size_t sg_cnt; 355 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); 359 360 if (need_inval) { 361 if (sg_cnt) { 362 inv_wr.wr_cqe = &io_comp_cqe; 363 inv_wr.sg_list = NULL; 364 inv_wr.num_sge = 0; 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); 370 } else { 371 WARN_ON_ONCE(1); 372 need_inval = false; 373 } 374 } 375 } 376 377 trace_send_io_resp_imm(id, need_inval, always_invalidate, errno); 378 379 if (need_inval && always_invalidate) { 380 wr = &inv_wr; 381 inv_wr.next = &rwr.wr; 382 rwr.wr.next = &imm_wr.wr; 383 } else if (always_invalidate) { 384 wr = &rwr.wr; 385 rwr.wr.next = &imm_wr.wr; 386 } else if (need_inval) { 387 wr = &inv_wr; 388 inv_wr.next = &imm_wr.wr; 389 } else { 390 wr = &imm_wr.wr; 391 } 392 /* 393 * From time to time we have to post signalled sends, 394 * or send queue will fill up and only QP reset can help. 395 */ 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) { 401 struct ib_sge list; 402 struct rtrs_msg_rkey_rsp *msg; 403 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; 408 rwr.wr.num_sge = 0; 409 rwr.wr.send_flags = 0; 410 rwr.mr = srv_mr->mr; 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); 418 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); 428 } else { 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; 432 } 433 imm_wr.wr.send_flags = flags; 434 imm_wr.wr.wr_cqe = &io_comp_cqe; 435 436 imm_wr.wr.ex.imm_data = cpu_to_be32(imm); 437 438 err = ib_post_send(id->con->c.qp, wr, NULL); 439 if (err) 440 rtrs_err_rl(s, "Posting RDMA-Reply to QP failed, err: %d\n", 441 err); 442 443 return err; 444 } 445 446 void close_path(struct rtrs_srv_path *srv_path) 447 { 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); 451 } 452 453 static inline const char *rtrs_srv_state_str(enum rtrs_srv_state state) 454 { 455 switch (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"; 464 default: 465 return "UNKNOWN"; 466 } 467 } 468 469 /** 470 * rtrs_srv_resp_rdma() - Finish an RDMA request 471 * 472 * @id: Internal RTRS operation identifier 473 * @status: Response Code sent to the other side for this operation. 474 * 0 = success, <=0 error 475 * Context: any 476 * 477 * Finish a RDMA operation. A message is sent to the client and the 478 * corresponding memory areas will be released. 479 */ 480 bool rtrs_srv_resp_rdma(struct rtrs_srv_op *id, int status) 481 { 482 struct rtrs_srv_path *srv_path; 483 struct rtrs_srv_con *con; 484 struct rtrs_path *s; 485 int err; 486 487 if (WARN_ON(!id)) 488 return true; 489 490 con = id->con; 491 s = con->c.path; 492 srv_path = to_srv_path(s); 493 494 id->status = status; 495 496 if (srv_path->state != RTRS_SRV_CONNECTED) { 497 rtrs_err_rl(s, 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)); 501 goto out; 502 } 503 if (always_invalidate) { 504 struct rtrs_srv_mr *mr = &srv_path->mrs[id->msg_id]; 505 506 ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey)); 507 } 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), 511 con->c.cid); 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); 516 return false; 517 } 518 519 if (status || id->dir == WRITE || !id->rd_msg->sg_cnt) 520 err = send_io_resp_imm(con, id, status); 521 else 522 err = rdma_write_sg(id); 523 524 if (err) { 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); 528 } 529 out: 530 rtrs_srv_put_ops_ids(srv_path); 531 return true; 532 } 533 EXPORT_SYMBOL(rtrs_srv_resp_rdma); 534 535 /** 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. 539 */ 540 void rtrs_srv_set_sess_priv(struct rtrs_srv_sess *srv, void *priv) 541 { 542 srv->priv = priv; 543 } 544 EXPORT_SYMBOL(rtrs_srv_set_sess_priv); 545 546 static void unmap_cont_bufs(struct rtrs_srv_path *srv_path) 547 { 548 int i; 549 550 for (i = 0; i < srv_path->mrs_num; i++) { 551 struct rtrs_srv_mr *srv_mr; 552 553 srv_mr = &srv_path->mrs[i]; 554 555 if (always_invalidate) 556 rtrs_iu_free(srv_mr->iu, srv_path->s.dev->ib_dev, 1); 557 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); 562 } 563 kfree(srv_path->mrs); 564 } 565 566 static int map_cont_bufs(struct rtrs_srv_path *srv_path) 567 { 568 struct rtrs_srv_sess *srv = srv_path->srv; 569 struct rtrs_path *ss = &srv_path->s; 570 int i, err, mrs_num; 571 unsigned int chunk_bits; 572 int chunks_per_mr = 1; 573 struct ib_mr *mr; 574 struct sg_table *sgt; 575 576 /* 577 * Here we map queue_depth chunks to MR. Firstly we have to 578 * figure out how many chunks can we map per MR. 579 */ 580 if (always_invalidate) { 581 /* 582 * in order to do invalidate for each chunks of memory, we needs 583 * more memory regions. 584 */ 585 mrs_num = srv->queue_depth; 586 } else { 587 chunks_per_mr = 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); 591 } 592 593 srv_path->mrs = kcalloc(mrs_num, sizeof(*srv_path->mrs), GFP_KERNEL); 594 if (!srv_path->mrs) 595 return -ENOMEM; 596 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; 602 603 sgt = &srv_mr->sgt; 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); 608 609 err = sg_alloc_table(sgt, chunks_per_mr, GFP_KERNEL); 610 if (err) 611 goto err; 612 613 for_each_sg(sgt->sgl, s, chunks_per_mr, i) 614 sg_set_page(s, srv->chunks[chunks + i], 615 max_chunk_size, 0); 616 617 nr_sgt = ib_dma_map_sg(srv_path->s.dev->ib_dev, sgt->sgl, 618 sgt->nents, DMA_BIDIRECTIONAL); 619 if (!nr_sgt) { 620 err = -EINVAL; 621 goto free_sg; 622 } 623 mr = ib_alloc_mr(srv_path->s.dev->ib_pd, IB_MR_TYPE_MEM_REG, 624 nr_sgt); 625 if (IS_ERR(mr)) { 626 err = PTR_ERR(mr); 627 goto unmap_sg; 628 } 629 nr = ib_map_mr_sg(mr, sgt->sgl, nr_sgt, 630 NULL, max_chunk_size); 631 if (nr != nr_sgt) { 632 err = nr < 0 ? nr : -EINVAL; 633 goto dereg_mr; 634 } 635 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); 641 if (!srv_mr->iu) { 642 err = -ENOMEM; 643 rtrs_err(ss, "rtrs_iu_alloc(), err: %d\n", err); 644 goto dereg_mr; 645 } 646 } 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); 650 651 ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey)); 652 srv_mr->mr = mr; 653 } 654 655 chunk_bits = ilog2(srv->queue_depth - 1) + 1; 656 srv_path->mem_bits = (MAX_IMM_PAYL_BITS - chunk_bits); 657 658 return 0; 659 660 dereg_mr: 661 ib_dereg_mr(mr); 662 unmap_sg: 663 ib_dma_unmap_sg(srv_path->s.dev->ib_dev, sgt->sgl, 664 sgt->nents, DMA_BIDIRECTIONAL); 665 free_sg: 666 sg_free_table(sgt); 667 err: 668 unmap_cont_bufs(srv_path); 669 670 return err; 671 } 672 673 static void rtrs_srv_hb_err_handler(struct rtrs_con *c) 674 { 675 close_path(to_srv_path(c->path)); 676 } 677 678 static void rtrs_srv_init_hb(struct rtrs_srv_path *srv_path) 679 { 680 rtrs_init_hb(&srv_path->s, &io_comp_cqe, 681 RTRS_HB_INTERVAL_MS, 682 RTRS_HB_MISSED_MAX, 683 rtrs_srv_hb_err_handler, 684 rtrs_wq); 685 } 686 687 static void rtrs_srv_start_hb(struct rtrs_srv_path *srv_path) 688 { 689 rtrs_start_hb(&srv_path->s); 690 } 691 692 static void rtrs_srv_stop_hb(struct rtrs_srv_path *srv_path) 693 { 694 rtrs_stop_hb(&srv_path->s); 695 } 696 697 static void rtrs_srv_info_rsp_done(struct ib_cq *cq, struct ib_wc *wc) 698 { 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); 702 struct rtrs_iu *iu; 703 704 iu = container_of(wc->wr_cqe, struct rtrs_iu, cqe); 705 rtrs_iu_free(iu, srv_path->s.dev->ib_dev, 1); 706 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); 711 return; 712 } 713 WARN_ON(wc->opcode != IB_WC_SEND); 714 } 715 716 static int rtrs_srv_path_up(struct rtrs_srv_path *srv_path) 717 { 718 struct rtrs_srv_sess *srv = srv_path->srv; 719 struct rtrs_srv_ctx *ctx = srv->ctx; 720 int up, ret = 0; 721 722 mutex_lock(&srv->paths_ev_mutex); 723 up = ++srv->paths_up; 724 if (up == 1) 725 ret = ctx->ops.link_ev(srv, RTRS_SRV_LINK_EV_CONNECTED, NULL); 726 mutex_unlock(&srv->paths_ev_mutex); 727 728 /* Mark session as established */ 729 if (!ret) 730 srv_path->established = true; 731 732 return ret; 733 } 734 735 static void rtrs_srv_path_down(struct rtrs_srv_path *srv_path) 736 { 737 struct rtrs_srv_sess *srv = srv_path->srv; 738 struct rtrs_srv_ctx *ctx = srv->ctx; 739 740 if (!srv_path->established) 741 return; 742 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); 749 } 750 751 static bool exist_pathname(struct rtrs_srv_ctx *ctx, 752 const char *pathname, const uuid_t *path_uuid) 753 { 754 struct rtrs_srv_sess *srv; 755 struct rtrs_srv_path *srv_path; 756 bool found = false; 757 758 mutex_lock(&ctx->srv_mutex); 759 list_for_each_entry(srv, &ctx->srv_list, ctx_list) { 760 mutex_lock(&srv->paths_mutex); 761 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); 765 continue; 766 } 767 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)) { 771 found = true; 772 break; 773 } 774 } 775 mutex_unlock(&srv->paths_mutex); 776 if (found) 777 break; 778 } 779 mutex_unlock(&ctx->srv_mutex); 780 return found; 781 } 782 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); 785 786 static int process_info_req(struct rtrs_srv_con *con, 787 struct rtrs_msg_info_req *msg) 788 { 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; 795 int mri, err; 796 size_t tx_sz; 797 798 err = post_recv_path(srv_path); 799 if (err) { 800 rtrs_err(s, "post_recv_path(), err: %d\n", err); 801 return err; 802 } 803 804 if (strchr(msg->pathname, '/') || strchr(msg->pathname, '.')) { 805 rtrs_err(s, "pathname cannot contain / and .\n"); 806 return -EINVAL; 807 } 808 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); 812 return -EPERM; 813 } 814 strscpy(srv_path->s.sessname, msg->pathname, 815 sizeof(srv_path->s.sessname)); 816 817 rwr = kcalloc(srv_path->mrs_num, sizeof(*rwr), GFP_KERNEL); 818 if (!rwr) 819 return -ENOMEM; 820 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); 825 if (!tx_iu) { 826 err = -ENOMEM; 827 goto rwr_free; 828 } 829 830 rsp = tx_iu->buf; 831 rsp->type = cpu_to_le16(RTRS_MSG_INFO_RSP); 832 rsp->sg_cnt = cpu_to_le16(srv_path->mrs_num); 833 834 for (mri = 0; mri < srv_path->mrs_num; mri++) { 835 struct ib_mr *mr = srv_path->mrs[mri].mr; 836 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); 840 841 /* 842 * Fill in reg MR request and chain them *backwards* 843 */ 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; 849 rwr[mri].mr = mr; 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; 854 } 855 856 err = rtrs_srv_create_path_files(srv_path); 857 if (err) 858 goto iu_free; 859 kobject_get(&srv_path->kobj); 860 get_device(&srv_path->srv->dev); 861 err = rtrs_srv_change_state(srv_path, RTRS_SRV_CONNECTED); 862 if (!err) { 863 rtrs_err(s, "rtrs_srv_change_state(), err: %d\n", err); 864 goto iu_free; 865 } 866 867 rtrs_srv_start_hb(srv_path); 868 869 /* 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. 874 */ 875 err = rtrs_srv_path_up(srv_path); 876 if (err) { 877 rtrs_err(s, "rtrs_srv_path_up(), err: %d\n", err); 878 goto iu_free; 879 } 880 881 ib_dma_sync_single_for_device(srv_path->s.dev->ib_dev, 882 tx_iu->dma_addr, 883 tx_iu->size, DMA_TO_DEVICE); 884 885 /* Send info response */ 886 err = rtrs_iu_post_send(&con->c, tx_iu, tx_sz, reg_wr); 887 if (err) { 888 rtrs_err(s, "rtrs_iu_post_send(), err: %d\n", err); 889 iu_free: 890 rtrs_iu_free(tx_iu, srv_path->s.dev->ib_dev, 1); 891 } 892 rwr_free: 893 kfree(rwr); 894 895 return err; 896 } 897 898 static void rtrs_srv_info_req_done(struct ib_cq *cq, struct ib_wc *wc) 899 { 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; 904 struct rtrs_iu *iu; 905 int err; 906 907 WARN_ON(con->c.cid); 908 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)); 913 goto close; 914 } 915 WARN_ON(wc->opcode != IB_WC_RECV); 916 917 if (wc->byte_len < sizeof(*msg)) { 918 rtrs_err(s, "Sess info request is malformed: size %d\n", 919 wc->byte_len); 920 goto close; 921 } 922 ib_dma_sync_single_for_cpu(srv_path->s.dev->ib_dev, iu->dma_addr, 923 iu->size, DMA_FROM_DEVICE); 924 msg = iu->buf; 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)); 928 goto close; 929 } 930 err = process_info_req(con, msg); 931 if (err) 932 goto close; 933 934 rtrs_iu_free(iu, srv_path->s.dev->ib_dev, 1); 935 return; 936 close: 937 rtrs_iu_free(iu, srv_path->s.dev->ib_dev, 1); 938 close_path(srv_path); 939 } 940 941 static int post_recv_info_req(struct rtrs_srv_con *con) 942 { 943 struct rtrs_path *s = con->c.path; 944 struct rtrs_srv_path *srv_path = to_srv_path(s); 945 struct rtrs_iu *rx_iu; 946 int err; 947 948 rx_iu = rtrs_iu_alloc(1, sizeof(struct rtrs_msg_info_req), 949 GFP_KERNEL, srv_path->s.dev->ib_dev, 950 DMA_FROM_DEVICE, rtrs_srv_info_req_done); 951 if (!rx_iu) 952 return -ENOMEM; 953 /* Prepare for getting info response */ 954 err = rtrs_iu_post_recv(&con->c, rx_iu); 955 if (err) { 956 rtrs_err(s, "rtrs_iu_post_recv(), err: %d\n", err); 957 rtrs_iu_free(rx_iu, srv_path->s.dev->ib_dev, 1); 958 return err; 959 } 960 961 return 0; 962 } 963 964 static int post_recv_io(struct rtrs_srv_con *con, size_t q_size) 965 { 966 int i, err; 967 968 for (i = 0; i < q_size; i++) { 969 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe); 970 if (err) 971 return err; 972 } 973 974 return 0; 975 } 976 977 static int post_recv_path(struct rtrs_srv_path *srv_path) 978 { 979 struct rtrs_srv_sess *srv = srv_path->srv; 980 struct rtrs_path *s = &srv_path->s; 981 size_t q_size; 982 int err, cid; 983 984 for (cid = 0; cid < srv_path->s.con_num; cid++) { 985 if (cid == 0) 986 q_size = SERVICE_CON_QUEUE_DEPTH; 987 else 988 q_size = srv->queue_depth; 989 if (srv_path->state != RTRS_SRV_CONNECTING) { 990 rtrs_err(s, "Path state invalid. state %s\n", 991 rtrs_srv_state_str(srv_path->state)); 992 return -EIO; 993 } 994 995 if (!srv_path->s.con[cid]) { 996 rtrs_err(s, "Conn not set for %d\n", cid); 997 return -EIO; 998 } 999 1000 err = post_recv_io(to_srv_con(srv_path->s.con[cid]), q_size); 1001 if (err) { 1002 rtrs_err(s, "post_recv_io(), err: %d\n", err); 1003 return err; 1004 } 1005 } 1006 1007 return 0; 1008 } 1009 1010 static void process_read(struct rtrs_srv_con *con, 1011 struct rtrs_msg_rdma_read *msg, 1012 u32 buf_id, u32 off) 1013 { 1014 struct rtrs_path *s = con->c.path; 1015 struct rtrs_srv_path *srv_path = to_srv_path(s); 1016 struct rtrs_srv_sess *srv = srv_path->srv; 1017 struct rtrs_srv_ctx *ctx = srv->ctx; 1018 struct rtrs_srv_op *id; 1019 1020 size_t usr_len, data_len; 1021 void *data; 1022 int ret; 1023 1024 if (srv_path->state != RTRS_SRV_CONNECTED) { 1025 rtrs_err_rl(s, 1026 "Processing read request failed, session is disconnected, sess state %s\n", 1027 rtrs_srv_state_str(srv_path->state)); 1028 return; 1029 } 1030 if (msg->sg_cnt != 1 && msg->sg_cnt != 0) { 1031 rtrs_err_rl(s, 1032 "Processing read request failed, invalid message\n"); 1033 return; 1034 } 1035 rtrs_srv_get_ops_ids(srv_path); 1036 rtrs_srv_update_rdma_stats(srv_path->stats, off, READ); 1037 id = srv_path->ops_ids[buf_id]; 1038 id->con = con; 1039 id->dir = READ; 1040 id->msg_id = buf_id; 1041 id->rd_msg = msg; 1042 usr_len = le16_to_cpu(msg->usr_len); 1043 data_len = off - usr_len; 1044 data = page_address(srv->chunks[buf_id]); 1045 ret = ctx->ops.rdma_ev(srv->priv, id, data, data_len, 1046 data + data_len, usr_len); 1047 1048 if (ret) { 1049 rtrs_err_rl(s, 1050 "Processing read request failed, user module cb reported for msg_id %d, err: %d\n", 1051 buf_id, ret); 1052 goto send_err_msg; 1053 } 1054 1055 return; 1056 1057 send_err_msg: 1058 ret = send_io_resp_imm(con, id, ret); 1059 if (ret < 0) { 1060 rtrs_err_rl(s, 1061 "Sending err msg for failed RDMA-Write-Req failed, msg_id %d, err: %d\n", 1062 buf_id, ret); 1063 close_path(srv_path); 1064 } 1065 rtrs_srv_put_ops_ids(srv_path); 1066 } 1067 1068 static void process_write(struct rtrs_srv_con *con, 1069 struct rtrs_msg_rdma_write *req, 1070 u32 buf_id, u32 off) 1071 { 1072 struct rtrs_path *s = con->c.path; 1073 struct rtrs_srv_path *srv_path = to_srv_path(s); 1074 struct rtrs_srv_sess *srv = srv_path->srv; 1075 struct rtrs_srv_ctx *ctx = srv->ctx; 1076 struct rtrs_srv_op *id; 1077 1078 size_t data_len, usr_len; 1079 void *data; 1080 int ret; 1081 1082 if (srv_path->state != RTRS_SRV_CONNECTED) { 1083 rtrs_err_rl(s, 1084 "Processing write request failed, session is disconnected, sess state %s\n", 1085 rtrs_srv_state_str(srv_path->state)); 1086 return; 1087 } 1088 rtrs_srv_get_ops_ids(srv_path); 1089 rtrs_srv_update_rdma_stats(srv_path->stats, off, WRITE); 1090 id = srv_path->ops_ids[buf_id]; 1091 id->con = con; 1092 id->dir = WRITE; 1093 id->msg_id = buf_id; 1094 1095 usr_len = le16_to_cpu(req->usr_len); 1096 data_len = off - usr_len; 1097 data = page_address(srv->chunks[buf_id]); 1098 ret = ctx->ops.rdma_ev(srv->priv, id, data, data_len, 1099 data + data_len, usr_len); 1100 if (ret) { 1101 rtrs_err_rl(s, 1102 "Processing write request failed, user module callback reports err: %d\n", 1103 ret); 1104 goto send_err_msg; 1105 } 1106 1107 return; 1108 1109 send_err_msg: 1110 ret = send_io_resp_imm(con, id, ret); 1111 if (ret < 0) { 1112 rtrs_err_rl(s, 1113 "Processing write request failed, sending I/O response failed, msg_id %d, err: %d\n", 1114 buf_id, ret); 1115 close_path(srv_path); 1116 } 1117 rtrs_srv_put_ops_ids(srv_path); 1118 } 1119 1120 static void process_io_req(struct rtrs_srv_con *con, void *msg, 1121 u32 id, u32 off) 1122 { 1123 struct rtrs_path *s = con->c.path; 1124 struct rtrs_srv_path *srv_path = to_srv_path(s); 1125 struct rtrs_msg_rdma_hdr *hdr; 1126 unsigned int type; 1127 1128 ib_dma_sync_single_for_cpu(srv_path->s.dev->ib_dev, 1129 srv_path->dma_addr[id], 1130 max_chunk_size, DMA_BIDIRECTIONAL); 1131 hdr = msg; 1132 type = le16_to_cpu(hdr->type); 1133 1134 switch (type) { 1135 case RTRS_MSG_WRITE: 1136 process_write(con, msg, id, off); 1137 break; 1138 case RTRS_MSG_READ: 1139 process_read(con, msg, id, off); 1140 break; 1141 default: 1142 rtrs_err(s, 1143 "Processing I/O request failed, unknown message type received: 0x%02x\n", 1144 type); 1145 goto err; 1146 } 1147 1148 return; 1149 1150 err: 1151 close_path(srv_path); 1152 } 1153 1154 static void rtrs_srv_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) 1155 { 1156 struct rtrs_srv_mr *mr = 1157 container_of(wc->wr_cqe, typeof(*mr), inv_cqe); 1158 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context); 1159 struct rtrs_path *s = con->c.path; 1160 struct rtrs_srv_path *srv_path = to_srv_path(s); 1161 struct rtrs_srv_sess *srv = srv_path->srv; 1162 u32 msg_id, off; 1163 void *data; 1164 1165 if (wc->status != IB_WC_SUCCESS) { 1166 rtrs_err(s, "Failed IB_WR_LOCAL_INV: %s\n", 1167 ib_wc_status_msg(wc->status)); 1168 close_path(srv_path); 1169 } 1170 msg_id = mr->msg_id; 1171 off = mr->msg_off; 1172 data = page_address(srv->chunks[msg_id]) + off; 1173 process_io_req(con, data, msg_id, off); 1174 } 1175 1176 static int rtrs_srv_inv_rkey(struct rtrs_srv_con *con, 1177 struct rtrs_srv_mr *mr) 1178 { 1179 struct ib_send_wr wr = { 1180 .opcode = IB_WR_LOCAL_INV, 1181 .wr_cqe = &mr->inv_cqe, 1182 .send_flags = IB_SEND_SIGNALED, 1183 .ex.invalidate_rkey = mr->mr->rkey, 1184 }; 1185 mr->inv_cqe.done = rtrs_srv_inv_rkey_done; 1186 1187 return ib_post_send(con->c.qp, &wr, NULL); 1188 } 1189 1190 static void rtrs_rdma_process_wr_wait_list(struct rtrs_srv_con *con) 1191 { 1192 spin_lock(&con->rsp_wr_wait_lock); 1193 while (!list_empty(&con->rsp_wr_wait_list)) { 1194 struct rtrs_srv_op *id; 1195 int ret; 1196 1197 id = list_entry(con->rsp_wr_wait_list.next, 1198 struct rtrs_srv_op, wait_list); 1199 list_del(&id->wait_list); 1200 1201 spin_unlock(&con->rsp_wr_wait_lock); 1202 ret = rtrs_srv_resp_rdma(id, id->status); 1203 spin_lock(&con->rsp_wr_wait_lock); 1204 1205 if (!ret) { 1206 list_add(&id->wait_list, &con->rsp_wr_wait_list); 1207 break; 1208 } 1209 } 1210 spin_unlock(&con->rsp_wr_wait_lock); 1211 } 1212 1213 static void rtrs_srv_rdma_done(struct ib_cq *cq, struct ib_wc *wc) 1214 { 1215 struct rtrs_srv_con *con = to_srv_con(wc->qp->qp_context); 1216 struct rtrs_path *s = con->c.path; 1217 struct rtrs_srv_path *srv_path = to_srv_path(s); 1218 struct rtrs_srv_sess *srv = srv_path->srv; 1219 u32 imm_type, imm_payload; 1220 int err; 1221 1222 if (wc->status != IB_WC_SUCCESS) { 1223 if (wc->status != IB_WC_WR_FLUSH_ERR) { 1224 rtrs_err(s, 1225 "%s (wr_cqe: %p, type: %d, vendor_err: 0x%x, len: %u)\n", 1226 ib_wc_status_msg(wc->status), wc->wr_cqe, 1227 wc->opcode, wc->vendor_err, wc->byte_len); 1228 close_path(srv_path); 1229 } 1230 return; 1231 } 1232 1233 switch (wc->opcode) { 1234 case IB_WC_RECV_RDMA_WITH_IMM: 1235 /* 1236 * post_recv() RDMA write completions of IO reqs (read/write) 1237 * and hb 1238 */ 1239 if (WARN_ON(wc->wr_cqe != &io_comp_cqe)) 1240 return; 1241 srv_path->s.hb_missed_cnt = 0; 1242 err = rtrs_post_recv_empty(&con->c, &io_comp_cqe); 1243 if (err) { 1244 rtrs_err(s, "rtrs_post_recv(), err: %d\n", err); 1245 close_path(srv_path); 1246 break; 1247 } 1248 rtrs_from_imm(be32_to_cpu(wc->ex.imm_data), 1249 &imm_type, &imm_payload); 1250 if (imm_type == RTRS_IO_REQ_IMM) { 1251 u32 msg_id, off; 1252 void *data; 1253 1254 msg_id = imm_payload >> srv_path->mem_bits; 1255 off = imm_payload & ((1 << srv_path->mem_bits) - 1); 1256 if (msg_id >= srv->queue_depth || off >= max_chunk_size) { 1257 rtrs_err(s, "Wrong msg_id %u, off %u\n", 1258 msg_id, off); 1259 close_path(srv_path); 1260 return; 1261 } 1262 if (always_invalidate) { 1263 struct rtrs_srv_mr *mr = &srv_path->mrs[msg_id]; 1264 1265 mr->msg_off = off; 1266 mr->msg_id = msg_id; 1267 err = rtrs_srv_inv_rkey(con, mr); 1268 if (err) { 1269 rtrs_err(s, "rtrs_post_recv(), err: %d\n", 1270 err); 1271 close_path(srv_path); 1272 break; 1273 } 1274 } else { 1275 data = page_address(srv->chunks[msg_id]) + off; 1276 process_io_req(con, data, msg_id, off); 1277 } 1278 } else if (imm_type == RTRS_HB_MSG_IMM) { 1279 WARN_ON(con->c.cid); 1280 rtrs_send_hb_ack(&srv_path->s); 1281 } else if (imm_type == RTRS_HB_ACK_IMM) { 1282 WARN_ON(con->c.cid); 1283 srv_path->s.hb_missed_cnt = 0; 1284 } else { 1285 rtrs_wrn(s, "Unknown IMM type %u\n", imm_type); 1286 } 1287 break; 1288 case IB_WC_RDMA_WRITE: 1289 case IB_WC_SEND: 1290 /* 1291 * post_send() RDMA write completions of IO reqs (read/write) 1292 * and hb. 1293 */ 1294 atomic_add(s->signal_interval, &con->c.sq_wr_avail); 1295 1296 if (!list_empty_careful(&con->rsp_wr_wait_list)) 1297 rtrs_rdma_process_wr_wait_list(con); 1298 1299 break; 1300 default: 1301 rtrs_wrn(s, "Unexpected WC type: %d\n", wc->opcode); 1302 return; 1303 } 1304 } 1305 1306 /** 1307 * rtrs_srv_get_path_name() - Get rtrs_srv peer hostname. 1308 * @srv: Session 1309 * @pathname: Pathname buffer 1310 * @len: Length of sessname buffer 1311 */ 1312 int rtrs_srv_get_path_name(struct rtrs_srv_sess *srv, char *pathname, 1313 size_t len) 1314 { 1315 struct rtrs_srv_path *srv_path; 1316 int err = -ENOTCONN; 1317 1318 mutex_lock(&srv->paths_mutex); 1319 list_for_each_entry(srv_path, &srv->paths_list, s.entry) { 1320 if (srv_path->state != RTRS_SRV_CONNECTED) 1321 continue; 1322 strscpy(pathname, srv_path->s.sessname, 1323 min_t(size_t, sizeof(srv_path->s.sessname), len)); 1324 err = 0; 1325 break; 1326 } 1327 mutex_unlock(&srv->paths_mutex); 1328 1329 return err; 1330 } 1331 EXPORT_SYMBOL(rtrs_srv_get_path_name); 1332 1333 /** 1334 * rtrs_srv_get_queue_depth() - Get rtrs_srv qdepth. 1335 * @srv: Session 1336 */ 1337 int rtrs_srv_get_queue_depth(struct rtrs_srv_sess *srv) 1338 { 1339 return srv->queue_depth; 1340 } 1341 EXPORT_SYMBOL(rtrs_srv_get_queue_depth); 1342 1343 static int find_next_bit_ring(struct rtrs_srv_path *srv_path) 1344 { 1345 struct ib_device *ib_dev = srv_path->s.dev->ib_dev; 1346 int v; 1347 1348 v = cpumask_next(srv_path->cur_cq_vector, &cq_affinity_mask); 1349 if (v >= nr_cpu_ids || v >= ib_dev->num_comp_vectors) 1350 v = cpumask_first(&cq_affinity_mask); 1351 return v; 1352 } 1353 1354 static int rtrs_srv_get_next_cq_vector(struct rtrs_srv_path *srv_path) 1355 { 1356 srv_path->cur_cq_vector = find_next_bit_ring(srv_path); 1357 1358 return srv_path->cur_cq_vector; 1359 } 1360 1361 static void rtrs_srv_dev_release(struct device *dev) 1362 { 1363 struct rtrs_srv_sess *srv = container_of(dev, struct rtrs_srv_sess, 1364 dev); 1365 1366 kfree(srv); 1367 } 1368 1369 static void free_srv(struct rtrs_srv_sess *srv) 1370 { 1371 int i; 1372 1373 WARN_ON(refcount_read(&srv->refcount)); 1374 for (i = 0; i < srv->queue_depth; i++) 1375 __free_pages(srv->chunks[i], get_order(max_chunk_size)); 1376 kfree(srv->chunks); 1377 mutex_destroy(&srv->paths_mutex); 1378 mutex_destroy(&srv->paths_ev_mutex); 1379 /* last put to release the srv structure */ 1380 put_device(&srv->dev); 1381 } 1382 1383 static struct rtrs_srv_sess *get_or_create_srv(struct rtrs_srv_ctx *ctx, 1384 const uuid_t *paths_uuid, 1385 bool first_conn) 1386 { 1387 struct rtrs_srv_sess *srv; 1388 int i; 1389 1390 mutex_lock(&ctx->srv_mutex); 1391 list_for_each_entry(srv, &ctx->srv_list, ctx_list) { 1392 if (uuid_equal(&srv->paths_uuid, paths_uuid) && 1393 refcount_inc_not_zero(&srv->refcount)) { 1394 mutex_unlock(&ctx->srv_mutex); 1395 return srv; 1396 } 1397 } 1398 mutex_unlock(&ctx->srv_mutex); 1399 /* 1400 * If this request is not the first connection request from the 1401 * client for this session then fail and return error. 1402 */ 1403 if (!first_conn) { 1404 pr_err_ratelimited("Error: Not the first connection request for this session\n"); 1405 return ERR_PTR(-ENXIO); 1406 } 1407 1408 /* need to allocate a new srv */ 1409 srv = kzalloc(sizeof(*srv), GFP_KERNEL); 1410 if (!srv) 1411 return ERR_PTR(-ENOMEM); 1412 1413 INIT_LIST_HEAD(&srv->paths_list); 1414 mutex_init(&srv->paths_mutex); 1415 mutex_init(&srv->paths_ev_mutex); 1416 uuid_copy(&srv->paths_uuid, paths_uuid); 1417 srv->queue_depth = sess_queue_depth; 1418 srv->ctx = ctx; 1419 device_initialize(&srv->dev); 1420 srv->dev.release = rtrs_srv_dev_release; 1421 1422 srv->chunks = kcalloc(srv->queue_depth, sizeof(*srv->chunks), 1423 GFP_KERNEL); 1424 if (!srv->chunks) 1425 goto err_free_srv; 1426 1427 for (i = 0; i < srv->queue_depth; i++) { 1428 srv->chunks[i] = alloc_pages(GFP_KERNEL, 1429 get_order(max_chunk_size)); 1430 if (!srv->chunks[i]) 1431 goto err_free_chunks; 1432 } 1433 refcount_set(&srv->refcount, 1); 1434 mutex_lock(&ctx->srv_mutex); 1435 list_add(&srv->ctx_list, &ctx->srv_list); 1436 mutex_unlock(&ctx->srv_mutex); 1437 1438 return srv; 1439 1440 err_free_chunks: 1441 while (i--) 1442 __free_pages(srv->chunks[i], get_order(max_chunk_size)); 1443 kfree(srv->chunks); 1444 1445 err_free_srv: 1446 kfree(srv); 1447 return ERR_PTR(-ENOMEM); 1448 } 1449 1450 static void put_srv(struct rtrs_srv_sess *srv) 1451 { 1452 if (refcount_dec_and_test(&srv->refcount)) { 1453 struct rtrs_srv_ctx *ctx = srv->ctx; 1454 1455 WARN_ON(srv->dev.kobj.state_in_sysfs); 1456 1457 mutex_lock(&ctx->srv_mutex); 1458 list_del(&srv->ctx_list); 1459 mutex_unlock(&ctx->srv_mutex); 1460 free_srv(srv); 1461 } 1462 } 1463 1464 static void __add_path_to_srv(struct rtrs_srv_sess *srv, 1465 struct rtrs_srv_path *srv_path) 1466 { 1467 list_add_tail(&srv_path->s.entry, &srv->paths_list); 1468 srv->paths_num++; 1469 WARN_ON(srv->paths_num >= MAX_PATHS_NUM); 1470 } 1471 1472 static void del_path_from_srv(struct rtrs_srv_path *srv_path) 1473 { 1474 struct rtrs_srv_sess *srv = srv_path->srv; 1475 1476 if (WARN_ON(!srv)) 1477 return; 1478 1479 mutex_lock(&srv->paths_mutex); 1480 list_del(&srv_path->s.entry); 1481 WARN_ON(!srv->paths_num); 1482 srv->paths_num--; 1483 mutex_unlock(&srv->paths_mutex); 1484 } 1485 1486 /* return true if addresses are the same, error other wise */ 1487 static int sockaddr_cmp(const struct sockaddr *a, const struct sockaddr *b) 1488 { 1489 switch (a->sa_family) { 1490 case AF_IB: 1491 return memcmp(&((struct sockaddr_ib *)a)->sib_addr, 1492 &((struct sockaddr_ib *)b)->sib_addr, 1493 sizeof(struct ib_addr)) && 1494 (b->sa_family == AF_IB); 1495 case AF_INET: 1496 return memcmp(&((struct sockaddr_in *)a)->sin_addr, 1497 &((struct sockaddr_in *)b)->sin_addr, 1498 sizeof(struct in_addr)) && 1499 (b->sa_family == AF_INET); 1500 case AF_INET6: 1501 return memcmp(&((struct sockaddr_in6 *)a)->sin6_addr, 1502 &((struct sockaddr_in6 *)b)->sin6_addr, 1503 sizeof(struct in6_addr)) && 1504 (b->sa_family == AF_INET6); 1505 default: 1506 return -ENOENT; 1507 } 1508 } 1509 1510 static bool __is_path_w_addr_exists(struct rtrs_srv_sess *srv, 1511 struct rdma_addr *addr) 1512 { 1513 struct rtrs_srv_path *srv_path; 1514 1515 list_for_each_entry(srv_path, &srv->paths_list, s.entry) 1516 if (!sockaddr_cmp((struct sockaddr *)&srv_path->s.dst_addr, 1517 (struct sockaddr *)&addr->dst_addr) && 1518 !sockaddr_cmp((struct sockaddr *)&srv_path->s.src_addr, 1519 (struct sockaddr *)&addr->src_addr)) 1520 return true; 1521 1522 return false; 1523 } 1524 1525 static void free_path(struct rtrs_srv_path *srv_path) 1526 { 1527 if (srv_path->kobj.state_in_sysfs) { 1528 kobject_del(&srv_path->kobj); 1529 kobject_put(&srv_path->kobj); 1530 } else { 1531 free_percpu(srv_path->stats->rdma_stats); 1532 kfree(srv_path->stats); 1533 kfree(srv_path); 1534 } 1535 } 1536 1537 static void rtrs_srv_close_work(struct work_struct *work) 1538 { 1539 struct rtrs_srv_path *srv_path; 1540 struct rtrs_srv_con *con; 1541 int i; 1542 1543 srv_path = container_of(work, typeof(*srv_path), close_work); 1544 1545 rtrs_srv_stop_hb(srv_path); 1546 1547 for (i = 0; i < srv_path->s.con_num; i++) { 1548 if (!srv_path->s.con[i]) 1549 continue; 1550 con = to_srv_con(srv_path->s.con[i]); 1551 rdma_disconnect(con->c.cm_id); 1552 ib_drain_qp(con->c.qp); 1553 } 1554 1555 /* 1556 * Degrade ref count to the usual model with a single shared 1557 * atomic_t counter 1558 */ 1559 percpu_ref_kill(&srv_path->ids_inflight_ref); 1560 1561 /* Wait for all completion */ 1562 wait_for_completion(&srv_path->complete_done); 1563 1564 rtrs_srv_destroy_path_files(srv_path); 1565 1566 /* Notify upper layer if we are the last path */ 1567 rtrs_srv_path_down(srv_path); 1568 1569 unmap_cont_bufs(srv_path); 1570 rtrs_srv_free_ops_ids(srv_path); 1571 1572 for (i = 0; i < srv_path->s.con_num; i++) { 1573 if (!srv_path->s.con[i]) 1574 continue; 1575 con = to_srv_con(srv_path->s.con[i]); 1576 rtrs_cq_qp_destroy(&con->c); 1577 rdma_destroy_id(con->c.cm_id); 1578 kfree(con); 1579 } 1580 rtrs_ib_dev_put(srv_path->s.dev); 1581 1582 del_path_from_srv(srv_path); 1583 put_srv(srv_path->srv); 1584 srv_path->srv = NULL; 1585 rtrs_srv_change_state(srv_path, RTRS_SRV_CLOSED); 1586 1587 kfree(srv_path->dma_addr); 1588 kfree(srv_path->s.con); 1589 free_path(srv_path); 1590 } 1591 1592 static int rtrs_rdma_do_accept(struct rtrs_srv_path *srv_path, 1593 struct rdma_cm_id *cm_id) 1594 { 1595 struct rtrs_srv_sess *srv = srv_path->srv; 1596 struct rtrs_msg_conn_rsp msg; 1597 struct rdma_conn_param param; 1598 int err; 1599 1600 param = (struct rdma_conn_param) { 1601 .rnr_retry_count = 7, 1602 .private_data = &msg, 1603 .private_data_len = sizeof(msg), 1604 }; 1605 1606 msg = (struct rtrs_msg_conn_rsp) { 1607 .magic = cpu_to_le16(RTRS_MAGIC), 1608 .version = cpu_to_le16(RTRS_PROTO_VER), 1609 .queue_depth = cpu_to_le16(srv->queue_depth), 1610 .max_io_size = cpu_to_le32(max_chunk_size - MAX_HDR_SIZE), 1611 .max_hdr_size = cpu_to_le32(MAX_HDR_SIZE), 1612 }; 1613 1614 if (always_invalidate) 1615 msg.flags = cpu_to_le32(RTRS_MSG_NEW_RKEY_F); 1616 1617 err = rdma_accept(cm_id, ¶m); 1618 if (err) 1619 pr_err("rdma_accept(), err: %d\n", err); 1620 1621 return err; 1622 } 1623 1624 static int rtrs_rdma_do_reject(struct rdma_cm_id *cm_id, int errno) 1625 { 1626 struct rtrs_msg_conn_rsp msg; 1627 int err; 1628 1629 msg = (struct rtrs_msg_conn_rsp) { 1630 .magic = cpu_to_le16(RTRS_MAGIC), 1631 .version = cpu_to_le16(RTRS_PROTO_VER), 1632 .errno = cpu_to_le16(errno), 1633 }; 1634 1635 err = rdma_reject(cm_id, &msg, sizeof(msg), IB_CM_REJ_CONSUMER_DEFINED); 1636 if (err) 1637 pr_err("rdma_reject(), err: %d\n", err); 1638 1639 /* Bounce errno back */ 1640 return errno; 1641 } 1642 1643 static struct rtrs_srv_path * 1644 __find_path(struct rtrs_srv_sess *srv, const uuid_t *sess_uuid) 1645 { 1646 struct rtrs_srv_path *srv_path; 1647 1648 list_for_each_entry(srv_path, &srv->paths_list, s.entry) { 1649 if (uuid_equal(&srv_path->s.uuid, sess_uuid)) 1650 return srv_path; 1651 } 1652 1653 return NULL; 1654 } 1655 1656 static int create_con(struct rtrs_srv_path *srv_path, 1657 struct rdma_cm_id *cm_id, 1658 unsigned int cid) 1659 { 1660 struct rtrs_srv_sess *srv = srv_path->srv; 1661 struct rtrs_path *s = &srv_path->s; 1662 struct rtrs_srv_con *con; 1663 1664 u32 cq_num, max_send_wr, max_recv_wr, wr_limit; 1665 int err, cq_vector; 1666 1667 con = kzalloc(sizeof(*con), GFP_KERNEL); 1668 if (!con) { 1669 err = -ENOMEM; 1670 goto err; 1671 } 1672 1673 spin_lock_init(&con->rsp_wr_wait_lock); 1674 INIT_LIST_HEAD(&con->rsp_wr_wait_list); 1675 con->c.cm_id = cm_id; 1676 con->c.path = &srv_path->s; 1677 con->c.cid = cid; 1678 atomic_set(&con->c.wr_cnt, 1); 1679 wr_limit = srv_path->s.dev->ib_dev->attrs.max_qp_wr; 1680 1681 if (con->c.cid == 0) { 1682 /* 1683 * All receive and all send (each requiring invalidate) 1684 * + 2 for drain and heartbeat 1685 */ 1686 max_send_wr = min_t(int, wr_limit, 1687 SERVICE_CON_QUEUE_DEPTH * 2 + 2); 1688 max_recv_wr = max_send_wr; 1689 s->signal_interval = min_not_zero(srv->queue_depth, 1690 (size_t)SERVICE_CON_QUEUE_DEPTH); 1691 } else { 1692 /* when always_invlaidate enalbed, we need linv+rinv+mr+imm */ 1693 if (always_invalidate) 1694 max_send_wr = 1695 min_t(int, wr_limit, 1696 srv->queue_depth * (1 + 4) + 1); 1697 else 1698 max_send_wr = 1699 min_t(int, wr_limit, 1700 srv->queue_depth * (1 + 2) + 1); 1701 1702 max_recv_wr = srv->queue_depth + 1; 1703 } 1704 cq_num = max_send_wr + max_recv_wr; 1705 atomic_set(&con->c.sq_wr_avail, max_send_wr); 1706 cq_vector = rtrs_srv_get_next_cq_vector(srv_path); 1707 1708 /* TODO: SOFTIRQ can be faster, but be careful with softirq context */ 1709 err = rtrs_cq_qp_create(&srv_path->s, &con->c, 1, cq_vector, cq_num, 1710 max_send_wr, max_recv_wr, 1711 IB_POLL_WORKQUEUE); 1712 if (err) { 1713 rtrs_err(s, "rtrs_cq_qp_create(), err: %d\n", err); 1714 goto free_con; 1715 } 1716 if (con->c.cid == 0) { 1717 err = post_recv_info_req(con); 1718 if (err) 1719 goto free_cqqp; 1720 } 1721 WARN_ON(srv_path->s.con[cid]); 1722 srv_path->s.con[cid] = &con->c; 1723 1724 /* 1725 * Change context from server to current connection. The other 1726 * way is to use cm_id->qp->qp_context, which does not work on OFED. 1727 */ 1728 cm_id->context = &con->c; 1729 1730 return 0; 1731 1732 free_cqqp: 1733 rtrs_cq_qp_destroy(&con->c); 1734 free_con: 1735 kfree(con); 1736 1737 err: 1738 return err; 1739 } 1740 1741 static struct rtrs_srv_path *__alloc_path(struct rtrs_srv_sess *srv, 1742 struct rdma_cm_id *cm_id, 1743 unsigned int con_num, 1744 unsigned int recon_cnt, 1745 const uuid_t *uuid) 1746 { 1747 struct rtrs_srv_path *srv_path; 1748 int err = -ENOMEM; 1749 char str[NAME_MAX]; 1750 struct rtrs_addr path; 1751 1752 if (srv->paths_num >= MAX_PATHS_NUM) { 1753 err = -ECONNRESET; 1754 goto err; 1755 } 1756 if (__is_path_w_addr_exists(srv, &cm_id->route.addr)) { 1757 err = -EEXIST; 1758 pr_err("Path with same addr exists\n"); 1759 goto err; 1760 } 1761 srv_path = kzalloc(sizeof(*srv_path), GFP_KERNEL); 1762 if (!srv_path) 1763 goto err; 1764 1765 srv_path->stats = kzalloc(sizeof(*srv_path->stats), GFP_KERNEL); 1766 if (!srv_path->stats) 1767 goto err_free_sess; 1768 1769 srv_path->stats->rdma_stats = alloc_percpu(struct rtrs_srv_stats_rdma_stats); 1770 if (!srv_path->stats->rdma_stats) 1771 goto err_free_stats; 1772 1773 srv_path->stats->srv_path = srv_path; 1774 1775 srv_path->dma_addr = kcalloc(srv->queue_depth, 1776 sizeof(*srv_path->dma_addr), 1777 GFP_KERNEL); 1778 if (!srv_path->dma_addr) 1779 goto err_free_percpu; 1780 1781 srv_path->s.con = kcalloc(con_num, sizeof(*srv_path->s.con), 1782 GFP_KERNEL); 1783 if (!srv_path->s.con) 1784 goto err_free_dma_addr; 1785 1786 srv_path->state = RTRS_SRV_CONNECTING; 1787 srv_path->srv = srv; 1788 srv_path->cur_cq_vector = -1; 1789 srv_path->s.dst_addr = cm_id->route.addr.dst_addr; 1790 srv_path->s.src_addr = cm_id->route.addr.src_addr; 1791 1792 /* temporary until receiving session-name from client */ 1793 path.src = &srv_path->s.src_addr; 1794 path.dst = &srv_path->s.dst_addr; 1795 rtrs_addr_to_str(&path, str, sizeof(str)); 1796 strscpy(srv_path->s.sessname, str, sizeof(srv_path->s.sessname)); 1797 1798 srv_path->s.con_num = con_num; 1799 srv_path->s.irq_con_num = con_num; 1800 srv_path->s.recon_cnt = recon_cnt; 1801 uuid_copy(&srv_path->s.uuid, uuid); 1802 spin_lock_init(&srv_path->state_lock); 1803 INIT_WORK(&srv_path->close_work, rtrs_srv_close_work); 1804 rtrs_srv_init_hb(srv_path); 1805 1806 srv_path->s.dev = rtrs_ib_dev_find_or_add(cm_id->device, &dev_pd); 1807 if (!srv_path->s.dev) { 1808 err = -ENOMEM; 1809 goto err_free_con; 1810 } 1811 err = map_cont_bufs(srv_path); 1812 if (err) 1813 goto err_put_dev; 1814 1815 err = rtrs_srv_alloc_ops_ids(srv_path); 1816 if (err) 1817 goto err_unmap_bufs; 1818 1819 __add_path_to_srv(srv, srv_path); 1820 1821 return srv_path; 1822 1823 err_unmap_bufs: 1824 unmap_cont_bufs(srv_path); 1825 err_put_dev: 1826 rtrs_ib_dev_put(srv_path->s.dev); 1827 err_free_con: 1828 kfree(srv_path->s.con); 1829 err_free_dma_addr: 1830 kfree(srv_path->dma_addr); 1831 err_free_percpu: 1832 free_percpu(srv_path->stats->rdma_stats); 1833 err_free_stats: 1834 kfree(srv_path->stats); 1835 err_free_sess: 1836 kfree(srv_path); 1837 err: 1838 return ERR_PTR(err); 1839 } 1840 1841 static int rtrs_rdma_connect(struct rdma_cm_id *cm_id, 1842 const struct rtrs_msg_conn_req *msg, 1843 size_t len) 1844 { 1845 struct rtrs_srv_ctx *ctx = cm_id->context; 1846 struct rtrs_srv_path *srv_path; 1847 struct rtrs_srv_sess *srv; 1848 1849 u16 version, con_num, cid; 1850 u16 recon_cnt; 1851 int err = -ECONNRESET; 1852 1853 if (len < sizeof(*msg)) { 1854 pr_err("Invalid RTRS connection request\n"); 1855 goto reject_w_err; 1856 } 1857 if (le16_to_cpu(msg->magic) != RTRS_MAGIC) { 1858 pr_err("Invalid RTRS magic\n"); 1859 goto reject_w_err; 1860 } 1861 version = le16_to_cpu(msg->version); 1862 if (version >> 8 != RTRS_PROTO_VER_MAJOR) { 1863 pr_err("Unsupported major RTRS version: %d, expected %d\n", 1864 version >> 8, RTRS_PROTO_VER_MAJOR); 1865 goto reject_w_err; 1866 } 1867 con_num = le16_to_cpu(msg->cid_num); 1868 if (con_num > 4096) { 1869 /* Sanity check */ 1870 pr_err("Too many connections requested: %d\n", con_num); 1871 goto reject_w_err; 1872 } 1873 cid = le16_to_cpu(msg->cid); 1874 if (cid >= con_num) { 1875 /* Sanity check */ 1876 pr_err("Incorrect cid: %d >= %d\n", cid, con_num); 1877 goto reject_w_err; 1878 } 1879 recon_cnt = le16_to_cpu(msg->recon_cnt); 1880 srv = get_or_create_srv(ctx, &msg->paths_uuid, msg->first_conn); 1881 if (IS_ERR(srv)) { 1882 err = PTR_ERR(srv); 1883 pr_err("get_or_create_srv(), error %d\n", err); 1884 goto reject_w_err; 1885 } 1886 mutex_lock(&srv->paths_mutex); 1887 srv_path = __find_path(srv, &msg->sess_uuid); 1888 if (srv_path) { 1889 struct rtrs_path *s = &srv_path->s; 1890 1891 /* Session already holds a reference */ 1892 put_srv(srv); 1893 1894 if (srv_path->state != RTRS_SRV_CONNECTING) { 1895 rtrs_err(s, "Session in wrong state: %s\n", 1896 rtrs_srv_state_str(srv_path->state)); 1897 mutex_unlock(&srv->paths_mutex); 1898 goto reject_w_err; 1899 } 1900 /* 1901 * Sanity checks 1902 */ 1903 if (con_num != s->con_num || cid >= s->con_num) { 1904 rtrs_err(s, "Incorrect request: %d, %d\n", 1905 cid, con_num); 1906 mutex_unlock(&srv->paths_mutex); 1907 goto reject_w_err; 1908 } 1909 if (s->con[cid]) { 1910 rtrs_err(s, "Connection already exists: %d\n", 1911 cid); 1912 mutex_unlock(&srv->paths_mutex); 1913 goto reject_w_err; 1914 } 1915 } else { 1916 srv_path = __alloc_path(srv, cm_id, con_num, recon_cnt, 1917 &msg->sess_uuid); 1918 if (IS_ERR(srv_path)) { 1919 mutex_unlock(&srv->paths_mutex); 1920 put_srv(srv); 1921 err = PTR_ERR(srv_path); 1922 pr_err("RTRS server session allocation failed: %d\n", err); 1923 goto reject_w_err; 1924 } 1925 } 1926 err = create_con(srv_path, cm_id, cid); 1927 if (err) { 1928 rtrs_err((&srv_path->s), "create_con(), error %d\n", err); 1929 rtrs_rdma_do_reject(cm_id, err); 1930 /* 1931 * Since session has other connections we follow normal way 1932 * through workqueue, but still return an error to tell cma.c 1933 * to call rdma_destroy_id() for current connection. 1934 */ 1935 goto close_and_return_err; 1936 } 1937 err = rtrs_rdma_do_accept(srv_path, cm_id); 1938 if (err) { 1939 rtrs_err((&srv_path->s), "rtrs_rdma_do_accept(), error %d\n", err); 1940 rtrs_rdma_do_reject(cm_id, err); 1941 /* 1942 * Since current connection was successfully added to the 1943 * session we follow normal way through workqueue to close the 1944 * session, thus return 0 to tell cma.c we call 1945 * rdma_destroy_id() ourselves. 1946 */ 1947 err = 0; 1948 goto close_and_return_err; 1949 } 1950 mutex_unlock(&srv->paths_mutex); 1951 1952 return 0; 1953 1954 reject_w_err: 1955 return rtrs_rdma_do_reject(cm_id, err); 1956 1957 close_and_return_err: 1958 mutex_unlock(&srv->paths_mutex); 1959 close_path(srv_path); 1960 1961 return err; 1962 } 1963 1964 static int rtrs_srv_rdma_cm_handler(struct rdma_cm_id *cm_id, 1965 struct rdma_cm_event *ev) 1966 { 1967 struct rtrs_srv_path *srv_path = NULL; 1968 struct rtrs_path *s = NULL; 1969 struct rtrs_con *c = NULL; 1970 1971 if (ev->event == RDMA_CM_EVENT_CONNECT_REQUEST) 1972 /* 1973 * In case of error cma.c will destroy cm_id, 1974 * see cma_process_remove() 1975 */ 1976 return rtrs_rdma_connect(cm_id, ev->param.conn.private_data, 1977 ev->param.conn.private_data_len); 1978 1979 c = cm_id->context; 1980 s = c->path; 1981 srv_path = to_srv_path(s); 1982 1983 switch (ev->event) { 1984 case RDMA_CM_EVENT_ESTABLISHED: 1985 /* Nothing here */ 1986 break; 1987 case RDMA_CM_EVENT_REJECTED: 1988 case RDMA_CM_EVENT_CONNECT_ERROR: 1989 case RDMA_CM_EVENT_UNREACHABLE: 1990 rtrs_err(s, "CM error (CM event: %s, err: %d)\n", 1991 rdma_event_msg(ev->event), ev->status); 1992 fallthrough; 1993 case RDMA_CM_EVENT_DISCONNECTED: 1994 case RDMA_CM_EVENT_ADDR_CHANGE: 1995 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 1996 case RDMA_CM_EVENT_DEVICE_REMOVAL: 1997 close_path(srv_path); 1998 break; 1999 default: 2000 pr_err("Ignoring unexpected CM event %s, err %d\n", 2001 rdma_event_msg(ev->event), ev->status); 2002 break; 2003 } 2004 2005 return 0; 2006 } 2007 2008 static struct rdma_cm_id *rtrs_srv_cm_init(struct rtrs_srv_ctx *ctx, 2009 struct sockaddr *addr, 2010 enum rdma_ucm_port_space ps) 2011 { 2012 struct rdma_cm_id *cm_id; 2013 int ret; 2014 2015 cm_id = rdma_create_id(&init_net, rtrs_srv_rdma_cm_handler, 2016 ctx, ps, IB_QPT_RC); 2017 if (IS_ERR(cm_id)) { 2018 ret = PTR_ERR(cm_id); 2019 pr_err("Creating id for RDMA connection failed, err: %d\n", 2020 ret); 2021 goto err_out; 2022 } 2023 ret = rdma_bind_addr(cm_id, addr); 2024 if (ret) { 2025 pr_err("Binding RDMA address failed, err: %d\n", ret); 2026 goto err_cm; 2027 } 2028 ret = rdma_listen(cm_id, 64); 2029 if (ret) { 2030 pr_err("Listening on RDMA connection failed, err: %d\n", 2031 ret); 2032 goto err_cm; 2033 } 2034 2035 return cm_id; 2036 2037 err_cm: 2038 rdma_destroy_id(cm_id); 2039 err_out: 2040 2041 return ERR_PTR(ret); 2042 } 2043 2044 static int rtrs_srv_rdma_init(struct rtrs_srv_ctx *ctx, u16 port) 2045 { 2046 struct sockaddr_in6 sin = { 2047 .sin6_family = AF_INET6, 2048 .sin6_addr = IN6ADDR_ANY_INIT, 2049 .sin6_port = htons(port), 2050 }; 2051 struct sockaddr_ib sib = { 2052 .sib_family = AF_IB, 2053 .sib_sid = cpu_to_be64(RDMA_IB_IP_PS_IB | port), 2054 .sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL), 2055 .sib_pkey = cpu_to_be16(0xffff), 2056 }; 2057 struct rdma_cm_id *cm_ip, *cm_ib; 2058 int ret; 2059 2060 /* 2061 * We accept both IPoIB and IB connections, so we need to keep 2062 * two cm id's, one for each socket type and port space. 2063 * If the cm initialization of one of the id's fails, we abort 2064 * everything. 2065 */ 2066 cm_ip = rtrs_srv_cm_init(ctx, (struct sockaddr *)&sin, RDMA_PS_TCP); 2067 if (IS_ERR(cm_ip)) 2068 return PTR_ERR(cm_ip); 2069 2070 cm_ib = rtrs_srv_cm_init(ctx, (struct sockaddr *)&sib, RDMA_PS_IB); 2071 if (IS_ERR(cm_ib)) { 2072 ret = PTR_ERR(cm_ib); 2073 goto free_cm_ip; 2074 } 2075 2076 ctx->cm_id_ip = cm_ip; 2077 ctx->cm_id_ib = cm_ib; 2078 2079 return 0; 2080 2081 free_cm_ip: 2082 rdma_destroy_id(cm_ip); 2083 2084 return ret; 2085 } 2086 2087 static struct rtrs_srv_ctx *alloc_srv_ctx(struct rtrs_srv_ops *ops) 2088 { 2089 struct rtrs_srv_ctx *ctx; 2090 2091 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 2092 if (!ctx) 2093 return NULL; 2094 2095 ctx->ops = *ops; 2096 mutex_init(&ctx->srv_mutex); 2097 INIT_LIST_HEAD(&ctx->srv_list); 2098 2099 return ctx; 2100 } 2101 2102 static void free_srv_ctx(struct rtrs_srv_ctx *ctx) 2103 { 2104 WARN_ON(!list_empty(&ctx->srv_list)); 2105 mutex_destroy(&ctx->srv_mutex); 2106 kfree(ctx); 2107 } 2108 2109 static int rtrs_srv_add_one(struct ib_device *device) 2110 { 2111 struct rtrs_srv_ctx *ctx; 2112 int ret = 0; 2113 2114 mutex_lock(&ib_ctx.ib_dev_mutex); 2115 if (ib_ctx.ib_dev_count) 2116 goto out; 2117 2118 /* 2119 * Since our CM IDs are NOT bound to any ib device we will create them 2120 * only once 2121 */ 2122 ctx = ib_ctx.srv_ctx; 2123 ret = rtrs_srv_rdma_init(ctx, ib_ctx.port); 2124 if (ret) { 2125 /* 2126 * We errored out here. 2127 * According to the ib code, if we encounter an error here then the 2128 * error code is ignored, and no more calls to our ops are made. 2129 */ 2130 pr_err("Failed to initialize RDMA connection"); 2131 goto err_out; 2132 } 2133 2134 out: 2135 /* 2136 * Keep a track on the number of ib devices added 2137 */ 2138 ib_ctx.ib_dev_count++; 2139 2140 err_out: 2141 mutex_unlock(&ib_ctx.ib_dev_mutex); 2142 return ret; 2143 } 2144 2145 static void rtrs_srv_remove_one(struct ib_device *device, void *client_data) 2146 { 2147 struct rtrs_srv_ctx *ctx; 2148 2149 mutex_lock(&ib_ctx.ib_dev_mutex); 2150 ib_ctx.ib_dev_count--; 2151 2152 if (ib_ctx.ib_dev_count) 2153 goto out; 2154 2155 /* 2156 * Since our CM IDs are NOT bound to any ib device we will remove them 2157 * only once, when the last device is removed 2158 */ 2159 ctx = ib_ctx.srv_ctx; 2160 rdma_destroy_id(ctx->cm_id_ip); 2161 rdma_destroy_id(ctx->cm_id_ib); 2162 2163 out: 2164 mutex_unlock(&ib_ctx.ib_dev_mutex); 2165 } 2166 2167 static struct ib_client rtrs_srv_client = { 2168 .name = "rtrs_server", 2169 .add = rtrs_srv_add_one, 2170 .remove = rtrs_srv_remove_one 2171 }; 2172 2173 /** 2174 * rtrs_srv_open() - open RTRS server context 2175 * @ops: callback functions 2176 * @port: port to listen on 2177 * 2178 * Creates server context with specified callbacks. 2179 * 2180 * Return a valid pointer on success otherwise PTR_ERR. 2181 */ 2182 struct rtrs_srv_ctx *rtrs_srv_open(struct rtrs_srv_ops *ops, u16 port) 2183 { 2184 struct rtrs_srv_ctx *ctx; 2185 int err; 2186 2187 ctx = alloc_srv_ctx(ops); 2188 if (!ctx) 2189 return ERR_PTR(-ENOMEM); 2190 2191 mutex_init(&ib_ctx.ib_dev_mutex); 2192 ib_ctx.srv_ctx = ctx; 2193 ib_ctx.port = port; 2194 2195 err = ib_register_client(&rtrs_srv_client); 2196 if (err) { 2197 free_srv_ctx(ctx); 2198 return ERR_PTR(err); 2199 } 2200 2201 return ctx; 2202 } 2203 EXPORT_SYMBOL(rtrs_srv_open); 2204 2205 static void close_paths(struct rtrs_srv_sess *srv) 2206 { 2207 struct rtrs_srv_path *srv_path; 2208 2209 mutex_lock(&srv->paths_mutex); 2210 list_for_each_entry(srv_path, &srv->paths_list, s.entry) 2211 close_path(srv_path); 2212 mutex_unlock(&srv->paths_mutex); 2213 } 2214 2215 static void close_ctx(struct rtrs_srv_ctx *ctx) 2216 { 2217 struct rtrs_srv_sess *srv; 2218 2219 mutex_lock(&ctx->srv_mutex); 2220 list_for_each_entry(srv, &ctx->srv_list, ctx_list) 2221 close_paths(srv); 2222 mutex_unlock(&ctx->srv_mutex); 2223 flush_workqueue(rtrs_wq); 2224 } 2225 2226 /** 2227 * rtrs_srv_close() - close RTRS server context 2228 * @ctx: pointer to server context 2229 * 2230 * Closes RTRS server context with all client sessions. 2231 */ 2232 void rtrs_srv_close(struct rtrs_srv_ctx *ctx) 2233 { 2234 ib_unregister_client(&rtrs_srv_client); 2235 mutex_destroy(&ib_ctx.ib_dev_mutex); 2236 close_ctx(ctx); 2237 free_srv_ctx(ctx); 2238 } 2239 EXPORT_SYMBOL(rtrs_srv_close); 2240 2241 static int check_module_params(void) 2242 { 2243 if (sess_queue_depth < 1 || sess_queue_depth > MAX_SESS_QUEUE_DEPTH) { 2244 pr_err("Invalid sess_queue_depth value %d, has to be >= %d, <= %d.\n", 2245 sess_queue_depth, 1, MAX_SESS_QUEUE_DEPTH); 2246 return -EINVAL; 2247 } 2248 if (max_chunk_size < MIN_CHUNK_SIZE || !is_power_of_2(max_chunk_size)) { 2249 pr_err("Invalid max_chunk_size value %d, has to be >= %d and should be power of two.\n", 2250 max_chunk_size, MIN_CHUNK_SIZE); 2251 return -EINVAL; 2252 } 2253 2254 /* 2255 * Check if IB immediate data size is enough to hold the mem_id and the 2256 * offset inside the memory chunk 2257 */ 2258 if ((ilog2(sess_queue_depth - 1) + 1) + 2259 (ilog2(max_chunk_size - 1) + 1) > MAX_IMM_PAYL_BITS) { 2260 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", 2261 MAX_IMM_PAYL_BITS, sess_queue_depth, max_chunk_size); 2262 return -EINVAL; 2263 } 2264 2265 return 0; 2266 } 2267 2268 static int __init rtrs_server_init(void) 2269 { 2270 int err; 2271 2272 pr_info("Loading module %s, proto %s: (max_chunk_size: %d (pure IO %ld, headers %ld) , sess_queue_depth: %d, always_invalidate: %d)\n", 2273 KBUILD_MODNAME, RTRS_PROTO_VER_STRING, 2274 max_chunk_size, max_chunk_size - MAX_HDR_SIZE, MAX_HDR_SIZE, 2275 sess_queue_depth, always_invalidate); 2276 2277 rtrs_rdma_dev_pd_init(0, &dev_pd); 2278 2279 err = check_module_params(); 2280 if (err) { 2281 pr_err("Failed to load module, invalid module parameters, err: %d\n", 2282 err); 2283 return err; 2284 } 2285 err = class_register(&rtrs_dev_class); 2286 if (err) 2287 goto out_err; 2288 2289 rtrs_wq = alloc_workqueue("rtrs_server_wq", 0, 0); 2290 if (!rtrs_wq) { 2291 err = -ENOMEM; 2292 goto out_dev_class; 2293 } 2294 2295 return 0; 2296 2297 out_dev_class: 2298 class_unregister(&rtrs_dev_class); 2299 out_err: 2300 return err; 2301 } 2302 2303 static void __exit rtrs_server_exit(void) 2304 { 2305 destroy_workqueue(rtrs_wq); 2306 class_unregister(&rtrs_dev_class); 2307 rtrs_rdma_dev_pd_deinit(&dev_pd); 2308 } 2309 2310 module_init(rtrs_server_init); 2311 module_exit(rtrs_server_exit); 2312