xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 6cc44a6fb46e1ecc1c28125aa8fa34d317aa9ea7)
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);
589b8d26b3bSNicholas Bellinger 	kref_get(&isert_conn->conn_kref);
590b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
59159464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
592f46d6a8aSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
593b8d26b3bSNicholas Bellinger 
594b8d26b3bSNicholas Bellinger 	cma_id->context = isert_conn;
595b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
596b8d26b3bSNicholas Bellinger 	isert_conn->responder_resources = event->param.conn.responder_resources;
597b8d26b3bSNicholas Bellinger 	isert_conn->initiator_depth = event->param.conn.initiator_depth;
598b8d26b3bSNicholas Bellinger 	pr_debug("Using responder_resources: %u initiator_depth: %u\n",
599b8d26b3bSNicholas Bellinger 		 isert_conn->responder_resources, isert_conn->initiator_depth);
600b8d26b3bSNicholas Bellinger 
601b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
602b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
603b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
604b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
605b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
606b8d26b3bSNicholas Bellinger 		goto out;
607b8d26b3bSNicholas Bellinger 	}
608b8d26b3bSNicholas Bellinger 
609b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
610b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
611b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
612b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
613b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
614b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
615b8d26b3bSNicholas Bellinger 
616b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
617b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
618b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
619b8d26b3bSNicholas Bellinger 
620b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
621b8d26b3bSNicholas Bellinger 	if (ret) {
622b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
623b8d26b3bSNicholas Bellinger 		       ret);
624b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
625b8d26b3bSNicholas Bellinger 		goto out_login_buf;
626b8d26b3bSNicholas Bellinger 	}
627b8d26b3bSNicholas Bellinger 
628b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
629b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
630b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
631b8d26b3bSNicholas Bellinger 
632b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
633b8d26b3bSNicholas Bellinger 	if (ret) {
634b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
635b8d26b3bSNicholas Bellinger 		       ret);
636b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
637b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
638b8d26b3bSNicholas Bellinger 	}
639b8d26b3bSNicholas Bellinger 
640b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
641b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
642b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
643b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
644b8d26b3bSNicholas Bellinger 	}
645b8d26b3bSNicholas Bellinger 
646b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
647eb6ab132SSagi Grimberg 	isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
648eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_pd)) {
649eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_pd);
650eb6ab132SSagi Grimberg 		pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
651eb6ab132SSagi Grimberg 		       isert_conn, ret);
652eb6ab132SSagi Grimberg 		goto out_pd;
653eb6ab132SSagi Grimberg 	}
654b8d26b3bSNicholas Bellinger 
655eb6ab132SSagi Grimberg 	isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
656eb6ab132SSagi Grimberg 					   IB_ACCESS_LOCAL_WRITE);
657eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_mr)) {
658eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_mr);
659eb6ab132SSagi Grimberg 		pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
660eb6ab132SSagi Grimberg 		       isert_conn, ret);
661eb6ab132SSagi Grimberg 		goto out_mr;
662eb6ab132SSagi Grimberg 	}
663b8d26b3bSNicholas Bellinger 
66414f4b54fSSagi Grimberg 	pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
665d3e125daSSagi Grimberg 	if (pi_support && !device->pi_capable) {
666d3e125daSSagi Grimberg 		pr_err("Protection information requested but not supported\n");
667d3e125daSSagi Grimberg 		ret = -EINVAL;
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 {
748b8d26b3bSNicholas Bellinger 	return;
749b8d26b3bSNicholas Bellinger }
750b8d26b3bSNicholas Bellinger 
751b8d26b3bSNicholas Bellinger static void
752b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
753b8d26b3bSNicholas Bellinger {
754b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
755b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
756b8d26b3bSNicholas Bellinger 
757b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
758b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
759b8d26b3bSNicholas Bellinger 
760b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
761b8d26b3bSNicholas Bellinger }
762b8d26b3bSNicholas Bellinger 
763b8d26b3bSNicholas Bellinger static void
764b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
765b8d26b3bSNicholas Bellinger {
766b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
767b8d26b3bSNicholas Bellinger }
768b8d26b3bSNicholas Bellinger 
769b8d26b3bSNicholas Bellinger static void
770b8d26b3bSNicholas Bellinger isert_disconnect_work(struct work_struct *work)
771b8d26b3bSNicholas Bellinger {
772b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(work,
773b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_logout_work);
774b8d26b3bSNicholas Bellinger 
775b8d26b3bSNicholas Bellinger 	pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
776b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
777defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
778defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
779b8d26b3bSNicholas Bellinger 
780b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
781b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
782b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
783b2cb9649SNicholas Bellinger 		goto wake_up;
784b8d26b3bSNicholas Bellinger 	}
785b2cb9649SNicholas Bellinger 	if (!isert_conn->conn_cm_id) {
786b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
787b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
788b2cb9649SNicholas Bellinger 		return;
789b2cb9649SNicholas Bellinger 	}
790b2cb9649SNicholas Bellinger 	if (!isert_conn->logout_posted) {
791b2cb9649SNicholas Bellinger 		pr_debug("Calling rdma_disconnect for !logout_posted from"
792b2cb9649SNicholas Bellinger 			 " isert_disconnect_work\n");
793b2cb9649SNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
794b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
795b2cb9649SNicholas Bellinger 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
796b2cb9649SNicholas Bellinger 		goto wake_up;
797b2cb9649SNicholas Bellinger 	}
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
806b8d26b3bSNicholas Bellinger isert_disconnected_handler(struct rdma_cm_id *cma_id)
807b8d26b3bSNicholas Bellinger {
808b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
809b8d26b3bSNicholas Bellinger 
810b8d26b3bSNicholas Bellinger 	INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
811b8d26b3bSNicholas Bellinger 	schedule_work(&isert_conn->conn_logout_work);
812b8d26b3bSNicholas Bellinger }
813b8d26b3bSNicholas Bellinger 
814b8d26b3bSNicholas Bellinger static int
815b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
816b8d26b3bSNicholas Bellinger {
817b8d26b3bSNicholas Bellinger 	int ret = 0;
818b8d26b3bSNicholas Bellinger 
819b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
820b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
821b8d26b3bSNicholas Bellinger 
822b8d26b3bSNicholas Bellinger 	switch (event->event) {
823b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
824b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
825b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
826b8d26b3bSNicholas Bellinger 		break;
827b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
828b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
829b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
830b8d26b3bSNicholas Bellinger 		break;
831b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DISCONNECTED:
832b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
833b8d26b3bSNicholas Bellinger 		isert_disconnected_handler(cma_id);
834b8d26b3bSNicholas Bellinger 		break;
835b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
836b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ADDR_CHANGE:
837b8d26b3bSNicholas Bellinger 		break;
838b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
839b8d26b3bSNicholas Bellinger 	default:
840b8d26b3bSNicholas Bellinger 		pr_err("Unknown 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) {
1057f46d6a8aSNicholas Bellinger 			if (isert_conn->conn_device->use_fastreg) {
1058f46d6a8aSNicholas Bellinger 				u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1059f46d6a8aSNicholas Bellinger 
1060f46d6a8aSNicholas Bellinger 				ret = isert_conn_create_fastreg_pool(isert_conn,
1061f46d6a8aSNicholas Bellinger 								     pi_support);
1062f46d6a8aSNicholas Bellinger 				if (ret) {
1063f46d6a8aSNicholas Bellinger 					pr_err("Conn: %p failed to create"
1064f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1065f46d6a8aSNicholas Bellinger 					return ret;
1066f46d6a8aSNicholas Bellinger 				}
1067f46d6a8aSNicholas Bellinger 			}
1068f46d6a8aSNicholas Bellinger 
1069b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1070b8d26b3bSNicholas Bellinger 			if (ret)
1071b8d26b3bSNicholas Bellinger 				return ret;
1072b8d26b3bSNicholas Bellinger 
1073b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1074b8d26b3bSNicholas Bellinger 			if (ret)
1075b8d26b3bSNicholas Bellinger 				return ret;
1076b8d26b3bSNicholas Bellinger 
1077b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_UP;
1078b8d26b3bSNicholas Bellinger 			goto post_send;
1079b8d26b3bSNicholas Bellinger 		}
1080b8d26b3bSNicholas Bellinger 
1081b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1082b8d26b3bSNicholas Bellinger 		if (ret)
1083b8d26b3bSNicholas Bellinger 			return ret;
1084b8d26b3bSNicholas Bellinger 	}
1085b8d26b3bSNicholas Bellinger post_send:
1086b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1087b8d26b3bSNicholas Bellinger 	if (ret)
1088b8d26b3bSNicholas Bellinger 		return ret;
1089b8d26b3bSNicholas Bellinger 
1090b8d26b3bSNicholas Bellinger 	return 0;
1091b8d26b3bSNicholas Bellinger }
1092b8d26b3bSNicholas Bellinger 
1093b8d26b3bSNicholas Bellinger static void
1094b8d26b3bSNicholas Bellinger isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1095b8d26b3bSNicholas Bellinger 		   struct isert_conn *isert_conn)
1096b8d26b3bSNicholas Bellinger {
1097b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1098b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1099b8d26b3bSNicholas Bellinger 	int size;
1100b8d26b3bSNicholas Bellinger 
1101b8d26b3bSNicholas Bellinger 	if (!login) {
1102b8d26b3bSNicholas Bellinger 		pr_err("conn->conn_login is NULL\n");
1103b8d26b3bSNicholas Bellinger 		dump_stack();
1104b8d26b3bSNicholas Bellinger 		return;
1105b8d26b3bSNicholas Bellinger 	}
1106b8d26b3bSNicholas Bellinger 
1107b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1108b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1109b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1110b8d26b3bSNicholas Bellinger 		/*
1111b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1112b8d26b3bSNicholas Bellinger 		 * login request PDU.
1113b8d26b3bSNicholas Bellinger 		 */
1114b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1115b8d26b3bSNicholas Bellinger 		login->current_stage =
1116b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1117b8d26b3bSNicholas Bellinger 			 >> 2;
1118b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1119b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1120b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1121b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1122b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1123b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1124b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1125b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1126b8d26b3bSNicholas Bellinger 	}
1127b8d26b3bSNicholas Bellinger 
1128b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1129b8d26b3bSNicholas Bellinger 
1130b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1131b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1132b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1133b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1134b8d26b3bSNicholas Bellinger 
11356faaa85fSNicholas Bellinger 	if (login->first_request) {
1136b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
11376faaa85fSNicholas Bellinger 		return;
11386faaa85fSNicholas Bellinger 	}
11396faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1140b8d26b3bSNicholas Bellinger }
1141b8d26b3bSNicholas Bellinger 
1142b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1143676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1144b8d26b3bSNicholas Bellinger {
1145b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1146b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1147d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1148b8d26b3bSNicholas Bellinger 
1149676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1150d703ce2fSNicholas Bellinger 	if (!cmd) {
1151d703ce2fSNicholas Bellinger 		pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1152b8d26b3bSNicholas Bellinger 		return NULL;
1153b8d26b3bSNicholas Bellinger 	}
1154d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1155b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1156d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1157b8d26b3bSNicholas Bellinger 
1158d703ce2fSNicholas Bellinger 	return cmd;
1159b8d26b3bSNicholas Bellinger }
1160b8d26b3bSNicholas Bellinger 
1161b8d26b3bSNicholas Bellinger static int
1162b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1163d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1164d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1165b8d26b3bSNicholas Bellinger {
1166b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1167b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1168b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1169b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1170b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1171b8d26b3bSNicholas Bellinger 
1172b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1173b8d26b3bSNicholas Bellinger 	if (rc < 0)
1174b8d26b3bSNicholas Bellinger 		return rc;
1175b8d26b3bSNicholas Bellinger 
1176b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1177b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1178b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1179b8d26b3bSNicholas Bellinger 
1180b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1181b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1182b8d26b3bSNicholas Bellinger 		return 0;
1183b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1184b8d26b3bSNicholas Bellinger 		dump_payload = true;
1185b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1186b8d26b3bSNicholas Bellinger 	}
1187b8d26b3bSNicholas Bellinger 
1188b8d26b3bSNicholas Bellinger 	if (!imm_data)
1189b8d26b3bSNicholas Bellinger 		return 0;
1190b8d26b3bSNicholas Bellinger 
1191b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1192b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1193b8d26b3bSNicholas Bellinger 
1194b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1195b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1196b8d26b3bSNicholas Bellinger 
1197b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1198b8d26b3bSNicholas Bellinger 
1199b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1200b8d26b3bSNicholas Bellinger 
1201b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1202b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1203b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1204b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1205b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1206b8d26b3bSNicholas Bellinger 	}
1207b8d26b3bSNicholas Bellinger 
1208b8d26b3bSNicholas Bellinger sequence_cmd:
1209561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1210b8d26b3bSNicholas Bellinger 
1211b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1212b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
1213*6cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1214*6cc44a6fSNicholas Bellinger 		target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1215b8d26b3bSNicholas Bellinger 
1216b8d26b3bSNicholas Bellinger 	return 0;
1217b8d26b3bSNicholas Bellinger }
1218b8d26b3bSNicholas Bellinger 
1219b8d26b3bSNicholas Bellinger static int
1220b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1221b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1222b8d26b3bSNicholas Bellinger {
1223b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1224b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1225b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1226b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1227b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1228b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1229b8d26b3bSNicholas Bellinger 
1230b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1231b8d26b3bSNicholas Bellinger 	if (rc < 0)
1232b8d26b3bSNicholas Bellinger 		return rc;
1233b8d26b3bSNicholas Bellinger 	else if (!cmd)
1234b8d26b3bSNicholas Bellinger 		return 0;
1235b8d26b3bSNicholas Bellinger 	/*
1236b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1237b8d26b3bSNicholas Bellinger 	 */
1238b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
1239b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
1240b8d26b3bSNicholas Bellinger 		dump_stack();
1241b8d26b3bSNicholas Bellinger 		return -1;
1242b8d26b3bSNicholas Bellinger 	}
1243b8d26b3bSNicholas Bellinger 
1244b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1245b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1246b8d26b3bSNicholas Bellinger 
1247b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1248b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1249b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1250b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1251b8d26b3bSNicholas Bellinger 	/*
1252b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1253b8d26b3bSNicholas Bellinger 	 */
1254b8d26b3bSNicholas Bellinger 	if (page_off) {
1255b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1256b8d26b3bSNicholas Bellinger 		dump_stack();
1257b8d26b3bSNicholas Bellinger 		return -1;
1258b8d26b3bSNicholas Bellinger 	}
1259b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1260b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1261b8d26b3bSNicholas Bellinger 
1262b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1263b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1264b8d26b3bSNicholas Bellinger 
1265b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1266b8d26b3bSNicholas Bellinger 	if (rc < 0)
1267b8d26b3bSNicholas Bellinger 		return rc;
1268b8d26b3bSNicholas Bellinger 
1269b8d26b3bSNicholas Bellinger 	return 0;
1270b8d26b3bSNicholas Bellinger }
1271b8d26b3bSNicholas Bellinger 
1272b8d26b3bSNicholas Bellinger static int
1273778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1274d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1275d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1276778de368SNicholas Bellinger {
1277778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1278778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1279778de368SNicholas Bellinger 	int rc;
1280778de368SNicholas Bellinger 
1281778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1282778de368SNicholas Bellinger 	if (rc < 0)
1283778de368SNicholas Bellinger 		return rc;
1284778de368SNicholas Bellinger 	/*
1285778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1286778de368SNicholas Bellinger 	 */
1287778de368SNicholas Bellinger 
1288778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1289778de368SNicholas Bellinger }
1290778de368SNicholas Bellinger 
1291778de368SNicholas Bellinger static int
1292adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1293d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1294d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1295adb54c29SNicholas Bellinger {
1296adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1297adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1298adb54c29SNicholas Bellinger 	int rc;
1299adb54c29SNicholas Bellinger 	unsigned char *text_in;
1300adb54c29SNicholas Bellinger 
1301adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1302adb54c29SNicholas Bellinger 	if (rc < 0)
1303adb54c29SNicholas Bellinger 		return rc;
1304adb54c29SNicholas Bellinger 
1305adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1306adb54c29SNicholas Bellinger 	if (!text_in) {
1307adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1308adb54c29SNicholas Bellinger 		       payload_length);
1309adb54c29SNicholas Bellinger 		return -ENOMEM;
1310adb54c29SNicholas Bellinger 	}
1311adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1312adb54c29SNicholas Bellinger 
1313adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1314adb54c29SNicholas Bellinger 
1315adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1316adb54c29SNicholas Bellinger }
1317adb54c29SNicholas Bellinger 
1318adb54c29SNicholas Bellinger static int
1319b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1320b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1321b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1322b8d26b3bSNicholas Bellinger {
1323b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1324b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1325ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1326b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1327b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1328b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1329b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1330b8d26b3bSNicholas Bellinger 
1331ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1332ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1333ca40d24eSNicholas Bellinger 		pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1334ca40d24eSNicholas Bellinger 		       " ignoring\n", opcode);
1335ca40d24eSNicholas Bellinger 		return 0;
1336ca40d24eSNicholas Bellinger 	}
1337ca40d24eSNicholas Bellinger 
1338b8d26b3bSNicholas Bellinger 	switch (opcode) {
1339b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1340676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1341b8d26b3bSNicholas Bellinger 		if (!cmd)
1342b8d26b3bSNicholas Bellinger 			break;
1343b8d26b3bSNicholas Bellinger 
1344d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1345b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1346b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1347b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1348b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1349b8d26b3bSNicholas Bellinger 
1350d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1351b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1352b8d26b3bSNicholas Bellinger 		break;
1353b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1354676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1355b8d26b3bSNicholas Bellinger 		if (!cmd)
1356b8d26b3bSNicholas Bellinger 			break;
1357b8d26b3bSNicholas Bellinger 
1358d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1359d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1360778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1361b8d26b3bSNicholas Bellinger 		break;
1362b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1363b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1364b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1365b8d26b3bSNicholas Bellinger 		break;
1366b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1367676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1368b8d26b3bSNicholas Bellinger 		if (!cmd)
1369b8d26b3bSNicholas Bellinger 			break;
1370b8d26b3bSNicholas Bellinger 
1371b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1372b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1373b8d26b3bSNicholas Bellinger 		break;
1374b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1375676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1376b8d26b3bSNicholas Bellinger 		if (!cmd)
1377b8d26b3bSNicholas Bellinger 			break;
1378b8d26b3bSNicholas Bellinger 
1379b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1380b8d26b3bSNicholas Bellinger 		if (ret > 0)
1381b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1382b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1383b8d26b3bSNicholas Bellinger 						    HZ);
1384b8d26b3bSNicholas Bellinger 		break;
1385adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1386676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1387adb54c29SNicholas Bellinger 		if (!cmd)
1388adb54c29SNicholas Bellinger 			break;
1389adb54c29SNicholas Bellinger 
1390d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1391d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1392adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1393adb54c29SNicholas Bellinger 		break;
1394b8d26b3bSNicholas Bellinger 	default:
1395b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1396b8d26b3bSNicholas Bellinger 		dump_stack();
1397b8d26b3bSNicholas Bellinger 		break;
1398b8d26b3bSNicholas Bellinger 	}
1399b8d26b3bSNicholas Bellinger 
1400b8d26b3bSNicholas Bellinger 	return ret;
1401b8d26b3bSNicholas Bellinger }
1402b8d26b3bSNicholas Bellinger 
1403b8d26b3bSNicholas Bellinger static void
1404b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1405b8d26b3bSNicholas Bellinger {
1406b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1407b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1408b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1409b8d26b3bSNicholas Bellinger 	int rc;
1410b8d26b3bSNicholas Bellinger 
1411b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1412b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1413b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1414b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1415b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1416b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1417b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1418b8d26b3bSNicholas Bellinger 		}
1419b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1420b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1421b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1422b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1423b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1424b8d26b3bSNicholas Bellinger 		}
1425b8d26b3bSNicholas Bellinger 
1426b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1427b8d26b3bSNicholas Bellinger 		break;
1428b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1429b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1430b8d26b3bSNicholas Bellinger 		break;
1431b8d26b3bSNicholas Bellinger 	default:
1432b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1433b8d26b3bSNicholas Bellinger 		break;
1434b8d26b3bSNicholas Bellinger 	}
1435b8d26b3bSNicholas Bellinger 
1436b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1437b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1438b8d26b3bSNicholas Bellinger }
1439b8d26b3bSNicholas Bellinger 
1440b8d26b3bSNicholas Bellinger static void
1441b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1442b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1443b8d26b3bSNicholas Bellinger {
1444b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1445b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1446b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1447b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1448b8d26b3bSNicholas Bellinger 
1449b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1450b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1451b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1452b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1453b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1454b8d26b3bSNicholas Bellinger 	} else {
1455b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1456b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1457b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1458b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1459b8d26b3bSNicholas Bellinger 	}
1460b8d26b3bSNicholas Bellinger 
1461b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1462b8d26b3bSNicholas Bellinger 
1463b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1464b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1465b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1466b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1467b8d26b3bSNicholas Bellinger 
1468b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1469b8d26b3bSNicholas Bellinger 		isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1470b8d26b3bSNicholas Bellinger 				   isert_conn);
1471b8d26b3bSNicholas Bellinger 	else
1472b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
1473b8d26b3bSNicholas Bellinger 
1474b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1475b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1476b8d26b3bSNicholas Bellinger 
1477b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1478b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1479b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1480b8d26b3bSNicholas Bellinger 
1481b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1482b8d26b3bSNicholas Bellinger 		return;
1483b8d26b3bSNicholas Bellinger 
1484b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1485b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1486b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1487b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1488b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1489b8d26b3bSNicholas Bellinger 		if (err) {
1490b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1491b8d26b3bSNicholas Bellinger 			       count, err);
1492b8d26b3bSNicholas Bellinger 		}
1493b8d26b3bSNicholas Bellinger 	}
1494b8d26b3bSNicholas Bellinger }
1495b8d26b3bSNicholas Bellinger 
1496e3d7e4c3SSagi Grimberg static int
1497e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1498e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1499e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1500e3d7e4c3SSagi Grimberg {
1501e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1502e3d7e4c3SSagi Grimberg 
1503e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1504e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1505e3d7e4c3SSagi Grimberg 
1506e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1507e3d7e4c3SSagi Grimberg 	data->offset = offset;
1508e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1509e3d7e4c3SSagi Grimberg 
1510e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1511e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1512e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1513e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1514e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1515e3d7e4c3SSagi Grimberg 
1516e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1517e3d7e4c3SSagi Grimberg 					data->dma_dir);
1518e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
1519e3d7e4c3SSagi Grimberg 		pr_err("Cmd: unable to dma map SGs %p\n", sg);
1520e3d7e4c3SSagi Grimberg 		return -EINVAL;
1521e3d7e4c3SSagi Grimberg 	}
1522e3d7e4c3SSagi Grimberg 
1523e3d7e4c3SSagi Grimberg 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1524e3d7e4c3SSagi Grimberg 		 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1525e3d7e4c3SSagi Grimberg 
1526e3d7e4c3SSagi Grimberg 	return 0;
1527e3d7e4c3SSagi Grimberg }
1528e3d7e4c3SSagi Grimberg 
1529e3d7e4c3SSagi Grimberg static void
1530e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1531e3d7e4c3SSagi Grimberg {
1532e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1533e3d7e4c3SSagi Grimberg 
1534e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1535e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1536e3d7e4c3SSagi Grimberg }
1537e3d7e4c3SSagi Grimberg 
1538e3d7e4c3SSagi Grimberg 
1539e3d7e4c3SSagi Grimberg 
1540b8d26b3bSNicholas Bellinger static void
1541b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1542b8d26b3bSNicholas Bellinger {
1543b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1544b8d26b3bSNicholas Bellinger 
154590ecc6e2SVu Pham 	pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1546e3d7e4c3SSagi Grimberg 
1547e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
154890ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1549e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1550b8d26b3bSNicholas Bellinger 	}
1551b8d26b3bSNicholas Bellinger 
155290ecc6e2SVu Pham 	if (wr->send_wr) {
155390ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1554b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1555b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
155690ecc6e2SVu Pham 	}
1557b8d26b3bSNicholas Bellinger 
155890ecc6e2SVu Pham 	if (wr->ib_sge) {
155990ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
156090ecc6e2SVu Pham 		kfree(wr->ib_sge);
156190ecc6e2SVu Pham 		wr->ib_sge = NULL;
156290ecc6e2SVu Pham 	}
1563b8d26b3bSNicholas Bellinger }
1564b8d26b3bSNicholas Bellinger 
1565b8d26b3bSNicholas Bellinger static void
1566a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
156759464ef4SVu Pham {
156859464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
156959464ef4SVu Pham 	LIST_HEAD(unmap_list);
157059464ef4SVu Pham 
1571a3a5a826SSagi Grimberg 	pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
157259464ef4SVu Pham 
157359464ef4SVu Pham 	if (wr->fr_desc) {
1574a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
157559464ef4SVu Pham 			 isert_cmd, wr->fr_desc);
15769e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
15779e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
15789e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
15799e961ae7SSagi Grimberg 		}
158059464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1581a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
158259464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
158359464ef4SVu Pham 		wr->fr_desc = NULL;
158459464ef4SVu Pham 	}
158559464ef4SVu Pham 
1586e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
1587a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1588e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
158959464ef4SVu Pham 	}
159059464ef4SVu Pham 
159159464ef4SVu Pham 	wr->ib_sge = NULL;
159259464ef4SVu Pham 	wr->send_wr = NULL;
159359464ef4SVu Pham }
159459464ef4SVu Pham 
159559464ef4SVu Pham static void
159603e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1597b8d26b3bSNicholas Bellinger {
1598d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1599b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1600186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1601d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1602b8d26b3bSNicholas Bellinger 
1603b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1604b8d26b3bSNicholas Bellinger 
1605b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1606b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1607b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1608b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16095159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1610b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1611b8d26b3bSNicholas Bellinger 
161203e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1613b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
161403e7848aSNicholas Bellinger 			/*
161503e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
161603e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
161703e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
161803e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
161903e7848aSNicholas Bellinger 			 */
162003e7848aSNicholas Bellinger 			if (comp_err &&
162103e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
162203e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
162303e7848aSNicholas Bellinger 
162403e7848aSNicholas Bellinger 				target_put_sess_cmd(se_cmd->se_sess, se_cmd);
162503e7848aSNicholas Bellinger 			}
162603e7848aSNicholas Bellinger 		}
1627b8d26b3bSNicholas Bellinger 
1628d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1629186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1630186a9647SNicholas Bellinger 		break;
1631b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1632186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1633186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16345159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1635186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1636186a9647SNicholas Bellinger 
1637b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1638b8d26b3bSNicholas Bellinger 		break;
1639b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1640b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1641adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1642b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1643b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16445159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1645b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1646b8d26b3bSNicholas Bellinger 
1647b8d26b3bSNicholas Bellinger 		/*
1648b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1649b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1650b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1651b8d26b3bSNicholas Bellinger 		 */
1652b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
16533df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
16543df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
16553df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1656b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1657b8d26b3bSNicholas Bellinger 			break;
1658b8d26b3bSNicholas Bellinger 		}
1659b8d26b3bSNicholas Bellinger 		/*
1660b8d26b3bSNicholas Bellinger 		 * Fall-through
1661b8d26b3bSNicholas Bellinger 		 */
1662b8d26b3bSNicholas Bellinger 	default:
1663d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1664b8d26b3bSNicholas Bellinger 		break;
1665b8d26b3bSNicholas Bellinger 	}
1666b8d26b3bSNicholas Bellinger }
1667b8d26b3bSNicholas Bellinger 
1668b8d26b3bSNicholas Bellinger static void
1669b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1670b8d26b3bSNicholas Bellinger {
1671b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1672b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1673b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1674b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1675b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1676b8d26b3bSNicholas Bellinger 	}
1677b8d26b3bSNicholas Bellinger }
1678b8d26b3bSNicholas Bellinger 
1679b8d26b3bSNicholas Bellinger static void
1680b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
168103e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1682b8d26b3bSNicholas Bellinger {
1683dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1684dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1685dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1686dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1687dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1688b8d26b3bSNicholas Bellinger 	}
1689b8d26b3bSNicholas Bellinger 
1690b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
169103e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1692b8d26b3bSNicholas Bellinger }
1693b8d26b3bSNicholas Bellinger 
169496b7973eSSagi Grimberg static int
169596b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1696f93f3a70SSagi Grimberg {
16979e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
169896b7973eSSagi Grimberg 	int ret;
1699f93f3a70SSagi Grimberg 
170096b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
17019e961ae7SSagi Grimberg 	if (ret) {
17029e961ae7SSagi Grimberg 		pr_err("ib_check_mr_status failed, ret %d\n", ret);
17039e961ae7SSagi Grimberg 		goto fail_mr_status;
17049e961ae7SSagi Grimberg 	}
170596b7973eSSagi Grimberg 
17069e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
170796b7973eSSagi Grimberg 		u64 sec_offset_err;
17089e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
17099e961ae7SSagi Grimberg 
17109e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
17119e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
17129e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
17139e961ae7SSagi Grimberg 			break;
17149e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
17159e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
17169e961ae7SSagi Grimberg 			break;
17179e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
17189e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
17199e961ae7SSagi Grimberg 			break;
17209e961ae7SSagi Grimberg 		}
172196b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
172296b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
172396b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
17249e961ae7SSagi Grimberg 
17259e961ae7SSagi Grimberg 		pr_err("isert: PI error found type %d at sector 0x%llx "
17269e961ae7SSagi Grimberg 		       "expected 0x%x vs actual 0x%x\n",
17279e961ae7SSagi Grimberg 		       mr_status.sig_err.err_type,
17289e961ae7SSagi Grimberg 		       (unsigned long long)se_cmd->bad_sector,
17299e961ae7SSagi Grimberg 		       mr_status.sig_err.expected,
17309e961ae7SSagi Grimberg 		       mr_status.sig_err.actual);
17319e961ae7SSagi Grimberg 		ret = 1;
17329e961ae7SSagi Grimberg 	}
17339e961ae7SSagi Grimberg 
17349e961ae7SSagi Grimberg fail_mr_status:
173596b7973eSSagi Grimberg 	return ret;
173696b7973eSSagi Grimberg }
173796b7973eSSagi Grimberg 
173896b7973eSSagi Grimberg static void
173996b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
174096b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
174196b7973eSSagi Grimberg {
174296b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
174396b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
174496b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
174596b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
174696b7973eSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
174796b7973eSSagi Grimberg 	int ret = 0;
174896b7973eSSagi Grimberg 
174996b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
175096b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
175196b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
175296b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
175396b7973eSSagi Grimberg 	}
175496b7973eSSagi Grimberg 
1755f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1756897bb2c9SSagi Grimberg 	wr->send_wr_num = 0;
17579e961ae7SSagi Grimberg 	if (ret)
17589e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
17599e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
17609e961ae7SSagi Grimberg 	else
1761f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1762f93f3a70SSagi Grimberg }
1763f93f3a70SSagi Grimberg 
1764f93f3a70SSagi Grimberg static void
1765b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1766b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1767b8d26b3bSNicholas Bellinger {
1768b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1769d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1770b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
177190ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1772d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
17735bac4b1aSSagi Grimberg 	int ret = 0;
1774b8d26b3bSNicholas Bellinger 
17759e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
177696b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
177796b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
177896b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
17799e961ae7SSagi Grimberg 	}
17809e961ae7SSagi Grimberg 
1781b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1782d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1783e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1784b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1785b8d26b3bSNicholas Bellinger 
178690ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1787b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1788b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1789b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1790b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1791b8d26b3bSNicholas Bellinger 
17925bac4b1aSSagi Grimberg 	if (ret)
17935bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
17945bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
17955bac4b1aSSagi Grimberg 	else
1796b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1797b8d26b3bSNicholas Bellinger }
1798b8d26b3bSNicholas Bellinger 
1799b8d26b3bSNicholas Bellinger static void
1800b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1801b8d26b3bSNicholas Bellinger {
1802b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1803b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1804b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1805b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1806d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1807b8d26b3bSNicholas Bellinger 
1808b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1809b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1810b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1811b8d26b3bSNicholas Bellinger 
1812b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1813b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1814b8d26b3bSNicholas Bellinger 
1815b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
181603e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1817b8d26b3bSNicholas Bellinger 		break;
1818b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1819b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1820b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1821b8d26b3bSNicholas Bellinger 
1822b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
182303e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
18243df8f68aSNicholas Bellinger 		break;
1825b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1826b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1827b8d26b3bSNicholas Bellinger 		/*
1828b8d26b3bSNicholas Bellinger 		 * Call atomic_dec(&isert_conn->post_send_buf_count)
1829defd8848SNicholas Bellinger 		 * from isert_wait_conn()
1830b8d26b3bSNicholas Bellinger 		 */
1831b8d26b3bSNicholas Bellinger 		isert_conn->logout_posted = true;
1832b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1833b8d26b3bSNicholas Bellinger 		break;
1834adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1835adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1836adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
183703e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1838adb54c29SNicholas Bellinger 		break;
1839b8d26b3bSNicholas Bellinger 	default:
1840b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1841b8d26b3bSNicholas Bellinger 		dump_stack();
1842b8d26b3bSNicholas Bellinger 		break;
1843b8d26b3bSNicholas Bellinger 	}
1844b8d26b3bSNicholas Bellinger }
1845b8d26b3bSNicholas Bellinger 
1846b8d26b3bSNicholas Bellinger static void
1847b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1848b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1849b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1850b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1851b8d26b3bSNicholas Bellinger {
1852d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1853b6b87a1dSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1854b8d26b3bSNicholas Bellinger 
1855b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
18563df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1857adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1858adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1859b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1860b8d26b3bSNicholas Bellinger 
1861b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1862b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1863b8d26b3bSNicholas Bellinger 		return;
1864b8d26b3bSNicholas Bellinger 	}
1865897bb2c9SSagi Grimberg 
1866897bb2c9SSagi Grimberg 	/**
1867897bb2c9SSagi Grimberg 	 * If send_wr_num is 0 this means that we got
1868897bb2c9SSagi Grimberg 	 * RDMA completion and we cleared it and we should
1869897bb2c9SSagi Grimberg 	 * simply decrement the response post. else the
1870897bb2c9SSagi Grimberg 	 * response is incorporated in send_wr_num, just
1871897bb2c9SSagi Grimberg 	 * sub it.
1872897bb2c9SSagi Grimberg 	 **/
1873897bb2c9SSagi Grimberg 	if (wr->send_wr_num)
1874897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1875897bb2c9SSagi Grimberg 	else
1876897bb2c9SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1877b8d26b3bSNicholas Bellinger 
1878b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
187903e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1880b8d26b3bSNicholas Bellinger }
1881b8d26b3bSNicholas Bellinger 
1882b8d26b3bSNicholas Bellinger static void
188395b60f07SNicholas Bellinger __isert_send_completion(struct iser_tx_desc *tx_desc,
1884b8d26b3bSNicholas Bellinger 		        struct isert_conn *isert_conn)
1885b8d26b3bSNicholas Bellinger {
1886b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1887b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1888b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1889b8d26b3bSNicholas Bellinger 
1890b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1891b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1892b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1893b8d26b3bSNicholas Bellinger 		return;
1894b8d26b3bSNicholas Bellinger 	}
1895b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1896b8d26b3bSNicholas Bellinger 
1897b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1898b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
1899b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1900b8d26b3bSNicholas Bellinger 		dump_stack();
1901b8d26b3bSNicholas Bellinger 		break;
1902b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1903b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1904b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1905b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1906b8d26b3bSNicholas Bellinger 		break;
1907b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1908f93f3a70SSagi Grimberg 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1909897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1910f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
1911b8d26b3bSNicholas Bellinger 		break;
1912b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1913b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1914b8d26b3bSNicholas Bellinger 
1915b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1916b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1917b8d26b3bSNicholas Bellinger 		break;
1918b8d26b3bSNicholas Bellinger 	default:
1919b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1920b8d26b3bSNicholas Bellinger 		dump_stack();
1921b8d26b3bSNicholas Bellinger 		break;
1922b8d26b3bSNicholas Bellinger 	}
1923b8d26b3bSNicholas Bellinger }
1924b8d26b3bSNicholas Bellinger 
1925b8d26b3bSNicholas Bellinger static void
192695b60f07SNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
192795b60f07SNicholas Bellinger 		      struct isert_conn *isert_conn)
192895b60f07SNicholas Bellinger {
192995b60f07SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
193095b60f07SNicholas Bellinger 	struct iser_tx_desc *t;
193195b60f07SNicholas Bellinger 	/*
193295b60f07SNicholas Bellinger 	 * Drain coalesced completion llist starting from comp_llnode_batch
193395b60f07SNicholas Bellinger 	 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
193495b60f07SNicholas Bellinger 	 */
193595b60f07SNicholas Bellinger 	while (llnode) {
193695b60f07SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
193795b60f07SNicholas Bellinger 		llnode = llist_next(llnode);
193895b60f07SNicholas Bellinger 		__isert_send_completion(t, isert_conn);
193995b60f07SNicholas Bellinger 	}
194095b60f07SNicholas Bellinger 	__isert_send_completion(tx_desc, isert_conn);
194195b60f07SNicholas Bellinger }
194295b60f07SNicholas Bellinger 
194395b60f07SNicholas Bellinger static void
1944ebbe4421SNicholas Bellinger isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1945ebbe4421SNicholas Bellinger {
1946ebbe4421SNicholas Bellinger 	struct llist_node *llnode;
1947ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
1948ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
1949ebbe4421SNicholas Bellinger 
1950ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
1951ebbe4421SNicholas Bellinger 	llnode = llist_del_all(&isert_conn->conn_comp_llist);
1952ebbe4421SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
1953ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
1954ebbe4421SNicholas Bellinger 
1955ebbe4421SNicholas Bellinger 	while (llnode) {
1956ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1957ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
1958ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
1959ebbe4421SNicholas Bellinger 
1960897bb2c9SSagi Grimberg 		/**
1961897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
1962897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
1963897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
1964897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
1965897bb2c9SSagi Grimberg 		 * sub it.
1966897bb2c9SSagi Grimberg 		 **/
1967897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
1968897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
1969897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
1970897bb2c9SSagi Grimberg 		else
1971897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
197203e7848aSNicholas Bellinger 
197303e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
1974ebbe4421SNicholas Bellinger 	}
1975ebbe4421SNicholas Bellinger }
1976ebbe4421SNicholas Bellinger 
1977ebbe4421SNicholas Bellinger static void
1978defd8848SNicholas Bellinger isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1979b8d26b3bSNicholas Bellinger {
1980b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1981b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1982ebbe4421SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
1983ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
1984ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
1985ebbe4421SNicholas Bellinger 
1986ebbe4421SNicholas Bellinger 	while (llnode) {
1987ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1988ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
1989ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
1990ebbe4421SNicholas Bellinger 
1991897bb2c9SSagi Grimberg 		/**
1992897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
1993897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
1994897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
1995897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
1996897bb2c9SSagi Grimberg 		 * sub it.
1997897bb2c9SSagi Grimberg 		 **/
1998897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
1999897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
2000897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
2001897bb2c9SSagi Grimberg 		else
2002897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
200303e7848aSNicholas Bellinger 
200403e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
2005ebbe4421SNicholas Bellinger 	}
2006ebbe4421SNicholas Bellinger 	tx_desc->comp_llnode_batch = NULL;
2007b8d26b3bSNicholas Bellinger 
2008b8d26b3bSNicholas Bellinger 	if (!isert_cmd)
2009b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
2010b8d26b3bSNicholas Bellinger 	else
201103e7848aSNicholas Bellinger 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
2012b8d26b3bSNicholas Bellinger }
2013b8d26b3bSNicholas Bellinger 
2014defd8848SNicholas Bellinger static void
2015defd8848SNicholas Bellinger isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2016defd8848SNicholas Bellinger {
2017defd8848SNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2018defd8848SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
2019defd8848SNicholas Bellinger 
2020defd8848SNicholas Bellinger 	if (isert_conn->post_recv_buf_count)
2021defd8848SNicholas Bellinger 		return;
2022defd8848SNicholas Bellinger 
2023ebbe4421SNicholas Bellinger 	isert_cq_drain_comp_llist(isert_conn, ib_dev);
2024ebbe4421SNicholas Bellinger 
2025defd8848SNicholas Bellinger 	if (conn->sess) {
2026defd8848SNicholas Bellinger 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2027defd8848SNicholas Bellinger 		target_wait_for_sess_cmds(conn->sess->se_sess);
2028defd8848SNicholas Bellinger 	}
2029defd8848SNicholas Bellinger 
2030defd8848SNicholas Bellinger 	while (atomic_read(&isert_conn->post_send_buf_count))
2031defd8848SNicholas Bellinger 		msleep(3000);
2032b8d26b3bSNicholas Bellinger 
2033b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2034defd8848SNicholas Bellinger 	isert_conn->state = ISER_CONN_DOWN;
2035b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2036b2cb9649SNicholas Bellinger 
2037defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait_comp_err);
2038b8d26b3bSNicholas Bellinger }
2039b8d26b3bSNicholas Bellinger 
2040b8d26b3bSNicholas Bellinger static void
2041b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
2042b8d26b3bSNicholas Bellinger {
2043b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2044b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
2045b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2046b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2047b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2048b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2049b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
2050b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2051b8d26b3bSNicholas Bellinger 
2052b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2053b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2054b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2055b8d26b3bSNicholas Bellinger 
2056b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2057b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
2058b8d26b3bSNicholas Bellinger 		} else {
2059b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2060b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
2061c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
2062defd8848SNicholas Bellinger 
20639bb4ca68SNicholas Bellinger 			if (wc.wr_id != ISER_FASTREG_LI_WRID) {
2064ebbe4421SNicholas Bellinger 				if (tx_desc->llnode_active)
2065ebbe4421SNicholas Bellinger 					continue;
2066ebbe4421SNicholas Bellinger 
2067b8d26b3bSNicholas Bellinger 				atomic_dec(&isert_conn->post_send_buf_count);
2068defd8848SNicholas Bellinger 				isert_cq_tx_comp_err(tx_desc, isert_conn);
2069b8d26b3bSNicholas Bellinger 			}
2070b8d26b3bSNicholas Bellinger 		}
20719bb4ca68SNicholas Bellinger 	}
2072b8d26b3bSNicholas Bellinger 
2073b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2074b8d26b3bSNicholas Bellinger }
2075b8d26b3bSNicholas Bellinger 
2076b8d26b3bSNicholas Bellinger static void
2077b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
2078b8d26b3bSNicholas Bellinger {
2079b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2080b8d26b3bSNicholas Bellinger 
2081b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2082b8d26b3bSNicholas Bellinger }
2083b8d26b3bSNicholas Bellinger 
2084b8d26b3bSNicholas Bellinger static void
2085b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
2086b8d26b3bSNicholas Bellinger {
2087b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2088b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
2089b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2090b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2091b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2092b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2093b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
2094b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2095b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
2096b8d26b3bSNicholas Bellinger 
2097b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2098b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2099b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2100b8d26b3bSNicholas Bellinger 
2101b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2102b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
2103b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
2104b8d26b3bSNicholas Bellinger 		} else {
2105b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2106c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
2107b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
2108c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
2109c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
2110c5a2adbfSNicholas Bellinger 			}
2111b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
2112defd8848SNicholas Bellinger 			isert_cq_rx_comp_err(isert_conn);
2113b8d26b3bSNicholas Bellinger 		}
2114b8d26b3bSNicholas Bellinger 	}
2115b8d26b3bSNicholas Bellinger 
2116b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2117b8d26b3bSNicholas Bellinger }
2118b8d26b3bSNicholas Bellinger 
2119b8d26b3bSNicholas Bellinger static void
2120b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
2121b8d26b3bSNicholas Bellinger {
2122b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2123b8d26b3bSNicholas Bellinger 
2124b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2125b8d26b3bSNicholas Bellinger }
2126b8d26b3bSNicholas Bellinger 
2127b8d26b3bSNicholas Bellinger static int
2128b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2129b8d26b3bSNicholas Bellinger {
2130b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2131b8d26b3bSNicholas Bellinger 	int ret;
2132b8d26b3bSNicholas Bellinger 
2133b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
2134b8d26b3bSNicholas Bellinger 
2135b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2136b8d26b3bSNicholas Bellinger 			   &wr_failed);
2137b8d26b3bSNicholas Bellinger 	if (ret) {
2138b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
2139b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2140b8d26b3bSNicholas Bellinger 		return ret;
2141b8d26b3bSNicholas Bellinger 	}
2142b8d26b3bSNicholas Bellinger 	return ret;
2143b8d26b3bSNicholas Bellinger }
2144b8d26b3bSNicholas Bellinger 
2145b8d26b3bSNicholas Bellinger static int
2146b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2147b8d26b3bSNicholas Bellinger {
2148d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2149b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2150b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2151b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2152b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2153b8d26b3bSNicholas Bellinger 
2154b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2155b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2156b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2157b8d26b3bSNicholas Bellinger 	/*
2158b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2159b8d26b3bSNicholas Bellinger 	 */
2160b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2161b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2162b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2163b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2164b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2165dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2166b8d26b3bSNicholas Bellinger 
2167b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2168b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2169b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2170b8d26b3bSNicholas Bellinger 
2171b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2172b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2173dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2174b8d26b3bSNicholas Bellinger 
2175dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2176dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2177b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2178b8d26b3bSNicholas Bellinger 
2179dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2180dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2181dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
2182b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2183b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2184b8d26b3bSNicholas Bellinger 	}
2185b8d26b3bSNicholas Bellinger 
218695b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
2187b8d26b3bSNicholas Bellinger 
2188b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2189b8d26b3bSNicholas Bellinger 
2190b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2191b8d26b3bSNicholas Bellinger }
2192b8d26b3bSNicholas Bellinger 
2193131e6abcSNicholas Bellinger static void
2194131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2195131e6abcSNicholas Bellinger {
2196131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2197131e6abcSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2198131e6abcSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2199131e6abcSNicholas Bellinger 
2200131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2201131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2202131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2203131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2204131e6abcSNicholas Bellinger 
2205131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2206131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2207131e6abcSNicholas Bellinger 
2208131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2209131e6abcSNicholas Bellinger }
2210131e6abcSNicholas Bellinger 
2211e70beee7SNicholas Bellinger static enum target_prot_op
2212e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2213e70beee7SNicholas Bellinger {
2214e70beee7SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2215e70beee7SNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2216e70beee7SNicholas Bellinger 
2217e70beee7SNicholas Bellinger 	if (device->pi_capable)
2218e70beee7SNicholas Bellinger 		return TARGET_PROT_ALL;
2219e70beee7SNicholas Bellinger 
2220e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2221e70beee7SNicholas Bellinger }
2222e70beee7SNicholas Bellinger 
2223b8d26b3bSNicholas Bellinger static int
2224b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2225b8d26b3bSNicholas Bellinger 		bool nopout_response)
2226b8d26b3bSNicholas Bellinger {
2227d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2228b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2229b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2230b8d26b3bSNicholas Bellinger 
2231b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2232b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2233b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2234b8d26b3bSNicholas Bellinger 			       nopout_response);
2235b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
223695b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2237b8d26b3bSNicholas Bellinger 
22388b513d0cSMasanari Iida 	pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2239b8d26b3bSNicholas Bellinger 
2240b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2241b8d26b3bSNicholas Bellinger }
2242b8d26b3bSNicholas Bellinger 
2243b8d26b3bSNicholas Bellinger static int
2244b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2245b8d26b3bSNicholas Bellinger {
2246d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2247b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2248b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2249b8d26b3bSNicholas Bellinger 
2250b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2251b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2252b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2253b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
225495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2255b8d26b3bSNicholas Bellinger 
2256b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2257b8d26b3bSNicholas Bellinger 
2258b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2259b8d26b3bSNicholas Bellinger }
2260b8d26b3bSNicholas Bellinger 
2261b8d26b3bSNicholas Bellinger static int
2262b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2263b8d26b3bSNicholas Bellinger {
2264d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2265b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2266b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2267b8d26b3bSNicholas Bellinger 
2268b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2269b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2270b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2271b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
227295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2273b8d26b3bSNicholas Bellinger 
2274b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2275b8d26b3bSNicholas Bellinger 
2276b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2277b8d26b3bSNicholas Bellinger }
2278b8d26b3bSNicholas Bellinger 
2279b8d26b3bSNicholas Bellinger static int
2280b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2281b8d26b3bSNicholas Bellinger {
2282d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2283b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2284b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
22853df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
22863df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
22873df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
22883df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2289b8d26b3bSNicholas Bellinger 
2290b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
22913df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2292b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
22933df8f68aSNicholas Bellinger 
22943df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2295dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
22963df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
22973df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2298dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2299dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
23003df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
23013df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
23023df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
23033df8f68aSNicholas Bellinger 
230495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2305b8d26b3bSNicholas Bellinger 
2306b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2307b8d26b3bSNicholas Bellinger 
2308b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2309b8d26b3bSNicholas Bellinger }
2310b8d26b3bSNicholas Bellinger 
2311b8d26b3bSNicholas Bellinger static int
2312adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2313adb54c29SNicholas Bellinger {
2314d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2315adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2316adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2317adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2318adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2319adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2320adb54c29SNicholas Bellinger 	int rc;
2321adb54c29SNicholas Bellinger 
2322adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2323adb54c29SNicholas Bellinger 	rc = iscsit_build_text_rsp(cmd, conn, hdr);
2324adb54c29SNicholas Bellinger 	if (rc < 0)
2325adb54c29SNicholas Bellinger 		return rc;
2326adb54c29SNicholas Bellinger 
2327adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2328adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2329adb54c29SNicholas Bellinger 
2330adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2331adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2332adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2333adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2334adb54c29SNicholas Bellinger 
2335adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2336adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2337adb54c29SNicholas Bellinger 
2338adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2339adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2340adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
2341adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2342adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2343adb54c29SNicholas Bellinger 	}
234495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2345adb54c29SNicholas Bellinger 
2346adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2347adb54c29SNicholas Bellinger 
2348adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2349adb54c29SNicholas Bellinger }
2350adb54c29SNicholas Bellinger 
2351adb54c29SNicholas Bellinger static int
2352b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2353b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2354b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2355b8d26b3bSNicholas Bellinger {
2356d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2357b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2358b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2359b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2360b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2361b8d26b3bSNicholas Bellinger 
2362b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2363b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2364b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2365b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2366b8d26b3bSNicholas Bellinger 
2367b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2368b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2369b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2370b8d26b3bSNicholas Bellinger 	/*
2371b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2372b8d26b3bSNicholas Bellinger 	 */
2373b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2374b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2375b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
2376b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
2377b8d26b3bSNicholas Bellinger 
2378b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2379b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2380b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2381b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2382b8d26b3bSNicholas Bellinger 
238390ecc6e2SVu Pham 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
238490ecc6e2SVu Pham 			 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2385b8d26b3bSNicholas Bellinger 		page_off = 0;
2386b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2387b8d26b3bSNicholas Bellinger 		ib_sge++;
2388b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2389b8d26b3bSNicholas Bellinger 	}
2390b8d26b3bSNicholas Bellinger 
2391b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2392b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
2393b8d26b3bSNicholas Bellinger 
2394b8d26b3bSNicholas Bellinger 	return sg_nents;
2395b8d26b3bSNicholas Bellinger }
2396b8d26b3bSNicholas Bellinger 
2397b8d26b3bSNicholas Bellinger static int
239890ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
239990ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2400b8d26b3bSNicholas Bellinger {
2401b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2402d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2403b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2404e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
240590ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2406b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2407e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2408e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2409b8d26b3bSNicholas Bellinger 
241090ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2411b8d26b3bSNicholas Bellinger 
2412e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2413e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2414e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2415e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2416e3d7e4c3SSagi Grimberg 	if (ret)
2417e3d7e4c3SSagi Grimberg 		return ret;
2418b8d26b3bSNicholas Bellinger 
2419e3d7e4c3SSagi Grimberg 	data_left = data->len;
2420e3d7e4c3SSagi Grimberg 	offset = data->offset;
2421b8d26b3bSNicholas Bellinger 
2422e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2423b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
242490ecc6e2SVu Pham 		pr_warn("Unable to allocate ib_sge\n");
2425b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2426e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2427b8d26b3bSNicholas Bellinger 	}
242890ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2429b8d26b3bSNicholas Bellinger 
2430e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2431b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2432b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2433b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
243490ecc6e2SVu Pham 		pr_debug("Unable to allocate wr->send_wr\n");
2435b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2436e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2437b8d26b3bSNicholas Bellinger 	}
2438b8d26b3bSNicholas Bellinger 
2439b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2440b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2441b8d26b3bSNicholas Bellinger 
2442b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2443b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2444b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2445b8d26b3bSNicholas Bellinger 
2446b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
244790ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
244890ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2449b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2450b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
245190ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
245290ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
245390ecc6e2SVu Pham 			else
245490ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
245590ecc6e2SVu Pham 		} else {
245690ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
245790ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
245890ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
245990ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
246090ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
246190ecc6e2SVu Pham 			else
246290ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
246390ecc6e2SVu Pham 		}
2464b8d26b3bSNicholas Bellinger 
2465b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2466b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2467b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2468b8d26b3bSNicholas Bellinger 
2469b8d26b3bSNicholas Bellinger 		offset += data_len;
247090ecc6e2SVu Pham 		va_offset += data_len;
2471b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2472b8d26b3bSNicholas Bellinger 	}
247390ecc6e2SVu Pham 
247490ecc6e2SVu Pham 	return 0;
2475e3d7e4c3SSagi Grimberg unmap_cmd:
2476e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2477e3d7e4c3SSagi Grimberg 
247890ecc6e2SVu Pham 	return ret;
247990ecc6e2SVu Pham }
248090ecc6e2SVu Pham 
248190ecc6e2SVu Pham static int
248259464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
248359464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
248459464ef4SVu Pham {
248559464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
248659464ef4SVu Pham 	struct scatterlist *tmp_sg;
248759464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
248859464ef4SVu Pham 
248959464ef4SVu Pham 	n_pages = 0;
249059464ef4SVu Pham 	new_chunk = 1;
249159464ef4SVu Pham 	last_ent = sg_nents - 1;
249259464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
249359464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
249459464ef4SVu Pham 		if (new_chunk)
249559464ef4SVu Pham 			chunk_start = start_addr;
249659464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
249759464ef4SVu Pham 
249859464ef4SVu Pham 		pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
249959464ef4SVu Pham 			 i, (unsigned long long)tmp_sg->dma_address,
250059464ef4SVu Pham 			 tmp_sg->length);
250159464ef4SVu Pham 
250259464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
250359464ef4SVu Pham 			new_chunk = 0;
250459464ef4SVu Pham 			continue;
250559464ef4SVu Pham 		}
250659464ef4SVu Pham 		new_chunk = 1;
250759464ef4SVu Pham 
250859464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
250959464ef4SVu Pham 		do {
251059464ef4SVu Pham 			fr_pl[n_pages++] = page;
251159464ef4SVu Pham 			pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
251259464ef4SVu Pham 				 n_pages - 1, page);
251359464ef4SVu Pham 			page += PAGE_SIZE;
251459464ef4SVu Pham 		} while (page < end_addr);
251559464ef4SVu Pham 	}
251659464ef4SVu Pham 
251759464ef4SVu Pham 	return n_pages;
251859464ef4SVu Pham }
251959464ef4SVu Pham 
252059464ef4SVu Pham static int
2521e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2522e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2523e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
25249e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2525e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
252659464ef4SVu Pham {
252759464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
25289e961ae7SSagi Grimberg 	struct ib_mr *mr;
25299e961ae7SSagi Grimberg 	struct ib_fast_reg_page_list *frpl;
253059464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
253159464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
25329bd626e7SSagi Grimberg 	int ret, pagelist_len;
25339bd626e7SSagi Grimberg 	u32 page_off;
253459464ef4SVu Pham 	u8 key;
253559464ef4SVu Pham 
2536e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
2537e3d7e4c3SSagi Grimberg 		sge->lkey = isert_conn->conn_mr->lkey;
2538e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2539e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
25409e961ae7SSagi Grimberg 		pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
25419e961ae7SSagi Grimberg 			 __func__, __LINE__, sge->addr, sge->length,
25429e961ae7SSagi Grimberg 			 sge->lkey);
2543e3d7e4c3SSagi Grimberg 		return 0;
2544e3d7e4c3SSagi Grimberg 	}
2545e3d7e4c3SSagi Grimberg 
25469e961ae7SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID) {
25479e961ae7SSagi Grimberg 		/* Registering data buffer */
25489e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
25499e961ae7SSagi Grimberg 		frpl = fr_desc->data_frpl;
25509e961ae7SSagi Grimberg 	} else {
25519e961ae7SSagi Grimberg 		/* Registering protection buffer */
25529e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
25539e961ae7SSagi Grimberg 		frpl = fr_desc->pi_ctx->prot_frpl;
25549e961ae7SSagi Grimberg 	}
25559e961ae7SSagi Grimberg 
2556e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
255759464ef4SVu Pham 
25589bd626e7SSagi Grimberg 	pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2559e3d7e4c3SSagi Grimberg 		 fr_desc, mem->nents, mem->offset);
256059464ef4SVu Pham 
2561e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
25629e961ae7SSagi Grimberg 					     &frpl->page_list[0]);
256359464ef4SVu Pham 
2564d3e125daSSagi Grimberg 	if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
256559464ef4SVu Pham 		memset(&inv_wr, 0, sizeof(inv_wr));
25669bb4ca68SNicholas Bellinger 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
256759464ef4SVu Pham 		inv_wr.opcode = IB_WR_LOCAL_INV;
25689e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = mr->rkey;
256959464ef4SVu Pham 		wr = &inv_wr;
257059464ef4SVu Pham 		/* Bump the key */
25719e961ae7SSagi Grimberg 		key = (u8)(mr->rkey & 0x000000FF);
25729e961ae7SSagi Grimberg 		ib_update_fast_reg_key(mr, ++key);
257359464ef4SVu Pham 	}
257459464ef4SVu Pham 
257559464ef4SVu Pham 	/* Prepare FASTREG WR */
257659464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
25779bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
257859464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
25799e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
25809e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.page_list = frpl;
258159464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
258259464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2583e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
25849e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.rkey = mr->rkey;
258559464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
258659464ef4SVu Pham 
258759464ef4SVu Pham 	if (!wr)
258859464ef4SVu Pham 		wr = &fr_wr;
258959464ef4SVu Pham 	else
259059464ef4SVu Pham 		wr->next = &fr_wr;
259159464ef4SVu Pham 
259259464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
259359464ef4SVu Pham 	if (ret) {
259459464ef4SVu Pham 		pr_err("fast registration failed, ret:%d\n", ret);
259559464ef4SVu Pham 		return ret;
259659464ef4SVu Pham 	}
25979e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
259859464ef4SVu Pham 
25999e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
26009e961ae7SSagi Grimberg 	sge->addr = frpl->page_list[0] + page_off;
2601e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
260259464ef4SVu Pham 
26039e961ae7SSagi Grimberg 	pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
26049e961ae7SSagi Grimberg 		 __func__, __LINE__, sge->addr, sge->length,
26059e961ae7SSagi Grimberg 		 sge->lkey);
260659464ef4SVu Pham 
260759464ef4SVu Pham 	return ret;
260859464ef4SVu Pham }
260959464ef4SVu Pham 
26109e961ae7SSagi Grimberg static inline enum ib_t10_dif_type
26119e961ae7SSagi Grimberg se2ib_prot_type(enum target_prot_type prot_type)
26129e961ae7SSagi Grimberg {
26139e961ae7SSagi Grimberg 	switch (prot_type) {
26149e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE0_PROT:
26159e961ae7SSagi Grimberg 		return IB_T10DIF_NONE;
26169e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE1_PROT:
26179e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE1;
26189e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE2_PROT:
26199e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE2;
26209e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE3_PROT:
26219e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE3;
26229e961ae7SSagi Grimberg 	default:
26239e961ae7SSagi Grimberg 		return IB_T10DIF_NONE;
26249e961ae7SSagi Grimberg 	}
26259e961ae7SSagi Grimberg }
26269e961ae7SSagi Grimberg 
26279e961ae7SSagi Grimberg static int
26289e961ae7SSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
26299e961ae7SSagi Grimberg {
26309e961ae7SSagi Grimberg 	enum ib_t10_dif_type ib_prot_type = se2ib_prot_type(se_cmd->prot_type);
26319e961ae7SSagi Grimberg 
26329e961ae7SSagi Grimberg 	sig_attrs->mem.sig_type = IB_SIG_TYPE_T10_DIF;
26339e961ae7SSagi Grimberg 	sig_attrs->wire.sig_type = IB_SIG_TYPE_T10_DIF;
26349e961ae7SSagi Grimberg 	sig_attrs->mem.sig.dif.pi_interval =
26359e961ae7SSagi Grimberg 				se_cmd->se_dev->dev_attrib.block_size;
26369e961ae7SSagi Grimberg 	sig_attrs->wire.sig.dif.pi_interval =
26379e961ae7SSagi Grimberg 				se_cmd->se_dev->dev_attrib.block_size;
26389e961ae7SSagi Grimberg 
26399e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
26409e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
26419e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
26429e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = IB_T10DIF_NONE;
26439e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = ib_prot_type;
26449e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
26459e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
26469e961ae7SSagi Grimberg 		break;
26479e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
26489e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
26499e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = ib_prot_type;
26509e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
26519e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
26529e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = IB_T10DIF_NONE;
26539e961ae7SSagi Grimberg 		break;
26549e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
26559e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
26569e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = ib_prot_type;
26579e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
26589e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
26599e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = ib_prot_type;
26609e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
26619e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
26629e961ae7SSagi Grimberg 		break;
26639e961ae7SSagi Grimberg 	default:
26649e961ae7SSagi Grimberg 		pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
26659e961ae7SSagi Grimberg 		return -EINVAL;
26669e961ae7SSagi Grimberg 	}
26679e961ae7SSagi Grimberg 
26689e961ae7SSagi Grimberg 	return 0;
26699e961ae7SSagi Grimberg }
26709e961ae7SSagi Grimberg 
26719e961ae7SSagi Grimberg static inline u8
26729e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
26739e961ae7SSagi Grimberg {
26749e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
26759e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
26769e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
26779e961ae7SSagi Grimberg }
26789e961ae7SSagi Grimberg 
26799e961ae7SSagi Grimberg static int
26809e961ae7SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
26819e961ae7SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc,
26829e961ae7SSagi Grimberg 		 struct ib_sge *data_sge, struct ib_sge *prot_sge,
26839e961ae7SSagi Grimberg 		 struct ib_sge *sig_sge)
26849e961ae7SSagi Grimberg {
26859e961ae7SSagi Grimberg 	struct ib_send_wr sig_wr, inv_wr;
26869e961ae7SSagi Grimberg 	struct ib_send_wr *bad_wr, *wr = NULL;
26879e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
26889e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
26899e961ae7SSagi Grimberg 	int ret;
26909e961ae7SSagi Grimberg 	u32 key;
26919e961ae7SSagi Grimberg 
26929e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
26939e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
26949e961ae7SSagi Grimberg 	if (ret)
26959e961ae7SSagi Grimberg 		goto err;
26969e961ae7SSagi Grimberg 
26979e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
26989e961ae7SSagi Grimberg 
26999e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
27009e961ae7SSagi Grimberg 		memset(&inv_wr, 0, sizeof(inv_wr));
27019e961ae7SSagi Grimberg 		inv_wr.opcode = IB_WR_LOCAL_INV;
2702c2caa207SSagi Grimberg 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
27039e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
27049e961ae7SSagi Grimberg 		wr = &inv_wr;
27059e961ae7SSagi Grimberg 		/* Bump the key */
27069e961ae7SSagi Grimberg 		key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
27079e961ae7SSagi Grimberg 		ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
27089e961ae7SSagi Grimberg 	}
27099e961ae7SSagi Grimberg 
27109e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
27119e961ae7SSagi Grimberg 	sig_wr.opcode = IB_WR_REG_SIG_MR;
2712c2caa207SSagi Grimberg 	sig_wr.wr_id = ISER_FASTREG_LI_WRID;
27139e961ae7SSagi Grimberg 	sig_wr.sg_list = data_sge;
27149e961ae7SSagi Grimberg 	sig_wr.num_sge = 1;
27159e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
27169e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
27179e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
27189e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
27199e961ae7SSagi Grimberg 		sig_wr.wr.sig_handover.prot = prot_sge;
27209e961ae7SSagi Grimberg 
27219e961ae7SSagi Grimberg 	if (!wr)
27229e961ae7SSagi Grimberg 		wr = &sig_wr;
27239e961ae7SSagi Grimberg 	else
27249e961ae7SSagi Grimberg 		wr->next = &sig_wr;
27259e961ae7SSagi Grimberg 
27269e961ae7SSagi Grimberg 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
27279e961ae7SSagi Grimberg 	if (ret) {
27289e961ae7SSagi Grimberg 		pr_err("fast registration failed, ret:%d\n", ret);
27299e961ae7SSagi Grimberg 		goto err;
27309e961ae7SSagi Grimberg 	}
27319e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
27329e961ae7SSagi Grimberg 
27339e961ae7SSagi Grimberg 	sig_sge->lkey = pi_ctx->sig_mr->lkey;
27349e961ae7SSagi Grimberg 	sig_sge->addr = 0;
27359e961ae7SSagi Grimberg 	sig_sge->length = se_cmd->data_length;
27369e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
27379e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
27389e961ae7SSagi Grimberg 		/*
27399e961ae7SSagi Grimberg 		 * We have protection guards on the wire
27409e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
27419e961ae7SSagi Grimberg 		 */
27429e961ae7SSagi Grimberg 		sig_sge->length += se_cmd->prot_length;
27439e961ae7SSagi Grimberg 
27449e961ae7SSagi Grimberg 	pr_debug("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
27459e961ae7SSagi Grimberg 		 sig_sge->addr, sig_sge->length,
27469e961ae7SSagi Grimberg 		 sig_sge->lkey);
27479e961ae7SSagi Grimberg err:
27489e961ae7SSagi Grimberg 	return ret;
27499e961ae7SSagi Grimberg }
27509e961ae7SSagi Grimberg 
275159464ef4SVu Pham static int
2752a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
275359464ef4SVu Pham 	       struct isert_rdma_wr *wr)
275459464ef4SVu Pham {
275559464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
275659464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2757e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
27589e961ae7SSagi Grimberg 	struct ib_sge data_sge;
275959464ef4SVu Pham 	struct ib_send_wr *send_wr;
2760e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2761e3d7e4c3SSagi Grimberg 	u32 offset;
2762e3d7e4c3SSagi Grimberg 	int ret = 0;
276359464ef4SVu Pham 	unsigned long flags;
276459464ef4SVu Pham 
276559464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2766e3d7e4c3SSagi Grimberg 
2767e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2768e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2769e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2770e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2771e3d7e4c3SSagi Grimberg 	if (ret)
2772e3d7e4c3SSagi Grimberg 		return ret;
2773e3d7e4c3SSagi Grimberg 
27749e961ae7SSagi Grimberg 	if (wr->data.dma_nents != 1 ||
27759e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_NORMAL) {
2776e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2777e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2778e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2779e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2780e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2781e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
278259464ef4SVu Pham 	}
278359464ef4SVu Pham 
27849e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
27859e961ae7SSagi Grimberg 				ISERT_DATA_KEY_VALID, &data_sge);
2786e3d7e4c3SSagi Grimberg 	if (ret)
2787e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
278859464ef4SVu Pham 
27899e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
27909e961ae7SSagi Grimberg 		struct ib_sge prot_sge, sig_sge;
27919e961ae7SSagi Grimberg 
27929e961ae7SSagi Grimberg 		if (se_cmd->t_prot_sg) {
27939e961ae7SSagi Grimberg 			ret = isert_map_data_buf(isert_conn, isert_cmd,
27949e961ae7SSagi Grimberg 						 se_cmd->t_prot_sg,
27959e961ae7SSagi Grimberg 						 se_cmd->t_prot_nents,
27969e961ae7SSagi Grimberg 						 se_cmd->prot_length,
27979e961ae7SSagi Grimberg 						 0, wr->iser_ib_op, &wr->prot);
27989e961ae7SSagi Grimberg 			if (ret)
27999e961ae7SSagi Grimberg 				goto unmap_cmd;
28009e961ae7SSagi Grimberg 
28019e961ae7SSagi Grimberg 			ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
28029e961ae7SSagi Grimberg 						ISERT_PROT_KEY_VALID, &prot_sge);
28039e961ae7SSagi Grimberg 			if (ret)
28049e961ae7SSagi Grimberg 				goto unmap_prot_cmd;
28059e961ae7SSagi Grimberg 		}
28069e961ae7SSagi Grimberg 
28079e961ae7SSagi Grimberg 		ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
28089e961ae7SSagi Grimberg 				       &data_sge, &prot_sge, &sig_sge);
28099e961ae7SSagi Grimberg 		if (ret)
28109e961ae7SSagi Grimberg 			goto unmap_prot_cmd;
28119e961ae7SSagi Grimberg 
28129e961ae7SSagi Grimberg 		fr_desc->ind |= ISERT_PROTECTED;
28139e961ae7SSagi Grimberg 		memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
28149e961ae7SSagi Grimberg 	} else
28159e961ae7SSagi Grimberg 		memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
28169e961ae7SSagi Grimberg 
2817e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
281859464ef4SVu Pham 	wr->send_wr_num = 1;
281959464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
282059464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
282159464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
282259464ef4SVu Pham 
282359464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2824e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
282559464ef4SVu Pham 	send_wr->num_sge = 1;
282659464ef4SVu Pham 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
282759464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
282859464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
282959464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
283059464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
28319e961ae7SSagi Grimberg 		send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
28329e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
283359464ef4SVu Pham 	} else {
283459464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
283559464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
283659464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
283759464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
283859464ef4SVu Pham 	}
283959464ef4SVu Pham 
284059464ef4SVu Pham 	return 0;
28419e961ae7SSagi Grimberg unmap_prot_cmd:
28429e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
28439e961ae7SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2844e3d7e4c3SSagi Grimberg unmap_cmd:
2845e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2846e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2847e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2848e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2849e3d7e4c3SSagi Grimberg 	}
2850e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
285159464ef4SVu Pham 
285259464ef4SVu Pham 	return ret;
285359464ef4SVu Pham }
285459464ef4SVu Pham 
285559464ef4SVu Pham static int
285690ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
285790ecc6e2SVu Pham {
285890ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
285959464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
286090ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
286190ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2862d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
286390ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
286490ecc6e2SVu Pham 	int rc;
286590ecc6e2SVu Pham 
286690ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
286790ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
286890ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2869d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
287090ecc6e2SVu Pham 	if (rc) {
287190ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
287290ecc6e2SVu Pham 		return rc;
287390ecc6e2SVu Pham 	}
287490ecc6e2SVu Pham 
28759e961ae7SSagi Grimberg 	if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2876b8d26b3bSNicholas Bellinger 		/*
2877b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2878b8d26b3bSNicholas Bellinger 		 */
28799e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
28809e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
288104d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2882b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2883b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
288495b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
288595b60f07SNicholas Bellinger 				   &isert_cmd->tx_desc.send_wr, true);
28869e961ae7SSagi Grimberg 		isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
2887897bb2c9SSagi Grimberg 		wr->send_wr_num += 1;
28889e961ae7SSagi Grimberg 	}
2889b8d26b3bSNicholas Bellinger 
2890897bb2c9SSagi Grimberg 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2891b8d26b3bSNicholas Bellinger 
2892b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2893b8d26b3bSNicholas Bellinger 	if (rc) {
2894b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2895897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2896b8d26b3bSNicholas Bellinger 	}
28979e961ae7SSagi Grimberg 
28989e961ae7SSagi Grimberg 	if (se_cmd->prot_op == TARGET_PROT_NORMAL)
28999e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
29009e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
29019e961ae7SSagi Grimberg 	else
29029e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
290390ecc6e2SVu Pham 			 isert_cmd);
2904b8d26b3bSNicholas Bellinger 
290590ecc6e2SVu Pham 	return 1;
2906b8d26b3bSNicholas Bellinger }
2907b8d26b3bSNicholas Bellinger 
2908b8d26b3bSNicholas Bellinger static int
2909b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2910b8d26b3bSNicholas Bellinger {
2911b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2912d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2913b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2914b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2915d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
291690ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
291790ecc6e2SVu Pham 	int rc;
2918b8d26b3bSNicholas Bellinger 
291990ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
292090ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2921b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2922d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
292390ecc6e2SVu Pham 	if (rc) {
292490ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
292590ecc6e2SVu Pham 		return rc;
2926b8d26b3bSNicholas Bellinger 	}
2927b8d26b3bSNicholas Bellinger 
2928b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2929b8d26b3bSNicholas Bellinger 
2930b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2931b8d26b3bSNicholas Bellinger 	if (rc) {
2932b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2933b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2934b8d26b3bSNicholas Bellinger 	}
293590ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
293690ecc6e2SVu Pham 		 isert_cmd);
2937b8d26b3bSNicholas Bellinger 
293890ecc6e2SVu Pham 	return 0;
2939b8d26b3bSNicholas Bellinger }
2940b8d26b3bSNicholas Bellinger 
2941b8d26b3bSNicholas Bellinger static int
2942b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2943b8d26b3bSNicholas Bellinger {
2944b8d26b3bSNicholas Bellinger 	int ret;
2945b8d26b3bSNicholas Bellinger 
2946b8d26b3bSNicholas Bellinger 	switch (state) {
2947b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2948b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2949b8d26b3bSNicholas Bellinger 		break;
2950b8d26b3bSNicholas Bellinger 	default:
2951b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
2952b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2953b8d26b3bSNicholas Bellinger 		break;
2954b8d26b3bSNicholas Bellinger 	}
2955b8d26b3bSNicholas Bellinger 
2956b8d26b3bSNicholas Bellinger 	return ret;
2957b8d26b3bSNicholas Bellinger }
2958b8d26b3bSNicholas Bellinger 
2959b8d26b3bSNicholas Bellinger static int
2960b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2961b8d26b3bSNicholas Bellinger {
2962b8d26b3bSNicholas Bellinger 	int ret;
2963b8d26b3bSNicholas Bellinger 
2964b8d26b3bSNicholas Bellinger 	switch (state) {
2965b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2966b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2967b8d26b3bSNicholas Bellinger 		if (!ret) {
2968b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
2969b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
2970b8d26b3bSNicholas Bellinger 		}
2971b8d26b3bSNicholas Bellinger 		break;
2972b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2973b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2974b8d26b3bSNicholas Bellinger 		break;
2975b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2976b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2977b8d26b3bSNicholas Bellinger 		break;
2978b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2979b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2980b8d26b3bSNicholas Bellinger 		break;
2981adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2982adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2983adb54c29SNicholas Bellinger 		break;
2984b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2985b8d26b3bSNicholas Bellinger 		/*
2986b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2987b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2988b8d26b3bSNicholas Bellinger 		 */
2989b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2990b8d26b3bSNicholas Bellinger 		break;
2991b8d26b3bSNicholas Bellinger 	default:
2992b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
2993b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2994b8d26b3bSNicholas Bellinger 		break;
2995b8d26b3bSNicholas Bellinger 	}
2996b8d26b3bSNicholas Bellinger 
2997b8d26b3bSNicholas Bellinger 	return ret;
2998b8d26b3bSNicholas Bellinger }
2999b8d26b3bSNicholas Bellinger 
3000b8d26b3bSNicholas Bellinger static int
3001b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
3002b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
3003b8d26b3bSNicholas Bellinger {
3004b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
3005b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
3006b8d26b3bSNicholas Bellinger 	struct sockaddr *sa;
3007b8d26b3bSNicholas Bellinger 	int ret;
3008b8d26b3bSNicholas Bellinger 
3009b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3010b8d26b3bSNicholas Bellinger 	if (!isert_np) {
3011b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
3012b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3013b8d26b3bSNicholas Bellinger 	}
3014531b7bf4SSagi Grimberg 	sema_init(&isert_np->np_sem, 0);
3015b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
3016b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
3017b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
3018b8d26b3bSNicholas Bellinger 
3019b8d26b3bSNicholas Bellinger 	sa = (struct sockaddr *)ksockaddr;
3020b8d26b3bSNicholas Bellinger 	pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
3021b8d26b3bSNicholas Bellinger 	/*
3022b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3023b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3024b8d26b3bSNicholas Bellinger 	 */
3025b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
3026b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
3027b8d26b3bSNicholas Bellinger 
3028b8d26b3bSNicholas Bellinger 	isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3029b8d26b3bSNicholas Bellinger 				IB_QPT_RC);
3030b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3031b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3032b8d26b3bSNicholas Bellinger 		       PTR_ERR(isert_lid));
3033b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3034b8d26b3bSNicholas Bellinger 		goto out;
3035b8d26b3bSNicholas Bellinger 	}
3036b8d26b3bSNicholas Bellinger 
3037b8d26b3bSNicholas Bellinger 	ret = rdma_bind_addr(isert_lid, sa);
3038b8d26b3bSNicholas Bellinger 	if (ret) {
3039b8d26b3bSNicholas Bellinger 		pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3040b8d26b3bSNicholas Bellinger 		goto out_lid;
3041b8d26b3bSNicholas Bellinger 	}
3042b8d26b3bSNicholas Bellinger 
3043b8d26b3bSNicholas Bellinger 	ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3044b8d26b3bSNicholas Bellinger 	if (ret) {
3045b8d26b3bSNicholas Bellinger 		pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3046b8d26b3bSNicholas Bellinger 		goto out_lid;
3047b8d26b3bSNicholas Bellinger 	}
3048b8d26b3bSNicholas Bellinger 
3049b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
3050b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3051b8d26b3bSNicholas Bellinger 	pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3052b8d26b3bSNicholas Bellinger 
3053b8d26b3bSNicholas Bellinger 	return 0;
3054b8d26b3bSNicholas Bellinger 
3055b8d26b3bSNicholas Bellinger out_lid:
3056b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_lid);
3057b8d26b3bSNicholas Bellinger out:
3058b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3059b8d26b3bSNicholas Bellinger 	return ret;
3060b8d26b3bSNicholas Bellinger }
3061b8d26b3bSNicholas Bellinger 
3062b8d26b3bSNicholas Bellinger static int
3063b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3064b8d26b3bSNicholas Bellinger {
3065b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3066b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3067b8d26b3bSNicholas Bellinger 	int ret;
3068b8d26b3bSNicholas Bellinger 
3069b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3070b8d26b3bSNicholas Bellinger 	cp.responder_resources = isert_conn->responder_resources;
3071b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3072b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3073b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3074b8d26b3bSNicholas Bellinger 
3075b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3076b8d26b3bSNicholas Bellinger 
3077b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3078b8d26b3bSNicholas Bellinger 	if (ret) {
3079b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
3080b8d26b3bSNicholas Bellinger 		return ret;
3081b8d26b3bSNicholas Bellinger 	}
3082b8d26b3bSNicholas Bellinger 
3083b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3084b8d26b3bSNicholas Bellinger 
3085b8d26b3bSNicholas Bellinger 	return 0;
3086b8d26b3bSNicholas Bellinger }
3087b8d26b3bSNicholas Bellinger 
3088b8d26b3bSNicholas Bellinger static int
3089b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3090b8d26b3bSNicholas Bellinger {
3091b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3092b8d26b3bSNicholas Bellinger 	int ret;
3093b8d26b3bSNicholas Bellinger 
3094b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
30956faaa85fSNicholas Bellinger 	/*
30966faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
30976faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
30986faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
30996faaa85fSNicholas Bellinger 	 * into a NOP.
31006faaa85fSNicholas Bellinger 	 */
31016faaa85fSNicholas Bellinger 	if (!login->first_request)
31026faaa85fSNicholas Bellinger 		return 0;
3103b8d26b3bSNicholas Bellinger 
3104b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3105b8d26b3bSNicholas Bellinger 	if (ret)
3106b8d26b3bSNicholas Bellinger 		return ret;
3107b8d26b3bSNicholas Bellinger 
3108b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3109b8d26b3bSNicholas Bellinger 	return 0;
3110b8d26b3bSNicholas Bellinger }
3111b8d26b3bSNicholas Bellinger 
3112b8d26b3bSNicholas Bellinger static void
3113b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3114b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3115b8d26b3bSNicholas Bellinger {
3116b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3117b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3118b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
3119b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
3120b8d26b3bSNicholas Bellinger 
3121b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3122b8d26b3bSNicholas Bellinger 
3123b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
3124b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3125b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3126b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3127b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
3128b8d26b3bSNicholas Bellinger 
3129b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3130b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3131b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3132b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
3133b8d26b3bSNicholas Bellinger 	} else {
3134b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3135b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
3136b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3137b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
3138b8d26b3bSNicholas Bellinger 
3139b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3140b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
3141b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3142b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
3143b8d26b3bSNicholas Bellinger 	}
3144b8d26b3bSNicholas Bellinger }
3145b8d26b3bSNicholas Bellinger 
3146b8d26b3bSNicholas Bellinger static int
3147b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3148b8d26b3bSNicholas Bellinger {
3149b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3150b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3151b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
3152b8d26b3bSNicholas Bellinger 
3153b8d26b3bSNicholas Bellinger accept_wait:
3154531b7bf4SSagi Grimberg 	ret = down_interruptible(&isert_np->np_sem);
3155b8d26b3bSNicholas Bellinger 	if (max_accept > 5)
3156b8d26b3bSNicholas Bellinger 		return -ENODEV;
3157b8d26b3bSNicholas Bellinger 
3158b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3159b8d26b3bSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
3160b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
3161531b7bf4SSagi Grimberg 		pr_debug("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
3162b8d26b3bSNicholas Bellinger 		return -ENODEV;
3163b8d26b3bSNicholas Bellinger 	}
3164b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3165b8d26b3bSNicholas Bellinger 
3166b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3167b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
3168b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
3169b8d26b3bSNicholas Bellinger 		max_accept++;
3170b8d26b3bSNicholas Bellinger 		goto accept_wait;
3171b8d26b3bSNicholas Bellinger 	}
3172b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
3173b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
3174b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
3175b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3176b8d26b3bSNicholas Bellinger 
3177b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3178b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3179b8d26b3bSNicholas Bellinger 	max_accept = 0;
3180b8d26b3bSNicholas Bellinger 
3181b8d26b3bSNicholas Bellinger 	ret = isert_rdma_post_recvl(isert_conn);
3182b8d26b3bSNicholas Bellinger 	if (ret)
3183b8d26b3bSNicholas Bellinger 		return ret;
3184b8d26b3bSNicholas Bellinger 
3185b8d26b3bSNicholas Bellinger 	ret = isert_rdma_accept(isert_conn);
3186b8d26b3bSNicholas Bellinger 	if (ret)
3187b8d26b3bSNicholas Bellinger 		return ret;
3188b8d26b3bSNicholas Bellinger 
3189b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3190b8d26b3bSNicholas Bellinger 
3191b8d26b3bSNicholas Bellinger 	pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3192b8d26b3bSNicholas Bellinger 	return 0;
3193b8d26b3bSNicholas Bellinger }
3194b8d26b3bSNicholas Bellinger 
3195b8d26b3bSNicholas Bellinger static void
3196b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3197b8d26b3bSNicholas Bellinger {
3198b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3199b8d26b3bSNicholas Bellinger 
3200b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_np->np_cm_id);
3201b8d26b3bSNicholas Bellinger 
3202b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3203b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3204b8d26b3bSNicholas Bellinger }
3205b8d26b3bSNicholas Bellinger 
3206defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3207b8d26b3bSNicholas Bellinger {
3208b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3209b8d26b3bSNicholas Bellinger 
3210defd8848SNicholas Bellinger 	pr_debug("isert_wait_conn: Starting \n");
3211b8d26b3bSNicholas Bellinger 	/*
3212b8d26b3bSNicholas Bellinger 	 * Decrement post_send_buf_count for special case when called
3213b8d26b3bSNicholas Bellinger 	 * from isert_do_control_comp() -> iscsit_logout_post_handler()
3214b8d26b3bSNicholas Bellinger 	 */
3215b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
3216b8d26b3bSNicholas Bellinger 	if (isert_conn->logout_posted)
3217b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
3218b8d26b3bSNicholas Bellinger 
3219b2cb9649SNicholas Bellinger 	if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
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 {
3296b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3297b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3298b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
3299b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3300b8d26b3bSNicholas Bellinger }
3301b8d26b3bSNicholas Bellinger 
3302b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3303b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
3304b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3305b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3306b8d26b3bSNicholas Bellinger 
3307b8d26b3bSNicholas Bellinger module_init(isert_init);
3308b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3309