xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 7651e2d6c5b359a28c2d4c904fec6608d1021ca8)
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);
45549f39a5STetsuo Handa static struct workqueue_struct *isert_login_wq;
46b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
47b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
48b8d26b3bSNicholas Bellinger 
49f93f3a70SSagi Grimberg static int
50be36d683SMax Gurtovoy isert_put_response(struct iscsit_conn *conn, struct iscsit_cmd *cmd);
512371e5daSSagi Grimberg static int
529679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn);
532371e5daSSagi Grimberg static int
542371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
55ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
56d40945d8SVu Pham 
572f1b6b7dSSagi Grimberg static void isert_release_work(struct work_struct *work);
589679cc51SChristoph Hellwig static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc);
599679cc51SChristoph Hellwig static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc);
609679cc51SChristoph Hellwig static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc);
619679cc51SChristoph Hellwig static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc);
622f1b6b7dSSagi Grimberg 
63dae7a75fSMax Gurtovoy static int isert_sg_tablesize_set(const char *val, const struct kernel_param *kp)
64dae7a75fSMax Gurtovoy {
65dae7a75fSMax Gurtovoy 	int n = 0, ret;
66dae7a75fSMax Gurtovoy 
67dae7a75fSMax Gurtovoy 	ret = kstrtoint(val, 10, &n);
68dae7a75fSMax Gurtovoy 	if (ret != 0 || n < ISCSI_ISER_MIN_SG_TABLESIZE ||
69dae7a75fSMax Gurtovoy 	    n > ISCSI_ISER_MAX_SG_TABLESIZE)
70dae7a75fSMax Gurtovoy 		return -EINVAL;
71dae7a75fSMax Gurtovoy 
72dae7a75fSMax Gurtovoy 	return param_set_int(val, kp);
73dae7a75fSMax Gurtovoy }
74dae7a75fSMax Gurtovoy 
75302cc7c3SSagi Grimberg static inline bool
76302cc7c3SSagi Grimberg isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
77302cc7c3SSagi Grimberg {
7823a548eeSSagi Grimberg 	return (conn->pi_support &&
79302cc7c3SSagi Grimberg 		cmd->prot_op != TARGET_PROT_NORMAL);
80302cc7c3SSagi Grimberg }
81302cc7c3SSagi Grimberg 
82d40945d8SVu Pham static void
83b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
84b8d26b3bSNicholas Bellinger {
856700425eSSagi Grimberg 	struct isert_conn *isert_conn = context;
86b8d26b3bSNicholas Bellinger 
87ea8a1616SSagi Grimberg 	isert_err("%s (%d): conn %p\n",
88ea8a1616SSagi Grimberg 		  ib_event_msg(e->event), e->event, isert_conn);
89ea8a1616SSagi Grimberg 
90b8d26b3bSNicholas Bellinger 	switch (e->event) {
91b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
92dac6ab30SSagi Grimberg 		rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
93b8d26b3bSNicholas Bellinger 		break;
94b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
954c22e07fSSagi Grimberg 		isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
96b8d26b3bSNicholas Bellinger 		break;
97b8d26b3bSNicholas Bellinger 	default:
98b8d26b3bSNicholas Bellinger 		break;
99b8d26b3bSNicholas Bellinger 	}
100b8d26b3bSNicholas Bellinger }
101b8d26b3bSNicholas Bellinger 
10240fc069aSSagi Grimberg static struct ib_qp *
10340fc069aSSagi Grimberg isert_create_qp(struct isert_conn *isert_conn,
10440fc069aSSagi Grimberg 		struct rdma_cm_id *cma_id)
10540fc069aSSagi Grimberg {
106c6e66307SYamin Friedman 	u32 cq_size = ISERT_QP_MAX_REQ_DTOS + ISERT_QP_MAX_RECV_DTOS + 2;
107dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
108c6e66307SYamin Friedman 	struct ib_device *ib_dev = device->ib_device;
10940fc069aSSagi Grimberg 	struct ib_qp_init_attr attr;
110317000b9SMax Gurtovoy 	int ret, factor;
111b8d26b3bSNicholas Bellinger 
112c6e66307SYamin Friedman 	isert_conn->cq = ib_cq_pool_get(ib_dev, cq_size, -1, IB_POLL_WORKQUEUE);
113c6e66307SYamin Friedman 	if (IS_ERR(isert_conn->cq)) {
114c6e66307SYamin Friedman 		isert_err("Unable to allocate cq\n");
115c6e66307SYamin Friedman 		ret = PTR_ERR(isert_conn->cq);
116c6e66307SYamin Friedman 		return ERR_PTR(ret);
117c6e66307SYamin Friedman 	}
118c6e66307SYamin Friedman 	isert_conn->cq_size = cq_size;
119c6e66307SYamin Friedman 
120b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
121b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
122b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
123c6e66307SYamin Friedman 	attr.send_cq = isert_conn->cq;
124c6e66307SYamin Friedman 	attr.recv_cq = isert_conn->cq;
125572a1434SSagi Grimberg 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS + 1;
126bdf20e72SSagi Grimberg 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
127317000b9SMax Gurtovoy 	factor = rdma_rw_mr_factor(device->ib_device, cma_id->port_num,
128dae7a75fSMax Gurtovoy 				   isert_sg_tablesize);
129317000b9SMax Gurtovoy 	attr.cap.max_rdma_ctxs = ISCSI_DEF_XMIT_CMDS_MAX * factor;
13033023fb8SSteve Wise 	attr.cap.max_send_sge = device->ib_device->attrs.max_send_sge;
131b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
132b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
133b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
134570db170SSagi Grimberg 	if (device->pi_capable)
135c0a6cbb9SIsrael Rukshin 		attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
136b8d26b3bSNicholas Bellinger 
13767cb3949SSagi Grimberg 	ret = rdma_create_qp(cma_id, device->pd, &attr);
138b8d26b3bSNicholas Bellinger 	if (ret) {
13924f412ddSSagi Grimberg 		isert_err("rdma_create_qp failed for cma_id %d\n", ret);
140c6e66307SYamin Friedman 		ib_cq_pool_put(isert_conn->cq, isert_conn->cq_size);
141c6e66307SYamin Friedman 
14240fc069aSSagi Grimberg 		return ERR_PTR(ret);
14340fc069aSSagi Grimberg 	}
14440fc069aSSagi Grimberg 
14540fc069aSSagi Grimberg 	return cma_id->qp;
14640fc069aSSagi Grimberg }
14740fc069aSSagi Grimberg 
14840fc069aSSagi Grimberg static int
149b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
150b8d26b3bSNicholas Bellinger {
151dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
15267cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
153b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
154b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
155b8d26b3bSNicholas Bellinger 	u64 dma_addr;
156b8d26b3bSNicholas Bellinger 	int i, j;
157b8d26b3bSNicholas Bellinger 
1586396bb22SKees Cook 	isert_conn->rx_descs = kcalloc(ISERT_QP_MAX_RECV_DTOS,
1596396bb22SKees Cook 				       sizeof(struct iser_rx_desc),
1606396bb22SKees Cook 				       GFP_KERNEL);
161dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
16293b80f29SLeon Romanovsky 		return -ENOMEM;
163b8d26b3bSNicholas Bellinger 
164dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
165b8d26b3bSNicholas Bellinger 
166b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
1670b089c1eSSagi Grimberg 		dma_addr = ib_dma_map_single(ib_dev, rx_desc->buf,
1680b089c1eSSagi Grimberg 					ISER_RX_SIZE, DMA_FROM_DEVICE);
169b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
170b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
171b8d26b3bSNicholas Bellinger 
172b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
173b8d26b3bSNicholas Bellinger 
174b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
1750b089c1eSSagi Grimberg 		rx_sg->addr = rx_desc->dma_addr + isert_get_hdr_offset(rx_desc);
176b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
17734efc7dfSJason Gunthorpe 		rx_sg->lkey = device->pd->local_dma_lkey;
1789679cc51SChristoph Hellwig 		rx_desc->rx_cqe.done = isert_recv_done;
179b8d26b3bSNicholas Bellinger 	}
180b8d26b3bSNicholas Bellinger 
181b8d26b3bSNicholas Bellinger 	return 0;
182b8d26b3bSNicholas Bellinger 
183b8d26b3bSNicholas Bellinger dma_map_fail:
184dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
185b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
186b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
1870b089c1eSSagi Grimberg 				    ISER_RX_SIZE, DMA_FROM_DEVICE);
188b8d26b3bSNicholas Bellinger 	}
189dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
190dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
1914c22e07fSSagi Grimberg 	isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
192b8d26b3bSNicholas Bellinger 	return -ENOMEM;
193b8d26b3bSNicholas Bellinger }
194b8d26b3bSNicholas Bellinger 
195b8d26b3bSNicholas Bellinger static void
196b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
197b8d26b3bSNicholas Bellinger {
198dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
199b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
200b8d26b3bSNicholas Bellinger 	int i;
201b8d26b3bSNicholas Bellinger 
202dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
203b8d26b3bSNicholas Bellinger 		return;
204b8d26b3bSNicholas Bellinger 
205dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
206b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
207b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
2080b089c1eSSagi Grimberg 				    ISER_RX_SIZE, DMA_FROM_DEVICE);
209b8d26b3bSNicholas Bellinger 	}
210b8d26b3bSNicholas Bellinger 
211dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
212dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
213b8d26b3bSNicholas Bellinger }
214b8d26b3bSNicholas Bellinger 
215172369c5SSagi Grimberg static int
216172369c5SSagi Grimberg isert_create_device_ib_res(struct isert_device *device)
217172369c5SSagi Grimberg {
2184a061b28SOr Gerlitz 	struct ib_device *ib_dev = device->ib_device;
219fd8205e8SSagi Grimberg 	int ret;
220172369c5SSagi Grimberg 
22133023fb8SSteve Wise 	isert_dbg("devattr->max_send_sge: %d devattr->max_recv_sge %d\n",
22233023fb8SSteve Wise 		  ib_dev->attrs.max_send_sge, ib_dev->attrs.max_recv_sge);
2234a061b28SOr Gerlitz 	isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
224172369c5SSagi Grimberg 
225ed082d36SChristoph Hellwig 	device->pd = ib_alloc_pd(ib_dev, 0);
22667cb3949SSagi Grimberg 	if (IS_ERR(device->pd)) {
22767cb3949SSagi Grimberg 		ret = PTR_ERR(device->pd);
22867cb3949SSagi Grimberg 		isert_err("failed to allocate pd, device %p, ret=%d\n",
22967cb3949SSagi Grimberg 			  device, ret);
230c6e66307SYamin Friedman 		return ret;
23167cb3949SSagi Grimberg 	}
23267cb3949SSagi Grimberg 
233172369c5SSagi Grimberg 	/* Check signature cap */
234e945c653SJason Gunthorpe 	if (ib_dev->attrs.kernel_cap_flags & IBK_INTEGRITY_HANDOVER)
235a6dc16b6SMax Gurtovoy 		device->pi_capable = true;
236a6dc16b6SMax Gurtovoy 	else
237a6dc16b6SMax Gurtovoy 		device->pi_capable = false;
23867cb3949SSagi Grimberg 
239b8d26b3bSNicholas Bellinger 	return 0;
240b8d26b3bSNicholas Bellinger }
241b8d26b3bSNicholas Bellinger 
242b8d26b3bSNicholas Bellinger static void
243b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
244b8d26b3bSNicholas Bellinger {
24524f412ddSSagi Grimberg 	isert_info("device %p\n", device);
246b8d26b3bSNicholas Bellinger 
24767cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
248b8d26b3bSNicholas Bellinger }
249b8d26b3bSNicholas Bellinger 
250b8d26b3bSNicholas Bellinger static void
251cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device)
252b8d26b3bSNicholas Bellinger {
253b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
254b8d26b3bSNicholas Bellinger 	device->refcount--;
2554c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
256b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
257b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
258b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
259b8d26b3bSNicholas Bellinger 		kfree(device);
260b8d26b3bSNicholas Bellinger 	}
261b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
262b8d26b3bSNicholas Bellinger }
263b8d26b3bSNicholas Bellinger 
264b8d26b3bSNicholas Bellinger static struct isert_device *
265cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id)
266b8d26b3bSNicholas Bellinger {
267b8d26b3bSNicholas Bellinger 	struct isert_device *device;
268b8d26b3bSNicholas Bellinger 	int ret;
269b8d26b3bSNicholas Bellinger 
270b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
271b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
272b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
273b8d26b3bSNicholas Bellinger 			device->refcount++;
2744c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
2754c22e07fSSagi Grimberg 				   device, device->refcount);
276b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
277b8d26b3bSNicholas Bellinger 			return device;
278b8d26b3bSNicholas Bellinger 		}
279b8d26b3bSNicholas Bellinger 	}
280b8d26b3bSNicholas Bellinger 
281b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
282b8d26b3bSNicholas Bellinger 	if (!device) {
283b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
284b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
285b8d26b3bSNicholas Bellinger 	}
286b8d26b3bSNicholas Bellinger 
287b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
288b8d26b3bSNicholas Bellinger 
289b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
290b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
291b8d26b3bSNicholas Bellinger 	if (ret) {
292b8d26b3bSNicholas Bellinger 		kfree(device);
293b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
294b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
295b8d26b3bSNicholas Bellinger 	}
296b8d26b3bSNicholas Bellinger 
297b8d26b3bSNicholas Bellinger 	device->refcount++;
298b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
2994c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
3004c22e07fSSagi Grimberg 		   device, device->refcount);
301b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
302b8d26b3bSNicholas Bellinger 
303b8d26b3bSNicholas Bellinger 	return device;
304b8d26b3bSNicholas Bellinger }
305b8d26b3bSNicholas Bellinger 
30659464ef4SVu Pham static void
307ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn)
308ae9ea9edSSagi Grimberg {
309ae9ea9edSSagi Grimberg 	isert_conn->state = ISER_CONN_INIT;
310bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_conn->node);
311dac6ab30SSagi Grimberg 	init_completion(&isert_conn->login_comp);
312ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
31363b268d2SRaju Rangoju 	init_waitqueue_head(&isert_conn->rem_wait);
314dac6ab30SSagi Grimberg 	kref_init(&isert_conn->kref);
315dac6ab30SSagi Grimberg 	mutex_init(&isert_conn->mutex);
3162f1b6b7dSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
317ae9ea9edSSagi Grimberg }
318ae9ea9edSSagi Grimberg 
319ae9ea9edSSagi Grimberg static void
320ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn)
321ae9ea9edSSagi Grimberg {
322dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
323ae9ea9edSSagi Grimberg 
324ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
325ed1083b2SChristoph Hellwig 			    ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
3265adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
3275adabdd1SChristoph Hellwig 
3280b089c1eSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_desc->dma_addr,
3290b089c1eSSagi Grimberg 			    ISER_RX_SIZE, DMA_FROM_DEVICE);
3300b089c1eSSagi Grimberg 	kfree(isert_conn->login_desc);
331ae9ea9edSSagi Grimberg }
332ae9ea9edSSagi Grimberg 
333ae9ea9edSSagi Grimberg static int
334ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn,
335ae9ea9edSSagi Grimberg 		      struct ib_device *ib_dev)
336ae9ea9edSSagi Grimberg {
337ae9ea9edSSagi Grimberg 	int ret;
338ae9ea9edSSagi Grimberg 
3390b089c1eSSagi Grimberg 	isert_conn->login_desc = kzalloc(sizeof(*isert_conn->login_desc),
3405adabdd1SChristoph Hellwig 			GFP_KERNEL);
3410b089c1eSSagi Grimberg 	if (!isert_conn->login_desc)
342ae9ea9edSSagi Grimberg 		return -ENOMEM;
343ae9ea9edSSagi Grimberg 
3440b089c1eSSagi Grimberg 	isert_conn->login_desc->dma_addr = ib_dma_map_single(ib_dev,
3450b089c1eSSagi Grimberg 				isert_conn->login_desc->buf,
3460b089c1eSSagi Grimberg 				ISER_RX_SIZE, DMA_FROM_DEVICE);
3470b089c1eSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_desc->dma_addr);
348ae9ea9edSSagi Grimberg 	if (ret) {
3490b089c1eSSagi Grimberg 		isert_err("login_desc dma mapping error: %d\n", ret);
3500b089c1eSSagi Grimberg 		isert_conn->login_desc->dma_addr = 0;
3510b089c1eSSagi Grimberg 		goto out_free_login_desc;
3525adabdd1SChristoph Hellwig 	}
3535adabdd1SChristoph Hellwig 
3545adabdd1SChristoph Hellwig 	isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
3555adabdd1SChristoph Hellwig 	if (!isert_conn->login_rsp_buf) {
3561d5840c9SWei Yongjun 		ret = -ENOMEM;
3570b089c1eSSagi Grimberg 		goto out_unmap_login_desc;
358ae9ea9edSSagi Grimberg 	}
359ae9ea9edSSagi Grimberg 
360ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
3615adabdd1SChristoph Hellwig 					isert_conn->login_rsp_buf,
362ed1083b2SChristoph Hellwig 					ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
363ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
364ae9ea9edSSagi Grimberg 	if (ret) {
365ae9ea9edSSagi Grimberg 		isert_err("login_rsp_dma mapping error: %d\n", ret);
366ae9ea9edSSagi Grimberg 		isert_conn->login_rsp_dma = 0;
3675adabdd1SChristoph Hellwig 		goto out_free_login_rsp_buf;
368ae9ea9edSSagi Grimberg 	}
369ae9ea9edSSagi Grimberg 
370ae9ea9edSSagi Grimberg 	return 0;
371ae9ea9edSSagi Grimberg 
3725adabdd1SChristoph Hellwig out_free_login_rsp_buf:
3735adabdd1SChristoph Hellwig 	kfree(isert_conn->login_rsp_buf);
3740b089c1eSSagi Grimberg out_unmap_login_desc:
3750b089c1eSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_desc->dma_addr,
3760b089c1eSSagi Grimberg 			    ISER_RX_SIZE, DMA_FROM_DEVICE);
3770b089c1eSSagi Grimberg out_free_login_desc:
3780b089c1eSSagi Grimberg 	kfree(isert_conn->login_desc);
379ae9ea9edSSagi Grimberg 	return ret;
380ae9ea9edSSagi Grimberg }
381ae9ea9edSSagi Grimberg 
382422bd0acSJenny Derzhavetz static void
383422bd0acSJenny Derzhavetz isert_set_nego_params(struct isert_conn *isert_conn,
384422bd0acSJenny Derzhavetz 		      struct rdma_conn_param *param)
385422bd0acSJenny Derzhavetz {
386422bd0acSJenny Derzhavetz 	struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
387422bd0acSJenny Derzhavetz 
388422bd0acSJenny Derzhavetz 	/* Set max inflight RDMA READ requests */
389422bd0acSJenny Derzhavetz 	isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
390422bd0acSJenny Derzhavetz 				attr->max_qp_init_rd_atom);
391422bd0acSJenny Derzhavetz 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
392422bd0acSJenny Derzhavetz 
393422bd0acSJenny Derzhavetz 	if (param->private_data) {
394422bd0acSJenny Derzhavetz 		u8 flags = *(u8 *)param->private_data;
395422bd0acSJenny Derzhavetz 
396422bd0acSJenny Derzhavetz 		/*
397422bd0acSJenny Derzhavetz 		 * use remote invalidation if the both initiator
398422bd0acSJenny Derzhavetz 		 * and the HCA support it
399422bd0acSJenny Derzhavetz 		 */
400422bd0acSJenny Derzhavetz 		isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
401422bd0acSJenny Derzhavetz 					  (attr->device_cap_flags &
402422bd0acSJenny Derzhavetz 					   IB_DEVICE_MEM_MGT_EXTENSIONS);
403422bd0acSJenny Derzhavetz 		if (isert_conn->snd_w_inv)
404422bd0acSJenny Derzhavetz 			isert_info("Using remote invalidation\n");
405422bd0acSJenny Derzhavetz 	}
406422bd0acSJenny Derzhavetz }
407422bd0acSJenny Derzhavetz 
408c6e66307SYamin Friedman static void
409c6e66307SYamin Friedman isert_destroy_qp(struct isert_conn *isert_conn)
410c6e66307SYamin Friedman {
411c6e66307SYamin Friedman 	ib_destroy_qp(isert_conn->qp);
412c6e66307SYamin Friedman 	ib_cq_pool_put(isert_conn->cq, isert_conn->cq_size);
413c6e66307SYamin Friedman }
414c6e66307SYamin Friedman 
415b8d26b3bSNicholas Bellinger static int
416b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
417b8d26b3bSNicholas Bellinger {
418ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
419ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
420b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
421b8d26b3bSNicholas Bellinger 	struct isert_device *device;
422b8d26b3bSNicholas Bellinger 	int ret = 0;
42314f4b54fSSagi Grimberg 
42414f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
42514f4b54fSSagi Grimberg 	if (!np->enabled) {
42614f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
42724f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
4288094ba0aSLeon Romanovsky 		return rdma_reject(cma_id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
42914f4b54fSSagi Grimberg 	}
43014f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
431b8d26b3bSNicholas Bellinger 
4324c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
433b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
434b8d26b3bSNicholas Bellinger 
435b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
436ae9ea9edSSagi Grimberg 	if (!isert_conn)
437b8d26b3bSNicholas Bellinger 		return -ENOMEM;
438b8d26b3bSNicholas Bellinger 
439ae9ea9edSSagi Grimberg 	isert_init_conn(isert_conn);
440dac6ab30SSagi Grimberg 	isert_conn->cm_id = cma_id;
441b8d26b3bSNicholas Bellinger 
442cf8ae958SSagi Grimberg 	device = isert_device_get(cma_id);
443b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
444b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
445adb76a52SLv Yunlong 		goto out;
446b8d26b3bSNicholas Bellinger 	}
447dac6ab30SSagi Grimberg 	isert_conn->device = device;
448b8d26b3bSNicholas Bellinger 
449adb76a52SLv Yunlong 	ret = isert_alloc_login_buf(isert_conn, cma_id->device);
450adb76a52SLv Yunlong 	if (ret)
451adb76a52SLv Yunlong 		goto out_conn_dev;
452adb76a52SLv Yunlong 
453422bd0acSJenny Derzhavetz 	isert_set_nego_params(isert_conn, &event->param.conn);
4541a92e17eSSagi Grimberg 
455c6e66307SYamin Friedman 	isert_conn->qp = isert_create_qp(isert_conn, cma_id);
456c6e66307SYamin Friedman 	if (IS_ERR(isert_conn->qp)) {
457c6e66307SYamin Friedman 		ret = PTR_ERR(isert_conn->qp);
458adb76a52SLv Yunlong 		goto out_rsp_dma_map;
459c6e66307SYamin Friedman 	}
460b8d26b3bSNicholas Bellinger 
4619679cc51SChristoph Hellwig 	ret = isert_login_post_recv(isert_conn);
4622371e5daSSagi Grimberg 	if (ret)
463c6e66307SYamin Friedman 		goto out_destroy_qp;
4642371e5daSSagi Grimberg 
4652371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
4662371e5daSSagi Grimberg 	if (ret)
467c6e66307SYamin Friedman 		goto out_destroy_qp;
4682371e5daSSagi Grimberg 
469ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
470bd379220SJenny Derzhavetz 	list_add_tail(&isert_conn->node, &isert_np->accepted);
471ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
472b8d26b3bSNicholas Bellinger 
473b8d26b3bSNicholas Bellinger 	return 0;
474b8d26b3bSNicholas Bellinger 
475c6e66307SYamin Friedman out_destroy_qp:
476c6e66307SYamin Friedman 	isert_destroy_qp(isert_conn);
477b8d26b3bSNicholas Bellinger out_rsp_dma_map:
478ae9ea9edSSagi Grimberg 	isert_free_login_buf(isert_conn);
479adb76a52SLv Yunlong out_conn_dev:
480adb76a52SLv Yunlong 	isert_device_put(device);
481b8d26b3bSNicholas Bellinger out:
482b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
4838094ba0aSLeon Romanovsky 	rdma_reject(cma_id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
484b8d26b3bSNicholas Bellinger 	return ret;
485b8d26b3bSNicholas Bellinger }
486b8d26b3bSNicholas Bellinger 
487b8d26b3bSNicholas Bellinger static void
488b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
489b8d26b3bSNicholas Bellinger {
490dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
491b8d26b3bSNicholas Bellinger 
4924c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
493b8d26b3bSNicholas Bellinger 
49457df81e3SSagi Grimberg 	BUG_ON(!device);
49557df81e3SSagi Grimberg 
49619e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
49763b268d2SRaju Rangoju 	if (isert_conn->cm_id &&
49863b268d2SRaju Rangoju 	    !isert_conn->dev_removed)
499dac6ab30SSagi Grimberg 		rdma_destroy_id(isert_conn->cm_id);
50019e2090fSSagi Grimberg 
501c6e66307SYamin Friedman 	if (isert_conn->qp)
502c6e66307SYamin Friedman 		isert_destroy_qp(isert_conn);
503b8d26b3bSNicholas Bellinger 
5040b089c1eSSagi Grimberg 	if (isert_conn->login_desc)
505ae9ea9edSSagi Grimberg 		isert_free_login_buf(isert_conn);
506ae9ea9edSSagi Grimberg 
507cf8ae958SSagi Grimberg 	isert_device_put(device);
50857df81e3SSagi Grimberg 
50963b268d2SRaju Rangoju 	if (isert_conn->dev_removed)
51063b268d2SRaju Rangoju 		wake_up_interruptible(&isert_conn->rem_wait);
51163b268d2SRaju Rangoju 	else
51257df81e3SSagi Grimberg 		kfree(isert_conn);
513b8d26b3bSNicholas Bellinger }
514b8d26b3bSNicholas Bellinger 
515b8d26b3bSNicholas Bellinger static void
516b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
517b8d26b3bSNicholas Bellinger {
51819e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
519bd379220SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
520c2f88b17SSagi Grimberg 
52124f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
522128e9cc8SSagi Grimberg 
523dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
524128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_UP;
525bd379220SJenny Derzhavetz 	kref_get(&isert_conn->kref);
526dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
527bd379220SJenny Derzhavetz 
528bd379220SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
529bd379220SJenny Derzhavetz 	list_move_tail(&isert_conn->node, &isert_np->pending);
530bd379220SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
531bd379220SJenny Derzhavetz 
532bd379220SJenny Derzhavetz 	isert_info("np %p: Allow accept_np to continue\n", isert_np);
533bd379220SJenny Derzhavetz 	up(&isert_np->sem);
534b8d26b3bSNicholas Bellinger }
535b8d26b3bSNicholas Bellinger 
536b8d26b3bSNicholas Bellinger static void
537dac6ab30SSagi Grimberg isert_release_kref(struct kref *kref)
538b8d26b3bSNicholas Bellinger {
539b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
540dac6ab30SSagi Grimberg 				struct isert_conn, kref);
541b8d26b3bSNicholas Bellinger 
5424c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
5434c22e07fSSagi Grimberg 		   current->pid);
544b8d26b3bSNicholas Bellinger 
545b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
546b8d26b3bSNicholas Bellinger }
547b8d26b3bSNicholas Bellinger 
548b8d26b3bSNicholas Bellinger static void
549b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
550b8d26b3bSNicholas Bellinger {
551dac6ab30SSagi Grimberg 	kref_put(&isert_conn->kref, isert_release_kref);
552b8d26b3bSNicholas Bellinger }
553b8d26b3bSNicholas Bellinger 
5546d1fba0cSJenny Derzhavetz static void
5556d1fba0cSJenny Derzhavetz isert_handle_unbound_conn(struct isert_conn *isert_conn)
5566d1fba0cSJenny Derzhavetz {
5576d1fba0cSJenny Derzhavetz 	struct isert_np *isert_np = isert_conn->cm_id->context;
5586d1fba0cSJenny Derzhavetz 
5596d1fba0cSJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
5606d1fba0cSJenny Derzhavetz 	if (!list_empty(&isert_conn->node)) {
5616d1fba0cSJenny Derzhavetz 		/*
5626d1fba0cSJenny Derzhavetz 		 * This means iscsi doesn't know this connection
5636d1fba0cSJenny Derzhavetz 		 * so schedule a cleanup ourselves
5646d1fba0cSJenny Derzhavetz 		 */
5656d1fba0cSJenny Derzhavetz 		list_del_init(&isert_conn->node);
5666d1fba0cSJenny Derzhavetz 		isert_put_conn(isert_conn);
5676d1fba0cSJenny Derzhavetz 		queue_work(isert_release_wq, &isert_conn->release_work);
5686d1fba0cSJenny Derzhavetz 	}
5696d1fba0cSJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
5706d1fba0cSJenny Derzhavetz }
5716d1fba0cSJenny Derzhavetz 
572954f2372SSagi Grimberg /**
573954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
574954f2372SSagi Grimberg  * @isert_conn: isert connection struct
575954f2372SSagi Grimberg  *
576954f2372SSagi Grimberg  * Notes:
577aea92980SJenny Derzhavetz  * In case the connection state is BOUND, move state
578954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
579128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
580954f2372SSagi Grimberg  *
581dac6ab30SSagi Grimberg  * This routine must be called with mutex held. Thus it is
582954f2372SSagi Grimberg  * safe to call multiple times.
583954f2372SSagi Grimberg  */
584954f2372SSagi Grimberg static void
585954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
586954f2372SSagi Grimberg {
587954f2372SSagi Grimberg 	int err;
588954f2372SSagi Grimberg 
5896d1fba0cSJenny Derzhavetz 	if (isert_conn->state >= ISER_CONN_TERMINATING)
5906d1fba0cSJenny Derzhavetz 		return;
5916d1fba0cSJenny Derzhavetz 
59224f412ddSSagi Grimberg 	isert_info("Terminating conn %p state %d\n",
593954f2372SSagi Grimberg 		   isert_conn, isert_conn->state);
594128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_TERMINATING;
595dac6ab30SSagi Grimberg 	err = rdma_disconnect(isert_conn->cm_id);
596954f2372SSagi Grimberg 	if (err)
59724f412ddSSagi Grimberg 		isert_warn("Failed rdma_disconnect isert_conn %p\n",
598954f2372SSagi Grimberg 			   isert_conn);
599954f2372SSagi Grimberg }
600954f2372SSagi Grimberg 
6013b726ae2SSagi Grimberg static int
602ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
603ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
604b8d26b3bSNicholas Bellinger {
605ea8a1616SSagi Grimberg 	isert_dbg("%s (%d): isert np %p\n",
606ea8a1616SSagi Grimberg 		  rdma_event_msg(event), event, isert_np);
6073b726ae2SSagi Grimberg 
608ca6c1d82SSagi Grimberg 	switch (event) {
609ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
610ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = NULL;
611ca6c1d82SSagi Grimberg 		break;
612ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
613ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = isert_setup_id(isert_np);
614ed8cb0a4SJenny Derzhavetz 		if (IS_ERR(isert_np->cm_id)) {
61524f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
616ed8cb0a4SJenny Derzhavetz 				  isert_np, PTR_ERR(isert_np->cm_id));
617ed8cb0a4SJenny Derzhavetz 			isert_np->cm_id = NULL;
618ca6c1d82SSagi Grimberg 		}
619ca6c1d82SSagi Grimberg 		break;
620ca6c1d82SSagi Grimberg 	default:
62124f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
622ca6c1d82SSagi Grimberg 			  isert_np, event);
623ca6c1d82SSagi Grimberg 	}
624ca6c1d82SSagi Grimberg 
6253b726ae2SSagi Grimberg 	return -1;
6263b726ae2SSagi Grimberg }
6273b726ae2SSagi Grimberg 
628ca6c1d82SSagi Grimberg static int
629ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
630ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
631ca6c1d82SSagi Grimberg {
632f81bf458SJenny Derzhavetz 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
633ca6c1d82SSagi Grimberg 
634dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
6356d1fba0cSJenny Derzhavetz 	switch (isert_conn->state) {
6366d1fba0cSJenny Derzhavetz 	case ISER_CONN_TERMINATING:
6376d1fba0cSJenny Derzhavetz 		break;
6386d1fba0cSJenny Derzhavetz 	case ISER_CONN_UP:
639128e9cc8SSagi Grimberg 		isert_conn_terminate(isert_conn);
640572a1434SSagi Grimberg 		ib_drain_qp(isert_conn->qp);
6416d1fba0cSJenny Derzhavetz 		isert_handle_unbound_conn(isert_conn);
6426d1fba0cSJenny Derzhavetz 		break;
6436d1fba0cSJenny Derzhavetz 	case ISER_CONN_BOUND:
6446d1fba0cSJenny Derzhavetz 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
6456d1fba0cSJenny Derzhavetz 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
6466d1fba0cSJenny Derzhavetz 		break;
6476d1fba0cSJenny Derzhavetz 	default:
648506f71d1SColin Ian King 		isert_warn("conn %p terminating in state %d\n",
6496d1fba0cSJenny Derzhavetz 			   isert_conn, isert_conn->state);
6506d1fba0cSJenny Derzhavetz 	}
651dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
652128e9cc8SSagi Grimberg 
6533b726ae2SSagi Grimberg 	return 0;
654b8d26b3bSNicholas Bellinger }
655b8d26b3bSNicholas Bellinger 
6564a579da2SSagi Grimberg static int
657954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
658954f2372SSagi Grimberg {
65919e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
660*7651e2d6SSaravanan Vajravel 	struct isert_np *isert_np = cma_id->context;
661954f2372SSagi Grimberg 
662cd52cb26SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
663*7651e2d6SSaravanan Vajravel 
664*7651e2d6SSaravanan Vajravel 	mutex_lock(&isert_np->mutex);
665bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
666*7651e2d6SSaravanan Vajravel 	mutex_unlock(&isert_np->mutex);
667dac6ab30SSagi Grimberg 	isert_conn->cm_id = NULL;
668954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
6694a579da2SSagi Grimberg 
6704a579da2SSagi Grimberg 	return -1;
671954f2372SSagi Grimberg }
672954f2372SSagi Grimberg 
673b8d26b3bSNicholas Bellinger static int
674b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
675b8d26b3bSNicholas Bellinger {
676f81bf458SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
67763b268d2SRaju Rangoju 	struct isert_conn *isert_conn;
678b8d26b3bSNicholas Bellinger 	int ret = 0;
679b8d26b3bSNicholas Bellinger 
680ea8a1616SSagi Grimberg 	isert_info("%s (%d): status %d id %p np %p\n",
681ea8a1616SSagi Grimberg 		   rdma_event_msg(event->event), event->event,
6824c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
683b8d26b3bSNicholas Bellinger 
684f81bf458SJenny Derzhavetz 	if (isert_np->cm_id == cma_id)
685f81bf458SJenny Derzhavetz 		return isert_np_cma_handler(cma_id->context, event->event);
686f81bf458SJenny Derzhavetz 
687b8d26b3bSNicholas Bellinger 	switch (event->event) {
688b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
689b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
6903b726ae2SSagi Grimberg 		if (ret)
6914c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
692b8d26b3bSNicholas Bellinger 		break;
693b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
694b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
695b8d26b3bSNicholas Bellinger 		break;
696df561f66SGustavo A. R. Silva 	case RDMA_CM_EVENT_ADDR_CHANGE:
697df561f66SGustavo A. R. Silva 	case RDMA_CM_EVENT_DISCONNECTED:
69888c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
699ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
700b8d26b3bSNicholas Bellinger 		break;
70163b268d2SRaju Rangoju 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
70263b268d2SRaju Rangoju 		isert_conn = cma_id->qp->qp_context;
70363b268d2SRaju Rangoju 		isert_conn->dev_removed = true;
70463b268d2SRaju Rangoju 		isert_disconnected_handler(cma_id, event->event);
70563b268d2SRaju Rangoju 		wait_event_interruptible(isert_conn->rem_wait,
70663b268d2SRaju Rangoju 					 isert_conn->state == ISER_CONN_DOWN);
70763b268d2SRaju Rangoju 		kfree(isert_conn);
70863b268d2SRaju Rangoju 		/*
70963b268d2SRaju Rangoju 		 * return non-zero from the callback to destroy
71063b268d2SRaju Rangoju 		 * the rdma cm id
71163b268d2SRaju Rangoju 		 */
71263b268d2SRaju Rangoju 		return 1;
713d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_REJECTED:
7141e38a366SSteve Wise 		isert_info("Connection rejected: %s\n",
7151e38a366SSteve Wise 			   rdma_reject_msg(cma_id, event->status));
716df561f66SGustavo A. R. Silva 		fallthrough;
717d2271bd0SBart Van Assche 	case RDMA_CM_EVENT_UNREACHABLE:
718b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
7194a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
720954f2372SSagi Grimberg 		break;
721b8d26b3bSNicholas Bellinger 	default:
72224f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
723b8d26b3bSNicholas Bellinger 		break;
724b8d26b3bSNicholas Bellinger 	}
725b8d26b3bSNicholas Bellinger 
726b8d26b3bSNicholas Bellinger 	return ret;
727b8d26b3bSNicholas Bellinger }
728b8d26b3bSNicholas Bellinger 
729b8d26b3bSNicholas Bellinger static int
7304366b19cSJenny Derzhavetz isert_post_recvm(struct isert_conn *isert_conn, u32 count)
731b8d26b3bSNicholas Bellinger {
732e01a7674SBart Van Assche 	struct ib_recv_wr *rx_wr;
733b8d26b3bSNicholas Bellinger 	int i, ret;
734b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
735b8d26b3bSNicholas Bellinger 
736dac6ab30SSagi Grimberg 	for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
7374366b19cSJenny Derzhavetz 		rx_desc = &isert_conn->rx_descs[i];
7389679cc51SChristoph Hellwig 
7399679cc51SChristoph Hellwig 		rx_wr->wr_cqe = &rx_desc->rx_cqe;
740b8d26b3bSNicholas Bellinger 		rx_wr->sg_list = &rx_desc->rx_sg;
741b8d26b3bSNicholas Bellinger 		rx_wr->num_sge = 1;
742b8d26b3bSNicholas Bellinger 		rx_wr->next = rx_wr + 1;
7437a56dc88SSagi Grimberg 		rx_desc->in_use = false;
744b8d26b3bSNicholas Bellinger 	}
745b8d26b3bSNicholas Bellinger 	rx_wr--;
746b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
747b8d26b3bSNicholas Bellinger 
748e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr, NULL);
7496d1fba0cSJenny Derzhavetz 	if (ret)
75024f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
7514366b19cSJenny Derzhavetz 
7524366b19cSJenny Derzhavetz 	return ret;
7534366b19cSJenny Derzhavetz }
7544366b19cSJenny Derzhavetz 
7554366b19cSJenny Derzhavetz static int
7564366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
7574366b19cSJenny Derzhavetz {
758e01a7674SBart Van Assche 	struct ib_recv_wr rx_wr;
7594366b19cSJenny Derzhavetz 	int ret;
7604366b19cSJenny Derzhavetz 
7617a56dc88SSagi Grimberg 	if (!rx_desc->in_use) {
7627a56dc88SSagi Grimberg 		/*
7637a56dc88SSagi Grimberg 		 * if the descriptor is not in-use we already reposted it
7647a56dc88SSagi Grimberg 		 * for recv, so just silently return
7657a56dc88SSagi Grimberg 		 */
7667a56dc88SSagi Grimberg 		return 0;
7677a56dc88SSagi Grimberg 	}
7687a56dc88SSagi Grimberg 
7697a56dc88SSagi Grimberg 	rx_desc->in_use = false;
7709679cc51SChristoph Hellwig 	rx_wr.wr_cqe = &rx_desc->rx_cqe;
7714366b19cSJenny Derzhavetz 	rx_wr.sg_list = &rx_desc->rx_sg;
7724366b19cSJenny Derzhavetz 	rx_wr.num_sge = 1;
7734366b19cSJenny Derzhavetz 	rx_wr.next = NULL;
7744366b19cSJenny Derzhavetz 
775e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL);
7766d1fba0cSJenny Derzhavetz 	if (ret)
7774366b19cSJenny Derzhavetz 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
7784366b19cSJenny Derzhavetz 
779b8d26b3bSNicholas Bellinger 	return ret;
780b8d26b3bSNicholas Bellinger }
781b8d26b3bSNicholas Bellinger 
782b8d26b3bSNicholas Bellinger static int
7839679cc51SChristoph Hellwig isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
784b8d26b3bSNicholas Bellinger {
785dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
786e01a7674SBart Van Assche 	struct ib_send_wr send_wr;
787b8d26b3bSNicholas Bellinger 	int ret;
788b8d26b3bSNicholas Bellinger 
789b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
790b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
791b8d26b3bSNicholas Bellinger 
7929679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_login_send_done;
7939679cc51SChristoph Hellwig 
794b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
7959679cc51SChristoph Hellwig 	send_wr.wr_cqe	= &tx_desc->tx_cqe;
796b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
797b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
798b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
799b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
800b8d26b3bSNicholas Bellinger 
801e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &send_wr, NULL);
802bdf20e72SSagi Grimberg 	if (ret)
80324f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
804b8d26b3bSNicholas Bellinger 
805b8d26b3bSNicholas Bellinger 	return ret;
806b8d26b3bSNicholas Bellinger }
807b8d26b3bSNicholas Bellinger 
808b8d26b3bSNicholas Bellinger static void
809763b6965SAlex Estrin __isert_create_send_desc(struct isert_device *device,
810b8d26b3bSNicholas Bellinger 			 struct iser_tx_desc *tx_desc)
811b8d26b3bSNicholas Bellinger {
812b8d26b3bSNicholas Bellinger 
813d3cf81f9SSagi Grimberg 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
814d3cf81f9SSagi Grimberg 	tx_desc->iser_header.flags = ISCSI_CTRL;
815b8d26b3bSNicholas Bellinger 
816b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
817b8d26b3bSNicholas Bellinger 
81834efc7dfSJason Gunthorpe 	if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
81934efc7dfSJason Gunthorpe 		tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
82024f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
821b8d26b3bSNicholas Bellinger 	}
822b8d26b3bSNicholas Bellinger }
823b8d26b3bSNicholas Bellinger 
824763b6965SAlex Estrin static void
825763b6965SAlex Estrin isert_create_send_desc(struct isert_conn *isert_conn,
826763b6965SAlex Estrin 		       struct isert_cmd *isert_cmd,
827763b6965SAlex Estrin 		       struct iser_tx_desc *tx_desc)
828763b6965SAlex Estrin {
829763b6965SAlex Estrin 	struct isert_device *device = isert_conn->device;
830763b6965SAlex Estrin 	struct ib_device *ib_dev = device->ib_device;
831763b6965SAlex Estrin 
832763b6965SAlex Estrin 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
833763b6965SAlex Estrin 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
834763b6965SAlex Estrin 
835763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
836763b6965SAlex Estrin }
837763b6965SAlex Estrin 
838b8d26b3bSNicholas Bellinger static int
839b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
840b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
841b8d26b3bSNicholas Bellinger {
842dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
84367cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
844b8d26b3bSNicholas Bellinger 	u64 dma_addr;
845b8d26b3bSNicholas Bellinger 
846b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
847b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
848b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
84924f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
850b8d26b3bSNicholas Bellinger 		return -ENOMEM;
851b8d26b3bSNicholas Bellinger 	}
852b8d26b3bSNicholas Bellinger 
853b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
854b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
855b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
85634efc7dfSJason Gunthorpe 	tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
857b8d26b3bSNicholas Bellinger 
8584c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
8594c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
8604c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
861b8d26b3bSNicholas Bellinger 
862b8d26b3bSNicholas Bellinger 	return 0;
863b8d26b3bSNicholas Bellinger }
864b8d26b3bSNicholas Bellinger 
865b8d26b3bSNicholas Bellinger static void
86695b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
86768a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
868b8d26b3bSNicholas Bellinger {
86995b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
87095b60f07SNicholas Bellinger 
8719679cc51SChristoph Hellwig 	tx_desc->tx_cqe.done = isert_send_done;
8729679cc51SChristoph Hellwig 	send_wr->wr_cqe = &tx_desc->tx_cqe;
873422bd0acSJenny Derzhavetz 
874422bd0acSJenny Derzhavetz 	if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
875422bd0acSJenny Derzhavetz 		send_wr->opcode  = IB_WR_SEND_WITH_INV;
876422bd0acSJenny Derzhavetz 		send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
877422bd0acSJenny Derzhavetz 	} else {
878b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_SEND;
879422bd0acSJenny Derzhavetz 	}
880422bd0acSJenny Derzhavetz 
88195b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
882b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
88395b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
884b8d26b3bSNicholas Bellinger }
885b8d26b3bSNicholas Bellinger 
886b8d26b3bSNicholas Bellinger static int
8879679cc51SChristoph Hellwig isert_login_post_recv(struct isert_conn *isert_conn)
888b8d26b3bSNicholas Bellinger {
889e01a7674SBart Van Assche 	struct ib_recv_wr rx_wr;
890b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
891b8d26b3bSNicholas Bellinger 	int ret;
892b8d26b3bSNicholas Bellinger 
893b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
8940b089c1eSSagi Grimberg 	sge.addr = isert_conn->login_desc->dma_addr +
8950b089c1eSSagi Grimberg 		isert_get_hdr_offset(isert_conn->login_desc);
896ed1083b2SChristoph Hellwig 	sge.length = ISER_RX_PAYLOAD_SIZE;
89734efc7dfSJason Gunthorpe 	sge.lkey = isert_conn->device->pd->local_dma_lkey;
898b8d26b3bSNicholas Bellinger 
89924f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
900b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
901b8d26b3bSNicholas Bellinger 
9020b089c1eSSagi Grimberg 	isert_conn->login_desc->rx_cqe.done = isert_login_recv_done;
9039679cc51SChristoph Hellwig 
904b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
9050b089c1eSSagi Grimberg 	rx_wr.wr_cqe = &isert_conn->login_desc->rx_cqe;
906b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
907b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
908b8d26b3bSNicholas Bellinger 
909e01a7674SBart Van Assche 	ret = ib_post_recv(isert_conn->qp, &rx_wr, NULL);
9106d1fba0cSJenny Derzhavetz 	if (ret)
91124f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
912b8d26b3bSNicholas Bellinger 
913b8d26b3bSNicholas Bellinger 	return ret;
914b8d26b3bSNicholas Bellinger }
915b8d26b3bSNicholas Bellinger 
916b8d26b3bSNicholas Bellinger static int
917be36d683SMax Gurtovoy isert_put_login_tx(struct iscsit_conn *conn, struct iscsi_login *login,
918b8d26b3bSNicholas Bellinger 		   u32 length)
919b8d26b3bSNicholas Bellinger {
920b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
921dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
92267cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
923dac6ab30SSagi Grimberg 	struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
924b8d26b3bSNicholas Bellinger 	int ret;
925b8d26b3bSNicholas Bellinger 
926763b6965SAlex Estrin 	__isert_create_send_desc(device, tx_desc);
927b8d26b3bSNicholas Bellinger 
928b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
929b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
930b8d26b3bSNicholas Bellinger 
931b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
932b8d26b3bSNicholas Bellinger 
933b8d26b3bSNicholas Bellinger 	if (length > 0) {
934b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
935b8d26b3bSNicholas Bellinger 
936b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
937b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
938b8d26b3bSNicholas Bellinger 
939b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
940b8d26b3bSNicholas Bellinger 
941b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
942b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
943b8d26b3bSNicholas Bellinger 
944b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
945b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
94634efc7dfSJason Gunthorpe 		tx_dsg->lkey	= isert_conn->device->pd->local_dma_lkey;
947b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
948b8d26b3bSNicholas Bellinger 	}
949b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
950b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
951b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
952b8d26b3bSNicholas Bellinger 			if (ret)
953b8d26b3bSNicholas Bellinger 				return ret;
954b8d26b3bSNicholas Bellinger 
9554366b19cSJenny Derzhavetz 			ret = isert_post_recvm(isert_conn,
9564366b19cSJenny Derzhavetz 					       ISERT_QP_MAX_RECV_DTOS);
957b8d26b3bSNicholas Bellinger 			if (ret)
958b8d26b3bSNicholas Bellinger 				return ret;
959b8d26b3bSNicholas Bellinger 
960128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
961dac6ab30SSagi Grimberg 			mutex_lock(&isert_conn->mutex);
962128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
963dac6ab30SSagi Grimberg 			mutex_unlock(&isert_conn->mutex);
964b8d26b3bSNicholas Bellinger 			goto post_send;
965b8d26b3bSNicholas Bellinger 		}
966b8d26b3bSNicholas Bellinger 
9679679cc51SChristoph Hellwig 		ret = isert_login_post_recv(isert_conn);
968b8d26b3bSNicholas Bellinger 		if (ret)
969b8d26b3bSNicholas Bellinger 			return ret;
970b8d26b3bSNicholas Bellinger 	}
971b8d26b3bSNicholas Bellinger post_send:
9729679cc51SChristoph Hellwig 	ret = isert_login_post_send(isert_conn, tx_desc);
973b8d26b3bSNicholas Bellinger 	if (ret)
974b8d26b3bSNicholas Bellinger 		return ret;
975b8d26b3bSNicholas Bellinger 
976b8d26b3bSNicholas Bellinger 	return 0;
977b8d26b3bSNicholas Bellinger }
978b8d26b3bSNicholas Bellinger 
979b8d26b3bSNicholas Bellinger static void
9802371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
981b8d26b3bSNicholas Bellinger {
9820b089c1eSSagi Grimberg 	struct iser_rx_desc *rx_desc = isert_conn->login_desc;
9832371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
984be36d683SMax Gurtovoy 	struct iscsit_conn *conn = isert_conn->conn;
985b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
986b8d26b3bSNicholas Bellinger 	int size;
987b8d26b3bSNicholas Bellinger 
98824f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
9892371e5daSSagi Grimberg 
9902371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
991b8d26b3bSNicholas Bellinger 
992b8d26b3bSNicholas Bellinger 	if (login->first_request) {
993b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
9940b089c1eSSagi Grimberg 			(struct iscsi_login_req *)isert_get_iscsi_hdr(rx_desc);
995b8d26b3bSNicholas Bellinger 		/*
996b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
997b8d26b3bSNicholas Bellinger 		 * login request PDU.
998b8d26b3bSNicholas Bellinger 		 */
999b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
10002a402120SMaurizio Lombardi 		login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(
10012a402120SMaurizio Lombardi 				login_req->flags);
1002b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1003b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1004b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1005b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1006b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1007b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1008b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1009b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1010b8d26b3bSNicholas Bellinger 	}
1011b8d26b3bSNicholas Bellinger 
10120b089c1eSSagi Grimberg 	memcpy(&login->req[0], isert_get_iscsi_hdr(rx_desc), ISCSI_HDR_LEN);
1013b8d26b3bSNicholas Bellinger 
1014b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
10154c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
10164c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
10174c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
10180b089c1eSSagi Grimberg 	memcpy(login->req_buf, isert_get_data(rx_desc), size);
1019b8d26b3bSNicholas Bellinger 
10206faaa85fSNicholas Bellinger 	if (login->first_request) {
1021dac6ab30SSagi Grimberg 		complete(&isert_conn->login_comp);
10226faaa85fSNicholas Bellinger 		return;
10236faaa85fSNicholas Bellinger 	}
1024549f39a5STetsuo Handa 	queue_delayed_work(isert_login_wq, &conn->login_work, 0);
1025b8d26b3bSNicholas Bellinger }
1026b8d26b3bSNicholas Bellinger 
102766cd9d4eSMax Gurtovoy static struct iscsit_cmd
1028be36d683SMax Gurtovoy *isert_allocate_cmd(struct iscsit_conn *conn, struct iser_rx_desc *rx_desc)
1029b8d26b3bSNicholas Bellinger {
10306700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1031b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
103266cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
1033b8d26b3bSNicholas Bellinger 
1034676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1035d703ce2fSNicholas Bellinger 	if (!cmd) {
103666cd9d4eSMax Gurtovoy 		isert_err("Unable to allocate iscsit_cmd + isert_cmd\n");
1037b8d26b3bSNicholas Bellinger 		return NULL;
1038b8d26b3bSNicholas Bellinger 	}
1039d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1040b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
104166cd9d4eSMax Gurtovoy 	isert_cmd->iscsit_cmd = cmd;
10424366b19cSJenny Derzhavetz 	isert_cmd->rx_desc = rx_desc;
1043b8d26b3bSNicholas Bellinger 
1044d703ce2fSNicholas Bellinger 	return cmd;
1045b8d26b3bSNicholas Bellinger }
1046b8d26b3bSNicholas Bellinger 
1047b8d26b3bSNicholas Bellinger static int
1048b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
104966cd9d4eSMax Gurtovoy 		      struct isert_cmd *isert_cmd, struct iscsit_cmd *cmd,
1050d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1051b8d26b3bSNicholas Bellinger {
1052be36d683SMax Gurtovoy 	struct iscsit_conn *conn = isert_conn->conn;
1053b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1054b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1055b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
10569fd60088SJenny Derzhavetz 	unsigned int data_len;
1057b8d26b3bSNicholas Bellinger 
1058b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1059b8d26b3bSNicholas Bellinger 	if (rc < 0)
1060b8d26b3bSNicholas Bellinger 		return rc;
1061b8d26b3bSNicholas Bellinger 
1062b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1063b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1064b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
10659fd60088SJenny Derzhavetz 	data_len = cmd->se_cmd.data_length;
1066b8d26b3bSNicholas Bellinger 
10679fd60088SJenny Derzhavetz 	if (imm_data && imm_data_len == data_len)
10689fd60088SJenny Derzhavetz 		cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1069b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1070b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1071b8d26b3bSNicholas Bellinger 		return 0;
1072b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1073b8d26b3bSNicholas Bellinger 		dump_payload = true;
1074b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1075b8d26b3bSNicholas Bellinger 	}
1076b8d26b3bSNicholas Bellinger 
1077b8d26b3bSNicholas Bellinger 	if (!imm_data)
1078b8d26b3bSNicholas Bellinger 		return 0;
1079b8d26b3bSNicholas Bellinger 
10809fd60088SJenny Derzhavetz 	if (imm_data_len != data_len) {
1081b8d26b3bSNicholas Bellinger 		sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
10829fd60088SJenny Derzhavetz 		sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
10830b089c1eSSagi Grimberg 				    isert_get_data(rx_desc), imm_data_len);
10849fd60088SJenny Derzhavetz 		isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
10859fd60088SJenny Derzhavetz 			  sg_nents, imm_data_len);
10869fd60088SJenny Derzhavetz 	} else {
10879fd60088SJenny Derzhavetz 		sg_init_table(&isert_cmd->sg, 1);
10889fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_sg = &isert_cmd->sg;
10899fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_nents = 1;
10900b089c1eSSagi Grimberg 		sg_set_buf(&isert_cmd->sg, isert_get_data(rx_desc),
10910b089c1eSSagi Grimberg 				imm_data_len);
10929fd60088SJenny Derzhavetz 		isert_dbg("Transfer Immediate imm_data_len: %d\n",
10939fd60088SJenny Derzhavetz 			  imm_data_len);
10949fd60088SJenny Derzhavetz 	}
1095b8d26b3bSNicholas Bellinger 
1096b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1097b8d26b3bSNicholas Bellinger 
1098b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1099b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1100b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1101b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1102b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1103b8d26b3bSNicholas Bellinger 	}
1104b8d26b3bSNicholas Bellinger 
1105b8d26b3bSNicholas Bellinger sequence_cmd:
1106561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1107b8d26b3bSNicholas Bellinger 
1108f7a95c90SZou Wei 	if (!rc && !dump_payload && unsol_data)
11090300b114SBart Van Assche 		iscsit_set_unsolicited_dataout(cmd);
11106cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1111afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1112b8d26b3bSNicholas Bellinger 
1113b8d26b3bSNicholas Bellinger 	return 0;
1114b8d26b3bSNicholas Bellinger }
1115b8d26b3bSNicholas Bellinger 
1116b8d26b3bSNicholas Bellinger static int
1117b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1118b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1119b8d26b3bSNicholas Bellinger {
1120b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1121be36d683SMax Gurtovoy 	struct iscsit_conn *conn = isert_conn->conn;
112266cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = NULL;
1123b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1124b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1125b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1126b8d26b3bSNicholas Bellinger 
1127b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1128b8d26b3bSNicholas Bellinger 	if (rc < 0)
1129b8d26b3bSNicholas Bellinger 		return rc;
1130b8d26b3bSNicholas Bellinger 	else if (!cmd)
1131b8d26b3bSNicholas Bellinger 		return 0;
1132b8d26b3bSNicholas Bellinger 	/*
1133b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1134b8d26b3bSNicholas Bellinger 	 */
1135b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
113624f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1137b8d26b3bSNicholas Bellinger 		dump_stack();
1138b8d26b3bSNicholas Bellinger 		return -1;
1139b8d26b3bSNicholas Bellinger 	}
1140b8d26b3bSNicholas Bellinger 
11414c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
11424c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
11434c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
11444c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1145b8d26b3bSNicholas Bellinger 
1146b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1147b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1148b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1149b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1150b8d26b3bSNicholas Bellinger 	/*
1151b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1152b8d26b3bSNicholas Bellinger 	 */
1153b8d26b3bSNicholas Bellinger 	if (page_off) {
11544c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1155b8d26b3bSNicholas Bellinger 		dump_stack();
1156b8d26b3bSNicholas Bellinger 		return -1;
1157b8d26b3bSNicholas Bellinger 	}
11584c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
11594c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
11600b089c1eSSagi Grimberg 		  sg_nents, isert_get_data(rx_desc), unsol_data_len);
1161b8d26b3bSNicholas Bellinger 
11620b089c1eSSagi Grimberg 	sg_copy_from_buffer(sg_start, sg_nents, isert_get_data(rx_desc),
1163b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1164b8d26b3bSNicholas Bellinger 
1165b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1166b8d26b3bSNicholas Bellinger 	if (rc < 0)
1167b8d26b3bSNicholas Bellinger 		return rc;
1168b8d26b3bSNicholas Bellinger 
11694366b19cSJenny Derzhavetz 	/*
11704366b19cSJenny Derzhavetz 	 * multiple data-outs on the same command can arrive -
11714366b19cSJenny Derzhavetz 	 * so post the buffer before hand
11724366b19cSJenny Derzhavetz 	 */
1173c97119b6SMax Gurtovoy 	return isert_post_recv(isert_conn, rx_desc);
1174b8d26b3bSNicholas Bellinger }
1175b8d26b3bSNicholas Bellinger 
1176b8d26b3bSNicholas Bellinger static int
1177778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
117866cd9d4eSMax Gurtovoy 		     struct iscsit_cmd *cmd, struct iser_rx_desc *rx_desc,
1179d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1180778de368SNicholas Bellinger {
1181be36d683SMax Gurtovoy 	struct iscsit_conn *conn = isert_conn->conn;
1182778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1183778de368SNicholas Bellinger 	int rc;
1184778de368SNicholas Bellinger 
1185778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1186778de368SNicholas Bellinger 	if (rc < 0)
1187778de368SNicholas Bellinger 		return rc;
1188778de368SNicholas Bellinger 	/*
1189778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1190778de368SNicholas Bellinger 	 */
1191778de368SNicholas Bellinger 
1192778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1193778de368SNicholas Bellinger }
1194778de368SNicholas Bellinger 
1195778de368SNicholas Bellinger static int
1196adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
119766cd9d4eSMax Gurtovoy 		      struct iscsit_cmd *cmd, struct iser_rx_desc *rx_desc,
1198d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1199adb54c29SNicholas Bellinger {
1200be36d683SMax Gurtovoy 	struct iscsit_conn *conn = isert_conn->conn;
1201adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1202adb54c29SNicholas Bellinger 	int rc;
1203b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1204adb54c29SNicholas Bellinger 
1205adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1206adb54c29SNicholas Bellinger 	if (rc < 0)
1207adb54c29SNicholas Bellinger 		return rc;
1208adb54c29SNicholas Bellinger 
1209b44a2b67SSagi Grimberg 	if (payload_length) {
1210adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
121193b80f29SLeon Romanovsky 		if (!text_in)
1212adb54c29SNicholas Bellinger 			return -ENOMEM;
1213adb54c29SNicholas Bellinger 	}
1214adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1215adb54c29SNicholas Bellinger 
12160b089c1eSSagi Grimberg 	memcpy(cmd->text_in_ptr, isert_get_data(rx_desc), payload_length);
1217adb54c29SNicholas Bellinger 
1218adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1219adb54c29SNicholas Bellinger }
1220adb54c29SNicholas Bellinger 
1221adb54c29SNicholas Bellinger static int
1222b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1223b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1224b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1225b8d26b3bSNicholas Bellinger {
12260b089c1eSSagi Grimberg 	struct iscsi_hdr *hdr = isert_get_iscsi_hdr(rx_desc);
1227be36d683SMax Gurtovoy 	struct iscsit_conn *conn = isert_conn->conn;
122866cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
1229b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1230b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1231b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1232b8d26b3bSNicholas Bellinger 
1233fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1234ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
123524f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1236ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1237ca40d24eSNicholas Bellinger 		return 0;
1238ca40d24eSNicholas Bellinger 	}
1239ca40d24eSNicholas Bellinger 
1240b8d26b3bSNicholas Bellinger 	switch (opcode) {
1241b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
12424366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1243b8d26b3bSNicholas Bellinger 		if (!cmd)
1244b8d26b3bSNicholas Bellinger 			break;
1245b8d26b3bSNicholas Bellinger 
1246d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1247b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1248b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1249b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1250b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1251422bd0acSJenny Derzhavetz 		isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
1252b8d26b3bSNicholas Bellinger 
1253d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1254b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1255b8d26b3bSNicholas Bellinger 		break;
1256b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
12574366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1258b8d26b3bSNicholas Bellinger 		if (!cmd)
1259b8d26b3bSNicholas Bellinger 			break;
1260b8d26b3bSNicholas Bellinger 
1261d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1262d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1263778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1264b8d26b3bSNicholas Bellinger 		break;
1265b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1266b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1267b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1268b8d26b3bSNicholas Bellinger 		break;
1269b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
12704366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1271b8d26b3bSNicholas Bellinger 		if (!cmd)
1272b8d26b3bSNicholas Bellinger 			break;
1273b8d26b3bSNicholas Bellinger 
1274b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1275b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1276b8d26b3bSNicholas Bellinger 		break;
1277b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
12784366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1279b8d26b3bSNicholas Bellinger 		if (!cmd)
1280b8d26b3bSNicholas Bellinger 			break;
1281b8d26b3bSNicholas Bellinger 
1282b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1283b8d26b3bSNicholas Bellinger 		break;
1284adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
12854366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1286e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
12874366b19cSJenny Derzhavetz 		else
12884366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
12894366b19cSJenny Derzhavetz 
1290e4f4e801SSagi Grimberg 		if (!cmd)
1291e4f4e801SSagi Grimberg 			break;
1292adb54c29SNicholas Bellinger 
1293d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1294d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1295adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1296adb54c29SNicholas Bellinger 		break;
1297b8d26b3bSNicholas Bellinger 	default:
129824f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1299b8d26b3bSNicholas Bellinger 		dump_stack();
1300b8d26b3bSNicholas Bellinger 		break;
1301b8d26b3bSNicholas Bellinger 	}
1302b8d26b3bSNicholas Bellinger 
1303b8d26b3bSNicholas Bellinger 	return ret;
1304b8d26b3bSNicholas Bellinger }
1305b8d26b3bSNicholas Bellinger 
1306b8d26b3bSNicholas Bellinger static void
13079679cc51SChristoph Hellwig isert_print_wc(struct ib_wc *wc, const char *type)
1308b8d26b3bSNicholas Bellinger {
13099679cc51SChristoph Hellwig 	if (wc->status != IB_WC_WR_FLUSH_ERR)
13109679cc51SChristoph Hellwig 		isert_err("%s failure: %s (%d) vend_err %x\n", type,
13119679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status,
13129679cc51SChristoph Hellwig 			  wc->vendor_err);
13139679cc51SChristoph Hellwig 	else
13149679cc51SChristoph Hellwig 		isert_dbg("%s failure: %s (%d)\n", type,
13159679cc51SChristoph Hellwig 			  ib_wc_status_msg(wc->status), wc->status);
13169679cc51SChristoph Hellwig }
13179679cc51SChristoph Hellwig 
13189679cc51SChristoph Hellwig static void
13199679cc51SChristoph Hellwig isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
13209679cc51SChristoph Hellwig {
13219679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
13229679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
13239679cc51SChristoph Hellwig 	struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
13240b089c1eSSagi Grimberg 	struct iscsi_hdr *hdr = isert_get_iscsi_hdr(rx_desc);
13250b089c1eSSagi Grimberg 	struct iser_ctrl *iser_ctrl = isert_get_iser_hdr(rx_desc);
1326b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1327b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1328b8d26b3bSNicholas Bellinger 
13299679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
13309679cc51SChristoph Hellwig 		isert_print_wc(wc, "recv");
13319679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
13329679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
13339679cc51SChristoph Hellwig 		return;
13349679cc51SChristoph Hellwig 	}
13359679cc51SChristoph Hellwig 
13367a56dc88SSagi Grimberg 	rx_desc->in_use = true;
13377a56dc88SSagi Grimberg 
13389679cc51SChristoph Hellwig 	ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
13390b089c1eSSagi Grimberg 			ISER_RX_SIZE, DMA_FROM_DEVICE);
13409679cc51SChristoph Hellwig 
13419679cc51SChristoph Hellwig 	isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
13429679cc51SChristoph Hellwig 		 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
13439679cc51SChristoph Hellwig 		 (int)(wc->byte_len - ISER_HEADERS_LEN));
13449679cc51SChristoph Hellwig 
1345d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1346b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1347d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1348d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1349d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
13504c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1351b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1352b8d26b3bSNicholas Bellinger 		}
1353d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1354d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1355d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
13564c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1357b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1358b8d26b3bSNicholas Bellinger 		}
1359b8d26b3bSNicholas Bellinger 
136024f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1361b8d26b3bSNicholas Bellinger 		break;
1362b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
136324f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1364b8d26b3bSNicholas Bellinger 		break;
1365b8d26b3bSNicholas Bellinger 	default:
1366d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1367b8d26b3bSNicholas Bellinger 		break;
1368b8d26b3bSNicholas Bellinger 	}
1369b8d26b3bSNicholas Bellinger 
13706c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1371b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
13729679cc51SChristoph Hellwig 
13739679cc51SChristoph Hellwig 	ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
13740b089c1eSSagi Grimberg 			ISER_RX_SIZE, DMA_FROM_DEVICE);
1375b8d26b3bSNicholas Bellinger }
1376b8d26b3bSNicholas Bellinger 
1377b8d26b3bSNicholas Bellinger static void
13789679cc51SChristoph Hellwig isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1379b8d26b3bSNicholas Bellinger {
13809679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1381fce50a2fSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->device->ib_device;
1382b8d26b3bSNicholas Bellinger 
13839679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
13849679cc51SChristoph Hellwig 		isert_print_wc(wc, "login recv");
13859679cc51SChristoph Hellwig 		return;
1386b8d26b3bSNicholas Bellinger 	}
1387b8d26b3bSNicholas Bellinger 
13880b089c1eSSagi Grimberg 	ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_desc->dma_addr,
13890b089c1eSSagi Grimberg 			ISER_RX_SIZE, DMA_FROM_DEVICE);
1390b8d26b3bSNicholas Bellinger 
13919679cc51SChristoph Hellwig 	isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
1392b8d26b3bSNicholas Bellinger 
13932371e5daSSagi Grimberg 	if (isert_conn->conn) {
13942371e5daSSagi Grimberg 		struct iscsi_login *login = isert_conn->conn->conn_login;
13952371e5daSSagi Grimberg 
13962371e5daSSagi Grimberg 		if (login && !login->first_request)
13972371e5daSSagi Grimberg 			isert_rx_login_req(isert_conn);
13982371e5daSSagi Grimberg 	}
13999679cc51SChristoph Hellwig 
1400dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
14012371e5daSSagi Grimberg 	complete(&isert_conn->login_req_comp);
1402dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
1403b8d26b3bSNicholas Bellinger 
14040b089c1eSSagi Grimberg 	ib_dma_sync_single_for_device(ib_dev, isert_conn->login_desc->dma_addr,
14050b089c1eSSagi Grimberg 				ISER_RX_SIZE, DMA_FROM_DEVICE);
1406b8d26b3bSNicholas Bellinger }
1407b8d26b3bSNicholas Bellinger 
1408e3d7e4c3SSagi Grimberg static void
140938a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
1410e3d7e4c3SSagi Grimberg {
141166cd9d4eSMax Gurtovoy 	struct se_cmd *se_cmd = &cmd->iscsit_cmd->se_cmd;
141238a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
1413e3d7e4c3SSagi Grimberg 
141438a2d0d4SChristoph Hellwig 	if (!cmd->rw.nr_ops)
141538a2d0d4SChristoph Hellwig 		return;
141638a2d0d4SChristoph Hellwig 
141738a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
141838a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
141938a2d0d4SChristoph Hellwig 				conn->cm_id->port_num, se_cmd->t_data_sg,
142038a2d0d4SChristoph Hellwig 				se_cmd->t_data_nents, se_cmd->t_prot_sg,
142138a2d0d4SChristoph Hellwig 				se_cmd->t_prot_nents, dir);
142238a2d0d4SChristoph Hellwig 	} else {
142338a2d0d4SChristoph Hellwig 		rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
142438a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
1425e3d7e4c3SSagi Grimberg 	}
1426e3d7e4c3SSagi Grimberg 
142738a2d0d4SChristoph Hellwig 	cmd->rw.nr_ops = 0;
142859464ef4SVu Pham }
142959464ef4SVu Pham 
143059464ef4SVu Pham static void
143103e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1432b8d26b3bSNicholas Bellinger {
143366cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = isert_cmd->iscsit_cmd;
1434b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1435be36d683SMax Gurtovoy 	struct iscsit_conn *conn = isert_conn->conn;
1436e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1437b8d26b3bSNicholas Bellinger 
14384c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1439b8d26b3bSNicholas Bellinger 
1440b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1441b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1442b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1443b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14445159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1445b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1446b8d26b3bSNicholas Bellinger 
144703e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1448b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
144903e7848aSNicholas Bellinger 			/*
145003e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
145103e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
145203e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
145303e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
145403e7848aSNicholas Bellinger 			 */
145503e7848aSNicholas Bellinger 			if (comp_err &&
145603e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
145703e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
145803e7848aSNicholas Bellinger 
1459afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
146003e7848aSNicholas Bellinger 			}
146103e7848aSNicholas Bellinger 		}
1462b8d26b3bSNicholas Bellinger 
146338a2d0d4SChristoph Hellwig 		isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1464186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1465186a9647SNicholas Bellinger 		break;
1466b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1467186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1468186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14695159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1470186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1471186a9647SNicholas Bellinger 
1472b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1473b8d26b3bSNicholas Bellinger 		break;
1474b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1475b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1476adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1477e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1478e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1479e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1480e4f4e801SSagi Grimberg 			break;
1481e4f4e801SSagi Grimberg 
1482b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1483b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14845159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1485b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1486b8d26b3bSNicholas Bellinger 
1487b8d26b3bSNicholas Bellinger 		/*
1488b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1489b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1490b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1491b8d26b3bSNicholas Bellinger 		 */
1492b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
149311378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
14943df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1495b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1496b8d26b3bSNicholas Bellinger 			break;
1497b8d26b3bSNicholas Bellinger 		}
1498df561f66SGustavo A. R. Silva 		fallthrough;
1499b8d26b3bSNicholas Bellinger 	default:
1500d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1501b8d26b3bSNicholas Bellinger 		break;
1502b8d26b3bSNicholas Bellinger 	}
1503b8d26b3bSNicholas Bellinger }
1504b8d26b3bSNicholas Bellinger 
1505b8d26b3bSNicholas Bellinger static void
1506b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1507b8d26b3bSNicholas Bellinger {
1508b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
15094c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1510b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1511b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1512b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1513b8d26b3bSNicholas Bellinger 	}
1514b8d26b3bSNicholas Bellinger }
1515b8d26b3bSNicholas Bellinger 
1516b8d26b3bSNicholas Bellinger static void
1517b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
151803e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1519b8d26b3bSNicholas Bellinger {
1520dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
15214c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1522dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1523dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1524dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1525b8d26b3bSNicholas Bellinger 	}
1526b8d26b3bSNicholas Bellinger 
1527b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
152803e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1529b8d26b3bSNicholas Bellinger }
1530b8d26b3bSNicholas Bellinger 
153196b7973eSSagi Grimberg static int
153296b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1533f93f3a70SSagi Grimberg {
15349e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
153596b7973eSSagi Grimberg 	int ret;
1536f93f3a70SSagi Grimberg 
153796b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
15389e961ae7SSagi Grimberg 	if (ret) {
153924f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
15409e961ae7SSagi Grimberg 		goto fail_mr_status;
15419e961ae7SSagi Grimberg 	}
154296b7973eSSagi Grimberg 
15439e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
154496b7973eSSagi Grimberg 		u64 sec_offset_err;
15459e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
15469e961ae7SSagi Grimberg 
15479e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
15489e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
15499e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
15509e961ae7SSagi Grimberg 			break;
15519e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
15529e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
15539e961ae7SSagi Grimberg 			break;
15549e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
15559e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
15569e961ae7SSagi Grimberg 			break;
15579e961ae7SSagi Grimberg 		}
155896b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
155996b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
15608dd992fbSDavid Disseldorp 		se_cmd->sense_info = sec_offset_err + se_cmd->t_task_lba;
15619e961ae7SSagi Grimberg 
15624c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
15639e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
15649e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
15658dd992fbSDavid Disseldorp 			  (unsigned long long)se_cmd->sense_info,
15669e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
15679e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
15689e961ae7SSagi Grimberg 		ret = 1;
15699e961ae7SSagi Grimberg 	}
15709e961ae7SSagi Grimberg 
15719e961ae7SSagi Grimberg fail_mr_status:
157296b7973eSSagi Grimberg 	return ret;
157396b7973eSSagi Grimberg }
157496b7973eSSagi Grimberg 
157596b7973eSSagi Grimberg static void
15769679cc51SChristoph Hellwig isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
157796b7973eSSagi Grimberg {
15789679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
1579dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
15809679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1581e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
158266cd9d4eSMax Gurtovoy 	struct se_cmd *cmd = &isert_cmd->iscsit_cmd->se_cmd;
158396b7973eSSagi Grimberg 	int ret = 0;
158496b7973eSSagi Grimberg 
15859679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
15869679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma write");
15879679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
15889679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
15899679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
15909679cc51SChristoph Hellwig 		return;
15919679cc51SChristoph Hellwig 	}
15929679cc51SChristoph Hellwig 
15939679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
15949679cc51SChristoph Hellwig 
1595e9a53e73SIsrael Rukshin 	ret = isert_check_pi_status(cmd, isert_cmd->rw.reg->mr);
159638a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
159796b7973eSSagi Grimberg 
1598555a65f6SNicholas Bellinger 	if (ret) {
1599555a65f6SNicholas Bellinger 		/*
1600555a65f6SNicholas Bellinger 		 * transport_generic_request_failure() expects to have
1601555a65f6SNicholas Bellinger 		 * plus two references to handle queue-full, so re-add
1602555a65f6SNicholas Bellinger 		 * one here as target-core will have already dropped
1603555a65f6SNicholas Bellinger 		 * it after the first isert_put_datain() callback.
1604555a65f6SNicholas Bellinger 		 */
1605555a65f6SNicholas Bellinger 		kref_get(&cmd->cmd_kref);
1606555a65f6SNicholas Bellinger 		transport_generic_request_failure(cmd, cmd->pi_err);
1607555a65f6SNicholas Bellinger 	} else {
1608555a65f6SNicholas Bellinger 		/*
1609555a65f6SNicholas Bellinger 		 * XXX: isert_put_response() failure is not retried.
1610555a65f6SNicholas Bellinger 		 */
161166cd9d4eSMax Gurtovoy 		ret = isert_put_response(isert_conn->conn, isert_cmd->iscsit_cmd);
16129e961ae7SSagi Grimberg 		if (ret)
1613555a65f6SNicholas Bellinger 			pr_warn_ratelimited("isert_put_response() ret: %d\n", ret);
1614555a65f6SNicholas Bellinger 	}
1615f93f3a70SSagi Grimberg }
1616f93f3a70SSagi Grimberg 
1617f93f3a70SSagi Grimberg static void
16189679cc51SChristoph Hellwig isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1619b8d26b3bSNicholas Bellinger {
16209679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16219679cc51SChristoph Hellwig 	struct isert_device *device = isert_conn->device;
16229679cc51SChristoph Hellwig 	struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
1623e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
162466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = isert_cmd->iscsit_cmd;
1625b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
16265bac4b1aSSagi Grimberg 	int ret = 0;
1627b8d26b3bSNicholas Bellinger 
16289679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
16299679cc51SChristoph Hellwig 		isert_print_wc(wc, "rdma read");
16309679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
16319679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
16329679cc51SChristoph Hellwig 		isert_completion_put(desc, isert_cmd, device->ib_device, true);
16339679cc51SChristoph Hellwig 		return;
16349679cc51SChristoph Hellwig 	}
16359679cc51SChristoph Hellwig 
16369679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
16379679cc51SChristoph Hellwig 
1638b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
163938a2d0d4SChristoph Hellwig 
164038a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd))
1641e9a53e73SIsrael Rukshin 		ret = isert_check_pi_status(se_cmd, isert_cmd->rw.reg->mr);
164238a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
164338a2d0d4SChristoph Hellwig 	cmd->write_data_done = 0;
1644b8d26b3bSNicholas Bellinger 
164524f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1646b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1647b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1648b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1649b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1650b8d26b3bSNicholas Bellinger 
1651555a65f6SNicholas Bellinger 	/*
1652555a65f6SNicholas Bellinger 	 * transport_generic_request_failure() will drop the extra
1653555a65f6SNicholas Bellinger 	 * se_cmd->cmd_kref reference after T10-PI error, and handle
1654555a65f6SNicholas Bellinger 	 * any non-zero ->queue_status() callback error retries.
1655555a65f6SNicholas Bellinger 	 */
1656555a65f6SNicholas Bellinger 	if (ret)
1657555a65f6SNicholas Bellinger 		transport_generic_request_failure(se_cmd, se_cmd->pi_err);
1658555a65f6SNicholas Bellinger 	else
1659b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1660b8d26b3bSNicholas Bellinger }
1661b8d26b3bSNicholas Bellinger 
1662b8d26b3bSNicholas Bellinger static void
1663b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1664b8d26b3bSNicholas Bellinger {
1665b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1666b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1667b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1668dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
166966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = isert_cmd->iscsit_cmd;
1670b8d26b3bSNicholas Bellinger 
16714c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
16724c22e07fSSagi Grimberg 
1673b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1674b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1675b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1676df561f66SGustavo A. R. Silva 		fallthrough;
1677d2271bd0SBart Van Assche 	case ISTATE_SEND_REJECT:
1678d2271bd0SBart Van Assche 	case ISTATE_SEND_TEXTRSP:
1679b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
16804c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
16814c22e07fSSagi Grimberg 				     ib_dev, false);
16823df8f68aSNicholas Bellinger 		break;
1683b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1684b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1685b8d26b3bSNicholas Bellinger 		break;
1686b8d26b3bSNicholas Bellinger 	default:
16874c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1688b8d26b3bSNicholas Bellinger 		dump_stack();
1689b8d26b3bSNicholas Bellinger 		break;
1690b8d26b3bSNicholas Bellinger 	}
1691b8d26b3bSNicholas Bellinger }
1692b8d26b3bSNicholas Bellinger 
1693b8d26b3bSNicholas Bellinger static void
16949679cc51SChristoph Hellwig isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
1695b8d26b3bSNicholas Bellinger {
16969679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
16979679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
16989679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1699b8d26b3bSNicholas Bellinger 
17009679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17019679cc51SChristoph Hellwig 		isert_print_wc(wc, "login send");
17029679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17039679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17049679cc51SChristoph Hellwig 	}
17059679cc51SChristoph Hellwig 
17069679cc51SChristoph Hellwig 	isert_unmap_tx_desc(tx_desc, ib_dev);
17079679cc51SChristoph Hellwig }
17089679cc51SChristoph Hellwig 
17099679cc51SChristoph Hellwig static void
17109679cc51SChristoph Hellwig isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
17119679cc51SChristoph Hellwig {
17129679cc51SChristoph Hellwig 	struct isert_conn *isert_conn = wc->qp->qp_context;
17139679cc51SChristoph Hellwig 	struct ib_device *ib_dev = isert_conn->cm_id->device;
17149679cc51SChristoph Hellwig 	struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
1715e3416ab2SChristoph Hellwig 	struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
17169679cc51SChristoph Hellwig 
17179679cc51SChristoph Hellwig 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
17189679cc51SChristoph Hellwig 		isert_print_wc(wc, "send");
17199679cc51SChristoph Hellwig 		if (wc->status != IB_WC_WR_FLUSH_ERR)
17209679cc51SChristoph Hellwig 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
17219679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
17229679cc51SChristoph Hellwig 		return;
17239679cc51SChristoph Hellwig 	}
17249679cc51SChristoph Hellwig 
17259679cc51SChristoph Hellwig 	isert_dbg("Cmd %p\n", isert_cmd);
17269679cc51SChristoph Hellwig 
172766cd9d4eSMax Gurtovoy 	switch (isert_cmd->iscsit_cmd->i_state) {
17289679cc51SChristoph Hellwig 	case ISTATE_SEND_TASKMGTRSP:
17299679cc51SChristoph Hellwig 	case ISTATE_SEND_LOGOUTRSP:
17309679cc51SChristoph Hellwig 	case ISTATE_SEND_REJECT:
17319679cc51SChristoph Hellwig 	case ISTATE_SEND_TEXTRSP:
1732b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1733b8d26b3bSNicholas Bellinger 
1734b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1735b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1736b8d26b3bSNicholas Bellinger 		return;
1737b8d26b3bSNicholas Bellinger 	default:
173866cd9d4eSMax Gurtovoy 		isert_cmd->iscsit_cmd->i_state = ISTATE_SENT_STATUS;
17399679cc51SChristoph Hellwig 		isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1740b8d26b3bSNicholas Bellinger 		break;
1741b8d26b3bSNicholas Bellinger 	}
1742b8d26b3bSNicholas Bellinger }
1743b8d26b3bSNicholas Bellinger 
1744b8d26b3bSNicholas Bellinger static int
1745b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1746b8d26b3bSNicholas Bellinger {
1747b8d26b3bSNicholas Bellinger 	int ret;
1748b8d26b3bSNicholas Bellinger 
17494366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
1750c97119b6SMax Gurtovoy 	if (ret)
17514366b19cSJenny Derzhavetz 		return ret;
17524366b19cSJenny Derzhavetz 
1753e01a7674SBart Van Assche 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr, NULL);
1754b8d26b3bSNicholas Bellinger 	if (ret) {
175524f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
1756b8d26b3bSNicholas Bellinger 		return ret;
1757b8d26b3bSNicholas Bellinger 	}
1758b8d26b3bSNicholas Bellinger 	return ret;
1759b8d26b3bSNicholas Bellinger }
1760b8d26b3bSNicholas Bellinger 
1761b8d26b3bSNicholas Bellinger static int
1762be36d683SMax Gurtovoy isert_put_response(struct iscsit_conn *conn, struct iscsit_cmd *cmd)
1763b8d26b3bSNicholas Bellinger {
1764d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
17656700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1766b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1767b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1768b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1769b8d26b3bSNicholas Bellinger 
1770b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1771b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1772b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1773b8d26b3bSNicholas Bellinger 	/*
1774b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1775b8d26b3bSNicholas Bellinger 	 */
1776b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1777b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1778b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1779dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
178067cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1781b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1782dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1783b8d26b3bSNicholas Bellinger 
1784b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1785b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1786b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1787b8d26b3bSNicholas Bellinger 
1788b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1789b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1790dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1791b8d26b3bSNicholas Bellinger 
1792dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1793dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1794b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1795def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1796def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1797b8d26b3bSNicholas Bellinger 
1798dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1799dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1800dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
180134efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1802b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1803b8d26b3bSNicholas Bellinger 	}
1804b8d26b3bSNicholas Bellinger 
180568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1806b8d26b3bSNicholas Bellinger 
18074c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
1808b8d26b3bSNicholas Bellinger 
1809b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1810b8d26b3bSNicholas Bellinger }
1811b8d26b3bSNicholas Bellinger 
1812131e6abcSNicholas Bellinger static void
1813be36d683SMax Gurtovoy isert_aborted_task(struct iscsit_conn *conn, struct iscsit_cmd *cmd)
1814131e6abcSNicholas Bellinger {
1815131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18166700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1817131e6abcSNicholas Bellinger 
1818131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
1819131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
1820131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
1821131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1822131e6abcSNicholas Bellinger 
1823131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
1824131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
182538a2d0d4SChristoph Hellwig 	isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1826131e6abcSNicholas Bellinger }
1827131e6abcSNicholas Bellinger 
1828e70beee7SNicholas Bellinger static enum target_prot_op
1829be36d683SMax Gurtovoy isert_get_sup_prot_ops(struct iscsit_conn *conn)
1830e70beee7SNicholas Bellinger {
18316700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1832dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1833e70beee7SNicholas Bellinger 
183423a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
183523a548eeSSagi Grimberg 		if (device->pi_capable) {
183624f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
183723a548eeSSagi Grimberg 			isert_conn->pi_support = true;
1838e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
183923a548eeSSagi Grimberg 		}
184023a548eeSSagi Grimberg 	}
184123a548eeSSagi Grimberg 
184224f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
184323a548eeSSagi Grimberg 	isert_conn->pi_support = false;
1844e70beee7SNicholas Bellinger 
1845e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
1846e70beee7SNicholas Bellinger }
1847e70beee7SNicholas Bellinger 
1848b8d26b3bSNicholas Bellinger static int
1849be36d683SMax Gurtovoy isert_put_nopin(struct iscsit_cmd *cmd, struct iscsit_conn *conn,
1850b8d26b3bSNicholas Bellinger 		bool nopout_response)
1851b8d26b3bSNicholas Bellinger {
1852d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18536700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1854b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1855b8d26b3bSNicholas Bellinger 
1856b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1857b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1858b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1859b8d26b3bSNicholas Bellinger 			       nopout_response);
1860b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
186168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1862b8d26b3bSNicholas Bellinger 
18634c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
1864b8d26b3bSNicholas Bellinger 
1865b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1866b8d26b3bSNicholas Bellinger }
1867b8d26b3bSNicholas Bellinger 
1868b8d26b3bSNicholas Bellinger static int
1869be36d683SMax Gurtovoy isert_put_logout_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
1870b8d26b3bSNicholas Bellinger {
1871d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18726700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1873b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1874b8d26b3bSNicholas Bellinger 
1875b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1876b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1877b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1878b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
187968a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1880b8d26b3bSNicholas Bellinger 
18814c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
1882b8d26b3bSNicholas Bellinger 
1883b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1884b8d26b3bSNicholas Bellinger }
1885b8d26b3bSNicholas Bellinger 
1886b8d26b3bSNicholas Bellinger static int
1887be36d683SMax Gurtovoy isert_put_tm_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
1888b8d26b3bSNicholas Bellinger {
1889d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
18906700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1891b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1892b8d26b3bSNicholas Bellinger 
1893b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1894b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1895b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1896b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
189768a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1898b8d26b3bSNicholas Bellinger 
18994c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
1900b8d26b3bSNicholas Bellinger 
1901b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1902b8d26b3bSNicholas Bellinger }
1903b8d26b3bSNicholas Bellinger 
1904b8d26b3bSNicholas Bellinger static int
1905be36d683SMax Gurtovoy isert_put_reject(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
1906b8d26b3bSNicholas Bellinger {
1907d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19086700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1909b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1910dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
191167cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
19123df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
19133df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
19143df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1915b8d26b3bSNicholas Bellinger 
1916b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
19173df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
1918b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
19193df8f68aSNicholas Bellinger 
19203df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
1921dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
19223df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
19233df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
1924def4a6ffSAlexey Khoroshilov 	if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1925def4a6ffSAlexey Khoroshilov 		return -ENOMEM;
1926dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1927dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
19283df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
192934efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
19303df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
19313df8f68aSNicholas Bellinger 
193268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1933b8d26b3bSNicholas Bellinger 
19344c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
1935b8d26b3bSNicholas Bellinger 
1936b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1937b8d26b3bSNicholas Bellinger }
1938b8d26b3bSNicholas Bellinger 
1939b8d26b3bSNicholas Bellinger static int
1940be36d683SMax Gurtovoy isert_put_text_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
1941adb54c29SNicholas Bellinger {
1942d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
19436700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1944adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1945adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
1946adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1947adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
1948adb54c29SNicholas Bellinger 	int rc;
1949adb54c29SNicholas Bellinger 
1950adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
195122c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
1952adb54c29SNicholas Bellinger 	if (rc < 0)
1953adb54c29SNicholas Bellinger 		return rc;
1954adb54c29SNicholas Bellinger 
1955adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
1956adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1957adb54c29SNicholas Bellinger 
1958adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
1959dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
196067cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
1961adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1962adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
1963adb54c29SNicholas Bellinger 
1964adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1965adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1966def4a6ffSAlexey Khoroshilov 		if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1967def4a6ffSAlexey Khoroshilov 			return -ENOMEM;
1968adb54c29SNicholas Bellinger 
1969adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
1970adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1971adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
197234efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
1973adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1974adb54c29SNicholas Bellinger 	}
197568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
1976adb54c29SNicholas Bellinger 
1977f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
1978adb54c29SNicholas Bellinger 
1979adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1980adb54c29SNicholas Bellinger }
1981adb54c29SNicholas Bellinger 
19823d73cf1aSSagi Grimberg static inline void
1983dae6345aSIsrael Rukshin isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_domain *domain)
19849e961ae7SSagi Grimberg {
198578eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
19863d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
19873d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
19883d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
198978eda2bbSSagi Grimberg 	/*
199078eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
199178eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
199278eda2bbSSagi Grimberg 	 * from se_cmd.
199378eda2bbSSagi Grimberg 	 */
199478eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
199578eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
199678eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
199778eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
199878eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
199978eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
2000ec53a2a6SMax Gurtovoy }
20013d73cf1aSSagi Grimberg 
20023d73cf1aSSagi Grimberg static int
20033d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
20043d73cf1aSSagi Grimberg {
200538a2d0d4SChristoph Hellwig 	memset(sig_attrs, 0, sizeof(*sig_attrs));
200638a2d0d4SChristoph Hellwig 
20079e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
20089e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
20099e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
201078eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
2011dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
20129e961ae7SSagi Grimberg 		break;
20139e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
20149e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
201578eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
2016dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
20179e961ae7SSagi Grimberg 		break;
20189e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
20199e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
2020dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->wire);
2021dae6345aSIsrael Rukshin 		isert_set_dif_domain(se_cmd, &sig_attrs->mem);
20229e961ae7SSagi Grimberg 		break;
20239e961ae7SSagi Grimberg 	default:
202424f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
20259e961ae7SSagi Grimberg 		return -EINVAL;
20269e961ae7SSagi Grimberg 	}
20279e961ae7SSagi Grimberg 
202826232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD)
202926232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
203026232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG)
203126232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
203226232872SMax Gurtovoy 	if (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)
203326232872SMax Gurtovoy 		sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
203426232872SMax Gurtovoy 
20359e961ae7SSagi Grimberg 	return 0;
20369e961ae7SSagi Grimberg }
20379e961ae7SSagi Grimberg 
20389e961ae7SSagi Grimberg static int
203938a2d0d4SChristoph Hellwig isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
204038a2d0d4SChristoph Hellwig 		struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
20419e961ae7SSagi Grimberg {
204266cd9d4eSMax Gurtovoy 	struct se_cmd *se_cmd = &cmd->iscsit_cmd->se_cmd;
204338a2d0d4SChristoph Hellwig 	enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
204438a2d0d4SChristoph Hellwig 	u8 port_num = conn->cm_id->port_num;
204538a2d0d4SChristoph Hellwig 	u64 addr;
204638a2d0d4SChristoph Hellwig 	u32 rkey, offset;
20479e961ae7SSagi Grimberg 	int ret;
20489e961ae7SSagi Grimberg 
204966f53e6fSBharat Potnuri 	if (cmd->ctx_init_done)
205066f53e6fSBharat Potnuri 		goto rdma_ctx_post;
205166f53e6fSBharat Potnuri 
205238a2d0d4SChristoph Hellwig 	if (dir == DMA_FROM_DEVICE) {
205338a2d0d4SChristoph Hellwig 		addr = cmd->write_va;
205438a2d0d4SChristoph Hellwig 		rkey = cmd->write_stag;
205566cd9d4eSMax Gurtovoy 		offset = cmd->iscsit_cmd->write_data_done;
205638a2d0d4SChristoph Hellwig 	} else {
205738a2d0d4SChristoph Hellwig 		addr = cmd->read_va;
205838a2d0d4SChristoph Hellwig 		rkey = cmd->read_stag;
205938a2d0d4SChristoph Hellwig 		offset = 0;
206038a2d0d4SChristoph Hellwig 	}
206138a2d0d4SChristoph Hellwig 
206238a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(conn, se_cmd)) {
206338a2d0d4SChristoph Hellwig 		struct ib_sig_attrs sig_attrs;
206438a2d0d4SChristoph Hellwig 
20659e961ae7SSagi Grimberg 		ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
20669e961ae7SSagi Grimberg 		if (ret)
2067e3d7e4c3SSagi Grimberg 			return ret;
2068e3d7e4c3SSagi Grimberg 
206938a2d0d4SChristoph Hellwig 		WARN_ON_ONCE(offset);
207038a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
207138a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
207238a2d0d4SChristoph Hellwig 				se_cmd->t_prot_sg, se_cmd->t_prot_nents,
207338a2d0d4SChristoph Hellwig 				&sig_attrs, addr, rkey, dir);
2074570db170SSagi Grimberg 	} else {
207538a2d0d4SChristoph Hellwig 		ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
207638a2d0d4SChristoph Hellwig 				se_cmd->t_data_sg, se_cmd->t_data_nents,
207738a2d0d4SChristoph Hellwig 				offset, addr, rkey, dir);
207838a2d0d4SChristoph Hellwig 	}
207966f53e6fSBharat Potnuri 
208038a2d0d4SChristoph Hellwig 	if (ret < 0) {
208138a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
208238a2d0d4SChristoph Hellwig 		return ret;
20839e961ae7SSagi Grimberg 	}
20849e961ae7SSagi Grimberg 
208566f53e6fSBharat Potnuri 	cmd->ctx_init_done = true;
208666f53e6fSBharat Potnuri 
208766f53e6fSBharat Potnuri rdma_ctx_post:
208838a2d0d4SChristoph Hellwig 	ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
208938a2d0d4SChristoph Hellwig 	if (ret < 0)
209038a2d0d4SChristoph Hellwig 		isert_err("Cmd: %p failed to post RDMA res\n", cmd);
209159464ef4SVu Pham 	return ret;
209259464ef4SVu Pham }
209359464ef4SVu Pham 
209459464ef4SVu Pham static int
2095be36d683SMax Gurtovoy isert_put_datain(struct iscsit_conn *conn, struct iscsit_cmd *cmd)
209690ecc6e2SVu Pham {
209790ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
209859464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
20996700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
210038a2d0d4SChristoph Hellwig 	struct ib_cqe *cqe = NULL;
210138a2d0d4SChristoph Hellwig 	struct ib_send_wr *chain_wr = NULL;
210290ecc6e2SVu Pham 	int rc;
210390ecc6e2SVu Pham 
210424f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
210590ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
21064c22e07fSSagi Grimberg 
210738a2d0d4SChristoph Hellwig 	if (isert_prot_cmd(isert_conn, se_cmd)) {
210838a2d0d4SChristoph Hellwig 		isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
210938a2d0d4SChristoph Hellwig 		cqe = &isert_cmd->tx_desc.tx_cqe;
211038a2d0d4SChristoph Hellwig 	} else {
2111b8d26b3bSNicholas Bellinger 		/*
2112b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2113b8d26b3bSNicholas Bellinger 		 */
21149e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
21159e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
211604d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2117b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2118b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
211995b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
212068a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
21214366b19cSJenny Derzhavetz 
21224366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2123c97119b6SMax Gurtovoy 		if (rc)
21244366b19cSJenny Derzhavetz 			return rc;
212538a2d0d4SChristoph Hellwig 
212638a2d0d4SChristoph Hellwig 		chain_wr = &isert_cmd->tx_desc.send_wr;
21279e961ae7SSagi Grimberg 	}
2128b8d26b3bSNicholas Bellinger 
2129555a65f6SNicholas Bellinger 	rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2130555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n",
2131555a65f6SNicholas Bellinger 		  isert_cmd, rc);
2132555a65f6SNicholas Bellinger 	return rc;
2133b8d26b3bSNicholas Bellinger }
2134b8d26b3bSNicholas Bellinger 
2135b8d26b3bSNicholas Bellinger static int
2136be36d683SMax Gurtovoy isert_get_dataout(struct iscsit_conn *conn, struct iscsit_cmd *cmd, bool recovery)
2137b8d26b3bSNicholas Bellinger {
2138d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2139555a65f6SNicholas Bellinger 	int ret;
2140b8d26b3bSNicholas Bellinger 
214124f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
214238a2d0d4SChristoph Hellwig 		 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
2143b8d26b3bSNicholas Bellinger 
214438a2d0d4SChristoph Hellwig 	isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2145555a65f6SNicholas Bellinger 	ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context,
214638a2d0d4SChristoph Hellwig 				     &isert_cmd->tx_desc.tx_cqe, NULL);
2147bdf20e72SSagi Grimberg 
2148555a65f6SNicholas Bellinger 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n",
2149555a65f6SNicholas Bellinger 		 isert_cmd, ret);
2150555a65f6SNicholas Bellinger 	return ret;
2151b8d26b3bSNicholas Bellinger }
2152b8d26b3bSNicholas Bellinger 
2153b8d26b3bSNicholas Bellinger static int
2154be36d683SMax Gurtovoy isert_immediate_queue(struct iscsit_conn *conn, struct iscsit_cmd *cmd, int state)
2155b8d26b3bSNicholas Bellinger {
2156a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2157a4c15cd9SJenny Derzhavetz 	int ret = 0;
2158b8d26b3bSNicholas Bellinger 
2159b8d26b3bSNicholas Bellinger 	switch (state) {
2160a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2161a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2162a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2163a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2164a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2165a4c15cd9SJenny Derzhavetz 		break;
2166b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2167b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2168b8d26b3bSNicholas Bellinger 		break;
2169b8d26b3bSNicholas Bellinger 	default:
217024f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2171b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2172b8d26b3bSNicholas Bellinger 		break;
2173b8d26b3bSNicholas Bellinger 	}
2174b8d26b3bSNicholas Bellinger 
2175b8d26b3bSNicholas Bellinger 	return ret;
2176b8d26b3bSNicholas Bellinger }
2177b8d26b3bSNicholas Bellinger 
2178b8d26b3bSNicholas Bellinger static int
2179be36d683SMax Gurtovoy isert_response_queue(struct iscsit_conn *conn, struct iscsit_cmd *cmd, int state)
2180b8d26b3bSNicholas Bellinger {
2181991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2182b8d26b3bSNicholas Bellinger 	int ret;
2183b8d26b3bSNicholas Bellinger 
2184b8d26b3bSNicholas Bellinger 	switch (state) {
2185b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2186b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2187991bb764SSagi Grimberg 		if (!ret)
2188991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2189b8d26b3bSNicholas Bellinger 		break;
2190b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2191b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2192b8d26b3bSNicholas Bellinger 		break;
2193b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2194b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2195b8d26b3bSNicholas Bellinger 		break;
2196b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2197b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2198b8d26b3bSNicholas Bellinger 		break;
2199adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2200adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2201adb54c29SNicholas Bellinger 		break;
2202b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2203b8d26b3bSNicholas Bellinger 		/*
2204b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2205b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2206b8d26b3bSNicholas Bellinger 		 */
2207b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2208b8d26b3bSNicholas Bellinger 		break;
2209b8d26b3bSNicholas Bellinger 	default:
221024f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2211b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2212b8d26b3bSNicholas Bellinger 		break;
2213b8d26b3bSNicholas Bellinger 	}
2214b8d26b3bSNicholas Bellinger 
2215b8d26b3bSNicholas Bellinger 	return ret;
2216b8d26b3bSNicholas Bellinger }
2217b8d26b3bSNicholas Bellinger 
2218ca6c1d82SSagi Grimberg struct rdma_cm_id *
2219ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2220ca6c1d82SSagi Grimberg {
2221ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2222ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2223ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2224ca6c1d82SSagi Grimberg 	int ret;
2225ca6c1d82SSagi Grimberg 
2226ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
222724f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2228ca6c1d82SSagi Grimberg 
2229fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
2230ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2231ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
223224f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2233ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2234ca6c1d82SSagi Grimberg 		goto out;
2235ca6c1d82SSagi Grimberg 	}
223624f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2237ca6c1d82SSagi Grimberg 
2238221442eaSMax Gurtovoy 	/*
2239221442eaSMax Gurtovoy 	 * Allow both IPv4 and IPv6 sockets to bind a single port
2240221442eaSMax Gurtovoy 	 * at the same time.
2241221442eaSMax Gurtovoy 	 */
2242221442eaSMax Gurtovoy 	ret = rdma_set_afonly(id, 1);
2243221442eaSMax Gurtovoy 	if (ret) {
2244221442eaSMax Gurtovoy 		isert_err("rdma_set_afonly() failed: %d\n", ret);
2245221442eaSMax Gurtovoy 		goto out_id;
2246221442eaSMax Gurtovoy 	}
2247221442eaSMax Gurtovoy 
2248ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2249ca6c1d82SSagi Grimberg 	if (ret) {
225024f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2251ca6c1d82SSagi Grimberg 		goto out_id;
2252ca6c1d82SSagi Grimberg 	}
2253ca6c1d82SSagi Grimberg 
2254992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
2255ca6c1d82SSagi Grimberg 	if (ret) {
225624f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2257ca6c1d82SSagi Grimberg 		goto out_id;
2258ca6c1d82SSagi Grimberg 	}
2259ca6c1d82SSagi Grimberg 
2260ca6c1d82SSagi Grimberg 	return id;
2261ca6c1d82SSagi Grimberg out_id:
2262ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
2263ca6c1d82SSagi Grimberg out:
2264ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
2265ca6c1d82SSagi Grimberg }
2266ca6c1d82SSagi Grimberg 
2267b8d26b3bSNicholas Bellinger static int
2268b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
226913a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
2270b8d26b3bSNicholas Bellinger {
2271b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2272b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2273b8d26b3bSNicholas Bellinger 	int ret;
2274b8d26b3bSNicholas Bellinger 
2275b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
227693b80f29SLeon Romanovsky 	if (!isert_np)
2277b8d26b3bSNicholas Bellinger 		return -ENOMEM;
227893b80f29SLeon Romanovsky 
2279ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
2280ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
2281bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
2282bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
2283ca6c1d82SSagi Grimberg 	isert_np->np = np;
2284b8d26b3bSNicholas Bellinger 
2285b8d26b3bSNicholas Bellinger 	/*
2286b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2287b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2288b8d26b3bSNicholas Bellinger 	 */
2289b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
229013a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
2291b8d26b3bSNicholas Bellinger 
2292ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
2293b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2294b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2295b8d26b3bSNicholas Bellinger 		goto out;
2296b8d26b3bSNicholas Bellinger 	}
2297b8d26b3bSNicholas Bellinger 
2298ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
2299b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2300b8d26b3bSNicholas Bellinger 
2301b8d26b3bSNicholas Bellinger 	return 0;
2302b8d26b3bSNicholas Bellinger 
2303b8d26b3bSNicholas Bellinger out:
2304b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2305ca6c1d82SSagi Grimberg 
2306b8d26b3bSNicholas Bellinger 	return ret;
2307b8d26b3bSNicholas Bellinger }
2308b8d26b3bSNicholas Bellinger 
2309b8d26b3bSNicholas Bellinger static int
2310b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2311b8d26b3bSNicholas Bellinger {
2312dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2313b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2314b8d26b3bSNicholas Bellinger 	int ret;
231513bce482SJenny Derzhavetz 	struct iser_cm_hdr rsp_hdr;
2316b8d26b3bSNicholas Bellinger 
2317b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2318b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2319b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2320b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2321b8d26b3bSNicholas Bellinger 
232213bce482SJenny Derzhavetz 	memset(&rsp_hdr, 0, sizeof(rsp_hdr));
2323422bd0acSJenny Derzhavetz 	rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2324422bd0acSJenny Derzhavetz 	if (!isert_conn->snd_w_inv)
2325422bd0acSJenny Derzhavetz 		rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
232613bce482SJenny Derzhavetz 	cp.private_data = (void *)&rsp_hdr;
232713bce482SJenny Derzhavetz 	cp.private_data_len = sizeof(rsp_hdr);
232813bce482SJenny Derzhavetz 
2329b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2330b8d26b3bSNicholas Bellinger 	if (ret) {
233124f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
2332b8d26b3bSNicholas Bellinger 		return ret;
2333b8d26b3bSNicholas Bellinger 	}
2334b8d26b3bSNicholas Bellinger 
2335b8d26b3bSNicholas Bellinger 	return 0;
2336b8d26b3bSNicholas Bellinger }
2337b8d26b3bSNicholas Bellinger 
2338b8d26b3bSNicholas Bellinger static int
2339be36d683SMax Gurtovoy isert_get_login_rx(struct iscsit_conn *conn, struct iscsi_login *login)
2340b8d26b3bSNicholas Bellinger {
23416700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2342b8d26b3bSNicholas Bellinger 	int ret;
2343b8d26b3bSNicholas Bellinger 
234424f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
23452371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
23462371e5daSSagi Grimberg 	if (ret) {
234724f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
23482371e5daSSagi Grimberg 			  isert_conn);
23492371e5daSSagi Grimberg 		return ret;
23502371e5daSSagi Grimberg 	}
23512371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
23522371e5daSSagi Grimberg 
23536faaa85fSNicholas Bellinger 	/*
23546faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
2355549f39a5STetsuo Handa 	 * kick queue_delayed_work(isert_login_wq, &conn->login_work) as
2356549f39a5STetsuo Handa 	 * the packet is received, which turns this callback from
2357549f39a5STetsuo Handa 	 * iscsi_target_do_login_rx() into a NOP.
23586faaa85fSNicholas Bellinger 	 */
23596faaa85fSNicholas Bellinger 	if (!login->first_request)
23606faaa85fSNicholas Bellinger 		return 0;
2361b8d26b3bSNicholas Bellinger 
23622371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
23632371e5daSSagi Grimberg 
2364dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
2365dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
2366b8d26b3bSNicholas Bellinger 	if (ret)
2367b8d26b3bSNicholas Bellinger 		return ret;
2368b8d26b3bSNicholas Bellinger 
236924f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
23702371e5daSSagi Grimberg 
2371b8d26b3bSNicholas Bellinger 	return 0;
2372b8d26b3bSNicholas Bellinger }
2373b8d26b3bSNicholas Bellinger 
2374b8d26b3bSNicholas Bellinger static void
2375be36d683SMax Gurtovoy isert_set_conn_info(struct iscsi_np *np, struct iscsit_conn *conn,
2376b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2377b8d26b3bSNicholas Bellinger {
2378dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
2379b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2380b8d26b3bSNicholas Bellinger 
2381b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2382b8d26b3bSNicholas Bellinger 
2383dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
2384dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
2385b8d26b3bSNicholas Bellinger }
2386b8d26b3bSNicholas Bellinger 
2387b8d26b3bSNicholas Bellinger static int
2388be36d683SMax Gurtovoy isert_accept_np(struct iscsi_np *np, struct iscsit_conn *conn)
2389b8d26b3bSNicholas Bellinger {
23906700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2391b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2392c6b8e918SSagi Grimberg 	int ret;
2393b8d26b3bSNicholas Bellinger 
2394b8d26b3bSNicholas Bellinger accept_wait:
2395ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
2396c6b8e918SSagi Grimberg 	if (ret)
2397b8d26b3bSNicholas Bellinger 		return -ENODEV;
2398b8d26b3bSNicholas Bellinger 
2399b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2400e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
2401b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
240211378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
2403e346ab34SSagi Grimberg 			 np->np_thread_state);
2404bf194997SLeon Romanovsky 		/*
2405e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
2406e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
2407bf194997SLeon Romanovsky 		 */
2408b8d26b3bSNicholas Bellinger 		return -ENODEV;
2409b8d26b3bSNicholas Bellinger 	}
2410b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2411b8d26b3bSNicholas Bellinger 
2412ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2413bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
2414ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
2415b8d26b3bSNicholas Bellinger 		goto accept_wait;
2416b8d26b3bSNicholas Bellinger 	}
2417bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
2418bd379220SJenny Derzhavetz 			struct isert_conn, node);
2419bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
2420ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2421b8d26b3bSNicholas Bellinger 
2422b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2423b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2424aea92980SJenny Derzhavetz 	isert_conn->state = ISER_CONN_BOUND;
2425b8d26b3bSNicholas Bellinger 
2426b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2427b8d26b3bSNicholas Bellinger 
242824f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
24292371e5daSSagi Grimberg 
2430b8d26b3bSNicholas Bellinger 	return 0;
2431b8d26b3bSNicholas Bellinger }
2432b8d26b3bSNicholas Bellinger 
2433b8d26b3bSNicholas Bellinger static void
2434b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2435b8d26b3bSNicholas Bellinger {
24366700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
2437268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
2438691b0480SSaravanan Vajravel 	LIST_HEAD(drop_conn_list);
2439b8d26b3bSNicholas Bellinger 
2440ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
2441ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
2442b8d26b3bSNicholas Bellinger 
2443268e6811SSagi Grimberg 	/*
2444268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
2445268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
2446268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
2447268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
2448bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
2449268e6811SSagi Grimberg 	 */
2450ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
2451bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
2452bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
2453268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
2454bd379220SJenny Derzhavetz 					 &isert_np->pending,
2455bd379220SJenny Derzhavetz 					 node) {
2456bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
2457bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
2458691b0480SSaravanan Vajravel 			list_move_tail(&isert_conn->node, &drop_conn_list);
2459bd379220SJenny Derzhavetz 		}
2460bd379220SJenny Derzhavetz 	}
2461bd379220SJenny Derzhavetz 
2462bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
2463bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
2464bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
2465bd379220SJenny Derzhavetz 					 &isert_np->accepted,
2466bd379220SJenny Derzhavetz 					 node) {
246724f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
2468268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
2469691b0480SSaravanan Vajravel 			list_move_tail(&isert_conn->node, &drop_conn_list);
2470268e6811SSagi Grimberg 		}
2471268e6811SSagi Grimberg 	}
2472ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
2473268e6811SSagi Grimberg 
2474691b0480SSaravanan Vajravel 	list_for_each_entry_safe(isert_conn, n, &drop_conn_list, node) {
2475691b0480SSaravanan Vajravel 		list_del_init(&isert_conn->node);
2476691b0480SSaravanan Vajravel 		isert_connect_release(isert_conn);
2477691b0480SSaravanan Vajravel 	}
2478691b0480SSaravanan Vajravel 
2479b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2480b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2481b8d26b3bSNicholas Bellinger }
2482b8d26b3bSNicholas Bellinger 
2483b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
2484b02efbfcSSagi Grimberg {
2485b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
2486b02efbfcSSagi Grimberg 						     struct isert_conn,
2487b02efbfcSSagi Grimberg 						     release_work);
2488b02efbfcSSagi Grimberg 
248924f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
2490b02efbfcSSagi Grimberg 
2491dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2492b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
2493dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2494b02efbfcSSagi Grimberg 
249524f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
2496b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
2497b02efbfcSSagi Grimberg }
2498b02efbfcSSagi Grimberg 
2499bdf20e72SSagi Grimberg static void
2500991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
2501991bb764SSagi Grimberg {
2502be36d683SMax Gurtovoy 	struct iscsit_conn *conn = isert_conn->conn;
2503991bb764SSagi Grimberg 
25044c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
25054c22e07fSSagi Grimberg 
2506991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
250724f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
2508991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
2509991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
2510991bb764SSagi Grimberg 	}
2511991bb764SSagi Grimberg }
2512991bb764SSagi Grimberg 
251376261adaSBart Van Assche static void
2514be36d683SMax Gurtovoy isert_wait4cmds(struct iscsit_conn *conn)
251576261adaSBart Van Assche {
2516be36d683SMax Gurtovoy 	isert_info("iscsit_conn %p\n", conn);
251776261adaSBart Van Assche 
251876261adaSBart Van Assche 	if (conn->sess) {
25196d256beeSMike Christie 		target_stop_cmd_counter(conn->cmd_cnt);
25206d256beeSMike Christie 		target_wait_for_cmds(conn->cmd_cnt);
252176261adaSBart Van Assche 	}
252276261adaSBart Van Assche }
252376261adaSBart Van Assche 
25243e03c4b0SJenny Derzhavetz /**
25253e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
25263e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
25273e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
25283e03c4b0SJenny Derzhavetz  *
25293e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
25303e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
25313e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
25323e03c4b0SJenny Derzhavetz  */
25333e03c4b0SJenny Derzhavetz static void
2534be36d683SMax Gurtovoy isert_put_unsol_pending_cmds(struct iscsit_conn *conn)
25353e03c4b0SJenny Derzhavetz {
253666cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd, *tmp;
25373e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
25383e03c4b0SJenny Derzhavetz 
25393e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
25403e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
25413e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
25423e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
25433e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
25443e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
25453e03c4b0SJenny Derzhavetz 	}
25463e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
25473e03c4b0SJenny Derzhavetz 
25483e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
25493e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
25503e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
25513e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
25523e03c4b0SJenny Derzhavetz 
25533e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
25543e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
25553e03c4b0SJenny Derzhavetz 		}
25563e03c4b0SJenny Derzhavetz 	}
25573e03c4b0SJenny Derzhavetz }
25583e03c4b0SJenny Derzhavetz 
2559be36d683SMax Gurtovoy static void isert_wait_conn(struct iscsit_conn *conn)
2560b8d26b3bSNicholas Bellinger {
2561b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2562b8d26b3bSNicholas Bellinger 
25634c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
2564b8d26b3bSNicholas Bellinger 
2565dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
2566954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2567dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
2568b2cb9649SNicholas Bellinger 
2569572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
25703e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
257176261adaSBart Van Assche 	isert_wait4cmds(conn);
2572991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
2573954f2372SSagi Grimberg 
2574b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
2575defd8848SNicholas Bellinger }
2576defd8848SNicholas Bellinger 
2577be36d683SMax Gurtovoy static void isert_free_conn(struct iscsit_conn *conn)
2578defd8848SNicholas Bellinger {
2579defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2580b8d26b3bSNicholas Bellinger 
2581572a1434SSagi Grimberg 	ib_drain_qp(isert_conn->qp);
2582b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2583b8d26b3bSNicholas Bellinger }
2584b8d26b3bSNicholas Bellinger 
2585be36d683SMax Gurtovoy static void isert_get_rx_pdu(struct iscsit_conn *conn)
2586187e84f7SVarun Prakash {
2587187e84f7SVarun Prakash 	struct completion comp;
2588187e84f7SVarun Prakash 
2589187e84f7SVarun Prakash 	init_completion(&comp);
2590187e84f7SVarun Prakash 
2591187e84f7SVarun Prakash 	wait_for_completion_interruptible(&comp);
2592187e84f7SVarun Prakash }
2593187e84f7SVarun Prakash 
2594b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2595b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2596b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2597bd027d85SNicholas Bellinger 	.rdma_shutdown		= true,
2598d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2599b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2600b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2601b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2602b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2603defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2604b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2605b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2606b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2607b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2608b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2609b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2610b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2611b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2612131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
2613187e84f7SVarun Prakash 	.iscsit_get_rx_pdu	= isert_get_rx_pdu,
2614e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
2615b8d26b3bSNicholas Bellinger };
2616b8d26b3bSNicholas Bellinger 
2617b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2618b8d26b3bSNicholas Bellinger {
2619549f39a5STetsuo Handa 	isert_login_wq = alloc_workqueue("isert_login_wq", 0, 0);
2620549f39a5STetsuo Handa 	if (!isert_login_wq) {
2621549f39a5STetsuo Handa 		isert_err("Unable to allocate isert_login_wq\n");
2622549f39a5STetsuo Handa 		return -ENOMEM;
2623549f39a5STetsuo Handa 	}
2624b8d26b3bSNicholas Bellinger 
2625631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
2626631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
2627b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
262824f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
2629549f39a5STetsuo Handa 		goto destroy_login_wq;
2630b8d26b3bSNicholas Bellinger 	}
2631b8d26b3bSNicholas Bellinger 
2632b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2633b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
2634b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
263524f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
2636b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
2637b02efbfcSSagi Grimberg 	}
2638b02efbfcSSagi Grimberg 
2639b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
264024f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
2641b02efbfcSSagi Grimberg 
2642b8d26b3bSNicholas Bellinger 	return 0;
2643b8d26b3bSNicholas Bellinger 
2644b02efbfcSSagi Grimberg destroy_comp_wq:
2645b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
2646549f39a5STetsuo Handa destroy_login_wq:
2647549f39a5STetsuo Handa 	destroy_workqueue(isert_login_wq);
26486f0fae3dSSagi Grimberg 
2649549f39a5STetsuo Handa 	return -ENOMEM;
2650b8d26b3bSNicholas Bellinger }
2651b8d26b3bSNicholas Bellinger 
2652b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2653b8d26b3bSNicholas Bellinger {
2654549f39a5STetsuo Handa 	flush_workqueue(isert_login_wq);
2655b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
2656b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2657b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
26584c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
2659549f39a5STetsuo Handa 	destroy_workqueue(isert_login_wq);
2660b8d26b3bSNicholas Bellinger }
2661b8d26b3bSNicholas Bellinger 
2662b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2663b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2664b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2665b8d26b3bSNicholas Bellinger 
2666b8d26b3bSNicholas Bellinger module_init(isert_init);
2667b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2668