1/******************************************************************************* 2 * This file contains iSCSI extentions for RDMA (iSER) Verbs 3 * 4 * (c) Copyright 2013 Datera, Inc. 5 * 6 * Nicholas A. Bellinger <nab@linux-iscsi.org> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 ****************************************************************************/ 18 19#include <linux/string.h> 20#include <linux/module.h> 21#include <linux/scatterlist.h> 22#include <linux/socket.h> 23#include <linux/in.h> 24#include <linux/in6.h> 25#include <rdma/ib_verbs.h> 26#include <rdma/rdma_cm.h> 27#include <target/target_core_base.h> 28#include <target/target_core_fabric.h> 29#include <target/iscsi/iscsi_transport.h> 30#include <linux/semaphore.h> 31 32#include "isert_proto.h" 33#include "ib_isert.h" 34 35#define ISERT_MAX_CONN 8 36#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN) 37#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN) 38#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \ 39 ISERT_MAX_CONN) 40 41static int isert_debug_level; 42module_param_named(debug_level, isert_debug_level, int, 0644); 43MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)"); 44 45static DEFINE_MUTEX(device_list_mutex); 46static LIST_HEAD(device_list); 47static struct workqueue_struct *isert_comp_wq; 48static struct workqueue_struct *isert_release_wq; 49 50static void 51isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn); 52static int 53isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 54 struct isert_rdma_wr *wr); 55static void 56isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn); 57static int 58isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 59 struct isert_rdma_wr *wr); 60static int 61isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd); 62static int 63isert_rdma_post_recvl(struct isert_conn *isert_conn); 64static int 65isert_rdma_accept(struct isert_conn *isert_conn); 66struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np); 67 68static void isert_release_work(struct work_struct *work); 69 70static inline bool 71isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd) 72{ 73 return (conn->pi_support && 74 cmd->prot_op != TARGET_PROT_NORMAL); 75} 76 77 78static void 79isert_qp_event_callback(struct ib_event *e, void *context) 80{ 81 struct isert_conn *isert_conn = context; 82 83 isert_err("conn %p event: %d\n", isert_conn, e->event); 84 switch (e->event) { 85 case IB_EVENT_COMM_EST: 86 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST); 87 break; 88 case IB_EVENT_QP_LAST_WQE_REACHED: 89 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n"); 90 break; 91 default: 92 break; 93 } 94} 95 96static int 97isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr) 98{ 99 int ret; 100 101 ret = ib_query_device(ib_dev, devattr); 102 if (ret) { 103 isert_err("ib_query_device() failed: %d\n", ret); 104 return ret; 105 } 106 isert_dbg("devattr->max_sge: %d\n", devattr->max_sge); 107 isert_dbg("devattr->max_sge_rd: %d\n", devattr->max_sge_rd); 108 109 return 0; 110} 111 112static struct isert_comp * 113isert_comp_get(struct isert_conn *isert_conn) 114{ 115 struct isert_device *device = isert_conn->device; 116 struct isert_comp *comp; 117 int i, min = 0; 118 119 mutex_lock(&device_list_mutex); 120 for (i = 0; i < device->comps_used; i++) 121 if (device->comps[i].active_qps < 122 device->comps[min].active_qps) 123 min = i; 124 comp = &device->comps[min]; 125 comp->active_qps++; 126 mutex_unlock(&device_list_mutex); 127 128 isert_info("conn %p, using comp %p min_index: %d\n", 129 isert_conn, comp, min); 130 131 return comp; 132} 133 134static void 135isert_comp_put(struct isert_comp *comp) 136{ 137 mutex_lock(&device_list_mutex); 138 comp->active_qps--; 139 mutex_unlock(&device_list_mutex); 140} 141 142static struct ib_qp * 143isert_create_qp(struct isert_conn *isert_conn, 144 struct isert_comp *comp, 145 struct rdma_cm_id *cma_id) 146{ 147 struct isert_device *device = isert_conn->device; 148 struct ib_qp_init_attr attr; 149 int ret; 150 151 memset(&attr, 0, sizeof(struct ib_qp_init_attr)); 152 attr.event_handler = isert_qp_event_callback; 153 attr.qp_context = isert_conn; 154 attr.send_cq = comp->cq; 155 attr.recv_cq = comp->cq; 156 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS; 157 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1; 158 /* 159 * FIXME: Use devattr.max_sge - 2 for max_send_sge as 160 * work-around for RDMA_READs with ConnectX-2. 161 * 162 * Also, still make sure to have at least two SGEs for 163 * outgoing control PDU responses. 164 */ 165 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2); 166 isert_conn->max_sge = attr.cap.max_send_sge; 167 168 attr.cap.max_recv_sge = 1; 169 attr.sq_sig_type = IB_SIGNAL_REQ_WR; 170 attr.qp_type = IB_QPT_RC; 171 if (device->pi_capable) 172 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN; 173 174 ret = rdma_create_qp(cma_id, device->pd, &attr); 175 if (ret) { 176 isert_err("rdma_create_qp failed for cma_id %d\n", ret); 177 return ERR_PTR(ret); 178 } 179 180 return cma_id->qp; 181} 182 183static int 184isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id) 185{ 186 struct isert_comp *comp; 187 int ret; 188 189 comp = isert_comp_get(isert_conn); 190 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id); 191 if (IS_ERR(isert_conn->qp)) { 192 ret = PTR_ERR(isert_conn->qp); 193 goto err; 194 } 195 196 return 0; 197err: 198 isert_comp_put(comp); 199 return ret; 200} 201 202static void 203isert_cq_event_callback(struct ib_event *e, void *context) 204{ 205 isert_dbg("event: %d\n", e->event); 206} 207 208static int 209isert_alloc_rx_descriptors(struct isert_conn *isert_conn) 210{ 211 struct isert_device *device = isert_conn->device; 212 struct ib_device *ib_dev = device->ib_device; 213 struct iser_rx_desc *rx_desc; 214 struct ib_sge *rx_sg; 215 u64 dma_addr; 216 int i, j; 217 218 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS * 219 sizeof(struct iser_rx_desc), GFP_KERNEL); 220 if (!isert_conn->rx_descs) 221 goto fail; 222 223 rx_desc = isert_conn->rx_descs; 224 225 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) { 226 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc, 227 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE); 228 if (ib_dma_mapping_error(ib_dev, dma_addr)) 229 goto dma_map_fail; 230 231 rx_desc->dma_addr = dma_addr; 232 233 rx_sg = &rx_desc->rx_sg; 234 rx_sg->addr = rx_desc->dma_addr; 235 rx_sg->length = ISER_RX_PAYLOAD_SIZE; 236 rx_sg->lkey = device->mr->lkey; 237 } 238 239 isert_conn->rx_desc_head = 0; 240 241 return 0; 242 243dma_map_fail: 244 rx_desc = isert_conn->rx_descs; 245 for (j = 0; j < i; j++, rx_desc++) { 246 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr, 247 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE); 248 } 249 kfree(isert_conn->rx_descs); 250 isert_conn->rx_descs = NULL; 251fail: 252 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn); 253 254 return -ENOMEM; 255} 256 257static void 258isert_free_rx_descriptors(struct isert_conn *isert_conn) 259{ 260 struct ib_device *ib_dev = isert_conn->device->ib_device; 261 struct iser_rx_desc *rx_desc; 262 int i; 263 264 if (!isert_conn->rx_descs) 265 return; 266 267 rx_desc = isert_conn->rx_descs; 268 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) { 269 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr, 270 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE); 271 } 272 273 kfree(isert_conn->rx_descs); 274 isert_conn->rx_descs = NULL; 275} 276 277static void isert_cq_work(struct work_struct *); 278static void isert_cq_callback(struct ib_cq *, void *); 279 280static void 281isert_free_comps(struct isert_device *device) 282{ 283 int i; 284 285 for (i = 0; i < device->comps_used; i++) { 286 struct isert_comp *comp = &device->comps[i]; 287 288 if (comp->cq) { 289 cancel_work_sync(&comp->work); 290 ib_destroy_cq(comp->cq); 291 } 292 } 293 kfree(device->comps); 294} 295 296static int 297isert_alloc_comps(struct isert_device *device, 298 struct ib_device_attr *attr) 299{ 300 int i, max_cqe, ret = 0; 301 302 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(), 303 device->ib_device->num_comp_vectors)); 304 305 isert_info("Using %d CQs, %s supports %d vectors support " 306 "Fast registration %d pi_capable %d\n", 307 device->comps_used, device->ib_device->name, 308 device->ib_device->num_comp_vectors, device->use_fastreg, 309 device->pi_capable); 310 311 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp), 312 GFP_KERNEL); 313 if (!device->comps) { 314 isert_err("Unable to allocate completion contexts\n"); 315 return -ENOMEM; 316 } 317 318 max_cqe = min(ISER_MAX_CQ_LEN, attr->max_cqe); 319 320 for (i = 0; i < device->comps_used; i++) { 321 struct isert_comp *comp = &device->comps[i]; 322 323 comp->device = device; 324 INIT_WORK(&comp->work, isert_cq_work); 325 comp->cq = ib_create_cq(device->ib_device, 326 isert_cq_callback, 327 isert_cq_event_callback, 328 (void *)comp, 329 max_cqe, i); 330 if (IS_ERR(comp->cq)) { 331 isert_err("Unable to allocate cq\n"); 332 ret = PTR_ERR(comp->cq); 333 comp->cq = NULL; 334 goto out_cq; 335 } 336 337 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP); 338 if (ret) 339 goto out_cq; 340 } 341 342 return 0; 343out_cq: 344 isert_free_comps(device); 345 return ret; 346} 347 348static int 349isert_create_device_ib_res(struct isert_device *device) 350{ 351 struct ib_device_attr *dev_attr; 352 int ret; 353 354 dev_attr = &device->dev_attr; 355 ret = isert_query_device(device->ib_device, dev_attr); 356 if (ret) 357 return ret; 358 359 /* asign function handlers */ 360 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS && 361 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) { 362 device->use_fastreg = 1; 363 device->reg_rdma_mem = isert_reg_rdma; 364 device->unreg_rdma_mem = isert_unreg_rdma; 365 } else { 366 device->use_fastreg = 0; 367 device->reg_rdma_mem = isert_map_rdma; 368 device->unreg_rdma_mem = isert_unmap_cmd; 369 } 370 371 ret = isert_alloc_comps(device, dev_attr); 372 if (ret) 373 return ret; 374 375 device->pd = ib_alloc_pd(device->ib_device); 376 if (IS_ERR(device->pd)) { 377 ret = PTR_ERR(device->pd); 378 isert_err("failed to allocate pd, device %p, ret=%d\n", 379 device, ret); 380 goto out_cq; 381 } 382 383 device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE); 384 if (IS_ERR(device->mr)) { 385 ret = PTR_ERR(device->mr); 386 isert_err("failed to create dma mr, device %p, ret=%d\n", 387 device, ret); 388 goto out_mr; 389 } 390 391 /* Check signature cap */ 392 device->pi_capable = dev_attr->device_cap_flags & 393 IB_DEVICE_SIGNATURE_HANDOVER ? true : false; 394 395 return 0; 396 397out_mr: 398 ib_dealloc_pd(device->pd); 399out_cq: 400 isert_free_comps(device); 401 return ret; 402} 403 404static void 405isert_free_device_ib_res(struct isert_device *device) 406{ 407 isert_info("device %p\n", device); 408 409 ib_dereg_mr(device->mr); 410 ib_dealloc_pd(device->pd); 411 isert_free_comps(device); 412} 413 414static void 415isert_device_put(struct isert_device *device) 416{ 417 mutex_lock(&device_list_mutex); 418 device->refcount--; 419 isert_info("device %p refcount %d\n", device, device->refcount); 420 if (!device->refcount) { 421 isert_free_device_ib_res(device); 422 list_del(&device->dev_node); 423 kfree(device); 424 } 425 mutex_unlock(&device_list_mutex); 426} 427 428static struct isert_device * 429isert_device_get(struct rdma_cm_id *cma_id) 430{ 431 struct isert_device *device; 432 int ret; 433 434 mutex_lock(&device_list_mutex); 435 list_for_each_entry(device, &device_list, dev_node) { 436 if (device->ib_device->node_guid == cma_id->device->node_guid) { 437 device->refcount++; 438 isert_info("Found iser device %p refcount %d\n", 439 device, device->refcount); 440 mutex_unlock(&device_list_mutex); 441 return device; 442 } 443 } 444 445 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL); 446 if (!device) { 447 mutex_unlock(&device_list_mutex); 448 return ERR_PTR(-ENOMEM); 449 } 450 451 INIT_LIST_HEAD(&device->dev_node); 452 453 device->ib_device = cma_id->device; 454 ret = isert_create_device_ib_res(device); 455 if (ret) { 456 kfree(device); 457 mutex_unlock(&device_list_mutex); 458 return ERR_PTR(ret); 459 } 460 461 device->refcount++; 462 list_add_tail(&device->dev_node, &device_list); 463 isert_info("Created a new iser device %p refcount %d\n", 464 device, device->refcount); 465 mutex_unlock(&device_list_mutex); 466 467 return device; 468} 469 470static void 471isert_conn_free_fastreg_pool(struct isert_conn *isert_conn) 472{ 473 struct fast_reg_descriptor *fr_desc, *tmp; 474 int i = 0; 475 476 if (list_empty(&isert_conn->fr_pool)) 477 return; 478 479 isert_info("Freeing conn %p fastreg pool", isert_conn); 480 481 list_for_each_entry_safe(fr_desc, tmp, 482 &isert_conn->fr_pool, list) { 483 list_del(&fr_desc->list); 484 ib_free_fast_reg_page_list(fr_desc->data_frpl); 485 ib_dereg_mr(fr_desc->data_mr); 486 if (fr_desc->pi_ctx) { 487 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl); 488 ib_dereg_mr(fr_desc->pi_ctx->prot_mr); 489 ib_destroy_mr(fr_desc->pi_ctx->sig_mr); 490 kfree(fr_desc->pi_ctx); 491 } 492 kfree(fr_desc); 493 ++i; 494 } 495 496 if (i < isert_conn->fr_pool_size) 497 isert_warn("Pool still has %d regions registered\n", 498 isert_conn->fr_pool_size - i); 499} 500 501static int 502isert_create_pi_ctx(struct fast_reg_descriptor *desc, 503 struct ib_device *device, 504 struct ib_pd *pd) 505{ 506 struct ib_mr_init_attr mr_init_attr; 507 struct pi_context *pi_ctx; 508 int ret; 509 510 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL); 511 if (!pi_ctx) { 512 isert_err("Failed to allocate pi context\n"); 513 return -ENOMEM; 514 } 515 516 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device, 517 ISCSI_ISER_SG_TABLESIZE); 518 if (IS_ERR(pi_ctx->prot_frpl)) { 519 isert_err("Failed to allocate prot frpl err=%ld\n", 520 PTR_ERR(pi_ctx->prot_frpl)); 521 ret = PTR_ERR(pi_ctx->prot_frpl); 522 goto err_pi_ctx; 523 } 524 525 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE); 526 if (IS_ERR(pi_ctx->prot_mr)) { 527 isert_err("Failed to allocate prot frmr err=%ld\n", 528 PTR_ERR(pi_ctx->prot_mr)); 529 ret = PTR_ERR(pi_ctx->prot_mr); 530 goto err_prot_frpl; 531 } 532 desc->ind |= ISERT_PROT_KEY_VALID; 533 534 memset(&mr_init_attr, 0, sizeof(mr_init_attr)); 535 mr_init_attr.max_reg_descriptors = 2; 536 mr_init_attr.flags |= IB_MR_SIGNATURE_EN; 537 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr); 538 if (IS_ERR(pi_ctx->sig_mr)) { 539 isert_err("Failed to allocate signature enabled mr err=%ld\n", 540 PTR_ERR(pi_ctx->sig_mr)); 541 ret = PTR_ERR(pi_ctx->sig_mr); 542 goto err_prot_mr; 543 } 544 545 desc->pi_ctx = pi_ctx; 546 desc->ind |= ISERT_SIG_KEY_VALID; 547 desc->ind &= ~ISERT_PROTECTED; 548 549 return 0; 550 551err_prot_mr: 552 ib_dereg_mr(pi_ctx->prot_mr); 553err_prot_frpl: 554 ib_free_fast_reg_page_list(pi_ctx->prot_frpl); 555err_pi_ctx: 556 kfree(pi_ctx); 557 558 return ret; 559} 560 561static int 562isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd, 563 struct fast_reg_descriptor *fr_desc) 564{ 565 int ret; 566 567 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device, 568 ISCSI_ISER_SG_TABLESIZE); 569 if (IS_ERR(fr_desc->data_frpl)) { 570 isert_err("Failed to allocate data frpl err=%ld\n", 571 PTR_ERR(fr_desc->data_frpl)); 572 return PTR_ERR(fr_desc->data_frpl); 573 } 574 575 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE); 576 if (IS_ERR(fr_desc->data_mr)) { 577 isert_err("Failed to allocate data frmr err=%ld\n", 578 PTR_ERR(fr_desc->data_mr)); 579 ret = PTR_ERR(fr_desc->data_mr); 580 goto err_data_frpl; 581 } 582 fr_desc->ind |= ISERT_DATA_KEY_VALID; 583 584 isert_dbg("Created fr_desc %p\n", fr_desc); 585 586 return 0; 587 588err_data_frpl: 589 ib_free_fast_reg_page_list(fr_desc->data_frpl); 590 591 return ret; 592} 593 594static int 595isert_conn_create_fastreg_pool(struct isert_conn *isert_conn) 596{ 597 struct fast_reg_descriptor *fr_desc; 598 struct isert_device *device = isert_conn->device; 599 struct se_session *se_sess = isert_conn->conn->sess->se_sess; 600 struct se_node_acl *se_nacl = se_sess->se_node_acl; 601 int i, ret, tag_num; 602 /* 603 * Setup the number of FRMRs based upon the number of tags 604 * available to session in iscsi_target_locate_portal(). 605 */ 606 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth); 607 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS; 608 609 isert_conn->fr_pool_size = 0; 610 for (i = 0; i < tag_num; i++) { 611 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL); 612 if (!fr_desc) { 613 isert_err("Failed to allocate fast_reg descriptor\n"); 614 ret = -ENOMEM; 615 goto err; 616 } 617 618 ret = isert_create_fr_desc(device->ib_device, 619 device->pd, fr_desc); 620 if (ret) { 621 isert_err("Failed to create fastreg descriptor err=%d\n", 622 ret); 623 kfree(fr_desc); 624 goto err; 625 } 626 627 list_add_tail(&fr_desc->list, &isert_conn->fr_pool); 628 isert_conn->fr_pool_size++; 629 } 630 631 isert_dbg("Creating conn %p fastreg pool size=%d", 632 isert_conn, isert_conn->fr_pool_size); 633 634 return 0; 635 636err: 637 isert_conn_free_fastreg_pool(isert_conn); 638 return ret; 639} 640 641static void 642isert_init_conn(struct isert_conn *isert_conn) 643{ 644 isert_conn->state = ISER_CONN_INIT; 645 INIT_LIST_HEAD(&isert_conn->accept_node); 646 init_completion(&isert_conn->login_comp); 647 init_completion(&isert_conn->login_req_comp); 648 init_completion(&isert_conn->wait); 649 kref_init(&isert_conn->kref); 650 mutex_init(&isert_conn->mutex); 651 spin_lock_init(&isert_conn->pool_lock); 652 INIT_LIST_HEAD(&isert_conn->fr_pool); 653 INIT_WORK(&isert_conn->release_work, isert_release_work); 654} 655 656static void 657isert_free_login_buf(struct isert_conn *isert_conn) 658{ 659 struct ib_device *ib_dev = isert_conn->device->ib_device; 660 661 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma, 662 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE); 663 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma, 664 ISCSI_DEF_MAX_RECV_SEG_LEN, 665 DMA_FROM_DEVICE); 666 kfree(isert_conn->login_buf); 667} 668 669static int 670isert_alloc_login_buf(struct isert_conn *isert_conn, 671 struct ib_device *ib_dev) 672{ 673 int ret; 674 675 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN + 676 ISER_RX_LOGIN_SIZE, GFP_KERNEL); 677 if (!isert_conn->login_buf) { 678 isert_err("Unable to allocate isert_conn->login_buf\n"); 679 return -ENOMEM; 680 } 681 682 isert_conn->login_req_buf = isert_conn->login_buf; 683 isert_conn->login_rsp_buf = isert_conn->login_buf + 684 ISCSI_DEF_MAX_RECV_SEG_LEN; 685 686 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n", 687 isert_conn->login_buf, isert_conn->login_req_buf, 688 isert_conn->login_rsp_buf); 689 690 isert_conn->login_req_dma = ib_dma_map_single(ib_dev, 691 (void *)isert_conn->login_req_buf, 692 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE); 693 694 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma); 695 if (ret) { 696 isert_err("login_req_dma mapping error: %d\n", ret); 697 isert_conn->login_req_dma = 0; 698 goto out_login_buf; 699 } 700 701 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev, 702 (void *)isert_conn->login_rsp_buf, 703 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE); 704 705 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma); 706 if (ret) { 707 isert_err("login_rsp_dma mapping error: %d\n", ret); 708 isert_conn->login_rsp_dma = 0; 709 goto out_req_dma_map; 710 } 711 712 return 0; 713 714out_req_dma_map: 715 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma, 716 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE); 717out_login_buf: 718 kfree(isert_conn->login_buf); 719 return ret; 720} 721 722static int 723isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event) 724{ 725 struct isert_np *isert_np = cma_id->context; 726 struct iscsi_np *np = isert_np->np; 727 struct isert_conn *isert_conn; 728 struct isert_device *device; 729 int ret = 0; 730 731 spin_lock_bh(&np->np_thread_lock); 732 if (!np->enabled) { 733 spin_unlock_bh(&np->np_thread_lock); 734 isert_dbg("iscsi_np is not enabled, reject connect request\n"); 735 return rdma_reject(cma_id, NULL, 0); 736 } 737 spin_unlock_bh(&np->np_thread_lock); 738 739 isert_dbg("cma_id: %p, portal: %p\n", 740 cma_id, cma_id->context); 741 742 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL); 743 if (!isert_conn) 744 return -ENOMEM; 745 746 isert_init_conn(isert_conn); 747 isert_conn->cm_id = cma_id; 748 749 ret = isert_alloc_login_buf(isert_conn, cma_id->device); 750 if (ret) 751 goto out; 752 753 device = isert_device_get(cma_id); 754 if (IS_ERR(device)) { 755 ret = PTR_ERR(device); 756 goto out_rsp_dma_map; 757 } 758 isert_conn->device = device; 759 760 /* Set max inflight RDMA READ requests */ 761 isert_conn->initiator_depth = min_t(u8, 762 event->param.conn.initiator_depth, 763 device->dev_attr.max_qp_init_rd_atom); 764 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth); 765 766 ret = isert_conn_setup_qp(isert_conn, cma_id); 767 if (ret) 768 goto out_conn_dev; 769 770 ret = isert_rdma_post_recvl(isert_conn); 771 if (ret) 772 goto out_conn_dev; 773 774 ret = isert_rdma_accept(isert_conn); 775 if (ret) 776 goto out_conn_dev; 777 778 mutex_lock(&isert_np->np_accept_mutex); 779 list_add_tail(&isert_conn->accept_node, &isert_np->np_accept_list); 780 mutex_unlock(&isert_np->np_accept_mutex); 781 782 isert_info("np %p: Allow accept_np to continue\n", np); 783 up(&isert_np->np_sem); 784 return 0; 785 786out_conn_dev: 787 isert_device_put(device); 788out_rsp_dma_map: 789 isert_free_login_buf(isert_conn); 790out: 791 kfree(isert_conn); 792 rdma_reject(cma_id, NULL, 0); 793 return ret; 794} 795 796static void 797isert_connect_release(struct isert_conn *isert_conn) 798{ 799 struct isert_device *device = isert_conn->device; 800 801 isert_dbg("conn %p\n", isert_conn); 802 803 BUG_ON(!device); 804 805 if (device->use_fastreg) 806 isert_conn_free_fastreg_pool(isert_conn); 807 808 isert_free_rx_descriptors(isert_conn); 809 if (isert_conn->cm_id) 810 rdma_destroy_id(isert_conn->cm_id); 811 812 if (isert_conn->qp) { 813 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context; 814 815 isert_comp_put(comp); 816 ib_destroy_qp(isert_conn->qp); 817 } 818 819 if (isert_conn->login_buf) 820 isert_free_login_buf(isert_conn); 821 822 isert_device_put(device); 823 824 kfree(isert_conn); 825} 826 827static void 828isert_connected_handler(struct rdma_cm_id *cma_id) 829{ 830 struct isert_conn *isert_conn = cma_id->qp->qp_context; 831 832 isert_info("conn %p\n", isert_conn); 833 834 if (!kref_get_unless_zero(&isert_conn->kref)) { 835 isert_warn("conn %p connect_release is running\n", isert_conn); 836 return; 837 } 838 839 mutex_lock(&isert_conn->mutex); 840 if (isert_conn->state != ISER_CONN_FULL_FEATURE) 841 isert_conn->state = ISER_CONN_UP; 842 mutex_unlock(&isert_conn->mutex); 843} 844 845static void 846isert_release_kref(struct kref *kref) 847{ 848 struct isert_conn *isert_conn = container_of(kref, 849 struct isert_conn, kref); 850 851 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm, 852 current->pid); 853 854 isert_connect_release(isert_conn); 855} 856 857static void 858isert_put_conn(struct isert_conn *isert_conn) 859{ 860 kref_put(&isert_conn->kref, isert_release_kref); 861} 862 863/** 864 * isert_conn_terminate() - Initiate connection termination 865 * @isert_conn: isert connection struct 866 * 867 * Notes: 868 * In case the connection state is BOUND, move state 869 * to TEMINATING and start teardown sequence (rdma_disconnect). 870 * In case the connection state is UP, complete flush as well. 871 * 872 * This routine must be called with mutex held. Thus it is 873 * safe to call multiple times. 874 */ 875static void 876isert_conn_terminate(struct isert_conn *isert_conn) 877{ 878 int err; 879 880 switch (isert_conn->state) { 881 case ISER_CONN_TERMINATING: 882 break; 883 case ISER_CONN_UP: 884 case ISER_CONN_BOUND: 885 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */ 886 isert_info("Terminating conn %p state %d\n", 887 isert_conn, isert_conn->state); 888 isert_conn->state = ISER_CONN_TERMINATING; 889 err = rdma_disconnect(isert_conn->cm_id); 890 if (err) 891 isert_warn("Failed rdma_disconnect isert_conn %p\n", 892 isert_conn); 893 break; 894 default: 895 isert_warn("conn %p teminating in state %d\n", 896 isert_conn, isert_conn->state); 897 } 898} 899 900static int 901isert_np_cma_handler(struct isert_np *isert_np, 902 enum rdma_cm_event_type event) 903{ 904 isert_dbg("isert np %p, handling event %d\n", isert_np, event); 905 906 switch (event) { 907 case RDMA_CM_EVENT_DEVICE_REMOVAL: 908 isert_np->np_cm_id = NULL; 909 break; 910 case RDMA_CM_EVENT_ADDR_CHANGE: 911 isert_np->np_cm_id = isert_setup_id(isert_np); 912 if (IS_ERR(isert_np->np_cm_id)) { 913 isert_err("isert np %p setup id failed: %ld\n", 914 isert_np, PTR_ERR(isert_np->np_cm_id)); 915 isert_np->np_cm_id = NULL; 916 } 917 break; 918 default: 919 isert_err("isert np %p Unexpected event %d\n", 920 isert_np, event); 921 } 922 923 return -1; 924} 925 926static int 927isert_disconnected_handler(struct rdma_cm_id *cma_id, 928 enum rdma_cm_event_type event) 929{ 930 struct isert_np *isert_np = cma_id->context; 931 struct isert_conn *isert_conn = cma_id->qp->qp_context; 932 bool terminating = false; 933 934 mutex_lock(&isert_conn->mutex); 935 terminating = (isert_conn->state == ISER_CONN_TERMINATING); 936 isert_conn_terminate(isert_conn); 937 mutex_unlock(&isert_conn->mutex); 938 939 isert_info("conn %p completing wait\n", isert_conn); 940 complete(&isert_conn->wait); 941 942 if (terminating) 943 goto out; 944 945 mutex_lock(&isert_np->np_accept_mutex); 946 if (!list_empty(&isert_conn->accept_node)) { 947 list_del_init(&isert_conn->accept_node); 948 isert_put_conn(isert_conn); 949 queue_work(isert_release_wq, &isert_conn->release_work); 950 } 951 mutex_unlock(&isert_np->np_accept_mutex); 952 953out: 954 return 0; 955} 956 957static int 958isert_connect_error(struct rdma_cm_id *cma_id) 959{ 960 struct isert_conn *isert_conn = cma_id->qp->qp_context; 961 962 isert_conn->cm_id = NULL; 963 isert_put_conn(isert_conn); 964 965 return -1; 966} 967 968static int 969isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event) 970{ 971 struct isert_np *isert_np = cma_id->context; 972 int ret = 0; 973 974 isert_info("event %d status %d id %p np %p\n", event->event, 975 event->status, cma_id, cma_id->context); 976 977 if (isert_np->np_cm_id == cma_id) 978 return isert_np_cma_handler(cma_id->context, event->event); 979 980 switch (event->event) { 981 case RDMA_CM_EVENT_CONNECT_REQUEST: 982 ret = isert_connect_request(cma_id, event); 983 if (ret) 984 isert_err("failed handle connect request %d\n", ret); 985 break; 986 case RDMA_CM_EVENT_ESTABLISHED: 987 isert_connected_handler(cma_id); 988 break; 989 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */ 990 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */ 991 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */ 992 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */ 993 ret = isert_disconnected_handler(cma_id, event->event); 994 break; 995 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */ 996 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */ 997 case RDMA_CM_EVENT_CONNECT_ERROR: 998 ret = isert_connect_error(cma_id); 999 break; 1000 default: 1001 isert_err("Unhandled RDMA CMA event: %d\n", event->event); 1002 break; 1003 } 1004 1005 return ret; 1006} 1007 1008static int 1009isert_post_recv(struct isert_conn *isert_conn, u32 count) 1010{ 1011 struct ib_recv_wr *rx_wr, *rx_wr_failed; 1012 int i, ret; 1013 unsigned int rx_head = isert_conn->rx_desc_head; 1014 struct iser_rx_desc *rx_desc; 1015 1016 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) { 1017 rx_desc = &isert_conn->rx_descs[rx_head]; 1018 rx_wr->wr_id = (uintptr_t)rx_desc; 1019 rx_wr->sg_list = &rx_desc->rx_sg; 1020 rx_wr->num_sge = 1; 1021 rx_wr->next = rx_wr + 1; 1022 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1); 1023 } 1024 1025 rx_wr--; 1026 rx_wr->next = NULL; /* mark end of work requests list */ 1027 1028 isert_conn->post_recv_buf_count += count; 1029 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr, 1030 &rx_wr_failed); 1031 if (ret) { 1032 isert_err("ib_post_recv() failed with ret: %d\n", ret); 1033 isert_conn->post_recv_buf_count -= count; 1034 } else { 1035 isert_dbg("Posted %d RX buffers\n", count); 1036 isert_conn->rx_desc_head = rx_head; 1037 } 1038 return ret; 1039} 1040 1041static int 1042isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc) 1043{ 1044 struct ib_device *ib_dev = isert_conn->cm_id->device; 1045 struct ib_send_wr send_wr, *send_wr_failed; 1046 int ret; 1047 1048 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr, 1049 ISER_HEADERS_LEN, DMA_TO_DEVICE); 1050 1051 send_wr.next = NULL; 1052 send_wr.wr_id = (uintptr_t)tx_desc; 1053 send_wr.sg_list = tx_desc->tx_sg; 1054 send_wr.num_sge = tx_desc->num_sge; 1055 send_wr.opcode = IB_WR_SEND; 1056 send_wr.send_flags = IB_SEND_SIGNALED; 1057 1058 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed); 1059 if (ret) 1060 isert_err("ib_post_send() failed, ret: %d\n", ret); 1061 1062 return ret; 1063} 1064 1065static void 1066isert_create_send_desc(struct isert_conn *isert_conn, 1067 struct isert_cmd *isert_cmd, 1068 struct iser_tx_desc *tx_desc) 1069{ 1070 struct isert_device *device = isert_conn->device; 1071 struct ib_device *ib_dev = device->ib_device; 1072 1073 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr, 1074 ISER_HEADERS_LEN, DMA_TO_DEVICE); 1075 1076 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr)); 1077 tx_desc->iser_header.flags = ISER_VER; 1078 1079 tx_desc->num_sge = 1; 1080 tx_desc->isert_cmd = isert_cmd; 1081 1082 if (tx_desc->tx_sg[0].lkey != device->mr->lkey) { 1083 tx_desc->tx_sg[0].lkey = device->mr->lkey; 1084 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc); 1085 } 1086} 1087 1088static int 1089isert_init_tx_hdrs(struct isert_conn *isert_conn, 1090 struct iser_tx_desc *tx_desc) 1091{ 1092 struct isert_device *device = isert_conn->device; 1093 struct ib_device *ib_dev = device->ib_device; 1094 u64 dma_addr; 1095 1096 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc, 1097 ISER_HEADERS_LEN, DMA_TO_DEVICE); 1098 if (ib_dma_mapping_error(ib_dev, dma_addr)) { 1099 isert_err("ib_dma_mapping_error() failed\n"); 1100 return -ENOMEM; 1101 } 1102 1103 tx_desc->dma_addr = dma_addr; 1104 tx_desc->tx_sg[0].addr = tx_desc->dma_addr; 1105 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN; 1106 tx_desc->tx_sg[0].lkey = device->mr->lkey; 1107 1108 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n", 1109 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length, 1110 tx_desc->tx_sg[0].lkey); 1111 1112 return 0; 1113} 1114 1115static void 1116isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd, 1117 struct ib_send_wr *send_wr) 1118{ 1119 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc; 1120 1121 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND; 1122 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc; 1123 send_wr->opcode = IB_WR_SEND; 1124 send_wr->sg_list = &tx_desc->tx_sg[0]; 1125 send_wr->num_sge = isert_cmd->tx_desc.num_sge; 1126 send_wr->send_flags = IB_SEND_SIGNALED; 1127} 1128 1129static int 1130isert_rdma_post_recvl(struct isert_conn *isert_conn) 1131{ 1132 struct ib_recv_wr rx_wr, *rx_wr_fail; 1133 struct ib_sge sge; 1134 int ret; 1135 1136 memset(&sge, 0, sizeof(struct ib_sge)); 1137 sge.addr = isert_conn->login_req_dma; 1138 sge.length = ISER_RX_LOGIN_SIZE; 1139 sge.lkey = isert_conn->device->mr->lkey; 1140 1141 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n", 1142 sge.addr, sge.length, sge.lkey); 1143 1144 memset(&rx_wr, 0, sizeof(struct ib_recv_wr)); 1145 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf; 1146 rx_wr.sg_list = &sge; 1147 rx_wr.num_sge = 1; 1148 1149 isert_conn->post_recv_buf_count++; 1150 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail); 1151 if (ret) { 1152 isert_err("ib_post_recv() failed: %d\n", ret); 1153 isert_conn->post_recv_buf_count--; 1154 } 1155 1156 return ret; 1157} 1158 1159static int 1160isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login, 1161 u32 length) 1162{ 1163 struct isert_conn *isert_conn = conn->context; 1164 struct isert_device *device = isert_conn->device; 1165 struct ib_device *ib_dev = device->ib_device; 1166 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc; 1167 int ret; 1168 1169 isert_create_send_desc(isert_conn, NULL, tx_desc); 1170 1171 memcpy(&tx_desc->iscsi_header, &login->rsp[0], 1172 sizeof(struct iscsi_hdr)); 1173 1174 isert_init_tx_hdrs(isert_conn, tx_desc); 1175 1176 if (length > 0) { 1177 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1]; 1178 1179 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma, 1180 length, DMA_TO_DEVICE); 1181 1182 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length); 1183 1184 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma, 1185 length, DMA_TO_DEVICE); 1186 1187 tx_dsg->addr = isert_conn->login_rsp_dma; 1188 tx_dsg->length = length; 1189 tx_dsg->lkey = isert_conn->device->mr->lkey; 1190 tx_desc->num_sge = 2; 1191 } 1192 if (!login->login_failed) { 1193 if (login->login_complete) { 1194 if (!conn->sess->sess_ops->SessionType && 1195 isert_conn->device->use_fastreg) { 1196 ret = isert_conn_create_fastreg_pool(isert_conn); 1197 if (ret) { 1198 isert_err("Conn: %p failed to create" 1199 " fastreg pool\n", isert_conn); 1200 return ret; 1201 } 1202 } 1203 1204 ret = isert_alloc_rx_descriptors(isert_conn); 1205 if (ret) 1206 return ret; 1207 1208 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX); 1209 if (ret) 1210 return ret; 1211 1212 /* Now we are in FULL_FEATURE phase */ 1213 mutex_lock(&isert_conn->mutex); 1214 isert_conn->state = ISER_CONN_FULL_FEATURE; 1215 mutex_unlock(&isert_conn->mutex); 1216 goto post_send; 1217 } 1218 1219 ret = isert_rdma_post_recvl(isert_conn); 1220 if (ret) 1221 return ret; 1222 } 1223post_send: 1224 ret = isert_post_send(isert_conn, tx_desc); 1225 if (ret) 1226 return ret; 1227 1228 return 0; 1229} 1230 1231static void 1232isert_rx_login_req(struct isert_conn *isert_conn) 1233{ 1234 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf; 1235 int rx_buflen = isert_conn->login_req_len; 1236 struct iscsi_conn *conn = isert_conn->conn; 1237 struct iscsi_login *login = conn->conn_login; 1238 int size; 1239 1240 isert_info("conn %p\n", isert_conn); 1241 1242 WARN_ON_ONCE(!login); 1243 1244 if (login->first_request) { 1245 struct iscsi_login_req *login_req = 1246 (struct iscsi_login_req *)&rx_desc->iscsi_header; 1247 /* 1248 * Setup the initial iscsi_login values from the leading 1249 * login request PDU. 1250 */ 1251 login->leading_connection = (!login_req->tsih) ? 1 : 0; 1252 login->current_stage = 1253 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) 1254 >> 2; 1255 login->version_min = login_req->min_version; 1256 login->version_max = login_req->max_version; 1257 memcpy(login->isid, login_req->isid, 6); 1258 login->cmd_sn = be32_to_cpu(login_req->cmdsn); 1259 login->init_task_tag = login_req->itt; 1260 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn); 1261 login->cid = be16_to_cpu(login_req->cid); 1262 login->tsih = be16_to_cpu(login_req->tsih); 1263 } 1264 1265 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN); 1266 1267 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS); 1268 isert_dbg("Using login payload size: %d, rx_buflen: %d " 1269 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen, 1270 MAX_KEY_VALUE_PAIRS); 1271 memcpy(login->req_buf, &rx_desc->data[0], size); 1272 1273 if (login->first_request) { 1274 complete(&isert_conn->login_comp); 1275 return; 1276 } 1277 schedule_delayed_work(&conn->login_work, 0); 1278} 1279 1280static struct iscsi_cmd 1281*isert_allocate_cmd(struct iscsi_conn *conn) 1282{ 1283 struct isert_conn *isert_conn = conn->context; 1284 struct isert_cmd *isert_cmd; 1285 struct iscsi_cmd *cmd; 1286 1287 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 1288 if (!cmd) { 1289 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n"); 1290 return NULL; 1291 } 1292 isert_cmd = iscsit_priv_cmd(cmd); 1293 isert_cmd->conn = isert_conn; 1294 isert_cmd->iscsi_cmd = cmd; 1295 1296 return cmd; 1297} 1298 1299static int 1300isert_handle_scsi_cmd(struct isert_conn *isert_conn, 1301 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd, 1302 struct iser_rx_desc *rx_desc, unsigned char *buf) 1303{ 1304 struct iscsi_conn *conn = isert_conn->conn; 1305 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf; 1306 struct scatterlist *sg; 1307 int imm_data, imm_data_len, unsol_data, sg_nents, rc; 1308 bool dump_payload = false; 1309 1310 rc = iscsit_setup_scsi_cmd(conn, cmd, buf); 1311 if (rc < 0) 1312 return rc; 1313 1314 imm_data = cmd->immediate_data; 1315 imm_data_len = cmd->first_burst_len; 1316 unsol_data = cmd->unsolicited_data; 1317 1318 rc = iscsit_process_scsi_cmd(conn, cmd, hdr); 1319 if (rc < 0) { 1320 return 0; 1321 } else if (rc > 0) { 1322 dump_payload = true; 1323 goto sequence_cmd; 1324 } 1325 1326 if (!imm_data) 1327 return 0; 1328 1329 sg = &cmd->se_cmd.t_data_sg[0]; 1330 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE)); 1331 1332 isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n", 1333 sg, sg_nents, &rx_desc->data[0], imm_data_len); 1334 1335 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len); 1336 1337 cmd->write_data_done += imm_data_len; 1338 1339 if (cmd->write_data_done == cmd->se_cmd.data_length) { 1340 spin_lock_bh(&cmd->istate_lock); 1341 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; 1342 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; 1343 spin_unlock_bh(&cmd->istate_lock); 1344 } 1345 1346sequence_cmd: 1347 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn); 1348 1349 if (!rc && dump_payload == false && unsol_data) 1350 iscsit_set_unsoliticed_dataout(cmd); 1351 else if (dump_payload && imm_data) 1352 target_put_sess_cmd(&cmd->se_cmd); 1353 1354 return 0; 1355} 1356 1357static int 1358isert_handle_iscsi_dataout(struct isert_conn *isert_conn, 1359 struct iser_rx_desc *rx_desc, unsigned char *buf) 1360{ 1361 struct scatterlist *sg_start; 1362 struct iscsi_conn *conn = isert_conn->conn; 1363 struct iscsi_cmd *cmd = NULL; 1364 struct iscsi_data *hdr = (struct iscsi_data *)buf; 1365 u32 unsol_data_len = ntoh24(hdr->dlength); 1366 int rc, sg_nents, sg_off, page_off; 1367 1368 rc = iscsit_check_dataout_hdr(conn, buf, &cmd); 1369 if (rc < 0) 1370 return rc; 1371 else if (!cmd) 1372 return 0; 1373 /* 1374 * FIXME: Unexpected unsolicited_data out 1375 */ 1376 if (!cmd->unsolicited_data) { 1377 isert_err("Received unexpected solicited data payload\n"); 1378 dump_stack(); 1379 return -1; 1380 } 1381 1382 isert_dbg("Unsolicited DataOut unsol_data_len: %u, " 1383 "write_data_done: %u, data_length: %u\n", 1384 unsol_data_len, cmd->write_data_done, 1385 cmd->se_cmd.data_length); 1386 1387 sg_off = cmd->write_data_done / PAGE_SIZE; 1388 sg_start = &cmd->se_cmd.t_data_sg[sg_off]; 1389 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE)); 1390 page_off = cmd->write_data_done % PAGE_SIZE; 1391 /* 1392 * FIXME: Non page-aligned unsolicited_data out 1393 */ 1394 if (page_off) { 1395 isert_err("unexpected non-page aligned data payload\n"); 1396 dump_stack(); 1397 return -1; 1398 } 1399 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u " 1400 "sg_nents: %u from %p %u\n", sg_start, sg_off, 1401 sg_nents, &rx_desc->data[0], unsol_data_len); 1402 1403 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0], 1404 unsol_data_len); 1405 1406 rc = iscsit_check_dataout_payload(cmd, hdr, false); 1407 if (rc < 0) 1408 return rc; 1409 1410 return 0; 1411} 1412 1413static int 1414isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd, 1415 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc, 1416 unsigned char *buf) 1417{ 1418 struct iscsi_conn *conn = isert_conn->conn; 1419 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf; 1420 int rc; 1421 1422 rc = iscsit_setup_nop_out(conn, cmd, hdr); 1423 if (rc < 0) 1424 return rc; 1425 /* 1426 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload 1427 */ 1428 1429 return iscsit_process_nop_out(conn, cmd, hdr); 1430} 1431 1432static int 1433isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd, 1434 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc, 1435 struct iscsi_text *hdr) 1436{ 1437 struct iscsi_conn *conn = isert_conn->conn; 1438 u32 payload_length = ntoh24(hdr->dlength); 1439 int rc; 1440 unsigned char *text_in = NULL; 1441 1442 rc = iscsit_setup_text_cmd(conn, cmd, hdr); 1443 if (rc < 0) 1444 return rc; 1445 1446 if (payload_length) { 1447 text_in = kzalloc(payload_length, GFP_KERNEL); 1448 if (!text_in) { 1449 isert_err("Unable to allocate text_in of payload_length: %u\n", 1450 payload_length); 1451 return -ENOMEM; 1452 } 1453 } 1454 cmd->text_in_ptr = text_in; 1455 1456 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length); 1457 1458 return iscsit_process_text_cmd(conn, cmd, hdr); 1459} 1460 1461static int 1462isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc, 1463 uint32_t read_stag, uint64_t read_va, 1464 uint32_t write_stag, uint64_t write_va) 1465{ 1466 struct iscsi_hdr *hdr = &rx_desc->iscsi_header; 1467 struct iscsi_conn *conn = isert_conn->conn; 1468 struct iscsi_cmd *cmd; 1469 struct isert_cmd *isert_cmd; 1470 int ret = -EINVAL; 1471 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK); 1472 1473 if (conn->sess->sess_ops->SessionType && 1474 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) { 1475 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery," 1476 " ignoring\n", opcode); 1477 return 0; 1478 } 1479 1480 switch (opcode) { 1481 case ISCSI_OP_SCSI_CMD: 1482 cmd = isert_allocate_cmd(conn); 1483 if (!cmd) 1484 break; 1485 1486 isert_cmd = iscsit_priv_cmd(cmd); 1487 isert_cmd->read_stag = read_stag; 1488 isert_cmd->read_va = read_va; 1489 isert_cmd->write_stag = write_stag; 1490 isert_cmd->write_va = write_va; 1491 1492 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd, 1493 rx_desc, (unsigned char *)hdr); 1494 break; 1495 case ISCSI_OP_NOOP_OUT: 1496 cmd = isert_allocate_cmd(conn); 1497 if (!cmd) 1498 break; 1499 1500 isert_cmd = iscsit_priv_cmd(cmd); 1501 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd, 1502 rx_desc, (unsigned char *)hdr); 1503 break; 1504 case ISCSI_OP_SCSI_DATA_OUT: 1505 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc, 1506 (unsigned char *)hdr); 1507 break; 1508 case ISCSI_OP_SCSI_TMFUNC: 1509 cmd = isert_allocate_cmd(conn); 1510 if (!cmd) 1511 break; 1512 1513 ret = iscsit_handle_task_mgt_cmd(conn, cmd, 1514 (unsigned char *)hdr); 1515 break; 1516 case ISCSI_OP_LOGOUT: 1517 cmd = isert_allocate_cmd(conn); 1518 if (!cmd) 1519 break; 1520 1521 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr); 1522 break; 1523 case ISCSI_OP_TEXT: 1524 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) { 1525 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt); 1526 if (!cmd) 1527 break; 1528 } else { 1529 cmd = isert_allocate_cmd(conn); 1530 if (!cmd) 1531 break; 1532 } 1533 1534 isert_cmd = iscsit_priv_cmd(cmd); 1535 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd, 1536 rx_desc, (struct iscsi_text *)hdr); 1537 break; 1538 default: 1539 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode); 1540 dump_stack(); 1541 break; 1542 } 1543 1544 return ret; 1545} 1546 1547static void 1548isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn) 1549{ 1550 struct iser_hdr *iser_hdr = &rx_desc->iser_header; 1551 uint64_t read_va = 0, write_va = 0; 1552 uint32_t read_stag = 0, write_stag = 0; 1553 int rc; 1554 1555 switch (iser_hdr->flags & 0xF0) { 1556 case ISCSI_CTRL: 1557 if (iser_hdr->flags & ISER_RSV) { 1558 read_stag = be32_to_cpu(iser_hdr->read_stag); 1559 read_va = be64_to_cpu(iser_hdr->read_va); 1560 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n", 1561 read_stag, (unsigned long long)read_va); 1562 } 1563 if (iser_hdr->flags & ISER_WSV) { 1564 write_stag = be32_to_cpu(iser_hdr->write_stag); 1565 write_va = be64_to_cpu(iser_hdr->write_va); 1566 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n", 1567 write_stag, (unsigned long long)write_va); 1568 } 1569 1570 isert_dbg("ISER ISCSI_CTRL PDU\n"); 1571 break; 1572 case ISER_HELLO: 1573 isert_err("iSER Hello message\n"); 1574 break; 1575 default: 1576 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags); 1577 break; 1578 } 1579 1580 rc = isert_rx_opcode(isert_conn, rx_desc, 1581 read_stag, read_va, write_stag, write_va); 1582} 1583 1584static void 1585isert_rcv_completion(struct iser_rx_desc *desc, 1586 struct isert_conn *isert_conn, 1587 u32 xfer_len) 1588{ 1589 struct ib_device *ib_dev = isert_conn->cm_id->device; 1590 struct iscsi_hdr *hdr; 1591 u64 rx_dma; 1592 int rx_buflen, outstanding; 1593 1594 if ((char *)desc == isert_conn->login_req_buf) { 1595 rx_dma = isert_conn->login_req_dma; 1596 rx_buflen = ISER_RX_LOGIN_SIZE; 1597 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n", 1598 rx_dma, rx_buflen); 1599 } else { 1600 rx_dma = desc->dma_addr; 1601 rx_buflen = ISER_RX_PAYLOAD_SIZE; 1602 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n", 1603 rx_dma, rx_buflen); 1604 } 1605 1606 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE); 1607 1608 hdr = &desc->iscsi_header; 1609 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n", 1610 hdr->opcode, hdr->itt, hdr->flags, 1611 (int)(xfer_len - ISER_HEADERS_LEN)); 1612 1613 if ((char *)desc == isert_conn->login_req_buf) { 1614 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN; 1615 if (isert_conn->conn) { 1616 struct iscsi_login *login = isert_conn->conn->conn_login; 1617 1618 if (login && !login->first_request) 1619 isert_rx_login_req(isert_conn); 1620 } 1621 mutex_lock(&isert_conn->mutex); 1622 complete(&isert_conn->login_req_comp); 1623 mutex_unlock(&isert_conn->mutex); 1624 } else { 1625 isert_rx_do_work(desc, isert_conn); 1626 } 1627 1628 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen, 1629 DMA_FROM_DEVICE); 1630 1631 isert_conn->post_recv_buf_count--; 1632 isert_dbg("Decremented post_recv_buf_count: %d\n", 1633 isert_conn->post_recv_buf_count); 1634 1635 if ((char *)desc == isert_conn->login_req_buf) 1636 return; 1637 1638 outstanding = isert_conn->post_recv_buf_count; 1639 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) { 1640 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding, 1641 ISERT_MIN_POSTED_RX); 1642 err = isert_post_recv(isert_conn, count); 1643 if (err) { 1644 isert_err("isert_post_recv() count: %d failed, %d\n", 1645 count, err); 1646 } 1647 } 1648} 1649 1650static int 1651isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd, 1652 struct scatterlist *sg, u32 nents, u32 length, u32 offset, 1653 enum iser_ib_op_code op, struct isert_data_buf *data) 1654{ 1655 struct ib_device *ib_dev = isert_conn->cm_id->device; 1656 1657 data->dma_dir = op == ISER_IB_RDMA_WRITE ? 1658 DMA_TO_DEVICE : DMA_FROM_DEVICE; 1659 1660 data->len = length - offset; 1661 data->offset = offset; 1662 data->sg_off = data->offset / PAGE_SIZE; 1663 1664 data->sg = &sg[data->sg_off]; 1665 data->nents = min_t(unsigned int, nents - data->sg_off, 1666 ISCSI_ISER_SG_TABLESIZE); 1667 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE * 1668 PAGE_SIZE); 1669 1670 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents, 1671 data->dma_dir); 1672 if (unlikely(!data->dma_nents)) { 1673 isert_err("Cmd: unable to dma map SGs %p\n", sg); 1674 return -EINVAL; 1675 } 1676 1677 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n", 1678 isert_cmd, data->dma_nents, data->sg, data->nents, data->len); 1679 1680 return 0; 1681} 1682 1683static void 1684isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data) 1685{ 1686 struct ib_device *ib_dev = isert_conn->cm_id->device; 1687 1688 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir); 1689 memset(data, 0, sizeof(*data)); 1690} 1691 1692 1693 1694static void 1695isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn) 1696{ 1697 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr; 1698 1699 isert_dbg("Cmd %p\n", isert_cmd); 1700 1701 if (wr->data.sg) { 1702 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd); 1703 isert_unmap_data_buf(isert_conn, &wr->data); 1704 } 1705 1706 if (wr->send_wr) { 1707 isert_dbg("Cmd %p free send_wr\n", isert_cmd); 1708 kfree(wr->send_wr); 1709 wr->send_wr = NULL; 1710 } 1711 1712 if (wr->ib_sge) { 1713 isert_dbg("Cmd %p free ib_sge\n", isert_cmd); 1714 kfree(wr->ib_sge); 1715 wr->ib_sge = NULL; 1716 } 1717} 1718 1719static void 1720isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn) 1721{ 1722 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr; 1723 1724 isert_dbg("Cmd %p\n", isert_cmd); 1725 1726 if (wr->fr_desc) { 1727 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc); 1728 if (wr->fr_desc->ind & ISERT_PROTECTED) { 1729 isert_unmap_data_buf(isert_conn, &wr->prot); 1730 wr->fr_desc->ind &= ~ISERT_PROTECTED; 1731 } 1732 spin_lock_bh(&isert_conn->pool_lock); 1733 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool); 1734 spin_unlock_bh(&isert_conn->pool_lock); 1735 wr->fr_desc = NULL; 1736 } 1737 1738 if (wr->data.sg) { 1739 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd); 1740 isert_unmap_data_buf(isert_conn, &wr->data); 1741 } 1742 1743 wr->ib_sge = NULL; 1744 wr->send_wr = NULL; 1745} 1746 1747static void 1748isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err) 1749{ 1750 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd; 1751 struct isert_conn *isert_conn = isert_cmd->conn; 1752 struct iscsi_conn *conn = isert_conn->conn; 1753 struct isert_device *device = isert_conn->device; 1754 struct iscsi_text_rsp *hdr; 1755 1756 isert_dbg("Cmd %p\n", isert_cmd); 1757 1758 switch (cmd->iscsi_opcode) { 1759 case ISCSI_OP_SCSI_CMD: 1760 spin_lock_bh(&conn->cmd_lock); 1761 if (!list_empty(&cmd->i_conn_node)) 1762 list_del_init(&cmd->i_conn_node); 1763 spin_unlock_bh(&conn->cmd_lock); 1764 1765 if (cmd->data_direction == DMA_TO_DEVICE) { 1766 iscsit_stop_dataout_timer(cmd); 1767 /* 1768 * Check for special case during comp_err where 1769 * WRITE_PENDING has been handed off from core, 1770 * but requires an extra target_put_sess_cmd() 1771 * before transport_generic_free_cmd() below. 1772 */ 1773 if (comp_err && 1774 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) { 1775 struct se_cmd *se_cmd = &cmd->se_cmd; 1776 1777 target_put_sess_cmd(se_cmd); 1778 } 1779 } 1780 1781 device->unreg_rdma_mem(isert_cmd, isert_conn); 1782 transport_generic_free_cmd(&cmd->se_cmd, 0); 1783 break; 1784 case ISCSI_OP_SCSI_TMFUNC: 1785 spin_lock_bh(&conn->cmd_lock); 1786 if (!list_empty(&cmd->i_conn_node)) 1787 list_del_init(&cmd->i_conn_node); 1788 spin_unlock_bh(&conn->cmd_lock); 1789 1790 transport_generic_free_cmd(&cmd->se_cmd, 0); 1791 break; 1792 case ISCSI_OP_REJECT: 1793 case ISCSI_OP_NOOP_OUT: 1794 case ISCSI_OP_TEXT: 1795 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header; 1796 /* If the continue bit is on, keep the command alive */ 1797 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE) 1798 break; 1799 1800 spin_lock_bh(&conn->cmd_lock); 1801 if (!list_empty(&cmd->i_conn_node)) 1802 list_del_init(&cmd->i_conn_node); 1803 spin_unlock_bh(&conn->cmd_lock); 1804 1805 /* 1806 * Handle special case for REJECT when iscsi_add_reject*() has 1807 * overwritten the original iscsi_opcode assignment, and the 1808 * associated cmd->se_cmd needs to be released. 1809 */ 1810 if (cmd->se_cmd.se_tfo != NULL) { 1811 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n", 1812 cmd->iscsi_opcode); 1813 transport_generic_free_cmd(&cmd->se_cmd, 0); 1814 break; 1815 } 1816 /* 1817 * Fall-through 1818 */ 1819 default: 1820 iscsit_release_cmd(cmd); 1821 break; 1822 } 1823} 1824 1825static void 1826isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev) 1827{ 1828 if (tx_desc->dma_addr != 0) { 1829 isert_dbg("unmap single for tx_desc->dma_addr\n"); 1830 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr, 1831 ISER_HEADERS_LEN, DMA_TO_DEVICE); 1832 tx_desc->dma_addr = 0; 1833 } 1834} 1835 1836static void 1837isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd, 1838 struct ib_device *ib_dev, bool comp_err) 1839{ 1840 if (isert_cmd->pdu_buf_dma != 0) { 1841 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n"); 1842 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma, 1843 isert_cmd->pdu_buf_len, DMA_TO_DEVICE); 1844 isert_cmd->pdu_buf_dma = 0; 1845 } 1846 1847 isert_unmap_tx_desc(tx_desc, ib_dev); 1848 isert_put_cmd(isert_cmd, comp_err); 1849} 1850 1851static int 1852isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr) 1853{ 1854 struct ib_mr_status mr_status; 1855 int ret; 1856 1857 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status); 1858 if (ret) { 1859 isert_err("ib_check_mr_status failed, ret %d\n", ret); 1860 goto fail_mr_status; 1861 } 1862 1863 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { 1864 u64 sec_offset_err; 1865 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8; 1866 1867 switch (mr_status.sig_err.err_type) { 1868 case IB_SIG_BAD_GUARD: 1869 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; 1870 break; 1871 case IB_SIG_BAD_REFTAG: 1872 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 1873 break; 1874 case IB_SIG_BAD_APPTAG: 1875 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED; 1876 break; 1877 } 1878 sec_offset_err = mr_status.sig_err.sig_err_offset; 1879 do_div(sec_offset_err, block_size); 1880 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba; 1881 1882 isert_err("PI error found type %d at sector 0x%llx " 1883 "expected 0x%x vs actual 0x%x\n", 1884 mr_status.sig_err.err_type, 1885 (unsigned long long)se_cmd->bad_sector, 1886 mr_status.sig_err.expected, 1887 mr_status.sig_err.actual); 1888 ret = 1; 1889 } 1890 1891fail_mr_status: 1892 return ret; 1893} 1894 1895static void 1896isert_completion_rdma_write(struct iser_tx_desc *tx_desc, 1897 struct isert_cmd *isert_cmd) 1898{ 1899 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr; 1900 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd; 1901 struct se_cmd *se_cmd = &cmd->se_cmd; 1902 struct isert_conn *isert_conn = isert_cmd->conn; 1903 struct isert_device *device = isert_conn->device; 1904 int ret = 0; 1905 1906 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) { 1907 ret = isert_check_pi_status(se_cmd, 1908 wr->fr_desc->pi_ctx->sig_mr); 1909 wr->fr_desc->ind &= ~ISERT_PROTECTED; 1910 } 1911 1912 device->unreg_rdma_mem(isert_cmd, isert_conn); 1913 wr->send_wr_num = 0; 1914 if (ret) 1915 transport_send_check_condition_and_sense(se_cmd, 1916 se_cmd->pi_err, 0); 1917 else 1918 isert_put_response(isert_conn->conn, cmd); 1919} 1920 1921static void 1922isert_completion_rdma_read(struct iser_tx_desc *tx_desc, 1923 struct isert_cmd *isert_cmd) 1924{ 1925 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr; 1926 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd; 1927 struct se_cmd *se_cmd = &cmd->se_cmd; 1928 struct isert_conn *isert_conn = isert_cmd->conn; 1929 struct isert_device *device = isert_conn->device; 1930 int ret = 0; 1931 1932 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) { 1933 ret = isert_check_pi_status(se_cmd, 1934 wr->fr_desc->pi_ctx->sig_mr); 1935 wr->fr_desc->ind &= ~ISERT_PROTECTED; 1936 } 1937 1938 iscsit_stop_dataout_timer(cmd); 1939 device->unreg_rdma_mem(isert_cmd, isert_conn); 1940 cmd->write_data_done = wr->data.len; 1941 wr->send_wr_num = 0; 1942 1943 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd); 1944 spin_lock_bh(&cmd->istate_lock); 1945 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; 1946 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; 1947 spin_unlock_bh(&cmd->istate_lock); 1948 1949 if (ret) { 1950 target_put_sess_cmd(se_cmd); 1951 transport_send_check_condition_and_sense(se_cmd, 1952 se_cmd->pi_err, 0); 1953 } else { 1954 target_execute_cmd(se_cmd); 1955 } 1956} 1957 1958static void 1959isert_do_control_comp(struct work_struct *work) 1960{ 1961 struct isert_cmd *isert_cmd = container_of(work, 1962 struct isert_cmd, comp_work); 1963 struct isert_conn *isert_conn = isert_cmd->conn; 1964 struct ib_device *ib_dev = isert_conn->cm_id->device; 1965 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd; 1966 1967 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state); 1968 1969 switch (cmd->i_state) { 1970 case ISTATE_SEND_TASKMGTRSP: 1971 iscsit_tmr_post_handler(cmd, cmd->conn); 1972 case ISTATE_SEND_REJECT: /* FALLTHRU */ 1973 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */ 1974 cmd->i_state = ISTATE_SENT_STATUS; 1975 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, 1976 ib_dev, false); 1977 break; 1978 case ISTATE_SEND_LOGOUTRSP: 1979 iscsit_logout_post_handler(cmd, cmd->conn); 1980 break; 1981 default: 1982 isert_err("Unknown i_state %d\n", cmd->i_state); 1983 dump_stack(); 1984 break; 1985 } 1986} 1987 1988static void 1989isert_response_completion(struct iser_tx_desc *tx_desc, 1990 struct isert_cmd *isert_cmd, 1991 struct isert_conn *isert_conn, 1992 struct ib_device *ib_dev) 1993{ 1994 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd; 1995 1996 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP || 1997 cmd->i_state == ISTATE_SEND_LOGOUTRSP || 1998 cmd->i_state == ISTATE_SEND_REJECT || 1999 cmd->i_state == ISTATE_SEND_TEXTRSP) { 2000 isert_unmap_tx_desc(tx_desc, ib_dev); 2001 2002 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp); 2003 queue_work(isert_comp_wq, &isert_cmd->comp_work); 2004 return; 2005 } 2006 2007 cmd->i_state = ISTATE_SENT_STATUS; 2008 isert_completion_put(tx_desc, isert_cmd, ib_dev, false); 2009} 2010 2011static void 2012isert_snd_completion(struct iser_tx_desc *tx_desc, 2013 struct isert_conn *isert_conn) 2014{ 2015 struct ib_device *ib_dev = isert_conn->cm_id->device; 2016 struct isert_cmd *isert_cmd = tx_desc->isert_cmd; 2017 struct isert_rdma_wr *wr; 2018 2019 if (!isert_cmd) { 2020 isert_unmap_tx_desc(tx_desc, ib_dev); 2021 return; 2022 } 2023 wr = &isert_cmd->rdma_wr; 2024 2025 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op); 2026 2027 switch (wr->iser_ib_op) { 2028 case ISER_IB_SEND: 2029 isert_response_completion(tx_desc, isert_cmd, 2030 isert_conn, ib_dev); 2031 break; 2032 case ISER_IB_RDMA_WRITE: 2033 isert_completion_rdma_write(tx_desc, isert_cmd); 2034 break; 2035 case ISER_IB_RDMA_READ: 2036 isert_completion_rdma_read(tx_desc, isert_cmd); 2037 break; 2038 default: 2039 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op); 2040 dump_stack(); 2041 break; 2042 } 2043} 2044 2045/** 2046 * is_isert_tx_desc() - Indicate if the completion wr_id 2047 * is a TX descriptor or not. 2048 * @isert_conn: iser connection 2049 * @wr_id: completion WR identifier 2050 * 2051 * Since we cannot rely on wc opcode in FLUSH errors 2052 * we must work around it by checking if the wr_id address 2053 * falls in the iser connection rx_descs buffer. If so 2054 * it is an RX descriptor, otherwize it is a TX. 2055 */ 2056static inline bool 2057is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id) 2058{ 2059 void *start = isert_conn->rx_descs; 2060 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs); 2061 2062 if ((wr_id >= start && wr_id < start + len) || 2063 (wr_id == isert_conn->login_req_buf)) 2064 return false; 2065 2066 return true; 2067} 2068 2069static void 2070isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc) 2071{ 2072 if (wc->wr_id == ISER_BEACON_WRID) { 2073 isert_info("conn %p completing wait_comp_err\n", 2074 isert_conn); 2075 complete(&isert_conn->wait_comp_err); 2076 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) { 2077 struct ib_device *ib_dev = isert_conn->cm_id->device; 2078 struct isert_cmd *isert_cmd; 2079 struct iser_tx_desc *desc; 2080 2081 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id; 2082 isert_cmd = desc->isert_cmd; 2083 if (!isert_cmd) 2084 isert_unmap_tx_desc(desc, ib_dev); 2085 else 2086 isert_completion_put(desc, isert_cmd, ib_dev, true); 2087 } else { 2088 isert_conn->post_recv_buf_count--; 2089 if (!isert_conn->post_recv_buf_count && 2090 isert_conn->state >= ISER_CONN_BOUND) 2091 iscsit_cause_connection_reinstatement(isert_conn->conn, 0); 2092 } 2093} 2094 2095static void 2096isert_handle_wc(struct ib_wc *wc) 2097{ 2098 struct isert_conn *isert_conn; 2099 struct iser_tx_desc *tx_desc; 2100 struct iser_rx_desc *rx_desc; 2101 2102 isert_conn = wc->qp->qp_context; 2103 if (likely(wc->status == IB_WC_SUCCESS)) { 2104 if (wc->opcode == IB_WC_RECV) { 2105 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id; 2106 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len); 2107 } else { 2108 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id; 2109 isert_snd_completion(tx_desc, isert_conn); 2110 } 2111 } else { 2112 if (wc->status != IB_WC_WR_FLUSH_ERR) 2113 isert_err("wr id %llx status %d vend_err %x\n", 2114 wc->wr_id, wc->status, wc->vendor_err); 2115 else 2116 isert_dbg("flush error: wr id %llx\n", wc->wr_id); 2117 2118 if (wc->wr_id != ISER_FASTREG_LI_WRID) 2119 isert_cq_comp_err(isert_conn, wc); 2120 } 2121} 2122 2123static void 2124isert_cq_work(struct work_struct *work) 2125{ 2126 enum { isert_poll_budget = 65536 }; 2127 struct isert_comp *comp = container_of(work, struct isert_comp, 2128 work); 2129 struct ib_wc *const wcs = comp->wcs; 2130 int i, n, completed = 0; 2131 2132 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) { 2133 for (i = 0; i < n; i++) 2134 isert_handle_wc(&wcs[i]); 2135 2136 completed += n; 2137 if (completed >= isert_poll_budget) 2138 break; 2139 } 2140 2141 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP); 2142} 2143 2144static void 2145isert_cq_callback(struct ib_cq *cq, void *context) 2146{ 2147 struct isert_comp *comp = context; 2148 2149 queue_work(isert_comp_wq, &comp->work); 2150} 2151 2152static int 2153isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd) 2154{ 2155 struct ib_send_wr *wr_failed; 2156 int ret; 2157 2158 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr, 2159 &wr_failed); 2160 if (ret) { 2161 isert_err("ib_post_send failed with %d\n", ret); 2162 return ret; 2163 } 2164 return ret; 2165} 2166 2167static int 2168isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd) 2169{ 2170 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2171 struct isert_conn *isert_conn = conn->context; 2172 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 2173 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *) 2174 &isert_cmd->tx_desc.iscsi_header; 2175 2176 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 2177 iscsit_build_rsp_pdu(cmd, conn, true, hdr); 2178 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 2179 /* 2180 * Attach SENSE DATA payload to iSCSI Response PDU 2181 */ 2182 if (cmd->se_cmd.sense_buffer && 2183 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 2184 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { 2185 struct isert_device *device = isert_conn->device; 2186 struct ib_device *ib_dev = device->ib_device; 2187 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1]; 2188 u32 padding, pdu_len; 2189 2190 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, 2191 cmd->sense_buffer); 2192 cmd->se_cmd.scsi_sense_length += sizeof(__be16); 2193 2194 padding = -(cmd->se_cmd.scsi_sense_length) & 3; 2195 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length); 2196 pdu_len = cmd->se_cmd.scsi_sense_length + padding; 2197 2198 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev, 2199 (void *)cmd->sense_buffer, pdu_len, 2200 DMA_TO_DEVICE); 2201 2202 isert_cmd->pdu_buf_len = pdu_len; 2203 tx_dsg->addr = isert_cmd->pdu_buf_dma; 2204 tx_dsg->length = pdu_len; 2205 tx_dsg->lkey = device->mr->lkey; 2206 isert_cmd->tx_desc.num_sge = 2; 2207 } 2208 2209 isert_init_send_wr(isert_conn, isert_cmd, send_wr); 2210 2211 isert_dbg("Posting SCSI Response\n"); 2212 2213 return isert_post_response(isert_conn, isert_cmd); 2214} 2215 2216static void 2217isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd) 2218{ 2219 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2220 struct isert_conn *isert_conn = conn->context; 2221 struct isert_device *device = isert_conn->device; 2222 2223 spin_lock_bh(&conn->cmd_lock); 2224 if (!list_empty(&cmd->i_conn_node)) 2225 list_del_init(&cmd->i_conn_node); 2226 spin_unlock_bh(&conn->cmd_lock); 2227 2228 if (cmd->data_direction == DMA_TO_DEVICE) 2229 iscsit_stop_dataout_timer(cmd); 2230 2231 device->unreg_rdma_mem(isert_cmd, isert_conn); 2232} 2233 2234static enum target_prot_op 2235isert_get_sup_prot_ops(struct iscsi_conn *conn) 2236{ 2237 struct isert_conn *isert_conn = conn->context; 2238 struct isert_device *device = isert_conn->device; 2239 2240 if (conn->tpg->tpg_attrib.t10_pi) { 2241 if (device->pi_capable) { 2242 isert_info("conn %p PI offload enabled\n", isert_conn); 2243 isert_conn->pi_support = true; 2244 return TARGET_PROT_ALL; 2245 } 2246 } 2247 2248 isert_info("conn %p PI offload disabled\n", isert_conn); 2249 isert_conn->pi_support = false; 2250 2251 return TARGET_PROT_NORMAL; 2252} 2253 2254static int 2255isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 2256 bool nopout_response) 2257{ 2258 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2259 struct isert_conn *isert_conn = conn->context; 2260 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 2261 2262 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 2263 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *) 2264 &isert_cmd->tx_desc.iscsi_header, 2265 nopout_response); 2266 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 2267 isert_init_send_wr(isert_conn, isert_cmd, send_wr); 2268 2269 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn); 2270 2271 return isert_post_response(isert_conn, isert_cmd); 2272} 2273 2274static int 2275isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2276{ 2277 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2278 struct isert_conn *isert_conn = conn->context; 2279 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 2280 2281 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 2282 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *) 2283 &isert_cmd->tx_desc.iscsi_header); 2284 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 2285 isert_init_send_wr(isert_conn, isert_cmd, send_wr); 2286 2287 isert_dbg("conn %p Posting Logout Response\n", isert_conn); 2288 2289 return isert_post_response(isert_conn, isert_cmd); 2290} 2291 2292static int 2293isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2294{ 2295 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2296 struct isert_conn *isert_conn = conn->context; 2297 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 2298 2299 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 2300 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *) 2301 &isert_cmd->tx_desc.iscsi_header); 2302 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 2303 isert_init_send_wr(isert_conn, isert_cmd, send_wr); 2304 2305 isert_dbg("conn %p Posting Task Management Response\n", isert_conn); 2306 2307 return isert_post_response(isert_conn, isert_cmd); 2308} 2309 2310static int 2311isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2312{ 2313 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2314 struct isert_conn *isert_conn = conn->context; 2315 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 2316 struct isert_device *device = isert_conn->device; 2317 struct ib_device *ib_dev = device->ib_device; 2318 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1]; 2319 struct iscsi_reject *hdr = 2320 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header; 2321 2322 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 2323 iscsit_build_reject(cmd, conn, hdr); 2324 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 2325 2326 hton24(hdr->dlength, ISCSI_HDR_LEN); 2327 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev, 2328 (void *)cmd->buf_ptr, ISCSI_HDR_LEN, 2329 DMA_TO_DEVICE); 2330 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN; 2331 tx_dsg->addr = isert_cmd->pdu_buf_dma; 2332 tx_dsg->length = ISCSI_HDR_LEN; 2333 tx_dsg->lkey = device->mr->lkey; 2334 isert_cmd->tx_desc.num_sge = 2; 2335 2336 isert_init_send_wr(isert_conn, isert_cmd, send_wr); 2337 2338 isert_dbg("conn %p Posting Reject\n", isert_conn); 2339 2340 return isert_post_response(isert_conn, isert_cmd); 2341} 2342 2343static int 2344isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2345{ 2346 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2347 struct isert_conn *isert_conn = conn->context; 2348 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 2349 struct iscsi_text_rsp *hdr = 2350 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header; 2351 u32 txt_rsp_len; 2352 int rc; 2353 2354 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 2355 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND); 2356 if (rc < 0) 2357 return rc; 2358 2359 txt_rsp_len = rc; 2360 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 2361 2362 if (txt_rsp_len) { 2363 struct isert_device *device = isert_conn->device; 2364 struct ib_device *ib_dev = device->ib_device; 2365 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1]; 2366 void *txt_rsp_buf = cmd->buf_ptr; 2367 2368 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev, 2369 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE); 2370 2371 isert_cmd->pdu_buf_len = txt_rsp_len; 2372 tx_dsg->addr = isert_cmd->pdu_buf_dma; 2373 tx_dsg->length = txt_rsp_len; 2374 tx_dsg->lkey = device->mr->lkey; 2375 isert_cmd->tx_desc.num_sge = 2; 2376 } 2377 isert_init_send_wr(isert_conn, isert_cmd, send_wr); 2378 2379 isert_dbg("conn %p Text Response\n", isert_conn); 2380 2381 return isert_post_response(isert_conn, isert_cmd); 2382} 2383 2384static int 2385isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd, 2386 struct ib_sge *ib_sge, struct ib_send_wr *send_wr, 2387 u32 data_left, u32 offset) 2388{ 2389 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd; 2390 struct scatterlist *sg_start, *tmp_sg; 2391 struct isert_device *device = isert_conn->device; 2392 struct ib_device *ib_dev = device->ib_device; 2393 u32 sg_off, page_off; 2394 int i = 0, sg_nents; 2395 2396 sg_off = offset / PAGE_SIZE; 2397 sg_start = &cmd->se_cmd.t_data_sg[sg_off]; 2398 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge); 2399 page_off = offset % PAGE_SIZE; 2400 2401 send_wr->sg_list = ib_sge; 2402 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc; 2403 /* 2404 * Perform mapping of TCM scatterlist memory ib_sge dma_addr. 2405 */ 2406 for_each_sg(sg_start, tmp_sg, sg_nents, i) { 2407 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, " 2408 "page_off: %u\n", 2409 (unsigned long long)tmp_sg->dma_address, 2410 tmp_sg->length, page_off); 2411 2412 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off; 2413 ib_sge->length = min_t(u32, data_left, 2414 ib_sg_dma_len(ib_dev, tmp_sg) - page_off); 2415 ib_sge->lkey = device->mr->lkey; 2416 2417 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n", 2418 ib_sge->addr, ib_sge->length, ib_sge->lkey); 2419 page_off = 0; 2420 data_left -= ib_sge->length; 2421 if (!data_left) 2422 break; 2423 ib_sge++; 2424 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge); 2425 } 2426 2427 send_wr->num_sge = ++i; 2428 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n", 2429 send_wr->sg_list, send_wr->num_sge); 2430 2431 return send_wr->num_sge; 2432} 2433 2434static int 2435isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 2436 struct isert_rdma_wr *wr) 2437{ 2438 struct se_cmd *se_cmd = &cmd->se_cmd; 2439 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2440 struct isert_conn *isert_conn = conn->context; 2441 struct isert_data_buf *data = &wr->data; 2442 struct ib_send_wr *send_wr; 2443 struct ib_sge *ib_sge; 2444 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0; 2445 int ret = 0, i, ib_sge_cnt; 2446 2447 isert_cmd->tx_desc.isert_cmd = isert_cmd; 2448 2449 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0; 2450 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg, 2451 se_cmd->t_data_nents, se_cmd->data_length, 2452 offset, wr->iser_ib_op, &wr->data); 2453 if (ret) 2454 return ret; 2455 2456 data_left = data->len; 2457 offset = data->offset; 2458 2459 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL); 2460 if (!ib_sge) { 2461 isert_warn("Unable to allocate ib_sge\n"); 2462 ret = -ENOMEM; 2463 goto unmap_cmd; 2464 } 2465 wr->ib_sge = ib_sge; 2466 2467 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge); 2468 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num, 2469 GFP_KERNEL); 2470 if (!wr->send_wr) { 2471 isert_dbg("Unable to allocate wr->send_wr\n"); 2472 ret = -ENOMEM; 2473 goto unmap_cmd; 2474 } 2475 2476 wr->isert_cmd = isert_cmd; 2477 rdma_write_max = isert_conn->max_sge * PAGE_SIZE; 2478 2479 for (i = 0; i < wr->send_wr_num; i++) { 2480 send_wr = &isert_cmd->rdma_wr.send_wr[i]; 2481 data_len = min(data_left, rdma_write_max); 2482 2483 send_wr->send_flags = 0; 2484 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) { 2485 send_wr->opcode = IB_WR_RDMA_WRITE; 2486 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset; 2487 send_wr->wr.rdma.rkey = isert_cmd->read_stag; 2488 if (i + 1 == wr->send_wr_num) 2489 send_wr->next = &isert_cmd->tx_desc.send_wr; 2490 else 2491 send_wr->next = &wr->send_wr[i + 1]; 2492 } else { 2493 send_wr->opcode = IB_WR_RDMA_READ; 2494 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset; 2495 send_wr->wr.rdma.rkey = isert_cmd->write_stag; 2496 if (i + 1 == wr->send_wr_num) 2497 send_wr->send_flags = IB_SEND_SIGNALED; 2498 else 2499 send_wr->next = &wr->send_wr[i + 1]; 2500 } 2501 2502 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge, 2503 send_wr, data_len, offset); 2504 ib_sge += ib_sge_cnt; 2505 2506 offset += data_len; 2507 va_offset += data_len; 2508 data_left -= data_len; 2509 } 2510 2511 return 0; 2512unmap_cmd: 2513 isert_unmap_data_buf(isert_conn, data); 2514 2515 return ret; 2516} 2517 2518static int 2519isert_map_fr_pagelist(struct ib_device *ib_dev, 2520 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl) 2521{ 2522 u64 start_addr, end_addr, page, chunk_start = 0; 2523 struct scatterlist *tmp_sg; 2524 int i = 0, new_chunk, last_ent, n_pages; 2525 2526 n_pages = 0; 2527 new_chunk = 1; 2528 last_ent = sg_nents - 1; 2529 for_each_sg(sg_start, tmp_sg, sg_nents, i) { 2530 start_addr = ib_sg_dma_address(ib_dev, tmp_sg); 2531 if (new_chunk) 2532 chunk_start = start_addr; 2533 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg); 2534 2535 isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n", 2536 i, (unsigned long long)tmp_sg->dma_address, 2537 tmp_sg->length); 2538 2539 if ((end_addr & ~PAGE_MASK) && i < last_ent) { 2540 new_chunk = 0; 2541 continue; 2542 } 2543 new_chunk = 1; 2544 2545 page = chunk_start & PAGE_MASK; 2546 do { 2547 fr_pl[n_pages++] = page; 2548 isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n", 2549 n_pages - 1, page); 2550 page += PAGE_SIZE; 2551 } while (page < end_addr); 2552 } 2553 2554 return n_pages; 2555} 2556 2557static inline void 2558isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr) 2559{ 2560 u32 rkey; 2561 2562 memset(inv_wr, 0, sizeof(*inv_wr)); 2563 inv_wr->wr_id = ISER_FASTREG_LI_WRID; 2564 inv_wr->opcode = IB_WR_LOCAL_INV; 2565 inv_wr->ex.invalidate_rkey = mr->rkey; 2566 2567 /* Bump the key */ 2568 rkey = ib_inc_rkey(mr->rkey); 2569 ib_update_fast_reg_key(mr, rkey); 2570} 2571 2572static int 2573isert_fast_reg_mr(struct isert_conn *isert_conn, 2574 struct fast_reg_descriptor *fr_desc, 2575 struct isert_data_buf *mem, 2576 enum isert_indicator ind, 2577 struct ib_sge *sge) 2578{ 2579 struct isert_device *device = isert_conn->device; 2580 struct ib_device *ib_dev = device->ib_device; 2581 struct ib_mr *mr; 2582 struct ib_fast_reg_page_list *frpl; 2583 struct ib_send_wr fr_wr, inv_wr; 2584 struct ib_send_wr *bad_wr, *wr = NULL; 2585 int ret, pagelist_len; 2586 u32 page_off; 2587 2588 if (mem->dma_nents == 1) { 2589 sge->lkey = device->mr->lkey; 2590 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]); 2591 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]); 2592 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n", 2593 sge->addr, sge->length, sge->lkey); 2594 return 0; 2595 } 2596 2597 if (ind == ISERT_DATA_KEY_VALID) { 2598 /* Registering data buffer */ 2599 mr = fr_desc->data_mr; 2600 frpl = fr_desc->data_frpl; 2601 } else { 2602 /* Registering protection buffer */ 2603 mr = fr_desc->pi_ctx->prot_mr; 2604 frpl = fr_desc->pi_ctx->prot_frpl; 2605 } 2606 2607 page_off = mem->offset % PAGE_SIZE; 2608 2609 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n", 2610 fr_desc, mem->nents, mem->offset); 2611 2612 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents, 2613 &frpl->page_list[0]); 2614 2615 if (!(fr_desc->ind & ind)) { 2616 isert_inv_rkey(&inv_wr, mr); 2617 wr = &inv_wr; 2618 } 2619 2620 /* Prepare FASTREG WR */ 2621 memset(&fr_wr, 0, sizeof(fr_wr)); 2622 fr_wr.wr_id = ISER_FASTREG_LI_WRID; 2623 fr_wr.opcode = IB_WR_FAST_REG_MR; 2624 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off; 2625 fr_wr.wr.fast_reg.page_list = frpl; 2626 fr_wr.wr.fast_reg.page_list_len = pagelist_len; 2627 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT; 2628 fr_wr.wr.fast_reg.length = mem->len; 2629 fr_wr.wr.fast_reg.rkey = mr->rkey; 2630 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE; 2631 2632 if (!wr) 2633 wr = &fr_wr; 2634 else 2635 wr->next = &fr_wr; 2636 2637 ret = ib_post_send(isert_conn->qp, wr, &bad_wr); 2638 if (ret) { 2639 isert_err("fast registration failed, ret:%d\n", ret); 2640 return ret; 2641 } 2642 fr_desc->ind &= ~ind; 2643 2644 sge->lkey = mr->lkey; 2645 sge->addr = frpl->page_list[0] + page_off; 2646 sge->length = mem->len; 2647 2648 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n", 2649 sge->addr, sge->length, sge->lkey); 2650 2651 return ret; 2652} 2653 2654static inline void 2655isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs, 2656 struct ib_sig_domain *domain) 2657{ 2658 domain->sig_type = IB_SIG_TYPE_T10_DIF; 2659 domain->sig.dif.bg_type = IB_T10DIF_CRC; 2660 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size; 2661 domain->sig.dif.ref_tag = se_cmd->reftag_seed; 2662 /* 2663 * At the moment we hard code those, but if in the future 2664 * the target core would like to use it, we will take it 2665 * from se_cmd. 2666 */ 2667 domain->sig.dif.apptag_check_mask = 0xffff; 2668 domain->sig.dif.app_escape = true; 2669 domain->sig.dif.ref_escape = true; 2670 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT || 2671 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT) 2672 domain->sig.dif.ref_remap = true; 2673}; 2674 2675static int 2676isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs) 2677{ 2678 switch (se_cmd->prot_op) { 2679 case TARGET_PROT_DIN_INSERT: 2680 case TARGET_PROT_DOUT_STRIP: 2681 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE; 2682 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire); 2683 break; 2684 case TARGET_PROT_DOUT_INSERT: 2685 case TARGET_PROT_DIN_STRIP: 2686 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE; 2687 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem); 2688 break; 2689 case TARGET_PROT_DIN_PASS: 2690 case TARGET_PROT_DOUT_PASS: 2691 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire); 2692 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem); 2693 break; 2694 default: 2695 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op); 2696 return -EINVAL; 2697 } 2698 2699 return 0; 2700} 2701 2702static inline u8 2703isert_set_prot_checks(u8 prot_checks) 2704{ 2705 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) | 2706 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) | 2707 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0); 2708} 2709 2710static int 2711isert_reg_sig_mr(struct isert_conn *isert_conn, 2712 struct se_cmd *se_cmd, 2713 struct isert_rdma_wr *rdma_wr, 2714 struct fast_reg_descriptor *fr_desc) 2715{ 2716 struct ib_send_wr sig_wr, inv_wr; 2717 struct ib_send_wr *bad_wr, *wr = NULL; 2718 struct pi_context *pi_ctx = fr_desc->pi_ctx; 2719 struct ib_sig_attrs sig_attrs; 2720 int ret; 2721 2722 memset(&sig_attrs, 0, sizeof(sig_attrs)); 2723 ret = isert_set_sig_attrs(se_cmd, &sig_attrs); 2724 if (ret) 2725 goto err; 2726 2727 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks); 2728 2729 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) { 2730 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr); 2731 wr = &inv_wr; 2732 } 2733 2734 memset(&sig_wr, 0, sizeof(sig_wr)); 2735 sig_wr.opcode = IB_WR_REG_SIG_MR; 2736 sig_wr.wr_id = ISER_FASTREG_LI_WRID; 2737 sig_wr.sg_list = &rdma_wr->ib_sg[DATA]; 2738 sig_wr.num_sge = 1; 2739 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE; 2740 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs; 2741 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr; 2742 if (se_cmd->t_prot_sg) 2743 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT]; 2744 2745 if (!wr) 2746 wr = &sig_wr; 2747 else 2748 wr->next = &sig_wr; 2749 2750 ret = ib_post_send(isert_conn->qp, wr, &bad_wr); 2751 if (ret) { 2752 isert_err("fast registration failed, ret:%d\n", ret); 2753 goto err; 2754 } 2755 fr_desc->ind &= ~ISERT_SIG_KEY_VALID; 2756 2757 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey; 2758 rdma_wr->ib_sg[SIG].addr = 0; 2759 rdma_wr->ib_sg[SIG].length = se_cmd->data_length; 2760 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP && 2761 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT) 2762 /* 2763 * We have protection guards on the wire 2764 * so we need to set a larget transfer 2765 */ 2766 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length; 2767 2768 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n", 2769 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length, 2770 rdma_wr->ib_sg[SIG].lkey); 2771err: 2772 return ret; 2773} 2774 2775static int 2776isert_handle_prot_cmd(struct isert_conn *isert_conn, 2777 struct isert_cmd *isert_cmd, 2778 struct isert_rdma_wr *wr) 2779{ 2780 struct isert_device *device = isert_conn->device; 2781 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd; 2782 int ret; 2783 2784 if (!wr->fr_desc->pi_ctx) { 2785 ret = isert_create_pi_ctx(wr->fr_desc, 2786 device->ib_device, 2787 device->pd); 2788 if (ret) { 2789 isert_err("conn %p failed to allocate pi_ctx\n", 2790 isert_conn); 2791 return ret; 2792 } 2793 } 2794 2795 if (se_cmd->t_prot_sg) { 2796 ret = isert_map_data_buf(isert_conn, isert_cmd, 2797 se_cmd->t_prot_sg, 2798 se_cmd->t_prot_nents, 2799 se_cmd->prot_length, 2800 0, wr->iser_ib_op, &wr->prot); 2801 if (ret) { 2802 isert_err("conn %p failed to map protection buffer\n", 2803 isert_conn); 2804 return ret; 2805 } 2806 2807 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT])); 2808 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot, 2809 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]); 2810 if (ret) { 2811 isert_err("conn %p failed to fast reg mr\n", 2812 isert_conn); 2813 goto unmap_prot_cmd; 2814 } 2815 } 2816 2817 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc); 2818 if (ret) { 2819 isert_err("conn %p failed to fast reg mr\n", 2820 isert_conn); 2821 goto unmap_prot_cmd; 2822 } 2823 wr->fr_desc->ind |= ISERT_PROTECTED; 2824 2825 return 0; 2826 2827unmap_prot_cmd: 2828 if (se_cmd->t_prot_sg) 2829 isert_unmap_data_buf(isert_conn, &wr->prot); 2830 2831 return ret; 2832} 2833 2834static int 2835isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 2836 struct isert_rdma_wr *wr) 2837{ 2838 struct se_cmd *se_cmd = &cmd->se_cmd; 2839 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2840 struct isert_conn *isert_conn = conn->context; 2841 struct fast_reg_descriptor *fr_desc = NULL; 2842 struct ib_send_wr *send_wr; 2843 struct ib_sge *ib_sg; 2844 u32 offset; 2845 int ret = 0; 2846 unsigned long flags; 2847 2848 isert_cmd->tx_desc.isert_cmd = isert_cmd; 2849 2850 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0; 2851 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg, 2852 se_cmd->t_data_nents, se_cmd->data_length, 2853 offset, wr->iser_ib_op, &wr->data); 2854 if (ret) 2855 return ret; 2856 2857 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) { 2858 spin_lock_irqsave(&isert_conn->pool_lock, flags); 2859 fr_desc = list_first_entry(&isert_conn->fr_pool, 2860 struct fast_reg_descriptor, list); 2861 list_del(&fr_desc->list); 2862 spin_unlock_irqrestore(&isert_conn->pool_lock, flags); 2863 wr->fr_desc = fr_desc; 2864 } 2865 2866 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data, 2867 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]); 2868 if (ret) 2869 goto unmap_cmd; 2870 2871 if (isert_prot_cmd(isert_conn, se_cmd)) { 2872 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr); 2873 if (ret) 2874 goto unmap_cmd; 2875 2876 ib_sg = &wr->ib_sg[SIG]; 2877 } else { 2878 ib_sg = &wr->ib_sg[DATA]; 2879 } 2880 2881 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg)); 2882 wr->ib_sge = &wr->s_ib_sge; 2883 wr->send_wr_num = 1; 2884 memset(&wr->s_send_wr, 0, sizeof(*send_wr)); 2885 wr->send_wr = &wr->s_send_wr; 2886 wr->isert_cmd = isert_cmd; 2887 2888 send_wr = &isert_cmd->rdma_wr.s_send_wr; 2889 send_wr->sg_list = &wr->s_ib_sge; 2890 send_wr->num_sge = 1; 2891 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc; 2892 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) { 2893 send_wr->opcode = IB_WR_RDMA_WRITE; 2894 send_wr->wr.rdma.remote_addr = isert_cmd->read_va; 2895 send_wr->wr.rdma.rkey = isert_cmd->read_stag; 2896 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ? 2897 0 : IB_SEND_SIGNALED; 2898 } else { 2899 send_wr->opcode = IB_WR_RDMA_READ; 2900 send_wr->wr.rdma.remote_addr = isert_cmd->write_va; 2901 send_wr->wr.rdma.rkey = isert_cmd->write_stag; 2902 send_wr->send_flags = IB_SEND_SIGNALED; 2903 } 2904 2905 return 0; 2906 2907unmap_cmd: 2908 if (fr_desc) { 2909 spin_lock_irqsave(&isert_conn->pool_lock, flags); 2910 list_add_tail(&fr_desc->list, &isert_conn->fr_pool); 2911 spin_unlock_irqrestore(&isert_conn->pool_lock, flags); 2912 } 2913 isert_unmap_data_buf(isert_conn, &wr->data); 2914 2915 return ret; 2916} 2917 2918static int 2919isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd) 2920{ 2921 struct se_cmd *se_cmd = &cmd->se_cmd; 2922 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2923 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr; 2924 struct isert_conn *isert_conn = conn->context; 2925 struct isert_device *device = isert_conn->device; 2926 struct ib_send_wr *wr_failed; 2927 int rc; 2928 2929 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n", 2930 isert_cmd, se_cmd->data_length); 2931 2932 wr->iser_ib_op = ISER_IB_RDMA_WRITE; 2933 rc = device->reg_rdma_mem(conn, cmd, wr); 2934 if (rc) { 2935 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd); 2936 return rc; 2937 } 2938 2939 if (!isert_prot_cmd(isert_conn, se_cmd)) { 2940 /* 2941 * Build isert_conn->tx_desc for iSCSI response PDU and attach 2942 */ 2943 isert_create_send_desc(isert_conn, isert_cmd, 2944 &isert_cmd->tx_desc); 2945 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *) 2946 &isert_cmd->tx_desc.iscsi_header); 2947 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 2948 isert_init_send_wr(isert_conn, isert_cmd, 2949 &isert_cmd->tx_desc.send_wr); 2950 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr; 2951 wr->send_wr_num += 1; 2952 } 2953 2954 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed); 2955 if (rc) 2956 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n"); 2957 2958 if (!isert_prot_cmd(isert_conn, se_cmd)) 2959 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data " 2960 "READ\n", isert_cmd); 2961 else 2962 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n", 2963 isert_cmd); 2964 2965 return 1; 2966} 2967 2968static int 2969isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery) 2970{ 2971 struct se_cmd *se_cmd = &cmd->se_cmd; 2972 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2973 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr; 2974 struct isert_conn *isert_conn = conn->context; 2975 struct isert_device *device = isert_conn->device; 2976 struct ib_send_wr *wr_failed; 2977 int rc; 2978 2979 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n", 2980 isert_cmd, se_cmd->data_length, cmd->write_data_done); 2981 wr->iser_ib_op = ISER_IB_RDMA_READ; 2982 rc = device->reg_rdma_mem(conn, cmd, wr); 2983 if (rc) { 2984 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd); 2985 return rc; 2986 } 2987 2988 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed); 2989 if (rc) 2990 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n"); 2991 2992 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n", 2993 isert_cmd); 2994 2995 return 0; 2996} 2997 2998static int 2999isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state) 3000{ 3001 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 3002 int ret = 0; 3003 3004 switch (state) { 3005 case ISTATE_REMOVE: 3006 spin_lock_bh(&conn->cmd_lock); 3007 list_del_init(&cmd->i_conn_node); 3008 spin_unlock_bh(&conn->cmd_lock); 3009 isert_put_cmd(isert_cmd, true); 3010 break; 3011 case ISTATE_SEND_NOPIN_WANT_RESPONSE: 3012 ret = isert_put_nopin(cmd, conn, false); 3013 break; 3014 default: 3015 isert_err("Unknown immediate state: 0x%02x\n", state); 3016 ret = -EINVAL; 3017 break; 3018 } 3019 3020 return ret; 3021} 3022 3023static int 3024isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state) 3025{ 3026 struct isert_conn *isert_conn = conn->context; 3027 int ret; 3028 3029 switch (state) { 3030 case ISTATE_SEND_LOGOUTRSP: 3031 ret = isert_put_logout_rsp(cmd, conn); 3032 if (!ret) 3033 isert_conn->logout_posted = true; 3034 break; 3035 case ISTATE_SEND_NOPIN: 3036 ret = isert_put_nopin(cmd, conn, true); 3037 break; 3038 case ISTATE_SEND_TASKMGTRSP: 3039 ret = isert_put_tm_rsp(cmd, conn); 3040 break; 3041 case ISTATE_SEND_REJECT: 3042 ret = isert_put_reject(cmd, conn); 3043 break; 3044 case ISTATE_SEND_TEXTRSP: 3045 ret = isert_put_text_rsp(cmd, conn); 3046 break; 3047 case ISTATE_SEND_STATUS: 3048 /* 3049 * Special case for sending non GOOD SCSI status from TX thread 3050 * context during pre se_cmd excecution failure. 3051 */ 3052 ret = isert_put_response(conn, cmd); 3053 break; 3054 default: 3055 isert_err("Unknown response state: 0x%02x\n", state); 3056 ret = -EINVAL; 3057 break; 3058 } 3059 3060 return ret; 3061} 3062 3063struct rdma_cm_id * 3064isert_setup_id(struct isert_np *isert_np) 3065{ 3066 struct iscsi_np *np = isert_np->np; 3067 struct rdma_cm_id *id; 3068 struct sockaddr *sa; 3069 int ret; 3070 3071 sa = (struct sockaddr *)&np->np_sockaddr; 3072 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa); 3073 3074 id = rdma_create_id(isert_cma_handler, isert_np, 3075 RDMA_PS_TCP, IB_QPT_RC); 3076 if (IS_ERR(id)) { 3077 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id)); 3078 ret = PTR_ERR(id); 3079 goto out; 3080 } 3081 isert_dbg("id %p context %p\n", id, id->context); 3082 3083 ret = rdma_bind_addr(id, sa); 3084 if (ret) { 3085 isert_err("rdma_bind_addr() failed: %d\n", ret); 3086 goto out_id; 3087 } 3088 3089 ret = rdma_listen(id, 0); 3090 if (ret) { 3091 isert_err("rdma_listen() failed: %d\n", ret); 3092 goto out_id; 3093 } 3094 3095 return id; 3096out_id: 3097 rdma_destroy_id(id); 3098out: 3099 return ERR_PTR(ret); 3100} 3101 3102static int 3103isert_setup_np(struct iscsi_np *np, 3104 struct __kernel_sockaddr_storage *ksockaddr) 3105{ 3106 struct isert_np *isert_np; 3107 struct rdma_cm_id *isert_lid; 3108 int ret; 3109 3110 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL); 3111 if (!isert_np) { 3112 isert_err("Unable to allocate struct isert_np\n"); 3113 return -ENOMEM; 3114 } 3115 sema_init(&isert_np->np_sem, 0); 3116 mutex_init(&isert_np->np_accept_mutex); 3117 INIT_LIST_HEAD(&isert_np->np_accept_list); 3118 init_completion(&isert_np->np_login_comp); 3119 isert_np->np = np; 3120 3121 /* 3122 * Setup the np->np_sockaddr from the passed sockaddr setup 3123 * in iscsi_target_configfs.c code.. 3124 */ 3125 memcpy(&np->np_sockaddr, ksockaddr, 3126 sizeof(struct __kernel_sockaddr_storage)); 3127 3128 isert_lid = isert_setup_id(isert_np); 3129 if (IS_ERR(isert_lid)) { 3130 ret = PTR_ERR(isert_lid); 3131 goto out; 3132 } 3133 3134 isert_np->np_cm_id = isert_lid; 3135 np->np_context = isert_np; 3136 3137 return 0; 3138 3139out: 3140 kfree(isert_np); 3141 3142 return ret; 3143} 3144 3145static int 3146isert_rdma_accept(struct isert_conn *isert_conn) 3147{ 3148 struct rdma_cm_id *cm_id = isert_conn->cm_id; 3149 struct rdma_conn_param cp; 3150 int ret; 3151 3152 memset(&cp, 0, sizeof(struct rdma_conn_param)); 3153 cp.initiator_depth = isert_conn->initiator_depth; 3154 cp.retry_count = 7; 3155 cp.rnr_retry_count = 7; 3156 3157 ret = rdma_accept(cm_id, &cp); 3158 if (ret) { 3159 isert_err("rdma_accept() failed with: %d\n", ret); 3160 return ret; 3161 } 3162 3163 return 0; 3164} 3165 3166static int 3167isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login) 3168{ 3169 struct isert_conn *isert_conn = conn->context; 3170 int ret; 3171 3172 isert_info("before login_req comp conn: %p\n", isert_conn); 3173 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp); 3174 if (ret) { 3175 isert_err("isert_conn %p interrupted before got login req\n", 3176 isert_conn); 3177 return ret; 3178 } 3179 reinit_completion(&isert_conn->login_req_comp); 3180 3181 /* 3182 * For login requests after the first PDU, isert_rx_login_req() will 3183 * kick schedule_delayed_work(&conn->login_work) as the packet is 3184 * received, which turns this callback from iscsi_target_do_login_rx() 3185 * into a NOP. 3186 */ 3187 if (!login->first_request) 3188 return 0; 3189 3190 isert_rx_login_req(isert_conn); 3191 3192 isert_info("before login_comp conn: %p\n", conn); 3193 ret = wait_for_completion_interruptible(&isert_conn->login_comp); 3194 if (ret) 3195 return ret; 3196 3197 isert_info("processing login->req: %p\n", login->req); 3198 3199 return 0; 3200} 3201 3202static void 3203isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn, 3204 struct isert_conn *isert_conn) 3205{ 3206 struct rdma_cm_id *cm_id = isert_conn->cm_id; 3207 struct rdma_route *cm_route = &cm_id->route; 3208 struct sockaddr_in *sock_in; 3209 struct sockaddr_in6 *sock_in6; 3210 3211 conn->login_family = np->np_sockaddr.ss_family; 3212 3213 if (np->np_sockaddr.ss_family == AF_INET6) { 3214 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr; 3215 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c", 3216 &sock_in6->sin6_addr.in6_u); 3217 conn->login_port = ntohs(sock_in6->sin6_port); 3218 3219 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr; 3220 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c", 3221 &sock_in6->sin6_addr.in6_u); 3222 conn->local_port = ntohs(sock_in6->sin6_port); 3223 } else { 3224 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr; 3225 sprintf(conn->login_ip, "%pI4", 3226 &sock_in->sin_addr.s_addr); 3227 conn->login_port = ntohs(sock_in->sin_port); 3228 3229 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr; 3230 sprintf(conn->local_ip, "%pI4", 3231 &sock_in->sin_addr.s_addr); 3232 conn->local_port = ntohs(sock_in->sin_port); 3233 } 3234} 3235 3236static int 3237isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn) 3238{ 3239 struct isert_np *isert_np = np->np_context; 3240 struct isert_conn *isert_conn; 3241 int ret; 3242 3243accept_wait: 3244 ret = down_interruptible(&isert_np->np_sem); 3245 if (ret) 3246 return -ENODEV; 3247 3248 spin_lock_bh(&np->np_thread_lock); 3249 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) { 3250 spin_unlock_bh(&np->np_thread_lock); 3251 isert_dbg("np_thread_state %d\n", 3252 np->np_thread_state); 3253 /** 3254 * No point in stalling here when np_thread 3255 * is in state RESET/SHUTDOWN/EXIT - bail 3256 **/ 3257 return -ENODEV; 3258 } 3259 spin_unlock_bh(&np->np_thread_lock); 3260 3261 mutex_lock(&isert_np->np_accept_mutex); 3262 if (list_empty(&isert_np->np_accept_list)) { 3263 mutex_unlock(&isert_np->np_accept_mutex); 3264 goto accept_wait; 3265 } 3266 isert_conn = list_first_entry(&isert_np->np_accept_list, 3267 struct isert_conn, accept_node); 3268 list_del_init(&isert_conn->accept_node); 3269 mutex_unlock(&isert_np->np_accept_mutex); 3270 3271 conn->context = isert_conn; 3272 isert_conn->conn = conn; 3273 isert_conn->state = ISER_CONN_BOUND; 3274 3275 isert_set_conn_info(np, conn, isert_conn); 3276 3277 isert_dbg("Processing isert_conn: %p\n", isert_conn); 3278 3279 return 0; 3280} 3281 3282static void 3283isert_free_np(struct iscsi_np *np) 3284{ 3285 struct isert_np *isert_np = np->np_context; 3286 struct isert_conn *isert_conn, *n; 3287 3288 if (isert_np->np_cm_id) 3289 rdma_destroy_id(isert_np->np_cm_id); 3290 3291 /* 3292 * FIXME: At this point we don't have a good way to insure 3293 * that at this point we don't have hanging connections that 3294 * completed RDMA establishment but didn't start iscsi login 3295 * process. So work-around this by cleaning up what ever piled 3296 * up in np_accept_list. 3297 */ 3298 mutex_lock(&isert_np->np_accept_mutex); 3299 if (!list_empty(&isert_np->np_accept_list)) { 3300 isert_info("Still have isert connections, cleaning up...\n"); 3301 list_for_each_entry_safe(isert_conn, n, 3302 &isert_np->np_accept_list, 3303 accept_node) { 3304 isert_info("cleaning isert_conn %p state (%d)\n", 3305 isert_conn, isert_conn->state); 3306 isert_connect_release(isert_conn); 3307 } 3308 } 3309 mutex_unlock(&isert_np->np_accept_mutex); 3310 3311 np->np_context = NULL; 3312 kfree(isert_np); 3313} 3314 3315static void isert_release_work(struct work_struct *work) 3316{ 3317 struct isert_conn *isert_conn = container_of(work, 3318 struct isert_conn, 3319 release_work); 3320 3321 isert_info("Starting release conn %p\n", isert_conn); 3322 3323 wait_for_completion(&isert_conn->wait); 3324 3325 mutex_lock(&isert_conn->mutex); 3326 isert_conn->state = ISER_CONN_DOWN; 3327 mutex_unlock(&isert_conn->mutex); 3328 3329 isert_info("Destroying conn %p\n", isert_conn); 3330 isert_put_conn(isert_conn); 3331} 3332 3333static void 3334isert_wait4logout(struct isert_conn *isert_conn) 3335{ 3336 struct iscsi_conn *conn = isert_conn->conn; 3337 3338 isert_info("conn %p\n", isert_conn); 3339 3340 if (isert_conn->logout_posted) { 3341 isert_info("conn %p wait for conn_logout_comp\n", isert_conn); 3342 wait_for_completion_timeout(&conn->conn_logout_comp, 3343 SECONDS_FOR_LOGOUT_COMP * HZ); 3344 } 3345} 3346 3347static void 3348isert_wait4cmds(struct iscsi_conn *conn) 3349{ 3350 isert_info("iscsi_conn %p\n", conn); 3351 3352 if (conn->sess) { 3353 target_sess_cmd_list_set_waiting(conn->sess->se_sess); 3354 target_wait_for_sess_cmds(conn->sess->se_sess); 3355 } 3356} 3357 3358static void 3359isert_wait4flush(struct isert_conn *isert_conn) 3360{ 3361 struct ib_recv_wr *bad_wr; 3362 3363 isert_info("conn %p\n", isert_conn); 3364 3365 init_completion(&isert_conn->wait_comp_err); 3366 isert_conn->beacon.wr_id = ISER_BEACON_WRID; 3367 /* post an indication that all flush errors were consumed */ 3368 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) { 3369 isert_err("conn %p failed to post beacon", isert_conn); 3370 return; 3371 } 3372 3373 wait_for_completion(&isert_conn->wait_comp_err); 3374} 3375 3376/** 3377 * isert_put_unsol_pending_cmds() - Drop commands waiting for 3378 * unsolicitate dataout 3379 * @conn: iscsi connection 3380 * 3381 * We might still have commands that are waiting for unsolicited 3382 * dataouts messages. We must put the extra reference on those 3383 * before blocking on the target_wait_for_session_cmds 3384 */ 3385static void 3386isert_put_unsol_pending_cmds(struct iscsi_conn *conn) 3387{ 3388 struct iscsi_cmd *cmd, *tmp; 3389 static LIST_HEAD(drop_cmd_list); 3390 3391 spin_lock_bh(&conn->cmd_lock); 3392 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) { 3393 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) && 3394 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) && 3395 (cmd->write_data_done < cmd->se_cmd.data_length)) 3396 list_move_tail(&cmd->i_conn_node, &drop_cmd_list); 3397 } 3398 spin_unlock_bh(&conn->cmd_lock); 3399 3400 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) { 3401 list_del_init(&cmd->i_conn_node); 3402 if (cmd->i_state != ISTATE_REMOVE) { 3403 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 3404 3405 isert_info("conn %p dropping cmd %p\n", conn, cmd); 3406 isert_put_cmd(isert_cmd, true); 3407 } 3408 } 3409} 3410 3411static void isert_wait_conn(struct iscsi_conn *conn) 3412{ 3413 struct isert_conn *isert_conn = conn->context; 3414 3415 isert_info("Starting conn %p\n", isert_conn); 3416 3417 mutex_lock(&isert_conn->mutex); 3418 /* 3419 * Only wait for wait_comp_err if the isert_conn made it 3420 * into full feature phase.. 3421 */ 3422 if (isert_conn->state == ISER_CONN_INIT) { 3423 mutex_unlock(&isert_conn->mutex); 3424 return; 3425 } 3426 isert_conn_terminate(isert_conn); 3427 mutex_unlock(&isert_conn->mutex); 3428 3429 isert_wait4flush(isert_conn); 3430 isert_put_unsol_pending_cmds(conn); 3431 isert_wait4cmds(conn); 3432 isert_wait4logout(isert_conn); 3433 3434 queue_work(isert_release_wq, &isert_conn->release_work); 3435} 3436 3437static void isert_free_conn(struct iscsi_conn *conn) 3438{ 3439 struct isert_conn *isert_conn = conn->context; 3440 3441 isert_wait4flush(isert_conn); 3442 isert_put_conn(isert_conn); 3443} 3444 3445static struct iscsit_transport iser_target_transport = { 3446 .name = "IB/iSER", 3447 .transport_type = ISCSI_INFINIBAND, 3448 .priv_size = sizeof(struct isert_cmd), 3449 .owner = THIS_MODULE, 3450 .iscsit_setup_np = isert_setup_np, 3451 .iscsit_accept_np = isert_accept_np, 3452 .iscsit_free_np = isert_free_np, 3453 .iscsit_wait_conn = isert_wait_conn, 3454 .iscsit_free_conn = isert_free_conn, 3455 .iscsit_get_login_rx = isert_get_login_rx, 3456 .iscsit_put_login_tx = isert_put_login_tx, 3457 .iscsit_immediate_queue = isert_immediate_queue, 3458 .iscsit_response_queue = isert_response_queue, 3459 .iscsit_get_dataout = isert_get_dataout, 3460 .iscsit_queue_data_in = isert_put_datain, 3461 .iscsit_queue_status = isert_put_response, 3462 .iscsit_aborted_task = isert_aborted_task, 3463 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops, 3464}; 3465 3466static int __init isert_init(void) 3467{ 3468 int ret; 3469 3470 isert_comp_wq = alloc_workqueue("isert_comp_wq", 3471 WQ_UNBOUND | WQ_HIGHPRI, 0); 3472 if (!isert_comp_wq) { 3473 isert_err("Unable to allocate isert_comp_wq\n"); 3474 ret = -ENOMEM; 3475 return -ENOMEM; 3476 } 3477 3478 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND, 3479 WQ_UNBOUND_MAX_ACTIVE); 3480 if (!isert_release_wq) { 3481 isert_err("Unable to allocate isert_release_wq\n"); 3482 ret = -ENOMEM; 3483 goto destroy_comp_wq; 3484 } 3485 3486 iscsit_register_transport(&iser_target_transport); 3487 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n"); 3488 3489 return 0; 3490 3491destroy_comp_wq: 3492 destroy_workqueue(isert_comp_wq); 3493 3494 return ret; 3495} 3496 3497static void __exit isert_exit(void) 3498{ 3499 flush_scheduled_work(); 3500 destroy_workqueue(isert_release_wq); 3501 destroy_workqueue(isert_comp_wq); 3502 iscsit_unregister_transport(&iser_target_transport); 3503 isert_info("iSER_TARGET[0] - Released iser_target_transport\n"); 3504} 3505 3506MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure"); 3507MODULE_VERSION("1.0"); 3508MODULE_AUTHOR("nab@Linux-iSCSI.org"); 3509MODULE_LICENSE("GPL"); 3510 3511module_init(isert_init); 3512module_exit(isert_exit); 3513