xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 570db170f37715b7df23c95868169f3d9affa48c)
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>
2595b60f07SNicholas Bellinger #include <linux/llist.h>
26b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h>
27b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h>
28b8d26b3bSNicholas Bellinger #include <target/target_core_base.h>
29b8d26b3bSNicholas Bellinger #include <target/target_core_fabric.h>
30b8d26b3bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
31531b7bf4SSagi Grimberg #include <linux/semaphore.h>
32b8d26b3bSNicholas Bellinger 
33b8d26b3bSNicholas Bellinger #include "isert_proto.h"
34b8d26b3bSNicholas Bellinger #include "ib_isert.h"
35b8d26b3bSNicholas Bellinger 
36b8d26b3bSNicholas Bellinger #define	ISERT_MAX_CONN		8
37b8d26b3bSNicholas Bellinger #define ISER_MAX_RX_CQ_LEN	(ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
38b8d26b3bSNicholas Bellinger #define ISER_MAX_TX_CQ_LEN	(ISERT_QP_MAX_REQ_DTOS  * ISERT_MAX_CONN)
39b8d26b3bSNicholas Bellinger 
40b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
41b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
42b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_rx_wq;
43b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
44b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
45b8d26b3bSNicholas Bellinger 
46b8d26b3bSNicholas Bellinger static void
47d40945d8SVu Pham isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
48d40945d8SVu Pham static int
49d40945d8SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
50d40945d8SVu Pham 	       struct isert_rdma_wr *wr);
5159464ef4SVu Pham static void
52a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
5359464ef4SVu Pham static int
54a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
5559464ef4SVu Pham 	       struct isert_rdma_wr *wr);
56f93f3a70SSagi Grimberg static int
57f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
582371e5daSSagi Grimberg static int
592371e5daSSagi Grimberg isert_rdma_post_recvl(struct isert_conn *isert_conn);
602371e5daSSagi Grimberg static int
612371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
62ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
63d40945d8SVu Pham 
64d40945d8SVu Pham static void
65b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
66b8d26b3bSNicholas Bellinger {
67b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)context;
68b8d26b3bSNicholas Bellinger 
69b8d26b3bSNicholas Bellinger 	pr_err("isert_qp_event_callback event: %d\n", e->event);
70b8d26b3bSNicholas Bellinger 	switch (e->event) {
71b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
72b8d26b3bSNicholas Bellinger 		rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
73b8d26b3bSNicholas Bellinger 		break;
74b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
75b8d26b3bSNicholas Bellinger 		pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
76b8d26b3bSNicholas Bellinger 		break;
77b8d26b3bSNicholas Bellinger 	default:
78b8d26b3bSNicholas Bellinger 		break;
79b8d26b3bSNicholas Bellinger 	}
80b8d26b3bSNicholas Bellinger }
81b8d26b3bSNicholas Bellinger 
82b8d26b3bSNicholas Bellinger static int
83b8d26b3bSNicholas Bellinger isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
84b8d26b3bSNicholas Bellinger {
85b8d26b3bSNicholas Bellinger 	int ret;
86b8d26b3bSNicholas Bellinger 
87b8d26b3bSNicholas Bellinger 	ret = ib_query_device(ib_dev, devattr);
88b8d26b3bSNicholas Bellinger 	if (ret) {
89b8d26b3bSNicholas Bellinger 		pr_err("ib_query_device() failed: %d\n", ret);
90b8d26b3bSNicholas Bellinger 		return ret;
91b8d26b3bSNicholas Bellinger 	}
92b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
93b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
94b8d26b3bSNicholas Bellinger 
95b8d26b3bSNicholas Bellinger 	return 0;
96b8d26b3bSNicholas Bellinger }
97b8d26b3bSNicholas Bellinger 
98b8d26b3bSNicholas Bellinger static int
99*570db170SSagi Grimberg isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
100b8d26b3bSNicholas Bellinger {
101b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
102b8d26b3bSNicholas Bellinger 	struct ib_qp_init_attr attr;
103b8d26b3bSNicholas Bellinger 	int ret, index, min_index = 0;
104b8d26b3bSNicholas Bellinger 
105b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
106b8d26b3bSNicholas Bellinger 	for (index = 0; index < device->cqs_used; index++)
107b8d26b3bSNicholas Bellinger 		if (device->cq_active_qps[index] <
108b8d26b3bSNicholas Bellinger 		    device->cq_active_qps[min_index])
109b8d26b3bSNicholas Bellinger 			min_index = index;
110b8d26b3bSNicholas Bellinger 	device->cq_active_qps[min_index]++;
111b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
112b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
113b8d26b3bSNicholas Bellinger 
114b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
115b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
116b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
117b8d26b3bSNicholas Bellinger 	attr.send_cq = device->dev_tx_cq[min_index];
118b8d26b3bSNicholas Bellinger 	attr.recv_cq = device->dev_rx_cq[min_index];
119b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
120b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
121b8d26b3bSNicholas Bellinger 	/*
122b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
123f57915cfSOr Gerlitz 	 * work-around for RDMA_READs with ConnectX-2.
124f57915cfSOr Gerlitz 	 *
125f57915cfSOr Gerlitz 	 * Also, still make sure to have at least two SGEs for
126f57915cfSOr Gerlitz 	 * outgoing control PDU responses.
127b8d26b3bSNicholas Bellinger 	 */
128f57915cfSOr Gerlitz 	attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
129b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
130b8d26b3bSNicholas Bellinger 
131b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
132b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
133b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
134*570db170SSagi Grimberg 	if (device->pi_capable)
135d3e125daSSagi Grimberg 		attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
136b8d26b3bSNicholas Bellinger 
137b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
138b8d26b3bSNicholas Bellinger 		 cma_id->device);
139b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
140b8d26b3bSNicholas Bellinger 		 isert_conn->conn_pd->device);
141b8d26b3bSNicholas Bellinger 
142b8d26b3bSNicholas Bellinger 	ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
143b8d26b3bSNicholas Bellinger 	if (ret) {
144b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_qp failed for cma_id %d\n", ret);
14519e2090fSSagi Grimberg 		goto err;
146b8d26b3bSNicholas Bellinger 	}
147b8d26b3bSNicholas Bellinger 	isert_conn->conn_qp = cma_id->qp;
148b8d26b3bSNicholas Bellinger 	pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
149b8d26b3bSNicholas Bellinger 
150b8d26b3bSNicholas Bellinger 	return 0;
15119e2090fSSagi Grimberg err:
15219e2090fSSagi Grimberg 	mutex_lock(&device_list_mutex);
15319e2090fSSagi Grimberg 	device->cq_active_qps[min_index]--;
15419e2090fSSagi Grimberg 	mutex_unlock(&device_list_mutex);
15519e2090fSSagi Grimberg 
15619e2090fSSagi Grimberg 	return ret;
157b8d26b3bSNicholas Bellinger }
158b8d26b3bSNicholas Bellinger 
159b8d26b3bSNicholas Bellinger static void
160b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
161b8d26b3bSNicholas Bellinger {
162b8d26b3bSNicholas Bellinger 	pr_debug("isert_cq_event_callback event: %d\n", e->event);
163b8d26b3bSNicholas Bellinger }
164b8d26b3bSNicholas Bellinger 
165b8d26b3bSNicholas Bellinger static int
166b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
167b8d26b3bSNicholas Bellinger {
168b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
169b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
170b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
171b8d26b3bSNicholas Bellinger 	u64 dma_addr;
172b8d26b3bSNicholas Bellinger 	int i, j;
173b8d26b3bSNicholas Bellinger 
174b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
175b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
176b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
177b8d26b3bSNicholas Bellinger 		goto fail;
178b8d26b3bSNicholas Bellinger 
179b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
180b8d26b3bSNicholas Bellinger 
181b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
182b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
183b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
184b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
185b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
186b8d26b3bSNicholas Bellinger 
187b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
188b8d26b3bSNicholas Bellinger 
189b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
190b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
191b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
192b8d26b3bSNicholas Bellinger 		rx_sg->lkey = isert_conn->conn_mr->lkey;
193b8d26b3bSNicholas Bellinger 	}
194b8d26b3bSNicholas Bellinger 
195b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
196b8d26b3bSNicholas Bellinger 	return 0;
197b8d26b3bSNicholas Bellinger 
198b8d26b3bSNicholas Bellinger dma_map_fail:
199b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
200b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
201b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
202b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
203b8d26b3bSNicholas Bellinger 	}
204b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
205b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
206b8d26b3bSNicholas Bellinger fail:
207b8d26b3bSNicholas Bellinger 	return -ENOMEM;
208b8d26b3bSNicholas Bellinger }
209b8d26b3bSNicholas Bellinger 
210b8d26b3bSNicholas Bellinger static void
211b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
212b8d26b3bSNicholas Bellinger {
213b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
214b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
215b8d26b3bSNicholas Bellinger 	int i;
216b8d26b3bSNicholas Bellinger 
217b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
218b8d26b3bSNicholas Bellinger 		return;
219b8d26b3bSNicholas Bellinger 
220b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
221b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
222b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
223b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
224b8d26b3bSNicholas Bellinger 	}
225b8d26b3bSNicholas Bellinger 
226b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
227b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
228b8d26b3bSNicholas Bellinger }
229b8d26b3bSNicholas Bellinger 
2302853c2b6SNicholas Bellinger static void isert_cq_tx_work(struct work_struct *);
231b8d26b3bSNicholas Bellinger static void isert_cq_tx_callback(struct ib_cq *, void *);
2322853c2b6SNicholas Bellinger static void isert_cq_rx_work(struct work_struct *);
233b8d26b3bSNicholas Bellinger static void isert_cq_rx_callback(struct ib_cq *, void *);
234b8d26b3bSNicholas Bellinger 
235b8d26b3bSNicholas Bellinger static int
236b8d26b3bSNicholas Bellinger isert_create_device_ib_res(struct isert_device *device)
237b8d26b3bSNicholas Bellinger {
238b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = device->ib_device;
239b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
24059464ef4SVu Pham 	struct ib_device_attr *dev_attr;
241b8d26b3bSNicholas Bellinger 	int ret = 0, i, j;
242b1a5ad00SChris Moore 	int max_rx_cqe, max_tx_cqe;
243b8d26b3bSNicholas Bellinger 
24459464ef4SVu Pham 	dev_attr = &device->dev_attr;
24559464ef4SVu Pham 	ret = isert_query_device(ib_dev, dev_attr);
24659464ef4SVu Pham 	if (ret)
24759464ef4SVu Pham 		return ret;
24859464ef4SVu Pham 
249b1a5ad00SChris Moore 	max_rx_cqe = min(ISER_MAX_RX_CQ_LEN, dev_attr->max_cqe);
250b1a5ad00SChris Moore 	max_tx_cqe = min(ISER_MAX_TX_CQ_LEN, dev_attr->max_cqe);
251b1a5ad00SChris Moore 
252d40945d8SVu Pham 	/* asign function handlers */
253f2252258SSagi Grimberg 	if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
254f2252258SSagi Grimberg 	    dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
255a3a5a826SSagi Grimberg 		device->use_fastreg = 1;
256a3a5a826SSagi Grimberg 		device->reg_rdma_mem = isert_reg_rdma;
257a3a5a826SSagi Grimberg 		device->unreg_rdma_mem = isert_unreg_rdma;
25859464ef4SVu Pham 	} else {
259a3a5a826SSagi Grimberg 		device->use_fastreg = 0;
260d40945d8SVu Pham 		device->reg_rdma_mem = isert_map_rdma;
261d40945d8SVu Pham 		device->unreg_rdma_mem = isert_unmap_cmd;
26259464ef4SVu Pham 	}
263d40945d8SVu Pham 
264d3e125daSSagi Grimberg 	/* Check signature cap */
265d3e125daSSagi Grimberg 	device->pi_capable = dev_attr->device_cap_flags &
266d3e125daSSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
267d3e125daSSagi Grimberg 
268b8d26b3bSNicholas Bellinger 	device->cqs_used = min_t(int, num_online_cpus(),
269b8d26b3bSNicholas Bellinger 				 device->ib_device->num_comp_vectors);
270b8d26b3bSNicholas Bellinger 	device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
271a3a5a826SSagi Grimberg 	pr_debug("Using %d CQs, device %s supports %d vectors support "
272d3e125daSSagi Grimberg 		 "Fast registration %d pi_capable %d\n",
273b8d26b3bSNicholas Bellinger 		 device->cqs_used, device->ib_device->name,
274d3e125daSSagi Grimberg 		 device->ib_device->num_comp_vectors, device->use_fastreg,
275d3e125daSSagi Grimberg 		 device->pi_capable);
276b8d26b3bSNicholas Bellinger 	device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
277b8d26b3bSNicholas Bellinger 				device->cqs_used, GFP_KERNEL);
278b8d26b3bSNicholas Bellinger 	if (!device->cq_desc) {
279b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate device->cq_desc\n");
280b8d26b3bSNicholas Bellinger 		return -ENOMEM;
281b8d26b3bSNicholas Bellinger 	}
282b8d26b3bSNicholas Bellinger 	cq_desc = device->cq_desc;
283b8d26b3bSNicholas Bellinger 
284b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
285b8d26b3bSNicholas Bellinger 		cq_desc[i].device = device;
286b8d26b3bSNicholas Bellinger 		cq_desc[i].cq_index = i;
287b8d26b3bSNicholas Bellinger 
2882853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
289b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
290b8d26b3bSNicholas Bellinger 						isert_cq_rx_callback,
291b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
292b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
293b1a5ad00SChris Moore 						max_rx_cqe, i);
29494a71110SWei Yongjun 		if (IS_ERR(device->dev_rx_cq[i])) {
29594a71110SWei Yongjun 			ret = PTR_ERR(device->dev_rx_cq[i]);
29694a71110SWei Yongjun 			device->dev_rx_cq[i] = NULL;
297b8d26b3bSNicholas Bellinger 			goto out_cq;
29894a71110SWei Yongjun 		}
299b8d26b3bSNicholas Bellinger 
3002853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
301b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
302b8d26b3bSNicholas Bellinger 						isert_cq_tx_callback,
303b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
304b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
305b1a5ad00SChris Moore 						max_tx_cqe, i);
30694a71110SWei Yongjun 		if (IS_ERR(device->dev_tx_cq[i])) {
30794a71110SWei Yongjun 			ret = PTR_ERR(device->dev_tx_cq[i]);
30894a71110SWei Yongjun 			device->dev_tx_cq[i] = NULL;
30994a71110SWei Yongjun 			goto out_cq;
31094a71110SWei Yongjun 		}
31194a71110SWei Yongjun 
31294a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
31394a71110SWei Yongjun 		if (ret)
314b8d26b3bSNicholas Bellinger 			goto out_cq;
315b8d26b3bSNicholas Bellinger 
31694a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
31794a71110SWei Yongjun 		if (ret)
318b8d26b3bSNicholas Bellinger 			goto out_cq;
319b8d26b3bSNicholas Bellinger 	}
320b8d26b3bSNicholas Bellinger 
321b8d26b3bSNicholas Bellinger 	return 0;
322b8d26b3bSNicholas Bellinger 
323b8d26b3bSNicholas Bellinger out_cq:
324b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++) {
325b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[j];
326b8d26b3bSNicholas Bellinger 
327b8d26b3bSNicholas Bellinger 		if (device->dev_rx_cq[j]) {
328b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_rx_work);
329b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_rx_cq[j]);
330b8d26b3bSNicholas Bellinger 		}
331b8d26b3bSNicholas Bellinger 		if (device->dev_tx_cq[j]) {
332b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_tx_work);
333b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_tx_cq[j]);
334b8d26b3bSNicholas Bellinger 		}
335b8d26b3bSNicholas Bellinger 	}
336b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
337b8d26b3bSNicholas Bellinger 
338b8d26b3bSNicholas Bellinger 	return ret;
339b8d26b3bSNicholas Bellinger }
340b8d26b3bSNicholas Bellinger 
341b8d26b3bSNicholas Bellinger static void
342b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
343b8d26b3bSNicholas Bellinger {
344b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
345b8d26b3bSNicholas Bellinger 	int i;
346b8d26b3bSNicholas Bellinger 
347b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
348b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[i];
349b8d26b3bSNicholas Bellinger 
350b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_rx_work);
351b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_tx_work);
352b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_rx_cq[i]);
353b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_tx_cq[i]);
354b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = NULL;
355b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = NULL;
356b8d26b3bSNicholas Bellinger 	}
357b8d26b3bSNicholas Bellinger 
358b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
359b8d26b3bSNicholas Bellinger }
360b8d26b3bSNicholas Bellinger 
361b8d26b3bSNicholas Bellinger static void
362b8d26b3bSNicholas Bellinger isert_device_try_release(struct isert_device *device)
363b8d26b3bSNicholas Bellinger {
364b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
365b8d26b3bSNicholas Bellinger 	device->refcount--;
366b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
367b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
368b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
369b8d26b3bSNicholas Bellinger 		kfree(device);
370b8d26b3bSNicholas Bellinger 	}
371b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
372b8d26b3bSNicholas Bellinger }
373b8d26b3bSNicholas Bellinger 
374b8d26b3bSNicholas Bellinger static struct isert_device *
375b8d26b3bSNicholas Bellinger isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
376b8d26b3bSNicholas Bellinger {
377b8d26b3bSNicholas Bellinger 	struct isert_device *device;
378b8d26b3bSNicholas Bellinger 	int ret;
379b8d26b3bSNicholas Bellinger 
380b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
381b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
382b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
383b8d26b3bSNicholas Bellinger 			device->refcount++;
384b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
385b8d26b3bSNicholas Bellinger 			return device;
386b8d26b3bSNicholas Bellinger 		}
387b8d26b3bSNicholas Bellinger 	}
388b8d26b3bSNicholas Bellinger 
389b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
390b8d26b3bSNicholas Bellinger 	if (!device) {
391b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
392b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
393b8d26b3bSNicholas Bellinger 	}
394b8d26b3bSNicholas Bellinger 
395b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
396b8d26b3bSNicholas Bellinger 
397b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
398b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
399b8d26b3bSNicholas Bellinger 	if (ret) {
400b8d26b3bSNicholas Bellinger 		kfree(device);
401b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
402b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
403b8d26b3bSNicholas Bellinger 	}
404b8d26b3bSNicholas Bellinger 
405b8d26b3bSNicholas Bellinger 	device->refcount++;
406b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
407b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
408b8d26b3bSNicholas Bellinger 
409b8d26b3bSNicholas Bellinger 	return device;
410b8d26b3bSNicholas Bellinger }
411b8d26b3bSNicholas Bellinger 
41259464ef4SVu Pham static void
413a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
41459464ef4SVu Pham {
41559464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
41659464ef4SVu Pham 	int i = 0;
41759464ef4SVu Pham 
418a3a5a826SSagi Grimberg 	if (list_empty(&isert_conn->conn_fr_pool))
41959464ef4SVu Pham 		return;
42059464ef4SVu Pham 
421a3a5a826SSagi Grimberg 	pr_debug("Freeing conn %p fastreg pool", isert_conn);
42259464ef4SVu Pham 
42359464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
424a3a5a826SSagi Grimberg 				 &isert_conn->conn_fr_pool, list) {
42559464ef4SVu Pham 		list_del(&fr_desc->list);
42659464ef4SVu Pham 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
42759464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
428d3e125daSSagi Grimberg 		if (fr_desc->pi_ctx) {
429d3e125daSSagi Grimberg 			ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
430d3e125daSSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
431d3e125daSSagi Grimberg 			ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
432d3e125daSSagi Grimberg 			kfree(fr_desc->pi_ctx);
433d3e125daSSagi Grimberg 		}
43459464ef4SVu Pham 		kfree(fr_desc);
43559464ef4SVu Pham 		++i;
43659464ef4SVu Pham 	}
43759464ef4SVu Pham 
438a3a5a826SSagi Grimberg 	if (i < isert_conn->conn_fr_pool_size)
43959464ef4SVu Pham 		pr_warn("Pool still has %d regions registered\n",
440a3a5a826SSagi Grimberg 			isert_conn->conn_fr_pool_size - i);
44159464ef4SVu Pham }
44259464ef4SVu Pham 
44359464ef4SVu Pham static int
444*570db170SSagi Grimberg isert_create_pi_ctx(struct fast_reg_descriptor *desc,
445*570db170SSagi Grimberg 		    struct ib_device *device,
446*570db170SSagi Grimberg 		    struct ib_pd *pd)
447*570db170SSagi Grimberg {
448*570db170SSagi Grimberg 	struct ib_mr_init_attr mr_init_attr;
449*570db170SSagi Grimberg 	struct pi_context *pi_ctx;
450*570db170SSagi Grimberg 	int ret;
451*570db170SSagi Grimberg 
452*570db170SSagi Grimberg 	pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
453*570db170SSagi Grimberg 	if (!pi_ctx) {
454*570db170SSagi Grimberg 		pr_err("Failed to allocate pi context\n");
455*570db170SSagi Grimberg 		return -ENOMEM;
456*570db170SSagi Grimberg 	}
457*570db170SSagi Grimberg 
458*570db170SSagi Grimberg 	pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
459*570db170SSagi Grimberg 					    ISCSI_ISER_SG_TABLESIZE);
460*570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_frpl)) {
461*570db170SSagi Grimberg 		pr_err("Failed to allocate prot frpl err=%ld\n",
462*570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_frpl));
463*570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_frpl);
464*570db170SSagi Grimberg 		goto err_pi_ctx;
465*570db170SSagi Grimberg 	}
466*570db170SSagi Grimberg 
467*570db170SSagi Grimberg 	pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
468*570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_mr)) {
469*570db170SSagi Grimberg 		pr_err("Failed to allocate prot frmr err=%ld\n",
470*570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_mr));
471*570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_mr);
472*570db170SSagi Grimberg 		goto err_prot_frpl;
473*570db170SSagi Grimberg 	}
474*570db170SSagi Grimberg 	desc->ind |= ISERT_PROT_KEY_VALID;
475*570db170SSagi Grimberg 
476*570db170SSagi Grimberg 	memset(&mr_init_attr, 0, sizeof(mr_init_attr));
477*570db170SSagi Grimberg 	mr_init_attr.max_reg_descriptors = 2;
478*570db170SSagi Grimberg 	mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
479*570db170SSagi Grimberg 	pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
480*570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->sig_mr)) {
481*570db170SSagi Grimberg 		pr_err("Failed to allocate signature enabled mr err=%ld\n",
482*570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->sig_mr));
483*570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->sig_mr);
484*570db170SSagi Grimberg 		goto err_prot_mr;
485*570db170SSagi Grimberg 	}
486*570db170SSagi Grimberg 
487*570db170SSagi Grimberg 	desc->pi_ctx = pi_ctx;
488*570db170SSagi Grimberg 	desc->ind |= ISERT_SIG_KEY_VALID;
489*570db170SSagi Grimberg 	desc->ind &= ~ISERT_PROTECTED;
490*570db170SSagi Grimberg 
491*570db170SSagi Grimberg 	return 0;
492*570db170SSagi Grimberg 
493*570db170SSagi Grimberg err_prot_mr:
494*570db170SSagi Grimberg 	ib_dereg_mr(desc->pi_ctx->prot_mr);
495*570db170SSagi Grimberg err_prot_frpl:
496*570db170SSagi Grimberg 	ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
497*570db170SSagi Grimberg err_pi_ctx:
498*570db170SSagi Grimberg 	kfree(desc->pi_ctx);
499*570db170SSagi Grimberg 
500*570db170SSagi Grimberg 	return ret;
501*570db170SSagi Grimberg }
502*570db170SSagi Grimberg 
503*570db170SSagi Grimberg static int
504dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
505*570db170SSagi Grimberg 		     struct fast_reg_descriptor *fr_desc)
506dc87a90fSSagi Grimberg {
507d3e125daSSagi Grimberg 	int ret;
508d3e125daSSagi Grimberg 
509dc87a90fSSagi Grimberg 	fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
510dc87a90fSSagi Grimberg 							 ISCSI_ISER_SG_TABLESIZE);
511dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_frpl)) {
512dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frpl err=%ld\n",
513dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_frpl));
514dc87a90fSSagi Grimberg 		return PTR_ERR(fr_desc->data_frpl);
515dc87a90fSSagi Grimberg 	}
516dc87a90fSSagi Grimberg 
517dc87a90fSSagi Grimberg 	fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
518dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
519dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frmr err=%ld\n",
520dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_mr));
521d3e125daSSagi Grimberg 		ret = PTR_ERR(fr_desc->data_mr);
522d3e125daSSagi Grimberg 		goto err_data_frpl;
523dc87a90fSSagi Grimberg 	}
524d3e125daSSagi Grimberg 	fr_desc->ind |= ISERT_DATA_KEY_VALID;
525dc87a90fSSagi Grimberg 
526*570db170SSagi Grimberg 	pr_debug("Created fr_desc %p\n", fr_desc);
527dc87a90fSSagi Grimberg 
528dc87a90fSSagi Grimberg 	return 0;
529*570db170SSagi Grimberg 
530d3e125daSSagi Grimberg err_data_frpl:
531d3e125daSSagi Grimberg 	ib_free_fast_reg_page_list(fr_desc->data_frpl);
532d3e125daSSagi Grimberg 
533d3e125daSSagi Grimberg 	return ret;
534dc87a90fSSagi Grimberg }
535dc87a90fSSagi Grimberg 
536dc87a90fSSagi Grimberg static int
537*570db170SSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
53859464ef4SVu Pham {
53959464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
54059464ef4SVu Pham 	struct isert_device *device = isert_conn->conn_device;
541f46d6a8aSNicholas Bellinger 	struct se_session *se_sess = isert_conn->conn->sess->se_sess;
542f46d6a8aSNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
543f46d6a8aSNicholas Bellinger 	int i, ret, tag_num;
544f46d6a8aSNicholas Bellinger 	/*
545f46d6a8aSNicholas Bellinger 	 * Setup the number of FRMRs based upon the number of tags
546f46d6a8aSNicholas Bellinger 	 * available to session in iscsi_target_locate_portal().
547f46d6a8aSNicholas Bellinger 	 */
548f46d6a8aSNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
549f46d6a8aSNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
55059464ef4SVu Pham 
551a3a5a826SSagi Grimberg 	isert_conn->conn_fr_pool_size = 0;
552f46d6a8aSNicholas Bellinger 	for (i = 0; i < tag_num; i++) {
55359464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
55459464ef4SVu Pham 		if (!fr_desc) {
55559464ef4SVu Pham 			pr_err("Failed to allocate fast_reg descriptor\n");
55659464ef4SVu Pham 			ret = -ENOMEM;
55759464ef4SVu Pham 			goto err;
55859464ef4SVu Pham 		}
55959464ef4SVu Pham 
560dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
561*570db170SSagi Grimberg 					   isert_conn->conn_pd, fr_desc);
562dc87a90fSSagi Grimberg 		if (ret) {
563dc87a90fSSagi Grimberg 			pr_err("Failed to create fastreg descriptor err=%d\n",
564dc87a90fSSagi Grimberg 			       ret);
565a80e21b3SNicholas Bellinger 			kfree(fr_desc);
56659464ef4SVu Pham 			goto err;
56759464ef4SVu Pham 		}
56859464ef4SVu Pham 
569a3a5a826SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
570a3a5a826SSagi Grimberg 		isert_conn->conn_fr_pool_size++;
57159464ef4SVu Pham 	}
57259464ef4SVu Pham 
573a3a5a826SSagi Grimberg 	pr_debug("Creating conn %p fastreg pool size=%d",
574a3a5a826SSagi Grimberg 		 isert_conn, isert_conn->conn_fr_pool_size);
57559464ef4SVu Pham 
57659464ef4SVu Pham 	return 0;
57759464ef4SVu Pham 
57859464ef4SVu Pham err:
579a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
58059464ef4SVu Pham 	return ret;
58159464ef4SVu Pham }
58259464ef4SVu Pham 
583b8d26b3bSNicholas Bellinger static int
584b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
585b8d26b3bSNicholas Bellinger {
586ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
587ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
588b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
589b8d26b3bSNicholas Bellinger 	struct isert_device *device;
590b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
591b8d26b3bSNicholas Bellinger 	int ret = 0;
59214f4b54fSSagi Grimberg 
59314f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
59414f4b54fSSagi Grimberg 	if (!np->enabled) {
59514f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
59614f4b54fSSagi Grimberg 		pr_debug("iscsi_np is not enabled, reject connect request\n");
59714f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
59814f4b54fSSagi Grimberg 	}
59914f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
600b8d26b3bSNicholas Bellinger 
601b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
602b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
603b8d26b3bSNicholas Bellinger 
604b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
605b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
606b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn\n");
607b8d26b3bSNicholas Bellinger 		return -ENOMEM;
608b8d26b3bSNicholas Bellinger 	}
609b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
610b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
611b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
6122371e5daSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
613defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait);
614defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait_comp_err);
615b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
616b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
61759464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
618f46d6a8aSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
619b8d26b3bSNicholas Bellinger 
620b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
621b8d26b3bSNicholas Bellinger 
622b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
623b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
624b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
625b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
626b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
627b8d26b3bSNicholas Bellinger 		goto out;
628b8d26b3bSNicholas Bellinger 	}
629b8d26b3bSNicholas Bellinger 
630b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
631b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
632b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
633b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
634b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
635b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
636b8d26b3bSNicholas Bellinger 
637b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
638b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
639b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
640b8d26b3bSNicholas Bellinger 
641b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
642b8d26b3bSNicholas Bellinger 	if (ret) {
643b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
644b8d26b3bSNicholas Bellinger 		       ret);
645b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
646b8d26b3bSNicholas Bellinger 		goto out_login_buf;
647b8d26b3bSNicholas Bellinger 	}
648b8d26b3bSNicholas Bellinger 
649b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
650b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
651b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
652b8d26b3bSNicholas Bellinger 
653b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
654b8d26b3bSNicholas Bellinger 	if (ret) {
655b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
656b8d26b3bSNicholas Bellinger 		       ret);
657b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
658b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
659b8d26b3bSNicholas Bellinger 	}
660b8d26b3bSNicholas Bellinger 
661b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
662b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
663b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
664b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
665b8d26b3bSNicholas Bellinger 	}
666b8d26b3bSNicholas Bellinger 
6671a92e17eSSagi Grimberg 	/* Set max inflight RDMA READ requests */
6681a92e17eSSagi Grimberg 	isert_conn->initiator_depth = min_t(u8,
6691a92e17eSSagi Grimberg 				event->param.conn.initiator_depth,
6701a92e17eSSagi Grimberg 				device->dev_attr.max_qp_init_rd_atom);
6711a92e17eSSagi Grimberg 	pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
6721a92e17eSSagi Grimberg 
673b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
674eb6ab132SSagi Grimberg 	isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
675eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_pd)) {
676eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_pd);
677eb6ab132SSagi Grimberg 		pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
678eb6ab132SSagi Grimberg 		       isert_conn, ret);
679eb6ab132SSagi Grimberg 		goto out_pd;
680eb6ab132SSagi Grimberg 	}
681b8d26b3bSNicholas Bellinger 
682eb6ab132SSagi Grimberg 	isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
683eb6ab132SSagi Grimberg 					   IB_ACCESS_LOCAL_WRITE);
684eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_mr)) {
685eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_mr);
686eb6ab132SSagi Grimberg 		pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
687eb6ab132SSagi Grimberg 		       isert_conn, ret);
688eb6ab132SSagi Grimberg 		goto out_mr;
689eb6ab132SSagi Grimberg 	}
690b8d26b3bSNicholas Bellinger 
691*570db170SSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id);
692b8d26b3bSNicholas Bellinger 	if (ret)
693b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
694b8d26b3bSNicholas Bellinger 
6952371e5daSSagi Grimberg 	ret = isert_rdma_post_recvl(isert_conn);
6962371e5daSSagi Grimberg 	if (ret)
6972371e5daSSagi Grimberg 		goto out_conn_dev;
6982371e5daSSagi Grimberg 
6992371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
7002371e5daSSagi Grimberg 	if (ret)
7012371e5daSSagi Grimberg 		goto out_conn_dev;
7022371e5daSSagi Grimberg 
703b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
7049fe63c88SSagi Grimberg 	list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
705b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
706b8d26b3bSNicholas Bellinger 
707531b7bf4SSagi Grimberg 	pr_debug("isert_connect_request() up np_sem np: %p\n", np);
708531b7bf4SSagi Grimberg 	up(&isert_np->np_sem);
709b8d26b3bSNicholas Bellinger 	return 0;
710b8d26b3bSNicholas Bellinger 
711b8d26b3bSNicholas Bellinger out_conn_dev:
712eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
713eb6ab132SSagi Grimberg out_mr:
714eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
715eb6ab132SSagi Grimberg out_pd:
716b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
717b8d26b3bSNicholas Bellinger out_rsp_dma_map:
718b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
719b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
720b8d26b3bSNicholas Bellinger out_req_dma_map:
721b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
722b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
723b8d26b3bSNicholas Bellinger out_login_buf:
724b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
725b8d26b3bSNicholas Bellinger out:
726b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
7272371e5daSSagi Grimberg 	rdma_reject(cma_id, NULL, 0);
728b8d26b3bSNicholas Bellinger 	return ret;
729b8d26b3bSNicholas Bellinger }
730b8d26b3bSNicholas Bellinger 
731b8d26b3bSNicholas Bellinger static void
732b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
733b8d26b3bSNicholas Bellinger {
734b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
735b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
736b8d26b3bSNicholas Bellinger 	int cq_index;
737b8d26b3bSNicholas Bellinger 
738b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
739b8d26b3bSNicholas Bellinger 
740a3a5a826SSagi Grimberg 	if (device && device->use_fastreg)
741a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
74259464ef4SVu Pham 
74319e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
74419e2090fSSagi Grimberg 	rdma_destroy_id(isert_conn->conn_cm_id);
74519e2090fSSagi Grimberg 
746b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
747b8d26b3bSNicholas Bellinger 		cq_index = ((struct isert_cq_desc *)
748b8d26b3bSNicholas Bellinger 			isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
749b8d26b3bSNicholas Bellinger 		pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
75019e2090fSSagi Grimberg 		mutex_lock(&device_list_mutex);
751b8d26b3bSNicholas Bellinger 		isert_conn->conn_device->cq_active_qps[cq_index]--;
75219e2090fSSagi Grimberg 		mutex_unlock(&device_list_mutex);
753b8d26b3bSNicholas Bellinger 
75419e2090fSSagi Grimberg 		ib_destroy_qp(isert_conn->conn_qp);
755b8d26b3bSNicholas Bellinger 	}
756b8d26b3bSNicholas Bellinger 
757eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
758eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
759eb6ab132SSagi Grimberg 
760b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
761b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
762b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
763b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
764b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
765b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
766b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
767b8d26b3bSNicholas Bellinger 	}
768b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
769b8d26b3bSNicholas Bellinger 
770b8d26b3bSNicholas Bellinger 	if (device)
771b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
772b8d26b3bSNicholas Bellinger 
773b8d26b3bSNicholas Bellinger 	pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
774b8d26b3bSNicholas Bellinger }
775b8d26b3bSNicholas Bellinger 
776b8d26b3bSNicholas Bellinger static void
777b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
778b8d26b3bSNicholas Bellinger {
77919e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
780c2f88b17SSagi Grimberg 
781128e9cc8SSagi Grimberg 	pr_info("conn %p\n", isert_conn);
782128e9cc8SSagi Grimberg 
7832371e5daSSagi Grimberg 	if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
7842371e5daSSagi Grimberg 		pr_warn("conn %p connect_release is running\n", isert_conn);
7852371e5daSSagi Grimberg 		return;
7862371e5daSSagi Grimberg 	}
7872371e5daSSagi Grimberg 
7882371e5daSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
7892371e5daSSagi Grimberg 	if (isert_conn->state != ISER_CONN_FULL_FEATURE)
790128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_UP;
7912371e5daSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
792b8d26b3bSNicholas Bellinger }
793b8d26b3bSNicholas Bellinger 
794b8d26b3bSNicholas Bellinger static void
795b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
796b8d26b3bSNicholas Bellinger {
797b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
798b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
799b8d26b3bSNicholas Bellinger 
800b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
801b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
802b8d26b3bSNicholas Bellinger 
803b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
804b8d26b3bSNicholas Bellinger }
805b8d26b3bSNicholas Bellinger 
806b8d26b3bSNicholas Bellinger static void
807b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
808b8d26b3bSNicholas Bellinger {
809b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
810b8d26b3bSNicholas Bellinger }
811b8d26b3bSNicholas Bellinger 
812954f2372SSagi Grimberg /**
813954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
814954f2372SSagi Grimberg  * @isert_conn: isert connection struct
815954f2372SSagi Grimberg  *
816954f2372SSagi Grimberg  * Notes:
817128e9cc8SSagi Grimberg  * In case the connection state is FULL_FEATURE, move state
818954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
819128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
820954f2372SSagi Grimberg  *
821954f2372SSagi Grimberg  * This routine must be called with conn_mutex held. Thus it is
822954f2372SSagi Grimberg  * safe to call multiple times.
823954f2372SSagi Grimberg  */
824954f2372SSagi Grimberg static void
825954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
826954f2372SSagi Grimberg {
827954f2372SSagi Grimberg 	int err;
828954f2372SSagi Grimberg 
829128e9cc8SSagi Grimberg 	switch (isert_conn->state) {
830128e9cc8SSagi Grimberg 	case ISER_CONN_TERMINATING:
831128e9cc8SSagi Grimberg 		break;
832128e9cc8SSagi Grimberg 	case ISER_CONN_UP:
833128e9cc8SSagi Grimberg 		/*
834128e9cc8SSagi Grimberg 		 * No flush completions will occur as we didn't
835128e9cc8SSagi Grimberg 		 * get to ISER_CONN_FULL_FEATURE yet, complete
836128e9cc8SSagi Grimberg 		 * to allow teardown progress.
837128e9cc8SSagi Grimberg 		 */
838128e9cc8SSagi Grimberg 		complete(&isert_conn->conn_wait_comp_err);
839128e9cc8SSagi Grimberg 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
840954f2372SSagi Grimberg 		pr_info("Terminating conn %p state %d\n",
841954f2372SSagi Grimberg 			   isert_conn, isert_conn->state);
842128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_TERMINATING;
843954f2372SSagi Grimberg 		err = rdma_disconnect(isert_conn->conn_cm_id);
844954f2372SSagi Grimberg 		if (err)
845954f2372SSagi Grimberg 			pr_warn("Failed rdma_disconnect isert_conn %p\n",
846954f2372SSagi Grimberg 				   isert_conn);
847128e9cc8SSagi Grimberg 		break;
848128e9cc8SSagi Grimberg 	default:
849128e9cc8SSagi Grimberg 		pr_warn("conn %p teminating in state %d\n",
850128e9cc8SSagi Grimberg 			   isert_conn, isert_conn->state);
851954f2372SSagi Grimberg 	}
852954f2372SSagi Grimberg }
853954f2372SSagi Grimberg 
8543b726ae2SSagi Grimberg static int
855ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
856ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
857b8d26b3bSNicholas Bellinger {
858ca6c1d82SSagi Grimberg 	pr_debug("isert np %p, handling event %d\n", isert_np, event);
8593b726ae2SSagi Grimberg 
860ca6c1d82SSagi Grimberg 	switch (event) {
861ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
8623b726ae2SSagi Grimberg 		isert_np->np_cm_id = NULL;
863ca6c1d82SSagi Grimberg 		break;
864ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
865ca6c1d82SSagi Grimberg 		isert_np->np_cm_id = isert_setup_id(isert_np);
866ca6c1d82SSagi Grimberg 		if (IS_ERR(isert_np->np_cm_id)) {
867ca6c1d82SSagi Grimberg 			pr_err("isert np %p setup id failed: %ld\n",
868ca6c1d82SSagi Grimberg 				 isert_np, PTR_ERR(isert_np->np_cm_id));
869ca6c1d82SSagi Grimberg 			isert_np->np_cm_id = NULL;
870ca6c1d82SSagi Grimberg 		}
871ca6c1d82SSagi Grimberg 		break;
872ca6c1d82SSagi Grimberg 	default:
873ca6c1d82SSagi Grimberg 		pr_err("isert np %p Unexpected event %d\n",
874ca6c1d82SSagi Grimberg 			  isert_np, event);
875ca6c1d82SSagi Grimberg 	}
876ca6c1d82SSagi Grimberg 
8773b726ae2SSagi Grimberg 	return -1;
8783b726ae2SSagi Grimberg }
8793b726ae2SSagi Grimberg 
880ca6c1d82SSagi Grimberg static int
881ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
882ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
883ca6c1d82SSagi Grimberg {
884ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
885ca6c1d82SSagi Grimberg 	struct isert_conn *isert_conn;
886ca6c1d82SSagi Grimberg 
887ca6c1d82SSagi Grimberg 	if (isert_np->np_cm_id == cma_id)
888ca6c1d82SSagi Grimberg 		return isert_np_cma_handler(cma_id->context, event);
889ca6c1d82SSagi Grimberg 
89019e2090fSSagi Grimberg 	isert_conn = cma_id->qp->qp_context;
891b8d26b3bSNicholas Bellinger 
892128e9cc8SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
893128e9cc8SSagi Grimberg 	isert_conn_terminate(isert_conn);
894128e9cc8SSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
895128e9cc8SSagi Grimberg 
896128e9cc8SSagi Grimberg 	pr_info("conn %p completing conn_wait\n", isert_conn);
897128e9cc8SSagi Grimberg 	complete(&isert_conn->conn_wait);
8983b726ae2SSagi Grimberg 
8993b726ae2SSagi Grimberg 	return 0;
900b8d26b3bSNicholas Bellinger }
901b8d26b3bSNicholas Bellinger 
902954f2372SSagi Grimberg static void
903954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
904954f2372SSagi Grimberg {
90519e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
906954f2372SSagi Grimberg 
907954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
908954f2372SSagi Grimberg }
909954f2372SSagi Grimberg 
910b8d26b3bSNicholas Bellinger static int
911b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
912b8d26b3bSNicholas Bellinger {
913b8d26b3bSNicholas Bellinger 	int ret = 0;
914b8d26b3bSNicholas Bellinger 
915b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
916b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
917b8d26b3bSNicholas Bellinger 
918b8d26b3bSNicholas Bellinger 	switch (event->event) {
919b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
920b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
9213b726ae2SSagi Grimberg 		if (ret)
9223b726ae2SSagi Grimberg 			pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
9233b726ae2SSagi Grimberg 				event->event, ret);
924b8d26b3bSNicholas Bellinger 		break;
925b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
926b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
927b8d26b3bSNicholas Bellinger 		break;
92888c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
92988c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
93088c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
93188c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
932ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
933b8d26b3bSNicholas Bellinger 		break;
934954f2372SSagi Grimberg 	case RDMA_CM_EVENT_REJECTED:       /* FALLTHRU */
935954f2372SSagi Grimberg 	case RDMA_CM_EVENT_UNREACHABLE:    /* FALLTHRU */
936b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
937954f2372SSagi Grimberg 		isert_connect_error(cma_id);
938954f2372SSagi Grimberg 		break;
939b8d26b3bSNicholas Bellinger 	default:
94088c4015fSSagi Grimberg 		pr_err("Unhandled RDMA CMA event: %d\n", event->event);
941b8d26b3bSNicholas Bellinger 		break;
942b8d26b3bSNicholas Bellinger 	}
943b8d26b3bSNicholas Bellinger 
944b8d26b3bSNicholas Bellinger 	return ret;
945b8d26b3bSNicholas Bellinger }
946b8d26b3bSNicholas Bellinger 
947b8d26b3bSNicholas Bellinger static int
948b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
949b8d26b3bSNicholas Bellinger {
950b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
951b8d26b3bSNicholas Bellinger 	int i, ret;
952b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
953b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
954b8d26b3bSNicholas Bellinger 
955b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
956b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
957b8d26b3bSNicholas Bellinger 		rx_wr->wr_id	= (unsigned long)rx_desc;
958b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
959b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
960b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
961b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
962b8d26b3bSNicholas Bellinger 	}
963b8d26b3bSNicholas Bellinger 
964b8d26b3bSNicholas Bellinger 	rx_wr--;
965b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
966b8d26b3bSNicholas Bellinger 
967b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
968b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
969b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
970b8d26b3bSNicholas Bellinger 	if (ret) {
971b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed with ret: %d\n", ret);
972b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
973b8d26b3bSNicholas Bellinger 	} else {
974b8d26b3bSNicholas Bellinger 		pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
975b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
976b8d26b3bSNicholas Bellinger 	}
977b8d26b3bSNicholas Bellinger 	return ret;
978b8d26b3bSNicholas Bellinger }
979b8d26b3bSNicholas Bellinger 
980b8d26b3bSNicholas Bellinger static int
981b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
982b8d26b3bSNicholas Bellinger {
983b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
984b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
985b8d26b3bSNicholas Bellinger 	int ret;
986b8d26b3bSNicholas Bellinger 
987b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
988b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
989b8d26b3bSNicholas Bellinger 
990b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
991b8d26b3bSNicholas Bellinger 	send_wr.wr_id	= (unsigned long)tx_desc;
992b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
993b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
994b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
995b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
996b8d26b3bSNicholas Bellinger 
997b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
998b8d26b3bSNicholas Bellinger 
999b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
1000b8d26b3bSNicholas Bellinger 	if (ret) {
1001b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send() failed, ret: %d\n", ret);
1002b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1003b8d26b3bSNicholas Bellinger 	}
1004b8d26b3bSNicholas Bellinger 
1005b8d26b3bSNicholas Bellinger 	return ret;
1006b8d26b3bSNicholas Bellinger }
1007b8d26b3bSNicholas Bellinger 
1008b8d26b3bSNicholas Bellinger static void
1009b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
1010b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
1011b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
1012b8d26b3bSNicholas Bellinger {
1013b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1014b8d26b3bSNicholas Bellinger 
1015b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1016b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
1017b8d26b3bSNicholas Bellinger 
1018b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1019b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
1020b8d26b3bSNicholas Bellinger 
1021b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
1022b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
1023b8d26b3bSNicholas Bellinger 
1024b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
1025b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1026b8d26b3bSNicholas Bellinger 		pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1027b8d26b3bSNicholas Bellinger 	}
1028b8d26b3bSNicholas Bellinger }
1029b8d26b3bSNicholas Bellinger 
1030b8d26b3bSNicholas Bellinger static int
1031b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
1032b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
1033b8d26b3bSNicholas Bellinger {
1034b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1035b8d26b3bSNicholas Bellinger 	u64 dma_addr;
1036b8d26b3bSNicholas Bellinger 
1037b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1038b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
1039b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
1040b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error() failed\n");
1041b8d26b3bSNicholas Bellinger 		return -ENOMEM;
1042b8d26b3bSNicholas Bellinger 	}
1043b8d26b3bSNicholas Bellinger 
1044b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
1045b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
1046b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1047b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1048b8d26b3bSNicholas Bellinger 
1049b8d26b3bSNicholas Bellinger 	pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
1050b8d26b3bSNicholas Bellinger 		 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
1051b8d26b3bSNicholas Bellinger 		 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
1052b8d26b3bSNicholas Bellinger 
1053b8d26b3bSNicholas Bellinger 	return 0;
1054b8d26b3bSNicholas Bellinger }
1055b8d26b3bSNicholas Bellinger 
1056b8d26b3bSNicholas Bellinger static void
105795b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
105895b60f07SNicholas Bellinger 		   struct ib_send_wr *send_wr, bool coalesce)
1059b8d26b3bSNicholas Bellinger {
106095b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
106195b60f07SNicholas Bellinger 
1062b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1063b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1064b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
106595b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
1066b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
106795b60f07SNicholas Bellinger 	/*
106895b60f07SNicholas Bellinger 	 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
106995b60f07SNicholas Bellinger 	 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
107095b60f07SNicholas Bellinger 	 */
1071ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
1072128e9cc8SSagi Grimberg 	if (coalesce && isert_conn->state == ISER_CONN_FULL_FEATURE &&
107395b60f07SNicholas Bellinger 	    ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
1074ebbe4421SNicholas Bellinger 		tx_desc->llnode_active = true;
107595b60f07SNicholas Bellinger 		llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
1076ebbe4421SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
107795b60f07SNicholas Bellinger 		return;
107895b60f07SNicholas Bellinger 	}
107995b60f07SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
108095b60f07SNicholas Bellinger 	tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
1081ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
108295b60f07SNicholas Bellinger 
108395b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
1084b8d26b3bSNicholas Bellinger }
1085b8d26b3bSNicholas Bellinger 
1086b8d26b3bSNicholas Bellinger static int
1087b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
1088b8d26b3bSNicholas Bellinger {
1089b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
1090b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
1091b8d26b3bSNicholas Bellinger 	int ret;
1092b8d26b3bSNicholas Bellinger 
1093b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
1094b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
1095b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
1096b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
1097b8d26b3bSNicholas Bellinger 
1098b8d26b3bSNicholas Bellinger 	pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1099b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
1100b8d26b3bSNicholas Bellinger 
1101b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1102b8d26b3bSNicholas Bellinger 	rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1103b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1104b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1105b8d26b3bSNicholas Bellinger 
1106b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1107b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1108b8d26b3bSNicholas Bellinger 	if (ret) {
1109b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed: %d\n", ret);
1110b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1111b8d26b3bSNicholas Bellinger 	}
1112b8d26b3bSNicholas Bellinger 
1113b8d26b3bSNicholas Bellinger 	pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1114b8d26b3bSNicholas Bellinger 	return ret;
1115b8d26b3bSNicholas Bellinger }
1116b8d26b3bSNicholas Bellinger 
1117b8d26b3bSNicholas Bellinger static int
1118b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1119b8d26b3bSNicholas Bellinger 		   u32 length)
1120b8d26b3bSNicholas Bellinger {
1121b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1122b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1123b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1124b8d26b3bSNicholas Bellinger 	int ret;
1125b8d26b3bSNicholas Bellinger 
1126b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1127b8d26b3bSNicholas Bellinger 
1128b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1129b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1130b8d26b3bSNicholas Bellinger 
1131b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1132b8d26b3bSNicholas Bellinger 
1133b8d26b3bSNicholas Bellinger 	if (length > 0) {
1134b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1135b8d26b3bSNicholas Bellinger 
1136b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1137b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1138b8d26b3bSNicholas Bellinger 
1139b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1140b8d26b3bSNicholas Bellinger 
1141b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1142b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1143b8d26b3bSNicholas Bellinger 
1144b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1145b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
1146b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1147b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1148b8d26b3bSNicholas Bellinger 	}
1149b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1150b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1151e0546fc1SSagi Grimberg 			if (!conn->sess->sess_ops->SessionType &&
1152e0546fc1SSagi Grimberg 			    isert_conn->conn_device->use_fastreg) {
1153*570db170SSagi Grimberg 				ret = isert_conn_create_fastreg_pool(isert_conn);
1154f46d6a8aSNicholas Bellinger 				if (ret) {
1155f46d6a8aSNicholas Bellinger 					pr_err("Conn: %p failed to create"
1156f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1157f46d6a8aSNicholas Bellinger 					return ret;
1158f46d6a8aSNicholas Bellinger 				}
1159f46d6a8aSNicholas Bellinger 			}
1160f46d6a8aSNicholas Bellinger 
1161b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1162b8d26b3bSNicholas Bellinger 			if (ret)
1163b8d26b3bSNicholas Bellinger 				return ret;
1164b8d26b3bSNicholas Bellinger 
1165b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1166b8d26b3bSNicholas Bellinger 			if (ret)
1167b8d26b3bSNicholas Bellinger 				return ret;
1168b8d26b3bSNicholas Bellinger 
1169128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
11702371e5daSSagi Grimberg 			mutex_lock(&isert_conn->conn_mutex);
1171128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
11722371e5daSSagi Grimberg 			mutex_unlock(&isert_conn->conn_mutex);
1173b8d26b3bSNicholas Bellinger 			goto post_send;
1174b8d26b3bSNicholas Bellinger 		}
1175b8d26b3bSNicholas Bellinger 
1176b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1177b8d26b3bSNicholas Bellinger 		if (ret)
1178b8d26b3bSNicholas Bellinger 			return ret;
1179b8d26b3bSNicholas Bellinger 	}
1180b8d26b3bSNicholas Bellinger post_send:
1181b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1182b8d26b3bSNicholas Bellinger 	if (ret)
1183b8d26b3bSNicholas Bellinger 		return ret;
1184b8d26b3bSNicholas Bellinger 
1185b8d26b3bSNicholas Bellinger 	return 0;
1186b8d26b3bSNicholas Bellinger }
1187b8d26b3bSNicholas Bellinger 
1188b8d26b3bSNicholas Bellinger static void
11892371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1190b8d26b3bSNicholas Bellinger {
11912371e5daSSagi Grimberg 	struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
11922371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1193b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1194b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1195b8d26b3bSNicholas Bellinger 	int size;
1196b8d26b3bSNicholas Bellinger 
11972371e5daSSagi Grimberg 	pr_info("conn %p\n", isert_conn);
11982371e5daSSagi Grimberg 
11992371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1200b8d26b3bSNicholas Bellinger 
1201b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1202b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1203b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1204b8d26b3bSNicholas Bellinger 		/*
1205b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1206b8d26b3bSNicholas Bellinger 		 * login request PDU.
1207b8d26b3bSNicholas Bellinger 		 */
1208b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1209b8d26b3bSNicholas Bellinger 		login->current_stage =
1210b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1211b8d26b3bSNicholas Bellinger 			 >> 2;
1212b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1213b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1214b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1215b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1216b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1217b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1218b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1219b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1220b8d26b3bSNicholas Bellinger 	}
1221b8d26b3bSNicholas Bellinger 
1222b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1223b8d26b3bSNicholas Bellinger 
1224b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1225b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1226b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1227b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1228b8d26b3bSNicholas Bellinger 
12296faaa85fSNicholas Bellinger 	if (login->first_request) {
1230b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
12316faaa85fSNicholas Bellinger 		return;
12326faaa85fSNicholas Bellinger 	}
12336faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1234b8d26b3bSNicholas Bellinger }
1235b8d26b3bSNicholas Bellinger 
1236b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1237676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1238b8d26b3bSNicholas Bellinger {
1239b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1240b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1241d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1242b8d26b3bSNicholas Bellinger 
1243676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1244d703ce2fSNicholas Bellinger 	if (!cmd) {
1245d703ce2fSNicholas Bellinger 		pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1246b8d26b3bSNicholas Bellinger 		return NULL;
1247b8d26b3bSNicholas Bellinger 	}
1248d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1249b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1250d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1251b8d26b3bSNicholas Bellinger 
1252d703ce2fSNicholas Bellinger 	return cmd;
1253b8d26b3bSNicholas Bellinger }
1254b8d26b3bSNicholas Bellinger 
1255b8d26b3bSNicholas Bellinger static int
1256b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1257d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1258d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1259b8d26b3bSNicholas Bellinger {
1260b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1261b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1262b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1263b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1264b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1265b8d26b3bSNicholas Bellinger 
1266b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1267b8d26b3bSNicholas Bellinger 	if (rc < 0)
1268b8d26b3bSNicholas Bellinger 		return rc;
1269b8d26b3bSNicholas Bellinger 
1270b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1271b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1272b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1273b8d26b3bSNicholas Bellinger 
1274b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1275b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1276b8d26b3bSNicholas Bellinger 		return 0;
1277b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1278b8d26b3bSNicholas Bellinger 		dump_payload = true;
1279b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1280b8d26b3bSNicholas Bellinger 	}
1281b8d26b3bSNicholas Bellinger 
1282b8d26b3bSNicholas Bellinger 	if (!imm_data)
1283b8d26b3bSNicholas Bellinger 		return 0;
1284b8d26b3bSNicholas Bellinger 
1285b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1286b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1287b8d26b3bSNicholas Bellinger 
1288b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1289b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1290b8d26b3bSNicholas Bellinger 
1291b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1292b8d26b3bSNicholas Bellinger 
1293b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1294b8d26b3bSNicholas Bellinger 
1295b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1296b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1297b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1298b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1299b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1300b8d26b3bSNicholas Bellinger 	}
1301b8d26b3bSNicholas Bellinger 
1302b8d26b3bSNicholas Bellinger sequence_cmd:
1303561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1304b8d26b3bSNicholas Bellinger 
1305b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1306b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
13076cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
13086cc44a6fSNicholas Bellinger 		target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1309b8d26b3bSNicholas Bellinger 
1310b8d26b3bSNicholas Bellinger 	return 0;
1311b8d26b3bSNicholas Bellinger }
1312b8d26b3bSNicholas Bellinger 
1313b8d26b3bSNicholas Bellinger static int
1314b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1315b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1316b8d26b3bSNicholas Bellinger {
1317b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1318b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1319b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1320b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1321b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1322b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1323b8d26b3bSNicholas Bellinger 
1324b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1325b8d26b3bSNicholas Bellinger 	if (rc < 0)
1326b8d26b3bSNicholas Bellinger 		return rc;
1327b8d26b3bSNicholas Bellinger 	else if (!cmd)
1328b8d26b3bSNicholas Bellinger 		return 0;
1329b8d26b3bSNicholas Bellinger 	/*
1330b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1331b8d26b3bSNicholas Bellinger 	 */
1332b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
1333b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
1334b8d26b3bSNicholas Bellinger 		dump_stack();
1335b8d26b3bSNicholas Bellinger 		return -1;
1336b8d26b3bSNicholas Bellinger 	}
1337b8d26b3bSNicholas Bellinger 
1338b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1339b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1340b8d26b3bSNicholas Bellinger 
1341b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1342b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1343b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1344b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1345b8d26b3bSNicholas Bellinger 	/*
1346b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1347b8d26b3bSNicholas Bellinger 	 */
1348b8d26b3bSNicholas Bellinger 	if (page_off) {
1349b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1350b8d26b3bSNicholas Bellinger 		dump_stack();
1351b8d26b3bSNicholas Bellinger 		return -1;
1352b8d26b3bSNicholas Bellinger 	}
1353b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1354b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1355b8d26b3bSNicholas Bellinger 
1356b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1357b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1358b8d26b3bSNicholas Bellinger 
1359b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1360b8d26b3bSNicholas Bellinger 	if (rc < 0)
1361b8d26b3bSNicholas Bellinger 		return rc;
1362b8d26b3bSNicholas Bellinger 
1363b8d26b3bSNicholas Bellinger 	return 0;
1364b8d26b3bSNicholas Bellinger }
1365b8d26b3bSNicholas Bellinger 
1366b8d26b3bSNicholas Bellinger static int
1367778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1368d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1369d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1370778de368SNicholas Bellinger {
1371778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1372778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1373778de368SNicholas Bellinger 	int rc;
1374778de368SNicholas Bellinger 
1375778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1376778de368SNicholas Bellinger 	if (rc < 0)
1377778de368SNicholas Bellinger 		return rc;
1378778de368SNicholas Bellinger 	/*
1379778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1380778de368SNicholas Bellinger 	 */
1381778de368SNicholas Bellinger 
1382778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1383778de368SNicholas Bellinger }
1384778de368SNicholas Bellinger 
1385778de368SNicholas Bellinger static int
1386adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1387d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1388d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1389adb54c29SNicholas Bellinger {
1390adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1391adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1392adb54c29SNicholas Bellinger 	int rc;
1393adb54c29SNicholas Bellinger 	unsigned char *text_in;
1394adb54c29SNicholas Bellinger 
1395adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1396adb54c29SNicholas Bellinger 	if (rc < 0)
1397adb54c29SNicholas Bellinger 		return rc;
1398adb54c29SNicholas Bellinger 
1399adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1400adb54c29SNicholas Bellinger 	if (!text_in) {
1401adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1402adb54c29SNicholas Bellinger 		       payload_length);
1403adb54c29SNicholas Bellinger 		return -ENOMEM;
1404adb54c29SNicholas Bellinger 	}
1405adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1406adb54c29SNicholas Bellinger 
1407adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1408adb54c29SNicholas Bellinger 
1409adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1410adb54c29SNicholas Bellinger }
1411adb54c29SNicholas Bellinger 
1412adb54c29SNicholas Bellinger static int
1413b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1414b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1415b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1416b8d26b3bSNicholas Bellinger {
1417b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1418b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1419ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1420b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1421b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1422b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1423b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1424b8d26b3bSNicholas Bellinger 
1425ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1426ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1427ca40d24eSNicholas Bellinger 		pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1428ca40d24eSNicholas Bellinger 		       " ignoring\n", opcode);
1429ca40d24eSNicholas Bellinger 		return 0;
1430ca40d24eSNicholas Bellinger 	}
1431ca40d24eSNicholas Bellinger 
1432b8d26b3bSNicholas Bellinger 	switch (opcode) {
1433b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1434676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1435b8d26b3bSNicholas Bellinger 		if (!cmd)
1436b8d26b3bSNicholas Bellinger 			break;
1437b8d26b3bSNicholas Bellinger 
1438d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1439b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1440b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1441b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1442b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1443b8d26b3bSNicholas Bellinger 
1444d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1445b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1446b8d26b3bSNicholas Bellinger 		break;
1447b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1448676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1449b8d26b3bSNicholas Bellinger 		if (!cmd)
1450b8d26b3bSNicholas Bellinger 			break;
1451b8d26b3bSNicholas Bellinger 
1452d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1453d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1454778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1455b8d26b3bSNicholas Bellinger 		break;
1456b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1457b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1458b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1459b8d26b3bSNicholas Bellinger 		break;
1460b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1461676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1462b8d26b3bSNicholas Bellinger 		if (!cmd)
1463b8d26b3bSNicholas Bellinger 			break;
1464b8d26b3bSNicholas Bellinger 
1465b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1466b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1467b8d26b3bSNicholas Bellinger 		break;
1468b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1469676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1470b8d26b3bSNicholas Bellinger 		if (!cmd)
1471b8d26b3bSNicholas Bellinger 			break;
1472b8d26b3bSNicholas Bellinger 
1473b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1474b8d26b3bSNicholas Bellinger 		if (ret > 0)
1475b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1476b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1477b8d26b3bSNicholas Bellinger 						    HZ);
1478b8d26b3bSNicholas Bellinger 		break;
1479adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1480676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1481adb54c29SNicholas Bellinger 		if (!cmd)
1482adb54c29SNicholas Bellinger 			break;
1483adb54c29SNicholas Bellinger 
1484d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1485d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1486adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1487adb54c29SNicholas Bellinger 		break;
1488b8d26b3bSNicholas Bellinger 	default:
1489b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1490b8d26b3bSNicholas Bellinger 		dump_stack();
1491b8d26b3bSNicholas Bellinger 		break;
1492b8d26b3bSNicholas Bellinger 	}
1493b8d26b3bSNicholas Bellinger 
1494b8d26b3bSNicholas Bellinger 	return ret;
1495b8d26b3bSNicholas Bellinger }
1496b8d26b3bSNicholas Bellinger 
1497b8d26b3bSNicholas Bellinger static void
1498b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1499b8d26b3bSNicholas Bellinger {
1500b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1501b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1502b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1503b8d26b3bSNicholas Bellinger 	int rc;
1504b8d26b3bSNicholas Bellinger 
1505b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1506b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1507b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1508b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1509b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1510b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1511b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1512b8d26b3bSNicholas Bellinger 		}
1513b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1514b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1515b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1516b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1517b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1518b8d26b3bSNicholas Bellinger 		}
1519b8d26b3bSNicholas Bellinger 
1520b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1521b8d26b3bSNicholas Bellinger 		break;
1522b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1523b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1524b8d26b3bSNicholas Bellinger 		break;
1525b8d26b3bSNicholas Bellinger 	default:
1526b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1527b8d26b3bSNicholas Bellinger 		break;
1528b8d26b3bSNicholas Bellinger 	}
1529b8d26b3bSNicholas Bellinger 
1530b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1531b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1532b8d26b3bSNicholas Bellinger }
1533b8d26b3bSNicholas Bellinger 
1534b8d26b3bSNicholas Bellinger static void
1535b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1536b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1537b8d26b3bSNicholas Bellinger {
1538b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1539b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1540b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1541b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1542b8d26b3bSNicholas Bellinger 
1543b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1544b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1545b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1546b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1547b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1548b8d26b3bSNicholas Bellinger 	} else {
1549b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1550b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1551b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1552b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1553b8d26b3bSNicholas Bellinger 	}
1554b8d26b3bSNicholas Bellinger 
1555b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1556b8d26b3bSNicholas Bellinger 
1557b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1558b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1559b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1560b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1561b8d26b3bSNicholas Bellinger 
15622371e5daSSagi Grimberg 	if ((char *)desc == isert_conn->login_req_buf) {
15632371e5daSSagi Grimberg 		isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
15642371e5daSSagi Grimberg 		if (isert_conn->conn) {
15652371e5daSSagi Grimberg 			struct iscsi_login *login = isert_conn->conn->conn_login;
15662371e5daSSagi Grimberg 
15672371e5daSSagi Grimberg 			if (login && !login->first_request)
15682371e5daSSagi Grimberg 				isert_rx_login_req(isert_conn);
15692371e5daSSagi Grimberg 		}
15702371e5daSSagi Grimberg 		mutex_lock(&isert_conn->conn_mutex);
15712371e5daSSagi Grimberg 		complete(&isert_conn->login_req_comp);
15722371e5daSSagi Grimberg 		mutex_unlock(&isert_conn->conn_mutex);
15732371e5daSSagi Grimberg 	} else {
1574b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
15752371e5daSSagi Grimberg 	}
1576b8d26b3bSNicholas Bellinger 
1577b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1578b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1579b8d26b3bSNicholas Bellinger 
1580b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1581b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1582b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1583b8d26b3bSNicholas Bellinger 
1584b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1585b8d26b3bSNicholas Bellinger 		return;
1586b8d26b3bSNicholas Bellinger 
1587b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1588b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1589b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1590b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1591b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1592b8d26b3bSNicholas Bellinger 		if (err) {
1593b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1594b8d26b3bSNicholas Bellinger 			       count, err);
1595b8d26b3bSNicholas Bellinger 		}
1596b8d26b3bSNicholas Bellinger 	}
1597b8d26b3bSNicholas Bellinger }
1598b8d26b3bSNicholas Bellinger 
1599e3d7e4c3SSagi Grimberg static int
1600e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1601e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1602e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1603e3d7e4c3SSagi Grimberg {
1604e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1605e3d7e4c3SSagi Grimberg 
1606e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1607e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1608e3d7e4c3SSagi Grimberg 
1609e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1610e3d7e4c3SSagi Grimberg 	data->offset = offset;
1611e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1612e3d7e4c3SSagi Grimberg 
1613e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1614e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1615e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1616e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1617e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1618e3d7e4c3SSagi Grimberg 
1619e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1620e3d7e4c3SSagi Grimberg 					data->dma_dir);
1621e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
1622e3d7e4c3SSagi Grimberg 		pr_err("Cmd: unable to dma map SGs %p\n", sg);
1623e3d7e4c3SSagi Grimberg 		return -EINVAL;
1624e3d7e4c3SSagi Grimberg 	}
1625e3d7e4c3SSagi Grimberg 
1626e3d7e4c3SSagi Grimberg 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1627e3d7e4c3SSagi Grimberg 		 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1628e3d7e4c3SSagi Grimberg 
1629e3d7e4c3SSagi Grimberg 	return 0;
1630e3d7e4c3SSagi Grimberg }
1631e3d7e4c3SSagi Grimberg 
1632e3d7e4c3SSagi Grimberg static void
1633e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1634e3d7e4c3SSagi Grimberg {
1635e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1636e3d7e4c3SSagi Grimberg 
1637e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1638e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1639e3d7e4c3SSagi Grimberg }
1640e3d7e4c3SSagi Grimberg 
1641e3d7e4c3SSagi Grimberg 
1642e3d7e4c3SSagi Grimberg 
1643b8d26b3bSNicholas Bellinger static void
1644b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1645b8d26b3bSNicholas Bellinger {
1646b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1647b8d26b3bSNicholas Bellinger 
164890ecc6e2SVu Pham 	pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1649e3d7e4c3SSagi Grimberg 
1650e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
165190ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1652e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1653b8d26b3bSNicholas Bellinger 	}
1654b8d26b3bSNicholas Bellinger 
165590ecc6e2SVu Pham 	if (wr->send_wr) {
165690ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1657b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1658b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
165990ecc6e2SVu Pham 	}
1660b8d26b3bSNicholas Bellinger 
166190ecc6e2SVu Pham 	if (wr->ib_sge) {
166290ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
166390ecc6e2SVu Pham 		kfree(wr->ib_sge);
166490ecc6e2SVu Pham 		wr->ib_sge = NULL;
166590ecc6e2SVu Pham 	}
1666b8d26b3bSNicholas Bellinger }
1667b8d26b3bSNicholas Bellinger 
1668b8d26b3bSNicholas Bellinger static void
1669a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
167059464ef4SVu Pham {
167159464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
167259464ef4SVu Pham 	LIST_HEAD(unmap_list);
167359464ef4SVu Pham 
1674a3a5a826SSagi Grimberg 	pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
167559464ef4SVu Pham 
167659464ef4SVu Pham 	if (wr->fr_desc) {
1677a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
167859464ef4SVu Pham 			 isert_cmd, wr->fr_desc);
16799e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
16809e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
16819e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
16829e961ae7SSagi Grimberg 		}
168359464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1684a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
168559464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
168659464ef4SVu Pham 		wr->fr_desc = NULL;
168759464ef4SVu Pham 	}
168859464ef4SVu Pham 
1689e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
1690a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1691e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
169259464ef4SVu Pham 	}
169359464ef4SVu Pham 
169459464ef4SVu Pham 	wr->ib_sge = NULL;
169559464ef4SVu Pham 	wr->send_wr = NULL;
169659464ef4SVu Pham }
169759464ef4SVu Pham 
169859464ef4SVu Pham static void
169903e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1700b8d26b3bSNicholas Bellinger {
1701d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1702b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1703186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1704d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1705b8d26b3bSNicholas Bellinger 
1706b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1707b8d26b3bSNicholas Bellinger 
1708b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1709b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1710b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1711b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17125159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1713b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1714b8d26b3bSNicholas Bellinger 
171503e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1716b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
171703e7848aSNicholas Bellinger 			/*
171803e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
171903e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
172003e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
172103e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
172203e7848aSNicholas Bellinger 			 */
172303e7848aSNicholas Bellinger 			if (comp_err &&
172403e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
172503e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
172603e7848aSNicholas Bellinger 
172703e7848aSNicholas Bellinger 				target_put_sess_cmd(se_cmd->se_sess, se_cmd);
172803e7848aSNicholas Bellinger 			}
172903e7848aSNicholas Bellinger 		}
1730b8d26b3bSNicholas Bellinger 
1731d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1732186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1733186a9647SNicholas Bellinger 		break;
1734b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1735186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1736186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17375159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1738186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1739186a9647SNicholas Bellinger 
1740b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1741b8d26b3bSNicholas Bellinger 		break;
1742b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1743b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1744adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1745b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1746b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17475159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1748b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1749b8d26b3bSNicholas Bellinger 
1750b8d26b3bSNicholas Bellinger 		/*
1751b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1752b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1753b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1754b8d26b3bSNicholas Bellinger 		 */
1755b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
17563df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
17573df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
17583df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1759b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1760b8d26b3bSNicholas Bellinger 			break;
1761b8d26b3bSNicholas Bellinger 		}
1762b8d26b3bSNicholas Bellinger 		/*
1763b8d26b3bSNicholas Bellinger 		 * Fall-through
1764b8d26b3bSNicholas Bellinger 		 */
1765b8d26b3bSNicholas Bellinger 	default:
1766d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1767b8d26b3bSNicholas Bellinger 		break;
1768b8d26b3bSNicholas Bellinger 	}
1769b8d26b3bSNicholas Bellinger }
1770b8d26b3bSNicholas Bellinger 
1771b8d26b3bSNicholas Bellinger static void
1772b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1773b8d26b3bSNicholas Bellinger {
1774b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1775b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1776b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1777b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1778b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1779b8d26b3bSNicholas Bellinger 	}
1780b8d26b3bSNicholas Bellinger }
1781b8d26b3bSNicholas Bellinger 
1782b8d26b3bSNicholas Bellinger static void
1783b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
178403e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1785b8d26b3bSNicholas Bellinger {
1786dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1787dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1788dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1789dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1790dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1791b8d26b3bSNicholas Bellinger 	}
1792b8d26b3bSNicholas Bellinger 
1793b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
179403e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1795b8d26b3bSNicholas Bellinger }
1796b8d26b3bSNicholas Bellinger 
179796b7973eSSagi Grimberg static int
179896b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1799f93f3a70SSagi Grimberg {
18009e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
180196b7973eSSagi Grimberg 	int ret;
1802f93f3a70SSagi Grimberg 
180396b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
18049e961ae7SSagi Grimberg 	if (ret) {
18059e961ae7SSagi Grimberg 		pr_err("ib_check_mr_status failed, ret %d\n", ret);
18069e961ae7SSagi Grimberg 		goto fail_mr_status;
18079e961ae7SSagi Grimberg 	}
180896b7973eSSagi Grimberg 
18099e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
181096b7973eSSagi Grimberg 		u64 sec_offset_err;
18119e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
18129e961ae7SSagi Grimberg 
18139e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
18149e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
18159e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
18169e961ae7SSagi Grimberg 			break;
18179e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
18189e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
18199e961ae7SSagi Grimberg 			break;
18209e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
18219e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
18229e961ae7SSagi Grimberg 			break;
18239e961ae7SSagi Grimberg 		}
182496b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
182596b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
182696b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
18279e961ae7SSagi Grimberg 
18289e961ae7SSagi Grimberg 		pr_err("isert: PI error found type %d at sector 0x%llx "
18299e961ae7SSagi Grimberg 		       "expected 0x%x vs actual 0x%x\n",
18309e961ae7SSagi Grimberg 		       mr_status.sig_err.err_type,
18319e961ae7SSagi Grimberg 		       (unsigned long long)se_cmd->bad_sector,
18329e961ae7SSagi Grimberg 		       mr_status.sig_err.expected,
18339e961ae7SSagi Grimberg 		       mr_status.sig_err.actual);
18349e961ae7SSagi Grimberg 		ret = 1;
18359e961ae7SSagi Grimberg 	}
18369e961ae7SSagi Grimberg 
18379e961ae7SSagi Grimberg fail_mr_status:
183896b7973eSSagi Grimberg 	return ret;
183996b7973eSSagi Grimberg }
184096b7973eSSagi Grimberg 
184196b7973eSSagi Grimberg static void
184296b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
184396b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
184496b7973eSSagi Grimberg {
184596b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
184696b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
184796b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
184896b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
184996b7973eSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
185096b7973eSSagi Grimberg 	int ret = 0;
185196b7973eSSagi Grimberg 
185296b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
185396b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
185496b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
185596b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
185696b7973eSSagi Grimberg 	}
185796b7973eSSagi Grimberg 
1858f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1859897bb2c9SSagi Grimberg 	wr->send_wr_num = 0;
18609e961ae7SSagi Grimberg 	if (ret)
18619e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18629e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
18639e961ae7SSagi Grimberg 	else
1864f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1865f93f3a70SSagi Grimberg }
1866f93f3a70SSagi Grimberg 
1867f93f3a70SSagi Grimberg static void
1868b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1869b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1870b8d26b3bSNicholas Bellinger {
1871b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1872d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1873b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
187490ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1875d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
18765bac4b1aSSagi Grimberg 	int ret = 0;
1877b8d26b3bSNicholas Bellinger 
18789e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
187996b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
188096b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
188196b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
18829e961ae7SSagi Grimberg 	}
18839e961ae7SSagi Grimberg 
1884b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1885d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1886e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1887b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1888b8d26b3bSNicholas Bellinger 
188990ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1890b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1891b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1892b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1893b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1894b8d26b3bSNicholas Bellinger 
18955bac4b1aSSagi Grimberg 	if (ret)
18965bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18975bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
18985bac4b1aSSagi Grimberg 	else
1899b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1900b8d26b3bSNicholas Bellinger }
1901b8d26b3bSNicholas Bellinger 
1902b8d26b3bSNicholas Bellinger static void
1903b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1904b8d26b3bSNicholas Bellinger {
1905b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1906b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1907b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1908b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1909d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1910b8d26b3bSNicholas Bellinger 
1911b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1912b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1913b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1914b8d26b3bSNicholas Bellinger 
1915b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1916b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1917b8d26b3bSNicholas Bellinger 
1918b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
191903e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1920b8d26b3bSNicholas Bellinger 		break;
1921b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1922b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1923b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1924b8d26b3bSNicholas Bellinger 
1925b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
192603e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
19273df8f68aSNicholas Bellinger 		break;
1928b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1929b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
19309d49f5e2SSagi Grimberg 
19319d49f5e2SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1932b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1933b8d26b3bSNicholas Bellinger 		break;
1934adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1935adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1936adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
193703e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1938adb54c29SNicholas Bellinger 		break;
1939b8d26b3bSNicholas Bellinger 	default:
1940b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1941b8d26b3bSNicholas Bellinger 		dump_stack();
1942b8d26b3bSNicholas Bellinger 		break;
1943b8d26b3bSNicholas Bellinger 	}
1944b8d26b3bSNicholas Bellinger }
1945b8d26b3bSNicholas Bellinger 
1946b8d26b3bSNicholas Bellinger static void
1947b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1948b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1949b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1950b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1951b8d26b3bSNicholas Bellinger {
1952d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1953b6b87a1dSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1954b8d26b3bSNicholas Bellinger 
1955b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
19563df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1957adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1958adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1959b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1960b8d26b3bSNicholas Bellinger 
1961b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1962b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1963b8d26b3bSNicholas Bellinger 		return;
1964b8d26b3bSNicholas Bellinger 	}
1965897bb2c9SSagi Grimberg 
1966897bb2c9SSagi Grimberg 	/**
1967897bb2c9SSagi Grimberg 	 * If send_wr_num is 0 this means that we got
1968897bb2c9SSagi Grimberg 	 * RDMA completion and we cleared it and we should
1969897bb2c9SSagi Grimberg 	 * simply decrement the response post. else the
1970897bb2c9SSagi Grimberg 	 * response is incorporated in send_wr_num, just
1971897bb2c9SSagi Grimberg 	 * sub it.
1972897bb2c9SSagi Grimberg 	 **/
1973897bb2c9SSagi Grimberg 	if (wr->send_wr_num)
1974897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1975897bb2c9SSagi Grimberg 	else
1976897bb2c9SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1977b8d26b3bSNicholas Bellinger 
1978b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
197903e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1980b8d26b3bSNicholas Bellinger }
1981b8d26b3bSNicholas Bellinger 
1982b8d26b3bSNicholas Bellinger static void
198395b60f07SNicholas Bellinger __isert_send_completion(struct iser_tx_desc *tx_desc,
1984b8d26b3bSNicholas Bellinger 		        struct isert_conn *isert_conn)
1985b8d26b3bSNicholas Bellinger {
1986b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1987b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1988b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1989b8d26b3bSNicholas Bellinger 
1990b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1991b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1992b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1993b8d26b3bSNicholas Bellinger 		return;
1994b8d26b3bSNicholas Bellinger 	}
1995b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1996b8d26b3bSNicholas Bellinger 
1997b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1998b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
1999b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
2000b8d26b3bSNicholas Bellinger 		dump_stack();
2001b8d26b3bSNicholas Bellinger 		break;
2002b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
2003b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
2004b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
2005b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
2006b8d26b3bSNicholas Bellinger 		break;
2007b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
2008f93f3a70SSagi Grimberg 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
2009897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2010f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
2011b8d26b3bSNicholas Bellinger 		break;
2012b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
2013b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
2014b8d26b3bSNicholas Bellinger 
2015b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2016b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
2017b8d26b3bSNicholas Bellinger 		break;
2018b8d26b3bSNicholas Bellinger 	default:
2019b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
2020b8d26b3bSNicholas Bellinger 		dump_stack();
2021b8d26b3bSNicholas Bellinger 		break;
2022b8d26b3bSNicholas Bellinger 	}
2023b8d26b3bSNicholas Bellinger }
2024b8d26b3bSNicholas Bellinger 
2025b8d26b3bSNicholas Bellinger static void
202695b60f07SNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
202795b60f07SNicholas Bellinger 		      struct isert_conn *isert_conn)
202895b60f07SNicholas Bellinger {
202995b60f07SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
203095b60f07SNicholas Bellinger 	struct iser_tx_desc *t;
203195b60f07SNicholas Bellinger 	/*
203295b60f07SNicholas Bellinger 	 * Drain coalesced completion llist starting from comp_llnode_batch
203395b60f07SNicholas Bellinger 	 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
203495b60f07SNicholas Bellinger 	 */
203595b60f07SNicholas Bellinger 	while (llnode) {
203695b60f07SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
203795b60f07SNicholas Bellinger 		llnode = llist_next(llnode);
203895b60f07SNicholas Bellinger 		__isert_send_completion(t, isert_conn);
203995b60f07SNicholas Bellinger 	}
204095b60f07SNicholas Bellinger 	__isert_send_completion(tx_desc, isert_conn);
204195b60f07SNicholas Bellinger }
204295b60f07SNicholas Bellinger 
204395b60f07SNicholas Bellinger static void
2044ebbe4421SNicholas Bellinger isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
2045ebbe4421SNicholas Bellinger {
2046ebbe4421SNicholas Bellinger 	struct llist_node *llnode;
2047ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
2048ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
2049ebbe4421SNicholas Bellinger 
2050ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2051ebbe4421SNicholas Bellinger 	llnode = llist_del_all(&isert_conn->conn_comp_llist);
2052ebbe4421SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
2053ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2054ebbe4421SNicholas Bellinger 
2055ebbe4421SNicholas Bellinger 	while (llnode) {
2056ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2057ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
2058ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
2059ebbe4421SNicholas Bellinger 
2060897bb2c9SSagi Grimberg 		/**
2061897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
2062897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
2063897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
2064897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
2065897bb2c9SSagi Grimberg 		 * sub it.
2066897bb2c9SSagi Grimberg 		 **/
2067897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
2068897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
2069897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
2070897bb2c9SSagi Grimberg 		else
2071897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
207203e7848aSNicholas Bellinger 
207303e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
2074ebbe4421SNicholas Bellinger 	}
2075ebbe4421SNicholas Bellinger }
2076ebbe4421SNicholas Bellinger 
2077ebbe4421SNicholas Bellinger static void
2078defd8848SNicholas Bellinger isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
2079b8d26b3bSNicholas Bellinger {
2080b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2081b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2082ebbe4421SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
2083ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
2084ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
2085ebbe4421SNicholas Bellinger 
2086ebbe4421SNicholas Bellinger 	while (llnode) {
2087ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2088ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
2089ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
2090ebbe4421SNicholas Bellinger 
2091897bb2c9SSagi Grimberg 		/**
2092897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
2093897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
2094897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
2095897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
2096897bb2c9SSagi Grimberg 		 * sub it.
2097897bb2c9SSagi Grimberg 		 **/
2098897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
2099897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
2100897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
2101897bb2c9SSagi Grimberg 		else
2102897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
210303e7848aSNicholas Bellinger 
210403e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
2105ebbe4421SNicholas Bellinger 	}
2106ebbe4421SNicholas Bellinger 	tx_desc->comp_llnode_batch = NULL;
2107b8d26b3bSNicholas Bellinger 
2108b8d26b3bSNicholas Bellinger 	if (!isert_cmd)
2109b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
2110b8d26b3bSNicholas Bellinger 	else
211103e7848aSNicholas Bellinger 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
2112b8d26b3bSNicholas Bellinger }
2113b8d26b3bSNicholas Bellinger 
2114defd8848SNicholas Bellinger static void
2115defd8848SNicholas Bellinger isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2116defd8848SNicholas Bellinger {
2117defd8848SNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2118defd8848SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
2119defd8848SNicholas Bellinger 
2120defd8848SNicholas Bellinger 	if (isert_conn->post_recv_buf_count)
2121defd8848SNicholas Bellinger 		return;
2122defd8848SNicholas Bellinger 
2123ebbe4421SNicholas Bellinger 	isert_cq_drain_comp_llist(isert_conn, ib_dev);
2124ebbe4421SNicholas Bellinger 
2125defd8848SNicholas Bellinger 	if (conn->sess) {
2126defd8848SNicholas Bellinger 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2127defd8848SNicholas Bellinger 		target_wait_for_sess_cmds(conn->sess->se_sess);
2128defd8848SNicholas Bellinger 	}
2129defd8848SNicholas Bellinger 
2130defd8848SNicholas Bellinger 	while (atomic_read(&isert_conn->post_send_buf_count))
2131defd8848SNicholas Bellinger 		msleep(3000);
2132b8d26b3bSNicholas Bellinger 
2133b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2134954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2135b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2136b2cb9649SNicholas Bellinger 
21379d49f5e2SSagi Grimberg 	iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
21389d49f5e2SSagi Grimberg 
2139defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait_comp_err);
2140b8d26b3bSNicholas Bellinger }
2141b8d26b3bSNicholas Bellinger 
2142b8d26b3bSNicholas Bellinger static void
2143b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
2144b8d26b3bSNicholas Bellinger {
2145b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2146b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
2147b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2148b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2149b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2150b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2151b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
2152b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2153b8d26b3bSNicholas Bellinger 
2154b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2155b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2156b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2157b8d26b3bSNicholas Bellinger 
2158b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2159b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
2160b8d26b3bSNicholas Bellinger 		} else {
2161b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2162b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
2163c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
2164defd8848SNicholas Bellinger 
21659bb4ca68SNicholas Bellinger 			if (wc.wr_id != ISER_FASTREG_LI_WRID) {
2166ebbe4421SNicholas Bellinger 				if (tx_desc->llnode_active)
2167ebbe4421SNicholas Bellinger 					continue;
2168ebbe4421SNicholas Bellinger 
2169b8d26b3bSNicholas Bellinger 				atomic_dec(&isert_conn->post_send_buf_count);
2170defd8848SNicholas Bellinger 				isert_cq_tx_comp_err(tx_desc, isert_conn);
2171b8d26b3bSNicholas Bellinger 			}
2172b8d26b3bSNicholas Bellinger 		}
21739bb4ca68SNicholas Bellinger 	}
2174b8d26b3bSNicholas Bellinger 
2175b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2176b8d26b3bSNicholas Bellinger }
2177b8d26b3bSNicholas Bellinger 
2178b8d26b3bSNicholas Bellinger static void
2179b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
2180b8d26b3bSNicholas Bellinger {
2181b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2182b8d26b3bSNicholas Bellinger 
2183b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2184b8d26b3bSNicholas Bellinger }
2185b8d26b3bSNicholas Bellinger 
2186b8d26b3bSNicholas Bellinger static void
2187b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
2188b8d26b3bSNicholas Bellinger {
2189b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2190b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
2191b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2192b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2193b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2194b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2195b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
2196b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2197b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
2198b8d26b3bSNicholas Bellinger 
2199b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2200b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2201b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2202b8d26b3bSNicholas Bellinger 
2203b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2204b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
2205b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
2206b8d26b3bSNicholas Bellinger 		} else {
2207b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2208c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
2209b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
2210c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
2211c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
2212c5a2adbfSNicholas Bellinger 			}
2213b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
2214defd8848SNicholas Bellinger 			isert_cq_rx_comp_err(isert_conn);
2215b8d26b3bSNicholas Bellinger 		}
2216b8d26b3bSNicholas Bellinger 	}
2217b8d26b3bSNicholas Bellinger 
2218b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2219b8d26b3bSNicholas Bellinger }
2220b8d26b3bSNicholas Bellinger 
2221b8d26b3bSNicholas Bellinger static void
2222b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
2223b8d26b3bSNicholas Bellinger {
2224b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2225b8d26b3bSNicholas Bellinger 
2226b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2227b8d26b3bSNicholas Bellinger }
2228b8d26b3bSNicholas Bellinger 
2229b8d26b3bSNicholas Bellinger static int
2230b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2231b8d26b3bSNicholas Bellinger {
2232b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2233b8d26b3bSNicholas Bellinger 	int ret;
2234b8d26b3bSNicholas Bellinger 
2235b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
2236b8d26b3bSNicholas Bellinger 
2237b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2238b8d26b3bSNicholas Bellinger 			   &wr_failed);
2239b8d26b3bSNicholas Bellinger 	if (ret) {
2240b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
2241b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2242b8d26b3bSNicholas Bellinger 		return ret;
2243b8d26b3bSNicholas Bellinger 	}
2244b8d26b3bSNicholas Bellinger 	return ret;
2245b8d26b3bSNicholas Bellinger }
2246b8d26b3bSNicholas Bellinger 
2247b8d26b3bSNicholas Bellinger static int
2248b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2249b8d26b3bSNicholas Bellinger {
2250d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2251b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2252b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2253b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2254b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2255b8d26b3bSNicholas Bellinger 
2256b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2257b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2258b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2259b8d26b3bSNicholas Bellinger 	/*
2260b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2261b8d26b3bSNicholas Bellinger 	 */
2262b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2263b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2264b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2265b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2266b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2267dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2268b8d26b3bSNicholas Bellinger 
2269b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2270b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2271b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2272b8d26b3bSNicholas Bellinger 
2273b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2274b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2275dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2276b8d26b3bSNicholas Bellinger 
2277dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2278dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2279b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2280b8d26b3bSNicholas Bellinger 
2281dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2282dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2283dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
2284b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2285b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2286b8d26b3bSNicholas Bellinger 	}
2287b8d26b3bSNicholas Bellinger 
22880d0f660dSNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2289b8d26b3bSNicholas Bellinger 
2290b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2291b8d26b3bSNicholas Bellinger 
2292b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2293b8d26b3bSNicholas Bellinger }
2294b8d26b3bSNicholas Bellinger 
2295131e6abcSNicholas Bellinger static void
2296131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2297131e6abcSNicholas Bellinger {
2298131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2299131e6abcSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2300131e6abcSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2301131e6abcSNicholas Bellinger 
2302131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2303131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2304131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2305131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2306131e6abcSNicholas Bellinger 
2307131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2308131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2309131e6abcSNicholas Bellinger 
2310131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2311131e6abcSNicholas Bellinger }
2312131e6abcSNicholas Bellinger 
2313e70beee7SNicholas Bellinger static enum target_prot_op
2314e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2315e70beee7SNicholas Bellinger {
2316e70beee7SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2317e70beee7SNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2318e70beee7SNicholas Bellinger 
2319e70beee7SNicholas Bellinger 	if (device->pi_capable)
2320e70beee7SNicholas Bellinger 		return TARGET_PROT_ALL;
2321e70beee7SNicholas Bellinger 
2322e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2323e70beee7SNicholas Bellinger }
2324e70beee7SNicholas Bellinger 
2325b8d26b3bSNicholas Bellinger static int
2326b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2327b8d26b3bSNicholas Bellinger 		bool nopout_response)
2328b8d26b3bSNicholas Bellinger {
2329d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2330b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2331b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2332b8d26b3bSNicholas Bellinger 
2333b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2334b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2335b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2336b8d26b3bSNicholas Bellinger 			       nopout_response);
2337b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
233895b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2339b8d26b3bSNicholas Bellinger 
23408b513d0cSMasanari Iida 	pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2341b8d26b3bSNicholas Bellinger 
2342b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2343b8d26b3bSNicholas Bellinger }
2344b8d26b3bSNicholas Bellinger 
2345b8d26b3bSNicholas Bellinger static int
2346b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2347b8d26b3bSNicholas Bellinger {
2348d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2349b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2350b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2351b8d26b3bSNicholas Bellinger 
2352b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2353b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2354b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2355b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
235695b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2357b8d26b3bSNicholas Bellinger 
2358b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2359b8d26b3bSNicholas Bellinger 
2360b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2361b8d26b3bSNicholas Bellinger }
2362b8d26b3bSNicholas Bellinger 
2363b8d26b3bSNicholas Bellinger static int
2364b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2365b8d26b3bSNicholas Bellinger {
2366d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2367b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2368b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2369b8d26b3bSNicholas Bellinger 
2370b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2371b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2372b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2373b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
237495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2375b8d26b3bSNicholas Bellinger 
2376b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2377b8d26b3bSNicholas Bellinger 
2378b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2379b8d26b3bSNicholas Bellinger }
2380b8d26b3bSNicholas Bellinger 
2381b8d26b3bSNicholas Bellinger static int
2382b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2383b8d26b3bSNicholas Bellinger {
2384d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2385b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2386b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
23873df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
23883df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
23893df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
23903df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2391b8d26b3bSNicholas Bellinger 
2392b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
23933df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2394b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
23953df8f68aSNicholas Bellinger 
23963df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2397dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
23983df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
23993df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2400dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2401dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
24023df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
24033df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
24043df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
24053df8f68aSNicholas Bellinger 
240695b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2407b8d26b3bSNicholas Bellinger 
2408b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2409b8d26b3bSNicholas Bellinger 
2410b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2411b8d26b3bSNicholas Bellinger }
2412b8d26b3bSNicholas Bellinger 
2413b8d26b3bSNicholas Bellinger static int
2414adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2415adb54c29SNicholas Bellinger {
2416d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2417adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2418adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2419adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2420adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2421adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2422adb54c29SNicholas Bellinger 	int rc;
2423adb54c29SNicholas Bellinger 
2424adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
242522c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2426adb54c29SNicholas Bellinger 	if (rc < 0)
2427adb54c29SNicholas Bellinger 		return rc;
2428adb54c29SNicholas Bellinger 
2429adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2430adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2431adb54c29SNicholas Bellinger 
2432adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2433adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2434adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2435adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2436adb54c29SNicholas Bellinger 
2437adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2438adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2439adb54c29SNicholas Bellinger 
2440adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2441adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2442adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
2443adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2444adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2445adb54c29SNicholas Bellinger 	}
244695b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2447adb54c29SNicholas Bellinger 
2448adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2449adb54c29SNicholas Bellinger 
2450adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2451adb54c29SNicholas Bellinger }
2452adb54c29SNicholas Bellinger 
2453adb54c29SNicholas Bellinger static int
2454b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2455b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2456b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2457b8d26b3bSNicholas Bellinger {
2458d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2459b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2460b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2461b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2462b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2463b8d26b3bSNicholas Bellinger 
2464b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2465b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2466b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2467b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2468b8d26b3bSNicholas Bellinger 
2469b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2470b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2471b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2472b8d26b3bSNicholas Bellinger 	/*
2473b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2474b8d26b3bSNicholas Bellinger 	 */
2475b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2476b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2477b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
2478b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
2479b8d26b3bSNicholas Bellinger 
2480b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2481b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2482b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2483b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2484b8d26b3bSNicholas Bellinger 
248590ecc6e2SVu Pham 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
248690ecc6e2SVu Pham 			 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2487b8d26b3bSNicholas Bellinger 		page_off = 0;
2488b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2489b8d26b3bSNicholas Bellinger 		ib_sge++;
2490b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2491b8d26b3bSNicholas Bellinger 	}
2492b8d26b3bSNicholas Bellinger 
2493b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2494b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
2495b8d26b3bSNicholas Bellinger 
2496b8d26b3bSNicholas Bellinger 	return sg_nents;
2497b8d26b3bSNicholas Bellinger }
2498b8d26b3bSNicholas Bellinger 
2499b8d26b3bSNicholas Bellinger static int
250090ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
250190ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2502b8d26b3bSNicholas Bellinger {
2503b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2504d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2505b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2506e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
250790ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2508b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2509e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2510e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2511b8d26b3bSNicholas Bellinger 
251290ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2513b8d26b3bSNicholas Bellinger 
2514e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2515e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2516e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2517e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2518e3d7e4c3SSagi Grimberg 	if (ret)
2519e3d7e4c3SSagi Grimberg 		return ret;
2520b8d26b3bSNicholas Bellinger 
2521e3d7e4c3SSagi Grimberg 	data_left = data->len;
2522e3d7e4c3SSagi Grimberg 	offset = data->offset;
2523b8d26b3bSNicholas Bellinger 
2524e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2525b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
252690ecc6e2SVu Pham 		pr_warn("Unable to allocate ib_sge\n");
2527b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2528e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2529b8d26b3bSNicholas Bellinger 	}
253090ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2531b8d26b3bSNicholas Bellinger 
2532e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2533b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2534b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2535b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
253690ecc6e2SVu Pham 		pr_debug("Unable to allocate wr->send_wr\n");
2537b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2538e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2539b8d26b3bSNicholas Bellinger 	}
2540b8d26b3bSNicholas Bellinger 
2541b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2542b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2543b8d26b3bSNicholas Bellinger 
2544b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2545b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2546b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2547b8d26b3bSNicholas Bellinger 
2548b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
254990ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
255090ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2551b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2552b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
255390ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
255490ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
255590ecc6e2SVu Pham 			else
255690ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
255790ecc6e2SVu Pham 		} else {
255890ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
255990ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
256090ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
256190ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
256290ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
256390ecc6e2SVu Pham 			else
256490ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
256590ecc6e2SVu Pham 		}
2566b8d26b3bSNicholas Bellinger 
2567b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2568b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2569b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2570b8d26b3bSNicholas Bellinger 
2571b8d26b3bSNicholas Bellinger 		offset += data_len;
257290ecc6e2SVu Pham 		va_offset += data_len;
2573b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2574b8d26b3bSNicholas Bellinger 	}
257590ecc6e2SVu Pham 
257690ecc6e2SVu Pham 	return 0;
2577e3d7e4c3SSagi Grimberg unmap_cmd:
2578e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2579e3d7e4c3SSagi Grimberg 
258090ecc6e2SVu Pham 	return ret;
258190ecc6e2SVu Pham }
258290ecc6e2SVu Pham 
258390ecc6e2SVu Pham static int
258459464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
258559464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
258659464ef4SVu Pham {
258759464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
258859464ef4SVu Pham 	struct scatterlist *tmp_sg;
258959464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
259059464ef4SVu Pham 
259159464ef4SVu Pham 	n_pages = 0;
259259464ef4SVu Pham 	new_chunk = 1;
259359464ef4SVu Pham 	last_ent = sg_nents - 1;
259459464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
259559464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
259659464ef4SVu Pham 		if (new_chunk)
259759464ef4SVu Pham 			chunk_start = start_addr;
259859464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
259959464ef4SVu Pham 
260059464ef4SVu Pham 		pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
260159464ef4SVu Pham 			 i, (unsigned long long)tmp_sg->dma_address,
260259464ef4SVu Pham 			 tmp_sg->length);
260359464ef4SVu Pham 
260459464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
260559464ef4SVu Pham 			new_chunk = 0;
260659464ef4SVu Pham 			continue;
260759464ef4SVu Pham 		}
260859464ef4SVu Pham 		new_chunk = 1;
260959464ef4SVu Pham 
261059464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
261159464ef4SVu Pham 		do {
261259464ef4SVu Pham 			fr_pl[n_pages++] = page;
261359464ef4SVu Pham 			pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
261459464ef4SVu Pham 				 n_pages - 1, page);
261559464ef4SVu Pham 			page += PAGE_SIZE;
261659464ef4SVu Pham 		} while (page < end_addr);
261759464ef4SVu Pham 	}
261859464ef4SVu Pham 
261959464ef4SVu Pham 	return n_pages;
262059464ef4SVu Pham }
262159464ef4SVu Pham 
262259464ef4SVu Pham static int
2623e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2624e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2625e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
26269e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2627e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
262859464ef4SVu Pham {
262959464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
26309e961ae7SSagi Grimberg 	struct ib_mr *mr;
26319e961ae7SSagi Grimberg 	struct ib_fast_reg_page_list *frpl;
263259464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
263359464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
26349bd626e7SSagi Grimberg 	int ret, pagelist_len;
26359bd626e7SSagi Grimberg 	u32 page_off;
263659464ef4SVu Pham 	u8 key;
263759464ef4SVu Pham 
2638e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
2639e3d7e4c3SSagi Grimberg 		sge->lkey = isert_conn->conn_mr->lkey;
2640e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2641e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
26429e961ae7SSagi Grimberg 		pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
26439e961ae7SSagi Grimberg 			 __func__, __LINE__, sge->addr, sge->length,
26449e961ae7SSagi Grimberg 			 sge->lkey);
2645e3d7e4c3SSagi Grimberg 		return 0;
2646e3d7e4c3SSagi Grimberg 	}
2647e3d7e4c3SSagi Grimberg 
26489e961ae7SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID) {
26499e961ae7SSagi Grimberg 		/* Registering data buffer */
26509e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
26519e961ae7SSagi Grimberg 		frpl = fr_desc->data_frpl;
26529e961ae7SSagi Grimberg 	} else {
26539e961ae7SSagi Grimberg 		/* Registering protection buffer */
26549e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
26559e961ae7SSagi Grimberg 		frpl = fr_desc->pi_ctx->prot_frpl;
26569e961ae7SSagi Grimberg 	}
26579e961ae7SSagi Grimberg 
2658e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
265959464ef4SVu Pham 
26609bd626e7SSagi Grimberg 	pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2661e3d7e4c3SSagi Grimberg 		 fr_desc, mem->nents, mem->offset);
266259464ef4SVu Pham 
2663e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
26649e961ae7SSagi Grimberg 					     &frpl->page_list[0]);
266559464ef4SVu Pham 
2666d3e125daSSagi Grimberg 	if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
266759464ef4SVu Pham 		memset(&inv_wr, 0, sizeof(inv_wr));
26689bb4ca68SNicholas Bellinger 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
266959464ef4SVu Pham 		inv_wr.opcode = IB_WR_LOCAL_INV;
26709e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = mr->rkey;
267159464ef4SVu Pham 		wr = &inv_wr;
267259464ef4SVu Pham 		/* Bump the key */
26739e961ae7SSagi Grimberg 		key = (u8)(mr->rkey & 0x000000FF);
26749e961ae7SSagi Grimberg 		ib_update_fast_reg_key(mr, ++key);
267559464ef4SVu Pham 	}
267659464ef4SVu Pham 
267759464ef4SVu Pham 	/* Prepare FASTREG WR */
267859464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
26799bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
268059464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
26819e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
26829e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.page_list = frpl;
268359464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
268459464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2685e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
26869e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.rkey = mr->rkey;
268759464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
268859464ef4SVu Pham 
268959464ef4SVu Pham 	if (!wr)
269059464ef4SVu Pham 		wr = &fr_wr;
269159464ef4SVu Pham 	else
269259464ef4SVu Pham 		wr->next = &fr_wr;
269359464ef4SVu Pham 
269459464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
269559464ef4SVu Pham 	if (ret) {
269659464ef4SVu Pham 		pr_err("fast registration failed, ret:%d\n", ret);
269759464ef4SVu Pham 		return ret;
269859464ef4SVu Pham 	}
26999e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
270059464ef4SVu Pham 
27019e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
27029e961ae7SSagi Grimberg 	sge->addr = frpl->page_list[0] + page_off;
2703e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
270459464ef4SVu Pham 
27059e961ae7SSagi Grimberg 	pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
27069e961ae7SSagi Grimberg 		 __func__, __LINE__, sge->addr, sge->length,
27079e961ae7SSagi Grimberg 		 sge->lkey);
270859464ef4SVu Pham 
270959464ef4SVu Pham 	return ret;
271059464ef4SVu Pham }
271159464ef4SVu Pham 
27123d73cf1aSSagi Grimberg static inline void
27133d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
27143d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
27159e961ae7SSagi Grimberg {
271678eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
27173d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
27183d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
27193d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
272078eda2bbSSagi Grimberg 	/*
272178eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
272278eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
272378eda2bbSSagi Grimberg 	 * from se_cmd.
272478eda2bbSSagi Grimberg 	 */
272578eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
272678eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
272778eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
272878eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
272978eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
273078eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
27313d73cf1aSSagi Grimberg };
27323d73cf1aSSagi Grimberg 
27333d73cf1aSSagi Grimberg static int
27343d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
27353d73cf1aSSagi Grimberg {
27369e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
27379e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
27389e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
273978eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
27403d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
27419e961ae7SSagi Grimberg 		break;
27429e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
27439e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
274478eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
27453d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
27469e961ae7SSagi Grimberg 		break;
27479e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
27489e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
27493d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
27503d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
27519e961ae7SSagi Grimberg 		break;
27529e961ae7SSagi Grimberg 	default:
27539e961ae7SSagi Grimberg 		pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
27549e961ae7SSagi Grimberg 		return -EINVAL;
27559e961ae7SSagi Grimberg 	}
27569e961ae7SSagi Grimberg 
27579e961ae7SSagi Grimberg 	return 0;
27589e961ae7SSagi Grimberg }
27599e961ae7SSagi Grimberg 
27609e961ae7SSagi Grimberg static inline u8
27619e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
27629e961ae7SSagi Grimberg {
27639e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
27649e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
27659e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
27669e961ae7SSagi Grimberg }
27679e961ae7SSagi Grimberg 
27689e961ae7SSagi Grimberg static int
2769*570db170SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn,
2770*570db170SSagi Grimberg 		 struct se_cmd *se_cmd,
2771*570db170SSagi Grimberg 		 struct isert_rdma_wr *rdma_wr,
2772*570db170SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc)
27739e961ae7SSagi Grimberg {
27749e961ae7SSagi Grimberg 	struct ib_send_wr sig_wr, inv_wr;
27759e961ae7SSagi Grimberg 	struct ib_send_wr *bad_wr, *wr = NULL;
27769e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
27779e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
27789e961ae7SSagi Grimberg 	int ret;
27799e961ae7SSagi Grimberg 	u32 key;
27809e961ae7SSagi Grimberg 
27819e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
27829e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
27839e961ae7SSagi Grimberg 	if (ret)
27849e961ae7SSagi Grimberg 		goto err;
27859e961ae7SSagi Grimberg 
27869e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
27879e961ae7SSagi Grimberg 
27889e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
27899e961ae7SSagi Grimberg 		memset(&inv_wr, 0, sizeof(inv_wr));
27909e961ae7SSagi Grimberg 		inv_wr.opcode = IB_WR_LOCAL_INV;
2791c2caa207SSagi Grimberg 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
27929e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
27939e961ae7SSagi Grimberg 		wr = &inv_wr;
27949e961ae7SSagi Grimberg 		/* Bump the key */
27959e961ae7SSagi Grimberg 		key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
27969e961ae7SSagi Grimberg 		ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
27979e961ae7SSagi Grimberg 	}
27989e961ae7SSagi Grimberg 
27999e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
28009e961ae7SSagi Grimberg 	sig_wr.opcode = IB_WR_REG_SIG_MR;
2801c2caa207SSagi Grimberg 	sig_wr.wr_id = ISER_FASTREG_LI_WRID;
2802*570db170SSagi Grimberg 	sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
28039e961ae7SSagi Grimberg 	sig_wr.num_sge = 1;
28049e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
28059e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
28069e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
28079e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
2808*570db170SSagi Grimberg 		sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
28099e961ae7SSagi Grimberg 
28109e961ae7SSagi Grimberg 	if (!wr)
28119e961ae7SSagi Grimberg 		wr = &sig_wr;
28129e961ae7SSagi Grimberg 	else
28139e961ae7SSagi Grimberg 		wr->next = &sig_wr;
28149e961ae7SSagi Grimberg 
28159e961ae7SSagi Grimberg 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
28169e961ae7SSagi Grimberg 	if (ret) {
28179e961ae7SSagi Grimberg 		pr_err("fast registration failed, ret:%d\n", ret);
28189e961ae7SSagi Grimberg 		goto err;
28199e961ae7SSagi Grimberg 	}
28209e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
28219e961ae7SSagi Grimberg 
2822*570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2823*570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].addr = 0;
2824*570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
28259e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
28269e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
28279e961ae7SSagi Grimberg 		/*
28289e961ae7SSagi Grimberg 		 * We have protection guards on the wire
28299e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
28309e961ae7SSagi Grimberg 		 */
2831*570db170SSagi Grimberg 		rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
28329e961ae7SSagi Grimberg 
28339e961ae7SSagi Grimberg 	pr_debug("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
2834*570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2835*570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].lkey);
28369e961ae7SSagi Grimberg err:
28379e961ae7SSagi Grimberg 	return ret;
28389e961ae7SSagi Grimberg }
28399e961ae7SSagi Grimberg 
284059464ef4SVu Pham static int
2841*570db170SSagi Grimberg isert_handle_prot_cmd(struct isert_conn *isert_conn,
2842*570db170SSagi Grimberg 		      struct isert_cmd *isert_cmd,
2843*570db170SSagi Grimberg 		      struct isert_rdma_wr *wr)
2844*570db170SSagi Grimberg {
2845*570db170SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
2846*570db170SSagi Grimberg 	struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2847*570db170SSagi Grimberg 	int ret;
2848*570db170SSagi Grimberg 
2849*570db170SSagi Grimberg 	if (!wr->fr_desc->pi_ctx) {
2850*570db170SSagi Grimberg 		ret = isert_create_pi_ctx(wr->fr_desc,
2851*570db170SSagi Grimberg 					  device->ib_device,
2852*570db170SSagi Grimberg 					  isert_conn->conn_pd);
2853*570db170SSagi Grimberg 		if (ret) {
2854*570db170SSagi Grimberg 			pr_err("conn %p failed to allocate pi_ctx\n",
2855*570db170SSagi Grimberg 				  isert_conn);
2856*570db170SSagi Grimberg 			return ret;
2857*570db170SSagi Grimberg 		}
2858*570db170SSagi Grimberg 	}
2859*570db170SSagi Grimberg 
2860*570db170SSagi Grimberg 	if (se_cmd->t_prot_sg) {
2861*570db170SSagi Grimberg 		ret = isert_map_data_buf(isert_conn, isert_cmd,
2862*570db170SSagi Grimberg 					 se_cmd->t_prot_sg,
2863*570db170SSagi Grimberg 					 se_cmd->t_prot_nents,
2864*570db170SSagi Grimberg 					 se_cmd->prot_length,
2865*570db170SSagi Grimberg 					 0, wr->iser_ib_op, &wr->prot);
2866*570db170SSagi Grimberg 		if (ret) {
2867*570db170SSagi Grimberg 			pr_err("conn %p failed to map protection buffer\n",
2868*570db170SSagi Grimberg 				  isert_conn);
2869*570db170SSagi Grimberg 			return ret;
2870*570db170SSagi Grimberg 		}
2871*570db170SSagi Grimberg 
2872*570db170SSagi Grimberg 		memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2873*570db170SSagi Grimberg 		ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2874*570db170SSagi Grimberg 					ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2875*570db170SSagi Grimberg 		if (ret) {
2876*570db170SSagi Grimberg 			pr_err("conn %p failed to fast reg mr\n",
2877*570db170SSagi Grimberg 				  isert_conn);
2878*570db170SSagi Grimberg 			goto unmap_prot_cmd;
2879*570db170SSagi Grimberg 		}
2880*570db170SSagi Grimberg 	}
2881*570db170SSagi Grimberg 
2882*570db170SSagi Grimberg 	ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2883*570db170SSagi Grimberg 	if (ret) {
2884*570db170SSagi Grimberg 		pr_err("conn %p failed to fast reg mr\n",
2885*570db170SSagi Grimberg 			  isert_conn);
2886*570db170SSagi Grimberg 		goto unmap_prot_cmd;
2887*570db170SSagi Grimberg 	}
2888*570db170SSagi Grimberg 	wr->fr_desc->ind |= ISERT_PROTECTED;
2889*570db170SSagi Grimberg 
2890*570db170SSagi Grimberg 	return 0;
2891*570db170SSagi Grimberg 
2892*570db170SSagi Grimberg unmap_prot_cmd:
2893*570db170SSagi Grimberg 	if (se_cmd->t_prot_sg)
2894*570db170SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2895*570db170SSagi Grimberg 
2896*570db170SSagi Grimberg 	return ret;
2897*570db170SSagi Grimberg }
2898*570db170SSagi Grimberg 
2899*570db170SSagi Grimberg static int
2900a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
290159464ef4SVu Pham 	       struct isert_rdma_wr *wr)
290259464ef4SVu Pham {
290359464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
290459464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2905e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2906e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2907*570db170SSagi Grimberg 	struct ib_send_wr *send_wr;
2908*570db170SSagi Grimberg 	struct ib_sge *ib_sg;
2909e3d7e4c3SSagi Grimberg 	u32 offset;
2910e3d7e4c3SSagi Grimberg 	int ret = 0;
291159464ef4SVu Pham 	unsigned long flags;
291259464ef4SVu Pham 
291359464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2914e3d7e4c3SSagi Grimberg 
2915e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2916e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2917e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2918e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2919e3d7e4c3SSagi Grimberg 	if (ret)
2920e3d7e4c3SSagi Grimberg 		return ret;
2921e3d7e4c3SSagi Grimberg 
29229e961ae7SSagi Grimberg 	if (wr->data.dma_nents != 1 ||
29239e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_NORMAL) {
2924e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2925e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2926e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2927e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2928e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2929e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
293059464ef4SVu Pham 	}
293159464ef4SVu Pham 
29329e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2933*570db170SSagi Grimberg 				ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
2934e3d7e4c3SSagi Grimberg 	if (ret)
2935e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
293659464ef4SVu Pham 
29379e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2938*570db170SSagi Grimberg 		ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
29399e961ae7SSagi Grimberg 		if (ret)
29409e961ae7SSagi Grimberg 			goto unmap_cmd;
29419e961ae7SSagi Grimberg 
2942*570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[SIG];
2943*570db170SSagi Grimberg 	} else {
2944*570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[DATA];
29459e961ae7SSagi Grimberg 	}
29469e961ae7SSagi Grimberg 
2947*570db170SSagi Grimberg 	memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
2948e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
294959464ef4SVu Pham 	wr->send_wr_num = 1;
295059464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
295159464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
295259464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
295359464ef4SVu Pham 
295459464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2955e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
295659464ef4SVu Pham 	send_wr->num_sge = 1;
295759464ef4SVu Pham 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
295859464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
295959464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
296059464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
296159464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
29629e961ae7SSagi Grimberg 		send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
29639e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
296459464ef4SVu Pham 	} else {
296559464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
296659464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
296759464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
296859464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
296959464ef4SVu Pham 	}
297059464ef4SVu Pham 
297159464ef4SVu Pham 	return 0;
2972*570db170SSagi Grimberg 
2973e3d7e4c3SSagi Grimberg unmap_cmd:
2974e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2975e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2976e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2977e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2978e3d7e4c3SSagi Grimberg 	}
2979e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
298059464ef4SVu Pham 
298159464ef4SVu Pham 	return ret;
298259464ef4SVu Pham }
298359464ef4SVu Pham 
298459464ef4SVu Pham static int
298590ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
298690ecc6e2SVu Pham {
298790ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
298859464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
298990ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
299090ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2991d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
299290ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
299390ecc6e2SVu Pham 	int rc;
299490ecc6e2SVu Pham 
299590ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
299690ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
299790ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2998d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
299990ecc6e2SVu Pham 	if (rc) {
300090ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
300190ecc6e2SVu Pham 		return rc;
300290ecc6e2SVu Pham 	}
300390ecc6e2SVu Pham 
30049e961ae7SSagi Grimberg 	if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
3005b8d26b3bSNicholas Bellinger 		/*
3006b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
3007b8d26b3bSNicholas Bellinger 		 */
30089e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
30099e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
301004d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
3011b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
3012b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
301395b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
30140d0f660dSNicholas Bellinger 				   &isert_cmd->tx_desc.send_wr, false);
30159e961ae7SSagi Grimberg 		isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
3016897bb2c9SSagi Grimberg 		wr->send_wr_num += 1;
30179e961ae7SSagi Grimberg 	}
3018b8d26b3bSNicholas Bellinger 
3019897bb2c9SSagi Grimberg 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
3020b8d26b3bSNicholas Bellinger 
3021b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
3022b8d26b3bSNicholas Bellinger 	if (rc) {
3023b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
3024897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
3025b8d26b3bSNicholas Bellinger 	}
30269e961ae7SSagi Grimberg 
30279e961ae7SSagi Grimberg 	if (se_cmd->prot_op == TARGET_PROT_NORMAL)
30289e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
30299e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
30309e961ae7SSagi Grimberg 	else
30319e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
303290ecc6e2SVu Pham 			 isert_cmd);
3033b8d26b3bSNicholas Bellinger 
303490ecc6e2SVu Pham 	return 1;
3035b8d26b3bSNicholas Bellinger }
3036b8d26b3bSNicholas Bellinger 
3037b8d26b3bSNicholas Bellinger static int
3038b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
3039b8d26b3bSNicholas Bellinger {
3040b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
3041d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3042b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
3043b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3044d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
304590ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
304690ecc6e2SVu Pham 	int rc;
3047b8d26b3bSNicholas Bellinger 
304890ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
304990ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
3050b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
3051d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
305290ecc6e2SVu Pham 	if (rc) {
305390ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
305490ecc6e2SVu Pham 		return rc;
3055b8d26b3bSNicholas Bellinger 	}
3056b8d26b3bSNicholas Bellinger 
3057b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
3058b8d26b3bSNicholas Bellinger 
3059b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
3060b8d26b3bSNicholas Bellinger 	if (rc) {
3061b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
3062b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
3063b8d26b3bSNicholas Bellinger 	}
306490ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
306590ecc6e2SVu Pham 		 isert_cmd);
3066b8d26b3bSNicholas Bellinger 
306790ecc6e2SVu Pham 	return 0;
3068b8d26b3bSNicholas Bellinger }
3069b8d26b3bSNicholas Bellinger 
3070b8d26b3bSNicholas Bellinger static int
3071b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3072b8d26b3bSNicholas Bellinger {
3073b8d26b3bSNicholas Bellinger 	int ret;
3074b8d26b3bSNicholas Bellinger 
3075b8d26b3bSNicholas Bellinger 	switch (state) {
3076b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3077b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
3078b8d26b3bSNicholas Bellinger 		break;
3079b8d26b3bSNicholas Bellinger 	default:
3080b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
3081b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
3082b8d26b3bSNicholas Bellinger 		break;
3083b8d26b3bSNicholas Bellinger 	}
3084b8d26b3bSNicholas Bellinger 
3085b8d26b3bSNicholas Bellinger 	return ret;
3086b8d26b3bSNicholas Bellinger }
3087b8d26b3bSNicholas Bellinger 
3088b8d26b3bSNicholas Bellinger static int
3089b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3090b8d26b3bSNicholas Bellinger {
3091b8d26b3bSNicholas Bellinger 	int ret;
3092b8d26b3bSNicholas Bellinger 
3093b8d26b3bSNicholas Bellinger 	switch (state) {
3094b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
3095b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
3096b8d26b3bSNicholas Bellinger 		if (!ret) {
3097b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
3098b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
3099b8d26b3bSNicholas Bellinger 		}
3100b8d26b3bSNicholas Bellinger 		break;
3101b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
3102b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
3103b8d26b3bSNicholas Bellinger 		break;
3104b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
3105b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
3106b8d26b3bSNicholas Bellinger 		break;
3107b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
3108b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
3109b8d26b3bSNicholas Bellinger 		break;
3110adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
3111adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
3112adb54c29SNicholas Bellinger 		break;
3113b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
3114b8d26b3bSNicholas Bellinger 		/*
3115b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
3116b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
3117b8d26b3bSNicholas Bellinger 		 */
3118b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
3119b8d26b3bSNicholas Bellinger 		break;
3120b8d26b3bSNicholas Bellinger 	default:
3121b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
3122b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
3123b8d26b3bSNicholas Bellinger 		break;
3124b8d26b3bSNicholas Bellinger 	}
3125b8d26b3bSNicholas Bellinger 
3126b8d26b3bSNicholas Bellinger 	return ret;
3127b8d26b3bSNicholas Bellinger }
3128b8d26b3bSNicholas Bellinger 
3129ca6c1d82SSagi Grimberg struct rdma_cm_id *
3130ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
3131ca6c1d82SSagi Grimberg {
3132ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
3133ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
3134ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
3135ca6c1d82SSagi Grimberg 	int ret;
3136ca6c1d82SSagi Grimberg 
3137ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
3138ca6c1d82SSagi Grimberg 	pr_debug("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
3139ca6c1d82SSagi Grimberg 
3140ca6c1d82SSagi Grimberg 	id = rdma_create_id(isert_cma_handler, isert_np,
3141ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
3142ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
3143ca6c1d82SSagi Grimberg 		pr_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
3144ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
3145ca6c1d82SSagi Grimberg 		goto out;
3146ca6c1d82SSagi Grimberg 	}
3147ca6c1d82SSagi Grimberg 	pr_debug("id %p context %p\n", id, id->context);
3148ca6c1d82SSagi Grimberg 
3149ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
3150ca6c1d82SSagi Grimberg 	if (ret) {
3151ca6c1d82SSagi Grimberg 		pr_err("rdma_bind_addr() failed: %d\n", ret);
3152ca6c1d82SSagi Grimberg 		goto out_id;
3153ca6c1d82SSagi Grimberg 	}
3154ca6c1d82SSagi Grimberg 
3155ca6c1d82SSagi Grimberg 	ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
3156ca6c1d82SSagi Grimberg 	if (ret) {
3157ca6c1d82SSagi Grimberg 		pr_err("rdma_listen() failed: %d\n", ret);
3158ca6c1d82SSagi Grimberg 		goto out_id;
3159ca6c1d82SSagi Grimberg 	}
3160ca6c1d82SSagi Grimberg 
3161ca6c1d82SSagi Grimberg 	return id;
3162ca6c1d82SSagi Grimberg out_id:
3163ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
3164ca6c1d82SSagi Grimberg out:
3165ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
3166ca6c1d82SSagi Grimberg }
3167ca6c1d82SSagi Grimberg 
3168b8d26b3bSNicholas Bellinger static int
3169b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
3170b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
3171b8d26b3bSNicholas Bellinger {
3172b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
3173b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
3174b8d26b3bSNicholas Bellinger 	int ret;
3175b8d26b3bSNicholas Bellinger 
3176b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3177b8d26b3bSNicholas Bellinger 	if (!isert_np) {
3178b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
3179b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3180b8d26b3bSNicholas Bellinger 	}
3181531b7bf4SSagi Grimberg 	sema_init(&isert_np->np_sem, 0);
3182b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
3183b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
3184b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
3185ca6c1d82SSagi Grimberg 	isert_np->np = np;
3186b8d26b3bSNicholas Bellinger 
3187b8d26b3bSNicholas Bellinger 	/*
3188b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3189b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3190b8d26b3bSNicholas Bellinger 	 */
3191b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
3192b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
3193b8d26b3bSNicholas Bellinger 
3194ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
3195b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3196b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3197b8d26b3bSNicholas Bellinger 		goto out;
3198b8d26b3bSNicholas Bellinger 	}
3199b8d26b3bSNicholas Bellinger 
3200b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
3201b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3202b8d26b3bSNicholas Bellinger 
3203b8d26b3bSNicholas Bellinger 	return 0;
3204b8d26b3bSNicholas Bellinger 
3205b8d26b3bSNicholas Bellinger out:
3206b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3207ca6c1d82SSagi Grimberg 
3208b8d26b3bSNicholas Bellinger 	return ret;
3209b8d26b3bSNicholas Bellinger }
3210b8d26b3bSNicholas Bellinger 
3211b8d26b3bSNicholas Bellinger static int
3212b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3213b8d26b3bSNicholas Bellinger {
3214b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3215b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3216b8d26b3bSNicholas Bellinger 	int ret;
3217b8d26b3bSNicholas Bellinger 
3218b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3219b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3220b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3221b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3222b8d26b3bSNicholas Bellinger 
3223b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3224b8d26b3bSNicholas Bellinger 
3225b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3226b8d26b3bSNicholas Bellinger 	if (ret) {
3227b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
3228b8d26b3bSNicholas Bellinger 		return ret;
3229b8d26b3bSNicholas Bellinger 	}
3230b8d26b3bSNicholas Bellinger 
3231b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3232b8d26b3bSNicholas Bellinger 
3233b8d26b3bSNicholas Bellinger 	return 0;
3234b8d26b3bSNicholas Bellinger }
3235b8d26b3bSNicholas Bellinger 
3236b8d26b3bSNicholas Bellinger static int
3237b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3238b8d26b3bSNicholas Bellinger {
3239b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3240b8d26b3bSNicholas Bellinger 	int ret;
3241b8d26b3bSNicholas Bellinger 
32422371e5daSSagi Grimberg 	pr_info("before login_req comp conn: %p\n", isert_conn);
32432371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
32442371e5daSSagi Grimberg 	if (ret) {
32452371e5daSSagi Grimberg 		pr_err("isert_conn %p interrupted before got login req\n",
32462371e5daSSagi Grimberg 			  isert_conn);
32472371e5daSSagi Grimberg 		return ret;
32482371e5daSSagi Grimberg 	}
32492371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
32502371e5daSSagi Grimberg 
32516faaa85fSNicholas Bellinger 	/*
32526faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
32536faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
32546faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
32556faaa85fSNicholas Bellinger 	 * into a NOP.
32566faaa85fSNicholas Bellinger 	 */
32576faaa85fSNicholas Bellinger 	if (!login->first_request)
32586faaa85fSNicholas Bellinger 		return 0;
3259b8d26b3bSNicholas Bellinger 
32602371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
32612371e5daSSagi Grimberg 
32622371e5daSSagi Grimberg 	pr_info("before conn_login_comp conn: %p\n", conn);
3263b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3264b8d26b3bSNicholas Bellinger 	if (ret)
3265b8d26b3bSNicholas Bellinger 		return ret;
3266b8d26b3bSNicholas Bellinger 
32672371e5daSSagi Grimberg 	pr_info("processing login->req: %p\n", login->req);
32682371e5daSSagi Grimberg 
3269b8d26b3bSNicholas Bellinger 	return 0;
3270b8d26b3bSNicholas Bellinger }
3271b8d26b3bSNicholas Bellinger 
3272b8d26b3bSNicholas Bellinger static void
3273b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3274b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3275b8d26b3bSNicholas Bellinger {
3276b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3277b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3278b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
3279b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
3280b8d26b3bSNicholas Bellinger 
3281b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3282b8d26b3bSNicholas Bellinger 
3283b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
3284b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3285b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3286b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3287b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
3288b8d26b3bSNicholas Bellinger 
3289b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3290b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3291b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3292b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
3293b8d26b3bSNicholas Bellinger 	} else {
3294b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3295b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
3296b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3297b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
3298b8d26b3bSNicholas Bellinger 
3299b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3300b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
3301b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3302b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
3303b8d26b3bSNicholas Bellinger 	}
3304b8d26b3bSNicholas Bellinger }
3305b8d26b3bSNicholas Bellinger 
3306b8d26b3bSNicholas Bellinger static int
3307b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3308b8d26b3bSNicholas Bellinger {
3309b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3310b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3311b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
3312b8d26b3bSNicholas Bellinger 
3313b8d26b3bSNicholas Bellinger accept_wait:
3314531b7bf4SSagi Grimberg 	ret = down_interruptible(&isert_np->np_sem);
33151acff63fSSagi Grimberg 	if (ret || max_accept > 5)
3316b8d26b3bSNicholas Bellinger 		return -ENODEV;
3317b8d26b3bSNicholas Bellinger 
3318b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3319e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3320b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
3321e346ab34SSagi Grimberg 		pr_debug("np_thread_state %d for isert_accept_np\n",
3322e346ab34SSagi Grimberg 			 np->np_thread_state);
3323e346ab34SSagi Grimberg 		/**
3324e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
3325e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
3326e346ab34SSagi Grimberg 		 **/
3327b8d26b3bSNicholas Bellinger 		return -ENODEV;
3328b8d26b3bSNicholas Bellinger 	}
3329b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3330b8d26b3bSNicholas Bellinger 
3331b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3332b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
3333b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
3334b8d26b3bSNicholas Bellinger 		max_accept++;
3335b8d26b3bSNicholas Bellinger 		goto accept_wait;
3336b8d26b3bSNicholas Bellinger 	}
3337b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
3338b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
3339b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
3340b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3341b8d26b3bSNicholas Bellinger 
3342b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3343b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3344b8d26b3bSNicholas Bellinger 	max_accept = 0;
3345b8d26b3bSNicholas Bellinger 
3346b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3347b8d26b3bSNicholas Bellinger 
33482371e5daSSagi Grimberg 	pr_debug("Processing isert_conn: %p\n", isert_conn);
33492371e5daSSagi Grimberg 
3350b8d26b3bSNicholas Bellinger 	return 0;
3351b8d26b3bSNicholas Bellinger }
3352b8d26b3bSNicholas Bellinger 
3353b8d26b3bSNicholas Bellinger static void
3354b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3355b8d26b3bSNicholas Bellinger {
3356b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3357b8d26b3bSNicholas Bellinger 
33583b726ae2SSagi Grimberg 	if (isert_np->np_cm_id)
3359b8d26b3bSNicholas Bellinger 		rdma_destroy_id(isert_np->np_cm_id);
3360b8d26b3bSNicholas Bellinger 
3361b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3362b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3363b8d26b3bSNicholas Bellinger }
3364b8d26b3bSNicholas Bellinger 
3365b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
3366b02efbfcSSagi Grimberg {
3367b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
3368b02efbfcSSagi Grimberg 						     struct isert_conn,
3369b02efbfcSSagi Grimberg 						     release_work);
3370b02efbfcSSagi Grimberg 
3371b02efbfcSSagi Grimberg 	pr_info("Starting release conn %p\n", isert_conn);
3372b02efbfcSSagi Grimberg 
3373b02efbfcSSagi Grimberg 	wait_for_completion(&isert_conn->conn_wait);
3374b02efbfcSSagi Grimberg 
3375b02efbfcSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3376b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
3377b02efbfcSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
3378b02efbfcSSagi Grimberg 
3379b02efbfcSSagi Grimberg 	pr_info("Destroying conn %p\n", isert_conn);
3380b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
3381b02efbfcSSagi Grimberg }
3382b02efbfcSSagi Grimberg 
3383defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3384b8d26b3bSNicholas Bellinger {
3385b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3386b8d26b3bSNicholas Bellinger 
3387defd8848SNicholas Bellinger 	pr_debug("isert_wait_conn: Starting \n");
3388b8d26b3bSNicholas Bellinger 
33899d49f5e2SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3390b8d26b3bSNicholas Bellinger 	/*
3391b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
3392b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3393b8d26b3bSNicholas Bellinger 	 */
3394b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3395b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
3396b2cb9649SNicholas Bellinger 		return;
3397b2cb9649SNicholas Bellinger 	}
3398954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
3399b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
3400b2cb9649SNicholas Bellinger 
3401defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait_comp_err);
3402954f2372SSagi Grimberg 
3403b02efbfcSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
3404b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
3405defd8848SNicholas Bellinger }
3406defd8848SNicholas Bellinger 
3407defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3408defd8848SNicholas Bellinger {
3409defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3410b8d26b3bSNicholas Bellinger 
3411b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3412b8d26b3bSNicholas Bellinger }
3413b8d26b3bSNicholas Bellinger 
3414b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3415b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3416b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3417d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3418b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3419b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3420b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3421b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3422defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3423b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3424b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3425b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3426b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3427b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3428b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3429b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3430b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3431131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3432e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3433b8d26b3bSNicholas Bellinger };
3434b8d26b3bSNicholas Bellinger 
3435b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3436b8d26b3bSNicholas Bellinger {
3437b8d26b3bSNicholas Bellinger 	int ret;
3438b8d26b3bSNicholas Bellinger 
3439b8d26b3bSNicholas Bellinger 	isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3440b8d26b3bSNicholas Bellinger 	if (!isert_rx_wq) {
3441b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_rx_wq\n");
3442b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3443b8d26b3bSNicholas Bellinger 	}
3444b8d26b3bSNicholas Bellinger 
3445b8d26b3bSNicholas Bellinger 	isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3446b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
3447b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_comp_wq\n");
3448b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
3449b8d26b3bSNicholas Bellinger 		goto destroy_rx_wq;
3450b8d26b3bSNicholas Bellinger 	}
3451b8d26b3bSNicholas Bellinger 
3452b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3453b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
3454b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
3455b02efbfcSSagi Grimberg 		pr_err("Unable to allocate isert_release_wq\n");
3456b02efbfcSSagi Grimberg 		ret = -ENOMEM;
3457b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
3458b02efbfcSSagi Grimberg 	}
3459b02efbfcSSagi Grimberg 
3460b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
3461b02efbfcSSagi Grimberg 	pr_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3462b02efbfcSSagi Grimberg 
3463b8d26b3bSNicholas Bellinger 	return 0;
3464b8d26b3bSNicholas Bellinger 
3465b02efbfcSSagi Grimberg destroy_comp_wq:
3466b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
3467b8d26b3bSNicholas Bellinger destroy_rx_wq:
3468b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3469b8d26b3bSNicholas Bellinger 	return ret;
3470b8d26b3bSNicholas Bellinger }
3471b8d26b3bSNicholas Bellinger 
3472b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3473b8d26b3bSNicholas Bellinger {
3474f5ebec96SSagi Grimberg 	flush_scheduled_work();
3475b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
3476b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3477b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3478b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
3479b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3480b8d26b3bSNicholas Bellinger }
3481b8d26b3bSNicholas Bellinger 
3482b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3483b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
3484b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3485b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3486b8d26b3bSNicholas Bellinger 
3487b8d26b3bSNicholas Bellinger module_init(isert_init);
3488b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3489