xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 23a548ee656c8ba6da8cb2412070edcd62e2ac5d)
1b8d26b3bSNicholas Bellinger /*******************************************************************************
2b8d26b3bSNicholas Bellinger  * This file contains iSCSI extentions for RDMA (iSER) Verbs
3b8d26b3bSNicholas Bellinger  *
44c76251eSNicholas Bellinger  * (c) Copyright 2013 Datera, Inc.
5b8d26b3bSNicholas Bellinger  *
6b8d26b3bSNicholas Bellinger  * Nicholas A. Bellinger <nab@linux-iscsi.org>
7b8d26b3bSNicholas Bellinger  *
8b8d26b3bSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
9b8d26b3bSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
10b8d26b3bSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
11b8d26b3bSNicholas Bellinger  * (at your option) any later version.
12b8d26b3bSNicholas Bellinger  *
13b8d26b3bSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
14b8d26b3bSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15b8d26b3bSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16b8d26b3bSNicholas Bellinger  * GNU General Public License for more details.
17b8d26b3bSNicholas Bellinger  ****************************************************************************/
18b8d26b3bSNicholas Bellinger 
19b8d26b3bSNicholas Bellinger #include <linux/string.h>
20b8d26b3bSNicholas Bellinger #include <linux/module.h>
21b8d26b3bSNicholas Bellinger #include <linux/scatterlist.h>
22b8d26b3bSNicholas Bellinger #include <linux/socket.h>
23b8d26b3bSNicholas Bellinger #include <linux/in.h>
24b8d26b3bSNicholas Bellinger #include <linux/in6.h>
2595b60f07SNicholas Bellinger #include <linux/llist.h>
26b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h>
27b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h>
28b8d26b3bSNicholas Bellinger #include <target/target_core_base.h>
29b8d26b3bSNicholas Bellinger #include <target/target_core_fabric.h>
30b8d26b3bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
31531b7bf4SSagi Grimberg #include <linux/semaphore.h>
32b8d26b3bSNicholas Bellinger 
33b8d26b3bSNicholas Bellinger #include "isert_proto.h"
34b8d26b3bSNicholas Bellinger #include "ib_isert.h"
35b8d26b3bSNicholas Bellinger 
36b8d26b3bSNicholas Bellinger #define	ISERT_MAX_CONN		8
37b8d26b3bSNicholas Bellinger #define ISER_MAX_RX_CQ_LEN	(ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
38b8d26b3bSNicholas Bellinger #define ISER_MAX_TX_CQ_LEN	(ISERT_QP_MAX_REQ_DTOS  * ISERT_MAX_CONN)
39b8d26b3bSNicholas Bellinger 
40b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
41b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
42b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_rx_wq;
43b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
44b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
45b8d26b3bSNicholas Bellinger 
46b8d26b3bSNicholas Bellinger static void
47d40945d8SVu Pham isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
48d40945d8SVu Pham static int
49d40945d8SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
50d40945d8SVu Pham 	       struct isert_rdma_wr *wr);
5159464ef4SVu Pham static void
52a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
5359464ef4SVu Pham static int
54a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
5559464ef4SVu Pham 	       struct isert_rdma_wr *wr);
56f93f3a70SSagi Grimberg static int
57f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
582371e5daSSagi Grimberg static int
592371e5daSSagi Grimberg isert_rdma_post_recvl(struct isert_conn *isert_conn);
602371e5daSSagi Grimberg static int
612371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
62ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
63d40945d8SVu Pham 
64302cc7c3SSagi Grimberg static inline bool
65302cc7c3SSagi Grimberg isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
66302cc7c3SSagi Grimberg {
67*23a548eeSSagi Grimberg 	return (conn->pi_support &&
68302cc7c3SSagi Grimberg 		cmd->prot_op != TARGET_PROT_NORMAL);
69302cc7c3SSagi Grimberg }
70302cc7c3SSagi Grimberg 
71302cc7c3SSagi Grimberg 
72d40945d8SVu Pham static void
73b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
74b8d26b3bSNicholas Bellinger {
75b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)context;
76b8d26b3bSNicholas Bellinger 
77b8d26b3bSNicholas Bellinger 	pr_err("isert_qp_event_callback event: %d\n", e->event);
78b8d26b3bSNicholas Bellinger 	switch (e->event) {
79b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
80b8d26b3bSNicholas Bellinger 		rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
81b8d26b3bSNicholas Bellinger 		break;
82b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
83b8d26b3bSNicholas Bellinger 		pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
84b8d26b3bSNicholas Bellinger 		break;
85b8d26b3bSNicholas Bellinger 	default:
86b8d26b3bSNicholas Bellinger 		break;
87b8d26b3bSNicholas Bellinger 	}
88b8d26b3bSNicholas Bellinger }
89b8d26b3bSNicholas Bellinger 
90b8d26b3bSNicholas Bellinger static int
91b8d26b3bSNicholas Bellinger isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
92b8d26b3bSNicholas Bellinger {
93b8d26b3bSNicholas Bellinger 	int ret;
94b8d26b3bSNicholas Bellinger 
95b8d26b3bSNicholas Bellinger 	ret = ib_query_device(ib_dev, devattr);
96b8d26b3bSNicholas Bellinger 	if (ret) {
97b8d26b3bSNicholas Bellinger 		pr_err("ib_query_device() failed: %d\n", ret);
98b8d26b3bSNicholas Bellinger 		return ret;
99b8d26b3bSNicholas Bellinger 	}
100b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
101b8d26b3bSNicholas Bellinger 	pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
102b8d26b3bSNicholas Bellinger 
103b8d26b3bSNicholas Bellinger 	return 0;
104b8d26b3bSNicholas Bellinger }
105b8d26b3bSNicholas Bellinger 
106b8d26b3bSNicholas Bellinger static int
107570db170SSagi Grimberg isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
108b8d26b3bSNicholas Bellinger {
109b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
110b8d26b3bSNicholas Bellinger 	struct ib_qp_init_attr attr;
111b8d26b3bSNicholas Bellinger 	int ret, index, min_index = 0;
112b8d26b3bSNicholas Bellinger 
113b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
114b8d26b3bSNicholas Bellinger 	for (index = 0; index < device->cqs_used; index++)
115b8d26b3bSNicholas Bellinger 		if (device->cq_active_qps[index] <
116b8d26b3bSNicholas Bellinger 		    device->cq_active_qps[min_index])
117b8d26b3bSNicholas Bellinger 			min_index = index;
118b8d26b3bSNicholas Bellinger 	device->cq_active_qps[min_index]++;
119b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
120b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
121b8d26b3bSNicholas Bellinger 
122b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
123b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
124b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
125b8d26b3bSNicholas Bellinger 	attr.send_cq = device->dev_tx_cq[min_index];
126b8d26b3bSNicholas Bellinger 	attr.recv_cq = device->dev_rx_cq[min_index];
127b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
128b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
129b8d26b3bSNicholas Bellinger 	/*
130b8d26b3bSNicholas Bellinger 	 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
131f57915cfSOr Gerlitz 	 * work-around for RDMA_READs with ConnectX-2.
132f57915cfSOr Gerlitz 	 *
133f57915cfSOr Gerlitz 	 * Also, still make sure to have at least two SGEs for
134f57915cfSOr Gerlitz 	 * outgoing control PDU responses.
135b8d26b3bSNicholas Bellinger 	 */
136f57915cfSOr Gerlitz 	attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
137b8d26b3bSNicholas Bellinger 	isert_conn->max_sge = attr.cap.max_send_sge;
138b8d26b3bSNicholas Bellinger 
139b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
140b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
141b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
142570db170SSagi Grimberg 	if (device->pi_capable)
143d3e125daSSagi Grimberg 		attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
144b8d26b3bSNicholas Bellinger 
145b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
146b8d26b3bSNicholas Bellinger 		 cma_id->device);
147b8d26b3bSNicholas Bellinger 	pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
148b8d26b3bSNicholas Bellinger 		 isert_conn->conn_pd->device);
149b8d26b3bSNicholas Bellinger 
150b8d26b3bSNicholas Bellinger 	ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
151b8d26b3bSNicholas Bellinger 	if (ret) {
152b8d26b3bSNicholas Bellinger 		pr_err("rdma_create_qp failed for cma_id %d\n", ret);
15319e2090fSSagi Grimberg 		goto err;
154b8d26b3bSNicholas Bellinger 	}
155b8d26b3bSNicholas Bellinger 	isert_conn->conn_qp = cma_id->qp;
156b8d26b3bSNicholas Bellinger 	pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
157b8d26b3bSNicholas Bellinger 
158b8d26b3bSNicholas Bellinger 	return 0;
15919e2090fSSagi Grimberg err:
16019e2090fSSagi Grimberg 	mutex_lock(&device_list_mutex);
16119e2090fSSagi Grimberg 	device->cq_active_qps[min_index]--;
16219e2090fSSagi Grimberg 	mutex_unlock(&device_list_mutex);
16319e2090fSSagi Grimberg 
16419e2090fSSagi Grimberg 	return ret;
165b8d26b3bSNicholas Bellinger }
166b8d26b3bSNicholas Bellinger 
167b8d26b3bSNicholas Bellinger static void
168b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
169b8d26b3bSNicholas Bellinger {
170b8d26b3bSNicholas Bellinger 	pr_debug("isert_cq_event_callback event: %d\n", e->event);
171b8d26b3bSNicholas Bellinger }
172b8d26b3bSNicholas Bellinger 
173b8d26b3bSNicholas Bellinger static int
174b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
175b8d26b3bSNicholas Bellinger {
176b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
177b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
178b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
179b8d26b3bSNicholas Bellinger 	u64 dma_addr;
180b8d26b3bSNicholas Bellinger 	int i, j;
181b8d26b3bSNicholas Bellinger 
182b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
183b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
184b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
185b8d26b3bSNicholas Bellinger 		goto fail;
186b8d26b3bSNicholas Bellinger 
187b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
188b8d26b3bSNicholas Bellinger 
189b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
190b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
191b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
192b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
193b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
194b8d26b3bSNicholas Bellinger 
195b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
196b8d26b3bSNicholas Bellinger 
197b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
198b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
199b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
200b8d26b3bSNicholas Bellinger 		rx_sg->lkey = isert_conn->conn_mr->lkey;
201b8d26b3bSNicholas Bellinger 	}
202b8d26b3bSNicholas Bellinger 
203b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_desc_head = 0;
204b8d26b3bSNicholas Bellinger 	return 0;
205b8d26b3bSNicholas Bellinger 
206b8d26b3bSNicholas Bellinger dma_map_fail:
207b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
208b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
209b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
210b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
211b8d26b3bSNicholas Bellinger 	}
212b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
213b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
214b8d26b3bSNicholas Bellinger fail:
215b8d26b3bSNicholas Bellinger 	return -ENOMEM;
216b8d26b3bSNicholas Bellinger }
217b8d26b3bSNicholas Bellinger 
218b8d26b3bSNicholas Bellinger static void
219b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
220b8d26b3bSNicholas Bellinger {
221b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
222b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
223b8d26b3bSNicholas Bellinger 	int i;
224b8d26b3bSNicholas Bellinger 
225b8d26b3bSNicholas Bellinger 	if (!isert_conn->conn_rx_descs)
226b8d26b3bSNicholas Bellinger 		return;
227b8d26b3bSNicholas Bellinger 
228b8d26b3bSNicholas Bellinger 	rx_desc = isert_conn->conn_rx_descs;
229b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
230b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
231b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
232b8d26b3bSNicholas Bellinger 	}
233b8d26b3bSNicholas Bellinger 
234b8d26b3bSNicholas Bellinger 	kfree(isert_conn->conn_rx_descs);
235b8d26b3bSNicholas Bellinger 	isert_conn->conn_rx_descs = NULL;
236b8d26b3bSNicholas Bellinger }
237b8d26b3bSNicholas Bellinger 
2382853c2b6SNicholas Bellinger static void isert_cq_tx_work(struct work_struct *);
239b8d26b3bSNicholas Bellinger static void isert_cq_tx_callback(struct ib_cq *, void *);
2402853c2b6SNicholas Bellinger static void isert_cq_rx_work(struct work_struct *);
241b8d26b3bSNicholas Bellinger static void isert_cq_rx_callback(struct ib_cq *, void *);
242b8d26b3bSNicholas Bellinger 
243b8d26b3bSNicholas Bellinger static int
244b8d26b3bSNicholas Bellinger isert_create_device_ib_res(struct isert_device *device)
245b8d26b3bSNicholas Bellinger {
246b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = device->ib_device;
247b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
24859464ef4SVu Pham 	struct ib_device_attr *dev_attr;
249b8d26b3bSNicholas Bellinger 	int ret = 0, i, j;
250b1a5ad00SChris Moore 	int max_rx_cqe, max_tx_cqe;
251b8d26b3bSNicholas Bellinger 
25259464ef4SVu Pham 	dev_attr = &device->dev_attr;
25359464ef4SVu Pham 	ret = isert_query_device(ib_dev, dev_attr);
25459464ef4SVu Pham 	if (ret)
25559464ef4SVu Pham 		return ret;
25659464ef4SVu Pham 
257b1a5ad00SChris Moore 	max_rx_cqe = min(ISER_MAX_RX_CQ_LEN, dev_attr->max_cqe);
258b1a5ad00SChris Moore 	max_tx_cqe = min(ISER_MAX_TX_CQ_LEN, dev_attr->max_cqe);
259b1a5ad00SChris Moore 
260d40945d8SVu Pham 	/* asign function handlers */
261f2252258SSagi Grimberg 	if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
262f2252258SSagi Grimberg 	    dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
263a3a5a826SSagi Grimberg 		device->use_fastreg = 1;
264a3a5a826SSagi Grimberg 		device->reg_rdma_mem = isert_reg_rdma;
265a3a5a826SSagi Grimberg 		device->unreg_rdma_mem = isert_unreg_rdma;
26659464ef4SVu Pham 	} else {
267a3a5a826SSagi Grimberg 		device->use_fastreg = 0;
268d40945d8SVu Pham 		device->reg_rdma_mem = isert_map_rdma;
269d40945d8SVu Pham 		device->unreg_rdma_mem = isert_unmap_cmd;
27059464ef4SVu Pham 	}
271d40945d8SVu Pham 
272d3e125daSSagi Grimberg 	/* Check signature cap */
273d3e125daSSagi Grimberg 	device->pi_capable = dev_attr->device_cap_flags &
274d3e125daSSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
275d3e125daSSagi Grimberg 
276b8d26b3bSNicholas Bellinger 	device->cqs_used = min_t(int, num_online_cpus(),
277b8d26b3bSNicholas Bellinger 				 device->ib_device->num_comp_vectors);
278b8d26b3bSNicholas Bellinger 	device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
279a3a5a826SSagi Grimberg 	pr_debug("Using %d CQs, device %s supports %d vectors support "
280d3e125daSSagi Grimberg 		 "Fast registration %d pi_capable %d\n",
281b8d26b3bSNicholas Bellinger 		 device->cqs_used, device->ib_device->name,
282d3e125daSSagi Grimberg 		 device->ib_device->num_comp_vectors, device->use_fastreg,
283d3e125daSSagi Grimberg 		 device->pi_capable);
284b8d26b3bSNicholas Bellinger 	device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
285b8d26b3bSNicholas Bellinger 				device->cqs_used, GFP_KERNEL);
286b8d26b3bSNicholas Bellinger 	if (!device->cq_desc) {
287b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate device->cq_desc\n");
288b8d26b3bSNicholas Bellinger 		return -ENOMEM;
289b8d26b3bSNicholas Bellinger 	}
290b8d26b3bSNicholas Bellinger 	cq_desc = device->cq_desc;
291b8d26b3bSNicholas Bellinger 
292b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
293b8d26b3bSNicholas Bellinger 		cq_desc[i].device = device;
294b8d26b3bSNicholas Bellinger 		cq_desc[i].cq_index = i;
295b8d26b3bSNicholas Bellinger 
2962853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
297b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
298b8d26b3bSNicholas Bellinger 						isert_cq_rx_callback,
299b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
300b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
301b1a5ad00SChris Moore 						max_rx_cqe, i);
30294a71110SWei Yongjun 		if (IS_ERR(device->dev_rx_cq[i])) {
30394a71110SWei Yongjun 			ret = PTR_ERR(device->dev_rx_cq[i]);
30494a71110SWei Yongjun 			device->dev_rx_cq[i] = NULL;
305b8d26b3bSNicholas Bellinger 			goto out_cq;
30694a71110SWei Yongjun 		}
307b8d26b3bSNicholas Bellinger 
3082853c2b6SNicholas Bellinger 		INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
309b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
310b8d26b3bSNicholas Bellinger 						isert_cq_tx_callback,
311b8d26b3bSNicholas Bellinger 						isert_cq_event_callback,
312b8d26b3bSNicholas Bellinger 						(void *)&cq_desc[i],
313b1a5ad00SChris Moore 						max_tx_cqe, i);
31494a71110SWei Yongjun 		if (IS_ERR(device->dev_tx_cq[i])) {
31594a71110SWei Yongjun 			ret = PTR_ERR(device->dev_tx_cq[i]);
31694a71110SWei Yongjun 			device->dev_tx_cq[i] = NULL;
31794a71110SWei Yongjun 			goto out_cq;
31894a71110SWei Yongjun 		}
31994a71110SWei Yongjun 
32094a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
32194a71110SWei Yongjun 		if (ret)
322b8d26b3bSNicholas Bellinger 			goto out_cq;
323b8d26b3bSNicholas Bellinger 
32494a71110SWei Yongjun 		ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
32594a71110SWei Yongjun 		if (ret)
326b8d26b3bSNicholas Bellinger 			goto out_cq;
327b8d26b3bSNicholas Bellinger 	}
328b8d26b3bSNicholas Bellinger 
329b8d26b3bSNicholas Bellinger 	return 0;
330b8d26b3bSNicholas Bellinger 
331b8d26b3bSNicholas Bellinger out_cq:
332b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++) {
333b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[j];
334b8d26b3bSNicholas Bellinger 
335b8d26b3bSNicholas Bellinger 		if (device->dev_rx_cq[j]) {
336b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_rx_work);
337b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_rx_cq[j]);
338b8d26b3bSNicholas Bellinger 		}
339b8d26b3bSNicholas Bellinger 		if (device->dev_tx_cq[j]) {
340b8d26b3bSNicholas Bellinger 			cancel_work_sync(&cq_desc->cq_tx_work);
341b8d26b3bSNicholas Bellinger 			ib_destroy_cq(device->dev_tx_cq[j]);
342b8d26b3bSNicholas Bellinger 		}
343b8d26b3bSNicholas Bellinger 	}
344b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
345b8d26b3bSNicholas Bellinger 
346b8d26b3bSNicholas Bellinger 	return ret;
347b8d26b3bSNicholas Bellinger }
348b8d26b3bSNicholas Bellinger 
349b8d26b3bSNicholas Bellinger static void
350b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
351b8d26b3bSNicholas Bellinger {
352b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc;
353b8d26b3bSNicholas Bellinger 	int i;
354b8d26b3bSNicholas Bellinger 
355b8d26b3bSNicholas Bellinger 	for (i = 0; i < device->cqs_used; i++) {
356b8d26b3bSNicholas Bellinger 		cq_desc = &device->cq_desc[i];
357b8d26b3bSNicholas Bellinger 
358b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_rx_work);
359b8d26b3bSNicholas Bellinger 		cancel_work_sync(&cq_desc->cq_tx_work);
360b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_rx_cq[i]);
361b8d26b3bSNicholas Bellinger 		ib_destroy_cq(device->dev_tx_cq[i]);
362b8d26b3bSNicholas Bellinger 		device->dev_rx_cq[i] = NULL;
363b8d26b3bSNicholas Bellinger 		device->dev_tx_cq[i] = NULL;
364b8d26b3bSNicholas Bellinger 	}
365b8d26b3bSNicholas Bellinger 
366b8d26b3bSNicholas Bellinger 	kfree(device->cq_desc);
367b8d26b3bSNicholas Bellinger }
368b8d26b3bSNicholas Bellinger 
369b8d26b3bSNicholas Bellinger static void
370b8d26b3bSNicholas Bellinger isert_device_try_release(struct isert_device *device)
371b8d26b3bSNicholas Bellinger {
372b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
373b8d26b3bSNicholas Bellinger 	device->refcount--;
374b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
375b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
376b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
377b8d26b3bSNicholas Bellinger 		kfree(device);
378b8d26b3bSNicholas Bellinger 	}
379b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
380b8d26b3bSNicholas Bellinger }
381b8d26b3bSNicholas Bellinger 
382b8d26b3bSNicholas Bellinger static struct isert_device *
383b8d26b3bSNicholas Bellinger isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
384b8d26b3bSNicholas Bellinger {
385b8d26b3bSNicholas Bellinger 	struct isert_device *device;
386b8d26b3bSNicholas Bellinger 	int ret;
387b8d26b3bSNicholas Bellinger 
388b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
389b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
390b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
391b8d26b3bSNicholas Bellinger 			device->refcount++;
392b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
393b8d26b3bSNicholas Bellinger 			return device;
394b8d26b3bSNicholas Bellinger 		}
395b8d26b3bSNicholas Bellinger 	}
396b8d26b3bSNicholas Bellinger 
397b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
398b8d26b3bSNicholas Bellinger 	if (!device) {
399b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
400b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
401b8d26b3bSNicholas Bellinger 	}
402b8d26b3bSNicholas Bellinger 
403b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
404b8d26b3bSNicholas Bellinger 
405b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
406b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
407b8d26b3bSNicholas Bellinger 	if (ret) {
408b8d26b3bSNicholas Bellinger 		kfree(device);
409b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
410b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
411b8d26b3bSNicholas Bellinger 	}
412b8d26b3bSNicholas Bellinger 
413b8d26b3bSNicholas Bellinger 	device->refcount++;
414b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
415b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
416b8d26b3bSNicholas Bellinger 
417b8d26b3bSNicholas Bellinger 	return device;
418b8d26b3bSNicholas Bellinger }
419b8d26b3bSNicholas Bellinger 
42059464ef4SVu Pham static void
421a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
42259464ef4SVu Pham {
42359464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
42459464ef4SVu Pham 	int i = 0;
42559464ef4SVu Pham 
426a3a5a826SSagi Grimberg 	if (list_empty(&isert_conn->conn_fr_pool))
42759464ef4SVu Pham 		return;
42859464ef4SVu Pham 
429a3a5a826SSagi Grimberg 	pr_debug("Freeing conn %p fastreg pool", isert_conn);
43059464ef4SVu Pham 
43159464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
432a3a5a826SSagi Grimberg 				 &isert_conn->conn_fr_pool, list) {
43359464ef4SVu Pham 		list_del(&fr_desc->list);
43459464ef4SVu Pham 		ib_free_fast_reg_page_list(fr_desc->data_frpl);
43559464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
436d3e125daSSagi Grimberg 		if (fr_desc->pi_ctx) {
437d3e125daSSagi Grimberg 			ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
438d3e125daSSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
439d3e125daSSagi Grimberg 			ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
440d3e125daSSagi Grimberg 			kfree(fr_desc->pi_ctx);
441d3e125daSSagi Grimberg 		}
44259464ef4SVu Pham 		kfree(fr_desc);
44359464ef4SVu Pham 		++i;
44459464ef4SVu Pham 	}
44559464ef4SVu Pham 
446a3a5a826SSagi Grimberg 	if (i < isert_conn->conn_fr_pool_size)
44759464ef4SVu Pham 		pr_warn("Pool still has %d regions registered\n",
448a3a5a826SSagi Grimberg 			isert_conn->conn_fr_pool_size - i);
44959464ef4SVu Pham }
45059464ef4SVu Pham 
45159464ef4SVu Pham static int
452570db170SSagi Grimberg isert_create_pi_ctx(struct fast_reg_descriptor *desc,
453570db170SSagi Grimberg 		    struct ib_device *device,
454570db170SSagi Grimberg 		    struct ib_pd *pd)
455570db170SSagi Grimberg {
456570db170SSagi Grimberg 	struct ib_mr_init_attr mr_init_attr;
457570db170SSagi Grimberg 	struct pi_context *pi_ctx;
458570db170SSagi Grimberg 	int ret;
459570db170SSagi Grimberg 
460570db170SSagi Grimberg 	pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
461570db170SSagi Grimberg 	if (!pi_ctx) {
462570db170SSagi Grimberg 		pr_err("Failed to allocate pi context\n");
463570db170SSagi Grimberg 		return -ENOMEM;
464570db170SSagi Grimberg 	}
465570db170SSagi Grimberg 
466570db170SSagi Grimberg 	pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
467570db170SSagi Grimberg 					    ISCSI_ISER_SG_TABLESIZE);
468570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_frpl)) {
469570db170SSagi Grimberg 		pr_err("Failed to allocate prot frpl err=%ld\n",
470570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_frpl));
471570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_frpl);
472570db170SSagi Grimberg 		goto err_pi_ctx;
473570db170SSagi Grimberg 	}
474570db170SSagi Grimberg 
475570db170SSagi Grimberg 	pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
476570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_mr)) {
477570db170SSagi Grimberg 		pr_err("Failed to allocate prot frmr err=%ld\n",
478570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_mr));
479570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_mr);
480570db170SSagi Grimberg 		goto err_prot_frpl;
481570db170SSagi Grimberg 	}
482570db170SSagi Grimberg 	desc->ind |= ISERT_PROT_KEY_VALID;
483570db170SSagi Grimberg 
484570db170SSagi Grimberg 	memset(&mr_init_attr, 0, sizeof(mr_init_attr));
485570db170SSagi Grimberg 	mr_init_attr.max_reg_descriptors = 2;
486570db170SSagi Grimberg 	mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
487570db170SSagi Grimberg 	pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
488570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->sig_mr)) {
489570db170SSagi Grimberg 		pr_err("Failed to allocate signature enabled mr err=%ld\n",
490570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->sig_mr));
491570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->sig_mr);
492570db170SSagi Grimberg 		goto err_prot_mr;
493570db170SSagi Grimberg 	}
494570db170SSagi Grimberg 
495570db170SSagi Grimberg 	desc->pi_ctx = pi_ctx;
496570db170SSagi Grimberg 	desc->ind |= ISERT_SIG_KEY_VALID;
497570db170SSagi Grimberg 	desc->ind &= ~ISERT_PROTECTED;
498570db170SSagi Grimberg 
499570db170SSagi Grimberg 	return 0;
500570db170SSagi Grimberg 
501570db170SSagi Grimberg err_prot_mr:
502570db170SSagi Grimberg 	ib_dereg_mr(desc->pi_ctx->prot_mr);
503570db170SSagi Grimberg err_prot_frpl:
504570db170SSagi Grimberg 	ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
505570db170SSagi Grimberg err_pi_ctx:
506570db170SSagi Grimberg 	kfree(desc->pi_ctx);
507570db170SSagi Grimberg 
508570db170SSagi Grimberg 	return ret;
509570db170SSagi Grimberg }
510570db170SSagi Grimberg 
511570db170SSagi Grimberg static int
512dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
513570db170SSagi Grimberg 		     struct fast_reg_descriptor *fr_desc)
514dc87a90fSSagi Grimberg {
515d3e125daSSagi Grimberg 	int ret;
516d3e125daSSagi Grimberg 
517dc87a90fSSagi Grimberg 	fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
518dc87a90fSSagi Grimberg 							 ISCSI_ISER_SG_TABLESIZE);
519dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_frpl)) {
520dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frpl err=%ld\n",
521dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_frpl));
522dc87a90fSSagi Grimberg 		return PTR_ERR(fr_desc->data_frpl);
523dc87a90fSSagi Grimberg 	}
524dc87a90fSSagi Grimberg 
525dc87a90fSSagi Grimberg 	fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
526dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
527dc87a90fSSagi Grimberg 		pr_err("Failed to allocate data frmr err=%ld\n",
528dc87a90fSSagi Grimberg 		       PTR_ERR(fr_desc->data_mr));
529d3e125daSSagi Grimberg 		ret = PTR_ERR(fr_desc->data_mr);
530d3e125daSSagi Grimberg 		goto err_data_frpl;
531dc87a90fSSagi Grimberg 	}
532d3e125daSSagi Grimberg 	fr_desc->ind |= ISERT_DATA_KEY_VALID;
533dc87a90fSSagi Grimberg 
534570db170SSagi Grimberg 	pr_debug("Created fr_desc %p\n", fr_desc);
535dc87a90fSSagi Grimberg 
536dc87a90fSSagi Grimberg 	return 0;
537570db170SSagi Grimberg 
538d3e125daSSagi Grimberg err_data_frpl:
539d3e125daSSagi Grimberg 	ib_free_fast_reg_page_list(fr_desc->data_frpl);
540d3e125daSSagi Grimberg 
541d3e125daSSagi Grimberg 	return ret;
542dc87a90fSSagi Grimberg }
543dc87a90fSSagi Grimberg 
544dc87a90fSSagi Grimberg static int
545570db170SSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
54659464ef4SVu Pham {
54759464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
54859464ef4SVu Pham 	struct isert_device *device = isert_conn->conn_device;
549f46d6a8aSNicholas Bellinger 	struct se_session *se_sess = isert_conn->conn->sess->se_sess;
550f46d6a8aSNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
551f46d6a8aSNicholas Bellinger 	int i, ret, tag_num;
552f46d6a8aSNicholas Bellinger 	/*
553f46d6a8aSNicholas Bellinger 	 * Setup the number of FRMRs based upon the number of tags
554f46d6a8aSNicholas Bellinger 	 * available to session in iscsi_target_locate_portal().
555f46d6a8aSNicholas Bellinger 	 */
556f46d6a8aSNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
557f46d6a8aSNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
55859464ef4SVu Pham 
559a3a5a826SSagi Grimberg 	isert_conn->conn_fr_pool_size = 0;
560f46d6a8aSNicholas Bellinger 	for (i = 0; i < tag_num; i++) {
56159464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
56259464ef4SVu Pham 		if (!fr_desc) {
56359464ef4SVu Pham 			pr_err("Failed to allocate fast_reg descriptor\n");
56459464ef4SVu Pham 			ret = -ENOMEM;
56559464ef4SVu Pham 			goto err;
56659464ef4SVu Pham 		}
56759464ef4SVu Pham 
568dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
569570db170SSagi Grimberg 					   isert_conn->conn_pd, fr_desc);
570dc87a90fSSagi Grimberg 		if (ret) {
571dc87a90fSSagi Grimberg 			pr_err("Failed to create fastreg descriptor err=%d\n",
572dc87a90fSSagi Grimberg 			       ret);
573a80e21b3SNicholas Bellinger 			kfree(fr_desc);
57459464ef4SVu Pham 			goto err;
57559464ef4SVu Pham 		}
57659464ef4SVu Pham 
577a3a5a826SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
578a3a5a826SSagi Grimberg 		isert_conn->conn_fr_pool_size++;
57959464ef4SVu Pham 	}
58059464ef4SVu Pham 
581a3a5a826SSagi Grimberg 	pr_debug("Creating conn %p fastreg pool size=%d",
582a3a5a826SSagi Grimberg 		 isert_conn, isert_conn->conn_fr_pool_size);
58359464ef4SVu Pham 
58459464ef4SVu Pham 	return 0;
58559464ef4SVu Pham 
58659464ef4SVu Pham err:
587a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
58859464ef4SVu Pham 	return ret;
58959464ef4SVu Pham }
59059464ef4SVu Pham 
591b8d26b3bSNicholas Bellinger static int
592b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
593b8d26b3bSNicholas Bellinger {
594ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
595ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
596b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
597b8d26b3bSNicholas Bellinger 	struct isert_device *device;
598b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = cma_id->device;
599b8d26b3bSNicholas Bellinger 	int ret = 0;
60014f4b54fSSagi Grimberg 
60114f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
60214f4b54fSSagi Grimberg 	if (!np->enabled) {
60314f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
60414f4b54fSSagi Grimberg 		pr_debug("iscsi_np is not enabled, reject connect request\n");
60514f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
60614f4b54fSSagi Grimberg 	}
60714f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
608b8d26b3bSNicholas Bellinger 
609b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
610b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
611b8d26b3bSNicholas Bellinger 
612b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
613b8d26b3bSNicholas Bellinger 	if (!isert_conn) {
614b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn\n");
615b8d26b3bSNicholas Bellinger 		return -ENOMEM;
616b8d26b3bSNicholas Bellinger 	}
617b8d26b3bSNicholas Bellinger 	isert_conn->state = ISER_CONN_INIT;
618b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_accept_node);
619b8d26b3bSNicholas Bellinger 	init_completion(&isert_conn->conn_login_comp);
6202371e5daSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
621defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait);
622defd8848SNicholas Bellinger 	init_completion(&isert_conn->conn_wait_comp_err);
623b8d26b3bSNicholas Bellinger 	kref_init(&isert_conn->conn_kref);
624b2cb9649SNicholas Bellinger 	mutex_init(&isert_conn->conn_mutex);
62559464ef4SVu Pham 	spin_lock_init(&isert_conn->conn_lock);
626f46d6a8aSNicholas Bellinger 	INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
627b8d26b3bSNicholas Bellinger 
628b8d26b3bSNicholas Bellinger 	isert_conn->conn_cm_id = cma_id;
629b8d26b3bSNicholas Bellinger 
630b8d26b3bSNicholas Bellinger 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
631b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
632b8d26b3bSNicholas Bellinger 	if (!isert_conn->login_buf) {
633b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_conn->login_buf\n");
634b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
635b8d26b3bSNicholas Bellinger 		goto out;
636b8d26b3bSNicholas Bellinger 	}
637b8d26b3bSNicholas Bellinger 
638b8d26b3bSNicholas Bellinger 	isert_conn->login_req_buf = isert_conn->login_buf;
639b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_buf = isert_conn->login_buf +
640b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
641b8d26b3bSNicholas Bellinger 	pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
642b8d26b3bSNicholas Bellinger 		 isert_conn->login_buf, isert_conn->login_req_buf,
643b8d26b3bSNicholas Bellinger 		 isert_conn->login_rsp_buf);
644b8d26b3bSNicholas Bellinger 
645b8d26b3bSNicholas Bellinger 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
646b8d26b3bSNicholas Bellinger 				(void *)isert_conn->login_req_buf,
647b8d26b3bSNicholas Bellinger 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
648b8d26b3bSNicholas Bellinger 
649b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
650b8d26b3bSNicholas Bellinger 	if (ret) {
651b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
652b8d26b3bSNicholas Bellinger 		       ret);
653b8d26b3bSNicholas Bellinger 		isert_conn->login_req_dma = 0;
654b8d26b3bSNicholas Bellinger 		goto out_login_buf;
655b8d26b3bSNicholas Bellinger 	}
656b8d26b3bSNicholas Bellinger 
657b8d26b3bSNicholas Bellinger 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
658b8d26b3bSNicholas Bellinger 					(void *)isert_conn->login_rsp_buf,
659b8d26b3bSNicholas Bellinger 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
660b8d26b3bSNicholas Bellinger 
661b8d26b3bSNicholas Bellinger 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
662b8d26b3bSNicholas Bellinger 	if (ret) {
663b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
664b8d26b3bSNicholas Bellinger 		       ret);
665b8d26b3bSNicholas Bellinger 		isert_conn->login_rsp_dma = 0;
666b8d26b3bSNicholas Bellinger 		goto out_req_dma_map;
667b8d26b3bSNicholas Bellinger 	}
668b8d26b3bSNicholas Bellinger 
669b8d26b3bSNicholas Bellinger 	device = isert_device_find_by_ib_dev(cma_id);
670b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
671b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
672b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
673b8d26b3bSNicholas Bellinger 	}
674b8d26b3bSNicholas Bellinger 
6751a92e17eSSagi Grimberg 	/* Set max inflight RDMA READ requests */
6761a92e17eSSagi Grimberg 	isert_conn->initiator_depth = min_t(u8,
6771a92e17eSSagi Grimberg 				event->param.conn.initiator_depth,
6781a92e17eSSagi Grimberg 				device->dev_attr.max_qp_init_rd_atom);
6791a92e17eSSagi Grimberg 	pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
6801a92e17eSSagi Grimberg 
681b8d26b3bSNicholas Bellinger 	isert_conn->conn_device = device;
682eb6ab132SSagi Grimberg 	isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
683eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_pd)) {
684eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_pd);
685eb6ab132SSagi Grimberg 		pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
686eb6ab132SSagi Grimberg 		       isert_conn, ret);
687eb6ab132SSagi Grimberg 		goto out_pd;
688eb6ab132SSagi Grimberg 	}
689b8d26b3bSNicholas Bellinger 
690eb6ab132SSagi Grimberg 	isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
691eb6ab132SSagi Grimberg 					   IB_ACCESS_LOCAL_WRITE);
692eb6ab132SSagi Grimberg 	if (IS_ERR(isert_conn->conn_mr)) {
693eb6ab132SSagi Grimberg 		ret = PTR_ERR(isert_conn->conn_mr);
694eb6ab132SSagi Grimberg 		pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
695eb6ab132SSagi Grimberg 		       isert_conn, ret);
696eb6ab132SSagi Grimberg 		goto out_mr;
697eb6ab132SSagi Grimberg 	}
698b8d26b3bSNicholas Bellinger 
699570db170SSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id);
700b8d26b3bSNicholas Bellinger 	if (ret)
701b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
702b8d26b3bSNicholas Bellinger 
7032371e5daSSagi Grimberg 	ret = isert_rdma_post_recvl(isert_conn);
7042371e5daSSagi Grimberg 	if (ret)
7052371e5daSSagi Grimberg 		goto out_conn_dev;
7062371e5daSSagi Grimberg 
7072371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
7082371e5daSSagi Grimberg 	if (ret)
7092371e5daSSagi Grimberg 		goto out_conn_dev;
7102371e5daSSagi Grimberg 
711b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
7129fe63c88SSagi Grimberg 	list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
713b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
714b8d26b3bSNicholas Bellinger 
715531b7bf4SSagi Grimberg 	pr_debug("isert_connect_request() up np_sem np: %p\n", np);
716531b7bf4SSagi Grimberg 	up(&isert_np->np_sem);
717b8d26b3bSNicholas Bellinger 	return 0;
718b8d26b3bSNicholas Bellinger 
719b8d26b3bSNicholas Bellinger out_conn_dev:
720eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
721eb6ab132SSagi Grimberg out_mr:
722eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
723eb6ab132SSagi Grimberg out_pd:
724b8d26b3bSNicholas Bellinger 	isert_device_try_release(device);
725b8d26b3bSNicholas Bellinger out_rsp_dma_map:
726b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
727b8d26b3bSNicholas Bellinger 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
728b8d26b3bSNicholas Bellinger out_req_dma_map:
729b8d26b3bSNicholas Bellinger 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
730b8d26b3bSNicholas Bellinger 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
731b8d26b3bSNicholas Bellinger out_login_buf:
732b8d26b3bSNicholas Bellinger 	kfree(isert_conn->login_buf);
733b8d26b3bSNicholas Bellinger out:
734b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
7352371e5daSSagi Grimberg 	rdma_reject(cma_id, NULL, 0);
736b8d26b3bSNicholas Bellinger 	return ret;
737b8d26b3bSNicholas Bellinger }
738b8d26b3bSNicholas Bellinger 
739b8d26b3bSNicholas Bellinger static void
740b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
741b8d26b3bSNicholas Bellinger {
742b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
743b8d26b3bSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
744b8d26b3bSNicholas Bellinger 	int cq_index;
745b8d26b3bSNicholas Bellinger 
746b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
747b8d26b3bSNicholas Bellinger 
748a3a5a826SSagi Grimberg 	if (device && device->use_fastreg)
749a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
75059464ef4SVu Pham 
75119e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
75219e2090fSSagi Grimberg 	rdma_destroy_id(isert_conn->conn_cm_id);
75319e2090fSSagi Grimberg 
754b8d26b3bSNicholas Bellinger 	if (isert_conn->conn_qp) {
755b8d26b3bSNicholas Bellinger 		cq_index = ((struct isert_cq_desc *)
756b8d26b3bSNicholas Bellinger 			isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
757b8d26b3bSNicholas Bellinger 		pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
75819e2090fSSagi Grimberg 		mutex_lock(&device_list_mutex);
759b8d26b3bSNicholas Bellinger 		isert_conn->conn_device->cq_active_qps[cq_index]--;
76019e2090fSSagi Grimberg 		mutex_unlock(&device_list_mutex);
761b8d26b3bSNicholas Bellinger 
76219e2090fSSagi Grimberg 		ib_destroy_qp(isert_conn->conn_qp);
763b8d26b3bSNicholas Bellinger 	}
764b8d26b3bSNicholas Bellinger 
765eb6ab132SSagi Grimberg 	ib_dereg_mr(isert_conn->conn_mr);
766eb6ab132SSagi Grimberg 	ib_dealloc_pd(isert_conn->conn_pd);
767eb6ab132SSagi Grimberg 
768b8d26b3bSNicholas Bellinger 	if (isert_conn->login_buf) {
769b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
770b8d26b3bSNicholas Bellinger 				    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
771b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
772b8d26b3bSNicholas Bellinger 				    ISCSI_DEF_MAX_RECV_SEG_LEN,
773b8d26b3bSNicholas Bellinger 				    DMA_FROM_DEVICE);
774b8d26b3bSNicholas Bellinger 		kfree(isert_conn->login_buf);
775b8d26b3bSNicholas Bellinger 	}
776b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
777b8d26b3bSNicholas Bellinger 
778b8d26b3bSNicholas Bellinger 	if (device)
779b8d26b3bSNicholas Bellinger 		isert_device_try_release(device);
780b8d26b3bSNicholas Bellinger 
781b8d26b3bSNicholas Bellinger 	pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
782b8d26b3bSNicholas Bellinger }
783b8d26b3bSNicholas Bellinger 
784b8d26b3bSNicholas Bellinger static void
785b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
786b8d26b3bSNicholas Bellinger {
78719e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
788c2f88b17SSagi Grimberg 
789128e9cc8SSagi Grimberg 	pr_info("conn %p\n", isert_conn);
790128e9cc8SSagi Grimberg 
7912371e5daSSagi Grimberg 	if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
7922371e5daSSagi Grimberg 		pr_warn("conn %p connect_release is running\n", isert_conn);
7932371e5daSSagi Grimberg 		return;
7942371e5daSSagi Grimberg 	}
7952371e5daSSagi Grimberg 
7962371e5daSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
7972371e5daSSagi Grimberg 	if (isert_conn->state != ISER_CONN_FULL_FEATURE)
798128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_UP;
7992371e5daSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
800b8d26b3bSNicholas Bellinger }
801b8d26b3bSNicholas Bellinger 
802b8d26b3bSNicholas Bellinger static void
803b8d26b3bSNicholas Bellinger isert_release_conn_kref(struct kref *kref)
804b8d26b3bSNicholas Bellinger {
805b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
806b8d26b3bSNicholas Bellinger 				struct isert_conn, conn_kref);
807b8d26b3bSNicholas Bellinger 
808b8d26b3bSNicholas Bellinger 	pr_debug("Calling isert_connect_release for final kref %s/%d\n",
809b8d26b3bSNicholas Bellinger 		 current->comm, current->pid);
810b8d26b3bSNicholas Bellinger 
811b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
812b8d26b3bSNicholas Bellinger }
813b8d26b3bSNicholas Bellinger 
814b8d26b3bSNicholas Bellinger static void
815b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
816b8d26b3bSNicholas Bellinger {
817b8d26b3bSNicholas Bellinger 	kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
818b8d26b3bSNicholas Bellinger }
819b8d26b3bSNicholas Bellinger 
820954f2372SSagi Grimberg /**
821954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
822954f2372SSagi Grimberg  * @isert_conn: isert connection struct
823954f2372SSagi Grimberg  *
824954f2372SSagi Grimberg  * Notes:
825128e9cc8SSagi Grimberg  * In case the connection state is FULL_FEATURE, move state
826954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
827128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
828954f2372SSagi Grimberg  *
829954f2372SSagi Grimberg  * This routine must be called with conn_mutex held. Thus it is
830954f2372SSagi Grimberg  * safe to call multiple times.
831954f2372SSagi Grimberg  */
832954f2372SSagi Grimberg static void
833954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
834954f2372SSagi Grimberg {
835954f2372SSagi Grimberg 	int err;
836954f2372SSagi Grimberg 
837128e9cc8SSagi Grimberg 	switch (isert_conn->state) {
838128e9cc8SSagi Grimberg 	case ISER_CONN_TERMINATING:
839128e9cc8SSagi Grimberg 		break;
840128e9cc8SSagi Grimberg 	case ISER_CONN_UP:
841128e9cc8SSagi Grimberg 		/*
842128e9cc8SSagi Grimberg 		 * No flush completions will occur as we didn't
843128e9cc8SSagi Grimberg 		 * get to ISER_CONN_FULL_FEATURE yet, complete
844128e9cc8SSagi Grimberg 		 * to allow teardown progress.
845128e9cc8SSagi Grimberg 		 */
846128e9cc8SSagi Grimberg 		complete(&isert_conn->conn_wait_comp_err);
847128e9cc8SSagi Grimberg 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
848954f2372SSagi Grimberg 		pr_info("Terminating conn %p state %d\n",
849954f2372SSagi Grimberg 			   isert_conn, isert_conn->state);
850128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_TERMINATING;
851954f2372SSagi Grimberg 		err = rdma_disconnect(isert_conn->conn_cm_id);
852954f2372SSagi Grimberg 		if (err)
853954f2372SSagi Grimberg 			pr_warn("Failed rdma_disconnect isert_conn %p\n",
854954f2372SSagi Grimberg 				   isert_conn);
855128e9cc8SSagi Grimberg 		break;
856128e9cc8SSagi Grimberg 	default:
857128e9cc8SSagi Grimberg 		pr_warn("conn %p teminating in state %d\n",
858128e9cc8SSagi Grimberg 			   isert_conn, isert_conn->state);
859954f2372SSagi Grimberg 	}
860954f2372SSagi Grimberg }
861954f2372SSagi Grimberg 
8623b726ae2SSagi Grimberg static int
863ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
864ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
865b8d26b3bSNicholas Bellinger {
866ca6c1d82SSagi Grimberg 	pr_debug("isert np %p, handling event %d\n", isert_np, event);
8673b726ae2SSagi Grimberg 
868ca6c1d82SSagi Grimberg 	switch (event) {
869ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
8703b726ae2SSagi Grimberg 		isert_np->np_cm_id = NULL;
871ca6c1d82SSagi Grimberg 		break;
872ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
873ca6c1d82SSagi Grimberg 		isert_np->np_cm_id = isert_setup_id(isert_np);
874ca6c1d82SSagi Grimberg 		if (IS_ERR(isert_np->np_cm_id)) {
875ca6c1d82SSagi Grimberg 			pr_err("isert np %p setup id failed: %ld\n",
876ca6c1d82SSagi Grimberg 				 isert_np, PTR_ERR(isert_np->np_cm_id));
877ca6c1d82SSagi Grimberg 			isert_np->np_cm_id = NULL;
878ca6c1d82SSagi Grimberg 		}
879ca6c1d82SSagi Grimberg 		break;
880ca6c1d82SSagi Grimberg 	default:
881ca6c1d82SSagi Grimberg 		pr_err("isert np %p Unexpected event %d\n",
882ca6c1d82SSagi Grimberg 			  isert_np, event);
883ca6c1d82SSagi Grimberg 	}
884ca6c1d82SSagi Grimberg 
8853b726ae2SSagi Grimberg 	return -1;
8863b726ae2SSagi Grimberg }
8873b726ae2SSagi Grimberg 
888ca6c1d82SSagi Grimberg static int
889ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
890ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
891ca6c1d82SSagi Grimberg {
892ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
893ca6c1d82SSagi Grimberg 	struct isert_conn *isert_conn;
894ca6c1d82SSagi Grimberg 
895ca6c1d82SSagi Grimberg 	if (isert_np->np_cm_id == cma_id)
896ca6c1d82SSagi Grimberg 		return isert_np_cma_handler(cma_id->context, event);
897ca6c1d82SSagi Grimberg 
89819e2090fSSagi Grimberg 	isert_conn = cma_id->qp->qp_context;
899b8d26b3bSNicholas Bellinger 
900128e9cc8SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
901128e9cc8SSagi Grimberg 	isert_conn_terminate(isert_conn);
902128e9cc8SSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
903128e9cc8SSagi Grimberg 
904128e9cc8SSagi Grimberg 	pr_info("conn %p completing conn_wait\n", isert_conn);
905128e9cc8SSagi Grimberg 	complete(&isert_conn->conn_wait);
9063b726ae2SSagi Grimberg 
9073b726ae2SSagi Grimberg 	return 0;
908b8d26b3bSNicholas Bellinger }
909b8d26b3bSNicholas Bellinger 
910954f2372SSagi Grimberg static void
911954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
912954f2372SSagi Grimberg {
91319e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
914954f2372SSagi Grimberg 
915954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
916954f2372SSagi Grimberg }
917954f2372SSagi Grimberg 
918b8d26b3bSNicholas Bellinger static int
919b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
920b8d26b3bSNicholas Bellinger {
921b8d26b3bSNicholas Bellinger 	int ret = 0;
922b8d26b3bSNicholas Bellinger 
923b8d26b3bSNicholas Bellinger 	pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
924b8d26b3bSNicholas Bellinger 		 event->event, event->status, cma_id->context, cma_id);
925b8d26b3bSNicholas Bellinger 
926b8d26b3bSNicholas Bellinger 	switch (event->event) {
927b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
928b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
9293b726ae2SSagi Grimberg 		if (ret)
9303b726ae2SSagi Grimberg 			pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
9313b726ae2SSagi Grimberg 				event->event, ret);
932b8d26b3bSNicholas Bellinger 		break;
933b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
934b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
935b8d26b3bSNicholas Bellinger 		break;
93688c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
93788c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
93888c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
93988c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
940ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
941b8d26b3bSNicholas Bellinger 		break;
942954f2372SSagi Grimberg 	case RDMA_CM_EVENT_REJECTED:       /* FALLTHRU */
943954f2372SSagi Grimberg 	case RDMA_CM_EVENT_UNREACHABLE:    /* FALLTHRU */
944b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
945954f2372SSagi Grimberg 		isert_connect_error(cma_id);
946954f2372SSagi Grimberg 		break;
947b8d26b3bSNicholas Bellinger 	default:
94888c4015fSSagi Grimberg 		pr_err("Unhandled RDMA CMA event: %d\n", event->event);
949b8d26b3bSNicholas Bellinger 		break;
950b8d26b3bSNicholas Bellinger 	}
951b8d26b3bSNicholas Bellinger 
952b8d26b3bSNicholas Bellinger 	return ret;
953b8d26b3bSNicholas Bellinger }
954b8d26b3bSNicholas Bellinger 
955b8d26b3bSNicholas Bellinger static int
956b8d26b3bSNicholas Bellinger isert_post_recv(struct isert_conn *isert_conn, u32 count)
957b8d26b3bSNicholas Bellinger {
958b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
959b8d26b3bSNicholas Bellinger 	int i, ret;
960b8d26b3bSNicholas Bellinger 	unsigned int rx_head = isert_conn->conn_rx_desc_head;
961b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
962b8d26b3bSNicholas Bellinger 
963b8d26b3bSNicholas Bellinger 	for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
964b8d26b3bSNicholas Bellinger 		rx_desc		= &isert_conn->conn_rx_descs[rx_head];
965b8d26b3bSNicholas Bellinger 		rx_wr->wr_id	= (unsigned long)rx_desc;
966b8d26b3bSNicholas Bellinger 		rx_wr->sg_list	= &rx_desc->rx_sg;
967b8d26b3bSNicholas Bellinger 		rx_wr->num_sge	= 1;
968b8d26b3bSNicholas Bellinger 		rx_wr->next	= rx_wr + 1;
969b8d26b3bSNicholas Bellinger 		rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
970b8d26b3bSNicholas Bellinger 	}
971b8d26b3bSNicholas Bellinger 
972b8d26b3bSNicholas Bellinger 	rx_wr--;
973b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
974b8d26b3bSNicholas Bellinger 
975b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
976b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
977b8d26b3bSNicholas Bellinger 				&rx_wr_failed);
978b8d26b3bSNicholas Bellinger 	if (ret) {
979b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed with ret: %d\n", ret);
980b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
981b8d26b3bSNicholas Bellinger 	} else {
982b8d26b3bSNicholas Bellinger 		pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
983b8d26b3bSNicholas Bellinger 		isert_conn->conn_rx_desc_head = rx_head;
984b8d26b3bSNicholas Bellinger 	}
985b8d26b3bSNicholas Bellinger 	return ret;
986b8d26b3bSNicholas Bellinger }
987b8d26b3bSNicholas Bellinger 
988b8d26b3bSNicholas Bellinger static int
989b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
990b8d26b3bSNicholas Bellinger {
991b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
992b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
993b8d26b3bSNicholas Bellinger 	int ret;
994b8d26b3bSNicholas Bellinger 
995b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
996b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
997b8d26b3bSNicholas Bellinger 
998b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
999b8d26b3bSNicholas Bellinger 	send_wr.wr_id	= (unsigned long)tx_desc;
1000b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
1001b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
1002b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
1003b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
1004b8d26b3bSNicholas Bellinger 
1005b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
1006b8d26b3bSNicholas Bellinger 
1007b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
1008b8d26b3bSNicholas Bellinger 	if (ret) {
1009b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send() failed, ret: %d\n", ret);
1010b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1011b8d26b3bSNicholas Bellinger 	}
1012b8d26b3bSNicholas Bellinger 
1013b8d26b3bSNicholas Bellinger 	return ret;
1014b8d26b3bSNicholas Bellinger }
1015b8d26b3bSNicholas Bellinger 
1016b8d26b3bSNicholas Bellinger static void
1017b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
1018b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
1019b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
1020b8d26b3bSNicholas Bellinger {
1021b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1022b8d26b3bSNicholas Bellinger 
1023b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1024b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
1025b8d26b3bSNicholas Bellinger 
1026b8d26b3bSNicholas Bellinger 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1027b8d26b3bSNicholas Bellinger 	tx_desc->iser_header.flags = ISER_VER;
1028b8d26b3bSNicholas Bellinger 
1029b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
1030b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
1031b8d26b3bSNicholas Bellinger 
1032b8d26b3bSNicholas Bellinger 	if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
1033b8d26b3bSNicholas Bellinger 		tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1034b8d26b3bSNicholas Bellinger 		pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1035b8d26b3bSNicholas Bellinger 	}
1036b8d26b3bSNicholas Bellinger }
1037b8d26b3bSNicholas Bellinger 
1038b8d26b3bSNicholas Bellinger static int
1039b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
1040b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
1041b8d26b3bSNicholas Bellinger {
1042b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1043b8d26b3bSNicholas Bellinger 	u64 dma_addr;
1044b8d26b3bSNicholas Bellinger 
1045b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1046b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
1047b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
1048b8d26b3bSNicholas Bellinger 		pr_err("ib_dma_mapping_error() failed\n");
1049b8d26b3bSNicholas Bellinger 		return -ENOMEM;
1050b8d26b3bSNicholas Bellinger 	}
1051b8d26b3bSNicholas Bellinger 
1052b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
1053b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
1054b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
1055b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
1056b8d26b3bSNicholas Bellinger 
1057b8d26b3bSNicholas Bellinger 	pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
1058b8d26b3bSNicholas Bellinger 		 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
1059b8d26b3bSNicholas Bellinger 		 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
1060b8d26b3bSNicholas Bellinger 
1061b8d26b3bSNicholas Bellinger 	return 0;
1062b8d26b3bSNicholas Bellinger }
1063b8d26b3bSNicholas Bellinger 
1064b8d26b3bSNicholas Bellinger static void
106595b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
106695b60f07SNicholas Bellinger 		   struct ib_send_wr *send_wr, bool coalesce)
1067b8d26b3bSNicholas Bellinger {
106895b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
106995b60f07SNicholas Bellinger 
1070b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1071b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1072b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
107395b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
1074b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
107595b60f07SNicholas Bellinger 	/*
107695b60f07SNicholas Bellinger 	 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
107795b60f07SNicholas Bellinger 	 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
107895b60f07SNicholas Bellinger 	 */
1079ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
1080128e9cc8SSagi Grimberg 	if (coalesce && isert_conn->state == ISER_CONN_FULL_FEATURE &&
108195b60f07SNicholas Bellinger 	    ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
1082ebbe4421SNicholas Bellinger 		tx_desc->llnode_active = true;
108395b60f07SNicholas Bellinger 		llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
1084ebbe4421SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
108595b60f07SNicholas Bellinger 		return;
108695b60f07SNicholas Bellinger 	}
108795b60f07SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
108895b60f07SNicholas Bellinger 	tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
1089ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
109095b60f07SNicholas Bellinger 
109195b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
1092b8d26b3bSNicholas Bellinger }
1093b8d26b3bSNicholas Bellinger 
1094b8d26b3bSNicholas Bellinger static int
1095b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
1096b8d26b3bSNicholas Bellinger {
1097b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
1098b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
1099b8d26b3bSNicholas Bellinger 	int ret;
1100b8d26b3bSNicholas Bellinger 
1101b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
1102b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
1103b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
1104b8d26b3bSNicholas Bellinger 	sge.lkey = isert_conn->conn_mr->lkey;
1105b8d26b3bSNicholas Bellinger 
1106b8d26b3bSNicholas Bellinger 	pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1107b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
1108b8d26b3bSNicholas Bellinger 
1109b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1110b8d26b3bSNicholas Bellinger 	rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1111b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1112b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1113b8d26b3bSNicholas Bellinger 
1114b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1115b8d26b3bSNicholas Bellinger 	ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1116b8d26b3bSNicholas Bellinger 	if (ret) {
1117b8d26b3bSNicholas Bellinger 		pr_err("ib_post_recv() failed: %d\n", ret);
1118b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1119b8d26b3bSNicholas Bellinger 	}
1120b8d26b3bSNicholas Bellinger 
1121b8d26b3bSNicholas Bellinger 	pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1122b8d26b3bSNicholas Bellinger 	return ret;
1123b8d26b3bSNicholas Bellinger }
1124b8d26b3bSNicholas Bellinger 
1125b8d26b3bSNicholas Bellinger static int
1126b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1127b8d26b3bSNicholas Bellinger 		   u32 length)
1128b8d26b3bSNicholas Bellinger {
1129b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1130b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1131b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1132b8d26b3bSNicholas Bellinger 	int ret;
1133b8d26b3bSNicholas Bellinger 
1134b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1135b8d26b3bSNicholas Bellinger 
1136b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1137b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1138b8d26b3bSNicholas Bellinger 
1139b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1140b8d26b3bSNicholas Bellinger 
1141b8d26b3bSNicholas Bellinger 	if (length > 0) {
1142b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1143b8d26b3bSNicholas Bellinger 
1144b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1145b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1146b8d26b3bSNicholas Bellinger 
1147b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1148b8d26b3bSNicholas Bellinger 
1149b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1150b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1151b8d26b3bSNicholas Bellinger 
1152b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1153b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
1154b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
1155b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1156b8d26b3bSNicholas Bellinger 	}
1157b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1158b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1159e0546fc1SSagi Grimberg 			if (!conn->sess->sess_ops->SessionType &&
1160e0546fc1SSagi Grimberg 			    isert_conn->conn_device->use_fastreg) {
1161570db170SSagi Grimberg 				ret = isert_conn_create_fastreg_pool(isert_conn);
1162f46d6a8aSNicholas Bellinger 				if (ret) {
1163f46d6a8aSNicholas Bellinger 					pr_err("Conn: %p failed to create"
1164f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1165f46d6a8aSNicholas Bellinger 					return ret;
1166f46d6a8aSNicholas Bellinger 				}
1167f46d6a8aSNicholas Bellinger 			}
1168f46d6a8aSNicholas Bellinger 
1169b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1170b8d26b3bSNicholas Bellinger 			if (ret)
1171b8d26b3bSNicholas Bellinger 				return ret;
1172b8d26b3bSNicholas Bellinger 
1173b8d26b3bSNicholas Bellinger 			ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1174b8d26b3bSNicholas Bellinger 			if (ret)
1175b8d26b3bSNicholas Bellinger 				return ret;
1176b8d26b3bSNicholas Bellinger 
1177128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
11782371e5daSSagi Grimberg 			mutex_lock(&isert_conn->conn_mutex);
1179128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
11802371e5daSSagi Grimberg 			mutex_unlock(&isert_conn->conn_mutex);
1181b8d26b3bSNicholas Bellinger 			goto post_send;
1182b8d26b3bSNicholas Bellinger 		}
1183b8d26b3bSNicholas Bellinger 
1184b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1185b8d26b3bSNicholas Bellinger 		if (ret)
1186b8d26b3bSNicholas Bellinger 			return ret;
1187b8d26b3bSNicholas Bellinger 	}
1188b8d26b3bSNicholas Bellinger post_send:
1189b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1190b8d26b3bSNicholas Bellinger 	if (ret)
1191b8d26b3bSNicholas Bellinger 		return ret;
1192b8d26b3bSNicholas Bellinger 
1193b8d26b3bSNicholas Bellinger 	return 0;
1194b8d26b3bSNicholas Bellinger }
1195b8d26b3bSNicholas Bellinger 
1196b8d26b3bSNicholas Bellinger static void
11972371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1198b8d26b3bSNicholas Bellinger {
11992371e5daSSagi Grimberg 	struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
12002371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1201b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1202b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1203b8d26b3bSNicholas Bellinger 	int size;
1204b8d26b3bSNicholas Bellinger 
12052371e5daSSagi Grimberg 	pr_info("conn %p\n", isert_conn);
12062371e5daSSagi Grimberg 
12072371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1208b8d26b3bSNicholas Bellinger 
1209b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1210b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1211b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1212b8d26b3bSNicholas Bellinger 		/*
1213b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1214b8d26b3bSNicholas Bellinger 		 * login request PDU.
1215b8d26b3bSNicholas Bellinger 		 */
1216b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1217b8d26b3bSNicholas Bellinger 		login->current_stage =
1218b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1219b8d26b3bSNicholas Bellinger 			 >> 2;
1220b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1221b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1222b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1223b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1224b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1225b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1226b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1227b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1228b8d26b3bSNicholas Bellinger 	}
1229b8d26b3bSNicholas Bellinger 
1230b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1231b8d26b3bSNicholas Bellinger 
1232b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1233b8d26b3bSNicholas Bellinger 	pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1234b8d26b3bSNicholas Bellinger 		 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1235b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1236b8d26b3bSNicholas Bellinger 
12376faaa85fSNicholas Bellinger 	if (login->first_request) {
1238b8d26b3bSNicholas Bellinger 		complete(&isert_conn->conn_login_comp);
12396faaa85fSNicholas Bellinger 		return;
12406faaa85fSNicholas Bellinger 	}
12416faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1242b8d26b3bSNicholas Bellinger }
1243b8d26b3bSNicholas Bellinger 
1244b8d26b3bSNicholas Bellinger static struct iscsi_cmd
1245676687c6SNicholas Bellinger *isert_allocate_cmd(struct iscsi_conn *conn)
1246b8d26b3bSNicholas Bellinger {
1247b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1248b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1249d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1250b8d26b3bSNicholas Bellinger 
1251676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1252d703ce2fSNicholas Bellinger 	if (!cmd) {
1253d703ce2fSNicholas Bellinger 		pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1254b8d26b3bSNicholas Bellinger 		return NULL;
1255b8d26b3bSNicholas Bellinger 	}
1256d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1257b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1258d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
1259b8d26b3bSNicholas Bellinger 
1260d703ce2fSNicholas Bellinger 	return cmd;
1261b8d26b3bSNicholas Bellinger }
1262b8d26b3bSNicholas Bellinger 
1263b8d26b3bSNicholas Bellinger static int
1264b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1265d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1266d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1267b8d26b3bSNicholas Bellinger {
1268b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1269b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1270b8d26b3bSNicholas Bellinger 	struct scatterlist *sg;
1271b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1272b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
1273b8d26b3bSNicholas Bellinger 
1274b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1275b8d26b3bSNicholas Bellinger 	if (rc < 0)
1276b8d26b3bSNicholas Bellinger 		return rc;
1277b8d26b3bSNicholas Bellinger 
1278b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1279b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1280b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
1281b8d26b3bSNicholas Bellinger 
1282b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1283b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1284b8d26b3bSNicholas Bellinger 		return 0;
1285b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1286b8d26b3bSNicholas Bellinger 		dump_payload = true;
1287b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1288b8d26b3bSNicholas Bellinger 	}
1289b8d26b3bSNicholas Bellinger 
1290b8d26b3bSNicholas Bellinger 	if (!imm_data)
1291b8d26b3bSNicholas Bellinger 		return 0;
1292b8d26b3bSNicholas Bellinger 
1293b8d26b3bSNicholas Bellinger 	sg = &cmd->se_cmd.t_data_sg[0];
1294b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1295b8d26b3bSNicholas Bellinger 
1296b8d26b3bSNicholas Bellinger 	pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1297b8d26b3bSNicholas Bellinger 		 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1298b8d26b3bSNicholas Bellinger 
1299b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1300b8d26b3bSNicholas Bellinger 
1301b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1302b8d26b3bSNicholas Bellinger 
1303b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1304b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1305b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1306b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1307b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1308b8d26b3bSNicholas Bellinger 	}
1309b8d26b3bSNicholas Bellinger 
1310b8d26b3bSNicholas Bellinger sequence_cmd:
1311561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1312b8d26b3bSNicholas Bellinger 
1313b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1314b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
13156cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
13166cc44a6fSNicholas Bellinger 		target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1317b8d26b3bSNicholas Bellinger 
1318b8d26b3bSNicholas Bellinger 	return 0;
1319b8d26b3bSNicholas Bellinger }
1320b8d26b3bSNicholas Bellinger 
1321b8d26b3bSNicholas Bellinger static int
1322b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1323b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1324b8d26b3bSNicholas Bellinger {
1325b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1326b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1327b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1328b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1329b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1330b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1331b8d26b3bSNicholas Bellinger 
1332b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1333b8d26b3bSNicholas Bellinger 	if (rc < 0)
1334b8d26b3bSNicholas Bellinger 		return rc;
1335b8d26b3bSNicholas Bellinger 	else if (!cmd)
1336b8d26b3bSNicholas Bellinger 		return 0;
1337b8d26b3bSNicholas Bellinger 	/*
1338b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1339b8d26b3bSNicholas Bellinger 	 */
1340b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
1341b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected solicited data payload\n");
1342b8d26b3bSNicholas Bellinger 		dump_stack();
1343b8d26b3bSNicholas Bellinger 		return -1;
1344b8d26b3bSNicholas Bellinger 	}
1345b8d26b3bSNicholas Bellinger 
1346b8d26b3bSNicholas Bellinger 	pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1347b8d26b3bSNicholas Bellinger 		 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1348b8d26b3bSNicholas Bellinger 
1349b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1350b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1351b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1352b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1353b8d26b3bSNicholas Bellinger 	/*
1354b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1355b8d26b3bSNicholas Bellinger 	 */
1356b8d26b3bSNicholas Bellinger 	if (page_off) {
1357b8d26b3bSNicholas Bellinger 		pr_err("Received unexpected non-page aligned data payload\n");
1358b8d26b3bSNicholas Bellinger 		dump_stack();
1359b8d26b3bSNicholas Bellinger 		return -1;
1360b8d26b3bSNicholas Bellinger 	}
1361b8d26b3bSNicholas Bellinger 	pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1362b8d26b3bSNicholas Bellinger 		 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1363b8d26b3bSNicholas Bellinger 
1364b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1365b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1366b8d26b3bSNicholas Bellinger 
1367b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1368b8d26b3bSNicholas Bellinger 	if (rc < 0)
1369b8d26b3bSNicholas Bellinger 		return rc;
1370b8d26b3bSNicholas Bellinger 
1371b8d26b3bSNicholas Bellinger 	return 0;
1372b8d26b3bSNicholas Bellinger }
1373b8d26b3bSNicholas Bellinger 
1374b8d26b3bSNicholas Bellinger static int
1375778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1376d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1377d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1378778de368SNicholas Bellinger {
1379778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1380778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1381778de368SNicholas Bellinger 	int rc;
1382778de368SNicholas Bellinger 
1383778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1384778de368SNicholas Bellinger 	if (rc < 0)
1385778de368SNicholas Bellinger 		return rc;
1386778de368SNicholas Bellinger 	/*
1387778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1388778de368SNicholas Bellinger 	 */
1389778de368SNicholas Bellinger 
1390778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1391778de368SNicholas Bellinger }
1392778de368SNicholas Bellinger 
1393778de368SNicholas Bellinger static int
1394adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1395d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1396d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1397adb54c29SNicholas Bellinger {
1398adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1399adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1400adb54c29SNicholas Bellinger 	int rc;
1401adb54c29SNicholas Bellinger 	unsigned char *text_in;
1402adb54c29SNicholas Bellinger 
1403adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1404adb54c29SNicholas Bellinger 	if (rc < 0)
1405adb54c29SNicholas Bellinger 		return rc;
1406adb54c29SNicholas Bellinger 
1407adb54c29SNicholas Bellinger 	text_in = kzalloc(payload_length, GFP_KERNEL);
1408adb54c29SNicholas Bellinger 	if (!text_in) {
1409adb54c29SNicholas Bellinger 		pr_err("Unable to allocate text_in of payload_length: %u\n",
1410adb54c29SNicholas Bellinger 		       payload_length);
1411adb54c29SNicholas Bellinger 		return -ENOMEM;
1412adb54c29SNicholas Bellinger 	}
1413adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1414adb54c29SNicholas Bellinger 
1415adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1416adb54c29SNicholas Bellinger 
1417adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1418adb54c29SNicholas Bellinger }
1419adb54c29SNicholas Bellinger 
1420adb54c29SNicholas Bellinger static int
1421b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1422b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1423b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1424b8d26b3bSNicholas Bellinger {
1425b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1426b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1427ca40d24eSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1428b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1429b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1430b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1431b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1432b8d26b3bSNicholas Bellinger 
1433ca40d24eSNicholas Bellinger 	if (sess->sess_ops->SessionType &&
1434ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1435ca40d24eSNicholas Bellinger 		pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1436ca40d24eSNicholas Bellinger 		       " ignoring\n", opcode);
1437ca40d24eSNicholas Bellinger 		return 0;
1438ca40d24eSNicholas Bellinger 	}
1439ca40d24eSNicholas Bellinger 
1440b8d26b3bSNicholas Bellinger 	switch (opcode) {
1441b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1442676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1443b8d26b3bSNicholas Bellinger 		if (!cmd)
1444b8d26b3bSNicholas Bellinger 			break;
1445b8d26b3bSNicholas Bellinger 
1446d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1447b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1448b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1449b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1450b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1451b8d26b3bSNicholas Bellinger 
1452d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1453b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1454b8d26b3bSNicholas Bellinger 		break;
1455b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1456676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1457b8d26b3bSNicholas Bellinger 		if (!cmd)
1458b8d26b3bSNicholas Bellinger 			break;
1459b8d26b3bSNicholas Bellinger 
1460d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1461d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1462778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1463b8d26b3bSNicholas Bellinger 		break;
1464b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1465b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1466b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1467b8d26b3bSNicholas Bellinger 		break;
1468b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1469676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1470b8d26b3bSNicholas Bellinger 		if (!cmd)
1471b8d26b3bSNicholas Bellinger 			break;
1472b8d26b3bSNicholas Bellinger 
1473b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1474b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1475b8d26b3bSNicholas Bellinger 		break;
1476b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
1477676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1478b8d26b3bSNicholas Bellinger 		if (!cmd)
1479b8d26b3bSNicholas Bellinger 			break;
1480b8d26b3bSNicholas Bellinger 
1481b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1482b8d26b3bSNicholas Bellinger 		if (ret > 0)
1483b8d26b3bSNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
1484b8d26b3bSNicholas Bellinger 						    SECONDS_FOR_LOGOUT_COMP *
1485b8d26b3bSNicholas Bellinger 						    HZ);
1486b8d26b3bSNicholas Bellinger 		break;
1487adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1488676687c6SNicholas Bellinger 		cmd = isert_allocate_cmd(conn);
1489adb54c29SNicholas Bellinger 		if (!cmd)
1490adb54c29SNicholas Bellinger 			break;
1491adb54c29SNicholas Bellinger 
1492d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1493d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1494adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1495adb54c29SNicholas Bellinger 		break;
1496b8d26b3bSNicholas Bellinger 	default:
1497b8d26b3bSNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1498b8d26b3bSNicholas Bellinger 		dump_stack();
1499b8d26b3bSNicholas Bellinger 		break;
1500b8d26b3bSNicholas Bellinger 	}
1501b8d26b3bSNicholas Bellinger 
1502b8d26b3bSNicholas Bellinger 	return ret;
1503b8d26b3bSNicholas Bellinger }
1504b8d26b3bSNicholas Bellinger 
1505b8d26b3bSNicholas Bellinger static void
1506b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1507b8d26b3bSNicholas Bellinger {
1508b8d26b3bSNicholas Bellinger 	struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1509b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1510b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1511b8d26b3bSNicholas Bellinger 	int rc;
1512b8d26b3bSNicholas Bellinger 
1513b8d26b3bSNicholas Bellinger 	switch (iser_hdr->flags & 0xF0) {
1514b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1515b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_RSV) {
1516b8d26b3bSNicholas Bellinger 			read_stag = be32_to_cpu(iser_hdr->read_stag);
1517b8d26b3bSNicholas Bellinger 			read_va = be64_to_cpu(iser_hdr->read_va);
1518b8d26b3bSNicholas Bellinger 			pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1519b8d26b3bSNicholas Bellinger 				 read_stag, (unsigned long long)read_va);
1520b8d26b3bSNicholas Bellinger 		}
1521b8d26b3bSNicholas Bellinger 		if (iser_hdr->flags & ISER_WSV) {
1522b8d26b3bSNicholas Bellinger 			write_stag = be32_to_cpu(iser_hdr->write_stag);
1523b8d26b3bSNicholas Bellinger 			write_va = be64_to_cpu(iser_hdr->write_va);
1524b8d26b3bSNicholas Bellinger 			pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1525b8d26b3bSNicholas Bellinger 				 write_stag, (unsigned long long)write_va);
1526b8d26b3bSNicholas Bellinger 		}
1527b8d26b3bSNicholas Bellinger 
1528b8d26b3bSNicholas Bellinger 		pr_debug("ISER ISCSI_CTRL PDU\n");
1529b8d26b3bSNicholas Bellinger 		break;
1530b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
1531b8d26b3bSNicholas Bellinger 		pr_err("iSER Hello message\n");
1532b8d26b3bSNicholas Bellinger 		break;
1533b8d26b3bSNicholas Bellinger 	default:
1534b8d26b3bSNicholas Bellinger 		pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1535b8d26b3bSNicholas Bellinger 		break;
1536b8d26b3bSNicholas Bellinger 	}
1537b8d26b3bSNicholas Bellinger 
1538b8d26b3bSNicholas Bellinger 	rc = isert_rx_opcode(isert_conn, rx_desc,
1539b8d26b3bSNicholas Bellinger 			     read_stag, read_va, write_stag, write_va);
1540b8d26b3bSNicholas Bellinger }
1541b8d26b3bSNicholas Bellinger 
1542b8d26b3bSNicholas Bellinger static void
1543b8d26b3bSNicholas Bellinger isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1544b8d26b3bSNicholas Bellinger 		    unsigned long xfer_len)
1545b8d26b3bSNicholas Bellinger {
1546b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1547b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1548b8d26b3bSNicholas Bellinger 	u64 rx_dma;
1549b8d26b3bSNicholas Bellinger 	int rx_buflen, outstanding;
1550b8d26b3bSNicholas Bellinger 
1551b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1552b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1553b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
1554b8d26b3bSNicholas Bellinger 		pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1555b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1556b8d26b3bSNicholas Bellinger 	} else {
1557b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1558b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
1559b8d26b3bSNicholas Bellinger 		pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1560b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1561b8d26b3bSNicholas Bellinger 	}
1562b8d26b3bSNicholas Bellinger 
1563b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1564b8d26b3bSNicholas Bellinger 
1565b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
1566b8d26b3bSNicholas Bellinger 	pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1567b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1568b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1569b8d26b3bSNicholas Bellinger 
15702371e5daSSagi Grimberg 	if ((char *)desc == isert_conn->login_req_buf) {
15712371e5daSSagi Grimberg 		isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
15722371e5daSSagi Grimberg 		if (isert_conn->conn) {
15732371e5daSSagi Grimberg 			struct iscsi_login *login = isert_conn->conn->conn_login;
15742371e5daSSagi Grimberg 
15752371e5daSSagi Grimberg 			if (login && !login->first_request)
15762371e5daSSagi Grimberg 				isert_rx_login_req(isert_conn);
15772371e5daSSagi Grimberg 		}
15782371e5daSSagi Grimberg 		mutex_lock(&isert_conn->conn_mutex);
15792371e5daSSagi Grimberg 		complete(&isert_conn->login_req_comp);
15802371e5daSSagi Grimberg 		mutex_unlock(&isert_conn->conn_mutex);
15812371e5daSSagi Grimberg 	} else {
1582b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
15832371e5daSSagi Grimberg 	}
1584b8d26b3bSNicholas Bellinger 
1585b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1586b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1587b8d26b3bSNicholas Bellinger 
1588b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1589b8d26b3bSNicholas Bellinger 	pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1590b8d26b3bSNicholas Bellinger 		 isert_conn->post_recv_buf_count);
1591b8d26b3bSNicholas Bellinger 
1592b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf)
1593b8d26b3bSNicholas Bellinger 		return;
1594b8d26b3bSNicholas Bellinger 
1595b8d26b3bSNicholas Bellinger 	outstanding = isert_conn->post_recv_buf_count;
1596b8d26b3bSNicholas Bellinger 	if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1597b8d26b3bSNicholas Bellinger 		int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1598b8d26b3bSNicholas Bellinger 				ISERT_MIN_POSTED_RX);
1599b8d26b3bSNicholas Bellinger 		err = isert_post_recv(isert_conn, count);
1600b8d26b3bSNicholas Bellinger 		if (err) {
1601b8d26b3bSNicholas Bellinger 			pr_err("isert_post_recv() count: %d failed, %d\n",
1602b8d26b3bSNicholas Bellinger 			       count, err);
1603b8d26b3bSNicholas Bellinger 		}
1604b8d26b3bSNicholas Bellinger 	}
1605b8d26b3bSNicholas Bellinger }
1606b8d26b3bSNicholas Bellinger 
1607e3d7e4c3SSagi Grimberg static int
1608e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1609e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1610e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1611e3d7e4c3SSagi Grimberg {
1612e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1613e3d7e4c3SSagi Grimberg 
1614e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1615e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1616e3d7e4c3SSagi Grimberg 
1617e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1618e3d7e4c3SSagi Grimberg 	data->offset = offset;
1619e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1620e3d7e4c3SSagi Grimberg 
1621e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1622e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1623e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1624e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1625e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1626e3d7e4c3SSagi Grimberg 
1627e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1628e3d7e4c3SSagi Grimberg 					data->dma_dir);
1629e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
1630e3d7e4c3SSagi Grimberg 		pr_err("Cmd: unable to dma map SGs %p\n", sg);
1631e3d7e4c3SSagi Grimberg 		return -EINVAL;
1632e3d7e4c3SSagi Grimberg 	}
1633e3d7e4c3SSagi Grimberg 
1634e3d7e4c3SSagi Grimberg 	pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1635e3d7e4c3SSagi Grimberg 		 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1636e3d7e4c3SSagi Grimberg 
1637e3d7e4c3SSagi Grimberg 	return 0;
1638e3d7e4c3SSagi Grimberg }
1639e3d7e4c3SSagi Grimberg 
1640e3d7e4c3SSagi Grimberg static void
1641e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1642e3d7e4c3SSagi Grimberg {
1643e3d7e4c3SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1644e3d7e4c3SSagi Grimberg 
1645e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1646e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1647e3d7e4c3SSagi Grimberg }
1648e3d7e4c3SSagi Grimberg 
1649e3d7e4c3SSagi Grimberg 
1650e3d7e4c3SSagi Grimberg 
1651b8d26b3bSNicholas Bellinger static void
1652b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1653b8d26b3bSNicholas Bellinger {
1654b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1655b8d26b3bSNicholas Bellinger 
165690ecc6e2SVu Pham 	pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1657e3d7e4c3SSagi Grimberg 
1658e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
165990ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1660e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1661b8d26b3bSNicholas Bellinger 	}
1662b8d26b3bSNicholas Bellinger 
166390ecc6e2SVu Pham 	if (wr->send_wr) {
166490ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1665b8d26b3bSNicholas Bellinger 		kfree(wr->send_wr);
1666b8d26b3bSNicholas Bellinger 		wr->send_wr = NULL;
166790ecc6e2SVu Pham 	}
1668b8d26b3bSNicholas Bellinger 
166990ecc6e2SVu Pham 	if (wr->ib_sge) {
167090ecc6e2SVu Pham 		pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
167190ecc6e2SVu Pham 		kfree(wr->ib_sge);
167290ecc6e2SVu Pham 		wr->ib_sge = NULL;
167390ecc6e2SVu Pham 	}
1674b8d26b3bSNicholas Bellinger }
1675b8d26b3bSNicholas Bellinger 
1676b8d26b3bSNicholas Bellinger static void
1677a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
167859464ef4SVu Pham {
167959464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
168059464ef4SVu Pham 	LIST_HEAD(unmap_list);
168159464ef4SVu Pham 
1682a3a5a826SSagi Grimberg 	pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
168359464ef4SVu Pham 
168459464ef4SVu Pham 	if (wr->fr_desc) {
1685a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
168659464ef4SVu Pham 			 isert_cmd, wr->fr_desc);
16879e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
16889e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
16899e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
16909e961ae7SSagi Grimberg 		}
169159464ef4SVu Pham 		spin_lock_bh(&isert_conn->conn_lock);
1692a3a5a826SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
169359464ef4SVu Pham 		spin_unlock_bh(&isert_conn->conn_lock);
169459464ef4SVu Pham 		wr->fr_desc = NULL;
169559464ef4SVu Pham 	}
169659464ef4SVu Pham 
1697e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
1698a3a5a826SSagi Grimberg 		pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1699e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
170059464ef4SVu Pham 	}
170159464ef4SVu Pham 
170259464ef4SVu Pham 	wr->ib_sge = NULL;
170359464ef4SVu Pham 	wr->send_wr = NULL;
170459464ef4SVu Pham }
170559464ef4SVu Pham 
170659464ef4SVu Pham static void
170703e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1708b8d26b3bSNicholas Bellinger {
1709d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1710b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1711186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1712d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
1713b8d26b3bSNicholas Bellinger 
1714b8d26b3bSNicholas Bellinger 	pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1715b8d26b3bSNicholas Bellinger 
1716b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1717b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1718b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1719b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17205159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1721b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1722b8d26b3bSNicholas Bellinger 
172303e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1724b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
172503e7848aSNicholas Bellinger 			/*
172603e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
172703e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
172803e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
172903e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
173003e7848aSNicholas Bellinger 			 */
173103e7848aSNicholas Bellinger 			if (comp_err &&
173203e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
173303e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
173403e7848aSNicholas Bellinger 
173503e7848aSNicholas Bellinger 				target_put_sess_cmd(se_cmd->se_sess, se_cmd);
173603e7848aSNicholas Bellinger 			}
173703e7848aSNicholas Bellinger 		}
1738b8d26b3bSNicholas Bellinger 
1739d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1740186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1741186a9647SNicholas Bellinger 		break;
1742b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1743186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1744186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17455159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1746186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1747186a9647SNicholas Bellinger 
1748b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1749b8d26b3bSNicholas Bellinger 		break;
1750b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1751b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1752adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1753b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1754b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17555159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1756b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1757b8d26b3bSNicholas Bellinger 
1758b8d26b3bSNicholas Bellinger 		/*
1759b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1760b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1761b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1762b8d26b3bSNicholas Bellinger 		 */
1763b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
17643df8f68aSNicholas Bellinger 			pr_debug("Calling transport_generic_free_cmd from"
17653df8f68aSNicholas Bellinger 				 " isert_put_cmd for 0x%02x\n",
17663df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1767b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1768b8d26b3bSNicholas Bellinger 			break;
1769b8d26b3bSNicholas Bellinger 		}
1770b8d26b3bSNicholas Bellinger 		/*
1771b8d26b3bSNicholas Bellinger 		 * Fall-through
1772b8d26b3bSNicholas Bellinger 		 */
1773b8d26b3bSNicholas Bellinger 	default:
1774d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1775b8d26b3bSNicholas Bellinger 		break;
1776b8d26b3bSNicholas Bellinger 	}
1777b8d26b3bSNicholas Bellinger }
1778b8d26b3bSNicholas Bellinger 
1779b8d26b3bSNicholas Bellinger static void
1780b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1781b8d26b3bSNicholas Bellinger {
1782b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
1783b8d26b3bSNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1784b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1785b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1786b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1787b8d26b3bSNicholas Bellinger 	}
1788b8d26b3bSNicholas Bellinger }
1789b8d26b3bSNicholas Bellinger 
1790b8d26b3bSNicholas Bellinger static void
1791b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
179203e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1793b8d26b3bSNicholas Bellinger {
1794dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
1795dbbc5d11SNicholas Bellinger 		pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1796dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1797dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1798dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1799b8d26b3bSNicholas Bellinger 	}
1800b8d26b3bSNicholas Bellinger 
1801b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
180203e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1803b8d26b3bSNicholas Bellinger }
1804b8d26b3bSNicholas Bellinger 
180596b7973eSSagi Grimberg static int
180696b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1807f93f3a70SSagi Grimberg {
18089e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
180996b7973eSSagi Grimberg 	int ret;
1810f93f3a70SSagi Grimberg 
181196b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
18129e961ae7SSagi Grimberg 	if (ret) {
18139e961ae7SSagi Grimberg 		pr_err("ib_check_mr_status failed, ret %d\n", ret);
18149e961ae7SSagi Grimberg 		goto fail_mr_status;
18159e961ae7SSagi Grimberg 	}
181696b7973eSSagi Grimberg 
18179e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
181896b7973eSSagi Grimberg 		u64 sec_offset_err;
18199e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
18209e961ae7SSagi Grimberg 
18219e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
18229e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
18239e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
18249e961ae7SSagi Grimberg 			break;
18259e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
18269e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
18279e961ae7SSagi Grimberg 			break;
18289e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
18299e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
18309e961ae7SSagi Grimberg 			break;
18319e961ae7SSagi Grimberg 		}
183296b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
183396b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
183496b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
18359e961ae7SSagi Grimberg 
18369e961ae7SSagi Grimberg 		pr_err("isert: PI error found type %d at sector 0x%llx "
18379e961ae7SSagi Grimberg 		       "expected 0x%x vs actual 0x%x\n",
18389e961ae7SSagi Grimberg 		       mr_status.sig_err.err_type,
18399e961ae7SSagi Grimberg 		       (unsigned long long)se_cmd->bad_sector,
18409e961ae7SSagi Grimberg 		       mr_status.sig_err.expected,
18419e961ae7SSagi Grimberg 		       mr_status.sig_err.actual);
18429e961ae7SSagi Grimberg 		ret = 1;
18439e961ae7SSagi Grimberg 	}
18449e961ae7SSagi Grimberg 
18459e961ae7SSagi Grimberg fail_mr_status:
184696b7973eSSagi Grimberg 	return ret;
184796b7973eSSagi Grimberg }
184896b7973eSSagi Grimberg 
184996b7973eSSagi Grimberg static void
185096b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
185196b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
185296b7973eSSagi Grimberg {
185396b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
185496b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
185596b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
185696b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
185796b7973eSSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
185896b7973eSSagi Grimberg 	int ret = 0;
185996b7973eSSagi Grimberg 
186096b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
186196b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
186296b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
186396b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
186496b7973eSSagi Grimberg 	}
186596b7973eSSagi Grimberg 
1866f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1867897bb2c9SSagi Grimberg 	wr->send_wr_num = 0;
18689e961ae7SSagi Grimberg 	if (ret)
18699e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18709e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
18719e961ae7SSagi Grimberg 	else
1872f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1873f93f3a70SSagi Grimberg }
1874f93f3a70SSagi Grimberg 
1875f93f3a70SSagi Grimberg static void
1876b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1877b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1878b8d26b3bSNicholas Bellinger {
1879b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1880d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1881b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
188290ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1883d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
18845bac4b1aSSagi Grimberg 	int ret = 0;
1885b8d26b3bSNicholas Bellinger 
18869e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
188796b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
188896b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
188996b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
18909e961ae7SSagi Grimberg 	}
18919e961ae7SSagi Grimberg 
1892b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1893d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1894e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1895b6b87a1dSNicholas Bellinger 	wr->send_wr_num = 0;
1896b8d26b3bSNicholas Bellinger 
189790ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1898b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1899b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1900b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1901b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1902b8d26b3bSNicholas Bellinger 
19035bac4b1aSSagi Grimberg 	if (ret)
19045bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
19055bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
19065bac4b1aSSagi Grimberg 	else
1907b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1908b8d26b3bSNicholas Bellinger }
1909b8d26b3bSNicholas Bellinger 
1910b8d26b3bSNicholas Bellinger static void
1911b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1912b8d26b3bSNicholas Bellinger {
1913b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1914b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1915b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1916b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1917d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1918b8d26b3bSNicholas Bellinger 
1919b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1920b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1921b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1922b8d26b3bSNicholas Bellinger 
1923b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1924b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
1925b8d26b3bSNicholas Bellinger 
1926b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
192703e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1928b8d26b3bSNicholas Bellinger 		break;
1929b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
1930b8d26b3bSNicholas Bellinger 		pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1931b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1932b8d26b3bSNicholas Bellinger 
1933b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
193403e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
19353df8f68aSNicholas Bellinger 		break;
1936b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1937b8d26b3bSNicholas Bellinger 		pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
19389d49f5e2SSagi Grimberg 
19399d49f5e2SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1940b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1941b8d26b3bSNicholas Bellinger 		break;
1942adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
1943adb54c29SNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
1944adb54c29SNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
194503e7848aSNicholas Bellinger 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1946adb54c29SNicholas Bellinger 		break;
1947b8d26b3bSNicholas Bellinger 	default:
1948b8d26b3bSNicholas Bellinger 		pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1949b8d26b3bSNicholas Bellinger 		dump_stack();
1950b8d26b3bSNicholas Bellinger 		break;
1951b8d26b3bSNicholas Bellinger 	}
1952b8d26b3bSNicholas Bellinger }
1953b8d26b3bSNicholas Bellinger 
1954b8d26b3bSNicholas Bellinger static void
1955b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1956b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1957b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1958b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1959b8d26b3bSNicholas Bellinger {
1960d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1961b6b87a1dSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1962b8d26b3bSNicholas Bellinger 
1963b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
19643df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1965adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1966adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1967b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1968b8d26b3bSNicholas Bellinger 
1969b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1970b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1971b8d26b3bSNicholas Bellinger 		return;
1972b8d26b3bSNicholas Bellinger 	}
1973897bb2c9SSagi Grimberg 
1974897bb2c9SSagi Grimberg 	/**
1975897bb2c9SSagi Grimberg 	 * If send_wr_num is 0 this means that we got
1976897bb2c9SSagi Grimberg 	 * RDMA completion and we cleared it and we should
1977897bb2c9SSagi Grimberg 	 * simply decrement the response post. else the
1978897bb2c9SSagi Grimberg 	 * response is incorporated in send_wr_num, just
1979897bb2c9SSagi Grimberg 	 * sub it.
1980897bb2c9SSagi Grimberg 	 **/
1981897bb2c9SSagi Grimberg 	if (wr->send_wr_num)
1982897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1983897bb2c9SSagi Grimberg 	else
1984897bb2c9SSagi Grimberg 		atomic_dec(&isert_conn->post_send_buf_count);
1985b8d26b3bSNicholas Bellinger 
1986b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
198703e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1988b8d26b3bSNicholas Bellinger }
1989b8d26b3bSNicholas Bellinger 
1990b8d26b3bSNicholas Bellinger static void
199195b60f07SNicholas Bellinger __isert_send_completion(struct iser_tx_desc *tx_desc,
1992b8d26b3bSNicholas Bellinger 		        struct isert_conn *isert_conn)
1993b8d26b3bSNicholas Bellinger {
1994b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1995b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1996b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1997b8d26b3bSNicholas Bellinger 
1998b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1999b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2000b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
2001b8d26b3bSNicholas Bellinger 		return;
2002b8d26b3bSNicholas Bellinger 	}
2003b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
2004b8d26b3bSNicholas Bellinger 
2005b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
2006b8d26b3bSNicholas Bellinger 	case ISER_IB_RECV:
2007b8d26b3bSNicholas Bellinger 		pr_err("isert_send_completion: Got ISER_IB_RECV\n");
2008b8d26b3bSNicholas Bellinger 		dump_stack();
2009b8d26b3bSNicholas Bellinger 		break;
2010b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
2011b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
2012b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
2013b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
2014b8d26b3bSNicholas Bellinger 		break;
2015b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
2016f93f3a70SSagi Grimberg 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
2017897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2018f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
2019b8d26b3bSNicholas Bellinger 		break;
2020b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
2021b8d26b3bSNicholas Bellinger 		pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
2022b8d26b3bSNicholas Bellinger 
2023b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2024b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
2025b8d26b3bSNicholas Bellinger 		break;
2026b8d26b3bSNicholas Bellinger 	default:
2027b8d26b3bSNicholas Bellinger 		pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
2028b8d26b3bSNicholas Bellinger 		dump_stack();
2029b8d26b3bSNicholas Bellinger 		break;
2030b8d26b3bSNicholas Bellinger 	}
2031b8d26b3bSNicholas Bellinger }
2032b8d26b3bSNicholas Bellinger 
2033b8d26b3bSNicholas Bellinger static void
203495b60f07SNicholas Bellinger isert_send_completion(struct iser_tx_desc *tx_desc,
203595b60f07SNicholas Bellinger 		      struct isert_conn *isert_conn)
203695b60f07SNicholas Bellinger {
203795b60f07SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
203895b60f07SNicholas Bellinger 	struct iser_tx_desc *t;
203995b60f07SNicholas Bellinger 	/*
204095b60f07SNicholas Bellinger 	 * Drain coalesced completion llist starting from comp_llnode_batch
204195b60f07SNicholas Bellinger 	 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
204295b60f07SNicholas Bellinger 	 */
204395b60f07SNicholas Bellinger 	while (llnode) {
204495b60f07SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
204595b60f07SNicholas Bellinger 		llnode = llist_next(llnode);
204695b60f07SNicholas Bellinger 		__isert_send_completion(t, isert_conn);
204795b60f07SNicholas Bellinger 	}
204895b60f07SNicholas Bellinger 	__isert_send_completion(tx_desc, isert_conn);
204995b60f07SNicholas Bellinger }
205095b60f07SNicholas Bellinger 
205195b60f07SNicholas Bellinger static void
2052ebbe4421SNicholas Bellinger isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
2053ebbe4421SNicholas Bellinger {
2054ebbe4421SNicholas Bellinger 	struct llist_node *llnode;
2055ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
2056ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
2057ebbe4421SNicholas Bellinger 
2058ebbe4421SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2059ebbe4421SNicholas Bellinger 	llnode = llist_del_all(&isert_conn->conn_comp_llist);
2060ebbe4421SNicholas Bellinger 	isert_conn->conn_comp_batch = 0;
2061ebbe4421SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2062ebbe4421SNicholas Bellinger 
2063ebbe4421SNicholas Bellinger 	while (llnode) {
2064ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2065ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
2066ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
2067ebbe4421SNicholas Bellinger 
2068897bb2c9SSagi Grimberg 		/**
2069897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
2070897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
2071897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
2072897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
2073897bb2c9SSagi Grimberg 		 * sub it.
2074897bb2c9SSagi Grimberg 		 **/
2075897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
2076897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
2077897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
2078897bb2c9SSagi Grimberg 		else
2079897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
208003e7848aSNicholas Bellinger 
208103e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
2082ebbe4421SNicholas Bellinger 	}
2083ebbe4421SNicholas Bellinger }
2084ebbe4421SNicholas Bellinger 
2085ebbe4421SNicholas Bellinger static void
2086defd8848SNicholas Bellinger isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
2087b8d26b3bSNicholas Bellinger {
2088b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2089b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2090ebbe4421SNicholas Bellinger 	struct llist_node *llnode = tx_desc->comp_llnode_batch;
2091ebbe4421SNicholas Bellinger 	struct isert_rdma_wr *wr;
2092ebbe4421SNicholas Bellinger 	struct iser_tx_desc *t;
2093ebbe4421SNicholas Bellinger 
2094ebbe4421SNicholas Bellinger 	while (llnode) {
2095ebbe4421SNicholas Bellinger 		t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2096ebbe4421SNicholas Bellinger 		llnode = llist_next(llnode);
2097ebbe4421SNicholas Bellinger 		wr = &t->isert_cmd->rdma_wr;
2098ebbe4421SNicholas Bellinger 
2099897bb2c9SSagi Grimberg 		/**
2100897bb2c9SSagi Grimberg 		 * If send_wr_num is 0 this means that we got
2101897bb2c9SSagi Grimberg 		 * RDMA completion and we cleared it and we should
2102897bb2c9SSagi Grimberg 		 * simply decrement the response post. else the
2103897bb2c9SSagi Grimberg 		 * response is incorporated in send_wr_num, just
2104897bb2c9SSagi Grimberg 		 * sub it.
2105897bb2c9SSagi Grimberg 		 **/
2106897bb2c9SSagi Grimberg 		if (wr->send_wr_num)
2107897bb2c9SSagi Grimberg 			atomic_sub(wr->send_wr_num,
2108897bb2c9SSagi Grimberg 				   &isert_conn->post_send_buf_count);
2109897bb2c9SSagi Grimberg 		else
2110897bb2c9SSagi Grimberg 			atomic_dec(&isert_conn->post_send_buf_count);
211103e7848aSNicholas Bellinger 
211203e7848aSNicholas Bellinger 		isert_completion_put(t, t->isert_cmd, ib_dev, true);
2113ebbe4421SNicholas Bellinger 	}
2114ebbe4421SNicholas Bellinger 	tx_desc->comp_llnode_batch = NULL;
2115b8d26b3bSNicholas Bellinger 
2116b8d26b3bSNicholas Bellinger 	if (!isert_cmd)
2117b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
2118b8d26b3bSNicholas Bellinger 	else
211903e7848aSNicholas Bellinger 		isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
2120b8d26b3bSNicholas Bellinger }
2121b8d26b3bSNicholas Bellinger 
2122defd8848SNicholas Bellinger static void
2123defd8848SNicholas Bellinger isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2124defd8848SNicholas Bellinger {
2125defd8848SNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2126defd8848SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
2127defd8848SNicholas Bellinger 
2128defd8848SNicholas Bellinger 	if (isert_conn->post_recv_buf_count)
2129defd8848SNicholas Bellinger 		return;
2130defd8848SNicholas Bellinger 
2131ebbe4421SNicholas Bellinger 	isert_cq_drain_comp_llist(isert_conn, ib_dev);
2132ebbe4421SNicholas Bellinger 
2133defd8848SNicholas Bellinger 	if (conn->sess) {
2134defd8848SNicholas Bellinger 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2135defd8848SNicholas Bellinger 		target_wait_for_sess_cmds(conn->sess->se_sess);
2136defd8848SNicholas Bellinger 	}
2137defd8848SNicholas Bellinger 
2138defd8848SNicholas Bellinger 	while (atomic_read(&isert_conn->post_send_buf_count))
2139defd8848SNicholas Bellinger 		msleep(3000);
2140b8d26b3bSNicholas Bellinger 
2141b2cb9649SNicholas Bellinger 	mutex_lock(&isert_conn->conn_mutex);
2142954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
2143b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
2144b2cb9649SNicholas Bellinger 
21459d49f5e2SSagi Grimberg 	iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
21469d49f5e2SSagi Grimberg 
2147defd8848SNicholas Bellinger 	complete(&isert_conn->conn_wait_comp_err);
2148b8d26b3bSNicholas Bellinger }
2149b8d26b3bSNicholas Bellinger 
2150b8d26b3bSNicholas Bellinger static void
2151b8d26b3bSNicholas Bellinger isert_cq_tx_work(struct work_struct *work)
2152b8d26b3bSNicholas Bellinger {
2153b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2154b8d26b3bSNicholas Bellinger 				struct isert_cq_desc, cq_tx_work);
2155b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2156b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2157b8d26b3bSNicholas Bellinger 	struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2158b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2159b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
2160b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2161b8d26b3bSNicholas Bellinger 
2162b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2163b8d26b3bSNicholas Bellinger 		tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2164b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2165b8d26b3bSNicholas Bellinger 
2166b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2167b8d26b3bSNicholas Bellinger 			isert_send_completion(tx_desc, isert_conn);
2168b8d26b3bSNicholas Bellinger 		} else {
2169b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2170b8d26b3bSNicholas Bellinger 			pr_debug("TX wc.status: 0x%08x\n", wc.status);
2171c5a2adbfSNicholas Bellinger 			pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
2172defd8848SNicholas Bellinger 
21739bb4ca68SNicholas Bellinger 			if (wc.wr_id != ISER_FASTREG_LI_WRID) {
2174ebbe4421SNicholas Bellinger 				if (tx_desc->llnode_active)
2175ebbe4421SNicholas Bellinger 					continue;
2176ebbe4421SNicholas Bellinger 
2177b8d26b3bSNicholas Bellinger 				atomic_dec(&isert_conn->post_send_buf_count);
2178defd8848SNicholas Bellinger 				isert_cq_tx_comp_err(tx_desc, isert_conn);
2179b8d26b3bSNicholas Bellinger 			}
2180b8d26b3bSNicholas Bellinger 		}
21819bb4ca68SNicholas Bellinger 	}
2182b8d26b3bSNicholas Bellinger 
2183b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2184b8d26b3bSNicholas Bellinger }
2185b8d26b3bSNicholas Bellinger 
2186b8d26b3bSNicholas Bellinger static void
2187b8d26b3bSNicholas Bellinger isert_cq_tx_callback(struct ib_cq *cq, void *context)
2188b8d26b3bSNicholas Bellinger {
2189b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2190b8d26b3bSNicholas Bellinger 
2191b8d26b3bSNicholas Bellinger 	queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2192b8d26b3bSNicholas Bellinger }
2193b8d26b3bSNicholas Bellinger 
2194b8d26b3bSNicholas Bellinger static void
2195b8d26b3bSNicholas Bellinger isert_cq_rx_work(struct work_struct *work)
2196b8d26b3bSNicholas Bellinger {
2197b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = container_of(work,
2198b8d26b3bSNicholas Bellinger 			struct isert_cq_desc, cq_rx_work);
2199b8d26b3bSNicholas Bellinger 	struct isert_device *device = cq_desc->device;
2200b8d26b3bSNicholas Bellinger 	int cq_index = cq_desc->cq_index;
2201b8d26b3bSNicholas Bellinger 	struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2202b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2203b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
2204b8d26b3bSNicholas Bellinger 	struct ib_wc wc;
2205b8d26b3bSNicholas Bellinger 	unsigned long xfer_len;
2206b8d26b3bSNicholas Bellinger 
2207b8d26b3bSNicholas Bellinger 	while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2208b8d26b3bSNicholas Bellinger 		rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2209b8d26b3bSNicholas Bellinger 		isert_conn = wc.qp->qp_context;
2210b8d26b3bSNicholas Bellinger 
2211b8d26b3bSNicholas Bellinger 		if (wc.status == IB_WC_SUCCESS) {
2212b8d26b3bSNicholas Bellinger 			xfer_len = (unsigned long)wc.byte_len;
2213b8d26b3bSNicholas Bellinger 			isert_rx_completion(rx_desc, isert_conn, xfer_len);
2214b8d26b3bSNicholas Bellinger 		} else {
2215b8d26b3bSNicholas Bellinger 			pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2216c5a2adbfSNicholas Bellinger 			if (wc.status != IB_WC_WR_FLUSH_ERR) {
2217b8d26b3bSNicholas Bellinger 				pr_debug("RX wc.status: 0x%08x\n", wc.status);
2218c5a2adbfSNicholas Bellinger 				pr_debug("RX wc.vendor_err: 0x%08x\n",
2219c5a2adbfSNicholas Bellinger 					 wc.vendor_err);
2220c5a2adbfSNicholas Bellinger 			}
2221b8d26b3bSNicholas Bellinger 			isert_conn->post_recv_buf_count--;
2222defd8848SNicholas Bellinger 			isert_cq_rx_comp_err(isert_conn);
2223b8d26b3bSNicholas Bellinger 		}
2224b8d26b3bSNicholas Bellinger 	}
2225b8d26b3bSNicholas Bellinger 
2226b8d26b3bSNicholas Bellinger 	ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2227b8d26b3bSNicholas Bellinger }
2228b8d26b3bSNicholas Bellinger 
2229b8d26b3bSNicholas Bellinger static void
2230b8d26b3bSNicholas Bellinger isert_cq_rx_callback(struct ib_cq *cq, void *context)
2231b8d26b3bSNicholas Bellinger {
2232b8d26b3bSNicholas Bellinger 	struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2233b8d26b3bSNicholas Bellinger 
2234b8d26b3bSNicholas Bellinger 	queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2235b8d26b3bSNicholas Bellinger }
2236b8d26b3bSNicholas Bellinger 
2237b8d26b3bSNicholas Bellinger static int
2238b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2239b8d26b3bSNicholas Bellinger {
2240b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2241b8d26b3bSNicholas Bellinger 	int ret;
2242b8d26b3bSNicholas Bellinger 
2243b8d26b3bSNicholas Bellinger 	atomic_inc(&isert_conn->post_send_buf_count);
2244b8d26b3bSNicholas Bellinger 
2245b8d26b3bSNicholas Bellinger 	ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2246b8d26b3bSNicholas Bellinger 			   &wr_failed);
2247b8d26b3bSNicholas Bellinger 	if (ret) {
2248b8d26b3bSNicholas Bellinger 		pr_err("ib_post_send failed with %d\n", ret);
2249b8d26b3bSNicholas Bellinger 		atomic_dec(&isert_conn->post_send_buf_count);
2250b8d26b3bSNicholas Bellinger 		return ret;
2251b8d26b3bSNicholas Bellinger 	}
2252b8d26b3bSNicholas Bellinger 	return ret;
2253b8d26b3bSNicholas Bellinger }
2254b8d26b3bSNicholas Bellinger 
2255b8d26b3bSNicholas Bellinger static int
2256b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2257b8d26b3bSNicholas Bellinger {
2258d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2259b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2260b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2261b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2262b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2263b8d26b3bSNicholas Bellinger 
2264b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2265b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2266b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2267b8d26b3bSNicholas Bellinger 	/*
2268b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2269b8d26b3bSNicholas Bellinger 	 */
2270b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2271b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2272b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2273b8d26b3bSNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2274b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2275dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2276b8d26b3bSNicholas Bellinger 
2277b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2278b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2279b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2280b8d26b3bSNicholas Bellinger 
2281b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2282b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2283dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2284b8d26b3bSNicholas Bellinger 
2285dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2286dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2287b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2288b8d26b3bSNicholas Bellinger 
2289dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2290dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2291dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
2292b8d26b3bSNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2293b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2294b8d26b3bSNicholas Bellinger 	}
2295b8d26b3bSNicholas Bellinger 
22960d0f660dSNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2297b8d26b3bSNicholas Bellinger 
2298b8d26b3bSNicholas Bellinger 	pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2299b8d26b3bSNicholas Bellinger 
2300b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2301b8d26b3bSNicholas Bellinger }
2302b8d26b3bSNicholas Bellinger 
2303131e6abcSNicholas Bellinger static void
2304131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2305131e6abcSNicholas Bellinger {
2306131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2307131e6abcSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2308131e6abcSNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2309131e6abcSNicholas Bellinger 
2310131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2311131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2312131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2313131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2314131e6abcSNicholas Bellinger 
2315131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2316131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2317131e6abcSNicholas Bellinger 
2318131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2319131e6abcSNicholas Bellinger }
2320131e6abcSNicholas Bellinger 
2321e70beee7SNicholas Bellinger static enum target_prot_op
2322e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2323e70beee7SNicholas Bellinger {
2324e70beee7SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2325e70beee7SNicholas Bellinger 	struct isert_device *device = isert_conn->conn_device;
2326e70beee7SNicholas Bellinger 
2327*23a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
2328*23a548eeSSagi Grimberg 		if (device->pi_capable) {
2329*23a548eeSSagi Grimberg 			pr_info("conn %p PI offload enabled\n", isert_conn);
2330*23a548eeSSagi Grimberg 			isert_conn->pi_support = true;
2331e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
2332*23a548eeSSagi Grimberg 		}
2333*23a548eeSSagi Grimberg 	}
2334*23a548eeSSagi Grimberg 
2335*23a548eeSSagi Grimberg 	pr_info("conn %p PI offload disabled\n", isert_conn);
2336*23a548eeSSagi Grimberg 	isert_conn->pi_support = false;
2337e70beee7SNicholas Bellinger 
2338e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2339e70beee7SNicholas Bellinger }
2340e70beee7SNicholas Bellinger 
2341b8d26b3bSNicholas Bellinger static int
2342b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2343b8d26b3bSNicholas Bellinger 		bool nopout_response)
2344b8d26b3bSNicholas Bellinger {
2345d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2346b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2347b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2348b8d26b3bSNicholas Bellinger 
2349b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2350b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2351b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2352b8d26b3bSNicholas Bellinger 			       nopout_response);
2353b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
235495b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2355b8d26b3bSNicholas Bellinger 
23568b513d0cSMasanari Iida 	pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2357b8d26b3bSNicholas Bellinger 
2358b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2359b8d26b3bSNicholas Bellinger }
2360b8d26b3bSNicholas Bellinger 
2361b8d26b3bSNicholas Bellinger static int
2362b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2363b8d26b3bSNicholas Bellinger {
2364d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2365b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2366b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2367b8d26b3bSNicholas Bellinger 
2368b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2369b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2370b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2371b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
237295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2373b8d26b3bSNicholas Bellinger 
2374b8d26b3bSNicholas Bellinger 	pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2375b8d26b3bSNicholas Bellinger 
2376b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2377b8d26b3bSNicholas Bellinger }
2378b8d26b3bSNicholas Bellinger 
2379b8d26b3bSNicholas Bellinger static int
2380b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2381b8d26b3bSNicholas Bellinger {
2382d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2383b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2384b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2385b8d26b3bSNicholas Bellinger 
2386b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2387b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2388b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2389b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
239095b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2391b8d26b3bSNicholas Bellinger 
2392b8d26b3bSNicholas Bellinger 	pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2393b8d26b3bSNicholas Bellinger 
2394b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2395b8d26b3bSNicholas Bellinger }
2396b8d26b3bSNicholas Bellinger 
2397b8d26b3bSNicholas Bellinger static int
2398b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2399b8d26b3bSNicholas Bellinger {
2400d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2401b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2402b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
24033df8f68aSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
24043df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
24053df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
24063df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2407b8d26b3bSNicholas Bellinger 
2408b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
24093df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2410b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
24113df8f68aSNicholas Bellinger 
24123df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2413dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
24143df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
24153df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2416dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2417dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
24183df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
24193df8f68aSNicholas Bellinger 	tx_dsg->lkey	= isert_conn->conn_mr->lkey;
24203df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
24213df8f68aSNicholas Bellinger 
242295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2423b8d26b3bSNicholas Bellinger 
2424b8d26b3bSNicholas Bellinger 	pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2425b8d26b3bSNicholas Bellinger 
2426b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2427b8d26b3bSNicholas Bellinger }
2428b8d26b3bSNicholas Bellinger 
2429b8d26b3bSNicholas Bellinger static int
2430adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2431adb54c29SNicholas Bellinger {
2432d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2433adb54c29SNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2434adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2435adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2436adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2437adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2438adb54c29SNicholas Bellinger 	int rc;
2439adb54c29SNicholas Bellinger 
2440adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
244122c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2442adb54c29SNicholas Bellinger 	if (rc < 0)
2443adb54c29SNicholas Bellinger 		return rc;
2444adb54c29SNicholas Bellinger 
2445adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2446adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2447adb54c29SNicholas Bellinger 
2448adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2449adb54c29SNicholas Bellinger 		struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2450adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2451adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2452adb54c29SNicholas Bellinger 
2453adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2454adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2455adb54c29SNicholas Bellinger 
2456adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2457adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2458adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
2459adb54c29SNicholas Bellinger 		tx_dsg->lkey	= isert_conn->conn_mr->lkey;
2460adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2461adb54c29SNicholas Bellinger 	}
246295b60f07SNicholas Bellinger 	isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2463adb54c29SNicholas Bellinger 
2464adb54c29SNicholas Bellinger 	pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2465adb54c29SNicholas Bellinger 
2466adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2467adb54c29SNicholas Bellinger }
2468adb54c29SNicholas Bellinger 
2469adb54c29SNicholas Bellinger static int
2470b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2471b8d26b3bSNicholas Bellinger 		    struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2472b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2473b8d26b3bSNicholas Bellinger {
2474d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2475b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2476b8d26b3bSNicholas Bellinger 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2477b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2478b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2479b8d26b3bSNicholas Bellinger 
2480b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2481b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2482b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2483b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2484b8d26b3bSNicholas Bellinger 
2485b8d26b3bSNicholas Bellinger 	send_wr->sg_list = ib_sge;
2486b8d26b3bSNicholas Bellinger 	send_wr->num_sge = sg_nents;
2487b8d26b3bSNicholas Bellinger 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2488b8d26b3bSNicholas Bellinger 	/*
2489b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2490b8d26b3bSNicholas Bellinger 	 */
2491b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2492b8d26b3bSNicholas Bellinger 		pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2493b8d26b3bSNicholas Bellinger 			 (unsigned long long)tmp_sg->dma_address,
2494b8d26b3bSNicholas Bellinger 			 tmp_sg->length, page_off);
2495b8d26b3bSNicholas Bellinger 
2496b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2497b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2498b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2499b8d26b3bSNicholas Bellinger 		ib_sge->lkey = isert_conn->conn_mr->lkey;
2500b8d26b3bSNicholas Bellinger 
250190ecc6e2SVu Pham 		pr_debug("RDMA ib_sge: addr: 0x%16llx  length: %u lkey: %08x\n",
250290ecc6e2SVu Pham 			 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2503b8d26b3bSNicholas Bellinger 		page_off = 0;
2504b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
2505b8d26b3bSNicholas Bellinger 		ib_sge++;
2506b8d26b3bSNicholas Bellinger 		pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2507b8d26b3bSNicholas Bellinger 	}
2508b8d26b3bSNicholas Bellinger 
2509b8d26b3bSNicholas Bellinger 	pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2510b8d26b3bSNicholas Bellinger 		 send_wr->sg_list, send_wr->num_sge);
2511b8d26b3bSNicholas Bellinger 
2512b8d26b3bSNicholas Bellinger 	return sg_nents;
2513b8d26b3bSNicholas Bellinger }
2514b8d26b3bSNicholas Bellinger 
2515b8d26b3bSNicholas Bellinger static int
251690ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
251790ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2518b8d26b3bSNicholas Bellinger {
2519b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2520d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2521b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2522e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
252390ecc6e2SVu Pham 	struct ib_send_wr *send_wr;
2524b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2525e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2526e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2527b8d26b3bSNicholas Bellinger 
252890ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2529b8d26b3bSNicholas Bellinger 
2530e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2531e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2532e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2533e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2534e3d7e4c3SSagi Grimberg 	if (ret)
2535e3d7e4c3SSagi Grimberg 		return ret;
2536b8d26b3bSNicholas Bellinger 
2537e3d7e4c3SSagi Grimberg 	data_left = data->len;
2538e3d7e4c3SSagi Grimberg 	offset = data->offset;
2539b8d26b3bSNicholas Bellinger 
2540e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2541b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
254290ecc6e2SVu Pham 		pr_warn("Unable to allocate ib_sge\n");
2543b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2544e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2545b8d26b3bSNicholas Bellinger 	}
254690ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2547b8d26b3bSNicholas Bellinger 
2548e3d7e4c3SSagi Grimberg 	wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2549b8d26b3bSNicholas Bellinger 	wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2550b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2551b8d26b3bSNicholas Bellinger 	if (!wr->send_wr) {
255290ecc6e2SVu Pham 		pr_debug("Unable to allocate wr->send_wr\n");
2553b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2554e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2555b8d26b3bSNicholas Bellinger 	}
2556b8d26b3bSNicholas Bellinger 
2557b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2558b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2559b8d26b3bSNicholas Bellinger 
2560b8d26b3bSNicholas Bellinger 	for (i = 0; i < wr->send_wr_num; i++) {
2561b8d26b3bSNicholas Bellinger 		send_wr = &isert_cmd->rdma_wr.send_wr[i];
2562b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2563b8d26b3bSNicholas Bellinger 
2564b8d26b3bSNicholas Bellinger 		send_wr->send_flags = 0;
256590ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
256690ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_WRITE;
2567b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2568b8d26b3bSNicholas Bellinger 			send_wr->wr.rdma.rkey = isert_cmd->read_stag;
256990ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
257090ecc6e2SVu Pham 				send_wr->next = &isert_cmd->tx_desc.send_wr;
257190ecc6e2SVu Pham 			else
257290ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
257390ecc6e2SVu Pham 		} else {
257490ecc6e2SVu Pham 			send_wr->opcode = IB_WR_RDMA_READ;
257590ecc6e2SVu Pham 			send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
257690ecc6e2SVu Pham 			send_wr->wr.rdma.rkey = isert_cmd->write_stag;
257790ecc6e2SVu Pham 			if (i + 1 == wr->send_wr_num)
257890ecc6e2SVu Pham 				send_wr->send_flags = IB_SEND_SIGNALED;
257990ecc6e2SVu Pham 			else
258090ecc6e2SVu Pham 				send_wr->next = &wr->send_wr[i + 1];
258190ecc6e2SVu Pham 		}
2582b8d26b3bSNicholas Bellinger 
2583b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2584b8d26b3bSNicholas Bellinger 					send_wr, data_len, offset);
2585b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2586b8d26b3bSNicholas Bellinger 
2587b8d26b3bSNicholas Bellinger 		offset += data_len;
258890ecc6e2SVu Pham 		va_offset += data_len;
2589b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2590b8d26b3bSNicholas Bellinger 	}
259190ecc6e2SVu Pham 
259290ecc6e2SVu Pham 	return 0;
2593e3d7e4c3SSagi Grimberg unmap_cmd:
2594e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2595e3d7e4c3SSagi Grimberg 
259690ecc6e2SVu Pham 	return ret;
259790ecc6e2SVu Pham }
259890ecc6e2SVu Pham 
259990ecc6e2SVu Pham static int
260059464ef4SVu Pham isert_map_fr_pagelist(struct ib_device *ib_dev,
260159464ef4SVu Pham 		      struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
260259464ef4SVu Pham {
260359464ef4SVu Pham 	u64 start_addr, end_addr, page, chunk_start = 0;
260459464ef4SVu Pham 	struct scatterlist *tmp_sg;
260559464ef4SVu Pham 	int i = 0, new_chunk, last_ent, n_pages;
260659464ef4SVu Pham 
260759464ef4SVu Pham 	n_pages = 0;
260859464ef4SVu Pham 	new_chunk = 1;
260959464ef4SVu Pham 	last_ent = sg_nents - 1;
261059464ef4SVu Pham 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
261159464ef4SVu Pham 		start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
261259464ef4SVu Pham 		if (new_chunk)
261359464ef4SVu Pham 			chunk_start = start_addr;
261459464ef4SVu Pham 		end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
261559464ef4SVu Pham 
261659464ef4SVu Pham 		pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
261759464ef4SVu Pham 			 i, (unsigned long long)tmp_sg->dma_address,
261859464ef4SVu Pham 			 tmp_sg->length);
261959464ef4SVu Pham 
262059464ef4SVu Pham 		if ((end_addr & ~PAGE_MASK) && i < last_ent) {
262159464ef4SVu Pham 			new_chunk = 0;
262259464ef4SVu Pham 			continue;
262359464ef4SVu Pham 		}
262459464ef4SVu Pham 		new_chunk = 1;
262559464ef4SVu Pham 
262659464ef4SVu Pham 		page = chunk_start & PAGE_MASK;
262759464ef4SVu Pham 		do {
262859464ef4SVu Pham 			fr_pl[n_pages++] = page;
262959464ef4SVu Pham 			pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
263059464ef4SVu Pham 				 n_pages - 1, page);
263159464ef4SVu Pham 			page += PAGE_SIZE;
263259464ef4SVu Pham 		} while (page < end_addr);
263359464ef4SVu Pham 	}
263459464ef4SVu Pham 
263559464ef4SVu Pham 	return n_pages;
263659464ef4SVu Pham }
263759464ef4SVu Pham 
263859464ef4SVu Pham static int
2639e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2640e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2641e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
26429e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2643e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
264459464ef4SVu Pham {
264559464ef4SVu Pham 	struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
26469e961ae7SSagi Grimberg 	struct ib_mr *mr;
26479e961ae7SSagi Grimberg 	struct ib_fast_reg_page_list *frpl;
264859464ef4SVu Pham 	struct ib_send_wr fr_wr, inv_wr;
264959464ef4SVu Pham 	struct ib_send_wr *bad_wr, *wr = NULL;
26509bd626e7SSagi Grimberg 	int ret, pagelist_len;
26519bd626e7SSagi Grimberg 	u32 page_off;
265259464ef4SVu Pham 	u8 key;
265359464ef4SVu Pham 
2654e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
2655e3d7e4c3SSagi Grimberg 		sge->lkey = isert_conn->conn_mr->lkey;
2656e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2657e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
26589e961ae7SSagi Grimberg 		pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
26599e961ae7SSagi Grimberg 			 __func__, __LINE__, sge->addr, sge->length,
26609e961ae7SSagi Grimberg 			 sge->lkey);
2661e3d7e4c3SSagi Grimberg 		return 0;
2662e3d7e4c3SSagi Grimberg 	}
2663e3d7e4c3SSagi Grimberg 
26649e961ae7SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID) {
26659e961ae7SSagi Grimberg 		/* Registering data buffer */
26669e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
26679e961ae7SSagi Grimberg 		frpl = fr_desc->data_frpl;
26689e961ae7SSagi Grimberg 	} else {
26699e961ae7SSagi Grimberg 		/* Registering protection buffer */
26709e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
26719e961ae7SSagi Grimberg 		frpl = fr_desc->pi_ctx->prot_frpl;
26729e961ae7SSagi Grimberg 	}
26739e961ae7SSagi Grimberg 
2674e3d7e4c3SSagi Grimberg 	page_off = mem->offset % PAGE_SIZE;
267559464ef4SVu Pham 
26769bd626e7SSagi Grimberg 	pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2677e3d7e4c3SSagi Grimberg 		 fr_desc, mem->nents, mem->offset);
267859464ef4SVu Pham 
2679e3d7e4c3SSagi Grimberg 	pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
26809e961ae7SSagi Grimberg 					     &frpl->page_list[0]);
268159464ef4SVu Pham 
2682d3e125daSSagi Grimberg 	if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
268359464ef4SVu Pham 		memset(&inv_wr, 0, sizeof(inv_wr));
26849bb4ca68SNicholas Bellinger 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
268559464ef4SVu Pham 		inv_wr.opcode = IB_WR_LOCAL_INV;
26869e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = mr->rkey;
268759464ef4SVu Pham 		wr = &inv_wr;
268859464ef4SVu Pham 		/* Bump the key */
26899e961ae7SSagi Grimberg 		key = (u8)(mr->rkey & 0x000000FF);
26909e961ae7SSagi Grimberg 		ib_update_fast_reg_key(mr, ++key);
269159464ef4SVu Pham 	}
269259464ef4SVu Pham 
269359464ef4SVu Pham 	/* Prepare FASTREG WR */
269459464ef4SVu Pham 	memset(&fr_wr, 0, sizeof(fr_wr));
26959bb4ca68SNicholas Bellinger 	fr_wr.wr_id = ISER_FASTREG_LI_WRID;
269659464ef4SVu Pham 	fr_wr.opcode = IB_WR_FAST_REG_MR;
26979e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
26989e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.page_list = frpl;
269959464ef4SVu Pham 	fr_wr.wr.fast_reg.page_list_len = pagelist_len;
270059464ef4SVu Pham 	fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2701e3d7e4c3SSagi Grimberg 	fr_wr.wr.fast_reg.length = mem->len;
27029e961ae7SSagi Grimberg 	fr_wr.wr.fast_reg.rkey = mr->rkey;
270359464ef4SVu Pham 	fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
270459464ef4SVu Pham 
270559464ef4SVu Pham 	if (!wr)
270659464ef4SVu Pham 		wr = &fr_wr;
270759464ef4SVu Pham 	else
270859464ef4SVu Pham 		wr->next = &fr_wr;
270959464ef4SVu Pham 
271059464ef4SVu Pham 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
271159464ef4SVu Pham 	if (ret) {
271259464ef4SVu Pham 		pr_err("fast registration failed, ret:%d\n", ret);
271359464ef4SVu Pham 		return ret;
271459464ef4SVu Pham 	}
27159e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
271659464ef4SVu Pham 
27179e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
27189e961ae7SSagi Grimberg 	sge->addr = frpl->page_list[0] + page_off;
2719e3d7e4c3SSagi Grimberg 	sge->length = mem->len;
272059464ef4SVu Pham 
27219e961ae7SSagi Grimberg 	pr_debug("%s:%d sge: addr: 0x%llx  length: %u lkey: %x\n",
27229e961ae7SSagi Grimberg 		 __func__, __LINE__, sge->addr, sge->length,
27239e961ae7SSagi Grimberg 		 sge->lkey);
272459464ef4SVu Pham 
272559464ef4SVu Pham 	return ret;
272659464ef4SVu Pham }
272759464ef4SVu Pham 
27283d73cf1aSSagi Grimberg static inline void
27293d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
27303d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
27319e961ae7SSagi Grimberg {
273278eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
27333d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
27343d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
27353d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
273678eda2bbSSagi Grimberg 	/*
273778eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
273878eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
273978eda2bbSSagi Grimberg 	 * from se_cmd.
274078eda2bbSSagi Grimberg 	 */
274178eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
274278eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
274378eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
274478eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
274578eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
274678eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
27473d73cf1aSSagi Grimberg };
27483d73cf1aSSagi Grimberg 
27493d73cf1aSSagi Grimberg static int
27503d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
27513d73cf1aSSagi Grimberg {
27529e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
27539e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
27549e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
275578eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
27563d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
27579e961ae7SSagi Grimberg 		break;
27589e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
27599e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
276078eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
27613d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
27629e961ae7SSagi Grimberg 		break;
27639e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
27649e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
27653d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
27663d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
27679e961ae7SSagi Grimberg 		break;
27689e961ae7SSagi Grimberg 	default:
27699e961ae7SSagi Grimberg 		pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
27709e961ae7SSagi Grimberg 		return -EINVAL;
27719e961ae7SSagi Grimberg 	}
27729e961ae7SSagi Grimberg 
27739e961ae7SSagi Grimberg 	return 0;
27749e961ae7SSagi Grimberg }
27759e961ae7SSagi Grimberg 
27769e961ae7SSagi Grimberg static inline u8
27779e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
27789e961ae7SSagi Grimberg {
27799e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
27809e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
27819e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
27829e961ae7SSagi Grimberg }
27839e961ae7SSagi Grimberg 
27849e961ae7SSagi Grimberg static int
2785570db170SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn,
2786570db170SSagi Grimberg 		 struct se_cmd *se_cmd,
2787570db170SSagi Grimberg 		 struct isert_rdma_wr *rdma_wr,
2788570db170SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc)
27899e961ae7SSagi Grimberg {
27909e961ae7SSagi Grimberg 	struct ib_send_wr sig_wr, inv_wr;
27919e961ae7SSagi Grimberg 	struct ib_send_wr *bad_wr, *wr = NULL;
27929e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
27939e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
27949e961ae7SSagi Grimberg 	int ret;
27959e961ae7SSagi Grimberg 	u32 key;
27969e961ae7SSagi Grimberg 
27979e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
27989e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
27999e961ae7SSagi Grimberg 	if (ret)
28009e961ae7SSagi Grimberg 		goto err;
28019e961ae7SSagi Grimberg 
28029e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
28039e961ae7SSagi Grimberg 
28049e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
28059e961ae7SSagi Grimberg 		memset(&inv_wr, 0, sizeof(inv_wr));
28069e961ae7SSagi Grimberg 		inv_wr.opcode = IB_WR_LOCAL_INV;
2807c2caa207SSagi Grimberg 		inv_wr.wr_id = ISER_FASTREG_LI_WRID;
28089e961ae7SSagi Grimberg 		inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
28099e961ae7SSagi Grimberg 		wr = &inv_wr;
28109e961ae7SSagi Grimberg 		/* Bump the key */
28119e961ae7SSagi Grimberg 		key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
28129e961ae7SSagi Grimberg 		ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
28139e961ae7SSagi Grimberg 	}
28149e961ae7SSagi Grimberg 
28159e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
28169e961ae7SSagi Grimberg 	sig_wr.opcode = IB_WR_REG_SIG_MR;
2817c2caa207SSagi Grimberg 	sig_wr.wr_id = ISER_FASTREG_LI_WRID;
2818570db170SSagi Grimberg 	sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
28199e961ae7SSagi Grimberg 	sig_wr.num_sge = 1;
28209e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
28219e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
28229e961ae7SSagi Grimberg 	sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
28239e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
2824570db170SSagi Grimberg 		sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
28259e961ae7SSagi Grimberg 
28269e961ae7SSagi Grimberg 	if (!wr)
28279e961ae7SSagi Grimberg 		wr = &sig_wr;
28289e961ae7SSagi Grimberg 	else
28299e961ae7SSagi Grimberg 		wr->next = &sig_wr;
28309e961ae7SSagi Grimberg 
28319e961ae7SSagi Grimberg 	ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
28329e961ae7SSagi Grimberg 	if (ret) {
28339e961ae7SSagi Grimberg 		pr_err("fast registration failed, ret:%d\n", ret);
28349e961ae7SSagi Grimberg 		goto err;
28359e961ae7SSagi Grimberg 	}
28369e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
28379e961ae7SSagi Grimberg 
2838570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2839570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].addr = 0;
2840570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
28419e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
28429e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
28439e961ae7SSagi Grimberg 		/*
28449e961ae7SSagi Grimberg 		 * We have protection guards on the wire
28459e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
28469e961ae7SSagi Grimberg 		 */
2847570db170SSagi Grimberg 		rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
28489e961ae7SSagi Grimberg 
28499e961ae7SSagi Grimberg 	pr_debug("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
2850570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2851570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].lkey);
28529e961ae7SSagi Grimberg err:
28539e961ae7SSagi Grimberg 	return ret;
28549e961ae7SSagi Grimberg }
28559e961ae7SSagi Grimberg 
285659464ef4SVu Pham static int
2857570db170SSagi Grimberg isert_handle_prot_cmd(struct isert_conn *isert_conn,
2858570db170SSagi Grimberg 		      struct isert_cmd *isert_cmd,
2859570db170SSagi Grimberg 		      struct isert_rdma_wr *wr)
2860570db170SSagi Grimberg {
2861570db170SSagi Grimberg 	struct isert_device *device = isert_conn->conn_device;
2862570db170SSagi Grimberg 	struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2863570db170SSagi Grimberg 	int ret;
2864570db170SSagi Grimberg 
2865570db170SSagi Grimberg 	if (!wr->fr_desc->pi_ctx) {
2866570db170SSagi Grimberg 		ret = isert_create_pi_ctx(wr->fr_desc,
2867570db170SSagi Grimberg 					  device->ib_device,
2868570db170SSagi Grimberg 					  isert_conn->conn_pd);
2869570db170SSagi Grimberg 		if (ret) {
2870570db170SSagi Grimberg 			pr_err("conn %p failed to allocate pi_ctx\n",
2871570db170SSagi Grimberg 				  isert_conn);
2872570db170SSagi Grimberg 			return ret;
2873570db170SSagi Grimberg 		}
2874570db170SSagi Grimberg 	}
2875570db170SSagi Grimberg 
2876570db170SSagi Grimberg 	if (se_cmd->t_prot_sg) {
2877570db170SSagi Grimberg 		ret = isert_map_data_buf(isert_conn, isert_cmd,
2878570db170SSagi Grimberg 					 se_cmd->t_prot_sg,
2879570db170SSagi Grimberg 					 se_cmd->t_prot_nents,
2880570db170SSagi Grimberg 					 se_cmd->prot_length,
2881570db170SSagi Grimberg 					 0, wr->iser_ib_op, &wr->prot);
2882570db170SSagi Grimberg 		if (ret) {
2883570db170SSagi Grimberg 			pr_err("conn %p failed to map protection buffer\n",
2884570db170SSagi Grimberg 				  isert_conn);
2885570db170SSagi Grimberg 			return ret;
2886570db170SSagi Grimberg 		}
2887570db170SSagi Grimberg 
2888570db170SSagi Grimberg 		memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2889570db170SSagi Grimberg 		ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2890570db170SSagi Grimberg 					ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2891570db170SSagi Grimberg 		if (ret) {
2892570db170SSagi Grimberg 			pr_err("conn %p failed to fast reg mr\n",
2893570db170SSagi Grimberg 				  isert_conn);
2894570db170SSagi Grimberg 			goto unmap_prot_cmd;
2895570db170SSagi Grimberg 		}
2896570db170SSagi Grimberg 	}
2897570db170SSagi Grimberg 
2898570db170SSagi Grimberg 	ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2899570db170SSagi Grimberg 	if (ret) {
2900570db170SSagi Grimberg 		pr_err("conn %p failed to fast reg mr\n",
2901570db170SSagi Grimberg 			  isert_conn);
2902570db170SSagi Grimberg 		goto unmap_prot_cmd;
2903570db170SSagi Grimberg 	}
2904570db170SSagi Grimberg 	wr->fr_desc->ind |= ISERT_PROTECTED;
2905570db170SSagi Grimberg 
2906570db170SSagi Grimberg 	return 0;
2907570db170SSagi Grimberg 
2908570db170SSagi Grimberg unmap_prot_cmd:
2909570db170SSagi Grimberg 	if (se_cmd->t_prot_sg)
2910570db170SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2911570db170SSagi Grimberg 
2912570db170SSagi Grimberg 	return ret;
2913570db170SSagi Grimberg }
2914570db170SSagi Grimberg 
2915570db170SSagi Grimberg static int
2916a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
291759464ef4SVu Pham 	       struct isert_rdma_wr *wr)
291859464ef4SVu Pham {
291959464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
292059464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2921e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2922e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2923570db170SSagi Grimberg 	struct ib_send_wr *send_wr;
2924570db170SSagi Grimberg 	struct ib_sge *ib_sg;
2925e3d7e4c3SSagi Grimberg 	u32 offset;
2926e3d7e4c3SSagi Grimberg 	int ret = 0;
292759464ef4SVu Pham 	unsigned long flags;
292859464ef4SVu Pham 
292959464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2930e3d7e4c3SSagi Grimberg 
2931e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2932e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2933e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2934e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2935e3d7e4c3SSagi Grimberg 	if (ret)
2936e3d7e4c3SSagi Grimberg 		return ret;
2937e3d7e4c3SSagi Grimberg 
2938302cc7c3SSagi Grimberg 	if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
2939e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2940e3d7e4c3SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2941e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2942e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2943e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2944e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
294559464ef4SVu Pham 	}
294659464ef4SVu Pham 
29479e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2948570db170SSagi Grimberg 				ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
2949e3d7e4c3SSagi Grimberg 	if (ret)
2950e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
295159464ef4SVu Pham 
2952302cc7c3SSagi Grimberg 	if (isert_prot_cmd(isert_conn, se_cmd)) {
2953570db170SSagi Grimberg 		ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
29549e961ae7SSagi Grimberg 		if (ret)
29559e961ae7SSagi Grimberg 			goto unmap_cmd;
29569e961ae7SSagi Grimberg 
2957570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[SIG];
2958570db170SSagi Grimberg 	} else {
2959570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[DATA];
29609e961ae7SSagi Grimberg 	}
29619e961ae7SSagi Grimberg 
2962570db170SSagi Grimberg 	memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
2963e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
296459464ef4SVu Pham 	wr->send_wr_num = 1;
296559464ef4SVu Pham 	memset(&wr->s_send_wr, 0, sizeof(*send_wr));
296659464ef4SVu Pham 	wr->send_wr = &wr->s_send_wr;
296759464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
296859464ef4SVu Pham 
296959464ef4SVu Pham 	send_wr = &isert_cmd->rdma_wr.s_send_wr;
2970e3d7e4c3SSagi Grimberg 	send_wr->sg_list = &wr->s_ib_sge;
297159464ef4SVu Pham 	send_wr->num_sge = 1;
297259464ef4SVu Pham 	send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
297359464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
297459464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_WRITE;
297559464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
297659464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2977302cc7c3SSagi Grimberg 		send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
29789e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
297959464ef4SVu Pham 	} else {
298059464ef4SVu Pham 		send_wr->opcode = IB_WR_RDMA_READ;
298159464ef4SVu Pham 		send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
298259464ef4SVu Pham 		send_wr->wr.rdma.rkey = isert_cmd->write_stag;
298359464ef4SVu Pham 		send_wr->send_flags = IB_SEND_SIGNALED;
298459464ef4SVu Pham 	}
298559464ef4SVu Pham 
298659464ef4SVu Pham 	return 0;
2987570db170SSagi Grimberg 
2988e3d7e4c3SSagi Grimberg unmap_cmd:
2989e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2990e3d7e4c3SSagi Grimberg 		spin_lock_irqsave(&isert_conn->conn_lock, flags);
2991e3d7e4c3SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2992e3d7e4c3SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2993e3d7e4c3SSagi Grimberg 	}
2994e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
299559464ef4SVu Pham 
299659464ef4SVu Pham 	return ret;
299759464ef4SVu Pham }
299859464ef4SVu Pham 
299959464ef4SVu Pham static int
300090ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
300190ecc6e2SVu Pham {
300290ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
300359464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
300490ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
300590ecc6e2SVu Pham 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3006d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
300790ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
300890ecc6e2SVu Pham 	int rc;
300990ecc6e2SVu Pham 
301090ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
301190ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
301290ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
3013d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
301490ecc6e2SVu Pham 	if (rc) {
301590ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
301690ecc6e2SVu Pham 		return rc;
301790ecc6e2SVu Pham 	}
301890ecc6e2SVu Pham 
3019302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd)) {
3020b8d26b3bSNicholas Bellinger 		/*
3021b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
3022b8d26b3bSNicholas Bellinger 		 */
30239e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
30249e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
302504d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
3026b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
3027b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
302895b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
30290d0f660dSNicholas Bellinger 				   &isert_cmd->tx_desc.send_wr, false);
30309e961ae7SSagi Grimberg 		isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
3031897bb2c9SSagi Grimberg 		wr->send_wr_num += 1;
30329e961ae7SSagi Grimberg 	}
3033b8d26b3bSNicholas Bellinger 
3034897bb2c9SSagi Grimberg 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
3035b8d26b3bSNicholas Bellinger 
3036b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
3037b8d26b3bSNicholas Bellinger 	if (rc) {
3038b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
3039897bb2c9SSagi Grimberg 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
3040b8d26b3bSNicholas Bellinger 	}
30419e961ae7SSagi Grimberg 
3042302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd))
30439e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
30449e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
30459e961ae7SSagi Grimberg 	else
30469e961ae7SSagi Grimberg 		pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
304790ecc6e2SVu Pham 			 isert_cmd);
3048b8d26b3bSNicholas Bellinger 
304990ecc6e2SVu Pham 	return 1;
3050b8d26b3bSNicholas Bellinger }
3051b8d26b3bSNicholas Bellinger 
3052b8d26b3bSNicholas Bellinger static int
3053b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
3054b8d26b3bSNicholas Bellinger {
3055b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
3056d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3057b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
3058b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3059d40945d8SVu Pham 	struct isert_device *device = isert_conn->conn_device;
306090ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
306190ecc6e2SVu Pham 	int rc;
3062b8d26b3bSNicholas Bellinger 
306390ecc6e2SVu Pham 	pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
306490ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
3065b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
3066d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
306790ecc6e2SVu Pham 	if (rc) {
306890ecc6e2SVu Pham 		pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
306990ecc6e2SVu Pham 		return rc;
3070b8d26b3bSNicholas Bellinger 	}
3071b8d26b3bSNicholas Bellinger 
3072b6b87a1dSNicholas Bellinger 	atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
3073b8d26b3bSNicholas Bellinger 
3074b8d26b3bSNicholas Bellinger 	rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
3075b8d26b3bSNicholas Bellinger 	if (rc) {
3076b8d26b3bSNicholas Bellinger 		pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
3077b6b87a1dSNicholas Bellinger 		atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
3078b8d26b3bSNicholas Bellinger 	}
307990ecc6e2SVu Pham 	pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
308090ecc6e2SVu Pham 		 isert_cmd);
3081b8d26b3bSNicholas Bellinger 
308290ecc6e2SVu Pham 	return 0;
3083b8d26b3bSNicholas Bellinger }
3084b8d26b3bSNicholas Bellinger 
3085b8d26b3bSNicholas Bellinger static int
3086b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3087b8d26b3bSNicholas Bellinger {
3088b8d26b3bSNicholas Bellinger 	int ret;
3089b8d26b3bSNicholas Bellinger 
3090b8d26b3bSNicholas Bellinger 	switch (state) {
3091b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3092b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
3093b8d26b3bSNicholas Bellinger 		break;
3094b8d26b3bSNicholas Bellinger 	default:
3095b8d26b3bSNicholas Bellinger 		pr_err("Unknown immediate state: 0x%02x\n", state);
3096b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
3097b8d26b3bSNicholas Bellinger 		break;
3098b8d26b3bSNicholas Bellinger 	}
3099b8d26b3bSNicholas Bellinger 
3100b8d26b3bSNicholas Bellinger 	return ret;
3101b8d26b3bSNicholas Bellinger }
3102b8d26b3bSNicholas Bellinger 
3103b8d26b3bSNicholas Bellinger static int
3104b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3105b8d26b3bSNicholas Bellinger {
3106b8d26b3bSNicholas Bellinger 	int ret;
3107b8d26b3bSNicholas Bellinger 
3108b8d26b3bSNicholas Bellinger 	switch (state) {
3109b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
3110b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
3111b8d26b3bSNicholas Bellinger 		if (!ret) {
3112b8d26b3bSNicholas Bellinger 			pr_debug("Returning iSER Logout -EAGAIN\n");
3113b8d26b3bSNicholas Bellinger 			ret = -EAGAIN;
3114b8d26b3bSNicholas Bellinger 		}
3115b8d26b3bSNicholas Bellinger 		break;
3116b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
3117b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
3118b8d26b3bSNicholas Bellinger 		break;
3119b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
3120b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
3121b8d26b3bSNicholas Bellinger 		break;
3122b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
3123b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
3124b8d26b3bSNicholas Bellinger 		break;
3125adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
3126adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
3127adb54c29SNicholas Bellinger 		break;
3128b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
3129b8d26b3bSNicholas Bellinger 		/*
3130b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
3131b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
3132b8d26b3bSNicholas Bellinger 		 */
3133b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
3134b8d26b3bSNicholas Bellinger 		break;
3135b8d26b3bSNicholas Bellinger 	default:
3136b8d26b3bSNicholas Bellinger 		pr_err("Unknown response state: 0x%02x\n", state);
3137b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
3138b8d26b3bSNicholas Bellinger 		break;
3139b8d26b3bSNicholas Bellinger 	}
3140b8d26b3bSNicholas Bellinger 
3141b8d26b3bSNicholas Bellinger 	return ret;
3142b8d26b3bSNicholas Bellinger }
3143b8d26b3bSNicholas Bellinger 
3144ca6c1d82SSagi Grimberg struct rdma_cm_id *
3145ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
3146ca6c1d82SSagi Grimberg {
3147ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
3148ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
3149ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
3150ca6c1d82SSagi Grimberg 	int ret;
3151ca6c1d82SSagi Grimberg 
3152ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
3153ca6c1d82SSagi Grimberg 	pr_debug("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
3154ca6c1d82SSagi Grimberg 
3155ca6c1d82SSagi Grimberg 	id = rdma_create_id(isert_cma_handler, isert_np,
3156ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
3157ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
3158ca6c1d82SSagi Grimberg 		pr_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
3159ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
3160ca6c1d82SSagi Grimberg 		goto out;
3161ca6c1d82SSagi Grimberg 	}
3162ca6c1d82SSagi Grimberg 	pr_debug("id %p context %p\n", id, id->context);
3163ca6c1d82SSagi Grimberg 
3164ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
3165ca6c1d82SSagi Grimberg 	if (ret) {
3166ca6c1d82SSagi Grimberg 		pr_err("rdma_bind_addr() failed: %d\n", ret);
3167ca6c1d82SSagi Grimberg 		goto out_id;
3168ca6c1d82SSagi Grimberg 	}
3169ca6c1d82SSagi Grimberg 
3170ca6c1d82SSagi Grimberg 	ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
3171ca6c1d82SSagi Grimberg 	if (ret) {
3172ca6c1d82SSagi Grimberg 		pr_err("rdma_listen() failed: %d\n", ret);
3173ca6c1d82SSagi Grimberg 		goto out_id;
3174ca6c1d82SSagi Grimberg 	}
3175ca6c1d82SSagi Grimberg 
3176ca6c1d82SSagi Grimberg 	return id;
3177ca6c1d82SSagi Grimberg out_id:
3178ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
3179ca6c1d82SSagi Grimberg out:
3180ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
3181ca6c1d82SSagi Grimberg }
3182ca6c1d82SSagi Grimberg 
3183b8d26b3bSNicholas Bellinger static int
3184b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
3185b8d26b3bSNicholas Bellinger 	       struct __kernel_sockaddr_storage *ksockaddr)
3186b8d26b3bSNicholas Bellinger {
3187b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
3188b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
3189b8d26b3bSNicholas Bellinger 	int ret;
3190b8d26b3bSNicholas Bellinger 
3191b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3192b8d26b3bSNicholas Bellinger 	if (!isert_np) {
3193b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate struct isert_np\n");
3194b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3195b8d26b3bSNicholas Bellinger 	}
3196531b7bf4SSagi Grimberg 	sema_init(&isert_np->np_sem, 0);
3197b8d26b3bSNicholas Bellinger 	mutex_init(&isert_np->np_accept_mutex);
3198b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&isert_np->np_accept_list);
3199b8d26b3bSNicholas Bellinger 	init_completion(&isert_np->np_login_comp);
3200ca6c1d82SSagi Grimberg 	isert_np->np = np;
3201b8d26b3bSNicholas Bellinger 
3202b8d26b3bSNicholas Bellinger 	/*
3203b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3204b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3205b8d26b3bSNicholas Bellinger 	 */
3206b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
3207b8d26b3bSNicholas Bellinger 	       sizeof(struct __kernel_sockaddr_storage));
3208b8d26b3bSNicholas Bellinger 
3209ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
3210b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3211b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3212b8d26b3bSNicholas Bellinger 		goto out;
3213b8d26b3bSNicholas Bellinger 	}
3214b8d26b3bSNicholas Bellinger 
3215b8d26b3bSNicholas Bellinger 	isert_np->np_cm_id = isert_lid;
3216b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3217b8d26b3bSNicholas Bellinger 
3218b8d26b3bSNicholas Bellinger 	return 0;
3219b8d26b3bSNicholas Bellinger 
3220b8d26b3bSNicholas Bellinger out:
3221b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3222ca6c1d82SSagi Grimberg 
3223b8d26b3bSNicholas Bellinger 	return ret;
3224b8d26b3bSNicholas Bellinger }
3225b8d26b3bSNicholas Bellinger 
3226b8d26b3bSNicholas Bellinger static int
3227b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3228b8d26b3bSNicholas Bellinger {
3229b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3230b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3231b8d26b3bSNicholas Bellinger 	int ret;
3232b8d26b3bSNicholas Bellinger 
3233b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3234b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3235b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3236b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3237b8d26b3bSNicholas Bellinger 
3238b8d26b3bSNicholas Bellinger 	pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3239b8d26b3bSNicholas Bellinger 
3240b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3241b8d26b3bSNicholas Bellinger 	if (ret) {
3242b8d26b3bSNicholas Bellinger 		pr_err("rdma_accept() failed with: %d\n", ret);
3243b8d26b3bSNicholas Bellinger 		return ret;
3244b8d26b3bSNicholas Bellinger 	}
3245b8d26b3bSNicholas Bellinger 
3246b8d26b3bSNicholas Bellinger 	pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3247b8d26b3bSNicholas Bellinger 
3248b8d26b3bSNicholas Bellinger 	return 0;
3249b8d26b3bSNicholas Bellinger }
3250b8d26b3bSNicholas Bellinger 
3251b8d26b3bSNicholas Bellinger static int
3252b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3253b8d26b3bSNicholas Bellinger {
3254b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3255b8d26b3bSNicholas Bellinger 	int ret;
3256b8d26b3bSNicholas Bellinger 
32572371e5daSSagi Grimberg 	pr_info("before login_req comp conn: %p\n", isert_conn);
32582371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
32592371e5daSSagi Grimberg 	if (ret) {
32602371e5daSSagi Grimberg 		pr_err("isert_conn %p interrupted before got login req\n",
32612371e5daSSagi Grimberg 			  isert_conn);
32622371e5daSSagi Grimberg 		return ret;
32632371e5daSSagi Grimberg 	}
32642371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
32652371e5daSSagi Grimberg 
32666faaa85fSNicholas Bellinger 	/*
32676faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
32686faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
32696faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
32706faaa85fSNicholas Bellinger 	 * into a NOP.
32716faaa85fSNicholas Bellinger 	 */
32726faaa85fSNicholas Bellinger 	if (!login->first_request)
32736faaa85fSNicholas Bellinger 		return 0;
3274b8d26b3bSNicholas Bellinger 
32752371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
32762371e5daSSagi Grimberg 
32772371e5daSSagi Grimberg 	pr_info("before conn_login_comp conn: %p\n", conn);
3278b8d26b3bSNicholas Bellinger 	ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3279b8d26b3bSNicholas Bellinger 	if (ret)
3280b8d26b3bSNicholas Bellinger 		return ret;
3281b8d26b3bSNicholas Bellinger 
32822371e5daSSagi Grimberg 	pr_info("processing login->req: %p\n", login->req);
32832371e5daSSagi Grimberg 
3284b8d26b3bSNicholas Bellinger 	return 0;
3285b8d26b3bSNicholas Bellinger }
3286b8d26b3bSNicholas Bellinger 
3287b8d26b3bSNicholas Bellinger static void
3288b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3289b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3290b8d26b3bSNicholas Bellinger {
3291b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3292b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3293b8d26b3bSNicholas Bellinger 	struct sockaddr_in *sock_in;
3294b8d26b3bSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
3295b8d26b3bSNicholas Bellinger 
3296b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3297b8d26b3bSNicholas Bellinger 
3298b8d26b3bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
3299b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3300b8d26b3bSNicholas Bellinger 		snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3301b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3302b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in6->sin6_port);
3303b8d26b3bSNicholas Bellinger 
3304b8d26b3bSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3305b8d26b3bSNicholas Bellinger 		snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3306b8d26b3bSNicholas Bellinger 			 &sock_in6->sin6_addr.in6_u);
3307b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in6->sin6_port);
3308b8d26b3bSNicholas Bellinger 	} else {
3309b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3310b8d26b3bSNicholas Bellinger 		sprintf(conn->login_ip, "%pI4",
3311b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3312b8d26b3bSNicholas Bellinger 		conn->login_port = ntohs(sock_in->sin_port);
3313b8d26b3bSNicholas Bellinger 
3314b8d26b3bSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3315b8d26b3bSNicholas Bellinger 		sprintf(conn->local_ip, "%pI4",
3316b8d26b3bSNicholas Bellinger 			&sock_in->sin_addr.s_addr);
3317b8d26b3bSNicholas Bellinger 		conn->local_port = ntohs(sock_in->sin_port);
3318b8d26b3bSNicholas Bellinger 	}
3319b8d26b3bSNicholas Bellinger }
3320b8d26b3bSNicholas Bellinger 
3321b8d26b3bSNicholas Bellinger static int
3322b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3323b8d26b3bSNicholas Bellinger {
3324b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3325b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3326b8d26b3bSNicholas Bellinger 	int max_accept = 0, ret;
3327b8d26b3bSNicholas Bellinger 
3328b8d26b3bSNicholas Bellinger accept_wait:
3329531b7bf4SSagi Grimberg 	ret = down_interruptible(&isert_np->np_sem);
33301acff63fSSagi Grimberg 	if (ret || max_accept > 5)
3331b8d26b3bSNicholas Bellinger 		return -ENODEV;
3332b8d26b3bSNicholas Bellinger 
3333b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3334e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3335b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
3336e346ab34SSagi Grimberg 		pr_debug("np_thread_state %d for isert_accept_np\n",
3337e346ab34SSagi Grimberg 			 np->np_thread_state);
3338e346ab34SSagi Grimberg 		/**
3339e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
3340e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
3341e346ab34SSagi Grimberg 		 **/
3342b8d26b3bSNicholas Bellinger 		return -ENODEV;
3343b8d26b3bSNicholas Bellinger 	}
3344b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3345b8d26b3bSNicholas Bellinger 
3346b8d26b3bSNicholas Bellinger 	mutex_lock(&isert_np->np_accept_mutex);
3347b8d26b3bSNicholas Bellinger 	if (list_empty(&isert_np->np_accept_list)) {
3348b8d26b3bSNicholas Bellinger 		mutex_unlock(&isert_np->np_accept_mutex);
3349b8d26b3bSNicholas Bellinger 		max_accept++;
3350b8d26b3bSNicholas Bellinger 		goto accept_wait;
3351b8d26b3bSNicholas Bellinger 	}
3352b8d26b3bSNicholas Bellinger 	isert_conn = list_first_entry(&isert_np->np_accept_list,
3353b8d26b3bSNicholas Bellinger 			struct isert_conn, conn_accept_node);
3354b8d26b3bSNicholas Bellinger 	list_del_init(&isert_conn->conn_accept_node);
3355b8d26b3bSNicholas Bellinger 	mutex_unlock(&isert_np->np_accept_mutex);
3356b8d26b3bSNicholas Bellinger 
3357b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3358b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3359b8d26b3bSNicholas Bellinger 	max_accept = 0;
3360b8d26b3bSNicholas Bellinger 
3361b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3362b8d26b3bSNicholas Bellinger 
33632371e5daSSagi Grimberg 	pr_debug("Processing isert_conn: %p\n", isert_conn);
33642371e5daSSagi Grimberg 
3365b8d26b3bSNicholas Bellinger 	return 0;
3366b8d26b3bSNicholas Bellinger }
3367b8d26b3bSNicholas Bellinger 
3368b8d26b3bSNicholas Bellinger static void
3369b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3370b8d26b3bSNicholas Bellinger {
3371b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np = (struct isert_np *)np->np_context;
3372b8d26b3bSNicholas Bellinger 
33733b726ae2SSagi Grimberg 	if (isert_np->np_cm_id)
3374b8d26b3bSNicholas Bellinger 		rdma_destroy_id(isert_np->np_cm_id);
3375b8d26b3bSNicholas Bellinger 
3376b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3377b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3378b8d26b3bSNicholas Bellinger }
3379b8d26b3bSNicholas Bellinger 
3380b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
3381b02efbfcSSagi Grimberg {
3382b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
3383b02efbfcSSagi Grimberg 						     struct isert_conn,
3384b02efbfcSSagi Grimberg 						     release_work);
3385b02efbfcSSagi Grimberg 
3386b02efbfcSSagi Grimberg 	pr_info("Starting release conn %p\n", isert_conn);
3387b02efbfcSSagi Grimberg 
3388b02efbfcSSagi Grimberg 	wait_for_completion(&isert_conn->conn_wait);
3389b02efbfcSSagi Grimberg 
3390b02efbfcSSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3391b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
3392b02efbfcSSagi Grimberg 	mutex_unlock(&isert_conn->conn_mutex);
3393b02efbfcSSagi Grimberg 
3394b02efbfcSSagi Grimberg 	pr_info("Destroying conn %p\n", isert_conn);
3395b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
3396b02efbfcSSagi Grimberg }
3397b02efbfcSSagi Grimberg 
3398defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3399b8d26b3bSNicholas Bellinger {
3400b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3401b8d26b3bSNicholas Bellinger 
3402defd8848SNicholas Bellinger 	pr_debug("isert_wait_conn: Starting \n");
3403b8d26b3bSNicholas Bellinger 
34049d49f5e2SSagi Grimberg 	mutex_lock(&isert_conn->conn_mutex);
3405b8d26b3bSNicholas Bellinger 	/*
3406b8d26b3bSNicholas Bellinger 	 * Only wait for conn_wait_comp_err if the isert_conn made it
3407b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3408b8d26b3bSNicholas Bellinger 	 */
3409b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3410b2cb9649SNicholas Bellinger 		mutex_unlock(&isert_conn->conn_mutex);
3411b2cb9649SNicholas Bellinger 		return;
3412b2cb9649SNicholas Bellinger 	}
3413954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
3414b2cb9649SNicholas Bellinger 	mutex_unlock(&isert_conn->conn_mutex);
3415b2cb9649SNicholas Bellinger 
3416defd8848SNicholas Bellinger 	wait_for_completion(&isert_conn->conn_wait_comp_err);
3417954f2372SSagi Grimberg 
3418b02efbfcSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
3419b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
3420defd8848SNicholas Bellinger }
3421defd8848SNicholas Bellinger 
3422defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3423defd8848SNicholas Bellinger {
3424defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3425b8d26b3bSNicholas Bellinger 
3426b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3427b8d26b3bSNicholas Bellinger }
3428b8d26b3bSNicholas Bellinger 
3429b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3430b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3431b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3432d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3433b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3434b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3435b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3436b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3437defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3438b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3439b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3440b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3441b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3442b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3443b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3444b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3445b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3446131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3447e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3448b8d26b3bSNicholas Bellinger };
3449b8d26b3bSNicholas Bellinger 
3450b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3451b8d26b3bSNicholas Bellinger {
3452b8d26b3bSNicholas Bellinger 	int ret;
3453b8d26b3bSNicholas Bellinger 
3454b8d26b3bSNicholas Bellinger 	isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3455b8d26b3bSNicholas Bellinger 	if (!isert_rx_wq) {
3456b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_rx_wq\n");
3457b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3458b8d26b3bSNicholas Bellinger 	}
3459b8d26b3bSNicholas Bellinger 
3460b8d26b3bSNicholas Bellinger 	isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3461b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
3462b8d26b3bSNicholas Bellinger 		pr_err("Unable to allocate isert_comp_wq\n");
3463b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
3464b8d26b3bSNicholas Bellinger 		goto destroy_rx_wq;
3465b8d26b3bSNicholas Bellinger 	}
3466b8d26b3bSNicholas Bellinger 
3467b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3468b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
3469b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
3470b02efbfcSSagi Grimberg 		pr_err("Unable to allocate isert_release_wq\n");
3471b02efbfcSSagi Grimberg 		ret = -ENOMEM;
3472b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
3473b02efbfcSSagi Grimberg 	}
3474b02efbfcSSagi Grimberg 
3475b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
3476b02efbfcSSagi Grimberg 	pr_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3477b02efbfcSSagi Grimberg 
3478b8d26b3bSNicholas Bellinger 	return 0;
3479b8d26b3bSNicholas Bellinger 
3480b02efbfcSSagi Grimberg destroy_comp_wq:
3481b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
3482b8d26b3bSNicholas Bellinger destroy_rx_wq:
3483b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3484b8d26b3bSNicholas Bellinger 	return ret;
3485b8d26b3bSNicholas Bellinger }
3486b8d26b3bSNicholas Bellinger 
3487b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3488b8d26b3bSNicholas Bellinger {
3489f5ebec96SSagi Grimberg 	flush_scheduled_work();
3490b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
3491b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3492b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_rx_wq);
3493b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
3494b8d26b3bSNicholas Bellinger 	pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3495b8d26b3bSNicholas Bellinger }
3496b8d26b3bSNicholas Bellinger 
3497b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3498b8d26b3bSNicholas Bellinger MODULE_VERSION("0.1");
3499b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3500b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3501b8d26b3bSNicholas Bellinger 
3502b8d26b3bSNicholas Bellinger module_init(isert_init);
3503b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3504