xref: /linux/drivers/infiniband/ulp/isert/ib_isert.c (revision d3cf81f9c805d599e91d1dcaebdd82ec17c299a6)
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 "isert_proto.h"
33b8d26b3bSNicholas Bellinger #include "ib_isert.h"
34b8d26b3bSNicholas Bellinger 
35b8d26b3bSNicholas Bellinger #define	ISERT_MAX_CONN		8
36b8d26b3bSNicholas Bellinger #define ISER_MAX_RX_CQ_LEN	(ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37b8d26b3bSNicholas Bellinger #define ISER_MAX_TX_CQ_LEN	(ISERT_QP_MAX_REQ_DTOS  * ISERT_MAX_CONN)
38bdf20e72SSagi Grimberg #define ISER_MAX_CQ_LEN		(ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39bdf20e72SSagi Grimberg 				 ISERT_MAX_CONN)
40b8d26b3bSNicholas Bellinger 
4145678b6bSSagi Grimberg static int isert_debug_level;
4224f412ddSSagi Grimberg module_param_named(debug_level, isert_debug_level, int, 0644);
4324f412ddSSagi Grimberg MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
4424f412ddSSagi Grimberg 
45b8d26b3bSNicholas Bellinger static DEFINE_MUTEX(device_list_mutex);
46b8d26b3bSNicholas Bellinger static LIST_HEAD(device_list);
47b8d26b3bSNicholas Bellinger static struct workqueue_struct *isert_comp_wq;
48b02efbfcSSagi Grimberg static struct workqueue_struct *isert_release_wq;
49b8d26b3bSNicholas Bellinger 
50b8d26b3bSNicholas Bellinger static void
51d40945d8SVu Pham isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52d40945d8SVu Pham static int
53d40945d8SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54d40945d8SVu Pham 	       struct isert_rdma_wr *wr);
5559464ef4SVu Pham static void
56a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
5759464ef4SVu Pham static int
58a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
5959464ef4SVu Pham 	       struct isert_rdma_wr *wr);
60f93f3a70SSagi Grimberg static int
61f93f3a70SSagi Grimberg isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
622371e5daSSagi Grimberg static int
632371e5daSSagi Grimberg isert_rdma_post_recvl(struct isert_conn *isert_conn);
642371e5daSSagi Grimberg static int
652371e5daSSagi Grimberg isert_rdma_accept(struct isert_conn *isert_conn);
66ca6c1d82SSagi Grimberg struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
67d40945d8SVu Pham 
682f1b6b7dSSagi Grimberg static void isert_release_work(struct work_struct *work);
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)
350172369c5SSagi Grimberg 		return ret;
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);
368b8d26b3bSNicholas Bellinger 	return ret;
369b8d26b3bSNicholas Bellinger }
370b8d26b3bSNicholas Bellinger 
371b8d26b3bSNicholas Bellinger static void
372b8d26b3bSNicholas Bellinger isert_free_device_ib_res(struct isert_device *device)
373b8d26b3bSNicholas Bellinger {
37424f412ddSSagi Grimberg 	isert_info("device %p\n", device);
375b8d26b3bSNicholas Bellinger 
37667cb3949SSagi Grimberg 	ib_dealloc_pd(device->pd);
377172369c5SSagi Grimberg 	isert_free_comps(device);
378b8d26b3bSNicholas Bellinger }
379b8d26b3bSNicholas Bellinger 
380b8d26b3bSNicholas Bellinger static void
381cf8ae958SSagi Grimberg isert_device_put(struct isert_device *device)
382b8d26b3bSNicholas Bellinger {
383b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
384b8d26b3bSNicholas Bellinger 	device->refcount--;
3854c22e07fSSagi Grimberg 	isert_info("device %p refcount %d\n", device, device->refcount);
386b8d26b3bSNicholas Bellinger 	if (!device->refcount) {
387b8d26b3bSNicholas Bellinger 		isert_free_device_ib_res(device);
388b8d26b3bSNicholas Bellinger 		list_del(&device->dev_node);
389b8d26b3bSNicholas Bellinger 		kfree(device);
390b8d26b3bSNicholas Bellinger 	}
391b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
392b8d26b3bSNicholas Bellinger }
393b8d26b3bSNicholas Bellinger 
394b8d26b3bSNicholas Bellinger static struct isert_device *
395cf8ae958SSagi Grimberg isert_device_get(struct rdma_cm_id *cma_id)
396b8d26b3bSNicholas Bellinger {
397b8d26b3bSNicholas Bellinger 	struct isert_device *device;
398b8d26b3bSNicholas Bellinger 	int ret;
399b8d26b3bSNicholas Bellinger 
400b8d26b3bSNicholas Bellinger 	mutex_lock(&device_list_mutex);
401b8d26b3bSNicholas Bellinger 	list_for_each_entry(device, &device_list, dev_node) {
402b8d26b3bSNicholas Bellinger 		if (device->ib_device->node_guid == cma_id->device->node_guid) {
403b8d26b3bSNicholas Bellinger 			device->refcount++;
4044c22e07fSSagi Grimberg 			isert_info("Found iser device %p refcount %d\n",
4054c22e07fSSagi Grimberg 				   device, device->refcount);
406b8d26b3bSNicholas Bellinger 			mutex_unlock(&device_list_mutex);
407b8d26b3bSNicholas Bellinger 			return device;
408b8d26b3bSNicholas Bellinger 		}
409b8d26b3bSNicholas Bellinger 	}
410b8d26b3bSNicholas Bellinger 
411b8d26b3bSNicholas Bellinger 	device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
412b8d26b3bSNicholas Bellinger 	if (!device) {
413b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
414b8d26b3bSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
415b8d26b3bSNicholas Bellinger 	}
416b8d26b3bSNicholas Bellinger 
417b8d26b3bSNicholas Bellinger 	INIT_LIST_HEAD(&device->dev_node);
418b8d26b3bSNicholas Bellinger 
419b8d26b3bSNicholas Bellinger 	device->ib_device = cma_id->device;
420b8d26b3bSNicholas Bellinger 	ret = isert_create_device_ib_res(device);
421b8d26b3bSNicholas Bellinger 	if (ret) {
422b8d26b3bSNicholas Bellinger 		kfree(device);
423b8d26b3bSNicholas Bellinger 		mutex_unlock(&device_list_mutex);
424b8d26b3bSNicholas Bellinger 		return ERR_PTR(ret);
425b8d26b3bSNicholas Bellinger 	}
426b8d26b3bSNicholas Bellinger 
427b8d26b3bSNicholas Bellinger 	device->refcount++;
428b8d26b3bSNicholas Bellinger 	list_add_tail(&device->dev_node, &device_list);
4294c22e07fSSagi Grimberg 	isert_info("Created a new iser device %p refcount %d\n",
4304c22e07fSSagi Grimberg 		   device, device->refcount);
431b8d26b3bSNicholas Bellinger 	mutex_unlock(&device_list_mutex);
432b8d26b3bSNicholas Bellinger 
433b8d26b3bSNicholas Bellinger 	return device;
434b8d26b3bSNicholas Bellinger }
435b8d26b3bSNicholas Bellinger 
43659464ef4SVu Pham static void
437a3a5a826SSagi Grimberg isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
43859464ef4SVu Pham {
43959464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc, *tmp;
44059464ef4SVu Pham 	int i = 0;
44159464ef4SVu Pham 
442dac6ab30SSagi Grimberg 	if (list_empty(&isert_conn->fr_pool))
44359464ef4SVu Pham 		return;
44459464ef4SVu Pham 
4454c22e07fSSagi Grimberg 	isert_info("Freeing conn %p fastreg pool", isert_conn);
44659464ef4SVu Pham 
44759464ef4SVu Pham 	list_for_each_entry_safe(fr_desc, tmp,
448dac6ab30SSagi Grimberg 				 &isert_conn->fr_pool, list) {
44959464ef4SVu Pham 		list_del(&fr_desc->list);
45059464ef4SVu Pham 		ib_dereg_mr(fr_desc->data_mr);
451d3e125daSSagi Grimberg 		if (fr_desc->pi_ctx) {
452d3e125daSSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
4538b91ffc1SSagi Grimberg 			ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
454d3e125daSSagi Grimberg 			kfree(fr_desc->pi_ctx);
455d3e125daSSagi Grimberg 		}
45659464ef4SVu Pham 		kfree(fr_desc);
45759464ef4SVu Pham 		++i;
45859464ef4SVu Pham 	}
45959464ef4SVu Pham 
460dac6ab30SSagi Grimberg 	if (i < isert_conn->fr_pool_size)
46124f412ddSSagi Grimberg 		isert_warn("Pool still has %d regions registered\n",
462dac6ab30SSagi Grimberg 			isert_conn->fr_pool_size - i);
46359464ef4SVu Pham }
46459464ef4SVu Pham 
46559464ef4SVu Pham static int
466570db170SSagi Grimberg isert_create_pi_ctx(struct fast_reg_descriptor *desc,
467570db170SSagi Grimberg 		    struct ib_device *device,
468570db170SSagi Grimberg 		    struct ib_pd *pd)
469570db170SSagi Grimberg {
470570db170SSagi Grimberg 	struct pi_context *pi_ctx;
471570db170SSagi Grimberg 	int ret;
472570db170SSagi Grimberg 
473570db170SSagi Grimberg 	pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
474570db170SSagi Grimberg 	if (!pi_ctx) {
47524f412ddSSagi Grimberg 		isert_err("Failed to allocate pi context\n");
476570db170SSagi Grimberg 		return -ENOMEM;
477570db170SSagi Grimberg 	}
478570db170SSagi Grimberg 
479a89be2ccSSagi Grimberg 	pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
480a89be2ccSSagi Grimberg 				      ISCSI_ISER_SG_TABLESIZE);
481570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->prot_mr)) {
48224f412ddSSagi Grimberg 		isert_err("Failed to allocate prot frmr err=%ld\n",
483570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->prot_mr));
484570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->prot_mr);
48516c2d702SSagi Grimberg 		goto err_pi_ctx;
486570db170SSagi Grimberg 	}
487570db170SSagi Grimberg 	desc->ind |= ISERT_PROT_KEY_VALID;
488570db170SSagi Grimberg 
4899bee178bSSagi Grimberg 	pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
490570db170SSagi Grimberg 	if (IS_ERR(pi_ctx->sig_mr)) {
49124f412ddSSagi Grimberg 		isert_err("Failed to allocate signature enabled mr err=%ld\n",
492570db170SSagi Grimberg 			  PTR_ERR(pi_ctx->sig_mr));
493570db170SSagi Grimberg 		ret = PTR_ERR(pi_ctx->sig_mr);
494570db170SSagi Grimberg 		goto err_prot_mr;
495570db170SSagi Grimberg 	}
496570db170SSagi Grimberg 
497570db170SSagi Grimberg 	desc->pi_ctx = pi_ctx;
498570db170SSagi Grimberg 	desc->ind |= ISERT_SIG_KEY_VALID;
499570db170SSagi Grimberg 	desc->ind &= ~ISERT_PROTECTED;
500570db170SSagi Grimberg 
501570db170SSagi Grimberg 	return 0;
502570db170SSagi Grimberg 
503570db170SSagi Grimberg err_prot_mr:
504b2feda4fSRoland Dreier 	ib_dereg_mr(pi_ctx->prot_mr);
505570db170SSagi Grimberg err_pi_ctx:
506b2feda4fSRoland Dreier 	kfree(pi_ctx);
507570db170SSagi Grimberg 
508570db170SSagi Grimberg 	return ret;
509570db170SSagi Grimberg }
510570db170SSagi Grimberg 
511570db170SSagi Grimberg static int
512dc87a90fSSagi Grimberg isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
513570db170SSagi Grimberg 		     struct fast_reg_descriptor *fr_desc)
514dc87a90fSSagi Grimberg {
515a89be2ccSSagi Grimberg 	fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
516a89be2ccSSagi Grimberg 				       ISCSI_ISER_SG_TABLESIZE);
517dc87a90fSSagi Grimberg 	if (IS_ERR(fr_desc->data_mr)) {
51824f412ddSSagi Grimberg 		isert_err("Failed to allocate data frmr err=%ld\n",
519dc87a90fSSagi Grimberg 			  PTR_ERR(fr_desc->data_mr));
52016c2d702SSagi Grimberg 		return PTR_ERR(fr_desc->data_mr);
521dc87a90fSSagi Grimberg 	}
522d3e125daSSagi Grimberg 	fr_desc->ind |= ISERT_DATA_KEY_VALID;
523dc87a90fSSagi Grimberg 
52424f412ddSSagi Grimberg 	isert_dbg("Created fr_desc %p\n", fr_desc);
525dc87a90fSSagi Grimberg 
526dc87a90fSSagi Grimberg 	return 0;
527dc87a90fSSagi Grimberg }
528dc87a90fSSagi Grimberg 
529dc87a90fSSagi Grimberg static int
530570db170SSagi Grimberg isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
53159464ef4SVu Pham {
53259464ef4SVu Pham 	struct fast_reg_descriptor *fr_desc;
533dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
534f46d6a8aSNicholas Bellinger 	struct se_session *se_sess = isert_conn->conn->sess->se_sess;
535f46d6a8aSNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
536f46d6a8aSNicholas Bellinger 	int i, ret, tag_num;
537f46d6a8aSNicholas Bellinger 	/*
538f46d6a8aSNicholas Bellinger 	 * Setup the number of FRMRs based upon the number of tags
539f46d6a8aSNicholas Bellinger 	 * available to session in iscsi_target_locate_portal().
540f46d6a8aSNicholas Bellinger 	 */
541f46d6a8aSNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
542f46d6a8aSNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
54359464ef4SVu Pham 
544dac6ab30SSagi Grimberg 	isert_conn->fr_pool_size = 0;
545f46d6a8aSNicholas Bellinger 	for (i = 0; i < tag_num; i++) {
54659464ef4SVu Pham 		fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
54759464ef4SVu Pham 		if (!fr_desc) {
54824f412ddSSagi Grimberg 			isert_err("Failed to allocate fast_reg descriptor\n");
54959464ef4SVu Pham 			ret = -ENOMEM;
55059464ef4SVu Pham 			goto err;
55159464ef4SVu Pham 		}
55259464ef4SVu Pham 
553dc87a90fSSagi Grimberg 		ret = isert_create_fr_desc(device->ib_device,
55467cb3949SSagi Grimberg 					   device->pd, fr_desc);
555dc87a90fSSagi Grimberg 		if (ret) {
55624f412ddSSagi Grimberg 			isert_err("Failed to create fastreg descriptor err=%d\n",
557dc87a90fSSagi Grimberg 			       ret);
558a80e21b3SNicholas Bellinger 			kfree(fr_desc);
55959464ef4SVu Pham 			goto err;
56059464ef4SVu Pham 		}
56159464ef4SVu Pham 
562dac6ab30SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
563dac6ab30SSagi Grimberg 		isert_conn->fr_pool_size++;
56459464ef4SVu Pham 	}
56559464ef4SVu Pham 
56624f412ddSSagi Grimberg 	isert_dbg("Creating conn %p fastreg pool size=%d",
567dac6ab30SSagi Grimberg 		 isert_conn, isert_conn->fr_pool_size);
56859464ef4SVu Pham 
56959464ef4SVu Pham 	return 0;
57059464ef4SVu Pham 
57159464ef4SVu Pham err:
572a3a5a826SSagi Grimberg 	isert_conn_free_fastreg_pool(isert_conn);
57359464ef4SVu Pham 	return ret;
57459464ef4SVu Pham }
57559464ef4SVu Pham 
576ae9ea9edSSagi Grimberg static void
577ae9ea9edSSagi Grimberg isert_init_conn(struct isert_conn *isert_conn)
578ae9ea9edSSagi Grimberg {
579ae9ea9edSSagi Grimberg 	isert_conn->state = ISER_CONN_INIT;
580bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_conn->node);
581dac6ab30SSagi Grimberg 	init_completion(&isert_conn->login_comp);
582ae9ea9edSSagi Grimberg 	init_completion(&isert_conn->login_req_comp);
583dac6ab30SSagi Grimberg 	init_completion(&isert_conn->wait);
584dac6ab30SSagi Grimberg 	kref_init(&isert_conn->kref);
585dac6ab30SSagi Grimberg 	mutex_init(&isert_conn->mutex);
586dac6ab30SSagi Grimberg 	spin_lock_init(&isert_conn->pool_lock);
587dac6ab30SSagi Grimberg 	INIT_LIST_HEAD(&isert_conn->fr_pool);
5882f1b6b7dSSagi Grimberg 	INIT_WORK(&isert_conn->release_work, isert_release_work);
589ae9ea9edSSagi Grimberg }
590ae9ea9edSSagi Grimberg 
591ae9ea9edSSagi Grimberg static void
592ae9ea9edSSagi Grimberg isert_free_login_buf(struct isert_conn *isert_conn)
593ae9ea9edSSagi Grimberg {
594dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->device->ib_device;
595ae9ea9edSSagi Grimberg 
596ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
597ae9ea9edSSagi Grimberg 			    ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
598ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
599ae9ea9edSSagi Grimberg 			    ISCSI_DEF_MAX_RECV_SEG_LEN,
600ae9ea9edSSagi Grimberg 			    DMA_FROM_DEVICE);
601ae9ea9edSSagi Grimberg 	kfree(isert_conn->login_buf);
602ae9ea9edSSagi Grimberg }
603ae9ea9edSSagi Grimberg 
604ae9ea9edSSagi Grimberg static int
605ae9ea9edSSagi Grimberg isert_alloc_login_buf(struct isert_conn *isert_conn,
606ae9ea9edSSagi Grimberg 		      struct ib_device *ib_dev)
607ae9ea9edSSagi Grimberg {
608ae9ea9edSSagi Grimberg 	int ret;
609ae9ea9edSSagi Grimberg 
610ae9ea9edSSagi Grimberg 	isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
611ae9ea9edSSagi Grimberg 					ISER_RX_LOGIN_SIZE, GFP_KERNEL);
612ae9ea9edSSagi Grimberg 	if (!isert_conn->login_buf) {
613ae9ea9edSSagi Grimberg 		isert_err("Unable to allocate isert_conn->login_buf\n");
614ae9ea9edSSagi Grimberg 		return -ENOMEM;
615ae9ea9edSSagi Grimberg 	}
616ae9ea9edSSagi Grimberg 
617ae9ea9edSSagi Grimberg 	isert_conn->login_req_buf = isert_conn->login_buf;
618ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_buf = isert_conn->login_buf +
619ae9ea9edSSagi Grimberg 				    ISCSI_DEF_MAX_RECV_SEG_LEN;
620ae9ea9edSSagi Grimberg 
621ae9ea9edSSagi Grimberg 	isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
622ae9ea9edSSagi Grimberg 		 isert_conn->login_buf, isert_conn->login_req_buf,
623ae9ea9edSSagi Grimberg 		 isert_conn->login_rsp_buf);
624ae9ea9edSSagi Grimberg 
625ae9ea9edSSagi Grimberg 	isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
626ae9ea9edSSagi Grimberg 				(void *)isert_conn->login_req_buf,
627ae9ea9edSSagi Grimberg 				ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
628ae9ea9edSSagi Grimberg 
629ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
630ae9ea9edSSagi Grimberg 	if (ret) {
631ae9ea9edSSagi Grimberg 		isert_err("login_req_dma mapping error: %d\n", ret);
632ae9ea9edSSagi Grimberg 		isert_conn->login_req_dma = 0;
633ae9ea9edSSagi Grimberg 		goto out_login_buf;
634ae9ea9edSSagi Grimberg 	}
635ae9ea9edSSagi Grimberg 
636ae9ea9edSSagi Grimberg 	isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
637ae9ea9edSSagi Grimberg 					(void *)isert_conn->login_rsp_buf,
638ae9ea9edSSagi Grimberg 					ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
639ae9ea9edSSagi Grimberg 
640ae9ea9edSSagi Grimberg 	ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
641ae9ea9edSSagi Grimberg 	if (ret) {
642ae9ea9edSSagi Grimberg 		isert_err("login_rsp_dma mapping error: %d\n", ret);
643ae9ea9edSSagi Grimberg 		isert_conn->login_rsp_dma = 0;
644ae9ea9edSSagi Grimberg 		goto out_req_dma_map;
645ae9ea9edSSagi Grimberg 	}
646ae9ea9edSSagi Grimberg 
647ae9ea9edSSagi Grimberg 	return 0;
648ae9ea9edSSagi Grimberg 
649ae9ea9edSSagi Grimberg out_req_dma_map:
650ae9ea9edSSagi Grimberg 	ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
651ae9ea9edSSagi Grimberg 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
652ae9ea9edSSagi Grimberg out_login_buf:
653ae9ea9edSSagi Grimberg 	kfree(isert_conn->login_buf);
654ae9ea9edSSagi Grimberg 	return ret;
655ae9ea9edSSagi Grimberg }
656ae9ea9edSSagi Grimberg 
657b8d26b3bSNicholas Bellinger static int
658b8d26b3bSNicholas Bellinger isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
659b8d26b3bSNicholas Bellinger {
660ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
661ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
662b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
663b8d26b3bSNicholas Bellinger 	struct isert_device *device;
664b8d26b3bSNicholas Bellinger 	int ret = 0;
66514f4b54fSSagi Grimberg 
66614f4b54fSSagi Grimberg 	spin_lock_bh(&np->np_thread_lock);
66714f4b54fSSagi Grimberg 	if (!np->enabled) {
66814f4b54fSSagi Grimberg 		spin_unlock_bh(&np->np_thread_lock);
66924f412ddSSagi Grimberg 		isert_dbg("iscsi_np is not enabled, reject connect request\n");
67014f4b54fSSagi Grimberg 		return rdma_reject(cma_id, NULL, 0);
67114f4b54fSSagi Grimberg 	}
67214f4b54fSSagi Grimberg 	spin_unlock_bh(&np->np_thread_lock);
673b8d26b3bSNicholas Bellinger 
6744c22e07fSSagi Grimberg 	isert_dbg("cma_id: %p, portal: %p\n",
675b8d26b3bSNicholas Bellinger 		 cma_id, cma_id->context);
676b8d26b3bSNicholas Bellinger 
677b8d26b3bSNicholas Bellinger 	isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
678ae9ea9edSSagi Grimberg 	if (!isert_conn)
679b8d26b3bSNicholas Bellinger 		return -ENOMEM;
680b8d26b3bSNicholas Bellinger 
681ae9ea9edSSagi Grimberg 	isert_init_conn(isert_conn);
682dac6ab30SSagi Grimberg 	isert_conn->cm_id = cma_id;
683b8d26b3bSNicholas Bellinger 
684ae9ea9edSSagi Grimberg 	ret = isert_alloc_login_buf(isert_conn, cma_id->device);
685ae9ea9edSSagi Grimberg 	if (ret)
686b8d26b3bSNicholas Bellinger 		goto out;
687b8d26b3bSNicholas Bellinger 
688cf8ae958SSagi Grimberg 	device = isert_device_get(cma_id);
689b8d26b3bSNicholas Bellinger 	if (IS_ERR(device)) {
690b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(device);
691b8d26b3bSNicholas Bellinger 		goto out_rsp_dma_map;
692b8d26b3bSNicholas Bellinger 	}
693dac6ab30SSagi Grimberg 	isert_conn->device = device;
694b8d26b3bSNicholas Bellinger 
6951a92e17eSSagi Grimberg 	/* Set max inflight RDMA READ requests */
6961a92e17eSSagi Grimberg 	isert_conn->initiator_depth = min_t(u8,
6971a92e17eSSagi Grimberg 				event->param.conn.initiator_depth,
6984a061b28SOr Gerlitz 				device->ib_device->attrs.max_qp_init_rd_atom);
69924f412ddSSagi Grimberg 	isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
7001a92e17eSSagi Grimberg 
701570db170SSagi Grimberg 	ret = isert_conn_setup_qp(isert_conn, cma_id);
702b8d26b3bSNicholas Bellinger 	if (ret)
703b8d26b3bSNicholas Bellinger 		goto out_conn_dev;
704b8d26b3bSNicholas Bellinger 
7052371e5daSSagi Grimberg 	ret = isert_rdma_post_recvl(isert_conn);
7062371e5daSSagi Grimberg 	if (ret)
7072371e5daSSagi Grimberg 		goto out_conn_dev;
7082371e5daSSagi Grimberg 
7092371e5daSSagi Grimberg 	ret = isert_rdma_accept(isert_conn);
7102371e5daSSagi Grimberg 	if (ret)
7112371e5daSSagi Grimberg 		goto out_conn_dev;
7122371e5daSSagi Grimberg 
713ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
714bd379220SJenny Derzhavetz 	list_add_tail(&isert_conn->node, &isert_np->accepted);
715ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
716b8d26b3bSNicholas Bellinger 
717b8d26b3bSNicholas Bellinger 	return 0;
718b8d26b3bSNicholas Bellinger 
719b8d26b3bSNicholas Bellinger out_conn_dev:
720cf8ae958SSagi Grimberg 	isert_device_put(device);
721b8d26b3bSNicholas Bellinger out_rsp_dma_map:
722ae9ea9edSSagi Grimberg 	isert_free_login_buf(isert_conn);
723b8d26b3bSNicholas Bellinger out:
724b8d26b3bSNicholas Bellinger 	kfree(isert_conn);
7252371e5daSSagi Grimberg 	rdma_reject(cma_id, NULL, 0);
726b8d26b3bSNicholas Bellinger 	return ret;
727b8d26b3bSNicholas Bellinger }
728b8d26b3bSNicholas Bellinger 
729b8d26b3bSNicholas Bellinger static void
730b8d26b3bSNicholas Bellinger isert_connect_release(struct isert_conn *isert_conn)
731b8d26b3bSNicholas Bellinger {
732dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
733b8d26b3bSNicholas Bellinger 
7344c22e07fSSagi Grimberg 	isert_dbg("conn %p\n", isert_conn);
735b8d26b3bSNicholas Bellinger 
73657df81e3SSagi Grimberg 	BUG_ON(!device);
73757df81e3SSagi Grimberg 
73857df81e3SSagi Grimberg 	if (device->use_fastreg)
739a3a5a826SSagi Grimberg 		isert_conn_free_fastreg_pool(isert_conn);
74059464ef4SVu Pham 
74119e2090fSSagi Grimberg 	isert_free_rx_descriptors(isert_conn);
742dac6ab30SSagi Grimberg 	if (isert_conn->cm_id)
743dac6ab30SSagi Grimberg 		rdma_destroy_id(isert_conn->cm_id);
74419e2090fSSagi Grimberg 
745dac6ab30SSagi Grimberg 	if (isert_conn->qp) {
746dac6ab30SSagi Grimberg 		struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
7474a295baeSSagi Grimberg 
74840fc069aSSagi Grimberg 		isert_comp_put(comp);
749dac6ab30SSagi Grimberg 		ib_destroy_qp(isert_conn->qp);
750b8d26b3bSNicholas Bellinger 	}
751b8d26b3bSNicholas Bellinger 
752ae9ea9edSSagi Grimberg 	if (isert_conn->login_buf)
753ae9ea9edSSagi Grimberg 		isert_free_login_buf(isert_conn);
754ae9ea9edSSagi Grimberg 
755cf8ae958SSagi Grimberg 	isert_device_put(device);
75657df81e3SSagi Grimberg 
75757df81e3SSagi Grimberg 	kfree(isert_conn);
758b8d26b3bSNicholas Bellinger }
759b8d26b3bSNicholas Bellinger 
760b8d26b3bSNicholas Bellinger static void
761b8d26b3bSNicholas Bellinger isert_connected_handler(struct rdma_cm_id *cma_id)
762b8d26b3bSNicholas Bellinger {
76319e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
764bd379220SJenny Derzhavetz 	struct isert_np *isert_np = cma_id->context;
765c2f88b17SSagi Grimberg 
76624f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
767128e9cc8SSagi Grimberg 
768dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
769128e9cc8SSagi Grimberg 	isert_conn->state = ISER_CONN_UP;
770bd379220SJenny Derzhavetz 	kref_get(&isert_conn->kref);
771dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
772bd379220SJenny Derzhavetz 
773bd379220SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
774bd379220SJenny Derzhavetz 	list_move_tail(&isert_conn->node, &isert_np->pending);
775bd379220SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
776bd379220SJenny Derzhavetz 
777bd379220SJenny Derzhavetz 	isert_info("np %p: Allow accept_np to continue\n", isert_np);
778bd379220SJenny Derzhavetz 	up(&isert_np->sem);
779b8d26b3bSNicholas Bellinger }
780b8d26b3bSNicholas Bellinger 
781b8d26b3bSNicholas Bellinger static void
782dac6ab30SSagi Grimberg isert_release_kref(struct kref *kref)
783b8d26b3bSNicholas Bellinger {
784b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = container_of(kref,
785dac6ab30SSagi Grimberg 				struct isert_conn, kref);
786b8d26b3bSNicholas Bellinger 
7874c22e07fSSagi Grimberg 	isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
7884c22e07fSSagi Grimberg 		   current->pid);
789b8d26b3bSNicholas Bellinger 
790b8d26b3bSNicholas Bellinger 	isert_connect_release(isert_conn);
791b8d26b3bSNicholas Bellinger }
792b8d26b3bSNicholas Bellinger 
793b8d26b3bSNicholas Bellinger static void
794b8d26b3bSNicholas Bellinger isert_put_conn(struct isert_conn *isert_conn)
795b8d26b3bSNicholas Bellinger {
796dac6ab30SSagi Grimberg 	kref_put(&isert_conn->kref, isert_release_kref);
797b8d26b3bSNicholas Bellinger }
798b8d26b3bSNicholas Bellinger 
799954f2372SSagi Grimberg /**
800954f2372SSagi Grimberg  * isert_conn_terminate() - Initiate connection termination
801954f2372SSagi Grimberg  * @isert_conn: isert connection struct
802954f2372SSagi Grimberg  *
803954f2372SSagi Grimberg  * Notes:
804128e9cc8SSagi Grimberg  * In case the connection state is FULL_FEATURE, move state
805954f2372SSagi Grimberg  * to TEMINATING and start teardown sequence (rdma_disconnect).
806128e9cc8SSagi Grimberg  * In case the connection state is UP, complete flush as well.
807954f2372SSagi Grimberg  *
808dac6ab30SSagi Grimberg  * This routine must be called with mutex held. Thus it is
809954f2372SSagi Grimberg  * safe to call multiple times.
810954f2372SSagi Grimberg  */
811954f2372SSagi Grimberg static void
812954f2372SSagi Grimberg isert_conn_terminate(struct isert_conn *isert_conn)
813954f2372SSagi Grimberg {
814954f2372SSagi Grimberg 	int err;
815954f2372SSagi Grimberg 
816128e9cc8SSagi Grimberg 	switch (isert_conn->state) {
817128e9cc8SSagi Grimberg 	case ISER_CONN_TERMINATING:
818128e9cc8SSagi Grimberg 		break;
819128e9cc8SSagi Grimberg 	case ISER_CONN_UP:
820128e9cc8SSagi Grimberg 	case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
82124f412ddSSagi Grimberg 		isert_info("Terminating conn %p state %d\n",
822954f2372SSagi Grimberg 			   isert_conn, isert_conn->state);
823128e9cc8SSagi Grimberg 		isert_conn->state = ISER_CONN_TERMINATING;
824dac6ab30SSagi Grimberg 		err = rdma_disconnect(isert_conn->cm_id);
825954f2372SSagi Grimberg 		if (err)
82624f412ddSSagi Grimberg 			isert_warn("Failed rdma_disconnect isert_conn %p\n",
827954f2372SSagi Grimberg 				   isert_conn);
828128e9cc8SSagi Grimberg 		break;
829128e9cc8SSagi Grimberg 	default:
83024f412ddSSagi Grimberg 		isert_warn("conn %p teminating in state %d\n",
831128e9cc8SSagi Grimberg 			   isert_conn, isert_conn->state);
832954f2372SSagi Grimberg 	}
833954f2372SSagi Grimberg }
834954f2372SSagi Grimberg 
8353b726ae2SSagi Grimberg static int
836ca6c1d82SSagi Grimberg isert_np_cma_handler(struct isert_np *isert_np,
837ca6c1d82SSagi Grimberg 		     enum rdma_cm_event_type event)
838b8d26b3bSNicholas Bellinger {
839ea8a1616SSagi Grimberg 	isert_dbg("%s (%d): isert np %p\n",
840ea8a1616SSagi Grimberg 		  rdma_event_msg(event), event, isert_np);
8413b726ae2SSagi Grimberg 
842ca6c1d82SSagi Grimberg 	switch (event) {
843ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
844ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = NULL;
845ca6c1d82SSagi Grimberg 		break;
846ca6c1d82SSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:
847ed8cb0a4SJenny Derzhavetz 		isert_np->cm_id = isert_setup_id(isert_np);
848ed8cb0a4SJenny Derzhavetz 		if (IS_ERR(isert_np->cm_id)) {
84924f412ddSSagi Grimberg 			isert_err("isert np %p setup id failed: %ld\n",
850ed8cb0a4SJenny Derzhavetz 				  isert_np, PTR_ERR(isert_np->cm_id));
851ed8cb0a4SJenny Derzhavetz 			isert_np->cm_id = NULL;
852ca6c1d82SSagi Grimberg 		}
853ca6c1d82SSagi Grimberg 		break;
854ca6c1d82SSagi Grimberg 	default:
85524f412ddSSagi Grimberg 		isert_err("isert np %p Unexpected event %d\n",
856ca6c1d82SSagi Grimberg 			  isert_np, event);
857ca6c1d82SSagi Grimberg 	}
858ca6c1d82SSagi Grimberg 
8593b726ae2SSagi Grimberg 	return -1;
8603b726ae2SSagi Grimberg }
8613b726ae2SSagi Grimberg 
862ca6c1d82SSagi Grimberg static int
863ca6c1d82SSagi Grimberg isert_disconnected_handler(struct rdma_cm_id *cma_id,
864ca6c1d82SSagi Grimberg 			   enum rdma_cm_event_type event)
865ca6c1d82SSagi Grimberg {
866ca6c1d82SSagi Grimberg 	struct isert_np *isert_np = cma_id->context;
867ca6c1d82SSagi Grimberg 	struct isert_conn *isert_conn;
8682f1b6b7dSSagi Grimberg 	bool terminating = false;
869ca6c1d82SSagi Grimberg 
870ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id == cma_id)
871ca6c1d82SSagi Grimberg 		return isert_np_cma_handler(cma_id->context, event);
872ca6c1d82SSagi Grimberg 
87319e2090fSSagi Grimberg 	isert_conn = cma_id->qp->qp_context;
874b8d26b3bSNicholas Bellinger 
875dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
8762f1b6b7dSSagi Grimberg 	terminating = (isert_conn->state == ISER_CONN_TERMINATING);
877128e9cc8SSagi Grimberg 	isert_conn_terminate(isert_conn);
878dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
879128e9cc8SSagi Grimberg 
880dac6ab30SSagi Grimberg 	isert_info("conn %p completing wait\n", isert_conn);
881dac6ab30SSagi Grimberg 	complete(&isert_conn->wait);
8823b726ae2SSagi Grimberg 
8832f1b6b7dSSagi Grimberg 	if (terminating)
8842f1b6b7dSSagi Grimberg 		goto out;
8852f1b6b7dSSagi Grimberg 
886ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
887bd379220SJenny Derzhavetz 	if (!list_empty(&isert_conn->node)) {
888bd379220SJenny Derzhavetz 		list_del_init(&isert_conn->node);
8892f1b6b7dSSagi Grimberg 		isert_put_conn(isert_conn);
8902f1b6b7dSSagi Grimberg 		queue_work(isert_release_wq, &isert_conn->release_work);
8912f1b6b7dSSagi Grimberg 	}
892ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
8932f1b6b7dSSagi Grimberg 
8942f1b6b7dSSagi Grimberg out:
8953b726ae2SSagi Grimberg 	return 0;
896b8d26b3bSNicholas Bellinger }
897b8d26b3bSNicholas Bellinger 
8984a579da2SSagi Grimberg static int
899954f2372SSagi Grimberg isert_connect_error(struct rdma_cm_id *cma_id)
900954f2372SSagi Grimberg {
90119e2090fSSagi Grimberg 	struct isert_conn *isert_conn = cma_id->qp->qp_context;
902954f2372SSagi Grimberg 
903bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
904dac6ab30SSagi Grimberg 	isert_conn->cm_id = NULL;
905954f2372SSagi Grimberg 	isert_put_conn(isert_conn);
9064a579da2SSagi Grimberg 
9074a579da2SSagi Grimberg 	return -1;
908954f2372SSagi Grimberg }
909954f2372SSagi Grimberg 
910b8d26b3bSNicholas Bellinger static int
911b8d26b3bSNicholas Bellinger isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
912b8d26b3bSNicholas Bellinger {
913b8d26b3bSNicholas Bellinger 	int ret = 0;
914b8d26b3bSNicholas Bellinger 
915ea8a1616SSagi Grimberg 	isert_info("%s (%d): status %d id %p np %p\n",
916ea8a1616SSagi Grimberg 		   rdma_event_msg(event->event), event->event,
9174c22e07fSSagi Grimberg 		   event->status, cma_id, cma_id->context);
918b8d26b3bSNicholas Bellinger 
919b8d26b3bSNicholas Bellinger 	switch (event->event) {
920b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_REQUEST:
921b8d26b3bSNicholas Bellinger 		ret = isert_connect_request(cma_id, event);
9223b726ae2SSagi Grimberg 		if (ret)
9234c22e07fSSagi Grimberg 			isert_err("failed handle connect request %d\n", ret);
924b8d26b3bSNicholas Bellinger 		break;
925b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_ESTABLISHED:
926b8d26b3bSNicholas Bellinger 		isert_connected_handler(cma_id);
927b8d26b3bSNicholas Bellinger 		break;
92888c4015fSSagi Grimberg 	case RDMA_CM_EVENT_ADDR_CHANGE:    /* FALLTHRU */
92988c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DISCONNECTED:   /* FALLTHRU */
93088c4015fSSagi Grimberg 	case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
93188c4015fSSagi Grimberg 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:  /* FALLTHRU */
932ca6c1d82SSagi Grimberg 		ret = isert_disconnected_handler(cma_id, event->event);
933b8d26b3bSNicholas Bellinger 		break;
934954f2372SSagi Grimberg 	case RDMA_CM_EVENT_REJECTED:       /* FALLTHRU */
935954f2372SSagi Grimberg 	case RDMA_CM_EVENT_UNREACHABLE:    /* FALLTHRU */
936b8d26b3bSNicholas Bellinger 	case RDMA_CM_EVENT_CONNECT_ERROR:
9374a579da2SSagi Grimberg 		ret = isert_connect_error(cma_id);
938954f2372SSagi Grimberg 		break;
939b8d26b3bSNicholas Bellinger 	default:
94024f412ddSSagi Grimberg 		isert_err("Unhandled RDMA CMA event: %d\n", event->event);
941b8d26b3bSNicholas Bellinger 		break;
942b8d26b3bSNicholas Bellinger 	}
943b8d26b3bSNicholas Bellinger 
944b8d26b3bSNicholas Bellinger 	return ret;
945b8d26b3bSNicholas Bellinger }
946b8d26b3bSNicholas Bellinger 
947b8d26b3bSNicholas Bellinger static int
9484366b19cSJenny Derzhavetz isert_post_recvm(struct isert_conn *isert_conn, u32 count)
949b8d26b3bSNicholas Bellinger {
950b8d26b3bSNicholas Bellinger 	struct ib_recv_wr *rx_wr, *rx_wr_failed;
951b8d26b3bSNicholas Bellinger 	int i, ret;
952b8d26b3bSNicholas Bellinger 	struct iser_rx_desc *rx_desc;
953b8d26b3bSNicholas Bellinger 
954dac6ab30SSagi Grimberg 	for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
9554366b19cSJenny Derzhavetz 		rx_desc = &isert_conn->rx_descs[i];
956b0a191e7SSagi Grimberg 		rx_wr->wr_id = (uintptr_t)rx_desc;
957b8d26b3bSNicholas Bellinger 		rx_wr->sg_list = &rx_desc->rx_sg;
958b8d26b3bSNicholas Bellinger 		rx_wr->num_sge = 1;
959b8d26b3bSNicholas Bellinger 		rx_wr->next = rx_wr + 1;
960b8d26b3bSNicholas Bellinger 	}
961b8d26b3bSNicholas Bellinger 	rx_wr--;
962b8d26b3bSNicholas Bellinger 	rx_wr->next = NULL; /* mark end of work requests list */
963b8d26b3bSNicholas Bellinger 
964b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count += count;
965dac6ab30SSagi Grimberg 	ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
966b8d26b3bSNicholas Bellinger 			   &rx_wr_failed);
967b8d26b3bSNicholas Bellinger 	if (ret) {
96824f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
969b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count -= count;
970b8d26b3bSNicholas Bellinger 	}
9714366b19cSJenny Derzhavetz 
9724366b19cSJenny Derzhavetz 	return ret;
9734366b19cSJenny Derzhavetz }
9744366b19cSJenny Derzhavetz 
9754366b19cSJenny Derzhavetz static int
9764366b19cSJenny Derzhavetz isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
9774366b19cSJenny Derzhavetz {
9784366b19cSJenny Derzhavetz 	struct ib_recv_wr *rx_wr_failed, rx_wr;
9794366b19cSJenny Derzhavetz 	int ret;
9804366b19cSJenny Derzhavetz 
9814366b19cSJenny Derzhavetz 	rx_wr.wr_id = (uintptr_t)rx_desc;
9824366b19cSJenny Derzhavetz 	rx_wr.sg_list = &rx_desc->rx_sg;
9834366b19cSJenny Derzhavetz 	rx_wr.num_sge = 1;
9844366b19cSJenny Derzhavetz 	rx_wr.next = NULL;
9854366b19cSJenny Derzhavetz 
9864366b19cSJenny Derzhavetz 	isert_conn->post_recv_buf_count++;
9874366b19cSJenny Derzhavetz 	ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
9884366b19cSJenny Derzhavetz 	if (ret) {
9894366b19cSJenny Derzhavetz 		isert_err("ib_post_recv() failed with ret: %d\n", ret);
9904366b19cSJenny Derzhavetz 		isert_conn->post_recv_buf_count--;
9914366b19cSJenny Derzhavetz 	}
9924366b19cSJenny Derzhavetz 
993b8d26b3bSNicholas Bellinger 	return ret;
994b8d26b3bSNicholas Bellinger }
995b8d26b3bSNicholas Bellinger 
996b8d26b3bSNicholas Bellinger static int
997b8d26b3bSNicholas Bellinger isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
998b8d26b3bSNicholas Bellinger {
999dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1000b8d26b3bSNicholas Bellinger 	struct ib_send_wr send_wr, *send_wr_failed;
1001b8d26b3bSNicholas Bellinger 	int ret;
1002b8d26b3bSNicholas Bellinger 
1003b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1004b8d26b3bSNicholas Bellinger 				      ISER_HEADERS_LEN, DMA_TO_DEVICE);
1005b8d26b3bSNicholas Bellinger 
1006b8d26b3bSNicholas Bellinger 	send_wr.next	= NULL;
1007b0a191e7SSagi Grimberg 	send_wr.wr_id	= (uintptr_t)tx_desc;
1008b8d26b3bSNicholas Bellinger 	send_wr.sg_list	= tx_desc->tx_sg;
1009b8d26b3bSNicholas Bellinger 	send_wr.num_sge	= tx_desc->num_sge;
1010b8d26b3bSNicholas Bellinger 	send_wr.opcode	= IB_WR_SEND;
1011b8d26b3bSNicholas Bellinger 	send_wr.send_flags = IB_SEND_SIGNALED;
1012b8d26b3bSNicholas Bellinger 
1013dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
1014bdf20e72SSagi Grimberg 	if (ret)
101524f412ddSSagi Grimberg 		isert_err("ib_post_send() failed, ret: %d\n", ret);
1016b8d26b3bSNicholas Bellinger 
1017b8d26b3bSNicholas Bellinger 	return ret;
1018b8d26b3bSNicholas Bellinger }
1019b8d26b3bSNicholas Bellinger 
1020b8d26b3bSNicholas Bellinger static void
1021b8d26b3bSNicholas Bellinger isert_create_send_desc(struct isert_conn *isert_conn,
1022b8d26b3bSNicholas Bellinger 		       struct isert_cmd *isert_cmd,
1023b8d26b3bSNicholas Bellinger 		       struct iser_tx_desc *tx_desc)
1024b8d26b3bSNicholas Bellinger {
1025dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
102667cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1027b8d26b3bSNicholas Bellinger 
1028b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1029b8d26b3bSNicholas Bellinger 				   ISER_HEADERS_LEN, DMA_TO_DEVICE);
1030b8d26b3bSNicholas Bellinger 
1031*d3cf81f9SSagi Grimberg 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
1032*d3cf81f9SSagi Grimberg 	tx_desc->iser_header.flags = ISCSI_CTRL;
1033b8d26b3bSNicholas Bellinger 
1034b8d26b3bSNicholas Bellinger 	tx_desc->num_sge = 1;
1035b8d26b3bSNicholas Bellinger 	tx_desc->isert_cmd = isert_cmd;
1036b8d26b3bSNicholas Bellinger 
103734efc7dfSJason Gunthorpe 	if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
103834efc7dfSJason Gunthorpe 		tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
103924f412ddSSagi Grimberg 		isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
1040b8d26b3bSNicholas Bellinger 	}
1041b8d26b3bSNicholas Bellinger }
1042b8d26b3bSNicholas Bellinger 
1043b8d26b3bSNicholas Bellinger static int
1044b8d26b3bSNicholas Bellinger isert_init_tx_hdrs(struct isert_conn *isert_conn,
1045b8d26b3bSNicholas Bellinger 		   struct iser_tx_desc *tx_desc)
1046b8d26b3bSNicholas Bellinger {
1047dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
104867cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1049b8d26b3bSNicholas Bellinger 	u64 dma_addr;
1050b8d26b3bSNicholas Bellinger 
1051b8d26b3bSNicholas Bellinger 	dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1052b8d26b3bSNicholas Bellinger 			ISER_HEADERS_LEN, DMA_TO_DEVICE);
1053b8d26b3bSNicholas Bellinger 	if (ib_dma_mapping_error(ib_dev, dma_addr)) {
105424f412ddSSagi Grimberg 		isert_err("ib_dma_mapping_error() failed\n");
1055b8d26b3bSNicholas Bellinger 		return -ENOMEM;
1056b8d26b3bSNicholas Bellinger 	}
1057b8d26b3bSNicholas Bellinger 
1058b8d26b3bSNicholas Bellinger 	tx_desc->dma_addr = dma_addr;
1059b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].addr	= tx_desc->dma_addr;
1060b8d26b3bSNicholas Bellinger 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
106134efc7dfSJason Gunthorpe 	tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
1062b8d26b3bSNicholas Bellinger 
10634c22e07fSSagi Grimberg 	isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
10644c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
10654c22e07fSSagi Grimberg 		  tx_desc->tx_sg[0].lkey);
1066b8d26b3bSNicholas Bellinger 
1067b8d26b3bSNicholas Bellinger 	return 0;
1068b8d26b3bSNicholas Bellinger }
1069b8d26b3bSNicholas Bellinger 
1070b8d26b3bSNicholas Bellinger static void
107195b60f07SNicholas Bellinger isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
107268a86deeSSagi Grimberg 		   struct ib_send_wr *send_wr)
1073b8d26b3bSNicholas Bellinger {
107495b60f07SNicholas Bellinger 	struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
107595b60f07SNicholas Bellinger 
1076b8d26b3bSNicholas Bellinger 	isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1077b0a191e7SSagi Grimberg 	send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
1078b8d26b3bSNicholas Bellinger 	send_wr->opcode = IB_WR_SEND;
107995b60f07SNicholas Bellinger 	send_wr->sg_list = &tx_desc->tx_sg[0];
1080b8d26b3bSNicholas Bellinger 	send_wr->num_sge = isert_cmd->tx_desc.num_sge;
108195b60f07SNicholas Bellinger 	send_wr->send_flags = IB_SEND_SIGNALED;
1082b8d26b3bSNicholas Bellinger }
1083b8d26b3bSNicholas Bellinger 
1084b8d26b3bSNicholas Bellinger static int
1085b8d26b3bSNicholas Bellinger isert_rdma_post_recvl(struct isert_conn *isert_conn)
1086b8d26b3bSNicholas Bellinger {
1087b8d26b3bSNicholas Bellinger 	struct ib_recv_wr rx_wr, *rx_wr_fail;
1088b8d26b3bSNicholas Bellinger 	struct ib_sge sge;
1089b8d26b3bSNicholas Bellinger 	int ret;
1090b8d26b3bSNicholas Bellinger 
1091b8d26b3bSNicholas Bellinger 	memset(&sge, 0, sizeof(struct ib_sge));
1092b8d26b3bSNicholas Bellinger 	sge.addr = isert_conn->login_req_dma;
1093b8d26b3bSNicholas Bellinger 	sge.length = ISER_RX_LOGIN_SIZE;
109434efc7dfSJason Gunthorpe 	sge.lkey = isert_conn->device->pd->local_dma_lkey;
1095b8d26b3bSNicholas Bellinger 
109624f412ddSSagi Grimberg 	isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
1097b8d26b3bSNicholas Bellinger 		sge.addr, sge.length, sge.lkey);
1098b8d26b3bSNicholas Bellinger 
1099b8d26b3bSNicholas Bellinger 	memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1100b0a191e7SSagi Grimberg 	rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
1101b8d26b3bSNicholas Bellinger 	rx_wr.sg_list = &sge;
1102b8d26b3bSNicholas Bellinger 	rx_wr.num_sge = 1;
1103b8d26b3bSNicholas Bellinger 
1104b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count++;
1105dac6ab30SSagi Grimberg 	ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
1106b8d26b3bSNicholas Bellinger 	if (ret) {
110724f412ddSSagi Grimberg 		isert_err("ib_post_recv() failed: %d\n", ret);
1108b8d26b3bSNicholas Bellinger 		isert_conn->post_recv_buf_count--;
1109b8d26b3bSNicholas Bellinger 	}
1110b8d26b3bSNicholas Bellinger 
1111b8d26b3bSNicholas Bellinger 	return ret;
1112b8d26b3bSNicholas Bellinger }
1113b8d26b3bSNicholas Bellinger 
1114b8d26b3bSNicholas Bellinger static int
1115b8d26b3bSNicholas Bellinger isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1116b8d26b3bSNicholas Bellinger 		   u32 length)
1117b8d26b3bSNicholas Bellinger {
1118b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
1119dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
112067cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
1121dac6ab30SSagi Grimberg 	struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
1122b8d26b3bSNicholas Bellinger 	int ret;
1123b8d26b3bSNicholas Bellinger 
1124b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, NULL, tx_desc);
1125b8d26b3bSNicholas Bellinger 
1126b8d26b3bSNicholas Bellinger 	memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1127b8d26b3bSNicholas Bellinger 	       sizeof(struct iscsi_hdr));
1128b8d26b3bSNicholas Bellinger 
1129b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, tx_desc);
1130b8d26b3bSNicholas Bellinger 
1131b8d26b3bSNicholas Bellinger 	if (length > 0) {
1132b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1133b8d26b3bSNicholas Bellinger 
1134b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1135b8d26b3bSNicholas Bellinger 					   length, DMA_TO_DEVICE);
1136b8d26b3bSNicholas Bellinger 
1137b8d26b3bSNicholas Bellinger 		memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1138b8d26b3bSNicholas Bellinger 
1139b8d26b3bSNicholas Bellinger 		ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1140b8d26b3bSNicholas Bellinger 					      length, DMA_TO_DEVICE);
1141b8d26b3bSNicholas Bellinger 
1142b8d26b3bSNicholas Bellinger 		tx_dsg->addr	= isert_conn->login_rsp_dma;
1143b8d26b3bSNicholas Bellinger 		tx_dsg->length	= length;
114434efc7dfSJason Gunthorpe 		tx_dsg->lkey	= isert_conn->device->pd->local_dma_lkey;
1145b8d26b3bSNicholas Bellinger 		tx_desc->num_sge = 2;
1146b8d26b3bSNicholas Bellinger 	}
1147b8d26b3bSNicholas Bellinger 	if (!login->login_failed) {
1148b8d26b3bSNicholas Bellinger 		if (login->login_complete) {
1149e0546fc1SSagi Grimberg 			if (!conn->sess->sess_ops->SessionType &&
1150dac6ab30SSagi Grimberg 			    isert_conn->device->use_fastreg) {
1151570db170SSagi Grimberg 				ret = isert_conn_create_fastreg_pool(isert_conn);
1152f46d6a8aSNicholas Bellinger 				if (ret) {
115324f412ddSSagi Grimberg 					isert_err("Conn: %p failed to create"
1154f46d6a8aSNicholas Bellinger 					       " fastreg pool\n", isert_conn);
1155f46d6a8aSNicholas Bellinger 					return ret;
1156f46d6a8aSNicholas Bellinger 				}
1157f46d6a8aSNicholas Bellinger 			}
1158f46d6a8aSNicholas Bellinger 
1159b8d26b3bSNicholas Bellinger 			ret = isert_alloc_rx_descriptors(isert_conn);
1160b8d26b3bSNicholas Bellinger 			if (ret)
1161b8d26b3bSNicholas Bellinger 				return ret;
1162b8d26b3bSNicholas Bellinger 
11634366b19cSJenny Derzhavetz 			ret = isert_post_recvm(isert_conn,
11644366b19cSJenny Derzhavetz 					       ISERT_QP_MAX_RECV_DTOS);
1165b8d26b3bSNicholas Bellinger 			if (ret)
1166b8d26b3bSNicholas Bellinger 				return ret;
1167b8d26b3bSNicholas Bellinger 
1168128e9cc8SSagi Grimberg 			/* Now we are in FULL_FEATURE phase */
1169dac6ab30SSagi Grimberg 			mutex_lock(&isert_conn->mutex);
1170128e9cc8SSagi Grimberg 			isert_conn->state = ISER_CONN_FULL_FEATURE;
1171dac6ab30SSagi Grimberg 			mutex_unlock(&isert_conn->mutex);
1172b8d26b3bSNicholas Bellinger 			goto post_send;
1173b8d26b3bSNicholas Bellinger 		}
1174b8d26b3bSNicholas Bellinger 
1175b8d26b3bSNicholas Bellinger 		ret = isert_rdma_post_recvl(isert_conn);
1176b8d26b3bSNicholas Bellinger 		if (ret)
1177b8d26b3bSNicholas Bellinger 			return ret;
1178b8d26b3bSNicholas Bellinger 	}
1179b8d26b3bSNicholas Bellinger post_send:
1180b8d26b3bSNicholas Bellinger 	ret = isert_post_send(isert_conn, tx_desc);
1181b8d26b3bSNicholas Bellinger 	if (ret)
1182b8d26b3bSNicholas Bellinger 		return ret;
1183b8d26b3bSNicholas Bellinger 
1184b8d26b3bSNicholas Bellinger 	return 0;
1185b8d26b3bSNicholas Bellinger }
1186b8d26b3bSNicholas Bellinger 
1187b8d26b3bSNicholas Bellinger static void
11882371e5daSSagi Grimberg isert_rx_login_req(struct isert_conn *isert_conn)
1189b8d26b3bSNicholas Bellinger {
11902371e5daSSagi Grimberg 	struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
11912371e5daSSagi Grimberg 	int rx_buflen = isert_conn->login_req_len;
1192b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1193b8d26b3bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1194b8d26b3bSNicholas Bellinger 	int size;
1195b8d26b3bSNicholas Bellinger 
119624f412ddSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
11972371e5daSSagi Grimberg 
11982371e5daSSagi Grimberg 	WARN_ON_ONCE(!login);
1199b8d26b3bSNicholas Bellinger 
1200b8d26b3bSNicholas Bellinger 	if (login->first_request) {
1201b8d26b3bSNicholas Bellinger 		struct iscsi_login_req *login_req =
1202b8d26b3bSNicholas Bellinger 			(struct iscsi_login_req *)&rx_desc->iscsi_header;
1203b8d26b3bSNicholas Bellinger 		/*
1204b8d26b3bSNicholas Bellinger 		 * Setup the initial iscsi_login values from the leading
1205b8d26b3bSNicholas Bellinger 		 * login request PDU.
1206b8d26b3bSNicholas Bellinger 		 */
1207b8d26b3bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1208b8d26b3bSNicholas Bellinger 		login->current_stage =
1209b8d26b3bSNicholas Bellinger 			(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1210b8d26b3bSNicholas Bellinger 			 >> 2;
1211b8d26b3bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1212b8d26b3bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1213b8d26b3bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1214b8d26b3bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1215b8d26b3bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1216b8d26b3bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1217b8d26b3bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1218b8d26b3bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1219b8d26b3bSNicholas Bellinger 	}
1220b8d26b3bSNicholas Bellinger 
1221b8d26b3bSNicholas Bellinger 	memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1222b8d26b3bSNicholas Bellinger 
1223b8d26b3bSNicholas Bellinger 	size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
12244c22e07fSSagi Grimberg 	isert_dbg("Using login payload size: %d, rx_buflen: %d "
12254c22e07fSSagi Grimberg 		  "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
12264c22e07fSSagi Grimberg 		  MAX_KEY_VALUE_PAIRS);
1227b8d26b3bSNicholas Bellinger 	memcpy(login->req_buf, &rx_desc->data[0], size);
1228b8d26b3bSNicholas Bellinger 
12296faaa85fSNicholas Bellinger 	if (login->first_request) {
1230dac6ab30SSagi Grimberg 		complete(&isert_conn->login_comp);
12316faaa85fSNicholas Bellinger 		return;
12326faaa85fSNicholas Bellinger 	}
12336faaa85fSNicholas Bellinger 	schedule_delayed_work(&conn->login_work, 0);
1234b8d26b3bSNicholas Bellinger }
1235b8d26b3bSNicholas Bellinger 
1236b8d26b3bSNicholas Bellinger static struct iscsi_cmd
12374366b19cSJenny Derzhavetz *isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
1238b8d26b3bSNicholas Bellinger {
12396700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
1240b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1241d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd;
1242b8d26b3bSNicholas Bellinger 
1243676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1244d703ce2fSNicholas Bellinger 	if (!cmd) {
124524f412ddSSagi Grimberg 		isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1246b8d26b3bSNicholas Bellinger 		return NULL;
1247b8d26b3bSNicholas Bellinger 	}
1248d703ce2fSNicholas Bellinger 	isert_cmd = iscsit_priv_cmd(cmd);
1249b8d26b3bSNicholas Bellinger 	isert_cmd->conn = isert_conn;
1250d703ce2fSNicholas Bellinger 	isert_cmd->iscsi_cmd = cmd;
12514366b19cSJenny Derzhavetz 	isert_cmd->rx_desc = rx_desc;
1252b8d26b3bSNicholas Bellinger 
1253d703ce2fSNicholas Bellinger 	return cmd;
1254b8d26b3bSNicholas Bellinger }
1255b8d26b3bSNicholas Bellinger 
1256b8d26b3bSNicholas Bellinger static int
1257b8d26b3bSNicholas Bellinger isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1258d703ce2fSNicholas Bellinger 		      struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1259d703ce2fSNicholas Bellinger 		      struct iser_rx_desc *rx_desc, unsigned char *buf)
1260b8d26b3bSNicholas Bellinger {
1261b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1262b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1263b8d26b3bSNicholas Bellinger 	int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1264b8d26b3bSNicholas Bellinger 	bool dump_payload = false;
12659fd60088SJenny Derzhavetz 	unsigned int data_len;
1266b8d26b3bSNicholas Bellinger 
1267b8d26b3bSNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1268b8d26b3bSNicholas Bellinger 	if (rc < 0)
1269b8d26b3bSNicholas Bellinger 		return rc;
1270b8d26b3bSNicholas Bellinger 
1271b8d26b3bSNicholas Bellinger 	imm_data = cmd->immediate_data;
1272b8d26b3bSNicholas Bellinger 	imm_data_len = cmd->first_burst_len;
1273b8d26b3bSNicholas Bellinger 	unsol_data = cmd->unsolicited_data;
12749fd60088SJenny Derzhavetz 	data_len = cmd->se_cmd.data_length;
1275b8d26b3bSNicholas Bellinger 
12769fd60088SJenny Derzhavetz 	if (imm_data && imm_data_len == data_len)
12779fd60088SJenny Derzhavetz 		cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1278b8d26b3bSNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1279b8d26b3bSNicholas Bellinger 	if (rc < 0) {
1280b8d26b3bSNicholas Bellinger 		return 0;
1281b8d26b3bSNicholas Bellinger 	} else if (rc > 0) {
1282b8d26b3bSNicholas Bellinger 		dump_payload = true;
1283b8d26b3bSNicholas Bellinger 		goto sequence_cmd;
1284b8d26b3bSNicholas Bellinger 	}
1285b8d26b3bSNicholas Bellinger 
1286b8d26b3bSNicholas Bellinger 	if (!imm_data)
1287b8d26b3bSNicholas Bellinger 		return 0;
1288b8d26b3bSNicholas Bellinger 
12899fd60088SJenny Derzhavetz 	if (imm_data_len != data_len) {
1290b8d26b3bSNicholas Bellinger 		sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
12919fd60088SJenny Derzhavetz 		sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
12929fd60088SJenny Derzhavetz 				    &rx_desc->data[0], imm_data_len);
12939fd60088SJenny Derzhavetz 		isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
12949fd60088SJenny Derzhavetz 			  sg_nents, imm_data_len);
12959fd60088SJenny Derzhavetz 	} else {
12969fd60088SJenny Derzhavetz 		sg_init_table(&isert_cmd->sg, 1);
12979fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_sg = &isert_cmd->sg;
12989fd60088SJenny Derzhavetz 		cmd->se_cmd.t_data_nents = 1;
12999fd60088SJenny Derzhavetz 		sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
13009fd60088SJenny Derzhavetz 		isert_dbg("Transfer Immediate imm_data_len: %d\n",
13019fd60088SJenny Derzhavetz 			  imm_data_len);
13029fd60088SJenny Derzhavetz 	}
1303b8d26b3bSNicholas Bellinger 
1304b8d26b3bSNicholas Bellinger 	cmd->write_data_done += imm_data_len;
1305b8d26b3bSNicholas Bellinger 
1306b8d26b3bSNicholas Bellinger 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
1307b8d26b3bSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1308b8d26b3bSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1309b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1310b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1311b8d26b3bSNicholas Bellinger 	}
1312b8d26b3bSNicholas Bellinger 
1313b8d26b3bSNicholas Bellinger sequence_cmd:
1314561bf158SNicholas Bellinger 	rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1315b8d26b3bSNicholas Bellinger 
1316b8d26b3bSNicholas Bellinger 	if (!rc && dump_payload == false && unsol_data)
1317b8d26b3bSNicholas Bellinger 		iscsit_set_unsoliticed_dataout(cmd);
13186cc44a6fSNicholas Bellinger 	else if (dump_payload && imm_data)
1319afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1320b8d26b3bSNicholas Bellinger 
1321b8d26b3bSNicholas Bellinger 	return 0;
1322b8d26b3bSNicholas Bellinger }
1323b8d26b3bSNicholas Bellinger 
1324b8d26b3bSNicholas Bellinger static int
1325b8d26b3bSNicholas Bellinger isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1326b8d26b3bSNicholas Bellinger 			   struct iser_rx_desc *rx_desc, unsigned char *buf)
1327b8d26b3bSNicholas Bellinger {
1328b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start;
1329b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1330b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1331b8d26b3bSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
1332b8d26b3bSNicholas Bellinger 	u32 unsol_data_len = ntoh24(hdr->dlength);
1333b8d26b3bSNicholas Bellinger 	int rc, sg_nents, sg_off, page_off;
1334b8d26b3bSNicholas Bellinger 
1335b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1336b8d26b3bSNicholas Bellinger 	if (rc < 0)
1337b8d26b3bSNicholas Bellinger 		return rc;
1338b8d26b3bSNicholas Bellinger 	else if (!cmd)
1339b8d26b3bSNicholas Bellinger 		return 0;
1340b8d26b3bSNicholas Bellinger 	/*
1341b8d26b3bSNicholas Bellinger 	 * FIXME: Unexpected unsolicited_data out
1342b8d26b3bSNicholas Bellinger 	 */
1343b8d26b3bSNicholas Bellinger 	if (!cmd->unsolicited_data) {
134424f412ddSSagi Grimberg 		isert_err("Received unexpected solicited data payload\n");
1345b8d26b3bSNicholas Bellinger 		dump_stack();
1346b8d26b3bSNicholas Bellinger 		return -1;
1347b8d26b3bSNicholas Bellinger 	}
1348b8d26b3bSNicholas Bellinger 
13494c22e07fSSagi Grimberg 	isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
13504c22e07fSSagi Grimberg 		  "write_data_done: %u, data_length: %u\n",
13514c22e07fSSagi Grimberg 		  unsol_data_len,  cmd->write_data_done,
13524c22e07fSSagi Grimberg 		  cmd->se_cmd.data_length);
1353b8d26b3bSNicholas Bellinger 
1354b8d26b3bSNicholas Bellinger 	sg_off = cmd->write_data_done / PAGE_SIZE;
1355b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1356b8d26b3bSNicholas Bellinger 	sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1357b8d26b3bSNicholas Bellinger 	page_off = cmd->write_data_done % PAGE_SIZE;
1358b8d26b3bSNicholas Bellinger 	/*
1359b8d26b3bSNicholas Bellinger 	 * FIXME: Non page-aligned unsolicited_data out
1360b8d26b3bSNicholas Bellinger 	 */
1361b8d26b3bSNicholas Bellinger 	if (page_off) {
13624c22e07fSSagi Grimberg 		isert_err("unexpected non-page aligned data payload\n");
1363b8d26b3bSNicholas Bellinger 		dump_stack();
1364b8d26b3bSNicholas Bellinger 		return -1;
1365b8d26b3bSNicholas Bellinger 	}
13664c22e07fSSagi Grimberg 	isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
13674c22e07fSSagi Grimberg 		  "sg_nents: %u from %p %u\n", sg_start, sg_off,
13684c22e07fSSagi Grimberg 		  sg_nents, &rx_desc->data[0], unsol_data_len);
1369b8d26b3bSNicholas Bellinger 
1370b8d26b3bSNicholas Bellinger 	sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1371b8d26b3bSNicholas Bellinger 			    unsol_data_len);
1372b8d26b3bSNicholas Bellinger 
1373b8d26b3bSNicholas Bellinger 	rc = iscsit_check_dataout_payload(cmd, hdr, false);
1374b8d26b3bSNicholas Bellinger 	if (rc < 0)
1375b8d26b3bSNicholas Bellinger 		return rc;
1376b8d26b3bSNicholas Bellinger 
13774366b19cSJenny Derzhavetz 	/*
13784366b19cSJenny Derzhavetz 	 * multiple data-outs on the same command can arrive -
13794366b19cSJenny Derzhavetz 	 * so post the buffer before hand
13804366b19cSJenny Derzhavetz 	 */
13814366b19cSJenny Derzhavetz 	rc = isert_post_recv(isert_conn, rx_desc);
13824366b19cSJenny Derzhavetz 	if (rc) {
13834366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", rc);
13844366b19cSJenny Derzhavetz 		return rc;
13854366b19cSJenny Derzhavetz 	}
1386b8d26b3bSNicholas Bellinger 	return 0;
1387b8d26b3bSNicholas Bellinger }
1388b8d26b3bSNicholas Bellinger 
1389b8d26b3bSNicholas Bellinger static int
1390778de368SNicholas Bellinger isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1391d703ce2fSNicholas Bellinger 		     struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1392d703ce2fSNicholas Bellinger 		     unsigned char *buf)
1393778de368SNicholas Bellinger {
1394778de368SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1395778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1396778de368SNicholas Bellinger 	int rc;
1397778de368SNicholas Bellinger 
1398778de368SNicholas Bellinger 	rc = iscsit_setup_nop_out(conn, cmd, hdr);
1399778de368SNicholas Bellinger 	if (rc < 0)
1400778de368SNicholas Bellinger 		return rc;
1401778de368SNicholas Bellinger 	/*
1402778de368SNicholas Bellinger 	 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1403778de368SNicholas Bellinger 	 */
1404778de368SNicholas Bellinger 
1405778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1406778de368SNicholas Bellinger }
1407778de368SNicholas Bellinger 
1408778de368SNicholas Bellinger static int
1409adb54c29SNicholas Bellinger isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1410d703ce2fSNicholas Bellinger 		      struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1411d703ce2fSNicholas Bellinger 		      struct iscsi_text *hdr)
1412adb54c29SNicholas Bellinger {
1413adb54c29SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1414adb54c29SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1415adb54c29SNicholas Bellinger 	int rc;
1416b44a2b67SSagi Grimberg 	unsigned char *text_in = NULL;
1417adb54c29SNicholas Bellinger 
1418adb54c29SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1419adb54c29SNicholas Bellinger 	if (rc < 0)
1420adb54c29SNicholas Bellinger 		return rc;
1421adb54c29SNicholas Bellinger 
1422b44a2b67SSagi Grimberg 	if (payload_length) {
1423adb54c29SNicholas Bellinger 		text_in = kzalloc(payload_length, GFP_KERNEL);
1424adb54c29SNicholas Bellinger 		if (!text_in) {
142524f412ddSSagi Grimberg 			isert_err("Unable to allocate text_in of payload_length: %u\n",
1426adb54c29SNicholas Bellinger 				  payload_length);
1427adb54c29SNicholas Bellinger 			return -ENOMEM;
1428adb54c29SNicholas Bellinger 		}
1429b44a2b67SSagi Grimberg 	}
1430adb54c29SNicholas Bellinger 	cmd->text_in_ptr = text_in;
1431adb54c29SNicholas Bellinger 
1432adb54c29SNicholas Bellinger 	memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1433adb54c29SNicholas Bellinger 
1434adb54c29SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
1435adb54c29SNicholas Bellinger }
1436adb54c29SNicholas Bellinger 
1437adb54c29SNicholas Bellinger static int
1438b8d26b3bSNicholas Bellinger isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1439b8d26b3bSNicholas Bellinger 		uint32_t read_stag, uint64_t read_va,
1440b8d26b3bSNicholas Bellinger 		uint32_t write_stag, uint64_t write_va)
1441b8d26b3bSNicholas Bellinger {
1442b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1443b8d26b3bSNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1444b8d26b3bSNicholas Bellinger 	struct iscsi_cmd *cmd;
1445b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd;
1446b8d26b3bSNicholas Bellinger 	int ret = -EINVAL;
1447b8d26b3bSNicholas Bellinger 	u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1448b8d26b3bSNicholas Bellinger 
1449fb140271SSagi Grimberg 	if (conn->sess->sess_ops->SessionType &&
1450ca40d24eSNicholas Bellinger 	   (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
145124f412ddSSagi Grimberg 		isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1452ca40d24eSNicholas Bellinger 			  " ignoring\n", opcode);
1453ca40d24eSNicholas Bellinger 		return 0;
1454ca40d24eSNicholas Bellinger 	}
1455ca40d24eSNicholas Bellinger 
1456b8d26b3bSNicholas Bellinger 	switch (opcode) {
1457b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
14584366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1459b8d26b3bSNicholas Bellinger 		if (!cmd)
1460b8d26b3bSNicholas Bellinger 			break;
1461b8d26b3bSNicholas Bellinger 
1462d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1463b8d26b3bSNicholas Bellinger 		isert_cmd->read_stag = read_stag;
1464b8d26b3bSNicholas Bellinger 		isert_cmd->read_va = read_va;
1465b8d26b3bSNicholas Bellinger 		isert_cmd->write_stag = write_stag;
1466b8d26b3bSNicholas Bellinger 		isert_cmd->write_va = write_va;
1467b8d26b3bSNicholas Bellinger 
1468d703ce2fSNicholas Bellinger 		ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1469b8d26b3bSNicholas Bellinger 					rx_desc, (unsigned char *)hdr);
1470b8d26b3bSNicholas Bellinger 		break;
1471b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
14724366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1473b8d26b3bSNicholas Bellinger 		if (!cmd)
1474b8d26b3bSNicholas Bellinger 			break;
1475b8d26b3bSNicholas Bellinger 
1476d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1477d703ce2fSNicholas Bellinger 		ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1478778de368SNicholas Bellinger 					   rx_desc, (unsigned char *)hdr);
1479b8d26b3bSNicholas Bellinger 		break;
1480b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
1481b8d26b3bSNicholas Bellinger 		ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1482b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1483b8d26b3bSNicholas Bellinger 		break;
1484b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
14854366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1486b8d26b3bSNicholas Bellinger 		if (!cmd)
1487b8d26b3bSNicholas Bellinger 			break;
1488b8d26b3bSNicholas Bellinger 
1489b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1490b8d26b3bSNicholas Bellinger 						(unsigned char *)hdr);
1491b8d26b3bSNicholas Bellinger 		break;
1492b8d26b3bSNicholas Bellinger 	case ISCSI_OP_LOGOUT:
14934366b19cSJenny Derzhavetz 		cmd = isert_allocate_cmd(conn, rx_desc);
1494b8d26b3bSNicholas Bellinger 		if (!cmd)
1495b8d26b3bSNicholas Bellinger 			break;
1496b8d26b3bSNicholas Bellinger 
1497b8d26b3bSNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1498b8d26b3bSNicholas Bellinger 		break;
1499adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
15004366b19cSJenny Derzhavetz 		if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
1501e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
15024366b19cSJenny Derzhavetz 		else
15034366b19cSJenny Derzhavetz 			cmd = isert_allocate_cmd(conn, rx_desc);
15044366b19cSJenny Derzhavetz 
1505e4f4e801SSagi Grimberg 		if (!cmd)
1506e4f4e801SSagi Grimberg 			break;
1507adb54c29SNicholas Bellinger 
1508d703ce2fSNicholas Bellinger 		isert_cmd = iscsit_priv_cmd(cmd);
1509d703ce2fSNicholas Bellinger 		ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1510adb54c29SNicholas Bellinger 					    rx_desc, (struct iscsi_text *)hdr);
1511adb54c29SNicholas Bellinger 		break;
1512b8d26b3bSNicholas Bellinger 	default:
151324f412ddSSagi Grimberg 		isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1514b8d26b3bSNicholas Bellinger 		dump_stack();
1515b8d26b3bSNicholas Bellinger 		break;
1516b8d26b3bSNicholas Bellinger 	}
1517b8d26b3bSNicholas Bellinger 
1518b8d26b3bSNicholas Bellinger 	return ret;
1519b8d26b3bSNicholas Bellinger }
1520b8d26b3bSNicholas Bellinger 
1521b8d26b3bSNicholas Bellinger static void
1522b8d26b3bSNicholas Bellinger isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1523b8d26b3bSNicholas Bellinger {
1524*d3cf81f9SSagi Grimberg 	struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
1525b8d26b3bSNicholas Bellinger 	uint64_t read_va = 0, write_va = 0;
1526b8d26b3bSNicholas Bellinger 	uint32_t read_stag = 0, write_stag = 0;
1527b8d26b3bSNicholas Bellinger 
1528*d3cf81f9SSagi Grimberg 	switch (iser_ctrl->flags & 0xF0) {
1529b8d26b3bSNicholas Bellinger 	case ISCSI_CTRL:
1530*d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_RSV) {
1531*d3cf81f9SSagi Grimberg 			read_stag = be32_to_cpu(iser_ctrl->read_stag);
1532*d3cf81f9SSagi Grimberg 			read_va = be64_to_cpu(iser_ctrl->read_va);
15334c22e07fSSagi Grimberg 			isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1534b8d26b3bSNicholas Bellinger 				  read_stag, (unsigned long long)read_va);
1535b8d26b3bSNicholas Bellinger 		}
1536*d3cf81f9SSagi Grimberg 		if (iser_ctrl->flags & ISER_WSV) {
1537*d3cf81f9SSagi Grimberg 			write_stag = be32_to_cpu(iser_ctrl->write_stag);
1538*d3cf81f9SSagi Grimberg 			write_va = be64_to_cpu(iser_ctrl->write_va);
15394c22e07fSSagi Grimberg 			isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1540b8d26b3bSNicholas Bellinger 				  write_stag, (unsigned long long)write_va);
1541b8d26b3bSNicholas Bellinger 		}
1542b8d26b3bSNicholas Bellinger 
154324f412ddSSagi Grimberg 		isert_dbg("ISER ISCSI_CTRL PDU\n");
1544b8d26b3bSNicholas Bellinger 		break;
1545b8d26b3bSNicholas Bellinger 	case ISER_HELLO:
154624f412ddSSagi Grimberg 		isert_err("iSER Hello message\n");
1547b8d26b3bSNicholas Bellinger 		break;
1548b8d26b3bSNicholas Bellinger 	default:
1549*d3cf81f9SSagi Grimberg 		isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
1550b8d26b3bSNicholas Bellinger 		break;
1551b8d26b3bSNicholas Bellinger 	}
1552b8d26b3bSNicholas Bellinger 
15536c760b3dSBart Van Assche 	isert_rx_opcode(isert_conn, rx_desc,
1554b8d26b3bSNicholas Bellinger 			read_stag, read_va, write_stag, write_va);
1555b8d26b3bSNicholas Bellinger }
1556b8d26b3bSNicholas Bellinger 
1557b8d26b3bSNicholas Bellinger static void
15587748681bSSagi Grimberg isert_rcv_completion(struct iser_rx_desc *desc,
15597748681bSSagi Grimberg 		     struct isert_conn *isert_conn,
15604a295baeSSagi Grimberg 		     u32 xfer_len)
1561b8d26b3bSNicholas Bellinger {
1562dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1563b8d26b3bSNicholas Bellinger 	struct iscsi_hdr *hdr;
1564b8d26b3bSNicholas Bellinger 	u64 rx_dma;
15654366b19cSJenny Derzhavetz 	int rx_buflen;
1566b8d26b3bSNicholas Bellinger 
1567b8d26b3bSNicholas Bellinger 	if ((char *)desc == isert_conn->login_req_buf) {
1568b8d26b3bSNicholas Bellinger 		rx_dma = isert_conn->login_req_dma;
1569b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_LOGIN_SIZE;
15704c22e07fSSagi Grimberg 		isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1571b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1572b8d26b3bSNicholas Bellinger 	} else {
1573b8d26b3bSNicholas Bellinger 		rx_dma = desc->dma_addr;
1574b8d26b3bSNicholas Bellinger 		rx_buflen = ISER_RX_PAYLOAD_SIZE;
15754c22e07fSSagi Grimberg 		isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1576b8d26b3bSNicholas Bellinger 			 rx_dma, rx_buflen);
1577b8d26b3bSNicholas Bellinger 	}
1578b8d26b3bSNicholas Bellinger 
1579b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1580b8d26b3bSNicholas Bellinger 
1581b8d26b3bSNicholas Bellinger 	hdr = &desc->iscsi_header;
158224f412ddSSagi Grimberg 	isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1583b8d26b3bSNicholas Bellinger 		 hdr->opcode, hdr->itt, hdr->flags,
1584b8d26b3bSNicholas Bellinger 		 (int)(xfer_len - ISER_HEADERS_LEN));
1585b8d26b3bSNicholas Bellinger 
15862371e5daSSagi Grimberg 	if ((char *)desc == isert_conn->login_req_buf) {
15872371e5daSSagi Grimberg 		isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
15882371e5daSSagi Grimberg 		if (isert_conn->conn) {
15892371e5daSSagi Grimberg 			struct iscsi_login *login = isert_conn->conn->conn_login;
15902371e5daSSagi Grimberg 
15912371e5daSSagi Grimberg 			if (login && !login->first_request)
15922371e5daSSagi Grimberg 				isert_rx_login_req(isert_conn);
15932371e5daSSagi Grimberg 		}
1594dac6ab30SSagi Grimberg 		mutex_lock(&isert_conn->mutex);
15952371e5daSSagi Grimberg 		complete(&isert_conn->login_req_comp);
1596dac6ab30SSagi Grimberg 		mutex_unlock(&isert_conn->mutex);
15972371e5daSSagi Grimberg 	} else {
1598b8d26b3bSNicholas Bellinger 		isert_rx_do_work(desc, isert_conn);
15992371e5daSSagi Grimberg 	}
1600b8d26b3bSNicholas Bellinger 
1601b8d26b3bSNicholas Bellinger 	ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1602b8d26b3bSNicholas Bellinger 				      DMA_FROM_DEVICE);
1603b8d26b3bSNicholas Bellinger 
1604b8d26b3bSNicholas Bellinger 	isert_conn->post_recv_buf_count--;
1605b8d26b3bSNicholas Bellinger }
1606b8d26b3bSNicholas Bellinger 
1607e3d7e4c3SSagi Grimberg static int
1608e3d7e4c3SSagi Grimberg isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1609e3d7e4c3SSagi Grimberg 		   struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1610e3d7e4c3SSagi Grimberg 		   enum iser_ib_op_code op, struct isert_data_buf *data)
1611e3d7e4c3SSagi Grimberg {
1612dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1613e3d7e4c3SSagi Grimberg 
1614e3d7e4c3SSagi Grimberg 	data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1615e3d7e4c3SSagi Grimberg 			      DMA_TO_DEVICE : DMA_FROM_DEVICE;
1616e3d7e4c3SSagi Grimberg 
1617e3d7e4c3SSagi Grimberg 	data->len = length - offset;
1618e3d7e4c3SSagi Grimberg 	data->offset = offset;
1619e3d7e4c3SSagi Grimberg 	data->sg_off = data->offset / PAGE_SIZE;
1620e3d7e4c3SSagi Grimberg 
1621e3d7e4c3SSagi Grimberg 	data->sg = &sg[data->sg_off];
1622e3d7e4c3SSagi Grimberg 	data->nents = min_t(unsigned int, nents - data->sg_off,
1623e3d7e4c3SSagi Grimberg 					  ISCSI_ISER_SG_TABLESIZE);
1624e3d7e4c3SSagi Grimberg 	data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1625e3d7e4c3SSagi Grimberg 					PAGE_SIZE);
1626e3d7e4c3SSagi Grimberg 
1627e3d7e4c3SSagi Grimberg 	data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1628e3d7e4c3SSagi Grimberg 					data->dma_dir);
1629e3d7e4c3SSagi Grimberg 	if (unlikely(!data->dma_nents)) {
163024f412ddSSagi Grimberg 		isert_err("Cmd: unable to dma map SGs %p\n", sg);
1631e3d7e4c3SSagi Grimberg 		return -EINVAL;
1632e3d7e4c3SSagi Grimberg 	}
1633e3d7e4c3SSagi Grimberg 
163424f412ddSSagi Grimberg 	isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1635e3d7e4c3SSagi Grimberg 		  isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1636e3d7e4c3SSagi Grimberg 
1637e3d7e4c3SSagi Grimberg 	return 0;
1638e3d7e4c3SSagi Grimberg }
1639e3d7e4c3SSagi Grimberg 
1640e3d7e4c3SSagi Grimberg static void
1641e3d7e4c3SSagi Grimberg isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1642e3d7e4c3SSagi Grimberg {
1643dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1644e3d7e4c3SSagi Grimberg 
1645e3d7e4c3SSagi Grimberg 	ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1646e3d7e4c3SSagi Grimberg 	memset(data, 0, sizeof(*data));
1647e3d7e4c3SSagi Grimberg }
1648e3d7e4c3SSagi Grimberg 
1649e3d7e4c3SSagi Grimberg 
1650e3d7e4c3SSagi Grimberg 
1651b8d26b3bSNicholas Bellinger static void
1652b8d26b3bSNicholas Bellinger isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1653b8d26b3bSNicholas Bellinger {
1654b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1655b8d26b3bSNicholas Bellinger 
16564c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1657e3d7e4c3SSagi Grimberg 
1658e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
16594c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1660e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
1661b8d26b3bSNicholas Bellinger 	}
1662b8d26b3bSNicholas Bellinger 
1663e622f2f4SChristoph Hellwig 	if (wr->rdma_wr) {
16644c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free send_wr\n", isert_cmd);
1665e622f2f4SChristoph Hellwig 		kfree(wr->rdma_wr);
1666e622f2f4SChristoph Hellwig 		wr->rdma_wr = NULL;
166790ecc6e2SVu Pham 	}
1668b8d26b3bSNicholas Bellinger 
166990ecc6e2SVu Pham 	if (wr->ib_sge) {
16704c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
167190ecc6e2SVu Pham 		kfree(wr->ib_sge);
167290ecc6e2SVu Pham 		wr->ib_sge = NULL;
167390ecc6e2SVu Pham 	}
1674b8d26b3bSNicholas Bellinger }
1675b8d26b3bSNicholas Bellinger 
1676b8d26b3bSNicholas Bellinger static void
1677a3a5a826SSagi Grimberg isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
167859464ef4SVu Pham {
167959464ef4SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
168059464ef4SVu Pham 
16814c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
168259464ef4SVu Pham 
168359464ef4SVu Pham 	if (wr->fr_desc) {
16844c22e07fSSagi Grimberg 		isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
16859e961ae7SSagi Grimberg 		if (wr->fr_desc->ind & ISERT_PROTECTED) {
16869e961ae7SSagi Grimberg 			isert_unmap_data_buf(isert_conn, &wr->prot);
16879e961ae7SSagi Grimberg 			wr->fr_desc->ind &= ~ISERT_PROTECTED;
16889e961ae7SSagi Grimberg 		}
1689dac6ab30SSagi Grimberg 		spin_lock_bh(&isert_conn->pool_lock);
1690dac6ab30SSagi Grimberg 		list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1691dac6ab30SSagi Grimberg 		spin_unlock_bh(&isert_conn->pool_lock);
169259464ef4SVu Pham 		wr->fr_desc = NULL;
169359464ef4SVu Pham 	}
169459464ef4SVu Pham 
1695e3d7e4c3SSagi Grimberg 	if (wr->data.sg) {
16964c22e07fSSagi Grimberg 		isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
1697e3d7e4c3SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->data);
169859464ef4SVu Pham 	}
169959464ef4SVu Pham 
170059464ef4SVu Pham 	wr->ib_sge = NULL;
1701e622f2f4SChristoph Hellwig 	wr->rdma_wr = NULL;
170259464ef4SVu Pham }
170359464ef4SVu Pham 
170459464ef4SVu Pham static void
170503e7848aSNicholas Bellinger isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1706b8d26b3bSNicholas Bellinger {
1707d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1708b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1709186a9647SNicholas Bellinger 	struct iscsi_conn *conn = isert_conn->conn;
1710dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
1711e4f4e801SSagi Grimberg 	struct iscsi_text_rsp *hdr;
1712b8d26b3bSNicholas Bellinger 
17134c22e07fSSagi Grimberg 	isert_dbg("Cmd %p\n", isert_cmd);
1714b8d26b3bSNicholas Bellinger 
1715b8d26b3bSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
1716b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
1717b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1718b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17195159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1720b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1721b8d26b3bSNicholas Bellinger 
172203e7848aSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
1723b8d26b3bSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
172403e7848aSNicholas Bellinger 			/*
172503e7848aSNicholas Bellinger 			 * Check for special case during comp_err where
172603e7848aSNicholas Bellinger 			 * WRITE_PENDING has been handed off from core,
172703e7848aSNicholas Bellinger 			 * but requires an extra target_put_sess_cmd()
172803e7848aSNicholas Bellinger 			 * before transport_generic_free_cmd() below.
172903e7848aSNicholas Bellinger 			 */
173003e7848aSNicholas Bellinger 			if (comp_err &&
173103e7848aSNicholas Bellinger 			    cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
173203e7848aSNicholas Bellinger 				struct se_cmd *se_cmd = &cmd->se_cmd;
173303e7848aSNicholas Bellinger 
1734afc16604SBart Van Assche 				target_put_sess_cmd(se_cmd);
173503e7848aSNicholas Bellinger 			}
173603e7848aSNicholas Bellinger 		}
1737b8d26b3bSNicholas Bellinger 
1738d40945d8SVu Pham 		device->unreg_rdma_mem(isert_cmd, isert_conn);
1739186a9647SNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1740186a9647SNicholas Bellinger 		break;
1741b8d26b3bSNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
1742186a9647SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1743186a9647SNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17445159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1745186a9647SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1746186a9647SNicholas Bellinger 
1747b8d26b3bSNicholas Bellinger 		transport_generic_free_cmd(&cmd->se_cmd, 0);
1748b8d26b3bSNicholas Bellinger 		break;
1749b8d26b3bSNicholas Bellinger 	case ISCSI_OP_REJECT:
1750b8d26b3bSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
1751adb54c29SNicholas Bellinger 	case ISCSI_OP_TEXT:
1752e4f4e801SSagi Grimberg 		hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1753e4f4e801SSagi Grimberg 		/* If the continue bit is on, keep the command alive */
1754e4f4e801SSagi Grimberg 		if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1755e4f4e801SSagi Grimberg 			break;
1756e4f4e801SSagi Grimberg 
1757b8d26b3bSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1758b8d26b3bSNicholas Bellinger 		if (!list_empty(&cmd->i_conn_node))
17595159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
1760b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1761b8d26b3bSNicholas Bellinger 
1762b8d26b3bSNicholas Bellinger 		/*
1763b8d26b3bSNicholas Bellinger 		 * Handle special case for REJECT when iscsi_add_reject*() has
1764b8d26b3bSNicholas Bellinger 		 * overwritten the original iscsi_opcode assignment, and the
1765b8d26b3bSNicholas Bellinger 		 * associated cmd->se_cmd needs to be released.
1766b8d26b3bSNicholas Bellinger 		 */
1767b8d26b3bSNicholas Bellinger 		if (cmd->se_cmd.se_tfo != NULL) {
176811378cdbSRasmus Villemoes 			isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
17693df8f68aSNicholas Bellinger 				 cmd->iscsi_opcode);
1770b8d26b3bSNicholas Bellinger 			transport_generic_free_cmd(&cmd->se_cmd, 0);
1771b8d26b3bSNicholas Bellinger 			break;
1772b8d26b3bSNicholas Bellinger 		}
1773b8d26b3bSNicholas Bellinger 		/*
1774b8d26b3bSNicholas Bellinger 		 * Fall-through
1775b8d26b3bSNicholas Bellinger 		 */
1776b8d26b3bSNicholas Bellinger 	default:
1777d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
1778b8d26b3bSNicholas Bellinger 		break;
1779b8d26b3bSNicholas Bellinger 	}
1780b8d26b3bSNicholas Bellinger }
1781b8d26b3bSNicholas Bellinger 
1782b8d26b3bSNicholas Bellinger static void
1783b8d26b3bSNicholas Bellinger isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1784b8d26b3bSNicholas Bellinger {
1785b8d26b3bSNicholas Bellinger 	if (tx_desc->dma_addr != 0) {
17864c22e07fSSagi Grimberg 		isert_dbg("unmap single for tx_desc->dma_addr\n");
1787b8d26b3bSNicholas Bellinger 		ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1788b8d26b3bSNicholas Bellinger 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
1789b8d26b3bSNicholas Bellinger 		tx_desc->dma_addr = 0;
1790b8d26b3bSNicholas Bellinger 	}
1791b8d26b3bSNicholas Bellinger }
1792b8d26b3bSNicholas Bellinger 
1793b8d26b3bSNicholas Bellinger static void
1794b8d26b3bSNicholas Bellinger isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
179503e7848aSNicholas Bellinger 		     struct ib_device *ib_dev, bool comp_err)
1796b8d26b3bSNicholas Bellinger {
1797dbbc5d11SNicholas Bellinger 	if (isert_cmd->pdu_buf_dma != 0) {
17984c22e07fSSagi Grimberg 		isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
1799dbbc5d11SNicholas Bellinger 		ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1800dbbc5d11SNicholas Bellinger 				    isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1801dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = 0;
1802b8d26b3bSNicholas Bellinger 	}
1803b8d26b3bSNicholas Bellinger 
1804b8d26b3bSNicholas Bellinger 	isert_unmap_tx_desc(tx_desc, ib_dev);
180503e7848aSNicholas Bellinger 	isert_put_cmd(isert_cmd, comp_err);
1806b8d26b3bSNicholas Bellinger }
1807b8d26b3bSNicholas Bellinger 
180896b7973eSSagi Grimberg static int
180996b7973eSSagi Grimberg isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1810f93f3a70SSagi Grimberg {
18119e961ae7SSagi Grimberg 	struct ib_mr_status mr_status;
181296b7973eSSagi Grimberg 	int ret;
1813f93f3a70SSagi Grimberg 
181496b7973eSSagi Grimberg 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
18159e961ae7SSagi Grimberg 	if (ret) {
181624f412ddSSagi Grimberg 		isert_err("ib_check_mr_status failed, ret %d\n", ret);
18179e961ae7SSagi Grimberg 		goto fail_mr_status;
18189e961ae7SSagi Grimberg 	}
181996b7973eSSagi Grimberg 
18209e961ae7SSagi Grimberg 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
182196b7973eSSagi Grimberg 		u64 sec_offset_err;
18229e961ae7SSagi Grimberg 		u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
18239e961ae7SSagi Grimberg 
18249e961ae7SSagi Grimberg 		switch (mr_status.sig_err.err_type) {
18259e961ae7SSagi Grimberg 		case IB_SIG_BAD_GUARD:
18269e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
18279e961ae7SSagi Grimberg 			break;
18289e961ae7SSagi Grimberg 		case IB_SIG_BAD_REFTAG:
18299e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
18309e961ae7SSagi Grimberg 			break;
18319e961ae7SSagi Grimberg 		case IB_SIG_BAD_APPTAG:
18329e961ae7SSagi Grimberg 			se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
18339e961ae7SSagi Grimberg 			break;
18349e961ae7SSagi Grimberg 		}
183596b7973eSSagi Grimberg 		sec_offset_err = mr_status.sig_err.sig_err_offset;
183696b7973eSSagi Grimberg 		do_div(sec_offset_err, block_size);
183796b7973eSSagi Grimberg 		se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
18389e961ae7SSagi Grimberg 
18394c22e07fSSagi Grimberg 		isert_err("PI error found type %d at sector 0x%llx "
18409e961ae7SSagi Grimberg 			  "expected 0x%x vs actual 0x%x\n",
18419e961ae7SSagi Grimberg 			  mr_status.sig_err.err_type,
18429e961ae7SSagi Grimberg 			  (unsigned long long)se_cmd->bad_sector,
18439e961ae7SSagi Grimberg 			  mr_status.sig_err.expected,
18449e961ae7SSagi Grimberg 			  mr_status.sig_err.actual);
18459e961ae7SSagi Grimberg 		ret = 1;
18469e961ae7SSagi Grimberg 	}
18479e961ae7SSagi Grimberg 
18489e961ae7SSagi Grimberg fail_mr_status:
184996b7973eSSagi Grimberg 	return ret;
185096b7973eSSagi Grimberg }
185196b7973eSSagi Grimberg 
185296b7973eSSagi Grimberg static void
185396b7973eSSagi Grimberg isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
185496b7973eSSagi Grimberg 			    struct isert_cmd *isert_cmd)
185596b7973eSSagi Grimberg {
185696b7973eSSagi Grimberg 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
185796b7973eSSagi Grimberg 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
185896b7973eSSagi Grimberg 	struct se_cmd *se_cmd = &cmd->se_cmd;
185996b7973eSSagi Grimberg 	struct isert_conn *isert_conn = isert_cmd->conn;
1860dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
186196b7973eSSagi Grimberg 	int ret = 0;
186296b7973eSSagi Grimberg 
186396b7973eSSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
186496b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
186596b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
186696b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
186796b7973eSSagi Grimberg 	}
186896b7973eSSagi Grimberg 
1869f93f3a70SSagi Grimberg 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1870e622f2f4SChristoph Hellwig 	wr->rdma_wr_num = 0;
18719e961ae7SSagi Grimberg 	if (ret)
18729e961ae7SSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
18739e961ae7SSagi Grimberg 							 se_cmd->pi_err, 0);
18749e961ae7SSagi Grimberg 	else
1875f93f3a70SSagi Grimberg 		isert_put_response(isert_conn->conn, cmd);
1876f93f3a70SSagi Grimberg }
1877f93f3a70SSagi Grimberg 
1878f93f3a70SSagi Grimberg static void
1879b8d26b3bSNicholas Bellinger isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1880b8d26b3bSNicholas Bellinger 			   struct isert_cmd *isert_cmd)
1881b8d26b3bSNicholas Bellinger {
1882b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1883d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1884b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
188590ecc6e2SVu Pham 	struct isert_conn *isert_conn = isert_cmd->conn;
1886dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
18875bac4b1aSSagi Grimberg 	int ret = 0;
1888b8d26b3bSNicholas Bellinger 
18899e961ae7SSagi Grimberg 	if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
189096b7973eSSagi Grimberg 		ret = isert_check_pi_status(se_cmd,
189196b7973eSSagi Grimberg 					    wr->fr_desc->pi_ctx->sig_mr);
189296b7973eSSagi Grimberg 		wr->fr_desc->ind &= ~ISERT_PROTECTED;
18939e961ae7SSagi Grimberg 	}
18949e961ae7SSagi Grimberg 
1895b8d26b3bSNicholas Bellinger 	iscsit_stop_dataout_timer(cmd);
1896d40945d8SVu Pham 	device->unreg_rdma_mem(isert_cmd, isert_conn);
1897e3d7e4c3SSagi Grimberg 	cmd->write_data_done = wr->data.len;
1898e622f2f4SChristoph Hellwig 	wr->rdma_wr_num = 0;
1899b8d26b3bSNicholas Bellinger 
190024f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1901b8d26b3bSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1902b8d26b3bSNicholas Bellinger 	cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1903b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1904b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1905b8d26b3bSNicholas Bellinger 
1906364189f0SSagi Grimberg 	if (ret) {
1907afc16604SBart Van Assche 		target_put_sess_cmd(se_cmd);
19085bac4b1aSSagi Grimberg 		transport_send_check_condition_and_sense(se_cmd,
19095bac4b1aSSagi Grimberg 							 se_cmd->pi_err, 0);
1910364189f0SSagi Grimberg 	} else {
1911b8d26b3bSNicholas Bellinger 		target_execute_cmd(se_cmd);
1912b8d26b3bSNicholas Bellinger 	}
1913364189f0SSagi Grimberg }
1914b8d26b3bSNicholas Bellinger 
1915b8d26b3bSNicholas Bellinger static void
1916b8d26b3bSNicholas Bellinger isert_do_control_comp(struct work_struct *work)
1917b8d26b3bSNicholas Bellinger {
1918b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = container_of(work,
1919b8d26b3bSNicholas Bellinger 			struct isert_cmd, comp_work);
1920b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = isert_cmd->conn;
1921dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1922d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1923b8d26b3bSNicholas Bellinger 
19244c22e07fSSagi Grimberg 	isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
19254c22e07fSSagi Grimberg 
1926b8d26b3bSNicholas Bellinger 	switch (cmd->i_state) {
1927b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
1928b8d26b3bSNicholas Bellinger 		iscsit_tmr_post_handler(cmd, cmd->conn);
192910633c37SSagi Grimberg 	case ISTATE_SEND_REJECT:   /* FALLTHRU */
193010633c37SSagi Grimberg 	case ISTATE_SEND_TEXTRSP:  /* FALLTHRU */
1931b8d26b3bSNicholas Bellinger 		cmd->i_state = ISTATE_SENT_STATUS;
19324c22e07fSSagi Grimberg 		isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
19334c22e07fSSagi Grimberg 				     ib_dev, false);
19343df8f68aSNicholas Bellinger 		break;
1935b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
1936b8d26b3bSNicholas Bellinger 		iscsit_logout_post_handler(cmd, cmd->conn);
1937b8d26b3bSNicholas Bellinger 		break;
1938b8d26b3bSNicholas Bellinger 	default:
19394c22e07fSSagi Grimberg 		isert_err("Unknown i_state %d\n", cmd->i_state);
1940b8d26b3bSNicholas Bellinger 		dump_stack();
1941b8d26b3bSNicholas Bellinger 		break;
1942b8d26b3bSNicholas Bellinger 	}
1943b8d26b3bSNicholas Bellinger }
1944b8d26b3bSNicholas Bellinger 
1945b8d26b3bSNicholas Bellinger static void
1946b8d26b3bSNicholas Bellinger isert_response_completion(struct iser_tx_desc *tx_desc,
1947b8d26b3bSNicholas Bellinger 			  struct isert_cmd *isert_cmd,
1948b8d26b3bSNicholas Bellinger 			  struct isert_conn *isert_conn,
1949b8d26b3bSNicholas Bellinger 			  struct ib_device *ib_dev)
1950b8d26b3bSNicholas Bellinger {
1951d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1952b8d26b3bSNicholas Bellinger 
1953b8d26b3bSNicholas Bellinger 	if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
19543df8f68aSNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1955adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_REJECT ||
1956adb54c29SNicholas Bellinger 	    cmd->i_state == ISTATE_SEND_TEXTRSP) {
1957b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1958b8d26b3bSNicholas Bellinger 
1959b8d26b3bSNicholas Bellinger 		INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1960b8d26b3bSNicholas Bellinger 		queue_work(isert_comp_wq, &isert_cmd->comp_work);
1961b8d26b3bSNicholas Bellinger 		return;
1962b8d26b3bSNicholas Bellinger 	}
1963897bb2c9SSagi Grimberg 
1964b8d26b3bSNicholas Bellinger 	cmd->i_state = ISTATE_SENT_STATUS;
196503e7848aSNicholas Bellinger 	isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1966b8d26b3bSNicholas Bellinger }
1967b8d26b3bSNicholas Bellinger 
1968b8d26b3bSNicholas Bellinger static void
19697748681bSSagi Grimberg isert_snd_completion(struct iser_tx_desc *tx_desc,
1970b8d26b3bSNicholas Bellinger 		      struct isert_conn *isert_conn)
1971b8d26b3bSNicholas Bellinger {
1972dac6ab30SSagi Grimberg 	struct ib_device *ib_dev = isert_conn->cm_id->device;
1973b8d26b3bSNicholas Bellinger 	struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1974b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr;
1975b8d26b3bSNicholas Bellinger 
1976b8d26b3bSNicholas Bellinger 	if (!isert_cmd) {
1977b8d26b3bSNicholas Bellinger 		isert_unmap_tx_desc(tx_desc, ib_dev);
1978b8d26b3bSNicholas Bellinger 		return;
1979b8d26b3bSNicholas Bellinger 	}
1980b8d26b3bSNicholas Bellinger 	wr = &isert_cmd->rdma_wr;
1981b8d26b3bSNicholas Bellinger 
19824c22e07fSSagi Grimberg 	isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
19834c22e07fSSagi Grimberg 
1984b8d26b3bSNicholas Bellinger 	switch (wr->iser_ib_op) {
1985b8d26b3bSNicholas Bellinger 	case ISER_IB_SEND:
1986b8d26b3bSNicholas Bellinger 		isert_response_completion(tx_desc, isert_cmd,
1987b8d26b3bSNicholas Bellinger 					  isert_conn, ib_dev);
1988b8d26b3bSNicholas Bellinger 		break;
1989b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_WRITE:
1990f93f3a70SSagi Grimberg 		isert_completion_rdma_write(tx_desc, isert_cmd);
1991b8d26b3bSNicholas Bellinger 		break;
1992b8d26b3bSNicholas Bellinger 	case ISER_IB_RDMA_READ:
1993b8d26b3bSNicholas Bellinger 		isert_completion_rdma_read(tx_desc, isert_cmd);
1994b8d26b3bSNicholas Bellinger 		break;
1995b8d26b3bSNicholas Bellinger 	default:
19964c22e07fSSagi Grimberg 		isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
1997b8d26b3bSNicholas Bellinger 		dump_stack();
1998b8d26b3bSNicholas Bellinger 		break;
1999b8d26b3bSNicholas Bellinger 	}
2000b8d26b3bSNicholas Bellinger }
2001b8d26b3bSNicholas Bellinger 
20026f0fae3dSSagi Grimberg /**
20036f0fae3dSSagi Grimberg  * is_isert_tx_desc() - Indicate if the completion wr_id
20046f0fae3dSSagi Grimberg  *     is a TX descriptor or not.
20056f0fae3dSSagi Grimberg  * @isert_conn: iser connection
20066f0fae3dSSagi Grimberg  * @wr_id: completion WR identifier
20076f0fae3dSSagi Grimberg  *
20086f0fae3dSSagi Grimberg  * Since we cannot rely on wc opcode in FLUSH errors
20096f0fae3dSSagi Grimberg  * we must work around it by checking if the wr_id address
20106f0fae3dSSagi Grimberg  * falls in the iser connection rx_descs buffer. If so
20116f0fae3dSSagi Grimberg  * it is an RX descriptor, otherwize it is a TX.
20126f0fae3dSSagi Grimberg  */
20136f0fae3dSSagi Grimberg static inline bool
20146f0fae3dSSagi Grimberg is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
2015b8d26b3bSNicholas Bellinger {
2016dac6ab30SSagi Grimberg 	void *start = isert_conn->rx_descs;
2017dac6ab30SSagi Grimberg 	int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
20186f0fae3dSSagi Grimberg 
20196f0fae3dSSagi Grimberg 	if (wr_id >= start && wr_id < start + len)
20206f0fae3dSSagi Grimberg 		return false;
20216f0fae3dSSagi Grimberg 
20226f0fae3dSSagi Grimberg 	return true;
20236f0fae3dSSagi Grimberg }
20246f0fae3dSSagi Grimberg 
20256f0fae3dSSagi Grimberg static void
20266f0fae3dSSagi Grimberg isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
20276f0fae3dSSagi Grimberg {
2028bdf20e72SSagi Grimberg 	if (wc->wr_id == ISER_BEACON_WRID) {
2029dac6ab30SSagi Grimberg 		isert_info("conn %p completing wait_comp_err\n",
2030bdf20e72SSagi Grimberg 			   isert_conn);
2031dac6ab30SSagi Grimberg 		complete(&isert_conn->wait_comp_err);
2032ed4520aeSNicholas Bellinger 	} else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
2033dac6ab30SSagi Grimberg 		struct ib_device *ib_dev = isert_conn->cm_id->device;
2034df43debdSSagi Grimberg 		struct isert_cmd *isert_cmd;
20356f0fae3dSSagi Grimberg 		struct iser_tx_desc *desc;
2036b8d26b3bSNicholas Bellinger 
20376f0fae3dSSagi Grimberg 		desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20386f0fae3dSSagi Grimberg 		isert_cmd = desc->isert_cmd;
2039b8d26b3bSNicholas Bellinger 		if (!isert_cmd)
2040df43debdSSagi Grimberg 			isert_unmap_tx_desc(desc, ib_dev);
2041b8d26b3bSNicholas Bellinger 		else
2042df43debdSSagi Grimberg 			isert_completion_put(desc, isert_cmd, ib_dev, true);
2043df43debdSSagi Grimberg 	} else {
2044df43debdSSagi Grimberg 		isert_conn->post_recv_buf_count--;
2045bdf20e72SSagi Grimberg 		if (!isert_conn->post_recv_buf_count)
20469d49f5e2SSagi Grimberg 			iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2047b8d26b3bSNicholas Bellinger 	}
2048df43debdSSagi Grimberg }
2049b8d26b3bSNicholas Bellinger 
2050b8d26b3bSNicholas Bellinger static void
20516f0fae3dSSagi Grimberg isert_handle_wc(struct ib_wc *wc)
2052b8d26b3bSNicholas Bellinger {
2053b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
2054b8d26b3bSNicholas Bellinger 	struct iser_tx_desc *tx_desc;
20556f0fae3dSSagi Grimberg 	struct iser_rx_desc *rx_desc;
2056b8d26b3bSNicholas Bellinger 
20576f0fae3dSSagi Grimberg 	isert_conn = wc->qp->qp_context;
20586f0fae3dSSagi Grimberg 	if (likely(wc->status == IB_WC_SUCCESS)) {
20596f0fae3dSSagi Grimberg 		if (wc->opcode == IB_WC_RECV) {
20606f0fae3dSSagi Grimberg 			rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
20617748681bSSagi Grimberg 			isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
2062b8d26b3bSNicholas Bellinger 		} else {
20636f0fae3dSSagi Grimberg 			tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
20647748681bSSagi Grimberg 			isert_snd_completion(tx_desc, isert_conn);
2065b8d26b3bSNicholas Bellinger 		}
20666f0fae3dSSagi Grimberg 	} else {
20676f0fae3dSSagi Grimberg 		if (wc->status != IB_WC_WR_FLUSH_ERR)
2068ea8a1616SSagi Grimberg 			isert_err("%s (%d): wr id %llx vend_err %x\n",
2069ea8a1616SSagi Grimberg 				  ib_wc_status_msg(wc->status), wc->status,
2070ea8a1616SSagi Grimberg 				  wc->wr_id, wc->vendor_err);
20716f0fae3dSSagi Grimberg 		else
2072ea8a1616SSagi Grimberg 			isert_dbg("%s (%d): wr id %llx\n",
2073ea8a1616SSagi Grimberg 				  ib_wc_status_msg(wc->status), wc->status,
2074ea8a1616SSagi Grimberg 				  wc->wr_id);
2075b8d26b3bSNicholas Bellinger 
20766f0fae3dSSagi Grimberg 		if (wc->wr_id != ISER_FASTREG_LI_WRID)
20776f0fae3dSSagi Grimberg 			isert_cq_comp_err(isert_conn, wc);
20786f0fae3dSSagi Grimberg 	}
2079b8d26b3bSNicholas Bellinger }
2080b8d26b3bSNicholas Bellinger 
2081b8d26b3bSNicholas Bellinger static void
20826f0fae3dSSagi Grimberg isert_cq_work(struct work_struct *work)
2083b8d26b3bSNicholas Bellinger {
208437d9fe80SSagi Grimberg 	enum { isert_poll_budget = 65536 };
20854a295baeSSagi Grimberg 	struct isert_comp *comp = container_of(work, struct isert_comp,
20866f0fae3dSSagi Grimberg 					       work);
208736ea63b5SSagi Grimberg 	struct ib_wc *const wcs = comp->wcs;
208836ea63b5SSagi Grimberg 	int i, n, completed = 0;
2089b8d26b3bSNicholas Bellinger 
209036ea63b5SSagi Grimberg 	while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
209136ea63b5SSagi Grimberg 		for (i = 0; i < n; i++)
209236ea63b5SSagi Grimberg 			isert_handle_wc(&wcs[i]);
2093b8d26b3bSNicholas Bellinger 
209436ea63b5SSagi Grimberg 		completed += n;
209536ea63b5SSagi Grimberg 		if (completed >= isert_poll_budget)
209637d9fe80SSagi Grimberg 			break;
209737d9fe80SSagi Grimberg 	}
209837d9fe80SSagi Grimberg 
20996f0fae3dSSagi Grimberg 	ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
2100b8d26b3bSNicholas Bellinger }
2101b8d26b3bSNicholas Bellinger 
2102b8d26b3bSNicholas Bellinger static void
21036f0fae3dSSagi Grimberg isert_cq_callback(struct ib_cq *cq, void *context)
2104b8d26b3bSNicholas Bellinger {
21054a295baeSSagi Grimberg 	struct isert_comp *comp = context;
2106b8d26b3bSNicholas Bellinger 
21076f0fae3dSSagi Grimberg 	queue_work(isert_comp_wq, &comp->work);
2108b8d26b3bSNicholas Bellinger }
2109b8d26b3bSNicholas Bellinger 
2110b8d26b3bSNicholas Bellinger static int
2111b8d26b3bSNicholas Bellinger isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2112b8d26b3bSNicholas Bellinger {
2113b8d26b3bSNicholas Bellinger 	struct ib_send_wr *wr_failed;
2114b8d26b3bSNicholas Bellinger 	int ret;
2115b8d26b3bSNicholas Bellinger 
21164366b19cSJenny Derzhavetz 	ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
21174366b19cSJenny Derzhavetz 	if (ret) {
21184366b19cSJenny Derzhavetz 		isert_err("ib_post_recv failed with %d\n", ret);
21194366b19cSJenny Derzhavetz 		return ret;
21204366b19cSJenny Derzhavetz 	}
21214366b19cSJenny Derzhavetz 
2122dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
2123b8d26b3bSNicholas Bellinger 			   &wr_failed);
2124b8d26b3bSNicholas Bellinger 	if (ret) {
212524f412ddSSagi Grimberg 		isert_err("ib_post_send failed with %d\n", ret);
2126b8d26b3bSNicholas Bellinger 		return ret;
2127b8d26b3bSNicholas Bellinger 	}
2128b8d26b3bSNicholas Bellinger 	return ret;
2129b8d26b3bSNicholas Bellinger }
2130b8d26b3bSNicholas Bellinger 
2131b8d26b3bSNicholas Bellinger static int
2132b8d26b3bSNicholas Bellinger isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2133b8d26b3bSNicholas Bellinger {
2134d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
21356700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2136b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2137b8d26b3bSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2138b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header;
2139b8d26b3bSNicholas Bellinger 
2140b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2141b8d26b3bSNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2142b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2143b8d26b3bSNicholas Bellinger 	/*
2144b8d26b3bSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
2145b8d26b3bSNicholas Bellinger 	 */
2146b8d26b3bSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
2147b8d26b3bSNicholas Bellinger 	    ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2148b8d26b3bSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2149dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
215067cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2151b8d26b3bSNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2152dbbc5d11SNicholas Bellinger 		u32 padding, pdu_len;
2153b8d26b3bSNicholas Bellinger 
2154b8d26b3bSNicholas Bellinger 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2155b8d26b3bSNicholas Bellinger 				   cmd->sense_buffer);
2156b8d26b3bSNicholas Bellinger 		cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2157b8d26b3bSNicholas Bellinger 
2158b8d26b3bSNicholas Bellinger 		padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2159b8d26b3bSNicholas Bellinger 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2160dbbc5d11SNicholas Bellinger 		pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2161b8d26b3bSNicholas Bellinger 
2162dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2163dbbc5d11SNicholas Bellinger 				(void *)cmd->sense_buffer, pdu_len,
2164b8d26b3bSNicholas Bellinger 				DMA_TO_DEVICE);
2165b8d26b3bSNicholas Bellinger 
2166dbbc5d11SNicholas Bellinger 		isert_cmd->pdu_buf_len = pdu_len;
2167dbbc5d11SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2168dbbc5d11SNicholas Bellinger 		tx_dsg->length	= pdu_len;
216934efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
2170b8d26b3bSNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2171b8d26b3bSNicholas Bellinger 	}
2172b8d26b3bSNicholas Bellinger 
217368a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2174b8d26b3bSNicholas Bellinger 
21754c22e07fSSagi Grimberg 	isert_dbg("Posting SCSI Response\n");
2176b8d26b3bSNicholas Bellinger 
2177b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2178b8d26b3bSNicholas Bellinger }
2179b8d26b3bSNicholas Bellinger 
2180131e6abcSNicholas Bellinger static void
2181131e6abcSNicholas Bellinger isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2182131e6abcSNicholas Bellinger {
2183131e6abcSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
21846700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2185dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
2186131e6abcSNicholas Bellinger 
2187131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2188131e6abcSNicholas Bellinger 	if (!list_empty(&cmd->i_conn_node))
2189131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
2190131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2191131e6abcSNicholas Bellinger 
2192131e6abcSNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE)
2193131e6abcSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
2194131e6abcSNicholas Bellinger 
2195131e6abcSNicholas Bellinger 	device->unreg_rdma_mem(isert_cmd, isert_conn);
2196131e6abcSNicholas Bellinger }
2197131e6abcSNicholas Bellinger 
2198e70beee7SNicholas Bellinger static enum target_prot_op
2199e70beee7SNicholas Bellinger isert_get_sup_prot_ops(struct iscsi_conn *conn)
2200e70beee7SNicholas Bellinger {
22016700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2202dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
2203e70beee7SNicholas Bellinger 
220423a548eeSSagi Grimberg 	if (conn->tpg->tpg_attrib.t10_pi) {
220523a548eeSSagi Grimberg 		if (device->pi_capable) {
220624f412ddSSagi Grimberg 			isert_info("conn %p PI offload enabled\n", isert_conn);
220723a548eeSSagi Grimberg 			isert_conn->pi_support = true;
2208e70beee7SNicholas Bellinger 			return TARGET_PROT_ALL;
220923a548eeSSagi Grimberg 		}
221023a548eeSSagi Grimberg 	}
221123a548eeSSagi Grimberg 
221224f412ddSSagi Grimberg 	isert_info("conn %p PI offload disabled\n", isert_conn);
221323a548eeSSagi Grimberg 	isert_conn->pi_support = false;
2214e70beee7SNicholas Bellinger 
2215e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
2216e70beee7SNicholas Bellinger }
2217e70beee7SNicholas Bellinger 
2218b8d26b3bSNicholas Bellinger static int
2219b8d26b3bSNicholas Bellinger isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2220b8d26b3bSNicholas Bellinger 		bool nopout_response)
2221b8d26b3bSNicholas Bellinger {
2222d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22236700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2224b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2225b8d26b3bSNicholas Bellinger 
2226b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2227b8d26b3bSNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2228b8d26b3bSNicholas Bellinger 			       &isert_cmd->tx_desc.iscsi_header,
2229b8d26b3bSNicholas Bellinger 			       nopout_response);
2230b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
223168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2232b8d26b3bSNicholas Bellinger 
22334c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
2234b8d26b3bSNicholas Bellinger 
2235b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2236b8d26b3bSNicholas Bellinger }
2237b8d26b3bSNicholas Bellinger 
2238b8d26b3bSNicholas Bellinger static int
2239b8d26b3bSNicholas Bellinger isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2240b8d26b3bSNicholas Bellinger {
2241d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22426700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2243b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2244b8d26b3bSNicholas Bellinger 
2245b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2246b8d26b3bSNicholas Bellinger 	iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2247b8d26b3bSNicholas Bellinger 				&isert_cmd->tx_desc.iscsi_header);
2248b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
224968a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2250b8d26b3bSNicholas Bellinger 
22514c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Logout Response\n", isert_conn);
2252b8d26b3bSNicholas Bellinger 
2253b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2254b8d26b3bSNicholas Bellinger }
2255b8d26b3bSNicholas Bellinger 
2256b8d26b3bSNicholas Bellinger static int
2257b8d26b3bSNicholas Bellinger isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2258b8d26b3bSNicholas Bellinger {
2259d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22606700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2261b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2262b8d26b3bSNicholas Bellinger 
2263b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2264b8d26b3bSNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2265b8d26b3bSNicholas Bellinger 				  &isert_cmd->tx_desc.iscsi_header);
2266b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
226768a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2268b8d26b3bSNicholas Bellinger 
22694c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
2270b8d26b3bSNicholas Bellinger 
2271b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2272b8d26b3bSNicholas Bellinger }
2273b8d26b3bSNicholas Bellinger 
2274b8d26b3bSNicholas Bellinger static int
2275b8d26b3bSNicholas Bellinger isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2276b8d26b3bSNicholas Bellinger {
2277d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
22786700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2279b8d26b3bSNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2280dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
228167cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
22823df8f68aSNicholas Bellinger 	struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
22833df8f68aSNicholas Bellinger 	struct iscsi_reject *hdr =
22843df8f68aSNicholas Bellinger 		(struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2285b8d26b3bSNicholas Bellinger 
2286b8d26b3bSNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
22873df8f68aSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
2288b8d26b3bSNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
22893df8f68aSNicholas Bellinger 
22903df8f68aSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
2291dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
22923df8f68aSNicholas Bellinger 			(void *)cmd->buf_ptr, ISCSI_HDR_LEN,
22933df8f68aSNicholas Bellinger 			DMA_TO_DEVICE);
2294dbbc5d11SNicholas Bellinger 	isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2295dbbc5d11SNicholas Bellinger 	tx_dsg->addr	= isert_cmd->pdu_buf_dma;
22963df8f68aSNicholas Bellinger 	tx_dsg->length	= ISCSI_HDR_LEN;
229734efc7dfSJason Gunthorpe 	tx_dsg->lkey	= device->pd->local_dma_lkey;
22983df8f68aSNicholas Bellinger 	isert_cmd->tx_desc.num_sge = 2;
22993df8f68aSNicholas Bellinger 
230068a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2301b8d26b3bSNicholas Bellinger 
23024c22e07fSSagi Grimberg 	isert_dbg("conn %p Posting Reject\n", isert_conn);
2303b8d26b3bSNicholas Bellinger 
2304b8d26b3bSNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2305b8d26b3bSNicholas Bellinger }
2306b8d26b3bSNicholas Bellinger 
2307b8d26b3bSNicholas Bellinger static int
2308adb54c29SNicholas Bellinger isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2309adb54c29SNicholas Bellinger {
2310d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
23116700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2312adb54c29SNicholas Bellinger 	struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2313adb54c29SNicholas Bellinger 	struct iscsi_text_rsp *hdr =
2314adb54c29SNicholas Bellinger 		(struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2315adb54c29SNicholas Bellinger 	u32 txt_rsp_len;
2316adb54c29SNicholas Bellinger 	int rc;
2317adb54c29SNicholas Bellinger 
2318adb54c29SNicholas Bellinger 	isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
231922c7aaa5SSagi Grimberg 	rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2320adb54c29SNicholas Bellinger 	if (rc < 0)
2321adb54c29SNicholas Bellinger 		return rc;
2322adb54c29SNicholas Bellinger 
2323adb54c29SNicholas Bellinger 	txt_rsp_len = rc;
2324adb54c29SNicholas Bellinger 	isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2325adb54c29SNicholas Bellinger 
2326adb54c29SNicholas Bellinger 	if (txt_rsp_len) {
2327dac6ab30SSagi Grimberg 		struct isert_device *device = isert_conn->device;
232867cb3949SSagi Grimberg 		struct ib_device *ib_dev = device->ib_device;
2329adb54c29SNicholas Bellinger 		struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2330adb54c29SNicholas Bellinger 		void *txt_rsp_buf = cmd->buf_ptr;
2331adb54c29SNicholas Bellinger 
2332adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2333adb54c29SNicholas Bellinger 				txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2334adb54c29SNicholas Bellinger 
2335adb54c29SNicholas Bellinger 		isert_cmd->pdu_buf_len = txt_rsp_len;
2336adb54c29SNicholas Bellinger 		tx_dsg->addr	= isert_cmd->pdu_buf_dma;
2337adb54c29SNicholas Bellinger 		tx_dsg->length	= txt_rsp_len;
233834efc7dfSJason Gunthorpe 		tx_dsg->lkey	= device->pd->local_dma_lkey;
2339adb54c29SNicholas Bellinger 		isert_cmd->tx_desc.num_sge = 2;
2340adb54c29SNicholas Bellinger 	}
234168a86deeSSagi Grimberg 	isert_init_send_wr(isert_conn, isert_cmd, send_wr);
2342adb54c29SNicholas Bellinger 
2343f64d2792SSagi Grimberg 	isert_dbg("conn %p Text Response\n", isert_conn);
2344adb54c29SNicholas Bellinger 
2345adb54c29SNicholas Bellinger 	return isert_post_response(isert_conn, isert_cmd);
2346adb54c29SNicholas Bellinger }
2347adb54c29SNicholas Bellinger 
2348adb54c29SNicholas Bellinger static int
2349b8d26b3bSNicholas Bellinger isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2350e622f2f4SChristoph Hellwig 		    struct ib_sge *ib_sge, struct ib_rdma_wr *rdma_wr,
2351b8d26b3bSNicholas Bellinger 		    u32 data_left, u32 offset)
2352b8d26b3bSNicholas Bellinger {
2353d703ce2fSNicholas Bellinger 	struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2354b8d26b3bSNicholas Bellinger 	struct scatterlist *sg_start, *tmp_sg;
2355dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
235667cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
2357b8d26b3bSNicholas Bellinger 	u32 sg_off, page_off;
2358b8d26b3bSNicholas Bellinger 	int i = 0, sg_nents;
2359b8d26b3bSNicholas Bellinger 
2360b8d26b3bSNicholas Bellinger 	sg_off = offset / PAGE_SIZE;
2361b8d26b3bSNicholas Bellinger 	sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2362b8d26b3bSNicholas Bellinger 	sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2363b8d26b3bSNicholas Bellinger 	page_off = offset % PAGE_SIZE;
2364b8d26b3bSNicholas Bellinger 
2365e622f2f4SChristoph Hellwig 	rdma_wr->wr.sg_list = ib_sge;
2366e622f2f4SChristoph Hellwig 	rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
2367b8d26b3bSNicholas Bellinger 	/*
2368b8d26b3bSNicholas Bellinger 	 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2369b8d26b3bSNicholas Bellinger 	 */
2370b8d26b3bSNicholas Bellinger 	for_each_sg(sg_start, tmp_sg, sg_nents, i) {
23714c22e07fSSagi Grimberg 		isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
23724c22e07fSSagi Grimberg 			  "page_off: %u\n",
2373b8d26b3bSNicholas Bellinger 			  (unsigned long long)tmp_sg->dma_address,
2374b8d26b3bSNicholas Bellinger 			  tmp_sg->length, page_off);
2375b8d26b3bSNicholas Bellinger 
2376b8d26b3bSNicholas Bellinger 		ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2377b8d26b3bSNicholas Bellinger 		ib_sge->length = min_t(u32, data_left,
2378b8d26b3bSNicholas Bellinger 				ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
237934efc7dfSJason Gunthorpe 		ib_sge->lkey = device->pd->local_dma_lkey;
2380b8d26b3bSNicholas Bellinger 
23814c22e07fSSagi Grimberg 		isert_dbg("RDMA ib_sge: addr: 0x%llx  length: %u lkey: %x\n",
238290ecc6e2SVu Pham 			  ib_sge->addr, ib_sge->length, ib_sge->lkey);
2383b8d26b3bSNicholas Bellinger 		page_off = 0;
2384b8d26b3bSNicholas Bellinger 		data_left -= ib_sge->length;
23859253e667SSagi Grimberg 		if (!data_left)
23869253e667SSagi Grimberg 			break;
2387b8d26b3bSNicholas Bellinger 		ib_sge++;
238824f412ddSSagi Grimberg 		isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
2389b8d26b3bSNicholas Bellinger 	}
2390b8d26b3bSNicholas Bellinger 
2391e622f2f4SChristoph Hellwig 	rdma_wr->wr.num_sge = ++i;
239224f412ddSSagi Grimberg 	isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2393e622f2f4SChristoph Hellwig 		  rdma_wr->wr.sg_list, rdma_wr->wr.num_sge);
2394b8d26b3bSNicholas Bellinger 
2395e622f2f4SChristoph Hellwig 	return rdma_wr->wr.num_sge;
2396b8d26b3bSNicholas Bellinger }
2397b8d26b3bSNicholas Bellinger 
2398b8d26b3bSNicholas Bellinger static int
239990ecc6e2SVu Pham isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
240090ecc6e2SVu Pham 	       struct isert_rdma_wr *wr)
2401b8d26b3bSNicholas Bellinger {
2402b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2403d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
24046700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2405e3d7e4c3SSagi Grimberg 	struct isert_data_buf *data = &wr->data;
2406e622f2f4SChristoph Hellwig 	struct ib_rdma_wr *rdma_wr;
2407b8d26b3bSNicholas Bellinger 	struct ib_sge *ib_sge;
2408e3d7e4c3SSagi Grimberg 	u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2409e3d7e4c3SSagi Grimberg 	int ret = 0, i, ib_sge_cnt;
2410b8d26b3bSNicholas Bellinger 
241190ecc6e2SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2412b8d26b3bSNicholas Bellinger 
2413e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2414e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2415e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2416e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2417e3d7e4c3SSagi Grimberg 	if (ret)
2418e3d7e4c3SSagi Grimberg 		return ret;
2419b8d26b3bSNicholas Bellinger 
2420e3d7e4c3SSagi Grimberg 	data_left = data->len;
2421e3d7e4c3SSagi Grimberg 	offset = data->offset;
2422b8d26b3bSNicholas Bellinger 
2423e3d7e4c3SSagi Grimberg 	ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2424b8d26b3bSNicholas Bellinger 	if (!ib_sge) {
242524f412ddSSagi Grimberg 		isert_warn("Unable to allocate ib_sge\n");
2426b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2427e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2428b8d26b3bSNicholas Bellinger 	}
242990ecc6e2SVu Pham 	wr->ib_sge = ib_sge;
2430b8d26b3bSNicholas Bellinger 
2431e622f2f4SChristoph Hellwig 	wr->rdma_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2432e622f2f4SChristoph Hellwig 	wr->rdma_wr = kzalloc(sizeof(struct ib_rdma_wr) * wr->rdma_wr_num,
2433b8d26b3bSNicholas Bellinger 				GFP_KERNEL);
2434e622f2f4SChristoph Hellwig 	if (!wr->rdma_wr) {
2435e622f2f4SChristoph Hellwig 		isert_dbg("Unable to allocate wr->rdma_wr\n");
2436b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
2437e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
2438b8d26b3bSNicholas Bellinger 	}
2439b8d26b3bSNicholas Bellinger 
2440b8d26b3bSNicholas Bellinger 	wr->isert_cmd = isert_cmd;
2441b8d26b3bSNicholas Bellinger 	rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2442b8d26b3bSNicholas Bellinger 
2443e622f2f4SChristoph Hellwig 	for (i = 0; i < wr->rdma_wr_num; i++) {
2444e622f2f4SChristoph Hellwig 		rdma_wr = &isert_cmd->rdma_wr.rdma_wr[i];
2445b8d26b3bSNicholas Bellinger 		data_len = min(data_left, rdma_write_max);
2446b8d26b3bSNicholas Bellinger 
2447e622f2f4SChristoph Hellwig 		rdma_wr->wr.send_flags = 0;
244890ecc6e2SVu Pham 		if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2449e622f2f4SChristoph Hellwig 			rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2450e622f2f4SChristoph Hellwig 			rdma_wr->remote_addr = isert_cmd->read_va + offset;
2451e622f2f4SChristoph Hellwig 			rdma_wr->rkey = isert_cmd->read_stag;
2452e622f2f4SChristoph Hellwig 			if (i + 1 == wr->rdma_wr_num)
2453e622f2f4SChristoph Hellwig 				rdma_wr->wr.next = &isert_cmd->tx_desc.send_wr;
245490ecc6e2SVu Pham 			else
2455e622f2f4SChristoph Hellwig 				rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
245690ecc6e2SVu Pham 		} else {
2457e622f2f4SChristoph Hellwig 			rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2458e622f2f4SChristoph Hellwig 			rdma_wr->remote_addr = isert_cmd->write_va + va_offset;
2459e622f2f4SChristoph Hellwig 			rdma_wr->rkey = isert_cmd->write_stag;
2460e622f2f4SChristoph Hellwig 			if (i + 1 == wr->rdma_wr_num)
2461e622f2f4SChristoph Hellwig 				rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
246290ecc6e2SVu Pham 			else
2463e622f2f4SChristoph Hellwig 				rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
246490ecc6e2SVu Pham 		}
2465b8d26b3bSNicholas Bellinger 
2466b8d26b3bSNicholas Bellinger 		ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2467e622f2f4SChristoph Hellwig 					rdma_wr, data_len, offset);
2468b8d26b3bSNicholas Bellinger 		ib_sge += ib_sge_cnt;
2469b8d26b3bSNicholas Bellinger 
2470b8d26b3bSNicholas Bellinger 		offset += data_len;
247190ecc6e2SVu Pham 		va_offset += data_len;
2472b8d26b3bSNicholas Bellinger 		data_left -= data_len;
2473b8d26b3bSNicholas Bellinger 	}
247490ecc6e2SVu Pham 
247590ecc6e2SVu Pham 	return 0;
2476e3d7e4c3SSagi Grimberg unmap_cmd:
2477e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, data);
2478e3d7e4c3SSagi Grimberg 
247990ecc6e2SVu Pham 	return ret;
248090ecc6e2SVu Pham }
248190ecc6e2SVu Pham 
248210633c37SSagi Grimberg static inline void
248310633c37SSagi Grimberg isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
248410633c37SSagi Grimberg {
248510633c37SSagi Grimberg 	u32 rkey;
248610633c37SSagi Grimberg 
248710633c37SSagi Grimberg 	memset(inv_wr, 0, sizeof(*inv_wr));
248810633c37SSagi Grimberg 	inv_wr->wr_id = ISER_FASTREG_LI_WRID;
248910633c37SSagi Grimberg 	inv_wr->opcode = IB_WR_LOCAL_INV;
249010633c37SSagi Grimberg 	inv_wr->ex.invalidate_rkey = mr->rkey;
249110633c37SSagi Grimberg 
249210633c37SSagi Grimberg 	/* Bump the key */
249310633c37SSagi Grimberg 	rkey = ib_inc_rkey(mr->rkey);
249410633c37SSagi Grimberg 	ib_update_fast_reg_key(mr, rkey);
249510633c37SSagi Grimberg }
249610633c37SSagi Grimberg 
249759464ef4SVu Pham static int
2498e3d7e4c3SSagi Grimberg isert_fast_reg_mr(struct isert_conn *isert_conn,
2499e3d7e4c3SSagi Grimberg 		  struct fast_reg_descriptor *fr_desc,
2500e3d7e4c3SSagi Grimberg 		  struct isert_data_buf *mem,
25019e961ae7SSagi Grimberg 		  enum isert_indicator ind,
2502e3d7e4c3SSagi Grimberg 		  struct ib_sge *sge)
250359464ef4SVu Pham {
2504dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
250567cb3949SSagi Grimberg 	struct ib_device *ib_dev = device->ib_device;
25069e961ae7SSagi Grimberg 	struct ib_mr *mr;
250716c2d702SSagi Grimberg 	struct ib_reg_wr reg_wr;
2508e622f2f4SChristoph Hellwig 	struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
250916c2d702SSagi Grimberg 	int ret, n;
251059464ef4SVu Pham 
2511e3d7e4c3SSagi Grimberg 	if (mem->dma_nents == 1) {
251234efc7dfSJason Gunthorpe 		sge->lkey = device->pd->local_dma_lkey;
2513e3d7e4c3SSagi Grimberg 		sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2514e3d7e4c3SSagi Grimberg 		sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
25154c22e07fSSagi Grimberg 		isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
25164c22e07fSSagi Grimberg 			 sge->addr, sge->length, sge->lkey);
2517e3d7e4c3SSagi Grimberg 		return 0;
2518e3d7e4c3SSagi Grimberg 	}
2519e3d7e4c3SSagi Grimberg 
252016c2d702SSagi Grimberg 	if (ind == ISERT_DATA_KEY_VALID)
25219e961ae7SSagi Grimberg 		/* Registering data buffer */
25229e961ae7SSagi Grimberg 		mr = fr_desc->data_mr;
252316c2d702SSagi Grimberg 	else
25249e961ae7SSagi Grimberg 		/* Registering protection buffer */
25259e961ae7SSagi Grimberg 		mr = fr_desc->pi_ctx->prot_mr;
252659464ef4SVu Pham 
252710633c37SSagi Grimberg 	if (!(fr_desc->ind & ind)) {
252810633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, mr);
252959464ef4SVu Pham 		wr = &inv_wr;
253059464ef4SVu Pham 	}
253159464ef4SVu Pham 
253216c2d702SSagi Grimberg 	n = ib_map_mr_sg(mr, mem->sg, mem->nents, PAGE_SIZE);
253316c2d702SSagi Grimberg 	if (unlikely(n != mem->nents)) {
253416c2d702SSagi Grimberg 		isert_err("failed to map mr sg (%d/%d)\n",
253516c2d702SSagi Grimberg 			 n, mem->nents);
253616c2d702SSagi Grimberg 		return n < 0 ? n : -EINVAL;
253716c2d702SSagi Grimberg 	}
253816c2d702SSagi Grimberg 
253916c2d702SSagi Grimberg 	isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
254016c2d702SSagi Grimberg 		  fr_desc, mem->nents, mem->offset);
254116c2d702SSagi Grimberg 
254216c2d702SSagi Grimberg 	reg_wr.wr.next = NULL;
254316c2d702SSagi Grimberg 	reg_wr.wr.opcode = IB_WR_REG_MR;
254416c2d702SSagi Grimberg 	reg_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
254516c2d702SSagi Grimberg 	reg_wr.wr.send_flags = 0;
254616c2d702SSagi Grimberg 	reg_wr.wr.num_sge = 0;
254716c2d702SSagi Grimberg 	reg_wr.mr = mr;
254816c2d702SSagi Grimberg 	reg_wr.key = mr->lkey;
254916c2d702SSagi Grimberg 	reg_wr.access = IB_ACCESS_LOCAL_WRITE;
255059464ef4SVu Pham 
255159464ef4SVu Pham 	if (!wr)
255216c2d702SSagi Grimberg 		wr = &reg_wr.wr;
255359464ef4SVu Pham 	else
255416c2d702SSagi Grimberg 		wr->next = &reg_wr.wr;
255559464ef4SVu Pham 
2556dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
255759464ef4SVu Pham 	if (ret) {
255824f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
255959464ef4SVu Pham 		return ret;
256059464ef4SVu Pham 	}
25619e961ae7SSagi Grimberg 	fr_desc->ind &= ~ind;
256259464ef4SVu Pham 
25639e961ae7SSagi Grimberg 	sge->lkey = mr->lkey;
256416c2d702SSagi Grimberg 	sge->addr = mr->iova;
256516c2d702SSagi Grimberg 	sge->length = mr->length;
256659464ef4SVu Pham 
25674c22e07fSSagi Grimberg 	isert_dbg("sge: addr: 0x%llx  length: %u lkey: %x\n",
25684c22e07fSSagi Grimberg 		  sge->addr, sge->length, sge->lkey);
256959464ef4SVu Pham 
257059464ef4SVu Pham 	return ret;
257159464ef4SVu Pham }
257259464ef4SVu Pham 
25733d73cf1aSSagi Grimberg static inline void
25743d73cf1aSSagi Grimberg isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
25753d73cf1aSSagi Grimberg 		     struct ib_sig_domain *domain)
25769e961ae7SSagi Grimberg {
257778eda2bbSSagi Grimberg 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
25783d73cf1aSSagi Grimberg 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
25793d73cf1aSSagi Grimberg 	domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
25803d73cf1aSSagi Grimberg 	domain->sig.dif.ref_tag = se_cmd->reftag_seed;
258178eda2bbSSagi Grimberg 	/*
258278eda2bbSSagi Grimberg 	 * At the moment we hard code those, but if in the future
258378eda2bbSSagi Grimberg 	 * the target core would like to use it, we will take it
258478eda2bbSSagi Grimberg 	 * from se_cmd.
258578eda2bbSSagi Grimberg 	 */
258678eda2bbSSagi Grimberg 	domain->sig.dif.apptag_check_mask = 0xffff;
258778eda2bbSSagi Grimberg 	domain->sig.dif.app_escape = true;
258878eda2bbSSagi Grimberg 	domain->sig.dif.ref_escape = true;
258978eda2bbSSagi Grimberg 	if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
259078eda2bbSSagi Grimberg 	    se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
259178eda2bbSSagi Grimberg 		domain->sig.dif.ref_remap = true;
25923d73cf1aSSagi Grimberg };
25933d73cf1aSSagi Grimberg 
25943d73cf1aSSagi Grimberg static int
25953d73cf1aSSagi Grimberg isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
25963d73cf1aSSagi Grimberg {
25979e961ae7SSagi Grimberg 	switch (se_cmd->prot_op) {
25989e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_INSERT:
25999e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_STRIP:
260078eda2bbSSagi Grimberg 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
26013d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
26029e961ae7SSagi Grimberg 		break;
26039e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_INSERT:
26049e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_STRIP:
260578eda2bbSSagi Grimberg 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
26063d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
26079e961ae7SSagi Grimberg 		break;
26089e961ae7SSagi Grimberg 	case TARGET_PROT_DIN_PASS:
26099e961ae7SSagi Grimberg 	case TARGET_PROT_DOUT_PASS:
26103d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
26113d73cf1aSSagi Grimberg 		isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
26129e961ae7SSagi Grimberg 		break;
26139e961ae7SSagi Grimberg 	default:
261424f412ddSSagi Grimberg 		isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
26159e961ae7SSagi Grimberg 		return -EINVAL;
26169e961ae7SSagi Grimberg 	}
26179e961ae7SSagi Grimberg 
26189e961ae7SSagi Grimberg 	return 0;
26199e961ae7SSagi Grimberg }
26209e961ae7SSagi Grimberg 
26219e961ae7SSagi Grimberg static inline u8
26229e961ae7SSagi Grimberg isert_set_prot_checks(u8 prot_checks)
26239e961ae7SSagi Grimberg {
26249e961ae7SSagi Grimberg 	return (prot_checks & TARGET_DIF_CHECK_GUARD  ? 0xc0 : 0) |
26259e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
26269e961ae7SSagi Grimberg 	       (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
26279e961ae7SSagi Grimberg }
26289e961ae7SSagi Grimberg 
26299e961ae7SSagi Grimberg static int
2630570db170SSagi Grimberg isert_reg_sig_mr(struct isert_conn *isert_conn,
2631570db170SSagi Grimberg 		 struct se_cmd *se_cmd,
2632570db170SSagi Grimberg 		 struct isert_rdma_wr *rdma_wr,
2633570db170SSagi Grimberg 		 struct fast_reg_descriptor *fr_desc)
26349e961ae7SSagi Grimberg {
2635e622f2f4SChristoph Hellwig 	struct ib_sig_handover_wr sig_wr;
2636e622f2f4SChristoph Hellwig 	struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
26379e961ae7SSagi Grimberg 	struct pi_context *pi_ctx = fr_desc->pi_ctx;
26389e961ae7SSagi Grimberg 	struct ib_sig_attrs sig_attrs;
26399e961ae7SSagi Grimberg 	int ret;
26409e961ae7SSagi Grimberg 
26419e961ae7SSagi Grimberg 	memset(&sig_attrs, 0, sizeof(sig_attrs));
26429e961ae7SSagi Grimberg 	ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
26439e961ae7SSagi Grimberg 	if (ret)
26449e961ae7SSagi Grimberg 		goto err;
26459e961ae7SSagi Grimberg 
26469e961ae7SSagi Grimberg 	sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
26479e961ae7SSagi Grimberg 
26489e961ae7SSagi Grimberg 	if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
264910633c37SSagi Grimberg 		isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
26509e961ae7SSagi Grimberg 		wr = &inv_wr;
26519e961ae7SSagi Grimberg 	}
26529e961ae7SSagi Grimberg 
26539e961ae7SSagi Grimberg 	memset(&sig_wr, 0, sizeof(sig_wr));
2654e622f2f4SChristoph Hellwig 	sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
2655e622f2f4SChristoph Hellwig 	sig_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
2656e622f2f4SChristoph Hellwig 	sig_wr.wr.sg_list = &rdma_wr->ib_sg[DATA];
2657e622f2f4SChristoph Hellwig 	sig_wr.wr.num_sge = 1;
2658e622f2f4SChristoph Hellwig 	sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
2659e622f2f4SChristoph Hellwig 	sig_wr.sig_attrs = &sig_attrs;
2660e622f2f4SChristoph Hellwig 	sig_wr.sig_mr = pi_ctx->sig_mr;
26619e961ae7SSagi Grimberg 	if (se_cmd->t_prot_sg)
2662e622f2f4SChristoph Hellwig 		sig_wr.prot = &rdma_wr->ib_sg[PROT];
26639e961ae7SSagi Grimberg 
26649e961ae7SSagi Grimberg 	if (!wr)
2665e622f2f4SChristoph Hellwig 		wr = &sig_wr.wr;
26669e961ae7SSagi Grimberg 	else
2667e622f2f4SChristoph Hellwig 		wr->next = &sig_wr.wr;
26689e961ae7SSagi Grimberg 
2669dac6ab30SSagi Grimberg 	ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
26709e961ae7SSagi Grimberg 	if (ret) {
267124f412ddSSagi Grimberg 		isert_err("fast registration failed, ret:%d\n", ret);
26729e961ae7SSagi Grimberg 		goto err;
26739e961ae7SSagi Grimberg 	}
26749e961ae7SSagi Grimberg 	fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
26759e961ae7SSagi Grimberg 
2676570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2677570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].addr = 0;
2678570db170SSagi Grimberg 	rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
26799e961ae7SSagi Grimberg 	if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
26809e961ae7SSagi Grimberg 	    se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
26819e961ae7SSagi Grimberg 		/*
26829e961ae7SSagi Grimberg 		 * We have protection guards on the wire
26839e961ae7SSagi Grimberg 		 * so we need to set a larget transfer
26849e961ae7SSagi Grimberg 		 */
2685570db170SSagi Grimberg 		rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
26869e961ae7SSagi Grimberg 
268724f412ddSSagi Grimberg 	isert_dbg("sig_sge: addr: 0x%llx  length: %u lkey: %x\n",
2688570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2689570db170SSagi Grimberg 		  rdma_wr->ib_sg[SIG].lkey);
26909e961ae7SSagi Grimberg err:
26919e961ae7SSagi Grimberg 	return ret;
26929e961ae7SSagi Grimberg }
26939e961ae7SSagi Grimberg 
269459464ef4SVu Pham static int
2695570db170SSagi Grimberg isert_handle_prot_cmd(struct isert_conn *isert_conn,
2696570db170SSagi Grimberg 		      struct isert_cmd *isert_cmd,
2697570db170SSagi Grimberg 		      struct isert_rdma_wr *wr)
2698570db170SSagi Grimberg {
2699dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
2700570db170SSagi Grimberg 	struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2701570db170SSagi Grimberg 	int ret;
2702570db170SSagi Grimberg 
2703570db170SSagi Grimberg 	if (!wr->fr_desc->pi_ctx) {
2704570db170SSagi Grimberg 		ret = isert_create_pi_ctx(wr->fr_desc,
2705570db170SSagi Grimberg 					  device->ib_device,
270667cb3949SSagi Grimberg 					  device->pd);
2707570db170SSagi Grimberg 		if (ret) {
270824f412ddSSagi Grimberg 			isert_err("conn %p failed to allocate pi_ctx\n",
2709570db170SSagi Grimberg 				  isert_conn);
2710570db170SSagi Grimberg 			return ret;
2711570db170SSagi Grimberg 		}
2712570db170SSagi Grimberg 	}
2713570db170SSagi Grimberg 
2714570db170SSagi Grimberg 	if (se_cmd->t_prot_sg) {
2715570db170SSagi Grimberg 		ret = isert_map_data_buf(isert_conn, isert_cmd,
2716570db170SSagi Grimberg 					 se_cmd->t_prot_sg,
2717570db170SSagi Grimberg 					 se_cmd->t_prot_nents,
2718570db170SSagi Grimberg 					 se_cmd->prot_length,
2719570db170SSagi Grimberg 					 0, wr->iser_ib_op, &wr->prot);
2720570db170SSagi Grimberg 		if (ret) {
272124f412ddSSagi Grimberg 			isert_err("conn %p failed to map protection buffer\n",
2722570db170SSagi Grimberg 				  isert_conn);
2723570db170SSagi Grimberg 			return ret;
2724570db170SSagi Grimberg 		}
2725570db170SSagi Grimberg 
2726570db170SSagi Grimberg 		memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2727570db170SSagi Grimberg 		ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2728570db170SSagi Grimberg 					ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2729570db170SSagi Grimberg 		if (ret) {
273024f412ddSSagi Grimberg 			isert_err("conn %p failed to fast reg mr\n",
2731570db170SSagi Grimberg 				  isert_conn);
2732570db170SSagi Grimberg 			goto unmap_prot_cmd;
2733570db170SSagi Grimberg 		}
2734570db170SSagi Grimberg 	}
2735570db170SSagi Grimberg 
2736570db170SSagi Grimberg 	ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2737570db170SSagi Grimberg 	if (ret) {
273824f412ddSSagi Grimberg 		isert_err("conn %p failed to fast reg mr\n",
2739570db170SSagi Grimberg 			  isert_conn);
2740570db170SSagi Grimberg 		goto unmap_prot_cmd;
2741570db170SSagi Grimberg 	}
2742570db170SSagi Grimberg 	wr->fr_desc->ind |= ISERT_PROTECTED;
2743570db170SSagi Grimberg 
2744570db170SSagi Grimberg 	return 0;
2745570db170SSagi Grimberg 
2746570db170SSagi Grimberg unmap_prot_cmd:
2747570db170SSagi Grimberg 	if (se_cmd->t_prot_sg)
2748570db170SSagi Grimberg 		isert_unmap_data_buf(isert_conn, &wr->prot);
2749570db170SSagi Grimberg 
2750570db170SSagi Grimberg 	return ret;
2751570db170SSagi Grimberg }
2752570db170SSagi Grimberg 
2753570db170SSagi Grimberg static int
2754a3a5a826SSagi Grimberg isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
275559464ef4SVu Pham 	       struct isert_rdma_wr *wr)
275659464ef4SVu Pham {
275759464ef4SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
275859464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2759e3d7e4c3SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2760e3d7e4c3SSagi Grimberg 	struct fast_reg_descriptor *fr_desc = NULL;
2761e622f2f4SChristoph Hellwig 	struct ib_rdma_wr *rdma_wr;
2762570db170SSagi Grimberg 	struct ib_sge *ib_sg;
2763e3d7e4c3SSagi Grimberg 	u32 offset;
2764e3d7e4c3SSagi Grimberg 	int ret = 0;
276559464ef4SVu Pham 	unsigned long flags;
276659464ef4SVu Pham 
276759464ef4SVu Pham 	isert_cmd->tx_desc.isert_cmd = isert_cmd;
2768e3d7e4c3SSagi Grimberg 
2769e3d7e4c3SSagi Grimberg 	offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2770e3d7e4c3SSagi Grimberg 	ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2771e3d7e4c3SSagi Grimberg 				 se_cmd->t_data_nents, se_cmd->data_length,
2772e3d7e4c3SSagi Grimberg 				 offset, wr->iser_ib_op, &wr->data);
2773e3d7e4c3SSagi Grimberg 	if (ret)
2774e3d7e4c3SSagi Grimberg 		return ret;
2775e3d7e4c3SSagi Grimberg 
2776302cc7c3SSagi Grimberg 	if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
2777dac6ab30SSagi Grimberg 		spin_lock_irqsave(&isert_conn->pool_lock, flags);
2778dac6ab30SSagi Grimberg 		fr_desc = list_first_entry(&isert_conn->fr_pool,
2779e3d7e4c3SSagi Grimberg 					   struct fast_reg_descriptor, list);
2780e3d7e4c3SSagi Grimberg 		list_del(&fr_desc->list);
2781dac6ab30SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
2782e3d7e4c3SSagi Grimberg 		wr->fr_desc = fr_desc;
278359464ef4SVu Pham 	}
278459464ef4SVu Pham 
27859e961ae7SSagi Grimberg 	ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2786570db170SSagi Grimberg 				ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
2787e3d7e4c3SSagi Grimberg 	if (ret)
2788e3d7e4c3SSagi Grimberg 		goto unmap_cmd;
278959464ef4SVu Pham 
2790302cc7c3SSagi Grimberg 	if (isert_prot_cmd(isert_conn, se_cmd)) {
2791570db170SSagi Grimberg 		ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
27929e961ae7SSagi Grimberg 		if (ret)
27939e961ae7SSagi Grimberg 			goto unmap_cmd;
27949e961ae7SSagi Grimberg 
2795570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[SIG];
2796570db170SSagi Grimberg 	} else {
2797570db170SSagi Grimberg 		ib_sg = &wr->ib_sg[DATA];
27989e961ae7SSagi Grimberg 	}
27999e961ae7SSagi Grimberg 
2800570db170SSagi Grimberg 	memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
2801e3d7e4c3SSagi Grimberg 	wr->ib_sge = &wr->s_ib_sge;
2802e622f2f4SChristoph Hellwig 	wr->rdma_wr_num = 1;
2803e622f2f4SChristoph Hellwig 	memset(&wr->s_rdma_wr, 0, sizeof(wr->s_rdma_wr));
2804e622f2f4SChristoph Hellwig 	wr->rdma_wr = &wr->s_rdma_wr;
280559464ef4SVu Pham 	wr->isert_cmd = isert_cmd;
280659464ef4SVu Pham 
2807e622f2f4SChristoph Hellwig 	rdma_wr = &isert_cmd->rdma_wr.s_rdma_wr;
2808e622f2f4SChristoph Hellwig 	rdma_wr->wr.sg_list = &wr->s_ib_sge;
2809e622f2f4SChristoph Hellwig 	rdma_wr->wr.num_sge = 1;
2810e622f2f4SChristoph Hellwig 	rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
281159464ef4SVu Pham 	if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2812e622f2f4SChristoph Hellwig 		rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2813e622f2f4SChristoph Hellwig 		rdma_wr->remote_addr = isert_cmd->read_va;
2814e622f2f4SChristoph Hellwig 		rdma_wr->rkey = isert_cmd->read_stag;
2815e622f2f4SChristoph Hellwig 		rdma_wr->wr.send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
28169e961ae7SSagi Grimberg 				      0 : IB_SEND_SIGNALED;
281759464ef4SVu Pham 	} else {
2818e622f2f4SChristoph Hellwig 		rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2819e622f2f4SChristoph Hellwig 		rdma_wr->remote_addr = isert_cmd->write_va;
2820e622f2f4SChristoph Hellwig 		rdma_wr->rkey = isert_cmd->write_stag;
2821e622f2f4SChristoph Hellwig 		rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
282259464ef4SVu Pham 	}
282359464ef4SVu Pham 
282459464ef4SVu Pham 	return 0;
2825570db170SSagi Grimberg 
2826e3d7e4c3SSagi Grimberg unmap_cmd:
2827e3d7e4c3SSagi Grimberg 	if (fr_desc) {
2828dac6ab30SSagi Grimberg 		spin_lock_irqsave(&isert_conn->pool_lock, flags);
2829dac6ab30SSagi Grimberg 		list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2830dac6ab30SSagi Grimberg 		spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
2831e3d7e4c3SSagi Grimberg 	}
2832e3d7e4c3SSagi Grimberg 	isert_unmap_data_buf(isert_conn, &wr->data);
283359464ef4SVu Pham 
283459464ef4SVu Pham 	return ret;
283559464ef4SVu Pham }
283659464ef4SVu Pham 
283759464ef4SVu Pham static int
283890ecc6e2SVu Pham isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
283990ecc6e2SVu Pham {
284090ecc6e2SVu Pham 	struct se_cmd *se_cmd = &cmd->se_cmd;
284159464ef4SVu Pham 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
284290ecc6e2SVu Pham 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
28436700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2844dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
284590ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
284690ecc6e2SVu Pham 	int rc;
284790ecc6e2SVu Pham 
284824f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
284990ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length);
28504c22e07fSSagi Grimberg 
285190ecc6e2SVu Pham 	wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2852d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
285390ecc6e2SVu Pham 	if (rc) {
285424f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
285590ecc6e2SVu Pham 		return rc;
285690ecc6e2SVu Pham 	}
285790ecc6e2SVu Pham 
2858302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd)) {
2859b8d26b3bSNicholas Bellinger 		/*
2860b8d26b3bSNicholas Bellinger 		 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2861b8d26b3bSNicholas Bellinger 		 */
28629e961ae7SSagi Grimberg 		isert_create_send_desc(isert_conn, isert_cmd,
28639e961ae7SSagi Grimberg 				       &isert_cmd->tx_desc);
286404d9cd12SNicholas Bellinger 		iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2865b8d26b3bSNicholas Bellinger 				     &isert_cmd->tx_desc.iscsi_header);
2866b8d26b3bSNicholas Bellinger 		isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
286795b60f07SNicholas Bellinger 		isert_init_send_wr(isert_conn, isert_cmd,
286868a86deeSSagi Grimberg 				   &isert_cmd->tx_desc.send_wr);
2869e622f2f4SChristoph Hellwig 		isert_cmd->rdma_wr.s_rdma_wr.wr.next = &isert_cmd->tx_desc.send_wr;
2870e622f2f4SChristoph Hellwig 		wr->rdma_wr_num += 1;
28714366b19cSJenny Derzhavetz 
28724366b19cSJenny Derzhavetz 		rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
28734366b19cSJenny Derzhavetz 		if (rc) {
28744366b19cSJenny Derzhavetz 			isert_err("ib_post_recv failed with %d\n", rc);
28754366b19cSJenny Derzhavetz 			return rc;
28764366b19cSJenny Derzhavetz 		}
28779e961ae7SSagi Grimberg 	}
2878b8d26b3bSNicholas Bellinger 
2879e622f2f4SChristoph Hellwig 	rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
2880bdf20e72SSagi Grimberg 	if (rc)
288124f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
28829e961ae7SSagi Grimberg 
2883302cc7c3SSagi Grimberg 	if (!isert_prot_cmd(isert_conn, se_cmd))
288424f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
28859e961ae7SSagi Grimberg 			 "READ\n", isert_cmd);
28869e961ae7SSagi Grimberg 	else
288724f412ddSSagi Grimberg 		isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
288890ecc6e2SVu Pham 			 isert_cmd);
2889b8d26b3bSNicholas Bellinger 
289090ecc6e2SVu Pham 	return 1;
2891b8d26b3bSNicholas Bellinger }
2892b8d26b3bSNicholas Bellinger 
2893b8d26b3bSNicholas Bellinger static int
2894b8d26b3bSNicholas Bellinger isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2895b8d26b3bSNicholas Bellinger {
2896b8d26b3bSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
2897d703ce2fSNicholas Bellinger 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2898b8d26b3bSNicholas Bellinger 	struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
28996700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2900dac6ab30SSagi Grimberg 	struct isert_device *device = isert_conn->device;
290190ecc6e2SVu Pham 	struct ib_send_wr *wr_failed;
290290ecc6e2SVu Pham 	int rc;
2903b8d26b3bSNicholas Bellinger 
290424f412ddSSagi Grimberg 	isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
290590ecc6e2SVu Pham 		 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2906b8d26b3bSNicholas Bellinger 	wr->iser_ib_op = ISER_IB_RDMA_READ;
2907d40945d8SVu Pham 	rc = device->reg_rdma_mem(conn, cmd, wr);
290890ecc6e2SVu Pham 	if (rc) {
290924f412ddSSagi Grimberg 		isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
291090ecc6e2SVu Pham 		return rc;
2911b8d26b3bSNicholas Bellinger 	}
2912b8d26b3bSNicholas Bellinger 
2913e622f2f4SChristoph Hellwig 	rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
2914bdf20e72SSagi Grimberg 	if (rc)
291524f412ddSSagi Grimberg 		isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2916bdf20e72SSagi Grimberg 
291724f412ddSSagi Grimberg 	isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
291890ecc6e2SVu Pham 		 isert_cmd);
2919b8d26b3bSNicholas Bellinger 
292090ecc6e2SVu Pham 	return 0;
2921b8d26b3bSNicholas Bellinger }
2922b8d26b3bSNicholas Bellinger 
2923b8d26b3bSNicholas Bellinger static int
2924b8d26b3bSNicholas Bellinger isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2925b8d26b3bSNicholas Bellinger {
2926a4c15cd9SJenny Derzhavetz 	struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2927a4c15cd9SJenny Derzhavetz 	int ret = 0;
2928b8d26b3bSNicholas Bellinger 
2929b8d26b3bSNicholas Bellinger 	switch (state) {
2930a4c15cd9SJenny Derzhavetz 	case ISTATE_REMOVE:
2931a4c15cd9SJenny Derzhavetz 		spin_lock_bh(&conn->cmd_lock);
2932a4c15cd9SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
2933a4c15cd9SJenny Derzhavetz 		spin_unlock_bh(&conn->cmd_lock);
2934a4c15cd9SJenny Derzhavetz 		isert_put_cmd(isert_cmd, true);
2935a4c15cd9SJenny Derzhavetz 		break;
2936b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2937b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, false);
2938b8d26b3bSNicholas Bellinger 		break;
2939b8d26b3bSNicholas Bellinger 	default:
294024f412ddSSagi Grimberg 		isert_err("Unknown immediate state: 0x%02x\n", state);
2941b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2942b8d26b3bSNicholas Bellinger 		break;
2943b8d26b3bSNicholas Bellinger 	}
2944b8d26b3bSNicholas Bellinger 
2945b8d26b3bSNicholas Bellinger 	return ret;
2946b8d26b3bSNicholas Bellinger }
2947b8d26b3bSNicholas Bellinger 
2948b8d26b3bSNicholas Bellinger static int
2949b8d26b3bSNicholas Bellinger isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2950b8d26b3bSNicholas Bellinger {
2951991bb764SSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
2952b8d26b3bSNicholas Bellinger 	int ret;
2953b8d26b3bSNicholas Bellinger 
2954b8d26b3bSNicholas Bellinger 	switch (state) {
2955b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
2956b8d26b3bSNicholas Bellinger 		ret = isert_put_logout_rsp(cmd, conn);
2957991bb764SSagi Grimberg 		if (!ret)
2958991bb764SSagi Grimberg 			isert_conn->logout_posted = true;
2959b8d26b3bSNicholas Bellinger 		break;
2960b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
2961b8d26b3bSNicholas Bellinger 		ret = isert_put_nopin(cmd, conn, true);
2962b8d26b3bSNicholas Bellinger 		break;
2963b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
2964b8d26b3bSNicholas Bellinger 		ret = isert_put_tm_rsp(cmd, conn);
2965b8d26b3bSNicholas Bellinger 		break;
2966b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_REJECT:
2967b8d26b3bSNicholas Bellinger 		ret = isert_put_reject(cmd, conn);
2968b8d26b3bSNicholas Bellinger 		break;
2969adb54c29SNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
2970adb54c29SNicholas Bellinger 		ret = isert_put_text_rsp(cmd, conn);
2971adb54c29SNicholas Bellinger 		break;
2972b8d26b3bSNicholas Bellinger 	case ISTATE_SEND_STATUS:
2973b8d26b3bSNicholas Bellinger 		/*
2974b8d26b3bSNicholas Bellinger 		 * Special case for sending non GOOD SCSI status from TX thread
2975b8d26b3bSNicholas Bellinger 		 * context during pre se_cmd excecution failure.
2976b8d26b3bSNicholas Bellinger 		 */
2977b8d26b3bSNicholas Bellinger 		ret = isert_put_response(conn, cmd);
2978b8d26b3bSNicholas Bellinger 		break;
2979b8d26b3bSNicholas Bellinger 	default:
298024f412ddSSagi Grimberg 		isert_err("Unknown response state: 0x%02x\n", state);
2981b8d26b3bSNicholas Bellinger 		ret = -EINVAL;
2982b8d26b3bSNicholas Bellinger 		break;
2983b8d26b3bSNicholas Bellinger 	}
2984b8d26b3bSNicholas Bellinger 
2985b8d26b3bSNicholas Bellinger 	return ret;
2986b8d26b3bSNicholas Bellinger }
2987b8d26b3bSNicholas Bellinger 
2988ca6c1d82SSagi Grimberg struct rdma_cm_id *
2989ca6c1d82SSagi Grimberg isert_setup_id(struct isert_np *isert_np)
2990ca6c1d82SSagi Grimberg {
2991ca6c1d82SSagi Grimberg 	struct iscsi_np *np = isert_np->np;
2992ca6c1d82SSagi Grimberg 	struct rdma_cm_id *id;
2993ca6c1d82SSagi Grimberg 	struct sockaddr *sa;
2994ca6c1d82SSagi Grimberg 	int ret;
2995ca6c1d82SSagi Grimberg 
2996ca6c1d82SSagi Grimberg 	sa = (struct sockaddr *)&np->np_sockaddr;
299724f412ddSSagi Grimberg 	isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
2998ca6c1d82SSagi Grimberg 
2999fa20105eSGuy Shapiro 	id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
3000ca6c1d82SSagi Grimberg 			    RDMA_PS_TCP, IB_QPT_RC);
3001ca6c1d82SSagi Grimberg 	if (IS_ERR(id)) {
300224f412ddSSagi Grimberg 		isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
3003ca6c1d82SSagi Grimberg 		ret = PTR_ERR(id);
3004ca6c1d82SSagi Grimberg 		goto out;
3005ca6c1d82SSagi Grimberg 	}
300624f412ddSSagi Grimberg 	isert_dbg("id %p context %p\n", id, id->context);
3007ca6c1d82SSagi Grimberg 
3008ca6c1d82SSagi Grimberg 	ret = rdma_bind_addr(id, sa);
3009ca6c1d82SSagi Grimberg 	if (ret) {
301024f412ddSSagi Grimberg 		isert_err("rdma_bind_addr() failed: %d\n", ret);
3011ca6c1d82SSagi Grimberg 		goto out_id;
3012ca6c1d82SSagi Grimberg 	}
3013ca6c1d82SSagi Grimberg 
3014992607e8SSagi Grimberg 	ret = rdma_listen(id, 0);
3015ca6c1d82SSagi Grimberg 	if (ret) {
301624f412ddSSagi Grimberg 		isert_err("rdma_listen() failed: %d\n", ret);
3017ca6c1d82SSagi Grimberg 		goto out_id;
3018ca6c1d82SSagi Grimberg 	}
3019ca6c1d82SSagi Grimberg 
3020ca6c1d82SSagi Grimberg 	return id;
3021ca6c1d82SSagi Grimberg out_id:
3022ca6c1d82SSagi Grimberg 	rdma_destroy_id(id);
3023ca6c1d82SSagi Grimberg out:
3024ca6c1d82SSagi Grimberg 	return ERR_PTR(ret);
3025ca6c1d82SSagi Grimberg }
3026ca6c1d82SSagi Grimberg 
3027b8d26b3bSNicholas Bellinger static int
3028b8d26b3bSNicholas Bellinger isert_setup_np(struct iscsi_np *np,
302913a3cf08SAndy Grover 	       struct sockaddr_storage *ksockaddr)
3030b8d26b3bSNicholas Bellinger {
3031b8d26b3bSNicholas Bellinger 	struct isert_np *isert_np;
3032b8d26b3bSNicholas Bellinger 	struct rdma_cm_id *isert_lid;
3033b8d26b3bSNicholas Bellinger 	int ret;
3034b8d26b3bSNicholas Bellinger 
3035b8d26b3bSNicholas Bellinger 	isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3036b8d26b3bSNicholas Bellinger 	if (!isert_np) {
303724f412ddSSagi Grimberg 		isert_err("Unable to allocate struct isert_np\n");
3038b8d26b3bSNicholas Bellinger 		return -ENOMEM;
3039b8d26b3bSNicholas Bellinger 	}
3040ed8cb0a4SJenny Derzhavetz 	sema_init(&isert_np->sem, 0);
3041ed8cb0a4SJenny Derzhavetz 	mutex_init(&isert_np->mutex);
3042bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->accepted);
3043bd379220SJenny Derzhavetz 	INIT_LIST_HEAD(&isert_np->pending);
3044ca6c1d82SSagi Grimberg 	isert_np->np = np;
3045b8d26b3bSNicholas Bellinger 
3046b8d26b3bSNicholas Bellinger 	/*
3047b8d26b3bSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
3048b8d26b3bSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
3049b8d26b3bSNicholas Bellinger 	 */
3050b8d26b3bSNicholas Bellinger 	memcpy(&np->np_sockaddr, ksockaddr,
305113a3cf08SAndy Grover 	       sizeof(struct sockaddr_storage));
3052b8d26b3bSNicholas Bellinger 
3053ca6c1d82SSagi Grimberg 	isert_lid = isert_setup_id(isert_np);
3054b8d26b3bSNicholas Bellinger 	if (IS_ERR(isert_lid)) {
3055b8d26b3bSNicholas Bellinger 		ret = PTR_ERR(isert_lid);
3056b8d26b3bSNicholas Bellinger 		goto out;
3057b8d26b3bSNicholas Bellinger 	}
3058b8d26b3bSNicholas Bellinger 
3059ed8cb0a4SJenny Derzhavetz 	isert_np->cm_id = isert_lid;
3060b8d26b3bSNicholas Bellinger 	np->np_context = isert_np;
3061b8d26b3bSNicholas Bellinger 
3062b8d26b3bSNicholas Bellinger 	return 0;
3063b8d26b3bSNicholas Bellinger 
3064b8d26b3bSNicholas Bellinger out:
3065b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3066ca6c1d82SSagi Grimberg 
3067b8d26b3bSNicholas Bellinger 	return ret;
3068b8d26b3bSNicholas Bellinger }
3069b8d26b3bSNicholas Bellinger 
3070b8d26b3bSNicholas Bellinger static int
3071b8d26b3bSNicholas Bellinger isert_rdma_accept(struct isert_conn *isert_conn)
3072b8d26b3bSNicholas Bellinger {
3073dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
3074b8d26b3bSNicholas Bellinger 	struct rdma_conn_param cp;
3075b8d26b3bSNicholas Bellinger 	int ret;
3076b8d26b3bSNicholas Bellinger 
3077b8d26b3bSNicholas Bellinger 	memset(&cp, 0, sizeof(struct rdma_conn_param));
3078b8d26b3bSNicholas Bellinger 	cp.initiator_depth = isert_conn->initiator_depth;
3079b8d26b3bSNicholas Bellinger 	cp.retry_count = 7;
3080b8d26b3bSNicholas Bellinger 	cp.rnr_retry_count = 7;
3081b8d26b3bSNicholas Bellinger 
3082b8d26b3bSNicholas Bellinger 	ret = rdma_accept(cm_id, &cp);
3083b8d26b3bSNicholas Bellinger 	if (ret) {
308424f412ddSSagi Grimberg 		isert_err("rdma_accept() failed with: %d\n", ret);
3085b8d26b3bSNicholas Bellinger 		return ret;
3086b8d26b3bSNicholas Bellinger 	}
3087b8d26b3bSNicholas Bellinger 
3088b8d26b3bSNicholas Bellinger 	return 0;
3089b8d26b3bSNicholas Bellinger }
3090b8d26b3bSNicholas Bellinger 
3091b8d26b3bSNicholas Bellinger static int
3092b8d26b3bSNicholas Bellinger isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3093b8d26b3bSNicholas Bellinger {
30946700425eSSagi Grimberg 	struct isert_conn *isert_conn = conn->context;
3095b8d26b3bSNicholas Bellinger 	int ret;
3096b8d26b3bSNicholas Bellinger 
309724f412ddSSagi Grimberg 	isert_info("before login_req comp conn: %p\n", isert_conn);
30982371e5daSSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
30992371e5daSSagi Grimberg 	if (ret) {
310024f412ddSSagi Grimberg 		isert_err("isert_conn %p interrupted before got login req\n",
31012371e5daSSagi Grimberg 			  isert_conn);
31022371e5daSSagi Grimberg 		return ret;
31032371e5daSSagi Grimberg 	}
31042371e5daSSagi Grimberg 	reinit_completion(&isert_conn->login_req_comp);
31052371e5daSSagi Grimberg 
31066faaa85fSNicholas Bellinger 	/*
31076faaa85fSNicholas Bellinger 	 * For login requests after the first PDU, isert_rx_login_req() will
31086faaa85fSNicholas Bellinger 	 * kick schedule_delayed_work(&conn->login_work) as the packet is
31096faaa85fSNicholas Bellinger 	 * received, which turns this callback from iscsi_target_do_login_rx()
31106faaa85fSNicholas Bellinger 	 * into a NOP.
31116faaa85fSNicholas Bellinger 	 */
31126faaa85fSNicholas Bellinger 	if (!login->first_request)
31136faaa85fSNicholas Bellinger 		return 0;
3114b8d26b3bSNicholas Bellinger 
31152371e5daSSagi Grimberg 	isert_rx_login_req(isert_conn);
31162371e5daSSagi Grimberg 
3117dac6ab30SSagi Grimberg 	isert_info("before login_comp conn: %p\n", conn);
3118dac6ab30SSagi Grimberg 	ret = wait_for_completion_interruptible(&isert_conn->login_comp);
3119b8d26b3bSNicholas Bellinger 	if (ret)
3120b8d26b3bSNicholas Bellinger 		return ret;
3121b8d26b3bSNicholas Bellinger 
312224f412ddSSagi Grimberg 	isert_info("processing login->req: %p\n", login->req);
31232371e5daSSagi Grimberg 
3124b8d26b3bSNicholas Bellinger 	return 0;
3125b8d26b3bSNicholas Bellinger }
3126b8d26b3bSNicholas Bellinger 
3127b8d26b3bSNicholas Bellinger static void
3128b8d26b3bSNicholas Bellinger isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3129b8d26b3bSNicholas Bellinger 		    struct isert_conn *isert_conn)
3130b8d26b3bSNicholas Bellinger {
3131dac6ab30SSagi Grimberg 	struct rdma_cm_id *cm_id = isert_conn->cm_id;
3132b8d26b3bSNicholas Bellinger 	struct rdma_route *cm_route = &cm_id->route;
3133b8d26b3bSNicholas Bellinger 
3134b8d26b3bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
3135b8d26b3bSNicholas Bellinger 
3136dc58f760SAndy Grover 	conn->login_sockaddr = cm_route->addr.dst_addr;
3137dc58f760SAndy Grover 	conn->local_sockaddr = cm_route->addr.src_addr;
3138b8d26b3bSNicholas Bellinger }
3139b8d26b3bSNicholas Bellinger 
3140b8d26b3bSNicholas Bellinger static int
3141b8d26b3bSNicholas Bellinger isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3142b8d26b3bSNicholas Bellinger {
31436700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
3144b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn;
3145c6b8e918SSagi Grimberg 	int ret;
3146b8d26b3bSNicholas Bellinger 
3147b8d26b3bSNicholas Bellinger accept_wait:
3148ed8cb0a4SJenny Derzhavetz 	ret = down_interruptible(&isert_np->sem);
3149c6b8e918SSagi Grimberg 	if (ret)
3150b8d26b3bSNicholas Bellinger 		return -ENODEV;
3151b8d26b3bSNicholas Bellinger 
3152b8d26b3bSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
3153e346ab34SSagi Grimberg 	if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
3154b8d26b3bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
315511378cdbSRasmus Villemoes 		isert_dbg("np_thread_state %d\n",
3156e346ab34SSagi Grimberg 			 np->np_thread_state);
3157e346ab34SSagi Grimberg 		/**
3158e346ab34SSagi Grimberg 		 * No point in stalling here when np_thread
3159e346ab34SSagi Grimberg 		 * is in state RESET/SHUTDOWN/EXIT - bail
3160e346ab34SSagi Grimberg 		 **/
3161b8d26b3bSNicholas Bellinger 		return -ENODEV;
3162b8d26b3bSNicholas Bellinger 	}
3163b8d26b3bSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
3164b8d26b3bSNicholas Bellinger 
3165ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
3166bd379220SJenny Derzhavetz 	if (list_empty(&isert_np->pending)) {
3167ed8cb0a4SJenny Derzhavetz 		mutex_unlock(&isert_np->mutex);
3168b8d26b3bSNicholas Bellinger 		goto accept_wait;
3169b8d26b3bSNicholas Bellinger 	}
3170bd379220SJenny Derzhavetz 	isert_conn = list_first_entry(&isert_np->pending,
3171bd379220SJenny Derzhavetz 			struct isert_conn, node);
3172bd379220SJenny Derzhavetz 	list_del_init(&isert_conn->node);
3173ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
3174b8d26b3bSNicholas Bellinger 
3175b8d26b3bSNicholas Bellinger 	conn->context = isert_conn;
3176b8d26b3bSNicholas Bellinger 	isert_conn->conn = conn;
3177b8d26b3bSNicholas Bellinger 
3178b8d26b3bSNicholas Bellinger 	isert_set_conn_info(np, conn, isert_conn);
3179b8d26b3bSNicholas Bellinger 
318024f412ddSSagi Grimberg 	isert_dbg("Processing isert_conn: %p\n", isert_conn);
31812371e5daSSagi Grimberg 
3182b8d26b3bSNicholas Bellinger 	return 0;
3183b8d26b3bSNicholas Bellinger }
3184b8d26b3bSNicholas Bellinger 
3185b8d26b3bSNicholas Bellinger static void
3186b8d26b3bSNicholas Bellinger isert_free_np(struct iscsi_np *np)
3187b8d26b3bSNicholas Bellinger {
31886700425eSSagi Grimberg 	struct isert_np *isert_np = np->np_context;
3189268e6811SSagi Grimberg 	struct isert_conn *isert_conn, *n;
3190b8d26b3bSNicholas Bellinger 
3191ed8cb0a4SJenny Derzhavetz 	if (isert_np->cm_id)
3192ed8cb0a4SJenny Derzhavetz 		rdma_destroy_id(isert_np->cm_id);
3193b8d26b3bSNicholas Bellinger 
3194268e6811SSagi Grimberg 	/*
3195268e6811SSagi Grimberg 	 * FIXME: At this point we don't have a good way to insure
3196268e6811SSagi Grimberg 	 * that at this point we don't have hanging connections that
3197268e6811SSagi Grimberg 	 * completed RDMA establishment but didn't start iscsi login
3198268e6811SSagi Grimberg 	 * process. So work-around this by cleaning up what ever piled
3199bd379220SJenny Derzhavetz 	 * up in accepted and pending lists.
3200268e6811SSagi Grimberg 	 */
3201ed8cb0a4SJenny Derzhavetz 	mutex_lock(&isert_np->mutex);
3202bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->pending)) {
3203bd379220SJenny Derzhavetz 		isert_info("Still have isert pending connections\n");
3204268e6811SSagi Grimberg 		list_for_each_entry_safe(isert_conn, n,
3205bd379220SJenny Derzhavetz 					 &isert_np->pending,
3206bd379220SJenny Derzhavetz 					 node) {
3207bd379220SJenny Derzhavetz 			isert_info("cleaning isert_conn %p state (%d)\n",
3208bd379220SJenny Derzhavetz 				   isert_conn, isert_conn->state);
3209bd379220SJenny Derzhavetz 			isert_connect_release(isert_conn);
3210bd379220SJenny Derzhavetz 		}
3211bd379220SJenny Derzhavetz 	}
3212bd379220SJenny Derzhavetz 
3213bd379220SJenny Derzhavetz 	if (!list_empty(&isert_np->accepted)) {
3214bd379220SJenny Derzhavetz 		isert_info("Still have isert accepted connections\n");
3215bd379220SJenny Derzhavetz 		list_for_each_entry_safe(isert_conn, n,
3216bd379220SJenny Derzhavetz 					 &isert_np->accepted,
3217bd379220SJenny Derzhavetz 					 node) {
321824f412ddSSagi Grimberg 			isert_info("cleaning isert_conn %p state (%d)\n",
3219268e6811SSagi Grimberg 				   isert_conn, isert_conn->state);
3220268e6811SSagi Grimberg 			isert_connect_release(isert_conn);
3221268e6811SSagi Grimberg 		}
3222268e6811SSagi Grimberg 	}
3223ed8cb0a4SJenny Derzhavetz 	mutex_unlock(&isert_np->mutex);
3224268e6811SSagi Grimberg 
3225b8d26b3bSNicholas Bellinger 	np->np_context = NULL;
3226b8d26b3bSNicholas Bellinger 	kfree(isert_np);
3227b8d26b3bSNicholas Bellinger }
3228b8d26b3bSNicholas Bellinger 
3229b02efbfcSSagi Grimberg static void isert_release_work(struct work_struct *work)
3230b02efbfcSSagi Grimberg {
3231b02efbfcSSagi Grimberg 	struct isert_conn *isert_conn = container_of(work,
3232b02efbfcSSagi Grimberg 						     struct isert_conn,
3233b02efbfcSSagi Grimberg 						     release_work);
3234b02efbfcSSagi Grimberg 
323524f412ddSSagi Grimberg 	isert_info("Starting release conn %p\n", isert_conn);
3236b02efbfcSSagi Grimberg 
3237dac6ab30SSagi Grimberg 	wait_for_completion(&isert_conn->wait);
3238b02efbfcSSagi Grimberg 
3239dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
3240b02efbfcSSagi Grimberg 	isert_conn->state = ISER_CONN_DOWN;
3241dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
3242b02efbfcSSagi Grimberg 
324324f412ddSSagi Grimberg 	isert_info("Destroying conn %p\n", isert_conn);
3244b02efbfcSSagi Grimberg 	isert_put_conn(isert_conn);
3245b02efbfcSSagi Grimberg }
3246b02efbfcSSagi Grimberg 
3247bdf20e72SSagi Grimberg static void
3248991bb764SSagi Grimberg isert_wait4logout(struct isert_conn *isert_conn)
3249991bb764SSagi Grimberg {
3250991bb764SSagi Grimberg 	struct iscsi_conn *conn = isert_conn->conn;
3251991bb764SSagi Grimberg 
32524c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
32534c22e07fSSagi Grimberg 
3254991bb764SSagi Grimberg 	if (isert_conn->logout_posted) {
325524f412ddSSagi Grimberg 		isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
3256991bb764SSagi Grimberg 		wait_for_completion_timeout(&conn->conn_logout_comp,
3257991bb764SSagi Grimberg 					    SECONDS_FOR_LOGOUT_COMP * HZ);
3258991bb764SSagi Grimberg 	}
3259991bb764SSagi Grimberg }
3260991bb764SSagi Grimberg 
3261991bb764SSagi Grimberg static void
3262c7e160eeSSagi Grimberg isert_wait4cmds(struct iscsi_conn *conn)
3263c7e160eeSSagi Grimberg {
32644c22e07fSSagi Grimberg 	isert_info("iscsi_conn %p\n", conn);
32654c22e07fSSagi Grimberg 
3266c7e160eeSSagi Grimberg 	if (conn->sess) {
3267c7e160eeSSagi Grimberg 		target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3268c7e160eeSSagi Grimberg 		target_wait_for_sess_cmds(conn->sess->se_sess);
3269c7e160eeSSagi Grimberg 	}
3270c7e160eeSSagi Grimberg }
3271c7e160eeSSagi Grimberg 
3272c7e160eeSSagi Grimberg static void
3273bdf20e72SSagi Grimberg isert_wait4flush(struct isert_conn *isert_conn)
3274bdf20e72SSagi Grimberg {
3275bdf20e72SSagi Grimberg 	struct ib_recv_wr *bad_wr;
3276bdf20e72SSagi Grimberg 
32774c22e07fSSagi Grimberg 	isert_info("conn %p\n", isert_conn);
32784c22e07fSSagi Grimberg 
3279dac6ab30SSagi Grimberg 	init_completion(&isert_conn->wait_comp_err);
3280bdf20e72SSagi Grimberg 	isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3281bdf20e72SSagi Grimberg 	/* post an indication that all flush errors were consumed */
3282dac6ab30SSagi Grimberg 	if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
328324f412ddSSagi Grimberg 		isert_err("conn %p failed to post beacon", isert_conn);
3284bdf20e72SSagi Grimberg 		return;
3285bdf20e72SSagi Grimberg 	}
3286bdf20e72SSagi Grimberg 
3287dac6ab30SSagi Grimberg 	wait_for_completion(&isert_conn->wait_comp_err);
3288bdf20e72SSagi Grimberg }
3289bdf20e72SSagi Grimberg 
32903e03c4b0SJenny Derzhavetz /**
32913e03c4b0SJenny Derzhavetz  * isert_put_unsol_pending_cmds() - Drop commands waiting for
32923e03c4b0SJenny Derzhavetz  *     unsolicitate dataout
32933e03c4b0SJenny Derzhavetz  * @conn:    iscsi connection
32943e03c4b0SJenny Derzhavetz  *
32953e03c4b0SJenny Derzhavetz  * We might still have commands that are waiting for unsolicited
32963e03c4b0SJenny Derzhavetz  * dataouts messages. We must put the extra reference on those
32973e03c4b0SJenny Derzhavetz  * before blocking on the target_wait_for_session_cmds
32983e03c4b0SJenny Derzhavetz  */
32993e03c4b0SJenny Derzhavetz static void
33003e03c4b0SJenny Derzhavetz isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
33013e03c4b0SJenny Derzhavetz {
33023e03c4b0SJenny Derzhavetz 	struct iscsi_cmd *cmd, *tmp;
33033e03c4b0SJenny Derzhavetz 	static LIST_HEAD(drop_cmd_list);
33043e03c4b0SJenny Derzhavetz 
33053e03c4b0SJenny Derzhavetz 	spin_lock_bh(&conn->cmd_lock);
33063e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
33073e03c4b0SJenny Derzhavetz 		if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
33083e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
33093e03c4b0SJenny Derzhavetz 		    (cmd->write_data_done < cmd->se_cmd.data_length))
33103e03c4b0SJenny Derzhavetz 			list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
33113e03c4b0SJenny Derzhavetz 	}
33123e03c4b0SJenny Derzhavetz 	spin_unlock_bh(&conn->cmd_lock);
33133e03c4b0SJenny Derzhavetz 
33143e03c4b0SJenny Derzhavetz 	list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
33153e03c4b0SJenny Derzhavetz 		list_del_init(&cmd->i_conn_node);
33163e03c4b0SJenny Derzhavetz 		if (cmd->i_state != ISTATE_REMOVE) {
33173e03c4b0SJenny Derzhavetz 			struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
33183e03c4b0SJenny Derzhavetz 
33193e03c4b0SJenny Derzhavetz 			isert_info("conn %p dropping cmd %p\n", conn, cmd);
33203e03c4b0SJenny Derzhavetz 			isert_put_cmd(isert_cmd, true);
33213e03c4b0SJenny Derzhavetz 		}
33223e03c4b0SJenny Derzhavetz 	}
33233e03c4b0SJenny Derzhavetz }
33243e03c4b0SJenny Derzhavetz 
3325defd8848SNicholas Bellinger static void isert_wait_conn(struct iscsi_conn *conn)
3326b8d26b3bSNicholas Bellinger {
3327b8d26b3bSNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3328b8d26b3bSNicholas Bellinger 
33294c22e07fSSagi Grimberg 	isert_info("Starting conn %p\n", isert_conn);
3330b8d26b3bSNicholas Bellinger 
3331dac6ab30SSagi Grimberg 	mutex_lock(&isert_conn->mutex);
3332b8d26b3bSNicholas Bellinger 	/*
3333dac6ab30SSagi Grimberg 	 * Only wait for wait_comp_err if the isert_conn made it
3334b8d26b3bSNicholas Bellinger 	 * into full feature phase..
3335b8d26b3bSNicholas Bellinger 	 */
3336b2cb9649SNicholas Bellinger 	if (isert_conn->state == ISER_CONN_INIT) {
3337dac6ab30SSagi Grimberg 		mutex_unlock(&isert_conn->mutex);
3338b2cb9649SNicholas Bellinger 		return;
3339b2cb9649SNicholas Bellinger 	}
3340954f2372SSagi Grimberg 	isert_conn_terminate(isert_conn);
3341dac6ab30SSagi Grimberg 	mutex_unlock(&isert_conn->mutex);
3342b2cb9649SNicholas Bellinger 
3343bdf20e72SSagi Grimberg 	isert_wait4flush(isert_conn);
33443e03c4b0SJenny Derzhavetz 	isert_put_unsol_pending_cmds(conn);
33453e03c4b0SJenny Derzhavetz 	isert_wait4cmds(conn);
3346991bb764SSagi Grimberg 	isert_wait4logout(isert_conn);
3347954f2372SSagi Grimberg 
3348b02efbfcSSagi Grimberg 	queue_work(isert_release_wq, &isert_conn->release_work);
3349defd8848SNicholas Bellinger }
3350defd8848SNicholas Bellinger 
3351defd8848SNicholas Bellinger static void isert_free_conn(struct iscsi_conn *conn)
3352defd8848SNicholas Bellinger {
3353defd8848SNicholas Bellinger 	struct isert_conn *isert_conn = conn->context;
3354b8d26b3bSNicholas Bellinger 
3355524630d5SSagi Grimberg 	isert_wait4flush(isert_conn);
3356b8d26b3bSNicholas Bellinger 	isert_put_conn(isert_conn);
3357b8d26b3bSNicholas Bellinger }
3358b8d26b3bSNicholas Bellinger 
3359b8d26b3bSNicholas Bellinger static struct iscsit_transport iser_target_transport = {
3360b8d26b3bSNicholas Bellinger 	.name			= "IB/iSER",
3361b8d26b3bSNicholas Bellinger 	.transport_type		= ISCSI_INFINIBAND,
3362d703ce2fSNicholas Bellinger 	.priv_size		= sizeof(struct isert_cmd),
3363b8d26b3bSNicholas Bellinger 	.owner			= THIS_MODULE,
3364b8d26b3bSNicholas Bellinger 	.iscsit_setup_np	= isert_setup_np,
3365b8d26b3bSNicholas Bellinger 	.iscsit_accept_np	= isert_accept_np,
3366b8d26b3bSNicholas Bellinger 	.iscsit_free_np		= isert_free_np,
3367defd8848SNicholas Bellinger 	.iscsit_wait_conn	= isert_wait_conn,
3368b8d26b3bSNicholas Bellinger 	.iscsit_free_conn	= isert_free_conn,
3369b8d26b3bSNicholas Bellinger 	.iscsit_get_login_rx	= isert_get_login_rx,
3370b8d26b3bSNicholas Bellinger 	.iscsit_put_login_tx	= isert_put_login_tx,
3371b8d26b3bSNicholas Bellinger 	.iscsit_immediate_queue	= isert_immediate_queue,
3372b8d26b3bSNicholas Bellinger 	.iscsit_response_queue	= isert_response_queue,
3373b8d26b3bSNicholas Bellinger 	.iscsit_get_dataout	= isert_get_dataout,
3374b8d26b3bSNicholas Bellinger 	.iscsit_queue_data_in	= isert_put_datain,
3375b8d26b3bSNicholas Bellinger 	.iscsit_queue_status	= isert_put_response,
3376131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= isert_aborted_task,
3377e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
3378b8d26b3bSNicholas Bellinger };
3379b8d26b3bSNicholas Bellinger 
3380b8d26b3bSNicholas Bellinger static int __init isert_init(void)
3381b8d26b3bSNicholas Bellinger {
3382b8d26b3bSNicholas Bellinger 	int ret;
3383b8d26b3bSNicholas Bellinger 
3384631af550SSagi Grimberg 	isert_comp_wq = alloc_workqueue("isert_comp_wq",
3385631af550SSagi Grimberg 					WQ_UNBOUND | WQ_HIGHPRI, 0);
3386b8d26b3bSNicholas Bellinger 	if (!isert_comp_wq) {
338724f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_comp_wq\n");
3388b8d26b3bSNicholas Bellinger 		ret = -ENOMEM;
33896f0fae3dSSagi Grimberg 		return -ENOMEM;
3390b8d26b3bSNicholas Bellinger 	}
3391b8d26b3bSNicholas Bellinger 
3392b02efbfcSSagi Grimberg 	isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3393b02efbfcSSagi Grimberg 					WQ_UNBOUND_MAX_ACTIVE);
3394b02efbfcSSagi Grimberg 	if (!isert_release_wq) {
339524f412ddSSagi Grimberg 		isert_err("Unable to allocate isert_release_wq\n");
3396b02efbfcSSagi Grimberg 		ret = -ENOMEM;
3397b02efbfcSSagi Grimberg 		goto destroy_comp_wq;
3398b02efbfcSSagi Grimberg 	}
3399b02efbfcSSagi Grimberg 
3400b8d26b3bSNicholas Bellinger 	iscsit_register_transport(&iser_target_transport);
340124f412ddSSagi Grimberg 	isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
3402b02efbfcSSagi Grimberg 
3403b8d26b3bSNicholas Bellinger 	return 0;
3404b8d26b3bSNicholas Bellinger 
3405b02efbfcSSagi Grimberg destroy_comp_wq:
3406b02efbfcSSagi Grimberg 	destroy_workqueue(isert_comp_wq);
34076f0fae3dSSagi Grimberg 
3408b8d26b3bSNicholas Bellinger 	return ret;
3409b8d26b3bSNicholas Bellinger }
3410b8d26b3bSNicholas Bellinger 
3411b8d26b3bSNicholas Bellinger static void __exit isert_exit(void)
3412b8d26b3bSNicholas Bellinger {
3413f5ebec96SSagi Grimberg 	flush_scheduled_work();
3414b02efbfcSSagi Grimberg 	destroy_workqueue(isert_release_wq);
3415b8d26b3bSNicholas Bellinger 	destroy_workqueue(isert_comp_wq);
3416b8d26b3bSNicholas Bellinger 	iscsit_unregister_transport(&iser_target_transport);
34174c22e07fSSagi Grimberg 	isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
3418b8d26b3bSNicholas Bellinger }
3419b8d26b3bSNicholas Bellinger 
3420b8d26b3bSNicholas Bellinger MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
34219e35eff4SSagi Grimberg MODULE_VERSION("1.0");
3422b8d26b3bSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3423b8d26b3bSNicholas Bellinger MODULE_LICENSE("GPL");
3424b8d26b3bSNicholas Bellinger 
3425b8d26b3bSNicholas Bellinger module_init(isert_init);
3426b8d26b3bSNicholas Bellinger module_exit(isert_exit);
3427