xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision c97119b6d3135fe173751cf8bf53136f322bd506)
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;
66788c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
66888c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
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));
687d2271bd0SBart Van Assche 		/* fall through */
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 	 */
1143*c97119b6SMax Gurtovoy 	return isert_post_recv(isert_conn, rx_desc);
1144b8d26b3bSNicholas Bellinger }
1145b8d26b3bSNicholas Bellinger 
1146b8d26b3bSNicholas Bellinger static int
1147778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1148d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1149d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1150778de368SNicholas Bellinger {
1151778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1152778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1153778de368SNicholas Bellinger 	int rc;
1154778de368SNicholas Bellinger 
1155778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1156778de368SNicholas Bellinger 	if (rc < 0)
1157778de368SNicholas Bellinger 		return rc;
1158778de368SNicholas Bellinger 	/*
1159778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1160778de368SNicholas Bellinger 	 */
1161778de368SNicholas Bellinger 
1162778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1163778de368SNicholas Bellinger }
1164778de368SNicholas Bellinger 
1165778de368SNicholas Bellinger static int
1166adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1167d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1168d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1169adb54c29SNicholas Bellinger {
1170adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1171adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1172adb54c29SNicholas Bellinger 	int rc;
1173b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1174adb54c29SNicholas Bellinger 
1175adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1176adb54c29SNicholas Bellinger 	if (rc < 0)
1177adb54c29SNicholas Bellinger 		return rc;
1178adb54c29SNicholas Bellinger 
1179b44a2b67SSagi Grimberg 	if (payload_length) {
1180adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
118193b80f29SLeon Romanovsky 		if (!text_in)
1182adb54c29SNicholas Bellinger 			return -ENOMEM;
1183adb54c29SNicholas Bellinger 	}
1184adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1185adb54c29SNicholas Bellinger 
1186adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1187adb54c29SNicholas Bellinger 
1188adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1189adb54c29SNicholas Bellinger }
1190adb54c29SNicholas Bellinger 
1191adb54c29SNicholas Bellinger static int
1192b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1193b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1194b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1195b8d26b3bSNicholas Bellinger {
1196b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1197b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1198b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1199b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1200b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1201b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1202b8d26b3bSNicholas Bellinger 
1203fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1204ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
120524f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1206ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1207ca40d24eSNicholas Bellinger 		return 0;
1208ca40d24eSNicholas Bellinger 	}
1209ca40d24eSNicholas Bellinger 
1210b8d26b3bSNicholas Bellinger 	switch (opcode) {
1211b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
12124366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1213b8d26b3bSNicholas Bellinger 		if (!cmd)
1214b8d26b3bSNicholas Bellinger 			break;
1215b8d26b3bSNicholas Bellinger 
1216d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1217b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1218b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1219b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1220b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1221422bd0acSJenny Derzhavetz 		isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
1222b8d26b3bSNicholas Bellinger 
1223d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1224b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1225b8d26b3bSNicholas Bellinger 		break;
1226b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
12274366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1228b8d26b3bSNicholas Bellinger 		if (!cmd)
1229b8d26b3bSNicholas Bellinger 			break;
1230b8d26b3bSNicholas Bellinger 
1231d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1232d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1233778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1234b8d26b3bSNicholas Bellinger 		break;
1235b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1236b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1237b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1238b8d26b3bSNicholas Bellinger 		break;
1239b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
12404366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1241b8d26b3bSNicholas Bellinger 		if (!cmd)
1242b8d26b3bSNicholas Bellinger 			break;
1243b8d26b3bSNicholas Bellinger 
1244b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1245b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1246b8d26b3bSNicholas Bellinger 		break;
1247b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
12484366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1249b8d26b3bSNicholas Bellinger 		if (!cmd)
1250b8d26b3bSNicholas Bellinger 			break;
1251b8d26b3bSNicholas Bellinger 
1252b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1253b8d26b3bSNicholas Bellinger 		break;
1254adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
12554366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1256e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
12574366b19cSJenny Derzhavetz 		else
12584366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
12594366b19cSJenny Derzhavetz 
1260e4f4e801SSagi Grimberg 		if (!cmd)
1261e4f4e801SSagi Grimberg 			break;
1262adb54c29SNicholas Bellinger 
1263d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1264d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1265adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1266adb54c29SNicholas Bellinger 		break;
1267b8d26b3bSNicholas Bellinger 	default:
126824f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1269b8d26b3bSNicholas Bellinger 		dump_stack();
1270b8d26b3bSNicholas Bellinger 		break;
1271b8d26b3bSNicholas Bellinger 	}
1272b8d26b3bSNicholas Bellinger 
1273b8d26b3bSNicholas Bellinger 	return ret;
1274b8d26b3bSNicholas Bellinger }
1275b8d26b3bSNicholas Bellinger 
1276b8d26b3bSNicholas Bellinger static void
12779679cc51SChristoph Hellwig isert_print_wc(struct ib_wc *wc, const char *type)
1278b8d26b3bSNicholas Bellinger {
12799679cc51SChristoph Hellwig 	if (wc->status != IB_WC_WR_FLUSH_ERR)
12809679cc51SChristoph Hellwig 		isert_err("%s failure: %s (%d) vend_err %x\n", type,
12819679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status,
12829679cc51SChristoph Hellwig 			  wc->vendor_err);
12839679cc51SChristoph Hellwig 	else
12849679cc51SChristoph Hellwig 		isert_dbg("%s failure: %s (%d)\n", type,
12859679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status);
12869679cc51SChristoph Hellwig }
12879679cc51SChristoph Hellwig 
12889679cc51SChristoph Hellwig static void
12899679cc51SChristoph Hellwig isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
12909679cc51SChristoph Hellwig {
12919679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
12929679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
12939679cc51SChristoph Hellwig 	struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
12949679cc51SChristoph Hellwig 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1295d3cf81f9SSagi Grimberg 	struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
1296b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1297b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1298b8d26b3bSNicholas Bellinger 
12999679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
13009679cc51SChristoph Hellwig 		isert_print_wc(wc, "recv");
13019679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
13029679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
13039679cc51SChristoph Hellwig 		return;
13049679cc51SChristoph Hellwig 	}
13059679cc51SChristoph Hellwig 
13067a56dc88SSagi Grimberg 	rx_desc->in_use = true;
13077a56dc88SSagi Grimberg 
13089679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
13099679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
13109679cc51SChristoph Hellwig 
13119679cc51SChristoph Hellwig 	isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
13129679cc51SChristoph Hellwig 		 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
13139679cc51SChristoph Hellwig 		 (int)(wc->byte_len - ISER_HEADERS_LEN));
13149679cc51SChristoph Hellwig 
1315d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1316b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1317d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1318d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1319d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
13204c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1321b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1322b8d26b3bSNicholas Bellinger 		}
1323d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1324d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1325d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
13264c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1327b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1328b8d26b3bSNicholas Bellinger 		}
1329b8d26b3bSNicholas Bellinger 
133024f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1331b8d26b3bSNicholas Bellinger 		break;
1332b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
133324f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1334b8d26b3bSNicholas Bellinger 		break;
1335b8d26b3bSNicholas Bellinger 	default:
1336d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1337b8d26b3bSNicholas Bellinger 		break;
1338b8d26b3bSNicholas Bellinger 	}
1339b8d26b3bSNicholas Bellinger 
13406c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1341b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
13429679cc51SChristoph Hellwig 
13439679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
13449679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1345b8d26b3bSNicholas Bellinger }
1346b8d26b3bSNicholas Bellinger 
1347b8d26b3bSNicholas Bellinger static void
13489679cc51SChristoph Hellwig isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1349b8d26b3bSNicholas Bellinger {
13509679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1351fce50a2fSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->device->ib_device;
1352b8d26b3bSNicholas Bellinger 
13539679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
13549679cc51SChristoph Hellwig 		isert_print_wc(wc, "login recv");
13559679cc51SChristoph Hellwig 		return;
1356b8d26b3bSNicholas Bellinger 	}
1357b8d26b3bSNicholas Bellinger 
13589679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
13599679cc51SChristoph Hellwig 			ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1360b8d26b3bSNicholas Bellinger 
13619679cc51SChristoph Hellwig 	isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
1362b8d26b3bSNicholas Bellinger 
13632371e5daSSagi Grimberg 	if (isert_conn->conn) {
13642371e5daSSagi Grimberg 		struct iscsi_login *login = isert_conn->conn->conn_login;
13652371e5daSSagi Grimberg 
13662371e5daSSagi Grimberg 		if (login && !login->first_request)
13672371e5daSSagi Grimberg 			isert_rx_login_req(isert_conn);
13682371e5daSSagi Grimberg 	}
13699679cc51SChristoph Hellwig 
1370dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
13712371e5daSSagi Grimberg 	complete(&isert_conn->login_req_comp);
1372dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
1373b8d26b3bSNicholas Bellinger 
13749679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
13759679cc51SChristoph Hellwig 				ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1376b8d26b3bSNicholas Bellinger }
1377b8d26b3bSNicholas Bellinger 
1378e3d7e4c3SSagi Grimberg static void
137938a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
1380e3d7e4c3SSagi Grimberg {
138138a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
138238a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
1383e3d7e4c3SSagi Grimberg 
138438a2d0d4SChristoph Hellwig 	if (!cmd->rw.nr_ops)
138538a2d0d4SChristoph Hellwig 		return;
138638a2d0d4SChristoph Hellwig 
138738a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
138838a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
138938a2d0d4SChristoph Hellwig 				conn->cm_id->port_num, se_cmd->t_data_sg,
139038a2d0d4SChristoph Hellwig 				se_cmd->t_data_nents, se_cmd->t_prot_sg,
139138a2d0d4SChristoph Hellwig 				se_cmd->t_prot_nents, dir);
139238a2d0d4SChristoph Hellwig 	} else {
139338a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
139438a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
1395e3d7e4c3SSagi Grimberg 	}
1396e3d7e4c3SSagi Grimberg 
139738a2d0d4SChristoph Hellwig 	cmd->rw.nr_ops = 0;
139859464ef4SVu Pham }
139959464ef4SVu Pham 
140059464ef4SVu Pham static void
140103e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1402b8d26b3bSNicholas Bellinger {
1403d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1404b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1405186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1406e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1407b8d26b3bSNicholas Bellinger 
14084c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1409b8d26b3bSNicholas Bellinger 
1410b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1411b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1412b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1413b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14145159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1415b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1416b8d26b3bSNicholas Bellinger 
141703e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1418b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
141903e7848aSNicholas Bellinger 			/*
142003e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
142103e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
142203e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
142303e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
142403e7848aSNicholas Bellinger 			 */
142503e7848aSNicholas Bellinger 			if (comp_err &&
142603e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
142703e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
142803e7848aSNicholas Bellinger 
1429afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
143003e7848aSNicholas Bellinger 			}
143103e7848aSNicholas Bellinger 		}
1432b8d26b3bSNicholas Bellinger 
143338a2d0d4SChristoph Hellwig 		isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1434186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1435186a9647SNicholas Bellinger 		break;
1436b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1437186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1438186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14395159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1440186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1441186a9647SNicholas Bellinger 
1442b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1443b8d26b3bSNicholas Bellinger 		break;
1444b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1445b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1446adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1447e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1448e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1449e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1450e4f4e801SSagi Grimberg 			break;
1451e4f4e801SSagi Grimberg 
1452b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1453b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14545159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1455b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1456b8d26b3bSNicholas Bellinger 
1457b8d26b3bSNicholas Bellinger 		/*
1458b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1459b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1460b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1461b8d26b3bSNicholas Bellinger 		 */
1462b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
146311378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
14643df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1465b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1466b8d26b3bSNicholas Bellinger 			break;
1467b8d26b3bSNicholas Bellinger 		}
1468d2271bd0SBart Van Assche 		/* fall through */
1469b8d26b3bSNicholas Bellinger 	default:
1470d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1471b8d26b3bSNicholas Bellinger 		break;
1472b8d26b3bSNicholas Bellinger 	}
1473b8d26b3bSNicholas Bellinger }
1474b8d26b3bSNicholas Bellinger 
1475b8d26b3bSNicholas Bellinger static void
1476b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1477b8d26b3bSNicholas Bellinger {
1478b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
14794c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1480b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1481b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1482b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1483b8d26b3bSNicholas Bellinger 	}
1484b8d26b3bSNicholas Bellinger }
1485b8d26b3bSNicholas Bellinger 
1486b8d26b3bSNicholas Bellinger static void
1487b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
148803e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1489b8d26b3bSNicholas Bellinger {
1490dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
14914c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1492dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1493dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1494dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1495b8d26b3bSNicholas Bellinger 	}
1496b8d26b3bSNicholas Bellinger 
1497b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
149803e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1499b8d26b3bSNicholas Bellinger }
1500b8d26b3bSNicholas Bellinger 
150196b7973eSSagi Grimberg static int
150296b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1503f93f3a70SSagi Grimberg {
15049e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
150596b7973eSSagi Grimberg 	int ret;
1506f93f3a70SSagi Grimberg 
150796b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
15089e961ae7SSagi Grimberg 	if (ret) {
150924f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
15109e961ae7SSagi Grimberg 		goto fail_mr_status;
15119e961ae7SSagi Grimberg 	}
151296b7973eSSagi Grimberg 
15139e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
151496b7973eSSagi Grimberg 		u64 sec_offset_err;
15159e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
15169e961ae7SSagi Grimberg 
15179e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
15189e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
15199e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
15209e961ae7SSagi Grimberg 			break;
15219e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
15229e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
15239e961ae7SSagi Grimberg 			break;
15249e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
15259e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
15269e961ae7SSagi Grimberg 			break;
15279e961ae7SSagi Grimberg 		}
152896b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
152996b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
153096b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
15319e961ae7SSagi Grimberg 
15324c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
15339e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
15349e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
15359e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
15369e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
15379e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
15389e961ae7SSagi Grimberg 		ret = 1;
15399e961ae7SSagi Grimberg 	}
15409e961ae7SSagi Grimberg 
15419e961ae7SSagi Grimberg fail_mr_status:
154296b7973eSSagi Grimberg 	return ret;
154396b7973eSSagi Grimberg }
154496b7973eSSagi Grimberg 
154596b7973eSSagi Grimberg static void
15469679cc51SChristoph Hellwig isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
154796b7973eSSagi Grimberg {
15489679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1549dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
15509679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1551e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
15529679cc51SChristoph Hellwig 	struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
155396b7973eSSagi Grimberg 	int ret = 0;
155496b7973eSSagi Grimberg 
15559679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
15569679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma write");
15579679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
15589679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
15599679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
15609679cc51SChristoph Hellwig 		return;
15619679cc51SChristoph Hellwig 	}
15629679cc51SChristoph Hellwig 
15639679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
15649679cc51SChristoph Hellwig 
1565e9a53e73SIsrael Rukshin 	ret = isert_check_pi_status(cmd, isert_cmd->rw.reg->mr);
156638a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
156796b7973eSSagi Grimberg 
1568555a65f6SNicholas Bellinger 	if (ret) {
1569555a65f6SNicholas Bellinger 		/*
1570555a65f6SNicholas Bellinger 		 * transport_generic_request_failure() expects to have
1571555a65f6SNicholas Bellinger 		 * plus two references to handle queue-full, so re-add
1572555a65f6SNicholas Bellinger 		 * one here as target-core will have already dropped
1573555a65f6SNicholas Bellinger 		 * it after the first isert_put_datain() callback.
1574555a65f6SNicholas Bellinger 		 */
1575555a65f6SNicholas Bellinger 		kref_get(&cmd->cmd_kref);
1576555a65f6SNicholas Bellinger 		transport_generic_request_failure(cmd, cmd->pi_err);
1577555a65f6SNicholas Bellinger 	} else {
1578555a65f6SNicholas Bellinger 		/*
1579555a65f6SNicholas Bellinger 		 * XXX: isert_put_response() failure is not retried.
1580555a65f6SNicholas Bellinger 		 */
1581555a65f6SNicholas Bellinger 		ret = isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
15829e961ae7SSagi Grimberg 		if (ret)
1583555a65f6SNicholas Bellinger 			pr_warn_ratelimited("isert_put_response() ret: %d\n", ret);
1584555a65f6SNicholas Bellinger 	}
1585f93f3a70SSagi Grimberg }
1586f93f3a70SSagi Grimberg 
1587f93f3a70SSagi Grimberg static void
15889679cc51SChristoph Hellwig isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1589b8d26b3bSNicholas Bellinger {
15909679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
15919679cc51SChristoph Hellwig 	struct isert_device *device = isert_conn->device;
15929679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1593e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
1594d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1595b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
15965bac4b1aSSagi Grimberg 	int ret = 0;
1597b8d26b3bSNicholas Bellinger 
15989679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
15999679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma read");
16009679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16019679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16029679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
16039679cc51SChristoph Hellwig 		return;
16049679cc51SChristoph Hellwig 	}
16059679cc51SChristoph Hellwig 
16069679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
16079679cc51SChristoph Hellwig 
1608b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
160938a2d0d4SChristoph Hellwig 
161038a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd))
1611e9a53e73SIsrael Rukshin 		ret = isert_check_pi_status(se_cmd, isert_cmd->rw.reg->mr);
161238a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
161338a2d0d4SChristoph Hellwig 	cmd->write_data_done = 0;
1614b8d26b3bSNicholas Bellinger 
161524f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1616b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1617b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1618b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1619b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1620b8d26b3bSNicholas Bellinger 
1621555a65f6SNicholas Bellinger 	/*
1622555a65f6SNicholas Bellinger 	 * transport_generic_request_failure() will drop the extra
1623555a65f6SNicholas Bellinger 	 * se_cmd->cmd_kref reference after T10-PI error, and handle
1624555a65f6SNicholas Bellinger 	 * any non-zero ->queue_status() callback error retries.
1625555a65f6SNicholas Bellinger 	 */
1626555a65f6SNicholas Bellinger 	if (ret)
1627555a65f6SNicholas Bellinger 		transport_generic_request_failure(se_cmd, se_cmd->pi_err);
1628555a65f6SNicholas Bellinger 	else
1629b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1630b8d26b3bSNicholas Bellinger }
1631b8d26b3bSNicholas Bellinger 
1632b8d26b3bSNicholas Bellinger static void
1633b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1634b8d26b3bSNicholas Bellinger {
1635b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1636b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1637b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1638dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1639d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1640b8d26b3bSNicholas Bellinger 
16414c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
16424c22e07fSSagi Grimberg 
1643b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1644b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1645b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1646d2271bd0SBart Van Assche 		/* fall through */
1647d2271bd0SBart Van Assche 	case ISTATE_SEND_REJECT:
1648d2271bd0SBart Van Assche 	case ISTATE_SEND_TEXTRSP:
1649b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
16504c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
16514c22e07fSSagi Grimberg 				     ib_dev, false);
16523df8f68aSNicholas Bellinger 		break;
1653b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1654b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1655b8d26b3bSNicholas Bellinger 		break;
1656b8d26b3bSNicholas Bellinger 	default:
16574c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1658b8d26b3bSNicholas Bellinger 		dump_stack();
1659b8d26b3bSNicholas Bellinger 		break;
1660b8d26b3bSNicholas Bellinger 	}
1661b8d26b3bSNicholas Bellinger }
1662b8d26b3bSNicholas Bellinger 
1663b8d26b3bSNicholas Bellinger static void
16649679cc51SChristoph Hellwig isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
1665b8d26b3bSNicholas Bellinger {
16669679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16679679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
16689679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1669b8d26b3bSNicholas Bellinger 
16709679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16719679cc51SChristoph Hellwig 		isert_print_wc(wc, "login send");
16729679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16739679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16749679cc51SChristoph Hellwig 	}
16759679cc51SChristoph Hellwig 
16769679cc51SChristoph Hellwig 	isert_unmap_tx_desc(tx_desc, ib_dev);
16779679cc51SChristoph Hellwig }
16789679cc51SChristoph Hellwig 
16799679cc51SChristoph Hellwig static void
16809679cc51SChristoph Hellwig isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
16819679cc51SChristoph Hellwig {
16829679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16839679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
16849679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1685e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
16869679cc51SChristoph Hellwig 
16879679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16889679cc51SChristoph Hellwig 		isert_print_wc(wc, "send");
16899679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16909679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16919679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
16929679cc51SChristoph Hellwig 		return;
16939679cc51SChristoph Hellwig 	}
16949679cc51SChristoph Hellwig 
16959679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
16969679cc51SChristoph Hellwig 
16979679cc51SChristoph Hellwig 	switch (isert_cmd->iscsi_cmd->i_state) {
16989679cc51SChristoph Hellwig 	case ISTATE_SEND_TASKMGTRSP:
16999679cc51SChristoph Hellwig 	case ISTATE_SEND_LOGOUTRSP:
17009679cc51SChristoph Hellwig 	case ISTATE_SEND_REJECT:
17019679cc51SChristoph Hellwig 	case ISTATE_SEND_TEXTRSP:
1702b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1703b8d26b3bSNicholas Bellinger 
1704b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1705b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1706b8d26b3bSNicholas Bellinger 		return;
1707b8d26b3bSNicholas Bellinger 	default:
17089679cc51SChristoph Hellwig 		isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
17099679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1710b8d26b3bSNicholas Bellinger 		break;
1711b8d26b3bSNicholas Bellinger 	}
1712b8d26b3bSNicholas Bellinger }
1713b8d26b3bSNicholas Bellinger 
1714b8d26b3bSNicholas Bellinger static int
1715b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1716b8d26b3bSNicholas Bellinger {
1717b8d26b3bSNicholas Bellinger 	int ret;
1718b8d26b3bSNicholas Bellinger 
17194366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
1720*c97119b6SMax Gurtovoy 	if (ret)
17214366b19cSJenny Derzhavetz 		return ret;
17224366b19cSJenny Derzhavetz 
1723e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr, NULL);
1724b8d26b3bSNicholas Bellinger 	if (ret) {
172524f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
1726b8d26b3bSNicholas Bellinger 		return ret;
1727b8d26b3bSNicholas Bellinger 	}
1728b8d26b3bSNicholas Bellinger 	return ret;
1729b8d26b3bSNicholas Bellinger }
1730b8d26b3bSNicholas Bellinger 
1731b8d26b3bSNicholas Bellinger static int
1732b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1733b8d26b3bSNicholas Bellinger {
1734d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
17356700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1736b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1737b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1738b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1739b8d26b3bSNicholas Bellinger 
1740b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1741b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1742b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1743b8d26b3bSNicholas Bellinger 	/*
1744b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1745b8d26b3bSNicholas Bellinger 	 */
1746b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1747b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1748b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1749dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
175067cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1751b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1752dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1753b8d26b3bSNicholas Bellinger 
1754b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1755b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1756b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1757b8d26b3bSNicholas Bellinger 
1758b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1759b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1760dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1761b8d26b3bSNicholas Bellinger 
1762dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1763dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1764b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1765def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1766def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1767b8d26b3bSNicholas Bellinger 
1768dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1769dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1770dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
177134efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1772b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1773b8d26b3bSNicholas Bellinger 	}
1774b8d26b3bSNicholas Bellinger 
177568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1776b8d26b3bSNicholas Bellinger 
17774c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
1778b8d26b3bSNicholas Bellinger 
1779b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1780b8d26b3bSNicholas Bellinger }
1781b8d26b3bSNicholas Bellinger 
1782131e6abcSNicholas Bellinger static void
1783131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1784131e6abcSNicholas Bellinger {
1785131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
17866700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1787131e6abcSNicholas Bellinger 
1788131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
1789131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
1790131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
1791131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1792131e6abcSNicholas Bellinger 
1793131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
1794131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
179538a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1796131e6abcSNicholas Bellinger }
1797131e6abcSNicholas Bellinger 
1798e70beee7SNicholas Bellinger static enum target_prot_op
1799e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
1800e70beee7SNicholas Bellinger {
18016700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1802dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1803e70beee7SNicholas Bellinger 
180423a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
180523a548eeSSagi Grimberg 		if (device->pi_capable) {
180624f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
180723a548eeSSagi Grimberg 			isert_conn->pi_support = true;
1808e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
180923a548eeSSagi Grimberg 		}
181023a548eeSSagi Grimberg 	}
181123a548eeSSagi Grimberg 
181224f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
181323a548eeSSagi Grimberg 	isert_conn->pi_support = false;
1814e70beee7SNicholas Bellinger 
1815e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
1816e70beee7SNicholas Bellinger }
1817e70beee7SNicholas Bellinger 
1818b8d26b3bSNicholas Bellinger static int
1819b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1820b8d26b3bSNicholas Bellinger 		bool nopout_response)
1821b8d26b3bSNicholas Bellinger {
1822d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18236700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1824b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1825b8d26b3bSNicholas Bellinger 
1826b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1827b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1828b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1829b8d26b3bSNicholas Bellinger 			       nopout_response);
1830b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
183168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1832b8d26b3bSNicholas Bellinger 
18334c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
1834b8d26b3bSNicholas Bellinger 
1835b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1836b8d26b3bSNicholas Bellinger }
1837b8d26b3bSNicholas Bellinger 
1838b8d26b3bSNicholas Bellinger static int
1839b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1840b8d26b3bSNicholas Bellinger {
1841d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18426700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1843b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1844b8d26b3bSNicholas Bellinger 
1845b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1846b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1847b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1848b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
184968a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1850b8d26b3bSNicholas Bellinger 
18514c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
1852b8d26b3bSNicholas Bellinger 
1853b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1854b8d26b3bSNicholas Bellinger }
1855b8d26b3bSNicholas Bellinger 
1856b8d26b3bSNicholas Bellinger static int
1857b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1858b8d26b3bSNicholas Bellinger {
1859d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18606700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1861b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1862b8d26b3bSNicholas Bellinger 
1863b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1864b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1865b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1866b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
186768a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1868b8d26b3bSNicholas Bellinger 
18694c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
1870b8d26b3bSNicholas Bellinger 
1871b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1872b8d26b3bSNicholas Bellinger }
1873b8d26b3bSNicholas Bellinger 
1874b8d26b3bSNicholas Bellinger static int
1875b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1876b8d26b3bSNicholas Bellinger {
1877d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18786700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1879b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1880dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
188167cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
18823df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
18833df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
18843df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1885b8d26b3bSNicholas Bellinger 
1886b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
18873df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
1888b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
18893df8f68aSNicholas Bellinger 
18903df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
1891dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
18923df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
18933df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
1894def4a6ffSAlexey Khoroshilov 	if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1895def4a6ffSAlexey Khoroshilov 		return -ENOMEM;
1896dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1897dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
18983df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
189934efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
19003df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
19013df8f68aSNicholas Bellinger 
190268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1903b8d26b3bSNicholas Bellinger 
19044c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
1905b8d26b3bSNicholas Bellinger 
1906b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1907b8d26b3bSNicholas Bellinger }
1908b8d26b3bSNicholas Bellinger 
1909b8d26b3bSNicholas Bellinger static int
1910adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1911adb54c29SNicholas Bellinger {
1912d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19136700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1914adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1915adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
1916adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1917adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
1918adb54c29SNicholas Bellinger 	int rc;
1919adb54c29SNicholas Bellinger 
1920adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
192122c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
1922adb54c29SNicholas Bellinger 	if (rc < 0)
1923adb54c29SNicholas Bellinger 		return rc;
1924adb54c29SNicholas Bellinger 
1925adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
1926adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1927adb54c29SNicholas Bellinger 
1928adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
1929dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
193067cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1931adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1932adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
1933adb54c29SNicholas Bellinger 
1934adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1935adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1936def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1937def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1938adb54c29SNicholas Bellinger 
1939adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
1940adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1941adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
194234efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1943adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1944adb54c29SNicholas Bellinger 	}
194568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1946adb54c29SNicholas Bellinger 
1947f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
1948adb54c29SNicholas Bellinger 
1949adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1950adb54c29SNicholas Bellinger }
1951adb54c29SNicholas Bellinger 
19523d73cf1aSSagi Grimberg static inline void
1953dae6345aSIsrael Rukshin isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_domain *domain)
19549e961ae7SSagi Grimberg {
195578eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
19563d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
19573d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
19583d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
195978eda2bbSSagi Grimberg 	/*
196078eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
196178eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
196278eda2bbSSagi Grimberg 	 * from se_cmd.
196378eda2bbSSagi Grimberg 	 */
196478eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
196578eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
196678eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
196778eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
196878eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
196978eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
19703d73cf1aSSagi Grimberg };
19713d73cf1aSSagi Grimberg 
19723d73cf1aSSagi Grimberg static int
19733d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
19743d73cf1aSSagi Grimberg {
197538a2d0d4SChristoph Hellwig 	memset(sig_attrs, 0, sizeof(*sig_attrs));
197638a2d0d4SChristoph Hellwig 
19779e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
19789e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
19799e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
198078eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
1981dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
19829e961ae7SSagi Grimberg 		break;
19839e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
19849e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
198578eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
1986dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
19879e961ae7SSagi Grimberg 		break;
19889e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
19899e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
1990dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
1991dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
19929e961ae7SSagi Grimberg 		break;
19939e961ae7SSagi Grimberg 	default:
199424f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
19959e961ae7SSagi Grimberg 		return -EINVAL;
19969e961ae7SSagi Grimberg 	}
19979e961ae7SSagi Grimberg 
199826232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD)
199926232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
200026232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG)
200126232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
200226232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)
200326232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
200426232872SMax Gurtovoy 
20059e961ae7SSagi Grimberg 	return 0;
20069e961ae7SSagi Grimberg }
20079e961ae7SSagi Grimberg 
20089e961ae7SSagi Grimberg static int
200938a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
201038a2d0d4SChristoph Hellwig 		struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
20119e961ae7SSagi Grimberg {
201238a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
201338a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
201438a2d0d4SChristoph Hellwig 	u8 port_num = conn->cm_id->port_num;
201538a2d0d4SChristoph Hellwig 	u64 addr;
201638a2d0d4SChristoph Hellwig 	u32 rkey, offset;
20179e961ae7SSagi Grimberg 	int ret;
20189e961ae7SSagi Grimberg 
201966f53e6fSBharat Potnuri 	if (cmd->ctx_init_done)
202066f53e6fSBharat Potnuri 		goto rdma_ctx_post;
202166f53e6fSBharat Potnuri 
202238a2d0d4SChristoph Hellwig 	if (dir == DMA_FROM_DEVICE) {
202338a2d0d4SChristoph Hellwig 		addr = cmd->write_va;
202438a2d0d4SChristoph Hellwig 		rkey = cmd->write_stag;
202538a2d0d4SChristoph Hellwig 		offset = cmd->iscsi_cmd->write_data_done;
202638a2d0d4SChristoph Hellwig 	} else {
202738a2d0d4SChristoph Hellwig 		addr = cmd->read_va;
202838a2d0d4SChristoph Hellwig 		rkey = cmd->read_stag;
202938a2d0d4SChristoph Hellwig 		offset = 0;
203038a2d0d4SChristoph Hellwig 	}
203138a2d0d4SChristoph Hellwig 
203238a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
203338a2d0d4SChristoph Hellwig 		struct ib_sig_attrs sig_attrs;
203438a2d0d4SChristoph Hellwig 
20359e961ae7SSagi Grimberg 		ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
20369e961ae7SSagi Grimberg 		if (ret)
2037e3d7e4c3SSagi Grimberg 			return ret;
2038e3d7e4c3SSagi Grimberg 
203938a2d0d4SChristoph Hellwig 		WARN_ON_ONCE(offset);
204038a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
204138a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
204238a2d0d4SChristoph Hellwig 				se_cmd->t_prot_sg, se_cmd->t_prot_nents,
204338a2d0d4SChristoph Hellwig 				&sig_attrs, addr, rkey, dir);
2044570db170SSagi Grimberg 	} else {
204538a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
204638a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
204738a2d0d4SChristoph Hellwig 				offset, addr, rkey, dir);
204838a2d0d4SChristoph Hellwig 	}
204966f53e6fSBharat Potnuri 
205038a2d0d4SChristoph Hellwig 	if (ret < 0) {
205138a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
205238a2d0d4SChristoph Hellwig 		return ret;
20539e961ae7SSagi Grimberg 	}
20549e961ae7SSagi Grimberg 
205566f53e6fSBharat Potnuri 	cmd->ctx_init_done = true;
205666f53e6fSBharat Potnuri 
205766f53e6fSBharat Potnuri rdma_ctx_post:
205838a2d0d4SChristoph Hellwig 	ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
205938a2d0d4SChristoph Hellwig 	if (ret < 0)
206038a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to post RDMA res\n", cmd);
206159464ef4SVu Pham 	return ret;
206259464ef4SVu Pham }
206359464ef4SVu Pham 
206459464ef4SVu Pham static int
206590ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
206690ecc6e2SVu Pham {
206790ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
206859464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
20696700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
207038a2d0d4SChristoph Hellwig 	struct ib_cqe *cqe = NULL;
207138a2d0d4SChristoph Hellwig 	struct ib_send_wr *chain_wr = NULL;
207290ecc6e2SVu Pham 	int rc;
207390ecc6e2SVu Pham 
207424f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
207590ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
20764c22e07fSSagi Grimberg 
207738a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd)) {
207838a2d0d4SChristoph Hellwig 		isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
207938a2d0d4SChristoph Hellwig 		cqe = &isert_cmd->tx_desc.tx_cqe;
208038a2d0d4SChristoph Hellwig 	} else {
2081b8d26b3bSNicholas Bellinger 		/*
2082b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2083b8d26b3bSNicholas Bellinger 		 */
20849e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
20859e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
208604d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2087b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2088b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
208995b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
209068a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
20914366b19cSJenny Derzhavetz 
20924366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2093*c97119b6SMax Gurtovoy 		if (rc)
20944366b19cSJenny Derzhavetz 			return rc;
209538a2d0d4SChristoph Hellwig 
209638a2d0d4SChristoph Hellwig 		chain_wr = &isert_cmd->tx_desc.send_wr;
20979e961ae7SSagi Grimberg 	}
2098b8d26b3bSNicholas Bellinger 
2099555a65f6SNicholas Bellinger 	rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2100555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n",
2101555a65f6SNicholas Bellinger 		  isert_cmd, rc);
2102555a65f6SNicholas Bellinger 	return rc;
2103b8d26b3bSNicholas Bellinger }
2104b8d26b3bSNicholas Bellinger 
2105b8d26b3bSNicholas Bellinger static int
2106b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2107b8d26b3bSNicholas Bellinger {
2108d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2109555a65f6SNicholas Bellinger 	int ret;
2110b8d26b3bSNicholas Bellinger 
211124f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
211238a2d0d4SChristoph Hellwig 		 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
2113b8d26b3bSNicholas Bellinger 
211438a2d0d4SChristoph Hellwig 	isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2115555a65f6SNicholas Bellinger 	ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context,
211638a2d0d4SChristoph Hellwig 				     &isert_cmd->tx_desc.tx_cqe, NULL);
2117bdf20e72SSagi Grimberg 
2118555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n",
2119555a65f6SNicholas Bellinger 		 isert_cmd, ret);
2120555a65f6SNicholas Bellinger 	return ret;
2121b8d26b3bSNicholas Bellinger }
2122b8d26b3bSNicholas Bellinger 
2123b8d26b3bSNicholas Bellinger static int
2124b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2125b8d26b3bSNicholas Bellinger {
2126a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2127a4c15cd9SJenny Derzhavetz 	int ret = 0;
2128b8d26b3bSNicholas Bellinger 
2129b8d26b3bSNicholas Bellinger 	switch (state) {
2130a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2131a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2132a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2133a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2134a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2135a4c15cd9SJenny Derzhavetz 		break;
2136b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2137b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2138b8d26b3bSNicholas Bellinger 		break;
2139b8d26b3bSNicholas Bellinger 	default:
214024f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2141b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2142b8d26b3bSNicholas Bellinger 		break;
2143b8d26b3bSNicholas Bellinger 	}
2144b8d26b3bSNicholas Bellinger 
2145b8d26b3bSNicholas Bellinger 	return ret;
2146b8d26b3bSNicholas Bellinger }
2147b8d26b3bSNicholas Bellinger 
2148b8d26b3bSNicholas Bellinger static int
2149b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2150b8d26b3bSNicholas Bellinger {
2151991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2152b8d26b3bSNicholas Bellinger 	int ret;
2153b8d26b3bSNicholas Bellinger 
2154b8d26b3bSNicholas Bellinger 	switch (state) {
2155b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2156b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2157991bb764SSagi Grimberg 		if (!ret)
2158991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2159b8d26b3bSNicholas Bellinger 		break;
2160b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2161b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2162b8d26b3bSNicholas Bellinger 		break;
2163b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2164b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2165b8d26b3bSNicholas Bellinger 		break;
2166b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2167b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2168b8d26b3bSNicholas Bellinger 		break;
2169adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2170adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2171adb54c29SNicholas Bellinger 		break;
2172b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2173b8d26b3bSNicholas Bellinger 		/*
2174b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2175b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2176b8d26b3bSNicholas Bellinger 		 */
2177b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2178b8d26b3bSNicholas Bellinger 		break;
2179b8d26b3bSNicholas Bellinger 	default:
218024f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2181b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2182b8d26b3bSNicholas Bellinger 		break;
2183b8d26b3bSNicholas Bellinger 	}
2184b8d26b3bSNicholas Bellinger 
2185b8d26b3bSNicholas Bellinger 	return ret;
2186b8d26b3bSNicholas Bellinger }
2187b8d26b3bSNicholas Bellinger 
2188ca6c1d82SSagi Grimberg struct rdma_cm_id *
2189ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2190ca6c1d82SSagi Grimberg {
2191ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2192ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2193ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2194ca6c1d82SSagi Grimberg 	int ret;
2195ca6c1d82SSagi Grimberg 
2196ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
219724f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2198ca6c1d82SSagi Grimberg 
2199fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
2200ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2201ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
220224f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2203ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2204ca6c1d82SSagi Grimberg 		goto out;
2205ca6c1d82SSagi Grimberg 	}
220624f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2207ca6c1d82SSagi Grimberg 
2208ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2209ca6c1d82SSagi Grimberg 	if (ret) {
221024f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2211ca6c1d82SSagi Grimberg 		goto out_id;
2212ca6c1d82SSagi Grimberg 	}
2213ca6c1d82SSagi Grimberg 
2214992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
2215ca6c1d82SSagi Grimberg 	if (ret) {
221624f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2217ca6c1d82SSagi Grimberg 		goto out_id;
2218ca6c1d82SSagi Grimberg 	}
2219ca6c1d82SSagi Grimberg 
2220ca6c1d82SSagi Grimberg 	return id;
2221ca6c1d82SSagi Grimberg out_id:
2222ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
2223ca6c1d82SSagi Grimberg out:
2224ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
2225ca6c1d82SSagi Grimberg }
2226ca6c1d82SSagi Grimberg 
2227b8d26b3bSNicholas Bellinger static int
2228b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
222913a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
2230b8d26b3bSNicholas Bellinger {
2231b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2232b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2233b8d26b3bSNicholas Bellinger 	int ret;
2234b8d26b3bSNicholas Bellinger 
2235b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
223693b80f29SLeon Romanovsky 	if (!isert_np)
2237b8d26b3bSNicholas Bellinger 		return -ENOMEM;
223893b80f29SLeon Romanovsky 
2239ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
2240ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
2241bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
2242bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
2243ca6c1d82SSagi Grimberg 	isert_np->np = np;
2244b8d26b3bSNicholas Bellinger 
2245b8d26b3bSNicholas Bellinger 	/*
2246b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2247b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2248b8d26b3bSNicholas Bellinger 	 */
2249b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
225013a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
2251b8d26b3bSNicholas Bellinger 
2252ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
2253b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2254b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2255b8d26b3bSNicholas Bellinger 		goto out;
2256b8d26b3bSNicholas Bellinger 	}
2257b8d26b3bSNicholas Bellinger 
2258ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
2259b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2260b8d26b3bSNicholas Bellinger 
2261b8d26b3bSNicholas Bellinger 	return 0;
2262b8d26b3bSNicholas Bellinger 
2263b8d26b3bSNicholas Bellinger out:
2264b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2265ca6c1d82SSagi Grimberg 
2266b8d26b3bSNicholas Bellinger 	return ret;
2267b8d26b3bSNicholas Bellinger }
2268b8d26b3bSNicholas Bellinger 
2269b8d26b3bSNicholas Bellinger static int
2270b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2271b8d26b3bSNicholas Bellinger {
2272dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2273b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2274b8d26b3bSNicholas Bellinger 	int ret;
227513bce482SJenny Derzhavetz 	struct iser_cm_hdr rsp_hdr;
2276b8d26b3bSNicholas Bellinger 
2277b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2278b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2279b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2280b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2281b8d26b3bSNicholas Bellinger 
228213bce482SJenny Derzhavetz 	memset(&rsp_hdr, 0, sizeof(rsp_hdr));
2283422bd0acSJenny Derzhavetz 	rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2284422bd0acSJenny Derzhavetz 	if (!isert_conn->snd_w_inv)
2285422bd0acSJenny Derzhavetz 		rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
228613bce482SJenny Derzhavetz 	cp.private_data = (void *)&rsp_hdr;
228713bce482SJenny Derzhavetz 	cp.private_data_len = sizeof(rsp_hdr);
228813bce482SJenny Derzhavetz 
2289b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2290b8d26b3bSNicholas Bellinger 	if (ret) {
229124f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
2292b8d26b3bSNicholas Bellinger 		return ret;
2293b8d26b3bSNicholas Bellinger 	}
2294b8d26b3bSNicholas Bellinger 
2295b8d26b3bSNicholas Bellinger 	return 0;
2296b8d26b3bSNicholas Bellinger }
2297b8d26b3bSNicholas Bellinger 
2298b8d26b3bSNicholas Bellinger static int
2299b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2300b8d26b3bSNicholas Bellinger {
23016700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2302b8d26b3bSNicholas Bellinger 	int ret;
2303b8d26b3bSNicholas Bellinger 
230424f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
23052371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
23062371e5daSSagi Grimberg 	if (ret) {
230724f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
23082371e5daSSagi Grimberg 			  isert_conn);
23092371e5daSSagi Grimberg 		return ret;
23102371e5daSSagi Grimberg 	}
23112371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
23122371e5daSSagi Grimberg 
23136faaa85fSNicholas Bellinger 	/*
23146faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
23156faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
23166faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
23176faaa85fSNicholas Bellinger 	 * into a NOP.
23186faaa85fSNicholas Bellinger 	 */
23196faaa85fSNicholas Bellinger 	if (!login->first_request)
23206faaa85fSNicholas Bellinger 		return 0;
2321b8d26b3bSNicholas Bellinger 
23222371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
23232371e5daSSagi Grimberg 
2324dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
2325dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
2326b8d26b3bSNicholas Bellinger 	if (ret)
2327b8d26b3bSNicholas Bellinger 		return ret;
2328b8d26b3bSNicholas Bellinger 
232924f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
23302371e5daSSagi Grimberg 
2331b8d26b3bSNicholas Bellinger 	return 0;
2332b8d26b3bSNicholas Bellinger }
2333b8d26b3bSNicholas Bellinger 
2334b8d26b3bSNicholas Bellinger static void
2335b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2336b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2337b8d26b3bSNicholas Bellinger {
2338dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2339b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2340b8d26b3bSNicholas Bellinger 
2341b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2342b8d26b3bSNicholas Bellinger 
2343dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
2344dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
2345b8d26b3bSNicholas Bellinger }
2346b8d26b3bSNicholas Bellinger 
2347b8d26b3bSNicholas Bellinger static int
2348b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2349b8d26b3bSNicholas Bellinger {
23506700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2351b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2352c6b8e918SSagi Grimberg 	int ret;
2353b8d26b3bSNicholas Bellinger 
2354b8d26b3bSNicholas Bellinger accept_wait:
2355ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
2356c6b8e918SSagi Grimberg 	if (ret)
2357b8d26b3bSNicholas Bellinger 		return -ENODEV;
2358b8d26b3bSNicholas Bellinger 
2359b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2360e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
2361b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
236211378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
2363e346ab34SSagi Grimberg 			 np->np_thread_state);
2364e346ab34SSagi Grimberg 		/**
2365e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
2366e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
2367e346ab34SSagi Grimberg 		 **/
2368b8d26b3bSNicholas Bellinger 		return -ENODEV;
2369b8d26b3bSNicholas Bellinger 	}
2370b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2371b8d26b3bSNicholas Bellinger 
2372ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2373bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
2374ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
2375b8d26b3bSNicholas Bellinger 		goto accept_wait;
2376b8d26b3bSNicholas Bellinger 	}
2377bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
2378bd379220SJenny Derzhavetz 			struct isert_conn, node);
2379bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
2380ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2381b8d26b3bSNicholas Bellinger 
2382b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2383b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2384aea92980SJenny Derzhavetz 	isert_conn->state = ISER_CONN_BOUND;
2385b8d26b3bSNicholas Bellinger 
2386b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2387b8d26b3bSNicholas Bellinger 
238824f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
23892371e5daSSagi Grimberg 
2390b8d26b3bSNicholas Bellinger 	return 0;
2391b8d26b3bSNicholas Bellinger }
2392b8d26b3bSNicholas Bellinger 
2393b8d26b3bSNicholas Bellinger static void
2394b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2395b8d26b3bSNicholas Bellinger {
23966700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2397268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
2398b8d26b3bSNicholas Bellinger 
2399ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
2400ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
2401b8d26b3bSNicholas Bellinger 
2402268e6811SSagi Grimberg 	/*
2403268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
2404268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
2405268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
2406268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
2407bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
2408268e6811SSagi Grimberg 	 */
2409ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2410bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
2411bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
2412268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
2413bd379220SJenny Derzhavetz 					 &isert_np->pending,
2414bd379220SJenny Derzhavetz 					 node) {
2415bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
2416bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
2417bd379220SJenny Derzhavetz 			isert_connect_release(isert_conn);
2418bd379220SJenny Derzhavetz 		}
2419bd379220SJenny Derzhavetz 	}
2420bd379220SJenny Derzhavetz 
2421bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
2422bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
2423bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
2424bd379220SJenny Derzhavetz 					 &isert_np->accepted,
2425bd379220SJenny Derzhavetz 					 node) {
242624f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
2427268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
2428268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
2429268e6811SSagi Grimberg 		}
2430268e6811SSagi Grimberg 	}
2431ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2432268e6811SSagi Grimberg 
2433b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2434b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2435b8d26b3bSNicholas Bellinger }
2436b8d26b3bSNicholas Bellinger 
2437b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
2438b02efbfcSSagi Grimberg {
2439b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
2440b02efbfcSSagi Grimberg 						     struct isert_conn,
2441b02efbfcSSagi Grimberg 						     release_work);
2442b02efbfcSSagi Grimberg 
244324f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
2444b02efbfcSSagi Grimberg 
2445dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2446b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
2447dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2448b02efbfcSSagi Grimberg 
244924f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
2450b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
2451b02efbfcSSagi Grimberg }
2452b02efbfcSSagi Grimberg 
2453bdf20e72SSagi Grimberg static void
2454991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
2455991bb764SSagi Grimberg {
2456991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
2457991bb764SSagi Grimberg 
24584c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
24594c22e07fSSagi Grimberg 
2460991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
246124f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
2462991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
2463991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
2464991bb764SSagi Grimberg 	}
2465991bb764SSagi Grimberg }
2466991bb764SSagi Grimberg 
246776261adaSBart Van Assche static void
246876261adaSBart Van Assche isert_wait4cmds(struct iscsi_conn *conn)
246976261adaSBart Van Assche {
247076261adaSBart Van Assche 	isert_info("iscsi_conn %p\n", conn);
247176261adaSBart Van Assche 
247276261adaSBart Van Assche 	if (conn->sess) {
247376261adaSBart Van Assche 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
247476261adaSBart Van Assche 		target_wait_for_sess_cmds(conn->sess->se_sess);
247576261adaSBart Van Assche 	}
247676261adaSBart Van Assche }
247776261adaSBart Van Assche 
24783e03c4b0SJenny Derzhavetz /**
24793e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
24803e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
24813e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
24823e03c4b0SJenny Derzhavetz  *
24833e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
24843e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
24853e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
24863e03c4b0SJenny Derzhavetz  */
24873e03c4b0SJenny Derzhavetz static void
24883e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
24893e03c4b0SJenny Derzhavetz {
24903e03c4b0SJenny Derzhavetz 	struct iscsi_cmd *cmd, *tmp;
24913e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
24923e03c4b0SJenny Derzhavetz 
24933e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
24943e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
24953e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
24963e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
24973e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
24983e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
24993e03c4b0SJenny Derzhavetz 	}
25003e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
25013e03c4b0SJenny Derzhavetz 
25023e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
25033e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
25043e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
25053e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
25063e03c4b0SJenny Derzhavetz 
25073e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
25083e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
25093e03c4b0SJenny Derzhavetz 		}
25103e03c4b0SJenny Derzhavetz 	}
25113e03c4b0SJenny Derzhavetz }
25123e03c4b0SJenny Derzhavetz 
2513defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
2514b8d26b3bSNicholas Bellinger {
2515b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2516b8d26b3bSNicholas Bellinger 
25174c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
2518b8d26b3bSNicholas Bellinger 
2519dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2520954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2521dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2522b2cb9649SNicholas Bellinger 
2523572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
25243e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
252576261adaSBart Van Assche 	isert_wait4cmds(conn);
2526991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
2527954f2372SSagi Grimberg 
2528b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
2529defd8848SNicholas Bellinger }
2530defd8848SNicholas Bellinger 
2531defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2532defd8848SNicholas Bellinger {
2533defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2534b8d26b3bSNicholas Bellinger 
2535572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
2536b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2537b8d26b3bSNicholas Bellinger }
2538b8d26b3bSNicholas Bellinger 
2539187e84f7SVarun Prakash static void isert_get_rx_pdu(struct iscsi_conn *conn)
2540187e84f7SVarun Prakash {
2541187e84f7SVarun Prakash 	struct completion comp;
2542187e84f7SVarun Prakash 
2543187e84f7SVarun Prakash 	init_completion(&comp);
2544187e84f7SVarun Prakash 
2545187e84f7SVarun Prakash 	wait_for_completion_interruptible(&comp);
2546187e84f7SVarun Prakash }
2547187e84f7SVarun Prakash 
2548b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2549b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2550b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2551bd027d85SNicholas Bellinger 	.rdma_shutdown		= true,
2552d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2553b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2554b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2555b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2556b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2557defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2558b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2559b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2560b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2561b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2562b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2563b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2564b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2565b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2566131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
2567187e84f7SVarun Prakash 	.iscsit_get_rx_pdu	= isert_get_rx_pdu,
2568e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
2569b8d26b3bSNicholas Bellinger };
2570b8d26b3bSNicholas Bellinger 
2571b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2572b8d26b3bSNicholas Bellinger {
2573b8d26b3bSNicholas Bellinger 	int ret;
2574b8d26b3bSNicholas Bellinger 
2575631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
2576631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
2577b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
257824f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
25796f0fae3dSSagi Grimberg 		return -ENOMEM;
2580b8d26b3bSNicholas Bellinger 	}
2581b8d26b3bSNicholas Bellinger 
2582b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2583b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
2584b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
258524f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
2586b02efbfcSSagi Grimberg 		ret = -ENOMEM;
2587b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
2588b02efbfcSSagi Grimberg 	}
2589b02efbfcSSagi Grimberg 
2590b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
259124f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
2592b02efbfcSSagi Grimberg 
2593b8d26b3bSNicholas Bellinger 	return 0;
2594b8d26b3bSNicholas Bellinger 
2595b02efbfcSSagi Grimberg destroy_comp_wq:
2596b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
25976f0fae3dSSagi Grimberg 
2598b8d26b3bSNicholas Bellinger 	return ret;
2599b8d26b3bSNicholas Bellinger }
2600b8d26b3bSNicholas Bellinger 
2601b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2602b8d26b3bSNicholas Bellinger {
2603f5ebec96SSagi Grimberg 	flush_scheduled_work();
2604b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
2605b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2606b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
26074c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
2608b8d26b3bSNicholas Bellinger }
2609b8d26b3bSNicholas Bellinger 
2610b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2611b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2612b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2613b8d26b3bSNicholas Bellinger 
2614b8d26b3bSNicholas Bellinger module_init(isert_init);
2615b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2616