blob: 37016f01b5a54ad01640094910b3eadd463b1eae [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)
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +020036#define ISER_MAX_TX_CQ_LEN \
37 ((ISERT_QP_MAX_REQ_DTOS + ISCSI_DEF_XMIT_CMDS_MAX) * ISERT_MAX_CONN)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020038#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39 ISERT_MAX_CONN)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080040
Sagi Grimberg45678b62015-01-25 19:11:19 +020041static int isert_debug_level;
Sagi Grimberg24f412d2014-12-07 13:12:02 +020042module_param_named(debug_level, isert_debug_level, int, 0644);
43MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
44
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080045static DEFINE_MUTEX(device_list_mutex);
46static LIST_HEAD(device_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080047static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +020048static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080049
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020050static int
51isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020052static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +020053isert_login_post_recv(struct isert_conn *isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020054static int
55isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020056struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030057
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030058static void isert_release_work(struct work_struct *work);
Christoph Hellwig9679cc52016-02-24 19:24:06 +020059static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc);
60static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc);
61static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc);
62static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030063
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020064static inline bool
65isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
66{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020067 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020068 cmd->prot_op != TARGET_PROT_NORMAL);
69}
70
71
Vu Phamd40945d2013-08-28 23:23:34 +030072static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080073isert_qp_event_callback(struct ib_event *e, void *context)
74{
Sagi Grimberg67004252015-03-29 15:52:09 +030075 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080076
Sagi Grimbergea8a1612015-05-18 13:40:31 +030077 isert_err("%s (%d): conn %p\n",
78 ib_event_msg(e->event), e->event, isert_conn);
79
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080080 switch (e->event) {
81 case IB_EVENT_COMM_EST:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030082 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080083 break;
84 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020085 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080086 break;
87 default:
88 break;
89 }
90}
91
Sagi Grimberg40fc0692015-03-29 15:52:10 +030092static struct isert_comp *
93isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080094{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030095 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +020096 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +030097 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080098
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080099 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200100 for (i = 0; i < device->comps_used; i++)
101 if (device->comps[i].active_qps <
102 device->comps[min].active_qps)
103 min = i;
104 comp = &device->comps[min];
105 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300106 mutex_unlock(&device_list_mutex);
107
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200108 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200109 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300110
111 return comp;
112}
113
114static void
115isert_comp_put(struct isert_comp *comp)
116{
117 mutex_lock(&device_list_mutex);
118 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800119 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300120}
121
122static struct ib_qp *
123isert_create_qp(struct isert_conn *isert_conn,
124 struct isert_comp *comp,
125 struct rdma_cm_id *cma_id)
126{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300127 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300128 struct ib_qp_init_attr attr;
129 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800130
131 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
132 attr.event_handler = isert_qp_event_callback;
133 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200134 attr.send_cq = comp->cq;
135 attr.recv_cq = comp->cq;
Sagi Grimberg572a1432016-02-24 19:24:09 +0200136 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS + 1;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200137 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +0200138 attr.cap.max_rdma_ctxs = ISCSI_DEF_XMIT_CMDS_MAX;
Or Gerlitz4a061b22015-12-18 10:59:46 +0200139 attr.cap.max_send_sge = device->ib_device->attrs.max_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800140 attr.cap.max_recv_sge = 1;
141 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
142 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200143 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200144 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800145
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300146 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800147 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200148 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300149 return ERR_PTR(ret);
150 }
151
152 return cma_id->qp;
153}
154
155static int
156isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
157{
158 struct isert_comp *comp;
159 int ret;
160
161 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300162 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
163 if (IS_ERR(isert_conn->qp)) {
164 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200165 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800166 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800167
168 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200169err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300170 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200171 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800172}
173
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800174static int
175isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
176{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300177 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300178 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800179 struct iser_rx_desc *rx_desc;
180 struct ib_sge *rx_sg;
181 u64 dma_addr;
182 int i, j;
183
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300184 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800185 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300186 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800187 goto fail;
188
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300189 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800190
191 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
192 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
193 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
194 if (ib_dma_mapping_error(ib_dev, dma_addr))
195 goto dma_map_fail;
196
197 rx_desc->dma_addr = dma_addr;
198
199 rx_sg = &rx_desc->rx_sg;
200 rx_sg->addr = rx_desc->dma_addr;
201 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600202 rx_sg->lkey = device->pd->local_dma_lkey;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200203 rx_desc->rx_cqe.done = isert_recv_done;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800204 }
205
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800206 return 0;
207
208dma_map_fail:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300209 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800210 for (j = 0; j < i; j++, rx_desc++) {
211 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
212 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
213 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300214 kfree(isert_conn->rx_descs);
215 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800216fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200217 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
218
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800219 return -ENOMEM;
220}
221
222static void
223isert_free_rx_descriptors(struct isert_conn *isert_conn)
224{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300225 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800226 struct iser_rx_desc *rx_desc;
227 int i;
228
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300229 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800230 return;
231
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300232 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800233 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
234 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
235 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
236 }
237
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300238 kfree(isert_conn->rx_descs);
239 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800240}
241
Sagi Grimberg172369c2015-03-29 15:52:11 +0300242static void
243isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800244{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300245 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800246
Sagi Grimberg172369c2015-03-29 15:52:11 +0300247 for (i = 0; i < device->comps_used; i++) {
248 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300249
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200250 if (comp->cq)
251 ib_free_cq(comp->cq);
Vu Pham59464ef2013-08-28 23:23:35 +0300252 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300253 kfree(device->comps);
254}
Vu Phamd40945d2013-08-28 23:23:34 +0300255
Sagi Grimberg172369c2015-03-29 15:52:11 +0300256static int
Or Gerlitz4a061b22015-12-18 10:59:46 +0200257isert_alloc_comps(struct isert_device *device)
Sagi Grimberg172369c2015-03-29 15:52:11 +0300258{
259 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200260
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200261 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300262 device->ib_device->num_comp_vectors));
263
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200264 isert_info("Using %d CQs, %s supports %d vectors support "
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +0200265 "pi_capable %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200266 device->comps_used, device->ib_device->name,
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +0200267 device->ib_device->num_comp_vectors,
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200268 device->pi_capable);
269
270 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
271 GFP_KERNEL);
272 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200273 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800274 return -ENOMEM;
275 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800276
Or Gerlitz4a061b22015-12-18 10:59:46 +0200277 max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300278
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200279 for (i = 0; i < device->comps_used; i++) {
280 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800281
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200282 comp->device = device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200283 comp->cq = ib_alloc_cq(device->ib_device, comp, max_cqe, i,
284 IB_POLL_WORKQUEUE);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200285 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300286 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200287 ret = PTR_ERR(comp->cq);
288 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800289 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800290 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800291 }
292
Sagi Grimberg172369c2015-03-29 15:52:11 +0300293 return 0;
294out_cq:
295 isert_free_comps(device);
296 return ret;
297}
298
299static int
300isert_create_device_ib_res(struct isert_device *device)
301{
Or Gerlitz4a061b22015-12-18 10:59:46 +0200302 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300303 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300304
Or Gerlitz4a061b22015-12-18 10:59:46 +0200305 isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
306 isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300307
Or Gerlitz4a061b22015-12-18 10:59:46 +0200308 ret = isert_alloc_comps(device);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300309 if (ret)
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800310 goto out;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300311
Christoph Hellwiged082d32016-09-05 12:56:17 +0200312 device->pd = ib_alloc_pd(ib_dev, 0);
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300313 if (IS_ERR(device->pd)) {
314 ret = PTR_ERR(device->pd);
315 isert_err("failed to allocate pd, device %p, ret=%d\n",
316 device, ret);
317 goto out_cq;
318 }
319
Sagi Grimberg172369c2015-03-29 15:52:11 +0300320 /* Check signature cap */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200321 device->pi_capable = ib_dev->attrs.device_cap_flags &
Sagi Grimberg172369c2015-03-29 15:52:11 +0300322 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300323
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800324 return 0;
325
326out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300327 isert_free_comps(device);
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800328out:
329 if (ret > 0)
330 ret = -EINVAL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800331 return ret;
332}
333
334static void
335isert_free_device_ib_res(struct isert_device *device)
336{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200337 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800338
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300339 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300340 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800341}
342
343static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300344isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800345{
346 mutex_lock(&device_list_mutex);
347 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200348 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800349 if (!device->refcount) {
350 isert_free_device_ib_res(device);
351 list_del(&device->dev_node);
352 kfree(device);
353 }
354 mutex_unlock(&device_list_mutex);
355}
356
357static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300358isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800359{
360 struct isert_device *device;
361 int ret;
362
363 mutex_lock(&device_list_mutex);
364 list_for_each_entry(device, &device_list, dev_node) {
365 if (device->ib_device->node_guid == cma_id->device->node_guid) {
366 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200367 isert_info("Found iser device %p refcount %d\n",
368 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800369 mutex_unlock(&device_list_mutex);
370 return device;
371 }
372 }
373
374 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
375 if (!device) {
376 mutex_unlock(&device_list_mutex);
377 return ERR_PTR(-ENOMEM);
378 }
379
380 INIT_LIST_HEAD(&device->dev_node);
381
382 device->ib_device = cma_id->device;
383 ret = isert_create_device_ib_res(device);
384 if (ret) {
385 kfree(device);
386 mutex_unlock(&device_list_mutex);
387 return ERR_PTR(ret);
388 }
389
390 device->refcount++;
391 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200392 isert_info("Created a new iser device %p refcount %d\n",
393 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800394 mutex_unlock(&device_list_mutex);
395
396 return device;
397}
398
Vu Pham59464ef2013-08-28 23:23:35 +0300399static void
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300400isert_init_conn(struct isert_conn *isert_conn)
401{
402 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300403 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300404 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300405 init_completion(&isert_conn->login_req_comp);
Raju Rangoju63b268d2016-08-29 17:15:49 +0530406 init_waitqueue_head(&isert_conn->rem_wait);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300407 kref_init(&isert_conn->kref);
408 mutex_init(&isert_conn->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300409 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300410}
411
412static void
413isert_free_login_buf(struct isert_conn *isert_conn)
414{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300415 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300416
417 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200418 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200419 kfree(isert_conn->login_rsp_buf);
420
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300421 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200422 ISER_RX_PAYLOAD_SIZE,
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300423 DMA_FROM_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200424 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300425}
426
427static int
428isert_alloc_login_buf(struct isert_conn *isert_conn,
429 struct ib_device *ib_dev)
430{
431 int ret;
432
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200433 isert_conn->login_req_buf = kzalloc(sizeof(*isert_conn->login_req_buf),
434 GFP_KERNEL);
435 if (!isert_conn->login_req_buf) {
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300436 isert_err("Unable to allocate isert_conn->login_buf\n");
437 return -ENOMEM;
438 }
439
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300440 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200441 isert_conn->login_req_buf,
442 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300443 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
444 if (ret) {
445 isert_err("login_req_dma mapping error: %d\n", ret);
446 isert_conn->login_req_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200447 goto out_free_login_req_buf;
448 }
449
450 isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
451 if (!isert_conn->login_rsp_buf) {
Wei Yongjun1d5840c2016-08-07 12:20:38 +0000452 ret = -ENOMEM;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200453 goto out_unmap_login_req_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300454 }
455
456 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200457 isert_conn->login_rsp_buf,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200458 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300459 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
460 if (ret) {
461 isert_err("login_rsp_dma mapping error: %d\n", ret);
462 isert_conn->login_rsp_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200463 goto out_free_login_rsp_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300464 }
465
466 return 0;
467
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200468out_free_login_rsp_buf:
469 kfree(isert_conn->login_rsp_buf);
470out_unmap_login_req_buf:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300471 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200472 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
473out_free_login_req_buf:
474 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300475 return ret;
476}
477
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200478static void
479isert_set_nego_params(struct isert_conn *isert_conn,
480 struct rdma_conn_param *param)
481{
482 struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
483
484 /* Set max inflight RDMA READ requests */
485 isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
486 attr->max_qp_init_rd_atom);
487 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
488
489 if (param->private_data) {
490 u8 flags = *(u8 *)param->private_data;
491
492 /*
493 * use remote invalidation if the both initiator
494 * and the HCA support it
495 */
496 isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
497 (attr->device_cap_flags &
498 IB_DEVICE_MEM_MGT_EXTENSIONS);
499 if (isert_conn->snd_w_inv)
500 isert_info("Using remote invalidation\n");
501 }
502}
503
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800504static int
505isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
506{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200507 struct isert_np *isert_np = cma_id->context;
508 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800509 struct isert_conn *isert_conn;
510 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800511 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300512
513 spin_lock_bh(&np->np_thread_lock);
514 if (!np->enabled) {
515 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200516 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300517 return rdma_reject(cma_id, NULL, 0);
518 }
519 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800520
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200521 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800522 cma_id, cma_id->context);
523
524 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300525 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800526 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800527
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300528 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300529 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800530
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300531 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
532 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800533 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800534
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300535 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800536 if (IS_ERR(device)) {
537 ret = PTR_ERR(device);
538 goto out_rsp_dma_map;
539 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300540 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800541
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200542 isert_set_nego_params(isert_conn, &event->param.conn);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300543
Sagi Grimberg570db172014-12-02 16:57:31 +0200544 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800545 if (ret)
546 goto out_conn_dev;
547
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200548 ret = isert_login_post_recv(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200549 if (ret)
550 goto out_conn_dev;
551
552 ret = isert_rdma_accept(isert_conn);
553 if (ret)
554 goto out_conn_dev;
555
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300556 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300557 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300558 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800559
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800560 return 0;
561
562out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300563 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800564out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300565 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800566out:
567 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200568 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800569 return ret;
570}
571
572static void
573isert_connect_release(struct isert_conn *isert_conn)
574{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300575 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800576
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200577 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800578
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300579 BUG_ON(!device);
580
Sagi Grimberg19e20902014-12-02 16:57:26 +0200581 isert_free_rx_descriptors(isert_conn);
Raju Rangoju63b268d2016-08-29 17:15:49 +0530582 if (isert_conn->cm_id &&
583 !isert_conn->dev_removed)
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300584 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200585
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300586 if (isert_conn->qp) {
587 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200588
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300589 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300590 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800591 }
592
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200593 if (isert_conn->login_req_buf)
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300594 isert_free_login_buf(isert_conn);
595
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300596 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800597
Raju Rangoju63b268d2016-08-29 17:15:49 +0530598 if (isert_conn->dev_removed)
599 wake_up_interruptible(&isert_conn->rem_wait);
600 else
601 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800602}
603
604static void
605isert_connected_handler(struct rdma_cm_id *cma_id)
606{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200607 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300608 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300609
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200610 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200611
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300612 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300613 isert_conn->state = ISER_CONN_UP;
614 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300615 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300616
617 mutex_lock(&isert_np->mutex);
618 list_move_tail(&isert_conn->node, &isert_np->pending);
619 mutex_unlock(&isert_np->mutex);
620
621 isert_info("np %p: Allow accept_np to continue\n", isert_np);
622 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800623}
624
625static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300626isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800627{
628 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300629 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800630
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200631 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
632 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800633
634 isert_connect_release(isert_conn);
635}
636
637static void
638isert_put_conn(struct isert_conn *isert_conn)
639{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300640 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800641}
642
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200643static void
644isert_handle_unbound_conn(struct isert_conn *isert_conn)
645{
646 struct isert_np *isert_np = isert_conn->cm_id->context;
647
648 mutex_lock(&isert_np->mutex);
649 if (!list_empty(&isert_conn->node)) {
650 /*
651 * This means iscsi doesn't know this connection
652 * so schedule a cleanup ourselves
653 */
654 list_del_init(&isert_conn->node);
655 isert_put_conn(isert_conn);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200656 queue_work(isert_release_wq, &isert_conn->release_work);
657 }
658 mutex_unlock(&isert_np->mutex);
659}
660
Sagi Grimberg954f2372014-12-02 16:57:17 +0200661/**
662 * isert_conn_terminate() - Initiate connection termination
663 * @isert_conn: isert connection struct
664 *
665 * Notes:
Jenny Derzhavetzaea92982016-02-24 19:23:59 +0200666 * In case the connection state is BOUND, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200667 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200668 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200669 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300670 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200671 * safe to call multiple times.
672 */
673static void
674isert_conn_terminate(struct isert_conn *isert_conn)
675{
676 int err;
677
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200678 if (isert_conn->state >= ISER_CONN_TERMINATING)
679 return;
680
681 isert_info("Terminating conn %p state %d\n",
682 isert_conn, isert_conn->state);
683 isert_conn->state = ISER_CONN_TERMINATING;
684 err = rdma_disconnect(isert_conn->cm_id);
685 if (err)
686 isert_warn("Failed rdma_disconnect isert_conn %p\n",
687 isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200688}
689
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700690static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200691isert_np_cma_handler(struct isert_np *isert_np,
692 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800693{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300694 isert_dbg("%s (%d): isert np %p\n",
695 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200696
697 switch (event) {
698 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300699 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200700 break;
701 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300702 isert_np->cm_id = isert_setup_id(isert_np);
703 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200704 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300705 isert_np, PTR_ERR(isert_np->cm_id));
706 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200707 }
708 break;
709 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200710 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200711 isert_np, event);
712 }
713
714 return -1;
715}
716
717static int
718isert_disconnected_handler(struct rdma_cm_id *cma_id,
719 enum rdma_cm_event_type event)
720{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200721 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700722
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300723 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200724 switch (isert_conn->state) {
725 case ISER_CONN_TERMINATING:
726 break;
727 case ISER_CONN_UP:
728 isert_conn_terminate(isert_conn);
Sagi Grimberg572a1432016-02-24 19:24:09 +0200729 ib_drain_qp(isert_conn->qp);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200730 isert_handle_unbound_conn(isert_conn);
731 break;
732 case ISER_CONN_BOUND:
733 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
734 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
735 break;
736 default:
737 isert_warn("conn %p teminating in state %d\n",
738 isert_conn, isert_conn->state);
739 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300740 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200741
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700742 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800743}
744
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300745static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200746isert_connect_error(struct rdma_cm_id *cma_id)
747{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200748 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200749
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300750 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300751 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200752 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300753
754 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200755}
756
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800757static int
758isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
759{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200760 struct isert_np *isert_np = cma_id->context;
Raju Rangoju63b268d2016-08-29 17:15:49 +0530761 struct isert_conn *isert_conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800762 int ret = 0;
763
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300764 isert_info("%s (%d): status %d id %p np %p\n",
765 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200766 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800767
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200768 if (isert_np->cm_id == cma_id)
769 return isert_np_cma_handler(cma_id->context, event->event);
770
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800771 switch (event->event) {
772 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800773 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700774 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200775 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800776 break;
777 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800778 isert_connected_handler(cma_id);
779 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300780 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
781 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300782 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200783 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800784 break;
Raju Rangoju63b268d2016-08-29 17:15:49 +0530785 case RDMA_CM_EVENT_DEVICE_REMOVAL:
786 isert_conn = cma_id->qp->qp_context;
787 isert_conn->dev_removed = true;
788 isert_disconnected_handler(cma_id, event->event);
789 wait_event_interruptible(isert_conn->rem_wait,
790 isert_conn->state == ISER_CONN_DOWN);
791 kfree(isert_conn);
792 /*
793 * return non-zero from the callback to destroy
794 * the rdma cm id
795 */
796 return 1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200797 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
Steve Wise1e38a362016-10-26 12:36:48 -0700798 isert_info("Connection rejected: %s\n",
799 rdma_reject_msg(cma_id, event->status));
Sagi Grimberg954f2372014-12-02 16:57:17 +0200800 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800801 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300802 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200803 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800804 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200805 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800806 break;
807 }
808
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800809 return ret;
810}
811
812static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300813isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800814{
815 struct ib_recv_wr *rx_wr, *rx_wr_failed;
816 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800817 struct iser_rx_desc *rx_desc;
818
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300819 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300820 rx_desc = &isert_conn->rx_descs[i];
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200821
822 rx_wr->wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300823 rx_wr->sg_list = &rx_desc->rx_sg;
824 rx_wr->num_sge = 1;
825 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800826 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800827 rx_wr--;
828 rx_wr->next = NULL; /* mark end of work requests list */
829
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300830 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300831 &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200832 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200833 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300834
835 return ret;
836}
837
838static int
839isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
840{
841 struct ib_recv_wr *rx_wr_failed, rx_wr;
842 int ret;
843
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200844 rx_wr.wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300845 rx_wr.sg_list = &rx_desc->rx_sg;
846 rx_wr.num_sge = 1;
847 rx_wr.next = NULL;
848
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300849 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200850 if (ret)
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300851 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300852
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800853 return ret;
854}
855
856static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200857isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800858{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300859 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800860 struct ib_send_wr send_wr, *send_wr_failed;
861 int ret;
862
863 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
864 ISER_HEADERS_LEN, DMA_TO_DEVICE);
865
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200866 tx_desc->tx_cqe.done = isert_login_send_done;
867
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800868 send_wr.next = NULL;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200869 send_wr.wr_cqe = &tx_desc->tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800870 send_wr.sg_list = tx_desc->tx_sg;
871 send_wr.num_sge = tx_desc->num_sge;
872 send_wr.opcode = IB_WR_SEND;
873 send_wr.send_flags = IB_SEND_SIGNALED;
874
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300875 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200876 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200877 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800878
879 return ret;
880}
881
882static void
883isert_create_send_desc(struct isert_conn *isert_conn,
884 struct isert_cmd *isert_cmd,
885 struct iser_tx_desc *tx_desc)
886{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300887 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300888 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800889
890 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
891 ISER_HEADERS_LEN, DMA_TO_DEVICE);
892
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +0200893 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
894 tx_desc->iser_header.flags = ISCSI_CTRL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800895
896 tx_desc->num_sge = 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800897
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600898 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
899 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200900 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800901 }
902}
903
904static int
905isert_init_tx_hdrs(struct isert_conn *isert_conn,
906 struct iser_tx_desc *tx_desc)
907{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300908 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300909 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800910 u64 dma_addr;
911
912 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
913 ISER_HEADERS_LEN, DMA_TO_DEVICE);
914 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200915 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800916 return -ENOMEM;
917 }
918
919 tx_desc->dma_addr = dma_addr;
920 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
921 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600922 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800923
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200924 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
925 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
926 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800927
928 return 0;
929}
930
931static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800932isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +0200933 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800934{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800935 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
936
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200937 tx_desc->tx_cqe.done = isert_send_done;
938 send_wr->wr_cqe = &tx_desc->tx_cqe;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200939
940 if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
941 send_wr->opcode = IB_WR_SEND_WITH_INV;
942 send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
943 } else {
944 send_wr->opcode = IB_WR_SEND;
945 }
946
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800947 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800948 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800949 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800950}
951
952static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200953isert_login_post_recv(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800954{
955 struct ib_recv_wr rx_wr, *rx_wr_fail;
956 struct ib_sge sge;
957 int ret;
958
959 memset(&sge, 0, sizeof(struct ib_sge));
960 sge.addr = isert_conn->login_req_dma;
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200961 sge.length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600962 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800963
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200964 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800965 sge.addr, sge.length, sge.lkey);
966
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200967 isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
968
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800969 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200970 rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800971 rx_wr.sg_list = &sge;
972 rx_wr.num_sge = 1;
973
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300974 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200975 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200976 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800977
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800978 return ret;
979}
980
981static int
982isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
983 u32 length)
984{
985 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300986 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300987 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300988 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800989 int ret;
990
991 isert_create_send_desc(isert_conn, NULL, tx_desc);
992
993 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
994 sizeof(struct iscsi_hdr));
995
996 isert_init_tx_hdrs(isert_conn, tx_desc);
997
998 if (length > 0) {
999 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1000
1001 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1002 length, DMA_TO_DEVICE);
1003
1004 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1005
1006 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1007 length, DMA_TO_DEVICE);
1008
1009 tx_dsg->addr = isert_conn->login_rsp_dma;
1010 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001011 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001012 tx_desc->num_sge = 2;
1013 }
1014 if (!login->login_failed) {
1015 if (login->login_complete) {
1016 ret = isert_alloc_rx_descriptors(isert_conn);
1017 if (ret)
1018 return ret;
1019
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001020 ret = isert_post_recvm(isert_conn,
1021 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001022 if (ret)
1023 return ret;
1024
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001025 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001026 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001027 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001028 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001029 goto post_send;
1030 }
1031
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001032 ret = isert_login_post_recv(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001033 if (ret)
1034 return ret;
1035 }
1036post_send:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001037 ret = isert_login_post_send(isert_conn, tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001038 if (ret)
1039 return ret;
1040
1041 return 0;
1042}
1043
1044static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001045isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001046{
Christoph Hellwig5adabdd2016-02-24 19:24:05 +02001047 struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001048 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001049 struct iscsi_conn *conn = isert_conn->conn;
1050 struct iscsi_login *login = conn->conn_login;
1051 int size;
1052
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001053 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001054
1055 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001056
1057 if (login->first_request) {
1058 struct iscsi_login_req *login_req =
1059 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1060 /*
1061 * Setup the initial iscsi_login values from the leading
1062 * login request PDU.
1063 */
1064 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1065 login->current_stage =
1066 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1067 >> 2;
1068 login->version_min = login_req->min_version;
1069 login->version_max = login_req->max_version;
1070 memcpy(login->isid, login_req->isid, 6);
1071 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1072 login->init_task_tag = login_req->itt;
1073 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1074 login->cid = be16_to_cpu(login_req->cid);
1075 login->tsih = be16_to_cpu(login_req->tsih);
1076 }
1077
1078 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1079
1080 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001081 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1082 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1083 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001084 memcpy(login->req_buf, &rx_desc->data[0], size);
1085
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001086 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001087 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001088 return;
1089 }
1090 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001091}
1092
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001093static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001094*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001095{
Sagi Grimberg67004252015-03-29 15:52:09 +03001096 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001097 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001098 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001099
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001100 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001101 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001102 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001103 return NULL;
1104 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001105 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001106 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001107 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001108 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001109
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001110 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001111}
1112
1113static int
1114isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001115 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1116 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001117{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001118 struct iscsi_conn *conn = isert_conn->conn;
1119 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001120 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1121 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001122 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001123
1124 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1125 if (rc < 0)
1126 return rc;
1127
1128 imm_data = cmd->immediate_data;
1129 imm_data_len = cmd->first_burst_len;
1130 unsol_data = cmd->unsolicited_data;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001131 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001132
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001133 if (imm_data && imm_data_len == data_len)
1134 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001135 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1136 if (rc < 0) {
1137 return 0;
1138 } else if (rc > 0) {
1139 dump_payload = true;
1140 goto sequence_cmd;
1141 }
1142
1143 if (!imm_data)
1144 return 0;
1145
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001146 if (imm_data_len != data_len) {
1147 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1148 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1149 &rx_desc->data[0], imm_data_len);
1150 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1151 sg_nents, imm_data_len);
1152 } else {
1153 sg_init_table(&isert_cmd->sg, 1);
1154 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1155 cmd->se_cmd.t_data_nents = 1;
1156 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1157 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1158 imm_data_len);
1159 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001160
1161 cmd->write_data_done += imm_data_len;
1162
1163 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1164 spin_lock_bh(&cmd->istate_lock);
1165 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1166 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1167 spin_unlock_bh(&cmd->istate_lock);
1168 }
1169
1170sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001171 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001172
1173 if (!rc && dump_payload == false && unsol_data)
1174 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001175 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001176 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001177
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001178 return 0;
1179}
1180
1181static int
1182isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1183 struct iser_rx_desc *rx_desc, unsigned char *buf)
1184{
1185 struct scatterlist *sg_start;
1186 struct iscsi_conn *conn = isert_conn->conn;
1187 struct iscsi_cmd *cmd = NULL;
1188 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1189 u32 unsol_data_len = ntoh24(hdr->dlength);
1190 int rc, sg_nents, sg_off, page_off;
1191
1192 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1193 if (rc < 0)
1194 return rc;
1195 else if (!cmd)
1196 return 0;
1197 /*
1198 * FIXME: Unexpected unsolicited_data out
1199 */
1200 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001201 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001202 dump_stack();
1203 return -1;
1204 }
1205
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001206 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1207 "write_data_done: %u, data_length: %u\n",
1208 unsol_data_len, cmd->write_data_done,
1209 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001210
1211 sg_off = cmd->write_data_done / PAGE_SIZE;
1212 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1213 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1214 page_off = cmd->write_data_done % PAGE_SIZE;
1215 /*
1216 * FIXME: Non page-aligned unsolicited_data out
1217 */
1218 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001219 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001220 dump_stack();
1221 return -1;
1222 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001223 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1224 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1225 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001226
1227 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1228 unsol_data_len);
1229
1230 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1231 if (rc < 0)
1232 return rc;
1233
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001234 /*
1235 * multiple data-outs on the same command can arrive -
1236 * so post the buffer before hand
1237 */
1238 rc = isert_post_recv(isert_conn, rx_desc);
1239 if (rc) {
1240 isert_err("ib_post_recv failed with %d\n", rc);
1241 return rc;
1242 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001243 return 0;
1244}
1245
1246static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001247isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001248 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1249 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001250{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001251 struct iscsi_conn *conn = isert_conn->conn;
1252 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1253 int rc;
1254
1255 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1256 if (rc < 0)
1257 return rc;
1258 /*
1259 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1260 */
1261
1262 return iscsit_process_nop_out(conn, cmd, hdr);
1263}
1264
1265static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001266isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001267 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1268 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001269{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001270 struct iscsi_conn *conn = isert_conn->conn;
1271 u32 payload_length = ntoh24(hdr->dlength);
1272 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001273 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001274
1275 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1276 if (rc < 0)
1277 return rc;
1278
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001279 if (payload_length) {
1280 text_in = kzalloc(payload_length, GFP_KERNEL);
1281 if (!text_in) {
1282 isert_err("Unable to allocate text_in of payload_length: %u\n",
1283 payload_length);
1284 return -ENOMEM;
1285 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001286 }
1287 cmd->text_in_ptr = text_in;
1288
1289 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1290
1291 return iscsit_process_text_cmd(conn, cmd, hdr);
1292}
1293
1294static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001295isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1296 uint32_t read_stag, uint64_t read_va,
1297 uint32_t write_stag, uint64_t write_va)
1298{
1299 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1300 struct iscsi_conn *conn = isert_conn->conn;
1301 struct iscsi_cmd *cmd;
1302 struct isert_cmd *isert_cmd;
1303 int ret = -EINVAL;
1304 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1305
Sagi Grimbergfb140272015-03-29 15:52:08 +03001306 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001307 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001308 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001309 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001310 return 0;
1311 }
1312
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001313 switch (opcode) {
1314 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001315 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001316 if (!cmd)
1317 break;
1318
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001319 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001320 isert_cmd->read_stag = read_stag;
1321 isert_cmd->read_va = read_va;
1322 isert_cmd->write_stag = write_stag;
1323 isert_cmd->write_va = write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001324 isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001325
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001326 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001327 rx_desc, (unsigned char *)hdr);
1328 break;
1329 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001330 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001331 if (!cmd)
1332 break;
1333
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001334 isert_cmd = iscsit_priv_cmd(cmd);
1335 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001336 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001337 break;
1338 case ISCSI_OP_SCSI_DATA_OUT:
1339 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1340 (unsigned char *)hdr);
1341 break;
1342 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001343 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001344 if (!cmd)
1345 break;
1346
1347 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1348 (unsigned char *)hdr);
1349 break;
1350 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001351 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001352 if (!cmd)
1353 break;
1354
1355 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001356 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001357 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001358 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001359 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001360 else
1361 cmd = isert_allocate_cmd(conn, rx_desc);
1362
1363 if (!cmd)
1364 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001365
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001366 isert_cmd = iscsit_priv_cmd(cmd);
1367 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001368 rx_desc, (struct iscsi_text *)hdr);
1369 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001370 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001371 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001372 dump_stack();
1373 break;
1374 }
1375
1376 return ret;
1377}
1378
1379static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001380isert_print_wc(struct ib_wc *wc, const char *type)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001381{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001382 if (wc->status != IB_WC_WR_FLUSH_ERR)
1383 isert_err("%s failure: %s (%d) vend_err %x\n", type,
1384 ib_wc_status_msg(wc->status), wc->status,
1385 wc->vendor_err);
1386 else
1387 isert_dbg("%s failure: %s (%d)\n", type,
1388 ib_wc_status_msg(wc->status), wc->status);
1389}
1390
1391static void
1392isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1393{
1394 struct isert_conn *isert_conn = wc->qp->qp_context;
1395 struct ib_device *ib_dev = isert_conn->cm_id->device;
1396 struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
1397 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001398 struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001399 uint64_t read_va = 0, write_va = 0;
1400 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001401
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001402 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1403 isert_print_wc(wc, "recv");
1404 if (wc->status != IB_WC_WR_FLUSH_ERR)
1405 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1406 return;
1407 }
1408
1409 ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
1410 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1411
1412 isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1413 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
1414 (int)(wc->byte_len - ISER_HEADERS_LEN));
1415
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001416 switch (iser_ctrl->flags & 0xF0) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001417 case ISCSI_CTRL:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001418 if (iser_ctrl->flags & ISER_RSV) {
1419 read_stag = be32_to_cpu(iser_ctrl->read_stag);
1420 read_va = be64_to_cpu(iser_ctrl->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001421 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1422 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001423 }
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001424 if (iser_ctrl->flags & ISER_WSV) {
1425 write_stag = be32_to_cpu(iser_ctrl->write_stag);
1426 write_va = be64_to_cpu(iser_ctrl->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001427 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1428 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001429 }
1430
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001431 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001432 break;
1433 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001434 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001435 break;
1436 default:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001437 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001438 break;
1439 }
1440
Bart Van Assche6c760b32015-10-22 11:14:18 -07001441 isert_rx_opcode(isert_conn, rx_desc,
1442 read_stag, read_va, write_stag, write_va);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001443
1444 ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
1445 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001446}
1447
1448static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001449isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001450{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001451 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001452 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001453
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001454 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1455 isert_print_wc(wc, "login recv");
1456 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001457 }
1458
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001459 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
1460 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001461
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001462 isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001463
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001464 if (isert_conn->conn) {
1465 struct iscsi_login *login = isert_conn->conn->conn_login;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001466
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001467 if (login && !login->first_request)
1468 isert_rx_login_req(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001469 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001470
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001471 mutex_lock(&isert_conn->mutex);
1472 complete(&isert_conn->login_req_comp);
1473 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001474
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001475 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
1476 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001477}
1478
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001479static void
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001480isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001481{
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001482 struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
1483 enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001484
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001485 if (!cmd->rw.nr_ops)
1486 return;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001487
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001488 if (isert_prot_cmd(conn, se_cmd)) {
1489 rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
1490 conn->cm_id->port_num, se_cmd->t_data_sg,
1491 se_cmd->t_data_nents, se_cmd->t_prot_sg,
1492 se_cmd->t_prot_nents, dir);
1493 } else {
1494 rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
1495 se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001496 }
1497
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001498 cmd->rw.nr_ops = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03001499}
1500
1501static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001502isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001503{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001504 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001505 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001506 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001507 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001508
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001509 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001510
1511 switch (cmd->iscsi_opcode) {
1512 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001513 spin_lock_bh(&conn->cmd_lock);
1514 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001515 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001516 spin_unlock_bh(&conn->cmd_lock);
1517
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001518 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001519 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001520 /*
1521 * Check for special case during comp_err where
1522 * WRITE_PENDING has been handed off from core,
1523 * but requires an extra target_put_sess_cmd()
1524 * before transport_generic_free_cmd() below.
1525 */
1526 if (comp_err &&
1527 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1528 struct se_cmd *se_cmd = &cmd->se_cmd;
1529
Bart Van Asscheafc16602015-04-27 13:52:36 +02001530 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001531 }
1532 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001533
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001534 isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001535 transport_generic_free_cmd(&cmd->se_cmd, 0);
1536 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001537 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001538 spin_lock_bh(&conn->cmd_lock);
1539 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001540 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001541 spin_unlock_bh(&conn->cmd_lock);
1542
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001543 transport_generic_free_cmd(&cmd->se_cmd, 0);
1544 break;
1545 case ISCSI_OP_REJECT:
1546 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001547 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001548 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1549 /* If the continue bit is on, keep the command alive */
1550 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1551 break;
1552
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001553 spin_lock_bh(&conn->cmd_lock);
1554 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001555 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001556 spin_unlock_bh(&conn->cmd_lock);
1557
1558 /*
1559 * Handle special case for REJECT when iscsi_add_reject*() has
1560 * overwritten the original iscsi_opcode assignment, and the
1561 * associated cmd->se_cmd needs to be released.
1562 */
1563 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001564 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001565 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001566 transport_generic_free_cmd(&cmd->se_cmd, 0);
1567 break;
1568 }
1569 /*
1570 * Fall-through
1571 */
1572 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001573 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001574 break;
1575 }
1576}
1577
1578static void
1579isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1580{
1581 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001582 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001583 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1584 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1585 tx_desc->dma_addr = 0;
1586 }
1587}
1588
1589static void
1590isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001591 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001592{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001593 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001594 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001595 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1596 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1597 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001598 }
1599
1600 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001601 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001602}
1603
Sagi Grimberg96b79732014-03-17 12:52:18 +02001604static int
1605isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1606{
1607 struct ib_mr_status mr_status;
1608 int ret;
1609
1610 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1611 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001612 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001613 goto fail_mr_status;
1614 }
1615
1616 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1617 u64 sec_offset_err;
1618 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1619
1620 switch (mr_status.sig_err.err_type) {
1621 case IB_SIG_BAD_GUARD:
1622 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1623 break;
1624 case IB_SIG_BAD_REFTAG:
1625 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1626 break;
1627 case IB_SIG_BAD_APPTAG:
1628 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1629 break;
1630 }
1631 sec_offset_err = mr_status.sig_err.sig_err_offset;
1632 do_div(sec_offset_err, block_size);
1633 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1634
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001635 isert_err("PI error found type %d at sector 0x%llx "
1636 "expected 0x%x vs actual 0x%x\n",
1637 mr_status.sig_err.err_type,
1638 (unsigned long long)se_cmd->bad_sector,
1639 mr_status.sig_err.expected,
1640 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001641 ret = 1;
1642 }
1643
1644fail_mr_status:
1645 return ret;
1646}
1647
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001648static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001649isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001650{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001651 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001652 struct isert_device *device = isert_conn->device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001653 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001654 struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001655 struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001656 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001657
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001658 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1659 isert_print_wc(wc, "rdma write");
1660 if (wc->status != IB_WC_WR_FLUSH_ERR)
1661 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1662 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1663 return;
1664 }
1665
1666 isert_dbg("Cmd %p\n", isert_cmd);
1667
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001668 ret = isert_check_pi_status(cmd, isert_cmd->rw.sig->sig_mr);
1669 isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001670
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001671 if (ret)
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001672 transport_send_check_condition_and_sense(cmd, cmd->pi_err, 0);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001673 else
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001674 isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001675}
1676
1677static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001678isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001679{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001680 struct isert_conn *isert_conn = wc->qp->qp_context;
1681 struct isert_device *device = isert_conn->device;
1682 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001683 struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001684 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001685 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001686 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001687
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001688 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1689 isert_print_wc(wc, "rdma read");
1690 if (wc->status != IB_WC_WR_FLUSH_ERR)
1691 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1692 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1693 return;
1694 }
1695
1696 isert_dbg("Cmd %p\n", isert_cmd);
1697
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001698 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001699
1700 if (isert_prot_cmd(isert_conn, se_cmd))
1701 ret = isert_check_pi_status(se_cmd, isert_cmd->rw.sig->sig_mr);
1702 isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1703 cmd->write_data_done = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001704
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001705 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001706 spin_lock_bh(&cmd->istate_lock);
1707 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1708 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1709 spin_unlock_bh(&cmd->istate_lock);
1710
Sagi Grimberg364189f2015-03-29 15:52:03 +03001711 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001712 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001713 transport_send_check_condition_and_sense(se_cmd,
1714 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001715 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001716 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001717 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001718}
1719
1720static void
1721isert_do_control_comp(struct work_struct *work)
1722{
1723 struct isert_cmd *isert_cmd = container_of(work,
1724 struct isert_cmd, comp_work);
1725 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001726 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001727 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001728
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001729 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1730
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001731 switch (cmd->i_state) {
1732 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001733 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001734 case ISTATE_SEND_REJECT: /* FALLTHRU */
1735 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001736 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001737 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1738 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001739 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001740 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001741 iscsit_logout_post_handler(cmd, cmd->conn);
1742 break;
1743 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001744 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001745 dump_stack();
1746 break;
1747 }
1748}
1749
1750static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001751isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001752{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001753 struct isert_conn *isert_conn = wc->qp->qp_context;
1754 struct ib_device *ib_dev = isert_conn->cm_id->device;
1755 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001756
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001757 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1758 isert_print_wc(wc, "login send");
1759 if (wc->status != IB_WC_WR_FLUSH_ERR)
1760 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1761 }
1762
1763 isert_unmap_tx_desc(tx_desc, ib_dev);
1764}
1765
1766static void
1767isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
1768{
1769 struct isert_conn *isert_conn = wc->qp->qp_context;
1770 struct ib_device *ib_dev = isert_conn->cm_id->device;
1771 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001772 struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001773
1774 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1775 isert_print_wc(wc, "send");
1776 if (wc->status != IB_WC_WR_FLUSH_ERR)
1777 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1778 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
1779 return;
1780 }
1781
1782 isert_dbg("Cmd %p\n", isert_cmd);
1783
1784 switch (isert_cmd->iscsi_cmd->i_state) {
1785 case ISTATE_SEND_TASKMGTRSP:
1786 case ISTATE_SEND_LOGOUTRSP:
1787 case ISTATE_SEND_REJECT:
1788 case ISTATE_SEND_TEXTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001789 isert_unmap_tx_desc(tx_desc, ib_dev);
1790
1791 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1792 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1793 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001794 default:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001795 isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
1796 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001797 break;
1798 }
1799}
1800
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001801static int
1802isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1803{
1804 struct ib_send_wr *wr_failed;
1805 int ret;
1806
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001807 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
1808 if (ret) {
1809 isert_err("ib_post_recv failed with %d\n", ret);
1810 return ret;
1811 }
1812
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001813 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001814 &wr_failed);
1815 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001816 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001817 return ret;
1818 }
1819 return ret;
1820}
1821
1822static int
1823isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1824{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001825 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001826 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001827 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1828 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1829 &isert_cmd->tx_desc.iscsi_header;
1830
1831 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1832 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1833 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1834 /*
1835 * Attach SENSE DATA payload to iSCSI Response PDU
1836 */
1837 if (cmd->se_cmd.sense_buffer &&
1838 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1839 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001840 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001841 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001842 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001843 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001844
1845 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1846 cmd->sense_buffer);
1847 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1848
1849 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1850 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001851 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001852
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001853 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1854 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001855 DMA_TO_DEVICE);
1856
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001857 isert_cmd->pdu_buf_len = pdu_len;
1858 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1859 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001860 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001861 isert_cmd->tx_desc.num_sge = 2;
1862 }
1863
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001864 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001865
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001866 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001867
1868 return isert_post_response(isert_conn, isert_cmd);
1869}
1870
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001871static void
1872isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1873{
1874 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001875 struct isert_conn *isert_conn = conn->context;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001876
1877 spin_lock_bh(&conn->cmd_lock);
1878 if (!list_empty(&cmd->i_conn_node))
1879 list_del_init(&cmd->i_conn_node);
1880 spin_unlock_bh(&conn->cmd_lock);
1881
1882 if (cmd->data_direction == DMA_TO_DEVICE)
1883 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001884 isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001885}
1886
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001887static enum target_prot_op
1888isert_get_sup_prot_ops(struct iscsi_conn *conn)
1889{
Sagi Grimberg67004252015-03-29 15:52:09 +03001890 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001891 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001892
Sagi Grimberg23a548e2014-12-02 16:57:35 +02001893 if (conn->tpg->tpg_attrib.t10_pi) {
1894 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001895 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02001896 isert_conn->pi_support = true;
1897 return TARGET_PROT_ALL;
1898 }
1899 }
1900
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001901 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02001902 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001903
1904 return TARGET_PROT_NORMAL;
1905}
1906
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001907static int
1908isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1909 bool nopout_response)
1910{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001911 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001912 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001913 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1914
1915 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1916 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1917 &isert_cmd->tx_desc.iscsi_header,
1918 nopout_response);
1919 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001920 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001921
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001922 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001923
1924 return isert_post_response(isert_conn, isert_cmd);
1925}
1926
1927static int
1928isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1929{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001930 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001931 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001932 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1933
1934 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1935 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1936 &isert_cmd->tx_desc.iscsi_header);
1937 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001938 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001939
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001940 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001941
1942 return isert_post_response(isert_conn, isert_cmd);
1943}
1944
1945static int
1946isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1947{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001948 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001949 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001950 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1951
1952 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1953 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1954 &isert_cmd->tx_desc.iscsi_header);
1955 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001956 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001957
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001958 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001959
1960 return isert_post_response(isert_conn, isert_cmd);
1961}
1962
1963static int
1964isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1965{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001966 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001967 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001968 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001969 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001970 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001971 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1972 struct iscsi_reject *hdr =
1973 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001974
1975 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001976 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001977 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001978
1979 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001980 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001981 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1982 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001983 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1984 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001985 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001986 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001987 isert_cmd->tx_desc.num_sge = 2;
1988
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001989 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001990
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001991 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001992
1993 return isert_post_response(isert_conn, isert_cmd);
1994}
1995
1996static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001997isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1998{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001999 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002000 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002001 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2002 struct iscsi_text_rsp *hdr =
2003 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2004 u32 txt_rsp_len;
2005 int rc;
2006
2007 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002008 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002009 if (rc < 0)
2010 return rc;
2011
2012 txt_rsp_len = rc;
2013 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2014
2015 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002016 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002017 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002018 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2019 void *txt_rsp_buf = cmd->buf_ptr;
2020
2021 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2022 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2023
2024 isert_cmd->pdu_buf_len = txt_rsp_len;
2025 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2026 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002027 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002028 isert_cmd->tx_desc.num_sge = 2;
2029 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002030 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002031
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002032 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002033
2034 return isert_post_response(isert_conn, isert_cmd);
2035}
2036
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002037static inline void
2038isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2039 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002040{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002041 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002042 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2043 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2044 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002045 /*
2046 * At the moment we hard code those, but if in the future
2047 * the target core would like to use it, we will take it
2048 * from se_cmd.
2049 */
2050 domain->sig.dif.apptag_check_mask = 0xffff;
2051 domain->sig.dif.app_escape = true;
2052 domain->sig.dif.ref_escape = true;
2053 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2054 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2055 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002056};
2057
2058static int
2059isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2060{
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002061 memset(sig_attrs, 0, sizeof(*sig_attrs));
2062
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002063 switch (se_cmd->prot_op) {
2064 case TARGET_PROT_DIN_INSERT:
2065 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002066 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002067 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002068 break;
2069 case TARGET_PROT_DOUT_INSERT:
2070 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002071 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002072 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002073 break;
2074 case TARGET_PROT_DIN_PASS:
2075 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002076 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2077 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002078 break;
2079 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002080 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002081 return -EINVAL;
2082 }
2083
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002084 sig_attrs->check_mask =
2085 (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2086 (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2087 (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002088 return 0;
2089}
2090
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002091static int
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002092isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
2093 struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002094{
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002095 struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
2096 enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
2097 u8 port_num = conn->cm_id->port_num;
2098 u64 addr;
2099 u32 rkey, offset;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002100 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002101
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002102 if (dir == DMA_FROM_DEVICE) {
2103 addr = cmd->write_va;
2104 rkey = cmd->write_stag;
2105 offset = cmd->iscsi_cmd->write_data_done;
2106 } else {
2107 addr = cmd->read_va;
2108 rkey = cmd->read_stag;
2109 offset = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002110 }
2111
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002112 if (isert_prot_cmd(conn, se_cmd)) {
2113 struct ib_sig_attrs sig_attrs;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002114
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002115 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002116 if (ret)
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002117 return ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002118
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002119 WARN_ON_ONCE(offset);
2120 ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
2121 se_cmd->t_data_sg, se_cmd->t_data_nents,
2122 se_cmd->t_prot_sg, se_cmd->t_prot_nents,
2123 &sig_attrs, addr, rkey, dir);
Sagi Grimberg570db172014-12-02 16:57:31 +02002124 } else {
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002125 ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
2126 se_cmd->t_data_sg, se_cmd->t_data_nents,
2127 offset, addr, rkey, dir);
2128 }
2129 if (ret < 0) {
2130 isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
2131 return ret;
Sagi Grimberg570db172014-12-02 16:57:31 +02002132 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002133
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002134 ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
2135 if (ret < 0)
2136 isert_err("Cmd: %p failed to post RDMA res\n", cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03002137 return ret;
2138}
2139
2140static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002141isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2142{
2143 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002144 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002145 struct isert_conn *isert_conn = conn->context;
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002146 struct ib_cqe *cqe = NULL;
2147 struct ib_send_wr *chain_wr = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002148 int rc;
2149
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002150 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002151 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002152
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002153 if (isert_prot_cmd(isert_conn, se_cmd)) {
2154 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
2155 cqe = &isert_cmd->tx_desc.tx_cqe;
2156 } else {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002157 /*
2158 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2159 */
2160 isert_create_send_desc(isert_conn, isert_cmd,
2161 &isert_cmd->tx_desc);
2162 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2163 &isert_cmd->tx_desc.iscsi_header);
2164 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2165 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002166 &isert_cmd->tx_desc.send_wr);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002167
2168 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2169 if (rc) {
2170 isert_err("ib_post_recv failed with %d\n", rc);
2171 return rc;
2172 }
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002173
2174 chain_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002175 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002176
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002177 isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2178 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002179 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002180}
2181
2182static int
2183isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2184{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002185 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002186
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002187 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002188 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002189
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002190 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2191 isert_rdma_rw_ctx_post(isert_cmd, conn->context,
2192 &isert_cmd->tx_desc.tx_cqe, NULL);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002193
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002194 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002195 isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002196 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002197}
2198
2199static int
2200isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2201{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002202 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2203 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002204
2205 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002206 case ISTATE_REMOVE:
2207 spin_lock_bh(&conn->cmd_lock);
2208 list_del_init(&cmd->i_conn_node);
2209 spin_unlock_bh(&conn->cmd_lock);
2210 isert_put_cmd(isert_cmd, true);
2211 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002212 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2213 ret = isert_put_nopin(cmd, conn, false);
2214 break;
2215 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002216 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002217 ret = -EINVAL;
2218 break;
2219 }
2220
2221 return ret;
2222}
2223
2224static int
2225isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2226{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002227 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002228 int ret;
2229
2230 switch (state) {
2231 case ISTATE_SEND_LOGOUTRSP:
2232 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002233 if (!ret)
2234 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002235 break;
2236 case ISTATE_SEND_NOPIN:
2237 ret = isert_put_nopin(cmd, conn, true);
2238 break;
2239 case ISTATE_SEND_TASKMGTRSP:
2240 ret = isert_put_tm_rsp(cmd, conn);
2241 break;
2242 case ISTATE_SEND_REJECT:
2243 ret = isert_put_reject(cmd, conn);
2244 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002245 case ISTATE_SEND_TEXTRSP:
2246 ret = isert_put_text_rsp(cmd, conn);
2247 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002248 case ISTATE_SEND_STATUS:
2249 /*
2250 * Special case for sending non GOOD SCSI status from TX thread
2251 * context during pre se_cmd excecution failure.
2252 */
2253 ret = isert_put_response(conn, cmd);
2254 break;
2255 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002256 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002257 ret = -EINVAL;
2258 break;
2259 }
2260
2261 return ret;
2262}
2263
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002264struct rdma_cm_id *
2265isert_setup_id(struct isert_np *isert_np)
2266{
2267 struct iscsi_np *np = isert_np->np;
2268 struct rdma_cm_id *id;
2269 struct sockaddr *sa;
2270 int ret;
2271
2272 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002273 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002274
Guy Shapirofa201052015-10-22 15:20:10 +03002275 id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002276 RDMA_PS_TCP, IB_QPT_RC);
2277 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002278 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002279 ret = PTR_ERR(id);
2280 goto out;
2281 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002282 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002283
2284 ret = rdma_bind_addr(id, sa);
2285 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002286 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002287 goto out_id;
2288 }
2289
Sagi Grimberg992607e2015-03-29 15:52:18 +03002290 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002291 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002292 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002293 goto out_id;
2294 }
2295
2296 return id;
2297out_id:
2298 rdma_destroy_id(id);
2299out:
2300 return ERR_PTR(ret);
2301}
2302
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002303static int
2304isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07002305 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002306{
2307 struct isert_np *isert_np;
2308 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002309 int ret;
2310
2311 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2312 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002313 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002314 return -ENOMEM;
2315 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002316 sema_init(&isert_np->sem, 0);
2317 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002318 INIT_LIST_HEAD(&isert_np->accepted);
2319 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002320 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002321
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002322 /*
2323 * Setup the np->np_sockaddr from the passed sockaddr setup
2324 * in iscsi_target_configfs.c code..
2325 */
2326 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07002327 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002328
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002329 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002330 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002331 ret = PTR_ERR(isert_lid);
2332 goto out;
2333 }
2334
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002335 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002336 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002337
2338 return 0;
2339
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002340out:
2341 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002342
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002343 return ret;
2344}
2345
2346static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002347isert_rdma_accept(struct isert_conn *isert_conn)
2348{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002349 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002350 struct rdma_conn_param cp;
2351 int ret;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02002352 struct iser_cm_hdr rsp_hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002353
2354 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002355 cp.initiator_depth = isert_conn->initiator_depth;
2356 cp.retry_count = 7;
2357 cp.rnr_retry_count = 7;
2358
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02002359 memset(&rsp_hdr, 0, sizeof(rsp_hdr));
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02002360 rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2361 if (!isert_conn->snd_w_inv)
2362 rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02002363 cp.private_data = (void *)&rsp_hdr;
2364 cp.private_data_len = sizeof(rsp_hdr);
2365
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002366 ret = rdma_accept(cm_id, &cp);
2367 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002368 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002369 return ret;
2370 }
2371
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002372 return 0;
2373}
2374
2375static int
2376isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2377{
Sagi Grimberg67004252015-03-29 15:52:09 +03002378 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002379 int ret;
2380
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002381 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002382 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
2383 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002384 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002385 isert_conn);
2386 return ret;
2387 }
2388 reinit_completion(&isert_conn->login_req_comp);
2389
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07002390 /*
2391 * For login requests after the first PDU, isert_rx_login_req() will
2392 * kick schedule_delayed_work(&conn->login_work) as the packet is
2393 * received, which turns this callback from iscsi_target_do_login_rx()
2394 * into a NOP.
2395 */
2396 if (!login->first_request)
2397 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002398
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002399 isert_rx_login_req(isert_conn);
2400
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002401 isert_info("before login_comp conn: %p\n", conn);
2402 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002403 if (ret)
2404 return ret;
2405
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002406 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002407
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002408 return 0;
2409}
2410
2411static void
2412isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2413 struct isert_conn *isert_conn)
2414{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002415 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002416 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002417
2418 conn->login_family = np->np_sockaddr.ss_family;
2419
Andy Groverdc58f762015-08-24 10:26:05 -07002420 conn->login_sockaddr = cm_route->addr.dst_addr;
2421 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002422}
2423
2424static int
2425isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2426{
Sagi Grimberg67004252015-03-29 15:52:09 +03002427 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002428 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03002429 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002430
2431accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002432 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03002433 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002434 return -ENODEV;
2435
2436 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03002437 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002438 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01002439 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03002440 np->np_thread_state);
2441 /**
2442 * No point in stalling here when np_thread
2443 * is in state RESET/SHUTDOWN/EXIT - bail
2444 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002445 return -ENODEV;
2446 }
2447 spin_unlock_bh(&np->np_thread_lock);
2448
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002449 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002450 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002451 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002452 goto accept_wait;
2453 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002454 isert_conn = list_first_entry(&isert_np->pending,
2455 struct isert_conn, node);
2456 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002457 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002458
2459 conn->context = isert_conn;
2460 isert_conn->conn = conn;
Jenny Derzhavetzaea92982016-02-24 19:23:59 +02002461 isert_conn->state = ISER_CONN_BOUND;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002462
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002463 isert_set_conn_info(np, conn, isert_conn);
2464
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002465 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002466
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002467 return 0;
2468}
2469
2470static void
2471isert_free_np(struct iscsi_np *np)
2472{
Sagi Grimberg67004252015-03-29 15:52:09 +03002473 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02002474 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002475
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002476 if (isert_np->cm_id)
2477 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002478
Sagi Grimberg268e6812014-12-02 16:57:36 +02002479 /*
2480 * FIXME: At this point we don't have a good way to insure
2481 * that at this point we don't have hanging connections that
2482 * completed RDMA establishment but didn't start iscsi login
2483 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002484 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02002485 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002486 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002487 if (!list_empty(&isert_np->pending)) {
2488 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02002489 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002490 &isert_np->pending,
2491 node) {
2492 isert_info("cleaning isert_conn %p state (%d)\n",
2493 isert_conn, isert_conn->state);
2494 isert_connect_release(isert_conn);
2495 }
2496 }
2497
2498 if (!list_empty(&isert_np->accepted)) {
2499 isert_info("Still have isert accepted connections\n");
2500 list_for_each_entry_safe(isert_conn, n,
2501 &isert_np->accepted,
2502 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002503 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02002504 isert_conn, isert_conn->state);
2505 isert_connect_release(isert_conn);
2506 }
2507 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002508 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02002509
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002510 np->np_context = NULL;
2511 kfree(isert_np);
2512}
2513
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002514static void isert_release_work(struct work_struct *work)
2515{
2516 struct isert_conn *isert_conn = container_of(work,
2517 struct isert_conn,
2518 release_work);
2519
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002520 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002521
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002522 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002523 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002524 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002525
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002526 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002527 isert_put_conn(isert_conn);
2528}
2529
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002530static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02002531isert_wait4logout(struct isert_conn *isert_conn)
2532{
2533 struct iscsi_conn *conn = isert_conn->conn;
2534
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002535 isert_info("conn %p\n", isert_conn);
2536
Sagi Grimberg991bb762014-12-07 13:12:01 +02002537 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002538 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002539 wait_for_completion_timeout(&conn->conn_logout_comp,
2540 SECONDS_FOR_LOGOUT_COMP * HZ);
2541 }
2542}
2543
2544static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02002545isert_wait4cmds(struct iscsi_conn *conn)
2546{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002547 isert_info("iscsi_conn %p\n", conn);
2548
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02002549 if (conn->sess) {
2550 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2551 target_wait_for_sess_cmds(conn->sess->se_sess);
2552 }
2553}
2554
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03002555/**
2556 * isert_put_unsol_pending_cmds() - Drop commands waiting for
2557 * unsolicitate dataout
2558 * @conn: iscsi connection
2559 *
2560 * We might still have commands that are waiting for unsolicited
2561 * dataouts messages. We must put the extra reference on those
2562 * before blocking on the target_wait_for_session_cmds
2563 */
2564static void
2565isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
2566{
2567 struct iscsi_cmd *cmd, *tmp;
2568 static LIST_HEAD(drop_cmd_list);
2569
2570 spin_lock_bh(&conn->cmd_lock);
2571 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
2572 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
2573 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
2574 (cmd->write_data_done < cmd->se_cmd.data_length))
2575 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
2576 }
2577 spin_unlock_bh(&conn->cmd_lock);
2578
2579 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
2580 list_del_init(&cmd->i_conn_node);
2581 if (cmd->i_state != ISTATE_REMOVE) {
2582 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2583
2584 isert_info("conn %p dropping cmd %p\n", conn, cmd);
2585 isert_put_cmd(isert_cmd, true);
2586 }
2587 }
2588}
2589
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002590static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002591{
2592 struct isert_conn *isert_conn = conn->context;
2593
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002594 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002595
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002596 mutex_lock(&isert_conn->mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02002597 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002598 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002599
Sagi Grimberg572a1432016-02-24 19:24:09 +02002600 ib_drain_qp(isert_conn->qp);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03002601 isert_put_unsol_pending_cmds(conn);
2602 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002603 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02002604
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002605 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002606}
2607
2608static void isert_free_conn(struct iscsi_conn *conn)
2609{
2610 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002611
Sagi Grimberg572a1432016-02-24 19:24:09 +02002612 ib_drain_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002613 isert_put_conn(isert_conn);
2614}
2615
Varun Prakash187e84f2016-04-20 00:00:10 +05302616static void isert_get_rx_pdu(struct iscsi_conn *conn)
2617{
2618 struct completion comp;
2619
2620 init_completion(&comp);
2621
2622 wait_for_completion_interruptible(&comp);
2623}
2624
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002625static struct iscsit_transport iser_target_transport = {
2626 .name = "IB/iSER",
2627 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07002628 .rdma_shutdown = true,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002629 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002630 .owner = THIS_MODULE,
2631 .iscsit_setup_np = isert_setup_np,
2632 .iscsit_accept_np = isert_accept_np,
2633 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002634 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002635 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002636 .iscsit_get_login_rx = isert_get_login_rx,
2637 .iscsit_put_login_tx = isert_put_login_tx,
2638 .iscsit_immediate_queue = isert_immediate_queue,
2639 .iscsit_response_queue = isert_response_queue,
2640 .iscsit_get_dataout = isert_get_dataout,
2641 .iscsit_queue_data_in = isert_put_datain,
2642 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002643 .iscsit_aborted_task = isert_aborted_task,
Varun Prakash187e84f2016-04-20 00:00:10 +05302644 .iscsit_get_rx_pdu = isert_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002645 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002646};
2647
2648static int __init isert_init(void)
2649{
2650 int ret;
2651
Sagi Grimberg631af552015-01-25 19:09:50 +02002652 isert_comp_wq = alloc_workqueue("isert_comp_wq",
2653 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002654 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002655 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002656 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002657 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002658 }
2659
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002660 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2661 WQ_UNBOUND_MAX_ACTIVE);
2662 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002663 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002664 ret = -ENOMEM;
2665 goto destroy_comp_wq;
2666 }
2667
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002668 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002669 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002670
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002671 return 0;
2672
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002673destroy_comp_wq:
2674 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002675
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002676 return ret;
2677}
2678
2679static void __exit isert_exit(void)
2680{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03002681 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002682 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002683 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002684 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002685 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002686}
2687
2688MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03002689MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002690MODULE_AUTHOR("nab@Linux-iSCSI.org");
2691MODULE_LICENSE("GPL");
2692
2693module_init(isert_init);
2694module_exit(isert_exit);