1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2b8d26b3bSNicholas Bellinger /******************************************************************************* 3b8d26b3bSNicholas Bellinger * This file contains iSCSI extentions for RDMA (iSER) Verbs 4b8d26b3bSNicholas Bellinger * 54c76251eSNicholas Bellinger * (c) Copyright 2013 Datera, Inc. 6b8d26b3bSNicholas Bellinger * 7b8d26b3bSNicholas Bellinger * Nicholas A. Bellinger <nab@linux-iscsi.org> 8b8d26b3bSNicholas Bellinger * 9b8d26b3bSNicholas Bellinger ****************************************************************************/ 10b8d26b3bSNicholas Bellinger 11b8d26b3bSNicholas Bellinger #include <linux/string.h> 12b8d26b3bSNicholas Bellinger #include <linux/module.h> 13b8d26b3bSNicholas Bellinger #include <linux/scatterlist.h> 14b8d26b3bSNicholas Bellinger #include <linux/socket.h> 15b8d26b3bSNicholas Bellinger #include <linux/in.h> 16b8d26b3bSNicholas Bellinger #include <linux/in6.h> 17b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h> 188094ba0aSLeon Romanovsky #include <rdma/ib_cm.h> 19b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h> 20b8d26b3bSNicholas Bellinger #include <target/target_core_base.h> 21b8d26b3bSNicholas Bellinger #include <target/target_core_fabric.h> 22b8d26b3bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h> 23531b7bf4SSagi Grimberg #include <linux/semaphore.h> 24b8d26b3bSNicholas Bellinger 25b8d26b3bSNicholas Bellinger #include "ib_isert.h" 26b8d26b3bSNicholas Bellinger 2745678b6bSSagi Grimberg static int isert_debug_level; 2824f412ddSSagi Grimberg module_param_named(debug_level, isert_debug_level, int, 0644); 2924f412ddSSagi Grimberg MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)"); 3024f412ddSSagi Grimberg 31dae7a75fSMax Gurtovoy static int isert_sg_tablesize_set(const char *val, 32dae7a75fSMax Gurtovoy const struct kernel_param *kp); 33dae7a75fSMax Gurtovoy static const struct kernel_param_ops sg_tablesize_ops = { 34dae7a75fSMax Gurtovoy .set = isert_sg_tablesize_set, 35dae7a75fSMax Gurtovoy .get = param_get_int, 36dae7a75fSMax Gurtovoy }; 37dae7a75fSMax Gurtovoy 38109d19a5SMax Gurtovoy static int isert_sg_tablesize = ISCSI_ISER_MIN_SG_TABLESIZE; 39dae7a75fSMax Gurtovoy module_param_cb(sg_tablesize, &sg_tablesize_ops, &isert_sg_tablesize, 0644); 40dae7a75fSMax Gurtovoy MODULE_PARM_DESC(sg_tablesize, 41109d19a5SMax Gurtovoy "Number of gather/scatter entries in a single scsi command, should >= 128 (default: 128, max: 4096)"); 42dae7a75fSMax Gurtovoy 43b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex); 44b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list); 45549f39a5STetsuo Handa static struct workqueue_struct *isert_login_wq; 46b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq; 47b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq; 48b8d26b3bSNicholas Bellinger 49f93f3a70SSagi Grimberg static int 50be36d683SMax Gurtovoy isert_put_response(struct iscsit_conn *conn, struct iscsit_cmd *cmd); 512371e5daSSagi Grimberg static int 529679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn); 532371e5daSSagi Grimberg static int 542371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn); 55ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np); 56d40945d8SVu Pham 572f1b6b7dSSagi Grimberg static void isert_release_work(struct work_struct *work); 589679cc51SChristoph Hellwig static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc); 599679cc51SChristoph Hellwig static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc); 609679cc51SChristoph Hellwig static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc); 619679cc51SChristoph Hellwig static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc); 622f1b6b7dSSagi Grimberg 63dae7a75fSMax Gurtovoy static int isert_sg_tablesize_set(const char *val, const struct kernel_param *kp) 64dae7a75fSMax Gurtovoy { 65dae7a75fSMax Gurtovoy int n = 0, ret; 66dae7a75fSMax Gurtovoy 67dae7a75fSMax Gurtovoy ret = kstrtoint(val, 10, &n); 68dae7a75fSMax Gurtovoy if (ret != 0 || n < ISCSI_ISER_MIN_SG_TABLESIZE || 69dae7a75fSMax Gurtovoy n > ISCSI_ISER_MAX_SG_TABLESIZE) 70dae7a75fSMax Gurtovoy return -EINVAL; 71dae7a75fSMax Gurtovoy 72dae7a75fSMax Gurtovoy return param_set_int(val, kp); 73dae7a75fSMax Gurtovoy } 74dae7a75fSMax Gurtovoy 75302cc7c3SSagi Grimberg static inline bool 76302cc7c3SSagi Grimberg isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd) 77302cc7c3SSagi Grimberg { 7823a548eeSSagi Grimberg return (conn->pi_support && 79302cc7c3SSagi Grimberg cmd->prot_op != TARGET_PROT_NORMAL); 80302cc7c3SSagi Grimberg } 81302cc7c3SSagi Grimberg 82d40945d8SVu Pham static void 83b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context) 84b8d26b3bSNicholas Bellinger { 856700425eSSagi Grimberg struct isert_conn *isert_conn = context; 86b8d26b3bSNicholas Bellinger 87ea8a1616SSagi Grimberg isert_err("%s (%d): conn %p\n", 88ea8a1616SSagi Grimberg ib_event_msg(e->event), e->event, isert_conn); 89ea8a1616SSagi Grimberg 90b8d26b3bSNicholas Bellinger switch (e->event) { 91b8d26b3bSNicholas Bellinger case IB_EVENT_COMM_EST: 92dac6ab30SSagi Grimberg rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST); 93b8d26b3bSNicholas Bellinger break; 94b8d26b3bSNicholas Bellinger case IB_EVENT_QP_LAST_WQE_REACHED: 954c22e07fSSagi Grimberg isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n"); 96b8d26b3bSNicholas Bellinger break; 97b8d26b3bSNicholas Bellinger default: 98b8d26b3bSNicholas Bellinger break; 99b8d26b3bSNicholas Bellinger } 100b8d26b3bSNicholas Bellinger } 101b8d26b3bSNicholas Bellinger 10240fc069aSSagi Grimberg static struct ib_qp * 10340fc069aSSagi Grimberg isert_create_qp(struct isert_conn *isert_conn, 10440fc069aSSagi Grimberg struct rdma_cm_id *cma_id) 10540fc069aSSagi Grimberg { 106c6e66307SYamin Friedman u32 cq_size = ISERT_QP_MAX_REQ_DTOS + ISERT_QP_MAX_RECV_DTOS + 2; 107dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 108c6e66307SYamin Friedman struct ib_device *ib_dev = device->ib_device; 10940fc069aSSagi Grimberg struct ib_qp_init_attr attr; 110317000b9SMax Gurtovoy int ret, factor; 111b8d26b3bSNicholas Bellinger 112c6e66307SYamin Friedman isert_conn->cq = ib_cq_pool_get(ib_dev, cq_size, -1, IB_POLL_WORKQUEUE); 113c6e66307SYamin Friedman if (IS_ERR(isert_conn->cq)) { 114c6e66307SYamin Friedman isert_err("Unable to allocate cq\n"); 115c6e66307SYamin Friedman ret = PTR_ERR(isert_conn->cq); 116c6e66307SYamin Friedman return ERR_PTR(ret); 117c6e66307SYamin Friedman } 118c6e66307SYamin Friedman isert_conn->cq_size = cq_size; 119c6e66307SYamin Friedman 120b8d26b3bSNicholas Bellinger memset(&attr, 0, sizeof(struct ib_qp_init_attr)); 121b8d26b3bSNicholas Bellinger attr.event_handler = isert_qp_event_callback; 122b8d26b3bSNicholas Bellinger attr.qp_context = isert_conn; 123c6e66307SYamin Friedman attr.send_cq = isert_conn->cq; 124c6e66307SYamin Friedman attr.recv_cq = isert_conn->cq; 125572a1434SSagi Grimberg attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS + 1; 126bdf20e72SSagi Grimberg attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1; 127317000b9SMax Gurtovoy factor = rdma_rw_mr_factor(device->ib_device, cma_id->port_num, 128dae7a75fSMax Gurtovoy isert_sg_tablesize); 129317000b9SMax Gurtovoy attr.cap.max_rdma_ctxs = ISCSI_DEF_XMIT_CMDS_MAX * factor; 13033023fb8SSteve Wise attr.cap.max_send_sge = device->ib_device->attrs.max_send_sge; 131b8d26b3bSNicholas Bellinger attr.cap.max_recv_sge = 1; 132b8d26b3bSNicholas Bellinger attr.sq_sig_type = IB_SIGNAL_REQ_WR; 133b8d26b3bSNicholas Bellinger attr.qp_type = IB_QPT_RC; 134570db170SSagi Grimberg if (device->pi_capable) 135c0a6cbb9SIsrael Rukshin attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN; 136b8d26b3bSNicholas Bellinger 13767cb3949SSagi Grimberg ret = rdma_create_qp(cma_id, device->pd, &attr); 138b8d26b3bSNicholas Bellinger if (ret) { 13924f412ddSSagi Grimberg isert_err("rdma_create_qp failed for cma_id %d\n", ret); 140c6e66307SYamin Friedman ib_cq_pool_put(isert_conn->cq, isert_conn->cq_size); 141c6e66307SYamin Friedman 14240fc069aSSagi Grimberg return ERR_PTR(ret); 14340fc069aSSagi Grimberg } 14440fc069aSSagi Grimberg 14540fc069aSSagi Grimberg return cma_id->qp; 14640fc069aSSagi Grimberg } 14740fc069aSSagi Grimberg 14840fc069aSSagi Grimberg static int 149b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn) 150b8d26b3bSNicholas Bellinger { 151dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 15267cb3949SSagi Grimberg struct ib_device *ib_dev = device->ib_device; 153b8d26b3bSNicholas Bellinger struct iser_rx_desc *rx_desc; 154b8d26b3bSNicholas Bellinger struct ib_sge *rx_sg; 155b8d26b3bSNicholas Bellinger u64 dma_addr; 156b8d26b3bSNicholas Bellinger int i, j; 157b8d26b3bSNicholas Bellinger 1586396bb22SKees Cook isert_conn->rx_descs = kcalloc(ISERT_QP_MAX_RECV_DTOS, 1596396bb22SKees Cook sizeof(struct iser_rx_desc), 1606396bb22SKees Cook GFP_KERNEL); 161dac6ab30SSagi Grimberg if (!isert_conn->rx_descs) 16293b80f29SLeon Romanovsky return -ENOMEM; 163b8d26b3bSNicholas Bellinger 164dac6ab30SSagi Grimberg rx_desc = isert_conn->rx_descs; 165b8d26b3bSNicholas Bellinger 166b8d26b3bSNicholas Bellinger for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) { 1670b089c1eSSagi Grimberg dma_addr = ib_dma_map_single(ib_dev, rx_desc->buf, 1680b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 169b8d26b3bSNicholas Bellinger if (ib_dma_mapping_error(ib_dev, dma_addr)) 170b8d26b3bSNicholas Bellinger goto dma_map_fail; 171b8d26b3bSNicholas Bellinger 172b8d26b3bSNicholas Bellinger rx_desc->dma_addr = dma_addr; 173b8d26b3bSNicholas Bellinger 174b8d26b3bSNicholas Bellinger rx_sg = &rx_desc->rx_sg; 1750b089c1eSSagi Grimberg rx_sg->addr = rx_desc->dma_addr + isert_get_hdr_offset(rx_desc); 176b8d26b3bSNicholas Bellinger rx_sg->length = ISER_RX_PAYLOAD_SIZE; 17734efc7dfSJason Gunthorpe rx_sg->lkey = device->pd->local_dma_lkey; 1789679cc51SChristoph Hellwig rx_desc->rx_cqe.done = isert_recv_done; 179b8d26b3bSNicholas Bellinger } 180b8d26b3bSNicholas Bellinger 181b8d26b3bSNicholas Bellinger return 0; 182b8d26b3bSNicholas Bellinger 183b8d26b3bSNicholas Bellinger dma_map_fail: 184dac6ab30SSagi Grimberg rx_desc = isert_conn->rx_descs; 185b8d26b3bSNicholas Bellinger for (j = 0; j < i; j++, rx_desc++) { 186b8d26b3bSNicholas Bellinger ib_dma_unmap_single(ib_dev, rx_desc->dma_addr, 1870b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 188b8d26b3bSNicholas Bellinger } 189dac6ab30SSagi Grimberg kfree(isert_conn->rx_descs); 190dac6ab30SSagi Grimberg isert_conn->rx_descs = NULL; 1914c22e07fSSagi Grimberg isert_err("conn %p failed to allocate rx descriptors\n", isert_conn); 192b8d26b3bSNicholas Bellinger return -ENOMEM; 193b8d26b3bSNicholas Bellinger } 194b8d26b3bSNicholas Bellinger 195b8d26b3bSNicholas Bellinger static void 196b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn) 197b8d26b3bSNicholas Bellinger { 198dac6ab30SSagi Grimberg struct ib_device *ib_dev = isert_conn->device->ib_device; 199b8d26b3bSNicholas Bellinger struct iser_rx_desc *rx_desc; 200b8d26b3bSNicholas Bellinger int i; 201b8d26b3bSNicholas Bellinger 202dac6ab30SSagi Grimberg if (!isert_conn->rx_descs) 203b8d26b3bSNicholas Bellinger return; 204b8d26b3bSNicholas Bellinger 205dac6ab30SSagi Grimberg rx_desc = isert_conn->rx_descs; 206b8d26b3bSNicholas Bellinger for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) { 207b8d26b3bSNicholas Bellinger ib_dma_unmap_single(ib_dev, rx_desc->dma_addr, 2080b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 209b8d26b3bSNicholas Bellinger } 210b8d26b3bSNicholas Bellinger 211dac6ab30SSagi Grimberg kfree(isert_conn->rx_descs); 212dac6ab30SSagi Grimberg isert_conn->rx_descs = NULL; 213b8d26b3bSNicholas Bellinger } 214b8d26b3bSNicholas Bellinger 215172369c5SSagi Grimberg static int 216172369c5SSagi Grimberg isert_create_device_ib_res(struct isert_device *device) 217172369c5SSagi Grimberg { 2184a061b28SOr Gerlitz struct ib_device *ib_dev = device->ib_device; 219fd8205e8SSagi Grimberg int ret; 220172369c5SSagi Grimberg 22133023fb8SSteve Wise isert_dbg("devattr->max_send_sge: %d devattr->max_recv_sge %d\n", 22233023fb8SSteve Wise ib_dev->attrs.max_send_sge, ib_dev->attrs.max_recv_sge); 2234a061b28SOr Gerlitz isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd); 224172369c5SSagi Grimberg 225ed082d36SChristoph Hellwig device->pd = ib_alloc_pd(ib_dev, 0); 22667cb3949SSagi Grimberg if (IS_ERR(device->pd)) { 22767cb3949SSagi Grimberg ret = PTR_ERR(device->pd); 22867cb3949SSagi Grimberg isert_err("failed to allocate pd, device %p, ret=%d\n", 22967cb3949SSagi Grimberg device, ret); 230c6e66307SYamin Friedman return ret; 23167cb3949SSagi Grimberg } 23267cb3949SSagi Grimberg 233172369c5SSagi Grimberg /* Check signature cap */ 234e945c653SJason Gunthorpe if (ib_dev->attrs.kernel_cap_flags & IBK_INTEGRITY_HANDOVER) 235a6dc16b6SMax Gurtovoy device->pi_capable = true; 236a6dc16b6SMax Gurtovoy else 237a6dc16b6SMax Gurtovoy device->pi_capable = false; 23867cb3949SSagi Grimberg 239b8d26b3bSNicholas Bellinger return 0; 240b8d26b3bSNicholas Bellinger } 241b8d26b3bSNicholas Bellinger 242b8d26b3bSNicholas Bellinger static void 243b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device) 244b8d26b3bSNicholas Bellinger { 24524f412ddSSagi Grimberg isert_info("device %p\n", device); 246b8d26b3bSNicholas Bellinger 24767cb3949SSagi Grimberg ib_dealloc_pd(device->pd); 248b8d26b3bSNicholas Bellinger } 249b8d26b3bSNicholas Bellinger 250b8d26b3bSNicholas Bellinger static void 251cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device) 252b8d26b3bSNicholas Bellinger { 253b8d26b3bSNicholas Bellinger mutex_lock(&device_list_mutex); 254b8d26b3bSNicholas Bellinger device->refcount--; 2554c22e07fSSagi Grimberg isert_info("device %p refcount %d\n", device, device->refcount); 256b8d26b3bSNicholas Bellinger if (!device->refcount) { 257b8d26b3bSNicholas Bellinger isert_free_device_ib_res(device); 258b8d26b3bSNicholas Bellinger list_del(&device->dev_node); 259b8d26b3bSNicholas Bellinger kfree(device); 260b8d26b3bSNicholas Bellinger } 261b8d26b3bSNicholas Bellinger mutex_unlock(&device_list_mutex); 262b8d26b3bSNicholas Bellinger } 263b8d26b3bSNicholas Bellinger 264b8d26b3bSNicholas Bellinger static struct isert_device * 265cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id) 266b8d26b3bSNicholas Bellinger { 267b8d26b3bSNicholas Bellinger struct isert_device *device; 268b8d26b3bSNicholas Bellinger int ret; 269b8d26b3bSNicholas Bellinger 270b8d26b3bSNicholas Bellinger mutex_lock(&device_list_mutex); 271b8d26b3bSNicholas Bellinger list_for_each_entry(device, &device_list, dev_node) { 272b8d26b3bSNicholas Bellinger if (device->ib_device->node_guid == cma_id->device->node_guid) { 273b8d26b3bSNicholas Bellinger device->refcount++; 2744c22e07fSSagi Grimberg isert_info("Found iser device %p refcount %d\n", 2754c22e07fSSagi Grimberg device, device->refcount); 276b8d26b3bSNicholas Bellinger mutex_unlock(&device_list_mutex); 277b8d26b3bSNicholas Bellinger return device; 278b8d26b3bSNicholas Bellinger } 279b8d26b3bSNicholas Bellinger } 280b8d26b3bSNicholas Bellinger 281b8d26b3bSNicholas Bellinger device = kzalloc(sizeof(struct isert_device), GFP_KERNEL); 282b8d26b3bSNicholas Bellinger if (!device) { 283b8d26b3bSNicholas Bellinger mutex_unlock(&device_list_mutex); 284b8d26b3bSNicholas Bellinger return ERR_PTR(-ENOMEM); 285b8d26b3bSNicholas Bellinger } 286b8d26b3bSNicholas Bellinger 287b8d26b3bSNicholas Bellinger INIT_LIST_HEAD(&device->dev_node); 288b8d26b3bSNicholas Bellinger 289b8d26b3bSNicholas Bellinger device->ib_device = cma_id->device; 290b8d26b3bSNicholas Bellinger ret = isert_create_device_ib_res(device); 291b8d26b3bSNicholas Bellinger if (ret) { 292b8d26b3bSNicholas Bellinger kfree(device); 293b8d26b3bSNicholas Bellinger mutex_unlock(&device_list_mutex); 294b8d26b3bSNicholas Bellinger return ERR_PTR(ret); 295b8d26b3bSNicholas Bellinger } 296b8d26b3bSNicholas Bellinger 297b8d26b3bSNicholas Bellinger device->refcount++; 298b8d26b3bSNicholas Bellinger list_add_tail(&device->dev_node, &device_list); 2994c22e07fSSagi Grimberg isert_info("Created a new iser device %p refcount %d\n", 3004c22e07fSSagi Grimberg device, device->refcount); 301b8d26b3bSNicholas Bellinger mutex_unlock(&device_list_mutex); 302b8d26b3bSNicholas Bellinger 303b8d26b3bSNicholas Bellinger return device; 304b8d26b3bSNicholas Bellinger } 305b8d26b3bSNicholas Bellinger 30659464ef4SVu Pham static void 307ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn) 308ae9ea9edSSagi Grimberg { 309ae9ea9edSSagi Grimberg isert_conn->state = ISER_CONN_INIT; 310bd379220SJenny Derzhavetz INIT_LIST_HEAD(&isert_conn->node); 311dac6ab30SSagi Grimberg init_completion(&isert_conn->login_comp); 312ae9ea9edSSagi Grimberg init_completion(&isert_conn->login_req_comp); 31363b268d2SRaju Rangoju init_waitqueue_head(&isert_conn->rem_wait); 314dac6ab30SSagi Grimberg kref_init(&isert_conn->kref); 315dac6ab30SSagi Grimberg mutex_init(&isert_conn->mutex); 3162f1b6b7dSSagi Grimberg INIT_WORK(&isert_conn->release_work, isert_release_work); 317ae9ea9edSSagi Grimberg } 318ae9ea9edSSagi Grimberg 319ae9ea9edSSagi Grimberg static void 320ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn) 321ae9ea9edSSagi Grimberg { 322dac6ab30SSagi Grimberg struct ib_device *ib_dev = isert_conn->device->ib_device; 323ae9ea9edSSagi Grimberg 324ae9ea9edSSagi Grimberg ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma, 325ed1083b2SChristoph Hellwig ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE); 3265adabdd1SChristoph Hellwig kfree(isert_conn->login_rsp_buf); 3275adabdd1SChristoph Hellwig 3280b089c1eSSagi Grimberg ib_dma_unmap_single(ib_dev, isert_conn->login_desc->dma_addr, 3290b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 3300b089c1eSSagi Grimberg kfree(isert_conn->login_desc); 331ae9ea9edSSagi Grimberg } 332ae9ea9edSSagi Grimberg 333ae9ea9edSSagi Grimberg static int 334ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn, 335ae9ea9edSSagi Grimberg struct ib_device *ib_dev) 336ae9ea9edSSagi Grimberg { 337ae9ea9edSSagi Grimberg int ret; 338ae9ea9edSSagi Grimberg 3390b089c1eSSagi Grimberg isert_conn->login_desc = kzalloc(sizeof(*isert_conn->login_desc), 3405adabdd1SChristoph Hellwig GFP_KERNEL); 3410b089c1eSSagi Grimberg if (!isert_conn->login_desc) 342ae9ea9edSSagi Grimberg return -ENOMEM; 343ae9ea9edSSagi Grimberg 3440b089c1eSSagi Grimberg isert_conn->login_desc->dma_addr = ib_dma_map_single(ib_dev, 3450b089c1eSSagi Grimberg isert_conn->login_desc->buf, 3460b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 3470b089c1eSSagi Grimberg ret = ib_dma_mapping_error(ib_dev, isert_conn->login_desc->dma_addr); 348ae9ea9edSSagi Grimberg if (ret) { 3490b089c1eSSagi Grimberg isert_err("login_desc dma mapping error: %d\n", ret); 3500b089c1eSSagi Grimberg isert_conn->login_desc->dma_addr = 0; 3510b089c1eSSagi Grimberg goto out_free_login_desc; 3525adabdd1SChristoph Hellwig } 3535adabdd1SChristoph Hellwig 3545adabdd1SChristoph Hellwig isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL); 3555adabdd1SChristoph Hellwig if (!isert_conn->login_rsp_buf) { 3561d5840c9SWei Yongjun ret = -ENOMEM; 3570b089c1eSSagi Grimberg goto out_unmap_login_desc; 358ae9ea9edSSagi Grimberg } 359ae9ea9edSSagi Grimberg 360ae9ea9edSSagi Grimberg isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev, 3615adabdd1SChristoph Hellwig isert_conn->login_rsp_buf, 362ed1083b2SChristoph Hellwig ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE); 363ae9ea9edSSagi Grimberg ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma); 364ae9ea9edSSagi Grimberg if (ret) { 365ae9ea9edSSagi Grimberg isert_err("login_rsp_dma mapping error: %d\n", ret); 366ae9ea9edSSagi Grimberg isert_conn->login_rsp_dma = 0; 3675adabdd1SChristoph Hellwig goto out_free_login_rsp_buf; 368ae9ea9edSSagi Grimberg } 369ae9ea9edSSagi Grimberg 370ae9ea9edSSagi Grimberg return 0; 371ae9ea9edSSagi Grimberg 3725adabdd1SChristoph Hellwig out_free_login_rsp_buf: 3735adabdd1SChristoph Hellwig kfree(isert_conn->login_rsp_buf); 3740b089c1eSSagi Grimberg out_unmap_login_desc: 3750b089c1eSSagi Grimberg ib_dma_unmap_single(ib_dev, isert_conn->login_desc->dma_addr, 3760b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 3770b089c1eSSagi Grimberg out_free_login_desc: 3780b089c1eSSagi Grimberg kfree(isert_conn->login_desc); 379ae9ea9edSSagi Grimberg return ret; 380ae9ea9edSSagi Grimberg } 381ae9ea9edSSagi Grimberg 382422bd0acSJenny Derzhavetz static void 383422bd0acSJenny Derzhavetz isert_set_nego_params(struct isert_conn *isert_conn, 384422bd0acSJenny Derzhavetz struct rdma_conn_param *param) 385422bd0acSJenny Derzhavetz { 386422bd0acSJenny Derzhavetz struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs; 387422bd0acSJenny Derzhavetz 388422bd0acSJenny Derzhavetz /* Set max inflight RDMA READ requests */ 389422bd0acSJenny Derzhavetz isert_conn->initiator_depth = min_t(u8, param->initiator_depth, 390422bd0acSJenny Derzhavetz attr->max_qp_init_rd_atom); 391422bd0acSJenny Derzhavetz isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth); 392422bd0acSJenny Derzhavetz 393422bd0acSJenny Derzhavetz if (param->private_data) { 394422bd0acSJenny Derzhavetz u8 flags = *(u8 *)param->private_data; 395422bd0acSJenny Derzhavetz 396422bd0acSJenny Derzhavetz /* 397422bd0acSJenny Derzhavetz * use remote invalidation if the both initiator 398422bd0acSJenny Derzhavetz * and the HCA support it 399422bd0acSJenny Derzhavetz */ 400422bd0acSJenny Derzhavetz isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) && 401422bd0acSJenny Derzhavetz (attr->device_cap_flags & 402422bd0acSJenny Derzhavetz IB_DEVICE_MEM_MGT_EXTENSIONS); 403422bd0acSJenny Derzhavetz if (isert_conn->snd_w_inv) 404422bd0acSJenny Derzhavetz isert_info("Using remote invalidation\n"); 405422bd0acSJenny Derzhavetz } 406422bd0acSJenny Derzhavetz } 407422bd0acSJenny Derzhavetz 408c6e66307SYamin Friedman static void 409c6e66307SYamin Friedman isert_destroy_qp(struct isert_conn *isert_conn) 410c6e66307SYamin Friedman { 411c6e66307SYamin Friedman ib_destroy_qp(isert_conn->qp); 412c6e66307SYamin Friedman ib_cq_pool_put(isert_conn->cq, isert_conn->cq_size); 413c6e66307SYamin Friedman } 414c6e66307SYamin Friedman 415b8d26b3bSNicholas Bellinger static int 416b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event) 417b8d26b3bSNicholas Bellinger { 418ca6c1d82SSagi Grimberg struct isert_np *isert_np = cma_id->context; 419ca6c1d82SSagi Grimberg struct iscsi_np *np = isert_np->np; 420b8d26b3bSNicholas Bellinger struct isert_conn *isert_conn; 421b8d26b3bSNicholas Bellinger struct isert_device *device; 422b8d26b3bSNicholas Bellinger int ret = 0; 42314f4b54fSSagi Grimberg 42414f4b54fSSagi Grimberg spin_lock_bh(&np->np_thread_lock); 42514f4b54fSSagi Grimberg if (!np->enabled) { 42614f4b54fSSagi Grimberg spin_unlock_bh(&np->np_thread_lock); 42724f412ddSSagi Grimberg isert_dbg("iscsi_np is not enabled, reject connect request\n"); 4288094ba0aSLeon Romanovsky return rdma_reject(cma_id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED); 42914f4b54fSSagi Grimberg } 43014f4b54fSSagi Grimberg spin_unlock_bh(&np->np_thread_lock); 431b8d26b3bSNicholas Bellinger 4324c22e07fSSagi Grimberg isert_dbg("cma_id: %p, portal: %p\n", 433b8d26b3bSNicholas Bellinger cma_id, cma_id->context); 434b8d26b3bSNicholas Bellinger 435b8d26b3bSNicholas Bellinger isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL); 436ae9ea9edSSagi Grimberg if (!isert_conn) 437b8d26b3bSNicholas Bellinger return -ENOMEM; 438b8d26b3bSNicholas Bellinger 439ae9ea9edSSagi Grimberg isert_init_conn(isert_conn); 440dac6ab30SSagi Grimberg isert_conn->cm_id = cma_id; 441b8d26b3bSNicholas Bellinger 442cf8ae958SSagi Grimberg device = isert_device_get(cma_id); 443b8d26b3bSNicholas Bellinger if (IS_ERR(device)) { 444b8d26b3bSNicholas Bellinger ret = PTR_ERR(device); 445adb76a52SLv Yunlong goto out; 446b8d26b3bSNicholas Bellinger } 447dac6ab30SSagi Grimberg isert_conn->device = device; 448b8d26b3bSNicholas Bellinger 449adb76a52SLv Yunlong ret = isert_alloc_login_buf(isert_conn, cma_id->device); 450adb76a52SLv Yunlong if (ret) 451adb76a52SLv Yunlong goto out_conn_dev; 452adb76a52SLv Yunlong 453422bd0acSJenny Derzhavetz isert_set_nego_params(isert_conn, &event->param.conn); 4541a92e17eSSagi Grimberg 455c6e66307SYamin Friedman isert_conn->qp = isert_create_qp(isert_conn, cma_id); 456c6e66307SYamin Friedman if (IS_ERR(isert_conn->qp)) { 457c6e66307SYamin Friedman ret = PTR_ERR(isert_conn->qp); 458adb76a52SLv Yunlong goto out_rsp_dma_map; 459c6e66307SYamin Friedman } 460b8d26b3bSNicholas Bellinger 4619679cc51SChristoph Hellwig ret = isert_login_post_recv(isert_conn); 4622371e5daSSagi Grimberg if (ret) 463c6e66307SYamin Friedman goto out_destroy_qp; 4642371e5daSSagi Grimberg 4652371e5daSSagi Grimberg ret = isert_rdma_accept(isert_conn); 4662371e5daSSagi Grimberg if (ret) 467c6e66307SYamin Friedman goto out_destroy_qp; 4682371e5daSSagi Grimberg 469ed8cb0a4SJenny Derzhavetz mutex_lock(&isert_np->mutex); 470bd379220SJenny Derzhavetz list_add_tail(&isert_conn->node, &isert_np->accepted); 471ed8cb0a4SJenny Derzhavetz mutex_unlock(&isert_np->mutex); 472b8d26b3bSNicholas Bellinger 473b8d26b3bSNicholas Bellinger return 0; 474b8d26b3bSNicholas Bellinger 475c6e66307SYamin Friedman out_destroy_qp: 476c6e66307SYamin Friedman isert_destroy_qp(isert_conn); 477b8d26b3bSNicholas Bellinger out_rsp_dma_map: 478ae9ea9edSSagi Grimberg isert_free_login_buf(isert_conn); 479adb76a52SLv Yunlong out_conn_dev: 480adb76a52SLv Yunlong isert_device_put(device); 481b8d26b3bSNicholas Bellinger out: 482b8d26b3bSNicholas Bellinger kfree(isert_conn); 4838094ba0aSLeon Romanovsky rdma_reject(cma_id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED); 484b8d26b3bSNicholas Bellinger return ret; 485b8d26b3bSNicholas Bellinger } 486b8d26b3bSNicholas Bellinger 487b8d26b3bSNicholas Bellinger static void 488b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn) 489b8d26b3bSNicholas Bellinger { 490dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 491b8d26b3bSNicholas Bellinger 4924c22e07fSSagi Grimberg isert_dbg("conn %p\n", isert_conn); 493b8d26b3bSNicholas Bellinger 49457df81e3SSagi Grimberg BUG_ON(!device); 49557df81e3SSagi Grimberg 49619e2090fSSagi Grimberg isert_free_rx_descriptors(isert_conn); 49763b268d2SRaju Rangoju if (isert_conn->cm_id && 49863b268d2SRaju Rangoju !isert_conn->dev_removed) 499dac6ab30SSagi Grimberg rdma_destroy_id(isert_conn->cm_id); 50019e2090fSSagi Grimberg 501c6e66307SYamin Friedman if (isert_conn->qp) 502c6e66307SYamin Friedman isert_destroy_qp(isert_conn); 503b8d26b3bSNicholas Bellinger 5040b089c1eSSagi Grimberg if (isert_conn->login_desc) 505ae9ea9edSSagi Grimberg isert_free_login_buf(isert_conn); 506ae9ea9edSSagi Grimberg 507cf8ae958SSagi Grimberg isert_device_put(device); 50857df81e3SSagi Grimberg 50963b268d2SRaju Rangoju if (isert_conn->dev_removed) 51063b268d2SRaju Rangoju wake_up_interruptible(&isert_conn->rem_wait); 51163b268d2SRaju Rangoju else 51257df81e3SSagi Grimberg kfree(isert_conn); 513b8d26b3bSNicholas Bellinger } 514b8d26b3bSNicholas Bellinger 515b8d26b3bSNicholas Bellinger static void 516b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id) 517b8d26b3bSNicholas Bellinger { 51819e2090fSSagi Grimberg struct isert_conn *isert_conn = cma_id->qp->qp_context; 519bd379220SJenny Derzhavetz struct isert_np *isert_np = cma_id->context; 520c2f88b17SSagi Grimberg 52124f412ddSSagi Grimberg isert_info("conn %p\n", isert_conn); 522128e9cc8SSagi Grimberg 523dac6ab30SSagi Grimberg mutex_lock(&isert_conn->mutex); 524128e9cc8SSagi Grimberg isert_conn->state = ISER_CONN_UP; 525bd379220SJenny Derzhavetz kref_get(&isert_conn->kref); 526dac6ab30SSagi Grimberg mutex_unlock(&isert_conn->mutex); 527bd379220SJenny Derzhavetz 528bd379220SJenny Derzhavetz mutex_lock(&isert_np->mutex); 529bd379220SJenny Derzhavetz list_move_tail(&isert_conn->node, &isert_np->pending); 530bd379220SJenny Derzhavetz mutex_unlock(&isert_np->mutex); 531bd379220SJenny Derzhavetz 532bd379220SJenny Derzhavetz isert_info("np %p: Allow accept_np to continue\n", isert_np); 533bd379220SJenny Derzhavetz up(&isert_np->sem); 534b8d26b3bSNicholas Bellinger } 535b8d26b3bSNicholas Bellinger 536b8d26b3bSNicholas Bellinger static void 537dac6ab30SSagi Grimberg isert_release_kref(struct kref *kref) 538b8d26b3bSNicholas Bellinger { 539b8d26b3bSNicholas Bellinger struct isert_conn *isert_conn = container_of(kref, 540dac6ab30SSagi Grimberg struct isert_conn, kref); 541b8d26b3bSNicholas Bellinger 5424c22e07fSSagi Grimberg isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm, 5434c22e07fSSagi Grimberg current->pid); 544b8d26b3bSNicholas Bellinger 545b8d26b3bSNicholas Bellinger isert_connect_release(isert_conn); 546b8d26b3bSNicholas Bellinger } 547b8d26b3bSNicholas Bellinger 548b8d26b3bSNicholas Bellinger static void 549b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn) 550b8d26b3bSNicholas Bellinger { 551dac6ab30SSagi Grimberg kref_put(&isert_conn->kref, isert_release_kref); 552b8d26b3bSNicholas Bellinger } 553b8d26b3bSNicholas Bellinger 5546d1fba0cSJenny Derzhavetz static void 5556d1fba0cSJenny Derzhavetz isert_handle_unbound_conn(struct isert_conn *isert_conn) 5566d1fba0cSJenny Derzhavetz { 5576d1fba0cSJenny Derzhavetz struct isert_np *isert_np = isert_conn->cm_id->context; 5586d1fba0cSJenny Derzhavetz 5596d1fba0cSJenny Derzhavetz mutex_lock(&isert_np->mutex); 5606d1fba0cSJenny Derzhavetz if (!list_empty(&isert_conn->node)) { 5616d1fba0cSJenny Derzhavetz /* 5626d1fba0cSJenny Derzhavetz * This means iscsi doesn't know this connection 5636d1fba0cSJenny Derzhavetz * so schedule a cleanup ourselves 5646d1fba0cSJenny Derzhavetz */ 5656d1fba0cSJenny Derzhavetz list_del_init(&isert_conn->node); 5666d1fba0cSJenny Derzhavetz isert_put_conn(isert_conn); 5676d1fba0cSJenny Derzhavetz queue_work(isert_release_wq, &isert_conn->release_work); 5686d1fba0cSJenny Derzhavetz } 5696d1fba0cSJenny Derzhavetz mutex_unlock(&isert_np->mutex); 5706d1fba0cSJenny Derzhavetz } 5716d1fba0cSJenny Derzhavetz 572954f2372SSagi Grimberg /** 573954f2372SSagi Grimberg * isert_conn_terminate() - Initiate connection termination 574954f2372SSagi Grimberg * @isert_conn: isert connection struct 575954f2372SSagi Grimberg * 576954f2372SSagi Grimberg * Notes: 577aea92980SJenny Derzhavetz * In case the connection state is BOUND, move state 578954f2372SSagi Grimberg * to TEMINATING and start teardown sequence (rdma_disconnect). 579128e9cc8SSagi Grimberg * In case the connection state is UP, complete flush as well. 580954f2372SSagi Grimberg * 581dac6ab30SSagi Grimberg * This routine must be called with mutex held. Thus it is 582954f2372SSagi Grimberg * safe to call multiple times. 583954f2372SSagi Grimberg */ 584954f2372SSagi Grimberg static void 585954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn) 586954f2372SSagi Grimberg { 587954f2372SSagi Grimberg int err; 588954f2372SSagi Grimberg 5896d1fba0cSJenny Derzhavetz if (isert_conn->state >= ISER_CONN_TERMINATING) 5906d1fba0cSJenny Derzhavetz return; 5916d1fba0cSJenny Derzhavetz 59224f412ddSSagi Grimberg isert_info("Terminating conn %p state %d\n", 593954f2372SSagi Grimberg isert_conn, isert_conn->state); 594128e9cc8SSagi Grimberg isert_conn->state = ISER_CONN_TERMINATING; 595dac6ab30SSagi Grimberg err = rdma_disconnect(isert_conn->cm_id); 596954f2372SSagi Grimberg if (err) 59724f412ddSSagi Grimberg isert_warn("Failed rdma_disconnect isert_conn %p\n", 598954f2372SSagi Grimberg isert_conn); 599954f2372SSagi Grimberg } 600954f2372SSagi Grimberg 6013b726ae2SSagi Grimberg static int 602ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np, 603ca6c1d82SSagi Grimberg enum rdma_cm_event_type event) 604b8d26b3bSNicholas Bellinger { 605ea8a1616SSagi Grimberg isert_dbg("%s (%d): isert np %p\n", 606ea8a1616SSagi Grimberg rdma_event_msg(event), event, isert_np); 6073b726ae2SSagi Grimberg 608ca6c1d82SSagi Grimberg switch (event) { 609ca6c1d82SSagi Grimberg case RDMA_CM_EVENT_DEVICE_REMOVAL: 610ed8cb0a4SJenny Derzhavetz isert_np->cm_id = NULL; 611ca6c1d82SSagi Grimberg break; 612ca6c1d82SSagi Grimberg case RDMA_CM_EVENT_ADDR_CHANGE: 613ed8cb0a4SJenny Derzhavetz isert_np->cm_id = isert_setup_id(isert_np); 614ed8cb0a4SJenny Derzhavetz if (IS_ERR(isert_np->cm_id)) { 61524f412ddSSagi Grimberg isert_err("isert np %p setup id failed: %ld\n", 616ed8cb0a4SJenny Derzhavetz isert_np, PTR_ERR(isert_np->cm_id)); 617ed8cb0a4SJenny Derzhavetz isert_np->cm_id = NULL; 618ca6c1d82SSagi Grimberg } 619ca6c1d82SSagi Grimberg break; 620ca6c1d82SSagi Grimberg default: 62124f412ddSSagi Grimberg isert_err("isert np %p Unexpected event %d\n", 622ca6c1d82SSagi Grimberg isert_np, event); 623ca6c1d82SSagi Grimberg } 624ca6c1d82SSagi Grimberg 6253b726ae2SSagi Grimberg return -1; 6263b726ae2SSagi Grimberg } 6273b726ae2SSagi Grimberg 628ca6c1d82SSagi Grimberg static int 629ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id, 630ca6c1d82SSagi Grimberg enum rdma_cm_event_type event) 631ca6c1d82SSagi Grimberg { 632f81bf458SJenny Derzhavetz struct isert_conn *isert_conn = cma_id->qp->qp_context; 633ca6c1d82SSagi Grimberg 634dac6ab30SSagi Grimberg mutex_lock(&isert_conn->mutex); 6356d1fba0cSJenny Derzhavetz switch (isert_conn->state) { 6366d1fba0cSJenny Derzhavetz case ISER_CONN_TERMINATING: 6376d1fba0cSJenny Derzhavetz break; 6386d1fba0cSJenny Derzhavetz case ISER_CONN_UP: 639128e9cc8SSagi Grimberg isert_conn_terminate(isert_conn); 640572a1434SSagi Grimberg ib_drain_qp(isert_conn->qp); 6416d1fba0cSJenny Derzhavetz isert_handle_unbound_conn(isert_conn); 6426d1fba0cSJenny Derzhavetz break; 6436d1fba0cSJenny Derzhavetz case ISER_CONN_BOUND: 6446d1fba0cSJenny Derzhavetz case ISER_CONN_FULL_FEATURE: /* FALLTHRU */ 6456d1fba0cSJenny Derzhavetz iscsit_cause_connection_reinstatement(isert_conn->conn, 0); 6466d1fba0cSJenny Derzhavetz break; 6476d1fba0cSJenny Derzhavetz default: 648506f71d1SColin Ian King isert_warn("conn %p terminating in state %d\n", 6496d1fba0cSJenny Derzhavetz isert_conn, isert_conn->state); 6506d1fba0cSJenny Derzhavetz } 651dac6ab30SSagi Grimberg mutex_unlock(&isert_conn->mutex); 652128e9cc8SSagi Grimberg 6533b726ae2SSagi Grimberg return 0; 654b8d26b3bSNicholas Bellinger } 655b8d26b3bSNicholas Bellinger 6564a579da2SSagi Grimberg static int 657954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id) 658954f2372SSagi Grimberg { 65919e2090fSSagi Grimberg struct isert_conn *isert_conn = cma_id->qp->qp_context; 660954f2372SSagi Grimberg 661cd52cb26SSagi Grimberg ib_drain_qp(isert_conn->qp); 662bd379220SJenny Derzhavetz list_del_init(&isert_conn->node); 663dac6ab30SSagi Grimberg isert_conn->cm_id = NULL; 664954f2372SSagi Grimberg isert_put_conn(isert_conn); 6654a579da2SSagi Grimberg 6664a579da2SSagi Grimberg return -1; 667954f2372SSagi Grimberg } 668954f2372SSagi Grimberg 669b8d26b3bSNicholas Bellinger static int 670b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event) 671b8d26b3bSNicholas Bellinger { 672f81bf458SJenny Derzhavetz struct isert_np *isert_np = cma_id->context; 67363b268d2SRaju Rangoju struct isert_conn *isert_conn; 674b8d26b3bSNicholas Bellinger int ret = 0; 675b8d26b3bSNicholas Bellinger 676ea8a1616SSagi Grimberg isert_info("%s (%d): status %d id %p np %p\n", 677ea8a1616SSagi Grimberg rdma_event_msg(event->event), event->event, 6784c22e07fSSagi Grimberg event->status, cma_id, cma_id->context); 679b8d26b3bSNicholas Bellinger 680f81bf458SJenny Derzhavetz if (isert_np->cm_id == cma_id) 681f81bf458SJenny Derzhavetz return isert_np_cma_handler(cma_id->context, event->event); 682f81bf458SJenny Derzhavetz 683b8d26b3bSNicholas Bellinger switch (event->event) { 684b8d26b3bSNicholas Bellinger case RDMA_CM_EVENT_CONNECT_REQUEST: 685b8d26b3bSNicholas Bellinger ret = isert_connect_request(cma_id, event); 6863b726ae2SSagi Grimberg if (ret) 6874c22e07fSSagi Grimberg isert_err("failed handle connect request %d\n", ret); 688b8d26b3bSNicholas Bellinger break; 689b8d26b3bSNicholas Bellinger case RDMA_CM_EVENT_ESTABLISHED: 690b8d26b3bSNicholas Bellinger isert_connected_handler(cma_id); 691b8d26b3bSNicholas Bellinger break; 692df561f66SGustavo A. R. Silva case RDMA_CM_EVENT_ADDR_CHANGE: 693df561f66SGustavo A. R. Silva case RDMA_CM_EVENT_DISCONNECTED: 69488c4015fSSagi Grimberg case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */ 695ca6c1d82SSagi Grimberg ret = isert_disconnected_handler(cma_id, event->event); 696b8d26b3bSNicholas Bellinger break; 69763b268d2SRaju Rangoju case RDMA_CM_EVENT_DEVICE_REMOVAL: 69863b268d2SRaju Rangoju isert_conn = cma_id->qp->qp_context; 69963b268d2SRaju Rangoju isert_conn->dev_removed = true; 70063b268d2SRaju Rangoju isert_disconnected_handler(cma_id, event->event); 70163b268d2SRaju Rangoju wait_event_interruptible(isert_conn->rem_wait, 70263b268d2SRaju Rangoju isert_conn->state == ISER_CONN_DOWN); 70363b268d2SRaju Rangoju kfree(isert_conn); 70463b268d2SRaju Rangoju /* 70563b268d2SRaju Rangoju * return non-zero from the callback to destroy 70663b268d2SRaju Rangoju * the rdma cm id 70763b268d2SRaju Rangoju */ 70863b268d2SRaju Rangoju return 1; 709d2271bd0SBart Van Assche case RDMA_CM_EVENT_REJECTED: 7101e38a366SSteve Wise isert_info("Connection rejected: %s\n", 7111e38a366SSteve Wise rdma_reject_msg(cma_id, event->status)); 712df561f66SGustavo A. R. Silva fallthrough; 713d2271bd0SBart Van Assche case RDMA_CM_EVENT_UNREACHABLE: 714b8d26b3bSNicholas Bellinger case RDMA_CM_EVENT_CONNECT_ERROR: 7154a579da2SSagi Grimberg ret = isert_connect_error(cma_id); 716954f2372SSagi Grimberg break; 717b8d26b3bSNicholas Bellinger default: 71824f412ddSSagi Grimberg isert_err("Unhandled RDMA CMA event: %d\n", event->event); 719b8d26b3bSNicholas Bellinger break; 720b8d26b3bSNicholas Bellinger } 721b8d26b3bSNicholas Bellinger 722b8d26b3bSNicholas Bellinger return ret; 723b8d26b3bSNicholas Bellinger } 724b8d26b3bSNicholas Bellinger 725b8d26b3bSNicholas Bellinger static int 7264366b19cSJenny Derzhavetz isert_post_recvm(struct isert_conn *isert_conn, u32 count) 727b8d26b3bSNicholas Bellinger { 728e01a7674SBart Van Assche struct ib_recv_wr *rx_wr; 729b8d26b3bSNicholas Bellinger int i, ret; 730b8d26b3bSNicholas Bellinger struct iser_rx_desc *rx_desc; 731b8d26b3bSNicholas Bellinger 732dac6ab30SSagi Grimberg for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) { 7334366b19cSJenny Derzhavetz rx_desc = &isert_conn->rx_descs[i]; 7349679cc51SChristoph Hellwig 7359679cc51SChristoph Hellwig rx_wr->wr_cqe = &rx_desc->rx_cqe; 736b8d26b3bSNicholas Bellinger rx_wr->sg_list = &rx_desc->rx_sg; 737b8d26b3bSNicholas Bellinger rx_wr->num_sge = 1; 738b8d26b3bSNicholas Bellinger rx_wr->next = rx_wr + 1; 7397a56dc88SSagi Grimberg rx_desc->in_use = false; 740b8d26b3bSNicholas Bellinger } 741b8d26b3bSNicholas Bellinger rx_wr--; 742b8d26b3bSNicholas Bellinger rx_wr->next = NULL; /* mark end of work requests list */ 743b8d26b3bSNicholas Bellinger 744e01a7674SBart Van Assche ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr, NULL); 7456d1fba0cSJenny Derzhavetz if (ret) 74624f412ddSSagi Grimberg isert_err("ib_post_recv() failed with ret: %d\n", ret); 7474366b19cSJenny Derzhavetz 7484366b19cSJenny Derzhavetz return ret; 7494366b19cSJenny Derzhavetz } 7504366b19cSJenny Derzhavetz 7514366b19cSJenny Derzhavetz static int 7524366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc) 7534366b19cSJenny Derzhavetz { 754e01a7674SBart Van Assche struct ib_recv_wr rx_wr; 7554366b19cSJenny Derzhavetz int ret; 7564366b19cSJenny Derzhavetz 7577a56dc88SSagi Grimberg if (!rx_desc->in_use) { 7587a56dc88SSagi Grimberg /* 7597a56dc88SSagi Grimberg * if the descriptor is not in-use we already reposted it 7607a56dc88SSagi Grimberg * for recv, so just silently return 7617a56dc88SSagi Grimberg */ 7627a56dc88SSagi Grimberg return 0; 7637a56dc88SSagi Grimberg } 7647a56dc88SSagi Grimberg 7657a56dc88SSagi Grimberg rx_desc->in_use = false; 7669679cc51SChristoph Hellwig rx_wr.wr_cqe = &rx_desc->rx_cqe; 7674366b19cSJenny Derzhavetz rx_wr.sg_list = &rx_desc->rx_sg; 7684366b19cSJenny Derzhavetz rx_wr.num_sge = 1; 7694366b19cSJenny Derzhavetz rx_wr.next = NULL; 7704366b19cSJenny Derzhavetz 771e01a7674SBart Van Assche ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL); 7726d1fba0cSJenny Derzhavetz if (ret) 7734366b19cSJenny Derzhavetz isert_err("ib_post_recv() failed with ret: %d\n", ret); 7744366b19cSJenny Derzhavetz 775b8d26b3bSNicholas Bellinger return ret; 776b8d26b3bSNicholas Bellinger } 777b8d26b3bSNicholas Bellinger 778b8d26b3bSNicholas Bellinger static int 7799679cc51SChristoph Hellwig isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc) 780b8d26b3bSNicholas Bellinger { 781dac6ab30SSagi Grimberg struct ib_device *ib_dev = isert_conn->cm_id->device; 782e01a7674SBart Van Assche struct ib_send_wr send_wr; 783b8d26b3bSNicholas Bellinger int ret; 784b8d26b3bSNicholas Bellinger 785b8d26b3bSNicholas Bellinger ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr, 786b8d26b3bSNicholas Bellinger ISER_HEADERS_LEN, DMA_TO_DEVICE); 787b8d26b3bSNicholas Bellinger 7889679cc51SChristoph Hellwig tx_desc->tx_cqe.done = isert_login_send_done; 7899679cc51SChristoph Hellwig 790b8d26b3bSNicholas Bellinger send_wr.next = NULL; 7919679cc51SChristoph Hellwig send_wr.wr_cqe = &tx_desc->tx_cqe; 792b8d26b3bSNicholas Bellinger send_wr.sg_list = tx_desc->tx_sg; 793b8d26b3bSNicholas Bellinger send_wr.num_sge = tx_desc->num_sge; 794b8d26b3bSNicholas Bellinger send_wr.opcode = IB_WR_SEND; 795b8d26b3bSNicholas Bellinger send_wr.send_flags = IB_SEND_SIGNALED; 796b8d26b3bSNicholas Bellinger 797e01a7674SBart Van Assche ret = ib_post_send(isert_conn->qp, &send_wr, NULL); 798bdf20e72SSagi Grimberg if (ret) 79924f412ddSSagi Grimberg isert_err("ib_post_send() failed, ret: %d\n", ret); 800b8d26b3bSNicholas Bellinger 801b8d26b3bSNicholas Bellinger return ret; 802b8d26b3bSNicholas Bellinger } 803b8d26b3bSNicholas Bellinger 804b8d26b3bSNicholas Bellinger static void 805763b6965SAlex Estrin __isert_create_send_desc(struct isert_device *device, 806b8d26b3bSNicholas Bellinger struct iser_tx_desc *tx_desc) 807b8d26b3bSNicholas Bellinger { 808b8d26b3bSNicholas Bellinger 809d3cf81f9SSagi Grimberg memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl)); 810d3cf81f9SSagi Grimberg tx_desc->iser_header.flags = ISCSI_CTRL; 811b8d26b3bSNicholas Bellinger 812b8d26b3bSNicholas Bellinger tx_desc->num_sge = 1; 813b8d26b3bSNicholas Bellinger 81434efc7dfSJason Gunthorpe if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) { 81534efc7dfSJason Gunthorpe tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey; 81624f412ddSSagi Grimberg isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc); 817b8d26b3bSNicholas Bellinger } 818b8d26b3bSNicholas Bellinger } 819b8d26b3bSNicholas Bellinger 820763b6965SAlex Estrin static void 821763b6965SAlex Estrin isert_create_send_desc(struct isert_conn *isert_conn, 822763b6965SAlex Estrin struct isert_cmd *isert_cmd, 823763b6965SAlex Estrin struct iser_tx_desc *tx_desc) 824763b6965SAlex Estrin { 825763b6965SAlex Estrin struct isert_device *device = isert_conn->device; 826763b6965SAlex Estrin struct ib_device *ib_dev = device->ib_device; 827763b6965SAlex Estrin 828763b6965SAlex Estrin ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr, 829763b6965SAlex Estrin ISER_HEADERS_LEN, DMA_TO_DEVICE); 830763b6965SAlex Estrin 831763b6965SAlex Estrin __isert_create_send_desc(device, tx_desc); 832763b6965SAlex Estrin } 833763b6965SAlex Estrin 834b8d26b3bSNicholas Bellinger static int 835b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn, 836b8d26b3bSNicholas Bellinger struct iser_tx_desc *tx_desc) 837b8d26b3bSNicholas Bellinger { 838dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 83967cb3949SSagi Grimberg struct ib_device *ib_dev = device->ib_device; 840b8d26b3bSNicholas Bellinger u64 dma_addr; 841b8d26b3bSNicholas Bellinger 842b8d26b3bSNicholas Bellinger dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc, 843b8d26b3bSNicholas Bellinger ISER_HEADERS_LEN, DMA_TO_DEVICE); 844b8d26b3bSNicholas Bellinger if (ib_dma_mapping_error(ib_dev, dma_addr)) { 84524f412ddSSagi Grimberg isert_err("ib_dma_mapping_error() failed\n"); 846b8d26b3bSNicholas Bellinger return -ENOMEM; 847b8d26b3bSNicholas Bellinger } 848b8d26b3bSNicholas Bellinger 849b8d26b3bSNicholas Bellinger tx_desc->dma_addr = dma_addr; 850b8d26b3bSNicholas Bellinger tx_desc->tx_sg[0].addr = tx_desc->dma_addr; 851b8d26b3bSNicholas Bellinger tx_desc->tx_sg[0].length = ISER_HEADERS_LEN; 85234efc7dfSJason Gunthorpe tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey; 853b8d26b3bSNicholas Bellinger 8544c22e07fSSagi Grimberg isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n", 8554c22e07fSSagi Grimberg tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length, 8564c22e07fSSagi Grimberg tx_desc->tx_sg[0].lkey); 857b8d26b3bSNicholas Bellinger 858b8d26b3bSNicholas Bellinger return 0; 859b8d26b3bSNicholas Bellinger } 860b8d26b3bSNicholas Bellinger 861b8d26b3bSNicholas Bellinger static void 86295b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd, 86368a86deeSSagi Grimberg struct ib_send_wr *send_wr) 864b8d26b3bSNicholas Bellinger { 86595b60f07SNicholas Bellinger struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc; 86695b60f07SNicholas Bellinger 8679679cc51SChristoph Hellwig tx_desc->tx_cqe.done = isert_send_done; 8689679cc51SChristoph Hellwig send_wr->wr_cqe = &tx_desc->tx_cqe; 869422bd0acSJenny Derzhavetz 870422bd0acSJenny Derzhavetz if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) { 871422bd0acSJenny Derzhavetz send_wr->opcode = IB_WR_SEND_WITH_INV; 872422bd0acSJenny Derzhavetz send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey; 873422bd0acSJenny Derzhavetz } else { 874b8d26b3bSNicholas Bellinger send_wr->opcode = IB_WR_SEND; 875422bd0acSJenny Derzhavetz } 876422bd0acSJenny Derzhavetz 87795b60f07SNicholas Bellinger send_wr->sg_list = &tx_desc->tx_sg[0]; 878b8d26b3bSNicholas Bellinger send_wr->num_sge = isert_cmd->tx_desc.num_sge; 87995b60f07SNicholas Bellinger send_wr->send_flags = IB_SEND_SIGNALED; 880b8d26b3bSNicholas Bellinger } 881b8d26b3bSNicholas Bellinger 882b8d26b3bSNicholas Bellinger static int 8839679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn) 884b8d26b3bSNicholas Bellinger { 885e01a7674SBart Van Assche struct ib_recv_wr rx_wr; 886b8d26b3bSNicholas Bellinger struct ib_sge sge; 887b8d26b3bSNicholas Bellinger int ret; 888b8d26b3bSNicholas Bellinger 889b8d26b3bSNicholas Bellinger memset(&sge, 0, sizeof(struct ib_sge)); 8900b089c1eSSagi Grimberg sge.addr = isert_conn->login_desc->dma_addr + 8910b089c1eSSagi Grimberg isert_get_hdr_offset(isert_conn->login_desc); 892ed1083b2SChristoph Hellwig sge.length = ISER_RX_PAYLOAD_SIZE; 89334efc7dfSJason Gunthorpe sge.lkey = isert_conn->device->pd->local_dma_lkey; 894b8d26b3bSNicholas Bellinger 89524f412ddSSagi Grimberg isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n", 896b8d26b3bSNicholas Bellinger sge.addr, sge.length, sge.lkey); 897b8d26b3bSNicholas Bellinger 8980b089c1eSSagi Grimberg isert_conn->login_desc->rx_cqe.done = isert_login_recv_done; 8999679cc51SChristoph Hellwig 900b8d26b3bSNicholas Bellinger memset(&rx_wr, 0, sizeof(struct ib_recv_wr)); 9010b089c1eSSagi Grimberg rx_wr.wr_cqe = &isert_conn->login_desc->rx_cqe; 902b8d26b3bSNicholas Bellinger rx_wr.sg_list = &sge; 903b8d26b3bSNicholas Bellinger rx_wr.num_sge = 1; 904b8d26b3bSNicholas Bellinger 905e01a7674SBart Van Assche ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL); 9066d1fba0cSJenny Derzhavetz if (ret) 90724f412ddSSagi Grimberg isert_err("ib_post_recv() failed: %d\n", ret); 908b8d26b3bSNicholas Bellinger 909b8d26b3bSNicholas Bellinger return ret; 910b8d26b3bSNicholas Bellinger } 911b8d26b3bSNicholas Bellinger 912b8d26b3bSNicholas Bellinger static int 913be36d683SMax Gurtovoy isert_put_login_tx(struct iscsit_conn *conn, struct iscsi_login *login, 914b8d26b3bSNicholas Bellinger u32 length) 915b8d26b3bSNicholas Bellinger { 916b8d26b3bSNicholas Bellinger struct isert_conn *isert_conn = conn->context; 917dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 91867cb3949SSagi Grimberg struct ib_device *ib_dev = device->ib_device; 919dac6ab30SSagi Grimberg struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc; 920b8d26b3bSNicholas Bellinger int ret; 921b8d26b3bSNicholas Bellinger 922763b6965SAlex Estrin __isert_create_send_desc(device, tx_desc); 923b8d26b3bSNicholas Bellinger 924b8d26b3bSNicholas Bellinger memcpy(&tx_desc->iscsi_header, &login->rsp[0], 925b8d26b3bSNicholas Bellinger sizeof(struct iscsi_hdr)); 926b8d26b3bSNicholas Bellinger 927b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(isert_conn, tx_desc); 928b8d26b3bSNicholas Bellinger 929b8d26b3bSNicholas Bellinger if (length > 0) { 930b8d26b3bSNicholas Bellinger struct ib_sge *tx_dsg = &tx_desc->tx_sg[1]; 931b8d26b3bSNicholas Bellinger 932b8d26b3bSNicholas Bellinger ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma, 933b8d26b3bSNicholas Bellinger length, DMA_TO_DEVICE); 934b8d26b3bSNicholas Bellinger 935b8d26b3bSNicholas Bellinger memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length); 936b8d26b3bSNicholas Bellinger 937b8d26b3bSNicholas Bellinger ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma, 938b8d26b3bSNicholas Bellinger length, DMA_TO_DEVICE); 939b8d26b3bSNicholas Bellinger 940b8d26b3bSNicholas Bellinger tx_dsg->addr = isert_conn->login_rsp_dma; 941b8d26b3bSNicholas Bellinger tx_dsg->length = length; 94234efc7dfSJason Gunthorpe tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey; 943b8d26b3bSNicholas Bellinger tx_desc->num_sge = 2; 944b8d26b3bSNicholas Bellinger } 945b8d26b3bSNicholas Bellinger if (!login->login_failed) { 946b8d26b3bSNicholas Bellinger if (login->login_complete) { 947b8d26b3bSNicholas Bellinger ret = isert_alloc_rx_descriptors(isert_conn); 948b8d26b3bSNicholas Bellinger if (ret) 949b8d26b3bSNicholas Bellinger return ret; 950b8d26b3bSNicholas Bellinger 9514366b19cSJenny Derzhavetz ret = isert_post_recvm(isert_conn, 9524366b19cSJenny Derzhavetz ISERT_QP_MAX_RECV_DTOS); 953b8d26b3bSNicholas Bellinger if (ret) 954b8d26b3bSNicholas Bellinger return ret; 955b8d26b3bSNicholas Bellinger 956128e9cc8SSagi Grimberg /* Now we are in FULL_FEATURE phase */ 957dac6ab30SSagi Grimberg mutex_lock(&isert_conn->mutex); 958128e9cc8SSagi Grimberg isert_conn->state = ISER_CONN_FULL_FEATURE; 959dac6ab30SSagi Grimberg mutex_unlock(&isert_conn->mutex); 960b8d26b3bSNicholas Bellinger goto post_send; 961b8d26b3bSNicholas Bellinger } 962b8d26b3bSNicholas Bellinger 9639679cc51SChristoph Hellwig ret = isert_login_post_recv(isert_conn); 964b8d26b3bSNicholas Bellinger if (ret) 965b8d26b3bSNicholas Bellinger return ret; 966b8d26b3bSNicholas Bellinger } 967b8d26b3bSNicholas Bellinger post_send: 9689679cc51SChristoph Hellwig ret = isert_login_post_send(isert_conn, tx_desc); 969b8d26b3bSNicholas Bellinger if (ret) 970b8d26b3bSNicholas Bellinger return ret; 971b8d26b3bSNicholas Bellinger 972b8d26b3bSNicholas Bellinger return 0; 973b8d26b3bSNicholas Bellinger } 974b8d26b3bSNicholas Bellinger 975b8d26b3bSNicholas Bellinger static void 9762371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn) 977b8d26b3bSNicholas Bellinger { 9780b089c1eSSagi Grimberg struct iser_rx_desc *rx_desc = isert_conn->login_desc; 9792371e5daSSagi Grimberg int rx_buflen = isert_conn->login_req_len; 980be36d683SMax Gurtovoy struct iscsit_conn *conn = isert_conn->conn; 981b8d26b3bSNicholas Bellinger struct iscsi_login *login = conn->conn_login; 982b8d26b3bSNicholas Bellinger int size; 983b8d26b3bSNicholas Bellinger 98424f412ddSSagi Grimberg isert_info("conn %p\n", isert_conn); 9852371e5daSSagi Grimberg 9862371e5daSSagi Grimberg WARN_ON_ONCE(!login); 987b8d26b3bSNicholas Bellinger 988b8d26b3bSNicholas Bellinger if (login->first_request) { 989b8d26b3bSNicholas Bellinger struct iscsi_login_req *login_req = 9900b089c1eSSagi Grimberg (struct iscsi_login_req *)isert_get_iscsi_hdr(rx_desc); 991b8d26b3bSNicholas Bellinger /* 992b8d26b3bSNicholas Bellinger * Setup the initial iscsi_login values from the leading 993b8d26b3bSNicholas Bellinger * login request PDU. 994b8d26b3bSNicholas Bellinger */ 995b8d26b3bSNicholas Bellinger login->leading_connection = (!login_req->tsih) ? 1 : 0; 9962a402120SMaurizio Lombardi login->current_stage = ISCSI_LOGIN_CURRENT_STAGE( 9972a402120SMaurizio Lombardi login_req->flags); 998b8d26b3bSNicholas Bellinger login->version_min = login_req->min_version; 999b8d26b3bSNicholas Bellinger login->version_max = login_req->max_version; 1000b8d26b3bSNicholas Bellinger memcpy(login->isid, login_req->isid, 6); 1001b8d26b3bSNicholas Bellinger login->cmd_sn = be32_to_cpu(login_req->cmdsn); 1002b8d26b3bSNicholas Bellinger login->init_task_tag = login_req->itt; 1003b8d26b3bSNicholas Bellinger login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn); 1004b8d26b3bSNicholas Bellinger login->cid = be16_to_cpu(login_req->cid); 1005b8d26b3bSNicholas Bellinger login->tsih = be16_to_cpu(login_req->tsih); 1006b8d26b3bSNicholas Bellinger } 1007b8d26b3bSNicholas Bellinger 10080b089c1eSSagi Grimberg memcpy(&login->req[0], isert_get_iscsi_hdr(rx_desc), ISCSI_HDR_LEN); 1009b8d26b3bSNicholas Bellinger 1010b8d26b3bSNicholas Bellinger size = min(rx_buflen, MAX_KEY_VALUE_PAIRS); 10114c22e07fSSagi Grimberg isert_dbg("Using login payload size: %d, rx_buflen: %d " 10124c22e07fSSagi Grimberg "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen, 10134c22e07fSSagi Grimberg MAX_KEY_VALUE_PAIRS); 10140b089c1eSSagi Grimberg memcpy(login->req_buf, isert_get_data(rx_desc), size); 1015b8d26b3bSNicholas Bellinger 10166faaa85fSNicholas Bellinger if (login->first_request) { 1017dac6ab30SSagi Grimberg complete(&isert_conn->login_comp); 10186faaa85fSNicholas Bellinger return; 10196faaa85fSNicholas Bellinger } 1020549f39a5STetsuo Handa queue_delayed_work(isert_login_wq, &conn->login_work, 0); 1021b8d26b3bSNicholas Bellinger } 1022b8d26b3bSNicholas Bellinger 102366cd9d4eSMax Gurtovoy static struct iscsit_cmd 1024be36d683SMax Gurtovoy *isert_allocate_cmd(struct iscsit_conn *conn, struct iser_rx_desc *rx_desc) 1025b8d26b3bSNicholas Bellinger { 10266700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1027b8d26b3bSNicholas Bellinger struct isert_cmd *isert_cmd; 102866cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd; 1029b8d26b3bSNicholas Bellinger 1030676687c6SNicholas Bellinger cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 1031d703ce2fSNicholas Bellinger if (!cmd) { 103266cd9d4eSMax Gurtovoy isert_err("Unable to allocate iscsit_cmd + isert_cmd\n"); 1033b8d26b3bSNicholas Bellinger return NULL; 1034b8d26b3bSNicholas Bellinger } 1035d703ce2fSNicholas Bellinger isert_cmd = iscsit_priv_cmd(cmd); 1036b8d26b3bSNicholas Bellinger isert_cmd->conn = isert_conn; 103766cd9d4eSMax Gurtovoy isert_cmd->iscsit_cmd = cmd; 10384366b19cSJenny Derzhavetz isert_cmd->rx_desc = rx_desc; 1039b8d26b3bSNicholas Bellinger 1040d703ce2fSNicholas Bellinger return cmd; 1041b8d26b3bSNicholas Bellinger } 1042b8d26b3bSNicholas Bellinger 1043b8d26b3bSNicholas Bellinger static int 1044b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn, 104566cd9d4eSMax Gurtovoy struct isert_cmd *isert_cmd, struct iscsit_cmd *cmd, 1046d703ce2fSNicholas Bellinger struct iser_rx_desc *rx_desc, unsigned char *buf) 1047b8d26b3bSNicholas Bellinger { 1048be36d683SMax Gurtovoy struct iscsit_conn *conn = isert_conn->conn; 1049b8d26b3bSNicholas Bellinger struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf; 1050b8d26b3bSNicholas Bellinger int imm_data, imm_data_len, unsol_data, sg_nents, rc; 1051b8d26b3bSNicholas Bellinger bool dump_payload = false; 10529fd60088SJenny Derzhavetz unsigned int data_len; 1053b8d26b3bSNicholas Bellinger 1054b8d26b3bSNicholas Bellinger rc = iscsit_setup_scsi_cmd(conn, cmd, buf); 1055b8d26b3bSNicholas Bellinger if (rc < 0) 1056b8d26b3bSNicholas Bellinger return rc; 1057b8d26b3bSNicholas Bellinger 1058b8d26b3bSNicholas Bellinger imm_data = cmd->immediate_data; 1059b8d26b3bSNicholas Bellinger imm_data_len = cmd->first_burst_len; 1060b8d26b3bSNicholas Bellinger unsol_data = cmd->unsolicited_data; 10619fd60088SJenny Derzhavetz data_len = cmd->se_cmd.data_length; 1062b8d26b3bSNicholas Bellinger 10639fd60088SJenny Derzhavetz if (imm_data && imm_data_len == data_len) 10649fd60088SJenny Derzhavetz cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC; 1065b8d26b3bSNicholas Bellinger rc = iscsit_process_scsi_cmd(conn, cmd, hdr); 1066b8d26b3bSNicholas Bellinger if (rc < 0) { 1067b8d26b3bSNicholas Bellinger return 0; 1068b8d26b3bSNicholas Bellinger } else if (rc > 0) { 1069b8d26b3bSNicholas Bellinger dump_payload = true; 1070b8d26b3bSNicholas Bellinger goto sequence_cmd; 1071b8d26b3bSNicholas Bellinger } 1072b8d26b3bSNicholas Bellinger 1073b8d26b3bSNicholas Bellinger if (!imm_data) 1074b8d26b3bSNicholas Bellinger return 0; 1075b8d26b3bSNicholas Bellinger 10769fd60088SJenny Derzhavetz if (imm_data_len != data_len) { 1077b8d26b3bSNicholas Bellinger sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE)); 10789fd60088SJenny Derzhavetz sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents, 10790b089c1eSSagi Grimberg isert_get_data(rx_desc), imm_data_len); 10809fd60088SJenny Derzhavetz isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n", 10819fd60088SJenny Derzhavetz sg_nents, imm_data_len); 10829fd60088SJenny Derzhavetz } else { 10839fd60088SJenny Derzhavetz sg_init_table(&isert_cmd->sg, 1); 10849fd60088SJenny Derzhavetz cmd->se_cmd.t_data_sg = &isert_cmd->sg; 10859fd60088SJenny Derzhavetz cmd->se_cmd.t_data_nents = 1; 10860b089c1eSSagi Grimberg sg_set_buf(&isert_cmd->sg, isert_get_data(rx_desc), 10870b089c1eSSagi Grimberg imm_data_len); 10889fd60088SJenny Derzhavetz isert_dbg("Transfer Immediate imm_data_len: %d\n", 10899fd60088SJenny Derzhavetz imm_data_len); 10909fd60088SJenny Derzhavetz } 1091b8d26b3bSNicholas Bellinger 1092b8d26b3bSNicholas Bellinger cmd->write_data_done += imm_data_len; 1093b8d26b3bSNicholas Bellinger 1094b8d26b3bSNicholas Bellinger if (cmd->write_data_done == cmd->se_cmd.data_length) { 1095b8d26b3bSNicholas Bellinger spin_lock_bh(&cmd->istate_lock); 1096b8d26b3bSNicholas Bellinger cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; 1097b8d26b3bSNicholas Bellinger cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; 1098b8d26b3bSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock); 1099b8d26b3bSNicholas Bellinger } 1100b8d26b3bSNicholas Bellinger 1101b8d26b3bSNicholas Bellinger sequence_cmd: 1102561bf158SNicholas Bellinger rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn); 1103b8d26b3bSNicholas Bellinger 1104f7a95c90SZou Wei if (!rc && !dump_payload && unsol_data) 11050300b114SBart Van Assche iscsit_set_unsolicited_dataout(cmd); 11066cc44a6fSNicholas Bellinger else if (dump_payload && imm_data) 1107afc16604SBart Van Assche target_put_sess_cmd(&cmd->se_cmd); 1108b8d26b3bSNicholas Bellinger 1109b8d26b3bSNicholas Bellinger return 0; 1110b8d26b3bSNicholas Bellinger } 1111b8d26b3bSNicholas Bellinger 1112b8d26b3bSNicholas Bellinger static int 1113b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn, 1114b8d26b3bSNicholas Bellinger struct iser_rx_desc *rx_desc, unsigned char *buf) 1115b8d26b3bSNicholas Bellinger { 1116b8d26b3bSNicholas Bellinger struct scatterlist *sg_start; 1117be36d683SMax Gurtovoy struct iscsit_conn *conn = isert_conn->conn; 111866cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd = NULL; 1119b8d26b3bSNicholas Bellinger struct iscsi_data *hdr = (struct iscsi_data *)buf; 1120b8d26b3bSNicholas Bellinger u32 unsol_data_len = ntoh24(hdr->dlength); 1121b8d26b3bSNicholas Bellinger int rc, sg_nents, sg_off, page_off; 1122b8d26b3bSNicholas Bellinger 1123b8d26b3bSNicholas Bellinger rc = iscsit_check_dataout_hdr(conn, buf, &cmd); 1124b8d26b3bSNicholas Bellinger if (rc < 0) 1125b8d26b3bSNicholas Bellinger return rc; 1126b8d26b3bSNicholas Bellinger else if (!cmd) 1127b8d26b3bSNicholas Bellinger return 0; 1128b8d26b3bSNicholas Bellinger /* 1129b8d26b3bSNicholas Bellinger * FIXME: Unexpected unsolicited_data out 1130b8d26b3bSNicholas Bellinger */ 1131b8d26b3bSNicholas Bellinger if (!cmd->unsolicited_data) { 113224f412ddSSagi Grimberg isert_err("Received unexpected solicited data payload\n"); 1133b8d26b3bSNicholas Bellinger dump_stack(); 1134b8d26b3bSNicholas Bellinger return -1; 1135b8d26b3bSNicholas Bellinger } 1136b8d26b3bSNicholas Bellinger 11374c22e07fSSagi Grimberg isert_dbg("Unsolicited DataOut unsol_data_len: %u, " 11384c22e07fSSagi Grimberg "write_data_done: %u, data_length: %u\n", 11394c22e07fSSagi Grimberg unsol_data_len, cmd->write_data_done, 11404c22e07fSSagi Grimberg cmd->se_cmd.data_length); 1141b8d26b3bSNicholas Bellinger 1142b8d26b3bSNicholas Bellinger sg_off = cmd->write_data_done / PAGE_SIZE; 1143b8d26b3bSNicholas Bellinger sg_start = &cmd->se_cmd.t_data_sg[sg_off]; 1144b8d26b3bSNicholas Bellinger sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE)); 1145b8d26b3bSNicholas Bellinger page_off = cmd->write_data_done % PAGE_SIZE; 1146b8d26b3bSNicholas Bellinger /* 1147b8d26b3bSNicholas Bellinger * FIXME: Non page-aligned unsolicited_data out 1148b8d26b3bSNicholas Bellinger */ 1149b8d26b3bSNicholas Bellinger if (page_off) { 11504c22e07fSSagi Grimberg isert_err("unexpected non-page aligned data payload\n"); 1151b8d26b3bSNicholas Bellinger dump_stack(); 1152b8d26b3bSNicholas Bellinger return -1; 1153b8d26b3bSNicholas Bellinger } 11544c22e07fSSagi Grimberg isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u " 11554c22e07fSSagi Grimberg "sg_nents: %u from %p %u\n", sg_start, sg_off, 11560b089c1eSSagi Grimberg sg_nents, isert_get_data(rx_desc), unsol_data_len); 1157b8d26b3bSNicholas Bellinger 11580b089c1eSSagi Grimberg sg_copy_from_buffer(sg_start, sg_nents, isert_get_data(rx_desc), 1159b8d26b3bSNicholas Bellinger unsol_data_len); 1160b8d26b3bSNicholas Bellinger 1161b8d26b3bSNicholas Bellinger rc = iscsit_check_dataout_payload(cmd, hdr, false); 1162b8d26b3bSNicholas Bellinger if (rc < 0) 1163b8d26b3bSNicholas Bellinger return rc; 1164b8d26b3bSNicholas Bellinger 11654366b19cSJenny Derzhavetz /* 11664366b19cSJenny Derzhavetz * multiple data-outs on the same command can arrive - 11674366b19cSJenny Derzhavetz * so post the buffer before hand 11684366b19cSJenny Derzhavetz */ 1169c97119b6SMax Gurtovoy return isert_post_recv(isert_conn, rx_desc); 1170b8d26b3bSNicholas Bellinger } 1171b8d26b3bSNicholas Bellinger 1172b8d26b3bSNicholas Bellinger static int 1173778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd, 117466cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd, struct iser_rx_desc *rx_desc, 1175d703ce2fSNicholas Bellinger unsigned char *buf) 1176778de368SNicholas Bellinger { 1177be36d683SMax Gurtovoy struct iscsit_conn *conn = isert_conn->conn; 1178778de368SNicholas Bellinger struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf; 1179778de368SNicholas Bellinger int rc; 1180778de368SNicholas Bellinger 1181778de368SNicholas Bellinger rc = iscsit_setup_nop_out(conn, cmd, hdr); 1182778de368SNicholas Bellinger if (rc < 0) 1183778de368SNicholas Bellinger return rc; 1184778de368SNicholas Bellinger /* 1185778de368SNicholas Bellinger * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload 1186778de368SNicholas Bellinger */ 1187778de368SNicholas Bellinger 1188778de368SNicholas Bellinger return iscsit_process_nop_out(conn, cmd, hdr); 1189778de368SNicholas Bellinger } 1190778de368SNicholas Bellinger 1191778de368SNicholas Bellinger static int 1192adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd, 119366cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd, struct iser_rx_desc *rx_desc, 1194d703ce2fSNicholas Bellinger struct iscsi_text *hdr) 1195adb54c29SNicholas Bellinger { 1196be36d683SMax Gurtovoy struct iscsit_conn *conn = isert_conn->conn; 1197adb54c29SNicholas Bellinger u32 payload_length = ntoh24(hdr->dlength); 1198adb54c29SNicholas Bellinger int rc; 1199b44a2b67SSagi Grimberg unsigned char *text_in = NULL; 1200adb54c29SNicholas Bellinger 1201adb54c29SNicholas Bellinger rc = iscsit_setup_text_cmd(conn, cmd, hdr); 1202adb54c29SNicholas Bellinger if (rc < 0) 1203adb54c29SNicholas Bellinger return rc; 1204adb54c29SNicholas Bellinger 1205b44a2b67SSagi Grimberg if (payload_length) { 1206adb54c29SNicholas Bellinger text_in = kzalloc(payload_length, GFP_KERNEL); 120793b80f29SLeon Romanovsky if (!text_in) 1208adb54c29SNicholas Bellinger return -ENOMEM; 1209adb54c29SNicholas Bellinger } 1210adb54c29SNicholas Bellinger cmd->text_in_ptr = text_in; 1211adb54c29SNicholas Bellinger 12120b089c1eSSagi Grimberg memcpy(cmd->text_in_ptr, isert_get_data(rx_desc), payload_length); 1213adb54c29SNicholas Bellinger 1214adb54c29SNicholas Bellinger return iscsit_process_text_cmd(conn, cmd, hdr); 1215adb54c29SNicholas Bellinger } 1216adb54c29SNicholas Bellinger 1217adb54c29SNicholas Bellinger static int 1218b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc, 1219b8d26b3bSNicholas Bellinger uint32_t read_stag, uint64_t read_va, 1220b8d26b3bSNicholas Bellinger uint32_t write_stag, uint64_t write_va) 1221b8d26b3bSNicholas Bellinger { 12220b089c1eSSagi Grimberg struct iscsi_hdr *hdr = isert_get_iscsi_hdr(rx_desc); 1223be36d683SMax Gurtovoy struct iscsit_conn *conn = isert_conn->conn; 122466cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd; 1225b8d26b3bSNicholas Bellinger struct isert_cmd *isert_cmd; 1226b8d26b3bSNicholas Bellinger int ret = -EINVAL; 1227b8d26b3bSNicholas Bellinger u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK); 1228b8d26b3bSNicholas Bellinger 1229fb140271SSagi Grimberg if (conn->sess->sess_ops->SessionType && 1230ca40d24eSNicholas Bellinger (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) { 123124f412ddSSagi Grimberg isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery," 1232ca40d24eSNicholas Bellinger " ignoring\n", opcode); 1233ca40d24eSNicholas Bellinger return 0; 1234ca40d24eSNicholas Bellinger } 1235ca40d24eSNicholas Bellinger 1236b8d26b3bSNicholas Bellinger switch (opcode) { 1237b8d26b3bSNicholas Bellinger case ISCSI_OP_SCSI_CMD: 12384366b19cSJenny Derzhavetz cmd = isert_allocate_cmd(conn, rx_desc); 1239b8d26b3bSNicholas Bellinger if (!cmd) 1240b8d26b3bSNicholas Bellinger break; 1241b8d26b3bSNicholas Bellinger 1242d703ce2fSNicholas Bellinger isert_cmd = iscsit_priv_cmd(cmd); 1243b8d26b3bSNicholas Bellinger isert_cmd->read_stag = read_stag; 1244b8d26b3bSNicholas Bellinger isert_cmd->read_va = read_va; 1245b8d26b3bSNicholas Bellinger isert_cmd->write_stag = write_stag; 1246b8d26b3bSNicholas Bellinger isert_cmd->write_va = write_va; 1247422bd0acSJenny Derzhavetz isert_cmd->inv_rkey = read_stag ? read_stag : write_stag; 1248b8d26b3bSNicholas Bellinger 1249d703ce2fSNicholas Bellinger ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd, 1250b8d26b3bSNicholas Bellinger rx_desc, (unsigned char *)hdr); 1251b8d26b3bSNicholas Bellinger break; 1252b8d26b3bSNicholas Bellinger case ISCSI_OP_NOOP_OUT: 12534366b19cSJenny Derzhavetz cmd = isert_allocate_cmd(conn, rx_desc); 1254b8d26b3bSNicholas Bellinger if (!cmd) 1255b8d26b3bSNicholas Bellinger break; 1256b8d26b3bSNicholas Bellinger 1257d703ce2fSNicholas Bellinger isert_cmd = iscsit_priv_cmd(cmd); 1258d703ce2fSNicholas Bellinger ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd, 1259778de368SNicholas Bellinger rx_desc, (unsigned char *)hdr); 1260b8d26b3bSNicholas Bellinger break; 1261b8d26b3bSNicholas Bellinger case ISCSI_OP_SCSI_DATA_OUT: 1262b8d26b3bSNicholas Bellinger ret = isert_handle_iscsi_dataout(isert_conn, rx_desc, 1263b8d26b3bSNicholas Bellinger (unsigned char *)hdr); 1264b8d26b3bSNicholas Bellinger break; 1265b8d26b3bSNicholas Bellinger case ISCSI_OP_SCSI_TMFUNC: 12664366b19cSJenny Derzhavetz cmd = isert_allocate_cmd(conn, rx_desc); 1267b8d26b3bSNicholas Bellinger if (!cmd) 1268b8d26b3bSNicholas Bellinger break; 1269b8d26b3bSNicholas Bellinger 1270b8d26b3bSNicholas Bellinger ret = iscsit_handle_task_mgt_cmd(conn, cmd, 1271b8d26b3bSNicholas Bellinger (unsigned char *)hdr); 1272b8d26b3bSNicholas Bellinger break; 1273b8d26b3bSNicholas Bellinger case ISCSI_OP_LOGOUT: 12744366b19cSJenny Derzhavetz cmd = isert_allocate_cmd(conn, rx_desc); 1275b8d26b3bSNicholas Bellinger if (!cmd) 1276b8d26b3bSNicholas Bellinger break; 1277b8d26b3bSNicholas Bellinger 1278b8d26b3bSNicholas Bellinger ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr); 1279b8d26b3bSNicholas Bellinger break; 1280adb54c29SNicholas Bellinger case ISCSI_OP_TEXT: 12814366b19cSJenny Derzhavetz if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) 1282e4f4e801SSagi Grimberg cmd = iscsit_find_cmd_from_itt(conn, hdr->itt); 12834366b19cSJenny Derzhavetz else 12844366b19cSJenny Derzhavetz cmd = isert_allocate_cmd(conn, rx_desc); 12854366b19cSJenny Derzhavetz 1286e4f4e801SSagi Grimberg if (!cmd) 1287e4f4e801SSagi Grimberg break; 1288adb54c29SNicholas Bellinger 1289d703ce2fSNicholas Bellinger isert_cmd = iscsit_priv_cmd(cmd); 1290d703ce2fSNicholas Bellinger ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd, 1291adb54c29SNicholas Bellinger rx_desc, (struct iscsi_text *)hdr); 1292adb54c29SNicholas Bellinger break; 1293b8d26b3bSNicholas Bellinger default: 129424f412ddSSagi Grimberg isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode); 1295b8d26b3bSNicholas Bellinger dump_stack(); 1296b8d26b3bSNicholas Bellinger break; 1297b8d26b3bSNicholas Bellinger } 1298b8d26b3bSNicholas Bellinger 1299b8d26b3bSNicholas Bellinger return ret; 1300b8d26b3bSNicholas Bellinger } 1301b8d26b3bSNicholas Bellinger 1302b8d26b3bSNicholas Bellinger static void 13039679cc51SChristoph Hellwig isert_print_wc(struct ib_wc *wc, const char *type) 1304b8d26b3bSNicholas Bellinger { 13059679cc51SChristoph Hellwig if (wc->status != IB_WC_WR_FLUSH_ERR) 13069679cc51SChristoph Hellwig isert_err("%s failure: %s (%d) vend_err %x\n", type, 13079679cc51SChristoph Hellwig ib_wc_status_msg(wc->status), wc->status, 13089679cc51SChristoph Hellwig wc->vendor_err); 13099679cc51SChristoph Hellwig else 13109679cc51SChristoph Hellwig isert_dbg("%s failure: %s (%d)\n", type, 13119679cc51SChristoph Hellwig ib_wc_status_msg(wc->status), wc->status); 13129679cc51SChristoph Hellwig } 13139679cc51SChristoph Hellwig 13149679cc51SChristoph Hellwig static void 13159679cc51SChristoph Hellwig isert_recv_done(struct ib_cq *cq, struct ib_wc *wc) 13169679cc51SChristoph Hellwig { 13179679cc51SChristoph Hellwig struct isert_conn *isert_conn = wc->qp->qp_context; 13189679cc51SChristoph Hellwig struct ib_device *ib_dev = isert_conn->cm_id->device; 13199679cc51SChristoph Hellwig struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe); 13200b089c1eSSagi Grimberg struct iscsi_hdr *hdr = isert_get_iscsi_hdr(rx_desc); 13210b089c1eSSagi Grimberg struct iser_ctrl *iser_ctrl = isert_get_iser_hdr(rx_desc); 1322b8d26b3bSNicholas Bellinger uint64_t read_va = 0, write_va = 0; 1323b8d26b3bSNicholas Bellinger uint32_t read_stag = 0, write_stag = 0; 1324b8d26b3bSNicholas Bellinger 13259679cc51SChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS)) { 13269679cc51SChristoph Hellwig isert_print_wc(wc, "recv"); 13279679cc51SChristoph Hellwig if (wc->status != IB_WC_WR_FLUSH_ERR) 13289679cc51SChristoph Hellwig iscsit_cause_connection_reinstatement(isert_conn->conn, 0); 13299679cc51SChristoph Hellwig return; 13309679cc51SChristoph Hellwig } 13319679cc51SChristoph Hellwig 13327a56dc88SSagi Grimberg rx_desc->in_use = true; 13337a56dc88SSagi Grimberg 13349679cc51SChristoph Hellwig ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr, 13350b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 13369679cc51SChristoph Hellwig 13379679cc51SChristoph Hellwig isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n", 13389679cc51SChristoph Hellwig rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags, 13399679cc51SChristoph Hellwig (int)(wc->byte_len - ISER_HEADERS_LEN)); 13409679cc51SChristoph Hellwig 1341d3cf81f9SSagi Grimberg switch (iser_ctrl->flags & 0xF0) { 1342b8d26b3bSNicholas Bellinger case ISCSI_CTRL: 1343d3cf81f9SSagi Grimberg if (iser_ctrl->flags & ISER_RSV) { 1344d3cf81f9SSagi Grimberg read_stag = be32_to_cpu(iser_ctrl->read_stag); 1345d3cf81f9SSagi Grimberg read_va = be64_to_cpu(iser_ctrl->read_va); 13464c22e07fSSagi Grimberg isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n", 1347b8d26b3bSNicholas Bellinger read_stag, (unsigned long long)read_va); 1348b8d26b3bSNicholas Bellinger } 1349d3cf81f9SSagi Grimberg if (iser_ctrl->flags & ISER_WSV) { 1350d3cf81f9SSagi Grimberg write_stag = be32_to_cpu(iser_ctrl->write_stag); 1351d3cf81f9SSagi Grimberg write_va = be64_to_cpu(iser_ctrl->write_va); 13524c22e07fSSagi Grimberg isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n", 1353b8d26b3bSNicholas Bellinger write_stag, (unsigned long long)write_va); 1354b8d26b3bSNicholas Bellinger } 1355b8d26b3bSNicholas Bellinger 135624f412ddSSagi Grimberg isert_dbg("ISER ISCSI_CTRL PDU\n"); 1357b8d26b3bSNicholas Bellinger break; 1358b8d26b3bSNicholas Bellinger case ISER_HELLO: 135924f412ddSSagi Grimberg isert_err("iSER Hello message\n"); 1360b8d26b3bSNicholas Bellinger break; 1361b8d26b3bSNicholas Bellinger default: 1362d3cf81f9SSagi Grimberg isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags); 1363b8d26b3bSNicholas Bellinger break; 1364b8d26b3bSNicholas Bellinger } 1365b8d26b3bSNicholas Bellinger 13666c760b3dSBart Van Assche isert_rx_opcode(isert_conn, rx_desc, 1367b8d26b3bSNicholas Bellinger read_stag, read_va, write_stag, write_va); 13689679cc51SChristoph Hellwig 13699679cc51SChristoph Hellwig ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr, 13700b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 1371b8d26b3bSNicholas Bellinger } 1372b8d26b3bSNicholas Bellinger 1373b8d26b3bSNicholas Bellinger static void 13749679cc51SChristoph Hellwig isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc) 1375b8d26b3bSNicholas Bellinger { 13769679cc51SChristoph Hellwig struct isert_conn *isert_conn = wc->qp->qp_context; 1377fce50a2fSNicholas Bellinger struct ib_device *ib_dev = isert_conn->device->ib_device; 1378b8d26b3bSNicholas Bellinger 13799679cc51SChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS)) { 13809679cc51SChristoph Hellwig isert_print_wc(wc, "login recv"); 13819679cc51SChristoph Hellwig return; 1382b8d26b3bSNicholas Bellinger } 1383b8d26b3bSNicholas Bellinger 13840b089c1eSSagi Grimberg ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_desc->dma_addr, 13850b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 1386b8d26b3bSNicholas Bellinger 13879679cc51SChristoph Hellwig isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN; 1388b8d26b3bSNicholas Bellinger 13892371e5daSSagi Grimberg if (isert_conn->conn) { 13902371e5daSSagi Grimberg struct iscsi_login *login = isert_conn->conn->conn_login; 13912371e5daSSagi Grimberg 13922371e5daSSagi Grimberg if (login && !login->first_request) 13932371e5daSSagi Grimberg isert_rx_login_req(isert_conn); 13942371e5daSSagi Grimberg } 13959679cc51SChristoph Hellwig 1396dac6ab30SSagi Grimberg mutex_lock(&isert_conn->mutex); 13972371e5daSSagi Grimberg complete(&isert_conn->login_req_comp); 1398dac6ab30SSagi Grimberg mutex_unlock(&isert_conn->mutex); 1399b8d26b3bSNicholas Bellinger 14000b089c1eSSagi Grimberg ib_dma_sync_single_for_device(ib_dev, isert_conn->login_desc->dma_addr, 14010b089c1eSSagi Grimberg ISER_RX_SIZE, DMA_FROM_DEVICE); 1402b8d26b3bSNicholas Bellinger } 1403b8d26b3bSNicholas Bellinger 1404e3d7e4c3SSagi Grimberg static void 140538a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn) 1406e3d7e4c3SSagi Grimberg { 140766cd9d4eSMax Gurtovoy struct se_cmd *se_cmd = &cmd->iscsit_cmd->se_cmd; 140838a2d0d4SChristoph Hellwig enum dma_data_direction dir = target_reverse_dma_direction(se_cmd); 1409e3d7e4c3SSagi Grimberg 141038a2d0d4SChristoph Hellwig if (!cmd->rw.nr_ops) 141138a2d0d4SChristoph Hellwig return; 141238a2d0d4SChristoph Hellwig 141338a2d0d4SChristoph Hellwig if (isert_prot_cmd(conn, se_cmd)) { 141438a2d0d4SChristoph Hellwig rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp, 141538a2d0d4SChristoph Hellwig conn->cm_id->port_num, se_cmd->t_data_sg, 141638a2d0d4SChristoph Hellwig se_cmd->t_data_nents, se_cmd->t_prot_sg, 141738a2d0d4SChristoph Hellwig se_cmd->t_prot_nents, dir); 141838a2d0d4SChristoph Hellwig } else { 141938a2d0d4SChristoph Hellwig rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num, 142038a2d0d4SChristoph Hellwig se_cmd->t_data_sg, se_cmd->t_data_nents, dir); 1421e3d7e4c3SSagi Grimberg } 1422e3d7e4c3SSagi Grimberg 142338a2d0d4SChristoph Hellwig cmd->rw.nr_ops = 0; 142459464ef4SVu Pham } 142559464ef4SVu Pham 142659464ef4SVu Pham static void 142703e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err) 1428b8d26b3bSNicholas Bellinger { 142966cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd = isert_cmd->iscsit_cmd; 1430b8d26b3bSNicholas Bellinger struct isert_conn *isert_conn = isert_cmd->conn; 1431be36d683SMax Gurtovoy struct iscsit_conn *conn = isert_conn->conn; 1432e4f4e801SSagi Grimberg struct iscsi_text_rsp *hdr; 1433b8d26b3bSNicholas Bellinger 14344c22e07fSSagi Grimberg isert_dbg("Cmd %p\n", isert_cmd); 1435b8d26b3bSNicholas Bellinger 1436b8d26b3bSNicholas Bellinger switch (cmd->iscsi_opcode) { 1437b8d26b3bSNicholas Bellinger case ISCSI_OP_SCSI_CMD: 1438b8d26b3bSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 1439b8d26b3bSNicholas Bellinger if (!list_empty(&cmd->i_conn_node)) 14405159d763SNicholas Bellinger list_del_init(&cmd->i_conn_node); 1441b8d26b3bSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 1442b8d26b3bSNicholas Bellinger 144303e7848aSNicholas Bellinger if (cmd->data_direction == DMA_TO_DEVICE) { 1444b8d26b3bSNicholas Bellinger iscsit_stop_dataout_timer(cmd); 144503e7848aSNicholas Bellinger /* 144603e7848aSNicholas Bellinger * Check for special case during comp_err where 144703e7848aSNicholas Bellinger * WRITE_PENDING has been handed off from core, 144803e7848aSNicholas Bellinger * but requires an extra target_put_sess_cmd() 144903e7848aSNicholas Bellinger * before transport_generic_free_cmd() below. 145003e7848aSNicholas Bellinger */ 145103e7848aSNicholas Bellinger if (comp_err && 145203e7848aSNicholas Bellinger cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) { 145303e7848aSNicholas Bellinger struct se_cmd *se_cmd = &cmd->se_cmd; 145403e7848aSNicholas Bellinger 1455afc16604SBart Van Assche target_put_sess_cmd(se_cmd); 145603e7848aSNicholas Bellinger } 145703e7848aSNicholas Bellinger } 1458b8d26b3bSNicholas Bellinger 145938a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn); 1460186a9647SNicholas Bellinger transport_generic_free_cmd(&cmd->se_cmd, 0); 1461186a9647SNicholas Bellinger break; 1462b8d26b3bSNicholas Bellinger case ISCSI_OP_SCSI_TMFUNC: 1463186a9647SNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 1464186a9647SNicholas Bellinger if (!list_empty(&cmd->i_conn_node)) 14655159d763SNicholas Bellinger list_del_init(&cmd->i_conn_node); 1466186a9647SNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 1467186a9647SNicholas Bellinger 1468b8d26b3bSNicholas Bellinger transport_generic_free_cmd(&cmd->se_cmd, 0); 1469b8d26b3bSNicholas Bellinger break; 1470b8d26b3bSNicholas Bellinger case ISCSI_OP_REJECT: 1471b8d26b3bSNicholas Bellinger case ISCSI_OP_NOOP_OUT: 1472adb54c29SNicholas Bellinger case ISCSI_OP_TEXT: 1473e4f4e801SSagi Grimberg hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header; 1474e4f4e801SSagi Grimberg /* If the continue bit is on, keep the command alive */ 1475e4f4e801SSagi Grimberg if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE) 1476e4f4e801SSagi Grimberg break; 1477e4f4e801SSagi Grimberg 1478b8d26b3bSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 1479b8d26b3bSNicholas Bellinger if (!list_empty(&cmd->i_conn_node)) 14805159d763SNicholas Bellinger list_del_init(&cmd->i_conn_node); 1481b8d26b3bSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 1482b8d26b3bSNicholas Bellinger 1483b8d26b3bSNicholas Bellinger /* 1484b8d26b3bSNicholas Bellinger * Handle special case for REJECT when iscsi_add_reject*() has 1485b8d26b3bSNicholas Bellinger * overwritten the original iscsi_opcode assignment, and the 1486b8d26b3bSNicholas Bellinger * associated cmd->se_cmd needs to be released. 1487b8d26b3bSNicholas Bellinger */ 1488b8d26b3bSNicholas Bellinger if (cmd->se_cmd.se_tfo != NULL) { 148911378cdbSRasmus Villemoes isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n", 14903df8f68aSNicholas Bellinger cmd->iscsi_opcode); 1491b8d26b3bSNicholas Bellinger transport_generic_free_cmd(&cmd->se_cmd, 0); 1492b8d26b3bSNicholas Bellinger break; 1493b8d26b3bSNicholas Bellinger } 1494df561f66SGustavo A. R. Silva fallthrough; 1495b8d26b3bSNicholas Bellinger default: 1496d703ce2fSNicholas Bellinger iscsit_release_cmd(cmd); 1497b8d26b3bSNicholas Bellinger break; 1498b8d26b3bSNicholas Bellinger } 1499b8d26b3bSNicholas Bellinger } 1500b8d26b3bSNicholas Bellinger 1501b8d26b3bSNicholas Bellinger static void 1502b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev) 1503b8d26b3bSNicholas Bellinger { 1504b8d26b3bSNicholas Bellinger if (tx_desc->dma_addr != 0) { 15054c22e07fSSagi Grimberg isert_dbg("unmap single for tx_desc->dma_addr\n"); 1506b8d26b3bSNicholas Bellinger ib_dma_unmap_single(ib_dev, tx_desc->dma_addr, 1507b8d26b3bSNicholas Bellinger ISER_HEADERS_LEN, DMA_TO_DEVICE); 1508b8d26b3bSNicholas Bellinger tx_desc->dma_addr = 0; 1509b8d26b3bSNicholas Bellinger } 1510b8d26b3bSNicholas Bellinger } 1511b8d26b3bSNicholas Bellinger 1512b8d26b3bSNicholas Bellinger static void 1513b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd, 151403e7848aSNicholas Bellinger struct ib_device *ib_dev, bool comp_err) 1515b8d26b3bSNicholas Bellinger { 1516dbbc5d11SNicholas Bellinger if (isert_cmd->pdu_buf_dma != 0) { 15174c22e07fSSagi Grimberg isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n"); 1518dbbc5d11SNicholas Bellinger ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma, 1519dbbc5d11SNicholas Bellinger isert_cmd->pdu_buf_len, DMA_TO_DEVICE); 1520dbbc5d11SNicholas Bellinger isert_cmd->pdu_buf_dma = 0; 1521b8d26b3bSNicholas Bellinger } 1522b8d26b3bSNicholas Bellinger 1523b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(tx_desc, ib_dev); 152403e7848aSNicholas Bellinger isert_put_cmd(isert_cmd, comp_err); 1525b8d26b3bSNicholas Bellinger } 1526b8d26b3bSNicholas Bellinger 152796b7973eSSagi Grimberg static int 152896b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr) 1529f93f3a70SSagi Grimberg { 15309e961ae7SSagi Grimberg struct ib_mr_status mr_status; 153196b7973eSSagi Grimberg int ret; 1532f93f3a70SSagi Grimberg 153396b7973eSSagi Grimberg ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status); 15349e961ae7SSagi Grimberg if (ret) { 153524f412ddSSagi Grimberg isert_err("ib_check_mr_status failed, ret %d\n", ret); 15369e961ae7SSagi Grimberg goto fail_mr_status; 15379e961ae7SSagi Grimberg } 153896b7973eSSagi Grimberg 15399e961ae7SSagi Grimberg if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { 154096b7973eSSagi Grimberg u64 sec_offset_err; 15419e961ae7SSagi Grimberg u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8; 15429e961ae7SSagi Grimberg 15439e961ae7SSagi Grimberg switch (mr_status.sig_err.err_type) { 15449e961ae7SSagi Grimberg case IB_SIG_BAD_GUARD: 15459e961ae7SSagi Grimberg se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; 15469e961ae7SSagi Grimberg break; 15479e961ae7SSagi Grimberg case IB_SIG_BAD_REFTAG: 15489e961ae7SSagi Grimberg se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 15499e961ae7SSagi Grimberg break; 15509e961ae7SSagi Grimberg case IB_SIG_BAD_APPTAG: 15519e961ae7SSagi Grimberg se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED; 15529e961ae7SSagi Grimberg break; 15539e961ae7SSagi Grimberg } 155496b7973eSSagi Grimberg sec_offset_err = mr_status.sig_err.sig_err_offset; 155596b7973eSSagi Grimberg do_div(sec_offset_err, block_size); 15568dd992fbSDavid Disseldorp se_cmd->sense_info = sec_offset_err + se_cmd->t_task_lba; 15579e961ae7SSagi Grimberg 15584c22e07fSSagi Grimberg isert_err("PI error found type %d at sector 0x%llx " 15599e961ae7SSagi Grimberg "expected 0x%x vs actual 0x%x\n", 15609e961ae7SSagi Grimberg mr_status.sig_err.err_type, 15618dd992fbSDavid Disseldorp (unsigned long long)se_cmd->sense_info, 15629e961ae7SSagi Grimberg mr_status.sig_err.expected, 15639e961ae7SSagi Grimberg mr_status.sig_err.actual); 15649e961ae7SSagi Grimberg ret = 1; 15659e961ae7SSagi Grimberg } 15669e961ae7SSagi Grimberg 15679e961ae7SSagi Grimberg fail_mr_status: 156896b7973eSSagi Grimberg return ret; 156996b7973eSSagi Grimberg } 157096b7973eSSagi Grimberg 157196b7973eSSagi Grimberg static void 15729679cc51SChristoph Hellwig isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc) 157396b7973eSSagi Grimberg { 15749679cc51SChristoph Hellwig struct isert_conn *isert_conn = wc->qp->qp_context; 1575dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 15769679cc51SChristoph Hellwig struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe); 1577e3416ab2SChristoph Hellwig struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc); 157866cd9d4eSMax Gurtovoy struct se_cmd *cmd = &isert_cmd->iscsit_cmd->se_cmd; 157996b7973eSSagi Grimberg int ret = 0; 158096b7973eSSagi Grimberg 15819679cc51SChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS)) { 15829679cc51SChristoph Hellwig isert_print_wc(wc, "rdma write"); 15839679cc51SChristoph Hellwig if (wc->status != IB_WC_WR_FLUSH_ERR) 15849679cc51SChristoph Hellwig iscsit_cause_connection_reinstatement(isert_conn->conn, 0); 15859679cc51SChristoph Hellwig isert_completion_put(desc, isert_cmd, device->ib_device, true); 15869679cc51SChristoph Hellwig return; 15879679cc51SChristoph Hellwig } 15889679cc51SChristoph Hellwig 15899679cc51SChristoph Hellwig isert_dbg("Cmd %p\n", isert_cmd); 15909679cc51SChristoph Hellwig 1591e9a53e73SIsrael Rukshin ret = isert_check_pi_status(cmd, isert_cmd->rw.reg->mr); 159238a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn); 159396b7973eSSagi Grimberg 1594555a65f6SNicholas Bellinger if (ret) { 1595555a65f6SNicholas Bellinger /* 1596555a65f6SNicholas Bellinger * transport_generic_request_failure() expects to have 1597555a65f6SNicholas Bellinger * plus two references to handle queue-full, so re-add 1598555a65f6SNicholas Bellinger * one here as target-core will have already dropped 1599555a65f6SNicholas Bellinger * it after the first isert_put_datain() callback. 1600555a65f6SNicholas Bellinger */ 1601555a65f6SNicholas Bellinger kref_get(&cmd->cmd_kref); 1602555a65f6SNicholas Bellinger transport_generic_request_failure(cmd, cmd->pi_err); 1603555a65f6SNicholas Bellinger } else { 1604555a65f6SNicholas Bellinger /* 1605555a65f6SNicholas Bellinger * XXX: isert_put_response() failure is not retried. 1606555a65f6SNicholas Bellinger */ 160766cd9d4eSMax Gurtovoy ret = isert_put_response(isert_conn->conn, isert_cmd->iscsit_cmd); 16089e961ae7SSagi Grimberg if (ret) 1609555a65f6SNicholas Bellinger pr_warn_ratelimited("isert_put_response() ret: %d\n", ret); 1610555a65f6SNicholas Bellinger } 1611f93f3a70SSagi Grimberg } 1612f93f3a70SSagi Grimberg 1613f93f3a70SSagi Grimberg static void 16149679cc51SChristoph Hellwig isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc) 1615b8d26b3bSNicholas Bellinger { 16169679cc51SChristoph Hellwig struct isert_conn *isert_conn = wc->qp->qp_context; 16179679cc51SChristoph Hellwig struct isert_device *device = isert_conn->device; 16189679cc51SChristoph Hellwig struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe); 1619e3416ab2SChristoph Hellwig struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc); 162066cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd = isert_cmd->iscsit_cmd; 1621b8d26b3bSNicholas Bellinger struct se_cmd *se_cmd = &cmd->se_cmd; 16225bac4b1aSSagi Grimberg int ret = 0; 1623b8d26b3bSNicholas Bellinger 16249679cc51SChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS)) { 16259679cc51SChristoph Hellwig isert_print_wc(wc, "rdma read"); 16269679cc51SChristoph Hellwig if (wc->status != IB_WC_WR_FLUSH_ERR) 16279679cc51SChristoph Hellwig iscsit_cause_connection_reinstatement(isert_conn->conn, 0); 16289679cc51SChristoph Hellwig isert_completion_put(desc, isert_cmd, device->ib_device, true); 16299679cc51SChristoph Hellwig return; 16309679cc51SChristoph Hellwig } 16319679cc51SChristoph Hellwig 16329679cc51SChristoph Hellwig isert_dbg("Cmd %p\n", isert_cmd); 16339679cc51SChristoph Hellwig 1634b8d26b3bSNicholas Bellinger iscsit_stop_dataout_timer(cmd); 163538a2d0d4SChristoph Hellwig 163638a2d0d4SChristoph Hellwig if (isert_prot_cmd(isert_conn, se_cmd)) 1637e9a53e73SIsrael Rukshin ret = isert_check_pi_status(se_cmd, isert_cmd->rw.reg->mr); 163838a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn); 163938a2d0d4SChristoph Hellwig cmd->write_data_done = 0; 1640b8d26b3bSNicholas Bellinger 164124f412ddSSagi Grimberg isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd); 1642b8d26b3bSNicholas Bellinger spin_lock_bh(&cmd->istate_lock); 1643b8d26b3bSNicholas Bellinger cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; 1644b8d26b3bSNicholas Bellinger cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; 1645b8d26b3bSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock); 1646b8d26b3bSNicholas Bellinger 1647555a65f6SNicholas Bellinger /* 1648555a65f6SNicholas Bellinger * transport_generic_request_failure() will drop the extra 1649555a65f6SNicholas Bellinger * se_cmd->cmd_kref reference after T10-PI error, and handle 1650555a65f6SNicholas Bellinger * any non-zero ->queue_status() callback error retries. 1651555a65f6SNicholas Bellinger */ 1652555a65f6SNicholas Bellinger if (ret) 1653555a65f6SNicholas Bellinger transport_generic_request_failure(se_cmd, se_cmd->pi_err); 1654555a65f6SNicholas Bellinger else 1655b8d26b3bSNicholas Bellinger target_execute_cmd(se_cmd); 1656b8d26b3bSNicholas Bellinger } 1657b8d26b3bSNicholas Bellinger 1658b8d26b3bSNicholas Bellinger static void 1659b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work) 1660b8d26b3bSNicholas Bellinger { 1661b8d26b3bSNicholas Bellinger struct isert_cmd *isert_cmd = container_of(work, 1662b8d26b3bSNicholas Bellinger struct isert_cmd, comp_work); 1663b8d26b3bSNicholas Bellinger struct isert_conn *isert_conn = isert_cmd->conn; 1664dac6ab30SSagi Grimberg struct ib_device *ib_dev = isert_conn->cm_id->device; 166566cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd = isert_cmd->iscsit_cmd; 1666b8d26b3bSNicholas Bellinger 16674c22e07fSSagi Grimberg isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state); 16684c22e07fSSagi Grimberg 1669b8d26b3bSNicholas Bellinger switch (cmd->i_state) { 1670b8d26b3bSNicholas Bellinger case ISTATE_SEND_TASKMGTRSP: 1671b8d26b3bSNicholas Bellinger iscsit_tmr_post_handler(cmd, cmd->conn); 1672df561f66SGustavo A. R. Silva fallthrough; 1673d2271bd0SBart Van Assche case ISTATE_SEND_REJECT: 1674d2271bd0SBart Van Assche case ISTATE_SEND_TEXTRSP: 1675b8d26b3bSNicholas Bellinger cmd->i_state = ISTATE_SENT_STATUS; 16764c22e07fSSagi Grimberg isert_completion_put(&isert_cmd->tx_desc, isert_cmd, 16774c22e07fSSagi Grimberg ib_dev, false); 16783df8f68aSNicholas Bellinger break; 1679b8d26b3bSNicholas Bellinger case ISTATE_SEND_LOGOUTRSP: 1680b8d26b3bSNicholas Bellinger iscsit_logout_post_handler(cmd, cmd->conn); 1681b8d26b3bSNicholas Bellinger break; 1682b8d26b3bSNicholas Bellinger default: 16834c22e07fSSagi Grimberg isert_err("Unknown i_state %d\n", cmd->i_state); 1684b8d26b3bSNicholas Bellinger dump_stack(); 1685b8d26b3bSNicholas Bellinger break; 1686b8d26b3bSNicholas Bellinger } 1687b8d26b3bSNicholas Bellinger } 1688b8d26b3bSNicholas Bellinger 1689b8d26b3bSNicholas Bellinger static void 16909679cc51SChristoph Hellwig isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc) 1691b8d26b3bSNicholas Bellinger { 16929679cc51SChristoph Hellwig struct isert_conn *isert_conn = wc->qp->qp_context; 16939679cc51SChristoph Hellwig struct ib_device *ib_dev = isert_conn->cm_id->device; 16949679cc51SChristoph Hellwig struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe); 1695b8d26b3bSNicholas Bellinger 16969679cc51SChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS)) { 16979679cc51SChristoph Hellwig isert_print_wc(wc, "login send"); 16989679cc51SChristoph Hellwig if (wc->status != IB_WC_WR_FLUSH_ERR) 16999679cc51SChristoph Hellwig iscsit_cause_connection_reinstatement(isert_conn->conn, 0); 17009679cc51SChristoph Hellwig } 17019679cc51SChristoph Hellwig 17029679cc51SChristoph Hellwig isert_unmap_tx_desc(tx_desc, ib_dev); 17039679cc51SChristoph Hellwig } 17049679cc51SChristoph Hellwig 17059679cc51SChristoph Hellwig static void 17069679cc51SChristoph Hellwig isert_send_done(struct ib_cq *cq, struct ib_wc *wc) 17079679cc51SChristoph Hellwig { 17089679cc51SChristoph Hellwig struct isert_conn *isert_conn = wc->qp->qp_context; 17099679cc51SChristoph Hellwig struct ib_device *ib_dev = isert_conn->cm_id->device; 17109679cc51SChristoph Hellwig struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe); 1711e3416ab2SChristoph Hellwig struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc); 17129679cc51SChristoph Hellwig 17139679cc51SChristoph Hellwig if (unlikely(wc->status != IB_WC_SUCCESS)) { 17149679cc51SChristoph Hellwig isert_print_wc(wc, "send"); 17159679cc51SChristoph Hellwig if (wc->status != IB_WC_WR_FLUSH_ERR) 17169679cc51SChristoph Hellwig iscsit_cause_connection_reinstatement(isert_conn->conn, 0); 17179679cc51SChristoph Hellwig isert_completion_put(tx_desc, isert_cmd, ib_dev, true); 17189679cc51SChristoph Hellwig return; 17199679cc51SChristoph Hellwig } 17209679cc51SChristoph Hellwig 17219679cc51SChristoph Hellwig isert_dbg("Cmd %p\n", isert_cmd); 17229679cc51SChristoph Hellwig 172366cd9d4eSMax Gurtovoy switch (isert_cmd->iscsit_cmd->i_state) { 17249679cc51SChristoph Hellwig case ISTATE_SEND_TASKMGTRSP: 17259679cc51SChristoph Hellwig case ISTATE_SEND_LOGOUTRSP: 17269679cc51SChristoph Hellwig case ISTATE_SEND_REJECT: 17279679cc51SChristoph Hellwig case ISTATE_SEND_TEXTRSP: 1728b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(tx_desc, ib_dev); 1729b8d26b3bSNicholas Bellinger 1730b8d26b3bSNicholas Bellinger INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp); 1731b8d26b3bSNicholas Bellinger queue_work(isert_comp_wq, &isert_cmd->comp_work); 1732b8d26b3bSNicholas Bellinger return; 1733b8d26b3bSNicholas Bellinger default: 173466cd9d4eSMax Gurtovoy isert_cmd->iscsit_cmd->i_state = ISTATE_SENT_STATUS; 17359679cc51SChristoph Hellwig isert_completion_put(tx_desc, isert_cmd, ib_dev, false); 1736b8d26b3bSNicholas Bellinger break; 1737b8d26b3bSNicholas Bellinger } 1738b8d26b3bSNicholas Bellinger } 1739b8d26b3bSNicholas Bellinger 1740b8d26b3bSNicholas Bellinger static int 1741b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd) 1742b8d26b3bSNicholas Bellinger { 1743b8d26b3bSNicholas Bellinger int ret; 1744b8d26b3bSNicholas Bellinger 17454366b19cSJenny Derzhavetz ret = isert_post_recv(isert_conn, isert_cmd->rx_desc); 1746c97119b6SMax Gurtovoy if (ret) 17474366b19cSJenny Derzhavetz return ret; 17484366b19cSJenny Derzhavetz 1749e01a7674SBart Van Assche ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr, NULL); 1750b8d26b3bSNicholas Bellinger if (ret) { 175124f412ddSSagi Grimberg isert_err("ib_post_send failed with %d\n", ret); 1752b8d26b3bSNicholas Bellinger return ret; 1753b8d26b3bSNicholas Bellinger } 1754b8d26b3bSNicholas Bellinger return ret; 1755b8d26b3bSNicholas Bellinger } 1756b8d26b3bSNicholas Bellinger 1757b8d26b3bSNicholas Bellinger static int 1758be36d683SMax Gurtovoy isert_put_response(struct iscsit_conn *conn, struct iscsit_cmd *cmd) 1759b8d26b3bSNicholas Bellinger { 1760d703ce2fSNicholas Bellinger struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 17616700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1762b8d26b3bSNicholas Bellinger struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 1763b8d26b3bSNicholas Bellinger struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *) 1764b8d26b3bSNicholas Bellinger &isert_cmd->tx_desc.iscsi_header; 1765b8d26b3bSNicholas Bellinger 1766b8d26b3bSNicholas Bellinger isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 1767b8d26b3bSNicholas Bellinger iscsit_build_rsp_pdu(cmd, conn, true, hdr); 1768b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 1769b8d26b3bSNicholas Bellinger /* 1770b8d26b3bSNicholas Bellinger * Attach SENSE DATA payload to iSCSI Response PDU 1771b8d26b3bSNicholas Bellinger */ 1772b8d26b3bSNicholas Bellinger if (cmd->se_cmd.sense_buffer && 1773b8d26b3bSNicholas Bellinger ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 1774b8d26b3bSNicholas Bellinger (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { 1775dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 177667cb3949SSagi Grimberg struct ib_device *ib_dev = device->ib_device; 1777b8d26b3bSNicholas Bellinger struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1]; 1778dbbc5d11SNicholas Bellinger u32 padding, pdu_len; 1779b8d26b3bSNicholas Bellinger 1780b8d26b3bSNicholas Bellinger put_unaligned_be16(cmd->se_cmd.scsi_sense_length, 1781b8d26b3bSNicholas Bellinger cmd->sense_buffer); 1782b8d26b3bSNicholas Bellinger cmd->se_cmd.scsi_sense_length += sizeof(__be16); 1783b8d26b3bSNicholas Bellinger 1784b8d26b3bSNicholas Bellinger padding = -(cmd->se_cmd.scsi_sense_length) & 3; 1785b8d26b3bSNicholas Bellinger hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length); 1786dbbc5d11SNicholas Bellinger pdu_len = cmd->se_cmd.scsi_sense_length + padding; 1787b8d26b3bSNicholas Bellinger 1788dbbc5d11SNicholas Bellinger isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev, 1789dbbc5d11SNicholas Bellinger (void *)cmd->sense_buffer, pdu_len, 1790b8d26b3bSNicholas Bellinger DMA_TO_DEVICE); 1791def4a6ffSAlexey Khoroshilov if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma)) 1792def4a6ffSAlexey Khoroshilov return -ENOMEM; 1793b8d26b3bSNicholas Bellinger 1794dbbc5d11SNicholas Bellinger isert_cmd->pdu_buf_len = pdu_len; 1795dbbc5d11SNicholas Bellinger tx_dsg->addr = isert_cmd->pdu_buf_dma; 1796dbbc5d11SNicholas Bellinger tx_dsg->length = pdu_len; 179734efc7dfSJason Gunthorpe tx_dsg->lkey = device->pd->local_dma_lkey; 1798b8d26b3bSNicholas Bellinger isert_cmd->tx_desc.num_sge = 2; 1799b8d26b3bSNicholas Bellinger } 1800b8d26b3bSNicholas Bellinger 180168a86deeSSagi Grimberg isert_init_send_wr(isert_conn, isert_cmd, send_wr); 1802b8d26b3bSNicholas Bellinger 18034c22e07fSSagi Grimberg isert_dbg("Posting SCSI Response\n"); 1804b8d26b3bSNicholas Bellinger 1805b8d26b3bSNicholas Bellinger return isert_post_response(isert_conn, isert_cmd); 1806b8d26b3bSNicholas Bellinger } 1807b8d26b3bSNicholas Bellinger 1808131e6abcSNicholas Bellinger static void 1809be36d683SMax Gurtovoy isert_aborted_task(struct iscsit_conn *conn, struct iscsit_cmd *cmd) 1810131e6abcSNicholas Bellinger { 1811131e6abcSNicholas Bellinger struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 18126700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1813131e6abcSNicholas Bellinger 1814131e6abcSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 1815131e6abcSNicholas Bellinger if (!list_empty(&cmd->i_conn_node)) 1816131e6abcSNicholas Bellinger list_del_init(&cmd->i_conn_node); 1817131e6abcSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 1818131e6abcSNicholas Bellinger 1819131e6abcSNicholas Bellinger if (cmd->data_direction == DMA_TO_DEVICE) 1820131e6abcSNicholas Bellinger iscsit_stop_dataout_timer(cmd); 182138a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn); 1822131e6abcSNicholas Bellinger } 1823131e6abcSNicholas Bellinger 1824e70beee7SNicholas Bellinger static enum target_prot_op 1825be36d683SMax Gurtovoy isert_get_sup_prot_ops(struct iscsit_conn *conn) 1826e70beee7SNicholas Bellinger { 18276700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1828dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 1829e70beee7SNicholas Bellinger 183023a548eeSSagi Grimberg if (conn->tpg->tpg_attrib.t10_pi) { 183123a548eeSSagi Grimberg if (device->pi_capable) { 183224f412ddSSagi Grimberg isert_info("conn %p PI offload enabled\n", isert_conn); 183323a548eeSSagi Grimberg isert_conn->pi_support = true; 1834e70beee7SNicholas Bellinger return TARGET_PROT_ALL; 183523a548eeSSagi Grimberg } 183623a548eeSSagi Grimberg } 183723a548eeSSagi Grimberg 183824f412ddSSagi Grimberg isert_info("conn %p PI offload disabled\n", isert_conn); 183923a548eeSSagi Grimberg isert_conn->pi_support = false; 1840e70beee7SNicholas Bellinger 1841e70beee7SNicholas Bellinger return TARGET_PROT_NORMAL; 1842e70beee7SNicholas Bellinger } 1843e70beee7SNicholas Bellinger 1844b8d26b3bSNicholas Bellinger static int 1845be36d683SMax Gurtovoy isert_put_nopin(struct iscsit_cmd *cmd, struct iscsit_conn *conn, 1846b8d26b3bSNicholas Bellinger bool nopout_response) 1847b8d26b3bSNicholas Bellinger { 1848d703ce2fSNicholas Bellinger struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 18496700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1850b8d26b3bSNicholas Bellinger struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 1851b8d26b3bSNicholas Bellinger 1852b8d26b3bSNicholas Bellinger isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 1853b8d26b3bSNicholas Bellinger iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *) 1854b8d26b3bSNicholas Bellinger &isert_cmd->tx_desc.iscsi_header, 1855b8d26b3bSNicholas Bellinger nopout_response); 1856b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 185768a86deeSSagi Grimberg isert_init_send_wr(isert_conn, isert_cmd, send_wr); 1858b8d26b3bSNicholas Bellinger 18594c22e07fSSagi Grimberg isert_dbg("conn %p Posting NOPIN Response\n", isert_conn); 1860b8d26b3bSNicholas Bellinger 1861b8d26b3bSNicholas Bellinger return isert_post_response(isert_conn, isert_cmd); 1862b8d26b3bSNicholas Bellinger } 1863b8d26b3bSNicholas Bellinger 1864b8d26b3bSNicholas Bellinger static int 1865be36d683SMax Gurtovoy isert_put_logout_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn) 1866b8d26b3bSNicholas Bellinger { 1867d703ce2fSNicholas Bellinger struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 18686700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1869b8d26b3bSNicholas Bellinger struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 1870b8d26b3bSNicholas Bellinger 1871b8d26b3bSNicholas Bellinger isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 1872b8d26b3bSNicholas Bellinger iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *) 1873b8d26b3bSNicholas Bellinger &isert_cmd->tx_desc.iscsi_header); 1874b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 187568a86deeSSagi Grimberg isert_init_send_wr(isert_conn, isert_cmd, send_wr); 1876b8d26b3bSNicholas Bellinger 18774c22e07fSSagi Grimberg isert_dbg("conn %p Posting Logout Response\n", isert_conn); 1878b8d26b3bSNicholas Bellinger 1879b8d26b3bSNicholas Bellinger return isert_post_response(isert_conn, isert_cmd); 1880b8d26b3bSNicholas Bellinger } 1881b8d26b3bSNicholas Bellinger 1882b8d26b3bSNicholas Bellinger static int 1883be36d683SMax Gurtovoy isert_put_tm_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn) 1884b8d26b3bSNicholas Bellinger { 1885d703ce2fSNicholas Bellinger struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 18866700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1887b8d26b3bSNicholas Bellinger struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 1888b8d26b3bSNicholas Bellinger 1889b8d26b3bSNicholas Bellinger isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 1890b8d26b3bSNicholas Bellinger iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *) 1891b8d26b3bSNicholas Bellinger &isert_cmd->tx_desc.iscsi_header); 1892b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 189368a86deeSSagi Grimberg isert_init_send_wr(isert_conn, isert_cmd, send_wr); 1894b8d26b3bSNicholas Bellinger 18954c22e07fSSagi Grimberg isert_dbg("conn %p Posting Task Management Response\n", isert_conn); 1896b8d26b3bSNicholas Bellinger 1897b8d26b3bSNicholas Bellinger return isert_post_response(isert_conn, isert_cmd); 1898b8d26b3bSNicholas Bellinger } 1899b8d26b3bSNicholas Bellinger 1900b8d26b3bSNicholas Bellinger static int 1901be36d683SMax Gurtovoy isert_put_reject(struct iscsit_cmd *cmd, struct iscsit_conn *conn) 1902b8d26b3bSNicholas Bellinger { 1903d703ce2fSNicholas Bellinger struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 19046700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1905b8d26b3bSNicholas Bellinger struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 1906dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 190767cb3949SSagi Grimberg struct ib_device *ib_dev = device->ib_device; 19083df8f68aSNicholas Bellinger struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1]; 19093df8f68aSNicholas Bellinger struct iscsi_reject *hdr = 19103df8f68aSNicholas Bellinger (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header; 1911b8d26b3bSNicholas Bellinger 1912b8d26b3bSNicholas Bellinger isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 19133df8f68aSNicholas Bellinger iscsit_build_reject(cmd, conn, hdr); 1914b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 19153df8f68aSNicholas Bellinger 19163df8f68aSNicholas Bellinger hton24(hdr->dlength, ISCSI_HDR_LEN); 1917dbbc5d11SNicholas Bellinger isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev, 19183df8f68aSNicholas Bellinger (void *)cmd->buf_ptr, ISCSI_HDR_LEN, 19193df8f68aSNicholas Bellinger DMA_TO_DEVICE); 1920def4a6ffSAlexey Khoroshilov if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma)) 1921def4a6ffSAlexey Khoroshilov return -ENOMEM; 1922dbbc5d11SNicholas Bellinger isert_cmd->pdu_buf_len = ISCSI_HDR_LEN; 1923dbbc5d11SNicholas Bellinger tx_dsg->addr = isert_cmd->pdu_buf_dma; 19243df8f68aSNicholas Bellinger tx_dsg->length = ISCSI_HDR_LEN; 192534efc7dfSJason Gunthorpe tx_dsg->lkey = device->pd->local_dma_lkey; 19263df8f68aSNicholas Bellinger isert_cmd->tx_desc.num_sge = 2; 19273df8f68aSNicholas Bellinger 192868a86deeSSagi Grimberg isert_init_send_wr(isert_conn, isert_cmd, send_wr); 1929b8d26b3bSNicholas Bellinger 19304c22e07fSSagi Grimberg isert_dbg("conn %p Posting Reject\n", isert_conn); 1931b8d26b3bSNicholas Bellinger 1932b8d26b3bSNicholas Bellinger return isert_post_response(isert_conn, isert_cmd); 1933b8d26b3bSNicholas Bellinger } 1934b8d26b3bSNicholas Bellinger 1935b8d26b3bSNicholas Bellinger static int 1936be36d683SMax Gurtovoy isert_put_text_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn) 1937adb54c29SNicholas Bellinger { 1938d703ce2fSNicholas Bellinger struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 19396700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 1940adb54c29SNicholas Bellinger struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr; 1941adb54c29SNicholas Bellinger struct iscsi_text_rsp *hdr = 1942adb54c29SNicholas Bellinger (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header; 1943adb54c29SNicholas Bellinger u32 txt_rsp_len; 1944adb54c29SNicholas Bellinger int rc; 1945adb54c29SNicholas Bellinger 1946adb54c29SNicholas Bellinger isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc); 194722c7aaa5SSagi Grimberg rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND); 1948adb54c29SNicholas Bellinger if (rc < 0) 1949adb54c29SNicholas Bellinger return rc; 1950adb54c29SNicholas Bellinger 1951adb54c29SNicholas Bellinger txt_rsp_len = rc; 1952adb54c29SNicholas Bellinger isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 1953adb54c29SNicholas Bellinger 1954adb54c29SNicholas Bellinger if (txt_rsp_len) { 1955dac6ab30SSagi Grimberg struct isert_device *device = isert_conn->device; 195667cb3949SSagi Grimberg struct ib_device *ib_dev = device->ib_device; 1957adb54c29SNicholas Bellinger struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1]; 1958adb54c29SNicholas Bellinger void *txt_rsp_buf = cmd->buf_ptr; 1959adb54c29SNicholas Bellinger 1960adb54c29SNicholas Bellinger isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev, 1961adb54c29SNicholas Bellinger txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE); 1962def4a6ffSAlexey Khoroshilov if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma)) 1963def4a6ffSAlexey Khoroshilov return -ENOMEM; 1964adb54c29SNicholas Bellinger 1965adb54c29SNicholas Bellinger isert_cmd->pdu_buf_len = txt_rsp_len; 1966adb54c29SNicholas Bellinger tx_dsg->addr = isert_cmd->pdu_buf_dma; 1967adb54c29SNicholas Bellinger tx_dsg->length = txt_rsp_len; 196834efc7dfSJason Gunthorpe tx_dsg->lkey = device->pd->local_dma_lkey; 1969adb54c29SNicholas Bellinger isert_cmd->tx_desc.num_sge = 2; 1970adb54c29SNicholas Bellinger } 197168a86deeSSagi Grimberg isert_init_send_wr(isert_conn, isert_cmd, send_wr); 1972adb54c29SNicholas Bellinger 1973f64d2792SSagi Grimberg isert_dbg("conn %p Text Response\n", isert_conn); 1974adb54c29SNicholas Bellinger 1975adb54c29SNicholas Bellinger return isert_post_response(isert_conn, isert_cmd); 1976adb54c29SNicholas Bellinger } 1977adb54c29SNicholas Bellinger 19783d73cf1aSSagi Grimberg static inline void 1979dae6345aSIsrael Rukshin isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_domain *domain) 19809e961ae7SSagi Grimberg { 198178eda2bbSSagi Grimberg domain->sig_type = IB_SIG_TYPE_T10_DIF; 19823d73cf1aSSagi Grimberg domain->sig.dif.bg_type = IB_T10DIF_CRC; 19833d73cf1aSSagi Grimberg domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size; 19843d73cf1aSSagi Grimberg domain->sig.dif.ref_tag = se_cmd->reftag_seed; 198578eda2bbSSagi Grimberg /* 198678eda2bbSSagi Grimberg * At the moment we hard code those, but if in the future 198778eda2bbSSagi Grimberg * the target core would like to use it, we will take it 198878eda2bbSSagi Grimberg * from se_cmd. 198978eda2bbSSagi Grimberg */ 199078eda2bbSSagi Grimberg domain->sig.dif.apptag_check_mask = 0xffff; 199178eda2bbSSagi Grimberg domain->sig.dif.app_escape = true; 199278eda2bbSSagi Grimberg domain->sig.dif.ref_escape = true; 199378eda2bbSSagi Grimberg if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT || 199478eda2bbSSagi Grimberg se_cmd->prot_type == TARGET_DIF_TYPE2_PROT) 199578eda2bbSSagi Grimberg domain->sig.dif.ref_remap = true; 1996ec53a2a6SMax Gurtovoy } 19973d73cf1aSSagi Grimberg 19983d73cf1aSSagi Grimberg static int 19993d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs) 20003d73cf1aSSagi Grimberg { 200138a2d0d4SChristoph Hellwig memset(sig_attrs, 0, sizeof(*sig_attrs)); 200238a2d0d4SChristoph Hellwig 20039e961ae7SSagi Grimberg switch (se_cmd->prot_op) { 20049e961ae7SSagi Grimberg case TARGET_PROT_DIN_INSERT: 20059e961ae7SSagi Grimberg case TARGET_PROT_DOUT_STRIP: 200678eda2bbSSagi Grimberg sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE; 2007dae6345aSIsrael Rukshin isert_set_dif_domain(se_cmd, &sig_attrs->wire); 20089e961ae7SSagi Grimberg break; 20099e961ae7SSagi Grimberg case TARGET_PROT_DOUT_INSERT: 20109e961ae7SSagi Grimberg case TARGET_PROT_DIN_STRIP: 201178eda2bbSSagi Grimberg sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE; 2012dae6345aSIsrael Rukshin isert_set_dif_domain(se_cmd, &sig_attrs->mem); 20139e961ae7SSagi Grimberg break; 20149e961ae7SSagi Grimberg case TARGET_PROT_DIN_PASS: 20159e961ae7SSagi Grimberg case TARGET_PROT_DOUT_PASS: 2016dae6345aSIsrael Rukshin isert_set_dif_domain(se_cmd, &sig_attrs->wire); 2017dae6345aSIsrael Rukshin isert_set_dif_domain(se_cmd, &sig_attrs->mem); 20189e961ae7SSagi Grimberg break; 20199e961ae7SSagi Grimberg default: 202024f412ddSSagi Grimberg isert_err("Unsupported PI operation %d\n", se_cmd->prot_op); 20219e961ae7SSagi Grimberg return -EINVAL; 20229e961ae7SSagi Grimberg } 20239e961ae7SSagi Grimberg 202426232872SMax Gurtovoy if (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD) 202526232872SMax Gurtovoy sig_attrs->check_mask |= IB_SIG_CHECK_GUARD; 202626232872SMax Gurtovoy if (se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG) 202726232872SMax Gurtovoy sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG; 202826232872SMax Gurtovoy if (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG) 202926232872SMax Gurtovoy sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG; 203026232872SMax Gurtovoy 20319e961ae7SSagi Grimberg return 0; 20329e961ae7SSagi Grimberg } 20339e961ae7SSagi Grimberg 20349e961ae7SSagi Grimberg static int 203538a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn, 203638a2d0d4SChristoph Hellwig struct ib_cqe *cqe, struct ib_send_wr *chain_wr) 20379e961ae7SSagi Grimberg { 203866cd9d4eSMax Gurtovoy struct se_cmd *se_cmd = &cmd->iscsit_cmd->se_cmd; 203938a2d0d4SChristoph Hellwig enum dma_data_direction dir = target_reverse_dma_direction(se_cmd); 204038a2d0d4SChristoph Hellwig u8 port_num = conn->cm_id->port_num; 204138a2d0d4SChristoph Hellwig u64 addr; 204238a2d0d4SChristoph Hellwig u32 rkey, offset; 20439e961ae7SSagi Grimberg int ret; 20449e961ae7SSagi Grimberg 204566f53e6fSBharat Potnuri if (cmd->ctx_init_done) 204666f53e6fSBharat Potnuri goto rdma_ctx_post; 204766f53e6fSBharat Potnuri 204838a2d0d4SChristoph Hellwig if (dir == DMA_FROM_DEVICE) { 204938a2d0d4SChristoph Hellwig addr = cmd->write_va; 205038a2d0d4SChristoph Hellwig rkey = cmd->write_stag; 205166cd9d4eSMax Gurtovoy offset = cmd->iscsit_cmd->write_data_done; 205238a2d0d4SChristoph Hellwig } else { 205338a2d0d4SChristoph Hellwig addr = cmd->read_va; 205438a2d0d4SChristoph Hellwig rkey = cmd->read_stag; 205538a2d0d4SChristoph Hellwig offset = 0; 205638a2d0d4SChristoph Hellwig } 205738a2d0d4SChristoph Hellwig 205838a2d0d4SChristoph Hellwig if (isert_prot_cmd(conn, se_cmd)) { 205938a2d0d4SChristoph Hellwig struct ib_sig_attrs sig_attrs; 206038a2d0d4SChristoph Hellwig 20619e961ae7SSagi Grimberg ret = isert_set_sig_attrs(se_cmd, &sig_attrs); 20629e961ae7SSagi Grimberg if (ret) 2063e3d7e4c3SSagi Grimberg return ret; 2064e3d7e4c3SSagi Grimberg 206538a2d0d4SChristoph Hellwig WARN_ON_ONCE(offset); 206638a2d0d4SChristoph Hellwig ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num, 206738a2d0d4SChristoph Hellwig se_cmd->t_data_sg, se_cmd->t_data_nents, 206838a2d0d4SChristoph Hellwig se_cmd->t_prot_sg, se_cmd->t_prot_nents, 206938a2d0d4SChristoph Hellwig &sig_attrs, addr, rkey, dir); 2070570db170SSagi Grimberg } else { 207138a2d0d4SChristoph Hellwig ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num, 207238a2d0d4SChristoph Hellwig se_cmd->t_data_sg, se_cmd->t_data_nents, 207338a2d0d4SChristoph Hellwig offset, addr, rkey, dir); 207438a2d0d4SChristoph Hellwig } 207566f53e6fSBharat Potnuri 207638a2d0d4SChristoph Hellwig if (ret < 0) { 207738a2d0d4SChristoph Hellwig isert_err("Cmd: %p failed to prepare RDMA res\n", cmd); 207838a2d0d4SChristoph Hellwig return ret; 20799e961ae7SSagi Grimberg } 20809e961ae7SSagi Grimberg 208166f53e6fSBharat Potnuri cmd->ctx_init_done = true; 208266f53e6fSBharat Potnuri 208366f53e6fSBharat Potnuri rdma_ctx_post: 208438a2d0d4SChristoph Hellwig ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr); 208538a2d0d4SChristoph Hellwig if (ret < 0) 208638a2d0d4SChristoph Hellwig isert_err("Cmd: %p failed to post RDMA res\n", cmd); 208759464ef4SVu Pham return ret; 208859464ef4SVu Pham } 208959464ef4SVu Pham 209059464ef4SVu Pham static int 2091be36d683SMax Gurtovoy isert_put_datain(struct iscsit_conn *conn, struct iscsit_cmd *cmd) 209290ecc6e2SVu Pham { 209390ecc6e2SVu Pham struct se_cmd *se_cmd = &cmd->se_cmd; 209459464ef4SVu Pham struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 20956700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 209638a2d0d4SChristoph Hellwig struct ib_cqe *cqe = NULL; 209738a2d0d4SChristoph Hellwig struct ib_send_wr *chain_wr = NULL; 209890ecc6e2SVu Pham int rc; 209990ecc6e2SVu Pham 210024f412ddSSagi Grimberg isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n", 210190ecc6e2SVu Pham isert_cmd, se_cmd->data_length); 21024c22e07fSSagi Grimberg 210338a2d0d4SChristoph Hellwig if (isert_prot_cmd(isert_conn, se_cmd)) { 210438a2d0d4SChristoph Hellwig isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done; 210538a2d0d4SChristoph Hellwig cqe = &isert_cmd->tx_desc.tx_cqe; 210638a2d0d4SChristoph Hellwig } else { 2107b8d26b3bSNicholas Bellinger /* 2108b8d26b3bSNicholas Bellinger * Build isert_conn->tx_desc for iSCSI response PDU and attach 2109b8d26b3bSNicholas Bellinger */ 21109e961ae7SSagi Grimberg isert_create_send_desc(isert_conn, isert_cmd, 21119e961ae7SSagi Grimberg &isert_cmd->tx_desc); 211204d9cd12SNicholas Bellinger iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *) 2113b8d26b3bSNicholas Bellinger &isert_cmd->tx_desc.iscsi_header); 2114b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc); 211595b60f07SNicholas Bellinger isert_init_send_wr(isert_conn, isert_cmd, 211668a86deeSSagi Grimberg &isert_cmd->tx_desc.send_wr); 21174366b19cSJenny Derzhavetz 21184366b19cSJenny Derzhavetz rc = isert_post_recv(isert_conn, isert_cmd->rx_desc); 2119c97119b6SMax Gurtovoy if (rc) 21204366b19cSJenny Derzhavetz return rc; 212138a2d0d4SChristoph Hellwig 212238a2d0d4SChristoph Hellwig chain_wr = &isert_cmd->tx_desc.send_wr; 21239e961ae7SSagi Grimberg } 2124b8d26b3bSNicholas Bellinger 2125555a65f6SNicholas Bellinger rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr); 2126555a65f6SNicholas Bellinger isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n", 2127555a65f6SNicholas Bellinger isert_cmd, rc); 2128555a65f6SNicholas Bellinger return rc; 2129b8d26b3bSNicholas Bellinger } 2130b8d26b3bSNicholas Bellinger 2131b8d26b3bSNicholas Bellinger static int 2132be36d683SMax Gurtovoy isert_get_dataout(struct iscsit_conn *conn, struct iscsit_cmd *cmd, bool recovery) 2133b8d26b3bSNicholas Bellinger { 2134d703ce2fSNicholas Bellinger struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2135555a65f6SNicholas Bellinger int ret; 2136b8d26b3bSNicholas Bellinger 213724f412ddSSagi Grimberg isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n", 213838a2d0d4SChristoph Hellwig isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done); 2139b8d26b3bSNicholas Bellinger 214038a2d0d4SChristoph Hellwig isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done; 2141555a65f6SNicholas Bellinger ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context, 214238a2d0d4SChristoph Hellwig &isert_cmd->tx_desc.tx_cqe, NULL); 2143bdf20e72SSagi Grimberg 2144555a65f6SNicholas Bellinger isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n", 2145555a65f6SNicholas Bellinger isert_cmd, ret); 2146555a65f6SNicholas Bellinger return ret; 2147b8d26b3bSNicholas Bellinger } 2148b8d26b3bSNicholas Bellinger 2149b8d26b3bSNicholas Bellinger static int 2150be36d683SMax Gurtovoy isert_immediate_queue(struct iscsit_conn *conn, struct iscsit_cmd *cmd, int state) 2151b8d26b3bSNicholas Bellinger { 2152a4c15cd9SJenny Derzhavetz struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 2153a4c15cd9SJenny Derzhavetz int ret = 0; 2154b8d26b3bSNicholas Bellinger 2155b8d26b3bSNicholas Bellinger switch (state) { 2156a4c15cd9SJenny Derzhavetz case ISTATE_REMOVE: 2157a4c15cd9SJenny Derzhavetz spin_lock_bh(&conn->cmd_lock); 2158a4c15cd9SJenny Derzhavetz list_del_init(&cmd->i_conn_node); 2159a4c15cd9SJenny Derzhavetz spin_unlock_bh(&conn->cmd_lock); 2160a4c15cd9SJenny Derzhavetz isert_put_cmd(isert_cmd, true); 2161a4c15cd9SJenny Derzhavetz break; 2162b8d26b3bSNicholas Bellinger case ISTATE_SEND_NOPIN_WANT_RESPONSE: 2163b8d26b3bSNicholas Bellinger ret = isert_put_nopin(cmd, conn, false); 2164b8d26b3bSNicholas Bellinger break; 2165b8d26b3bSNicholas Bellinger default: 216624f412ddSSagi Grimberg isert_err("Unknown immediate state: 0x%02x\n", state); 2167b8d26b3bSNicholas Bellinger ret = -EINVAL; 2168b8d26b3bSNicholas Bellinger break; 2169b8d26b3bSNicholas Bellinger } 2170b8d26b3bSNicholas Bellinger 2171b8d26b3bSNicholas Bellinger return ret; 2172b8d26b3bSNicholas Bellinger } 2173b8d26b3bSNicholas Bellinger 2174b8d26b3bSNicholas Bellinger static int 2175be36d683SMax Gurtovoy isert_response_queue(struct iscsit_conn *conn, struct iscsit_cmd *cmd, int state) 2176b8d26b3bSNicholas Bellinger { 2177991bb764SSagi Grimberg struct isert_conn *isert_conn = conn->context; 2178b8d26b3bSNicholas Bellinger int ret; 2179b8d26b3bSNicholas Bellinger 2180b8d26b3bSNicholas Bellinger switch (state) { 2181b8d26b3bSNicholas Bellinger case ISTATE_SEND_LOGOUTRSP: 2182b8d26b3bSNicholas Bellinger ret = isert_put_logout_rsp(cmd, conn); 2183991bb764SSagi Grimberg if (!ret) 2184991bb764SSagi Grimberg isert_conn->logout_posted = true; 2185b8d26b3bSNicholas Bellinger break; 2186b8d26b3bSNicholas Bellinger case ISTATE_SEND_NOPIN: 2187b8d26b3bSNicholas Bellinger ret = isert_put_nopin(cmd, conn, true); 2188b8d26b3bSNicholas Bellinger break; 2189b8d26b3bSNicholas Bellinger case ISTATE_SEND_TASKMGTRSP: 2190b8d26b3bSNicholas Bellinger ret = isert_put_tm_rsp(cmd, conn); 2191b8d26b3bSNicholas Bellinger break; 2192b8d26b3bSNicholas Bellinger case ISTATE_SEND_REJECT: 2193b8d26b3bSNicholas Bellinger ret = isert_put_reject(cmd, conn); 2194b8d26b3bSNicholas Bellinger break; 2195adb54c29SNicholas Bellinger case ISTATE_SEND_TEXTRSP: 2196adb54c29SNicholas Bellinger ret = isert_put_text_rsp(cmd, conn); 2197adb54c29SNicholas Bellinger break; 2198b8d26b3bSNicholas Bellinger case ISTATE_SEND_STATUS: 2199b8d26b3bSNicholas Bellinger /* 2200b8d26b3bSNicholas Bellinger * Special case for sending non GOOD SCSI status from TX thread 2201b8d26b3bSNicholas Bellinger * context during pre se_cmd excecution failure. 2202b8d26b3bSNicholas Bellinger */ 2203b8d26b3bSNicholas Bellinger ret = isert_put_response(conn, cmd); 2204b8d26b3bSNicholas Bellinger break; 2205b8d26b3bSNicholas Bellinger default: 220624f412ddSSagi Grimberg isert_err("Unknown response state: 0x%02x\n", state); 2207b8d26b3bSNicholas Bellinger ret = -EINVAL; 2208b8d26b3bSNicholas Bellinger break; 2209b8d26b3bSNicholas Bellinger } 2210b8d26b3bSNicholas Bellinger 2211b8d26b3bSNicholas Bellinger return ret; 2212b8d26b3bSNicholas Bellinger } 2213b8d26b3bSNicholas Bellinger 2214ca6c1d82SSagi Grimberg struct rdma_cm_id * 2215ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np) 2216ca6c1d82SSagi Grimberg { 2217ca6c1d82SSagi Grimberg struct iscsi_np *np = isert_np->np; 2218ca6c1d82SSagi Grimberg struct rdma_cm_id *id; 2219ca6c1d82SSagi Grimberg struct sockaddr *sa; 2220ca6c1d82SSagi Grimberg int ret; 2221ca6c1d82SSagi Grimberg 2222ca6c1d82SSagi Grimberg sa = (struct sockaddr *)&np->np_sockaddr; 222324f412ddSSagi Grimberg isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa); 2224ca6c1d82SSagi Grimberg 2225fa20105eSGuy Shapiro id = rdma_create_id(&init_net, isert_cma_handler, isert_np, 2226ca6c1d82SSagi Grimberg RDMA_PS_TCP, IB_QPT_RC); 2227ca6c1d82SSagi Grimberg if (IS_ERR(id)) { 222824f412ddSSagi Grimberg isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id)); 2229ca6c1d82SSagi Grimberg ret = PTR_ERR(id); 2230ca6c1d82SSagi Grimberg goto out; 2231ca6c1d82SSagi Grimberg } 223224f412ddSSagi Grimberg isert_dbg("id %p context %p\n", id, id->context); 2233ca6c1d82SSagi Grimberg 2234221442eaSMax Gurtovoy /* 2235221442eaSMax Gurtovoy * Allow both IPv4 and IPv6 sockets to bind a single port 2236221442eaSMax Gurtovoy * at the same time. 2237221442eaSMax Gurtovoy */ 2238221442eaSMax Gurtovoy ret = rdma_set_afonly(id, 1); 2239221442eaSMax Gurtovoy if (ret) { 2240221442eaSMax Gurtovoy isert_err("rdma_set_afonly() failed: %d\n", ret); 2241221442eaSMax Gurtovoy goto out_id; 2242221442eaSMax Gurtovoy } 2243221442eaSMax Gurtovoy 2244ca6c1d82SSagi Grimberg ret = rdma_bind_addr(id, sa); 2245ca6c1d82SSagi Grimberg if (ret) { 224624f412ddSSagi Grimberg isert_err("rdma_bind_addr() failed: %d\n", ret); 2247ca6c1d82SSagi Grimberg goto out_id; 2248ca6c1d82SSagi Grimberg } 2249ca6c1d82SSagi Grimberg 2250992607e8SSagi Grimberg ret = rdma_listen(id, 0); 2251ca6c1d82SSagi Grimberg if (ret) { 225224f412ddSSagi Grimberg isert_err("rdma_listen() failed: %d\n", ret); 2253ca6c1d82SSagi Grimberg goto out_id; 2254ca6c1d82SSagi Grimberg } 2255ca6c1d82SSagi Grimberg 2256ca6c1d82SSagi Grimberg return id; 2257ca6c1d82SSagi Grimberg out_id: 2258ca6c1d82SSagi Grimberg rdma_destroy_id(id); 2259ca6c1d82SSagi Grimberg out: 2260ca6c1d82SSagi Grimberg return ERR_PTR(ret); 2261ca6c1d82SSagi Grimberg } 2262ca6c1d82SSagi Grimberg 2263b8d26b3bSNicholas Bellinger static int 2264b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np, 226513a3cf08SAndy Grover struct sockaddr_storage *ksockaddr) 2266b8d26b3bSNicholas Bellinger { 2267b8d26b3bSNicholas Bellinger struct isert_np *isert_np; 2268b8d26b3bSNicholas Bellinger struct rdma_cm_id *isert_lid; 2269b8d26b3bSNicholas Bellinger int ret; 2270b8d26b3bSNicholas Bellinger 2271b8d26b3bSNicholas Bellinger isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL); 227293b80f29SLeon Romanovsky if (!isert_np) 2273b8d26b3bSNicholas Bellinger return -ENOMEM; 227493b80f29SLeon Romanovsky 2275ed8cb0a4SJenny Derzhavetz sema_init(&isert_np->sem, 0); 2276ed8cb0a4SJenny Derzhavetz mutex_init(&isert_np->mutex); 2277bd379220SJenny Derzhavetz INIT_LIST_HEAD(&isert_np->accepted); 2278bd379220SJenny Derzhavetz INIT_LIST_HEAD(&isert_np->pending); 2279ca6c1d82SSagi Grimberg isert_np->np = np; 2280b8d26b3bSNicholas Bellinger 2281b8d26b3bSNicholas Bellinger /* 2282b8d26b3bSNicholas Bellinger * Setup the np->np_sockaddr from the passed sockaddr setup 2283b8d26b3bSNicholas Bellinger * in iscsi_target_configfs.c code.. 2284b8d26b3bSNicholas Bellinger */ 2285b8d26b3bSNicholas Bellinger memcpy(&np->np_sockaddr, ksockaddr, 228613a3cf08SAndy Grover sizeof(struct sockaddr_storage)); 2287b8d26b3bSNicholas Bellinger 2288ca6c1d82SSagi Grimberg isert_lid = isert_setup_id(isert_np); 2289b8d26b3bSNicholas Bellinger if (IS_ERR(isert_lid)) { 2290b8d26b3bSNicholas Bellinger ret = PTR_ERR(isert_lid); 2291b8d26b3bSNicholas Bellinger goto out; 2292b8d26b3bSNicholas Bellinger } 2293b8d26b3bSNicholas Bellinger 2294ed8cb0a4SJenny Derzhavetz isert_np->cm_id = isert_lid; 2295b8d26b3bSNicholas Bellinger np->np_context = isert_np; 2296b8d26b3bSNicholas Bellinger 2297b8d26b3bSNicholas Bellinger return 0; 2298b8d26b3bSNicholas Bellinger 2299b8d26b3bSNicholas Bellinger out: 2300b8d26b3bSNicholas Bellinger kfree(isert_np); 2301ca6c1d82SSagi Grimberg 2302b8d26b3bSNicholas Bellinger return ret; 2303b8d26b3bSNicholas Bellinger } 2304b8d26b3bSNicholas Bellinger 2305b8d26b3bSNicholas Bellinger static int 2306b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn) 2307b8d26b3bSNicholas Bellinger { 2308dac6ab30SSagi Grimberg struct rdma_cm_id *cm_id = isert_conn->cm_id; 2309b8d26b3bSNicholas Bellinger struct rdma_conn_param cp; 2310b8d26b3bSNicholas Bellinger int ret; 231113bce482SJenny Derzhavetz struct iser_cm_hdr rsp_hdr; 2312b8d26b3bSNicholas Bellinger 2313b8d26b3bSNicholas Bellinger memset(&cp, 0, sizeof(struct rdma_conn_param)); 2314b8d26b3bSNicholas Bellinger cp.initiator_depth = isert_conn->initiator_depth; 2315b8d26b3bSNicholas Bellinger cp.retry_count = 7; 2316b8d26b3bSNicholas Bellinger cp.rnr_retry_count = 7; 2317b8d26b3bSNicholas Bellinger 231813bce482SJenny Derzhavetz memset(&rsp_hdr, 0, sizeof(rsp_hdr)); 2319422bd0acSJenny Derzhavetz rsp_hdr.flags = ISERT_ZBVA_NOT_USED; 2320422bd0acSJenny Derzhavetz if (!isert_conn->snd_w_inv) 2321422bd0acSJenny Derzhavetz rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED; 232213bce482SJenny Derzhavetz cp.private_data = (void *)&rsp_hdr; 232313bce482SJenny Derzhavetz cp.private_data_len = sizeof(rsp_hdr); 232413bce482SJenny Derzhavetz 2325b8d26b3bSNicholas Bellinger ret = rdma_accept(cm_id, &cp); 2326b8d26b3bSNicholas Bellinger if (ret) { 232724f412ddSSagi Grimberg isert_err("rdma_accept() failed with: %d\n", ret); 2328b8d26b3bSNicholas Bellinger return ret; 2329b8d26b3bSNicholas Bellinger } 2330b8d26b3bSNicholas Bellinger 2331b8d26b3bSNicholas Bellinger return 0; 2332b8d26b3bSNicholas Bellinger } 2333b8d26b3bSNicholas Bellinger 2334b8d26b3bSNicholas Bellinger static int 2335be36d683SMax Gurtovoy isert_get_login_rx(struct iscsit_conn *conn, struct iscsi_login *login) 2336b8d26b3bSNicholas Bellinger { 23376700425eSSagi Grimberg struct isert_conn *isert_conn = conn->context; 2338b8d26b3bSNicholas Bellinger int ret; 2339b8d26b3bSNicholas Bellinger 234024f412ddSSagi Grimberg isert_info("before login_req comp conn: %p\n", isert_conn); 23412371e5daSSagi Grimberg ret = wait_for_completion_interruptible(&isert_conn->login_req_comp); 23422371e5daSSagi Grimberg if (ret) { 234324f412ddSSagi Grimberg isert_err("isert_conn %p interrupted before got login req\n", 23442371e5daSSagi Grimberg isert_conn); 23452371e5daSSagi Grimberg return ret; 23462371e5daSSagi Grimberg } 23472371e5daSSagi Grimberg reinit_completion(&isert_conn->login_req_comp); 23482371e5daSSagi Grimberg 23496faaa85fSNicholas Bellinger /* 23506faaa85fSNicholas Bellinger * For login requests after the first PDU, isert_rx_login_req() will 2351549f39a5STetsuo Handa * kick queue_delayed_work(isert_login_wq, &conn->login_work) as 2352549f39a5STetsuo Handa * the packet is received, which turns this callback from 2353549f39a5STetsuo Handa * iscsi_target_do_login_rx() into a NOP. 23546faaa85fSNicholas Bellinger */ 23556faaa85fSNicholas Bellinger if (!login->first_request) 23566faaa85fSNicholas Bellinger return 0; 2357b8d26b3bSNicholas Bellinger 23582371e5daSSagi Grimberg isert_rx_login_req(isert_conn); 23592371e5daSSagi Grimberg 2360dac6ab30SSagi Grimberg isert_info("before login_comp conn: %p\n", conn); 2361dac6ab30SSagi Grimberg ret = wait_for_completion_interruptible(&isert_conn->login_comp); 2362b8d26b3bSNicholas Bellinger if (ret) 2363b8d26b3bSNicholas Bellinger return ret; 2364b8d26b3bSNicholas Bellinger 236524f412ddSSagi Grimberg isert_info("processing login->req: %p\n", login->req); 23662371e5daSSagi Grimberg 2367b8d26b3bSNicholas Bellinger return 0; 2368b8d26b3bSNicholas Bellinger } 2369b8d26b3bSNicholas Bellinger 2370b8d26b3bSNicholas Bellinger static void 2371be36d683SMax Gurtovoy isert_set_conn_info(struct iscsi_np *np, struct iscsit_conn *conn, 2372b8d26b3bSNicholas Bellinger struct isert_conn *isert_conn) 2373b8d26b3bSNicholas Bellinger { 2374dac6ab30SSagi Grimberg struct rdma_cm_id *cm_id = isert_conn->cm_id; 2375b8d26b3bSNicholas Bellinger struct rdma_route *cm_route = &cm_id->route; 2376b8d26b3bSNicholas Bellinger 2377b8d26b3bSNicholas Bellinger conn->login_family = np->np_sockaddr.ss_family; 2378b8d26b3bSNicholas Bellinger 2379dc58f760SAndy Grover conn->login_sockaddr = cm_route->addr.dst_addr; 2380dc58f760SAndy Grover conn->local_sockaddr = cm_route->addr.src_addr; 2381b8d26b3bSNicholas Bellinger } 2382b8d26b3bSNicholas Bellinger 2383b8d26b3bSNicholas Bellinger static int 2384be36d683SMax Gurtovoy isert_accept_np(struct iscsi_np *np, struct iscsit_conn *conn) 2385b8d26b3bSNicholas Bellinger { 23866700425eSSagi Grimberg struct isert_np *isert_np = np->np_context; 2387b8d26b3bSNicholas Bellinger struct isert_conn *isert_conn; 2388c6b8e918SSagi Grimberg int ret; 2389b8d26b3bSNicholas Bellinger 2390b8d26b3bSNicholas Bellinger accept_wait: 2391ed8cb0a4SJenny Derzhavetz ret = down_interruptible(&isert_np->sem); 2392c6b8e918SSagi Grimberg if (ret) 2393b8d26b3bSNicholas Bellinger return -ENODEV; 2394b8d26b3bSNicholas Bellinger 2395b8d26b3bSNicholas Bellinger spin_lock_bh(&np->np_thread_lock); 2396e346ab34SSagi Grimberg if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) { 2397b8d26b3bSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 239811378cdbSRasmus Villemoes isert_dbg("np_thread_state %d\n", 2399e346ab34SSagi Grimberg np->np_thread_state); 2400bf194997SLeon Romanovsky /* 2401e346ab34SSagi Grimberg * No point in stalling here when np_thread 2402e346ab34SSagi Grimberg * is in state RESET/SHUTDOWN/EXIT - bail 2403bf194997SLeon Romanovsky */ 2404b8d26b3bSNicholas Bellinger return -ENODEV; 2405b8d26b3bSNicholas Bellinger } 2406b8d26b3bSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 2407b8d26b3bSNicholas Bellinger 2408ed8cb0a4SJenny Derzhavetz mutex_lock(&isert_np->mutex); 2409bd379220SJenny Derzhavetz if (list_empty(&isert_np->pending)) { 2410ed8cb0a4SJenny Derzhavetz mutex_unlock(&isert_np->mutex); 2411b8d26b3bSNicholas Bellinger goto accept_wait; 2412b8d26b3bSNicholas Bellinger } 2413bd379220SJenny Derzhavetz isert_conn = list_first_entry(&isert_np->pending, 2414bd379220SJenny Derzhavetz struct isert_conn, node); 2415bd379220SJenny Derzhavetz list_del_init(&isert_conn->node); 2416ed8cb0a4SJenny Derzhavetz mutex_unlock(&isert_np->mutex); 2417b8d26b3bSNicholas Bellinger 2418b8d26b3bSNicholas Bellinger conn->context = isert_conn; 2419b8d26b3bSNicholas Bellinger isert_conn->conn = conn; 2420aea92980SJenny Derzhavetz isert_conn->state = ISER_CONN_BOUND; 2421b8d26b3bSNicholas Bellinger 2422b8d26b3bSNicholas Bellinger isert_set_conn_info(np, conn, isert_conn); 2423b8d26b3bSNicholas Bellinger 242424f412ddSSagi Grimberg isert_dbg("Processing isert_conn: %p\n", isert_conn); 24252371e5daSSagi Grimberg 2426b8d26b3bSNicholas Bellinger return 0; 2427b8d26b3bSNicholas Bellinger } 2428b8d26b3bSNicholas Bellinger 2429b8d26b3bSNicholas Bellinger static void 2430b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np) 2431b8d26b3bSNicholas Bellinger { 24326700425eSSagi Grimberg struct isert_np *isert_np = np->np_context; 2433268e6811SSagi Grimberg struct isert_conn *isert_conn, *n; 2434*691b0480SSaravanan Vajravel LIST_HEAD(drop_conn_list); 2435b8d26b3bSNicholas Bellinger 2436ed8cb0a4SJenny Derzhavetz if (isert_np->cm_id) 2437ed8cb0a4SJenny Derzhavetz rdma_destroy_id(isert_np->cm_id); 2438b8d26b3bSNicholas Bellinger 2439268e6811SSagi Grimberg /* 2440268e6811SSagi Grimberg * FIXME: At this point we don't have a good way to insure 2441268e6811SSagi Grimberg * that at this point we don't have hanging connections that 2442268e6811SSagi Grimberg * completed RDMA establishment but didn't start iscsi login 2443268e6811SSagi Grimberg * process. So work-around this by cleaning up what ever piled 2444bd379220SJenny Derzhavetz * up in accepted and pending lists. 2445268e6811SSagi Grimberg */ 2446ed8cb0a4SJenny Derzhavetz mutex_lock(&isert_np->mutex); 2447bd379220SJenny Derzhavetz if (!list_empty(&isert_np->pending)) { 2448bd379220SJenny Derzhavetz isert_info("Still have isert pending connections\n"); 2449268e6811SSagi Grimberg list_for_each_entry_safe(isert_conn, n, 2450bd379220SJenny Derzhavetz &isert_np->pending, 2451bd379220SJenny Derzhavetz node) { 2452bd379220SJenny Derzhavetz isert_info("cleaning isert_conn %p state (%d)\n", 2453bd379220SJenny Derzhavetz isert_conn, isert_conn->state); 2454*691b0480SSaravanan Vajravel list_move_tail(&isert_conn->node, &drop_conn_list); 2455bd379220SJenny Derzhavetz } 2456bd379220SJenny Derzhavetz } 2457bd379220SJenny Derzhavetz 2458bd379220SJenny Derzhavetz if (!list_empty(&isert_np->accepted)) { 2459bd379220SJenny Derzhavetz isert_info("Still have isert accepted connections\n"); 2460bd379220SJenny Derzhavetz list_for_each_entry_safe(isert_conn, n, 2461bd379220SJenny Derzhavetz &isert_np->accepted, 2462bd379220SJenny Derzhavetz node) { 246324f412ddSSagi Grimberg isert_info("cleaning isert_conn %p state (%d)\n", 2464268e6811SSagi Grimberg isert_conn, isert_conn->state); 2465*691b0480SSaravanan Vajravel list_move_tail(&isert_conn->node, &drop_conn_list); 2466268e6811SSagi Grimberg } 2467268e6811SSagi Grimberg } 2468ed8cb0a4SJenny Derzhavetz mutex_unlock(&isert_np->mutex); 2469268e6811SSagi Grimberg 2470*691b0480SSaravanan Vajravel list_for_each_entry_safe(isert_conn, n, &drop_conn_list, node) { 2471*691b0480SSaravanan Vajravel list_del_init(&isert_conn->node); 2472*691b0480SSaravanan Vajravel isert_connect_release(isert_conn); 2473*691b0480SSaravanan Vajravel } 2474*691b0480SSaravanan Vajravel 2475b8d26b3bSNicholas Bellinger np->np_context = NULL; 2476b8d26b3bSNicholas Bellinger kfree(isert_np); 2477b8d26b3bSNicholas Bellinger } 2478b8d26b3bSNicholas Bellinger 2479b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work) 2480b02efbfcSSagi Grimberg { 2481b02efbfcSSagi Grimberg struct isert_conn *isert_conn = container_of(work, 2482b02efbfcSSagi Grimberg struct isert_conn, 2483b02efbfcSSagi Grimberg release_work); 2484b02efbfcSSagi Grimberg 248524f412ddSSagi Grimberg isert_info("Starting release conn %p\n", isert_conn); 2486b02efbfcSSagi Grimberg 2487dac6ab30SSagi Grimberg mutex_lock(&isert_conn->mutex); 2488b02efbfcSSagi Grimberg isert_conn->state = ISER_CONN_DOWN; 2489dac6ab30SSagi Grimberg mutex_unlock(&isert_conn->mutex); 2490b02efbfcSSagi Grimberg 249124f412ddSSagi Grimberg isert_info("Destroying conn %p\n", isert_conn); 2492b02efbfcSSagi Grimberg isert_put_conn(isert_conn); 2493b02efbfcSSagi Grimberg } 2494b02efbfcSSagi Grimberg 2495bdf20e72SSagi Grimberg static void 2496991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn) 2497991bb764SSagi Grimberg { 2498be36d683SMax Gurtovoy struct iscsit_conn *conn = isert_conn->conn; 2499991bb764SSagi Grimberg 25004c22e07fSSagi Grimberg isert_info("conn %p\n", isert_conn); 25014c22e07fSSagi Grimberg 2502991bb764SSagi Grimberg if (isert_conn->logout_posted) { 250324f412ddSSagi Grimberg isert_info("conn %p wait for conn_logout_comp\n", isert_conn); 2504991bb764SSagi Grimberg wait_for_completion_timeout(&conn->conn_logout_comp, 2505991bb764SSagi Grimberg SECONDS_FOR_LOGOUT_COMP * HZ); 2506991bb764SSagi Grimberg } 2507991bb764SSagi Grimberg } 2508991bb764SSagi Grimberg 250976261adaSBart Van Assche static void 2510be36d683SMax Gurtovoy isert_wait4cmds(struct iscsit_conn *conn) 251176261adaSBart Van Assche { 2512be36d683SMax Gurtovoy isert_info("iscsit_conn %p\n", conn); 251376261adaSBart Van Assche 251476261adaSBart Van Assche if (conn->sess) { 25156d256beeSMike Christie target_stop_cmd_counter(conn->cmd_cnt); 25166d256beeSMike Christie target_wait_for_cmds(conn->cmd_cnt); 251776261adaSBart Van Assche } 251876261adaSBart Van Assche } 251976261adaSBart Van Assche 25203e03c4b0SJenny Derzhavetz /** 25213e03c4b0SJenny Derzhavetz * isert_put_unsol_pending_cmds() - Drop commands waiting for 25223e03c4b0SJenny Derzhavetz * unsolicitate dataout 25233e03c4b0SJenny Derzhavetz * @conn: iscsi connection 25243e03c4b0SJenny Derzhavetz * 25253e03c4b0SJenny Derzhavetz * We might still have commands that are waiting for unsolicited 25263e03c4b0SJenny Derzhavetz * dataouts messages. We must put the extra reference on those 25273e03c4b0SJenny Derzhavetz * before blocking on the target_wait_for_session_cmds 25283e03c4b0SJenny Derzhavetz */ 25293e03c4b0SJenny Derzhavetz static void 2530be36d683SMax Gurtovoy isert_put_unsol_pending_cmds(struct iscsit_conn *conn) 25313e03c4b0SJenny Derzhavetz { 253266cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd, *tmp; 25333e03c4b0SJenny Derzhavetz static LIST_HEAD(drop_cmd_list); 25343e03c4b0SJenny Derzhavetz 25353e03c4b0SJenny Derzhavetz spin_lock_bh(&conn->cmd_lock); 25363e03c4b0SJenny Derzhavetz list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) { 25373e03c4b0SJenny Derzhavetz if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) && 25383e03c4b0SJenny Derzhavetz (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) && 25393e03c4b0SJenny Derzhavetz (cmd->write_data_done < cmd->se_cmd.data_length)) 25403e03c4b0SJenny Derzhavetz list_move_tail(&cmd->i_conn_node, &drop_cmd_list); 25413e03c4b0SJenny Derzhavetz } 25423e03c4b0SJenny Derzhavetz spin_unlock_bh(&conn->cmd_lock); 25433e03c4b0SJenny Derzhavetz 25443e03c4b0SJenny Derzhavetz list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) { 25453e03c4b0SJenny Derzhavetz list_del_init(&cmd->i_conn_node); 25463e03c4b0SJenny Derzhavetz if (cmd->i_state != ISTATE_REMOVE) { 25473e03c4b0SJenny Derzhavetz struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd); 25483e03c4b0SJenny Derzhavetz 25493e03c4b0SJenny Derzhavetz isert_info("conn %p dropping cmd %p\n", conn, cmd); 25503e03c4b0SJenny Derzhavetz isert_put_cmd(isert_cmd, true); 25513e03c4b0SJenny Derzhavetz } 25523e03c4b0SJenny Derzhavetz } 25533e03c4b0SJenny Derzhavetz } 25543e03c4b0SJenny Derzhavetz 2555be36d683SMax Gurtovoy static void isert_wait_conn(struct iscsit_conn *conn) 2556b8d26b3bSNicholas Bellinger { 2557b8d26b3bSNicholas Bellinger struct isert_conn *isert_conn = conn->context; 2558b8d26b3bSNicholas Bellinger 25594c22e07fSSagi Grimberg isert_info("Starting conn %p\n", isert_conn); 2560b8d26b3bSNicholas Bellinger 2561dac6ab30SSagi Grimberg mutex_lock(&isert_conn->mutex); 2562954f2372SSagi Grimberg isert_conn_terminate(isert_conn); 2563dac6ab30SSagi Grimberg mutex_unlock(&isert_conn->mutex); 2564b2cb9649SNicholas Bellinger 2565572a1434SSagi Grimberg ib_drain_qp(isert_conn->qp); 25663e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(conn); 256776261adaSBart Van Assche isert_wait4cmds(conn); 2568991bb764SSagi Grimberg isert_wait4logout(isert_conn); 2569954f2372SSagi Grimberg 2570b02efbfcSSagi Grimberg queue_work(isert_release_wq, &isert_conn->release_work); 2571defd8848SNicholas Bellinger } 2572defd8848SNicholas Bellinger 2573be36d683SMax Gurtovoy static void isert_free_conn(struct iscsit_conn *conn) 2574defd8848SNicholas Bellinger { 2575defd8848SNicholas Bellinger struct isert_conn *isert_conn = conn->context; 2576b8d26b3bSNicholas Bellinger 2577572a1434SSagi Grimberg ib_drain_qp(isert_conn->qp); 2578b8d26b3bSNicholas Bellinger isert_put_conn(isert_conn); 2579b8d26b3bSNicholas Bellinger } 2580b8d26b3bSNicholas Bellinger 2581be36d683SMax Gurtovoy static void isert_get_rx_pdu(struct iscsit_conn *conn) 2582187e84f7SVarun Prakash { 2583187e84f7SVarun Prakash struct completion comp; 2584187e84f7SVarun Prakash 2585187e84f7SVarun Prakash init_completion(&comp); 2586187e84f7SVarun Prakash 2587187e84f7SVarun Prakash wait_for_completion_interruptible(&comp); 2588187e84f7SVarun Prakash } 2589187e84f7SVarun Prakash 2590b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = { 2591b8d26b3bSNicholas Bellinger .name = "IB/iSER", 2592b8d26b3bSNicholas Bellinger .transport_type = ISCSI_INFINIBAND, 2593bd027d85SNicholas Bellinger .rdma_shutdown = true, 2594d703ce2fSNicholas Bellinger .priv_size = sizeof(struct isert_cmd), 2595b8d26b3bSNicholas Bellinger .owner = THIS_MODULE, 2596b8d26b3bSNicholas Bellinger .iscsit_setup_np = isert_setup_np, 2597b8d26b3bSNicholas Bellinger .iscsit_accept_np = isert_accept_np, 2598b8d26b3bSNicholas Bellinger .iscsit_free_np = isert_free_np, 2599defd8848SNicholas Bellinger .iscsit_wait_conn = isert_wait_conn, 2600b8d26b3bSNicholas Bellinger .iscsit_free_conn = isert_free_conn, 2601b8d26b3bSNicholas Bellinger .iscsit_get_login_rx = isert_get_login_rx, 2602b8d26b3bSNicholas Bellinger .iscsit_put_login_tx = isert_put_login_tx, 2603b8d26b3bSNicholas Bellinger .iscsit_immediate_queue = isert_immediate_queue, 2604b8d26b3bSNicholas Bellinger .iscsit_response_queue = isert_response_queue, 2605b8d26b3bSNicholas Bellinger .iscsit_get_dataout = isert_get_dataout, 2606b8d26b3bSNicholas Bellinger .iscsit_queue_data_in = isert_put_datain, 2607b8d26b3bSNicholas Bellinger .iscsit_queue_status = isert_put_response, 2608131e6abcSNicholas Bellinger .iscsit_aborted_task = isert_aborted_task, 2609187e84f7SVarun Prakash .iscsit_get_rx_pdu = isert_get_rx_pdu, 2610e70beee7SNicholas Bellinger .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops, 2611b8d26b3bSNicholas Bellinger }; 2612b8d26b3bSNicholas Bellinger 2613b8d26b3bSNicholas Bellinger static int __init isert_init(void) 2614b8d26b3bSNicholas Bellinger { 2615549f39a5STetsuo Handa isert_login_wq = alloc_workqueue("isert_login_wq", 0, 0); 2616549f39a5STetsuo Handa if (!isert_login_wq) { 2617549f39a5STetsuo Handa isert_err("Unable to allocate isert_login_wq\n"); 2618549f39a5STetsuo Handa return -ENOMEM; 2619549f39a5STetsuo Handa } 2620b8d26b3bSNicholas Bellinger 2621631af550SSagi Grimberg isert_comp_wq = alloc_workqueue("isert_comp_wq", 2622631af550SSagi Grimberg WQ_UNBOUND | WQ_HIGHPRI, 0); 2623b8d26b3bSNicholas Bellinger if (!isert_comp_wq) { 262424f412ddSSagi Grimberg isert_err("Unable to allocate isert_comp_wq\n"); 2625549f39a5STetsuo Handa goto destroy_login_wq; 2626b8d26b3bSNicholas Bellinger } 2627b8d26b3bSNicholas Bellinger 2628b02efbfcSSagi Grimberg isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND, 2629b02efbfcSSagi Grimberg WQ_UNBOUND_MAX_ACTIVE); 2630b02efbfcSSagi Grimberg if (!isert_release_wq) { 263124f412ddSSagi Grimberg isert_err("Unable to allocate isert_release_wq\n"); 2632b02efbfcSSagi Grimberg goto destroy_comp_wq; 2633b02efbfcSSagi Grimberg } 2634b02efbfcSSagi Grimberg 2635b8d26b3bSNicholas Bellinger iscsit_register_transport(&iser_target_transport); 263624f412ddSSagi Grimberg isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n"); 2637b02efbfcSSagi Grimberg 2638b8d26b3bSNicholas Bellinger return 0; 2639b8d26b3bSNicholas Bellinger 2640b02efbfcSSagi Grimberg destroy_comp_wq: 2641b02efbfcSSagi Grimberg destroy_workqueue(isert_comp_wq); 2642549f39a5STetsuo Handa destroy_login_wq: 2643549f39a5STetsuo Handa destroy_workqueue(isert_login_wq); 26446f0fae3dSSagi Grimberg 2645549f39a5STetsuo Handa return -ENOMEM; 2646b8d26b3bSNicholas Bellinger } 2647b8d26b3bSNicholas Bellinger 2648b8d26b3bSNicholas Bellinger static void __exit isert_exit(void) 2649b8d26b3bSNicholas Bellinger { 2650549f39a5STetsuo Handa flush_workqueue(isert_login_wq); 2651b02efbfcSSagi Grimberg destroy_workqueue(isert_release_wq); 2652b8d26b3bSNicholas Bellinger destroy_workqueue(isert_comp_wq); 2653b8d26b3bSNicholas Bellinger iscsit_unregister_transport(&iser_target_transport); 26544c22e07fSSagi Grimberg isert_info("iSER_TARGET[0] - Released iser_target_transport\n"); 2655549f39a5STetsuo Handa destroy_workqueue(isert_login_wq); 2656b8d26b3bSNicholas Bellinger } 2657b8d26b3bSNicholas Bellinger 2658b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure"); 2659b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org"); 2660b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL"); 2661b8d26b3bSNicholas Bellinger 2662b8d26b3bSNicholas Bellinger module_init(isert_init); 2663b8d26b3bSNicholas Bellinger module_exit(isert_exit); 2664