blob: 9b33c0c97468169449cbe1c9bb5a093a515a2ea3 [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)
Leon Romanovsky93b80f22016-11-03 16:44:24 +0200187 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800188
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;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200216 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800217 return -ENOMEM;
218}
219
220static void
221isert_free_rx_descriptors(struct isert_conn *isert_conn)
222{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300223 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800224 struct iser_rx_desc *rx_desc;
225 int i;
226
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300227 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800228 return;
229
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300230 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800231 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
232 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
233 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
234 }
235
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300236 kfree(isert_conn->rx_descs);
237 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800238}
239
Sagi Grimberg172369c2015-03-29 15:52:11 +0300240static void
241isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800242{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300243 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800244
Sagi Grimberg172369c2015-03-29 15:52:11 +0300245 for (i = 0; i < device->comps_used; i++) {
246 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300247
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200248 if (comp->cq)
249 ib_free_cq(comp->cq);
Vu Pham59464ef2013-08-28 23:23:35 +0300250 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300251 kfree(device->comps);
252}
Vu Phamd40945d2013-08-28 23:23:34 +0300253
Sagi Grimberg172369c2015-03-29 15:52:11 +0300254static int
Or Gerlitz4a061b22015-12-18 10:59:46 +0200255isert_alloc_comps(struct isert_device *device)
Sagi Grimberg172369c2015-03-29 15:52:11 +0300256{
257 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200258
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200259 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300260 device->ib_device->num_comp_vectors));
261
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200262 isert_info("Using %d CQs, %s supports %d vectors support "
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +0200263 "pi_capable %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200264 device->comps_used, device->ib_device->name,
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +0200265 device->ib_device->num_comp_vectors,
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200266 device->pi_capable);
267
268 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
269 GFP_KERNEL);
Leon Romanovsky93b80f22016-11-03 16:44:24 +0200270 if (!device->comps)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800271 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800272
Or Gerlitz4a061b22015-12-18 10:59:46 +0200273 max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300274
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200275 for (i = 0; i < device->comps_used; i++) {
276 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800277
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200278 comp->device = device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200279 comp->cq = ib_alloc_cq(device->ib_device, comp, max_cqe, i,
280 IB_POLL_WORKQUEUE);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200281 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300282 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200283 ret = PTR_ERR(comp->cq);
284 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800285 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800286 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800287 }
288
Sagi Grimberg172369c2015-03-29 15:52:11 +0300289 return 0;
290out_cq:
291 isert_free_comps(device);
292 return ret;
293}
294
295static int
296isert_create_device_ib_res(struct isert_device *device)
297{
Or Gerlitz4a061b22015-12-18 10:59:46 +0200298 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300299 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300300
Or Gerlitz4a061b22015-12-18 10:59:46 +0200301 isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
302 isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300303
Or Gerlitz4a061b22015-12-18 10:59:46 +0200304 ret = isert_alloc_comps(device);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300305 if (ret)
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800306 goto out;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300307
Christoph Hellwiged082d32016-09-05 12:56:17 +0200308 device->pd = ib_alloc_pd(ib_dev, 0);
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300309 if (IS_ERR(device->pd)) {
310 ret = PTR_ERR(device->pd);
311 isert_err("failed to allocate pd, device %p, ret=%d\n",
312 device, ret);
313 goto out_cq;
314 }
315
Sagi Grimberg172369c2015-03-29 15:52:11 +0300316 /* Check signature cap */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200317 device->pi_capable = ib_dev->attrs.device_cap_flags &
Sagi Grimberg172369c2015-03-29 15:52:11 +0300318 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300319
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800320 return 0;
321
322out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300323 isert_free_comps(device);
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800324out:
325 if (ret > 0)
326 ret = -EINVAL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800327 return ret;
328}
329
330static void
331isert_free_device_ib_res(struct isert_device *device)
332{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200333 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800334
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300335 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300336 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800337}
338
339static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300340isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800341{
342 mutex_lock(&device_list_mutex);
343 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200344 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800345 if (!device->refcount) {
346 isert_free_device_ib_res(device);
347 list_del(&device->dev_node);
348 kfree(device);
349 }
350 mutex_unlock(&device_list_mutex);
351}
352
353static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300354isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800355{
356 struct isert_device *device;
357 int ret;
358
359 mutex_lock(&device_list_mutex);
360 list_for_each_entry(device, &device_list, dev_node) {
361 if (device->ib_device->node_guid == cma_id->device->node_guid) {
362 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200363 isert_info("Found iser device %p refcount %d\n",
364 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800365 mutex_unlock(&device_list_mutex);
366 return device;
367 }
368 }
369
370 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
371 if (!device) {
372 mutex_unlock(&device_list_mutex);
373 return ERR_PTR(-ENOMEM);
374 }
375
376 INIT_LIST_HEAD(&device->dev_node);
377
378 device->ib_device = cma_id->device;
379 ret = isert_create_device_ib_res(device);
380 if (ret) {
381 kfree(device);
382 mutex_unlock(&device_list_mutex);
383 return ERR_PTR(ret);
384 }
385
386 device->refcount++;
387 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200388 isert_info("Created a new iser device %p refcount %d\n",
389 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800390 mutex_unlock(&device_list_mutex);
391
392 return device;
393}
394
Vu Pham59464ef2013-08-28 23:23:35 +0300395static void
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300396isert_init_conn(struct isert_conn *isert_conn)
397{
398 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300399 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300400 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300401 init_completion(&isert_conn->login_req_comp);
Raju Rangoju63b268d2016-08-29 17:15:49 +0530402 init_waitqueue_head(&isert_conn->rem_wait);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300403 kref_init(&isert_conn->kref);
404 mutex_init(&isert_conn->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300405 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300406}
407
408static void
409isert_free_login_buf(struct isert_conn *isert_conn)
410{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300411 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300412
413 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200414 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200415 kfree(isert_conn->login_rsp_buf);
416
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300417 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200418 ISER_RX_PAYLOAD_SIZE,
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300419 DMA_FROM_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200420 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300421}
422
423static int
424isert_alloc_login_buf(struct isert_conn *isert_conn,
425 struct ib_device *ib_dev)
426{
427 int ret;
428
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200429 isert_conn->login_req_buf = kzalloc(sizeof(*isert_conn->login_req_buf),
430 GFP_KERNEL);
Leon Romanovsky93b80f22016-11-03 16:44:24 +0200431 if (!isert_conn->login_req_buf)
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300432 return -ENOMEM;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300433
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300434 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200435 isert_conn->login_req_buf,
436 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300437 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
438 if (ret) {
439 isert_err("login_req_dma mapping error: %d\n", ret);
440 isert_conn->login_req_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200441 goto out_free_login_req_buf;
442 }
443
444 isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
445 if (!isert_conn->login_rsp_buf) {
Wei Yongjun1d5840c2016-08-07 12:20:38 +0000446 ret = -ENOMEM;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200447 goto out_unmap_login_req_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300448 }
449
450 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200451 isert_conn->login_rsp_buf,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200452 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300453 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
454 if (ret) {
455 isert_err("login_rsp_dma mapping error: %d\n", ret);
456 isert_conn->login_rsp_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200457 goto out_free_login_rsp_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300458 }
459
460 return 0;
461
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200462out_free_login_rsp_buf:
463 kfree(isert_conn->login_rsp_buf);
464out_unmap_login_req_buf:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300465 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200466 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
467out_free_login_req_buf:
468 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300469 return ret;
470}
471
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200472static void
473isert_set_nego_params(struct isert_conn *isert_conn,
474 struct rdma_conn_param *param)
475{
476 struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
477
478 /* Set max inflight RDMA READ requests */
479 isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
480 attr->max_qp_init_rd_atom);
481 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
482
483 if (param->private_data) {
484 u8 flags = *(u8 *)param->private_data;
485
486 /*
487 * use remote invalidation if the both initiator
488 * and the HCA support it
489 */
490 isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
491 (attr->device_cap_flags &
492 IB_DEVICE_MEM_MGT_EXTENSIONS);
493 if (isert_conn->snd_w_inv)
494 isert_info("Using remote invalidation\n");
495 }
496}
497
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800498static int
499isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
500{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200501 struct isert_np *isert_np = cma_id->context;
502 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800503 struct isert_conn *isert_conn;
504 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800505 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300506
507 spin_lock_bh(&np->np_thread_lock);
508 if (!np->enabled) {
509 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200510 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300511 return rdma_reject(cma_id, NULL, 0);
512 }
513 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800514
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200515 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800516 cma_id, cma_id->context);
517
518 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300519 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800520 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800521
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300522 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300523 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800524
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300525 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
526 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800527 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800528
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300529 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800530 if (IS_ERR(device)) {
531 ret = PTR_ERR(device);
532 goto out_rsp_dma_map;
533 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300534 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800535
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200536 isert_set_nego_params(isert_conn, &event->param.conn);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300537
Sagi Grimberg570db172014-12-02 16:57:31 +0200538 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800539 if (ret)
540 goto out_conn_dev;
541
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200542 ret = isert_login_post_recv(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200543 if (ret)
544 goto out_conn_dev;
545
546 ret = isert_rdma_accept(isert_conn);
547 if (ret)
548 goto out_conn_dev;
549
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300550 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300551 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300552 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800553
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800554 return 0;
555
556out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300557 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800558out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300559 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800560out:
561 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200562 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800563 return ret;
564}
565
566static void
567isert_connect_release(struct isert_conn *isert_conn)
568{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300569 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800570
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200571 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800572
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300573 BUG_ON(!device);
574
Sagi Grimberg19e20902014-12-02 16:57:26 +0200575 isert_free_rx_descriptors(isert_conn);
Raju Rangoju63b268d2016-08-29 17:15:49 +0530576 if (isert_conn->cm_id &&
577 !isert_conn->dev_removed)
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300578 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200579
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300580 if (isert_conn->qp) {
581 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200582
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300583 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300584 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800585 }
586
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200587 if (isert_conn->login_req_buf)
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300588 isert_free_login_buf(isert_conn);
589
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300590 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800591
Raju Rangoju63b268d2016-08-29 17:15:49 +0530592 if (isert_conn->dev_removed)
593 wake_up_interruptible(&isert_conn->rem_wait);
594 else
595 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800596}
597
598static void
599isert_connected_handler(struct rdma_cm_id *cma_id)
600{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200601 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300602 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300603
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200604 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200605
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300606 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300607 isert_conn->state = ISER_CONN_UP;
608 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300609 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300610
611 mutex_lock(&isert_np->mutex);
612 list_move_tail(&isert_conn->node, &isert_np->pending);
613 mutex_unlock(&isert_np->mutex);
614
615 isert_info("np %p: Allow accept_np to continue\n", isert_np);
616 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800617}
618
619static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300620isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800621{
622 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300623 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800624
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200625 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
626 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800627
628 isert_connect_release(isert_conn);
629}
630
631static void
632isert_put_conn(struct isert_conn *isert_conn)
633{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300634 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800635}
636
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200637static void
638isert_handle_unbound_conn(struct isert_conn *isert_conn)
639{
640 struct isert_np *isert_np = isert_conn->cm_id->context;
641
642 mutex_lock(&isert_np->mutex);
643 if (!list_empty(&isert_conn->node)) {
644 /*
645 * This means iscsi doesn't know this connection
646 * so schedule a cleanup ourselves
647 */
648 list_del_init(&isert_conn->node);
649 isert_put_conn(isert_conn);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200650 queue_work(isert_release_wq, &isert_conn->release_work);
651 }
652 mutex_unlock(&isert_np->mutex);
653}
654
Sagi Grimberg954f2372014-12-02 16:57:17 +0200655/**
656 * isert_conn_terminate() - Initiate connection termination
657 * @isert_conn: isert connection struct
658 *
659 * Notes:
Jenny Derzhavetzaea92982016-02-24 19:23:59 +0200660 * In case the connection state is BOUND, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200661 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200662 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200663 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300664 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200665 * safe to call multiple times.
666 */
667static void
668isert_conn_terminate(struct isert_conn *isert_conn)
669{
670 int err;
671
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200672 if (isert_conn->state >= ISER_CONN_TERMINATING)
673 return;
674
675 isert_info("Terminating conn %p state %d\n",
676 isert_conn, isert_conn->state);
677 isert_conn->state = ISER_CONN_TERMINATING;
678 err = rdma_disconnect(isert_conn->cm_id);
679 if (err)
680 isert_warn("Failed rdma_disconnect isert_conn %p\n",
681 isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200682}
683
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700684static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200685isert_np_cma_handler(struct isert_np *isert_np,
686 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800687{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300688 isert_dbg("%s (%d): isert np %p\n",
689 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200690
691 switch (event) {
692 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300693 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200694 break;
695 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300696 isert_np->cm_id = isert_setup_id(isert_np);
697 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200698 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300699 isert_np, PTR_ERR(isert_np->cm_id));
700 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200701 }
702 break;
703 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200704 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200705 isert_np, event);
706 }
707
708 return -1;
709}
710
711static int
712isert_disconnected_handler(struct rdma_cm_id *cma_id,
713 enum rdma_cm_event_type event)
714{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200715 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700716
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300717 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200718 switch (isert_conn->state) {
719 case ISER_CONN_TERMINATING:
720 break;
721 case ISER_CONN_UP:
722 isert_conn_terminate(isert_conn);
Sagi Grimberg572a1432016-02-24 19:24:09 +0200723 ib_drain_qp(isert_conn->qp);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200724 isert_handle_unbound_conn(isert_conn);
725 break;
726 case ISER_CONN_BOUND:
727 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
728 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
729 break;
730 default:
Colin Ian King506f71d2016-12-30 14:38:05 +0000731 isert_warn("conn %p terminating in state %d\n",
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200732 isert_conn, isert_conn->state);
733 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300734 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200735
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700736 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800737}
738
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300739static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200740isert_connect_error(struct rdma_cm_id *cma_id)
741{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200742 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200743
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300744 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300745 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200746 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300747
748 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200749}
750
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800751static int
752isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
753{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200754 struct isert_np *isert_np = cma_id->context;
Raju Rangoju63b268d2016-08-29 17:15:49 +0530755 struct isert_conn *isert_conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800756 int ret = 0;
757
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300758 isert_info("%s (%d): status %d id %p np %p\n",
759 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200760 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800761
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200762 if (isert_np->cm_id == cma_id)
763 return isert_np_cma_handler(cma_id->context, event->event);
764
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800765 switch (event->event) {
766 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800767 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700768 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200769 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800770 break;
771 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800772 isert_connected_handler(cma_id);
773 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300774 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
775 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300776 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200777 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800778 break;
Raju Rangoju63b268d2016-08-29 17:15:49 +0530779 case RDMA_CM_EVENT_DEVICE_REMOVAL:
780 isert_conn = cma_id->qp->qp_context;
781 isert_conn->dev_removed = true;
782 isert_disconnected_handler(cma_id, event->event);
783 wait_event_interruptible(isert_conn->rem_wait,
784 isert_conn->state == ISER_CONN_DOWN);
785 kfree(isert_conn);
786 /*
787 * return non-zero from the callback to destroy
788 * the rdma cm id
789 */
790 return 1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200791 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
Steve Wise1e38a362016-10-26 12:36:48 -0700792 isert_info("Connection rejected: %s\n",
793 rdma_reject_msg(cma_id, event->status));
Sagi Grimberg954f2372014-12-02 16:57:17 +0200794 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800795 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300796 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200797 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800798 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200799 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800800 break;
801 }
802
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800803 return ret;
804}
805
806static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300807isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800808{
809 struct ib_recv_wr *rx_wr, *rx_wr_failed;
810 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800811 struct iser_rx_desc *rx_desc;
812
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300813 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300814 rx_desc = &isert_conn->rx_descs[i];
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200815
816 rx_wr->wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300817 rx_wr->sg_list = &rx_desc->rx_sg;
818 rx_wr->num_sge = 1;
819 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800820 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800821 rx_wr--;
822 rx_wr->next = NULL; /* mark end of work requests list */
823
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300824 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300825 &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200826 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200827 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300828
829 return ret;
830}
831
832static int
833isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
834{
835 struct ib_recv_wr *rx_wr_failed, rx_wr;
836 int ret;
837
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200838 rx_wr.wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300839 rx_wr.sg_list = &rx_desc->rx_sg;
840 rx_wr.num_sge = 1;
841 rx_wr.next = NULL;
842
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300843 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200844 if (ret)
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300845 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300846
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800847 return ret;
848}
849
850static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200851isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800852{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300853 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800854 struct ib_send_wr send_wr, *send_wr_failed;
855 int ret;
856
857 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
858 ISER_HEADERS_LEN, DMA_TO_DEVICE);
859
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200860 tx_desc->tx_cqe.done = isert_login_send_done;
861
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800862 send_wr.next = NULL;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200863 send_wr.wr_cqe = &tx_desc->tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800864 send_wr.sg_list = tx_desc->tx_sg;
865 send_wr.num_sge = tx_desc->num_sge;
866 send_wr.opcode = IB_WR_SEND;
867 send_wr.send_flags = IB_SEND_SIGNALED;
868
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300869 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200870 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200871 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800872
873 return ret;
874}
875
876static void
877isert_create_send_desc(struct isert_conn *isert_conn,
878 struct isert_cmd *isert_cmd,
879 struct iser_tx_desc *tx_desc)
880{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300881 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300882 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800883
884 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
885 ISER_HEADERS_LEN, DMA_TO_DEVICE);
886
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +0200887 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
888 tx_desc->iser_header.flags = ISCSI_CTRL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800889
890 tx_desc->num_sge = 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800891
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600892 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
893 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200894 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800895 }
896}
897
898static int
899isert_init_tx_hdrs(struct isert_conn *isert_conn,
900 struct iser_tx_desc *tx_desc)
901{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300902 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300903 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800904 u64 dma_addr;
905
906 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
907 ISER_HEADERS_LEN, DMA_TO_DEVICE);
908 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200909 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800910 return -ENOMEM;
911 }
912
913 tx_desc->dma_addr = dma_addr;
914 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
915 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600916 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800917
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200918 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
919 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
920 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800921
922 return 0;
923}
924
925static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800926isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +0200927 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800928{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800929 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
930
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200931 tx_desc->tx_cqe.done = isert_send_done;
932 send_wr->wr_cqe = &tx_desc->tx_cqe;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200933
934 if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
935 send_wr->opcode = IB_WR_SEND_WITH_INV;
936 send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
937 } else {
938 send_wr->opcode = IB_WR_SEND;
939 }
940
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800941 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800942 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800943 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800944}
945
946static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200947isert_login_post_recv(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800948{
949 struct ib_recv_wr rx_wr, *rx_wr_fail;
950 struct ib_sge sge;
951 int ret;
952
953 memset(&sge, 0, sizeof(struct ib_sge));
954 sge.addr = isert_conn->login_req_dma;
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200955 sge.length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600956 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800957
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200958 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800959 sge.addr, sge.length, sge.lkey);
960
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200961 isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
962
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800963 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200964 rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800965 rx_wr.sg_list = &sge;
966 rx_wr.num_sge = 1;
967
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300968 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200969 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200970 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800971
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800972 return ret;
973}
974
975static int
976isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
977 u32 length)
978{
979 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300980 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300981 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300982 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800983 int ret;
984
985 isert_create_send_desc(isert_conn, NULL, tx_desc);
986
987 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
988 sizeof(struct iscsi_hdr));
989
990 isert_init_tx_hdrs(isert_conn, tx_desc);
991
992 if (length > 0) {
993 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
994
995 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
996 length, DMA_TO_DEVICE);
997
998 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
999
1000 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1001 length, DMA_TO_DEVICE);
1002
1003 tx_dsg->addr = isert_conn->login_rsp_dma;
1004 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001005 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001006 tx_desc->num_sge = 2;
1007 }
1008 if (!login->login_failed) {
1009 if (login->login_complete) {
1010 ret = isert_alloc_rx_descriptors(isert_conn);
1011 if (ret)
1012 return ret;
1013
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001014 ret = isert_post_recvm(isert_conn,
1015 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001016 if (ret)
1017 return ret;
1018
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001019 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001020 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001021 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001022 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001023 goto post_send;
1024 }
1025
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001026 ret = isert_login_post_recv(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001027 if (ret)
1028 return ret;
1029 }
1030post_send:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001031 ret = isert_login_post_send(isert_conn, tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001032 if (ret)
1033 return ret;
1034
1035 return 0;
1036}
1037
1038static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001039isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001040{
Christoph Hellwig5adabdd2016-02-24 19:24:05 +02001041 struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001042 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001043 struct iscsi_conn *conn = isert_conn->conn;
1044 struct iscsi_login *login = conn->conn_login;
1045 int size;
1046
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001047 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001048
1049 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001050
1051 if (login->first_request) {
1052 struct iscsi_login_req *login_req =
1053 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1054 /*
1055 * Setup the initial iscsi_login values from the leading
1056 * login request PDU.
1057 */
1058 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1059 login->current_stage =
1060 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1061 >> 2;
1062 login->version_min = login_req->min_version;
1063 login->version_max = login_req->max_version;
1064 memcpy(login->isid, login_req->isid, 6);
1065 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1066 login->init_task_tag = login_req->itt;
1067 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1068 login->cid = be16_to_cpu(login_req->cid);
1069 login->tsih = be16_to_cpu(login_req->tsih);
1070 }
1071
1072 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1073
1074 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001075 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1076 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1077 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001078 memcpy(login->req_buf, &rx_desc->data[0], size);
1079
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001080 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001081 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001082 return;
1083 }
1084 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001085}
1086
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001087static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001088*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001089{
Sagi Grimberg67004252015-03-29 15:52:09 +03001090 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001091 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001092 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001093
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001094 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001095 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001096 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001097 return NULL;
1098 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001099 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001100 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001101 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001102 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001103
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001104 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001105}
1106
1107static int
1108isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001109 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1110 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001111{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001112 struct iscsi_conn *conn = isert_conn->conn;
1113 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001114 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1115 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001116 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001117
1118 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1119 if (rc < 0)
1120 return rc;
1121
1122 imm_data = cmd->immediate_data;
1123 imm_data_len = cmd->first_burst_len;
1124 unsol_data = cmd->unsolicited_data;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001125 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001126
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001127 if (imm_data && imm_data_len == data_len)
1128 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001129 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1130 if (rc < 0) {
1131 return 0;
1132 } else if (rc > 0) {
1133 dump_payload = true;
1134 goto sequence_cmd;
1135 }
1136
1137 if (!imm_data)
1138 return 0;
1139
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001140 if (imm_data_len != data_len) {
1141 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1142 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1143 &rx_desc->data[0], imm_data_len);
1144 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1145 sg_nents, imm_data_len);
1146 } else {
1147 sg_init_table(&isert_cmd->sg, 1);
1148 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1149 cmd->se_cmd.t_data_nents = 1;
1150 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1151 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1152 imm_data_len);
1153 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001154
1155 cmd->write_data_done += imm_data_len;
1156
1157 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1158 spin_lock_bh(&cmd->istate_lock);
1159 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1160 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1161 spin_unlock_bh(&cmd->istate_lock);
1162 }
1163
1164sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001165 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001166
1167 if (!rc && dump_payload == false && unsol_data)
1168 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001169 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001170 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001171
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001172 return 0;
1173}
1174
1175static int
1176isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1177 struct iser_rx_desc *rx_desc, unsigned char *buf)
1178{
1179 struct scatterlist *sg_start;
1180 struct iscsi_conn *conn = isert_conn->conn;
1181 struct iscsi_cmd *cmd = NULL;
1182 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1183 u32 unsol_data_len = ntoh24(hdr->dlength);
1184 int rc, sg_nents, sg_off, page_off;
1185
1186 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1187 if (rc < 0)
1188 return rc;
1189 else if (!cmd)
1190 return 0;
1191 /*
1192 * FIXME: Unexpected unsolicited_data out
1193 */
1194 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001195 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001196 dump_stack();
1197 return -1;
1198 }
1199
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001200 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1201 "write_data_done: %u, data_length: %u\n",
1202 unsol_data_len, cmd->write_data_done,
1203 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001204
1205 sg_off = cmd->write_data_done / PAGE_SIZE;
1206 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1207 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1208 page_off = cmd->write_data_done % PAGE_SIZE;
1209 /*
1210 * FIXME: Non page-aligned unsolicited_data out
1211 */
1212 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001213 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001214 dump_stack();
1215 return -1;
1216 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001217 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1218 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1219 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001220
1221 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1222 unsol_data_len);
1223
1224 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1225 if (rc < 0)
1226 return rc;
1227
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001228 /*
1229 * multiple data-outs on the same command can arrive -
1230 * so post the buffer before hand
1231 */
1232 rc = isert_post_recv(isert_conn, rx_desc);
1233 if (rc) {
1234 isert_err("ib_post_recv failed with %d\n", rc);
1235 return rc;
1236 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001237 return 0;
1238}
1239
1240static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001241isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001242 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1243 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001244{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001245 struct iscsi_conn *conn = isert_conn->conn;
1246 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1247 int rc;
1248
1249 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1250 if (rc < 0)
1251 return rc;
1252 /*
1253 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1254 */
1255
1256 return iscsit_process_nop_out(conn, cmd, hdr);
1257}
1258
1259static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001260isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001261 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1262 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001263{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001264 struct iscsi_conn *conn = isert_conn->conn;
1265 u32 payload_length = ntoh24(hdr->dlength);
1266 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001267 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001268
1269 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1270 if (rc < 0)
1271 return rc;
1272
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001273 if (payload_length) {
1274 text_in = kzalloc(payload_length, GFP_KERNEL);
Leon Romanovsky93b80f22016-11-03 16:44:24 +02001275 if (!text_in)
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001276 return -ENOMEM;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001277 }
1278 cmd->text_in_ptr = text_in;
1279
1280 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1281
1282 return iscsit_process_text_cmd(conn, cmd, hdr);
1283}
1284
1285static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001286isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1287 uint32_t read_stag, uint64_t read_va,
1288 uint32_t write_stag, uint64_t write_va)
1289{
1290 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1291 struct iscsi_conn *conn = isert_conn->conn;
1292 struct iscsi_cmd *cmd;
1293 struct isert_cmd *isert_cmd;
1294 int ret = -EINVAL;
1295 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1296
Sagi Grimbergfb140272015-03-29 15:52:08 +03001297 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001298 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001299 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001300 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001301 return 0;
1302 }
1303
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001304 switch (opcode) {
1305 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001306 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001307 if (!cmd)
1308 break;
1309
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001310 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001311 isert_cmd->read_stag = read_stag;
1312 isert_cmd->read_va = read_va;
1313 isert_cmd->write_stag = write_stag;
1314 isert_cmd->write_va = write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001315 isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001316
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001317 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001318 rx_desc, (unsigned char *)hdr);
1319 break;
1320 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001321 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001322 if (!cmd)
1323 break;
1324
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001325 isert_cmd = iscsit_priv_cmd(cmd);
1326 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001327 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001328 break;
1329 case ISCSI_OP_SCSI_DATA_OUT:
1330 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1331 (unsigned char *)hdr);
1332 break;
1333 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001334 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001335 if (!cmd)
1336 break;
1337
1338 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1339 (unsigned char *)hdr);
1340 break;
1341 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001342 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001343 if (!cmd)
1344 break;
1345
1346 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001347 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001348 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001349 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001350 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001351 else
1352 cmd = isert_allocate_cmd(conn, rx_desc);
1353
1354 if (!cmd)
1355 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001356
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001357 isert_cmd = iscsit_priv_cmd(cmd);
1358 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001359 rx_desc, (struct iscsi_text *)hdr);
1360 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001361 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001362 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001363 dump_stack();
1364 break;
1365 }
1366
1367 return ret;
1368}
1369
1370static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001371isert_print_wc(struct ib_wc *wc, const char *type)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001372{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001373 if (wc->status != IB_WC_WR_FLUSH_ERR)
1374 isert_err("%s failure: %s (%d) vend_err %x\n", type,
1375 ib_wc_status_msg(wc->status), wc->status,
1376 wc->vendor_err);
1377 else
1378 isert_dbg("%s failure: %s (%d)\n", type,
1379 ib_wc_status_msg(wc->status), wc->status);
1380}
1381
1382static void
1383isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1384{
1385 struct isert_conn *isert_conn = wc->qp->qp_context;
1386 struct ib_device *ib_dev = isert_conn->cm_id->device;
1387 struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
1388 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001389 struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001390 uint64_t read_va = 0, write_va = 0;
1391 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001392
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001393 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1394 isert_print_wc(wc, "recv");
1395 if (wc->status != IB_WC_WR_FLUSH_ERR)
1396 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1397 return;
1398 }
1399
1400 ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
1401 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1402
1403 isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1404 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
1405 (int)(wc->byte_len - ISER_HEADERS_LEN));
1406
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001407 switch (iser_ctrl->flags & 0xF0) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001408 case ISCSI_CTRL:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001409 if (iser_ctrl->flags & ISER_RSV) {
1410 read_stag = be32_to_cpu(iser_ctrl->read_stag);
1411 read_va = be64_to_cpu(iser_ctrl->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001412 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1413 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001414 }
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001415 if (iser_ctrl->flags & ISER_WSV) {
1416 write_stag = be32_to_cpu(iser_ctrl->write_stag);
1417 write_va = be64_to_cpu(iser_ctrl->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001418 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1419 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001420 }
1421
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001422 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001423 break;
1424 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001425 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001426 break;
1427 default:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001428 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001429 break;
1430 }
1431
Bart Van Assche6c760b32015-10-22 11:14:18 -07001432 isert_rx_opcode(isert_conn, rx_desc,
1433 read_stag, read_va, write_stag, write_va);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001434
1435 ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
1436 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001437}
1438
1439static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001440isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001441{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001442 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001443 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001444
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001445 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1446 isert_print_wc(wc, "login recv");
1447 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001448 }
1449
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001450 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
1451 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001452
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001453 isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001454
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001455 if (isert_conn->conn) {
1456 struct iscsi_login *login = isert_conn->conn->conn_login;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001457
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001458 if (login && !login->first_request)
1459 isert_rx_login_req(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001460 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001461
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001462 mutex_lock(&isert_conn->mutex);
1463 complete(&isert_conn->login_req_comp);
1464 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001465
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001466 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
1467 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001468}
1469
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001470static void
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001471isert_rdma_rw_ctx_destroy(struct isert_cmd *cmd, struct isert_conn *conn)
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001472{
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001473 struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
1474 enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001475
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001476 if (!cmd->rw.nr_ops)
1477 return;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001478
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001479 if (isert_prot_cmd(conn, se_cmd)) {
1480 rdma_rw_ctx_destroy_signature(&cmd->rw, conn->qp,
1481 conn->cm_id->port_num, se_cmd->t_data_sg,
1482 se_cmd->t_data_nents, se_cmd->t_prot_sg,
1483 se_cmd->t_prot_nents, dir);
1484 } else {
1485 rdma_rw_ctx_destroy(&cmd->rw, conn->qp, conn->cm_id->port_num,
1486 se_cmd->t_data_sg, se_cmd->t_data_nents, dir);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001487 }
1488
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001489 cmd->rw.nr_ops = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03001490}
1491
1492static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001493isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001494{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001495 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001496 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001497 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001498 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001499
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001500 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001501
1502 switch (cmd->iscsi_opcode) {
1503 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001504 spin_lock_bh(&conn->cmd_lock);
1505 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001506 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001507 spin_unlock_bh(&conn->cmd_lock);
1508
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001509 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001510 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001511 /*
1512 * Check for special case during comp_err where
1513 * WRITE_PENDING has been handed off from core,
1514 * but requires an extra target_put_sess_cmd()
1515 * before transport_generic_free_cmd() below.
1516 */
1517 if (comp_err &&
1518 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1519 struct se_cmd *se_cmd = &cmd->se_cmd;
1520
Bart Van Asscheafc16602015-04-27 13:52:36 +02001521 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001522 }
1523 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001524
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001525 isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001526 transport_generic_free_cmd(&cmd->se_cmd, 0);
1527 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001528 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001529 spin_lock_bh(&conn->cmd_lock);
1530 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001531 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001532 spin_unlock_bh(&conn->cmd_lock);
1533
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001534 transport_generic_free_cmd(&cmd->se_cmd, 0);
1535 break;
1536 case ISCSI_OP_REJECT:
1537 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001538 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001539 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1540 /* If the continue bit is on, keep the command alive */
1541 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1542 break;
1543
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001544 spin_lock_bh(&conn->cmd_lock);
1545 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001546 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001547 spin_unlock_bh(&conn->cmd_lock);
1548
1549 /*
1550 * Handle special case for REJECT when iscsi_add_reject*() has
1551 * overwritten the original iscsi_opcode assignment, and the
1552 * associated cmd->se_cmd needs to be released.
1553 */
1554 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001555 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001556 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001557 transport_generic_free_cmd(&cmd->se_cmd, 0);
1558 break;
1559 }
1560 /*
1561 * Fall-through
1562 */
1563 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001564 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001565 break;
1566 }
1567}
1568
1569static void
1570isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1571{
1572 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001573 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001574 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1575 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1576 tx_desc->dma_addr = 0;
1577 }
1578}
1579
1580static void
1581isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001582 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001583{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001584 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001585 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001586 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1587 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1588 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001589 }
1590
1591 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001592 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001593}
1594
Sagi Grimberg96b79732014-03-17 12:52:18 +02001595static int
1596isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1597{
1598 struct ib_mr_status mr_status;
1599 int ret;
1600
1601 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1602 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001603 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001604 goto fail_mr_status;
1605 }
1606
1607 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1608 u64 sec_offset_err;
1609 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1610
1611 switch (mr_status.sig_err.err_type) {
1612 case IB_SIG_BAD_GUARD:
1613 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1614 break;
1615 case IB_SIG_BAD_REFTAG:
1616 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1617 break;
1618 case IB_SIG_BAD_APPTAG:
1619 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1620 break;
1621 }
1622 sec_offset_err = mr_status.sig_err.sig_err_offset;
1623 do_div(sec_offset_err, block_size);
1624 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1625
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001626 isert_err("PI error found type %d at sector 0x%llx "
1627 "expected 0x%x vs actual 0x%x\n",
1628 mr_status.sig_err.err_type,
1629 (unsigned long long)se_cmd->bad_sector,
1630 mr_status.sig_err.expected,
1631 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001632 ret = 1;
1633 }
1634
1635fail_mr_status:
1636 return ret;
1637}
1638
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001639static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001640isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001641{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001642 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001643 struct isert_device *device = isert_conn->device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001644 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001645 struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001646 struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001647 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001648
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001649 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1650 isert_print_wc(wc, "rdma write");
1651 if (wc->status != IB_WC_WR_FLUSH_ERR)
1652 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1653 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1654 return;
1655 }
1656
1657 isert_dbg("Cmd %p\n", isert_cmd);
1658
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001659 ret = isert_check_pi_status(cmd, isert_cmd->rw.sig->sig_mr);
1660 isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001661
Nicholas Bellinger555a65f2016-10-30 18:58:39 -07001662 if (ret) {
1663 /*
1664 * transport_generic_request_failure() expects to have
1665 * plus two references to handle queue-full, so re-add
1666 * one here as target-core will have already dropped
1667 * it after the first isert_put_datain() callback.
1668 */
1669 kref_get(&cmd->cmd_kref);
1670 transport_generic_request_failure(cmd, cmd->pi_err);
1671 } else {
1672 /*
1673 * XXX: isert_put_response() failure is not retried.
1674 */
1675 ret = isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
1676 if (ret)
1677 pr_warn_ratelimited("isert_put_response() ret: %d\n", ret);
1678 }
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001679}
1680
1681static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001682isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001683{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001684 struct isert_conn *isert_conn = wc->qp->qp_context;
1685 struct isert_device *device = isert_conn->device;
1686 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001687 struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001688 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001689 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001690 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001691
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001692 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1693 isert_print_wc(wc, "rdma read");
1694 if (wc->status != IB_WC_WR_FLUSH_ERR)
1695 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1696 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1697 return;
1698 }
1699
1700 isert_dbg("Cmd %p\n", isert_cmd);
1701
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001702 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001703
1704 if (isert_prot_cmd(isert_conn, se_cmd))
1705 ret = isert_check_pi_status(se_cmd, isert_cmd->rw.sig->sig_mr);
1706 isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
1707 cmd->write_data_done = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001708
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001709 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001710 spin_lock_bh(&cmd->istate_lock);
1711 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1712 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1713 spin_unlock_bh(&cmd->istate_lock);
1714
Nicholas Bellinger555a65f2016-10-30 18:58:39 -07001715 /*
1716 * transport_generic_request_failure() will drop the extra
1717 * se_cmd->cmd_kref reference after T10-PI error, and handle
1718 * any non-zero ->queue_status() callback error retries.
1719 */
1720 if (ret)
1721 transport_generic_request_failure(se_cmd, se_cmd->pi_err);
1722 else
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001723 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001724}
1725
1726static void
1727isert_do_control_comp(struct work_struct *work)
1728{
1729 struct isert_cmd *isert_cmd = container_of(work,
1730 struct isert_cmd, comp_work);
1731 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001732 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001733 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001734
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001735 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1736
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001737 switch (cmd->i_state) {
1738 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001739 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001740 case ISTATE_SEND_REJECT: /* FALLTHRU */
1741 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001742 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001743 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1744 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001745 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001746 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001747 iscsit_logout_post_handler(cmd, cmd->conn);
1748 break;
1749 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001750 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001751 dump_stack();
1752 break;
1753 }
1754}
1755
1756static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001757isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001758{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001759 struct isert_conn *isert_conn = wc->qp->qp_context;
1760 struct ib_device *ib_dev = isert_conn->cm_id->device;
1761 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001762
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001763 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1764 isert_print_wc(wc, "login send");
1765 if (wc->status != IB_WC_WR_FLUSH_ERR)
1766 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1767 }
1768
1769 isert_unmap_tx_desc(tx_desc, ib_dev);
1770}
1771
1772static void
1773isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
1774{
1775 struct isert_conn *isert_conn = wc->qp->qp_context;
1776 struct ib_device *ib_dev = isert_conn->cm_id->device;
1777 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001778 struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001779
1780 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1781 isert_print_wc(wc, "send");
1782 if (wc->status != IB_WC_WR_FLUSH_ERR)
1783 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1784 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
1785 return;
1786 }
1787
1788 isert_dbg("Cmd %p\n", isert_cmd);
1789
1790 switch (isert_cmd->iscsi_cmd->i_state) {
1791 case ISTATE_SEND_TASKMGTRSP:
1792 case ISTATE_SEND_LOGOUTRSP:
1793 case ISTATE_SEND_REJECT:
1794 case ISTATE_SEND_TEXTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001795 isert_unmap_tx_desc(tx_desc, ib_dev);
1796
1797 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1798 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1799 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001800 default:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001801 isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
1802 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001803 break;
1804 }
1805}
1806
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001807static int
1808isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1809{
1810 struct ib_send_wr *wr_failed;
1811 int ret;
1812
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001813 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
1814 if (ret) {
1815 isert_err("ib_post_recv failed with %d\n", ret);
1816 return ret;
1817 }
1818
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001819 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001820 &wr_failed);
1821 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001822 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001823 return ret;
1824 }
1825 return ret;
1826}
1827
1828static int
1829isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1830{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001831 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001832 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001833 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1834 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1835 &isert_cmd->tx_desc.iscsi_header;
1836
1837 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1838 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1839 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1840 /*
1841 * Attach SENSE DATA payload to iSCSI Response PDU
1842 */
1843 if (cmd->se_cmd.sense_buffer &&
1844 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1845 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001846 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001847 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001848 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001849 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001850
1851 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1852 cmd->sense_buffer);
1853 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1854
1855 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1856 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001857 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001858
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001859 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1860 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001861 DMA_TO_DEVICE);
Alexey Khoroshilovdef4a6f2016-10-22 01:01:21 +03001862 if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1863 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001864
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001865 isert_cmd->pdu_buf_len = pdu_len;
1866 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1867 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001868 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001869 isert_cmd->tx_desc.num_sge = 2;
1870 }
1871
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001872 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001873
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001874 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001875
1876 return isert_post_response(isert_conn, isert_cmd);
1877}
1878
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001879static void
1880isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1881{
1882 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001883 struct isert_conn *isert_conn = conn->context;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001884
1885 spin_lock_bh(&conn->cmd_lock);
1886 if (!list_empty(&cmd->i_conn_node))
1887 list_del_init(&cmd->i_conn_node);
1888 spin_unlock_bh(&conn->cmd_lock);
1889
1890 if (cmd->data_direction == DMA_TO_DEVICE)
1891 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02001892 isert_rdma_rw_ctx_destroy(isert_cmd, isert_conn);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001893}
1894
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001895static enum target_prot_op
1896isert_get_sup_prot_ops(struct iscsi_conn *conn)
1897{
Sagi Grimberg67004252015-03-29 15:52:09 +03001898 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001899 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001900
Sagi Grimberg23a548e2014-12-02 16:57:35 +02001901 if (conn->tpg->tpg_attrib.t10_pi) {
1902 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001903 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02001904 isert_conn->pi_support = true;
1905 return TARGET_PROT_ALL;
1906 }
1907 }
1908
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001909 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02001910 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001911
1912 return TARGET_PROT_NORMAL;
1913}
1914
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001915static int
1916isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1917 bool nopout_response)
1918{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001919 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001920 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001921 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1922
1923 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1924 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1925 &isert_cmd->tx_desc.iscsi_header,
1926 nopout_response);
1927 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001928 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001929
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001930 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001931
1932 return isert_post_response(isert_conn, isert_cmd);
1933}
1934
1935static int
1936isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1937{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001938 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001939 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001940 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1941
1942 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1943 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1944 &isert_cmd->tx_desc.iscsi_header);
1945 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001946 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001947
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001948 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001949
1950 return isert_post_response(isert_conn, isert_cmd);
1951}
1952
1953static int
1954isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1955{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001956 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001957 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001958 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1959
1960 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1961 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1962 &isert_cmd->tx_desc.iscsi_header);
1963 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001964 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001965
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001966 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001967
1968 return isert_post_response(isert_conn, isert_cmd);
1969}
1970
1971static int
1972isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1973{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001974 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03001975 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001976 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001977 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001978 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001979 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1980 struct iscsi_reject *hdr =
1981 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001982
1983 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001984 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001985 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001986
1987 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001988 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001989 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1990 DMA_TO_DEVICE);
Alexey Khoroshilovdef4a6f2016-10-22 01:01:21 +03001991 if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
1992 return -ENOMEM;
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001993 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1994 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001995 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001996 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001997 isert_cmd->tx_desc.num_sge = 2;
1998
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001999 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002000
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002001 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002002
2003 return isert_post_response(isert_conn, isert_cmd);
2004}
2005
2006static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002007isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2008{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002009 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002010 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002011 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2012 struct iscsi_text_rsp *hdr =
2013 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2014 u32 txt_rsp_len;
2015 int rc;
2016
2017 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002018 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002019 if (rc < 0)
2020 return rc;
2021
2022 txt_rsp_len = rc;
2023 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2024
2025 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002026 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002027 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002028 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2029 void *txt_rsp_buf = cmd->buf_ptr;
2030
2031 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2032 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
Alexey Khoroshilovdef4a6f2016-10-22 01:01:21 +03002033 if (ib_dma_mapping_error(ib_dev, isert_cmd->pdu_buf_dma))
2034 return -ENOMEM;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002035
2036 isert_cmd->pdu_buf_len = txt_rsp_len;
2037 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2038 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002039 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002040 isert_cmd->tx_desc.num_sge = 2;
2041 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002042 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002043
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002044 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002045
2046 return isert_post_response(isert_conn, isert_cmd);
2047}
2048
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002049static inline void
2050isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2051 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002052{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002053 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002054 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2055 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2056 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002057 /*
2058 * At the moment we hard code those, but if in the future
2059 * the target core would like to use it, we will take it
2060 * from se_cmd.
2061 */
2062 domain->sig.dif.apptag_check_mask = 0xffff;
2063 domain->sig.dif.app_escape = true;
2064 domain->sig.dif.ref_escape = true;
2065 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2066 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2067 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002068};
2069
2070static int
2071isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2072{
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002073 memset(sig_attrs, 0, sizeof(*sig_attrs));
2074
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002075 switch (se_cmd->prot_op) {
2076 case TARGET_PROT_DIN_INSERT:
2077 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002078 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002079 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002080 break;
2081 case TARGET_PROT_DOUT_INSERT:
2082 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002083 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002084 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002085 break;
2086 case TARGET_PROT_DIN_PASS:
2087 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002088 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2089 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002090 break;
2091 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002092 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002093 return -EINVAL;
2094 }
2095
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002096 sig_attrs->check_mask =
2097 (se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2098 (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2099 (se_cmd->prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002100 return 0;
2101}
2102
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002103static int
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002104isert_rdma_rw_ctx_post(struct isert_cmd *cmd, struct isert_conn *conn,
2105 struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002106{
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002107 struct se_cmd *se_cmd = &cmd->iscsi_cmd->se_cmd;
2108 enum dma_data_direction dir = target_reverse_dma_direction(se_cmd);
2109 u8 port_num = conn->cm_id->port_num;
2110 u64 addr;
2111 u32 rkey, offset;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002112 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002113
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002114 if (dir == DMA_FROM_DEVICE) {
2115 addr = cmd->write_va;
2116 rkey = cmd->write_stag;
2117 offset = cmd->iscsi_cmd->write_data_done;
2118 } else {
2119 addr = cmd->read_va;
2120 rkey = cmd->read_stag;
2121 offset = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002122 }
2123
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002124 if (isert_prot_cmd(conn, se_cmd)) {
2125 struct ib_sig_attrs sig_attrs;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002126
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002127 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002128 if (ret)
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002129 return ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002130
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002131 WARN_ON_ONCE(offset);
2132 ret = rdma_rw_ctx_signature_init(&cmd->rw, conn->qp, port_num,
2133 se_cmd->t_data_sg, se_cmd->t_data_nents,
2134 se_cmd->t_prot_sg, se_cmd->t_prot_nents,
2135 &sig_attrs, addr, rkey, dir);
Sagi Grimberg570db172014-12-02 16:57:31 +02002136 } else {
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002137 ret = rdma_rw_ctx_init(&cmd->rw, conn->qp, port_num,
2138 se_cmd->t_data_sg, se_cmd->t_data_nents,
2139 offset, addr, rkey, dir);
2140 }
2141 if (ret < 0) {
2142 isert_err("Cmd: %p failed to prepare RDMA res\n", cmd);
2143 return ret;
Sagi Grimberg570db172014-12-02 16:57:31 +02002144 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002145
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002146 ret = rdma_rw_ctx_post(&cmd->rw, conn->qp, port_num, cqe, chain_wr);
2147 if (ret < 0)
2148 isert_err("Cmd: %p failed to post RDMA res\n", cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03002149 return ret;
2150}
2151
2152static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002153isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2154{
2155 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002156 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002157 struct isert_conn *isert_conn = conn->context;
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002158 struct ib_cqe *cqe = NULL;
2159 struct ib_send_wr *chain_wr = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002160 int rc;
2161
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002162 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002163 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002164
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002165 if (isert_prot_cmd(isert_conn, se_cmd)) {
2166 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
2167 cqe = &isert_cmd->tx_desc.tx_cqe;
2168 } else {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002169 /*
2170 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2171 */
2172 isert_create_send_desc(isert_conn, isert_cmd,
2173 &isert_cmd->tx_desc);
2174 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2175 &isert_cmd->tx_desc.iscsi_header);
2176 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2177 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002178 &isert_cmd->tx_desc.send_wr);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002179
2180 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2181 if (rc) {
2182 isert_err("ib_post_recv failed with %d\n", rc);
2183 return rc;
2184 }
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002185
2186 chain_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002187 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002188
Nicholas Bellinger555a65f2016-10-30 18:58:39 -07002189 rc = isert_rdma_rw_ctx_post(isert_cmd, isert_conn, cqe, chain_wr);
2190 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ rc: %d\n",
2191 isert_cmd, rc);
2192 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002193}
2194
2195static int
2196isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2197{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002198 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellinger555a65f2016-10-30 18:58:39 -07002199 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002200
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002201 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002202 isert_cmd, cmd->se_cmd.data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002203
Christoph Hellwig38a2d0d42016-05-03 18:01:13 +02002204 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
Nicholas Bellinger555a65f2016-10-30 18:58:39 -07002205 ret = isert_rdma_rw_ctx_post(isert_cmd, conn->context,
2206 &isert_cmd->tx_desc.tx_cqe, NULL);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002207
Nicholas Bellinger555a65f2016-10-30 18:58:39 -07002208 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE rc: %d\n",
2209 isert_cmd, ret);
2210 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002211}
2212
2213static int
2214isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2215{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002216 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2217 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002218
2219 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002220 case ISTATE_REMOVE:
2221 spin_lock_bh(&conn->cmd_lock);
2222 list_del_init(&cmd->i_conn_node);
2223 spin_unlock_bh(&conn->cmd_lock);
2224 isert_put_cmd(isert_cmd, true);
2225 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002226 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2227 ret = isert_put_nopin(cmd, conn, false);
2228 break;
2229 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002230 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002231 ret = -EINVAL;
2232 break;
2233 }
2234
2235 return ret;
2236}
2237
2238static int
2239isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2240{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002241 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002242 int ret;
2243
2244 switch (state) {
2245 case ISTATE_SEND_LOGOUTRSP:
2246 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002247 if (!ret)
2248 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002249 break;
2250 case ISTATE_SEND_NOPIN:
2251 ret = isert_put_nopin(cmd, conn, true);
2252 break;
2253 case ISTATE_SEND_TASKMGTRSP:
2254 ret = isert_put_tm_rsp(cmd, conn);
2255 break;
2256 case ISTATE_SEND_REJECT:
2257 ret = isert_put_reject(cmd, conn);
2258 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002259 case ISTATE_SEND_TEXTRSP:
2260 ret = isert_put_text_rsp(cmd, conn);
2261 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002262 case ISTATE_SEND_STATUS:
2263 /*
2264 * Special case for sending non GOOD SCSI status from TX thread
2265 * context during pre se_cmd excecution failure.
2266 */
2267 ret = isert_put_response(conn, cmd);
2268 break;
2269 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002270 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002271 ret = -EINVAL;
2272 break;
2273 }
2274
2275 return ret;
2276}
2277
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002278struct rdma_cm_id *
2279isert_setup_id(struct isert_np *isert_np)
2280{
2281 struct iscsi_np *np = isert_np->np;
2282 struct rdma_cm_id *id;
2283 struct sockaddr *sa;
2284 int ret;
2285
2286 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002287 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002288
Guy Shapirofa201052015-10-22 15:20:10 +03002289 id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002290 RDMA_PS_TCP, IB_QPT_RC);
2291 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002292 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002293 ret = PTR_ERR(id);
2294 goto out;
2295 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002296 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002297
2298 ret = rdma_bind_addr(id, sa);
2299 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002300 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002301 goto out_id;
2302 }
2303
Sagi Grimberg992607e2015-03-29 15:52:18 +03002304 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002305 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002306 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002307 goto out_id;
2308 }
2309
2310 return id;
2311out_id:
2312 rdma_destroy_id(id);
2313out:
2314 return ERR_PTR(ret);
2315}
2316
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002317static int
2318isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07002319 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002320{
2321 struct isert_np *isert_np;
2322 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002323 int ret;
2324
2325 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
Leon Romanovsky93b80f22016-11-03 16:44:24 +02002326 if (!isert_np)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002327 return -ENOMEM;
Leon Romanovsky93b80f22016-11-03 16:44:24 +02002328
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002329 sema_init(&isert_np->sem, 0);
2330 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002331 INIT_LIST_HEAD(&isert_np->accepted);
2332 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002333 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002334
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002335 /*
2336 * Setup the np->np_sockaddr from the passed sockaddr setup
2337 * in iscsi_target_configfs.c code..
2338 */
2339 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07002340 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002341
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002342 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002343 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002344 ret = PTR_ERR(isert_lid);
2345 goto out;
2346 }
2347
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002348 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002350
2351 return 0;
2352
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002353out:
2354 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002355
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002356 return ret;
2357}
2358
2359static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002360isert_rdma_accept(struct isert_conn *isert_conn)
2361{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002362 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002363 struct rdma_conn_param cp;
2364 int ret;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02002365 struct iser_cm_hdr rsp_hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002366
2367 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002368 cp.initiator_depth = isert_conn->initiator_depth;
2369 cp.retry_count = 7;
2370 cp.rnr_retry_count = 7;
2371
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02002372 memset(&rsp_hdr, 0, sizeof(rsp_hdr));
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02002373 rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
2374 if (!isert_conn->snd_w_inv)
2375 rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02002376 cp.private_data = (void *)&rsp_hdr;
2377 cp.private_data_len = sizeof(rsp_hdr);
2378
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002379 ret = rdma_accept(cm_id, &cp);
2380 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002381 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002382 return ret;
2383 }
2384
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002385 return 0;
2386}
2387
2388static int
2389isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2390{
Sagi Grimberg67004252015-03-29 15:52:09 +03002391 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002392 int ret;
2393
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002394 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002395 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
2396 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002397 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002398 isert_conn);
2399 return ret;
2400 }
2401 reinit_completion(&isert_conn->login_req_comp);
2402
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07002403 /*
2404 * For login requests after the first PDU, isert_rx_login_req() will
2405 * kick schedule_delayed_work(&conn->login_work) as the packet is
2406 * received, which turns this callback from iscsi_target_do_login_rx()
2407 * into a NOP.
2408 */
2409 if (!login->first_request)
2410 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002411
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002412 isert_rx_login_req(isert_conn);
2413
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002414 isert_info("before login_comp conn: %p\n", conn);
2415 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002416 if (ret)
2417 return ret;
2418
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002419 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002420
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002421 return 0;
2422}
2423
2424static void
2425isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2426 struct isert_conn *isert_conn)
2427{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002428 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002429 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002430
2431 conn->login_family = np->np_sockaddr.ss_family;
2432
Andy Groverdc58f762015-08-24 10:26:05 -07002433 conn->login_sockaddr = cm_route->addr.dst_addr;
2434 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002435}
2436
2437static int
2438isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2439{
Sagi Grimberg67004252015-03-29 15:52:09 +03002440 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002441 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03002442 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002443
2444accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002445 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03002446 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002447 return -ENODEV;
2448
2449 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03002450 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002451 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01002452 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03002453 np->np_thread_state);
2454 /**
2455 * No point in stalling here when np_thread
2456 * is in state RESET/SHUTDOWN/EXIT - bail
2457 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002458 return -ENODEV;
2459 }
2460 spin_unlock_bh(&np->np_thread_lock);
2461
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002462 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002463 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002464 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002465 goto accept_wait;
2466 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002467 isert_conn = list_first_entry(&isert_np->pending,
2468 struct isert_conn, node);
2469 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002470 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002471
2472 conn->context = isert_conn;
2473 isert_conn->conn = conn;
Jenny Derzhavetzaea92982016-02-24 19:23:59 +02002474 isert_conn->state = ISER_CONN_BOUND;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002475
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002476 isert_set_conn_info(np, conn, isert_conn);
2477
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002478 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02002479
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002480 return 0;
2481}
2482
2483static void
2484isert_free_np(struct iscsi_np *np)
2485{
Sagi Grimberg67004252015-03-29 15:52:09 +03002486 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02002487 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002488
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002489 if (isert_np->cm_id)
2490 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002491
Sagi Grimberg268e6812014-12-02 16:57:36 +02002492 /*
2493 * FIXME: At this point we don't have a good way to insure
2494 * that at this point we don't have hanging connections that
2495 * completed RDMA establishment but didn't start iscsi login
2496 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002497 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02002498 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002499 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002500 if (!list_empty(&isert_np->pending)) {
2501 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02002502 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002503 &isert_np->pending,
2504 node) {
2505 isert_info("cleaning isert_conn %p state (%d)\n",
2506 isert_conn, isert_conn->state);
2507 isert_connect_release(isert_conn);
2508 }
2509 }
2510
2511 if (!list_empty(&isert_np->accepted)) {
2512 isert_info("Still have isert accepted connections\n");
2513 list_for_each_entry_safe(isert_conn, n,
2514 &isert_np->accepted,
2515 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002516 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02002517 isert_conn, isert_conn->state);
2518 isert_connect_release(isert_conn);
2519 }
2520 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002521 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02002522
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002523 np->np_context = NULL;
2524 kfree(isert_np);
2525}
2526
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002527static void isert_release_work(struct work_struct *work)
2528{
2529 struct isert_conn *isert_conn = container_of(work,
2530 struct isert_conn,
2531 release_work);
2532
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002533 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002534
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002535 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002536 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002537 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002538
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002539 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002540 isert_put_conn(isert_conn);
2541}
2542
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002543static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02002544isert_wait4logout(struct isert_conn *isert_conn)
2545{
2546 struct iscsi_conn *conn = isert_conn->conn;
2547
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002548 isert_info("conn %p\n", isert_conn);
2549
Sagi Grimberg991bb762014-12-07 13:12:01 +02002550 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002551 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002552 wait_for_completion_timeout(&conn->conn_logout_comp,
2553 SECONDS_FOR_LOGOUT_COMP * HZ);
2554 }
2555}
2556
2557static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02002558isert_wait4cmds(struct iscsi_conn *conn)
2559{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002560 isert_info("iscsi_conn %p\n", conn);
2561
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02002562 if (conn->sess) {
2563 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2564 target_wait_for_sess_cmds(conn->sess->se_sess);
2565 }
2566}
2567
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03002568/**
2569 * isert_put_unsol_pending_cmds() - Drop commands waiting for
2570 * unsolicitate dataout
2571 * @conn: iscsi connection
2572 *
2573 * We might still have commands that are waiting for unsolicited
2574 * dataouts messages. We must put the extra reference on those
2575 * before blocking on the target_wait_for_session_cmds
2576 */
2577static void
2578isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
2579{
2580 struct iscsi_cmd *cmd, *tmp;
2581 static LIST_HEAD(drop_cmd_list);
2582
2583 spin_lock_bh(&conn->cmd_lock);
2584 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
2585 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
2586 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
2587 (cmd->write_data_done < cmd->se_cmd.data_length))
2588 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
2589 }
2590 spin_unlock_bh(&conn->cmd_lock);
2591
2592 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
2593 list_del_init(&cmd->i_conn_node);
2594 if (cmd->i_state != ISTATE_REMOVE) {
2595 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2596
2597 isert_info("conn %p dropping cmd %p\n", conn, cmd);
2598 isert_put_cmd(isert_cmd, true);
2599 }
2600 }
2601}
2602
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002603static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002604{
2605 struct isert_conn *isert_conn = conn->context;
2606
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002607 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002608
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002609 mutex_lock(&isert_conn->mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02002610 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002611 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002612
Sagi Grimberg572a1432016-02-24 19:24:09 +02002613 ib_drain_qp(isert_conn->qp);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03002614 isert_put_unsol_pending_cmds(conn);
2615 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002616 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02002617
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002618 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002619}
2620
2621static void isert_free_conn(struct iscsi_conn *conn)
2622{
2623 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002624
Sagi Grimberg572a1432016-02-24 19:24:09 +02002625 ib_drain_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002626 isert_put_conn(isert_conn);
2627}
2628
Varun Prakash187e84f2016-04-20 00:00:10 +05302629static void isert_get_rx_pdu(struct iscsi_conn *conn)
2630{
2631 struct completion comp;
2632
2633 init_completion(&comp);
2634
2635 wait_for_completion_interruptible(&comp);
2636}
2637
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002638static struct iscsit_transport iser_target_transport = {
2639 .name = "IB/iSER",
2640 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07002641 .rdma_shutdown = true,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002642 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002643 .owner = THIS_MODULE,
2644 .iscsit_setup_np = isert_setup_np,
2645 .iscsit_accept_np = isert_accept_np,
2646 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002647 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002648 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002649 .iscsit_get_login_rx = isert_get_login_rx,
2650 .iscsit_put_login_tx = isert_put_login_tx,
2651 .iscsit_immediate_queue = isert_immediate_queue,
2652 .iscsit_response_queue = isert_response_queue,
2653 .iscsit_get_dataout = isert_get_dataout,
2654 .iscsit_queue_data_in = isert_put_datain,
2655 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002656 .iscsit_aborted_task = isert_aborted_task,
Varun Prakash187e84f2016-04-20 00:00:10 +05302657 .iscsit_get_rx_pdu = isert_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002658 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002659};
2660
2661static int __init isert_init(void)
2662{
2663 int ret;
2664
Sagi Grimberg631af552015-01-25 19:09:50 +02002665 isert_comp_wq = alloc_workqueue("isert_comp_wq",
2666 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002667 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002668 isert_err("Unable to allocate isert_comp_wq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002669 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002670 }
2671
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002672 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
2673 WQ_UNBOUND_MAX_ACTIVE);
2674 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002675 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002676 ret = -ENOMEM;
2677 goto destroy_comp_wq;
2678 }
2679
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002680 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002681 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002682
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002683 return 0;
2684
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002685destroy_comp_wq:
2686 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002687
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002688 return ret;
2689}
2690
2691static void __exit isert_exit(void)
2692{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03002693 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02002694 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002695 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002696 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002697 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002698}
2699
2700MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03002701MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002702MODULE_AUTHOR("nab@Linux-iSCSI.org");
2703MODULE_LICENSE("GPL");
2704
2705module_init(isert_init);
2706module_exit(isert_exit);