xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 9bb4ca68fc48eea618b1af1c1cde2a251ae32d1b)
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);
54d40945d8SVu Pham 
55d40945d8SVu Pham static void
56b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
57b8d26b3bSNicholas Bellinger {
58b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)context;
59b8d26b3bSNicholas Bellinger 
60b8d26b3bSNicholas Bellinger 	pr_err("isert_qp_event_callback event: %d\n", e->event);
61b8d26b3bSNicholas Bellinger 	switch (e->event) {
62b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
63b8d26b3bSNicholas Bellinger 		rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
64b8d26b3bSNicholas Bellinger 		break;
65b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
66b8d26b3bSNicholas Bellinger 		pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
67b8d26b3bSNicholas Bellinger 		break;
68b8d26b3bSNicholas Bellinger 	default:
69b8d26b3bSNicholas Bellinger 		break;
70b8d26b3bSNicholas Bellinger 	}
71b8d26b3bSNicholas Bellinger }
72b8d26b3bSNicholas Bellinger 
73b8d26b3bSNicholas Bellinger static int
74b8d26b3bSNicholas Bellinger isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
75b8d26b3bSNicholas Bellinger {
76b8d26b3bSNicholas Bellinger 	int ret;
77b8d26b3bSNicholas Bellinger 
78b8d26b3bSNicholas Bellinger 	ret = ib_query_device(ib_dev, devattr);
79b8d26b3bSNicholas Bellinger 	if (ret) {
80b8d26b3bSNicholas Bellinger 		pr_err("ib_query_device() failed: %d\n", ret);
81b8d26b3bSNicholas Bellinger 		return ret;
82b8d26b3bSNicholas Bellinger 	}
83b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
84b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
85b8d26b3bSNicholas Bellinger 
86b8d26b3bSNicholas Bellinger 	return 0;
87b8d26b3bSNicholas Bellinger }
88b8d26b3bSNicholas Bellinger 
89b8d26b3bSNicholas Bellinger static int
90b8d26b3bSNicholas Bellinger isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
91b8d26b3bSNicholas Bellinger {
92b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
93b8d26b3bSNicholas Bellinger 	struct ib_qp_init_attr attr;
94b8d26b3bSNicholas Bellinger 	int ret, index, min_index = 0;
95b8d26b3bSNicholas Bellinger 
96b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
97b8d26b3bSNicholas Bellinger 	for (index = 0; index < device->cqs_used; index++)
98b8d26b3bSNicholas Bellinger 		if (device->cq_active_qps[index] <
99b8d26b3bSNicholas Bellinger 		    device->cq_active_qps[min_index])
100b8d26b3bSNicholas Bellinger 			min_index = index;
101b8d26b3bSNicholas Bellinger 	device->cq_active_qps[min_index]++;
102b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
103b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
104b8d26b3bSNicholas Bellinger 
105b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
106b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
107b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
108b8d26b3bSNicholas Bellinger 	attr.send_cq = device->dev_tx_cq[min_index];
109b8d26b3bSNicholas Bellinger 	attr.recv_cq = device->dev_rx_cq[min_index];
110b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
111b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
112b8d26b3bSNicholas Bellinger 	/*
113b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
114b8d26b3bSNicholas Bellinger 	 * work-around for RDMA_READ..
115b8d26b3bSNicholas Bellinger 	 */
11659464ef4SVu Pham 	attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
117b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
118b8d26b3bSNicholas Bellinger 
119b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
120b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
121b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
122b8d26b3bSNicholas Bellinger 
123b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
124b8d26b3bSNicholas Bellinger 		 cma_id->device);
125b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
126b8d26b3bSNicholas Bellinger 		 isert_conn->conn_pd->device);
127b8d26b3bSNicholas Bellinger 
128b8d26b3bSNicholas Bellinger 	ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
129b8d26b3bSNicholas Bellinger 	if (ret) {
130b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_qp failed for cma_id %d\n", ret);
131b8d26b3bSNicholas Bellinger 		return ret;
132b8d26b3bSNicholas Bellinger 	}
133b8d26b3bSNicholas Bellinger 	isert_conn->conn_qp = cma_id->qp;
134b8d26b3bSNicholas Bellinger 	pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
135b8d26b3bSNicholas Bellinger 
136b8d26b3bSNicholas Bellinger 	return 0;
137b8d26b3bSNicholas Bellinger }
138b8d26b3bSNicholas Bellinger 
139b8d26b3bSNicholas Bellinger static void
140b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
141b8d26b3bSNicholas Bellinger {
142b8d26b3bSNicholas Bellinger 	pr_debug("isert_cq_event_callback event: %d\n", e->event);
143b8d26b3bSNicholas Bellinger }
144b8d26b3bSNicholas Bellinger 
145b8d26b3bSNicholas Bellinger static int
146b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
147b8d26b3bSNicholas Bellinger {
148b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
149b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
150b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
151b8d26b3bSNicholas Bellinger 	u64 dma_addr;
152b8d26b3bSNicholas Bellinger 	int i, j;
153b8d26b3bSNicholas Bellinger 
154b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
155b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
156b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
157b8d26b3bSNicholas Bellinger 		goto fail;
158b8d26b3bSNicholas Bellinger 
159b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
160b8d26b3bSNicholas Bellinger 
161b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
162b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
163b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
164b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
165b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
166b8d26b3bSNicholas Bellinger 
167b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
168b8d26b3bSNicholas Bellinger 
169b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
170b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
171b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
172b8d26b3bSNicholas Bellinger 		rx_sg->lkey = isert_conn->conn_mr->lkey;
173b8d26b3bSNicholas Bellinger 	}
174b8d26b3bSNicholas Bellinger 
175b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
176b8d26b3bSNicholas Bellinger 	return 0;
177b8d26b3bSNicholas Bellinger 
178b8d26b3bSNicholas Bellinger dma_map_fail:
179b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
180b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
181b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
182b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
183b8d26b3bSNicholas Bellinger 	}
184b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
185b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
186b8d26b3bSNicholas Bellinger fail:
187b8d26b3bSNicholas Bellinger 	return -ENOMEM;
188b8d26b3bSNicholas Bellinger }
189b8d26b3bSNicholas Bellinger 
190b8d26b3bSNicholas Bellinger static void
191b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
192b8d26b3bSNicholas Bellinger {
193b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
194b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
195b8d26b3bSNicholas Bellinger 	int i;
196b8d26b3bSNicholas Bellinger 
197b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
198b8d26b3bSNicholas Bellinger 		return;
199b8d26b3bSNicholas Bellinger 
200b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
201b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
202b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
203b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
204b8d26b3bSNicholas Bellinger 	}
205b8d26b3bSNicholas Bellinger 
206b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
207b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
208b8d26b3bSNicholas Bellinger }
209b8d26b3bSNicholas Bellinger 
2102853c2b6SNicholas Bellinger static void isert_cq_tx_work(struct work_struct *);
211b8d26b3bSNicholas Bellinger static void isert_cq_tx_callback(struct ib_cq *, void *);
2122853c2b6SNicholas Bellinger static void isert_cq_rx_work(struct work_struct *);
213b8d26b3bSNicholas Bellinger static void isert_cq_rx_callback(struct ib_cq *, void *);
214b8d26b3bSNicholas Bellinger 
215b8d26b3bSNicholas Bellinger static int
216b8d26b3bSNicholas Bellinger isert_create_device_ib_res(struct isert_device *device)
217b8d26b3bSNicholas Bellinger {
218b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = device->ib_device;
219b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
22059464ef4SVu Pham 	struct ib_device_attr *dev_attr;
221b8d26b3bSNicholas Bellinger 	int ret = 0, i, j;
222b8d26b3bSNicholas Bellinger 
22359464ef4SVu Pham 	dev_attr = &device->dev_attr;
22459464ef4SVu Pham 	ret = isert_query_device(ib_dev, dev_attr);
22559464ef4SVu Pham 	if (ret)
22659464ef4SVu Pham 		return ret;
22759464ef4SVu Pham 
228d40945d8SVu Pham 	/* asign function handlers */
22959464ef4SVu Pham 	if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
230a3a5a826SSagi Grimberg 		device->use_fastreg = 1;
231a3a5a826SSagi Grimberg 		device->reg_rdma_mem = isert_reg_rdma;
232a3a5a826SSagi Grimberg 		device->unreg_rdma_mem = isert_unreg_rdma;
23359464ef4SVu Pham 	} else {
234a3a5a826SSagi Grimberg 		device->use_fastreg = 0;
235d40945d8SVu Pham 		device->reg_rdma_mem = isert_map_rdma;
236d40945d8SVu Pham 		device->unreg_rdma_mem = isert_unmap_cmd;
23759464ef4SVu Pham 	}
238d40945d8SVu Pham 
239b8d26b3bSNicholas Bellinger 	device->cqs_used = min_t(int, num_online_cpus(),
240b8d26b3bSNicholas Bellinger 				 device->ib_device->num_comp_vectors);
241b8d26b3bSNicholas Bellinger 	device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
242a3a5a826SSagi Grimberg 	pr_debug("Using %d CQs, device %s supports %d vectors support "
243a3a5a826SSagi Grimberg 		 "Fast registration %d\n",
244b8d26b3bSNicholas Bellinger 		 device->cqs_used, device->ib_device->name,
245a3a5a826SSagi Grimberg 		 device->ib_device->num_comp_vectors, device->use_fastreg);
246b8d26b3bSNicholas Bellinger 	device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
247b8d26b3bSNicholas Bellinger 				device->cqs_used, GFP_KERNEL);
248b8d26b3bSNicholas Bellinger 	if (!device->cq_desc) {
249b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate device->cq_desc\n");
250b8d26b3bSNicholas Bellinger 		return -ENOMEM;
251b8d26b3bSNicholas Bellinger 	}
252b8d26b3bSNicholas Bellinger 	cq_desc = device->cq_desc;
253b8d26b3bSNicholas Bellinger 
254b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
255b8d26b3bSNicholas Bellinger 		cq_desc[i].device = device;
256b8d26b3bSNicholas Bellinger 		cq_desc[i].cq_index = i;
257b8d26b3bSNicholas Bellinger 
2582853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
259b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
260b8d26b3bSNicholas Bellinger 						isert_cq_rx_callback,
261b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
262b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
263b8d26b3bSNicholas Bellinger 						ISER_MAX_RX_CQ_LEN, i);
26494a71110SWei Yongjun 		if (IS_ERR(device->dev_rx_cq[i])) {
26594a71110SWei Yongjun 			ret = PTR_ERR(device->dev_rx_cq[i]);
26694a71110SWei Yongjun 			device->dev_rx_cq[i] = NULL;
267b8d26b3bSNicholas Bellinger 			goto out_cq;
26894a71110SWei Yongjun 		}
269b8d26b3bSNicholas Bellinger 
2702853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
271b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
272b8d26b3bSNicholas Bellinger 						isert_cq_tx_callback,
273b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
274b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
275b8d26b3bSNicholas Bellinger 						ISER_MAX_TX_CQ_LEN, i);
27694a71110SWei Yongjun 		if (IS_ERR(device->dev_tx_cq[i])) {
27794a71110SWei Yongjun 			ret = PTR_ERR(device->dev_tx_cq[i]);
27894a71110SWei Yongjun 			device->dev_tx_cq[i] = NULL;
27994a71110SWei Yongjun 			goto out_cq;
28094a71110SWei Yongjun 		}
28194a71110SWei Yongjun 
28294a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
28394a71110SWei Yongjun 		if (ret)
284b8d26b3bSNicholas Bellinger 			goto out_cq;
285b8d26b3bSNicholas Bellinger 
28694a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
28794a71110SWei Yongjun 		if (ret)
288b8d26b3bSNicholas Bellinger 			goto out_cq;
289b8d26b3bSNicholas Bellinger 	}
290b8d26b3bSNicholas Bellinger 
291b8d26b3bSNicholas Bellinger 	return 0;
292b8d26b3bSNicholas Bellinger 
293b8d26b3bSNicholas Bellinger out_cq:
294b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++) {
295b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[j];
296b8d26b3bSNicholas Bellinger 
297b8d26b3bSNicholas Bellinger 		if (device->dev_rx_cq[j]) {
298b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_rx_work);
299b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_rx_cq[j]);
300b8d26b3bSNicholas Bellinger 		}
301b8d26b3bSNicholas Bellinger 		if (device->dev_tx_cq[j]) {
302b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_tx_work);
303b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_tx_cq[j]);
304b8d26b3bSNicholas Bellinger 		}
305b8d26b3bSNicholas Bellinger 	}
306b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
307b8d26b3bSNicholas Bellinger 
308b8d26b3bSNicholas Bellinger 	return ret;
309b8d26b3bSNicholas Bellinger }
310b8d26b3bSNicholas Bellinger 
311b8d26b3bSNicholas Bellinger static void
312b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
313b8d26b3bSNicholas Bellinger {
314b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
315b8d26b3bSNicholas Bellinger 	int i;
316b8d26b3bSNicholas Bellinger 
317b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
318b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[i];
319b8d26b3bSNicholas Bellinger 
320b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_rx_work);
321b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_tx_work);
322b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_rx_cq[i]);
323b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_tx_cq[i]);
324b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = NULL;
325b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = NULL;
326b8d26b3bSNicholas Bellinger 	}
327b8d26b3bSNicholas Bellinger 
328b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
329b8d26b3bSNicholas Bellinger }
330b8d26b3bSNicholas Bellinger 
331b8d26b3bSNicholas Bellinger static void
332b8d26b3bSNicholas Bellinger isert_device_try_release(struct isert_device *device)
333b8d26b3bSNicholas Bellinger {
334b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
335b8d26b3bSNicholas Bellinger 	device->refcount--;
336b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
337b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
338b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
339b8d26b3bSNicholas Bellinger 		kfree(device);
340b8d26b3bSNicholas Bellinger 	}
341b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
342b8d26b3bSNicholas Bellinger }
343b8d26b3bSNicholas Bellinger 
344b8d26b3bSNicholas Bellinger static struct isert_device *
345b8d26b3bSNicholas Bellinger isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
346b8d26b3bSNicholas Bellinger {
347b8d26b3bSNicholas Bellinger 	struct isert_device *device;
348b8d26b3bSNicholas Bellinger 	int ret;
349b8d26b3bSNicholas Bellinger 
350b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
351b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
352b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
353b8d26b3bSNicholas Bellinger 			device->refcount++;
354b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
355b8d26b3bSNicholas Bellinger 			return device;
356b8d26b3bSNicholas Bellinger 		}
357b8d26b3bSNicholas Bellinger 	}
358b8d26b3bSNicholas Bellinger 
359b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
360b8d26b3bSNicholas Bellinger 	if (!device) {
361b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
362b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
363b8d26b3bSNicholas Bellinger 	}
364b8d26b3bSNicholas Bellinger 
365b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
366b8d26b3bSNicholas Bellinger 
367b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
368b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
369b8d26b3bSNicholas Bellinger 	if (ret) {
370b8d26b3bSNicholas Bellinger 		kfree(device);
371b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
372b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
373b8d26b3bSNicholas Bellinger 	}
374b8d26b3bSNicholas Bellinger 
375b8d26b3bSNicholas Bellinger 	device->refcount++;
376b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
377b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
378b8d26b3bSNicholas Bellinger 
379b8d26b3bSNicholas Bellinger 	return device;
380b8d26b3bSNicholas Bellinger }
381b8d26b3bSNicholas Bellinger 
38259464ef4SVu Pham static void
383a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
38459464ef4SVu Pham {
38559464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
38659464ef4SVu Pham 	int i = 0;
38759464ef4SVu Pham 
388a3a5a826SSagi Grimberg 	if (list_empty(&isert_conn->conn_fr_pool))
38959464ef4SVu Pham 		return;
39059464ef4SVu Pham 
391a3a5a826SSagi Grimberg 	pr_debug("Freeing conn %p fastreg pool", isert_conn);
39259464ef4SVu Pham 
39359464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
394a3a5a826SSagi Grimberg 				 &isert_conn->conn_fr_pool, list) {
39559464ef4SVu Pham 		list_del(&fr_desc->list);
39659464ef4SVu Pham 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
39759464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
39859464ef4SVu Pham 		kfree(fr_desc);
39959464ef4SVu Pham 		++i;
40059464ef4SVu Pham 	}
40159464ef4SVu Pham 
402a3a5a826SSagi Grimberg 	if (i < isert_conn->conn_fr_pool_size)
40359464ef4SVu Pham 		pr_warn("Pool still has %d regions registered\n",
404a3a5a826SSagi Grimberg 			isert_conn->conn_fr_pool_size - i);
40559464ef4SVu Pham }
40659464ef4SVu Pham 
40759464ef4SVu Pham static int
408dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
409dc87a90fSSagi Grimberg 		     struct fast_reg_descriptor *fr_desc)
410dc87a90fSSagi Grimberg {
411dc87a90fSSagi Grimberg 	fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
412dc87a90fSSagi Grimberg 							 ISCSI_ISER_SG_TABLESIZE);
413dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_frpl)) {
414dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frpl err=%ld\n",
415dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_frpl));
416dc87a90fSSagi Grimberg 		return PTR_ERR(fr_desc->data_frpl);
417dc87a90fSSagi Grimberg 	}
418dc87a90fSSagi Grimberg 
419dc87a90fSSagi Grimberg 	fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
420dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
421dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frmr err=%ld\n",
422dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_mr));
423dc87a90fSSagi Grimberg 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
424dc87a90fSSagi Grimberg 		return PTR_ERR(fr_desc->data_mr);
425dc87a90fSSagi Grimberg 	}
426dc87a90fSSagi Grimberg 	pr_debug("Create fr_desc %p page_list %p\n",
427dc87a90fSSagi Grimberg 		 fr_desc, fr_desc->data_frpl->page_list);
428dc87a90fSSagi Grimberg 
429dc87a90fSSagi Grimberg 	fr_desc->valid = true;
430dc87a90fSSagi Grimberg 
431dc87a90fSSagi Grimberg 	return 0;
432dc87a90fSSagi Grimberg }
433dc87a90fSSagi Grimberg 
434dc87a90fSSagi Grimberg static int
435a3a5a826SSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
43659464ef4SVu Pham {
43759464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
43859464ef4SVu Pham 	struct isert_device *device = isert_conn->conn_device;
43959464ef4SVu Pham 	int i, ret;
44059464ef4SVu Pham 
441a3a5a826SSagi Grimberg 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
442a3a5a826SSagi Grimberg 	isert_conn->conn_fr_pool_size = 0;
44359464ef4SVu Pham 	for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
44459464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
44559464ef4SVu Pham 		if (!fr_desc) {
44659464ef4SVu Pham 			pr_err("Failed to allocate fast_reg descriptor\n");
44759464ef4SVu Pham 			ret = -ENOMEM;
44859464ef4SVu Pham 			goto err;
44959464ef4SVu Pham 		}
45059464ef4SVu Pham 
451dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
452dc87a90fSSagi Grimberg 					   isert_conn->conn_pd, fr_desc);
453dc87a90fSSagi Grimberg 		if (ret) {
454dc87a90fSSagi Grimberg 			pr_err("Failed to create fastreg descriptor err=%d\n",
455dc87a90fSSagi Grimberg 			       ret);
456a80e21b3SNicholas Bellinger 			kfree(fr_desc);
45759464ef4SVu Pham 			goto err;
45859464ef4SVu Pham 		}
45959464ef4SVu Pham 
460a3a5a826SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
461a3a5a826SSagi Grimberg 		isert_conn->conn_fr_pool_size++;
46259464ef4SVu Pham 	}
46359464ef4SVu Pham 
464a3a5a826SSagi Grimberg 	pr_debug("Creating conn %p fastreg pool size=%d",
465a3a5a826SSagi Grimberg 		 isert_conn, isert_conn->conn_fr_pool_size);
46659464ef4SVu Pham 
46759464ef4SVu Pham 	return 0;
46859464ef4SVu Pham 
46959464ef4SVu Pham err:
470a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
47159464ef4SVu Pham 	return ret;
47259464ef4SVu Pham }
47359464ef4SVu Pham 
474b8d26b3bSNicholas Bellinger static int
475b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
476b8d26b3bSNicholas Bellinger {
477b8d26b3bSNicholas Bellinger 	struct iscsi_np *np = cma_id->context;
478b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = np->np_context;
479b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
480b8d26b3bSNicholas Bellinger 	struct isert_device *device;
481b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
482b8d26b3bSNicholas Bellinger 	int ret = 0;
483b8d26b3bSNicholas Bellinger 
484b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
485b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
486b8d26b3bSNicholas Bellinger 
487b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
488b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
489b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn\n");
490b8d26b3bSNicholas Bellinger 		return -ENOMEM;
491b8d26b3bSNicholas Bellinger 	}
492b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
493b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
494b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
495defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait);
496defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait_comp_err);
497b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
498b8d26b3bSNicholas Bellinger 	kref_get(&isert_conn->conn_kref);
499b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
50095b60f07SNicholas Bellinger 	mutex_init(&isert_conn->conn_comp_mutex);
50159464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
502b8d26b3bSNicholas Bellinger 
503b8d26b3bSNicholas Bellinger 	cma_id->context = isert_conn;
504b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
505b8d26b3bSNicholas Bellinger 	isert_conn->responder_resources = event->param.conn.responder_resources;
506b8d26b3bSNicholas Bellinger 	isert_conn->initiator_depth = event->param.conn.initiator_depth;
507b8d26b3bSNicholas Bellinger 	pr_debug("Using responder_resources: %u initiator_depth: %u\n",
508b8d26b3bSNicholas Bellinger 		 isert_conn->responder_resources, isert_conn->initiator_depth);
509b8d26b3bSNicholas Bellinger 
510b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
511b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
512b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
513b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
514b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
515b8d26b3bSNicholas Bellinger 		goto out;
516b8d26b3bSNicholas Bellinger 	}
517b8d26b3bSNicholas Bellinger 
518b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
519b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
520b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
521b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
522b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
523b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
524b8d26b3bSNicholas Bellinger 
525b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
526b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
527b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
528b8d26b3bSNicholas Bellinger 
529b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
530b8d26b3bSNicholas Bellinger 	if (ret) {
531b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
532b8d26b3bSNicholas Bellinger 		       ret);
533b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
534b8d26b3bSNicholas Bellinger 		goto out_login_buf;
535b8d26b3bSNicholas Bellinger 	}
536b8d26b3bSNicholas Bellinger 
537b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
538b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
539b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
540b8d26b3bSNicholas Bellinger 
541b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
542b8d26b3bSNicholas Bellinger 	if (ret) {
543b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
544b8d26b3bSNicholas Bellinger 		       ret);
545b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
546b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
547b8d26b3bSNicholas Bellinger 	}
548b8d26b3bSNicholas Bellinger 
549b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
550b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
551b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
552b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
553b8d26b3bSNicholas Bellinger 	}
554b8d26b3bSNicholas Bellinger 
555b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
556eb6ab132SSagi Grimberg 	isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
557eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_pd)) {
558eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_pd);
559eb6ab132SSagi Grimberg 		pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
560eb6ab132SSagi Grimberg 		       isert_conn, ret);
561eb6ab132SSagi Grimberg 		goto out_pd;
562eb6ab132SSagi Grimberg 	}
563b8d26b3bSNicholas Bellinger 
564eb6ab132SSagi Grimberg 	isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
565eb6ab132SSagi Grimberg 					   IB_ACCESS_LOCAL_WRITE);
566eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_mr)) {
567eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_mr);
568eb6ab132SSagi Grimberg 		pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
569eb6ab132SSagi Grimberg 		       isert_conn, ret);
570eb6ab132SSagi Grimberg 		goto out_mr;
571eb6ab132SSagi Grimberg 	}
572b8d26b3bSNicholas Bellinger 
573a3a5a826SSagi Grimberg 	if (device->use_fastreg) {
574a3a5a826SSagi Grimberg 		ret = isert_conn_create_fastreg_pool(isert_conn);
57559464ef4SVu Pham 		if (ret) {
576a3a5a826SSagi Grimberg 			pr_err("Conn: %p failed to create fastreg pool\n",
577a3a5a826SSagi Grimberg 			       isert_conn);
578a3a5a826SSagi Grimberg 			goto out_fastreg;
57959464ef4SVu Pham 		}
58059464ef4SVu Pham 	}
58159464ef4SVu Pham 
582b8d26b3bSNicholas Bellinger 	ret = isert_conn_setup_qp(isert_conn, cma_id);
583b8d26b3bSNicholas Bellinger 	if (ret)
584b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
585b8d26b3bSNicholas Bellinger 
586b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
587b8d26b3bSNicholas Bellinger 	list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
588b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
589b8d26b3bSNicholas Bellinger 
590b8d26b3bSNicholas Bellinger 	pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
591b8d26b3bSNicholas Bellinger 	wake_up(&isert_np->np_accept_wq);
592b8d26b3bSNicholas Bellinger 	return 0;
593b8d26b3bSNicholas Bellinger 
594b8d26b3bSNicholas Bellinger out_conn_dev:
595a3a5a826SSagi Grimberg 	if (device->use_fastreg)
596a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
597a3a5a826SSagi Grimberg out_fastreg:
598eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
599eb6ab132SSagi Grimberg out_mr:
600eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
601eb6ab132SSagi Grimberg out_pd:
602b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
603b8d26b3bSNicholas Bellinger out_rsp_dma_map:
604b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
605b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
606b8d26b3bSNicholas Bellinger out_req_dma_map:
607b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
608b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
609b8d26b3bSNicholas Bellinger out_login_buf:
610b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
611b8d26b3bSNicholas Bellinger out:
612b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
613b8d26b3bSNicholas Bellinger 	return ret;
614b8d26b3bSNicholas Bellinger }
615b8d26b3bSNicholas Bellinger 
616b8d26b3bSNicholas Bellinger static void
617b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
618b8d26b3bSNicholas Bellinger {
619b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
620b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
621b8d26b3bSNicholas Bellinger 	int cq_index;
622b8d26b3bSNicholas Bellinger 
623b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
624b8d26b3bSNicholas Bellinger 
625a3a5a826SSagi Grimberg 	if (device && device->use_fastreg)
626a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
62759464ef4SVu Pham 
628b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
629b8d26b3bSNicholas Bellinger 		cq_index = ((struct isert_cq_desc *)
630b8d26b3bSNicholas Bellinger 			isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
631b8d26b3bSNicholas Bellinger 		pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
632b8d26b3bSNicholas Bellinger 		isert_conn->conn_device->cq_active_qps[cq_index]--;
633b8d26b3bSNicholas Bellinger 
634b8d26b3bSNicholas Bellinger 		rdma_destroy_qp(isert_conn->conn_cm_id);
635b8d26b3bSNicholas Bellinger 	}
636b8d26b3bSNicholas Bellinger 
637b8d26b3bSNicholas Bellinger 	isert_free_rx_descriptors(isert_conn);
638b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_conn->conn_cm_id);
639b8d26b3bSNicholas Bellinger 
640eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
641eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
642eb6ab132SSagi Grimberg 
643b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
644b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
645b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
646b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
647b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
648b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
649b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
650b8d26b3bSNicholas Bellinger 	}
651b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
652b8d26b3bSNicholas Bellinger 
653b8d26b3bSNicholas Bellinger 	if (device)
654b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
655b8d26b3bSNicholas Bellinger 
656b8d26b3bSNicholas Bellinger 	pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
657b8d26b3bSNicholas Bellinger }
658b8d26b3bSNicholas Bellinger 
659b8d26b3bSNicholas Bellinger static void
660b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
661b8d26b3bSNicholas Bellinger {
662b8d26b3bSNicholas Bellinger 	return;
663b8d26b3bSNicholas Bellinger }
664b8d26b3bSNicholas Bellinger 
665b8d26b3bSNicholas Bellinger static void
666b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
667b8d26b3bSNicholas Bellinger {
668b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
669b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
670b8d26b3bSNicholas Bellinger 
671b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
672b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
673b8d26b3bSNicholas Bellinger 
674b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
675b8d26b3bSNicholas Bellinger }
676b8d26b3bSNicholas Bellinger 
677b8d26b3bSNicholas Bellinger static void
678b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
679b8d26b3bSNicholas Bellinger {
680b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
681b8d26b3bSNicholas Bellinger }
682b8d26b3bSNicholas Bellinger 
683b8d26b3bSNicholas Bellinger static void
684b8d26b3bSNicholas Bellinger isert_disconnect_work(struct work_struct *work)
685b8d26b3bSNicholas Bellinger {
686b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(work,
687b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_logout_work);
688b8d26b3bSNicholas Bellinger 
689b8d26b3bSNicholas Bellinger 	pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
690b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
691defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
692defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
693b8d26b3bSNicholas Bellinger 
694b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
695b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
696b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
697b2cb9649SNicholas Bellinger 		goto wake_up;
698b8d26b3bSNicholas Bellinger 	}
699b2cb9649SNicholas Bellinger 	if (!isert_conn->conn_cm_id) {
700b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
701b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
702b2cb9649SNicholas Bellinger 		return;
703b2cb9649SNicholas Bellinger 	}
704b2cb9649SNicholas Bellinger 	if (!isert_conn->logout_posted) {
705b2cb9649SNicholas Bellinger 		pr_debug("Calling rdma_disconnect for !logout_posted from"
706b2cb9649SNicholas Bellinger 			 " isert_disconnect_work\n");
707b2cb9649SNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
708b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
709b2cb9649SNicholas Bellinger 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
710b2cb9649SNicholas Bellinger 		goto wake_up;
711b2cb9649SNicholas Bellinger 	}
712b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
713b8d26b3bSNicholas Bellinger 
714b2cb9649SNicholas Bellinger wake_up:
715defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait);
716b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
717b8d26b3bSNicholas Bellinger }
718b8d26b3bSNicholas Bellinger 
719b8d26b3bSNicholas Bellinger static void
720b8d26b3bSNicholas Bellinger isert_disconnected_handler(struct rdma_cm_id *cma_id)
721b8d26b3bSNicholas Bellinger {
722b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
723b8d26b3bSNicholas Bellinger 
724b8d26b3bSNicholas Bellinger 	INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
725b8d26b3bSNicholas Bellinger 	schedule_work(&isert_conn->conn_logout_work);
726b8d26b3bSNicholas Bellinger }
727b8d26b3bSNicholas Bellinger 
728b8d26b3bSNicholas Bellinger static int
729b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
730b8d26b3bSNicholas Bellinger {
731b8d26b3bSNicholas Bellinger 	int ret = 0;
732b8d26b3bSNicholas Bellinger 
733b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
734b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
735b8d26b3bSNicholas Bellinger 
736b8d26b3bSNicholas Bellinger 	switch (event->event) {
737b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
738b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
739b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
740b8d26b3bSNicholas Bellinger 		break;
741b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
742b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
743b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
744b8d26b3bSNicholas Bellinger 		break;
745b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DISCONNECTED:
746b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
747b8d26b3bSNicholas Bellinger 		isert_disconnected_handler(cma_id);
748b8d26b3bSNicholas Bellinger 		break;
749b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
750b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ADDR_CHANGE:
751b8d26b3bSNicholas Bellinger 		break;
752b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
753b8d26b3bSNicholas Bellinger 	default:
754b8d26b3bSNicholas Bellinger 		pr_err("Unknown RDMA CMA event: %d\n", event->event);
755b8d26b3bSNicholas Bellinger 		break;
756b8d26b3bSNicholas Bellinger 	}
757b8d26b3bSNicholas Bellinger 
758b8d26b3bSNicholas Bellinger 	if (ret != 0) {
759b8d26b3bSNicholas Bellinger 		pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
760b8d26b3bSNicholas Bellinger 		       event->event, ret);
761b8d26b3bSNicholas Bellinger 		dump_stack();
762b8d26b3bSNicholas Bellinger 	}
763b8d26b3bSNicholas Bellinger 
764b8d26b3bSNicholas Bellinger 	return ret;
765b8d26b3bSNicholas Bellinger }
766b8d26b3bSNicholas Bellinger 
767b8d26b3bSNicholas Bellinger static int
768b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
769b8d26b3bSNicholas Bellinger {
770b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
771b8d26b3bSNicholas Bellinger 	int i, ret;
772b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
773b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
774b8d26b3bSNicholas Bellinger 
775b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
776b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
777b8d26b3bSNicholas Bellinger 		rx_wr->wr_id	= (unsigned long)rx_desc;
778b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
779b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
780b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
781b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
782b8d26b3bSNicholas Bellinger 	}
783b8d26b3bSNicholas Bellinger 
784b8d26b3bSNicholas Bellinger 	rx_wr--;
785b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
786b8d26b3bSNicholas Bellinger 
787b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
788b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
789b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
790b8d26b3bSNicholas Bellinger 	if (ret) {
791b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed with ret: %d\n", ret);
792b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
793b8d26b3bSNicholas Bellinger 	} else {
794b8d26b3bSNicholas Bellinger 		pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
795b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
796b8d26b3bSNicholas Bellinger 	}
797b8d26b3bSNicholas Bellinger 	return ret;
798b8d26b3bSNicholas Bellinger }
799b8d26b3bSNicholas Bellinger 
800b8d26b3bSNicholas Bellinger static int
801b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
802b8d26b3bSNicholas Bellinger {
803b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
804b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
805b8d26b3bSNicholas Bellinger 	int ret;
806b8d26b3bSNicholas Bellinger 
807b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
808b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
809b8d26b3bSNicholas Bellinger 
810b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
811b8d26b3bSNicholas Bellinger 	send_wr.wr_id	= (unsigned long)tx_desc;
812b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
813b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
814b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
815b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
816b8d26b3bSNicholas Bellinger 
817b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
818b8d26b3bSNicholas Bellinger 
819b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
820b8d26b3bSNicholas Bellinger 	if (ret) {
821b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send() failed, ret: %d\n", ret);
822b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
823b8d26b3bSNicholas Bellinger 	}
824b8d26b3bSNicholas Bellinger 
825b8d26b3bSNicholas Bellinger 	return ret;
826b8d26b3bSNicholas Bellinger }
827b8d26b3bSNicholas Bellinger 
828b8d26b3bSNicholas Bellinger static void
829b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
830b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
831b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
832b8d26b3bSNicholas Bellinger {
833b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
834b8d26b3bSNicholas Bellinger 
835b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
836b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
837b8d26b3bSNicholas Bellinger 
838b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
839b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
840b8d26b3bSNicholas Bellinger 
841b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
842b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
843b8d26b3bSNicholas Bellinger 
844b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
845b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
846b8d26b3bSNicholas Bellinger 		pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
847b8d26b3bSNicholas Bellinger 	}
848b8d26b3bSNicholas Bellinger }
849b8d26b3bSNicholas Bellinger 
850b8d26b3bSNicholas Bellinger static int
851b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
852b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
853b8d26b3bSNicholas Bellinger {
854b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
855b8d26b3bSNicholas Bellinger 	u64 dma_addr;
856b8d26b3bSNicholas Bellinger 
857b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
858b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
859b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
860b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error() failed\n");
861b8d26b3bSNicholas Bellinger 		return -ENOMEM;
862b8d26b3bSNicholas Bellinger 	}
863b8d26b3bSNicholas Bellinger 
864b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
865b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
866b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
867b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
868b8d26b3bSNicholas Bellinger 
869b8d26b3bSNicholas Bellinger 	pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
870b8d26b3bSNicholas Bellinger 		 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
871b8d26b3bSNicholas Bellinger 		 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
872b8d26b3bSNicholas Bellinger 
873b8d26b3bSNicholas Bellinger 	return 0;
874b8d26b3bSNicholas Bellinger }
875b8d26b3bSNicholas Bellinger 
876b8d26b3bSNicholas Bellinger static void
87795b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
87895b60f07SNicholas Bellinger 		   struct ib_send_wr *send_wr, bool coalesce)
879b8d26b3bSNicholas Bellinger {
88095b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
88195b60f07SNicholas Bellinger 
882b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
883b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
884b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
88595b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
886b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
88795b60f07SNicholas Bellinger 	/*
88895b60f07SNicholas Bellinger 	 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
88995b60f07SNicholas Bellinger 	 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
89095b60f07SNicholas Bellinger 	 */
89195b60f07SNicholas Bellinger 	mutex_lock(&isert_conn->conn_comp_mutex);
89295b60f07SNicholas Bellinger 	if (coalesce &&
89395b60f07SNicholas Bellinger 	    ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
89495b60f07SNicholas Bellinger 		llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
89595b60f07SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_comp_mutex);
89695b60f07SNicholas Bellinger 		return;
89795b60f07SNicholas Bellinger 	}
89895b60f07SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
89995b60f07SNicholas Bellinger 	tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
90095b60f07SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_comp_mutex);
90195b60f07SNicholas Bellinger 
90295b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
903b8d26b3bSNicholas Bellinger }
904b8d26b3bSNicholas Bellinger 
905b8d26b3bSNicholas Bellinger static int
906b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
907b8d26b3bSNicholas Bellinger {
908b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
909b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
910b8d26b3bSNicholas Bellinger 	int ret;
911b8d26b3bSNicholas Bellinger 
912b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
913b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
914b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
915b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
916b8d26b3bSNicholas Bellinger 
917b8d26b3bSNicholas Bellinger 	pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
918b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
919b8d26b3bSNicholas Bellinger 
920b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
921b8d26b3bSNicholas Bellinger 	rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
922b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
923b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
924b8d26b3bSNicholas Bellinger 
925b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
926b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
927b8d26b3bSNicholas Bellinger 	if (ret) {
928b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed: %d\n", ret);
929b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
930b8d26b3bSNicholas Bellinger 	}
931b8d26b3bSNicholas Bellinger 
932b8d26b3bSNicholas Bellinger 	pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
933b8d26b3bSNicholas Bellinger 	return ret;
934b8d26b3bSNicholas Bellinger }
935b8d26b3bSNicholas Bellinger 
936b8d26b3bSNicholas Bellinger static int
937b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
938b8d26b3bSNicholas Bellinger 		   u32 length)
939b8d26b3bSNicholas Bellinger {
940b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
941b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
942b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
943b8d26b3bSNicholas Bellinger 	int ret;
944b8d26b3bSNicholas Bellinger 
945b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
946b8d26b3bSNicholas Bellinger 
947b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
948b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
949b8d26b3bSNicholas Bellinger 
950b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
951b8d26b3bSNicholas Bellinger 
952b8d26b3bSNicholas Bellinger 	if (length > 0) {
953b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
954b8d26b3bSNicholas Bellinger 
955b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
956b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
957b8d26b3bSNicholas Bellinger 
958b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
959b8d26b3bSNicholas Bellinger 
960b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
961b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
962b8d26b3bSNicholas Bellinger 
963b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
964b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
965b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
966b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
967b8d26b3bSNicholas Bellinger 	}
968b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
969b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
970b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
971b8d26b3bSNicholas Bellinger 			if (ret)
972b8d26b3bSNicholas Bellinger 				return ret;
973b8d26b3bSNicholas Bellinger 
974b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
975b8d26b3bSNicholas Bellinger 			if (ret)
976b8d26b3bSNicholas Bellinger 				return ret;
977b8d26b3bSNicholas Bellinger 
978b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_UP;
979b8d26b3bSNicholas Bellinger 			goto post_send;
980b8d26b3bSNicholas Bellinger 		}
981b8d26b3bSNicholas Bellinger 
982b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
983b8d26b3bSNicholas Bellinger 		if (ret)
984b8d26b3bSNicholas Bellinger 			return ret;
985b8d26b3bSNicholas Bellinger 	}
986b8d26b3bSNicholas Bellinger post_send:
987b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
988b8d26b3bSNicholas Bellinger 	if (ret)
989b8d26b3bSNicholas Bellinger 		return ret;
990b8d26b3bSNicholas Bellinger 
991b8d26b3bSNicholas Bellinger 	return 0;
992b8d26b3bSNicholas Bellinger }
993b8d26b3bSNicholas Bellinger 
994b8d26b3bSNicholas Bellinger static void
995b8d26b3bSNicholas Bellinger isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
996b8d26b3bSNicholas Bellinger 		   struct isert_conn *isert_conn)
997b8d26b3bSNicholas Bellinger {
998b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
999b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1000b8d26b3bSNicholas Bellinger 	int size;
1001b8d26b3bSNicholas Bellinger 
1002b8d26b3bSNicholas Bellinger 	if (!login) {
1003b8d26b3bSNicholas Bellinger 		pr_err("conn->conn_login is NULL\n");
1004b8d26b3bSNicholas Bellinger 		dump_stack();
1005b8d26b3bSNicholas Bellinger 		return;
1006b8d26b3bSNicholas Bellinger 	}
1007b8d26b3bSNicholas Bellinger 
1008b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1009b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1010b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1011b8d26b3bSNicholas Bellinger 		/*
1012b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1013b8d26b3bSNicholas Bellinger 		 * login request PDU.
1014b8d26b3bSNicholas Bellinger 		 */
1015b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1016b8d26b3bSNicholas Bellinger 		login->current_stage =
1017b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1018b8d26b3bSNicholas Bellinger 			 >> 2;
1019b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1020b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1021b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1022b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1023b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1024b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1025b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1026b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1027b8d26b3bSNicholas Bellinger 	}
1028b8d26b3bSNicholas Bellinger 
1029b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1030b8d26b3bSNicholas Bellinger 
1031b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1032b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1033b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1034b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1035b8d26b3bSNicholas Bellinger 
10366faaa85fSNicholas Bellinger 	if (login->first_request) {
1037b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
10386faaa85fSNicholas Bellinger 		return;
10396faaa85fSNicholas Bellinger 	}
10406faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1041b8d26b3bSNicholas Bellinger }
1042b8d26b3bSNicholas Bellinger 
1043b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1044676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1045b8d26b3bSNicholas Bellinger {
1046b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1047b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1048d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1049b8d26b3bSNicholas Bellinger 
1050676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1051d703ce2fSNicholas Bellinger 	if (!cmd) {
1052d703ce2fSNicholas Bellinger 		pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1053b8d26b3bSNicholas Bellinger 		return NULL;
1054b8d26b3bSNicholas Bellinger 	}
1055d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1056b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1057d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1058b8d26b3bSNicholas Bellinger 
1059d703ce2fSNicholas Bellinger 	return cmd;
1060b8d26b3bSNicholas Bellinger }
1061b8d26b3bSNicholas Bellinger 
1062b8d26b3bSNicholas Bellinger static int
1063b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1064d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1065d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1066b8d26b3bSNicholas Bellinger {
1067b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1068b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1069b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1070b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1071b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1072b8d26b3bSNicholas Bellinger 
1073b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1074b8d26b3bSNicholas Bellinger 	if (rc < 0)
1075b8d26b3bSNicholas Bellinger 		return rc;
1076b8d26b3bSNicholas Bellinger 
1077b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1078b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1079b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1080b8d26b3bSNicholas Bellinger 
1081b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1082b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1083b8d26b3bSNicholas Bellinger 		return 0;
1084b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1085b8d26b3bSNicholas Bellinger 		dump_payload = true;
1086b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1087b8d26b3bSNicholas Bellinger 	}
1088b8d26b3bSNicholas Bellinger 
1089b8d26b3bSNicholas Bellinger 	if (!imm_data)
1090b8d26b3bSNicholas Bellinger 		return 0;
1091b8d26b3bSNicholas Bellinger 
1092b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1093b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1094b8d26b3bSNicholas Bellinger 
1095b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1096b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1097b8d26b3bSNicholas Bellinger 
1098b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1099b8d26b3bSNicholas Bellinger 
1100b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1101b8d26b3bSNicholas Bellinger 
1102b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1103b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1104b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1105b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1106b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1107b8d26b3bSNicholas Bellinger 	}
1108b8d26b3bSNicholas Bellinger 
1109b8d26b3bSNicholas Bellinger sequence_cmd:
1110561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1111b8d26b3bSNicholas Bellinger 
1112b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1113b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
1114b8d26b3bSNicholas Bellinger 
1115b8d26b3bSNicholas Bellinger 	return 0;
1116b8d26b3bSNicholas Bellinger }
1117b8d26b3bSNicholas Bellinger 
1118b8d26b3bSNicholas Bellinger static int
1119b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1120b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1121b8d26b3bSNicholas Bellinger {
1122b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1123b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1124b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1125b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1126b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1127b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1128b8d26b3bSNicholas Bellinger 
1129b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1130b8d26b3bSNicholas Bellinger 	if (rc < 0)
1131b8d26b3bSNicholas Bellinger 		return rc;
1132b8d26b3bSNicholas Bellinger 	else if (!cmd)
1133b8d26b3bSNicholas Bellinger 		return 0;
1134b8d26b3bSNicholas Bellinger 	/*
1135b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1136b8d26b3bSNicholas Bellinger 	 */
1137b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
1138b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
1139b8d26b3bSNicholas Bellinger 		dump_stack();
1140b8d26b3bSNicholas Bellinger 		return -1;
1141b8d26b3bSNicholas Bellinger 	}
1142b8d26b3bSNicholas Bellinger 
1143b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1144b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1145b8d26b3bSNicholas Bellinger 
1146b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1147b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1148b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1149b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1150b8d26b3bSNicholas Bellinger 	/*
1151b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1152b8d26b3bSNicholas Bellinger 	 */
1153b8d26b3bSNicholas Bellinger 	if (page_off) {
1154b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1155b8d26b3bSNicholas Bellinger 		dump_stack();
1156b8d26b3bSNicholas Bellinger 		return -1;
1157b8d26b3bSNicholas Bellinger 	}
1158b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1159b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1160b8d26b3bSNicholas Bellinger 
1161b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1162b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1163b8d26b3bSNicholas Bellinger 
1164b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1165b8d26b3bSNicholas Bellinger 	if (rc < 0)
1166b8d26b3bSNicholas Bellinger 		return rc;
1167b8d26b3bSNicholas Bellinger 
1168b8d26b3bSNicholas Bellinger 	return 0;
1169b8d26b3bSNicholas Bellinger }
1170b8d26b3bSNicholas Bellinger 
1171b8d26b3bSNicholas Bellinger static int
1172778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1173d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1174d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1175778de368SNicholas Bellinger {
1176778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1177778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1178778de368SNicholas Bellinger 	int rc;
1179778de368SNicholas Bellinger 
1180778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1181778de368SNicholas Bellinger 	if (rc < 0)
1182778de368SNicholas Bellinger 		return rc;
1183778de368SNicholas Bellinger 	/*
1184778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1185778de368SNicholas Bellinger 	 */
1186778de368SNicholas Bellinger 
1187778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1188778de368SNicholas Bellinger }
1189778de368SNicholas Bellinger 
1190778de368SNicholas Bellinger static int
1191adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1192d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1193d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1194adb54c29SNicholas Bellinger {
1195adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1196adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1197adb54c29SNicholas Bellinger 	int rc;
1198adb54c29SNicholas Bellinger 	unsigned char *text_in;
1199adb54c29SNicholas Bellinger 
1200adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1201adb54c29SNicholas Bellinger 	if (rc < 0)
1202adb54c29SNicholas Bellinger 		return rc;
1203adb54c29SNicholas Bellinger 
1204adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1205adb54c29SNicholas Bellinger 	if (!text_in) {
1206adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1207adb54c29SNicholas Bellinger 		       payload_length);
1208adb54c29SNicholas Bellinger 		return -ENOMEM;
1209adb54c29SNicholas Bellinger 	}
1210adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1211adb54c29SNicholas Bellinger 
1212adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1213adb54c29SNicholas Bellinger 
1214adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1215adb54c29SNicholas Bellinger }
1216adb54c29SNicholas Bellinger 
1217adb54c29SNicholas Bellinger static int
1218b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1219b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1220b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1221b8d26b3bSNicholas Bellinger {
1222b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1223b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1224ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1225b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1226b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1227b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1228b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1229b8d26b3bSNicholas Bellinger 
1230ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1231ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1232ca40d24eSNicholas Bellinger 		pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1233ca40d24eSNicholas Bellinger 		       " ignoring\n", opcode);
1234ca40d24eSNicholas Bellinger 		return 0;
1235ca40d24eSNicholas Bellinger 	}
1236ca40d24eSNicholas Bellinger 
1237b8d26b3bSNicholas Bellinger 	switch (opcode) {
1238b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1239676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1240b8d26b3bSNicholas Bellinger 		if (!cmd)
1241b8d26b3bSNicholas Bellinger 			break;
1242b8d26b3bSNicholas Bellinger 
1243d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1244b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1245b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1246b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1247b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1248b8d26b3bSNicholas Bellinger 
1249d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1250b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1251b8d26b3bSNicholas Bellinger 		break;
1252b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1253676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1254b8d26b3bSNicholas Bellinger 		if (!cmd)
1255b8d26b3bSNicholas Bellinger 			break;
1256b8d26b3bSNicholas Bellinger 
1257d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1258d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1259778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1260b8d26b3bSNicholas Bellinger 		break;
1261b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1262b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1263b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1264b8d26b3bSNicholas Bellinger 		break;
1265b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1266676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1267b8d26b3bSNicholas Bellinger 		if (!cmd)
1268b8d26b3bSNicholas Bellinger 			break;
1269b8d26b3bSNicholas Bellinger 
1270b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1271b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1272b8d26b3bSNicholas Bellinger 		break;
1273b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1274676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1275b8d26b3bSNicholas Bellinger 		if (!cmd)
1276b8d26b3bSNicholas Bellinger 			break;
1277b8d26b3bSNicholas Bellinger 
1278b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1279b8d26b3bSNicholas Bellinger 		if (ret > 0)
1280b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1281b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1282b8d26b3bSNicholas Bellinger 						    HZ);
1283b8d26b3bSNicholas Bellinger 		break;
1284adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1285676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1286adb54c29SNicholas Bellinger 		if (!cmd)
1287adb54c29SNicholas Bellinger 			break;
1288adb54c29SNicholas Bellinger 
1289d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1290d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1291adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1292adb54c29SNicholas Bellinger 		break;
1293b8d26b3bSNicholas Bellinger 	default:
1294b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1295b8d26b3bSNicholas Bellinger 		dump_stack();
1296b8d26b3bSNicholas Bellinger 		break;
1297b8d26b3bSNicholas Bellinger 	}
1298b8d26b3bSNicholas Bellinger 
1299b8d26b3bSNicholas Bellinger 	return ret;
1300b8d26b3bSNicholas Bellinger }
1301b8d26b3bSNicholas Bellinger 
1302b8d26b3bSNicholas Bellinger static void
1303b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1304b8d26b3bSNicholas Bellinger {
1305b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1306b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1307b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1308b8d26b3bSNicholas Bellinger 	int rc;
1309b8d26b3bSNicholas Bellinger 
1310b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1311b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1312b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1313b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1314b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1315b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1316b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1317b8d26b3bSNicholas Bellinger 		}
1318b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1319b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1320b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1321b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1322b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1323b8d26b3bSNicholas Bellinger 		}
1324b8d26b3bSNicholas Bellinger 
1325b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1326b8d26b3bSNicholas Bellinger 		break;
1327b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1328b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1329b8d26b3bSNicholas Bellinger 		break;
1330b8d26b3bSNicholas Bellinger 	default:
1331b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1332b8d26b3bSNicholas Bellinger 		break;
1333b8d26b3bSNicholas Bellinger 	}
1334b8d26b3bSNicholas Bellinger 
1335b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1336b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1337b8d26b3bSNicholas Bellinger }
1338b8d26b3bSNicholas Bellinger 
1339b8d26b3bSNicholas Bellinger static void
1340b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1341b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1342b8d26b3bSNicholas Bellinger {
1343b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1344b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1345b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1346b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1347b8d26b3bSNicholas Bellinger 
1348b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1349b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1350b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1351b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1352b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1353b8d26b3bSNicholas Bellinger 	} else {
1354b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1355b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1356b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1357b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1358b8d26b3bSNicholas Bellinger 	}
1359b8d26b3bSNicholas Bellinger 
1360b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1361b8d26b3bSNicholas Bellinger 
1362b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1363b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1364b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1365b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1366b8d26b3bSNicholas Bellinger 
1367b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1368b8d26b3bSNicholas Bellinger 		isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1369b8d26b3bSNicholas Bellinger 				   isert_conn);
1370b8d26b3bSNicholas Bellinger 	else
1371b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
1372b8d26b3bSNicholas Bellinger 
1373b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1374b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1375b8d26b3bSNicholas Bellinger 
1376b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1377b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1378b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1379b8d26b3bSNicholas Bellinger 
1380b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1381b8d26b3bSNicholas Bellinger 		return;
1382b8d26b3bSNicholas Bellinger 
1383b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1384b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1385b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1386b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1387b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1388b8d26b3bSNicholas Bellinger 		if (err) {
1389b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1390b8d26b3bSNicholas Bellinger 			       count, err);
1391b8d26b3bSNicholas Bellinger 		}
1392b8d26b3bSNicholas Bellinger 	}
1393b8d26b3bSNicholas Bellinger }
1394b8d26b3bSNicholas Bellinger 
1395b8d26b3bSNicholas Bellinger static void
1396b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1397b8d26b3bSNicholas Bellinger {
1398b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1399b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1400b8d26b3bSNicholas Bellinger 
140190ecc6e2SVu Pham 	pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1402b8d26b3bSNicholas Bellinger 	if (wr->sge) {
140390ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
140490ecc6e2SVu Pham 		ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
140590ecc6e2SVu Pham 				(wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
140690ecc6e2SVu Pham 				DMA_TO_DEVICE : DMA_FROM_DEVICE);
1407b8d26b3bSNicholas Bellinger 		wr->sge = NULL;
1408b8d26b3bSNicholas Bellinger 	}
1409b8d26b3bSNicholas Bellinger 
141090ecc6e2SVu Pham 	if (wr->send_wr) {
141190ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1412b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1413b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
141490ecc6e2SVu Pham 	}
1415b8d26b3bSNicholas Bellinger 
141690ecc6e2SVu Pham 	if (wr->ib_sge) {
141790ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
141890ecc6e2SVu Pham 		kfree(wr->ib_sge);
141990ecc6e2SVu Pham 		wr->ib_sge = NULL;
142090ecc6e2SVu Pham 	}
1421b8d26b3bSNicholas Bellinger }
1422b8d26b3bSNicholas Bellinger 
1423b8d26b3bSNicholas Bellinger static void
1424a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
142559464ef4SVu Pham {
142659464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
142759464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
142859464ef4SVu Pham 	LIST_HEAD(unmap_list);
142959464ef4SVu Pham 
1430a3a5a826SSagi Grimberg 	pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
143159464ef4SVu Pham 
143259464ef4SVu Pham 	if (wr->fr_desc) {
1433a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
143459464ef4SVu Pham 			 isert_cmd, wr->fr_desc);
143559464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1436a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
143759464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
143859464ef4SVu Pham 		wr->fr_desc = NULL;
143959464ef4SVu Pham 	}
144059464ef4SVu Pham 
144159464ef4SVu Pham 	if (wr->sge) {
1442a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
144359464ef4SVu Pham 		ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
144459464ef4SVu Pham 				(wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
144559464ef4SVu Pham 				DMA_TO_DEVICE : DMA_FROM_DEVICE);
144659464ef4SVu Pham 		wr->sge = NULL;
144759464ef4SVu Pham 	}
144859464ef4SVu Pham 
144959464ef4SVu Pham 	wr->ib_sge = NULL;
145059464ef4SVu Pham 	wr->send_wr = NULL;
145159464ef4SVu Pham }
145259464ef4SVu Pham 
145359464ef4SVu Pham static void
1454b8d26b3bSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd)
1455b8d26b3bSNicholas Bellinger {
1456d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1457b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1458186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1459d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1460b8d26b3bSNicholas Bellinger 
1461b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1462b8d26b3bSNicholas Bellinger 
1463b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1464b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1465b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1466b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14675159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1468b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1469b8d26b3bSNicholas Bellinger 
1470b8d26b3bSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE)
1471b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
1472b8d26b3bSNicholas Bellinger 
1473d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1474186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1475186a9647SNicholas Bellinger 		break;
1476b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1477186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1478186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14795159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1480186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1481186a9647SNicholas Bellinger 
1482b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1483b8d26b3bSNicholas Bellinger 		break;
1484b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1485b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1486adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1487b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1488b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
14895159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1490b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1491b8d26b3bSNicholas Bellinger 
1492b8d26b3bSNicholas Bellinger 		/*
1493b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1494b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1495b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1496b8d26b3bSNicholas Bellinger 		 */
1497b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
14983df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
14993df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
15003df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1501b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1502b8d26b3bSNicholas Bellinger 			break;
1503b8d26b3bSNicholas Bellinger 		}
1504b8d26b3bSNicholas Bellinger 		/*
1505b8d26b3bSNicholas Bellinger 		 * Fall-through
1506b8d26b3bSNicholas Bellinger 		 */
1507b8d26b3bSNicholas Bellinger 	default:
1508d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1509b8d26b3bSNicholas Bellinger 		break;
1510b8d26b3bSNicholas Bellinger 	}
1511b8d26b3bSNicholas Bellinger }
1512b8d26b3bSNicholas Bellinger 
1513b8d26b3bSNicholas Bellinger static void
1514b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1515b8d26b3bSNicholas Bellinger {
1516b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1517b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1518b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1519b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1520b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1521b8d26b3bSNicholas Bellinger 	}
1522b8d26b3bSNicholas Bellinger }
1523b8d26b3bSNicholas Bellinger 
1524b8d26b3bSNicholas Bellinger static void
1525b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1526b8d26b3bSNicholas Bellinger 		     struct ib_device *ib_dev)
1527b8d26b3bSNicholas Bellinger {
1528dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1529dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1530dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1531dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1532dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1533b8d26b3bSNicholas Bellinger 	}
1534b8d26b3bSNicholas Bellinger 
1535b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
1536b8d26b3bSNicholas Bellinger 	isert_put_cmd(isert_cmd);
1537b8d26b3bSNicholas Bellinger }
1538b8d26b3bSNicholas Bellinger 
1539b8d26b3bSNicholas Bellinger static void
1540b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1541b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1542b8d26b3bSNicholas Bellinger {
1543b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1544d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1545b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
154690ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1547d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1548b8d26b3bSNicholas Bellinger 
1549b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1550d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
155190ecc6e2SVu Pham 	cmd->write_data_done = wr->cur_rdma_length;
1552b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1553b8d26b3bSNicholas Bellinger 
155490ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1555b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1556b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1557b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1558b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1559b8d26b3bSNicholas Bellinger 
1560b8d26b3bSNicholas Bellinger 	target_execute_cmd(se_cmd);
1561b8d26b3bSNicholas Bellinger }
1562b8d26b3bSNicholas Bellinger 
1563b8d26b3bSNicholas Bellinger static void
1564b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1565b8d26b3bSNicholas Bellinger {
1566b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1567b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1568b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1569b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1570d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1571b8d26b3bSNicholas Bellinger 
1572b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1573b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1574b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1575b8d26b3bSNicholas Bellinger 
1576b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1577b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1578b8d26b3bSNicholas Bellinger 
1579b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1580b8d26b3bSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1581b8d26b3bSNicholas Bellinger 		break;
1582b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1583b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1584b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1585b8d26b3bSNicholas Bellinger 
1586b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1587b8d26b3bSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
15883df8f68aSNicholas Bellinger 		break;
1589b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1590b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1591b8d26b3bSNicholas Bellinger 		/*
1592b8d26b3bSNicholas Bellinger 		 * Call atomic_dec(&isert_conn->post_send_buf_count)
1593defd8848SNicholas Bellinger 		 * from isert_wait_conn()
1594b8d26b3bSNicholas Bellinger 		 */
1595b8d26b3bSNicholas Bellinger 		isert_conn->logout_posted = true;
1596b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1597b8d26b3bSNicholas Bellinger 		break;
1598adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1599adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1600adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1601adb54c29SNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1602adb54c29SNicholas Bellinger 		break;
1603b8d26b3bSNicholas Bellinger 	default:
1604b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1605b8d26b3bSNicholas Bellinger 		dump_stack();
1606b8d26b3bSNicholas Bellinger 		break;
1607b8d26b3bSNicholas Bellinger 	}
1608b8d26b3bSNicholas Bellinger }
1609b8d26b3bSNicholas Bellinger 
1610b8d26b3bSNicholas Bellinger static void
1611b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1612b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1613b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1614b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1615b8d26b3bSNicholas Bellinger {
1616d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1617b6b87a1dSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1618b8d26b3bSNicholas Bellinger 
1619b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
16203df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1621adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1622adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1623b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1624b8d26b3bSNicholas Bellinger 
1625b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1626b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1627b8d26b3bSNicholas Bellinger 		return;
1628b8d26b3bSNicholas Bellinger 	}
1629b6b87a1dSNicholas Bellinger 	atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1630b8d26b3bSNicholas Bellinger 
1631b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
1632b8d26b3bSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev);
1633b8d26b3bSNicholas Bellinger }
1634b8d26b3bSNicholas Bellinger 
1635b8d26b3bSNicholas Bellinger static void
163695b60f07SNicholas Bellinger __isert_send_completion(struct iser_tx_desc *tx_desc,
1637b8d26b3bSNicholas Bellinger 		        struct isert_conn *isert_conn)
1638b8d26b3bSNicholas Bellinger {
1639b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1640b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1641b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1642b8d26b3bSNicholas Bellinger 
1643b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1644b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1645b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1646b8d26b3bSNicholas Bellinger 		return;
1647b8d26b3bSNicholas Bellinger 	}
1648b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1649b8d26b3bSNicholas Bellinger 
1650b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1651b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
1652b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1653b8d26b3bSNicholas Bellinger 		dump_stack();
1654b8d26b3bSNicholas Bellinger 		break;
1655b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1656b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1657b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1658b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1659b8d26b3bSNicholas Bellinger 		break;
1660b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1661b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1662b8d26b3bSNicholas Bellinger 		dump_stack();
1663b8d26b3bSNicholas Bellinger 		break;
1664b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1665b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1666b8d26b3bSNicholas Bellinger 
1667b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1668b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1669b8d26b3bSNicholas Bellinger 		break;
1670b8d26b3bSNicholas Bellinger 	default:
1671b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1672b8d26b3bSNicholas Bellinger 		dump_stack();
1673b8d26b3bSNicholas Bellinger 		break;
1674b8d26b3bSNicholas Bellinger 	}
1675b8d26b3bSNicholas Bellinger }
1676b8d26b3bSNicholas Bellinger 
1677b8d26b3bSNicholas Bellinger static void
167895b60f07SNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
167995b60f07SNicholas Bellinger 		      struct isert_conn *isert_conn)
168095b60f07SNicholas Bellinger {
168195b60f07SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
168295b60f07SNicholas Bellinger 	struct iser_tx_desc *t;
168395b60f07SNicholas Bellinger 	/*
168495b60f07SNicholas Bellinger 	 * Drain coalesced completion llist starting from comp_llnode_batch
168595b60f07SNicholas Bellinger 	 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
168695b60f07SNicholas Bellinger 	 */
168795b60f07SNicholas Bellinger 	while (llnode) {
168895b60f07SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
168995b60f07SNicholas Bellinger 		llnode = llist_next(llnode);
169095b60f07SNicholas Bellinger 		__isert_send_completion(t, isert_conn);
169195b60f07SNicholas Bellinger 	}
169295b60f07SNicholas Bellinger 	__isert_send_completion(tx_desc, isert_conn);
169395b60f07SNicholas Bellinger }
169495b60f07SNicholas Bellinger 
169595b60f07SNicholas Bellinger static void
1696defd8848SNicholas Bellinger isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1697b8d26b3bSNicholas Bellinger {
1698b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1699b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1700b8d26b3bSNicholas Bellinger 
1701b8d26b3bSNicholas Bellinger 	if (!isert_cmd)
1702b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1703b8d26b3bSNicholas Bellinger 	else
1704b8d26b3bSNicholas Bellinger 		isert_completion_put(tx_desc, isert_cmd, ib_dev);
1705b8d26b3bSNicholas Bellinger }
1706b8d26b3bSNicholas Bellinger 
1707defd8848SNicholas Bellinger static void
1708defd8848SNicholas Bellinger isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1709defd8848SNicholas Bellinger {
1710defd8848SNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1711defd8848SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1712defd8848SNicholas Bellinger 
1713defd8848SNicholas Bellinger 	if (isert_conn->post_recv_buf_count)
1714defd8848SNicholas Bellinger 		return;
1715defd8848SNicholas Bellinger 
1716defd8848SNicholas Bellinger 	if (conn->sess) {
1717defd8848SNicholas Bellinger 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1718defd8848SNicholas Bellinger 		target_wait_for_sess_cmds(conn->sess->se_sess);
1719defd8848SNicholas Bellinger 	}
1720defd8848SNicholas Bellinger 
1721defd8848SNicholas Bellinger 	while (atomic_read(&isert_conn->post_send_buf_count))
1722defd8848SNicholas Bellinger 		msleep(3000);
1723b8d26b3bSNicholas Bellinger 
1724b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
1725defd8848SNicholas Bellinger 	isert_conn->state = ISER_CONN_DOWN;
1726b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
1727b2cb9649SNicholas Bellinger 
1728defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait_comp_err);
1729b8d26b3bSNicholas Bellinger }
1730b8d26b3bSNicholas Bellinger 
1731b8d26b3bSNicholas Bellinger static void
1732b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
1733b8d26b3bSNicholas Bellinger {
1734b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
1735b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
1736b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
1737b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
1738b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1739b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
1740b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
1741b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
1742b8d26b3bSNicholas Bellinger 
1743b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1744b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1745b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
1746b8d26b3bSNicholas Bellinger 
1747b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
1748b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
1749b8d26b3bSNicholas Bellinger 		} else {
1750b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1751b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
1752c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
1753defd8848SNicholas Bellinger 
1754*9bb4ca68SNicholas Bellinger 			if (wc.wr_id != ISER_FASTREG_LI_WRID) {
1755b8d26b3bSNicholas Bellinger 				atomic_dec(&isert_conn->post_send_buf_count);
1756defd8848SNicholas Bellinger 				isert_cq_tx_comp_err(tx_desc, isert_conn);
1757b8d26b3bSNicholas Bellinger 			}
1758b8d26b3bSNicholas Bellinger 		}
1759*9bb4ca68SNicholas Bellinger 	}
1760b8d26b3bSNicholas Bellinger 
1761b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1762b8d26b3bSNicholas Bellinger }
1763b8d26b3bSNicholas Bellinger 
1764b8d26b3bSNicholas Bellinger static void
1765b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
1766b8d26b3bSNicholas Bellinger {
1767b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1768b8d26b3bSNicholas Bellinger 
1769b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1770b8d26b3bSNicholas Bellinger }
1771b8d26b3bSNicholas Bellinger 
1772b8d26b3bSNicholas Bellinger static void
1773b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
1774b8d26b3bSNicholas Bellinger {
1775b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
1776b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
1777b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
1778b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
1779b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1780b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
1781b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
1782b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
1783b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
1784b8d26b3bSNicholas Bellinger 
1785b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1786b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1787b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
1788b8d26b3bSNicholas Bellinger 
1789b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
1790b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
1791b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
1792b8d26b3bSNicholas Bellinger 		} else {
1793b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1794c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
1795b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
1796c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
1797c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
1798c5a2adbfSNicholas Bellinger 			}
1799b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
1800defd8848SNicholas Bellinger 			isert_cq_rx_comp_err(isert_conn);
1801b8d26b3bSNicholas Bellinger 		}
1802b8d26b3bSNicholas Bellinger 	}
1803b8d26b3bSNicholas Bellinger 
1804b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1805b8d26b3bSNicholas Bellinger }
1806b8d26b3bSNicholas Bellinger 
1807b8d26b3bSNicholas Bellinger static void
1808b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
1809b8d26b3bSNicholas Bellinger {
1810b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1811b8d26b3bSNicholas Bellinger 
1812b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1813b8d26b3bSNicholas Bellinger }
1814b8d26b3bSNicholas Bellinger 
1815b8d26b3bSNicholas Bellinger static int
1816b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1817b8d26b3bSNicholas Bellinger {
1818b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
1819b8d26b3bSNicholas Bellinger 	int ret;
1820b8d26b3bSNicholas Bellinger 
1821b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
1822b8d26b3bSNicholas Bellinger 
1823b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1824b8d26b3bSNicholas Bellinger 			   &wr_failed);
1825b8d26b3bSNicholas Bellinger 	if (ret) {
1826b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
1827b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1828b8d26b3bSNicholas Bellinger 		return ret;
1829b8d26b3bSNicholas Bellinger 	}
1830b8d26b3bSNicholas Bellinger 	return ret;
1831b8d26b3bSNicholas Bellinger }
1832b8d26b3bSNicholas Bellinger 
1833b8d26b3bSNicholas Bellinger static int
1834b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1835b8d26b3bSNicholas Bellinger {
1836d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1837b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1838b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1839b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1840b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1841b8d26b3bSNicholas Bellinger 
1842b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1843b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1844b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1845b8d26b3bSNicholas Bellinger 	/*
1846b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1847b8d26b3bSNicholas Bellinger 	 */
1848b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1849b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1850b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1851b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1852b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1853dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1854b8d26b3bSNicholas Bellinger 
1855b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1856b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1857b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1858b8d26b3bSNicholas Bellinger 
1859b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1860b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1861dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1862b8d26b3bSNicholas Bellinger 
1863dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1864dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1865b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1866b8d26b3bSNicholas Bellinger 
1867dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1868dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1869dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
1870b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1871b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1872b8d26b3bSNicholas Bellinger 	}
1873b8d26b3bSNicholas Bellinger 
187495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
1875b8d26b3bSNicholas Bellinger 
1876b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1877b8d26b3bSNicholas Bellinger 
1878b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1879b8d26b3bSNicholas Bellinger }
1880b8d26b3bSNicholas Bellinger 
1881b8d26b3bSNicholas Bellinger static int
1882b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1883b8d26b3bSNicholas Bellinger 		bool nopout_response)
1884b8d26b3bSNicholas Bellinger {
1885d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1886b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1887b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1888b8d26b3bSNicholas Bellinger 
1889b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1890b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1891b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1892b8d26b3bSNicholas Bellinger 			       nopout_response);
1893b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
189495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1895b8d26b3bSNicholas Bellinger 
18968b513d0cSMasanari Iida 	pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1897b8d26b3bSNicholas Bellinger 
1898b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1899b8d26b3bSNicholas Bellinger }
1900b8d26b3bSNicholas Bellinger 
1901b8d26b3bSNicholas Bellinger static int
1902b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1903b8d26b3bSNicholas Bellinger {
1904d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1905b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1906b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1907b8d26b3bSNicholas Bellinger 
1908b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1909b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1910b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1911b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
191295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1913b8d26b3bSNicholas Bellinger 
1914b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1915b8d26b3bSNicholas Bellinger 
1916b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1917b8d26b3bSNicholas Bellinger }
1918b8d26b3bSNicholas Bellinger 
1919b8d26b3bSNicholas Bellinger static int
1920b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1921b8d26b3bSNicholas Bellinger {
1922d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1923b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1924b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1925b8d26b3bSNicholas Bellinger 
1926b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1927b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1928b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1929b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
193095b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1931b8d26b3bSNicholas Bellinger 
1932b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1933b8d26b3bSNicholas Bellinger 
1934b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1935b8d26b3bSNicholas Bellinger }
1936b8d26b3bSNicholas Bellinger 
1937b8d26b3bSNicholas Bellinger static int
1938b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1939b8d26b3bSNicholas Bellinger {
1940d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1941b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1942b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
19433df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
19443df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
19453df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
19463df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1947b8d26b3bSNicholas Bellinger 
1948b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
19493df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
1950b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
19513df8f68aSNicholas Bellinger 
19523df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
1953dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
19543df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
19553df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
1956dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1957dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
19583df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
19593df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
19603df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
19613df8f68aSNicholas Bellinger 
196295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1963b8d26b3bSNicholas Bellinger 
1964b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1965b8d26b3bSNicholas Bellinger 
1966b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1967b8d26b3bSNicholas Bellinger }
1968b8d26b3bSNicholas Bellinger 
1969b8d26b3bSNicholas Bellinger static int
1970adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1971adb54c29SNicholas Bellinger {
1972d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1973adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1974adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1975adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
1976adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1977adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
1978adb54c29SNicholas Bellinger 	int rc;
1979adb54c29SNicholas Bellinger 
1980adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1981adb54c29SNicholas Bellinger 	rc = iscsit_build_text_rsp(cmd, conn, hdr);
1982adb54c29SNicholas Bellinger 	if (rc < 0)
1983adb54c29SNicholas Bellinger 		return rc;
1984adb54c29SNicholas Bellinger 
1985adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
1986adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1987adb54c29SNicholas Bellinger 
1988adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
1989adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1990adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1991adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
1992adb54c29SNicholas Bellinger 
1993adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1994adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1995adb54c29SNicholas Bellinger 
1996adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
1997adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1998adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
1999adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2000adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2001adb54c29SNicholas Bellinger 	}
200295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2003adb54c29SNicholas Bellinger 
2004adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2005adb54c29SNicholas Bellinger 
2006adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2007adb54c29SNicholas Bellinger }
2008adb54c29SNicholas Bellinger 
2009adb54c29SNicholas Bellinger static int
2010b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2011b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2012b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2013b8d26b3bSNicholas Bellinger {
2014d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2015b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2016b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2017b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2018b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2019b8d26b3bSNicholas Bellinger 
2020b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2021b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2022b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2023b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2024b8d26b3bSNicholas Bellinger 
2025b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2026b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2027b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2028b8d26b3bSNicholas Bellinger 	/*
2029b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2030b8d26b3bSNicholas Bellinger 	 */
2031b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2032b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2033b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
2034b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
2035b8d26b3bSNicholas Bellinger 
2036b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2037b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2038b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2039b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2040b8d26b3bSNicholas Bellinger 
204190ecc6e2SVu Pham 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
204290ecc6e2SVu Pham 			 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2043b8d26b3bSNicholas Bellinger 		page_off = 0;
2044b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2045b8d26b3bSNicholas Bellinger 		ib_sge++;
2046b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2047b8d26b3bSNicholas Bellinger 	}
2048b8d26b3bSNicholas Bellinger 
2049b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2050b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
2051b8d26b3bSNicholas Bellinger 
2052b8d26b3bSNicholas Bellinger 	return sg_nents;
2053b8d26b3bSNicholas Bellinger }
2054b8d26b3bSNicholas Bellinger 
2055b8d26b3bSNicholas Bellinger static int
205690ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
205790ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2058b8d26b3bSNicholas Bellinger {
2059b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2060d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2061b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2062b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
206390ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2064b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
206590ecc6e2SVu Pham 	struct scatterlist *sg_start;
206690ecc6e2SVu Pham 	u32 sg_off = 0, sg_nents;
206790ecc6e2SVu Pham 	u32 offset = 0, data_len, data_left, rdma_write_max, va_offset = 0;
206890ecc6e2SVu Pham 	int ret = 0, count, i, ib_sge_cnt;
2069b8d26b3bSNicholas Bellinger 
207090ecc6e2SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
207190ecc6e2SVu Pham 		data_left = se_cmd->data_length;
207290ecc6e2SVu Pham 	} else {
207390ecc6e2SVu Pham 		sg_off = cmd->write_data_done / PAGE_SIZE;
207490ecc6e2SVu Pham 		data_left = se_cmd->data_length - cmd->write_data_done;
207590ecc6e2SVu Pham 		offset = cmd->write_data_done;
207690ecc6e2SVu Pham 		isert_cmd->tx_desc.isert_cmd = isert_cmd;
207790ecc6e2SVu Pham 	}
2078b8d26b3bSNicholas Bellinger 
207990ecc6e2SVu Pham 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
208090ecc6e2SVu Pham 	sg_nents = se_cmd->t_data_nents - sg_off;
2081b8d26b3bSNicholas Bellinger 
208290ecc6e2SVu Pham 	count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
208390ecc6e2SVu Pham 			      (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
208490ecc6e2SVu Pham 			      DMA_TO_DEVICE : DMA_FROM_DEVICE);
2085b8d26b3bSNicholas Bellinger 	if (unlikely(!count)) {
208690ecc6e2SVu Pham 		pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
2087b8d26b3bSNicholas Bellinger 		return -EINVAL;
2088b8d26b3bSNicholas Bellinger 	}
208990ecc6e2SVu Pham 	wr->sge = sg_start;
2090b8d26b3bSNicholas Bellinger 	wr->num_sge = sg_nents;
209190ecc6e2SVu Pham 	wr->cur_rdma_length = data_left;
209290ecc6e2SVu Pham 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
209390ecc6e2SVu Pham 		 isert_cmd, count, sg_start, sg_nents, data_left);
2094b8d26b3bSNicholas Bellinger 
2095b8d26b3bSNicholas Bellinger 	ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
2096b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
209790ecc6e2SVu Pham 		pr_warn("Unable to allocate ib_sge\n");
2098b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2099b8d26b3bSNicholas Bellinger 		goto unmap_sg;
2100b8d26b3bSNicholas Bellinger 	}
210190ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2102b8d26b3bSNicholas Bellinger 
2103b8d26b3bSNicholas Bellinger 	wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
2104b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2105b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2106b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
210790ecc6e2SVu Pham 		pr_debug("Unable to allocate wr->send_wr\n");
2108b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2109b8d26b3bSNicholas Bellinger 		goto unmap_sg;
2110b8d26b3bSNicholas Bellinger 	}
2111b8d26b3bSNicholas Bellinger 
2112b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2113b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2114b8d26b3bSNicholas Bellinger 
2115b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2116b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2117b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2118b8d26b3bSNicholas Bellinger 
2119b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
212090ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
212190ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2122b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2123b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
212490ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
212590ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
212690ecc6e2SVu Pham 			else
212790ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
212890ecc6e2SVu Pham 		} else {
212990ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
213090ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
213190ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
213290ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
213390ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
213490ecc6e2SVu Pham 			else
213590ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
213690ecc6e2SVu Pham 		}
2137b8d26b3bSNicholas Bellinger 
2138b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2139b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2140b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2141b8d26b3bSNicholas Bellinger 
2142b8d26b3bSNicholas Bellinger 		offset += data_len;
214390ecc6e2SVu Pham 		va_offset += data_len;
2144b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2145b8d26b3bSNicholas Bellinger 	}
214690ecc6e2SVu Pham 
214790ecc6e2SVu Pham 	return 0;
214890ecc6e2SVu Pham unmap_sg:
214990ecc6e2SVu Pham 	ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
215090ecc6e2SVu Pham 			(wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
215190ecc6e2SVu Pham 			DMA_TO_DEVICE : DMA_FROM_DEVICE);
215290ecc6e2SVu Pham 	return ret;
215390ecc6e2SVu Pham }
215490ecc6e2SVu Pham 
215590ecc6e2SVu Pham static int
215659464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
215759464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
215859464ef4SVu Pham {
215959464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
216059464ef4SVu Pham 	struct scatterlist *tmp_sg;
216159464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
216259464ef4SVu Pham 
216359464ef4SVu Pham 	n_pages = 0;
216459464ef4SVu Pham 	new_chunk = 1;
216559464ef4SVu Pham 	last_ent = sg_nents - 1;
216659464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
216759464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
216859464ef4SVu Pham 		if (new_chunk)
216959464ef4SVu Pham 			chunk_start = start_addr;
217059464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
217159464ef4SVu Pham 
217259464ef4SVu Pham 		pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
217359464ef4SVu Pham 			 i, (unsigned long long)tmp_sg->dma_address,
217459464ef4SVu Pham 			 tmp_sg->length);
217559464ef4SVu Pham 
217659464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
217759464ef4SVu Pham 			new_chunk = 0;
217859464ef4SVu Pham 			continue;
217959464ef4SVu Pham 		}
218059464ef4SVu Pham 		new_chunk = 1;
218159464ef4SVu Pham 
218259464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
218359464ef4SVu Pham 		do {
218459464ef4SVu Pham 			fr_pl[n_pages++] = page;
218559464ef4SVu Pham 			pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
218659464ef4SVu Pham 				 n_pages - 1, page);
218759464ef4SVu Pham 			page += PAGE_SIZE;
218859464ef4SVu Pham 		} while (page < end_addr);
218959464ef4SVu Pham 	}
219059464ef4SVu Pham 
219159464ef4SVu Pham 	return n_pages;
219259464ef4SVu Pham }
219359464ef4SVu Pham 
219459464ef4SVu Pham static int
219559464ef4SVu Pham isert_fast_reg_mr(struct fast_reg_descriptor *fr_desc,
21969bd626e7SSagi Grimberg 		  struct isert_conn *isert_conn, struct scatterlist *sg_start,
21979bd626e7SSagi Grimberg 		  struct ib_sge *ib_sge, u32 sg_nents, u32 offset,
21989bd626e7SSagi Grimberg 		  unsigned int data_len)
219959464ef4SVu Pham {
220059464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
220159464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
220259464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
22039bd626e7SSagi Grimberg 	int ret, pagelist_len;
22049bd626e7SSagi Grimberg 	u32 page_off;
220559464ef4SVu Pham 	u8 key;
220659464ef4SVu Pham 
22079bd626e7SSagi Grimberg 	sg_nents = min_t(unsigned int, sg_nents, ISCSI_ISER_SG_TABLESIZE);
220859464ef4SVu Pham 	page_off = offset % PAGE_SIZE;
220959464ef4SVu Pham 
22109bd626e7SSagi Grimberg 	pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
22119bd626e7SSagi Grimberg 		 fr_desc, sg_nents, offset);
221259464ef4SVu Pham 
221359464ef4SVu Pham 	pagelist_len = isert_map_fr_pagelist(ib_dev, sg_start, sg_nents,
221459464ef4SVu Pham 					     &fr_desc->data_frpl->page_list[0]);
221559464ef4SVu Pham 
221659464ef4SVu Pham 	if (!fr_desc->valid) {
221759464ef4SVu Pham 		memset(&inv_wr, 0, sizeof(inv_wr));
2218*9bb4ca68SNicholas Bellinger 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
221959464ef4SVu Pham 		inv_wr.opcode = IB_WR_LOCAL_INV;
222059464ef4SVu Pham 		inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
222159464ef4SVu Pham 		wr = &inv_wr;
222259464ef4SVu Pham 		/* Bump the key */
222359464ef4SVu Pham 		key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
222459464ef4SVu Pham 		ib_update_fast_reg_key(fr_desc->data_mr, ++key);
222559464ef4SVu Pham 	}
222659464ef4SVu Pham 
222759464ef4SVu Pham 	/* Prepare FASTREG WR */
222859464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
2229*9bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
223059464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
223159464ef4SVu Pham 	fr_wr.wr.fast_reg.iova_start =
223259464ef4SVu Pham 		fr_desc->data_frpl->page_list[0] + page_off;
223359464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
223459464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
223559464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
223659464ef4SVu Pham 	fr_wr.wr.fast_reg.length = data_len;
223759464ef4SVu Pham 	fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
223859464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
223959464ef4SVu Pham 
224059464ef4SVu Pham 	if (!wr)
224159464ef4SVu Pham 		wr = &fr_wr;
224259464ef4SVu Pham 	else
224359464ef4SVu Pham 		wr->next = &fr_wr;
224459464ef4SVu Pham 
224559464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
224659464ef4SVu Pham 	if (ret) {
224759464ef4SVu Pham 		pr_err("fast registration failed, ret:%d\n", ret);
224859464ef4SVu Pham 		return ret;
224959464ef4SVu Pham 	}
225059464ef4SVu Pham 	fr_desc->valid = false;
225159464ef4SVu Pham 
225259464ef4SVu Pham 	ib_sge->lkey = fr_desc->data_mr->lkey;
225359464ef4SVu Pham 	ib_sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
225459464ef4SVu Pham 	ib_sge->length = data_len;
225559464ef4SVu Pham 
225659464ef4SVu Pham 	pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
225759464ef4SVu Pham 		 ib_sge->addr, ib_sge->length, ib_sge->lkey);
225859464ef4SVu Pham 
225959464ef4SVu Pham 	return ret;
226059464ef4SVu Pham }
226159464ef4SVu Pham 
226259464ef4SVu Pham static int
2263a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
226459464ef4SVu Pham 	       struct isert_rdma_wr *wr)
226559464ef4SVu Pham {
226659464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
226759464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
226859464ef4SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
226959464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
227059464ef4SVu Pham 	struct ib_send_wr *send_wr;
227159464ef4SVu Pham 	struct ib_sge *ib_sge;
227259464ef4SVu Pham 	struct scatterlist *sg_start;
227359464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
227459464ef4SVu Pham 	u32 sg_off = 0, sg_nents;
227559464ef4SVu Pham 	u32 offset = 0, data_len, data_left, rdma_write_max;
227659464ef4SVu Pham 	int ret = 0, count;
227759464ef4SVu Pham 	unsigned long flags;
227859464ef4SVu Pham 
227959464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
228059464ef4SVu Pham 		data_left = se_cmd->data_length;
228159464ef4SVu Pham 	} else {
228259464ef4SVu Pham 		offset = cmd->write_data_done;
22839bd626e7SSagi Grimberg 		sg_off = offset / PAGE_SIZE;
22849bd626e7SSagi Grimberg 		data_left = se_cmd->data_length - cmd->write_data_done;
228559464ef4SVu Pham 		isert_cmd->tx_desc.isert_cmd = isert_cmd;
228659464ef4SVu Pham 	}
228759464ef4SVu Pham 
228859464ef4SVu Pham 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
228959464ef4SVu Pham 	sg_nents = se_cmd->t_data_nents - sg_off;
229059464ef4SVu Pham 
229159464ef4SVu Pham 	count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
229259464ef4SVu Pham 			      (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
229359464ef4SVu Pham 			      DMA_TO_DEVICE : DMA_FROM_DEVICE);
229459464ef4SVu Pham 	if (unlikely(!count)) {
229559464ef4SVu Pham 		pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
229659464ef4SVu Pham 		return -EINVAL;
229759464ef4SVu Pham 	}
229859464ef4SVu Pham 	wr->sge = sg_start;
229959464ef4SVu Pham 	wr->num_sge = sg_nents;
230059464ef4SVu Pham 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
230159464ef4SVu Pham 		 isert_cmd, count, sg_start, sg_nents, data_left);
230259464ef4SVu Pham 
230359464ef4SVu Pham 	memset(&wr->s_ib_sge, 0, sizeof(*ib_sge));
230459464ef4SVu Pham 	ib_sge = &wr->s_ib_sge;
230559464ef4SVu Pham 	wr->ib_sge = ib_sge;
230659464ef4SVu Pham 
230759464ef4SVu Pham 	wr->send_wr_num = 1;
230859464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
230959464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
231059464ef4SVu Pham 
231159464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
231259464ef4SVu Pham 	rdma_write_max = ISCSI_ISER_SG_TABLESIZE * PAGE_SIZE;
231359464ef4SVu Pham 
231459464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
231559464ef4SVu Pham 	send_wr->sg_list = ib_sge;
231659464ef4SVu Pham 	send_wr->num_sge = 1;
231759464ef4SVu Pham 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
231859464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
231959464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
232059464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
232159464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
232259464ef4SVu Pham 		send_wr->send_flags = 0;
232359464ef4SVu Pham 		send_wr->next = &isert_cmd->tx_desc.send_wr;
232459464ef4SVu Pham 	} else {
232559464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
232659464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
232759464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
232859464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
232959464ef4SVu Pham 	}
233059464ef4SVu Pham 
233159464ef4SVu Pham 	data_len = min(data_left, rdma_write_max);
233259464ef4SVu Pham 	wr->cur_rdma_length = data_len;
233359464ef4SVu Pham 
2334f01b9f73SVu Pham 	/* if there is a single dma entry, dma mr is sufficient */
2335f01b9f73SVu Pham 	if (count == 1) {
2336f01b9f73SVu Pham 		ib_sge->addr = ib_sg_dma_address(ib_dev, &sg_start[0]);
2337f01b9f73SVu Pham 		ib_sge->length = ib_sg_dma_len(ib_dev, &sg_start[0]);
2338f01b9f73SVu Pham 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2339f01b9f73SVu Pham 		wr->fr_desc = NULL;
2340f01b9f73SVu Pham 	} else {
234159464ef4SVu Pham 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2342a3a5a826SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
234359464ef4SVu Pham 					   struct fast_reg_descriptor, list);
234459464ef4SVu Pham 		list_del(&fr_desc->list);
234559464ef4SVu Pham 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
234659464ef4SVu Pham 		wr->fr_desc = fr_desc;
234759464ef4SVu Pham 
23489bd626e7SSagi Grimberg 		ret = isert_fast_reg_mr(fr_desc, isert_conn, sg_start,
23499bd626e7SSagi Grimberg 					ib_sge, sg_nents, offset, data_len);
235059464ef4SVu Pham 		if (ret) {
2351a3a5a826SSagi Grimberg 			list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
235259464ef4SVu Pham 			goto unmap_sg;
235359464ef4SVu Pham 		}
2354f01b9f73SVu Pham 	}
235559464ef4SVu Pham 
235659464ef4SVu Pham 	return 0;
235759464ef4SVu Pham 
235859464ef4SVu Pham unmap_sg:
235959464ef4SVu Pham 	ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
236059464ef4SVu Pham 			(wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
236159464ef4SVu Pham 			DMA_TO_DEVICE : DMA_FROM_DEVICE);
236259464ef4SVu Pham 	return ret;
236359464ef4SVu Pham }
236459464ef4SVu Pham 
236559464ef4SVu Pham static int
236690ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
236790ecc6e2SVu Pham {
236890ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
236959464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
237090ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
237190ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2372d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
237390ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
237490ecc6e2SVu Pham 	int rc;
237590ecc6e2SVu Pham 
237690ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
237790ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
237890ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2379d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
238090ecc6e2SVu Pham 	if (rc) {
238190ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
238290ecc6e2SVu Pham 		return rc;
238390ecc6e2SVu Pham 	}
238490ecc6e2SVu Pham 
2385b8d26b3bSNicholas Bellinger 	/*
2386b8d26b3bSNicholas Bellinger 	 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2387b8d26b3bSNicholas Bellinger 	 */
2388b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
238904d9cd12SNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2390b8d26b3bSNicholas Bellinger 			     &isert_cmd->tx_desc.iscsi_header);
2391b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
239295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd,
239395b60f07SNicholas Bellinger 			   &isert_cmd->tx_desc.send_wr, true);
2394b8d26b3bSNicholas Bellinger 
2395b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
2396b8d26b3bSNicholas Bellinger 
2397b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2398b8d26b3bSNicholas Bellinger 	if (rc) {
2399b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2400b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
2401b8d26b3bSNicholas Bellinger 	}
240290ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
240390ecc6e2SVu Pham 		 isert_cmd);
2404b8d26b3bSNicholas Bellinger 
240590ecc6e2SVu Pham 	return 1;
2406b8d26b3bSNicholas Bellinger }
2407b8d26b3bSNicholas Bellinger 
2408b8d26b3bSNicholas Bellinger static int
2409b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2410b8d26b3bSNicholas Bellinger {
2411b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2412d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2413b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2414b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2415d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
241690ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
241790ecc6e2SVu Pham 	int rc;
2418b8d26b3bSNicholas Bellinger 
241990ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
242090ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2421b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2422d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
242390ecc6e2SVu Pham 	if (rc) {
242490ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
242590ecc6e2SVu Pham 		return rc;
2426b8d26b3bSNicholas Bellinger 	}
2427b8d26b3bSNicholas Bellinger 
2428b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2429b8d26b3bSNicholas Bellinger 
2430b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2431b8d26b3bSNicholas Bellinger 	if (rc) {
2432b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2433b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2434b8d26b3bSNicholas Bellinger 	}
243590ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
243690ecc6e2SVu Pham 		 isert_cmd);
2437b8d26b3bSNicholas Bellinger 
243890ecc6e2SVu Pham 	return 0;
2439b8d26b3bSNicholas Bellinger }
2440b8d26b3bSNicholas Bellinger 
2441b8d26b3bSNicholas Bellinger static int
2442b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2443b8d26b3bSNicholas Bellinger {
2444b8d26b3bSNicholas Bellinger 	int ret;
2445b8d26b3bSNicholas Bellinger 
2446b8d26b3bSNicholas Bellinger 	switch (state) {
2447b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2448b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2449b8d26b3bSNicholas Bellinger 		break;
2450b8d26b3bSNicholas Bellinger 	default:
2451b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
2452b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2453b8d26b3bSNicholas Bellinger 		break;
2454b8d26b3bSNicholas Bellinger 	}
2455b8d26b3bSNicholas Bellinger 
2456b8d26b3bSNicholas Bellinger 	return ret;
2457b8d26b3bSNicholas Bellinger }
2458b8d26b3bSNicholas Bellinger 
2459b8d26b3bSNicholas Bellinger static int
2460b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2461b8d26b3bSNicholas Bellinger {
2462b8d26b3bSNicholas Bellinger 	int ret;
2463b8d26b3bSNicholas Bellinger 
2464b8d26b3bSNicholas Bellinger 	switch (state) {
2465b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2466b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2467b8d26b3bSNicholas Bellinger 		if (!ret) {
2468b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
2469b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
2470b8d26b3bSNicholas Bellinger 		}
2471b8d26b3bSNicholas Bellinger 		break;
2472b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2473b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2474b8d26b3bSNicholas Bellinger 		break;
2475b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2476b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2477b8d26b3bSNicholas Bellinger 		break;
2478b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2479b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2480b8d26b3bSNicholas Bellinger 		break;
2481adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2482adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2483adb54c29SNicholas Bellinger 		break;
2484b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2485b8d26b3bSNicholas Bellinger 		/*
2486b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2487b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2488b8d26b3bSNicholas Bellinger 		 */
2489b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2490b8d26b3bSNicholas Bellinger 		break;
2491b8d26b3bSNicholas Bellinger 	default:
2492b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
2493b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2494b8d26b3bSNicholas Bellinger 		break;
2495b8d26b3bSNicholas Bellinger 	}
2496b8d26b3bSNicholas Bellinger 
2497b8d26b3bSNicholas Bellinger 	return ret;
2498b8d26b3bSNicholas Bellinger }
2499b8d26b3bSNicholas Bellinger 
2500b8d26b3bSNicholas Bellinger static int
2501b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
2502b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
2503b8d26b3bSNicholas Bellinger {
2504b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2505b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2506b8d26b3bSNicholas Bellinger 	struct sockaddr *sa;
2507b8d26b3bSNicholas Bellinger 	int ret;
2508b8d26b3bSNicholas Bellinger 
2509b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2510b8d26b3bSNicholas Bellinger 	if (!isert_np) {
2511b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
2512b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2513b8d26b3bSNicholas Bellinger 	}
2514b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_np->np_accept_wq);
2515b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
2516b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
2517b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
2518b8d26b3bSNicholas Bellinger 
2519b8d26b3bSNicholas Bellinger 	sa = (struct sockaddr *)ksockaddr;
2520b8d26b3bSNicholas Bellinger 	pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2521b8d26b3bSNicholas Bellinger 	/*
2522b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2523b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2524b8d26b3bSNicholas Bellinger 	 */
2525b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
2526b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
2527b8d26b3bSNicholas Bellinger 
2528b8d26b3bSNicholas Bellinger 	isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2529b8d26b3bSNicholas Bellinger 				IB_QPT_RC);
2530b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2531b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2532b8d26b3bSNicholas Bellinger 		       PTR_ERR(isert_lid));
2533b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2534b8d26b3bSNicholas Bellinger 		goto out;
2535b8d26b3bSNicholas Bellinger 	}
2536b8d26b3bSNicholas Bellinger 
2537b8d26b3bSNicholas Bellinger 	ret = rdma_bind_addr(isert_lid, sa);
2538b8d26b3bSNicholas Bellinger 	if (ret) {
2539b8d26b3bSNicholas Bellinger 		pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2540b8d26b3bSNicholas Bellinger 		goto out_lid;
2541b8d26b3bSNicholas Bellinger 	}
2542b8d26b3bSNicholas Bellinger 
2543b8d26b3bSNicholas Bellinger 	ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2544b8d26b3bSNicholas Bellinger 	if (ret) {
2545b8d26b3bSNicholas Bellinger 		pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2546b8d26b3bSNicholas Bellinger 		goto out_lid;
2547b8d26b3bSNicholas Bellinger 	}
2548b8d26b3bSNicholas Bellinger 
2549b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
2550b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2551b8d26b3bSNicholas Bellinger 	pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2552b8d26b3bSNicholas Bellinger 
2553b8d26b3bSNicholas Bellinger 	return 0;
2554b8d26b3bSNicholas Bellinger 
2555b8d26b3bSNicholas Bellinger out_lid:
2556b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_lid);
2557b8d26b3bSNicholas Bellinger out:
2558b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2559b8d26b3bSNicholas Bellinger 	return ret;
2560b8d26b3bSNicholas Bellinger }
2561b8d26b3bSNicholas Bellinger 
2562b8d26b3bSNicholas Bellinger static int
2563b8d26b3bSNicholas Bellinger isert_check_accept_queue(struct isert_np *isert_np)
2564b8d26b3bSNicholas Bellinger {
2565b8d26b3bSNicholas Bellinger 	int empty;
2566b8d26b3bSNicholas Bellinger 
2567b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
2568b8d26b3bSNicholas Bellinger 	empty = list_empty(&isert_np->np_accept_list);
2569b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
2570b8d26b3bSNicholas Bellinger 
2571b8d26b3bSNicholas Bellinger 	return empty;
2572b8d26b3bSNicholas Bellinger }
2573b8d26b3bSNicholas Bellinger 
2574b8d26b3bSNicholas Bellinger static int
2575b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2576b8d26b3bSNicholas Bellinger {
2577b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2578b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2579b8d26b3bSNicholas Bellinger 	int ret;
2580b8d26b3bSNicholas Bellinger 
2581b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2582b8d26b3bSNicholas Bellinger 	cp.responder_resources = isert_conn->responder_resources;
2583b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2584b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2585b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2586b8d26b3bSNicholas Bellinger 
2587b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2588b8d26b3bSNicholas Bellinger 
2589b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2590b8d26b3bSNicholas Bellinger 	if (ret) {
2591b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
2592b8d26b3bSNicholas Bellinger 		return ret;
2593b8d26b3bSNicholas Bellinger 	}
2594b8d26b3bSNicholas Bellinger 
2595b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2596b8d26b3bSNicholas Bellinger 
2597b8d26b3bSNicholas Bellinger 	return 0;
2598b8d26b3bSNicholas Bellinger }
2599b8d26b3bSNicholas Bellinger 
2600b8d26b3bSNicholas Bellinger static int
2601b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2602b8d26b3bSNicholas Bellinger {
2603b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2604b8d26b3bSNicholas Bellinger 	int ret;
2605b8d26b3bSNicholas Bellinger 
2606b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
26076faaa85fSNicholas Bellinger 	/*
26086faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
26096faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
26106faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
26116faaa85fSNicholas Bellinger 	 * into a NOP.
26126faaa85fSNicholas Bellinger 	 */
26136faaa85fSNicholas Bellinger 	if (!login->first_request)
26146faaa85fSNicholas Bellinger 		return 0;
2615b8d26b3bSNicholas Bellinger 
2616b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2617b8d26b3bSNicholas Bellinger 	if (ret)
2618b8d26b3bSNicholas Bellinger 		return ret;
2619b8d26b3bSNicholas Bellinger 
2620b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2621b8d26b3bSNicholas Bellinger 	return 0;
2622b8d26b3bSNicholas Bellinger }
2623b8d26b3bSNicholas Bellinger 
2624b8d26b3bSNicholas Bellinger static void
2625b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2626b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2627b8d26b3bSNicholas Bellinger {
2628b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2629b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2630b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
2631b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
2632b8d26b3bSNicholas Bellinger 
2633b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2634b8d26b3bSNicholas Bellinger 
2635b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
2636b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2637b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2638b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
2639b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
2640b8d26b3bSNicholas Bellinger 
2641b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2642b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2643b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
2644b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
2645b8d26b3bSNicholas Bellinger 	} else {
2646b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2647b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
2648b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
2649b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
2650b8d26b3bSNicholas Bellinger 
2651b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2652b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
2653b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
2654b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
2655b8d26b3bSNicholas Bellinger 	}
2656b8d26b3bSNicholas Bellinger }
2657b8d26b3bSNicholas Bellinger 
2658b8d26b3bSNicholas Bellinger static int
2659b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2660b8d26b3bSNicholas Bellinger {
2661b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
2662b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2663b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
2664b8d26b3bSNicholas Bellinger 
2665b8d26b3bSNicholas Bellinger accept_wait:
2666b8d26b3bSNicholas Bellinger 	ret = wait_event_interruptible(isert_np->np_accept_wq,
2667b8d26b3bSNicholas Bellinger 			!isert_check_accept_queue(isert_np) ||
2668b8d26b3bSNicholas Bellinger 			np->np_thread_state == ISCSI_NP_THREAD_RESET);
2669b8d26b3bSNicholas Bellinger 	if (max_accept > 5)
2670b8d26b3bSNicholas Bellinger 		return -ENODEV;
2671b8d26b3bSNicholas Bellinger 
2672b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2673b8d26b3bSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2674b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
2675b8d26b3bSNicholas Bellinger 		pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2676b8d26b3bSNicholas Bellinger 		return -ENODEV;
2677b8d26b3bSNicholas Bellinger 	}
2678b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2679b8d26b3bSNicholas Bellinger 
2680b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
2681b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
2682b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
2683b8d26b3bSNicholas Bellinger 		max_accept++;
2684b8d26b3bSNicholas Bellinger 		goto accept_wait;
2685b8d26b3bSNicholas Bellinger 	}
2686b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
2687b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
2688b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
2689b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
2690b8d26b3bSNicholas Bellinger 
2691b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2692b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2693b8d26b3bSNicholas Bellinger 	max_accept = 0;
2694b8d26b3bSNicholas Bellinger 
2695b8d26b3bSNicholas Bellinger 	ret = isert_rdma_post_recvl(isert_conn);
2696b8d26b3bSNicholas Bellinger 	if (ret)
2697b8d26b3bSNicholas Bellinger 		return ret;
2698b8d26b3bSNicholas Bellinger 
2699b8d26b3bSNicholas Bellinger 	ret = isert_rdma_accept(isert_conn);
2700b8d26b3bSNicholas Bellinger 	if (ret)
2701b8d26b3bSNicholas Bellinger 		return ret;
2702b8d26b3bSNicholas Bellinger 
2703b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2704b8d26b3bSNicholas Bellinger 
2705b8d26b3bSNicholas Bellinger 	pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2706b8d26b3bSNicholas Bellinger 	return 0;
2707b8d26b3bSNicholas Bellinger }
2708b8d26b3bSNicholas Bellinger 
2709b8d26b3bSNicholas Bellinger static void
2710b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2711b8d26b3bSNicholas Bellinger {
2712b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
2713b8d26b3bSNicholas Bellinger 
2714b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_np->np_cm_id);
2715b8d26b3bSNicholas Bellinger 
2716b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2717b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2718b8d26b3bSNicholas Bellinger }
2719b8d26b3bSNicholas Bellinger 
2720defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
2721b8d26b3bSNicholas Bellinger {
2722b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2723b8d26b3bSNicholas Bellinger 
2724defd8848SNicholas Bellinger 	pr_debug("isert_wait_conn: Starting \n");
2725b8d26b3bSNicholas Bellinger 	/*
2726b8d26b3bSNicholas Bellinger 	 * Decrement post_send_buf_count for special case when called
2727b8d26b3bSNicholas Bellinger 	 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2728b8d26b3bSNicholas Bellinger 	 */
2729b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2730b8d26b3bSNicholas Bellinger 	if (isert_conn->logout_posted)
2731b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2732b8d26b3bSNicholas Bellinger 
2733b2cb9649SNicholas Bellinger 	if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
2734defd8848SNicholas Bellinger 		pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
2735b8d26b3bSNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
2736b2cb9649SNicholas Bellinger 	}
2737b8d26b3bSNicholas Bellinger 	/*
2738b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
2739b8d26b3bSNicholas Bellinger 	 * into full feature phase..
2740b8d26b3bSNicholas Bellinger 	 */
2741b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
2742b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
2743b2cb9649SNicholas Bellinger 		return;
2744b2cb9649SNicholas Bellinger 	}
2745defd8848SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP)
2746defd8848SNicholas Bellinger 		isert_conn->state = ISER_CONN_TERMINATING;
2747b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2748b2cb9649SNicholas Bellinger 
2749defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait_comp_err);
2750defd8848SNicholas Bellinger 
2751defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait);
2752defd8848SNicholas Bellinger }
2753defd8848SNicholas Bellinger 
2754defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2755defd8848SNicholas Bellinger {
2756defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2757b8d26b3bSNicholas Bellinger 
2758b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2759b8d26b3bSNicholas Bellinger }
2760b8d26b3bSNicholas Bellinger 
2761b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2762b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2763b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2764d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2765b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2766b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2767b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2768b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2769defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
2770b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2771b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2772b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2773b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2774b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2775b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2776b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2777b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2778b8d26b3bSNicholas Bellinger };
2779b8d26b3bSNicholas Bellinger 
2780b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2781b8d26b3bSNicholas Bellinger {
2782b8d26b3bSNicholas Bellinger 	int ret;
2783b8d26b3bSNicholas Bellinger 
2784b8d26b3bSNicholas Bellinger 	isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2785b8d26b3bSNicholas Bellinger 	if (!isert_rx_wq) {
2786b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_rx_wq\n");
2787b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2788b8d26b3bSNicholas Bellinger 	}
2789b8d26b3bSNicholas Bellinger 
2790b8d26b3bSNicholas Bellinger 	isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2791b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
2792b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_comp_wq\n");
2793b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2794b8d26b3bSNicholas Bellinger 		goto destroy_rx_wq;
2795b8d26b3bSNicholas Bellinger 	}
2796b8d26b3bSNicholas Bellinger 
2797b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
2798b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2799b8d26b3bSNicholas Bellinger 	return 0;
2800b8d26b3bSNicholas Bellinger 
2801b8d26b3bSNicholas Bellinger destroy_rx_wq:
2802b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
2803b8d26b3bSNicholas Bellinger 	return ret;
2804b8d26b3bSNicholas Bellinger }
2805b8d26b3bSNicholas Bellinger 
2806b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2807b8d26b3bSNicholas Bellinger {
2808b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2809b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
2810b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
2811b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2812b8d26b3bSNicholas Bellinger }
2813b8d26b3bSNicholas Bellinger 
2814b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2815b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
2816b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2817b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2818b8d26b3bSNicholas Bellinger 
2819b8d26b3bSNicholas Bellinger module_init(isert_init);
2820b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2821