xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision adb54c29310e8785e4f2eb0dc76e0e2fde943b11)
1b8d26b3bSNicholas Bellinger /*******************************************************************************
2b8d26b3bSNicholas Bellinger  * This file contains iSCSI extentions for RDMA (iSER) Verbs
3b8d26b3bSNicholas Bellinger  *
4b8d26b3bSNicholas Bellinger  * (c) Copyright 2013 RisingTide Systems LLC.
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 static struct kmem_cache *isert_cmd_cache;
43b8d26b3bSNicholas Bellinger 
44b8d26b3bSNicholas Bellinger static void
45b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
46b8d26b3bSNicholas Bellinger {
47b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)context;
48b8d26b3bSNicholas Bellinger 
49b8d26b3bSNicholas Bellinger 	pr_err("isert_qp_event_callback event: %d\n", e->event);
50b8d26b3bSNicholas Bellinger 	switch (e->event) {
51b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
52b8d26b3bSNicholas Bellinger 		rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
53b8d26b3bSNicholas Bellinger 		break;
54b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
55b8d26b3bSNicholas Bellinger 		pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
56b8d26b3bSNicholas Bellinger 		break;
57b8d26b3bSNicholas Bellinger 	default:
58b8d26b3bSNicholas Bellinger 		break;
59b8d26b3bSNicholas Bellinger 	}
60b8d26b3bSNicholas Bellinger }
61b8d26b3bSNicholas Bellinger 
62b8d26b3bSNicholas Bellinger static int
63b8d26b3bSNicholas Bellinger isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
64b8d26b3bSNicholas Bellinger {
65b8d26b3bSNicholas Bellinger 	int ret;
66b8d26b3bSNicholas Bellinger 
67b8d26b3bSNicholas Bellinger 	ret = ib_query_device(ib_dev, devattr);
68b8d26b3bSNicholas Bellinger 	if (ret) {
69b8d26b3bSNicholas Bellinger 		pr_err("ib_query_device() failed: %d\n", ret);
70b8d26b3bSNicholas Bellinger 		return ret;
71b8d26b3bSNicholas Bellinger 	}
72b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
73b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
74b8d26b3bSNicholas Bellinger 
75b8d26b3bSNicholas Bellinger 	return 0;
76b8d26b3bSNicholas Bellinger }
77b8d26b3bSNicholas Bellinger 
78b8d26b3bSNicholas Bellinger static int
79b8d26b3bSNicholas Bellinger isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
80b8d26b3bSNicholas Bellinger {
81b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
82b8d26b3bSNicholas Bellinger 	struct ib_qp_init_attr attr;
83b8d26b3bSNicholas Bellinger 	struct ib_device_attr devattr;
84b8d26b3bSNicholas Bellinger 	int ret, index, min_index = 0;
85b8d26b3bSNicholas Bellinger 
86b8d26b3bSNicholas Bellinger 	memset(&devattr, 0, sizeof(struct ib_device_attr));
87b8d26b3bSNicholas Bellinger 	ret = isert_query_device(cma_id->device, &devattr);
88b8d26b3bSNicholas Bellinger 	if (ret)
89b8d26b3bSNicholas Bellinger 		return ret;
90b8d26b3bSNicholas Bellinger 
91b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
92b8d26b3bSNicholas Bellinger 	for (index = 0; index < device->cqs_used; index++)
93b8d26b3bSNicholas Bellinger 		if (device->cq_active_qps[index] <
94b8d26b3bSNicholas Bellinger 		    device->cq_active_qps[min_index])
95b8d26b3bSNicholas Bellinger 			min_index = index;
96b8d26b3bSNicholas Bellinger 	device->cq_active_qps[min_index]++;
97b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
98b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
99b8d26b3bSNicholas Bellinger 
100b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
101b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
102b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
103b8d26b3bSNicholas Bellinger 	attr.send_cq = device->dev_tx_cq[min_index];
104b8d26b3bSNicholas Bellinger 	attr.recv_cq = device->dev_rx_cq[min_index];
105b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
106b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
107b8d26b3bSNicholas Bellinger 	/*
108b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
109b8d26b3bSNicholas Bellinger 	 * work-around for RDMA_READ..
110b8d26b3bSNicholas Bellinger 	 */
111b8d26b3bSNicholas Bellinger 	attr.cap.max_send_sge = devattr.max_sge - 2;
112b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
113b8d26b3bSNicholas Bellinger 
114b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
115b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
116b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
117b8d26b3bSNicholas Bellinger 
118b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
119b8d26b3bSNicholas Bellinger 		 cma_id->device);
120b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
121b8d26b3bSNicholas Bellinger 		 isert_conn->conn_pd->device);
122b8d26b3bSNicholas Bellinger 
123b8d26b3bSNicholas Bellinger 	ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
124b8d26b3bSNicholas Bellinger 	if (ret) {
125b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_qp failed for cma_id %d\n", ret);
126b8d26b3bSNicholas Bellinger 		return ret;
127b8d26b3bSNicholas Bellinger 	}
128b8d26b3bSNicholas Bellinger 	isert_conn->conn_qp = cma_id->qp;
129b8d26b3bSNicholas Bellinger 	pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
130b8d26b3bSNicholas Bellinger 
131b8d26b3bSNicholas Bellinger 	return 0;
132b8d26b3bSNicholas Bellinger }
133b8d26b3bSNicholas Bellinger 
134b8d26b3bSNicholas Bellinger static void
135b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
136b8d26b3bSNicholas Bellinger {
137b8d26b3bSNicholas Bellinger 	pr_debug("isert_cq_event_callback event: %d\n", e->event);
138b8d26b3bSNicholas Bellinger }
139b8d26b3bSNicholas Bellinger 
140b8d26b3bSNicholas Bellinger static int
141b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
142b8d26b3bSNicholas Bellinger {
143b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
144b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
145b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
146b8d26b3bSNicholas Bellinger 	u64 dma_addr;
147b8d26b3bSNicholas Bellinger 	int i, j;
148b8d26b3bSNicholas Bellinger 
149b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
150b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
151b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
152b8d26b3bSNicholas Bellinger 		goto fail;
153b8d26b3bSNicholas Bellinger 
154b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
155b8d26b3bSNicholas Bellinger 
156b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
157b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
158b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
159b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
160b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
161b8d26b3bSNicholas Bellinger 
162b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
163b8d26b3bSNicholas Bellinger 
164b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
165b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
166b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
167b8d26b3bSNicholas Bellinger 		rx_sg->lkey = isert_conn->conn_mr->lkey;
168b8d26b3bSNicholas Bellinger 	}
169b8d26b3bSNicholas Bellinger 
170b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
171b8d26b3bSNicholas Bellinger 	return 0;
172b8d26b3bSNicholas Bellinger 
173b8d26b3bSNicholas Bellinger dma_map_fail:
174b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
175b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
176b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
177b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
178b8d26b3bSNicholas Bellinger 	}
179b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
180b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
181b8d26b3bSNicholas Bellinger fail:
182b8d26b3bSNicholas Bellinger 	return -ENOMEM;
183b8d26b3bSNicholas Bellinger }
184b8d26b3bSNicholas Bellinger 
185b8d26b3bSNicholas Bellinger static void
186b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
187b8d26b3bSNicholas Bellinger {
188b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
189b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
190b8d26b3bSNicholas Bellinger 	int i;
191b8d26b3bSNicholas Bellinger 
192b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
193b8d26b3bSNicholas Bellinger 		return;
194b8d26b3bSNicholas Bellinger 
195b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
196b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
197b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
198b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
199b8d26b3bSNicholas Bellinger 	}
200b8d26b3bSNicholas Bellinger 
201b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
202b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
203b8d26b3bSNicholas Bellinger }
204b8d26b3bSNicholas Bellinger 
205b8d26b3bSNicholas Bellinger static void isert_cq_tx_callback(struct ib_cq *, void *);
206b8d26b3bSNicholas Bellinger static void isert_cq_rx_callback(struct ib_cq *, void *);
207b8d26b3bSNicholas Bellinger 
208b8d26b3bSNicholas Bellinger static int
209b8d26b3bSNicholas Bellinger isert_create_device_ib_res(struct isert_device *device)
210b8d26b3bSNicholas Bellinger {
211b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = device->ib_device;
212b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
213b8d26b3bSNicholas Bellinger 	int ret = 0, i, j;
214b8d26b3bSNicholas Bellinger 
215b8d26b3bSNicholas Bellinger 	device->cqs_used = min_t(int, num_online_cpus(),
216b8d26b3bSNicholas Bellinger 				 device->ib_device->num_comp_vectors);
217b8d26b3bSNicholas Bellinger 	device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
218b8d26b3bSNicholas Bellinger 	pr_debug("Using %d CQs, device %s supports %d vectors\n",
219b8d26b3bSNicholas Bellinger 		 device->cqs_used, device->ib_device->name,
220b8d26b3bSNicholas Bellinger 		 device->ib_device->num_comp_vectors);
221b8d26b3bSNicholas Bellinger 	device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
222b8d26b3bSNicholas Bellinger 				device->cqs_used, GFP_KERNEL);
223b8d26b3bSNicholas Bellinger 	if (!device->cq_desc) {
224b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate device->cq_desc\n");
225b8d26b3bSNicholas Bellinger 		return -ENOMEM;
226b8d26b3bSNicholas Bellinger 	}
227b8d26b3bSNicholas Bellinger 	cq_desc = device->cq_desc;
228b8d26b3bSNicholas Bellinger 
229b8d26b3bSNicholas Bellinger 	device->dev_pd = ib_alloc_pd(ib_dev);
230b8d26b3bSNicholas Bellinger 	if (IS_ERR(device->dev_pd)) {
231b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device->dev_pd);
232b8d26b3bSNicholas Bellinger 		pr_err("ib_alloc_pd failed for dev_pd: %d\n", ret);
233b8d26b3bSNicholas Bellinger 		goto out_cq_desc;
234b8d26b3bSNicholas Bellinger 	}
235b8d26b3bSNicholas Bellinger 
236b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
237b8d26b3bSNicholas Bellinger 		cq_desc[i].device = device;
238b8d26b3bSNicholas Bellinger 		cq_desc[i].cq_index = i;
239b8d26b3bSNicholas Bellinger 
240b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
241b8d26b3bSNicholas Bellinger 						isert_cq_rx_callback,
242b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
243b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
244b8d26b3bSNicholas Bellinger 						ISER_MAX_RX_CQ_LEN, i);
245b8d26b3bSNicholas Bellinger 		if (IS_ERR(device->dev_rx_cq[i]))
246b8d26b3bSNicholas Bellinger 			goto out_cq;
247b8d26b3bSNicholas Bellinger 
248b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
249b8d26b3bSNicholas Bellinger 						isert_cq_tx_callback,
250b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
251b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
252b8d26b3bSNicholas Bellinger 						ISER_MAX_TX_CQ_LEN, i);
253b8d26b3bSNicholas Bellinger 		if (IS_ERR(device->dev_tx_cq[i]))
254b8d26b3bSNicholas Bellinger 			goto out_cq;
255b8d26b3bSNicholas Bellinger 
256b8d26b3bSNicholas Bellinger 		if (ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP))
257b8d26b3bSNicholas Bellinger 			goto out_cq;
258b8d26b3bSNicholas Bellinger 
259b8d26b3bSNicholas Bellinger 		if (ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP))
260b8d26b3bSNicholas Bellinger 			goto out_cq;
261b8d26b3bSNicholas Bellinger 	}
262b8d26b3bSNicholas Bellinger 
263b8d26b3bSNicholas Bellinger 	device->dev_mr = ib_get_dma_mr(device->dev_pd, IB_ACCESS_LOCAL_WRITE);
264b8d26b3bSNicholas Bellinger 	if (IS_ERR(device->dev_mr)) {
265b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device->dev_mr);
266b8d26b3bSNicholas Bellinger 		pr_err("ib_get_dma_mr failed for dev_mr: %d\n", ret);
267b8d26b3bSNicholas Bellinger 		goto out_cq;
268b8d26b3bSNicholas Bellinger 	}
269b8d26b3bSNicholas Bellinger 
270b8d26b3bSNicholas Bellinger 	return 0;
271b8d26b3bSNicholas Bellinger 
272b8d26b3bSNicholas Bellinger out_cq:
273b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++) {
274b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[j];
275b8d26b3bSNicholas Bellinger 
276b8d26b3bSNicholas Bellinger 		if (device->dev_rx_cq[j]) {
277b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_rx_work);
278b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_rx_cq[j]);
279b8d26b3bSNicholas Bellinger 		}
280b8d26b3bSNicholas Bellinger 		if (device->dev_tx_cq[j]) {
281b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_tx_work);
282b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_tx_cq[j]);
283b8d26b3bSNicholas Bellinger 		}
284b8d26b3bSNicholas Bellinger 	}
285b8d26b3bSNicholas Bellinger 	ib_dealloc_pd(device->dev_pd);
286b8d26b3bSNicholas Bellinger 
287b8d26b3bSNicholas Bellinger out_cq_desc:
288b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
289b8d26b3bSNicholas Bellinger 
290b8d26b3bSNicholas Bellinger 	return ret;
291b8d26b3bSNicholas Bellinger }
292b8d26b3bSNicholas Bellinger 
293b8d26b3bSNicholas Bellinger static void
294b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
295b8d26b3bSNicholas Bellinger {
296b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
297b8d26b3bSNicholas Bellinger 	int i;
298b8d26b3bSNicholas Bellinger 
299b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
300b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[i];
301b8d26b3bSNicholas Bellinger 
302b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_rx_work);
303b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_tx_work);
304b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_rx_cq[i]);
305b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_tx_cq[i]);
306b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = NULL;
307b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = NULL;
308b8d26b3bSNicholas Bellinger 	}
309b8d26b3bSNicholas Bellinger 
310b8d26b3bSNicholas Bellinger 	ib_dereg_mr(device->dev_mr);
311b8d26b3bSNicholas Bellinger 	ib_dealloc_pd(device->dev_pd);
312b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
313b8d26b3bSNicholas Bellinger }
314b8d26b3bSNicholas Bellinger 
315b8d26b3bSNicholas Bellinger static void
316b8d26b3bSNicholas Bellinger isert_device_try_release(struct isert_device *device)
317b8d26b3bSNicholas Bellinger {
318b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
319b8d26b3bSNicholas Bellinger 	device->refcount--;
320b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
321b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
322b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
323b8d26b3bSNicholas Bellinger 		kfree(device);
324b8d26b3bSNicholas Bellinger 	}
325b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
326b8d26b3bSNicholas Bellinger }
327b8d26b3bSNicholas Bellinger 
328b8d26b3bSNicholas Bellinger static struct isert_device *
329b8d26b3bSNicholas Bellinger isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
330b8d26b3bSNicholas Bellinger {
331b8d26b3bSNicholas Bellinger 	struct isert_device *device;
332b8d26b3bSNicholas Bellinger 	int ret;
333b8d26b3bSNicholas Bellinger 
334b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
335b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
336b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
337b8d26b3bSNicholas Bellinger 			device->refcount++;
338b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
339b8d26b3bSNicholas Bellinger 			return device;
340b8d26b3bSNicholas Bellinger 		}
341b8d26b3bSNicholas Bellinger 	}
342b8d26b3bSNicholas Bellinger 
343b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
344b8d26b3bSNicholas Bellinger 	if (!device) {
345b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
346b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
347b8d26b3bSNicholas Bellinger 	}
348b8d26b3bSNicholas Bellinger 
349b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
350b8d26b3bSNicholas Bellinger 
351b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
352b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
353b8d26b3bSNicholas Bellinger 	if (ret) {
354b8d26b3bSNicholas Bellinger 		kfree(device);
355b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
356b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
357b8d26b3bSNicholas Bellinger 	}
358b8d26b3bSNicholas Bellinger 
359b8d26b3bSNicholas Bellinger 	device->refcount++;
360b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
361b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
362b8d26b3bSNicholas Bellinger 
363b8d26b3bSNicholas Bellinger 	return device;
364b8d26b3bSNicholas Bellinger }
365b8d26b3bSNicholas Bellinger 
366b8d26b3bSNicholas Bellinger static int
367b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
368b8d26b3bSNicholas Bellinger {
369b8d26b3bSNicholas Bellinger 	struct iscsi_np *np = cma_id->context;
370b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = np->np_context;
371b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
372b8d26b3bSNicholas Bellinger 	struct isert_device *device;
373b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
374b8d26b3bSNicholas Bellinger 	int ret = 0;
375b8d26b3bSNicholas Bellinger 
376b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
377b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
378b8d26b3bSNicholas Bellinger 
379b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
380b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
381b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn\n");
382b8d26b3bSNicholas Bellinger 		return -ENOMEM;
383b8d26b3bSNicholas Bellinger 	}
384b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
385b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
386b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
387b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_conn->conn_wait);
388b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_conn->conn_wait_comp_err);
389b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
390b8d26b3bSNicholas Bellinger 	kref_get(&isert_conn->conn_kref);
391b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
392b8d26b3bSNicholas Bellinger 
393b8d26b3bSNicholas Bellinger 	cma_id->context = isert_conn;
394b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
395b8d26b3bSNicholas Bellinger 	isert_conn->responder_resources = event->param.conn.responder_resources;
396b8d26b3bSNicholas Bellinger 	isert_conn->initiator_depth = event->param.conn.initiator_depth;
397b8d26b3bSNicholas Bellinger 	pr_debug("Using responder_resources: %u initiator_depth: %u\n",
398b8d26b3bSNicholas Bellinger 		 isert_conn->responder_resources, isert_conn->initiator_depth);
399b8d26b3bSNicholas Bellinger 
400b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
401b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
402b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
403b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
404b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
405b8d26b3bSNicholas Bellinger 		goto out;
406b8d26b3bSNicholas Bellinger 	}
407b8d26b3bSNicholas Bellinger 
408b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
409b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
410b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
411b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
412b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
413b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
414b8d26b3bSNicholas Bellinger 
415b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
416b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
417b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
418b8d26b3bSNicholas Bellinger 
419b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
420b8d26b3bSNicholas Bellinger 	if (ret) {
421b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
422b8d26b3bSNicholas Bellinger 		       ret);
423b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
424b8d26b3bSNicholas Bellinger 		goto out_login_buf;
425b8d26b3bSNicholas Bellinger 	}
426b8d26b3bSNicholas Bellinger 
427b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
428b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
429b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
430b8d26b3bSNicholas Bellinger 
431b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
432b8d26b3bSNicholas Bellinger 	if (ret) {
433b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
434b8d26b3bSNicholas Bellinger 		       ret);
435b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
436b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
437b8d26b3bSNicholas Bellinger 	}
438b8d26b3bSNicholas Bellinger 
439b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
440b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
441b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
442b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
443b8d26b3bSNicholas Bellinger 	}
444b8d26b3bSNicholas Bellinger 
445b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
446b8d26b3bSNicholas Bellinger 	isert_conn->conn_pd = device->dev_pd;
447b8d26b3bSNicholas Bellinger 	isert_conn->conn_mr = device->dev_mr;
448b8d26b3bSNicholas Bellinger 
449b8d26b3bSNicholas Bellinger 	ret = isert_conn_setup_qp(isert_conn, cma_id);
450b8d26b3bSNicholas Bellinger 	if (ret)
451b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
452b8d26b3bSNicholas Bellinger 
453b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
454b8d26b3bSNicholas Bellinger 	list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
455b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
456b8d26b3bSNicholas Bellinger 
457b8d26b3bSNicholas Bellinger 	pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
458b8d26b3bSNicholas Bellinger 	wake_up(&isert_np->np_accept_wq);
459b8d26b3bSNicholas Bellinger 	return 0;
460b8d26b3bSNicholas Bellinger 
461b8d26b3bSNicholas Bellinger out_conn_dev:
462b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
463b8d26b3bSNicholas Bellinger out_rsp_dma_map:
464b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
465b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
466b8d26b3bSNicholas Bellinger out_req_dma_map:
467b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
468b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
469b8d26b3bSNicholas Bellinger out_login_buf:
470b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
471b8d26b3bSNicholas Bellinger out:
472b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
473b8d26b3bSNicholas Bellinger 	return ret;
474b8d26b3bSNicholas Bellinger }
475b8d26b3bSNicholas Bellinger 
476b8d26b3bSNicholas Bellinger static void
477b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
478b8d26b3bSNicholas Bellinger {
479b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
480b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
481b8d26b3bSNicholas Bellinger 	int cq_index;
482b8d26b3bSNicholas Bellinger 
483b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
484b8d26b3bSNicholas Bellinger 
485b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
486b8d26b3bSNicholas Bellinger 		cq_index = ((struct isert_cq_desc *)
487b8d26b3bSNicholas Bellinger 			isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
488b8d26b3bSNicholas Bellinger 		pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
489b8d26b3bSNicholas Bellinger 		isert_conn->conn_device->cq_active_qps[cq_index]--;
490b8d26b3bSNicholas Bellinger 
491b8d26b3bSNicholas Bellinger 		rdma_destroy_qp(isert_conn->conn_cm_id);
492b8d26b3bSNicholas Bellinger 	}
493b8d26b3bSNicholas Bellinger 
494b8d26b3bSNicholas Bellinger 	isert_free_rx_descriptors(isert_conn);
495b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_conn->conn_cm_id);
496b8d26b3bSNicholas Bellinger 
497b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
498b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
499b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
500b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
501b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
502b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
503b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
504b8d26b3bSNicholas Bellinger 	}
505b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
506b8d26b3bSNicholas Bellinger 
507b8d26b3bSNicholas Bellinger 	if (device)
508b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
509b8d26b3bSNicholas Bellinger 
510b8d26b3bSNicholas Bellinger 	pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
511b8d26b3bSNicholas Bellinger }
512b8d26b3bSNicholas Bellinger 
513b8d26b3bSNicholas Bellinger static void
514b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
515b8d26b3bSNicholas Bellinger {
516b8d26b3bSNicholas Bellinger 	return;
517b8d26b3bSNicholas Bellinger }
518b8d26b3bSNicholas Bellinger 
519b8d26b3bSNicholas Bellinger static void
520b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
521b8d26b3bSNicholas Bellinger {
522b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
523b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
524b8d26b3bSNicholas Bellinger 
525b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
526b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
527b8d26b3bSNicholas Bellinger 
528b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
529b8d26b3bSNicholas Bellinger }
530b8d26b3bSNicholas Bellinger 
531b8d26b3bSNicholas Bellinger static void
532b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
533b8d26b3bSNicholas Bellinger {
534b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
535b8d26b3bSNicholas Bellinger }
536b8d26b3bSNicholas Bellinger 
537b8d26b3bSNicholas Bellinger static void
538b8d26b3bSNicholas Bellinger isert_disconnect_work(struct work_struct *work)
539b8d26b3bSNicholas Bellinger {
540b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(work,
541b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_logout_work);
542b8d26b3bSNicholas Bellinger 
543b8d26b3bSNicholas Bellinger 	pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
544b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
545b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_DOWN;
546b8d26b3bSNicholas Bellinger 
547b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
548b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
549b8d26b3bSNicholas Bellinger 		pr_debug("Calling wake_up(&isert_conn->conn_wait);\n");
550b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
551b2cb9649SNicholas Bellinger 		goto wake_up;
552b8d26b3bSNicholas Bellinger 	}
553b2cb9649SNicholas Bellinger 	if (!isert_conn->conn_cm_id) {
554b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
555b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
556b2cb9649SNicholas Bellinger 		return;
557b2cb9649SNicholas Bellinger 	}
558b2cb9649SNicholas Bellinger 	if (!isert_conn->logout_posted) {
559b2cb9649SNicholas Bellinger 		pr_debug("Calling rdma_disconnect for !logout_posted from"
560b2cb9649SNicholas Bellinger 			 " isert_disconnect_work\n");
561b2cb9649SNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
562b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
563b2cb9649SNicholas Bellinger 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
564b2cb9649SNicholas Bellinger 		goto wake_up;
565b2cb9649SNicholas Bellinger 	}
566b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
567b8d26b3bSNicholas Bellinger 
568b2cb9649SNicholas Bellinger wake_up:
569b2cb9649SNicholas Bellinger 	wake_up(&isert_conn->conn_wait);
570b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
571b8d26b3bSNicholas Bellinger }
572b8d26b3bSNicholas Bellinger 
573b8d26b3bSNicholas Bellinger static void
574b8d26b3bSNicholas Bellinger isert_disconnected_handler(struct rdma_cm_id *cma_id)
575b8d26b3bSNicholas Bellinger {
576b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
577b8d26b3bSNicholas Bellinger 
578b8d26b3bSNicholas Bellinger 	INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
579b8d26b3bSNicholas Bellinger 	schedule_work(&isert_conn->conn_logout_work);
580b8d26b3bSNicholas Bellinger }
581b8d26b3bSNicholas Bellinger 
582b8d26b3bSNicholas Bellinger static int
583b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
584b8d26b3bSNicholas Bellinger {
585b8d26b3bSNicholas Bellinger 	int ret = 0;
586b8d26b3bSNicholas Bellinger 
587b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
588b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
589b8d26b3bSNicholas Bellinger 
590b8d26b3bSNicholas Bellinger 	switch (event->event) {
591b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
592b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
593b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
594b8d26b3bSNicholas Bellinger 		break;
595b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
596b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
597b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
598b8d26b3bSNicholas Bellinger 		break;
599b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DISCONNECTED:
600b8d26b3bSNicholas Bellinger 		pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
601b8d26b3bSNicholas Bellinger 		isert_disconnected_handler(cma_id);
602b8d26b3bSNicholas Bellinger 		break;
603b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
604b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ADDR_CHANGE:
605b8d26b3bSNicholas Bellinger 		break;
606b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
607b8d26b3bSNicholas Bellinger 	default:
608b8d26b3bSNicholas Bellinger 		pr_err("Unknown RDMA CMA event: %d\n", event->event);
609b8d26b3bSNicholas Bellinger 		break;
610b8d26b3bSNicholas Bellinger 	}
611b8d26b3bSNicholas Bellinger 
612b8d26b3bSNicholas Bellinger 	if (ret != 0) {
613b8d26b3bSNicholas Bellinger 		pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
614b8d26b3bSNicholas Bellinger 		       event->event, ret);
615b8d26b3bSNicholas Bellinger 		dump_stack();
616b8d26b3bSNicholas Bellinger 	}
617b8d26b3bSNicholas Bellinger 
618b8d26b3bSNicholas Bellinger 	return ret;
619b8d26b3bSNicholas Bellinger }
620b8d26b3bSNicholas Bellinger 
621b8d26b3bSNicholas Bellinger static int
622b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
623b8d26b3bSNicholas Bellinger {
624b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
625b8d26b3bSNicholas Bellinger 	int i, ret;
626b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
627b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
628b8d26b3bSNicholas Bellinger 
629b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
630b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
631b8d26b3bSNicholas Bellinger 		rx_wr->wr_id	= (unsigned long)rx_desc;
632b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
633b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
634b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
635b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
636b8d26b3bSNicholas Bellinger 	}
637b8d26b3bSNicholas Bellinger 
638b8d26b3bSNicholas Bellinger 	rx_wr--;
639b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
640b8d26b3bSNicholas Bellinger 
641b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
642b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
643b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
644b8d26b3bSNicholas Bellinger 	if (ret) {
645b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed with ret: %d\n", ret);
646b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
647b8d26b3bSNicholas Bellinger 	} else {
648b8d26b3bSNicholas Bellinger 		pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
649b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
650b8d26b3bSNicholas Bellinger 	}
651b8d26b3bSNicholas Bellinger 	return ret;
652b8d26b3bSNicholas Bellinger }
653b8d26b3bSNicholas Bellinger 
654b8d26b3bSNicholas Bellinger static int
655b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
656b8d26b3bSNicholas Bellinger {
657b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
658b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
659b8d26b3bSNicholas Bellinger 	int ret;
660b8d26b3bSNicholas Bellinger 
661b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
662b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
663b8d26b3bSNicholas Bellinger 
664b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
665b8d26b3bSNicholas Bellinger 	send_wr.wr_id	= (unsigned long)tx_desc;
666b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
667b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
668b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
669b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
670b8d26b3bSNicholas Bellinger 
671b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
672b8d26b3bSNicholas Bellinger 
673b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
674b8d26b3bSNicholas Bellinger 	if (ret) {
675b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send() failed, ret: %d\n", ret);
676b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
677b8d26b3bSNicholas Bellinger 	}
678b8d26b3bSNicholas Bellinger 
679b8d26b3bSNicholas Bellinger 	return ret;
680b8d26b3bSNicholas Bellinger }
681b8d26b3bSNicholas Bellinger 
682b8d26b3bSNicholas Bellinger static void
683b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
684b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
685b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
686b8d26b3bSNicholas Bellinger {
687b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
688b8d26b3bSNicholas Bellinger 
689b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
690b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
691b8d26b3bSNicholas Bellinger 
692b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
693b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
694b8d26b3bSNicholas Bellinger 
695b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
696b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
697b8d26b3bSNicholas Bellinger 
698b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
699b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
700b8d26b3bSNicholas Bellinger 		pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
701b8d26b3bSNicholas Bellinger 	}
702b8d26b3bSNicholas Bellinger }
703b8d26b3bSNicholas Bellinger 
704b8d26b3bSNicholas Bellinger static int
705b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
706b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
707b8d26b3bSNicholas Bellinger {
708b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
709b8d26b3bSNicholas Bellinger 	u64 dma_addr;
710b8d26b3bSNicholas Bellinger 
711b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
712b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
713b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
714b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error() failed\n");
715b8d26b3bSNicholas Bellinger 		return -ENOMEM;
716b8d26b3bSNicholas Bellinger 	}
717b8d26b3bSNicholas Bellinger 
718b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
719b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
720b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
721b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
722b8d26b3bSNicholas Bellinger 
723b8d26b3bSNicholas Bellinger 	pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
724b8d26b3bSNicholas Bellinger 		 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
725b8d26b3bSNicholas Bellinger 		 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
726b8d26b3bSNicholas Bellinger 
727b8d26b3bSNicholas Bellinger 	return 0;
728b8d26b3bSNicholas Bellinger }
729b8d26b3bSNicholas Bellinger 
730b8d26b3bSNicholas Bellinger static void
731b8d26b3bSNicholas Bellinger isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
732b8d26b3bSNicholas Bellinger {
733b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
734b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
735b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
736b8d26b3bSNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
737b8d26b3bSNicholas Bellinger 	send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
738b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
739b8d26b3bSNicholas Bellinger }
740b8d26b3bSNicholas Bellinger 
741b8d26b3bSNicholas Bellinger static int
742b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
743b8d26b3bSNicholas Bellinger {
744b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
745b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
746b8d26b3bSNicholas Bellinger 	int ret;
747b8d26b3bSNicholas Bellinger 
748b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
749b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
750b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
751b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
752b8d26b3bSNicholas Bellinger 
753b8d26b3bSNicholas Bellinger 	pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
754b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
755b8d26b3bSNicholas Bellinger 
756b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
757b8d26b3bSNicholas Bellinger 	rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
758b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
759b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
760b8d26b3bSNicholas Bellinger 
761b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
762b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
763b8d26b3bSNicholas Bellinger 	if (ret) {
764b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed: %d\n", ret);
765b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
766b8d26b3bSNicholas Bellinger 	}
767b8d26b3bSNicholas Bellinger 
768b8d26b3bSNicholas Bellinger 	pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
769b8d26b3bSNicholas Bellinger 	return ret;
770b8d26b3bSNicholas Bellinger }
771b8d26b3bSNicholas Bellinger 
772b8d26b3bSNicholas Bellinger static int
773b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
774b8d26b3bSNicholas Bellinger 		   u32 length)
775b8d26b3bSNicholas Bellinger {
776b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
777b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
778b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
779b8d26b3bSNicholas Bellinger 	int ret;
780b8d26b3bSNicholas Bellinger 
781b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
782b8d26b3bSNicholas Bellinger 
783b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
784b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
785b8d26b3bSNicholas Bellinger 
786b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
787b8d26b3bSNicholas Bellinger 
788b8d26b3bSNicholas Bellinger 	if (length > 0) {
789b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
790b8d26b3bSNicholas Bellinger 
791b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
792b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
793b8d26b3bSNicholas Bellinger 
794b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
795b8d26b3bSNicholas Bellinger 
796b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
797b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
798b8d26b3bSNicholas Bellinger 
799b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
800b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
801b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
802b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
803b8d26b3bSNicholas Bellinger 	}
804b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
805b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
806b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
807b8d26b3bSNicholas Bellinger 			if (ret)
808b8d26b3bSNicholas Bellinger 				return ret;
809b8d26b3bSNicholas Bellinger 
810b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
811b8d26b3bSNicholas Bellinger 			if (ret)
812b8d26b3bSNicholas Bellinger 				return ret;
813b8d26b3bSNicholas Bellinger 
814b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_UP;
815b8d26b3bSNicholas Bellinger 			goto post_send;
816b8d26b3bSNicholas Bellinger 		}
817b8d26b3bSNicholas Bellinger 
818b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
819b8d26b3bSNicholas Bellinger 		if (ret)
820b8d26b3bSNicholas Bellinger 			return ret;
821b8d26b3bSNicholas Bellinger 	}
822b8d26b3bSNicholas Bellinger post_send:
823b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
824b8d26b3bSNicholas Bellinger 	if (ret)
825b8d26b3bSNicholas Bellinger 		return ret;
826b8d26b3bSNicholas Bellinger 
827b8d26b3bSNicholas Bellinger 	return 0;
828b8d26b3bSNicholas Bellinger }
829b8d26b3bSNicholas Bellinger 
830b8d26b3bSNicholas Bellinger static void
831b8d26b3bSNicholas Bellinger isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
832b8d26b3bSNicholas Bellinger 		   struct isert_conn *isert_conn)
833b8d26b3bSNicholas Bellinger {
834b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
835b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
836b8d26b3bSNicholas Bellinger 	int size;
837b8d26b3bSNicholas Bellinger 
838b8d26b3bSNicholas Bellinger 	if (!login) {
839b8d26b3bSNicholas Bellinger 		pr_err("conn->conn_login is NULL\n");
840b8d26b3bSNicholas Bellinger 		dump_stack();
841b8d26b3bSNicholas Bellinger 		return;
842b8d26b3bSNicholas Bellinger 	}
843b8d26b3bSNicholas Bellinger 
844b8d26b3bSNicholas Bellinger 	if (login->first_request) {
845b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
846b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
847b8d26b3bSNicholas Bellinger 		/*
848b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
849b8d26b3bSNicholas Bellinger 		 * login request PDU.
850b8d26b3bSNicholas Bellinger 		 */
851b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
852b8d26b3bSNicholas Bellinger 		login->current_stage =
853b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
854b8d26b3bSNicholas Bellinger 			 >> 2;
855b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
856b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
857b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
858b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
859b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
860b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
861b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
862b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
863b8d26b3bSNicholas Bellinger 	}
864b8d26b3bSNicholas Bellinger 
865b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
866b8d26b3bSNicholas Bellinger 
867b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
868b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
869b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
870b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
871b8d26b3bSNicholas Bellinger 
872b8d26b3bSNicholas Bellinger 	complete(&isert_conn->conn_login_comp);
873b8d26b3bSNicholas Bellinger }
874b8d26b3bSNicholas Bellinger 
875b8d26b3bSNicholas Bellinger static void
876b8d26b3bSNicholas Bellinger isert_release_cmd(struct iscsi_cmd *cmd)
877b8d26b3bSNicholas Bellinger {
878b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd, struct isert_cmd,
879b8d26b3bSNicholas Bellinger 						   iscsi_cmd);
880b8d26b3bSNicholas Bellinger 
881b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_release_cmd %p >>>>>>>>>>>>>>>.\n", isert_cmd);
882b8d26b3bSNicholas Bellinger 
883b8d26b3bSNicholas Bellinger 	kfree(cmd->buf_ptr);
884b8d26b3bSNicholas Bellinger 	kfree(cmd->tmr_req);
885b8d26b3bSNicholas Bellinger 
886b8d26b3bSNicholas Bellinger 	kmem_cache_free(isert_cmd_cache, isert_cmd);
887b8d26b3bSNicholas Bellinger }
888b8d26b3bSNicholas Bellinger 
889b8d26b3bSNicholas Bellinger static struct iscsi_cmd
890b8d26b3bSNicholas Bellinger *isert_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp)
891b8d26b3bSNicholas Bellinger {
892b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
893b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
894b8d26b3bSNicholas Bellinger 
895b8d26b3bSNicholas Bellinger 	isert_cmd = kmem_cache_zalloc(isert_cmd_cache, gfp);
896b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
897b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_cmd\n");
898b8d26b3bSNicholas Bellinger 		return NULL;
899b8d26b3bSNicholas Bellinger 	}
900b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
901b8d26b3bSNicholas Bellinger 	isert_cmd->iscsi_cmd.release_cmd = &isert_release_cmd;
902b8d26b3bSNicholas Bellinger 
903b8d26b3bSNicholas Bellinger 	return &isert_cmd->iscsi_cmd;
904b8d26b3bSNicholas Bellinger }
905b8d26b3bSNicholas Bellinger 
906b8d26b3bSNicholas Bellinger static int
907b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
908b8d26b3bSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iser_rx_desc *rx_desc,
909b8d26b3bSNicholas Bellinger 		      unsigned char *buf)
910b8d26b3bSNicholas Bellinger {
911b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
912b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
913b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
914b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
915b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
916b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
917b8d26b3bSNicholas Bellinger 
918b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
919b8d26b3bSNicholas Bellinger 	if (rc < 0)
920b8d26b3bSNicholas Bellinger 		return rc;
921b8d26b3bSNicholas Bellinger 
922b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
923b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
924b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
925b8d26b3bSNicholas Bellinger 
926b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
927b8d26b3bSNicholas Bellinger 	if (rc < 0) {
928b8d26b3bSNicholas Bellinger 		return 0;
929b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
930b8d26b3bSNicholas Bellinger 		dump_payload = true;
931b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
932b8d26b3bSNicholas Bellinger 	}
933b8d26b3bSNicholas Bellinger 
934b8d26b3bSNicholas Bellinger 	if (!imm_data)
935b8d26b3bSNicholas Bellinger 		return 0;
936b8d26b3bSNicholas Bellinger 
937b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
938b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
939b8d26b3bSNicholas Bellinger 
940b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
941b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
942b8d26b3bSNicholas Bellinger 
943b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
944b8d26b3bSNicholas Bellinger 
945b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
946b8d26b3bSNicholas Bellinger 
947b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
948b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
949b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
950b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
951b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
952b8d26b3bSNicholas Bellinger 	}
953b8d26b3bSNicholas Bellinger 
954b8d26b3bSNicholas Bellinger sequence_cmd:
955561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
956b8d26b3bSNicholas Bellinger 
957b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
958b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
959b8d26b3bSNicholas Bellinger 
960b8d26b3bSNicholas Bellinger 	return 0;
961b8d26b3bSNicholas Bellinger }
962b8d26b3bSNicholas Bellinger 
963b8d26b3bSNicholas Bellinger static int
964b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
965b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
966b8d26b3bSNicholas Bellinger {
967b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
968b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
969b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
970b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
971b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
972b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
973b8d26b3bSNicholas Bellinger 
974b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
975b8d26b3bSNicholas Bellinger 	if (rc < 0)
976b8d26b3bSNicholas Bellinger 		return rc;
977b8d26b3bSNicholas Bellinger 	else if (!cmd)
978b8d26b3bSNicholas Bellinger 		return 0;
979b8d26b3bSNicholas Bellinger 	/*
980b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
981b8d26b3bSNicholas Bellinger 	 */
982b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
983b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
984b8d26b3bSNicholas Bellinger 		dump_stack();
985b8d26b3bSNicholas Bellinger 		return -1;
986b8d26b3bSNicholas Bellinger 	}
987b8d26b3bSNicholas Bellinger 
988b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
989b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
990b8d26b3bSNicholas Bellinger 
991b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
992b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
993b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
994b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
995b8d26b3bSNicholas Bellinger 	/*
996b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
997b8d26b3bSNicholas Bellinger 	 */
998b8d26b3bSNicholas Bellinger 	if (page_off) {
999b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1000b8d26b3bSNicholas Bellinger 		dump_stack();
1001b8d26b3bSNicholas Bellinger 		return -1;
1002b8d26b3bSNicholas Bellinger 	}
1003b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1004b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1005b8d26b3bSNicholas Bellinger 
1006b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1007b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1008b8d26b3bSNicholas Bellinger 
1009b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1010b8d26b3bSNicholas Bellinger 	if (rc < 0)
1011b8d26b3bSNicholas Bellinger 		return rc;
1012b8d26b3bSNicholas Bellinger 
1013b8d26b3bSNicholas Bellinger 	return 0;
1014b8d26b3bSNicholas Bellinger }
1015b8d26b3bSNicholas Bellinger 
1016b8d26b3bSNicholas Bellinger static int
1017778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1018778de368SNicholas Bellinger 		     struct iser_rx_desc *rx_desc, unsigned char *buf)
1019778de368SNicholas Bellinger {
1020778de368SNicholas Bellinger 	struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1021778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1022778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1023778de368SNicholas Bellinger 	int rc;
1024778de368SNicholas Bellinger 
1025778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1026778de368SNicholas Bellinger 	if (rc < 0)
1027778de368SNicholas Bellinger 		return rc;
1028778de368SNicholas Bellinger 	/*
1029778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1030778de368SNicholas Bellinger 	 */
1031778de368SNicholas Bellinger 
1032778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1033778de368SNicholas Bellinger }
1034778de368SNicholas Bellinger 
1035778de368SNicholas Bellinger static int
1036*adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1037*adb54c29SNicholas Bellinger 		      struct iser_rx_desc *rx_desc, struct iscsi_text *hdr)
1038*adb54c29SNicholas Bellinger {
1039*adb54c29SNicholas Bellinger 	struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1040*adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1041*adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1042*adb54c29SNicholas Bellinger 	int rc;
1043*adb54c29SNicholas Bellinger 	unsigned char *text_in;
1044*adb54c29SNicholas Bellinger 
1045*adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1046*adb54c29SNicholas Bellinger 	if (rc < 0)
1047*adb54c29SNicholas Bellinger 		return rc;
1048*adb54c29SNicholas Bellinger 
1049*adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1050*adb54c29SNicholas Bellinger 	if (!text_in) {
1051*adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1052*adb54c29SNicholas Bellinger 		       payload_length);
1053*adb54c29SNicholas Bellinger 		return -ENOMEM;
1054*adb54c29SNicholas Bellinger 	}
1055*adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1056*adb54c29SNicholas Bellinger 
1057*adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1058*adb54c29SNicholas Bellinger 
1059*adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1060*adb54c29SNicholas Bellinger }
1061*adb54c29SNicholas Bellinger 
1062*adb54c29SNicholas Bellinger static int
1063b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1064b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1065b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1066b8d26b3bSNicholas Bellinger {
1067b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1068b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1069b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1070b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1071b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1072b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1073b8d26b3bSNicholas Bellinger 
1074b8d26b3bSNicholas Bellinger 	switch (opcode) {
1075b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1076b8d26b3bSNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1077b8d26b3bSNicholas Bellinger 		if (!cmd)
1078b8d26b3bSNicholas Bellinger 			break;
1079b8d26b3bSNicholas Bellinger 
1080b8d26b3bSNicholas Bellinger 		isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
1081b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1082b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1083b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1084b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1085b8d26b3bSNicholas Bellinger 
1086b8d26b3bSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd,
1087b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1088b8d26b3bSNicholas Bellinger 		break;
1089b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1090b8d26b3bSNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1091b8d26b3bSNicholas Bellinger 		if (!cmd)
1092b8d26b3bSNicholas Bellinger 			break;
1093b8d26b3bSNicholas Bellinger 
1094778de368SNicholas Bellinger 		isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
1095778de368SNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd,
1096778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1097b8d26b3bSNicholas Bellinger 		break;
1098b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1099b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1100b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1101b8d26b3bSNicholas Bellinger 		break;
1102b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1103b8d26b3bSNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1104b8d26b3bSNicholas Bellinger 		if (!cmd)
1105b8d26b3bSNicholas Bellinger 			break;
1106b8d26b3bSNicholas Bellinger 
1107b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1108b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1109b8d26b3bSNicholas Bellinger 		break;
1110b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1111b8d26b3bSNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1112b8d26b3bSNicholas Bellinger 		if (!cmd)
1113b8d26b3bSNicholas Bellinger 			break;
1114b8d26b3bSNicholas Bellinger 
1115b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1116b8d26b3bSNicholas Bellinger 		if (ret > 0)
1117b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1118b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1119b8d26b3bSNicholas Bellinger 						    HZ);
1120b8d26b3bSNicholas Bellinger 		break;
1121*adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1122*adb54c29SNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1123*adb54c29SNicholas Bellinger 		if (!cmd)
1124*adb54c29SNicholas Bellinger 			break;
1125*adb54c29SNicholas Bellinger 
1126*adb54c29SNicholas Bellinger 		isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
1127*adb54c29SNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd,
1128*adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1129*adb54c29SNicholas Bellinger 		break;
1130b8d26b3bSNicholas Bellinger 	default:
1131b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1132b8d26b3bSNicholas Bellinger 		dump_stack();
1133b8d26b3bSNicholas Bellinger 		break;
1134b8d26b3bSNicholas Bellinger 	}
1135b8d26b3bSNicholas Bellinger 
1136b8d26b3bSNicholas Bellinger 	return ret;
1137b8d26b3bSNicholas Bellinger }
1138b8d26b3bSNicholas Bellinger 
1139b8d26b3bSNicholas Bellinger static void
1140b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1141b8d26b3bSNicholas Bellinger {
1142b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1143b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1144b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1145b8d26b3bSNicholas Bellinger 	int rc;
1146b8d26b3bSNicholas Bellinger 
1147b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1148b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1149b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1150b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1151b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1152b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1153b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1154b8d26b3bSNicholas Bellinger 		}
1155b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1156b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1157b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1158b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1159b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1160b8d26b3bSNicholas Bellinger 		}
1161b8d26b3bSNicholas Bellinger 
1162b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1163b8d26b3bSNicholas Bellinger 		break;
1164b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1165b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1166b8d26b3bSNicholas Bellinger 		break;
1167b8d26b3bSNicholas Bellinger 	default:
1168b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1169b8d26b3bSNicholas Bellinger 		break;
1170b8d26b3bSNicholas Bellinger 	}
1171b8d26b3bSNicholas Bellinger 
1172b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1173b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1174b8d26b3bSNicholas Bellinger }
1175b8d26b3bSNicholas Bellinger 
1176b8d26b3bSNicholas Bellinger static void
1177b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1178b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1179b8d26b3bSNicholas Bellinger {
1180b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1181b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1182b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1183b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1184b8d26b3bSNicholas Bellinger 
1185b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1186b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1187b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1188b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1189b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1190b8d26b3bSNicholas Bellinger 	} else {
1191b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1192b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1193b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1194b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1195b8d26b3bSNicholas Bellinger 	}
1196b8d26b3bSNicholas Bellinger 
1197b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1198b8d26b3bSNicholas Bellinger 
1199b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1200b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1201b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1202b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1203b8d26b3bSNicholas Bellinger 
1204b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1205b8d26b3bSNicholas Bellinger 		isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1206b8d26b3bSNicholas Bellinger 				   isert_conn);
1207b8d26b3bSNicholas Bellinger 	else
1208b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
1209b8d26b3bSNicholas Bellinger 
1210b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1211b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1212b8d26b3bSNicholas Bellinger 
1213b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1214b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1215b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1216b8d26b3bSNicholas Bellinger 
1217b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1218b8d26b3bSNicholas Bellinger 		return;
1219b8d26b3bSNicholas Bellinger 
1220b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1221b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1222b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1223b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1224b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1225b8d26b3bSNicholas Bellinger 		if (err) {
1226b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1227b8d26b3bSNicholas Bellinger 			       count, err);
1228b8d26b3bSNicholas Bellinger 		}
1229b8d26b3bSNicholas Bellinger 	}
1230b8d26b3bSNicholas Bellinger }
1231b8d26b3bSNicholas Bellinger 
1232b8d26b3bSNicholas Bellinger static void
1233b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1234b8d26b3bSNicholas Bellinger {
1235b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1236b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1237b8d26b3bSNicholas Bellinger 
1238b8d26b3bSNicholas Bellinger 	pr_debug("isert_unmap_cmd >>>>>>>>>>>>>>>>>>>>>>>\n");
1239b8d26b3bSNicholas Bellinger 
1240b8d26b3bSNicholas Bellinger 	if (wr->sge) {
1241b8d26b3bSNicholas Bellinger 		ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1242b8d26b3bSNicholas Bellinger 		wr->sge = NULL;
1243b8d26b3bSNicholas Bellinger 	}
1244b8d26b3bSNicholas Bellinger 
1245b8d26b3bSNicholas Bellinger 	kfree(wr->send_wr);
1246b8d26b3bSNicholas Bellinger 	wr->send_wr = NULL;
1247b8d26b3bSNicholas Bellinger 
1248b8d26b3bSNicholas Bellinger 	kfree(isert_cmd->ib_sge);
1249b8d26b3bSNicholas Bellinger 	isert_cmd->ib_sge = NULL;
1250b8d26b3bSNicholas Bellinger }
1251b8d26b3bSNicholas Bellinger 
1252b8d26b3bSNicholas Bellinger static void
1253b8d26b3bSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd)
1254b8d26b3bSNicholas Bellinger {
1255b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1256b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1257186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1258b8d26b3bSNicholas Bellinger 
1259b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1260b8d26b3bSNicholas Bellinger 
1261b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1262b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1263b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1264b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
1265b8d26b3bSNicholas Bellinger 			list_del(&cmd->i_conn_node);
1266b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1267b8d26b3bSNicholas Bellinger 
1268b8d26b3bSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE)
1269b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
1270b8d26b3bSNicholas Bellinger 
1271b8d26b3bSNicholas Bellinger 		isert_unmap_cmd(isert_cmd, isert_conn);
1272186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1273186a9647SNicholas Bellinger 		break;
1274b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1275186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1276186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
1277186a9647SNicholas Bellinger 			list_del(&cmd->i_conn_node);
1278186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1279186a9647SNicholas Bellinger 
1280b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1281b8d26b3bSNicholas Bellinger 		break;
1282b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1283b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1284*adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1285b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1286b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
1287b8d26b3bSNicholas Bellinger 			list_del(&cmd->i_conn_node);
1288b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1289b8d26b3bSNicholas Bellinger 
1290b8d26b3bSNicholas Bellinger 		/*
1291b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1292b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1293b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1294b8d26b3bSNicholas Bellinger 		 */
1295b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
12963df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
12973df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
12983df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1299b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1300b8d26b3bSNicholas Bellinger 			break;
1301b8d26b3bSNicholas Bellinger 		}
1302b8d26b3bSNicholas Bellinger 		/*
1303b8d26b3bSNicholas Bellinger 		 * Fall-through
1304b8d26b3bSNicholas Bellinger 		 */
1305b8d26b3bSNicholas Bellinger 	default:
1306b8d26b3bSNicholas Bellinger 		isert_release_cmd(cmd);
1307b8d26b3bSNicholas Bellinger 		break;
1308b8d26b3bSNicholas Bellinger 	}
1309b8d26b3bSNicholas Bellinger }
1310b8d26b3bSNicholas Bellinger 
1311b8d26b3bSNicholas Bellinger static void
1312b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1313b8d26b3bSNicholas Bellinger {
1314b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1315b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1316b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1317b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1318b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1319b8d26b3bSNicholas Bellinger 	}
1320b8d26b3bSNicholas Bellinger }
1321b8d26b3bSNicholas Bellinger 
1322b8d26b3bSNicholas Bellinger static void
1323b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1324b8d26b3bSNicholas Bellinger 		     struct ib_device *ib_dev)
1325b8d26b3bSNicholas Bellinger {
1326dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1327dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1328dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1329dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1330dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1331b8d26b3bSNicholas Bellinger 	}
1332b8d26b3bSNicholas Bellinger 
1333b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
1334b8d26b3bSNicholas Bellinger 	isert_put_cmd(isert_cmd);
1335b8d26b3bSNicholas Bellinger }
1336b8d26b3bSNicholas Bellinger 
1337b8d26b3bSNicholas Bellinger static void
1338b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1339b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1340b8d26b3bSNicholas Bellinger {
1341b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1342b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1343b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
1344b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_cmd->conn->conn_cm_id->device;
1345b8d26b3bSNicholas Bellinger 
1346b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1347b8d26b3bSNicholas Bellinger 
1348b8d26b3bSNicholas Bellinger 	if (wr->sge) {
1349b8d26b3bSNicholas Bellinger 		pr_debug("isert_do_rdma_read_comp: Unmapping wr->sge from t_data_sg\n");
1350b8d26b3bSNicholas Bellinger 		ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1351b8d26b3bSNicholas Bellinger 		wr->sge = NULL;
1352b8d26b3bSNicholas Bellinger 	}
1353b8d26b3bSNicholas Bellinger 
1354b8d26b3bSNicholas Bellinger 	if (isert_cmd->ib_sge) {
1355b8d26b3bSNicholas Bellinger 		pr_debug("isert_do_rdma_read_comp: Freeing isert_cmd->ib_sge\n");
1356b8d26b3bSNicholas Bellinger 		kfree(isert_cmd->ib_sge);
1357b8d26b3bSNicholas Bellinger 		isert_cmd->ib_sge = NULL;
1358b8d26b3bSNicholas Bellinger 	}
1359b8d26b3bSNicholas Bellinger 
1360b8d26b3bSNicholas Bellinger 	cmd->write_data_done = se_cmd->data_length;
1361b8d26b3bSNicholas Bellinger 
1362b8d26b3bSNicholas Bellinger 	pr_debug("isert_do_rdma_read_comp, calling target_execute_cmd\n");
1363b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1364b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1365b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1366b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1367b8d26b3bSNicholas Bellinger 
1368b8d26b3bSNicholas Bellinger 	target_execute_cmd(se_cmd);
1369b8d26b3bSNicholas Bellinger }
1370b8d26b3bSNicholas Bellinger 
1371b8d26b3bSNicholas Bellinger static void
1372b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1373b8d26b3bSNicholas Bellinger {
1374b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1375b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1376b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1377b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1378b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1379b8d26b3bSNicholas Bellinger 
1380b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1381b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1382b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1383b8d26b3bSNicholas Bellinger 
1384b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1385b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1386b8d26b3bSNicholas Bellinger 
1387b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1388b8d26b3bSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1389b8d26b3bSNicholas Bellinger 		break;
1390b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1391b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1392b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1393b8d26b3bSNicholas Bellinger 
1394b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1395b8d26b3bSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
13963df8f68aSNicholas Bellinger 		break;
1397b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1398b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1399b8d26b3bSNicholas Bellinger 		/*
1400b8d26b3bSNicholas Bellinger 		 * Call atomic_dec(&isert_conn->post_send_buf_count)
1401b8d26b3bSNicholas Bellinger 		 * from isert_free_conn()
1402b8d26b3bSNicholas Bellinger 		 */
1403b8d26b3bSNicholas Bellinger 		isert_conn->logout_posted = true;
1404b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1405b8d26b3bSNicholas Bellinger 		break;
1406*adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1407*adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1408*adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
1409*adb54c29SNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1410*adb54c29SNicholas Bellinger 		break;
1411b8d26b3bSNicholas Bellinger 	default:
1412b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1413b8d26b3bSNicholas Bellinger 		dump_stack();
1414b8d26b3bSNicholas Bellinger 		break;
1415b8d26b3bSNicholas Bellinger 	}
1416b8d26b3bSNicholas Bellinger }
1417b8d26b3bSNicholas Bellinger 
1418b8d26b3bSNicholas Bellinger static void
1419b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1420b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1421b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1422b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1423b8d26b3bSNicholas Bellinger {
1424b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1425b8d26b3bSNicholas Bellinger 
1426b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
14273df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1428*adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1429*adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1430b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1431b8d26b3bSNicholas Bellinger 
1432b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1433b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1434b8d26b3bSNicholas Bellinger 		return;
1435b8d26b3bSNicholas Bellinger 	}
1436b8d26b3bSNicholas Bellinger 	atomic_dec(&isert_conn->post_send_buf_count);
1437b8d26b3bSNicholas Bellinger 
1438b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
1439b8d26b3bSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev);
1440b8d26b3bSNicholas Bellinger }
1441b8d26b3bSNicholas Bellinger 
1442b8d26b3bSNicholas Bellinger static void
1443b8d26b3bSNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
1444b8d26b3bSNicholas Bellinger 		      struct isert_conn *isert_conn)
1445b8d26b3bSNicholas Bellinger {
1446b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1447b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1448b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1449b8d26b3bSNicholas Bellinger 
1450b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1451b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1452b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1453b8d26b3bSNicholas Bellinger 		return;
1454b8d26b3bSNicholas Bellinger 	}
1455b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1456b8d26b3bSNicholas Bellinger 
1457b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1458b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
1459b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1460b8d26b3bSNicholas Bellinger 		dump_stack();
1461b8d26b3bSNicholas Bellinger 		break;
1462b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1463b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1464b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1465b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1466b8d26b3bSNicholas Bellinger 		break;
1467b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1468b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1469b8d26b3bSNicholas Bellinger 		dump_stack();
1470b8d26b3bSNicholas Bellinger 		break;
1471b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1472b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1473b8d26b3bSNicholas Bellinger 
1474b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1475b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1476b8d26b3bSNicholas Bellinger 		break;
1477b8d26b3bSNicholas Bellinger 	default:
1478b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1479b8d26b3bSNicholas Bellinger 		dump_stack();
1480b8d26b3bSNicholas Bellinger 		break;
1481b8d26b3bSNicholas Bellinger 	}
1482b8d26b3bSNicholas Bellinger }
1483b8d26b3bSNicholas Bellinger 
1484b8d26b3bSNicholas Bellinger static void
1485b8d26b3bSNicholas Bellinger isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1486b8d26b3bSNicholas Bellinger {
1487b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1488b8d26b3bSNicholas Bellinger 
1489b8d26b3bSNicholas Bellinger 	if (tx_desc) {
1490b8d26b3bSNicholas Bellinger 		struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1491b8d26b3bSNicholas Bellinger 
1492b8d26b3bSNicholas Bellinger 		if (!isert_cmd)
1493b8d26b3bSNicholas Bellinger 			isert_unmap_tx_desc(tx_desc, ib_dev);
1494b8d26b3bSNicholas Bellinger 		else
1495b8d26b3bSNicholas Bellinger 			isert_completion_put(tx_desc, isert_cmd, ib_dev);
1496b8d26b3bSNicholas Bellinger 	}
1497b8d26b3bSNicholas Bellinger 
1498b8d26b3bSNicholas Bellinger 	if (isert_conn->post_recv_buf_count == 0 &&
1499b8d26b3bSNicholas Bellinger 	    atomic_read(&isert_conn->post_send_buf_count) == 0) {
1500b8d26b3bSNicholas Bellinger 		pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1501b8d26b3bSNicholas Bellinger 		pr_debug("Calling wake_up from isert_cq_comp_err\n");
1502b8d26b3bSNicholas Bellinger 
1503b2cb9649SNicholas Bellinger 		mutex_lock(&isert_conn->conn_mutex);
1504b2cb9649SNicholas Bellinger 		if (isert_conn->state != ISER_CONN_DOWN)
1505b8d26b3bSNicholas Bellinger 			isert_conn->state = ISER_CONN_TERMINATING;
1506b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
1507b2cb9649SNicholas Bellinger 
1508b8d26b3bSNicholas Bellinger 		wake_up(&isert_conn->conn_wait_comp_err);
1509b8d26b3bSNicholas Bellinger 	}
1510b8d26b3bSNicholas Bellinger }
1511b8d26b3bSNicholas Bellinger 
1512b8d26b3bSNicholas Bellinger static void
1513b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
1514b8d26b3bSNicholas Bellinger {
1515b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
1516b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
1517b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
1518b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
1519b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1520b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
1521b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
1522b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
1523b8d26b3bSNicholas Bellinger 
1524b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1525b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1526b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
1527b8d26b3bSNicholas Bellinger 
1528b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
1529b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
1530b8d26b3bSNicholas Bellinger 		} else {
1531b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1532b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
1533c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
1534b8d26b3bSNicholas Bellinger 			atomic_dec(&isert_conn->post_send_buf_count);
1535b8d26b3bSNicholas Bellinger 			isert_cq_comp_err(tx_desc, isert_conn);
1536b8d26b3bSNicholas Bellinger 		}
1537b8d26b3bSNicholas Bellinger 	}
1538b8d26b3bSNicholas Bellinger 
1539b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1540b8d26b3bSNicholas Bellinger }
1541b8d26b3bSNicholas Bellinger 
1542b8d26b3bSNicholas Bellinger static void
1543b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
1544b8d26b3bSNicholas Bellinger {
1545b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1546b8d26b3bSNicholas Bellinger 
1547b8d26b3bSNicholas Bellinger 	INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
1548b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1549b8d26b3bSNicholas Bellinger }
1550b8d26b3bSNicholas Bellinger 
1551b8d26b3bSNicholas Bellinger static void
1552b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
1553b8d26b3bSNicholas Bellinger {
1554b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
1555b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
1556b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
1557b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
1558b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1559b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
1560b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
1561b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
1562b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
1563b8d26b3bSNicholas Bellinger 
1564b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1565b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1566b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
1567b8d26b3bSNicholas Bellinger 
1568b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
1569b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
1570b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
1571b8d26b3bSNicholas Bellinger 		} else {
1572b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1573c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
1574b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
1575c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
1576c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
1577c5a2adbfSNicholas Bellinger 			}
1578b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
1579b8d26b3bSNicholas Bellinger 			isert_cq_comp_err(NULL, isert_conn);
1580b8d26b3bSNicholas Bellinger 		}
1581b8d26b3bSNicholas Bellinger 	}
1582b8d26b3bSNicholas Bellinger 
1583b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1584b8d26b3bSNicholas Bellinger }
1585b8d26b3bSNicholas Bellinger 
1586b8d26b3bSNicholas Bellinger static void
1587b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
1588b8d26b3bSNicholas Bellinger {
1589b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1590b8d26b3bSNicholas Bellinger 
1591b8d26b3bSNicholas Bellinger 	INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
1592b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1593b8d26b3bSNicholas Bellinger }
1594b8d26b3bSNicholas Bellinger 
1595b8d26b3bSNicholas Bellinger static int
1596b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1597b8d26b3bSNicholas Bellinger {
1598b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
1599b8d26b3bSNicholas Bellinger 	int ret;
1600b8d26b3bSNicholas Bellinger 
1601b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
1602b8d26b3bSNicholas Bellinger 
1603b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1604b8d26b3bSNicholas Bellinger 			   &wr_failed);
1605b8d26b3bSNicholas Bellinger 	if (ret) {
1606b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
1607b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1608b8d26b3bSNicholas Bellinger 		return ret;
1609b8d26b3bSNicholas Bellinger 	}
1610b8d26b3bSNicholas Bellinger 	return ret;
1611b8d26b3bSNicholas Bellinger }
1612b8d26b3bSNicholas Bellinger 
1613b8d26b3bSNicholas Bellinger static int
1614b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1615b8d26b3bSNicholas Bellinger {
1616b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd,
1617b8d26b3bSNicholas Bellinger 					struct isert_cmd, iscsi_cmd);
1618b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1619b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1620b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1621b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
1622b8d26b3bSNicholas Bellinger 
1623b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1624b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1625b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1626b8d26b3bSNicholas Bellinger 	/*
1627b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
1628b8d26b3bSNicholas Bellinger 	 */
1629b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
1630b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1631b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1632b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1633b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1634dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
1635b8d26b3bSNicholas Bellinger 
1636b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1637b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
1638b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1639b8d26b3bSNicholas Bellinger 
1640b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1641b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1642dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1643b8d26b3bSNicholas Bellinger 
1644dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1645dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
1646b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
1647b8d26b3bSNicholas Bellinger 
1648dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
1649dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1650dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
1651b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1652b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1653b8d26b3bSNicholas Bellinger 	}
1654b8d26b3bSNicholas Bellinger 
1655b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1656b8d26b3bSNicholas Bellinger 
1657b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1658b8d26b3bSNicholas Bellinger 
1659b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1660b8d26b3bSNicholas Bellinger }
1661b8d26b3bSNicholas Bellinger 
1662b8d26b3bSNicholas Bellinger static int
1663b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1664b8d26b3bSNicholas Bellinger 		bool nopout_response)
1665b8d26b3bSNicholas Bellinger {
1666b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd,
1667b8d26b3bSNicholas Bellinger 				struct isert_cmd, iscsi_cmd);
1668b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1669b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1670b8d26b3bSNicholas Bellinger 
1671b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1672b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1673b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
1674b8d26b3bSNicholas Bellinger 			       nopout_response);
1675b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1676b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1677b8d26b3bSNicholas Bellinger 
1678b8d26b3bSNicholas Bellinger 	pr_debug("Posting NOPIN Reponse IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1679b8d26b3bSNicholas Bellinger 
1680b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1681b8d26b3bSNicholas Bellinger }
1682b8d26b3bSNicholas Bellinger 
1683b8d26b3bSNicholas Bellinger static int
1684b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1685b8d26b3bSNicholas Bellinger {
1686b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd,
1687b8d26b3bSNicholas Bellinger 				struct isert_cmd, iscsi_cmd);
1688b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1689b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1690b8d26b3bSNicholas Bellinger 
1691b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1692b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1693b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
1694b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1695b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1696b8d26b3bSNicholas Bellinger 
1697b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1698b8d26b3bSNicholas Bellinger 
1699b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1700b8d26b3bSNicholas Bellinger }
1701b8d26b3bSNicholas Bellinger 
1702b8d26b3bSNicholas Bellinger static int
1703b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1704b8d26b3bSNicholas Bellinger {
1705b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd,
1706b8d26b3bSNicholas Bellinger 				struct isert_cmd, iscsi_cmd);
1707b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1708b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1709b8d26b3bSNicholas Bellinger 
1710b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1711b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1712b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
1713b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1714b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1715b8d26b3bSNicholas Bellinger 
1716b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1717b8d26b3bSNicholas Bellinger 
1718b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1719b8d26b3bSNicholas Bellinger }
1720b8d26b3bSNicholas Bellinger 
1721b8d26b3bSNicholas Bellinger static int
1722b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1723b8d26b3bSNicholas Bellinger {
1724b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd,
1725b8d26b3bSNicholas Bellinger 				struct isert_cmd, iscsi_cmd);
1726b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1727b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
17283df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
17293df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
17303df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
17313df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1732b8d26b3bSNicholas Bellinger 
1733b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
17343df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
1735b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
17363df8f68aSNicholas Bellinger 
17373df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
1738dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
17393df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
17403df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
1741dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1742dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
17433df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
17443df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
17453df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
17463df8f68aSNicholas Bellinger 
1747b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1748b8d26b3bSNicholas Bellinger 
1749b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1750b8d26b3bSNicholas Bellinger 
1751b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1752b8d26b3bSNicholas Bellinger }
1753b8d26b3bSNicholas Bellinger 
1754b8d26b3bSNicholas Bellinger static int
1755*adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1756*adb54c29SNicholas Bellinger {
1757*adb54c29SNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd,
1758*adb54c29SNicholas Bellinger 				struct isert_cmd, iscsi_cmd);
1759*adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1760*adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1761*adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
1762*adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1763*adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
1764*adb54c29SNicholas Bellinger 	int rc;
1765*adb54c29SNicholas Bellinger 
1766*adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1767*adb54c29SNicholas Bellinger 	rc = iscsit_build_text_rsp(cmd, conn, hdr);
1768*adb54c29SNicholas Bellinger 	if (rc < 0)
1769*adb54c29SNicholas Bellinger 		return rc;
1770*adb54c29SNicholas Bellinger 
1771*adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
1772*adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1773*adb54c29SNicholas Bellinger 
1774*adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
1775*adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1776*adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1777*adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
1778*adb54c29SNicholas Bellinger 
1779*adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1780*adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1781*adb54c29SNicholas Bellinger 
1782*adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
1783*adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
1784*adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
1785*adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1786*adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
1787*adb54c29SNicholas Bellinger 	}
1788*adb54c29SNicholas Bellinger 	isert_init_send_wr(isert_cmd, send_wr);
1789*adb54c29SNicholas Bellinger 
1790*adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1791*adb54c29SNicholas Bellinger 
1792*adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
1793*adb54c29SNicholas Bellinger }
1794*adb54c29SNicholas Bellinger 
1795*adb54c29SNicholas Bellinger static int
1796b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1797b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1798b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
1799b8d26b3bSNicholas Bellinger {
1800b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1801b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
1802b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1803b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
1804b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
1805b8d26b3bSNicholas Bellinger 
1806b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
1807b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1808b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1809b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
1810b8d26b3bSNicholas Bellinger 
1811b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
1812b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
1813b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1814b8d26b3bSNicholas Bellinger 	/*
1815b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1816b8d26b3bSNicholas Bellinger 	 */
1817b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1818b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1819b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
1820b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
1821b8d26b3bSNicholas Bellinger 
1822b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1823b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
1824b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
1825b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
1826b8d26b3bSNicholas Bellinger 
1827b8d26b3bSNicholas Bellinger 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u\n",
1828b8d26b3bSNicholas Bellinger 			 ib_sge->addr, ib_sge->length);
1829b8d26b3bSNicholas Bellinger 		page_off = 0;
1830b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
1831b8d26b3bSNicholas Bellinger 		ib_sge++;
1832b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
1833b8d26b3bSNicholas Bellinger 	}
1834b8d26b3bSNicholas Bellinger 
1835b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
1836b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
1837b8d26b3bSNicholas Bellinger 
1838b8d26b3bSNicholas Bellinger 	return sg_nents;
1839b8d26b3bSNicholas Bellinger }
1840b8d26b3bSNicholas Bellinger 
1841b8d26b3bSNicholas Bellinger static int
1842b8d26b3bSNicholas Bellinger isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1843b8d26b3bSNicholas Bellinger {
1844b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
1845b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd,
1846b8d26b3bSNicholas Bellinger 					struct isert_cmd, iscsi_cmd);
1847b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1848b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1849b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed, *send_wr;
1850b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1851b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
1852b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1853b8d26b3bSNicholas Bellinger 	u32 offset = 0, data_len, data_left, rdma_write_max;
1854b8d26b3bSNicholas Bellinger 	int rc, ret = 0, count, sg_nents, i, ib_sge_cnt;
1855b8d26b3bSNicholas Bellinger 
1856b8d26b3bSNicholas Bellinger 	pr_debug("RDMA_WRITE: data_length: %u\n", se_cmd->data_length);
1857b8d26b3bSNicholas Bellinger 
1858b8d26b3bSNicholas Bellinger 	sg = &se_cmd->t_data_sg[0];
1859b8d26b3bSNicholas Bellinger 	sg_nents = se_cmd->t_data_nents;
1860b8d26b3bSNicholas Bellinger 
1861b8d26b3bSNicholas Bellinger 	count = ib_dma_map_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1862b8d26b3bSNicholas Bellinger 	if (unlikely(!count)) {
1863b8d26b3bSNicholas Bellinger 		pr_err("Unable to map put_datain SGs\n");
1864b8d26b3bSNicholas Bellinger 		return -EINVAL;
1865b8d26b3bSNicholas Bellinger 	}
1866b8d26b3bSNicholas Bellinger 	wr->sge = sg;
1867b8d26b3bSNicholas Bellinger 	wr->num_sge = sg_nents;
1868b8d26b3bSNicholas Bellinger 	pr_debug("Mapped IB count: %u sg: %p sg_nents: %u for RDMA_WRITE\n",
1869b8d26b3bSNicholas Bellinger 		 count, sg, sg_nents);
1870b8d26b3bSNicholas Bellinger 
1871b8d26b3bSNicholas Bellinger 	ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1872b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
1873b8d26b3bSNicholas Bellinger 		pr_warn("Unable to allocate datain ib_sge\n");
1874b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
1875b8d26b3bSNicholas Bellinger 		goto unmap_sg;
1876b8d26b3bSNicholas Bellinger 	}
1877b8d26b3bSNicholas Bellinger 	isert_cmd->ib_sge = ib_sge;
1878b8d26b3bSNicholas Bellinger 
1879b8d26b3bSNicholas Bellinger 	pr_debug("Allocated ib_sge: %p from t_data_ents: %d for RDMA_WRITE\n",
1880b8d26b3bSNicholas Bellinger 		 ib_sge, se_cmd->t_data_nents);
1881b8d26b3bSNicholas Bellinger 
1882b8d26b3bSNicholas Bellinger 	wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1883b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1884b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
1885b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
1886b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate wr->send_wr\n");
1887b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
1888b8d26b3bSNicholas Bellinger 		goto unmap_sg;
1889b8d26b3bSNicholas Bellinger 	}
1890b8d26b3bSNicholas Bellinger 	pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1891b8d26b3bSNicholas Bellinger 		 wr->send_wr, wr->send_wr_num);
1892b8d26b3bSNicholas Bellinger 
1893b8d26b3bSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
1894b8d26b3bSNicholas Bellinger 	cmd->stat_sn = conn->stat_sn++;
1895b8d26b3bSNicholas Bellinger 
1896b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
1897b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1898b8d26b3bSNicholas Bellinger 	data_left = se_cmd->data_length;
1899b8d26b3bSNicholas Bellinger 
1900b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
1901b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
1902b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
1903b8d26b3bSNicholas Bellinger 
1904b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_RDMA_WRITE;
1905b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
1906b8d26b3bSNicholas Bellinger 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
1907b8d26b3bSNicholas Bellinger 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
1908b8d26b3bSNicholas Bellinger 
1909b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1910b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
1911b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
1912b8d26b3bSNicholas Bellinger 
1913b8d26b3bSNicholas Bellinger 		if (i + 1 == wr->send_wr_num)
1914b8d26b3bSNicholas Bellinger 			send_wr->next = &isert_cmd->tx_desc.send_wr;
1915b8d26b3bSNicholas Bellinger 		else
1916b8d26b3bSNicholas Bellinger 			send_wr->next = &wr->send_wr[i + 1];
1917b8d26b3bSNicholas Bellinger 
1918b8d26b3bSNicholas Bellinger 		offset += data_len;
1919b8d26b3bSNicholas Bellinger 		data_left -= data_len;
1920b8d26b3bSNicholas Bellinger 	}
1921b8d26b3bSNicholas Bellinger 	/*
1922b8d26b3bSNicholas Bellinger 	 * Build isert_conn->tx_desc for iSCSI response PDU and attach
1923b8d26b3bSNicholas Bellinger 	 */
1924b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1925b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
1926b8d26b3bSNicholas Bellinger 			     &isert_cmd->tx_desc.iscsi_header);
1927b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1928b8d26b3bSNicholas Bellinger 	isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
1929b8d26b3bSNicholas Bellinger 
1930b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
1931b8d26b3bSNicholas Bellinger 
1932b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1933b8d26b3bSNicholas Bellinger 	if (rc) {
1934b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
1935b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1936b8d26b3bSNicholas Bellinger 	}
1937b8d26b3bSNicholas Bellinger 	pr_debug("Posted RDMA_WRITE + Response for iSER Data READ\n");
1938b8d26b3bSNicholas Bellinger 	return 1;
1939b8d26b3bSNicholas Bellinger 
1940b8d26b3bSNicholas Bellinger unmap_sg:
1941b8d26b3bSNicholas Bellinger 	ib_dma_unmap_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1942b8d26b3bSNicholas Bellinger 	return ret;
1943b8d26b3bSNicholas Bellinger }
1944b8d26b3bSNicholas Bellinger 
1945b8d26b3bSNicholas Bellinger static int
1946b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
1947b8d26b3bSNicholas Bellinger {
1948b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
1949b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(cmd,
1950b8d26b3bSNicholas Bellinger 					struct isert_cmd, iscsi_cmd);
1951b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1952b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1953b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed, *send_wr;
1954b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
1955b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1956b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1957b8d26b3bSNicholas Bellinger 	u32 sg_off, sg_nents, page_off, va_offset = 0;
1958b8d26b3bSNicholas Bellinger 	u32 offset = 0, data_len, data_left, rdma_write_max;
1959b8d26b3bSNicholas Bellinger 	int rc, ret = 0, count, i, ib_sge_cnt;
1960b8d26b3bSNicholas Bellinger 
1961b8d26b3bSNicholas Bellinger 	pr_debug("RDMA_READ: data_length: %u write_data_done: %u\n",
1962b8d26b3bSNicholas Bellinger 		 se_cmd->data_length, cmd->write_data_done);
1963b8d26b3bSNicholas Bellinger 
1964b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1965b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1966b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1967b8d26b3bSNicholas Bellinger 
1968b8d26b3bSNicholas Bellinger 	pr_debug("RDMA_READ: sg_off: %d, sg_start: %p page_off: %d\n",
1969b8d26b3bSNicholas Bellinger 		 sg_off, sg_start, page_off);
1970b8d26b3bSNicholas Bellinger 
1971b8d26b3bSNicholas Bellinger 	data_left = se_cmd->data_length - cmd->write_data_done;
1972b8d26b3bSNicholas Bellinger 	sg_nents = se_cmd->t_data_nents - sg_off;
1973b8d26b3bSNicholas Bellinger 
1974b8d26b3bSNicholas Bellinger 	pr_debug("RDMA_READ: data_left: %d, sg_nents: %d\n",
1975b8d26b3bSNicholas Bellinger 		 data_left, sg_nents);
1976b8d26b3bSNicholas Bellinger 
1977b8d26b3bSNicholas Bellinger 	count = ib_dma_map_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1978b8d26b3bSNicholas Bellinger 	if (unlikely(!count)) {
1979b8d26b3bSNicholas Bellinger 		pr_err("Unable to map get_dataout SGs\n");
1980b8d26b3bSNicholas Bellinger 		return -EINVAL;
1981b8d26b3bSNicholas Bellinger 	}
1982b8d26b3bSNicholas Bellinger 	wr->sge = sg_start;
1983b8d26b3bSNicholas Bellinger 	wr->num_sge = sg_nents;
1984b8d26b3bSNicholas Bellinger 	pr_debug("Mapped IB count: %u sg_start: %p sg_nents: %u for RDMA_READ\n",
1985b8d26b3bSNicholas Bellinger 		 count, sg_start, sg_nents);
1986b8d26b3bSNicholas Bellinger 
1987b8d26b3bSNicholas Bellinger 	ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1988b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
1989b8d26b3bSNicholas Bellinger 		pr_warn("Unable to allocate dataout ib_sge\n");
1990b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
1991b8d26b3bSNicholas Bellinger 		goto unmap_sg;
1992b8d26b3bSNicholas Bellinger 	}
1993b8d26b3bSNicholas Bellinger 	isert_cmd->ib_sge = ib_sge;
1994b8d26b3bSNicholas Bellinger 
1995b8d26b3bSNicholas Bellinger 	pr_debug("Using ib_sge: %p from sg_ents: %d for RDMA_READ\n",
1996b8d26b3bSNicholas Bellinger 		 ib_sge, sg_nents);
1997b8d26b3bSNicholas Bellinger 
1998b8d26b3bSNicholas Bellinger 	wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1999b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2000b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2001b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
2002b8d26b3bSNicholas Bellinger 		pr_debug("Unable to allocate wr->send_wr\n");
2003b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2004b8d26b3bSNicholas Bellinger 		goto unmap_sg;
2005b8d26b3bSNicholas Bellinger 	}
2006b8d26b3bSNicholas Bellinger 	pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
2007b8d26b3bSNicholas Bellinger 		 wr->send_wr, wr->send_wr_num);
2008b8d26b3bSNicholas Bellinger 
2009b8d26b3bSNicholas Bellinger 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2010b8d26b3bSNicholas Bellinger 
2011b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2012b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2013b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2014b8d26b3bSNicholas Bellinger 	offset = cmd->write_data_done;
2015b8d26b3bSNicholas Bellinger 
2016b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2017b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2018b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2019b8d26b3bSNicholas Bellinger 
2020b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_RDMA_READ;
2021b8d26b3bSNicholas Bellinger 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2022b8d26b3bSNicholas Bellinger 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2023b8d26b3bSNicholas Bellinger 
2024b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2025b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2026b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2027b8d26b3bSNicholas Bellinger 
2028b8d26b3bSNicholas Bellinger 		if (i + 1 == wr->send_wr_num)
2029b8d26b3bSNicholas Bellinger 			send_wr->send_flags = IB_SEND_SIGNALED;
2030b8d26b3bSNicholas Bellinger 		else
2031b8d26b3bSNicholas Bellinger 			send_wr->next = &wr->send_wr[i + 1];
2032b8d26b3bSNicholas Bellinger 
2033b8d26b3bSNicholas Bellinger 		offset += data_len;
2034b8d26b3bSNicholas Bellinger 		va_offset += data_len;
2035b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2036b8d26b3bSNicholas Bellinger 	}
2037b8d26b3bSNicholas Bellinger 
2038b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
2039b8d26b3bSNicholas Bellinger 
2040b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2041b8d26b3bSNicholas Bellinger 	if (rc) {
2042b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2043b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2044b8d26b3bSNicholas Bellinger 	}
2045b8d26b3bSNicholas Bellinger 	pr_debug("Posted RDMA_READ memory for ISER Data WRITE\n");
2046b8d26b3bSNicholas Bellinger 	return 0;
2047b8d26b3bSNicholas Bellinger 
2048b8d26b3bSNicholas Bellinger unmap_sg:
2049b8d26b3bSNicholas Bellinger 	ib_dma_unmap_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
2050b8d26b3bSNicholas Bellinger 	return ret;
2051b8d26b3bSNicholas Bellinger }
2052b8d26b3bSNicholas Bellinger 
2053b8d26b3bSNicholas Bellinger static int
2054b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2055b8d26b3bSNicholas Bellinger {
2056b8d26b3bSNicholas Bellinger 	int ret;
2057b8d26b3bSNicholas Bellinger 
2058b8d26b3bSNicholas Bellinger 	switch (state) {
2059b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2060b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2061b8d26b3bSNicholas Bellinger 		break;
2062b8d26b3bSNicholas Bellinger 	default:
2063b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
2064b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2065b8d26b3bSNicholas Bellinger 		break;
2066b8d26b3bSNicholas Bellinger 	}
2067b8d26b3bSNicholas Bellinger 
2068b8d26b3bSNicholas Bellinger 	return ret;
2069b8d26b3bSNicholas Bellinger }
2070b8d26b3bSNicholas Bellinger 
2071b8d26b3bSNicholas Bellinger static int
2072b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2073b8d26b3bSNicholas Bellinger {
2074b8d26b3bSNicholas Bellinger 	int ret;
2075b8d26b3bSNicholas Bellinger 
2076b8d26b3bSNicholas Bellinger 	switch (state) {
2077b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2078b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2079b8d26b3bSNicholas Bellinger 		if (!ret) {
2080b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
2081b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
2082b8d26b3bSNicholas Bellinger 		}
2083b8d26b3bSNicholas Bellinger 		break;
2084b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2085b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2086b8d26b3bSNicholas Bellinger 		break;
2087b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2088b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2089b8d26b3bSNicholas Bellinger 		break;
2090b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2091b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2092b8d26b3bSNicholas Bellinger 		break;
2093*adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2094*adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2095*adb54c29SNicholas Bellinger 		break;
2096b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2097b8d26b3bSNicholas Bellinger 		/*
2098b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2099b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2100b8d26b3bSNicholas Bellinger 		 */
2101b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2102b8d26b3bSNicholas Bellinger 		break;
2103b8d26b3bSNicholas Bellinger 	default:
2104b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
2105b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2106b8d26b3bSNicholas Bellinger 		break;
2107b8d26b3bSNicholas Bellinger 	}
2108b8d26b3bSNicholas Bellinger 
2109b8d26b3bSNicholas Bellinger 	return ret;
2110b8d26b3bSNicholas Bellinger }
2111b8d26b3bSNicholas Bellinger 
2112b8d26b3bSNicholas Bellinger static int
2113b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
2114b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
2115b8d26b3bSNicholas Bellinger {
2116b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
2117b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
2118b8d26b3bSNicholas Bellinger 	struct sockaddr *sa;
2119b8d26b3bSNicholas Bellinger 	int ret;
2120b8d26b3bSNicholas Bellinger 
2121b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2122b8d26b3bSNicholas Bellinger 	if (!isert_np) {
2123b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
2124b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2125b8d26b3bSNicholas Bellinger 	}
2126b8d26b3bSNicholas Bellinger 	init_waitqueue_head(&isert_np->np_accept_wq);
2127b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
2128b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
2129b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
2130b8d26b3bSNicholas Bellinger 
2131b8d26b3bSNicholas Bellinger 	sa = (struct sockaddr *)ksockaddr;
2132b8d26b3bSNicholas Bellinger 	pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2133b8d26b3bSNicholas Bellinger 	/*
2134b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
2135b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
2136b8d26b3bSNicholas Bellinger 	 */
2137b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
2138b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
2139b8d26b3bSNicholas Bellinger 
2140b8d26b3bSNicholas Bellinger 	isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2141b8d26b3bSNicholas Bellinger 				IB_QPT_RC);
2142b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
2143b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2144b8d26b3bSNicholas Bellinger 		       PTR_ERR(isert_lid));
2145b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
2146b8d26b3bSNicholas Bellinger 		goto out;
2147b8d26b3bSNicholas Bellinger 	}
2148b8d26b3bSNicholas Bellinger 
2149b8d26b3bSNicholas Bellinger 	ret = rdma_bind_addr(isert_lid, sa);
2150b8d26b3bSNicholas Bellinger 	if (ret) {
2151b8d26b3bSNicholas Bellinger 		pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2152b8d26b3bSNicholas Bellinger 		goto out_lid;
2153b8d26b3bSNicholas Bellinger 	}
2154b8d26b3bSNicholas Bellinger 
2155b8d26b3bSNicholas Bellinger 	ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2156b8d26b3bSNicholas Bellinger 	if (ret) {
2157b8d26b3bSNicholas Bellinger 		pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2158b8d26b3bSNicholas Bellinger 		goto out_lid;
2159b8d26b3bSNicholas Bellinger 	}
2160b8d26b3bSNicholas Bellinger 
2161b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
2162b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
2163b8d26b3bSNicholas Bellinger 	pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2164b8d26b3bSNicholas Bellinger 
2165b8d26b3bSNicholas Bellinger 	return 0;
2166b8d26b3bSNicholas Bellinger 
2167b8d26b3bSNicholas Bellinger out_lid:
2168b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_lid);
2169b8d26b3bSNicholas Bellinger out:
2170b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2171b8d26b3bSNicholas Bellinger 	return ret;
2172b8d26b3bSNicholas Bellinger }
2173b8d26b3bSNicholas Bellinger 
2174b8d26b3bSNicholas Bellinger static int
2175b8d26b3bSNicholas Bellinger isert_check_accept_queue(struct isert_np *isert_np)
2176b8d26b3bSNicholas Bellinger {
2177b8d26b3bSNicholas Bellinger 	int empty;
2178b8d26b3bSNicholas Bellinger 
2179b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
2180b8d26b3bSNicholas Bellinger 	empty = list_empty(&isert_np->np_accept_list);
2181b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
2182b8d26b3bSNicholas Bellinger 
2183b8d26b3bSNicholas Bellinger 	return empty;
2184b8d26b3bSNicholas Bellinger }
2185b8d26b3bSNicholas Bellinger 
2186b8d26b3bSNicholas Bellinger static int
2187b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
2188b8d26b3bSNicholas Bellinger {
2189b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2190b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
2191b8d26b3bSNicholas Bellinger 	int ret;
2192b8d26b3bSNicholas Bellinger 
2193b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
2194b8d26b3bSNicholas Bellinger 	cp.responder_resources = isert_conn->responder_resources;
2195b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
2196b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
2197b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
2198b8d26b3bSNicholas Bellinger 
2199b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2200b8d26b3bSNicholas Bellinger 
2201b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
2202b8d26b3bSNicholas Bellinger 	if (ret) {
2203b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
2204b8d26b3bSNicholas Bellinger 		return ret;
2205b8d26b3bSNicholas Bellinger 	}
2206b8d26b3bSNicholas Bellinger 
2207b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2208b8d26b3bSNicholas Bellinger 
2209b8d26b3bSNicholas Bellinger 	return 0;
2210b8d26b3bSNicholas Bellinger }
2211b8d26b3bSNicholas Bellinger 
2212b8d26b3bSNicholas Bellinger static int
2213b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2214b8d26b3bSNicholas Bellinger {
2215b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2216b8d26b3bSNicholas Bellinger 	int ret;
2217b8d26b3bSNicholas Bellinger 
2218b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
2219b8d26b3bSNicholas Bellinger 
2220b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2221b8d26b3bSNicholas Bellinger 	if (ret)
2222b8d26b3bSNicholas Bellinger 		return ret;
2223b8d26b3bSNicholas Bellinger 
2224b8d26b3bSNicholas Bellinger 	pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2225b8d26b3bSNicholas Bellinger 	return 0;
2226b8d26b3bSNicholas Bellinger }
2227b8d26b3bSNicholas Bellinger 
2228b8d26b3bSNicholas Bellinger static void
2229b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2230b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
2231b8d26b3bSNicholas Bellinger {
2232b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2233b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
2234b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
2235b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
2236b8d26b3bSNicholas Bellinger 
2237b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
2238b8d26b3bSNicholas Bellinger 
2239b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
2240b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2241b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2242b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
2243b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
2244b8d26b3bSNicholas Bellinger 
2245b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2246b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2247b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
2248b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
2249b8d26b3bSNicholas Bellinger 	} else {
2250b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2251b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
2252b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
2253b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
2254b8d26b3bSNicholas Bellinger 
2255b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2256b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
2257b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
2258b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
2259b8d26b3bSNicholas Bellinger 	}
2260b8d26b3bSNicholas Bellinger }
2261b8d26b3bSNicholas Bellinger 
2262b8d26b3bSNicholas Bellinger static int
2263b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2264b8d26b3bSNicholas Bellinger {
2265b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
2266b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2267b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
2268b8d26b3bSNicholas Bellinger 
2269b8d26b3bSNicholas Bellinger accept_wait:
2270b8d26b3bSNicholas Bellinger 	ret = wait_event_interruptible(isert_np->np_accept_wq,
2271b8d26b3bSNicholas Bellinger 			!isert_check_accept_queue(isert_np) ||
2272b8d26b3bSNicholas Bellinger 			np->np_thread_state == ISCSI_NP_THREAD_RESET);
2273b8d26b3bSNicholas Bellinger 	if (max_accept > 5)
2274b8d26b3bSNicholas Bellinger 		return -ENODEV;
2275b8d26b3bSNicholas Bellinger 
2276b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
2277b8d26b3bSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2278b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
2279b8d26b3bSNicholas Bellinger 		pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2280b8d26b3bSNicholas Bellinger 		return -ENODEV;
2281b8d26b3bSNicholas Bellinger 	}
2282b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
2283b8d26b3bSNicholas Bellinger 
2284b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
2285b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
2286b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
2287b8d26b3bSNicholas Bellinger 		max_accept++;
2288b8d26b3bSNicholas Bellinger 		goto accept_wait;
2289b8d26b3bSNicholas Bellinger 	}
2290b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
2291b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
2292b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
2293b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
2294b8d26b3bSNicholas Bellinger 
2295b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
2296b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
2297b8d26b3bSNicholas Bellinger 	max_accept = 0;
2298b8d26b3bSNicholas Bellinger 
2299b8d26b3bSNicholas Bellinger 	ret = isert_rdma_post_recvl(isert_conn);
2300b8d26b3bSNicholas Bellinger 	if (ret)
2301b8d26b3bSNicholas Bellinger 		return ret;
2302b8d26b3bSNicholas Bellinger 
2303b8d26b3bSNicholas Bellinger 	ret = isert_rdma_accept(isert_conn);
2304b8d26b3bSNicholas Bellinger 	if (ret)
2305b8d26b3bSNicholas Bellinger 		return ret;
2306b8d26b3bSNicholas Bellinger 
2307b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
2308b8d26b3bSNicholas Bellinger 
2309b8d26b3bSNicholas Bellinger 	pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2310b8d26b3bSNicholas Bellinger 	return 0;
2311b8d26b3bSNicholas Bellinger }
2312b8d26b3bSNicholas Bellinger 
2313b8d26b3bSNicholas Bellinger static void
2314b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
2315b8d26b3bSNicholas Bellinger {
2316b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
2317b8d26b3bSNicholas Bellinger 
2318b8d26b3bSNicholas Bellinger 	rdma_destroy_id(isert_np->np_cm_id);
2319b8d26b3bSNicholas Bellinger 
2320b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
2321b8d26b3bSNicholas Bellinger 	kfree(isert_np);
2322b8d26b3bSNicholas Bellinger }
2323b8d26b3bSNicholas Bellinger 
2324b2cb9649SNicholas Bellinger static int isert_check_state(struct isert_conn *isert_conn, int state)
2325b2cb9649SNicholas Bellinger {
2326b2cb9649SNicholas Bellinger 	int ret;
2327b2cb9649SNicholas Bellinger 
2328b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2329b2cb9649SNicholas Bellinger 	ret = (isert_conn->state == state);
2330b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2331b2cb9649SNicholas Bellinger 
2332b2cb9649SNicholas Bellinger 	return ret;
2333b2cb9649SNicholas Bellinger }
2334b2cb9649SNicholas Bellinger 
2335b8d26b3bSNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
2336b8d26b3bSNicholas Bellinger {
2337b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
2338b8d26b3bSNicholas Bellinger 
2339b8d26b3bSNicholas Bellinger 	pr_debug("isert_free_conn: Starting \n");
2340b8d26b3bSNicholas Bellinger 	/*
2341b8d26b3bSNicholas Bellinger 	 * Decrement post_send_buf_count for special case when called
2342b8d26b3bSNicholas Bellinger 	 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2343b8d26b3bSNicholas Bellinger 	 */
2344b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2345b8d26b3bSNicholas Bellinger 	if (isert_conn->logout_posted)
2346b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2347b8d26b3bSNicholas Bellinger 
2348b2cb9649SNicholas Bellinger 	if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
2349b2cb9649SNicholas Bellinger 		pr_debug("Calling rdma_disconnect from isert_free_conn\n");
2350b8d26b3bSNicholas Bellinger 		rdma_disconnect(isert_conn->conn_cm_id);
2351b2cb9649SNicholas Bellinger 	}
2352b8d26b3bSNicholas Bellinger 	/*
2353b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
2354b8d26b3bSNicholas Bellinger 	 * into full feature phase..
2355b8d26b3bSNicholas Bellinger 	 */
2356b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_UP) {
2357b8d26b3bSNicholas Bellinger 		pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
2358b8d26b3bSNicholas Bellinger 			 isert_conn->state);
2359b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
2360b8d26b3bSNicholas Bellinger 
2361b2cb9649SNicholas Bellinger 		wait_event(isert_conn->conn_wait_comp_err,
2362b2cb9649SNicholas Bellinger 			  (isert_check_state(isert_conn, ISER_CONN_TERMINATING)));
2363b2cb9649SNicholas Bellinger 
2364b2cb9649SNicholas Bellinger 		wait_event(isert_conn->conn_wait,
2365b2cb9649SNicholas Bellinger 			  (isert_check_state(isert_conn, ISER_CONN_DOWN)));
2366b2cb9649SNicholas Bellinger 
2367b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
2368b2cb9649SNicholas Bellinger 		return;
2369b2cb9649SNicholas Bellinger 	}
2370b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
2371b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
2372b2cb9649SNicholas Bellinger 		isert_put_conn(isert_conn);
2373b2cb9649SNicholas Bellinger 		return;
2374b2cb9649SNicholas Bellinger 	}
2375b2cb9649SNicholas Bellinger 	pr_debug("isert_free_conn: wait_event conn_wait %d\n",
2376b2cb9649SNicholas Bellinger 		 isert_conn->state);
2377b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2378b2cb9649SNicholas Bellinger 
2379b2cb9649SNicholas Bellinger 	wait_event(isert_conn->conn_wait,
2380b2cb9649SNicholas Bellinger 		  (isert_check_state(isert_conn, ISER_CONN_DOWN)));
2381b8d26b3bSNicholas Bellinger 
2382b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
2383b8d26b3bSNicholas Bellinger }
2384b8d26b3bSNicholas Bellinger 
2385b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
2386b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
2387b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
2388b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
2389b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
2390b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
2391b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
2392b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
2393b8d26b3bSNicholas Bellinger 	.iscsit_alloc_cmd	= isert_alloc_cmd,
2394b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
2395b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
2396b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
2397b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
2398b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
2399b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
2400b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
2401b8d26b3bSNicholas Bellinger };
2402b8d26b3bSNicholas Bellinger 
2403b8d26b3bSNicholas Bellinger static int __init isert_init(void)
2404b8d26b3bSNicholas Bellinger {
2405b8d26b3bSNicholas Bellinger 	int ret;
2406b8d26b3bSNicholas Bellinger 
2407b8d26b3bSNicholas Bellinger 	isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2408b8d26b3bSNicholas Bellinger 	if (!isert_rx_wq) {
2409b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_rx_wq\n");
2410b8d26b3bSNicholas Bellinger 		return -ENOMEM;
2411b8d26b3bSNicholas Bellinger 	}
2412b8d26b3bSNicholas Bellinger 
2413b8d26b3bSNicholas Bellinger 	isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2414b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
2415b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_comp_wq\n");
2416b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2417b8d26b3bSNicholas Bellinger 		goto destroy_rx_wq;
2418b8d26b3bSNicholas Bellinger 	}
2419b8d26b3bSNicholas Bellinger 
2420b8d26b3bSNicholas Bellinger 	isert_cmd_cache = kmem_cache_create("isert_cmd_cache",
2421b8d26b3bSNicholas Bellinger 			sizeof(struct isert_cmd), __alignof__(struct isert_cmd),
2422b8d26b3bSNicholas Bellinger 			0, NULL);
2423b8d26b3bSNicholas Bellinger 	if (!isert_cmd_cache) {
2424b8d26b3bSNicholas Bellinger 		pr_err("Unable to create isert_cmd_cache\n");
2425b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2426b8d26b3bSNicholas Bellinger 		goto destroy_tx_cq;
2427b8d26b3bSNicholas Bellinger 	}
2428b8d26b3bSNicholas Bellinger 
2429b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
2430b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2431b8d26b3bSNicholas Bellinger 	return 0;
2432b8d26b3bSNicholas Bellinger 
2433b8d26b3bSNicholas Bellinger destroy_tx_cq:
2434b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2435b8d26b3bSNicholas Bellinger destroy_rx_wq:
2436b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
2437b8d26b3bSNicholas Bellinger 	return ret;
2438b8d26b3bSNicholas Bellinger }
2439b8d26b3bSNicholas Bellinger 
2440b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
2441b8d26b3bSNicholas Bellinger {
2442b8d26b3bSNicholas Bellinger 	kmem_cache_destroy(isert_cmd_cache);
2443b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
2444b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
2445b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
2446b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2447b8d26b3bSNicholas Bellinger }
2448b8d26b3bSNicholas Bellinger 
2449b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2450b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
2451b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
2452b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
2453b8d26b3bSNicholas Bellinger 
2454b8d26b3bSNicholas Bellinger module_init(isert_init);
2455b8d26b3bSNicholas Bellinger module_exit(isert_exit);
2456