xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision c2f88b17a1d97ca4ecd96cc22333a7a4f1407d39)
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;
44b8d26b3bSNicholas Bellinger 
45b8d26b3bSNicholas Bellinger static void
46d40945d8SVu Pham isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
47d40945d8SVu Pham static int
48d40945d8SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
49d40945d8SVu Pham 	       struct isert_rdma_wr *wr);
5059464ef4SVu Pham static void
51a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
5259464ef4SVu Pham static int
53a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
5459464ef4SVu Pham 	       struct isert_rdma_wr *wr);
55f93f3a70SSagi Grimberg static int
56f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
57d40945d8SVu Pham 
58d40945d8SVu Pham static void
59b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
60b8d26b3bSNicholas Bellinger {
61b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)context;
62b8d26b3bSNicholas Bellinger 
63b8d26b3bSNicholas Bellinger 	pr_err("isert_qp_event_callback event: %d\n", e->event);
64b8d26b3bSNicholas Bellinger 	switch (e->event) {
65b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
66b8d26b3bSNicholas Bellinger 		rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
67b8d26b3bSNicholas Bellinger 		break;
68b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
69b8d26b3bSNicholas Bellinger 		pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
70b8d26b3bSNicholas Bellinger 		break;
71b8d26b3bSNicholas Bellinger 	default:
72b8d26b3bSNicholas Bellinger 		break;
73b8d26b3bSNicholas Bellinger 	}
74b8d26b3bSNicholas Bellinger }
75b8d26b3bSNicholas Bellinger 
76b8d26b3bSNicholas Bellinger static int
77b8d26b3bSNicholas Bellinger isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
78b8d26b3bSNicholas Bellinger {
79b8d26b3bSNicholas Bellinger 	int ret;
80b8d26b3bSNicholas Bellinger 
81b8d26b3bSNicholas Bellinger 	ret = ib_query_device(ib_dev, devattr);
82b8d26b3bSNicholas Bellinger 	if (ret) {
83b8d26b3bSNicholas Bellinger 		pr_err("ib_query_device() failed: %d\n", ret);
84b8d26b3bSNicholas Bellinger 		return ret;
85b8d26b3bSNicholas Bellinger 	}
86b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
87b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
88b8d26b3bSNicholas Bellinger 
89b8d26b3bSNicholas Bellinger 	return 0;
90b8d26b3bSNicholas Bellinger }
91b8d26b3bSNicholas Bellinger 
92b8d26b3bSNicholas Bellinger static int
93d3e125daSSagi Grimberg isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id,
94d3e125daSSagi Grimberg 		    u8 protection)
95b8d26b3bSNicholas Bellinger {
96b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
97b8d26b3bSNicholas Bellinger 	struct ib_qp_init_attr attr;
98b8d26b3bSNicholas Bellinger 	int ret, index, min_index = 0;
99b8d26b3bSNicholas Bellinger 
100b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
101b8d26b3bSNicholas Bellinger 	for (index = 0; index < device->cqs_used; index++)
102b8d26b3bSNicholas Bellinger 		if (device->cq_active_qps[index] <
103b8d26b3bSNicholas Bellinger 		    device->cq_active_qps[min_index])
104b8d26b3bSNicholas Bellinger 			min_index = index;
105b8d26b3bSNicholas Bellinger 	device->cq_active_qps[min_index]++;
106b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
107b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
108b8d26b3bSNicholas Bellinger 
109b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
110b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
111b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
112b8d26b3bSNicholas Bellinger 	attr.send_cq = device->dev_tx_cq[min_index];
113b8d26b3bSNicholas Bellinger 	attr.recv_cq = device->dev_rx_cq[min_index];
114b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
115b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
116b8d26b3bSNicholas Bellinger 	/*
117b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
118b8d26b3bSNicholas Bellinger 	 * work-around for RDMA_READ..
119b8d26b3bSNicholas Bellinger 	 */
12059464ef4SVu Pham 	attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
121b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
122b8d26b3bSNicholas Bellinger 
123b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
124b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
125b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
126d3e125daSSagi Grimberg 	if (protection)
127d3e125daSSagi Grimberg 		attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
128b8d26b3bSNicholas Bellinger 
129b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
130b8d26b3bSNicholas Bellinger 		 cma_id->device);
131b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
132b8d26b3bSNicholas Bellinger 		 isert_conn->conn_pd->device);
133b8d26b3bSNicholas Bellinger 
134b8d26b3bSNicholas Bellinger 	ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
135b8d26b3bSNicholas Bellinger 	if (ret) {
136b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_qp failed for cma_id %d\n", ret);
137b8d26b3bSNicholas Bellinger 		return ret;
138b8d26b3bSNicholas Bellinger 	}
139b8d26b3bSNicholas Bellinger 	isert_conn->conn_qp = cma_id->qp;
140b8d26b3bSNicholas Bellinger 	pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
141b8d26b3bSNicholas Bellinger 
142b8d26b3bSNicholas Bellinger 	return 0;
143b8d26b3bSNicholas Bellinger }
144b8d26b3bSNicholas Bellinger 
145b8d26b3bSNicholas Bellinger static void
146b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
147b8d26b3bSNicholas Bellinger {
148b8d26b3bSNicholas Bellinger 	pr_debug("isert_cq_event_callback event: %d\n", e->event);
149b8d26b3bSNicholas Bellinger }
150b8d26b3bSNicholas Bellinger 
151b8d26b3bSNicholas Bellinger static int
152b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
153b8d26b3bSNicholas Bellinger {
154b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
155b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
156b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
157b8d26b3bSNicholas Bellinger 	u64 dma_addr;
158b8d26b3bSNicholas Bellinger 	int i, j;
159b8d26b3bSNicholas Bellinger 
160b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
161b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
162b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
163b8d26b3bSNicholas Bellinger 		goto fail;
164b8d26b3bSNicholas Bellinger 
165b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
166b8d26b3bSNicholas Bellinger 
167b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
168b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
169b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
170b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
171b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
172b8d26b3bSNicholas Bellinger 
173b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
174b8d26b3bSNicholas Bellinger 
175b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
176b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
177b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
178b8d26b3bSNicholas Bellinger 		rx_sg->lkey = isert_conn->conn_mr->lkey;
179b8d26b3bSNicholas Bellinger 	}
180b8d26b3bSNicholas Bellinger 
181b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
182b8d26b3bSNicholas Bellinger 	return 0;
183b8d26b3bSNicholas Bellinger 
184b8d26b3bSNicholas Bellinger dma_map_fail:
185b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
186b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
187b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
188b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
189b8d26b3bSNicholas Bellinger 	}
190b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
191b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
192b8d26b3bSNicholas Bellinger fail:
193b8d26b3bSNicholas Bellinger 	return -ENOMEM;
194b8d26b3bSNicholas Bellinger }
195b8d26b3bSNicholas Bellinger 
196b8d26b3bSNicholas Bellinger static void
197b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
198b8d26b3bSNicholas Bellinger {
199b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
200b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
201b8d26b3bSNicholas Bellinger 	int i;
202b8d26b3bSNicholas Bellinger 
203b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
204b8d26b3bSNicholas Bellinger 		return;
205b8d26b3bSNicholas Bellinger 
206b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
207b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
208b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
209b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
210b8d26b3bSNicholas Bellinger 	}
211b8d26b3bSNicholas Bellinger 
212b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
213b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
214b8d26b3bSNicholas Bellinger }
215b8d26b3bSNicholas Bellinger 
2162853c2b6SNicholas Bellinger static void isert_cq_tx_work(struct work_struct *);
217b8d26b3bSNicholas Bellinger static void isert_cq_tx_callback(struct ib_cq *, void *);
2182853c2b6SNicholas Bellinger static void isert_cq_rx_work(struct work_struct *);
219b8d26b3bSNicholas Bellinger static void isert_cq_rx_callback(struct ib_cq *, void *);
220b8d26b3bSNicholas Bellinger 
221b8d26b3bSNicholas Bellinger static int
222b8d26b3bSNicholas Bellinger isert_create_device_ib_res(struct isert_device *device)
223b8d26b3bSNicholas Bellinger {
224b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = device->ib_device;
225b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
22659464ef4SVu Pham 	struct ib_device_attr *dev_attr;
227b8d26b3bSNicholas Bellinger 	int ret = 0, i, j;
228b8d26b3bSNicholas Bellinger 
22959464ef4SVu Pham 	dev_attr = &device->dev_attr;
23059464ef4SVu Pham 	ret = isert_query_device(ib_dev, dev_attr);
23159464ef4SVu Pham 	if (ret)
23259464ef4SVu Pham 		return ret;
23359464ef4SVu Pham 
234d40945d8SVu Pham 	/* asign function handlers */
235f2252258SSagi Grimberg 	if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
236f2252258SSagi Grimberg 	    dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
237a3a5a826SSagi Grimberg 		device->use_fastreg = 1;
238a3a5a826SSagi Grimberg 		device->reg_rdma_mem = isert_reg_rdma;
239a3a5a826SSagi Grimberg 		device->unreg_rdma_mem = isert_unreg_rdma;
24059464ef4SVu Pham 	} else {
241a3a5a826SSagi Grimberg 		device->use_fastreg = 0;
242d40945d8SVu Pham 		device->reg_rdma_mem = isert_map_rdma;
243d40945d8SVu Pham 		device->unreg_rdma_mem = isert_unmap_cmd;
24459464ef4SVu Pham 	}
245d40945d8SVu Pham 
246d3e125daSSagi Grimberg 	/* Check signature cap */
247d3e125daSSagi Grimberg 	device->pi_capable = dev_attr->device_cap_flags &
248d3e125daSSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
249d3e125daSSagi Grimberg 
250b8d26b3bSNicholas Bellinger 	device->cqs_used = min_t(int, num_online_cpus(),
251b8d26b3bSNicholas Bellinger 				 device->ib_device->num_comp_vectors);
252b8d26b3bSNicholas Bellinger 	device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
253a3a5a826SSagi Grimberg 	pr_debug("Using %d CQs, device %s supports %d vectors support "
254d3e125daSSagi Grimberg 		 "Fast registration %d pi_capable %d\n",
255b8d26b3bSNicholas Bellinger 		 device->cqs_used, device->ib_device->name,
256d3e125daSSagi Grimberg 		 device->ib_device->num_comp_vectors, device->use_fastreg,
257d3e125daSSagi Grimberg 		 device->pi_capable);
258b8d26b3bSNicholas Bellinger 	device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
259b8d26b3bSNicholas Bellinger 				device->cqs_used, GFP_KERNEL);
260b8d26b3bSNicholas Bellinger 	if (!device->cq_desc) {
261b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate device->cq_desc\n");
262b8d26b3bSNicholas Bellinger 		return -ENOMEM;
263b8d26b3bSNicholas Bellinger 	}
264b8d26b3bSNicholas Bellinger 	cq_desc = device->cq_desc;
265b8d26b3bSNicholas Bellinger 
266b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
267b8d26b3bSNicholas Bellinger 		cq_desc[i].device = device;
268b8d26b3bSNicholas Bellinger 		cq_desc[i].cq_index = i;
269b8d26b3bSNicholas Bellinger 
2702853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
271b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
272b8d26b3bSNicholas Bellinger 						isert_cq_rx_callback,
273b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
274b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
275b8d26b3bSNicholas Bellinger 						ISER_MAX_RX_CQ_LEN, i);
27694a71110SWei Yongjun 		if (IS_ERR(device->dev_rx_cq[i])) {
27794a71110SWei Yongjun 			ret = PTR_ERR(device->dev_rx_cq[i]);
27894a71110SWei Yongjun 			device->dev_rx_cq[i] = NULL;
279b8d26b3bSNicholas Bellinger 			goto out_cq;
28094a71110SWei Yongjun 		}
281b8d26b3bSNicholas Bellinger 
2822853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
283b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
284b8d26b3bSNicholas Bellinger 						isert_cq_tx_callback,
285b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
286b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
287b8d26b3bSNicholas Bellinger 						ISER_MAX_TX_CQ_LEN, i);
28894a71110SWei Yongjun 		if (IS_ERR(device->dev_tx_cq[i])) {
28994a71110SWei Yongjun 			ret = PTR_ERR(device->dev_tx_cq[i]);
29094a71110SWei Yongjun 			device->dev_tx_cq[i] = NULL;
29194a71110SWei Yongjun 			goto out_cq;
29294a71110SWei Yongjun 		}
29394a71110SWei Yongjun 
29494a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
29594a71110SWei Yongjun 		if (ret)
296b8d26b3bSNicholas Bellinger 			goto out_cq;
297b8d26b3bSNicholas Bellinger 
29894a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
29994a71110SWei Yongjun 		if (ret)
300b8d26b3bSNicholas Bellinger 			goto out_cq;
301b8d26b3bSNicholas Bellinger 	}
302b8d26b3bSNicholas Bellinger 
303b8d26b3bSNicholas Bellinger 	return 0;
304b8d26b3bSNicholas Bellinger 
305b8d26b3bSNicholas Bellinger out_cq:
306b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++) {
307b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[j];
308b8d26b3bSNicholas Bellinger 
309b8d26b3bSNicholas Bellinger 		if (device->dev_rx_cq[j]) {
310b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_rx_work);
311b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_rx_cq[j]);
312b8d26b3bSNicholas Bellinger 		}
313b8d26b3bSNicholas Bellinger 		if (device->dev_tx_cq[j]) {
314b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_tx_work);
315b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_tx_cq[j]);
316b8d26b3bSNicholas Bellinger 		}
317b8d26b3bSNicholas Bellinger 	}
318b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
319b8d26b3bSNicholas Bellinger 
320b8d26b3bSNicholas Bellinger 	return ret;
321b8d26b3bSNicholas Bellinger }
322b8d26b3bSNicholas Bellinger 
323b8d26b3bSNicholas Bellinger static void
324b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
325b8d26b3bSNicholas Bellinger {
326b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
327b8d26b3bSNicholas Bellinger 	int i;
328b8d26b3bSNicholas Bellinger 
329b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
330b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[i];
331b8d26b3bSNicholas Bellinger 
332b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_rx_work);
333b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_tx_work);
334b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_rx_cq[i]);
335b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_tx_cq[i]);
336b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = NULL;
337b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = NULL;
338b8d26b3bSNicholas Bellinger 	}
339b8d26b3bSNicholas Bellinger 
340b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
341b8d26b3bSNicholas Bellinger }
342b8d26b3bSNicholas Bellinger 
343b8d26b3bSNicholas Bellinger static void
344b8d26b3bSNicholas Bellinger isert_device_try_release(struct isert_device *device)
345b8d26b3bSNicholas Bellinger {
346b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
347b8d26b3bSNicholas Bellinger 	device->refcount--;
348b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
349b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
350b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
351b8d26b3bSNicholas Bellinger 		kfree(device);
352b8d26b3bSNicholas Bellinger 	}
353b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
354b8d26b3bSNicholas Bellinger }
355b8d26b3bSNicholas Bellinger 
356b8d26b3bSNicholas Bellinger static struct isert_device *
357b8d26b3bSNicholas Bellinger isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
358b8d26b3bSNicholas Bellinger {
359b8d26b3bSNicholas Bellinger 	struct isert_device *device;
360b8d26b3bSNicholas Bellinger 	int ret;
361b8d26b3bSNicholas Bellinger 
362b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
363b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
364b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
365b8d26b3bSNicholas Bellinger 			device->refcount++;
366b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
367b8d26b3bSNicholas Bellinger 			return device;
368b8d26b3bSNicholas Bellinger 		}
369b8d26b3bSNicholas Bellinger 	}
370b8d26b3bSNicholas Bellinger 
371b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
372b8d26b3bSNicholas Bellinger 	if (!device) {
373b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
374b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
375b8d26b3bSNicholas Bellinger 	}
376b8d26b3bSNicholas Bellinger 
377b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
378b8d26b3bSNicholas Bellinger 
379b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
380b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
381b8d26b3bSNicholas Bellinger 	if (ret) {
382b8d26b3bSNicholas Bellinger 		kfree(device);
383b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
384b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
385b8d26b3bSNicholas Bellinger 	}
386b8d26b3bSNicholas Bellinger 
387b8d26b3bSNicholas Bellinger 	device->refcount++;
388b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
389b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
390b8d26b3bSNicholas Bellinger 
391b8d26b3bSNicholas Bellinger 	return device;
392b8d26b3bSNicholas Bellinger }
393b8d26b3bSNicholas Bellinger 
39459464ef4SVu Pham static void
395a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
39659464ef4SVu Pham {
39759464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
39859464ef4SVu Pham 	int i = 0;
39959464ef4SVu Pham 
400a3a5a826SSagi Grimberg 	if (list_empty(&isert_conn->conn_fr_pool))
40159464ef4SVu Pham 		return;
40259464ef4SVu Pham 
403a3a5a826SSagi Grimberg 	pr_debug("Freeing conn %p fastreg pool", isert_conn);
40459464ef4SVu Pham 
40559464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
406a3a5a826SSagi Grimberg 				 &isert_conn->conn_fr_pool, list) {
40759464ef4SVu Pham 		list_del(&fr_desc->list);
40859464ef4SVu Pham 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
40959464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
410d3e125daSSagi Grimberg 		if (fr_desc->pi_ctx) {
411d3e125daSSagi Grimberg 			ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
412d3e125daSSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
413d3e125daSSagi Grimberg 			ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
414d3e125daSSagi Grimberg 			kfree(fr_desc->pi_ctx);
415d3e125daSSagi Grimberg 		}
41659464ef4SVu Pham 		kfree(fr_desc);
41759464ef4SVu Pham 		++i;
41859464ef4SVu Pham 	}
41959464ef4SVu Pham 
420a3a5a826SSagi Grimberg 	if (i < isert_conn->conn_fr_pool_size)
42159464ef4SVu Pham 		pr_warn("Pool still has %d regions registered\n",
422a3a5a826SSagi Grimberg 			isert_conn->conn_fr_pool_size - i);
42359464ef4SVu Pham }
42459464ef4SVu Pham 
42559464ef4SVu Pham static int
426dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
427d3e125daSSagi Grimberg 		     struct fast_reg_descriptor *fr_desc, u8 protection)
428dc87a90fSSagi Grimberg {
429d3e125daSSagi Grimberg 	int ret;
430d3e125daSSagi Grimberg 
431dc87a90fSSagi Grimberg 	fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
432dc87a90fSSagi Grimberg 							 ISCSI_ISER_SG_TABLESIZE);
433dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_frpl)) {
434dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frpl err=%ld\n",
435dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_frpl));
436dc87a90fSSagi Grimberg 		return PTR_ERR(fr_desc->data_frpl);
437dc87a90fSSagi Grimberg 	}
438dc87a90fSSagi Grimberg 
439dc87a90fSSagi Grimberg 	fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
440dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
441dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frmr err=%ld\n",
442dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_mr));
443d3e125daSSagi Grimberg 		ret = PTR_ERR(fr_desc->data_mr);
444d3e125daSSagi Grimberg 		goto err_data_frpl;
445dc87a90fSSagi Grimberg 	}
446dc87a90fSSagi Grimberg 	pr_debug("Create fr_desc %p page_list %p\n",
447dc87a90fSSagi Grimberg 		 fr_desc, fr_desc->data_frpl->page_list);
448d3e125daSSagi Grimberg 	fr_desc->ind |= ISERT_DATA_KEY_VALID;
449dc87a90fSSagi Grimberg 
450d3e125daSSagi Grimberg 	if (protection) {
451d3e125daSSagi Grimberg 		struct ib_mr_init_attr mr_init_attr = {0};
452d3e125daSSagi Grimberg 		struct pi_context *pi_ctx;
453d3e125daSSagi Grimberg 
454d3e125daSSagi Grimberg 		fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
455d3e125daSSagi Grimberg 		if (!fr_desc->pi_ctx) {
456d3e125daSSagi Grimberg 			pr_err("Failed to allocate pi context\n");
457d3e125daSSagi Grimberg 			ret = -ENOMEM;
458d3e125daSSagi Grimberg 			goto err_data_mr;
459d3e125daSSagi Grimberg 		}
460d3e125daSSagi Grimberg 		pi_ctx = fr_desc->pi_ctx;
461d3e125daSSagi Grimberg 
462d3e125daSSagi Grimberg 		pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
463d3e125daSSagi Grimberg 						    ISCSI_ISER_SG_TABLESIZE);
464d3e125daSSagi Grimberg 		if (IS_ERR(pi_ctx->prot_frpl)) {
465d3e125daSSagi Grimberg 			pr_err("Failed to allocate prot frpl err=%ld\n",
466d3e125daSSagi Grimberg 			       PTR_ERR(pi_ctx->prot_frpl));
467d3e125daSSagi Grimberg 			ret = PTR_ERR(pi_ctx->prot_frpl);
468d3e125daSSagi Grimberg 			goto err_pi_ctx;
469d3e125daSSagi Grimberg 		}
470d3e125daSSagi Grimberg 
471d3e125daSSagi Grimberg 		pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
472d3e125daSSagi Grimberg 		if (IS_ERR(pi_ctx->prot_mr)) {
473d3e125daSSagi Grimberg 			pr_err("Failed to allocate prot frmr err=%ld\n",
474d3e125daSSagi Grimberg 			       PTR_ERR(pi_ctx->prot_mr));
475d3e125daSSagi Grimberg 			ret = PTR_ERR(pi_ctx->prot_mr);
476d3e125daSSagi Grimberg 			goto err_prot_frpl;
477d3e125daSSagi Grimberg 		}
478d3e125daSSagi Grimberg 		fr_desc->ind |= ISERT_PROT_KEY_VALID;
479d3e125daSSagi Grimberg 
480d3e125daSSagi Grimberg 		mr_init_attr.max_reg_descriptors = 2;
481d3e125daSSagi Grimberg 		mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
482d3e125daSSagi Grimberg 		pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
483d3e125daSSagi Grimberg 		if (IS_ERR(pi_ctx->sig_mr)) {
484d3e125daSSagi Grimberg 			pr_err("Failed to allocate signature enabled mr err=%ld\n",
485d3e125daSSagi Grimberg 			       PTR_ERR(pi_ctx->sig_mr));
486d3e125daSSagi Grimberg 			ret = PTR_ERR(pi_ctx->sig_mr);
487d3e125daSSagi Grimberg 			goto err_prot_mr;
488d3e125daSSagi Grimberg 		}
489d3e125daSSagi Grimberg 		fr_desc->ind |= ISERT_SIG_KEY_VALID;
490d3e125daSSagi Grimberg 	}
491d3e125daSSagi Grimberg 	fr_desc->ind &= ~ISERT_PROTECTED;
492dc87a90fSSagi Grimberg 
493dc87a90fSSagi Grimberg 	return 0;
494d3e125daSSagi Grimberg err_prot_mr:
495d3e125daSSagi Grimberg 	ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
496d3e125daSSagi Grimberg err_prot_frpl:
497d3e125daSSagi Grimberg 	ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
498d3e125daSSagi Grimberg err_pi_ctx:
499d3e125daSSagi Grimberg 	kfree(fr_desc->pi_ctx);
500d3e125daSSagi Grimberg err_data_mr:
501d3e125daSSagi Grimberg 	ib_dereg_mr(fr_desc->data_mr);
502d3e125daSSagi Grimberg err_data_frpl:
503d3e125daSSagi Grimberg 	ib_free_fast_reg_page_list(fr_desc->data_frpl);
504d3e125daSSagi Grimberg 
505d3e125daSSagi Grimberg 	return ret;
506dc87a90fSSagi Grimberg }
507dc87a90fSSagi Grimberg 
508dc87a90fSSagi Grimberg static int
509d3e125daSSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
51059464ef4SVu Pham {
51159464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
51259464ef4SVu Pham 	struct isert_device *device = isert_conn->conn_device;
513f46d6a8aSNicholas Bellinger 	struct se_session *se_sess = isert_conn->conn->sess->se_sess;
514f46d6a8aSNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
515f46d6a8aSNicholas Bellinger 	int i, ret, tag_num;
516f46d6a8aSNicholas Bellinger 	/*
517f46d6a8aSNicholas Bellinger 	 * Setup the number of FRMRs based upon the number of tags
518f46d6a8aSNicholas Bellinger 	 * available to session in iscsi_target_locate_portal().
519f46d6a8aSNicholas Bellinger 	 */
520f46d6a8aSNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
521f46d6a8aSNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
52259464ef4SVu Pham 
523a3a5a826SSagi Grimberg 	isert_conn->conn_fr_pool_size = 0;
524f46d6a8aSNicholas Bellinger 	for (i = 0; i < tag_num; i++) {
52559464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
52659464ef4SVu Pham 		if (!fr_desc) {
52759464ef4SVu Pham 			pr_err("Failed to allocate fast_reg descriptor\n");
52859464ef4SVu Pham 			ret = -ENOMEM;
52959464ef4SVu Pham 			goto err;
53059464ef4SVu Pham 		}
53159464ef4SVu Pham 
532dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
533d3e125daSSagi Grimberg 					   isert_conn->conn_pd, fr_desc,
534d3e125daSSagi Grimberg 					   pi_support);
535dc87a90fSSagi Grimberg 		if (ret) {
536dc87a90fSSagi Grimberg 			pr_err("Failed to create fastreg descriptor err=%d\n",
537dc87a90fSSagi Grimberg 			       ret);
538a80e21b3SNicholas Bellinger 			kfree(fr_desc);
53959464ef4SVu Pham 			goto err;
54059464ef4SVu Pham 		}
54159464ef4SVu Pham 
542a3a5a826SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
543a3a5a826SSagi Grimberg 		isert_conn->conn_fr_pool_size++;
54459464ef4SVu Pham 	}
54559464ef4SVu Pham 
546a3a5a826SSagi Grimberg 	pr_debug("Creating conn %p fastreg pool size=%d",
547a3a5a826SSagi Grimberg 		 isert_conn, isert_conn->conn_fr_pool_size);
54859464ef4SVu Pham 
54959464ef4SVu Pham 	return 0;
55059464ef4SVu Pham 
55159464ef4SVu Pham err:
552a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
55359464ef4SVu Pham 	return ret;
55459464ef4SVu Pham }
55559464ef4SVu Pham 
556b8d26b3bSNicholas Bellinger static int
557b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
558b8d26b3bSNicholas Bellinger {
559b8d26b3bSNicholas Bellinger 	struct iscsi_np *np = cma_id->context;
560b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = np->np_context;
561b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
562b8d26b3bSNicholas Bellinger 	struct isert_device *device;
563b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
564b8d26b3bSNicholas Bellinger 	int ret = 0;
56514f4b54fSSagi Grimberg 	u8 pi_support;
56614f4b54fSSagi Grimberg 
56714f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
56814f4b54fSSagi Grimberg 	if (!np->enabled) {
56914f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
57014f4b54fSSagi Grimberg 		pr_debug("iscsi_np is not enabled, reject connect request\n");
57114f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
57214f4b54fSSagi Grimberg 	}
57314f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
574b8d26b3bSNicholas Bellinger 
575b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
576b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
577b8d26b3bSNicholas Bellinger 
578b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
579b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
580b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn\n");
581b8d26b3bSNicholas Bellinger 		return -ENOMEM;
582b8d26b3bSNicholas Bellinger 	}
583b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
584b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
585b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
586defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait);
587defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait_comp_err);
588b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
589b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
59059464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
591f46d6a8aSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
592b8d26b3bSNicholas Bellinger 
593b8d26b3bSNicholas Bellinger 	cma_id->context = isert_conn;
594b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
595b8d26b3bSNicholas Bellinger 	isert_conn->responder_resources = event->param.conn.responder_resources;
596b8d26b3bSNicholas Bellinger 	isert_conn->initiator_depth = event->param.conn.initiator_depth;
597b8d26b3bSNicholas Bellinger 	pr_debug("Using responder_resources: %u initiator_depth: %u\n",
598b8d26b3bSNicholas Bellinger 		 isert_conn->responder_resources, isert_conn->initiator_depth);
599b8d26b3bSNicholas Bellinger 
600b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
601b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
602b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
603b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
604b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
605b8d26b3bSNicholas Bellinger 		goto out;
606b8d26b3bSNicholas Bellinger 	}
607b8d26b3bSNicholas Bellinger 
608b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
609b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
610b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
611b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
612b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
613b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
614b8d26b3bSNicholas Bellinger 
615b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
616b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
617b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
618b8d26b3bSNicholas Bellinger 
619b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
620b8d26b3bSNicholas Bellinger 	if (ret) {
621b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
622b8d26b3bSNicholas Bellinger 		       ret);
623b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
624b8d26b3bSNicholas Bellinger 		goto out_login_buf;
625b8d26b3bSNicholas Bellinger 	}
626b8d26b3bSNicholas Bellinger 
627b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
628b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
629b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
630b8d26b3bSNicholas Bellinger 
631b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
632b8d26b3bSNicholas Bellinger 	if (ret) {
633b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
634b8d26b3bSNicholas Bellinger 		       ret);
635b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
636b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
637b8d26b3bSNicholas Bellinger 	}
638b8d26b3bSNicholas Bellinger 
639b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
640b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
641b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
642b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
643b8d26b3bSNicholas Bellinger 	}
644b8d26b3bSNicholas Bellinger 
645b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
646eb6ab132SSagi Grimberg 	isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
647eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_pd)) {
648eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_pd);
649eb6ab132SSagi Grimberg 		pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
650eb6ab132SSagi Grimberg 		       isert_conn, ret);
651eb6ab132SSagi Grimberg 		goto out_pd;
652eb6ab132SSagi Grimberg 	}
653b8d26b3bSNicholas Bellinger 
654eb6ab132SSagi Grimberg 	isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
655eb6ab132SSagi Grimberg 					   IB_ACCESS_LOCAL_WRITE);
656eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_mr)) {
657eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_mr);
658eb6ab132SSagi Grimberg 		pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
659eb6ab132SSagi Grimberg 		       isert_conn, ret);
660eb6ab132SSagi Grimberg 		goto out_mr;
661eb6ab132SSagi Grimberg 	}
662b8d26b3bSNicholas Bellinger 
66314f4b54fSSagi Grimberg 	pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
664d3e125daSSagi Grimberg 	if (pi_support && !device->pi_capable) {
6655f80ff8eSSagi Grimberg 		pr_err("Protection information requested but not supported, "
6665f80ff8eSSagi Grimberg 		       "rejecting connect request\n");
6675f80ff8eSSagi Grimberg 		ret = rdma_reject(cma_id, NULL, 0);
668d3e125daSSagi Grimberg 		goto out_mr;
669d3e125daSSagi Grimberg 	}
670d3e125daSSagi Grimberg 
671d3e125daSSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
672b8d26b3bSNicholas Bellinger 	if (ret)
673b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
674b8d26b3bSNicholas Bellinger 
675b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
6769fe63c88SSagi Grimberg 	list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
677b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
678b8d26b3bSNicholas Bellinger 
679531b7bf4SSagi Grimberg 	pr_debug("isert_connect_request() up np_sem np: %p\n", np);
680531b7bf4SSagi Grimberg 	up(&isert_np->np_sem);
681b8d26b3bSNicholas Bellinger 	return 0;
682b8d26b3bSNicholas Bellinger 
683b8d26b3bSNicholas Bellinger out_conn_dev:
684eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
685eb6ab132SSagi Grimberg out_mr:
686eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
687eb6ab132SSagi Grimberg out_pd:
688b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
689b8d26b3bSNicholas Bellinger out_rsp_dma_map:
690b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
691b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
692b8d26b3bSNicholas Bellinger out_req_dma_map:
693b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
694b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
695b8d26b3bSNicholas Bellinger out_login_buf:
696b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
697b8d26b3bSNicholas Bellinger out:
698b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
699b8d26b3bSNicholas Bellinger 	return ret;
700b8d26b3bSNicholas Bellinger }
701b8d26b3bSNicholas Bellinger 
702b8d26b3bSNicholas Bellinger static void
703b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
704b8d26b3bSNicholas Bellinger {
705b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
706b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
707b8d26b3bSNicholas Bellinger 	int cq_index;
708b8d26b3bSNicholas Bellinger 
709b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
710b8d26b3bSNicholas Bellinger 
711a3a5a826SSagi Grimberg 	if (device && device->use_fastreg)
712a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
71359464ef4SVu Pham 
714b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
715b8d26b3bSNicholas Bellinger 		cq_index = ((struct isert_cq_desc *)
716b8d26b3bSNicholas Bellinger 			isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
717b8d26b3bSNicholas Bellinger 		pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
718b8d26b3bSNicholas Bellinger 		isert_conn->conn_device->cq_active_qps[cq_index]--;
719b8d26b3bSNicholas Bellinger 
720b8d26b3bSNicholas Bellinger 		rdma_destroy_qp(isert_conn->conn_cm_id);
721b8d26b3bSNicholas Bellinger 	}
722b8d26b3bSNicholas Bellinger 
723b8d26b3bSNicholas Bellinger 	isert_free_rx_descriptors(isert_conn);
724b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_conn->conn_cm_id);
725b8d26b3bSNicholas Bellinger 
726eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
727eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
728eb6ab132SSagi Grimberg 
729b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
730b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
731b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
732b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
733b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
734b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
735b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
736b8d26b3bSNicholas Bellinger 	}
737b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
738b8d26b3bSNicholas Bellinger 
739b8d26b3bSNicholas Bellinger 	if (device)
740b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
741b8d26b3bSNicholas Bellinger 
742b8d26b3bSNicholas Bellinger 	pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
743b8d26b3bSNicholas Bellinger }
744b8d26b3bSNicholas Bellinger 
745b8d26b3bSNicholas Bellinger static void
746b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
747b8d26b3bSNicholas Bellinger {
748*c2f88b17SSagi Grimberg 	struct isert_conn *isert_conn = cma_id->context;
749*c2f88b17SSagi Grimberg 
750*c2f88b17SSagi Grimberg 	kref_get(&isert_conn->conn_kref);
751b8d26b3bSNicholas Bellinger }
752b8d26b3bSNicholas Bellinger 
753b8d26b3bSNicholas Bellinger static void
754b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
755b8d26b3bSNicholas Bellinger {
756b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
757b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
758b8d26b3bSNicholas Bellinger 
759b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
760b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
761b8d26b3bSNicholas Bellinger 
762b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
763b8d26b3bSNicholas Bellinger }
764b8d26b3bSNicholas Bellinger 
765b8d26b3bSNicholas Bellinger static void
766b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
767b8d26b3bSNicholas Bellinger {
768b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
769b8d26b3bSNicholas Bellinger }
770b8d26b3bSNicholas Bellinger 
771b8d26b3bSNicholas Bellinger static void
772b8d26b3bSNicholas Bellinger isert_disconnect_work(struct work_struct *work)
773b8d26b3bSNicholas Bellinger {
774b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(work,
775b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_logout_work);
776b8d26b3bSNicholas Bellinger 
777b8d26b3bSNicholas Bellinger 	pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
778b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
779defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
780defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
781b8d26b3bSNicholas Bellinger 
782b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
783b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
784b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
785b2cb9649SNicholas Bellinger 		goto wake_up;
786b8d26b3bSNicholas Bellinger 	}
787b2cb9649SNicholas Bellinger 	if (!isert_conn->conn_cm_id) {
788b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
789b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
790b2cb9649SNicholas Bellinger 		return;
791b2cb9649SNicholas Bellinger 	}
7929d49f5e2SSagi Grimberg 
79388c4015fSSagi Grimberg 	if (isert_conn->disconnect) {
7949d49f5e2SSagi Grimberg 		/* Send DREQ/DREP towards our initiator */
795b2cb9649SNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
796b2cb9649SNicholas Bellinger 	}
7979d49f5e2SSagi Grimberg 
798b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
799b8d26b3bSNicholas Bellinger 
800b2cb9649SNicholas Bellinger wake_up:
801defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait);
802b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
803b8d26b3bSNicholas Bellinger }
804b8d26b3bSNicholas Bellinger 
805b8d26b3bSNicholas Bellinger static void
80688c4015fSSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id, bool disconnect)
807b8d26b3bSNicholas Bellinger {
808b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
809b8d26b3bSNicholas Bellinger 
81088c4015fSSagi Grimberg 	isert_conn->disconnect = disconnect;
811b8d26b3bSNicholas Bellinger 	INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
812b8d26b3bSNicholas Bellinger 	schedule_work(&isert_conn->conn_logout_work);
813b8d26b3bSNicholas Bellinger }
814b8d26b3bSNicholas Bellinger 
815b8d26b3bSNicholas Bellinger static int
816b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
817b8d26b3bSNicholas Bellinger {
818b8d26b3bSNicholas Bellinger 	int ret = 0;
81988c4015fSSagi Grimberg 	bool disconnect = false;
820b8d26b3bSNicholas Bellinger 
821b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
822b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
823b8d26b3bSNicholas Bellinger 
824b8d26b3bSNicholas Bellinger 	switch (event->event) {
825b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
826b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
827b8d26b3bSNicholas Bellinger 		break;
828b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
829b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
830b8d26b3bSNicholas Bellinger 		break;
83188c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
83288c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
83388c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
83488c4015fSSagi Grimberg 		disconnect = true;
83588c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
83688c4015fSSagi Grimberg 		isert_disconnected_handler(cma_id, disconnect);
837b8d26b3bSNicholas Bellinger 		break;
838b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
839b8d26b3bSNicholas Bellinger 	default:
84088c4015fSSagi Grimberg 		pr_err("Unhandled RDMA CMA event: %d\n", event->event);
841b8d26b3bSNicholas Bellinger 		break;
842b8d26b3bSNicholas Bellinger 	}
843b8d26b3bSNicholas Bellinger 
844b8d26b3bSNicholas Bellinger 	if (ret != 0) {
845b8d26b3bSNicholas Bellinger 		pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
846b8d26b3bSNicholas Bellinger 		       event->event, ret);
847b8d26b3bSNicholas Bellinger 		dump_stack();
848b8d26b3bSNicholas Bellinger 	}
849b8d26b3bSNicholas Bellinger 
850b8d26b3bSNicholas Bellinger 	return ret;
851b8d26b3bSNicholas Bellinger }
852b8d26b3bSNicholas Bellinger 
853b8d26b3bSNicholas Bellinger static int
854b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
855b8d26b3bSNicholas Bellinger {
856b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
857b8d26b3bSNicholas Bellinger 	int i, ret;
858b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
859b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
860b8d26b3bSNicholas Bellinger 
861b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
862b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
863b8d26b3bSNicholas Bellinger 		rx_wr->wr_id	= (unsigned long)rx_desc;
864b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
865b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
866b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
867b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
868b8d26b3bSNicholas Bellinger 	}
869b8d26b3bSNicholas Bellinger 
870b8d26b3bSNicholas Bellinger 	rx_wr--;
871b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
872b8d26b3bSNicholas Bellinger 
873b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
874b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
875b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
876b8d26b3bSNicholas Bellinger 	if (ret) {
877b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed with ret: %d\n", ret);
878b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
879b8d26b3bSNicholas Bellinger 	} else {
880b8d26b3bSNicholas Bellinger 		pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
881b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
882b8d26b3bSNicholas Bellinger 	}
883b8d26b3bSNicholas Bellinger 	return ret;
884b8d26b3bSNicholas Bellinger }
885b8d26b3bSNicholas Bellinger 
886b8d26b3bSNicholas Bellinger static int
887b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
888b8d26b3bSNicholas Bellinger {
889b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
890b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
891b8d26b3bSNicholas Bellinger 	int ret;
892b8d26b3bSNicholas Bellinger 
893b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
894b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
895b8d26b3bSNicholas Bellinger 
896b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
897b8d26b3bSNicholas Bellinger 	send_wr.wr_id	= (unsigned long)tx_desc;
898b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
899b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
900b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
901b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
902b8d26b3bSNicholas Bellinger 
903b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
904b8d26b3bSNicholas Bellinger 
905b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
906b8d26b3bSNicholas Bellinger 	if (ret) {
907b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send() failed, ret: %d\n", ret);
908b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
909b8d26b3bSNicholas Bellinger 	}
910b8d26b3bSNicholas Bellinger 
911b8d26b3bSNicholas Bellinger 	return ret;
912b8d26b3bSNicholas Bellinger }
913b8d26b3bSNicholas Bellinger 
914b8d26b3bSNicholas Bellinger static void
915b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
916b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
917b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
918b8d26b3bSNicholas Bellinger {
919b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
920b8d26b3bSNicholas Bellinger 
921b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
922b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
923b8d26b3bSNicholas Bellinger 
924b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
925b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
926b8d26b3bSNicholas Bellinger 
927b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
928b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
929b8d26b3bSNicholas Bellinger 
930b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
931b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
932b8d26b3bSNicholas Bellinger 		pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
933b8d26b3bSNicholas Bellinger 	}
934b8d26b3bSNicholas Bellinger }
935b8d26b3bSNicholas Bellinger 
936b8d26b3bSNicholas Bellinger static int
937b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
938b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
939b8d26b3bSNicholas Bellinger {
940b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
941b8d26b3bSNicholas Bellinger 	u64 dma_addr;
942b8d26b3bSNicholas Bellinger 
943b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
944b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
945b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
946b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error() failed\n");
947b8d26b3bSNicholas Bellinger 		return -ENOMEM;
948b8d26b3bSNicholas Bellinger 	}
949b8d26b3bSNicholas Bellinger 
950b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
951b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
952b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
953b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
954b8d26b3bSNicholas Bellinger 
955b8d26b3bSNicholas Bellinger 	pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
956b8d26b3bSNicholas Bellinger 		 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
957b8d26b3bSNicholas Bellinger 		 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
958b8d26b3bSNicholas Bellinger 
959b8d26b3bSNicholas Bellinger 	return 0;
960b8d26b3bSNicholas Bellinger }
961b8d26b3bSNicholas Bellinger 
962b8d26b3bSNicholas Bellinger static void
96395b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
96495b60f07SNicholas Bellinger 		   struct ib_send_wr *send_wr, bool coalesce)
965b8d26b3bSNicholas Bellinger {
96695b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
96795b60f07SNicholas Bellinger 
968b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
969b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
970b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
97195b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
972b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
97395b60f07SNicholas Bellinger 	/*
97495b60f07SNicholas Bellinger 	 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
97595b60f07SNicholas Bellinger 	 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
97695b60f07SNicholas Bellinger 	 */
977ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
978ebbe4421SNicholas Bellinger 	if (coalesce && isert_conn->state == ISER_CONN_UP &&
97995b60f07SNicholas Bellinger 	    ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
980ebbe4421SNicholas Bellinger 		tx_desc->llnode_active = true;
98195b60f07SNicholas Bellinger 		llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
982ebbe4421SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
98395b60f07SNicholas Bellinger 		return;
98495b60f07SNicholas Bellinger 	}
98595b60f07SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
98695b60f07SNicholas Bellinger 	tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
987ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
98895b60f07SNicholas Bellinger 
98995b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
990b8d26b3bSNicholas Bellinger }
991b8d26b3bSNicholas Bellinger 
992b8d26b3bSNicholas Bellinger static int
993b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
994b8d26b3bSNicholas Bellinger {
995b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
996b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
997b8d26b3bSNicholas Bellinger 	int ret;
998b8d26b3bSNicholas Bellinger 
999b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
1000b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
1001b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
1002b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
1003b8d26b3bSNicholas Bellinger 
1004b8d26b3bSNicholas Bellinger 	pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1005b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
1006b8d26b3bSNicholas Bellinger 
1007b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1008b8d26b3bSNicholas Bellinger 	rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1009b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1010b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1011b8d26b3bSNicholas Bellinger 
1012b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1013b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1014b8d26b3bSNicholas Bellinger 	if (ret) {
1015b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed: %d\n", ret);
1016b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1017b8d26b3bSNicholas Bellinger 	}
1018b8d26b3bSNicholas Bellinger 
1019b8d26b3bSNicholas Bellinger 	pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1020b8d26b3bSNicholas Bellinger 	return ret;
1021b8d26b3bSNicholas Bellinger }
1022b8d26b3bSNicholas Bellinger 
1023b8d26b3bSNicholas Bellinger static int
1024b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1025b8d26b3bSNicholas Bellinger 		   u32 length)
1026b8d26b3bSNicholas Bellinger {
1027b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1028b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1029b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1030b8d26b3bSNicholas Bellinger 	int ret;
1031b8d26b3bSNicholas Bellinger 
1032b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1033b8d26b3bSNicholas Bellinger 
1034b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1035b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1036b8d26b3bSNicholas Bellinger 
1037b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1038b8d26b3bSNicholas Bellinger 
1039b8d26b3bSNicholas Bellinger 	if (length > 0) {
1040b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1041b8d26b3bSNicholas Bellinger 
1042b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1043b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1044b8d26b3bSNicholas Bellinger 
1045b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1046b8d26b3bSNicholas Bellinger 
1047b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1048b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1049b8d26b3bSNicholas Bellinger 
1050b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1051b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
1052b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1053b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1054b8d26b3bSNicholas Bellinger 	}
1055b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1056b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1057e0546fc1SSagi Grimberg 			if (!conn->sess->sess_ops->SessionType &&
1058e0546fc1SSagi Grimberg 			    isert_conn->conn_device->use_fastreg) {
1059e0546fc1SSagi Grimberg 				/* Normal Session and fastreg is used */
1060f46d6a8aSNicholas Bellinger 				u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1061f46d6a8aSNicholas Bellinger 
1062f46d6a8aSNicholas Bellinger 				ret = isert_conn_create_fastreg_pool(isert_conn,
1063f46d6a8aSNicholas Bellinger 								     pi_support);
1064f46d6a8aSNicholas Bellinger 				if (ret) {
1065f46d6a8aSNicholas Bellinger 					pr_err("Conn: %p failed to create"
1066f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1067f46d6a8aSNicholas Bellinger 					return ret;
1068f46d6a8aSNicholas Bellinger 				}
1069f46d6a8aSNicholas Bellinger 			}
1070f46d6a8aSNicholas Bellinger 
1071b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1072b8d26b3bSNicholas Bellinger 			if (ret)
1073b8d26b3bSNicholas Bellinger 				return ret;
1074b8d26b3bSNicholas Bellinger 
1075b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1076b8d26b3bSNicholas Bellinger 			if (ret)
1077b8d26b3bSNicholas Bellinger 				return ret;
1078b8d26b3bSNicholas Bellinger 
1079b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_UP;
1080b8d26b3bSNicholas Bellinger 			goto post_send;
1081b8d26b3bSNicholas Bellinger 		}
1082b8d26b3bSNicholas Bellinger 
1083b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1084b8d26b3bSNicholas Bellinger 		if (ret)
1085b8d26b3bSNicholas Bellinger 			return ret;
1086b8d26b3bSNicholas Bellinger 	}
1087b8d26b3bSNicholas Bellinger post_send:
1088b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1089b8d26b3bSNicholas Bellinger 	if (ret)
1090b8d26b3bSNicholas Bellinger 		return ret;
1091b8d26b3bSNicholas Bellinger 
1092b8d26b3bSNicholas Bellinger 	return 0;
1093b8d26b3bSNicholas Bellinger }
1094b8d26b3bSNicholas Bellinger 
1095b8d26b3bSNicholas Bellinger static void
1096b8d26b3bSNicholas Bellinger isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1097b8d26b3bSNicholas Bellinger 		   struct isert_conn *isert_conn)
1098b8d26b3bSNicholas Bellinger {
1099b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1100b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1101b8d26b3bSNicholas Bellinger 	int size;
1102b8d26b3bSNicholas Bellinger 
1103b8d26b3bSNicholas Bellinger 	if (!login) {
1104b8d26b3bSNicholas Bellinger 		pr_err("conn->conn_login is NULL\n");
1105b8d26b3bSNicholas Bellinger 		dump_stack();
1106b8d26b3bSNicholas Bellinger 		return;
1107b8d26b3bSNicholas Bellinger 	}
1108b8d26b3bSNicholas Bellinger 
1109b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1110b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1111b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1112b8d26b3bSNicholas Bellinger 		/*
1113b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1114b8d26b3bSNicholas Bellinger 		 * login request PDU.
1115b8d26b3bSNicholas Bellinger 		 */
1116b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1117b8d26b3bSNicholas Bellinger 		login->current_stage =
1118b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1119b8d26b3bSNicholas Bellinger 			 >> 2;
1120b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1121b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1122b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1123b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1124b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1125b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1126b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1127b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1128b8d26b3bSNicholas Bellinger 	}
1129b8d26b3bSNicholas Bellinger 
1130b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1131b8d26b3bSNicholas Bellinger 
1132b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1133b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1134b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1135b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1136b8d26b3bSNicholas Bellinger 
11376faaa85fSNicholas Bellinger 	if (login->first_request) {
1138b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
11396faaa85fSNicholas Bellinger 		return;
11406faaa85fSNicholas Bellinger 	}
11416faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1142b8d26b3bSNicholas Bellinger }
1143b8d26b3bSNicholas Bellinger 
1144b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1145676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1146b8d26b3bSNicholas Bellinger {
1147b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1148b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1149d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1150b8d26b3bSNicholas Bellinger 
1151676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1152d703ce2fSNicholas Bellinger 	if (!cmd) {
1153d703ce2fSNicholas Bellinger 		pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1154b8d26b3bSNicholas Bellinger 		return NULL;
1155b8d26b3bSNicholas Bellinger 	}
1156d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1157b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1158d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1159b8d26b3bSNicholas Bellinger 
1160d703ce2fSNicholas Bellinger 	return cmd;
1161b8d26b3bSNicholas Bellinger }
1162b8d26b3bSNicholas Bellinger 
1163b8d26b3bSNicholas Bellinger static int
1164b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1165d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1166d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1167b8d26b3bSNicholas Bellinger {
1168b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1169b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1170b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1171b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1172b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1173b8d26b3bSNicholas Bellinger 
1174b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1175b8d26b3bSNicholas Bellinger 	if (rc < 0)
1176b8d26b3bSNicholas Bellinger 		return rc;
1177b8d26b3bSNicholas Bellinger 
1178b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1179b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1180b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1181b8d26b3bSNicholas Bellinger 
1182b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1183b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1184b8d26b3bSNicholas Bellinger 		return 0;
1185b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1186b8d26b3bSNicholas Bellinger 		dump_payload = true;
1187b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1188b8d26b3bSNicholas Bellinger 	}
1189b8d26b3bSNicholas Bellinger 
1190b8d26b3bSNicholas Bellinger 	if (!imm_data)
1191b8d26b3bSNicholas Bellinger 		return 0;
1192b8d26b3bSNicholas Bellinger 
1193b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1194b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1195b8d26b3bSNicholas Bellinger 
1196b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1197b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1198b8d26b3bSNicholas Bellinger 
1199b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1200b8d26b3bSNicholas Bellinger 
1201b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1202b8d26b3bSNicholas Bellinger 
1203b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1204b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1205b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1206b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1207b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1208b8d26b3bSNicholas Bellinger 	}
1209b8d26b3bSNicholas Bellinger 
1210b8d26b3bSNicholas Bellinger sequence_cmd:
1211561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1212b8d26b3bSNicholas Bellinger 
1213b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1214b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
12156cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
12166cc44a6fSNicholas Bellinger 		target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1217b8d26b3bSNicholas Bellinger 
1218b8d26b3bSNicholas Bellinger 	return 0;
1219b8d26b3bSNicholas Bellinger }
1220b8d26b3bSNicholas Bellinger 
1221b8d26b3bSNicholas Bellinger static int
1222b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1223b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1224b8d26b3bSNicholas Bellinger {
1225b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1226b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1227b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1228b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1229b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1230b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1231b8d26b3bSNicholas Bellinger 
1232b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1233b8d26b3bSNicholas Bellinger 	if (rc < 0)
1234b8d26b3bSNicholas Bellinger 		return rc;
1235b8d26b3bSNicholas Bellinger 	else if (!cmd)
1236b8d26b3bSNicholas Bellinger 		return 0;
1237b8d26b3bSNicholas Bellinger 	/*
1238b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1239b8d26b3bSNicholas Bellinger 	 */
1240b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
1241b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
1242b8d26b3bSNicholas Bellinger 		dump_stack();
1243b8d26b3bSNicholas Bellinger 		return -1;
1244b8d26b3bSNicholas Bellinger 	}
1245b8d26b3bSNicholas Bellinger 
1246b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1247b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1248b8d26b3bSNicholas Bellinger 
1249b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1250b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1251b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1252b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1253b8d26b3bSNicholas Bellinger 	/*
1254b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1255b8d26b3bSNicholas Bellinger 	 */
1256b8d26b3bSNicholas Bellinger 	if (page_off) {
1257b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1258b8d26b3bSNicholas Bellinger 		dump_stack();
1259b8d26b3bSNicholas Bellinger 		return -1;
1260b8d26b3bSNicholas Bellinger 	}
1261b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1262b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1263b8d26b3bSNicholas Bellinger 
1264b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1265b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1266b8d26b3bSNicholas Bellinger 
1267b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1268b8d26b3bSNicholas Bellinger 	if (rc < 0)
1269b8d26b3bSNicholas Bellinger 		return rc;
1270b8d26b3bSNicholas Bellinger 
1271b8d26b3bSNicholas Bellinger 	return 0;
1272b8d26b3bSNicholas Bellinger }
1273b8d26b3bSNicholas Bellinger 
1274b8d26b3bSNicholas Bellinger static int
1275778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1276d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1277d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1278778de368SNicholas Bellinger {
1279778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1280778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1281778de368SNicholas Bellinger 	int rc;
1282778de368SNicholas Bellinger 
1283778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1284778de368SNicholas Bellinger 	if (rc < 0)
1285778de368SNicholas Bellinger 		return rc;
1286778de368SNicholas Bellinger 	/*
1287778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1288778de368SNicholas Bellinger 	 */
1289778de368SNicholas Bellinger 
1290778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1291778de368SNicholas Bellinger }
1292778de368SNicholas Bellinger 
1293778de368SNicholas Bellinger static int
1294adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1295d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1296d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1297adb54c29SNicholas Bellinger {
1298adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1299adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1300adb54c29SNicholas Bellinger 	int rc;
1301adb54c29SNicholas Bellinger 	unsigned char *text_in;
1302adb54c29SNicholas Bellinger 
1303adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1304adb54c29SNicholas Bellinger 	if (rc < 0)
1305adb54c29SNicholas Bellinger 		return rc;
1306adb54c29SNicholas Bellinger 
1307adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1308adb54c29SNicholas Bellinger 	if (!text_in) {
1309adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1310adb54c29SNicholas Bellinger 		       payload_length);
1311adb54c29SNicholas Bellinger 		return -ENOMEM;
1312adb54c29SNicholas Bellinger 	}
1313adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1314adb54c29SNicholas Bellinger 
1315adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1316adb54c29SNicholas Bellinger 
1317adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1318adb54c29SNicholas Bellinger }
1319adb54c29SNicholas Bellinger 
1320adb54c29SNicholas Bellinger static int
1321b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1322b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1323b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1324b8d26b3bSNicholas Bellinger {
1325b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1326b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1327ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1328b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1329b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1330b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1331b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1332b8d26b3bSNicholas Bellinger 
1333ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1334ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1335ca40d24eSNicholas Bellinger 		pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1336ca40d24eSNicholas Bellinger 		       " ignoring\n", opcode);
1337ca40d24eSNicholas Bellinger 		return 0;
1338ca40d24eSNicholas Bellinger 	}
1339ca40d24eSNicholas Bellinger 
1340b8d26b3bSNicholas Bellinger 	switch (opcode) {
1341b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1342676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1343b8d26b3bSNicholas Bellinger 		if (!cmd)
1344b8d26b3bSNicholas Bellinger 			break;
1345b8d26b3bSNicholas Bellinger 
1346d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1347b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1348b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1349b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1350b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1351b8d26b3bSNicholas Bellinger 
1352d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1353b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1354b8d26b3bSNicholas Bellinger 		break;
1355b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1356676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1357b8d26b3bSNicholas Bellinger 		if (!cmd)
1358b8d26b3bSNicholas Bellinger 			break;
1359b8d26b3bSNicholas Bellinger 
1360d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1361d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1362778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1363b8d26b3bSNicholas Bellinger 		break;
1364b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1365b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1366b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1367b8d26b3bSNicholas Bellinger 		break;
1368b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1369676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1370b8d26b3bSNicholas Bellinger 		if (!cmd)
1371b8d26b3bSNicholas Bellinger 			break;
1372b8d26b3bSNicholas Bellinger 
1373b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1374b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1375b8d26b3bSNicholas Bellinger 		break;
1376b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1377676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1378b8d26b3bSNicholas Bellinger 		if (!cmd)
1379b8d26b3bSNicholas Bellinger 			break;
1380b8d26b3bSNicholas Bellinger 
1381b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1382b8d26b3bSNicholas Bellinger 		if (ret > 0)
1383b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1384b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1385b8d26b3bSNicholas Bellinger 						    HZ);
1386b8d26b3bSNicholas Bellinger 		break;
1387adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1388676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1389adb54c29SNicholas Bellinger 		if (!cmd)
1390adb54c29SNicholas Bellinger 			break;
1391adb54c29SNicholas Bellinger 
1392d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1393d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1394adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1395adb54c29SNicholas Bellinger 		break;
1396b8d26b3bSNicholas Bellinger 	default:
1397b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1398b8d26b3bSNicholas Bellinger 		dump_stack();
1399b8d26b3bSNicholas Bellinger 		break;
1400b8d26b3bSNicholas Bellinger 	}
1401b8d26b3bSNicholas Bellinger 
1402b8d26b3bSNicholas Bellinger 	return ret;
1403b8d26b3bSNicholas Bellinger }
1404b8d26b3bSNicholas Bellinger 
1405b8d26b3bSNicholas Bellinger static void
1406b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1407b8d26b3bSNicholas Bellinger {
1408b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1409b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1410b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1411b8d26b3bSNicholas Bellinger 	int rc;
1412b8d26b3bSNicholas Bellinger 
1413b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1414b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1415b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1416b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1417b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1418b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1419b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1420b8d26b3bSNicholas Bellinger 		}
1421b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1422b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1423b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1424b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1425b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1426b8d26b3bSNicholas Bellinger 		}
1427b8d26b3bSNicholas Bellinger 
1428b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1429b8d26b3bSNicholas Bellinger 		break;
1430b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1431b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1432b8d26b3bSNicholas Bellinger 		break;
1433b8d26b3bSNicholas Bellinger 	default:
1434b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1435b8d26b3bSNicholas Bellinger 		break;
1436b8d26b3bSNicholas Bellinger 	}
1437b8d26b3bSNicholas Bellinger 
1438b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1439b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1440b8d26b3bSNicholas Bellinger }
1441b8d26b3bSNicholas Bellinger 
1442b8d26b3bSNicholas Bellinger static void
1443b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1444b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1445b8d26b3bSNicholas Bellinger {
1446b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1447b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1448b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1449b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1450b8d26b3bSNicholas Bellinger 
1451b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1452b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1453b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1454b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1455b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1456b8d26b3bSNicholas Bellinger 	} else {
1457b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1458b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1459b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1460b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1461b8d26b3bSNicholas Bellinger 	}
1462b8d26b3bSNicholas Bellinger 
1463b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1464b8d26b3bSNicholas Bellinger 
1465b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1466b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1467b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1468b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1469b8d26b3bSNicholas Bellinger 
1470b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1471b8d26b3bSNicholas Bellinger 		isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1472b8d26b3bSNicholas Bellinger 				   isert_conn);
1473b8d26b3bSNicholas Bellinger 	else
1474b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
1475b8d26b3bSNicholas Bellinger 
1476b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1477b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1478b8d26b3bSNicholas Bellinger 
1479b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1480b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1481b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1482b8d26b3bSNicholas Bellinger 
1483b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1484b8d26b3bSNicholas Bellinger 		return;
1485b8d26b3bSNicholas Bellinger 
1486b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1487b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1488b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1489b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1490b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1491b8d26b3bSNicholas Bellinger 		if (err) {
1492b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1493b8d26b3bSNicholas Bellinger 			       count, err);
1494b8d26b3bSNicholas Bellinger 		}
1495b8d26b3bSNicholas Bellinger 	}
1496b8d26b3bSNicholas Bellinger }
1497b8d26b3bSNicholas Bellinger 
1498e3d7e4c3SSagi Grimberg static int
1499e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1500e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1501e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1502e3d7e4c3SSagi Grimberg {
1503e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1504e3d7e4c3SSagi Grimberg 
1505e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1506e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1507e3d7e4c3SSagi Grimberg 
1508e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1509e3d7e4c3SSagi Grimberg 	data->offset = offset;
1510e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1511e3d7e4c3SSagi Grimberg 
1512e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1513e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1514e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1515e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1516e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1517e3d7e4c3SSagi Grimberg 
1518e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1519e3d7e4c3SSagi Grimberg 					data->dma_dir);
1520e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
1521e3d7e4c3SSagi Grimberg 		pr_err("Cmd: unable to dma map SGs %p\n", sg);
1522e3d7e4c3SSagi Grimberg 		return -EINVAL;
1523e3d7e4c3SSagi Grimberg 	}
1524e3d7e4c3SSagi Grimberg 
1525e3d7e4c3SSagi Grimberg 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1526e3d7e4c3SSagi Grimberg 		 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1527e3d7e4c3SSagi Grimberg 
1528e3d7e4c3SSagi Grimberg 	return 0;
1529e3d7e4c3SSagi Grimberg }
1530e3d7e4c3SSagi Grimberg 
1531e3d7e4c3SSagi Grimberg static void
1532e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1533e3d7e4c3SSagi Grimberg {
1534e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1535e3d7e4c3SSagi Grimberg 
1536e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1537e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1538e3d7e4c3SSagi Grimberg }
1539e3d7e4c3SSagi Grimberg 
1540e3d7e4c3SSagi Grimberg 
1541e3d7e4c3SSagi Grimberg 
1542b8d26b3bSNicholas Bellinger static void
1543b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1544b8d26b3bSNicholas Bellinger {
1545b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1546b8d26b3bSNicholas Bellinger 
154790ecc6e2SVu Pham 	pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1548e3d7e4c3SSagi Grimberg 
1549e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
155090ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1551e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1552b8d26b3bSNicholas Bellinger 	}
1553b8d26b3bSNicholas Bellinger 
155490ecc6e2SVu Pham 	if (wr->send_wr) {
155590ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1556b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1557b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
155890ecc6e2SVu Pham 	}
1559b8d26b3bSNicholas Bellinger 
156090ecc6e2SVu Pham 	if (wr->ib_sge) {
156190ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
156290ecc6e2SVu Pham 		kfree(wr->ib_sge);
156390ecc6e2SVu Pham 		wr->ib_sge = NULL;
156490ecc6e2SVu Pham 	}
1565b8d26b3bSNicholas Bellinger }
1566b8d26b3bSNicholas Bellinger 
1567b8d26b3bSNicholas Bellinger static void
1568a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
156959464ef4SVu Pham {
157059464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
157159464ef4SVu Pham 	LIST_HEAD(unmap_list);
157259464ef4SVu Pham 
1573a3a5a826SSagi Grimberg 	pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
157459464ef4SVu Pham 
157559464ef4SVu Pham 	if (wr->fr_desc) {
1576a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
157759464ef4SVu Pham 			 isert_cmd, wr->fr_desc);
15789e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
15799e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
15809e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
15819e961ae7SSagi Grimberg 		}
158259464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1583a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
158459464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
158559464ef4SVu Pham 		wr->fr_desc = NULL;
158659464ef4SVu Pham 	}
158759464ef4SVu Pham 
1588e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
1589a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1590e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
159159464ef4SVu Pham 	}
159259464ef4SVu Pham 
159359464ef4SVu Pham 	wr->ib_sge = NULL;
159459464ef4SVu Pham 	wr->send_wr = NULL;
159559464ef4SVu Pham }
159659464ef4SVu Pham 
159759464ef4SVu Pham static void
159803e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1599b8d26b3bSNicholas Bellinger {
1600d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1601b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1602186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1603d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1604b8d26b3bSNicholas Bellinger 
1605b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1606b8d26b3bSNicholas Bellinger 
1607b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1608b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1609b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1610b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16115159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1612b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1613b8d26b3bSNicholas Bellinger 
161403e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1615b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
161603e7848aSNicholas Bellinger 			/*
161703e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
161803e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
161903e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
162003e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
162103e7848aSNicholas Bellinger 			 */
162203e7848aSNicholas Bellinger 			if (comp_err &&
162303e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
162403e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
162503e7848aSNicholas Bellinger 
162603e7848aSNicholas Bellinger 				target_put_sess_cmd(se_cmd->se_sess, se_cmd);
162703e7848aSNicholas Bellinger 			}
162803e7848aSNicholas Bellinger 		}
1629b8d26b3bSNicholas Bellinger 
1630d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1631186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1632186a9647SNicholas Bellinger 		break;
1633b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1634186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1635186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16365159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1637186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1638186a9647SNicholas Bellinger 
1639b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1640b8d26b3bSNicholas Bellinger 		break;
1641b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1642b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1643adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1644b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1645b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16465159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1647b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1648b8d26b3bSNicholas Bellinger 
1649b8d26b3bSNicholas Bellinger 		/*
1650b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1651b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1652b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1653b8d26b3bSNicholas Bellinger 		 */
1654b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
16553df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
16563df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
16573df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1658b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1659b8d26b3bSNicholas Bellinger 			break;
1660b8d26b3bSNicholas Bellinger 		}
1661b8d26b3bSNicholas Bellinger 		/*
1662b8d26b3bSNicholas Bellinger 		 * Fall-through
1663b8d26b3bSNicholas Bellinger 		 */
1664b8d26b3bSNicholas Bellinger 	default:
1665d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1666b8d26b3bSNicholas Bellinger 		break;
1667b8d26b3bSNicholas Bellinger 	}
1668b8d26b3bSNicholas Bellinger }
1669b8d26b3bSNicholas Bellinger 
1670b8d26b3bSNicholas Bellinger static void
1671b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1672b8d26b3bSNicholas Bellinger {
1673b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1674b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1675b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1676b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1677b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1678b8d26b3bSNicholas Bellinger 	}
1679b8d26b3bSNicholas Bellinger }
1680b8d26b3bSNicholas Bellinger 
1681b8d26b3bSNicholas Bellinger static void
1682b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
168303e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1684b8d26b3bSNicholas Bellinger {
1685dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1686dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1687dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1688dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1689dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1690b8d26b3bSNicholas Bellinger 	}
1691b8d26b3bSNicholas Bellinger 
1692b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
169303e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1694b8d26b3bSNicholas Bellinger }
1695b8d26b3bSNicholas Bellinger 
169696b7973eSSagi Grimberg static int
169796b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1698f93f3a70SSagi Grimberg {
16999e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
170096b7973eSSagi Grimberg 	int ret;
1701f93f3a70SSagi Grimberg 
170296b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
17039e961ae7SSagi Grimberg 	if (ret) {
17049e961ae7SSagi Grimberg 		pr_err("ib_check_mr_status failed, ret %d\n", ret);
17059e961ae7SSagi Grimberg 		goto fail_mr_status;
17069e961ae7SSagi Grimberg 	}
170796b7973eSSagi Grimberg 
17089e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
170996b7973eSSagi Grimberg 		u64 sec_offset_err;
17109e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
17119e961ae7SSagi Grimberg 
17129e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
17139e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
17149e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
17159e961ae7SSagi Grimberg 			break;
17169e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
17179e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
17189e961ae7SSagi Grimberg 			break;
17199e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
17209e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
17219e961ae7SSagi Grimberg 			break;
17229e961ae7SSagi Grimberg 		}
172396b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
172496b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
172596b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
17269e961ae7SSagi Grimberg 
17279e961ae7SSagi Grimberg 		pr_err("isert: PI error found type %d at sector 0x%llx "
17289e961ae7SSagi Grimberg 		       "expected 0x%x vs actual 0x%x\n",
17299e961ae7SSagi Grimberg 		       mr_status.sig_err.err_type,
17309e961ae7SSagi Grimberg 		       (unsigned long long)se_cmd->bad_sector,
17319e961ae7SSagi Grimberg 		       mr_status.sig_err.expected,
17329e961ae7SSagi Grimberg 		       mr_status.sig_err.actual);
17339e961ae7SSagi Grimberg 		ret = 1;
17349e961ae7SSagi Grimberg 	}
17359e961ae7SSagi Grimberg 
17369e961ae7SSagi Grimberg fail_mr_status:
173796b7973eSSagi Grimberg 	return ret;
173896b7973eSSagi Grimberg }
173996b7973eSSagi Grimberg 
174096b7973eSSagi Grimberg static void
174196b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
174296b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
174396b7973eSSagi Grimberg {
174496b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
174596b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
174696b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
174796b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
174896b7973eSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
174996b7973eSSagi Grimberg 	int ret = 0;
175096b7973eSSagi Grimberg 
175196b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
175296b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
175396b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
175496b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
175596b7973eSSagi Grimberg 	}
175696b7973eSSagi Grimberg 
1757f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1758897bb2c9SSagi Grimberg 	wr->send_wr_num = 0;
17599e961ae7SSagi Grimberg 	if (ret)
17609e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
17619e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
17629e961ae7SSagi Grimberg 	else
1763f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1764f93f3a70SSagi Grimberg }
1765f93f3a70SSagi Grimberg 
1766f93f3a70SSagi Grimberg static void
1767b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1768b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1769b8d26b3bSNicholas Bellinger {
1770b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1771d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1772b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
177390ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1774d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
17755bac4b1aSSagi Grimberg 	int ret = 0;
1776b8d26b3bSNicholas Bellinger 
17779e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
177896b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
177996b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
178096b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
17819e961ae7SSagi Grimberg 	}
17829e961ae7SSagi Grimberg 
1783b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1784d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1785e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1786b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1787b8d26b3bSNicholas Bellinger 
178890ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1789b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1790b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1791b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1792b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1793b8d26b3bSNicholas Bellinger 
17945bac4b1aSSagi Grimberg 	if (ret)
17955bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
17965bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
17975bac4b1aSSagi Grimberg 	else
1798b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1799b8d26b3bSNicholas Bellinger }
1800b8d26b3bSNicholas Bellinger 
1801b8d26b3bSNicholas Bellinger static void
1802b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1803b8d26b3bSNicholas Bellinger {
1804b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1805b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1806b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1807b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1808d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1809b8d26b3bSNicholas Bellinger 
1810b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1811b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1812b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1813b8d26b3bSNicholas Bellinger 
1814b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1815b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1816b8d26b3bSNicholas Bellinger 
1817b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
181803e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1819b8d26b3bSNicholas Bellinger 		break;
1820b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1821b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1822b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1823b8d26b3bSNicholas Bellinger 
1824b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
182503e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
18263df8f68aSNicholas Bellinger 		break;
1827b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1828b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
18299d49f5e2SSagi Grimberg 
18309d49f5e2SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1831b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1832b8d26b3bSNicholas Bellinger 		break;
1833adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1834adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1835adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
183603e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1837adb54c29SNicholas Bellinger 		break;
1838b8d26b3bSNicholas Bellinger 	default:
1839b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1840b8d26b3bSNicholas Bellinger 		dump_stack();
1841b8d26b3bSNicholas Bellinger 		break;
1842b8d26b3bSNicholas Bellinger 	}
1843b8d26b3bSNicholas Bellinger }
1844b8d26b3bSNicholas Bellinger 
1845b8d26b3bSNicholas Bellinger static void
1846b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1847b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1848b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1849b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1850b8d26b3bSNicholas Bellinger {
1851d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1852b6b87a1dSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1853b8d26b3bSNicholas Bellinger 
1854b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
18553df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1856adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1857adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1858b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1859b8d26b3bSNicholas Bellinger 
1860b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1861b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1862b8d26b3bSNicholas Bellinger 		return;
1863b8d26b3bSNicholas Bellinger 	}
1864897bb2c9SSagi Grimberg 
1865897bb2c9SSagi Grimberg 	/**
1866897bb2c9SSagi Grimberg 	 * If send_wr_num is 0 this means that we got
1867897bb2c9SSagi Grimberg 	 * RDMA completion and we cleared it and we should
1868897bb2c9SSagi Grimberg 	 * simply decrement the response post. else the
1869897bb2c9SSagi Grimberg 	 * response is incorporated in send_wr_num, just
1870897bb2c9SSagi Grimberg 	 * sub it.
1871897bb2c9SSagi Grimberg 	 **/
1872897bb2c9SSagi Grimberg 	if (wr->send_wr_num)
1873897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1874897bb2c9SSagi Grimberg 	else
1875897bb2c9SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1876b8d26b3bSNicholas Bellinger 
1877b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
187803e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1879b8d26b3bSNicholas Bellinger }
1880b8d26b3bSNicholas Bellinger 
1881b8d26b3bSNicholas Bellinger static void
188295b60f07SNicholas Bellinger __isert_send_completion(struct iser_tx_desc *tx_desc,
1883b8d26b3bSNicholas Bellinger 		        struct isert_conn *isert_conn)
1884b8d26b3bSNicholas Bellinger {
1885b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1886b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1887b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1888b8d26b3bSNicholas Bellinger 
1889b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1890b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1891b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1892b8d26b3bSNicholas Bellinger 		return;
1893b8d26b3bSNicholas Bellinger 	}
1894b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1895b8d26b3bSNicholas Bellinger 
1896b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1897b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
1898b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1899b8d26b3bSNicholas Bellinger 		dump_stack();
1900b8d26b3bSNicholas Bellinger 		break;
1901b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1902b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1903b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1904b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1905b8d26b3bSNicholas Bellinger 		break;
1906b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1907f93f3a70SSagi Grimberg 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1908897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1909f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
1910b8d26b3bSNicholas Bellinger 		break;
1911b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1912b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1913b8d26b3bSNicholas Bellinger 
1914b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1915b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1916b8d26b3bSNicholas Bellinger 		break;
1917b8d26b3bSNicholas Bellinger 	default:
1918b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1919b8d26b3bSNicholas Bellinger 		dump_stack();
1920b8d26b3bSNicholas Bellinger 		break;
1921b8d26b3bSNicholas Bellinger 	}
1922b8d26b3bSNicholas Bellinger }
1923b8d26b3bSNicholas Bellinger 
1924b8d26b3bSNicholas Bellinger static void
192595b60f07SNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
192695b60f07SNicholas Bellinger 		      struct isert_conn *isert_conn)
192795b60f07SNicholas Bellinger {
192895b60f07SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
192995b60f07SNicholas Bellinger 	struct iser_tx_desc *t;
193095b60f07SNicholas Bellinger 	/*
193195b60f07SNicholas Bellinger 	 * Drain coalesced completion llist starting from comp_llnode_batch
193295b60f07SNicholas Bellinger 	 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
193395b60f07SNicholas Bellinger 	 */
193495b60f07SNicholas Bellinger 	while (llnode) {
193595b60f07SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
193695b60f07SNicholas Bellinger 		llnode = llist_next(llnode);
193795b60f07SNicholas Bellinger 		__isert_send_completion(t, isert_conn);
193895b60f07SNicholas Bellinger 	}
193995b60f07SNicholas Bellinger 	__isert_send_completion(tx_desc, isert_conn);
194095b60f07SNicholas Bellinger }
194195b60f07SNicholas Bellinger 
194295b60f07SNicholas Bellinger static void
1943ebbe4421SNicholas Bellinger isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1944ebbe4421SNicholas Bellinger {
1945ebbe4421SNicholas Bellinger 	struct llist_node *llnode;
1946ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
1947ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
1948ebbe4421SNicholas Bellinger 
1949ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
1950ebbe4421SNicholas Bellinger 	llnode = llist_del_all(&isert_conn->conn_comp_llist);
1951ebbe4421SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
1952ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
1953ebbe4421SNicholas Bellinger 
1954ebbe4421SNicholas Bellinger 	while (llnode) {
1955ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1956ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
1957ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
1958ebbe4421SNicholas Bellinger 
1959897bb2c9SSagi Grimberg 		/**
1960897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
1961897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
1962897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
1963897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
1964897bb2c9SSagi Grimberg 		 * sub it.
1965897bb2c9SSagi Grimberg 		 **/
1966897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
1967897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
1968897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
1969897bb2c9SSagi Grimberg 		else
1970897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
197103e7848aSNicholas Bellinger 
197203e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
1973ebbe4421SNicholas Bellinger 	}
1974ebbe4421SNicholas Bellinger }
1975ebbe4421SNicholas Bellinger 
1976ebbe4421SNicholas Bellinger static void
1977defd8848SNicholas Bellinger isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1978b8d26b3bSNicholas Bellinger {
1979b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1980b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1981ebbe4421SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
1982ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
1983ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
1984ebbe4421SNicholas Bellinger 
1985ebbe4421SNicholas Bellinger 	while (llnode) {
1986ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1987ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
1988ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
1989ebbe4421SNicholas Bellinger 
1990897bb2c9SSagi Grimberg 		/**
1991897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
1992897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
1993897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
1994897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
1995897bb2c9SSagi Grimberg 		 * sub it.
1996897bb2c9SSagi Grimberg 		 **/
1997897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
1998897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
1999897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
2000897bb2c9SSagi Grimberg 		else
2001897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
200203e7848aSNicholas Bellinger 
200303e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
2004ebbe4421SNicholas Bellinger 	}
2005ebbe4421SNicholas Bellinger 	tx_desc->comp_llnode_batch = NULL;
2006b8d26b3bSNicholas Bellinger 
2007b8d26b3bSNicholas Bellinger 	if (!isert_cmd)
2008b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
2009b8d26b3bSNicholas Bellinger 	else
201003e7848aSNicholas Bellinger 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
2011b8d26b3bSNicholas Bellinger }
2012b8d26b3bSNicholas Bellinger 
2013defd8848SNicholas Bellinger static void
2014defd8848SNicholas Bellinger isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2015defd8848SNicholas Bellinger {
2016defd8848SNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2017defd8848SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
2018defd8848SNicholas Bellinger 
2019defd8848SNicholas Bellinger 	if (isert_conn->post_recv_buf_count)
2020defd8848SNicholas Bellinger 		return;
2021defd8848SNicholas Bellinger 
2022ebbe4421SNicholas Bellinger 	isert_cq_drain_comp_llist(isert_conn, ib_dev);
2023ebbe4421SNicholas Bellinger 
2024defd8848SNicholas Bellinger 	if (conn->sess) {
2025defd8848SNicholas Bellinger 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2026defd8848SNicholas Bellinger 		target_wait_for_sess_cmds(conn->sess->se_sess);
2027defd8848SNicholas Bellinger 	}
2028defd8848SNicholas Bellinger 
2029defd8848SNicholas Bellinger 	while (atomic_read(&isert_conn->post_send_buf_count))
2030defd8848SNicholas Bellinger 		msleep(3000);
2031b8d26b3bSNicholas Bellinger 
2032b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2033defd8848SNicholas Bellinger 	isert_conn->state = ISER_CONN_DOWN;
2034b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2035b2cb9649SNicholas Bellinger 
20369d49f5e2SSagi Grimberg 	iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
20379d49f5e2SSagi Grimberg 
2038defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait_comp_err);
2039b8d26b3bSNicholas Bellinger }
2040b8d26b3bSNicholas Bellinger 
2041b8d26b3bSNicholas Bellinger static void
2042b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
2043b8d26b3bSNicholas Bellinger {
2044b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2045b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
2046b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2047b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2048b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2049b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2050b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
2051b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2052b8d26b3bSNicholas Bellinger 
2053b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2054b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2055b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2056b8d26b3bSNicholas Bellinger 
2057b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2058b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
2059b8d26b3bSNicholas Bellinger 		} else {
2060b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2061b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
2062c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
2063defd8848SNicholas Bellinger 
20649bb4ca68SNicholas Bellinger 			if (wc.wr_id != ISER_FASTREG_LI_WRID) {
2065ebbe4421SNicholas Bellinger 				if (tx_desc->llnode_active)
2066ebbe4421SNicholas Bellinger 					continue;
2067ebbe4421SNicholas Bellinger 
2068b8d26b3bSNicholas Bellinger 				atomic_dec(&isert_conn->post_send_buf_count);
2069defd8848SNicholas Bellinger 				isert_cq_tx_comp_err(tx_desc, isert_conn);
2070b8d26b3bSNicholas Bellinger 			}
2071b8d26b3bSNicholas Bellinger 		}
20729bb4ca68SNicholas Bellinger 	}
2073b8d26b3bSNicholas Bellinger 
2074b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2075b8d26b3bSNicholas Bellinger }
2076b8d26b3bSNicholas Bellinger 
2077b8d26b3bSNicholas Bellinger static void
2078b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
2079b8d26b3bSNicholas Bellinger {
2080b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2081b8d26b3bSNicholas Bellinger 
2082b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2083b8d26b3bSNicholas Bellinger }
2084b8d26b3bSNicholas Bellinger 
2085b8d26b3bSNicholas Bellinger static void
2086b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
2087b8d26b3bSNicholas Bellinger {
2088b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2089b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
2090b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2091b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2092b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2093b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2094b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
2095b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2096b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
2097b8d26b3bSNicholas Bellinger 
2098b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2099b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2100b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2101b8d26b3bSNicholas Bellinger 
2102b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2103b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
2104b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
2105b8d26b3bSNicholas Bellinger 		} else {
2106b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2107c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
2108b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
2109c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
2110c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
2111c5a2adbfSNicholas Bellinger 			}
2112b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
2113defd8848SNicholas Bellinger 			isert_cq_rx_comp_err(isert_conn);
2114b8d26b3bSNicholas Bellinger 		}
2115b8d26b3bSNicholas Bellinger 	}
2116b8d26b3bSNicholas Bellinger 
2117b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2118b8d26b3bSNicholas Bellinger }
2119b8d26b3bSNicholas Bellinger 
2120b8d26b3bSNicholas Bellinger static void
2121b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
2122b8d26b3bSNicholas Bellinger {
2123b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2124b8d26b3bSNicholas Bellinger 
2125b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2126b8d26b3bSNicholas Bellinger }
2127b8d26b3bSNicholas Bellinger 
2128b8d26b3bSNicholas Bellinger static int
2129b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2130b8d26b3bSNicholas Bellinger {
2131b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2132b8d26b3bSNicholas Bellinger 	int ret;
2133b8d26b3bSNicholas Bellinger 
2134b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
2135b8d26b3bSNicholas Bellinger 
2136b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2137b8d26b3bSNicholas Bellinger 			   &wr_failed);
2138b8d26b3bSNicholas Bellinger 	if (ret) {
2139b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
2140b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2141b8d26b3bSNicholas Bellinger 		return ret;
2142b8d26b3bSNicholas Bellinger 	}
2143b8d26b3bSNicholas Bellinger 	return ret;
2144b8d26b3bSNicholas Bellinger }
2145b8d26b3bSNicholas Bellinger 
2146b8d26b3bSNicholas Bellinger static int
2147b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2148b8d26b3bSNicholas Bellinger {
2149d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2150b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2151b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2152b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2153b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2154b8d26b3bSNicholas Bellinger 
2155b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2156b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2157b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2158b8d26b3bSNicholas Bellinger 	/*
2159b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2160b8d26b3bSNicholas Bellinger 	 */
2161b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2162b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2163b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2164b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2165b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2166dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2167b8d26b3bSNicholas Bellinger 
2168b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2169b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2170b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2171b8d26b3bSNicholas Bellinger 
2172b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2173b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2174dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2175b8d26b3bSNicholas Bellinger 
2176dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2177dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2178b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2179b8d26b3bSNicholas Bellinger 
2180dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2181dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2182dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
2183b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2184b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2185b8d26b3bSNicholas Bellinger 	}
2186b8d26b3bSNicholas Bellinger 
218795b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
2188b8d26b3bSNicholas Bellinger 
2189b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2190b8d26b3bSNicholas Bellinger 
2191b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2192b8d26b3bSNicholas Bellinger }
2193b8d26b3bSNicholas Bellinger 
2194131e6abcSNicholas Bellinger static void
2195131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2196131e6abcSNicholas Bellinger {
2197131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2198131e6abcSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2199131e6abcSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2200131e6abcSNicholas Bellinger 
2201131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2202131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2203131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2204131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2205131e6abcSNicholas Bellinger 
2206131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2207131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2208131e6abcSNicholas Bellinger 
2209131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2210131e6abcSNicholas Bellinger }
2211131e6abcSNicholas Bellinger 
2212e70beee7SNicholas Bellinger static enum target_prot_op
2213e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2214e70beee7SNicholas Bellinger {
2215e70beee7SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2216e70beee7SNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2217e70beee7SNicholas Bellinger 
2218e70beee7SNicholas Bellinger 	if (device->pi_capable)
2219e70beee7SNicholas Bellinger 		return TARGET_PROT_ALL;
2220e70beee7SNicholas Bellinger 
2221e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2222e70beee7SNicholas Bellinger }
2223e70beee7SNicholas Bellinger 
2224b8d26b3bSNicholas Bellinger static int
2225b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2226b8d26b3bSNicholas Bellinger 		bool nopout_response)
2227b8d26b3bSNicholas Bellinger {
2228d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2229b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2230b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2231b8d26b3bSNicholas Bellinger 
2232b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2233b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2234b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2235b8d26b3bSNicholas Bellinger 			       nopout_response);
2236b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
223795b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2238b8d26b3bSNicholas Bellinger 
22398b513d0cSMasanari Iida 	pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2240b8d26b3bSNicholas Bellinger 
2241b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2242b8d26b3bSNicholas Bellinger }
2243b8d26b3bSNicholas Bellinger 
2244b8d26b3bSNicholas Bellinger static int
2245b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2246b8d26b3bSNicholas Bellinger {
2247d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2248b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2249b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2250b8d26b3bSNicholas Bellinger 
2251b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2252b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2253b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2254b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
225595b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2256b8d26b3bSNicholas Bellinger 
2257b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2258b8d26b3bSNicholas Bellinger 
2259b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2260b8d26b3bSNicholas Bellinger }
2261b8d26b3bSNicholas Bellinger 
2262b8d26b3bSNicholas Bellinger static int
2263b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2264b8d26b3bSNicholas Bellinger {
2265d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2266b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2267b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2268b8d26b3bSNicholas Bellinger 
2269b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2270b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2271b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2272b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
227395b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2274b8d26b3bSNicholas Bellinger 
2275b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2276b8d26b3bSNicholas Bellinger 
2277b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2278b8d26b3bSNicholas Bellinger }
2279b8d26b3bSNicholas Bellinger 
2280b8d26b3bSNicholas Bellinger static int
2281b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2282b8d26b3bSNicholas Bellinger {
2283d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2284b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2285b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
22863df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
22873df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
22883df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
22893df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2290b8d26b3bSNicholas Bellinger 
2291b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
22923df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2293b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
22943df8f68aSNicholas Bellinger 
22953df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2296dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
22973df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
22983df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2299dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2300dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
23013df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
23023df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
23033df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
23043df8f68aSNicholas Bellinger 
230595b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2306b8d26b3bSNicholas Bellinger 
2307b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2308b8d26b3bSNicholas Bellinger 
2309b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2310b8d26b3bSNicholas Bellinger }
2311b8d26b3bSNicholas Bellinger 
2312b8d26b3bSNicholas Bellinger static int
2313adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2314adb54c29SNicholas Bellinger {
2315d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2316adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2317adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2318adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2319adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2320adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2321adb54c29SNicholas Bellinger 	int rc;
2322adb54c29SNicholas Bellinger 
2323adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
232422c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2325adb54c29SNicholas Bellinger 	if (rc < 0)
2326adb54c29SNicholas Bellinger 		return rc;
2327adb54c29SNicholas Bellinger 
2328adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2329adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2330adb54c29SNicholas Bellinger 
2331adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2332adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2333adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2334adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2335adb54c29SNicholas Bellinger 
2336adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2337adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2338adb54c29SNicholas Bellinger 
2339adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2340adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2341adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
2342adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2343adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2344adb54c29SNicholas Bellinger 	}
234595b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2346adb54c29SNicholas Bellinger 
2347adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2348adb54c29SNicholas Bellinger 
2349adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2350adb54c29SNicholas Bellinger }
2351adb54c29SNicholas Bellinger 
2352adb54c29SNicholas Bellinger static int
2353b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2354b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2355b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2356b8d26b3bSNicholas Bellinger {
2357d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2358b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2359b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2360b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2361b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2362b8d26b3bSNicholas Bellinger 
2363b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2364b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2365b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2366b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2367b8d26b3bSNicholas Bellinger 
2368b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2369b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2370b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2371b8d26b3bSNicholas Bellinger 	/*
2372b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2373b8d26b3bSNicholas Bellinger 	 */
2374b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2375b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2376b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
2377b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
2378b8d26b3bSNicholas Bellinger 
2379b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2380b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2381b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2382b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2383b8d26b3bSNicholas Bellinger 
238490ecc6e2SVu Pham 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
238590ecc6e2SVu Pham 			 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2386b8d26b3bSNicholas Bellinger 		page_off = 0;
2387b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2388b8d26b3bSNicholas Bellinger 		ib_sge++;
2389b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2390b8d26b3bSNicholas Bellinger 	}
2391b8d26b3bSNicholas Bellinger 
2392b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2393b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
2394b8d26b3bSNicholas Bellinger 
2395b8d26b3bSNicholas Bellinger 	return sg_nents;
2396b8d26b3bSNicholas Bellinger }
2397b8d26b3bSNicholas Bellinger 
2398b8d26b3bSNicholas Bellinger static int
239990ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
240090ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2401b8d26b3bSNicholas Bellinger {
2402b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2403d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2404b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2405e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
240690ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2407b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2408e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2409e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2410b8d26b3bSNicholas Bellinger 
241190ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2412b8d26b3bSNicholas Bellinger 
2413e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2414e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2415e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2416e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2417e3d7e4c3SSagi Grimberg 	if (ret)
2418e3d7e4c3SSagi Grimberg 		return ret;
2419b8d26b3bSNicholas Bellinger 
2420e3d7e4c3SSagi Grimberg 	data_left = data->len;
2421e3d7e4c3SSagi Grimberg 	offset = data->offset;
2422b8d26b3bSNicholas Bellinger 
2423e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2424b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
242590ecc6e2SVu Pham 		pr_warn("Unable to allocate ib_sge\n");
2426b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2427e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2428b8d26b3bSNicholas Bellinger 	}
242990ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2430b8d26b3bSNicholas Bellinger 
2431e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2432b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2433b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2434b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
243590ecc6e2SVu Pham 		pr_debug("Unable to allocate wr->send_wr\n");
2436b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2437e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2438b8d26b3bSNicholas Bellinger 	}
2439b8d26b3bSNicholas Bellinger 
2440b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2441b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2442b8d26b3bSNicholas Bellinger 
2443b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2444b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2445b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2446b8d26b3bSNicholas Bellinger 
2447b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
244890ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
244990ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2450b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2451b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
245290ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
245390ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
245490ecc6e2SVu Pham 			else
245590ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
245690ecc6e2SVu Pham 		} else {
245790ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
245890ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
245990ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
246090ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
246190ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
246290ecc6e2SVu Pham 			else
246390ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
246490ecc6e2SVu Pham 		}
2465b8d26b3bSNicholas Bellinger 
2466b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2467b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2468b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2469b8d26b3bSNicholas Bellinger 
2470b8d26b3bSNicholas Bellinger 		offset += data_len;
247190ecc6e2SVu Pham 		va_offset += data_len;
2472b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2473b8d26b3bSNicholas Bellinger 	}
247490ecc6e2SVu Pham 
247590ecc6e2SVu Pham 	return 0;
2476e3d7e4c3SSagi Grimberg unmap_cmd:
2477e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2478e3d7e4c3SSagi Grimberg 
247990ecc6e2SVu Pham 	return ret;
248090ecc6e2SVu Pham }
248190ecc6e2SVu Pham 
248290ecc6e2SVu Pham static int
248359464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
248459464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
248559464ef4SVu Pham {
248659464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
248759464ef4SVu Pham 	struct scatterlist *tmp_sg;
248859464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
248959464ef4SVu Pham 
249059464ef4SVu Pham 	n_pages = 0;
249159464ef4SVu Pham 	new_chunk = 1;
249259464ef4SVu Pham 	last_ent = sg_nents - 1;
249359464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
249459464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
249559464ef4SVu Pham 		if (new_chunk)
249659464ef4SVu Pham 			chunk_start = start_addr;
249759464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
249859464ef4SVu Pham 
249959464ef4SVu Pham 		pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
250059464ef4SVu Pham 			 i, (unsigned long long)tmp_sg->dma_address,
250159464ef4SVu Pham 			 tmp_sg->length);
250259464ef4SVu Pham 
250359464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
250459464ef4SVu Pham 			new_chunk = 0;
250559464ef4SVu Pham 			continue;
250659464ef4SVu Pham 		}
250759464ef4SVu Pham 		new_chunk = 1;
250859464ef4SVu Pham 
250959464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
251059464ef4SVu Pham 		do {
251159464ef4SVu Pham 			fr_pl[n_pages++] = page;
251259464ef4SVu Pham 			pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
251359464ef4SVu Pham 				 n_pages - 1, page);
251459464ef4SVu Pham 			page += PAGE_SIZE;
251559464ef4SVu Pham 		} while (page < end_addr);
251659464ef4SVu Pham 	}
251759464ef4SVu Pham 
251859464ef4SVu Pham 	return n_pages;
251959464ef4SVu Pham }
252059464ef4SVu Pham 
252159464ef4SVu Pham static int
2522e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2523e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2524e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
25259e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2526e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
252759464ef4SVu Pham {
252859464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
25299e961ae7SSagi Grimberg 	struct ib_mr *mr;
25309e961ae7SSagi Grimberg 	struct ib_fast_reg_page_list *frpl;
253159464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
253259464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
25339bd626e7SSagi Grimberg 	int ret, pagelist_len;
25349bd626e7SSagi Grimberg 	u32 page_off;
253559464ef4SVu Pham 	u8 key;
253659464ef4SVu Pham 
2537e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
2538e3d7e4c3SSagi Grimberg 		sge->lkey = isert_conn->conn_mr->lkey;
2539e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2540e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
25419e961ae7SSagi Grimberg 		pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
25429e961ae7SSagi Grimberg 			 __func__, __LINE__, sge->addr, sge->length,
25439e961ae7SSagi Grimberg 			 sge->lkey);
2544e3d7e4c3SSagi Grimberg 		return 0;
2545e3d7e4c3SSagi Grimberg 	}
2546e3d7e4c3SSagi Grimberg 
25479e961ae7SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID) {
25489e961ae7SSagi Grimberg 		/* Registering data buffer */
25499e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
25509e961ae7SSagi Grimberg 		frpl = fr_desc->data_frpl;
25519e961ae7SSagi Grimberg 	} else {
25529e961ae7SSagi Grimberg 		/* Registering protection buffer */
25539e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
25549e961ae7SSagi Grimberg 		frpl = fr_desc->pi_ctx->prot_frpl;
25559e961ae7SSagi Grimberg 	}
25569e961ae7SSagi Grimberg 
2557e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
255859464ef4SVu Pham 
25599bd626e7SSagi Grimberg 	pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2560e3d7e4c3SSagi Grimberg 		 fr_desc, mem->nents, mem->offset);
256159464ef4SVu Pham 
2562e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
25639e961ae7SSagi Grimberg 					     &frpl->page_list[0]);
256459464ef4SVu Pham 
2565d3e125daSSagi Grimberg 	if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
256659464ef4SVu Pham 		memset(&inv_wr, 0, sizeof(inv_wr));
25679bb4ca68SNicholas Bellinger 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
256859464ef4SVu Pham 		inv_wr.opcode = IB_WR_LOCAL_INV;
25699e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = mr->rkey;
257059464ef4SVu Pham 		wr = &inv_wr;
257159464ef4SVu Pham 		/* Bump the key */
25729e961ae7SSagi Grimberg 		key = (u8)(mr->rkey & 0x000000FF);
25739e961ae7SSagi Grimberg 		ib_update_fast_reg_key(mr, ++key);
257459464ef4SVu Pham 	}
257559464ef4SVu Pham 
257659464ef4SVu Pham 	/* Prepare FASTREG WR */
257759464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
25789bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
257959464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
25809e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
25819e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.page_list = frpl;
258259464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
258359464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2584e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
25859e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.rkey = mr->rkey;
258659464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
258759464ef4SVu Pham 
258859464ef4SVu Pham 	if (!wr)
258959464ef4SVu Pham 		wr = &fr_wr;
259059464ef4SVu Pham 	else
259159464ef4SVu Pham 		wr->next = &fr_wr;
259259464ef4SVu Pham 
259359464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
259459464ef4SVu Pham 	if (ret) {
259559464ef4SVu Pham 		pr_err("fast registration failed, ret:%d\n", ret);
259659464ef4SVu Pham 		return ret;
259759464ef4SVu Pham 	}
25989e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
259959464ef4SVu Pham 
26009e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
26019e961ae7SSagi Grimberg 	sge->addr = frpl->page_list[0] + page_off;
2602e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
260359464ef4SVu Pham 
26049e961ae7SSagi Grimberg 	pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
26059e961ae7SSagi Grimberg 		 __func__, __LINE__, sge->addr, sge->length,
26069e961ae7SSagi Grimberg 		 sge->lkey);
260759464ef4SVu Pham 
260859464ef4SVu Pham 	return ret;
260959464ef4SVu Pham }
261059464ef4SVu Pham 
26119e961ae7SSagi Grimberg static inline enum ib_t10_dif_type
26129e961ae7SSagi Grimberg se2ib_prot_type(enum target_prot_type prot_type)
26139e961ae7SSagi Grimberg {
26149e961ae7SSagi Grimberg 	switch (prot_type) {
26159e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE0_PROT:
26169e961ae7SSagi Grimberg 		return IB_T10DIF_NONE;
26179e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE1_PROT:
26189e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE1;
26199e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE2_PROT:
26209e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE2;
26219e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE3_PROT:
26229e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE3;
26239e961ae7SSagi Grimberg 	default:
26249e961ae7SSagi Grimberg 		return IB_T10DIF_NONE;
26259e961ae7SSagi Grimberg 	}
26269e961ae7SSagi Grimberg }
26279e961ae7SSagi Grimberg 
26289e961ae7SSagi Grimberg static int
26299e961ae7SSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
26309e961ae7SSagi Grimberg {
26319e961ae7SSagi Grimberg 	enum ib_t10_dif_type ib_prot_type = se2ib_prot_type(se_cmd->prot_type);
26329e961ae7SSagi Grimberg 
26339e961ae7SSagi Grimberg 	sig_attrs->mem.sig_type = IB_SIG_TYPE_T10_DIF;
26349e961ae7SSagi Grimberg 	sig_attrs->wire.sig_type = IB_SIG_TYPE_T10_DIF;
26359e961ae7SSagi Grimberg 	sig_attrs->mem.sig.dif.pi_interval =
26369e961ae7SSagi Grimberg 				se_cmd->se_dev->dev_attrib.block_size;
26379e961ae7SSagi Grimberg 	sig_attrs->wire.sig.dif.pi_interval =
26389e961ae7SSagi Grimberg 				se_cmd->se_dev->dev_attrib.block_size;
26399e961ae7SSagi Grimberg 
26409e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
26419e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
26429e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
26439e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = IB_T10DIF_NONE;
26449e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = ib_prot_type;
26459e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
26469e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
26479e961ae7SSagi Grimberg 		break;
26489e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
26499e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
26509e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = ib_prot_type;
26519e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
26529e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
26539e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = IB_T10DIF_NONE;
26549e961ae7SSagi Grimberg 		break;
26559e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
26569e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
26579e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = ib_prot_type;
26589e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
26599e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
26609e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = ib_prot_type;
26619e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
26629e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
26639e961ae7SSagi Grimberg 		break;
26649e961ae7SSagi Grimberg 	default:
26659e961ae7SSagi Grimberg 		pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
26669e961ae7SSagi Grimberg 		return -EINVAL;
26679e961ae7SSagi Grimberg 	}
26689e961ae7SSagi Grimberg 
26699e961ae7SSagi Grimberg 	return 0;
26709e961ae7SSagi Grimberg }
26719e961ae7SSagi Grimberg 
26729e961ae7SSagi Grimberg static inline u8
26739e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
26749e961ae7SSagi Grimberg {
26759e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
26769e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
26779e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
26789e961ae7SSagi Grimberg }
26799e961ae7SSagi Grimberg 
26809e961ae7SSagi Grimberg static int
26819e961ae7SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
26829e961ae7SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc,
26839e961ae7SSagi Grimberg 		 struct ib_sge *data_sge, struct ib_sge *prot_sge,
26849e961ae7SSagi Grimberg 		 struct ib_sge *sig_sge)
26859e961ae7SSagi Grimberg {
26869e961ae7SSagi Grimberg 	struct ib_send_wr sig_wr, inv_wr;
26879e961ae7SSagi Grimberg 	struct ib_send_wr *bad_wr, *wr = NULL;
26889e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
26899e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
26909e961ae7SSagi Grimberg 	int ret;
26919e961ae7SSagi Grimberg 	u32 key;
26929e961ae7SSagi Grimberg 
26939e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
26949e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
26959e961ae7SSagi Grimberg 	if (ret)
26969e961ae7SSagi Grimberg 		goto err;
26979e961ae7SSagi Grimberg 
26989e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
26999e961ae7SSagi Grimberg 
27009e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
27019e961ae7SSagi Grimberg 		memset(&inv_wr, 0, sizeof(inv_wr));
27029e961ae7SSagi Grimberg 		inv_wr.opcode = IB_WR_LOCAL_INV;
2703c2caa207SSagi Grimberg 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
27049e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
27059e961ae7SSagi Grimberg 		wr = &inv_wr;
27069e961ae7SSagi Grimberg 		/* Bump the key */
27079e961ae7SSagi Grimberg 		key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
27089e961ae7SSagi Grimberg 		ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
27099e961ae7SSagi Grimberg 	}
27109e961ae7SSagi Grimberg 
27119e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
27129e961ae7SSagi Grimberg 	sig_wr.opcode = IB_WR_REG_SIG_MR;
2713c2caa207SSagi Grimberg 	sig_wr.wr_id = ISER_FASTREG_LI_WRID;
27149e961ae7SSagi Grimberg 	sig_wr.sg_list = data_sge;
27159e961ae7SSagi Grimberg 	sig_wr.num_sge = 1;
27169e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
27179e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
27189e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
27199e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
27209e961ae7SSagi Grimberg 		sig_wr.wr.sig_handover.prot = prot_sge;
27219e961ae7SSagi Grimberg 
27229e961ae7SSagi Grimberg 	if (!wr)
27239e961ae7SSagi Grimberg 		wr = &sig_wr;
27249e961ae7SSagi Grimberg 	else
27259e961ae7SSagi Grimberg 		wr->next = &sig_wr;
27269e961ae7SSagi Grimberg 
27279e961ae7SSagi Grimberg 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
27289e961ae7SSagi Grimberg 	if (ret) {
27299e961ae7SSagi Grimberg 		pr_err("fast registration failed, ret:%d\n", ret);
27309e961ae7SSagi Grimberg 		goto err;
27319e961ae7SSagi Grimberg 	}
27329e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
27339e961ae7SSagi Grimberg 
27349e961ae7SSagi Grimberg 	sig_sge->lkey = pi_ctx->sig_mr->lkey;
27359e961ae7SSagi Grimberg 	sig_sge->addr = 0;
27369e961ae7SSagi Grimberg 	sig_sge->length = se_cmd->data_length;
27379e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
27389e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
27399e961ae7SSagi Grimberg 		/*
27409e961ae7SSagi Grimberg 		 * We have protection guards on the wire
27419e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
27429e961ae7SSagi Grimberg 		 */
27439e961ae7SSagi Grimberg 		sig_sge->length += se_cmd->prot_length;
27449e961ae7SSagi Grimberg 
27459e961ae7SSagi Grimberg 	pr_debug("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
27469e961ae7SSagi Grimberg 		 sig_sge->addr, sig_sge->length,
27479e961ae7SSagi Grimberg 		 sig_sge->lkey);
27489e961ae7SSagi Grimberg err:
27499e961ae7SSagi Grimberg 	return ret;
27509e961ae7SSagi Grimberg }
27519e961ae7SSagi Grimberg 
275259464ef4SVu Pham static int
2753a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
275459464ef4SVu Pham 	       struct isert_rdma_wr *wr)
275559464ef4SVu Pham {
275659464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
275759464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2758e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
27599e961ae7SSagi Grimberg 	struct ib_sge data_sge;
276059464ef4SVu Pham 	struct ib_send_wr *send_wr;
2761e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2762e3d7e4c3SSagi Grimberg 	u32 offset;
2763e3d7e4c3SSagi Grimberg 	int ret = 0;
276459464ef4SVu Pham 	unsigned long flags;
276559464ef4SVu Pham 
276659464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2767e3d7e4c3SSagi Grimberg 
2768e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2769e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2770e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2771e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2772e3d7e4c3SSagi Grimberg 	if (ret)
2773e3d7e4c3SSagi Grimberg 		return ret;
2774e3d7e4c3SSagi Grimberg 
27759e961ae7SSagi Grimberg 	if (wr->data.dma_nents != 1 ||
27769e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_NORMAL) {
2777e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2778e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2779e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2780e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2781e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2782e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
278359464ef4SVu Pham 	}
278459464ef4SVu Pham 
27859e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
27869e961ae7SSagi Grimberg 				ISERT_DATA_KEY_VALID, &data_sge);
2787e3d7e4c3SSagi Grimberg 	if (ret)
2788e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
278959464ef4SVu Pham 
27909e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
27919e961ae7SSagi Grimberg 		struct ib_sge prot_sge, sig_sge;
27929e961ae7SSagi Grimberg 
27939e961ae7SSagi Grimberg 		if (se_cmd->t_prot_sg) {
27949e961ae7SSagi Grimberg 			ret = isert_map_data_buf(isert_conn, isert_cmd,
27959e961ae7SSagi Grimberg 						 se_cmd->t_prot_sg,
27969e961ae7SSagi Grimberg 						 se_cmd->t_prot_nents,
27979e961ae7SSagi Grimberg 						 se_cmd->prot_length,
27989e961ae7SSagi Grimberg 						 0, wr->iser_ib_op, &wr->prot);
27999e961ae7SSagi Grimberg 			if (ret)
28009e961ae7SSagi Grimberg 				goto unmap_cmd;
28019e961ae7SSagi Grimberg 
28029e961ae7SSagi Grimberg 			ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
28039e961ae7SSagi Grimberg 						ISERT_PROT_KEY_VALID, &prot_sge);
28049e961ae7SSagi Grimberg 			if (ret)
28059e961ae7SSagi Grimberg 				goto unmap_prot_cmd;
28069e961ae7SSagi Grimberg 		}
28079e961ae7SSagi Grimberg 
28089e961ae7SSagi Grimberg 		ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
28099e961ae7SSagi Grimberg 				       &data_sge, &prot_sge, &sig_sge);
28109e961ae7SSagi Grimberg 		if (ret)
28119e961ae7SSagi Grimberg 			goto unmap_prot_cmd;
28129e961ae7SSagi Grimberg 
28139e961ae7SSagi Grimberg 		fr_desc->ind |= ISERT_PROTECTED;
28149e961ae7SSagi Grimberg 		memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
28159e961ae7SSagi Grimberg 	} else
28169e961ae7SSagi Grimberg 		memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
28179e961ae7SSagi Grimberg 
2818e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
281959464ef4SVu Pham 	wr->send_wr_num = 1;
282059464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
282159464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
282259464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
282359464ef4SVu Pham 
282459464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2825e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
282659464ef4SVu Pham 	send_wr->num_sge = 1;
282759464ef4SVu Pham 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
282859464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
282959464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
283059464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
283159464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
28329e961ae7SSagi Grimberg 		send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
28339e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
283459464ef4SVu Pham 	} else {
283559464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
283659464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
283759464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
283859464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
283959464ef4SVu Pham 	}
284059464ef4SVu Pham 
284159464ef4SVu Pham 	return 0;
28429e961ae7SSagi Grimberg unmap_prot_cmd:
28439e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
28449e961ae7SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2845e3d7e4c3SSagi Grimberg unmap_cmd:
2846e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2847e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2848e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2849e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2850e3d7e4c3SSagi Grimberg 	}
2851e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
285259464ef4SVu Pham 
285359464ef4SVu Pham 	return ret;
285459464ef4SVu Pham }
285559464ef4SVu Pham 
285659464ef4SVu Pham static int
285790ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
285890ecc6e2SVu Pham {
285990ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
286059464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
286190ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
286290ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2863d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
286490ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
286590ecc6e2SVu Pham 	int rc;
286690ecc6e2SVu Pham 
286790ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
286890ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
286990ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2870d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
287190ecc6e2SVu Pham 	if (rc) {
287290ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
287390ecc6e2SVu Pham 		return rc;
287490ecc6e2SVu Pham 	}
287590ecc6e2SVu Pham 
28769e961ae7SSagi Grimberg 	if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2877b8d26b3bSNicholas Bellinger 		/*
2878b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2879b8d26b3bSNicholas Bellinger 		 */
28809e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
28819e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
288204d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2883b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2884b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
288595b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
288695b60f07SNicholas Bellinger 				   &isert_cmd->tx_desc.send_wr, true);
28879e961ae7SSagi Grimberg 		isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
2888897bb2c9SSagi Grimberg 		wr->send_wr_num += 1;
28899e961ae7SSagi Grimberg 	}
2890b8d26b3bSNicholas Bellinger 
2891897bb2c9SSagi Grimberg 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2892b8d26b3bSNicholas Bellinger 
2893b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2894b8d26b3bSNicholas Bellinger 	if (rc) {
2895b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2896897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2897b8d26b3bSNicholas Bellinger 	}
28989e961ae7SSagi Grimberg 
28999e961ae7SSagi Grimberg 	if (se_cmd->prot_op == TARGET_PROT_NORMAL)
29009e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
29019e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
29029e961ae7SSagi Grimberg 	else
29039e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
290490ecc6e2SVu Pham 			 isert_cmd);
2905b8d26b3bSNicholas Bellinger 
290690ecc6e2SVu Pham 	return 1;
2907b8d26b3bSNicholas Bellinger }
2908b8d26b3bSNicholas Bellinger 
2909b8d26b3bSNicholas Bellinger static int
2910b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2911b8d26b3bSNicholas Bellinger {
2912b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2913d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2914b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2915b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2916d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
291790ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
291890ecc6e2SVu Pham 	int rc;
2919b8d26b3bSNicholas Bellinger 
292090ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
292190ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2922b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2923d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
292490ecc6e2SVu Pham 	if (rc) {
292590ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
292690ecc6e2SVu Pham 		return rc;
2927b8d26b3bSNicholas Bellinger 	}
2928b8d26b3bSNicholas Bellinger 
2929b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2930b8d26b3bSNicholas Bellinger 
2931b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2932b8d26b3bSNicholas Bellinger 	if (rc) {
2933b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2934b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2935b8d26b3bSNicholas Bellinger 	}
293690ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
293790ecc6e2SVu Pham 		 isert_cmd);
2938b8d26b3bSNicholas Bellinger 
293990ecc6e2SVu Pham 	return 0;
2940b8d26b3bSNicholas Bellinger }
2941b8d26b3bSNicholas Bellinger 
2942b8d26b3bSNicholas Bellinger static int
2943b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2944b8d26b3bSNicholas Bellinger {
2945b8d26b3bSNicholas Bellinger 	int ret;
2946b8d26b3bSNicholas Bellinger 
2947b8d26b3bSNicholas Bellinger 	switch (state) {
2948b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2949b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2950b8d26b3bSNicholas Bellinger 		break;
2951b8d26b3bSNicholas Bellinger 	default:
2952b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
2953b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2954b8d26b3bSNicholas Bellinger 		break;
2955b8d26b3bSNicholas Bellinger 	}
2956b8d26b3bSNicholas Bellinger 
2957b8d26b3bSNicholas Bellinger 	return ret;
2958b8d26b3bSNicholas Bellinger }
2959b8d26b3bSNicholas Bellinger 
2960b8d26b3bSNicholas Bellinger static int
2961b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2962b8d26b3bSNicholas Bellinger {
2963b8d26b3bSNicholas Bellinger 	int ret;
2964b8d26b3bSNicholas Bellinger 
2965b8d26b3bSNicholas Bellinger 	switch (state) {
2966b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2967b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2968b8d26b3bSNicholas Bellinger 		if (!ret) {
2969b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
2970b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
2971b8d26b3bSNicholas Bellinger 		}
2972b8d26b3bSNicholas Bellinger 		break;
2973b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2974b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2975b8d26b3bSNicholas Bellinger 		break;
2976b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2977b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2978b8d26b3bSNicholas Bellinger 		break;
2979b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2980b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2981b8d26b3bSNicholas Bellinger 		break;
2982adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2983adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2984adb54c29SNicholas Bellinger 		break;
2985b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2986b8d26b3bSNicholas Bellinger 		/*
2987b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2988b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2989b8d26b3bSNicholas Bellinger 		 */
2990b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2991b8d26b3bSNicholas Bellinger 		break;
2992b8d26b3bSNicholas Bellinger 	default:
2993b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
2994b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2995b8d26b3bSNicholas Bellinger 		break;
2996b8d26b3bSNicholas Bellinger 	}
2997b8d26b3bSNicholas Bellinger 
2998b8d26b3bSNicholas Bellinger 	return ret;
2999b8d26b3bSNicholas Bellinger }
3000b8d26b3bSNicholas Bellinger 
3001b8d26b3bSNicholas Bellinger static int
3002b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
3003b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
3004b8d26b3bSNicholas Bellinger {
3005b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
3006b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
3007b8d26b3bSNicholas Bellinger 	struct sockaddr *sa;
3008b8d26b3bSNicholas Bellinger 	int ret;
3009b8d26b3bSNicholas Bellinger 
3010b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3011b8d26b3bSNicholas Bellinger 	if (!isert_np) {
3012b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
3013b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3014b8d26b3bSNicholas Bellinger 	}
3015531b7bf4SSagi Grimberg 	sema_init(&isert_np->np_sem, 0);
3016b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
3017b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
3018b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
3019b8d26b3bSNicholas Bellinger 
3020b8d26b3bSNicholas Bellinger 	sa = (struct sockaddr *)ksockaddr;
3021b8d26b3bSNicholas Bellinger 	pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
3022b8d26b3bSNicholas Bellinger 	/*
3023b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3024b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3025b8d26b3bSNicholas Bellinger 	 */
3026b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
3027b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
3028b8d26b3bSNicholas Bellinger 
3029b8d26b3bSNicholas Bellinger 	isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3030b8d26b3bSNicholas Bellinger 				IB_QPT_RC);
3031b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3032b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3033b8d26b3bSNicholas Bellinger 		       PTR_ERR(isert_lid));
3034b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3035b8d26b3bSNicholas Bellinger 		goto out;
3036b8d26b3bSNicholas Bellinger 	}
3037b8d26b3bSNicholas Bellinger 
3038b8d26b3bSNicholas Bellinger 	ret = rdma_bind_addr(isert_lid, sa);
3039b8d26b3bSNicholas Bellinger 	if (ret) {
3040b8d26b3bSNicholas Bellinger 		pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3041b8d26b3bSNicholas Bellinger 		goto out_lid;
3042b8d26b3bSNicholas Bellinger 	}
3043b8d26b3bSNicholas Bellinger 
3044b8d26b3bSNicholas Bellinger 	ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3045b8d26b3bSNicholas Bellinger 	if (ret) {
3046b8d26b3bSNicholas Bellinger 		pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3047b8d26b3bSNicholas Bellinger 		goto out_lid;
3048b8d26b3bSNicholas Bellinger 	}
3049b8d26b3bSNicholas Bellinger 
3050b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
3051b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3052b8d26b3bSNicholas Bellinger 	pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3053b8d26b3bSNicholas Bellinger 
3054b8d26b3bSNicholas Bellinger 	return 0;
3055b8d26b3bSNicholas Bellinger 
3056b8d26b3bSNicholas Bellinger out_lid:
3057b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_lid);
3058b8d26b3bSNicholas Bellinger out:
3059b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3060b8d26b3bSNicholas Bellinger 	return ret;
3061b8d26b3bSNicholas Bellinger }
3062b8d26b3bSNicholas Bellinger 
3063b8d26b3bSNicholas Bellinger static int
3064b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3065b8d26b3bSNicholas Bellinger {
3066b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3067b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3068b8d26b3bSNicholas Bellinger 	int ret;
3069b8d26b3bSNicholas Bellinger 
3070b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3071b8d26b3bSNicholas Bellinger 	cp.responder_resources = isert_conn->responder_resources;
3072b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3073b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3074b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3075b8d26b3bSNicholas Bellinger 
3076b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3077b8d26b3bSNicholas Bellinger 
3078b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3079b8d26b3bSNicholas Bellinger 	if (ret) {
3080b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
3081b8d26b3bSNicholas Bellinger 		return ret;
3082b8d26b3bSNicholas Bellinger 	}
3083b8d26b3bSNicholas Bellinger 
3084b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3085b8d26b3bSNicholas Bellinger 
3086b8d26b3bSNicholas Bellinger 	return 0;
3087b8d26b3bSNicholas Bellinger }
3088b8d26b3bSNicholas Bellinger 
3089b8d26b3bSNicholas Bellinger static int
3090b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3091b8d26b3bSNicholas Bellinger {
3092b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3093b8d26b3bSNicholas Bellinger 	int ret;
3094b8d26b3bSNicholas Bellinger 
3095b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
30966faaa85fSNicholas Bellinger 	/*
30976faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
30986faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
30996faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
31006faaa85fSNicholas Bellinger 	 * into a NOP.
31016faaa85fSNicholas Bellinger 	 */
31026faaa85fSNicholas Bellinger 	if (!login->first_request)
31036faaa85fSNicholas Bellinger 		return 0;
3104b8d26b3bSNicholas Bellinger 
3105b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3106b8d26b3bSNicholas Bellinger 	if (ret)
3107b8d26b3bSNicholas Bellinger 		return ret;
3108b8d26b3bSNicholas Bellinger 
3109b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3110b8d26b3bSNicholas Bellinger 	return 0;
3111b8d26b3bSNicholas Bellinger }
3112b8d26b3bSNicholas Bellinger 
3113b8d26b3bSNicholas Bellinger static void
3114b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3115b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3116b8d26b3bSNicholas Bellinger {
3117b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3118b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3119b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
3120b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
3121b8d26b3bSNicholas Bellinger 
3122b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3123b8d26b3bSNicholas Bellinger 
3124b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
3125b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3126b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3127b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3128b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
3129b8d26b3bSNicholas Bellinger 
3130b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3131b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3132b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3133b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
3134b8d26b3bSNicholas Bellinger 	} else {
3135b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3136b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
3137b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3138b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
3139b8d26b3bSNicholas Bellinger 
3140b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3141b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
3142b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3143b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
3144b8d26b3bSNicholas Bellinger 	}
3145b8d26b3bSNicholas Bellinger }
3146b8d26b3bSNicholas Bellinger 
3147b8d26b3bSNicholas Bellinger static int
3148b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3149b8d26b3bSNicholas Bellinger {
3150b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3151b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3152b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
3153b8d26b3bSNicholas Bellinger 
3154b8d26b3bSNicholas Bellinger accept_wait:
3155531b7bf4SSagi Grimberg 	ret = down_interruptible(&isert_np->np_sem);
3156b8d26b3bSNicholas Bellinger 	if (max_accept > 5)
3157b8d26b3bSNicholas Bellinger 		return -ENODEV;
3158b8d26b3bSNicholas Bellinger 
3159b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3160e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3161b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
3162e346ab34SSagi Grimberg 		pr_debug("np_thread_state %d for isert_accept_np\n",
3163e346ab34SSagi Grimberg 			 np->np_thread_state);
3164e346ab34SSagi Grimberg 		/**
3165e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
3166e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
3167e346ab34SSagi Grimberg 		 **/
3168b8d26b3bSNicholas Bellinger 		return -ENODEV;
3169b8d26b3bSNicholas Bellinger 	}
3170b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3171b8d26b3bSNicholas Bellinger 
3172b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3173b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
3174b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
3175b8d26b3bSNicholas Bellinger 		max_accept++;
3176b8d26b3bSNicholas Bellinger 		goto accept_wait;
3177b8d26b3bSNicholas Bellinger 	}
3178b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
3179b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
3180b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
3181b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3182b8d26b3bSNicholas Bellinger 
3183b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3184b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3185b8d26b3bSNicholas Bellinger 	max_accept = 0;
3186b8d26b3bSNicholas Bellinger 
3187b8d26b3bSNicholas Bellinger 	ret = isert_rdma_post_recvl(isert_conn);
3188b8d26b3bSNicholas Bellinger 	if (ret)
3189b8d26b3bSNicholas Bellinger 		return ret;
3190b8d26b3bSNicholas Bellinger 
3191b8d26b3bSNicholas Bellinger 	ret = isert_rdma_accept(isert_conn);
3192b8d26b3bSNicholas Bellinger 	if (ret)
3193b8d26b3bSNicholas Bellinger 		return ret;
3194b8d26b3bSNicholas Bellinger 
3195b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3196b8d26b3bSNicholas Bellinger 
3197b8d26b3bSNicholas Bellinger 	pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3198b8d26b3bSNicholas Bellinger 	return 0;
3199b8d26b3bSNicholas Bellinger }
3200b8d26b3bSNicholas Bellinger 
3201b8d26b3bSNicholas Bellinger static void
3202b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3203b8d26b3bSNicholas Bellinger {
3204b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3205b8d26b3bSNicholas Bellinger 
3206b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_np->np_cm_id);
3207b8d26b3bSNicholas Bellinger 
3208b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3209b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3210b8d26b3bSNicholas Bellinger }
3211b8d26b3bSNicholas Bellinger 
3212defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3213b8d26b3bSNicholas Bellinger {
3214b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3215b8d26b3bSNicholas Bellinger 
3216defd8848SNicholas Bellinger 	pr_debug("isert_wait_conn: Starting \n");
3217b8d26b3bSNicholas Bellinger 
32189d49f5e2SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
32199d49f5e2SSagi Grimberg 	if (isert_conn->conn_cm_id) {
3220defd8848SNicholas Bellinger 		pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
3221b8d26b3bSNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
3222b2cb9649SNicholas Bellinger 	}
3223b8d26b3bSNicholas Bellinger 	/*
3224b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
3225b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3226b8d26b3bSNicholas Bellinger 	 */
3227b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3228b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
3229b2cb9649SNicholas Bellinger 		return;
3230b2cb9649SNicholas Bellinger 	}
3231defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
3232defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
3233b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
3234b2cb9649SNicholas Bellinger 
3235defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait_comp_err);
3236defd8848SNicholas Bellinger 
3237defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait);
3238defd8848SNicholas Bellinger }
3239defd8848SNicholas Bellinger 
3240defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3241defd8848SNicholas Bellinger {
3242defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3243b8d26b3bSNicholas Bellinger 
3244b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3245b8d26b3bSNicholas Bellinger }
3246b8d26b3bSNicholas Bellinger 
3247b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3248b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3249b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3250d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3251b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3252b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3253b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3254b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3255defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3256b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3257b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3258b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3259b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3260b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3261b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3262b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3263b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3264131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3265e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3266b8d26b3bSNicholas Bellinger };
3267b8d26b3bSNicholas Bellinger 
3268b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3269b8d26b3bSNicholas Bellinger {
3270b8d26b3bSNicholas Bellinger 	int ret;
3271b8d26b3bSNicholas Bellinger 
3272b8d26b3bSNicholas Bellinger 	isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3273b8d26b3bSNicholas Bellinger 	if (!isert_rx_wq) {
3274b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_rx_wq\n");
3275b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3276b8d26b3bSNicholas Bellinger 	}
3277b8d26b3bSNicholas Bellinger 
3278b8d26b3bSNicholas Bellinger 	isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3279b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
3280b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_comp_wq\n");
3281b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
3282b8d26b3bSNicholas Bellinger 		goto destroy_rx_wq;
3283b8d26b3bSNicholas Bellinger 	}
3284b8d26b3bSNicholas Bellinger 
3285b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
3286b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3287b8d26b3bSNicholas Bellinger 	return 0;
3288b8d26b3bSNicholas Bellinger 
3289b8d26b3bSNicholas Bellinger destroy_rx_wq:
3290b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3291b8d26b3bSNicholas Bellinger 	return ret;
3292b8d26b3bSNicholas Bellinger }
3293b8d26b3bSNicholas Bellinger 
3294b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3295b8d26b3bSNicholas Bellinger {
3296f5ebec96SSagi Grimberg 	flush_scheduled_work();
3297b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3298b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3299b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
3300b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3301b8d26b3bSNicholas Bellinger }
3302b8d26b3bSNicholas Bellinger 
3303b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3304b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
3305b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3306b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3307b8d26b3bSNicholas Bellinger 
3308b8d26b3bSNicholas Bellinger module_init(isert_init);
3309b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3310