xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision d2271bd0834b8b2497ce39ca84820dd033b2deae)
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;
1394a061b28SOr Gerlitz 	attr.cap.max_send_sge = device->ib_device->attrs.max_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 
184dac6ab30SSagi Grimberg 	isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
185b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
186dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
18793b80f29SLeon Romanovsky 		return -ENOMEM;
188b8d26b3bSNicholas Bellinger 
189dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
190b8d26b3bSNicholas Bellinger 
191b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
192b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
193b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
194b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
195b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
196b8d26b3bSNicholas Bellinger 
197b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
198b8d26b3bSNicholas Bellinger 
199b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
200b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
201b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
20234efc7dfSJason Gunthorpe 		rx_sg->lkey = device->pd->local_dma_lkey;
2039679cc51SChristoph Hellwig 		rx_desc->rx_cqe.done = isert_recv_done;
204b8d26b3bSNicholas Bellinger 	}
205b8d26b3bSNicholas Bellinger 
206b8d26b3bSNicholas Bellinger 	return 0;
207b8d26b3bSNicholas Bellinger 
208b8d26b3bSNicholas Bellinger dma_map_fail:
209dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
210b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
211b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
212b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
213b8d26b3bSNicholas Bellinger 	}
214dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
215dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
2164c22e07fSSagi Grimberg 	isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
217b8d26b3bSNicholas Bellinger 	return -ENOMEM;
218b8d26b3bSNicholas Bellinger }
219b8d26b3bSNicholas Bellinger 
220b8d26b3bSNicholas Bellinger static void
221b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
222b8d26b3bSNicholas Bellinger {
223dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
224b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
225b8d26b3bSNicholas Bellinger 	int i;
226b8d26b3bSNicholas Bellinger 
227dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
228b8d26b3bSNicholas Bellinger 		return;
229b8d26b3bSNicholas Bellinger 
230dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
231b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
232b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
233b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
234b8d26b3bSNicholas Bellinger 	}
235b8d26b3bSNicholas Bellinger 
236dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
237dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
238b8d26b3bSNicholas Bellinger }
239b8d26b3bSNicholas Bellinger 
240172369c5SSagi Grimberg static void
241172369c5SSagi Grimberg isert_free_comps(struct isert_device *device)
242b8d26b3bSNicholas Bellinger {
243172369c5SSagi Grimberg 	int i;
244b8d26b3bSNicholas Bellinger 
245172369c5SSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
246172369c5SSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
24759464ef4SVu Pham 
2489679cc51SChristoph Hellwig 		if (comp->cq)
2499679cc51SChristoph Hellwig 			ib_free_cq(comp->cq);
250172369c5SSagi Grimberg 	}
251172369c5SSagi Grimberg 	kfree(device->comps);
25259464ef4SVu Pham }
253d40945d8SVu Pham 
254172369c5SSagi Grimberg static int
2554a061b28SOr Gerlitz isert_alloc_comps(struct isert_device *device)
256172369c5SSagi Grimberg {
257172369c5SSagi Grimberg 	int i, max_cqe, ret = 0;
258d3e125daSSagi Grimberg 
2594a295baeSSagi Grimberg 	device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
2604a295baeSSagi Grimberg 				 device->ib_device->num_comp_vectors));
261172369c5SSagi Grimberg 
26224f412ddSSagi Grimberg 	isert_info("Using %d CQs, %s supports %d vectors support "
26338a2d0d4SChristoph Hellwig 		   "pi_capable %d\n",
2644a295baeSSagi Grimberg 		   device->comps_used, device->ib_device->name,
26538a2d0d4SChristoph Hellwig 		   device->ib_device->num_comp_vectors,
266d3e125daSSagi Grimberg 		   device->pi_capable);
2674a295baeSSagi Grimberg 
2684a295baeSSagi Grimberg 	device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
2694a295baeSSagi Grimberg 				GFP_KERNEL);
27093b80f29SLeon Romanovsky 	if (!device->comps)
271b8d26b3bSNicholas Bellinger 		return -ENOMEM;
272b8d26b3bSNicholas Bellinger 
2734a061b28SOr Gerlitz 	max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
274172369c5SSagi Grimberg 
2754a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
2764a295baeSSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
277b8d26b3bSNicholas Bellinger 
2784a295baeSSagi Grimberg 		comp->device = device;
2799679cc51SChristoph Hellwig 		comp->cq = ib_alloc_cq(device->ib_device, comp, max_cqe, i,
2809679cc51SChristoph Hellwig 				IB_POLL_WORKQUEUE);
2816f0fae3dSSagi Grimberg 		if (IS_ERR(comp->cq)) {
282172369c5SSagi Grimberg 			isert_err("Unable to allocate cq\n");
2836f0fae3dSSagi Grimberg 			ret = PTR_ERR(comp->cq);
2846f0fae3dSSagi Grimberg 			comp->cq = NULL;
285b8d26b3bSNicholas Bellinger 			goto out_cq;
28694a71110SWei Yongjun 		}
287b8d26b3bSNicholas Bellinger 	}
288b8d26b3bSNicholas Bellinger 
289172369c5SSagi Grimberg 	return 0;
290172369c5SSagi Grimberg out_cq:
291172369c5SSagi Grimberg 	isert_free_comps(device);
292172369c5SSagi Grimberg 	return ret;
293172369c5SSagi Grimberg }
294172369c5SSagi Grimberg 
295172369c5SSagi Grimberg static int
296172369c5SSagi Grimberg isert_create_device_ib_res(struct isert_device *device)
297172369c5SSagi Grimberg {
2984a061b28SOr Gerlitz 	struct ib_device *ib_dev = device->ib_device;
299fd8205e8SSagi Grimberg 	int ret;
300172369c5SSagi Grimberg 
3014a061b28SOr Gerlitz 	isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
3024a061b28SOr Gerlitz 	isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
303172369c5SSagi Grimberg 
3044a061b28SOr Gerlitz 	ret = isert_alloc_comps(device);
305172369c5SSagi Grimberg 	if (ret)
306373a4cd7SNicholas Bellinger 		goto out;
307172369c5SSagi Grimberg 
308ed082d36SChristoph Hellwig 	device->pd = ib_alloc_pd(ib_dev, 0);
30967cb3949SSagi Grimberg 	if (IS_ERR(device->pd)) {
31067cb3949SSagi Grimberg 		ret = PTR_ERR(device->pd);
31167cb3949SSagi Grimberg 		isert_err("failed to allocate pd, device %p, ret=%d\n",
31267cb3949SSagi Grimberg 			  device, ret);
31367cb3949SSagi Grimberg 		goto out_cq;
31467cb3949SSagi Grimberg 	}
31567cb3949SSagi Grimberg 
316172369c5SSagi Grimberg 	/* Check signature cap */
3174a061b28SOr Gerlitz 	device->pi_capable = ib_dev->attrs.device_cap_flags &
318172369c5SSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
31967cb3949SSagi Grimberg 
320b8d26b3bSNicholas Bellinger 	return 0;
321b8d26b3bSNicholas Bellinger 
322b8d26b3bSNicholas Bellinger out_cq:
323172369c5SSagi Grimberg 	isert_free_comps(device);
324373a4cd7SNicholas Bellinger out:
325373a4cd7SNicholas Bellinger 	if (ret > 0)
326373a4cd7SNicholas Bellinger 		ret = -EINVAL;
327b8d26b3bSNicholas Bellinger 	return ret;
328b8d26b3bSNicholas Bellinger }
329b8d26b3bSNicholas Bellinger 
330b8d26b3bSNicholas Bellinger static void
331b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
332b8d26b3bSNicholas Bellinger {
33324f412ddSSagi Grimberg 	isert_info("device %p\n", device);
334b8d26b3bSNicholas Bellinger 
33567cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
336172369c5SSagi Grimberg 	isert_free_comps(device);
337b8d26b3bSNicholas Bellinger }
338b8d26b3bSNicholas Bellinger 
339b8d26b3bSNicholas Bellinger static void
340cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device)
341b8d26b3bSNicholas Bellinger {
342b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
343b8d26b3bSNicholas Bellinger 	device->refcount--;
3444c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
345b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
346b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
347b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
348b8d26b3bSNicholas Bellinger 		kfree(device);
349b8d26b3bSNicholas Bellinger 	}
350b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
351b8d26b3bSNicholas Bellinger }
352b8d26b3bSNicholas Bellinger 
353b8d26b3bSNicholas Bellinger static struct isert_device *
354cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id)
355b8d26b3bSNicholas Bellinger {
356b8d26b3bSNicholas Bellinger 	struct isert_device *device;
357b8d26b3bSNicholas Bellinger 	int ret;
358b8d26b3bSNicholas Bellinger 
359b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
360b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
361b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
362b8d26b3bSNicholas Bellinger 			device->refcount++;
3634c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
3644c22e07fSSagi Grimberg 				   device, device->refcount);
365b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
366b8d26b3bSNicholas Bellinger 			return device;
367b8d26b3bSNicholas Bellinger 		}
368b8d26b3bSNicholas Bellinger 	}
369b8d26b3bSNicholas Bellinger 
370b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
371b8d26b3bSNicholas Bellinger 	if (!device) {
372b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
373b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
374b8d26b3bSNicholas Bellinger 	}
375b8d26b3bSNicholas Bellinger 
376b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
377b8d26b3bSNicholas Bellinger 
378b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
379b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
380b8d26b3bSNicholas Bellinger 	if (ret) {
381b8d26b3bSNicholas Bellinger 		kfree(device);
382b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
383b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
384b8d26b3bSNicholas Bellinger 	}
385b8d26b3bSNicholas Bellinger 
386b8d26b3bSNicholas Bellinger 	device->refcount++;
387b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
3884c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
3894c22e07fSSagi Grimberg 		   device, device->refcount);
390b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
391b8d26b3bSNicholas Bellinger 
392b8d26b3bSNicholas Bellinger 	return device;
393b8d26b3bSNicholas Bellinger }
394b8d26b3bSNicholas Bellinger 
39559464ef4SVu Pham static void
396ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn)
397ae9ea9edSSagi Grimberg {
398ae9ea9edSSagi Grimberg 	isert_conn->state = ISER_CONN_INIT;
399bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_conn->node);
400dac6ab30SSagi Grimberg 	init_completion(&isert_conn->login_comp);
401ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
40263b268d2SRaju Rangoju 	init_waitqueue_head(&isert_conn->rem_wait);
403dac6ab30SSagi Grimberg 	kref_init(&isert_conn->kref);
404dac6ab30SSagi Grimberg 	mutex_init(&isert_conn->mutex);
4052f1b6b7dSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
406ae9ea9edSSagi Grimberg }
407ae9ea9edSSagi Grimberg 
408ae9ea9edSSagi Grimberg static void
409ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn)
410ae9ea9edSSagi Grimberg {
411dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
412ae9ea9edSSagi Grimberg 
413ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
414ed1083b2SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
4155adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
4165adabdd1SChristoph Hellwig 
417ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
4185adabdd1SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE,
419ae9ea9edSSagi Grimberg 			    DMA_FROM_DEVICE);
4205adabdd1SChristoph Hellwig 	kfree(isert_conn->login_req_buf);
421ae9ea9edSSagi Grimberg }
422ae9ea9edSSagi Grimberg 
423ae9ea9edSSagi Grimberg static int
424ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn,
425ae9ea9edSSagi Grimberg 		      struct ib_device *ib_dev)
426ae9ea9edSSagi Grimberg {
427ae9ea9edSSagi Grimberg 	int ret;
428ae9ea9edSSagi Grimberg 
4295adabdd1SChristoph Hellwig 	isert_conn->login_req_buf = kzalloc(sizeof(*isert_conn->login_req_buf),
4305adabdd1SChristoph Hellwig 			GFP_KERNEL);
43193b80f29SLeon Romanovsky 	if (!isert_conn->login_req_buf)
432ae9ea9edSSagi Grimberg 		return -ENOMEM;
433ae9ea9edSSagi Grimberg 
434ae9ea9edSSagi Grimberg 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
4355adabdd1SChristoph Hellwig 				isert_conn->login_req_buf,
4365adabdd1SChristoph Hellwig 				ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
437ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
438ae9ea9edSSagi Grimberg 	if (ret) {
439ae9ea9edSSagi Grimberg 		isert_err("login_req_dma mapping error: %d\n", ret);
440ae9ea9edSSagi Grimberg 		isert_conn->login_req_dma = 0;
4415adabdd1SChristoph Hellwig 		goto out_free_login_req_buf;
4425adabdd1SChristoph Hellwig 	}
4435adabdd1SChristoph Hellwig 
4445adabdd1SChristoph Hellwig 	isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
4455adabdd1SChristoph Hellwig 	if (!isert_conn->login_rsp_buf) {
4461d5840c9SWei Yongjun 		ret = -ENOMEM;
4475adabdd1SChristoph Hellwig 		goto out_unmap_login_req_buf;
448ae9ea9edSSagi Grimberg 	}
449ae9ea9edSSagi Grimberg 
450ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
4515adabdd1SChristoph Hellwig 					isert_conn->login_rsp_buf,
452ed1083b2SChristoph Hellwig 					ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
453ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
454ae9ea9edSSagi Grimberg 	if (ret) {
455ae9ea9edSSagi Grimberg 		isert_err("login_rsp_dma mapping error: %d\n", ret);
456ae9ea9edSSagi Grimberg 		isert_conn->login_rsp_dma = 0;
4575adabdd1SChristoph Hellwig 		goto out_free_login_rsp_buf;
458ae9ea9edSSagi Grimberg 	}
459ae9ea9edSSagi Grimberg 
460ae9ea9edSSagi Grimberg 	return 0;
461ae9ea9edSSagi Grimberg 
4625adabdd1SChristoph Hellwig out_free_login_rsp_buf:
4635adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
4645adabdd1SChristoph Hellwig out_unmap_login_req_buf:
465ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
4665adabdd1SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
4675adabdd1SChristoph Hellwig out_free_login_req_buf:
4685adabdd1SChristoph Hellwig 	kfree(isert_conn->login_req_buf);
469ae9ea9edSSagi Grimberg 	return ret;
470ae9ea9edSSagi Grimberg }
471ae9ea9edSSagi Grimberg 
472422bd0acSJenny Derzhavetz static void
473422bd0acSJenny Derzhavetz isert_set_nego_params(struct isert_conn *isert_conn,
474422bd0acSJenny Derzhavetz 		      struct rdma_conn_param *param)
475422bd0acSJenny Derzhavetz {
476422bd0acSJenny Derzhavetz 	struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
477422bd0acSJenny Derzhavetz 
478422bd0acSJenny Derzhavetz 	/* Set max inflight RDMA READ requests */
479422bd0acSJenny Derzhavetz 	isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
480422bd0acSJenny Derzhavetz 				attr->max_qp_init_rd_atom);
481422bd0acSJenny Derzhavetz 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
482422bd0acSJenny Derzhavetz 
483422bd0acSJenny Derzhavetz 	if (param->private_data) {
484422bd0acSJenny Derzhavetz 		u8 flags = *(u8 *)param->private_data;
485422bd0acSJenny Derzhavetz 
486422bd0acSJenny Derzhavetz 		/*
487422bd0acSJenny Derzhavetz 		 * use remote invalidation if the both initiator
488422bd0acSJenny Derzhavetz 		 * and the HCA support it
489422bd0acSJenny Derzhavetz 		 */
490422bd0acSJenny Derzhavetz 		isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
491422bd0acSJenny Derzhavetz 					  (attr->device_cap_flags &
492422bd0acSJenny Derzhavetz 					   IB_DEVICE_MEM_MGT_EXTENSIONS);
493422bd0acSJenny Derzhavetz 		if (isert_conn->snd_w_inv)
494422bd0acSJenny Derzhavetz 			isert_info("Using remote invalidation\n");
495422bd0acSJenny Derzhavetz 	}
496422bd0acSJenny Derzhavetz }
497422bd0acSJenny Derzhavetz 
498b8d26b3bSNicholas Bellinger static int
499b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
500b8d26b3bSNicholas Bellinger {
501ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
502ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
503b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
504b8d26b3bSNicholas Bellinger 	struct isert_device *device;
505b8d26b3bSNicholas Bellinger 	int ret = 0;
50614f4b54fSSagi Grimberg 
50714f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
50814f4b54fSSagi Grimberg 	if (!np->enabled) {
50914f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
51024f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
51114f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
51214f4b54fSSagi Grimberg 	}
51314f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
514b8d26b3bSNicholas Bellinger 
5154c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
516b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
517b8d26b3bSNicholas Bellinger 
518b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
519ae9ea9edSSagi Grimberg 	if (!isert_conn)
520b8d26b3bSNicholas Bellinger 		return -ENOMEM;
521b8d26b3bSNicholas Bellinger 
522ae9ea9edSSagi Grimberg 	isert_init_conn(isert_conn);
523dac6ab30SSagi Grimberg 	isert_conn->cm_id = cma_id;
524b8d26b3bSNicholas Bellinger 
525ae9ea9edSSagi Grimberg 	ret = isert_alloc_login_buf(isert_conn, cma_id->device);
526ae9ea9edSSagi Grimberg 	if (ret)
527b8d26b3bSNicholas Bellinger 		goto out;
528b8d26b3bSNicholas Bellinger 
529cf8ae958SSagi Grimberg 	device = isert_device_get(cma_id);
530b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
531b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
532b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
533b8d26b3bSNicholas Bellinger 	}
534dac6ab30SSagi Grimberg 	isert_conn->device = device;
535b8d26b3bSNicholas Bellinger 
536422bd0acSJenny Derzhavetz 	isert_set_nego_params(isert_conn, &event->param.conn);
5371a92e17eSSagi Grimberg 
538570db170SSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id);
539b8d26b3bSNicholas Bellinger 	if (ret)
540b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
541b8d26b3bSNicholas Bellinger 
5429679cc51SChristoph Hellwig 	ret = isert_login_post_recv(isert_conn);
5432371e5daSSagi Grimberg 	if (ret)
5442371e5daSSagi Grimberg 		goto out_conn_dev;
5452371e5daSSagi Grimberg 
5462371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
5472371e5daSSagi Grimberg 	if (ret)
5482371e5daSSagi Grimberg 		goto out_conn_dev;
5492371e5daSSagi Grimberg 
550ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
551bd379220SJenny Derzhavetz 	list_add_tail(&isert_conn->node, &isert_np->accepted);
552ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
553b8d26b3bSNicholas Bellinger 
554b8d26b3bSNicholas Bellinger 	return 0;
555b8d26b3bSNicholas Bellinger 
556b8d26b3bSNicholas Bellinger out_conn_dev:
557cf8ae958SSagi Grimberg 	isert_device_put(device);
558b8d26b3bSNicholas Bellinger out_rsp_dma_map:
559ae9ea9edSSagi Grimberg 	isert_free_login_buf(isert_conn);
560b8d26b3bSNicholas Bellinger out:
561b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
5622371e5daSSagi Grimberg 	rdma_reject(cma_id, NULL, 0);
563b8d26b3bSNicholas Bellinger 	return ret;
564b8d26b3bSNicholas Bellinger }
565b8d26b3bSNicholas Bellinger 
566b8d26b3bSNicholas Bellinger static void
567b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
568b8d26b3bSNicholas Bellinger {
569dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
570b8d26b3bSNicholas Bellinger 
5714c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
572b8d26b3bSNicholas Bellinger 
57357df81e3SSagi Grimberg 	BUG_ON(!device);
57457df81e3SSagi Grimberg 
57519e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
57663b268d2SRaju Rangoju 	if (isert_conn->cm_id &&
57763b268d2SRaju Rangoju 	    !isert_conn->dev_removed)
578dac6ab30SSagi Grimberg 		rdma_destroy_id(isert_conn->cm_id);
57919e2090fSSagi Grimberg 
580dac6ab30SSagi Grimberg 	if (isert_conn->qp) {
581dac6ab30SSagi Grimberg 		struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
5824a295baeSSagi Grimberg 
58340fc069aSSagi Grimberg 		isert_comp_put(comp);
584dac6ab30SSagi Grimberg 		ib_destroy_qp(isert_conn->qp);
585b8d26b3bSNicholas Bellinger 	}
586b8d26b3bSNicholas Bellinger 
5875adabdd1SChristoph Hellwig 	if (isert_conn->login_req_buf)
588ae9ea9edSSagi Grimberg 		isert_free_login_buf(isert_conn);
589ae9ea9edSSagi Grimberg 
590cf8ae958SSagi Grimberg 	isert_device_put(device);
59157df81e3SSagi Grimberg 
59263b268d2SRaju Rangoju 	if (isert_conn->dev_removed)
59363b268d2SRaju Rangoju 		wake_up_interruptible(&isert_conn->rem_wait);
59463b268d2SRaju Rangoju 	else
59557df81e3SSagi Grimberg 		kfree(isert_conn);
596b8d26b3bSNicholas Bellinger }
597b8d26b3bSNicholas Bellinger 
598b8d26b3bSNicholas Bellinger static void
599b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
600b8d26b3bSNicholas Bellinger {
60119e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
602bd379220SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
603c2f88b17SSagi Grimberg 
60424f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
605128e9cc8SSagi Grimberg 
606dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
607128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_UP;
608bd379220SJenny Derzhavetz 	kref_get(&isert_conn->kref);
609dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
610bd379220SJenny Derzhavetz 
611bd379220SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
612bd379220SJenny Derzhavetz 	list_move_tail(&isert_conn->node, &isert_np->pending);
613bd379220SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
614bd379220SJenny Derzhavetz 
615bd379220SJenny Derzhavetz 	isert_info("np %p: Allow accept_np to continue\n", isert_np);
616bd379220SJenny Derzhavetz 	up(&isert_np->sem);
617b8d26b3bSNicholas Bellinger }
618b8d26b3bSNicholas Bellinger 
619b8d26b3bSNicholas Bellinger static void
620dac6ab30SSagi Grimberg isert_release_kref(struct kref *kref)
621b8d26b3bSNicholas Bellinger {
622b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
623dac6ab30SSagi Grimberg 				struct isert_conn, kref);
624b8d26b3bSNicholas Bellinger 
6254c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
6264c22e07fSSagi Grimberg 		   current->pid);
627b8d26b3bSNicholas Bellinger 
628b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
629b8d26b3bSNicholas Bellinger }
630b8d26b3bSNicholas Bellinger 
631b8d26b3bSNicholas Bellinger static void
632b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
633b8d26b3bSNicholas Bellinger {
634dac6ab30SSagi Grimberg 	kref_put(&isert_conn->kref, isert_release_kref);
635b8d26b3bSNicholas Bellinger }
636b8d26b3bSNicholas Bellinger 
6376d1fba0cSJenny Derzhavetz static void
6386d1fba0cSJenny Derzhavetz isert_handle_unbound_conn(struct isert_conn *isert_conn)
6396d1fba0cSJenny Derzhavetz {
6406d1fba0cSJenny Derzhavetz 	struct isert_np *isert_np = isert_conn->cm_id->context;
6416d1fba0cSJenny Derzhavetz 
6426d1fba0cSJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
6436d1fba0cSJenny Derzhavetz 	if (!list_empty(&isert_conn->node)) {
6446d1fba0cSJenny Derzhavetz 		/*
6456d1fba0cSJenny Derzhavetz 		 * This means iscsi doesn't know this connection
6466d1fba0cSJenny Derzhavetz 		 * so schedule a cleanup ourselves
6476d1fba0cSJenny Derzhavetz 		 */
6486d1fba0cSJenny Derzhavetz 		list_del_init(&isert_conn->node);
6496d1fba0cSJenny Derzhavetz 		isert_put_conn(isert_conn);
6506d1fba0cSJenny Derzhavetz 		queue_work(isert_release_wq, &isert_conn->release_work);
6516d1fba0cSJenny Derzhavetz 	}
6526d1fba0cSJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
6536d1fba0cSJenny Derzhavetz }
6546d1fba0cSJenny Derzhavetz 
655954f2372SSagi Grimberg /**
656954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
657954f2372SSagi Grimberg  * @isert_conn: isert connection struct
658954f2372SSagi Grimberg  *
659954f2372SSagi Grimberg  * Notes:
660aea92980SJenny Derzhavetz  * In case the connection state is BOUND, move state
661954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
662128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
663954f2372SSagi Grimberg  *
664dac6ab30SSagi Grimberg  * This routine must be called with mutex held. Thus it is
665954f2372SSagi Grimberg  * safe to call multiple times.
666954f2372SSagi Grimberg  */
667954f2372SSagi Grimberg static void
668954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
669954f2372SSagi Grimberg {
670954f2372SSagi Grimberg 	int err;
671954f2372SSagi Grimberg 
6726d1fba0cSJenny Derzhavetz 	if (isert_conn->state >= ISER_CONN_TERMINATING)
6736d1fba0cSJenny Derzhavetz 		return;
6746d1fba0cSJenny Derzhavetz 
67524f412ddSSagi Grimberg 	isert_info("Terminating conn %p state %d\n",
676954f2372SSagi Grimberg 		   isert_conn, isert_conn->state);
677128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_TERMINATING;
678dac6ab30SSagi Grimberg 	err = rdma_disconnect(isert_conn->cm_id);
679954f2372SSagi Grimberg 	if (err)
68024f412ddSSagi Grimberg 		isert_warn("Failed rdma_disconnect isert_conn %p\n",
681954f2372SSagi Grimberg 			   isert_conn);
682954f2372SSagi Grimberg }
683954f2372SSagi Grimberg 
6843b726ae2SSagi Grimberg static int
685ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
686ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
687b8d26b3bSNicholas Bellinger {
688ea8a1616SSagi Grimberg 	isert_dbg("%s (%d): isert np %p\n",
689ea8a1616SSagi Grimberg 		  rdma_event_msg(event), event, isert_np);
6903b726ae2SSagi Grimberg 
691ca6c1d82SSagi Grimberg 	switch (event) {
692ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
693ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = NULL;
694ca6c1d82SSagi Grimberg 		break;
695ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
696ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = isert_setup_id(isert_np);
697ed8cb0a4SJenny Derzhavetz 		if (IS_ERR(isert_np->cm_id)) {
69824f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
699ed8cb0a4SJenny Derzhavetz 				  isert_np, PTR_ERR(isert_np->cm_id));
700ed8cb0a4SJenny Derzhavetz 			isert_np->cm_id = NULL;
701ca6c1d82SSagi Grimberg 		}
702ca6c1d82SSagi Grimberg 		break;
703ca6c1d82SSagi Grimberg 	default:
70424f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
705ca6c1d82SSagi Grimberg 			  isert_np, event);
706ca6c1d82SSagi Grimberg 	}
707ca6c1d82SSagi Grimberg 
7083b726ae2SSagi Grimberg 	return -1;
7093b726ae2SSagi Grimberg }
7103b726ae2SSagi Grimberg 
711ca6c1d82SSagi Grimberg static int
712ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
713ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
714ca6c1d82SSagi Grimberg {
715f81bf458SJenny Derzhavetz 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
716ca6c1d82SSagi Grimberg 
717dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
7186d1fba0cSJenny Derzhavetz 	switch (isert_conn->state) {
7196d1fba0cSJenny Derzhavetz 	case ISER_CONN_TERMINATING:
7206d1fba0cSJenny Derzhavetz 		break;
7216d1fba0cSJenny Derzhavetz 	case ISER_CONN_UP:
722128e9cc8SSagi Grimberg 		isert_conn_terminate(isert_conn);
723572a1434SSagi Grimberg 		ib_drain_qp(isert_conn->qp);
7246d1fba0cSJenny Derzhavetz 		isert_handle_unbound_conn(isert_conn);
7256d1fba0cSJenny Derzhavetz 		break;
7266d1fba0cSJenny Derzhavetz 	case ISER_CONN_BOUND:
7276d1fba0cSJenny Derzhavetz 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
7286d1fba0cSJenny Derzhavetz 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
7296d1fba0cSJenny Derzhavetz 		break;
7306d1fba0cSJenny Derzhavetz 	default:
731506f71d1SColin Ian King 		isert_warn("conn %p terminating in state %d\n",
7326d1fba0cSJenny Derzhavetz 			   isert_conn, isert_conn->state);
7336d1fba0cSJenny Derzhavetz 	}
734dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
735128e9cc8SSagi Grimberg 
7363b726ae2SSagi Grimberg 	return 0;
737b8d26b3bSNicholas Bellinger }
738b8d26b3bSNicholas Bellinger 
7394a579da2SSagi Grimberg static int
740954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
741954f2372SSagi Grimberg {
74219e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
743954f2372SSagi Grimberg 
744bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
745dac6ab30SSagi Grimberg 	isert_conn->cm_id = NULL;
746954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
7474a579da2SSagi Grimberg 
7484a579da2SSagi Grimberg 	return -1;
749954f2372SSagi Grimberg }
750954f2372SSagi Grimberg 
751b8d26b3bSNicholas Bellinger static int
752b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
753b8d26b3bSNicholas Bellinger {
754f81bf458SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
75563b268d2SRaju Rangoju 	struct isert_conn *isert_conn;
756b8d26b3bSNicholas Bellinger 	int ret = 0;
757b8d26b3bSNicholas Bellinger 
758ea8a1616SSagi Grimberg 	isert_info("%s (%d): status %d id %p np %p\n",
759ea8a1616SSagi Grimberg 		   rdma_event_msg(event->event), event->event,
7604c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
761b8d26b3bSNicholas Bellinger 
762f81bf458SJenny Derzhavetz 	if (isert_np->cm_id == cma_id)
763f81bf458SJenny Derzhavetz 		return isert_np_cma_handler(cma_id->context, event->event);
764f81bf458SJenny Derzhavetz 
765b8d26b3bSNicholas Bellinger 	switch (event->event) {
766b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
767b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
7683b726ae2SSagi Grimberg 		if (ret)
7694c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
770b8d26b3bSNicholas Bellinger 		break;
771b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
772b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
773b8d26b3bSNicholas Bellinger 		break;
77488c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
77588c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
77688c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
777ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
778b8d26b3bSNicholas Bellinger 		break;
77963b268d2SRaju Rangoju 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
78063b268d2SRaju Rangoju 		isert_conn = cma_id->qp->qp_context;
78163b268d2SRaju Rangoju 		isert_conn->dev_removed = true;
78263b268d2SRaju Rangoju 		isert_disconnected_handler(cma_id, event->event);
78363b268d2SRaju Rangoju 		wait_event_interruptible(isert_conn->rem_wait,
78463b268d2SRaju Rangoju 					 isert_conn->state == ISER_CONN_DOWN);
78563b268d2SRaju Rangoju 		kfree(isert_conn);
78663b268d2SRaju Rangoju 		/*
78763b268d2SRaju Rangoju 		 * return non-zero from the callback to destroy
78863b268d2SRaju Rangoju 		 * the rdma cm id
78963b268d2SRaju Rangoju 		 */
79063b268d2SRaju Rangoju 		return 1;
791*d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_REJECTED:
7921e38a366SSteve Wise 		isert_info("Connection rejected: %s\n",
7931e38a366SSteve Wise 			   rdma_reject_msg(cma_id, event->status));
794*d2271bd0SBart Van Assche 		/* fall through */
795*d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_UNREACHABLE:
796b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
7974a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
798954f2372SSagi Grimberg 		break;
799b8d26b3bSNicholas Bellinger 	default:
80024f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
801b8d26b3bSNicholas Bellinger 		break;
802b8d26b3bSNicholas Bellinger 	}
803b8d26b3bSNicholas Bellinger 
804b8d26b3bSNicholas Bellinger 	return ret;
805b8d26b3bSNicholas Bellinger }
806b8d26b3bSNicholas Bellinger 
807b8d26b3bSNicholas Bellinger static int
8084366b19cSJenny Derzhavetz isert_post_recvm(struct isert_conn *isert_conn, u32 count)
809b8d26b3bSNicholas Bellinger {
810b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
811b8d26b3bSNicholas Bellinger 	int i, ret;
812b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
813b8d26b3bSNicholas Bellinger 
814dac6ab30SSagi Grimberg 	for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
8154366b19cSJenny Derzhavetz 		rx_desc = &isert_conn->rx_descs[i];
8169679cc51SChristoph Hellwig 
8179679cc51SChristoph Hellwig 		rx_wr->wr_cqe = &rx_desc->rx_cqe;
818b8d26b3bSNicholas Bellinger 		rx_wr->sg_list = &rx_desc->rx_sg;
819b8d26b3bSNicholas Bellinger 		rx_wr->num_sge = 1;
820b8d26b3bSNicholas Bellinger 		rx_wr->next = rx_wr + 1;
8217a56dc88SSagi Grimberg 		rx_desc->in_use = false;
822b8d26b3bSNicholas Bellinger 	}
823b8d26b3bSNicholas Bellinger 	rx_wr--;
824b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
825b8d26b3bSNicholas Bellinger 
826dac6ab30SSagi Grimberg 	ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
827b8d26b3bSNicholas Bellinger 			   &rx_wr_failed);
8286d1fba0cSJenny Derzhavetz 	if (ret)
82924f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
8304366b19cSJenny Derzhavetz 
8314366b19cSJenny Derzhavetz 	return ret;
8324366b19cSJenny Derzhavetz }
8334366b19cSJenny Derzhavetz 
8344366b19cSJenny Derzhavetz static int
8354366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
8364366b19cSJenny Derzhavetz {
8374366b19cSJenny Derzhavetz 	struct ib_recv_wr *rx_wr_failed, rx_wr;
8384366b19cSJenny Derzhavetz 	int ret;
8394366b19cSJenny Derzhavetz 
8407a56dc88SSagi Grimberg 	if (!rx_desc->in_use) {
8417a56dc88SSagi Grimberg 		/*
8427a56dc88SSagi Grimberg 		 * if the descriptor is not in-use we already reposted it
8437a56dc88SSagi Grimberg 		 * for recv, so just silently return
8447a56dc88SSagi Grimberg 		 */
8457a56dc88SSagi Grimberg 		return 0;
8467a56dc88SSagi Grimberg 	}
8477a56dc88SSagi Grimberg 
8487a56dc88SSagi Grimberg 	rx_desc->in_use = false;
8499679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &rx_desc->rx_cqe;
8504366b19cSJenny Derzhavetz 	rx_wr.sg_list = &rx_desc->rx_sg;
8514366b19cSJenny Derzhavetz 	rx_wr.num_sge = 1;
8524366b19cSJenny Derzhavetz 	rx_wr.next = NULL;
8534366b19cSJenny Derzhavetz 
8544366b19cSJenny Derzhavetz 	ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
8556d1fba0cSJenny Derzhavetz 	if (ret)
8564366b19cSJenny Derzhavetz 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
8574366b19cSJenny Derzhavetz 
858b8d26b3bSNicholas Bellinger 	return ret;
859b8d26b3bSNicholas Bellinger }
860b8d26b3bSNicholas Bellinger 
861b8d26b3bSNicholas Bellinger static int
8629679cc51SChristoph Hellwig isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
863b8d26b3bSNicholas Bellinger {
864dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
865b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
866b8d26b3bSNicholas Bellinger 	int ret;
867b8d26b3bSNicholas Bellinger 
868b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
869b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
870b8d26b3bSNicholas Bellinger 
8719679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_login_send_done;
8729679cc51SChristoph Hellwig 
873b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
8749679cc51SChristoph Hellwig 	send_wr.wr_cqe	= &tx_desc->tx_cqe;
875b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
876b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
877b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
878b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
879b8d26b3bSNicholas Bellinger 
880dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
881bdf20e72SSagi Grimberg 	if (ret)
88224f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
883b8d26b3bSNicholas Bellinger 
884b8d26b3bSNicholas Bellinger 	return ret;
885b8d26b3bSNicholas Bellinger }
886b8d26b3bSNicholas Bellinger 
887b8d26b3bSNicholas Bellinger static void
888b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
889b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
890b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
891b8d26b3bSNicholas Bellinger {
892dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
89367cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
894b8d26b3bSNicholas Bellinger 
895b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
896b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
897b8d26b3bSNicholas Bellinger 
898d3cf81f9SSagi Grimberg 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
899d3cf81f9SSagi Grimberg 	tx_desc->iser_header.flags = ISCSI_CTRL;
900b8d26b3bSNicholas Bellinger 
901b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
902b8d26b3bSNicholas Bellinger 
90334efc7dfSJason Gunthorpe 	if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
90434efc7dfSJason Gunthorpe 		tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
90524f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
906b8d26b3bSNicholas Bellinger 	}
907b8d26b3bSNicholas Bellinger }
908b8d26b3bSNicholas Bellinger 
909b8d26b3bSNicholas Bellinger static int
910b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
911b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
912b8d26b3bSNicholas Bellinger {
913dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
91467cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
915b8d26b3bSNicholas Bellinger 	u64 dma_addr;
916b8d26b3bSNicholas Bellinger 
917b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
918b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
919b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
92024f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
921b8d26b3bSNicholas Bellinger 		return -ENOMEM;
922b8d26b3bSNicholas Bellinger 	}
923b8d26b3bSNicholas Bellinger 
924b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
925b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
926b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
92734efc7dfSJason Gunthorpe 	tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
928b8d26b3bSNicholas Bellinger 
9294c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
9304c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
9314c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
932b8d26b3bSNicholas Bellinger 
933b8d26b3bSNicholas Bellinger 	return 0;
934b8d26b3bSNicholas Bellinger }
935b8d26b3bSNicholas Bellinger 
936b8d26b3bSNicholas Bellinger static void
93795b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
93868a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
939b8d26b3bSNicholas Bellinger {
94095b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
94195b60f07SNicholas Bellinger 
9429679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_send_done;
9439679cc51SChristoph Hellwig 	send_wr->wr_cqe = &tx_desc->tx_cqe;
944422bd0acSJenny Derzhavetz 
945422bd0acSJenny Derzhavetz 	if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
946422bd0acSJenny Derzhavetz 		send_wr->opcode  = IB_WR_SEND_WITH_INV;
947422bd0acSJenny Derzhavetz 		send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
948422bd0acSJenny Derzhavetz 	} else {
949b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_SEND;
950422bd0acSJenny Derzhavetz 	}
951422bd0acSJenny Derzhavetz 
95295b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
953b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
95495b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
955b8d26b3bSNicholas Bellinger }
956b8d26b3bSNicholas Bellinger 
957b8d26b3bSNicholas Bellinger static int
9589679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn)
959b8d26b3bSNicholas Bellinger {
960b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
961b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
962b8d26b3bSNicholas Bellinger 	int ret;
963b8d26b3bSNicholas Bellinger 
964b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
965b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
966ed1083b2SChristoph Hellwig 	sge.length = ISER_RX_PAYLOAD_SIZE;
96734efc7dfSJason Gunthorpe 	sge.lkey = isert_conn->device->pd->local_dma_lkey;
968b8d26b3bSNicholas Bellinger 
96924f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
970b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
971b8d26b3bSNicholas Bellinger 
9729679cc51SChristoph Hellwig 	isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
9739679cc51SChristoph Hellwig 
974b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
9759679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
976b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
977b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
978b8d26b3bSNicholas Bellinger 
979dac6ab30SSagi Grimberg 	ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
9806d1fba0cSJenny Derzhavetz 	if (ret)
98124f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
982b8d26b3bSNicholas Bellinger 
983b8d26b3bSNicholas Bellinger 	return ret;
984b8d26b3bSNicholas Bellinger }
985b8d26b3bSNicholas Bellinger 
986b8d26b3bSNicholas Bellinger static int
987b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
988b8d26b3bSNicholas Bellinger 		   u32 length)
989b8d26b3bSNicholas Bellinger {
990b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
991dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
99267cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
993dac6ab30SSagi Grimberg 	struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
994b8d26b3bSNicholas Bellinger 	int ret;
995b8d26b3bSNicholas Bellinger 
996b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
997b8d26b3bSNicholas Bellinger 
998b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
999b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1000b8d26b3bSNicholas Bellinger 
1001b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1002b8d26b3bSNicholas Bellinger 
1003b8d26b3bSNicholas Bellinger 	if (length > 0) {
1004b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1005b8d26b3bSNicholas Bellinger 
1006b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1007b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1008b8d26b3bSNicholas Bellinger 
1009b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1010b8d26b3bSNicholas Bellinger 
1011b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1012b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1013b8d26b3bSNicholas Bellinger 
1014b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1015b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
101634efc7dfSJason Gunthorpe 		tx_dsg->lkey	= isert_conn->device->pd->local_dma_lkey;
1017b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1018b8d26b3bSNicholas Bellinger 	}
1019b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1020b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1021b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1022b8d26b3bSNicholas Bellinger 			if (ret)
1023b8d26b3bSNicholas Bellinger 				return ret;
1024b8d26b3bSNicholas Bellinger 
10254366b19cSJenny Derzhavetz 			ret = isert_post_recvm(isert_conn,
10264366b19cSJenny Derzhavetz 					       ISERT_QP_MAX_RECV_DTOS);
1027b8d26b3bSNicholas Bellinger 			if (ret)
1028b8d26b3bSNicholas Bellinger 				return ret;
1029b8d26b3bSNicholas Bellinger 
1030128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
1031dac6ab30SSagi Grimberg 			mutex_lock(&isert_conn->mutex);
1032128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
1033dac6ab30SSagi Grimberg 			mutex_unlock(&isert_conn->mutex);
1034b8d26b3bSNicholas Bellinger 			goto post_send;
1035b8d26b3bSNicholas Bellinger 		}
1036b8d26b3bSNicholas Bellinger 
10379679cc51SChristoph Hellwig 		ret = isert_login_post_recv(isert_conn);
1038b8d26b3bSNicholas Bellinger 		if (ret)
1039b8d26b3bSNicholas Bellinger 			return ret;
1040b8d26b3bSNicholas Bellinger 	}
1041b8d26b3bSNicholas Bellinger post_send:
10429679cc51SChristoph Hellwig 	ret = isert_login_post_send(isert_conn, tx_desc);
1043b8d26b3bSNicholas Bellinger 	if (ret)
1044b8d26b3bSNicholas Bellinger 		return ret;
1045b8d26b3bSNicholas Bellinger 
1046b8d26b3bSNicholas Bellinger 	return 0;
1047b8d26b3bSNicholas Bellinger }
1048b8d26b3bSNicholas Bellinger 
1049b8d26b3bSNicholas Bellinger static void
10502371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1051b8d26b3bSNicholas Bellinger {
10525adabdd1SChristoph Hellwig 	struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
10532371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1054b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1055b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1056b8d26b3bSNicholas Bellinger 	int size;
1057b8d26b3bSNicholas Bellinger 
105824f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
10592371e5daSSagi Grimberg 
10602371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1061b8d26b3bSNicholas Bellinger 
1062b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1063b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1064b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1065b8d26b3bSNicholas Bellinger 		/*
1066b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1067b8d26b3bSNicholas Bellinger 		 * login request PDU.
1068b8d26b3bSNicholas Bellinger 		 */
1069b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1070b8d26b3bSNicholas Bellinger 		login->current_stage =
1071b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1072b8d26b3bSNicholas Bellinger 			 >> 2;
1073b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1074b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1075b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1076b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1077b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1078b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1079b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1080b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1081b8d26b3bSNicholas Bellinger 	}
1082b8d26b3bSNicholas Bellinger 
1083b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1084b8d26b3bSNicholas Bellinger 
1085b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
10864c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
10874c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
10884c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
1089b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1090b8d26b3bSNicholas Bellinger 
10916faaa85fSNicholas Bellinger 	if (login->first_request) {
1092dac6ab30SSagi Grimberg 		complete(&isert_conn->login_comp);
10936faaa85fSNicholas Bellinger 		return;
10946faaa85fSNicholas Bellinger 	}
10956faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1096b8d26b3bSNicholas Bellinger }
1097b8d26b3bSNicholas Bellinger 
1098b8d26b3bSNicholas Bellinger static struct iscsi_cmd
10994366b19cSJenny Derzhavetz *isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
1100b8d26b3bSNicholas Bellinger {
11016700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1102b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1103d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1104b8d26b3bSNicholas Bellinger 
1105676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1106d703ce2fSNicholas Bellinger 	if (!cmd) {
110724f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1108b8d26b3bSNicholas Bellinger 		return NULL;
1109b8d26b3bSNicholas Bellinger 	}
1110d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1111b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1112d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
11134366b19cSJenny Derzhavetz 	isert_cmd->rx_desc = rx_desc;
1114b8d26b3bSNicholas Bellinger 
1115d703ce2fSNicholas Bellinger 	return cmd;
1116b8d26b3bSNicholas Bellinger }
1117b8d26b3bSNicholas Bellinger 
1118b8d26b3bSNicholas Bellinger static int
1119b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1120d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1121d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1122b8d26b3bSNicholas Bellinger {
1123b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1124b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1125b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1126b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
11279fd60088SJenny Derzhavetz 	unsigned int data_len;
1128b8d26b3bSNicholas Bellinger 
1129b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1130b8d26b3bSNicholas Bellinger 	if (rc < 0)
1131b8d26b3bSNicholas Bellinger 		return rc;
1132b8d26b3bSNicholas Bellinger 
1133b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1134b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1135b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
11369fd60088SJenny Derzhavetz 	data_len = cmd->se_cmd.data_length;
1137b8d26b3bSNicholas Bellinger 
11389fd60088SJenny Derzhavetz 	if (imm_data && imm_data_len == data_len)
11399fd60088SJenny Derzhavetz 		cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1140b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1141b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1142b8d26b3bSNicholas Bellinger 		return 0;
1143b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1144b8d26b3bSNicholas Bellinger 		dump_payload = true;
1145b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1146b8d26b3bSNicholas Bellinger 	}
1147b8d26b3bSNicholas Bellinger 
1148b8d26b3bSNicholas Bellinger 	if (!imm_data)
1149b8d26b3bSNicholas Bellinger 		return 0;
1150b8d26b3bSNicholas Bellinger 
11519fd60088SJenny Derzhavetz 	if (imm_data_len != data_len) {
1152b8d26b3bSNicholas Bellinger 		sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
11539fd60088SJenny Derzhavetz 		sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
11549fd60088SJenny Derzhavetz 				    &rx_desc->data[0], imm_data_len);
11559fd60088SJenny Derzhavetz 		isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
11569fd60088SJenny Derzhavetz 			  sg_nents, imm_data_len);
11579fd60088SJenny Derzhavetz 	} else {
11589fd60088SJenny Derzhavetz 		sg_init_table(&isert_cmd->sg, 1);
11599fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_sg = &isert_cmd->sg;
11609fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_nents = 1;
11619fd60088SJenny Derzhavetz 		sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
11629fd60088SJenny Derzhavetz 		isert_dbg("Transfer Immediate imm_data_len: %d\n",
11639fd60088SJenny Derzhavetz 			  imm_data_len);
11649fd60088SJenny Derzhavetz 	}
1165b8d26b3bSNicholas Bellinger 
1166b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1167b8d26b3bSNicholas Bellinger 
1168b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1169b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1170b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1171b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1172b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1173b8d26b3bSNicholas Bellinger 	}
1174b8d26b3bSNicholas Bellinger 
1175b8d26b3bSNicholas Bellinger sequence_cmd:
1176561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1177b8d26b3bSNicholas Bellinger 
1178b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1179b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
11806cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1181afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1182b8d26b3bSNicholas Bellinger 
1183b8d26b3bSNicholas Bellinger 	return 0;
1184b8d26b3bSNicholas Bellinger }
1185b8d26b3bSNicholas Bellinger 
1186b8d26b3bSNicholas Bellinger static int
1187b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1188b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1189b8d26b3bSNicholas Bellinger {
1190b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1191b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1192b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1193b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1194b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1195b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1196b8d26b3bSNicholas Bellinger 
1197b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1198b8d26b3bSNicholas Bellinger 	if (rc < 0)
1199b8d26b3bSNicholas Bellinger 		return rc;
1200b8d26b3bSNicholas Bellinger 	else if (!cmd)
1201b8d26b3bSNicholas Bellinger 		return 0;
1202b8d26b3bSNicholas Bellinger 	/*
1203b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1204b8d26b3bSNicholas Bellinger 	 */
1205b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
120624f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1207b8d26b3bSNicholas Bellinger 		dump_stack();
1208b8d26b3bSNicholas Bellinger 		return -1;
1209b8d26b3bSNicholas Bellinger 	}
1210b8d26b3bSNicholas Bellinger 
12114c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
12124c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
12134c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
12144c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1215b8d26b3bSNicholas Bellinger 
1216b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1217b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1218b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1219b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1220b8d26b3bSNicholas Bellinger 	/*
1221b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1222b8d26b3bSNicholas Bellinger 	 */
1223b8d26b3bSNicholas Bellinger 	if (page_off) {
12244c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1225b8d26b3bSNicholas Bellinger 		dump_stack();
1226b8d26b3bSNicholas Bellinger 		return -1;
1227b8d26b3bSNicholas Bellinger 	}
12284c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
12294c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
12304c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1231b8d26b3bSNicholas Bellinger 
1232b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1233b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1234b8d26b3bSNicholas Bellinger 
1235b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1236b8d26b3bSNicholas Bellinger 	if (rc < 0)
1237b8d26b3bSNicholas Bellinger 		return rc;
1238b8d26b3bSNicholas Bellinger 
12394366b19cSJenny Derzhavetz 	/*
12404366b19cSJenny Derzhavetz 	 * multiple data-outs on the same command can arrive -
12414366b19cSJenny Derzhavetz 	 * so post the buffer before hand
12424366b19cSJenny Derzhavetz 	 */
12434366b19cSJenny Derzhavetz 	rc = isert_post_recv(isert_conn, rx_desc);
12444366b19cSJenny Derzhavetz 	if (rc) {
12454366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", rc);
12464366b19cSJenny Derzhavetz 		return rc;
12474366b19cSJenny Derzhavetz 	}
1248b8d26b3bSNicholas Bellinger 	return 0;
1249b8d26b3bSNicholas Bellinger }
1250b8d26b3bSNicholas Bellinger 
1251b8d26b3bSNicholas Bellinger static int
1252778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1253d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1254d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1255778de368SNicholas Bellinger {
1256778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1257778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1258778de368SNicholas Bellinger 	int rc;
1259778de368SNicholas Bellinger 
1260778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1261778de368SNicholas Bellinger 	if (rc < 0)
1262778de368SNicholas Bellinger 		return rc;
1263778de368SNicholas Bellinger 	/*
1264778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1265778de368SNicholas Bellinger 	 */
1266778de368SNicholas Bellinger 
1267778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1268778de368SNicholas Bellinger }
1269778de368SNicholas Bellinger 
1270778de368SNicholas Bellinger static int
1271adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1272d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1273d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1274adb54c29SNicholas Bellinger {
1275adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1276adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1277adb54c29SNicholas Bellinger 	int rc;
1278b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1279adb54c29SNicholas Bellinger 
1280adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1281adb54c29SNicholas Bellinger 	if (rc < 0)
1282adb54c29SNicholas Bellinger 		return rc;
1283adb54c29SNicholas Bellinger 
1284b44a2b67SSagi Grimberg 	if (payload_length) {
1285adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
128693b80f29SLeon Romanovsky 		if (!text_in)
1287adb54c29SNicholas Bellinger 			return -ENOMEM;
1288adb54c29SNicholas Bellinger 	}
1289adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1290adb54c29SNicholas Bellinger 
1291adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1292adb54c29SNicholas Bellinger 
1293adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1294adb54c29SNicholas Bellinger }
1295adb54c29SNicholas Bellinger 
1296adb54c29SNicholas Bellinger static int
1297b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1298b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1299b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1300b8d26b3bSNicholas Bellinger {
1301b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1302b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1303b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1304b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1305b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1306b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1307b8d26b3bSNicholas Bellinger 
1308fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1309ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
131024f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1311ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1312ca40d24eSNicholas Bellinger 		return 0;
1313ca40d24eSNicholas Bellinger 	}
1314ca40d24eSNicholas Bellinger 
1315b8d26b3bSNicholas Bellinger 	switch (opcode) {
1316b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
13174366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1318b8d26b3bSNicholas Bellinger 		if (!cmd)
1319b8d26b3bSNicholas Bellinger 			break;
1320b8d26b3bSNicholas Bellinger 
1321d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1322b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1323b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1324b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1325b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1326422bd0acSJenny Derzhavetz 		isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
1327b8d26b3bSNicholas Bellinger 
1328d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1329b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1330b8d26b3bSNicholas Bellinger 		break;
1331b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
13324366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1333b8d26b3bSNicholas Bellinger 		if (!cmd)
1334b8d26b3bSNicholas Bellinger 			break;
1335b8d26b3bSNicholas Bellinger 
1336d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1337d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1338778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1339b8d26b3bSNicholas Bellinger 		break;
1340b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1341b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1342b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1343b8d26b3bSNicholas Bellinger 		break;
1344b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
13454366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1346b8d26b3bSNicholas Bellinger 		if (!cmd)
1347b8d26b3bSNicholas Bellinger 			break;
1348b8d26b3bSNicholas Bellinger 
1349b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1350b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1351b8d26b3bSNicholas Bellinger 		break;
1352b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
13534366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1354b8d26b3bSNicholas Bellinger 		if (!cmd)
1355b8d26b3bSNicholas Bellinger 			break;
1356b8d26b3bSNicholas Bellinger 
1357b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1358b8d26b3bSNicholas Bellinger 		break;
1359adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
13604366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1361e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
13624366b19cSJenny Derzhavetz 		else
13634366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
13644366b19cSJenny Derzhavetz 
1365e4f4e801SSagi Grimberg 		if (!cmd)
1366e4f4e801SSagi Grimberg 			break;
1367adb54c29SNicholas Bellinger 
1368d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1369d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1370adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1371adb54c29SNicholas Bellinger 		break;
1372b8d26b3bSNicholas Bellinger 	default:
137324f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1374b8d26b3bSNicholas Bellinger 		dump_stack();
1375b8d26b3bSNicholas Bellinger 		break;
1376b8d26b3bSNicholas Bellinger 	}
1377b8d26b3bSNicholas Bellinger 
1378b8d26b3bSNicholas Bellinger 	return ret;
1379b8d26b3bSNicholas Bellinger }
1380b8d26b3bSNicholas Bellinger 
1381b8d26b3bSNicholas Bellinger static void
13829679cc51SChristoph Hellwig isert_print_wc(struct ib_wc *wc, const char *type)
1383b8d26b3bSNicholas Bellinger {
13849679cc51SChristoph Hellwig 	if (wc->status != IB_WC_WR_FLUSH_ERR)
13859679cc51SChristoph Hellwig 		isert_err("%s failure: %s (%d) vend_err %x\n", type,
13869679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status,
13879679cc51SChristoph Hellwig 			  wc->vendor_err);
13889679cc51SChristoph Hellwig 	else
13899679cc51SChristoph Hellwig 		isert_dbg("%s failure: %s (%d)\n", type,
13909679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status);
13919679cc51SChristoph Hellwig }
13929679cc51SChristoph Hellwig 
13939679cc51SChristoph Hellwig static void
13949679cc51SChristoph Hellwig isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
13959679cc51SChristoph Hellwig {
13969679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
13979679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
13989679cc51SChristoph Hellwig 	struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
13999679cc51SChristoph Hellwig 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1400d3cf81f9SSagi Grimberg 	struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
1401b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1402b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1403b8d26b3bSNicholas Bellinger 
14049679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
14059679cc51SChristoph Hellwig 		isert_print_wc(wc, "recv");
14069679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
14079679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
14089679cc51SChristoph Hellwig 		return;
14099679cc51SChristoph Hellwig 	}
14109679cc51SChristoph Hellwig 
14117a56dc88SSagi Grimberg 	rx_desc->in_use = true;
14127a56dc88SSagi Grimberg 
14139679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
14149679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
14159679cc51SChristoph Hellwig 
14169679cc51SChristoph Hellwig 	isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
14179679cc51SChristoph Hellwig 		 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
14189679cc51SChristoph Hellwig 		 (int)(wc->byte_len - ISER_HEADERS_LEN));
14199679cc51SChristoph Hellwig 
1420d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1421b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1422d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1423d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1424d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
14254c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1426b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1427b8d26b3bSNicholas Bellinger 		}
1428d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1429d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1430d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
14314c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1432b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1433b8d26b3bSNicholas Bellinger 		}
1434b8d26b3bSNicholas Bellinger 
143524f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1436b8d26b3bSNicholas Bellinger 		break;
1437b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
143824f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1439b8d26b3bSNicholas Bellinger 		break;
1440b8d26b3bSNicholas Bellinger 	default:
1441d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1442b8d26b3bSNicholas Bellinger 		break;
1443b8d26b3bSNicholas Bellinger 	}
1444b8d26b3bSNicholas Bellinger 
14456c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1446b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
14479679cc51SChristoph Hellwig 
14489679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
14499679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1450b8d26b3bSNicholas Bellinger }
1451b8d26b3bSNicholas Bellinger 
1452b8d26b3bSNicholas Bellinger static void
14539679cc51SChristoph Hellwig isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1454b8d26b3bSNicholas Bellinger {
14559679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1456fce50a2fSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->device->ib_device;
1457b8d26b3bSNicholas Bellinger 
14589679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
14599679cc51SChristoph Hellwig 		isert_print_wc(wc, "login recv");
14609679cc51SChristoph Hellwig 		return;
1461b8d26b3bSNicholas Bellinger 	}
1462b8d26b3bSNicholas Bellinger 
14639679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
14649679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1465b8d26b3bSNicholas Bellinger 
14669679cc51SChristoph Hellwig 	isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
1467b8d26b3bSNicholas Bellinger 
14682371e5daSSagi Grimberg 	if (isert_conn->conn) {
14692371e5daSSagi Grimberg 		struct iscsi_login *login = isert_conn->conn->conn_login;
14702371e5daSSagi Grimberg 
14712371e5daSSagi Grimberg 		if (login && !login->first_request)
14722371e5daSSagi Grimberg 			isert_rx_login_req(isert_conn);
14732371e5daSSagi Grimberg 	}
14749679cc51SChristoph Hellwig 
1475dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
14762371e5daSSagi Grimberg 	complete(&isert_conn->login_req_comp);
1477dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
1478b8d26b3bSNicholas Bellinger 
14799679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
14809679cc51SChristoph Hellwig 				ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1481b8d26b3bSNicholas Bellinger }
1482b8d26b3bSNicholas Bellinger 
1483e3d7e4c3SSagi Grimberg static void
148438a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
1485e3d7e4c3SSagi Grimberg {
148638a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
148738a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
1488e3d7e4c3SSagi Grimberg 
148938a2d0d4SChristoph Hellwig 	if (!cmd->rw.nr_ops)
149038a2d0d4SChristoph Hellwig 		return;
149138a2d0d4SChristoph Hellwig 
149238a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
149338a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
149438a2d0d4SChristoph Hellwig 				conn->cm_id->port_num, se_cmd->t_data_sg,
149538a2d0d4SChristoph Hellwig 				se_cmd->t_data_nents, se_cmd->t_prot_sg,
149638a2d0d4SChristoph Hellwig 				se_cmd->t_prot_nents, dir);
149738a2d0d4SChristoph Hellwig 	} else {
149838a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
149938a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
1500e3d7e4c3SSagi Grimberg 	}
1501e3d7e4c3SSagi Grimberg 
150238a2d0d4SChristoph Hellwig 	cmd->rw.nr_ops = 0;
150359464ef4SVu Pham }
150459464ef4SVu Pham 
150559464ef4SVu Pham static void
150603e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1507b8d26b3bSNicholas Bellinger {
1508d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1509b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1510186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1511e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1512b8d26b3bSNicholas Bellinger 
15134c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1514b8d26b3bSNicholas Bellinger 
1515b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1516b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1517b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1518b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15195159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1520b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1521b8d26b3bSNicholas Bellinger 
152203e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1523b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
152403e7848aSNicholas Bellinger 			/*
152503e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
152603e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
152703e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
152803e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
152903e7848aSNicholas Bellinger 			 */
153003e7848aSNicholas Bellinger 			if (comp_err &&
153103e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
153203e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
153303e7848aSNicholas Bellinger 
1534afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
153503e7848aSNicholas Bellinger 			}
153603e7848aSNicholas Bellinger 		}
1537b8d26b3bSNicholas Bellinger 
153838a2d0d4SChristoph Hellwig 		isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1539186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1540186a9647SNicholas Bellinger 		break;
1541b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1542186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1543186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15445159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1545186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1546186a9647SNicholas Bellinger 
1547b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1548b8d26b3bSNicholas Bellinger 		break;
1549b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1550b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1551adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1552e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1553e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1554e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1555e4f4e801SSagi Grimberg 			break;
1556e4f4e801SSagi Grimberg 
1557b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1558b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15595159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1560b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1561b8d26b3bSNicholas Bellinger 
1562b8d26b3bSNicholas Bellinger 		/*
1563b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1564b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1565b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1566b8d26b3bSNicholas Bellinger 		 */
1567b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
156811378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
15693df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1570b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1571b8d26b3bSNicholas Bellinger 			break;
1572b8d26b3bSNicholas Bellinger 		}
1573*d2271bd0SBart Van Assche 		/* fall through */
1574b8d26b3bSNicholas Bellinger 	default:
1575d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1576b8d26b3bSNicholas Bellinger 		break;
1577b8d26b3bSNicholas Bellinger 	}
1578b8d26b3bSNicholas Bellinger }
1579b8d26b3bSNicholas Bellinger 
1580b8d26b3bSNicholas Bellinger static void
1581b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1582b8d26b3bSNicholas Bellinger {
1583b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
15844c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1585b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1586b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1587b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1588b8d26b3bSNicholas Bellinger 	}
1589b8d26b3bSNicholas Bellinger }
1590b8d26b3bSNicholas Bellinger 
1591b8d26b3bSNicholas Bellinger static void
1592b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
159303e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1594b8d26b3bSNicholas Bellinger {
1595dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
15964c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1597dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1598dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1599dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1600b8d26b3bSNicholas Bellinger 	}
1601b8d26b3bSNicholas Bellinger 
1602b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
160303e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1604b8d26b3bSNicholas Bellinger }
1605b8d26b3bSNicholas Bellinger 
160696b7973eSSagi Grimberg static int
160796b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1608f93f3a70SSagi Grimberg {
16099e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
161096b7973eSSagi Grimberg 	int ret;
1611f93f3a70SSagi Grimberg 
161296b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
16139e961ae7SSagi Grimberg 	if (ret) {
161424f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
16159e961ae7SSagi Grimberg 		goto fail_mr_status;
16169e961ae7SSagi Grimberg 	}
161796b7973eSSagi Grimberg 
16189e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
161996b7973eSSagi Grimberg 		u64 sec_offset_err;
16209e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
16219e961ae7SSagi Grimberg 
16229e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
16239e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
16249e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
16259e961ae7SSagi Grimberg 			break;
16269e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
16279e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
16289e961ae7SSagi Grimberg 			break;
16299e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
16309e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
16319e961ae7SSagi Grimberg 			break;
16329e961ae7SSagi Grimberg 		}
163396b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
163496b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
163596b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
16369e961ae7SSagi Grimberg 
16374c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
16389e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
16399e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
16409e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
16419e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
16429e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
16439e961ae7SSagi Grimberg 		ret = 1;
16449e961ae7SSagi Grimberg 	}
16459e961ae7SSagi Grimberg 
16469e961ae7SSagi Grimberg fail_mr_status:
164796b7973eSSagi Grimberg 	return ret;
164896b7973eSSagi Grimberg }
164996b7973eSSagi Grimberg 
165096b7973eSSagi Grimberg static void
16519679cc51SChristoph Hellwig isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
165296b7973eSSagi Grimberg {
16539679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1654dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
16559679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1656e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
16579679cc51SChristoph Hellwig 	struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
165896b7973eSSagi Grimberg 	int ret = 0;
165996b7973eSSagi Grimberg 
16609679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16619679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma write");
16629679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16639679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16649679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
16659679cc51SChristoph Hellwig 		return;
16669679cc51SChristoph Hellwig 	}
16679679cc51SChristoph Hellwig 
16689679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
16699679cc51SChristoph Hellwig 
167038a2d0d4SChristoph Hellwig 	ret = isert_check_pi_status(cmd, isert_cmd->rw.sig->sig_mr);
167138a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
167296b7973eSSagi Grimberg 
1673555a65f6SNicholas Bellinger 	if (ret) {
1674555a65f6SNicholas Bellinger 		/*
1675555a65f6SNicholas Bellinger 		 * transport_generic_request_failure() expects to have
1676555a65f6SNicholas Bellinger 		 * plus two references to handle queue-full, so re-add
1677555a65f6SNicholas Bellinger 		 * one here as target-core will have already dropped
1678555a65f6SNicholas Bellinger 		 * it after the first isert_put_datain() callback.
1679555a65f6SNicholas Bellinger 		 */
1680555a65f6SNicholas Bellinger 		kref_get(&cmd->cmd_kref);
1681555a65f6SNicholas Bellinger 		transport_generic_request_failure(cmd, cmd->pi_err);
1682555a65f6SNicholas Bellinger 	} else {
1683555a65f6SNicholas Bellinger 		/*
1684555a65f6SNicholas Bellinger 		 * XXX: isert_put_response() failure is not retried.
1685555a65f6SNicholas Bellinger 		 */
1686555a65f6SNicholas Bellinger 		ret = isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
16879e961ae7SSagi Grimberg 		if (ret)
1688555a65f6SNicholas Bellinger 			pr_warn_ratelimited("isert_put_response() ret: %d\n", ret);
1689555a65f6SNicholas Bellinger 	}
1690f93f3a70SSagi Grimberg }
1691f93f3a70SSagi Grimberg 
1692f93f3a70SSagi Grimberg static void
16939679cc51SChristoph Hellwig isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1694b8d26b3bSNicholas Bellinger {
16959679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16969679cc51SChristoph Hellwig 	struct isert_device *device = isert_conn->device;
16979679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1698e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
1699d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1700b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
17015bac4b1aSSagi Grimberg 	int ret = 0;
1702b8d26b3bSNicholas Bellinger 
17039679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17049679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma read");
17059679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17069679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17079679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
17089679cc51SChristoph Hellwig 		return;
17099679cc51SChristoph Hellwig 	}
17109679cc51SChristoph Hellwig 
17119679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
17129679cc51SChristoph Hellwig 
1713b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
171438a2d0d4SChristoph Hellwig 
171538a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd))
171638a2d0d4SChristoph Hellwig 		ret = isert_check_pi_status(se_cmd, isert_cmd->rw.sig->sig_mr);
171738a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
171838a2d0d4SChristoph Hellwig 	cmd->write_data_done = 0;
1719b8d26b3bSNicholas Bellinger 
172024f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1721b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1722b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1723b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1724b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1725b8d26b3bSNicholas Bellinger 
1726555a65f6SNicholas Bellinger 	/*
1727555a65f6SNicholas Bellinger 	 * transport_generic_request_failure() will drop the extra
1728555a65f6SNicholas Bellinger 	 * se_cmd->cmd_kref reference after T10-PI error, and handle
1729555a65f6SNicholas Bellinger 	 * any non-zero ->queue_status() callback error retries.
1730555a65f6SNicholas Bellinger 	 */
1731555a65f6SNicholas Bellinger 	if (ret)
1732555a65f6SNicholas Bellinger 		transport_generic_request_failure(se_cmd, se_cmd->pi_err);
1733555a65f6SNicholas Bellinger 	else
1734b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1735b8d26b3bSNicholas Bellinger }
1736b8d26b3bSNicholas Bellinger 
1737b8d26b3bSNicholas Bellinger static void
1738b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1739b8d26b3bSNicholas Bellinger {
1740b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1741b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1742b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1743dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1744d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1745b8d26b3bSNicholas Bellinger 
17464c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
17474c22e07fSSagi Grimberg 
1748b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1749b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1750b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1751*d2271bd0SBart Van Assche 		/* fall through */
1752*d2271bd0SBart Van Assche 	case ISTATE_SEND_REJECT:
1753*d2271bd0SBart Van Assche 	case ISTATE_SEND_TEXTRSP:
1754b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
17554c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
17564c22e07fSSagi Grimberg 				     ib_dev, false);
17573df8f68aSNicholas Bellinger 		break;
1758b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1759b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1760b8d26b3bSNicholas Bellinger 		break;
1761b8d26b3bSNicholas Bellinger 	default:
17624c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1763b8d26b3bSNicholas Bellinger 		dump_stack();
1764b8d26b3bSNicholas Bellinger 		break;
1765b8d26b3bSNicholas Bellinger 	}
1766b8d26b3bSNicholas Bellinger }
1767b8d26b3bSNicholas Bellinger 
1768b8d26b3bSNicholas Bellinger static void
17699679cc51SChristoph Hellwig isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
1770b8d26b3bSNicholas Bellinger {
17719679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17729679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
17739679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1774b8d26b3bSNicholas Bellinger 
17759679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17769679cc51SChristoph Hellwig 		isert_print_wc(wc, "login send");
17779679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17789679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17799679cc51SChristoph Hellwig 	}
17809679cc51SChristoph Hellwig 
17819679cc51SChristoph Hellwig 	isert_unmap_tx_desc(tx_desc, ib_dev);
17829679cc51SChristoph Hellwig }
17839679cc51SChristoph Hellwig 
17849679cc51SChristoph Hellwig static void
17859679cc51SChristoph Hellwig isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
17869679cc51SChristoph Hellwig {
17879679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17889679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
17899679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1790e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
17919679cc51SChristoph Hellwig 
17929679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17939679cc51SChristoph Hellwig 		isert_print_wc(wc, "send");
17949679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17959679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17969679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
17979679cc51SChristoph Hellwig 		return;
17989679cc51SChristoph Hellwig 	}
17999679cc51SChristoph Hellwig 
18009679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
18019679cc51SChristoph Hellwig 
18029679cc51SChristoph Hellwig 	switch (isert_cmd->iscsi_cmd->i_state) {
18039679cc51SChristoph Hellwig 	case ISTATE_SEND_TASKMGTRSP:
18049679cc51SChristoph Hellwig 	case ISTATE_SEND_LOGOUTRSP:
18059679cc51SChristoph Hellwig 	case ISTATE_SEND_REJECT:
18069679cc51SChristoph Hellwig 	case ISTATE_SEND_TEXTRSP:
1807b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1808b8d26b3bSNicholas Bellinger 
1809b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1810b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1811b8d26b3bSNicholas Bellinger 		return;
1812b8d26b3bSNicholas Bellinger 	default:
18139679cc51SChristoph Hellwig 		isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
18149679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1815b8d26b3bSNicholas Bellinger 		break;
1816b8d26b3bSNicholas Bellinger 	}
1817b8d26b3bSNicholas Bellinger }
1818b8d26b3bSNicholas Bellinger 
1819b8d26b3bSNicholas Bellinger static int
1820b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1821b8d26b3bSNicholas Bellinger {
1822b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
1823b8d26b3bSNicholas Bellinger 	int ret;
1824b8d26b3bSNicholas Bellinger 
18254366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
18264366b19cSJenny Derzhavetz 	if (ret) {
18274366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", ret);
18284366b19cSJenny Derzhavetz 		return ret;
18294366b19cSJenny Derzhavetz 	}
18304366b19cSJenny Derzhavetz 
1831dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
1832b8d26b3bSNicholas Bellinger 			   &wr_failed);
1833b8d26b3bSNicholas Bellinger 	if (ret) {
183424f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
1835b8d26b3bSNicholas Bellinger 		return ret;
1836b8d26b3bSNicholas Bellinger 	}
1837b8d26b3bSNicholas Bellinger 	return ret;
1838b8d26b3bSNicholas Bellinger }
1839b8d26b3bSNicholas Bellinger 
1840b8d26b3bSNicholas Bellinger static int
1841b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1842b8d26b3bSNicholas Bellinger {
1843d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18446700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1845b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1846b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1847b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1848b8d26b3bSNicholas Bellinger 
1849b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1850b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1851b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1852b8d26b3bSNicholas Bellinger 	/*
1853b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1854b8d26b3bSNicholas Bellinger 	 */
1855b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1856b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1857b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1858dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
185967cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1860b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1861dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1862b8d26b3bSNicholas Bellinger 
1863b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1864b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1865b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1866b8d26b3bSNicholas Bellinger 
1867b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1868b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1869dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1870b8d26b3bSNicholas Bellinger 
1871dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1872dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1873b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1874def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1875def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1876b8d26b3bSNicholas Bellinger 
1877dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1878dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1879dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
188034efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1881b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1882b8d26b3bSNicholas Bellinger 	}
1883b8d26b3bSNicholas Bellinger 
188468a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1885b8d26b3bSNicholas Bellinger 
18864c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
1887b8d26b3bSNicholas Bellinger 
1888b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1889b8d26b3bSNicholas Bellinger }
1890b8d26b3bSNicholas Bellinger 
1891131e6abcSNicholas Bellinger static void
1892131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1893131e6abcSNicholas Bellinger {
1894131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18956700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1896131e6abcSNicholas Bellinger 
1897131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
1898131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
1899131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
1900131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1901131e6abcSNicholas Bellinger 
1902131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
1903131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
190438a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1905131e6abcSNicholas Bellinger }
1906131e6abcSNicholas Bellinger 
1907e70beee7SNicholas Bellinger static enum target_prot_op
1908e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
1909e70beee7SNicholas Bellinger {
19106700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1911dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1912e70beee7SNicholas Bellinger 
191323a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
191423a548eeSSagi Grimberg 		if (device->pi_capable) {
191524f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
191623a548eeSSagi Grimberg 			isert_conn->pi_support = true;
1917e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
191823a548eeSSagi Grimberg 		}
191923a548eeSSagi Grimberg 	}
192023a548eeSSagi Grimberg 
192124f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
192223a548eeSSagi Grimberg 	isert_conn->pi_support = false;
1923e70beee7SNicholas Bellinger 
1924e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
1925e70beee7SNicholas Bellinger }
1926e70beee7SNicholas Bellinger 
1927b8d26b3bSNicholas Bellinger static int
1928b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1929b8d26b3bSNicholas Bellinger 		bool nopout_response)
1930b8d26b3bSNicholas Bellinger {
1931d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19326700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1933b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1934b8d26b3bSNicholas Bellinger 
1935b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1936b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1937b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1938b8d26b3bSNicholas Bellinger 			       nopout_response);
1939b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
194068a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1941b8d26b3bSNicholas Bellinger 
19424c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
1943b8d26b3bSNicholas Bellinger 
1944b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1945b8d26b3bSNicholas Bellinger }
1946b8d26b3bSNicholas Bellinger 
1947b8d26b3bSNicholas Bellinger static int
1948b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1949b8d26b3bSNicholas Bellinger {
1950d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19516700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1952b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1953b8d26b3bSNicholas Bellinger 
1954b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1955b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1956b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1957b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
195868a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1959b8d26b3bSNicholas Bellinger 
19604c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
1961b8d26b3bSNicholas Bellinger 
1962b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1963b8d26b3bSNicholas Bellinger }
1964b8d26b3bSNicholas Bellinger 
1965b8d26b3bSNicholas Bellinger static int
1966b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1967b8d26b3bSNicholas Bellinger {
1968d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19696700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1970b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1971b8d26b3bSNicholas Bellinger 
1972b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1973b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1974b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1975b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
197668a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1977b8d26b3bSNicholas Bellinger 
19784c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
1979b8d26b3bSNicholas Bellinger 
1980b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1981b8d26b3bSNicholas Bellinger }
1982b8d26b3bSNicholas Bellinger 
1983b8d26b3bSNicholas Bellinger static int
1984b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1985b8d26b3bSNicholas Bellinger {
1986d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19876700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1988b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1989dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
199067cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
19913df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
19923df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
19933df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1994b8d26b3bSNicholas Bellinger 
1995b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
19963df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
1997b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
19983df8f68aSNicholas Bellinger 
19993df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2000dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
20013df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
20023df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2003def4a6ffSAlexey Khoroshilov 	if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
2004def4a6ffSAlexey Khoroshilov 		return -ENOMEM;
2005dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2006dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
20073df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
200834efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
20093df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
20103df8f68aSNicholas Bellinger 
201168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2012b8d26b3bSNicholas Bellinger 
20134c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
2014b8d26b3bSNicholas Bellinger 
2015b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2016b8d26b3bSNicholas Bellinger }
2017b8d26b3bSNicholas Bellinger 
2018b8d26b3bSNicholas Bellinger static int
2019adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2020adb54c29SNicholas Bellinger {
2021d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
20226700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2023adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2024adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2025adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2026adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2027adb54c29SNicholas Bellinger 	int rc;
2028adb54c29SNicholas Bellinger 
2029adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
203022c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2031adb54c29SNicholas Bellinger 	if (rc < 0)
2032adb54c29SNicholas Bellinger 		return rc;
2033adb54c29SNicholas Bellinger 
2034adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2035adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2036adb54c29SNicholas Bellinger 
2037adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2038dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
203967cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2040adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2041adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2042adb54c29SNicholas Bellinger 
2043adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2044adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2045def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
2046def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
2047adb54c29SNicholas Bellinger 
2048adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2049adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2050adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
205134efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
2052adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2053adb54c29SNicholas Bellinger 	}
205468a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2055adb54c29SNicholas Bellinger 
2056f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
2057adb54c29SNicholas Bellinger 
2058adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2059adb54c29SNicholas Bellinger }
2060adb54c29SNicholas Bellinger 
20613d73cf1aSSagi Grimberg static inline void
20623d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
20633d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
20649e961ae7SSagi Grimberg {
206578eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
20663d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
20673d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
20683d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
206978eda2bbSSagi Grimberg 	/*
207078eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
207178eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
207278eda2bbSSagi Grimberg 	 * from se_cmd.
207378eda2bbSSagi Grimberg 	 */
207478eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
207578eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
207678eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
207778eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
207878eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
207978eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
20803d73cf1aSSagi Grimberg };
20813d73cf1aSSagi Grimberg 
20823d73cf1aSSagi Grimberg static int
20833d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
20843d73cf1aSSagi Grimberg {
208538a2d0d4SChristoph Hellwig 	memset(sig_attrs, 0, sizeof(*sig_attrs));
208638a2d0d4SChristoph Hellwig 
20879e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
20889e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
20899e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
209078eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
20913d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
20929e961ae7SSagi Grimberg 		break;
20939e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
20949e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
209578eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
20963d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
20979e961ae7SSagi Grimberg 		break;
20989e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
20999e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
21003d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
21013d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
21029e961ae7SSagi Grimberg 		break;
21039e961ae7SSagi Grimberg 	default:
210424f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
21059e961ae7SSagi Grimberg 		return -EINVAL;
21069e961ae7SSagi Grimberg 	}
21079e961ae7SSagi Grimberg 
210838a2d0d4SChristoph Hellwig 	sig_attrs->check_mask =
210938a2d0d4SChristoph Hellwig 	       (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
211038a2d0d4SChristoph Hellwig 	       (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
211138a2d0d4SChristoph Hellwig 	       (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
21129e961ae7SSagi Grimberg 	return 0;
21139e961ae7SSagi Grimberg }
21149e961ae7SSagi Grimberg 
21159e961ae7SSagi Grimberg static int
211638a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
211738a2d0d4SChristoph Hellwig 		struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
21189e961ae7SSagi Grimberg {
211938a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
212038a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
212138a2d0d4SChristoph Hellwig 	u8 port_num = conn->cm_id->port_num;
212238a2d0d4SChristoph Hellwig 	u64 addr;
212338a2d0d4SChristoph Hellwig 	u32 rkey, offset;
21249e961ae7SSagi Grimberg 	int ret;
21259e961ae7SSagi Grimberg 
212638a2d0d4SChristoph Hellwig 	if (dir == DMA_FROM_DEVICE) {
212738a2d0d4SChristoph Hellwig 		addr = cmd->write_va;
212838a2d0d4SChristoph Hellwig 		rkey = cmd->write_stag;
212938a2d0d4SChristoph Hellwig 		offset = cmd->iscsi_cmd->write_data_done;
213038a2d0d4SChristoph Hellwig 	} else {
213138a2d0d4SChristoph Hellwig 		addr = cmd->read_va;
213238a2d0d4SChristoph Hellwig 		rkey = cmd->read_stag;
213338a2d0d4SChristoph Hellwig 		offset = 0;
213438a2d0d4SChristoph Hellwig 	}
213538a2d0d4SChristoph Hellwig 
213638a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
213738a2d0d4SChristoph Hellwig 		struct ib_sig_attrs sig_attrs;
213838a2d0d4SChristoph Hellwig 
21399e961ae7SSagi Grimberg 		ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
21409e961ae7SSagi Grimberg 		if (ret)
2141e3d7e4c3SSagi Grimberg 			return ret;
2142e3d7e4c3SSagi Grimberg 
214338a2d0d4SChristoph Hellwig 		WARN_ON_ONCE(offset);
214438a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
214538a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
214638a2d0d4SChristoph Hellwig 				se_cmd->t_prot_sg, se_cmd->t_prot_nents,
214738a2d0d4SChristoph Hellwig 				&sig_attrs, addr, rkey, dir);
2148570db170SSagi Grimberg 	} else {
214938a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
215038a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
215138a2d0d4SChristoph Hellwig 				offset, addr, rkey, dir);
215238a2d0d4SChristoph Hellwig 	}
215338a2d0d4SChristoph Hellwig 	if (ret < 0) {
215438a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
215538a2d0d4SChristoph Hellwig 		return ret;
21569e961ae7SSagi Grimberg 	}
21579e961ae7SSagi Grimberg 
215838a2d0d4SChristoph Hellwig 	ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
215938a2d0d4SChristoph Hellwig 	if (ret < 0)
216038a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to post RDMA res\n", cmd);
216159464ef4SVu Pham 	return ret;
216259464ef4SVu Pham }
216359464ef4SVu Pham 
216459464ef4SVu Pham static int
216590ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
216690ecc6e2SVu Pham {
216790ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
216859464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
21696700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
217038a2d0d4SChristoph Hellwig 	struct ib_cqe *cqe = NULL;
217138a2d0d4SChristoph Hellwig 	struct ib_send_wr *chain_wr = NULL;
217290ecc6e2SVu Pham 	int rc;
217390ecc6e2SVu Pham 
217424f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
217590ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
21764c22e07fSSagi Grimberg 
217738a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd)) {
217838a2d0d4SChristoph Hellwig 		isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
217938a2d0d4SChristoph Hellwig 		cqe = &isert_cmd->tx_desc.tx_cqe;
218038a2d0d4SChristoph Hellwig 	} else {
2181b8d26b3bSNicholas Bellinger 		/*
2182b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2183b8d26b3bSNicholas Bellinger 		 */
21849e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
21859e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
218604d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2187b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2188b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
218995b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
219068a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
21914366b19cSJenny Derzhavetz 
21924366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
21934366b19cSJenny Derzhavetz 		if (rc) {
21944366b19cSJenny Derzhavetz 			isert_err("ib_post_recv failed with %d\n", rc);
21954366b19cSJenny Derzhavetz 			return rc;
21964366b19cSJenny Derzhavetz 		}
219738a2d0d4SChristoph Hellwig 
219838a2d0d4SChristoph Hellwig 		chain_wr = &isert_cmd->tx_desc.send_wr;
21999e961ae7SSagi Grimberg 	}
2200b8d26b3bSNicholas Bellinger 
2201555a65f6SNicholas Bellinger 	rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2202555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n",
2203555a65f6SNicholas Bellinger 		  isert_cmd, rc);
2204555a65f6SNicholas Bellinger 	return rc;
2205b8d26b3bSNicholas Bellinger }
2206b8d26b3bSNicholas Bellinger 
2207b8d26b3bSNicholas Bellinger static int
2208b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2209b8d26b3bSNicholas Bellinger {
2210d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2211555a65f6SNicholas Bellinger 	int ret;
2212b8d26b3bSNicholas Bellinger 
221324f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
221438a2d0d4SChristoph Hellwig 		 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
2215b8d26b3bSNicholas Bellinger 
221638a2d0d4SChristoph Hellwig 	isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2217555a65f6SNicholas Bellinger 	ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context,
221838a2d0d4SChristoph Hellwig 				     &isert_cmd->tx_desc.tx_cqe, NULL);
2219bdf20e72SSagi Grimberg 
2220555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n",
2221555a65f6SNicholas Bellinger 		 isert_cmd, ret);
2222555a65f6SNicholas Bellinger 	return ret;
2223b8d26b3bSNicholas Bellinger }
2224b8d26b3bSNicholas Bellinger 
2225b8d26b3bSNicholas Bellinger static int
2226b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2227b8d26b3bSNicholas Bellinger {
2228a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2229a4c15cd9SJenny Derzhavetz 	int ret = 0;
2230b8d26b3bSNicholas Bellinger 
2231b8d26b3bSNicholas Bellinger 	switch (state) {
2232a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2233a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2234a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2235a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2236a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2237a4c15cd9SJenny Derzhavetz 		break;
2238b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2239b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2240b8d26b3bSNicholas Bellinger 		break;
2241b8d26b3bSNicholas Bellinger 	default:
224224f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2243b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2244b8d26b3bSNicholas Bellinger 		break;
2245b8d26b3bSNicholas Bellinger 	}
2246b8d26b3bSNicholas Bellinger 
2247b8d26b3bSNicholas Bellinger 	return ret;
2248b8d26b3bSNicholas Bellinger }
2249b8d26b3bSNicholas Bellinger 
2250b8d26b3bSNicholas Bellinger static int
2251b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2252b8d26b3bSNicholas Bellinger {
2253991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2254b8d26b3bSNicholas Bellinger 	int ret;
2255b8d26b3bSNicholas Bellinger 
2256b8d26b3bSNicholas Bellinger 	switch (state) {
2257b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2258b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2259991bb764SSagi Grimberg 		if (!ret)
2260991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2261b8d26b3bSNicholas Bellinger 		break;
2262b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2263b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2264b8d26b3bSNicholas Bellinger 		break;
2265b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2266b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2267b8d26b3bSNicholas Bellinger 		break;
2268b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2269b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2270b8d26b3bSNicholas Bellinger 		break;
2271adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2272adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2273adb54c29SNicholas Bellinger 		break;
2274b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2275b8d26b3bSNicholas Bellinger 		/*
2276b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2277b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2278b8d26b3bSNicholas Bellinger 		 */
2279b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2280b8d26b3bSNicholas Bellinger 		break;
2281b8d26b3bSNicholas Bellinger 	default:
228224f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2283b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2284b8d26b3bSNicholas Bellinger 		break;
2285b8d26b3bSNicholas Bellinger 	}
2286b8d26b3bSNicholas Bellinger 
2287b8d26b3bSNicholas Bellinger 	return ret;
2288b8d26b3bSNicholas Bellinger }
2289b8d26b3bSNicholas Bellinger 
2290ca6c1d82SSagi Grimberg struct rdma_cm_id *
2291ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2292ca6c1d82SSagi Grimberg {
2293ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2294ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2295ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2296ca6c1d82SSagi Grimberg 	int ret;
2297ca6c1d82SSagi Grimberg 
2298ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
229924f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2300ca6c1d82SSagi Grimberg 
2301fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
2302ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2303ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
230424f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2305ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2306ca6c1d82SSagi Grimberg 		goto out;
2307ca6c1d82SSagi Grimberg 	}
230824f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2309ca6c1d82SSagi Grimberg 
2310ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2311ca6c1d82SSagi Grimberg 	if (ret) {
231224f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2313ca6c1d82SSagi Grimberg 		goto out_id;
2314ca6c1d82SSagi Grimberg 	}
2315ca6c1d82SSagi Grimberg 
2316992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
2317ca6c1d82SSagi Grimberg 	if (ret) {
231824f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2319ca6c1d82SSagi Grimberg 		goto out_id;
2320ca6c1d82SSagi Grimberg 	}
2321ca6c1d82SSagi Grimberg 
2322ca6c1d82SSagi Grimberg 	return id;
2323ca6c1d82SSagi Grimberg out_id:
2324ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
2325ca6c1d82SSagi Grimberg out:
2326ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
2327ca6c1d82SSagi Grimberg }
2328ca6c1d82SSagi Grimberg 
2329b8d26b3bSNicholas Bellinger static int
2330b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
233113a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
2332b8d26b3bSNicholas Bellinger {
2333b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2334b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2335b8d26b3bSNicholas Bellinger 	int ret;
2336b8d26b3bSNicholas Bellinger 
2337b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
233893b80f29SLeon Romanovsky 	if (!isert_np)
2339b8d26b3bSNicholas Bellinger 		return -ENOMEM;
234093b80f29SLeon Romanovsky 
2341ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
2342ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
2343bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
2344bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
2345ca6c1d82SSagi Grimberg 	isert_np->np = np;
2346b8d26b3bSNicholas Bellinger 
2347b8d26b3bSNicholas Bellinger 	/*
2348b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2349b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2350b8d26b3bSNicholas Bellinger 	 */
2351b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
235213a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
2353b8d26b3bSNicholas Bellinger 
2354ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
2355b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2356b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2357b8d26b3bSNicholas Bellinger 		goto out;
2358b8d26b3bSNicholas Bellinger 	}
2359b8d26b3bSNicholas Bellinger 
2360ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
2361b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2362b8d26b3bSNicholas Bellinger 
2363b8d26b3bSNicholas Bellinger 	return 0;
2364b8d26b3bSNicholas Bellinger 
2365b8d26b3bSNicholas Bellinger out:
2366b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2367ca6c1d82SSagi Grimberg 
2368b8d26b3bSNicholas Bellinger 	return ret;
2369b8d26b3bSNicholas Bellinger }
2370b8d26b3bSNicholas Bellinger 
2371b8d26b3bSNicholas Bellinger static int
2372b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2373b8d26b3bSNicholas Bellinger {
2374dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2375b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2376b8d26b3bSNicholas Bellinger 	int ret;
237713bce482SJenny Derzhavetz 	struct iser_cm_hdr rsp_hdr;
2378b8d26b3bSNicholas Bellinger 
2379b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2380b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2381b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2382b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2383b8d26b3bSNicholas Bellinger 
238413bce482SJenny Derzhavetz 	memset(&rsp_hdr, 0, sizeof(rsp_hdr));
2385422bd0acSJenny Derzhavetz 	rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2386422bd0acSJenny Derzhavetz 	if (!isert_conn->snd_w_inv)
2387422bd0acSJenny Derzhavetz 		rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
238813bce482SJenny Derzhavetz 	cp.private_data = (void *)&rsp_hdr;
238913bce482SJenny Derzhavetz 	cp.private_data_len = sizeof(rsp_hdr);
239013bce482SJenny Derzhavetz 
2391b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2392b8d26b3bSNicholas Bellinger 	if (ret) {
239324f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
2394b8d26b3bSNicholas Bellinger 		return ret;
2395b8d26b3bSNicholas Bellinger 	}
2396b8d26b3bSNicholas Bellinger 
2397b8d26b3bSNicholas Bellinger 	return 0;
2398b8d26b3bSNicholas Bellinger }
2399b8d26b3bSNicholas Bellinger 
2400b8d26b3bSNicholas Bellinger static int
2401b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2402b8d26b3bSNicholas Bellinger {
24036700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2404b8d26b3bSNicholas Bellinger 	int ret;
2405b8d26b3bSNicholas Bellinger 
240624f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
24072371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
24082371e5daSSagi Grimberg 	if (ret) {
240924f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
24102371e5daSSagi Grimberg 			  isert_conn);
24112371e5daSSagi Grimberg 		return ret;
24122371e5daSSagi Grimberg 	}
24132371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
24142371e5daSSagi Grimberg 
24156faaa85fSNicholas Bellinger 	/*
24166faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
24176faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
24186faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
24196faaa85fSNicholas Bellinger 	 * into a NOP.
24206faaa85fSNicholas Bellinger 	 */
24216faaa85fSNicholas Bellinger 	if (!login->first_request)
24226faaa85fSNicholas Bellinger 		return 0;
2423b8d26b3bSNicholas Bellinger 
24242371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
24252371e5daSSagi Grimberg 
2426dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
2427dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
2428b8d26b3bSNicholas Bellinger 	if (ret)
2429b8d26b3bSNicholas Bellinger 		return ret;
2430b8d26b3bSNicholas Bellinger 
243124f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
24322371e5daSSagi Grimberg 
2433b8d26b3bSNicholas Bellinger 	return 0;
2434b8d26b3bSNicholas Bellinger }
2435b8d26b3bSNicholas Bellinger 
2436b8d26b3bSNicholas Bellinger static void
2437b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2438b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2439b8d26b3bSNicholas Bellinger {
2440dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2441b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2442b8d26b3bSNicholas Bellinger 
2443b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2444b8d26b3bSNicholas Bellinger 
2445dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
2446dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
2447b8d26b3bSNicholas Bellinger }
2448b8d26b3bSNicholas Bellinger 
2449b8d26b3bSNicholas Bellinger static int
2450b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2451b8d26b3bSNicholas Bellinger {
24526700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2453b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2454c6b8e918SSagi Grimberg 	int ret;
2455b8d26b3bSNicholas Bellinger 
2456b8d26b3bSNicholas Bellinger accept_wait:
2457ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
2458c6b8e918SSagi Grimberg 	if (ret)
2459b8d26b3bSNicholas Bellinger 		return -ENODEV;
2460b8d26b3bSNicholas Bellinger 
2461b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2462e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
2463b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
246411378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
2465e346ab34SSagi Grimberg 			 np->np_thread_state);
2466e346ab34SSagi Grimberg 		/**
2467e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
2468e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
2469e346ab34SSagi Grimberg 		 **/
2470b8d26b3bSNicholas Bellinger 		return -ENODEV;
2471b8d26b3bSNicholas Bellinger 	}
2472b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2473b8d26b3bSNicholas Bellinger 
2474ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2475bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
2476ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
2477b8d26b3bSNicholas Bellinger 		goto accept_wait;
2478b8d26b3bSNicholas Bellinger 	}
2479bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
2480bd379220SJenny Derzhavetz 			struct isert_conn, node);
2481bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
2482ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2483b8d26b3bSNicholas Bellinger 
2484b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2485b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2486aea92980SJenny Derzhavetz 	isert_conn->state = ISER_CONN_BOUND;
2487b8d26b3bSNicholas Bellinger 
2488b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2489b8d26b3bSNicholas Bellinger 
249024f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
24912371e5daSSagi Grimberg 
2492b8d26b3bSNicholas Bellinger 	return 0;
2493b8d26b3bSNicholas Bellinger }
2494b8d26b3bSNicholas Bellinger 
2495b8d26b3bSNicholas Bellinger static void
2496b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2497b8d26b3bSNicholas Bellinger {
24986700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2499268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
2500b8d26b3bSNicholas Bellinger 
2501ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
2502ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
2503b8d26b3bSNicholas Bellinger 
2504268e6811SSagi Grimberg 	/*
2505268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
2506268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
2507268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
2508268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
2509bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
2510268e6811SSagi Grimberg 	 */
2511ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2512bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
2513bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
2514268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
2515bd379220SJenny Derzhavetz 					 &isert_np->pending,
2516bd379220SJenny Derzhavetz 					 node) {
2517bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
2518bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
2519bd379220SJenny Derzhavetz 			isert_connect_release(isert_conn);
2520bd379220SJenny Derzhavetz 		}
2521bd379220SJenny Derzhavetz 	}
2522bd379220SJenny Derzhavetz 
2523bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
2524bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
2525bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
2526bd379220SJenny Derzhavetz 					 &isert_np->accepted,
2527bd379220SJenny Derzhavetz 					 node) {
252824f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
2529268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
2530268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
2531268e6811SSagi Grimberg 		}
2532268e6811SSagi Grimberg 	}
2533ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2534268e6811SSagi Grimberg 
2535b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2536b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2537b8d26b3bSNicholas Bellinger }
2538b8d26b3bSNicholas Bellinger 
2539b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
2540b02efbfcSSagi Grimberg {
2541b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
2542b02efbfcSSagi Grimberg 						     struct isert_conn,
2543b02efbfcSSagi Grimberg 						     release_work);
2544b02efbfcSSagi Grimberg 
254524f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
2546b02efbfcSSagi Grimberg 
2547dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2548b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
2549dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2550b02efbfcSSagi Grimberg 
255124f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
2552b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
2553b02efbfcSSagi Grimberg }
2554b02efbfcSSagi Grimberg 
2555bdf20e72SSagi Grimberg static void
2556991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
2557991bb764SSagi Grimberg {
2558991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
2559991bb764SSagi Grimberg 
25604c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
25614c22e07fSSagi Grimberg 
2562991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
256324f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
2564991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
2565991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
2566991bb764SSagi Grimberg 	}
2567991bb764SSagi Grimberg }
2568991bb764SSagi Grimberg 
2569991bb764SSagi Grimberg static void
2570c7e160eeSSagi Grimberg isert_wait4cmds(struct iscsi_conn *conn)
2571c7e160eeSSagi Grimberg {
25724c22e07fSSagi Grimberg 	isert_info("iscsi_conn %p\n", conn);
25734c22e07fSSagi Grimberg 
2574c7e160eeSSagi Grimberg 	if (conn->sess) {
2575c7e160eeSSagi Grimberg 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2576c7e160eeSSagi Grimberg 		target_wait_for_sess_cmds(conn->sess->se_sess);
2577c7e160eeSSagi Grimberg 	}
2578c7e160eeSSagi Grimberg }
2579c7e160eeSSagi Grimberg 
25803e03c4b0SJenny Derzhavetz /**
25813e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
25823e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
25833e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
25843e03c4b0SJenny Derzhavetz  *
25853e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
25863e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
25873e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
25883e03c4b0SJenny Derzhavetz  */
25893e03c4b0SJenny Derzhavetz static void
25903e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
25913e03c4b0SJenny Derzhavetz {
25923e03c4b0SJenny Derzhavetz 	struct iscsi_cmd *cmd, *tmp;
25933e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
25943e03c4b0SJenny Derzhavetz 
25953e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
25963e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
25973e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
25983e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
25993e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
26003e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
26013e03c4b0SJenny Derzhavetz 	}
26023e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
26033e03c4b0SJenny Derzhavetz 
26043e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
26053e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
26063e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
26073e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
26083e03c4b0SJenny Derzhavetz 
26093e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
26103e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
26113e03c4b0SJenny Derzhavetz 		}
26123e03c4b0SJenny Derzhavetz 	}
26133e03c4b0SJenny Derzhavetz }
26143e03c4b0SJenny Derzhavetz 
2615defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
2616b8d26b3bSNicholas Bellinger {
2617b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2618b8d26b3bSNicholas Bellinger 
26194c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
2620b8d26b3bSNicholas Bellinger 
2621dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2622954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2623dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2624b2cb9649SNicholas Bellinger 
2625572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
26263e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
26273e03c4b0SJenny Derzhavetz 	isert_wait4cmds(conn);
2628991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
2629954f2372SSagi Grimberg 
2630b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
2631defd8848SNicholas Bellinger }
2632defd8848SNicholas Bellinger 
2633defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2634defd8848SNicholas Bellinger {
2635defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2636b8d26b3bSNicholas Bellinger 
2637572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
2638b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2639b8d26b3bSNicholas Bellinger }
2640b8d26b3bSNicholas Bellinger 
2641187e84f7SVarun Prakash static void isert_get_rx_pdu(struct iscsi_conn *conn)
2642187e84f7SVarun Prakash {
2643187e84f7SVarun Prakash 	struct completion comp;
2644187e84f7SVarun Prakash 
2645187e84f7SVarun Prakash 	init_completion(&comp);
2646187e84f7SVarun Prakash 
2647187e84f7SVarun Prakash 	wait_for_completion_interruptible(&comp);
2648187e84f7SVarun Prakash }
2649187e84f7SVarun Prakash 
2650b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2651b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2652b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2653bd027d85SNicholas Bellinger 	.rdma_shutdown		= true,
2654d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2655b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2656b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2657b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2658b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2659defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2660b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2661b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2662b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2663b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2664b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2665b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2666b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2667b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2668131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
2669187e84f7SVarun Prakash 	.iscsit_get_rx_pdu	= isert_get_rx_pdu,
2670e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
2671b8d26b3bSNicholas Bellinger };
2672b8d26b3bSNicholas Bellinger 
2673b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2674b8d26b3bSNicholas Bellinger {
2675b8d26b3bSNicholas Bellinger 	int ret;
2676b8d26b3bSNicholas Bellinger 
2677631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
2678631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
2679b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
268024f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
26816f0fae3dSSagi Grimberg 		return -ENOMEM;
2682b8d26b3bSNicholas Bellinger 	}
2683b8d26b3bSNicholas Bellinger 
2684b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2685b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
2686b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
268724f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
2688b02efbfcSSagi Grimberg 		ret = -ENOMEM;
2689b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
2690b02efbfcSSagi Grimberg 	}
2691b02efbfcSSagi Grimberg 
2692b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
269324f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
2694b02efbfcSSagi Grimberg 
2695b8d26b3bSNicholas Bellinger 	return 0;
2696b8d26b3bSNicholas Bellinger 
2697b02efbfcSSagi Grimberg destroy_comp_wq:
2698b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
26996f0fae3dSSagi Grimberg 
2700b8d26b3bSNicholas Bellinger 	return ret;
2701b8d26b3bSNicholas Bellinger }
2702b8d26b3bSNicholas Bellinger 
2703b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2704b8d26b3bSNicholas Bellinger {
2705f5ebec96SSagi Grimberg 	flush_scheduled_work();
2706b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
2707b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2708b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
27094c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
2710b8d26b3bSNicholas Bellinger }
2711b8d26b3bSNicholas Bellinger 
2712b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2713b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2714b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2715b8d26b3bSNicholas Bellinger 
2716b8d26b3bSNicholas Bellinger module_init(isert_init);
2717b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2718