1 /* 2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved. 3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved. 4 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved. 5 * 6 * This software is available to you under a choice of one of two 7 * licenses. You may choose to be licensed under the terms of the GNU 8 * General Public License (GPL) Version 2, available from the file 9 * COPYING in the main directory of this source tree, or the 10 * OpenIB.org BSD license below: 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer. 19 * 20 * - Redistributions in binary form must reproduce the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer in the documentation and/or other materials 23 * provided with the distribution. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 * SOFTWARE. 33 */ 34 #include <linux/kernel.h> 35 #include <linux/module.h> 36 #include <linux/slab.h> 37 #include <linux/delay.h> 38 39 #include "iscsi_iser.h" 40 41 #define ISCSI_ISER_MAX_CONN 8 42 #define ISER_MAX_RX_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN) 43 #define ISER_MAX_TX_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN) 44 #define ISER_MAX_CQ_LEN (ISER_MAX_RX_LEN + ISER_MAX_TX_LEN + \ 45 ISCSI_ISER_MAX_CONN) 46 47 static int iser_cq_poll_limit = 512; 48 49 static void iser_cq_tasklet_fn(unsigned long data); 50 static void iser_cq_callback(struct ib_cq *cq, void *cq_context); 51 52 static void iser_cq_event_callback(struct ib_event *cause, void *context) 53 { 54 iser_err("cq event %s (%d)\n", 55 ib_event_msg(cause->event), cause->event); 56 } 57 58 static void iser_qp_event_callback(struct ib_event *cause, void *context) 59 { 60 iser_err("qp event %s (%d)\n", 61 ib_event_msg(cause->event), cause->event); 62 } 63 64 static void iser_event_handler(struct ib_event_handler *handler, 65 struct ib_event *event) 66 { 67 iser_err("async event %s (%d) on device %s port %d\n", 68 ib_event_msg(event->event), event->event, 69 event->device->name, event->element.port_num); 70 } 71 72 /** 73 * iser_create_device_ib_res - creates Protection Domain (PD), Completion 74 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with 75 * the adapator. 76 * 77 * returns 0 on success, -1 on failure 78 */ 79 static int iser_create_device_ib_res(struct iser_device *device) 80 { 81 struct ib_device_attr *dev_attr = &device->dev_attr; 82 int ret, i, max_cqe; 83 84 ret = ib_query_device(device->ib_device, dev_attr); 85 if (ret) { 86 pr_warn("Query device failed for %s\n", device->ib_device->name); 87 return ret; 88 } 89 90 ret = iser_assign_reg_ops(device); 91 if (ret) 92 return ret; 93 94 device->comps_used = min_t(int, num_online_cpus(), 95 device->ib_device->num_comp_vectors); 96 97 device->comps = kcalloc(device->comps_used, sizeof(*device->comps), 98 GFP_KERNEL); 99 if (!device->comps) 100 goto comps_err; 101 102 max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe); 103 104 iser_info("using %d CQs, device %s supports %d vectors max_cqe %d\n", 105 device->comps_used, device->ib_device->name, 106 device->ib_device->num_comp_vectors, max_cqe); 107 108 device->pd = ib_alloc_pd(device->ib_device); 109 if (IS_ERR(device->pd)) 110 goto pd_err; 111 112 for (i = 0; i < device->comps_used; i++) { 113 struct ib_cq_init_attr cq_attr = {}; 114 struct iser_comp *comp = &device->comps[i]; 115 116 comp->device = device; 117 cq_attr.cqe = max_cqe; 118 cq_attr.comp_vector = i; 119 comp->cq = ib_create_cq(device->ib_device, 120 iser_cq_callback, 121 iser_cq_event_callback, 122 (void *)comp, 123 &cq_attr); 124 if (IS_ERR(comp->cq)) { 125 comp->cq = NULL; 126 goto cq_err; 127 } 128 129 if (ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP)) 130 goto cq_err; 131 132 tasklet_init(&comp->tasklet, iser_cq_tasklet_fn, 133 (unsigned long)comp); 134 } 135 136 if (!iser_always_reg) { 137 int access = IB_ACCESS_LOCAL_WRITE | 138 IB_ACCESS_REMOTE_WRITE | 139 IB_ACCESS_REMOTE_READ; 140 141 device->mr = ib_get_dma_mr(device->pd, access); 142 if (IS_ERR(device->mr)) 143 goto dma_mr_err; 144 } 145 146 INIT_IB_EVENT_HANDLER(&device->event_handler, device->ib_device, 147 iser_event_handler); 148 if (ib_register_event_handler(&device->event_handler)) 149 goto handler_err; 150 151 return 0; 152 153 handler_err: 154 if (device->mr) 155 ib_dereg_mr(device->mr); 156 dma_mr_err: 157 for (i = 0; i < device->comps_used; i++) 158 tasklet_kill(&device->comps[i].tasklet); 159 cq_err: 160 for (i = 0; i < device->comps_used; i++) { 161 struct iser_comp *comp = &device->comps[i]; 162 163 if (comp->cq) 164 ib_destroy_cq(comp->cq); 165 } 166 ib_dealloc_pd(device->pd); 167 pd_err: 168 kfree(device->comps); 169 comps_err: 170 iser_err("failed to allocate an IB resource\n"); 171 return -1; 172 } 173 174 /** 175 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR, 176 * CQ and PD created with the device associated with the adapator. 177 */ 178 static void iser_free_device_ib_res(struct iser_device *device) 179 { 180 int i; 181 182 for (i = 0; i < device->comps_used; i++) { 183 struct iser_comp *comp = &device->comps[i]; 184 185 tasklet_kill(&comp->tasklet); 186 ib_destroy_cq(comp->cq); 187 comp->cq = NULL; 188 } 189 190 (void)ib_unregister_event_handler(&device->event_handler); 191 if (device->mr) 192 (void)ib_dereg_mr(device->mr); 193 ib_dealloc_pd(device->pd); 194 195 kfree(device->comps); 196 device->comps = NULL; 197 198 device->mr = NULL; 199 device->pd = NULL; 200 } 201 202 /** 203 * iser_alloc_fmr_pool - Creates FMR pool and page_vector 204 * 205 * returns 0 on success, or errno code on failure 206 */ 207 int iser_alloc_fmr_pool(struct ib_conn *ib_conn, 208 unsigned cmds_max, 209 unsigned int size) 210 { 211 struct iser_device *device = ib_conn->device; 212 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool; 213 struct iser_page_vec *page_vec; 214 struct iser_fr_desc *desc; 215 struct ib_fmr_pool *fmr_pool; 216 struct ib_fmr_pool_param params; 217 int ret; 218 219 INIT_LIST_HEAD(&fr_pool->list); 220 spin_lock_init(&fr_pool->lock); 221 222 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 223 if (!desc) 224 return -ENOMEM; 225 226 page_vec = kmalloc(sizeof(*page_vec) + (sizeof(u64) * size), 227 GFP_KERNEL); 228 if (!page_vec) { 229 ret = -ENOMEM; 230 goto err_frpl; 231 } 232 233 page_vec->pages = (u64 *)(page_vec + 1); 234 235 params.page_shift = SHIFT_4K; 236 params.max_pages_per_fmr = size; 237 /* make the pool size twice the max number of SCSI commands * 238 * the ML is expected to queue, watermark for unmap at 50% */ 239 params.pool_size = cmds_max * 2; 240 params.dirty_watermark = cmds_max; 241 params.cache = 0; 242 params.flush_function = NULL; 243 params.access = (IB_ACCESS_LOCAL_WRITE | 244 IB_ACCESS_REMOTE_WRITE | 245 IB_ACCESS_REMOTE_READ); 246 247 fmr_pool = ib_create_fmr_pool(device->pd, ¶ms); 248 if (IS_ERR(fmr_pool)) { 249 ret = PTR_ERR(fmr_pool); 250 iser_err("FMR allocation failed, err %d\n", ret); 251 goto err_fmr; 252 } 253 254 desc->rsc.page_vec = page_vec; 255 desc->rsc.fmr_pool = fmr_pool; 256 list_add(&desc->list, &fr_pool->list); 257 258 return 0; 259 260 err_fmr: 261 kfree(page_vec); 262 err_frpl: 263 kfree(desc); 264 265 return ret; 266 } 267 268 /** 269 * iser_free_fmr_pool - releases the FMR pool and page vec 270 */ 271 void iser_free_fmr_pool(struct ib_conn *ib_conn) 272 { 273 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool; 274 struct iser_fr_desc *desc; 275 276 desc = list_first_entry(&fr_pool->list, 277 struct iser_fr_desc, list); 278 list_del(&desc->list); 279 280 iser_info("freeing conn %p fmr pool %p\n", 281 ib_conn, desc->rsc.fmr_pool); 282 283 ib_destroy_fmr_pool(desc->rsc.fmr_pool); 284 kfree(desc->rsc.page_vec); 285 kfree(desc); 286 } 287 288 static int 289 iser_alloc_reg_res(struct ib_device *ib_device, 290 struct ib_pd *pd, 291 struct iser_reg_resources *res, 292 unsigned int size) 293 { 294 int ret; 295 296 res->mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG, size); 297 if (IS_ERR(res->mr)) { 298 ret = PTR_ERR(res->mr); 299 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret); 300 return ret; 301 } 302 res->mr_valid = 1; 303 304 return 0; 305 } 306 307 static void 308 iser_free_reg_res(struct iser_reg_resources *rsc) 309 { 310 ib_dereg_mr(rsc->mr); 311 } 312 313 static int 314 iser_alloc_pi_ctx(struct ib_device *ib_device, 315 struct ib_pd *pd, 316 struct iser_fr_desc *desc, 317 unsigned int size) 318 { 319 struct iser_pi_context *pi_ctx = NULL; 320 int ret; 321 322 desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL); 323 if (!desc->pi_ctx) 324 return -ENOMEM; 325 326 pi_ctx = desc->pi_ctx; 327 328 ret = iser_alloc_reg_res(ib_device, pd, &pi_ctx->rsc, size); 329 if (ret) { 330 iser_err("failed to allocate reg_resources\n"); 331 goto alloc_reg_res_err; 332 } 333 334 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2); 335 if (IS_ERR(pi_ctx->sig_mr)) { 336 ret = PTR_ERR(pi_ctx->sig_mr); 337 goto sig_mr_failure; 338 } 339 pi_ctx->sig_mr_valid = 1; 340 desc->pi_ctx->sig_protected = 0; 341 342 return 0; 343 344 sig_mr_failure: 345 iser_free_reg_res(&pi_ctx->rsc); 346 alloc_reg_res_err: 347 kfree(desc->pi_ctx); 348 349 return ret; 350 } 351 352 static void 353 iser_free_pi_ctx(struct iser_pi_context *pi_ctx) 354 { 355 iser_free_reg_res(&pi_ctx->rsc); 356 ib_dereg_mr(pi_ctx->sig_mr); 357 kfree(pi_ctx); 358 } 359 360 static struct iser_fr_desc * 361 iser_create_fastreg_desc(struct ib_device *ib_device, 362 struct ib_pd *pd, 363 bool pi_enable, 364 unsigned int size) 365 { 366 struct iser_fr_desc *desc; 367 int ret; 368 369 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 370 if (!desc) 371 return ERR_PTR(-ENOMEM); 372 373 ret = iser_alloc_reg_res(ib_device, pd, &desc->rsc, size); 374 if (ret) 375 goto reg_res_alloc_failure; 376 377 if (pi_enable) { 378 ret = iser_alloc_pi_ctx(ib_device, pd, desc, size); 379 if (ret) 380 goto pi_ctx_alloc_failure; 381 } 382 383 return desc; 384 385 pi_ctx_alloc_failure: 386 iser_free_reg_res(&desc->rsc); 387 reg_res_alloc_failure: 388 kfree(desc); 389 390 return ERR_PTR(ret); 391 } 392 393 /** 394 * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors 395 * for fast registration work requests. 396 * returns 0 on success, or errno code on failure 397 */ 398 int iser_alloc_fastreg_pool(struct ib_conn *ib_conn, 399 unsigned cmds_max, 400 unsigned int size) 401 { 402 struct iser_device *device = ib_conn->device; 403 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool; 404 struct iser_fr_desc *desc; 405 int i, ret; 406 407 INIT_LIST_HEAD(&fr_pool->list); 408 spin_lock_init(&fr_pool->lock); 409 fr_pool->size = 0; 410 for (i = 0; i < cmds_max; i++) { 411 desc = iser_create_fastreg_desc(device->ib_device, device->pd, 412 ib_conn->pi_support, size); 413 if (IS_ERR(desc)) { 414 ret = PTR_ERR(desc); 415 goto err; 416 } 417 418 list_add_tail(&desc->list, &fr_pool->list); 419 fr_pool->size++; 420 } 421 422 return 0; 423 424 err: 425 iser_free_fastreg_pool(ib_conn); 426 return ret; 427 } 428 429 /** 430 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors 431 */ 432 void iser_free_fastreg_pool(struct ib_conn *ib_conn) 433 { 434 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool; 435 struct iser_fr_desc *desc, *tmp; 436 int i = 0; 437 438 if (list_empty(&fr_pool->list)) 439 return; 440 441 iser_info("freeing conn %p fr pool\n", ib_conn); 442 443 list_for_each_entry_safe(desc, tmp, &fr_pool->list, list) { 444 list_del(&desc->list); 445 iser_free_reg_res(&desc->rsc); 446 if (desc->pi_ctx) 447 iser_free_pi_ctx(desc->pi_ctx); 448 kfree(desc); 449 ++i; 450 } 451 452 if (i < fr_pool->size) 453 iser_warn("pool still has %d regions registered\n", 454 fr_pool->size - i); 455 } 456 457 /** 458 * iser_create_ib_conn_res - Queue-Pair (QP) 459 * 460 * returns 0 on success, -1 on failure 461 */ 462 static int iser_create_ib_conn_res(struct ib_conn *ib_conn) 463 { 464 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn, 465 ib_conn); 466 struct iser_device *device; 467 struct ib_device_attr *dev_attr; 468 struct ib_qp_init_attr init_attr; 469 int ret = -ENOMEM; 470 int index, min_index = 0; 471 472 BUG_ON(ib_conn->device == NULL); 473 474 device = ib_conn->device; 475 dev_attr = &device->dev_attr; 476 477 memset(&init_attr, 0, sizeof init_attr); 478 479 mutex_lock(&ig.connlist_mutex); 480 /* select the CQ with the minimal number of usages */ 481 for (index = 0; index < device->comps_used; index++) { 482 if (device->comps[index].active_qps < 483 device->comps[min_index].active_qps) 484 min_index = index; 485 } 486 ib_conn->comp = &device->comps[min_index]; 487 ib_conn->comp->active_qps++; 488 mutex_unlock(&ig.connlist_mutex); 489 iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn); 490 491 init_attr.event_handler = iser_qp_event_callback; 492 init_attr.qp_context = (void *)ib_conn; 493 init_attr.send_cq = ib_conn->comp->cq; 494 init_attr.recv_cq = ib_conn->comp->cq; 495 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS; 496 init_attr.cap.max_send_sge = 2; 497 init_attr.cap.max_recv_sge = 1; 498 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 499 init_attr.qp_type = IB_QPT_RC; 500 if (ib_conn->pi_support) { 501 init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1; 502 init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN; 503 iser_conn->max_cmds = 504 ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS); 505 } else { 506 if (dev_attr->max_qp_wr > ISER_QP_MAX_REQ_DTOS) { 507 init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1; 508 iser_conn->max_cmds = 509 ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS); 510 } else { 511 init_attr.cap.max_send_wr = dev_attr->max_qp_wr; 512 iser_conn->max_cmds = 513 ISER_GET_MAX_XMIT_CMDS(dev_attr->max_qp_wr); 514 iser_dbg("device %s supports max_send_wr %d\n", 515 device->ib_device->name, dev_attr->max_qp_wr); 516 } 517 } 518 519 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr); 520 if (ret) 521 goto out_err; 522 523 ib_conn->qp = ib_conn->cma_id->qp; 524 iser_info("setting conn %p cma_id %p qp %p\n", 525 ib_conn, ib_conn->cma_id, 526 ib_conn->cma_id->qp); 527 return ret; 528 529 out_err: 530 mutex_lock(&ig.connlist_mutex); 531 ib_conn->comp->active_qps--; 532 mutex_unlock(&ig.connlist_mutex); 533 iser_err("unable to alloc mem or create resource, err %d\n", ret); 534 535 return ret; 536 } 537 538 /** 539 * based on the resolved device node GUID see if there already allocated 540 * device for this device. If there's no such, create one. 541 */ 542 static 543 struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id) 544 { 545 struct iser_device *device; 546 547 mutex_lock(&ig.device_list_mutex); 548 549 list_for_each_entry(device, &ig.device_list, ig_list) 550 /* find if there's a match using the node GUID */ 551 if (device->ib_device->node_guid == cma_id->device->node_guid) 552 goto inc_refcnt; 553 554 device = kzalloc(sizeof *device, GFP_KERNEL); 555 if (device == NULL) 556 goto out; 557 558 /* assign this device to the device */ 559 device->ib_device = cma_id->device; 560 /* init the device and link it into ig device list */ 561 if (iser_create_device_ib_res(device)) { 562 kfree(device); 563 device = NULL; 564 goto out; 565 } 566 list_add(&device->ig_list, &ig.device_list); 567 568 inc_refcnt: 569 device->refcount++; 570 out: 571 mutex_unlock(&ig.device_list_mutex); 572 return device; 573 } 574 575 /* if there's no demand for this device, release it */ 576 static void iser_device_try_release(struct iser_device *device) 577 { 578 mutex_lock(&ig.device_list_mutex); 579 device->refcount--; 580 iser_info("device %p refcount %d\n", device, device->refcount); 581 if (!device->refcount) { 582 iser_free_device_ib_res(device); 583 list_del(&device->ig_list); 584 kfree(device); 585 } 586 mutex_unlock(&ig.device_list_mutex); 587 } 588 589 /** 590 * Called with state mutex held 591 **/ 592 static int iser_conn_state_comp_exch(struct iser_conn *iser_conn, 593 enum iser_conn_state comp, 594 enum iser_conn_state exch) 595 { 596 int ret; 597 598 ret = (iser_conn->state == comp); 599 if (ret) 600 iser_conn->state = exch; 601 602 return ret; 603 } 604 605 void iser_release_work(struct work_struct *work) 606 { 607 struct iser_conn *iser_conn; 608 609 iser_conn = container_of(work, struct iser_conn, release_work); 610 611 /* Wait for conn_stop to complete */ 612 wait_for_completion(&iser_conn->stop_completion); 613 /* Wait for IB resouces cleanup to complete */ 614 wait_for_completion(&iser_conn->ib_completion); 615 616 mutex_lock(&iser_conn->state_mutex); 617 iser_conn->state = ISER_CONN_DOWN; 618 mutex_unlock(&iser_conn->state_mutex); 619 620 iser_conn_release(iser_conn); 621 } 622 623 /** 624 * iser_free_ib_conn_res - release IB related resources 625 * @iser_conn: iser connection struct 626 * @destroy: indicator if we need to try to release the 627 * iser device and memory regoins pool (only iscsi 628 * shutdown and DEVICE_REMOVAL will use this). 629 * 630 * This routine is called with the iser state mutex held 631 * so the cm_id removal is out of here. It is Safe to 632 * be invoked multiple times. 633 */ 634 static void iser_free_ib_conn_res(struct iser_conn *iser_conn, 635 bool destroy) 636 { 637 struct ib_conn *ib_conn = &iser_conn->ib_conn; 638 struct iser_device *device = ib_conn->device; 639 640 iser_info("freeing conn %p cma_id %p qp %p\n", 641 iser_conn, ib_conn->cma_id, ib_conn->qp); 642 643 if (ib_conn->qp != NULL) { 644 ib_conn->comp->active_qps--; 645 rdma_destroy_qp(ib_conn->cma_id); 646 ib_conn->qp = NULL; 647 } 648 649 if (destroy) { 650 if (iser_conn->rx_descs) 651 iser_free_rx_descriptors(iser_conn); 652 653 if (device != NULL) { 654 iser_device_try_release(device); 655 ib_conn->device = NULL; 656 } 657 } 658 } 659 660 /** 661 * Frees all conn objects and deallocs conn descriptor 662 */ 663 void iser_conn_release(struct iser_conn *iser_conn) 664 { 665 struct ib_conn *ib_conn = &iser_conn->ib_conn; 666 667 mutex_lock(&ig.connlist_mutex); 668 list_del(&iser_conn->conn_list); 669 mutex_unlock(&ig.connlist_mutex); 670 671 mutex_lock(&iser_conn->state_mutex); 672 /* In case we endup here without ep_disconnect being invoked. */ 673 if (iser_conn->state != ISER_CONN_DOWN) { 674 iser_warn("iser conn %p state %d, expected state down.\n", 675 iser_conn, iser_conn->state); 676 iscsi_destroy_endpoint(iser_conn->ep); 677 iser_conn->state = ISER_CONN_DOWN; 678 } 679 /* 680 * In case we never got to bind stage, we still need to 681 * release IB resources (which is safe to call more than once). 682 */ 683 iser_free_ib_conn_res(iser_conn, true); 684 mutex_unlock(&iser_conn->state_mutex); 685 686 if (ib_conn->cma_id != NULL) { 687 rdma_destroy_id(ib_conn->cma_id); 688 ib_conn->cma_id = NULL; 689 } 690 691 kfree(iser_conn); 692 } 693 694 /** 695 * triggers start of the disconnect procedures and wait for them to be done 696 * Called with state mutex held 697 */ 698 int iser_conn_terminate(struct iser_conn *iser_conn) 699 { 700 struct ib_conn *ib_conn = &iser_conn->ib_conn; 701 struct ib_send_wr *bad_wr; 702 int err = 0; 703 704 /* terminate the iser conn only if the conn state is UP */ 705 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP, 706 ISER_CONN_TERMINATING)) 707 return 0; 708 709 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state); 710 711 /* suspend queuing of new iscsi commands */ 712 if (iser_conn->iscsi_conn) 713 iscsi_suspend_queue(iser_conn->iscsi_conn); 714 715 /* 716 * In case we didn't already clean up the cma_id (peer initiated 717 * a disconnection), we need to Cause the CMA to change the QP 718 * state to ERROR. 719 */ 720 if (ib_conn->cma_id) { 721 err = rdma_disconnect(ib_conn->cma_id); 722 if (err) 723 iser_err("Failed to disconnect, conn: 0x%p err %d\n", 724 iser_conn, err); 725 726 /* post an indication that all flush errors were consumed */ 727 err = ib_post_send(ib_conn->qp, &ib_conn->beacon, &bad_wr); 728 if (err) { 729 iser_err("conn %p failed to post beacon", ib_conn); 730 return 1; 731 } 732 733 wait_for_completion(&ib_conn->flush_comp); 734 } 735 736 return 1; 737 } 738 739 /** 740 * Called with state mutex held 741 **/ 742 static void iser_connect_error(struct rdma_cm_id *cma_id) 743 { 744 struct iser_conn *iser_conn; 745 746 iser_conn = (struct iser_conn *)cma_id->context; 747 iser_conn->state = ISER_CONN_TERMINATING; 748 } 749 750 static void 751 iser_calc_scsi_params(struct iser_conn *iser_conn, 752 unsigned int max_sectors) 753 { 754 struct iser_device *device = iser_conn->ib_conn.device; 755 unsigned short sg_tablesize, sup_sg_tablesize; 756 757 sg_tablesize = DIV_ROUND_UP(max_sectors * 512, SIZE_4K); 758 sup_sg_tablesize = min_t(unsigned, ISCSI_ISER_MAX_SG_TABLESIZE, 759 device->dev_attr.max_fast_reg_page_list_len); 760 761 if (sg_tablesize > sup_sg_tablesize) { 762 sg_tablesize = sup_sg_tablesize; 763 iser_conn->scsi_max_sectors = sg_tablesize * SIZE_4K / 512; 764 } else { 765 iser_conn->scsi_max_sectors = max_sectors; 766 } 767 768 iser_conn->scsi_sg_tablesize = sg_tablesize; 769 770 iser_dbg("iser_conn %p, sg_tablesize %u, max_sectors %u\n", 771 iser_conn, iser_conn->scsi_sg_tablesize, 772 iser_conn->scsi_max_sectors); 773 } 774 775 /** 776 * Called with state mutex held 777 **/ 778 static void iser_addr_handler(struct rdma_cm_id *cma_id) 779 { 780 struct iser_device *device; 781 struct iser_conn *iser_conn; 782 struct ib_conn *ib_conn; 783 int ret; 784 785 iser_conn = (struct iser_conn *)cma_id->context; 786 if (iser_conn->state != ISER_CONN_PENDING) 787 /* bailout */ 788 return; 789 790 ib_conn = &iser_conn->ib_conn; 791 device = iser_device_find_by_ib_device(cma_id); 792 if (!device) { 793 iser_err("device lookup/creation failed\n"); 794 iser_connect_error(cma_id); 795 return; 796 } 797 798 ib_conn->device = device; 799 800 /* connection T10-PI support */ 801 if (iser_pi_enable) { 802 if (!(device->dev_attr.device_cap_flags & 803 IB_DEVICE_SIGNATURE_HANDOVER)) { 804 iser_warn("T10-PI requested but not supported on %s, " 805 "continue without T10-PI\n", 806 ib_conn->device->ib_device->name); 807 ib_conn->pi_support = false; 808 } else { 809 ib_conn->pi_support = true; 810 } 811 } 812 813 iser_calc_scsi_params(iser_conn, iser_max_sectors); 814 815 ret = rdma_resolve_route(cma_id, 1000); 816 if (ret) { 817 iser_err("resolve route failed: %d\n", ret); 818 iser_connect_error(cma_id); 819 return; 820 } 821 } 822 823 /** 824 * Called with state mutex held 825 **/ 826 static void iser_route_handler(struct rdma_cm_id *cma_id) 827 { 828 struct rdma_conn_param conn_param; 829 int ret; 830 struct iser_cm_hdr req_hdr; 831 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; 832 struct ib_conn *ib_conn = &iser_conn->ib_conn; 833 struct iser_device *device = ib_conn->device; 834 835 if (iser_conn->state != ISER_CONN_PENDING) 836 /* bailout */ 837 return; 838 839 ret = iser_create_ib_conn_res(ib_conn); 840 if (ret) 841 goto failure; 842 843 memset(&conn_param, 0, sizeof conn_param); 844 conn_param.responder_resources = device->dev_attr.max_qp_rd_atom; 845 conn_param.initiator_depth = 1; 846 conn_param.retry_count = 7; 847 conn_param.rnr_retry_count = 6; 848 849 memset(&req_hdr, 0, sizeof(req_hdr)); 850 req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED | 851 ISER_SEND_W_INV_NOT_SUPPORTED); 852 conn_param.private_data = (void *)&req_hdr; 853 conn_param.private_data_len = sizeof(struct iser_cm_hdr); 854 855 ret = rdma_connect(cma_id, &conn_param); 856 if (ret) { 857 iser_err("failure connecting: %d\n", ret); 858 goto failure; 859 } 860 861 return; 862 failure: 863 iser_connect_error(cma_id); 864 } 865 866 static void iser_connected_handler(struct rdma_cm_id *cma_id) 867 { 868 struct iser_conn *iser_conn; 869 struct ib_qp_attr attr; 870 struct ib_qp_init_attr init_attr; 871 872 iser_conn = (struct iser_conn *)cma_id->context; 873 if (iser_conn->state != ISER_CONN_PENDING) 874 /* bailout */ 875 return; 876 877 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr); 878 iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num); 879 880 iser_conn->state = ISER_CONN_UP; 881 complete(&iser_conn->up_completion); 882 } 883 884 static void iser_disconnected_handler(struct rdma_cm_id *cma_id) 885 { 886 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; 887 888 if (iser_conn_terminate(iser_conn)) { 889 if (iser_conn->iscsi_conn) 890 iscsi_conn_failure(iser_conn->iscsi_conn, 891 ISCSI_ERR_CONN_FAILED); 892 else 893 iser_err("iscsi_iser connection isn't bound\n"); 894 } 895 } 896 897 static void iser_cleanup_handler(struct rdma_cm_id *cma_id, 898 bool destroy) 899 { 900 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; 901 902 /* 903 * We are not guaranteed that we visited disconnected_handler 904 * by now, call it here to be safe that we handle CM drep 905 * and flush errors. 906 */ 907 iser_disconnected_handler(cma_id); 908 iser_free_ib_conn_res(iser_conn, destroy); 909 complete(&iser_conn->ib_completion); 910 }; 911 912 static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event) 913 { 914 struct iser_conn *iser_conn; 915 int ret = 0; 916 917 iser_conn = (struct iser_conn *)cma_id->context; 918 iser_info("%s (%d): status %d conn %p id %p\n", 919 rdma_event_msg(event->event), event->event, 920 event->status, cma_id->context, cma_id); 921 922 mutex_lock(&iser_conn->state_mutex); 923 switch (event->event) { 924 case RDMA_CM_EVENT_ADDR_RESOLVED: 925 iser_addr_handler(cma_id); 926 break; 927 case RDMA_CM_EVENT_ROUTE_RESOLVED: 928 iser_route_handler(cma_id); 929 break; 930 case RDMA_CM_EVENT_ESTABLISHED: 931 iser_connected_handler(cma_id); 932 break; 933 case RDMA_CM_EVENT_ADDR_ERROR: 934 case RDMA_CM_EVENT_ROUTE_ERROR: 935 case RDMA_CM_EVENT_CONNECT_ERROR: 936 case RDMA_CM_EVENT_UNREACHABLE: 937 case RDMA_CM_EVENT_REJECTED: 938 iser_connect_error(cma_id); 939 break; 940 case RDMA_CM_EVENT_DISCONNECTED: 941 case RDMA_CM_EVENT_ADDR_CHANGE: 942 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 943 iser_cleanup_handler(cma_id, false); 944 break; 945 case RDMA_CM_EVENT_DEVICE_REMOVAL: 946 /* 947 * we *must* destroy the device as we cannot rely 948 * on iscsid to be around to initiate error handling. 949 * also if we are not in state DOWN implicitly destroy 950 * the cma_id. 951 */ 952 iser_cleanup_handler(cma_id, true); 953 if (iser_conn->state != ISER_CONN_DOWN) { 954 iser_conn->ib_conn.cma_id = NULL; 955 ret = 1; 956 } 957 break; 958 default: 959 iser_err("Unexpected RDMA CM event: %s (%d)\n", 960 rdma_event_msg(event->event), event->event); 961 break; 962 } 963 mutex_unlock(&iser_conn->state_mutex); 964 965 return ret; 966 } 967 968 void iser_conn_init(struct iser_conn *iser_conn) 969 { 970 iser_conn->state = ISER_CONN_INIT; 971 iser_conn->ib_conn.post_recv_buf_count = 0; 972 init_completion(&iser_conn->ib_conn.flush_comp); 973 init_completion(&iser_conn->stop_completion); 974 init_completion(&iser_conn->ib_completion); 975 init_completion(&iser_conn->up_completion); 976 INIT_LIST_HEAD(&iser_conn->conn_list); 977 mutex_init(&iser_conn->state_mutex); 978 } 979 980 /** 981 * starts the process of connecting to the target 982 * sleeps until the connection is established or rejected 983 */ 984 int iser_connect(struct iser_conn *iser_conn, 985 struct sockaddr *src_addr, 986 struct sockaddr *dst_addr, 987 int non_blocking) 988 { 989 struct ib_conn *ib_conn = &iser_conn->ib_conn; 990 int err = 0; 991 992 mutex_lock(&iser_conn->state_mutex); 993 994 sprintf(iser_conn->name, "%pISp", dst_addr); 995 996 iser_info("connecting to: %s\n", iser_conn->name); 997 998 /* the device is known only --after-- address resolution */ 999 ib_conn->device = NULL; 1000 1001 iser_conn->state = ISER_CONN_PENDING; 1002 1003 ib_conn->beacon.wr_id = ISER_BEACON_WRID; 1004 ib_conn->beacon.opcode = IB_WR_SEND; 1005 1006 ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler, 1007 (void *)iser_conn, 1008 RDMA_PS_TCP, IB_QPT_RC); 1009 if (IS_ERR(ib_conn->cma_id)) { 1010 err = PTR_ERR(ib_conn->cma_id); 1011 iser_err("rdma_create_id failed: %d\n", err); 1012 goto id_failure; 1013 } 1014 1015 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000); 1016 if (err) { 1017 iser_err("rdma_resolve_addr failed: %d\n", err); 1018 goto addr_failure; 1019 } 1020 1021 if (!non_blocking) { 1022 wait_for_completion_interruptible(&iser_conn->up_completion); 1023 1024 if (iser_conn->state != ISER_CONN_UP) { 1025 err = -EIO; 1026 goto connect_failure; 1027 } 1028 } 1029 mutex_unlock(&iser_conn->state_mutex); 1030 1031 mutex_lock(&ig.connlist_mutex); 1032 list_add(&iser_conn->conn_list, &ig.connlist); 1033 mutex_unlock(&ig.connlist_mutex); 1034 return 0; 1035 1036 id_failure: 1037 ib_conn->cma_id = NULL; 1038 addr_failure: 1039 iser_conn->state = ISER_CONN_DOWN; 1040 connect_failure: 1041 mutex_unlock(&iser_conn->state_mutex); 1042 iser_conn_release(iser_conn); 1043 return err; 1044 } 1045 1046 int iser_post_recvl(struct iser_conn *iser_conn) 1047 { 1048 struct ib_recv_wr rx_wr, *rx_wr_failed; 1049 struct ib_conn *ib_conn = &iser_conn->ib_conn; 1050 struct ib_sge sge; 1051 int ib_ret; 1052 1053 sge.addr = iser_conn->login_resp_dma; 1054 sge.length = ISER_RX_LOGIN_SIZE; 1055 sge.lkey = ib_conn->device->pd->local_dma_lkey; 1056 1057 rx_wr.wr_id = (uintptr_t)iser_conn->login_resp_buf; 1058 rx_wr.sg_list = &sge; 1059 rx_wr.num_sge = 1; 1060 rx_wr.next = NULL; 1061 1062 ib_conn->post_recv_buf_count++; 1063 ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed); 1064 if (ib_ret) { 1065 iser_err("ib_post_recv failed ret=%d\n", ib_ret); 1066 ib_conn->post_recv_buf_count--; 1067 } 1068 return ib_ret; 1069 } 1070 1071 int iser_post_recvm(struct iser_conn *iser_conn, int count) 1072 { 1073 struct ib_recv_wr *rx_wr, *rx_wr_failed; 1074 int i, ib_ret; 1075 struct ib_conn *ib_conn = &iser_conn->ib_conn; 1076 unsigned int my_rx_head = iser_conn->rx_desc_head; 1077 struct iser_rx_desc *rx_desc; 1078 1079 for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) { 1080 rx_desc = &iser_conn->rx_descs[my_rx_head]; 1081 rx_wr->wr_id = (uintptr_t)rx_desc; 1082 rx_wr->sg_list = &rx_desc->rx_sg; 1083 rx_wr->num_sge = 1; 1084 rx_wr->next = rx_wr + 1; 1085 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask; 1086 } 1087 1088 rx_wr--; 1089 rx_wr->next = NULL; /* mark end of work requests list */ 1090 1091 ib_conn->post_recv_buf_count += count; 1092 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed); 1093 if (ib_ret) { 1094 iser_err("ib_post_recv failed ret=%d\n", ib_ret); 1095 ib_conn->post_recv_buf_count -= count; 1096 } else 1097 iser_conn->rx_desc_head = my_rx_head; 1098 return ib_ret; 1099 } 1100 1101 1102 /** 1103 * iser_start_send - Initiate a Send DTO operation 1104 * 1105 * returns 0 on success, -1 on failure 1106 */ 1107 int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc, 1108 bool signal) 1109 { 1110 struct ib_send_wr *bad_wr, *wr = iser_tx_next_wr(tx_desc); 1111 int ib_ret; 1112 1113 ib_dma_sync_single_for_device(ib_conn->device->ib_device, 1114 tx_desc->dma_addr, ISER_HEADERS_LEN, 1115 DMA_TO_DEVICE); 1116 1117 wr->next = NULL; 1118 wr->wr_id = (uintptr_t)tx_desc; 1119 wr->sg_list = tx_desc->tx_sg; 1120 wr->num_sge = tx_desc->num_sge; 1121 wr->opcode = IB_WR_SEND; 1122 wr->send_flags = signal ? IB_SEND_SIGNALED : 0; 1123 1124 ib_ret = ib_post_send(ib_conn->qp, &tx_desc->wrs[0].send, &bad_wr); 1125 if (ib_ret) 1126 iser_err("ib_post_send failed, ret:%d opcode:%d\n", 1127 ib_ret, bad_wr->opcode); 1128 1129 return ib_ret; 1130 } 1131 1132 /** 1133 * is_iser_tx_desc - Indicate if the completion wr_id 1134 * is a TX descriptor or not. 1135 * @iser_conn: iser connection 1136 * @wr_id: completion WR identifier 1137 * 1138 * Since we cannot rely on wc opcode in FLUSH errors 1139 * we must work around it by checking if the wr_id address 1140 * falls in the iser connection rx_descs buffer. If so 1141 * it is an RX descriptor, otherwize it is a TX. 1142 */ 1143 static inline bool 1144 is_iser_tx_desc(struct iser_conn *iser_conn, void *wr_id) 1145 { 1146 void *start = iser_conn->rx_descs; 1147 int len = iser_conn->num_rx_descs * sizeof(*iser_conn->rx_descs); 1148 1149 if (wr_id >= start && wr_id < start + len) 1150 return false; 1151 1152 return true; 1153 } 1154 1155 /** 1156 * iser_handle_comp_error() - Handle error completion 1157 * @ib_conn: connection RDMA resources 1158 * @wc: work completion 1159 * 1160 * Notes: We may handle a FLUSH error completion and in this case 1161 * we only cleanup in case TX type was DATAOUT. For non-FLUSH 1162 * error completion we should also notify iscsi layer that 1163 * connection is failed (in case we passed bind stage). 1164 */ 1165 static void 1166 iser_handle_comp_error(struct ib_conn *ib_conn, 1167 struct ib_wc *wc) 1168 { 1169 void *wr_id = (void *)(uintptr_t)wc->wr_id; 1170 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn, 1171 ib_conn); 1172 1173 if (wc->status != IB_WC_WR_FLUSH_ERR) 1174 if (iser_conn->iscsi_conn) 1175 iscsi_conn_failure(iser_conn->iscsi_conn, 1176 ISCSI_ERR_CONN_FAILED); 1177 1178 if (wc->wr_id == ISER_FASTREG_LI_WRID) 1179 return; 1180 1181 if (is_iser_tx_desc(iser_conn, wr_id)) { 1182 struct iser_tx_desc *desc = wr_id; 1183 1184 if (desc->type == ISCSI_TX_DATAOUT) 1185 kmem_cache_free(ig.desc_cache, desc); 1186 } else { 1187 ib_conn->post_recv_buf_count--; 1188 } 1189 } 1190 1191 /** 1192 * iser_handle_wc - handle a single work completion 1193 * @wc: work completion 1194 * 1195 * Soft-IRQ context, work completion can be either 1196 * SEND or RECV, and can turn out successful or 1197 * with error (or flush error). 1198 */ 1199 static void iser_handle_wc(struct ib_wc *wc) 1200 { 1201 struct ib_conn *ib_conn; 1202 struct iser_tx_desc *tx_desc; 1203 struct iser_rx_desc *rx_desc; 1204 1205 ib_conn = wc->qp->qp_context; 1206 if (likely(wc->status == IB_WC_SUCCESS)) { 1207 if (wc->opcode == IB_WC_RECV) { 1208 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id; 1209 iser_rcv_completion(rx_desc, wc->byte_len, 1210 ib_conn); 1211 } else 1212 if (wc->opcode == IB_WC_SEND) { 1213 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id; 1214 iser_snd_completion(tx_desc, ib_conn); 1215 } else { 1216 iser_err("Unknown wc opcode %d\n", wc->opcode); 1217 } 1218 } else { 1219 if (wc->status != IB_WC_WR_FLUSH_ERR) 1220 iser_err("%s (%d): wr id %llx vend_err %x\n", 1221 ib_wc_status_msg(wc->status), wc->status, 1222 wc->wr_id, wc->vendor_err); 1223 else 1224 iser_dbg("%s (%d): wr id %llx\n", 1225 ib_wc_status_msg(wc->status), wc->status, 1226 wc->wr_id); 1227 1228 if (wc->wr_id == ISER_BEACON_WRID) 1229 /* all flush errors were consumed */ 1230 complete(&ib_conn->flush_comp); 1231 else 1232 iser_handle_comp_error(ib_conn, wc); 1233 } 1234 } 1235 1236 /** 1237 * iser_cq_tasklet_fn - iSER completion polling loop 1238 * @data: iSER completion context 1239 * 1240 * Soft-IRQ context, polling connection CQ until 1241 * either CQ was empty or we exausted polling budget 1242 */ 1243 static void iser_cq_tasklet_fn(unsigned long data) 1244 { 1245 struct iser_comp *comp = (struct iser_comp *)data; 1246 struct ib_cq *cq = comp->cq; 1247 struct ib_wc *const wcs = comp->wcs; 1248 int i, n, completed = 0; 1249 1250 while ((n = ib_poll_cq(cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) { 1251 for (i = 0; i < n; i++) 1252 iser_handle_wc(&wcs[i]); 1253 1254 completed += n; 1255 if (completed >= iser_cq_poll_limit) 1256 break; 1257 } 1258 1259 /* 1260 * It is assumed here that arming CQ only once its empty 1261 * would not cause interrupts to be missed. 1262 */ 1263 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 1264 1265 iser_dbg("got %d completions\n", completed); 1266 } 1267 1268 static void iser_cq_callback(struct ib_cq *cq, void *cq_context) 1269 { 1270 struct iser_comp *comp = cq_context; 1271 1272 tasklet_schedule(&comp->tasklet); 1273 } 1274 1275 u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task, 1276 enum iser_data_dir cmd_dir, sector_t *sector) 1277 { 1278 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir]; 1279 struct iser_fr_desc *desc = reg->mem_h; 1280 unsigned long sector_size = iser_task->sc->device->sector_size; 1281 struct ib_mr_status mr_status; 1282 int ret; 1283 1284 if (desc && desc->pi_ctx->sig_protected) { 1285 desc->pi_ctx->sig_protected = 0; 1286 ret = ib_check_mr_status(desc->pi_ctx->sig_mr, 1287 IB_MR_CHECK_SIG_STATUS, &mr_status); 1288 if (ret) { 1289 pr_err("ib_check_mr_status failed, ret %d\n", ret); 1290 goto err; 1291 } 1292 1293 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { 1294 sector_t sector_off = mr_status.sig_err.sig_err_offset; 1295 1296 sector_div(sector_off, sector_size + 8); 1297 *sector = scsi_get_lba(iser_task->sc) + sector_off; 1298 1299 pr_err("PI error found type %d at sector %llx " 1300 "expected %x vs actual %x\n", 1301 mr_status.sig_err.err_type, 1302 (unsigned long long)*sector, 1303 mr_status.sig_err.expected, 1304 mr_status.sig_err.actual); 1305 1306 switch (mr_status.sig_err.err_type) { 1307 case IB_SIG_BAD_GUARD: 1308 return 0x1; 1309 case IB_SIG_BAD_REFTAG: 1310 return 0x3; 1311 case IB_SIG_BAD_APPTAG: 1312 return 0x2; 1313 } 1314 } 1315 } 1316 1317 return 0; 1318 err: 1319 /* Not alot we can do here, return ambiguous guard error */ 1320 return 0x1; 1321 } 1322