blob: 60b30d338a813447782449a54b6f7161874d4b22 [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2013 Datera, Inc.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08005 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19#include <linux/string.h>
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/socket.h>
23#include <linux/in.h>
24#include <linux/in6.h>
25#include <rdma/ib_verbs.h>
26#include <rdma/rdma_cm.h>
27#include <target/target_core_base.h>
28#include <target/target_core_fabric.h>
29#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg531b7bf2014-04-29 13:13:45 +030030#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080031
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080032#include "ib_isert.h"
33
34#define ISERT_MAX_CONN 8
35#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
36#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020037#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
38 ISERT_MAX_CONN)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080039
Sagi Grimberg45678b62015-01-25 19:11:19 +020040static int isert_debug_level;
Sagi Grimberg24f412d2014-12-07 13:12:02 +020041module_param_named(debug_level, isert_debug_level, int, 0644);
42MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
43
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080044static DEFINE_MUTEX(device_list_mutex);
45static LIST_HEAD(device_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080046static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +020047static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080048
49static void
Vu Phamd40945d2013-08-28 23:23:34 +030050isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
51static int
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +020052isert_map_rdma(struct isert_cmd *isert_cmd, struct iscsi_conn *conn);
Vu Pham59464ef2013-08-28 23:23:35 +030053static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020054isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030055static int
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +020056isert_reg_rdma(struct isert_cmd *isert_cmd, struct iscsi_conn *conn);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020057static int
58isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020059static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +020060isert_login_post_recv(struct isert_conn *isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020061static int
62isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020063struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030064
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030065static void isert_release_work(struct work_struct *work);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +020066static void isert_wait4flush(struct isert_conn *isert_conn);
Christoph Hellwig9679cc52016-02-24 19:24:06 +020067static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc);
68static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc);
69static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc);
70static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030071
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020072static inline bool
73isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
74{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020075 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020076 cmd->prot_op != TARGET_PROT_NORMAL);
77}
78
79
Vu Phamd40945d2013-08-28 23:23:34 +030080static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080081isert_qp_event_callback(struct ib_event *e, void *context)
82{
Sagi Grimberg67004252015-03-29 15:52:09 +030083 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080084
Sagi Grimbergea8a1612015-05-18 13:40:31 +030085 isert_err("%s (%d): conn %p\n",
86 ib_event_msg(e->event), e->event, isert_conn);
87
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080088 switch (e->event) {
89 case IB_EVENT_COMM_EST:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030090 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080091 break;
92 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020093 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080094 break;
95 default:
96 break;
97 }
98}
99
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300100static struct isert_comp *
101isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800102{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300103 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200104 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300105 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800106
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800107 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200108 for (i = 0; i < device->comps_used; i++)
109 if (device->comps[i].active_qps <
110 device->comps[min].active_qps)
111 min = i;
112 comp = &device->comps[min];
113 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300114 mutex_unlock(&device_list_mutex);
115
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200116 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200117 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300118
119 return comp;
120}
121
122static void
123isert_comp_put(struct isert_comp *comp)
124{
125 mutex_lock(&device_list_mutex);
126 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800127 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300128}
129
130static struct ib_qp *
131isert_create_qp(struct isert_conn *isert_conn,
132 struct isert_comp *comp,
133 struct rdma_cm_id *cma_id)
134{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300135 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300136 struct ib_qp_init_attr attr;
137 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800138
139 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
140 attr.event_handler = isert_qp_event_callback;
141 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200142 attr.send_cq = comp->cq;
143 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800144 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200145 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Or Gerlitz4a061b22015-12-18 10:59:46 +0200146 attr.cap.max_send_sge = device->ib_device->attrs.max_sge;
147 isert_conn->max_sge = min(device->ib_device->attrs.max_sge,
148 device->ib_device->attrs.max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800149 attr.cap.max_recv_sge = 1;
150 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
151 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200152 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200153 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800154
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300155 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800156 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200157 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300158 return ERR_PTR(ret);
159 }
160
161 return cma_id->qp;
162}
163
164static int
165isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
166{
167 struct isert_comp *comp;
168 int ret;
169
170 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300171 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
172 if (IS_ERR(isert_conn->qp)) {
173 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200174 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800175 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800176
177 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200178err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300179 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200180 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800181}
182
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800183static int
184isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
185{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300186 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300187 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800188 struct iser_rx_desc *rx_desc;
189 struct ib_sge *rx_sg;
190 u64 dma_addr;
191 int i, j;
192
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300193 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800194 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300195 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800196 goto fail;
197
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300198 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800199
200 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
201 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
202 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
203 if (ib_dma_mapping_error(ib_dev, dma_addr))
204 goto dma_map_fail;
205
206 rx_desc->dma_addr = dma_addr;
207
208 rx_sg = &rx_desc->rx_sg;
209 rx_sg->addr = rx_desc->dma_addr;
210 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600211 rx_sg->lkey = device->pd->local_dma_lkey;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200212 rx_desc->rx_cqe.done = isert_recv_done;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800213 }
214
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800215 return 0;
216
217dma_map_fail:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300218 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800219 for (j = 0; j < i; j++, rx_desc++) {
220 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
221 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
222 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300223 kfree(isert_conn->rx_descs);
224 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800225fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200226 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
227
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800228 return -ENOMEM;
229}
230
231static void
232isert_free_rx_descriptors(struct isert_conn *isert_conn)
233{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300234 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800235 struct iser_rx_desc *rx_desc;
236 int i;
237
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300238 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800239 return;
240
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300241 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800242 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
243 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
244 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
245 }
246
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300247 kfree(isert_conn->rx_descs);
248 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800249}
250
Sagi Grimberg172369c2015-03-29 15:52:11 +0300251static void
252isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800253{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300254 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800255
Sagi Grimberg172369c2015-03-29 15:52:11 +0300256 for (i = 0; i < device->comps_used; i++) {
257 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300258
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200259 if (comp->cq)
260 ib_free_cq(comp->cq);
Vu Pham59464ef2013-08-28 23:23:35 +0300261 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300262 kfree(device->comps);
263}
Vu Phamd40945d2013-08-28 23:23:34 +0300264
Sagi Grimberg172369c2015-03-29 15:52:11 +0300265static int
Or Gerlitz4a061b22015-12-18 10:59:46 +0200266isert_alloc_comps(struct isert_device *device)
Sagi Grimberg172369c2015-03-29 15:52:11 +0300267{
268 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200269
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200270 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300271 device->ib_device->num_comp_vectors));
272
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200273 isert_info("Using %d CQs, %s supports %d vectors support "
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200274 "Fast registration %d pi_capable %d\n",
275 device->comps_used, device->ib_device->name,
276 device->ib_device->num_comp_vectors, device->use_fastreg,
277 device->pi_capable);
278
279 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
280 GFP_KERNEL);
281 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200282 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800283 return -ENOMEM;
284 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800285
Or Gerlitz4a061b22015-12-18 10:59:46 +0200286 max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300287
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200288 for (i = 0; i < device->comps_used; i++) {
289 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800290
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200291 comp->device = device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200292 comp->cq = ib_alloc_cq(device->ib_device, comp, max_cqe, i,
293 IB_POLL_WORKQUEUE);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200294 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300295 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200296 ret = PTR_ERR(comp->cq);
297 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800298 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800299 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800300 }
301
Sagi Grimberg172369c2015-03-29 15:52:11 +0300302 return 0;
303out_cq:
304 isert_free_comps(device);
305 return ret;
306}
307
308static int
309isert_create_device_ib_res(struct isert_device *device)
310{
Or Gerlitz4a061b22015-12-18 10:59:46 +0200311 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300312 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300313
Or Gerlitz4a061b22015-12-18 10:59:46 +0200314 isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
315 isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300316
317 /* asign function handlers */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200318 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
319 ib_dev->attrs.device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300320 device->use_fastreg = 1;
321 device->reg_rdma_mem = isert_reg_rdma;
322 device->unreg_rdma_mem = isert_unreg_rdma;
323 } else {
324 device->use_fastreg = 0;
325 device->reg_rdma_mem = isert_map_rdma;
326 device->unreg_rdma_mem = isert_unmap_cmd;
327 }
328
Or Gerlitz4a061b22015-12-18 10:59:46 +0200329 ret = isert_alloc_comps(device);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300330 if (ret)
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800331 goto out;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300332
Or Gerlitz4a061b22015-12-18 10:59:46 +0200333 device->pd = ib_alloc_pd(ib_dev);
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300334 if (IS_ERR(device->pd)) {
335 ret = PTR_ERR(device->pd);
336 isert_err("failed to allocate pd, device %p, ret=%d\n",
337 device, ret);
338 goto out_cq;
339 }
340
Sagi Grimberg172369c2015-03-29 15:52:11 +0300341 /* Check signature cap */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200342 device->pi_capable = ib_dev->attrs.device_cap_flags &
Sagi Grimberg172369c2015-03-29 15:52:11 +0300343 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300344
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800345 return 0;
346
347out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300348 isert_free_comps(device);
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800349out:
350 if (ret > 0)
351 ret = -EINVAL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800352 return ret;
353}
354
355static void
356isert_free_device_ib_res(struct isert_device *device)
357{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200358 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800359
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300360 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300361 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800362}
363
364static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300365isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800366{
367 mutex_lock(&device_list_mutex);
368 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200369 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800370 if (!device->refcount) {
371 isert_free_device_ib_res(device);
372 list_del(&device->dev_node);
373 kfree(device);
374 }
375 mutex_unlock(&device_list_mutex);
376}
377
378static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300379isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800380{
381 struct isert_device *device;
382 int ret;
383
384 mutex_lock(&device_list_mutex);
385 list_for_each_entry(device, &device_list, dev_node) {
386 if (device->ib_device->node_guid == cma_id->device->node_guid) {
387 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200388 isert_info("Found iser device %p refcount %d\n",
389 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800390 mutex_unlock(&device_list_mutex);
391 return device;
392 }
393 }
394
395 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
396 if (!device) {
397 mutex_unlock(&device_list_mutex);
398 return ERR_PTR(-ENOMEM);
399 }
400
401 INIT_LIST_HEAD(&device->dev_node);
402
403 device->ib_device = cma_id->device;
404 ret = isert_create_device_ib_res(device);
405 if (ret) {
406 kfree(device);
407 mutex_unlock(&device_list_mutex);
408 return ERR_PTR(ret);
409 }
410
411 device->refcount++;
412 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200413 isert_info("Created a new iser device %p refcount %d\n",
414 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800415 mutex_unlock(&device_list_mutex);
416
417 return device;
418}
419
Vu Pham59464ef2013-08-28 23:23:35 +0300420static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200421isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300422{
423 struct fast_reg_descriptor *fr_desc, *tmp;
424 int i = 0;
425
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300426 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300427 return;
428
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200429 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300430
431 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300432 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300433 list_del(&fr_desc->list);
Vu Pham59464ef2013-08-28 23:23:35 +0300434 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200435 if (fr_desc->pi_ctx) {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200436 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300437 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200438 kfree(fr_desc->pi_ctx);
439 }
Vu Pham59464ef2013-08-28 23:23:35 +0300440 kfree(fr_desc);
441 ++i;
442 }
443
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300444 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200445 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300446 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300447}
448
449static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200450isert_create_pi_ctx(struct fast_reg_descriptor *desc,
451 struct ib_device *device,
452 struct ib_pd *pd)
453{
Sagi Grimberg570db172014-12-02 16:57:31 +0200454 struct pi_context *pi_ctx;
455 int ret;
456
457 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
458 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200459 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200460 return -ENOMEM;
461 }
462
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300463 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
464 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimberg570db172014-12-02 16:57:31 +0200465 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200466 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200467 PTR_ERR(pi_ctx->prot_mr));
468 ret = PTR_ERR(pi_ctx->prot_mr);
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300469 goto err_pi_ctx;
Sagi Grimberg570db172014-12-02 16:57:31 +0200470 }
471 desc->ind |= ISERT_PROT_KEY_VALID;
472
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300473 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg570db172014-12-02 16:57:31 +0200474 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200475 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200476 PTR_ERR(pi_ctx->sig_mr));
477 ret = PTR_ERR(pi_ctx->sig_mr);
478 goto err_prot_mr;
479 }
480
481 desc->pi_ctx = pi_ctx;
482 desc->ind |= ISERT_SIG_KEY_VALID;
483 desc->ind &= ~ISERT_PROTECTED;
484
485 return 0;
486
487err_prot_mr:
Roland Dreierb2feda42015-05-29 23:12:10 -0700488 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg570db172014-12-02 16:57:31 +0200489err_pi_ctx:
Roland Dreierb2feda42015-05-29 23:12:10 -0700490 kfree(pi_ctx);
Sagi Grimberg570db172014-12-02 16:57:31 +0200491
492 return ret;
493}
494
495static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200496isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200497 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200498{
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300499 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
500 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200501 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200502 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200503 PTR_ERR(fr_desc->data_mr));
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300504 return PTR_ERR(fr_desc->data_mr);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200505 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200506 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200507
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200508 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200509
510 return 0;
511}
512
513static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200514isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300515{
516 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300517 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700518 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
519 struct se_node_acl *se_nacl = se_sess->se_node_acl;
520 int i, ret, tag_num;
521 /*
522 * Setup the number of FRMRs based upon the number of tags
523 * available to session in iscsi_target_locate_portal().
524 */
525 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
526 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300527
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300528 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700529 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300530 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
531 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200532 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300533 ret = -ENOMEM;
534 goto err;
535 }
536
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200537 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300538 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200539 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200540 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200541 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800542 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300543 goto err;
544 }
545
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300546 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
547 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300548 }
549
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200550 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300551 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300552
553 return 0;
554
555err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200556 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300557 return ret;
558}
559
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300560static void
561isert_init_conn(struct isert_conn *isert_conn)
562{
563 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300564 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300565 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300566 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300567 kref_init(&isert_conn->kref);
568 mutex_init(&isert_conn->mutex);
569 spin_lock_init(&isert_conn->pool_lock);
570 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300571 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300572}
573
574static void
575isert_free_login_buf(struct isert_conn *isert_conn)
576{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300577 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300578
579 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200580 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200581 kfree(isert_conn->login_rsp_buf);
582
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300583 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200584 ISER_RX_PAYLOAD_SIZE,
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300585 DMA_FROM_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200586 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300587}
588
589static int
590isert_alloc_login_buf(struct isert_conn *isert_conn,
591 struct ib_device *ib_dev)
592{
593 int ret;
594
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200595 isert_conn->login_req_buf = kzalloc(sizeof(*isert_conn->login_req_buf),
596 GFP_KERNEL);
597 if (!isert_conn->login_req_buf) {
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300598 isert_err("Unable to allocate isert_conn->login_buf\n");
599 return -ENOMEM;
600 }
601
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300602 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200603 isert_conn->login_req_buf,
604 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300605 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
606 if (ret) {
607 isert_err("login_req_dma mapping error: %d\n", ret);
608 isert_conn->login_req_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200609 goto out_free_login_req_buf;
610 }
611
612 isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
613 if (!isert_conn->login_rsp_buf) {
614 isert_err("Unable to allocate isert_conn->login_rspbuf\n");
615 goto out_unmap_login_req_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300616 }
617
618 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200619 isert_conn->login_rsp_buf,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200620 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300621 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
622 if (ret) {
623 isert_err("login_rsp_dma mapping error: %d\n", ret);
624 isert_conn->login_rsp_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200625 goto out_free_login_rsp_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300626 }
627
628 return 0;
629
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200630out_free_login_rsp_buf:
631 kfree(isert_conn->login_rsp_buf);
632out_unmap_login_req_buf:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300633 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200634 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
635out_free_login_req_buf:
636 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300637 return ret;
638}
639
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200640static void
641isert_set_nego_params(struct isert_conn *isert_conn,
642 struct rdma_conn_param *param)
643{
644 struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
645
646 /* Set max inflight RDMA READ requests */
647 isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
648 attr->max_qp_init_rd_atom);
649 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
650
651 if (param->private_data) {
652 u8 flags = *(u8 *)param->private_data;
653
654 /*
655 * use remote invalidation if the both initiator
656 * and the HCA support it
657 */
658 isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
659 (attr->device_cap_flags &
660 IB_DEVICE_MEM_MGT_EXTENSIONS);
661 if (isert_conn->snd_w_inv)
662 isert_info("Using remote invalidation\n");
663 }
664}
665
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800666static int
667isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
668{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200669 struct isert_np *isert_np = cma_id->context;
670 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800671 struct isert_conn *isert_conn;
672 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800673 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300674
675 spin_lock_bh(&np->np_thread_lock);
676 if (!np->enabled) {
677 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200678 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300679 return rdma_reject(cma_id, NULL, 0);
680 }
681 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800682
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200683 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800684 cma_id, cma_id->context);
685
686 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300687 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800688 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800689
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300690 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300691 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800692
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300693 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
694 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800695 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800696
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300697 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800698 if (IS_ERR(device)) {
699 ret = PTR_ERR(device);
700 goto out_rsp_dma_map;
701 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300702 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800703
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200704 isert_set_nego_params(isert_conn, &event->param.conn);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300705
Sagi Grimberg570db172014-12-02 16:57:31 +0200706 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800707 if (ret)
708 goto out_conn_dev;
709
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200710 ret = isert_login_post_recv(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200711 if (ret)
712 goto out_conn_dev;
713
714 ret = isert_rdma_accept(isert_conn);
715 if (ret)
716 goto out_conn_dev;
717
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300718 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300719 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300720 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800721
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800722 return 0;
723
724out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300725 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800726out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300727 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800728out:
729 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200730 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800731 return ret;
732}
733
734static void
735isert_connect_release(struct isert_conn *isert_conn)
736{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300737 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800738
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200739 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800740
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300741 BUG_ON(!device);
742
743 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200744 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300745
Sagi Grimberg19e20902014-12-02 16:57:26 +0200746 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300747 if (isert_conn->cm_id)
748 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200749
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300750 if (isert_conn->qp) {
751 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200752
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300753 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300754 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800755 }
756
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200757 if (isert_conn->login_req_buf)
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300758 isert_free_login_buf(isert_conn);
759
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300760 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800761
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300762 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800763}
764
765static void
766isert_connected_handler(struct rdma_cm_id *cma_id)
767{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200768 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300769 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300770
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200771 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200772
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300773 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300774 isert_conn->state = ISER_CONN_UP;
775 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300776 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300777
778 mutex_lock(&isert_np->mutex);
779 list_move_tail(&isert_conn->node, &isert_np->pending);
780 mutex_unlock(&isert_np->mutex);
781
782 isert_info("np %p: Allow accept_np to continue\n", isert_np);
783 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800784}
785
786static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300787isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800788{
789 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300790 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800791
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200792 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
793 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800794
795 isert_connect_release(isert_conn);
796}
797
798static void
799isert_put_conn(struct isert_conn *isert_conn)
800{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300801 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800802}
803
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200804static void
805isert_handle_unbound_conn(struct isert_conn *isert_conn)
806{
807 struct isert_np *isert_np = isert_conn->cm_id->context;
808
809 mutex_lock(&isert_np->mutex);
810 if (!list_empty(&isert_conn->node)) {
811 /*
812 * This means iscsi doesn't know this connection
813 * so schedule a cleanup ourselves
814 */
815 list_del_init(&isert_conn->node);
816 isert_put_conn(isert_conn);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200817 queue_work(isert_release_wq, &isert_conn->release_work);
818 }
819 mutex_unlock(&isert_np->mutex);
820}
821
Sagi Grimberg954f2372014-12-02 16:57:17 +0200822/**
823 * isert_conn_terminate() - Initiate connection termination
824 * @isert_conn: isert connection struct
825 *
826 * Notes:
Jenny Derzhavetzaea92982016-02-24 19:23:59 +0200827 * In case the connection state is BOUND, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200828 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200829 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200830 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300831 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200832 * safe to call multiple times.
833 */
834static void
835isert_conn_terminate(struct isert_conn *isert_conn)
836{
837 int err;
838
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200839 if (isert_conn->state >= ISER_CONN_TERMINATING)
840 return;
841
842 isert_info("Terminating conn %p state %d\n",
843 isert_conn, isert_conn->state);
844 isert_conn->state = ISER_CONN_TERMINATING;
845 err = rdma_disconnect(isert_conn->cm_id);
846 if (err)
847 isert_warn("Failed rdma_disconnect isert_conn %p\n",
848 isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200849}
850
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700851static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200852isert_np_cma_handler(struct isert_np *isert_np,
853 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800854{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300855 isert_dbg("%s (%d): isert np %p\n",
856 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200857
858 switch (event) {
859 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300860 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200861 break;
862 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300863 isert_np->cm_id = isert_setup_id(isert_np);
864 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200865 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300866 isert_np, PTR_ERR(isert_np->cm_id));
867 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200868 }
869 break;
870 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200871 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200872 isert_np, event);
873 }
874
875 return -1;
876}
877
878static int
879isert_disconnected_handler(struct rdma_cm_id *cma_id,
880 enum rdma_cm_event_type event)
881{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200882 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700883
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300884 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200885 switch (isert_conn->state) {
886 case ISER_CONN_TERMINATING:
887 break;
888 case ISER_CONN_UP:
889 isert_conn_terminate(isert_conn);
890 isert_wait4flush(isert_conn);
891 isert_handle_unbound_conn(isert_conn);
892 break;
893 case ISER_CONN_BOUND:
894 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
895 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
896 break;
897 default:
898 isert_warn("conn %p teminating in state %d\n",
899 isert_conn, isert_conn->state);
900 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300901 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200902
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700903 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800904}
905
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300906static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200907isert_connect_error(struct rdma_cm_id *cma_id)
908{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200909 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200910
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300911 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300912 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200913 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300914
915 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200916}
917
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800918static int
919isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
920{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200921 struct isert_np *isert_np = cma_id->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800922 int ret = 0;
923
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300924 isert_info("%s (%d): status %d id %p np %p\n",
925 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200926 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800927
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200928 if (isert_np->cm_id == cma_id)
929 return isert_np_cma_handler(cma_id->context, event->event);
930
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800931 switch (event->event) {
932 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800933 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700934 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200935 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800936 break;
937 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800938 isert_connected_handler(cma_id);
939 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300940 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
941 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
942 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300943 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200944 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800945 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200946 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
947 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800948 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300949 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200950 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800951 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200952 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800953 break;
954 }
955
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800956 return ret;
957}
958
959static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300960isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800961{
962 struct ib_recv_wr *rx_wr, *rx_wr_failed;
963 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800964 struct iser_rx_desc *rx_desc;
965
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300966 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300967 rx_desc = &isert_conn->rx_descs[i];
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200968
969 rx_wr->wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300970 rx_wr->sg_list = &rx_desc->rx_sg;
971 rx_wr->num_sge = 1;
972 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800973 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800974 rx_wr--;
975 rx_wr->next = NULL; /* mark end of work requests list */
976
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300977 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300978 &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200979 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200980 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300981
982 return ret;
983}
984
985static int
986isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
987{
988 struct ib_recv_wr *rx_wr_failed, rx_wr;
989 int ret;
990
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200991 rx_wr.wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300992 rx_wr.sg_list = &rx_desc->rx_sg;
993 rx_wr.num_sge = 1;
994 rx_wr.next = NULL;
995
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300996 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200997 if (ret)
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300998 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300999
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001000 return ret;
1001}
1002
1003static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001004isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001005{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001006 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001007 struct ib_send_wr send_wr, *send_wr_failed;
1008 int ret;
1009
1010 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1011 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1012
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001013 tx_desc->tx_cqe.done = isert_login_send_done;
1014
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001015 send_wr.next = NULL;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001016 send_wr.wr_cqe = &tx_desc->tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001017 send_wr.sg_list = tx_desc->tx_sg;
1018 send_wr.num_sge = tx_desc->num_sge;
1019 send_wr.opcode = IB_WR_SEND;
1020 send_wr.send_flags = IB_SEND_SIGNALED;
1021
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001022 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001023 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001024 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001025
1026 return ret;
1027}
1028
1029static void
1030isert_create_send_desc(struct isert_conn *isert_conn,
1031 struct isert_cmd *isert_cmd,
1032 struct iser_tx_desc *tx_desc)
1033{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001034 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001035 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001036
1037 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1038 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1039
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001040 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
1041 tx_desc->iser_header.flags = ISCSI_CTRL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001042
1043 tx_desc->num_sge = 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001044
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001045 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1046 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001047 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001048 }
1049}
1050
1051static int
1052isert_init_tx_hdrs(struct isert_conn *isert_conn,
1053 struct iser_tx_desc *tx_desc)
1054{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001055 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001056 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001057 u64 dma_addr;
1058
1059 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1060 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1061 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001062 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001063 return -ENOMEM;
1064 }
1065
1066 tx_desc->dma_addr = dma_addr;
1067 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1068 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001069 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001070
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001071 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1072 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1073 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001074
1075 return 0;
1076}
1077
1078static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001079isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001080 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001081{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001082 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1083
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001084 isert_cmd->iser_ib_op = ISER_IB_SEND;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001085 tx_desc->tx_cqe.done = isert_send_done;
1086 send_wr->wr_cqe = &tx_desc->tx_cqe;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001087
1088 if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
1089 send_wr->opcode = IB_WR_SEND_WITH_INV;
1090 send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
1091 } else {
1092 send_wr->opcode = IB_WR_SEND;
1093 }
1094
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001095 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001096 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001097 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001098}
1099
1100static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001101isert_login_post_recv(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001102{
1103 struct ib_recv_wr rx_wr, *rx_wr_fail;
1104 struct ib_sge sge;
1105 int ret;
1106
1107 memset(&sge, 0, sizeof(struct ib_sge));
1108 sge.addr = isert_conn->login_req_dma;
Christoph Hellwiged1083b2016-02-24 19:24:04 +02001109 sge.length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001110 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001111
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001112 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001113 sge.addr, sge.length, sge.lkey);
1114
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001115 isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
1116
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001117 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001118 rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001119 rx_wr.sg_list = &sge;
1120 rx_wr.num_sge = 1;
1121
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001122 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +02001123 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001124 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001125
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001126 return ret;
1127}
1128
1129static int
1130isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1131 u32 length)
1132{
1133 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001134 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001135 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001136 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001137 int ret;
1138
1139 isert_create_send_desc(isert_conn, NULL, tx_desc);
1140
1141 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1142 sizeof(struct iscsi_hdr));
1143
1144 isert_init_tx_hdrs(isert_conn, tx_desc);
1145
1146 if (length > 0) {
1147 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1148
1149 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1150 length, DMA_TO_DEVICE);
1151
1152 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1153
1154 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1155 length, DMA_TO_DEVICE);
1156
1157 tx_dsg->addr = isert_conn->login_rsp_dma;
1158 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001159 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001160 tx_desc->num_sge = 2;
1161 }
1162 if (!login->login_failed) {
1163 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001164 if (!conn->sess->sess_ops->SessionType &&
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001165 isert_conn->device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001166 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001167 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001168 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001169 " fastreg pool\n", isert_conn);
1170 return ret;
1171 }
1172 }
1173
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001174 ret = isert_alloc_rx_descriptors(isert_conn);
1175 if (ret)
1176 return ret;
1177
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001178 ret = isert_post_recvm(isert_conn,
1179 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001180 if (ret)
1181 return ret;
1182
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001183 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001184 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001185 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001186 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001187 goto post_send;
1188 }
1189
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001190 ret = isert_login_post_recv(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001191 if (ret)
1192 return ret;
1193 }
1194post_send:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001195 ret = isert_login_post_send(isert_conn, tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001196 if (ret)
1197 return ret;
1198
1199 return 0;
1200}
1201
1202static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001203isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001204{
Christoph Hellwig5adabdd2016-02-24 19:24:05 +02001205 struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001206 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001207 struct iscsi_conn *conn = isert_conn->conn;
1208 struct iscsi_login *login = conn->conn_login;
1209 int size;
1210
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001211 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001212
1213 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001214
1215 if (login->first_request) {
1216 struct iscsi_login_req *login_req =
1217 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1218 /*
1219 * Setup the initial iscsi_login values from the leading
1220 * login request PDU.
1221 */
1222 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1223 login->current_stage =
1224 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1225 >> 2;
1226 login->version_min = login_req->min_version;
1227 login->version_max = login_req->max_version;
1228 memcpy(login->isid, login_req->isid, 6);
1229 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1230 login->init_task_tag = login_req->itt;
1231 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1232 login->cid = be16_to_cpu(login_req->cid);
1233 login->tsih = be16_to_cpu(login_req->tsih);
1234 }
1235
1236 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1237
1238 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001239 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1240 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1241 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001242 memcpy(login->req_buf, &rx_desc->data[0], size);
1243
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001244 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001245 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001246 return;
1247 }
1248 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001249}
1250
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001251static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001252*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001253{
Sagi Grimberg67004252015-03-29 15:52:09 +03001254 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001255 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001256 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001257
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001258 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001259 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001260 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001261 return NULL;
1262 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001263 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001264 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001265 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001266 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001267
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001268 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001269}
1270
1271static int
1272isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001273 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1274 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001275{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001276 struct iscsi_conn *conn = isert_conn->conn;
1277 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001278 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1279 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001280 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001281
1282 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1283 if (rc < 0)
1284 return rc;
1285
1286 imm_data = cmd->immediate_data;
1287 imm_data_len = cmd->first_burst_len;
1288 unsol_data = cmd->unsolicited_data;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001289 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001290
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001291 if (imm_data && imm_data_len == data_len)
1292 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001293 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1294 if (rc < 0) {
1295 return 0;
1296 } else if (rc > 0) {
1297 dump_payload = true;
1298 goto sequence_cmd;
1299 }
1300
1301 if (!imm_data)
1302 return 0;
1303
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001304 if (imm_data_len != data_len) {
1305 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1306 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1307 &rx_desc->data[0], imm_data_len);
1308 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1309 sg_nents, imm_data_len);
1310 } else {
1311 sg_init_table(&isert_cmd->sg, 1);
1312 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1313 cmd->se_cmd.t_data_nents = 1;
1314 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1315 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1316 imm_data_len);
1317 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001318
1319 cmd->write_data_done += imm_data_len;
1320
1321 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1322 spin_lock_bh(&cmd->istate_lock);
1323 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1324 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1325 spin_unlock_bh(&cmd->istate_lock);
1326 }
1327
1328sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001329 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001330
1331 if (!rc && dump_payload == false && unsol_data)
1332 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001333 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001334 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001335
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001336 return 0;
1337}
1338
1339static int
1340isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1341 struct iser_rx_desc *rx_desc, unsigned char *buf)
1342{
1343 struct scatterlist *sg_start;
1344 struct iscsi_conn *conn = isert_conn->conn;
1345 struct iscsi_cmd *cmd = NULL;
1346 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1347 u32 unsol_data_len = ntoh24(hdr->dlength);
1348 int rc, sg_nents, sg_off, page_off;
1349
1350 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1351 if (rc < 0)
1352 return rc;
1353 else if (!cmd)
1354 return 0;
1355 /*
1356 * FIXME: Unexpected unsolicited_data out
1357 */
1358 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001359 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001360 dump_stack();
1361 return -1;
1362 }
1363
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001364 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1365 "write_data_done: %u, data_length: %u\n",
1366 unsol_data_len, cmd->write_data_done,
1367 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001368
1369 sg_off = cmd->write_data_done / PAGE_SIZE;
1370 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1371 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1372 page_off = cmd->write_data_done % PAGE_SIZE;
1373 /*
1374 * FIXME: Non page-aligned unsolicited_data out
1375 */
1376 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001377 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001378 dump_stack();
1379 return -1;
1380 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001381 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1382 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1383 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001384
1385 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1386 unsol_data_len);
1387
1388 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1389 if (rc < 0)
1390 return rc;
1391
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001392 /*
1393 * multiple data-outs on the same command can arrive -
1394 * so post the buffer before hand
1395 */
1396 rc = isert_post_recv(isert_conn, rx_desc);
1397 if (rc) {
1398 isert_err("ib_post_recv failed with %d\n", rc);
1399 return rc;
1400 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001401 return 0;
1402}
1403
1404static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001405isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001406 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1407 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001408{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001409 struct iscsi_conn *conn = isert_conn->conn;
1410 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1411 int rc;
1412
1413 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1414 if (rc < 0)
1415 return rc;
1416 /*
1417 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1418 */
1419
1420 return iscsit_process_nop_out(conn, cmd, hdr);
1421}
1422
1423static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001424isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001425 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1426 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001427{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001428 struct iscsi_conn *conn = isert_conn->conn;
1429 u32 payload_length = ntoh24(hdr->dlength);
1430 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001431 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001432
1433 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1434 if (rc < 0)
1435 return rc;
1436
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001437 if (payload_length) {
1438 text_in = kzalloc(payload_length, GFP_KERNEL);
1439 if (!text_in) {
1440 isert_err("Unable to allocate text_in of payload_length: %u\n",
1441 payload_length);
1442 return -ENOMEM;
1443 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001444 }
1445 cmd->text_in_ptr = text_in;
1446
1447 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1448
1449 return iscsit_process_text_cmd(conn, cmd, hdr);
1450}
1451
1452static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001453isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1454 uint32_t read_stag, uint64_t read_va,
1455 uint32_t write_stag, uint64_t write_va)
1456{
1457 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1458 struct iscsi_conn *conn = isert_conn->conn;
1459 struct iscsi_cmd *cmd;
1460 struct isert_cmd *isert_cmd;
1461 int ret = -EINVAL;
1462 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1463
Sagi Grimbergfb140272015-03-29 15:52:08 +03001464 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001465 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001466 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001467 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001468 return 0;
1469 }
1470
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001471 switch (opcode) {
1472 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001473 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001474 if (!cmd)
1475 break;
1476
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001477 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001478 isert_cmd->read_stag = read_stag;
1479 isert_cmd->read_va = read_va;
1480 isert_cmd->write_stag = write_stag;
1481 isert_cmd->write_va = write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001482 isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001483
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001484 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001485 rx_desc, (unsigned char *)hdr);
1486 break;
1487 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001488 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001489 if (!cmd)
1490 break;
1491
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001492 isert_cmd = iscsit_priv_cmd(cmd);
1493 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001494 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001495 break;
1496 case ISCSI_OP_SCSI_DATA_OUT:
1497 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1498 (unsigned char *)hdr);
1499 break;
1500 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001501 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001502 if (!cmd)
1503 break;
1504
1505 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1506 (unsigned char *)hdr);
1507 break;
1508 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001509 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001510 if (!cmd)
1511 break;
1512
1513 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001514 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001515 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001516 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001517 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001518 else
1519 cmd = isert_allocate_cmd(conn, rx_desc);
1520
1521 if (!cmd)
1522 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001523
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001524 isert_cmd = iscsit_priv_cmd(cmd);
1525 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001526 rx_desc, (struct iscsi_text *)hdr);
1527 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001528 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001529 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001530 dump_stack();
1531 break;
1532 }
1533
1534 return ret;
1535}
1536
1537static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001538isert_print_wc(struct ib_wc *wc, const char *type)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001539{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001540 if (wc->status != IB_WC_WR_FLUSH_ERR)
1541 isert_err("%s failure: %s (%d) vend_err %x\n", type,
1542 ib_wc_status_msg(wc->status), wc->status,
1543 wc->vendor_err);
1544 else
1545 isert_dbg("%s failure: %s (%d)\n", type,
1546 ib_wc_status_msg(wc->status), wc->status);
1547}
1548
1549static void
1550isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1551{
1552 struct isert_conn *isert_conn = wc->qp->qp_context;
1553 struct ib_device *ib_dev = isert_conn->cm_id->device;
1554 struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
1555 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001556 struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001557 uint64_t read_va = 0, write_va = 0;
1558 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001559
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001560 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1561 isert_print_wc(wc, "recv");
1562 if (wc->status != IB_WC_WR_FLUSH_ERR)
1563 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1564 return;
1565 }
1566
1567 ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
1568 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1569
1570 isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1571 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
1572 (int)(wc->byte_len - ISER_HEADERS_LEN));
1573
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001574 switch (iser_ctrl->flags & 0xF0) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001575 case ISCSI_CTRL:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001576 if (iser_ctrl->flags & ISER_RSV) {
1577 read_stag = be32_to_cpu(iser_ctrl->read_stag);
1578 read_va = be64_to_cpu(iser_ctrl->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001579 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1580 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001581 }
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001582 if (iser_ctrl->flags & ISER_WSV) {
1583 write_stag = be32_to_cpu(iser_ctrl->write_stag);
1584 write_va = be64_to_cpu(iser_ctrl->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001585 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1586 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001587 }
1588
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001589 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001590 break;
1591 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001592 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001593 break;
1594 default:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001595 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001596 break;
1597 }
1598
Bart Van Assche6c760b32015-10-22 11:14:18 -07001599 isert_rx_opcode(isert_conn, rx_desc,
1600 read_stag, read_va, write_stag, write_va);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001601
1602 ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
1603 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001604}
1605
1606static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001607isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001608{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001609 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001610 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001611
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001612 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1613 isert_print_wc(wc, "login recv");
1614 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001615 }
1616
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001617 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
1618 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001619
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001620 isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001621
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001622 if (isert_conn->conn) {
1623 struct iscsi_login *login = isert_conn->conn->conn_login;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001624
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001625 if (login && !login->first_request)
1626 isert_rx_login_req(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001627 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001628
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001629 mutex_lock(&isert_conn->mutex);
1630 complete(&isert_conn->login_req_comp);
1631 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001632
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001633 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
1634 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001635}
1636
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001637static int
1638isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1639 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1640 enum iser_ib_op_code op, struct isert_data_buf *data)
1641{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001642 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001643
1644 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1645 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1646
1647 data->len = length - offset;
1648 data->offset = offset;
1649 data->sg_off = data->offset / PAGE_SIZE;
1650
1651 data->sg = &sg[data->sg_off];
1652 data->nents = min_t(unsigned int, nents - data->sg_off,
1653 ISCSI_ISER_SG_TABLESIZE);
1654 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1655 PAGE_SIZE);
1656
1657 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1658 data->dma_dir);
1659 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001660 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001661 return -EINVAL;
1662 }
1663
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001664 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001665 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001666
1667 return 0;
1668}
1669
1670static void
1671isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1672{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001673 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001674
1675 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1676 memset(data, 0, sizeof(*data));
1677}
1678
1679
1680
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001681static void
1682isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1683{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001684 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001685
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001686 if (isert_cmd->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001687 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001688 isert_unmap_data_buf(isert_conn, &isert_cmd->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001689 }
1690
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001691 if (isert_cmd->rdma_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001692 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001693 kfree(isert_cmd->rdma_wr);
1694 isert_cmd->rdma_wr = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001695 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001696
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001697 if (isert_cmd->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001698 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001699 kfree(isert_cmd->ib_sge);
1700 isert_cmd->ib_sge = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001701 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001702}
1703
1704static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001705isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001706{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001707 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001708
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001709 if (isert_cmd->fr_desc) {
1710 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, isert_cmd->fr_desc);
1711 if (isert_cmd->fr_desc->ind & ISERT_PROTECTED) {
1712 isert_unmap_data_buf(isert_conn, &isert_cmd->prot);
1713 isert_cmd->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001714 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001715 spin_lock_bh(&isert_conn->pool_lock);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001716 list_add_tail(&isert_cmd->fr_desc->list, &isert_conn->fr_pool);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001717 spin_unlock_bh(&isert_conn->pool_lock);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001718 isert_cmd->fr_desc = NULL;
Vu Pham59464ef2013-08-28 23:23:35 +03001719 }
1720
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001721 if (isert_cmd->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001722 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001723 isert_unmap_data_buf(isert_conn, &isert_cmd->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001724 }
1725
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001726 isert_cmd->ib_sge = NULL;
1727 isert_cmd->rdma_wr = NULL;
Vu Pham59464ef2013-08-28 23:23:35 +03001728}
1729
1730static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001731isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001732{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001733 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001734 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001735 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001736 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001737 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001738
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001739 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001740
1741 switch (cmd->iscsi_opcode) {
1742 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001743 spin_lock_bh(&conn->cmd_lock);
1744 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001745 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001746 spin_unlock_bh(&conn->cmd_lock);
1747
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001748 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001749 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001750 /*
1751 * Check for special case during comp_err where
1752 * WRITE_PENDING has been handed off from core,
1753 * but requires an extra target_put_sess_cmd()
1754 * before transport_generic_free_cmd() below.
1755 */
1756 if (comp_err &&
1757 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1758 struct se_cmd *se_cmd = &cmd->se_cmd;
1759
Bart Van Asscheafc16602015-04-27 13:52:36 +02001760 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001761 }
1762 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001763
Vu Phamd40945d2013-08-28 23:23:34 +03001764 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001765 transport_generic_free_cmd(&cmd->se_cmd, 0);
1766 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001767 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001768 spin_lock_bh(&conn->cmd_lock);
1769 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001770 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001771 spin_unlock_bh(&conn->cmd_lock);
1772
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001773 transport_generic_free_cmd(&cmd->se_cmd, 0);
1774 break;
1775 case ISCSI_OP_REJECT:
1776 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001777 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001778 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1779 /* If the continue bit is on, keep the command alive */
1780 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1781 break;
1782
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001783 spin_lock_bh(&conn->cmd_lock);
1784 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001785 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001786 spin_unlock_bh(&conn->cmd_lock);
1787
1788 /*
1789 * Handle special case for REJECT when iscsi_add_reject*() has
1790 * overwritten the original iscsi_opcode assignment, and the
1791 * associated cmd->se_cmd needs to be released.
1792 */
1793 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001794 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001795 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001796 transport_generic_free_cmd(&cmd->se_cmd, 0);
1797 break;
1798 }
1799 /*
1800 * Fall-through
1801 */
1802 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001803 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001804 break;
1805 }
1806}
1807
1808static void
1809isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1810{
1811 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001812 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001813 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1814 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1815 tx_desc->dma_addr = 0;
1816 }
1817}
1818
1819static void
1820isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001821 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001822{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001823 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001824 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001825 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1826 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1827 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001828 }
1829
1830 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001831 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001832}
1833
Sagi Grimberg96b79732014-03-17 12:52:18 +02001834static int
1835isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1836{
1837 struct ib_mr_status mr_status;
1838 int ret;
1839
1840 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1841 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001842 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001843 goto fail_mr_status;
1844 }
1845
1846 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1847 u64 sec_offset_err;
1848 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1849
1850 switch (mr_status.sig_err.err_type) {
1851 case IB_SIG_BAD_GUARD:
1852 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1853 break;
1854 case IB_SIG_BAD_REFTAG:
1855 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1856 break;
1857 case IB_SIG_BAD_APPTAG:
1858 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1859 break;
1860 }
1861 sec_offset_err = mr_status.sig_err.sig_err_offset;
1862 do_div(sec_offset_err, block_size);
1863 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1864
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001865 isert_err("PI error found type %d at sector 0x%llx "
1866 "expected 0x%x vs actual 0x%x\n",
1867 mr_status.sig_err.err_type,
1868 (unsigned long long)se_cmd->bad_sector,
1869 mr_status.sig_err.expected,
1870 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001871 ret = 1;
1872 }
1873
1874fail_mr_status:
1875 return ret;
1876}
1877
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001878static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001879isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001880{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001881 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001882 struct isert_device *device = isert_conn->device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001883 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001884 struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001885 struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001886 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001887
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001888 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1889 isert_print_wc(wc, "rdma write");
1890 if (wc->status != IB_WC_WR_FLUSH_ERR)
1891 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1892 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1893 return;
1894 }
1895
1896 isert_dbg("Cmd %p\n", isert_cmd);
1897
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001898 if (isert_cmd->fr_desc && isert_cmd->fr_desc->ind & ISERT_PROTECTED) {
1899 ret = isert_check_pi_status(cmd,
1900 isert_cmd->fr_desc->pi_ctx->sig_mr);
1901 isert_cmd->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001902 }
1903
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001904 device->unreg_rdma_mem(isert_cmd, isert_conn);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001905 isert_cmd->rdma_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001906 if (ret)
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001907 transport_send_check_condition_and_sense(cmd, cmd->pi_err, 0);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001908 else
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001909 isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001910}
1911
1912static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001913isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001914{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001915 struct isert_conn *isert_conn = wc->qp->qp_context;
1916 struct isert_device *device = isert_conn->device;
1917 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001918 struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001919 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001920 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001921 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001922
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001923 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1924 isert_print_wc(wc, "rdma read");
1925 if (wc->status != IB_WC_WR_FLUSH_ERR)
1926 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1927 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1928 return;
1929 }
1930
1931 isert_dbg("Cmd %p\n", isert_cmd);
1932
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001933 if (isert_cmd->fr_desc && isert_cmd->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001934 ret = isert_check_pi_status(se_cmd,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001935 isert_cmd->fr_desc->pi_ctx->sig_mr);
1936 isert_cmd->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001937 }
1938
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001939 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001940 device->unreg_rdma_mem(isert_cmd, isert_conn);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001941 cmd->write_data_done = isert_cmd->data.len;
1942 isert_cmd->rdma_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001943
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001944 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001945 spin_lock_bh(&cmd->istate_lock);
1946 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1947 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1948 spin_unlock_bh(&cmd->istate_lock);
1949
Sagi Grimberg364189f2015-03-29 15:52:03 +03001950 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001951 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001952 transport_send_check_condition_and_sense(se_cmd,
1953 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001954 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001955 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001956 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001957}
1958
1959static void
1960isert_do_control_comp(struct work_struct *work)
1961{
1962 struct isert_cmd *isert_cmd = container_of(work,
1963 struct isert_cmd, comp_work);
1964 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001965 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001966 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001967
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001968 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1969
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001970 switch (cmd->i_state) {
1971 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001972 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001973 case ISTATE_SEND_REJECT: /* FALLTHRU */
1974 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001975 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001976 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1977 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001978 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001979 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001980 iscsit_logout_post_handler(cmd, cmd->conn);
1981 break;
1982 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001983 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001984 dump_stack();
1985 break;
1986 }
1987}
1988
1989static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001990isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001991{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001992 struct isert_conn *isert_conn = wc->qp->qp_context;
1993 struct ib_device *ib_dev = isert_conn->cm_id->device;
1994 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001995
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001996 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1997 isert_print_wc(wc, "login send");
1998 if (wc->status != IB_WC_WR_FLUSH_ERR)
1999 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2000 }
2001
2002 isert_unmap_tx_desc(tx_desc, ib_dev);
2003}
2004
2005static void
2006isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
2007{
2008 struct isert_conn *isert_conn = wc->qp->qp_context;
2009 struct ib_device *ib_dev = isert_conn->cm_id->device;
2010 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02002011 struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002012
2013 if (unlikely(wc->status != IB_WC_SUCCESS)) {
2014 isert_print_wc(wc, "send");
2015 if (wc->status != IB_WC_WR_FLUSH_ERR)
2016 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2017 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
2018 return;
2019 }
2020
2021 isert_dbg("Cmd %p\n", isert_cmd);
2022
2023 switch (isert_cmd->iscsi_cmd->i_state) {
2024 case ISTATE_SEND_TASKMGTRSP:
2025 case ISTATE_SEND_LOGOUTRSP:
2026 case ISTATE_SEND_REJECT:
2027 case ISTATE_SEND_TEXTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002028 isert_unmap_tx_desc(tx_desc, ib_dev);
2029
2030 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
2031 queue_work(isert_comp_wq, &isert_cmd->comp_work);
2032 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002033 default:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002034 isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
2035 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002036 break;
2037 }
2038}
2039
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002040static int
2041isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2042{
2043 struct ib_send_wr *wr_failed;
2044 int ret;
2045
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002046 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2047 if (ret) {
2048 isert_err("ib_post_recv failed with %d\n", ret);
2049 return ret;
2050 }
2051
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002052 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002053 &wr_failed);
2054 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002055 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002056 return ret;
2057 }
2058 return ret;
2059}
2060
2061static int
2062isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2063{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002064 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002065 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002066 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2067 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2068 &isert_cmd->tx_desc.iscsi_header;
2069
2070 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2071 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2072 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2073 /*
2074 * Attach SENSE DATA payload to iSCSI Response PDU
2075 */
2076 if (cmd->se_cmd.sense_buffer &&
2077 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2078 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002079 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002080 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002081 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002082 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002083
2084 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2085 cmd->sense_buffer);
2086 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2087
2088 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2089 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002090 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002091
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002092 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2093 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002094 DMA_TO_DEVICE);
2095
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002096 isert_cmd->pdu_buf_len = pdu_len;
2097 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2098 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002099 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002100 isert_cmd->tx_desc.num_sge = 2;
2101 }
2102
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002103 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002104
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002105 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002106
2107 return isert_post_response(isert_conn, isert_cmd);
2108}
2109
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002110static void
2111isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2112{
2113 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002114 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002115 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002116
2117 spin_lock_bh(&conn->cmd_lock);
2118 if (!list_empty(&cmd->i_conn_node))
2119 list_del_init(&cmd->i_conn_node);
2120 spin_unlock_bh(&conn->cmd_lock);
2121
2122 if (cmd->data_direction == DMA_TO_DEVICE)
2123 iscsit_stop_dataout_timer(cmd);
2124
2125 device->unreg_rdma_mem(isert_cmd, isert_conn);
2126}
2127
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002128static enum target_prot_op
2129isert_get_sup_prot_ops(struct iscsi_conn *conn)
2130{
Sagi Grimberg67004252015-03-29 15:52:09 +03002131 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002132 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002133
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002134 if (conn->tpg->tpg_attrib.t10_pi) {
2135 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002136 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002137 isert_conn->pi_support = true;
2138 return TARGET_PROT_ALL;
2139 }
2140 }
2141
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002142 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002143 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002144
2145 return TARGET_PROT_NORMAL;
2146}
2147
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002148static int
2149isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2150 bool nopout_response)
2151{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002152 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002153 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002154 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2155
2156 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2157 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2158 &isert_cmd->tx_desc.iscsi_header,
2159 nopout_response);
2160 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002161 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002162
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002163 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002164
2165 return isert_post_response(isert_conn, isert_cmd);
2166}
2167
2168static int
2169isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2170{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002171 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002172 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002173 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2174
2175 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2176 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2177 &isert_cmd->tx_desc.iscsi_header);
2178 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002179 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002180
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002181 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002182
2183 return isert_post_response(isert_conn, isert_cmd);
2184}
2185
2186static int
2187isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2188{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002189 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002190 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002191 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2192
2193 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2194 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2195 &isert_cmd->tx_desc.iscsi_header);
2196 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002197 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002198
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002199 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002200
2201 return isert_post_response(isert_conn, isert_cmd);
2202}
2203
2204static int
2205isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2206{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002207 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002208 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002209 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002210 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002211 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002212 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2213 struct iscsi_reject *hdr =
2214 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002215
2216 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002217 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002218 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002219
2220 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002221 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002222 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2223 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002224 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2225 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002226 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002227 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002228 isert_cmd->tx_desc.num_sge = 2;
2229
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002230 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002231
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002232 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002233
2234 return isert_post_response(isert_conn, isert_cmd);
2235}
2236
2237static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002238isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2239{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002240 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002241 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002242 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2243 struct iscsi_text_rsp *hdr =
2244 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2245 u32 txt_rsp_len;
2246 int rc;
2247
2248 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002249 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002250 if (rc < 0)
2251 return rc;
2252
2253 txt_rsp_len = rc;
2254 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2255
2256 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002257 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002258 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002259 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2260 void *txt_rsp_buf = cmd->buf_ptr;
2261
2262 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2263 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2264
2265 isert_cmd->pdu_buf_len = txt_rsp_len;
2266 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2267 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002268 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002269 isert_cmd->tx_desc.num_sge = 2;
2270 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002271 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002272
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002273 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002274
2275 return isert_post_response(isert_conn, isert_cmd);
2276}
2277
2278static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002279isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002280 struct ib_sge *ib_sge, struct ib_rdma_wr *rdma_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002281 u32 data_left, u32 offset)
2282{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002283 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002284 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002285 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002286 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002287 u32 sg_off, page_off;
2288 int i = 0, sg_nents;
2289
2290 sg_off = offset / PAGE_SIZE;
2291 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2292 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2293 page_off = offset % PAGE_SIZE;
2294
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002295 rdma_wr->wr.sg_list = ib_sge;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002296 rdma_wr->wr.wr_cqe = &isert_cmd->tx_desc.tx_cqe;
2297
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002298 /*
2299 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2300 */
2301 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002302 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2303 "page_off: %u\n",
2304 (unsigned long long)tmp_sg->dma_address,
2305 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002306
2307 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2308 ib_sge->length = min_t(u32, data_left,
2309 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002310 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002311
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002312 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2313 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002314 page_off = 0;
2315 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002316 if (!data_left)
2317 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002318 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002319 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002320 }
2321
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002322 rdma_wr->wr.num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002323 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002324 rdma_wr->wr.sg_list, rdma_wr->wr.num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002325
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002326 return rdma_wr->wr.num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002327}
2328
2329static int
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002330isert_map_rdma(struct isert_cmd *isert_cmd, struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002331{
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002332 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002333 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberg67004252015-03-29 15:52:09 +03002334 struct isert_conn *isert_conn = conn->context;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002335 struct isert_data_buf *data = &isert_cmd->data;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002336 struct ib_rdma_wr *rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002337 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002338 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2339 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002340
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002341 offset = isert_cmd->iser_ib_op == ISER_IB_RDMA_READ ?
2342 cmd->write_data_done : 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002343 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2344 se_cmd->t_data_nents, se_cmd->data_length,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002345 offset, isert_cmd->iser_ib_op,
2346 &isert_cmd->data);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002347 if (ret)
2348 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002350 data_left = data->len;
2351 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002352
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002353 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002354 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002355 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002356 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002357 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002358 }
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002359 isert_cmd->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002360
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002361 isert_cmd->rdma_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2362 isert_cmd->rdma_wr = kzalloc(sizeof(struct ib_rdma_wr) *
2363 isert_cmd->rdma_wr_num, GFP_KERNEL);
2364 if (!isert_cmd->rdma_wr) {
2365 isert_dbg("Unable to allocate isert_cmd->rdma_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002366 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002367 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002368 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002369
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002370 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002371
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002372 for (i = 0; i < isert_cmd->rdma_wr_num; i++) {
2373 rdma_wr = &isert_cmd->rdma_wr[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002374 data_len = min(data_left, rdma_write_max);
2375
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002376 rdma_wr->wr.send_flags = 0;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002377 if (isert_cmd->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002378 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
2379
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002380 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2381 rdma_wr->remote_addr = isert_cmd->read_va + offset;
2382 rdma_wr->rkey = isert_cmd->read_stag;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002383 if (i + 1 == isert_cmd->rdma_wr_num)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002384 rdma_wr->wr.next = &isert_cmd->tx_desc.send_wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002385 else
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002386 rdma_wr->wr.next = &isert_cmd->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002387 } else {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002388 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2389
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002390 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2391 rdma_wr->remote_addr = isert_cmd->write_va + va_offset;
2392 rdma_wr->rkey = isert_cmd->write_stag;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002393 if (i + 1 == isert_cmd->rdma_wr_num)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002394 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002395 else
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002396 rdma_wr->wr.next = &isert_cmd->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002397 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002398
2399 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002400 rdma_wr, data_len, offset);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002401 ib_sge += ib_sge_cnt;
2402
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002403 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002404 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002405 data_left -= data_len;
2406 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002407
2408 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002409unmap_cmd:
2410 isert_unmap_data_buf(isert_conn, data);
2411
Vu Pham90ecc6e2013-08-28 23:23:33 +03002412 return ret;
2413}
2414
Sagi Grimberg10633c32014-12-07 13:12:04 +02002415static inline void
2416isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2417{
2418 u32 rkey;
2419
2420 memset(inv_wr, 0, sizeof(*inv_wr));
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002421 inv_wr->wr_cqe = NULL;
Sagi Grimberg10633c32014-12-07 13:12:04 +02002422 inv_wr->opcode = IB_WR_LOCAL_INV;
2423 inv_wr->ex.invalidate_rkey = mr->rkey;
2424
2425 /* Bump the key */
2426 rkey = ib_inc_rkey(mr->rkey);
2427 ib_update_fast_reg_key(mr, rkey);
2428}
2429
Vu Pham59464ef2013-08-28 23:23:35 +03002430static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002431isert_fast_reg_mr(struct isert_conn *isert_conn,
2432 struct fast_reg_descriptor *fr_desc,
2433 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002434 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002435 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002436{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002437 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002438 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002439 struct ib_mr *mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002440 struct ib_reg_wr reg_wr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002441 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002442 int ret, n;
Vu Pham59464ef2013-08-28 23:23:35 +03002443
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002444 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002445 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002446 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2447 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002448 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2449 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002450 return 0;
2451 }
2452
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002453 if (ind == ISERT_DATA_KEY_VALID)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002454 /* Registering data buffer */
2455 mr = fr_desc->data_mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002456 else
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002457 /* Registering protection buffer */
2458 mr = fr_desc->pi_ctx->prot_mr;
Vu Pham59464ef2013-08-28 23:23:35 +03002459
Sagi Grimberg10633c32014-12-07 13:12:04 +02002460 if (!(fr_desc->ind & ind)) {
2461 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002462 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002463 }
2464
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002465 n = ib_map_mr_sg(mr, mem->sg, mem->nents, PAGE_SIZE);
2466 if (unlikely(n != mem->nents)) {
2467 isert_err("failed to map mr sg (%d/%d)\n",
2468 n, mem->nents);
2469 return n < 0 ? n : -EINVAL;
2470 }
2471
2472 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2473 fr_desc, mem->nents, mem->offset);
2474
2475 reg_wr.wr.next = NULL;
2476 reg_wr.wr.opcode = IB_WR_REG_MR;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002477 reg_wr.wr.wr_cqe = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002478 reg_wr.wr.send_flags = 0;
2479 reg_wr.wr.num_sge = 0;
2480 reg_wr.mr = mr;
2481 reg_wr.key = mr->lkey;
2482 reg_wr.access = IB_ACCESS_LOCAL_WRITE;
Vu Pham59464ef2013-08-28 23:23:35 +03002483
2484 if (!wr)
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002485 wr = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002486 else
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002487 wr->next = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002488
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002489 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002490 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002491 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002492 return ret;
2493 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002494 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002495
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002496 sge->lkey = mr->lkey;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002497 sge->addr = mr->iova;
2498 sge->length = mr->length;
Vu Pham59464ef2013-08-28 23:23:35 +03002499
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002500 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2501 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002502
2503 return ret;
2504}
2505
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002506static inline void
2507isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2508 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002509{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002510 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002511 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2512 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2513 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002514 /*
2515 * At the moment we hard code those, but if in the future
2516 * the target core would like to use it, we will take it
2517 * from se_cmd.
2518 */
2519 domain->sig.dif.apptag_check_mask = 0xffff;
2520 domain->sig.dif.app_escape = true;
2521 domain->sig.dif.ref_escape = true;
2522 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2523 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2524 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002525};
2526
2527static int
2528isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2529{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002530 switch (se_cmd->prot_op) {
2531 case TARGET_PROT_DIN_INSERT:
2532 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002533 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002534 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002535 break;
2536 case TARGET_PROT_DOUT_INSERT:
2537 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002538 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002539 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002540 break;
2541 case TARGET_PROT_DIN_PASS:
2542 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002543 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2544 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002545 break;
2546 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002547 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002548 return -EINVAL;
2549 }
2550
2551 return 0;
2552}
2553
2554static inline u8
2555isert_set_prot_checks(u8 prot_checks)
2556{
2557 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2558 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2559 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2560}
2561
2562static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002563isert_reg_sig_mr(struct isert_conn *isert_conn,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002564 struct isert_cmd *isert_cmd,
Sagi Grimberg570db172014-12-02 16:57:31 +02002565 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002566{
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002567 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002568 struct ib_sig_handover_wr sig_wr;
2569 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002570 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2571 struct ib_sig_attrs sig_attrs;
2572 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002573
2574 memset(&sig_attrs, 0, sizeof(sig_attrs));
2575 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2576 if (ret)
2577 goto err;
2578
2579 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2580
2581 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002582 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002583 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002584 }
2585
2586 memset(&sig_wr, 0, sizeof(sig_wr));
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002587 sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002588 sig_wr.wr.wr_cqe = NULL;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002589 sig_wr.wr.sg_list = &isert_cmd->ib_sg[DATA];
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002590 sig_wr.wr.num_sge = 1;
2591 sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
2592 sig_wr.sig_attrs = &sig_attrs;
2593 sig_wr.sig_mr = pi_ctx->sig_mr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002594 if (se_cmd->t_prot_sg)
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002595 sig_wr.prot = &isert_cmd->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002596
2597 if (!wr)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002598 wr = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002599 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002600 wr->next = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002601
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002602 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002603 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002604 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002605 goto err;
2606 }
2607 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2608
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002609 isert_cmd->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2610 isert_cmd->ib_sg[SIG].addr = 0;
2611 isert_cmd->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002612 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2613 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2614 /*
2615 * We have protection guards on the wire
2616 * so we need to set a larget transfer
2617 */
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002618 isert_cmd->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002619
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002620 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002621 isert_cmd->ib_sg[SIG].addr, isert_cmd->ib_sg[SIG].length,
2622 isert_cmd->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002623err:
2624 return ret;
2625}
2626
Vu Pham59464ef2013-08-28 23:23:35 +03002627static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002628isert_handle_prot_cmd(struct isert_conn *isert_conn,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002629 struct isert_cmd *isert_cmd)
Sagi Grimberg570db172014-12-02 16:57:31 +02002630{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002631 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002632 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2633 int ret;
2634
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002635 if (!isert_cmd->fr_desc->pi_ctx) {
2636 ret = isert_create_pi_ctx(isert_cmd->fr_desc,
Sagi Grimberg570db172014-12-02 16:57:31 +02002637 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002638 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002639 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002640 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002641 isert_conn);
2642 return ret;
2643 }
2644 }
2645
2646 if (se_cmd->t_prot_sg) {
2647 ret = isert_map_data_buf(isert_conn, isert_cmd,
2648 se_cmd->t_prot_sg,
2649 se_cmd->t_prot_nents,
2650 se_cmd->prot_length,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002651 0,
2652 isert_cmd->iser_ib_op,
2653 &isert_cmd->prot);
Sagi Grimberg570db172014-12-02 16:57:31 +02002654 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002655 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002656 isert_conn);
2657 return ret;
2658 }
2659
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002660 memset(&isert_cmd->ib_sg[PROT], 0, sizeof(isert_cmd->ib_sg[PROT]));
2661 ret = isert_fast_reg_mr(isert_conn, isert_cmd->fr_desc,
2662 &isert_cmd->prot,
2663 ISERT_PROT_KEY_VALID,
2664 &isert_cmd->ib_sg[PROT]);
Sagi Grimberg570db172014-12-02 16:57:31 +02002665 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002666 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002667 isert_conn);
2668 goto unmap_prot_cmd;
2669 }
2670 }
2671
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002672 ret = isert_reg_sig_mr(isert_conn, isert_cmd, isert_cmd->fr_desc);
Sagi Grimberg570db172014-12-02 16:57:31 +02002673 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002674 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002675 isert_conn);
2676 goto unmap_prot_cmd;
2677 }
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002678 isert_cmd->fr_desc->ind |= ISERT_PROTECTED;
Sagi Grimberg570db172014-12-02 16:57:31 +02002679
2680 return 0;
2681
2682unmap_prot_cmd:
2683 if (se_cmd->t_prot_sg)
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002684 isert_unmap_data_buf(isert_conn, &isert_cmd->prot);
Sagi Grimberg570db172014-12-02 16:57:31 +02002685
2686 return ret;
2687}
2688
2689static int
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002690isert_reg_rdma(struct isert_cmd *isert_cmd, struct iscsi_conn *conn)
Vu Pham59464ef2013-08-28 23:23:35 +03002691{
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002692 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002693 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002694 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002695 struct fast_reg_descriptor *fr_desc = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002696 struct ib_rdma_wr *rdma_wr;
Sagi Grimberg570db172014-12-02 16:57:31 +02002697 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002698 u32 offset;
2699 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002700 unsigned long flags;
2701
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002702 offset = isert_cmd->iser_ib_op == ISER_IB_RDMA_READ ?
2703 cmd->write_data_done : 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002704 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2705 se_cmd->t_data_nents, se_cmd->data_length,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002706 offset, isert_cmd->iser_ib_op,
2707 &isert_cmd->data);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002708 if (ret)
2709 return ret;
2710
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002711 if (isert_cmd->data.dma_nents != 1 ||
2712 isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002713 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2714 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002715 struct fast_reg_descriptor, list);
2716 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002717 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002718 isert_cmd->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002719 }
2720
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002721 ret = isert_fast_reg_mr(isert_conn, fr_desc, &isert_cmd->data,
2722 ISERT_DATA_KEY_VALID, &isert_cmd->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002723 if (ret)
2724 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002725
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002726 if (isert_prot_cmd(isert_conn, se_cmd)) {
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002727 ret = isert_handle_prot_cmd(isert_conn, isert_cmd);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002728 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002729 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002730
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002731 ib_sg = &isert_cmd->ib_sg[SIG];
Sagi Grimberg570db172014-12-02 16:57:31 +02002732 } else {
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002733 ib_sg = &isert_cmd->ib_sg[DATA];
Sagi Grimberg570db172014-12-02 16:57:31 +02002734 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002735
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002736 memcpy(&isert_cmd->s_ib_sge, ib_sg, sizeof(*ib_sg));
2737 isert_cmd->ib_sge = &isert_cmd->s_ib_sge;
2738 isert_cmd->rdma_wr_num = 1;
2739 memset(&isert_cmd->s_rdma_wr, 0, sizeof(isert_cmd->s_rdma_wr));
2740 isert_cmd->rdma_wr = &isert_cmd->s_rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002741
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002742 rdma_wr = &isert_cmd->s_rdma_wr;
2743 rdma_wr->wr.sg_list = &isert_cmd->s_ib_sge;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002744 rdma_wr->wr.num_sge = 1;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002745 rdma_wr->wr.wr_cqe = &isert_cmd->tx_desc.tx_cqe;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002746 if (isert_cmd->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002747 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
2748
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002749 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2750 rdma_wr->remote_addr = isert_cmd->read_va;
2751 rdma_wr->rkey = isert_cmd->read_stag;
2752 rdma_wr->wr.send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002753 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002754 } else {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002755 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2756
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002757 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2758 rdma_wr->remote_addr = isert_cmd->write_va;
2759 rdma_wr->rkey = isert_cmd->write_stag;
2760 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002761 }
2762
Vu Pham59464ef2013-08-28 23:23:35 +03002763 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002764
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002765unmap_cmd:
2766 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002767 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2768 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2769 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002770 }
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002771 isert_unmap_data_buf(isert_conn, &isert_cmd->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002772
Vu Pham59464ef2013-08-28 23:23:35 +03002773 return ret;
2774}
2775
2776static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002777isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2778{
2779 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002780 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002781 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002782 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002783 struct ib_send_wr *wr_failed;
2784 int rc;
2785
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002786 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002787 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002788
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002789 isert_cmd->iser_ib_op = ISER_IB_RDMA_WRITE;
2790 rc = device->reg_rdma_mem(isert_cmd, conn);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002791 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002792 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002793 return rc;
2794 }
2795
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002796 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002797 /*
2798 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2799 */
2800 isert_create_send_desc(isert_conn, isert_cmd,
2801 &isert_cmd->tx_desc);
2802 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2803 &isert_cmd->tx_desc.iscsi_header);
2804 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2805 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002806 &isert_cmd->tx_desc.send_wr);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002807 isert_cmd->s_rdma_wr.wr.next = &isert_cmd->tx_desc.send_wr;
2808 isert_cmd->rdma_wr_num += 1;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002809
2810 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2811 if (rc) {
2812 isert_err("ib_post_recv failed with %d\n", rc);
2813 return rc;
2814 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002815 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002816
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002817 rc = ib_post_send(isert_conn->qp, &isert_cmd->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002818 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002819 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002820
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002821 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002822 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002823 "READ\n", isert_cmd);
2824 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002825 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002826 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002827
Vu Pham90ecc6e2013-08-28 23:23:33 +03002828 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002829}
2830
2831static int
2832isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2833{
2834 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002835 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002836 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002837 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002838 struct ib_send_wr *wr_failed;
2839 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002840
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002841 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002842 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002843 isert_cmd->iser_ib_op = ISER_IB_RDMA_READ;
2844 rc = device->reg_rdma_mem(isert_cmd, conn);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002845 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002846 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002847 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002848 }
2849
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002850 rc = ib_post_send(isert_conn->qp, &isert_cmd->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002851 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002852 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002853
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002854 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002855 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002856
Vu Pham90ecc6e2013-08-28 23:23:33 +03002857 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002858}
2859
2860static int
2861isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2862{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002863 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2864 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002865
2866 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002867 case ISTATE_REMOVE:
2868 spin_lock_bh(&conn->cmd_lock);
2869 list_del_init(&cmd->i_conn_node);
2870 spin_unlock_bh(&conn->cmd_lock);
2871 isert_put_cmd(isert_cmd, true);
2872 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002873 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2874 ret = isert_put_nopin(cmd, conn, false);
2875 break;
2876 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002877 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002878 ret = -EINVAL;
2879 break;
2880 }
2881
2882 return ret;
2883}
2884
2885static int
2886isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2887{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002888 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002889 int ret;
2890
2891 switch (state) {
2892 case ISTATE_SEND_LOGOUTRSP:
2893 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002894 if (!ret)
2895 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002896 break;
2897 case ISTATE_SEND_NOPIN:
2898 ret = isert_put_nopin(cmd, conn, true);
2899 break;
2900 case ISTATE_SEND_TASKMGTRSP:
2901 ret = isert_put_tm_rsp(cmd, conn);
2902 break;
2903 case ISTATE_SEND_REJECT:
2904 ret = isert_put_reject(cmd, conn);
2905 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002906 case ISTATE_SEND_TEXTRSP:
2907 ret = isert_put_text_rsp(cmd, conn);
2908 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002909 case ISTATE_SEND_STATUS:
2910 /*
2911 * Special case for sending non GOOD SCSI status from TX thread
2912 * context during pre se_cmd excecution failure.
2913 */
2914 ret = isert_put_response(conn, cmd);
2915 break;
2916 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002917 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002918 ret = -EINVAL;
2919 break;
2920 }
2921
2922 return ret;
2923}
2924
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002925struct rdma_cm_id *
2926isert_setup_id(struct isert_np *isert_np)
2927{
2928 struct iscsi_np *np = isert_np->np;
2929 struct rdma_cm_id *id;
2930 struct sockaddr *sa;
2931 int ret;
2932
2933 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002934 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002935
Guy Shapirofa201052015-10-22 15:20:10 +03002936 id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002937 RDMA_PS_TCP, IB_QPT_RC);
2938 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002939 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002940 ret = PTR_ERR(id);
2941 goto out;
2942 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002943 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002944
2945 ret = rdma_bind_addr(id, sa);
2946 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002947 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002948 goto out_id;
2949 }
2950
Sagi Grimberg992607e2015-03-29 15:52:18 +03002951 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002952 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002953 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002954 goto out_id;
2955 }
2956
2957 return id;
2958out_id:
2959 rdma_destroy_id(id);
2960out:
2961 return ERR_PTR(ret);
2962}
2963
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002964static int
2965isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07002966 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002967{
2968 struct isert_np *isert_np;
2969 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002970 int ret;
2971
2972 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2973 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002974 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002975 return -ENOMEM;
2976 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002977 sema_init(&isert_np->sem, 0);
2978 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002979 INIT_LIST_HEAD(&isert_np->accepted);
2980 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002981 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002982
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002983 /*
2984 * Setup the np->np_sockaddr from the passed sockaddr setup
2985 * in iscsi_target_configfs.c code..
2986 */
2987 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07002988 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002989
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002990 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002991 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002992 ret = PTR_ERR(isert_lid);
2993 goto out;
2994 }
2995
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002996 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002997 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002998
2999 return 0;
3000
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003001out:
3002 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003003
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003004 return ret;
3005}
3006
3007static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003008isert_rdma_accept(struct isert_conn *isert_conn)
3009{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003010 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003011 struct rdma_conn_param cp;
3012 int ret;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003013 struct iser_cm_hdr rsp_hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003014
3015 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003016 cp.initiator_depth = isert_conn->initiator_depth;
3017 cp.retry_count = 7;
3018 cp.rnr_retry_count = 7;
3019
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003020 memset(&rsp_hdr, 0, sizeof(rsp_hdr));
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02003021 rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
3022 if (!isert_conn->snd_w_inv)
3023 rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003024 cp.private_data = (void *)&rsp_hdr;
3025 cp.private_data_len = sizeof(rsp_hdr);
3026
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003027 ret = rdma_accept(cm_id, &cp);
3028 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003029 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003030 return ret;
3031 }
3032
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003033 return 0;
3034}
3035
3036static int
3037isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3038{
Sagi Grimberg67004252015-03-29 15:52:09 +03003039 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003040 int ret;
3041
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003042 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003043 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3044 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003045 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003046 isert_conn);
3047 return ret;
3048 }
3049 reinit_completion(&isert_conn->login_req_comp);
3050
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003051 /*
3052 * For login requests after the first PDU, isert_rx_login_req() will
3053 * kick schedule_delayed_work(&conn->login_work) as the packet is
3054 * received, which turns this callback from iscsi_target_do_login_rx()
3055 * into a NOP.
3056 */
3057 if (!login->first_request)
3058 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003059
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003060 isert_rx_login_req(isert_conn);
3061
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003062 isert_info("before login_comp conn: %p\n", conn);
3063 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003064 if (ret)
3065 return ret;
3066
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003067 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003068
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003069 return 0;
3070}
3071
3072static void
3073isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3074 struct isert_conn *isert_conn)
3075{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003076 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003077 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003078
3079 conn->login_family = np->np_sockaddr.ss_family;
3080
Andy Groverdc58f762015-08-24 10:26:05 -07003081 conn->login_sockaddr = cm_route->addr.dst_addr;
3082 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003083}
3084
3085static int
3086isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3087{
Sagi Grimberg67004252015-03-29 15:52:09 +03003088 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003089 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003090 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003091
3092accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003093 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003094 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003095 return -ENODEV;
3096
3097 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003098 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003099 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003100 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003101 np->np_thread_state);
3102 /**
3103 * No point in stalling here when np_thread
3104 * is in state RESET/SHUTDOWN/EXIT - bail
3105 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003106 return -ENODEV;
3107 }
3108 spin_unlock_bh(&np->np_thread_lock);
3109
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003110 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003111 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003112 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003113 goto accept_wait;
3114 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003115 isert_conn = list_first_entry(&isert_np->pending,
3116 struct isert_conn, node);
3117 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003118 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003119
3120 conn->context = isert_conn;
3121 isert_conn->conn = conn;
Jenny Derzhavetzaea92982016-02-24 19:23:59 +02003122 isert_conn->state = ISER_CONN_BOUND;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003123
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003124 isert_set_conn_info(np, conn, isert_conn);
3125
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003126 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003127
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003128 return 0;
3129}
3130
3131static void
3132isert_free_np(struct iscsi_np *np)
3133{
Sagi Grimberg67004252015-03-29 15:52:09 +03003134 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003135 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003136
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003137 if (isert_np->cm_id)
3138 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003139
Sagi Grimberg268e6812014-12-02 16:57:36 +02003140 /*
3141 * FIXME: At this point we don't have a good way to insure
3142 * that at this point we don't have hanging connections that
3143 * completed RDMA establishment but didn't start iscsi login
3144 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003145 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003146 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003147 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003148 if (!list_empty(&isert_np->pending)) {
3149 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003150 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003151 &isert_np->pending,
3152 node) {
3153 isert_info("cleaning isert_conn %p state (%d)\n",
3154 isert_conn, isert_conn->state);
3155 isert_connect_release(isert_conn);
3156 }
3157 }
3158
3159 if (!list_empty(&isert_np->accepted)) {
3160 isert_info("Still have isert accepted connections\n");
3161 list_for_each_entry_safe(isert_conn, n,
3162 &isert_np->accepted,
3163 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003164 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003165 isert_conn, isert_conn->state);
3166 isert_connect_release(isert_conn);
3167 }
3168 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003169 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003170
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003171 np->np_context = NULL;
3172 kfree(isert_np);
3173}
3174
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003175static void isert_release_work(struct work_struct *work)
3176{
3177 struct isert_conn *isert_conn = container_of(work,
3178 struct isert_conn,
3179 release_work);
3180
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003181 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003182
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003183 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003184 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003185 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003186
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003187 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003188 isert_put_conn(isert_conn);
3189}
3190
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003191static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003192isert_wait4logout(struct isert_conn *isert_conn)
3193{
3194 struct iscsi_conn *conn = isert_conn->conn;
3195
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003196 isert_info("conn %p\n", isert_conn);
3197
Sagi Grimberg991bb762014-12-07 13:12:01 +02003198 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003199 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003200 wait_for_completion_timeout(&conn->conn_logout_comp,
3201 SECONDS_FOR_LOGOUT_COMP * HZ);
3202 }
3203}
3204
3205static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003206isert_wait4cmds(struct iscsi_conn *conn)
3207{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003208 isert_info("iscsi_conn %p\n", conn);
3209
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003210 if (conn->sess) {
3211 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3212 target_wait_for_sess_cmds(conn->sess->se_sess);
3213 }
3214}
3215
3216static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02003217isert_beacon_done(struct ib_cq *cq, struct ib_wc *wc)
3218{
3219 struct isert_conn *isert_conn = wc->qp->qp_context;
3220
3221 isert_print_wc(wc, "beacon");
3222
3223 isert_info("conn %p completing wait_comp_err\n", isert_conn);
3224 complete(&isert_conn->wait_comp_err);
3225}
3226
3227static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003228isert_wait4flush(struct isert_conn *isert_conn)
3229{
3230 struct ib_recv_wr *bad_wr;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02003231 static struct ib_cqe cqe = { .done = isert_beacon_done };
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003232
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003233 isert_info("conn %p\n", isert_conn);
3234
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003235 init_completion(&isert_conn->wait_comp_err);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02003236 isert_conn->beacon.wr_cqe = &cqe;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003237 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003238 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003239 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003240 return;
3241 }
3242
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003243 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003244}
3245
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003246/**
3247 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3248 * unsolicitate dataout
3249 * @conn: iscsi connection
3250 *
3251 * We might still have commands that are waiting for unsolicited
3252 * dataouts messages. We must put the extra reference on those
3253 * before blocking on the target_wait_for_session_cmds
3254 */
3255static void
3256isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3257{
3258 struct iscsi_cmd *cmd, *tmp;
3259 static LIST_HEAD(drop_cmd_list);
3260
3261 spin_lock_bh(&conn->cmd_lock);
3262 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3263 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3264 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3265 (cmd->write_data_done < cmd->se_cmd.data_length))
3266 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3267 }
3268 spin_unlock_bh(&conn->cmd_lock);
3269
3270 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3271 list_del_init(&cmd->i_conn_node);
3272 if (cmd->i_state != ISTATE_REMOVE) {
3273 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3274
3275 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3276 isert_put_cmd(isert_cmd, true);
3277 }
3278 }
3279}
3280
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003281static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003282{
3283 struct isert_conn *isert_conn = conn->context;
3284
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003285 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003286
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003287 mutex_lock(&isert_conn->mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003288 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003289 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003290
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003291 isert_wait4flush(isert_conn);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003292 isert_put_unsol_pending_cmds(conn);
3293 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003294 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003295
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003296 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003297}
3298
3299static void isert_free_conn(struct iscsi_conn *conn)
3300{
3301 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003302
Sagi Grimberg524630d2015-06-04 19:49:21 +03003303 isert_wait4flush(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003304 isert_put_conn(isert_conn);
3305}
3306
3307static struct iscsit_transport iser_target_transport = {
3308 .name = "IB/iSER",
3309 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003310 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003311 .owner = THIS_MODULE,
3312 .iscsit_setup_np = isert_setup_np,
3313 .iscsit_accept_np = isert_accept_np,
3314 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003315 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003316 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003317 .iscsit_get_login_rx = isert_get_login_rx,
3318 .iscsit_put_login_tx = isert_put_login_tx,
3319 .iscsit_immediate_queue = isert_immediate_queue,
3320 .iscsit_response_queue = isert_response_queue,
3321 .iscsit_get_dataout = isert_get_dataout,
3322 .iscsit_queue_data_in = isert_put_datain,
3323 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003324 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003325 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003326};
3327
3328static int __init isert_init(void)
3329{
3330 int ret;
3331
Sagi Grimberg631af552015-01-25 19:09:50 +02003332 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3333 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003334 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003335 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003336 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003337 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003338 }
3339
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003340 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3341 WQ_UNBOUND_MAX_ACTIVE);
3342 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003343 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003344 ret = -ENOMEM;
3345 goto destroy_comp_wq;
3346 }
3347
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003348 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003349 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003350
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003351 return 0;
3352
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003353destroy_comp_wq:
3354 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003355
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003356 return ret;
3357}
3358
3359static void __exit isert_exit(void)
3360{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003361 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003362 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003363 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003364 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003365 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003366}
3367
3368MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003369MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003370MODULE_AUTHOR("nab@Linux-iSCSI.org");
3371MODULE_LICENSE("GPL");
3372
3373module_init(isert_init);
3374module_exit(isert_exit);