xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision dae6345aa7a992fda77b5d0de286c9b238931fbd)
1b8d26b3bSNicholas Bellinger /*******************************************************************************
2b8d26b3bSNicholas Bellinger  * This file contains iSCSI extentions for RDMA (iSER) Verbs
3b8d26b3bSNicholas Bellinger  *
44c76251eSNicholas Bellinger  * (c) Copyright 2013 Datera, Inc.
5b8d26b3bSNicholas Bellinger  *
6b8d26b3bSNicholas Bellinger  * Nicholas A. Bellinger <nab@linux-iscsi.org>
7b8d26b3bSNicholas Bellinger  *
8b8d26b3bSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
9b8d26b3bSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
10b8d26b3bSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
11b8d26b3bSNicholas Bellinger  * (at your option) any later version.
12b8d26b3bSNicholas Bellinger  *
13b8d26b3bSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
14b8d26b3bSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15b8d26b3bSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16b8d26b3bSNicholas Bellinger  * GNU General Public License for more details.
17b8d26b3bSNicholas Bellinger  ****************************************************************************/
18b8d26b3bSNicholas Bellinger 
19b8d26b3bSNicholas Bellinger #include <linux/string.h>
20b8d26b3bSNicholas Bellinger #include <linux/module.h>
21b8d26b3bSNicholas Bellinger #include <linux/scatterlist.h>
22b8d26b3bSNicholas Bellinger #include <linux/socket.h>
23b8d26b3bSNicholas Bellinger #include <linux/in.h>
24b8d26b3bSNicholas Bellinger #include <linux/in6.h>
25b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h>
26b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h>
27b8d26b3bSNicholas Bellinger #include <target/target_core_base.h>
28b8d26b3bSNicholas Bellinger #include <target/target_core_fabric.h>
29b8d26b3bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
30531b7bf4SSagi Grimberg #include <linux/semaphore.h>
31b8d26b3bSNicholas Bellinger 
32b8d26b3bSNicholas Bellinger #include "ib_isert.h"
33b8d26b3bSNicholas Bellinger 
34b8d26b3bSNicholas Bellinger #define	ISERT_MAX_CONN		8
35b8d26b3bSNicholas Bellinger #define ISER_MAX_RX_CQ_LEN	(ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
3638a2d0d4SChristoph Hellwig #define ISER_MAX_TX_CQ_LEN \
3738a2d0d4SChristoph Hellwig 	((ISERT_QP_MAX_REQ_DTOS + ISCSI_DEF_XMIT_CMDS_MAX) * ISERT_MAX_CONN)
38bdf20e72SSagi Grimberg #define ISER_MAX_CQ_LEN		(ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39bdf20e72SSagi Grimberg 				 ISERT_MAX_CONN)
40b8d26b3bSNicholas Bellinger 
4145678b6bSSagi Grimberg static int isert_debug_level;
4224f412ddSSagi Grimberg module_param_named(debug_level, isert_debug_level, int, 0644);
4324f412ddSSagi Grimberg MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
4424f412ddSSagi Grimberg 
45b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
46b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
47b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
48b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
49b8d26b3bSNicholas Bellinger 
50f93f3a70SSagi Grimberg static int
51f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
522371e5daSSagi Grimberg static int
539679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn);
542371e5daSSagi Grimberg static int
552371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
56ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
57d40945d8SVu Pham 
582f1b6b7dSSagi Grimberg static void isert_release_work(struct work_struct *work);
599679cc51SChristoph Hellwig static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc);
609679cc51SChristoph Hellwig static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc);
619679cc51SChristoph Hellwig static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc);
629679cc51SChristoph Hellwig static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc);
632f1b6b7dSSagi Grimberg 
64302cc7c3SSagi Grimberg static inline bool
65302cc7c3SSagi Grimberg isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
66302cc7c3SSagi Grimberg {
6723a548eeSSagi Grimberg 	return (conn->pi_support &&
68302cc7c3SSagi Grimberg 		cmd->prot_op != TARGET_PROT_NORMAL);
69302cc7c3SSagi Grimberg }
70302cc7c3SSagi Grimberg 
71302cc7c3SSagi Grimberg 
72d40945d8SVu Pham static void
73b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
74b8d26b3bSNicholas Bellinger {
756700425eSSagi Grimberg 	struct isert_conn *isert_conn = context;
76b8d26b3bSNicholas Bellinger 
77ea8a1616SSagi Grimberg 	isert_err("%s (%d): conn %p\n",
78ea8a1616SSagi Grimberg 		  ib_event_msg(e->event), e->event, isert_conn);
79ea8a1616SSagi Grimberg 
80b8d26b3bSNicholas Bellinger 	switch (e->event) {
81b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
82dac6ab30SSagi Grimberg 		rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
83b8d26b3bSNicholas Bellinger 		break;
84b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
854c22e07fSSagi Grimberg 		isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
86b8d26b3bSNicholas Bellinger 		break;
87b8d26b3bSNicholas Bellinger 	default:
88b8d26b3bSNicholas Bellinger 		break;
89b8d26b3bSNicholas Bellinger 	}
90b8d26b3bSNicholas Bellinger }
91b8d26b3bSNicholas Bellinger 
9240fc069aSSagi Grimberg static struct isert_comp *
9340fc069aSSagi Grimberg isert_comp_get(struct isert_conn *isert_conn)
94b8d26b3bSNicholas Bellinger {
95dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
964a295baeSSagi Grimberg 	struct isert_comp *comp;
9740fc069aSSagi Grimberg 	int i, min = 0;
98b8d26b3bSNicholas Bellinger 
99b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
1004a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++)
1014a295baeSSagi Grimberg 		if (device->comps[i].active_qps <
1024a295baeSSagi Grimberg 		    device->comps[min].active_qps)
1034a295baeSSagi Grimberg 			min = i;
1044a295baeSSagi Grimberg 	comp = &device->comps[min];
1054a295baeSSagi Grimberg 	comp->active_qps++;
10640fc069aSSagi Grimberg 	mutex_unlock(&device_list_mutex);
10740fc069aSSagi Grimberg 
10824f412ddSSagi Grimberg 	isert_info("conn %p, using comp %p min_index: %d\n",
1094a295baeSSagi Grimberg 		   isert_conn, comp, min);
11040fc069aSSagi Grimberg 
11140fc069aSSagi Grimberg 	return comp;
11240fc069aSSagi Grimberg }
11340fc069aSSagi Grimberg 
11440fc069aSSagi Grimberg static void
11540fc069aSSagi Grimberg isert_comp_put(struct isert_comp *comp)
11640fc069aSSagi Grimberg {
11740fc069aSSagi Grimberg 	mutex_lock(&device_list_mutex);
11840fc069aSSagi Grimberg 	comp->active_qps--;
119b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
12040fc069aSSagi Grimberg }
12140fc069aSSagi Grimberg 
12240fc069aSSagi Grimberg static struct ib_qp *
12340fc069aSSagi Grimberg isert_create_qp(struct isert_conn *isert_conn,
12440fc069aSSagi Grimberg 		struct isert_comp *comp,
12540fc069aSSagi Grimberg 		struct rdma_cm_id *cma_id)
12640fc069aSSagi Grimberg {
127dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
12840fc069aSSagi Grimberg 	struct ib_qp_init_attr attr;
12940fc069aSSagi Grimberg 	int ret;
130b8d26b3bSNicholas Bellinger 
131b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
132b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
133b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
1346f0fae3dSSagi Grimberg 	attr.send_cq = comp->cq;
1356f0fae3dSSagi Grimberg 	attr.recv_cq = comp->cq;
136572a1434SSagi Grimberg 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS + 1;
137bdf20e72SSagi Grimberg 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
13838a2d0d4SChristoph Hellwig 	attr.cap.max_rdma_ctxs = ISCSI_DEF_XMIT_CMDS_MAX;
13933023fb8SSteve Wise 	attr.cap.max_send_sge = device->ib_device->attrs.max_send_sge;
140b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
141b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
142b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
143570db170SSagi Grimberg 	if (device->pi_capable)
144d3e125daSSagi Grimberg 		attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
145b8d26b3bSNicholas Bellinger 
14667cb3949SSagi Grimberg 	ret = rdma_create_qp(cma_id, device->pd, &attr);
147b8d26b3bSNicholas Bellinger 	if (ret) {
14824f412ddSSagi Grimberg 		isert_err("rdma_create_qp failed for cma_id %d\n", ret);
14940fc069aSSagi Grimberg 		return ERR_PTR(ret);
15040fc069aSSagi Grimberg 	}
15140fc069aSSagi Grimberg 
15240fc069aSSagi Grimberg 	return cma_id->qp;
15340fc069aSSagi Grimberg }
15440fc069aSSagi Grimberg 
15540fc069aSSagi Grimberg static int
15640fc069aSSagi Grimberg isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
15740fc069aSSagi Grimberg {
15840fc069aSSagi Grimberg 	struct isert_comp *comp;
15940fc069aSSagi Grimberg 	int ret;
16040fc069aSSagi Grimberg 
16140fc069aSSagi Grimberg 	comp = isert_comp_get(isert_conn);
162dac6ab30SSagi Grimberg 	isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
163dac6ab30SSagi Grimberg 	if (IS_ERR(isert_conn->qp)) {
164dac6ab30SSagi Grimberg 		ret = PTR_ERR(isert_conn->qp);
16519e2090fSSagi Grimberg 		goto err;
166b8d26b3bSNicholas Bellinger 	}
167b8d26b3bSNicholas Bellinger 
168b8d26b3bSNicholas Bellinger 	return 0;
16919e2090fSSagi Grimberg err:
17040fc069aSSagi Grimberg 	isert_comp_put(comp);
17119e2090fSSagi Grimberg 	return ret;
172b8d26b3bSNicholas Bellinger }
173b8d26b3bSNicholas Bellinger 
174b8d26b3bSNicholas Bellinger static int
175b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
176b8d26b3bSNicholas Bellinger {
177dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
17867cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
179b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
180b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
181b8d26b3bSNicholas Bellinger 	u64 dma_addr;
182b8d26b3bSNicholas Bellinger 	int i, j;
183b8d26b3bSNicholas Bellinger 
1846396bb22SKees Cook 	isert_conn->rx_descs = kcalloc(ISERT_QP_MAX_RECV_DTOS,
1856396bb22SKees Cook 				       sizeof(struct iser_rx_desc),
1866396bb22SKees Cook 				       GFP_KERNEL);
187dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
18893b80f29SLeon Romanovsky 		return -ENOMEM;
189b8d26b3bSNicholas Bellinger 
190dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
191b8d26b3bSNicholas Bellinger 
192b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
193b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
194b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
195b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
196b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
197b8d26b3bSNicholas Bellinger 
198b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
199b8d26b3bSNicholas Bellinger 
200b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
201b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
202b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
20334efc7dfSJason Gunthorpe 		rx_sg->lkey = device->pd->local_dma_lkey;
2049679cc51SChristoph Hellwig 		rx_desc->rx_cqe.done = isert_recv_done;
205b8d26b3bSNicholas Bellinger 	}
206b8d26b3bSNicholas Bellinger 
207b8d26b3bSNicholas Bellinger 	return 0;
208b8d26b3bSNicholas Bellinger 
209b8d26b3bSNicholas Bellinger dma_map_fail:
210dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
211b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
212b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
213b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
214b8d26b3bSNicholas Bellinger 	}
215dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
216dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
2174c22e07fSSagi Grimberg 	isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
218b8d26b3bSNicholas Bellinger 	return -ENOMEM;
219b8d26b3bSNicholas Bellinger }
220b8d26b3bSNicholas Bellinger 
221b8d26b3bSNicholas Bellinger static void
222b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
223b8d26b3bSNicholas Bellinger {
224dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
225b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
226b8d26b3bSNicholas Bellinger 	int i;
227b8d26b3bSNicholas Bellinger 
228dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
229b8d26b3bSNicholas Bellinger 		return;
230b8d26b3bSNicholas Bellinger 
231dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
232b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
233b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
234b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
235b8d26b3bSNicholas Bellinger 	}
236b8d26b3bSNicholas Bellinger 
237dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
238dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
239b8d26b3bSNicholas Bellinger }
240b8d26b3bSNicholas Bellinger 
241172369c5SSagi Grimberg static void
242172369c5SSagi Grimberg isert_free_comps(struct isert_device *device)
243b8d26b3bSNicholas Bellinger {
244172369c5SSagi Grimberg 	int i;
245b8d26b3bSNicholas Bellinger 
246172369c5SSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
247172369c5SSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
24859464ef4SVu Pham 
2499679cc51SChristoph Hellwig 		if (comp->cq)
2509679cc51SChristoph Hellwig 			ib_free_cq(comp->cq);
251172369c5SSagi Grimberg 	}
252172369c5SSagi Grimberg 	kfree(device->comps);
25359464ef4SVu Pham }
254d40945d8SVu Pham 
255172369c5SSagi Grimberg static int
2564a061b28SOr Gerlitz isert_alloc_comps(struct isert_device *device)
257172369c5SSagi Grimberg {
258172369c5SSagi Grimberg 	int i, max_cqe, ret = 0;
259d3e125daSSagi Grimberg 
2604a295baeSSagi Grimberg 	device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
2614a295baeSSagi Grimberg 				 device->ib_device->num_comp_vectors));
262172369c5SSagi Grimberg 
26324f412ddSSagi Grimberg 	isert_info("Using %d CQs, %s supports %d vectors support "
26438a2d0d4SChristoph Hellwig 		   "pi_capable %d\n",
2656c854111SJason Gunthorpe 		   device->comps_used, dev_name(&device->ib_device->dev),
26638a2d0d4SChristoph Hellwig 		   device->ib_device->num_comp_vectors,
267d3e125daSSagi Grimberg 		   device->pi_capable);
2684a295baeSSagi Grimberg 
2694a295baeSSagi Grimberg 	device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
2704a295baeSSagi Grimberg 				GFP_KERNEL);
27193b80f29SLeon Romanovsky 	if (!device->comps)
272b8d26b3bSNicholas Bellinger 		return -ENOMEM;
273b8d26b3bSNicholas Bellinger 
2744a061b28SOr Gerlitz 	max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
275172369c5SSagi Grimberg 
2764a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
2774a295baeSSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
278b8d26b3bSNicholas Bellinger 
2794a295baeSSagi Grimberg 		comp->device = device;
2809679cc51SChristoph Hellwig 		comp->cq = ib_alloc_cq(device->ib_device, comp, max_cqe, i,
2819679cc51SChristoph Hellwig 				IB_POLL_WORKQUEUE);
2826f0fae3dSSagi Grimberg 		if (IS_ERR(comp->cq)) {
283172369c5SSagi Grimberg 			isert_err("Unable to allocate cq\n");
2846f0fae3dSSagi Grimberg 			ret = PTR_ERR(comp->cq);
2856f0fae3dSSagi Grimberg 			comp->cq = NULL;
286b8d26b3bSNicholas Bellinger 			goto out_cq;
28794a71110SWei Yongjun 		}
288b8d26b3bSNicholas Bellinger 	}
289b8d26b3bSNicholas Bellinger 
290172369c5SSagi Grimberg 	return 0;
291172369c5SSagi Grimberg out_cq:
292172369c5SSagi Grimberg 	isert_free_comps(device);
293172369c5SSagi Grimberg 	return ret;
294172369c5SSagi Grimberg }
295172369c5SSagi Grimberg 
296172369c5SSagi Grimberg static int
297172369c5SSagi Grimberg isert_create_device_ib_res(struct isert_device *device)
298172369c5SSagi Grimberg {
2994a061b28SOr Gerlitz 	struct ib_device *ib_dev = device->ib_device;
300fd8205e8SSagi Grimberg 	int ret;
301172369c5SSagi Grimberg 
30233023fb8SSteve Wise 	isert_dbg("devattr->max_send_sge: %d devattr->max_recv_sge %d\n",
30333023fb8SSteve Wise 		  ib_dev->attrs.max_send_sge, ib_dev->attrs.max_recv_sge);
3044a061b28SOr Gerlitz 	isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
305172369c5SSagi Grimberg 
3064a061b28SOr Gerlitz 	ret = isert_alloc_comps(device);
307172369c5SSagi Grimberg 	if (ret)
308373a4cd7SNicholas Bellinger 		goto out;
309172369c5SSagi Grimberg 
310ed082d36SChristoph Hellwig 	device->pd = ib_alloc_pd(ib_dev, 0);
31167cb3949SSagi Grimberg 	if (IS_ERR(device->pd)) {
31267cb3949SSagi Grimberg 		ret = PTR_ERR(device->pd);
31367cb3949SSagi Grimberg 		isert_err("failed to allocate pd, device %p, ret=%d\n",
31467cb3949SSagi Grimberg 			  device, ret);
31567cb3949SSagi Grimberg 		goto out_cq;
31667cb3949SSagi Grimberg 	}
31767cb3949SSagi Grimberg 
318172369c5SSagi Grimberg 	/* Check signature cap */
3194a061b28SOr Gerlitz 	device->pi_capable = ib_dev->attrs.device_cap_flags &
320172369c5SSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
32167cb3949SSagi Grimberg 
322b8d26b3bSNicholas Bellinger 	return 0;
323b8d26b3bSNicholas Bellinger 
324b8d26b3bSNicholas Bellinger out_cq:
325172369c5SSagi Grimberg 	isert_free_comps(device);
326373a4cd7SNicholas Bellinger out:
327373a4cd7SNicholas Bellinger 	if (ret > 0)
328373a4cd7SNicholas Bellinger 		ret = -EINVAL;
329b8d26b3bSNicholas Bellinger 	return ret;
330b8d26b3bSNicholas Bellinger }
331b8d26b3bSNicholas Bellinger 
332b8d26b3bSNicholas Bellinger static void
333b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
334b8d26b3bSNicholas Bellinger {
33524f412ddSSagi Grimberg 	isert_info("device %p\n", device);
336b8d26b3bSNicholas Bellinger 
33767cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
338172369c5SSagi Grimberg 	isert_free_comps(device);
339b8d26b3bSNicholas Bellinger }
340b8d26b3bSNicholas Bellinger 
341b8d26b3bSNicholas Bellinger static void
342cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device)
343b8d26b3bSNicholas Bellinger {
344b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
345b8d26b3bSNicholas Bellinger 	device->refcount--;
3464c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
347b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
348b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
349b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
350b8d26b3bSNicholas Bellinger 		kfree(device);
351b8d26b3bSNicholas Bellinger 	}
352b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
353b8d26b3bSNicholas Bellinger }
354b8d26b3bSNicholas Bellinger 
355b8d26b3bSNicholas Bellinger static struct isert_device *
356cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id)
357b8d26b3bSNicholas Bellinger {
358b8d26b3bSNicholas Bellinger 	struct isert_device *device;
359b8d26b3bSNicholas Bellinger 	int ret;
360b8d26b3bSNicholas Bellinger 
361b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
362b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
363b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
364b8d26b3bSNicholas Bellinger 			device->refcount++;
3654c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
3664c22e07fSSagi Grimberg 				   device, device->refcount);
367b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
368b8d26b3bSNicholas Bellinger 			return device;
369b8d26b3bSNicholas Bellinger 		}
370b8d26b3bSNicholas Bellinger 	}
371b8d26b3bSNicholas Bellinger 
372b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
373b8d26b3bSNicholas Bellinger 	if (!device) {
374b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
375b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
376b8d26b3bSNicholas Bellinger 	}
377b8d26b3bSNicholas Bellinger 
378b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
379b8d26b3bSNicholas Bellinger 
380b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
381b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
382b8d26b3bSNicholas Bellinger 	if (ret) {
383b8d26b3bSNicholas Bellinger 		kfree(device);
384b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
385b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
386b8d26b3bSNicholas Bellinger 	}
387b8d26b3bSNicholas Bellinger 
388b8d26b3bSNicholas Bellinger 	device->refcount++;
389b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
3904c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
3914c22e07fSSagi Grimberg 		   device, device->refcount);
392b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
393b8d26b3bSNicholas Bellinger 
394b8d26b3bSNicholas Bellinger 	return device;
395b8d26b3bSNicholas Bellinger }
396b8d26b3bSNicholas Bellinger 
39759464ef4SVu Pham static void
398ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn)
399ae9ea9edSSagi Grimberg {
400ae9ea9edSSagi Grimberg 	isert_conn->state = ISER_CONN_INIT;
401bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_conn->node);
402dac6ab30SSagi Grimberg 	init_completion(&isert_conn->login_comp);
403ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
40463b268d2SRaju Rangoju 	init_waitqueue_head(&isert_conn->rem_wait);
405dac6ab30SSagi Grimberg 	kref_init(&isert_conn->kref);
406dac6ab30SSagi Grimberg 	mutex_init(&isert_conn->mutex);
4072f1b6b7dSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
408ae9ea9edSSagi Grimberg }
409ae9ea9edSSagi Grimberg 
410ae9ea9edSSagi Grimberg static void
411ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn)
412ae9ea9edSSagi Grimberg {
413dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
414ae9ea9edSSagi Grimberg 
415ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
416ed1083b2SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
4175adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
4185adabdd1SChristoph Hellwig 
419ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
4205adabdd1SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE,
421ae9ea9edSSagi Grimberg 			    DMA_FROM_DEVICE);
4225adabdd1SChristoph Hellwig 	kfree(isert_conn->login_req_buf);
423ae9ea9edSSagi Grimberg }
424ae9ea9edSSagi Grimberg 
425ae9ea9edSSagi Grimberg static int
426ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn,
427ae9ea9edSSagi Grimberg 		      struct ib_device *ib_dev)
428ae9ea9edSSagi Grimberg {
429ae9ea9edSSagi Grimberg 	int ret;
430ae9ea9edSSagi Grimberg 
4315adabdd1SChristoph Hellwig 	isert_conn->login_req_buf = kzalloc(sizeof(*isert_conn->login_req_buf),
4325adabdd1SChristoph Hellwig 			GFP_KERNEL);
43393b80f29SLeon Romanovsky 	if (!isert_conn->login_req_buf)
434ae9ea9edSSagi Grimberg 		return -ENOMEM;
435ae9ea9edSSagi Grimberg 
436ae9ea9edSSagi Grimberg 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
4375adabdd1SChristoph Hellwig 				isert_conn->login_req_buf,
4385adabdd1SChristoph Hellwig 				ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
439ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
440ae9ea9edSSagi Grimberg 	if (ret) {
441ae9ea9edSSagi Grimberg 		isert_err("login_req_dma mapping error: %d\n", ret);
442ae9ea9edSSagi Grimberg 		isert_conn->login_req_dma = 0;
4435adabdd1SChristoph Hellwig 		goto out_free_login_req_buf;
4445adabdd1SChristoph Hellwig 	}
4455adabdd1SChristoph Hellwig 
4465adabdd1SChristoph Hellwig 	isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
4475adabdd1SChristoph Hellwig 	if (!isert_conn->login_rsp_buf) {
4481d5840c9SWei Yongjun 		ret = -ENOMEM;
4495adabdd1SChristoph Hellwig 		goto out_unmap_login_req_buf;
450ae9ea9edSSagi Grimberg 	}
451ae9ea9edSSagi Grimberg 
452ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
4535adabdd1SChristoph Hellwig 					isert_conn->login_rsp_buf,
454ed1083b2SChristoph Hellwig 					ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
455ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
456ae9ea9edSSagi Grimberg 	if (ret) {
457ae9ea9edSSagi Grimberg 		isert_err("login_rsp_dma mapping error: %d\n", ret);
458ae9ea9edSSagi Grimberg 		isert_conn->login_rsp_dma = 0;
4595adabdd1SChristoph Hellwig 		goto out_free_login_rsp_buf;
460ae9ea9edSSagi Grimberg 	}
461ae9ea9edSSagi Grimberg 
462ae9ea9edSSagi Grimberg 	return 0;
463ae9ea9edSSagi Grimberg 
4645adabdd1SChristoph Hellwig out_free_login_rsp_buf:
4655adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
4665adabdd1SChristoph Hellwig out_unmap_login_req_buf:
467ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
4685adabdd1SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
4695adabdd1SChristoph Hellwig out_free_login_req_buf:
4705adabdd1SChristoph Hellwig 	kfree(isert_conn->login_req_buf);
471ae9ea9edSSagi Grimberg 	return ret;
472ae9ea9edSSagi Grimberg }
473ae9ea9edSSagi Grimberg 
474422bd0acSJenny Derzhavetz static void
475422bd0acSJenny Derzhavetz isert_set_nego_params(struct isert_conn *isert_conn,
476422bd0acSJenny Derzhavetz 		      struct rdma_conn_param *param)
477422bd0acSJenny Derzhavetz {
478422bd0acSJenny Derzhavetz 	struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
479422bd0acSJenny Derzhavetz 
480422bd0acSJenny Derzhavetz 	/* Set max inflight RDMA READ requests */
481422bd0acSJenny Derzhavetz 	isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
482422bd0acSJenny Derzhavetz 				attr->max_qp_init_rd_atom);
483422bd0acSJenny Derzhavetz 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
484422bd0acSJenny Derzhavetz 
485422bd0acSJenny Derzhavetz 	if (param->private_data) {
486422bd0acSJenny Derzhavetz 		u8 flags = *(u8 *)param->private_data;
487422bd0acSJenny Derzhavetz 
488422bd0acSJenny Derzhavetz 		/*
489422bd0acSJenny Derzhavetz 		 * use remote invalidation if the both initiator
490422bd0acSJenny Derzhavetz 		 * and the HCA support it
491422bd0acSJenny Derzhavetz 		 */
492422bd0acSJenny Derzhavetz 		isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
493422bd0acSJenny Derzhavetz 					  (attr->device_cap_flags &
494422bd0acSJenny Derzhavetz 					   IB_DEVICE_MEM_MGT_EXTENSIONS);
495422bd0acSJenny Derzhavetz 		if (isert_conn->snd_w_inv)
496422bd0acSJenny Derzhavetz 			isert_info("Using remote invalidation\n");
497422bd0acSJenny Derzhavetz 	}
498422bd0acSJenny Derzhavetz }
499422bd0acSJenny Derzhavetz 
500b8d26b3bSNicholas Bellinger static int
501b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
502b8d26b3bSNicholas Bellinger {
503ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
504ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
505b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
506b8d26b3bSNicholas Bellinger 	struct isert_device *device;
507b8d26b3bSNicholas Bellinger 	int ret = 0;
50814f4b54fSSagi Grimberg 
50914f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
51014f4b54fSSagi Grimberg 	if (!np->enabled) {
51114f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
51224f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
51314f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
51414f4b54fSSagi Grimberg 	}
51514f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
516b8d26b3bSNicholas Bellinger 
5174c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
518b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
519b8d26b3bSNicholas Bellinger 
520b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
521ae9ea9edSSagi Grimberg 	if (!isert_conn)
522b8d26b3bSNicholas Bellinger 		return -ENOMEM;
523b8d26b3bSNicholas Bellinger 
524ae9ea9edSSagi Grimberg 	isert_init_conn(isert_conn);
525dac6ab30SSagi Grimberg 	isert_conn->cm_id = cma_id;
526b8d26b3bSNicholas Bellinger 
527ae9ea9edSSagi Grimberg 	ret = isert_alloc_login_buf(isert_conn, cma_id->device);
528ae9ea9edSSagi Grimberg 	if (ret)
529b8d26b3bSNicholas Bellinger 		goto out;
530b8d26b3bSNicholas Bellinger 
531cf8ae958SSagi Grimberg 	device = isert_device_get(cma_id);
532b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
533b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
534b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
535b8d26b3bSNicholas Bellinger 	}
536dac6ab30SSagi Grimberg 	isert_conn->device = device;
537b8d26b3bSNicholas Bellinger 
538422bd0acSJenny Derzhavetz 	isert_set_nego_params(isert_conn, &event->param.conn);
5391a92e17eSSagi Grimberg 
540570db170SSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id);
541b8d26b3bSNicholas Bellinger 	if (ret)
542b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
543b8d26b3bSNicholas Bellinger 
5449679cc51SChristoph Hellwig 	ret = isert_login_post_recv(isert_conn);
5452371e5daSSagi Grimberg 	if (ret)
5462371e5daSSagi Grimberg 		goto out_conn_dev;
5472371e5daSSagi Grimberg 
5482371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
5492371e5daSSagi Grimberg 	if (ret)
5502371e5daSSagi Grimberg 		goto out_conn_dev;
5512371e5daSSagi Grimberg 
552ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
553bd379220SJenny Derzhavetz 	list_add_tail(&isert_conn->node, &isert_np->accepted);
554ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
555b8d26b3bSNicholas Bellinger 
556b8d26b3bSNicholas Bellinger 	return 0;
557b8d26b3bSNicholas Bellinger 
558b8d26b3bSNicholas Bellinger out_conn_dev:
559cf8ae958SSagi Grimberg 	isert_device_put(device);
560b8d26b3bSNicholas Bellinger out_rsp_dma_map:
561ae9ea9edSSagi Grimberg 	isert_free_login_buf(isert_conn);
562b8d26b3bSNicholas Bellinger out:
563b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
5642371e5daSSagi Grimberg 	rdma_reject(cma_id, NULL, 0);
565b8d26b3bSNicholas Bellinger 	return ret;
566b8d26b3bSNicholas Bellinger }
567b8d26b3bSNicholas Bellinger 
568b8d26b3bSNicholas Bellinger static void
569b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
570b8d26b3bSNicholas Bellinger {
571dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
572b8d26b3bSNicholas Bellinger 
5734c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
574b8d26b3bSNicholas Bellinger 
57557df81e3SSagi Grimberg 	BUG_ON(!device);
57657df81e3SSagi Grimberg 
57719e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
57863b268d2SRaju Rangoju 	if (isert_conn->cm_id &&
57963b268d2SRaju Rangoju 	    !isert_conn->dev_removed)
580dac6ab30SSagi Grimberg 		rdma_destroy_id(isert_conn->cm_id);
58119e2090fSSagi Grimberg 
582dac6ab30SSagi Grimberg 	if (isert_conn->qp) {
583dac6ab30SSagi Grimberg 		struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
5844a295baeSSagi Grimberg 
58540fc069aSSagi Grimberg 		isert_comp_put(comp);
586dac6ab30SSagi Grimberg 		ib_destroy_qp(isert_conn->qp);
587b8d26b3bSNicholas Bellinger 	}
588b8d26b3bSNicholas Bellinger 
5895adabdd1SChristoph Hellwig 	if (isert_conn->login_req_buf)
590ae9ea9edSSagi Grimberg 		isert_free_login_buf(isert_conn);
591ae9ea9edSSagi Grimberg 
592cf8ae958SSagi Grimberg 	isert_device_put(device);
59357df81e3SSagi Grimberg 
59463b268d2SRaju Rangoju 	if (isert_conn->dev_removed)
59563b268d2SRaju Rangoju 		wake_up_interruptible(&isert_conn->rem_wait);
59663b268d2SRaju Rangoju 	else
59757df81e3SSagi Grimberg 		kfree(isert_conn);
598b8d26b3bSNicholas Bellinger }
599b8d26b3bSNicholas Bellinger 
600b8d26b3bSNicholas Bellinger static void
601b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
602b8d26b3bSNicholas Bellinger {
60319e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
604bd379220SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
605c2f88b17SSagi Grimberg 
60624f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
607128e9cc8SSagi Grimberg 
608dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
609128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_UP;
610bd379220SJenny Derzhavetz 	kref_get(&isert_conn->kref);
611dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
612bd379220SJenny Derzhavetz 
613bd379220SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
614bd379220SJenny Derzhavetz 	list_move_tail(&isert_conn->node, &isert_np->pending);
615bd379220SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
616bd379220SJenny Derzhavetz 
617bd379220SJenny Derzhavetz 	isert_info("np %p: Allow accept_np to continue\n", isert_np);
618bd379220SJenny Derzhavetz 	up(&isert_np->sem);
619b8d26b3bSNicholas Bellinger }
620b8d26b3bSNicholas Bellinger 
621b8d26b3bSNicholas Bellinger static void
622dac6ab30SSagi Grimberg isert_release_kref(struct kref *kref)
623b8d26b3bSNicholas Bellinger {
624b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
625dac6ab30SSagi Grimberg 				struct isert_conn, kref);
626b8d26b3bSNicholas Bellinger 
6274c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
6284c22e07fSSagi Grimberg 		   current->pid);
629b8d26b3bSNicholas Bellinger 
630b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
631b8d26b3bSNicholas Bellinger }
632b8d26b3bSNicholas Bellinger 
633b8d26b3bSNicholas Bellinger static void
634b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
635b8d26b3bSNicholas Bellinger {
636dac6ab30SSagi Grimberg 	kref_put(&isert_conn->kref, isert_release_kref);
637b8d26b3bSNicholas Bellinger }
638b8d26b3bSNicholas Bellinger 
6396d1fba0cSJenny Derzhavetz static void
6406d1fba0cSJenny Derzhavetz isert_handle_unbound_conn(struct isert_conn *isert_conn)
6416d1fba0cSJenny Derzhavetz {
6426d1fba0cSJenny Derzhavetz 	struct isert_np *isert_np = isert_conn->cm_id->context;
6436d1fba0cSJenny Derzhavetz 
6446d1fba0cSJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
6456d1fba0cSJenny Derzhavetz 	if (!list_empty(&isert_conn->node)) {
6466d1fba0cSJenny Derzhavetz 		/*
6476d1fba0cSJenny Derzhavetz 		 * This means iscsi doesn't know this connection
6486d1fba0cSJenny Derzhavetz 		 * so schedule a cleanup ourselves
6496d1fba0cSJenny Derzhavetz 		 */
6506d1fba0cSJenny Derzhavetz 		list_del_init(&isert_conn->node);
6516d1fba0cSJenny Derzhavetz 		isert_put_conn(isert_conn);
6526d1fba0cSJenny Derzhavetz 		queue_work(isert_release_wq, &isert_conn->release_work);
6536d1fba0cSJenny Derzhavetz 	}
6546d1fba0cSJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
6556d1fba0cSJenny Derzhavetz }
6566d1fba0cSJenny Derzhavetz 
657954f2372SSagi Grimberg /**
658954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
659954f2372SSagi Grimberg  * @isert_conn: isert connection struct
660954f2372SSagi Grimberg  *
661954f2372SSagi Grimberg  * Notes:
662aea92980SJenny Derzhavetz  * In case the connection state is BOUND, move state
663954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
664128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
665954f2372SSagi Grimberg  *
666dac6ab30SSagi Grimberg  * This routine must be called with mutex held. Thus it is
667954f2372SSagi Grimberg  * safe to call multiple times.
668954f2372SSagi Grimberg  */
669954f2372SSagi Grimberg static void
670954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
671954f2372SSagi Grimberg {
672954f2372SSagi Grimberg 	int err;
673954f2372SSagi Grimberg 
6746d1fba0cSJenny Derzhavetz 	if (isert_conn->state >= ISER_CONN_TERMINATING)
6756d1fba0cSJenny Derzhavetz 		return;
6766d1fba0cSJenny Derzhavetz 
67724f412ddSSagi Grimberg 	isert_info("Terminating conn %p state %d\n",
678954f2372SSagi Grimberg 		   isert_conn, isert_conn->state);
679128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_TERMINATING;
680dac6ab30SSagi Grimberg 	err = rdma_disconnect(isert_conn->cm_id);
681954f2372SSagi Grimberg 	if (err)
68224f412ddSSagi Grimberg 		isert_warn("Failed rdma_disconnect isert_conn %p\n",
683954f2372SSagi Grimberg 			   isert_conn);
684954f2372SSagi Grimberg }
685954f2372SSagi Grimberg 
6863b726ae2SSagi Grimberg static int
687ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
688ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
689b8d26b3bSNicholas Bellinger {
690ea8a1616SSagi Grimberg 	isert_dbg("%s (%d): isert np %p\n",
691ea8a1616SSagi Grimberg 		  rdma_event_msg(event), event, isert_np);
6923b726ae2SSagi Grimberg 
693ca6c1d82SSagi Grimberg 	switch (event) {
694ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
695ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = NULL;
696ca6c1d82SSagi Grimberg 		break;
697ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
698ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = isert_setup_id(isert_np);
699ed8cb0a4SJenny Derzhavetz 		if (IS_ERR(isert_np->cm_id)) {
70024f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
701ed8cb0a4SJenny Derzhavetz 				  isert_np, PTR_ERR(isert_np->cm_id));
702ed8cb0a4SJenny Derzhavetz 			isert_np->cm_id = NULL;
703ca6c1d82SSagi Grimberg 		}
704ca6c1d82SSagi Grimberg 		break;
705ca6c1d82SSagi Grimberg 	default:
70624f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
707ca6c1d82SSagi Grimberg 			  isert_np, event);
708ca6c1d82SSagi Grimberg 	}
709ca6c1d82SSagi Grimberg 
7103b726ae2SSagi Grimberg 	return -1;
7113b726ae2SSagi Grimberg }
7123b726ae2SSagi Grimberg 
713ca6c1d82SSagi Grimberg static int
714ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
715ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
716ca6c1d82SSagi Grimberg {
717f81bf458SJenny Derzhavetz 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
718ca6c1d82SSagi Grimberg 
719dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
7206d1fba0cSJenny Derzhavetz 	switch (isert_conn->state) {
7216d1fba0cSJenny Derzhavetz 	case ISER_CONN_TERMINATING:
7226d1fba0cSJenny Derzhavetz 		break;
7236d1fba0cSJenny Derzhavetz 	case ISER_CONN_UP:
724128e9cc8SSagi Grimberg 		isert_conn_terminate(isert_conn);
725572a1434SSagi Grimberg 		ib_drain_qp(isert_conn->qp);
7266d1fba0cSJenny Derzhavetz 		isert_handle_unbound_conn(isert_conn);
7276d1fba0cSJenny Derzhavetz 		break;
7286d1fba0cSJenny Derzhavetz 	case ISER_CONN_BOUND:
7296d1fba0cSJenny Derzhavetz 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
7306d1fba0cSJenny Derzhavetz 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
7316d1fba0cSJenny Derzhavetz 		break;
7326d1fba0cSJenny Derzhavetz 	default:
733506f71d1SColin Ian King 		isert_warn("conn %p terminating in state %d\n",
7346d1fba0cSJenny Derzhavetz 			   isert_conn, isert_conn->state);
7356d1fba0cSJenny Derzhavetz 	}
736dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
737128e9cc8SSagi Grimberg 
7383b726ae2SSagi Grimberg 	return 0;
739b8d26b3bSNicholas Bellinger }
740b8d26b3bSNicholas Bellinger 
7414a579da2SSagi Grimberg static int
742954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
743954f2372SSagi Grimberg {
74419e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
745954f2372SSagi Grimberg 
746cd52cb26SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
747bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
748dac6ab30SSagi Grimberg 	isert_conn->cm_id = NULL;
749954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
7504a579da2SSagi Grimberg 
7514a579da2SSagi Grimberg 	return -1;
752954f2372SSagi Grimberg }
753954f2372SSagi Grimberg 
754b8d26b3bSNicholas Bellinger static int
755b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
756b8d26b3bSNicholas Bellinger {
757f81bf458SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
75863b268d2SRaju Rangoju 	struct isert_conn *isert_conn;
759b8d26b3bSNicholas Bellinger 	int ret = 0;
760b8d26b3bSNicholas Bellinger 
761ea8a1616SSagi Grimberg 	isert_info("%s (%d): status %d id %p np %p\n",
762ea8a1616SSagi Grimberg 		   rdma_event_msg(event->event), event->event,
7634c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
764b8d26b3bSNicholas Bellinger 
765f81bf458SJenny Derzhavetz 	if (isert_np->cm_id == cma_id)
766f81bf458SJenny Derzhavetz 		return isert_np_cma_handler(cma_id->context, event->event);
767f81bf458SJenny Derzhavetz 
768b8d26b3bSNicholas Bellinger 	switch (event->event) {
769b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
770b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
7713b726ae2SSagi Grimberg 		if (ret)
7724c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
773b8d26b3bSNicholas Bellinger 		break;
774b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
775b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
776b8d26b3bSNicholas Bellinger 		break;
77788c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
77888c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
77988c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
780ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
781b8d26b3bSNicholas Bellinger 		break;
78263b268d2SRaju Rangoju 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
78363b268d2SRaju Rangoju 		isert_conn = cma_id->qp->qp_context;
78463b268d2SRaju Rangoju 		isert_conn->dev_removed = true;
78563b268d2SRaju Rangoju 		isert_disconnected_handler(cma_id, event->event);
78663b268d2SRaju Rangoju 		wait_event_interruptible(isert_conn->rem_wait,
78763b268d2SRaju Rangoju 					 isert_conn->state == ISER_CONN_DOWN);
78863b268d2SRaju Rangoju 		kfree(isert_conn);
78963b268d2SRaju Rangoju 		/*
79063b268d2SRaju Rangoju 		 * return non-zero from the callback to destroy
79163b268d2SRaju Rangoju 		 * the rdma cm id
79263b268d2SRaju Rangoju 		 */
79363b268d2SRaju Rangoju 		return 1;
794d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_REJECTED:
7951e38a366SSteve Wise 		isert_info("Connection rejected: %s\n",
7961e38a366SSteve Wise 			   rdma_reject_msg(cma_id, event->status));
797d2271bd0SBart Van Assche 		/* fall through */
798d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_UNREACHABLE:
799b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
8004a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
801954f2372SSagi Grimberg 		break;
802b8d26b3bSNicholas Bellinger 	default:
80324f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
804b8d26b3bSNicholas Bellinger 		break;
805b8d26b3bSNicholas Bellinger 	}
806b8d26b3bSNicholas Bellinger 
807b8d26b3bSNicholas Bellinger 	return ret;
808b8d26b3bSNicholas Bellinger }
809b8d26b3bSNicholas Bellinger 
810b8d26b3bSNicholas Bellinger static int
8114366b19cSJenny Derzhavetz isert_post_recvm(struct isert_conn *isert_conn, u32 count)
812b8d26b3bSNicholas Bellinger {
813e01a7674SBart Van Assche 	struct ib_recv_wr *rx_wr;
814b8d26b3bSNicholas Bellinger 	int i, ret;
815b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
816b8d26b3bSNicholas Bellinger 
817dac6ab30SSagi Grimberg 	for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
8184366b19cSJenny Derzhavetz 		rx_desc = &isert_conn->rx_descs[i];
8199679cc51SChristoph Hellwig 
8209679cc51SChristoph Hellwig 		rx_wr->wr_cqe = &rx_desc->rx_cqe;
821b8d26b3bSNicholas Bellinger 		rx_wr->sg_list = &rx_desc->rx_sg;
822b8d26b3bSNicholas Bellinger 		rx_wr->num_sge = 1;
823b8d26b3bSNicholas Bellinger 		rx_wr->next = rx_wr + 1;
8247a56dc88SSagi Grimberg 		rx_desc->in_use = false;
825b8d26b3bSNicholas Bellinger 	}
826b8d26b3bSNicholas Bellinger 	rx_wr--;
827b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
828b8d26b3bSNicholas Bellinger 
829e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr, NULL);
8306d1fba0cSJenny Derzhavetz 	if (ret)
83124f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
8324366b19cSJenny Derzhavetz 
8334366b19cSJenny Derzhavetz 	return ret;
8344366b19cSJenny Derzhavetz }
8354366b19cSJenny Derzhavetz 
8364366b19cSJenny Derzhavetz static int
8374366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
8384366b19cSJenny Derzhavetz {
839e01a7674SBart Van Assche 	struct ib_recv_wr rx_wr;
8404366b19cSJenny Derzhavetz 	int ret;
8414366b19cSJenny Derzhavetz 
8427a56dc88SSagi Grimberg 	if (!rx_desc->in_use) {
8437a56dc88SSagi Grimberg 		/*
8447a56dc88SSagi Grimberg 		 * if the descriptor is not in-use we already reposted it
8457a56dc88SSagi Grimberg 		 * for recv, so just silently return
8467a56dc88SSagi Grimberg 		 */
8477a56dc88SSagi Grimberg 		return 0;
8487a56dc88SSagi Grimberg 	}
8497a56dc88SSagi Grimberg 
8507a56dc88SSagi Grimberg 	rx_desc->in_use = false;
8519679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &rx_desc->rx_cqe;
8524366b19cSJenny Derzhavetz 	rx_wr.sg_list = &rx_desc->rx_sg;
8534366b19cSJenny Derzhavetz 	rx_wr.num_sge = 1;
8544366b19cSJenny Derzhavetz 	rx_wr.next = NULL;
8554366b19cSJenny Derzhavetz 
856e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL);
8576d1fba0cSJenny Derzhavetz 	if (ret)
8584366b19cSJenny Derzhavetz 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
8594366b19cSJenny Derzhavetz 
860b8d26b3bSNicholas Bellinger 	return ret;
861b8d26b3bSNicholas Bellinger }
862b8d26b3bSNicholas Bellinger 
863b8d26b3bSNicholas Bellinger static int
8649679cc51SChristoph Hellwig isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
865b8d26b3bSNicholas Bellinger {
866dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
867e01a7674SBart Van Assche 	struct ib_send_wr send_wr;
868b8d26b3bSNicholas Bellinger 	int ret;
869b8d26b3bSNicholas Bellinger 
870b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
871b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
872b8d26b3bSNicholas Bellinger 
8739679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_login_send_done;
8749679cc51SChristoph Hellwig 
875b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
8769679cc51SChristoph Hellwig 	send_wr.wr_cqe	= &tx_desc->tx_cqe;
877b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
878b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
879b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
880b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
881b8d26b3bSNicholas Bellinger 
882e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &send_wr, NULL);
883bdf20e72SSagi Grimberg 	if (ret)
88424f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
885b8d26b3bSNicholas Bellinger 
886b8d26b3bSNicholas Bellinger 	return ret;
887b8d26b3bSNicholas Bellinger }
888b8d26b3bSNicholas Bellinger 
889b8d26b3bSNicholas Bellinger static void
890763b6965SAlex Estrin __isert_create_send_desc(struct isert_device *device,
891b8d26b3bSNicholas Bellinger 			 struct iser_tx_desc *tx_desc)
892b8d26b3bSNicholas Bellinger {
893b8d26b3bSNicholas Bellinger 
894d3cf81f9SSagi Grimberg 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
895d3cf81f9SSagi Grimberg 	tx_desc->iser_header.flags = ISCSI_CTRL;
896b8d26b3bSNicholas Bellinger 
897b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
898b8d26b3bSNicholas Bellinger 
89934efc7dfSJason Gunthorpe 	if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
90034efc7dfSJason Gunthorpe 		tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
90124f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
902b8d26b3bSNicholas Bellinger 	}
903b8d26b3bSNicholas Bellinger }
904b8d26b3bSNicholas Bellinger 
905763b6965SAlex Estrin static void
906763b6965SAlex Estrin isert_create_send_desc(struct isert_conn *isert_conn,
907763b6965SAlex Estrin 		       struct isert_cmd *isert_cmd,
908763b6965SAlex Estrin 		       struct iser_tx_desc *tx_desc)
909763b6965SAlex Estrin {
910763b6965SAlex Estrin 	struct isert_device *device = isert_conn->device;
911763b6965SAlex Estrin 	struct ib_device *ib_dev = device->ib_device;
912763b6965SAlex Estrin 
913763b6965SAlex Estrin 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
914763b6965SAlex Estrin 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
915763b6965SAlex Estrin 
916763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
917763b6965SAlex Estrin }
918763b6965SAlex Estrin 
919b8d26b3bSNicholas Bellinger static int
920b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
921b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
922b8d26b3bSNicholas Bellinger {
923dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
92467cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
925b8d26b3bSNicholas Bellinger 	u64 dma_addr;
926b8d26b3bSNicholas Bellinger 
927b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
928b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
929b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
93024f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
931b8d26b3bSNicholas Bellinger 		return -ENOMEM;
932b8d26b3bSNicholas Bellinger 	}
933b8d26b3bSNicholas Bellinger 
934b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
935b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
936b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
93734efc7dfSJason Gunthorpe 	tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
938b8d26b3bSNicholas Bellinger 
9394c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
9404c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
9414c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
942b8d26b3bSNicholas Bellinger 
943b8d26b3bSNicholas Bellinger 	return 0;
944b8d26b3bSNicholas Bellinger }
945b8d26b3bSNicholas Bellinger 
946b8d26b3bSNicholas Bellinger static void
94795b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
94868a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
949b8d26b3bSNicholas Bellinger {
95095b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
95195b60f07SNicholas Bellinger 
9529679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_send_done;
9539679cc51SChristoph Hellwig 	send_wr->wr_cqe = &tx_desc->tx_cqe;
954422bd0acSJenny Derzhavetz 
955422bd0acSJenny Derzhavetz 	if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
956422bd0acSJenny Derzhavetz 		send_wr->opcode  = IB_WR_SEND_WITH_INV;
957422bd0acSJenny Derzhavetz 		send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
958422bd0acSJenny Derzhavetz 	} else {
959b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_SEND;
960422bd0acSJenny Derzhavetz 	}
961422bd0acSJenny Derzhavetz 
96295b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
963b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
96495b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
965b8d26b3bSNicholas Bellinger }
966b8d26b3bSNicholas Bellinger 
967b8d26b3bSNicholas Bellinger static int
9689679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn)
969b8d26b3bSNicholas Bellinger {
970e01a7674SBart Van Assche 	struct ib_recv_wr rx_wr;
971b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
972b8d26b3bSNicholas Bellinger 	int ret;
973b8d26b3bSNicholas Bellinger 
974b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
975b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
976ed1083b2SChristoph Hellwig 	sge.length = ISER_RX_PAYLOAD_SIZE;
97734efc7dfSJason Gunthorpe 	sge.lkey = isert_conn->device->pd->local_dma_lkey;
978b8d26b3bSNicholas Bellinger 
97924f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
980b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
981b8d26b3bSNicholas Bellinger 
9829679cc51SChristoph Hellwig 	isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
9839679cc51SChristoph Hellwig 
984b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
9859679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
986b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
987b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
988b8d26b3bSNicholas Bellinger 
989e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL);
9906d1fba0cSJenny Derzhavetz 	if (ret)
99124f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
992b8d26b3bSNicholas Bellinger 
993b8d26b3bSNicholas Bellinger 	return ret;
994b8d26b3bSNicholas Bellinger }
995b8d26b3bSNicholas Bellinger 
996b8d26b3bSNicholas Bellinger static int
997b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
998b8d26b3bSNicholas Bellinger 		   u32 length)
999b8d26b3bSNicholas Bellinger {
1000b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1001dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
100267cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1003dac6ab30SSagi Grimberg 	struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
1004b8d26b3bSNicholas Bellinger 	int ret;
1005b8d26b3bSNicholas Bellinger 
1006763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
1007b8d26b3bSNicholas Bellinger 
1008b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1009b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1010b8d26b3bSNicholas Bellinger 
1011b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1012b8d26b3bSNicholas Bellinger 
1013b8d26b3bSNicholas Bellinger 	if (length > 0) {
1014b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1015b8d26b3bSNicholas Bellinger 
1016b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1017b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1018b8d26b3bSNicholas Bellinger 
1019b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1020b8d26b3bSNicholas Bellinger 
1021b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1022b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1023b8d26b3bSNicholas Bellinger 
1024b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1025b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
102634efc7dfSJason Gunthorpe 		tx_dsg->lkey	= isert_conn->device->pd->local_dma_lkey;
1027b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1028b8d26b3bSNicholas Bellinger 	}
1029b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1030b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1031b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1032b8d26b3bSNicholas Bellinger 			if (ret)
1033b8d26b3bSNicholas Bellinger 				return ret;
1034b8d26b3bSNicholas Bellinger 
10354366b19cSJenny Derzhavetz 			ret = isert_post_recvm(isert_conn,
10364366b19cSJenny Derzhavetz 					       ISERT_QP_MAX_RECV_DTOS);
1037b8d26b3bSNicholas Bellinger 			if (ret)
1038b8d26b3bSNicholas Bellinger 				return ret;
1039b8d26b3bSNicholas Bellinger 
1040128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
1041dac6ab30SSagi Grimberg 			mutex_lock(&isert_conn->mutex);
1042128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
1043dac6ab30SSagi Grimberg 			mutex_unlock(&isert_conn->mutex);
1044b8d26b3bSNicholas Bellinger 			goto post_send;
1045b8d26b3bSNicholas Bellinger 		}
1046b8d26b3bSNicholas Bellinger 
10479679cc51SChristoph Hellwig 		ret = isert_login_post_recv(isert_conn);
1048b8d26b3bSNicholas Bellinger 		if (ret)
1049b8d26b3bSNicholas Bellinger 			return ret;
1050b8d26b3bSNicholas Bellinger 	}
1051b8d26b3bSNicholas Bellinger post_send:
10529679cc51SChristoph Hellwig 	ret = isert_login_post_send(isert_conn, tx_desc);
1053b8d26b3bSNicholas Bellinger 	if (ret)
1054b8d26b3bSNicholas Bellinger 		return ret;
1055b8d26b3bSNicholas Bellinger 
1056b8d26b3bSNicholas Bellinger 	return 0;
1057b8d26b3bSNicholas Bellinger }
1058b8d26b3bSNicholas Bellinger 
1059b8d26b3bSNicholas Bellinger static void
10602371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1061b8d26b3bSNicholas Bellinger {
10625adabdd1SChristoph Hellwig 	struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
10632371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1064b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1065b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1066b8d26b3bSNicholas Bellinger 	int size;
1067b8d26b3bSNicholas Bellinger 
106824f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
10692371e5daSSagi Grimberg 
10702371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1071b8d26b3bSNicholas Bellinger 
1072b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1073b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1074b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1075b8d26b3bSNicholas Bellinger 		/*
1076b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1077b8d26b3bSNicholas Bellinger 		 * login request PDU.
1078b8d26b3bSNicholas Bellinger 		 */
1079b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1080b8d26b3bSNicholas Bellinger 		login->current_stage =
1081b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1082b8d26b3bSNicholas Bellinger 			 >> 2;
1083b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1084b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1085b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1086b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1087b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1088b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1089b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1090b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1091b8d26b3bSNicholas Bellinger 	}
1092b8d26b3bSNicholas Bellinger 
1093b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1094b8d26b3bSNicholas Bellinger 
1095b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
10964c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
10974c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
10984c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
1099b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1100b8d26b3bSNicholas Bellinger 
11016faaa85fSNicholas Bellinger 	if (login->first_request) {
1102dac6ab30SSagi Grimberg 		complete(&isert_conn->login_comp);
11036faaa85fSNicholas Bellinger 		return;
11046faaa85fSNicholas Bellinger 	}
11056faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1106b8d26b3bSNicholas Bellinger }
1107b8d26b3bSNicholas Bellinger 
1108b8d26b3bSNicholas Bellinger static struct iscsi_cmd
11094366b19cSJenny Derzhavetz *isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
1110b8d26b3bSNicholas Bellinger {
11116700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1112b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1113d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1114b8d26b3bSNicholas Bellinger 
1115676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1116d703ce2fSNicholas Bellinger 	if (!cmd) {
111724f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1118b8d26b3bSNicholas Bellinger 		return NULL;
1119b8d26b3bSNicholas Bellinger 	}
1120d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1121b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1122d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
11234366b19cSJenny Derzhavetz 	isert_cmd->rx_desc = rx_desc;
1124b8d26b3bSNicholas Bellinger 
1125d703ce2fSNicholas Bellinger 	return cmd;
1126b8d26b3bSNicholas Bellinger }
1127b8d26b3bSNicholas Bellinger 
1128b8d26b3bSNicholas Bellinger static int
1129b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1130d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1131d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1132b8d26b3bSNicholas Bellinger {
1133b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1134b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1135b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1136b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
11379fd60088SJenny Derzhavetz 	unsigned int data_len;
1138b8d26b3bSNicholas Bellinger 
1139b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1140b8d26b3bSNicholas Bellinger 	if (rc < 0)
1141b8d26b3bSNicholas Bellinger 		return rc;
1142b8d26b3bSNicholas Bellinger 
1143b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1144b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1145b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
11469fd60088SJenny Derzhavetz 	data_len = cmd->se_cmd.data_length;
1147b8d26b3bSNicholas Bellinger 
11489fd60088SJenny Derzhavetz 	if (imm_data && imm_data_len == data_len)
11499fd60088SJenny Derzhavetz 		cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1150b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1151b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1152b8d26b3bSNicholas Bellinger 		return 0;
1153b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1154b8d26b3bSNicholas Bellinger 		dump_payload = true;
1155b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1156b8d26b3bSNicholas Bellinger 	}
1157b8d26b3bSNicholas Bellinger 
1158b8d26b3bSNicholas Bellinger 	if (!imm_data)
1159b8d26b3bSNicholas Bellinger 		return 0;
1160b8d26b3bSNicholas Bellinger 
11619fd60088SJenny Derzhavetz 	if (imm_data_len != data_len) {
1162b8d26b3bSNicholas Bellinger 		sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
11639fd60088SJenny Derzhavetz 		sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
11649fd60088SJenny Derzhavetz 				    &rx_desc->data[0], imm_data_len);
11659fd60088SJenny Derzhavetz 		isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
11669fd60088SJenny Derzhavetz 			  sg_nents, imm_data_len);
11679fd60088SJenny Derzhavetz 	} else {
11689fd60088SJenny Derzhavetz 		sg_init_table(&isert_cmd->sg, 1);
11699fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_sg = &isert_cmd->sg;
11709fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_nents = 1;
11719fd60088SJenny Derzhavetz 		sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
11729fd60088SJenny Derzhavetz 		isert_dbg("Transfer Immediate imm_data_len: %d\n",
11739fd60088SJenny Derzhavetz 			  imm_data_len);
11749fd60088SJenny Derzhavetz 	}
1175b8d26b3bSNicholas Bellinger 
1176b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1177b8d26b3bSNicholas Bellinger 
1178b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1179b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1180b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1181b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1182b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1183b8d26b3bSNicholas Bellinger 	}
1184b8d26b3bSNicholas Bellinger 
1185b8d26b3bSNicholas Bellinger sequence_cmd:
1186561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1187b8d26b3bSNicholas Bellinger 
1188b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
11890300b114SBart Van Assche 		iscsit_set_unsolicited_dataout(cmd);
11906cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1191afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1192b8d26b3bSNicholas Bellinger 
1193b8d26b3bSNicholas Bellinger 	return 0;
1194b8d26b3bSNicholas Bellinger }
1195b8d26b3bSNicholas Bellinger 
1196b8d26b3bSNicholas Bellinger static int
1197b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1198b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1199b8d26b3bSNicholas Bellinger {
1200b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1201b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1202b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1203b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1204b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1205b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1206b8d26b3bSNicholas Bellinger 
1207b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1208b8d26b3bSNicholas Bellinger 	if (rc < 0)
1209b8d26b3bSNicholas Bellinger 		return rc;
1210b8d26b3bSNicholas Bellinger 	else if (!cmd)
1211b8d26b3bSNicholas Bellinger 		return 0;
1212b8d26b3bSNicholas Bellinger 	/*
1213b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1214b8d26b3bSNicholas Bellinger 	 */
1215b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
121624f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1217b8d26b3bSNicholas Bellinger 		dump_stack();
1218b8d26b3bSNicholas Bellinger 		return -1;
1219b8d26b3bSNicholas Bellinger 	}
1220b8d26b3bSNicholas Bellinger 
12214c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
12224c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
12234c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
12244c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1225b8d26b3bSNicholas Bellinger 
1226b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1227b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1228b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1229b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1230b8d26b3bSNicholas Bellinger 	/*
1231b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1232b8d26b3bSNicholas Bellinger 	 */
1233b8d26b3bSNicholas Bellinger 	if (page_off) {
12344c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1235b8d26b3bSNicholas Bellinger 		dump_stack();
1236b8d26b3bSNicholas Bellinger 		return -1;
1237b8d26b3bSNicholas Bellinger 	}
12384c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
12394c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
12404c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1241b8d26b3bSNicholas Bellinger 
1242b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1243b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1244b8d26b3bSNicholas Bellinger 
1245b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1246b8d26b3bSNicholas Bellinger 	if (rc < 0)
1247b8d26b3bSNicholas Bellinger 		return rc;
1248b8d26b3bSNicholas Bellinger 
12494366b19cSJenny Derzhavetz 	/*
12504366b19cSJenny Derzhavetz 	 * multiple data-outs on the same command can arrive -
12514366b19cSJenny Derzhavetz 	 * so post the buffer before hand
12524366b19cSJenny Derzhavetz 	 */
12534366b19cSJenny Derzhavetz 	rc = isert_post_recv(isert_conn, rx_desc);
12544366b19cSJenny Derzhavetz 	if (rc) {
12554366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", rc);
12564366b19cSJenny Derzhavetz 		return rc;
12574366b19cSJenny Derzhavetz 	}
1258b8d26b3bSNicholas Bellinger 	return 0;
1259b8d26b3bSNicholas Bellinger }
1260b8d26b3bSNicholas Bellinger 
1261b8d26b3bSNicholas Bellinger static int
1262778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1263d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1264d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1265778de368SNicholas Bellinger {
1266778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1267778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1268778de368SNicholas Bellinger 	int rc;
1269778de368SNicholas Bellinger 
1270778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1271778de368SNicholas Bellinger 	if (rc < 0)
1272778de368SNicholas Bellinger 		return rc;
1273778de368SNicholas Bellinger 	/*
1274778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1275778de368SNicholas Bellinger 	 */
1276778de368SNicholas Bellinger 
1277778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1278778de368SNicholas Bellinger }
1279778de368SNicholas Bellinger 
1280778de368SNicholas Bellinger static int
1281adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1282d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1283d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1284adb54c29SNicholas Bellinger {
1285adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1286adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1287adb54c29SNicholas Bellinger 	int rc;
1288b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1289adb54c29SNicholas Bellinger 
1290adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1291adb54c29SNicholas Bellinger 	if (rc < 0)
1292adb54c29SNicholas Bellinger 		return rc;
1293adb54c29SNicholas Bellinger 
1294b44a2b67SSagi Grimberg 	if (payload_length) {
1295adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
129693b80f29SLeon Romanovsky 		if (!text_in)
1297adb54c29SNicholas Bellinger 			return -ENOMEM;
1298adb54c29SNicholas Bellinger 	}
1299adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1300adb54c29SNicholas Bellinger 
1301adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1302adb54c29SNicholas Bellinger 
1303adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1304adb54c29SNicholas Bellinger }
1305adb54c29SNicholas Bellinger 
1306adb54c29SNicholas Bellinger static int
1307b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1308b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1309b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1310b8d26b3bSNicholas Bellinger {
1311b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1312b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1313b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1314b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1315b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1316b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1317b8d26b3bSNicholas Bellinger 
1318fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1319ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
132024f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1321ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1322ca40d24eSNicholas Bellinger 		return 0;
1323ca40d24eSNicholas Bellinger 	}
1324ca40d24eSNicholas Bellinger 
1325b8d26b3bSNicholas Bellinger 	switch (opcode) {
1326b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
13274366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1328b8d26b3bSNicholas Bellinger 		if (!cmd)
1329b8d26b3bSNicholas Bellinger 			break;
1330b8d26b3bSNicholas Bellinger 
1331d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1332b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1333b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1334b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1335b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1336422bd0acSJenny Derzhavetz 		isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
1337b8d26b3bSNicholas Bellinger 
1338d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1339b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1340b8d26b3bSNicholas Bellinger 		break;
1341b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
13424366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1343b8d26b3bSNicholas Bellinger 		if (!cmd)
1344b8d26b3bSNicholas Bellinger 			break;
1345b8d26b3bSNicholas Bellinger 
1346d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1347d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1348778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1349b8d26b3bSNicholas Bellinger 		break;
1350b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1351b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1352b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1353b8d26b3bSNicholas Bellinger 		break;
1354b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
13554366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1356b8d26b3bSNicholas Bellinger 		if (!cmd)
1357b8d26b3bSNicholas Bellinger 			break;
1358b8d26b3bSNicholas Bellinger 
1359b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1360b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1361b8d26b3bSNicholas Bellinger 		break;
1362b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
13634366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1364b8d26b3bSNicholas Bellinger 		if (!cmd)
1365b8d26b3bSNicholas Bellinger 			break;
1366b8d26b3bSNicholas Bellinger 
1367b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1368b8d26b3bSNicholas Bellinger 		break;
1369adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
13704366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1371e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
13724366b19cSJenny Derzhavetz 		else
13734366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
13744366b19cSJenny Derzhavetz 
1375e4f4e801SSagi Grimberg 		if (!cmd)
1376e4f4e801SSagi Grimberg 			break;
1377adb54c29SNicholas Bellinger 
1378d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1379d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1380adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1381adb54c29SNicholas Bellinger 		break;
1382b8d26b3bSNicholas Bellinger 	default:
138324f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1384b8d26b3bSNicholas Bellinger 		dump_stack();
1385b8d26b3bSNicholas Bellinger 		break;
1386b8d26b3bSNicholas Bellinger 	}
1387b8d26b3bSNicholas Bellinger 
1388b8d26b3bSNicholas Bellinger 	return ret;
1389b8d26b3bSNicholas Bellinger }
1390b8d26b3bSNicholas Bellinger 
1391b8d26b3bSNicholas Bellinger static void
13929679cc51SChristoph Hellwig isert_print_wc(struct ib_wc *wc, const char *type)
1393b8d26b3bSNicholas Bellinger {
13949679cc51SChristoph Hellwig 	if (wc->status != IB_WC_WR_FLUSH_ERR)
13959679cc51SChristoph Hellwig 		isert_err("%s failure: %s (%d) vend_err %x\n", type,
13969679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status,
13979679cc51SChristoph Hellwig 			  wc->vendor_err);
13989679cc51SChristoph Hellwig 	else
13999679cc51SChristoph Hellwig 		isert_dbg("%s failure: %s (%d)\n", type,
14009679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status);
14019679cc51SChristoph Hellwig }
14029679cc51SChristoph Hellwig 
14039679cc51SChristoph Hellwig static void
14049679cc51SChristoph Hellwig isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
14059679cc51SChristoph Hellwig {
14069679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
14079679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
14089679cc51SChristoph Hellwig 	struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
14099679cc51SChristoph Hellwig 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1410d3cf81f9SSagi Grimberg 	struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
1411b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1412b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1413b8d26b3bSNicholas Bellinger 
14149679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
14159679cc51SChristoph Hellwig 		isert_print_wc(wc, "recv");
14169679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
14179679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
14189679cc51SChristoph Hellwig 		return;
14199679cc51SChristoph Hellwig 	}
14209679cc51SChristoph Hellwig 
14217a56dc88SSagi Grimberg 	rx_desc->in_use = true;
14227a56dc88SSagi Grimberg 
14239679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
14249679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
14259679cc51SChristoph Hellwig 
14269679cc51SChristoph Hellwig 	isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
14279679cc51SChristoph Hellwig 		 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
14289679cc51SChristoph Hellwig 		 (int)(wc->byte_len - ISER_HEADERS_LEN));
14299679cc51SChristoph Hellwig 
1430d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1431b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1432d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1433d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1434d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
14354c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1436b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1437b8d26b3bSNicholas Bellinger 		}
1438d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1439d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1440d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
14414c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1442b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1443b8d26b3bSNicholas Bellinger 		}
1444b8d26b3bSNicholas Bellinger 
144524f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1446b8d26b3bSNicholas Bellinger 		break;
1447b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
144824f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1449b8d26b3bSNicholas Bellinger 		break;
1450b8d26b3bSNicholas Bellinger 	default:
1451d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1452b8d26b3bSNicholas Bellinger 		break;
1453b8d26b3bSNicholas Bellinger 	}
1454b8d26b3bSNicholas Bellinger 
14556c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1456b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
14579679cc51SChristoph Hellwig 
14589679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
14599679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1460b8d26b3bSNicholas Bellinger }
1461b8d26b3bSNicholas Bellinger 
1462b8d26b3bSNicholas Bellinger static void
14639679cc51SChristoph Hellwig isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1464b8d26b3bSNicholas Bellinger {
14659679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1466fce50a2fSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->device->ib_device;
1467b8d26b3bSNicholas Bellinger 
14689679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
14699679cc51SChristoph Hellwig 		isert_print_wc(wc, "login recv");
14709679cc51SChristoph Hellwig 		return;
1471b8d26b3bSNicholas Bellinger 	}
1472b8d26b3bSNicholas Bellinger 
14739679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
14749679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1475b8d26b3bSNicholas Bellinger 
14769679cc51SChristoph Hellwig 	isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
1477b8d26b3bSNicholas Bellinger 
14782371e5daSSagi Grimberg 	if (isert_conn->conn) {
14792371e5daSSagi Grimberg 		struct iscsi_login *login = isert_conn->conn->conn_login;
14802371e5daSSagi Grimberg 
14812371e5daSSagi Grimberg 		if (login && !login->first_request)
14822371e5daSSagi Grimberg 			isert_rx_login_req(isert_conn);
14832371e5daSSagi Grimberg 	}
14849679cc51SChristoph Hellwig 
1485dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
14862371e5daSSagi Grimberg 	complete(&isert_conn->login_req_comp);
1487dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
1488b8d26b3bSNicholas Bellinger 
14899679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
14909679cc51SChristoph Hellwig 				ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1491b8d26b3bSNicholas Bellinger }
1492b8d26b3bSNicholas Bellinger 
1493e3d7e4c3SSagi Grimberg static void
149438a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
1495e3d7e4c3SSagi Grimberg {
149638a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
149738a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
1498e3d7e4c3SSagi Grimberg 
149938a2d0d4SChristoph Hellwig 	if (!cmd->rw.nr_ops)
150038a2d0d4SChristoph Hellwig 		return;
150138a2d0d4SChristoph Hellwig 
150238a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
150338a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
150438a2d0d4SChristoph Hellwig 				conn->cm_id->port_num, se_cmd->t_data_sg,
150538a2d0d4SChristoph Hellwig 				se_cmd->t_data_nents, se_cmd->t_prot_sg,
150638a2d0d4SChristoph Hellwig 				se_cmd->t_prot_nents, dir);
150738a2d0d4SChristoph Hellwig 	} else {
150838a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
150938a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
1510e3d7e4c3SSagi Grimberg 	}
1511e3d7e4c3SSagi Grimberg 
151238a2d0d4SChristoph Hellwig 	cmd->rw.nr_ops = 0;
151359464ef4SVu Pham }
151459464ef4SVu Pham 
151559464ef4SVu Pham static void
151603e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1517b8d26b3bSNicholas Bellinger {
1518d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1519b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1520186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1521e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1522b8d26b3bSNicholas Bellinger 
15234c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1524b8d26b3bSNicholas Bellinger 
1525b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1526b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1527b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1528b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15295159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1530b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1531b8d26b3bSNicholas Bellinger 
153203e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1533b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
153403e7848aSNicholas Bellinger 			/*
153503e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
153603e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
153703e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
153803e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
153903e7848aSNicholas Bellinger 			 */
154003e7848aSNicholas Bellinger 			if (comp_err &&
154103e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
154203e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
154303e7848aSNicholas Bellinger 
1544afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
154503e7848aSNicholas Bellinger 			}
154603e7848aSNicholas Bellinger 		}
1547b8d26b3bSNicholas Bellinger 
154838a2d0d4SChristoph Hellwig 		isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1549186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1550186a9647SNicholas Bellinger 		break;
1551b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1552186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1553186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15545159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1555186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1556186a9647SNicholas Bellinger 
1557b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1558b8d26b3bSNicholas Bellinger 		break;
1559b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1560b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1561adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1562e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1563e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1564e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1565e4f4e801SSagi Grimberg 			break;
1566e4f4e801SSagi Grimberg 
1567b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1568b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15695159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1570b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1571b8d26b3bSNicholas Bellinger 
1572b8d26b3bSNicholas Bellinger 		/*
1573b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1574b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1575b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1576b8d26b3bSNicholas Bellinger 		 */
1577b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
157811378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
15793df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1580b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1581b8d26b3bSNicholas Bellinger 			break;
1582b8d26b3bSNicholas Bellinger 		}
1583d2271bd0SBart Van Assche 		/* fall through */
1584b8d26b3bSNicholas Bellinger 	default:
1585d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1586b8d26b3bSNicholas Bellinger 		break;
1587b8d26b3bSNicholas Bellinger 	}
1588b8d26b3bSNicholas Bellinger }
1589b8d26b3bSNicholas Bellinger 
1590b8d26b3bSNicholas Bellinger static void
1591b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1592b8d26b3bSNicholas Bellinger {
1593b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
15944c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1595b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1596b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1597b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1598b8d26b3bSNicholas Bellinger 	}
1599b8d26b3bSNicholas Bellinger }
1600b8d26b3bSNicholas Bellinger 
1601b8d26b3bSNicholas Bellinger static void
1602b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
160303e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1604b8d26b3bSNicholas Bellinger {
1605dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
16064c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1607dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1608dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1609dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1610b8d26b3bSNicholas Bellinger 	}
1611b8d26b3bSNicholas Bellinger 
1612b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
161303e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1614b8d26b3bSNicholas Bellinger }
1615b8d26b3bSNicholas Bellinger 
161696b7973eSSagi Grimberg static int
161796b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1618f93f3a70SSagi Grimberg {
16199e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
162096b7973eSSagi Grimberg 	int ret;
1621f93f3a70SSagi Grimberg 
162296b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
16239e961ae7SSagi Grimberg 	if (ret) {
162424f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
16259e961ae7SSagi Grimberg 		goto fail_mr_status;
16269e961ae7SSagi Grimberg 	}
162796b7973eSSagi Grimberg 
16289e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
162996b7973eSSagi Grimberg 		u64 sec_offset_err;
16309e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
16319e961ae7SSagi Grimberg 
16329e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
16339e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
16349e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
16359e961ae7SSagi Grimberg 			break;
16369e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
16379e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
16389e961ae7SSagi Grimberg 			break;
16399e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
16409e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
16419e961ae7SSagi Grimberg 			break;
16429e961ae7SSagi Grimberg 		}
164396b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
164496b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
164596b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
16469e961ae7SSagi Grimberg 
16474c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
16489e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
16499e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
16509e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
16519e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
16529e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
16539e961ae7SSagi Grimberg 		ret = 1;
16549e961ae7SSagi Grimberg 	}
16559e961ae7SSagi Grimberg 
16569e961ae7SSagi Grimberg fail_mr_status:
165796b7973eSSagi Grimberg 	return ret;
165896b7973eSSagi Grimberg }
165996b7973eSSagi Grimberg 
166096b7973eSSagi Grimberg static void
16619679cc51SChristoph Hellwig isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
166296b7973eSSagi Grimberg {
16639679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1664dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
16659679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1666e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
16679679cc51SChristoph Hellwig 	struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
166896b7973eSSagi Grimberg 	int ret = 0;
166996b7973eSSagi Grimberg 
16709679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16719679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma write");
16729679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16739679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16749679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
16759679cc51SChristoph Hellwig 		return;
16769679cc51SChristoph Hellwig 	}
16779679cc51SChristoph Hellwig 
16789679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
16799679cc51SChristoph Hellwig 
168038a2d0d4SChristoph Hellwig 	ret = isert_check_pi_status(cmd, isert_cmd->rw.sig->sig_mr);
168138a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
168296b7973eSSagi Grimberg 
1683555a65f6SNicholas Bellinger 	if (ret) {
1684555a65f6SNicholas Bellinger 		/*
1685555a65f6SNicholas Bellinger 		 * transport_generic_request_failure() expects to have
1686555a65f6SNicholas Bellinger 		 * plus two references to handle queue-full, so re-add
1687555a65f6SNicholas Bellinger 		 * one here as target-core will have already dropped
1688555a65f6SNicholas Bellinger 		 * it after the first isert_put_datain() callback.
1689555a65f6SNicholas Bellinger 		 */
1690555a65f6SNicholas Bellinger 		kref_get(&cmd->cmd_kref);
1691555a65f6SNicholas Bellinger 		transport_generic_request_failure(cmd, cmd->pi_err);
1692555a65f6SNicholas Bellinger 	} else {
1693555a65f6SNicholas Bellinger 		/*
1694555a65f6SNicholas Bellinger 		 * XXX: isert_put_response() failure is not retried.
1695555a65f6SNicholas Bellinger 		 */
1696555a65f6SNicholas Bellinger 		ret = isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
16979e961ae7SSagi Grimberg 		if (ret)
1698555a65f6SNicholas Bellinger 			pr_warn_ratelimited("isert_put_response() ret: %d\n", ret);
1699555a65f6SNicholas Bellinger 	}
1700f93f3a70SSagi Grimberg }
1701f93f3a70SSagi Grimberg 
1702f93f3a70SSagi Grimberg static void
17039679cc51SChristoph Hellwig isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1704b8d26b3bSNicholas Bellinger {
17059679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17069679cc51SChristoph Hellwig 	struct isert_device *device = isert_conn->device;
17079679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1708e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
1709d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1710b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
17115bac4b1aSSagi Grimberg 	int ret = 0;
1712b8d26b3bSNicholas Bellinger 
17139679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17149679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma read");
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(desc, isert_cmd, device->ib_device, true);
17189679cc51SChristoph Hellwig 		return;
17199679cc51SChristoph Hellwig 	}
17209679cc51SChristoph Hellwig 
17219679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
17229679cc51SChristoph Hellwig 
1723b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
172438a2d0d4SChristoph Hellwig 
172538a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd))
172638a2d0d4SChristoph Hellwig 		ret = isert_check_pi_status(se_cmd, isert_cmd->rw.sig->sig_mr);
172738a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
172838a2d0d4SChristoph Hellwig 	cmd->write_data_done = 0;
1729b8d26b3bSNicholas Bellinger 
173024f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1731b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1732b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1733b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1734b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1735b8d26b3bSNicholas Bellinger 
1736555a65f6SNicholas Bellinger 	/*
1737555a65f6SNicholas Bellinger 	 * transport_generic_request_failure() will drop the extra
1738555a65f6SNicholas Bellinger 	 * se_cmd->cmd_kref reference after T10-PI error, and handle
1739555a65f6SNicholas Bellinger 	 * any non-zero ->queue_status() callback error retries.
1740555a65f6SNicholas Bellinger 	 */
1741555a65f6SNicholas Bellinger 	if (ret)
1742555a65f6SNicholas Bellinger 		transport_generic_request_failure(se_cmd, se_cmd->pi_err);
1743555a65f6SNicholas Bellinger 	else
1744b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1745b8d26b3bSNicholas Bellinger }
1746b8d26b3bSNicholas Bellinger 
1747b8d26b3bSNicholas Bellinger static void
1748b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1749b8d26b3bSNicholas Bellinger {
1750b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1751b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1752b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1753dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1754d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1755b8d26b3bSNicholas Bellinger 
17564c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
17574c22e07fSSagi Grimberg 
1758b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1759b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1760b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1761d2271bd0SBart Van Assche 		/* fall through */
1762d2271bd0SBart Van Assche 	case ISTATE_SEND_REJECT:
1763d2271bd0SBart Van Assche 	case ISTATE_SEND_TEXTRSP:
1764b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
17654c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
17664c22e07fSSagi Grimberg 				     ib_dev, false);
17673df8f68aSNicholas Bellinger 		break;
1768b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1769b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1770b8d26b3bSNicholas Bellinger 		break;
1771b8d26b3bSNicholas Bellinger 	default:
17724c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1773b8d26b3bSNicholas Bellinger 		dump_stack();
1774b8d26b3bSNicholas Bellinger 		break;
1775b8d26b3bSNicholas Bellinger 	}
1776b8d26b3bSNicholas Bellinger }
1777b8d26b3bSNicholas Bellinger 
1778b8d26b3bSNicholas Bellinger static void
17799679cc51SChristoph Hellwig isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
1780b8d26b3bSNicholas Bellinger {
17819679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17829679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
17839679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1784b8d26b3bSNicholas Bellinger 
17859679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17869679cc51SChristoph Hellwig 		isert_print_wc(wc, "login send");
17879679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17889679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17899679cc51SChristoph Hellwig 	}
17909679cc51SChristoph Hellwig 
17919679cc51SChristoph Hellwig 	isert_unmap_tx_desc(tx_desc, ib_dev);
17929679cc51SChristoph Hellwig }
17939679cc51SChristoph Hellwig 
17949679cc51SChristoph Hellwig static void
17959679cc51SChristoph Hellwig isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
17969679cc51SChristoph Hellwig {
17979679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17989679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
17999679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1800e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
18019679cc51SChristoph Hellwig 
18029679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
18039679cc51SChristoph Hellwig 		isert_print_wc(wc, "send");
18049679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
18059679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
18069679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
18079679cc51SChristoph Hellwig 		return;
18089679cc51SChristoph Hellwig 	}
18099679cc51SChristoph Hellwig 
18109679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
18119679cc51SChristoph Hellwig 
18129679cc51SChristoph Hellwig 	switch (isert_cmd->iscsi_cmd->i_state) {
18139679cc51SChristoph Hellwig 	case ISTATE_SEND_TASKMGTRSP:
18149679cc51SChristoph Hellwig 	case ISTATE_SEND_LOGOUTRSP:
18159679cc51SChristoph Hellwig 	case ISTATE_SEND_REJECT:
18169679cc51SChristoph Hellwig 	case ISTATE_SEND_TEXTRSP:
1817b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1818b8d26b3bSNicholas Bellinger 
1819b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1820b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1821b8d26b3bSNicholas Bellinger 		return;
1822b8d26b3bSNicholas Bellinger 	default:
18239679cc51SChristoph Hellwig 		isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
18249679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1825b8d26b3bSNicholas Bellinger 		break;
1826b8d26b3bSNicholas Bellinger 	}
1827b8d26b3bSNicholas Bellinger }
1828b8d26b3bSNicholas Bellinger 
1829b8d26b3bSNicholas Bellinger static int
1830b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1831b8d26b3bSNicholas Bellinger {
1832b8d26b3bSNicholas Bellinger 	int ret;
1833b8d26b3bSNicholas Bellinger 
18344366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
18354366b19cSJenny Derzhavetz 	if (ret) {
18364366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", ret);
18374366b19cSJenny Derzhavetz 		return ret;
18384366b19cSJenny Derzhavetz 	}
18394366b19cSJenny Derzhavetz 
1840e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr, NULL);
1841b8d26b3bSNicholas Bellinger 	if (ret) {
184224f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
1843b8d26b3bSNicholas Bellinger 		return ret;
1844b8d26b3bSNicholas Bellinger 	}
1845b8d26b3bSNicholas Bellinger 	return ret;
1846b8d26b3bSNicholas Bellinger }
1847b8d26b3bSNicholas Bellinger 
1848b8d26b3bSNicholas Bellinger static int
1849b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1850b8d26b3bSNicholas Bellinger {
1851d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18526700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1853b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1854b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1855b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1856b8d26b3bSNicholas Bellinger 
1857b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1858b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1859b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1860b8d26b3bSNicholas Bellinger 	/*
1861b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1862b8d26b3bSNicholas Bellinger 	 */
1863b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1864b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1865b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1866dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
186767cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1868b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1869dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1870b8d26b3bSNicholas Bellinger 
1871b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1872b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1873b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1874b8d26b3bSNicholas Bellinger 
1875b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1876b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1877dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1878b8d26b3bSNicholas Bellinger 
1879dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1880dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1881b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1882def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1883def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1884b8d26b3bSNicholas Bellinger 
1885dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1886dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1887dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
188834efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1889b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1890b8d26b3bSNicholas Bellinger 	}
1891b8d26b3bSNicholas Bellinger 
189268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1893b8d26b3bSNicholas Bellinger 
18944c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
1895b8d26b3bSNicholas Bellinger 
1896b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1897b8d26b3bSNicholas Bellinger }
1898b8d26b3bSNicholas Bellinger 
1899131e6abcSNicholas Bellinger static void
1900131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1901131e6abcSNicholas Bellinger {
1902131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19036700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1904131e6abcSNicholas Bellinger 
1905131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
1906131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
1907131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
1908131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1909131e6abcSNicholas Bellinger 
1910131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
1911131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
191238a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1913131e6abcSNicholas Bellinger }
1914131e6abcSNicholas Bellinger 
1915e70beee7SNicholas Bellinger static enum target_prot_op
1916e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
1917e70beee7SNicholas Bellinger {
19186700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1919dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1920e70beee7SNicholas Bellinger 
192123a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
192223a548eeSSagi Grimberg 		if (device->pi_capable) {
192324f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
192423a548eeSSagi Grimberg 			isert_conn->pi_support = true;
1925e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
192623a548eeSSagi Grimberg 		}
192723a548eeSSagi Grimberg 	}
192823a548eeSSagi Grimberg 
192924f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
193023a548eeSSagi Grimberg 	isert_conn->pi_support = false;
1931e70beee7SNicholas Bellinger 
1932e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
1933e70beee7SNicholas Bellinger }
1934e70beee7SNicholas Bellinger 
1935b8d26b3bSNicholas Bellinger static int
1936b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1937b8d26b3bSNicholas Bellinger 		bool nopout_response)
1938b8d26b3bSNicholas Bellinger {
1939d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19406700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1941b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1942b8d26b3bSNicholas Bellinger 
1943b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1944b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1945b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1946b8d26b3bSNicholas Bellinger 			       nopout_response);
1947b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
194868a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1949b8d26b3bSNicholas Bellinger 
19504c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
1951b8d26b3bSNicholas Bellinger 
1952b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1953b8d26b3bSNicholas Bellinger }
1954b8d26b3bSNicholas Bellinger 
1955b8d26b3bSNicholas Bellinger static int
1956b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1957b8d26b3bSNicholas Bellinger {
1958d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19596700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1960b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1961b8d26b3bSNicholas Bellinger 
1962b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1963b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1964b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1965b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
196668a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1967b8d26b3bSNicholas Bellinger 
19684c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
1969b8d26b3bSNicholas Bellinger 
1970b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1971b8d26b3bSNicholas Bellinger }
1972b8d26b3bSNicholas Bellinger 
1973b8d26b3bSNicholas Bellinger static int
1974b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1975b8d26b3bSNicholas Bellinger {
1976d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19776700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1978b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1979b8d26b3bSNicholas Bellinger 
1980b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1981b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1982b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1983b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
198468a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1985b8d26b3bSNicholas Bellinger 
19864c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
1987b8d26b3bSNicholas Bellinger 
1988b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1989b8d26b3bSNicholas Bellinger }
1990b8d26b3bSNicholas Bellinger 
1991b8d26b3bSNicholas Bellinger static int
1992b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1993b8d26b3bSNicholas Bellinger {
1994d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19956700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1996b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1997dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
199867cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
19993df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
20003df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
20013df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2002b8d26b3bSNicholas Bellinger 
2003b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
20043df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2005b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
20063df8f68aSNicholas Bellinger 
20073df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2008dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
20093df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
20103df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2011def4a6ffSAlexey Khoroshilov 	if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
2012def4a6ffSAlexey Khoroshilov 		return -ENOMEM;
2013dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2014dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
20153df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
201634efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
20173df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
20183df8f68aSNicholas Bellinger 
201968a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2020b8d26b3bSNicholas Bellinger 
20214c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
2022b8d26b3bSNicholas Bellinger 
2023b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2024b8d26b3bSNicholas Bellinger }
2025b8d26b3bSNicholas Bellinger 
2026b8d26b3bSNicholas Bellinger static int
2027adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2028adb54c29SNicholas Bellinger {
2029d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
20306700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2031adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2032adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2033adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2034adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2035adb54c29SNicholas Bellinger 	int rc;
2036adb54c29SNicholas Bellinger 
2037adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
203822c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2039adb54c29SNicholas Bellinger 	if (rc < 0)
2040adb54c29SNicholas Bellinger 		return rc;
2041adb54c29SNicholas Bellinger 
2042adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2043adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2044adb54c29SNicholas Bellinger 
2045adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2046dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
204767cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2048adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2049adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2050adb54c29SNicholas Bellinger 
2051adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2052adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2053def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
2054def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
2055adb54c29SNicholas Bellinger 
2056adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2057adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2058adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
205934efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
2060adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2061adb54c29SNicholas Bellinger 	}
206268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2063adb54c29SNicholas Bellinger 
2064f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
2065adb54c29SNicholas Bellinger 
2066adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2067adb54c29SNicholas Bellinger }
2068adb54c29SNicholas Bellinger 
20693d73cf1aSSagi Grimberg static inline void
2070*dae6345aSIsrael Rukshin isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_domain *domain)
20719e961ae7SSagi Grimberg {
207278eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
20733d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
20743d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
20753d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
207678eda2bbSSagi Grimberg 	/*
207778eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
207878eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
207978eda2bbSSagi Grimberg 	 * from se_cmd.
208078eda2bbSSagi Grimberg 	 */
208178eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
208278eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
208378eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
208478eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
208578eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
208678eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
20873d73cf1aSSagi Grimberg };
20883d73cf1aSSagi Grimberg 
20893d73cf1aSSagi Grimberg static int
20903d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
20913d73cf1aSSagi Grimberg {
209238a2d0d4SChristoph Hellwig 	memset(sig_attrs, 0, sizeof(*sig_attrs));
209338a2d0d4SChristoph Hellwig 
20949e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
20959e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
20969e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
209778eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
2098*dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
20999e961ae7SSagi Grimberg 		break;
21009e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
21019e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
210278eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
2103*dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
21049e961ae7SSagi Grimberg 		break;
21059e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
21069e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
2107*dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
2108*dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
21099e961ae7SSagi Grimberg 		break;
21109e961ae7SSagi Grimberg 	default:
211124f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
21129e961ae7SSagi Grimberg 		return -EINVAL;
21139e961ae7SSagi Grimberg 	}
21149e961ae7SSagi Grimberg 
211526232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD)
211626232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
211726232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG)
211826232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
211926232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)
212026232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
212126232872SMax Gurtovoy 
21229e961ae7SSagi Grimberg 	return 0;
21239e961ae7SSagi Grimberg }
21249e961ae7SSagi Grimberg 
21259e961ae7SSagi Grimberg static int
212638a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
212738a2d0d4SChristoph Hellwig 		struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
21289e961ae7SSagi Grimberg {
212938a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
213038a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
213138a2d0d4SChristoph Hellwig 	u8 port_num = conn->cm_id->port_num;
213238a2d0d4SChristoph Hellwig 	u64 addr;
213338a2d0d4SChristoph Hellwig 	u32 rkey, offset;
21349e961ae7SSagi Grimberg 	int ret;
21359e961ae7SSagi Grimberg 
213666f53e6fSBharat Potnuri 	if (cmd->ctx_init_done)
213766f53e6fSBharat Potnuri 		goto rdma_ctx_post;
213866f53e6fSBharat Potnuri 
213938a2d0d4SChristoph Hellwig 	if (dir == DMA_FROM_DEVICE) {
214038a2d0d4SChristoph Hellwig 		addr = cmd->write_va;
214138a2d0d4SChristoph Hellwig 		rkey = cmd->write_stag;
214238a2d0d4SChristoph Hellwig 		offset = cmd->iscsi_cmd->write_data_done;
214338a2d0d4SChristoph Hellwig 	} else {
214438a2d0d4SChristoph Hellwig 		addr = cmd->read_va;
214538a2d0d4SChristoph Hellwig 		rkey = cmd->read_stag;
214638a2d0d4SChristoph Hellwig 		offset = 0;
214738a2d0d4SChristoph Hellwig 	}
214838a2d0d4SChristoph Hellwig 
214938a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
215038a2d0d4SChristoph Hellwig 		struct ib_sig_attrs sig_attrs;
215138a2d0d4SChristoph Hellwig 
21529e961ae7SSagi Grimberg 		ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
21539e961ae7SSagi Grimberg 		if (ret)
2154e3d7e4c3SSagi Grimberg 			return ret;
2155e3d7e4c3SSagi Grimberg 
215638a2d0d4SChristoph Hellwig 		WARN_ON_ONCE(offset);
215738a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
215838a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
215938a2d0d4SChristoph Hellwig 				se_cmd->t_prot_sg, se_cmd->t_prot_nents,
216038a2d0d4SChristoph Hellwig 				&sig_attrs, addr, rkey, dir);
2161570db170SSagi Grimberg 	} else {
216238a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
216338a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
216438a2d0d4SChristoph Hellwig 				offset, addr, rkey, dir);
216538a2d0d4SChristoph Hellwig 	}
216666f53e6fSBharat Potnuri 
216738a2d0d4SChristoph Hellwig 	if (ret < 0) {
216838a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
216938a2d0d4SChristoph Hellwig 		return ret;
21709e961ae7SSagi Grimberg 	}
21719e961ae7SSagi Grimberg 
217266f53e6fSBharat Potnuri 	cmd->ctx_init_done = true;
217366f53e6fSBharat Potnuri 
217466f53e6fSBharat Potnuri rdma_ctx_post:
217538a2d0d4SChristoph Hellwig 	ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
217638a2d0d4SChristoph Hellwig 	if (ret < 0)
217738a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to post RDMA res\n", cmd);
217859464ef4SVu Pham 	return ret;
217959464ef4SVu Pham }
218059464ef4SVu Pham 
218159464ef4SVu Pham static int
218290ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
218390ecc6e2SVu Pham {
218490ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
218559464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
21866700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
218738a2d0d4SChristoph Hellwig 	struct ib_cqe *cqe = NULL;
218838a2d0d4SChristoph Hellwig 	struct ib_send_wr *chain_wr = NULL;
218990ecc6e2SVu Pham 	int rc;
219090ecc6e2SVu Pham 
219124f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
219290ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
21934c22e07fSSagi Grimberg 
219438a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd)) {
219538a2d0d4SChristoph Hellwig 		isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
219638a2d0d4SChristoph Hellwig 		cqe = &isert_cmd->tx_desc.tx_cqe;
219738a2d0d4SChristoph Hellwig 	} else {
2198b8d26b3bSNicholas Bellinger 		/*
2199b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2200b8d26b3bSNicholas Bellinger 		 */
22019e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
22029e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
220304d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2204b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2205b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
220695b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
220768a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
22084366b19cSJenny Derzhavetz 
22094366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
22104366b19cSJenny Derzhavetz 		if (rc) {
22114366b19cSJenny Derzhavetz 			isert_err("ib_post_recv failed with %d\n", rc);
22124366b19cSJenny Derzhavetz 			return rc;
22134366b19cSJenny Derzhavetz 		}
221438a2d0d4SChristoph Hellwig 
221538a2d0d4SChristoph Hellwig 		chain_wr = &isert_cmd->tx_desc.send_wr;
22169e961ae7SSagi Grimberg 	}
2217b8d26b3bSNicholas Bellinger 
2218555a65f6SNicholas Bellinger 	rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2219555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n",
2220555a65f6SNicholas Bellinger 		  isert_cmd, rc);
2221555a65f6SNicholas Bellinger 	return rc;
2222b8d26b3bSNicholas Bellinger }
2223b8d26b3bSNicholas Bellinger 
2224b8d26b3bSNicholas Bellinger static int
2225b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2226b8d26b3bSNicholas Bellinger {
2227d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2228555a65f6SNicholas Bellinger 	int ret;
2229b8d26b3bSNicholas Bellinger 
223024f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
223138a2d0d4SChristoph Hellwig 		 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
2232b8d26b3bSNicholas Bellinger 
223338a2d0d4SChristoph Hellwig 	isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2234555a65f6SNicholas Bellinger 	ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context,
223538a2d0d4SChristoph Hellwig 				     &isert_cmd->tx_desc.tx_cqe, NULL);
2236bdf20e72SSagi Grimberg 
2237555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n",
2238555a65f6SNicholas Bellinger 		 isert_cmd, ret);
2239555a65f6SNicholas Bellinger 	return ret;
2240b8d26b3bSNicholas Bellinger }
2241b8d26b3bSNicholas Bellinger 
2242b8d26b3bSNicholas Bellinger static int
2243b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2244b8d26b3bSNicholas Bellinger {
2245a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2246a4c15cd9SJenny Derzhavetz 	int ret = 0;
2247b8d26b3bSNicholas Bellinger 
2248b8d26b3bSNicholas Bellinger 	switch (state) {
2249a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2250a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2251a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2252a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2253a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2254a4c15cd9SJenny Derzhavetz 		break;
2255b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2256b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2257b8d26b3bSNicholas Bellinger 		break;
2258b8d26b3bSNicholas Bellinger 	default:
225924f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2260b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2261b8d26b3bSNicholas Bellinger 		break;
2262b8d26b3bSNicholas Bellinger 	}
2263b8d26b3bSNicholas Bellinger 
2264b8d26b3bSNicholas Bellinger 	return ret;
2265b8d26b3bSNicholas Bellinger }
2266b8d26b3bSNicholas Bellinger 
2267b8d26b3bSNicholas Bellinger static int
2268b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2269b8d26b3bSNicholas Bellinger {
2270991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2271b8d26b3bSNicholas Bellinger 	int ret;
2272b8d26b3bSNicholas Bellinger 
2273b8d26b3bSNicholas Bellinger 	switch (state) {
2274b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2275b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2276991bb764SSagi Grimberg 		if (!ret)
2277991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2278b8d26b3bSNicholas Bellinger 		break;
2279b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2280b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2281b8d26b3bSNicholas Bellinger 		break;
2282b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2283b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2284b8d26b3bSNicholas Bellinger 		break;
2285b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2286b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2287b8d26b3bSNicholas Bellinger 		break;
2288adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2289adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2290adb54c29SNicholas Bellinger 		break;
2291b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2292b8d26b3bSNicholas Bellinger 		/*
2293b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2294b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2295b8d26b3bSNicholas Bellinger 		 */
2296b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2297b8d26b3bSNicholas Bellinger 		break;
2298b8d26b3bSNicholas Bellinger 	default:
229924f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2300b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2301b8d26b3bSNicholas Bellinger 		break;
2302b8d26b3bSNicholas Bellinger 	}
2303b8d26b3bSNicholas Bellinger 
2304b8d26b3bSNicholas Bellinger 	return ret;
2305b8d26b3bSNicholas Bellinger }
2306b8d26b3bSNicholas Bellinger 
2307ca6c1d82SSagi Grimberg struct rdma_cm_id *
2308ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2309ca6c1d82SSagi Grimberg {
2310ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2311ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2312ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2313ca6c1d82SSagi Grimberg 	int ret;
2314ca6c1d82SSagi Grimberg 
2315ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
231624f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2317ca6c1d82SSagi Grimberg 
2318fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
2319ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2320ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
232124f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2322ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2323ca6c1d82SSagi Grimberg 		goto out;
2324ca6c1d82SSagi Grimberg 	}
232524f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2326ca6c1d82SSagi Grimberg 
2327ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2328ca6c1d82SSagi Grimberg 	if (ret) {
232924f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2330ca6c1d82SSagi Grimberg 		goto out_id;
2331ca6c1d82SSagi Grimberg 	}
2332ca6c1d82SSagi Grimberg 
2333992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
2334ca6c1d82SSagi Grimberg 	if (ret) {
233524f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2336ca6c1d82SSagi Grimberg 		goto out_id;
2337ca6c1d82SSagi Grimberg 	}
2338ca6c1d82SSagi Grimberg 
2339ca6c1d82SSagi Grimberg 	return id;
2340ca6c1d82SSagi Grimberg out_id:
2341ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
2342ca6c1d82SSagi Grimberg out:
2343ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
2344ca6c1d82SSagi Grimberg }
2345ca6c1d82SSagi Grimberg 
2346b8d26b3bSNicholas Bellinger static int
2347b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
234813a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
2349b8d26b3bSNicholas Bellinger {
2350b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2351b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2352b8d26b3bSNicholas Bellinger 	int ret;
2353b8d26b3bSNicholas Bellinger 
2354b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
235593b80f29SLeon Romanovsky 	if (!isert_np)
2356b8d26b3bSNicholas Bellinger 		return -ENOMEM;
235793b80f29SLeon Romanovsky 
2358ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
2359ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
2360bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
2361bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
2362ca6c1d82SSagi Grimberg 	isert_np->np = np;
2363b8d26b3bSNicholas Bellinger 
2364b8d26b3bSNicholas Bellinger 	/*
2365b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2366b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2367b8d26b3bSNicholas Bellinger 	 */
2368b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
236913a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
2370b8d26b3bSNicholas Bellinger 
2371ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
2372b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2373b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2374b8d26b3bSNicholas Bellinger 		goto out;
2375b8d26b3bSNicholas Bellinger 	}
2376b8d26b3bSNicholas Bellinger 
2377ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
2378b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2379b8d26b3bSNicholas Bellinger 
2380b8d26b3bSNicholas Bellinger 	return 0;
2381b8d26b3bSNicholas Bellinger 
2382b8d26b3bSNicholas Bellinger out:
2383b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2384ca6c1d82SSagi Grimberg 
2385b8d26b3bSNicholas Bellinger 	return ret;
2386b8d26b3bSNicholas Bellinger }
2387b8d26b3bSNicholas Bellinger 
2388b8d26b3bSNicholas Bellinger static int
2389b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2390b8d26b3bSNicholas Bellinger {
2391dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2392b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2393b8d26b3bSNicholas Bellinger 	int ret;
239413bce482SJenny Derzhavetz 	struct iser_cm_hdr rsp_hdr;
2395b8d26b3bSNicholas Bellinger 
2396b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2397b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2398b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2399b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2400b8d26b3bSNicholas Bellinger 
240113bce482SJenny Derzhavetz 	memset(&rsp_hdr, 0, sizeof(rsp_hdr));
2402422bd0acSJenny Derzhavetz 	rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2403422bd0acSJenny Derzhavetz 	if (!isert_conn->snd_w_inv)
2404422bd0acSJenny Derzhavetz 		rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
240513bce482SJenny Derzhavetz 	cp.private_data = (void *)&rsp_hdr;
240613bce482SJenny Derzhavetz 	cp.private_data_len = sizeof(rsp_hdr);
240713bce482SJenny Derzhavetz 
2408b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2409b8d26b3bSNicholas Bellinger 	if (ret) {
241024f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
2411b8d26b3bSNicholas Bellinger 		return ret;
2412b8d26b3bSNicholas Bellinger 	}
2413b8d26b3bSNicholas Bellinger 
2414b8d26b3bSNicholas Bellinger 	return 0;
2415b8d26b3bSNicholas Bellinger }
2416b8d26b3bSNicholas Bellinger 
2417b8d26b3bSNicholas Bellinger static int
2418b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2419b8d26b3bSNicholas Bellinger {
24206700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2421b8d26b3bSNicholas Bellinger 	int ret;
2422b8d26b3bSNicholas Bellinger 
242324f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
24242371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
24252371e5daSSagi Grimberg 	if (ret) {
242624f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
24272371e5daSSagi Grimberg 			  isert_conn);
24282371e5daSSagi Grimberg 		return ret;
24292371e5daSSagi Grimberg 	}
24302371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
24312371e5daSSagi Grimberg 
24326faaa85fSNicholas Bellinger 	/*
24336faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
24346faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
24356faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
24366faaa85fSNicholas Bellinger 	 * into a NOP.
24376faaa85fSNicholas Bellinger 	 */
24386faaa85fSNicholas Bellinger 	if (!login->first_request)
24396faaa85fSNicholas Bellinger 		return 0;
2440b8d26b3bSNicholas Bellinger 
24412371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
24422371e5daSSagi Grimberg 
2443dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
2444dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
2445b8d26b3bSNicholas Bellinger 	if (ret)
2446b8d26b3bSNicholas Bellinger 		return ret;
2447b8d26b3bSNicholas Bellinger 
244824f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
24492371e5daSSagi Grimberg 
2450b8d26b3bSNicholas Bellinger 	return 0;
2451b8d26b3bSNicholas Bellinger }
2452b8d26b3bSNicholas Bellinger 
2453b8d26b3bSNicholas Bellinger static void
2454b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2455b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2456b8d26b3bSNicholas Bellinger {
2457dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2458b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2459b8d26b3bSNicholas Bellinger 
2460b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2461b8d26b3bSNicholas Bellinger 
2462dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
2463dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
2464b8d26b3bSNicholas Bellinger }
2465b8d26b3bSNicholas Bellinger 
2466b8d26b3bSNicholas Bellinger static int
2467b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2468b8d26b3bSNicholas Bellinger {
24696700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2470b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2471c6b8e918SSagi Grimberg 	int ret;
2472b8d26b3bSNicholas Bellinger 
2473b8d26b3bSNicholas Bellinger accept_wait:
2474ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
2475c6b8e918SSagi Grimberg 	if (ret)
2476b8d26b3bSNicholas Bellinger 		return -ENODEV;
2477b8d26b3bSNicholas Bellinger 
2478b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2479e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
2480b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
248111378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
2482e346ab34SSagi Grimberg 			 np->np_thread_state);
2483e346ab34SSagi Grimberg 		/**
2484e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
2485e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
2486e346ab34SSagi Grimberg 		 **/
2487b8d26b3bSNicholas Bellinger 		return -ENODEV;
2488b8d26b3bSNicholas Bellinger 	}
2489b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2490b8d26b3bSNicholas Bellinger 
2491ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2492bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
2493ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
2494b8d26b3bSNicholas Bellinger 		goto accept_wait;
2495b8d26b3bSNicholas Bellinger 	}
2496bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
2497bd379220SJenny Derzhavetz 			struct isert_conn, node);
2498bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
2499ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2500b8d26b3bSNicholas Bellinger 
2501b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2502b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2503aea92980SJenny Derzhavetz 	isert_conn->state = ISER_CONN_BOUND;
2504b8d26b3bSNicholas Bellinger 
2505b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2506b8d26b3bSNicholas Bellinger 
250724f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
25082371e5daSSagi Grimberg 
2509b8d26b3bSNicholas Bellinger 	return 0;
2510b8d26b3bSNicholas Bellinger }
2511b8d26b3bSNicholas Bellinger 
2512b8d26b3bSNicholas Bellinger static void
2513b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2514b8d26b3bSNicholas Bellinger {
25156700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2516268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
2517b8d26b3bSNicholas Bellinger 
2518ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
2519ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
2520b8d26b3bSNicholas Bellinger 
2521268e6811SSagi Grimberg 	/*
2522268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
2523268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
2524268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
2525268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
2526bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
2527268e6811SSagi Grimberg 	 */
2528ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2529bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
2530bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
2531268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
2532bd379220SJenny Derzhavetz 					 &isert_np->pending,
2533bd379220SJenny Derzhavetz 					 node) {
2534bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
2535bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
2536bd379220SJenny Derzhavetz 			isert_connect_release(isert_conn);
2537bd379220SJenny Derzhavetz 		}
2538bd379220SJenny Derzhavetz 	}
2539bd379220SJenny Derzhavetz 
2540bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
2541bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
2542bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
2543bd379220SJenny Derzhavetz 					 &isert_np->accepted,
2544bd379220SJenny Derzhavetz 					 node) {
254524f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
2546268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
2547268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
2548268e6811SSagi Grimberg 		}
2549268e6811SSagi Grimberg 	}
2550ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2551268e6811SSagi Grimberg 
2552b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2553b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2554b8d26b3bSNicholas Bellinger }
2555b8d26b3bSNicholas Bellinger 
2556b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
2557b02efbfcSSagi Grimberg {
2558b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
2559b02efbfcSSagi Grimberg 						     struct isert_conn,
2560b02efbfcSSagi Grimberg 						     release_work);
2561b02efbfcSSagi Grimberg 
256224f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
2563b02efbfcSSagi Grimberg 
2564dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2565b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
2566dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2567b02efbfcSSagi Grimberg 
256824f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
2569b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
2570b02efbfcSSagi Grimberg }
2571b02efbfcSSagi Grimberg 
2572bdf20e72SSagi Grimberg static void
2573991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
2574991bb764SSagi Grimberg {
2575991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
2576991bb764SSagi Grimberg 
25774c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
25784c22e07fSSagi Grimberg 
2579991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
258024f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
2581991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
2582991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
2583991bb764SSagi Grimberg 	}
2584991bb764SSagi Grimberg }
2585991bb764SSagi Grimberg 
2586991bb764SSagi Grimberg static void
2587c7e160eeSSagi Grimberg isert_wait4cmds(struct iscsi_conn *conn)
2588c7e160eeSSagi Grimberg {
25894c22e07fSSagi Grimberg 	isert_info("iscsi_conn %p\n", conn);
25904c22e07fSSagi Grimberg 
2591c7e160eeSSagi Grimberg 	if (conn->sess) {
2592c7e160eeSSagi Grimberg 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2593c7e160eeSSagi Grimberg 		target_wait_for_sess_cmds(conn->sess->se_sess);
2594c7e160eeSSagi Grimberg 	}
2595c7e160eeSSagi Grimberg }
2596c7e160eeSSagi Grimberg 
25973e03c4b0SJenny Derzhavetz /**
25983e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
25993e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
26003e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
26013e03c4b0SJenny Derzhavetz  *
26023e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
26033e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
26043e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
26053e03c4b0SJenny Derzhavetz  */
26063e03c4b0SJenny Derzhavetz static void
26073e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
26083e03c4b0SJenny Derzhavetz {
26093e03c4b0SJenny Derzhavetz 	struct iscsi_cmd *cmd, *tmp;
26103e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
26113e03c4b0SJenny Derzhavetz 
26123e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
26133e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
26143e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
26153e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
26163e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
26173e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
26183e03c4b0SJenny Derzhavetz 	}
26193e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
26203e03c4b0SJenny Derzhavetz 
26213e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
26223e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
26233e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
26243e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
26253e03c4b0SJenny Derzhavetz 
26263e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
26273e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
26283e03c4b0SJenny Derzhavetz 		}
26293e03c4b0SJenny Derzhavetz 	}
26303e03c4b0SJenny Derzhavetz }
26313e03c4b0SJenny Derzhavetz 
2632defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
2633b8d26b3bSNicholas Bellinger {
2634b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2635b8d26b3bSNicholas Bellinger 
26364c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
2637b8d26b3bSNicholas Bellinger 
2638dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2639954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2640dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2641b2cb9649SNicholas Bellinger 
2642572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
26433e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
26443e03c4b0SJenny Derzhavetz 	isert_wait4cmds(conn);
2645991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
2646954f2372SSagi Grimberg 
2647b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
2648defd8848SNicholas Bellinger }
2649defd8848SNicholas Bellinger 
2650defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2651defd8848SNicholas Bellinger {
2652defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2653b8d26b3bSNicholas Bellinger 
2654572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
2655b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2656b8d26b3bSNicholas Bellinger }
2657b8d26b3bSNicholas Bellinger 
2658187e84f7SVarun Prakash static void isert_get_rx_pdu(struct iscsi_conn *conn)
2659187e84f7SVarun Prakash {
2660187e84f7SVarun Prakash 	struct completion comp;
2661187e84f7SVarun Prakash 
2662187e84f7SVarun Prakash 	init_completion(&comp);
2663187e84f7SVarun Prakash 
2664187e84f7SVarun Prakash 	wait_for_completion_interruptible(&comp);
2665187e84f7SVarun Prakash }
2666187e84f7SVarun Prakash 
2667b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2668b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2669b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2670bd027d85SNicholas Bellinger 	.rdma_shutdown		= true,
2671d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2672b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2673b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2674b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2675b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2676defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2677b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2678b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2679b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2680b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2681b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2682b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2683b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2684b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2685131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
2686187e84f7SVarun Prakash 	.iscsit_get_rx_pdu	= isert_get_rx_pdu,
2687e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
2688b8d26b3bSNicholas Bellinger };
2689b8d26b3bSNicholas Bellinger 
2690b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2691b8d26b3bSNicholas Bellinger {
2692b8d26b3bSNicholas Bellinger 	int ret;
2693b8d26b3bSNicholas Bellinger 
2694631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
2695631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
2696b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
269724f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
26986f0fae3dSSagi Grimberg 		return -ENOMEM;
2699b8d26b3bSNicholas Bellinger 	}
2700b8d26b3bSNicholas Bellinger 
2701b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2702b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
2703b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
270424f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
2705b02efbfcSSagi Grimberg 		ret = -ENOMEM;
2706b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
2707b02efbfcSSagi Grimberg 	}
2708b02efbfcSSagi Grimberg 
2709b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
271024f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
2711b02efbfcSSagi Grimberg 
2712b8d26b3bSNicholas Bellinger 	return 0;
2713b8d26b3bSNicholas Bellinger 
2714b02efbfcSSagi Grimberg destroy_comp_wq:
2715b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
27166f0fae3dSSagi Grimberg 
2717b8d26b3bSNicholas Bellinger 	return ret;
2718b8d26b3bSNicholas Bellinger }
2719b8d26b3bSNicholas Bellinger 
2720b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2721b8d26b3bSNicholas Bellinger {
2722f5ebec96SSagi Grimberg 	flush_scheduled_work();
2723b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
2724b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2725b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
27264c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
2727b8d26b3bSNicholas Bellinger }
2728b8d26b3bSNicholas Bellinger 
2729b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2730b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2731b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2732b8d26b3bSNicholas Bellinger 
2733b8d26b3bSNicholas Bellinger module_init(isert_init);
2734b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2735