xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision df561f6688fef775baa341a0f5d960becd248b11)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b8d26b3bSNicholas Bellinger /*******************************************************************************
3b8d26b3bSNicholas Bellinger  * This file contains iSCSI extentions for RDMA (iSER) Verbs
4b8d26b3bSNicholas Bellinger  *
54c76251eSNicholas Bellinger  * (c) Copyright 2013 Datera, Inc.
6b8d26b3bSNicholas Bellinger  *
7b8d26b3bSNicholas Bellinger  * Nicholas A. Bellinger <nab@linux-iscsi.org>
8b8d26b3bSNicholas Bellinger  *
9b8d26b3bSNicholas Bellinger  ****************************************************************************/
10b8d26b3bSNicholas Bellinger 
11b8d26b3bSNicholas Bellinger #include <linux/string.h>
12b8d26b3bSNicholas Bellinger #include <linux/module.h>
13b8d26b3bSNicholas Bellinger #include <linux/scatterlist.h>
14b8d26b3bSNicholas Bellinger #include <linux/socket.h>
15b8d26b3bSNicholas Bellinger #include <linux/in.h>
16b8d26b3bSNicholas Bellinger #include <linux/in6.h>
17b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h>
188094ba0aSLeon Romanovsky #include <rdma/ib_cm.h>
19b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h>
20b8d26b3bSNicholas Bellinger #include <target/target_core_base.h>
21b8d26b3bSNicholas Bellinger #include <target/target_core_fabric.h>
22b8d26b3bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
23531b7bf4SSagi Grimberg #include <linux/semaphore.h>
24b8d26b3bSNicholas Bellinger 
25b8d26b3bSNicholas Bellinger #include "ib_isert.h"
26b8d26b3bSNicholas Bellinger 
2745678b6bSSagi Grimberg static int isert_debug_level;
2824f412ddSSagi Grimberg module_param_named(debug_level, isert_debug_level, int, 0644);
2924f412ddSSagi Grimberg MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
3024f412ddSSagi Grimberg 
31b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
32b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
33b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
34b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
35b8d26b3bSNicholas Bellinger 
36f93f3a70SSagi Grimberg static int
37f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
382371e5daSSagi Grimberg static int
399679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn);
402371e5daSSagi Grimberg static int
412371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
42ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
43d40945d8SVu Pham 
442f1b6b7dSSagi Grimberg static void isert_release_work(struct work_struct *work);
459679cc51SChristoph Hellwig static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc);
469679cc51SChristoph Hellwig static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc);
479679cc51SChristoph Hellwig static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc);
489679cc51SChristoph Hellwig static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc);
492f1b6b7dSSagi Grimberg 
50302cc7c3SSagi Grimberg static inline bool
51302cc7c3SSagi Grimberg isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
52302cc7c3SSagi Grimberg {
5323a548eeSSagi Grimberg 	return (conn->pi_support &&
54302cc7c3SSagi Grimberg 		cmd->prot_op != TARGET_PROT_NORMAL);
55302cc7c3SSagi Grimberg }
56302cc7c3SSagi Grimberg 
57302cc7c3SSagi Grimberg 
58d40945d8SVu Pham static void
59b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
60b8d26b3bSNicholas Bellinger {
616700425eSSagi Grimberg 	struct isert_conn *isert_conn = context;
62b8d26b3bSNicholas Bellinger 
63ea8a1616SSagi Grimberg 	isert_err("%s (%d): conn %p\n",
64ea8a1616SSagi Grimberg 		  ib_event_msg(e->event), e->event, isert_conn);
65ea8a1616SSagi Grimberg 
66b8d26b3bSNicholas Bellinger 	switch (e->event) {
67b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
68dac6ab30SSagi Grimberg 		rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
69b8d26b3bSNicholas Bellinger 		break;
70b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
714c22e07fSSagi Grimberg 		isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
72b8d26b3bSNicholas Bellinger 		break;
73b8d26b3bSNicholas Bellinger 	default:
74b8d26b3bSNicholas Bellinger 		break;
75b8d26b3bSNicholas Bellinger 	}
76b8d26b3bSNicholas Bellinger }
77b8d26b3bSNicholas Bellinger 
7840fc069aSSagi Grimberg static struct ib_qp *
7940fc069aSSagi Grimberg isert_create_qp(struct isert_conn *isert_conn,
8040fc069aSSagi Grimberg 		struct rdma_cm_id *cma_id)
8140fc069aSSagi Grimberg {
82c6e66307SYamin Friedman 	u32 cq_size = ISERT_QP_MAX_REQ_DTOS + ISERT_QP_MAX_RECV_DTOS + 2;
83dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
84c6e66307SYamin Friedman 	struct ib_device *ib_dev = device->ib_device;
8540fc069aSSagi Grimberg 	struct ib_qp_init_attr attr;
86317000b9SMax Gurtovoy 	int ret, factor;
87b8d26b3bSNicholas Bellinger 
88c6e66307SYamin Friedman 	isert_conn->cq = ib_cq_pool_get(ib_dev, cq_size, -1, IB_POLL_WORKQUEUE);
89c6e66307SYamin Friedman 	if (IS_ERR(isert_conn->cq)) {
90c6e66307SYamin Friedman 		isert_err("Unable to allocate cq\n");
91c6e66307SYamin Friedman 		ret = PTR_ERR(isert_conn->cq);
92c6e66307SYamin Friedman 		return ERR_PTR(ret);
93c6e66307SYamin Friedman 	}
94c6e66307SYamin Friedman 	isert_conn->cq_size = cq_size;
95c6e66307SYamin Friedman 
96b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
97b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
98b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
99c6e66307SYamin Friedman 	attr.send_cq = isert_conn->cq;
100c6e66307SYamin Friedman 	attr.recv_cq = isert_conn->cq;
101572a1434SSagi Grimberg 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS + 1;
102bdf20e72SSagi Grimberg 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
103317000b9SMax Gurtovoy 	factor = rdma_rw_mr_factor(device->ib_device, cma_id->port_num,
104317000b9SMax Gurtovoy 				   ISCSI_ISER_MAX_SG_TABLESIZE);
105317000b9SMax Gurtovoy 	attr.cap.max_rdma_ctxs = ISCSI_DEF_XMIT_CMDS_MAX * factor;
10633023fb8SSteve Wise 	attr.cap.max_send_sge = device->ib_device->attrs.max_send_sge;
107b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
108b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
109b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
110570db170SSagi Grimberg 	if (device->pi_capable)
111c0a6cbb9SIsrael Rukshin 		attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
112b8d26b3bSNicholas Bellinger 
11367cb3949SSagi Grimberg 	ret = rdma_create_qp(cma_id, device->pd, &attr);
114b8d26b3bSNicholas Bellinger 	if (ret) {
11524f412ddSSagi Grimberg 		isert_err("rdma_create_qp failed for cma_id %d\n", ret);
116c6e66307SYamin Friedman 		ib_cq_pool_put(isert_conn->cq, isert_conn->cq_size);
117c6e66307SYamin Friedman 
11840fc069aSSagi Grimberg 		return ERR_PTR(ret);
11940fc069aSSagi Grimberg 	}
12040fc069aSSagi Grimberg 
12140fc069aSSagi Grimberg 	return cma_id->qp;
12240fc069aSSagi Grimberg }
12340fc069aSSagi Grimberg 
12440fc069aSSagi Grimberg static int
125b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
126b8d26b3bSNicholas Bellinger {
127dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
12867cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
129b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
130b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
131b8d26b3bSNicholas Bellinger 	u64 dma_addr;
132b8d26b3bSNicholas Bellinger 	int i, j;
133b8d26b3bSNicholas Bellinger 
1346396bb22SKees Cook 	isert_conn->rx_descs = kcalloc(ISERT_QP_MAX_RECV_DTOS,
1356396bb22SKees Cook 				       sizeof(struct iser_rx_desc),
1366396bb22SKees Cook 				       GFP_KERNEL);
137dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
13893b80f29SLeon Romanovsky 		return -ENOMEM;
139b8d26b3bSNicholas Bellinger 
140dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
141b8d26b3bSNicholas Bellinger 
142b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
143b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
144b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
145b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
146b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
147b8d26b3bSNicholas Bellinger 
148b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
149b8d26b3bSNicholas Bellinger 
150b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
151b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
152b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
15334efc7dfSJason Gunthorpe 		rx_sg->lkey = device->pd->local_dma_lkey;
1549679cc51SChristoph Hellwig 		rx_desc->rx_cqe.done = isert_recv_done;
155b8d26b3bSNicholas Bellinger 	}
156b8d26b3bSNicholas Bellinger 
157b8d26b3bSNicholas Bellinger 	return 0;
158b8d26b3bSNicholas Bellinger 
159b8d26b3bSNicholas Bellinger dma_map_fail:
160dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
161b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
162b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
163b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
164b8d26b3bSNicholas Bellinger 	}
165dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
166dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
1674c22e07fSSagi Grimberg 	isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
168b8d26b3bSNicholas Bellinger 	return -ENOMEM;
169b8d26b3bSNicholas Bellinger }
170b8d26b3bSNicholas Bellinger 
171b8d26b3bSNicholas Bellinger static void
172b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
173b8d26b3bSNicholas Bellinger {
174dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
175b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
176b8d26b3bSNicholas Bellinger 	int i;
177b8d26b3bSNicholas Bellinger 
178dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
179b8d26b3bSNicholas Bellinger 		return;
180b8d26b3bSNicholas Bellinger 
181dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
182b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
183b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
184b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
185b8d26b3bSNicholas Bellinger 	}
186b8d26b3bSNicholas Bellinger 
187dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
188dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
189b8d26b3bSNicholas Bellinger }
190b8d26b3bSNicholas Bellinger 
191172369c5SSagi Grimberg static int
192172369c5SSagi Grimberg isert_create_device_ib_res(struct isert_device *device)
193172369c5SSagi Grimberg {
1944a061b28SOr Gerlitz 	struct ib_device *ib_dev = device->ib_device;
195fd8205e8SSagi Grimberg 	int ret;
196172369c5SSagi Grimberg 
19733023fb8SSteve Wise 	isert_dbg("devattr->max_send_sge: %d devattr->max_recv_sge %d\n",
19833023fb8SSteve Wise 		  ib_dev->attrs.max_send_sge, ib_dev->attrs.max_recv_sge);
1994a061b28SOr Gerlitz 	isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
200172369c5SSagi Grimberg 
201ed082d36SChristoph Hellwig 	device->pd = ib_alloc_pd(ib_dev, 0);
20267cb3949SSagi Grimberg 	if (IS_ERR(device->pd)) {
20367cb3949SSagi Grimberg 		ret = PTR_ERR(device->pd);
20467cb3949SSagi Grimberg 		isert_err("failed to allocate pd, device %p, ret=%d\n",
20567cb3949SSagi Grimberg 			  device, ret);
206c6e66307SYamin Friedman 		return ret;
20767cb3949SSagi Grimberg 	}
20867cb3949SSagi Grimberg 
209172369c5SSagi Grimberg 	/* Check signature cap */
2104a061b28SOr Gerlitz 	device->pi_capable = ib_dev->attrs.device_cap_flags &
211c0a6cbb9SIsrael Rukshin 			     IB_DEVICE_INTEGRITY_HANDOVER ? true : false;
21267cb3949SSagi Grimberg 
213b8d26b3bSNicholas Bellinger 	return 0;
214b8d26b3bSNicholas Bellinger }
215b8d26b3bSNicholas Bellinger 
216b8d26b3bSNicholas Bellinger static void
217b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
218b8d26b3bSNicholas Bellinger {
21924f412ddSSagi Grimberg 	isert_info("device %p\n", device);
220b8d26b3bSNicholas Bellinger 
22167cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
222b8d26b3bSNicholas Bellinger }
223b8d26b3bSNicholas Bellinger 
224b8d26b3bSNicholas Bellinger static void
225cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device)
226b8d26b3bSNicholas Bellinger {
227b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
228b8d26b3bSNicholas Bellinger 	device->refcount--;
2294c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
230b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
231b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
232b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
233b8d26b3bSNicholas Bellinger 		kfree(device);
234b8d26b3bSNicholas Bellinger 	}
235b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
236b8d26b3bSNicholas Bellinger }
237b8d26b3bSNicholas Bellinger 
238b8d26b3bSNicholas Bellinger static struct isert_device *
239cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id)
240b8d26b3bSNicholas Bellinger {
241b8d26b3bSNicholas Bellinger 	struct isert_device *device;
242b8d26b3bSNicholas Bellinger 	int ret;
243b8d26b3bSNicholas Bellinger 
244b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
245b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
246b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
247b8d26b3bSNicholas Bellinger 			device->refcount++;
2484c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
2494c22e07fSSagi Grimberg 				   device, device->refcount);
250b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
251b8d26b3bSNicholas Bellinger 			return device;
252b8d26b3bSNicholas Bellinger 		}
253b8d26b3bSNicholas Bellinger 	}
254b8d26b3bSNicholas Bellinger 
255b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
256b8d26b3bSNicholas Bellinger 	if (!device) {
257b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
258b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
259b8d26b3bSNicholas Bellinger 	}
260b8d26b3bSNicholas Bellinger 
261b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
262b8d26b3bSNicholas Bellinger 
263b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
264b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
265b8d26b3bSNicholas Bellinger 	if (ret) {
266b8d26b3bSNicholas Bellinger 		kfree(device);
267b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
268b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
269b8d26b3bSNicholas Bellinger 	}
270b8d26b3bSNicholas Bellinger 
271b8d26b3bSNicholas Bellinger 	device->refcount++;
272b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
2734c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
2744c22e07fSSagi Grimberg 		   device, device->refcount);
275b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
276b8d26b3bSNicholas Bellinger 
277b8d26b3bSNicholas Bellinger 	return device;
278b8d26b3bSNicholas Bellinger }
279b8d26b3bSNicholas Bellinger 
28059464ef4SVu Pham static void
281ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn)
282ae9ea9edSSagi Grimberg {
283ae9ea9edSSagi Grimberg 	isert_conn->state = ISER_CONN_INIT;
284bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_conn->node);
285dac6ab30SSagi Grimberg 	init_completion(&isert_conn->login_comp);
286ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
28763b268d2SRaju Rangoju 	init_waitqueue_head(&isert_conn->rem_wait);
288dac6ab30SSagi Grimberg 	kref_init(&isert_conn->kref);
289dac6ab30SSagi Grimberg 	mutex_init(&isert_conn->mutex);
2902f1b6b7dSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
291ae9ea9edSSagi Grimberg }
292ae9ea9edSSagi Grimberg 
293ae9ea9edSSagi Grimberg static void
294ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn)
295ae9ea9edSSagi Grimberg {
296dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
297ae9ea9edSSagi Grimberg 
298ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
299ed1083b2SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
3005adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
3015adabdd1SChristoph Hellwig 
302ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
3035adabdd1SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE,
304ae9ea9edSSagi Grimberg 			    DMA_FROM_DEVICE);
3055adabdd1SChristoph Hellwig 	kfree(isert_conn->login_req_buf);
306ae9ea9edSSagi Grimberg }
307ae9ea9edSSagi Grimberg 
308ae9ea9edSSagi Grimberg static int
309ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn,
310ae9ea9edSSagi Grimberg 		      struct ib_device *ib_dev)
311ae9ea9edSSagi Grimberg {
312ae9ea9edSSagi Grimberg 	int ret;
313ae9ea9edSSagi Grimberg 
3145adabdd1SChristoph Hellwig 	isert_conn->login_req_buf = kzalloc(sizeof(*isert_conn->login_req_buf),
3155adabdd1SChristoph Hellwig 			GFP_KERNEL);
31693b80f29SLeon Romanovsky 	if (!isert_conn->login_req_buf)
317ae9ea9edSSagi Grimberg 		return -ENOMEM;
318ae9ea9edSSagi Grimberg 
319ae9ea9edSSagi Grimberg 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
3205adabdd1SChristoph Hellwig 				isert_conn->login_req_buf,
3215adabdd1SChristoph Hellwig 				ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
322ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
323ae9ea9edSSagi Grimberg 	if (ret) {
324ae9ea9edSSagi Grimberg 		isert_err("login_req_dma mapping error: %d\n", ret);
325ae9ea9edSSagi Grimberg 		isert_conn->login_req_dma = 0;
3265adabdd1SChristoph Hellwig 		goto out_free_login_req_buf;
3275adabdd1SChristoph Hellwig 	}
3285adabdd1SChristoph Hellwig 
3295adabdd1SChristoph Hellwig 	isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
3305adabdd1SChristoph Hellwig 	if (!isert_conn->login_rsp_buf) {
3311d5840c9SWei Yongjun 		ret = -ENOMEM;
3325adabdd1SChristoph Hellwig 		goto out_unmap_login_req_buf;
333ae9ea9edSSagi Grimberg 	}
334ae9ea9edSSagi Grimberg 
335ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
3365adabdd1SChristoph Hellwig 					isert_conn->login_rsp_buf,
337ed1083b2SChristoph Hellwig 					ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
338ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
339ae9ea9edSSagi Grimberg 	if (ret) {
340ae9ea9edSSagi Grimberg 		isert_err("login_rsp_dma mapping error: %d\n", ret);
341ae9ea9edSSagi Grimberg 		isert_conn->login_rsp_dma = 0;
3425adabdd1SChristoph Hellwig 		goto out_free_login_rsp_buf;
343ae9ea9edSSagi Grimberg 	}
344ae9ea9edSSagi Grimberg 
345ae9ea9edSSagi Grimberg 	return 0;
346ae9ea9edSSagi Grimberg 
3475adabdd1SChristoph Hellwig out_free_login_rsp_buf:
3485adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
3495adabdd1SChristoph Hellwig out_unmap_login_req_buf:
350ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
3515adabdd1SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
3525adabdd1SChristoph Hellwig out_free_login_req_buf:
3535adabdd1SChristoph Hellwig 	kfree(isert_conn->login_req_buf);
354ae9ea9edSSagi Grimberg 	return ret;
355ae9ea9edSSagi Grimberg }
356ae9ea9edSSagi Grimberg 
357422bd0acSJenny Derzhavetz static void
358422bd0acSJenny Derzhavetz isert_set_nego_params(struct isert_conn *isert_conn,
359422bd0acSJenny Derzhavetz 		      struct rdma_conn_param *param)
360422bd0acSJenny Derzhavetz {
361422bd0acSJenny Derzhavetz 	struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
362422bd0acSJenny Derzhavetz 
363422bd0acSJenny Derzhavetz 	/* Set max inflight RDMA READ requests */
364422bd0acSJenny Derzhavetz 	isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
365422bd0acSJenny Derzhavetz 				attr->max_qp_init_rd_atom);
366422bd0acSJenny Derzhavetz 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
367422bd0acSJenny Derzhavetz 
368422bd0acSJenny Derzhavetz 	if (param->private_data) {
369422bd0acSJenny Derzhavetz 		u8 flags = *(u8 *)param->private_data;
370422bd0acSJenny Derzhavetz 
371422bd0acSJenny Derzhavetz 		/*
372422bd0acSJenny Derzhavetz 		 * use remote invalidation if the both initiator
373422bd0acSJenny Derzhavetz 		 * and the HCA support it
374422bd0acSJenny Derzhavetz 		 */
375422bd0acSJenny Derzhavetz 		isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
376422bd0acSJenny Derzhavetz 					  (attr->device_cap_flags &
377422bd0acSJenny Derzhavetz 					   IB_DEVICE_MEM_MGT_EXTENSIONS);
378422bd0acSJenny Derzhavetz 		if (isert_conn->snd_w_inv)
379422bd0acSJenny Derzhavetz 			isert_info("Using remote invalidation\n");
380422bd0acSJenny Derzhavetz 	}
381422bd0acSJenny Derzhavetz }
382422bd0acSJenny Derzhavetz 
383c6e66307SYamin Friedman static void
384c6e66307SYamin Friedman isert_destroy_qp(struct isert_conn *isert_conn)
385c6e66307SYamin Friedman {
386c6e66307SYamin Friedman 	ib_destroy_qp(isert_conn->qp);
387c6e66307SYamin Friedman 	ib_cq_pool_put(isert_conn->cq, isert_conn->cq_size);
388c6e66307SYamin Friedman }
389c6e66307SYamin Friedman 
390b8d26b3bSNicholas Bellinger static int
391b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
392b8d26b3bSNicholas Bellinger {
393ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
394ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
395b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
396b8d26b3bSNicholas Bellinger 	struct isert_device *device;
397b8d26b3bSNicholas Bellinger 	int ret = 0;
39814f4b54fSSagi Grimberg 
39914f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
40014f4b54fSSagi Grimberg 	if (!np->enabled) {
40114f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
40224f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
4038094ba0aSLeon Romanovsky 		return rdma_reject(cma_id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
40414f4b54fSSagi Grimberg 	}
40514f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
406b8d26b3bSNicholas Bellinger 
4074c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
408b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
409b8d26b3bSNicholas Bellinger 
410b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
411ae9ea9edSSagi Grimberg 	if (!isert_conn)
412b8d26b3bSNicholas Bellinger 		return -ENOMEM;
413b8d26b3bSNicholas Bellinger 
414ae9ea9edSSagi Grimberg 	isert_init_conn(isert_conn);
415dac6ab30SSagi Grimberg 	isert_conn->cm_id = cma_id;
416b8d26b3bSNicholas Bellinger 
417ae9ea9edSSagi Grimberg 	ret = isert_alloc_login_buf(isert_conn, cma_id->device);
418ae9ea9edSSagi Grimberg 	if (ret)
419b8d26b3bSNicholas Bellinger 		goto out;
420b8d26b3bSNicholas Bellinger 
421cf8ae958SSagi Grimberg 	device = isert_device_get(cma_id);
422b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
423b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
424b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
425b8d26b3bSNicholas Bellinger 	}
426dac6ab30SSagi Grimberg 	isert_conn->device = device;
427b8d26b3bSNicholas Bellinger 
428422bd0acSJenny Derzhavetz 	isert_set_nego_params(isert_conn, &event->param.conn);
4291a92e17eSSagi Grimberg 
430c6e66307SYamin Friedman 	isert_conn->qp = isert_create_qp(isert_conn, cma_id);
431c6e66307SYamin Friedman 	if (IS_ERR(isert_conn->qp)) {
432c6e66307SYamin Friedman 		ret = PTR_ERR(isert_conn->qp);
433b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
434c6e66307SYamin Friedman 	}
435b8d26b3bSNicholas Bellinger 
4369679cc51SChristoph Hellwig 	ret = isert_login_post_recv(isert_conn);
4372371e5daSSagi Grimberg 	if (ret)
438c6e66307SYamin Friedman 		goto out_destroy_qp;
4392371e5daSSagi Grimberg 
4402371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
4412371e5daSSagi Grimberg 	if (ret)
442c6e66307SYamin Friedman 		goto out_destroy_qp;
4432371e5daSSagi Grimberg 
444ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
445bd379220SJenny Derzhavetz 	list_add_tail(&isert_conn->node, &isert_np->accepted);
446ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
447b8d26b3bSNicholas Bellinger 
448b8d26b3bSNicholas Bellinger 	return 0;
449b8d26b3bSNicholas Bellinger 
450c6e66307SYamin Friedman out_destroy_qp:
451c6e66307SYamin Friedman 	isert_destroy_qp(isert_conn);
452b8d26b3bSNicholas Bellinger out_conn_dev:
453cf8ae958SSagi Grimberg 	isert_device_put(device);
454b8d26b3bSNicholas Bellinger out_rsp_dma_map:
455ae9ea9edSSagi Grimberg 	isert_free_login_buf(isert_conn);
456b8d26b3bSNicholas Bellinger out:
457b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
4588094ba0aSLeon Romanovsky 	rdma_reject(cma_id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
459b8d26b3bSNicholas Bellinger 	return ret;
460b8d26b3bSNicholas Bellinger }
461b8d26b3bSNicholas Bellinger 
462b8d26b3bSNicholas Bellinger static void
463b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
464b8d26b3bSNicholas Bellinger {
465dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
466b8d26b3bSNicholas Bellinger 
4674c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
468b8d26b3bSNicholas Bellinger 
46957df81e3SSagi Grimberg 	BUG_ON(!device);
47057df81e3SSagi Grimberg 
47119e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
47263b268d2SRaju Rangoju 	if (isert_conn->cm_id &&
47363b268d2SRaju Rangoju 	    !isert_conn->dev_removed)
474dac6ab30SSagi Grimberg 		rdma_destroy_id(isert_conn->cm_id);
47519e2090fSSagi Grimberg 
476c6e66307SYamin Friedman 	if (isert_conn->qp)
477c6e66307SYamin Friedman 		isert_destroy_qp(isert_conn);
478b8d26b3bSNicholas Bellinger 
4795adabdd1SChristoph Hellwig 	if (isert_conn->login_req_buf)
480ae9ea9edSSagi Grimberg 		isert_free_login_buf(isert_conn);
481ae9ea9edSSagi Grimberg 
482cf8ae958SSagi Grimberg 	isert_device_put(device);
48357df81e3SSagi Grimberg 
48463b268d2SRaju Rangoju 	if (isert_conn->dev_removed)
48563b268d2SRaju Rangoju 		wake_up_interruptible(&isert_conn->rem_wait);
48663b268d2SRaju Rangoju 	else
48757df81e3SSagi Grimberg 		kfree(isert_conn);
488b8d26b3bSNicholas Bellinger }
489b8d26b3bSNicholas Bellinger 
490b8d26b3bSNicholas Bellinger static void
491b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
492b8d26b3bSNicholas Bellinger {
49319e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
494bd379220SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
495c2f88b17SSagi Grimberg 
49624f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
497128e9cc8SSagi Grimberg 
498dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
499128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_UP;
500bd379220SJenny Derzhavetz 	kref_get(&isert_conn->kref);
501dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
502bd379220SJenny Derzhavetz 
503bd379220SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
504bd379220SJenny Derzhavetz 	list_move_tail(&isert_conn->node, &isert_np->pending);
505bd379220SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
506bd379220SJenny Derzhavetz 
507bd379220SJenny Derzhavetz 	isert_info("np %p: Allow accept_np to continue\n", isert_np);
508bd379220SJenny Derzhavetz 	up(&isert_np->sem);
509b8d26b3bSNicholas Bellinger }
510b8d26b3bSNicholas Bellinger 
511b8d26b3bSNicholas Bellinger static void
512dac6ab30SSagi Grimberg isert_release_kref(struct kref *kref)
513b8d26b3bSNicholas Bellinger {
514b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
515dac6ab30SSagi Grimberg 				struct isert_conn, kref);
516b8d26b3bSNicholas Bellinger 
5174c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
5184c22e07fSSagi Grimberg 		   current->pid);
519b8d26b3bSNicholas Bellinger 
520b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
521b8d26b3bSNicholas Bellinger }
522b8d26b3bSNicholas Bellinger 
523b8d26b3bSNicholas Bellinger static void
524b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
525b8d26b3bSNicholas Bellinger {
526dac6ab30SSagi Grimberg 	kref_put(&isert_conn->kref, isert_release_kref);
527b8d26b3bSNicholas Bellinger }
528b8d26b3bSNicholas Bellinger 
5296d1fba0cSJenny Derzhavetz static void
5306d1fba0cSJenny Derzhavetz isert_handle_unbound_conn(struct isert_conn *isert_conn)
5316d1fba0cSJenny Derzhavetz {
5326d1fba0cSJenny Derzhavetz 	struct isert_np *isert_np = isert_conn->cm_id->context;
5336d1fba0cSJenny Derzhavetz 
5346d1fba0cSJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
5356d1fba0cSJenny Derzhavetz 	if (!list_empty(&isert_conn->node)) {
5366d1fba0cSJenny Derzhavetz 		/*
5376d1fba0cSJenny Derzhavetz 		 * This means iscsi doesn't know this connection
5386d1fba0cSJenny Derzhavetz 		 * so schedule a cleanup ourselves
5396d1fba0cSJenny Derzhavetz 		 */
5406d1fba0cSJenny Derzhavetz 		list_del_init(&isert_conn->node);
5416d1fba0cSJenny Derzhavetz 		isert_put_conn(isert_conn);
5426d1fba0cSJenny Derzhavetz 		queue_work(isert_release_wq, &isert_conn->release_work);
5436d1fba0cSJenny Derzhavetz 	}
5446d1fba0cSJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
5456d1fba0cSJenny Derzhavetz }
5466d1fba0cSJenny Derzhavetz 
547954f2372SSagi Grimberg /**
548954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
549954f2372SSagi Grimberg  * @isert_conn: isert connection struct
550954f2372SSagi Grimberg  *
551954f2372SSagi Grimberg  * Notes:
552aea92980SJenny Derzhavetz  * In case the connection state is BOUND, move state
553954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
554128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
555954f2372SSagi Grimberg  *
556dac6ab30SSagi Grimberg  * This routine must be called with mutex held. Thus it is
557954f2372SSagi Grimberg  * safe to call multiple times.
558954f2372SSagi Grimberg  */
559954f2372SSagi Grimberg static void
560954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
561954f2372SSagi Grimberg {
562954f2372SSagi Grimberg 	int err;
563954f2372SSagi Grimberg 
5646d1fba0cSJenny Derzhavetz 	if (isert_conn->state >= ISER_CONN_TERMINATING)
5656d1fba0cSJenny Derzhavetz 		return;
5666d1fba0cSJenny Derzhavetz 
56724f412ddSSagi Grimberg 	isert_info("Terminating conn %p state %d\n",
568954f2372SSagi Grimberg 		   isert_conn, isert_conn->state);
569128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_TERMINATING;
570dac6ab30SSagi Grimberg 	err = rdma_disconnect(isert_conn->cm_id);
571954f2372SSagi Grimberg 	if (err)
57224f412ddSSagi Grimberg 		isert_warn("Failed rdma_disconnect isert_conn %p\n",
573954f2372SSagi Grimberg 			   isert_conn);
574954f2372SSagi Grimberg }
575954f2372SSagi Grimberg 
5763b726ae2SSagi Grimberg static int
577ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
578ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
579b8d26b3bSNicholas Bellinger {
580ea8a1616SSagi Grimberg 	isert_dbg("%s (%d): isert np %p\n",
581ea8a1616SSagi Grimberg 		  rdma_event_msg(event), event, isert_np);
5823b726ae2SSagi Grimberg 
583ca6c1d82SSagi Grimberg 	switch (event) {
584ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
585ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = NULL;
586ca6c1d82SSagi Grimberg 		break;
587ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
588ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = isert_setup_id(isert_np);
589ed8cb0a4SJenny Derzhavetz 		if (IS_ERR(isert_np->cm_id)) {
59024f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
591ed8cb0a4SJenny Derzhavetz 				  isert_np, PTR_ERR(isert_np->cm_id));
592ed8cb0a4SJenny Derzhavetz 			isert_np->cm_id = NULL;
593ca6c1d82SSagi Grimberg 		}
594ca6c1d82SSagi Grimberg 		break;
595ca6c1d82SSagi Grimberg 	default:
59624f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
597ca6c1d82SSagi Grimberg 			  isert_np, event);
598ca6c1d82SSagi Grimberg 	}
599ca6c1d82SSagi Grimberg 
6003b726ae2SSagi Grimberg 	return -1;
6013b726ae2SSagi Grimberg }
6023b726ae2SSagi Grimberg 
603ca6c1d82SSagi Grimberg static int
604ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
605ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
606ca6c1d82SSagi Grimberg {
607f81bf458SJenny Derzhavetz 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
608ca6c1d82SSagi Grimberg 
609dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
6106d1fba0cSJenny Derzhavetz 	switch (isert_conn->state) {
6116d1fba0cSJenny Derzhavetz 	case ISER_CONN_TERMINATING:
6126d1fba0cSJenny Derzhavetz 		break;
6136d1fba0cSJenny Derzhavetz 	case ISER_CONN_UP:
614128e9cc8SSagi Grimberg 		isert_conn_terminate(isert_conn);
615572a1434SSagi Grimberg 		ib_drain_qp(isert_conn->qp);
6166d1fba0cSJenny Derzhavetz 		isert_handle_unbound_conn(isert_conn);
6176d1fba0cSJenny Derzhavetz 		break;
6186d1fba0cSJenny Derzhavetz 	case ISER_CONN_BOUND:
6196d1fba0cSJenny Derzhavetz 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
6206d1fba0cSJenny Derzhavetz 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
6216d1fba0cSJenny Derzhavetz 		break;
6226d1fba0cSJenny Derzhavetz 	default:
623506f71d1SColin Ian King 		isert_warn("conn %p terminating in state %d\n",
6246d1fba0cSJenny Derzhavetz 			   isert_conn, isert_conn->state);
6256d1fba0cSJenny Derzhavetz 	}
626dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
627128e9cc8SSagi Grimberg 
6283b726ae2SSagi Grimberg 	return 0;
629b8d26b3bSNicholas Bellinger }
630b8d26b3bSNicholas Bellinger 
6314a579da2SSagi Grimberg static int
632954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
633954f2372SSagi Grimberg {
63419e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
635954f2372SSagi Grimberg 
636cd52cb26SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
637bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
638dac6ab30SSagi Grimberg 	isert_conn->cm_id = NULL;
639954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
6404a579da2SSagi Grimberg 
6414a579da2SSagi Grimberg 	return -1;
642954f2372SSagi Grimberg }
643954f2372SSagi Grimberg 
644b8d26b3bSNicholas Bellinger static int
645b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
646b8d26b3bSNicholas Bellinger {
647f81bf458SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
64863b268d2SRaju Rangoju 	struct isert_conn *isert_conn;
649b8d26b3bSNicholas Bellinger 	int ret = 0;
650b8d26b3bSNicholas Bellinger 
651ea8a1616SSagi Grimberg 	isert_info("%s (%d): status %d id %p np %p\n",
652ea8a1616SSagi Grimberg 		   rdma_event_msg(event->event), event->event,
6534c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
654b8d26b3bSNicholas Bellinger 
655f81bf458SJenny Derzhavetz 	if (isert_np->cm_id == cma_id)
656f81bf458SJenny Derzhavetz 		return isert_np_cma_handler(cma_id->context, event->event);
657f81bf458SJenny Derzhavetz 
658b8d26b3bSNicholas Bellinger 	switch (event->event) {
659b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
660b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
6613b726ae2SSagi Grimberg 		if (ret)
6624c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
663b8d26b3bSNicholas Bellinger 		break;
664b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
665b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
666b8d26b3bSNicholas Bellinger 		break;
667*df561f66SGustavo A. R. Silva 	case RDMA_CM_EVENT_ADDR_CHANGE:
668*df561f66SGustavo A. R. Silva 	case RDMA_CM_EVENT_DISCONNECTED:
66988c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
670ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
671b8d26b3bSNicholas Bellinger 		break;
67263b268d2SRaju Rangoju 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
67363b268d2SRaju Rangoju 		isert_conn = cma_id->qp->qp_context;
67463b268d2SRaju Rangoju 		isert_conn->dev_removed = true;
67563b268d2SRaju Rangoju 		isert_disconnected_handler(cma_id, event->event);
67663b268d2SRaju Rangoju 		wait_event_interruptible(isert_conn->rem_wait,
67763b268d2SRaju Rangoju 					 isert_conn->state == ISER_CONN_DOWN);
67863b268d2SRaju Rangoju 		kfree(isert_conn);
67963b268d2SRaju Rangoju 		/*
68063b268d2SRaju Rangoju 		 * return non-zero from the callback to destroy
68163b268d2SRaju Rangoju 		 * the rdma cm id
68263b268d2SRaju Rangoju 		 */
68363b268d2SRaju Rangoju 		return 1;
684d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_REJECTED:
6851e38a366SSteve Wise 		isert_info("Connection rejected: %s\n",
6861e38a366SSteve Wise 			   rdma_reject_msg(cma_id, event->status));
687*df561f66SGustavo A. R. Silva 		fallthrough;
688d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_UNREACHABLE:
689b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
6904a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
691954f2372SSagi Grimberg 		break;
692b8d26b3bSNicholas Bellinger 	default:
69324f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
694b8d26b3bSNicholas Bellinger 		break;
695b8d26b3bSNicholas Bellinger 	}
696b8d26b3bSNicholas Bellinger 
697b8d26b3bSNicholas Bellinger 	return ret;
698b8d26b3bSNicholas Bellinger }
699b8d26b3bSNicholas Bellinger 
700b8d26b3bSNicholas Bellinger static int
7014366b19cSJenny Derzhavetz isert_post_recvm(struct isert_conn *isert_conn, u32 count)
702b8d26b3bSNicholas Bellinger {
703e01a7674SBart Van Assche 	struct ib_recv_wr *rx_wr;
704b8d26b3bSNicholas Bellinger 	int i, ret;
705b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
706b8d26b3bSNicholas Bellinger 
707dac6ab30SSagi Grimberg 	for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
7084366b19cSJenny Derzhavetz 		rx_desc = &isert_conn->rx_descs[i];
7099679cc51SChristoph Hellwig 
7109679cc51SChristoph Hellwig 		rx_wr->wr_cqe = &rx_desc->rx_cqe;
711b8d26b3bSNicholas Bellinger 		rx_wr->sg_list = &rx_desc->rx_sg;
712b8d26b3bSNicholas Bellinger 		rx_wr->num_sge = 1;
713b8d26b3bSNicholas Bellinger 		rx_wr->next = rx_wr + 1;
7147a56dc88SSagi Grimberg 		rx_desc->in_use = false;
715b8d26b3bSNicholas Bellinger 	}
716b8d26b3bSNicholas Bellinger 	rx_wr--;
717b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
718b8d26b3bSNicholas Bellinger 
719e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr, NULL);
7206d1fba0cSJenny Derzhavetz 	if (ret)
72124f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
7224366b19cSJenny Derzhavetz 
7234366b19cSJenny Derzhavetz 	return ret;
7244366b19cSJenny Derzhavetz }
7254366b19cSJenny Derzhavetz 
7264366b19cSJenny Derzhavetz static int
7274366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
7284366b19cSJenny Derzhavetz {
729e01a7674SBart Van Assche 	struct ib_recv_wr rx_wr;
7304366b19cSJenny Derzhavetz 	int ret;
7314366b19cSJenny Derzhavetz 
7327a56dc88SSagi Grimberg 	if (!rx_desc->in_use) {
7337a56dc88SSagi Grimberg 		/*
7347a56dc88SSagi Grimberg 		 * if the descriptor is not in-use we already reposted it
7357a56dc88SSagi Grimberg 		 * for recv, so just silently return
7367a56dc88SSagi Grimberg 		 */
7377a56dc88SSagi Grimberg 		return 0;
7387a56dc88SSagi Grimberg 	}
7397a56dc88SSagi Grimberg 
7407a56dc88SSagi Grimberg 	rx_desc->in_use = false;
7419679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &rx_desc->rx_cqe;
7424366b19cSJenny Derzhavetz 	rx_wr.sg_list = &rx_desc->rx_sg;
7434366b19cSJenny Derzhavetz 	rx_wr.num_sge = 1;
7444366b19cSJenny Derzhavetz 	rx_wr.next = NULL;
7454366b19cSJenny Derzhavetz 
746e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL);
7476d1fba0cSJenny Derzhavetz 	if (ret)
7484366b19cSJenny Derzhavetz 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
7494366b19cSJenny Derzhavetz 
750b8d26b3bSNicholas Bellinger 	return ret;
751b8d26b3bSNicholas Bellinger }
752b8d26b3bSNicholas Bellinger 
753b8d26b3bSNicholas Bellinger static int
7549679cc51SChristoph Hellwig isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
755b8d26b3bSNicholas Bellinger {
756dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
757e01a7674SBart Van Assche 	struct ib_send_wr send_wr;
758b8d26b3bSNicholas Bellinger 	int ret;
759b8d26b3bSNicholas Bellinger 
760b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
761b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
762b8d26b3bSNicholas Bellinger 
7639679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_login_send_done;
7649679cc51SChristoph Hellwig 
765b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
7669679cc51SChristoph Hellwig 	send_wr.wr_cqe	= &tx_desc->tx_cqe;
767b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
768b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
769b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
770b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
771b8d26b3bSNicholas Bellinger 
772e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &send_wr, NULL);
773bdf20e72SSagi Grimberg 	if (ret)
77424f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
775b8d26b3bSNicholas Bellinger 
776b8d26b3bSNicholas Bellinger 	return ret;
777b8d26b3bSNicholas Bellinger }
778b8d26b3bSNicholas Bellinger 
779b8d26b3bSNicholas Bellinger static void
780763b6965SAlex Estrin __isert_create_send_desc(struct isert_device *device,
781b8d26b3bSNicholas Bellinger 			 struct iser_tx_desc *tx_desc)
782b8d26b3bSNicholas Bellinger {
783b8d26b3bSNicholas Bellinger 
784d3cf81f9SSagi Grimberg 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
785d3cf81f9SSagi Grimberg 	tx_desc->iser_header.flags = ISCSI_CTRL;
786b8d26b3bSNicholas Bellinger 
787b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
788b8d26b3bSNicholas Bellinger 
78934efc7dfSJason Gunthorpe 	if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
79034efc7dfSJason Gunthorpe 		tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
79124f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
792b8d26b3bSNicholas Bellinger 	}
793b8d26b3bSNicholas Bellinger }
794b8d26b3bSNicholas Bellinger 
795763b6965SAlex Estrin static void
796763b6965SAlex Estrin isert_create_send_desc(struct isert_conn *isert_conn,
797763b6965SAlex Estrin 		       struct isert_cmd *isert_cmd,
798763b6965SAlex Estrin 		       struct iser_tx_desc *tx_desc)
799763b6965SAlex Estrin {
800763b6965SAlex Estrin 	struct isert_device *device = isert_conn->device;
801763b6965SAlex Estrin 	struct ib_device *ib_dev = device->ib_device;
802763b6965SAlex Estrin 
803763b6965SAlex Estrin 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
804763b6965SAlex Estrin 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
805763b6965SAlex Estrin 
806763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
807763b6965SAlex Estrin }
808763b6965SAlex Estrin 
809b8d26b3bSNicholas Bellinger static int
810b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
811b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
812b8d26b3bSNicholas Bellinger {
813dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
81467cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
815b8d26b3bSNicholas Bellinger 	u64 dma_addr;
816b8d26b3bSNicholas Bellinger 
817b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
818b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
819b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
82024f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
821b8d26b3bSNicholas Bellinger 		return -ENOMEM;
822b8d26b3bSNicholas Bellinger 	}
823b8d26b3bSNicholas Bellinger 
824b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
825b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
826b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
82734efc7dfSJason Gunthorpe 	tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
828b8d26b3bSNicholas Bellinger 
8294c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
8304c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
8314c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
832b8d26b3bSNicholas Bellinger 
833b8d26b3bSNicholas Bellinger 	return 0;
834b8d26b3bSNicholas Bellinger }
835b8d26b3bSNicholas Bellinger 
836b8d26b3bSNicholas Bellinger static void
83795b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
83868a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
839b8d26b3bSNicholas Bellinger {
84095b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
84195b60f07SNicholas Bellinger 
8429679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_send_done;
8439679cc51SChristoph Hellwig 	send_wr->wr_cqe = &tx_desc->tx_cqe;
844422bd0acSJenny Derzhavetz 
845422bd0acSJenny Derzhavetz 	if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
846422bd0acSJenny Derzhavetz 		send_wr->opcode  = IB_WR_SEND_WITH_INV;
847422bd0acSJenny Derzhavetz 		send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
848422bd0acSJenny Derzhavetz 	} else {
849b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_SEND;
850422bd0acSJenny Derzhavetz 	}
851422bd0acSJenny Derzhavetz 
85295b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
853b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
85495b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
855b8d26b3bSNicholas Bellinger }
856b8d26b3bSNicholas Bellinger 
857b8d26b3bSNicholas Bellinger static int
8589679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn)
859b8d26b3bSNicholas Bellinger {
860e01a7674SBart Van Assche 	struct ib_recv_wr rx_wr;
861b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
862b8d26b3bSNicholas Bellinger 	int ret;
863b8d26b3bSNicholas Bellinger 
864b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
865b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
866ed1083b2SChristoph Hellwig 	sge.length = ISER_RX_PAYLOAD_SIZE;
86734efc7dfSJason Gunthorpe 	sge.lkey = isert_conn->device->pd->local_dma_lkey;
868b8d26b3bSNicholas Bellinger 
86924f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
870b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
871b8d26b3bSNicholas Bellinger 
8729679cc51SChristoph Hellwig 	isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
8739679cc51SChristoph Hellwig 
874b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
8759679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
876b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
877b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
878b8d26b3bSNicholas Bellinger 
879e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL);
8806d1fba0cSJenny Derzhavetz 	if (ret)
88124f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
882b8d26b3bSNicholas Bellinger 
883b8d26b3bSNicholas Bellinger 	return ret;
884b8d26b3bSNicholas Bellinger }
885b8d26b3bSNicholas Bellinger 
886b8d26b3bSNicholas Bellinger static int
887b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
888b8d26b3bSNicholas Bellinger 		   u32 length)
889b8d26b3bSNicholas Bellinger {
890b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
891dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
89267cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
893dac6ab30SSagi Grimberg 	struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
894b8d26b3bSNicholas Bellinger 	int ret;
895b8d26b3bSNicholas Bellinger 
896763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
897b8d26b3bSNicholas Bellinger 
898b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
899b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
900b8d26b3bSNicholas Bellinger 
901b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
902b8d26b3bSNicholas Bellinger 
903b8d26b3bSNicholas Bellinger 	if (length > 0) {
904b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
905b8d26b3bSNicholas Bellinger 
906b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
907b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
908b8d26b3bSNicholas Bellinger 
909b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
910b8d26b3bSNicholas Bellinger 
911b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
912b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
913b8d26b3bSNicholas Bellinger 
914b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
915b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
91634efc7dfSJason Gunthorpe 		tx_dsg->lkey	= isert_conn->device->pd->local_dma_lkey;
917b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
918b8d26b3bSNicholas Bellinger 	}
919b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
920b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
921b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
922b8d26b3bSNicholas Bellinger 			if (ret)
923b8d26b3bSNicholas Bellinger 				return ret;
924b8d26b3bSNicholas Bellinger 
9254366b19cSJenny Derzhavetz 			ret = isert_post_recvm(isert_conn,
9264366b19cSJenny Derzhavetz 					       ISERT_QP_MAX_RECV_DTOS);
927b8d26b3bSNicholas Bellinger 			if (ret)
928b8d26b3bSNicholas Bellinger 				return ret;
929b8d26b3bSNicholas Bellinger 
930128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
931dac6ab30SSagi Grimberg 			mutex_lock(&isert_conn->mutex);
932128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
933dac6ab30SSagi Grimberg 			mutex_unlock(&isert_conn->mutex);
934b8d26b3bSNicholas Bellinger 			goto post_send;
935b8d26b3bSNicholas Bellinger 		}
936b8d26b3bSNicholas Bellinger 
9379679cc51SChristoph Hellwig 		ret = isert_login_post_recv(isert_conn);
938b8d26b3bSNicholas Bellinger 		if (ret)
939b8d26b3bSNicholas Bellinger 			return ret;
940b8d26b3bSNicholas Bellinger 	}
941b8d26b3bSNicholas Bellinger post_send:
9429679cc51SChristoph Hellwig 	ret = isert_login_post_send(isert_conn, tx_desc);
943b8d26b3bSNicholas Bellinger 	if (ret)
944b8d26b3bSNicholas Bellinger 		return ret;
945b8d26b3bSNicholas Bellinger 
946b8d26b3bSNicholas Bellinger 	return 0;
947b8d26b3bSNicholas Bellinger }
948b8d26b3bSNicholas Bellinger 
949b8d26b3bSNicholas Bellinger static void
9502371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
951b8d26b3bSNicholas Bellinger {
9525adabdd1SChristoph Hellwig 	struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
9532371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
954b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
955b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
956b8d26b3bSNicholas Bellinger 	int size;
957b8d26b3bSNicholas Bellinger 
95824f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
9592371e5daSSagi Grimberg 
9602371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
961b8d26b3bSNicholas Bellinger 
962b8d26b3bSNicholas Bellinger 	if (login->first_request) {
963b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
964b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
965b8d26b3bSNicholas Bellinger 		/*
966b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
967b8d26b3bSNicholas Bellinger 		 * login request PDU.
968b8d26b3bSNicholas Bellinger 		 */
969b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
970b8d26b3bSNicholas Bellinger 		login->current_stage =
971b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
972b8d26b3bSNicholas Bellinger 			 >> 2;
973b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
974b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
975b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
976b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
977b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
978b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
979b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
980b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
981b8d26b3bSNicholas Bellinger 	}
982b8d26b3bSNicholas Bellinger 
983b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
984b8d26b3bSNicholas Bellinger 
985b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
9864c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
9874c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
9884c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
989b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
990b8d26b3bSNicholas Bellinger 
9916faaa85fSNicholas Bellinger 	if (login->first_request) {
992dac6ab30SSagi Grimberg 		complete(&isert_conn->login_comp);
9936faaa85fSNicholas Bellinger 		return;
9946faaa85fSNicholas Bellinger 	}
9956faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
996b8d26b3bSNicholas Bellinger }
997b8d26b3bSNicholas Bellinger 
998b8d26b3bSNicholas Bellinger static struct iscsi_cmd
9994366b19cSJenny Derzhavetz *isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
1000b8d26b3bSNicholas Bellinger {
10016700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1002b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1003d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1004b8d26b3bSNicholas Bellinger 
1005676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1006d703ce2fSNicholas Bellinger 	if (!cmd) {
100724f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1008b8d26b3bSNicholas Bellinger 		return NULL;
1009b8d26b3bSNicholas Bellinger 	}
1010d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1011b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1012d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
10134366b19cSJenny Derzhavetz 	isert_cmd->rx_desc = rx_desc;
1014b8d26b3bSNicholas Bellinger 
1015d703ce2fSNicholas Bellinger 	return cmd;
1016b8d26b3bSNicholas Bellinger }
1017b8d26b3bSNicholas Bellinger 
1018b8d26b3bSNicholas Bellinger static int
1019b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1020d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1021d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1022b8d26b3bSNicholas Bellinger {
1023b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1024b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1025b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1026b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
10279fd60088SJenny Derzhavetz 	unsigned int data_len;
1028b8d26b3bSNicholas Bellinger 
1029b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1030b8d26b3bSNicholas Bellinger 	if (rc < 0)
1031b8d26b3bSNicholas Bellinger 		return rc;
1032b8d26b3bSNicholas Bellinger 
1033b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1034b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1035b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
10369fd60088SJenny Derzhavetz 	data_len = cmd->se_cmd.data_length;
1037b8d26b3bSNicholas Bellinger 
10389fd60088SJenny Derzhavetz 	if (imm_data && imm_data_len == data_len)
10399fd60088SJenny Derzhavetz 		cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1040b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1041b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1042b8d26b3bSNicholas Bellinger 		return 0;
1043b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1044b8d26b3bSNicholas Bellinger 		dump_payload = true;
1045b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1046b8d26b3bSNicholas Bellinger 	}
1047b8d26b3bSNicholas Bellinger 
1048b8d26b3bSNicholas Bellinger 	if (!imm_data)
1049b8d26b3bSNicholas Bellinger 		return 0;
1050b8d26b3bSNicholas Bellinger 
10519fd60088SJenny Derzhavetz 	if (imm_data_len != data_len) {
1052b8d26b3bSNicholas Bellinger 		sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
10539fd60088SJenny Derzhavetz 		sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
10549fd60088SJenny Derzhavetz 				    &rx_desc->data[0], imm_data_len);
10559fd60088SJenny Derzhavetz 		isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
10569fd60088SJenny Derzhavetz 			  sg_nents, imm_data_len);
10579fd60088SJenny Derzhavetz 	} else {
10589fd60088SJenny Derzhavetz 		sg_init_table(&isert_cmd->sg, 1);
10599fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_sg = &isert_cmd->sg;
10609fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_nents = 1;
10619fd60088SJenny Derzhavetz 		sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
10629fd60088SJenny Derzhavetz 		isert_dbg("Transfer Immediate imm_data_len: %d\n",
10639fd60088SJenny Derzhavetz 			  imm_data_len);
10649fd60088SJenny Derzhavetz 	}
1065b8d26b3bSNicholas Bellinger 
1066b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1067b8d26b3bSNicholas Bellinger 
1068b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1069b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1070b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1071b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1072b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1073b8d26b3bSNicholas Bellinger 	}
1074b8d26b3bSNicholas Bellinger 
1075b8d26b3bSNicholas Bellinger sequence_cmd:
1076561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1077b8d26b3bSNicholas Bellinger 
1078b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
10790300b114SBart Van Assche 		iscsit_set_unsolicited_dataout(cmd);
10806cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1081afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1082b8d26b3bSNicholas Bellinger 
1083b8d26b3bSNicholas Bellinger 	return 0;
1084b8d26b3bSNicholas Bellinger }
1085b8d26b3bSNicholas Bellinger 
1086b8d26b3bSNicholas Bellinger static int
1087b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1088b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1089b8d26b3bSNicholas Bellinger {
1090b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1091b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1092b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1093b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1094b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1095b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1096b8d26b3bSNicholas Bellinger 
1097b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1098b8d26b3bSNicholas Bellinger 	if (rc < 0)
1099b8d26b3bSNicholas Bellinger 		return rc;
1100b8d26b3bSNicholas Bellinger 	else if (!cmd)
1101b8d26b3bSNicholas Bellinger 		return 0;
1102b8d26b3bSNicholas Bellinger 	/*
1103b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1104b8d26b3bSNicholas Bellinger 	 */
1105b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
110624f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1107b8d26b3bSNicholas Bellinger 		dump_stack();
1108b8d26b3bSNicholas Bellinger 		return -1;
1109b8d26b3bSNicholas Bellinger 	}
1110b8d26b3bSNicholas Bellinger 
11114c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
11124c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
11134c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
11144c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1115b8d26b3bSNicholas Bellinger 
1116b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1117b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1118b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1119b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1120b8d26b3bSNicholas Bellinger 	/*
1121b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1122b8d26b3bSNicholas Bellinger 	 */
1123b8d26b3bSNicholas Bellinger 	if (page_off) {
11244c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1125b8d26b3bSNicholas Bellinger 		dump_stack();
1126b8d26b3bSNicholas Bellinger 		return -1;
1127b8d26b3bSNicholas Bellinger 	}
11284c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
11294c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
11304c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1131b8d26b3bSNicholas Bellinger 
1132b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1133b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1134b8d26b3bSNicholas Bellinger 
1135b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1136b8d26b3bSNicholas Bellinger 	if (rc < 0)
1137b8d26b3bSNicholas Bellinger 		return rc;
1138b8d26b3bSNicholas Bellinger 
11394366b19cSJenny Derzhavetz 	/*
11404366b19cSJenny Derzhavetz 	 * multiple data-outs on the same command can arrive -
11414366b19cSJenny Derzhavetz 	 * so post the buffer before hand
11424366b19cSJenny Derzhavetz 	 */
11434366b19cSJenny Derzhavetz 	rc = isert_post_recv(isert_conn, rx_desc);
11444366b19cSJenny Derzhavetz 	if (rc) {
11454366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", rc);
11464366b19cSJenny Derzhavetz 		return rc;
11474366b19cSJenny Derzhavetz 	}
1148b8d26b3bSNicholas Bellinger 	return 0;
1149b8d26b3bSNicholas Bellinger }
1150b8d26b3bSNicholas Bellinger 
1151b8d26b3bSNicholas Bellinger static int
1152778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1153d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1154d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1155778de368SNicholas Bellinger {
1156778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1157778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1158778de368SNicholas Bellinger 	int rc;
1159778de368SNicholas Bellinger 
1160778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1161778de368SNicholas Bellinger 	if (rc < 0)
1162778de368SNicholas Bellinger 		return rc;
1163778de368SNicholas Bellinger 	/*
1164778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1165778de368SNicholas Bellinger 	 */
1166778de368SNicholas Bellinger 
1167778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1168778de368SNicholas Bellinger }
1169778de368SNicholas Bellinger 
1170778de368SNicholas Bellinger static int
1171adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1172d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1173d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1174adb54c29SNicholas Bellinger {
1175adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1176adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1177adb54c29SNicholas Bellinger 	int rc;
1178b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1179adb54c29SNicholas Bellinger 
1180adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1181adb54c29SNicholas Bellinger 	if (rc < 0)
1182adb54c29SNicholas Bellinger 		return rc;
1183adb54c29SNicholas Bellinger 
1184b44a2b67SSagi Grimberg 	if (payload_length) {
1185adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
118693b80f29SLeon Romanovsky 		if (!text_in)
1187adb54c29SNicholas Bellinger 			return -ENOMEM;
1188adb54c29SNicholas Bellinger 	}
1189adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1190adb54c29SNicholas Bellinger 
1191adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1192adb54c29SNicholas Bellinger 
1193adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1194adb54c29SNicholas Bellinger }
1195adb54c29SNicholas Bellinger 
1196adb54c29SNicholas Bellinger static int
1197b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1198b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1199b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1200b8d26b3bSNicholas Bellinger {
1201b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1202b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1203b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1204b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1205b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1206b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1207b8d26b3bSNicholas Bellinger 
1208fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1209ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
121024f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1211ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1212ca40d24eSNicholas Bellinger 		return 0;
1213ca40d24eSNicholas Bellinger 	}
1214ca40d24eSNicholas Bellinger 
1215b8d26b3bSNicholas Bellinger 	switch (opcode) {
1216b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
12174366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1218b8d26b3bSNicholas Bellinger 		if (!cmd)
1219b8d26b3bSNicholas Bellinger 			break;
1220b8d26b3bSNicholas Bellinger 
1221d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1222b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1223b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1224b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1225b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1226422bd0acSJenny Derzhavetz 		isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
1227b8d26b3bSNicholas Bellinger 
1228d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1229b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1230b8d26b3bSNicholas Bellinger 		break;
1231b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
12324366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1233b8d26b3bSNicholas Bellinger 		if (!cmd)
1234b8d26b3bSNicholas Bellinger 			break;
1235b8d26b3bSNicholas Bellinger 
1236d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1237d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1238778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1239b8d26b3bSNicholas Bellinger 		break;
1240b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1241b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1242b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1243b8d26b3bSNicholas Bellinger 		break;
1244b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
12454366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1246b8d26b3bSNicholas Bellinger 		if (!cmd)
1247b8d26b3bSNicholas Bellinger 			break;
1248b8d26b3bSNicholas Bellinger 
1249b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1250b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1251b8d26b3bSNicholas Bellinger 		break;
1252b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
12534366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1254b8d26b3bSNicholas Bellinger 		if (!cmd)
1255b8d26b3bSNicholas Bellinger 			break;
1256b8d26b3bSNicholas Bellinger 
1257b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1258b8d26b3bSNicholas Bellinger 		break;
1259adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
12604366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1261e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
12624366b19cSJenny Derzhavetz 		else
12634366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
12644366b19cSJenny Derzhavetz 
1265e4f4e801SSagi Grimberg 		if (!cmd)
1266e4f4e801SSagi Grimberg 			break;
1267adb54c29SNicholas Bellinger 
1268d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1269d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1270adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1271adb54c29SNicholas Bellinger 		break;
1272b8d26b3bSNicholas Bellinger 	default:
127324f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1274b8d26b3bSNicholas Bellinger 		dump_stack();
1275b8d26b3bSNicholas Bellinger 		break;
1276b8d26b3bSNicholas Bellinger 	}
1277b8d26b3bSNicholas Bellinger 
1278b8d26b3bSNicholas Bellinger 	return ret;
1279b8d26b3bSNicholas Bellinger }
1280b8d26b3bSNicholas Bellinger 
1281b8d26b3bSNicholas Bellinger static void
12829679cc51SChristoph Hellwig isert_print_wc(struct ib_wc *wc, const char *type)
1283b8d26b3bSNicholas Bellinger {
12849679cc51SChristoph Hellwig 	if (wc->status != IB_WC_WR_FLUSH_ERR)
12859679cc51SChristoph Hellwig 		isert_err("%s failure: %s (%d) vend_err %x\n", type,
12869679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status,
12879679cc51SChristoph Hellwig 			  wc->vendor_err);
12889679cc51SChristoph Hellwig 	else
12899679cc51SChristoph Hellwig 		isert_dbg("%s failure: %s (%d)\n", type,
12909679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status);
12919679cc51SChristoph Hellwig }
12929679cc51SChristoph Hellwig 
12939679cc51SChristoph Hellwig static void
12949679cc51SChristoph Hellwig isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
12959679cc51SChristoph Hellwig {
12969679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
12979679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
12989679cc51SChristoph Hellwig 	struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
12999679cc51SChristoph Hellwig 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1300d3cf81f9SSagi Grimberg 	struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
1301b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1302b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1303b8d26b3bSNicholas Bellinger 
13049679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
13059679cc51SChristoph Hellwig 		isert_print_wc(wc, "recv");
13069679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
13079679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
13089679cc51SChristoph Hellwig 		return;
13099679cc51SChristoph Hellwig 	}
13109679cc51SChristoph Hellwig 
13117a56dc88SSagi Grimberg 	rx_desc->in_use = true;
13127a56dc88SSagi Grimberg 
13139679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
13149679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
13159679cc51SChristoph Hellwig 
13169679cc51SChristoph Hellwig 	isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
13179679cc51SChristoph Hellwig 		 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
13189679cc51SChristoph Hellwig 		 (int)(wc->byte_len - ISER_HEADERS_LEN));
13199679cc51SChristoph Hellwig 
1320d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1321b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1322d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1323d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1324d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
13254c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1326b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1327b8d26b3bSNicholas Bellinger 		}
1328d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1329d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1330d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
13314c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1332b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1333b8d26b3bSNicholas Bellinger 		}
1334b8d26b3bSNicholas Bellinger 
133524f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1336b8d26b3bSNicholas Bellinger 		break;
1337b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
133824f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1339b8d26b3bSNicholas Bellinger 		break;
1340b8d26b3bSNicholas Bellinger 	default:
1341d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1342b8d26b3bSNicholas Bellinger 		break;
1343b8d26b3bSNicholas Bellinger 	}
1344b8d26b3bSNicholas Bellinger 
13456c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1346b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
13479679cc51SChristoph Hellwig 
13489679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
13499679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1350b8d26b3bSNicholas Bellinger }
1351b8d26b3bSNicholas Bellinger 
1352b8d26b3bSNicholas Bellinger static void
13539679cc51SChristoph Hellwig isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1354b8d26b3bSNicholas Bellinger {
13559679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1356fce50a2fSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->device->ib_device;
1357b8d26b3bSNicholas Bellinger 
13589679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
13599679cc51SChristoph Hellwig 		isert_print_wc(wc, "login recv");
13609679cc51SChristoph Hellwig 		return;
1361b8d26b3bSNicholas Bellinger 	}
1362b8d26b3bSNicholas Bellinger 
13639679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
13649679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1365b8d26b3bSNicholas Bellinger 
13669679cc51SChristoph Hellwig 	isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
1367b8d26b3bSNicholas Bellinger 
13682371e5daSSagi Grimberg 	if (isert_conn->conn) {
13692371e5daSSagi Grimberg 		struct iscsi_login *login = isert_conn->conn->conn_login;
13702371e5daSSagi Grimberg 
13712371e5daSSagi Grimberg 		if (login && !login->first_request)
13722371e5daSSagi Grimberg 			isert_rx_login_req(isert_conn);
13732371e5daSSagi Grimberg 	}
13749679cc51SChristoph Hellwig 
1375dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
13762371e5daSSagi Grimberg 	complete(&isert_conn->login_req_comp);
1377dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
1378b8d26b3bSNicholas Bellinger 
13799679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
13809679cc51SChristoph Hellwig 				ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1381b8d26b3bSNicholas Bellinger }
1382b8d26b3bSNicholas Bellinger 
1383e3d7e4c3SSagi Grimberg static void
138438a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
1385e3d7e4c3SSagi Grimberg {
138638a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
138738a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
1388e3d7e4c3SSagi Grimberg 
138938a2d0d4SChristoph Hellwig 	if (!cmd->rw.nr_ops)
139038a2d0d4SChristoph Hellwig 		return;
139138a2d0d4SChristoph Hellwig 
139238a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
139338a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
139438a2d0d4SChristoph Hellwig 				conn->cm_id->port_num, se_cmd->t_data_sg,
139538a2d0d4SChristoph Hellwig 				se_cmd->t_data_nents, se_cmd->t_prot_sg,
139638a2d0d4SChristoph Hellwig 				se_cmd->t_prot_nents, dir);
139738a2d0d4SChristoph Hellwig 	} else {
139838a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
139938a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
1400e3d7e4c3SSagi Grimberg 	}
1401e3d7e4c3SSagi Grimberg 
140238a2d0d4SChristoph Hellwig 	cmd->rw.nr_ops = 0;
140359464ef4SVu Pham }
140459464ef4SVu Pham 
140559464ef4SVu Pham static void
140603e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1407b8d26b3bSNicholas Bellinger {
1408d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1409b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1410186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1411e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1412b8d26b3bSNicholas Bellinger 
14134c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1414b8d26b3bSNicholas Bellinger 
1415b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1416b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1417b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1418b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14195159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1420b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1421b8d26b3bSNicholas Bellinger 
142203e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1423b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
142403e7848aSNicholas Bellinger 			/*
142503e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
142603e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
142703e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
142803e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
142903e7848aSNicholas Bellinger 			 */
143003e7848aSNicholas Bellinger 			if (comp_err &&
143103e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
143203e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
143303e7848aSNicholas Bellinger 
1434afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
143503e7848aSNicholas Bellinger 			}
143603e7848aSNicholas Bellinger 		}
1437b8d26b3bSNicholas Bellinger 
143838a2d0d4SChristoph Hellwig 		isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1439186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1440186a9647SNicholas Bellinger 		break;
1441b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1442186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1443186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14445159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1445186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1446186a9647SNicholas Bellinger 
1447b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1448b8d26b3bSNicholas Bellinger 		break;
1449b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1450b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1451adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1452e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1453e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1454e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1455e4f4e801SSagi Grimberg 			break;
1456e4f4e801SSagi Grimberg 
1457b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1458b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14595159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1460b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1461b8d26b3bSNicholas Bellinger 
1462b8d26b3bSNicholas Bellinger 		/*
1463b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1464b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1465b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1466b8d26b3bSNicholas Bellinger 		 */
1467b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
146811378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
14693df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1470b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1471b8d26b3bSNicholas Bellinger 			break;
1472b8d26b3bSNicholas Bellinger 		}
1473*df561f66SGustavo A. R. Silva 		fallthrough;
1474b8d26b3bSNicholas Bellinger 	default:
1475d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1476b8d26b3bSNicholas Bellinger 		break;
1477b8d26b3bSNicholas Bellinger 	}
1478b8d26b3bSNicholas Bellinger }
1479b8d26b3bSNicholas Bellinger 
1480b8d26b3bSNicholas Bellinger static void
1481b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1482b8d26b3bSNicholas Bellinger {
1483b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
14844c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1485b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1486b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1487b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1488b8d26b3bSNicholas Bellinger 	}
1489b8d26b3bSNicholas Bellinger }
1490b8d26b3bSNicholas Bellinger 
1491b8d26b3bSNicholas Bellinger static void
1492b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
149303e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1494b8d26b3bSNicholas Bellinger {
1495dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
14964c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1497dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1498dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1499dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1500b8d26b3bSNicholas Bellinger 	}
1501b8d26b3bSNicholas Bellinger 
1502b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
150303e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1504b8d26b3bSNicholas Bellinger }
1505b8d26b3bSNicholas Bellinger 
150696b7973eSSagi Grimberg static int
150796b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1508f93f3a70SSagi Grimberg {
15099e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
151096b7973eSSagi Grimberg 	int ret;
1511f93f3a70SSagi Grimberg 
151296b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
15139e961ae7SSagi Grimberg 	if (ret) {
151424f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
15159e961ae7SSagi Grimberg 		goto fail_mr_status;
15169e961ae7SSagi Grimberg 	}
151796b7973eSSagi Grimberg 
15189e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
151996b7973eSSagi Grimberg 		u64 sec_offset_err;
15209e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
15219e961ae7SSagi Grimberg 
15229e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
15239e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
15249e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
15259e961ae7SSagi Grimberg 			break;
15269e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
15279e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
15289e961ae7SSagi Grimberg 			break;
15299e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
15309e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
15319e961ae7SSagi Grimberg 			break;
15329e961ae7SSagi Grimberg 		}
153396b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
153496b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
153596b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
15369e961ae7SSagi Grimberg 
15374c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
15389e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
15399e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
15409e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
15419e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
15429e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
15439e961ae7SSagi Grimberg 		ret = 1;
15449e961ae7SSagi Grimberg 	}
15459e961ae7SSagi Grimberg 
15469e961ae7SSagi Grimberg fail_mr_status:
154796b7973eSSagi Grimberg 	return ret;
154896b7973eSSagi Grimberg }
154996b7973eSSagi Grimberg 
155096b7973eSSagi Grimberg static void
15519679cc51SChristoph Hellwig isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
155296b7973eSSagi Grimberg {
15539679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1554dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
15559679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1556e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
15579679cc51SChristoph Hellwig 	struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
155896b7973eSSagi Grimberg 	int ret = 0;
155996b7973eSSagi Grimberg 
15609679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
15619679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma write");
15629679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
15639679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
15649679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
15659679cc51SChristoph Hellwig 		return;
15669679cc51SChristoph Hellwig 	}
15679679cc51SChristoph Hellwig 
15689679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
15699679cc51SChristoph Hellwig 
1570e9a53e73SIsrael Rukshin 	ret = isert_check_pi_status(cmd, isert_cmd->rw.reg->mr);
157138a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
157296b7973eSSagi Grimberg 
1573555a65f6SNicholas Bellinger 	if (ret) {
1574555a65f6SNicholas Bellinger 		/*
1575555a65f6SNicholas Bellinger 		 * transport_generic_request_failure() expects to have
1576555a65f6SNicholas Bellinger 		 * plus two references to handle queue-full, so re-add
1577555a65f6SNicholas Bellinger 		 * one here as target-core will have already dropped
1578555a65f6SNicholas Bellinger 		 * it after the first isert_put_datain() callback.
1579555a65f6SNicholas Bellinger 		 */
1580555a65f6SNicholas Bellinger 		kref_get(&cmd->cmd_kref);
1581555a65f6SNicholas Bellinger 		transport_generic_request_failure(cmd, cmd->pi_err);
1582555a65f6SNicholas Bellinger 	} else {
1583555a65f6SNicholas Bellinger 		/*
1584555a65f6SNicholas Bellinger 		 * XXX: isert_put_response() failure is not retried.
1585555a65f6SNicholas Bellinger 		 */
1586555a65f6SNicholas Bellinger 		ret = isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
15879e961ae7SSagi Grimberg 		if (ret)
1588555a65f6SNicholas Bellinger 			pr_warn_ratelimited("isert_put_response() ret: %d\n", ret);
1589555a65f6SNicholas Bellinger 	}
1590f93f3a70SSagi Grimberg }
1591f93f3a70SSagi Grimberg 
1592f93f3a70SSagi Grimberg static void
15939679cc51SChristoph Hellwig isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1594b8d26b3bSNicholas Bellinger {
15959679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
15969679cc51SChristoph Hellwig 	struct isert_device *device = isert_conn->device;
15979679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1598e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
1599d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1600b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
16015bac4b1aSSagi Grimberg 	int ret = 0;
1602b8d26b3bSNicholas Bellinger 
16039679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16049679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma read");
16059679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16069679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16079679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
16089679cc51SChristoph Hellwig 		return;
16099679cc51SChristoph Hellwig 	}
16109679cc51SChristoph Hellwig 
16119679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
16129679cc51SChristoph Hellwig 
1613b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
161438a2d0d4SChristoph Hellwig 
161538a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd))
1616e9a53e73SIsrael Rukshin 		ret = isert_check_pi_status(se_cmd, isert_cmd->rw.reg->mr);
161738a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
161838a2d0d4SChristoph Hellwig 	cmd->write_data_done = 0;
1619b8d26b3bSNicholas Bellinger 
162024f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1621b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1622b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1623b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1624b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1625b8d26b3bSNicholas Bellinger 
1626555a65f6SNicholas Bellinger 	/*
1627555a65f6SNicholas Bellinger 	 * transport_generic_request_failure() will drop the extra
1628555a65f6SNicholas Bellinger 	 * se_cmd->cmd_kref reference after T10-PI error, and handle
1629555a65f6SNicholas Bellinger 	 * any non-zero ->queue_status() callback error retries.
1630555a65f6SNicholas Bellinger 	 */
1631555a65f6SNicholas Bellinger 	if (ret)
1632555a65f6SNicholas Bellinger 		transport_generic_request_failure(se_cmd, se_cmd->pi_err);
1633555a65f6SNicholas Bellinger 	else
1634b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1635b8d26b3bSNicholas Bellinger }
1636b8d26b3bSNicholas Bellinger 
1637b8d26b3bSNicholas Bellinger static void
1638b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1639b8d26b3bSNicholas Bellinger {
1640b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1641b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1642b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1643dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1644d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1645b8d26b3bSNicholas Bellinger 
16464c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
16474c22e07fSSagi Grimberg 
1648b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1649b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1650b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1651*df561f66SGustavo A. R. Silva 		fallthrough;
1652d2271bd0SBart Van Assche 	case ISTATE_SEND_REJECT:
1653d2271bd0SBart Van Assche 	case ISTATE_SEND_TEXTRSP:
1654b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
16554c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
16564c22e07fSSagi Grimberg 				     ib_dev, false);
16573df8f68aSNicholas Bellinger 		break;
1658b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1659b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1660b8d26b3bSNicholas Bellinger 		break;
1661b8d26b3bSNicholas Bellinger 	default:
16624c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1663b8d26b3bSNicholas Bellinger 		dump_stack();
1664b8d26b3bSNicholas Bellinger 		break;
1665b8d26b3bSNicholas Bellinger 	}
1666b8d26b3bSNicholas Bellinger }
1667b8d26b3bSNicholas Bellinger 
1668b8d26b3bSNicholas Bellinger static void
16699679cc51SChristoph Hellwig isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
1670b8d26b3bSNicholas Bellinger {
16719679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16729679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
16739679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1674b8d26b3bSNicholas Bellinger 
16759679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16769679cc51SChristoph Hellwig 		isert_print_wc(wc, "login send");
16779679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16789679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16799679cc51SChristoph Hellwig 	}
16809679cc51SChristoph Hellwig 
16819679cc51SChristoph Hellwig 	isert_unmap_tx_desc(tx_desc, ib_dev);
16829679cc51SChristoph Hellwig }
16839679cc51SChristoph Hellwig 
16849679cc51SChristoph Hellwig static void
16859679cc51SChristoph Hellwig isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
16869679cc51SChristoph Hellwig {
16879679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16889679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
16899679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1690e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
16919679cc51SChristoph Hellwig 
16929679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16939679cc51SChristoph Hellwig 		isert_print_wc(wc, "send");
16949679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16959679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16969679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
16979679cc51SChristoph Hellwig 		return;
16989679cc51SChristoph Hellwig 	}
16999679cc51SChristoph Hellwig 
17009679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
17019679cc51SChristoph Hellwig 
17029679cc51SChristoph Hellwig 	switch (isert_cmd->iscsi_cmd->i_state) {
17039679cc51SChristoph Hellwig 	case ISTATE_SEND_TASKMGTRSP:
17049679cc51SChristoph Hellwig 	case ISTATE_SEND_LOGOUTRSP:
17059679cc51SChristoph Hellwig 	case ISTATE_SEND_REJECT:
17069679cc51SChristoph Hellwig 	case ISTATE_SEND_TEXTRSP:
1707b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1708b8d26b3bSNicholas Bellinger 
1709b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1710b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1711b8d26b3bSNicholas Bellinger 		return;
1712b8d26b3bSNicholas Bellinger 	default:
17139679cc51SChristoph Hellwig 		isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
17149679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1715b8d26b3bSNicholas Bellinger 		break;
1716b8d26b3bSNicholas Bellinger 	}
1717b8d26b3bSNicholas Bellinger }
1718b8d26b3bSNicholas Bellinger 
1719b8d26b3bSNicholas Bellinger static int
1720b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1721b8d26b3bSNicholas Bellinger {
1722b8d26b3bSNicholas Bellinger 	int ret;
1723b8d26b3bSNicholas Bellinger 
17244366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
17254366b19cSJenny Derzhavetz 	if (ret) {
17264366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", ret);
17274366b19cSJenny Derzhavetz 		return ret;
17284366b19cSJenny Derzhavetz 	}
17294366b19cSJenny Derzhavetz 
1730e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr, NULL);
1731b8d26b3bSNicholas Bellinger 	if (ret) {
173224f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
1733b8d26b3bSNicholas Bellinger 		return ret;
1734b8d26b3bSNicholas Bellinger 	}
1735b8d26b3bSNicholas Bellinger 	return ret;
1736b8d26b3bSNicholas Bellinger }
1737b8d26b3bSNicholas Bellinger 
1738b8d26b3bSNicholas Bellinger static int
1739b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1740b8d26b3bSNicholas Bellinger {
1741d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
17426700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1743b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1744b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1745b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1746b8d26b3bSNicholas Bellinger 
1747b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1748b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1749b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1750b8d26b3bSNicholas Bellinger 	/*
1751b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1752b8d26b3bSNicholas Bellinger 	 */
1753b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1754b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1755b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1756dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
175767cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1758b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1759dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1760b8d26b3bSNicholas Bellinger 
1761b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1762b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1763b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1764b8d26b3bSNicholas Bellinger 
1765b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1766b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1767dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1768b8d26b3bSNicholas Bellinger 
1769dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1770dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1771b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1772def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1773def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1774b8d26b3bSNicholas Bellinger 
1775dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1776dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1777dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
177834efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1779b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1780b8d26b3bSNicholas Bellinger 	}
1781b8d26b3bSNicholas Bellinger 
178268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1783b8d26b3bSNicholas Bellinger 
17844c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
1785b8d26b3bSNicholas Bellinger 
1786b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1787b8d26b3bSNicholas Bellinger }
1788b8d26b3bSNicholas Bellinger 
1789131e6abcSNicholas Bellinger static void
1790131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1791131e6abcSNicholas Bellinger {
1792131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
17936700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1794131e6abcSNicholas Bellinger 
1795131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
1796131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
1797131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
1798131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1799131e6abcSNicholas Bellinger 
1800131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
1801131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
180238a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1803131e6abcSNicholas Bellinger }
1804131e6abcSNicholas Bellinger 
1805e70beee7SNicholas Bellinger static enum target_prot_op
1806e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
1807e70beee7SNicholas Bellinger {
18086700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1809dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1810e70beee7SNicholas Bellinger 
181123a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
181223a548eeSSagi Grimberg 		if (device->pi_capable) {
181324f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
181423a548eeSSagi Grimberg 			isert_conn->pi_support = true;
1815e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
181623a548eeSSagi Grimberg 		}
181723a548eeSSagi Grimberg 	}
181823a548eeSSagi Grimberg 
181924f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
182023a548eeSSagi Grimberg 	isert_conn->pi_support = false;
1821e70beee7SNicholas Bellinger 
1822e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
1823e70beee7SNicholas Bellinger }
1824e70beee7SNicholas Bellinger 
1825b8d26b3bSNicholas Bellinger static int
1826b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1827b8d26b3bSNicholas Bellinger 		bool nopout_response)
1828b8d26b3bSNicholas Bellinger {
1829d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18306700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1831b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1832b8d26b3bSNicholas Bellinger 
1833b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1834b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1835b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1836b8d26b3bSNicholas Bellinger 			       nopout_response);
1837b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
183868a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1839b8d26b3bSNicholas Bellinger 
18404c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
1841b8d26b3bSNicholas Bellinger 
1842b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1843b8d26b3bSNicholas Bellinger }
1844b8d26b3bSNicholas Bellinger 
1845b8d26b3bSNicholas Bellinger static int
1846b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1847b8d26b3bSNicholas Bellinger {
1848d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18496700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1850b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1851b8d26b3bSNicholas Bellinger 
1852b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1853b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1854b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1855b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
185668a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1857b8d26b3bSNicholas Bellinger 
18584c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
1859b8d26b3bSNicholas Bellinger 
1860b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1861b8d26b3bSNicholas Bellinger }
1862b8d26b3bSNicholas Bellinger 
1863b8d26b3bSNicholas Bellinger static int
1864b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1865b8d26b3bSNicholas Bellinger {
1866d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18676700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1868b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1869b8d26b3bSNicholas Bellinger 
1870b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1871b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1872b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1873b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
187468a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1875b8d26b3bSNicholas Bellinger 
18764c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
1877b8d26b3bSNicholas Bellinger 
1878b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1879b8d26b3bSNicholas Bellinger }
1880b8d26b3bSNicholas Bellinger 
1881b8d26b3bSNicholas Bellinger static int
1882b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1883b8d26b3bSNicholas Bellinger {
1884d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18856700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1886b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1887dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
188867cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
18893df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
18903df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
18913df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1892b8d26b3bSNicholas Bellinger 
1893b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
18943df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
1895b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
18963df8f68aSNicholas Bellinger 
18973df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
1898dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
18993df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
19003df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
1901def4a6ffSAlexey Khoroshilov 	if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1902def4a6ffSAlexey Khoroshilov 		return -ENOMEM;
1903dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1904dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
19053df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
190634efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
19073df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
19083df8f68aSNicholas Bellinger 
190968a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1910b8d26b3bSNicholas Bellinger 
19114c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
1912b8d26b3bSNicholas Bellinger 
1913b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1914b8d26b3bSNicholas Bellinger }
1915b8d26b3bSNicholas Bellinger 
1916b8d26b3bSNicholas Bellinger static int
1917adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1918adb54c29SNicholas Bellinger {
1919d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19206700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1921adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1922adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
1923adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1924adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
1925adb54c29SNicholas Bellinger 	int rc;
1926adb54c29SNicholas Bellinger 
1927adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
192822c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
1929adb54c29SNicholas Bellinger 	if (rc < 0)
1930adb54c29SNicholas Bellinger 		return rc;
1931adb54c29SNicholas Bellinger 
1932adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
1933adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1934adb54c29SNicholas Bellinger 
1935adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
1936dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
193767cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1938adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1939adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
1940adb54c29SNicholas Bellinger 
1941adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1942adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1943def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1944def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1945adb54c29SNicholas Bellinger 
1946adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
1947adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1948adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
194934efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1950adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1951adb54c29SNicholas Bellinger 	}
195268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1953adb54c29SNicholas Bellinger 
1954f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
1955adb54c29SNicholas Bellinger 
1956adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1957adb54c29SNicholas Bellinger }
1958adb54c29SNicholas Bellinger 
19593d73cf1aSSagi Grimberg static inline void
1960dae6345aSIsrael Rukshin isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_domain *domain)
19619e961ae7SSagi Grimberg {
196278eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
19633d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
19643d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
19653d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
196678eda2bbSSagi Grimberg 	/*
196778eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
196878eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
196978eda2bbSSagi Grimberg 	 * from se_cmd.
197078eda2bbSSagi Grimberg 	 */
197178eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
197278eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
197378eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
197478eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
197578eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
197678eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
19773d73cf1aSSagi Grimberg };
19783d73cf1aSSagi Grimberg 
19793d73cf1aSSagi Grimberg static int
19803d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
19813d73cf1aSSagi Grimberg {
198238a2d0d4SChristoph Hellwig 	memset(sig_attrs, 0, sizeof(*sig_attrs));
198338a2d0d4SChristoph Hellwig 
19849e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
19859e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
19869e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
198778eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
1988dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
19899e961ae7SSagi Grimberg 		break;
19909e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
19919e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
199278eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
1993dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
19949e961ae7SSagi Grimberg 		break;
19959e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
19969e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
1997dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
1998dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
19999e961ae7SSagi Grimberg 		break;
20009e961ae7SSagi Grimberg 	default:
200124f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
20029e961ae7SSagi Grimberg 		return -EINVAL;
20039e961ae7SSagi Grimberg 	}
20049e961ae7SSagi Grimberg 
200526232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD)
200626232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
200726232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG)
200826232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
200926232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)
201026232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
201126232872SMax Gurtovoy 
20129e961ae7SSagi Grimberg 	return 0;
20139e961ae7SSagi Grimberg }
20149e961ae7SSagi Grimberg 
20159e961ae7SSagi Grimberg static int
201638a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
201738a2d0d4SChristoph Hellwig 		struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
20189e961ae7SSagi Grimberg {
201938a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
202038a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
202138a2d0d4SChristoph Hellwig 	u8 port_num = conn->cm_id->port_num;
202238a2d0d4SChristoph Hellwig 	u64 addr;
202338a2d0d4SChristoph Hellwig 	u32 rkey, offset;
20249e961ae7SSagi Grimberg 	int ret;
20259e961ae7SSagi Grimberg 
202666f53e6fSBharat Potnuri 	if (cmd->ctx_init_done)
202766f53e6fSBharat Potnuri 		goto rdma_ctx_post;
202866f53e6fSBharat Potnuri 
202938a2d0d4SChristoph Hellwig 	if (dir == DMA_FROM_DEVICE) {
203038a2d0d4SChristoph Hellwig 		addr = cmd->write_va;
203138a2d0d4SChristoph Hellwig 		rkey = cmd->write_stag;
203238a2d0d4SChristoph Hellwig 		offset = cmd->iscsi_cmd->write_data_done;
203338a2d0d4SChristoph Hellwig 	} else {
203438a2d0d4SChristoph Hellwig 		addr = cmd->read_va;
203538a2d0d4SChristoph Hellwig 		rkey = cmd->read_stag;
203638a2d0d4SChristoph Hellwig 		offset = 0;
203738a2d0d4SChristoph Hellwig 	}
203838a2d0d4SChristoph Hellwig 
203938a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
204038a2d0d4SChristoph Hellwig 		struct ib_sig_attrs sig_attrs;
204138a2d0d4SChristoph Hellwig 
20429e961ae7SSagi Grimberg 		ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
20439e961ae7SSagi Grimberg 		if (ret)
2044e3d7e4c3SSagi Grimberg 			return ret;
2045e3d7e4c3SSagi Grimberg 
204638a2d0d4SChristoph Hellwig 		WARN_ON_ONCE(offset);
204738a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
204838a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
204938a2d0d4SChristoph Hellwig 				se_cmd->t_prot_sg, se_cmd->t_prot_nents,
205038a2d0d4SChristoph Hellwig 				&sig_attrs, addr, rkey, dir);
2051570db170SSagi Grimberg 	} else {
205238a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
205338a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
205438a2d0d4SChristoph Hellwig 				offset, addr, rkey, dir);
205538a2d0d4SChristoph Hellwig 	}
205666f53e6fSBharat Potnuri 
205738a2d0d4SChristoph Hellwig 	if (ret < 0) {
205838a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
205938a2d0d4SChristoph Hellwig 		return ret;
20609e961ae7SSagi Grimberg 	}
20619e961ae7SSagi Grimberg 
206266f53e6fSBharat Potnuri 	cmd->ctx_init_done = true;
206366f53e6fSBharat Potnuri 
206466f53e6fSBharat Potnuri rdma_ctx_post:
206538a2d0d4SChristoph Hellwig 	ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
206638a2d0d4SChristoph Hellwig 	if (ret < 0)
206738a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to post RDMA res\n", cmd);
206859464ef4SVu Pham 	return ret;
206959464ef4SVu Pham }
207059464ef4SVu Pham 
207159464ef4SVu Pham static int
207290ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
207390ecc6e2SVu Pham {
207490ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
207559464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
20766700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
207738a2d0d4SChristoph Hellwig 	struct ib_cqe *cqe = NULL;
207838a2d0d4SChristoph Hellwig 	struct ib_send_wr *chain_wr = NULL;
207990ecc6e2SVu Pham 	int rc;
208090ecc6e2SVu Pham 
208124f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
208290ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
20834c22e07fSSagi Grimberg 
208438a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd)) {
208538a2d0d4SChristoph Hellwig 		isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
208638a2d0d4SChristoph Hellwig 		cqe = &isert_cmd->tx_desc.tx_cqe;
208738a2d0d4SChristoph Hellwig 	} else {
2088b8d26b3bSNicholas Bellinger 		/*
2089b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2090b8d26b3bSNicholas Bellinger 		 */
20919e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
20929e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
209304d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2094b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2095b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
209695b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
209768a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
20984366b19cSJenny Derzhavetz 
20994366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
21004366b19cSJenny Derzhavetz 		if (rc) {
21014366b19cSJenny Derzhavetz 			isert_err("ib_post_recv failed with %d\n", rc);
21024366b19cSJenny Derzhavetz 			return rc;
21034366b19cSJenny Derzhavetz 		}
210438a2d0d4SChristoph Hellwig 
210538a2d0d4SChristoph Hellwig 		chain_wr = &isert_cmd->tx_desc.send_wr;
21069e961ae7SSagi Grimberg 	}
2107b8d26b3bSNicholas Bellinger 
2108555a65f6SNicholas Bellinger 	rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2109555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n",
2110555a65f6SNicholas Bellinger 		  isert_cmd, rc);
2111555a65f6SNicholas Bellinger 	return rc;
2112b8d26b3bSNicholas Bellinger }
2113b8d26b3bSNicholas Bellinger 
2114b8d26b3bSNicholas Bellinger static int
2115b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2116b8d26b3bSNicholas Bellinger {
2117d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2118555a65f6SNicholas Bellinger 	int ret;
2119b8d26b3bSNicholas Bellinger 
212024f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
212138a2d0d4SChristoph Hellwig 		 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
2122b8d26b3bSNicholas Bellinger 
212338a2d0d4SChristoph Hellwig 	isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2124555a65f6SNicholas Bellinger 	ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context,
212538a2d0d4SChristoph Hellwig 				     &isert_cmd->tx_desc.tx_cqe, NULL);
2126bdf20e72SSagi Grimberg 
2127555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n",
2128555a65f6SNicholas Bellinger 		 isert_cmd, ret);
2129555a65f6SNicholas Bellinger 	return ret;
2130b8d26b3bSNicholas Bellinger }
2131b8d26b3bSNicholas Bellinger 
2132b8d26b3bSNicholas Bellinger static int
2133b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2134b8d26b3bSNicholas Bellinger {
2135a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2136a4c15cd9SJenny Derzhavetz 	int ret = 0;
2137b8d26b3bSNicholas Bellinger 
2138b8d26b3bSNicholas Bellinger 	switch (state) {
2139a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2140a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2141a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2142a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2143a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2144a4c15cd9SJenny Derzhavetz 		break;
2145b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2146b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2147b8d26b3bSNicholas Bellinger 		break;
2148b8d26b3bSNicholas Bellinger 	default:
214924f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2150b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2151b8d26b3bSNicholas Bellinger 		break;
2152b8d26b3bSNicholas Bellinger 	}
2153b8d26b3bSNicholas Bellinger 
2154b8d26b3bSNicholas Bellinger 	return ret;
2155b8d26b3bSNicholas Bellinger }
2156b8d26b3bSNicholas Bellinger 
2157b8d26b3bSNicholas Bellinger static int
2158b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2159b8d26b3bSNicholas Bellinger {
2160991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2161b8d26b3bSNicholas Bellinger 	int ret;
2162b8d26b3bSNicholas Bellinger 
2163b8d26b3bSNicholas Bellinger 	switch (state) {
2164b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2165b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2166991bb764SSagi Grimberg 		if (!ret)
2167991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2168b8d26b3bSNicholas Bellinger 		break;
2169b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2170b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2171b8d26b3bSNicholas Bellinger 		break;
2172b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2173b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2174b8d26b3bSNicholas Bellinger 		break;
2175b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2176b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2177b8d26b3bSNicholas Bellinger 		break;
2178adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2179adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2180adb54c29SNicholas Bellinger 		break;
2181b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2182b8d26b3bSNicholas Bellinger 		/*
2183b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2184b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2185b8d26b3bSNicholas Bellinger 		 */
2186b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2187b8d26b3bSNicholas Bellinger 		break;
2188b8d26b3bSNicholas Bellinger 	default:
218924f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2190b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2191b8d26b3bSNicholas Bellinger 		break;
2192b8d26b3bSNicholas Bellinger 	}
2193b8d26b3bSNicholas Bellinger 
2194b8d26b3bSNicholas Bellinger 	return ret;
2195b8d26b3bSNicholas Bellinger }
2196b8d26b3bSNicholas Bellinger 
2197ca6c1d82SSagi Grimberg struct rdma_cm_id *
2198ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2199ca6c1d82SSagi Grimberg {
2200ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2201ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2202ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2203ca6c1d82SSagi Grimberg 	int ret;
2204ca6c1d82SSagi Grimberg 
2205ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
220624f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2207ca6c1d82SSagi Grimberg 
2208fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
2209ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2210ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
221124f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2212ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2213ca6c1d82SSagi Grimberg 		goto out;
2214ca6c1d82SSagi Grimberg 	}
221524f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2216ca6c1d82SSagi Grimberg 
2217ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2218ca6c1d82SSagi Grimberg 	if (ret) {
221924f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2220ca6c1d82SSagi Grimberg 		goto out_id;
2221ca6c1d82SSagi Grimberg 	}
2222ca6c1d82SSagi Grimberg 
2223992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
2224ca6c1d82SSagi Grimberg 	if (ret) {
222524f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2226ca6c1d82SSagi Grimberg 		goto out_id;
2227ca6c1d82SSagi Grimberg 	}
2228ca6c1d82SSagi Grimberg 
2229ca6c1d82SSagi Grimberg 	return id;
2230ca6c1d82SSagi Grimberg out_id:
2231ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
2232ca6c1d82SSagi Grimberg out:
2233ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
2234ca6c1d82SSagi Grimberg }
2235ca6c1d82SSagi Grimberg 
2236b8d26b3bSNicholas Bellinger static int
2237b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
223813a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
2239b8d26b3bSNicholas Bellinger {
2240b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2241b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2242b8d26b3bSNicholas Bellinger 	int ret;
2243b8d26b3bSNicholas Bellinger 
2244b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
224593b80f29SLeon Romanovsky 	if (!isert_np)
2246b8d26b3bSNicholas Bellinger 		return -ENOMEM;
224793b80f29SLeon Romanovsky 
2248ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
2249ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
2250bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
2251bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
2252ca6c1d82SSagi Grimberg 	isert_np->np = np;
2253b8d26b3bSNicholas Bellinger 
2254b8d26b3bSNicholas Bellinger 	/*
2255b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2256b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2257b8d26b3bSNicholas Bellinger 	 */
2258b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
225913a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
2260b8d26b3bSNicholas Bellinger 
2261ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
2262b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2263b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2264b8d26b3bSNicholas Bellinger 		goto out;
2265b8d26b3bSNicholas Bellinger 	}
2266b8d26b3bSNicholas Bellinger 
2267ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
2268b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2269b8d26b3bSNicholas Bellinger 
2270b8d26b3bSNicholas Bellinger 	return 0;
2271b8d26b3bSNicholas Bellinger 
2272b8d26b3bSNicholas Bellinger out:
2273b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2274ca6c1d82SSagi Grimberg 
2275b8d26b3bSNicholas Bellinger 	return ret;
2276b8d26b3bSNicholas Bellinger }
2277b8d26b3bSNicholas Bellinger 
2278b8d26b3bSNicholas Bellinger static int
2279b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2280b8d26b3bSNicholas Bellinger {
2281dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2282b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2283b8d26b3bSNicholas Bellinger 	int ret;
228413bce482SJenny Derzhavetz 	struct iser_cm_hdr rsp_hdr;
2285b8d26b3bSNicholas Bellinger 
2286b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2287b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2288b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2289b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2290b8d26b3bSNicholas Bellinger 
229113bce482SJenny Derzhavetz 	memset(&rsp_hdr, 0, sizeof(rsp_hdr));
2292422bd0acSJenny Derzhavetz 	rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2293422bd0acSJenny Derzhavetz 	if (!isert_conn->snd_w_inv)
2294422bd0acSJenny Derzhavetz 		rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
229513bce482SJenny Derzhavetz 	cp.private_data = (void *)&rsp_hdr;
229613bce482SJenny Derzhavetz 	cp.private_data_len = sizeof(rsp_hdr);
229713bce482SJenny Derzhavetz 
2298b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2299b8d26b3bSNicholas Bellinger 	if (ret) {
230024f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
2301b8d26b3bSNicholas Bellinger 		return ret;
2302b8d26b3bSNicholas Bellinger 	}
2303b8d26b3bSNicholas Bellinger 
2304b8d26b3bSNicholas Bellinger 	return 0;
2305b8d26b3bSNicholas Bellinger }
2306b8d26b3bSNicholas Bellinger 
2307b8d26b3bSNicholas Bellinger static int
2308b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2309b8d26b3bSNicholas Bellinger {
23106700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2311b8d26b3bSNicholas Bellinger 	int ret;
2312b8d26b3bSNicholas Bellinger 
231324f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
23142371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
23152371e5daSSagi Grimberg 	if (ret) {
231624f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
23172371e5daSSagi Grimberg 			  isert_conn);
23182371e5daSSagi Grimberg 		return ret;
23192371e5daSSagi Grimberg 	}
23202371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
23212371e5daSSagi Grimberg 
23226faaa85fSNicholas Bellinger 	/*
23236faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
23246faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
23256faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
23266faaa85fSNicholas Bellinger 	 * into a NOP.
23276faaa85fSNicholas Bellinger 	 */
23286faaa85fSNicholas Bellinger 	if (!login->first_request)
23296faaa85fSNicholas Bellinger 		return 0;
2330b8d26b3bSNicholas Bellinger 
23312371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
23322371e5daSSagi Grimberg 
2333dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
2334dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
2335b8d26b3bSNicholas Bellinger 	if (ret)
2336b8d26b3bSNicholas Bellinger 		return ret;
2337b8d26b3bSNicholas Bellinger 
233824f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
23392371e5daSSagi Grimberg 
2340b8d26b3bSNicholas Bellinger 	return 0;
2341b8d26b3bSNicholas Bellinger }
2342b8d26b3bSNicholas Bellinger 
2343b8d26b3bSNicholas Bellinger static void
2344b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2345b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2346b8d26b3bSNicholas Bellinger {
2347dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2348b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2349b8d26b3bSNicholas Bellinger 
2350b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2351b8d26b3bSNicholas Bellinger 
2352dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
2353dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
2354b8d26b3bSNicholas Bellinger }
2355b8d26b3bSNicholas Bellinger 
2356b8d26b3bSNicholas Bellinger static int
2357b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2358b8d26b3bSNicholas Bellinger {
23596700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2360b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2361c6b8e918SSagi Grimberg 	int ret;
2362b8d26b3bSNicholas Bellinger 
2363b8d26b3bSNicholas Bellinger accept_wait:
2364ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
2365c6b8e918SSagi Grimberg 	if (ret)
2366b8d26b3bSNicholas Bellinger 		return -ENODEV;
2367b8d26b3bSNicholas Bellinger 
2368b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2369e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
2370b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
237111378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
2372e346ab34SSagi Grimberg 			 np->np_thread_state);
2373e346ab34SSagi Grimberg 		/**
2374e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
2375e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
2376e346ab34SSagi Grimberg 		 **/
2377b8d26b3bSNicholas Bellinger 		return -ENODEV;
2378b8d26b3bSNicholas Bellinger 	}
2379b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2380b8d26b3bSNicholas Bellinger 
2381ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2382bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
2383ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
2384b8d26b3bSNicholas Bellinger 		goto accept_wait;
2385b8d26b3bSNicholas Bellinger 	}
2386bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
2387bd379220SJenny Derzhavetz 			struct isert_conn, node);
2388bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
2389ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2390b8d26b3bSNicholas Bellinger 
2391b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2392b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2393aea92980SJenny Derzhavetz 	isert_conn->state = ISER_CONN_BOUND;
2394b8d26b3bSNicholas Bellinger 
2395b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2396b8d26b3bSNicholas Bellinger 
239724f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
23982371e5daSSagi Grimberg 
2399b8d26b3bSNicholas Bellinger 	return 0;
2400b8d26b3bSNicholas Bellinger }
2401b8d26b3bSNicholas Bellinger 
2402b8d26b3bSNicholas Bellinger static void
2403b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2404b8d26b3bSNicholas Bellinger {
24056700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2406268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
2407b8d26b3bSNicholas Bellinger 
2408ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
2409ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
2410b8d26b3bSNicholas Bellinger 
2411268e6811SSagi Grimberg 	/*
2412268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
2413268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
2414268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
2415268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
2416bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
2417268e6811SSagi Grimberg 	 */
2418ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2419bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
2420bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
2421268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
2422bd379220SJenny Derzhavetz 					 &isert_np->pending,
2423bd379220SJenny Derzhavetz 					 node) {
2424bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
2425bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
2426bd379220SJenny Derzhavetz 			isert_connect_release(isert_conn);
2427bd379220SJenny Derzhavetz 		}
2428bd379220SJenny Derzhavetz 	}
2429bd379220SJenny Derzhavetz 
2430bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
2431bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
2432bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
2433bd379220SJenny Derzhavetz 					 &isert_np->accepted,
2434bd379220SJenny Derzhavetz 					 node) {
243524f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
2436268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
2437268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
2438268e6811SSagi Grimberg 		}
2439268e6811SSagi Grimberg 	}
2440ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2441268e6811SSagi Grimberg 
2442b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2443b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2444b8d26b3bSNicholas Bellinger }
2445b8d26b3bSNicholas Bellinger 
2446b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
2447b02efbfcSSagi Grimberg {
2448b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
2449b02efbfcSSagi Grimberg 						     struct isert_conn,
2450b02efbfcSSagi Grimberg 						     release_work);
2451b02efbfcSSagi Grimberg 
245224f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
2453b02efbfcSSagi Grimberg 
2454dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2455b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
2456dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2457b02efbfcSSagi Grimberg 
245824f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
2459b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
2460b02efbfcSSagi Grimberg }
2461b02efbfcSSagi Grimberg 
2462bdf20e72SSagi Grimberg static void
2463991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
2464991bb764SSagi Grimberg {
2465991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
2466991bb764SSagi Grimberg 
24674c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
24684c22e07fSSagi Grimberg 
2469991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
247024f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
2471991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
2472991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
2473991bb764SSagi Grimberg 	}
2474991bb764SSagi Grimberg }
2475991bb764SSagi Grimberg 
247676261adaSBart Van Assche static void
247776261adaSBart Van Assche isert_wait4cmds(struct iscsi_conn *conn)
247876261adaSBart Van Assche {
247976261adaSBart Van Assche 	isert_info("iscsi_conn %p\n", conn);
248076261adaSBart Van Assche 
248176261adaSBart Van Assche 	if (conn->sess) {
248276261adaSBart Van Assche 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
248376261adaSBart Van Assche 		target_wait_for_sess_cmds(conn->sess->se_sess);
248476261adaSBart Van Assche 	}
248576261adaSBart Van Assche }
248676261adaSBart Van Assche 
24873e03c4b0SJenny Derzhavetz /**
24883e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
24893e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
24903e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
24913e03c4b0SJenny Derzhavetz  *
24923e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
24933e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
24943e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
24953e03c4b0SJenny Derzhavetz  */
24963e03c4b0SJenny Derzhavetz static void
24973e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
24983e03c4b0SJenny Derzhavetz {
24993e03c4b0SJenny Derzhavetz 	struct iscsi_cmd *cmd, *tmp;
25003e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
25013e03c4b0SJenny Derzhavetz 
25023e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
25033e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
25043e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
25053e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
25063e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
25073e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
25083e03c4b0SJenny Derzhavetz 	}
25093e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
25103e03c4b0SJenny Derzhavetz 
25113e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
25123e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
25133e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
25143e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
25153e03c4b0SJenny Derzhavetz 
25163e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
25173e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
25183e03c4b0SJenny Derzhavetz 		}
25193e03c4b0SJenny Derzhavetz 	}
25203e03c4b0SJenny Derzhavetz }
25213e03c4b0SJenny Derzhavetz 
2522defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
2523b8d26b3bSNicholas Bellinger {
2524b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2525b8d26b3bSNicholas Bellinger 
25264c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
2527b8d26b3bSNicholas Bellinger 
2528dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2529954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2530dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2531b2cb9649SNicholas Bellinger 
2532572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
25333e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
253476261adaSBart Van Assche 	isert_wait4cmds(conn);
2535991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
2536954f2372SSagi Grimberg 
2537b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
2538defd8848SNicholas Bellinger }
2539defd8848SNicholas Bellinger 
2540defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2541defd8848SNicholas Bellinger {
2542defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2543b8d26b3bSNicholas Bellinger 
2544572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
2545b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2546b8d26b3bSNicholas Bellinger }
2547b8d26b3bSNicholas Bellinger 
2548187e84f7SVarun Prakash static void isert_get_rx_pdu(struct iscsi_conn *conn)
2549187e84f7SVarun Prakash {
2550187e84f7SVarun Prakash 	struct completion comp;
2551187e84f7SVarun Prakash 
2552187e84f7SVarun Prakash 	init_completion(&comp);
2553187e84f7SVarun Prakash 
2554187e84f7SVarun Prakash 	wait_for_completion_interruptible(&comp);
2555187e84f7SVarun Prakash }
2556187e84f7SVarun Prakash 
2557b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2558b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2559b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2560bd027d85SNicholas Bellinger 	.rdma_shutdown		= true,
2561d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2562b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2563b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2564b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2565b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2566defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2567b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2568b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2569b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2570b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2571b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2572b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2573b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2574b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2575131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
2576187e84f7SVarun Prakash 	.iscsit_get_rx_pdu	= isert_get_rx_pdu,
2577e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
2578b8d26b3bSNicholas Bellinger };
2579b8d26b3bSNicholas Bellinger 
2580b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2581b8d26b3bSNicholas Bellinger {
2582b8d26b3bSNicholas Bellinger 	int ret;
2583b8d26b3bSNicholas Bellinger 
2584631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
2585631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
2586b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
258724f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
25886f0fae3dSSagi Grimberg 		return -ENOMEM;
2589b8d26b3bSNicholas Bellinger 	}
2590b8d26b3bSNicholas Bellinger 
2591b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2592b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
2593b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
259424f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
2595b02efbfcSSagi Grimberg 		ret = -ENOMEM;
2596b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
2597b02efbfcSSagi Grimberg 	}
2598b02efbfcSSagi Grimberg 
2599b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
260024f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
2601b02efbfcSSagi Grimberg 
2602b8d26b3bSNicholas Bellinger 	return 0;
2603b8d26b3bSNicholas Bellinger 
2604b02efbfcSSagi Grimberg destroy_comp_wq:
2605b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
26066f0fae3dSSagi Grimberg 
2607b8d26b3bSNicholas Bellinger 	return ret;
2608b8d26b3bSNicholas Bellinger }
2609b8d26b3bSNicholas Bellinger 
2610b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2611b8d26b3bSNicholas Bellinger {
2612f5ebec96SSagi Grimberg 	flush_scheduled_work();
2613b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
2614b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2615b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
26164c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
2617b8d26b3bSNicholas Bellinger }
2618b8d26b3bSNicholas Bellinger 
2619b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2620b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2621b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2622b8d26b3bSNicholas Bellinger 
2623b8d26b3bSNicholas Bellinger module_init(isert_init);
2624b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2625