xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision b44a2b6790b04621c14de176757a09193699bc37)
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 {
225b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->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 ib_device *ib_dev = isert_conn->conn_cm_id->device;
723b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_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);
73119e2090fSSagi Grimberg 	rdma_destroy_id(isert_conn->conn_cm_id);
73219e2090fSSagi Grimberg 
733b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
7344a295baeSSagi Grimberg 		struct isert_comp *comp = isert_conn->conn_qp->recv_cq->cq_context;
7354a295baeSSagi Grimberg 
73624f412ddSSagi Grimberg 		isert_dbg("dec completion context %p active_qps\n", comp);
73719e2090fSSagi Grimberg 		mutex_lock(&device_list_mutex);
7384a295baeSSagi Grimberg 		comp->active_qps--;
73919e2090fSSagi Grimberg 		mutex_unlock(&device_list_mutex);
740b8d26b3bSNicholas Bellinger 
74119e2090fSSagi Grimberg 		ib_destroy_qp(isert_conn->conn_qp);
742b8d26b3bSNicholas Bellinger 	}
743b8d26b3bSNicholas Bellinger 
744eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
745eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
746eb6ab132SSagi Grimberg 
747b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
748b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
749b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
750b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
751b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
752b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
753b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
754b8d26b3bSNicholas Bellinger 	}
755b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
756b8d26b3bSNicholas Bellinger 
757b8d26b3bSNicholas Bellinger 	if (device)
758b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
759b8d26b3bSNicholas Bellinger }
760b8d26b3bSNicholas Bellinger 
761b8d26b3bSNicholas Bellinger static void
762b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
763b8d26b3bSNicholas Bellinger {
76419e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
765c2f88b17SSagi Grimberg 
76624f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
767128e9cc8SSagi Grimberg 
7682371e5daSSagi Grimberg 	if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
76924f412ddSSagi Grimberg 		isert_warn("conn %p connect_release is running\n", isert_conn);
7702371e5daSSagi Grimberg 		return;
7712371e5daSSagi Grimberg 	}
7722371e5daSSagi Grimberg 
7732371e5daSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
7742371e5daSSagi Grimberg 	if (isert_conn->state != ISER_CONN_FULL_FEATURE)
775128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_UP;
7762371e5daSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
777b8d26b3bSNicholas Bellinger }
778b8d26b3bSNicholas Bellinger 
779b8d26b3bSNicholas Bellinger static void
780b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
781b8d26b3bSNicholas Bellinger {
782b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
783b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
784b8d26b3bSNicholas Bellinger 
7854c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
7864c22e07fSSagi Grimberg 		   current->pid);
787b8d26b3bSNicholas Bellinger 
788b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
789b8d26b3bSNicholas Bellinger }
790b8d26b3bSNicholas Bellinger 
791b8d26b3bSNicholas Bellinger static void
792b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
793b8d26b3bSNicholas Bellinger {
794b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
795b8d26b3bSNicholas Bellinger }
796b8d26b3bSNicholas Bellinger 
797954f2372SSagi Grimberg /**
798954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
799954f2372SSagi Grimberg  * @isert_conn: isert connection struct
800954f2372SSagi Grimberg  *
801954f2372SSagi Grimberg  * Notes:
802128e9cc8SSagi Grimberg  * In case the connection state is FULL_FEATURE, move state
803954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
804128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
805954f2372SSagi Grimberg  *
806954f2372SSagi Grimberg  * This routine must be called with conn_mutex held. Thus it is
807954f2372SSagi Grimberg  * safe to call multiple times.
808954f2372SSagi Grimberg  */
809954f2372SSagi Grimberg static void
810954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
811954f2372SSagi Grimberg {
812954f2372SSagi Grimberg 	int err;
813954f2372SSagi Grimberg 
814128e9cc8SSagi Grimberg 	switch (isert_conn->state) {
815128e9cc8SSagi Grimberg 	case ISER_CONN_TERMINATING:
816128e9cc8SSagi Grimberg 		break;
817128e9cc8SSagi Grimberg 	case ISER_CONN_UP:
818128e9cc8SSagi Grimberg 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
81924f412ddSSagi Grimberg 		isert_info("Terminating conn %p state %d\n",
820954f2372SSagi Grimberg 			   isert_conn, isert_conn->state);
821128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_TERMINATING;
822954f2372SSagi Grimberg 		err = rdma_disconnect(isert_conn->conn_cm_id);
823954f2372SSagi Grimberg 		if (err)
82424f412ddSSagi Grimberg 			isert_warn("Failed rdma_disconnect isert_conn %p\n",
825954f2372SSagi Grimberg 				   isert_conn);
826128e9cc8SSagi Grimberg 		break;
827128e9cc8SSagi Grimberg 	default:
82824f412ddSSagi Grimberg 		isert_warn("conn %p teminating in state %d\n",
829128e9cc8SSagi Grimberg 			   isert_conn, isert_conn->state);
830954f2372SSagi Grimberg 	}
831954f2372SSagi Grimberg }
832954f2372SSagi Grimberg 
8333b726ae2SSagi Grimberg static int
834ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
835ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
836b8d26b3bSNicholas Bellinger {
83724f412ddSSagi Grimberg 	isert_dbg("isert np %p, handling event %d\n", isert_np, event);
8383b726ae2SSagi Grimberg 
839ca6c1d82SSagi Grimberg 	switch (event) {
840ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
8413b726ae2SSagi Grimberg 		isert_np->np_cm_id = NULL;
842ca6c1d82SSagi Grimberg 		break;
843ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
844ca6c1d82SSagi Grimberg 		isert_np->np_cm_id = isert_setup_id(isert_np);
845ca6c1d82SSagi Grimberg 		if (IS_ERR(isert_np->np_cm_id)) {
84624f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
847ca6c1d82SSagi Grimberg 				  isert_np, PTR_ERR(isert_np->np_cm_id));
848ca6c1d82SSagi Grimberg 			isert_np->np_cm_id = NULL;
849ca6c1d82SSagi Grimberg 		}
850ca6c1d82SSagi Grimberg 		break;
851ca6c1d82SSagi Grimberg 	default:
85224f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
853ca6c1d82SSagi Grimberg 			  isert_np, event);
854ca6c1d82SSagi Grimberg 	}
855ca6c1d82SSagi Grimberg 
8563b726ae2SSagi Grimberg 	return -1;
8573b726ae2SSagi Grimberg }
8583b726ae2SSagi Grimberg 
859ca6c1d82SSagi Grimberg static int
860ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
861ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
862ca6c1d82SSagi Grimberg {
863ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
864ca6c1d82SSagi Grimberg 	struct isert_conn *isert_conn;
865ca6c1d82SSagi Grimberg 
866ca6c1d82SSagi Grimberg 	if (isert_np->np_cm_id == cma_id)
867ca6c1d82SSagi Grimberg 		return isert_np_cma_handler(cma_id->context, event);
868ca6c1d82SSagi Grimberg 
86919e2090fSSagi Grimberg 	isert_conn = cma_id->qp->qp_context;
870b8d26b3bSNicholas Bellinger 
871128e9cc8SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
872128e9cc8SSagi Grimberg 	isert_conn_terminate(isert_conn);
873128e9cc8SSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
874128e9cc8SSagi Grimberg 
87524f412ddSSagi Grimberg 	isert_info("conn %p completing conn_wait\n", isert_conn);
876128e9cc8SSagi Grimberg 	complete(&isert_conn->conn_wait);
8773b726ae2SSagi Grimberg 
8783b726ae2SSagi Grimberg 	return 0;
879b8d26b3bSNicholas Bellinger }
880b8d26b3bSNicholas Bellinger 
881954f2372SSagi Grimberg static void
882954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
883954f2372SSagi Grimberg {
88419e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
885954f2372SSagi Grimberg 
886954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
887954f2372SSagi Grimberg }
888954f2372SSagi Grimberg 
889b8d26b3bSNicholas Bellinger static int
890b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
891b8d26b3bSNicholas Bellinger {
892b8d26b3bSNicholas Bellinger 	int ret = 0;
893b8d26b3bSNicholas Bellinger 
8944c22e07fSSagi Grimberg 	isert_info("event %d status %d id %p np %p\n", event->event,
8954c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
896b8d26b3bSNicholas Bellinger 
897b8d26b3bSNicholas Bellinger 	switch (event->event) {
898b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
899b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
9003b726ae2SSagi Grimberg 		if (ret)
9014c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
902b8d26b3bSNicholas Bellinger 		break;
903b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
904b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
905b8d26b3bSNicholas Bellinger 		break;
90688c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
90788c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
90888c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
90988c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
910ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
911b8d26b3bSNicholas Bellinger 		break;
912954f2372SSagi Grimberg 	case RDMA_CM_EVENT_REJECTED:       /* FALLTHRU */
913954f2372SSagi Grimberg 	case RDMA_CM_EVENT_UNREACHABLE:    /* FALLTHRU */
914b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
915954f2372SSagi Grimberg 		isert_connect_error(cma_id);
916954f2372SSagi Grimberg 		break;
917b8d26b3bSNicholas Bellinger 	default:
91824f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
919b8d26b3bSNicholas Bellinger 		break;
920b8d26b3bSNicholas Bellinger 	}
921b8d26b3bSNicholas Bellinger 
922b8d26b3bSNicholas Bellinger 	return ret;
923b8d26b3bSNicholas Bellinger }
924b8d26b3bSNicholas Bellinger 
925b8d26b3bSNicholas Bellinger static int
926b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
927b8d26b3bSNicholas Bellinger {
928b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
929b8d26b3bSNicholas Bellinger 	int i, ret;
930b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
931b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
932b8d26b3bSNicholas Bellinger 
933b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
934b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
935b0a191e7SSagi Grimberg 		rx_wr->wr_id	= (uintptr_t)rx_desc;
936b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
937b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
938b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
939b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
940b8d26b3bSNicholas Bellinger 	}
941b8d26b3bSNicholas Bellinger 
942b8d26b3bSNicholas Bellinger 	rx_wr--;
943b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
944b8d26b3bSNicholas Bellinger 
945b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
946b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
947b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
948b8d26b3bSNicholas Bellinger 	if (ret) {
94924f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
950b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
951b8d26b3bSNicholas Bellinger 	} else {
95224f412ddSSagi Grimberg 		isert_dbg("isert_post_recv(): Posted %d RX buffers\n", count);
953b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
954b8d26b3bSNicholas Bellinger 	}
955b8d26b3bSNicholas Bellinger 	return ret;
956b8d26b3bSNicholas Bellinger }
957b8d26b3bSNicholas Bellinger 
958b8d26b3bSNicholas Bellinger static int
959b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
960b8d26b3bSNicholas Bellinger {
961b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
962b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
963b8d26b3bSNicholas Bellinger 	int ret;
964b8d26b3bSNicholas Bellinger 
965b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
966b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
967b8d26b3bSNicholas Bellinger 
968b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
969b0a191e7SSagi Grimberg 	send_wr.wr_id	= (uintptr_t)tx_desc;
970b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
971b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
972b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
973b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
974b8d26b3bSNicholas Bellinger 
975b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
976bdf20e72SSagi Grimberg 	if (ret)
97724f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
978b8d26b3bSNicholas Bellinger 
979b8d26b3bSNicholas Bellinger 	return ret;
980b8d26b3bSNicholas Bellinger }
981b8d26b3bSNicholas Bellinger 
982b8d26b3bSNicholas Bellinger static void
983b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
984b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
985b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
986b8d26b3bSNicholas Bellinger {
987b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
988b8d26b3bSNicholas Bellinger 
989b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
990b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
991b8d26b3bSNicholas Bellinger 
992b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
993b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
994b8d26b3bSNicholas Bellinger 
995b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
996b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
997b8d26b3bSNicholas Bellinger 
998b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
999b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
100024f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1001b8d26b3bSNicholas Bellinger 	}
1002b8d26b3bSNicholas Bellinger }
1003b8d26b3bSNicholas Bellinger 
1004b8d26b3bSNicholas Bellinger static int
1005b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
1006b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
1007b8d26b3bSNicholas Bellinger {
1008b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1009b8d26b3bSNicholas Bellinger 	u64 dma_addr;
1010b8d26b3bSNicholas Bellinger 
1011b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1012b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
1013b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
101424f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
1015b8d26b3bSNicholas Bellinger 		return -ENOMEM;
1016b8d26b3bSNicholas Bellinger 	}
1017b8d26b3bSNicholas Bellinger 
1018b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
1019b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
1020b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1021b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1022b8d26b3bSNicholas Bellinger 
10234c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
10244c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
10254c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
1026b8d26b3bSNicholas Bellinger 
1027b8d26b3bSNicholas Bellinger 	return 0;
1028b8d26b3bSNicholas Bellinger }
1029b8d26b3bSNicholas Bellinger 
1030b8d26b3bSNicholas Bellinger static void
103195b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
103268a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
1033b8d26b3bSNicholas Bellinger {
103495b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
103595b60f07SNicholas Bellinger 
1036b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1037b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
1038b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
103995b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
1040b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
104195b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
1042b8d26b3bSNicholas Bellinger }
1043b8d26b3bSNicholas Bellinger 
1044b8d26b3bSNicholas Bellinger static int
1045b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
1046b8d26b3bSNicholas Bellinger {
1047b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
1048b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
1049b8d26b3bSNicholas Bellinger 	int ret;
1050b8d26b3bSNicholas Bellinger 
1051b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
1052b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
1053b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
1054b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
1055b8d26b3bSNicholas Bellinger 
105624f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
1057b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
1058b8d26b3bSNicholas Bellinger 
1059b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1060b0a191e7SSagi Grimberg 	rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
1061b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1062b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1063b8d26b3bSNicholas Bellinger 
1064b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1065b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1066b8d26b3bSNicholas Bellinger 	if (ret) {
106724f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
1068b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1069b8d26b3bSNicholas Bellinger 	}
1070b8d26b3bSNicholas Bellinger 
1071b8d26b3bSNicholas Bellinger 	return ret;
1072b8d26b3bSNicholas Bellinger }
1073b8d26b3bSNicholas Bellinger 
1074b8d26b3bSNicholas Bellinger static int
1075b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1076b8d26b3bSNicholas Bellinger 		   u32 length)
1077b8d26b3bSNicholas Bellinger {
1078b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1079b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1080b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1081b8d26b3bSNicholas Bellinger 	int ret;
1082b8d26b3bSNicholas Bellinger 
1083b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1084b8d26b3bSNicholas Bellinger 
1085b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1086b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1087b8d26b3bSNicholas Bellinger 
1088b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1089b8d26b3bSNicholas Bellinger 
1090b8d26b3bSNicholas Bellinger 	if (length > 0) {
1091b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1092b8d26b3bSNicholas Bellinger 
1093b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1094b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1095b8d26b3bSNicholas Bellinger 
1096b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1097b8d26b3bSNicholas Bellinger 
1098b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1099b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1100b8d26b3bSNicholas Bellinger 
1101b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1102b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
1103b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1104b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1105b8d26b3bSNicholas Bellinger 	}
1106b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1107b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1108e0546fc1SSagi Grimberg 			if (!conn->sess->sess_ops->SessionType &&
1109e0546fc1SSagi Grimberg 			    isert_conn->conn_device->use_fastreg) {
1110570db170SSagi Grimberg 				ret = isert_conn_create_fastreg_pool(isert_conn);
1111f46d6a8aSNicholas Bellinger 				if (ret) {
111224f412ddSSagi Grimberg 					isert_err("Conn: %p failed to create"
1113f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1114f46d6a8aSNicholas Bellinger 					return ret;
1115f46d6a8aSNicholas Bellinger 				}
1116f46d6a8aSNicholas Bellinger 			}
1117f46d6a8aSNicholas Bellinger 
1118b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1119b8d26b3bSNicholas Bellinger 			if (ret)
1120b8d26b3bSNicholas Bellinger 				return ret;
1121b8d26b3bSNicholas Bellinger 
1122b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1123b8d26b3bSNicholas Bellinger 			if (ret)
1124b8d26b3bSNicholas Bellinger 				return ret;
1125b8d26b3bSNicholas Bellinger 
1126128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
11272371e5daSSagi Grimberg 			mutex_lock(&isert_conn->conn_mutex);
1128128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
11292371e5daSSagi Grimberg 			mutex_unlock(&isert_conn->conn_mutex);
1130b8d26b3bSNicholas Bellinger 			goto post_send;
1131b8d26b3bSNicholas Bellinger 		}
1132b8d26b3bSNicholas Bellinger 
1133b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1134b8d26b3bSNicholas Bellinger 		if (ret)
1135b8d26b3bSNicholas Bellinger 			return ret;
1136b8d26b3bSNicholas Bellinger 	}
1137b8d26b3bSNicholas Bellinger post_send:
1138b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1139b8d26b3bSNicholas Bellinger 	if (ret)
1140b8d26b3bSNicholas Bellinger 		return ret;
1141b8d26b3bSNicholas Bellinger 
1142b8d26b3bSNicholas Bellinger 	return 0;
1143b8d26b3bSNicholas Bellinger }
1144b8d26b3bSNicholas Bellinger 
1145b8d26b3bSNicholas Bellinger static void
11462371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1147b8d26b3bSNicholas Bellinger {
11482371e5daSSagi Grimberg 	struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
11492371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1150b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1151b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1152b8d26b3bSNicholas Bellinger 	int size;
1153b8d26b3bSNicholas Bellinger 
115424f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
11552371e5daSSagi Grimberg 
11562371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1157b8d26b3bSNicholas Bellinger 
1158b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1159b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1160b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1161b8d26b3bSNicholas Bellinger 		/*
1162b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1163b8d26b3bSNicholas Bellinger 		 * login request PDU.
1164b8d26b3bSNicholas Bellinger 		 */
1165b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1166b8d26b3bSNicholas Bellinger 		login->current_stage =
1167b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1168b8d26b3bSNicholas Bellinger 			 >> 2;
1169b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1170b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1171b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1172b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1173b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1174b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1175b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1176b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1177b8d26b3bSNicholas Bellinger 	}
1178b8d26b3bSNicholas Bellinger 
1179b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1180b8d26b3bSNicholas Bellinger 
1181b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
11824c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
11834c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
11844c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
1185b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1186b8d26b3bSNicholas Bellinger 
11876faaa85fSNicholas Bellinger 	if (login->first_request) {
1188b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
11896faaa85fSNicholas Bellinger 		return;
11906faaa85fSNicholas Bellinger 	}
11916faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1192b8d26b3bSNicholas Bellinger }
1193b8d26b3bSNicholas Bellinger 
1194b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1195676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1196b8d26b3bSNicholas Bellinger {
1197b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1198b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1199d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1200b8d26b3bSNicholas Bellinger 
1201676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1202d703ce2fSNicholas Bellinger 	if (!cmd) {
120324f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1204b8d26b3bSNicholas Bellinger 		return NULL;
1205b8d26b3bSNicholas Bellinger 	}
1206d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1207b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1208d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1209b8d26b3bSNicholas Bellinger 
1210d703ce2fSNicholas Bellinger 	return cmd;
1211b8d26b3bSNicholas Bellinger }
1212b8d26b3bSNicholas Bellinger 
1213b8d26b3bSNicholas Bellinger static int
1214b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1215d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1216d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1217b8d26b3bSNicholas Bellinger {
1218b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1219b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1220b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1221b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1222b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1223b8d26b3bSNicholas Bellinger 
1224b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1225b8d26b3bSNicholas Bellinger 	if (rc < 0)
1226b8d26b3bSNicholas Bellinger 		return rc;
1227b8d26b3bSNicholas Bellinger 
1228b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1229b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1230b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1231b8d26b3bSNicholas Bellinger 
1232b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1233b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1234b8d26b3bSNicholas Bellinger 		return 0;
1235b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1236b8d26b3bSNicholas Bellinger 		dump_payload = true;
1237b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1238b8d26b3bSNicholas Bellinger 	}
1239b8d26b3bSNicholas Bellinger 
1240b8d26b3bSNicholas Bellinger 	if (!imm_data)
1241b8d26b3bSNicholas Bellinger 		return 0;
1242b8d26b3bSNicholas Bellinger 
1243b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1244b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1245b8d26b3bSNicholas Bellinger 
124624f412ddSSagi Grimberg 	isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1247b8d26b3bSNicholas Bellinger 		  sg, sg_nents, &rx_desc->data[0], imm_data_len);
1248b8d26b3bSNicholas Bellinger 
1249b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1250b8d26b3bSNicholas Bellinger 
1251b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1252b8d26b3bSNicholas Bellinger 
1253b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1254b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1255b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1256b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1257b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1258b8d26b3bSNicholas Bellinger 	}
1259b8d26b3bSNicholas Bellinger 
1260b8d26b3bSNicholas Bellinger sequence_cmd:
1261561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1262b8d26b3bSNicholas Bellinger 
1263b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1264b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
12656cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
12666cc44a6fSNicholas Bellinger 		target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1267b8d26b3bSNicholas Bellinger 
1268b8d26b3bSNicholas Bellinger 	return 0;
1269b8d26b3bSNicholas Bellinger }
1270b8d26b3bSNicholas Bellinger 
1271b8d26b3bSNicholas Bellinger static int
1272b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1273b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1274b8d26b3bSNicholas Bellinger {
1275b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1276b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1277b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1278b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1279b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1280b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1281b8d26b3bSNicholas Bellinger 
1282b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1283b8d26b3bSNicholas Bellinger 	if (rc < 0)
1284b8d26b3bSNicholas Bellinger 		return rc;
1285b8d26b3bSNicholas Bellinger 	else if (!cmd)
1286b8d26b3bSNicholas Bellinger 		return 0;
1287b8d26b3bSNicholas Bellinger 	/*
1288b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1289b8d26b3bSNicholas Bellinger 	 */
1290b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
129124f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1292b8d26b3bSNicholas Bellinger 		dump_stack();
1293b8d26b3bSNicholas Bellinger 		return -1;
1294b8d26b3bSNicholas Bellinger 	}
1295b8d26b3bSNicholas Bellinger 
12964c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
12974c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
12984c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
12994c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1300b8d26b3bSNicholas Bellinger 
1301b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1302b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1303b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1304b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1305b8d26b3bSNicholas Bellinger 	/*
1306b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1307b8d26b3bSNicholas Bellinger 	 */
1308b8d26b3bSNicholas Bellinger 	if (page_off) {
13094c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1310b8d26b3bSNicholas Bellinger 		dump_stack();
1311b8d26b3bSNicholas Bellinger 		return -1;
1312b8d26b3bSNicholas Bellinger 	}
13134c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
13144c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
13154c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1316b8d26b3bSNicholas Bellinger 
1317b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1318b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1319b8d26b3bSNicholas Bellinger 
1320b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1321b8d26b3bSNicholas Bellinger 	if (rc < 0)
1322b8d26b3bSNicholas Bellinger 		return rc;
1323b8d26b3bSNicholas Bellinger 
1324b8d26b3bSNicholas Bellinger 	return 0;
1325b8d26b3bSNicholas Bellinger }
1326b8d26b3bSNicholas Bellinger 
1327b8d26b3bSNicholas Bellinger static int
1328778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1329d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1330d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1331778de368SNicholas Bellinger {
1332778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1333778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1334778de368SNicholas Bellinger 	int rc;
1335778de368SNicholas Bellinger 
1336778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1337778de368SNicholas Bellinger 	if (rc < 0)
1338778de368SNicholas Bellinger 		return rc;
1339778de368SNicholas Bellinger 	/*
1340778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1341778de368SNicholas Bellinger 	 */
1342778de368SNicholas Bellinger 
1343778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1344778de368SNicholas Bellinger }
1345778de368SNicholas Bellinger 
1346778de368SNicholas Bellinger static int
1347adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1348d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1349d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1350adb54c29SNicholas Bellinger {
1351adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1352adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1353adb54c29SNicholas Bellinger 	int rc;
1354*b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1355adb54c29SNicholas Bellinger 
1356adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1357adb54c29SNicholas Bellinger 	if (rc < 0)
1358adb54c29SNicholas Bellinger 		return rc;
1359adb54c29SNicholas Bellinger 
1360*b44a2b67SSagi Grimberg 	if (payload_length) {
1361adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
1362adb54c29SNicholas Bellinger 		if (!text_in) {
136324f412ddSSagi Grimberg 			isert_err("Unable to allocate text_in of payload_length: %u\n",
1364adb54c29SNicholas Bellinger 				  payload_length);
1365adb54c29SNicholas Bellinger 			return -ENOMEM;
1366adb54c29SNicholas Bellinger 		}
1367*b44a2b67SSagi Grimberg 	}
1368adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1369adb54c29SNicholas Bellinger 
1370adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1371adb54c29SNicholas Bellinger 
1372adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1373adb54c29SNicholas Bellinger }
1374adb54c29SNicholas Bellinger 
1375adb54c29SNicholas Bellinger static int
1376b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1377b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1378b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1379b8d26b3bSNicholas Bellinger {
1380b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1381b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1382ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1383b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1384b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1385b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1386b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1387b8d26b3bSNicholas Bellinger 
1388ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1389ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
139024f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1391ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1392ca40d24eSNicholas Bellinger 		return 0;
1393ca40d24eSNicholas Bellinger 	}
1394ca40d24eSNicholas Bellinger 
1395b8d26b3bSNicholas Bellinger 	switch (opcode) {
1396b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1397676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1398b8d26b3bSNicholas Bellinger 		if (!cmd)
1399b8d26b3bSNicholas Bellinger 			break;
1400b8d26b3bSNicholas Bellinger 
1401d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1402b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1403b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1404b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1405b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1406b8d26b3bSNicholas Bellinger 
1407d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1408b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1409b8d26b3bSNicholas Bellinger 		break;
1410b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1411676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1412b8d26b3bSNicholas Bellinger 		if (!cmd)
1413b8d26b3bSNicholas Bellinger 			break;
1414b8d26b3bSNicholas Bellinger 
1415d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1416d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1417778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1418b8d26b3bSNicholas Bellinger 		break;
1419b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1420b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1421b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1422b8d26b3bSNicholas Bellinger 		break;
1423b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1424676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1425b8d26b3bSNicholas Bellinger 		if (!cmd)
1426b8d26b3bSNicholas Bellinger 			break;
1427b8d26b3bSNicholas Bellinger 
1428b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1429b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1430b8d26b3bSNicholas Bellinger 		break;
1431b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1432676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1433b8d26b3bSNicholas Bellinger 		if (!cmd)
1434b8d26b3bSNicholas Bellinger 			break;
1435b8d26b3bSNicholas Bellinger 
1436b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1437b8d26b3bSNicholas Bellinger 		break;
1438adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1439676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1440adb54c29SNicholas Bellinger 		if (!cmd)
1441adb54c29SNicholas Bellinger 			break;
1442adb54c29SNicholas Bellinger 
1443d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1444d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1445adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1446adb54c29SNicholas Bellinger 		break;
1447b8d26b3bSNicholas Bellinger 	default:
144824f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1449b8d26b3bSNicholas Bellinger 		dump_stack();
1450b8d26b3bSNicholas Bellinger 		break;
1451b8d26b3bSNicholas Bellinger 	}
1452b8d26b3bSNicholas Bellinger 
1453b8d26b3bSNicholas Bellinger 	return ret;
1454b8d26b3bSNicholas Bellinger }
1455b8d26b3bSNicholas Bellinger 
1456b8d26b3bSNicholas Bellinger static void
1457b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1458b8d26b3bSNicholas Bellinger {
1459b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1460b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1461b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1462b8d26b3bSNicholas Bellinger 	int rc;
1463b8d26b3bSNicholas Bellinger 
1464b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1465b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1466b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1467b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1468b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
14694c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1470b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1471b8d26b3bSNicholas Bellinger 		}
1472b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1473b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1474b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
14754c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1476b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1477b8d26b3bSNicholas Bellinger 		}
1478b8d26b3bSNicholas Bellinger 
147924f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1480b8d26b3bSNicholas Bellinger 		break;
1481b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
148224f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1483b8d26b3bSNicholas Bellinger 		break;
1484b8d26b3bSNicholas Bellinger 	default:
148524f412ddSSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1486b8d26b3bSNicholas Bellinger 		break;
1487b8d26b3bSNicholas Bellinger 	}
1488b8d26b3bSNicholas Bellinger 
1489b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1490b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1491b8d26b3bSNicholas Bellinger }
1492b8d26b3bSNicholas Bellinger 
1493b8d26b3bSNicholas Bellinger static void
1494b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
14954a295baeSSagi Grimberg 		    u32 xfer_len)
1496b8d26b3bSNicholas Bellinger {
1497b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1498b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1499b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1500b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1501b8d26b3bSNicholas Bellinger 
1502b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1503b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1504b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
15054c22e07fSSagi Grimberg 		isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1506b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1507b8d26b3bSNicholas Bellinger 	} else {
1508b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1509b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
15104c22e07fSSagi Grimberg 		isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1511b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1512b8d26b3bSNicholas Bellinger 	}
1513b8d26b3bSNicholas Bellinger 
1514b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1515b8d26b3bSNicholas Bellinger 
1516b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
151724f412ddSSagi Grimberg 	isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1518b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1519b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1520b8d26b3bSNicholas Bellinger 
15212371e5daSSagi Grimberg 	if ((char *)desc == isert_conn->login_req_buf) {
15222371e5daSSagi Grimberg 		isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
15232371e5daSSagi Grimberg 		if (isert_conn->conn) {
15242371e5daSSagi Grimberg 			struct iscsi_login *login = isert_conn->conn->conn_login;
15252371e5daSSagi Grimberg 
15262371e5daSSagi Grimberg 			if (login && !login->first_request)
15272371e5daSSagi Grimberg 				isert_rx_login_req(isert_conn);
15282371e5daSSagi Grimberg 		}
15292371e5daSSagi Grimberg 		mutex_lock(&isert_conn->conn_mutex);
15302371e5daSSagi Grimberg 		complete(&isert_conn->login_req_comp);
15312371e5daSSagi Grimberg 		mutex_unlock(&isert_conn->conn_mutex);
15322371e5daSSagi Grimberg 	} else {
1533b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
15342371e5daSSagi Grimberg 	}
1535b8d26b3bSNicholas Bellinger 
1536b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1537b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1538b8d26b3bSNicholas Bellinger 
1539b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
15404c22e07fSSagi Grimberg 	isert_dbg("Decremented post_recv_buf_count: %d\n",
1541b8d26b3bSNicholas Bellinger 		  isert_conn->post_recv_buf_count);
1542b8d26b3bSNicholas Bellinger 
1543b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1544b8d26b3bSNicholas Bellinger 		return;
1545b8d26b3bSNicholas Bellinger 
1546b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1547b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1548b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1549b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1550b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1551b8d26b3bSNicholas Bellinger 		if (err) {
155224f412ddSSagi Grimberg 			isert_err("isert_post_recv() count: %d failed, %d\n",
1553b8d26b3bSNicholas Bellinger 			       count, err);
1554b8d26b3bSNicholas Bellinger 		}
1555b8d26b3bSNicholas Bellinger 	}
1556b8d26b3bSNicholas Bellinger }
1557b8d26b3bSNicholas Bellinger 
1558e3d7e4c3SSagi Grimberg static int
1559e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1560e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1561e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1562e3d7e4c3SSagi Grimberg {
1563e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1564e3d7e4c3SSagi Grimberg 
1565e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1566e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1567e3d7e4c3SSagi Grimberg 
1568e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1569e3d7e4c3SSagi Grimberg 	data->offset = offset;
1570e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1571e3d7e4c3SSagi Grimberg 
1572e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1573e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1574e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1575e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1576e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1577e3d7e4c3SSagi Grimberg 
1578e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1579e3d7e4c3SSagi Grimberg 					data->dma_dir);
1580e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
158124f412ddSSagi Grimberg 		isert_err("Cmd: unable to dma map SGs %p\n", sg);
1582e3d7e4c3SSagi Grimberg 		return -EINVAL;
1583e3d7e4c3SSagi Grimberg 	}
1584e3d7e4c3SSagi Grimberg 
158524f412ddSSagi Grimberg 	isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1586e3d7e4c3SSagi Grimberg 		  isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1587e3d7e4c3SSagi Grimberg 
1588e3d7e4c3SSagi Grimberg 	return 0;
1589e3d7e4c3SSagi Grimberg }
1590e3d7e4c3SSagi Grimberg 
1591e3d7e4c3SSagi Grimberg static void
1592e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1593e3d7e4c3SSagi Grimberg {
1594e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1595e3d7e4c3SSagi Grimberg 
1596e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1597e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1598e3d7e4c3SSagi Grimberg }
1599e3d7e4c3SSagi Grimberg 
1600e3d7e4c3SSagi Grimberg 
1601e3d7e4c3SSagi Grimberg 
1602b8d26b3bSNicholas Bellinger static void
1603b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1604b8d26b3bSNicholas Bellinger {
1605b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1606b8d26b3bSNicholas Bellinger 
16074c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1608e3d7e4c3SSagi Grimberg 
1609e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
16104c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1611e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1612b8d26b3bSNicholas Bellinger 	}
1613b8d26b3bSNicholas Bellinger 
161490ecc6e2SVu Pham 	if (wr->send_wr) {
16154c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free send_wr\n", isert_cmd);
1616b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1617b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
161890ecc6e2SVu Pham 	}
1619b8d26b3bSNicholas Bellinger 
162090ecc6e2SVu Pham 	if (wr->ib_sge) {
16214c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
162290ecc6e2SVu Pham 		kfree(wr->ib_sge);
162390ecc6e2SVu Pham 		wr->ib_sge = NULL;
162490ecc6e2SVu Pham 	}
1625b8d26b3bSNicholas Bellinger }
1626b8d26b3bSNicholas Bellinger 
1627b8d26b3bSNicholas Bellinger static void
1628a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
162959464ef4SVu Pham {
163059464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
163159464ef4SVu Pham 	LIST_HEAD(unmap_list);
163259464ef4SVu Pham 
16334c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
163459464ef4SVu Pham 
163559464ef4SVu Pham 	if (wr->fr_desc) {
16364c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
16379e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
16389e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
16399e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
16409e961ae7SSagi Grimberg 		}
164159464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1642a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
164359464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
164459464ef4SVu Pham 		wr->fr_desc = NULL;
164559464ef4SVu Pham 	}
164659464ef4SVu Pham 
1647e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
16484c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1649e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
165059464ef4SVu Pham 	}
165159464ef4SVu Pham 
165259464ef4SVu Pham 	wr->ib_sge = NULL;
165359464ef4SVu Pham 	wr->send_wr = NULL;
165459464ef4SVu Pham }
165559464ef4SVu Pham 
165659464ef4SVu Pham static void
165703e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1658b8d26b3bSNicholas Bellinger {
1659d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1660b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1661186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1662d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1663b8d26b3bSNicholas Bellinger 
16644c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1665b8d26b3bSNicholas Bellinger 
1666b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1667b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1668b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1669b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16705159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1671b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1672b8d26b3bSNicholas Bellinger 
167303e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1674b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
167503e7848aSNicholas Bellinger 			/*
167603e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
167703e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
167803e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
167903e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
168003e7848aSNicholas Bellinger 			 */
168103e7848aSNicholas Bellinger 			if (comp_err &&
168203e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
168303e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
168403e7848aSNicholas Bellinger 
168503e7848aSNicholas Bellinger 				target_put_sess_cmd(se_cmd->se_sess, se_cmd);
168603e7848aSNicholas Bellinger 			}
168703e7848aSNicholas Bellinger 		}
1688b8d26b3bSNicholas Bellinger 
1689d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1690186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1691186a9647SNicholas Bellinger 		break;
1692b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1693186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1694186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16955159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1696186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1697186a9647SNicholas Bellinger 
1698b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1699b8d26b3bSNicholas Bellinger 		break;
1700b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1701b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1702adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1703b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1704b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17055159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1706b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1707b8d26b3bSNicholas Bellinger 
1708b8d26b3bSNicholas Bellinger 		/*
1709b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1710b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1711b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1712b8d26b3bSNicholas Bellinger 		 */
1713b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
171424f412ddSSagi Grimberg 			isert_dbg("Calling transport_generic_free_cmd from"
17153df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
17163df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1717b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1718b8d26b3bSNicholas Bellinger 			break;
1719b8d26b3bSNicholas Bellinger 		}
1720b8d26b3bSNicholas Bellinger 		/*
1721b8d26b3bSNicholas Bellinger 		 * Fall-through
1722b8d26b3bSNicholas Bellinger 		 */
1723b8d26b3bSNicholas Bellinger 	default:
1724d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1725b8d26b3bSNicholas Bellinger 		break;
1726b8d26b3bSNicholas Bellinger 	}
1727b8d26b3bSNicholas Bellinger }
1728b8d26b3bSNicholas Bellinger 
1729b8d26b3bSNicholas Bellinger static void
1730b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1731b8d26b3bSNicholas Bellinger {
1732b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
17334c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1734b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1735b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1736b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1737b8d26b3bSNicholas Bellinger 	}
1738b8d26b3bSNicholas Bellinger }
1739b8d26b3bSNicholas Bellinger 
1740b8d26b3bSNicholas Bellinger static void
1741b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
174203e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1743b8d26b3bSNicholas Bellinger {
1744dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
17454c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1746dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1747dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1748dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1749b8d26b3bSNicholas Bellinger 	}
1750b8d26b3bSNicholas Bellinger 
1751b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
175203e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1753b8d26b3bSNicholas Bellinger }
1754b8d26b3bSNicholas Bellinger 
175596b7973eSSagi Grimberg static int
175696b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1757f93f3a70SSagi Grimberg {
17589e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
175996b7973eSSagi Grimberg 	int ret;
1760f93f3a70SSagi Grimberg 
176196b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
17629e961ae7SSagi Grimberg 	if (ret) {
176324f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
17649e961ae7SSagi Grimberg 		goto fail_mr_status;
17659e961ae7SSagi Grimberg 	}
176696b7973eSSagi Grimberg 
17679e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
176896b7973eSSagi Grimberg 		u64 sec_offset_err;
17699e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
17709e961ae7SSagi Grimberg 
17719e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
17729e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
17739e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
17749e961ae7SSagi Grimberg 			break;
17759e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
17769e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
17779e961ae7SSagi Grimberg 			break;
17789e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
17799e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
17809e961ae7SSagi Grimberg 			break;
17819e961ae7SSagi Grimberg 		}
178296b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
178396b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
178496b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
17859e961ae7SSagi Grimberg 
17864c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
17879e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
17889e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
17899e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
17909e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
17919e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
17929e961ae7SSagi Grimberg 		ret = 1;
17939e961ae7SSagi Grimberg 	}
17949e961ae7SSagi Grimberg 
17959e961ae7SSagi Grimberg fail_mr_status:
179696b7973eSSagi Grimberg 	return ret;
179796b7973eSSagi Grimberg }
179896b7973eSSagi Grimberg 
179996b7973eSSagi Grimberg static void
180096b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
180196b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
180296b7973eSSagi Grimberg {
180396b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
180496b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
180596b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
180696b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
180796b7973eSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
180896b7973eSSagi Grimberg 	int ret = 0;
180996b7973eSSagi Grimberg 
181096b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
181196b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
181296b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
181396b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
181496b7973eSSagi Grimberg 	}
181596b7973eSSagi Grimberg 
1816f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1817897bb2c9SSagi Grimberg 	wr->send_wr_num = 0;
18189e961ae7SSagi Grimberg 	if (ret)
18199e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18209e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
18219e961ae7SSagi Grimberg 	else
1822f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1823f93f3a70SSagi Grimberg }
1824f93f3a70SSagi Grimberg 
1825f93f3a70SSagi Grimberg static void
1826b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1827b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1828b8d26b3bSNicholas Bellinger {
1829b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1830d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1831b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
183290ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1833d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
18345bac4b1aSSagi Grimberg 	int ret = 0;
1835b8d26b3bSNicholas Bellinger 
18369e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
183796b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
183896b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
183996b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
18409e961ae7SSagi Grimberg 	}
18419e961ae7SSagi Grimberg 
1842b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1843d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1844e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1845b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1846b8d26b3bSNicholas Bellinger 
184724f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1848b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1849b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1850b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1851b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1852b8d26b3bSNicholas Bellinger 
18535bac4b1aSSagi Grimberg 	if (ret)
18545bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18555bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
18565bac4b1aSSagi Grimberg 	else
1857b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1858b8d26b3bSNicholas Bellinger }
1859b8d26b3bSNicholas Bellinger 
1860b8d26b3bSNicholas Bellinger static void
1861b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1862b8d26b3bSNicholas Bellinger {
1863b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1864b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1865b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1866b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1867d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1868b8d26b3bSNicholas Bellinger 
18694c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
18704c22e07fSSagi Grimberg 
1871b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1872b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1873b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
187410633c37SSagi Grimberg 	case ISTATE_SEND_REJECT:   /* FALLTHRU */
187510633c37SSagi Grimberg 	case ISTATE_SEND_TEXTRSP:  /* FALLTHRU */
1876b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
18774c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
18784c22e07fSSagi Grimberg 				     ib_dev, false);
18793df8f68aSNicholas Bellinger 		break;
1880b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1881b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1882b8d26b3bSNicholas Bellinger 		break;
1883b8d26b3bSNicholas Bellinger 	default:
18844c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1885b8d26b3bSNicholas Bellinger 		dump_stack();
1886b8d26b3bSNicholas Bellinger 		break;
1887b8d26b3bSNicholas Bellinger 	}
1888b8d26b3bSNicholas Bellinger }
1889b8d26b3bSNicholas Bellinger 
1890b8d26b3bSNicholas Bellinger static void
1891b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1892b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1893b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1894b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1895b8d26b3bSNicholas Bellinger {
1896d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1897b8d26b3bSNicholas Bellinger 
1898b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
18993df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1900adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1901adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1902b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1903b8d26b3bSNicholas Bellinger 
1904b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1905b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1906b8d26b3bSNicholas Bellinger 		return;
1907b8d26b3bSNicholas Bellinger 	}
1908897bb2c9SSagi Grimberg 
1909b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
191003e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1911b8d26b3bSNicholas Bellinger }
1912b8d26b3bSNicholas Bellinger 
1913b8d26b3bSNicholas Bellinger static void
191468a86deeSSagi Grimberg isert_send_completion(struct iser_tx_desc *tx_desc,
1915b8d26b3bSNicholas Bellinger 		      struct isert_conn *isert_conn)
1916b8d26b3bSNicholas Bellinger {
1917b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1918b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1919b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1920b8d26b3bSNicholas Bellinger 
1921b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1922b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1923b8d26b3bSNicholas Bellinger 		return;
1924b8d26b3bSNicholas Bellinger 	}
1925b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1926b8d26b3bSNicholas Bellinger 
19274c22e07fSSagi Grimberg 	isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
19284c22e07fSSagi Grimberg 
1929b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1930b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
19314c22e07fSSagi Grimberg 		isert_err("Got ISER_IB_RECV\n");
1932b8d26b3bSNicholas Bellinger 		dump_stack();
1933b8d26b3bSNicholas Bellinger 		break;
1934b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1935b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1936b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1937b8d26b3bSNicholas Bellinger 		break;
1938b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1939f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
1940b8d26b3bSNicholas Bellinger 		break;
1941b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1942b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1943b8d26b3bSNicholas Bellinger 		break;
1944b8d26b3bSNicholas Bellinger 	default:
19454c22e07fSSagi Grimberg 		isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
1946b8d26b3bSNicholas Bellinger 		dump_stack();
1947b8d26b3bSNicholas Bellinger 		break;
1948b8d26b3bSNicholas Bellinger 	}
1949b8d26b3bSNicholas Bellinger }
1950b8d26b3bSNicholas Bellinger 
19516f0fae3dSSagi Grimberg /**
19526f0fae3dSSagi Grimberg  * is_isert_tx_desc() - Indicate if the completion wr_id
19536f0fae3dSSagi Grimberg  *     is a TX descriptor or not.
19546f0fae3dSSagi Grimberg  * @isert_conn: iser connection
19556f0fae3dSSagi Grimberg  * @wr_id: completion WR identifier
19566f0fae3dSSagi Grimberg  *
19576f0fae3dSSagi Grimberg  * Since we cannot rely on wc opcode in FLUSH errors
19586f0fae3dSSagi Grimberg  * we must work around it by checking if the wr_id address
19596f0fae3dSSagi Grimberg  * falls in the iser connection rx_descs buffer. If so
19606f0fae3dSSagi Grimberg  * it is an RX descriptor, otherwize it is a TX.
19616f0fae3dSSagi Grimberg  */
19626f0fae3dSSagi Grimberg static inline bool
19636f0fae3dSSagi Grimberg is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
1964b8d26b3bSNicholas Bellinger {
19656f0fae3dSSagi Grimberg 	void *start = isert_conn->conn_rx_descs;
19666f0fae3dSSagi Grimberg 	int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->conn_rx_descs);
19676f0fae3dSSagi Grimberg 
19686f0fae3dSSagi Grimberg 	if (wr_id >= start && wr_id < start + len)
19696f0fae3dSSagi Grimberg 		return false;
19706f0fae3dSSagi Grimberg 
19716f0fae3dSSagi Grimberg 	return true;
19726f0fae3dSSagi Grimberg }
19736f0fae3dSSagi Grimberg 
19746f0fae3dSSagi Grimberg static void
19756f0fae3dSSagi Grimberg isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
19766f0fae3dSSagi Grimberg {
1977bdf20e72SSagi Grimberg 	if (wc->wr_id == ISER_BEACON_WRID) {
197824f412ddSSagi Grimberg 		isert_info("conn %p completing conn_wait_comp_err\n",
1979bdf20e72SSagi Grimberg 			   isert_conn);
1980bdf20e72SSagi Grimberg 		complete(&isert_conn->conn_wait_comp_err);
1981ed4520aeSNicholas Bellinger 	} else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
1982b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1983df43debdSSagi Grimberg 		struct isert_cmd *isert_cmd;
19846f0fae3dSSagi Grimberg 		struct iser_tx_desc *desc;
1985b8d26b3bSNicholas Bellinger 
19866f0fae3dSSagi Grimberg 		desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
19876f0fae3dSSagi Grimberg 		isert_cmd = desc->isert_cmd;
1988b8d26b3bSNicholas Bellinger 		if (!isert_cmd)
1989df43debdSSagi Grimberg 			isert_unmap_tx_desc(desc, ib_dev);
1990b8d26b3bSNicholas Bellinger 		else
1991df43debdSSagi Grimberg 			isert_completion_put(desc, isert_cmd, ib_dev, true);
1992df43debdSSagi Grimberg 	} else {
1993df43debdSSagi Grimberg 		isert_conn->post_recv_buf_count--;
1994bdf20e72SSagi Grimberg 		if (!isert_conn->post_recv_buf_count)
19959d49f5e2SSagi Grimberg 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1996b8d26b3bSNicholas Bellinger 	}
1997df43debdSSagi Grimberg }
1998b8d26b3bSNicholas Bellinger 
1999b8d26b3bSNicholas Bellinger static void
20006f0fae3dSSagi Grimberg isert_handle_wc(struct ib_wc *wc)
2001b8d26b3bSNicholas Bellinger {
2002b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2003b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
20046f0fae3dSSagi Grimberg 	struct iser_rx_desc *rx_desc;
2005b8d26b3bSNicholas Bellinger 
20066f0fae3dSSagi Grimberg 	isert_conn = wc->qp->qp_context;
20076f0fae3dSSagi Grimberg 	if (likely(wc->status == IB_WC_SUCCESS)) {
20086f0fae3dSSagi Grimberg 		if (wc->opcode == IB_WC_RECV) {
20096f0fae3dSSagi Grimberg 			rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
20106f0fae3dSSagi Grimberg 			isert_rx_completion(rx_desc, isert_conn, wc->byte_len);
2011b8d26b3bSNicholas Bellinger 		} else {
20126f0fae3dSSagi Grimberg 			tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20136f0fae3dSSagi Grimberg 			isert_send_completion(tx_desc, isert_conn);
2014b8d26b3bSNicholas Bellinger 		}
20156f0fae3dSSagi Grimberg 	} else {
20166f0fae3dSSagi Grimberg 		if (wc->status != IB_WC_WR_FLUSH_ERR)
201724f412ddSSagi Grimberg 			isert_err("wr id %llx status %d vend_err %x\n",
20186f0fae3dSSagi Grimberg 				  wc->wr_id, wc->status, wc->vendor_err);
20196f0fae3dSSagi Grimberg 		else
202024f412ddSSagi Grimberg 			isert_dbg("flush error: wr id %llx\n", wc->wr_id);
2021b8d26b3bSNicholas Bellinger 
20226f0fae3dSSagi Grimberg 		if (wc->wr_id != ISER_FASTREG_LI_WRID)
20236f0fae3dSSagi Grimberg 			isert_cq_comp_err(isert_conn, wc);
20246f0fae3dSSagi Grimberg 	}
2025b8d26b3bSNicholas Bellinger }
2026b8d26b3bSNicholas Bellinger 
2027b8d26b3bSNicholas Bellinger static void
20286f0fae3dSSagi Grimberg isert_cq_work(struct work_struct *work)
2029b8d26b3bSNicholas Bellinger {
203037d9fe80SSagi Grimberg 	enum { isert_poll_budget = 65536 };
20314a295baeSSagi Grimberg 	struct isert_comp *comp = container_of(work, struct isert_comp,
20326f0fae3dSSagi Grimberg 					       work);
203336ea63b5SSagi Grimberg 	struct ib_wc *const wcs = comp->wcs;
203436ea63b5SSagi Grimberg 	int i, n, completed = 0;
2035b8d26b3bSNicholas Bellinger 
203636ea63b5SSagi Grimberg 	while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
203736ea63b5SSagi Grimberg 		for (i = 0; i < n; i++)
203836ea63b5SSagi Grimberg 			isert_handle_wc(&wcs[i]);
2039b8d26b3bSNicholas Bellinger 
204036ea63b5SSagi Grimberg 		completed += n;
204136ea63b5SSagi Grimberg 		if (completed >= isert_poll_budget)
204237d9fe80SSagi Grimberg 			break;
204337d9fe80SSagi Grimberg 	}
204437d9fe80SSagi Grimberg 
20456f0fae3dSSagi Grimberg 	ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
2046b8d26b3bSNicholas Bellinger }
2047b8d26b3bSNicholas Bellinger 
2048b8d26b3bSNicholas Bellinger static void
20496f0fae3dSSagi Grimberg isert_cq_callback(struct ib_cq *cq, void *context)
2050b8d26b3bSNicholas Bellinger {
20514a295baeSSagi Grimberg 	struct isert_comp *comp = context;
2052b8d26b3bSNicholas Bellinger 
20536f0fae3dSSagi Grimberg 	queue_work(isert_comp_wq, &comp->work);
2054b8d26b3bSNicholas Bellinger }
2055b8d26b3bSNicholas Bellinger 
2056b8d26b3bSNicholas Bellinger static int
2057b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2058b8d26b3bSNicholas Bellinger {
2059b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2060b8d26b3bSNicholas Bellinger 	int ret;
2061b8d26b3bSNicholas Bellinger 
2062b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2063b8d26b3bSNicholas Bellinger 			   &wr_failed);
2064b8d26b3bSNicholas Bellinger 	if (ret) {
206524f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
2066b8d26b3bSNicholas Bellinger 		return ret;
2067b8d26b3bSNicholas Bellinger 	}
2068b8d26b3bSNicholas Bellinger 	return ret;
2069b8d26b3bSNicholas Bellinger }
2070b8d26b3bSNicholas Bellinger 
2071b8d26b3bSNicholas Bellinger static int
2072b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2073b8d26b3bSNicholas Bellinger {
2074d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2075b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2076b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2077b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2078b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2079b8d26b3bSNicholas Bellinger 
2080b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2081b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2082b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2083b8d26b3bSNicholas Bellinger 	/*
2084b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2085b8d26b3bSNicholas Bellinger 	 */
2086b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2087b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2088b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2089b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2090b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2091dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2092b8d26b3bSNicholas Bellinger 
2093b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2094b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2095b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2096b8d26b3bSNicholas Bellinger 
2097b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2098b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2099dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2100b8d26b3bSNicholas Bellinger 
2101dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2102dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2103b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2104b8d26b3bSNicholas Bellinger 
2105dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2106dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2107dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
2108b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2109b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2110b8d26b3bSNicholas Bellinger 	}
2111b8d26b3bSNicholas Bellinger 
211268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2113b8d26b3bSNicholas Bellinger 
21144c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
2115b8d26b3bSNicholas Bellinger 
2116b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2117b8d26b3bSNicholas Bellinger }
2118b8d26b3bSNicholas Bellinger 
2119131e6abcSNicholas Bellinger static void
2120131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2121131e6abcSNicholas Bellinger {
2122131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2123131e6abcSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2124131e6abcSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2125131e6abcSNicholas Bellinger 
2126131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2127131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2128131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2129131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2130131e6abcSNicholas Bellinger 
2131131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2132131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2133131e6abcSNicholas Bellinger 
2134131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2135131e6abcSNicholas Bellinger }
2136131e6abcSNicholas Bellinger 
2137e70beee7SNicholas Bellinger static enum target_prot_op
2138e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2139e70beee7SNicholas Bellinger {
2140e70beee7SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2141e70beee7SNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2142e70beee7SNicholas Bellinger 
214323a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
214423a548eeSSagi Grimberg 		if (device->pi_capable) {
214524f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
214623a548eeSSagi Grimberg 			isert_conn->pi_support = true;
2147e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
214823a548eeSSagi Grimberg 		}
214923a548eeSSagi Grimberg 	}
215023a548eeSSagi Grimberg 
215124f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
215223a548eeSSagi Grimberg 	isert_conn->pi_support = false;
2153e70beee7SNicholas Bellinger 
2154e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2155e70beee7SNicholas Bellinger }
2156e70beee7SNicholas Bellinger 
2157b8d26b3bSNicholas Bellinger static int
2158b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2159b8d26b3bSNicholas Bellinger 		bool nopout_response)
2160b8d26b3bSNicholas Bellinger {
2161d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2162b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2163b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2164b8d26b3bSNicholas Bellinger 
2165b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2166b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2167b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2168b8d26b3bSNicholas Bellinger 			       nopout_response);
2169b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
217068a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2171b8d26b3bSNicholas Bellinger 
21724c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
2173b8d26b3bSNicholas Bellinger 
2174b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2175b8d26b3bSNicholas Bellinger }
2176b8d26b3bSNicholas Bellinger 
2177b8d26b3bSNicholas Bellinger static int
2178b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2179b8d26b3bSNicholas Bellinger {
2180d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2181b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2182b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2183b8d26b3bSNicholas Bellinger 
2184b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2185b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2186b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2187b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
218868a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2189b8d26b3bSNicholas Bellinger 
21904c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
2191b8d26b3bSNicholas Bellinger 
2192b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2193b8d26b3bSNicholas Bellinger }
2194b8d26b3bSNicholas Bellinger 
2195b8d26b3bSNicholas Bellinger static int
2196b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2197b8d26b3bSNicholas Bellinger {
2198d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2199b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2200b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2201b8d26b3bSNicholas Bellinger 
2202b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2203b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2204b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2205b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
220668a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2207b8d26b3bSNicholas Bellinger 
22084c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
2209b8d26b3bSNicholas Bellinger 
2210b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2211b8d26b3bSNicholas Bellinger }
2212b8d26b3bSNicholas Bellinger 
2213b8d26b3bSNicholas Bellinger static int
2214b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2215b8d26b3bSNicholas Bellinger {
2216d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2217b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2218b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
22193df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
22203df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
22213df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
22223df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2223b8d26b3bSNicholas Bellinger 
2224b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
22253df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2226b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
22273df8f68aSNicholas Bellinger 
22283df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2229dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
22303df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
22313df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2232dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2233dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
22343df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
22353df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
22363df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
22373df8f68aSNicholas Bellinger 
223868a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2239b8d26b3bSNicholas Bellinger 
22404c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
2241b8d26b3bSNicholas Bellinger 
2242b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2243b8d26b3bSNicholas Bellinger }
2244b8d26b3bSNicholas Bellinger 
2245b8d26b3bSNicholas Bellinger static int
2246adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2247adb54c29SNicholas Bellinger {
2248d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2249adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2250adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2251adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2252adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2253adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2254adb54c29SNicholas Bellinger 	int rc;
2255adb54c29SNicholas Bellinger 
2256adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
225722c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2258adb54c29SNicholas Bellinger 	if (rc < 0)
2259adb54c29SNicholas Bellinger 		return rc;
2260adb54c29SNicholas Bellinger 
2261adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2262adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2263adb54c29SNicholas Bellinger 
2264adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2265adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2266adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2267adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2268adb54c29SNicholas Bellinger 
2269adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2270adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2271adb54c29SNicholas Bellinger 
2272adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2273adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2274adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
2275adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2276adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2277adb54c29SNicholas Bellinger 	}
227868a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2279adb54c29SNicholas Bellinger 
2280f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
2281adb54c29SNicholas Bellinger 
2282adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2283adb54c29SNicholas Bellinger }
2284adb54c29SNicholas Bellinger 
2285adb54c29SNicholas Bellinger static int
2286b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2287b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2288b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2289b8d26b3bSNicholas Bellinger {
2290d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2291b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2292b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2293b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2294b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2295b8d26b3bSNicholas Bellinger 
2296b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2297b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2298b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2299b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2300b8d26b3bSNicholas Bellinger 
2301b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2302b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2303b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
2304b8d26b3bSNicholas Bellinger 	/*
2305b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2306b8d26b3bSNicholas Bellinger 	 */
2307b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
23084c22e07fSSagi Grimberg 		isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
23094c22e07fSSagi Grimberg 			  "page_off: %u\n",
2310b8d26b3bSNicholas Bellinger 			  (unsigned long long)tmp_sg->dma_address,
2311b8d26b3bSNicholas Bellinger 			  tmp_sg->length, page_off);
2312b8d26b3bSNicholas Bellinger 
2313b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2314b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2315b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2316b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2317b8d26b3bSNicholas Bellinger 
23184c22e07fSSagi Grimberg 		isert_dbg("RDMA ib_sge: addr: 0x%llx  length: %u lkey: %x\n",
231990ecc6e2SVu Pham 			  ib_sge->addr, ib_sge->length, ib_sge->lkey);
2320b8d26b3bSNicholas Bellinger 		page_off = 0;
2321b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2322b8d26b3bSNicholas Bellinger 		ib_sge++;
232324f412ddSSagi Grimberg 		isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
2324b8d26b3bSNicholas Bellinger 	}
2325b8d26b3bSNicholas Bellinger 
232624f412ddSSagi Grimberg 	isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2327b8d26b3bSNicholas Bellinger 		  send_wr->sg_list, send_wr->num_sge);
2328b8d26b3bSNicholas Bellinger 
2329b8d26b3bSNicholas Bellinger 	return sg_nents;
2330b8d26b3bSNicholas Bellinger }
2331b8d26b3bSNicholas Bellinger 
2332b8d26b3bSNicholas Bellinger static int
233390ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
233490ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2335b8d26b3bSNicholas Bellinger {
2336b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2337d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2338b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2339e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
234090ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2341b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2342e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2343e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2344b8d26b3bSNicholas Bellinger 
234590ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2346b8d26b3bSNicholas Bellinger 
2347e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2348e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2349e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2350e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2351e3d7e4c3SSagi Grimberg 	if (ret)
2352e3d7e4c3SSagi Grimberg 		return ret;
2353b8d26b3bSNicholas Bellinger 
2354e3d7e4c3SSagi Grimberg 	data_left = data->len;
2355e3d7e4c3SSagi Grimberg 	offset = data->offset;
2356b8d26b3bSNicholas Bellinger 
2357e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2358b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
235924f412ddSSagi Grimberg 		isert_warn("Unable to allocate ib_sge\n");
2360b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2361e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2362b8d26b3bSNicholas Bellinger 	}
236390ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2364b8d26b3bSNicholas Bellinger 
2365e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2366b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2367b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2368b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
236924f412ddSSagi Grimberg 		isert_dbg("Unable to allocate wr->send_wr\n");
2370b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2371e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2372b8d26b3bSNicholas Bellinger 	}
2373b8d26b3bSNicholas Bellinger 
2374b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2375b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2376b8d26b3bSNicholas Bellinger 
2377b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2378b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2379b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2380b8d26b3bSNicholas Bellinger 
2381b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
238290ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
238390ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2384b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2385b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
238690ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
238790ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
238890ecc6e2SVu Pham 			else
238990ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
239090ecc6e2SVu Pham 		} else {
239190ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
239290ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
239390ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
239490ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
239590ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
239690ecc6e2SVu Pham 			else
239790ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
239890ecc6e2SVu Pham 		}
2399b8d26b3bSNicholas Bellinger 
2400b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2401b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2402b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2403b8d26b3bSNicholas Bellinger 
2404b8d26b3bSNicholas Bellinger 		offset += data_len;
240590ecc6e2SVu Pham 		va_offset += data_len;
2406b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2407b8d26b3bSNicholas Bellinger 	}
240890ecc6e2SVu Pham 
240990ecc6e2SVu Pham 	return 0;
2410e3d7e4c3SSagi Grimberg unmap_cmd:
2411e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2412e3d7e4c3SSagi Grimberg 
241390ecc6e2SVu Pham 	return ret;
241490ecc6e2SVu Pham }
241590ecc6e2SVu Pham 
241690ecc6e2SVu Pham static int
241759464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
241859464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
241959464ef4SVu Pham {
242059464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
242159464ef4SVu Pham 	struct scatterlist *tmp_sg;
242259464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
242359464ef4SVu Pham 
242459464ef4SVu Pham 	n_pages = 0;
242559464ef4SVu Pham 	new_chunk = 1;
242659464ef4SVu Pham 	last_ent = sg_nents - 1;
242759464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
242859464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
242959464ef4SVu Pham 		if (new_chunk)
243059464ef4SVu Pham 			chunk_start = start_addr;
243159464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
243259464ef4SVu Pham 
24334c22e07fSSagi Grimberg 		isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
243459464ef4SVu Pham 			  i, (unsigned long long)tmp_sg->dma_address,
243559464ef4SVu Pham 			  tmp_sg->length);
243659464ef4SVu Pham 
243759464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
243859464ef4SVu Pham 			new_chunk = 0;
243959464ef4SVu Pham 			continue;
244059464ef4SVu Pham 		}
244159464ef4SVu Pham 		new_chunk = 1;
244259464ef4SVu Pham 
244359464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
244459464ef4SVu Pham 		do {
244559464ef4SVu Pham 			fr_pl[n_pages++] = page;
24464c22e07fSSagi Grimberg 			isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
244759464ef4SVu Pham 				  n_pages - 1, page);
244859464ef4SVu Pham 			page += PAGE_SIZE;
244959464ef4SVu Pham 		} while (page < end_addr);
245059464ef4SVu Pham 	}
245159464ef4SVu Pham 
245259464ef4SVu Pham 	return n_pages;
245359464ef4SVu Pham }
245459464ef4SVu Pham 
245510633c37SSagi Grimberg static inline void
245610633c37SSagi Grimberg isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
245710633c37SSagi Grimberg {
245810633c37SSagi Grimberg 	u32 rkey;
245910633c37SSagi Grimberg 
246010633c37SSagi Grimberg 	memset(inv_wr, 0, sizeof(*inv_wr));
246110633c37SSagi Grimberg 	inv_wr->wr_id = ISER_FASTREG_LI_WRID;
246210633c37SSagi Grimberg 	inv_wr->opcode = IB_WR_LOCAL_INV;
246310633c37SSagi Grimberg 	inv_wr->ex.invalidate_rkey = mr->rkey;
246410633c37SSagi Grimberg 
246510633c37SSagi Grimberg 	/* Bump the key */
246610633c37SSagi Grimberg 	rkey = ib_inc_rkey(mr->rkey);
246710633c37SSagi Grimberg 	ib_update_fast_reg_key(mr, rkey);
246810633c37SSagi Grimberg }
246910633c37SSagi Grimberg 
247059464ef4SVu Pham static int
2471e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2472e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2473e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
24749e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2475e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
247659464ef4SVu Pham {
247759464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
24789e961ae7SSagi Grimberg 	struct ib_mr *mr;
24799e961ae7SSagi Grimberg 	struct ib_fast_reg_page_list *frpl;
248059464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
248159464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
24829bd626e7SSagi Grimberg 	int ret, pagelist_len;
24839bd626e7SSagi Grimberg 	u32 page_off;
248459464ef4SVu Pham 
2485e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
2486e3d7e4c3SSagi Grimberg 		sge->lkey = isert_conn->conn_mr->lkey;
2487e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2488e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
24894c22e07fSSagi Grimberg 		isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
24904c22e07fSSagi Grimberg 			 sge->addr, sge->length, sge->lkey);
2491e3d7e4c3SSagi Grimberg 		return 0;
2492e3d7e4c3SSagi Grimberg 	}
2493e3d7e4c3SSagi Grimberg 
24949e961ae7SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID) {
24959e961ae7SSagi Grimberg 		/* Registering data buffer */
24969e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
24979e961ae7SSagi Grimberg 		frpl = fr_desc->data_frpl;
24989e961ae7SSagi Grimberg 	} else {
24999e961ae7SSagi Grimberg 		/* Registering protection buffer */
25009e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
25019e961ae7SSagi Grimberg 		frpl = fr_desc->pi_ctx->prot_frpl;
25029e961ae7SSagi Grimberg 	}
25039e961ae7SSagi Grimberg 
2504e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
250559464ef4SVu Pham 
250624f412ddSSagi Grimberg 	isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2507e3d7e4c3SSagi Grimberg 		  fr_desc, mem->nents, mem->offset);
250859464ef4SVu Pham 
2509e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
25109e961ae7SSagi Grimberg 					     &frpl->page_list[0]);
251159464ef4SVu Pham 
251210633c37SSagi Grimberg 	if (!(fr_desc->ind & ind)) {
251310633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, mr);
251459464ef4SVu Pham 		wr = &inv_wr;
251559464ef4SVu Pham 	}
251659464ef4SVu Pham 
251759464ef4SVu Pham 	/* Prepare FASTREG WR */
251859464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
25199bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
252059464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
25219e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
25229e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.page_list = frpl;
252359464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
252459464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2525e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
25269e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.rkey = mr->rkey;
252759464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
252859464ef4SVu Pham 
252959464ef4SVu Pham 	if (!wr)
253059464ef4SVu Pham 		wr = &fr_wr;
253159464ef4SVu Pham 	else
253259464ef4SVu Pham 		wr->next = &fr_wr;
253359464ef4SVu Pham 
253459464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
253559464ef4SVu Pham 	if (ret) {
253624f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
253759464ef4SVu Pham 		return ret;
253859464ef4SVu Pham 	}
25399e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
254059464ef4SVu Pham 
25419e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
25429e961ae7SSagi Grimberg 	sge->addr = frpl->page_list[0] + page_off;
2543e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
254459464ef4SVu Pham 
25454c22e07fSSagi Grimberg 	isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
25464c22e07fSSagi Grimberg 		  sge->addr, sge->length, sge->lkey);
254759464ef4SVu Pham 
254859464ef4SVu Pham 	return ret;
254959464ef4SVu Pham }
255059464ef4SVu Pham 
25513d73cf1aSSagi Grimberg static inline void
25523d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
25533d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
25549e961ae7SSagi Grimberg {
255578eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
25563d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
25573d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
25583d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
255978eda2bbSSagi Grimberg 	/*
256078eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
256178eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
256278eda2bbSSagi Grimberg 	 * from se_cmd.
256378eda2bbSSagi Grimberg 	 */
256478eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
256578eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
256678eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
256778eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
256878eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
256978eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
25703d73cf1aSSagi Grimberg };
25713d73cf1aSSagi Grimberg 
25723d73cf1aSSagi Grimberg static int
25733d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
25743d73cf1aSSagi Grimberg {
25759e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
25769e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
25779e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
257878eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
25793d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
25809e961ae7SSagi Grimberg 		break;
25819e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
25829e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
258378eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
25843d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
25859e961ae7SSagi Grimberg 		break;
25869e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
25879e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
25883d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
25893d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
25909e961ae7SSagi Grimberg 		break;
25919e961ae7SSagi Grimberg 	default:
259224f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
25939e961ae7SSagi Grimberg 		return -EINVAL;
25949e961ae7SSagi Grimberg 	}
25959e961ae7SSagi Grimberg 
25969e961ae7SSagi Grimberg 	return 0;
25979e961ae7SSagi Grimberg }
25989e961ae7SSagi Grimberg 
25999e961ae7SSagi Grimberg static inline u8
26009e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
26019e961ae7SSagi Grimberg {
26029e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
26039e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
26049e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
26059e961ae7SSagi Grimberg }
26069e961ae7SSagi Grimberg 
26079e961ae7SSagi Grimberg static int
2608570db170SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn,
2609570db170SSagi Grimberg 		 struct se_cmd *se_cmd,
2610570db170SSagi Grimberg 		 struct isert_rdma_wr *rdma_wr,
2611570db170SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc)
26129e961ae7SSagi Grimberg {
26139e961ae7SSagi Grimberg 	struct ib_send_wr sig_wr, inv_wr;
26149e961ae7SSagi Grimberg 	struct ib_send_wr *bad_wr, *wr = NULL;
26159e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
26169e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
26179e961ae7SSagi Grimberg 	int ret;
26189e961ae7SSagi Grimberg 
26199e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
26209e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
26219e961ae7SSagi Grimberg 	if (ret)
26229e961ae7SSagi Grimberg 		goto err;
26239e961ae7SSagi Grimberg 
26249e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
26259e961ae7SSagi Grimberg 
26269e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
262710633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
26289e961ae7SSagi Grimberg 		wr = &inv_wr;
26299e961ae7SSagi Grimberg 	}
26309e961ae7SSagi Grimberg 
26319e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
26329e961ae7SSagi Grimberg 	sig_wr.opcode = IB_WR_REG_SIG_MR;
2633c2caa207SSagi Grimberg 	sig_wr.wr_id = ISER_FASTREG_LI_WRID;
2634570db170SSagi Grimberg 	sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
26359e961ae7SSagi Grimberg 	sig_wr.num_sge = 1;
26369e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
26379e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
26389e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
26399e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
2640570db170SSagi Grimberg 		sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
26419e961ae7SSagi Grimberg 
26429e961ae7SSagi Grimberg 	if (!wr)
26439e961ae7SSagi Grimberg 		wr = &sig_wr;
26449e961ae7SSagi Grimberg 	else
26459e961ae7SSagi Grimberg 		wr->next = &sig_wr;
26469e961ae7SSagi Grimberg 
26479e961ae7SSagi Grimberg 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
26489e961ae7SSagi Grimberg 	if (ret) {
264924f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
26509e961ae7SSagi Grimberg 		goto err;
26519e961ae7SSagi Grimberg 	}
26529e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
26539e961ae7SSagi Grimberg 
2654570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2655570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].addr = 0;
2656570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
26579e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
26589e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
26599e961ae7SSagi Grimberg 		/*
26609e961ae7SSagi Grimberg 		 * We have protection guards on the wire
26619e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
26629e961ae7SSagi Grimberg 		 */
2663570db170SSagi Grimberg 		rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
26649e961ae7SSagi Grimberg 
266524f412ddSSagi Grimberg 	isert_dbg("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
2666570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2667570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].lkey);
26689e961ae7SSagi Grimberg err:
26699e961ae7SSagi Grimberg 	return ret;
26709e961ae7SSagi Grimberg }
26719e961ae7SSagi Grimberg 
267259464ef4SVu Pham static int
2673570db170SSagi Grimberg isert_handle_prot_cmd(struct isert_conn *isert_conn,
2674570db170SSagi Grimberg 		      struct isert_cmd *isert_cmd,
2675570db170SSagi Grimberg 		      struct isert_rdma_wr *wr)
2676570db170SSagi Grimberg {
2677570db170SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
2678570db170SSagi Grimberg 	struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2679570db170SSagi Grimberg 	int ret;
2680570db170SSagi Grimberg 
2681570db170SSagi Grimberg 	if (!wr->fr_desc->pi_ctx) {
2682570db170SSagi Grimberg 		ret = isert_create_pi_ctx(wr->fr_desc,
2683570db170SSagi Grimberg 					  device->ib_device,
2684570db170SSagi Grimberg 					  isert_conn->conn_pd);
2685570db170SSagi Grimberg 		if (ret) {
268624f412ddSSagi Grimberg 			isert_err("conn %p failed to allocate pi_ctx\n",
2687570db170SSagi Grimberg 				  isert_conn);
2688570db170SSagi Grimberg 			return ret;
2689570db170SSagi Grimberg 		}
2690570db170SSagi Grimberg 	}
2691570db170SSagi Grimberg 
2692570db170SSagi Grimberg 	if (se_cmd->t_prot_sg) {
2693570db170SSagi Grimberg 		ret = isert_map_data_buf(isert_conn, isert_cmd,
2694570db170SSagi Grimberg 					 se_cmd->t_prot_sg,
2695570db170SSagi Grimberg 					 se_cmd->t_prot_nents,
2696570db170SSagi Grimberg 					 se_cmd->prot_length,
2697570db170SSagi Grimberg 					 0, wr->iser_ib_op, &wr->prot);
2698570db170SSagi Grimberg 		if (ret) {
269924f412ddSSagi Grimberg 			isert_err("conn %p failed to map protection buffer\n",
2700570db170SSagi Grimberg 				  isert_conn);
2701570db170SSagi Grimberg 			return ret;
2702570db170SSagi Grimberg 		}
2703570db170SSagi Grimberg 
2704570db170SSagi Grimberg 		memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2705570db170SSagi Grimberg 		ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2706570db170SSagi Grimberg 					ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2707570db170SSagi Grimberg 		if (ret) {
270824f412ddSSagi Grimberg 			isert_err("conn %p failed to fast reg mr\n",
2709570db170SSagi Grimberg 				  isert_conn);
2710570db170SSagi Grimberg 			goto unmap_prot_cmd;
2711570db170SSagi Grimberg 		}
2712570db170SSagi Grimberg 	}
2713570db170SSagi Grimberg 
2714570db170SSagi Grimberg 	ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2715570db170SSagi Grimberg 	if (ret) {
271624f412ddSSagi Grimberg 		isert_err("conn %p failed to fast reg mr\n",
2717570db170SSagi Grimberg 			  isert_conn);
2718570db170SSagi Grimberg 		goto unmap_prot_cmd;
2719570db170SSagi Grimberg 	}
2720570db170SSagi Grimberg 	wr->fr_desc->ind |= ISERT_PROTECTED;
2721570db170SSagi Grimberg 
2722570db170SSagi Grimberg 	return 0;
2723570db170SSagi Grimberg 
2724570db170SSagi Grimberg unmap_prot_cmd:
2725570db170SSagi Grimberg 	if (se_cmd->t_prot_sg)
2726570db170SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2727570db170SSagi Grimberg 
2728570db170SSagi Grimberg 	return ret;
2729570db170SSagi Grimberg }
2730570db170SSagi Grimberg 
2731570db170SSagi Grimberg static int
2732a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
273359464ef4SVu Pham 	       struct isert_rdma_wr *wr)
273459464ef4SVu Pham {
273559464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
273659464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2737e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2738e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2739570db170SSagi Grimberg 	struct ib_send_wr *send_wr;
2740570db170SSagi Grimberg 	struct ib_sge *ib_sg;
2741e3d7e4c3SSagi Grimberg 	u32 offset;
2742e3d7e4c3SSagi Grimberg 	int ret = 0;
274359464ef4SVu Pham 	unsigned long flags;
274459464ef4SVu Pham 
274559464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2746e3d7e4c3SSagi Grimberg 
2747e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2748e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2749e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2750e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2751e3d7e4c3SSagi Grimberg 	if (ret)
2752e3d7e4c3SSagi Grimberg 		return ret;
2753e3d7e4c3SSagi Grimberg 
2754302cc7c3SSagi Grimberg 	if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
2755e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2756e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2757e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2758e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2759e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2760e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
276159464ef4SVu Pham 	}
276259464ef4SVu Pham 
27639e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2764570db170SSagi Grimberg 				ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
2765e3d7e4c3SSagi Grimberg 	if (ret)
2766e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
276759464ef4SVu Pham 
2768302cc7c3SSagi Grimberg 	if (isert_prot_cmd(isert_conn, se_cmd)) {
2769570db170SSagi Grimberg 		ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
27709e961ae7SSagi Grimberg 		if (ret)
27719e961ae7SSagi Grimberg 			goto unmap_cmd;
27729e961ae7SSagi Grimberg 
2773570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[SIG];
2774570db170SSagi Grimberg 	} else {
2775570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[DATA];
27769e961ae7SSagi Grimberg 	}
27779e961ae7SSagi Grimberg 
2778570db170SSagi Grimberg 	memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
2779e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
278059464ef4SVu Pham 	wr->send_wr_num = 1;
278159464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
278259464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
278359464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
278459464ef4SVu Pham 
278559464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2786e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
278759464ef4SVu Pham 	send_wr->num_sge = 1;
2788b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
278959464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
279059464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
279159464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
279259464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2793302cc7c3SSagi Grimberg 		send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
27949e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
279559464ef4SVu Pham 	} else {
279659464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
279759464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
279859464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
279959464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
280059464ef4SVu Pham 	}
280159464ef4SVu Pham 
280259464ef4SVu Pham 	return 0;
2803570db170SSagi Grimberg 
2804e3d7e4c3SSagi Grimberg unmap_cmd:
2805e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2806e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2807e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2808e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2809e3d7e4c3SSagi Grimberg 	}
2810e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
281159464ef4SVu Pham 
281259464ef4SVu Pham 	return ret;
281359464ef4SVu Pham }
281459464ef4SVu Pham 
281559464ef4SVu Pham static int
281690ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
281790ecc6e2SVu Pham {
281890ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
281959464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
282090ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
282190ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2822d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
282390ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
282490ecc6e2SVu Pham 	int rc;
282590ecc6e2SVu Pham 
282624f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
282790ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
28284c22e07fSSagi Grimberg 
282990ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2830d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
283190ecc6e2SVu Pham 	if (rc) {
283224f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
283390ecc6e2SVu Pham 		return rc;
283490ecc6e2SVu Pham 	}
283590ecc6e2SVu Pham 
2836302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd)) {
2837b8d26b3bSNicholas Bellinger 		/*
2838b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2839b8d26b3bSNicholas Bellinger 		 */
28409e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
28419e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
284204d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2843b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2844b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
284595b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
284668a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
28479e961ae7SSagi Grimberg 		isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
2848897bb2c9SSagi Grimberg 		wr->send_wr_num += 1;
28499e961ae7SSagi Grimberg 	}
2850b8d26b3bSNicholas Bellinger 
2851b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2852bdf20e72SSagi Grimberg 	if (rc)
285324f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
28549e961ae7SSagi Grimberg 
2855302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd))
285624f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
28579e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
28589e961ae7SSagi Grimberg 	else
285924f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
286090ecc6e2SVu Pham 			 isert_cmd);
2861b8d26b3bSNicholas Bellinger 
286290ecc6e2SVu Pham 	return 1;
2863b8d26b3bSNicholas Bellinger }
2864b8d26b3bSNicholas Bellinger 
2865b8d26b3bSNicholas Bellinger static int
2866b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2867b8d26b3bSNicholas Bellinger {
2868b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2869d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2870b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2871b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2872d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
287390ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
287490ecc6e2SVu Pham 	int rc;
2875b8d26b3bSNicholas Bellinger 
287624f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
287790ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2878b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2879d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
288090ecc6e2SVu Pham 	if (rc) {
288124f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
288290ecc6e2SVu Pham 		return rc;
2883b8d26b3bSNicholas Bellinger 	}
2884b8d26b3bSNicholas Bellinger 
2885b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2886bdf20e72SSagi Grimberg 	if (rc)
288724f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2888bdf20e72SSagi Grimberg 
288924f412ddSSagi Grimberg 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
289090ecc6e2SVu Pham 		 isert_cmd);
2891b8d26b3bSNicholas Bellinger 
289290ecc6e2SVu Pham 	return 0;
2893b8d26b3bSNicholas Bellinger }
2894b8d26b3bSNicholas Bellinger 
2895b8d26b3bSNicholas Bellinger static int
2896b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2897b8d26b3bSNicholas Bellinger {
2898b8d26b3bSNicholas Bellinger 	int ret;
2899b8d26b3bSNicholas Bellinger 
2900b8d26b3bSNicholas Bellinger 	switch (state) {
2901b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2902b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2903b8d26b3bSNicholas Bellinger 		break;
2904b8d26b3bSNicholas Bellinger 	default:
290524f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2906b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2907b8d26b3bSNicholas Bellinger 		break;
2908b8d26b3bSNicholas Bellinger 	}
2909b8d26b3bSNicholas Bellinger 
2910b8d26b3bSNicholas Bellinger 	return ret;
2911b8d26b3bSNicholas Bellinger }
2912b8d26b3bSNicholas Bellinger 
2913b8d26b3bSNicholas Bellinger static int
2914b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2915b8d26b3bSNicholas Bellinger {
2916991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2917b8d26b3bSNicholas Bellinger 	int ret;
2918b8d26b3bSNicholas Bellinger 
2919b8d26b3bSNicholas Bellinger 	switch (state) {
2920b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2921b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2922991bb764SSagi Grimberg 		if (!ret)
2923991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2924b8d26b3bSNicholas Bellinger 		break;
2925b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2926b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2927b8d26b3bSNicholas Bellinger 		break;
2928b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2929b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2930b8d26b3bSNicholas Bellinger 		break;
2931b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2932b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2933b8d26b3bSNicholas Bellinger 		break;
2934adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2935adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2936adb54c29SNicholas Bellinger 		break;
2937b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2938b8d26b3bSNicholas Bellinger 		/*
2939b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2940b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2941b8d26b3bSNicholas Bellinger 		 */
2942b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2943b8d26b3bSNicholas Bellinger 		break;
2944b8d26b3bSNicholas Bellinger 	default:
294524f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2946b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2947b8d26b3bSNicholas Bellinger 		break;
2948b8d26b3bSNicholas Bellinger 	}
2949b8d26b3bSNicholas Bellinger 
2950b8d26b3bSNicholas Bellinger 	return ret;
2951b8d26b3bSNicholas Bellinger }
2952b8d26b3bSNicholas Bellinger 
2953ca6c1d82SSagi Grimberg struct rdma_cm_id *
2954ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2955ca6c1d82SSagi Grimberg {
2956ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2957ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2958ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2959ca6c1d82SSagi Grimberg 	int ret;
2960ca6c1d82SSagi Grimberg 
2961ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
296224f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2963ca6c1d82SSagi Grimberg 
2964ca6c1d82SSagi Grimberg 	id = rdma_create_id(isert_cma_handler, isert_np,
2965ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
2966ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
296724f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
2968ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
2969ca6c1d82SSagi Grimberg 		goto out;
2970ca6c1d82SSagi Grimberg 	}
297124f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
2972ca6c1d82SSagi Grimberg 
2973ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
2974ca6c1d82SSagi Grimberg 	if (ret) {
297524f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
2976ca6c1d82SSagi Grimberg 		goto out_id;
2977ca6c1d82SSagi Grimberg 	}
2978ca6c1d82SSagi Grimberg 
2979ca6c1d82SSagi Grimberg 	ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
2980ca6c1d82SSagi Grimberg 	if (ret) {
298124f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
2982ca6c1d82SSagi Grimberg 		goto out_id;
2983ca6c1d82SSagi Grimberg 	}
2984ca6c1d82SSagi Grimberg 
2985ca6c1d82SSagi Grimberg 	return id;
2986ca6c1d82SSagi Grimberg out_id:
2987ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
2988ca6c1d82SSagi Grimberg out:
2989ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
2990ca6c1d82SSagi Grimberg }
2991ca6c1d82SSagi Grimberg 
2992b8d26b3bSNicholas Bellinger static int
2993b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
2994b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
2995b8d26b3bSNicholas Bellinger {
2996b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2997b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2998b8d26b3bSNicholas Bellinger 	int ret;
2999b8d26b3bSNicholas Bellinger 
3000b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3001b8d26b3bSNicholas Bellinger 	if (!isert_np) {
300224f412ddSSagi Grimberg 		isert_err("Unable to allocate struct isert_np\n");
3003b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3004b8d26b3bSNicholas Bellinger 	}
3005531b7bf4SSagi Grimberg 	sema_init(&isert_np->np_sem, 0);
3006b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
3007b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
3008b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
3009ca6c1d82SSagi Grimberg 	isert_np->np = np;
3010b8d26b3bSNicholas Bellinger 
3011b8d26b3bSNicholas Bellinger 	/*
3012b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3013b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3014b8d26b3bSNicholas Bellinger 	 */
3015b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
3016b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
3017b8d26b3bSNicholas Bellinger 
3018ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
3019b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3020b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3021b8d26b3bSNicholas Bellinger 		goto out;
3022b8d26b3bSNicholas Bellinger 	}
3023b8d26b3bSNicholas Bellinger 
3024b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
3025b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3026b8d26b3bSNicholas Bellinger 
3027b8d26b3bSNicholas Bellinger 	return 0;
3028b8d26b3bSNicholas Bellinger 
3029b8d26b3bSNicholas Bellinger out:
3030b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3031ca6c1d82SSagi Grimberg 
3032b8d26b3bSNicholas Bellinger 	return ret;
3033b8d26b3bSNicholas Bellinger }
3034b8d26b3bSNicholas Bellinger 
3035b8d26b3bSNicholas Bellinger static int
3036b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3037b8d26b3bSNicholas Bellinger {
3038b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3039b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3040b8d26b3bSNicholas Bellinger 	int ret;
3041b8d26b3bSNicholas Bellinger 
3042b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3043b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3044b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3045b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3046b8d26b3bSNicholas Bellinger 
3047b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3048b8d26b3bSNicholas Bellinger 	if (ret) {
304924f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
3050b8d26b3bSNicholas Bellinger 		return ret;
3051b8d26b3bSNicholas Bellinger 	}
3052b8d26b3bSNicholas Bellinger 
3053b8d26b3bSNicholas Bellinger 	return 0;
3054b8d26b3bSNicholas Bellinger }
3055b8d26b3bSNicholas Bellinger 
3056b8d26b3bSNicholas Bellinger static int
3057b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3058b8d26b3bSNicholas Bellinger {
3059b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3060b8d26b3bSNicholas Bellinger 	int ret;
3061b8d26b3bSNicholas Bellinger 
306224f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
30632371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
30642371e5daSSagi Grimberg 	if (ret) {
306524f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
30662371e5daSSagi Grimberg 			  isert_conn);
30672371e5daSSagi Grimberg 		return ret;
30682371e5daSSagi Grimberg 	}
30692371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
30702371e5daSSagi Grimberg 
30716faaa85fSNicholas Bellinger 	/*
30726faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
30736faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
30746faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
30756faaa85fSNicholas Bellinger 	 * into a NOP.
30766faaa85fSNicholas Bellinger 	 */
30776faaa85fSNicholas Bellinger 	if (!login->first_request)
30786faaa85fSNicholas Bellinger 		return 0;
3079b8d26b3bSNicholas Bellinger 
30802371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
30812371e5daSSagi Grimberg 
308224f412ddSSagi Grimberg 	isert_info("before conn_login_comp conn: %p\n", conn);
3083b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3084b8d26b3bSNicholas Bellinger 	if (ret)
3085b8d26b3bSNicholas Bellinger 		return ret;
3086b8d26b3bSNicholas Bellinger 
308724f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
30882371e5daSSagi Grimberg 
3089b8d26b3bSNicholas Bellinger 	return 0;
3090b8d26b3bSNicholas Bellinger }
3091b8d26b3bSNicholas Bellinger 
3092b8d26b3bSNicholas Bellinger static void
3093b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3094b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3095b8d26b3bSNicholas Bellinger {
3096b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3097b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3098b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
3099b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
3100b8d26b3bSNicholas Bellinger 
3101b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3102b8d26b3bSNicholas Bellinger 
3103b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
3104b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3105b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3106b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3107b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
3108b8d26b3bSNicholas Bellinger 
3109b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3110b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3111b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3112b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
3113b8d26b3bSNicholas Bellinger 	} else {
3114b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3115b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
3116b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3117b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
3118b8d26b3bSNicholas Bellinger 
3119b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3120b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
3121b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3122b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
3123b8d26b3bSNicholas Bellinger 	}
3124b8d26b3bSNicholas Bellinger }
3125b8d26b3bSNicholas Bellinger 
3126b8d26b3bSNicholas Bellinger static int
3127b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3128b8d26b3bSNicholas Bellinger {
3129b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3130b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3131b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
3132b8d26b3bSNicholas Bellinger 
3133b8d26b3bSNicholas Bellinger accept_wait:
3134531b7bf4SSagi Grimberg 	ret = down_interruptible(&isert_np->np_sem);
31351acff63fSSagi Grimberg 	if (ret || max_accept > 5)
3136b8d26b3bSNicholas Bellinger 		return -ENODEV;
3137b8d26b3bSNicholas Bellinger 
3138b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3139e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3140b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
314124f412ddSSagi Grimberg 		isert_dbg("np_thread_state %d for isert_accept_np\n",
3142e346ab34SSagi Grimberg 			 np->np_thread_state);
3143e346ab34SSagi Grimberg 		/**
3144e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
3145e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
3146e346ab34SSagi Grimberg 		 **/
3147b8d26b3bSNicholas Bellinger 		return -ENODEV;
3148b8d26b3bSNicholas Bellinger 	}
3149b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3150b8d26b3bSNicholas Bellinger 
3151b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3152b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
3153b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
3154b8d26b3bSNicholas Bellinger 		max_accept++;
3155b8d26b3bSNicholas Bellinger 		goto accept_wait;
3156b8d26b3bSNicholas Bellinger 	}
3157b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
3158b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
3159b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
3160b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3161b8d26b3bSNicholas Bellinger 
3162b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3163b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3164b8d26b3bSNicholas Bellinger 	max_accept = 0;
3165b8d26b3bSNicholas Bellinger 
3166b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3167b8d26b3bSNicholas Bellinger 
316824f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
31692371e5daSSagi Grimberg 
3170b8d26b3bSNicholas Bellinger 	return 0;
3171b8d26b3bSNicholas Bellinger }
3172b8d26b3bSNicholas Bellinger 
3173b8d26b3bSNicholas Bellinger static void
3174b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3175b8d26b3bSNicholas Bellinger {
3176b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3177268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
3178b8d26b3bSNicholas Bellinger 
31793b726ae2SSagi Grimberg 	if (isert_np->np_cm_id)
3180b8d26b3bSNicholas Bellinger 		rdma_destroy_id(isert_np->np_cm_id);
3181b8d26b3bSNicholas Bellinger 
3182268e6811SSagi Grimberg 	/*
3183268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
3184268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
3185268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
3186268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
3187268e6811SSagi Grimberg 	 * up in np_accept_list.
3188268e6811SSagi Grimberg 	 */
3189268e6811SSagi Grimberg 	mutex_lock(&isert_np->np_accept_mutex);
3190268e6811SSagi Grimberg 	if (!list_empty(&isert_np->np_accept_list)) {
319124f412ddSSagi Grimberg 		isert_info("Still have isert connections, cleaning up...\n");
3192268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
3193268e6811SSagi Grimberg 					 &isert_np->np_accept_list,
3194268e6811SSagi Grimberg 					 conn_accept_node) {
319524f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
3196268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
3197268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
3198268e6811SSagi Grimberg 		}
3199268e6811SSagi Grimberg 	}
3200268e6811SSagi Grimberg 	mutex_unlock(&isert_np->np_accept_mutex);
3201268e6811SSagi Grimberg 
3202b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3203b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3204b8d26b3bSNicholas Bellinger }
3205b8d26b3bSNicholas Bellinger 
3206b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
3207b02efbfcSSagi Grimberg {
3208b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
3209b02efbfcSSagi Grimberg 						     struct isert_conn,
3210b02efbfcSSagi Grimberg 						     release_work);
3211b02efbfcSSagi Grimberg 
321224f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
3213b02efbfcSSagi Grimberg 
3214b02efbfcSSagi Grimberg 	wait_for_completion(&isert_conn->conn_wait);
3215b02efbfcSSagi Grimberg 
3216b02efbfcSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3217b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
3218b02efbfcSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
3219b02efbfcSSagi Grimberg 
322024f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
3221b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
3222b02efbfcSSagi Grimberg }
3223b02efbfcSSagi Grimberg 
3224bdf20e72SSagi Grimberg static void
3225991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
3226991bb764SSagi Grimberg {
3227991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
3228991bb764SSagi Grimberg 
32294c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
32304c22e07fSSagi Grimberg 
3231991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
323224f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
3233991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
3234991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
3235991bb764SSagi Grimberg 	}
3236991bb764SSagi Grimberg }
3237991bb764SSagi Grimberg 
3238991bb764SSagi Grimberg static void
3239c7e160eeSSagi Grimberg isert_wait4cmds(struct iscsi_conn *conn)
3240c7e160eeSSagi Grimberg {
32414c22e07fSSagi Grimberg 	isert_info("iscsi_conn %p\n", conn);
32424c22e07fSSagi Grimberg 
3243c7e160eeSSagi Grimberg 	if (conn->sess) {
3244c7e160eeSSagi Grimberg 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3245c7e160eeSSagi Grimberg 		target_wait_for_sess_cmds(conn->sess->se_sess);
3246c7e160eeSSagi Grimberg 	}
3247c7e160eeSSagi Grimberg }
3248c7e160eeSSagi Grimberg 
3249c7e160eeSSagi Grimberg static void
3250bdf20e72SSagi Grimberg isert_wait4flush(struct isert_conn *isert_conn)
3251bdf20e72SSagi Grimberg {
3252bdf20e72SSagi Grimberg 	struct ib_recv_wr *bad_wr;
3253bdf20e72SSagi Grimberg 
32544c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
32554c22e07fSSagi Grimberg 
3256bdf20e72SSagi Grimberg 	init_completion(&isert_conn->conn_wait_comp_err);
3257bdf20e72SSagi Grimberg 	isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3258bdf20e72SSagi Grimberg 	/* post an indication that all flush errors were consumed */
3259bdf20e72SSagi Grimberg 	if (ib_post_recv(isert_conn->conn_qp, &isert_conn->beacon, &bad_wr)) {
326024f412ddSSagi Grimberg 		isert_err("conn %p failed to post beacon", isert_conn);
3261bdf20e72SSagi Grimberg 		return;
3262bdf20e72SSagi Grimberg 	}
3263bdf20e72SSagi Grimberg 
3264bdf20e72SSagi Grimberg 	wait_for_completion(&isert_conn->conn_wait_comp_err);
3265bdf20e72SSagi Grimberg }
3266bdf20e72SSagi Grimberg 
3267defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3268b8d26b3bSNicholas Bellinger {
3269b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3270b8d26b3bSNicholas Bellinger 
32714c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
3272b8d26b3bSNicholas Bellinger 
32739d49f5e2SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3274b8d26b3bSNicholas Bellinger 	/*
3275b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
3276b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3277b8d26b3bSNicholas Bellinger 	 */
3278b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3279b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
3280b2cb9649SNicholas Bellinger 		return;
3281b2cb9649SNicholas Bellinger 	}
3282954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
3283b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
3284b2cb9649SNicholas Bellinger 
3285c7e160eeSSagi Grimberg 	isert_wait4cmds(conn);
3286bdf20e72SSagi Grimberg 	isert_wait4flush(isert_conn);
3287991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
3288954f2372SSagi Grimberg 
3289b02efbfcSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
3290b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
3291defd8848SNicholas Bellinger }
3292defd8848SNicholas Bellinger 
3293defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3294defd8848SNicholas Bellinger {
3295defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3296b8d26b3bSNicholas Bellinger 
3297b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3298b8d26b3bSNicholas Bellinger }
3299b8d26b3bSNicholas Bellinger 
3300b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3301b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3302b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3303d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3304b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3305b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3306b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3307b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3308defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3309b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3310b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3311b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3312b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3313b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3314b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3315b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3316b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3317131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3318e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3319b8d26b3bSNicholas Bellinger };
3320b8d26b3bSNicholas Bellinger 
3321b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3322b8d26b3bSNicholas Bellinger {
3323b8d26b3bSNicholas Bellinger 	int ret;
3324b8d26b3bSNicholas Bellinger 
3325631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
3326631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
3327b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
332824f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
3329b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
33306f0fae3dSSagi Grimberg 		return -ENOMEM;
3331b8d26b3bSNicholas Bellinger 	}
3332b8d26b3bSNicholas Bellinger 
3333b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3334b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
3335b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
333624f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
3337b02efbfcSSagi Grimberg 		ret = -ENOMEM;
3338b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
3339b02efbfcSSagi Grimberg 	}
3340b02efbfcSSagi Grimberg 
3341b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
334224f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3343b02efbfcSSagi Grimberg 
3344b8d26b3bSNicholas Bellinger 	return 0;
3345b8d26b3bSNicholas Bellinger 
3346b02efbfcSSagi Grimberg destroy_comp_wq:
3347b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
33486f0fae3dSSagi Grimberg 
3349b8d26b3bSNicholas Bellinger 	return ret;
3350b8d26b3bSNicholas Bellinger }
3351b8d26b3bSNicholas Bellinger 
3352b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3353b8d26b3bSNicholas Bellinger {
3354f5ebec96SSagi Grimberg 	flush_scheduled_work();
3355b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
3356b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3357b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
33584c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
3359b8d26b3bSNicholas Bellinger }
3360b8d26b3bSNicholas Bellinger 
3361b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3362b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
3363b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3364b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3365b8d26b3bSNicholas Bellinger 
3366b8d26b3bSNicholas Bellinger module_init(isert_init);
3367b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3368