xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision c6b8e9180de76624d0a01148d6edd0107edebed1)
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 {
796700425eSSagi Grimberg 	struct isert_conn *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 
11040fc069aSSagi Grimberg static struct isert_comp *
11140fc069aSSagi Grimberg isert_comp_get(struct isert_conn *isert_conn)
112b8d26b3bSNicholas Bellinger {
113b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
1144a295baeSSagi Grimberg 	struct isert_comp *comp;
11540fc069aSSagi Grimberg 	int i, min = 0;
116b8d26b3bSNicholas Bellinger 
117b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
1184a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++)
1194a295baeSSagi Grimberg 		if (device->comps[i].active_qps <
1204a295baeSSagi Grimberg 		    device->comps[min].active_qps)
1214a295baeSSagi Grimberg 			min = i;
1224a295baeSSagi Grimberg 	comp = &device->comps[min];
1234a295baeSSagi Grimberg 	comp->active_qps++;
12440fc069aSSagi Grimberg 	mutex_unlock(&device_list_mutex);
12540fc069aSSagi Grimberg 
12624f412ddSSagi Grimberg 	isert_info("conn %p, using comp %p min_index: %d\n",
1274a295baeSSagi Grimberg 		   isert_conn, comp, min);
12840fc069aSSagi Grimberg 
12940fc069aSSagi Grimberg 	return comp;
13040fc069aSSagi Grimberg }
13140fc069aSSagi Grimberg 
13240fc069aSSagi Grimberg static void
13340fc069aSSagi Grimberg isert_comp_put(struct isert_comp *comp)
13440fc069aSSagi Grimberg {
13540fc069aSSagi Grimberg 	mutex_lock(&device_list_mutex);
13640fc069aSSagi Grimberg 	comp->active_qps--;
137b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
13840fc069aSSagi Grimberg }
13940fc069aSSagi Grimberg 
14040fc069aSSagi Grimberg static struct ib_qp *
14140fc069aSSagi Grimberg isert_create_qp(struct isert_conn *isert_conn,
14240fc069aSSagi Grimberg 		struct isert_comp *comp,
14340fc069aSSagi Grimberg 		struct rdma_cm_id *cma_id)
14440fc069aSSagi Grimberg {
14540fc069aSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
14640fc069aSSagi Grimberg 	struct ib_qp_init_attr attr;
14740fc069aSSagi Grimberg 	int ret;
148b8d26b3bSNicholas Bellinger 
149b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
150b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
151b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
1526f0fae3dSSagi Grimberg 	attr.send_cq = comp->cq;
1536f0fae3dSSagi Grimberg 	attr.recv_cq = comp->cq;
154b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
155bdf20e72SSagi Grimberg 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
156b8d26b3bSNicholas Bellinger 	/*
157b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
158f57915cfSOr Gerlitz 	 * work-around for RDMA_READs with ConnectX-2.
159f57915cfSOr Gerlitz 	 *
160f57915cfSOr Gerlitz 	 * Also, still make sure to have at least two SGEs for
161f57915cfSOr Gerlitz 	 * outgoing control PDU responses.
162b8d26b3bSNicholas Bellinger 	 */
163f57915cfSOr Gerlitz 	attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
164b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
165b8d26b3bSNicholas Bellinger 
166b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
167b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
168b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
169570db170SSagi Grimberg 	if (device->pi_capable)
170d3e125daSSagi Grimberg 		attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
171b8d26b3bSNicholas Bellinger 
17267cb3949SSagi Grimberg 	ret = rdma_create_qp(cma_id, device->pd, &attr);
173b8d26b3bSNicholas Bellinger 	if (ret) {
17424f412ddSSagi Grimberg 		isert_err("rdma_create_qp failed for cma_id %d\n", ret);
17540fc069aSSagi Grimberg 		return ERR_PTR(ret);
17640fc069aSSagi Grimberg 	}
17740fc069aSSagi Grimberg 
17840fc069aSSagi Grimberg 	return cma_id->qp;
17940fc069aSSagi Grimberg }
18040fc069aSSagi Grimberg 
18140fc069aSSagi Grimberg static int
18240fc069aSSagi Grimberg isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
18340fc069aSSagi Grimberg {
18440fc069aSSagi Grimberg 	struct isert_comp *comp;
18540fc069aSSagi Grimberg 	int ret;
18640fc069aSSagi Grimberg 
18740fc069aSSagi Grimberg 	comp = isert_comp_get(isert_conn);
18840fc069aSSagi Grimberg 	isert_conn->conn_qp = isert_create_qp(isert_conn, comp, cma_id);
18940fc069aSSagi Grimberg 	if (IS_ERR(isert_conn->conn_qp)) {
19040fc069aSSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_qp);
19119e2090fSSagi Grimberg 		goto err;
192b8d26b3bSNicholas Bellinger 	}
193b8d26b3bSNicholas Bellinger 
194b8d26b3bSNicholas Bellinger 	return 0;
19519e2090fSSagi Grimberg err:
19640fc069aSSagi Grimberg 	isert_comp_put(comp);
19719e2090fSSagi Grimberg 	return ret;
198b8d26b3bSNicholas Bellinger }
199b8d26b3bSNicholas Bellinger 
200b8d26b3bSNicholas Bellinger static void
201b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
202b8d26b3bSNicholas Bellinger {
2034c22e07fSSagi Grimberg 	isert_dbg("event: %d\n", e->event);
204b8d26b3bSNicholas Bellinger }
205b8d26b3bSNicholas Bellinger 
206b8d26b3bSNicholas Bellinger static int
207b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
208b8d26b3bSNicholas Bellinger {
20967cb3949SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
21067cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
211b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
212b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
213b8d26b3bSNicholas Bellinger 	u64 dma_addr;
214b8d26b3bSNicholas Bellinger 	int i, j;
215b8d26b3bSNicholas Bellinger 
216b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
217b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
218b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
219b8d26b3bSNicholas Bellinger 		goto fail;
220b8d26b3bSNicholas Bellinger 
221b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
222b8d26b3bSNicholas Bellinger 
223b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
224b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
225b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
226b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
227b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
228b8d26b3bSNicholas Bellinger 
229b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
230b8d26b3bSNicholas Bellinger 
231b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
232b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
233b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
23467cb3949SSagi Grimberg 		rx_sg->lkey = device->mr->lkey;
235b8d26b3bSNicholas Bellinger 	}
236b8d26b3bSNicholas Bellinger 
237b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
2384c22e07fSSagi Grimberg 
239b8d26b3bSNicholas Bellinger 	return 0;
240b8d26b3bSNicholas Bellinger 
241b8d26b3bSNicholas Bellinger dma_map_fail:
242b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
243b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
244b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
245b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
246b8d26b3bSNicholas Bellinger 	}
247b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
248b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
249b8d26b3bSNicholas Bellinger fail:
2504c22e07fSSagi Grimberg 	isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
2514c22e07fSSagi Grimberg 
252b8d26b3bSNicholas Bellinger 	return -ENOMEM;
253b8d26b3bSNicholas Bellinger }
254b8d26b3bSNicholas Bellinger 
255b8d26b3bSNicholas Bellinger static void
256b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
257b8d26b3bSNicholas Bellinger {
2584a579da2SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_device->ib_device;
259b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
260b8d26b3bSNicholas Bellinger 	int i;
261b8d26b3bSNicholas Bellinger 
262b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
263b8d26b3bSNicholas Bellinger 		return;
264b8d26b3bSNicholas Bellinger 
265b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
266b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
267b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
268b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
269b8d26b3bSNicholas Bellinger 	}
270b8d26b3bSNicholas Bellinger 
271b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
272b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
273b8d26b3bSNicholas Bellinger }
274b8d26b3bSNicholas Bellinger 
2756f0fae3dSSagi Grimberg static void isert_cq_work(struct work_struct *);
2766f0fae3dSSagi Grimberg static void isert_cq_callback(struct ib_cq *, void *);
277b8d26b3bSNicholas Bellinger 
278172369c5SSagi Grimberg static void
279172369c5SSagi Grimberg isert_free_comps(struct isert_device *device)
280b8d26b3bSNicholas Bellinger {
281172369c5SSagi Grimberg 	int i;
282b8d26b3bSNicholas Bellinger 
283172369c5SSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
284172369c5SSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
28559464ef4SVu Pham 
286172369c5SSagi Grimberg 		if (comp->cq) {
287172369c5SSagi Grimberg 			cancel_work_sync(&comp->work);
288172369c5SSagi Grimberg 			ib_destroy_cq(comp->cq);
289172369c5SSagi Grimberg 		}
290172369c5SSagi Grimberg 	}
291172369c5SSagi Grimberg 	kfree(device->comps);
29259464ef4SVu Pham }
293d40945d8SVu Pham 
294172369c5SSagi Grimberg static int
295172369c5SSagi Grimberg isert_alloc_comps(struct isert_device *device,
296172369c5SSagi Grimberg 		  struct ib_device_attr *attr)
297172369c5SSagi Grimberg {
298172369c5SSagi Grimberg 	int i, max_cqe, ret = 0;
299d3e125daSSagi Grimberg 
3004a295baeSSagi Grimberg 	device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
3014a295baeSSagi Grimberg 				 device->ib_device->num_comp_vectors));
302172369c5SSagi Grimberg 
30324f412ddSSagi Grimberg 	isert_info("Using %d CQs, %s supports %d vectors support "
304d3e125daSSagi Grimberg 		   "Fast registration %d pi_capable %d\n",
3054a295baeSSagi Grimberg 		   device->comps_used, device->ib_device->name,
306d3e125daSSagi Grimberg 		   device->ib_device->num_comp_vectors, device->use_fastreg,
307d3e125daSSagi Grimberg 		   device->pi_capable);
3084a295baeSSagi Grimberg 
3094a295baeSSagi Grimberg 	device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
3104a295baeSSagi Grimberg 				GFP_KERNEL);
3114a295baeSSagi Grimberg 	if (!device->comps) {
31224f412ddSSagi Grimberg 		isert_err("Unable to allocate completion contexts\n");
313b8d26b3bSNicholas Bellinger 		return -ENOMEM;
314b8d26b3bSNicholas Bellinger 	}
315b8d26b3bSNicholas Bellinger 
316172369c5SSagi Grimberg 	max_cqe = min(ISER_MAX_CQ_LEN, attr->max_cqe);
317172369c5SSagi Grimberg 
3184a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
3194a295baeSSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
320b8d26b3bSNicholas Bellinger 
3214a295baeSSagi Grimberg 		comp->device = device;
3226f0fae3dSSagi Grimberg 		INIT_WORK(&comp->work, isert_cq_work);
3236f0fae3dSSagi Grimberg 		comp->cq = ib_create_cq(device->ib_device,
3246f0fae3dSSagi Grimberg 					isert_cq_callback,
325b8d26b3bSNicholas Bellinger 					isert_cq_event_callback,
3264a295baeSSagi Grimberg 					(void *)comp,
3276f0fae3dSSagi Grimberg 					max_cqe, i);
3286f0fae3dSSagi Grimberg 		if (IS_ERR(comp->cq)) {
329172369c5SSagi Grimberg 			isert_err("Unable to allocate cq\n");
3306f0fae3dSSagi Grimberg 			ret = PTR_ERR(comp->cq);
3316f0fae3dSSagi Grimberg 			comp->cq = NULL;
332b8d26b3bSNicholas Bellinger 			goto out_cq;
33394a71110SWei Yongjun 		}
334b8d26b3bSNicholas Bellinger 
3356f0fae3dSSagi Grimberg 		ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
33694a71110SWei Yongjun 		if (ret)
337b8d26b3bSNicholas Bellinger 			goto out_cq;
338b8d26b3bSNicholas Bellinger 	}
339b8d26b3bSNicholas Bellinger 
340172369c5SSagi Grimberg 	return 0;
341172369c5SSagi Grimberg out_cq:
342172369c5SSagi Grimberg 	isert_free_comps(device);
343172369c5SSagi Grimberg 	return ret;
344172369c5SSagi Grimberg }
345172369c5SSagi Grimberg 
346172369c5SSagi Grimberg static int
347172369c5SSagi Grimberg isert_create_device_ib_res(struct isert_device *device)
348172369c5SSagi Grimberg {
349172369c5SSagi Grimberg 	struct ib_device_attr *dev_attr;
350fd8205e8SSagi Grimberg 	int ret;
351172369c5SSagi Grimberg 
352172369c5SSagi Grimberg 	dev_attr = &device->dev_attr;
353fd8205e8SSagi Grimberg 	ret = isert_query_device(device->ib_device, dev_attr);
354172369c5SSagi Grimberg 	if (ret)
355172369c5SSagi Grimberg 		return ret;
356172369c5SSagi Grimberg 
357172369c5SSagi Grimberg 	/* asign function handlers */
358172369c5SSagi Grimberg 	if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
359172369c5SSagi Grimberg 	    dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
360172369c5SSagi Grimberg 		device->use_fastreg = 1;
361172369c5SSagi Grimberg 		device->reg_rdma_mem = isert_reg_rdma;
362172369c5SSagi Grimberg 		device->unreg_rdma_mem = isert_unreg_rdma;
363172369c5SSagi Grimberg 	} else {
364172369c5SSagi Grimberg 		device->use_fastreg = 0;
365172369c5SSagi Grimberg 		device->reg_rdma_mem = isert_map_rdma;
366172369c5SSagi Grimberg 		device->unreg_rdma_mem = isert_unmap_cmd;
367172369c5SSagi Grimberg 	}
368172369c5SSagi Grimberg 
369172369c5SSagi Grimberg 	ret = isert_alloc_comps(device, dev_attr);
370172369c5SSagi Grimberg 	if (ret)
371172369c5SSagi Grimberg 		return ret;
372172369c5SSagi Grimberg 
37367cb3949SSagi Grimberg 	device->pd = ib_alloc_pd(device->ib_device);
37467cb3949SSagi Grimberg 	if (IS_ERR(device->pd)) {
37567cb3949SSagi Grimberg 		ret = PTR_ERR(device->pd);
37667cb3949SSagi Grimberg 		isert_err("failed to allocate pd, device %p, ret=%d\n",
37767cb3949SSagi Grimberg 			  device, ret);
37867cb3949SSagi Grimberg 		goto out_cq;
37967cb3949SSagi Grimberg 	}
38067cb3949SSagi Grimberg 
38167cb3949SSagi Grimberg 	device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE);
38267cb3949SSagi Grimberg 	if (IS_ERR(device->mr)) {
38367cb3949SSagi Grimberg 		ret = PTR_ERR(device->mr);
38467cb3949SSagi Grimberg 		isert_err("failed to create dma mr, device %p, ret=%d\n",
38567cb3949SSagi Grimberg 			  device, ret);
38667cb3949SSagi Grimberg 		goto out_mr;
38767cb3949SSagi Grimberg 	}
38867cb3949SSagi Grimberg 
389172369c5SSagi Grimberg 	/* Check signature cap */
390172369c5SSagi Grimberg 	device->pi_capable = dev_attr->device_cap_flags &
391172369c5SSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
39267cb3949SSagi Grimberg 
393b8d26b3bSNicholas Bellinger 	return 0;
394b8d26b3bSNicholas Bellinger 
39567cb3949SSagi Grimberg out_mr:
39667cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
397b8d26b3bSNicholas Bellinger out_cq:
398172369c5SSagi Grimberg 	isert_free_comps(device);
399b8d26b3bSNicholas Bellinger 	return ret;
400b8d26b3bSNicholas Bellinger }
401b8d26b3bSNicholas Bellinger 
402b8d26b3bSNicholas Bellinger static void
403b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
404b8d26b3bSNicholas Bellinger {
40524f412ddSSagi Grimberg 	isert_info("device %p\n", device);
406b8d26b3bSNicholas Bellinger 
40767cb3949SSagi Grimberg 	ib_dereg_mr(device->mr);
40867cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
409172369c5SSagi Grimberg 	isert_free_comps(device);
410b8d26b3bSNicholas Bellinger }
411b8d26b3bSNicholas Bellinger 
412b8d26b3bSNicholas Bellinger static void
413cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device)
414b8d26b3bSNicholas Bellinger {
415b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
416b8d26b3bSNicholas Bellinger 	device->refcount--;
4174c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
418b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
419b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
420b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
421b8d26b3bSNicholas Bellinger 		kfree(device);
422b8d26b3bSNicholas Bellinger 	}
423b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
424b8d26b3bSNicholas Bellinger }
425b8d26b3bSNicholas Bellinger 
426b8d26b3bSNicholas Bellinger static struct isert_device *
427cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id)
428b8d26b3bSNicholas Bellinger {
429b8d26b3bSNicholas Bellinger 	struct isert_device *device;
430b8d26b3bSNicholas Bellinger 	int ret;
431b8d26b3bSNicholas Bellinger 
432b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
433b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
434b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
435b8d26b3bSNicholas Bellinger 			device->refcount++;
4364c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
4374c22e07fSSagi Grimberg 				   device, device->refcount);
438b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
439b8d26b3bSNicholas Bellinger 			return device;
440b8d26b3bSNicholas Bellinger 		}
441b8d26b3bSNicholas Bellinger 	}
442b8d26b3bSNicholas Bellinger 
443b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
444b8d26b3bSNicholas Bellinger 	if (!device) {
445b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
446b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
447b8d26b3bSNicholas Bellinger 	}
448b8d26b3bSNicholas Bellinger 
449b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
450b8d26b3bSNicholas Bellinger 
451b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
452b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
453b8d26b3bSNicholas Bellinger 	if (ret) {
454b8d26b3bSNicholas Bellinger 		kfree(device);
455b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
456b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
457b8d26b3bSNicholas Bellinger 	}
458b8d26b3bSNicholas Bellinger 
459b8d26b3bSNicholas Bellinger 	device->refcount++;
460b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
4614c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
4624c22e07fSSagi Grimberg 		   device, device->refcount);
463b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
464b8d26b3bSNicholas Bellinger 
465b8d26b3bSNicholas Bellinger 	return device;
466b8d26b3bSNicholas Bellinger }
467b8d26b3bSNicholas Bellinger 
46859464ef4SVu Pham static void
469a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
47059464ef4SVu Pham {
47159464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
47259464ef4SVu Pham 	int i = 0;
47359464ef4SVu Pham 
474a3a5a826SSagi Grimberg 	if (list_empty(&isert_conn->conn_fr_pool))
47559464ef4SVu Pham 		return;
47659464ef4SVu Pham 
4774c22e07fSSagi Grimberg 	isert_info("Freeing conn %p fastreg pool", isert_conn);
47859464ef4SVu Pham 
47959464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
480a3a5a826SSagi Grimberg 				 &isert_conn->conn_fr_pool, list) {
48159464ef4SVu Pham 		list_del(&fr_desc->list);
48259464ef4SVu Pham 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
48359464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
484d3e125daSSagi Grimberg 		if (fr_desc->pi_ctx) {
485d3e125daSSagi Grimberg 			ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
486d3e125daSSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
487d3e125daSSagi Grimberg 			ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
488d3e125daSSagi Grimberg 			kfree(fr_desc->pi_ctx);
489d3e125daSSagi Grimberg 		}
49059464ef4SVu Pham 		kfree(fr_desc);
49159464ef4SVu Pham 		++i;
49259464ef4SVu Pham 	}
49359464ef4SVu Pham 
494a3a5a826SSagi Grimberg 	if (i < isert_conn->conn_fr_pool_size)
49524f412ddSSagi Grimberg 		isert_warn("Pool still has %d regions registered\n",
496a3a5a826SSagi Grimberg 			isert_conn->conn_fr_pool_size - i);
49759464ef4SVu Pham }
49859464ef4SVu Pham 
49959464ef4SVu Pham static int
500570db170SSagi Grimberg isert_create_pi_ctx(struct fast_reg_descriptor *desc,
501570db170SSagi Grimberg 		    struct ib_device *device,
502570db170SSagi Grimberg 		    struct ib_pd *pd)
503570db170SSagi Grimberg {
504570db170SSagi Grimberg 	struct ib_mr_init_attr mr_init_attr;
505570db170SSagi Grimberg 	struct pi_context *pi_ctx;
506570db170SSagi Grimberg 	int ret;
507570db170SSagi Grimberg 
508570db170SSagi Grimberg 	pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
509570db170SSagi Grimberg 	if (!pi_ctx) {
51024f412ddSSagi Grimberg 		isert_err("Failed to allocate pi context\n");
511570db170SSagi Grimberg 		return -ENOMEM;
512570db170SSagi Grimberg 	}
513570db170SSagi Grimberg 
514570db170SSagi Grimberg 	pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
515570db170SSagi Grimberg 					    ISCSI_ISER_SG_TABLESIZE);
516570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_frpl)) {
51724f412ddSSagi Grimberg 		isert_err("Failed to allocate prot frpl err=%ld\n",
518570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_frpl));
519570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_frpl);
520570db170SSagi Grimberg 		goto err_pi_ctx;
521570db170SSagi Grimberg 	}
522570db170SSagi Grimberg 
523570db170SSagi Grimberg 	pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
524570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_mr)) {
52524f412ddSSagi Grimberg 		isert_err("Failed to allocate prot frmr err=%ld\n",
526570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_mr));
527570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_mr);
528570db170SSagi Grimberg 		goto err_prot_frpl;
529570db170SSagi Grimberg 	}
530570db170SSagi Grimberg 	desc->ind |= ISERT_PROT_KEY_VALID;
531570db170SSagi Grimberg 
532570db170SSagi Grimberg 	memset(&mr_init_attr, 0, sizeof(mr_init_attr));
533570db170SSagi Grimberg 	mr_init_attr.max_reg_descriptors = 2;
534570db170SSagi Grimberg 	mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
535570db170SSagi Grimberg 	pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
536570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->sig_mr)) {
53724f412ddSSagi Grimberg 		isert_err("Failed to allocate signature enabled mr err=%ld\n",
538570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->sig_mr));
539570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->sig_mr);
540570db170SSagi Grimberg 		goto err_prot_mr;
541570db170SSagi Grimberg 	}
542570db170SSagi Grimberg 
543570db170SSagi Grimberg 	desc->pi_ctx = pi_ctx;
544570db170SSagi Grimberg 	desc->ind |= ISERT_SIG_KEY_VALID;
545570db170SSagi Grimberg 	desc->ind &= ~ISERT_PROTECTED;
546570db170SSagi Grimberg 
547570db170SSagi Grimberg 	return 0;
548570db170SSagi Grimberg 
549570db170SSagi Grimberg err_prot_mr:
550570db170SSagi Grimberg 	ib_dereg_mr(desc->pi_ctx->prot_mr);
551570db170SSagi Grimberg err_prot_frpl:
552570db170SSagi Grimberg 	ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
553570db170SSagi Grimberg err_pi_ctx:
554570db170SSagi Grimberg 	kfree(desc->pi_ctx);
555570db170SSagi Grimberg 
556570db170SSagi Grimberg 	return ret;
557570db170SSagi Grimberg }
558570db170SSagi Grimberg 
559570db170SSagi Grimberg static int
560dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
561570db170SSagi Grimberg 		     struct fast_reg_descriptor *fr_desc)
562dc87a90fSSagi Grimberg {
563d3e125daSSagi Grimberg 	int ret;
564d3e125daSSagi Grimberg 
565dc87a90fSSagi Grimberg 	fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
566dc87a90fSSagi Grimberg 							 ISCSI_ISER_SG_TABLESIZE);
567dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_frpl)) {
56824f412ddSSagi Grimberg 		isert_err("Failed to allocate data frpl err=%ld\n",
569dc87a90fSSagi Grimberg 			  PTR_ERR(fr_desc->data_frpl));
570dc87a90fSSagi Grimberg 		return PTR_ERR(fr_desc->data_frpl);
571dc87a90fSSagi Grimberg 	}
572dc87a90fSSagi Grimberg 
573dc87a90fSSagi Grimberg 	fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
574dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
57524f412ddSSagi Grimberg 		isert_err("Failed to allocate data frmr err=%ld\n",
576dc87a90fSSagi Grimberg 			  PTR_ERR(fr_desc->data_mr));
577d3e125daSSagi Grimberg 		ret = PTR_ERR(fr_desc->data_mr);
578d3e125daSSagi Grimberg 		goto err_data_frpl;
579dc87a90fSSagi Grimberg 	}
580d3e125daSSagi Grimberg 	fr_desc->ind |= ISERT_DATA_KEY_VALID;
581dc87a90fSSagi Grimberg 
58224f412ddSSagi Grimberg 	isert_dbg("Created fr_desc %p\n", fr_desc);
583dc87a90fSSagi Grimberg 
584dc87a90fSSagi Grimberg 	return 0;
585570db170SSagi Grimberg 
586d3e125daSSagi Grimberg err_data_frpl:
587d3e125daSSagi Grimberg 	ib_free_fast_reg_page_list(fr_desc->data_frpl);
588d3e125daSSagi Grimberg 
589d3e125daSSagi Grimberg 	return ret;
590dc87a90fSSagi Grimberg }
591dc87a90fSSagi Grimberg 
592dc87a90fSSagi Grimberg static int
593570db170SSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
59459464ef4SVu Pham {
59559464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
59659464ef4SVu Pham 	struct isert_device *device = isert_conn->conn_device;
597f46d6a8aSNicholas Bellinger 	struct se_session *se_sess = isert_conn->conn->sess->se_sess;
598f46d6a8aSNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
599f46d6a8aSNicholas Bellinger 	int i, ret, tag_num;
600f46d6a8aSNicholas Bellinger 	/*
601f46d6a8aSNicholas Bellinger 	 * Setup the number of FRMRs based upon the number of tags
602f46d6a8aSNicholas Bellinger 	 * available to session in iscsi_target_locate_portal().
603f46d6a8aSNicholas Bellinger 	 */
604f46d6a8aSNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
605f46d6a8aSNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
60659464ef4SVu Pham 
607a3a5a826SSagi Grimberg 	isert_conn->conn_fr_pool_size = 0;
608f46d6a8aSNicholas Bellinger 	for (i = 0; i < tag_num; i++) {
60959464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
61059464ef4SVu Pham 		if (!fr_desc) {
61124f412ddSSagi Grimberg 			isert_err("Failed to allocate fast_reg descriptor\n");
61259464ef4SVu Pham 			ret = -ENOMEM;
61359464ef4SVu Pham 			goto err;
61459464ef4SVu Pham 		}
61559464ef4SVu Pham 
616dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
61767cb3949SSagi Grimberg 					   device->pd, fr_desc);
618dc87a90fSSagi Grimberg 		if (ret) {
61924f412ddSSagi Grimberg 			isert_err("Failed to create fastreg descriptor err=%d\n",
620dc87a90fSSagi Grimberg 			       ret);
621a80e21b3SNicholas Bellinger 			kfree(fr_desc);
62259464ef4SVu Pham 			goto err;
62359464ef4SVu Pham 		}
62459464ef4SVu Pham 
625a3a5a826SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
626a3a5a826SSagi Grimberg 		isert_conn->conn_fr_pool_size++;
62759464ef4SVu Pham 	}
62859464ef4SVu Pham 
62924f412ddSSagi Grimberg 	isert_dbg("Creating conn %p fastreg pool size=%d",
630a3a5a826SSagi Grimberg 		 isert_conn, isert_conn->conn_fr_pool_size);
63159464ef4SVu Pham 
63259464ef4SVu Pham 	return 0;
63359464ef4SVu Pham 
63459464ef4SVu Pham err:
635a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
63659464ef4SVu Pham 	return ret;
63759464ef4SVu Pham }
63859464ef4SVu Pham 
639ae9ea9edSSagi Grimberg static void
640ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn)
641ae9ea9edSSagi Grimberg {
642ae9ea9edSSagi Grimberg 	isert_conn->state = ISER_CONN_INIT;
643ae9ea9edSSagi Grimberg 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
644ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->conn_login_comp);
645ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
646ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->conn_wait);
647ae9ea9edSSagi Grimberg 	kref_init(&isert_conn->conn_kref);
648ae9ea9edSSagi Grimberg 	mutex_init(&isert_conn->conn_mutex);
649ae9ea9edSSagi Grimberg 	spin_lock_init(&isert_conn->conn_lock);
650ae9ea9edSSagi Grimberg 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
651ae9ea9edSSagi Grimberg }
652ae9ea9edSSagi Grimberg 
653ae9ea9edSSagi Grimberg static void
654ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn)
655ae9ea9edSSagi Grimberg {
656ae9ea9edSSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_device->ib_device;
657ae9ea9edSSagi Grimberg 
658ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
659ae9ea9edSSagi Grimberg 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
660ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
661ae9ea9edSSagi Grimberg 			    ISCSI_DEF_MAX_RECV_SEG_LEN,
662ae9ea9edSSagi Grimberg 			    DMA_FROM_DEVICE);
663ae9ea9edSSagi Grimberg 	kfree(isert_conn->login_buf);
664ae9ea9edSSagi Grimberg }
665ae9ea9edSSagi Grimberg 
666ae9ea9edSSagi Grimberg static int
667ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn,
668ae9ea9edSSagi Grimberg 		      struct ib_device *ib_dev)
669ae9ea9edSSagi Grimberg {
670ae9ea9edSSagi Grimberg 	int ret;
671ae9ea9edSSagi Grimberg 
672ae9ea9edSSagi Grimberg 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
673ae9ea9edSSagi Grimberg 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
674ae9ea9edSSagi Grimberg 	if (!isert_conn->login_buf) {
675ae9ea9edSSagi Grimberg 		isert_err("Unable to allocate isert_conn->login_buf\n");
676ae9ea9edSSagi Grimberg 		return -ENOMEM;
677ae9ea9edSSagi Grimberg 	}
678ae9ea9edSSagi Grimberg 
679ae9ea9edSSagi Grimberg 	isert_conn->login_req_buf = isert_conn->login_buf;
680ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_buf = isert_conn->login_buf +
681ae9ea9edSSagi Grimberg 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
682ae9ea9edSSagi Grimberg 
683ae9ea9edSSagi Grimberg 	isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
684ae9ea9edSSagi Grimberg 		 isert_conn->login_buf, isert_conn->login_req_buf,
685ae9ea9edSSagi Grimberg 		 isert_conn->login_rsp_buf);
686ae9ea9edSSagi Grimberg 
687ae9ea9edSSagi Grimberg 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
688ae9ea9edSSagi Grimberg 				(void *)isert_conn->login_req_buf,
689ae9ea9edSSagi Grimberg 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
690ae9ea9edSSagi Grimberg 
691ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
692ae9ea9edSSagi Grimberg 	if (ret) {
693ae9ea9edSSagi Grimberg 		isert_err("login_req_dma mapping error: %d\n", ret);
694ae9ea9edSSagi Grimberg 		isert_conn->login_req_dma = 0;
695ae9ea9edSSagi Grimberg 		goto out_login_buf;
696ae9ea9edSSagi Grimberg 	}
697ae9ea9edSSagi Grimberg 
698ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
699ae9ea9edSSagi Grimberg 					(void *)isert_conn->login_rsp_buf,
700ae9ea9edSSagi Grimberg 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
701ae9ea9edSSagi Grimberg 
702ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
703ae9ea9edSSagi Grimberg 	if (ret) {
704ae9ea9edSSagi Grimberg 		isert_err("login_rsp_dma mapping error: %d\n", ret);
705ae9ea9edSSagi Grimberg 		isert_conn->login_rsp_dma = 0;
706ae9ea9edSSagi Grimberg 		goto out_req_dma_map;
707ae9ea9edSSagi Grimberg 	}
708ae9ea9edSSagi Grimberg 
709ae9ea9edSSagi Grimberg 	return 0;
710ae9ea9edSSagi Grimberg 
711ae9ea9edSSagi Grimberg out_req_dma_map:
712ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
713ae9ea9edSSagi Grimberg 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
714ae9ea9edSSagi Grimberg out_login_buf:
715ae9ea9edSSagi Grimberg 	kfree(isert_conn->login_buf);
716ae9ea9edSSagi Grimberg 	return ret;
717ae9ea9edSSagi Grimberg }
718ae9ea9edSSagi Grimberg 
719b8d26b3bSNicholas Bellinger static int
720b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
721b8d26b3bSNicholas Bellinger {
722ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
723ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
724b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
725b8d26b3bSNicholas Bellinger 	struct isert_device *device;
726b8d26b3bSNicholas Bellinger 	int ret = 0;
72714f4b54fSSagi Grimberg 
72814f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
72914f4b54fSSagi Grimberg 	if (!np->enabled) {
73014f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
73124f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
73214f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
73314f4b54fSSagi Grimberg 	}
73414f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
735b8d26b3bSNicholas Bellinger 
7364c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
737b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
738b8d26b3bSNicholas Bellinger 
739b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
740ae9ea9edSSagi Grimberg 	if (!isert_conn)
741b8d26b3bSNicholas Bellinger 		return -ENOMEM;
742b8d26b3bSNicholas Bellinger 
743ae9ea9edSSagi Grimberg 	isert_init_conn(isert_conn);
744b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
745b8d26b3bSNicholas Bellinger 
746ae9ea9edSSagi Grimberg 	ret = isert_alloc_login_buf(isert_conn, cma_id->device);
747ae9ea9edSSagi Grimberg 	if (ret)
748b8d26b3bSNicholas Bellinger 		goto out;
749b8d26b3bSNicholas Bellinger 
750cf8ae958SSagi Grimberg 	device = isert_device_get(cma_id);
751b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
752b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
753b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
754b8d26b3bSNicholas Bellinger 	}
755cf8ae958SSagi Grimberg 	isert_conn->conn_device = device;
756b8d26b3bSNicholas Bellinger 
7571a92e17eSSagi Grimberg 	/* Set max inflight RDMA READ requests */
7581a92e17eSSagi Grimberg 	isert_conn->initiator_depth = min_t(u8,
7591a92e17eSSagi Grimberg 				event->param.conn.initiator_depth,
7601a92e17eSSagi Grimberg 				device->dev_attr.max_qp_init_rd_atom);
76124f412ddSSagi Grimberg 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
7621a92e17eSSagi Grimberg 
763570db170SSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id);
764b8d26b3bSNicholas Bellinger 	if (ret)
765b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
766b8d26b3bSNicholas Bellinger 
7672371e5daSSagi Grimberg 	ret = isert_rdma_post_recvl(isert_conn);
7682371e5daSSagi Grimberg 	if (ret)
7692371e5daSSagi Grimberg 		goto out_conn_dev;
7702371e5daSSagi Grimberg 
7712371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
7722371e5daSSagi Grimberg 	if (ret)
7732371e5daSSagi Grimberg 		goto out_conn_dev;
7742371e5daSSagi Grimberg 
775b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
7769fe63c88SSagi Grimberg 	list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
777b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
778b8d26b3bSNicholas Bellinger 
77924f412ddSSagi Grimberg 	isert_info("np %p: Allow accept_np to continue\n", np);
780531b7bf4SSagi Grimberg 	up(&isert_np->np_sem);
781b8d26b3bSNicholas Bellinger 	return 0;
782b8d26b3bSNicholas Bellinger 
783b8d26b3bSNicholas Bellinger out_conn_dev:
784cf8ae958SSagi Grimberg 	isert_device_put(device);
785b8d26b3bSNicholas Bellinger out_rsp_dma_map:
786ae9ea9edSSagi Grimberg 	isert_free_login_buf(isert_conn);
787b8d26b3bSNicholas Bellinger out:
788b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
7892371e5daSSagi Grimberg 	rdma_reject(cma_id, NULL, 0);
790b8d26b3bSNicholas Bellinger 	return ret;
791b8d26b3bSNicholas Bellinger }
792b8d26b3bSNicholas Bellinger 
793b8d26b3bSNicholas Bellinger static void
794b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
795b8d26b3bSNicholas Bellinger {
796b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
797b8d26b3bSNicholas Bellinger 
7984c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
799b8d26b3bSNicholas Bellinger 
800a3a5a826SSagi Grimberg 	if (device && device->use_fastreg)
801a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
80259464ef4SVu Pham 
80319e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
8044a579da2SSagi Grimberg 	if (isert_conn->conn_cm_id)
80519e2090fSSagi Grimberg 		rdma_destroy_id(isert_conn->conn_cm_id);
80619e2090fSSagi Grimberg 
807b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
8084a295baeSSagi Grimberg 		struct isert_comp *comp = isert_conn->conn_qp->recv_cq->cq_context;
8094a295baeSSagi Grimberg 
81040fc069aSSagi Grimberg 		isert_comp_put(comp);
81119e2090fSSagi Grimberg 		ib_destroy_qp(isert_conn->conn_qp);
812b8d26b3bSNicholas Bellinger 	}
813b8d26b3bSNicholas Bellinger 
814ae9ea9edSSagi Grimberg 	if (isert_conn->login_buf)
815ae9ea9edSSagi Grimberg 		isert_free_login_buf(isert_conn);
816ae9ea9edSSagi Grimberg 
817b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
818b8d26b3bSNicholas Bellinger 
819b8d26b3bSNicholas Bellinger 	if (device)
820cf8ae958SSagi Grimberg 		isert_device_put(device);
821b8d26b3bSNicholas Bellinger }
822b8d26b3bSNicholas Bellinger 
823b8d26b3bSNicholas Bellinger static void
824b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
825b8d26b3bSNicholas Bellinger {
82619e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
827c2f88b17SSagi Grimberg 
82824f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
829128e9cc8SSagi Grimberg 
8302371e5daSSagi Grimberg 	if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
83124f412ddSSagi Grimberg 		isert_warn("conn %p connect_release is running\n", isert_conn);
8322371e5daSSagi Grimberg 		return;
8332371e5daSSagi Grimberg 	}
8342371e5daSSagi Grimberg 
8352371e5daSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
8362371e5daSSagi Grimberg 	if (isert_conn->state != ISER_CONN_FULL_FEATURE)
837128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_UP;
8382371e5daSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
839b8d26b3bSNicholas Bellinger }
840b8d26b3bSNicholas Bellinger 
841b8d26b3bSNicholas Bellinger static void
842b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
843b8d26b3bSNicholas Bellinger {
844b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
845b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
846b8d26b3bSNicholas Bellinger 
8474c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
8484c22e07fSSagi Grimberg 		   current->pid);
849b8d26b3bSNicholas Bellinger 
850b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
851b8d26b3bSNicholas Bellinger }
852b8d26b3bSNicholas Bellinger 
853b8d26b3bSNicholas Bellinger static void
854b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
855b8d26b3bSNicholas Bellinger {
856b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
857b8d26b3bSNicholas Bellinger }
858b8d26b3bSNicholas Bellinger 
859954f2372SSagi Grimberg /**
860954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
861954f2372SSagi Grimberg  * @isert_conn: isert connection struct
862954f2372SSagi Grimberg  *
863954f2372SSagi Grimberg  * Notes:
864128e9cc8SSagi Grimberg  * In case the connection state is FULL_FEATURE, move state
865954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
866128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
867954f2372SSagi Grimberg  *
868954f2372SSagi Grimberg  * This routine must be called with conn_mutex held. Thus it is
869954f2372SSagi Grimberg  * safe to call multiple times.
870954f2372SSagi Grimberg  */
871954f2372SSagi Grimberg static void
872954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
873954f2372SSagi Grimberg {
874954f2372SSagi Grimberg 	int err;
875954f2372SSagi Grimberg 
876128e9cc8SSagi Grimberg 	switch (isert_conn->state) {
877128e9cc8SSagi Grimberg 	case ISER_CONN_TERMINATING:
878128e9cc8SSagi Grimberg 		break;
879128e9cc8SSagi Grimberg 	case ISER_CONN_UP:
880128e9cc8SSagi Grimberg 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
88124f412ddSSagi Grimberg 		isert_info("Terminating conn %p state %d\n",
882954f2372SSagi Grimberg 			   isert_conn, isert_conn->state);
883128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_TERMINATING;
884954f2372SSagi Grimberg 		err = rdma_disconnect(isert_conn->conn_cm_id);
885954f2372SSagi Grimberg 		if (err)
88624f412ddSSagi Grimberg 			isert_warn("Failed rdma_disconnect isert_conn %p\n",
887954f2372SSagi Grimberg 				   isert_conn);
888128e9cc8SSagi Grimberg 		break;
889128e9cc8SSagi Grimberg 	default:
89024f412ddSSagi Grimberg 		isert_warn("conn %p teminating in state %d\n",
891128e9cc8SSagi Grimberg 			   isert_conn, isert_conn->state);
892954f2372SSagi Grimberg 	}
893954f2372SSagi Grimberg }
894954f2372SSagi Grimberg 
8953b726ae2SSagi Grimberg static int
896ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
897ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
898b8d26b3bSNicholas Bellinger {
89924f412ddSSagi Grimberg 	isert_dbg("isert np %p, handling event %d\n", isert_np, event);
9003b726ae2SSagi Grimberg 
901ca6c1d82SSagi Grimberg 	switch (event) {
902ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
9033b726ae2SSagi Grimberg 		isert_np->np_cm_id = NULL;
904ca6c1d82SSagi Grimberg 		break;
905ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
906ca6c1d82SSagi Grimberg 		isert_np->np_cm_id = isert_setup_id(isert_np);
907ca6c1d82SSagi Grimberg 		if (IS_ERR(isert_np->np_cm_id)) {
90824f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
909ca6c1d82SSagi Grimberg 				  isert_np, PTR_ERR(isert_np->np_cm_id));
910ca6c1d82SSagi Grimberg 			isert_np->np_cm_id = NULL;
911ca6c1d82SSagi Grimberg 		}
912ca6c1d82SSagi Grimberg 		break;
913ca6c1d82SSagi Grimberg 	default:
91424f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
915ca6c1d82SSagi Grimberg 			  isert_np, event);
916ca6c1d82SSagi Grimberg 	}
917ca6c1d82SSagi Grimberg 
9183b726ae2SSagi Grimberg 	return -1;
9193b726ae2SSagi Grimberg }
9203b726ae2SSagi Grimberg 
921ca6c1d82SSagi Grimberg static int
922ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
923ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
924ca6c1d82SSagi Grimberg {
925ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
926ca6c1d82SSagi Grimberg 	struct isert_conn *isert_conn;
927ca6c1d82SSagi Grimberg 
928ca6c1d82SSagi Grimberg 	if (isert_np->np_cm_id == cma_id)
929ca6c1d82SSagi Grimberg 		return isert_np_cma_handler(cma_id->context, event);
930ca6c1d82SSagi Grimberg 
93119e2090fSSagi Grimberg 	isert_conn = cma_id->qp->qp_context;
932b8d26b3bSNicholas Bellinger 
933128e9cc8SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
934128e9cc8SSagi Grimberg 	isert_conn_terminate(isert_conn);
935128e9cc8SSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
936128e9cc8SSagi Grimberg 
93724f412ddSSagi Grimberg 	isert_info("conn %p completing conn_wait\n", isert_conn);
938128e9cc8SSagi Grimberg 	complete(&isert_conn->conn_wait);
9393b726ae2SSagi Grimberg 
9403b726ae2SSagi Grimberg 	return 0;
941b8d26b3bSNicholas Bellinger }
942b8d26b3bSNicholas Bellinger 
9434a579da2SSagi Grimberg static int
944954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
945954f2372SSagi Grimberg {
94619e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
947954f2372SSagi Grimberg 
9484a579da2SSagi Grimberg 	isert_conn->conn_cm_id = NULL;
949954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
9504a579da2SSagi Grimberg 
9514a579da2SSagi Grimberg 	return -1;
952954f2372SSagi Grimberg }
953954f2372SSagi Grimberg 
954b8d26b3bSNicholas Bellinger static int
955b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
956b8d26b3bSNicholas Bellinger {
957b8d26b3bSNicholas Bellinger 	int ret = 0;
958b8d26b3bSNicholas Bellinger 
9594c22e07fSSagi Grimberg 	isert_info("event %d status %d id %p np %p\n", event->event,
9604c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
961b8d26b3bSNicholas Bellinger 
962b8d26b3bSNicholas Bellinger 	switch (event->event) {
963b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
964b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
9653b726ae2SSagi Grimberg 		if (ret)
9664c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
967b8d26b3bSNicholas Bellinger 		break;
968b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
969b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
970b8d26b3bSNicholas Bellinger 		break;
97188c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
97288c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
97388c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
97488c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
975ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
976b8d26b3bSNicholas Bellinger 		break;
977954f2372SSagi Grimberg 	case RDMA_CM_EVENT_REJECTED:       /* FALLTHRU */
978954f2372SSagi Grimberg 	case RDMA_CM_EVENT_UNREACHABLE:    /* FALLTHRU */
979b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
9804a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
981954f2372SSagi Grimberg 		break;
982b8d26b3bSNicholas Bellinger 	default:
98324f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
984b8d26b3bSNicholas Bellinger 		break;
985b8d26b3bSNicholas Bellinger 	}
986b8d26b3bSNicholas Bellinger 
987b8d26b3bSNicholas Bellinger 	return ret;
988b8d26b3bSNicholas Bellinger }
989b8d26b3bSNicholas Bellinger 
990b8d26b3bSNicholas Bellinger static int
991b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
992b8d26b3bSNicholas Bellinger {
993b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
994b8d26b3bSNicholas Bellinger 	int i, ret;
995b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
996b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
997b8d26b3bSNicholas Bellinger 
998b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
999b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
1000b0a191e7SSagi Grimberg 		rx_wr->wr_id	= (uintptr_t)rx_desc;
1001b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
1002b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
1003b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
1004b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
1005b8d26b3bSNicholas Bellinger 	}
1006b8d26b3bSNicholas Bellinger 
1007b8d26b3bSNicholas Bellinger 	rx_wr--;
1008b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
1009b8d26b3bSNicholas Bellinger 
1010b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
1011b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
1012b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
1013b8d26b3bSNicholas Bellinger 	if (ret) {
101424f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
1015b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
1016b8d26b3bSNicholas Bellinger 	} else {
101711378cdbSRasmus Villemoes 		isert_dbg("Posted %d RX buffers\n", count);
1018b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
1019b8d26b3bSNicholas Bellinger 	}
1020b8d26b3bSNicholas Bellinger 	return ret;
1021b8d26b3bSNicholas Bellinger }
1022b8d26b3bSNicholas Bellinger 
1023b8d26b3bSNicholas Bellinger static int
1024b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1025b8d26b3bSNicholas Bellinger {
1026b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1027b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
1028b8d26b3bSNicholas Bellinger 	int ret;
1029b8d26b3bSNicholas Bellinger 
1030b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1031b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
1032b8d26b3bSNicholas Bellinger 
1033b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
1034b0a191e7SSagi Grimberg 	send_wr.wr_id	= (uintptr_t)tx_desc;
1035b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
1036b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
1037b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
1038b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
1039b8d26b3bSNicholas Bellinger 
1040b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
1041bdf20e72SSagi Grimberg 	if (ret)
104224f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
1043b8d26b3bSNicholas Bellinger 
1044b8d26b3bSNicholas Bellinger 	return ret;
1045b8d26b3bSNicholas Bellinger }
1046b8d26b3bSNicholas Bellinger 
1047b8d26b3bSNicholas Bellinger static void
1048b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
1049b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
1050b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
1051b8d26b3bSNicholas Bellinger {
105267cb3949SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
105367cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1054b8d26b3bSNicholas Bellinger 
1055b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1056b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
1057b8d26b3bSNicholas Bellinger 
1058b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1059b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
1060b8d26b3bSNicholas Bellinger 
1061b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
1062b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
1063b8d26b3bSNicholas Bellinger 
106467cb3949SSagi Grimberg 	if (tx_desc->tx_sg[0].lkey != device->mr->lkey) {
106567cb3949SSagi Grimberg 		tx_desc->tx_sg[0].lkey = device->mr->lkey;
106624f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1067b8d26b3bSNicholas Bellinger 	}
1068b8d26b3bSNicholas Bellinger }
1069b8d26b3bSNicholas Bellinger 
1070b8d26b3bSNicholas Bellinger static int
1071b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
1072b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
1073b8d26b3bSNicholas Bellinger {
107467cb3949SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
107567cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1076b8d26b3bSNicholas Bellinger 	u64 dma_addr;
1077b8d26b3bSNicholas Bellinger 
1078b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1079b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
1080b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
108124f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
1082b8d26b3bSNicholas Bellinger 		return -ENOMEM;
1083b8d26b3bSNicholas Bellinger 	}
1084b8d26b3bSNicholas Bellinger 
1085b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
1086b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
1087b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
108867cb3949SSagi Grimberg 	tx_desc->tx_sg[0].lkey = device->mr->lkey;
1089b8d26b3bSNicholas Bellinger 
10904c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
10914c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
10924c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
1093b8d26b3bSNicholas Bellinger 
1094b8d26b3bSNicholas Bellinger 	return 0;
1095b8d26b3bSNicholas Bellinger }
1096b8d26b3bSNicholas Bellinger 
1097b8d26b3bSNicholas Bellinger static void
109895b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
109968a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
1100b8d26b3bSNicholas Bellinger {
110195b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
110295b60f07SNicholas Bellinger 
1103b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1104b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
1105b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
110695b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
1107b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
110895b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
1109b8d26b3bSNicholas Bellinger }
1110b8d26b3bSNicholas Bellinger 
1111b8d26b3bSNicholas Bellinger static int
1112b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
1113b8d26b3bSNicholas Bellinger {
1114b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
1115b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
1116b8d26b3bSNicholas Bellinger 	int ret;
1117b8d26b3bSNicholas Bellinger 
1118b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
1119b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
1120b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
112167cb3949SSagi Grimberg 	sge.lkey = isert_conn->conn_device->mr->lkey;
1122b8d26b3bSNicholas Bellinger 
112324f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
1124b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
1125b8d26b3bSNicholas Bellinger 
1126b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1127b0a191e7SSagi Grimberg 	rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
1128b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1129b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1130b8d26b3bSNicholas Bellinger 
1131b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1132b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1133b8d26b3bSNicholas Bellinger 	if (ret) {
113424f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
1135b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1136b8d26b3bSNicholas Bellinger 	}
1137b8d26b3bSNicholas Bellinger 
1138b8d26b3bSNicholas Bellinger 	return ret;
1139b8d26b3bSNicholas Bellinger }
1140b8d26b3bSNicholas Bellinger 
1141b8d26b3bSNicholas Bellinger static int
1142b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1143b8d26b3bSNicholas Bellinger 		   u32 length)
1144b8d26b3bSNicholas Bellinger {
1145b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
114667cb3949SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
114767cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1148b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1149b8d26b3bSNicholas Bellinger 	int ret;
1150b8d26b3bSNicholas Bellinger 
1151b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1152b8d26b3bSNicholas Bellinger 
1153b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1154b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1155b8d26b3bSNicholas Bellinger 
1156b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1157b8d26b3bSNicholas Bellinger 
1158b8d26b3bSNicholas Bellinger 	if (length > 0) {
1159b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1160b8d26b3bSNicholas Bellinger 
1161b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1162b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1163b8d26b3bSNicholas Bellinger 
1164b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1165b8d26b3bSNicholas Bellinger 
1166b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1167b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1168b8d26b3bSNicholas Bellinger 
1169b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1170b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
117167cb3949SSagi Grimberg 		tx_dsg->lkey	= isert_conn->conn_device->mr->lkey;
1172b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1173b8d26b3bSNicholas Bellinger 	}
1174b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1175b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1176e0546fc1SSagi Grimberg 			if (!conn->sess->sess_ops->SessionType &&
1177e0546fc1SSagi Grimberg 			    isert_conn->conn_device->use_fastreg) {
1178570db170SSagi Grimberg 				ret = isert_conn_create_fastreg_pool(isert_conn);
1179f46d6a8aSNicholas Bellinger 				if (ret) {
118024f412ddSSagi Grimberg 					isert_err("Conn: %p failed to create"
1181f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1182f46d6a8aSNicholas Bellinger 					return ret;
1183f46d6a8aSNicholas Bellinger 				}
1184f46d6a8aSNicholas Bellinger 			}
1185f46d6a8aSNicholas Bellinger 
1186b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1187b8d26b3bSNicholas Bellinger 			if (ret)
1188b8d26b3bSNicholas Bellinger 				return ret;
1189b8d26b3bSNicholas Bellinger 
1190b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1191b8d26b3bSNicholas Bellinger 			if (ret)
1192b8d26b3bSNicholas Bellinger 				return ret;
1193b8d26b3bSNicholas Bellinger 
1194128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
11952371e5daSSagi Grimberg 			mutex_lock(&isert_conn->conn_mutex);
1196128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
11972371e5daSSagi Grimberg 			mutex_unlock(&isert_conn->conn_mutex);
1198b8d26b3bSNicholas Bellinger 			goto post_send;
1199b8d26b3bSNicholas Bellinger 		}
1200b8d26b3bSNicholas Bellinger 
1201b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1202b8d26b3bSNicholas Bellinger 		if (ret)
1203b8d26b3bSNicholas Bellinger 			return ret;
1204b8d26b3bSNicholas Bellinger 	}
1205b8d26b3bSNicholas Bellinger post_send:
1206b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1207b8d26b3bSNicholas Bellinger 	if (ret)
1208b8d26b3bSNicholas Bellinger 		return ret;
1209b8d26b3bSNicholas Bellinger 
1210b8d26b3bSNicholas Bellinger 	return 0;
1211b8d26b3bSNicholas Bellinger }
1212b8d26b3bSNicholas Bellinger 
1213b8d26b3bSNicholas Bellinger static void
12142371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1215b8d26b3bSNicholas Bellinger {
12162371e5daSSagi Grimberg 	struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
12172371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1218b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1219b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1220b8d26b3bSNicholas Bellinger 	int size;
1221b8d26b3bSNicholas Bellinger 
122224f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
12232371e5daSSagi Grimberg 
12242371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1225b8d26b3bSNicholas Bellinger 
1226b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1227b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1228b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1229b8d26b3bSNicholas Bellinger 		/*
1230b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1231b8d26b3bSNicholas Bellinger 		 * login request PDU.
1232b8d26b3bSNicholas Bellinger 		 */
1233b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1234b8d26b3bSNicholas Bellinger 		login->current_stage =
1235b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1236b8d26b3bSNicholas Bellinger 			 >> 2;
1237b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1238b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1239b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1240b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1241b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1242b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1243b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1244b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1245b8d26b3bSNicholas Bellinger 	}
1246b8d26b3bSNicholas Bellinger 
1247b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1248b8d26b3bSNicholas Bellinger 
1249b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
12504c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
12514c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
12524c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
1253b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1254b8d26b3bSNicholas Bellinger 
12556faaa85fSNicholas Bellinger 	if (login->first_request) {
1256b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
12576faaa85fSNicholas Bellinger 		return;
12586faaa85fSNicholas Bellinger 	}
12596faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1260b8d26b3bSNicholas Bellinger }
1261b8d26b3bSNicholas Bellinger 
1262b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1263676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1264b8d26b3bSNicholas Bellinger {
12656700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1266b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1267d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1268b8d26b3bSNicholas Bellinger 
1269676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1270d703ce2fSNicholas Bellinger 	if (!cmd) {
127124f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1272b8d26b3bSNicholas Bellinger 		return NULL;
1273b8d26b3bSNicholas Bellinger 	}
1274d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1275b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1276d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1277b8d26b3bSNicholas Bellinger 
1278d703ce2fSNicholas Bellinger 	return cmd;
1279b8d26b3bSNicholas Bellinger }
1280b8d26b3bSNicholas Bellinger 
1281b8d26b3bSNicholas Bellinger static int
1282b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1283d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1284d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1285b8d26b3bSNicholas Bellinger {
1286b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1287b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1288b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1289b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1290b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1291b8d26b3bSNicholas Bellinger 
1292b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1293b8d26b3bSNicholas Bellinger 	if (rc < 0)
1294b8d26b3bSNicholas Bellinger 		return rc;
1295b8d26b3bSNicholas Bellinger 
1296b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1297b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1298b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1299b8d26b3bSNicholas Bellinger 
1300b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1301b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1302b8d26b3bSNicholas Bellinger 		return 0;
1303b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1304b8d26b3bSNicholas Bellinger 		dump_payload = true;
1305b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1306b8d26b3bSNicholas Bellinger 	}
1307b8d26b3bSNicholas Bellinger 
1308b8d26b3bSNicholas Bellinger 	if (!imm_data)
1309b8d26b3bSNicholas Bellinger 		return 0;
1310b8d26b3bSNicholas Bellinger 
1311b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1312b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1313b8d26b3bSNicholas Bellinger 
131424f412ddSSagi Grimberg 	isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1315b8d26b3bSNicholas Bellinger 		  sg, sg_nents, &rx_desc->data[0], imm_data_len);
1316b8d26b3bSNicholas Bellinger 
1317b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1318b8d26b3bSNicholas Bellinger 
1319b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1320b8d26b3bSNicholas Bellinger 
1321b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1322b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1323b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1324b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1325b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1326b8d26b3bSNicholas Bellinger 	}
1327b8d26b3bSNicholas Bellinger 
1328b8d26b3bSNicholas Bellinger sequence_cmd:
1329561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1330b8d26b3bSNicholas Bellinger 
1331b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1332b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
13336cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
13346cc44a6fSNicholas Bellinger 		target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1335b8d26b3bSNicholas Bellinger 
1336b8d26b3bSNicholas Bellinger 	return 0;
1337b8d26b3bSNicholas Bellinger }
1338b8d26b3bSNicholas Bellinger 
1339b8d26b3bSNicholas Bellinger static int
1340b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1341b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1342b8d26b3bSNicholas Bellinger {
1343b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1344b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1345b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1346b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1347b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1348b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1349b8d26b3bSNicholas Bellinger 
1350b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1351b8d26b3bSNicholas Bellinger 	if (rc < 0)
1352b8d26b3bSNicholas Bellinger 		return rc;
1353b8d26b3bSNicholas Bellinger 	else if (!cmd)
1354b8d26b3bSNicholas Bellinger 		return 0;
1355b8d26b3bSNicholas Bellinger 	/*
1356b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1357b8d26b3bSNicholas Bellinger 	 */
1358b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
135924f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1360b8d26b3bSNicholas Bellinger 		dump_stack();
1361b8d26b3bSNicholas Bellinger 		return -1;
1362b8d26b3bSNicholas Bellinger 	}
1363b8d26b3bSNicholas Bellinger 
13644c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
13654c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
13664c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
13674c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1368b8d26b3bSNicholas Bellinger 
1369b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1370b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1371b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1372b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1373b8d26b3bSNicholas Bellinger 	/*
1374b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1375b8d26b3bSNicholas Bellinger 	 */
1376b8d26b3bSNicholas Bellinger 	if (page_off) {
13774c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1378b8d26b3bSNicholas Bellinger 		dump_stack();
1379b8d26b3bSNicholas Bellinger 		return -1;
1380b8d26b3bSNicholas Bellinger 	}
13814c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
13824c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
13834c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1384b8d26b3bSNicholas Bellinger 
1385b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1386b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1387b8d26b3bSNicholas Bellinger 
1388b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1389b8d26b3bSNicholas Bellinger 	if (rc < 0)
1390b8d26b3bSNicholas Bellinger 		return rc;
1391b8d26b3bSNicholas Bellinger 
1392b8d26b3bSNicholas Bellinger 	return 0;
1393b8d26b3bSNicholas Bellinger }
1394b8d26b3bSNicholas Bellinger 
1395b8d26b3bSNicholas Bellinger static int
1396778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1397d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1398d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1399778de368SNicholas Bellinger {
1400778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1401778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1402778de368SNicholas Bellinger 	int rc;
1403778de368SNicholas Bellinger 
1404778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1405778de368SNicholas Bellinger 	if (rc < 0)
1406778de368SNicholas Bellinger 		return rc;
1407778de368SNicholas Bellinger 	/*
1408778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1409778de368SNicholas Bellinger 	 */
1410778de368SNicholas Bellinger 
1411778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1412778de368SNicholas Bellinger }
1413778de368SNicholas Bellinger 
1414778de368SNicholas Bellinger static int
1415adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1416d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1417d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1418adb54c29SNicholas Bellinger {
1419adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1420adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1421adb54c29SNicholas Bellinger 	int rc;
1422b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1423adb54c29SNicholas Bellinger 
1424adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1425adb54c29SNicholas Bellinger 	if (rc < 0)
1426adb54c29SNicholas Bellinger 		return rc;
1427adb54c29SNicholas Bellinger 
1428b44a2b67SSagi Grimberg 	if (payload_length) {
1429adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
1430adb54c29SNicholas Bellinger 		if (!text_in) {
143124f412ddSSagi Grimberg 			isert_err("Unable to allocate text_in of payload_length: %u\n",
1432adb54c29SNicholas Bellinger 				  payload_length);
1433adb54c29SNicholas Bellinger 			return -ENOMEM;
1434adb54c29SNicholas Bellinger 		}
1435b44a2b67SSagi Grimberg 	}
1436adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1437adb54c29SNicholas Bellinger 
1438adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1439adb54c29SNicholas Bellinger 
1440adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1441adb54c29SNicholas Bellinger }
1442adb54c29SNicholas Bellinger 
1443adb54c29SNicholas Bellinger static int
1444b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1445b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1446b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1447b8d26b3bSNicholas Bellinger {
1448b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1449b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1450b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1451b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1452b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1453b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1454b8d26b3bSNicholas Bellinger 
1455fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1456ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
145724f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1458ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1459ca40d24eSNicholas Bellinger 		return 0;
1460ca40d24eSNicholas Bellinger 	}
1461ca40d24eSNicholas Bellinger 
1462b8d26b3bSNicholas Bellinger 	switch (opcode) {
1463b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1464676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1465b8d26b3bSNicholas Bellinger 		if (!cmd)
1466b8d26b3bSNicholas Bellinger 			break;
1467b8d26b3bSNicholas Bellinger 
1468d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1469b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1470b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1471b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1472b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1473b8d26b3bSNicholas Bellinger 
1474d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1475b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1476b8d26b3bSNicholas Bellinger 		break;
1477b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1478676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1479b8d26b3bSNicholas Bellinger 		if (!cmd)
1480b8d26b3bSNicholas Bellinger 			break;
1481b8d26b3bSNicholas Bellinger 
1482d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1483d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1484778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1485b8d26b3bSNicholas Bellinger 		break;
1486b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1487b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1488b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1489b8d26b3bSNicholas Bellinger 		break;
1490b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1491676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1492b8d26b3bSNicholas Bellinger 		if (!cmd)
1493b8d26b3bSNicholas Bellinger 			break;
1494b8d26b3bSNicholas Bellinger 
1495b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1496b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1497b8d26b3bSNicholas Bellinger 		break;
1498b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1499676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1500b8d26b3bSNicholas Bellinger 		if (!cmd)
1501b8d26b3bSNicholas Bellinger 			break;
1502b8d26b3bSNicholas Bellinger 
1503b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1504b8d26b3bSNicholas Bellinger 		break;
1505adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1506e4f4e801SSagi Grimberg 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) {
1507e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
1508e4f4e801SSagi Grimberg 			if (!cmd)
1509e4f4e801SSagi Grimberg 				break;
1510e4f4e801SSagi Grimberg 		} else {
1511676687c6SNicholas Bellinger 			cmd = isert_allocate_cmd(conn);
1512adb54c29SNicholas Bellinger 			if (!cmd)
1513adb54c29SNicholas Bellinger 				break;
1514e4f4e801SSagi Grimberg 		}
1515adb54c29SNicholas Bellinger 
1516d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1517d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1518adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1519adb54c29SNicholas Bellinger 		break;
1520b8d26b3bSNicholas Bellinger 	default:
152124f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1522b8d26b3bSNicholas Bellinger 		dump_stack();
1523b8d26b3bSNicholas Bellinger 		break;
1524b8d26b3bSNicholas Bellinger 	}
1525b8d26b3bSNicholas Bellinger 
1526b8d26b3bSNicholas Bellinger 	return ret;
1527b8d26b3bSNicholas Bellinger }
1528b8d26b3bSNicholas Bellinger 
1529b8d26b3bSNicholas Bellinger static void
1530b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1531b8d26b3bSNicholas Bellinger {
1532b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1533b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1534b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1535b8d26b3bSNicholas Bellinger 	int rc;
1536b8d26b3bSNicholas Bellinger 
1537b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1538b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1539b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1540b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1541b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
15424c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1543b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1544b8d26b3bSNicholas Bellinger 		}
1545b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1546b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1547b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
15484c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1549b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1550b8d26b3bSNicholas Bellinger 		}
1551b8d26b3bSNicholas Bellinger 
155224f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1553b8d26b3bSNicholas Bellinger 		break;
1554b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
155524f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1556b8d26b3bSNicholas Bellinger 		break;
1557b8d26b3bSNicholas Bellinger 	default:
155824f412ddSSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1559b8d26b3bSNicholas Bellinger 		break;
1560b8d26b3bSNicholas Bellinger 	}
1561b8d26b3bSNicholas Bellinger 
1562b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1563b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1564b8d26b3bSNicholas Bellinger }
1565b8d26b3bSNicholas Bellinger 
1566b8d26b3bSNicholas Bellinger static void
15677748681bSSagi Grimberg isert_rcv_completion(struct iser_rx_desc *desc,
15687748681bSSagi Grimberg 		     struct isert_conn *isert_conn,
15694a295baeSSagi Grimberg 		     u32 xfer_len)
1570b8d26b3bSNicholas Bellinger {
1571b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1572b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1573b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1574b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1575b8d26b3bSNicholas Bellinger 
1576b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1577b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1578b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
15794c22e07fSSagi Grimberg 		isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1580b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1581b8d26b3bSNicholas Bellinger 	} else {
1582b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1583b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
15844c22e07fSSagi Grimberg 		isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1585b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1586b8d26b3bSNicholas Bellinger 	}
1587b8d26b3bSNicholas Bellinger 
1588b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1589b8d26b3bSNicholas Bellinger 
1590b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
159124f412ddSSagi Grimberg 	isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1592b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1593b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1594b8d26b3bSNicholas Bellinger 
15952371e5daSSagi Grimberg 	if ((char *)desc == isert_conn->login_req_buf) {
15962371e5daSSagi Grimberg 		isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
15972371e5daSSagi Grimberg 		if (isert_conn->conn) {
15982371e5daSSagi Grimberg 			struct iscsi_login *login = isert_conn->conn->conn_login;
15992371e5daSSagi Grimberg 
16002371e5daSSagi Grimberg 			if (login && !login->first_request)
16012371e5daSSagi Grimberg 				isert_rx_login_req(isert_conn);
16022371e5daSSagi Grimberg 		}
16032371e5daSSagi Grimberg 		mutex_lock(&isert_conn->conn_mutex);
16042371e5daSSagi Grimberg 		complete(&isert_conn->login_req_comp);
16052371e5daSSagi Grimberg 		mutex_unlock(&isert_conn->conn_mutex);
16062371e5daSSagi Grimberg 	} else {
1607b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
16082371e5daSSagi Grimberg 	}
1609b8d26b3bSNicholas Bellinger 
1610b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1611b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1612b8d26b3bSNicholas Bellinger 
1613b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
16144c22e07fSSagi Grimberg 	isert_dbg("Decremented post_recv_buf_count: %d\n",
1615b8d26b3bSNicholas Bellinger 		  isert_conn->post_recv_buf_count);
1616b8d26b3bSNicholas Bellinger 
1617b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1618b8d26b3bSNicholas Bellinger 		return;
1619b8d26b3bSNicholas Bellinger 
1620b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1621b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1622b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1623b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1624b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1625b8d26b3bSNicholas Bellinger 		if (err) {
162624f412ddSSagi Grimberg 			isert_err("isert_post_recv() count: %d failed, %d\n",
1627b8d26b3bSNicholas Bellinger 			       count, err);
1628b8d26b3bSNicholas Bellinger 		}
1629b8d26b3bSNicholas Bellinger 	}
1630b8d26b3bSNicholas Bellinger }
1631b8d26b3bSNicholas Bellinger 
1632e3d7e4c3SSagi Grimberg static int
1633e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1634e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1635e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1636e3d7e4c3SSagi Grimberg {
1637e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1638e3d7e4c3SSagi Grimberg 
1639e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1640e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1641e3d7e4c3SSagi Grimberg 
1642e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1643e3d7e4c3SSagi Grimberg 	data->offset = offset;
1644e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1645e3d7e4c3SSagi Grimberg 
1646e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1647e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1648e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1649e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1650e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1651e3d7e4c3SSagi Grimberg 
1652e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1653e3d7e4c3SSagi Grimberg 					data->dma_dir);
1654e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
165524f412ddSSagi Grimberg 		isert_err("Cmd: unable to dma map SGs %p\n", sg);
1656e3d7e4c3SSagi Grimberg 		return -EINVAL;
1657e3d7e4c3SSagi Grimberg 	}
1658e3d7e4c3SSagi Grimberg 
165924f412ddSSagi Grimberg 	isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1660e3d7e4c3SSagi Grimberg 		  isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1661e3d7e4c3SSagi Grimberg 
1662e3d7e4c3SSagi Grimberg 	return 0;
1663e3d7e4c3SSagi Grimberg }
1664e3d7e4c3SSagi Grimberg 
1665e3d7e4c3SSagi Grimberg static void
1666e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1667e3d7e4c3SSagi Grimberg {
1668e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1669e3d7e4c3SSagi Grimberg 
1670e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1671e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1672e3d7e4c3SSagi Grimberg }
1673e3d7e4c3SSagi Grimberg 
1674e3d7e4c3SSagi Grimberg 
1675e3d7e4c3SSagi Grimberg 
1676b8d26b3bSNicholas Bellinger static void
1677b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1678b8d26b3bSNicholas Bellinger {
1679b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1680b8d26b3bSNicholas Bellinger 
16814c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1682e3d7e4c3SSagi Grimberg 
1683e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
16844c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1685e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1686b8d26b3bSNicholas Bellinger 	}
1687b8d26b3bSNicholas Bellinger 
168890ecc6e2SVu Pham 	if (wr->send_wr) {
16894c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free send_wr\n", isert_cmd);
1690b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1691b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
169290ecc6e2SVu Pham 	}
1693b8d26b3bSNicholas Bellinger 
169490ecc6e2SVu Pham 	if (wr->ib_sge) {
16954c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
169690ecc6e2SVu Pham 		kfree(wr->ib_sge);
169790ecc6e2SVu Pham 		wr->ib_sge = NULL;
169890ecc6e2SVu Pham 	}
1699b8d26b3bSNicholas Bellinger }
1700b8d26b3bSNicholas Bellinger 
1701b8d26b3bSNicholas Bellinger static void
1702a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
170359464ef4SVu Pham {
170459464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
170559464ef4SVu Pham 
17064c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
170759464ef4SVu Pham 
170859464ef4SVu Pham 	if (wr->fr_desc) {
17094c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
17109e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
17119e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
17129e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
17139e961ae7SSagi Grimberg 		}
171459464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1715a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
171659464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
171759464ef4SVu Pham 		wr->fr_desc = NULL;
171859464ef4SVu Pham 	}
171959464ef4SVu Pham 
1720e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
17214c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1722e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
172359464ef4SVu Pham 	}
172459464ef4SVu Pham 
172559464ef4SVu Pham 	wr->ib_sge = NULL;
172659464ef4SVu Pham 	wr->send_wr = NULL;
172759464ef4SVu Pham }
172859464ef4SVu Pham 
172959464ef4SVu Pham static void
173003e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1731b8d26b3bSNicholas Bellinger {
1732d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1733b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1734186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1735d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1736e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1737b8d26b3bSNicholas Bellinger 
17384c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1739b8d26b3bSNicholas Bellinger 
1740b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1741b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1742b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1743b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17445159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1745b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1746b8d26b3bSNicholas Bellinger 
174703e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1748b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
174903e7848aSNicholas Bellinger 			/*
175003e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
175103e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
175203e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
175303e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
175403e7848aSNicholas Bellinger 			 */
175503e7848aSNicholas Bellinger 			if (comp_err &&
175603e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
175703e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
175803e7848aSNicholas Bellinger 
175903e7848aSNicholas Bellinger 				target_put_sess_cmd(se_cmd->se_sess, se_cmd);
176003e7848aSNicholas Bellinger 			}
176103e7848aSNicholas Bellinger 		}
1762b8d26b3bSNicholas Bellinger 
1763d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1764186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1765186a9647SNicholas Bellinger 		break;
1766b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1767186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1768186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17695159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1770186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1771186a9647SNicholas Bellinger 
1772b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1773b8d26b3bSNicholas Bellinger 		break;
1774b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1775b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1776adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1777e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1778e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1779e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1780e4f4e801SSagi Grimberg 			break;
1781e4f4e801SSagi Grimberg 
1782b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1783b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17845159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1785b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1786b8d26b3bSNicholas Bellinger 
1787b8d26b3bSNicholas Bellinger 		/*
1788b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1789b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1790b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1791b8d26b3bSNicholas Bellinger 		 */
1792b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
179311378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
17943df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1795b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1796b8d26b3bSNicholas Bellinger 			break;
1797b8d26b3bSNicholas Bellinger 		}
1798b8d26b3bSNicholas Bellinger 		/*
1799b8d26b3bSNicholas Bellinger 		 * Fall-through
1800b8d26b3bSNicholas Bellinger 		 */
1801b8d26b3bSNicholas Bellinger 	default:
1802d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1803b8d26b3bSNicholas Bellinger 		break;
1804b8d26b3bSNicholas Bellinger 	}
1805b8d26b3bSNicholas Bellinger }
1806b8d26b3bSNicholas Bellinger 
1807b8d26b3bSNicholas Bellinger static void
1808b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1809b8d26b3bSNicholas Bellinger {
1810b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
18114c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1812b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1813b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1814b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1815b8d26b3bSNicholas Bellinger 	}
1816b8d26b3bSNicholas Bellinger }
1817b8d26b3bSNicholas Bellinger 
1818b8d26b3bSNicholas Bellinger static void
1819b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
182003e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1821b8d26b3bSNicholas Bellinger {
1822dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
18234c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1824dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1825dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1826dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1827b8d26b3bSNicholas Bellinger 	}
1828b8d26b3bSNicholas Bellinger 
1829b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
183003e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1831b8d26b3bSNicholas Bellinger }
1832b8d26b3bSNicholas Bellinger 
183396b7973eSSagi Grimberg static int
183496b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1835f93f3a70SSagi Grimberg {
18369e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
183796b7973eSSagi Grimberg 	int ret;
1838f93f3a70SSagi Grimberg 
183996b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
18409e961ae7SSagi Grimberg 	if (ret) {
184124f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
18429e961ae7SSagi Grimberg 		goto fail_mr_status;
18439e961ae7SSagi Grimberg 	}
184496b7973eSSagi Grimberg 
18459e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
184696b7973eSSagi Grimberg 		u64 sec_offset_err;
18479e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
18489e961ae7SSagi Grimberg 
18499e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
18509e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
18519e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
18529e961ae7SSagi Grimberg 			break;
18539e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
18549e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
18559e961ae7SSagi Grimberg 			break;
18569e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
18579e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
18589e961ae7SSagi Grimberg 			break;
18599e961ae7SSagi Grimberg 		}
186096b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
186196b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
186296b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
18639e961ae7SSagi Grimberg 
18644c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
18659e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
18669e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
18679e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
18689e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
18699e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
18709e961ae7SSagi Grimberg 		ret = 1;
18719e961ae7SSagi Grimberg 	}
18729e961ae7SSagi Grimberg 
18739e961ae7SSagi Grimberg fail_mr_status:
187496b7973eSSagi Grimberg 	return ret;
187596b7973eSSagi Grimberg }
187696b7973eSSagi Grimberg 
187796b7973eSSagi Grimberg static void
187896b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
187996b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
188096b7973eSSagi Grimberg {
188196b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
188296b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
188396b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
188496b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
188596b7973eSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
188696b7973eSSagi Grimberg 	int ret = 0;
188796b7973eSSagi Grimberg 
188896b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
188996b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
189096b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
189196b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
189296b7973eSSagi Grimberg 	}
189396b7973eSSagi Grimberg 
1894f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1895897bb2c9SSagi Grimberg 	wr->send_wr_num = 0;
18969e961ae7SSagi Grimberg 	if (ret)
18979e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18989e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
18999e961ae7SSagi Grimberg 	else
1900f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1901f93f3a70SSagi Grimberg }
1902f93f3a70SSagi Grimberg 
1903f93f3a70SSagi Grimberg static void
1904b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1905b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1906b8d26b3bSNicholas Bellinger {
1907b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1908d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1909b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
191090ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1911d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
19125bac4b1aSSagi Grimberg 	int ret = 0;
1913b8d26b3bSNicholas Bellinger 
19149e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
191596b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
191696b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
191796b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
19189e961ae7SSagi Grimberg 	}
19199e961ae7SSagi Grimberg 
1920b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1921d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1922e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1923b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1924b8d26b3bSNicholas Bellinger 
192524f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1926b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1927b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1928b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1929b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1930b8d26b3bSNicholas Bellinger 
1931364189f0SSagi Grimberg 	if (ret) {
1932364189f0SSagi Grimberg 		target_put_sess_cmd(se_cmd->se_sess, se_cmd);
19335bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
19345bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
1935364189f0SSagi Grimberg 	} else {
1936b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1937b8d26b3bSNicholas Bellinger 	}
1938364189f0SSagi Grimberg }
1939b8d26b3bSNicholas Bellinger 
1940b8d26b3bSNicholas Bellinger static void
1941b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1942b8d26b3bSNicholas Bellinger {
1943b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1944b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1945b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1946b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1947d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1948b8d26b3bSNicholas Bellinger 
19494c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
19504c22e07fSSagi Grimberg 
1951b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1952b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1953b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
195410633c37SSagi Grimberg 	case ISTATE_SEND_REJECT:   /* FALLTHRU */
195510633c37SSagi Grimberg 	case ISTATE_SEND_TEXTRSP:  /* FALLTHRU */
1956b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
19574c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
19584c22e07fSSagi Grimberg 				     ib_dev, false);
19593df8f68aSNicholas Bellinger 		break;
1960b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1961b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1962b8d26b3bSNicholas Bellinger 		break;
1963b8d26b3bSNicholas Bellinger 	default:
19644c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1965b8d26b3bSNicholas Bellinger 		dump_stack();
1966b8d26b3bSNicholas Bellinger 		break;
1967b8d26b3bSNicholas Bellinger 	}
1968b8d26b3bSNicholas Bellinger }
1969b8d26b3bSNicholas Bellinger 
1970b8d26b3bSNicholas Bellinger static void
1971b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1972b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1973b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1974b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1975b8d26b3bSNicholas Bellinger {
1976d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1977b8d26b3bSNicholas Bellinger 
1978b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
19793df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1980adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1981adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1982b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1983b8d26b3bSNicholas Bellinger 
1984b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1985b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1986b8d26b3bSNicholas Bellinger 		return;
1987b8d26b3bSNicholas Bellinger 	}
1988897bb2c9SSagi Grimberg 
1989b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
199003e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1991b8d26b3bSNicholas Bellinger }
1992b8d26b3bSNicholas Bellinger 
1993b8d26b3bSNicholas Bellinger static void
19947748681bSSagi Grimberg isert_snd_completion(struct iser_tx_desc *tx_desc,
1995b8d26b3bSNicholas Bellinger 		      struct isert_conn *isert_conn)
1996b8d26b3bSNicholas Bellinger {
1997b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1998b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1999b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
2000b8d26b3bSNicholas Bellinger 
2001b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
2002b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
2003b8d26b3bSNicholas Bellinger 		return;
2004b8d26b3bSNicholas Bellinger 	}
2005b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
2006b8d26b3bSNicholas Bellinger 
20074c22e07fSSagi Grimberg 	isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
20084c22e07fSSagi Grimberg 
2009b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
2010b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
2011b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
2012b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
2013b8d26b3bSNicholas Bellinger 		break;
2014b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
2015f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
2016b8d26b3bSNicholas Bellinger 		break;
2017b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
2018b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
2019b8d26b3bSNicholas Bellinger 		break;
2020b8d26b3bSNicholas Bellinger 	default:
20214c22e07fSSagi Grimberg 		isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
2022b8d26b3bSNicholas Bellinger 		dump_stack();
2023b8d26b3bSNicholas Bellinger 		break;
2024b8d26b3bSNicholas Bellinger 	}
2025b8d26b3bSNicholas Bellinger }
2026b8d26b3bSNicholas Bellinger 
20276f0fae3dSSagi Grimberg /**
20286f0fae3dSSagi Grimberg  * is_isert_tx_desc() - Indicate if the completion wr_id
20296f0fae3dSSagi Grimberg  *     is a TX descriptor or not.
20306f0fae3dSSagi Grimberg  * @isert_conn: iser connection
20316f0fae3dSSagi Grimberg  * @wr_id: completion WR identifier
20326f0fae3dSSagi Grimberg  *
20336f0fae3dSSagi Grimberg  * Since we cannot rely on wc opcode in FLUSH errors
20346f0fae3dSSagi Grimberg  * we must work around it by checking if the wr_id address
20356f0fae3dSSagi Grimberg  * falls in the iser connection rx_descs buffer. If so
20366f0fae3dSSagi Grimberg  * it is an RX descriptor, otherwize it is a TX.
20376f0fae3dSSagi Grimberg  */
20386f0fae3dSSagi Grimberg static inline bool
20396f0fae3dSSagi Grimberg is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
2040b8d26b3bSNicholas Bellinger {
20416f0fae3dSSagi Grimberg 	void *start = isert_conn->conn_rx_descs;
20426f0fae3dSSagi Grimberg 	int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->conn_rx_descs);
20436f0fae3dSSagi Grimberg 
20446f0fae3dSSagi Grimberg 	if (wr_id >= start && wr_id < start + len)
20456f0fae3dSSagi Grimberg 		return false;
20466f0fae3dSSagi Grimberg 
20476f0fae3dSSagi Grimberg 	return true;
20486f0fae3dSSagi Grimberg }
20496f0fae3dSSagi Grimberg 
20506f0fae3dSSagi Grimberg static void
20516f0fae3dSSagi Grimberg isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
20526f0fae3dSSagi Grimberg {
2053bdf20e72SSagi Grimberg 	if (wc->wr_id == ISER_BEACON_WRID) {
205424f412ddSSagi Grimberg 		isert_info("conn %p completing conn_wait_comp_err\n",
2055bdf20e72SSagi Grimberg 			   isert_conn);
2056bdf20e72SSagi Grimberg 		complete(&isert_conn->conn_wait_comp_err);
2057ed4520aeSNicholas Bellinger 	} else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
2058b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2059df43debdSSagi Grimberg 		struct isert_cmd *isert_cmd;
20606f0fae3dSSagi Grimberg 		struct iser_tx_desc *desc;
2061b8d26b3bSNicholas Bellinger 
20626f0fae3dSSagi Grimberg 		desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20636f0fae3dSSagi Grimberg 		isert_cmd = desc->isert_cmd;
2064b8d26b3bSNicholas Bellinger 		if (!isert_cmd)
2065df43debdSSagi Grimberg 			isert_unmap_tx_desc(desc, ib_dev);
2066b8d26b3bSNicholas Bellinger 		else
2067df43debdSSagi Grimberg 			isert_completion_put(desc, isert_cmd, ib_dev, true);
2068df43debdSSagi Grimberg 	} else {
2069df43debdSSagi Grimberg 		isert_conn->post_recv_buf_count--;
2070bdf20e72SSagi Grimberg 		if (!isert_conn->post_recv_buf_count)
20719d49f5e2SSagi Grimberg 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2072b8d26b3bSNicholas Bellinger 	}
2073df43debdSSagi Grimberg }
2074b8d26b3bSNicholas Bellinger 
2075b8d26b3bSNicholas Bellinger static void
20766f0fae3dSSagi Grimberg isert_handle_wc(struct ib_wc *wc)
2077b8d26b3bSNicholas Bellinger {
2078b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2079b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
20806f0fae3dSSagi Grimberg 	struct iser_rx_desc *rx_desc;
2081b8d26b3bSNicholas Bellinger 
20826f0fae3dSSagi Grimberg 	isert_conn = wc->qp->qp_context;
20836f0fae3dSSagi Grimberg 	if (likely(wc->status == IB_WC_SUCCESS)) {
20846f0fae3dSSagi Grimberg 		if (wc->opcode == IB_WC_RECV) {
20856f0fae3dSSagi Grimberg 			rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
20867748681bSSagi Grimberg 			isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
2087b8d26b3bSNicholas Bellinger 		} else {
20886f0fae3dSSagi Grimberg 			tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20897748681bSSagi Grimberg 			isert_snd_completion(tx_desc, isert_conn);
2090b8d26b3bSNicholas Bellinger 		}
20916f0fae3dSSagi Grimberg 	} else {
20926f0fae3dSSagi Grimberg 		if (wc->status != IB_WC_WR_FLUSH_ERR)
209324f412ddSSagi Grimberg 			isert_err("wr id %llx status %d vend_err %x\n",
20946f0fae3dSSagi Grimberg 				  wc->wr_id, wc->status, wc->vendor_err);
20956f0fae3dSSagi Grimberg 		else
209624f412ddSSagi Grimberg 			isert_dbg("flush error: wr id %llx\n", wc->wr_id);
2097b8d26b3bSNicholas Bellinger 
20986f0fae3dSSagi Grimberg 		if (wc->wr_id != ISER_FASTREG_LI_WRID)
20996f0fae3dSSagi Grimberg 			isert_cq_comp_err(isert_conn, wc);
21006f0fae3dSSagi Grimberg 	}
2101b8d26b3bSNicholas Bellinger }
2102b8d26b3bSNicholas Bellinger 
2103b8d26b3bSNicholas Bellinger static void
21046f0fae3dSSagi Grimberg isert_cq_work(struct work_struct *work)
2105b8d26b3bSNicholas Bellinger {
210637d9fe80SSagi Grimberg 	enum { isert_poll_budget = 65536 };
21074a295baeSSagi Grimberg 	struct isert_comp *comp = container_of(work, struct isert_comp,
21086f0fae3dSSagi Grimberg 					       work);
210936ea63b5SSagi Grimberg 	struct ib_wc *const wcs = comp->wcs;
211036ea63b5SSagi Grimberg 	int i, n, completed = 0;
2111b8d26b3bSNicholas Bellinger 
211236ea63b5SSagi Grimberg 	while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
211336ea63b5SSagi Grimberg 		for (i = 0; i < n; i++)
211436ea63b5SSagi Grimberg 			isert_handle_wc(&wcs[i]);
2115b8d26b3bSNicholas Bellinger 
211636ea63b5SSagi Grimberg 		completed += n;
211736ea63b5SSagi Grimberg 		if (completed >= isert_poll_budget)
211837d9fe80SSagi Grimberg 			break;
211937d9fe80SSagi Grimberg 	}
212037d9fe80SSagi Grimberg 
21216f0fae3dSSagi Grimberg 	ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
2122b8d26b3bSNicholas Bellinger }
2123b8d26b3bSNicholas Bellinger 
2124b8d26b3bSNicholas Bellinger static void
21256f0fae3dSSagi Grimberg isert_cq_callback(struct ib_cq *cq, void *context)
2126b8d26b3bSNicholas Bellinger {
21274a295baeSSagi Grimberg 	struct isert_comp *comp = context;
2128b8d26b3bSNicholas Bellinger 
21296f0fae3dSSagi Grimberg 	queue_work(isert_comp_wq, &comp->work);
2130b8d26b3bSNicholas Bellinger }
2131b8d26b3bSNicholas Bellinger 
2132b8d26b3bSNicholas Bellinger static int
2133b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2134b8d26b3bSNicholas Bellinger {
2135b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2136b8d26b3bSNicholas Bellinger 	int ret;
2137b8d26b3bSNicholas Bellinger 
2138b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2139b8d26b3bSNicholas Bellinger 			   &wr_failed);
2140b8d26b3bSNicholas Bellinger 	if (ret) {
214124f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
2142b8d26b3bSNicholas Bellinger 		return ret;
2143b8d26b3bSNicholas Bellinger 	}
2144b8d26b3bSNicholas Bellinger 	return ret;
2145b8d26b3bSNicholas Bellinger }
2146b8d26b3bSNicholas Bellinger 
2147b8d26b3bSNicholas Bellinger static int
2148b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2149b8d26b3bSNicholas Bellinger {
2150d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
21516700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2152b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2153b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2154b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2155b8d26b3bSNicholas Bellinger 
2156b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2157b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2158b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2159b8d26b3bSNicholas Bellinger 	/*
2160b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2161b8d26b3bSNicholas Bellinger 	 */
2162b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2163b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2164b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
216567cb3949SSagi Grimberg 		struct isert_device *device = isert_conn->conn_device;
216667cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2167b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2168dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2169b8d26b3bSNicholas Bellinger 
2170b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2171b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2172b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2173b8d26b3bSNicholas Bellinger 
2174b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2175b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2176dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2177b8d26b3bSNicholas Bellinger 
2178dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2179dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2180b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2181b8d26b3bSNicholas Bellinger 
2182dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2183dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2184dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
218567cb3949SSagi Grimberg 		tx_dsg->lkey	= device->mr->lkey;
2186b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2187b8d26b3bSNicholas Bellinger 	}
2188b8d26b3bSNicholas Bellinger 
218968a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2190b8d26b3bSNicholas Bellinger 
21914c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
2192b8d26b3bSNicholas Bellinger 
2193b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2194b8d26b3bSNicholas Bellinger }
2195b8d26b3bSNicholas Bellinger 
2196131e6abcSNicholas Bellinger static void
2197131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2198131e6abcSNicholas Bellinger {
2199131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22006700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2201131e6abcSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2202131e6abcSNicholas Bellinger 
2203131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2204131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2205131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2206131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2207131e6abcSNicholas Bellinger 
2208131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2209131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2210131e6abcSNicholas Bellinger 
2211131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2212131e6abcSNicholas Bellinger }
2213131e6abcSNicholas Bellinger 
2214e70beee7SNicholas Bellinger static enum target_prot_op
2215e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2216e70beee7SNicholas Bellinger {
22176700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2218e70beee7SNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2219e70beee7SNicholas Bellinger 
222023a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
222123a548eeSSagi Grimberg 		if (device->pi_capable) {
222224f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
222323a548eeSSagi Grimberg 			isert_conn->pi_support = true;
2224e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
222523a548eeSSagi Grimberg 		}
222623a548eeSSagi Grimberg 	}
222723a548eeSSagi Grimberg 
222824f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
222923a548eeSSagi Grimberg 	isert_conn->pi_support = false;
2230e70beee7SNicholas Bellinger 
2231e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2232e70beee7SNicholas Bellinger }
2233e70beee7SNicholas Bellinger 
2234b8d26b3bSNicholas Bellinger static int
2235b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2236b8d26b3bSNicholas Bellinger 		bool nopout_response)
2237b8d26b3bSNicholas Bellinger {
2238d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22396700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2240b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2241b8d26b3bSNicholas Bellinger 
2242b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2243b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2244b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2245b8d26b3bSNicholas Bellinger 			       nopout_response);
2246b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
224768a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2248b8d26b3bSNicholas Bellinger 
22494c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
2250b8d26b3bSNicholas Bellinger 
2251b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2252b8d26b3bSNicholas Bellinger }
2253b8d26b3bSNicholas Bellinger 
2254b8d26b3bSNicholas Bellinger static int
2255b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2256b8d26b3bSNicholas Bellinger {
2257d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22586700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2259b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2260b8d26b3bSNicholas Bellinger 
2261b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2262b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2263b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2264b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
226568a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2266b8d26b3bSNicholas Bellinger 
22674c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
2268b8d26b3bSNicholas Bellinger 
2269b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2270b8d26b3bSNicholas Bellinger }
2271b8d26b3bSNicholas Bellinger 
2272b8d26b3bSNicholas Bellinger static int
2273b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2274b8d26b3bSNicholas Bellinger {
2275d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22766700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2277b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2278b8d26b3bSNicholas Bellinger 
2279b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2280b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2281b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2282b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
228368a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2284b8d26b3bSNicholas Bellinger 
22854c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
2286b8d26b3bSNicholas Bellinger 
2287b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2288b8d26b3bSNicholas Bellinger }
2289b8d26b3bSNicholas Bellinger 
2290b8d26b3bSNicholas Bellinger static int
2291b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2292b8d26b3bSNicholas Bellinger {
2293d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22946700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2295b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
229667cb3949SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
229767cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
22983df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
22993df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
23003df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2301b8d26b3bSNicholas Bellinger 
2302b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
23033df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2304b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
23053df8f68aSNicholas Bellinger 
23063df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2307dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
23083df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
23093df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2310dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2311dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
23123df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
231367cb3949SSagi Grimberg 	tx_dsg->lkey	= device->mr->lkey;
23143df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
23153df8f68aSNicholas Bellinger 
231668a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2317b8d26b3bSNicholas Bellinger 
23184c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
2319b8d26b3bSNicholas Bellinger 
2320b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2321b8d26b3bSNicholas Bellinger }
2322b8d26b3bSNicholas Bellinger 
2323b8d26b3bSNicholas Bellinger static int
2324adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2325adb54c29SNicholas Bellinger {
2326d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
23276700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2328adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2329adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2330adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2331adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2332adb54c29SNicholas Bellinger 	int rc;
2333adb54c29SNicholas Bellinger 
2334adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
233522c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2336adb54c29SNicholas Bellinger 	if (rc < 0)
2337adb54c29SNicholas Bellinger 		return rc;
2338adb54c29SNicholas Bellinger 
2339adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2340adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2341adb54c29SNicholas Bellinger 
2342adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
234367cb3949SSagi Grimberg 		struct isert_device *device = isert_conn->conn_device;
234467cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2345adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2346adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2347adb54c29SNicholas Bellinger 
2348adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2349adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2350adb54c29SNicholas Bellinger 
2351adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2352adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2353adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
235467cb3949SSagi Grimberg 		tx_dsg->lkey	= device->mr->lkey;
2355adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2356adb54c29SNicholas Bellinger 	}
235768a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2358adb54c29SNicholas Bellinger 
2359f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
2360adb54c29SNicholas Bellinger 
2361adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2362adb54c29SNicholas Bellinger }
2363adb54c29SNicholas Bellinger 
2364adb54c29SNicholas Bellinger static int
2365b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2366b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2367b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2368b8d26b3bSNicholas Bellinger {
2369d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2370b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
237167cb3949SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
237267cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
2373b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2374b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2375b8d26b3bSNicholas Bellinger 
2376b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2377b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2378b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2379b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2380b8d26b3bSNicholas Bellinger 
2381b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2382b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2383b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
2384b8d26b3bSNicholas Bellinger 	/*
2385b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2386b8d26b3bSNicholas Bellinger 	 */
2387b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
23884c22e07fSSagi Grimberg 		isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
23894c22e07fSSagi Grimberg 			  "page_off: %u\n",
2390b8d26b3bSNicholas Bellinger 			  (unsigned long long)tmp_sg->dma_address,
2391b8d26b3bSNicholas Bellinger 			  tmp_sg->length, page_off);
2392b8d26b3bSNicholas Bellinger 
2393b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2394b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2395b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
239667cb3949SSagi Grimberg 		ib_sge->lkey = device->mr->lkey;
2397b8d26b3bSNicholas Bellinger 
23984c22e07fSSagi Grimberg 		isert_dbg("RDMA ib_sge: addr: 0x%llx  length: %u lkey: %x\n",
239990ecc6e2SVu Pham 			  ib_sge->addr, ib_sge->length, ib_sge->lkey);
2400b8d26b3bSNicholas Bellinger 		page_off = 0;
2401b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2402b8d26b3bSNicholas Bellinger 		ib_sge++;
240324f412ddSSagi Grimberg 		isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
2404b8d26b3bSNicholas Bellinger 	}
2405b8d26b3bSNicholas Bellinger 
240624f412ddSSagi Grimberg 	isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2407b8d26b3bSNicholas Bellinger 		  send_wr->sg_list, send_wr->num_sge);
2408b8d26b3bSNicholas Bellinger 
2409b8d26b3bSNicholas Bellinger 	return sg_nents;
2410b8d26b3bSNicholas Bellinger }
2411b8d26b3bSNicholas Bellinger 
2412b8d26b3bSNicholas Bellinger static int
241390ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
241490ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2415b8d26b3bSNicholas Bellinger {
2416b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2417d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
24186700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2419e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
242090ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2421b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2422e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2423e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2424b8d26b3bSNicholas Bellinger 
242590ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2426b8d26b3bSNicholas Bellinger 
2427e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2428e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2429e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2430e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2431e3d7e4c3SSagi Grimberg 	if (ret)
2432e3d7e4c3SSagi Grimberg 		return ret;
2433b8d26b3bSNicholas Bellinger 
2434e3d7e4c3SSagi Grimberg 	data_left = data->len;
2435e3d7e4c3SSagi Grimberg 	offset = data->offset;
2436b8d26b3bSNicholas Bellinger 
2437e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2438b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
243924f412ddSSagi Grimberg 		isert_warn("Unable to allocate ib_sge\n");
2440b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2441e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2442b8d26b3bSNicholas Bellinger 	}
244390ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2444b8d26b3bSNicholas Bellinger 
2445e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2446b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2447b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2448b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
244924f412ddSSagi Grimberg 		isert_dbg("Unable to allocate wr->send_wr\n");
2450b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2451e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2452b8d26b3bSNicholas Bellinger 	}
2453b8d26b3bSNicholas Bellinger 
2454b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2455b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2456b8d26b3bSNicholas Bellinger 
2457b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2458b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2459b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2460b8d26b3bSNicholas Bellinger 
2461b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
246290ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
246390ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2464b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2465b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
246690ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
246790ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
246890ecc6e2SVu Pham 			else
246990ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
247090ecc6e2SVu Pham 		} else {
247190ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
247290ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
247390ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
247490ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
247590ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
247690ecc6e2SVu Pham 			else
247790ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
247890ecc6e2SVu Pham 		}
2479b8d26b3bSNicholas Bellinger 
2480b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2481b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2482b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2483b8d26b3bSNicholas Bellinger 
2484b8d26b3bSNicholas Bellinger 		offset += data_len;
248590ecc6e2SVu Pham 		va_offset += data_len;
2486b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2487b8d26b3bSNicholas Bellinger 	}
248890ecc6e2SVu Pham 
248990ecc6e2SVu Pham 	return 0;
2490e3d7e4c3SSagi Grimberg unmap_cmd:
2491e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2492e3d7e4c3SSagi Grimberg 
249390ecc6e2SVu Pham 	return ret;
249490ecc6e2SVu Pham }
249590ecc6e2SVu Pham 
249690ecc6e2SVu Pham static int
249759464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
249859464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
249959464ef4SVu Pham {
250059464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
250159464ef4SVu Pham 	struct scatterlist *tmp_sg;
250259464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
250359464ef4SVu Pham 
250459464ef4SVu Pham 	n_pages = 0;
250559464ef4SVu Pham 	new_chunk = 1;
250659464ef4SVu Pham 	last_ent = sg_nents - 1;
250759464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
250859464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
250959464ef4SVu Pham 		if (new_chunk)
251059464ef4SVu Pham 			chunk_start = start_addr;
251159464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
251259464ef4SVu Pham 
25134c22e07fSSagi Grimberg 		isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
251459464ef4SVu Pham 			  i, (unsigned long long)tmp_sg->dma_address,
251559464ef4SVu Pham 			  tmp_sg->length);
251659464ef4SVu Pham 
251759464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
251859464ef4SVu Pham 			new_chunk = 0;
251959464ef4SVu Pham 			continue;
252059464ef4SVu Pham 		}
252159464ef4SVu Pham 		new_chunk = 1;
252259464ef4SVu Pham 
252359464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
252459464ef4SVu Pham 		do {
252559464ef4SVu Pham 			fr_pl[n_pages++] = page;
25264c22e07fSSagi Grimberg 			isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
252759464ef4SVu Pham 				  n_pages - 1, page);
252859464ef4SVu Pham 			page += PAGE_SIZE;
252959464ef4SVu Pham 		} while (page < end_addr);
253059464ef4SVu Pham 	}
253159464ef4SVu Pham 
253259464ef4SVu Pham 	return n_pages;
253359464ef4SVu Pham }
253459464ef4SVu Pham 
253510633c37SSagi Grimberg static inline void
253610633c37SSagi Grimberg isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
253710633c37SSagi Grimberg {
253810633c37SSagi Grimberg 	u32 rkey;
253910633c37SSagi Grimberg 
254010633c37SSagi Grimberg 	memset(inv_wr, 0, sizeof(*inv_wr));
254110633c37SSagi Grimberg 	inv_wr->wr_id = ISER_FASTREG_LI_WRID;
254210633c37SSagi Grimberg 	inv_wr->opcode = IB_WR_LOCAL_INV;
254310633c37SSagi Grimberg 	inv_wr->ex.invalidate_rkey = mr->rkey;
254410633c37SSagi Grimberg 
254510633c37SSagi Grimberg 	/* Bump the key */
254610633c37SSagi Grimberg 	rkey = ib_inc_rkey(mr->rkey);
254710633c37SSagi Grimberg 	ib_update_fast_reg_key(mr, rkey);
254810633c37SSagi Grimberg }
254910633c37SSagi Grimberg 
255059464ef4SVu Pham static int
2551e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2552e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2553e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
25549e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2555e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
255659464ef4SVu Pham {
255767cb3949SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
255867cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
25599e961ae7SSagi Grimberg 	struct ib_mr *mr;
25609e961ae7SSagi Grimberg 	struct ib_fast_reg_page_list *frpl;
256159464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
256259464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
25639bd626e7SSagi Grimberg 	int ret, pagelist_len;
25649bd626e7SSagi Grimberg 	u32 page_off;
256559464ef4SVu Pham 
2566e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
256767cb3949SSagi Grimberg 		sge->lkey = device->mr->lkey;
2568e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2569e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
25704c22e07fSSagi Grimberg 		isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
25714c22e07fSSagi Grimberg 			 sge->addr, sge->length, sge->lkey);
2572e3d7e4c3SSagi Grimberg 		return 0;
2573e3d7e4c3SSagi Grimberg 	}
2574e3d7e4c3SSagi Grimberg 
25759e961ae7SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID) {
25769e961ae7SSagi Grimberg 		/* Registering data buffer */
25779e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
25789e961ae7SSagi Grimberg 		frpl = fr_desc->data_frpl;
25799e961ae7SSagi Grimberg 	} else {
25809e961ae7SSagi Grimberg 		/* Registering protection buffer */
25819e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
25829e961ae7SSagi Grimberg 		frpl = fr_desc->pi_ctx->prot_frpl;
25839e961ae7SSagi Grimberg 	}
25849e961ae7SSagi Grimberg 
2585e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
258659464ef4SVu Pham 
258724f412ddSSagi Grimberg 	isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2588e3d7e4c3SSagi Grimberg 		  fr_desc, mem->nents, mem->offset);
258959464ef4SVu Pham 
2590e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
25919e961ae7SSagi Grimberg 					     &frpl->page_list[0]);
259259464ef4SVu Pham 
259310633c37SSagi Grimberg 	if (!(fr_desc->ind & ind)) {
259410633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, mr);
259559464ef4SVu Pham 		wr = &inv_wr;
259659464ef4SVu Pham 	}
259759464ef4SVu Pham 
259859464ef4SVu Pham 	/* Prepare FASTREG WR */
259959464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
26009bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
260159464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
26029e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
26039e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.page_list = frpl;
260459464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
260559464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2606e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
26079e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.rkey = mr->rkey;
260859464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
260959464ef4SVu Pham 
261059464ef4SVu Pham 	if (!wr)
261159464ef4SVu Pham 		wr = &fr_wr;
261259464ef4SVu Pham 	else
261359464ef4SVu Pham 		wr->next = &fr_wr;
261459464ef4SVu Pham 
261559464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
261659464ef4SVu Pham 	if (ret) {
261724f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
261859464ef4SVu Pham 		return ret;
261959464ef4SVu Pham 	}
26209e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
262159464ef4SVu Pham 
26229e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
26239e961ae7SSagi Grimberg 	sge->addr = frpl->page_list[0] + page_off;
2624e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
262559464ef4SVu Pham 
26264c22e07fSSagi Grimberg 	isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
26274c22e07fSSagi Grimberg 		  sge->addr, sge->length, sge->lkey);
262859464ef4SVu Pham 
262959464ef4SVu Pham 	return ret;
263059464ef4SVu Pham }
263159464ef4SVu Pham 
26323d73cf1aSSagi Grimberg static inline void
26333d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
26343d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
26359e961ae7SSagi Grimberg {
263678eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
26373d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
26383d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
26393d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
264078eda2bbSSagi Grimberg 	/*
264178eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
264278eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
264378eda2bbSSagi Grimberg 	 * from se_cmd.
264478eda2bbSSagi Grimberg 	 */
264578eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
264678eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
264778eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
264878eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
264978eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
265078eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
26513d73cf1aSSagi Grimberg };
26523d73cf1aSSagi Grimberg 
26533d73cf1aSSagi Grimberg static int
26543d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
26553d73cf1aSSagi Grimberg {
26569e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
26579e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
26589e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
265978eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
26603d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
26619e961ae7SSagi Grimberg 		break;
26629e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
26639e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
266478eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
26653d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
26669e961ae7SSagi Grimberg 		break;
26679e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
26689e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
26693d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
26703d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
26719e961ae7SSagi Grimberg 		break;
26729e961ae7SSagi Grimberg 	default:
267324f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
26749e961ae7SSagi Grimberg 		return -EINVAL;
26759e961ae7SSagi Grimberg 	}
26769e961ae7SSagi Grimberg 
26779e961ae7SSagi Grimberg 	return 0;
26789e961ae7SSagi Grimberg }
26799e961ae7SSagi Grimberg 
26809e961ae7SSagi Grimberg static inline u8
26819e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
26829e961ae7SSagi Grimberg {
26839e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
26849e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
26859e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
26869e961ae7SSagi Grimberg }
26879e961ae7SSagi Grimberg 
26889e961ae7SSagi Grimberg static int
2689570db170SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn,
2690570db170SSagi Grimberg 		 struct se_cmd *se_cmd,
2691570db170SSagi Grimberg 		 struct isert_rdma_wr *rdma_wr,
2692570db170SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc)
26939e961ae7SSagi Grimberg {
26949e961ae7SSagi Grimberg 	struct ib_send_wr sig_wr, inv_wr;
26959e961ae7SSagi Grimberg 	struct ib_send_wr *bad_wr, *wr = NULL;
26969e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
26979e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
26989e961ae7SSagi Grimberg 	int ret;
26999e961ae7SSagi Grimberg 
27009e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
27019e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
27029e961ae7SSagi Grimberg 	if (ret)
27039e961ae7SSagi Grimberg 		goto err;
27049e961ae7SSagi Grimberg 
27059e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
27069e961ae7SSagi Grimberg 
27079e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
270810633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
27099e961ae7SSagi Grimberg 		wr = &inv_wr;
27109e961ae7SSagi Grimberg 	}
27119e961ae7SSagi Grimberg 
27129e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
27139e961ae7SSagi Grimberg 	sig_wr.opcode = IB_WR_REG_SIG_MR;
2714c2caa207SSagi Grimberg 	sig_wr.wr_id = ISER_FASTREG_LI_WRID;
2715570db170SSagi Grimberg 	sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
27169e961ae7SSagi Grimberg 	sig_wr.num_sge = 1;
27179e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
27189e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
27199e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
27209e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
2721570db170SSagi Grimberg 		sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
27229e961ae7SSagi Grimberg 
27239e961ae7SSagi Grimberg 	if (!wr)
27249e961ae7SSagi Grimberg 		wr = &sig_wr;
27259e961ae7SSagi Grimberg 	else
27269e961ae7SSagi Grimberg 		wr->next = &sig_wr;
27279e961ae7SSagi Grimberg 
27289e961ae7SSagi Grimberg 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
27299e961ae7SSagi Grimberg 	if (ret) {
273024f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
27319e961ae7SSagi Grimberg 		goto err;
27329e961ae7SSagi Grimberg 	}
27339e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
27349e961ae7SSagi Grimberg 
2735570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2736570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].addr = 0;
2737570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
27389e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
27399e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
27409e961ae7SSagi Grimberg 		/*
27419e961ae7SSagi Grimberg 		 * We have protection guards on the wire
27429e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
27439e961ae7SSagi Grimberg 		 */
2744570db170SSagi Grimberg 		rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
27459e961ae7SSagi Grimberg 
274624f412ddSSagi Grimberg 	isert_dbg("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
2747570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2748570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].lkey);
27499e961ae7SSagi Grimberg err:
27509e961ae7SSagi Grimberg 	return ret;
27519e961ae7SSagi Grimberg }
27529e961ae7SSagi Grimberg 
275359464ef4SVu Pham static int
2754570db170SSagi Grimberg isert_handle_prot_cmd(struct isert_conn *isert_conn,
2755570db170SSagi Grimberg 		      struct isert_cmd *isert_cmd,
2756570db170SSagi Grimberg 		      struct isert_rdma_wr *wr)
2757570db170SSagi Grimberg {
2758570db170SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
2759570db170SSagi Grimberg 	struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2760570db170SSagi Grimberg 	int ret;
2761570db170SSagi Grimberg 
2762570db170SSagi Grimberg 	if (!wr->fr_desc->pi_ctx) {
2763570db170SSagi Grimberg 		ret = isert_create_pi_ctx(wr->fr_desc,
2764570db170SSagi Grimberg 					  device->ib_device,
276567cb3949SSagi Grimberg 					  device->pd);
2766570db170SSagi Grimberg 		if (ret) {
276724f412ddSSagi Grimberg 			isert_err("conn %p failed to allocate pi_ctx\n",
2768570db170SSagi Grimberg 				  isert_conn);
2769570db170SSagi Grimberg 			return ret;
2770570db170SSagi Grimberg 		}
2771570db170SSagi Grimberg 	}
2772570db170SSagi Grimberg 
2773570db170SSagi Grimberg 	if (se_cmd->t_prot_sg) {
2774570db170SSagi Grimberg 		ret = isert_map_data_buf(isert_conn, isert_cmd,
2775570db170SSagi Grimberg 					 se_cmd->t_prot_sg,
2776570db170SSagi Grimberg 					 se_cmd->t_prot_nents,
2777570db170SSagi Grimberg 					 se_cmd->prot_length,
2778570db170SSagi Grimberg 					 0, wr->iser_ib_op, &wr->prot);
2779570db170SSagi Grimberg 		if (ret) {
278024f412ddSSagi Grimberg 			isert_err("conn %p failed to map protection buffer\n",
2781570db170SSagi Grimberg 				  isert_conn);
2782570db170SSagi Grimberg 			return ret;
2783570db170SSagi Grimberg 		}
2784570db170SSagi Grimberg 
2785570db170SSagi Grimberg 		memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2786570db170SSagi Grimberg 		ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2787570db170SSagi Grimberg 					ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2788570db170SSagi Grimberg 		if (ret) {
278924f412ddSSagi Grimberg 			isert_err("conn %p failed to fast reg mr\n",
2790570db170SSagi Grimberg 				  isert_conn);
2791570db170SSagi Grimberg 			goto unmap_prot_cmd;
2792570db170SSagi Grimberg 		}
2793570db170SSagi Grimberg 	}
2794570db170SSagi Grimberg 
2795570db170SSagi Grimberg 	ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2796570db170SSagi Grimberg 	if (ret) {
279724f412ddSSagi Grimberg 		isert_err("conn %p failed to fast reg mr\n",
2798570db170SSagi Grimberg 			  isert_conn);
2799570db170SSagi Grimberg 		goto unmap_prot_cmd;
2800570db170SSagi Grimberg 	}
2801570db170SSagi Grimberg 	wr->fr_desc->ind |= ISERT_PROTECTED;
2802570db170SSagi Grimberg 
2803570db170SSagi Grimberg 	return 0;
2804570db170SSagi Grimberg 
2805570db170SSagi Grimberg unmap_prot_cmd:
2806570db170SSagi Grimberg 	if (se_cmd->t_prot_sg)
2807570db170SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2808570db170SSagi Grimberg 
2809570db170SSagi Grimberg 	return ret;
2810570db170SSagi Grimberg }
2811570db170SSagi Grimberg 
2812570db170SSagi Grimberg static int
2813a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
281459464ef4SVu Pham 	       struct isert_rdma_wr *wr)
281559464ef4SVu Pham {
281659464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
281759464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2818e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2819e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2820570db170SSagi Grimberg 	struct ib_send_wr *send_wr;
2821570db170SSagi Grimberg 	struct ib_sge *ib_sg;
2822e3d7e4c3SSagi Grimberg 	u32 offset;
2823e3d7e4c3SSagi Grimberg 	int ret = 0;
282459464ef4SVu Pham 	unsigned long flags;
282559464ef4SVu Pham 
282659464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2827e3d7e4c3SSagi Grimberg 
2828e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2829e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2830e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2831e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2832e3d7e4c3SSagi Grimberg 	if (ret)
2833e3d7e4c3SSagi Grimberg 		return ret;
2834e3d7e4c3SSagi Grimberg 
2835302cc7c3SSagi Grimberg 	if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
2836e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2837e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2838e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2839e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2840e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2841e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
284259464ef4SVu Pham 	}
284359464ef4SVu Pham 
28449e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2845570db170SSagi Grimberg 				ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
2846e3d7e4c3SSagi Grimberg 	if (ret)
2847e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
284859464ef4SVu Pham 
2849302cc7c3SSagi Grimberg 	if (isert_prot_cmd(isert_conn, se_cmd)) {
2850570db170SSagi Grimberg 		ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
28519e961ae7SSagi Grimberg 		if (ret)
28529e961ae7SSagi Grimberg 			goto unmap_cmd;
28539e961ae7SSagi Grimberg 
2854570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[SIG];
2855570db170SSagi Grimberg 	} else {
2856570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[DATA];
28579e961ae7SSagi Grimberg 	}
28589e961ae7SSagi Grimberg 
2859570db170SSagi Grimberg 	memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
2860e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
286159464ef4SVu Pham 	wr->send_wr_num = 1;
286259464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
286359464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
286459464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
286559464ef4SVu Pham 
286659464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2867e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
286859464ef4SVu Pham 	send_wr->num_sge = 1;
2869b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
287059464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
287159464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
287259464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
287359464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2874302cc7c3SSagi Grimberg 		send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
28759e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
287659464ef4SVu Pham 	} else {
287759464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
287859464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
287959464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
288059464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
288159464ef4SVu Pham 	}
288259464ef4SVu Pham 
288359464ef4SVu Pham 	return 0;
2884570db170SSagi Grimberg 
2885e3d7e4c3SSagi Grimberg unmap_cmd:
2886e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2887e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2888e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2889e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2890e3d7e4c3SSagi Grimberg 	}
2891e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
289259464ef4SVu Pham 
289359464ef4SVu Pham 	return ret;
289459464ef4SVu Pham }
289559464ef4SVu Pham 
289659464ef4SVu Pham static int
289790ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
289890ecc6e2SVu Pham {
289990ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
290059464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
290190ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
29026700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2903d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
290490ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
290590ecc6e2SVu Pham 	int rc;
290690ecc6e2SVu Pham 
290724f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
290890ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
29094c22e07fSSagi Grimberg 
291090ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2911d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
291290ecc6e2SVu Pham 	if (rc) {
291324f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
291490ecc6e2SVu Pham 		return rc;
291590ecc6e2SVu Pham 	}
291690ecc6e2SVu Pham 
2917302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd)) {
2918b8d26b3bSNicholas Bellinger 		/*
2919b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2920b8d26b3bSNicholas Bellinger 		 */
29219e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
29229e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
292304d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2924b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2925b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
292695b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
292768a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
29289e961ae7SSagi Grimberg 		isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
2929897bb2c9SSagi Grimberg 		wr->send_wr_num += 1;
29309e961ae7SSagi Grimberg 	}
2931b8d26b3bSNicholas Bellinger 
2932b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2933bdf20e72SSagi Grimberg 	if (rc)
293424f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
29359e961ae7SSagi Grimberg 
2936302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd))
293724f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
29389e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
29399e961ae7SSagi Grimberg 	else
294024f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
294190ecc6e2SVu Pham 			 isert_cmd);
2942b8d26b3bSNicholas Bellinger 
294390ecc6e2SVu Pham 	return 1;
2944b8d26b3bSNicholas Bellinger }
2945b8d26b3bSNicholas Bellinger 
2946b8d26b3bSNicholas Bellinger static int
2947b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2948b8d26b3bSNicholas Bellinger {
2949b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2950d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2951b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
29526700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2953d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
295490ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
295590ecc6e2SVu Pham 	int rc;
2956b8d26b3bSNicholas Bellinger 
295724f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
295890ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2959b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2960d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
296190ecc6e2SVu Pham 	if (rc) {
296224f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
296390ecc6e2SVu Pham 		return rc;
2964b8d26b3bSNicholas Bellinger 	}
2965b8d26b3bSNicholas Bellinger 
2966b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2967bdf20e72SSagi Grimberg 	if (rc)
296824f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2969bdf20e72SSagi Grimberg 
297024f412ddSSagi Grimberg 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
297190ecc6e2SVu Pham 		 isert_cmd);
2972b8d26b3bSNicholas Bellinger 
297390ecc6e2SVu Pham 	return 0;
2974b8d26b3bSNicholas Bellinger }
2975b8d26b3bSNicholas Bellinger 
2976b8d26b3bSNicholas Bellinger static int
2977b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2978b8d26b3bSNicholas Bellinger {
2979b8d26b3bSNicholas Bellinger 	int ret;
2980b8d26b3bSNicholas Bellinger 
2981b8d26b3bSNicholas Bellinger 	switch (state) {
2982b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2983b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2984b8d26b3bSNicholas Bellinger 		break;
2985b8d26b3bSNicholas Bellinger 	default:
298624f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2987b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2988b8d26b3bSNicholas Bellinger 		break;
2989b8d26b3bSNicholas Bellinger 	}
2990b8d26b3bSNicholas Bellinger 
2991b8d26b3bSNicholas Bellinger 	return ret;
2992b8d26b3bSNicholas Bellinger }
2993b8d26b3bSNicholas Bellinger 
2994b8d26b3bSNicholas Bellinger static int
2995b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2996b8d26b3bSNicholas Bellinger {
2997991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2998b8d26b3bSNicholas Bellinger 	int ret;
2999b8d26b3bSNicholas Bellinger 
3000b8d26b3bSNicholas Bellinger 	switch (state) {
3001b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
3002b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
3003991bb764SSagi Grimberg 		if (!ret)
3004991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
3005b8d26b3bSNicholas Bellinger 		break;
3006b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
3007b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
3008b8d26b3bSNicholas Bellinger 		break;
3009b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
3010b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
3011b8d26b3bSNicholas Bellinger 		break;
3012b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
3013b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
3014b8d26b3bSNicholas Bellinger 		break;
3015adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
3016adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
3017adb54c29SNicholas Bellinger 		break;
3018b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
3019b8d26b3bSNicholas Bellinger 		/*
3020b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
3021b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
3022b8d26b3bSNicholas Bellinger 		 */
3023b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
3024b8d26b3bSNicholas Bellinger 		break;
3025b8d26b3bSNicholas Bellinger 	default:
302624f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
3027b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
3028b8d26b3bSNicholas Bellinger 		break;
3029b8d26b3bSNicholas Bellinger 	}
3030b8d26b3bSNicholas Bellinger 
3031b8d26b3bSNicholas Bellinger 	return ret;
3032b8d26b3bSNicholas Bellinger }
3033b8d26b3bSNicholas Bellinger 
3034ca6c1d82SSagi Grimberg struct rdma_cm_id *
3035ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
3036ca6c1d82SSagi Grimberg {
3037ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
3038ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
3039ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
3040ca6c1d82SSagi Grimberg 	int ret;
3041ca6c1d82SSagi Grimberg 
3042ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
304324f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
3044ca6c1d82SSagi Grimberg 
3045ca6c1d82SSagi Grimberg 	id = rdma_create_id(isert_cma_handler, isert_np,
3046ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
3047ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
304824f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
3049ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
3050ca6c1d82SSagi Grimberg 		goto out;
3051ca6c1d82SSagi Grimberg 	}
305224f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
3053ca6c1d82SSagi Grimberg 
3054ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
3055ca6c1d82SSagi Grimberg 	if (ret) {
305624f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
3057ca6c1d82SSagi Grimberg 		goto out_id;
3058ca6c1d82SSagi Grimberg 	}
3059ca6c1d82SSagi Grimberg 
3060ca6c1d82SSagi Grimberg 	ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
3061ca6c1d82SSagi Grimberg 	if (ret) {
306224f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
3063ca6c1d82SSagi Grimberg 		goto out_id;
3064ca6c1d82SSagi Grimberg 	}
3065ca6c1d82SSagi Grimberg 
3066ca6c1d82SSagi Grimberg 	return id;
3067ca6c1d82SSagi Grimberg out_id:
3068ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
3069ca6c1d82SSagi Grimberg out:
3070ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
3071ca6c1d82SSagi Grimberg }
3072ca6c1d82SSagi Grimberg 
3073b8d26b3bSNicholas Bellinger static int
3074b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
3075b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
3076b8d26b3bSNicholas Bellinger {
3077b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
3078b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
3079b8d26b3bSNicholas Bellinger 	int ret;
3080b8d26b3bSNicholas Bellinger 
3081b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3082b8d26b3bSNicholas Bellinger 	if (!isert_np) {
308324f412ddSSagi Grimberg 		isert_err("Unable to allocate struct isert_np\n");
3084b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3085b8d26b3bSNicholas Bellinger 	}
3086531b7bf4SSagi Grimberg 	sema_init(&isert_np->np_sem, 0);
3087b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
3088b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
3089b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
3090ca6c1d82SSagi Grimberg 	isert_np->np = np;
3091b8d26b3bSNicholas Bellinger 
3092b8d26b3bSNicholas Bellinger 	/*
3093b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3094b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3095b8d26b3bSNicholas Bellinger 	 */
3096b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
3097b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
3098b8d26b3bSNicholas Bellinger 
3099ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
3100b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3101b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3102b8d26b3bSNicholas Bellinger 		goto out;
3103b8d26b3bSNicholas Bellinger 	}
3104b8d26b3bSNicholas Bellinger 
3105b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
3106b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3107b8d26b3bSNicholas Bellinger 
3108b8d26b3bSNicholas Bellinger 	return 0;
3109b8d26b3bSNicholas Bellinger 
3110b8d26b3bSNicholas Bellinger out:
3111b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3112ca6c1d82SSagi Grimberg 
3113b8d26b3bSNicholas Bellinger 	return ret;
3114b8d26b3bSNicholas Bellinger }
3115b8d26b3bSNicholas Bellinger 
3116b8d26b3bSNicholas Bellinger static int
3117b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3118b8d26b3bSNicholas Bellinger {
3119b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3120b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3121b8d26b3bSNicholas Bellinger 	int ret;
3122b8d26b3bSNicholas Bellinger 
3123b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3124b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3125b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3126b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3127b8d26b3bSNicholas Bellinger 
3128b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3129b8d26b3bSNicholas Bellinger 	if (ret) {
313024f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
3131b8d26b3bSNicholas Bellinger 		return ret;
3132b8d26b3bSNicholas Bellinger 	}
3133b8d26b3bSNicholas Bellinger 
3134b8d26b3bSNicholas Bellinger 	return 0;
3135b8d26b3bSNicholas Bellinger }
3136b8d26b3bSNicholas Bellinger 
3137b8d26b3bSNicholas Bellinger static int
3138b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3139b8d26b3bSNicholas Bellinger {
31406700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
3141b8d26b3bSNicholas Bellinger 	int ret;
3142b8d26b3bSNicholas Bellinger 
314324f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
31442371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
31452371e5daSSagi Grimberg 	if (ret) {
314624f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
31472371e5daSSagi Grimberg 			  isert_conn);
31482371e5daSSagi Grimberg 		return ret;
31492371e5daSSagi Grimberg 	}
31502371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
31512371e5daSSagi Grimberg 
31526faaa85fSNicholas Bellinger 	/*
31536faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
31546faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
31556faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
31566faaa85fSNicholas Bellinger 	 * into a NOP.
31576faaa85fSNicholas Bellinger 	 */
31586faaa85fSNicholas Bellinger 	if (!login->first_request)
31596faaa85fSNicholas Bellinger 		return 0;
3160b8d26b3bSNicholas Bellinger 
31612371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
31622371e5daSSagi Grimberg 
316324f412ddSSagi Grimberg 	isert_info("before conn_login_comp conn: %p\n", conn);
3164b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3165b8d26b3bSNicholas Bellinger 	if (ret)
3166b8d26b3bSNicholas Bellinger 		return ret;
3167b8d26b3bSNicholas Bellinger 
316824f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
31692371e5daSSagi Grimberg 
3170b8d26b3bSNicholas Bellinger 	return 0;
3171b8d26b3bSNicholas Bellinger }
3172b8d26b3bSNicholas Bellinger 
3173b8d26b3bSNicholas Bellinger static void
3174b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3175b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3176b8d26b3bSNicholas Bellinger {
3177b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3178b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3179b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
3180b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
3181b8d26b3bSNicholas Bellinger 
3182b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3183b8d26b3bSNicholas Bellinger 
3184b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
3185b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3186b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3187b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3188b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
3189b8d26b3bSNicholas Bellinger 
3190b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3191b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3192b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3193b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
3194b8d26b3bSNicholas Bellinger 	} else {
3195b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3196b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
3197b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3198b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
3199b8d26b3bSNicholas Bellinger 
3200b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3201b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
3202b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3203b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
3204b8d26b3bSNicholas Bellinger 	}
3205b8d26b3bSNicholas Bellinger }
3206b8d26b3bSNicholas Bellinger 
3207b8d26b3bSNicholas Bellinger static int
3208b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3209b8d26b3bSNicholas Bellinger {
32106700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
3211b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3212*c6b8e918SSagi Grimberg 	int ret;
3213b8d26b3bSNicholas Bellinger 
3214b8d26b3bSNicholas Bellinger accept_wait:
3215531b7bf4SSagi Grimberg 	ret = down_interruptible(&isert_np->np_sem);
3216*c6b8e918SSagi Grimberg 	if (ret)
3217b8d26b3bSNicholas Bellinger 		return -ENODEV;
3218b8d26b3bSNicholas Bellinger 
3219b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3220e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3221b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
322211378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
3223e346ab34SSagi Grimberg 			 np->np_thread_state);
3224e346ab34SSagi Grimberg 		/**
3225e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
3226e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
3227e346ab34SSagi Grimberg 		 **/
3228b8d26b3bSNicholas Bellinger 		return -ENODEV;
3229b8d26b3bSNicholas Bellinger 	}
3230b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3231b8d26b3bSNicholas Bellinger 
3232b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3233b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
3234b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
3235b8d26b3bSNicholas Bellinger 		goto accept_wait;
3236b8d26b3bSNicholas Bellinger 	}
3237b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
3238b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
3239b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
3240b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3241b8d26b3bSNicholas Bellinger 
3242b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3243b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3244b8d26b3bSNicholas Bellinger 
3245b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3246b8d26b3bSNicholas Bellinger 
324724f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
32482371e5daSSagi Grimberg 
3249b8d26b3bSNicholas Bellinger 	return 0;
3250b8d26b3bSNicholas Bellinger }
3251b8d26b3bSNicholas Bellinger 
3252b8d26b3bSNicholas Bellinger static void
3253b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3254b8d26b3bSNicholas Bellinger {
32556700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
3256268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
3257b8d26b3bSNicholas Bellinger 
32583b726ae2SSagi Grimberg 	if (isert_np->np_cm_id)
3259b8d26b3bSNicholas Bellinger 		rdma_destroy_id(isert_np->np_cm_id);
3260b8d26b3bSNicholas Bellinger 
3261268e6811SSagi Grimberg 	/*
3262268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
3263268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
3264268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
3265268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
3266268e6811SSagi Grimberg 	 * up in np_accept_list.
3267268e6811SSagi Grimberg 	 */
3268268e6811SSagi Grimberg 	mutex_lock(&isert_np->np_accept_mutex);
3269268e6811SSagi Grimberg 	if (!list_empty(&isert_np->np_accept_list)) {
327024f412ddSSagi Grimberg 		isert_info("Still have isert connections, cleaning up...\n");
3271268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
3272268e6811SSagi Grimberg 					 &isert_np->np_accept_list,
3273268e6811SSagi Grimberg 					 conn_accept_node) {
327424f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
3275268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
3276268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
3277268e6811SSagi Grimberg 		}
3278268e6811SSagi Grimberg 	}
3279268e6811SSagi Grimberg 	mutex_unlock(&isert_np->np_accept_mutex);
3280268e6811SSagi Grimberg 
3281b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3282b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3283b8d26b3bSNicholas Bellinger }
3284b8d26b3bSNicholas Bellinger 
3285b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
3286b02efbfcSSagi Grimberg {
3287b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
3288b02efbfcSSagi Grimberg 						     struct isert_conn,
3289b02efbfcSSagi Grimberg 						     release_work);
3290b02efbfcSSagi Grimberg 
329124f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
3292b02efbfcSSagi Grimberg 
3293b02efbfcSSagi Grimberg 	wait_for_completion(&isert_conn->conn_wait);
3294b02efbfcSSagi Grimberg 
3295b02efbfcSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3296b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
3297b02efbfcSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
3298b02efbfcSSagi Grimberg 
329924f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
3300b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
3301b02efbfcSSagi Grimberg }
3302b02efbfcSSagi Grimberg 
3303bdf20e72SSagi Grimberg static void
3304991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
3305991bb764SSagi Grimberg {
3306991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
3307991bb764SSagi Grimberg 
33084c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
33094c22e07fSSagi Grimberg 
3310991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
331124f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
3312991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
3313991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
3314991bb764SSagi Grimberg 	}
3315991bb764SSagi Grimberg }
3316991bb764SSagi Grimberg 
3317991bb764SSagi Grimberg static void
3318c7e160eeSSagi Grimberg isert_wait4cmds(struct iscsi_conn *conn)
3319c7e160eeSSagi Grimberg {
33204c22e07fSSagi Grimberg 	isert_info("iscsi_conn %p\n", conn);
33214c22e07fSSagi Grimberg 
3322c7e160eeSSagi Grimberg 	if (conn->sess) {
3323c7e160eeSSagi Grimberg 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3324c7e160eeSSagi Grimberg 		target_wait_for_sess_cmds(conn->sess->se_sess);
3325c7e160eeSSagi Grimberg 	}
3326c7e160eeSSagi Grimberg }
3327c7e160eeSSagi Grimberg 
3328c7e160eeSSagi Grimberg static void
3329bdf20e72SSagi Grimberg isert_wait4flush(struct isert_conn *isert_conn)
3330bdf20e72SSagi Grimberg {
3331bdf20e72SSagi Grimberg 	struct ib_recv_wr *bad_wr;
3332bdf20e72SSagi Grimberg 
33334c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
33344c22e07fSSagi Grimberg 
3335bdf20e72SSagi Grimberg 	init_completion(&isert_conn->conn_wait_comp_err);
3336bdf20e72SSagi Grimberg 	isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3337bdf20e72SSagi Grimberg 	/* post an indication that all flush errors were consumed */
3338bdf20e72SSagi Grimberg 	if (ib_post_recv(isert_conn->conn_qp, &isert_conn->beacon, &bad_wr)) {
333924f412ddSSagi Grimberg 		isert_err("conn %p failed to post beacon", isert_conn);
3340bdf20e72SSagi Grimberg 		return;
3341bdf20e72SSagi Grimberg 	}
3342bdf20e72SSagi Grimberg 
3343bdf20e72SSagi Grimberg 	wait_for_completion(&isert_conn->conn_wait_comp_err);
3344bdf20e72SSagi Grimberg }
3345bdf20e72SSagi Grimberg 
3346defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3347b8d26b3bSNicholas Bellinger {
3348b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3349b8d26b3bSNicholas Bellinger 
33504c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
3351b8d26b3bSNicholas Bellinger 
33529d49f5e2SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3353b8d26b3bSNicholas Bellinger 	/*
3354b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
3355b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3356b8d26b3bSNicholas Bellinger 	 */
3357b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3358b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
3359b2cb9649SNicholas Bellinger 		return;
3360b2cb9649SNicholas Bellinger 	}
3361954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
3362b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
3363b2cb9649SNicholas Bellinger 
3364c7e160eeSSagi Grimberg 	isert_wait4cmds(conn);
3365bdf20e72SSagi Grimberg 	isert_wait4flush(isert_conn);
3366991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
3367954f2372SSagi Grimberg 
3368b02efbfcSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
3369b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
3370defd8848SNicholas Bellinger }
3371defd8848SNicholas Bellinger 
3372defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3373defd8848SNicholas Bellinger {
3374defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3375b8d26b3bSNicholas Bellinger 
3376b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3377b8d26b3bSNicholas Bellinger }
3378b8d26b3bSNicholas Bellinger 
3379b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3380b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3381b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3382d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3383b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3384b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3385b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3386b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3387defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3388b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3389b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3390b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3391b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3392b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3393b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3394b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3395b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3396131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3397e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3398b8d26b3bSNicholas Bellinger };
3399b8d26b3bSNicholas Bellinger 
3400b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3401b8d26b3bSNicholas Bellinger {
3402b8d26b3bSNicholas Bellinger 	int ret;
3403b8d26b3bSNicholas Bellinger 
3404631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
3405631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
3406b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
340724f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
3408b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
34096f0fae3dSSagi Grimberg 		return -ENOMEM;
3410b8d26b3bSNicholas Bellinger 	}
3411b8d26b3bSNicholas Bellinger 
3412b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3413b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
3414b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
341524f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
3416b02efbfcSSagi Grimberg 		ret = -ENOMEM;
3417b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
3418b02efbfcSSagi Grimberg 	}
3419b02efbfcSSagi Grimberg 
3420b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
342124f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3422b02efbfcSSagi Grimberg 
3423b8d26b3bSNicholas Bellinger 	return 0;
3424b8d26b3bSNicholas Bellinger 
3425b02efbfcSSagi Grimberg destroy_comp_wq:
3426b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
34276f0fae3dSSagi Grimberg 
3428b8d26b3bSNicholas Bellinger 	return ret;
3429b8d26b3bSNicholas Bellinger }
3430b8d26b3bSNicholas Bellinger 
3431b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3432b8d26b3bSNicholas Bellinger {
3433f5ebec96SSagi Grimberg 	flush_scheduled_work();
3434b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
3435b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3436b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
34374c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
3438b8d26b3bSNicholas Bellinger }
3439b8d26b3bSNicholas Bellinger 
3440b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3441b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
3442b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3443b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3444b8d26b3bSNicholas Bellinger 
3445b8d26b3bSNicholas Bellinger module_init(isert_init);
3446b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3447