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