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 void iser_qp_event_callback(struct ib_event *cause, void *context) 48 { 49 iser_err("qp event %s (%d)\n", 50 ib_event_msg(cause->event), cause->event); 51 } 52 53 static void iser_event_handler(struct ib_event_handler *handler, 54 struct ib_event *event) 55 { 56 iser_err("async event %s (%d) on device %s port %d\n", 57 ib_event_msg(event->event), event->event, 58 dev_name(&event->device->dev), event->element.port_num); 59 } 60 61 /* 62 * iser_create_device_ib_res - creates Protection Domain (PD), Completion 63 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with 64 * the adaptor. 65 * 66 * Return: 0 on success, -1 on failure 67 */ 68 static int iser_create_device_ib_res(struct iser_device *device) 69 { 70 struct ib_device *ib_dev = device->ib_device; 71 int ret, i, max_cqe; 72 73 ret = iser_assign_reg_ops(device); 74 if (ret) 75 return ret; 76 77 device->comps_used = min_t(int, num_online_cpus(), 78 ib_dev->num_comp_vectors); 79 80 device->comps = kcalloc(device->comps_used, sizeof(*device->comps), 81 GFP_KERNEL); 82 if (!device->comps) 83 goto comps_err; 84 85 max_cqe = min(ISER_MAX_CQ_LEN, ib_dev->attrs.max_cqe); 86 87 iser_info("using %d CQs, device %s supports %d vectors max_cqe %d\n", 88 device->comps_used, dev_name(&ib_dev->dev), 89 ib_dev->num_comp_vectors, max_cqe); 90 91 device->pd = ib_alloc_pd(ib_dev, 92 iser_always_reg ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY); 93 if (IS_ERR(device->pd)) 94 goto pd_err; 95 96 for (i = 0; i < device->comps_used; i++) { 97 struct iser_comp *comp = &device->comps[i]; 98 99 comp->cq = ib_alloc_cq(ib_dev, comp, max_cqe, i, 100 IB_POLL_SOFTIRQ); 101 if (IS_ERR(comp->cq)) { 102 comp->cq = NULL; 103 goto cq_err; 104 } 105 } 106 107 INIT_IB_EVENT_HANDLER(&device->event_handler, ib_dev, 108 iser_event_handler); 109 ib_register_event_handler(&device->event_handler); 110 return 0; 111 112 cq_err: 113 for (i = 0; i < device->comps_used; i++) { 114 struct iser_comp *comp = &device->comps[i]; 115 116 if (comp->cq) 117 ib_free_cq(comp->cq); 118 } 119 ib_dealloc_pd(device->pd); 120 pd_err: 121 kfree(device->comps); 122 comps_err: 123 iser_err("failed to allocate an IB resource\n"); 124 return -1; 125 } 126 127 /* 128 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR, 129 * CQ and PD created with the device associated with the adaptor. 130 */ 131 static void iser_free_device_ib_res(struct iser_device *device) 132 { 133 int i; 134 135 for (i = 0; i < device->comps_used; i++) { 136 struct iser_comp *comp = &device->comps[i]; 137 138 ib_free_cq(comp->cq); 139 comp->cq = NULL; 140 } 141 142 ib_unregister_event_handler(&device->event_handler); 143 ib_dealloc_pd(device->pd); 144 145 kfree(device->comps); 146 device->comps = NULL; 147 device->pd = NULL; 148 } 149 150 /** 151 * iser_alloc_fmr_pool - Creates FMR pool and page_vector 152 * @ib_conn: connection RDMA resources 153 * @cmds_max: max number of SCSI commands for this connection 154 * @size: max number of pages per map request 155 * 156 * Return: 0 on success, or errno code on failure 157 */ 158 int iser_alloc_fmr_pool(struct ib_conn *ib_conn, 159 unsigned cmds_max, 160 unsigned int size) 161 { 162 struct iser_device *device = ib_conn->device; 163 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool; 164 struct iser_page_vec *page_vec; 165 struct iser_fr_desc *desc; 166 struct ib_fmr_pool *fmr_pool; 167 struct ib_fmr_pool_param params; 168 int ret; 169 170 INIT_LIST_HEAD(&fr_pool->list); 171 spin_lock_init(&fr_pool->lock); 172 173 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 174 if (!desc) 175 return -ENOMEM; 176 177 page_vec = kmalloc(sizeof(*page_vec) + (sizeof(u64) * size), 178 GFP_KERNEL); 179 if (!page_vec) { 180 ret = -ENOMEM; 181 goto err_frpl; 182 } 183 184 page_vec->pages = (u64 *)(page_vec + 1); 185 186 params.page_shift = ilog2(SZ_4K); 187 params.max_pages_per_fmr = size; 188 /* make the pool size twice the max number of SCSI commands * 189 * the ML is expected to queue, watermark for unmap at 50% */ 190 params.pool_size = cmds_max * 2; 191 params.dirty_watermark = cmds_max; 192 params.cache = 0; 193 params.flush_function = NULL; 194 params.access = (IB_ACCESS_LOCAL_WRITE | 195 IB_ACCESS_REMOTE_WRITE | 196 IB_ACCESS_REMOTE_READ); 197 198 fmr_pool = ib_create_fmr_pool(device->pd, ¶ms); 199 if (IS_ERR(fmr_pool)) { 200 ret = PTR_ERR(fmr_pool); 201 iser_err("FMR allocation failed, err %d\n", ret); 202 goto err_fmr; 203 } 204 205 desc->rsc.page_vec = page_vec; 206 desc->rsc.fmr_pool = fmr_pool; 207 list_add(&desc->list, &fr_pool->list); 208 209 return 0; 210 211 err_fmr: 212 kfree(page_vec); 213 err_frpl: 214 kfree(desc); 215 216 return ret; 217 } 218 219 /** 220 * iser_free_fmr_pool - releases the FMR pool and page vec 221 * @ib_conn: connection RDMA resources 222 */ 223 void iser_free_fmr_pool(struct ib_conn *ib_conn) 224 { 225 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool; 226 struct iser_fr_desc *desc; 227 228 desc = list_first_entry(&fr_pool->list, 229 struct iser_fr_desc, list); 230 list_del(&desc->list); 231 232 iser_info("freeing conn %p fmr pool %p\n", 233 ib_conn, desc->rsc.fmr_pool); 234 235 ib_destroy_fmr_pool(desc->rsc.fmr_pool); 236 kfree(desc->rsc.page_vec); 237 kfree(desc); 238 } 239 240 static struct iser_fr_desc * 241 iser_create_fastreg_desc(struct iser_device *device, 242 struct ib_pd *pd, 243 bool pi_enable, 244 unsigned int size) 245 { 246 struct iser_fr_desc *desc; 247 struct ib_device *ib_dev = device->ib_device; 248 enum ib_mr_type mr_type; 249 int ret; 250 251 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 252 if (!desc) 253 return ERR_PTR(-ENOMEM); 254 255 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG) 256 mr_type = IB_MR_TYPE_SG_GAPS; 257 else 258 mr_type = IB_MR_TYPE_MEM_REG; 259 260 desc->rsc.mr = ib_alloc_mr(pd, mr_type, size); 261 if (IS_ERR(desc->rsc.mr)) { 262 ret = PTR_ERR(desc->rsc.mr); 263 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret); 264 goto err_alloc_mr; 265 } 266 267 if (pi_enable) { 268 desc->rsc.sig_mr = ib_alloc_mr_integrity(pd, size, size); 269 if (IS_ERR(desc->rsc.sig_mr)) { 270 ret = PTR_ERR(desc->rsc.sig_mr); 271 iser_err("Failed to allocate sig_mr err=%d\n", ret); 272 goto err_alloc_mr_integrity; 273 } 274 } 275 desc->rsc.mr_valid = 0; 276 277 return desc; 278 279 err_alloc_mr_integrity: 280 ib_dereg_mr(desc->rsc.mr); 281 err_alloc_mr: 282 kfree(desc); 283 284 return ERR_PTR(ret); 285 } 286 287 static void iser_destroy_fastreg_desc(struct iser_fr_desc *desc) 288 { 289 struct iser_reg_resources *res = &desc->rsc; 290 291 ib_dereg_mr(res->mr); 292 if (res->sig_mr) { 293 ib_dereg_mr(res->sig_mr); 294 res->sig_mr = NULL; 295 } 296 kfree(desc); 297 } 298 299 /** 300 * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors 301 * for fast registration work requests. 302 * @ib_conn: connection RDMA resources 303 * @cmds_max: max number of SCSI commands for this connection 304 * @size: max number of pages per map request 305 * 306 * Return: 0 on success, or errno code on failure 307 */ 308 int iser_alloc_fastreg_pool(struct ib_conn *ib_conn, 309 unsigned cmds_max, 310 unsigned int size) 311 { 312 struct iser_device *device = ib_conn->device; 313 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool; 314 struct iser_fr_desc *desc; 315 int i, ret; 316 317 INIT_LIST_HEAD(&fr_pool->list); 318 INIT_LIST_HEAD(&fr_pool->all_list); 319 spin_lock_init(&fr_pool->lock); 320 fr_pool->size = 0; 321 for (i = 0; i < cmds_max; i++) { 322 desc = iser_create_fastreg_desc(device, device->pd, 323 ib_conn->pi_support, size); 324 if (IS_ERR(desc)) { 325 ret = PTR_ERR(desc); 326 goto err; 327 } 328 329 list_add_tail(&desc->list, &fr_pool->list); 330 list_add_tail(&desc->all_list, &fr_pool->all_list); 331 fr_pool->size++; 332 } 333 334 return 0; 335 336 err: 337 iser_free_fastreg_pool(ib_conn); 338 return ret; 339 } 340 341 /** 342 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors 343 * @ib_conn: connection RDMA resources 344 */ 345 void iser_free_fastreg_pool(struct ib_conn *ib_conn) 346 { 347 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool; 348 struct iser_fr_desc *desc, *tmp; 349 int i = 0; 350 351 if (list_empty(&fr_pool->all_list)) 352 return; 353 354 iser_info("freeing conn %p fr pool\n", ib_conn); 355 356 list_for_each_entry_safe(desc, tmp, &fr_pool->all_list, all_list) { 357 list_del(&desc->all_list); 358 iser_destroy_fastreg_desc(desc); 359 ++i; 360 } 361 362 if (i < fr_pool->size) 363 iser_warn("pool still has %d regions registered\n", 364 fr_pool->size - i); 365 } 366 367 /* 368 * iser_create_ib_conn_res - Queue-Pair (QP) 369 * 370 * Return: 0 on success, -1 on failure 371 */ 372 static int iser_create_ib_conn_res(struct ib_conn *ib_conn) 373 { 374 struct iser_conn *iser_conn = to_iser_conn(ib_conn); 375 struct iser_device *device; 376 struct ib_device *ib_dev; 377 struct ib_qp_init_attr init_attr; 378 int ret = -ENOMEM; 379 int index, min_index = 0; 380 381 BUG_ON(ib_conn->device == NULL); 382 383 device = ib_conn->device; 384 ib_dev = device->ib_device; 385 386 memset(&init_attr, 0, sizeof init_attr); 387 388 mutex_lock(&ig.connlist_mutex); 389 /* select the CQ with the minimal number of usages */ 390 for (index = 0; index < device->comps_used; index++) { 391 if (device->comps[index].active_qps < 392 device->comps[min_index].active_qps) 393 min_index = index; 394 } 395 ib_conn->comp = &device->comps[min_index]; 396 ib_conn->comp->active_qps++; 397 mutex_unlock(&ig.connlist_mutex); 398 iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn); 399 400 init_attr.event_handler = iser_qp_event_callback; 401 init_attr.qp_context = (void *)ib_conn; 402 init_attr.send_cq = ib_conn->comp->cq; 403 init_attr.recv_cq = ib_conn->comp->cq; 404 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS; 405 init_attr.cap.max_send_sge = 2; 406 init_attr.cap.max_recv_sge = 1; 407 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 408 init_attr.qp_type = IB_QPT_RC; 409 if (ib_conn->pi_support) { 410 init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1; 411 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN; 412 iser_conn->max_cmds = 413 ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS); 414 } else { 415 if (ib_dev->attrs.max_qp_wr > ISER_QP_MAX_REQ_DTOS) { 416 init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1; 417 iser_conn->max_cmds = 418 ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS); 419 } else { 420 init_attr.cap.max_send_wr = ib_dev->attrs.max_qp_wr; 421 iser_conn->max_cmds = 422 ISER_GET_MAX_XMIT_CMDS(ib_dev->attrs.max_qp_wr); 423 iser_dbg("device %s supports max_send_wr %d\n", 424 dev_name(&device->ib_device->dev), 425 ib_dev->attrs.max_qp_wr); 426 } 427 } 428 429 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr); 430 if (ret) 431 goto out_err; 432 433 ib_conn->qp = ib_conn->cma_id->qp; 434 iser_info("setting conn %p cma_id %p qp %p\n", 435 ib_conn, ib_conn->cma_id, 436 ib_conn->cma_id->qp); 437 return ret; 438 439 out_err: 440 mutex_lock(&ig.connlist_mutex); 441 ib_conn->comp->active_qps--; 442 mutex_unlock(&ig.connlist_mutex); 443 iser_err("unable to alloc mem or create resource, err %d\n", ret); 444 445 return ret; 446 } 447 448 /* 449 * based on the resolved device node GUID see if there already allocated 450 * device for this device. If there's no such, create one. 451 */ 452 static 453 struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id) 454 { 455 struct iser_device *device; 456 457 mutex_lock(&ig.device_list_mutex); 458 459 list_for_each_entry(device, &ig.device_list, ig_list) 460 /* find if there's a match using the node GUID */ 461 if (device->ib_device->node_guid == cma_id->device->node_guid) 462 goto inc_refcnt; 463 464 device = kzalloc(sizeof *device, GFP_KERNEL); 465 if (device == NULL) 466 goto out; 467 468 /* assign this device to the device */ 469 device->ib_device = cma_id->device; 470 /* init the device and link it into ig device list */ 471 if (iser_create_device_ib_res(device)) { 472 kfree(device); 473 device = NULL; 474 goto out; 475 } 476 list_add(&device->ig_list, &ig.device_list); 477 478 inc_refcnt: 479 device->refcount++; 480 out: 481 mutex_unlock(&ig.device_list_mutex); 482 return device; 483 } 484 485 /* if there's no demand for this device, release it */ 486 static void iser_device_try_release(struct iser_device *device) 487 { 488 mutex_lock(&ig.device_list_mutex); 489 device->refcount--; 490 iser_info("device %p refcount %d\n", device, device->refcount); 491 if (!device->refcount) { 492 iser_free_device_ib_res(device); 493 list_del(&device->ig_list); 494 kfree(device); 495 } 496 mutex_unlock(&ig.device_list_mutex); 497 } 498 499 /* 500 * Called with state mutex held 501 */ 502 static int iser_conn_state_comp_exch(struct iser_conn *iser_conn, 503 enum iser_conn_state comp, 504 enum iser_conn_state exch) 505 { 506 int ret; 507 508 ret = (iser_conn->state == comp); 509 if (ret) 510 iser_conn->state = exch; 511 512 return ret; 513 } 514 515 void iser_release_work(struct work_struct *work) 516 { 517 struct iser_conn *iser_conn; 518 519 iser_conn = container_of(work, struct iser_conn, release_work); 520 521 /* Wait for conn_stop to complete */ 522 wait_for_completion(&iser_conn->stop_completion); 523 /* Wait for IB resouces cleanup to complete */ 524 wait_for_completion(&iser_conn->ib_completion); 525 526 mutex_lock(&iser_conn->state_mutex); 527 iser_conn->state = ISER_CONN_DOWN; 528 mutex_unlock(&iser_conn->state_mutex); 529 530 iser_conn_release(iser_conn); 531 } 532 533 /** 534 * iser_free_ib_conn_res - release IB related resources 535 * @iser_conn: iser connection struct 536 * @destroy: indicator if we need to try to release the 537 * iser device and memory regoins pool (only iscsi 538 * shutdown and DEVICE_REMOVAL will use this). 539 * 540 * This routine is called with the iser state mutex held 541 * so the cm_id removal is out of here. It is Safe to 542 * be invoked multiple times. 543 */ 544 static void iser_free_ib_conn_res(struct iser_conn *iser_conn, 545 bool destroy) 546 { 547 struct ib_conn *ib_conn = &iser_conn->ib_conn; 548 struct iser_device *device = ib_conn->device; 549 550 iser_info("freeing conn %p cma_id %p qp %p\n", 551 iser_conn, ib_conn->cma_id, ib_conn->qp); 552 553 if (ib_conn->qp != NULL) { 554 mutex_lock(&ig.connlist_mutex); 555 ib_conn->comp->active_qps--; 556 mutex_unlock(&ig.connlist_mutex); 557 rdma_destroy_qp(ib_conn->cma_id); 558 ib_conn->qp = NULL; 559 } 560 561 if (destroy) { 562 if (iser_conn->rx_descs) 563 iser_free_rx_descriptors(iser_conn); 564 565 if (device != NULL) { 566 iser_device_try_release(device); 567 ib_conn->device = NULL; 568 } 569 } 570 } 571 572 /** 573 * iser_conn_release - Frees all conn objects and deallocs conn descriptor 574 * @iser_conn: iSER connection context 575 */ 576 void iser_conn_release(struct iser_conn *iser_conn) 577 { 578 struct ib_conn *ib_conn = &iser_conn->ib_conn; 579 580 mutex_lock(&ig.connlist_mutex); 581 list_del(&iser_conn->conn_list); 582 mutex_unlock(&ig.connlist_mutex); 583 584 mutex_lock(&iser_conn->state_mutex); 585 /* In case we endup here without ep_disconnect being invoked. */ 586 if (iser_conn->state != ISER_CONN_DOWN) { 587 iser_warn("iser conn %p state %d, expected state down.\n", 588 iser_conn, iser_conn->state); 589 iscsi_destroy_endpoint(iser_conn->ep); 590 iser_conn->state = ISER_CONN_DOWN; 591 } 592 /* 593 * In case we never got to bind stage, we still need to 594 * release IB resources (which is safe to call more than once). 595 */ 596 iser_free_ib_conn_res(iser_conn, true); 597 mutex_unlock(&iser_conn->state_mutex); 598 599 if (ib_conn->cma_id != NULL) { 600 rdma_destroy_id(ib_conn->cma_id); 601 ib_conn->cma_id = NULL; 602 } 603 604 kfree(iser_conn); 605 } 606 607 /** 608 * iser_conn_terminate - triggers start of the disconnect procedures and 609 * waits for them to be done 610 * @iser_conn: iSER connection context 611 * 612 * Called with state mutex held 613 */ 614 int iser_conn_terminate(struct iser_conn *iser_conn) 615 { 616 struct ib_conn *ib_conn = &iser_conn->ib_conn; 617 int err = 0; 618 619 /* terminate the iser conn only if the conn state is UP */ 620 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP, 621 ISER_CONN_TERMINATING)) 622 return 0; 623 624 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state); 625 626 /* suspend queuing of new iscsi commands */ 627 if (iser_conn->iscsi_conn) 628 iscsi_suspend_queue(iser_conn->iscsi_conn); 629 630 /* 631 * In case we didn't already clean up the cma_id (peer initiated 632 * a disconnection), we need to Cause the CMA to change the QP 633 * state to ERROR. 634 */ 635 if (ib_conn->cma_id) { 636 err = rdma_disconnect(ib_conn->cma_id); 637 if (err) 638 iser_err("Failed to disconnect, conn: 0x%p err %d\n", 639 iser_conn, err); 640 641 /* block until all flush errors are consumed */ 642 ib_drain_sq(ib_conn->qp); 643 } 644 645 return 1; 646 } 647 648 /* 649 * Called with state mutex held 650 */ 651 static void iser_connect_error(struct rdma_cm_id *cma_id) 652 { 653 struct iser_conn *iser_conn; 654 655 iser_conn = (struct iser_conn *)cma_id->context; 656 iser_conn->state = ISER_CONN_TERMINATING; 657 } 658 659 static void 660 iser_calc_scsi_params(struct iser_conn *iser_conn, 661 unsigned int max_sectors) 662 { 663 struct iser_device *device = iser_conn->ib_conn.device; 664 struct ib_device_attr *attr = &device->ib_device->attrs; 665 unsigned short sg_tablesize, sup_sg_tablesize; 666 unsigned short reserved_mr_pages; 667 u32 max_num_sg; 668 669 /* 670 * FRs without SG_GAPS or FMRs can only map up to a (device) page per 671 * entry, but if the first entry is misaligned we'll end up using two 672 * entries (head and tail) for a single page worth data, so one 673 * additional entry is required. 674 */ 675 if ((attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) && 676 (attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)) 677 reserved_mr_pages = 0; 678 else 679 reserved_mr_pages = 1; 680 681 if (iser_conn->ib_conn.pi_support) 682 max_num_sg = attr->max_pi_fast_reg_page_list_len; 683 else 684 max_num_sg = attr->max_fast_reg_page_list_len; 685 686 sg_tablesize = DIV_ROUND_UP(max_sectors * SECTOR_SIZE, SZ_4K); 687 if (attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) 688 sup_sg_tablesize = 689 min_t( 690 uint, ISCSI_ISER_MAX_SG_TABLESIZE, 691 max_num_sg - reserved_mr_pages); 692 else 693 sup_sg_tablesize = ISCSI_ISER_MAX_SG_TABLESIZE; 694 695 iser_conn->scsi_sg_tablesize = min(sg_tablesize, sup_sg_tablesize); 696 iser_conn->pages_per_mr = 697 iser_conn->scsi_sg_tablesize + reserved_mr_pages; 698 } 699 700 /* 701 * Called with state mutex held 702 */ 703 static void iser_addr_handler(struct rdma_cm_id *cma_id) 704 { 705 struct iser_device *device; 706 struct iser_conn *iser_conn; 707 struct ib_conn *ib_conn; 708 int ret; 709 710 iser_conn = (struct iser_conn *)cma_id->context; 711 if (iser_conn->state != ISER_CONN_PENDING) 712 /* bailout */ 713 return; 714 715 ib_conn = &iser_conn->ib_conn; 716 device = iser_device_find_by_ib_device(cma_id); 717 if (!device) { 718 iser_err("device lookup/creation failed\n"); 719 iser_connect_error(cma_id); 720 return; 721 } 722 723 ib_conn->device = device; 724 725 /* connection T10-PI support */ 726 if (iser_pi_enable) { 727 if (!(device->ib_device->attrs.device_cap_flags & 728 IB_DEVICE_INTEGRITY_HANDOVER)) { 729 iser_warn("T10-PI requested but not supported on %s, " 730 "continue without T10-PI\n", 731 dev_name(&ib_conn->device->ib_device->dev)); 732 ib_conn->pi_support = false; 733 } else { 734 ib_conn->pi_support = true; 735 } 736 } 737 738 iser_calc_scsi_params(iser_conn, iser_max_sectors); 739 740 ret = rdma_resolve_route(cma_id, 1000); 741 if (ret) { 742 iser_err("resolve route failed: %d\n", ret); 743 iser_connect_error(cma_id); 744 return; 745 } 746 } 747 748 /* 749 * Called with state mutex held 750 */ 751 static void iser_route_handler(struct rdma_cm_id *cma_id) 752 { 753 struct rdma_conn_param conn_param; 754 int ret; 755 struct iser_cm_hdr req_hdr; 756 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; 757 struct ib_conn *ib_conn = &iser_conn->ib_conn; 758 struct iser_device *device = ib_conn->device; 759 760 if (iser_conn->state != ISER_CONN_PENDING) 761 /* bailout */ 762 return; 763 764 ret = iser_create_ib_conn_res(ib_conn); 765 if (ret) 766 goto failure; 767 768 memset(&conn_param, 0, sizeof conn_param); 769 conn_param.responder_resources = device->ib_device->attrs.max_qp_rd_atom; 770 conn_param.initiator_depth = 1; 771 conn_param.retry_count = 7; 772 conn_param.rnr_retry_count = 6; 773 774 memset(&req_hdr, 0, sizeof(req_hdr)); 775 req_hdr.flags = ISER_ZBVA_NOT_SUP; 776 if (!device->remote_inv_sup) 777 req_hdr.flags |= ISER_SEND_W_INV_NOT_SUP; 778 conn_param.private_data = (void *)&req_hdr; 779 conn_param.private_data_len = sizeof(struct iser_cm_hdr); 780 781 ret = rdma_connect(cma_id, &conn_param); 782 if (ret) { 783 iser_err("failure connecting: %d\n", ret); 784 goto failure; 785 } 786 787 return; 788 failure: 789 iser_connect_error(cma_id); 790 } 791 792 static void iser_connected_handler(struct rdma_cm_id *cma_id, 793 const void *private_data) 794 { 795 struct iser_conn *iser_conn; 796 struct ib_qp_attr attr; 797 struct ib_qp_init_attr init_attr; 798 799 iser_conn = (struct iser_conn *)cma_id->context; 800 if (iser_conn->state != ISER_CONN_PENDING) 801 /* bailout */ 802 return; 803 804 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr); 805 iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num); 806 807 if (private_data) { 808 u8 flags = *(u8 *)private_data; 809 810 iser_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP); 811 } 812 813 iser_info("conn %p: negotiated %s invalidation\n", 814 iser_conn, iser_conn->snd_w_inv ? "remote" : "local"); 815 816 iser_conn->state = ISER_CONN_UP; 817 complete(&iser_conn->up_completion); 818 } 819 820 static void iser_disconnected_handler(struct rdma_cm_id *cma_id) 821 { 822 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; 823 824 if (iser_conn_terminate(iser_conn)) { 825 if (iser_conn->iscsi_conn) 826 iscsi_conn_failure(iser_conn->iscsi_conn, 827 ISCSI_ERR_CONN_FAILED); 828 else 829 iser_err("iscsi_iser connection isn't bound\n"); 830 } 831 } 832 833 static void iser_cleanup_handler(struct rdma_cm_id *cma_id, 834 bool destroy) 835 { 836 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context; 837 838 /* 839 * We are not guaranteed that we visited disconnected_handler 840 * by now, call it here to be safe that we handle CM drep 841 * and flush errors. 842 */ 843 iser_disconnected_handler(cma_id); 844 iser_free_ib_conn_res(iser_conn, destroy); 845 complete(&iser_conn->ib_completion); 846 }; 847 848 static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event) 849 { 850 struct iser_conn *iser_conn; 851 int ret = 0; 852 853 iser_conn = (struct iser_conn *)cma_id->context; 854 iser_info("%s (%d): status %d conn %p id %p\n", 855 rdma_event_msg(event->event), event->event, 856 event->status, cma_id->context, cma_id); 857 858 mutex_lock(&iser_conn->state_mutex); 859 switch (event->event) { 860 case RDMA_CM_EVENT_ADDR_RESOLVED: 861 iser_addr_handler(cma_id); 862 break; 863 case RDMA_CM_EVENT_ROUTE_RESOLVED: 864 iser_route_handler(cma_id); 865 break; 866 case RDMA_CM_EVENT_ESTABLISHED: 867 iser_connected_handler(cma_id, event->param.conn.private_data); 868 break; 869 case RDMA_CM_EVENT_REJECTED: 870 iser_info("Connection rejected: %s\n", 871 rdma_reject_msg(cma_id, event->status)); 872 /* FALLTHROUGH */ 873 case RDMA_CM_EVENT_ADDR_ERROR: 874 case RDMA_CM_EVENT_ROUTE_ERROR: 875 case RDMA_CM_EVENT_CONNECT_ERROR: 876 case RDMA_CM_EVENT_UNREACHABLE: 877 iser_connect_error(cma_id); 878 break; 879 case RDMA_CM_EVENT_DISCONNECTED: 880 case RDMA_CM_EVENT_ADDR_CHANGE: 881 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 882 iser_cleanup_handler(cma_id, false); 883 break; 884 case RDMA_CM_EVENT_DEVICE_REMOVAL: 885 /* 886 * we *must* destroy the device as we cannot rely 887 * on iscsid to be around to initiate error handling. 888 * also if we are not in state DOWN implicitly destroy 889 * the cma_id. 890 */ 891 iser_cleanup_handler(cma_id, true); 892 if (iser_conn->state != ISER_CONN_DOWN) { 893 iser_conn->ib_conn.cma_id = NULL; 894 ret = 1; 895 } 896 break; 897 default: 898 iser_err("Unexpected RDMA CM event: %s (%d)\n", 899 rdma_event_msg(event->event), event->event); 900 break; 901 } 902 mutex_unlock(&iser_conn->state_mutex); 903 904 return ret; 905 } 906 907 void iser_conn_init(struct iser_conn *iser_conn) 908 { 909 struct ib_conn *ib_conn = &iser_conn->ib_conn; 910 911 iser_conn->state = ISER_CONN_INIT; 912 init_completion(&iser_conn->stop_completion); 913 init_completion(&iser_conn->ib_completion); 914 init_completion(&iser_conn->up_completion); 915 INIT_LIST_HEAD(&iser_conn->conn_list); 916 mutex_init(&iser_conn->state_mutex); 917 918 ib_conn->post_recv_buf_count = 0; 919 ib_conn->reg_cqe.done = iser_reg_comp; 920 } 921 922 /** 923 * starts the process of connecting to the target 924 * sleeps until the connection is established or rejected 925 */ 926 int iser_connect(struct iser_conn *iser_conn, 927 struct sockaddr *src_addr, 928 struct sockaddr *dst_addr, 929 int non_blocking) 930 { 931 struct ib_conn *ib_conn = &iser_conn->ib_conn; 932 int err = 0; 933 934 mutex_lock(&iser_conn->state_mutex); 935 936 sprintf(iser_conn->name, "%pISp", dst_addr); 937 938 iser_info("connecting to: %s\n", iser_conn->name); 939 940 /* the device is known only --after-- address resolution */ 941 ib_conn->device = NULL; 942 943 iser_conn->state = ISER_CONN_PENDING; 944 945 ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler, 946 (void *)iser_conn, 947 RDMA_PS_TCP, IB_QPT_RC); 948 if (IS_ERR(ib_conn->cma_id)) { 949 err = PTR_ERR(ib_conn->cma_id); 950 iser_err("rdma_create_id failed: %d\n", err); 951 goto id_failure; 952 } 953 954 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000); 955 if (err) { 956 iser_err("rdma_resolve_addr failed: %d\n", err); 957 goto addr_failure; 958 } 959 960 if (!non_blocking) { 961 wait_for_completion_interruptible(&iser_conn->up_completion); 962 963 if (iser_conn->state != ISER_CONN_UP) { 964 err = -EIO; 965 goto connect_failure; 966 } 967 } 968 mutex_unlock(&iser_conn->state_mutex); 969 970 mutex_lock(&ig.connlist_mutex); 971 list_add(&iser_conn->conn_list, &ig.connlist); 972 mutex_unlock(&ig.connlist_mutex); 973 return 0; 974 975 id_failure: 976 ib_conn->cma_id = NULL; 977 addr_failure: 978 iser_conn->state = ISER_CONN_DOWN; 979 connect_failure: 980 mutex_unlock(&iser_conn->state_mutex); 981 iser_conn_release(iser_conn); 982 return err; 983 } 984 985 int iser_post_recvl(struct iser_conn *iser_conn) 986 { 987 struct ib_conn *ib_conn = &iser_conn->ib_conn; 988 struct iser_login_desc *desc = &iser_conn->login_desc; 989 struct ib_recv_wr wr; 990 int ib_ret; 991 992 desc->sge.addr = desc->rsp_dma; 993 desc->sge.length = ISER_RX_LOGIN_SIZE; 994 desc->sge.lkey = ib_conn->device->pd->local_dma_lkey; 995 996 desc->cqe.done = iser_login_rsp; 997 wr.wr_cqe = &desc->cqe; 998 wr.sg_list = &desc->sge; 999 wr.num_sge = 1; 1000 wr.next = NULL; 1001 1002 ib_conn->post_recv_buf_count++; 1003 ib_ret = ib_post_recv(ib_conn->qp, &wr, NULL); 1004 if (ib_ret) { 1005 iser_err("ib_post_recv failed ret=%d\n", ib_ret); 1006 ib_conn->post_recv_buf_count--; 1007 } 1008 1009 return ib_ret; 1010 } 1011 1012 int iser_post_recvm(struct iser_conn *iser_conn, int count) 1013 { 1014 struct ib_conn *ib_conn = &iser_conn->ib_conn; 1015 unsigned int my_rx_head = iser_conn->rx_desc_head; 1016 struct iser_rx_desc *rx_desc; 1017 struct ib_recv_wr *wr; 1018 int i, ib_ret; 1019 1020 for (wr = ib_conn->rx_wr, i = 0; i < count; i++, wr++) { 1021 rx_desc = &iser_conn->rx_descs[my_rx_head]; 1022 rx_desc->cqe.done = iser_task_rsp; 1023 wr->wr_cqe = &rx_desc->cqe; 1024 wr->sg_list = &rx_desc->rx_sg; 1025 wr->num_sge = 1; 1026 wr->next = wr + 1; 1027 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask; 1028 } 1029 1030 wr--; 1031 wr->next = NULL; /* mark end of work requests list */ 1032 1033 ib_conn->post_recv_buf_count += count; 1034 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, NULL); 1035 if (unlikely(ib_ret)) { 1036 iser_err("ib_post_recv failed ret=%d\n", ib_ret); 1037 ib_conn->post_recv_buf_count -= count; 1038 } else 1039 iser_conn->rx_desc_head = my_rx_head; 1040 1041 return ib_ret; 1042 } 1043 1044 1045 /** 1046 * iser_post_send - Initiate a Send DTO operation 1047 * @ib_conn: connection RDMA resources 1048 * @tx_desc: iSER TX descriptor 1049 * @signal: true to send work request as SIGNALED 1050 * 1051 * Return: 0 on success, -1 on failure 1052 */ 1053 int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc, 1054 bool signal) 1055 { 1056 struct ib_send_wr *wr = &tx_desc->send_wr; 1057 struct ib_send_wr *first_wr; 1058 int ib_ret; 1059 1060 ib_dma_sync_single_for_device(ib_conn->device->ib_device, 1061 tx_desc->dma_addr, ISER_HEADERS_LEN, 1062 DMA_TO_DEVICE); 1063 1064 wr->next = NULL; 1065 wr->wr_cqe = &tx_desc->cqe; 1066 wr->sg_list = tx_desc->tx_sg; 1067 wr->num_sge = tx_desc->num_sge; 1068 wr->opcode = IB_WR_SEND; 1069 wr->send_flags = signal ? IB_SEND_SIGNALED : 0; 1070 1071 if (tx_desc->inv_wr.next) 1072 first_wr = &tx_desc->inv_wr; 1073 else if (tx_desc->reg_wr.wr.next) 1074 first_wr = &tx_desc->reg_wr.wr; 1075 else 1076 first_wr = wr; 1077 1078 ib_ret = ib_post_send(ib_conn->qp, first_wr, NULL); 1079 if (unlikely(ib_ret)) 1080 iser_err("ib_post_send failed, ret:%d opcode:%d\n", 1081 ib_ret, wr->opcode); 1082 1083 return ib_ret; 1084 } 1085 1086 u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task, 1087 enum iser_data_dir cmd_dir, sector_t *sector) 1088 { 1089 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir]; 1090 struct iser_fr_desc *desc = reg->mem_h; 1091 unsigned long sector_size = iser_task->sc->device->sector_size; 1092 struct ib_mr_status mr_status; 1093 int ret; 1094 1095 if (desc && desc->sig_protected) { 1096 desc->sig_protected = false; 1097 ret = ib_check_mr_status(desc->rsc.sig_mr, 1098 IB_MR_CHECK_SIG_STATUS, &mr_status); 1099 if (ret) { 1100 iser_err("ib_check_mr_status failed, ret %d\n", ret); 1101 /* Not a lot we can do, return ambiguous guard error */ 1102 *sector = 0; 1103 return 0x1; 1104 } 1105 1106 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { 1107 sector_t sector_off = mr_status.sig_err.sig_err_offset; 1108 1109 sector_div(sector_off, sector_size + 8); 1110 *sector = scsi_get_lba(iser_task->sc) + sector_off; 1111 1112 iser_err("PI error found type %d at sector %llx " 1113 "expected %x vs actual %x\n", 1114 mr_status.sig_err.err_type, 1115 (unsigned long long)*sector, 1116 mr_status.sig_err.expected, 1117 mr_status.sig_err.actual); 1118 1119 switch (mr_status.sig_err.err_type) { 1120 case IB_SIG_BAD_GUARD: 1121 return 0x1; 1122 case IB_SIG_BAD_REFTAG: 1123 return 0x3; 1124 case IB_SIG_BAD_APPTAG: 1125 return 0x2; 1126 } 1127 } 1128 } 1129 1130 return 0; 1131 } 1132 1133 void iser_err_comp(struct ib_wc *wc, const char *type) 1134 { 1135 if (wc->status != IB_WC_WR_FLUSH_ERR) { 1136 struct iser_conn *iser_conn = to_iser_conn(wc->qp->qp_context); 1137 1138 iser_err("%s failure: %s (%d) vend_err %#x\n", type, 1139 ib_wc_status_msg(wc->status), wc->status, 1140 wc->vendor_err); 1141 1142 if (iser_conn->iscsi_conn) 1143 iscsi_conn_failure(iser_conn->iscsi_conn, 1144 ISCSI_ERR_CONN_FAILED); 1145 } else { 1146 iser_dbg("%s failure: %s (%d)\n", type, 1147 ib_wc_status_msg(wc->status), wc->status); 1148 } 1149 } 1150