xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 4c76251e8e8f704ed7eace603a8041458a1ed16f)
1b8d26b3bSNicholas Bellinger /*******************************************************************************
2b8d26b3bSNicholas Bellinger  * This file contains iSCSI extentions for RDMA (iSER) Verbs
3b8d26b3bSNicholas Bellinger  *
4*4c76251eSNicholas 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>
25b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h>
26b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h>
27b8d26b3bSNicholas Bellinger #include <target/target_core_base.h>
28b8d26b3bSNicholas Bellinger #include <target/target_core_fabric.h>
29b8d26b3bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
30b8d26b3bSNicholas Bellinger 
31b8d26b3bSNicholas Bellinger #include "isert_proto.h"
32b8d26b3bSNicholas Bellinger #include "ib_isert.h"
33b8d26b3bSNicholas Bellinger 
34b8d26b3bSNicholas Bellinger #define	ISERT_MAX_CONN		8
35b8d26b3bSNicholas Bellinger #define ISER_MAX_RX_CQ_LEN	(ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
36b8d26b3bSNicholas Bellinger #define ISER_MAX_TX_CQ_LEN	(ISERT_QP_MAX_REQ_DTOS  * ISERT_MAX_CONN)
37b8d26b3bSNicholas Bellinger 
38b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
39b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
40b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_rx_wq;
41b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
42b8d26b3bSNicholas Bellinger 
43b8d26b3bSNicholas Bellinger static void
44d40945d8SVu Pham isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
45d40945d8SVu Pham static int
46d40945d8SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
47d40945d8SVu Pham 	       struct isert_rdma_wr *wr);
4859464ef4SVu Pham static void
4959464ef4SVu Pham isert_unreg_rdma_frwr(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
5059464ef4SVu Pham static int
5159464ef4SVu Pham isert_reg_rdma_frwr(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
5259464ef4SVu Pham 		    struct isert_rdma_wr *wr);
53d40945d8SVu Pham 
54d40945d8SVu Pham static void
55b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
56b8d26b3bSNicholas Bellinger {
57b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)context;
58b8d26b3bSNicholas Bellinger 
59b8d26b3bSNicholas Bellinger 	pr_err("isert_qp_event_callback event: %d\n", e->event);
60b8d26b3bSNicholas Bellinger 	switch (e->event) {
61b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
62b8d26b3bSNicholas Bellinger 		rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
63b8d26b3bSNicholas Bellinger 		break;
64b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
65b8d26b3bSNicholas Bellinger 		pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
66b8d26b3bSNicholas Bellinger 		break;
67b8d26b3bSNicholas Bellinger 	default:
68b8d26b3bSNicholas Bellinger 		break;
69b8d26b3bSNicholas Bellinger 	}
70b8d26b3bSNicholas Bellinger }
71b8d26b3bSNicholas Bellinger 
72b8d26b3bSNicholas Bellinger static int
73b8d26b3bSNicholas Bellinger isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
74b8d26b3bSNicholas Bellinger {
75b8d26b3bSNicholas Bellinger 	int ret;
76b8d26b3bSNicholas Bellinger 
77b8d26b3bSNicholas Bellinger 	ret = ib_query_device(ib_dev, devattr);
78b8d26b3bSNicholas Bellinger 	if (ret) {
79b8d26b3bSNicholas Bellinger 		pr_err("ib_query_device() failed: %d\n", ret);
80b8d26b3bSNicholas Bellinger 		return ret;
81b8d26b3bSNicholas Bellinger 	}
82b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
83b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
84b8d26b3bSNicholas Bellinger 
85b8d26b3bSNicholas Bellinger 	return 0;
86b8d26b3bSNicholas Bellinger }
87b8d26b3bSNicholas Bellinger 
88b8d26b3bSNicholas Bellinger static int
89b8d26b3bSNicholas Bellinger isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
90b8d26b3bSNicholas Bellinger {
91b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
92b8d26b3bSNicholas Bellinger 	struct ib_qp_init_attr attr;
93b8d26b3bSNicholas Bellinger 	int ret, index, min_index = 0;
94b8d26b3bSNicholas Bellinger 
95b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
96b8d26b3bSNicholas Bellinger 	for (index = 0; index < device->cqs_used; index++)
97b8d26b3bSNicholas Bellinger 		if (device->cq_active_qps[index] <
98b8d26b3bSNicholas Bellinger 		    device->cq_active_qps[min_index])
99b8d26b3bSNicholas Bellinger 			min_index = index;
100b8d26b3bSNicholas Bellinger 	device->cq_active_qps[min_index]++;
101b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
102b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
103b8d26b3bSNicholas Bellinger 
104b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
105b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
106b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
107b8d26b3bSNicholas Bellinger 	attr.send_cq = device->dev_tx_cq[min_index];
108b8d26b3bSNicholas Bellinger 	attr.recv_cq = device->dev_rx_cq[min_index];
109b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
110b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
111b8d26b3bSNicholas Bellinger 	/*
112b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
113b8d26b3bSNicholas Bellinger 	 * work-around for RDMA_READ..
114b8d26b3bSNicholas Bellinger 	 */
11559464ef4SVu Pham 	attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
116b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
117b8d26b3bSNicholas Bellinger 
118b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
119b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
120b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
121b8d26b3bSNicholas Bellinger 
122b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
123b8d26b3bSNicholas Bellinger 		 cma_id->device);
124b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
125b8d26b3bSNicholas Bellinger 		 isert_conn->conn_pd->device);
126b8d26b3bSNicholas Bellinger 
127b8d26b3bSNicholas Bellinger 	ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
128b8d26b3bSNicholas Bellinger 	if (ret) {
129b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_qp failed for cma_id %d\n", ret);
130b8d26b3bSNicholas Bellinger 		return ret;
131b8d26b3bSNicholas Bellinger 	}
132b8d26b3bSNicholas Bellinger 	isert_conn->conn_qp = cma_id->qp;
133b8d26b3bSNicholas Bellinger 	pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
134b8d26b3bSNicholas Bellinger 
135b8d26b3bSNicholas Bellinger 	return 0;
136b8d26b3bSNicholas Bellinger }
137b8d26b3bSNicholas Bellinger 
138b8d26b3bSNicholas Bellinger static void
139b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
140b8d26b3bSNicholas Bellinger {
141b8d26b3bSNicholas Bellinger 	pr_debug("isert_cq_event_callback event: %d\n", e->event);
142b8d26b3bSNicholas Bellinger }
143b8d26b3bSNicholas Bellinger 
144b8d26b3bSNicholas Bellinger static int
145b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
146b8d26b3bSNicholas Bellinger {
147b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
148b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
149b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
150b8d26b3bSNicholas Bellinger 	u64 dma_addr;
151b8d26b3bSNicholas Bellinger 	int i, j;
152b8d26b3bSNicholas Bellinger 
153b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
154b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
155b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
156b8d26b3bSNicholas Bellinger 		goto fail;
157b8d26b3bSNicholas Bellinger 
158b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
159b8d26b3bSNicholas Bellinger 
160b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
161b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
162b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
163b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
164b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
165b8d26b3bSNicholas Bellinger 
166b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
167b8d26b3bSNicholas Bellinger 
168b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
169b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
170b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
171b8d26b3bSNicholas Bellinger 		rx_sg->lkey = isert_conn->conn_mr->lkey;
172b8d26b3bSNicholas Bellinger 	}
173b8d26b3bSNicholas Bellinger 
174b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
175b8d26b3bSNicholas Bellinger 	return 0;
176b8d26b3bSNicholas Bellinger 
177b8d26b3bSNicholas Bellinger dma_map_fail:
178b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
179b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
180b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
181b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
182b8d26b3bSNicholas Bellinger 	}
183b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
184b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
185b8d26b3bSNicholas Bellinger fail:
186b8d26b3bSNicholas Bellinger 	return -ENOMEM;
187b8d26b3bSNicholas Bellinger }
188b8d26b3bSNicholas Bellinger 
189b8d26b3bSNicholas Bellinger static void
190b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
191b8d26b3bSNicholas Bellinger {
192b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
193b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
194b8d26b3bSNicholas Bellinger 	int i;
195b8d26b3bSNicholas Bellinger 
196b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
197b8d26b3bSNicholas Bellinger 		return;
198b8d26b3bSNicholas Bellinger 
199b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
200b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
201b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
202b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
203b8d26b3bSNicholas Bellinger 	}
204b8d26b3bSNicholas Bellinger 
205b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
206b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
207b8d26b3bSNicholas Bellinger }
208b8d26b3bSNicholas Bellinger 
209b8d26b3bSNicholas Bellinger static void isert_cq_tx_callback(struct ib_cq *, void *);
210b8d26b3bSNicholas Bellinger static void isert_cq_rx_callback(struct ib_cq *, void *);
211b8d26b3bSNicholas Bellinger 
212b8d26b3bSNicholas Bellinger static int
213b8d26b3bSNicholas Bellinger isert_create_device_ib_res(struct isert_device *device)
214b8d26b3bSNicholas Bellinger {
215b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = device->ib_device;
216b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
21759464ef4SVu Pham 	struct ib_device_attr *dev_attr;
218b8d26b3bSNicholas Bellinger 	int ret = 0, i, j;
219b8d26b3bSNicholas Bellinger 
22059464ef4SVu Pham 	dev_attr = &device->dev_attr;
22159464ef4SVu Pham 	ret = isert_query_device(ib_dev, dev_attr);
22259464ef4SVu Pham 	if (ret)
22359464ef4SVu Pham 		return ret;
22459464ef4SVu Pham 
225d40945d8SVu Pham 	/* asign function handlers */
22659464ef4SVu Pham 	if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
22759464ef4SVu Pham 		device->use_frwr = 1;
22859464ef4SVu Pham 		device->reg_rdma_mem = isert_reg_rdma_frwr;
22959464ef4SVu Pham 		device->unreg_rdma_mem = isert_unreg_rdma_frwr;
23059464ef4SVu Pham 	} else {
23159464ef4SVu Pham 		device->use_frwr = 0;
232d40945d8SVu Pham 		device->reg_rdma_mem = isert_map_rdma;
233d40945d8SVu Pham 		device->unreg_rdma_mem = isert_unmap_cmd;
23459464ef4SVu Pham 	}
235d40945d8SVu Pham 
236b8d26b3bSNicholas Bellinger 	device->cqs_used = min_t(int, num_online_cpus(),
237b8d26b3bSNicholas Bellinger 				 device->ib_device->num_comp_vectors);
238b8d26b3bSNicholas Bellinger 	device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
23959464ef4SVu Pham 	pr_debug("Using %d CQs, device %s supports %d vectors support FRWR %d\n",
240b8d26b3bSNicholas Bellinger 		 device->cqs_used, device->ib_device->name,
24159464ef4SVu Pham 		 device->ib_device->num_comp_vectors, device->use_frwr);
242b8d26b3bSNicholas Bellinger 	device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
243b8d26b3bSNicholas Bellinger 				device->cqs_used, GFP_KERNEL);
244b8d26b3bSNicholas Bellinger 	if (!device->cq_desc) {
245b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate device->cq_desc\n");
246b8d26b3bSNicholas Bellinger 		return -ENOMEM;
247b8d26b3bSNicholas Bellinger 	}
248b8d26b3bSNicholas Bellinger 	cq_desc = device->cq_desc;
249b8d26b3bSNicholas Bellinger 
250b8d26b3bSNicholas Bellinger 	device->dev_pd = ib_alloc_pd(ib_dev);
251b8d26b3bSNicholas Bellinger 	if (IS_ERR(device->dev_pd)) {
252b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device->dev_pd);
253b8d26b3bSNicholas Bellinger 		pr_err("ib_alloc_pd failed for dev_pd: %d\n", ret);
254b8d26b3bSNicholas Bellinger 		goto out_cq_desc;
255b8d26b3bSNicholas Bellinger 	}
256b8d26b3bSNicholas Bellinger 
257b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
258b8d26b3bSNicholas Bellinger 		cq_desc[i].device = device;
259b8d26b3bSNicholas Bellinger 		cq_desc[i].cq_index = i;
260b8d26b3bSNicholas Bellinger 
261b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
262b8d26b3bSNicholas Bellinger 						isert_cq_rx_callback,
263b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
264b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
265b8d26b3bSNicholas Bellinger 						ISER_MAX_RX_CQ_LEN, i);
266b8d26b3bSNicholas Bellinger 		if (IS_ERR(device->dev_rx_cq[i]))
267b8d26b3bSNicholas Bellinger 			goto out_cq;
268b8d26b3bSNicholas Bellinger 
269b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
270b8d26b3bSNicholas Bellinger 						isert_cq_tx_callback,
271b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
272b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
273b8d26b3bSNicholas Bellinger 						ISER_MAX_TX_CQ_LEN, i);
274b8d26b3bSNicholas Bellinger 		if (IS_ERR(device->dev_tx_cq[i]))
275b8d26b3bSNicholas Bellinger 			goto out_cq;
276b8d26b3bSNicholas Bellinger 
277b8d26b3bSNicholas Bellinger 		if (ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP))
278b8d26b3bSNicholas Bellinger 			goto out_cq;
279b8d26b3bSNicholas Bellinger 
280b8d26b3bSNicholas Bellinger 		if (ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP))
281b8d26b3bSNicholas Bellinger 			goto out_cq;
282b8d26b3bSNicholas Bellinger 	}
283b8d26b3bSNicholas Bellinger 
284b8d26b3bSNicholas Bellinger 	device->dev_mr = ib_get_dma_mr(device->dev_pd, IB_ACCESS_LOCAL_WRITE);
285b8d26b3bSNicholas Bellinger 	if (IS_ERR(device->dev_mr)) {
286b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device->dev_mr);
287b8d26b3bSNicholas Bellinger 		pr_err("ib_get_dma_mr failed for dev_mr: %d\n", 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 	ib_dealloc_pd(device->dev_pd);
307b8d26b3bSNicholas Bellinger 
308b8d26b3bSNicholas Bellinger out_cq_desc:
309b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
310b8d26b3bSNicholas Bellinger 
311b8d26b3bSNicholas Bellinger 	return ret;
312b8d26b3bSNicholas Bellinger }
313b8d26b3bSNicholas Bellinger 
314b8d26b3bSNicholas Bellinger static void
315b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
316b8d26b3bSNicholas Bellinger {
317b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
318b8d26b3bSNicholas Bellinger 	int i;
319b8d26b3bSNicholas Bellinger 
320b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
321b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[i];
322b8d26b3bSNicholas Bellinger 
323b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_rx_work);
324b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_tx_work);
325b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_rx_cq[i]);
326b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_tx_cq[i]);
327b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = NULL;
328b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = NULL;
329b8d26b3bSNicholas Bellinger 	}
330b8d26b3bSNicholas Bellinger 
331b8d26b3bSNicholas Bellinger 	ib_dereg_mr(device->dev_mr);
332b8d26b3bSNicholas Bellinger 	ib_dealloc_pd(device->dev_pd);
333b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
334b8d26b3bSNicholas Bellinger }
335b8d26b3bSNicholas Bellinger 
336b8d26b3bSNicholas Bellinger static void
337b8d26b3bSNicholas Bellinger isert_device_try_release(struct isert_device *device)
338b8d26b3bSNicholas Bellinger {
339b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
340b8d26b3bSNicholas Bellinger 	device->refcount--;
341b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
342b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
343b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
344b8d26b3bSNicholas Bellinger 		kfree(device);
345b8d26b3bSNicholas Bellinger 	}
346b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
347b8d26b3bSNicholas Bellinger }
348b8d26b3bSNicholas Bellinger 
349b8d26b3bSNicholas Bellinger static struct isert_device *
350b8d26b3bSNicholas Bellinger isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
351b8d26b3bSNicholas Bellinger {
352b8d26b3bSNicholas Bellinger 	struct isert_device *device;
353b8d26b3bSNicholas Bellinger 	int ret;
354b8d26b3bSNicholas Bellinger 
355b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
356b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
357b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
358b8d26b3bSNicholas Bellinger 			device->refcount++;
359b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
360b8d26b3bSNicholas Bellinger 			return device;
361b8d26b3bSNicholas Bellinger 		}
362b8d26b3bSNicholas Bellinger 	}
363b8d26b3bSNicholas Bellinger 
364b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
365b8d26b3bSNicholas Bellinger 	if (!device) {
366b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
367b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
368b8d26b3bSNicholas Bellinger 	}
369b8d26b3bSNicholas Bellinger 
370b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
371b8d26b3bSNicholas Bellinger 
372b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
373b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
374b8d26b3bSNicholas Bellinger 	if (ret) {
375b8d26b3bSNicholas Bellinger 		kfree(device);
376b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
377b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
378b8d26b3bSNicholas Bellinger 	}
379b8d26b3bSNicholas Bellinger 
380b8d26b3bSNicholas Bellinger 	device->refcount++;
381b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
382b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
383b8d26b3bSNicholas Bellinger 
384b8d26b3bSNicholas Bellinger 	return device;
385b8d26b3bSNicholas Bellinger }
386b8d26b3bSNicholas Bellinger 
38759464ef4SVu Pham static void
38859464ef4SVu Pham isert_conn_free_frwr_pool(struct isert_conn *isert_conn)
38959464ef4SVu Pham {
39059464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
39159464ef4SVu Pham 	int i = 0;
39259464ef4SVu Pham 
39359464ef4SVu Pham 	if (list_empty(&isert_conn->conn_frwr_pool))
39459464ef4SVu Pham 		return;
39559464ef4SVu Pham 
39659464ef4SVu Pham 	pr_debug("Freeing conn %p frwr pool", isert_conn);
39759464ef4SVu Pham 
39859464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
39959464ef4SVu Pham 				 &isert_conn->conn_frwr_pool, list) {
40059464ef4SVu Pham 		list_del(&fr_desc->list);
40159464ef4SVu Pham 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
40259464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
40359464ef4SVu Pham 		kfree(fr_desc);
40459464ef4SVu Pham 		++i;
40559464ef4SVu Pham 	}
40659464ef4SVu Pham 
40759464ef4SVu Pham 	if (i < isert_conn->conn_frwr_pool_size)
40859464ef4SVu Pham 		pr_warn("Pool still has %d regions registered\n",
40959464ef4SVu Pham 			isert_conn->conn_frwr_pool_size - i);
41059464ef4SVu Pham }
41159464ef4SVu Pham 
41259464ef4SVu Pham static int
41359464ef4SVu Pham isert_conn_create_frwr_pool(struct isert_conn *isert_conn)
41459464ef4SVu Pham {
41559464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
41659464ef4SVu Pham 	struct isert_device *device = isert_conn->conn_device;
41759464ef4SVu Pham 	int i, ret;
41859464ef4SVu Pham 
41959464ef4SVu Pham 	INIT_LIST_HEAD(&isert_conn->conn_frwr_pool);
42059464ef4SVu Pham 	isert_conn->conn_frwr_pool_size = 0;
42159464ef4SVu Pham 	for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
42259464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
42359464ef4SVu Pham 		if (!fr_desc) {
42459464ef4SVu Pham 			pr_err("Failed to allocate fast_reg descriptor\n");
42559464ef4SVu Pham 			ret = -ENOMEM;
42659464ef4SVu Pham 			goto err;
42759464ef4SVu Pham 		}
42859464ef4SVu Pham 
42959464ef4SVu Pham 		fr_desc->data_frpl =
43059464ef4SVu Pham 			ib_alloc_fast_reg_page_list(device->ib_device,
43159464ef4SVu Pham 						    ISCSI_ISER_SG_TABLESIZE);
43259464ef4SVu Pham 		if (IS_ERR(fr_desc->data_frpl)) {
43359464ef4SVu Pham 			pr_err("Failed to allocate fr_pg_list err=%ld\n",
43459464ef4SVu Pham 			       PTR_ERR(fr_desc->data_frpl));
43559464ef4SVu Pham 			ret = PTR_ERR(fr_desc->data_frpl);
43659464ef4SVu Pham 			goto err;
43759464ef4SVu Pham 		}
43859464ef4SVu Pham 
43959464ef4SVu Pham 		fr_desc->data_mr = ib_alloc_fast_reg_mr(device->dev_pd,
44059464ef4SVu Pham 					ISCSI_ISER_SG_TABLESIZE);
44159464ef4SVu Pham 		if (IS_ERR(fr_desc->data_mr)) {
44259464ef4SVu Pham 			pr_err("Failed to allocate frmr err=%ld\n",
44359464ef4SVu Pham 			       PTR_ERR(fr_desc->data_mr));
44459464ef4SVu Pham 			ret = PTR_ERR(fr_desc->data_mr);
44559464ef4SVu Pham 			ib_free_fast_reg_page_list(fr_desc->data_frpl);
44659464ef4SVu Pham 			goto err;
44759464ef4SVu Pham 		}
44859464ef4SVu Pham 		pr_debug("Create fr_desc %p page_list %p\n",
44959464ef4SVu Pham 			 fr_desc, fr_desc->data_frpl->page_list);
45059464ef4SVu Pham 
45159464ef4SVu Pham 		fr_desc->valid = true;
45259464ef4SVu Pham 		list_add_tail(&fr_desc->list, &isert_conn->conn_frwr_pool);
45359464ef4SVu Pham 		isert_conn->conn_frwr_pool_size++;
45459464ef4SVu Pham 	}
45559464ef4SVu Pham 
45659464ef4SVu Pham 	pr_debug("Creating conn %p frwr pool size=%d",
45759464ef4SVu Pham 		 isert_conn, isert_conn->conn_frwr_pool_size);
45859464ef4SVu Pham 
45959464ef4SVu Pham 	return 0;
46059464ef4SVu Pham 
46159464ef4SVu Pham err:
46259464ef4SVu Pham 	isert_conn_free_frwr_pool(isert_conn);
46359464ef4SVu Pham 	return ret;
46459464ef4SVu Pham }
46559464ef4SVu Pham 
466b8d26b3bSNicholas Bellinger static int
467b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
468b8d26b3bSNicholas Bellinger {
469b8d26b3bSNicholas Bellinger 	struct iscsi_np *np = cma_id->context;
470b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = np->np_context;
471b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
472b8d26b3bSNicholas Bellinger 	struct isert_device *device;
473b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
474b8d26b3bSNicholas Bellinger 	int ret = 0;
475b8d26b3bSNicholas Bellinger 
476b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
477b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
478b8d26b3bSNicholas Bellinger 
479b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
480b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
481b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn\n");
482b8d26b3bSNicholas Bellinger 		return -ENOMEM;
483b8d26b3bSNicholas Bellinger 	}
484b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
485b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
486b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
487b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_conn->conn_wait);
488b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_conn->conn_wait_comp_err);
489b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
490b8d26b3bSNicholas Bellinger 	kref_get(&isert_conn->conn_kref);
491b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
49259464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
493b8d26b3bSNicholas Bellinger 
494b8d26b3bSNicholas Bellinger 	cma_id->context = isert_conn;
495b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
496b8d26b3bSNicholas Bellinger 	isert_conn->responder_resources = event->param.conn.responder_resources;
497b8d26b3bSNicholas Bellinger 	isert_conn->initiator_depth = event->param.conn.initiator_depth;
498b8d26b3bSNicholas Bellinger 	pr_debug("Using responder_resources: %u initiator_depth: %u\n",
499b8d26b3bSNicholas Bellinger 		 isert_conn->responder_resources, isert_conn->initiator_depth);
500b8d26b3bSNicholas Bellinger 
501b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
502b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
503b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
504b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
505b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
506b8d26b3bSNicholas Bellinger 		goto out;
507b8d26b3bSNicholas Bellinger 	}
508b8d26b3bSNicholas Bellinger 
509b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
510b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
511b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
512b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
513b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
514b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
515b8d26b3bSNicholas Bellinger 
516b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
517b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
518b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
519b8d26b3bSNicholas Bellinger 
520b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
521b8d26b3bSNicholas Bellinger 	if (ret) {
522b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
523b8d26b3bSNicholas Bellinger 		       ret);
524b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
525b8d26b3bSNicholas Bellinger 		goto out_login_buf;
526b8d26b3bSNicholas Bellinger 	}
527b8d26b3bSNicholas Bellinger 
528b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
529b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
530b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
531b8d26b3bSNicholas Bellinger 
532b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
533b8d26b3bSNicholas Bellinger 	if (ret) {
534b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
535b8d26b3bSNicholas Bellinger 		       ret);
536b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
537b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
538b8d26b3bSNicholas Bellinger 	}
539b8d26b3bSNicholas Bellinger 
540b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
541b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
542b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
543b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
544b8d26b3bSNicholas Bellinger 	}
545b8d26b3bSNicholas Bellinger 
546b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
547b8d26b3bSNicholas Bellinger 	isert_conn->conn_pd = device->dev_pd;
548b8d26b3bSNicholas Bellinger 	isert_conn->conn_mr = device->dev_mr;
549b8d26b3bSNicholas Bellinger 
55059464ef4SVu Pham 	if (device->use_frwr) {
55159464ef4SVu Pham 		ret = isert_conn_create_frwr_pool(isert_conn);
55259464ef4SVu Pham 		if (ret) {
55359464ef4SVu Pham 			pr_err("Conn: %p failed to create frwr_pool\n", isert_conn);
55459464ef4SVu Pham 			goto out_frwr;
55559464ef4SVu Pham 		}
55659464ef4SVu Pham 	}
55759464ef4SVu Pham 
558b8d26b3bSNicholas Bellinger 	ret = isert_conn_setup_qp(isert_conn, cma_id);
559b8d26b3bSNicholas Bellinger 	if (ret)
560b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
561b8d26b3bSNicholas Bellinger 
562b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
563b8d26b3bSNicholas Bellinger 	list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
564b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
565b8d26b3bSNicholas Bellinger 
566b8d26b3bSNicholas Bellinger 	pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
567b8d26b3bSNicholas Bellinger 	wake_up(&isert_np->np_accept_wq);
568b8d26b3bSNicholas Bellinger 	return 0;
569b8d26b3bSNicholas Bellinger 
570b8d26b3bSNicholas Bellinger out_conn_dev:
57159464ef4SVu Pham 	if (device->use_frwr)
57259464ef4SVu Pham 		isert_conn_free_frwr_pool(isert_conn);
57359464ef4SVu Pham out_frwr:
574b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
575b8d26b3bSNicholas Bellinger out_rsp_dma_map:
576b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
577b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
578b8d26b3bSNicholas Bellinger out_req_dma_map:
579b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
580b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
581b8d26b3bSNicholas Bellinger out_login_buf:
582b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
583b8d26b3bSNicholas Bellinger out:
584b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
585b8d26b3bSNicholas Bellinger 	return ret;
586b8d26b3bSNicholas Bellinger }
587b8d26b3bSNicholas Bellinger 
588b8d26b3bSNicholas Bellinger static void
589b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
590b8d26b3bSNicholas Bellinger {
591b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
592b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
593b8d26b3bSNicholas Bellinger 	int cq_index;
594b8d26b3bSNicholas Bellinger 
595b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
596b8d26b3bSNicholas Bellinger 
59759464ef4SVu Pham 	if (device->use_frwr)
59859464ef4SVu Pham 		isert_conn_free_frwr_pool(isert_conn);
59959464ef4SVu Pham 
600b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
601b8d26b3bSNicholas Bellinger 		cq_index = ((struct isert_cq_desc *)
602b8d26b3bSNicholas Bellinger 			isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
603b8d26b3bSNicholas Bellinger 		pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
604b8d26b3bSNicholas Bellinger 		isert_conn->conn_device->cq_active_qps[cq_index]--;
605b8d26b3bSNicholas Bellinger 
606b8d26b3bSNicholas Bellinger 		rdma_destroy_qp(isert_conn->conn_cm_id);
607b8d26b3bSNicholas Bellinger 	}
608b8d26b3bSNicholas Bellinger 
609b8d26b3bSNicholas Bellinger 	isert_free_rx_descriptors(isert_conn);
610b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_conn->conn_cm_id);
611b8d26b3bSNicholas Bellinger 
612b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
613b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
614b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
615b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
616b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
617b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
618b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
619b8d26b3bSNicholas Bellinger 	}
620b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
621b8d26b3bSNicholas Bellinger 
622b8d26b3bSNicholas Bellinger 	if (device)
623b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
624b8d26b3bSNicholas Bellinger 
625b8d26b3bSNicholas Bellinger 	pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
626b8d26b3bSNicholas Bellinger }
627b8d26b3bSNicholas Bellinger 
628b8d26b3bSNicholas Bellinger static void
629b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
630b8d26b3bSNicholas Bellinger {
631b8d26b3bSNicholas Bellinger 	return;
632b8d26b3bSNicholas Bellinger }
633b8d26b3bSNicholas Bellinger 
634b8d26b3bSNicholas Bellinger static void
635b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
636b8d26b3bSNicholas Bellinger {
637b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
638b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
639b8d26b3bSNicholas Bellinger 
640b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
641b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
642b8d26b3bSNicholas Bellinger 
643b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
644b8d26b3bSNicholas Bellinger }
645b8d26b3bSNicholas Bellinger 
646b8d26b3bSNicholas Bellinger static void
647b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
648b8d26b3bSNicholas Bellinger {
649b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
650b8d26b3bSNicholas Bellinger }
651b8d26b3bSNicholas Bellinger 
652b8d26b3bSNicholas Bellinger static void
653b8d26b3bSNicholas Bellinger isert_disconnect_work(struct work_struct *work)
654b8d26b3bSNicholas Bellinger {
655b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(work,
656b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_logout_work);
657b8d26b3bSNicholas Bellinger 
658b8d26b3bSNicholas Bellinger 	pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
659b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
660b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_DOWN;
661b8d26b3bSNicholas Bellinger 
662b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
663b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
664b8d26b3bSNicholas Bellinger 		pr_debug("Calling wake_up(&isert_conn->conn_wait);\n");
665b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
666b2cb9649SNicholas Bellinger 		goto wake_up;
667b8d26b3bSNicholas Bellinger 	}
668b2cb9649SNicholas Bellinger 	if (!isert_conn->conn_cm_id) {
669b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
670b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
671b2cb9649SNicholas Bellinger 		return;
672b2cb9649SNicholas Bellinger 	}
673b2cb9649SNicholas Bellinger 	if (!isert_conn->logout_posted) {
674b2cb9649SNicholas Bellinger 		pr_debug("Calling rdma_disconnect for !logout_posted from"
675b2cb9649SNicholas Bellinger 			 " isert_disconnect_work\n");
676b2cb9649SNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
677b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
678b2cb9649SNicholas Bellinger 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
679b2cb9649SNicholas Bellinger 		goto wake_up;
680b2cb9649SNicholas Bellinger 	}
681b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
682b8d26b3bSNicholas Bellinger 
683b2cb9649SNicholas Bellinger wake_up:
684b2cb9649SNicholas Bellinger 	wake_up(&isert_conn->conn_wait);
685b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
686b8d26b3bSNicholas Bellinger }
687b8d26b3bSNicholas Bellinger 
688b8d26b3bSNicholas Bellinger static void
689b8d26b3bSNicholas Bellinger isert_disconnected_handler(struct rdma_cm_id *cma_id)
690b8d26b3bSNicholas Bellinger {
691b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
692b8d26b3bSNicholas Bellinger 
693b8d26b3bSNicholas Bellinger 	INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
694b8d26b3bSNicholas Bellinger 	schedule_work(&isert_conn->conn_logout_work);
695b8d26b3bSNicholas Bellinger }
696b8d26b3bSNicholas Bellinger 
697b8d26b3bSNicholas Bellinger static int
698b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
699b8d26b3bSNicholas Bellinger {
700b8d26b3bSNicholas Bellinger 	int ret = 0;
701b8d26b3bSNicholas Bellinger 
702b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
703b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
704b8d26b3bSNicholas Bellinger 
705b8d26b3bSNicholas Bellinger 	switch (event->event) {
706b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
707b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
708b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
709b8d26b3bSNicholas Bellinger 		break;
710b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
711b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
712b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
713b8d26b3bSNicholas Bellinger 		break;
714b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DISCONNECTED:
715b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
716b8d26b3bSNicholas Bellinger 		isert_disconnected_handler(cma_id);
717b8d26b3bSNicholas Bellinger 		break;
718b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
719b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ADDR_CHANGE:
720b8d26b3bSNicholas Bellinger 		break;
721b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
722b8d26b3bSNicholas Bellinger 	default:
723b8d26b3bSNicholas Bellinger 		pr_err("Unknown RDMA CMA event: %d\n", event->event);
724b8d26b3bSNicholas Bellinger 		break;
725b8d26b3bSNicholas Bellinger 	}
726b8d26b3bSNicholas Bellinger 
727b8d26b3bSNicholas Bellinger 	if (ret != 0) {
728b8d26b3bSNicholas Bellinger 		pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
729b8d26b3bSNicholas Bellinger 		       event->event, ret);
730b8d26b3bSNicholas Bellinger 		dump_stack();
731b8d26b3bSNicholas Bellinger 	}
732b8d26b3bSNicholas Bellinger 
733b8d26b3bSNicholas Bellinger 	return ret;
734b8d26b3bSNicholas Bellinger }
735b8d26b3bSNicholas Bellinger 
736b8d26b3bSNicholas Bellinger static int
737b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
738b8d26b3bSNicholas Bellinger {
739b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
740b8d26b3bSNicholas Bellinger 	int i, ret;
741b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
742b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
743b8d26b3bSNicholas Bellinger 
744b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
745b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
746b8d26b3bSNicholas Bellinger 		rx_wr->wr_id	= (unsigned long)rx_desc;
747b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
748b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
749b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
750b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
751b8d26b3bSNicholas Bellinger 	}
752b8d26b3bSNicholas Bellinger 
753b8d26b3bSNicholas Bellinger 	rx_wr--;
754b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
755b8d26b3bSNicholas Bellinger 
756b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
757b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
758b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
759b8d26b3bSNicholas Bellinger 	if (ret) {
760b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed with ret: %d\n", ret);
761b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
762b8d26b3bSNicholas Bellinger 	} else {
763b8d26b3bSNicholas Bellinger 		pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
764b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
765b8d26b3bSNicholas Bellinger 	}
766b8d26b3bSNicholas Bellinger 	return ret;
767b8d26b3bSNicholas Bellinger }
768b8d26b3bSNicholas Bellinger 
769b8d26b3bSNicholas Bellinger static int
770b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
771b8d26b3bSNicholas Bellinger {
772b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
773b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
774b8d26b3bSNicholas Bellinger 	int ret;
775b8d26b3bSNicholas Bellinger 
776b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
777b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
778b8d26b3bSNicholas Bellinger 
779b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
780b8d26b3bSNicholas Bellinger 	send_wr.wr_id	= (unsigned long)tx_desc;
781b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
782b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
783b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
784b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
785b8d26b3bSNicholas Bellinger 
786b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
787b8d26b3bSNicholas Bellinger 
788b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
789b8d26b3bSNicholas Bellinger 	if (ret) {
790b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send() failed, ret: %d\n", ret);
791b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
792b8d26b3bSNicholas Bellinger 	}
793b8d26b3bSNicholas Bellinger 
794b8d26b3bSNicholas Bellinger 	return ret;
795b8d26b3bSNicholas Bellinger }
796b8d26b3bSNicholas Bellinger 
797b8d26b3bSNicholas Bellinger static void
798b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
799b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
800b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
801b8d26b3bSNicholas Bellinger {
802b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
803b8d26b3bSNicholas Bellinger 
804b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
805b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
806b8d26b3bSNicholas Bellinger 
807b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
808b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
809b8d26b3bSNicholas Bellinger 
810b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
811b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
812b8d26b3bSNicholas Bellinger 
813b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
814b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
815b8d26b3bSNicholas Bellinger 		pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
816b8d26b3bSNicholas Bellinger 	}
817b8d26b3bSNicholas Bellinger }
818b8d26b3bSNicholas Bellinger 
819b8d26b3bSNicholas Bellinger static int
820b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
821b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
822b8d26b3bSNicholas Bellinger {
823b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
824b8d26b3bSNicholas Bellinger 	u64 dma_addr;
825b8d26b3bSNicholas Bellinger 
826b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
827b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
828b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
829b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error() failed\n");
830b8d26b3bSNicholas Bellinger 		return -ENOMEM;
831b8d26b3bSNicholas Bellinger 	}
832b8d26b3bSNicholas Bellinger 
833b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
834b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
835b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
836b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
837b8d26b3bSNicholas Bellinger 
838b8d26b3bSNicholas Bellinger 	pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
839b8d26b3bSNicholas Bellinger 		 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
840b8d26b3bSNicholas Bellinger 		 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
841b8d26b3bSNicholas Bellinger 
842b8d26b3bSNicholas Bellinger 	return 0;
843b8d26b3bSNicholas Bellinger }
844b8d26b3bSNicholas Bellinger 
845b8d26b3bSNicholas Bellinger static void
846b8d26b3bSNicholas Bellinger isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
847b8d26b3bSNicholas Bellinger {
848b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
849b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
850b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
851b8d26b3bSNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
852b8d26b3bSNicholas Bellinger 	send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
853b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
854b8d26b3bSNicholas Bellinger }
855b8d26b3bSNicholas Bellinger 
856b8d26b3bSNicholas Bellinger static int
857b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
858b8d26b3bSNicholas Bellinger {
859b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
860b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
861b8d26b3bSNicholas Bellinger 	int ret;
862b8d26b3bSNicholas Bellinger 
863b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
864b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
865b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
866b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
867b8d26b3bSNicholas Bellinger 
868b8d26b3bSNicholas Bellinger 	pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
869b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
870b8d26b3bSNicholas Bellinger 
871b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
872b8d26b3bSNicholas Bellinger 	rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
873b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
874b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
875b8d26b3bSNicholas Bellinger 
876b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
877b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
878b8d26b3bSNicholas Bellinger 	if (ret) {
879b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed: %d\n", ret);
880b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
881b8d26b3bSNicholas Bellinger 	}
882b8d26b3bSNicholas Bellinger 
883b8d26b3bSNicholas Bellinger 	pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
884b8d26b3bSNicholas Bellinger 	return ret;
885b8d26b3bSNicholas Bellinger }
886b8d26b3bSNicholas Bellinger 
887b8d26b3bSNicholas Bellinger static int
888b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
889b8d26b3bSNicholas Bellinger 		   u32 length)
890b8d26b3bSNicholas Bellinger {
891b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
892b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
893b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
894b8d26b3bSNicholas Bellinger 	int ret;
895b8d26b3bSNicholas Bellinger 
896b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
897b8d26b3bSNicholas Bellinger 
898b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
899b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
900b8d26b3bSNicholas Bellinger 
901b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
902b8d26b3bSNicholas Bellinger 
903b8d26b3bSNicholas Bellinger 	if (length > 0) {
904b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
905b8d26b3bSNicholas Bellinger 
906b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
907b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
908b8d26b3bSNicholas Bellinger 
909b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
910b8d26b3bSNicholas Bellinger 
911b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
912b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
913b8d26b3bSNicholas Bellinger 
914b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
915b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
916b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
917b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
918b8d26b3bSNicholas Bellinger 	}
919b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
920b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
921b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
922b8d26b3bSNicholas Bellinger 			if (ret)
923b8d26b3bSNicholas Bellinger 				return ret;
924b8d26b3bSNicholas Bellinger 
925b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
926b8d26b3bSNicholas Bellinger 			if (ret)
927b8d26b3bSNicholas Bellinger 				return ret;
928b8d26b3bSNicholas Bellinger 
929b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_UP;
930b8d26b3bSNicholas Bellinger 			goto post_send;
931b8d26b3bSNicholas Bellinger 		}
932b8d26b3bSNicholas Bellinger 
933b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
934b8d26b3bSNicholas Bellinger 		if (ret)
935b8d26b3bSNicholas Bellinger 			return ret;
936b8d26b3bSNicholas Bellinger 	}
937b8d26b3bSNicholas Bellinger post_send:
938b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
939b8d26b3bSNicholas Bellinger 	if (ret)
940b8d26b3bSNicholas Bellinger 		return ret;
941b8d26b3bSNicholas Bellinger 
942b8d26b3bSNicholas Bellinger 	return 0;
943b8d26b3bSNicholas Bellinger }
944b8d26b3bSNicholas Bellinger 
945b8d26b3bSNicholas Bellinger static void
946b8d26b3bSNicholas Bellinger isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
947b8d26b3bSNicholas Bellinger 		   struct isert_conn *isert_conn)
948b8d26b3bSNicholas Bellinger {
949b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
950b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
951b8d26b3bSNicholas Bellinger 	int size;
952b8d26b3bSNicholas Bellinger 
953b8d26b3bSNicholas Bellinger 	if (!login) {
954b8d26b3bSNicholas Bellinger 		pr_err("conn->conn_login is NULL\n");
955b8d26b3bSNicholas Bellinger 		dump_stack();
956b8d26b3bSNicholas Bellinger 		return;
957b8d26b3bSNicholas Bellinger 	}
958b8d26b3bSNicholas Bellinger 
959b8d26b3bSNicholas Bellinger 	if (login->first_request) {
960b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
961b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
962b8d26b3bSNicholas Bellinger 		/*
963b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
964b8d26b3bSNicholas Bellinger 		 * login request PDU.
965b8d26b3bSNicholas Bellinger 		 */
966b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
967b8d26b3bSNicholas Bellinger 		login->current_stage =
968b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
969b8d26b3bSNicholas Bellinger 			 >> 2;
970b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
971b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
972b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
973b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
974b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
975b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
976b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
977b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
978b8d26b3bSNicholas Bellinger 	}
979b8d26b3bSNicholas Bellinger 
980b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
981b8d26b3bSNicholas Bellinger 
982b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
983b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
984b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
985b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
986b8d26b3bSNicholas Bellinger 
9876faaa85fSNicholas Bellinger 	if (login->first_request) {
988b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
9896faaa85fSNicholas Bellinger 		return;
9906faaa85fSNicholas Bellinger 	}
9916faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
992b8d26b3bSNicholas Bellinger }
993b8d26b3bSNicholas Bellinger 
994b8d26b3bSNicholas Bellinger static struct iscsi_cmd
995d703ce2fSNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp)
996b8d26b3bSNicholas Bellinger {
997b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
998b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
999d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1000b8d26b3bSNicholas Bellinger 
1001d703ce2fSNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, gfp);
1002d703ce2fSNicholas Bellinger 	if (!cmd) {
1003d703ce2fSNicholas Bellinger 		pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1004b8d26b3bSNicholas Bellinger 		return NULL;
1005b8d26b3bSNicholas Bellinger 	}
1006d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1007b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1008d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1009b8d26b3bSNicholas Bellinger 
1010d703ce2fSNicholas Bellinger 	return cmd;
1011b8d26b3bSNicholas Bellinger }
1012b8d26b3bSNicholas Bellinger 
1013b8d26b3bSNicholas Bellinger static int
1014b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1015d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1016d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1017b8d26b3bSNicholas Bellinger {
1018b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1019b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1020b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1021b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1022b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1023b8d26b3bSNicholas Bellinger 
1024b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1025b8d26b3bSNicholas Bellinger 	if (rc < 0)
1026b8d26b3bSNicholas Bellinger 		return rc;
1027b8d26b3bSNicholas Bellinger 
1028b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1029b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1030b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1031b8d26b3bSNicholas Bellinger 
1032b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1033b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1034b8d26b3bSNicholas Bellinger 		return 0;
1035b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1036b8d26b3bSNicholas Bellinger 		dump_payload = true;
1037b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1038b8d26b3bSNicholas Bellinger 	}
1039b8d26b3bSNicholas Bellinger 
1040b8d26b3bSNicholas Bellinger 	if (!imm_data)
1041b8d26b3bSNicholas Bellinger 		return 0;
1042b8d26b3bSNicholas Bellinger 
1043b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1044b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1045b8d26b3bSNicholas Bellinger 
1046b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1047b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1048b8d26b3bSNicholas Bellinger 
1049b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1050b8d26b3bSNicholas Bellinger 
1051b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1052b8d26b3bSNicholas Bellinger 
1053b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1054b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1055b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1056b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1057b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1058b8d26b3bSNicholas Bellinger 	}
1059b8d26b3bSNicholas Bellinger 
1060b8d26b3bSNicholas Bellinger sequence_cmd:
1061561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1062b8d26b3bSNicholas Bellinger 
1063b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1064b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
1065b8d26b3bSNicholas Bellinger 
1066b8d26b3bSNicholas Bellinger 	return 0;
1067b8d26b3bSNicholas Bellinger }
1068b8d26b3bSNicholas Bellinger 
1069b8d26b3bSNicholas Bellinger static int
1070b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1071b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1072b8d26b3bSNicholas Bellinger {
1073b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1074b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1075b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1076b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1077b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1078b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1079b8d26b3bSNicholas Bellinger 
1080b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1081b8d26b3bSNicholas Bellinger 	if (rc < 0)
1082b8d26b3bSNicholas Bellinger 		return rc;
1083b8d26b3bSNicholas Bellinger 	else if (!cmd)
1084b8d26b3bSNicholas Bellinger 		return 0;
1085b8d26b3bSNicholas Bellinger 	/*
1086b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1087b8d26b3bSNicholas Bellinger 	 */
1088b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
1089b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
1090b8d26b3bSNicholas Bellinger 		dump_stack();
1091b8d26b3bSNicholas Bellinger 		return -1;
1092b8d26b3bSNicholas Bellinger 	}
1093b8d26b3bSNicholas Bellinger 
1094b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1095b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1096b8d26b3bSNicholas Bellinger 
1097b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1098b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1099b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1100b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1101b8d26b3bSNicholas Bellinger 	/*
1102b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1103b8d26b3bSNicholas Bellinger 	 */
1104b8d26b3bSNicholas Bellinger 	if (page_off) {
1105b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1106b8d26b3bSNicholas Bellinger 		dump_stack();
1107b8d26b3bSNicholas Bellinger 		return -1;
1108b8d26b3bSNicholas Bellinger 	}
1109b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1110b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1111b8d26b3bSNicholas Bellinger 
1112b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1113b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1114b8d26b3bSNicholas Bellinger 
1115b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1116b8d26b3bSNicholas Bellinger 	if (rc < 0)
1117b8d26b3bSNicholas Bellinger 		return rc;
1118b8d26b3bSNicholas Bellinger 
1119b8d26b3bSNicholas Bellinger 	return 0;
1120b8d26b3bSNicholas Bellinger }
1121b8d26b3bSNicholas Bellinger 
1122b8d26b3bSNicholas Bellinger static int
1123778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1124d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1125d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1126778de368SNicholas Bellinger {
1127778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1128778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1129778de368SNicholas Bellinger 	int rc;
1130778de368SNicholas Bellinger 
1131778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1132778de368SNicholas Bellinger 	if (rc < 0)
1133778de368SNicholas Bellinger 		return rc;
1134778de368SNicholas Bellinger 	/*
1135778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1136778de368SNicholas Bellinger 	 */
1137778de368SNicholas Bellinger 
1138778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1139778de368SNicholas Bellinger }
1140778de368SNicholas Bellinger 
1141778de368SNicholas Bellinger static int
1142adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1143d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1144d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1145adb54c29SNicholas Bellinger {
1146adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1147adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1148adb54c29SNicholas Bellinger 	int rc;
1149adb54c29SNicholas Bellinger 	unsigned char *text_in;
1150adb54c29SNicholas Bellinger 
1151adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1152adb54c29SNicholas Bellinger 	if (rc < 0)
1153adb54c29SNicholas Bellinger 		return rc;
1154adb54c29SNicholas Bellinger 
1155adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1156adb54c29SNicholas Bellinger 	if (!text_in) {
1157adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1158adb54c29SNicholas Bellinger 		       payload_length);
1159adb54c29SNicholas Bellinger 		return -ENOMEM;
1160adb54c29SNicholas Bellinger 	}
1161adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1162adb54c29SNicholas Bellinger 
1163adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1164adb54c29SNicholas Bellinger 
1165adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1166adb54c29SNicholas Bellinger }
1167adb54c29SNicholas Bellinger 
1168adb54c29SNicholas Bellinger static int
1169b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1170b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1171b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1172b8d26b3bSNicholas Bellinger {
1173b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1174b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1175ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1176b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1177b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1178b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1179b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1180b8d26b3bSNicholas Bellinger 
1181ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1182ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1183ca40d24eSNicholas Bellinger 		pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1184ca40d24eSNicholas Bellinger 		       " ignoring\n", opcode);
1185ca40d24eSNicholas Bellinger 		return 0;
1186ca40d24eSNicholas Bellinger 	}
1187ca40d24eSNicholas Bellinger 
1188b8d26b3bSNicholas Bellinger 	switch (opcode) {
1189b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1190d703ce2fSNicholas Bellinger 		cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1191b8d26b3bSNicholas Bellinger 		if (!cmd)
1192b8d26b3bSNicholas Bellinger 			break;
1193b8d26b3bSNicholas Bellinger 
1194d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1195b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1196b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1197b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1198b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1199b8d26b3bSNicholas Bellinger 
1200d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1201b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1202b8d26b3bSNicholas Bellinger 		break;
1203b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1204d703ce2fSNicholas Bellinger 		cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1205b8d26b3bSNicholas Bellinger 		if (!cmd)
1206b8d26b3bSNicholas Bellinger 			break;
1207b8d26b3bSNicholas Bellinger 
1208d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1209d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1210778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1211b8d26b3bSNicholas Bellinger 		break;
1212b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1213b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1214b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1215b8d26b3bSNicholas Bellinger 		break;
1216b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1217d703ce2fSNicholas Bellinger 		cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1218b8d26b3bSNicholas Bellinger 		if (!cmd)
1219b8d26b3bSNicholas Bellinger 			break;
1220b8d26b3bSNicholas Bellinger 
1221b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1222b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1223b8d26b3bSNicholas Bellinger 		break;
1224b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1225d703ce2fSNicholas Bellinger 		cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1226b8d26b3bSNicholas Bellinger 		if (!cmd)
1227b8d26b3bSNicholas Bellinger 			break;
1228b8d26b3bSNicholas Bellinger 
1229b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1230b8d26b3bSNicholas Bellinger 		if (ret > 0)
1231b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1232b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1233b8d26b3bSNicholas Bellinger 						    HZ);
1234b8d26b3bSNicholas Bellinger 		break;
1235adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1236d703ce2fSNicholas Bellinger 		cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1237adb54c29SNicholas Bellinger 		if (!cmd)
1238adb54c29SNicholas Bellinger 			break;
1239adb54c29SNicholas Bellinger 
1240d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1241d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1242adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1243adb54c29SNicholas Bellinger 		break;
1244b8d26b3bSNicholas Bellinger 	default:
1245b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1246b8d26b3bSNicholas Bellinger 		dump_stack();
1247b8d26b3bSNicholas Bellinger 		break;
1248b8d26b3bSNicholas Bellinger 	}
1249b8d26b3bSNicholas Bellinger 
1250b8d26b3bSNicholas Bellinger 	return ret;
1251b8d26b3bSNicholas Bellinger }
1252b8d26b3bSNicholas Bellinger 
1253b8d26b3bSNicholas Bellinger static void
1254b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1255b8d26b3bSNicholas Bellinger {
1256b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1257b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1258b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1259b8d26b3bSNicholas Bellinger 	int rc;
1260b8d26b3bSNicholas Bellinger 
1261b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1262b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1263b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1264b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1265b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1266b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1267b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1268b8d26b3bSNicholas Bellinger 		}
1269b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1270b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1271b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1272b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1273b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1274b8d26b3bSNicholas Bellinger 		}
1275b8d26b3bSNicholas Bellinger 
1276b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1277b8d26b3bSNicholas Bellinger 		break;
1278b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1279b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1280b8d26b3bSNicholas Bellinger 		break;
1281b8d26b3bSNicholas Bellinger 	default:
1282b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1283b8d26b3bSNicholas Bellinger 		break;
1284b8d26b3bSNicholas Bellinger 	}
1285b8d26b3bSNicholas Bellinger 
1286b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1287b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1288b8d26b3bSNicholas Bellinger }
1289b8d26b3bSNicholas Bellinger 
1290b8d26b3bSNicholas Bellinger static void
1291b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1292b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1293b8d26b3bSNicholas Bellinger {
1294b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1295b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1296b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1297b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1298b8d26b3bSNicholas Bellinger 
1299b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1300b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1301b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1302b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1303b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1304b8d26b3bSNicholas Bellinger 	} else {
1305b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1306b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1307b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1308b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1309b8d26b3bSNicholas Bellinger 	}
1310b8d26b3bSNicholas Bellinger 
1311b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1312b8d26b3bSNicholas Bellinger 
1313b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1314b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1315b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1316b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1317b8d26b3bSNicholas Bellinger 
1318b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1319b8d26b3bSNicholas Bellinger 		isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1320b8d26b3bSNicholas Bellinger 				   isert_conn);
1321b8d26b3bSNicholas Bellinger 	else
1322b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
1323b8d26b3bSNicholas Bellinger 
1324b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1325b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1326b8d26b3bSNicholas Bellinger 
1327b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1328b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1329b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1330b8d26b3bSNicholas Bellinger 
1331b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1332b8d26b3bSNicholas Bellinger 		return;
1333b8d26b3bSNicholas Bellinger 
1334b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1335b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1336b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1337b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1338b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1339b8d26b3bSNicholas Bellinger 		if (err) {
1340b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1341b8d26b3bSNicholas Bellinger 			       count, err);
1342b8d26b3bSNicholas Bellinger 		}
1343b8d26b3bSNicholas Bellinger 	}
1344b8d26b3bSNicholas Bellinger }
1345b8d26b3bSNicholas Bellinger 
1346b8d26b3bSNicholas Bellinger static void
1347b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1348b8d26b3bSNicholas Bellinger {
1349b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1350b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1351b8d26b3bSNicholas Bellinger 
135290ecc6e2SVu Pham 	pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1353b8d26b3bSNicholas Bellinger 	if (wr->sge) {
135490ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
135590ecc6e2SVu Pham 		ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
135690ecc6e2SVu Pham 				(wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
135790ecc6e2SVu Pham 				DMA_TO_DEVICE : DMA_FROM_DEVICE);
1358b8d26b3bSNicholas Bellinger 		wr->sge = NULL;
1359b8d26b3bSNicholas Bellinger 	}
1360b8d26b3bSNicholas Bellinger 
136190ecc6e2SVu Pham 	if (wr->send_wr) {
136290ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1363b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1364b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
136590ecc6e2SVu Pham 	}
1366b8d26b3bSNicholas Bellinger 
136790ecc6e2SVu Pham 	if (wr->ib_sge) {
136890ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
136990ecc6e2SVu Pham 		kfree(wr->ib_sge);
137090ecc6e2SVu Pham 		wr->ib_sge = NULL;
137190ecc6e2SVu Pham 	}
1372b8d26b3bSNicholas Bellinger }
1373b8d26b3bSNicholas Bellinger 
1374b8d26b3bSNicholas Bellinger static void
137559464ef4SVu Pham isert_unreg_rdma_frwr(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
137659464ef4SVu Pham {
137759464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
137859464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
137959464ef4SVu Pham 	LIST_HEAD(unmap_list);
138059464ef4SVu Pham 
138159464ef4SVu Pham 	pr_debug("unreg_frwr_cmd: %p\n", isert_cmd);
138259464ef4SVu Pham 
138359464ef4SVu Pham 	if (wr->fr_desc) {
138459464ef4SVu Pham 		pr_debug("unreg_frwr_cmd: %p free fr_desc %p\n",
138559464ef4SVu Pham 			 isert_cmd, wr->fr_desc);
138659464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
138759464ef4SVu Pham 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_frwr_pool);
138859464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
138959464ef4SVu Pham 		wr->fr_desc = NULL;
139059464ef4SVu Pham 	}
139159464ef4SVu Pham 
139259464ef4SVu Pham 	if (wr->sge) {
139359464ef4SVu Pham 		pr_debug("unreg_frwr_cmd: %p unmap_sg op\n", isert_cmd);
139459464ef4SVu Pham 		ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
139559464ef4SVu Pham 				(wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
139659464ef4SVu Pham 				DMA_TO_DEVICE : DMA_FROM_DEVICE);
139759464ef4SVu Pham 		wr->sge = NULL;
139859464ef4SVu Pham 	}
139959464ef4SVu Pham 
140059464ef4SVu Pham 	wr->ib_sge = NULL;
140159464ef4SVu Pham 	wr->send_wr = NULL;
140259464ef4SVu Pham }
140359464ef4SVu Pham 
140459464ef4SVu Pham static void
1405b8d26b3bSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd)
1406b8d26b3bSNicholas Bellinger {
1407d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1408b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1409186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1410d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1411b8d26b3bSNicholas Bellinger 
1412b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1413b8d26b3bSNicholas Bellinger 
1414b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1415b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1416b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1417b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
1418b8d26b3bSNicholas Bellinger 			list_del(&cmd->i_conn_node);
1419b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1420b8d26b3bSNicholas Bellinger 
1421b8d26b3bSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE)
1422b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
1423b8d26b3bSNicholas Bellinger 
1424d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1425186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1426186a9647SNicholas Bellinger 		break;
1427b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1428186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1429186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
1430186a9647SNicholas Bellinger 			list_del(&cmd->i_conn_node);
1431186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1432186a9647SNicholas Bellinger 
1433b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1434b8d26b3bSNicholas Bellinger 		break;
1435b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1436b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1437adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1438b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1439b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
1440b8d26b3bSNicholas Bellinger 			list_del(&cmd->i_conn_node);
1441b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1442b8d26b3bSNicholas Bellinger 
1443b8d26b3bSNicholas Bellinger 		/*
1444b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1445b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1446b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1447b8d26b3bSNicholas Bellinger 		 */
1448b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
14493df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
14503df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
14513df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1452b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1453b8d26b3bSNicholas Bellinger 			break;
1454b8d26b3bSNicholas Bellinger 		}
1455b8d26b3bSNicholas Bellinger 		/*
1456b8d26b3bSNicholas Bellinger 		 * Fall-through
1457b8d26b3bSNicholas Bellinger 		 */
1458b8d26b3bSNicholas Bellinger 	default:
1459d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1460b8d26b3bSNicholas Bellinger 		break;
1461b8d26b3bSNicholas Bellinger 	}
1462b8d26b3bSNicholas Bellinger }
1463b8d26b3bSNicholas Bellinger 
1464b8d26b3bSNicholas Bellinger static void
1465b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1466b8d26b3bSNicholas Bellinger {
1467b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1468b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1469b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1470b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1471b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1472b8d26b3bSNicholas Bellinger 	}
1473b8d26b3bSNicholas Bellinger }
1474b8d26b3bSNicholas Bellinger 
1475b8d26b3bSNicholas Bellinger static void
1476b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1477b8d26b3bSNicholas Bellinger 		     struct ib_device *ib_dev)
1478b8d26b3bSNicholas Bellinger {
1479dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1480dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1481dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1482dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1483dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1484b8d26b3bSNicholas Bellinger 	}
1485b8d26b3bSNicholas Bellinger 
1486b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
1487b8d26b3bSNicholas Bellinger 	isert_put_cmd(isert_cmd);
1488b8d26b3bSNicholas Bellinger }
1489b8d26b3bSNicholas Bellinger 
1490b8d26b3bSNicholas Bellinger static void
1491b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1492b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1493b8d26b3bSNicholas Bellinger {
1494b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1495d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1496b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
149790ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1498d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1499b8d26b3bSNicholas Bellinger 
1500b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1501d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
150290ecc6e2SVu Pham 	cmd->write_data_done = wr->cur_rdma_length;
1503b8d26b3bSNicholas Bellinger 
150490ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1505b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1506b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1507b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1508b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1509b8d26b3bSNicholas Bellinger 
1510b8d26b3bSNicholas Bellinger 	target_execute_cmd(se_cmd);
1511b8d26b3bSNicholas Bellinger }
1512b8d26b3bSNicholas Bellinger 
1513b8d26b3bSNicholas Bellinger static void
1514b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1515b8d26b3bSNicholas Bellinger {
1516b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1517b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1518b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1519b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1520d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1521b8d26b3bSNicholas Bellinger 
1522b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1523b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1524b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1525b8d26b3bSNicholas Bellinger 
1526b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1527b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1528b8d26b3bSNicholas Bellinger 
1529b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1530b8d26b3bSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1531b8d26b3bSNicholas Bellinger 		break;
1532b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1533b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1534b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1535b8d26b3bSNicholas Bellinger 
1536b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1537b8d26b3bSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
15383df8f68aSNicholas Bellinger 		break;
1539b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1540b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1541b8d26b3bSNicholas Bellinger 		/*
1542b8d26b3bSNicholas Bellinger 		 * Call atomic_dec(&isert_conn->post_send_buf_count)
1543b8d26b3bSNicholas Bellinger 		 * from isert_free_conn()
1544b8d26b3bSNicholas Bellinger 		 */
1545b8d26b3bSNicholas Bellinger 		isert_conn->logout_posted = true;
1546b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1547b8d26b3bSNicholas Bellinger 		break;
1548adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1549adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1550adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1551adb54c29SNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1552adb54c29SNicholas Bellinger 		break;
1553b8d26b3bSNicholas Bellinger 	default:
1554b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1555b8d26b3bSNicholas Bellinger 		dump_stack();
1556b8d26b3bSNicholas Bellinger 		break;
1557b8d26b3bSNicholas Bellinger 	}
1558b8d26b3bSNicholas Bellinger }
1559b8d26b3bSNicholas Bellinger 
1560b8d26b3bSNicholas Bellinger static void
1561b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1562b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1563b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1564b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1565b8d26b3bSNicholas Bellinger {
1566d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1567b8d26b3bSNicholas Bellinger 
1568b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
15693df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1570adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1571adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1572b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1573b8d26b3bSNicholas Bellinger 
1574b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1575b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1576b8d26b3bSNicholas Bellinger 		return;
1577b8d26b3bSNicholas Bellinger 	}
1578b8d26b3bSNicholas Bellinger 	atomic_dec(&isert_conn->post_send_buf_count);
1579b8d26b3bSNicholas Bellinger 
1580b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
1581b8d26b3bSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev);
1582b8d26b3bSNicholas Bellinger }
1583b8d26b3bSNicholas Bellinger 
1584b8d26b3bSNicholas Bellinger static void
1585b8d26b3bSNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
1586b8d26b3bSNicholas Bellinger 		      struct isert_conn *isert_conn)
1587b8d26b3bSNicholas Bellinger {
1588b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1589b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1590b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1591b8d26b3bSNicholas Bellinger 
1592b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1593b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1594b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1595b8d26b3bSNicholas Bellinger 		return;
1596b8d26b3bSNicholas Bellinger 	}
1597b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1598b8d26b3bSNicholas Bellinger 
1599b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1600b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
1601b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1602b8d26b3bSNicholas Bellinger 		dump_stack();
1603b8d26b3bSNicholas Bellinger 		break;
1604b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1605b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1606b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1607b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1608b8d26b3bSNicholas Bellinger 		break;
1609b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1610b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1611b8d26b3bSNicholas Bellinger 		dump_stack();
1612b8d26b3bSNicholas Bellinger 		break;
1613b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1614b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1615b8d26b3bSNicholas Bellinger 
1616b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1617b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1618b8d26b3bSNicholas Bellinger 		break;
1619b8d26b3bSNicholas Bellinger 	default:
1620b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1621b8d26b3bSNicholas Bellinger 		dump_stack();
1622b8d26b3bSNicholas Bellinger 		break;
1623b8d26b3bSNicholas Bellinger 	}
1624b8d26b3bSNicholas Bellinger }
1625b8d26b3bSNicholas Bellinger 
1626b8d26b3bSNicholas Bellinger static void
1627b8d26b3bSNicholas Bellinger isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1628b8d26b3bSNicholas Bellinger {
1629b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1630b8d26b3bSNicholas Bellinger 
1631b8d26b3bSNicholas Bellinger 	if (tx_desc) {
1632b8d26b3bSNicholas Bellinger 		struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1633b8d26b3bSNicholas Bellinger 
1634b8d26b3bSNicholas Bellinger 		if (!isert_cmd)
1635b8d26b3bSNicholas Bellinger 			isert_unmap_tx_desc(tx_desc, ib_dev);
1636b8d26b3bSNicholas Bellinger 		else
1637b8d26b3bSNicholas Bellinger 			isert_completion_put(tx_desc, isert_cmd, ib_dev);
1638b8d26b3bSNicholas Bellinger 	}
1639b8d26b3bSNicholas Bellinger 
1640b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
1641b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
1642b8d26b3bSNicholas Bellinger 		pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1643b8d26b3bSNicholas Bellinger 		pr_debug("Calling wake_up from isert_cq_comp_err\n");
1644b8d26b3bSNicholas Bellinger 
1645b2cb9649SNicholas Bellinger 		mutex_lock(&isert_conn->conn_mutex);
1646b2cb9649SNicholas Bellinger 		if (isert_conn->state != ISER_CONN_DOWN)
1647b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_TERMINATING;
1648b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
1649b2cb9649SNicholas Bellinger 
1650b8d26b3bSNicholas Bellinger 		wake_up(&isert_conn->conn_wait_comp_err);
1651b8d26b3bSNicholas Bellinger 	}
1652b8d26b3bSNicholas Bellinger }
1653b8d26b3bSNicholas Bellinger 
1654b8d26b3bSNicholas Bellinger static void
1655b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
1656b8d26b3bSNicholas Bellinger {
1657b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
1658b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
1659b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
1660b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
1661b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1662b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
1663b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
1664b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
1665b8d26b3bSNicholas Bellinger 
1666b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1667b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1668b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
1669b8d26b3bSNicholas Bellinger 
1670b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
1671b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
1672b8d26b3bSNicholas Bellinger 		} else {
1673b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1674b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
1675c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
1676b8d26b3bSNicholas Bellinger 			atomic_dec(&isert_conn->post_send_buf_count);
1677b8d26b3bSNicholas Bellinger 			isert_cq_comp_err(tx_desc, isert_conn);
1678b8d26b3bSNicholas Bellinger 		}
1679b8d26b3bSNicholas Bellinger 	}
1680b8d26b3bSNicholas Bellinger 
1681b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1682b8d26b3bSNicholas Bellinger }
1683b8d26b3bSNicholas Bellinger 
1684b8d26b3bSNicholas Bellinger static void
1685b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
1686b8d26b3bSNicholas Bellinger {
1687b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1688b8d26b3bSNicholas Bellinger 
1689b8d26b3bSNicholas Bellinger 	INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
1690b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1691b8d26b3bSNicholas Bellinger }
1692b8d26b3bSNicholas Bellinger 
1693b8d26b3bSNicholas Bellinger static void
1694b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
1695b8d26b3bSNicholas Bellinger {
1696b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
1697b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
1698b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
1699b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
1700b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1701b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
1702b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
1703b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
1704b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
1705b8d26b3bSNicholas Bellinger 
1706b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1707b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1708b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
1709b8d26b3bSNicholas Bellinger 
1710b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
1711b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
1712b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
1713b8d26b3bSNicholas Bellinger 		} else {
1714b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1715c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
1716b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
1717c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
1718c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
1719c5a2adbfSNicholas Bellinger 			}
1720b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
1721b8d26b3bSNicholas Bellinger 			isert_cq_comp_err(NULL, isert_conn);
1722b8d26b3bSNicholas Bellinger 		}
1723b8d26b3bSNicholas Bellinger 	}
1724b8d26b3bSNicholas Bellinger 
1725b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1726b8d26b3bSNicholas Bellinger }
1727b8d26b3bSNicholas Bellinger 
1728b8d26b3bSNicholas Bellinger static void
1729b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
1730b8d26b3bSNicholas Bellinger {
1731b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1732b8d26b3bSNicholas Bellinger 
1733b8d26b3bSNicholas Bellinger 	INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
1734b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1735b8d26b3bSNicholas Bellinger }
1736b8d26b3bSNicholas Bellinger 
1737b8d26b3bSNicholas Bellinger static int
1738b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1739b8d26b3bSNicholas Bellinger {
1740b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
1741b8d26b3bSNicholas Bellinger 	int ret;
1742b8d26b3bSNicholas Bellinger 
1743b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
1744b8d26b3bSNicholas Bellinger 
1745b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1746b8d26b3bSNicholas Bellinger 			   &wr_failed);
1747b8d26b3bSNicholas Bellinger 	if (ret) {
1748b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
1749b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1750b8d26b3bSNicholas Bellinger 		return ret;
1751b8d26b3bSNicholas Bellinger 	}
1752b8d26b3bSNicholas Bellinger 	return ret;
1753b8d26b3bSNicholas Bellinger }
1754b8d26b3bSNicholas Bellinger 
1755b8d26b3bSNicholas Bellinger static int
1756b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1757b8d26b3bSNicholas Bellinger {
1758d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1759b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1760b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1761b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1762b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1763b8d26b3bSNicholas Bellinger 
1764b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1765b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1766b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1767b8d26b3bSNicholas Bellinger 	/*
1768b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1769b8d26b3bSNicholas Bellinger 	 */
1770b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1771b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1772b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1773b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1774b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1775dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1776b8d26b3bSNicholas Bellinger 
1777b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1778b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1779b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1780b8d26b3bSNicholas Bellinger 
1781b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1782b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1783dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1784b8d26b3bSNicholas Bellinger 
1785dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1786dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1787b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1788b8d26b3bSNicholas Bellinger 
1789dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1790dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1791dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
1792b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1793b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1794b8d26b3bSNicholas Bellinger 	}
1795b8d26b3bSNicholas Bellinger 
1796b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1797b8d26b3bSNicholas Bellinger 
1798b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1799b8d26b3bSNicholas Bellinger 
1800b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1801b8d26b3bSNicholas Bellinger }
1802b8d26b3bSNicholas Bellinger 
1803b8d26b3bSNicholas Bellinger static int
1804b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1805b8d26b3bSNicholas Bellinger 		bool nopout_response)
1806b8d26b3bSNicholas Bellinger {
1807d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1808b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1809b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1810b8d26b3bSNicholas Bellinger 
1811b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1812b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1813b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1814b8d26b3bSNicholas Bellinger 			       nopout_response);
1815b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1816b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1817b8d26b3bSNicholas Bellinger 
18188b513d0cSMasanari Iida 	pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1819b8d26b3bSNicholas Bellinger 
1820b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1821b8d26b3bSNicholas Bellinger }
1822b8d26b3bSNicholas Bellinger 
1823b8d26b3bSNicholas Bellinger static int
1824b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1825b8d26b3bSNicholas Bellinger {
1826d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1827b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1828b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1829b8d26b3bSNicholas Bellinger 
1830b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1831b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1832b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1833b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1834b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1835b8d26b3bSNicholas Bellinger 
1836b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1837b8d26b3bSNicholas Bellinger 
1838b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1839b8d26b3bSNicholas Bellinger }
1840b8d26b3bSNicholas Bellinger 
1841b8d26b3bSNicholas Bellinger static int
1842b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1843b8d26b3bSNicholas Bellinger {
1844d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1845b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1846b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1847b8d26b3bSNicholas Bellinger 
1848b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1849b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1850b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1851b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1852b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1853b8d26b3bSNicholas Bellinger 
1854b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1855b8d26b3bSNicholas Bellinger 
1856b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1857b8d26b3bSNicholas Bellinger }
1858b8d26b3bSNicholas Bellinger 
1859b8d26b3bSNicholas Bellinger static int
1860b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1861b8d26b3bSNicholas Bellinger {
1862d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1863b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1864b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
18653df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
18663df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
18673df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
18683df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1869b8d26b3bSNicholas Bellinger 
1870b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
18713df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
1872b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
18733df8f68aSNicholas Bellinger 
18743df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
1875dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
18763df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
18773df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
1878dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1879dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
18803df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
18813df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
18823df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
18833df8f68aSNicholas Bellinger 
1884b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1885b8d26b3bSNicholas Bellinger 
1886b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1887b8d26b3bSNicholas Bellinger 
1888b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1889b8d26b3bSNicholas Bellinger }
1890b8d26b3bSNicholas Bellinger 
1891b8d26b3bSNicholas Bellinger static int
1892adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1893adb54c29SNicholas Bellinger {
1894d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1895adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1896adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1897adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
1898adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1899adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
1900adb54c29SNicholas Bellinger 	int rc;
1901adb54c29SNicholas Bellinger 
1902adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1903adb54c29SNicholas Bellinger 	rc = iscsit_build_text_rsp(cmd, conn, hdr);
1904adb54c29SNicholas Bellinger 	if (rc < 0)
1905adb54c29SNicholas Bellinger 		return rc;
1906adb54c29SNicholas Bellinger 
1907adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
1908adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1909adb54c29SNicholas Bellinger 
1910adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
1911adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1912adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1913adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
1914adb54c29SNicholas Bellinger 
1915adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1916adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1917adb54c29SNicholas Bellinger 
1918adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
1919adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1920adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
1921adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1922adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1923adb54c29SNicholas Bellinger 	}
1924adb54c29SNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1925adb54c29SNicholas Bellinger 
1926adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1927adb54c29SNicholas Bellinger 
1928adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1929adb54c29SNicholas Bellinger }
1930adb54c29SNicholas Bellinger 
1931adb54c29SNicholas Bellinger static int
1932b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1933b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1934b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
1935b8d26b3bSNicholas Bellinger {
1936d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1937b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
1938b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1939b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
1940b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
1941b8d26b3bSNicholas Bellinger 
1942b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
1943b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1944b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1945b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
1946b8d26b3bSNicholas Bellinger 
1947b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
1948b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
1949b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1950b8d26b3bSNicholas Bellinger 	/*
1951b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1952b8d26b3bSNicholas Bellinger 	 */
1953b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1954b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1955b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
1956b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
1957b8d26b3bSNicholas Bellinger 
1958b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1959b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
1960b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
1961b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
1962b8d26b3bSNicholas Bellinger 
196390ecc6e2SVu Pham 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
196490ecc6e2SVu Pham 			 ib_sge->addr, ib_sge->length, ib_sge->lkey);
1965b8d26b3bSNicholas Bellinger 		page_off = 0;
1966b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
1967b8d26b3bSNicholas Bellinger 		ib_sge++;
1968b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
1969b8d26b3bSNicholas Bellinger 	}
1970b8d26b3bSNicholas Bellinger 
1971b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
1972b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
1973b8d26b3bSNicholas Bellinger 
1974b8d26b3bSNicholas Bellinger 	return sg_nents;
1975b8d26b3bSNicholas Bellinger }
1976b8d26b3bSNicholas Bellinger 
1977b8d26b3bSNicholas Bellinger static int
197890ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
197990ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
1980b8d26b3bSNicholas Bellinger {
1981b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
1982d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1983b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1984b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
198590ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
1986b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
198790ecc6e2SVu Pham 	struct scatterlist *sg_start;
198890ecc6e2SVu Pham 	u32 sg_off = 0, sg_nents;
198990ecc6e2SVu Pham 	u32 offset = 0, data_len, data_left, rdma_write_max, va_offset = 0;
199090ecc6e2SVu Pham 	int ret = 0, count, i, ib_sge_cnt;
1991b8d26b3bSNicholas Bellinger 
199290ecc6e2SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
199390ecc6e2SVu Pham 		data_left = se_cmd->data_length;
199490ecc6e2SVu Pham 		iscsit_increment_maxcmdsn(cmd, conn->sess);
199590ecc6e2SVu Pham 		cmd->stat_sn = conn->stat_sn++;
199690ecc6e2SVu Pham 	} else {
199790ecc6e2SVu Pham 		sg_off = cmd->write_data_done / PAGE_SIZE;
199890ecc6e2SVu Pham 		data_left = se_cmd->data_length - cmd->write_data_done;
199990ecc6e2SVu Pham 		offset = cmd->write_data_done;
200090ecc6e2SVu Pham 		isert_cmd->tx_desc.isert_cmd = isert_cmd;
200190ecc6e2SVu Pham 	}
2002b8d26b3bSNicholas Bellinger 
200390ecc6e2SVu Pham 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
200490ecc6e2SVu Pham 	sg_nents = se_cmd->t_data_nents - sg_off;
2005b8d26b3bSNicholas Bellinger 
200690ecc6e2SVu Pham 	count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
200790ecc6e2SVu Pham 			      (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
200890ecc6e2SVu Pham 			      DMA_TO_DEVICE : DMA_FROM_DEVICE);
2009b8d26b3bSNicholas Bellinger 	if (unlikely(!count)) {
201090ecc6e2SVu Pham 		pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
2011b8d26b3bSNicholas Bellinger 		return -EINVAL;
2012b8d26b3bSNicholas Bellinger 	}
201390ecc6e2SVu Pham 	wr->sge = sg_start;
2014b8d26b3bSNicholas Bellinger 	wr->num_sge = sg_nents;
201590ecc6e2SVu Pham 	wr->cur_rdma_length = data_left;
201690ecc6e2SVu Pham 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
201790ecc6e2SVu Pham 		 isert_cmd, count, sg_start, sg_nents, data_left);
2018b8d26b3bSNicholas Bellinger 
2019b8d26b3bSNicholas Bellinger 	ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
2020b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
202190ecc6e2SVu Pham 		pr_warn("Unable to allocate ib_sge\n");
2022b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2023b8d26b3bSNicholas Bellinger 		goto unmap_sg;
2024b8d26b3bSNicholas Bellinger 	}
202590ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2026b8d26b3bSNicholas Bellinger 
2027b8d26b3bSNicholas Bellinger 	wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
2028b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2029b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2030b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
203190ecc6e2SVu Pham 		pr_debug("Unable to allocate wr->send_wr\n");
2032b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2033b8d26b3bSNicholas Bellinger 		goto unmap_sg;
2034b8d26b3bSNicholas Bellinger 	}
2035b8d26b3bSNicholas Bellinger 
2036b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2037b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2038b8d26b3bSNicholas Bellinger 
2039b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2040b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2041b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2042b8d26b3bSNicholas Bellinger 
2043b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
204490ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
204590ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2046b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2047b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
204890ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
204990ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
205090ecc6e2SVu Pham 			else
205190ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
205290ecc6e2SVu Pham 		} else {
205390ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
205490ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
205590ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
205690ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
205790ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
205890ecc6e2SVu Pham 			else
205990ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
206090ecc6e2SVu Pham 		}
2061b8d26b3bSNicholas Bellinger 
2062b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2063b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2064b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2065b8d26b3bSNicholas Bellinger 
2066b8d26b3bSNicholas Bellinger 		offset += data_len;
206790ecc6e2SVu Pham 		va_offset += data_len;
2068b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2069b8d26b3bSNicholas Bellinger 	}
207090ecc6e2SVu Pham 
207190ecc6e2SVu Pham 	return 0;
207290ecc6e2SVu Pham unmap_sg:
207390ecc6e2SVu Pham 	ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
207490ecc6e2SVu Pham 			(wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
207590ecc6e2SVu Pham 			DMA_TO_DEVICE : DMA_FROM_DEVICE);
207690ecc6e2SVu Pham 	return ret;
207790ecc6e2SVu Pham }
207890ecc6e2SVu Pham 
207990ecc6e2SVu Pham static int
208059464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
208159464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
208259464ef4SVu Pham {
208359464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
208459464ef4SVu Pham 	struct scatterlist *tmp_sg;
208559464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
208659464ef4SVu Pham 
208759464ef4SVu Pham 	n_pages = 0;
208859464ef4SVu Pham 	new_chunk = 1;
208959464ef4SVu Pham 	last_ent = sg_nents - 1;
209059464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
209159464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
209259464ef4SVu Pham 		if (new_chunk)
209359464ef4SVu Pham 			chunk_start = start_addr;
209459464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
209559464ef4SVu Pham 
209659464ef4SVu Pham 		pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
209759464ef4SVu Pham 			 i, (unsigned long long)tmp_sg->dma_address,
209859464ef4SVu Pham 			 tmp_sg->length);
209959464ef4SVu Pham 
210059464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
210159464ef4SVu Pham 			new_chunk = 0;
210259464ef4SVu Pham 			continue;
210359464ef4SVu Pham 		}
210459464ef4SVu Pham 		new_chunk = 1;
210559464ef4SVu Pham 
210659464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
210759464ef4SVu Pham 		do {
210859464ef4SVu Pham 			fr_pl[n_pages++] = page;
210959464ef4SVu Pham 			pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
211059464ef4SVu Pham 				 n_pages - 1, page);
211159464ef4SVu Pham 			page += PAGE_SIZE;
211259464ef4SVu Pham 		} while (page < end_addr);
211359464ef4SVu Pham 	}
211459464ef4SVu Pham 
211559464ef4SVu Pham 	return n_pages;
211659464ef4SVu Pham }
211759464ef4SVu Pham 
211859464ef4SVu Pham static int
211959464ef4SVu Pham isert_fast_reg_mr(struct fast_reg_descriptor *fr_desc,
212059464ef4SVu Pham 		  struct isert_cmd *isert_cmd, struct isert_conn *isert_conn,
212159464ef4SVu Pham 		  struct ib_sge *ib_sge, u32 offset, unsigned int data_len)
212259464ef4SVu Pham {
212359464ef4SVu Pham 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
212459464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
212559464ef4SVu Pham 	struct scatterlist *sg_start;
212659464ef4SVu Pham 	u32 sg_off, page_off;
212759464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
212859464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
212959464ef4SVu Pham 	u8 key;
213059464ef4SVu Pham 	int ret, sg_nents, pagelist_len;
213159464ef4SVu Pham 
213259464ef4SVu Pham 	sg_off = offset / PAGE_SIZE;
213359464ef4SVu Pham 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
213459464ef4SVu Pham 	sg_nents = min_t(unsigned int, cmd->se_cmd.t_data_nents - sg_off,
213559464ef4SVu Pham 			 ISCSI_ISER_SG_TABLESIZE);
213659464ef4SVu Pham 	page_off = offset % PAGE_SIZE;
213759464ef4SVu Pham 
213859464ef4SVu Pham 	pr_debug("Cmd: %p use fr_desc %p sg_nents %d sg_off %d offset %u\n",
213959464ef4SVu Pham 		 isert_cmd, fr_desc, sg_nents, sg_off, offset);
214059464ef4SVu Pham 
214159464ef4SVu Pham 	pagelist_len = isert_map_fr_pagelist(ib_dev, sg_start, sg_nents,
214259464ef4SVu Pham 					     &fr_desc->data_frpl->page_list[0]);
214359464ef4SVu Pham 
214459464ef4SVu Pham 	if (!fr_desc->valid) {
214559464ef4SVu Pham 		memset(&inv_wr, 0, sizeof(inv_wr));
214659464ef4SVu Pham 		inv_wr.opcode = IB_WR_LOCAL_INV;
214759464ef4SVu Pham 		inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
214859464ef4SVu Pham 		wr = &inv_wr;
214959464ef4SVu Pham 		/* Bump the key */
215059464ef4SVu Pham 		key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
215159464ef4SVu Pham 		ib_update_fast_reg_key(fr_desc->data_mr, ++key);
215259464ef4SVu Pham 	}
215359464ef4SVu Pham 
215459464ef4SVu Pham 	/* Prepare FASTREG WR */
215559464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
215659464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
215759464ef4SVu Pham 	fr_wr.wr.fast_reg.iova_start =
215859464ef4SVu Pham 		fr_desc->data_frpl->page_list[0] + page_off;
215959464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
216059464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
216159464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
216259464ef4SVu Pham 	fr_wr.wr.fast_reg.length = data_len;
216359464ef4SVu Pham 	fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
216459464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
216559464ef4SVu Pham 
216659464ef4SVu Pham 	if (!wr)
216759464ef4SVu Pham 		wr = &fr_wr;
216859464ef4SVu Pham 	else
216959464ef4SVu Pham 		wr->next = &fr_wr;
217059464ef4SVu Pham 
217159464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
217259464ef4SVu Pham 	if (ret) {
217359464ef4SVu Pham 		pr_err("fast registration failed, ret:%d\n", ret);
217459464ef4SVu Pham 		return ret;
217559464ef4SVu Pham 	}
217659464ef4SVu Pham 	fr_desc->valid = false;
217759464ef4SVu Pham 
217859464ef4SVu Pham 	ib_sge->lkey = fr_desc->data_mr->lkey;
217959464ef4SVu Pham 	ib_sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
218059464ef4SVu Pham 	ib_sge->length = data_len;
218159464ef4SVu Pham 
218259464ef4SVu Pham 	pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
218359464ef4SVu Pham 		 ib_sge->addr, ib_sge->length, ib_sge->lkey);
218459464ef4SVu Pham 
218559464ef4SVu Pham 	return ret;
218659464ef4SVu Pham }
218759464ef4SVu Pham 
218859464ef4SVu Pham static int
218959464ef4SVu Pham isert_reg_rdma_frwr(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
219059464ef4SVu Pham 		    struct isert_rdma_wr *wr)
219159464ef4SVu Pham {
219259464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
219359464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
219459464ef4SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
219559464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
219659464ef4SVu Pham 	struct ib_send_wr *send_wr;
219759464ef4SVu Pham 	struct ib_sge *ib_sge;
219859464ef4SVu Pham 	struct scatterlist *sg_start;
219959464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
220059464ef4SVu Pham 	u32 sg_off = 0, sg_nents;
220159464ef4SVu Pham 	u32 offset = 0, data_len, data_left, rdma_write_max;
220259464ef4SVu Pham 	int ret = 0, count;
220359464ef4SVu Pham 	unsigned long flags;
220459464ef4SVu Pham 
220559464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
220659464ef4SVu Pham 		data_left = se_cmd->data_length;
220759464ef4SVu Pham 		iscsit_increment_maxcmdsn(cmd, conn->sess);
220859464ef4SVu Pham 		cmd->stat_sn = conn->stat_sn++;
220959464ef4SVu Pham 	} else {
221059464ef4SVu Pham 		sg_off = cmd->write_data_done / PAGE_SIZE;
221159464ef4SVu Pham 		data_left = se_cmd->data_length - cmd->write_data_done;
221259464ef4SVu Pham 		offset = cmd->write_data_done;
221359464ef4SVu Pham 		isert_cmd->tx_desc.isert_cmd = isert_cmd;
221459464ef4SVu Pham 	}
221559464ef4SVu Pham 
221659464ef4SVu Pham 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
221759464ef4SVu Pham 	sg_nents = se_cmd->t_data_nents - sg_off;
221859464ef4SVu Pham 
221959464ef4SVu Pham 	count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
222059464ef4SVu Pham 			      (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
222159464ef4SVu Pham 			      DMA_TO_DEVICE : DMA_FROM_DEVICE);
222259464ef4SVu Pham 	if (unlikely(!count)) {
222359464ef4SVu Pham 		pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
222459464ef4SVu Pham 		return -EINVAL;
222559464ef4SVu Pham 	}
222659464ef4SVu Pham 	wr->sge = sg_start;
222759464ef4SVu Pham 	wr->num_sge = sg_nents;
222859464ef4SVu Pham 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
222959464ef4SVu Pham 		 isert_cmd, count, sg_start, sg_nents, data_left);
223059464ef4SVu Pham 
223159464ef4SVu Pham 	memset(&wr->s_ib_sge, 0, sizeof(*ib_sge));
223259464ef4SVu Pham 	ib_sge = &wr->s_ib_sge;
223359464ef4SVu Pham 	wr->ib_sge = ib_sge;
223459464ef4SVu Pham 
223559464ef4SVu Pham 	wr->send_wr_num = 1;
223659464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
223759464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
223859464ef4SVu Pham 
223959464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
224059464ef4SVu Pham 	rdma_write_max = ISCSI_ISER_SG_TABLESIZE * PAGE_SIZE;
224159464ef4SVu Pham 
224259464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
224359464ef4SVu Pham 	send_wr->sg_list = ib_sge;
224459464ef4SVu Pham 	send_wr->num_sge = 1;
224559464ef4SVu Pham 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
224659464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
224759464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
224859464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
224959464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
225059464ef4SVu Pham 		send_wr->send_flags = 0;
225159464ef4SVu Pham 		send_wr->next = &isert_cmd->tx_desc.send_wr;
225259464ef4SVu Pham 	} else {
225359464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
225459464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
225559464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
225659464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
225759464ef4SVu Pham 	}
225859464ef4SVu Pham 
225959464ef4SVu Pham 	data_len = min(data_left, rdma_write_max);
226059464ef4SVu Pham 	wr->cur_rdma_length = data_len;
226159464ef4SVu Pham 
226259464ef4SVu Pham 	spin_lock_irqsave(&isert_conn->conn_lock, flags);
226359464ef4SVu Pham 	fr_desc = list_first_entry(&isert_conn->conn_frwr_pool,
226459464ef4SVu Pham 				   struct fast_reg_descriptor, list);
226559464ef4SVu Pham 	list_del(&fr_desc->list);
226659464ef4SVu Pham 	spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
226759464ef4SVu Pham 	wr->fr_desc = fr_desc;
226859464ef4SVu Pham 
226959464ef4SVu Pham 	ret = isert_fast_reg_mr(fr_desc, isert_cmd, isert_conn,
227059464ef4SVu Pham 			  ib_sge, offset, data_len);
227159464ef4SVu Pham 	if (ret) {
227259464ef4SVu Pham 		list_add_tail(&fr_desc->list, &isert_conn->conn_frwr_pool);
227359464ef4SVu Pham 		goto unmap_sg;
227459464ef4SVu Pham 	}
227559464ef4SVu Pham 
227659464ef4SVu Pham 	return 0;
227759464ef4SVu Pham 
227859464ef4SVu Pham unmap_sg:
227959464ef4SVu Pham 	ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
228059464ef4SVu Pham 			(wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
228159464ef4SVu Pham 			DMA_TO_DEVICE : DMA_FROM_DEVICE);
228259464ef4SVu Pham 	return ret;
228359464ef4SVu Pham }
228459464ef4SVu Pham 
228559464ef4SVu Pham static int
228690ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
228790ecc6e2SVu Pham {
228890ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
228959464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
229090ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
229190ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2292d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
229390ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
229490ecc6e2SVu Pham 	int rc;
229590ecc6e2SVu Pham 
229690ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
229790ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
229890ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2299d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
230090ecc6e2SVu Pham 	if (rc) {
230190ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
230290ecc6e2SVu Pham 		return rc;
230390ecc6e2SVu Pham 	}
230490ecc6e2SVu Pham 
2305b8d26b3bSNicholas Bellinger 	/*
2306b8d26b3bSNicholas Bellinger 	 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2307b8d26b3bSNicholas Bellinger 	 */
2308b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2309b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
2310b8d26b3bSNicholas Bellinger 			     &isert_cmd->tx_desc.iscsi_header);
2311b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2312b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
2313b8d26b3bSNicholas Bellinger 
2314b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
2315b8d26b3bSNicholas Bellinger 
2316b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2317b8d26b3bSNicholas Bellinger 	if (rc) {
2318b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2319b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2320b8d26b3bSNicholas Bellinger 	}
232190ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
232290ecc6e2SVu Pham 		 isert_cmd);
2323b8d26b3bSNicholas Bellinger 
232490ecc6e2SVu Pham 	return 1;
2325b8d26b3bSNicholas Bellinger }
2326b8d26b3bSNicholas Bellinger 
2327b8d26b3bSNicholas Bellinger static int
2328b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2329b8d26b3bSNicholas Bellinger {
2330b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2331d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2332b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2333b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2334d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
233590ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
233690ecc6e2SVu Pham 	int rc;
2337b8d26b3bSNicholas Bellinger 
233890ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
233990ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2340b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2341d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
234290ecc6e2SVu Pham 	if (rc) {
234390ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
234490ecc6e2SVu Pham 		return rc;
2345b8d26b3bSNicholas Bellinger 	}
2346b8d26b3bSNicholas Bellinger 
2347b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
2348b8d26b3bSNicholas Bellinger 
2349b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2350b8d26b3bSNicholas Bellinger 	if (rc) {
2351b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2352b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2353b8d26b3bSNicholas Bellinger 	}
235490ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
235590ecc6e2SVu Pham 		 isert_cmd);
2356b8d26b3bSNicholas Bellinger 
235790ecc6e2SVu Pham 	return 0;
2358b8d26b3bSNicholas Bellinger }
2359b8d26b3bSNicholas Bellinger 
2360b8d26b3bSNicholas Bellinger static int
2361b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2362b8d26b3bSNicholas Bellinger {
2363b8d26b3bSNicholas Bellinger 	int ret;
2364b8d26b3bSNicholas Bellinger 
2365b8d26b3bSNicholas Bellinger 	switch (state) {
2366b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2367b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2368b8d26b3bSNicholas Bellinger 		break;
2369b8d26b3bSNicholas Bellinger 	default:
2370b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
2371b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2372b8d26b3bSNicholas Bellinger 		break;
2373b8d26b3bSNicholas Bellinger 	}
2374b8d26b3bSNicholas Bellinger 
2375b8d26b3bSNicholas Bellinger 	return ret;
2376b8d26b3bSNicholas Bellinger }
2377b8d26b3bSNicholas Bellinger 
2378b8d26b3bSNicholas Bellinger static int
2379b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2380b8d26b3bSNicholas Bellinger {
2381b8d26b3bSNicholas Bellinger 	int ret;
2382b8d26b3bSNicholas Bellinger 
2383b8d26b3bSNicholas Bellinger 	switch (state) {
2384b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2385b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2386b8d26b3bSNicholas Bellinger 		if (!ret) {
2387b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
2388b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
2389b8d26b3bSNicholas Bellinger 		}
2390b8d26b3bSNicholas Bellinger 		break;
2391b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2392b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2393b8d26b3bSNicholas Bellinger 		break;
2394b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2395b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2396b8d26b3bSNicholas Bellinger 		break;
2397b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2398b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2399b8d26b3bSNicholas Bellinger 		break;
2400adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2401adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2402adb54c29SNicholas Bellinger 		break;
2403b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2404b8d26b3bSNicholas Bellinger 		/*
2405b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2406b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2407b8d26b3bSNicholas Bellinger 		 */
2408b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2409b8d26b3bSNicholas Bellinger 		break;
2410b8d26b3bSNicholas Bellinger 	default:
2411b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
2412b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2413b8d26b3bSNicholas Bellinger 		break;
2414b8d26b3bSNicholas Bellinger 	}
2415b8d26b3bSNicholas Bellinger 
2416b8d26b3bSNicholas Bellinger 	return ret;
2417b8d26b3bSNicholas Bellinger }
2418b8d26b3bSNicholas Bellinger 
2419b8d26b3bSNicholas Bellinger static int
2420b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
2421b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
2422b8d26b3bSNicholas Bellinger {
2423b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2424b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2425b8d26b3bSNicholas Bellinger 	struct sockaddr *sa;
2426b8d26b3bSNicholas Bellinger 	int ret;
2427b8d26b3bSNicholas Bellinger 
2428b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2429b8d26b3bSNicholas Bellinger 	if (!isert_np) {
2430b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
2431b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2432b8d26b3bSNicholas Bellinger 	}
2433b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_np->np_accept_wq);
2434b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
2435b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
2436b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
2437b8d26b3bSNicholas Bellinger 
2438b8d26b3bSNicholas Bellinger 	sa = (struct sockaddr *)ksockaddr;
2439b8d26b3bSNicholas Bellinger 	pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2440b8d26b3bSNicholas Bellinger 	/*
2441b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2442b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2443b8d26b3bSNicholas Bellinger 	 */
2444b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
2445b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
2446b8d26b3bSNicholas Bellinger 
2447b8d26b3bSNicholas Bellinger 	isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2448b8d26b3bSNicholas Bellinger 				IB_QPT_RC);
2449b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2450b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2451b8d26b3bSNicholas Bellinger 		       PTR_ERR(isert_lid));
2452b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2453b8d26b3bSNicholas Bellinger 		goto out;
2454b8d26b3bSNicholas Bellinger 	}
2455b8d26b3bSNicholas Bellinger 
2456b8d26b3bSNicholas Bellinger 	ret = rdma_bind_addr(isert_lid, sa);
2457b8d26b3bSNicholas Bellinger 	if (ret) {
2458b8d26b3bSNicholas Bellinger 		pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2459b8d26b3bSNicholas Bellinger 		goto out_lid;
2460b8d26b3bSNicholas Bellinger 	}
2461b8d26b3bSNicholas Bellinger 
2462b8d26b3bSNicholas Bellinger 	ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2463b8d26b3bSNicholas Bellinger 	if (ret) {
2464b8d26b3bSNicholas Bellinger 		pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2465b8d26b3bSNicholas Bellinger 		goto out_lid;
2466b8d26b3bSNicholas Bellinger 	}
2467b8d26b3bSNicholas Bellinger 
2468b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
2469b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2470b8d26b3bSNicholas Bellinger 	pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2471b8d26b3bSNicholas Bellinger 
2472b8d26b3bSNicholas Bellinger 	return 0;
2473b8d26b3bSNicholas Bellinger 
2474b8d26b3bSNicholas Bellinger out_lid:
2475b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_lid);
2476b8d26b3bSNicholas Bellinger out:
2477b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2478b8d26b3bSNicholas Bellinger 	return ret;
2479b8d26b3bSNicholas Bellinger }
2480b8d26b3bSNicholas Bellinger 
2481b8d26b3bSNicholas Bellinger static int
2482b8d26b3bSNicholas Bellinger isert_check_accept_queue(struct isert_np *isert_np)
2483b8d26b3bSNicholas Bellinger {
2484b8d26b3bSNicholas Bellinger 	int empty;
2485b8d26b3bSNicholas Bellinger 
2486b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
2487b8d26b3bSNicholas Bellinger 	empty = list_empty(&isert_np->np_accept_list);
2488b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
2489b8d26b3bSNicholas Bellinger 
2490b8d26b3bSNicholas Bellinger 	return empty;
2491b8d26b3bSNicholas Bellinger }
2492b8d26b3bSNicholas Bellinger 
2493b8d26b3bSNicholas Bellinger static int
2494b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2495b8d26b3bSNicholas Bellinger {
2496b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2497b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2498b8d26b3bSNicholas Bellinger 	int ret;
2499b8d26b3bSNicholas Bellinger 
2500b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2501b8d26b3bSNicholas Bellinger 	cp.responder_resources = isert_conn->responder_resources;
2502b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2503b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2504b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2505b8d26b3bSNicholas Bellinger 
2506b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2507b8d26b3bSNicholas Bellinger 
2508b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2509b8d26b3bSNicholas Bellinger 	if (ret) {
2510b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
2511b8d26b3bSNicholas Bellinger 		return ret;
2512b8d26b3bSNicholas Bellinger 	}
2513b8d26b3bSNicholas Bellinger 
2514b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2515b8d26b3bSNicholas Bellinger 
2516b8d26b3bSNicholas Bellinger 	return 0;
2517b8d26b3bSNicholas Bellinger }
2518b8d26b3bSNicholas Bellinger 
2519b8d26b3bSNicholas Bellinger static int
2520b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2521b8d26b3bSNicholas Bellinger {
2522b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2523b8d26b3bSNicholas Bellinger 	int ret;
2524b8d26b3bSNicholas Bellinger 
2525b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
25266faaa85fSNicholas Bellinger 	/*
25276faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
25286faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
25296faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
25306faaa85fSNicholas Bellinger 	 * into a NOP.
25316faaa85fSNicholas Bellinger 	 */
25326faaa85fSNicholas Bellinger 	if (!login->first_request)
25336faaa85fSNicholas Bellinger 		return 0;
2534b8d26b3bSNicholas Bellinger 
2535b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2536b8d26b3bSNicholas Bellinger 	if (ret)
2537b8d26b3bSNicholas Bellinger 		return ret;
2538b8d26b3bSNicholas Bellinger 
2539b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2540b8d26b3bSNicholas Bellinger 	return 0;
2541b8d26b3bSNicholas Bellinger }
2542b8d26b3bSNicholas Bellinger 
2543b8d26b3bSNicholas Bellinger static void
2544b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2545b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2546b8d26b3bSNicholas Bellinger {
2547b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2548b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2549b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
2550b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
2551b8d26b3bSNicholas Bellinger 
2552b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2553b8d26b3bSNicholas Bellinger 
2554b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
2555b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2556b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2557b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
2558b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
2559b8d26b3bSNicholas Bellinger 
2560b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2561b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2562b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
2563b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
2564b8d26b3bSNicholas Bellinger 	} else {
2565b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2566b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
2567b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
2568b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
2569b8d26b3bSNicholas Bellinger 
2570b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2571b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
2572b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
2573b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
2574b8d26b3bSNicholas Bellinger 	}
2575b8d26b3bSNicholas Bellinger }
2576b8d26b3bSNicholas Bellinger 
2577b8d26b3bSNicholas Bellinger static int
2578b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2579b8d26b3bSNicholas Bellinger {
2580b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
2581b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2582b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
2583b8d26b3bSNicholas Bellinger 
2584b8d26b3bSNicholas Bellinger accept_wait:
2585b8d26b3bSNicholas Bellinger 	ret = wait_event_interruptible(isert_np->np_accept_wq,
2586b8d26b3bSNicholas Bellinger 			!isert_check_accept_queue(isert_np) ||
2587b8d26b3bSNicholas Bellinger 			np->np_thread_state == ISCSI_NP_THREAD_RESET);
2588b8d26b3bSNicholas Bellinger 	if (max_accept > 5)
2589b8d26b3bSNicholas Bellinger 		return -ENODEV;
2590b8d26b3bSNicholas Bellinger 
2591b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2592b8d26b3bSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2593b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
2594b8d26b3bSNicholas Bellinger 		pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2595b8d26b3bSNicholas Bellinger 		return -ENODEV;
2596b8d26b3bSNicholas Bellinger 	}
2597b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2598b8d26b3bSNicholas Bellinger 
2599b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
2600b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
2601b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
2602b8d26b3bSNicholas Bellinger 		max_accept++;
2603b8d26b3bSNicholas Bellinger 		goto accept_wait;
2604b8d26b3bSNicholas Bellinger 	}
2605b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
2606b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
2607b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
2608b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
2609b8d26b3bSNicholas Bellinger 
2610b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2611b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2612b8d26b3bSNicholas Bellinger 	max_accept = 0;
2613b8d26b3bSNicholas Bellinger 
2614b8d26b3bSNicholas Bellinger 	ret = isert_rdma_post_recvl(isert_conn);
2615b8d26b3bSNicholas Bellinger 	if (ret)
2616b8d26b3bSNicholas Bellinger 		return ret;
2617b8d26b3bSNicholas Bellinger 
2618b8d26b3bSNicholas Bellinger 	ret = isert_rdma_accept(isert_conn);
2619b8d26b3bSNicholas Bellinger 	if (ret)
2620b8d26b3bSNicholas Bellinger 		return ret;
2621b8d26b3bSNicholas Bellinger 
2622b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2623b8d26b3bSNicholas Bellinger 
2624b8d26b3bSNicholas Bellinger 	pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2625b8d26b3bSNicholas Bellinger 	return 0;
2626b8d26b3bSNicholas Bellinger }
2627b8d26b3bSNicholas Bellinger 
2628b8d26b3bSNicholas Bellinger static void
2629b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2630b8d26b3bSNicholas Bellinger {
2631b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
2632b8d26b3bSNicholas Bellinger 
2633b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_np->np_cm_id);
2634b8d26b3bSNicholas Bellinger 
2635b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2636b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2637b8d26b3bSNicholas Bellinger }
2638b8d26b3bSNicholas Bellinger 
2639b2cb9649SNicholas Bellinger static int isert_check_state(struct isert_conn *isert_conn, int state)
2640b2cb9649SNicholas Bellinger {
2641b2cb9649SNicholas Bellinger 	int ret;
2642b2cb9649SNicholas Bellinger 
2643b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2644b2cb9649SNicholas Bellinger 	ret = (isert_conn->state == state);
2645b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2646b2cb9649SNicholas Bellinger 
2647b2cb9649SNicholas Bellinger 	return ret;
2648b2cb9649SNicholas Bellinger }
2649b2cb9649SNicholas Bellinger 
2650b8d26b3bSNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2651b8d26b3bSNicholas Bellinger {
2652b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2653b8d26b3bSNicholas Bellinger 
2654b8d26b3bSNicholas Bellinger 	pr_debug("isert_free_conn: Starting \n");
2655b8d26b3bSNicholas Bellinger 	/*
2656b8d26b3bSNicholas Bellinger 	 * Decrement post_send_buf_count for special case when called
2657b8d26b3bSNicholas Bellinger 	 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2658b8d26b3bSNicholas Bellinger 	 */
2659b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2660b8d26b3bSNicholas Bellinger 	if (isert_conn->logout_posted)
2661b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2662b8d26b3bSNicholas Bellinger 
2663b2cb9649SNicholas Bellinger 	if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
2664b2cb9649SNicholas Bellinger 		pr_debug("Calling rdma_disconnect from isert_free_conn\n");
2665b8d26b3bSNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
2666b2cb9649SNicholas Bellinger 	}
2667b8d26b3bSNicholas Bellinger 	/*
2668b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
2669b8d26b3bSNicholas Bellinger 	 * into full feature phase..
2670b8d26b3bSNicholas Bellinger 	 */
2671b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP) {
2672b8d26b3bSNicholas Bellinger 		pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
2673b8d26b3bSNicholas Bellinger 			 isert_conn->state);
2674b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
2675b8d26b3bSNicholas Bellinger 
2676b2cb9649SNicholas Bellinger 		wait_event(isert_conn->conn_wait_comp_err,
2677b2cb9649SNicholas Bellinger 			  (isert_check_state(isert_conn, ISER_CONN_TERMINATING)));
2678b2cb9649SNicholas Bellinger 
2679b2cb9649SNicholas Bellinger 		wait_event(isert_conn->conn_wait,
2680b2cb9649SNicholas Bellinger 			  (isert_check_state(isert_conn, ISER_CONN_DOWN)));
2681b2cb9649SNicholas Bellinger 
2682b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
2683b2cb9649SNicholas Bellinger 		return;
2684b2cb9649SNicholas Bellinger 	}
2685b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
2686b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
2687b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
2688b2cb9649SNicholas Bellinger 		return;
2689b2cb9649SNicholas Bellinger 	}
2690b2cb9649SNicholas Bellinger 	pr_debug("isert_free_conn: wait_event conn_wait %d\n",
2691b2cb9649SNicholas Bellinger 		 isert_conn->state);
2692b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2693b2cb9649SNicholas Bellinger 
2694b2cb9649SNicholas Bellinger 	wait_event(isert_conn->conn_wait,
2695b2cb9649SNicholas Bellinger 		  (isert_check_state(isert_conn, ISER_CONN_DOWN)));
2696b8d26b3bSNicholas Bellinger 
2697b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2698b8d26b3bSNicholas Bellinger }
2699b8d26b3bSNicholas Bellinger 
2700b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2701b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2702b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2703d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
2704b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2705b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2706b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2707b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2708b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2709b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2710b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2711b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2712b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2713b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2714b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2715b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2716b8d26b3bSNicholas Bellinger };
2717b8d26b3bSNicholas Bellinger 
2718b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2719b8d26b3bSNicholas Bellinger {
2720b8d26b3bSNicholas Bellinger 	int ret;
2721b8d26b3bSNicholas Bellinger 
2722b8d26b3bSNicholas Bellinger 	isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2723b8d26b3bSNicholas Bellinger 	if (!isert_rx_wq) {
2724b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_rx_wq\n");
2725b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2726b8d26b3bSNicholas Bellinger 	}
2727b8d26b3bSNicholas Bellinger 
2728b8d26b3bSNicholas Bellinger 	isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2729b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
2730b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_comp_wq\n");
2731b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2732b8d26b3bSNicholas Bellinger 		goto destroy_rx_wq;
2733b8d26b3bSNicholas Bellinger 	}
2734b8d26b3bSNicholas Bellinger 
2735b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
2736b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2737b8d26b3bSNicholas Bellinger 	return 0;
2738b8d26b3bSNicholas Bellinger 
2739b8d26b3bSNicholas Bellinger destroy_rx_wq:
2740b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
2741b8d26b3bSNicholas Bellinger 	return ret;
2742b8d26b3bSNicholas Bellinger }
2743b8d26b3bSNicholas Bellinger 
2744b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2745b8d26b3bSNicholas Bellinger {
2746b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2747b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
2748b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
2749b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2750b8d26b3bSNicholas Bellinger }
2751b8d26b3bSNicholas Bellinger 
2752b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2753b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
2754b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2755b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2756b8d26b3bSNicholas Bellinger 
2757b8d26b3bSNicholas Bellinger module_init(isert_init);
2758b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2759