xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 33023fb85a42b53bf778bc025f9667b582282be4)
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;
139*33023fb8SSteve 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",
2654a295baeSSagi Grimberg 		   device->comps_used, device->ib_device->name,
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 
302*33023fb8SSteve Wise 	isert_dbg("devattr->max_send_sge: %d devattr->max_recv_sge %d\n",
303*33023fb8SSteve 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 {
813b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
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 
829dac6ab30SSagi Grimberg 	ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
830b8d26b3bSNicholas Bellinger 			   &rx_wr_failed);
8316d1fba0cSJenny Derzhavetz 	if (ret)
83224f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
8334366b19cSJenny Derzhavetz 
8344366b19cSJenny Derzhavetz 	return ret;
8354366b19cSJenny Derzhavetz }
8364366b19cSJenny Derzhavetz 
8374366b19cSJenny Derzhavetz static int
8384366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
8394366b19cSJenny Derzhavetz {
8404366b19cSJenny Derzhavetz 	struct ib_recv_wr *rx_wr_failed, rx_wr;
8414366b19cSJenny Derzhavetz 	int ret;
8424366b19cSJenny Derzhavetz 
8437a56dc88SSagi Grimberg 	if (!rx_desc->in_use) {
8447a56dc88SSagi Grimberg 		/*
8457a56dc88SSagi Grimberg 		 * if the descriptor is not in-use we already reposted it
8467a56dc88SSagi Grimberg 		 * for recv, so just silently return
8477a56dc88SSagi Grimberg 		 */
8487a56dc88SSagi Grimberg 		return 0;
8497a56dc88SSagi Grimberg 	}
8507a56dc88SSagi Grimberg 
8517a56dc88SSagi Grimberg 	rx_desc->in_use = false;
8529679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &rx_desc->rx_cqe;
8534366b19cSJenny Derzhavetz 	rx_wr.sg_list = &rx_desc->rx_sg;
8544366b19cSJenny Derzhavetz 	rx_wr.num_sge = 1;
8554366b19cSJenny Derzhavetz 	rx_wr.next = NULL;
8564366b19cSJenny Derzhavetz 
8574366b19cSJenny Derzhavetz 	ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
8586d1fba0cSJenny Derzhavetz 	if (ret)
8594366b19cSJenny Derzhavetz 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
8604366b19cSJenny Derzhavetz 
861b8d26b3bSNicholas Bellinger 	return ret;
862b8d26b3bSNicholas Bellinger }
863b8d26b3bSNicholas Bellinger 
864b8d26b3bSNicholas Bellinger static int
8659679cc51SChristoph Hellwig isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
866b8d26b3bSNicholas Bellinger {
867dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
868b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
869b8d26b3bSNicholas Bellinger 	int ret;
870b8d26b3bSNicholas Bellinger 
871b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
872b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
873b8d26b3bSNicholas Bellinger 
8749679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_login_send_done;
8759679cc51SChristoph Hellwig 
876b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
8779679cc51SChristoph Hellwig 	send_wr.wr_cqe	= &tx_desc->tx_cqe;
878b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
879b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
880b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
881b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
882b8d26b3bSNicholas Bellinger 
883dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
884bdf20e72SSagi Grimberg 	if (ret)
88524f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
886b8d26b3bSNicholas Bellinger 
887b8d26b3bSNicholas Bellinger 	return ret;
888b8d26b3bSNicholas Bellinger }
889b8d26b3bSNicholas Bellinger 
890b8d26b3bSNicholas Bellinger static void
891763b6965SAlex Estrin __isert_create_send_desc(struct isert_device *device,
892b8d26b3bSNicholas Bellinger 			 struct iser_tx_desc *tx_desc)
893b8d26b3bSNicholas Bellinger {
894b8d26b3bSNicholas Bellinger 
895d3cf81f9SSagi Grimberg 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
896d3cf81f9SSagi Grimberg 	tx_desc->iser_header.flags = ISCSI_CTRL;
897b8d26b3bSNicholas Bellinger 
898b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
899b8d26b3bSNicholas Bellinger 
90034efc7dfSJason Gunthorpe 	if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
90134efc7dfSJason Gunthorpe 		tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
90224f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
903b8d26b3bSNicholas Bellinger 	}
904b8d26b3bSNicholas Bellinger }
905b8d26b3bSNicholas Bellinger 
906763b6965SAlex Estrin static void
907763b6965SAlex Estrin isert_create_send_desc(struct isert_conn *isert_conn,
908763b6965SAlex Estrin 		       struct isert_cmd *isert_cmd,
909763b6965SAlex Estrin 		       struct iser_tx_desc *tx_desc)
910763b6965SAlex Estrin {
911763b6965SAlex Estrin 	struct isert_device *device = isert_conn->device;
912763b6965SAlex Estrin 	struct ib_device *ib_dev = device->ib_device;
913763b6965SAlex Estrin 
914763b6965SAlex Estrin 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
915763b6965SAlex Estrin 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
916763b6965SAlex Estrin 
917763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
918763b6965SAlex Estrin }
919763b6965SAlex Estrin 
920b8d26b3bSNicholas Bellinger static int
921b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
922b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
923b8d26b3bSNicholas Bellinger {
924dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
92567cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
926b8d26b3bSNicholas Bellinger 	u64 dma_addr;
927b8d26b3bSNicholas Bellinger 
928b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
929b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
930b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
93124f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
932b8d26b3bSNicholas Bellinger 		return -ENOMEM;
933b8d26b3bSNicholas Bellinger 	}
934b8d26b3bSNicholas Bellinger 
935b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
936b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
937b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
93834efc7dfSJason Gunthorpe 	tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
939b8d26b3bSNicholas Bellinger 
9404c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
9414c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
9424c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
943b8d26b3bSNicholas Bellinger 
944b8d26b3bSNicholas Bellinger 	return 0;
945b8d26b3bSNicholas Bellinger }
946b8d26b3bSNicholas Bellinger 
947b8d26b3bSNicholas Bellinger static void
94895b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
94968a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
950b8d26b3bSNicholas Bellinger {
95195b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
95295b60f07SNicholas Bellinger 
9539679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_send_done;
9549679cc51SChristoph Hellwig 	send_wr->wr_cqe = &tx_desc->tx_cqe;
955422bd0acSJenny Derzhavetz 
956422bd0acSJenny Derzhavetz 	if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
957422bd0acSJenny Derzhavetz 		send_wr->opcode  = IB_WR_SEND_WITH_INV;
958422bd0acSJenny Derzhavetz 		send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
959422bd0acSJenny Derzhavetz 	} else {
960b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_SEND;
961422bd0acSJenny Derzhavetz 	}
962422bd0acSJenny Derzhavetz 
96395b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
964b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
96595b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
966b8d26b3bSNicholas Bellinger }
967b8d26b3bSNicholas Bellinger 
968b8d26b3bSNicholas Bellinger static int
9699679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn)
970b8d26b3bSNicholas Bellinger {
971b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
972b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
973b8d26b3bSNicholas Bellinger 	int ret;
974b8d26b3bSNicholas Bellinger 
975b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
976b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
977ed1083b2SChristoph Hellwig 	sge.length = ISER_RX_PAYLOAD_SIZE;
97834efc7dfSJason Gunthorpe 	sge.lkey = isert_conn->device->pd->local_dma_lkey;
979b8d26b3bSNicholas Bellinger 
98024f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
981b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
982b8d26b3bSNicholas Bellinger 
9839679cc51SChristoph Hellwig 	isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
9849679cc51SChristoph Hellwig 
985b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
9869679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
987b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
988b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
989b8d26b3bSNicholas Bellinger 
990dac6ab30SSagi Grimberg 	ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
9916d1fba0cSJenny Derzhavetz 	if (ret)
99224f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
993b8d26b3bSNicholas Bellinger 
994b8d26b3bSNicholas Bellinger 	return ret;
995b8d26b3bSNicholas Bellinger }
996b8d26b3bSNicholas Bellinger 
997b8d26b3bSNicholas Bellinger static int
998b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
999b8d26b3bSNicholas Bellinger 		   u32 length)
1000b8d26b3bSNicholas Bellinger {
1001b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1002dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
100367cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1004dac6ab30SSagi Grimberg 	struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
1005b8d26b3bSNicholas Bellinger 	int ret;
1006b8d26b3bSNicholas Bellinger 
1007763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
1008b8d26b3bSNicholas Bellinger 
1009b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1010b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1011b8d26b3bSNicholas Bellinger 
1012b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1013b8d26b3bSNicholas Bellinger 
1014b8d26b3bSNicholas Bellinger 	if (length > 0) {
1015b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1016b8d26b3bSNicholas Bellinger 
1017b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1018b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1019b8d26b3bSNicholas Bellinger 
1020b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1021b8d26b3bSNicholas Bellinger 
1022b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1023b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1024b8d26b3bSNicholas Bellinger 
1025b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1026b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
102734efc7dfSJason Gunthorpe 		tx_dsg->lkey	= isert_conn->device->pd->local_dma_lkey;
1028b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1029b8d26b3bSNicholas Bellinger 	}
1030b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1031b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1032b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1033b8d26b3bSNicholas Bellinger 			if (ret)
1034b8d26b3bSNicholas Bellinger 				return ret;
1035b8d26b3bSNicholas Bellinger 
10364366b19cSJenny Derzhavetz 			ret = isert_post_recvm(isert_conn,
10374366b19cSJenny Derzhavetz 					       ISERT_QP_MAX_RECV_DTOS);
1038b8d26b3bSNicholas Bellinger 			if (ret)
1039b8d26b3bSNicholas Bellinger 				return ret;
1040b8d26b3bSNicholas Bellinger 
1041128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
1042dac6ab30SSagi Grimberg 			mutex_lock(&isert_conn->mutex);
1043128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
1044dac6ab30SSagi Grimberg 			mutex_unlock(&isert_conn->mutex);
1045b8d26b3bSNicholas Bellinger 			goto post_send;
1046b8d26b3bSNicholas Bellinger 		}
1047b8d26b3bSNicholas Bellinger 
10489679cc51SChristoph Hellwig 		ret = isert_login_post_recv(isert_conn);
1049b8d26b3bSNicholas Bellinger 		if (ret)
1050b8d26b3bSNicholas Bellinger 			return ret;
1051b8d26b3bSNicholas Bellinger 	}
1052b8d26b3bSNicholas Bellinger post_send:
10539679cc51SChristoph Hellwig 	ret = isert_login_post_send(isert_conn, tx_desc);
1054b8d26b3bSNicholas Bellinger 	if (ret)
1055b8d26b3bSNicholas Bellinger 		return ret;
1056b8d26b3bSNicholas Bellinger 
1057b8d26b3bSNicholas Bellinger 	return 0;
1058b8d26b3bSNicholas Bellinger }
1059b8d26b3bSNicholas Bellinger 
1060b8d26b3bSNicholas Bellinger static void
10612371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1062b8d26b3bSNicholas Bellinger {
10635adabdd1SChristoph Hellwig 	struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
10642371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1065b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1066b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1067b8d26b3bSNicholas Bellinger 	int size;
1068b8d26b3bSNicholas Bellinger 
106924f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
10702371e5daSSagi Grimberg 
10712371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1072b8d26b3bSNicholas Bellinger 
1073b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1074b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1075b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1076b8d26b3bSNicholas Bellinger 		/*
1077b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1078b8d26b3bSNicholas Bellinger 		 * login request PDU.
1079b8d26b3bSNicholas Bellinger 		 */
1080b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1081b8d26b3bSNicholas Bellinger 		login->current_stage =
1082b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1083b8d26b3bSNicholas Bellinger 			 >> 2;
1084b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1085b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1086b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1087b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1088b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1089b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1090b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1091b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1092b8d26b3bSNicholas Bellinger 	}
1093b8d26b3bSNicholas Bellinger 
1094b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1095b8d26b3bSNicholas Bellinger 
1096b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
10974c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
10984c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
10994c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
1100b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1101b8d26b3bSNicholas Bellinger 
11026faaa85fSNicholas Bellinger 	if (login->first_request) {
1103dac6ab30SSagi Grimberg 		complete(&isert_conn->login_comp);
11046faaa85fSNicholas Bellinger 		return;
11056faaa85fSNicholas Bellinger 	}
11066faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1107b8d26b3bSNicholas Bellinger }
1108b8d26b3bSNicholas Bellinger 
1109b8d26b3bSNicholas Bellinger static struct iscsi_cmd
11104366b19cSJenny Derzhavetz *isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
1111b8d26b3bSNicholas Bellinger {
11126700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1113b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1114d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1115b8d26b3bSNicholas Bellinger 
1116676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1117d703ce2fSNicholas Bellinger 	if (!cmd) {
111824f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1119b8d26b3bSNicholas Bellinger 		return NULL;
1120b8d26b3bSNicholas Bellinger 	}
1121d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1122b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1123d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
11244366b19cSJenny Derzhavetz 	isert_cmd->rx_desc = rx_desc;
1125b8d26b3bSNicholas Bellinger 
1126d703ce2fSNicholas Bellinger 	return cmd;
1127b8d26b3bSNicholas Bellinger }
1128b8d26b3bSNicholas Bellinger 
1129b8d26b3bSNicholas Bellinger static int
1130b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1131d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1132d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1133b8d26b3bSNicholas Bellinger {
1134b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1135b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1136b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1137b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
11389fd60088SJenny Derzhavetz 	unsigned int data_len;
1139b8d26b3bSNicholas Bellinger 
1140b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1141b8d26b3bSNicholas Bellinger 	if (rc < 0)
1142b8d26b3bSNicholas Bellinger 		return rc;
1143b8d26b3bSNicholas Bellinger 
1144b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1145b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1146b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
11479fd60088SJenny Derzhavetz 	data_len = cmd->se_cmd.data_length;
1148b8d26b3bSNicholas Bellinger 
11499fd60088SJenny Derzhavetz 	if (imm_data && imm_data_len == data_len)
11509fd60088SJenny Derzhavetz 		cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1151b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1152b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1153b8d26b3bSNicholas Bellinger 		return 0;
1154b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1155b8d26b3bSNicholas Bellinger 		dump_payload = true;
1156b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1157b8d26b3bSNicholas Bellinger 	}
1158b8d26b3bSNicholas Bellinger 
1159b8d26b3bSNicholas Bellinger 	if (!imm_data)
1160b8d26b3bSNicholas Bellinger 		return 0;
1161b8d26b3bSNicholas Bellinger 
11629fd60088SJenny Derzhavetz 	if (imm_data_len != data_len) {
1163b8d26b3bSNicholas Bellinger 		sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
11649fd60088SJenny Derzhavetz 		sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
11659fd60088SJenny Derzhavetz 				    &rx_desc->data[0], imm_data_len);
11669fd60088SJenny Derzhavetz 		isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
11679fd60088SJenny Derzhavetz 			  sg_nents, imm_data_len);
11689fd60088SJenny Derzhavetz 	} else {
11699fd60088SJenny Derzhavetz 		sg_init_table(&isert_cmd->sg, 1);
11709fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_sg = &isert_cmd->sg;
11719fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_nents = 1;
11729fd60088SJenny Derzhavetz 		sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
11739fd60088SJenny Derzhavetz 		isert_dbg("Transfer Immediate imm_data_len: %d\n",
11749fd60088SJenny Derzhavetz 			  imm_data_len);
11759fd60088SJenny Derzhavetz 	}
1176b8d26b3bSNicholas Bellinger 
1177b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1178b8d26b3bSNicholas Bellinger 
1179b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1180b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1181b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1182b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1183b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1184b8d26b3bSNicholas Bellinger 	}
1185b8d26b3bSNicholas Bellinger 
1186b8d26b3bSNicholas Bellinger sequence_cmd:
1187561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1188b8d26b3bSNicholas Bellinger 
1189b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1190b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
11916cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1192afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1193b8d26b3bSNicholas Bellinger 
1194b8d26b3bSNicholas Bellinger 	return 0;
1195b8d26b3bSNicholas Bellinger }
1196b8d26b3bSNicholas Bellinger 
1197b8d26b3bSNicholas Bellinger static int
1198b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1199b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1200b8d26b3bSNicholas Bellinger {
1201b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1202b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1203b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1204b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1205b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1206b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1207b8d26b3bSNicholas Bellinger 
1208b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1209b8d26b3bSNicholas Bellinger 	if (rc < 0)
1210b8d26b3bSNicholas Bellinger 		return rc;
1211b8d26b3bSNicholas Bellinger 	else if (!cmd)
1212b8d26b3bSNicholas Bellinger 		return 0;
1213b8d26b3bSNicholas Bellinger 	/*
1214b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1215b8d26b3bSNicholas Bellinger 	 */
1216b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
121724f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1218b8d26b3bSNicholas Bellinger 		dump_stack();
1219b8d26b3bSNicholas Bellinger 		return -1;
1220b8d26b3bSNicholas Bellinger 	}
1221b8d26b3bSNicholas Bellinger 
12224c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
12234c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
12244c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
12254c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1226b8d26b3bSNicholas Bellinger 
1227b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1228b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1229b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1230b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1231b8d26b3bSNicholas Bellinger 	/*
1232b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1233b8d26b3bSNicholas Bellinger 	 */
1234b8d26b3bSNicholas Bellinger 	if (page_off) {
12354c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1236b8d26b3bSNicholas Bellinger 		dump_stack();
1237b8d26b3bSNicholas Bellinger 		return -1;
1238b8d26b3bSNicholas Bellinger 	}
12394c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
12404c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
12414c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1242b8d26b3bSNicholas Bellinger 
1243b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1244b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1245b8d26b3bSNicholas Bellinger 
1246b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1247b8d26b3bSNicholas Bellinger 	if (rc < 0)
1248b8d26b3bSNicholas Bellinger 		return rc;
1249b8d26b3bSNicholas Bellinger 
12504366b19cSJenny Derzhavetz 	/*
12514366b19cSJenny Derzhavetz 	 * multiple data-outs on the same command can arrive -
12524366b19cSJenny Derzhavetz 	 * so post the buffer before hand
12534366b19cSJenny Derzhavetz 	 */
12544366b19cSJenny Derzhavetz 	rc = isert_post_recv(isert_conn, rx_desc);
12554366b19cSJenny Derzhavetz 	if (rc) {
12564366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", rc);
12574366b19cSJenny Derzhavetz 		return rc;
12584366b19cSJenny Derzhavetz 	}
1259b8d26b3bSNicholas Bellinger 	return 0;
1260b8d26b3bSNicholas Bellinger }
1261b8d26b3bSNicholas Bellinger 
1262b8d26b3bSNicholas Bellinger static int
1263778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1264d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1265d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1266778de368SNicholas Bellinger {
1267778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1268778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1269778de368SNicholas Bellinger 	int rc;
1270778de368SNicholas Bellinger 
1271778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1272778de368SNicholas Bellinger 	if (rc < 0)
1273778de368SNicholas Bellinger 		return rc;
1274778de368SNicholas Bellinger 	/*
1275778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1276778de368SNicholas Bellinger 	 */
1277778de368SNicholas Bellinger 
1278778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1279778de368SNicholas Bellinger }
1280778de368SNicholas Bellinger 
1281778de368SNicholas Bellinger static int
1282adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1283d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1284d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1285adb54c29SNicholas Bellinger {
1286adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1287adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1288adb54c29SNicholas Bellinger 	int rc;
1289b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1290adb54c29SNicholas Bellinger 
1291adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1292adb54c29SNicholas Bellinger 	if (rc < 0)
1293adb54c29SNicholas Bellinger 		return rc;
1294adb54c29SNicholas Bellinger 
1295b44a2b67SSagi Grimberg 	if (payload_length) {
1296adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
129793b80f29SLeon Romanovsky 		if (!text_in)
1298adb54c29SNicholas Bellinger 			return -ENOMEM;
1299adb54c29SNicholas Bellinger 	}
1300adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1301adb54c29SNicholas Bellinger 
1302adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1303adb54c29SNicholas Bellinger 
1304adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1305adb54c29SNicholas Bellinger }
1306adb54c29SNicholas Bellinger 
1307adb54c29SNicholas Bellinger static int
1308b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1309b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1310b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1311b8d26b3bSNicholas Bellinger {
1312b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1313b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1314b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1315b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1316b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1317b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1318b8d26b3bSNicholas Bellinger 
1319fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1320ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
132124f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1322ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1323ca40d24eSNicholas Bellinger 		return 0;
1324ca40d24eSNicholas Bellinger 	}
1325ca40d24eSNicholas Bellinger 
1326b8d26b3bSNicholas Bellinger 	switch (opcode) {
1327b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
13284366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1329b8d26b3bSNicholas Bellinger 		if (!cmd)
1330b8d26b3bSNicholas Bellinger 			break;
1331b8d26b3bSNicholas Bellinger 
1332d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1333b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1334b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1335b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1336b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1337422bd0acSJenny Derzhavetz 		isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
1338b8d26b3bSNicholas Bellinger 
1339d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1340b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1341b8d26b3bSNicholas Bellinger 		break;
1342b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
13434366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1344b8d26b3bSNicholas Bellinger 		if (!cmd)
1345b8d26b3bSNicholas Bellinger 			break;
1346b8d26b3bSNicholas Bellinger 
1347d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1348d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1349778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1350b8d26b3bSNicholas Bellinger 		break;
1351b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1352b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1353b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1354b8d26b3bSNicholas Bellinger 		break;
1355b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
13564366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1357b8d26b3bSNicholas Bellinger 		if (!cmd)
1358b8d26b3bSNicholas Bellinger 			break;
1359b8d26b3bSNicholas Bellinger 
1360b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1361b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1362b8d26b3bSNicholas Bellinger 		break;
1363b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
13644366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1365b8d26b3bSNicholas Bellinger 		if (!cmd)
1366b8d26b3bSNicholas Bellinger 			break;
1367b8d26b3bSNicholas Bellinger 
1368b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1369b8d26b3bSNicholas Bellinger 		break;
1370adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
13714366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1372e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
13734366b19cSJenny Derzhavetz 		else
13744366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
13754366b19cSJenny Derzhavetz 
1376e4f4e801SSagi Grimberg 		if (!cmd)
1377e4f4e801SSagi Grimberg 			break;
1378adb54c29SNicholas Bellinger 
1379d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1380d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1381adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1382adb54c29SNicholas Bellinger 		break;
1383b8d26b3bSNicholas Bellinger 	default:
138424f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1385b8d26b3bSNicholas Bellinger 		dump_stack();
1386b8d26b3bSNicholas Bellinger 		break;
1387b8d26b3bSNicholas Bellinger 	}
1388b8d26b3bSNicholas Bellinger 
1389b8d26b3bSNicholas Bellinger 	return ret;
1390b8d26b3bSNicholas Bellinger }
1391b8d26b3bSNicholas Bellinger 
1392b8d26b3bSNicholas Bellinger static void
13939679cc51SChristoph Hellwig isert_print_wc(struct ib_wc *wc, const char *type)
1394b8d26b3bSNicholas Bellinger {
13959679cc51SChristoph Hellwig 	if (wc->status != IB_WC_WR_FLUSH_ERR)
13969679cc51SChristoph Hellwig 		isert_err("%s failure: %s (%d) vend_err %x\n", type,
13979679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status,
13989679cc51SChristoph Hellwig 			  wc->vendor_err);
13999679cc51SChristoph Hellwig 	else
14009679cc51SChristoph Hellwig 		isert_dbg("%s failure: %s (%d)\n", type,
14019679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status);
14029679cc51SChristoph Hellwig }
14039679cc51SChristoph Hellwig 
14049679cc51SChristoph Hellwig static void
14059679cc51SChristoph Hellwig isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
14069679cc51SChristoph Hellwig {
14079679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
14089679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
14099679cc51SChristoph Hellwig 	struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
14109679cc51SChristoph Hellwig 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1411d3cf81f9SSagi Grimberg 	struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
1412b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1413b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1414b8d26b3bSNicholas Bellinger 
14159679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
14169679cc51SChristoph Hellwig 		isert_print_wc(wc, "recv");
14179679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
14189679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
14199679cc51SChristoph Hellwig 		return;
14209679cc51SChristoph Hellwig 	}
14219679cc51SChristoph Hellwig 
14227a56dc88SSagi Grimberg 	rx_desc->in_use = true;
14237a56dc88SSagi Grimberg 
14249679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
14259679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
14269679cc51SChristoph Hellwig 
14279679cc51SChristoph Hellwig 	isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
14289679cc51SChristoph Hellwig 		 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
14299679cc51SChristoph Hellwig 		 (int)(wc->byte_len - ISER_HEADERS_LEN));
14309679cc51SChristoph Hellwig 
1431d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1432b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1433d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1434d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1435d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
14364c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1437b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1438b8d26b3bSNicholas Bellinger 		}
1439d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1440d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1441d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
14424c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1443b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1444b8d26b3bSNicholas Bellinger 		}
1445b8d26b3bSNicholas Bellinger 
144624f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1447b8d26b3bSNicholas Bellinger 		break;
1448b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
144924f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1450b8d26b3bSNicholas Bellinger 		break;
1451b8d26b3bSNicholas Bellinger 	default:
1452d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1453b8d26b3bSNicholas Bellinger 		break;
1454b8d26b3bSNicholas Bellinger 	}
1455b8d26b3bSNicholas Bellinger 
14566c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1457b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
14589679cc51SChristoph Hellwig 
14599679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
14609679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1461b8d26b3bSNicholas Bellinger }
1462b8d26b3bSNicholas Bellinger 
1463b8d26b3bSNicholas Bellinger static void
14649679cc51SChristoph Hellwig isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1465b8d26b3bSNicholas Bellinger {
14669679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1467fce50a2fSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->device->ib_device;
1468b8d26b3bSNicholas Bellinger 
14699679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
14709679cc51SChristoph Hellwig 		isert_print_wc(wc, "login recv");
14719679cc51SChristoph Hellwig 		return;
1472b8d26b3bSNicholas Bellinger 	}
1473b8d26b3bSNicholas Bellinger 
14749679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
14759679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1476b8d26b3bSNicholas Bellinger 
14779679cc51SChristoph Hellwig 	isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
1478b8d26b3bSNicholas Bellinger 
14792371e5daSSagi Grimberg 	if (isert_conn->conn) {
14802371e5daSSagi Grimberg 		struct iscsi_login *login = isert_conn->conn->conn_login;
14812371e5daSSagi Grimberg 
14822371e5daSSagi Grimberg 		if (login && !login->first_request)
14832371e5daSSagi Grimberg 			isert_rx_login_req(isert_conn);
14842371e5daSSagi Grimberg 	}
14859679cc51SChristoph Hellwig 
1486dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
14872371e5daSSagi Grimberg 	complete(&isert_conn->login_req_comp);
1488dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
1489b8d26b3bSNicholas Bellinger 
14909679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
14919679cc51SChristoph Hellwig 				ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1492b8d26b3bSNicholas Bellinger }
1493b8d26b3bSNicholas Bellinger 
1494e3d7e4c3SSagi Grimberg static void
149538a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
1496e3d7e4c3SSagi Grimberg {
149738a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
149838a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
1499e3d7e4c3SSagi Grimberg 
150038a2d0d4SChristoph Hellwig 	if (!cmd->rw.nr_ops)
150138a2d0d4SChristoph Hellwig 		return;
150238a2d0d4SChristoph Hellwig 
150338a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
150438a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
150538a2d0d4SChristoph Hellwig 				conn->cm_id->port_num, se_cmd->t_data_sg,
150638a2d0d4SChristoph Hellwig 				se_cmd->t_data_nents, se_cmd->t_prot_sg,
150738a2d0d4SChristoph Hellwig 				se_cmd->t_prot_nents, dir);
150838a2d0d4SChristoph Hellwig 	} else {
150938a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
151038a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
1511e3d7e4c3SSagi Grimberg 	}
1512e3d7e4c3SSagi Grimberg 
151338a2d0d4SChristoph Hellwig 	cmd->rw.nr_ops = 0;
151459464ef4SVu Pham }
151559464ef4SVu Pham 
151659464ef4SVu Pham static void
151703e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1518b8d26b3bSNicholas Bellinger {
1519d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1520b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1521186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1522e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1523b8d26b3bSNicholas Bellinger 
15244c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1525b8d26b3bSNicholas Bellinger 
1526b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1527b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1528b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1529b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15305159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1531b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1532b8d26b3bSNicholas Bellinger 
153303e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1534b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
153503e7848aSNicholas Bellinger 			/*
153603e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
153703e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
153803e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
153903e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
154003e7848aSNicholas Bellinger 			 */
154103e7848aSNicholas Bellinger 			if (comp_err &&
154203e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
154303e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
154403e7848aSNicholas Bellinger 
1545afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
154603e7848aSNicholas Bellinger 			}
154703e7848aSNicholas Bellinger 		}
1548b8d26b3bSNicholas Bellinger 
154938a2d0d4SChristoph Hellwig 		isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1550186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1551186a9647SNicholas Bellinger 		break;
1552b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1553186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1554186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15555159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1556186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1557186a9647SNicholas Bellinger 
1558b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1559b8d26b3bSNicholas Bellinger 		break;
1560b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1561b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1562adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1563e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1564e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1565e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1566e4f4e801SSagi Grimberg 			break;
1567e4f4e801SSagi Grimberg 
1568b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1569b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15705159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1571b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1572b8d26b3bSNicholas Bellinger 
1573b8d26b3bSNicholas Bellinger 		/*
1574b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1575b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1576b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1577b8d26b3bSNicholas Bellinger 		 */
1578b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
157911378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
15803df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1581b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1582b8d26b3bSNicholas Bellinger 			break;
1583b8d26b3bSNicholas Bellinger 		}
1584d2271bd0SBart Van Assche 		/* fall through */
1585b8d26b3bSNicholas Bellinger 	default:
1586d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1587b8d26b3bSNicholas Bellinger 		break;
1588b8d26b3bSNicholas Bellinger 	}
1589b8d26b3bSNicholas Bellinger }
1590b8d26b3bSNicholas Bellinger 
1591b8d26b3bSNicholas Bellinger static void
1592b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1593b8d26b3bSNicholas Bellinger {
1594b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
15954c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1596b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1597b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1598b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1599b8d26b3bSNicholas Bellinger 	}
1600b8d26b3bSNicholas Bellinger }
1601b8d26b3bSNicholas Bellinger 
1602b8d26b3bSNicholas Bellinger static void
1603b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
160403e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1605b8d26b3bSNicholas Bellinger {
1606dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
16074c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1608dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1609dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1610dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1611b8d26b3bSNicholas Bellinger 	}
1612b8d26b3bSNicholas Bellinger 
1613b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
161403e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1615b8d26b3bSNicholas Bellinger }
1616b8d26b3bSNicholas Bellinger 
161796b7973eSSagi Grimberg static int
161896b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1619f93f3a70SSagi Grimberg {
16209e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
162196b7973eSSagi Grimberg 	int ret;
1622f93f3a70SSagi Grimberg 
162396b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
16249e961ae7SSagi Grimberg 	if (ret) {
162524f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
16269e961ae7SSagi Grimberg 		goto fail_mr_status;
16279e961ae7SSagi Grimberg 	}
162896b7973eSSagi Grimberg 
16299e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
163096b7973eSSagi Grimberg 		u64 sec_offset_err;
16319e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
16329e961ae7SSagi Grimberg 
16339e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
16349e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
16359e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
16369e961ae7SSagi Grimberg 			break;
16379e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
16389e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
16399e961ae7SSagi Grimberg 			break;
16409e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
16419e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
16429e961ae7SSagi Grimberg 			break;
16439e961ae7SSagi Grimberg 		}
164496b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
164596b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
164696b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
16479e961ae7SSagi Grimberg 
16484c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
16499e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
16509e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
16519e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
16529e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
16539e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
16549e961ae7SSagi Grimberg 		ret = 1;
16559e961ae7SSagi Grimberg 	}
16569e961ae7SSagi Grimberg 
16579e961ae7SSagi Grimberg fail_mr_status:
165896b7973eSSagi Grimberg 	return ret;
165996b7973eSSagi Grimberg }
166096b7973eSSagi Grimberg 
166196b7973eSSagi Grimberg static void
16629679cc51SChristoph Hellwig isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
166396b7973eSSagi Grimberg {
16649679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1665dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
16669679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1667e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
16689679cc51SChristoph Hellwig 	struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
166996b7973eSSagi Grimberg 	int ret = 0;
167096b7973eSSagi Grimberg 
16719679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16729679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma write");
16739679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16749679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16759679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
16769679cc51SChristoph Hellwig 		return;
16779679cc51SChristoph Hellwig 	}
16789679cc51SChristoph Hellwig 
16799679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
16809679cc51SChristoph Hellwig 
168138a2d0d4SChristoph Hellwig 	ret = isert_check_pi_status(cmd, isert_cmd->rw.sig->sig_mr);
168238a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
168396b7973eSSagi Grimberg 
1684555a65f6SNicholas Bellinger 	if (ret) {
1685555a65f6SNicholas Bellinger 		/*
1686555a65f6SNicholas Bellinger 		 * transport_generic_request_failure() expects to have
1687555a65f6SNicholas Bellinger 		 * plus two references to handle queue-full, so re-add
1688555a65f6SNicholas Bellinger 		 * one here as target-core will have already dropped
1689555a65f6SNicholas Bellinger 		 * it after the first isert_put_datain() callback.
1690555a65f6SNicholas Bellinger 		 */
1691555a65f6SNicholas Bellinger 		kref_get(&cmd->cmd_kref);
1692555a65f6SNicholas Bellinger 		transport_generic_request_failure(cmd, cmd->pi_err);
1693555a65f6SNicholas Bellinger 	} else {
1694555a65f6SNicholas Bellinger 		/*
1695555a65f6SNicholas Bellinger 		 * XXX: isert_put_response() failure is not retried.
1696555a65f6SNicholas Bellinger 		 */
1697555a65f6SNicholas Bellinger 		ret = isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
16989e961ae7SSagi Grimberg 		if (ret)
1699555a65f6SNicholas Bellinger 			pr_warn_ratelimited("isert_put_response() ret: %d\n", ret);
1700555a65f6SNicholas Bellinger 	}
1701f93f3a70SSagi Grimberg }
1702f93f3a70SSagi Grimberg 
1703f93f3a70SSagi Grimberg static void
17049679cc51SChristoph Hellwig isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1705b8d26b3bSNicholas Bellinger {
17069679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17079679cc51SChristoph Hellwig 	struct isert_device *device = isert_conn->device;
17089679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1709e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
1710d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1711b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
17125bac4b1aSSagi Grimberg 	int ret = 0;
1713b8d26b3bSNicholas Bellinger 
17149679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17159679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma read");
17169679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17179679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17189679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
17199679cc51SChristoph Hellwig 		return;
17209679cc51SChristoph Hellwig 	}
17219679cc51SChristoph Hellwig 
17229679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
17239679cc51SChristoph Hellwig 
1724b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
172538a2d0d4SChristoph Hellwig 
172638a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd))
172738a2d0d4SChristoph Hellwig 		ret = isert_check_pi_status(se_cmd, isert_cmd->rw.sig->sig_mr);
172838a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
172938a2d0d4SChristoph Hellwig 	cmd->write_data_done = 0;
1730b8d26b3bSNicholas Bellinger 
173124f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1732b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1733b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1734b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1735b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1736b8d26b3bSNicholas Bellinger 
1737555a65f6SNicholas Bellinger 	/*
1738555a65f6SNicholas Bellinger 	 * transport_generic_request_failure() will drop the extra
1739555a65f6SNicholas Bellinger 	 * se_cmd->cmd_kref reference after T10-PI error, and handle
1740555a65f6SNicholas Bellinger 	 * any non-zero ->queue_status() callback error retries.
1741555a65f6SNicholas Bellinger 	 */
1742555a65f6SNicholas Bellinger 	if (ret)
1743555a65f6SNicholas Bellinger 		transport_generic_request_failure(se_cmd, se_cmd->pi_err);
1744555a65f6SNicholas Bellinger 	else
1745b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1746b8d26b3bSNicholas Bellinger }
1747b8d26b3bSNicholas Bellinger 
1748b8d26b3bSNicholas Bellinger static void
1749b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1750b8d26b3bSNicholas Bellinger {
1751b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1752b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1753b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1754dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1755d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1756b8d26b3bSNicholas Bellinger 
17574c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
17584c22e07fSSagi Grimberg 
1759b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1760b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1761b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1762d2271bd0SBart Van Assche 		/* fall through */
1763d2271bd0SBart Van Assche 	case ISTATE_SEND_REJECT:
1764d2271bd0SBart Van Assche 	case ISTATE_SEND_TEXTRSP:
1765b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
17664c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
17674c22e07fSSagi Grimberg 				     ib_dev, false);
17683df8f68aSNicholas Bellinger 		break;
1769b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1770b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1771b8d26b3bSNicholas Bellinger 		break;
1772b8d26b3bSNicholas Bellinger 	default:
17734c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1774b8d26b3bSNicholas Bellinger 		dump_stack();
1775b8d26b3bSNicholas Bellinger 		break;
1776b8d26b3bSNicholas Bellinger 	}
1777b8d26b3bSNicholas Bellinger }
1778b8d26b3bSNicholas Bellinger 
1779b8d26b3bSNicholas Bellinger static void
17809679cc51SChristoph Hellwig isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
1781b8d26b3bSNicholas Bellinger {
17829679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17839679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
17849679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1785b8d26b3bSNicholas Bellinger 
17869679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17879679cc51SChristoph Hellwig 		isert_print_wc(wc, "login send");
17889679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17899679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17909679cc51SChristoph Hellwig 	}
17919679cc51SChristoph Hellwig 
17929679cc51SChristoph Hellwig 	isert_unmap_tx_desc(tx_desc, ib_dev);
17939679cc51SChristoph Hellwig }
17949679cc51SChristoph Hellwig 
17959679cc51SChristoph Hellwig static void
17969679cc51SChristoph Hellwig isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
17979679cc51SChristoph Hellwig {
17989679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17999679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
18009679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1801e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
18029679cc51SChristoph Hellwig 
18039679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
18049679cc51SChristoph Hellwig 		isert_print_wc(wc, "send");
18059679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
18069679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
18079679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
18089679cc51SChristoph Hellwig 		return;
18099679cc51SChristoph Hellwig 	}
18109679cc51SChristoph Hellwig 
18119679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
18129679cc51SChristoph Hellwig 
18139679cc51SChristoph Hellwig 	switch (isert_cmd->iscsi_cmd->i_state) {
18149679cc51SChristoph Hellwig 	case ISTATE_SEND_TASKMGTRSP:
18159679cc51SChristoph Hellwig 	case ISTATE_SEND_LOGOUTRSP:
18169679cc51SChristoph Hellwig 	case ISTATE_SEND_REJECT:
18179679cc51SChristoph Hellwig 	case ISTATE_SEND_TEXTRSP:
1818b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1819b8d26b3bSNicholas Bellinger 
1820b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1821b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1822b8d26b3bSNicholas Bellinger 		return;
1823b8d26b3bSNicholas Bellinger 	default:
18249679cc51SChristoph Hellwig 		isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
18259679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1826b8d26b3bSNicholas Bellinger 		break;
1827b8d26b3bSNicholas Bellinger 	}
1828b8d26b3bSNicholas Bellinger }
1829b8d26b3bSNicholas Bellinger 
1830b8d26b3bSNicholas Bellinger static int
1831b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1832b8d26b3bSNicholas Bellinger {
1833b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
1834b8d26b3bSNicholas Bellinger 	int ret;
1835b8d26b3bSNicholas Bellinger 
18364366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
18374366b19cSJenny Derzhavetz 	if (ret) {
18384366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", ret);
18394366b19cSJenny Derzhavetz 		return ret;
18404366b19cSJenny Derzhavetz 	}
18414366b19cSJenny Derzhavetz 
1842dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
1843b8d26b3bSNicholas Bellinger 			   &wr_failed);
1844b8d26b3bSNicholas Bellinger 	if (ret) {
184524f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
1846b8d26b3bSNicholas Bellinger 		return ret;
1847b8d26b3bSNicholas Bellinger 	}
1848b8d26b3bSNicholas Bellinger 	return ret;
1849b8d26b3bSNicholas Bellinger }
1850b8d26b3bSNicholas Bellinger 
1851b8d26b3bSNicholas Bellinger static int
1852b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1853b8d26b3bSNicholas Bellinger {
1854d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18556700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1856b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1857b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1858b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1859b8d26b3bSNicholas Bellinger 
1860b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1861b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1862b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1863b8d26b3bSNicholas Bellinger 	/*
1864b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1865b8d26b3bSNicholas Bellinger 	 */
1866b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1867b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1868b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1869dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
187067cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1871b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1872dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1873b8d26b3bSNicholas Bellinger 
1874b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1875b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1876b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1877b8d26b3bSNicholas Bellinger 
1878b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1879b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1880dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1881b8d26b3bSNicholas Bellinger 
1882dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1883dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1884b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1885def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1886def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1887b8d26b3bSNicholas Bellinger 
1888dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1889dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1890dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
189134efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1892b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1893b8d26b3bSNicholas Bellinger 	}
1894b8d26b3bSNicholas Bellinger 
189568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1896b8d26b3bSNicholas Bellinger 
18974c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
1898b8d26b3bSNicholas Bellinger 
1899b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1900b8d26b3bSNicholas Bellinger }
1901b8d26b3bSNicholas Bellinger 
1902131e6abcSNicholas Bellinger static void
1903131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1904131e6abcSNicholas Bellinger {
1905131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19066700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1907131e6abcSNicholas Bellinger 
1908131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
1909131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
1910131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
1911131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1912131e6abcSNicholas Bellinger 
1913131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
1914131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
191538a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1916131e6abcSNicholas Bellinger }
1917131e6abcSNicholas Bellinger 
1918e70beee7SNicholas Bellinger static enum target_prot_op
1919e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
1920e70beee7SNicholas Bellinger {
19216700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1922dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1923e70beee7SNicholas Bellinger 
192423a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
192523a548eeSSagi Grimberg 		if (device->pi_capable) {
192624f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
192723a548eeSSagi Grimberg 			isert_conn->pi_support = true;
1928e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
192923a548eeSSagi Grimberg 		}
193023a548eeSSagi Grimberg 	}
193123a548eeSSagi Grimberg 
193224f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
193323a548eeSSagi Grimberg 	isert_conn->pi_support = false;
1934e70beee7SNicholas Bellinger 
1935e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
1936e70beee7SNicholas Bellinger }
1937e70beee7SNicholas Bellinger 
1938b8d26b3bSNicholas Bellinger static int
1939b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1940b8d26b3bSNicholas Bellinger 		bool nopout_response)
1941b8d26b3bSNicholas Bellinger {
1942d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19436700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1944b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1945b8d26b3bSNicholas Bellinger 
1946b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1947b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1948b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1949b8d26b3bSNicholas Bellinger 			       nopout_response);
1950b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
195168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1952b8d26b3bSNicholas Bellinger 
19534c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
1954b8d26b3bSNicholas Bellinger 
1955b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1956b8d26b3bSNicholas Bellinger }
1957b8d26b3bSNicholas Bellinger 
1958b8d26b3bSNicholas Bellinger static int
1959b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1960b8d26b3bSNicholas Bellinger {
1961d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19626700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1963b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1964b8d26b3bSNicholas Bellinger 
1965b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1966b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1967b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1968b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
196968a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1970b8d26b3bSNicholas Bellinger 
19714c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
1972b8d26b3bSNicholas Bellinger 
1973b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1974b8d26b3bSNicholas Bellinger }
1975b8d26b3bSNicholas Bellinger 
1976b8d26b3bSNicholas Bellinger static int
1977b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1978b8d26b3bSNicholas Bellinger {
1979d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19806700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1981b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1982b8d26b3bSNicholas Bellinger 
1983b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1984b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1985b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1986b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
198768a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1988b8d26b3bSNicholas Bellinger 
19894c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
1990b8d26b3bSNicholas Bellinger 
1991b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1992b8d26b3bSNicholas Bellinger }
1993b8d26b3bSNicholas Bellinger 
1994b8d26b3bSNicholas Bellinger static int
1995b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1996b8d26b3bSNicholas Bellinger {
1997d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19986700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1999b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2000dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
200167cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
20023df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
20033df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
20043df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2005b8d26b3bSNicholas Bellinger 
2006b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
20073df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2008b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
20093df8f68aSNicholas Bellinger 
20103df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2011dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
20123df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
20133df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2014def4a6ffSAlexey Khoroshilov 	if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
2015def4a6ffSAlexey Khoroshilov 		return -ENOMEM;
2016dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2017dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
20183df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
201934efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
20203df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
20213df8f68aSNicholas Bellinger 
202268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2023b8d26b3bSNicholas Bellinger 
20244c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
2025b8d26b3bSNicholas Bellinger 
2026b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2027b8d26b3bSNicholas Bellinger }
2028b8d26b3bSNicholas Bellinger 
2029b8d26b3bSNicholas Bellinger static int
2030adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2031adb54c29SNicholas Bellinger {
2032d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
20336700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2034adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2035adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2036adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2037adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2038adb54c29SNicholas Bellinger 	int rc;
2039adb54c29SNicholas Bellinger 
2040adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
204122c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2042adb54c29SNicholas Bellinger 	if (rc < 0)
2043adb54c29SNicholas Bellinger 		return rc;
2044adb54c29SNicholas Bellinger 
2045adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2046adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2047adb54c29SNicholas Bellinger 
2048adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2049dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
205067cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2051adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2052adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2053adb54c29SNicholas Bellinger 
2054adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2055adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2056def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
2057def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
2058adb54c29SNicholas Bellinger 
2059adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2060adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2061adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
206234efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
2063adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2064adb54c29SNicholas Bellinger 	}
206568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2066adb54c29SNicholas Bellinger 
2067f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
2068adb54c29SNicholas Bellinger 
2069adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2070adb54c29SNicholas Bellinger }
2071adb54c29SNicholas Bellinger 
20723d73cf1aSSagi Grimberg static inline void
20733d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
20743d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
20759e961ae7SSagi Grimberg {
207678eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
20773d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
20783d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
20793d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
208078eda2bbSSagi Grimberg 	/*
208178eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
208278eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
208378eda2bbSSagi Grimberg 	 * from se_cmd.
208478eda2bbSSagi Grimberg 	 */
208578eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
208678eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
208778eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
208878eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
208978eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
209078eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
20913d73cf1aSSagi Grimberg };
20923d73cf1aSSagi Grimberg 
20933d73cf1aSSagi Grimberg static int
20943d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
20953d73cf1aSSagi Grimberg {
209638a2d0d4SChristoph Hellwig 	memset(sig_attrs, 0, sizeof(*sig_attrs));
209738a2d0d4SChristoph Hellwig 
20989e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
20999e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
21009e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
210178eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
21023d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
21039e961ae7SSagi Grimberg 		break;
21049e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
21059e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
210678eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
21073d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
21089e961ae7SSagi Grimberg 		break;
21099e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
21109e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
21113d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
21123d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
21139e961ae7SSagi Grimberg 		break;
21149e961ae7SSagi Grimberg 	default:
211524f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
21169e961ae7SSagi Grimberg 		return -EINVAL;
21179e961ae7SSagi Grimberg 	}
21189e961ae7SSagi Grimberg 
211926232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD)
212026232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
212126232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG)
212226232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
212326232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)
212426232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
212526232872SMax Gurtovoy 
21269e961ae7SSagi Grimberg 	return 0;
21279e961ae7SSagi Grimberg }
21289e961ae7SSagi Grimberg 
21299e961ae7SSagi Grimberg static int
213038a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
213138a2d0d4SChristoph Hellwig 		struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
21329e961ae7SSagi Grimberg {
213338a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
213438a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
213538a2d0d4SChristoph Hellwig 	u8 port_num = conn->cm_id->port_num;
213638a2d0d4SChristoph Hellwig 	u64 addr;
213738a2d0d4SChristoph Hellwig 	u32 rkey, offset;
21389e961ae7SSagi Grimberg 	int ret;
21399e961ae7SSagi Grimberg 
214066f53e6fSBharat Potnuri 	if (cmd->ctx_init_done)
214166f53e6fSBharat Potnuri 		goto rdma_ctx_post;
214266f53e6fSBharat Potnuri 
214338a2d0d4SChristoph Hellwig 	if (dir == DMA_FROM_DEVICE) {
214438a2d0d4SChristoph Hellwig 		addr = cmd->write_va;
214538a2d0d4SChristoph Hellwig 		rkey = cmd->write_stag;
214638a2d0d4SChristoph Hellwig 		offset = cmd->iscsi_cmd->write_data_done;
214738a2d0d4SChristoph Hellwig 	} else {
214838a2d0d4SChristoph Hellwig 		addr = cmd->read_va;
214938a2d0d4SChristoph Hellwig 		rkey = cmd->read_stag;
215038a2d0d4SChristoph Hellwig 		offset = 0;
215138a2d0d4SChristoph Hellwig 	}
215238a2d0d4SChristoph Hellwig 
215338a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
215438a2d0d4SChristoph Hellwig 		struct ib_sig_attrs sig_attrs;
215538a2d0d4SChristoph Hellwig 
21569e961ae7SSagi Grimberg 		ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
21579e961ae7SSagi Grimberg 		if (ret)
2158e3d7e4c3SSagi Grimberg 			return ret;
2159e3d7e4c3SSagi Grimberg 
216038a2d0d4SChristoph Hellwig 		WARN_ON_ONCE(offset);
216138a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
216238a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
216338a2d0d4SChristoph Hellwig 				se_cmd->t_prot_sg, se_cmd->t_prot_nents,
216438a2d0d4SChristoph Hellwig 				&sig_attrs, addr, rkey, dir);
2165570db170SSagi Grimberg 	} else {
216638a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
216738a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
216838a2d0d4SChristoph Hellwig 				offset, addr, rkey, dir);
216938a2d0d4SChristoph Hellwig 	}
217066f53e6fSBharat Potnuri 
217138a2d0d4SChristoph Hellwig 	if (ret < 0) {
217238a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
217338a2d0d4SChristoph Hellwig 		return ret;
21749e961ae7SSagi Grimberg 	}
21759e961ae7SSagi Grimberg 
217666f53e6fSBharat Potnuri 	cmd->ctx_init_done = true;
217766f53e6fSBharat Potnuri 
217866f53e6fSBharat Potnuri rdma_ctx_post:
217938a2d0d4SChristoph Hellwig 	ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
218038a2d0d4SChristoph Hellwig 	if (ret < 0)
218138a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to post RDMA res\n", cmd);
218259464ef4SVu Pham 	return ret;
218359464ef4SVu Pham }
218459464ef4SVu Pham 
218559464ef4SVu Pham static int
218690ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
218790ecc6e2SVu Pham {
218890ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
218959464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
21906700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
219138a2d0d4SChristoph Hellwig 	struct ib_cqe *cqe = NULL;
219238a2d0d4SChristoph Hellwig 	struct ib_send_wr *chain_wr = NULL;
219390ecc6e2SVu Pham 	int rc;
219490ecc6e2SVu Pham 
219524f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
219690ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
21974c22e07fSSagi Grimberg 
219838a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd)) {
219938a2d0d4SChristoph Hellwig 		isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
220038a2d0d4SChristoph Hellwig 		cqe = &isert_cmd->tx_desc.tx_cqe;
220138a2d0d4SChristoph Hellwig 	} else {
2202b8d26b3bSNicholas Bellinger 		/*
2203b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2204b8d26b3bSNicholas Bellinger 		 */
22059e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
22069e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
220704d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2208b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2209b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
221095b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
221168a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
22124366b19cSJenny Derzhavetz 
22134366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
22144366b19cSJenny Derzhavetz 		if (rc) {
22154366b19cSJenny Derzhavetz 			isert_err("ib_post_recv failed with %d\n", rc);
22164366b19cSJenny Derzhavetz 			return rc;
22174366b19cSJenny Derzhavetz 		}
221838a2d0d4SChristoph Hellwig 
221938a2d0d4SChristoph Hellwig 		chain_wr = &isert_cmd->tx_desc.send_wr;
22209e961ae7SSagi Grimberg 	}
2221b8d26b3bSNicholas Bellinger 
2222555a65f6SNicholas Bellinger 	rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2223555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n",
2224555a65f6SNicholas Bellinger 		  isert_cmd, rc);
2225555a65f6SNicholas Bellinger 	return rc;
2226b8d26b3bSNicholas Bellinger }
2227b8d26b3bSNicholas Bellinger 
2228b8d26b3bSNicholas Bellinger static int
2229b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2230b8d26b3bSNicholas Bellinger {
2231d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2232555a65f6SNicholas Bellinger 	int ret;
2233b8d26b3bSNicholas Bellinger 
223424f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
223538a2d0d4SChristoph Hellwig 		 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
2236b8d26b3bSNicholas Bellinger 
223738a2d0d4SChristoph Hellwig 	isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2238555a65f6SNicholas Bellinger 	ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context,
223938a2d0d4SChristoph Hellwig 				     &isert_cmd->tx_desc.tx_cqe, NULL);
2240bdf20e72SSagi Grimberg 
2241555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n",
2242555a65f6SNicholas Bellinger 		 isert_cmd, ret);
2243555a65f6SNicholas Bellinger 	return ret;
2244b8d26b3bSNicholas Bellinger }
2245b8d26b3bSNicholas Bellinger 
2246b8d26b3bSNicholas Bellinger static int
2247b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2248b8d26b3bSNicholas Bellinger {
2249a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2250a4c15cd9SJenny Derzhavetz 	int ret = 0;
2251b8d26b3bSNicholas Bellinger 
2252b8d26b3bSNicholas Bellinger 	switch (state) {
2253a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2254a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2255a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2256a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2257a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2258a4c15cd9SJenny Derzhavetz 		break;
2259b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2260b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2261b8d26b3bSNicholas Bellinger 		break;
2262b8d26b3bSNicholas Bellinger 	default:
226324f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2264b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2265b8d26b3bSNicholas Bellinger 		break;
2266b8d26b3bSNicholas Bellinger 	}
2267b8d26b3bSNicholas Bellinger 
2268b8d26b3bSNicholas Bellinger 	return ret;
2269b8d26b3bSNicholas Bellinger }
2270b8d26b3bSNicholas Bellinger 
2271b8d26b3bSNicholas Bellinger static int
2272b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2273b8d26b3bSNicholas Bellinger {
2274991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2275b8d26b3bSNicholas Bellinger 	int ret;
2276b8d26b3bSNicholas Bellinger 
2277b8d26b3bSNicholas Bellinger 	switch (state) {
2278b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2279b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2280991bb764SSagi Grimberg 		if (!ret)
2281991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2282b8d26b3bSNicholas Bellinger 		break;
2283b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2284b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2285b8d26b3bSNicholas Bellinger 		break;
2286b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2287b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2288b8d26b3bSNicholas Bellinger 		break;
2289b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2290b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2291b8d26b3bSNicholas Bellinger 		break;
2292adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2293adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2294adb54c29SNicholas Bellinger 		break;
2295b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2296b8d26b3bSNicholas Bellinger 		/*
2297b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2298b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2299b8d26b3bSNicholas Bellinger 		 */
2300b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2301b8d26b3bSNicholas Bellinger 		break;
2302b8d26b3bSNicholas Bellinger 	default:
230324f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2304b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2305b8d26b3bSNicholas Bellinger 		break;
2306b8d26b3bSNicholas Bellinger 	}
2307b8d26b3bSNicholas Bellinger 
2308b8d26b3bSNicholas Bellinger 	return ret;
2309b8d26b3bSNicholas Bellinger }
2310b8d26b3bSNicholas Bellinger 
2311ca6c1d82SSagi Grimberg struct rdma_cm_id *
2312ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2313ca6c1d82SSagi Grimberg {
2314ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2315ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2316ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2317ca6c1d82SSagi Grimberg 	int ret;
2318ca6c1d82SSagi Grimberg 
2319ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
232024f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2321ca6c1d82SSagi Grimberg 
2322fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
2323ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2324ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
232524f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2326ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2327ca6c1d82SSagi Grimberg 		goto out;
2328ca6c1d82SSagi Grimberg 	}
232924f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2330ca6c1d82SSagi Grimberg 
2331ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2332ca6c1d82SSagi Grimberg 	if (ret) {
233324f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2334ca6c1d82SSagi Grimberg 		goto out_id;
2335ca6c1d82SSagi Grimberg 	}
2336ca6c1d82SSagi Grimberg 
2337992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
2338ca6c1d82SSagi Grimberg 	if (ret) {
233924f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2340ca6c1d82SSagi Grimberg 		goto out_id;
2341ca6c1d82SSagi Grimberg 	}
2342ca6c1d82SSagi Grimberg 
2343ca6c1d82SSagi Grimberg 	return id;
2344ca6c1d82SSagi Grimberg out_id:
2345ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
2346ca6c1d82SSagi Grimberg out:
2347ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
2348ca6c1d82SSagi Grimberg }
2349ca6c1d82SSagi Grimberg 
2350b8d26b3bSNicholas Bellinger static int
2351b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
235213a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
2353b8d26b3bSNicholas Bellinger {
2354b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2355b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2356b8d26b3bSNicholas Bellinger 	int ret;
2357b8d26b3bSNicholas Bellinger 
2358b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
235993b80f29SLeon Romanovsky 	if (!isert_np)
2360b8d26b3bSNicholas Bellinger 		return -ENOMEM;
236193b80f29SLeon Romanovsky 
2362ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
2363ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
2364bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
2365bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
2366ca6c1d82SSagi Grimberg 	isert_np->np = np;
2367b8d26b3bSNicholas Bellinger 
2368b8d26b3bSNicholas Bellinger 	/*
2369b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2370b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2371b8d26b3bSNicholas Bellinger 	 */
2372b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
237313a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
2374b8d26b3bSNicholas Bellinger 
2375ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
2376b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2377b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2378b8d26b3bSNicholas Bellinger 		goto out;
2379b8d26b3bSNicholas Bellinger 	}
2380b8d26b3bSNicholas Bellinger 
2381ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
2382b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2383b8d26b3bSNicholas Bellinger 
2384b8d26b3bSNicholas Bellinger 	return 0;
2385b8d26b3bSNicholas Bellinger 
2386b8d26b3bSNicholas Bellinger out:
2387b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2388ca6c1d82SSagi Grimberg 
2389b8d26b3bSNicholas Bellinger 	return ret;
2390b8d26b3bSNicholas Bellinger }
2391b8d26b3bSNicholas Bellinger 
2392b8d26b3bSNicholas Bellinger static int
2393b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2394b8d26b3bSNicholas Bellinger {
2395dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2396b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2397b8d26b3bSNicholas Bellinger 	int ret;
239813bce482SJenny Derzhavetz 	struct iser_cm_hdr rsp_hdr;
2399b8d26b3bSNicholas Bellinger 
2400b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2401b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2402b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2403b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2404b8d26b3bSNicholas Bellinger 
240513bce482SJenny Derzhavetz 	memset(&rsp_hdr, 0, sizeof(rsp_hdr));
2406422bd0acSJenny Derzhavetz 	rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2407422bd0acSJenny Derzhavetz 	if (!isert_conn->snd_w_inv)
2408422bd0acSJenny Derzhavetz 		rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
240913bce482SJenny Derzhavetz 	cp.private_data = (void *)&rsp_hdr;
241013bce482SJenny Derzhavetz 	cp.private_data_len = sizeof(rsp_hdr);
241113bce482SJenny Derzhavetz 
2412b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2413b8d26b3bSNicholas Bellinger 	if (ret) {
241424f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
2415b8d26b3bSNicholas Bellinger 		return ret;
2416b8d26b3bSNicholas Bellinger 	}
2417b8d26b3bSNicholas Bellinger 
2418b8d26b3bSNicholas Bellinger 	return 0;
2419b8d26b3bSNicholas Bellinger }
2420b8d26b3bSNicholas Bellinger 
2421b8d26b3bSNicholas Bellinger static int
2422b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2423b8d26b3bSNicholas Bellinger {
24246700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2425b8d26b3bSNicholas Bellinger 	int ret;
2426b8d26b3bSNicholas Bellinger 
242724f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
24282371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
24292371e5daSSagi Grimberg 	if (ret) {
243024f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
24312371e5daSSagi Grimberg 			  isert_conn);
24322371e5daSSagi Grimberg 		return ret;
24332371e5daSSagi Grimberg 	}
24342371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
24352371e5daSSagi Grimberg 
24366faaa85fSNicholas Bellinger 	/*
24376faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
24386faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
24396faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
24406faaa85fSNicholas Bellinger 	 * into a NOP.
24416faaa85fSNicholas Bellinger 	 */
24426faaa85fSNicholas Bellinger 	if (!login->first_request)
24436faaa85fSNicholas Bellinger 		return 0;
2444b8d26b3bSNicholas Bellinger 
24452371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
24462371e5daSSagi Grimberg 
2447dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
2448dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
2449b8d26b3bSNicholas Bellinger 	if (ret)
2450b8d26b3bSNicholas Bellinger 		return ret;
2451b8d26b3bSNicholas Bellinger 
245224f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
24532371e5daSSagi Grimberg 
2454b8d26b3bSNicholas Bellinger 	return 0;
2455b8d26b3bSNicholas Bellinger }
2456b8d26b3bSNicholas Bellinger 
2457b8d26b3bSNicholas Bellinger static void
2458b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2459b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2460b8d26b3bSNicholas Bellinger {
2461dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2462b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2463b8d26b3bSNicholas Bellinger 
2464b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2465b8d26b3bSNicholas Bellinger 
2466dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
2467dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
2468b8d26b3bSNicholas Bellinger }
2469b8d26b3bSNicholas Bellinger 
2470b8d26b3bSNicholas Bellinger static int
2471b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2472b8d26b3bSNicholas Bellinger {
24736700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2474b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2475c6b8e918SSagi Grimberg 	int ret;
2476b8d26b3bSNicholas Bellinger 
2477b8d26b3bSNicholas Bellinger accept_wait:
2478ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
2479c6b8e918SSagi Grimberg 	if (ret)
2480b8d26b3bSNicholas Bellinger 		return -ENODEV;
2481b8d26b3bSNicholas Bellinger 
2482b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2483e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
2484b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
248511378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
2486e346ab34SSagi Grimberg 			 np->np_thread_state);
2487e346ab34SSagi Grimberg 		/**
2488e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
2489e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
2490e346ab34SSagi Grimberg 		 **/
2491b8d26b3bSNicholas Bellinger 		return -ENODEV;
2492b8d26b3bSNicholas Bellinger 	}
2493b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2494b8d26b3bSNicholas Bellinger 
2495ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2496bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
2497ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
2498b8d26b3bSNicholas Bellinger 		goto accept_wait;
2499b8d26b3bSNicholas Bellinger 	}
2500bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
2501bd379220SJenny Derzhavetz 			struct isert_conn, node);
2502bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
2503ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2504b8d26b3bSNicholas Bellinger 
2505b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2506b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2507aea92980SJenny Derzhavetz 	isert_conn->state = ISER_CONN_BOUND;
2508b8d26b3bSNicholas Bellinger 
2509b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2510b8d26b3bSNicholas Bellinger 
251124f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
25122371e5daSSagi Grimberg 
2513b8d26b3bSNicholas Bellinger 	return 0;
2514b8d26b3bSNicholas Bellinger }
2515b8d26b3bSNicholas Bellinger 
2516b8d26b3bSNicholas Bellinger static void
2517b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2518b8d26b3bSNicholas Bellinger {
25196700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2520268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
2521b8d26b3bSNicholas Bellinger 
2522ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
2523ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
2524b8d26b3bSNicholas Bellinger 
2525268e6811SSagi Grimberg 	/*
2526268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
2527268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
2528268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
2529268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
2530bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
2531268e6811SSagi Grimberg 	 */
2532ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2533bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
2534bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
2535268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
2536bd379220SJenny Derzhavetz 					 &isert_np->pending,
2537bd379220SJenny Derzhavetz 					 node) {
2538bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
2539bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
2540bd379220SJenny Derzhavetz 			isert_connect_release(isert_conn);
2541bd379220SJenny Derzhavetz 		}
2542bd379220SJenny Derzhavetz 	}
2543bd379220SJenny Derzhavetz 
2544bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
2545bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
2546bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
2547bd379220SJenny Derzhavetz 					 &isert_np->accepted,
2548bd379220SJenny Derzhavetz 					 node) {
254924f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
2550268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
2551268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
2552268e6811SSagi Grimberg 		}
2553268e6811SSagi Grimberg 	}
2554ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2555268e6811SSagi Grimberg 
2556b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2557b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2558b8d26b3bSNicholas Bellinger }
2559b8d26b3bSNicholas Bellinger 
2560b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
2561b02efbfcSSagi Grimberg {
2562b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
2563b02efbfcSSagi Grimberg 						     struct isert_conn,
2564b02efbfcSSagi Grimberg 						     release_work);
2565b02efbfcSSagi Grimberg 
256624f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
2567b02efbfcSSagi Grimberg 
2568dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2569b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
2570dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2571b02efbfcSSagi Grimberg 
257224f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
2573b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
2574b02efbfcSSagi Grimberg }
2575b02efbfcSSagi Grimberg 
2576bdf20e72SSagi Grimberg static void
2577991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
2578991bb764SSagi Grimberg {
2579991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
2580991bb764SSagi Grimberg 
25814c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
25824c22e07fSSagi Grimberg 
2583991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
258424f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
2585991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
2586991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
2587991bb764SSagi Grimberg 	}
2588991bb764SSagi Grimberg }
2589991bb764SSagi Grimberg 
2590991bb764SSagi Grimberg static void
2591c7e160eeSSagi Grimberg isert_wait4cmds(struct iscsi_conn *conn)
2592c7e160eeSSagi Grimberg {
25934c22e07fSSagi Grimberg 	isert_info("iscsi_conn %p\n", conn);
25944c22e07fSSagi Grimberg 
2595c7e160eeSSagi Grimberg 	if (conn->sess) {
2596c7e160eeSSagi Grimberg 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2597c7e160eeSSagi Grimberg 		target_wait_for_sess_cmds(conn->sess->se_sess);
2598c7e160eeSSagi Grimberg 	}
2599c7e160eeSSagi Grimberg }
2600c7e160eeSSagi Grimberg 
26013e03c4b0SJenny Derzhavetz /**
26023e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
26033e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
26043e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
26053e03c4b0SJenny Derzhavetz  *
26063e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
26073e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
26083e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
26093e03c4b0SJenny Derzhavetz  */
26103e03c4b0SJenny Derzhavetz static void
26113e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
26123e03c4b0SJenny Derzhavetz {
26133e03c4b0SJenny Derzhavetz 	struct iscsi_cmd *cmd, *tmp;
26143e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
26153e03c4b0SJenny Derzhavetz 
26163e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
26173e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
26183e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
26193e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
26203e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
26213e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
26223e03c4b0SJenny Derzhavetz 	}
26233e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
26243e03c4b0SJenny Derzhavetz 
26253e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
26263e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
26273e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
26283e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
26293e03c4b0SJenny Derzhavetz 
26303e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
26313e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
26323e03c4b0SJenny Derzhavetz 		}
26333e03c4b0SJenny Derzhavetz 	}
26343e03c4b0SJenny Derzhavetz }
26353e03c4b0SJenny Derzhavetz 
2636defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
2637b8d26b3bSNicholas Bellinger {
2638b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2639b8d26b3bSNicholas Bellinger 
26404c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
2641b8d26b3bSNicholas Bellinger 
2642dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2643954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2644dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2645b2cb9649SNicholas Bellinger 
2646572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
26473e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
26483e03c4b0SJenny Derzhavetz 	isert_wait4cmds(conn);
2649991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
2650954f2372SSagi Grimberg 
2651b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
2652defd8848SNicholas Bellinger }
2653defd8848SNicholas Bellinger 
2654defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2655defd8848SNicholas Bellinger {
2656defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2657b8d26b3bSNicholas Bellinger 
2658572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
2659b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2660b8d26b3bSNicholas Bellinger }
2661b8d26b3bSNicholas Bellinger 
2662187e84f7SVarun Prakash static void isert_get_rx_pdu(struct iscsi_conn *conn)
2663187e84f7SVarun Prakash {
2664187e84f7SVarun Prakash 	struct completion comp;
2665187e84f7SVarun Prakash 
2666187e84f7SVarun Prakash 	init_completion(&comp);
2667187e84f7SVarun Prakash 
2668187e84f7SVarun Prakash 	wait_for_completion_interruptible(&comp);
2669187e84f7SVarun Prakash }
2670187e84f7SVarun Prakash 
2671b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2672b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2673b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2674bd027d85SNicholas Bellinger 	.rdma_shutdown		= true,
2675d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2676b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2677b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2678b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2679b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2680defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2681b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2682b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2683b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2684b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2685b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2686b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2687b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2688b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2689131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
2690187e84f7SVarun Prakash 	.iscsit_get_rx_pdu	= isert_get_rx_pdu,
2691e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
2692b8d26b3bSNicholas Bellinger };
2693b8d26b3bSNicholas Bellinger 
2694b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2695b8d26b3bSNicholas Bellinger {
2696b8d26b3bSNicholas Bellinger 	int ret;
2697b8d26b3bSNicholas Bellinger 
2698631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
2699631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
2700b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
270124f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
27026f0fae3dSSagi Grimberg 		return -ENOMEM;
2703b8d26b3bSNicholas Bellinger 	}
2704b8d26b3bSNicholas Bellinger 
2705b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2706b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
2707b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
270824f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
2709b02efbfcSSagi Grimberg 		ret = -ENOMEM;
2710b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
2711b02efbfcSSagi Grimberg 	}
2712b02efbfcSSagi Grimberg 
2713b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
271424f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
2715b02efbfcSSagi Grimberg 
2716b8d26b3bSNicholas Bellinger 	return 0;
2717b8d26b3bSNicholas Bellinger 
2718b02efbfcSSagi Grimberg destroy_comp_wq:
2719b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
27206f0fae3dSSagi Grimberg 
2721b8d26b3bSNicholas Bellinger 	return ret;
2722b8d26b3bSNicholas Bellinger }
2723b8d26b3bSNicholas Bellinger 
2724b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2725b8d26b3bSNicholas Bellinger {
2726f5ebec96SSagi Grimberg 	flush_scheduled_work();
2727b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
2728b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2729b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
27304c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
2731b8d26b3bSNicholas Bellinger }
2732b8d26b3bSNicholas Bellinger 
2733b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2734b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2735b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2736b8d26b3bSNicholas Bellinger 
2737b8d26b3bSNicholas Bellinger module_init(isert_init);
2738b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2739