xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 4a579da2586bd3b79b025947ea24ede2bbfede62)
1b8d26b3bSNicholas Bellinger /*******************************************************************************
2b8d26b3bSNicholas Bellinger  * This file contains iSCSI extentions for RDMA (iSER) Verbs
3b8d26b3bSNicholas Bellinger  *
44c76251eSNicholas Bellinger  * (c) Copyright 2013 Datera, Inc.
5b8d26b3bSNicholas Bellinger  *
6b8d26b3bSNicholas Bellinger  * Nicholas A. Bellinger <nab@linux-iscsi.org>
7b8d26b3bSNicholas Bellinger  *
8b8d26b3bSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
9b8d26b3bSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
10b8d26b3bSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
11b8d26b3bSNicholas Bellinger  * (at your option) any later version.
12b8d26b3bSNicholas Bellinger  *
13b8d26b3bSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
14b8d26b3bSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15b8d26b3bSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16b8d26b3bSNicholas Bellinger  * GNU General Public License for more details.
17b8d26b3bSNicholas Bellinger  ****************************************************************************/
18b8d26b3bSNicholas Bellinger 
19b8d26b3bSNicholas Bellinger #include <linux/string.h>
20b8d26b3bSNicholas Bellinger #include <linux/module.h>
21b8d26b3bSNicholas Bellinger #include <linux/scatterlist.h>
22b8d26b3bSNicholas Bellinger #include <linux/socket.h>
23b8d26b3bSNicholas Bellinger #include <linux/in.h>
24b8d26b3bSNicholas Bellinger #include <linux/in6.h>
25b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h>
26b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h>
27b8d26b3bSNicholas Bellinger #include <target/target_core_base.h>
28b8d26b3bSNicholas Bellinger #include <target/target_core_fabric.h>
29b8d26b3bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
30531b7bf4SSagi Grimberg #include <linux/semaphore.h>
31b8d26b3bSNicholas Bellinger 
32b8d26b3bSNicholas Bellinger #include "isert_proto.h"
33b8d26b3bSNicholas Bellinger #include "ib_isert.h"
34b8d26b3bSNicholas Bellinger 
35b8d26b3bSNicholas Bellinger #define	ISERT_MAX_CONN		8
36b8d26b3bSNicholas Bellinger #define ISER_MAX_RX_CQ_LEN	(ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37b8d26b3bSNicholas Bellinger #define ISER_MAX_TX_CQ_LEN	(ISERT_QP_MAX_REQ_DTOS  * ISERT_MAX_CONN)
38bdf20e72SSagi Grimberg #define ISER_MAX_CQ_LEN		(ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39bdf20e72SSagi Grimberg 				 ISERT_MAX_CONN)
40b8d26b3bSNicholas Bellinger 
4145678b6bSSagi Grimberg static int isert_debug_level;
4224f412ddSSagi Grimberg module_param_named(debug_level, isert_debug_level, int, 0644);
4324f412ddSSagi Grimberg MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
4424f412ddSSagi Grimberg 
45b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
46b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
47b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
48b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
49b8d26b3bSNicholas Bellinger 
50b8d26b3bSNicholas Bellinger static void
51d40945d8SVu Pham isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52d40945d8SVu Pham static int
53d40945d8SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54d40945d8SVu Pham 	       struct isert_rdma_wr *wr);
5559464ef4SVu Pham static void
56a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
5759464ef4SVu Pham static int
58a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
5959464ef4SVu Pham 	       struct isert_rdma_wr *wr);
60f93f3a70SSagi Grimberg static int
61f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
622371e5daSSagi Grimberg static int
632371e5daSSagi Grimberg isert_rdma_post_recvl(struct isert_conn *isert_conn);
642371e5daSSagi Grimberg static int
652371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
66ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
67d40945d8SVu Pham 
68302cc7c3SSagi Grimberg static inline bool
69302cc7c3SSagi Grimberg isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
70302cc7c3SSagi Grimberg {
7123a548eeSSagi Grimberg 	return (conn->pi_support &&
72302cc7c3SSagi Grimberg 		cmd->prot_op != TARGET_PROT_NORMAL);
73302cc7c3SSagi Grimberg }
74302cc7c3SSagi Grimberg 
75302cc7c3SSagi Grimberg 
76d40945d8SVu Pham static void
77b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
78b8d26b3bSNicholas Bellinger {
79b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)context;
80b8d26b3bSNicholas Bellinger 
814c22e07fSSagi Grimberg 	isert_err("conn %p event: %d\n", isert_conn, e->event);
82b8d26b3bSNicholas Bellinger 	switch (e->event) {
83b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
84b8d26b3bSNicholas Bellinger 		rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
85b8d26b3bSNicholas Bellinger 		break;
86b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
874c22e07fSSagi Grimberg 		isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
88b8d26b3bSNicholas Bellinger 		break;
89b8d26b3bSNicholas Bellinger 	default:
90b8d26b3bSNicholas Bellinger 		break;
91b8d26b3bSNicholas Bellinger 	}
92b8d26b3bSNicholas Bellinger }
93b8d26b3bSNicholas Bellinger 
94b8d26b3bSNicholas Bellinger static int
95b8d26b3bSNicholas Bellinger isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
96b8d26b3bSNicholas Bellinger {
97b8d26b3bSNicholas Bellinger 	int ret;
98b8d26b3bSNicholas Bellinger 
99b8d26b3bSNicholas Bellinger 	ret = ib_query_device(ib_dev, devattr);
100b8d26b3bSNicholas Bellinger 	if (ret) {
10124f412ddSSagi Grimberg 		isert_err("ib_query_device() failed: %d\n", ret);
102b8d26b3bSNicholas Bellinger 		return ret;
103b8d26b3bSNicholas Bellinger 	}
10424f412ddSSagi Grimberg 	isert_dbg("devattr->max_sge: %d\n", devattr->max_sge);
10524f412ddSSagi Grimberg 	isert_dbg("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
106b8d26b3bSNicholas Bellinger 
107b8d26b3bSNicholas Bellinger 	return 0;
108b8d26b3bSNicholas Bellinger }
109b8d26b3bSNicholas Bellinger 
110b8d26b3bSNicholas Bellinger static int
111570db170SSagi Grimberg isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
112b8d26b3bSNicholas Bellinger {
113b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
114b8d26b3bSNicholas Bellinger 	struct ib_qp_init_attr attr;
1154a295baeSSagi Grimberg 	struct isert_comp *comp;
1164a295baeSSagi Grimberg 	int ret, i, min = 0;
117b8d26b3bSNicholas Bellinger 
118b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
1194a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++)
1204a295baeSSagi Grimberg 		if (device->comps[i].active_qps <
1214a295baeSSagi Grimberg 		    device->comps[min].active_qps)
1224a295baeSSagi Grimberg 			min = i;
1234a295baeSSagi Grimberg 	comp = &device->comps[min];
1244a295baeSSagi Grimberg 	comp->active_qps++;
12524f412ddSSagi Grimberg 	isert_info("conn %p, using comp %p min_index: %d\n",
1264a295baeSSagi Grimberg 		   isert_conn, comp, min);
127b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
128b8d26b3bSNicholas Bellinger 
129b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
130b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
131b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
1326f0fae3dSSagi Grimberg 	attr.send_cq = comp->cq;
1336f0fae3dSSagi Grimberg 	attr.recv_cq = comp->cq;
134b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
135bdf20e72SSagi Grimberg 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
136b8d26b3bSNicholas Bellinger 	/*
137b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
138f57915cfSOr Gerlitz 	 * work-around for RDMA_READs with ConnectX-2.
139f57915cfSOr Gerlitz 	 *
140f57915cfSOr Gerlitz 	 * Also, still make sure to have at least two SGEs for
141f57915cfSOr Gerlitz 	 * outgoing control PDU responses.
142b8d26b3bSNicholas Bellinger 	 */
143f57915cfSOr Gerlitz 	attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
144b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
145b8d26b3bSNicholas Bellinger 
146b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
147b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
148b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
149570db170SSagi Grimberg 	if (device->pi_capable)
150d3e125daSSagi Grimberg 		attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
151b8d26b3bSNicholas Bellinger 
152b8d26b3bSNicholas Bellinger 	ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
153b8d26b3bSNicholas Bellinger 	if (ret) {
15424f412ddSSagi Grimberg 		isert_err("rdma_create_qp failed for cma_id %d\n", ret);
15519e2090fSSagi Grimberg 		goto err;
156b8d26b3bSNicholas Bellinger 	}
157b8d26b3bSNicholas Bellinger 	isert_conn->conn_qp = cma_id->qp;
158b8d26b3bSNicholas Bellinger 
159b8d26b3bSNicholas Bellinger 	return 0;
16019e2090fSSagi Grimberg err:
16119e2090fSSagi Grimberg 	mutex_lock(&device_list_mutex);
1624a295baeSSagi Grimberg 	comp->active_qps--;
16319e2090fSSagi Grimberg 	mutex_unlock(&device_list_mutex);
16419e2090fSSagi Grimberg 
16519e2090fSSagi Grimberg 	return ret;
166b8d26b3bSNicholas Bellinger }
167b8d26b3bSNicholas Bellinger 
168b8d26b3bSNicholas Bellinger static void
169b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
170b8d26b3bSNicholas Bellinger {
1714c22e07fSSagi Grimberg 	isert_dbg("event: %d\n", e->event);
172b8d26b3bSNicholas Bellinger }
173b8d26b3bSNicholas Bellinger 
174b8d26b3bSNicholas Bellinger static int
175b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
176b8d26b3bSNicholas Bellinger {
177b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
178b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
179b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
180b8d26b3bSNicholas Bellinger 	u64 dma_addr;
181b8d26b3bSNicholas Bellinger 	int i, j;
182b8d26b3bSNicholas Bellinger 
183b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
184b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
185b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
186b8d26b3bSNicholas Bellinger 		goto fail;
187b8d26b3bSNicholas Bellinger 
188b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
189b8d26b3bSNicholas Bellinger 
190b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
191b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
192b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
193b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
194b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
195b8d26b3bSNicholas Bellinger 
196b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
197b8d26b3bSNicholas Bellinger 
198b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
199b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
200b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
201b8d26b3bSNicholas Bellinger 		rx_sg->lkey = isert_conn->conn_mr->lkey;
202b8d26b3bSNicholas Bellinger 	}
203b8d26b3bSNicholas Bellinger 
204b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
2054c22e07fSSagi Grimberg 
206b8d26b3bSNicholas Bellinger 	return 0;
207b8d26b3bSNicholas Bellinger 
208b8d26b3bSNicholas Bellinger dma_map_fail:
209b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
210b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
211b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
212b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
213b8d26b3bSNicholas Bellinger 	}
214b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
215b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
216b8d26b3bSNicholas Bellinger fail:
2174c22e07fSSagi Grimberg 	isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
2184c22e07fSSagi Grimberg 
219b8d26b3bSNicholas Bellinger 	return -ENOMEM;
220b8d26b3bSNicholas Bellinger }
221b8d26b3bSNicholas Bellinger 
222b8d26b3bSNicholas Bellinger static void
223b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
224b8d26b3bSNicholas Bellinger {
225*4a579da2SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_device->ib_device;
226b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
227b8d26b3bSNicholas Bellinger 	int i;
228b8d26b3bSNicholas Bellinger 
229b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
230b8d26b3bSNicholas Bellinger 		return;
231b8d26b3bSNicholas Bellinger 
232b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
233b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
234b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
235b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
236b8d26b3bSNicholas Bellinger 	}
237b8d26b3bSNicholas Bellinger 
238b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
239b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
240b8d26b3bSNicholas Bellinger }
241b8d26b3bSNicholas Bellinger 
2426f0fae3dSSagi Grimberg static void isert_cq_work(struct work_struct *);
2436f0fae3dSSagi Grimberg static void isert_cq_callback(struct ib_cq *, void *);
244b8d26b3bSNicholas Bellinger 
245b8d26b3bSNicholas Bellinger static int
246b8d26b3bSNicholas Bellinger isert_create_device_ib_res(struct isert_device *device)
247b8d26b3bSNicholas Bellinger {
248b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = device->ib_device;
24959464ef4SVu Pham 	struct ib_device_attr *dev_attr;
2504a295baeSSagi Grimberg 	int ret = 0, i;
2516f0fae3dSSagi Grimberg 	int max_cqe;
252b8d26b3bSNicholas Bellinger 
25359464ef4SVu Pham 	dev_attr = &device->dev_attr;
25459464ef4SVu Pham 	ret = isert_query_device(ib_dev, dev_attr);
25559464ef4SVu Pham 	if (ret)
25659464ef4SVu Pham 		return ret;
25759464ef4SVu Pham 
2586f0fae3dSSagi Grimberg 	max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe);
259b1a5ad00SChris Moore 
260d40945d8SVu Pham 	/* asign function handlers */
261f2252258SSagi Grimberg 	if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
262f2252258SSagi Grimberg 	    dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
263a3a5a826SSagi Grimberg 		device->use_fastreg = 1;
264a3a5a826SSagi Grimberg 		device->reg_rdma_mem = isert_reg_rdma;
265a3a5a826SSagi Grimberg 		device->unreg_rdma_mem = isert_unreg_rdma;
26659464ef4SVu Pham 	} else {
267a3a5a826SSagi Grimberg 		device->use_fastreg = 0;
268d40945d8SVu Pham 		device->reg_rdma_mem = isert_map_rdma;
269d40945d8SVu Pham 		device->unreg_rdma_mem = isert_unmap_cmd;
27059464ef4SVu Pham 	}
271d40945d8SVu Pham 
272d3e125daSSagi Grimberg 	/* Check signature cap */
273d3e125daSSagi Grimberg 	device->pi_capable = dev_attr->device_cap_flags &
274d3e125daSSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
275d3e125daSSagi Grimberg 
2764a295baeSSagi Grimberg 	device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
2774a295baeSSagi Grimberg 					device->ib_device->num_comp_vectors));
27824f412ddSSagi Grimberg 	isert_info("Using %d CQs, %s supports %d vectors support "
279d3e125daSSagi Grimberg 		   "Fast registration %d pi_capable %d\n",
2804a295baeSSagi Grimberg 		   device->comps_used, device->ib_device->name,
281d3e125daSSagi Grimberg 		   device->ib_device->num_comp_vectors, device->use_fastreg,
282d3e125daSSagi Grimberg 		   device->pi_capable);
2834a295baeSSagi Grimberg 
2844a295baeSSagi Grimberg 	device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
2854a295baeSSagi Grimberg 				GFP_KERNEL);
2864a295baeSSagi Grimberg 	if (!device->comps) {
28724f412ddSSagi Grimberg 		isert_err("Unable to allocate completion contexts\n");
288b8d26b3bSNicholas Bellinger 		return -ENOMEM;
289b8d26b3bSNicholas Bellinger 	}
290b8d26b3bSNicholas Bellinger 
2914a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
2924a295baeSSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
293b8d26b3bSNicholas Bellinger 
2944a295baeSSagi Grimberg 		comp->device = device;
2956f0fae3dSSagi Grimberg 		INIT_WORK(&comp->work, isert_cq_work);
2966f0fae3dSSagi Grimberg 		comp->cq = ib_create_cq(device->ib_device,
2976f0fae3dSSagi Grimberg 					isert_cq_callback,
298b8d26b3bSNicholas Bellinger 					isert_cq_event_callback,
2994a295baeSSagi Grimberg 					(void *)comp,
3006f0fae3dSSagi Grimberg 					max_cqe, i);
3016f0fae3dSSagi Grimberg 		if (IS_ERR(comp->cq)) {
3026f0fae3dSSagi Grimberg 			ret = PTR_ERR(comp->cq);
3036f0fae3dSSagi Grimberg 			comp->cq = NULL;
304b8d26b3bSNicholas Bellinger 			goto out_cq;
30594a71110SWei Yongjun 		}
306b8d26b3bSNicholas Bellinger 
3076f0fae3dSSagi Grimberg 		ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
30894a71110SWei Yongjun 		if (ret)
309b8d26b3bSNicholas Bellinger 			goto out_cq;
310b8d26b3bSNicholas Bellinger 	}
311b8d26b3bSNicholas Bellinger 
312b8d26b3bSNicholas Bellinger 	return 0;
313b8d26b3bSNicholas Bellinger 
314b8d26b3bSNicholas Bellinger out_cq:
3154a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
3164a295baeSSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
317b8d26b3bSNicholas Bellinger 
3186f0fae3dSSagi Grimberg 		if (comp->cq) {
3196f0fae3dSSagi Grimberg 			cancel_work_sync(&comp->work);
3206f0fae3dSSagi Grimberg 			ib_destroy_cq(comp->cq);
321b8d26b3bSNicholas Bellinger 		}
322b8d26b3bSNicholas Bellinger 	}
3234a295baeSSagi Grimberg 	kfree(device->comps);
324b8d26b3bSNicholas Bellinger 
325b8d26b3bSNicholas Bellinger 	return ret;
326b8d26b3bSNicholas Bellinger }
327b8d26b3bSNicholas Bellinger 
328b8d26b3bSNicholas Bellinger static void
329b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
330b8d26b3bSNicholas Bellinger {
331b8d26b3bSNicholas Bellinger 	int i;
332b8d26b3bSNicholas Bellinger 
33324f412ddSSagi Grimberg 	isert_info("device %p\n", device);
334b8d26b3bSNicholas Bellinger 
3354a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
3364a295baeSSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
3374a295baeSSagi Grimberg 
3386f0fae3dSSagi Grimberg 		cancel_work_sync(&comp->work);
3396f0fae3dSSagi Grimberg 		ib_destroy_cq(comp->cq);
3406f0fae3dSSagi Grimberg 		comp->cq = NULL;
341b8d26b3bSNicholas Bellinger 	}
3424a295baeSSagi Grimberg 	kfree(device->comps);
343b8d26b3bSNicholas Bellinger }
344b8d26b3bSNicholas Bellinger 
345b8d26b3bSNicholas Bellinger static void
346b8d26b3bSNicholas Bellinger isert_device_try_release(struct isert_device *device)
347b8d26b3bSNicholas Bellinger {
348b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
349b8d26b3bSNicholas Bellinger 	device->refcount--;
3504c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
351b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
352b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
353b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
354b8d26b3bSNicholas Bellinger 		kfree(device);
355b8d26b3bSNicholas Bellinger 	}
356b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
357b8d26b3bSNicholas Bellinger }
358b8d26b3bSNicholas Bellinger 
359b8d26b3bSNicholas Bellinger static struct isert_device *
360b8d26b3bSNicholas Bellinger isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
361b8d26b3bSNicholas Bellinger {
362b8d26b3bSNicholas Bellinger 	struct isert_device *device;
363b8d26b3bSNicholas Bellinger 	int ret;
364b8d26b3bSNicholas Bellinger 
365b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
366b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
367b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
368b8d26b3bSNicholas Bellinger 			device->refcount++;
3694c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
3704c22e07fSSagi Grimberg 				   device, device->refcount);
371b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
372b8d26b3bSNicholas Bellinger 			return device;
373b8d26b3bSNicholas Bellinger 		}
374b8d26b3bSNicholas Bellinger 	}
375b8d26b3bSNicholas Bellinger 
376b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
377b8d26b3bSNicholas Bellinger 	if (!device) {
378b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
379b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
380b8d26b3bSNicholas Bellinger 	}
381b8d26b3bSNicholas Bellinger 
382b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
383b8d26b3bSNicholas Bellinger 
384b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
385b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
386b8d26b3bSNicholas Bellinger 	if (ret) {
387b8d26b3bSNicholas Bellinger 		kfree(device);
388b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
389b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
390b8d26b3bSNicholas Bellinger 	}
391b8d26b3bSNicholas Bellinger 
392b8d26b3bSNicholas Bellinger 	device->refcount++;
393b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
3944c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
3954c22e07fSSagi Grimberg 		   device, device->refcount);
396b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
397b8d26b3bSNicholas Bellinger 
398b8d26b3bSNicholas Bellinger 	return device;
399b8d26b3bSNicholas Bellinger }
400b8d26b3bSNicholas Bellinger 
40159464ef4SVu Pham static void
402a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
40359464ef4SVu Pham {
40459464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
40559464ef4SVu Pham 	int i = 0;
40659464ef4SVu Pham 
407a3a5a826SSagi Grimberg 	if (list_empty(&isert_conn->conn_fr_pool))
40859464ef4SVu Pham 		return;
40959464ef4SVu Pham 
4104c22e07fSSagi Grimberg 	isert_info("Freeing conn %p fastreg pool", isert_conn);
41159464ef4SVu Pham 
41259464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
413a3a5a826SSagi Grimberg 				 &isert_conn->conn_fr_pool, list) {
41459464ef4SVu Pham 		list_del(&fr_desc->list);
41559464ef4SVu Pham 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
41659464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
417d3e125daSSagi Grimberg 		if (fr_desc->pi_ctx) {
418d3e125daSSagi Grimberg 			ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
419d3e125daSSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
420d3e125daSSagi Grimberg 			ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
421d3e125daSSagi Grimberg 			kfree(fr_desc->pi_ctx);
422d3e125daSSagi Grimberg 		}
42359464ef4SVu Pham 		kfree(fr_desc);
42459464ef4SVu Pham 		++i;
42559464ef4SVu Pham 	}
42659464ef4SVu Pham 
427a3a5a826SSagi Grimberg 	if (i < isert_conn->conn_fr_pool_size)
42824f412ddSSagi Grimberg 		isert_warn("Pool still has %d regions registered\n",
429a3a5a826SSagi Grimberg 			isert_conn->conn_fr_pool_size - i);
43059464ef4SVu Pham }
43159464ef4SVu Pham 
43259464ef4SVu Pham static int
433570db170SSagi Grimberg isert_create_pi_ctx(struct fast_reg_descriptor *desc,
434570db170SSagi Grimberg 		    struct ib_device *device,
435570db170SSagi Grimberg 		    struct ib_pd *pd)
436570db170SSagi Grimberg {
437570db170SSagi Grimberg 	struct ib_mr_init_attr mr_init_attr;
438570db170SSagi Grimberg 	struct pi_context *pi_ctx;
439570db170SSagi Grimberg 	int ret;
440570db170SSagi Grimberg 
441570db170SSagi Grimberg 	pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
442570db170SSagi Grimberg 	if (!pi_ctx) {
44324f412ddSSagi Grimberg 		isert_err("Failed to allocate pi context\n");
444570db170SSagi Grimberg 		return -ENOMEM;
445570db170SSagi Grimberg 	}
446570db170SSagi Grimberg 
447570db170SSagi Grimberg 	pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
448570db170SSagi Grimberg 					    ISCSI_ISER_SG_TABLESIZE);
449570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_frpl)) {
45024f412ddSSagi Grimberg 		isert_err("Failed to allocate prot frpl err=%ld\n",
451570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_frpl));
452570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_frpl);
453570db170SSagi Grimberg 		goto err_pi_ctx;
454570db170SSagi Grimberg 	}
455570db170SSagi Grimberg 
456570db170SSagi Grimberg 	pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
457570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_mr)) {
45824f412ddSSagi Grimberg 		isert_err("Failed to allocate prot frmr err=%ld\n",
459570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_mr));
460570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_mr);
461570db170SSagi Grimberg 		goto err_prot_frpl;
462570db170SSagi Grimberg 	}
463570db170SSagi Grimberg 	desc->ind |= ISERT_PROT_KEY_VALID;
464570db170SSagi Grimberg 
465570db170SSagi Grimberg 	memset(&mr_init_attr, 0, sizeof(mr_init_attr));
466570db170SSagi Grimberg 	mr_init_attr.max_reg_descriptors = 2;
467570db170SSagi Grimberg 	mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
468570db170SSagi Grimberg 	pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
469570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->sig_mr)) {
47024f412ddSSagi Grimberg 		isert_err("Failed to allocate signature enabled mr err=%ld\n",
471570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->sig_mr));
472570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->sig_mr);
473570db170SSagi Grimberg 		goto err_prot_mr;
474570db170SSagi Grimberg 	}
475570db170SSagi Grimberg 
476570db170SSagi Grimberg 	desc->pi_ctx = pi_ctx;
477570db170SSagi Grimberg 	desc->ind |= ISERT_SIG_KEY_VALID;
478570db170SSagi Grimberg 	desc->ind &= ~ISERT_PROTECTED;
479570db170SSagi Grimberg 
480570db170SSagi Grimberg 	return 0;
481570db170SSagi Grimberg 
482570db170SSagi Grimberg err_prot_mr:
483570db170SSagi Grimberg 	ib_dereg_mr(desc->pi_ctx->prot_mr);
484570db170SSagi Grimberg err_prot_frpl:
485570db170SSagi Grimberg 	ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
486570db170SSagi Grimberg err_pi_ctx:
487570db170SSagi Grimberg 	kfree(desc->pi_ctx);
488570db170SSagi Grimberg 
489570db170SSagi Grimberg 	return ret;
490570db170SSagi Grimberg }
491570db170SSagi Grimberg 
492570db170SSagi Grimberg static int
493dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
494570db170SSagi Grimberg 		     struct fast_reg_descriptor *fr_desc)
495dc87a90fSSagi Grimberg {
496d3e125daSSagi Grimberg 	int ret;
497d3e125daSSagi Grimberg 
498dc87a90fSSagi Grimberg 	fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
499dc87a90fSSagi Grimberg 							 ISCSI_ISER_SG_TABLESIZE);
500dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_frpl)) {
50124f412ddSSagi Grimberg 		isert_err("Failed to allocate data frpl err=%ld\n",
502dc87a90fSSagi Grimberg 			  PTR_ERR(fr_desc->data_frpl));
503dc87a90fSSagi Grimberg 		return PTR_ERR(fr_desc->data_frpl);
504dc87a90fSSagi Grimberg 	}
505dc87a90fSSagi Grimberg 
506dc87a90fSSagi Grimberg 	fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
507dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
50824f412ddSSagi Grimberg 		isert_err("Failed to allocate data frmr err=%ld\n",
509dc87a90fSSagi Grimberg 			  PTR_ERR(fr_desc->data_mr));
510d3e125daSSagi Grimberg 		ret = PTR_ERR(fr_desc->data_mr);
511d3e125daSSagi Grimberg 		goto err_data_frpl;
512dc87a90fSSagi Grimberg 	}
513d3e125daSSagi Grimberg 	fr_desc->ind |= ISERT_DATA_KEY_VALID;
514dc87a90fSSagi Grimberg 
51524f412ddSSagi Grimberg 	isert_dbg("Created fr_desc %p\n", fr_desc);
516dc87a90fSSagi Grimberg 
517dc87a90fSSagi Grimberg 	return 0;
518570db170SSagi Grimberg 
519d3e125daSSagi Grimberg err_data_frpl:
520d3e125daSSagi Grimberg 	ib_free_fast_reg_page_list(fr_desc->data_frpl);
521d3e125daSSagi Grimberg 
522d3e125daSSagi Grimberg 	return ret;
523dc87a90fSSagi Grimberg }
524dc87a90fSSagi Grimberg 
525dc87a90fSSagi Grimberg static int
526570db170SSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
52759464ef4SVu Pham {
52859464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
52959464ef4SVu Pham 	struct isert_device *device = isert_conn->conn_device;
530f46d6a8aSNicholas Bellinger 	struct se_session *se_sess = isert_conn->conn->sess->se_sess;
531f46d6a8aSNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
532f46d6a8aSNicholas Bellinger 	int i, ret, tag_num;
533f46d6a8aSNicholas Bellinger 	/*
534f46d6a8aSNicholas Bellinger 	 * Setup the number of FRMRs based upon the number of tags
535f46d6a8aSNicholas Bellinger 	 * available to session in iscsi_target_locate_portal().
536f46d6a8aSNicholas Bellinger 	 */
537f46d6a8aSNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
538f46d6a8aSNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
53959464ef4SVu Pham 
540a3a5a826SSagi Grimberg 	isert_conn->conn_fr_pool_size = 0;
541f46d6a8aSNicholas Bellinger 	for (i = 0; i < tag_num; i++) {
54259464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
54359464ef4SVu Pham 		if (!fr_desc) {
54424f412ddSSagi Grimberg 			isert_err("Failed to allocate fast_reg descriptor\n");
54559464ef4SVu Pham 			ret = -ENOMEM;
54659464ef4SVu Pham 			goto err;
54759464ef4SVu Pham 		}
54859464ef4SVu Pham 
549dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
550570db170SSagi Grimberg 					   isert_conn->conn_pd, fr_desc);
551dc87a90fSSagi Grimberg 		if (ret) {
55224f412ddSSagi Grimberg 			isert_err("Failed to create fastreg descriptor err=%d\n",
553dc87a90fSSagi Grimberg 			       ret);
554a80e21b3SNicholas Bellinger 			kfree(fr_desc);
55559464ef4SVu Pham 			goto err;
55659464ef4SVu Pham 		}
55759464ef4SVu Pham 
558a3a5a826SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
559a3a5a826SSagi Grimberg 		isert_conn->conn_fr_pool_size++;
56059464ef4SVu Pham 	}
56159464ef4SVu Pham 
56224f412ddSSagi Grimberg 	isert_dbg("Creating conn %p fastreg pool size=%d",
563a3a5a826SSagi Grimberg 		 isert_conn, isert_conn->conn_fr_pool_size);
56459464ef4SVu Pham 
56559464ef4SVu Pham 	return 0;
56659464ef4SVu Pham 
56759464ef4SVu Pham err:
568a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
56959464ef4SVu Pham 	return ret;
57059464ef4SVu Pham }
57159464ef4SVu Pham 
572b8d26b3bSNicholas Bellinger static int
573b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
574b8d26b3bSNicholas Bellinger {
575ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
576ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
577b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
578b8d26b3bSNicholas Bellinger 	struct isert_device *device;
579b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
580b8d26b3bSNicholas Bellinger 	int ret = 0;
58114f4b54fSSagi Grimberg 
58214f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
58314f4b54fSSagi Grimberg 	if (!np->enabled) {
58414f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
58524f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
58614f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
58714f4b54fSSagi Grimberg 	}
58814f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
589b8d26b3bSNicholas Bellinger 
5904c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
591b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
592b8d26b3bSNicholas Bellinger 
593b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
594b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
59524f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_conn\n");
596b8d26b3bSNicholas Bellinger 		return -ENOMEM;
597b8d26b3bSNicholas Bellinger 	}
598b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
599b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
600b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
6012371e5daSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
602defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait);
603b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
604b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
60559464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
606f46d6a8aSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
607b8d26b3bSNicholas Bellinger 
608b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
609b8d26b3bSNicholas Bellinger 
610b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
611b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
612b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
61324f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_conn->login_buf\n");
614b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
615b8d26b3bSNicholas Bellinger 		goto out;
616b8d26b3bSNicholas Bellinger 	}
617b8d26b3bSNicholas Bellinger 
618b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
619b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
620b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
62124f412ddSSagi Grimberg 	isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
622b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
623b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
624b8d26b3bSNicholas Bellinger 
625b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
626b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
627b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
628b8d26b3bSNicholas Bellinger 
629b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
630b8d26b3bSNicholas Bellinger 	if (ret) {
63124f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
632b8d26b3bSNicholas Bellinger 		       ret);
633b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
634b8d26b3bSNicholas Bellinger 		goto out_login_buf;
635b8d26b3bSNicholas Bellinger 	}
636b8d26b3bSNicholas Bellinger 
637b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
638b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
639b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
640b8d26b3bSNicholas Bellinger 
641b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
642b8d26b3bSNicholas Bellinger 	if (ret) {
64324f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
644b8d26b3bSNicholas Bellinger 		       ret);
645b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
646b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
647b8d26b3bSNicholas Bellinger 	}
648b8d26b3bSNicholas Bellinger 
649b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
650b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
651b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
652b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
653b8d26b3bSNicholas Bellinger 	}
654b8d26b3bSNicholas Bellinger 
6551a92e17eSSagi Grimberg 	/* Set max inflight RDMA READ requests */
6561a92e17eSSagi Grimberg 	isert_conn->initiator_depth = min_t(u8,
6571a92e17eSSagi Grimberg 				event->param.conn.initiator_depth,
6581a92e17eSSagi Grimberg 				device->dev_attr.max_qp_init_rd_atom);
65924f412ddSSagi Grimberg 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
6601a92e17eSSagi Grimberg 
661b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
662eb6ab132SSagi Grimberg 	isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
663eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_pd)) {
664eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_pd);
66524f412ddSSagi Grimberg 		isert_err("ib_alloc_pd failed for conn %p: ret=%d\n",
666eb6ab132SSagi Grimberg 		       isert_conn, ret);
667eb6ab132SSagi Grimberg 		goto out_pd;
668eb6ab132SSagi Grimberg 	}
669b8d26b3bSNicholas Bellinger 
670eb6ab132SSagi Grimberg 	isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
671eb6ab132SSagi Grimberg 					   IB_ACCESS_LOCAL_WRITE);
672eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_mr)) {
673eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_mr);
67424f412ddSSagi Grimberg 		isert_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
675eb6ab132SSagi Grimberg 		       isert_conn, ret);
676eb6ab132SSagi Grimberg 		goto out_mr;
677eb6ab132SSagi Grimberg 	}
678b8d26b3bSNicholas Bellinger 
679570db170SSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id);
680b8d26b3bSNicholas Bellinger 	if (ret)
681b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
682b8d26b3bSNicholas Bellinger 
6832371e5daSSagi Grimberg 	ret = isert_rdma_post_recvl(isert_conn);
6842371e5daSSagi Grimberg 	if (ret)
6852371e5daSSagi Grimberg 		goto out_conn_dev;
6862371e5daSSagi Grimberg 
6872371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
6882371e5daSSagi Grimberg 	if (ret)
6892371e5daSSagi Grimberg 		goto out_conn_dev;
6902371e5daSSagi Grimberg 
691b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
6929fe63c88SSagi Grimberg 	list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
693b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
694b8d26b3bSNicholas Bellinger 
69524f412ddSSagi Grimberg 	isert_info("np %p: Allow accept_np to continue\n", np);
696531b7bf4SSagi Grimberg 	up(&isert_np->np_sem);
697b8d26b3bSNicholas Bellinger 	return 0;
698b8d26b3bSNicholas Bellinger 
699b8d26b3bSNicholas Bellinger out_conn_dev:
700eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
701eb6ab132SSagi Grimberg out_mr:
702eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
703eb6ab132SSagi Grimberg out_pd:
704b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
705b8d26b3bSNicholas Bellinger out_rsp_dma_map:
706b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
707b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
708b8d26b3bSNicholas Bellinger out_req_dma_map:
709b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
710b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
711b8d26b3bSNicholas Bellinger out_login_buf:
712b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
713b8d26b3bSNicholas Bellinger out:
714b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
7152371e5daSSagi Grimberg 	rdma_reject(cma_id, NULL, 0);
716b8d26b3bSNicholas Bellinger 	return ret;
717b8d26b3bSNicholas Bellinger }
718b8d26b3bSNicholas Bellinger 
719b8d26b3bSNicholas Bellinger static void
720b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
721b8d26b3bSNicholas Bellinger {
722b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
723*4a579da2SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
724b8d26b3bSNicholas Bellinger 
7254c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
726b8d26b3bSNicholas Bellinger 
727a3a5a826SSagi Grimberg 	if (device && device->use_fastreg)
728a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
72959464ef4SVu Pham 
73019e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
731*4a579da2SSagi Grimberg 	if (isert_conn->conn_cm_id)
73219e2090fSSagi Grimberg 		rdma_destroy_id(isert_conn->conn_cm_id);
73319e2090fSSagi Grimberg 
734b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
7354a295baeSSagi Grimberg 		struct isert_comp *comp = isert_conn->conn_qp->recv_cq->cq_context;
7364a295baeSSagi Grimberg 
73724f412ddSSagi Grimberg 		isert_dbg("dec completion context %p active_qps\n", comp);
73819e2090fSSagi Grimberg 		mutex_lock(&device_list_mutex);
7394a295baeSSagi Grimberg 		comp->active_qps--;
74019e2090fSSagi Grimberg 		mutex_unlock(&device_list_mutex);
741b8d26b3bSNicholas Bellinger 
74219e2090fSSagi Grimberg 		ib_destroy_qp(isert_conn->conn_qp);
743b8d26b3bSNicholas Bellinger 	}
744b8d26b3bSNicholas Bellinger 
745eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
746eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
747eb6ab132SSagi Grimberg 
748b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
749b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
750b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
751b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
752b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
753b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
754b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
755b8d26b3bSNicholas Bellinger 	}
756b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
757b8d26b3bSNicholas Bellinger 
758b8d26b3bSNicholas Bellinger 	if (device)
759b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
760b8d26b3bSNicholas Bellinger }
761b8d26b3bSNicholas Bellinger 
762b8d26b3bSNicholas Bellinger static void
763b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
764b8d26b3bSNicholas Bellinger {
76519e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
766c2f88b17SSagi Grimberg 
76724f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
768128e9cc8SSagi Grimberg 
7692371e5daSSagi Grimberg 	if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
77024f412ddSSagi Grimberg 		isert_warn("conn %p connect_release is running\n", isert_conn);
7712371e5daSSagi Grimberg 		return;
7722371e5daSSagi Grimberg 	}
7732371e5daSSagi Grimberg 
7742371e5daSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
7752371e5daSSagi Grimberg 	if (isert_conn->state != ISER_CONN_FULL_FEATURE)
776128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_UP;
7772371e5daSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
778b8d26b3bSNicholas Bellinger }
779b8d26b3bSNicholas Bellinger 
780b8d26b3bSNicholas Bellinger static void
781b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
782b8d26b3bSNicholas Bellinger {
783b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
784b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
785b8d26b3bSNicholas Bellinger 
7864c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
7874c22e07fSSagi Grimberg 		   current->pid);
788b8d26b3bSNicholas Bellinger 
789b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
790b8d26b3bSNicholas Bellinger }
791b8d26b3bSNicholas Bellinger 
792b8d26b3bSNicholas Bellinger static void
793b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
794b8d26b3bSNicholas Bellinger {
795b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
796b8d26b3bSNicholas Bellinger }
797b8d26b3bSNicholas Bellinger 
798954f2372SSagi Grimberg /**
799954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
800954f2372SSagi Grimberg  * @isert_conn: isert connection struct
801954f2372SSagi Grimberg  *
802954f2372SSagi Grimberg  * Notes:
803128e9cc8SSagi Grimberg  * In case the connection state is FULL_FEATURE, move state
804954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
805128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
806954f2372SSagi Grimberg  *
807954f2372SSagi Grimberg  * This routine must be called with conn_mutex held. Thus it is
808954f2372SSagi Grimberg  * safe to call multiple times.
809954f2372SSagi Grimberg  */
810954f2372SSagi Grimberg static void
811954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
812954f2372SSagi Grimberg {
813954f2372SSagi Grimberg 	int err;
814954f2372SSagi Grimberg 
815128e9cc8SSagi Grimberg 	switch (isert_conn->state) {
816128e9cc8SSagi Grimberg 	case ISER_CONN_TERMINATING:
817128e9cc8SSagi Grimberg 		break;
818128e9cc8SSagi Grimberg 	case ISER_CONN_UP:
819128e9cc8SSagi Grimberg 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
82024f412ddSSagi Grimberg 		isert_info("Terminating conn %p state %d\n",
821954f2372SSagi Grimberg 			   isert_conn, isert_conn->state);
822128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_TERMINATING;
823954f2372SSagi Grimberg 		err = rdma_disconnect(isert_conn->conn_cm_id);
824954f2372SSagi Grimberg 		if (err)
82524f412ddSSagi Grimberg 			isert_warn("Failed rdma_disconnect isert_conn %p\n",
826954f2372SSagi Grimberg 				   isert_conn);
827128e9cc8SSagi Grimberg 		break;
828128e9cc8SSagi Grimberg 	default:
82924f412ddSSagi Grimberg 		isert_warn("conn %p teminating in state %d\n",
830128e9cc8SSagi Grimberg 			   isert_conn, isert_conn->state);
831954f2372SSagi Grimberg 	}
832954f2372SSagi Grimberg }
833954f2372SSagi Grimberg 
8343b726ae2SSagi Grimberg static int
835ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
836ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
837b8d26b3bSNicholas Bellinger {
83824f412ddSSagi Grimberg 	isert_dbg("isert np %p, handling event %d\n", isert_np, event);
8393b726ae2SSagi Grimberg 
840ca6c1d82SSagi Grimberg 	switch (event) {
841ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
8423b726ae2SSagi Grimberg 		isert_np->np_cm_id = NULL;
843ca6c1d82SSagi Grimberg 		break;
844ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
845ca6c1d82SSagi Grimberg 		isert_np->np_cm_id = isert_setup_id(isert_np);
846ca6c1d82SSagi Grimberg 		if (IS_ERR(isert_np->np_cm_id)) {
84724f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
848ca6c1d82SSagi Grimberg 				  isert_np, PTR_ERR(isert_np->np_cm_id));
849ca6c1d82SSagi Grimberg 			isert_np->np_cm_id = NULL;
850ca6c1d82SSagi Grimberg 		}
851ca6c1d82SSagi Grimberg 		break;
852ca6c1d82SSagi Grimberg 	default:
85324f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
854ca6c1d82SSagi Grimberg 			  isert_np, event);
855ca6c1d82SSagi Grimberg 	}
856ca6c1d82SSagi Grimberg 
8573b726ae2SSagi Grimberg 	return -1;
8583b726ae2SSagi Grimberg }
8593b726ae2SSagi Grimberg 
860ca6c1d82SSagi Grimberg static int
861ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
862ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
863ca6c1d82SSagi Grimberg {
864ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
865ca6c1d82SSagi Grimberg 	struct isert_conn *isert_conn;
866ca6c1d82SSagi Grimberg 
867ca6c1d82SSagi Grimberg 	if (isert_np->np_cm_id == cma_id)
868ca6c1d82SSagi Grimberg 		return isert_np_cma_handler(cma_id->context, event);
869ca6c1d82SSagi Grimberg 
87019e2090fSSagi Grimberg 	isert_conn = cma_id->qp->qp_context;
871b8d26b3bSNicholas Bellinger 
872128e9cc8SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
873128e9cc8SSagi Grimberg 	isert_conn_terminate(isert_conn);
874128e9cc8SSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
875128e9cc8SSagi Grimberg 
87624f412ddSSagi Grimberg 	isert_info("conn %p completing conn_wait\n", isert_conn);
877128e9cc8SSagi Grimberg 	complete(&isert_conn->conn_wait);
8783b726ae2SSagi Grimberg 
8793b726ae2SSagi Grimberg 	return 0;
880b8d26b3bSNicholas Bellinger }
881b8d26b3bSNicholas Bellinger 
882*4a579da2SSagi Grimberg static int
883954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
884954f2372SSagi Grimberg {
88519e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
886954f2372SSagi Grimberg 
887*4a579da2SSagi Grimberg 	isert_conn->conn_cm_id = NULL;
888954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
889*4a579da2SSagi Grimberg 
890*4a579da2SSagi Grimberg 	return -1;
891954f2372SSagi Grimberg }
892954f2372SSagi Grimberg 
893b8d26b3bSNicholas Bellinger static int
894b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
895b8d26b3bSNicholas Bellinger {
896b8d26b3bSNicholas Bellinger 	int ret = 0;
897b8d26b3bSNicholas Bellinger 
8984c22e07fSSagi Grimberg 	isert_info("event %d status %d id %p np %p\n", event->event,
8994c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
900b8d26b3bSNicholas Bellinger 
901b8d26b3bSNicholas Bellinger 	switch (event->event) {
902b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
903b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
9043b726ae2SSagi Grimberg 		if (ret)
9054c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
906b8d26b3bSNicholas Bellinger 		break;
907b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
908b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
909b8d26b3bSNicholas Bellinger 		break;
91088c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
91188c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
91288c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
91388c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
914ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
915b8d26b3bSNicholas Bellinger 		break;
916954f2372SSagi Grimberg 	case RDMA_CM_EVENT_REJECTED:       /* FALLTHRU */
917954f2372SSagi Grimberg 	case RDMA_CM_EVENT_UNREACHABLE:    /* FALLTHRU */
918b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
919*4a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
920954f2372SSagi Grimberg 		break;
921b8d26b3bSNicholas Bellinger 	default:
92224f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
923b8d26b3bSNicholas Bellinger 		break;
924b8d26b3bSNicholas Bellinger 	}
925b8d26b3bSNicholas Bellinger 
926b8d26b3bSNicholas Bellinger 	return ret;
927b8d26b3bSNicholas Bellinger }
928b8d26b3bSNicholas Bellinger 
929b8d26b3bSNicholas Bellinger static int
930b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
931b8d26b3bSNicholas Bellinger {
932b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
933b8d26b3bSNicholas Bellinger 	int i, ret;
934b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
935b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
936b8d26b3bSNicholas Bellinger 
937b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
938b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
939b0a191e7SSagi Grimberg 		rx_wr->wr_id	= (uintptr_t)rx_desc;
940b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
941b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
942b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
943b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
944b8d26b3bSNicholas Bellinger 	}
945b8d26b3bSNicholas Bellinger 
946b8d26b3bSNicholas Bellinger 	rx_wr--;
947b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
948b8d26b3bSNicholas Bellinger 
949b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
950b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
951b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
952b8d26b3bSNicholas Bellinger 	if (ret) {
95324f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
954b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
955b8d26b3bSNicholas Bellinger 	} else {
95611378cdbSRasmus Villemoes 		isert_dbg("Posted %d RX buffers\n", count);
957b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
958b8d26b3bSNicholas Bellinger 	}
959b8d26b3bSNicholas Bellinger 	return ret;
960b8d26b3bSNicholas Bellinger }
961b8d26b3bSNicholas Bellinger 
962b8d26b3bSNicholas Bellinger static int
963b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
964b8d26b3bSNicholas Bellinger {
965b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
966b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
967b8d26b3bSNicholas Bellinger 	int ret;
968b8d26b3bSNicholas Bellinger 
969b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
970b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
971b8d26b3bSNicholas Bellinger 
972b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
973b0a191e7SSagi Grimberg 	send_wr.wr_id	= (uintptr_t)tx_desc;
974b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
975b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
976b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
977b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
978b8d26b3bSNicholas Bellinger 
979b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
980bdf20e72SSagi Grimberg 	if (ret)
98124f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
982b8d26b3bSNicholas Bellinger 
983b8d26b3bSNicholas Bellinger 	return ret;
984b8d26b3bSNicholas Bellinger }
985b8d26b3bSNicholas Bellinger 
986b8d26b3bSNicholas Bellinger static void
987b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
988b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
989b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
990b8d26b3bSNicholas Bellinger {
991b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
992b8d26b3bSNicholas Bellinger 
993b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
994b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
995b8d26b3bSNicholas Bellinger 
996b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
997b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
998b8d26b3bSNicholas Bellinger 
999b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
1000b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
1001b8d26b3bSNicholas Bellinger 
1002b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
1003b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
100424f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1005b8d26b3bSNicholas Bellinger 	}
1006b8d26b3bSNicholas Bellinger }
1007b8d26b3bSNicholas Bellinger 
1008b8d26b3bSNicholas Bellinger static int
1009b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
1010b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
1011b8d26b3bSNicholas Bellinger {
1012b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1013b8d26b3bSNicholas Bellinger 	u64 dma_addr;
1014b8d26b3bSNicholas Bellinger 
1015b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1016b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
1017b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
101824f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
1019b8d26b3bSNicholas Bellinger 		return -ENOMEM;
1020b8d26b3bSNicholas Bellinger 	}
1021b8d26b3bSNicholas Bellinger 
1022b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
1023b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
1024b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1025b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1026b8d26b3bSNicholas Bellinger 
10274c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
10284c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
10294c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
1030b8d26b3bSNicholas Bellinger 
1031b8d26b3bSNicholas Bellinger 	return 0;
1032b8d26b3bSNicholas Bellinger }
1033b8d26b3bSNicholas Bellinger 
1034b8d26b3bSNicholas Bellinger static void
103595b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
103668a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
1037b8d26b3bSNicholas Bellinger {
103895b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
103995b60f07SNicholas Bellinger 
1040b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1041b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
1042b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
104395b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
1044b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
104595b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
1046b8d26b3bSNicholas Bellinger }
1047b8d26b3bSNicholas Bellinger 
1048b8d26b3bSNicholas Bellinger static int
1049b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
1050b8d26b3bSNicholas Bellinger {
1051b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
1052b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
1053b8d26b3bSNicholas Bellinger 	int ret;
1054b8d26b3bSNicholas Bellinger 
1055b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
1056b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
1057b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
1058b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
1059b8d26b3bSNicholas Bellinger 
106024f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
1061b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
1062b8d26b3bSNicholas Bellinger 
1063b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1064b0a191e7SSagi Grimberg 	rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
1065b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1066b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1067b8d26b3bSNicholas Bellinger 
1068b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1069b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1070b8d26b3bSNicholas Bellinger 	if (ret) {
107124f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
1072b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1073b8d26b3bSNicholas Bellinger 	}
1074b8d26b3bSNicholas Bellinger 
1075b8d26b3bSNicholas Bellinger 	return ret;
1076b8d26b3bSNicholas Bellinger }
1077b8d26b3bSNicholas Bellinger 
1078b8d26b3bSNicholas Bellinger static int
1079b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1080b8d26b3bSNicholas Bellinger 		   u32 length)
1081b8d26b3bSNicholas Bellinger {
1082b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1083b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1084b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1085b8d26b3bSNicholas Bellinger 	int ret;
1086b8d26b3bSNicholas Bellinger 
1087b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1088b8d26b3bSNicholas Bellinger 
1089b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1090b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1091b8d26b3bSNicholas Bellinger 
1092b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1093b8d26b3bSNicholas Bellinger 
1094b8d26b3bSNicholas Bellinger 	if (length > 0) {
1095b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1096b8d26b3bSNicholas Bellinger 
1097b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1098b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1099b8d26b3bSNicholas Bellinger 
1100b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1101b8d26b3bSNicholas Bellinger 
1102b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1103b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1104b8d26b3bSNicholas Bellinger 
1105b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1106b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
1107b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1108b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1109b8d26b3bSNicholas Bellinger 	}
1110b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1111b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1112e0546fc1SSagi Grimberg 			if (!conn->sess->sess_ops->SessionType &&
1113e0546fc1SSagi Grimberg 			    isert_conn->conn_device->use_fastreg) {
1114570db170SSagi Grimberg 				ret = isert_conn_create_fastreg_pool(isert_conn);
1115f46d6a8aSNicholas Bellinger 				if (ret) {
111624f412ddSSagi Grimberg 					isert_err("Conn: %p failed to create"
1117f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1118f46d6a8aSNicholas Bellinger 					return ret;
1119f46d6a8aSNicholas Bellinger 				}
1120f46d6a8aSNicholas Bellinger 			}
1121f46d6a8aSNicholas Bellinger 
1122b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1123b8d26b3bSNicholas Bellinger 			if (ret)
1124b8d26b3bSNicholas Bellinger 				return ret;
1125b8d26b3bSNicholas Bellinger 
1126b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1127b8d26b3bSNicholas Bellinger 			if (ret)
1128b8d26b3bSNicholas Bellinger 				return ret;
1129b8d26b3bSNicholas Bellinger 
1130128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
11312371e5daSSagi Grimberg 			mutex_lock(&isert_conn->conn_mutex);
1132128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
11332371e5daSSagi Grimberg 			mutex_unlock(&isert_conn->conn_mutex);
1134b8d26b3bSNicholas Bellinger 			goto post_send;
1135b8d26b3bSNicholas Bellinger 		}
1136b8d26b3bSNicholas Bellinger 
1137b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1138b8d26b3bSNicholas Bellinger 		if (ret)
1139b8d26b3bSNicholas Bellinger 			return ret;
1140b8d26b3bSNicholas Bellinger 	}
1141b8d26b3bSNicholas Bellinger post_send:
1142b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1143b8d26b3bSNicholas Bellinger 	if (ret)
1144b8d26b3bSNicholas Bellinger 		return ret;
1145b8d26b3bSNicholas Bellinger 
1146b8d26b3bSNicholas Bellinger 	return 0;
1147b8d26b3bSNicholas Bellinger }
1148b8d26b3bSNicholas Bellinger 
1149b8d26b3bSNicholas Bellinger static void
11502371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1151b8d26b3bSNicholas Bellinger {
11522371e5daSSagi Grimberg 	struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
11532371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1154b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1155b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1156b8d26b3bSNicholas Bellinger 	int size;
1157b8d26b3bSNicholas Bellinger 
115824f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
11592371e5daSSagi Grimberg 
11602371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1161b8d26b3bSNicholas Bellinger 
1162b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1163b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1164b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1165b8d26b3bSNicholas Bellinger 		/*
1166b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1167b8d26b3bSNicholas Bellinger 		 * login request PDU.
1168b8d26b3bSNicholas Bellinger 		 */
1169b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1170b8d26b3bSNicholas Bellinger 		login->current_stage =
1171b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1172b8d26b3bSNicholas Bellinger 			 >> 2;
1173b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1174b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1175b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1176b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1177b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1178b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1179b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1180b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1181b8d26b3bSNicholas Bellinger 	}
1182b8d26b3bSNicholas Bellinger 
1183b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1184b8d26b3bSNicholas Bellinger 
1185b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
11864c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
11874c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
11884c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
1189b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1190b8d26b3bSNicholas Bellinger 
11916faaa85fSNicholas Bellinger 	if (login->first_request) {
1192b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
11936faaa85fSNicholas Bellinger 		return;
11946faaa85fSNicholas Bellinger 	}
11956faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1196b8d26b3bSNicholas Bellinger }
1197b8d26b3bSNicholas Bellinger 
1198b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1199676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1200b8d26b3bSNicholas Bellinger {
1201b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1202b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1203d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1204b8d26b3bSNicholas Bellinger 
1205676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1206d703ce2fSNicholas Bellinger 	if (!cmd) {
120724f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1208b8d26b3bSNicholas Bellinger 		return NULL;
1209b8d26b3bSNicholas Bellinger 	}
1210d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1211b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1212d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1213b8d26b3bSNicholas Bellinger 
1214d703ce2fSNicholas Bellinger 	return cmd;
1215b8d26b3bSNicholas Bellinger }
1216b8d26b3bSNicholas Bellinger 
1217b8d26b3bSNicholas Bellinger static int
1218b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1219d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1220d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1221b8d26b3bSNicholas Bellinger {
1222b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1223b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1224b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1225b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1226b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1227b8d26b3bSNicholas Bellinger 
1228b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1229b8d26b3bSNicholas Bellinger 	if (rc < 0)
1230b8d26b3bSNicholas Bellinger 		return rc;
1231b8d26b3bSNicholas Bellinger 
1232b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1233b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1234b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1235b8d26b3bSNicholas Bellinger 
1236b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1237b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1238b8d26b3bSNicholas Bellinger 		return 0;
1239b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1240b8d26b3bSNicholas Bellinger 		dump_payload = true;
1241b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1242b8d26b3bSNicholas Bellinger 	}
1243b8d26b3bSNicholas Bellinger 
1244b8d26b3bSNicholas Bellinger 	if (!imm_data)
1245b8d26b3bSNicholas Bellinger 		return 0;
1246b8d26b3bSNicholas Bellinger 
1247b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1248b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1249b8d26b3bSNicholas Bellinger 
125024f412ddSSagi Grimberg 	isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1251b8d26b3bSNicholas Bellinger 		  sg, sg_nents, &rx_desc->data[0], imm_data_len);
1252b8d26b3bSNicholas Bellinger 
1253b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1254b8d26b3bSNicholas Bellinger 
1255b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1256b8d26b3bSNicholas Bellinger 
1257b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1258b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1259b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1260b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1261b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1262b8d26b3bSNicholas Bellinger 	}
1263b8d26b3bSNicholas Bellinger 
1264b8d26b3bSNicholas Bellinger sequence_cmd:
1265561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1266b8d26b3bSNicholas Bellinger 
1267b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1268b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
12696cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
12706cc44a6fSNicholas Bellinger 		target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1271b8d26b3bSNicholas Bellinger 
1272b8d26b3bSNicholas Bellinger 	return 0;
1273b8d26b3bSNicholas Bellinger }
1274b8d26b3bSNicholas Bellinger 
1275b8d26b3bSNicholas Bellinger static int
1276b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1277b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1278b8d26b3bSNicholas Bellinger {
1279b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1280b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1281b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1282b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1283b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1284b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1285b8d26b3bSNicholas Bellinger 
1286b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1287b8d26b3bSNicholas Bellinger 	if (rc < 0)
1288b8d26b3bSNicholas Bellinger 		return rc;
1289b8d26b3bSNicholas Bellinger 	else if (!cmd)
1290b8d26b3bSNicholas Bellinger 		return 0;
1291b8d26b3bSNicholas Bellinger 	/*
1292b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1293b8d26b3bSNicholas Bellinger 	 */
1294b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
129524f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1296b8d26b3bSNicholas Bellinger 		dump_stack();
1297b8d26b3bSNicholas Bellinger 		return -1;
1298b8d26b3bSNicholas Bellinger 	}
1299b8d26b3bSNicholas Bellinger 
13004c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
13014c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
13024c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
13034c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1304b8d26b3bSNicholas Bellinger 
1305b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1306b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1307b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1308b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1309b8d26b3bSNicholas Bellinger 	/*
1310b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1311b8d26b3bSNicholas Bellinger 	 */
1312b8d26b3bSNicholas Bellinger 	if (page_off) {
13134c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1314b8d26b3bSNicholas Bellinger 		dump_stack();
1315b8d26b3bSNicholas Bellinger 		return -1;
1316b8d26b3bSNicholas Bellinger 	}
13174c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
13184c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
13194c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1320b8d26b3bSNicholas Bellinger 
1321b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1322b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1323b8d26b3bSNicholas Bellinger 
1324b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1325b8d26b3bSNicholas Bellinger 	if (rc < 0)
1326b8d26b3bSNicholas Bellinger 		return rc;
1327b8d26b3bSNicholas Bellinger 
1328b8d26b3bSNicholas Bellinger 	return 0;
1329b8d26b3bSNicholas Bellinger }
1330b8d26b3bSNicholas Bellinger 
1331b8d26b3bSNicholas Bellinger static int
1332778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1333d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1334d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1335778de368SNicholas Bellinger {
1336778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1337778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1338778de368SNicholas Bellinger 	int rc;
1339778de368SNicholas Bellinger 
1340778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1341778de368SNicholas Bellinger 	if (rc < 0)
1342778de368SNicholas Bellinger 		return rc;
1343778de368SNicholas Bellinger 	/*
1344778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1345778de368SNicholas Bellinger 	 */
1346778de368SNicholas Bellinger 
1347778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1348778de368SNicholas Bellinger }
1349778de368SNicholas Bellinger 
1350778de368SNicholas Bellinger static int
1351adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1352d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1353d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1354adb54c29SNicholas Bellinger {
1355adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1356adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1357adb54c29SNicholas Bellinger 	int rc;
1358b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1359adb54c29SNicholas Bellinger 
1360adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1361adb54c29SNicholas Bellinger 	if (rc < 0)
1362adb54c29SNicholas Bellinger 		return rc;
1363adb54c29SNicholas Bellinger 
1364b44a2b67SSagi Grimberg 	if (payload_length) {
1365adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
1366adb54c29SNicholas Bellinger 		if (!text_in) {
136724f412ddSSagi Grimberg 			isert_err("Unable to allocate text_in of payload_length: %u\n",
1368adb54c29SNicholas Bellinger 				  payload_length);
1369adb54c29SNicholas Bellinger 			return -ENOMEM;
1370adb54c29SNicholas Bellinger 		}
1371b44a2b67SSagi Grimberg 	}
1372adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1373adb54c29SNicholas Bellinger 
1374adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1375adb54c29SNicholas Bellinger 
1376adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1377adb54c29SNicholas Bellinger }
1378adb54c29SNicholas Bellinger 
1379adb54c29SNicholas Bellinger static int
1380b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1381b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1382b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1383b8d26b3bSNicholas Bellinger {
1384b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1385b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1386ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1387b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1388b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1389b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1390b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1391b8d26b3bSNicholas Bellinger 
1392ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1393ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
139424f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1395ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1396ca40d24eSNicholas Bellinger 		return 0;
1397ca40d24eSNicholas Bellinger 	}
1398ca40d24eSNicholas Bellinger 
1399b8d26b3bSNicholas Bellinger 	switch (opcode) {
1400b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1401676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1402b8d26b3bSNicholas Bellinger 		if (!cmd)
1403b8d26b3bSNicholas Bellinger 			break;
1404b8d26b3bSNicholas Bellinger 
1405d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1406b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1407b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1408b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1409b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1410b8d26b3bSNicholas Bellinger 
1411d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1412b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1413b8d26b3bSNicholas Bellinger 		break;
1414b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1415676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1416b8d26b3bSNicholas Bellinger 		if (!cmd)
1417b8d26b3bSNicholas Bellinger 			break;
1418b8d26b3bSNicholas Bellinger 
1419d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1420d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1421778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1422b8d26b3bSNicholas Bellinger 		break;
1423b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1424b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1425b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1426b8d26b3bSNicholas Bellinger 		break;
1427b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1428676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1429b8d26b3bSNicholas Bellinger 		if (!cmd)
1430b8d26b3bSNicholas Bellinger 			break;
1431b8d26b3bSNicholas Bellinger 
1432b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1433b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1434b8d26b3bSNicholas Bellinger 		break;
1435b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1436676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1437b8d26b3bSNicholas Bellinger 		if (!cmd)
1438b8d26b3bSNicholas Bellinger 			break;
1439b8d26b3bSNicholas Bellinger 
1440b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1441b8d26b3bSNicholas Bellinger 		break;
1442adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1443e4f4e801SSagi Grimberg 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) {
1444e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
1445e4f4e801SSagi Grimberg 			if (!cmd)
1446e4f4e801SSagi Grimberg 				break;
1447e4f4e801SSagi Grimberg 		} else {
1448676687c6SNicholas Bellinger 			cmd = isert_allocate_cmd(conn);
1449adb54c29SNicholas Bellinger 			if (!cmd)
1450adb54c29SNicholas Bellinger 				break;
1451e4f4e801SSagi Grimberg 		}
1452adb54c29SNicholas Bellinger 
1453d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1454d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1455adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1456adb54c29SNicholas Bellinger 		break;
1457b8d26b3bSNicholas Bellinger 	default:
145824f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1459b8d26b3bSNicholas Bellinger 		dump_stack();
1460b8d26b3bSNicholas Bellinger 		break;
1461b8d26b3bSNicholas Bellinger 	}
1462b8d26b3bSNicholas Bellinger 
1463b8d26b3bSNicholas Bellinger 	return ret;
1464b8d26b3bSNicholas Bellinger }
1465b8d26b3bSNicholas Bellinger 
1466b8d26b3bSNicholas Bellinger static void
1467b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1468b8d26b3bSNicholas Bellinger {
1469b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1470b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1471b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1472b8d26b3bSNicholas Bellinger 	int rc;
1473b8d26b3bSNicholas Bellinger 
1474b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1475b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1476b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1477b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1478b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
14794c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1480b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1481b8d26b3bSNicholas Bellinger 		}
1482b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1483b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1484b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
14854c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1486b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1487b8d26b3bSNicholas Bellinger 		}
1488b8d26b3bSNicholas Bellinger 
148924f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1490b8d26b3bSNicholas Bellinger 		break;
1491b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
149224f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1493b8d26b3bSNicholas Bellinger 		break;
1494b8d26b3bSNicholas Bellinger 	default:
149524f412ddSSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1496b8d26b3bSNicholas Bellinger 		break;
1497b8d26b3bSNicholas Bellinger 	}
1498b8d26b3bSNicholas Bellinger 
1499b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1500b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1501b8d26b3bSNicholas Bellinger }
1502b8d26b3bSNicholas Bellinger 
1503b8d26b3bSNicholas Bellinger static void
1504b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
15054a295baeSSagi Grimberg 		    u32 xfer_len)
1506b8d26b3bSNicholas Bellinger {
1507b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1508b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1509b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1510b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1511b8d26b3bSNicholas Bellinger 
1512b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1513b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1514b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
15154c22e07fSSagi Grimberg 		isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1516b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1517b8d26b3bSNicholas Bellinger 	} else {
1518b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1519b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
15204c22e07fSSagi Grimberg 		isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1521b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1522b8d26b3bSNicholas Bellinger 	}
1523b8d26b3bSNicholas Bellinger 
1524b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1525b8d26b3bSNicholas Bellinger 
1526b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
152724f412ddSSagi Grimberg 	isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1528b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1529b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1530b8d26b3bSNicholas Bellinger 
15312371e5daSSagi Grimberg 	if ((char *)desc == isert_conn->login_req_buf) {
15322371e5daSSagi Grimberg 		isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
15332371e5daSSagi Grimberg 		if (isert_conn->conn) {
15342371e5daSSagi Grimberg 			struct iscsi_login *login = isert_conn->conn->conn_login;
15352371e5daSSagi Grimberg 
15362371e5daSSagi Grimberg 			if (login && !login->first_request)
15372371e5daSSagi Grimberg 				isert_rx_login_req(isert_conn);
15382371e5daSSagi Grimberg 		}
15392371e5daSSagi Grimberg 		mutex_lock(&isert_conn->conn_mutex);
15402371e5daSSagi Grimberg 		complete(&isert_conn->login_req_comp);
15412371e5daSSagi Grimberg 		mutex_unlock(&isert_conn->conn_mutex);
15422371e5daSSagi Grimberg 	} else {
1543b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
15442371e5daSSagi Grimberg 	}
1545b8d26b3bSNicholas Bellinger 
1546b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1547b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1548b8d26b3bSNicholas Bellinger 
1549b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
15504c22e07fSSagi Grimberg 	isert_dbg("Decremented post_recv_buf_count: %d\n",
1551b8d26b3bSNicholas Bellinger 		  isert_conn->post_recv_buf_count);
1552b8d26b3bSNicholas Bellinger 
1553b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1554b8d26b3bSNicholas Bellinger 		return;
1555b8d26b3bSNicholas Bellinger 
1556b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1557b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1558b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1559b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1560b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1561b8d26b3bSNicholas Bellinger 		if (err) {
156224f412ddSSagi Grimberg 			isert_err("isert_post_recv() count: %d failed, %d\n",
1563b8d26b3bSNicholas Bellinger 			       count, err);
1564b8d26b3bSNicholas Bellinger 		}
1565b8d26b3bSNicholas Bellinger 	}
1566b8d26b3bSNicholas Bellinger }
1567b8d26b3bSNicholas Bellinger 
1568e3d7e4c3SSagi Grimberg static int
1569e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1570e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1571e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1572e3d7e4c3SSagi Grimberg {
1573e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1574e3d7e4c3SSagi Grimberg 
1575e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1576e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1577e3d7e4c3SSagi Grimberg 
1578e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1579e3d7e4c3SSagi Grimberg 	data->offset = offset;
1580e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1581e3d7e4c3SSagi Grimberg 
1582e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1583e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1584e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1585e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1586e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1587e3d7e4c3SSagi Grimberg 
1588e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1589e3d7e4c3SSagi Grimberg 					data->dma_dir);
1590e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
159124f412ddSSagi Grimberg 		isert_err("Cmd: unable to dma map SGs %p\n", sg);
1592e3d7e4c3SSagi Grimberg 		return -EINVAL;
1593e3d7e4c3SSagi Grimberg 	}
1594e3d7e4c3SSagi Grimberg 
159524f412ddSSagi Grimberg 	isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1596e3d7e4c3SSagi Grimberg 		  isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1597e3d7e4c3SSagi Grimberg 
1598e3d7e4c3SSagi Grimberg 	return 0;
1599e3d7e4c3SSagi Grimberg }
1600e3d7e4c3SSagi Grimberg 
1601e3d7e4c3SSagi Grimberg static void
1602e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1603e3d7e4c3SSagi Grimberg {
1604e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1605e3d7e4c3SSagi Grimberg 
1606e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1607e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1608e3d7e4c3SSagi Grimberg }
1609e3d7e4c3SSagi Grimberg 
1610e3d7e4c3SSagi Grimberg 
1611e3d7e4c3SSagi Grimberg 
1612b8d26b3bSNicholas Bellinger static void
1613b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1614b8d26b3bSNicholas Bellinger {
1615b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1616b8d26b3bSNicholas Bellinger 
16174c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1618e3d7e4c3SSagi Grimberg 
1619e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
16204c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1621e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1622b8d26b3bSNicholas Bellinger 	}
1623b8d26b3bSNicholas Bellinger 
162490ecc6e2SVu Pham 	if (wr->send_wr) {
16254c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free send_wr\n", isert_cmd);
1626b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1627b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
162890ecc6e2SVu Pham 	}
1629b8d26b3bSNicholas Bellinger 
163090ecc6e2SVu Pham 	if (wr->ib_sge) {
16314c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
163290ecc6e2SVu Pham 		kfree(wr->ib_sge);
163390ecc6e2SVu Pham 		wr->ib_sge = NULL;
163490ecc6e2SVu Pham 	}
1635b8d26b3bSNicholas Bellinger }
1636b8d26b3bSNicholas Bellinger 
1637b8d26b3bSNicholas Bellinger static void
1638a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
163959464ef4SVu Pham {
164059464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
164159464ef4SVu Pham 	LIST_HEAD(unmap_list);
164259464ef4SVu Pham 
16434c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
164459464ef4SVu Pham 
164559464ef4SVu Pham 	if (wr->fr_desc) {
16464c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
16479e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
16489e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
16499e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
16509e961ae7SSagi Grimberg 		}
165159464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1652a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
165359464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
165459464ef4SVu Pham 		wr->fr_desc = NULL;
165559464ef4SVu Pham 	}
165659464ef4SVu Pham 
1657e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
16584c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1659e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
166059464ef4SVu Pham 	}
166159464ef4SVu Pham 
166259464ef4SVu Pham 	wr->ib_sge = NULL;
166359464ef4SVu Pham 	wr->send_wr = NULL;
166459464ef4SVu Pham }
166559464ef4SVu Pham 
166659464ef4SVu Pham static void
166703e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1668b8d26b3bSNicholas Bellinger {
1669d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1670b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1671186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1672d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1673e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1674b8d26b3bSNicholas Bellinger 
16754c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1676b8d26b3bSNicholas Bellinger 
1677b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1678b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1679b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1680b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16815159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1682b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1683b8d26b3bSNicholas Bellinger 
168403e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1685b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
168603e7848aSNicholas Bellinger 			/*
168703e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
168803e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
168903e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
169003e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
169103e7848aSNicholas Bellinger 			 */
169203e7848aSNicholas Bellinger 			if (comp_err &&
169303e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
169403e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
169503e7848aSNicholas Bellinger 
169603e7848aSNicholas Bellinger 				target_put_sess_cmd(se_cmd->se_sess, se_cmd);
169703e7848aSNicholas Bellinger 			}
169803e7848aSNicholas Bellinger 		}
1699b8d26b3bSNicholas Bellinger 
1700d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1701186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1702186a9647SNicholas Bellinger 		break;
1703b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1704186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1705186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17065159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1707186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1708186a9647SNicholas Bellinger 
1709b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1710b8d26b3bSNicholas Bellinger 		break;
1711b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1712b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1713adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1714e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1715e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1716e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1717e4f4e801SSagi Grimberg 			break;
1718e4f4e801SSagi Grimberg 
1719b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1720b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17215159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1722b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1723b8d26b3bSNicholas Bellinger 
1724b8d26b3bSNicholas Bellinger 		/*
1725b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1726b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1727b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1728b8d26b3bSNicholas Bellinger 		 */
1729b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
173011378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
17313df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1732b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1733b8d26b3bSNicholas Bellinger 			break;
1734b8d26b3bSNicholas Bellinger 		}
1735b8d26b3bSNicholas Bellinger 		/*
1736b8d26b3bSNicholas Bellinger 		 * Fall-through
1737b8d26b3bSNicholas Bellinger 		 */
1738b8d26b3bSNicholas Bellinger 	default:
1739d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1740b8d26b3bSNicholas Bellinger 		break;
1741b8d26b3bSNicholas Bellinger 	}
1742b8d26b3bSNicholas Bellinger }
1743b8d26b3bSNicholas Bellinger 
1744b8d26b3bSNicholas Bellinger static void
1745b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1746b8d26b3bSNicholas Bellinger {
1747b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
17484c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1749b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1750b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1751b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1752b8d26b3bSNicholas Bellinger 	}
1753b8d26b3bSNicholas Bellinger }
1754b8d26b3bSNicholas Bellinger 
1755b8d26b3bSNicholas Bellinger static void
1756b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
175703e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1758b8d26b3bSNicholas Bellinger {
1759dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
17604c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1761dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1762dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1763dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1764b8d26b3bSNicholas Bellinger 	}
1765b8d26b3bSNicholas Bellinger 
1766b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
176703e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1768b8d26b3bSNicholas Bellinger }
1769b8d26b3bSNicholas Bellinger 
177096b7973eSSagi Grimberg static int
177196b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1772f93f3a70SSagi Grimberg {
17739e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
177496b7973eSSagi Grimberg 	int ret;
1775f93f3a70SSagi Grimberg 
177696b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
17779e961ae7SSagi Grimberg 	if (ret) {
177824f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
17799e961ae7SSagi Grimberg 		goto fail_mr_status;
17809e961ae7SSagi Grimberg 	}
178196b7973eSSagi Grimberg 
17829e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
178396b7973eSSagi Grimberg 		u64 sec_offset_err;
17849e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
17859e961ae7SSagi Grimberg 
17869e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
17879e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
17889e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
17899e961ae7SSagi Grimberg 			break;
17909e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
17919e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
17929e961ae7SSagi Grimberg 			break;
17939e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
17949e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
17959e961ae7SSagi Grimberg 			break;
17969e961ae7SSagi Grimberg 		}
179796b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
179896b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
179996b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
18009e961ae7SSagi Grimberg 
18014c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
18029e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
18039e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
18049e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
18059e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
18069e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
18079e961ae7SSagi Grimberg 		ret = 1;
18089e961ae7SSagi Grimberg 	}
18099e961ae7SSagi Grimberg 
18109e961ae7SSagi Grimberg fail_mr_status:
181196b7973eSSagi Grimberg 	return ret;
181296b7973eSSagi Grimberg }
181396b7973eSSagi Grimberg 
181496b7973eSSagi Grimberg static void
181596b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
181696b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
181796b7973eSSagi Grimberg {
181896b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
181996b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
182096b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
182196b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
182296b7973eSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
182396b7973eSSagi Grimberg 	int ret = 0;
182496b7973eSSagi Grimberg 
182596b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
182696b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
182796b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
182896b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
182996b7973eSSagi Grimberg 	}
183096b7973eSSagi Grimberg 
1831f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1832897bb2c9SSagi Grimberg 	wr->send_wr_num = 0;
18339e961ae7SSagi Grimberg 	if (ret)
18349e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18359e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
18369e961ae7SSagi Grimberg 	else
1837f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1838f93f3a70SSagi Grimberg }
1839f93f3a70SSagi Grimberg 
1840f93f3a70SSagi Grimberg static void
1841b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1842b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1843b8d26b3bSNicholas Bellinger {
1844b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1845d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1846b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
184790ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1848d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
18495bac4b1aSSagi Grimberg 	int ret = 0;
1850b8d26b3bSNicholas Bellinger 
18519e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
185296b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
185396b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
185496b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
18559e961ae7SSagi Grimberg 	}
18569e961ae7SSagi Grimberg 
1857b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1858d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1859e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1860b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1861b8d26b3bSNicholas Bellinger 
186224f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1863b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1864b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1865b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1866b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1867b8d26b3bSNicholas Bellinger 
1868364189f0SSagi Grimberg 	if (ret) {
1869364189f0SSagi Grimberg 		target_put_sess_cmd(se_cmd->se_sess, se_cmd);
18705bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18715bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
1872364189f0SSagi Grimberg 	} else {
1873b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1874b8d26b3bSNicholas Bellinger 	}
1875364189f0SSagi Grimberg }
1876b8d26b3bSNicholas Bellinger 
1877b8d26b3bSNicholas Bellinger static void
1878b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1879b8d26b3bSNicholas Bellinger {
1880b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1881b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1882b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1883b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1884d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1885b8d26b3bSNicholas Bellinger 
18864c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
18874c22e07fSSagi Grimberg 
1888b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1889b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1890b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
189110633c37SSagi Grimberg 	case ISTATE_SEND_REJECT:   /* FALLTHRU */
189210633c37SSagi Grimberg 	case ISTATE_SEND_TEXTRSP:  /* FALLTHRU */
1893b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
18944c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
18954c22e07fSSagi Grimberg 				     ib_dev, false);
18963df8f68aSNicholas Bellinger 		break;
1897b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1898b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1899b8d26b3bSNicholas Bellinger 		break;
1900b8d26b3bSNicholas Bellinger 	default:
19014c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1902b8d26b3bSNicholas Bellinger 		dump_stack();
1903b8d26b3bSNicholas Bellinger 		break;
1904b8d26b3bSNicholas Bellinger 	}
1905b8d26b3bSNicholas Bellinger }
1906b8d26b3bSNicholas Bellinger 
1907b8d26b3bSNicholas Bellinger static void
1908b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1909b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1910b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1911b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1912b8d26b3bSNicholas Bellinger {
1913d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1914b8d26b3bSNicholas Bellinger 
1915b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
19163df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1917adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1918adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1919b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1920b8d26b3bSNicholas Bellinger 
1921b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1922b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1923b8d26b3bSNicholas Bellinger 		return;
1924b8d26b3bSNicholas Bellinger 	}
1925897bb2c9SSagi Grimberg 
1926b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
192703e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1928b8d26b3bSNicholas Bellinger }
1929b8d26b3bSNicholas Bellinger 
1930b8d26b3bSNicholas Bellinger static void
193168a86deeSSagi Grimberg isert_send_completion(struct iser_tx_desc *tx_desc,
1932b8d26b3bSNicholas Bellinger 		      struct isert_conn *isert_conn)
1933b8d26b3bSNicholas Bellinger {
1934b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1935b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1936b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1937b8d26b3bSNicholas Bellinger 
1938b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1939b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1940b8d26b3bSNicholas Bellinger 		return;
1941b8d26b3bSNicholas Bellinger 	}
1942b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1943b8d26b3bSNicholas Bellinger 
19444c22e07fSSagi Grimberg 	isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
19454c22e07fSSagi Grimberg 
1946b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1947b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
19484c22e07fSSagi Grimberg 		isert_err("Got ISER_IB_RECV\n");
1949b8d26b3bSNicholas Bellinger 		dump_stack();
1950b8d26b3bSNicholas Bellinger 		break;
1951b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1952b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1953b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1954b8d26b3bSNicholas Bellinger 		break;
1955b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1956f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
1957b8d26b3bSNicholas Bellinger 		break;
1958b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1959b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1960b8d26b3bSNicholas Bellinger 		break;
1961b8d26b3bSNicholas Bellinger 	default:
19624c22e07fSSagi Grimberg 		isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
1963b8d26b3bSNicholas Bellinger 		dump_stack();
1964b8d26b3bSNicholas Bellinger 		break;
1965b8d26b3bSNicholas Bellinger 	}
1966b8d26b3bSNicholas Bellinger }
1967b8d26b3bSNicholas Bellinger 
19686f0fae3dSSagi Grimberg /**
19696f0fae3dSSagi Grimberg  * is_isert_tx_desc() - Indicate if the completion wr_id
19706f0fae3dSSagi Grimberg  *     is a TX descriptor or not.
19716f0fae3dSSagi Grimberg  * @isert_conn: iser connection
19726f0fae3dSSagi Grimberg  * @wr_id: completion WR identifier
19736f0fae3dSSagi Grimberg  *
19746f0fae3dSSagi Grimberg  * Since we cannot rely on wc opcode in FLUSH errors
19756f0fae3dSSagi Grimberg  * we must work around it by checking if the wr_id address
19766f0fae3dSSagi Grimberg  * falls in the iser connection rx_descs buffer. If so
19776f0fae3dSSagi Grimberg  * it is an RX descriptor, otherwize it is a TX.
19786f0fae3dSSagi Grimberg  */
19796f0fae3dSSagi Grimberg static inline bool
19806f0fae3dSSagi Grimberg is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
1981b8d26b3bSNicholas Bellinger {
19826f0fae3dSSagi Grimberg 	void *start = isert_conn->conn_rx_descs;
19836f0fae3dSSagi Grimberg 	int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->conn_rx_descs);
19846f0fae3dSSagi Grimberg 
19856f0fae3dSSagi Grimberg 	if (wr_id >= start && wr_id < start + len)
19866f0fae3dSSagi Grimberg 		return false;
19876f0fae3dSSagi Grimberg 
19886f0fae3dSSagi Grimberg 	return true;
19896f0fae3dSSagi Grimberg }
19906f0fae3dSSagi Grimberg 
19916f0fae3dSSagi Grimberg static void
19926f0fae3dSSagi Grimberg isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
19936f0fae3dSSagi Grimberg {
1994bdf20e72SSagi Grimberg 	if (wc->wr_id == ISER_BEACON_WRID) {
199524f412ddSSagi Grimberg 		isert_info("conn %p completing conn_wait_comp_err\n",
1996bdf20e72SSagi Grimberg 			   isert_conn);
1997bdf20e72SSagi Grimberg 		complete(&isert_conn->conn_wait_comp_err);
1998ed4520aeSNicholas Bellinger 	} else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
1999b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2000df43debdSSagi Grimberg 		struct isert_cmd *isert_cmd;
20016f0fae3dSSagi Grimberg 		struct iser_tx_desc *desc;
2002b8d26b3bSNicholas Bellinger 
20036f0fae3dSSagi Grimberg 		desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20046f0fae3dSSagi Grimberg 		isert_cmd = desc->isert_cmd;
2005b8d26b3bSNicholas Bellinger 		if (!isert_cmd)
2006df43debdSSagi Grimberg 			isert_unmap_tx_desc(desc, ib_dev);
2007b8d26b3bSNicholas Bellinger 		else
2008df43debdSSagi Grimberg 			isert_completion_put(desc, isert_cmd, ib_dev, true);
2009df43debdSSagi Grimberg 	} else {
2010df43debdSSagi Grimberg 		isert_conn->post_recv_buf_count--;
2011bdf20e72SSagi Grimberg 		if (!isert_conn->post_recv_buf_count)
20129d49f5e2SSagi Grimberg 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2013b8d26b3bSNicholas Bellinger 	}
2014df43debdSSagi Grimberg }
2015b8d26b3bSNicholas Bellinger 
2016b8d26b3bSNicholas Bellinger static void
20176f0fae3dSSagi Grimberg isert_handle_wc(struct ib_wc *wc)
2018b8d26b3bSNicholas Bellinger {
2019b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2020b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
20216f0fae3dSSagi Grimberg 	struct iser_rx_desc *rx_desc;
2022b8d26b3bSNicholas Bellinger 
20236f0fae3dSSagi Grimberg 	isert_conn = wc->qp->qp_context;
20246f0fae3dSSagi Grimberg 	if (likely(wc->status == IB_WC_SUCCESS)) {
20256f0fae3dSSagi Grimberg 		if (wc->opcode == IB_WC_RECV) {
20266f0fae3dSSagi Grimberg 			rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
20276f0fae3dSSagi Grimberg 			isert_rx_completion(rx_desc, isert_conn, wc->byte_len);
2028b8d26b3bSNicholas Bellinger 		} else {
20296f0fae3dSSagi Grimberg 			tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20306f0fae3dSSagi Grimberg 			isert_send_completion(tx_desc, isert_conn);
2031b8d26b3bSNicholas Bellinger 		}
20326f0fae3dSSagi Grimberg 	} else {
20336f0fae3dSSagi Grimberg 		if (wc->status != IB_WC_WR_FLUSH_ERR)
203424f412ddSSagi Grimberg 			isert_err("wr id %llx status %d vend_err %x\n",
20356f0fae3dSSagi Grimberg 				  wc->wr_id, wc->status, wc->vendor_err);
20366f0fae3dSSagi Grimberg 		else
203724f412ddSSagi Grimberg 			isert_dbg("flush error: wr id %llx\n", wc->wr_id);
2038b8d26b3bSNicholas Bellinger 
20396f0fae3dSSagi Grimberg 		if (wc->wr_id != ISER_FASTREG_LI_WRID)
20406f0fae3dSSagi Grimberg 			isert_cq_comp_err(isert_conn, wc);
20416f0fae3dSSagi Grimberg 	}
2042b8d26b3bSNicholas Bellinger }
2043b8d26b3bSNicholas Bellinger 
2044b8d26b3bSNicholas Bellinger static void
20456f0fae3dSSagi Grimberg isert_cq_work(struct work_struct *work)
2046b8d26b3bSNicholas Bellinger {
204737d9fe80SSagi Grimberg 	enum { isert_poll_budget = 65536 };
20484a295baeSSagi Grimberg 	struct isert_comp *comp = container_of(work, struct isert_comp,
20496f0fae3dSSagi Grimberg 					       work);
205036ea63b5SSagi Grimberg 	struct ib_wc *const wcs = comp->wcs;
205136ea63b5SSagi Grimberg 	int i, n, completed = 0;
2052b8d26b3bSNicholas Bellinger 
205336ea63b5SSagi Grimberg 	while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
205436ea63b5SSagi Grimberg 		for (i = 0; i < n; i++)
205536ea63b5SSagi Grimberg 			isert_handle_wc(&wcs[i]);
2056b8d26b3bSNicholas Bellinger 
205736ea63b5SSagi Grimberg 		completed += n;
205836ea63b5SSagi Grimberg 		if (completed >= isert_poll_budget)
205937d9fe80SSagi Grimberg 			break;
206037d9fe80SSagi Grimberg 	}
206137d9fe80SSagi Grimberg 
20626f0fae3dSSagi Grimberg 	ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
2063b8d26b3bSNicholas Bellinger }
2064b8d26b3bSNicholas Bellinger 
2065b8d26b3bSNicholas Bellinger static void
20666f0fae3dSSagi Grimberg isert_cq_callback(struct ib_cq *cq, void *context)
2067b8d26b3bSNicholas Bellinger {
20684a295baeSSagi Grimberg 	struct isert_comp *comp = context;
2069b8d26b3bSNicholas Bellinger 
20706f0fae3dSSagi Grimberg 	queue_work(isert_comp_wq, &comp->work);
2071b8d26b3bSNicholas Bellinger }
2072b8d26b3bSNicholas Bellinger 
2073b8d26b3bSNicholas Bellinger static int
2074b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2075b8d26b3bSNicholas Bellinger {
2076b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2077b8d26b3bSNicholas Bellinger 	int ret;
2078b8d26b3bSNicholas Bellinger 
2079b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2080b8d26b3bSNicholas Bellinger 			   &wr_failed);
2081b8d26b3bSNicholas Bellinger 	if (ret) {
208224f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
2083b8d26b3bSNicholas Bellinger 		return ret;
2084b8d26b3bSNicholas Bellinger 	}
2085b8d26b3bSNicholas Bellinger 	return ret;
2086b8d26b3bSNicholas Bellinger }
2087b8d26b3bSNicholas Bellinger 
2088b8d26b3bSNicholas Bellinger static int
2089b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2090b8d26b3bSNicholas Bellinger {
2091d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2092b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2093b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2094b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2095b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2096b8d26b3bSNicholas Bellinger 
2097b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2098b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2099b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2100b8d26b3bSNicholas Bellinger 	/*
2101b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2102b8d26b3bSNicholas Bellinger 	 */
2103b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2104b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2105b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2106b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2107b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2108dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2109b8d26b3bSNicholas Bellinger 
2110b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2111b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2112b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2113b8d26b3bSNicholas Bellinger 
2114b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2115b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2116dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2117b8d26b3bSNicholas Bellinger 
2118dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2119dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2120b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2121b8d26b3bSNicholas Bellinger 
2122dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2123dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2124dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
2125b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2126b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2127b8d26b3bSNicholas Bellinger 	}
2128b8d26b3bSNicholas Bellinger 
212968a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2130b8d26b3bSNicholas Bellinger 
21314c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
2132b8d26b3bSNicholas Bellinger 
2133b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2134b8d26b3bSNicholas Bellinger }
2135b8d26b3bSNicholas Bellinger 
2136131e6abcSNicholas Bellinger static void
2137131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2138131e6abcSNicholas Bellinger {
2139131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2140131e6abcSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2141131e6abcSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2142131e6abcSNicholas Bellinger 
2143131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2144131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2145131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2146131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2147131e6abcSNicholas Bellinger 
2148131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2149131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2150131e6abcSNicholas Bellinger 
2151131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2152131e6abcSNicholas Bellinger }
2153131e6abcSNicholas Bellinger 
2154e70beee7SNicholas Bellinger static enum target_prot_op
2155e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2156e70beee7SNicholas Bellinger {
2157e70beee7SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2158e70beee7SNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2159e70beee7SNicholas Bellinger 
216023a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
216123a548eeSSagi Grimberg 		if (device->pi_capable) {
216224f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
216323a548eeSSagi Grimberg 			isert_conn->pi_support = true;
2164e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
216523a548eeSSagi Grimberg 		}
216623a548eeSSagi Grimberg 	}
216723a548eeSSagi Grimberg 
216824f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
216923a548eeSSagi Grimberg 	isert_conn->pi_support = false;
2170e70beee7SNicholas Bellinger 
2171e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2172e70beee7SNicholas Bellinger }
2173e70beee7SNicholas Bellinger 
2174b8d26b3bSNicholas Bellinger static int
2175b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2176b8d26b3bSNicholas Bellinger 		bool nopout_response)
2177b8d26b3bSNicholas Bellinger {
2178d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2179b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2180b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2181b8d26b3bSNicholas Bellinger 
2182b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2183b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2184b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2185b8d26b3bSNicholas Bellinger 			       nopout_response);
2186b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
218768a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2188b8d26b3bSNicholas Bellinger 
21894c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
2190b8d26b3bSNicholas Bellinger 
2191b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2192b8d26b3bSNicholas Bellinger }
2193b8d26b3bSNicholas Bellinger 
2194b8d26b3bSNicholas Bellinger static int
2195b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2196b8d26b3bSNicholas Bellinger {
2197d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2198b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2199b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2200b8d26b3bSNicholas Bellinger 
2201b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2202b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2203b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2204b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
220568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2206b8d26b3bSNicholas Bellinger 
22074c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
2208b8d26b3bSNicholas Bellinger 
2209b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2210b8d26b3bSNicholas Bellinger }
2211b8d26b3bSNicholas Bellinger 
2212b8d26b3bSNicholas Bellinger static int
2213b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2214b8d26b3bSNicholas Bellinger {
2215d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2216b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2217b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2218b8d26b3bSNicholas Bellinger 
2219b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2220b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2221b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2222b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
222368a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2224b8d26b3bSNicholas Bellinger 
22254c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
2226b8d26b3bSNicholas Bellinger 
2227b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2228b8d26b3bSNicholas Bellinger }
2229b8d26b3bSNicholas Bellinger 
2230b8d26b3bSNicholas Bellinger static int
2231b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2232b8d26b3bSNicholas Bellinger {
2233d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2234b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2235b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
22363df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
22373df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
22383df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
22393df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2240b8d26b3bSNicholas Bellinger 
2241b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
22423df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2243b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
22443df8f68aSNicholas Bellinger 
22453df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2246dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
22473df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
22483df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2249dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2250dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
22513df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
22523df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
22533df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
22543df8f68aSNicholas Bellinger 
225568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2256b8d26b3bSNicholas Bellinger 
22574c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
2258b8d26b3bSNicholas Bellinger 
2259b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2260b8d26b3bSNicholas Bellinger }
2261b8d26b3bSNicholas Bellinger 
2262b8d26b3bSNicholas Bellinger static int
2263adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2264adb54c29SNicholas Bellinger {
2265d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2266adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2267adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2268adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2269adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2270adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2271adb54c29SNicholas Bellinger 	int rc;
2272adb54c29SNicholas Bellinger 
2273adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
227422c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2275adb54c29SNicholas Bellinger 	if (rc < 0)
2276adb54c29SNicholas Bellinger 		return rc;
2277adb54c29SNicholas Bellinger 
2278adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2279adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2280adb54c29SNicholas Bellinger 
2281adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2282adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2283adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2284adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2285adb54c29SNicholas Bellinger 
2286adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2287adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2288adb54c29SNicholas Bellinger 
2289adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2290adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2291adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
2292adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2293adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2294adb54c29SNicholas Bellinger 	}
229568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2296adb54c29SNicholas Bellinger 
2297f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
2298adb54c29SNicholas Bellinger 
2299adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2300adb54c29SNicholas Bellinger }
2301adb54c29SNicholas Bellinger 
2302adb54c29SNicholas Bellinger static int
2303b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2304b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2305b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2306b8d26b3bSNicholas Bellinger {
2307d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2308b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2309b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2310b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2311b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2312b8d26b3bSNicholas Bellinger 
2313b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2314b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2315b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2316b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2317b8d26b3bSNicholas Bellinger 
2318b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2319b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2320b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
2321b8d26b3bSNicholas Bellinger 	/*
2322b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2323b8d26b3bSNicholas Bellinger 	 */
2324b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
23254c22e07fSSagi Grimberg 		isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
23264c22e07fSSagi Grimberg 			  "page_off: %u\n",
2327b8d26b3bSNicholas Bellinger 			  (unsigned long long)tmp_sg->dma_address,
2328b8d26b3bSNicholas Bellinger 			  tmp_sg->length, page_off);
2329b8d26b3bSNicholas Bellinger 
2330b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2331b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2332b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2333b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2334b8d26b3bSNicholas Bellinger 
23354c22e07fSSagi Grimberg 		isert_dbg("RDMA ib_sge: addr: 0x%llx  length: %u lkey: %x\n",
233690ecc6e2SVu Pham 			  ib_sge->addr, ib_sge->length, ib_sge->lkey);
2337b8d26b3bSNicholas Bellinger 		page_off = 0;
2338b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2339b8d26b3bSNicholas Bellinger 		ib_sge++;
234024f412ddSSagi Grimberg 		isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
2341b8d26b3bSNicholas Bellinger 	}
2342b8d26b3bSNicholas Bellinger 
234324f412ddSSagi Grimberg 	isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2344b8d26b3bSNicholas Bellinger 		  send_wr->sg_list, send_wr->num_sge);
2345b8d26b3bSNicholas Bellinger 
2346b8d26b3bSNicholas Bellinger 	return sg_nents;
2347b8d26b3bSNicholas Bellinger }
2348b8d26b3bSNicholas Bellinger 
2349b8d26b3bSNicholas Bellinger static int
235090ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
235190ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2352b8d26b3bSNicholas Bellinger {
2353b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2354d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2355b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2356e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
235790ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2358b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2359e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2360e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2361b8d26b3bSNicholas Bellinger 
236290ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2363b8d26b3bSNicholas Bellinger 
2364e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2365e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2366e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2367e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2368e3d7e4c3SSagi Grimberg 	if (ret)
2369e3d7e4c3SSagi Grimberg 		return ret;
2370b8d26b3bSNicholas Bellinger 
2371e3d7e4c3SSagi Grimberg 	data_left = data->len;
2372e3d7e4c3SSagi Grimberg 	offset = data->offset;
2373b8d26b3bSNicholas Bellinger 
2374e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2375b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
237624f412ddSSagi Grimberg 		isert_warn("Unable to allocate ib_sge\n");
2377b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2378e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2379b8d26b3bSNicholas Bellinger 	}
238090ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2381b8d26b3bSNicholas Bellinger 
2382e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2383b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2384b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2385b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
238624f412ddSSagi Grimberg 		isert_dbg("Unable to allocate wr->send_wr\n");
2387b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2388e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2389b8d26b3bSNicholas Bellinger 	}
2390b8d26b3bSNicholas Bellinger 
2391b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2392b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2393b8d26b3bSNicholas Bellinger 
2394b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2395b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2396b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2397b8d26b3bSNicholas Bellinger 
2398b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
239990ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
240090ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2401b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2402b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
240390ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
240490ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
240590ecc6e2SVu Pham 			else
240690ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
240790ecc6e2SVu Pham 		} else {
240890ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
240990ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
241090ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
241190ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
241290ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
241390ecc6e2SVu Pham 			else
241490ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
241590ecc6e2SVu Pham 		}
2416b8d26b3bSNicholas Bellinger 
2417b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2418b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2419b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2420b8d26b3bSNicholas Bellinger 
2421b8d26b3bSNicholas Bellinger 		offset += data_len;
242290ecc6e2SVu Pham 		va_offset += data_len;
2423b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2424b8d26b3bSNicholas Bellinger 	}
242590ecc6e2SVu Pham 
242690ecc6e2SVu Pham 	return 0;
2427e3d7e4c3SSagi Grimberg unmap_cmd:
2428e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2429e3d7e4c3SSagi Grimberg 
243090ecc6e2SVu Pham 	return ret;
243190ecc6e2SVu Pham }
243290ecc6e2SVu Pham 
243390ecc6e2SVu Pham static int
243459464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
243559464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
243659464ef4SVu Pham {
243759464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
243859464ef4SVu Pham 	struct scatterlist *tmp_sg;
243959464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
244059464ef4SVu Pham 
244159464ef4SVu Pham 	n_pages = 0;
244259464ef4SVu Pham 	new_chunk = 1;
244359464ef4SVu Pham 	last_ent = sg_nents - 1;
244459464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
244559464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
244659464ef4SVu Pham 		if (new_chunk)
244759464ef4SVu Pham 			chunk_start = start_addr;
244859464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
244959464ef4SVu Pham 
24504c22e07fSSagi Grimberg 		isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
245159464ef4SVu Pham 			  i, (unsigned long long)tmp_sg->dma_address,
245259464ef4SVu Pham 			  tmp_sg->length);
245359464ef4SVu Pham 
245459464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
245559464ef4SVu Pham 			new_chunk = 0;
245659464ef4SVu Pham 			continue;
245759464ef4SVu Pham 		}
245859464ef4SVu Pham 		new_chunk = 1;
245959464ef4SVu Pham 
246059464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
246159464ef4SVu Pham 		do {
246259464ef4SVu Pham 			fr_pl[n_pages++] = page;
24634c22e07fSSagi Grimberg 			isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
246459464ef4SVu Pham 				  n_pages - 1, page);
246559464ef4SVu Pham 			page += PAGE_SIZE;
246659464ef4SVu Pham 		} while (page < end_addr);
246759464ef4SVu Pham 	}
246859464ef4SVu Pham 
246959464ef4SVu Pham 	return n_pages;
247059464ef4SVu Pham }
247159464ef4SVu Pham 
247210633c37SSagi Grimberg static inline void
247310633c37SSagi Grimberg isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
247410633c37SSagi Grimberg {
247510633c37SSagi Grimberg 	u32 rkey;
247610633c37SSagi Grimberg 
247710633c37SSagi Grimberg 	memset(inv_wr, 0, sizeof(*inv_wr));
247810633c37SSagi Grimberg 	inv_wr->wr_id = ISER_FASTREG_LI_WRID;
247910633c37SSagi Grimberg 	inv_wr->opcode = IB_WR_LOCAL_INV;
248010633c37SSagi Grimberg 	inv_wr->ex.invalidate_rkey = mr->rkey;
248110633c37SSagi Grimberg 
248210633c37SSagi Grimberg 	/* Bump the key */
248310633c37SSagi Grimberg 	rkey = ib_inc_rkey(mr->rkey);
248410633c37SSagi Grimberg 	ib_update_fast_reg_key(mr, rkey);
248510633c37SSagi Grimberg }
248610633c37SSagi Grimberg 
248759464ef4SVu Pham static int
2488e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2489e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2490e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
24919e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2492e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
249359464ef4SVu Pham {
249459464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
24959e961ae7SSagi Grimberg 	struct ib_mr *mr;
24969e961ae7SSagi Grimberg 	struct ib_fast_reg_page_list *frpl;
249759464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
249859464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
24999bd626e7SSagi Grimberg 	int ret, pagelist_len;
25009bd626e7SSagi Grimberg 	u32 page_off;
250159464ef4SVu Pham 
2502e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
2503e3d7e4c3SSagi Grimberg 		sge->lkey = isert_conn->conn_mr->lkey;
2504e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2505e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
25064c22e07fSSagi Grimberg 		isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
25074c22e07fSSagi Grimberg 			 sge->addr, sge->length, sge->lkey);
2508e3d7e4c3SSagi Grimberg 		return 0;
2509e3d7e4c3SSagi Grimberg 	}
2510e3d7e4c3SSagi Grimberg 
25119e961ae7SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID) {
25129e961ae7SSagi Grimberg 		/* Registering data buffer */
25139e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
25149e961ae7SSagi Grimberg 		frpl = fr_desc->data_frpl;
25159e961ae7SSagi Grimberg 	} else {
25169e961ae7SSagi Grimberg 		/* Registering protection buffer */
25179e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
25189e961ae7SSagi Grimberg 		frpl = fr_desc->pi_ctx->prot_frpl;
25199e961ae7SSagi Grimberg 	}
25209e961ae7SSagi Grimberg 
2521e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
252259464ef4SVu Pham 
252324f412ddSSagi Grimberg 	isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2524e3d7e4c3SSagi Grimberg 		  fr_desc, mem->nents, mem->offset);
252559464ef4SVu Pham 
2526e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
25279e961ae7SSagi Grimberg 					     &frpl->page_list[0]);
252859464ef4SVu Pham 
252910633c37SSagi Grimberg 	if (!(fr_desc->ind & ind)) {
253010633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, mr);
253159464ef4SVu Pham 		wr = &inv_wr;
253259464ef4SVu Pham 	}
253359464ef4SVu Pham 
253459464ef4SVu Pham 	/* Prepare FASTREG WR */
253559464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
25369bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
253759464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
25389e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
25399e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.page_list = frpl;
254059464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
254159464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2542e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
25439e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.rkey = mr->rkey;
254459464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
254559464ef4SVu Pham 
254659464ef4SVu Pham 	if (!wr)
254759464ef4SVu Pham 		wr = &fr_wr;
254859464ef4SVu Pham 	else
254959464ef4SVu Pham 		wr->next = &fr_wr;
255059464ef4SVu Pham 
255159464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
255259464ef4SVu Pham 	if (ret) {
255324f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
255459464ef4SVu Pham 		return ret;
255559464ef4SVu Pham 	}
25569e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
255759464ef4SVu Pham 
25589e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
25599e961ae7SSagi Grimberg 	sge->addr = frpl->page_list[0] + page_off;
2560e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
256159464ef4SVu Pham 
25624c22e07fSSagi Grimberg 	isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
25634c22e07fSSagi Grimberg 		  sge->addr, sge->length, sge->lkey);
256459464ef4SVu Pham 
256559464ef4SVu Pham 	return ret;
256659464ef4SVu Pham }
256759464ef4SVu Pham 
25683d73cf1aSSagi Grimberg static inline void
25693d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
25703d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
25719e961ae7SSagi Grimberg {
257278eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
25733d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
25743d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
25753d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
257678eda2bbSSagi Grimberg 	/*
257778eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
257878eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
257978eda2bbSSagi Grimberg 	 * from se_cmd.
258078eda2bbSSagi Grimberg 	 */
258178eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
258278eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
258378eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
258478eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
258578eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
258678eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
25873d73cf1aSSagi Grimberg };
25883d73cf1aSSagi Grimberg 
25893d73cf1aSSagi Grimberg static int
25903d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
25913d73cf1aSSagi Grimberg {
25929e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
25939e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
25949e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
259578eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
25963d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
25979e961ae7SSagi Grimberg 		break;
25989e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
25999e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
260078eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
26013d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
26029e961ae7SSagi Grimberg 		break;
26039e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
26049e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
26053d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
26063d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
26079e961ae7SSagi Grimberg 		break;
26089e961ae7SSagi Grimberg 	default:
260924f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
26109e961ae7SSagi Grimberg 		return -EINVAL;
26119e961ae7SSagi Grimberg 	}
26129e961ae7SSagi Grimberg 
26139e961ae7SSagi Grimberg 	return 0;
26149e961ae7SSagi Grimberg }
26159e961ae7SSagi Grimberg 
26169e961ae7SSagi Grimberg static inline u8
26179e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
26189e961ae7SSagi Grimberg {
26199e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
26209e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
26219e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
26229e961ae7SSagi Grimberg }
26239e961ae7SSagi Grimberg 
26249e961ae7SSagi Grimberg static int
2625570db170SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn,
2626570db170SSagi Grimberg 		 struct se_cmd *se_cmd,
2627570db170SSagi Grimberg 		 struct isert_rdma_wr *rdma_wr,
2628570db170SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc)
26299e961ae7SSagi Grimberg {
26309e961ae7SSagi Grimberg 	struct ib_send_wr sig_wr, inv_wr;
26319e961ae7SSagi Grimberg 	struct ib_send_wr *bad_wr, *wr = NULL;
26329e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
26339e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
26349e961ae7SSagi Grimberg 	int ret;
26359e961ae7SSagi Grimberg 
26369e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
26379e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
26389e961ae7SSagi Grimberg 	if (ret)
26399e961ae7SSagi Grimberg 		goto err;
26409e961ae7SSagi Grimberg 
26419e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
26429e961ae7SSagi Grimberg 
26439e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
264410633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
26459e961ae7SSagi Grimberg 		wr = &inv_wr;
26469e961ae7SSagi Grimberg 	}
26479e961ae7SSagi Grimberg 
26489e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
26499e961ae7SSagi Grimberg 	sig_wr.opcode = IB_WR_REG_SIG_MR;
2650c2caa207SSagi Grimberg 	sig_wr.wr_id = ISER_FASTREG_LI_WRID;
2651570db170SSagi Grimberg 	sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
26529e961ae7SSagi Grimberg 	sig_wr.num_sge = 1;
26539e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
26549e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
26559e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
26569e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
2657570db170SSagi Grimberg 		sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
26589e961ae7SSagi Grimberg 
26599e961ae7SSagi Grimberg 	if (!wr)
26609e961ae7SSagi Grimberg 		wr = &sig_wr;
26619e961ae7SSagi Grimberg 	else
26629e961ae7SSagi Grimberg 		wr->next = &sig_wr;
26639e961ae7SSagi Grimberg 
26649e961ae7SSagi Grimberg 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
26659e961ae7SSagi Grimberg 	if (ret) {
266624f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
26679e961ae7SSagi Grimberg 		goto err;
26689e961ae7SSagi Grimberg 	}
26699e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
26709e961ae7SSagi Grimberg 
2671570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2672570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].addr = 0;
2673570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
26749e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
26759e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
26769e961ae7SSagi Grimberg 		/*
26779e961ae7SSagi Grimberg 		 * We have protection guards on the wire
26789e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
26799e961ae7SSagi Grimberg 		 */
2680570db170SSagi Grimberg 		rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
26819e961ae7SSagi Grimberg 
268224f412ddSSagi Grimberg 	isert_dbg("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
2683570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2684570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].lkey);
26859e961ae7SSagi Grimberg err:
26869e961ae7SSagi Grimberg 	return ret;
26879e961ae7SSagi Grimberg }
26889e961ae7SSagi Grimberg 
268959464ef4SVu Pham static int
2690570db170SSagi Grimberg isert_handle_prot_cmd(struct isert_conn *isert_conn,
2691570db170SSagi Grimberg 		      struct isert_cmd *isert_cmd,
2692570db170SSagi Grimberg 		      struct isert_rdma_wr *wr)
2693570db170SSagi Grimberg {
2694570db170SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
2695570db170SSagi Grimberg 	struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2696570db170SSagi Grimberg 	int ret;
2697570db170SSagi Grimberg 
2698570db170SSagi Grimberg 	if (!wr->fr_desc->pi_ctx) {
2699570db170SSagi Grimberg 		ret = isert_create_pi_ctx(wr->fr_desc,
2700570db170SSagi Grimberg 					  device->ib_device,
2701570db170SSagi Grimberg 					  isert_conn->conn_pd);
2702570db170SSagi Grimberg 		if (ret) {
270324f412ddSSagi Grimberg 			isert_err("conn %p failed to allocate pi_ctx\n",
2704570db170SSagi Grimberg 				  isert_conn);
2705570db170SSagi Grimberg 			return ret;
2706570db170SSagi Grimberg 		}
2707570db170SSagi Grimberg 	}
2708570db170SSagi Grimberg 
2709570db170SSagi Grimberg 	if (se_cmd->t_prot_sg) {
2710570db170SSagi Grimberg 		ret = isert_map_data_buf(isert_conn, isert_cmd,
2711570db170SSagi Grimberg 					 se_cmd->t_prot_sg,
2712570db170SSagi Grimberg 					 se_cmd->t_prot_nents,
2713570db170SSagi Grimberg 					 se_cmd->prot_length,
2714570db170SSagi Grimberg 					 0, wr->iser_ib_op, &wr->prot);
2715570db170SSagi Grimberg 		if (ret) {
271624f412ddSSagi Grimberg 			isert_err("conn %p failed to map protection buffer\n",
2717570db170SSagi Grimberg 				  isert_conn);
2718570db170SSagi Grimberg 			return ret;
2719570db170SSagi Grimberg 		}
2720570db170SSagi Grimberg 
2721570db170SSagi Grimberg 		memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2722570db170SSagi Grimberg 		ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2723570db170SSagi Grimberg 					ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2724570db170SSagi Grimberg 		if (ret) {
272524f412ddSSagi Grimberg 			isert_err("conn %p failed to fast reg mr\n",
2726570db170SSagi Grimberg 				  isert_conn);
2727570db170SSagi Grimberg 			goto unmap_prot_cmd;
2728570db170SSagi Grimberg 		}
2729570db170SSagi Grimberg 	}
2730570db170SSagi Grimberg 
2731570db170SSagi Grimberg 	ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2732570db170SSagi Grimberg 	if (ret) {
273324f412ddSSagi Grimberg 		isert_err("conn %p failed to fast reg mr\n",
2734570db170SSagi Grimberg 			  isert_conn);
2735570db170SSagi Grimberg 		goto unmap_prot_cmd;
2736570db170SSagi Grimberg 	}
2737570db170SSagi Grimberg 	wr->fr_desc->ind |= ISERT_PROTECTED;
2738570db170SSagi Grimberg 
2739570db170SSagi Grimberg 	return 0;
2740570db170SSagi Grimberg 
2741570db170SSagi Grimberg unmap_prot_cmd:
2742570db170SSagi Grimberg 	if (se_cmd->t_prot_sg)
2743570db170SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2744570db170SSagi Grimberg 
2745570db170SSagi Grimberg 	return ret;
2746570db170SSagi Grimberg }
2747570db170SSagi Grimberg 
2748570db170SSagi Grimberg static int
2749a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
275059464ef4SVu Pham 	       struct isert_rdma_wr *wr)
275159464ef4SVu Pham {
275259464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
275359464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2754e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2755e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2756570db170SSagi Grimberg 	struct ib_send_wr *send_wr;
2757570db170SSagi Grimberg 	struct ib_sge *ib_sg;
2758e3d7e4c3SSagi Grimberg 	u32 offset;
2759e3d7e4c3SSagi Grimberg 	int ret = 0;
276059464ef4SVu Pham 	unsigned long flags;
276159464ef4SVu Pham 
276259464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2763e3d7e4c3SSagi Grimberg 
2764e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2765e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2766e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2767e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2768e3d7e4c3SSagi Grimberg 	if (ret)
2769e3d7e4c3SSagi Grimberg 		return ret;
2770e3d7e4c3SSagi Grimberg 
2771302cc7c3SSagi Grimberg 	if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
2772e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2773e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2774e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2775e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2776e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2777e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
277859464ef4SVu Pham 	}
277959464ef4SVu Pham 
27809e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2781570db170SSagi Grimberg 				ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
2782e3d7e4c3SSagi Grimberg 	if (ret)
2783e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
278459464ef4SVu Pham 
2785302cc7c3SSagi Grimberg 	if (isert_prot_cmd(isert_conn, se_cmd)) {
2786570db170SSagi Grimberg 		ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
27879e961ae7SSagi Grimberg 		if (ret)
27889e961ae7SSagi Grimberg 			goto unmap_cmd;
27899e961ae7SSagi Grimberg 
2790570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[SIG];
2791570db170SSagi Grimberg 	} else {
2792570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[DATA];
27939e961ae7SSagi Grimberg 	}
27949e961ae7SSagi Grimberg 
2795570db170SSagi Grimberg 	memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
2796e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
279759464ef4SVu Pham 	wr->send_wr_num = 1;
279859464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
279959464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
280059464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
280159464ef4SVu Pham 
280259464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2803e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
280459464ef4SVu Pham 	send_wr->num_sge = 1;
2805b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
280659464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
280759464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
280859464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
280959464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2810302cc7c3SSagi Grimberg 		send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
28119e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
281259464ef4SVu Pham 	} else {
281359464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
281459464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
281559464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
281659464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
281759464ef4SVu Pham 	}
281859464ef4SVu Pham 
281959464ef4SVu Pham 	return 0;
2820570db170SSagi Grimberg 
2821e3d7e4c3SSagi Grimberg unmap_cmd:
2822e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2823e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2824e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2825e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2826e3d7e4c3SSagi Grimberg 	}
2827e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
282859464ef4SVu Pham 
282959464ef4SVu Pham 	return ret;
283059464ef4SVu Pham }
283159464ef4SVu Pham 
283259464ef4SVu Pham static int
283390ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
283490ecc6e2SVu Pham {
283590ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
283659464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
283790ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
283890ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2839d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
284090ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
284190ecc6e2SVu Pham 	int rc;
284290ecc6e2SVu Pham 
284324f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
284490ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
28454c22e07fSSagi Grimberg 
284690ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2847d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
284890ecc6e2SVu Pham 	if (rc) {
284924f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
285090ecc6e2SVu Pham 		return rc;
285190ecc6e2SVu Pham 	}
285290ecc6e2SVu Pham 
2853302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd)) {
2854b8d26b3bSNicholas Bellinger 		/*
2855b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2856b8d26b3bSNicholas Bellinger 		 */
28579e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
28589e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
285904d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2860b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2861b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
286295b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
286368a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
28649e961ae7SSagi Grimberg 		isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
2865897bb2c9SSagi Grimberg 		wr->send_wr_num += 1;
28669e961ae7SSagi Grimberg 	}
2867b8d26b3bSNicholas Bellinger 
2868b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2869bdf20e72SSagi Grimberg 	if (rc)
287024f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
28719e961ae7SSagi Grimberg 
2872302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd))
287324f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
28749e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
28759e961ae7SSagi Grimberg 	else
287624f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
287790ecc6e2SVu Pham 			 isert_cmd);
2878b8d26b3bSNicholas Bellinger 
287990ecc6e2SVu Pham 	return 1;
2880b8d26b3bSNicholas Bellinger }
2881b8d26b3bSNicholas Bellinger 
2882b8d26b3bSNicholas Bellinger static int
2883b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2884b8d26b3bSNicholas Bellinger {
2885b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2886d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2887b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2888b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2889d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
289090ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
289190ecc6e2SVu Pham 	int rc;
2892b8d26b3bSNicholas Bellinger 
289324f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
289490ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2895b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2896d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
289790ecc6e2SVu Pham 	if (rc) {
289824f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
289990ecc6e2SVu Pham 		return rc;
2900b8d26b3bSNicholas Bellinger 	}
2901b8d26b3bSNicholas Bellinger 
2902b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2903bdf20e72SSagi Grimberg 	if (rc)
290424f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2905bdf20e72SSagi Grimberg 
290624f412ddSSagi Grimberg 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
290790ecc6e2SVu Pham 		 isert_cmd);
2908b8d26b3bSNicholas Bellinger 
290990ecc6e2SVu Pham 	return 0;
2910b8d26b3bSNicholas Bellinger }
2911b8d26b3bSNicholas Bellinger 
2912b8d26b3bSNicholas Bellinger static int
2913b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2914b8d26b3bSNicholas Bellinger {
2915b8d26b3bSNicholas Bellinger 	int ret;
2916b8d26b3bSNicholas Bellinger 
2917b8d26b3bSNicholas Bellinger 	switch (state) {
2918b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2919b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2920b8d26b3bSNicholas Bellinger 		break;
2921b8d26b3bSNicholas Bellinger 	default:
292224f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2923b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2924b8d26b3bSNicholas Bellinger 		break;
2925b8d26b3bSNicholas Bellinger 	}
2926b8d26b3bSNicholas Bellinger 
2927b8d26b3bSNicholas Bellinger 	return ret;
2928b8d26b3bSNicholas Bellinger }
2929b8d26b3bSNicholas Bellinger 
2930b8d26b3bSNicholas Bellinger static int
2931b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2932b8d26b3bSNicholas Bellinger {
2933991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2934b8d26b3bSNicholas Bellinger 	int ret;
2935b8d26b3bSNicholas Bellinger 
2936b8d26b3bSNicholas Bellinger 	switch (state) {
2937b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2938b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2939991bb764SSagi Grimberg 		if (!ret)
2940991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2941b8d26b3bSNicholas Bellinger 		break;
2942b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2943b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2944b8d26b3bSNicholas Bellinger 		break;
2945b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2946b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2947b8d26b3bSNicholas Bellinger 		break;
2948b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2949b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2950b8d26b3bSNicholas Bellinger 		break;
2951adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2952adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2953adb54c29SNicholas Bellinger 		break;
2954b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2955b8d26b3bSNicholas Bellinger 		/*
2956b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2957b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2958b8d26b3bSNicholas Bellinger 		 */
2959b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2960b8d26b3bSNicholas Bellinger 		break;
2961b8d26b3bSNicholas Bellinger 	default:
296224f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2963b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2964b8d26b3bSNicholas Bellinger 		break;
2965b8d26b3bSNicholas Bellinger 	}
2966b8d26b3bSNicholas Bellinger 
2967b8d26b3bSNicholas Bellinger 	return ret;
2968b8d26b3bSNicholas Bellinger }
2969b8d26b3bSNicholas Bellinger 
2970ca6c1d82SSagi Grimberg struct rdma_cm_id *
2971ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2972ca6c1d82SSagi Grimberg {
2973ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2974ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2975ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2976ca6c1d82SSagi Grimberg 	int ret;
2977ca6c1d82SSagi Grimberg 
2978ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
297924f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2980ca6c1d82SSagi Grimberg 
2981ca6c1d82SSagi Grimberg 	id = rdma_create_id(isert_cma_handler, isert_np,
2982ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2983ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
298424f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2985ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2986ca6c1d82SSagi Grimberg 		goto out;
2987ca6c1d82SSagi Grimberg 	}
298824f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2989ca6c1d82SSagi Grimberg 
2990ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2991ca6c1d82SSagi Grimberg 	if (ret) {
299224f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2993ca6c1d82SSagi Grimberg 		goto out_id;
2994ca6c1d82SSagi Grimberg 	}
2995ca6c1d82SSagi Grimberg 
2996ca6c1d82SSagi Grimberg 	ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
2997ca6c1d82SSagi Grimberg 	if (ret) {
299824f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2999ca6c1d82SSagi Grimberg 		goto out_id;
3000ca6c1d82SSagi Grimberg 	}
3001ca6c1d82SSagi Grimberg 
3002ca6c1d82SSagi Grimberg 	return id;
3003ca6c1d82SSagi Grimberg out_id:
3004ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
3005ca6c1d82SSagi Grimberg out:
3006ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
3007ca6c1d82SSagi Grimberg }
3008ca6c1d82SSagi Grimberg 
3009b8d26b3bSNicholas Bellinger static int
3010b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
3011b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
3012b8d26b3bSNicholas Bellinger {
3013b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
3014b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
3015b8d26b3bSNicholas Bellinger 	int ret;
3016b8d26b3bSNicholas Bellinger 
3017b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3018b8d26b3bSNicholas Bellinger 	if (!isert_np) {
301924f412ddSSagi Grimberg 		isert_err("Unable to allocate struct isert_np\n");
3020b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3021b8d26b3bSNicholas Bellinger 	}
3022531b7bf4SSagi Grimberg 	sema_init(&isert_np->np_sem, 0);
3023b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
3024b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
3025b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
3026ca6c1d82SSagi Grimberg 	isert_np->np = np;
3027b8d26b3bSNicholas Bellinger 
3028b8d26b3bSNicholas Bellinger 	/*
3029b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3030b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3031b8d26b3bSNicholas Bellinger 	 */
3032b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
3033b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
3034b8d26b3bSNicholas Bellinger 
3035ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
3036b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3037b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3038b8d26b3bSNicholas Bellinger 		goto out;
3039b8d26b3bSNicholas Bellinger 	}
3040b8d26b3bSNicholas Bellinger 
3041b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
3042b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3043b8d26b3bSNicholas Bellinger 
3044b8d26b3bSNicholas Bellinger 	return 0;
3045b8d26b3bSNicholas Bellinger 
3046b8d26b3bSNicholas Bellinger out:
3047b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3048ca6c1d82SSagi Grimberg 
3049b8d26b3bSNicholas Bellinger 	return ret;
3050b8d26b3bSNicholas Bellinger }
3051b8d26b3bSNicholas Bellinger 
3052b8d26b3bSNicholas Bellinger static int
3053b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3054b8d26b3bSNicholas Bellinger {
3055b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3056b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3057b8d26b3bSNicholas Bellinger 	int ret;
3058b8d26b3bSNicholas Bellinger 
3059b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3060b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3061b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3062b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3063b8d26b3bSNicholas Bellinger 
3064b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3065b8d26b3bSNicholas Bellinger 	if (ret) {
306624f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
3067b8d26b3bSNicholas Bellinger 		return ret;
3068b8d26b3bSNicholas Bellinger 	}
3069b8d26b3bSNicholas Bellinger 
3070b8d26b3bSNicholas Bellinger 	return 0;
3071b8d26b3bSNicholas Bellinger }
3072b8d26b3bSNicholas Bellinger 
3073b8d26b3bSNicholas Bellinger static int
3074b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3075b8d26b3bSNicholas Bellinger {
3076b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3077b8d26b3bSNicholas Bellinger 	int ret;
3078b8d26b3bSNicholas Bellinger 
307924f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
30802371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
30812371e5daSSagi Grimberg 	if (ret) {
308224f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
30832371e5daSSagi Grimberg 			  isert_conn);
30842371e5daSSagi Grimberg 		return ret;
30852371e5daSSagi Grimberg 	}
30862371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
30872371e5daSSagi Grimberg 
30886faaa85fSNicholas Bellinger 	/*
30896faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
30906faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
30916faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
30926faaa85fSNicholas Bellinger 	 * into a NOP.
30936faaa85fSNicholas Bellinger 	 */
30946faaa85fSNicholas Bellinger 	if (!login->first_request)
30956faaa85fSNicholas Bellinger 		return 0;
3096b8d26b3bSNicholas Bellinger 
30972371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
30982371e5daSSagi Grimberg 
309924f412ddSSagi Grimberg 	isert_info("before conn_login_comp conn: %p\n", conn);
3100b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3101b8d26b3bSNicholas Bellinger 	if (ret)
3102b8d26b3bSNicholas Bellinger 		return ret;
3103b8d26b3bSNicholas Bellinger 
310424f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
31052371e5daSSagi Grimberg 
3106b8d26b3bSNicholas Bellinger 	return 0;
3107b8d26b3bSNicholas Bellinger }
3108b8d26b3bSNicholas Bellinger 
3109b8d26b3bSNicholas Bellinger static void
3110b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3111b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3112b8d26b3bSNicholas Bellinger {
3113b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3114b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3115b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
3116b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
3117b8d26b3bSNicholas Bellinger 
3118b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3119b8d26b3bSNicholas Bellinger 
3120b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
3121b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3122b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3123b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3124b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
3125b8d26b3bSNicholas Bellinger 
3126b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3127b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3128b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3129b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
3130b8d26b3bSNicholas Bellinger 	} else {
3131b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3132b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
3133b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3134b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
3135b8d26b3bSNicholas Bellinger 
3136b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3137b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
3138b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3139b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
3140b8d26b3bSNicholas Bellinger 	}
3141b8d26b3bSNicholas Bellinger }
3142b8d26b3bSNicholas Bellinger 
3143b8d26b3bSNicholas Bellinger static int
3144b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3145b8d26b3bSNicholas Bellinger {
3146b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3147b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3148b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
3149b8d26b3bSNicholas Bellinger 
3150b8d26b3bSNicholas Bellinger accept_wait:
3151531b7bf4SSagi Grimberg 	ret = down_interruptible(&isert_np->np_sem);
31521acff63fSSagi Grimberg 	if (ret || max_accept > 5)
3153b8d26b3bSNicholas Bellinger 		return -ENODEV;
3154b8d26b3bSNicholas Bellinger 
3155b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3156e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3157b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
315811378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
3159e346ab34SSagi Grimberg 			 np->np_thread_state);
3160e346ab34SSagi Grimberg 		/**
3161e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
3162e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
3163e346ab34SSagi Grimberg 		 **/
3164b8d26b3bSNicholas Bellinger 		return -ENODEV;
3165b8d26b3bSNicholas Bellinger 	}
3166b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3167b8d26b3bSNicholas Bellinger 
3168b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3169b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
3170b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
3171b8d26b3bSNicholas Bellinger 		max_accept++;
3172b8d26b3bSNicholas Bellinger 		goto accept_wait;
3173b8d26b3bSNicholas Bellinger 	}
3174b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
3175b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
3176b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
3177b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3178b8d26b3bSNicholas Bellinger 
3179b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3180b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3181b8d26b3bSNicholas Bellinger 	max_accept = 0;
3182b8d26b3bSNicholas Bellinger 
3183b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3184b8d26b3bSNicholas Bellinger 
318524f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
31862371e5daSSagi Grimberg 
3187b8d26b3bSNicholas Bellinger 	return 0;
3188b8d26b3bSNicholas Bellinger }
3189b8d26b3bSNicholas Bellinger 
3190b8d26b3bSNicholas Bellinger static void
3191b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3192b8d26b3bSNicholas Bellinger {
3193b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3194268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
3195b8d26b3bSNicholas Bellinger 
31963b726ae2SSagi Grimberg 	if (isert_np->np_cm_id)
3197b8d26b3bSNicholas Bellinger 		rdma_destroy_id(isert_np->np_cm_id);
3198b8d26b3bSNicholas Bellinger 
3199268e6811SSagi Grimberg 	/*
3200268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
3201268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
3202268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
3203268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
3204268e6811SSagi Grimberg 	 * up in np_accept_list.
3205268e6811SSagi Grimberg 	 */
3206268e6811SSagi Grimberg 	mutex_lock(&isert_np->np_accept_mutex);
3207268e6811SSagi Grimberg 	if (!list_empty(&isert_np->np_accept_list)) {
320824f412ddSSagi Grimberg 		isert_info("Still have isert connections, cleaning up...\n");
3209268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
3210268e6811SSagi Grimberg 					 &isert_np->np_accept_list,
3211268e6811SSagi Grimberg 					 conn_accept_node) {
321224f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
3213268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
3214268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
3215268e6811SSagi Grimberg 		}
3216268e6811SSagi Grimberg 	}
3217268e6811SSagi Grimberg 	mutex_unlock(&isert_np->np_accept_mutex);
3218268e6811SSagi Grimberg 
3219b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3220b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3221b8d26b3bSNicholas Bellinger }
3222b8d26b3bSNicholas Bellinger 
3223b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
3224b02efbfcSSagi Grimberg {
3225b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
3226b02efbfcSSagi Grimberg 						     struct isert_conn,
3227b02efbfcSSagi Grimberg 						     release_work);
3228b02efbfcSSagi Grimberg 
322924f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
3230b02efbfcSSagi Grimberg 
3231b02efbfcSSagi Grimberg 	wait_for_completion(&isert_conn->conn_wait);
3232b02efbfcSSagi Grimberg 
3233b02efbfcSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3234b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
3235b02efbfcSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
3236b02efbfcSSagi Grimberg 
323724f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
3238b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
3239b02efbfcSSagi Grimberg }
3240b02efbfcSSagi Grimberg 
3241bdf20e72SSagi Grimberg static void
3242991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
3243991bb764SSagi Grimberg {
3244991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
3245991bb764SSagi Grimberg 
32464c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
32474c22e07fSSagi Grimberg 
3248991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
324924f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
3250991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
3251991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
3252991bb764SSagi Grimberg 	}
3253991bb764SSagi Grimberg }
3254991bb764SSagi Grimberg 
3255991bb764SSagi Grimberg static void
3256c7e160eeSSagi Grimberg isert_wait4cmds(struct iscsi_conn *conn)
3257c7e160eeSSagi Grimberg {
32584c22e07fSSagi Grimberg 	isert_info("iscsi_conn %p\n", conn);
32594c22e07fSSagi Grimberg 
3260c7e160eeSSagi Grimberg 	if (conn->sess) {
3261c7e160eeSSagi Grimberg 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3262c7e160eeSSagi Grimberg 		target_wait_for_sess_cmds(conn->sess->se_sess);
3263c7e160eeSSagi Grimberg 	}
3264c7e160eeSSagi Grimberg }
3265c7e160eeSSagi Grimberg 
3266c7e160eeSSagi Grimberg static void
3267bdf20e72SSagi Grimberg isert_wait4flush(struct isert_conn *isert_conn)
3268bdf20e72SSagi Grimberg {
3269bdf20e72SSagi Grimberg 	struct ib_recv_wr *bad_wr;
3270bdf20e72SSagi Grimberg 
32714c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
32724c22e07fSSagi Grimberg 
3273bdf20e72SSagi Grimberg 	init_completion(&isert_conn->conn_wait_comp_err);
3274bdf20e72SSagi Grimberg 	isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3275bdf20e72SSagi Grimberg 	/* post an indication that all flush errors were consumed */
3276bdf20e72SSagi Grimberg 	if (ib_post_recv(isert_conn->conn_qp, &isert_conn->beacon, &bad_wr)) {
327724f412ddSSagi Grimberg 		isert_err("conn %p failed to post beacon", isert_conn);
3278bdf20e72SSagi Grimberg 		return;
3279bdf20e72SSagi Grimberg 	}
3280bdf20e72SSagi Grimberg 
3281bdf20e72SSagi Grimberg 	wait_for_completion(&isert_conn->conn_wait_comp_err);
3282bdf20e72SSagi Grimberg }
3283bdf20e72SSagi Grimberg 
3284defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3285b8d26b3bSNicholas Bellinger {
3286b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3287b8d26b3bSNicholas Bellinger 
32884c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
3289b8d26b3bSNicholas Bellinger 
32909d49f5e2SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3291b8d26b3bSNicholas Bellinger 	/*
3292b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
3293b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3294b8d26b3bSNicholas Bellinger 	 */
3295b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3296b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
3297b2cb9649SNicholas Bellinger 		return;
3298b2cb9649SNicholas Bellinger 	}
3299954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
3300b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
3301b2cb9649SNicholas Bellinger 
3302c7e160eeSSagi Grimberg 	isert_wait4cmds(conn);
3303bdf20e72SSagi Grimberg 	isert_wait4flush(isert_conn);
3304991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
3305954f2372SSagi Grimberg 
3306b02efbfcSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
3307b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
3308defd8848SNicholas Bellinger }
3309defd8848SNicholas Bellinger 
3310defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3311defd8848SNicholas Bellinger {
3312defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3313b8d26b3bSNicholas Bellinger 
3314b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3315b8d26b3bSNicholas Bellinger }
3316b8d26b3bSNicholas Bellinger 
3317b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3318b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3319b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3320d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3321b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3322b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3323b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3324b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3325defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3326b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3327b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3328b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3329b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3330b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3331b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3332b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3333b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3334131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3335e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3336b8d26b3bSNicholas Bellinger };
3337b8d26b3bSNicholas Bellinger 
3338b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3339b8d26b3bSNicholas Bellinger {
3340b8d26b3bSNicholas Bellinger 	int ret;
3341b8d26b3bSNicholas Bellinger 
3342631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
3343631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
3344b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
334524f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
3346b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
33476f0fae3dSSagi Grimberg 		return -ENOMEM;
3348b8d26b3bSNicholas Bellinger 	}
3349b8d26b3bSNicholas Bellinger 
3350b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3351b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
3352b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
335324f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
3354b02efbfcSSagi Grimberg 		ret = -ENOMEM;
3355b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
3356b02efbfcSSagi Grimberg 	}
3357b02efbfcSSagi Grimberg 
3358b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
335924f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3360b02efbfcSSagi Grimberg 
3361b8d26b3bSNicholas Bellinger 	return 0;
3362b8d26b3bSNicholas Bellinger 
3363b02efbfcSSagi Grimberg destroy_comp_wq:
3364b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
33656f0fae3dSSagi Grimberg 
3366b8d26b3bSNicholas Bellinger 	return ret;
3367b8d26b3bSNicholas Bellinger }
3368b8d26b3bSNicholas Bellinger 
3369b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3370b8d26b3bSNicholas Bellinger {
3371f5ebec96SSagi Grimberg 	flush_scheduled_work();
3372b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
3373b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3374b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
33754c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
3376b8d26b3bSNicholas Bellinger }
3377b8d26b3bSNicholas Bellinger 
3378b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3379b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
3380b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3381b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3382b8d26b3bSNicholas Bellinger 
3383b8d26b3bSNicholas Bellinger module_init(isert_init);
3384b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3385