xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision f93f3a70da9175b4641f93d466d779675eb83fa2)
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);
54*f93f3a70SSagi Grimberg static int
55*f93f3a70SSagi 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;
51159464ef4SVu Pham 	int i, ret;
51259464ef4SVu Pham 
513a3a5a826SSagi Grimberg 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
514a3a5a826SSagi Grimberg 	isert_conn->conn_fr_pool_size = 0;
51559464ef4SVu Pham 	for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
51659464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
51759464ef4SVu Pham 		if (!fr_desc) {
51859464ef4SVu Pham 			pr_err("Failed to allocate fast_reg descriptor\n");
51959464ef4SVu Pham 			ret = -ENOMEM;
52059464ef4SVu Pham 			goto err;
52159464ef4SVu Pham 		}
52259464ef4SVu Pham 
523dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
524d3e125daSSagi Grimberg 					   isert_conn->conn_pd, fr_desc,
525d3e125daSSagi Grimberg 					   pi_support);
526dc87a90fSSagi Grimberg 		if (ret) {
527dc87a90fSSagi Grimberg 			pr_err("Failed to create fastreg descriptor err=%d\n",
528dc87a90fSSagi Grimberg 			       ret);
529a80e21b3SNicholas Bellinger 			kfree(fr_desc);
53059464ef4SVu Pham 			goto err;
53159464ef4SVu Pham 		}
53259464ef4SVu Pham 
533a3a5a826SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
534a3a5a826SSagi Grimberg 		isert_conn->conn_fr_pool_size++;
53559464ef4SVu Pham 	}
53659464ef4SVu Pham 
537a3a5a826SSagi Grimberg 	pr_debug("Creating conn %p fastreg pool size=%d",
538a3a5a826SSagi Grimberg 		 isert_conn, isert_conn->conn_fr_pool_size);
53959464ef4SVu Pham 
54059464ef4SVu Pham 	return 0;
54159464ef4SVu Pham 
54259464ef4SVu Pham err:
543a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
54459464ef4SVu Pham 	return ret;
54559464ef4SVu Pham }
54659464ef4SVu Pham 
547b8d26b3bSNicholas Bellinger static int
548b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
549b8d26b3bSNicholas Bellinger {
550b8d26b3bSNicholas Bellinger 	struct iscsi_np *np = cma_id->context;
551b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = np->np_context;
552b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
553b8d26b3bSNicholas Bellinger 	struct isert_device *device;
554b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
555b8d26b3bSNicholas Bellinger 	int ret = 0;
556d3e125daSSagi Grimberg 	u8 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
557b8d26b3bSNicholas Bellinger 
558b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
559b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
560b8d26b3bSNicholas Bellinger 
561b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
562b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
563b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn\n");
564b8d26b3bSNicholas Bellinger 		return -ENOMEM;
565b8d26b3bSNicholas Bellinger 	}
566b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
567b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
568b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
569defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait);
570defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait_comp_err);
571b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
572b8d26b3bSNicholas Bellinger 	kref_get(&isert_conn->conn_kref);
573b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
57459464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
575b8d26b3bSNicholas Bellinger 
576b8d26b3bSNicholas Bellinger 	cma_id->context = isert_conn;
577b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
578b8d26b3bSNicholas Bellinger 	isert_conn->responder_resources = event->param.conn.responder_resources;
579b8d26b3bSNicholas Bellinger 	isert_conn->initiator_depth = event->param.conn.initiator_depth;
580b8d26b3bSNicholas Bellinger 	pr_debug("Using responder_resources: %u initiator_depth: %u\n",
581b8d26b3bSNicholas Bellinger 		 isert_conn->responder_resources, isert_conn->initiator_depth);
582b8d26b3bSNicholas Bellinger 
583b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
584b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
585b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
586b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
587b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
588b8d26b3bSNicholas Bellinger 		goto out;
589b8d26b3bSNicholas Bellinger 	}
590b8d26b3bSNicholas Bellinger 
591b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
592b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
593b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
594b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
595b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
596b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
597b8d26b3bSNicholas Bellinger 
598b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
599b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
600b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
601b8d26b3bSNicholas Bellinger 
602b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
603b8d26b3bSNicholas Bellinger 	if (ret) {
604b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
605b8d26b3bSNicholas Bellinger 		       ret);
606b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
607b8d26b3bSNicholas Bellinger 		goto out_login_buf;
608b8d26b3bSNicholas Bellinger 	}
609b8d26b3bSNicholas Bellinger 
610b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
611b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
612b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
613b8d26b3bSNicholas Bellinger 
614b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
615b8d26b3bSNicholas Bellinger 	if (ret) {
616b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
617b8d26b3bSNicholas Bellinger 		       ret);
618b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
619b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
620b8d26b3bSNicholas Bellinger 	}
621b8d26b3bSNicholas Bellinger 
622b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
623b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
624b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
625b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
626b8d26b3bSNicholas Bellinger 	}
627b8d26b3bSNicholas Bellinger 
628b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
629eb6ab132SSagi Grimberg 	isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
630eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_pd)) {
631eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_pd);
632eb6ab132SSagi Grimberg 		pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
633eb6ab132SSagi Grimberg 		       isert_conn, ret);
634eb6ab132SSagi Grimberg 		goto out_pd;
635eb6ab132SSagi Grimberg 	}
636b8d26b3bSNicholas Bellinger 
637eb6ab132SSagi Grimberg 	isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
638eb6ab132SSagi Grimberg 					   IB_ACCESS_LOCAL_WRITE);
639eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_mr)) {
640eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_mr);
641eb6ab132SSagi Grimberg 		pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
642eb6ab132SSagi Grimberg 		       isert_conn, ret);
643eb6ab132SSagi Grimberg 		goto out_mr;
644eb6ab132SSagi Grimberg 	}
645b8d26b3bSNicholas Bellinger 
646d3e125daSSagi Grimberg 	if (pi_support && !device->pi_capable) {
647d3e125daSSagi Grimberg 		pr_err("Protection information requested but not supported\n");
648d3e125daSSagi Grimberg 		ret = -EINVAL;
649d3e125daSSagi Grimberg 		goto out_mr;
650d3e125daSSagi Grimberg 	}
651d3e125daSSagi Grimberg 
652a3a5a826SSagi Grimberg 	if (device->use_fastreg) {
653d3e125daSSagi Grimberg 		ret = isert_conn_create_fastreg_pool(isert_conn, pi_support);
65459464ef4SVu Pham 		if (ret) {
655a3a5a826SSagi Grimberg 			pr_err("Conn: %p failed to create fastreg pool\n",
656a3a5a826SSagi Grimberg 			       isert_conn);
657a3a5a826SSagi Grimberg 			goto out_fastreg;
65859464ef4SVu Pham 		}
65959464ef4SVu Pham 	}
66059464ef4SVu Pham 
661d3e125daSSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
662b8d26b3bSNicholas Bellinger 	if (ret)
663b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
664b8d26b3bSNicholas Bellinger 
665b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
666b8d26b3bSNicholas Bellinger 	list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
667b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
668b8d26b3bSNicholas Bellinger 
669b8d26b3bSNicholas Bellinger 	pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
670b8d26b3bSNicholas Bellinger 	wake_up(&isert_np->np_accept_wq);
671b8d26b3bSNicholas Bellinger 	return 0;
672b8d26b3bSNicholas Bellinger 
673b8d26b3bSNicholas Bellinger out_conn_dev:
674a3a5a826SSagi Grimberg 	if (device->use_fastreg)
675a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
676a3a5a826SSagi Grimberg out_fastreg:
677eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
678eb6ab132SSagi Grimberg out_mr:
679eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
680eb6ab132SSagi Grimberg out_pd:
681b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
682b8d26b3bSNicholas Bellinger out_rsp_dma_map:
683b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
684b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
685b8d26b3bSNicholas Bellinger out_req_dma_map:
686b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
687b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
688b8d26b3bSNicholas Bellinger out_login_buf:
689b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
690b8d26b3bSNicholas Bellinger out:
691b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
692b8d26b3bSNicholas Bellinger 	return ret;
693b8d26b3bSNicholas Bellinger }
694b8d26b3bSNicholas Bellinger 
695b8d26b3bSNicholas Bellinger static void
696b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
697b8d26b3bSNicholas Bellinger {
698b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
699b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
700b8d26b3bSNicholas Bellinger 	int cq_index;
701b8d26b3bSNicholas Bellinger 
702b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
703b8d26b3bSNicholas Bellinger 
704a3a5a826SSagi Grimberg 	if (device && device->use_fastreg)
705a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
70659464ef4SVu Pham 
707b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
708b8d26b3bSNicholas Bellinger 		cq_index = ((struct isert_cq_desc *)
709b8d26b3bSNicholas Bellinger 			isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
710b8d26b3bSNicholas Bellinger 		pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
711b8d26b3bSNicholas Bellinger 		isert_conn->conn_device->cq_active_qps[cq_index]--;
712b8d26b3bSNicholas Bellinger 
713b8d26b3bSNicholas Bellinger 		rdma_destroy_qp(isert_conn->conn_cm_id);
714b8d26b3bSNicholas Bellinger 	}
715b8d26b3bSNicholas Bellinger 
716b8d26b3bSNicholas Bellinger 	isert_free_rx_descriptors(isert_conn);
717b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_conn->conn_cm_id);
718b8d26b3bSNicholas Bellinger 
719eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
720eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
721eb6ab132SSagi Grimberg 
722b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
723b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
724b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
725b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
726b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
727b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
728b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
729b8d26b3bSNicholas Bellinger 	}
730b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
731b8d26b3bSNicholas Bellinger 
732b8d26b3bSNicholas Bellinger 	if (device)
733b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
734b8d26b3bSNicholas Bellinger 
735b8d26b3bSNicholas Bellinger 	pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
736b8d26b3bSNicholas Bellinger }
737b8d26b3bSNicholas Bellinger 
738b8d26b3bSNicholas Bellinger static void
739b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
740b8d26b3bSNicholas Bellinger {
741b8d26b3bSNicholas Bellinger 	return;
742b8d26b3bSNicholas Bellinger }
743b8d26b3bSNicholas Bellinger 
744b8d26b3bSNicholas Bellinger static void
745b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
746b8d26b3bSNicholas Bellinger {
747b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
748b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
749b8d26b3bSNicholas Bellinger 
750b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
751b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
752b8d26b3bSNicholas Bellinger 
753b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
754b8d26b3bSNicholas Bellinger }
755b8d26b3bSNicholas Bellinger 
756b8d26b3bSNicholas Bellinger static void
757b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
758b8d26b3bSNicholas Bellinger {
759b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
760b8d26b3bSNicholas Bellinger }
761b8d26b3bSNicholas Bellinger 
762b8d26b3bSNicholas Bellinger static void
763b8d26b3bSNicholas Bellinger isert_disconnect_work(struct work_struct *work)
764b8d26b3bSNicholas Bellinger {
765b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(work,
766b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_logout_work);
767b8d26b3bSNicholas Bellinger 
768b8d26b3bSNicholas Bellinger 	pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
769b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
770defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
771defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
772b8d26b3bSNicholas Bellinger 
773b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
774b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
775b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
776b2cb9649SNicholas Bellinger 		goto wake_up;
777b8d26b3bSNicholas Bellinger 	}
778b2cb9649SNicholas Bellinger 	if (!isert_conn->conn_cm_id) {
779b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
780b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
781b2cb9649SNicholas Bellinger 		return;
782b2cb9649SNicholas Bellinger 	}
783b2cb9649SNicholas Bellinger 	if (!isert_conn->logout_posted) {
784b2cb9649SNicholas Bellinger 		pr_debug("Calling rdma_disconnect for !logout_posted from"
785b2cb9649SNicholas Bellinger 			 " isert_disconnect_work\n");
786b2cb9649SNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
787b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
788b2cb9649SNicholas Bellinger 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
789b2cb9649SNicholas Bellinger 		goto wake_up;
790b2cb9649SNicholas Bellinger 	}
791b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
792b8d26b3bSNicholas Bellinger 
793b2cb9649SNicholas Bellinger wake_up:
794defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait);
795b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
796b8d26b3bSNicholas Bellinger }
797b8d26b3bSNicholas Bellinger 
798b8d26b3bSNicholas Bellinger static void
799b8d26b3bSNicholas Bellinger isert_disconnected_handler(struct rdma_cm_id *cma_id)
800b8d26b3bSNicholas Bellinger {
801b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
802b8d26b3bSNicholas Bellinger 
803b8d26b3bSNicholas Bellinger 	INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
804b8d26b3bSNicholas Bellinger 	schedule_work(&isert_conn->conn_logout_work);
805b8d26b3bSNicholas Bellinger }
806b8d26b3bSNicholas Bellinger 
807b8d26b3bSNicholas Bellinger static int
808b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
809b8d26b3bSNicholas Bellinger {
810b8d26b3bSNicholas Bellinger 	int ret = 0;
811b8d26b3bSNicholas Bellinger 
812b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
813b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
814b8d26b3bSNicholas Bellinger 
815b8d26b3bSNicholas Bellinger 	switch (event->event) {
816b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
817b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
818b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
819b8d26b3bSNicholas Bellinger 		break;
820b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
821b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
822b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
823b8d26b3bSNicholas Bellinger 		break;
824b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DISCONNECTED:
825b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
826b8d26b3bSNicholas Bellinger 		isert_disconnected_handler(cma_id);
827b8d26b3bSNicholas Bellinger 		break;
828b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
829b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ADDR_CHANGE:
830b8d26b3bSNicholas Bellinger 		break;
831b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
832b8d26b3bSNicholas Bellinger 	default:
833b8d26b3bSNicholas Bellinger 		pr_err("Unknown RDMA CMA event: %d\n", event->event);
834b8d26b3bSNicholas Bellinger 		break;
835b8d26b3bSNicholas Bellinger 	}
836b8d26b3bSNicholas Bellinger 
837b8d26b3bSNicholas Bellinger 	if (ret != 0) {
838b8d26b3bSNicholas Bellinger 		pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
839b8d26b3bSNicholas Bellinger 		       event->event, ret);
840b8d26b3bSNicholas Bellinger 		dump_stack();
841b8d26b3bSNicholas Bellinger 	}
842b8d26b3bSNicholas Bellinger 
843b8d26b3bSNicholas Bellinger 	return ret;
844b8d26b3bSNicholas Bellinger }
845b8d26b3bSNicholas Bellinger 
846b8d26b3bSNicholas Bellinger static int
847b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
848b8d26b3bSNicholas Bellinger {
849b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
850b8d26b3bSNicholas Bellinger 	int i, ret;
851b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
852b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
853b8d26b3bSNicholas Bellinger 
854b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
855b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
856b8d26b3bSNicholas Bellinger 		rx_wr->wr_id	= (unsigned long)rx_desc;
857b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
858b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
859b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
860b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
861b8d26b3bSNicholas Bellinger 	}
862b8d26b3bSNicholas Bellinger 
863b8d26b3bSNicholas Bellinger 	rx_wr--;
864b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
865b8d26b3bSNicholas Bellinger 
866b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
867b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
868b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
869b8d26b3bSNicholas Bellinger 	if (ret) {
870b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed with ret: %d\n", ret);
871b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
872b8d26b3bSNicholas Bellinger 	} else {
873b8d26b3bSNicholas Bellinger 		pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
874b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
875b8d26b3bSNicholas Bellinger 	}
876b8d26b3bSNicholas Bellinger 	return ret;
877b8d26b3bSNicholas Bellinger }
878b8d26b3bSNicholas Bellinger 
879b8d26b3bSNicholas Bellinger static int
880b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
881b8d26b3bSNicholas Bellinger {
882b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
883b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
884b8d26b3bSNicholas Bellinger 	int ret;
885b8d26b3bSNicholas Bellinger 
886b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
887b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
888b8d26b3bSNicholas Bellinger 
889b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
890b8d26b3bSNicholas Bellinger 	send_wr.wr_id	= (unsigned long)tx_desc;
891b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
892b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
893b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
894b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
895b8d26b3bSNicholas Bellinger 
896b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
897b8d26b3bSNicholas Bellinger 
898b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
899b8d26b3bSNicholas Bellinger 	if (ret) {
900b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send() failed, ret: %d\n", ret);
901b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
902b8d26b3bSNicholas Bellinger 	}
903b8d26b3bSNicholas Bellinger 
904b8d26b3bSNicholas Bellinger 	return ret;
905b8d26b3bSNicholas Bellinger }
906b8d26b3bSNicholas Bellinger 
907b8d26b3bSNicholas Bellinger static void
908b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
909b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
910b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
911b8d26b3bSNicholas Bellinger {
912b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
913b8d26b3bSNicholas Bellinger 
914b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
915b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
916b8d26b3bSNicholas Bellinger 
917b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
918b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
919b8d26b3bSNicholas Bellinger 
920b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
921b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
922b8d26b3bSNicholas Bellinger 
923b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
924b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
925b8d26b3bSNicholas Bellinger 		pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
926b8d26b3bSNicholas Bellinger 	}
927b8d26b3bSNicholas Bellinger }
928b8d26b3bSNicholas Bellinger 
929b8d26b3bSNicholas Bellinger static int
930b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
931b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
932b8d26b3bSNicholas Bellinger {
933b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
934b8d26b3bSNicholas Bellinger 	u64 dma_addr;
935b8d26b3bSNicholas Bellinger 
936b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
937b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
938b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
939b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error() failed\n");
940b8d26b3bSNicholas Bellinger 		return -ENOMEM;
941b8d26b3bSNicholas Bellinger 	}
942b8d26b3bSNicholas Bellinger 
943b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
944b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
945b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
946b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
947b8d26b3bSNicholas Bellinger 
948b8d26b3bSNicholas Bellinger 	pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
949b8d26b3bSNicholas Bellinger 		 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
950b8d26b3bSNicholas Bellinger 		 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
951b8d26b3bSNicholas Bellinger 
952b8d26b3bSNicholas Bellinger 	return 0;
953b8d26b3bSNicholas Bellinger }
954b8d26b3bSNicholas Bellinger 
955b8d26b3bSNicholas Bellinger static void
95695b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
95795b60f07SNicholas Bellinger 		   struct ib_send_wr *send_wr, bool coalesce)
958b8d26b3bSNicholas Bellinger {
95995b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
96095b60f07SNicholas Bellinger 
961b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
962b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
963b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
96495b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
965b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
96695b60f07SNicholas Bellinger 	/*
96795b60f07SNicholas Bellinger 	 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
96895b60f07SNicholas Bellinger 	 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
96995b60f07SNicholas Bellinger 	 */
970ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
971ebbe4421SNicholas Bellinger 	if (coalesce && isert_conn->state == ISER_CONN_UP &&
97295b60f07SNicholas Bellinger 	    ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
973ebbe4421SNicholas Bellinger 		tx_desc->llnode_active = true;
97495b60f07SNicholas Bellinger 		llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
975ebbe4421SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
97695b60f07SNicholas Bellinger 		return;
97795b60f07SNicholas Bellinger 	}
97895b60f07SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
97995b60f07SNicholas Bellinger 	tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
980ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
98195b60f07SNicholas Bellinger 
98295b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
983b8d26b3bSNicholas Bellinger }
984b8d26b3bSNicholas Bellinger 
985b8d26b3bSNicholas Bellinger static int
986b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
987b8d26b3bSNicholas Bellinger {
988b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
989b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
990b8d26b3bSNicholas Bellinger 	int ret;
991b8d26b3bSNicholas Bellinger 
992b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
993b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
994b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
995b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
996b8d26b3bSNicholas Bellinger 
997b8d26b3bSNicholas Bellinger 	pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
998b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
999b8d26b3bSNicholas Bellinger 
1000b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1001b8d26b3bSNicholas Bellinger 	rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1002b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1003b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1004b8d26b3bSNicholas Bellinger 
1005b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1006b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1007b8d26b3bSNicholas Bellinger 	if (ret) {
1008b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed: %d\n", ret);
1009b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1010b8d26b3bSNicholas Bellinger 	}
1011b8d26b3bSNicholas Bellinger 
1012b8d26b3bSNicholas Bellinger 	pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1013b8d26b3bSNicholas Bellinger 	return ret;
1014b8d26b3bSNicholas Bellinger }
1015b8d26b3bSNicholas Bellinger 
1016b8d26b3bSNicholas Bellinger static int
1017b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1018b8d26b3bSNicholas Bellinger 		   u32 length)
1019b8d26b3bSNicholas Bellinger {
1020b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1021b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1022b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1023b8d26b3bSNicholas Bellinger 	int ret;
1024b8d26b3bSNicholas Bellinger 
1025b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1026b8d26b3bSNicholas Bellinger 
1027b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1028b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1029b8d26b3bSNicholas Bellinger 
1030b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1031b8d26b3bSNicholas Bellinger 
1032b8d26b3bSNicholas Bellinger 	if (length > 0) {
1033b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1034b8d26b3bSNicholas Bellinger 
1035b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1036b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1037b8d26b3bSNicholas Bellinger 
1038b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1039b8d26b3bSNicholas Bellinger 
1040b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1041b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1042b8d26b3bSNicholas Bellinger 
1043b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1044b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
1045b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1046b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1047b8d26b3bSNicholas Bellinger 	}
1048b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1049b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1050b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1051b8d26b3bSNicholas Bellinger 			if (ret)
1052b8d26b3bSNicholas Bellinger 				return ret;
1053b8d26b3bSNicholas Bellinger 
1054b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1055b8d26b3bSNicholas Bellinger 			if (ret)
1056b8d26b3bSNicholas Bellinger 				return ret;
1057b8d26b3bSNicholas Bellinger 
1058b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_UP;
1059b8d26b3bSNicholas Bellinger 			goto post_send;
1060b8d26b3bSNicholas Bellinger 		}
1061b8d26b3bSNicholas Bellinger 
1062b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1063b8d26b3bSNicholas Bellinger 		if (ret)
1064b8d26b3bSNicholas Bellinger 			return ret;
1065b8d26b3bSNicholas Bellinger 	}
1066b8d26b3bSNicholas Bellinger post_send:
1067b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1068b8d26b3bSNicholas Bellinger 	if (ret)
1069b8d26b3bSNicholas Bellinger 		return ret;
1070b8d26b3bSNicholas Bellinger 
1071b8d26b3bSNicholas Bellinger 	return 0;
1072b8d26b3bSNicholas Bellinger }
1073b8d26b3bSNicholas Bellinger 
1074b8d26b3bSNicholas Bellinger static void
1075b8d26b3bSNicholas Bellinger isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1076b8d26b3bSNicholas Bellinger 		   struct isert_conn *isert_conn)
1077b8d26b3bSNicholas Bellinger {
1078b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1079b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1080b8d26b3bSNicholas Bellinger 	int size;
1081b8d26b3bSNicholas Bellinger 
1082b8d26b3bSNicholas Bellinger 	if (!login) {
1083b8d26b3bSNicholas Bellinger 		pr_err("conn->conn_login is NULL\n");
1084b8d26b3bSNicholas Bellinger 		dump_stack();
1085b8d26b3bSNicholas Bellinger 		return;
1086b8d26b3bSNicholas Bellinger 	}
1087b8d26b3bSNicholas Bellinger 
1088b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1089b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1090b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1091b8d26b3bSNicholas Bellinger 		/*
1092b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1093b8d26b3bSNicholas Bellinger 		 * login request PDU.
1094b8d26b3bSNicholas Bellinger 		 */
1095b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1096b8d26b3bSNicholas Bellinger 		login->current_stage =
1097b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1098b8d26b3bSNicholas Bellinger 			 >> 2;
1099b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1100b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1101b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1102b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1103b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1104b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1105b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1106b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1107b8d26b3bSNicholas Bellinger 	}
1108b8d26b3bSNicholas Bellinger 
1109b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1110b8d26b3bSNicholas Bellinger 
1111b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1112b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1113b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1114b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1115b8d26b3bSNicholas Bellinger 
11166faaa85fSNicholas Bellinger 	if (login->first_request) {
1117b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
11186faaa85fSNicholas Bellinger 		return;
11196faaa85fSNicholas Bellinger 	}
11206faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1121b8d26b3bSNicholas Bellinger }
1122b8d26b3bSNicholas Bellinger 
1123b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1124676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1125b8d26b3bSNicholas Bellinger {
1126b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1127b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1128d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1129b8d26b3bSNicholas Bellinger 
1130676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1131d703ce2fSNicholas Bellinger 	if (!cmd) {
1132d703ce2fSNicholas Bellinger 		pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1133b8d26b3bSNicholas Bellinger 		return NULL;
1134b8d26b3bSNicholas Bellinger 	}
1135d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1136b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1137d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1138b8d26b3bSNicholas Bellinger 
1139d703ce2fSNicholas Bellinger 	return cmd;
1140b8d26b3bSNicholas Bellinger }
1141b8d26b3bSNicholas Bellinger 
1142b8d26b3bSNicholas Bellinger static int
1143b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1144d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1145d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1146b8d26b3bSNicholas Bellinger {
1147b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1148b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1149b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1150b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1151b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1152b8d26b3bSNicholas Bellinger 
1153b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1154b8d26b3bSNicholas Bellinger 	if (rc < 0)
1155b8d26b3bSNicholas Bellinger 		return rc;
1156b8d26b3bSNicholas Bellinger 
1157b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1158b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1159b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1160b8d26b3bSNicholas Bellinger 
1161b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1162b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1163b8d26b3bSNicholas Bellinger 		return 0;
1164b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1165b8d26b3bSNicholas Bellinger 		dump_payload = true;
1166b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1167b8d26b3bSNicholas Bellinger 	}
1168b8d26b3bSNicholas Bellinger 
1169b8d26b3bSNicholas Bellinger 	if (!imm_data)
1170b8d26b3bSNicholas Bellinger 		return 0;
1171b8d26b3bSNicholas Bellinger 
1172b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1173b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1174b8d26b3bSNicholas Bellinger 
1175b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1176b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1177b8d26b3bSNicholas Bellinger 
1178b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1179b8d26b3bSNicholas Bellinger 
1180b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1181b8d26b3bSNicholas Bellinger 
1182b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1183b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1184b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1185b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1186b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1187b8d26b3bSNicholas Bellinger 	}
1188b8d26b3bSNicholas Bellinger 
1189b8d26b3bSNicholas Bellinger sequence_cmd:
1190561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1191b8d26b3bSNicholas Bellinger 
1192b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1193b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
1194b8d26b3bSNicholas Bellinger 
1195b8d26b3bSNicholas Bellinger 	return 0;
1196b8d26b3bSNicholas Bellinger }
1197b8d26b3bSNicholas Bellinger 
1198b8d26b3bSNicholas Bellinger static int
1199b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1200b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1201b8d26b3bSNicholas Bellinger {
1202b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1203b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1204b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1205b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1206b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1207b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1208b8d26b3bSNicholas Bellinger 
1209b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1210b8d26b3bSNicholas Bellinger 	if (rc < 0)
1211b8d26b3bSNicholas Bellinger 		return rc;
1212b8d26b3bSNicholas Bellinger 	else if (!cmd)
1213b8d26b3bSNicholas Bellinger 		return 0;
1214b8d26b3bSNicholas Bellinger 	/*
1215b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1216b8d26b3bSNicholas Bellinger 	 */
1217b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
1218b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
1219b8d26b3bSNicholas Bellinger 		dump_stack();
1220b8d26b3bSNicholas Bellinger 		return -1;
1221b8d26b3bSNicholas Bellinger 	}
1222b8d26b3bSNicholas Bellinger 
1223b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1224b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1225b8d26b3bSNicholas Bellinger 
1226b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1227b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1228b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1229b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1230b8d26b3bSNicholas Bellinger 	/*
1231b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1232b8d26b3bSNicholas Bellinger 	 */
1233b8d26b3bSNicholas Bellinger 	if (page_off) {
1234b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1235b8d26b3bSNicholas Bellinger 		dump_stack();
1236b8d26b3bSNicholas Bellinger 		return -1;
1237b8d26b3bSNicholas Bellinger 	}
1238b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1239b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1240b8d26b3bSNicholas Bellinger 
1241b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1242b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1243b8d26b3bSNicholas Bellinger 
1244b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1245b8d26b3bSNicholas Bellinger 	if (rc < 0)
1246b8d26b3bSNicholas Bellinger 		return rc;
1247b8d26b3bSNicholas Bellinger 
1248b8d26b3bSNicholas Bellinger 	return 0;
1249b8d26b3bSNicholas Bellinger }
1250b8d26b3bSNicholas Bellinger 
1251b8d26b3bSNicholas Bellinger static int
1252778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1253d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1254d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1255778de368SNicholas Bellinger {
1256778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1257778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1258778de368SNicholas Bellinger 	int rc;
1259778de368SNicholas Bellinger 
1260778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1261778de368SNicholas Bellinger 	if (rc < 0)
1262778de368SNicholas Bellinger 		return rc;
1263778de368SNicholas Bellinger 	/*
1264778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1265778de368SNicholas Bellinger 	 */
1266778de368SNicholas Bellinger 
1267778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1268778de368SNicholas Bellinger }
1269778de368SNicholas Bellinger 
1270778de368SNicholas Bellinger static int
1271adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1272d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1273d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1274adb54c29SNicholas Bellinger {
1275adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1276adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1277adb54c29SNicholas Bellinger 	int rc;
1278adb54c29SNicholas Bellinger 	unsigned char *text_in;
1279adb54c29SNicholas Bellinger 
1280adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1281adb54c29SNicholas Bellinger 	if (rc < 0)
1282adb54c29SNicholas Bellinger 		return rc;
1283adb54c29SNicholas Bellinger 
1284adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1285adb54c29SNicholas Bellinger 	if (!text_in) {
1286adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1287adb54c29SNicholas Bellinger 		       payload_length);
1288adb54c29SNicholas Bellinger 		return -ENOMEM;
1289adb54c29SNicholas Bellinger 	}
1290adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1291adb54c29SNicholas Bellinger 
1292adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1293adb54c29SNicholas Bellinger 
1294adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1295adb54c29SNicholas Bellinger }
1296adb54c29SNicholas Bellinger 
1297adb54c29SNicholas Bellinger static int
1298b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1299b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1300b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1301b8d26b3bSNicholas Bellinger {
1302b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1303b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1304ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1305b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1306b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1307b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1308b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1309b8d26b3bSNicholas Bellinger 
1310ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1311ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1312ca40d24eSNicholas Bellinger 		pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1313ca40d24eSNicholas Bellinger 		       " ignoring\n", opcode);
1314ca40d24eSNicholas Bellinger 		return 0;
1315ca40d24eSNicholas Bellinger 	}
1316ca40d24eSNicholas Bellinger 
1317b8d26b3bSNicholas Bellinger 	switch (opcode) {
1318b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1319676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1320b8d26b3bSNicholas Bellinger 		if (!cmd)
1321b8d26b3bSNicholas Bellinger 			break;
1322b8d26b3bSNicholas Bellinger 
1323d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1324b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1325b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1326b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1327b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1328b8d26b3bSNicholas Bellinger 
1329d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1330b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1331b8d26b3bSNicholas Bellinger 		break;
1332b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1333676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1334b8d26b3bSNicholas Bellinger 		if (!cmd)
1335b8d26b3bSNicholas Bellinger 			break;
1336b8d26b3bSNicholas Bellinger 
1337d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1338d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1339778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1340b8d26b3bSNicholas Bellinger 		break;
1341b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1342b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1343b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1344b8d26b3bSNicholas Bellinger 		break;
1345b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1346676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1347b8d26b3bSNicholas Bellinger 		if (!cmd)
1348b8d26b3bSNicholas Bellinger 			break;
1349b8d26b3bSNicholas Bellinger 
1350b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1351b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1352b8d26b3bSNicholas Bellinger 		break;
1353b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1354676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1355b8d26b3bSNicholas Bellinger 		if (!cmd)
1356b8d26b3bSNicholas Bellinger 			break;
1357b8d26b3bSNicholas Bellinger 
1358b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1359b8d26b3bSNicholas Bellinger 		if (ret > 0)
1360b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1361b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1362b8d26b3bSNicholas Bellinger 						    HZ);
1363b8d26b3bSNicholas Bellinger 		break;
1364adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1365676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1366adb54c29SNicholas Bellinger 		if (!cmd)
1367adb54c29SNicholas Bellinger 			break;
1368adb54c29SNicholas Bellinger 
1369d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1370d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1371adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1372adb54c29SNicholas Bellinger 		break;
1373b8d26b3bSNicholas Bellinger 	default:
1374b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1375b8d26b3bSNicholas Bellinger 		dump_stack();
1376b8d26b3bSNicholas Bellinger 		break;
1377b8d26b3bSNicholas Bellinger 	}
1378b8d26b3bSNicholas Bellinger 
1379b8d26b3bSNicholas Bellinger 	return ret;
1380b8d26b3bSNicholas Bellinger }
1381b8d26b3bSNicholas Bellinger 
1382b8d26b3bSNicholas Bellinger static void
1383b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1384b8d26b3bSNicholas Bellinger {
1385b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1386b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1387b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1388b8d26b3bSNicholas Bellinger 	int rc;
1389b8d26b3bSNicholas Bellinger 
1390b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1391b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1392b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1393b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1394b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1395b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1396b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1397b8d26b3bSNicholas Bellinger 		}
1398b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1399b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1400b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1401b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1402b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1403b8d26b3bSNicholas Bellinger 		}
1404b8d26b3bSNicholas Bellinger 
1405b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1406b8d26b3bSNicholas Bellinger 		break;
1407b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1408b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1409b8d26b3bSNicholas Bellinger 		break;
1410b8d26b3bSNicholas Bellinger 	default:
1411b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1412b8d26b3bSNicholas Bellinger 		break;
1413b8d26b3bSNicholas Bellinger 	}
1414b8d26b3bSNicholas Bellinger 
1415b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1416b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1417b8d26b3bSNicholas Bellinger }
1418b8d26b3bSNicholas Bellinger 
1419b8d26b3bSNicholas Bellinger static void
1420b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1421b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1422b8d26b3bSNicholas Bellinger {
1423b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1424b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1425b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1426b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1427b8d26b3bSNicholas Bellinger 
1428b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1429b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1430b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1431b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1432b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1433b8d26b3bSNicholas Bellinger 	} else {
1434b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1435b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1436b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1437b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1438b8d26b3bSNicholas Bellinger 	}
1439b8d26b3bSNicholas Bellinger 
1440b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1441b8d26b3bSNicholas Bellinger 
1442b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1443b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1444b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1445b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1446b8d26b3bSNicholas Bellinger 
1447b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1448b8d26b3bSNicholas Bellinger 		isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1449b8d26b3bSNicholas Bellinger 				   isert_conn);
1450b8d26b3bSNicholas Bellinger 	else
1451b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
1452b8d26b3bSNicholas Bellinger 
1453b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1454b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1455b8d26b3bSNicholas Bellinger 
1456b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1457b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1458b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1459b8d26b3bSNicholas Bellinger 
1460b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1461b8d26b3bSNicholas Bellinger 		return;
1462b8d26b3bSNicholas Bellinger 
1463b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1464b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1465b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1466b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1467b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1468b8d26b3bSNicholas Bellinger 		if (err) {
1469b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1470b8d26b3bSNicholas Bellinger 			       count, err);
1471b8d26b3bSNicholas Bellinger 		}
1472b8d26b3bSNicholas Bellinger 	}
1473b8d26b3bSNicholas Bellinger }
1474b8d26b3bSNicholas Bellinger 
1475e3d7e4c3SSagi Grimberg static int
1476e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1477e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1478e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1479e3d7e4c3SSagi Grimberg {
1480e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1481e3d7e4c3SSagi Grimberg 
1482e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1483e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1484e3d7e4c3SSagi Grimberg 
1485e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1486e3d7e4c3SSagi Grimberg 	data->offset = offset;
1487e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1488e3d7e4c3SSagi Grimberg 
1489e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1490e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1491e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1492e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1493e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1494e3d7e4c3SSagi Grimberg 
1495e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1496e3d7e4c3SSagi Grimberg 					data->dma_dir);
1497e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
1498e3d7e4c3SSagi Grimberg 		pr_err("Cmd: unable to dma map SGs %p\n", sg);
1499e3d7e4c3SSagi Grimberg 		return -EINVAL;
1500e3d7e4c3SSagi Grimberg 	}
1501e3d7e4c3SSagi Grimberg 
1502e3d7e4c3SSagi Grimberg 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1503e3d7e4c3SSagi Grimberg 		 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1504e3d7e4c3SSagi Grimberg 
1505e3d7e4c3SSagi Grimberg 	return 0;
1506e3d7e4c3SSagi Grimberg }
1507e3d7e4c3SSagi Grimberg 
1508e3d7e4c3SSagi Grimberg static void
1509e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1510e3d7e4c3SSagi Grimberg {
1511e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1512e3d7e4c3SSagi Grimberg 
1513e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1514e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1515e3d7e4c3SSagi Grimberg }
1516e3d7e4c3SSagi Grimberg 
1517e3d7e4c3SSagi Grimberg 
1518e3d7e4c3SSagi Grimberg 
1519b8d26b3bSNicholas Bellinger static void
1520b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1521b8d26b3bSNicholas Bellinger {
1522b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1523b8d26b3bSNicholas Bellinger 
152490ecc6e2SVu Pham 	pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1525e3d7e4c3SSagi Grimberg 
1526e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
152790ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1528e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1529b8d26b3bSNicholas Bellinger 	}
1530b8d26b3bSNicholas Bellinger 
153190ecc6e2SVu Pham 	if (wr->send_wr) {
153290ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1533b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1534b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
153590ecc6e2SVu Pham 	}
1536b8d26b3bSNicholas Bellinger 
153790ecc6e2SVu Pham 	if (wr->ib_sge) {
153890ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
153990ecc6e2SVu Pham 		kfree(wr->ib_sge);
154090ecc6e2SVu Pham 		wr->ib_sge = NULL;
154190ecc6e2SVu Pham 	}
1542b8d26b3bSNicholas Bellinger }
1543b8d26b3bSNicholas Bellinger 
1544b8d26b3bSNicholas Bellinger static void
1545a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
154659464ef4SVu Pham {
154759464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
154859464ef4SVu Pham 	LIST_HEAD(unmap_list);
154959464ef4SVu Pham 
1550a3a5a826SSagi Grimberg 	pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
155159464ef4SVu Pham 
155259464ef4SVu Pham 	if (wr->fr_desc) {
1553a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
155459464ef4SVu Pham 			 isert_cmd, wr->fr_desc);
155559464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1556a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
155759464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
155859464ef4SVu Pham 		wr->fr_desc = NULL;
155959464ef4SVu Pham 	}
156059464ef4SVu Pham 
1561e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
1562a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1563e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
156459464ef4SVu Pham 	}
156559464ef4SVu Pham 
156659464ef4SVu Pham 	wr->ib_sge = NULL;
156759464ef4SVu Pham 	wr->send_wr = NULL;
156859464ef4SVu Pham }
156959464ef4SVu Pham 
157059464ef4SVu Pham static void
1571b8d26b3bSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd)
1572b8d26b3bSNicholas Bellinger {
1573d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1574b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1575186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1576d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1577b8d26b3bSNicholas Bellinger 
1578b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1579b8d26b3bSNicholas Bellinger 
1580b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1581b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1582b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1583b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15845159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1585b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1586b8d26b3bSNicholas Bellinger 
1587b8d26b3bSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE)
1588b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
1589b8d26b3bSNicholas Bellinger 
1590d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1591186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1592186a9647SNicholas Bellinger 		break;
1593b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1594186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1595186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
15965159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1597186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1598186a9647SNicholas Bellinger 
1599b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1600b8d26b3bSNicholas Bellinger 		break;
1601b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1602b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1603adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1604b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1605b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
16065159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1607b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1608b8d26b3bSNicholas Bellinger 
1609b8d26b3bSNicholas Bellinger 		/*
1610b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1611b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1612b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1613b8d26b3bSNicholas Bellinger 		 */
1614b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
16153df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
16163df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
16173df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1618b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1619b8d26b3bSNicholas Bellinger 			break;
1620b8d26b3bSNicholas Bellinger 		}
1621b8d26b3bSNicholas Bellinger 		/*
1622b8d26b3bSNicholas Bellinger 		 * Fall-through
1623b8d26b3bSNicholas Bellinger 		 */
1624b8d26b3bSNicholas Bellinger 	default:
1625d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1626b8d26b3bSNicholas Bellinger 		break;
1627b8d26b3bSNicholas Bellinger 	}
1628b8d26b3bSNicholas Bellinger }
1629b8d26b3bSNicholas Bellinger 
1630b8d26b3bSNicholas Bellinger static void
1631b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1632b8d26b3bSNicholas Bellinger {
1633b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1634b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1635b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1636b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1637b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1638b8d26b3bSNicholas Bellinger 	}
1639b8d26b3bSNicholas Bellinger }
1640b8d26b3bSNicholas Bellinger 
1641b8d26b3bSNicholas Bellinger static void
1642b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1643b8d26b3bSNicholas Bellinger 		     struct ib_device *ib_dev)
1644b8d26b3bSNicholas Bellinger {
1645dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1646dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1647dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1648dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1649dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1650b8d26b3bSNicholas Bellinger 	}
1651b8d26b3bSNicholas Bellinger 
1652b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
1653b8d26b3bSNicholas Bellinger 	isert_put_cmd(isert_cmd);
1654b8d26b3bSNicholas Bellinger }
1655b8d26b3bSNicholas Bellinger 
1656b8d26b3bSNicholas Bellinger static void
1657*f93f3a70SSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1658*f93f3a70SSagi Grimberg 			    struct isert_cmd *isert_cmd)
1659*f93f3a70SSagi Grimberg {
1660*f93f3a70SSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1661*f93f3a70SSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
1662*f93f3a70SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
1663*f93f3a70SSagi Grimberg 
1664*f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1665*f93f3a70SSagi Grimberg 	isert_put_response(isert_conn->conn, cmd);
1666*f93f3a70SSagi Grimberg }
1667*f93f3a70SSagi Grimberg 
1668*f93f3a70SSagi Grimberg static void
1669b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1670b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1671b8d26b3bSNicholas Bellinger {
1672b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1673d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1674b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
167590ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1676d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1677b8d26b3bSNicholas Bellinger 
1678b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1679d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1680e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1681b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1682b8d26b3bSNicholas Bellinger 
168390ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1684b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1685b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1686b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1687b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1688b8d26b3bSNicholas Bellinger 
1689b8d26b3bSNicholas Bellinger 	target_execute_cmd(se_cmd);
1690b8d26b3bSNicholas Bellinger }
1691b8d26b3bSNicholas Bellinger 
1692b8d26b3bSNicholas Bellinger static void
1693b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1694b8d26b3bSNicholas Bellinger {
1695b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1696b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1697b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1698b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1699d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1700b8d26b3bSNicholas Bellinger 
1701b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1702b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1703b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1704b8d26b3bSNicholas Bellinger 
1705b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1706b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1707b8d26b3bSNicholas Bellinger 
1708b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1709b8d26b3bSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1710b8d26b3bSNicholas Bellinger 		break;
1711b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1712b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1713b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1714b8d26b3bSNicholas Bellinger 
1715b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1716b8d26b3bSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
17173df8f68aSNicholas Bellinger 		break;
1718b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1719b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1720b8d26b3bSNicholas Bellinger 		/*
1721b8d26b3bSNicholas Bellinger 		 * Call atomic_dec(&isert_conn->post_send_buf_count)
1722defd8848SNicholas Bellinger 		 * from isert_wait_conn()
1723b8d26b3bSNicholas Bellinger 		 */
1724b8d26b3bSNicholas Bellinger 		isert_conn->logout_posted = true;
1725b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1726b8d26b3bSNicholas Bellinger 		break;
1727adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1728adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1729adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1730adb54c29SNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1731adb54c29SNicholas Bellinger 		break;
1732b8d26b3bSNicholas Bellinger 	default:
1733b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1734b8d26b3bSNicholas Bellinger 		dump_stack();
1735b8d26b3bSNicholas Bellinger 		break;
1736b8d26b3bSNicholas Bellinger 	}
1737b8d26b3bSNicholas Bellinger }
1738b8d26b3bSNicholas Bellinger 
1739b8d26b3bSNicholas Bellinger static void
1740b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1741b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1742b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1743b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1744b8d26b3bSNicholas Bellinger {
1745d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1746b6b87a1dSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1747b8d26b3bSNicholas Bellinger 
1748b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
17493df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1750adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1751adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1752b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1753b8d26b3bSNicholas Bellinger 
1754b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1755b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1756b8d26b3bSNicholas Bellinger 		return;
1757b8d26b3bSNicholas Bellinger 	}
1758b6b87a1dSNicholas Bellinger 	atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1759b8d26b3bSNicholas Bellinger 
1760b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
1761b8d26b3bSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev);
1762b8d26b3bSNicholas Bellinger }
1763b8d26b3bSNicholas Bellinger 
1764b8d26b3bSNicholas Bellinger static void
176595b60f07SNicholas Bellinger __isert_send_completion(struct iser_tx_desc *tx_desc,
1766b8d26b3bSNicholas Bellinger 		        struct isert_conn *isert_conn)
1767b8d26b3bSNicholas Bellinger {
1768b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1769b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1770b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1771b8d26b3bSNicholas Bellinger 
1772b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1773b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1774b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1775b8d26b3bSNicholas Bellinger 		return;
1776b8d26b3bSNicholas Bellinger 	}
1777b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1778b8d26b3bSNicholas Bellinger 
1779b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1780b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
1781b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1782b8d26b3bSNicholas Bellinger 		dump_stack();
1783b8d26b3bSNicholas Bellinger 		break;
1784b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1785b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1786b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1787b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1788b8d26b3bSNicholas Bellinger 		break;
1789b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1790*f93f3a70SSagi Grimberg 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1791*f93f3a70SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1792*f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
1793b8d26b3bSNicholas Bellinger 		break;
1794b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1795b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1796b8d26b3bSNicholas Bellinger 
1797b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1798b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1799b8d26b3bSNicholas Bellinger 		break;
1800b8d26b3bSNicholas Bellinger 	default:
1801b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1802b8d26b3bSNicholas Bellinger 		dump_stack();
1803b8d26b3bSNicholas Bellinger 		break;
1804b8d26b3bSNicholas Bellinger 	}
1805b8d26b3bSNicholas Bellinger }
1806b8d26b3bSNicholas Bellinger 
1807b8d26b3bSNicholas Bellinger static void
180895b60f07SNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
180995b60f07SNicholas Bellinger 		      struct isert_conn *isert_conn)
181095b60f07SNicholas Bellinger {
181195b60f07SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
181295b60f07SNicholas Bellinger 	struct iser_tx_desc *t;
181395b60f07SNicholas Bellinger 	/*
181495b60f07SNicholas Bellinger 	 * Drain coalesced completion llist starting from comp_llnode_batch
181595b60f07SNicholas Bellinger 	 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
181695b60f07SNicholas Bellinger 	 */
181795b60f07SNicholas Bellinger 	while (llnode) {
181895b60f07SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
181995b60f07SNicholas Bellinger 		llnode = llist_next(llnode);
182095b60f07SNicholas Bellinger 		__isert_send_completion(t, isert_conn);
182195b60f07SNicholas Bellinger 	}
182295b60f07SNicholas Bellinger 	__isert_send_completion(tx_desc, isert_conn);
182395b60f07SNicholas Bellinger }
182495b60f07SNicholas Bellinger 
182595b60f07SNicholas Bellinger static void
1826ebbe4421SNicholas Bellinger isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1827ebbe4421SNicholas Bellinger {
1828ebbe4421SNicholas Bellinger 	struct llist_node *llnode;
1829ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
1830ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
1831ebbe4421SNicholas Bellinger 
1832ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
1833ebbe4421SNicholas Bellinger 	llnode = llist_del_all(&isert_conn->conn_comp_llist);
1834ebbe4421SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
1835ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
1836ebbe4421SNicholas Bellinger 
1837ebbe4421SNicholas Bellinger 	while (llnode) {
1838ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1839ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
1840ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
1841ebbe4421SNicholas Bellinger 
1842ebbe4421SNicholas Bellinger 		atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1843ebbe4421SNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev);
1844ebbe4421SNicholas Bellinger 	}
1845ebbe4421SNicholas Bellinger }
1846ebbe4421SNicholas Bellinger 
1847ebbe4421SNicholas Bellinger static void
1848defd8848SNicholas Bellinger isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1849b8d26b3bSNicholas Bellinger {
1850b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1851b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1852ebbe4421SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
1853ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
1854ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
1855ebbe4421SNicholas Bellinger 
1856ebbe4421SNicholas Bellinger 	while (llnode) {
1857ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1858ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
1859ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
1860ebbe4421SNicholas Bellinger 
1861ebbe4421SNicholas Bellinger 		atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1862ebbe4421SNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev);
1863ebbe4421SNicholas Bellinger 	}
1864ebbe4421SNicholas Bellinger 	tx_desc->comp_llnode_batch = NULL;
1865b8d26b3bSNicholas Bellinger 
1866b8d26b3bSNicholas Bellinger 	if (!isert_cmd)
1867b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1868b8d26b3bSNicholas Bellinger 	else
1869b8d26b3bSNicholas Bellinger 		isert_completion_put(tx_desc, isert_cmd, ib_dev);
1870b8d26b3bSNicholas Bellinger }
1871b8d26b3bSNicholas Bellinger 
1872defd8848SNicholas Bellinger static void
1873defd8848SNicholas Bellinger isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1874defd8848SNicholas Bellinger {
1875defd8848SNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1876defd8848SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1877defd8848SNicholas Bellinger 
1878defd8848SNicholas Bellinger 	if (isert_conn->post_recv_buf_count)
1879defd8848SNicholas Bellinger 		return;
1880defd8848SNicholas Bellinger 
1881ebbe4421SNicholas Bellinger 	isert_cq_drain_comp_llist(isert_conn, ib_dev);
1882ebbe4421SNicholas Bellinger 
1883defd8848SNicholas Bellinger 	if (conn->sess) {
1884defd8848SNicholas Bellinger 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1885defd8848SNicholas Bellinger 		target_wait_for_sess_cmds(conn->sess->se_sess);
1886defd8848SNicholas Bellinger 	}
1887defd8848SNicholas Bellinger 
1888defd8848SNicholas Bellinger 	while (atomic_read(&isert_conn->post_send_buf_count))
1889defd8848SNicholas Bellinger 		msleep(3000);
1890b8d26b3bSNicholas Bellinger 
1891b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
1892defd8848SNicholas Bellinger 	isert_conn->state = ISER_CONN_DOWN;
1893b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
1894b2cb9649SNicholas Bellinger 
1895defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait_comp_err);
1896b8d26b3bSNicholas Bellinger }
1897b8d26b3bSNicholas Bellinger 
1898b8d26b3bSNicholas Bellinger static void
1899b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
1900b8d26b3bSNicholas Bellinger {
1901b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
1902b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
1903b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
1904b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
1905b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1906b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
1907b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
1908b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
1909b8d26b3bSNicholas Bellinger 
1910b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1911b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1912b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
1913b8d26b3bSNicholas Bellinger 
1914b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
1915b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
1916b8d26b3bSNicholas Bellinger 		} else {
1917b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1918b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
1919c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
1920defd8848SNicholas Bellinger 
19219bb4ca68SNicholas Bellinger 			if (wc.wr_id != ISER_FASTREG_LI_WRID) {
1922ebbe4421SNicholas Bellinger 				if (tx_desc->llnode_active)
1923ebbe4421SNicholas Bellinger 					continue;
1924ebbe4421SNicholas Bellinger 
1925b8d26b3bSNicholas Bellinger 				atomic_dec(&isert_conn->post_send_buf_count);
1926defd8848SNicholas Bellinger 				isert_cq_tx_comp_err(tx_desc, isert_conn);
1927b8d26b3bSNicholas Bellinger 			}
1928b8d26b3bSNicholas Bellinger 		}
19299bb4ca68SNicholas Bellinger 	}
1930b8d26b3bSNicholas Bellinger 
1931b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1932b8d26b3bSNicholas Bellinger }
1933b8d26b3bSNicholas Bellinger 
1934b8d26b3bSNicholas Bellinger static void
1935b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
1936b8d26b3bSNicholas Bellinger {
1937b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1938b8d26b3bSNicholas Bellinger 
1939b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1940b8d26b3bSNicholas Bellinger }
1941b8d26b3bSNicholas Bellinger 
1942b8d26b3bSNicholas Bellinger static void
1943b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
1944b8d26b3bSNicholas Bellinger {
1945b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
1946b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
1947b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
1948b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
1949b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1950b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
1951b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
1952b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
1953b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
1954b8d26b3bSNicholas Bellinger 
1955b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1956b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1957b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
1958b8d26b3bSNicholas Bellinger 
1959b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
1960b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
1961b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
1962b8d26b3bSNicholas Bellinger 		} else {
1963b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1964c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
1965b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
1966c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
1967c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
1968c5a2adbfSNicholas Bellinger 			}
1969b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
1970defd8848SNicholas Bellinger 			isert_cq_rx_comp_err(isert_conn);
1971b8d26b3bSNicholas Bellinger 		}
1972b8d26b3bSNicholas Bellinger 	}
1973b8d26b3bSNicholas Bellinger 
1974b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1975b8d26b3bSNicholas Bellinger }
1976b8d26b3bSNicholas Bellinger 
1977b8d26b3bSNicholas Bellinger static void
1978b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
1979b8d26b3bSNicholas Bellinger {
1980b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1981b8d26b3bSNicholas Bellinger 
1982b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1983b8d26b3bSNicholas Bellinger }
1984b8d26b3bSNicholas Bellinger 
1985b8d26b3bSNicholas Bellinger static int
1986b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1987b8d26b3bSNicholas Bellinger {
1988b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
1989b8d26b3bSNicholas Bellinger 	int ret;
1990b8d26b3bSNicholas Bellinger 
1991b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
1992b8d26b3bSNicholas Bellinger 
1993b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1994b8d26b3bSNicholas Bellinger 			   &wr_failed);
1995b8d26b3bSNicholas Bellinger 	if (ret) {
1996b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
1997b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1998b8d26b3bSNicholas Bellinger 		return ret;
1999b8d26b3bSNicholas Bellinger 	}
2000b8d26b3bSNicholas Bellinger 	return ret;
2001b8d26b3bSNicholas Bellinger }
2002b8d26b3bSNicholas Bellinger 
2003b8d26b3bSNicholas Bellinger static int
2004b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2005b8d26b3bSNicholas Bellinger {
2006d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2007b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2008b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2009b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2010b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2011b8d26b3bSNicholas Bellinger 
2012b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2013b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2014b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2015b8d26b3bSNicholas Bellinger 	/*
2016b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2017b8d26b3bSNicholas Bellinger 	 */
2018b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2019b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2020b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2021b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2022b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2023dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2024b8d26b3bSNicholas Bellinger 
2025b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2026b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2027b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2028b8d26b3bSNicholas Bellinger 
2029b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2030b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2031dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2032b8d26b3bSNicholas Bellinger 
2033dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2034dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2035b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2036b8d26b3bSNicholas Bellinger 
2037dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2038dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2039dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
2040b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2041b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2042b8d26b3bSNicholas Bellinger 	}
2043b8d26b3bSNicholas Bellinger 
204495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
2045b8d26b3bSNicholas Bellinger 
2046b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2047b8d26b3bSNicholas Bellinger 
2048b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2049b8d26b3bSNicholas Bellinger }
2050b8d26b3bSNicholas Bellinger 
2051b8d26b3bSNicholas Bellinger static int
2052b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2053b8d26b3bSNicholas Bellinger 		bool nopout_response)
2054b8d26b3bSNicholas Bellinger {
2055d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2056b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2057b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2058b8d26b3bSNicholas Bellinger 
2059b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2060b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2061b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2062b8d26b3bSNicholas Bellinger 			       nopout_response);
2063b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
206495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2065b8d26b3bSNicholas Bellinger 
20668b513d0cSMasanari Iida 	pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2067b8d26b3bSNicholas Bellinger 
2068b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2069b8d26b3bSNicholas Bellinger }
2070b8d26b3bSNicholas Bellinger 
2071b8d26b3bSNicholas Bellinger static int
2072b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2073b8d26b3bSNicholas Bellinger {
2074d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2075b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2076b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2077b8d26b3bSNicholas Bellinger 
2078b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2079b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2080b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2081b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
208295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2083b8d26b3bSNicholas Bellinger 
2084b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2085b8d26b3bSNicholas Bellinger 
2086b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2087b8d26b3bSNicholas Bellinger }
2088b8d26b3bSNicholas Bellinger 
2089b8d26b3bSNicholas Bellinger static int
2090b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2091b8d26b3bSNicholas Bellinger {
2092d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2093b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2094b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2095b8d26b3bSNicholas Bellinger 
2096b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2097b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2098b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2099b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
210095b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2101b8d26b3bSNicholas Bellinger 
2102b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2103b8d26b3bSNicholas Bellinger 
2104b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2105b8d26b3bSNicholas Bellinger }
2106b8d26b3bSNicholas Bellinger 
2107b8d26b3bSNicholas Bellinger static int
2108b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2109b8d26b3bSNicholas Bellinger {
2110d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2111b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2112b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
21133df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
21143df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
21153df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
21163df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2117b8d26b3bSNicholas Bellinger 
2118b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
21193df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2120b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
21213df8f68aSNicholas Bellinger 
21223df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2123dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
21243df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
21253df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2126dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2127dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
21283df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
21293df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
21303df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
21313df8f68aSNicholas Bellinger 
213295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2133b8d26b3bSNicholas Bellinger 
2134b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2135b8d26b3bSNicholas Bellinger 
2136b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2137b8d26b3bSNicholas Bellinger }
2138b8d26b3bSNicholas Bellinger 
2139b8d26b3bSNicholas Bellinger static int
2140adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2141adb54c29SNicholas Bellinger {
2142d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2143adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2144adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2145adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2146adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2147adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2148adb54c29SNicholas Bellinger 	int rc;
2149adb54c29SNicholas Bellinger 
2150adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2151adb54c29SNicholas Bellinger 	rc = iscsit_build_text_rsp(cmd, conn, hdr);
2152adb54c29SNicholas Bellinger 	if (rc < 0)
2153adb54c29SNicholas Bellinger 		return rc;
2154adb54c29SNicholas Bellinger 
2155adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2156adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2157adb54c29SNicholas Bellinger 
2158adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2159adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2160adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2161adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2162adb54c29SNicholas Bellinger 
2163adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2164adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2165adb54c29SNicholas Bellinger 
2166adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2167adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2168adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
2169adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2170adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2171adb54c29SNicholas Bellinger 	}
217295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2173adb54c29SNicholas Bellinger 
2174adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2175adb54c29SNicholas Bellinger 
2176adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2177adb54c29SNicholas Bellinger }
2178adb54c29SNicholas Bellinger 
2179adb54c29SNicholas Bellinger static int
2180b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2181b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2182b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2183b8d26b3bSNicholas Bellinger {
2184d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2185b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2186b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2187b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2188b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2189b8d26b3bSNicholas Bellinger 
2190b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2191b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2192b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2193b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2194b8d26b3bSNicholas Bellinger 
2195b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2196b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2197b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2198b8d26b3bSNicholas Bellinger 	/*
2199b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2200b8d26b3bSNicholas Bellinger 	 */
2201b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2202b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2203b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
2204b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
2205b8d26b3bSNicholas Bellinger 
2206b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2207b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2208b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2209b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2210b8d26b3bSNicholas Bellinger 
221190ecc6e2SVu Pham 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
221290ecc6e2SVu Pham 			 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2213b8d26b3bSNicholas Bellinger 		page_off = 0;
2214b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2215b8d26b3bSNicholas Bellinger 		ib_sge++;
2216b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2217b8d26b3bSNicholas Bellinger 	}
2218b8d26b3bSNicholas Bellinger 
2219b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2220b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
2221b8d26b3bSNicholas Bellinger 
2222b8d26b3bSNicholas Bellinger 	return sg_nents;
2223b8d26b3bSNicholas Bellinger }
2224b8d26b3bSNicholas Bellinger 
2225b8d26b3bSNicholas Bellinger static int
222690ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
222790ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2228b8d26b3bSNicholas Bellinger {
2229b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2230d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2231b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2232e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
223390ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2234b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2235e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2236e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2237b8d26b3bSNicholas Bellinger 
223890ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2239b8d26b3bSNicholas Bellinger 
2240e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2241e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2242e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2243e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2244e3d7e4c3SSagi Grimberg 	if (ret)
2245e3d7e4c3SSagi Grimberg 		return ret;
2246b8d26b3bSNicholas Bellinger 
2247e3d7e4c3SSagi Grimberg 	data_left = data->len;
2248e3d7e4c3SSagi Grimberg 	offset = data->offset;
2249b8d26b3bSNicholas Bellinger 
2250e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2251b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
225290ecc6e2SVu Pham 		pr_warn("Unable to allocate ib_sge\n");
2253b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2254e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2255b8d26b3bSNicholas Bellinger 	}
225690ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2257b8d26b3bSNicholas Bellinger 
2258e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2259b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2260b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2261b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
226290ecc6e2SVu Pham 		pr_debug("Unable to allocate wr->send_wr\n");
2263b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2264e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2265b8d26b3bSNicholas Bellinger 	}
2266b8d26b3bSNicholas Bellinger 
2267b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2268b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2269b8d26b3bSNicholas Bellinger 
2270b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2271b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2272b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2273b8d26b3bSNicholas Bellinger 
2274b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
227590ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
227690ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2277b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2278b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
227990ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
228090ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
228190ecc6e2SVu Pham 			else
228290ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
228390ecc6e2SVu Pham 		} else {
228490ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
228590ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
228690ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
228790ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
228890ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
228990ecc6e2SVu Pham 			else
229090ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
229190ecc6e2SVu Pham 		}
2292b8d26b3bSNicholas Bellinger 
2293b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2294b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2295b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2296b8d26b3bSNicholas Bellinger 
2297b8d26b3bSNicholas Bellinger 		offset += data_len;
229890ecc6e2SVu Pham 		va_offset += data_len;
2299b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2300b8d26b3bSNicholas Bellinger 	}
230190ecc6e2SVu Pham 
230290ecc6e2SVu Pham 	return 0;
2303e3d7e4c3SSagi Grimberg unmap_cmd:
2304e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2305e3d7e4c3SSagi Grimberg 
230690ecc6e2SVu Pham 	return ret;
230790ecc6e2SVu Pham }
230890ecc6e2SVu Pham 
230990ecc6e2SVu Pham static int
231059464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
231159464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
231259464ef4SVu Pham {
231359464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
231459464ef4SVu Pham 	struct scatterlist *tmp_sg;
231559464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
231659464ef4SVu Pham 
231759464ef4SVu Pham 	n_pages = 0;
231859464ef4SVu Pham 	new_chunk = 1;
231959464ef4SVu Pham 	last_ent = sg_nents - 1;
232059464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
232159464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
232259464ef4SVu Pham 		if (new_chunk)
232359464ef4SVu Pham 			chunk_start = start_addr;
232459464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
232559464ef4SVu Pham 
232659464ef4SVu Pham 		pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
232759464ef4SVu Pham 			 i, (unsigned long long)tmp_sg->dma_address,
232859464ef4SVu Pham 			 tmp_sg->length);
232959464ef4SVu Pham 
233059464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
233159464ef4SVu Pham 			new_chunk = 0;
233259464ef4SVu Pham 			continue;
233359464ef4SVu Pham 		}
233459464ef4SVu Pham 		new_chunk = 1;
233559464ef4SVu Pham 
233659464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
233759464ef4SVu Pham 		do {
233859464ef4SVu Pham 			fr_pl[n_pages++] = page;
233959464ef4SVu Pham 			pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
234059464ef4SVu Pham 				 n_pages - 1, page);
234159464ef4SVu Pham 			page += PAGE_SIZE;
234259464ef4SVu Pham 		} while (page < end_addr);
234359464ef4SVu Pham 	}
234459464ef4SVu Pham 
234559464ef4SVu Pham 	return n_pages;
234659464ef4SVu Pham }
234759464ef4SVu Pham 
234859464ef4SVu Pham static int
2349e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2350e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2351e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
2352e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
235359464ef4SVu Pham {
235459464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
235559464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
235659464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
23579bd626e7SSagi Grimberg 	int ret, pagelist_len;
23589bd626e7SSagi Grimberg 	u32 page_off;
235959464ef4SVu Pham 	u8 key;
236059464ef4SVu Pham 
2361e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
2362e3d7e4c3SSagi Grimberg 		sge->lkey = isert_conn->conn_mr->lkey;
2363e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2364e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
2365e3d7e4c3SSagi Grimberg 		return 0;
2366e3d7e4c3SSagi Grimberg 	}
2367e3d7e4c3SSagi Grimberg 
2368e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
236959464ef4SVu Pham 
23709bd626e7SSagi Grimberg 	pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2371e3d7e4c3SSagi Grimberg 		 fr_desc, mem->nents, mem->offset);
237259464ef4SVu Pham 
2373e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
237459464ef4SVu Pham 					     &fr_desc->data_frpl->page_list[0]);
237559464ef4SVu Pham 
2376d3e125daSSagi Grimberg 	if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
237759464ef4SVu Pham 		memset(&inv_wr, 0, sizeof(inv_wr));
23789bb4ca68SNicholas Bellinger 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
237959464ef4SVu Pham 		inv_wr.opcode = IB_WR_LOCAL_INV;
238059464ef4SVu Pham 		inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
238159464ef4SVu Pham 		wr = &inv_wr;
238259464ef4SVu Pham 		/* Bump the key */
238359464ef4SVu Pham 		key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
238459464ef4SVu Pham 		ib_update_fast_reg_key(fr_desc->data_mr, ++key);
238559464ef4SVu Pham 	}
238659464ef4SVu Pham 
238759464ef4SVu Pham 	/* Prepare FASTREG WR */
238859464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
23899bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
239059464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
239159464ef4SVu Pham 	fr_wr.wr.fast_reg.iova_start =
239259464ef4SVu Pham 		fr_desc->data_frpl->page_list[0] + page_off;
239359464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
239459464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
239559464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2396e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
239759464ef4SVu Pham 	fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
239859464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
239959464ef4SVu Pham 
240059464ef4SVu Pham 	if (!wr)
240159464ef4SVu Pham 		wr = &fr_wr;
240259464ef4SVu Pham 	else
240359464ef4SVu Pham 		wr->next = &fr_wr;
240459464ef4SVu Pham 
240559464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
240659464ef4SVu Pham 	if (ret) {
240759464ef4SVu Pham 		pr_err("fast registration failed, ret:%d\n", ret);
240859464ef4SVu Pham 		return ret;
240959464ef4SVu Pham 	}
2410d3e125daSSagi Grimberg 	fr_desc->ind &= ~ISERT_DATA_KEY_VALID;
241159464ef4SVu Pham 
2412e3d7e4c3SSagi Grimberg 	sge->lkey = fr_desc->data_mr->lkey;
2413e3d7e4c3SSagi Grimberg 	sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
2414e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
241559464ef4SVu Pham 
241659464ef4SVu Pham 	pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
2417e3d7e4c3SSagi Grimberg 		 sge->addr, sge->length, sge->lkey);
241859464ef4SVu Pham 
241959464ef4SVu Pham 	return ret;
242059464ef4SVu Pham }
242159464ef4SVu Pham 
242259464ef4SVu Pham static int
2423a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
242459464ef4SVu Pham 	       struct isert_rdma_wr *wr)
242559464ef4SVu Pham {
242659464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
242759464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2428e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
242959464ef4SVu Pham 	struct ib_send_wr *send_wr;
2430e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2431e3d7e4c3SSagi Grimberg 	u32 offset;
2432e3d7e4c3SSagi Grimberg 	int ret = 0;
243359464ef4SVu Pham 	unsigned long flags;
243459464ef4SVu Pham 
243559464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2436e3d7e4c3SSagi Grimberg 
2437e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2438e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2439e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2440e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2441e3d7e4c3SSagi Grimberg 	if (ret)
2442e3d7e4c3SSagi Grimberg 		return ret;
2443e3d7e4c3SSagi Grimberg 
2444e3d7e4c3SSagi Grimberg 	if (wr->data.dma_nents != 1) {
2445e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2446e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2447e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2448e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2449e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2450e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
245159464ef4SVu Pham 	}
245259464ef4SVu Pham 
2453e3d7e4c3SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data, &wr->s_ib_sge);
2454e3d7e4c3SSagi Grimberg 	if (ret)
2455e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
245659464ef4SVu Pham 
2457e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
245859464ef4SVu Pham 	wr->send_wr_num = 1;
245959464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
246059464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
246159464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
246259464ef4SVu Pham 
246359464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2464e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
246559464ef4SVu Pham 	send_wr->num_sge = 1;
246659464ef4SVu Pham 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
246759464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
246859464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
246959464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
247059464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
247159464ef4SVu Pham 		send_wr->send_flags = 0;
247259464ef4SVu Pham 		send_wr->next = &isert_cmd->tx_desc.send_wr;
247359464ef4SVu Pham 	} else {
247459464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
247559464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
247659464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
247759464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
247859464ef4SVu Pham 	}
247959464ef4SVu Pham 
248059464ef4SVu Pham 	return 0;
2481e3d7e4c3SSagi Grimberg unmap_cmd:
2482e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2483e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2484e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2485e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2486e3d7e4c3SSagi Grimberg 	}
2487e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
248859464ef4SVu Pham 
248959464ef4SVu Pham 	return ret;
249059464ef4SVu Pham }
249159464ef4SVu Pham 
249259464ef4SVu Pham static int
249390ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
249490ecc6e2SVu Pham {
249590ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
249659464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
249790ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
249890ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2499d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
250090ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
250190ecc6e2SVu Pham 	int rc;
250290ecc6e2SVu Pham 
250390ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
250490ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
250590ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2506d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
250790ecc6e2SVu Pham 	if (rc) {
250890ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
250990ecc6e2SVu Pham 		return rc;
251090ecc6e2SVu Pham 	}
251190ecc6e2SVu Pham 
2512b8d26b3bSNicholas Bellinger 	/*
2513b8d26b3bSNicholas Bellinger 	 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2514b8d26b3bSNicholas Bellinger 	 */
2515b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
251604d9cd12SNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2517b8d26b3bSNicholas Bellinger 			     &isert_cmd->tx_desc.iscsi_header);
2518b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
251995b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd,
252095b60f07SNicholas Bellinger 			   &isert_cmd->tx_desc.send_wr, true);
2521b8d26b3bSNicholas Bellinger 
2522b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
2523b8d26b3bSNicholas Bellinger 
2524b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2525b8d26b3bSNicholas Bellinger 	if (rc) {
2526b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2527b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
2528b8d26b3bSNicholas Bellinger 	}
252990ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
253090ecc6e2SVu Pham 		 isert_cmd);
2531b8d26b3bSNicholas Bellinger 
253290ecc6e2SVu Pham 	return 1;
2533b8d26b3bSNicholas Bellinger }
2534b8d26b3bSNicholas Bellinger 
2535b8d26b3bSNicholas Bellinger static int
2536b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2537b8d26b3bSNicholas Bellinger {
2538b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2539d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2540b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2541b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2542d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
254390ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
254490ecc6e2SVu Pham 	int rc;
2545b8d26b3bSNicholas Bellinger 
254690ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
254790ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2548b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2549d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
255090ecc6e2SVu Pham 	if (rc) {
255190ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
255290ecc6e2SVu Pham 		return rc;
2553b8d26b3bSNicholas Bellinger 	}
2554b8d26b3bSNicholas Bellinger 
2555b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2556b8d26b3bSNicholas Bellinger 
2557b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2558b8d26b3bSNicholas Bellinger 	if (rc) {
2559b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2560b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2561b8d26b3bSNicholas Bellinger 	}
256290ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
256390ecc6e2SVu Pham 		 isert_cmd);
2564b8d26b3bSNicholas Bellinger 
256590ecc6e2SVu Pham 	return 0;
2566b8d26b3bSNicholas Bellinger }
2567b8d26b3bSNicholas Bellinger 
2568b8d26b3bSNicholas Bellinger static int
2569b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2570b8d26b3bSNicholas Bellinger {
2571b8d26b3bSNicholas Bellinger 	int ret;
2572b8d26b3bSNicholas Bellinger 
2573b8d26b3bSNicholas Bellinger 	switch (state) {
2574b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2575b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2576b8d26b3bSNicholas Bellinger 		break;
2577b8d26b3bSNicholas Bellinger 	default:
2578b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
2579b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2580b8d26b3bSNicholas Bellinger 		break;
2581b8d26b3bSNicholas Bellinger 	}
2582b8d26b3bSNicholas Bellinger 
2583b8d26b3bSNicholas Bellinger 	return ret;
2584b8d26b3bSNicholas Bellinger }
2585b8d26b3bSNicholas Bellinger 
2586b8d26b3bSNicholas Bellinger static int
2587b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2588b8d26b3bSNicholas Bellinger {
2589b8d26b3bSNicholas Bellinger 	int ret;
2590b8d26b3bSNicholas Bellinger 
2591b8d26b3bSNicholas Bellinger 	switch (state) {
2592b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2593b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2594b8d26b3bSNicholas Bellinger 		if (!ret) {
2595b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
2596b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
2597b8d26b3bSNicholas Bellinger 		}
2598b8d26b3bSNicholas Bellinger 		break;
2599b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2600b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2601b8d26b3bSNicholas Bellinger 		break;
2602b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2603b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2604b8d26b3bSNicholas Bellinger 		break;
2605b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2606b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2607b8d26b3bSNicholas Bellinger 		break;
2608adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2609adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2610adb54c29SNicholas Bellinger 		break;
2611b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2612b8d26b3bSNicholas Bellinger 		/*
2613b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2614b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2615b8d26b3bSNicholas Bellinger 		 */
2616b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2617b8d26b3bSNicholas Bellinger 		break;
2618b8d26b3bSNicholas Bellinger 	default:
2619b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
2620b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2621b8d26b3bSNicholas Bellinger 		break;
2622b8d26b3bSNicholas Bellinger 	}
2623b8d26b3bSNicholas Bellinger 
2624b8d26b3bSNicholas Bellinger 	return ret;
2625b8d26b3bSNicholas Bellinger }
2626b8d26b3bSNicholas Bellinger 
2627b8d26b3bSNicholas Bellinger static int
2628b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
2629b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
2630b8d26b3bSNicholas Bellinger {
2631b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2632b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2633b8d26b3bSNicholas Bellinger 	struct sockaddr *sa;
2634b8d26b3bSNicholas Bellinger 	int ret;
2635b8d26b3bSNicholas Bellinger 
2636b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2637b8d26b3bSNicholas Bellinger 	if (!isert_np) {
2638b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
2639b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2640b8d26b3bSNicholas Bellinger 	}
2641b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_np->np_accept_wq);
2642b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
2643b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
2644b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
2645b8d26b3bSNicholas Bellinger 
2646b8d26b3bSNicholas Bellinger 	sa = (struct sockaddr *)ksockaddr;
2647b8d26b3bSNicholas Bellinger 	pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2648b8d26b3bSNicholas Bellinger 	/*
2649b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2650b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2651b8d26b3bSNicholas Bellinger 	 */
2652b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
2653b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
2654b8d26b3bSNicholas Bellinger 
2655b8d26b3bSNicholas Bellinger 	isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2656b8d26b3bSNicholas Bellinger 				IB_QPT_RC);
2657b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2658b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2659b8d26b3bSNicholas Bellinger 		       PTR_ERR(isert_lid));
2660b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2661b8d26b3bSNicholas Bellinger 		goto out;
2662b8d26b3bSNicholas Bellinger 	}
2663b8d26b3bSNicholas Bellinger 
2664b8d26b3bSNicholas Bellinger 	ret = rdma_bind_addr(isert_lid, sa);
2665b8d26b3bSNicholas Bellinger 	if (ret) {
2666b8d26b3bSNicholas Bellinger 		pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2667b8d26b3bSNicholas Bellinger 		goto out_lid;
2668b8d26b3bSNicholas Bellinger 	}
2669b8d26b3bSNicholas Bellinger 
2670b8d26b3bSNicholas Bellinger 	ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2671b8d26b3bSNicholas Bellinger 	if (ret) {
2672b8d26b3bSNicholas Bellinger 		pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2673b8d26b3bSNicholas Bellinger 		goto out_lid;
2674b8d26b3bSNicholas Bellinger 	}
2675b8d26b3bSNicholas Bellinger 
2676b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
2677b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2678b8d26b3bSNicholas Bellinger 	pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2679b8d26b3bSNicholas Bellinger 
2680b8d26b3bSNicholas Bellinger 	return 0;
2681b8d26b3bSNicholas Bellinger 
2682b8d26b3bSNicholas Bellinger out_lid:
2683b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_lid);
2684b8d26b3bSNicholas Bellinger out:
2685b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2686b8d26b3bSNicholas Bellinger 	return ret;
2687b8d26b3bSNicholas Bellinger }
2688b8d26b3bSNicholas Bellinger 
2689b8d26b3bSNicholas Bellinger static int
2690b8d26b3bSNicholas Bellinger isert_check_accept_queue(struct isert_np *isert_np)
2691b8d26b3bSNicholas Bellinger {
2692b8d26b3bSNicholas Bellinger 	int empty;
2693b8d26b3bSNicholas Bellinger 
2694b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
2695b8d26b3bSNicholas Bellinger 	empty = list_empty(&isert_np->np_accept_list);
2696b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
2697b8d26b3bSNicholas Bellinger 
2698b8d26b3bSNicholas Bellinger 	return empty;
2699b8d26b3bSNicholas Bellinger }
2700b8d26b3bSNicholas Bellinger 
2701b8d26b3bSNicholas Bellinger static int
2702b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2703b8d26b3bSNicholas Bellinger {
2704b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2705b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2706b8d26b3bSNicholas Bellinger 	int ret;
2707b8d26b3bSNicholas Bellinger 
2708b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2709b8d26b3bSNicholas Bellinger 	cp.responder_resources = isert_conn->responder_resources;
2710b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2711b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2712b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2713b8d26b3bSNicholas Bellinger 
2714b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2715b8d26b3bSNicholas Bellinger 
2716b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2717b8d26b3bSNicholas Bellinger 	if (ret) {
2718b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
2719b8d26b3bSNicholas Bellinger 		return ret;
2720b8d26b3bSNicholas Bellinger 	}
2721b8d26b3bSNicholas Bellinger 
2722b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2723b8d26b3bSNicholas Bellinger 
2724b8d26b3bSNicholas Bellinger 	return 0;
2725b8d26b3bSNicholas Bellinger }
2726b8d26b3bSNicholas Bellinger 
2727b8d26b3bSNicholas Bellinger static int
2728b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2729b8d26b3bSNicholas Bellinger {
2730b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2731b8d26b3bSNicholas Bellinger 	int ret;
2732b8d26b3bSNicholas Bellinger 
2733b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
27346faaa85fSNicholas Bellinger 	/*
27356faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
27366faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
27376faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
27386faaa85fSNicholas Bellinger 	 * into a NOP.
27396faaa85fSNicholas Bellinger 	 */
27406faaa85fSNicholas Bellinger 	if (!login->first_request)
27416faaa85fSNicholas Bellinger 		return 0;
2742b8d26b3bSNicholas Bellinger 
2743b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2744b8d26b3bSNicholas Bellinger 	if (ret)
2745b8d26b3bSNicholas Bellinger 		return ret;
2746b8d26b3bSNicholas Bellinger 
2747b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2748b8d26b3bSNicholas Bellinger 	return 0;
2749b8d26b3bSNicholas Bellinger }
2750b8d26b3bSNicholas Bellinger 
2751b8d26b3bSNicholas Bellinger static void
2752b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2753b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2754b8d26b3bSNicholas Bellinger {
2755b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2756b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2757b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
2758b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
2759b8d26b3bSNicholas Bellinger 
2760b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2761b8d26b3bSNicholas Bellinger 
2762b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
2763b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2764b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2765b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
2766b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
2767b8d26b3bSNicholas Bellinger 
2768b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2769b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2770b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
2771b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
2772b8d26b3bSNicholas Bellinger 	} else {
2773b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2774b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
2775b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
2776b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
2777b8d26b3bSNicholas Bellinger 
2778b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2779b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
2780b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
2781b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
2782b8d26b3bSNicholas Bellinger 	}
2783b8d26b3bSNicholas Bellinger }
2784b8d26b3bSNicholas Bellinger 
2785b8d26b3bSNicholas Bellinger static int
2786b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2787b8d26b3bSNicholas Bellinger {
2788b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
2789b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2790b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
2791b8d26b3bSNicholas Bellinger 
2792b8d26b3bSNicholas Bellinger accept_wait:
2793b8d26b3bSNicholas Bellinger 	ret = wait_event_interruptible(isert_np->np_accept_wq,
2794b8d26b3bSNicholas Bellinger 			!isert_check_accept_queue(isert_np) ||
2795b8d26b3bSNicholas Bellinger 			np->np_thread_state == ISCSI_NP_THREAD_RESET);
2796b8d26b3bSNicholas Bellinger 	if (max_accept > 5)
2797b8d26b3bSNicholas Bellinger 		return -ENODEV;
2798b8d26b3bSNicholas Bellinger 
2799b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2800b8d26b3bSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2801b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
2802b8d26b3bSNicholas Bellinger 		pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2803b8d26b3bSNicholas Bellinger 		return -ENODEV;
2804b8d26b3bSNicholas Bellinger 	}
2805b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2806b8d26b3bSNicholas Bellinger 
2807b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
2808b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
2809b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
2810b8d26b3bSNicholas Bellinger 		max_accept++;
2811b8d26b3bSNicholas Bellinger 		goto accept_wait;
2812b8d26b3bSNicholas Bellinger 	}
2813b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
2814b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
2815b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
2816b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
2817b8d26b3bSNicholas Bellinger 
2818b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2819b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2820b8d26b3bSNicholas Bellinger 	max_accept = 0;
2821b8d26b3bSNicholas Bellinger 
2822b8d26b3bSNicholas Bellinger 	ret = isert_rdma_post_recvl(isert_conn);
2823b8d26b3bSNicholas Bellinger 	if (ret)
2824b8d26b3bSNicholas Bellinger 		return ret;
2825b8d26b3bSNicholas Bellinger 
2826b8d26b3bSNicholas Bellinger 	ret = isert_rdma_accept(isert_conn);
2827b8d26b3bSNicholas Bellinger 	if (ret)
2828b8d26b3bSNicholas Bellinger 		return ret;
2829b8d26b3bSNicholas Bellinger 
2830b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2831b8d26b3bSNicholas Bellinger 
2832b8d26b3bSNicholas Bellinger 	pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2833b8d26b3bSNicholas Bellinger 	return 0;
2834b8d26b3bSNicholas Bellinger }
2835b8d26b3bSNicholas Bellinger 
2836b8d26b3bSNicholas Bellinger static void
2837b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2838b8d26b3bSNicholas Bellinger {
2839b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
2840b8d26b3bSNicholas Bellinger 
2841b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_np->np_cm_id);
2842b8d26b3bSNicholas Bellinger 
2843b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2844b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2845b8d26b3bSNicholas Bellinger }
2846b8d26b3bSNicholas Bellinger 
2847defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
2848b8d26b3bSNicholas Bellinger {
2849b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2850b8d26b3bSNicholas Bellinger 
2851defd8848SNicholas Bellinger 	pr_debug("isert_wait_conn: Starting \n");
2852b8d26b3bSNicholas Bellinger 	/*
2853b8d26b3bSNicholas Bellinger 	 * Decrement post_send_buf_count for special case when called
2854b8d26b3bSNicholas Bellinger 	 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2855b8d26b3bSNicholas Bellinger 	 */
2856b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2857b8d26b3bSNicholas Bellinger 	if (isert_conn->logout_posted)
2858b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2859b8d26b3bSNicholas Bellinger 
2860b2cb9649SNicholas Bellinger 	if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
2861defd8848SNicholas Bellinger 		pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
2862b8d26b3bSNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
2863b2cb9649SNicholas Bellinger 	}
2864b8d26b3bSNicholas Bellinger 	/*
2865b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
2866b8d26b3bSNicholas Bellinger 	 * into full feature phase..
2867b8d26b3bSNicholas Bellinger 	 */
2868b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
2869b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
2870b2cb9649SNicholas Bellinger 		return;
2871b2cb9649SNicholas Bellinger 	}
2872defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
2873defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
2874b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2875b2cb9649SNicholas Bellinger 
2876defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait_comp_err);
2877defd8848SNicholas Bellinger 
2878defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait);
2879defd8848SNicholas Bellinger }
2880defd8848SNicholas Bellinger 
2881defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2882defd8848SNicholas Bellinger {
2883defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2884b8d26b3bSNicholas Bellinger 
2885b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2886b8d26b3bSNicholas Bellinger }
2887b8d26b3bSNicholas Bellinger 
2888b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2889b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2890b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2891d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2892b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2893b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2894b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2895b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2896defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2897b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2898b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2899b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2900b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2901b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2902b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2903b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2904b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2905b8d26b3bSNicholas Bellinger };
2906b8d26b3bSNicholas Bellinger 
2907b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2908b8d26b3bSNicholas Bellinger {
2909b8d26b3bSNicholas Bellinger 	int ret;
2910b8d26b3bSNicholas Bellinger 
2911b8d26b3bSNicholas Bellinger 	isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2912b8d26b3bSNicholas Bellinger 	if (!isert_rx_wq) {
2913b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_rx_wq\n");
2914b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2915b8d26b3bSNicholas Bellinger 	}
2916b8d26b3bSNicholas Bellinger 
2917b8d26b3bSNicholas Bellinger 	isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2918b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
2919b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_comp_wq\n");
2920b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2921b8d26b3bSNicholas Bellinger 		goto destroy_rx_wq;
2922b8d26b3bSNicholas Bellinger 	}
2923b8d26b3bSNicholas Bellinger 
2924b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
2925b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2926b8d26b3bSNicholas Bellinger 	return 0;
2927b8d26b3bSNicholas Bellinger 
2928b8d26b3bSNicholas Bellinger destroy_rx_wq:
2929b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
2930b8d26b3bSNicholas Bellinger 	return ret;
2931b8d26b3bSNicholas Bellinger }
2932b8d26b3bSNicholas Bellinger 
2933b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2934b8d26b3bSNicholas Bellinger {
2935b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2936b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
2937b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
2938b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2939b8d26b3bSNicholas Bellinger }
2940b8d26b3bSNicholas Bellinger 
2941b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2942b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
2943b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2944b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2945b8d26b3bSNicholas Bellinger 
2946b8d26b3bSNicholas Bellinger module_init(isert_init);
2947b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2948