xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 03e7848a64ed535a30f5d7fc6dede2d5a6a2534b)
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>
31b8d26b3bSNicholas Bellinger 
32b8d26b3bSNicholas Bellinger #include "isert_proto.h"
33b8d26b3bSNicholas Bellinger #include "ib_isert.h"
34b8d26b3bSNicholas Bellinger 
35b8d26b3bSNicholas Bellinger #define	ISERT_MAX_CONN		8
36b8d26b3bSNicholas Bellinger #define ISER_MAX_RX_CQ_LEN	(ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37b8d26b3bSNicholas Bellinger #define ISER_MAX_TX_CQ_LEN	(ISERT_QP_MAX_REQ_DTOS  * ISERT_MAX_CONN)
38b8d26b3bSNicholas Bellinger 
39b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
40b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
41b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_rx_wq;
42b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
43b8d26b3bSNicholas Bellinger 
44b8d26b3bSNicholas Bellinger static void
45d40945d8SVu Pham isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
46d40945d8SVu Pham static int
47d40945d8SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
48d40945d8SVu Pham 	       struct isert_rdma_wr *wr);
4959464ef4SVu Pham static void
50a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
5159464ef4SVu Pham static int
52a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
5359464ef4SVu Pham 	       struct isert_rdma_wr *wr);
54f93f3a70SSagi Grimberg static int
55f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
56d40945d8SVu Pham 
57d40945d8SVu Pham static void
58b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
59b8d26b3bSNicholas Bellinger {
60b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)context;
61b8d26b3bSNicholas Bellinger 
62b8d26b3bSNicholas Bellinger 	pr_err("isert_qp_event_callback event: %d\n", e->event);
63b8d26b3bSNicholas Bellinger 	switch (e->event) {
64b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
65b8d26b3bSNicholas Bellinger 		rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
66b8d26b3bSNicholas Bellinger 		break;
67b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
68b8d26b3bSNicholas Bellinger 		pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
69b8d26b3bSNicholas Bellinger 		break;
70b8d26b3bSNicholas Bellinger 	default:
71b8d26b3bSNicholas Bellinger 		break;
72b8d26b3bSNicholas Bellinger 	}
73b8d26b3bSNicholas Bellinger }
74b8d26b3bSNicholas Bellinger 
75b8d26b3bSNicholas Bellinger static int
76b8d26b3bSNicholas Bellinger isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
77b8d26b3bSNicholas Bellinger {
78b8d26b3bSNicholas Bellinger 	int ret;
79b8d26b3bSNicholas Bellinger 
80b8d26b3bSNicholas Bellinger 	ret = ib_query_device(ib_dev, devattr);
81b8d26b3bSNicholas Bellinger 	if (ret) {
82b8d26b3bSNicholas Bellinger 		pr_err("ib_query_device() failed: %d\n", ret);
83b8d26b3bSNicholas Bellinger 		return ret;
84b8d26b3bSNicholas Bellinger 	}
85b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
86b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
87b8d26b3bSNicholas Bellinger 
88b8d26b3bSNicholas Bellinger 	return 0;
89b8d26b3bSNicholas Bellinger }
90b8d26b3bSNicholas Bellinger 
91b8d26b3bSNicholas Bellinger static int
92d3e125daSSagi Grimberg isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id,
93d3e125daSSagi Grimberg 		    u8 protection)
94b8d26b3bSNicholas Bellinger {
95b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
96b8d26b3bSNicholas Bellinger 	struct ib_qp_init_attr attr;
97b8d26b3bSNicholas Bellinger 	int ret, index, min_index = 0;
98b8d26b3bSNicholas Bellinger 
99b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
100b8d26b3bSNicholas Bellinger 	for (index = 0; index < device->cqs_used; index++)
101b8d26b3bSNicholas Bellinger 		if (device->cq_active_qps[index] <
102b8d26b3bSNicholas Bellinger 		    device->cq_active_qps[min_index])
103b8d26b3bSNicholas Bellinger 			min_index = index;
104b8d26b3bSNicholas Bellinger 	device->cq_active_qps[min_index]++;
105b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
106b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
107b8d26b3bSNicholas Bellinger 
108b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
109b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
110b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
111b8d26b3bSNicholas Bellinger 	attr.send_cq = device->dev_tx_cq[min_index];
112b8d26b3bSNicholas Bellinger 	attr.recv_cq = device->dev_rx_cq[min_index];
113b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
114b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
115b8d26b3bSNicholas Bellinger 	/*
116b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
117b8d26b3bSNicholas Bellinger 	 * work-around for RDMA_READ..
118b8d26b3bSNicholas Bellinger 	 */
11959464ef4SVu Pham 	attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
120b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
121b8d26b3bSNicholas Bellinger 
122b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
123b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
124b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
125d3e125daSSagi Grimberg 	if (protection)
126d3e125daSSagi Grimberg 		attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
127b8d26b3bSNicholas Bellinger 
128b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
129b8d26b3bSNicholas Bellinger 		 cma_id->device);
130b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
131b8d26b3bSNicholas Bellinger 		 isert_conn->conn_pd->device);
132b8d26b3bSNicholas Bellinger 
133b8d26b3bSNicholas Bellinger 	ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
134b8d26b3bSNicholas Bellinger 	if (ret) {
135b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_qp failed for cma_id %d\n", ret);
136b8d26b3bSNicholas Bellinger 		return ret;
137b8d26b3bSNicholas Bellinger 	}
138b8d26b3bSNicholas Bellinger 	isert_conn->conn_qp = cma_id->qp;
139b8d26b3bSNicholas Bellinger 	pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
140b8d26b3bSNicholas Bellinger 
141b8d26b3bSNicholas Bellinger 	return 0;
142b8d26b3bSNicholas Bellinger }
143b8d26b3bSNicholas Bellinger 
144b8d26b3bSNicholas Bellinger static void
145b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
146b8d26b3bSNicholas Bellinger {
147b8d26b3bSNicholas Bellinger 	pr_debug("isert_cq_event_callback event: %d\n", e->event);
148b8d26b3bSNicholas Bellinger }
149b8d26b3bSNicholas Bellinger 
150b8d26b3bSNicholas Bellinger static int
151b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
152b8d26b3bSNicholas Bellinger {
153b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
154b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
155b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
156b8d26b3bSNicholas Bellinger 	u64 dma_addr;
157b8d26b3bSNicholas Bellinger 	int i, j;
158b8d26b3bSNicholas Bellinger 
159b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
160b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
161b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
162b8d26b3bSNicholas Bellinger 		goto fail;
163b8d26b3bSNicholas Bellinger 
164b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
165b8d26b3bSNicholas Bellinger 
166b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
167b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
168b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
169b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
170b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
171b8d26b3bSNicholas Bellinger 
172b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
173b8d26b3bSNicholas Bellinger 
174b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
175b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
176b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
177b8d26b3bSNicholas Bellinger 		rx_sg->lkey = isert_conn->conn_mr->lkey;
178b8d26b3bSNicholas Bellinger 	}
179b8d26b3bSNicholas Bellinger 
180b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
181b8d26b3bSNicholas Bellinger 	return 0;
182b8d26b3bSNicholas Bellinger 
183b8d26b3bSNicholas Bellinger dma_map_fail:
184b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
185b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
186b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
187b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
188b8d26b3bSNicholas Bellinger 	}
189b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
190b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
191b8d26b3bSNicholas Bellinger fail:
192b8d26b3bSNicholas Bellinger 	return -ENOMEM;
193b8d26b3bSNicholas Bellinger }
194b8d26b3bSNicholas Bellinger 
195b8d26b3bSNicholas Bellinger static void
196b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
197b8d26b3bSNicholas Bellinger {
198b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
199b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
200b8d26b3bSNicholas Bellinger 	int i;
201b8d26b3bSNicholas Bellinger 
202b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
203b8d26b3bSNicholas Bellinger 		return;
204b8d26b3bSNicholas Bellinger 
205b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
206b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
207b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
208b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
209b8d26b3bSNicholas Bellinger 	}
210b8d26b3bSNicholas Bellinger 
211b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
212b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
213b8d26b3bSNicholas Bellinger }
214b8d26b3bSNicholas Bellinger 
2152853c2b6SNicholas Bellinger static void isert_cq_tx_work(struct work_struct *);
216b8d26b3bSNicholas Bellinger static void isert_cq_tx_callback(struct ib_cq *, void *);
2172853c2b6SNicholas Bellinger static void isert_cq_rx_work(struct work_struct *);
218b8d26b3bSNicholas Bellinger static void isert_cq_rx_callback(struct ib_cq *, void *);
219b8d26b3bSNicholas Bellinger 
220b8d26b3bSNicholas Bellinger static int
221b8d26b3bSNicholas Bellinger isert_create_device_ib_res(struct isert_device *device)
222b8d26b3bSNicholas Bellinger {
223b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = device->ib_device;
224b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
22559464ef4SVu Pham 	struct ib_device_attr *dev_attr;
226b8d26b3bSNicholas Bellinger 	int ret = 0, i, j;
227b8d26b3bSNicholas Bellinger 
22859464ef4SVu Pham 	dev_attr = &device->dev_attr;
22959464ef4SVu Pham 	ret = isert_query_device(ib_dev, dev_attr);
23059464ef4SVu Pham 	if (ret)
23159464ef4SVu Pham 		return ret;
23259464ef4SVu Pham 
233d40945d8SVu Pham 	/* asign function handlers */
23459464ef4SVu Pham 	if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
235a3a5a826SSagi Grimberg 		device->use_fastreg = 1;
236a3a5a826SSagi Grimberg 		device->reg_rdma_mem = isert_reg_rdma;
237a3a5a826SSagi Grimberg 		device->unreg_rdma_mem = isert_unreg_rdma;
23859464ef4SVu Pham 	} else {
239a3a5a826SSagi Grimberg 		device->use_fastreg = 0;
240d40945d8SVu Pham 		device->reg_rdma_mem = isert_map_rdma;
241d40945d8SVu Pham 		device->unreg_rdma_mem = isert_unmap_cmd;
24259464ef4SVu Pham 	}
243d40945d8SVu Pham 
244d3e125daSSagi Grimberg 	/* Check signature cap */
245d3e125daSSagi Grimberg 	device->pi_capable = dev_attr->device_cap_flags &
246d3e125daSSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
247d3e125daSSagi Grimberg 
248b8d26b3bSNicholas Bellinger 	device->cqs_used = min_t(int, num_online_cpus(),
249b8d26b3bSNicholas Bellinger 				 device->ib_device->num_comp_vectors);
250b8d26b3bSNicholas Bellinger 	device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
251a3a5a826SSagi Grimberg 	pr_debug("Using %d CQs, device %s supports %d vectors support "
252d3e125daSSagi Grimberg 		 "Fast registration %d pi_capable %d\n",
253b8d26b3bSNicholas Bellinger 		 device->cqs_used, device->ib_device->name,
254d3e125daSSagi Grimberg 		 device->ib_device->num_comp_vectors, device->use_fastreg,
255d3e125daSSagi Grimberg 		 device->pi_capable);
256b8d26b3bSNicholas Bellinger 	device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
257b8d26b3bSNicholas Bellinger 				device->cqs_used, GFP_KERNEL);
258b8d26b3bSNicholas Bellinger 	if (!device->cq_desc) {
259b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate device->cq_desc\n");
260b8d26b3bSNicholas Bellinger 		return -ENOMEM;
261b8d26b3bSNicholas Bellinger 	}
262b8d26b3bSNicholas Bellinger 	cq_desc = device->cq_desc;
263b8d26b3bSNicholas Bellinger 
264b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
265b8d26b3bSNicholas Bellinger 		cq_desc[i].device = device;
266b8d26b3bSNicholas Bellinger 		cq_desc[i].cq_index = i;
267b8d26b3bSNicholas Bellinger 
2682853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
269b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
270b8d26b3bSNicholas Bellinger 						isert_cq_rx_callback,
271b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
272b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
273b8d26b3bSNicholas Bellinger 						ISER_MAX_RX_CQ_LEN, i);
27494a71110SWei Yongjun 		if (IS_ERR(device->dev_rx_cq[i])) {
27594a71110SWei Yongjun 			ret = PTR_ERR(device->dev_rx_cq[i]);
27694a71110SWei Yongjun 			device->dev_rx_cq[i] = NULL;
277b8d26b3bSNicholas Bellinger 			goto out_cq;
27894a71110SWei Yongjun 		}
279b8d26b3bSNicholas Bellinger 
2802853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
281b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
282b8d26b3bSNicholas Bellinger 						isert_cq_tx_callback,
283b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
284b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
285b8d26b3bSNicholas Bellinger 						ISER_MAX_TX_CQ_LEN, i);
28694a71110SWei Yongjun 		if (IS_ERR(device->dev_tx_cq[i])) {
28794a71110SWei Yongjun 			ret = PTR_ERR(device->dev_tx_cq[i]);
28894a71110SWei Yongjun 			device->dev_tx_cq[i] = NULL;
28994a71110SWei Yongjun 			goto out_cq;
29094a71110SWei Yongjun 		}
29194a71110SWei Yongjun 
29294a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
29394a71110SWei Yongjun 		if (ret)
294b8d26b3bSNicholas Bellinger 			goto out_cq;
295b8d26b3bSNicholas Bellinger 
29694a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
29794a71110SWei Yongjun 		if (ret)
298b8d26b3bSNicholas Bellinger 			goto out_cq;
299b8d26b3bSNicholas Bellinger 	}
300b8d26b3bSNicholas Bellinger 
301b8d26b3bSNicholas Bellinger 	return 0;
302b8d26b3bSNicholas Bellinger 
303b8d26b3bSNicholas Bellinger out_cq:
304b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++) {
305b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[j];
306b8d26b3bSNicholas Bellinger 
307b8d26b3bSNicholas Bellinger 		if (device->dev_rx_cq[j]) {
308b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_rx_work);
309b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_rx_cq[j]);
310b8d26b3bSNicholas Bellinger 		}
311b8d26b3bSNicholas Bellinger 		if (device->dev_tx_cq[j]) {
312b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_tx_work);
313b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_tx_cq[j]);
314b8d26b3bSNicholas Bellinger 		}
315b8d26b3bSNicholas Bellinger 	}
316b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
317b8d26b3bSNicholas Bellinger 
318b8d26b3bSNicholas Bellinger 	return ret;
319b8d26b3bSNicholas Bellinger }
320b8d26b3bSNicholas Bellinger 
321b8d26b3bSNicholas Bellinger static void
322b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
323b8d26b3bSNicholas Bellinger {
324b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
325b8d26b3bSNicholas Bellinger 	int i;
326b8d26b3bSNicholas Bellinger 
327b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
328b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[i];
329b8d26b3bSNicholas Bellinger 
330b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_rx_work);
331b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_tx_work);
332b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_rx_cq[i]);
333b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_tx_cq[i]);
334b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = NULL;
335b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = NULL;
336b8d26b3bSNicholas Bellinger 	}
337b8d26b3bSNicholas Bellinger 
338b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
339b8d26b3bSNicholas Bellinger }
340b8d26b3bSNicholas Bellinger 
341b8d26b3bSNicholas Bellinger static void
342b8d26b3bSNicholas Bellinger isert_device_try_release(struct isert_device *device)
343b8d26b3bSNicholas Bellinger {
344b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
345b8d26b3bSNicholas Bellinger 	device->refcount--;
346b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
347b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
348b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
349b8d26b3bSNicholas Bellinger 		kfree(device);
350b8d26b3bSNicholas Bellinger 	}
351b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
352b8d26b3bSNicholas Bellinger }
353b8d26b3bSNicholas Bellinger 
354b8d26b3bSNicholas Bellinger static struct isert_device *
355b8d26b3bSNicholas Bellinger isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
356b8d26b3bSNicholas Bellinger {
357b8d26b3bSNicholas Bellinger 	struct isert_device *device;
358b8d26b3bSNicholas Bellinger 	int ret;
359b8d26b3bSNicholas Bellinger 
360b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
361b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
362b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
363b8d26b3bSNicholas Bellinger 			device->refcount++;
364b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
365b8d26b3bSNicholas Bellinger 			return device;
366b8d26b3bSNicholas Bellinger 		}
367b8d26b3bSNicholas Bellinger 	}
368b8d26b3bSNicholas Bellinger 
369b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
370b8d26b3bSNicholas Bellinger 	if (!device) {
371b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
372b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
373b8d26b3bSNicholas Bellinger 	}
374b8d26b3bSNicholas Bellinger 
375b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
376b8d26b3bSNicholas Bellinger 
377b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
378b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
379b8d26b3bSNicholas Bellinger 	if (ret) {
380b8d26b3bSNicholas Bellinger 		kfree(device);
381b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
382b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
383b8d26b3bSNicholas Bellinger 	}
384b8d26b3bSNicholas Bellinger 
385b8d26b3bSNicholas Bellinger 	device->refcount++;
386b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
387b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
388b8d26b3bSNicholas Bellinger 
389b8d26b3bSNicholas Bellinger 	return device;
390b8d26b3bSNicholas Bellinger }
391b8d26b3bSNicholas Bellinger 
39259464ef4SVu Pham static void
393a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
39459464ef4SVu Pham {
39559464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
39659464ef4SVu Pham 	int i = 0;
39759464ef4SVu Pham 
398a3a5a826SSagi Grimberg 	if (list_empty(&isert_conn->conn_fr_pool))
39959464ef4SVu Pham 		return;
40059464ef4SVu Pham 
401a3a5a826SSagi Grimberg 	pr_debug("Freeing conn %p fastreg pool", isert_conn);
40259464ef4SVu Pham 
40359464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
404a3a5a826SSagi Grimberg 				 &isert_conn->conn_fr_pool, list) {
40559464ef4SVu Pham 		list_del(&fr_desc->list);
40659464ef4SVu Pham 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
40759464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
408d3e125daSSagi Grimberg 		if (fr_desc->pi_ctx) {
409d3e125daSSagi Grimberg 			ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
410d3e125daSSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
411d3e125daSSagi Grimberg 			ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
412d3e125daSSagi Grimberg 			kfree(fr_desc->pi_ctx);
413d3e125daSSagi Grimberg 		}
41459464ef4SVu Pham 		kfree(fr_desc);
41559464ef4SVu Pham 		++i;
41659464ef4SVu Pham 	}
41759464ef4SVu Pham 
418a3a5a826SSagi Grimberg 	if (i < isert_conn->conn_fr_pool_size)
41959464ef4SVu Pham 		pr_warn("Pool still has %d regions registered\n",
420a3a5a826SSagi Grimberg 			isert_conn->conn_fr_pool_size - i);
42159464ef4SVu Pham }
42259464ef4SVu Pham 
42359464ef4SVu Pham static int
424dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
425d3e125daSSagi Grimberg 		     struct fast_reg_descriptor *fr_desc, u8 protection)
426dc87a90fSSagi Grimberg {
427d3e125daSSagi Grimberg 	int ret;
428d3e125daSSagi Grimberg 
429dc87a90fSSagi Grimberg 	fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
430dc87a90fSSagi Grimberg 							 ISCSI_ISER_SG_TABLESIZE);
431dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_frpl)) {
432dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frpl err=%ld\n",
433dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_frpl));
434dc87a90fSSagi Grimberg 		return PTR_ERR(fr_desc->data_frpl);
435dc87a90fSSagi Grimberg 	}
436dc87a90fSSagi Grimberg 
437dc87a90fSSagi Grimberg 	fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
438dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
439dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frmr err=%ld\n",
440dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_mr));
441d3e125daSSagi Grimberg 		ret = PTR_ERR(fr_desc->data_mr);
442d3e125daSSagi Grimberg 		goto err_data_frpl;
443dc87a90fSSagi Grimberg 	}
444dc87a90fSSagi Grimberg 	pr_debug("Create fr_desc %p page_list %p\n",
445dc87a90fSSagi Grimberg 		 fr_desc, fr_desc->data_frpl->page_list);
446d3e125daSSagi Grimberg 	fr_desc->ind |= ISERT_DATA_KEY_VALID;
447dc87a90fSSagi Grimberg 
448d3e125daSSagi Grimberg 	if (protection) {
449d3e125daSSagi Grimberg 		struct ib_mr_init_attr mr_init_attr = {0};
450d3e125daSSagi Grimberg 		struct pi_context *pi_ctx;
451d3e125daSSagi Grimberg 
452d3e125daSSagi Grimberg 		fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
453d3e125daSSagi Grimberg 		if (!fr_desc->pi_ctx) {
454d3e125daSSagi Grimberg 			pr_err("Failed to allocate pi context\n");
455d3e125daSSagi Grimberg 			ret = -ENOMEM;
456d3e125daSSagi Grimberg 			goto err_data_mr;
457d3e125daSSagi Grimberg 		}
458d3e125daSSagi Grimberg 		pi_ctx = fr_desc->pi_ctx;
459d3e125daSSagi Grimberg 
460d3e125daSSagi Grimberg 		pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
461d3e125daSSagi Grimberg 						    ISCSI_ISER_SG_TABLESIZE);
462d3e125daSSagi Grimberg 		if (IS_ERR(pi_ctx->prot_frpl)) {
463d3e125daSSagi Grimberg 			pr_err("Failed to allocate prot frpl err=%ld\n",
464d3e125daSSagi Grimberg 			       PTR_ERR(pi_ctx->prot_frpl));
465d3e125daSSagi Grimberg 			ret = PTR_ERR(pi_ctx->prot_frpl);
466d3e125daSSagi Grimberg 			goto err_pi_ctx;
467d3e125daSSagi Grimberg 		}
468d3e125daSSagi Grimberg 
469d3e125daSSagi Grimberg 		pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
470d3e125daSSagi Grimberg 		if (IS_ERR(pi_ctx->prot_mr)) {
471d3e125daSSagi Grimberg 			pr_err("Failed to allocate prot frmr err=%ld\n",
472d3e125daSSagi Grimberg 			       PTR_ERR(pi_ctx->prot_mr));
473d3e125daSSagi Grimberg 			ret = PTR_ERR(pi_ctx->prot_mr);
474d3e125daSSagi Grimberg 			goto err_prot_frpl;
475d3e125daSSagi Grimberg 		}
476d3e125daSSagi Grimberg 		fr_desc->ind |= ISERT_PROT_KEY_VALID;
477d3e125daSSagi Grimberg 
478d3e125daSSagi Grimberg 		mr_init_attr.max_reg_descriptors = 2;
479d3e125daSSagi Grimberg 		mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
480d3e125daSSagi Grimberg 		pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
481d3e125daSSagi Grimberg 		if (IS_ERR(pi_ctx->sig_mr)) {
482d3e125daSSagi Grimberg 			pr_err("Failed to allocate signature enabled mr err=%ld\n",
483d3e125daSSagi Grimberg 			       PTR_ERR(pi_ctx->sig_mr));
484d3e125daSSagi Grimberg 			ret = PTR_ERR(pi_ctx->sig_mr);
485d3e125daSSagi Grimberg 			goto err_prot_mr;
486d3e125daSSagi Grimberg 		}
487d3e125daSSagi Grimberg 		fr_desc->ind |= ISERT_SIG_KEY_VALID;
488d3e125daSSagi Grimberg 	}
489d3e125daSSagi Grimberg 	fr_desc->ind &= ~ISERT_PROTECTED;
490dc87a90fSSagi Grimberg 
491dc87a90fSSagi Grimberg 	return 0;
492d3e125daSSagi Grimberg err_prot_mr:
493d3e125daSSagi Grimberg 	ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
494d3e125daSSagi Grimberg err_prot_frpl:
495d3e125daSSagi Grimberg 	ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
496d3e125daSSagi Grimberg err_pi_ctx:
497d3e125daSSagi Grimberg 	kfree(fr_desc->pi_ctx);
498d3e125daSSagi Grimberg err_data_mr:
499d3e125daSSagi Grimberg 	ib_dereg_mr(fr_desc->data_mr);
500d3e125daSSagi Grimberg err_data_frpl:
501d3e125daSSagi Grimberg 	ib_free_fast_reg_page_list(fr_desc->data_frpl);
502d3e125daSSagi Grimberg 
503d3e125daSSagi Grimberg 	return ret;
504dc87a90fSSagi Grimberg }
505dc87a90fSSagi Grimberg 
506dc87a90fSSagi Grimberg static int
507d3e125daSSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
50859464ef4SVu Pham {
50959464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
51059464ef4SVu Pham 	struct isert_device *device = isert_conn->conn_device;
511f46d6a8aSNicholas Bellinger 	struct se_session *se_sess = isert_conn->conn->sess->se_sess;
512f46d6a8aSNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
513f46d6a8aSNicholas Bellinger 	int i, ret, tag_num;
514f46d6a8aSNicholas Bellinger 	/*
515f46d6a8aSNicholas Bellinger 	 * Setup the number of FRMRs based upon the number of tags
516f46d6a8aSNicholas Bellinger 	 * available to session in iscsi_target_locate_portal().
517f46d6a8aSNicholas Bellinger 	 */
518f46d6a8aSNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
519f46d6a8aSNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
52059464ef4SVu Pham 
521a3a5a826SSagi Grimberg 	isert_conn->conn_fr_pool_size = 0;
522f46d6a8aSNicholas Bellinger 	for (i = 0; i < tag_num; i++) {
52359464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
52459464ef4SVu Pham 		if (!fr_desc) {
52559464ef4SVu Pham 			pr_err("Failed to allocate fast_reg descriptor\n");
52659464ef4SVu Pham 			ret = -ENOMEM;
52759464ef4SVu Pham 			goto err;
52859464ef4SVu Pham 		}
52959464ef4SVu Pham 
530dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
531d3e125daSSagi Grimberg 					   isert_conn->conn_pd, fr_desc,
532d3e125daSSagi Grimberg 					   pi_support);
533dc87a90fSSagi Grimberg 		if (ret) {
534dc87a90fSSagi Grimberg 			pr_err("Failed to create fastreg descriptor err=%d\n",
535dc87a90fSSagi Grimberg 			       ret);
536a80e21b3SNicholas Bellinger 			kfree(fr_desc);
53759464ef4SVu Pham 			goto err;
53859464ef4SVu Pham 		}
53959464ef4SVu Pham 
540a3a5a826SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
541a3a5a826SSagi Grimberg 		isert_conn->conn_fr_pool_size++;
54259464ef4SVu Pham 	}
54359464ef4SVu Pham 
544a3a5a826SSagi Grimberg 	pr_debug("Creating conn %p fastreg pool size=%d",
545a3a5a826SSagi Grimberg 		 isert_conn, isert_conn->conn_fr_pool_size);
54659464ef4SVu Pham 
54759464ef4SVu Pham 	return 0;
54859464ef4SVu Pham 
54959464ef4SVu Pham err:
550a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
55159464ef4SVu Pham 	return ret;
55259464ef4SVu Pham }
55359464ef4SVu Pham 
554b8d26b3bSNicholas Bellinger static int
555b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
556b8d26b3bSNicholas Bellinger {
557b8d26b3bSNicholas Bellinger 	struct iscsi_np *np = cma_id->context;
558b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = np->np_context;
559b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
560b8d26b3bSNicholas Bellinger 	struct isert_device *device;
561b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
562b8d26b3bSNicholas Bellinger 	int ret = 0;
563d3e125daSSagi Grimberg 	u8 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
564b8d26b3bSNicholas Bellinger 
565b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
566b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
567b8d26b3bSNicholas Bellinger 
568b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
569b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
570b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn\n");
571b8d26b3bSNicholas Bellinger 		return -ENOMEM;
572b8d26b3bSNicholas Bellinger 	}
573b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
574b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
575b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
576defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait);
577defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait_comp_err);
578b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
579b8d26b3bSNicholas Bellinger 	kref_get(&isert_conn->conn_kref);
580b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
58159464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
582f46d6a8aSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
583b8d26b3bSNicholas Bellinger 
584b8d26b3bSNicholas Bellinger 	cma_id->context = isert_conn;
585b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
586b8d26b3bSNicholas Bellinger 	isert_conn->responder_resources = event->param.conn.responder_resources;
587b8d26b3bSNicholas Bellinger 	isert_conn->initiator_depth = event->param.conn.initiator_depth;
588b8d26b3bSNicholas Bellinger 	pr_debug("Using responder_resources: %u initiator_depth: %u\n",
589b8d26b3bSNicholas Bellinger 		 isert_conn->responder_resources, isert_conn->initiator_depth);
590b8d26b3bSNicholas Bellinger 
591b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
592b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
593b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
594b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
595b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
596b8d26b3bSNicholas Bellinger 		goto out;
597b8d26b3bSNicholas Bellinger 	}
598b8d26b3bSNicholas Bellinger 
599b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
600b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
601b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
602b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
603b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
604b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
605b8d26b3bSNicholas Bellinger 
606b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
607b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
608b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
609b8d26b3bSNicholas Bellinger 
610b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
611b8d26b3bSNicholas Bellinger 	if (ret) {
612b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
613b8d26b3bSNicholas Bellinger 		       ret);
614b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
615b8d26b3bSNicholas Bellinger 		goto out_login_buf;
616b8d26b3bSNicholas Bellinger 	}
617b8d26b3bSNicholas Bellinger 
618b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
619b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
620b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
621b8d26b3bSNicholas Bellinger 
622b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
623b8d26b3bSNicholas Bellinger 	if (ret) {
624b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
625b8d26b3bSNicholas Bellinger 		       ret);
626b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
627b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
628b8d26b3bSNicholas Bellinger 	}
629b8d26b3bSNicholas Bellinger 
630b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
631b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
632b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
633b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
634b8d26b3bSNicholas Bellinger 	}
635b8d26b3bSNicholas Bellinger 
636b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
637eb6ab132SSagi Grimberg 	isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
638eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_pd)) {
639eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_pd);
640eb6ab132SSagi Grimberg 		pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
641eb6ab132SSagi Grimberg 		       isert_conn, ret);
642eb6ab132SSagi Grimberg 		goto out_pd;
643eb6ab132SSagi Grimberg 	}
644b8d26b3bSNicholas Bellinger 
645eb6ab132SSagi Grimberg 	isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
646eb6ab132SSagi Grimberg 					   IB_ACCESS_LOCAL_WRITE);
647eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_mr)) {
648eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_mr);
649eb6ab132SSagi Grimberg 		pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
650eb6ab132SSagi Grimberg 		       isert_conn, ret);
651eb6ab132SSagi Grimberg 		goto out_mr;
652eb6ab132SSagi Grimberg 	}
653b8d26b3bSNicholas Bellinger 
654d3e125daSSagi Grimberg 	if (pi_support && !device->pi_capable) {
655d3e125daSSagi Grimberg 		pr_err("Protection information requested but not supported\n");
656d3e125daSSagi Grimberg 		ret = -EINVAL;
657d3e125daSSagi Grimberg 		goto out_mr;
658d3e125daSSagi Grimberg 	}
659d3e125daSSagi Grimberg 
660d3e125daSSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
661b8d26b3bSNicholas Bellinger 	if (ret)
662b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
663b8d26b3bSNicholas Bellinger 
664b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
665b8d26b3bSNicholas Bellinger 	list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
666b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
667b8d26b3bSNicholas Bellinger 
668b8d26b3bSNicholas Bellinger 	pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
669b8d26b3bSNicholas Bellinger 	wake_up(&isert_np->np_accept_wq);
670b8d26b3bSNicholas Bellinger 	return 0;
671b8d26b3bSNicholas Bellinger 
672b8d26b3bSNicholas Bellinger out_conn_dev:
673eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
674eb6ab132SSagi Grimberg out_mr:
675eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
676eb6ab132SSagi Grimberg out_pd:
677b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
678b8d26b3bSNicholas Bellinger out_rsp_dma_map:
679b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
680b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
681b8d26b3bSNicholas Bellinger out_req_dma_map:
682b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
683b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
684b8d26b3bSNicholas Bellinger out_login_buf:
685b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
686b8d26b3bSNicholas Bellinger out:
687b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
688b8d26b3bSNicholas Bellinger 	return ret;
689b8d26b3bSNicholas Bellinger }
690b8d26b3bSNicholas Bellinger 
691b8d26b3bSNicholas Bellinger static void
692b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
693b8d26b3bSNicholas Bellinger {
694b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
695b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
696b8d26b3bSNicholas Bellinger 	int cq_index;
697b8d26b3bSNicholas Bellinger 
698b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
699b8d26b3bSNicholas Bellinger 
700a3a5a826SSagi Grimberg 	if (device && device->use_fastreg)
701a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
70259464ef4SVu Pham 
703b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
704b8d26b3bSNicholas Bellinger 		cq_index = ((struct isert_cq_desc *)
705b8d26b3bSNicholas Bellinger 			isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
706b8d26b3bSNicholas Bellinger 		pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
707b8d26b3bSNicholas Bellinger 		isert_conn->conn_device->cq_active_qps[cq_index]--;
708b8d26b3bSNicholas Bellinger 
709b8d26b3bSNicholas Bellinger 		rdma_destroy_qp(isert_conn->conn_cm_id);
710b8d26b3bSNicholas Bellinger 	}
711b8d26b3bSNicholas Bellinger 
712b8d26b3bSNicholas Bellinger 	isert_free_rx_descriptors(isert_conn);
713b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_conn->conn_cm_id);
714b8d26b3bSNicholas Bellinger 
715eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
716eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
717eb6ab132SSagi Grimberg 
718b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
719b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
720b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
721b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
722b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
723b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
724b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
725b8d26b3bSNicholas Bellinger 	}
726b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
727b8d26b3bSNicholas Bellinger 
728b8d26b3bSNicholas Bellinger 	if (device)
729b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
730b8d26b3bSNicholas Bellinger 
731b8d26b3bSNicholas Bellinger 	pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
732b8d26b3bSNicholas Bellinger }
733b8d26b3bSNicholas Bellinger 
734b8d26b3bSNicholas Bellinger static void
735b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
736b8d26b3bSNicholas Bellinger {
737b8d26b3bSNicholas Bellinger 	return;
738b8d26b3bSNicholas Bellinger }
739b8d26b3bSNicholas Bellinger 
740b8d26b3bSNicholas Bellinger static void
741b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
742b8d26b3bSNicholas Bellinger {
743b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
744b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
745b8d26b3bSNicholas Bellinger 
746b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
747b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
748b8d26b3bSNicholas Bellinger 
749b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
750b8d26b3bSNicholas Bellinger }
751b8d26b3bSNicholas Bellinger 
752b8d26b3bSNicholas Bellinger static void
753b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
754b8d26b3bSNicholas Bellinger {
755b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
756b8d26b3bSNicholas Bellinger }
757b8d26b3bSNicholas Bellinger 
758b8d26b3bSNicholas Bellinger static void
759b8d26b3bSNicholas Bellinger isert_disconnect_work(struct work_struct *work)
760b8d26b3bSNicholas Bellinger {
761b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(work,
762b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_logout_work);
763b8d26b3bSNicholas Bellinger 
764b8d26b3bSNicholas Bellinger 	pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
765b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
766defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
767defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
768b8d26b3bSNicholas Bellinger 
769b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
770b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
771b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
772b2cb9649SNicholas Bellinger 		goto wake_up;
773b8d26b3bSNicholas Bellinger 	}
774b2cb9649SNicholas Bellinger 	if (!isert_conn->conn_cm_id) {
775b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
776b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
777b2cb9649SNicholas Bellinger 		return;
778b2cb9649SNicholas Bellinger 	}
779b2cb9649SNicholas Bellinger 	if (!isert_conn->logout_posted) {
780b2cb9649SNicholas Bellinger 		pr_debug("Calling rdma_disconnect for !logout_posted from"
781b2cb9649SNicholas Bellinger 			 " isert_disconnect_work\n");
782b2cb9649SNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
783b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
784b2cb9649SNicholas Bellinger 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
785b2cb9649SNicholas Bellinger 		goto wake_up;
786b2cb9649SNicholas Bellinger 	}
787b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
788b8d26b3bSNicholas Bellinger 
789b2cb9649SNicholas Bellinger wake_up:
790defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait);
791b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
792b8d26b3bSNicholas Bellinger }
793b8d26b3bSNicholas Bellinger 
794b8d26b3bSNicholas Bellinger static void
795b8d26b3bSNicholas Bellinger isert_disconnected_handler(struct rdma_cm_id *cma_id)
796b8d26b3bSNicholas Bellinger {
797b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
798b8d26b3bSNicholas Bellinger 
799b8d26b3bSNicholas Bellinger 	INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
800b8d26b3bSNicholas Bellinger 	schedule_work(&isert_conn->conn_logout_work);
801b8d26b3bSNicholas Bellinger }
802b8d26b3bSNicholas Bellinger 
803b8d26b3bSNicholas Bellinger static int
804b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
805b8d26b3bSNicholas Bellinger {
806b8d26b3bSNicholas Bellinger 	int ret = 0;
807b8d26b3bSNicholas Bellinger 
808b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
809b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
810b8d26b3bSNicholas Bellinger 
811b8d26b3bSNicholas Bellinger 	switch (event->event) {
812b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
813b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
814b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
815b8d26b3bSNicholas Bellinger 		break;
816b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
817b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
818b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
819b8d26b3bSNicholas Bellinger 		break;
820b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DISCONNECTED:
821b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
822b8d26b3bSNicholas Bellinger 		isert_disconnected_handler(cma_id);
823b8d26b3bSNicholas Bellinger 		break;
824b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
825b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ADDR_CHANGE:
826b8d26b3bSNicholas Bellinger 		break;
827b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
828b8d26b3bSNicholas Bellinger 	default:
829b8d26b3bSNicholas Bellinger 		pr_err("Unknown RDMA CMA event: %d\n", event->event);
830b8d26b3bSNicholas Bellinger 		break;
831b8d26b3bSNicholas Bellinger 	}
832b8d26b3bSNicholas Bellinger 
833b8d26b3bSNicholas Bellinger 	if (ret != 0) {
834b8d26b3bSNicholas Bellinger 		pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
835b8d26b3bSNicholas Bellinger 		       event->event, ret);
836b8d26b3bSNicholas Bellinger 		dump_stack();
837b8d26b3bSNicholas Bellinger 	}
838b8d26b3bSNicholas Bellinger 
839b8d26b3bSNicholas Bellinger 	return ret;
840b8d26b3bSNicholas Bellinger }
841b8d26b3bSNicholas Bellinger 
842b8d26b3bSNicholas Bellinger static int
843b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
844b8d26b3bSNicholas Bellinger {
845b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
846b8d26b3bSNicholas Bellinger 	int i, ret;
847b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
848b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
849b8d26b3bSNicholas Bellinger 
850b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
851b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
852b8d26b3bSNicholas Bellinger 		rx_wr->wr_id	= (unsigned long)rx_desc;
853b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
854b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
855b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
856b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
857b8d26b3bSNicholas Bellinger 	}
858b8d26b3bSNicholas Bellinger 
859b8d26b3bSNicholas Bellinger 	rx_wr--;
860b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
861b8d26b3bSNicholas Bellinger 
862b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
863b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
864b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
865b8d26b3bSNicholas Bellinger 	if (ret) {
866b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed with ret: %d\n", ret);
867b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
868b8d26b3bSNicholas Bellinger 	} else {
869b8d26b3bSNicholas Bellinger 		pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
870b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
871b8d26b3bSNicholas Bellinger 	}
872b8d26b3bSNicholas Bellinger 	return ret;
873b8d26b3bSNicholas Bellinger }
874b8d26b3bSNicholas Bellinger 
875b8d26b3bSNicholas Bellinger static int
876b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
877b8d26b3bSNicholas Bellinger {
878b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
879b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
880b8d26b3bSNicholas Bellinger 	int ret;
881b8d26b3bSNicholas Bellinger 
882b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
883b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
884b8d26b3bSNicholas Bellinger 
885b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
886b8d26b3bSNicholas Bellinger 	send_wr.wr_id	= (unsigned long)tx_desc;
887b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
888b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
889b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
890b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
891b8d26b3bSNicholas Bellinger 
892b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
893b8d26b3bSNicholas Bellinger 
894b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
895b8d26b3bSNicholas Bellinger 	if (ret) {
896b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send() failed, ret: %d\n", ret);
897b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
898b8d26b3bSNicholas Bellinger 	}
899b8d26b3bSNicholas Bellinger 
900b8d26b3bSNicholas Bellinger 	return ret;
901b8d26b3bSNicholas Bellinger }
902b8d26b3bSNicholas Bellinger 
903b8d26b3bSNicholas Bellinger static void
904b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
905b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
906b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
907b8d26b3bSNicholas Bellinger {
908b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
909b8d26b3bSNicholas Bellinger 
910b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
911b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
912b8d26b3bSNicholas Bellinger 
913b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
914b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
915b8d26b3bSNicholas Bellinger 
916b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
917b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
918b8d26b3bSNicholas Bellinger 
919b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
920b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
921b8d26b3bSNicholas Bellinger 		pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
922b8d26b3bSNicholas Bellinger 	}
923b8d26b3bSNicholas Bellinger }
924b8d26b3bSNicholas Bellinger 
925b8d26b3bSNicholas Bellinger static int
926b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
927b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
928b8d26b3bSNicholas Bellinger {
929b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
930b8d26b3bSNicholas Bellinger 	u64 dma_addr;
931b8d26b3bSNicholas Bellinger 
932b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
933b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
934b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
935b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error() failed\n");
936b8d26b3bSNicholas Bellinger 		return -ENOMEM;
937b8d26b3bSNicholas Bellinger 	}
938b8d26b3bSNicholas Bellinger 
939b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
940b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
941b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
942b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
943b8d26b3bSNicholas Bellinger 
944b8d26b3bSNicholas Bellinger 	pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
945b8d26b3bSNicholas Bellinger 		 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
946b8d26b3bSNicholas Bellinger 		 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
947b8d26b3bSNicholas Bellinger 
948b8d26b3bSNicholas Bellinger 	return 0;
949b8d26b3bSNicholas Bellinger }
950b8d26b3bSNicholas Bellinger 
951b8d26b3bSNicholas Bellinger static void
95295b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
95395b60f07SNicholas Bellinger 		   struct ib_send_wr *send_wr, bool coalesce)
954b8d26b3bSNicholas Bellinger {
95595b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
95695b60f07SNicholas Bellinger 
957b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
958b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
959b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
96095b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
961b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
96295b60f07SNicholas Bellinger 	/*
96395b60f07SNicholas Bellinger 	 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
96495b60f07SNicholas Bellinger 	 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
96595b60f07SNicholas Bellinger 	 */
966ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
967ebbe4421SNicholas Bellinger 	if (coalesce && isert_conn->state == ISER_CONN_UP &&
96895b60f07SNicholas Bellinger 	    ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
969ebbe4421SNicholas Bellinger 		tx_desc->llnode_active = true;
97095b60f07SNicholas Bellinger 		llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
971ebbe4421SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
97295b60f07SNicholas Bellinger 		return;
97395b60f07SNicholas Bellinger 	}
97495b60f07SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
97595b60f07SNicholas Bellinger 	tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
976ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
97795b60f07SNicholas Bellinger 
97895b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
979b8d26b3bSNicholas Bellinger }
980b8d26b3bSNicholas Bellinger 
981b8d26b3bSNicholas Bellinger static int
982b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
983b8d26b3bSNicholas Bellinger {
984b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
985b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
986b8d26b3bSNicholas Bellinger 	int ret;
987b8d26b3bSNicholas Bellinger 
988b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
989b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
990b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
991b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
992b8d26b3bSNicholas Bellinger 
993b8d26b3bSNicholas Bellinger 	pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
994b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
995b8d26b3bSNicholas Bellinger 
996b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
997b8d26b3bSNicholas Bellinger 	rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
998b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
999b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1000b8d26b3bSNicholas Bellinger 
1001b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1002b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1003b8d26b3bSNicholas Bellinger 	if (ret) {
1004b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed: %d\n", ret);
1005b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1006b8d26b3bSNicholas Bellinger 	}
1007b8d26b3bSNicholas Bellinger 
1008b8d26b3bSNicholas Bellinger 	pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1009b8d26b3bSNicholas Bellinger 	return ret;
1010b8d26b3bSNicholas Bellinger }
1011b8d26b3bSNicholas Bellinger 
1012b8d26b3bSNicholas Bellinger static int
1013b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1014b8d26b3bSNicholas Bellinger 		   u32 length)
1015b8d26b3bSNicholas Bellinger {
1016b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1017b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1018b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1019b8d26b3bSNicholas Bellinger 	int ret;
1020b8d26b3bSNicholas Bellinger 
1021b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1022b8d26b3bSNicholas Bellinger 
1023b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1024b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1025b8d26b3bSNicholas Bellinger 
1026b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1027b8d26b3bSNicholas Bellinger 
1028b8d26b3bSNicholas Bellinger 	if (length > 0) {
1029b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1030b8d26b3bSNicholas Bellinger 
1031b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1032b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1033b8d26b3bSNicholas Bellinger 
1034b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1035b8d26b3bSNicholas Bellinger 
1036b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1037b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1038b8d26b3bSNicholas Bellinger 
1039b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1040b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
1041b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1042b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1043b8d26b3bSNicholas Bellinger 	}
1044b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1045b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1046f46d6a8aSNicholas Bellinger 			if (isert_conn->conn_device->use_fastreg) {
1047f46d6a8aSNicholas Bellinger 				u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1048f46d6a8aSNicholas Bellinger 
1049f46d6a8aSNicholas Bellinger 				ret = isert_conn_create_fastreg_pool(isert_conn,
1050f46d6a8aSNicholas Bellinger 								     pi_support);
1051f46d6a8aSNicholas Bellinger 				if (ret) {
1052f46d6a8aSNicholas Bellinger 					pr_err("Conn: %p failed to create"
1053f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1054f46d6a8aSNicholas Bellinger 					return ret;
1055f46d6a8aSNicholas Bellinger 				}
1056f46d6a8aSNicholas Bellinger 			}
1057f46d6a8aSNicholas Bellinger 
1058b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1059b8d26b3bSNicholas Bellinger 			if (ret)
1060b8d26b3bSNicholas Bellinger 				return ret;
1061b8d26b3bSNicholas Bellinger 
1062b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1063b8d26b3bSNicholas Bellinger 			if (ret)
1064b8d26b3bSNicholas Bellinger 				return ret;
1065b8d26b3bSNicholas Bellinger 
1066b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_UP;
1067b8d26b3bSNicholas Bellinger 			goto post_send;
1068b8d26b3bSNicholas Bellinger 		}
1069b8d26b3bSNicholas Bellinger 
1070b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1071b8d26b3bSNicholas Bellinger 		if (ret)
1072b8d26b3bSNicholas Bellinger 			return ret;
1073b8d26b3bSNicholas Bellinger 	}
1074b8d26b3bSNicholas Bellinger post_send:
1075b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1076b8d26b3bSNicholas Bellinger 	if (ret)
1077b8d26b3bSNicholas Bellinger 		return ret;
1078b8d26b3bSNicholas Bellinger 
1079b8d26b3bSNicholas Bellinger 	return 0;
1080b8d26b3bSNicholas Bellinger }
1081b8d26b3bSNicholas Bellinger 
1082b8d26b3bSNicholas Bellinger static void
1083b8d26b3bSNicholas Bellinger isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1084b8d26b3bSNicholas Bellinger 		   struct isert_conn *isert_conn)
1085b8d26b3bSNicholas Bellinger {
1086b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1087b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1088b8d26b3bSNicholas Bellinger 	int size;
1089b8d26b3bSNicholas Bellinger 
1090b8d26b3bSNicholas Bellinger 	if (!login) {
1091b8d26b3bSNicholas Bellinger 		pr_err("conn->conn_login is NULL\n");
1092b8d26b3bSNicholas Bellinger 		dump_stack();
1093b8d26b3bSNicholas Bellinger 		return;
1094b8d26b3bSNicholas Bellinger 	}
1095b8d26b3bSNicholas Bellinger 
1096b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1097b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1098b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1099b8d26b3bSNicholas Bellinger 		/*
1100b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1101b8d26b3bSNicholas Bellinger 		 * login request PDU.
1102b8d26b3bSNicholas Bellinger 		 */
1103b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1104b8d26b3bSNicholas Bellinger 		login->current_stage =
1105b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1106b8d26b3bSNicholas Bellinger 			 >> 2;
1107b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1108b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1109b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1110b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1111b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1112b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1113b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1114b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1115b8d26b3bSNicholas Bellinger 	}
1116b8d26b3bSNicholas Bellinger 
1117b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1118b8d26b3bSNicholas Bellinger 
1119b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1120b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1121b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1122b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1123b8d26b3bSNicholas Bellinger 
11246faaa85fSNicholas Bellinger 	if (login->first_request) {
1125b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
11266faaa85fSNicholas Bellinger 		return;
11276faaa85fSNicholas Bellinger 	}
11286faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1129b8d26b3bSNicholas Bellinger }
1130b8d26b3bSNicholas Bellinger 
1131b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1132676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1133b8d26b3bSNicholas Bellinger {
1134b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1135b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1136d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1137b8d26b3bSNicholas Bellinger 
1138676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1139d703ce2fSNicholas Bellinger 	if (!cmd) {
1140d703ce2fSNicholas Bellinger 		pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1141b8d26b3bSNicholas Bellinger 		return NULL;
1142b8d26b3bSNicholas Bellinger 	}
1143d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1144b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1145d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1146b8d26b3bSNicholas Bellinger 
1147d703ce2fSNicholas Bellinger 	return cmd;
1148b8d26b3bSNicholas Bellinger }
1149b8d26b3bSNicholas Bellinger 
1150b8d26b3bSNicholas Bellinger static int
1151b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1152d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1153d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1154b8d26b3bSNicholas Bellinger {
1155b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1156b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1157b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1158b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1159b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1160b8d26b3bSNicholas Bellinger 
1161b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1162b8d26b3bSNicholas Bellinger 	if (rc < 0)
1163b8d26b3bSNicholas Bellinger 		return rc;
1164b8d26b3bSNicholas Bellinger 
1165b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1166b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1167b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1168b8d26b3bSNicholas Bellinger 
1169b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1170b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1171b8d26b3bSNicholas Bellinger 		return 0;
1172b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1173b8d26b3bSNicholas Bellinger 		dump_payload = true;
1174b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1175b8d26b3bSNicholas Bellinger 	}
1176b8d26b3bSNicholas Bellinger 
1177b8d26b3bSNicholas Bellinger 	if (!imm_data)
1178b8d26b3bSNicholas Bellinger 		return 0;
1179b8d26b3bSNicholas Bellinger 
1180b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1181b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1182b8d26b3bSNicholas Bellinger 
1183b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1184b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1185b8d26b3bSNicholas Bellinger 
1186b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1187b8d26b3bSNicholas Bellinger 
1188b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1189b8d26b3bSNicholas Bellinger 
1190b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1191b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1192b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1193b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1194b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1195b8d26b3bSNicholas Bellinger 	}
1196b8d26b3bSNicholas Bellinger 
1197b8d26b3bSNicholas Bellinger sequence_cmd:
1198561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1199b8d26b3bSNicholas Bellinger 
1200b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1201b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
1202b8d26b3bSNicholas Bellinger 
1203b8d26b3bSNicholas Bellinger 	return 0;
1204b8d26b3bSNicholas Bellinger }
1205b8d26b3bSNicholas Bellinger 
1206b8d26b3bSNicholas Bellinger static int
1207b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1208b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1209b8d26b3bSNicholas Bellinger {
1210b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1211b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1212b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1213b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1214b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1215b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1216b8d26b3bSNicholas Bellinger 
1217b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1218b8d26b3bSNicholas Bellinger 	if (rc < 0)
1219b8d26b3bSNicholas Bellinger 		return rc;
1220b8d26b3bSNicholas Bellinger 	else if (!cmd)
1221b8d26b3bSNicholas Bellinger 		return 0;
1222b8d26b3bSNicholas Bellinger 	/*
1223b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1224b8d26b3bSNicholas Bellinger 	 */
1225b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
1226b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
1227b8d26b3bSNicholas Bellinger 		dump_stack();
1228b8d26b3bSNicholas Bellinger 		return -1;
1229b8d26b3bSNicholas Bellinger 	}
1230b8d26b3bSNicholas Bellinger 
1231b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1232b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1233b8d26b3bSNicholas Bellinger 
1234b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1235b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1236b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1237b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1238b8d26b3bSNicholas Bellinger 	/*
1239b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1240b8d26b3bSNicholas Bellinger 	 */
1241b8d26b3bSNicholas Bellinger 	if (page_off) {
1242b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1243b8d26b3bSNicholas Bellinger 		dump_stack();
1244b8d26b3bSNicholas Bellinger 		return -1;
1245b8d26b3bSNicholas Bellinger 	}
1246b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1247b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1248b8d26b3bSNicholas Bellinger 
1249b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1250b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1251b8d26b3bSNicholas Bellinger 
1252b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1253b8d26b3bSNicholas Bellinger 	if (rc < 0)
1254b8d26b3bSNicholas Bellinger 		return rc;
1255b8d26b3bSNicholas Bellinger 
1256b8d26b3bSNicholas Bellinger 	return 0;
1257b8d26b3bSNicholas Bellinger }
1258b8d26b3bSNicholas Bellinger 
1259b8d26b3bSNicholas Bellinger static int
1260778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1261d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1262d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1263778de368SNicholas Bellinger {
1264778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1265778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1266778de368SNicholas Bellinger 	int rc;
1267778de368SNicholas Bellinger 
1268778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1269778de368SNicholas Bellinger 	if (rc < 0)
1270778de368SNicholas Bellinger 		return rc;
1271778de368SNicholas Bellinger 	/*
1272778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1273778de368SNicholas Bellinger 	 */
1274778de368SNicholas Bellinger 
1275778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1276778de368SNicholas Bellinger }
1277778de368SNicholas Bellinger 
1278778de368SNicholas Bellinger static int
1279adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1280d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1281d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1282adb54c29SNicholas Bellinger {
1283adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1284adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1285adb54c29SNicholas Bellinger 	int rc;
1286adb54c29SNicholas Bellinger 	unsigned char *text_in;
1287adb54c29SNicholas Bellinger 
1288adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1289adb54c29SNicholas Bellinger 	if (rc < 0)
1290adb54c29SNicholas Bellinger 		return rc;
1291adb54c29SNicholas Bellinger 
1292adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1293adb54c29SNicholas Bellinger 	if (!text_in) {
1294adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1295adb54c29SNicholas Bellinger 		       payload_length);
1296adb54c29SNicholas Bellinger 		return -ENOMEM;
1297adb54c29SNicholas Bellinger 	}
1298adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1299adb54c29SNicholas Bellinger 
1300adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1301adb54c29SNicholas Bellinger 
1302adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1303adb54c29SNicholas Bellinger }
1304adb54c29SNicholas Bellinger 
1305adb54c29SNicholas Bellinger static int
1306b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1307b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1308b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1309b8d26b3bSNicholas Bellinger {
1310b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1311b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1312ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1313b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1314b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1315b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1316b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1317b8d26b3bSNicholas Bellinger 
1318ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1319ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1320ca40d24eSNicholas Bellinger 		pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1321ca40d24eSNicholas Bellinger 		       " ignoring\n", opcode);
1322ca40d24eSNicholas Bellinger 		return 0;
1323ca40d24eSNicholas Bellinger 	}
1324ca40d24eSNicholas Bellinger 
1325b8d26b3bSNicholas Bellinger 	switch (opcode) {
1326b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1327676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1328b8d26b3bSNicholas Bellinger 		if (!cmd)
1329b8d26b3bSNicholas Bellinger 			break;
1330b8d26b3bSNicholas Bellinger 
1331d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1332b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1333b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1334b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1335b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1336b8d26b3bSNicholas Bellinger 
1337d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1338b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1339b8d26b3bSNicholas Bellinger 		break;
1340b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1341676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1342b8d26b3bSNicholas Bellinger 		if (!cmd)
1343b8d26b3bSNicholas Bellinger 			break;
1344b8d26b3bSNicholas Bellinger 
1345d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1346d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1347778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1348b8d26b3bSNicholas Bellinger 		break;
1349b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1350b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1351b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1352b8d26b3bSNicholas Bellinger 		break;
1353b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1354676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1355b8d26b3bSNicholas Bellinger 		if (!cmd)
1356b8d26b3bSNicholas Bellinger 			break;
1357b8d26b3bSNicholas Bellinger 
1358b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1359b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1360b8d26b3bSNicholas Bellinger 		break;
1361b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1362676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1363b8d26b3bSNicholas Bellinger 		if (!cmd)
1364b8d26b3bSNicholas Bellinger 			break;
1365b8d26b3bSNicholas Bellinger 
1366b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1367b8d26b3bSNicholas Bellinger 		if (ret > 0)
1368b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1369b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1370b8d26b3bSNicholas Bellinger 						    HZ);
1371b8d26b3bSNicholas Bellinger 		break;
1372adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1373676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1374adb54c29SNicholas Bellinger 		if (!cmd)
1375adb54c29SNicholas Bellinger 			break;
1376adb54c29SNicholas Bellinger 
1377d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1378d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1379adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1380adb54c29SNicholas Bellinger 		break;
1381b8d26b3bSNicholas Bellinger 	default:
1382b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1383b8d26b3bSNicholas Bellinger 		dump_stack();
1384b8d26b3bSNicholas Bellinger 		break;
1385b8d26b3bSNicholas Bellinger 	}
1386b8d26b3bSNicholas Bellinger 
1387b8d26b3bSNicholas Bellinger 	return ret;
1388b8d26b3bSNicholas Bellinger }
1389b8d26b3bSNicholas Bellinger 
1390b8d26b3bSNicholas Bellinger static void
1391b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1392b8d26b3bSNicholas Bellinger {
1393b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1394b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1395b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1396b8d26b3bSNicholas Bellinger 	int rc;
1397b8d26b3bSNicholas Bellinger 
1398b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1399b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1400b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1401b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1402b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1403b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1404b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1405b8d26b3bSNicholas Bellinger 		}
1406b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1407b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1408b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1409b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1410b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1411b8d26b3bSNicholas Bellinger 		}
1412b8d26b3bSNicholas Bellinger 
1413b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1414b8d26b3bSNicholas Bellinger 		break;
1415b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1416b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1417b8d26b3bSNicholas Bellinger 		break;
1418b8d26b3bSNicholas Bellinger 	default:
1419b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1420b8d26b3bSNicholas Bellinger 		break;
1421b8d26b3bSNicholas Bellinger 	}
1422b8d26b3bSNicholas Bellinger 
1423b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1424b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1425b8d26b3bSNicholas Bellinger }
1426b8d26b3bSNicholas Bellinger 
1427b8d26b3bSNicholas Bellinger static void
1428b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1429b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1430b8d26b3bSNicholas Bellinger {
1431b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1432b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1433b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1434b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1435b8d26b3bSNicholas Bellinger 
1436b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1437b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1438b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1439b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1440b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1441b8d26b3bSNicholas Bellinger 	} else {
1442b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1443b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1444b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1445b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1446b8d26b3bSNicholas Bellinger 	}
1447b8d26b3bSNicholas Bellinger 
1448b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1449b8d26b3bSNicholas Bellinger 
1450b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1451b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1452b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1453b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1454b8d26b3bSNicholas Bellinger 
1455b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1456b8d26b3bSNicholas Bellinger 		isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1457b8d26b3bSNicholas Bellinger 				   isert_conn);
1458b8d26b3bSNicholas Bellinger 	else
1459b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
1460b8d26b3bSNicholas Bellinger 
1461b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1462b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1463b8d26b3bSNicholas Bellinger 
1464b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1465b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1466b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1467b8d26b3bSNicholas Bellinger 
1468b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1469b8d26b3bSNicholas Bellinger 		return;
1470b8d26b3bSNicholas Bellinger 
1471b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1472b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1473b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1474b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1475b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1476b8d26b3bSNicholas Bellinger 		if (err) {
1477b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1478b8d26b3bSNicholas Bellinger 			       count, err);
1479b8d26b3bSNicholas Bellinger 		}
1480b8d26b3bSNicholas Bellinger 	}
1481b8d26b3bSNicholas Bellinger }
1482b8d26b3bSNicholas Bellinger 
1483e3d7e4c3SSagi Grimberg static int
1484e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1485e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1486e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1487e3d7e4c3SSagi Grimberg {
1488e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1489e3d7e4c3SSagi Grimberg 
1490e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1491e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1492e3d7e4c3SSagi Grimberg 
1493e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1494e3d7e4c3SSagi Grimberg 	data->offset = offset;
1495e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1496e3d7e4c3SSagi Grimberg 
1497e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1498e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1499e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1500e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1501e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1502e3d7e4c3SSagi Grimberg 
1503e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1504e3d7e4c3SSagi Grimberg 					data->dma_dir);
1505e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
1506e3d7e4c3SSagi Grimberg 		pr_err("Cmd: unable to dma map SGs %p\n", sg);
1507e3d7e4c3SSagi Grimberg 		return -EINVAL;
1508e3d7e4c3SSagi Grimberg 	}
1509e3d7e4c3SSagi Grimberg 
1510e3d7e4c3SSagi Grimberg 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1511e3d7e4c3SSagi Grimberg 		 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1512e3d7e4c3SSagi Grimberg 
1513e3d7e4c3SSagi Grimberg 	return 0;
1514e3d7e4c3SSagi Grimberg }
1515e3d7e4c3SSagi Grimberg 
1516e3d7e4c3SSagi Grimberg static void
1517e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1518e3d7e4c3SSagi Grimberg {
1519e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1520e3d7e4c3SSagi Grimberg 
1521e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1522e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1523e3d7e4c3SSagi Grimberg }
1524e3d7e4c3SSagi Grimberg 
1525e3d7e4c3SSagi Grimberg 
1526e3d7e4c3SSagi Grimberg 
1527b8d26b3bSNicholas Bellinger static void
1528b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1529b8d26b3bSNicholas Bellinger {
1530b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1531b8d26b3bSNicholas Bellinger 
153290ecc6e2SVu Pham 	pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1533e3d7e4c3SSagi Grimberg 
1534e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
153590ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1536e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1537b8d26b3bSNicholas Bellinger 	}
1538b8d26b3bSNicholas Bellinger 
153990ecc6e2SVu Pham 	if (wr->send_wr) {
154090ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1541b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1542b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
154390ecc6e2SVu Pham 	}
1544b8d26b3bSNicholas Bellinger 
154590ecc6e2SVu Pham 	if (wr->ib_sge) {
154690ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
154790ecc6e2SVu Pham 		kfree(wr->ib_sge);
154890ecc6e2SVu Pham 		wr->ib_sge = NULL;
154990ecc6e2SVu Pham 	}
1550b8d26b3bSNicholas Bellinger }
1551b8d26b3bSNicholas Bellinger 
1552b8d26b3bSNicholas Bellinger static void
1553a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
155459464ef4SVu Pham {
155559464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
155659464ef4SVu Pham 	LIST_HEAD(unmap_list);
155759464ef4SVu Pham 
1558a3a5a826SSagi Grimberg 	pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
155959464ef4SVu Pham 
156059464ef4SVu Pham 	if (wr->fr_desc) {
1561a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
156259464ef4SVu Pham 			 isert_cmd, wr->fr_desc);
15639e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
15649e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
15659e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
15669e961ae7SSagi Grimberg 		}
156759464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1568a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
156959464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
157059464ef4SVu Pham 		wr->fr_desc = NULL;
157159464ef4SVu Pham 	}
157259464ef4SVu Pham 
1573e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
1574a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1575e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
157659464ef4SVu Pham 	}
157759464ef4SVu Pham 
157859464ef4SVu Pham 	wr->ib_sge = NULL;
157959464ef4SVu Pham 	wr->send_wr = NULL;
158059464ef4SVu Pham }
158159464ef4SVu Pham 
158259464ef4SVu Pham static void
1583*03e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1584b8d26b3bSNicholas Bellinger {
1585d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1586b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1587186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1588d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1589b8d26b3bSNicholas Bellinger 
1590b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1591b8d26b3bSNicholas Bellinger 
1592b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1593b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1594b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1595b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15965159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1597b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1598b8d26b3bSNicholas Bellinger 
1599*03e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1600b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
1601*03e7848aSNicholas Bellinger 			/*
1602*03e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
1603*03e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
1604*03e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
1605*03e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
1606*03e7848aSNicholas Bellinger 			 */
1607*03e7848aSNicholas Bellinger 			if (comp_err &&
1608*03e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1609*03e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
1610*03e7848aSNicholas Bellinger 
1611*03e7848aSNicholas Bellinger 				target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1612*03e7848aSNicholas Bellinger 			}
1613*03e7848aSNicholas Bellinger 		}
1614b8d26b3bSNicholas Bellinger 
1615d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1616186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1617186a9647SNicholas Bellinger 		break;
1618b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1619186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1620186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16215159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1622186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1623186a9647SNicholas Bellinger 
1624b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1625b8d26b3bSNicholas Bellinger 		break;
1626b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1627b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1628adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1629b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1630b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16315159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1632b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1633b8d26b3bSNicholas Bellinger 
1634b8d26b3bSNicholas Bellinger 		/*
1635b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1636b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1637b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1638b8d26b3bSNicholas Bellinger 		 */
1639b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
16403df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
16413df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
16423df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1643b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1644b8d26b3bSNicholas Bellinger 			break;
1645b8d26b3bSNicholas Bellinger 		}
1646b8d26b3bSNicholas Bellinger 		/*
1647b8d26b3bSNicholas Bellinger 		 * Fall-through
1648b8d26b3bSNicholas Bellinger 		 */
1649b8d26b3bSNicholas Bellinger 	default:
1650d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1651b8d26b3bSNicholas Bellinger 		break;
1652b8d26b3bSNicholas Bellinger 	}
1653b8d26b3bSNicholas Bellinger }
1654b8d26b3bSNicholas Bellinger 
1655b8d26b3bSNicholas Bellinger static void
1656b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1657b8d26b3bSNicholas Bellinger {
1658b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1659b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1660b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1661b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1662b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1663b8d26b3bSNicholas Bellinger 	}
1664b8d26b3bSNicholas Bellinger }
1665b8d26b3bSNicholas Bellinger 
1666b8d26b3bSNicholas Bellinger static void
1667b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1668*03e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1669b8d26b3bSNicholas Bellinger {
1670dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1671dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1672dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1673dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1674dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1675b8d26b3bSNicholas Bellinger 	}
1676b8d26b3bSNicholas Bellinger 
1677b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
1678*03e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1679b8d26b3bSNicholas Bellinger }
1680b8d26b3bSNicholas Bellinger 
168196b7973eSSagi Grimberg static int
168296b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1683f93f3a70SSagi Grimberg {
16849e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
168596b7973eSSagi Grimberg 	int ret;
1686f93f3a70SSagi Grimberg 
168796b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
16889e961ae7SSagi Grimberg 	if (ret) {
16899e961ae7SSagi Grimberg 		pr_err("ib_check_mr_status failed, ret %d\n", ret);
16909e961ae7SSagi Grimberg 		goto fail_mr_status;
16919e961ae7SSagi Grimberg 	}
169296b7973eSSagi Grimberg 
16939e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
169496b7973eSSagi Grimberg 		u64 sec_offset_err;
16959e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
16969e961ae7SSagi Grimberg 
16979e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
16989e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
16999e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
17009e961ae7SSagi Grimberg 			break;
17019e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
17029e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
17039e961ae7SSagi Grimberg 			break;
17049e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
17059e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
17069e961ae7SSagi Grimberg 			break;
17079e961ae7SSagi Grimberg 		}
170896b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
170996b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
171096b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
17119e961ae7SSagi Grimberg 
17129e961ae7SSagi Grimberg 		pr_err("isert: PI error found type %d at sector 0x%llx "
17139e961ae7SSagi Grimberg 		       "expected 0x%x vs actual 0x%x\n",
17149e961ae7SSagi Grimberg 		       mr_status.sig_err.err_type,
17159e961ae7SSagi Grimberg 		       (unsigned long long)se_cmd->bad_sector,
17169e961ae7SSagi Grimberg 		       mr_status.sig_err.expected,
17179e961ae7SSagi Grimberg 		       mr_status.sig_err.actual);
17189e961ae7SSagi Grimberg 		ret = 1;
17199e961ae7SSagi Grimberg 	}
17209e961ae7SSagi Grimberg 
17219e961ae7SSagi Grimberg fail_mr_status:
172296b7973eSSagi Grimberg 	return ret;
172396b7973eSSagi Grimberg }
172496b7973eSSagi Grimberg 
172596b7973eSSagi Grimberg static void
172696b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
172796b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
172896b7973eSSagi Grimberg {
172996b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
173096b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
173196b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
173296b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
173396b7973eSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
173496b7973eSSagi Grimberg 	int ret = 0;
173596b7973eSSagi Grimberg 
173696b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
173796b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
173896b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
173996b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
174096b7973eSSagi Grimberg 	}
174196b7973eSSagi Grimberg 
1742f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1743897bb2c9SSagi Grimberg 	wr->send_wr_num = 0;
17449e961ae7SSagi Grimberg 	if (ret)
17459e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
17469e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
17479e961ae7SSagi Grimberg 	else
1748f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1749f93f3a70SSagi Grimberg }
1750f93f3a70SSagi Grimberg 
1751f93f3a70SSagi Grimberg static void
1752b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1753b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1754b8d26b3bSNicholas Bellinger {
1755b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1756d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1757b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
175890ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1759d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
17605bac4b1aSSagi Grimberg 	int ret = 0;
1761b8d26b3bSNicholas Bellinger 
17629e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
176396b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
176496b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
176596b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
17669e961ae7SSagi Grimberg 	}
17679e961ae7SSagi Grimberg 
1768b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1769d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1770e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1771b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1772b8d26b3bSNicholas Bellinger 
177390ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1774b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1775b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1776b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1777b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1778b8d26b3bSNicholas Bellinger 
17795bac4b1aSSagi Grimberg 	if (ret)
17805bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
17815bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
17825bac4b1aSSagi Grimberg 	else
1783b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1784b8d26b3bSNicholas Bellinger }
1785b8d26b3bSNicholas Bellinger 
1786b8d26b3bSNicholas Bellinger static void
1787b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1788b8d26b3bSNicholas Bellinger {
1789b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1790b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1791b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1792b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1793d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1794b8d26b3bSNicholas Bellinger 
1795b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1796b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1797b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1798b8d26b3bSNicholas Bellinger 
1799b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1800b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1801b8d26b3bSNicholas Bellinger 
1802b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1803*03e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1804b8d26b3bSNicholas Bellinger 		break;
1805b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1806b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1807b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1808b8d26b3bSNicholas Bellinger 
1809b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1810*03e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
18113df8f68aSNicholas Bellinger 		break;
1812b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1813b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1814b8d26b3bSNicholas Bellinger 		/*
1815b8d26b3bSNicholas Bellinger 		 * Call atomic_dec(&isert_conn->post_send_buf_count)
1816defd8848SNicholas Bellinger 		 * from isert_wait_conn()
1817b8d26b3bSNicholas Bellinger 		 */
1818b8d26b3bSNicholas Bellinger 		isert_conn->logout_posted = true;
1819b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1820b8d26b3bSNicholas Bellinger 		break;
1821adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1822adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1823adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1824*03e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1825adb54c29SNicholas Bellinger 		break;
1826b8d26b3bSNicholas Bellinger 	default:
1827b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1828b8d26b3bSNicholas Bellinger 		dump_stack();
1829b8d26b3bSNicholas Bellinger 		break;
1830b8d26b3bSNicholas Bellinger 	}
1831b8d26b3bSNicholas Bellinger }
1832b8d26b3bSNicholas Bellinger 
1833b8d26b3bSNicholas Bellinger static void
1834b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1835b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1836b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1837b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1838b8d26b3bSNicholas Bellinger {
1839d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1840b6b87a1dSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1841b8d26b3bSNicholas Bellinger 
1842b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
18433df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1844adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1845adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1846b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1847b8d26b3bSNicholas Bellinger 
1848b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1849b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1850b8d26b3bSNicholas Bellinger 		return;
1851b8d26b3bSNicholas Bellinger 	}
1852897bb2c9SSagi Grimberg 
1853897bb2c9SSagi Grimberg 	/**
1854897bb2c9SSagi Grimberg 	 * If send_wr_num is 0 this means that we got
1855897bb2c9SSagi Grimberg 	 * RDMA completion and we cleared it and we should
1856897bb2c9SSagi Grimberg 	 * simply decrement the response post. else the
1857897bb2c9SSagi Grimberg 	 * response is incorporated in send_wr_num, just
1858897bb2c9SSagi Grimberg 	 * sub it.
1859897bb2c9SSagi Grimberg 	 **/
1860897bb2c9SSagi Grimberg 	if (wr->send_wr_num)
1861897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1862897bb2c9SSagi Grimberg 	else
1863897bb2c9SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1864b8d26b3bSNicholas Bellinger 
1865b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
1866*03e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1867b8d26b3bSNicholas Bellinger }
1868b8d26b3bSNicholas Bellinger 
1869b8d26b3bSNicholas Bellinger static void
187095b60f07SNicholas Bellinger __isert_send_completion(struct iser_tx_desc *tx_desc,
1871b8d26b3bSNicholas Bellinger 		        struct isert_conn *isert_conn)
1872b8d26b3bSNicholas Bellinger {
1873b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1874b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1875b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1876b8d26b3bSNicholas Bellinger 
1877b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1878b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1879b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1880b8d26b3bSNicholas Bellinger 		return;
1881b8d26b3bSNicholas Bellinger 	}
1882b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1883b8d26b3bSNicholas Bellinger 
1884b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1885b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
1886b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1887b8d26b3bSNicholas Bellinger 		dump_stack();
1888b8d26b3bSNicholas Bellinger 		break;
1889b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1890b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1891b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1892b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1893b8d26b3bSNicholas Bellinger 		break;
1894b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1895f93f3a70SSagi Grimberg 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1896897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1897f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
1898b8d26b3bSNicholas Bellinger 		break;
1899b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1900b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1901b8d26b3bSNicholas Bellinger 
1902b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1903b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1904b8d26b3bSNicholas Bellinger 		break;
1905b8d26b3bSNicholas Bellinger 	default:
1906b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1907b8d26b3bSNicholas Bellinger 		dump_stack();
1908b8d26b3bSNicholas Bellinger 		break;
1909b8d26b3bSNicholas Bellinger 	}
1910b8d26b3bSNicholas Bellinger }
1911b8d26b3bSNicholas Bellinger 
1912b8d26b3bSNicholas Bellinger static void
191395b60f07SNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
191495b60f07SNicholas Bellinger 		      struct isert_conn *isert_conn)
191595b60f07SNicholas Bellinger {
191695b60f07SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
191795b60f07SNicholas Bellinger 	struct iser_tx_desc *t;
191895b60f07SNicholas Bellinger 	/*
191995b60f07SNicholas Bellinger 	 * Drain coalesced completion llist starting from comp_llnode_batch
192095b60f07SNicholas Bellinger 	 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
192195b60f07SNicholas Bellinger 	 */
192295b60f07SNicholas Bellinger 	while (llnode) {
192395b60f07SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
192495b60f07SNicholas Bellinger 		llnode = llist_next(llnode);
192595b60f07SNicholas Bellinger 		__isert_send_completion(t, isert_conn);
192695b60f07SNicholas Bellinger 	}
192795b60f07SNicholas Bellinger 	__isert_send_completion(tx_desc, isert_conn);
192895b60f07SNicholas Bellinger }
192995b60f07SNicholas Bellinger 
193095b60f07SNicholas Bellinger static void
1931ebbe4421SNicholas Bellinger isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1932ebbe4421SNicholas Bellinger {
1933ebbe4421SNicholas Bellinger 	struct llist_node *llnode;
1934ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
1935ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
1936ebbe4421SNicholas Bellinger 
1937ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
1938ebbe4421SNicholas Bellinger 	llnode = llist_del_all(&isert_conn->conn_comp_llist);
1939ebbe4421SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
1940ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
1941ebbe4421SNicholas Bellinger 
1942ebbe4421SNicholas Bellinger 	while (llnode) {
1943ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1944ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
1945ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
1946ebbe4421SNicholas Bellinger 
1947897bb2c9SSagi Grimberg 		/**
1948897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
1949897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
1950897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
1951897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
1952897bb2c9SSagi Grimberg 		 * sub it.
1953897bb2c9SSagi Grimberg 		 **/
1954897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
1955897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
1956897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
1957897bb2c9SSagi Grimberg 		else
1958897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
1959*03e7848aSNicholas Bellinger 
1960*03e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
1961ebbe4421SNicholas Bellinger 	}
1962ebbe4421SNicholas Bellinger }
1963ebbe4421SNicholas Bellinger 
1964ebbe4421SNicholas Bellinger static void
1965defd8848SNicholas Bellinger isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1966b8d26b3bSNicholas Bellinger {
1967b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1968b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1969ebbe4421SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
1970ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
1971ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
1972ebbe4421SNicholas Bellinger 
1973ebbe4421SNicholas Bellinger 	while (llnode) {
1974ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1975ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
1976ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
1977ebbe4421SNicholas Bellinger 
1978897bb2c9SSagi Grimberg 		/**
1979897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
1980897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
1981897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
1982897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
1983897bb2c9SSagi Grimberg 		 * sub it.
1984897bb2c9SSagi Grimberg 		 **/
1985897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
1986897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
1987897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
1988897bb2c9SSagi Grimberg 		else
1989897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
1990*03e7848aSNicholas Bellinger 
1991*03e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
1992ebbe4421SNicholas Bellinger 	}
1993ebbe4421SNicholas Bellinger 	tx_desc->comp_llnode_batch = NULL;
1994b8d26b3bSNicholas Bellinger 
1995b8d26b3bSNicholas Bellinger 	if (!isert_cmd)
1996b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1997b8d26b3bSNicholas Bellinger 	else
1998*03e7848aSNicholas Bellinger 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
1999b8d26b3bSNicholas Bellinger }
2000b8d26b3bSNicholas Bellinger 
2001defd8848SNicholas Bellinger static void
2002defd8848SNicholas Bellinger isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2003defd8848SNicholas Bellinger {
2004defd8848SNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2005defd8848SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
2006defd8848SNicholas Bellinger 
2007defd8848SNicholas Bellinger 	if (isert_conn->post_recv_buf_count)
2008defd8848SNicholas Bellinger 		return;
2009defd8848SNicholas Bellinger 
2010ebbe4421SNicholas Bellinger 	isert_cq_drain_comp_llist(isert_conn, ib_dev);
2011ebbe4421SNicholas Bellinger 
2012defd8848SNicholas Bellinger 	if (conn->sess) {
2013defd8848SNicholas Bellinger 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2014defd8848SNicholas Bellinger 		target_wait_for_sess_cmds(conn->sess->se_sess);
2015defd8848SNicholas Bellinger 	}
2016defd8848SNicholas Bellinger 
2017defd8848SNicholas Bellinger 	while (atomic_read(&isert_conn->post_send_buf_count))
2018defd8848SNicholas Bellinger 		msleep(3000);
2019b8d26b3bSNicholas Bellinger 
2020b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2021defd8848SNicholas Bellinger 	isert_conn->state = ISER_CONN_DOWN;
2022b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2023b2cb9649SNicholas Bellinger 
2024defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait_comp_err);
2025b8d26b3bSNicholas Bellinger }
2026b8d26b3bSNicholas Bellinger 
2027b8d26b3bSNicholas Bellinger static void
2028b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
2029b8d26b3bSNicholas Bellinger {
2030b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2031b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
2032b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2033b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2034b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2035b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2036b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
2037b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2038b8d26b3bSNicholas Bellinger 
2039b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2040b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2041b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2042b8d26b3bSNicholas Bellinger 
2043b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2044b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
2045b8d26b3bSNicholas Bellinger 		} else {
2046b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2047b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
2048c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
2049defd8848SNicholas Bellinger 
20509bb4ca68SNicholas Bellinger 			if (wc.wr_id != ISER_FASTREG_LI_WRID) {
2051ebbe4421SNicholas Bellinger 				if (tx_desc->llnode_active)
2052ebbe4421SNicholas Bellinger 					continue;
2053ebbe4421SNicholas Bellinger 
2054b8d26b3bSNicholas Bellinger 				atomic_dec(&isert_conn->post_send_buf_count);
2055defd8848SNicholas Bellinger 				isert_cq_tx_comp_err(tx_desc, isert_conn);
2056b8d26b3bSNicholas Bellinger 			}
2057b8d26b3bSNicholas Bellinger 		}
20589bb4ca68SNicholas Bellinger 	}
2059b8d26b3bSNicholas Bellinger 
2060b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2061b8d26b3bSNicholas Bellinger }
2062b8d26b3bSNicholas Bellinger 
2063b8d26b3bSNicholas Bellinger static void
2064b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
2065b8d26b3bSNicholas Bellinger {
2066b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2067b8d26b3bSNicholas Bellinger 
2068b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2069b8d26b3bSNicholas Bellinger }
2070b8d26b3bSNicholas Bellinger 
2071b8d26b3bSNicholas Bellinger static void
2072b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
2073b8d26b3bSNicholas Bellinger {
2074b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2075b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
2076b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2077b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2078b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2079b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2080b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
2081b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2082b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
2083b8d26b3bSNicholas Bellinger 
2084b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2085b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2086b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2087b8d26b3bSNicholas Bellinger 
2088b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2089b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
2090b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
2091b8d26b3bSNicholas Bellinger 		} else {
2092b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2093c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
2094b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
2095c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
2096c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
2097c5a2adbfSNicholas Bellinger 			}
2098b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
2099defd8848SNicholas Bellinger 			isert_cq_rx_comp_err(isert_conn);
2100b8d26b3bSNicholas Bellinger 		}
2101b8d26b3bSNicholas Bellinger 	}
2102b8d26b3bSNicholas Bellinger 
2103b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2104b8d26b3bSNicholas Bellinger }
2105b8d26b3bSNicholas Bellinger 
2106b8d26b3bSNicholas Bellinger static void
2107b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
2108b8d26b3bSNicholas Bellinger {
2109b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2110b8d26b3bSNicholas Bellinger 
2111b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2112b8d26b3bSNicholas Bellinger }
2113b8d26b3bSNicholas Bellinger 
2114b8d26b3bSNicholas Bellinger static int
2115b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2116b8d26b3bSNicholas Bellinger {
2117b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2118b8d26b3bSNicholas Bellinger 	int ret;
2119b8d26b3bSNicholas Bellinger 
2120b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
2121b8d26b3bSNicholas Bellinger 
2122b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2123b8d26b3bSNicholas Bellinger 			   &wr_failed);
2124b8d26b3bSNicholas Bellinger 	if (ret) {
2125b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
2126b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2127b8d26b3bSNicholas Bellinger 		return ret;
2128b8d26b3bSNicholas Bellinger 	}
2129b8d26b3bSNicholas Bellinger 	return ret;
2130b8d26b3bSNicholas Bellinger }
2131b8d26b3bSNicholas Bellinger 
2132b8d26b3bSNicholas Bellinger static int
2133b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2134b8d26b3bSNicholas Bellinger {
2135d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2136b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2137b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2138b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2139b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2140b8d26b3bSNicholas Bellinger 
2141b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2142b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2143b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2144b8d26b3bSNicholas Bellinger 	/*
2145b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2146b8d26b3bSNicholas Bellinger 	 */
2147b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2148b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2149b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2150b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2151b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2152dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2153b8d26b3bSNicholas Bellinger 
2154b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2155b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2156b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2157b8d26b3bSNicholas Bellinger 
2158b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2159b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2160dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2161b8d26b3bSNicholas Bellinger 
2162dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2163dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2164b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2165b8d26b3bSNicholas Bellinger 
2166dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2167dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2168dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
2169b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2170b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2171b8d26b3bSNicholas Bellinger 	}
2172b8d26b3bSNicholas Bellinger 
217395b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
2174b8d26b3bSNicholas Bellinger 
2175b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2176b8d26b3bSNicholas Bellinger 
2177b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2178b8d26b3bSNicholas Bellinger }
2179b8d26b3bSNicholas Bellinger 
2180131e6abcSNicholas Bellinger static void
2181131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2182131e6abcSNicholas Bellinger {
2183131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2184131e6abcSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2185131e6abcSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2186131e6abcSNicholas Bellinger 
2187131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2188131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2189131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2190131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2191131e6abcSNicholas Bellinger 
2192131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2193131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2194131e6abcSNicholas Bellinger 
2195131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2196131e6abcSNicholas Bellinger }
2197131e6abcSNicholas Bellinger 
2198b8d26b3bSNicholas Bellinger static int
2199b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2200b8d26b3bSNicholas Bellinger 		bool nopout_response)
2201b8d26b3bSNicholas Bellinger {
2202d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2203b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2204b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2205b8d26b3bSNicholas Bellinger 
2206b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2207b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2208b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2209b8d26b3bSNicholas Bellinger 			       nopout_response);
2210b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
221195b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2212b8d26b3bSNicholas Bellinger 
22138b513d0cSMasanari Iida 	pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2214b8d26b3bSNicholas Bellinger 
2215b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2216b8d26b3bSNicholas Bellinger }
2217b8d26b3bSNicholas Bellinger 
2218b8d26b3bSNicholas Bellinger static int
2219b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2220b8d26b3bSNicholas Bellinger {
2221d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2222b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2223b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2224b8d26b3bSNicholas Bellinger 
2225b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2226b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2227b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2228b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
222995b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2230b8d26b3bSNicholas Bellinger 
2231b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2232b8d26b3bSNicholas Bellinger 
2233b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2234b8d26b3bSNicholas Bellinger }
2235b8d26b3bSNicholas Bellinger 
2236b8d26b3bSNicholas Bellinger static int
2237b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2238b8d26b3bSNicholas Bellinger {
2239d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2240b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2241b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2242b8d26b3bSNicholas Bellinger 
2243b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2244b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2245b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2246b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
224795b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2248b8d26b3bSNicholas Bellinger 
2249b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2250b8d26b3bSNicholas Bellinger 
2251b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2252b8d26b3bSNicholas Bellinger }
2253b8d26b3bSNicholas Bellinger 
2254b8d26b3bSNicholas Bellinger static int
2255b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2256b8d26b3bSNicholas Bellinger {
2257d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2258b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2259b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
22603df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
22613df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
22623df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
22633df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2264b8d26b3bSNicholas Bellinger 
2265b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
22663df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2267b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
22683df8f68aSNicholas Bellinger 
22693df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2270dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
22713df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
22723df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2273dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2274dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
22753df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
22763df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
22773df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
22783df8f68aSNicholas Bellinger 
227995b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2280b8d26b3bSNicholas Bellinger 
2281b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2282b8d26b3bSNicholas Bellinger 
2283b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2284b8d26b3bSNicholas Bellinger }
2285b8d26b3bSNicholas Bellinger 
2286b8d26b3bSNicholas Bellinger static int
2287adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2288adb54c29SNicholas Bellinger {
2289d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2290adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2291adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2292adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2293adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2294adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2295adb54c29SNicholas Bellinger 	int rc;
2296adb54c29SNicholas Bellinger 
2297adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2298adb54c29SNicholas Bellinger 	rc = iscsit_build_text_rsp(cmd, conn, hdr);
2299adb54c29SNicholas Bellinger 	if (rc < 0)
2300adb54c29SNicholas Bellinger 		return rc;
2301adb54c29SNicholas Bellinger 
2302adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2303adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2304adb54c29SNicholas Bellinger 
2305adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2306adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2307adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2308adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2309adb54c29SNicholas Bellinger 
2310adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2311adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2312adb54c29SNicholas Bellinger 
2313adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2314adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2315adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
2316adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2317adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2318adb54c29SNicholas Bellinger 	}
231995b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2320adb54c29SNicholas Bellinger 
2321adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2322adb54c29SNicholas Bellinger 
2323adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2324adb54c29SNicholas Bellinger }
2325adb54c29SNicholas Bellinger 
2326adb54c29SNicholas Bellinger static int
2327b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2328b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2329b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2330b8d26b3bSNicholas Bellinger {
2331d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2332b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2333b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2334b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2335b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2336b8d26b3bSNicholas Bellinger 
2337b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2338b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2339b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2340b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2341b8d26b3bSNicholas Bellinger 
2342b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2343b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2344b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2345b8d26b3bSNicholas Bellinger 	/*
2346b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2347b8d26b3bSNicholas Bellinger 	 */
2348b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2349b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2350b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
2351b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
2352b8d26b3bSNicholas Bellinger 
2353b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2354b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2355b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2356b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2357b8d26b3bSNicholas Bellinger 
235890ecc6e2SVu Pham 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
235990ecc6e2SVu Pham 			 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2360b8d26b3bSNicholas Bellinger 		page_off = 0;
2361b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2362b8d26b3bSNicholas Bellinger 		ib_sge++;
2363b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2364b8d26b3bSNicholas Bellinger 	}
2365b8d26b3bSNicholas Bellinger 
2366b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2367b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
2368b8d26b3bSNicholas Bellinger 
2369b8d26b3bSNicholas Bellinger 	return sg_nents;
2370b8d26b3bSNicholas Bellinger }
2371b8d26b3bSNicholas Bellinger 
2372b8d26b3bSNicholas Bellinger static int
237390ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
237490ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2375b8d26b3bSNicholas Bellinger {
2376b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2377d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2378b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2379e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
238090ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2381b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2382e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2383e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2384b8d26b3bSNicholas Bellinger 
238590ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2386b8d26b3bSNicholas Bellinger 
2387e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2388e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2389e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2390e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2391e3d7e4c3SSagi Grimberg 	if (ret)
2392e3d7e4c3SSagi Grimberg 		return ret;
2393b8d26b3bSNicholas Bellinger 
2394e3d7e4c3SSagi Grimberg 	data_left = data->len;
2395e3d7e4c3SSagi Grimberg 	offset = data->offset;
2396b8d26b3bSNicholas Bellinger 
2397e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2398b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
239990ecc6e2SVu Pham 		pr_warn("Unable to allocate ib_sge\n");
2400b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2401e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2402b8d26b3bSNicholas Bellinger 	}
240390ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2404b8d26b3bSNicholas Bellinger 
2405e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2406b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2407b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2408b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
240990ecc6e2SVu Pham 		pr_debug("Unable to allocate wr->send_wr\n");
2410b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2411e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2412b8d26b3bSNicholas Bellinger 	}
2413b8d26b3bSNicholas Bellinger 
2414b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2415b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2416b8d26b3bSNicholas Bellinger 
2417b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2418b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2419b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2420b8d26b3bSNicholas Bellinger 
2421b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
242290ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
242390ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2424b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2425b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
242690ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
242790ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
242890ecc6e2SVu Pham 			else
242990ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
243090ecc6e2SVu Pham 		} else {
243190ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
243290ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
243390ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
243490ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
243590ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
243690ecc6e2SVu Pham 			else
243790ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
243890ecc6e2SVu Pham 		}
2439b8d26b3bSNicholas Bellinger 
2440b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2441b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2442b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2443b8d26b3bSNicholas Bellinger 
2444b8d26b3bSNicholas Bellinger 		offset += data_len;
244590ecc6e2SVu Pham 		va_offset += data_len;
2446b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2447b8d26b3bSNicholas Bellinger 	}
244890ecc6e2SVu Pham 
244990ecc6e2SVu Pham 	return 0;
2450e3d7e4c3SSagi Grimberg unmap_cmd:
2451e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2452e3d7e4c3SSagi Grimberg 
245390ecc6e2SVu Pham 	return ret;
245490ecc6e2SVu Pham }
245590ecc6e2SVu Pham 
245690ecc6e2SVu Pham static int
245759464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
245859464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
245959464ef4SVu Pham {
246059464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
246159464ef4SVu Pham 	struct scatterlist *tmp_sg;
246259464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
246359464ef4SVu Pham 
246459464ef4SVu Pham 	n_pages = 0;
246559464ef4SVu Pham 	new_chunk = 1;
246659464ef4SVu Pham 	last_ent = sg_nents - 1;
246759464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
246859464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
246959464ef4SVu Pham 		if (new_chunk)
247059464ef4SVu Pham 			chunk_start = start_addr;
247159464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
247259464ef4SVu Pham 
247359464ef4SVu Pham 		pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
247459464ef4SVu Pham 			 i, (unsigned long long)tmp_sg->dma_address,
247559464ef4SVu Pham 			 tmp_sg->length);
247659464ef4SVu Pham 
247759464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
247859464ef4SVu Pham 			new_chunk = 0;
247959464ef4SVu Pham 			continue;
248059464ef4SVu Pham 		}
248159464ef4SVu Pham 		new_chunk = 1;
248259464ef4SVu Pham 
248359464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
248459464ef4SVu Pham 		do {
248559464ef4SVu Pham 			fr_pl[n_pages++] = page;
248659464ef4SVu Pham 			pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
248759464ef4SVu Pham 				 n_pages - 1, page);
248859464ef4SVu Pham 			page += PAGE_SIZE;
248959464ef4SVu Pham 		} while (page < end_addr);
249059464ef4SVu Pham 	}
249159464ef4SVu Pham 
249259464ef4SVu Pham 	return n_pages;
249359464ef4SVu Pham }
249459464ef4SVu Pham 
249559464ef4SVu Pham static int
2496e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2497e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2498e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
24999e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2500e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
250159464ef4SVu Pham {
250259464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
25039e961ae7SSagi Grimberg 	struct ib_mr *mr;
25049e961ae7SSagi Grimberg 	struct ib_fast_reg_page_list *frpl;
250559464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
250659464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
25079bd626e7SSagi Grimberg 	int ret, pagelist_len;
25089bd626e7SSagi Grimberg 	u32 page_off;
250959464ef4SVu Pham 	u8 key;
251059464ef4SVu Pham 
2511e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
2512e3d7e4c3SSagi Grimberg 		sge->lkey = isert_conn->conn_mr->lkey;
2513e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2514e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
25159e961ae7SSagi Grimberg 		pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
25169e961ae7SSagi Grimberg 			 __func__, __LINE__, sge->addr, sge->length,
25179e961ae7SSagi Grimberg 			 sge->lkey);
2518e3d7e4c3SSagi Grimberg 		return 0;
2519e3d7e4c3SSagi Grimberg 	}
2520e3d7e4c3SSagi Grimberg 
25219e961ae7SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID) {
25229e961ae7SSagi Grimberg 		/* Registering data buffer */
25239e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
25249e961ae7SSagi Grimberg 		frpl = fr_desc->data_frpl;
25259e961ae7SSagi Grimberg 	} else {
25269e961ae7SSagi Grimberg 		/* Registering protection buffer */
25279e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
25289e961ae7SSagi Grimberg 		frpl = fr_desc->pi_ctx->prot_frpl;
25299e961ae7SSagi Grimberg 	}
25309e961ae7SSagi Grimberg 
2531e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
253259464ef4SVu Pham 
25339bd626e7SSagi Grimberg 	pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2534e3d7e4c3SSagi Grimberg 		 fr_desc, mem->nents, mem->offset);
253559464ef4SVu Pham 
2536e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
25379e961ae7SSagi Grimberg 					     &frpl->page_list[0]);
253859464ef4SVu Pham 
2539d3e125daSSagi Grimberg 	if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
254059464ef4SVu Pham 		memset(&inv_wr, 0, sizeof(inv_wr));
25419bb4ca68SNicholas Bellinger 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
254259464ef4SVu Pham 		inv_wr.opcode = IB_WR_LOCAL_INV;
25439e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = mr->rkey;
254459464ef4SVu Pham 		wr = &inv_wr;
254559464ef4SVu Pham 		/* Bump the key */
25469e961ae7SSagi Grimberg 		key = (u8)(mr->rkey & 0x000000FF);
25479e961ae7SSagi Grimberg 		ib_update_fast_reg_key(mr, ++key);
254859464ef4SVu Pham 	}
254959464ef4SVu Pham 
255059464ef4SVu Pham 	/* Prepare FASTREG WR */
255159464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
25529bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
255359464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
25549e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
25559e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.page_list = frpl;
255659464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
255759464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2558e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
25599e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.rkey = mr->rkey;
256059464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
256159464ef4SVu Pham 
256259464ef4SVu Pham 	if (!wr)
256359464ef4SVu Pham 		wr = &fr_wr;
256459464ef4SVu Pham 	else
256559464ef4SVu Pham 		wr->next = &fr_wr;
256659464ef4SVu Pham 
256759464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
256859464ef4SVu Pham 	if (ret) {
256959464ef4SVu Pham 		pr_err("fast registration failed, ret:%d\n", ret);
257059464ef4SVu Pham 		return ret;
257159464ef4SVu Pham 	}
25729e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
257359464ef4SVu Pham 
25749e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
25759e961ae7SSagi Grimberg 	sge->addr = frpl->page_list[0] + page_off;
2576e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
257759464ef4SVu Pham 
25789e961ae7SSagi Grimberg 	pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
25799e961ae7SSagi Grimberg 		 __func__, __LINE__, sge->addr, sge->length,
25809e961ae7SSagi Grimberg 		 sge->lkey);
258159464ef4SVu Pham 
258259464ef4SVu Pham 	return ret;
258359464ef4SVu Pham }
258459464ef4SVu Pham 
25859e961ae7SSagi Grimberg static inline enum ib_t10_dif_type
25869e961ae7SSagi Grimberg se2ib_prot_type(enum target_prot_type prot_type)
25879e961ae7SSagi Grimberg {
25889e961ae7SSagi Grimberg 	switch (prot_type) {
25899e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE0_PROT:
25909e961ae7SSagi Grimberg 		return IB_T10DIF_NONE;
25919e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE1_PROT:
25929e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE1;
25939e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE2_PROT:
25949e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE2;
25959e961ae7SSagi Grimberg 	case TARGET_DIF_TYPE3_PROT:
25969e961ae7SSagi Grimberg 		return IB_T10DIF_TYPE3;
25979e961ae7SSagi Grimberg 	default:
25989e961ae7SSagi Grimberg 		return IB_T10DIF_NONE;
25999e961ae7SSagi Grimberg 	}
26009e961ae7SSagi Grimberg }
26019e961ae7SSagi Grimberg 
26029e961ae7SSagi Grimberg static int
26039e961ae7SSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
26049e961ae7SSagi Grimberg {
26059e961ae7SSagi Grimberg 	enum ib_t10_dif_type ib_prot_type = se2ib_prot_type(se_cmd->prot_type);
26069e961ae7SSagi Grimberg 
26079e961ae7SSagi Grimberg 	sig_attrs->mem.sig_type = IB_SIG_TYPE_T10_DIF;
26089e961ae7SSagi Grimberg 	sig_attrs->wire.sig_type = IB_SIG_TYPE_T10_DIF;
26099e961ae7SSagi Grimberg 	sig_attrs->mem.sig.dif.pi_interval =
26109e961ae7SSagi Grimberg 				se_cmd->se_dev->dev_attrib.block_size;
26119e961ae7SSagi Grimberg 	sig_attrs->wire.sig.dif.pi_interval =
26129e961ae7SSagi Grimberg 				se_cmd->se_dev->dev_attrib.block_size;
26139e961ae7SSagi Grimberg 
26149e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
26159e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
26169e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
26179e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = IB_T10DIF_NONE;
26189e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = ib_prot_type;
26199e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
26209e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
26219e961ae7SSagi Grimberg 		break;
26229e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
26239e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
26249e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = ib_prot_type;
26259e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
26269e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
26279e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = IB_T10DIF_NONE;
26289e961ae7SSagi Grimberg 		break;
26299e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
26309e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
26319e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.type = ib_prot_type;
26329e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
26339e961ae7SSagi Grimberg 		sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
26349e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.type = ib_prot_type;
26359e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
26369e961ae7SSagi Grimberg 		sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
26379e961ae7SSagi Grimberg 		break;
26389e961ae7SSagi Grimberg 	default:
26399e961ae7SSagi Grimberg 		pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
26409e961ae7SSagi Grimberg 		return -EINVAL;
26419e961ae7SSagi Grimberg 	}
26429e961ae7SSagi Grimberg 
26439e961ae7SSagi Grimberg 	return 0;
26449e961ae7SSagi Grimberg }
26459e961ae7SSagi Grimberg 
26469e961ae7SSagi Grimberg static inline u8
26479e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
26489e961ae7SSagi Grimberg {
26499e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
26509e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
26519e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
26529e961ae7SSagi Grimberg }
26539e961ae7SSagi Grimberg 
26549e961ae7SSagi Grimberg static int
26559e961ae7SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
26569e961ae7SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc,
26579e961ae7SSagi Grimberg 		 struct ib_sge *data_sge, struct ib_sge *prot_sge,
26589e961ae7SSagi Grimberg 		 struct ib_sge *sig_sge)
26599e961ae7SSagi Grimberg {
26609e961ae7SSagi Grimberg 	struct ib_send_wr sig_wr, inv_wr;
26619e961ae7SSagi Grimberg 	struct ib_send_wr *bad_wr, *wr = NULL;
26629e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
26639e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
26649e961ae7SSagi Grimberg 	int ret;
26659e961ae7SSagi Grimberg 	u32 key;
26669e961ae7SSagi Grimberg 
26679e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
26689e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
26699e961ae7SSagi Grimberg 	if (ret)
26709e961ae7SSagi Grimberg 		goto err;
26719e961ae7SSagi Grimberg 
26729e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
26739e961ae7SSagi Grimberg 
26749e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
26759e961ae7SSagi Grimberg 		memset(&inv_wr, 0, sizeof(inv_wr));
26769e961ae7SSagi Grimberg 		inv_wr.opcode = IB_WR_LOCAL_INV;
2677c2caa207SSagi Grimberg 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
26789e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
26799e961ae7SSagi Grimberg 		wr = &inv_wr;
26809e961ae7SSagi Grimberg 		/* Bump the key */
26819e961ae7SSagi Grimberg 		key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
26829e961ae7SSagi Grimberg 		ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
26839e961ae7SSagi Grimberg 	}
26849e961ae7SSagi Grimberg 
26859e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
26869e961ae7SSagi Grimberg 	sig_wr.opcode = IB_WR_REG_SIG_MR;
2687c2caa207SSagi Grimberg 	sig_wr.wr_id = ISER_FASTREG_LI_WRID;
26889e961ae7SSagi Grimberg 	sig_wr.sg_list = data_sge;
26899e961ae7SSagi Grimberg 	sig_wr.num_sge = 1;
26909e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
26919e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
26929e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
26939e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
26949e961ae7SSagi Grimberg 		sig_wr.wr.sig_handover.prot = prot_sge;
26959e961ae7SSagi Grimberg 
26969e961ae7SSagi Grimberg 	if (!wr)
26979e961ae7SSagi Grimberg 		wr = &sig_wr;
26989e961ae7SSagi Grimberg 	else
26999e961ae7SSagi Grimberg 		wr->next = &sig_wr;
27009e961ae7SSagi Grimberg 
27019e961ae7SSagi Grimberg 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
27029e961ae7SSagi Grimberg 	if (ret) {
27039e961ae7SSagi Grimberg 		pr_err("fast registration failed, ret:%d\n", ret);
27049e961ae7SSagi Grimberg 		goto err;
27059e961ae7SSagi Grimberg 	}
27069e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
27079e961ae7SSagi Grimberg 
27089e961ae7SSagi Grimberg 	sig_sge->lkey = pi_ctx->sig_mr->lkey;
27099e961ae7SSagi Grimberg 	sig_sge->addr = 0;
27109e961ae7SSagi Grimberg 	sig_sge->length = se_cmd->data_length;
27119e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
27129e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
27139e961ae7SSagi Grimberg 		/*
27149e961ae7SSagi Grimberg 		 * We have protection guards on the wire
27159e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
27169e961ae7SSagi Grimberg 		 */
27179e961ae7SSagi Grimberg 		sig_sge->length += se_cmd->prot_length;
27189e961ae7SSagi Grimberg 
27199e961ae7SSagi Grimberg 	pr_debug("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
27209e961ae7SSagi Grimberg 		 sig_sge->addr, sig_sge->length,
27219e961ae7SSagi Grimberg 		 sig_sge->lkey);
27229e961ae7SSagi Grimberg err:
27239e961ae7SSagi Grimberg 	return ret;
27249e961ae7SSagi Grimberg }
27259e961ae7SSagi Grimberg 
272659464ef4SVu Pham static int
2727a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
272859464ef4SVu Pham 	       struct isert_rdma_wr *wr)
272959464ef4SVu Pham {
273059464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
273159464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2732e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
27339e961ae7SSagi Grimberg 	struct ib_sge data_sge;
273459464ef4SVu Pham 	struct ib_send_wr *send_wr;
2735e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2736e3d7e4c3SSagi Grimberg 	u32 offset;
2737e3d7e4c3SSagi Grimberg 	int ret = 0;
273859464ef4SVu Pham 	unsigned long flags;
273959464ef4SVu Pham 
274059464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2741e3d7e4c3SSagi Grimberg 
2742e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2743e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2744e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2745e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2746e3d7e4c3SSagi Grimberg 	if (ret)
2747e3d7e4c3SSagi Grimberg 		return ret;
2748e3d7e4c3SSagi Grimberg 
27499e961ae7SSagi Grimberg 	if (wr->data.dma_nents != 1 ||
27509e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_NORMAL) {
2751e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2752e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2753e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2754e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2755e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2756e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
275759464ef4SVu Pham 	}
275859464ef4SVu Pham 
27599e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
27609e961ae7SSagi Grimberg 				ISERT_DATA_KEY_VALID, &data_sge);
2761e3d7e4c3SSagi Grimberg 	if (ret)
2762e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
276359464ef4SVu Pham 
27649e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
27659e961ae7SSagi Grimberg 		struct ib_sge prot_sge, sig_sge;
27669e961ae7SSagi Grimberg 
27679e961ae7SSagi Grimberg 		if (se_cmd->t_prot_sg) {
27689e961ae7SSagi Grimberg 			ret = isert_map_data_buf(isert_conn, isert_cmd,
27699e961ae7SSagi Grimberg 						 se_cmd->t_prot_sg,
27709e961ae7SSagi Grimberg 						 se_cmd->t_prot_nents,
27719e961ae7SSagi Grimberg 						 se_cmd->prot_length,
27729e961ae7SSagi Grimberg 						 0, wr->iser_ib_op, &wr->prot);
27739e961ae7SSagi Grimberg 			if (ret)
27749e961ae7SSagi Grimberg 				goto unmap_cmd;
27759e961ae7SSagi Grimberg 
27769e961ae7SSagi Grimberg 			ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
27779e961ae7SSagi Grimberg 						ISERT_PROT_KEY_VALID, &prot_sge);
27789e961ae7SSagi Grimberg 			if (ret)
27799e961ae7SSagi Grimberg 				goto unmap_prot_cmd;
27809e961ae7SSagi Grimberg 		}
27819e961ae7SSagi Grimberg 
27829e961ae7SSagi Grimberg 		ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
27839e961ae7SSagi Grimberg 				       &data_sge, &prot_sge, &sig_sge);
27849e961ae7SSagi Grimberg 		if (ret)
27859e961ae7SSagi Grimberg 			goto unmap_prot_cmd;
27869e961ae7SSagi Grimberg 
27879e961ae7SSagi Grimberg 		fr_desc->ind |= ISERT_PROTECTED;
27889e961ae7SSagi Grimberg 		memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
27899e961ae7SSagi Grimberg 	} else
27909e961ae7SSagi Grimberg 		memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
27919e961ae7SSagi Grimberg 
2792e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
279359464ef4SVu Pham 	wr->send_wr_num = 1;
279459464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
279559464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
279659464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
279759464ef4SVu Pham 
279859464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2799e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
280059464ef4SVu Pham 	send_wr->num_sge = 1;
280159464ef4SVu Pham 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
280259464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
280359464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
280459464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
280559464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
28069e961ae7SSagi Grimberg 		send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
28079e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
280859464ef4SVu Pham 	} else {
280959464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
281059464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
281159464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
281259464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
281359464ef4SVu Pham 	}
281459464ef4SVu Pham 
281559464ef4SVu Pham 	return 0;
28169e961ae7SSagi Grimberg unmap_prot_cmd:
28179e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
28189e961ae7SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2819e3d7e4c3SSagi Grimberg unmap_cmd:
2820e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2821e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2822e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2823e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2824e3d7e4c3SSagi Grimberg 	}
2825e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
282659464ef4SVu Pham 
282759464ef4SVu Pham 	return ret;
282859464ef4SVu Pham }
282959464ef4SVu Pham 
283059464ef4SVu Pham static int
283190ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
283290ecc6e2SVu Pham {
283390ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
283459464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
283590ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
283690ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2837d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
283890ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
283990ecc6e2SVu Pham 	int rc;
284090ecc6e2SVu Pham 
284190ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
284290ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
284390ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2844d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
284590ecc6e2SVu Pham 	if (rc) {
284690ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
284790ecc6e2SVu Pham 		return rc;
284890ecc6e2SVu Pham 	}
284990ecc6e2SVu Pham 
28509e961ae7SSagi Grimberg 	if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2851b8d26b3bSNicholas Bellinger 		/*
2852b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2853b8d26b3bSNicholas Bellinger 		 */
28549e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
28559e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
285604d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2857b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2858b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
285995b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
286095b60f07SNicholas Bellinger 				   &isert_cmd->tx_desc.send_wr, true);
28619e961ae7SSagi Grimberg 		isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
2862897bb2c9SSagi Grimberg 		wr->send_wr_num += 1;
28639e961ae7SSagi Grimberg 	}
2864b8d26b3bSNicholas Bellinger 
2865897bb2c9SSagi Grimberg 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2866b8d26b3bSNicholas Bellinger 
2867b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2868b8d26b3bSNicholas Bellinger 	if (rc) {
2869b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2870897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2871b8d26b3bSNicholas Bellinger 	}
28729e961ae7SSagi Grimberg 
28739e961ae7SSagi Grimberg 	if (se_cmd->prot_op == TARGET_PROT_NORMAL)
28749e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
28759e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
28769e961ae7SSagi Grimberg 	else
28779e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
287890ecc6e2SVu Pham 			 isert_cmd);
2879b8d26b3bSNicholas Bellinger 
288090ecc6e2SVu Pham 	return 1;
2881b8d26b3bSNicholas Bellinger }
2882b8d26b3bSNicholas Bellinger 
2883b8d26b3bSNicholas Bellinger static int
2884b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2885b8d26b3bSNicholas Bellinger {
2886b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2887d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2888b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2889b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2890d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
289190ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
289290ecc6e2SVu Pham 	int rc;
2893b8d26b3bSNicholas Bellinger 
289490ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
289590ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2896b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2897d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
289890ecc6e2SVu Pham 	if (rc) {
289990ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
290090ecc6e2SVu Pham 		return rc;
2901b8d26b3bSNicholas Bellinger 	}
2902b8d26b3bSNicholas Bellinger 
2903b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2904b8d26b3bSNicholas Bellinger 
2905b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2906b8d26b3bSNicholas Bellinger 	if (rc) {
2907b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2908b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2909b8d26b3bSNicholas Bellinger 	}
291090ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
291190ecc6e2SVu Pham 		 isert_cmd);
2912b8d26b3bSNicholas Bellinger 
291390ecc6e2SVu Pham 	return 0;
2914b8d26b3bSNicholas Bellinger }
2915b8d26b3bSNicholas Bellinger 
2916b8d26b3bSNicholas Bellinger static int
2917b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2918b8d26b3bSNicholas Bellinger {
2919b8d26b3bSNicholas Bellinger 	int ret;
2920b8d26b3bSNicholas Bellinger 
2921b8d26b3bSNicholas Bellinger 	switch (state) {
2922b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2923b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2924b8d26b3bSNicholas Bellinger 		break;
2925b8d26b3bSNicholas Bellinger 	default:
2926b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
2927b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2928b8d26b3bSNicholas Bellinger 		break;
2929b8d26b3bSNicholas Bellinger 	}
2930b8d26b3bSNicholas Bellinger 
2931b8d26b3bSNicholas Bellinger 	return ret;
2932b8d26b3bSNicholas Bellinger }
2933b8d26b3bSNicholas Bellinger 
2934b8d26b3bSNicholas Bellinger static int
2935b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2936b8d26b3bSNicholas Bellinger {
2937b8d26b3bSNicholas Bellinger 	int ret;
2938b8d26b3bSNicholas Bellinger 
2939b8d26b3bSNicholas Bellinger 	switch (state) {
2940b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2941b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2942b8d26b3bSNicholas Bellinger 		if (!ret) {
2943b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
2944b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
2945b8d26b3bSNicholas Bellinger 		}
2946b8d26b3bSNicholas Bellinger 		break;
2947b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2948b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2949b8d26b3bSNicholas Bellinger 		break;
2950b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2951b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2952b8d26b3bSNicholas Bellinger 		break;
2953b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2954b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2955b8d26b3bSNicholas Bellinger 		break;
2956adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2957adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2958adb54c29SNicholas Bellinger 		break;
2959b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2960b8d26b3bSNicholas Bellinger 		/*
2961b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2962b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2963b8d26b3bSNicholas Bellinger 		 */
2964b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2965b8d26b3bSNicholas Bellinger 		break;
2966b8d26b3bSNicholas Bellinger 	default:
2967b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
2968b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2969b8d26b3bSNicholas Bellinger 		break;
2970b8d26b3bSNicholas Bellinger 	}
2971b8d26b3bSNicholas Bellinger 
2972b8d26b3bSNicholas Bellinger 	return ret;
2973b8d26b3bSNicholas Bellinger }
2974b8d26b3bSNicholas Bellinger 
2975b8d26b3bSNicholas Bellinger static int
2976b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
2977b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
2978b8d26b3bSNicholas Bellinger {
2979b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2980b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2981b8d26b3bSNicholas Bellinger 	struct sockaddr *sa;
2982b8d26b3bSNicholas Bellinger 	int ret;
2983b8d26b3bSNicholas Bellinger 
2984b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2985b8d26b3bSNicholas Bellinger 	if (!isert_np) {
2986b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
2987b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2988b8d26b3bSNicholas Bellinger 	}
2989b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_np->np_accept_wq);
2990b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
2991b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
2992b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
2993b8d26b3bSNicholas Bellinger 
2994b8d26b3bSNicholas Bellinger 	sa = (struct sockaddr *)ksockaddr;
2995b8d26b3bSNicholas Bellinger 	pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2996b8d26b3bSNicholas Bellinger 	/*
2997b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2998b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2999b8d26b3bSNicholas Bellinger 	 */
3000b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
3001b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
3002b8d26b3bSNicholas Bellinger 
3003b8d26b3bSNicholas Bellinger 	isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3004b8d26b3bSNicholas Bellinger 				IB_QPT_RC);
3005b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3006b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3007b8d26b3bSNicholas Bellinger 		       PTR_ERR(isert_lid));
3008b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3009b8d26b3bSNicholas Bellinger 		goto out;
3010b8d26b3bSNicholas Bellinger 	}
3011b8d26b3bSNicholas Bellinger 
3012b8d26b3bSNicholas Bellinger 	ret = rdma_bind_addr(isert_lid, sa);
3013b8d26b3bSNicholas Bellinger 	if (ret) {
3014b8d26b3bSNicholas Bellinger 		pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3015b8d26b3bSNicholas Bellinger 		goto out_lid;
3016b8d26b3bSNicholas Bellinger 	}
3017b8d26b3bSNicholas Bellinger 
3018b8d26b3bSNicholas Bellinger 	ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3019b8d26b3bSNicholas Bellinger 	if (ret) {
3020b8d26b3bSNicholas Bellinger 		pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3021b8d26b3bSNicholas Bellinger 		goto out_lid;
3022b8d26b3bSNicholas Bellinger 	}
3023b8d26b3bSNicholas Bellinger 
3024b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
3025b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3026b8d26b3bSNicholas Bellinger 	pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3027b8d26b3bSNicholas Bellinger 
3028b8d26b3bSNicholas Bellinger 	return 0;
3029b8d26b3bSNicholas Bellinger 
3030b8d26b3bSNicholas Bellinger out_lid:
3031b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_lid);
3032b8d26b3bSNicholas Bellinger out:
3033b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3034b8d26b3bSNicholas Bellinger 	return ret;
3035b8d26b3bSNicholas Bellinger }
3036b8d26b3bSNicholas Bellinger 
3037b8d26b3bSNicholas Bellinger static int
3038b8d26b3bSNicholas Bellinger isert_check_accept_queue(struct isert_np *isert_np)
3039b8d26b3bSNicholas Bellinger {
3040b8d26b3bSNicholas Bellinger 	int empty;
3041b8d26b3bSNicholas Bellinger 
3042b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3043b8d26b3bSNicholas Bellinger 	empty = list_empty(&isert_np->np_accept_list);
3044b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3045b8d26b3bSNicholas Bellinger 
3046b8d26b3bSNicholas Bellinger 	return empty;
3047b8d26b3bSNicholas Bellinger }
3048b8d26b3bSNicholas Bellinger 
3049b8d26b3bSNicholas Bellinger static int
3050b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3051b8d26b3bSNicholas Bellinger {
3052b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3053b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3054b8d26b3bSNicholas Bellinger 	int ret;
3055b8d26b3bSNicholas Bellinger 
3056b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3057b8d26b3bSNicholas Bellinger 	cp.responder_resources = isert_conn->responder_resources;
3058b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3059b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3060b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3061b8d26b3bSNicholas Bellinger 
3062b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3063b8d26b3bSNicholas Bellinger 
3064b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3065b8d26b3bSNicholas Bellinger 	if (ret) {
3066b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
3067b8d26b3bSNicholas Bellinger 		return ret;
3068b8d26b3bSNicholas Bellinger 	}
3069b8d26b3bSNicholas Bellinger 
3070b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3071b8d26b3bSNicholas Bellinger 
3072b8d26b3bSNicholas Bellinger 	return 0;
3073b8d26b3bSNicholas Bellinger }
3074b8d26b3bSNicholas Bellinger 
3075b8d26b3bSNicholas Bellinger static int
3076b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3077b8d26b3bSNicholas Bellinger {
3078b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3079b8d26b3bSNicholas Bellinger 	int ret;
3080b8d26b3bSNicholas Bellinger 
3081b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
30826faaa85fSNicholas Bellinger 	/*
30836faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
30846faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
30856faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
30866faaa85fSNicholas Bellinger 	 * into a NOP.
30876faaa85fSNicholas Bellinger 	 */
30886faaa85fSNicholas Bellinger 	if (!login->first_request)
30896faaa85fSNicholas Bellinger 		return 0;
3090b8d26b3bSNicholas Bellinger 
3091b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3092b8d26b3bSNicholas Bellinger 	if (ret)
3093b8d26b3bSNicholas Bellinger 		return ret;
3094b8d26b3bSNicholas Bellinger 
3095b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3096b8d26b3bSNicholas Bellinger 	return 0;
3097b8d26b3bSNicholas Bellinger }
3098b8d26b3bSNicholas Bellinger 
3099b8d26b3bSNicholas Bellinger static void
3100b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3101b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3102b8d26b3bSNicholas Bellinger {
3103b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3104b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3105b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
3106b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
3107b8d26b3bSNicholas Bellinger 
3108b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3109b8d26b3bSNicholas Bellinger 
3110b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
3111b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3112b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3113b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3114b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
3115b8d26b3bSNicholas Bellinger 
3116b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3117b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3118b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3119b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
3120b8d26b3bSNicholas Bellinger 	} else {
3121b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3122b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
3123b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3124b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
3125b8d26b3bSNicholas Bellinger 
3126b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3127b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
3128b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3129b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
3130b8d26b3bSNicholas Bellinger 	}
3131b8d26b3bSNicholas Bellinger }
3132b8d26b3bSNicholas Bellinger 
3133b8d26b3bSNicholas Bellinger static int
3134b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3135b8d26b3bSNicholas Bellinger {
3136b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3137b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3138b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
3139b8d26b3bSNicholas Bellinger 
3140b8d26b3bSNicholas Bellinger accept_wait:
3141b8d26b3bSNicholas Bellinger 	ret = wait_event_interruptible(isert_np->np_accept_wq,
3142b8d26b3bSNicholas Bellinger 			!isert_check_accept_queue(isert_np) ||
3143b8d26b3bSNicholas Bellinger 			np->np_thread_state == ISCSI_NP_THREAD_RESET);
3144b8d26b3bSNicholas Bellinger 	if (max_accept > 5)
3145b8d26b3bSNicholas Bellinger 		return -ENODEV;
3146b8d26b3bSNicholas Bellinger 
3147b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3148b8d26b3bSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
3149b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
3150b8d26b3bSNicholas Bellinger 		pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
3151b8d26b3bSNicholas Bellinger 		return -ENODEV;
3152b8d26b3bSNicholas Bellinger 	}
3153b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3154b8d26b3bSNicholas Bellinger 
3155b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3156b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
3157b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
3158b8d26b3bSNicholas Bellinger 		max_accept++;
3159b8d26b3bSNicholas Bellinger 		goto accept_wait;
3160b8d26b3bSNicholas Bellinger 	}
3161b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
3162b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
3163b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
3164b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3165b8d26b3bSNicholas Bellinger 
3166b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3167b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3168b8d26b3bSNicholas Bellinger 	max_accept = 0;
3169b8d26b3bSNicholas Bellinger 
3170b8d26b3bSNicholas Bellinger 	ret = isert_rdma_post_recvl(isert_conn);
3171b8d26b3bSNicholas Bellinger 	if (ret)
3172b8d26b3bSNicholas Bellinger 		return ret;
3173b8d26b3bSNicholas Bellinger 
3174b8d26b3bSNicholas Bellinger 	ret = isert_rdma_accept(isert_conn);
3175b8d26b3bSNicholas Bellinger 	if (ret)
3176b8d26b3bSNicholas Bellinger 		return ret;
3177b8d26b3bSNicholas Bellinger 
3178b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3179b8d26b3bSNicholas Bellinger 
3180b8d26b3bSNicholas Bellinger 	pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3181b8d26b3bSNicholas Bellinger 	return 0;
3182b8d26b3bSNicholas Bellinger }
3183b8d26b3bSNicholas Bellinger 
3184b8d26b3bSNicholas Bellinger static void
3185b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3186b8d26b3bSNicholas Bellinger {
3187b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3188b8d26b3bSNicholas Bellinger 
3189b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_np->np_cm_id);
3190b8d26b3bSNicholas Bellinger 
3191b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3192b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3193b8d26b3bSNicholas Bellinger }
3194b8d26b3bSNicholas Bellinger 
3195defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3196b8d26b3bSNicholas Bellinger {
3197b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3198b8d26b3bSNicholas Bellinger 
3199defd8848SNicholas Bellinger 	pr_debug("isert_wait_conn: Starting \n");
3200b8d26b3bSNicholas Bellinger 	/*
3201b8d26b3bSNicholas Bellinger 	 * Decrement post_send_buf_count for special case when called
3202b8d26b3bSNicholas Bellinger 	 * from isert_do_control_comp() -> iscsit_logout_post_handler()
3203b8d26b3bSNicholas Bellinger 	 */
3204b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
3205b8d26b3bSNicholas Bellinger 	if (isert_conn->logout_posted)
3206b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
3207b8d26b3bSNicholas Bellinger 
3208b2cb9649SNicholas Bellinger 	if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
3209defd8848SNicholas Bellinger 		pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
3210b8d26b3bSNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
3211b2cb9649SNicholas Bellinger 	}
3212b8d26b3bSNicholas Bellinger 	/*
3213b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
3214b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3215b8d26b3bSNicholas Bellinger 	 */
3216b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3217b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
3218b2cb9649SNicholas Bellinger 		return;
3219b2cb9649SNicholas Bellinger 	}
3220defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
3221defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
3222b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
3223b2cb9649SNicholas Bellinger 
3224defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait_comp_err);
3225defd8848SNicholas Bellinger 
3226defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait);
3227defd8848SNicholas Bellinger }
3228defd8848SNicholas Bellinger 
3229defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3230defd8848SNicholas Bellinger {
3231defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3232b8d26b3bSNicholas Bellinger 
3233b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3234b8d26b3bSNicholas Bellinger }
3235b8d26b3bSNicholas Bellinger 
3236b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3237b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3238b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3239d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3240b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3241b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3242b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3243b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3244defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3245b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3246b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3247b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3248b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3249b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3250b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3251b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3252b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3253131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3254b8d26b3bSNicholas Bellinger };
3255b8d26b3bSNicholas Bellinger 
3256b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3257b8d26b3bSNicholas Bellinger {
3258b8d26b3bSNicholas Bellinger 	int ret;
3259b8d26b3bSNicholas Bellinger 
3260b8d26b3bSNicholas Bellinger 	isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3261b8d26b3bSNicholas Bellinger 	if (!isert_rx_wq) {
3262b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_rx_wq\n");
3263b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3264b8d26b3bSNicholas Bellinger 	}
3265b8d26b3bSNicholas Bellinger 
3266b8d26b3bSNicholas Bellinger 	isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3267b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
3268b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_comp_wq\n");
3269b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
3270b8d26b3bSNicholas Bellinger 		goto destroy_rx_wq;
3271b8d26b3bSNicholas Bellinger 	}
3272b8d26b3bSNicholas Bellinger 
3273b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
3274b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3275b8d26b3bSNicholas Bellinger 	return 0;
3276b8d26b3bSNicholas Bellinger 
3277b8d26b3bSNicholas Bellinger destroy_rx_wq:
3278b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3279b8d26b3bSNicholas Bellinger 	return ret;
3280b8d26b3bSNicholas Bellinger }
3281b8d26b3bSNicholas Bellinger 
3282b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3283b8d26b3bSNicholas Bellinger {
3284b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3285b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3286b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
3287b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3288b8d26b3bSNicholas Bellinger }
3289b8d26b3bSNicholas Bellinger 
3290b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3291b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
3292b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3293b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3294b8d26b3bSNicholas Bellinger 
3295b8d26b3bSNicholas Bellinger module_init(isert_init);
3296b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3297