xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision e945c653c8e972d1b81a88e474d79f801b60213a)
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 
31dae7a75fSMax Gurtovoy static int isert_sg_tablesize_set(const char *val,
32dae7a75fSMax Gurtovoy 				  const struct kernel_param *kp);
33dae7a75fSMax Gurtovoy static const struct kernel_param_ops sg_tablesize_ops = {
34dae7a75fSMax Gurtovoy 	.set = isert_sg_tablesize_set,
35dae7a75fSMax Gurtovoy 	.get = param_get_int,
36dae7a75fSMax Gurtovoy };
37dae7a75fSMax Gurtovoy 
38109d19a5SMax Gurtovoy static int isert_sg_tablesize = ISCSI_ISER_MIN_SG_TABLESIZE;
39dae7a75fSMax Gurtovoy module_param_cb(sg_tablesize, &sg_tablesize_ops, &isert_sg_tablesize, 0644);
40dae7a75fSMax Gurtovoy MODULE_PARM_DESC(sg_tablesize,
41109d19a5SMax Gurtovoy 		 "Number of gather/scatter entries in a single scsi command, should >= 128 (default: 128, max: 4096)");
42dae7a75fSMax Gurtovoy 
43b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
44b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
45b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
46b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
47b8d26b3bSNicholas Bellinger 
48f93f3a70SSagi Grimberg static int
49f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
502371e5daSSagi Grimberg static int
519679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn);
522371e5daSSagi Grimberg static int
532371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
54ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
55d40945d8SVu Pham 
562f1b6b7dSSagi Grimberg static void isert_release_work(struct work_struct *work);
579679cc51SChristoph Hellwig static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc);
589679cc51SChristoph Hellwig static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc);
599679cc51SChristoph Hellwig static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc);
609679cc51SChristoph Hellwig static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc);
612f1b6b7dSSagi Grimberg 
62dae7a75fSMax Gurtovoy static int isert_sg_tablesize_set(const char *val, const struct kernel_param *kp)
63dae7a75fSMax Gurtovoy {
64dae7a75fSMax Gurtovoy 	int n = 0, ret;
65dae7a75fSMax Gurtovoy 
66dae7a75fSMax Gurtovoy 	ret = kstrtoint(val, 10, &n);
67dae7a75fSMax Gurtovoy 	if (ret != 0 || n < ISCSI_ISER_MIN_SG_TABLESIZE ||
68dae7a75fSMax Gurtovoy 	    n > ISCSI_ISER_MAX_SG_TABLESIZE)
69dae7a75fSMax Gurtovoy 		return -EINVAL;
70dae7a75fSMax Gurtovoy 
71dae7a75fSMax Gurtovoy 	return param_set_int(val, kp);
72dae7a75fSMax Gurtovoy }
73dae7a75fSMax Gurtovoy 
74302cc7c3SSagi Grimberg static inline bool
75302cc7c3SSagi Grimberg isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
76302cc7c3SSagi Grimberg {
7723a548eeSSagi Grimberg 	return (conn->pi_support &&
78302cc7c3SSagi Grimberg 		cmd->prot_op != TARGET_PROT_NORMAL);
79302cc7c3SSagi Grimberg }
80302cc7c3SSagi Grimberg 
81d40945d8SVu Pham static void
82b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
83b8d26b3bSNicholas Bellinger {
846700425eSSagi Grimberg 	struct isert_conn *isert_conn = context;
85b8d26b3bSNicholas Bellinger 
86ea8a1616SSagi Grimberg 	isert_err("%s (%d): conn %p\n",
87ea8a1616SSagi Grimberg 		  ib_event_msg(e->event), e->event, isert_conn);
88ea8a1616SSagi Grimberg 
89b8d26b3bSNicholas Bellinger 	switch (e->event) {
90b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
91dac6ab30SSagi Grimberg 		rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
92b8d26b3bSNicholas Bellinger 		break;
93b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
944c22e07fSSagi Grimberg 		isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
95b8d26b3bSNicholas Bellinger 		break;
96b8d26b3bSNicholas Bellinger 	default:
97b8d26b3bSNicholas Bellinger 		break;
98b8d26b3bSNicholas Bellinger 	}
99b8d26b3bSNicholas Bellinger }
100b8d26b3bSNicholas Bellinger 
10140fc069aSSagi Grimberg static struct ib_qp *
10240fc069aSSagi Grimberg isert_create_qp(struct isert_conn *isert_conn,
10340fc069aSSagi Grimberg 		struct rdma_cm_id *cma_id)
10440fc069aSSagi Grimberg {
105c6e66307SYamin Friedman 	u32 cq_size = ISERT_QP_MAX_REQ_DTOS + ISERT_QP_MAX_RECV_DTOS + 2;
106dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
107c6e66307SYamin Friedman 	struct ib_device *ib_dev = device->ib_device;
10840fc069aSSagi Grimberg 	struct ib_qp_init_attr attr;
109317000b9SMax Gurtovoy 	int ret, factor;
110b8d26b3bSNicholas Bellinger 
111c6e66307SYamin Friedman 	isert_conn->cq = ib_cq_pool_get(ib_dev, cq_size, -1, IB_POLL_WORKQUEUE);
112c6e66307SYamin Friedman 	if (IS_ERR(isert_conn->cq)) {
113c6e66307SYamin Friedman 		isert_err("Unable to allocate cq\n");
114c6e66307SYamin Friedman 		ret = PTR_ERR(isert_conn->cq);
115c6e66307SYamin Friedman 		return ERR_PTR(ret);
116c6e66307SYamin Friedman 	}
117c6e66307SYamin Friedman 	isert_conn->cq_size = cq_size;
118c6e66307SYamin Friedman 
119b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
120b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
121b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
122c6e66307SYamin Friedman 	attr.send_cq = isert_conn->cq;
123c6e66307SYamin Friedman 	attr.recv_cq = isert_conn->cq;
124572a1434SSagi Grimberg 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS + 1;
125bdf20e72SSagi Grimberg 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
126317000b9SMax Gurtovoy 	factor = rdma_rw_mr_factor(device->ib_device, cma_id->port_num,
127dae7a75fSMax Gurtovoy 				   isert_sg_tablesize);
128317000b9SMax Gurtovoy 	attr.cap.max_rdma_ctxs = ISCSI_DEF_XMIT_CMDS_MAX * factor;
12933023fb8SSteve Wise 	attr.cap.max_send_sge = device->ib_device->attrs.max_send_sge;
130b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
131b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
132b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
133570db170SSagi Grimberg 	if (device->pi_capable)
134c0a6cbb9SIsrael Rukshin 		attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
135b8d26b3bSNicholas Bellinger 
13667cb3949SSagi Grimberg 	ret = rdma_create_qp(cma_id, device->pd, &attr);
137b8d26b3bSNicholas Bellinger 	if (ret) {
13824f412ddSSagi Grimberg 		isert_err("rdma_create_qp failed for cma_id %d\n", ret);
139c6e66307SYamin Friedman 		ib_cq_pool_put(isert_conn->cq, isert_conn->cq_size);
140c6e66307SYamin Friedman 
14140fc069aSSagi Grimberg 		return ERR_PTR(ret);
14240fc069aSSagi Grimberg 	}
14340fc069aSSagi Grimberg 
14440fc069aSSagi Grimberg 	return cma_id->qp;
14540fc069aSSagi Grimberg }
14640fc069aSSagi Grimberg 
14740fc069aSSagi Grimberg static int
148b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
149b8d26b3bSNicholas Bellinger {
150dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
15167cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
152b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
153b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
154b8d26b3bSNicholas Bellinger 	u64 dma_addr;
155b8d26b3bSNicholas Bellinger 	int i, j;
156b8d26b3bSNicholas Bellinger 
1576396bb22SKees Cook 	isert_conn->rx_descs = kcalloc(ISERT_QP_MAX_RECV_DTOS,
1586396bb22SKees Cook 				       sizeof(struct iser_rx_desc),
1596396bb22SKees Cook 				       GFP_KERNEL);
160dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
16193b80f29SLeon Romanovsky 		return -ENOMEM;
162b8d26b3bSNicholas Bellinger 
163dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
164b8d26b3bSNicholas Bellinger 
165b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
1660b089c1eSSagi Grimberg 		dma_addr = ib_dma_map_single(ib_dev, rx_desc->buf,
1670b089c1eSSagi Grimberg 					ISER_RX_SIZE, DMA_FROM_DEVICE);
168b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
169b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
170b8d26b3bSNicholas Bellinger 
171b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
172b8d26b3bSNicholas Bellinger 
173b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
1740b089c1eSSagi Grimberg 		rx_sg->addr = rx_desc->dma_addr + isert_get_hdr_offset(rx_desc);
175b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
17634efc7dfSJason Gunthorpe 		rx_sg->lkey = device->pd->local_dma_lkey;
1779679cc51SChristoph Hellwig 		rx_desc->rx_cqe.done = isert_recv_done;
178b8d26b3bSNicholas Bellinger 	}
179b8d26b3bSNicholas Bellinger 
180b8d26b3bSNicholas Bellinger 	return 0;
181b8d26b3bSNicholas Bellinger 
182b8d26b3bSNicholas Bellinger dma_map_fail:
183dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
184b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
185b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
1860b089c1eSSagi Grimberg 				    ISER_RX_SIZE, DMA_FROM_DEVICE);
187b8d26b3bSNicholas Bellinger 	}
188dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
189dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
1904c22e07fSSagi Grimberg 	isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
191b8d26b3bSNicholas Bellinger 	return -ENOMEM;
192b8d26b3bSNicholas Bellinger }
193b8d26b3bSNicholas Bellinger 
194b8d26b3bSNicholas Bellinger static void
195b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
196b8d26b3bSNicholas Bellinger {
197dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
198b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
199b8d26b3bSNicholas Bellinger 	int i;
200b8d26b3bSNicholas Bellinger 
201dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
202b8d26b3bSNicholas Bellinger 		return;
203b8d26b3bSNicholas Bellinger 
204dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
205b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
206b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
2070b089c1eSSagi Grimberg 				    ISER_RX_SIZE, DMA_FROM_DEVICE);
208b8d26b3bSNicholas Bellinger 	}
209b8d26b3bSNicholas Bellinger 
210dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
211dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
212b8d26b3bSNicholas Bellinger }
213b8d26b3bSNicholas Bellinger 
214172369c5SSagi Grimberg static int
215172369c5SSagi Grimberg isert_create_device_ib_res(struct isert_device *device)
216172369c5SSagi Grimberg {
2174a061b28SOr Gerlitz 	struct ib_device *ib_dev = device->ib_device;
218fd8205e8SSagi Grimberg 	int ret;
219172369c5SSagi Grimberg 
22033023fb8SSteve Wise 	isert_dbg("devattr->max_send_sge: %d devattr->max_recv_sge %d\n",
22133023fb8SSteve Wise 		  ib_dev->attrs.max_send_sge, ib_dev->attrs.max_recv_sge);
2224a061b28SOr Gerlitz 	isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
223172369c5SSagi Grimberg 
224ed082d36SChristoph Hellwig 	device->pd = ib_alloc_pd(ib_dev, 0);
22567cb3949SSagi Grimberg 	if (IS_ERR(device->pd)) {
22667cb3949SSagi Grimberg 		ret = PTR_ERR(device->pd);
22767cb3949SSagi Grimberg 		isert_err("failed to allocate pd, device %p, ret=%d\n",
22867cb3949SSagi Grimberg 			  device, ret);
229c6e66307SYamin Friedman 		return ret;
23067cb3949SSagi Grimberg 	}
23167cb3949SSagi Grimberg 
232172369c5SSagi Grimberg 	/* Check signature cap */
233*e945c653SJason Gunthorpe 	if (ib_dev->attrs.kernel_cap_flags & IBK_INTEGRITY_HANDOVER)
234a6dc16b6SMax Gurtovoy 		device->pi_capable = true;
235a6dc16b6SMax Gurtovoy 	else
236a6dc16b6SMax Gurtovoy 		device->pi_capable = false;
23767cb3949SSagi Grimberg 
238b8d26b3bSNicholas Bellinger 	return 0;
239b8d26b3bSNicholas Bellinger }
240b8d26b3bSNicholas Bellinger 
241b8d26b3bSNicholas Bellinger static void
242b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
243b8d26b3bSNicholas Bellinger {
24424f412ddSSagi Grimberg 	isert_info("device %p\n", device);
245b8d26b3bSNicholas Bellinger 
24667cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
247b8d26b3bSNicholas Bellinger }
248b8d26b3bSNicholas Bellinger 
249b8d26b3bSNicholas Bellinger static void
250cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device)
251b8d26b3bSNicholas Bellinger {
252b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
253b8d26b3bSNicholas Bellinger 	device->refcount--;
2544c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
255b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
256b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
257b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
258b8d26b3bSNicholas Bellinger 		kfree(device);
259b8d26b3bSNicholas Bellinger 	}
260b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
261b8d26b3bSNicholas Bellinger }
262b8d26b3bSNicholas Bellinger 
263b8d26b3bSNicholas Bellinger static struct isert_device *
264cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id)
265b8d26b3bSNicholas Bellinger {
266b8d26b3bSNicholas Bellinger 	struct isert_device *device;
267b8d26b3bSNicholas Bellinger 	int ret;
268b8d26b3bSNicholas Bellinger 
269b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
270b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
271b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
272b8d26b3bSNicholas Bellinger 			device->refcount++;
2734c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
2744c22e07fSSagi Grimberg 				   device, device->refcount);
275b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
276b8d26b3bSNicholas Bellinger 			return device;
277b8d26b3bSNicholas Bellinger 		}
278b8d26b3bSNicholas Bellinger 	}
279b8d26b3bSNicholas Bellinger 
280b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
281b8d26b3bSNicholas Bellinger 	if (!device) {
282b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
283b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
284b8d26b3bSNicholas Bellinger 	}
285b8d26b3bSNicholas Bellinger 
286b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
287b8d26b3bSNicholas Bellinger 
288b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
289b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
290b8d26b3bSNicholas Bellinger 	if (ret) {
291b8d26b3bSNicholas Bellinger 		kfree(device);
292b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
293b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
294b8d26b3bSNicholas Bellinger 	}
295b8d26b3bSNicholas Bellinger 
296b8d26b3bSNicholas Bellinger 	device->refcount++;
297b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
2984c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
2994c22e07fSSagi Grimberg 		   device, device->refcount);
300b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
301b8d26b3bSNicholas Bellinger 
302b8d26b3bSNicholas Bellinger 	return device;
303b8d26b3bSNicholas Bellinger }
304b8d26b3bSNicholas Bellinger 
30559464ef4SVu Pham static void
306ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn)
307ae9ea9edSSagi Grimberg {
308ae9ea9edSSagi Grimberg 	isert_conn->state = ISER_CONN_INIT;
309bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_conn->node);
310dac6ab30SSagi Grimberg 	init_completion(&isert_conn->login_comp);
311ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
31263b268d2SRaju Rangoju 	init_waitqueue_head(&isert_conn->rem_wait);
313dac6ab30SSagi Grimberg 	kref_init(&isert_conn->kref);
314dac6ab30SSagi Grimberg 	mutex_init(&isert_conn->mutex);
3152f1b6b7dSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
316ae9ea9edSSagi Grimberg }
317ae9ea9edSSagi Grimberg 
318ae9ea9edSSagi Grimberg static void
319ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn)
320ae9ea9edSSagi Grimberg {
321dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
322ae9ea9edSSagi Grimberg 
323ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
324ed1083b2SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
3255adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
3265adabdd1SChristoph Hellwig 
3270b089c1eSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_desc->dma_addr,
3280b089c1eSSagi Grimberg 			    ISER_RX_SIZE, DMA_FROM_DEVICE);
3290b089c1eSSagi Grimberg 	kfree(isert_conn->login_desc);
330ae9ea9edSSagi Grimberg }
331ae9ea9edSSagi Grimberg 
332ae9ea9edSSagi Grimberg static int
333ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn,
334ae9ea9edSSagi Grimberg 		      struct ib_device *ib_dev)
335ae9ea9edSSagi Grimberg {
336ae9ea9edSSagi Grimberg 	int ret;
337ae9ea9edSSagi Grimberg 
3380b089c1eSSagi Grimberg 	isert_conn->login_desc = kzalloc(sizeof(*isert_conn->login_desc),
3395adabdd1SChristoph Hellwig 			GFP_KERNEL);
3400b089c1eSSagi Grimberg 	if (!isert_conn->login_desc)
341ae9ea9edSSagi Grimberg 		return -ENOMEM;
342ae9ea9edSSagi Grimberg 
3430b089c1eSSagi Grimberg 	isert_conn->login_desc->dma_addr = ib_dma_map_single(ib_dev,
3440b089c1eSSagi Grimberg 				isert_conn->login_desc->buf,
3450b089c1eSSagi Grimberg 				ISER_RX_SIZE, DMA_FROM_DEVICE);
3460b089c1eSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_desc->dma_addr);
347ae9ea9edSSagi Grimberg 	if (ret) {
3480b089c1eSSagi Grimberg 		isert_err("login_desc dma mapping error: %d\n", ret);
3490b089c1eSSagi Grimberg 		isert_conn->login_desc->dma_addr = 0;
3500b089c1eSSagi Grimberg 		goto out_free_login_desc;
3515adabdd1SChristoph Hellwig 	}
3525adabdd1SChristoph Hellwig 
3535adabdd1SChristoph Hellwig 	isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
3545adabdd1SChristoph Hellwig 	if (!isert_conn->login_rsp_buf) {
3551d5840c9SWei Yongjun 		ret = -ENOMEM;
3560b089c1eSSagi Grimberg 		goto out_unmap_login_desc;
357ae9ea9edSSagi Grimberg 	}
358ae9ea9edSSagi Grimberg 
359ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
3605adabdd1SChristoph Hellwig 					isert_conn->login_rsp_buf,
361ed1083b2SChristoph Hellwig 					ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
362ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
363ae9ea9edSSagi Grimberg 	if (ret) {
364ae9ea9edSSagi Grimberg 		isert_err("login_rsp_dma mapping error: %d\n", ret);
365ae9ea9edSSagi Grimberg 		isert_conn->login_rsp_dma = 0;
3665adabdd1SChristoph Hellwig 		goto out_free_login_rsp_buf;
367ae9ea9edSSagi Grimberg 	}
368ae9ea9edSSagi Grimberg 
369ae9ea9edSSagi Grimberg 	return 0;
370ae9ea9edSSagi Grimberg 
3715adabdd1SChristoph Hellwig out_free_login_rsp_buf:
3725adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
3730b089c1eSSagi Grimberg out_unmap_login_desc:
3740b089c1eSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_desc->dma_addr,
3750b089c1eSSagi Grimberg 			    ISER_RX_SIZE, DMA_FROM_DEVICE);
3760b089c1eSSagi Grimberg out_free_login_desc:
3770b089c1eSSagi Grimberg 	kfree(isert_conn->login_desc);
378ae9ea9edSSagi Grimberg 	return ret;
379ae9ea9edSSagi Grimberg }
380ae9ea9edSSagi Grimberg 
381422bd0acSJenny Derzhavetz static void
382422bd0acSJenny Derzhavetz isert_set_nego_params(struct isert_conn *isert_conn,
383422bd0acSJenny Derzhavetz 		      struct rdma_conn_param *param)
384422bd0acSJenny Derzhavetz {
385422bd0acSJenny Derzhavetz 	struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
386422bd0acSJenny Derzhavetz 
387422bd0acSJenny Derzhavetz 	/* Set max inflight RDMA READ requests */
388422bd0acSJenny Derzhavetz 	isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
389422bd0acSJenny Derzhavetz 				attr->max_qp_init_rd_atom);
390422bd0acSJenny Derzhavetz 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
391422bd0acSJenny Derzhavetz 
392422bd0acSJenny Derzhavetz 	if (param->private_data) {
393422bd0acSJenny Derzhavetz 		u8 flags = *(u8 *)param->private_data;
394422bd0acSJenny Derzhavetz 
395422bd0acSJenny Derzhavetz 		/*
396422bd0acSJenny Derzhavetz 		 * use remote invalidation if the both initiator
397422bd0acSJenny Derzhavetz 		 * and the HCA support it
398422bd0acSJenny Derzhavetz 		 */
399422bd0acSJenny Derzhavetz 		isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
400422bd0acSJenny Derzhavetz 					  (attr->device_cap_flags &
401422bd0acSJenny Derzhavetz 					   IB_DEVICE_MEM_MGT_EXTENSIONS);
402422bd0acSJenny Derzhavetz 		if (isert_conn->snd_w_inv)
403422bd0acSJenny Derzhavetz 			isert_info("Using remote invalidation\n");
404422bd0acSJenny Derzhavetz 	}
405422bd0acSJenny Derzhavetz }
406422bd0acSJenny Derzhavetz 
407c6e66307SYamin Friedman static void
408c6e66307SYamin Friedman isert_destroy_qp(struct isert_conn *isert_conn)
409c6e66307SYamin Friedman {
410c6e66307SYamin Friedman 	ib_destroy_qp(isert_conn->qp);
411c6e66307SYamin Friedman 	ib_cq_pool_put(isert_conn->cq, isert_conn->cq_size);
412c6e66307SYamin Friedman }
413c6e66307SYamin Friedman 
414b8d26b3bSNicholas Bellinger static int
415b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
416b8d26b3bSNicholas Bellinger {
417ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
418ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
419b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
420b8d26b3bSNicholas Bellinger 	struct isert_device *device;
421b8d26b3bSNicholas Bellinger 	int ret = 0;
42214f4b54fSSagi Grimberg 
42314f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
42414f4b54fSSagi Grimberg 	if (!np->enabled) {
42514f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
42624f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
4278094ba0aSLeon Romanovsky 		return rdma_reject(cma_id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
42814f4b54fSSagi Grimberg 	}
42914f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
430b8d26b3bSNicholas Bellinger 
4314c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
432b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
433b8d26b3bSNicholas Bellinger 
434b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
435ae9ea9edSSagi Grimberg 	if (!isert_conn)
436b8d26b3bSNicholas Bellinger 		return -ENOMEM;
437b8d26b3bSNicholas Bellinger 
438ae9ea9edSSagi Grimberg 	isert_init_conn(isert_conn);
439dac6ab30SSagi Grimberg 	isert_conn->cm_id = cma_id;
440b8d26b3bSNicholas Bellinger 
441cf8ae958SSagi Grimberg 	device = isert_device_get(cma_id);
442b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
443b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
444adb76a52SLv Yunlong 		goto out;
445b8d26b3bSNicholas Bellinger 	}
446dac6ab30SSagi Grimberg 	isert_conn->device = device;
447b8d26b3bSNicholas Bellinger 
448adb76a52SLv Yunlong 	ret = isert_alloc_login_buf(isert_conn, cma_id->device);
449adb76a52SLv Yunlong 	if (ret)
450adb76a52SLv Yunlong 		goto out_conn_dev;
451adb76a52SLv Yunlong 
452422bd0acSJenny Derzhavetz 	isert_set_nego_params(isert_conn, &event->param.conn);
4531a92e17eSSagi Grimberg 
454c6e66307SYamin Friedman 	isert_conn->qp = isert_create_qp(isert_conn, cma_id);
455c6e66307SYamin Friedman 	if (IS_ERR(isert_conn->qp)) {
456c6e66307SYamin Friedman 		ret = PTR_ERR(isert_conn->qp);
457adb76a52SLv Yunlong 		goto out_rsp_dma_map;
458c6e66307SYamin Friedman 	}
459b8d26b3bSNicholas Bellinger 
4609679cc51SChristoph Hellwig 	ret = isert_login_post_recv(isert_conn);
4612371e5daSSagi Grimberg 	if (ret)
462c6e66307SYamin Friedman 		goto out_destroy_qp;
4632371e5daSSagi Grimberg 
4642371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
4652371e5daSSagi Grimberg 	if (ret)
466c6e66307SYamin Friedman 		goto out_destroy_qp;
4672371e5daSSagi Grimberg 
468ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
469bd379220SJenny Derzhavetz 	list_add_tail(&isert_conn->node, &isert_np->accepted);
470ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
471b8d26b3bSNicholas Bellinger 
472b8d26b3bSNicholas Bellinger 	return 0;
473b8d26b3bSNicholas Bellinger 
474c6e66307SYamin Friedman out_destroy_qp:
475c6e66307SYamin Friedman 	isert_destroy_qp(isert_conn);
476b8d26b3bSNicholas Bellinger out_rsp_dma_map:
477ae9ea9edSSagi Grimberg 	isert_free_login_buf(isert_conn);
478adb76a52SLv Yunlong out_conn_dev:
479adb76a52SLv Yunlong 	isert_device_put(device);
480b8d26b3bSNicholas Bellinger out:
481b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
4828094ba0aSLeon Romanovsky 	rdma_reject(cma_id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
483b8d26b3bSNicholas Bellinger 	return ret;
484b8d26b3bSNicholas Bellinger }
485b8d26b3bSNicholas Bellinger 
486b8d26b3bSNicholas Bellinger static void
487b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
488b8d26b3bSNicholas Bellinger {
489dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
490b8d26b3bSNicholas Bellinger 
4914c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
492b8d26b3bSNicholas Bellinger 
49357df81e3SSagi Grimberg 	BUG_ON(!device);
49457df81e3SSagi Grimberg 
49519e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
49663b268d2SRaju Rangoju 	if (isert_conn->cm_id &&
49763b268d2SRaju Rangoju 	    !isert_conn->dev_removed)
498dac6ab30SSagi Grimberg 		rdma_destroy_id(isert_conn->cm_id);
49919e2090fSSagi Grimberg 
500c6e66307SYamin Friedman 	if (isert_conn->qp)
501c6e66307SYamin Friedman 		isert_destroy_qp(isert_conn);
502b8d26b3bSNicholas Bellinger 
5030b089c1eSSagi Grimberg 	if (isert_conn->login_desc)
504ae9ea9edSSagi Grimberg 		isert_free_login_buf(isert_conn);
505ae9ea9edSSagi Grimberg 
506cf8ae958SSagi Grimberg 	isert_device_put(device);
50757df81e3SSagi Grimberg 
50863b268d2SRaju Rangoju 	if (isert_conn->dev_removed)
50963b268d2SRaju Rangoju 		wake_up_interruptible(&isert_conn->rem_wait);
51063b268d2SRaju Rangoju 	else
51157df81e3SSagi Grimberg 		kfree(isert_conn);
512b8d26b3bSNicholas Bellinger }
513b8d26b3bSNicholas Bellinger 
514b8d26b3bSNicholas Bellinger static void
515b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
516b8d26b3bSNicholas Bellinger {
51719e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
518bd379220SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
519c2f88b17SSagi Grimberg 
52024f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
521128e9cc8SSagi Grimberg 
522dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
523128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_UP;
524bd379220SJenny Derzhavetz 	kref_get(&isert_conn->kref);
525dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
526bd379220SJenny Derzhavetz 
527bd379220SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
528bd379220SJenny Derzhavetz 	list_move_tail(&isert_conn->node, &isert_np->pending);
529bd379220SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
530bd379220SJenny Derzhavetz 
531bd379220SJenny Derzhavetz 	isert_info("np %p: Allow accept_np to continue\n", isert_np);
532bd379220SJenny Derzhavetz 	up(&isert_np->sem);
533b8d26b3bSNicholas Bellinger }
534b8d26b3bSNicholas Bellinger 
535b8d26b3bSNicholas Bellinger static void
536dac6ab30SSagi Grimberg isert_release_kref(struct kref *kref)
537b8d26b3bSNicholas Bellinger {
538b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
539dac6ab30SSagi Grimberg 				struct isert_conn, kref);
540b8d26b3bSNicholas Bellinger 
5414c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
5424c22e07fSSagi Grimberg 		   current->pid);
543b8d26b3bSNicholas Bellinger 
544b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
545b8d26b3bSNicholas Bellinger }
546b8d26b3bSNicholas Bellinger 
547b8d26b3bSNicholas Bellinger static void
548b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
549b8d26b3bSNicholas Bellinger {
550dac6ab30SSagi Grimberg 	kref_put(&isert_conn->kref, isert_release_kref);
551b8d26b3bSNicholas Bellinger }
552b8d26b3bSNicholas Bellinger 
5536d1fba0cSJenny Derzhavetz static void
5546d1fba0cSJenny Derzhavetz isert_handle_unbound_conn(struct isert_conn *isert_conn)
5556d1fba0cSJenny Derzhavetz {
5566d1fba0cSJenny Derzhavetz 	struct isert_np *isert_np = isert_conn->cm_id->context;
5576d1fba0cSJenny Derzhavetz 
5586d1fba0cSJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
5596d1fba0cSJenny Derzhavetz 	if (!list_empty(&isert_conn->node)) {
5606d1fba0cSJenny Derzhavetz 		/*
5616d1fba0cSJenny Derzhavetz 		 * This means iscsi doesn't know this connection
5626d1fba0cSJenny Derzhavetz 		 * so schedule a cleanup ourselves
5636d1fba0cSJenny Derzhavetz 		 */
5646d1fba0cSJenny Derzhavetz 		list_del_init(&isert_conn->node);
5656d1fba0cSJenny Derzhavetz 		isert_put_conn(isert_conn);
5666d1fba0cSJenny Derzhavetz 		queue_work(isert_release_wq, &isert_conn->release_work);
5676d1fba0cSJenny Derzhavetz 	}
5686d1fba0cSJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
5696d1fba0cSJenny Derzhavetz }
5706d1fba0cSJenny Derzhavetz 
571954f2372SSagi Grimberg /**
572954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
573954f2372SSagi Grimberg  * @isert_conn: isert connection struct
574954f2372SSagi Grimberg  *
575954f2372SSagi Grimberg  * Notes:
576aea92980SJenny Derzhavetz  * In case the connection state is BOUND, move state
577954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
578128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
579954f2372SSagi Grimberg  *
580dac6ab30SSagi Grimberg  * This routine must be called with mutex held. Thus it is
581954f2372SSagi Grimberg  * safe to call multiple times.
582954f2372SSagi Grimberg  */
583954f2372SSagi Grimberg static void
584954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
585954f2372SSagi Grimberg {
586954f2372SSagi Grimberg 	int err;
587954f2372SSagi Grimberg 
5886d1fba0cSJenny Derzhavetz 	if (isert_conn->state >= ISER_CONN_TERMINATING)
5896d1fba0cSJenny Derzhavetz 		return;
5906d1fba0cSJenny Derzhavetz 
59124f412ddSSagi Grimberg 	isert_info("Terminating conn %p state %d\n",
592954f2372SSagi Grimberg 		   isert_conn, isert_conn->state);
593128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_TERMINATING;
594dac6ab30SSagi Grimberg 	err = rdma_disconnect(isert_conn->cm_id);
595954f2372SSagi Grimberg 	if (err)
59624f412ddSSagi Grimberg 		isert_warn("Failed rdma_disconnect isert_conn %p\n",
597954f2372SSagi Grimberg 			   isert_conn);
598954f2372SSagi Grimberg }
599954f2372SSagi Grimberg 
6003b726ae2SSagi Grimberg static int
601ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
602ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
603b8d26b3bSNicholas Bellinger {
604ea8a1616SSagi Grimberg 	isert_dbg("%s (%d): isert np %p\n",
605ea8a1616SSagi Grimberg 		  rdma_event_msg(event), event, isert_np);
6063b726ae2SSagi Grimberg 
607ca6c1d82SSagi Grimberg 	switch (event) {
608ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
609ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = NULL;
610ca6c1d82SSagi Grimberg 		break;
611ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
612ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = isert_setup_id(isert_np);
613ed8cb0a4SJenny Derzhavetz 		if (IS_ERR(isert_np->cm_id)) {
61424f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
615ed8cb0a4SJenny Derzhavetz 				  isert_np, PTR_ERR(isert_np->cm_id));
616ed8cb0a4SJenny Derzhavetz 			isert_np->cm_id = NULL;
617ca6c1d82SSagi Grimberg 		}
618ca6c1d82SSagi Grimberg 		break;
619ca6c1d82SSagi Grimberg 	default:
62024f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
621ca6c1d82SSagi Grimberg 			  isert_np, event);
622ca6c1d82SSagi Grimberg 	}
623ca6c1d82SSagi Grimberg 
6243b726ae2SSagi Grimberg 	return -1;
6253b726ae2SSagi Grimberg }
6263b726ae2SSagi Grimberg 
627ca6c1d82SSagi Grimberg static int
628ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
629ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
630ca6c1d82SSagi Grimberg {
631f81bf458SJenny Derzhavetz 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
632ca6c1d82SSagi Grimberg 
633dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
6346d1fba0cSJenny Derzhavetz 	switch (isert_conn->state) {
6356d1fba0cSJenny Derzhavetz 	case ISER_CONN_TERMINATING:
6366d1fba0cSJenny Derzhavetz 		break;
6376d1fba0cSJenny Derzhavetz 	case ISER_CONN_UP:
638128e9cc8SSagi Grimberg 		isert_conn_terminate(isert_conn);
639572a1434SSagi Grimberg 		ib_drain_qp(isert_conn->qp);
6406d1fba0cSJenny Derzhavetz 		isert_handle_unbound_conn(isert_conn);
6416d1fba0cSJenny Derzhavetz 		break;
6426d1fba0cSJenny Derzhavetz 	case ISER_CONN_BOUND:
6436d1fba0cSJenny Derzhavetz 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
6446d1fba0cSJenny Derzhavetz 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
6456d1fba0cSJenny Derzhavetz 		break;
6466d1fba0cSJenny Derzhavetz 	default:
647506f71d1SColin Ian King 		isert_warn("conn %p terminating in state %d\n",
6486d1fba0cSJenny Derzhavetz 			   isert_conn, isert_conn->state);
6496d1fba0cSJenny Derzhavetz 	}
650dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
651128e9cc8SSagi Grimberg 
6523b726ae2SSagi Grimberg 	return 0;
653b8d26b3bSNicholas Bellinger }
654b8d26b3bSNicholas Bellinger 
6554a579da2SSagi Grimberg static int
656954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
657954f2372SSagi Grimberg {
65819e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
659954f2372SSagi Grimberg 
660cd52cb26SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
661bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
662dac6ab30SSagi Grimberg 	isert_conn->cm_id = NULL;
663954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
6644a579da2SSagi Grimberg 
6654a579da2SSagi Grimberg 	return -1;
666954f2372SSagi Grimberg }
667954f2372SSagi Grimberg 
668b8d26b3bSNicholas Bellinger static int
669b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
670b8d26b3bSNicholas Bellinger {
671f81bf458SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
67263b268d2SRaju Rangoju 	struct isert_conn *isert_conn;
673b8d26b3bSNicholas Bellinger 	int ret = 0;
674b8d26b3bSNicholas Bellinger 
675ea8a1616SSagi Grimberg 	isert_info("%s (%d): status %d id %p np %p\n",
676ea8a1616SSagi Grimberg 		   rdma_event_msg(event->event), event->event,
6774c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
678b8d26b3bSNicholas Bellinger 
679f81bf458SJenny Derzhavetz 	if (isert_np->cm_id == cma_id)
680f81bf458SJenny Derzhavetz 		return isert_np_cma_handler(cma_id->context, event->event);
681f81bf458SJenny Derzhavetz 
682b8d26b3bSNicholas Bellinger 	switch (event->event) {
683b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
684b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
6853b726ae2SSagi Grimberg 		if (ret)
6864c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
687b8d26b3bSNicholas Bellinger 		break;
688b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
689b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
690b8d26b3bSNicholas Bellinger 		break;
691df561f66SGustavo A. R. Silva 	case RDMA_CM_EVENT_ADDR_CHANGE:
692df561f66SGustavo A. R. Silva 	case RDMA_CM_EVENT_DISCONNECTED:
69388c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
694ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
695b8d26b3bSNicholas Bellinger 		break;
69663b268d2SRaju Rangoju 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
69763b268d2SRaju Rangoju 		isert_conn = cma_id->qp->qp_context;
69863b268d2SRaju Rangoju 		isert_conn->dev_removed = true;
69963b268d2SRaju Rangoju 		isert_disconnected_handler(cma_id, event->event);
70063b268d2SRaju Rangoju 		wait_event_interruptible(isert_conn->rem_wait,
70163b268d2SRaju Rangoju 					 isert_conn->state == ISER_CONN_DOWN);
70263b268d2SRaju Rangoju 		kfree(isert_conn);
70363b268d2SRaju Rangoju 		/*
70463b268d2SRaju Rangoju 		 * return non-zero from the callback to destroy
70563b268d2SRaju Rangoju 		 * the rdma cm id
70663b268d2SRaju Rangoju 		 */
70763b268d2SRaju Rangoju 		return 1;
708d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_REJECTED:
7091e38a366SSteve Wise 		isert_info("Connection rejected: %s\n",
7101e38a366SSteve Wise 			   rdma_reject_msg(cma_id, event->status));
711df561f66SGustavo A. R. Silva 		fallthrough;
712d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_UNREACHABLE:
713b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
7144a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
715954f2372SSagi Grimberg 		break;
716b8d26b3bSNicholas Bellinger 	default:
71724f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
718b8d26b3bSNicholas Bellinger 		break;
719b8d26b3bSNicholas Bellinger 	}
720b8d26b3bSNicholas Bellinger 
721b8d26b3bSNicholas Bellinger 	return ret;
722b8d26b3bSNicholas Bellinger }
723b8d26b3bSNicholas Bellinger 
724b8d26b3bSNicholas Bellinger static int
7254366b19cSJenny Derzhavetz isert_post_recvm(struct isert_conn *isert_conn, u32 count)
726b8d26b3bSNicholas Bellinger {
727e01a7674SBart Van Assche 	struct ib_recv_wr *rx_wr;
728b8d26b3bSNicholas Bellinger 	int i, ret;
729b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
730b8d26b3bSNicholas Bellinger 
731dac6ab30SSagi Grimberg 	for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
7324366b19cSJenny Derzhavetz 		rx_desc = &isert_conn->rx_descs[i];
7339679cc51SChristoph Hellwig 
7349679cc51SChristoph Hellwig 		rx_wr->wr_cqe = &rx_desc->rx_cqe;
735b8d26b3bSNicholas Bellinger 		rx_wr->sg_list = &rx_desc->rx_sg;
736b8d26b3bSNicholas Bellinger 		rx_wr->num_sge = 1;
737b8d26b3bSNicholas Bellinger 		rx_wr->next = rx_wr + 1;
7387a56dc88SSagi Grimberg 		rx_desc->in_use = false;
739b8d26b3bSNicholas Bellinger 	}
740b8d26b3bSNicholas Bellinger 	rx_wr--;
741b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
742b8d26b3bSNicholas Bellinger 
743e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr, NULL);
7446d1fba0cSJenny Derzhavetz 	if (ret)
74524f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
7464366b19cSJenny Derzhavetz 
7474366b19cSJenny Derzhavetz 	return ret;
7484366b19cSJenny Derzhavetz }
7494366b19cSJenny Derzhavetz 
7504366b19cSJenny Derzhavetz static int
7514366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
7524366b19cSJenny Derzhavetz {
753e01a7674SBart Van Assche 	struct ib_recv_wr rx_wr;
7544366b19cSJenny Derzhavetz 	int ret;
7554366b19cSJenny Derzhavetz 
7567a56dc88SSagi Grimberg 	if (!rx_desc->in_use) {
7577a56dc88SSagi Grimberg 		/*
7587a56dc88SSagi Grimberg 		 * if the descriptor is not in-use we already reposted it
7597a56dc88SSagi Grimberg 		 * for recv, so just silently return
7607a56dc88SSagi Grimberg 		 */
7617a56dc88SSagi Grimberg 		return 0;
7627a56dc88SSagi Grimberg 	}
7637a56dc88SSagi Grimberg 
7647a56dc88SSagi Grimberg 	rx_desc->in_use = false;
7659679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &rx_desc->rx_cqe;
7664366b19cSJenny Derzhavetz 	rx_wr.sg_list = &rx_desc->rx_sg;
7674366b19cSJenny Derzhavetz 	rx_wr.num_sge = 1;
7684366b19cSJenny Derzhavetz 	rx_wr.next = NULL;
7694366b19cSJenny Derzhavetz 
770e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL);
7716d1fba0cSJenny Derzhavetz 	if (ret)
7724366b19cSJenny Derzhavetz 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
7734366b19cSJenny Derzhavetz 
774b8d26b3bSNicholas Bellinger 	return ret;
775b8d26b3bSNicholas Bellinger }
776b8d26b3bSNicholas Bellinger 
777b8d26b3bSNicholas Bellinger static int
7789679cc51SChristoph Hellwig isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
779b8d26b3bSNicholas Bellinger {
780dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
781e01a7674SBart Van Assche 	struct ib_send_wr send_wr;
782b8d26b3bSNicholas Bellinger 	int ret;
783b8d26b3bSNicholas Bellinger 
784b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
785b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
786b8d26b3bSNicholas Bellinger 
7879679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_login_send_done;
7889679cc51SChristoph Hellwig 
789b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
7909679cc51SChristoph Hellwig 	send_wr.wr_cqe	= &tx_desc->tx_cqe;
791b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
792b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
793b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
794b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
795b8d26b3bSNicholas Bellinger 
796e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &send_wr, NULL);
797bdf20e72SSagi Grimberg 	if (ret)
79824f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
799b8d26b3bSNicholas Bellinger 
800b8d26b3bSNicholas Bellinger 	return ret;
801b8d26b3bSNicholas Bellinger }
802b8d26b3bSNicholas Bellinger 
803b8d26b3bSNicholas Bellinger static void
804763b6965SAlex Estrin __isert_create_send_desc(struct isert_device *device,
805b8d26b3bSNicholas Bellinger 			 struct iser_tx_desc *tx_desc)
806b8d26b3bSNicholas Bellinger {
807b8d26b3bSNicholas Bellinger 
808d3cf81f9SSagi Grimberg 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
809d3cf81f9SSagi Grimberg 	tx_desc->iser_header.flags = ISCSI_CTRL;
810b8d26b3bSNicholas Bellinger 
811b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
812b8d26b3bSNicholas Bellinger 
81334efc7dfSJason Gunthorpe 	if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
81434efc7dfSJason Gunthorpe 		tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
81524f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
816b8d26b3bSNicholas Bellinger 	}
817b8d26b3bSNicholas Bellinger }
818b8d26b3bSNicholas Bellinger 
819763b6965SAlex Estrin static void
820763b6965SAlex Estrin isert_create_send_desc(struct isert_conn *isert_conn,
821763b6965SAlex Estrin 		       struct isert_cmd *isert_cmd,
822763b6965SAlex Estrin 		       struct iser_tx_desc *tx_desc)
823763b6965SAlex Estrin {
824763b6965SAlex Estrin 	struct isert_device *device = isert_conn->device;
825763b6965SAlex Estrin 	struct ib_device *ib_dev = device->ib_device;
826763b6965SAlex Estrin 
827763b6965SAlex Estrin 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
828763b6965SAlex Estrin 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
829763b6965SAlex Estrin 
830763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
831763b6965SAlex Estrin }
832763b6965SAlex Estrin 
833b8d26b3bSNicholas Bellinger static int
834b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
835b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
836b8d26b3bSNicholas Bellinger {
837dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
83867cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
839b8d26b3bSNicholas Bellinger 	u64 dma_addr;
840b8d26b3bSNicholas Bellinger 
841b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
842b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
843b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
84424f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
845b8d26b3bSNicholas Bellinger 		return -ENOMEM;
846b8d26b3bSNicholas Bellinger 	}
847b8d26b3bSNicholas Bellinger 
848b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
849b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
850b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
85134efc7dfSJason Gunthorpe 	tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
852b8d26b3bSNicholas Bellinger 
8534c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
8544c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
8554c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
856b8d26b3bSNicholas Bellinger 
857b8d26b3bSNicholas Bellinger 	return 0;
858b8d26b3bSNicholas Bellinger }
859b8d26b3bSNicholas Bellinger 
860b8d26b3bSNicholas Bellinger static void
86195b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
86268a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
863b8d26b3bSNicholas Bellinger {
86495b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
86595b60f07SNicholas Bellinger 
8669679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_send_done;
8679679cc51SChristoph Hellwig 	send_wr->wr_cqe = &tx_desc->tx_cqe;
868422bd0acSJenny Derzhavetz 
869422bd0acSJenny Derzhavetz 	if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
870422bd0acSJenny Derzhavetz 		send_wr->opcode  = IB_WR_SEND_WITH_INV;
871422bd0acSJenny Derzhavetz 		send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
872422bd0acSJenny Derzhavetz 	} else {
873b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_SEND;
874422bd0acSJenny Derzhavetz 	}
875422bd0acSJenny Derzhavetz 
87695b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
877b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
87895b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
879b8d26b3bSNicholas Bellinger }
880b8d26b3bSNicholas Bellinger 
881b8d26b3bSNicholas Bellinger static int
8829679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn)
883b8d26b3bSNicholas Bellinger {
884e01a7674SBart Van Assche 	struct ib_recv_wr rx_wr;
885b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
886b8d26b3bSNicholas Bellinger 	int ret;
887b8d26b3bSNicholas Bellinger 
888b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
8890b089c1eSSagi Grimberg 	sge.addr = isert_conn->login_desc->dma_addr +
8900b089c1eSSagi Grimberg 		isert_get_hdr_offset(isert_conn->login_desc);
891ed1083b2SChristoph Hellwig 	sge.length = ISER_RX_PAYLOAD_SIZE;
89234efc7dfSJason Gunthorpe 	sge.lkey = isert_conn->device->pd->local_dma_lkey;
893b8d26b3bSNicholas Bellinger 
89424f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
895b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
896b8d26b3bSNicholas Bellinger 
8970b089c1eSSagi Grimberg 	isert_conn->login_desc->rx_cqe.done = isert_login_recv_done;
8989679cc51SChristoph Hellwig 
899b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
9000b089c1eSSagi Grimberg 	rx_wr.wr_cqe = &isert_conn->login_desc->rx_cqe;
901b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
902b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
903b8d26b3bSNicholas Bellinger 
904e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL);
9056d1fba0cSJenny Derzhavetz 	if (ret)
90624f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
907b8d26b3bSNicholas Bellinger 
908b8d26b3bSNicholas Bellinger 	return ret;
909b8d26b3bSNicholas Bellinger }
910b8d26b3bSNicholas Bellinger 
911b8d26b3bSNicholas Bellinger static int
912b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
913b8d26b3bSNicholas Bellinger 		   u32 length)
914b8d26b3bSNicholas Bellinger {
915b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
916dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
91767cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
918dac6ab30SSagi Grimberg 	struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
919b8d26b3bSNicholas Bellinger 	int ret;
920b8d26b3bSNicholas Bellinger 
921763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
922b8d26b3bSNicholas Bellinger 
923b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
924b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
925b8d26b3bSNicholas Bellinger 
926b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
927b8d26b3bSNicholas Bellinger 
928b8d26b3bSNicholas Bellinger 	if (length > 0) {
929b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
930b8d26b3bSNicholas Bellinger 
931b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
932b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
933b8d26b3bSNicholas Bellinger 
934b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
935b8d26b3bSNicholas Bellinger 
936b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
937b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
938b8d26b3bSNicholas Bellinger 
939b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
940b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
94134efc7dfSJason Gunthorpe 		tx_dsg->lkey	= isert_conn->device->pd->local_dma_lkey;
942b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
943b8d26b3bSNicholas Bellinger 	}
944b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
945b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
946b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
947b8d26b3bSNicholas Bellinger 			if (ret)
948b8d26b3bSNicholas Bellinger 				return ret;
949b8d26b3bSNicholas Bellinger 
9504366b19cSJenny Derzhavetz 			ret = isert_post_recvm(isert_conn,
9514366b19cSJenny Derzhavetz 					       ISERT_QP_MAX_RECV_DTOS);
952b8d26b3bSNicholas Bellinger 			if (ret)
953b8d26b3bSNicholas Bellinger 				return ret;
954b8d26b3bSNicholas Bellinger 
955128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
956dac6ab30SSagi Grimberg 			mutex_lock(&isert_conn->mutex);
957128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
958dac6ab30SSagi Grimberg 			mutex_unlock(&isert_conn->mutex);
959b8d26b3bSNicholas Bellinger 			goto post_send;
960b8d26b3bSNicholas Bellinger 		}
961b8d26b3bSNicholas Bellinger 
9629679cc51SChristoph Hellwig 		ret = isert_login_post_recv(isert_conn);
963b8d26b3bSNicholas Bellinger 		if (ret)
964b8d26b3bSNicholas Bellinger 			return ret;
965b8d26b3bSNicholas Bellinger 	}
966b8d26b3bSNicholas Bellinger post_send:
9679679cc51SChristoph Hellwig 	ret = isert_login_post_send(isert_conn, tx_desc);
968b8d26b3bSNicholas Bellinger 	if (ret)
969b8d26b3bSNicholas Bellinger 		return ret;
970b8d26b3bSNicholas Bellinger 
971b8d26b3bSNicholas Bellinger 	return 0;
972b8d26b3bSNicholas Bellinger }
973b8d26b3bSNicholas Bellinger 
974b8d26b3bSNicholas Bellinger static void
9752371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
976b8d26b3bSNicholas Bellinger {
9770b089c1eSSagi Grimberg 	struct iser_rx_desc *rx_desc = isert_conn->login_desc;
9782371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
979b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
980b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
981b8d26b3bSNicholas Bellinger 	int size;
982b8d26b3bSNicholas Bellinger 
98324f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
9842371e5daSSagi Grimberg 
9852371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
986b8d26b3bSNicholas Bellinger 
987b8d26b3bSNicholas Bellinger 	if (login->first_request) {
988b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
9890b089c1eSSagi Grimberg 			(struct iscsi_login_req *)isert_get_iscsi_hdr(rx_desc);
990b8d26b3bSNicholas Bellinger 		/*
991b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
992b8d26b3bSNicholas Bellinger 		 * login request PDU.
993b8d26b3bSNicholas Bellinger 		 */
994b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
995b8d26b3bSNicholas Bellinger 		login->current_stage =
996b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
997b8d26b3bSNicholas Bellinger 			 >> 2;
998b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
999b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1000b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1001b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1002b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1003b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1004b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1005b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1006b8d26b3bSNicholas Bellinger 	}
1007b8d26b3bSNicholas Bellinger 
10080b089c1eSSagi Grimberg 	memcpy(&login->req[0], isert_get_iscsi_hdr(rx_desc), ISCSI_HDR_LEN);
1009b8d26b3bSNicholas Bellinger 
1010b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
10114c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
10124c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
10134c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
10140b089c1eSSagi Grimberg 	memcpy(login->req_buf, isert_get_data(rx_desc), size);
1015b8d26b3bSNicholas Bellinger 
10166faaa85fSNicholas Bellinger 	if (login->first_request) {
1017dac6ab30SSagi Grimberg 		complete(&isert_conn->login_comp);
10186faaa85fSNicholas Bellinger 		return;
10196faaa85fSNicholas Bellinger 	}
10206faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1021b8d26b3bSNicholas Bellinger }
1022b8d26b3bSNicholas Bellinger 
1023b8d26b3bSNicholas Bellinger static struct iscsi_cmd
10244366b19cSJenny Derzhavetz *isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
1025b8d26b3bSNicholas Bellinger {
10266700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1027b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1028d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1029b8d26b3bSNicholas Bellinger 
1030676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1031d703ce2fSNicholas Bellinger 	if (!cmd) {
103224f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1033b8d26b3bSNicholas Bellinger 		return NULL;
1034b8d26b3bSNicholas Bellinger 	}
1035d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1036b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1037d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
10384366b19cSJenny Derzhavetz 	isert_cmd->rx_desc = rx_desc;
1039b8d26b3bSNicholas Bellinger 
1040d703ce2fSNicholas Bellinger 	return cmd;
1041b8d26b3bSNicholas Bellinger }
1042b8d26b3bSNicholas Bellinger 
1043b8d26b3bSNicholas Bellinger static int
1044b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1045d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1046d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1047b8d26b3bSNicholas Bellinger {
1048b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1049b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1050b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1051b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
10529fd60088SJenny Derzhavetz 	unsigned int data_len;
1053b8d26b3bSNicholas Bellinger 
1054b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1055b8d26b3bSNicholas Bellinger 	if (rc < 0)
1056b8d26b3bSNicholas Bellinger 		return rc;
1057b8d26b3bSNicholas Bellinger 
1058b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1059b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1060b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
10619fd60088SJenny Derzhavetz 	data_len = cmd->se_cmd.data_length;
1062b8d26b3bSNicholas Bellinger 
10639fd60088SJenny Derzhavetz 	if (imm_data && imm_data_len == data_len)
10649fd60088SJenny Derzhavetz 		cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1065b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1066b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1067b8d26b3bSNicholas Bellinger 		return 0;
1068b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1069b8d26b3bSNicholas Bellinger 		dump_payload = true;
1070b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1071b8d26b3bSNicholas Bellinger 	}
1072b8d26b3bSNicholas Bellinger 
1073b8d26b3bSNicholas Bellinger 	if (!imm_data)
1074b8d26b3bSNicholas Bellinger 		return 0;
1075b8d26b3bSNicholas Bellinger 
10769fd60088SJenny Derzhavetz 	if (imm_data_len != data_len) {
1077b8d26b3bSNicholas Bellinger 		sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
10789fd60088SJenny Derzhavetz 		sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
10790b089c1eSSagi Grimberg 				    isert_get_data(rx_desc), imm_data_len);
10809fd60088SJenny Derzhavetz 		isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
10819fd60088SJenny Derzhavetz 			  sg_nents, imm_data_len);
10829fd60088SJenny Derzhavetz 	} else {
10839fd60088SJenny Derzhavetz 		sg_init_table(&isert_cmd->sg, 1);
10849fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_sg = &isert_cmd->sg;
10859fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_nents = 1;
10860b089c1eSSagi Grimberg 		sg_set_buf(&isert_cmd->sg, isert_get_data(rx_desc),
10870b089c1eSSagi Grimberg 				imm_data_len);
10889fd60088SJenny Derzhavetz 		isert_dbg("Transfer Immediate imm_data_len: %d\n",
10899fd60088SJenny Derzhavetz 			  imm_data_len);
10909fd60088SJenny Derzhavetz 	}
1091b8d26b3bSNicholas Bellinger 
1092b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1093b8d26b3bSNicholas Bellinger 
1094b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1095b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1096b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1097b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1098b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1099b8d26b3bSNicholas Bellinger 	}
1100b8d26b3bSNicholas Bellinger 
1101b8d26b3bSNicholas Bellinger sequence_cmd:
1102561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1103b8d26b3bSNicholas Bellinger 
1104f7a95c90SZou Wei 	if (!rc && !dump_payload && unsol_data)
11050300b114SBart Van Assche 		iscsit_set_unsolicited_dataout(cmd);
11066cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1107afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1108b8d26b3bSNicholas Bellinger 
1109b8d26b3bSNicholas Bellinger 	return 0;
1110b8d26b3bSNicholas Bellinger }
1111b8d26b3bSNicholas Bellinger 
1112b8d26b3bSNicholas Bellinger static int
1113b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1114b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1115b8d26b3bSNicholas Bellinger {
1116b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1117b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1118b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1119b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1120b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1121b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1122b8d26b3bSNicholas Bellinger 
1123b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1124b8d26b3bSNicholas Bellinger 	if (rc < 0)
1125b8d26b3bSNicholas Bellinger 		return rc;
1126b8d26b3bSNicholas Bellinger 	else if (!cmd)
1127b8d26b3bSNicholas Bellinger 		return 0;
1128b8d26b3bSNicholas Bellinger 	/*
1129b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1130b8d26b3bSNicholas Bellinger 	 */
1131b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
113224f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1133b8d26b3bSNicholas Bellinger 		dump_stack();
1134b8d26b3bSNicholas Bellinger 		return -1;
1135b8d26b3bSNicholas Bellinger 	}
1136b8d26b3bSNicholas Bellinger 
11374c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
11384c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
11394c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
11404c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1141b8d26b3bSNicholas Bellinger 
1142b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1143b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1144b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1145b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1146b8d26b3bSNicholas Bellinger 	/*
1147b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1148b8d26b3bSNicholas Bellinger 	 */
1149b8d26b3bSNicholas Bellinger 	if (page_off) {
11504c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1151b8d26b3bSNicholas Bellinger 		dump_stack();
1152b8d26b3bSNicholas Bellinger 		return -1;
1153b8d26b3bSNicholas Bellinger 	}
11544c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
11554c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
11560b089c1eSSagi Grimberg 		  sg_nents, isert_get_data(rx_desc), unsol_data_len);
1157b8d26b3bSNicholas Bellinger 
11580b089c1eSSagi Grimberg 	sg_copy_from_buffer(sg_start, sg_nents, isert_get_data(rx_desc),
1159b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1160b8d26b3bSNicholas Bellinger 
1161b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1162b8d26b3bSNicholas Bellinger 	if (rc < 0)
1163b8d26b3bSNicholas Bellinger 		return rc;
1164b8d26b3bSNicholas Bellinger 
11654366b19cSJenny Derzhavetz 	/*
11664366b19cSJenny Derzhavetz 	 * multiple data-outs on the same command can arrive -
11674366b19cSJenny Derzhavetz 	 * so post the buffer before hand
11684366b19cSJenny Derzhavetz 	 */
1169c97119b6SMax Gurtovoy 	return isert_post_recv(isert_conn, rx_desc);
1170b8d26b3bSNicholas Bellinger }
1171b8d26b3bSNicholas Bellinger 
1172b8d26b3bSNicholas Bellinger static int
1173778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1174d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1175d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1176778de368SNicholas Bellinger {
1177778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1178778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1179778de368SNicholas Bellinger 	int rc;
1180778de368SNicholas Bellinger 
1181778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1182778de368SNicholas Bellinger 	if (rc < 0)
1183778de368SNicholas Bellinger 		return rc;
1184778de368SNicholas Bellinger 	/*
1185778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1186778de368SNicholas Bellinger 	 */
1187778de368SNicholas Bellinger 
1188778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1189778de368SNicholas Bellinger }
1190778de368SNicholas Bellinger 
1191778de368SNicholas Bellinger static int
1192adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1193d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1194d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1195adb54c29SNicholas Bellinger {
1196adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1197adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1198adb54c29SNicholas Bellinger 	int rc;
1199b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1200adb54c29SNicholas Bellinger 
1201adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1202adb54c29SNicholas Bellinger 	if (rc < 0)
1203adb54c29SNicholas Bellinger 		return rc;
1204adb54c29SNicholas Bellinger 
1205b44a2b67SSagi Grimberg 	if (payload_length) {
1206adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
120793b80f29SLeon Romanovsky 		if (!text_in)
1208adb54c29SNicholas Bellinger 			return -ENOMEM;
1209adb54c29SNicholas Bellinger 	}
1210adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1211adb54c29SNicholas Bellinger 
12120b089c1eSSagi Grimberg 	memcpy(cmd->text_in_ptr, isert_get_data(rx_desc), payload_length);
1213adb54c29SNicholas Bellinger 
1214adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1215adb54c29SNicholas Bellinger }
1216adb54c29SNicholas Bellinger 
1217adb54c29SNicholas Bellinger static int
1218b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1219b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1220b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1221b8d26b3bSNicholas Bellinger {
12220b089c1eSSagi Grimberg 	struct iscsi_hdr *hdr = isert_get_iscsi_hdr(rx_desc);
1223b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1224b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1225b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1226b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1227b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1228b8d26b3bSNicholas Bellinger 
1229fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1230ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
123124f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1232ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1233ca40d24eSNicholas Bellinger 		return 0;
1234ca40d24eSNicholas Bellinger 	}
1235ca40d24eSNicholas Bellinger 
1236b8d26b3bSNicholas Bellinger 	switch (opcode) {
1237b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
12384366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1239b8d26b3bSNicholas Bellinger 		if (!cmd)
1240b8d26b3bSNicholas Bellinger 			break;
1241b8d26b3bSNicholas Bellinger 
1242d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1243b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1244b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1245b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1246b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1247422bd0acSJenny Derzhavetz 		isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
1248b8d26b3bSNicholas Bellinger 
1249d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1250b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1251b8d26b3bSNicholas Bellinger 		break;
1252b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
12534366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1254b8d26b3bSNicholas Bellinger 		if (!cmd)
1255b8d26b3bSNicholas Bellinger 			break;
1256b8d26b3bSNicholas Bellinger 
1257d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1258d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1259778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1260b8d26b3bSNicholas Bellinger 		break;
1261b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1262b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1263b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1264b8d26b3bSNicholas Bellinger 		break;
1265b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
12664366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1267b8d26b3bSNicholas Bellinger 		if (!cmd)
1268b8d26b3bSNicholas Bellinger 			break;
1269b8d26b3bSNicholas Bellinger 
1270b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1271b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1272b8d26b3bSNicholas Bellinger 		break;
1273b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
12744366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1275b8d26b3bSNicholas Bellinger 		if (!cmd)
1276b8d26b3bSNicholas Bellinger 			break;
1277b8d26b3bSNicholas Bellinger 
1278b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1279b8d26b3bSNicholas Bellinger 		break;
1280adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
12814366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1282e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
12834366b19cSJenny Derzhavetz 		else
12844366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
12854366b19cSJenny Derzhavetz 
1286e4f4e801SSagi Grimberg 		if (!cmd)
1287e4f4e801SSagi Grimberg 			break;
1288adb54c29SNicholas Bellinger 
1289d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1290d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1291adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1292adb54c29SNicholas Bellinger 		break;
1293b8d26b3bSNicholas Bellinger 	default:
129424f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1295b8d26b3bSNicholas Bellinger 		dump_stack();
1296b8d26b3bSNicholas Bellinger 		break;
1297b8d26b3bSNicholas Bellinger 	}
1298b8d26b3bSNicholas Bellinger 
1299b8d26b3bSNicholas Bellinger 	return ret;
1300b8d26b3bSNicholas Bellinger }
1301b8d26b3bSNicholas Bellinger 
1302b8d26b3bSNicholas Bellinger static void
13039679cc51SChristoph Hellwig isert_print_wc(struct ib_wc *wc, const char *type)
1304b8d26b3bSNicholas Bellinger {
13059679cc51SChristoph Hellwig 	if (wc->status != IB_WC_WR_FLUSH_ERR)
13069679cc51SChristoph Hellwig 		isert_err("%s failure: %s (%d) vend_err %x\n", type,
13079679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status,
13089679cc51SChristoph Hellwig 			  wc->vendor_err);
13099679cc51SChristoph Hellwig 	else
13109679cc51SChristoph Hellwig 		isert_dbg("%s failure: %s (%d)\n", type,
13119679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status);
13129679cc51SChristoph Hellwig }
13139679cc51SChristoph Hellwig 
13149679cc51SChristoph Hellwig static void
13159679cc51SChristoph Hellwig isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
13169679cc51SChristoph Hellwig {
13179679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
13189679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
13199679cc51SChristoph Hellwig 	struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
13200b089c1eSSagi Grimberg 	struct iscsi_hdr *hdr = isert_get_iscsi_hdr(rx_desc);
13210b089c1eSSagi Grimberg 	struct iser_ctrl *iser_ctrl = isert_get_iser_hdr(rx_desc);
1322b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1323b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1324b8d26b3bSNicholas Bellinger 
13259679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
13269679cc51SChristoph Hellwig 		isert_print_wc(wc, "recv");
13279679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
13289679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
13299679cc51SChristoph Hellwig 		return;
13309679cc51SChristoph Hellwig 	}
13319679cc51SChristoph Hellwig 
13327a56dc88SSagi Grimberg 	rx_desc->in_use = true;
13337a56dc88SSagi Grimberg 
13349679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
13350b089c1eSSagi Grimberg 			ISER_RX_SIZE, DMA_FROM_DEVICE);
13369679cc51SChristoph Hellwig 
13379679cc51SChristoph Hellwig 	isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
13389679cc51SChristoph Hellwig 		 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
13399679cc51SChristoph Hellwig 		 (int)(wc->byte_len - ISER_HEADERS_LEN));
13409679cc51SChristoph Hellwig 
1341d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1342b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1343d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1344d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1345d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
13464c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1347b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1348b8d26b3bSNicholas Bellinger 		}
1349d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1350d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1351d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
13524c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1353b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1354b8d26b3bSNicholas Bellinger 		}
1355b8d26b3bSNicholas Bellinger 
135624f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1357b8d26b3bSNicholas Bellinger 		break;
1358b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
135924f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1360b8d26b3bSNicholas Bellinger 		break;
1361b8d26b3bSNicholas Bellinger 	default:
1362d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1363b8d26b3bSNicholas Bellinger 		break;
1364b8d26b3bSNicholas Bellinger 	}
1365b8d26b3bSNicholas Bellinger 
13666c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1367b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
13689679cc51SChristoph Hellwig 
13699679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
13700b089c1eSSagi Grimberg 			ISER_RX_SIZE, DMA_FROM_DEVICE);
1371b8d26b3bSNicholas Bellinger }
1372b8d26b3bSNicholas Bellinger 
1373b8d26b3bSNicholas Bellinger static void
13749679cc51SChristoph Hellwig isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1375b8d26b3bSNicholas Bellinger {
13769679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1377fce50a2fSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->device->ib_device;
1378b8d26b3bSNicholas Bellinger 
13799679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
13809679cc51SChristoph Hellwig 		isert_print_wc(wc, "login recv");
13819679cc51SChristoph Hellwig 		return;
1382b8d26b3bSNicholas Bellinger 	}
1383b8d26b3bSNicholas Bellinger 
13840b089c1eSSagi Grimberg 	ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_desc->dma_addr,
13850b089c1eSSagi Grimberg 			ISER_RX_SIZE, DMA_FROM_DEVICE);
1386b8d26b3bSNicholas Bellinger 
13879679cc51SChristoph Hellwig 	isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
1388b8d26b3bSNicholas Bellinger 
13892371e5daSSagi Grimberg 	if (isert_conn->conn) {
13902371e5daSSagi Grimberg 		struct iscsi_login *login = isert_conn->conn->conn_login;
13912371e5daSSagi Grimberg 
13922371e5daSSagi Grimberg 		if (login && !login->first_request)
13932371e5daSSagi Grimberg 			isert_rx_login_req(isert_conn);
13942371e5daSSagi Grimberg 	}
13959679cc51SChristoph Hellwig 
1396dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
13972371e5daSSagi Grimberg 	complete(&isert_conn->login_req_comp);
1398dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
1399b8d26b3bSNicholas Bellinger 
14000b089c1eSSagi Grimberg 	ib_dma_sync_single_for_device(ib_dev, isert_conn->login_desc->dma_addr,
14010b089c1eSSagi Grimberg 				ISER_RX_SIZE, DMA_FROM_DEVICE);
1402b8d26b3bSNicholas Bellinger }
1403b8d26b3bSNicholas Bellinger 
1404e3d7e4c3SSagi Grimberg static void
140538a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
1406e3d7e4c3SSagi Grimberg {
140738a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
140838a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
1409e3d7e4c3SSagi Grimberg 
141038a2d0d4SChristoph Hellwig 	if (!cmd->rw.nr_ops)
141138a2d0d4SChristoph Hellwig 		return;
141238a2d0d4SChristoph Hellwig 
141338a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
141438a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
141538a2d0d4SChristoph Hellwig 				conn->cm_id->port_num, se_cmd->t_data_sg,
141638a2d0d4SChristoph Hellwig 				se_cmd->t_data_nents, se_cmd->t_prot_sg,
141738a2d0d4SChristoph Hellwig 				se_cmd->t_prot_nents, dir);
141838a2d0d4SChristoph Hellwig 	} else {
141938a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
142038a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
1421e3d7e4c3SSagi Grimberg 	}
1422e3d7e4c3SSagi Grimberg 
142338a2d0d4SChristoph Hellwig 	cmd->rw.nr_ops = 0;
142459464ef4SVu Pham }
142559464ef4SVu Pham 
142659464ef4SVu Pham static void
142703e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1428b8d26b3bSNicholas Bellinger {
1429d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1430b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1431186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1432e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1433b8d26b3bSNicholas Bellinger 
14344c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1435b8d26b3bSNicholas Bellinger 
1436b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1437b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1438b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1439b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14405159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1441b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1442b8d26b3bSNicholas Bellinger 
144303e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1444b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
144503e7848aSNicholas Bellinger 			/*
144603e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
144703e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
144803e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
144903e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
145003e7848aSNicholas Bellinger 			 */
145103e7848aSNicholas Bellinger 			if (comp_err &&
145203e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
145303e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
145403e7848aSNicholas Bellinger 
1455afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
145603e7848aSNicholas Bellinger 			}
145703e7848aSNicholas Bellinger 		}
1458b8d26b3bSNicholas Bellinger 
145938a2d0d4SChristoph Hellwig 		isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1460186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1461186a9647SNicholas Bellinger 		break;
1462b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1463186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1464186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14655159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1466186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1467186a9647SNicholas Bellinger 
1468b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1469b8d26b3bSNicholas Bellinger 		break;
1470b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1471b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1472adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1473e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1474e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1475e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1476e4f4e801SSagi Grimberg 			break;
1477e4f4e801SSagi Grimberg 
1478b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1479b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14805159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1481b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1482b8d26b3bSNicholas Bellinger 
1483b8d26b3bSNicholas Bellinger 		/*
1484b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1485b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1486b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1487b8d26b3bSNicholas Bellinger 		 */
1488b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
148911378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
14903df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1491b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1492b8d26b3bSNicholas Bellinger 			break;
1493b8d26b3bSNicholas Bellinger 		}
1494df561f66SGustavo A. R. Silva 		fallthrough;
1495b8d26b3bSNicholas Bellinger 	default:
1496d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1497b8d26b3bSNicholas Bellinger 		break;
1498b8d26b3bSNicholas Bellinger 	}
1499b8d26b3bSNicholas Bellinger }
1500b8d26b3bSNicholas Bellinger 
1501b8d26b3bSNicholas Bellinger static void
1502b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1503b8d26b3bSNicholas Bellinger {
1504b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
15054c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1506b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1507b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1508b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1509b8d26b3bSNicholas Bellinger 	}
1510b8d26b3bSNicholas Bellinger }
1511b8d26b3bSNicholas Bellinger 
1512b8d26b3bSNicholas Bellinger static void
1513b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
151403e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1515b8d26b3bSNicholas Bellinger {
1516dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
15174c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1518dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1519dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1520dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1521b8d26b3bSNicholas Bellinger 	}
1522b8d26b3bSNicholas Bellinger 
1523b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
152403e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1525b8d26b3bSNicholas Bellinger }
1526b8d26b3bSNicholas Bellinger 
152796b7973eSSagi Grimberg static int
152896b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1529f93f3a70SSagi Grimberg {
15309e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
153196b7973eSSagi Grimberg 	int ret;
1532f93f3a70SSagi Grimberg 
153396b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
15349e961ae7SSagi Grimberg 	if (ret) {
153524f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
15369e961ae7SSagi Grimberg 		goto fail_mr_status;
15379e961ae7SSagi Grimberg 	}
153896b7973eSSagi Grimberg 
15399e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
154096b7973eSSagi Grimberg 		u64 sec_offset_err;
15419e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
15429e961ae7SSagi Grimberg 
15439e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
15449e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
15459e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
15469e961ae7SSagi Grimberg 			break;
15479e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
15489e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
15499e961ae7SSagi Grimberg 			break;
15509e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
15519e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
15529e961ae7SSagi Grimberg 			break;
15539e961ae7SSagi Grimberg 		}
155496b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
155596b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
15568dd992fbSDavid Disseldorp 		se_cmd->sense_info = sec_offset_err + se_cmd->t_task_lba;
15579e961ae7SSagi Grimberg 
15584c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
15599e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
15609e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
15618dd992fbSDavid Disseldorp 			  (unsigned long long)se_cmd->sense_info,
15629e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
15639e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
15649e961ae7SSagi Grimberg 		ret = 1;
15659e961ae7SSagi Grimberg 	}
15669e961ae7SSagi Grimberg 
15679e961ae7SSagi Grimberg fail_mr_status:
156896b7973eSSagi Grimberg 	return ret;
156996b7973eSSagi Grimberg }
157096b7973eSSagi Grimberg 
157196b7973eSSagi Grimberg static void
15729679cc51SChristoph Hellwig isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
157396b7973eSSagi Grimberg {
15749679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1575dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
15769679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1577e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
15789679cc51SChristoph Hellwig 	struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
157996b7973eSSagi Grimberg 	int ret = 0;
158096b7973eSSagi Grimberg 
15819679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
15829679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma write");
15839679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
15849679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
15859679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
15869679cc51SChristoph Hellwig 		return;
15879679cc51SChristoph Hellwig 	}
15889679cc51SChristoph Hellwig 
15899679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
15909679cc51SChristoph Hellwig 
1591e9a53e73SIsrael Rukshin 	ret = isert_check_pi_status(cmd, isert_cmd->rw.reg->mr);
159238a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
159396b7973eSSagi Grimberg 
1594555a65f6SNicholas Bellinger 	if (ret) {
1595555a65f6SNicholas Bellinger 		/*
1596555a65f6SNicholas Bellinger 		 * transport_generic_request_failure() expects to have
1597555a65f6SNicholas Bellinger 		 * plus two references to handle queue-full, so re-add
1598555a65f6SNicholas Bellinger 		 * one here as target-core will have already dropped
1599555a65f6SNicholas Bellinger 		 * it after the first isert_put_datain() callback.
1600555a65f6SNicholas Bellinger 		 */
1601555a65f6SNicholas Bellinger 		kref_get(&cmd->cmd_kref);
1602555a65f6SNicholas Bellinger 		transport_generic_request_failure(cmd, cmd->pi_err);
1603555a65f6SNicholas Bellinger 	} else {
1604555a65f6SNicholas Bellinger 		/*
1605555a65f6SNicholas Bellinger 		 * XXX: isert_put_response() failure is not retried.
1606555a65f6SNicholas Bellinger 		 */
1607555a65f6SNicholas Bellinger 		ret = isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
16089e961ae7SSagi Grimberg 		if (ret)
1609555a65f6SNicholas Bellinger 			pr_warn_ratelimited("isert_put_response() ret: %d\n", ret);
1610555a65f6SNicholas Bellinger 	}
1611f93f3a70SSagi Grimberg }
1612f93f3a70SSagi Grimberg 
1613f93f3a70SSagi Grimberg static void
16149679cc51SChristoph Hellwig isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1615b8d26b3bSNicholas Bellinger {
16169679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16179679cc51SChristoph Hellwig 	struct isert_device *device = isert_conn->device;
16189679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1619e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
1620d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1621b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
16225bac4b1aSSagi Grimberg 	int ret = 0;
1623b8d26b3bSNicholas Bellinger 
16249679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16259679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma read");
16269679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16279679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16289679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
16299679cc51SChristoph Hellwig 		return;
16309679cc51SChristoph Hellwig 	}
16319679cc51SChristoph Hellwig 
16329679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
16339679cc51SChristoph Hellwig 
1634b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
163538a2d0d4SChristoph Hellwig 
163638a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd))
1637e9a53e73SIsrael Rukshin 		ret = isert_check_pi_status(se_cmd, isert_cmd->rw.reg->mr);
163838a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
163938a2d0d4SChristoph Hellwig 	cmd->write_data_done = 0;
1640b8d26b3bSNicholas Bellinger 
164124f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1642b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1643b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1644b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1645b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1646b8d26b3bSNicholas Bellinger 
1647555a65f6SNicholas Bellinger 	/*
1648555a65f6SNicholas Bellinger 	 * transport_generic_request_failure() will drop the extra
1649555a65f6SNicholas Bellinger 	 * se_cmd->cmd_kref reference after T10-PI error, and handle
1650555a65f6SNicholas Bellinger 	 * any non-zero ->queue_status() callback error retries.
1651555a65f6SNicholas Bellinger 	 */
1652555a65f6SNicholas Bellinger 	if (ret)
1653555a65f6SNicholas Bellinger 		transport_generic_request_failure(se_cmd, se_cmd->pi_err);
1654555a65f6SNicholas Bellinger 	else
1655b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1656b8d26b3bSNicholas Bellinger }
1657b8d26b3bSNicholas Bellinger 
1658b8d26b3bSNicholas Bellinger static void
1659b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1660b8d26b3bSNicholas Bellinger {
1661b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1662b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1663b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1664dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1665d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1666b8d26b3bSNicholas Bellinger 
16674c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
16684c22e07fSSagi Grimberg 
1669b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1670b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1671b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1672df561f66SGustavo A. R. Silva 		fallthrough;
1673d2271bd0SBart Van Assche 	case ISTATE_SEND_REJECT:
1674d2271bd0SBart Van Assche 	case ISTATE_SEND_TEXTRSP:
1675b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
16764c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
16774c22e07fSSagi Grimberg 				     ib_dev, false);
16783df8f68aSNicholas Bellinger 		break;
1679b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1680b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1681b8d26b3bSNicholas Bellinger 		break;
1682b8d26b3bSNicholas Bellinger 	default:
16834c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1684b8d26b3bSNicholas Bellinger 		dump_stack();
1685b8d26b3bSNicholas Bellinger 		break;
1686b8d26b3bSNicholas Bellinger 	}
1687b8d26b3bSNicholas Bellinger }
1688b8d26b3bSNicholas Bellinger 
1689b8d26b3bSNicholas Bellinger static void
16909679cc51SChristoph Hellwig isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
1691b8d26b3bSNicholas Bellinger {
16929679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16939679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
16949679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1695b8d26b3bSNicholas Bellinger 
16969679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16979679cc51SChristoph Hellwig 		isert_print_wc(wc, "login send");
16989679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16999679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17009679cc51SChristoph Hellwig 	}
17019679cc51SChristoph Hellwig 
17029679cc51SChristoph Hellwig 	isert_unmap_tx_desc(tx_desc, ib_dev);
17039679cc51SChristoph Hellwig }
17049679cc51SChristoph Hellwig 
17059679cc51SChristoph Hellwig static void
17069679cc51SChristoph Hellwig isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
17079679cc51SChristoph Hellwig {
17089679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17099679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
17109679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1711e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
17129679cc51SChristoph Hellwig 
17139679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17149679cc51SChristoph Hellwig 		isert_print_wc(wc, "send");
17159679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17169679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17179679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
17189679cc51SChristoph Hellwig 		return;
17199679cc51SChristoph Hellwig 	}
17209679cc51SChristoph Hellwig 
17219679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
17229679cc51SChristoph Hellwig 
17239679cc51SChristoph Hellwig 	switch (isert_cmd->iscsi_cmd->i_state) {
17249679cc51SChristoph Hellwig 	case ISTATE_SEND_TASKMGTRSP:
17259679cc51SChristoph Hellwig 	case ISTATE_SEND_LOGOUTRSP:
17269679cc51SChristoph Hellwig 	case ISTATE_SEND_REJECT:
17279679cc51SChristoph Hellwig 	case ISTATE_SEND_TEXTRSP:
1728b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1729b8d26b3bSNicholas Bellinger 
1730b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1731b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1732b8d26b3bSNicholas Bellinger 		return;
1733b8d26b3bSNicholas Bellinger 	default:
17349679cc51SChristoph Hellwig 		isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
17359679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1736b8d26b3bSNicholas Bellinger 		break;
1737b8d26b3bSNicholas Bellinger 	}
1738b8d26b3bSNicholas Bellinger }
1739b8d26b3bSNicholas Bellinger 
1740b8d26b3bSNicholas Bellinger static int
1741b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1742b8d26b3bSNicholas Bellinger {
1743b8d26b3bSNicholas Bellinger 	int ret;
1744b8d26b3bSNicholas Bellinger 
17454366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
1746c97119b6SMax Gurtovoy 	if (ret)
17474366b19cSJenny Derzhavetz 		return ret;
17484366b19cSJenny Derzhavetz 
1749e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr, NULL);
1750b8d26b3bSNicholas Bellinger 	if (ret) {
175124f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
1752b8d26b3bSNicholas Bellinger 		return ret;
1753b8d26b3bSNicholas Bellinger 	}
1754b8d26b3bSNicholas Bellinger 	return ret;
1755b8d26b3bSNicholas Bellinger }
1756b8d26b3bSNicholas Bellinger 
1757b8d26b3bSNicholas Bellinger static int
1758b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1759b8d26b3bSNicholas Bellinger {
1760d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
17616700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1762b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1763b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1764b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1765b8d26b3bSNicholas Bellinger 
1766b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1767b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1768b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1769b8d26b3bSNicholas Bellinger 	/*
1770b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1771b8d26b3bSNicholas Bellinger 	 */
1772b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1773b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1774b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1775dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
177667cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1777b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1778dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1779b8d26b3bSNicholas Bellinger 
1780b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1781b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1782b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1783b8d26b3bSNicholas Bellinger 
1784b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1785b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1786dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1787b8d26b3bSNicholas Bellinger 
1788dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1789dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1790b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1791def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1792def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1793b8d26b3bSNicholas Bellinger 
1794dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1795dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1796dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
179734efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1798b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1799b8d26b3bSNicholas Bellinger 	}
1800b8d26b3bSNicholas Bellinger 
180168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1802b8d26b3bSNicholas Bellinger 
18034c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
1804b8d26b3bSNicholas Bellinger 
1805b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1806b8d26b3bSNicholas Bellinger }
1807b8d26b3bSNicholas Bellinger 
1808131e6abcSNicholas Bellinger static void
1809131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1810131e6abcSNicholas Bellinger {
1811131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18126700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1813131e6abcSNicholas Bellinger 
1814131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
1815131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
1816131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
1817131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1818131e6abcSNicholas Bellinger 
1819131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
1820131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
182138a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1822131e6abcSNicholas Bellinger }
1823131e6abcSNicholas Bellinger 
1824e70beee7SNicholas Bellinger static enum target_prot_op
1825e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
1826e70beee7SNicholas Bellinger {
18276700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1828dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1829e70beee7SNicholas Bellinger 
183023a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
183123a548eeSSagi Grimberg 		if (device->pi_capable) {
183224f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
183323a548eeSSagi Grimberg 			isert_conn->pi_support = true;
1834e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
183523a548eeSSagi Grimberg 		}
183623a548eeSSagi Grimberg 	}
183723a548eeSSagi Grimberg 
183824f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
183923a548eeSSagi Grimberg 	isert_conn->pi_support = false;
1840e70beee7SNicholas Bellinger 
1841e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
1842e70beee7SNicholas Bellinger }
1843e70beee7SNicholas Bellinger 
1844b8d26b3bSNicholas Bellinger static int
1845b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1846b8d26b3bSNicholas Bellinger 		bool nopout_response)
1847b8d26b3bSNicholas Bellinger {
1848d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18496700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1850b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1851b8d26b3bSNicholas Bellinger 
1852b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1853b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1854b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1855b8d26b3bSNicholas Bellinger 			       nopout_response);
1856b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
185768a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1858b8d26b3bSNicholas Bellinger 
18594c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
1860b8d26b3bSNicholas Bellinger 
1861b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1862b8d26b3bSNicholas Bellinger }
1863b8d26b3bSNicholas Bellinger 
1864b8d26b3bSNicholas Bellinger static int
1865b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1866b8d26b3bSNicholas Bellinger {
1867d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18686700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1869b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1870b8d26b3bSNicholas Bellinger 
1871b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1872b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1873b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1874b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
187568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1876b8d26b3bSNicholas Bellinger 
18774c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
1878b8d26b3bSNicholas Bellinger 
1879b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1880b8d26b3bSNicholas Bellinger }
1881b8d26b3bSNicholas Bellinger 
1882b8d26b3bSNicholas Bellinger static int
1883b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1884b8d26b3bSNicholas Bellinger {
1885d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18866700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1887b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1888b8d26b3bSNicholas Bellinger 
1889b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1890b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1891b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1892b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
189368a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1894b8d26b3bSNicholas Bellinger 
18954c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
1896b8d26b3bSNicholas Bellinger 
1897b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1898b8d26b3bSNicholas Bellinger }
1899b8d26b3bSNicholas Bellinger 
1900b8d26b3bSNicholas Bellinger static int
1901b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1902b8d26b3bSNicholas Bellinger {
1903d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19046700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1905b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1906dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
190767cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
19083df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
19093df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
19103df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1911b8d26b3bSNicholas Bellinger 
1912b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
19133df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
1914b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
19153df8f68aSNicholas Bellinger 
19163df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
1917dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
19183df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
19193df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
1920def4a6ffSAlexey Khoroshilov 	if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1921def4a6ffSAlexey Khoroshilov 		return -ENOMEM;
1922dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1923dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
19243df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
192534efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
19263df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
19273df8f68aSNicholas Bellinger 
192868a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1929b8d26b3bSNicholas Bellinger 
19304c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
1931b8d26b3bSNicholas Bellinger 
1932b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1933b8d26b3bSNicholas Bellinger }
1934b8d26b3bSNicholas Bellinger 
1935b8d26b3bSNicholas Bellinger static int
1936adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1937adb54c29SNicholas Bellinger {
1938d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19396700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1940adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1941adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
1942adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1943adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
1944adb54c29SNicholas Bellinger 	int rc;
1945adb54c29SNicholas Bellinger 
1946adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
194722c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
1948adb54c29SNicholas Bellinger 	if (rc < 0)
1949adb54c29SNicholas Bellinger 		return rc;
1950adb54c29SNicholas Bellinger 
1951adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
1952adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1953adb54c29SNicholas Bellinger 
1954adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
1955dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
195667cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1957adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1958adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
1959adb54c29SNicholas Bellinger 
1960adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1961adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1962def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1963def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1964adb54c29SNicholas Bellinger 
1965adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
1966adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1967adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
196834efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1969adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1970adb54c29SNicholas Bellinger 	}
197168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1972adb54c29SNicholas Bellinger 
1973f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
1974adb54c29SNicholas Bellinger 
1975adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1976adb54c29SNicholas Bellinger }
1977adb54c29SNicholas Bellinger 
19783d73cf1aSSagi Grimberg static inline void
1979dae6345aSIsrael Rukshin isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_domain *domain)
19809e961ae7SSagi Grimberg {
198178eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
19823d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
19833d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
19843d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
198578eda2bbSSagi Grimberg 	/*
198678eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
198778eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
198878eda2bbSSagi Grimberg 	 * from se_cmd.
198978eda2bbSSagi Grimberg 	 */
199078eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
199178eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
199278eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
199378eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
199478eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
199578eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
1996ec53a2a6SMax Gurtovoy }
19973d73cf1aSSagi Grimberg 
19983d73cf1aSSagi Grimberg static int
19993d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
20003d73cf1aSSagi Grimberg {
200138a2d0d4SChristoph Hellwig 	memset(sig_attrs, 0, sizeof(*sig_attrs));
200238a2d0d4SChristoph Hellwig 
20039e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
20049e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
20059e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
200678eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
2007dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
20089e961ae7SSagi Grimberg 		break;
20099e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
20109e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
201178eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
2012dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
20139e961ae7SSagi Grimberg 		break;
20149e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
20159e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
2016dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
2017dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
20189e961ae7SSagi Grimberg 		break;
20199e961ae7SSagi Grimberg 	default:
202024f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
20219e961ae7SSagi Grimberg 		return -EINVAL;
20229e961ae7SSagi Grimberg 	}
20239e961ae7SSagi Grimberg 
202426232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD)
202526232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
202626232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG)
202726232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
202826232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)
202926232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
203026232872SMax Gurtovoy 
20319e961ae7SSagi Grimberg 	return 0;
20329e961ae7SSagi Grimberg }
20339e961ae7SSagi Grimberg 
20349e961ae7SSagi Grimberg static int
203538a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
203638a2d0d4SChristoph Hellwig 		struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
20379e961ae7SSagi Grimberg {
203838a2d0d4SChristoph Hellwig 	struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
203938a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
204038a2d0d4SChristoph Hellwig 	u8 port_num = conn->cm_id->port_num;
204138a2d0d4SChristoph Hellwig 	u64 addr;
204238a2d0d4SChristoph Hellwig 	u32 rkey, offset;
20439e961ae7SSagi Grimberg 	int ret;
20449e961ae7SSagi Grimberg 
204566f53e6fSBharat Potnuri 	if (cmd->ctx_init_done)
204666f53e6fSBharat Potnuri 		goto rdma_ctx_post;
204766f53e6fSBharat Potnuri 
204838a2d0d4SChristoph Hellwig 	if (dir == DMA_FROM_DEVICE) {
204938a2d0d4SChristoph Hellwig 		addr = cmd->write_va;
205038a2d0d4SChristoph Hellwig 		rkey = cmd->write_stag;
205138a2d0d4SChristoph Hellwig 		offset = cmd->iscsi_cmd->write_data_done;
205238a2d0d4SChristoph Hellwig 	} else {
205338a2d0d4SChristoph Hellwig 		addr = cmd->read_va;
205438a2d0d4SChristoph Hellwig 		rkey = cmd->read_stag;
205538a2d0d4SChristoph Hellwig 		offset = 0;
205638a2d0d4SChristoph Hellwig 	}
205738a2d0d4SChristoph Hellwig 
205838a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
205938a2d0d4SChristoph Hellwig 		struct ib_sig_attrs sig_attrs;
206038a2d0d4SChristoph Hellwig 
20619e961ae7SSagi Grimberg 		ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
20629e961ae7SSagi Grimberg 		if (ret)
2063e3d7e4c3SSagi Grimberg 			return ret;
2064e3d7e4c3SSagi Grimberg 
206538a2d0d4SChristoph Hellwig 		WARN_ON_ONCE(offset);
206638a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
206738a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
206838a2d0d4SChristoph Hellwig 				se_cmd->t_prot_sg, se_cmd->t_prot_nents,
206938a2d0d4SChristoph Hellwig 				&sig_attrs, addr, rkey, dir);
2070570db170SSagi Grimberg 	} else {
207138a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
207238a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
207338a2d0d4SChristoph Hellwig 				offset, addr, rkey, dir);
207438a2d0d4SChristoph Hellwig 	}
207566f53e6fSBharat Potnuri 
207638a2d0d4SChristoph Hellwig 	if (ret < 0) {
207738a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
207838a2d0d4SChristoph Hellwig 		return ret;
20799e961ae7SSagi Grimberg 	}
20809e961ae7SSagi Grimberg 
208166f53e6fSBharat Potnuri 	cmd->ctx_init_done = true;
208266f53e6fSBharat Potnuri 
208366f53e6fSBharat Potnuri rdma_ctx_post:
208438a2d0d4SChristoph Hellwig 	ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
208538a2d0d4SChristoph Hellwig 	if (ret < 0)
208638a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to post RDMA res\n", cmd);
208759464ef4SVu Pham 	return ret;
208859464ef4SVu Pham }
208959464ef4SVu Pham 
209059464ef4SVu Pham static int
209190ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
209290ecc6e2SVu Pham {
209390ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
209459464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
20956700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
209638a2d0d4SChristoph Hellwig 	struct ib_cqe *cqe = NULL;
209738a2d0d4SChristoph Hellwig 	struct ib_send_wr *chain_wr = NULL;
209890ecc6e2SVu Pham 	int rc;
209990ecc6e2SVu Pham 
210024f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
210190ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
21024c22e07fSSagi Grimberg 
210338a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd)) {
210438a2d0d4SChristoph Hellwig 		isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
210538a2d0d4SChristoph Hellwig 		cqe = &isert_cmd->tx_desc.tx_cqe;
210638a2d0d4SChristoph Hellwig 	} else {
2107b8d26b3bSNicholas Bellinger 		/*
2108b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2109b8d26b3bSNicholas Bellinger 		 */
21109e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
21119e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
211204d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2113b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2114b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
211595b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
211668a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
21174366b19cSJenny Derzhavetz 
21184366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2119c97119b6SMax Gurtovoy 		if (rc)
21204366b19cSJenny Derzhavetz 			return rc;
212138a2d0d4SChristoph Hellwig 
212238a2d0d4SChristoph Hellwig 		chain_wr = &isert_cmd->tx_desc.send_wr;
21239e961ae7SSagi Grimberg 	}
2124b8d26b3bSNicholas Bellinger 
2125555a65f6SNicholas Bellinger 	rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2126555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n",
2127555a65f6SNicholas Bellinger 		  isert_cmd, rc);
2128555a65f6SNicholas Bellinger 	return rc;
2129b8d26b3bSNicholas Bellinger }
2130b8d26b3bSNicholas Bellinger 
2131b8d26b3bSNicholas Bellinger static int
2132b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2133b8d26b3bSNicholas Bellinger {
2134d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2135555a65f6SNicholas Bellinger 	int ret;
2136b8d26b3bSNicholas Bellinger 
213724f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
213838a2d0d4SChristoph Hellwig 		 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
2139b8d26b3bSNicholas Bellinger 
214038a2d0d4SChristoph Hellwig 	isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2141555a65f6SNicholas Bellinger 	ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context,
214238a2d0d4SChristoph Hellwig 				     &isert_cmd->tx_desc.tx_cqe, NULL);
2143bdf20e72SSagi Grimberg 
2144555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n",
2145555a65f6SNicholas Bellinger 		 isert_cmd, ret);
2146555a65f6SNicholas Bellinger 	return ret;
2147b8d26b3bSNicholas Bellinger }
2148b8d26b3bSNicholas Bellinger 
2149b8d26b3bSNicholas Bellinger static int
2150b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2151b8d26b3bSNicholas Bellinger {
2152a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2153a4c15cd9SJenny Derzhavetz 	int ret = 0;
2154b8d26b3bSNicholas Bellinger 
2155b8d26b3bSNicholas Bellinger 	switch (state) {
2156a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2157a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2158a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2159a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2160a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2161a4c15cd9SJenny Derzhavetz 		break;
2162b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2163b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2164b8d26b3bSNicholas Bellinger 		break;
2165b8d26b3bSNicholas Bellinger 	default:
216624f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2167b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2168b8d26b3bSNicholas Bellinger 		break;
2169b8d26b3bSNicholas Bellinger 	}
2170b8d26b3bSNicholas Bellinger 
2171b8d26b3bSNicholas Bellinger 	return ret;
2172b8d26b3bSNicholas Bellinger }
2173b8d26b3bSNicholas Bellinger 
2174b8d26b3bSNicholas Bellinger static int
2175b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2176b8d26b3bSNicholas Bellinger {
2177991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2178b8d26b3bSNicholas Bellinger 	int ret;
2179b8d26b3bSNicholas Bellinger 
2180b8d26b3bSNicholas Bellinger 	switch (state) {
2181b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2182b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2183991bb764SSagi Grimberg 		if (!ret)
2184991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2185b8d26b3bSNicholas Bellinger 		break;
2186b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2187b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2188b8d26b3bSNicholas Bellinger 		break;
2189b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2190b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2191b8d26b3bSNicholas Bellinger 		break;
2192b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2193b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2194b8d26b3bSNicholas Bellinger 		break;
2195adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2196adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2197adb54c29SNicholas Bellinger 		break;
2198b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2199b8d26b3bSNicholas Bellinger 		/*
2200b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2201b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2202b8d26b3bSNicholas Bellinger 		 */
2203b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2204b8d26b3bSNicholas Bellinger 		break;
2205b8d26b3bSNicholas Bellinger 	default:
220624f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2207b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2208b8d26b3bSNicholas Bellinger 		break;
2209b8d26b3bSNicholas Bellinger 	}
2210b8d26b3bSNicholas Bellinger 
2211b8d26b3bSNicholas Bellinger 	return ret;
2212b8d26b3bSNicholas Bellinger }
2213b8d26b3bSNicholas Bellinger 
2214ca6c1d82SSagi Grimberg struct rdma_cm_id *
2215ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2216ca6c1d82SSagi Grimberg {
2217ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2218ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2219ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2220ca6c1d82SSagi Grimberg 	int ret;
2221ca6c1d82SSagi Grimberg 
2222ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
222324f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2224ca6c1d82SSagi Grimberg 
2225fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
2226ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2227ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
222824f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2229ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2230ca6c1d82SSagi Grimberg 		goto out;
2231ca6c1d82SSagi Grimberg 	}
223224f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2233ca6c1d82SSagi Grimberg 
2234221442eaSMax Gurtovoy 	/*
2235221442eaSMax Gurtovoy 	 * Allow both IPv4 and IPv6 sockets to bind a single port
2236221442eaSMax Gurtovoy 	 * at the same time.
2237221442eaSMax Gurtovoy 	 */
2238221442eaSMax Gurtovoy 	ret = rdma_set_afonly(id, 1);
2239221442eaSMax Gurtovoy 	if (ret) {
2240221442eaSMax Gurtovoy 		isert_err("rdma_set_afonly() failed: %d\n", ret);
2241221442eaSMax Gurtovoy 		goto out_id;
2242221442eaSMax Gurtovoy 	}
2243221442eaSMax Gurtovoy 
2244ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2245ca6c1d82SSagi Grimberg 	if (ret) {
224624f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2247ca6c1d82SSagi Grimberg 		goto out_id;
2248ca6c1d82SSagi Grimberg 	}
2249ca6c1d82SSagi Grimberg 
2250992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
2251ca6c1d82SSagi Grimberg 	if (ret) {
225224f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2253ca6c1d82SSagi Grimberg 		goto out_id;
2254ca6c1d82SSagi Grimberg 	}
2255ca6c1d82SSagi Grimberg 
2256ca6c1d82SSagi Grimberg 	return id;
2257ca6c1d82SSagi Grimberg out_id:
2258ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
2259ca6c1d82SSagi Grimberg out:
2260ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
2261ca6c1d82SSagi Grimberg }
2262ca6c1d82SSagi Grimberg 
2263b8d26b3bSNicholas Bellinger static int
2264b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
226513a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
2266b8d26b3bSNicholas Bellinger {
2267b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2268b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2269b8d26b3bSNicholas Bellinger 	int ret;
2270b8d26b3bSNicholas Bellinger 
2271b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
227293b80f29SLeon Romanovsky 	if (!isert_np)
2273b8d26b3bSNicholas Bellinger 		return -ENOMEM;
227493b80f29SLeon Romanovsky 
2275ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
2276ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
2277bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
2278bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
2279ca6c1d82SSagi Grimberg 	isert_np->np = np;
2280b8d26b3bSNicholas Bellinger 
2281b8d26b3bSNicholas Bellinger 	/*
2282b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2283b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2284b8d26b3bSNicholas Bellinger 	 */
2285b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
228613a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
2287b8d26b3bSNicholas Bellinger 
2288ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
2289b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2290b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2291b8d26b3bSNicholas Bellinger 		goto out;
2292b8d26b3bSNicholas Bellinger 	}
2293b8d26b3bSNicholas Bellinger 
2294ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
2295b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2296b8d26b3bSNicholas Bellinger 
2297b8d26b3bSNicholas Bellinger 	return 0;
2298b8d26b3bSNicholas Bellinger 
2299b8d26b3bSNicholas Bellinger out:
2300b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2301ca6c1d82SSagi Grimberg 
2302b8d26b3bSNicholas Bellinger 	return ret;
2303b8d26b3bSNicholas Bellinger }
2304b8d26b3bSNicholas Bellinger 
2305b8d26b3bSNicholas Bellinger static int
2306b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2307b8d26b3bSNicholas Bellinger {
2308dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2309b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2310b8d26b3bSNicholas Bellinger 	int ret;
231113bce482SJenny Derzhavetz 	struct iser_cm_hdr rsp_hdr;
2312b8d26b3bSNicholas Bellinger 
2313b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2314b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2315b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2316b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2317b8d26b3bSNicholas Bellinger 
231813bce482SJenny Derzhavetz 	memset(&rsp_hdr, 0, sizeof(rsp_hdr));
2319422bd0acSJenny Derzhavetz 	rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2320422bd0acSJenny Derzhavetz 	if (!isert_conn->snd_w_inv)
2321422bd0acSJenny Derzhavetz 		rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
232213bce482SJenny Derzhavetz 	cp.private_data = (void *)&rsp_hdr;
232313bce482SJenny Derzhavetz 	cp.private_data_len = sizeof(rsp_hdr);
232413bce482SJenny Derzhavetz 
2325b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2326b8d26b3bSNicholas Bellinger 	if (ret) {
232724f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
2328b8d26b3bSNicholas Bellinger 		return ret;
2329b8d26b3bSNicholas Bellinger 	}
2330b8d26b3bSNicholas Bellinger 
2331b8d26b3bSNicholas Bellinger 	return 0;
2332b8d26b3bSNicholas Bellinger }
2333b8d26b3bSNicholas Bellinger 
2334b8d26b3bSNicholas Bellinger static int
2335b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2336b8d26b3bSNicholas Bellinger {
23376700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2338b8d26b3bSNicholas Bellinger 	int ret;
2339b8d26b3bSNicholas Bellinger 
234024f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
23412371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
23422371e5daSSagi Grimberg 	if (ret) {
234324f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
23442371e5daSSagi Grimberg 			  isert_conn);
23452371e5daSSagi Grimberg 		return ret;
23462371e5daSSagi Grimberg 	}
23472371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
23482371e5daSSagi Grimberg 
23496faaa85fSNicholas Bellinger 	/*
23506faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
23516faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
23526faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
23536faaa85fSNicholas Bellinger 	 * into a NOP.
23546faaa85fSNicholas Bellinger 	 */
23556faaa85fSNicholas Bellinger 	if (!login->first_request)
23566faaa85fSNicholas Bellinger 		return 0;
2357b8d26b3bSNicholas Bellinger 
23582371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
23592371e5daSSagi Grimberg 
2360dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
2361dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
2362b8d26b3bSNicholas Bellinger 	if (ret)
2363b8d26b3bSNicholas Bellinger 		return ret;
2364b8d26b3bSNicholas Bellinger 
236524f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
23662371e5daSSagi Grimberg 
2367b8d26b3bSNicholas Bellinger 	return 0;
2368b8d26b3bSNicholas Bellinger }
2369b8d26b3bSNicholas Bellinger 
2370b8d26b3bSNicholas Bellinger static void
2371b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2372b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2373b8d26b3bSNicholas Bellinger {
2374dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2375b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2376b8d26b3bSNicholas Bellinger 
2377b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2378b8d26b3bSNicholas Bellinger 
2379dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
2380dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
2381b8d26b3bSNicholas Bellinger }
2382b8d26b3bSNicholas Bellinger 
2383b8d26b3bSNicholas Bellinger static int
2384b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2385b8d26b3bSNicholas Bellinger {
23866700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2387b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2388c6b8e918SSagi Grimberg 	int ret;
2389b8d26b3bSNicholas Bellinger 
2390b8d26b3bSNicholas Bellinger accept_wait:
2391ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
2392c6b8e918SSagi Grimberg 	if (ret)
2393b8d26b3bSNicholas Bellinger 		return -ENODEV;
2394b8d26b3bSNicholas Bellinger 
2395b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2396e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
2397b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
239811378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
2399e346ab34SSagi Grimberg 			 np->np_thread_state);
2400bf194997SLeon Romanovsky 		/*
2401e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
2402e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
2403bf194997SLeon Romanovsky 		 */
2404b8d26b3bSNicholas Bellinger 		return -ENODEV;
2405b8d26b3bSNicholas Bellinger 	}
2406b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2407b8d26b3bSNicholas Bellinger 
2408ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2409bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
2410ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
2411b8d26b3bSNicholas Bellinger 		goto accept_wait;
2412b8d26b3bSNicholas Bellinger 	}
2413bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
2414bd379220SJenny Derzhavetz 			struct isert_conn, node);
2415bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
2416ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2417b8d26b3bSNicholas Bellinger 
2418b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2419b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2420aea92980SJenny Derzhavetz 	isert_conn->state = ISER_CONN_BOUND;
2421b8d26b3bSNicholas Bellinger 
2422b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2423b8d26b3bSNicholas Bellinger 
242424f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
24252371e5daSSagi Grimberg 
2426b8d26b3bSNicholas Bellinger 	return 0;
2427b8d26b3bSNicholas Bellinger }
2428b8d26b3bSNicholas Bellinger 
2429b8d26b3bSNicholas Bellinger static void
2430b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2431b8d26b3bSNicholas Bellinger {
24326700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2433268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
2434b8d26b3bSNicholas Bellinger 
2435ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
2436ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
2437b8d26b3bSNicholas Bellinger 
2438268e6811SSagi Grimberg 	/*
2439268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
2440268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
2441268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
2442268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
2443bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
2444268e6811SSagi Grimberg 	 */
2445ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2446bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
2447bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
2448268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
2449bd379220SJenny Derzhavetz 					 &isert_np->pending,
2450bd379220SJenny Derzhavetz 					 node) {
2451bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
2452bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
2453bd379220SJenny Derzhavetz 			isert_connect_release(isert_conn);
2454bd379220SJenny Derzhavetz 		}
2455bd379220SJenny Derzhavetz 	}
2456bd379220SJenny Derzhavetz 
2457bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
2458bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
2459bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
2460bd379220SJenny Derzhavetz 					 &isert_np->accepted,
2461bd379220SJenny Derzhavetz 					 node) {
246224f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
2463268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
2464268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
2465268e6811SSagi Grimberg 		}
2466268e6811SSagi Grimberg 	}
2467ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2468268e6811SSagi Grimberg 
2469b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2470b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2471b8d26b3bSNicholas Bellinger }
2472b8d26b3bSNicholas Bellinger 
2473b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
2474b02efbfcSSagi Grimberg {
2475b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
2476b02efbfcSSagi Grimberg 						     struct isert_conn,
2477b02efbfcSSagi Grimberg 						     release_work);
2478b02efbfcSSagi Grimberg 
247924f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
2480b02efbfcSSagi Grimberg 
2481dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2482b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
2483dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2484b02efbfcSSagi Grimberg 
248524f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
2486b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
2487b02efbfcSSagi Grimberg }
2488b02efbfcSSagi Grimberg 
2489bdf20e72SSagi Grimberg static void
2490991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
2491991bb764SSagi Grimberg {
2492991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
2493991bb764SSagi Grimberg 
24944c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
24954c22e07fSSagi Grimberg 
2496991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
249724f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
2498991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
2499991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
2500991bb764SSagi Grimberg 	}
2501991bb764SSagi Grimberg }
2502991bb764SSagi Grimberg 
250376261adaSBart Van Assche static void
250476261adaSBart Van Assche isert_wait4cmds(struct iscsi_conn *conn)
250576261adaSBart Van Assche {
250676261adaSBart Van Assche 	isert_info("iscsi_conn %p\n", conn);
250776261adaSBart Van Assche 
250876261adaSBart Van Assche 	if (conn->sess) {
25096f55b06fSMike Christie 		target_stop_session(conn->sess->se_sess);
251076261adaSBart Van Assche 		target_wait_for_sess_cmds(conn->sess->se_sess);
251176261adaSBart Van Assche 	}
251276261adaSBart Van Assche }
251376261adaSBart Van Assche 
25143e03c4b0SJenny Derzhavetz /**
25153e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
25163e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
25173e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
25183e03c4b0SJenny Derzhavetz  *
25193e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
25203e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
25213e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
25223e03c4b0SJenny Derzhavetz  */
25233e03c4b0SJenny Derzhavetz static void
25243e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
25253e03c4b0SJenny Derzhavetz {
25263e03c4b0SJenny Derzhavetz 	struct iscsi_cmd *cmd, *tmp;
25273e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
25283e03c4b0SJenny Derzhavetz 
25293e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
25303e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
25313e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
25323e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
25333e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
25343e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
25353e03c4b0SJenny Derzhavetz 	}
25363e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
25373e03c4b0SJenny Derzhavetz 
25383e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
25393e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
25403e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
25413e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
25423e03c4b0SJenny Derzhavetz 
25433e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
25443e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
25453e03c4b0SJenny Derzhavetz 		}
25463e03c4b0SJenny Derzhavetz 	}
25473e03c4b0SJenny Derzhavetz }
25483e03c4b0SJenny Derzhavetz 
2549defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
2550b8d26b3bSNicholas Bellinger {
2551b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2552b8d26b3bSNicholas Bellinger 
25534c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
2554b8d26b3bSNicholas Bellinger 
2555dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2556954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2557dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2558b2cb9649SNicholas Bellinger 
2559572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
25603e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
256176261adaSBart Van Assche 	isert_wait4cmds(conn);
2562991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
2563954f2372SSagi Grimberg 
2564b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
2565defd8848SNicholas Bellinger }
2566defd8848SNicholas Bellinger 
2567defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2568defd8848SNicholas Bellinger {
2569defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2570b8d26b3bSNicholas Bellinger 
2571572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
2572b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2573b8d26b3bSNicholas Bellinger }
2574b8d26b3bSNicholas Bellinger 
2575187e84f7SVarun Prakash static void isert_get_rx_pdu(struct iscsi_conn *conn)
2576187e84f7SVarun Prakash {
2577187e84f7SVarun Prakash 	struct completion comp;
2578187e84f7SVarun Prakash 
2579187e84f7SVarun Prakash 	init_completion(&comp);
2580187e84f7SVarun Prakash 
2581187e84f7SVarun Prakash 	wait_for_completion_interruptible(&comp);
2582187e84f7SVarun Prakash }
2583187e84f7SVarun Prakash 
2584b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2585b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2586b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2587bd027d85SNicholas Bellinger 	.rdma_shutdown		= true,
2588d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2589b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2590b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2591b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2592b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2593defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2594b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2595b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2596b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2597b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2598b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2599b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2600b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2601b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2602131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
2603187e84f7SVarun Prakash 	.iscsit_get_rx_pdu	= isert_get_rx_pdu,
2604e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
2605b8d26b3bSNicholas Bellinger };
2606b8d26b3bSNicholas Bellinger 
2607b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2608b8d26b3bSNicholas Bellinger {
2609b8d26b3bSNicholas Bellinger 	int ret;
2610b8d26b3bSNicholas Bellinger 
2611631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
2612631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
2613b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
261424f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
26156f0fae3dSSagi Grimberg 		return -ENOMEM;
2616b8d26b3bSNicholas Bellinger 	}
2617b8d26b3bSNicholas Bellinger 
2618b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2619b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
2620b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
262124f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
2622b02efbfcSSagi Grimberg 		ret = -ENOMEM;
2623b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
2624b02efbfcSSagi Grimberg 	}
2625b02efbfcSSagi Grimberg 
2626b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
262724f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
2628b02efbfcSSagi Grimberg 
2629b8d26b3bSNicholas Bellinger 	return 0;
2630b8d26b3bSNicholas Bellinger 
2631b02efbfcSSagi Grimberg destroy_comp_wq:
2632b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
26336f0fae3dSSagi Grimberg 
2634b8d26b3bSNicholas Bellinger 	return ret;
2635b8d26b3bSNicholas Bellinger }
2636b8d26b3bSNicholas Bellinger 
2637b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2638b8d26b3bSNicholas Bellinger {
2639f5ebec96SSagi Grimberg 	flush_scheduled_work();
2640b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
2641b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2642b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
26434c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
2644b8d26b3bSNicholas Bellinger }
2645b8d26b3bSNicholas Bellinger 
2646b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2647b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2648b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2649b8d26b3bSNicholas Bellinger 
2650b8d26b3bSNicholas Bellinger module_init(isert_init);
2651b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2652