xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision 6d1fba0c2cc7efe42fd761ecbba833ed0ea7b07e)
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>
25b8d26b3bSNicholas Bellinger #include <rdma/ib_verbs.h>
26b8d26b3bSNicholas Bellinger #include <rdma/rdma_cm.h>
27b8d26b3bSNicholas Bellinger #include <target/target_core_base.h>
28b8d26b3bSNicholas Bellinger #include <target/target_core_fabric.h>
29b8d26b3bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
30531b7bf4SSagi Grimberg #include <linux/semaphore.h>
31b8d26b3bSNicholas Bellinger 
32b8d26b3bSNicholas Bellinger #include "ib_isert.h"
33b8d26b3bSNicholas Bellinger 
34b8d26b3bSNicholas Bellinger #define	ISERT_MAX_CONN		8
35b8d26b3bSNicholas Bellinger #define ISER_MAX_RX_CQ_LEN	(ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
36b8d26b3bSNicholas Bellinger #define ISER_MAX_TX_CQ_LEN	(ISERT_QP_MAX_REQ_DTOS  * ISERT_MAX_CONN)
37bdf20e72SSagi Grimberg #define ISER_MAX_CQ_LEN		(ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
38bdf20e72SSagi Grimberg 				 ISERT_MAX_CONN)
39b8d26b3bSNicholas Bellinger 
4045678b6bSSagi Grimberg static int isert_debug_level;
4124f412ddSSagi Grimberg module_param_named(debug_level, isert_debug_level, int, 0644);
4224f412ddSSagi Grimberg MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
4324f412ddSSagi Grimberg 
44b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
45b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
46b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
47b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
48b8d26b3bSNicholas Bellinger 
49b8d26b3bSNicholas Bellinger static void
50d40945d8SVu Pham isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
51d40945d8SVu Pham static int
52d40945d8SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
53d40945d8SVu Pham 	       struct isert_rdma_wr *wr);
5459464ef4SVu Pham static void
55a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
5659464ef4SVu Pham static int
57a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
5859464ef4SVu Pham 	       struct isert_rdma_wr *wr);
59f93f3a70SSagi Grimberg static int
60f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
612371e5daSSagi Grimberg static int
622371e5daSSagi Grimberg isert_rdma_post_recvl(struct isert_conn *isert_conn);
632371e5daSSagi Grimberg static int
642371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
65ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
66d40945d8SVu Pham 
672f1b6b7dSSagi Grimberg static void isert_release_work(struct work_struct *work);
68*6d1fba0cSJenny Derzhavetz static void isert_wait4flush(struct isert_conn *isert_conn);
692f1b6b7dSSagi Grimberg 
70302cc7c3SSagi Grimberg static inline bool
71302cc7c3SSagi Grimberg isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
72302cc7c3SSagi Grimberg {
7323a548eeSSagi Grimberg 	return (conn->pi_support &&
74302cc7c3SSagi Grimberg 		cmd->prot_op != TARGET_PROT_NORMAL);
75302cc7c3SSagi Grimberg }
76302cc7c3SSagi Grimberg 
77302cc7c3SSagi Grimberg 
78d40945d8SVu Pham static void
79b8d26b3bSNicholas Bellinger isert_qp_event_callback(struct ib_event *e, void *context)
80b8d26b3bSNicholas Bellinger {
816700425eSSagi Grimberg 	struct isert_conn *isert_conn = context;
82b8d26b3bSNicholas Bellinger 
83ea8a1616SSagi Grimberg 	isert_err("%s (%d): conn %p\n",
84ea8a1616SSagi Grimberg 		  ib_event_msg(e->event), e->event, isert_conn);
85ea8a1616SSagi Grimberg 
86b8d26b3bSNicholas Bellinger 	switch (e->event) {
87b8d26b3bSNicholas Bellinger 	case IB_EVENT_COMM_EST:
88dac6ab30SSagi Grimberg 		rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
89b8d26b3bSNicholas Bellinger 		break;
90b8d26b3bSNicholas Bellinger 	case IB_EVENT_QP_LAST_WQE_REACHED:
914c22e07fSSagi Grimberg 		isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
92b8d26b3bSNicholas Bellinger 		break;
93b8d26b3bSNicholas Bellinger 	default:
94b8d26b3bSNicholas Bellinger 		break;
95b8d26b3bSNicholas Bellinger 	}
96b8d26b3bSNicholas Bellinger }
97b8d26b3bSNicholas Bellinger 
9840fc069aSSagi Grimberg static struct isert_comp *
9940fc069aSSagi Grimberg isert_comp_get(struct isert_conn *isert_conn)
100b8d26b3bSNicholas Bellinger {
101dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1024a295baeSSagi Grimberg 	struct isert_comp *comp;
10340fc069aSSagi Grimberg 	int i, min = 0;
104b8d26b3bSNicholas Bellinger 
105b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
1064a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++)
1074a295baeSSagi Grimberg 		if (device->comps[i].active_qps <
1084a295baeSSagi Grimberg 		    device->comps[min].active_qps)
1094a295baeSSagi Grimberg 			min = i;
1104a295baeSSagi Grimberg 	comp = &device->comps[min];
1114a295baeSSagi Grimberg 	comp->active_qps++;
11240fc069aSSagi Grimberg 	mutex_unlock(&device_list_mutex);
11340fc069aSSagi Grimberg 
11424f412ddSSagi Grimberg 	isert_info("conn %p, using comp %p min_index: %d\n",
1154a295baeSSagi Grimberg 		   isert_conn, comp, min);
11640fc069aSSagi Grimberg 
11740fc069aSSagi Grimberg 	return comp;
11840fc069aSSagi Grimberg }
11940fc069aSSagi Grimberg 
12040fc069aSSagi Grimberg static void
12140fc069aSSagi Grimberg isert_comp_put(struct isert_comp *comp)
12240fc069aSSagi Grimberg {
12340fc069aSSagi Grimberg 	mutex_lock(&device_list_mutex);
12440fc069aSSagi Grimberg 	comp->active_qps--;
125b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
12640fc069aSSagi Grimberg }
12740fc069aSSagi Grimberg 
12840fc069aSSagi Grimberg static struct ib_qp *
12940fc069aSSagi Grimberg isert_create_qp(struct isert_conn *isert_conn,
13040fc069aSSagi Grimberg 		struct isert_comp *comp,
13140fc069aSSagi Grimberg 		struct rdma_cm_id *cma_id)
13240fc069aSSagi Grimberg {
133dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
13440fc069aSSagi Grimberg 	struct ib_qp_init_attr attr;
13540fc069aSSagi Grimberg 	int ret;
136b8d26b3bSNicholas Bellinger 
137b8d26b3bSNicholas Bellinger 	memset(&attr, 0, sizeof(struct ib_qp_init_attr));
138b8d26b3bSNicholas Bellinger 	attr.event_handler = isert_qp_event_callback;
139b8d26b3bSNicholas Bellinger 	attr.qp_context = isert_conn;
1406f0fae3dSSagi Grimberg 	attr.send_cq = comp->cq;
1416f0fae3dSSagi Grimberg 	attr.recv_cq = comp->cq;
142b8d26b3bSNicholas Bellinger 	attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
143bdf20e72SSagi Grimberg 	attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
1444a061b28SOr Gerlitz 	attr.cap.max_send_sge = device->ib_device->attrs.max_sge;
1454a061b28SOr Gerlitz 	isert_conn->max_sge = min(device->ib_device->attrs.max_sge,
1464a061b28SOr Gerlitz 				  device->ib_device->attrs.max_sge_rd);
147b8d26b3bSNicholas Bellinger 	attr.cap.max_recv_sge = 1;
148b8d26b3bSNicholas Bellinger 	attr.sq_sig_type = IB_SIGNAL_REQ_WR;
149b8d26b3bSNicholas Bellinger 	attr.qp_type = IB_QPT_RC;
150570db170SSagi Grimberg 	if (device->pi_capable)
151d3e125daSSagi Grimberg 		attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
152b8d26b3bSNicholas Bellinger 
15367cb3949SSagi Grimberg 	ret = rdma_create_qp(cma_id, device->pd, &attr);
154b8d26b3bSNicholas Bellinger 	if (ret) {
15524f412ddSSagi Grimberg 		isert_err("rdma_create_qp failed for cma_id %d\n", ret);
15640fc069aSSagi Grimberg 		return ERR_PTR(ret);
15740fc069aSSagi Grimberg 	}
15840fc069aSSagi Grimberg 
15940fc069aSSagi Grimberg 	return cma_id->qp;
16040fc069aSSagi Grimberg }
16140fc069aSSagi Grimberg 
16240fc069aSSagi Grimberg static int
16340fc069aSSagi Grimberg isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
16440fc069aSSagi Grimberg {
16540fc069aSSagi Grimberg 	struct isert_comp *comp;
16640fc069aSSagi Grimberg 	int ret;
16740fc069aSSagi Grimberg 
16840fc069aSSagi Grimberg 	comp = isert_comp_get(isert_conn);
169dac6ab30SSagi Grimberg 	isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
170dac6ab30SSagi Grimberg 	if (IS_ERR(isert_conn->qp)) {
171dac6ab30SSagi Grimberg 		ret = PTR_ERR(isert_conn->qp);
17219e2090fSSagi Grimberg 		goto err;
173b8d26b3bSNicholas Bellinger 	}
174b8d26b3bSNicholas Bellinger 
175b8d26b3bSNicholas Bellinger 	return 0;
17619e2090fSSagi Grimberg err:
17740fc069aSSagi Grimberg 	isert_comp_put(comp);
17819e2090fSSagi Grimberg 	return ret;
179b8d26b3bSNicholas Bellinger }
180b8d26b3bSNicholas Bellinger 
181b8d26b3bSNicholas Bellinger static void
182b8d26b3bSNicholas Bellinger isert_cq_event_callback(struct ib_event *e, void *context)
183b8d26b3bSNicholas Bellinger {
1844c22e07fSSagi Grimberg 	isert_dbg("event: %d\n", e->event);
185b8d26b3bSNicholas Bellinger }
186b8d26b3bSNicholas Bellinger 
187b8d26b3bSNicholas Bellinger static int
188b8d26b3bSNicholas Bellinger isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
189b8d26b3bSNicholas Bellinger {
190dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
19167cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
192b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
193b8d26b3bSNicholas Bellinger 	struct ib_sge *rx_sg;
194b8d26b3bSNicholas Bellinger 	u64 dma_addr;
195b8d26b3bSNicholas Bellinger 	int i, j;
196b8d26b3bSNicholas Bellinger 
197dac6ab30SSagi Grimberg 	isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
198b8d26b3bSNicholas Bellinger 				sizeof(struct iser_rx_desc), GFP_KERNEL);
199dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
200b8d26b3bSNicholas Bellinger 		goto fail;
201b8d26b3bSNicholas Bellinger 
202dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
203b8d26b3bSNicholas Bellinger 
204b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
205b8d26b3bSNicholas Bellinger 		dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
206b8d26b3bSNicholas Bellinger 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
207b8d26b3bSNicholas Bellinger 		if (ib_dma_mapping_error(ib_dev, dma_addr))
208b8d26b3bSNicholas Bellinger 			goto dma_map_fail;
209b8d26b3bSNicholas Bellinger 
210b8d26b3bSNicholas Bellinger 		rx_desc->dma_addr = dma_addr;
211b8d26b3bSNicholas Bellinger 
212b8d26b3bSNicholas Bellinger 		rx_sg = &rx_desc->rx_sg;
213b8d26b3bSNicholas Bellinger 		rx_sg->addr = rx_desc->dma_addr;
214b8d26b3bSNicholas Bellinger 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
21534efc7dfSJason Gunthorpe 		rx_sg->lkey = device->pd->local_dma_lkey;
216b8d26b3bSNicholas Bellinger 	}
217b8d26b3bSNicholas Bellinger 
218b8d26b3bSNicholas Bellinger 	return 0;
219b8d26b3bSNicholas Bellinger 
220b8d26b3bSNicholas Bellinger dma_map_fail:
221dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
222b8d26b3bSNicholas Bellinger 	for (j = 0; j < i; j++, rx_desc++) {
223b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
224b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
225b8d26b3bSNicholas Bellinger 	}
226dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
227dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
228b8d26b3bSNicholas Bellinger fail:
2294c22e07fSSagi Grimberg 	isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
2304c22e07fSSagi Grimberg 
231b8d26b3bSNicholas Bellinger 	return -ENOMEM;
232b8d26b3bSNicholas Bellinger }
233b8d26b3bSNicholas Bellinger 
234b8d26b3bSNicholas Bellinger static void
235b8d26b3bSNicholas Bellinger isert_free_rx_descriptors(struct isert_conn *isert_conn)
236b8d26b3bSNicholas Bellinger {
237dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
238b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
239b8d26b3bSNicholas Bellinger 	int i;
240b8d26b3bSNicholas Bellinger 
241dac6ab30SSagi Grimberg 	if (!isert_conn->rx_descs)
242b8d26b3bSNicholas Bellinger 		return;
243b8d26b3bSNicholas Bellinger 
244dac6ab30SSagi Grimberg 	rx_desc = isert_conn->rx_descs;
245b8d26b3bSNicholas Bellinger 	for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++)  {
246b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
247b8d26b3bSNicholas Bellinger 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
248b8d26b3bSNicholas Bellinger 	}
249b8d26b3bSNicholas Bellinger 
250dac6ab30SSagi Grimberg 	kfree(isert_conn->rx_descs);
251dac6ab30SSagi Grimberg 	isert_conn->rx_descs = NULL;
252b8d26b3bSNicholas Bellinger }
253b8d26b3bSNicholas Bellinger 
2546f0fae3dSSagi Grimberg static void isert_cq_work(struct work_struct *);
2556f0fae3dSSagi Grimberg static void isert_cq_callback(struct ib_cq *, void *);
256b8d26b3bSNicholas Bellinger 
257172369c5SSagi Grimberg static void
258172369c5SSagi Grimberg isert_free_comps(struct isert_device *device)
259b8d26b3bSNicholas Bellinger {
260172369c5SSagi Grimberg 	int i;
261b8d26b3bSNicholas Bellinger 
262172369c5SSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
263172369c5SSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
26459464ef4SVu Pham 
265172369c5SSagi Grimberg 		if (comp->cq) {
266172369c5SSagi Grimberg 			cancel_work_sync(&comp->work);
267172369c5SSagi Grimberg 			ib_destroy_cq(comp->cq);
268172369c5SSagi Grimberg 		}
269172369c5SSagi Grimberg 	}
270172369c5SSagi Grimberg 	kfree(device->comps);
27159464ef4SVu Pham }
272d40945d8SVu Pham 
273172369c5SSagi Grimberg static int
2744a061b28SOr Gerlitz isert_alloc_comps(struct isert_device *device)
275172369c5SSagi Grimberg {
276172369c5SSagi Grimberg 	int i, max_cqe, ret = 0;
277d3e125daSSagi Grimberg 
2784a295baeSSagi Grimberg 	device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
2794a295baeSSagi Grimberg 				 device->ib_device->num_comp_vectors));
280172369c5SSagi Grimberg 
28124f412ddSSagi Grimberg 	isert_info("Using %d CQs, %s supports %d vectors support "
282d3e125daSSagi Grimberg 		   "Fast registration %d pi_capable %d\n",
2834a295baeSSagi Grimberg 		   device->comps_used, device->ib_device->name,
284d3e125daSSagi Grimberg 		   device->ib_device->num_comp_vectors, device->use_fastreg,
285d3e125daSSagi Grimberg 		   device->pi_capable);
2864a295baeSSagi Grimberg 
2874a295baeSSagi Grimberg 	device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
2884a295baeSSagi Grimberg 				GFP_KERNEL);
2894a295baeSSagi Grimberg 	if (!device->comps) {
29024f412ddSSagi Grimberg 		isert_err("Unable to allocate completion contexts\n");
291b8d26b3bSNicholas Bellinger 		return -ENOMEM;
292b8d26b3bSNicholas Bellinger 	}
293b8d26b3bSNicholas Bellinger 
2944a061b28SOr Gerlitz 	max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
295172369c5SSagi Grimberg 
2964a295baeSSagi Grimberg 	for (i = 0; i < device->comps_used; i++) {
2978e37210bSMatan Barak 		struct ib_cq_init_attr cq_attr = {};
2984a295baeSSagi Grimberg 		struct isert_comp *comp = &device->comps[i];
299b8d26b3bSNicholas Bellinger 
3004a295baeSSagi Grimberg 		comp->device = device;
3016f0fae3dSSagi Grimberg 		INIT_WORK(&comp->work, isert_cq_work);
3028e37210bSMatan Barak 		cq_attr.cqe = max_cqe;
3038e37210bSMatan Barak 		cq_attr.comp_vector = i;
3046f0fae3dSSagi Grimberg 		comp->cq = ib_create_cq(device->ib_device,
3056f0fae3dSSagi Grimberg 					isert_cq_callback,
306b8d26b3bSNicholas Bellinger 					isert_cq_event_callback,
3074a295baeSSagi Grimberg 					(void *)comp,
3088e37210bSMatan Barak 					&cq_attr);
3096f0fae3dSSagi Grimberg 		if (IS_ERR(comp->cq)) {
310172369c5SSagi Grimberg 			isert_err("Unable to allocate cq\n");
3116f0fae3dSSagi Grimberg 			ret = PTR_ERR(comp->cq);
3126f0fae3dSSagi Grimberg 			comp->cq = NULL;
313b8d26b3bSNicholas Bellinger 			goto out_cq;
31494a71110SWei Yongjun 		}
315b8d26b3bSNicholas Bellinger 
3166f0fae3dSSagi Grimberg 		ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
31794a71110SWei Yongjun 		if (ret)
318b8d26b3bSNicholas Bellinger 			goto out_cq;
319b8d26b3bSNicholas Bellinger 	}
320b8d26b3bSNicholas Bellinger 
321172369c5SSagi Grimberg 	return 0;
322172369c5SSagi Grimberg out_cq:
323172369c5SSagi Grimberg 	isert_free_comps(device);
324172369c5SSagi Grimberg 	return ret;
325172369c5SSagi Grimberg }
326172369c5SSagi Grimberg 
327172369c5SSagi Grimberg static int
328172369c5SSagi Grimberg isert_create_device_ib_res(struct isert_device *device)
329172369c5SSagi Grimberg {
3304a061b28SOr Gerlitz 	struct ib_device *ib_dev = device->ib_device;
331fd8205e8SSagi Grimberg 	int ret;
332172369c5SSagi Grimberg 
3334a061b28SOr Gerlitz 	isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
3344a061b28SOr Gerlitz 	isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
335172369c5SSagi Grimberg 
336172369c5SSagi Grimberg 	/* asign function handlers */
3374a061b28SOr Gerlitz 	if (ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
3384a061b28SOr Gerlitz 	    ib_dev->attrs.device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
339172369c5SSagi Grimberg 		device->use_fastreg = 1;
340172369c5SSagi Grimberg 		device->reg_rdma_mem = isert_reg_rdma;
341172369c5SSagi Grimberg 		device->unreg_rdma_mem = isert_unreg_rdma;
342172369c5SSagi Grimberg 	} else {
343172369c5SSagi Grimberg 		device->use_fastreg = 0;
344172369c5SSagi Grimberg 		device->reg_rdma_mem = isert_map_rdma;
345172369c5SSagi Grimberg 		device->unreg_rdma_mem = isert_unmap_cmd;
346172369c5SSagi Grimberg 	}
347172369c5SSagi Grimberg 
3484a061b28SOr Gerlitz 	ret = isert_alloc_comps(device);
349172369c5SSagi Grimberg 	if (ret)
350373a4cd7SNicholas Bellinger 		goto out;
351172369c5SSagi Grimberg 
3524a061b28SOr Gerlitz 	device->pd = ib_alloc_pd(ib_dev);
35367cb3949SSagi Grimberg 	if (IS_ERR(device->pd)) {
35467cb3949SSagi Grimberg 		ret = PTR_ERR(device->pd);
35567cb3949SSagi Grimberg 		isert_err("failed to allocate pd, device %p, ret=%d\n",
35667cb3949SSagi Grimberg 			  device, ret);
35767cb3949SSagi Grimberg 		goto out_cq;
35867cb3949SSagi Grimberg 	}
35967cb3949SSagi Grimberg 
360172369c5SSagi Grimberg 	/* Check signature cap */
3614a061b28SOr Gerlitz 	device->pi_capable = ib_dev->attrs.device_cap_flags &
362172369c5SSagi Grimberg 			     IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
36367cb3949SSagi Grimberg 
364b8d26b3bSNicholas Bellinger 	return 0;
365b8d26b3bSNicholas Bellinger 
366b8d26b3bSNicholas Bellinger out_cq:
367172369c5SSagi Grimberg 	isert_free_comps(device);
368373a4cd7SNicholas Bellinger out:
369373a4cd7SNicholas Bellinger 	if (ret > 0)
370373a4cd7SNicholas Bellinger 		ret = -EINVAL;
371b8d26b3bSNicholas Bellinger 	return ret;
372b8d26b3bSNicholas Bellinger }
373b8d26b3bSNicholas Bellinger 
374b8d26b3bSNicholas Bellinger static void
375b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
376b8d26b3bSNicholas Bellinger {
37724f412ddSSagi Grimberg 	isert_info("device %p\n", device);
378b8d26b3bSNicholas Bellinger 
37967cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
380172369c5SSagi Grimberg 	isert_free_comps(device);
381b8d26b3bSNicholas Bellinger }
382b8d26b3bSNicholas Bellinger 
383b8d26b3bSNicholas Bellinger static void
384cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device)
385b8d26b3bSNicholas Bellinger {
386b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
387b8d26b3bSNicholas Bellinger 	device->refcount--;
3884c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
389b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
390b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
391b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
392b8d26b3bSNicholas Bellinger 		kfree(device);
393b8d26b3bSNicholas Bellinger 	}
394b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
395b8d26b3bSNicholas Bellinger }
396b8d26b3bSNicholas Bellinger 
397b8d26b3bSNicholas Bellinger static struct isert_device *
398cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id)
399b8d26b3bSNicholas Bellinger {
400b8d26b3bSNicholas Bellinger 	struct isert_device *device;
401b8d26b3bSNicholas Bellinger 	int ret;
402b8d26b3bSNicholas Bellinger 
403b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
404b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
405b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
406b8d26b3bSNicholas Bellinger 			device->refcount++;
4074c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
4084c22e07fSSagi Grimberg 				   device, device->refcount);
409b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
410b8d26b3bSNicholas Bellinger 			return device;
411b8d26b3bSNicholas Bellinger 		}
412b8d26b3bSNicholas Bellinger 	}
413b8d26b3bSNicholas Bellinger 
414b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
415b8d26b3bSNicholas Bellinger 	if (!device) {
416b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
417b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
418b8d26b3bSNicholas Bellinger 	}
419b8d26b3bSNicholas Bellinger 
420b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
421b8d26b3bSNicholas Bellinger 
422b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
423b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
424b8d26b3bSNicholas Bellinger 	if (ret) {
425b8d26b3bSNicholas Bellinger 		kfree(device);
426b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
427b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
428b8d26b3bSNicholas Bellinger 	}
429b8d26b3bSNicholas Bellinger 
430b8d26b3bSNicholas Bellinger 	device->refcount++;
431b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
4324c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
4334c22e07fSSagi Grimberg 		   device, device->refcount);
434b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
435b8d26b3bSNicholas Bellinger 
436b8d26b3bSNicholas Bellinger 	return device;
437b8d26b3bSNicholas Bellinger }
438b8d26b3bSNicholas Bellinger 
43959464ef4SVu Pham static void
440a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
44159464ef4SVu Pham {
44259464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
44359464ef4SVu Pham 	int i = 0;
44459464ef4SVu Pham 
445dac6ab30SSagi Grimberg 	if (list_empty(&isert_conn->fr_pool))
44659464ef4SVu Pham 		return;
44759464ef4SVu Pham 
4484c22e07fSSagi Grimberg 	isert_info("Freeing conn %p fastreg pool", isert_conn);
44959464ef4SVu Pham 
45059464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
451dac6ab30SSagi Grimberg 				 &isert_conn->fr_pool, list) {
45259464ef4SVu Pham 		list_del(&fr_desc->list);
45359464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
454d3e125daSSagi Grimberg 		if (fr_desc->pi_ctx) {
455d3e125daSSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
4568b91ffc1SSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
457d3e125daSSagi Grimberg 			kfree(fr_desc->pi_ctx);
458d3e125daSSagi Grimberg 		}
45959464ef4SVu Pham 		kfree(fr_desc);
46059464ef4SVu Pham 		++i;
46159464ef4SVu Pham 	}
46259464ef4SVu Pham 
463dac6ab30SSagi Grimberg 	if (i < isert_conn->fr_pool_size)
46424f412ddSSagi Grimberg 		isert_warn("Pool still has %d regions registered\n",
465dac6ab30SSagi Grimberg 			isert_conn->fr_pool_size - i);
46659464ef4SVu Pham }
46759464ef4SVu Pham 
46859464ef4SVu Pham static int
469570db170SSagi Grimberg isert_create_pi_ctx(struct fast_reg_descriptor *desc,
470570db170SSagi Grimberg 		    struct ib_device *device,
471570db170SSagi Grimberg 		    struct ib_pd *pd)
472570db170SSagi Grimberg {
473570db170SSagi Grimberg 	struct pi_context *pi_ctx;
474570db170SSagi Grimberg 	int ret;
475570db170SSagi Grimberg 
476570db170SSagi Grimberg 	pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
477570db170SSagi Grimberg 	if (!pi_ctx) {
47824f412ddSSagi Grimberg 		isert_err("Failed to allocate pi context\n");
479570db170SSagi Grimberg 		return -ENOMEM;
480570db170SSagi Grimberg 	}
481570db170SSagi Grimberg 
482a89be2ccSSagi Grimberg 	pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
483a89be2ccSSagi Grimberg 				      ISCSI_ISER_SG_TABLESIZE);
484570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_mr)) {
48524f412ddSSagi Grimberg 		isert_err("Failed to allocate prot frmr err=%ld\n",
486570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_mr));
487570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_mr);
48816c2d702SSagi Grimberg 		goto err_pi_ctx;
489570db170SSagi Grimberg 	}
490570db170SSagi Grimberg 	desc->ind |= ISERT_PROT_KEY_VALID;
491570db170SSagi Grimberg 
4929bee178bSSagi Grimberg 	pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
493570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->sig_mr)) {
49424f412ddSSagi Grimberg 		isert_err("Failed to allocate signature enabled mr err=%ld\n",
495570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->sig_mr));
496570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->sig_mr);
497570db170SSagi Grimberg 		goto err_prot_mr;
498570db170SSagi Grimberg 	}
499570db170SSagi Grimberg 
500570db170SSagi Grimberg 	desc->pi_ctx = pi_ctx;
501570db170SSagi Grimberg 	desc->ind |= ISERT_SIG_KEY_VALID;
502570db170SSagi Grimberg 	desc->ind &= ~ISERT_PROTECTED;
503570db170SSagi Grimberg 
504570db170SSagi Grimberg 	return 0;
505570db170SSagi Grimberg 
506570db170SSagi Grimberg err_prot_mr:
507b2feda4fSRoland Dreier 	ib_dereg_mr(pi_ctx->prot_mr);
508570db170SSagi Grimberg err_pi_ctx:
509b2feda4fSRoland Dreier 	kfree(pi_ctx);
510570db170SSagi Grimberg 
511570db170SSagi Grimberg 	return ret;
512570db170SSagi Grimberg }
513570db170SSagi Grimberg 
514570db170SSagi Grimberg static int
515dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
516570db170SSagi Grimberg 		     struct fast_reg_descriptor *fr_desc)
517dc87a90fSSagi Grimberg {
518a89be2ccSSagi Grimberg 	fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
519a89be2ccSSagi Grimberg 				       ISCSI_ISER_SG_TABLESIZE);
520dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
52124f412ddSSagi Grimberg 		isert_err("Failed to allocate data frmr err=%ld\n",
522dc87a90fSSagi Grimberg 			  PTR_ERR(fr_desc->data_mr));
52316c2d702SSagi Grimberg 		return PTR_ERR(fr_desc->data_mr);
524dc87a90fSSagi Grimberg 	}
525d3e125daSSagi Grimberg 	fr_desc->ind |= ISERT_DATA_KEY_VALID;
526dc87a90fSSagi Grimberg 
52724f412ddSSagi Grimberg 	isert_dbg("Created fr_desc %p\n", fr_desc);
528dc87a90fSSagi Grimberg 
529dc87a90fSSagi Grimberg 	return 0;
530dc87a90fSSagi Grimberg }
531dc87a90fSSagi Grimberg 
532dc87a90fSSagi Grimberg static int
533570db170SSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
53459464ef4SVu Pham {
53559464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
536dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
537f46d6a8aSNicholas Bellinger 	struct se_session *se_sess = isert_conn->conn->sess->se_sess;
538f46d6a8aSNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
539f46d6a8aSNicholas Bellinger 	int i, ret, tag_num;
540f46d6a8aSNicholas Bellinger 	/*
541f46d6a8aSNicholas Bellinger 	 * Setup the number of FRMRs based upon the number of tags
542f46d6a8aSNicholas Bellinger 	 * available to session in iscsi_target_locate_portal().
543f46d6a8aSNicholas Bellinger 	 */
544f46d6a8aSNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
545f46d6a8aSNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
54659464ef4SVu Pham 
547dac6ab30SSagi Grimberg 	isert_conn->fr_pool_size = 0;
548f46d6a8aSNicholas Bellinger 	for (i = 0; i < tag_num; i++) {
54959464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
55059464ef4SVu Pham 		if (!fr_desc) {
55124f412ddSSagi Grimberg 			isert_err("Failed to allocate fast_reg descriptor\n");
55259464ef4SVu Pham 			ret = -ENOMEM;
55359464ef4SVu Pham 			goto err;
55459464ef4SVu Pham 		}
55559464ef4SVu Pham 
556dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
55767cb3949SSagi Grimberg 					   device->pd, fr_desc);
558dc87a90fSSagi Grimberg 		if (ret) {
55924f412ddSSagi Grimberg 			isert_err("Failed to create fastreg descriptor err=%d\n",
560dc87a90fSSagi Grimberg 			       ret);
561a80e21b3SNicholas Bellinger 			kfree(fr_desc);
56259464ef4SVu Pham 			goto err;
56359464ef4SVu Pham 		}
56459464ef4SVu Pham 
565dac6ab30SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
566dac6ab30SSagi Grimberg 		isert_conn->fr_pool_size++;
56759464ef4SVu Pham 	}
56859464ef4SVu Pham 
56924f412ddSSagi Grimberg 	isert_dbg("Creating conn %p fastreg pool size=%d",
570dac6ab30SSagi Grimberg 		 isert_conn, isert_conn->fr_pool_size);
57159464ef4SVu Pham 
57259464ef4SVu Pham 	return 0;
57359464ef4SVu Pham 
57459464ef4SVu Pham err:
575a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
57659464ef4SVu Pham 	return ret;
57759464ef4SVu Pham }
57859464ef4SVu Pham 
579ae9ea9edSSagi Grimberg static void
580ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn)
581ae9ea9edSSagi Grimberg {
582ae9ea9edSSagi Grimberg 	isert_conn->state = ISER_CONN_INIT;
583bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_conn->node);
584dac6ab30SSagi Grimberg 	init_completion(&isert_conn->login_comp);
585ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
586dac6ab30SSagi Grimberg 	init_completion(&isert_conn->wait);
587dac6ab30SSagi Grimberg 	kref_init(&isert_conn->kref);
588dac6ab30SSagi Grimberg 	mutex_init(&isert_conn->mutex);
589dac6ab30SSagi Grimberg 	spin_lock_init(&isert_conn->pool_lock);
590dac6ab30SSagi Grimberg 	INIT_LIST_HEAD(&isert_conn->fr_pool);
5912f1b6b7dSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
592ae9ea9edSSagi Grimberg }
593ae9ea9edSSagi Grimberg 
594ae9ea9edSSagi Grimberg static void
595ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn)
596ae9ea9edSSagi Grimberg {
597dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
598ae9ea9edSSagi Grimberg 
599ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
600ae9ea9edSSagi Grimberg 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
601ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
602ae9ea9edSSagi Grimberg 			    ISCSI_DEF_MAX_RECV_SEG_LEN,
603ae9ea9edSSagi Grimberg 			    DMA_FROM_DEVICE);
604ae9ea9edSSagi Grimberg 	kfree(isert_conn->login_buf);
605ae9ea9edSSagi Grimberg }
606ae9ea9edSSagi Grimberg 
607ae9ea9edSSagi Grimberg static int
608ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn,
609ae9ea9edSSagi Grimberg 		      struct ib_device *ib_dev)
610ae9ea9edSSagi Grimberg {
611ae9ea9edSSagi Grimberg 	int ret;
612ae9ea9edSSagi Grimberg 
613ae9ea9edSSagi Grimberg 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
614ae9ea9edSSagi Grimberg 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
615ae9ea9edSSagi Grimberg 	if (!isert_conn->login_buf) {
616ae9ea9edSSagi Grimberg 		isert_err("Unable to allocate isert_conn->login_buf\n");
617ae9ea9edSSagi Grimberg 		return -ENOMEM;
618ae9ea9edSSagi Grimberg 	}
619ae9ea9edSSagi Grimberg 
620ae9ea9edSSagi Grimberg 	isert_conn->login_req_buf = isert_conn->login_buf;
621ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_buf = isert_conn->login_buf +
622ae9ea9edSSagi Grimberg 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
623ae9ea9edSSagi Grimberg 
624ae9ea9edSSagi Grimberg 	isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
625ae9ea9edSSagi Grimberg 		 isert_conn->login_buf, isert_conn->login_req_buf,
626ae9ea9edSSagi Grimberg 		 isert_conn->login_rsp_buf);
627ae9ea9edSSagi Grimberg 
628ae9ea9edSSagi Grimberg 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
629ae9ea9edSSagi Grimberg 				(void *)isert_conn->login_req_buf,
630ae9ea9edSSagi Grimberg 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
631ae9ea9edSSagi Grimberg 
632ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
633ae9ea9edSSagi Grimberg 	if (ret) {
634ae9ea9edSSagi Grimberg 		isert_err("login_req_dma mapping error: %d\n", ret);
635ae9ea9edSSagi Grimberg 		isert_conn->login_req_dma = 0;
636ae9ea9edSSagi Grimberg 		goto out_login_buf;
637ae9ea9edSSagi Grimberg 	}
638ae9ea9edSSagi Grimberg 
639ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
640ae9ea9edSSagi Grimberg 					(void *)isert_conn->login_rsp_buf,
641ae9ea9edSSagi Grimberg 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
642ae9ea9edSSagi Grimberg 
643ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
644ae9ea9edSSagi Grimberg 	if (ret) {
645ae9ea9edSSagi Grimberg 		isert_err("login_rsp_dma mapping error: %d\n", ret);
646ae9ea9edSSagi Grimberg 		isert_conn->login_rsp_dma = 0;
647ae9ea9edSSagi Grimberg 		goto out_req_dma_map;
648ae9ea9edSSagi Grimberg 	}
649ae9ea9edSSagi Grimberg 
650ae9ea9edSSagi Grimberg 	return 0;
651ae9ea9edSSagi Grimberg 
652ae9ea9edSSagi Grimberg out_req_dma_map:
653ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
654ae9ea9edSSagi Grimberg 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
655ae9ea9edSSagi Grimberg out_login_buf:
656ae9ea9edSSagi Grimberg 	kfree(isert_conn->login_buf);
657ae9ea9edSSagi Grimberg 	return ret;
658ae9ea9edSSagi Grimberg }
659ae9ea9edSSagi Grimberg 
660422bd0acSJenny Derzhavetz static void
661422bd0acSJenny Derzhavetz isert_set_nego_params(struct isert_conn *isert_conn,
662422bd0acSJenny Derzhavetz 		      struct rdma_conn_param *param)
663422bd0acSJenny Derzhavetz {
664422bd0acSJenny Derzhavetz 	struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
665422bd0acSJenny Derzhavetz 
666422bd0acSJenny Derzhavetz 	/* Set max inflight RDMA READ requests */
667422bd0acSJenny Derzhavetz 	isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
668422bd0acSJenny Derzhavetz 				attr->max_qp_init_rd_atom);
669422bd0acSJenny Derzhavetz 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
670422bd0acSJenny Derzhavetz 
671422bd0acSJenny Derzhavetz 	if (param->private_data) {
672422bd0acSJenny Derzhavetz 		u8 flags = *(u8 *)param->private_data;
673422bd0acSJenny Derzhavetz 
674422bd0acSJenny Derzhavetz 		/*
675422bd0acSJenny Derzhavetz 		 * use remote invalidation if the both initiator
676422bd0acSJenny Derzhavetz 		 * and the HCA support it
677422bd0acSJenny Derzhavetz 		 */
678422bd0acSJenny Derzhavetz 		isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
679422bd0acSJenny Derzhavetz 					  (attr->device_cap_flags &
680422bd0acSJenny Derzhavetz 					   IB_DEVICE_MEM_MGT_EXTENSIONS);
681422bd0acSJenny Derzhavetz 		if (isert_conn->snd_w_inv)
682422bd0acSJenny Derzhavetz 			isert_info("Using remote invalidation\n");
683422bd0acSJenny Derzhavetz 	}
684422bd0acSJenny Derzhavetz }
685422bd0acSJenny Derzhavetz 
686b8d26b3bSNicholas Bellinger static int
687b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
688b8d26b3bSNicholas Bellinger {
689ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
690ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
691b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
692b8d26b3bSNicholas Bellinger 	struct isert_device *device;
693b8d26b3bSNicholas Bellinger 	int ret = 0;
69414f4b54fSSagi Grimberg 
69514f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
69614f4b54fSSagi Grimberg 	if (!np->enabled) {
69714f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
69824f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
69914f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
70014f4b54fSSagi Grimberg 	}
70114f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
702b8d26b3bSNicholas Bellinger 
7034c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
704b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
705b8d26b3bSNicholas Bellinger 
706b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
707ae9ea9edSSagi Grimberg 	if (!isert_conn)
708b8d26b3bSNicholas Bellinger 		return -ENOMEM;
709b8d26b3bSNicholas Bellinger 
710ae9ea9edSSagi Grimberg 	isert_init_conn(isert_conn);
711dac6ab30SSagi Grimberg 	isert_conn->cm_id = cma_id;
712b8d26b3bSNicholas Bellinger 
713ae9ea9edSSagi Grimberg 	ret = isert_alloc_login_buf(isert_conn, cma_id->device);
714ae9ea9edSSagi Grimberg 	if (ret)
715b8d26b3bSNicholas Bellinger 		goto out;
716b8d26b3bSNicholas Bellinger 
717cf8ae958SSagi Grimberg 	device = isert_device_get(cma_id);
718b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
719b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
720b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
721b8d26b3bSNicholas Bellinger 	}
722dac6ab30SSagi Grimberg 	isert_conn->device = device;
723b8d26b3bSNicholas Bellinger 
724422bd0acSJenny Derzhavetz 	isert_set_nego_params(isert_conn, &event->param.conn);
7251a92e17eSSagi Grimberg 
726570db170SSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id);
727b8d26b3bSNicholas Bellinger 	if (ret)
728b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
729b8d26b3bSNicholas Bellinger 
7302371e5daSSagi Grimberg 	ret = isert_rdma_post_recvl(isert_conn);
7312371e5daSSagi Grimberg 	if (ret)
7322371e5daSSagi Grimberg 		goto out_conn_dev;
7332371e5daSSagi Grimberg 
7342371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
7352371e5daSSagi Grimberg 	if (ret)
7362371e5daSSagi Grimberg 		goto out_conn_dev;
7372371e5daSSagi Grimberg 
738ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
739bd379220SJenny Derzhavetz 	list_add_tail(&isert_conn->node, &isert_np->accepted);
740ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
741b8d26b3bSNicholas Bellinger 
742b8d26b3bSNicholas Bellinger 	return 0;
743b8d26b3bSNicholas Bellinger 
744b8d26b3bSNicholas Bellinger out_conn_dev:
745cf8ae958SSagi Grimberg 	isert_device_put(device);
746b8d26b3bSNicholas Bellinger out_rsp_dma_map:
747ae9ea9edSSagi Grimberg 	isert_free_login_buf(isert_conn);
748b8d26b3bSNicholas Bellinger out:
749b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
7502371e5daSSagi Grimberg 	rdma_reject(cma_id, NULL, 0);
751b8d26b3bSNicholas Bellinger 	return ret;
752b8d26b3bSNicholas Bellinger }
753b8d26b3bSNicholas Bellinger 
754b8d26b3bSNicholas Bellinger static void
755b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
756b8d26b3bSNicholas Bellinger {
757dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
758b8d26b3bSNicholas Bellinger 
7594c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
760b8d26b3bSNicholas Bellinger 
76157df81e3SSagi Grimberg 	BUG_ON(!device);
76257df81e3SSagi Grimberg 
76357df81e3SSagi Grimberg 	if (device->use_fastreg)
764a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
76559464ef4SVu Pham 
76619e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
767dac6ab30SSagi Grimberg 	if (isert_conn->cm_id)
768dac6ab30SSagi Grimberg 		rdma_destroy_id(isert_conn->cm_id);
76919e2090fSSagi Grimberg 
770dac6ab30SSagi Grimberg 	if (isert_conn->qp) {
771dac6ab30SSagi Grimberg 		struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
7724a295baeSSagi Grimberg 
77340fc069aSSagi Grimberg 		isert_comp_put(comp);
774dac6ab30SSagi Grimberg 		ib_destroy_qp(isert_conn->qp);
775b8d26b3bSNicholas Bellinger 	}
776b8d26b3bSNicholas Bellinger 
777ae9ea9edSSagi Grimberg 	if (isert_conn->login_buf)
778ae9ea9edSSagi Grimberg 		isert_free_login_buf(isert_conn);
779ae9ea9edSSagi Grimberg 
780cf8ae958SSagi Grimberg 	isert_device_put(device);
78157df81e3SSagi Grimberg 
78257df81e3SSagi Grimberg 	kfree(isert_conn);
783b8d26b3bSNicholas Bellinger }
784b8d26b3bSNicholas Bellinger 
785b8d26b3bSNicholas Bellinger static void
786b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
787b8d26b3bSNicholas Bellinger {
78819e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
789bd379220SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
790c2f88b17SSagi Grimberg 
79124f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
792128e9cc8SSagi Grimberg 
793dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
794128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_UP;
795bd379220SJenny Derzhavetz 	kref_get(&isert_conn->kref);
796dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
797bd379220SJenny Derzhavetz 
798bd379220SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
799bd379220SJenny Derzhavetz 	list_move_tail(&isert_conn->node, &isert_np->pending);
800bd379220SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
801bd379220SJenny Derzhavetz 
802bd379220SJenny Derzhavetz 	isert_info("np %p: Allow accept_np to continue\n", isert_np);
803bd379220SJenny Derzhavetz 	up(&isert_np->sem);
804b8d26b3bSNicholas Bellinger }
805b8d26b3bSNicholas Bellinger 
806b8d26b3bSNicholas Bellinger static void
807dac6ab30SSagi Grimberg isert_release_kref(struct kref *kref)
808b8d26b3bSNicholas Bellinger {
809b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
810dac6ab30SSagi Grimberg 				struct isert_conn, kref);
811b8d26b3bSNicholas Bellinger 
8124c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
8134c22e07fSSagi Grimberg 		   current->pid);
814b8d26b3bSNicholas Bellinger 
815b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
816b8d26b3bSNicholas Bellinger }
817b8d26b3bSNicholas Bellinger 
818b8d26b3bSNicholas Bellinger static void
819b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
820b8d26b3bSNicholas Bellinger {
821dac6ab30SSagi Grimberg 	kref_put(&isert_conn->kref, isert_release_kref);
822b8d26b3bSNicholas Bellinger }
823b8d26b3bSNicholas Bellinger 
824*6d1fba0cSJenny Derzhavetz static void
825*6d1fba0cSJenny Derzhavetz isert_handle_unbound_conn(struct isert_conn *isert_conn)
826*6d1fba0cSJenny Derzhavetz {
827*6d1fba0cSJenny Derzhavetz 	struct isert_np *isert_np = isert_conn->cm_id->context;
828*6d1fba0cSJenny Derzhavetz 
829*6d1fba0cSJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
830*6d1fba0cSJenny Derzhavetz 	if (!list_empty(&isert_conn->node)) {
831*6d1fba0cSJenny Derzhavetz 		/*
832*6d1fba0cSJenny Derzhavetz 		 * This means iscsi doesn't know this connection
833*6d1fba0cSJenny Derzhavetz 		 * so schedule a cleanup ourselves
834*6d1fba0cSJenny Derzhavetz 		 */
835*6d1fba0cSJenny Derzhavetz 		list_del_init(&isert_conn->node);
836*6d1fba0cSJenny Derzhavetz 		isert_put_conn(isert_conn);
837*6d1fba0cSJenny Derzhavetz 		complete(&isert_conn->wait);
838*6d1fba0cSJenny Derzhavetz 		queue_work(isert_release_wq, &isert_conn->release_work);
839*6d1fba0cSJenny Derzhavetz 	}
840*6d1fba0cSJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
841*6d1fba0cSJenny Derzhavetz }
842*6d1fba0cSJenny Derzhavetz 
843954f2372SSagi Grimberg /**
844954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
845954f2372SSagi Grimberg  * @isert_conn: isert connection struct
846954f2372SSagi Grimberg  *
847954f2372SSagi Grimberg  * Notes:
848aea92980SJenny Derzhavetz  * In case the connection state is BOUND, move state
849954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
850128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
851954f2372SSagi Grimberg  *
852dac6ab30SSagi Grimberg  * This routine must be called with mutex held. Thus it is
853954f2372SSagi Grimberg  * safe to call multiple times.
854954f2372SSagi Grimberg  */
855954f2372SSagi Grimberg static void
856954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
857954f2372SSagi Grimberg {
858954f2372SSagi Grimberg 	int err;
859954f2372SSagi Grimberg 
860*6d1fba0cSJenny Derzhavetz 	if (isert_conn->state >= ISER_CONN_TERMINATING)
861*6d1fba0cSJenny Derzhavetz 		return;
862*6d1fba0cSJenny Derzhavetz 
86324f412ddSSagi Grimberg 	isert_info("Terminating conn %p state %d\n",
864954f2372SSagi Grimberg 		   isert_conn, isert_conn->state);
865128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_TERMINATING;
866dac6ab30SSagi Grimberg 	err = rdma_disconnect(isert_conn->cm_id);
867954f2372SSagi Grimberg 	if (err)
86824f412ddSSagi Grimberg 		isert_warn("Failed rdma_disconnect isert_conn %p\n",
869954f2372SSagi Grimberg 			   isert_conn);
870*6d1fba0cSJenny Derzhavetz 
871*6d1fba0cSJenny Derzhavetz 	isert_info("conn %p completing wait\n", isert_conn);
872*6d1fba0cSJenny Derzhavetz 	complete(&isert_conn->wait);
873954f2372SSagi Grimberg }
874954f2372SSagi Grimberg 
8753b726ae2SSagi Grimberg static int
876ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
877ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
878b8d26b3bSNicholas Bellinger {
879ea8a1616SSagi Grimberg 	isert_dbg("%s (%d): isert np %p\n",
880ea8a1616SSagi Grimberg 		  rdma_event_msg(event), event, isert_np);
8813b726ae2SSagi Grimberg 
882ca6c1d82SSagi Grimberg 	switch (event) {
883ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
884ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = NULL;
885ca6c1d82SSagi Grimberg 		break;
886ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
887ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = isert_setup_id(isert_np);
888ed8cb0a4SJenny Derzhavetz 		if (IS_ERR(isert_np->cm_id)) {
88924f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
890ed8cb0a4SJenny Derzhavetz 				  isert_np, PTR_ERR(isert_np->cm_id));
891ed8cb0a4SJenny Derzhavetz 			isert_np->cm_id = NULL;
892ca6c1d82SSagi Grimberg 		}
893ca6c1d82SSagi Grimberg 		break;
894ca6c1d82SSagi Grimberg 	default:
89524f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
896ca6c1d82SSagi Grimberg 			  isert_np, event);
897ca6c1d82SSagi Grimberg 	}
898ca6c1d82SSagi Grimberg 
8993b726ae2SSagi Grimberg 	return -1;
9003b726ae2SSagi Grimberg }
9013b726ae2SSagi Grimberg 
902ca6c1d82SSagi Grimberg static int
903ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
904ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
905ca6c1d82SSagi Grimberg {
906f81bf458SJenny Derzhavetz 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
907ca6c1d82SSagi Grimberg 
908dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
909*6d1fba0cSJenny Derzhavetz 	switch (isert_conn->state) {
910*6d1fba0cSJenny Derzhavetz 	case ISER_CONN_TERMINATING:
911*6d1fba0cSJenny Derzhavetz 		break;
912*6d1fba0cSJenny Derzhavetz 	case ISER_CONN_UP:
913128e9cc8SSagi Grimberg 		isert_conn_terminate(isert_conn);
914*6d1fba0cSJenny Derzhavetz 		isert_wait4flush(isert_conn);
915*6d1fba0cSJenny Derzhavetz 		isert_handle_unbound_conn(isert_conn);
916*6d1fba0cSJenny Derzhavetz 		break;
917*6d1fba0cSJenny Derzhavetz 	case ISER_CONN_BOUND:
918*6d1fba0cSJenny Derzhavetz 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
919*6d1fba0cSJenny Derzhavetz 		iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
920*6d1fba0cSJenny Derzhavetz 		break;
921*6d1fba0cSJenny Derzhavetz 	default:
922*6d1fba0cSJenny Derzhavetz 		isert_warn("conn %p teminating in state %d\n",
923*6d1fba0cSJenny Derzhavetz 			   isert_conn, isert_conn->state);
924*6d1fba0cSJenny Derzhavetz 	}
925dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
926128e9cc8SSagi Grimberg 
9273b726ae2SSagi Grimberg 	return 0;
928b8d26b3bSNicholas Bellinger }
929b8d26b3bSNicholas Bellinger 
9304a579da2SSagi Grimberg static int
931954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
932954f2372SSagi Grimberg {
93319e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
934954f2372SSagi Grimberg 
935bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
936dac6ab30SSagi Grimberg 	isert_conn->cm_id = NULL;
937954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
9384a579da2SSagi Grimberg 
9394a579da2SSagi Grimberg 	return -1;
940954f2372SSagi Grimberg }
941954f2372SSagi Grimberg 
942b8d26b3bSNicholas Bellinger static int
943b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
944b8d26b3bSNicholas Bellinger {
945f81bf458SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
946b8d26b3bSNicholas Bellinger 	int ret = 0;
947b8d26b3bSNicholas Bellinger 
948ea8a1616SSagi Grimberg 	isert_info("%s (%d): status %d id %p np %p\n",
949ea8a1616SSagi Grimberg 		   rdma_event_msg(event->event), event->event,
9504c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
951b8d26b3bSNicholas Bellinger 
952f81bf458SJenny Derzhavetz 	if (isert_np->cm_id == cma_id)
953f81bf458SJenny Derzhavetz 		return isert_np_cma_handler(cma_id->context, event->event);
954f81bf458SJenny Derzhavetz 
955b8d26b3bSNicholas Bellinger 	switch (event->event) {
956b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
957b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
9583b726ae2SSagi Grimberg 		if (ret)
9594c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
960b8d26b3bSNicholas Bellinger 		break;
961b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
962b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
963b8d26b3bSNicholas Bellinger 		break;
96488c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
96588c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
96688c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
96788c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
968ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
969b8d26b3bSNicholas Bellinger 		break;
970954f2372SSagi Grimberg 	case RDMA_CM_EVENT_REJECTED:       /* FALLTHRU */
971954f2372SSagi Grimberg 	case RDMA_CM_EVENT_UNREACHABLE:    /* FALLTHRU */
972b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
9734a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
974954f2372SSagi Grimberg 		break;
975b8d26b3bSNicholas Bellinger 	default:
97624f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
977b8d26b3bSNicholas Bellinger 		break;
978b8d26b3bSNicholas Bellinger 	}
979b8d26b3bSNicholas Bellinger 
980b8d26b3bSNicholas Bellinger 	return ret;
981b8d26b3bSNicholas Bellinger }
982b8d26b3bSNicholas Bellinger 
983b8d26b3bSNicholas Bellinger static int
9844366b19cSJenny Derzhavetz isert_post_recvm(struct isert_conn *isert_conn, u32 count)
985b8d26b3bSNicholas Bellinger {
986b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
987b8d26b3bSNicholas Bellinger 	int i, ret;
988b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
989b8d26b3bSNicholas Bellinger 
990dac6ab30SSagi Grimberg 	for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
9914366b19cSJenny Derzhavetz 		rx_desc = &isert_conn->rx_descs[i];
992b0a191e7SSagi Grimberg 		rx_wr->wr_id = (uintptr_t)rx_desc;
993b8d26b3bSNicholas Bellinger 		rx_wr->sg_list = &rx_desc->rx_sg;
994b8d26b3bSNicholas Bellinger 		rx_wr->num_sge = 1;
995b8d26b3bSNicholas Bellinger 		rx_wr->next = rx_wr + 1;
996b8d26b3bSNicholas Bellinger 	}
997b8d26b3bSNicholas Bellinger 	rx_wr--;
998b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
999b8d26b3bSNicholas Bellinger 
1000dac6ab30SSagi Grimberg 	ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
1001b8d26b3bSNicholas Bellinger 			   &rx_wr_failed);
1002*6d1fba0cSJenny Derzhavetz 	if (ret)
100324f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
10044366b19cSJenny Derzhavetz 
10054366b19cSJenny Derzhavetz 	return ret;
10064366b19cSJenny Derzhavetz }
10074366b19cSJenny Derzhavetz 
10084366b19cSJenny Derzhavetz static int
10094366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
10104366b19cSJenny Derzhavetz {
10114366b19cSJenny Derzhavetz 	struct ib_recv_wr *rx_wr_failed, rx_wr;
10124366b19cSJenny Derzhavetz 	int ret;
10134366b19cSJenny Derzhavetz 
10144366b19cSJenny Derzhavetz 	rx_wr.wr_id = (uintptr_t)rx_desc;
10154366b19cSJenny Derzhavetz 	rx_wr.sg_list = &rx_desc->rx_sg;
10164366b19cSJenny Derzhavetz 	rx_wr.num_sge = 1;
10174366b19cSJenny Derzhavetz 	rx_wr.next = NULL;
10184366b19cSJenny Derzhavetz 
10194366b19cSJenny Derzhavetz 	ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
1020*6d1fba0cSJenny Derzhavetz 	if (ret)
10214366b19cSJenny Derzhavetz 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
10224366b19cSJenny Derzhavetz 
1023b8d26b3bSNicholas Bellinger 	return ret;
1024b8d26b3bSNicholas Bellinger }
1025b8d26b3bSNicholas Bellinger 
1026b8d26b3bSNicholas Bellinger static int
1027b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1028b8d26b3bSNicholas Bellinger {
1029dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1030b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
1031b8d26b3bSNicholas Bellinger 	int ret;
1032b8d26b3bSNicholas Bellinger 
1033b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1034b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
1035b8d26b3bSNicholas Bellinger 
1036b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
1037b0a191e7SSagi Grimberg 	send_wr.wr_id	= (uintptr_t)tx_desc;
1038b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
1039b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
1040b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
1041b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
1042b8d26b3bSNicholas Bellinger 
1043dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
1044bdf20e72SSagi Grimberg 	if (ret)
104524f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
1046b8d26b3bSNicholas Bellinger 
1047b8d26b3bSNicholas Bellinger 	return ret;
1048b8d26b3bSNicholas Bellinger }
1049b8d26b3bSNicholas Bellinger 
1050b8d26b3bSNicholas Bellinger static void
1051b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
1052b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
1053b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
1054b8d26b3bSNicholas Bellinger {
1055dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
105667cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1057b8d26b3bSNicholas Bellinger 
1058b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1059b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
1060b8d26b3bSNicholas Bellinger 
1061d3cf81f9SSagi Grimberg 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
1062d3cf81f9SSagi Grimberg 	tx_desc->iser_header.flags = ISCSI_CTRL;
1063b8d26b3bSNicholas Bellinger 
1064b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
1065b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
1066b8d26b3bSNicholas Bellinger 
106734efc7dfSJason Gunthorpe 	if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
106834efc7dfSJason Gunthorpe 		tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
106924f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1070b8d26b3bSNicholas Bellinger 	}
1071b8d26b3bSNicholas Bellinger }
1072b8d26b3bSNicholas Bellinger 
1073b8d26b3bSNicholas Bellinger static int
1074b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
1075b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
1076b8d26b3bSNicholas Bellinger {
1077dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
107867cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1079b8d26b3bSNicholas Bellinger 	u64 dma_addr;
1080b8d26b3bSNicholas Bellinger 
1081b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1082b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
1083b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
108424f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
1085b8d26b3bSNicholas Bellinger 		return -ENOMEM;
1086b8d26b3bSNicholas Bellinger 	}
1087b8d26b3bSNicholas Bellinger 
1088b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
1089b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
1090b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
109134efc7dfSJason Gunthorpe 	tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
1092b8d26b3bSNicholas Bellinger 
10934c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
10944c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
10954c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
1096b8d26b3bSNicholas Bellinger 
1097b8d26b3bSNicholas Bellinger 	return 0;
1098b8d26b3bSNicholas Bellinger }
1099b8d26b3bSNicholas Bellinger 
1100b8d26b3bSNicholas Bellinger static void
110195b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
110268a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
1103b8d26b3bSNicholas Bellinger {
110495b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
110595b60f07SNicholas Bellinger 
1106b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1107b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
1108422bd0acSJenny Derzhavetz 
1109422bd0acSJenny Derzhavetz 	if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
1110422bd0acSJenny Derzhavetz 		send_wr->opcode  = IB_WR_SEND_WITH_INV;
1111422bd0acSJenny Derzhavetz 		send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
1112422bd0acSJenny Derzhavetz 	} else {
1113b8d26b3bSNicholas Bellinger 		send_wr->opcode = IB_WR_SEND;
1114422bd0acSJenny Derzhavetz 	}
1115422bd0acSJenny Derzhavetz 
111695b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
1117b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
111895b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
1119b8d26b3bSNicholas Bellinger }
1120b8d26b3bSNicholas Bellinger 
1121b8d26b3bSNicholas Bellinger static int
1122b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
1123b8d26b3bSNicholas Bellinger {
1124b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
1125b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
1126b8d26b3bSNicholas Bellinger 	int ret;
1127b8d26b3bSNicholas Bellinger 
1128b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
1129b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
1130b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
113134efc7dfSJason Gunthorpe 	sge.lkey = isert_conn->device->pd->local_dma_lkey;
1132b8d26b3bSNicholas Bellinger 
113324f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
1134b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
1135b8d26b3bSNicholas Bellinger 
1136b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1137b0a191e7SSagi Grimberg 	rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
1138b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1139b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1140b8d26b3bSNicholas Bellinger 
1141dac6ab30SSagi Grimberg 	ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
1142*6d1fba0cSJenny Derzhavetz 	if (ret)
114324f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
1144b8d26b3bSNicholas Bellinger 
1145b8d26b3bSNicholas Bellinger 	return ret;
1146b8d26b3bSNicholas Bellinger }
1147b8d26b3bSNicholas Bellinger 
1148b8d26b3bSNicholas Bellinger static int
1149b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1150b8d26b3bSNicholas Bellinger 		   u32 length)
1151b8d26b3bSNicholas Bellinger {
1152b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1153dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
115467cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1155dac6ab30SSagi Grimberg 	struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
1156b8d26b3bSNicholas Bellinger 	int ret;
1157b8d26b3bSNicholas Bellinger 
1158b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1159b8d26b3bSNicholas Bellinger 
1160b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1161b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1162b8d26b3bSNicholas Bellinger 
1163b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1164b8d26b3bSNicholas Bellinger 
1165b8d26b3bSNicholas Bellinger 	if (length > 0) {
1166b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1167b8d26b3bSNicholas Bellinger 
1168b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1169b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1170b8d26b3bSNicholas Bellinger 
1171b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1172b8d26b3bSNicholas Bellinger 
1173b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1174b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1175b8d26b3bSNicholas Bellinger 
1176b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1177b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
117834efc7dfSJason Gunthorpe 		tx_dsg->lkey	= isert_conn->device->pd->local_dma_lkey;
1179b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1180b8d26b3bSNicholas Bellinger 	}
1181b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1182b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1183e0546fc1SSagi Grimberg 			if (!conn->sess->sess_ops->SessionType &&
1184dac6ab30SSagi Grimberg 			    isert_conn->device->use_fastreg) {
1185570db170SSagi Grimberg 				ret = isert_conn_create_fastreg_pool(isert_conn);
1186f46d6a8aSNicholas Bellinger 				if (ret) {
118724f412ddSSagi Grimberg 					isert_err("Conn: %p failed to create"
1188f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1189f46d6a8aSNicholas Bellinger 					return ret;
1190f46d6a8aSNicholas Bellinger 				}
1191f46d6a8aSNicholas Bellinger 			}
1192f46d6a8aSNicholas Bellinger 
1193b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1194b8d26b3bSNicholas Bellinger 			if (ret)
1195b8d26b3bSNicholas Bellinger 				return ret;
1196b8d26b3bSNicholas Bellinger 
11974366b19cSJenny Derzhavetz 			ret = isert_post_recvm(isert_conn,
11984366b19cSJenny Derzhavetz 					       ISERT_QP_MAX_RECV_DTOS);
1199b8d26b3bSNicholas Bellinger 			if (ret)
1200b8d26b3bSNicholas Bellinger 				return ret;
1201b8d26b3bSNicholas Bellinger 
1202128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
1203dac6ab30SSagi Grimberg 			mutex_lock(&isert_conn->mutex);
1204128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
1205dac6ab30SSagi Grimberg 			mutex_unlock(&isert_conn->mutex);
1206b8d26b3bSNicholas Bellinger 			goto post_send;
1207b8d26b3bSNicholas Bellinger 		}
1208b8d26b3bSNicholas Bellinger 
1209b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1210b8d26b3bSNicholas Bellinger 		if (ret)
1211b8d26b3bSNicholas Bellinger 			return ret;
1212b8d26b3bSNicholas Bellinger 	}
1213b8d26b3bSNicholas Bellinger post_send:
1214b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1215b8d26b3bSNicholas Bellinger 	if (ret)
1216b8d26b3bSNicholas Bellinger 		return ret;
1217b8d26b3bSNicholas Bellinger 
1218b8d26b3bSNicholas Bellinger 	return 0;
1219b8d26b3bSNicholas Bellinger }
1220b8d26b3bSNicholas Bellinger 
1221b8d26b3bSNicholas Bellinger static void
12222371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1223b8d26b3bSNicholas Bellinger {
12242371e5daSSagi Grimberg 	struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
12252371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1226b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1227b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1228b8d26b3bSNicholas Bellinger 	int size;
1229b8d26b3bSNicholas Bellinger 
123024f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
12312371e5daSSagi Grimberg 
12322371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1233b8d26b3bSNicholas Bellinger 
1234b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1235b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1236b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1237b8d26b3bSNicholas Bellinger 		/*
1238b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1239b8d26b3bSNicholas Bellinger 		 * login request PDU.
1240b8d26b3bSNicholas Bellinger 		 */
1241b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1242b8d26b3bSNicholas Bellinger 		login->current_stage =
1243b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1244b8d26b3bSNicholas Bellinger 			 >> 2;
1245b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1246b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1247b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1248b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1249b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1250b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1251b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1252b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1253b8d26b3bSNicholas Bellinger 	}
1254b8d26b3bSNicholas Bellinger 
1255b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1256b8d26b3bSNicholas Bellinger 
1257b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
12584c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
12594c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
12604c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
1261b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1262b8d26b3bSNicholas Bellinger 
12636faaa85fSNicholas Bellinger 	if (login->first_request) {
1264dac6ab30SSagi Grimberg 		complete(&isert_conn->login_comp);
12656faaa85fSNicholas Bellinger 		return;
12666faaa85fSNicholas Bellinger 	}
12676faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1268b8d26b3bSNicholas Bellinger }
1269b8d26b3bSNicholas Bellinger 
1270b8d26b3bSNicholas Bellinger static struct iscsi_cmd
12714366b19cSJenny Derzhavetz *isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
1272b8d26b3bSNicholas Bellinger {
12736700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1274b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1275d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1276b8d26b3bSNicholas Bellinger 
1277676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1278d703ce2fSNicholas Bellinger 	if (!cmd) {
127924f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1280b8d26b3bSNicholas Bellinger 		return NULL;
1281b8d26b3bSNicholas Bellinger 	}
1282d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1283b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1284d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
12854366b19cSJenny Derzhavetz 	isert_cmd->rx_desc = rx_desc;
1286b8d26b3bSNicholas Bellinger 
1287d703ce2fSNicholas Bellinger 	return cmd;
1288b8d26b3bSNicholas Bellinger }
1289b8d26b3bSNicholas Bellinger 
1290b8d26b3bSNicholas Bellinger static int
1291b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1292d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1293d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1294b8d26b3bSNicholas Bellinger {
1295b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1296b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1297b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1298b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
12999fd60088SJenny Derzhavetz 	unsigned int data_len;
1300b8d26b3bSNicholas Bellinger 
1301b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1302b8d26b3bSNicholas Bellinger 	if (rc < 0)
1303b8d26b3bSNicholas Bellinger 		return rc;
1304b8d26b3bSNicholas Bellinger 
1305b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1306b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1307b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
13089fd60088SJenny Derzhavetz 	data_len = cmd->se_cmd.data_length;
1309b8d26b3bSNicholas Bellinger 
13109fd60088SJenny Derzhavetz 	if (imm_data && imm_data_len == data_len)
13119fd60088SJenny Derzhavetz 		cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1312b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1313b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1314b8d26b3bSNicholas Bellinger 		return 0;
1315b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1316b8d26b3bSNicholas Bellinger 		dump_payload = true;
1317b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1318b8d26b3bSNicholas Bellinger 	}
1319b8d26b3bSNicholas Bellinger 
1320b8d26b3bSNicholas Bellinger 	if (!imm_data)
1321b8d26b3bSNicholas Bellinger 		return 0;
1322b8d26b3bSNicholas Bellinger 
13239fd60088SJenny Derzhavetz 	if (imm_data_len != data_len) {
1324b8d26b3bSNicholas Bellinger 		sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
13259fd60088SJenny Derzhavetz 		sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
13269fd60088SJenny Derzhavetz 				    &rx_desc->data[0], imm_data_len);
13279fd60088SJenny Derzhavetz 		isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
13289fd60088SJenny Derzhavetz 			  sg_nents, imm_data_len);
13299fd60088SJenny Derzhavetz 	} else {
13309fd60088SJenny Derzhavetz 		sg_init_table(&isert_cmd->sg, 1);
13319fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_sg = &isert_cmd->sg;
13329fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_nents = 1;
13339fd60088SJenny Derzhavetz 		sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
13349fd60088SJenny Derzhavetz 		isert_dbg("Transfer Immediate imm_data_len: %d\n",
13359fd60088SJenny Derzhavetz 			  imm_data_len);
13369fd60088SJenny Derzhavetz 	}
1337b8d26b3bSNicholas Bellinger 
1338b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1339b8d26b3bSNicholas Bellinger 
1340b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1341b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1342b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1343b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1344b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1345b8d26b3bSNicholas Bellinger 	}
1346b8d26b3bSNicholas Bellinger 
1347b8d26b3bSNicholas Bellinger sequence_cmd:
1348561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1349b8d26b3bSNicholas Bellinger 
1350b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1351b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
13526cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1353afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1354b8d26b3bSNicholas Bellinger 
1355b8d26b3bSNicholas Bellinger 	return 0;
1356b8d26b3bSNicholas Bellinger }
1357b8d26b3bSNicholas Bellinger 
1358b8d26b3bSNicholas Bellinger static int
1359b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1360b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1361b8d26b3bSNicholas Bellinger {
1362b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1363b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1364b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1365b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1366b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1367b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1368b8d26b3bSNicholas Bellinger 
1369b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1370b8d26b3bSNicholas Bellinger 	if (rc < 0)
1371b8d26b3bSNicholas Bellinger 		return rc;
1372b8d26b3bSNicholas Bellinger 	else if (!cmd)
1373b8d26b3bSNicholas Bellinger 		return 0;
1374b8d26b3bSNicholas Bellinger 	/*
1375b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1376b8d26b3bSNicholas Bellinger 	 */
1377b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
137824f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1379b8d26b3bSNicholas Bellinger 		dump_stack();
1380b8d26b3bSNicholas Bellinger 		return -1;
1381b8d26b3bSNicholas Bellinger 	}
1382b8d26b3bSNicholas Bellinger 
13834c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
13844c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
13854c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
13864c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1387b8d26b3bSNicholas Bellinger 
1388b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1389b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1390b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1391b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1392b8d26b3bSNicholas Bellinger 	/*
1393b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1394b8d26b3bSNicholas Bellinger 	 */
1395b8d26b3bSNicholas Bellinger 	if (page_off) {
13964c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1397b8d26b3bSNicholas Bellinger 		dump_stack();
1398b8d26b3bSNicholas Bellinger 		return -1;
1399b8d26b3bSNicholas Bellinger 	}
14004c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
14014c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
14024c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1403b8d26b3bSNicholas Bellinger 
1404b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1405b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1406b8d26b3bSNicholas Bellinger 
1407b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1408b8d26b3bSNicholas Bellinger 	if (rc < 0)
1409b8d26b3bSNicholas Bellinger 		return rc;
1410b8d26b3bSNicholas Bellinger 
14114366b19cSJenny Derzhavetz 	/*
14124366b19cSJenny Derzhavetz 	 * multiple data-outs on the same command can arrive -
14134366b19cSJenny Derzhavetz 	 * so post the buffer before hand
14144366b19cSJenny Derzhavetz 	 */
14154366b19cSJenny Derzhavetz 	rc = isert_post_recv(isert_conn, rx_desc);
14164366b19cSJenny Derzhavetz 	if (rc) {
14174366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", rc);
14184366b19cSJenny Derzhavetz 		return rc;
14194366b19cSJenny Derzhavetz 	}
1420b8d26b3bSNicholas Bellinger 	return 0;
1421b8d26b3bSNicholas Bellinger }
1422b8d26b3bSNicholas Bellinger 
1423b8d26b3bSNicholas Bellinger static int
1424778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1425d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1426d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1427778de368SNicholas Bellinger {
1428778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1429778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1430778de368SNicholas Bellinger 	int rc;
1431778de368SNicholas Bellinger 
1432778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1433778de368SNicholas Bellinger 	if (rc < 0)
1434778de368SNicholas Bellinger 		return rc;
1435778de368SNicholas Bellinger 	/*
1436778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1437778de368SNicholas Bellinger 	 */
1438778de368SNicholas Bellinger 
1439778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1440778de368SNicholas Bellinger }
1441778de368SNicholas Bellinger 
1442778de368SNicholas Bellinger static int
1443adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1444d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1445d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1446adb54c29SNicholas Bellinger {
1447adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1448adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1449adb54c29SNicholas Bellinger 	int rc;
1450b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1451adb54c29SNicholas Bellinger 
1452adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1453adb54c29SNicholas Bellinger 	if (rc < 0)
1454adb54c29SNicholas Bellinger 		return rc;
1455adb54c29SNicholas Bellinger 
1456b44a2b67SSagi Grimberg 	if (payload_length) {
1457adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
1458adb54c29SNicholas Bellinger 		if (!text_in) {
145924f412ddSSagi Grimberg 			isert_err("Unable to allocate text_in of payload_length: %u\n",
1460adb54c29SNicholas Bellinger 				  payload_length);
1461adb54c29SNicholas Bellinger 			return -ENOMEM;
1462adb54c29SNicholas Bellinger 		}
1463b44a2b67SSagi Grimberg 	}
1464adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1465adb54c29SNicholas Bellinger 
1466adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1467adb54c29SNicholas Bellinger 
1468adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1469adb54c29SNicholas Bellinger }
1470adb54c29SNicholas Bellinger 
1471adb54c29SNicholas Bellinger static int
1472b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1473b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1474b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1475b8d26b3bSNicholas Bellinger {
1476b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1477b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1478b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1479b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1480b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1481b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1482b8d26b3bSNicholas Bellinger 
1483fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1484ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
148524f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1486ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1487ca40d24eSNicholas Bellinger 		return 0;
1488ca40d24eSNicholas Bellinger 	}
1489ca40d24eSNicholas Bellinger 
1490b8d26b3bSNicholas Bellinger 	switch (opcode) {
1491b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
14924366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1493b8d26b3bSNicholas Bellinger 		if (!cmd)
1494b8d26b3bSNicholas Bellinger 			break;
1495b8d26b3bSNicholas Bellinger 
1496d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1497b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1498b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1499b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1500b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1501422bd0acSJenny Derzhavetz 		isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
1502b8d26b3bSNicholas Bellinger 
1503d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1504b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1505b8d26b3bSNicholas Bellinger 		break;
1506b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
15074366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1508b8d26b3bSNicholas Bellinger 		if (!cmd)
1509b8d26b3bSNicholas Bellinger 			break;
1510b8d26b3bSNicholas Bellinger 
1511d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1512d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1513778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1514b8d26b3bSNicholas Bellinger 		break;
1515b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1516b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1517b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1518b8d26b3bSNicholas Bellinger 		break;
1519b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
15204366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1521b8d26b3bSNicholas Bellinger 		if (!cmd)
1522b8d26b3bSNicholas Bellinger 			break;
1523b8d26b3bSNicholas Bellinger 
1524b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1525b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1526b8d26b3bSNicholas Bellinger 		break;
1527b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
15284366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1529b8d26b3bSNicholas Bellinger 		if (!cmd)
1530b8d26b3bSNicholas Bellinger 			break;
1531b8d26b3bSNicholas Bellinger 
1532b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1533b8d26b3bSNicholas Bellinger 		break;
1534adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
15354366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1536e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
15374366b19cSJenny Derzhavetz 		else
15384366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
15394366b19cSJenny Derzhavetz 
1540e4f4e801SSagi Grimberg 		if (!cmd)
1541e4f4e801SSagi Grimberg 			break;
1542adb54c29SNicholas Bellinger 
1543d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1544d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1545adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1546adb54c29SNicholas Bellinger 		break;
1547b8d26b3bSNicholas Bellinger 	default:
154824f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1549b8d26b3bSNicholas Bellinger 		dump_stack();
1550b8d26b3bSNicholas Bellinger 		break;
1551b8d26b3bSNicholas Bellinger 	}
1552b8d26b3bSNicholas Bellinger 
1553b8d26b3bSNicholas Bellinger 	return ret;
1554b8d26b3bSNicholas Bellinger }
1555b8d26b3bSNicholas Bellinger 
1556b8d26b3bSNicholas Bellinger static void
1557b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1558b8d26b3bSNicholas Bellinger {
1559d3cf81f9SSagi Grimberg 	struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
1560b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1561b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1562b8d26b3bSNicholas Bellinger 
1563d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1564b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1565d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1566d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1567d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
15684c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1569b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1570b8d26b3bSNicholas Bellinger 		}
1571d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1572d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1573d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
15744c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1575b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1576b8d26b3bSNicholas Bellinger 		}
1577b8d26b3bSNicholas Bellinger 
157824f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1579b8d26b3bSNicholas Bellinger 		break;
1580b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
158124f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1582b8d26b3bSNicholas Bellinger 		break;
1583b8d26b3bSNicholas Bellinger 	default:
1584d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1585b8d26b3bSNicholas Bellinger 		break;
1586b8d26b3bSNicholas Bellinger 	}
1587b8d26b3bSNicholas Bellinger 
15886c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1589b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
1590b8d26b3bSNicholas Bellinger }
1591b8d26b3bSNicholas Bellinger 
1592b8d26b3bSNicholas Bellinger static void
15937748681bSSagi Grimberg isert_rcv_completion(struct iser_rx_desc *desc,
15947748681bSSagi Grimberg 		     struct isert_conn *isert_conn,
15954a295baeSSagi Grimberg 		     u32 xfer_len)
1596b8d26b3bSNicholas Bellinger {
1597dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1598b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1599b8d26b3bSNicholas Bellinger 	u64 rx_dma;
16004366b19cSJenny Derzhavetz 	int rx_buflen;
1601b8d26b3bSNicholas Bellinger 
1602b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1603b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1604b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
16054c22e07fSSagi Grimberg 		isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1606b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1607b8d26b3bSNicholas Bellinger 	} else {
1608b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1609b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
16104c22e07fSSagi Grimberg 		isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1611b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1612b8d26b3bSNicholas Bellinger 	}
1613b8d26b3bSNicholas Bellinger 
1614b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1615b8d26b3bSNicholas Bellinger 
1616b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
161724f412ddSSagi Grimberg 	isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1618b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1619b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1620b8d26b3bSNicholas Bellinger 
16212371e5daSSagi Grimberg 	if ((char *)desc == isert_conn->login_req_buf) {
16222371e5daSSagi Grimberg 		isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
16232371e5daSSagi Grimberg 		if (isert_conn->conn) {
16242371e5daSSagi Grimberg 			struct iscsi_login *login = isert_conn->conn->conn_login;
16252371e5daSSagi Grimberg 
16262371e5daSSagi Grimberg 			if (login && !login->first_request)
16272371e5daSSagi Grimberg 				isert_rx_login_req(isert_conn);
16282371e5daSSagi Grimberg 		}
1629dac6ab30SSagi Grimberg 		mutex_lock(&isert_conn->mutex);
16302371e5daSSagi Grimberg 		complete(&isert_conn->login_req_comp);
1631dac6ab30SSagi Grimberg 		mutex_unlock(&isert_conn->mutex);
16322371e5daSSagi Grimberg 	} else {
1633b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
16342371e5daSSagi Grimberg 	}
1635b8d26b3bSNicholas Bellinger 
1636b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1637b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1638b8d26b3bSNicholas Bellinger 
1639b8d26b3bSNicholas Bellinger }
1640b8d26b3bSNicholas Bellinger 
1641e3d7e4c3SSagi Grimberg static int
1642e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1643e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1644e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1645e3d7e4c3SSagi Grimberg {
1646dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1647e3d7e4c3SSagi Grimberg 
1648e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1649e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1650e3d7e4c3SSagi Grimberg 
1651e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1652e3d7e4c3SSagi Grimberg 	data->offset = offset;
1653e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1654e3d7e4c3SSagi Grimberg 
1655e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1656e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1657e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1658e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1659e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1660e3d7e4c3SSagi Grimberg 
1661e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1662e3d7e4c3SSagi Grimberg 					data->dma_dir);
1663e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
166424f412ddSSagi Grimberg 		isert_err("Cmd: unable to dma map SGs %p\n", sg);
1665e3d7e4c3SSagi Grimberg 		return -EINVAL;
1666e3d7e4c3SSagi Grimberg 	}
1667e3d7e4c3SSagi Grimberg 
166824f412ddSSagi Grimberg 	isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1669e3d7e4c3SSagi Grimberg 		  isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1670e3d7e4c3SSagi Grimberg 
1671e3d7e4c3SSagi Grimberg 	return 0;
1672e3d7e4c3SSagi Grimberg }
1673e3d7e4c3SSagi Grimberg 
1674e3d7e4c3SSagi Grimberg static void
1675e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1676e3d7e4c3SSagi Grimberg {
1677dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1678e3d7e4c3SSagi Grimberg 
1679e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1680e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1681e3d7e4c3SSagi Grimberg }
1682e3d7e4c3SSagi Grimberg 
1683e3d7e4c3SSagi Grimberg 
1684e3d7e4c3SSagi Grimberg 
1685b8d26b3bSNicholas Bellinger static void
1686b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1687b8d26b3bSNicholas Bellinger {
1688b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1689b8d26b3bSNicholas Bellinger 
16904c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1691e3d7e4c3SSagi Grimberg 
1692e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
16934c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1694e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1695b8d26b3bSNicholas Bellinger 	}
1696b8d26b3bSNicholas Bellinger 
1697e622f2f4SChristoph Hellwig 	if (wr->rdma_wr) {
16984c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free send_wr\n", isert_cmd);
1699e622f2f4SChristoph Hellwig 		kfree(wr->rdma_wr);
1700e622f2f4SChristoph Hellwig 		wr->rdma_wr = NULL;
170190ecc6e2SVu Pham 	}
1702b8d26b3bSNicholas Bellinger 
170390ecc6e2SVu Pham 	if (wr->ib_sge) {
17044c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
170590ecc6e2SVu Pham 		kfree(wr->ib_sge);
170690ecc6e2SVu Pham 		wr->ib_sge = NULL;
170790ecc6e2SVu Pham 	}
1708b8d26b3bSNicholas Bellinger }
1709b8d26b3bSNicholas Bellinger 
1710b8d26b3bSNicholas Bellinger static void
1711a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
171259464ef4SVu Pham {
171359464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
171459464ef4SVu Pham 
17154c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
171659464ef4SVu Pham 
171759464ef4SVu Pham 	if (wr->fr_desc) {
17184c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
17199e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
17209e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
17219e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
17229e961ae7SSagi Grimberg 		}
1723dac6ab30SSagi Grimberg 		spin_lock_bh(&isert_conn->pool_lock);
1724dac6ab30SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1725dac6ab30SSagi Grimberg 		spin_unlock_bh(&isert_conn->pool_lock);
172659464ef4SVu Pham 		wr->fr_desc = NULL;
172759464ef4SVu Pham 	}
172859464ef4SVu Pham 
1729e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
17304c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1731e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
173259464ef4SVu Pham 	}
173359464ef4SVu Pham 
173459464ef4SVu Pham 	wr->ib_sge = NULL;
1735e622f2f4SChristoph Hellwig 	wr->rdma_wr = NULL;
173659464ef4SVu Pham }
173759464ef4SVu Pham 
173859464ef4SVu Pham static void
173903e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1740b8d26b3bSNicholas Bellinger {
1741d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1742b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1743186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1744dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1745e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1746b8d26b3bSNicholas Bellinger 
17474c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1748b8d26b3bSNicholas Bellinger 
1749b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1750b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1751b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1752b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17535159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1754b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1755b8d26b3bSNicholas Bellinger 
175603e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1757b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
175803e7848aSNicholas Bellinger 			/*
175903e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
176003e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
176103e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
176203e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
176303e7848aSNicholas Bellinger 			 */
176403e7848aSNicholas Bellinger 			if (comp_err &&
176503e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
176603e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
176703e7848aSNicholas Bellinger 
1768afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
176903e7848aSNicholas Bellinger 			}
177003e7848aSNicholas Bellinger 		}
1771b8d26b3bSNicholas Bellinger 
1772d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1773186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1774186a9647SNicholas Bellinger 		break;
1775b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1776186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1777186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17785159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1779186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1780186a9647SNicholas Bellinger 
1781b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1782b8d26b3bSNicholas Bellinger 		break;
1783b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1784b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1785adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1786e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1787e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1788e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1789e4f4e801SSagi Grimberg 			break;
1790e4f4e801SSagi Grimberg 
1791b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1792b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17935159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1794b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1795b8d26b3bSNicholas Bellinger 
1796b8d26b3bSNicholas Bellinger 		/*
1797b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1798b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1799b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1800b8d26b3bSNicholas Bellinger 		 */
1801b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
180211378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
18033df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1804b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1805b8d26b3bSNicholas Bellinger 			break;
1806b8d26b3bSNicholas Bellinger 		}
1807b8d26b3bSNicholas Bellinger 		/*
1808b8d26b3bSNicholas Bellinger 		 * Fall-through
1809b8d26b3bSNicholas Bellinger 		 */
1810b8d26b3bSNicholas Bellinger 	default:
1811d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1812b8d26b3bSNicholas Bellinger 		break;
1813b8d26b3bSNicholas Bellinger 	}
1814b8d26b3bSNicholas Bellinger }
1815b8d26b3bSNicholas Bellinger 
1816b8d26b3bSNicholas Bellinger static void
1817b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1818b8d26b3bSNicholas Bellinger {
1819b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
18204c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1821b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1822b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1823b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1824b8d26b3bSNicholas Bellinger 	}
1825b8d26b3bSNicholas Bellinger }
1826b8d26b3bSNicholas Bellinger 
1827b8d26b3bSNicholas Bellinger static void
1828b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
182903e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1830b8d26b3bSNicholas Bellinger {
1831dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
18324c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1833dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1834dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1835dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1836b8d26b3bSNicholas Bellinger 	}
1837b8d26b3bSNicholas Bellinger 
1838b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
183903e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1840b8d26b3bSNicholas Bellinger }
1841b8d26b3bSNicholas Bellinger 
184296b7973eSSagi Grimberg static int
184396b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1844f93f3a70SSagi Grimberg {
18459e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
184696b7973eSSagi Grimberg 	int ret;
1847f93f3a70SSagi Grimberg 
184896b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
18499e961ae7SSagi Grimberg 	if (ret) {
185024f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
18519e961ae7SSagi Grimberg 		goto fail_mr_status;
18529e961ae7SSagi Grimberg 	}
185396b7973eSSagi Grimberg 
18549e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
185596b7973eSSagi Grimberg 		u64 sec_offset_err;
18569e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
18579e961ae7SSagi Grimberg 
18589e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
18599e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
18609e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
18619e961ae7SSagi Grimberg 			break;
18629e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
18639e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
18649e961ae7SSagi Grimberg 			break;
18659e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
18669e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
18679e961ae7SSagi Grimberg 			break;
18689e961ae7SSagi Grimberg 		}
186996b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
187096b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
187196b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
18729e961ae7SSagi Grimberg 
18734c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
18749e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
18759e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
18769e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
18779e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
18789e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
18799e961ae7SSagi Grimberg 		ret = 1;
18809e961ae7SSagi Grimberg 	}
18819e961ae7SSagi Grimberg 
18829e961ae7SSagi Grimberg fail_mr_status:
188396b7973eSSagi Grimberg 	return ret;
188496b7973eSSagi Grimberg }
188596b7973eSSagi Grimberg 
188696b7973eSSagi Grimberg static void
188796b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
188896b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
188996b7973eSSagi Grimberg {
189096b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
189196b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
189296b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
189396b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
1894dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
189596b7973eSSagi Grimberg 	int ret = 0;
189696b7973eSSagi Grimberg 
189796b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
189896b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
189996b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
190096b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
190196b7973eSSagi Grimberg 	}
190296b7973eSSagi Grimberg 
1903f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1904e622f2f4SChristoph Hellwig 	wr->rdma_wr_num = 0;
19059e961ae7SSagi Grimberg 	if (ret)
19069e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
19079e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
19089e961ae7SSagi Grimberg 	else
1909f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1910f93f3a70SSagi Grimberg }
1911f93f3a70SSagi Grimberg 
1912f93f3a70SSagi Grimberg static void
1913b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1914b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1915b8d26b3bSNicholas Bellinger {
1916b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1917d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1918b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
191990ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1920dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
19215bac4b1aSSagi Grimberg 	int ret = 0;
1922b8d26b3bSNicholas Bellinger 
19239e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
192496b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
192596b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
192696b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
19279e961ae7SSagi Grimberg 	}
19289e961ae7SSagi Grimberg 
1929b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1930d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1931e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1932e622f2f4SChristoph Hellwig 	wr->rdma_wr_num = 0;
1933b8d26b3bSNicholas Bellinger 
193424f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1935b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1936b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1937b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1938b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1939b8d26b3bSNicholas Bellinger 
1940364189f0SSagi Grimberg 	if (ret) {
1941afc16604SBart Van Assche 		target_put_sess_cmd(se_cmd);
19425bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
19435bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
1944364189f0SSagi Grimberg 	} else {
1945b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1946b8d26b3bSNicholas Bellinger 	}
1947364189f0SSagi Grimberg }
1948b8d26b3bSNicholas Bellinger 
1949b8d26b3bSNicholas Bellinger static void
1950b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1951b8d26b3bSNicholas Bellinger {
1952b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1953b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1954b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1955dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1956d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1957b8d26b3bSNicholas Bellinger 
19584c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
19594c22e07fSSagi Grimberg 
1960b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1961b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1962b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
196310633c37SSagi Grimberg 	case ISTATE_SEND_REJECT:   /* FALLTHRU */
196410633c37SSagi Grimberg 	case ISTATE_SEND_TEXTRSP:  /* FALLTHRU */
1965b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
19664c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
19674c22e07fSSagi Grimberg 				     ib_dev, false);
19683df8f68aSNicholas Bellinger 		break;
1969b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1970b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1971b8d26b3bSNicholas Bellinger 		break;
1972b8d26b3bSNicholas Bellinger 	default:
19734c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1974b8d26b3bSNicholas Bellinger 		dump_stack();
1975b8d26b3bSNicholas Bellinger 		break;
1976b8d26b3bSNicholas Bellinger 	}
1977b8d26b3bSNicholas Bellinger }
1978b8d26b3bSNicholas Bellinger 
1979b8d26b3bSNicholas Bellinger static void
1980b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1981b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1982b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1983b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1984b8d26b3bSNicholas Bellinger {
1985d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1986b8d26b3bSNicholas Bellinger 
1987b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
19883df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1989adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1990adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1991b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1992b8d26b3bSNicholas Bellinger 
1993b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1994b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1995b8d26b3bSNicholas Bellinger 		return;
1996b8d26b3bSNicholas Bellinger 	}
1997897bb2c9SSagi Grimberg 
1998b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
199903e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
2000b8d26b3bSNicholas Bellinger }
2001b8d26b3bSNicholas Bellinger 
2002b8d26b3bSNicholas Bellinger static void
20037748681bSSagi Grimberg isert_snd_completion(struct iser_tx_desc *tx_desc,
2004b8d26b3bSNicholas Bellinger 		      struct isert_conn *isert_conn)
2005b8d26b3bSNicholas Bellinger {
2006dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
2007b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2008b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
2009b8d26b3bSNicholas Bellinger 
2010b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
2011b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
2012b8d26b3bSNicholas Bellinger 		return;
2013b8d26b3bSNicholas Bellinger 	}
2014b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
2015b8d26b3bSNicholas Bellinger 
20164c22e07fSSagi Grimberg 	isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
20174c22e07fSSagi Grimberg 
2018b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
2019b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
2020b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
2021b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
2022b8d26b3bSNicholas Bellinger 		break;
2023b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
2024f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
2025b8d26b3bSNicholas Bellinger 		break;
2026b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
2027b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
2028b8d26b3bSNicholas Bellinger 		break;
2029b8d26b3bSNicholas Bellinger 	default:
20304c22e07fSSagi Grimberg 		isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
2031b8d26b3bSNicholas Bellinger 		dump_stack();
2032b8d26b3bSNicholas Bellinger 		break;
2033b8d26b3bSNicholas Bellinger 	}
2034b8d26b3bSNicholas Bellinger }
2035b8d26b3bSNicholas Bellinger 
20366f0fae3dSSagi Grimberg /**
20376f0fae3dSSagi Grimberg  * is_isert_tx_desc() - Indicate if the completion wr_id
20386f0fae3dSSagi Grimberg  *     is a TX descriptor or not.
20396f0fae3dSSagi Grimberg  * @isert_conn: iser connection
20406f0fae3dSSagi Grimberg  * @wr_id: completion WR identifier
20416f0fae3dSSagi Grimberg  *
20426f0fae3dSSagi Grimberg  * Since we cannot rely on wc opcode in FLUSH errors
20436f0fae3dSSagi Grimberg  * we must work around it by checking if the wr_id address
20446f0fae3dSSagi Grimberg  * falls in the iser connection rx_descs buffer. If so
20456f0fae3dSSagi Grimberg  * it is an RX descriptor, otherwize it is a TX.
20466f0fae3dSSagi Grimberg  */
20476f0fae3dSSagi Grimberg static inline bool
20486f0fae3dSSagi Grimberg is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
2049b8d26b3bSNicholas Bellinger {
2050dac6ab30SSagi Grimberg 	void *start = isert_conn->rx_descs;
2051dac6ab30SSagi Grimberg 	int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
20526f0fae3dSSagi Grimberg 
2053b89a7c25SJenny Derzhavetz 	if ((wr_id >= start && wr_id < start + len) ||
2054b89a7c25SJenny Derzhavetz 	    (wr_id == isert_conn->login_req_buf))
20556f0fae3dSSagi Grimberg 		return false;
20566f0fae3dSSagi Grimberg 
20576f0fae3dSSagi Grimberg 	return true;
20586f0fae3dSSagi Grimberg }
20596f0fae3dSSagi Grimberg 
20606f0fae3dSSagi Grimberg static void
20616f0fae3dSSagi Grimberg isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
20626f0fae3dSSagi Grimberg {
2063bdf20e72SSagi Grimberg 	if (wc->wr_id == ISER_BEACON_WRID) {
2064dac6ab30SSagi Grimberg 		isert_info("conn %p completing wait_comp_err\n",
2065bdf20e72SSagi Grimberg 			   isert_conn);
2066dac6ab30SSagi Grimberg 		complete(&isert_conn->wait_comp_err);
2067ed4520aeSNicholas Bellinger 	} else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
2068dac6ab30SSagi Grimberg 		struct ib_device *ib_dev = isert_conn->cm_id->device;
2069df43debdSSagi Grimberg 		struct isert_cmd *isert_cmd;
20706f0fae3dSSagi Grimberg 		struct iser_tx_desc *desc;
2071b8d26b3bSNicholas Bellinger 
20726f0fae3dSSagi Grimberg 		desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20736f0fae3dSSagi Grimberg 		isert_cmd = desc->isert_cmd;
2074b8d26b3bSNicholas Bellinger 		if (!isert_cmd)
2075df43debdSSagi Grimberg 			isert_unmap_tx_desc(desc, ib_dev);
2076b8d26b3bSNicholas Bellinger 		else
2077df43debdSSagi Grimberg 			isert_completion_put(desc, isert_cmd, ib_dev, true);
2078b8d26b3bSNicholas Bellinger 	}
2079df43debdSSagi Grimberg }
2080b8d26b3bSNicholas Bellinger 
2081b8d26b3bSNicholas Bellinger static void
20826f0fae3dSSagi Grimberg isert_handle_wc(struct ib_wc *wc)
2083b8d26b3bSNicholas Bellinger {
2084b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2085b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
20866f0fae3dSSagi Grimberg 	struct iser_rx_desc *rx_desc;
2087b8d26b3bSNicholas Bellinger 
20886f0fae3dSSagi Grimberg 	isert_conn = wc->qp->qp_context;
20896f0fae3dSSagi Grimberg 	if (likely(wc->status == IB_WC_SUCCESS)) {
20906f0fae3dSSagi Grimberg 		if (wc->opcode == IB_WC_RECV) {
20916f0fae3dSSagi Grimberg 			rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
20927748681bSSagi Grimberg 			isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
2093b8d26b3bSNicholas Bellinger 		} else {
20946f0fae3dSSagi Grimberg 			tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20957748681bSSagi Grimberg 			isert_snd_completion(tx_desc, isert_conn);
2096b8d26b3bSNicholas Bellinger 		}
20976f0fae3dSSagi Grimberg 	} else {
20986f0fae3dSSagi Grimberg 		if (wc->status != IB_WC_WR_FLUSH_ERR)
2099ea8a1616SSagi Grimberg 			isert_err("%s (%d): wr id %llx vend_err %x\n",
2100ea8a1616SSagi Grimberg 				  ib_wc_status_msg(wc->status), wc->status,
2101ea8a1616SSagi Grimberg 				  wc->wr_id, wc->vendor_err);
21026f0fae3dSSagi Grimberg 		else
2103ea8a1616SSagi Grimberg 			isert_dbg("%s (%d): wr id %llx\n",
2104ea8a1616SSagi Grimberg 				  ib_wc_status_msg(wc->status), wc->status,
2105ea8a1616SSagi Grimberg 				  wc->wr_id);
2106b8d26b3bSNicholas Bellinger 
21076f0fae3dSSagi Grimberg 		if (wc->wr_id != ISER_FASTREG_LI_WRID)
21086f0fae3dSSagi Grimberg 			isert_cq_comp_err(isert_conn, wc);
21096f0fae3dSSagi Grimberg 	}
2110b8d26b3bSNicholas Bellinger }
2111b8d26b3bSNicholas Bellinger 
2112b8d26b3bSNicholas Bellinger static void
21136f0fae3dSSagi Grimberg isert_cq_work(struct work_struct *work)
2114b8d26b3bSNicholas Bellinger {
211537d9fe80SSagi Grimberg 	enum { isert_poll_budget = 65536 };
21164a295baeSSagi Grimberg 	struct isert_comp *comp = container_of(work, struct isert_comp,
21176f0fae3dSSagi Grimberg 					       work);
211836ea63b5SSagi Grimberg 	struct ib_wc *const wcs = comp->wcs;
211936ea63b5SSagi Grimberg 	int i, n, completed = 0;
2120b8d26b3bSNicholas Bellinger 
212136ea63b5SSagi Grimberg 	while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
212236ea63b5SSagi Grimberg 		for (i = 0; i < n; i++)
212336ea63b5SSagi Grimberg 			isert_handle_wc(&wcs[i]);
2124b8d26b3bSNicholas Bellinger 
212536ea63b5SSagi Grimberg 		completed += n;
212636ea63b5SSagi Grimberg 		if (completed >= isert_poll_budget)
212737d9fe80SSagi Grimberg 			break;
212837d9fe80SSagi Grimberg 	}
212937d9fe80SSagi Grimberg 
21306f0fae3dSSagi Grimberg 	ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
2131b8d26b3bSNicholas Bellinger }
2132b8d26b3bSNicholas Bellinger 
2133b8d26b3bSNicholas Bellinger static void
21346f0fae3dSSagi Grimberg isert_cq_callback(struct ib_cq *cq, void *context)
2135b8d26b3bSNicholas Bellinger {
21364a295baeSSagi Grimberg 	struct isert_comp *comp = context;
2137b8d26b3bSNicholas Bellinger 
21386f0fae3dSSagi Grimberg 	queue_work(isert_comp_wq, &comp->work);
2139b8d26b3bSNicholas Bellinger }
2140b8d26b3bSNicholas Bellinger 
2141b8d26b3bSNicholas Bellinger static int
2142b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2143b8d26b3bSNicholas Bellinger {
2144b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2145b8d26b3bSNicholas Bellinger 	int ret;
2146b8d26b3bSNicholas Bellinger 
21474366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
21484366b19cSJenny Derzhavetz 	if (ret) {
21494366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", ret);
21504366b19cSJenny Derzhavetz 		return ret;
21514366b19cSJenny Derzhavetz 	}
21524366b19cSJenny Derzhavetz 
2153dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
2154b8d26b3bSNicholas Bellinger 			   &wr_failed);
2155b8d26b3bSNicholas Bellinger 	if (ret) {
215624f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
2157b8d26b3bSNicholas Bellinger 		return ret;
2158b8d26b3bSNicholas Bellinger 	}
2159b8d26b3bSNicholas Bellinger 	return ret;
2160b8d26b3bSNicholas Bellinger }
2161b8d26b3bSNicholas Bellinger 
2162b8d26b3bSNicholas Bellinger static int
2163b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2164b8d26b3bSNicholas Bellinger {
2165d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
21666700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2167b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2168b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2169b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2170b8d26b3bSNicholas Bellinger 
2171b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2172b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2173b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2174b8d26b3bSNicholas Bellinger 	/*
2175b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2176b8d26b3bSNicholas Bellinger 	 */
2177b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2178b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2179b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2180dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
218167cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2182b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2183dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2184b8d26b3bSNicholas Bellinger 
2185b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2186b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2187b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2188b8d26b3bSNicholas Bellinger 
2189b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2190b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2191dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2192b8d26b3bSNicholas Bellinger 
2193dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2194dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2195b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2196b8d26b3bSNicholas Bellinger 
2197dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2198dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2199dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
220034efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
2201b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2202b8d26b3bSNicholas Bellinger 	}
2203b8d26b3bSNicholas Bellinger 
220468a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2205b8d26b3bSNicholas Bellinger 
22064c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
2207b8d26b3bSNicholas Bellinger 
2208b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2209b8d26b3bSNicholas Bellinger }
2210b8d26b3bSNicholas Bellinger 
2211131e6abcSNicholas Bellinger static void
2212131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2213131e6abcSNicholas Bellinger {
2214131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22156700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2216dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
2217131e6abcSNicholas Bellinger 
2218131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2219131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2220131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2221131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2222131e6abcSNicholas Bellinger 
2223131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2224131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2225131e6abcSNicholas Bellinger 
2226131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2227131e6abcSNicholas Bellinger }
2228131e6abcSNicholas Bellinger 
2229e70beee7SNicholas Bellinger static enum target_prot_op
2230e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2231e70beee7SNicholas Bellinger {
22326700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2233dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
2234e70beee7SNicholas Bellinger 
223523a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
223623a548eeSSagi Grimberg 		if (device->pi_capable) {
223724f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
223823a548eeSSagi Grimberg 			isert_conn->pi_support = true;
2239e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
224023a548eeSSagi Grimberg 		}
224123a548eeSSagi Grimberg 	}
224223a548eeSSagi Grimberg 
224324f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
224423a548eeSSagi Grimberg 	isert_conn->pi_support = false;
2245e70beee7SNicholas Bellinger 
2246e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2247e70beee7SNicholas Bellinger }
2248e70beee7SNicholas Bellinger 
2249b8d26b3bSNicholas Bellinger static int
2250b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2251b8d26b3bSNicholas Bellinger 		bool nopout_response)
2252b8d26b3bSNicholas Bellinger {
2253d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22546700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2255b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2256b8d26b3bSNicholas Bellinger 
2257b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2258b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2259b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2260b8d26b3bSNicholas Bellinger 			       nopout_response);
2261b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
226268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2263b8d26b3bSNicholas Bellinger 
22644c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
2265b8d26b3bSNicholas Bellinger 
2266b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2267b8d26b3bSNicholas Bellinger }
2268b8d26b3bSNicholas Bellinger 
2269b8d26b3bSNicholas Bellinger static int
2270b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2271b8d26b3bSNicholas Bellinger {
2272d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22736700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2274b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2275b8d26b3bSNicholas Bellinger 
2276b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2277b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2278b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2279b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
228068a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2281b8d26b3bSNicholas Bellinger 
22824c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
2283b8d26b3bSNicholas Bellinger 
2284b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2285b8d26b3bSNicholas Bellinger }
2286b8d26b3bSNicholas Bellinger 
2287b8d26b3bSNicholas Bellinger static int
2288b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2289b8d26b3bSNicholas Bellinger {
2290d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22916700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2292b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2293b8d26b3bSNicholas Bellinger 
2294b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2295b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2296b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2297b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
229868a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2299b8d26b3bSNicholas Bellinger 
23004c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
2301b8d26b3bSNicholas Bellinger 
2302b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2303b8d26b3bSNicholas Bellinger }
2304b8d26b3bSNicholas Bellinger 
2305b8d26b3bSNicholas Bellinger static int
2306b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2307b8d26b3bSNicholas Bellinger {
2308d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
23096700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2310b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2311dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
231267cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
23133df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
23143df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
23153df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2316b8d26b3bSNicholas Bellinger 
2317b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
23183df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2319b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
23203df8f68aSNicholas Bellinger 
23213df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2322dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
23233df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
23243df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2325dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2326dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
23273df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
232834efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
23293df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
23303df8f68aSNicholas Bellinger 
233168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2332b8d26b3bSNicholas Bellinger 
23334c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
2334b8d26b3bSNicholas Bellinger 
2335b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2336b8d26b3bSNicholas Bellinger }
2337b8d26b3bSNicholas Bellinger 
2338b8d26b3bSNicholas Bellinger static int
2339adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2340adb54c29SNicholas Bellinger {
2341d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
23426700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2343adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2344adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2345adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2346adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2347adb54c29SNicholas Bellinger 	int rc;
2348adb54c29SNicholas Bellinger 
2349adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
235022c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2351adb54c29SNicholas Bellinger 	if (rc < 0)
2352adb54c29SNicholas Bellinger 		return rc;
2353adb54c29SNicholas Bellinger 
2354adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2355adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2356adb54c29SNicholas Bellinger 
2357adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2358dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
235967cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2360adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2361adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2362adb54c29SNicholas Bellinger 
2363adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2364adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2365adb54c29SNicholas Bellinger 
2366adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2367adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2368adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
236934efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
2370adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2371adb54c29SNicholas Bellinger 	}
237268a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2373adb54c29SNicholas Bellinger 
2374f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
2375adb54c29SNicholas Bellinger 
2376adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2377adb54c29SNicholas Bellinger }
2378adb54c29SNicholas Bellinger 
2379adb54c29SNicholas Bellinger static int
2380b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2381e622f2f4SChristoph Hellwig 		    struct ib_sge *ib_sge, struct ib_rdma_wr *rdma_wr,
2382b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2383b8d26b3bSNicholas Bellinger {
2384d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2385b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2386dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
238767cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
2388b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2389b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2390b8d26b3bSNicholas Bellinger 
2391b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2392b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2393b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2394b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2395b8d26b3bSNicholas Bellinger 
2396e622f2f4SChristoph Hellwig 	rdma_wr->wr.sg_list = ib_sge;
2397e622f2f4SChristoph Hellwig 	rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
2398b8d26b3bSNicholas Bellinger 	/*
2399b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2400b8d26b3bSNicholas Bellinger 	 */
2401b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
24024c22e07fSSagi Grimberg 		isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
24034c22e07fSSagi Grimberg 			  "page_off: %u\n",
2404b8d26b3bSNicholas Bellinger 			  (unsigned long long)tmp_sg->dma_address,
2405b8d26b3bSNicholas Bellinger 			  tmp_sg->length, page_off);
2406b8d26b3bSNicholas Bellinger 
2407b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2408b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2409b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
241034efc7dfSJason Gunthorpe 		ib_sge->lkey = device->pd->local_dma_lkey;
2411b8d26b3bSNicholas Bellinger 
24124c22e07fSSagi Grimberg 		isert_dbg("RDMA ib_sge: addr: 0x%llx  length: %u lkey: %x\n",
241390ecc6e2SVu Pham 			  ib_sge->addr, ib_sge->length, ib_sge->lkey);
2414b8d26b3bSNicholas Bellinger 		page_off = 0;
2415b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
24169253e667SSagi Grimberg 		if (!data_left)
24179253e667SSagi Grimberg 			break;
2418b8d26b3bSNicholas Bellinger 		ib_sge++;
241924f412ddSSagi Grimberg 		isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
2420b8d26b3bSNicholas Bellinger 	}
2421b8d26b3bSNicholas Bellinger 
2422e622f2f4SChristoph Hellwig 	rdma_wr->wr.num_sge = ++i;
242324f412ddSSagi Grimberg 	isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2424e622f2f4SChristoph Hellwig 		  rdma_wr->wr.sg_list, rdma_wr->wr.num_sge);
2425b8d26b3bSNicholas Bellinger 
2426e622f2f4SChristoph Hellwig 	return rdma_wr->wr.num_sge;
2427b8d26b3bSNicholas Bellinger }
2428b8d26b3bSNicholas Bellinger 
2429b8d26b3bSNicholas Bellinger static int
243090ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
243190ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2432b8d26b3bSNicholas Bellinger {
2433b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2434d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
24356700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2436e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
2437e622f2f4SChristoph Hellwig 	struct ib_rdma_wr *rdma_wr;
2438b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2439e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2440e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2441b8d26b3bSNicholas Bellinger 
244290ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2443b8d26b3bSNicholas Bellinger 
2444e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2445e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2446e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2447e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2448e3d7e4c3SSagi Grimberg 	if (ret)
2449e3d7e4c3SSagi Grimberg 		return ret;
2450b8d26b3bSNicholas Bellinger 
2451e3d7e4c3SSagi Grimberg 	data_left = data->len;
2452e3d7e4c3SSagi Grimberg 	offset = data->offset;
2453b8d26b3bSNicholas Bellinger 
2454e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2455b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
245624f412ddSSagi Grimberg 		isert_warn("Unable to allocate ib_sge\n");
2457b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2458e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2459b8d26b3bSNicholas Bellinger 	}
246090ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2461b8d26b3bSNicholas Bellinger 
2462e622f2f4SChristoph Hellwig 	wr->rdma_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2463e622f2f4SChristoph Hellwig 	wr->rdma_wr = kzalloc(sizeof(struct ib_rdma_wr) * wr->rdma_wr_num,
2464b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2465e622f2f4SChristoph Hellwig 	if (!wr->rdma_wr) {
2466e622f2f4SChristoph Hellwig 		isert_dbg("Unable to allocate wr->rdma_wr\n");
2467b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2468e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2469b8d26b3bSNicholas Bellinger 	}
2470b8d26b3bSNicholas Bellinger 
2471b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2472b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2473b8d26b3bSNicholas Bellinger 
2474e622f2f4SChristoph Hellwig 	for (i = 0; i < wr->rdma_wr_num; i++) {
2475e622f2f4SChristoph Hellwig 		rdma_wr = &isert_cmd->rdma_wr.rdma_wr[i];
2476b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2477b8d26b3bSNicholas Bellinger 
2478e622f2f4SChristoph Hellwig 		rdma_wr->wr.send_flags = 0;
247990ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2480e622f2f4SChristoph Hellwig 			rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2481e622f2f4SChristoph Hellwig 			rdma_wr->remote_addr = isert_cmd->read_va + offset;
2482e622f2f4SChristoph Hellwig 			rdma_wr->rkey = isert_cmd->read_stag;
2483e622f2f4SChristoph Hellwig 			if (i + 1 == wr->rdma_wr_num)
2484e622f2f4SChristoph Hellwig 				rdma_wr->wr.next = &isert_cmd->tx_desc.send_wr;
248590ecc6e2SVu Pham 			else
2486e622f2f4SChristoph Hellwig 				rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
248790ecc6e2SVu Pham 		} else {
2488e622f2f4SChristoph Hellwig 			rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2489e622f2f4SChristoph Hellwig 			rdma_wr->remote_addr = isert_cmd->write_va + va_offset;
2490e622f2f4SChristoph Hellwig 			rdma_wr->rkey = isert_cmd->write_stag;
2491e622f2f4SChristoph Hellwig 			if (i + 1 == wr->rdma_wr_num)
2492e622f2f4SChristoph Hellwig 				rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
249390ecc6e2SVu Pham 			else
2494e622f2f4SChristoph Hellwig 				rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
249590ecc6e2SVu Pham 		}
2496b8d26b3bSNicholas Bellinger 
2497b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2498e622f2f4SChristoph Hellwig 					rdma_wr, data_len, offset);
2499b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2500b8d26b3bSNicholas Bellinger 
2501b8d26b3bSNicholas Bellinger 		offset += data_len;
250290ecc6e2SVu Pham 		va_offset += data_len;
2503b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2504b8d26b3bSNicholas Bellinger 	}
250590ecc6e2SVu Pham 
250690ecc6e2SVu Pham 	return 0;
2507e3d7e4c3SSagi Grimberg unmap_cmd:
2508e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2509e3d7e4c3SSagi Grimberg 
251090ecc6e2SVu Pham 	return ret;
251190ecc6e2SVu Pham }
251290ecc6e2SVu Pham 
251310633c37SSagi Grimberg static inline void
251410633c37SSagi Grimberg isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
251510633c37SSagi Grimberg {
251610633c37SSagi Grimberg 	u32 rkey;
251710633c37SSagi Grimberg 
251810633c37SSagi Grimberg 	memset(inv_wr, 0, sizeof(*inv_wr));
251910633c37SSagi Grimberg 	inv_wr->wr_id = ISER_FASTREG_LI_WRID;
252010633c37SSagi Grimberg 	inv_wr->opcode = IB_WR_LOCAL_INV;
252110633c37SSagi Grimberg 	inv_wr->ex.invalidate_rkey = mr->rkey;
252210633c37SSagi Grimberg 
252310633c37SSagi Grimberg 	/* Bump the key */
252410633c37SSagi Grimberg 	rkey = ib_inc_rkey(mr->rkey);
252510633c37SSagi Grimberg 	ib_update_fast_reg_key(mr, rkey);
252610633c37SSagi Grimberg }
252710633c37SSagi Grimberg 
252859464ef4SVu Pham static int
2529e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2530e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2531e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
25329e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2533e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
253459464ef4SVu Pham {
2535dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
253667cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
25379e961ae7SSagi Grimberg 	struct ib_mr *mr;
253816c2d702SSagi Grimberg 	struct ib_reg_wr reg_wr;
2539e622f2f4SChristoph Hellwig 	struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
254016c2d702SSagi Grimberg 	int ret, n;
254159464ef4SVu Pham 
2542e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
254334efc7dfSJason Gunthorpe 		sge->lkey = device->pd->local_dma_lkey;
2544e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2545e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
25464c22e07fSSagi Grimberg 		isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
25474c22e07fSSagi Grimberg 			 sge->addr, sge->length, sge->lkey);
2548e3d7e4c3SSagi Grimberg 		return 0;
2549e3d7e4c3SSagi Grimberg 	}
2550e3d7e4c3SSagi Grimberg 
255116c2d702SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID)
25529e961ae7SSagi Grimberg 		/* Registering data buffer */
25539e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
255416c2d702SSagi Grimberg 	else
25559e961ae7SSagi Grimberg 		/* Registering protection buffer */
25569e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
255759464ef4SVu Pham 
255810633c37SSagi Grimberg 	if (!(fr_desc->ind & ind)) {
255910633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, mr);
256059464ef4SVu Pham 		wr = &inv_wr;
256159464ef4SVu Pham 	}
256259464ef4SVu Pham 
256316c2d702SSagi Grimberg 	n = ib_map_mr_sg(mr, mem->sg, mem->nents, PAGE_SIZE);
256416c2d702SSagi Grimberg 	if (unlikely(n != mem->nents)) {
256516c2d702SSagi Grimberg 		isert_err("failed to map mr sg (%d/%d)\n",
256616c2d702SSagi Grimberg 			 n, mem->nents);
256716c2d702SSagi Grimberg 		return n < 0 ? n : -EINVAL;
256816c2d702SSagi Grimberg 	}
256916c2d702SSagi Grimberg 
257016c2d702SSagi Grimberg 	isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
257116c2d702SSagi Grimberg 		  fr_desc, mem->nents, mem->offset);
257216c2d702SSagi Grimberg 
257316c2d702SSagi Grimberg 	reg_wr.wr.next = NULL;
257416c2d702SSagi Grimberg 	reg_wr.wr.opcode = IB_WR_REG_MR;
257516c2d702SSagi Grimberg 	reg_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
257616c2d702SSagi Grimberg 	reg_wr.wr.send_flags = 0;
257716c2d702SSagi Grimberg 	reg_wr.wr.num_sge = 0;
257816c2d702SSagi Grimberg 	reg_wr.mr = mr;
257916c2d702SSagi Grimberg 	reg_wr.key = mr->lkey;
258016c2d702SSagi Grimberg 	reg_wr.access = IB_ACCESS_LOCAL_WRITE;
258159464ef4SVu Pham 
258259464ef4SVu Pham 	if (!wr)
258316c2d702SSagi Grimberg 		wr = &reg_wr.wr;
258459464ef4SVu Pham 	else
258516c2d702SSagi Grimberg 		wr->next = &reg_wr.wr;
258659464ef4SVu Pham 
2587dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
258859464ef4SVu Pham 	if (ret) {
258924f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
259059464ef4SVu Pham 		return ret;
259159464ef4SVu Pham 	}
25929e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
259359464ef4SVu Pham 
25949e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
259516c2d702SSagi Grimberg 	sge->addr = mr->iova;
259616c2d702SSagi Grimberg 	sge->length = mr->length;
259759464ef4SVu Pham 
25984c22e07fSSagi Grimberg 	isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
25994c22e07fSSagi Grimberg 		  sge->addr, sge->length, sge->lkey);
260059464ef4SVu Pham 
260159464ef4SVu Pham 	return ret;
260259464ef4SVu Pham }
260359464ef4SVu Pham 
26043d73cf1aSSagi Grimberg static inline void
26053d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
26063d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
26079e961ae7SSagi Grimberg {
260878eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
26093d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
26103d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
26113d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
261278eda2bbSSagi Grimberg 	/*
261378eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
261478eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
261578eda2bbSSagi Grimberg 	 * from se_cmd.
261678eda2bbSSagi Grimberg 	 */
261778eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
261878eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
261978eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
262078eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
262178eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
262278eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
26233d73cf1aSSagi Grimberg };
26243d73cf1aSSagi Grimberg 
26253d73cf1aSSagi Grimberg static int
26263d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
26273d73cf1aSSagi Grimberg {
26289e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
26299e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
26309e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
263178eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
26323d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
26339e961ae7SSagi Grimberg 		break;
26349e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
26359e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
263678eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
26373d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
26389e961ae7SSagi Grimberg 		break;
26399e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
26409e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
26413d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
26423d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
26439e961ae7SSagi Grimberg 		break;
26449e961ae7SSagi Grimberg 	default:
264524f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
26469e961ae7SSagi Grimberg 		return -EINVAL;
26479e961ae7SSagi Grimberg 	}
26489e961ae7SSagi Grimberg 
26499e961ae7SSagi Grimberg 	return 0;
26509e961ae7SSagi Grimberg }
26519e961ae7SSagi Grimberg 
26529e961ae7SSagi Grimberg static inline u8
26539e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
26549e961ae7SSagi Grimberg {
26559e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
26569e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
26579e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
26589e961ae7SSagi Grimberg }
26599e961ae7SSagi Grimberg 
26609e961ae7SSagi Grimberg static int
2661570db170SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn,
2662570db170SSagi Grimberg 		 struct se_cmd *se_cmd,
2663570db170SSagi Grimberg 		 struct isert_rdma_wr *rdma_wr,
2664570db170SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc)
26659e961ae7SSagi Grimberg {
2666e622f2f4SChristoph Hellwig 	struct ib_sig_handover_wr sig_wr;
2667e622f2f4SChristoph Hellwig 	struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
26689e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
26699e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
26709e961ae7SSagi Grimberg 	int ret;
26719e961ae7SSagi Grimberg 
26729e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
26739e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
26749e961ae7SSagi Grimberg 	if (ret)
26759e961ae7SSagi Grimberg 		goto err;
26769e961ae7SSagi Grimberg 
26779e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
26789e961ae7SSagi Grimberg 
26799e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
268010633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
26819e961ae7SSagi Grimberg 		wr = &inv_wr;
26829e961ae7SSagi Grimberg 	}
26839e961ae7SSagi Grimberg 
26849e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
2685e622f2f4SChristoph Hellwig 	sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
2686e622f2f4SChristoph Hellwig 	sig_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
2687e622f2f4SChristoph Hellwig 	sig_wr.wr.sg_list = &rdma_wr->ib_sg[DATA];
2688e622f2f4SChristoph Hellwig 	sig_wr.wr.num_sge = 1;
2689e622f2f4SChristoph Hellwig 	sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
2690e622f2f4SChristoph Hellwig 	sig_wr.sig_attrs = &sig_attrs;
2691e622f2f4SChristoph Hellwig 	sig_wr.sig_mr = pi_ctx->sig_mr;
26929e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
2693e622f2f4SChristoph Hellwig 		sig_wr.prot = &rdma_wr->ib_sg[PROT];
26949e961ae7SSagi Grimberg 
26959e961ae7SSagi Grimberg 	if (!wr)
2696e622f2f4SChristoph Hellwig 		wr = &sig_wr.wr;
26979e961ae7SSagi Grimberg 	else
2698e622f2f4SChristoph Hellwig 		wr->next = &sig_wr.wr;
26999e961ae7SSagi Grimberg 
2700dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
27019e961ae7SSagi Grimberg 	if (ret) {
270224f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
27039e961ae7SSagi Grimberg 		goto err;
27049e961ae7SSagi Grimberg 	}
27059e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
27069e961ae7SSagi Grimberg 
2707570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2708570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].addr = 0;
2709570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
27109e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
27119e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
27129e961ae7SSagi Grimberg 		/*
27139e961ae7SSagi Grimberg 		 * We have protection guards on the wire
27149e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
27159e961ae7SSagi Grimberg 		 */
2716570db170SSagi Grimberg 		rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
27179e961ae7SSagi Grimberg 
271824f412ddSSagi Grimberg 	isert_dbg("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
2719570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2720570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].lkey);
27219e961ae7SSagi Grimberg err:
27229e961ae7SSagi Grimberg 	return ret;
27239e961ae7SSagi Grimberg }
27249e961ae7SSagi Grimberg 
272559464ef4SVu Pham static int
2726570db170SSagi Grimberg isert_handle_prot_cmd(struct isert_conn *isert_conn,
2727570db170SSagi Grimberg 		      struct isert_cmd *isert_cmd,
2728570db170SSagi Grimberg 		      struct isert_rdma_wr *wr)
2729570db170SSagi Grimberg {
2730dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
2731570db170SSagi Grimberg 	struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2732570db170SSagi Grimberg 	int ret;
2733570db170SSagi Grimberg 
2734570db170SSagi Grimberg 	if (!wr->fr_desc->pi_ctx) {
2735570db170SSagi Grimberg 		ret = isert_create_pi_ctx(wr->fr_desc,
2736570db170SSagi Grimberg 					  device->ib_device,
273767cb3949SSagi Grimberg 					  device->pd);
2738570db170SSagi Grimberg 		if (ret) {
273924f412ddSSagi Grimberg 			isert_err("conn %p failed to allocate pi_ctx\n",
2740570db170SSagi Grimberg 				  isert_conn);
2741570db170SSagi Grimberg 			return ret;
2742570db170SSagi Grimberg 		}
2743570db170SSagi Grimberg 	}
2744570db170SSagi Grimberg 
2745570db170SSagi Grimberg 	if (se_cmd->t_prot_sg) {
2746570db170SSagi Grimberg 		ret = isert_map_data_buf(isert_conn, isert_cmd,
2747570db170SSagi Grimberg 					 se_cmd->t_prot_sg,
2748570db170SSagi Grimberg 					 se_cmd->t_prot_nents,
2749570db170SSagi Grimberg 					 se_cmd->prot_length,
2750570db170SSagi Grimberg 					 0, wr->iser_ib_op, &wr->prot);
2751570db170SSagi Grimberg 		if (ret) {
275224f412ddSSagi Grimberg 			isert_err("conn %p failed to map protection buffer\n",
2753570db170SSagi Grimberg 				  isert_conn);
2754570db170SSagi Grimberg 			return ret;
2755570db170SSagi Grimberg 		}
2756570db170SSagi Grimberg 
2757570db170SSagi Grimberg 		memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2758570db170SSagi Grimberg 		ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2759570db170SSagi Grimberg 					ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2760570db170SSagi Grimberg 		if (ret) {
276124f412ddSSagi Grimberg 			isert_err("conn %p failed to fast reg mr\n",
2762570db170SSagi Grimberg 				  isert_conn);
2763570db170SSagi Grimberg 			goto unmap_prot_cmd;
2764570db170SSagi Grimberg 		}
2765570db170SSagi Grimberg 	}
2766570db170SSagi Grimberg 
2767570db170SSagi Grimberg 	ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2768570db170SSagi Grimberg 	if (ret) {
276924f412ddSSagi Grimberg 		isert_err("conn %p failed to fast reg mr\n",
2770570db170SSagi Grimberg 			  isert_conn);
2771570db170SSagi Grimberg 		goto unmap_prot_cmd;
2772570db170SSagi Grimberg 	}
2773570db170SSagi Grimberg 	wr->fr_desc->ind |= ISERT_PROTECTED;
2774570db170SSagi Grimberg 
2775570db170SSagi Grimberg 	return 0;
2776570db170SSagi Grimberg 
2777570db170SSagi Grimberg unmap_prot_cmd:
2778570db170SSagi Grimberg 	if (se_cmd->t_prot_sg)
2779570db170SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2780570db170SSagi Grimberg 
2781570db170SSagi Grimberg 	return ret;
2782570db170SSagi Grimberg }
2783570db170SSagi Grimberg 
2784570db170SSagi Grimberg static int
2785a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
278659464ef4SVu Pham 	       struct isert_rdma_wr *wr)
278759464ef4SVu Pham {
278859464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
278959464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2790e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2791e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2792e622f2f4SChristoph Hellwig 	struct ib_rdma_wr *rdma_wr;
2793570db170SSagi Grimberg 	struct ib_sge *ib_sg;
2794e3d7e4c3SSagi Grimberg 	u32 offset;
2795e3d7e4c3SSagi Grimberg 	int ret = 0;
279659464ef4SVu Pham 	unsigned long flags;
279759464ef4SVu Pham 
279859464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2799e3d7e4c3SSagi Grimberg 
2800e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2801e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2802e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2803e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2804e3d7e4c3SSagi Grimberg 	if (ret)
2805e3d7e4c3SSagi Grimberg 		return ret;
2806e3d7e4c3SSagi Grimberg 
2807302cc7c3SSagi Grimberg 	if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
2808dac6ab30SSagi Grimberg 		spin_lock_irqsave(&isert_conn->pool_lock, flags);
2809dac6ab30SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->fr_pool,
2810e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2811e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2812dac6ab30SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
2813e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
281459464ef4SVu Pham 	}
281559464ef4SVu Pham 
28169e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2817570db170SSagi Grimberg 				ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
2818e3d7e4c3SSagi Grimberg 	if (ret)
2819e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
282059464ef4SVu Pham 
2821302cc7c3SSagi Grimberg 	if (isert_prot_cmd(isert_conn, se_cmd)) {
2822570db170SSagi Grimberg 		ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
28239e961ae7SSagi Grimberg 		if (ret)
28249e961ae7SSagi Grimberg 			goto unmap_cmd;
28259e961ae7SSagi Grimberg 
2826570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[SIG];
2827570db170SSagi Grimberg 	} else {
2828570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[DATA];
28299e961ae7SSagi Grimberg 	}
28309e961ae7SSagi Grimberg 
2831570db170SSagi Grimberg 	memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
2832e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
2833e622f2f4SChristoph Hellwig 	wr->rdma_wr_num = 1;
2834e622f2f4SChristoph Hellwig 	memset(&wr->s_rdma_wr, 0, sizeof(wr->s_rdma_wr));
2835e622f2f4SChristoph Hellwig 	wr->rdma_wr = &wr->s_rdma_wr;
283659464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
283759464ef4SVu Pham 
2838e622f2f4SChristoph Hellwig 	rdma_wr = &isert_cmd->rdma_wr.s_rdma_wr;
2839e622f2f4SChristoph Hellwig 	rdma_wr->wr.sg_list = &wr->s_ib_sge;
2840e622f2f4SChristoph Hellwig 	rdma_wr->wr.num_sge = 1;
2841e622f2f4SChristoph Hellwig 	rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
284259464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2843e622f2f4SChristoph Hellwig 		rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2844e622f2f4SChristoph Hellwig 		rdma_wr->remote_addr = isert_cmd->read_va;
2845e622f2f4SChristoph Hellwig 		rdma_wr->rkey = isert_cmd->read_stag;
2846e622f2f4SChristoph Hellwig 		rdma_wr->wr.send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
28479e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
284859464ef4SVu Pham 	} else {
2849e622f2f4SChristoph Hellwig 		rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2850e622f2f4SChristoph Hellwig 		rdma_wr->remote_addr = isert_cmd->write_va;
2851e622f2f4SChristoph Hellwig 		rdma_wr->rkey = isert_cmd->write_stag;
2852e622f2f4SChristoph Hellwig 		rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
285359464ef4SVu Pham 	}
285459464ef4SVu Pham 
285559464ef4SVu Pham 	return 0;
2856570db170SSagi Grimberg 
2857e3d7e4c3SSagi Grimberg unmap_cmd:
2858e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2859dac6ab30SSagi Grimberg 		spin_lock_irqsave(&isert_conn->pool_lock, flags);
2860dac6ab30SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2861dac6ab30SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
2862e3d7e4c3SSagi Grimberg 	}
2863e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
286459464ef4SVu Pham 
286559464ef4SVu Pham 	return ret;
286659464ef4SVu Pham }
286759464ef4SVu Pham 
286859464ef4SVu Pham static int
286990ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
287090ecc6e2SVu Pham {
287190ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
287259464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
287390ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
28746700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2875dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
287690ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
287790ecc6e2SVu Pham 	int rc;
287890ecc6e2SVu Pham 
287924f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
288090ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
28814c22e07fSSagi Grimberg 
288290ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2883d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
288490ecc6e2SVu Pham 	if (rc) {
288524f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
288690ecc6e2SVu Pham 		return rc;
288790ecc6e2SVu Pham 	}
288890ecc6e2SVu Pham 
2889302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd)) {
2890b8d26b3bSNicholas Bellinger 		/*
2891b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2892b8d26b3bSNicholas Bellinger 		 */
28939e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
28949e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
289504d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2896b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2897b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
289895b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
289968a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
2900e622f2f4SChristoph Hellwig 		isert_cmd->rdma_wr.s_rdma_wr.wr.next = &isert_cmd->tx_desc.send_wr;
2901e622f2f4SChristoph Hellwig 		wr->rdma_wr_num += 1;
29024366b19cSJenny Derzhavetz 
29034366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
29044366b19cSJenny Derzhavetz 		if (rc) {
29054366b19cSJenny Derzhavetz 			isert_err("ib_post_recv failed with %d\n", rc);
29064366b19cSJenny Derzhavetz 			return rc;
29074366b19cSJenny Derzhavetz 		}
29089e961ae7SSagi Grimberg 	}
2909b8d26b3bSNicholas Bellinger 
2910e622f2f4SChristoph Hellwig 	rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
2911bdf20e72SSagi Grimberg 	if (rc)
291224f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
29139e961ae7SSagi Grimberg 
2914302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd))
291524f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
29169e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
29179e961ae7SSagi Grimberg 	else
291824f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
291990ecc6e2SVu Pham 			 isert_cmd);
2920b8d26b3bSNicholas Bellinger 
292190ecc6e2SVu Pham 	return 1;
2922b8d26b3bSNicholas Bellinger }
2923b8d26b3bSNicholas Bellinger 
2924b8d26b3bSNicholas Bellinger static int
2925b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2926b8d26b3bSNicholas Bellinger {
2927b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2928d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2929b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
29306700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2931dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
293290ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
293390ecc6e2SVu Pham 	int rc;
2934b8d26b3bSNicholas Bellinger 
293524f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
293690ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2937b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2938d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
293990ecc6e2SVu Pham 	if (rc) {
294024f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
294190ecc6e2SVu Pham 		return rc;
2942b8d26b3bSNicholas Bellinger 	}
2943b8d26b3bSNicholas Bellinger 
2944e622f2f4SChristoph Hellwig 	rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
2945bdf20e72SSagi Grimberg 	if (rc)
294624f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2947bdf20e72SSagi Grimberg 
294824f412ddSSagi Grimberg 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
294990ecc6e2SVu Pham 		 isert_cmd);
2950b8d26b3bSNicholas Bellinger 
295190ecc6e2SVu Pham 	return 0;
2952b8d26b3bSNicholas Bellinger }
2953b8d26b3bSNicholas Bellinger 
2954b8d26b3bSNicholas Bellinger static int
2955b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2956b8d26b3bSNicholas Bellinger {
2957a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2958a4c15cd9SJenny Derzhavetz 	int ret = 0;
2959b8d26b3bSNicholas Bellinger 
2960b8d26b3bSNicholas Bellinger 	switch (state) {
2961a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2962a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2963a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2964a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2965a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2966a4c15cd9SJenny Derzhavetz 		break;
2967b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2968b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2969b8d26b3bSNicholas Bellinger 		break;
2970b8d26b3bSNicholas Bellinger 	default:
297124f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2972b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2973b8d26b3bSNicholas Bellinger 		break;
2974b8d26b3bSNicholas Bellinger 	}
2975b8d26b3bSNicholas Bellinger 
2976b8d26b3bSNicholas Bellinger 	return ret;
2977b8d26b3bSNicholas Bellinger }
2978b8d26b3bSNicholas Bellinger 
2979b8d26b3bSNicholas Bellinger static int
2980b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2981b8d26b3bSNicholas Bellinger {
2982991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2983b8d26b3bSNicholas Bellinger 	int ret;
2984b8d26b3bSNicholas Bellinger 
2985b8d26b3bSNicholas Bellinger 	switch (state) {
2986b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2987b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2988991bb764SSagi Grimberg 		if (!ret)
2989991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2990b8d26b3bSNicholas Bellinger 		break;
2991b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2992b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2993b8d26b3bSNicholas Bellinger 		break;
2994b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2995b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2996b8d26b3bSNicholas Bellinger 		break;
2997b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2998b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2999b8d26b3bSNicholas Bellinger 		break;
3000adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
3001adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
3002adb54c29SNicholas Bellinger 		break;
3003b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
3004b8d26b3bSNicholas Bellinger 		/*
3005b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
3006b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
3007b8d26b3bSNicholas Bellinger 		 */
3008b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
3009b8d26b3bSNicholas Bellinger 		break;
3010b8d26b3bSNicholas Bellinger 	default:
301124f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
3012b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
3013b8d26b3bSNicholas Bellinger 		break;
3014b8d26b3bSNicholas Bellinger 	}
3015b8d26b3bSNicholas Bellinger 
3016b8d26b3bSNicholas Bellinger 	return ret;
3017b8d26b3bSNicholas Bellinger }
3018b8d26b3bSNicholas Bellinger 
3019ca6c1d82SSagi Grimberg struct rdma_cm_id *
3020ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
3021ca6c1d82SSagi Grimberg {
3022ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
3023ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
3024ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
3025ca6c1d82SSagi Grimberg 	int ret;
3026ca6c1d82SSagi Grimberg 
3027ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
302824f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
3029ca6c1d82SSagi Grimberg 
3030fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
3031ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
3032ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
303324f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
3034ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
3035ca6c1d82SSagi Grimberg 		goto out;
3036ca6c1d82SSagi Grimberg 	}
303724f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
3038ca6c1d82SSagi Grimberg 
3039ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
3040ca6c1d82SSagi Grimberg 	if (ret) {
304124f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
3042ca6c1d82SSagi Grimberg 		goto out_id;
3043ca6c1d82SSagi Grimberg 	}
3044ca6c1d82SSagi Grimberg 
3045992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
3046ca6c1d82SSagi Grimberg 	if (ret) {
304724f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
3048ca6c1d82SSagi Grimberg 		goto out_id;
3049ca6c1d82SSagi Grimberg 	}
3050ca6c1d82SSagi Grimberg 
3051ca6c1d82SSagi Grimberg 	return id;
3052ca6c1d82SSagi Grimberg out_id:
3053ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
3054ca6c1d82SSagi Grimberg out:
3055ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
3056ca6c1d82SSagi Grimberg }
3057ca6c1d82SSagi Grimberg 
3058b8d26b3bSNicholas Bellinger static int
3059b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
306013a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
3061b8d26b3bSNicholas Bellinger {
3062b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
3063b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
3064b8d26b3bSNicholas Bellinger 	int ret;
3065b8d26b3bSNicholas Bellinger 
3066b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3067b8d26b3bSNicholas Bellinger 	if (!isert_np) {
306824f412ddSSagi Grimberg 		isert_err("Unable to allocate struct isert_np\n");
3069b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3070b8d26b3bSNicholas Bellinger 	}
3071ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
3072ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
3073bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
3074bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
3075ca6c1d82SSagi Grimberg 	isert_np->np = np;
3076b8d26b3bSNicholas Bellinger 
3077b8d26b3bSNicholas Bellinger 	/*
3078b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3079b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3080b8d26b3bSNicholas Bellinger 	 */
3081b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
308213a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
3083b8d26b3bSNicholas Bellinger 
3084ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
3085b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3086b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3087b8d26b3bSNicholas Bellinger 		goto out;
3088b8d26b3bSNicholas Bellinger 	}
3089b8d26b3bSNicholas Bellinger 
3090ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
3091b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3092b8d26b3bSNicholas Bellinger 
3093b8d26b3bSNicholas Bellinger 	return 0;
3094b8d26b3bSNicholas Bellinger 
3095b8d26b3bSNicholas Bellinger out:
3096b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3097ca6c1d82SSagi Grimberg 
3098b8d26b3bSNicholas Bellinger 	return ret;
3099b8d26b3bSNicholas Bellinger }
3100b8d26b3bSNicholas Bellinger 
3101b8d26b3bSNicholas Bellinger static int
3102b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3103b8d26b3bSNicholas Bellinger {
3104dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
3105b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3106b8d26b3bSNicholas Bellinger 	int ret;
310713bce482SJenny Derzhavetz 	struct iser_cm_hdr rsp_hdr;
3108b8d26b3bSNicholas Bellinger 
3109b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3110b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3111b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3112b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3113b8d26b3bSNicholas Bellinger 
311413bce482SJenny Derzhavetz 	memset(&rsp_hdr, 0, sizeof(rsp_hdr));
3115422bd0acSJenny Derzhavetz 	rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
3116422bd0acSJenny Derzhavetz 	if (!isert_conn->snd_w_inv)
3117422bd0acSJenny Derzhavetz 		rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
311813bce482SJenny Derzhavetz 	cp.private_data = (void *)&rsp_hdr;
311913bce482SJenny Derzhavetz 	cp.private_data_len = sizeof(rsp_hdr);
312013bce482SJenny Derzhavetz 
3121b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3122b8d26b3bSNicholas Bellinger 	if (ret) {
312324f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
3124b8d26b3bSNicholas Bellinger 		return ret;
3125b8d26b3bSNicholas Bellinger 	}
3126b8d26b3bSNicholas Bellinger 
3127b8d26b3bSNicholas Bellinger 	return 0;
3128b8d26b3bSNicholas Bellinger }
3129b8d26b3bSNicholas Bellinger 
3130b8d26b3bSNicholas Bellinger static int
3131b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3132b8d26b3bSNicholas Bellinger {
31336700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
3134b8d26b3bSNicholas Bellinger 	int ret;
3135b8d26b3bSNicholas Bellinger 
313624f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
31372371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
31382371e5daSSagi Grimberg 	if (ret) {
313924f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
31402371e5daSSagi Grimberg 			  isert_conn);
31412371e5daSSagi Grimberg 		return ret;
31422371e5daSSagi Grimberg 	}
31432371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
31442371e5daSSagi Grimberg 
31456faaa85fSNicholas Bellinger 	/*
31466faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
31476faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
31486faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
31496faaa85fSNicholas Bellinger 	 * into a NOP.
31506faaa85fSNicholas Bellinger 	 */
31516faaa85fSNicholas Bellinger 	if (!login->first_request)
31526faaa85fSNicholas Bellinger 		return 0;
3153b8d26b3bSNicholas Bellinger 
31542371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
31552371e5daSSagi Grimberg 
3156dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
3157dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
3158b8d26b3bSNicholas Bellinger 	if (ret)
3159b8d26b3bSNicholas Bellinger 		return ret;
3160b8d26b3bSNicholas Bellinger 
316124f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
31622371e5daSSagi Grimberg 
3163b8d26b3bSNicholas Bellinger 	return 0;
3164b8d26b3bSNicholas Bellinger }
3165b8d26b3bSNicholas Bellinger 
3166b8d26b3bSNicholas Bellinger static void
3167b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3168b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3169b8d26b3bSNicholas Bellinger {
3170dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
3171b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3172b8d26b3bSNicholas Bellinger 
3173b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3174b8d26b3bSNicholas Bellinger 
3175dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
3176dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
3177b8d26b3bSNicholas Bellinger }
3178b8d26b3bSNicholas Bellinger 
3179b8d26b3bSNicholas Bellinger static int
3180b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3181b8d26b3bSNicholas Bellinger {
31826700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
3183b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3184c6b8e918SSagi Grimberg 	int ret;
3185b8d26b3bSNicholas Bellinger 
3186b8d26b3bSNicholas Bellinger accept_wait:
3187ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
3188c6b8e918SSagi Grimberg 	if (ret)
3189b8d26b3bSNicholas Bellinger 		return -ENODEV;
3190b8d26b3bSNicholas Bellinger 
3191b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3192e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3193b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
319411378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
3195e346ab34SSagi Grimberg 			 np->np_thread_state);
3196e346ab34SSagi Grimberg 		/**
3197e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
3198e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
3199e346ab34SSagi Grimberg 		 **/
3200b8d26b3bSNicholas Bellinger 		return -ENODEV;
3201b8d26b3bSNicholas Bellinger 	}
3202b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3203b8d26b3bSNicholas Bellinger 
3204ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
3205bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
3206ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
3207b8d26b3bSNicholas Bellinger 		goto accept_wait;
3208b8d26b3bSNicholas Bellinger 	}
3209bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
3210bd379220SJenny Derzhavetz 			struct isert_conn, node);
3211bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
3212ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
3213b8d26b3bSNicholas Bellinger 
3214b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3215b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3216aea92980SJenny Derzhavetz 	isert_conn->state = ISER_CONN_BOUND;
3217b8d26b3bSNicholas Bellinger 
3218b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3219b8d26b3bSNicholas Bellinger 
322024f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
32212371e5daSSagi Grimberg 
3222b8d26b3bSNicholas Bellinger 	return 0;
3223b8d26b3bSNicholas Bellinger }
3224b8d26b3bSNicholas Bellinger 
3225b8d26b3bSNicholas Bellinger static void
3226b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3227b8d26b3bSNicholas Bellinger {
32286700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
3229268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
3230b8d26b3bSNicholas Bellinger 
3231ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
3232ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
3233b8d26b3bSNicholas Bellinger 
3234268e6811SSagi Grimberg 	/*
3235268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
3236268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
3237268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
3238268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
3239bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
3240268e6811SSagi Grimberg 	 */
3241ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
3242bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
3243bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
3244268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
3245bd379220SJenny Derzhavetz 					 &isert_np->pending,
3246bd379220SJenny Derzhavetz 					 node) {
3247bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
3248bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
3249bd379220SJenny Derzhavetz 			isert_connect_release(isert_conn);
3250bd379220SJenny Derzhavetz 		}
3251bd379220SJenny Derzhavetz 	}
3252bd379220SJenny Derzhavetz 
3253bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
3254bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
3255bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
3256bd379220SJenny Derzhavetz 					 &isert_np->accepted,
3257bd379220SJenny Derzhavetz 					 node) {
325824f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
3259268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
3260268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
3261268e6811SSagi Grimberg 		}
3262268e6811SSagi Grimberg 	}
3263ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
3264268e6811SSagi Grimberg 
3265b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3266b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3267b8d26b3bSNicholas Bellinger }
3268b8d26b3bSNicholas Bellinger 
3269b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
3270b02efbfcSSagi Grimberg {
3271b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
3272b02efbfcSSagi Grimberg 						     struct isert_conn,
3273b02efbfcSSagi Grimberg 						     release_work);
3274b02efbfcSSagi Grimberg 
327524f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
3276b02efbfcSSagi Grimberg 
3277dac6ab30SSagi Grimberg 	wait_for_completion(&isert_conn->wait);
3278b02efbfcSSagi Grimberg 
3279dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
3280b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
3281dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
3282b02efbfcSSagi Grimberg 
328324f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
3284b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
3285b02efbfcSSagi Grimberg }
3286b02efbfcSSagi Grimberg 
3287bdf20e72SSagi Grimberg static void
3288991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
3289991bb764SSagi Grimberg {
3290991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
3291991bb764SSagi Grimberg 
32924c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
32934c22e07fSSagi Grimberg 
3294991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
329524f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
3296991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
3297991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
3298991bb764SSagi Grimberg 	}
3299991bb764SSagi Grimberg }
3300991bb764SSagi Grimberg 
3301991bb764SSagi Grimberg static void
3302c7e160eeSSagi Grimberg isert_wait4cmds(struct iscsi_conn *conn)
3303c7e160eeSSagi Grimberg {
33044c22e07fSSagi Grimberg 	isert_info("iscsi_conn %p\n", conn);
33054c22e07fSSagi Grimberg 
3306c7e160eeSSagi Grimberg 	if (conn->sess) {
3307c7e160eeSSagi Grimberg 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3308c7e160eeSSagi Grimberg 		target_wait_for_sess_cmds(conn->sess->se_sess);
3309c7e160eeSSagi Grimberg 	}
3310c7e160eeSSagi Grimberg }
3311c7e160eeSSagi Grimberg 
3312c7e160eeSSagi Grimberg static void
3313bdf20e72SSagi Grimberg isert_wait4flush(struct isert_conn *isert_conn)
3314bdf20e72SSagi Grimberg {
3315bdf20e72SSagi Grimberg 	struct ib_recv_wr *bad_wr;
3316bdf20e72SSagi Grimberg 
33174c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
33184c22e07fSSagi Grimberg 
3319dac6ab30SSagi Grimberg 	init_completion(&isert_conn->wait_comp_err);
3320bdf20e72SSagi Grimberg 	isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3321bdf20e72SSagi Grimberg 	/* post an indication that all flush errors were consumed */
3322dac6ab30SSagi Grimberg 	if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
332324f412ddSSagi Grimberg 		isert_err("conn %p failed to post beacon", isert_conn);
3324bdf20e72SSagi Grimberg 		return;
3325bdf20e72SSagi Grimberg 	}
3326bdf20e72SSagi Grimberg 
3327dac6ab30SSagi Grimberg 	wait_for_completion(&isert_conn->wait_comp_err);
3328bdf20e72SSagi Grimberg }
3329bdf20e72SSagi Grimberg 
33303e03c4b0SJenny Derzhavetz /**
33313e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
33323e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
33333e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
33343e03c4b0SJenny Derzhavetz  *
33353e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
33363e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
33373e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
33383e03c4b0SJenny Derzhavetz  */
33393e03c4b0SJenny Derzhavetz static void
33403e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
33413e03c4b0SJenny Derzhavetz {
33423e03c4b0SJenny Derzhavetz 	struct iscsi_cmd *cmd, *tmp;
33433e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
33443e03c4b0SJenny Derzhavetz 
33453e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
33463e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
33473e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
33483e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
33493e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
33503e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
33513e03c4b0SJenny Derzhavetz 	}
33523e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
33533e03c4b0SJenny Derzhavetz 
33543e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
33553e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
33563e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
33573e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
33583e03c4b0SJenny Derzhavetz 
33593e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
33603e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
33613e03c4b0SJenny Derzhavetz 		}
33623e03c4b0SJenny Derzhavetz 	}
33633e03c4b0SJenny Derzhavetz }
33643e03c4b0SJenny Derzhavetz 
3365defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3366b8d26b3bSNicholas Bellinger {
3367b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3368b8d26b3bSNicholas Bellinger 
33694c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
3370b8d26b3bSNicholas Bellinger 
3371dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
3372b8d26b3bSNicholas Bellinger 	/*
3373dac6ab30SSagi Grimberg 	 * Only wait for wait_comp_err if the isert_conn made it
3374b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3375b8d26b3bSNicholas Bellinger 	 */
3376b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3377dac6ab30SSagi Grimberg 		mutex_unlock(&isert_conn->mutex);
3378b2cb9649SNicholas Bellinger 		return;
3379b2cb9649SNicholas Bellinger 	}
3380954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
3381dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
3382b2cb9649SNicholas Bellinger 
3383bdf20e72SSagi Grimberg 	isert_wait4flush(isert_conn);
33843e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
33853e03c4b0SJenny Derzhavetz 	isert_wait4cmds(conn);
3386991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
3387954f2372SSagi Grimberg 
3388b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
3389defd8848SNicholas Bellinger }
3390defd8848SNicholas Bellinger 
3391defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3392defd8848SNicholas Bellinger {
3393defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3394b8d26b3bSNicholas Bellinger 
3395524630d5SSagi Grimberg 	isert_wait4flush(isert_conn);
3396b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3397b8d26b3bSNicholas Bellinger }
3398b8d26b3bSNicholas Bellinger 
3399b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3400b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3401b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3402d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3403b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3404b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3405b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3406b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3407defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3408b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3409b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3410b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3411b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3412b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3413b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3414b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3415b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3416131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3417e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3418b8d26b3bSNicholas Bellinger };
3419b8d26b3bSNicholas Bellinger 
3420b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3421b8d26b3bSNicholas Bellinger {
3422b8d26b3bSNicholas Bellinger 	int ret;
3423b8d26b3bSNicholas Bellinger 
3424631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
3425631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
3426b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
342724f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
3428b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
34296f0fae3dSSagi Grimberg 		return -ENOMEM;
3430b8d26b3bSNicholas Bellinger 	}
3431b8d26b3bSNicholas Bellinger 
3432b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3433b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
3434b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
343524f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
3436b02efbfcSSagi Grimberg 		ret = -ENOMEM;
3437b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
3438b02efbfcSSagi Grimberg 	}
3439b02efbfcSSagi Grimberg 
3440b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
344124f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3442b02efbfcSSagi Grimberg 
3443b8d26b3bSNicholas Bellinger 	return 0;
3444b8d26b3bSNicholas Bellinger 
3445b02efbfcSSagi Grimberg destroy_comp_wq:
3446b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
34476f0fae3dSSagi Grimberg 
3448b8d26b3bSNicholas Bellinger 	return ret;
3449b8d26b3bSNicholas Bellinger }
3450b8d26b3bSNicholas Bellinger 
3451b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3452b8d26b3bSNicholas Bellinger {
3453f5ebec96SSagi Grimberg 	flush_scheduled_work();
3454b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
3455b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3456b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
34574c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
3458b8d26b3bSNicholas Bellinger }
3459b8d26b3bSNicholas Bellinger 
3460b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
34619e35eff4SSagi Grimberg MODULE_VERSION("1.0");
3462b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3463b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3464b8d26b3bSNicholas Bellinger 
3465b8d26b3bSNicholas Bellinger module_init(isert_init);
3466b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3467