blob: eb32ba5354349bfe9401ebceffba48f0127507d3 [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2013 Datera, Inc.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08005 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19#include <linux/string.h>
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/socket.h>
23#include <linux/in.h>
24#include <linux/in6.h>
25#include <rdma/ib_verbs.h>
26#include <rdma/rdma_cm.h>
27#include <target/target_core_base.h>
28#include <target/target_core_fabric.h>
29#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg531b7bf2014-04-29 13:13:45 +030030#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080031
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080032#include "ib_isert.h"
33
34#define ISERT_MAX_CONN 8
35#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
36#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020037#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
38 ISERT_MAX_CONN)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080039
Sagi Grimberg45678b62015-01-25 19:11:19 +020040static int isert_debug_level;
Sagi Grimberg24f412d2014-12-07 13:12:02 +020041module_param_named(debug_level, isert_debug_level, int, 0644);
42MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
43
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080044static DEFINE_MUTEX(device_list_mutex);
45static LIST_HEAD(device_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080046static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +020047static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080048
49static void
Vu Phamd40945d2013-08-28 23:23:34 +030050isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
51static int
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +020052isert_map_rdma(struct isert_cmd *isert_cmd, struct iscsi_conn *conn);
Vu Pham59464ef2013-08-28 23:23:35 +030053static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020054isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030055static int
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +020056isert_reg_rdma(struct isert_cmd *isert_cmd, struct iscsi_conn *conn);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020057static int
58isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020059static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +020060isert_login_post_recv(struct isert_conn *isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020061static int
62isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020063struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030064
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030065static void isert_release_work(struct work_struct *work);
Christoph Hellwig9679cc52016-02-24 19:24:06 +020066static void isert_recv_done(struct ib_cq *cq, struct ib_wc *wc);
67static void isert_send_done(struct ib_cq *cq, struct ib_wc *wc);
68static void isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc);
69static void isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030070
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020071static inline bool
72isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
73{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020074 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020075 cmd->prot_op != TARGET_PROT_NORMAL);
76}
77
78
Vu Phamd40945d2013-08-28 23:23:34 +030079static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080080isert_qp_event_callback(struct ib_event *e, void *context)
81{
Sagi Grimberg67004252015-03-29 15:52:09 +030082 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080083
Sagi Grimbergea8a1612015-05-18 13:40:31 +030084 isert_err("%s (%d): conn %p\n",
85 ib_event_msg(e->event), e->event, isert_conn);
86
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080087 switch (e->event) {
88 case IB_EVENT_COMM_EST:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030089 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080090 break;
91 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020092 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080093 break;
94 default:
95 break;
96 }
97}
98
Sagi Grimberg40fc0692015-03-29 15:52:10 +030099static struct isert_comp *
100isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800101{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300102 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200103 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300104 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800105
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800106 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200107 for (i = 0; i < device->comps_used; i++)
108 if (device->comps[i].active_qps <
109 device->comps[min].active_qps)
110 min = i;
111 comp = &device->comps[min];
112 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300113 mutex_unlock(&device_list_mutex);
114
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200115 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200116 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300117
118 return comp;
119}
120
121static void
122isert_comp_put(struct isert_comp *comp)
123{
124 mutex_lock(&device_list_mutex);
125 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800126 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300127}
128
129static struct ib_qp *
130isert_create_qp(struct isert_conn *isert_conn,
131 struct isert_comp *comp,
132 struct rdma_cm_id *cma_id)
133{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300134 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300135 struct ib_qp_init_attr attr;
136 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800137
138 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
139 attr.event_handler = isert_qp_event_callback;
140 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200141 attr.send_cq = comp->cq;
142 attr.recv_cq = comp->cq;
Sagi Grimberg572a1432016-02-24 19:24:09 +0200143 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS + 1;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200144 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Or Gerlitz4a061b22015-12-18 10:59:46 +0200145 attr.cap.max_send_sge = device->ib_device->attrs.max_sge;
146 isert_conn->max_sge = min(device->ib_device->attrs.max_sge,
147 device->ib_device->attrs.max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800148 attr.cap.max_recv_sge = 1;
149 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
150 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200151 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200152 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800153
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300154 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800155 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200156 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300157 return ERR_PTR(ret);
158 }
159
160 return cma_id->qp;
161}
162
163static int
164isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
165{
166 struct isert_comp *comp;
167 int ret;
168
169 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300170 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
171 if (IS_ERR(isert_conn->qp)) {
172 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200173 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800174 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800175
176 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200177err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300178 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200179 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800180}
181
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800182static int
183isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
184{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300185 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300186 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800187 struct iser_rx_desc *rx_desc;
188 struct ib_sge *rx_sg;
189 u64 dma_addr;
190 int i, j;
191
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300192 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800193 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300194 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800195 goto fail;
196
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300197 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800198
199 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
200 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
201 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
202 if (ib_dma_mapping_error(ib_dev, dma_addr))
203 goto dma_map_fail;
204
205 rx_desc->dma_addr = dma_addr;
206
207 rx_sg = &rx_desc->rx_sg;
208 rx_sg->addr = rx_desc->dma_addr;
209 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600210 rx_sg->lkey = device->pd->local_dma_lkey;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200211 rx_desc->rx_cqe.done = isert_recv_done;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800212 }
213
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800214 return 0;
215
216dma_map_fail:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300217 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800218 for (j = 0; j < i; j++, rx_desc++) {
219 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
220 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
221 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300222 kfree(isert_conn->rx_descs);
223 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800224fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200225 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
226
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800227 return -ENOMEM;
228}
229
230static void
231isert_free_rx_descriptors(struct isert_conn *isert_conn)
232{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300233 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800234 struct iser_rx_desc *rx_desc;
235 int i;
236
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300237 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800238 return;
239
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300240 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800241 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
242 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
243 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
244 }
245
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300246 kfree(isert_conn->rx_descs);
247 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800248}
249
Sagi Grimberg172369c2015-03-29 15:52:11 +0300250static void
251isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800252{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300253 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800254
Sagi Grimberg172369c2015-03-29 15:52:11 +0300255 for (i = 0; i < device->comps_used; i++) {
256 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300257
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200258 if (comp->cq)
259 ib_free_cq(comp->cq);
Vu Pham59464ef2013-08-28 23:23:35 +0300260 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300261 kfree(device->comps);
262}
Vu Phamd40945d2013-08-28 23:23:34 +0300263
Sagi Grimberg172369c2015-03-29 15:52:11 +0300264static int
Or Gerlitz4a061b22015-12-18 10:59:46 +0200265isert_alloc_comps(struct isert_device *device)
Sagi Grimberg172369c2015-03-29 15:52:11 +0300266{
267 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200268
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200269 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300270 device->ib_device->num_comp_vectors));
271
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200272 isert_info("Using %d CQs, %s supports %d vectors support "
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200273 "Fast registration %d pi_capable %d\n",
274 device->comps_used, device->ib_device->name,
275 device->ib_device->num_comp_vectors, device->use_fastreg,
276 device->pi_capable);
277
278 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
279 GFP_KERNEL);
280 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200281 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800282 return -ENOMEM;
283 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800284
Or Gerlitz4a061b22015-12-18 10:59:46 +0200285 max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300286
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200287 for (i = 0; i < device->comps_used; i++) {
288 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800289
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200290 comp->device = device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200291 comp->cq = ib_alloc_cq(device->ib_device, comp, max_cqe, i,
292 IB_POLL_WORKQUEUE);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200293 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300294 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200295 ret = PTR_ERR(comp->cq);
296 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800297 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800298 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800299 }
300
Sagi Grimberg172369c2015-03-29 15:52:11 +0300301 return 0;
302out_cq:
303 isert_free_comps(device);
304 return ret;
305}
306
307static int
308isert_create_device_ib_res(struct isert_device *device)
309{
Or Gerlitz4a061b22015-12-18 10:59:46 +0200310 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300311 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300312
Or Gerlitz4a061b22015-12-18 10:59:46 +0200313 isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
314 isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300315
316 /* asign function handlers */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200317 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
318 ib_dev->attrs.device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300319 device->use_fastreg = 1;
320 device->reg_rdma_mem = isert_reg_rdma;
321 device->unreg_rdma_mem = isert_unreg_rdma;
322 } else {
323 device->use_fastreg = 0;
324 device->reg_rdma_mem = isert_map_rdma;
325 device->unreg_rdma_mem = isert_unmap_cmd;
326 }
327
Or Gerlitz4a061b22015-12-18 10:59:46 +0200328 ret = isert_alloc_comps(device);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300329 if (ret)
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800330 goto out;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300331
Or Gerlitz4a061b22015-12-18 10:59:46 +0200332 device->pd = ib_alloc_pd(ib_dev);
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300333 if (IS_ERR(device->pd)) {
334 ret = PTR_ERR(device->pd);
335 isert_err("failed to allocate pd, device %p, ret=%d\n",
336 device, ret);
337 goto out_cq;
338 }
339
Sagi Grimberg172369c2015-03-29 15:52:11 +0300340 /* Check signature cap */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200341 device->pi_capable = ib_dev->attrs.device_cap_flags &
Sagi Grimberg172369c2015-03-29 15:52:11 +0300342 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300343
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800344 return 0;
345
346out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300347 isert_free_comps(device);
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800348out:
349 if (ret > 0)
350 ret = -EINVAL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800351 return ret;
352}
353
354static void
355isert_free_device_ib_res(struct isert_device *device)
356{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200357 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800358
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300359 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300360 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800361}
362
363static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300364isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800365{
366 mutex_lock(&device_list_mutex);
367 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200368 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800369 if (!device->refcount) {
370 isert_free_device_ib_res(device);
371 list_del(&device->dev_node);
372 kfree(device);
373 }
374 mutex_unlock(&device_list_mutex);
375}
376
377static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300378isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800379{
380 struct isert_device *device;
381 int ret;
382
383 mutex_lock(&device_list_mutex);
384 list_for_each_entry(device, &device_list, dev_node) {
385 if (device->ib_device->node_guid == cma_id->device->node_guid) {
386 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200387 isert_info("Found iser device %p refcount %d\n",
388 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800389 mutex_unlock(&device_list_mutex);
390 return device;
391 }
392 }
393
394 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
395 if (!device) {
396 mutex_unlock(&device_list_mutex);
397 return ERR_PTR(-ENOMEM);
398 }
399
400 INIT_LIST_HEAD(&device->dev_node);
401
402 device->ib_device = cma_id->device;
403 ret = isert_create_device_ib_res(device);
404 if (ret) {
405 kfree(device);
406 mutex_unlock(&device_list_mutex);
407 return ERR_PTR(ret);
408 }
409
410 device->refcount++;
411 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200412 isert_info("Created a new iser device %p refcount %d\n",
413 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800414 mutex_unlock(&device_list_mutex);
415
416 return device;
417}
418
Vu Pham59464ef2013-08-28 23:23:35 +0300419static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200420isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300421{
422 struct fast_reg_descriptor *fr_desc, *tmp;
423 int i = 0;
424
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300425 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300426 return;
427
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200428 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300429
430 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300431 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300432 list_del(&fr_desc->list);
Vu Pham59464ef2013-08-28 23:23:35 +0300433 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200434 if (fr_desc->pi_ctx) {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200435 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300436 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200437 kfree(fr_desc->pi_ctx);
438 }
Vu Pham59464ef2013-08-28 23:23:35 +0300439 kfree(fr_desc);
440 ++i;
441 }
442
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300443 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200444 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300445 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300446}
447
448static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200449isert_create_pi_ctx(struct fast_reg_descriptor *desc,
450 struct ib_device *device,
451 struct ib_pd *pd)
452{
Sagi Grimberg570db172014-12-02 16:57:31 +0200453 struct pi_context *pi_ctx;
454 int ret;
455
456 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
457 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200458 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200459 return -ENOMEM;
460 }
461
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300462 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
463 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimberg570db172014-12-02 16:57:31 +0200464 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200465 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200466 PTR_ERR(pi_ctx->prot_mr));
467 ret = PTR_ERR(pi_ctx->prot_mr);
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300468 goto err_pi_ctx;
Sagi Grimberg570db172014-12-02 16:57:31 +0200469 }
470 desc->ind |= ISERT_PROT_KEY_VALID;
471
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300472 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg570db172014-12-02 16:57:31 +0200473 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200474 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200475 PTR_ERR(pi_ctx->sig_mr));
476 ret = PTR_ERR(pi_ctx->sig_mr);
477 goto err_prot_mr;
478 }
479
480 desc->pi_ctx = pi_ctx;
481 desc->ind |= ISERT_SIG_KEY_VALID;
482 desc->ind &= ~ISERT_PROTECTED;
483
484 return 0;
485
486err_prot_mr:
Roland Dreierb2feda42015-05-29 23:12:10 -0700487 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg570db172014-12-02 16:57:31 +0200488err_pi_ctx:
Roland Dreierb2feda42015-05-29 23:12:10 -0700489 kfree(pi_ctx);
Sagi Grimberg570db172014-12-02 16:57:31 +0200490
491 return ret;
492}
493
494static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200495isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200496 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200497{
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300498 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
499 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200500 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200501 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200502 PTR_ERR(fr_desc->data_mr));
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300503 return PTR_ERR(fr_desc->data_mr);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200504 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200505 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200506
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200507 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200508
509 return 0;
510}
511
512static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200513isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300514{
515 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300516 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700517 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
518 struct se_node_acl *se_nacl = se_sess->se_node_acl;
519 int i, ret, tag_num;
520 /*
521 * Setup the number of FRMRs based upon the number of tags
522 * available to session in iscsi_target_locate_portal().
523 */
524 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
525 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300526
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300527 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700528 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300529 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
530 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200531 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300532 ret = -ENOMEM;
533 goto err;
534 }
535
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200536 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300537 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200538 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200539 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200540 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800541 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300542 goto err;
543 }
544
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300545 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
546 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300547 }
548
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200549 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300550 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300551
552 return 0;
553
554err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200555 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300556 return ret;
557}
558
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300559static void
560isert_init_conn(struct isert_conn *isert_conn)
561{
562 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300563 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300564 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300565 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300566 kref_init(&isert_conn->kref);
567 mutex_init(&isert_conn->mutex);
568 spin_lock_init(&isert_conn->pool_lock);
569 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300570 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300571}
572
573static void
574isert_free_login_buf(struct isert_conn *isert_conn)
575{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300576 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300577
578 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200579 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200580 kfree(isert_conn->login_rsp_buf);
581
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300582 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200583 ISER_RX_PAYLOAD_SIZE,
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300584 DMA_FROM_DEVICE);
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200585 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300586}
587
588static int
589isert_alloc_login_buf(struct isert_conn *isert_conn,
590 struct ib_device *ib_dev)
591{
592 int ret;
593
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200594 isert_conn->login_req_buf = kzalloc(sizeof(*isert_conn->login_req_buf),
595 GFP_KERNEL);
596 if (!isert_conn->login_req_buf) {
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300597 isert_err("Unable to allocate isert_conn->login_buf\n");
598 return -ENOMEM;
599 }
600
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300601 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200602 isert_conn->login_req_buf,
603 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300604 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
605 if (ret) {
606 isert_err("login_req_dma mapping error: %d\n", ret);
607 isert_conn->login_req_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200608 goto out_free_login_req_buf;
609 }
610
611 isert_conn->login_rsp_buf = kzalloc(ISER_RX_PAYLOAD_SIZE, GFP_KERNEL);
612 if (!isert_conn->login_rsp_buf) {
613 isert_err("Unable to allocate isert_conn->login_rspbuf\n");
614 goto out_unmap_login_req_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300615 }
616
617 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200618 isert_conn->login_rsp_buf,
Christoph Hellwiged1083b2016-02-24 19:24:04 +0200619 ISER_RX_PAYLOAD_SIZE, DMA_TO_DEVICE);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300620 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
621 if (ret) {
622 isert_err("login_rsp_dma mapping error: %d\n", ret);
623 isert_conn->login_rsp_dma = 0;
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200624 goto out_free_login_rsp_buf;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300625 }
626
627 return 0;
628
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200629out_free_login_rsp_buf:
630 kfree(isert_conn->login_rsp_buf);
631out_unmap_login_req_buf:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300632 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200633 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
634out_free_login_req_buf:
635 kfree(isert_conn->login_req_buf);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300636 return ret;
637}
638
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200639static void
640isert_set_nego_params(struct isert_conn *isert_conn,
641 struct rdma_conn_param *param)
642{
643 struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
644
645 /* Set max inflight RDMA READ requests */
646 isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
647 attr->max_qp_init_rd_atom);
648 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
649
650 if (param->private_data) {
651 u8 flags = *(u8 *)param->private_data;
652
653 /*
654 * use remote invalidation if the both initiator
655 * and the HCA support it
656 */
657 isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
658 (attr->device_cap_flags &
659 IB_DEVICE_MEM_MGT_EXTENSIONS);
660 if (isert_conn->snd_w_inv)
661 isert_info("Using remote invalidation\n");
662 }
663}
664
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800665static int
666isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
667{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200668 struct isert_np *isert_np = cma_id->context;
669 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800670 struct isert_conn *isert_conn;
671 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800672 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300673
674 spin_lock_bh(&np->np_thread_lock);
675 if (!np->enabled) {
676 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200677 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300678 return rdma_reject(cma_id, NULL, 0);
679 }
680 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800681
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200682 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800683 cma_id, cma_id->context);
684
685 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300686 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800687 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800688
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300689 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300690 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800691
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300692 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
693 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800694 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800695
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300696 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800697 if (IS_ERR(device)) {
698 ret = PTR_ERR(device);
699 goto out_rsp_dma_map;
700 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300701 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800702
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200703 isert_set_nego_params(isert_conn, &event->param.conn);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300704
Sagi Grimberg570db172014-12-02 16:57:31 +0200705 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800706 if (ret)
707 goto out_conn_dev;
708
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200709 ret = isert_login_post_recv(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200710 if (ret)
711 goto out_conn_dev;
712
713 ret = isert_rdma_accept(isert_conn);
714 if (ret)
715 goto out_conn_dev;
716
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300717 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300718 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300719 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800720
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800721 return 0;
722
723out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300724 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800725out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300726 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800727out:
728 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200729 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800730 return ret;
731}
732
733static void
734isert_connect_release(struct isert_conn *isert_conn)
735{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300736 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800737
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200738 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800739
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300740 BUG_ON(!device);
741
742 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200743 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300744
Sagi Grimberg19e20902014-12-02 16:57:26 +0200745 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300746 if (isert_conn->cm_id)
747 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200748
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300749 if (isert_conn->qp) {
750 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200751
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300752 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300753 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800754 }
755
Christoph Hellwig5adabdd2016-02-24 19:24:05 +0200756 if (isert_conn->login_req_buf)
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300757 isert_free_login_buf(isert_conn);
758
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300759 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800760
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300761 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800762}
763
764static void
765isert_connected_handler(struct rdma_cm_id *cma_id)
766{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200767 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300768 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300769
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200770 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200771
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300772 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300773 isert_conn->state = ISER_CONN_UP;
774 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300775 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300776
777 mutex_lock(&isert_np->mutex);
778 list_move_tail(&isert_conn->node, &isert_np->pending);
779 mutex_unlock(&isert_np->mutex);
780
781 isert_info("np %p: Allow accept_np to continue\n", isert_np);
782 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800783}
784
785static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300786isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800787{
788 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300789 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800790
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200791 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
792 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800793
794 isert_connect_release(isert_conn);
795}
796
797static void
798isert_put_conn(struct isert_conn *isert_conn)
799{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300800 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800801}
802
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200803static void
804isert_handle_unbound_conn(struct isert_conn *isert_conn)
805{
806 struct isert_np *isert_np = isert_conn->cm_id->context;
807
808 mutex_lock(&isert_np->mutex);
809 if (!list_empty(&isert_conn->node)) {
810 /*
811 * This means iscsi doesn't know this connection
812 * so schedule a cleanup ourselves
813 */
814 list_del_init(&isert_conn->node);
815 isert_put_conn(isert_conn);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200816 queue_work(isert_release_wq, &isert_conn->release_work);
817 }
818 mutex_unlock(&isert_np->mutex);
819}
820
Sagi Grimberg954f2372014-12-02 16:57:17 +0200821/**
822 * isert_conn_terminate() - Initiate connection termination
823 * @isert_conn: isert connection struct
824 *
825 * Notes:
Jenny Derzhavetzaea92982016-02-24 19:23:59 +0200826 * In case the connection state is BOUND, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200827 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200828 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200829 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300830 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200831 * safe to call multiple times.
832 */
833static void
834isert_conn_terminate(struct isert_conn *isert_conn)
835{
836 int err;
837
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200838 if (isert_conn->state >= ISER_CONN_TERMINATING)
839 return;
840
841 isert_info("Terminating conn %p state %d\n",
842 isert_conn, isert_conn->state);
843 isert_conn->state = ISER_CONN_TERMINATING;
844 err = rdma_disconnect(isert_conn->cm_id);
845 if (err)
846 isert_warn("Failed rdma_disconnect isert_conn %p\n",
847 isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200848}
849
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700850static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200851isert_np_cma_handler(struct isert_np *isert_np,
852 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800853{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300854 isert_dbg("%s (%d): isert np %p\n",
855 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200856
857 switch (event) {
858 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300859 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200860 break;
861 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300862 isert_np->cm_id = isert_setup_id(isert_np);
863 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200864 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300865 isert_np, PTR_ERR(isert_np->cm_id));
866 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200867 }
868 break;
869 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200870 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200871 isert_np, event);
872 }
873
874 return -1;
875}
876
877static int
878isert_disconnected_handler(struct rdma_cm_id *cma_id,
879 enum rdma_cm_event_type event)
880{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200881 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700882
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300883 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200884 switch (isert_conn->state) {
885 case ISER_CONN_TERMINATING:
886 break;
887 case ISER_CONN_UP:
888 isert_conn_terminate(isert_conn);
Sagi Grimberg572a1432016-02-24 19:24:09 +0200889 ib_drain_qp(isert_conn->qp);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200890 isert_handle_unbound_conn(isert_conn);
891 break;
892 case ISER_CONN_BOUND:
893 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
894 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
895 break;
896 default:
897 isert_warn("conn %p teminating in state %d\n",
898 isert_conn, isert_conn->state);
899 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300900 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200901
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700902 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800903}
904
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300905static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200906isert_connect_error(struct rdma_cm_id *cma_id)
907{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200908 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200909
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300910 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300911 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200912 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300913
914 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200915}
916
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800917static int
918isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
919{
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200920 struct isert_np *isert_np = cma_id->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800921 int ret = 0;
922
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300923 isert_info("%s (%d): status %d id %p np %p\n",
924 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200925 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800926
Jenny Derzhavetzf81bf452016-02-24 19:24:00 +0200927 if (isert_np->cm_id == cma_id)
928 return isert_np_cma_handler(cma_id->context, event->event);
929
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800930 switch (event->event) {
931 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800932 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700933 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200934 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800935 break;
936 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800937 isert_connected_handler(cma_id);
938 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300939 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
940 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
941 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300942 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200943 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800944 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200945 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
946 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800947 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300948 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200949 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800950 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200951 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800952 break;
953 }
954
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800955 return ret;
956}
957
958static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300959isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800960{
961 struct ib_recv_wr *rx_wr, *rx_wr_failed;
962 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800963 struct iser_rx_desc *rx_desc;
964
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300965 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300966 rx_desc = &isert_conn->rx_descs[i];
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200967
968 rx_wr->wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300969 rx_wr->sg_list = &rx_desc->rx_sg;
970 rx_wr->num_sge = 1;
971 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800972 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800973 rx_wr--;
974 rx_wr->next = NULL; /* mark end of work requests list */
975
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300976 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300977 &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200978 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200979 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300980
981 return ret;
982}
983
984static int
985isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
986{
987 struct ib_recv_wr *rx_wr_failed, rx_wr;
988 int ret;
989
Christoph Hellwig9679cc52016-02-24 19:24:06 +0200990 rx_wr.wr_cqe = &rx_desc->rx_cqe;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300991 rx_wr.sg_list = &rx_desc->rx_sg;
992 rx_wr.num_sge = 1;
993 rx_wr.next = NULL;
994
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300995 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +0200996 if (ret)
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300997 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300998
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800999 return ret;
1000}
1001
1002static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001003isert_login_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001004{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001005 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001006 struct ib_send_wr send_wr, *send_wr_failed;
1007 int ret;
1008
1009 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1010 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1011
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001012 tx_desc->tx_cqe.done = isert_login_send_done;
1013
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001014 send_wr.next = NULL;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001015 send_wr.wr_cqe = &tx_desc->tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001016 send_wr.sg_list = tx_desc->tx_sg;
1017 send_wr.num_sge = tx_desc->num_sge;
1018 send_wr.opcode = IB_WR_SEND;
1019 send_wr.send_flags = IB_SEND_SIGNALED;
1020
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001021 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001022 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001023 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001024
1025 return ret;
1026}
1027
1028static void
1029isert_create_send_desc(struct isert_conn *isert_conn,
1030 struct isert_cmd *isert_cmd,
1031 struct iser_tx_desc *tx_desc)
1032{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001033 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001034 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001035
1036 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1037 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1038
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001039 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
1040 tx_desc->iser_header.flags = ISCSI_CTRL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001041
1042 tx_desc->num_sge = 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001043
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001044 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1045 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001046 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001047 }
1048}
1049
1050static int
1051isert_init_tx_hdrs(struct isert_conn *isert_conn,
1052 struct iser_tx_desc *tx_desc)
1053{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001054 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001055 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001056 u64 dma_addr;
1057
1058 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1059 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1060 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001061 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001062 return -ENOMEM;
1063 }
1064
1065 tx_desc->dma_addr = dma_addr;
1066 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1067 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001068 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001069
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001070 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1071 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1072 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001073
1074 return 0;
1075}
1076
1077static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001078isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001079 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001080{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001081 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1082
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001083 isert_cmd->iser_ib_op = ISER_IB_SEND;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001084 tx_desc->tx_cqe.done = isert_send_done;
1085 send_wr->wr_cqe = &tx_desc->tx_cqe;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001086
1087 if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
1088 send_wr->opcode = IB_WR_SEND_WITH_INV;
1089 send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
1090 } else {
1091 send_wr->opcode = IB_WR_SEND;
1092 }
1093
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001094 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001095 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001096 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001097}
1098
1099static int
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001100isert_login_post_recv(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001101{
1102 struct ib_recv_wr rx_wr, *rx_wr_fail;
1103 struct ib_sge sge;
1104 int ret;
1105
1106 memset(&sge, 0, sizeof(struct ib_sge));
1107 sge.addr = isert_conn->login_req_dma;
Christoph Hellwiged1083b2016-02-24 19:24:04 +02001108 sge.length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001109 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001110
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001111 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001112 sge.addr, sge.length, sge.lkey);
1113
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001114 isert_conn->login_req_buf->rx_cqe.done = isert_login_recv_done;
1115
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001116 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001117 rx_wr.wr_cqe = &isert_conn->login_req_buf->rx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001118 rx_wr.sg_list = &sge;
1119 rx_wr.num_sge = 1;
1120
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001121 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Jenny Derzhavetz6d1fba02016-02-24 19:24:01 +02001122 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001123 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001124
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001125 return ret;
1126}
1127
1128static int
1129isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1130 u32 length)
1131{
1132 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001133 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001134 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001135 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001136 int ret;
1137
1138 isert_create_send_desc(isert_conn, NULL, tx_desc);
1139
1140 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1141 sizeof(struct iscsi_hdr));
1142
1143 isert_init_tx_hdrs(isert_conn, tx_desc);
1144
1145 if (length > 0) {
1146 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1147
1148 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1149 length, DMA_TO_DEVICE);
1150
1151 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1152
1153 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1154 length, DMA_TO_DEVICE);
1155
1156 tx_dsg->addr = isert_conn->login_rsp_dma;
1157 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001158 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001159 tx_desc->num_sge = 2;
1160 }
1161 if (!login->login_failed) {
1162 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001163 if (!conn->sess->sess_ops->SessionType &&
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001164 isert_conn->device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001165 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001166 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001167 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001168 " fastreg pool\n", isert_conn);
1169 return ret;
1170 }
1171 }
1172
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001173 ret = isert_alloc_rx_descriptors(isert_conn);
1174 if (ret)
1175 return ret;
1176
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001177 ret = isert_post_recvm(isert_conn,
1178 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001179 if (ret)
1180 return ret;
1181
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001182 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001183 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001184 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001185 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001186 goto post_send;
1187 }
1188
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001189 ret = isert_login_post_recv(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001190 if (ret)
1191 return ret;
1192 }
1193post_send:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001194 ret = isert_login_post_send(isert_conn, tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001195 if (ret)
1196 return ret;
1197
1198 return 0;
1199}
1200
1201static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001202isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001203{
Christoph Hellwig5adabdd2016-02-24 19:24:05 +02001204 struct iser_rx_desc *rx_desc = isert_conn->login_req_buf;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001205 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001206 struct iscsi_conn *conn = isert_conn->conn;
1207 struct iscsi_login *login = conn->conn_login;
1208 int size;
1209
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001210 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001211
1212 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001213
1214 if (login->first_request) {
1215 struct iscsi_login_req *login_req =
1216 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1217 /*
1218 * Setup the initial iscsi_login values from the leading
1219 * login request PDU.
1220 */
1221 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1222 login->current_stage =
1223 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1224 >> 2;
1225 login->version_min = login_req->min_version;
1226 login->version_max = login_req->max_version;
1227 memcpy(login->isid, login_req->isid, 6);
1228 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1229 login->init_task_tag = login_req->itt;
1230 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1231 login->cid = be16_to_cpu(login_req->cid);
1232 login->tsih = be16_to_cpu(login_req->tsih);
1233 }
1234
1235 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1236
1237 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001238 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1239 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1240 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001241 memcpy(login->req_buf, &rx_desc->data[0], size);
1242
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001243 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001244 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001245 return;
1246 }
1247 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001248}
1249
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001250static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001251*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001252{
Sagi Grimberg67004252015-03-29 15:52:09 +03001253 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001254 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001255 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001256
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001257 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001258 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001259 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001260 return NULL;
1261 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001262 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001263 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001264 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001265 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001266
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001267 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001268}
1269
1270static int
1271isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001272 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1273 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001274{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001275 struct iscsi_conn *conn = isert_conn->conn;
1276 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001277 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1278 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001279 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001280
1281 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1282 if (rc < 0)
1283 return rc;
1284
1285 imm_data = cmd->immediate_data;
1286 imm_data_len = cmd->first_burst_len;
1287 unsol_data = cmd->unsolicited_data;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001288 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001289
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001290 if (imm_data && imm_data_len == data_len)
1291 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001292 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1293 if (rc < 0) {
1294 return 0;
1295 } else if (rc > 0) {
1296 dump_payload = true;
1297 goto sequence_cmd;
1298 }
1299
1300 if (!imm_data)
1301 return 0;
1302
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001303 if (imm_data_len != data_len) {
1304 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1305 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1306 &rx_desc->data[0], imm_data_len);
1307 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1308 sg_nents, imm_data_len);
1309 } else {
1310 sg_init_table(&isert_cmd->sg, 1);
1311 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1312 cmd->se_cmd.t_data_nents = 1;
1313 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1314 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1315 imm_data_len);
1316 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001317
1318 cmd->write_data_done += imm_data_len;
1319
1320 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1321 spin_lock_bh(&cmd->istate_lock);
1322 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1323 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1324 spin_unlock_bh(&cmd->istate_lock);
1325 }
1326
1327sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001328 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001329
1330 if (!rc && dump_payload == false && unsol_data)
1331 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001332 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001333 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001334
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001335 return 0;
1336}
1337
1338static int
1339isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1340 struct iser_rx_desc *rx_desc, unsigned char *buf)
1341{
1342 struct scatterlist *sg_start;
1343 struct iscsi_conn *conn = isert_conn->conn;
1344 struct iscsi_cmd *cmd = NULL;
1345 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1346 u32 unsol_data_len = ntoh24(hdr->dlength);
1347 int rc, sg_nents, sg_off, page_off;
1348
1349 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1350 if (rc < 0)
1351 return rc;
1352 else if (!cmd)
1353 return 0;
1354 /*
1355 * FIXME: Unexpected unsolicited_data out
1356 */
1357 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001358 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001359 dump_stack();
1360 return -1;
1361 }
1362
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001363 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1364 "write_data_done: %u, data_length: %u\n",
1365 unsol_data_len, cmd->write_data_done,
1366 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001367
1368 sg_off = cmd->write_data_done / PAGE_SIZE;
1369 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1370 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1371 page_off = cmd->write_data_done % PAGE_SIZE;
1372 /*
1373 * FIXME: Non page-aligned unsolicited_data out
1374 */
1375 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001376 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001377 dump_stack();
1378 return -1;
1379 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001380 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1381 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1382 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001383
1384 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1385 unsol_data_len);
1386
1387 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1388 if (rc < 0)
1389 return rc;
1390
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001391 /*
1392 * multiple data-outs on the same command can arrive -
1393 * so post the buffer before hand
1394 */
1395 rc = isert_post_recv(isert_conn, rx_desc);
1396 if (rc) {
1397 isert_err("ib_post_recv failed with %d\n", rc);
1398 return rc;
1399 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001400 return 0;
1401}
1402
1403static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001404isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001405 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1406 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001407{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001408 struct iscsi_conn *conn = isert_conn->conn;
1409 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1410 int rc;
1411
1412 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1413 if (rc < 0)
1414 return rc;
1415 /*
1416 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1417 */
1418
1419 return iscsit_process_nop_out(conn, cmd, hdr);
1420}
1421
1422static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001423isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001424 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1425 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001426{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001427 struct iscsi_conn *conn = isert_conn->conn;
1428 u32 payload_length = ntoh24(hdr->dlength);
1429 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001430 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001431
1432 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1433 if (rc < 0)
1434 return rc;
1435
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001436 if (payload_length) {
1437 text_in = kzalloc(payload_length, GFP_KERNEL);
1438 if (!text_in) {
1439 isert_err("Unable to allocate text_in of payload_length: %u\n",
1440 payload_length);
1441 return -ENOMEM;
1442 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001443 }
1444 cmd->text_in_ptr = text_in;
1445
1446 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1447
1448 return iscsit_process_text_cmd(conn, cmd, hdr);
1449}
1450
1451static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001452isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1453 uint32_t read_stag, uint64_t read_va,
1454 uint32_t write_stag, uint64_t write_va)
1455{
1456 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1457 struct iscsi_conn *conn = isert_conn->conn;
1458 struct iscsi_cmd *cmd;
1459 struct isert_cmd *isert_cmd;
1460 int ret = -EINVAL;
1461 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1462
Sagi Grimbergfb140272015-03-29 15:52:08 +03001463 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001464 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001465 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001466 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001467 return 0;
1468 }
1469
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001470 switch (opcode) {
1471 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001472 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001473 if (!cmd)
1474 break;
1475
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001476 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001477 isert_cmd->read_stag = read_stag;
1478 isert_cmd->read_va = read_va;
1479 isert_cmd->write_stag = write_stag;
1480 isert_cmd->write_va = write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001481 isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001482
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001483 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001484 rx_desc, (unsigned char *)hdr);
1485 break;
1486 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001487 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001488 if (!cmd)
1489 break;
1490
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001491 isert_cmd = iscsit_priv_cmd(cmd);
1492 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001493 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001494 break;
1495 case ISCSI_OP_SCSI_DATA_OUT:
1496 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1497 (unsigned char *)hdr);
1498 break;
1499 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001500 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001501 if (!cmd)
1502 break;
1503
1504 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1505 (unsigned char *)hdr);
1506 break;
1507 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001508 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001509 if (!cmd)
1510 break;
1511
1512 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001513 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001514 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001515 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001516 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001517 else
1518 cmd = isert_allocate_cmd(conn, rx_desc);
1519
1520 if (!cmd)
1521 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001522
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001523 isert_cmd = iscsit_priv_cmd(cmd);
1524 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001525 rx_desc, (struct iscsi_text *)hdr);
1526 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001527 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001528 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001529 dump_stack();
1530 break;
1531 }
1532
1533 return ret;
1534}
1535
1536static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001537isert_print_wc(struct ib_wc *wc, const char *type)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001538{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001539 if (wc->status != IB_WC_WR_FLUSH_ERR)
1540 isert_err("%s failure: %s (%d) vend_err %x\n", type,
1541 ib_wc_status_msg(wc->status), wc->status,
1542 wc->vendor_err);
1543 else
1544 isert_dbg("%s failure: %s (%d)\n", type,
1545 ib_wc_status_msg(wc->status), wc->status);
1546}
1547
1548static void
1549isert_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1550{
1551 struct isert_conn *isert_conn = wc->qp->qp_context;
1552 struct ib_device *ib_dev = isert_conn->cm_id->device;
1553 struct iser_rx_desc *rx_desc = cqe_to_rx_desc(wc->wr_cqe);
1554 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001555 struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001556 uint64_t read_va = 0, write_va = 0;
1557 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001558
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001559 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1560 isert_print_wc(wc, "recv");
1561 if (wc->status != IB_WC_WR_FLUSH_ERR)
1562 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1563 return;
1564 }
1565
1566 ib_dma_sync_single_for_cpu(ib_dev, rx_desc->dma_addr,
1567 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
1568
1569 isert_dbg("DMA: 0x%llx, iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1570 rx_desc->dma_addr, hdr->opcode, hdr->itt, hdr->flags,
1571 (int)(wc->byte_len - ISER_HEADERS_LEN));
1572
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001573 switch (iser_ctrl->flags & 0xF0) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001574 case ISCSI_CTRL:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001575 if (iser_ctrl->flags & ISER_RSV) {
1576 read_stag = be32_to_cpu(iser_ctrl->read_stag);
1577 read_va = be64_to_cpu(iser_ctrl->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001578 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1579 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001580 }
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001581 if (iser_ctrl->flags & ISER_WSV) {
1582 write_stag = be32_to_cpu(iser_ctrl->write_stag);
1583 write_va = be64_to_cpu(iser_ctrl->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001584 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1585 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001586 }
1587
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001588 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001589 break;
1590 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001591 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001592 break;
1593 default:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001594 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001595 break;
1596 }
1597
Bart Van Assche6c760b32015-10-22 11:14:18 -07001598 isert_rx_opcode(isert_conn, rx_desc,
1599 read_stag, read_va, write_stag, write_va);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001600
1601 ib_dma_sync_single_for_device(ib_dev, rx_desc->dma_addr,
1602 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001603}
1604
1605static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001606isert_login_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001607{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001608 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001609 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001610
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001611 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1612 isert_print_wc(wc, "login recv");
1613 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001614 }
1615
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001616 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_req_dma,
1617 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001618
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001619 isert_conn->login_req_len = wc->byte_len - ISER_HEADERS_LEN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001620
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001621 if (isert_conn->conn) {
1622 struct iscsi_login *login = isert_conn->conn->conn_login;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001623
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001624 if (login && !login->first_request)
1625 isert_rx_login_req(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001626 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001627
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001628 mutex_lock(&isert_conn->mutex);
1629 complete(&isert_conn->login_req_comp);
1630 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001631
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001632 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_req_dma,
1633 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001634}
1635
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001636static int
1637isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1638 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1639 enum iser_ib_op_code op, struct isert_data_buf *data)
1640{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001641 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001642
1643 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1644 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1645
1646 data->len = length - offset;
1647 data->offset = offset;
1648 data->sg_off = data->offset / PAGE_SIZE;
1649
1650 data->sg = &sg[data->sg_off];
1651 data->nents = min_t(unsigned int, nents - data->sg_off,
1652 ISCSI_ISER_SG_TABLESIZE);
1653 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1654 PAGE_SIZE);
1655
1656 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1657 data->dma_dir);
1658 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001659 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001660 return -EINVAL;
1661 }
1662
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001663 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001664 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001665
1666 return 0;
1667}
1668
1669static void
1670isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1671{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001672 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001673
1674 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1675 memset(data, 0, sizeof(*data));
1676}
1677
1678
1679
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001680static void
1681isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1682{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001683 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001684
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001685 if (isert_cmd->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001686 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001687 isert_unmap_data_buf(isert_conn, &isert_cmd->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001688 }
1689
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001690 if (isert_cmd->rdma_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001691 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001692 kfree(isert_cmd->rdma_wr);
1693 isert_cmd->rdma_wr = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001694 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001695
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001696 if (isert_cmd->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001697 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001698 kfree(isert_cmd->ib_sge);
1699 isert_cmd->ib_sge = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001700 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001701}
1702
1703static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001704isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001705{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001706 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001707
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001708 if (isert_cmd->fr_desc) {
1709 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, isert_cmd->fr_desc);
1710 if (isert_cmd->fr_desc->ind & ISERT_PROTECTED) {
1711 isert_unmap_data_buf(isert_conn, &isert_cmd->prot);
1712 isert_cmd->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001713 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001714 spin_lock_bh(&isert_conn->pool_lock);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001715 list_add_tail(&isert_cmd->fr_desc->list, &isert_conn->fr_pool);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001716 spin_unlock_bh(&isert_conn->pool_lock);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001717 isert_cmd->fr_desc = NULL;
Vu Pham59464ef2013-08-28 23:23:35 +03001718 }
1719
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001720 if (isert_cmd->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001721 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001722 isert_unmap_data_buf(isert_conn, &isert_cmd->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001723 }
1724
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001725 isert_cmd->ib_sge = NULL;
1726 isert_cmd->rdma_wr = NULL;
Vu Pham59464ef2013-08-28 23:23:35 +03001727}
1728
1729static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001730isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001731{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001732 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001733 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001734 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001735 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001736 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001737
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001738 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001739
1740 switch (cmd->iscsi_opcode) {
1741 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001742 spin_lock_bh(&conn->cmd_lock);
1743 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001744 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001745 spin_unlock_bh(&conn->cmd_lock);
1746
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001747 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001748 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001749 /*
1750 * Check for special case during comp_err where
1751 * WRITE_PENDING has been handed off from core,
1752 * but requires an extra target_put_sess_cmd()
1753 * before transport_generic_free_cmd() below.
1754 */
1755 if (comp_err &&
1756 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1757 struct se_cmd *se_cmd = &cmd->se_cmd;
1758
Bart Van Asscheafc16602015-04-27 13:52:36 +02001759 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001760 }
1761 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001762
Vu Phamd40945d2013-08-28 23:23:34 +03001763 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001764 transport_generic_free_cmd(&cmd->se_cmd, 0);
1765 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001766 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001767 spin_lock_bh(&conn->cmd_lock);
1768 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001769 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001770 spin_unlock_bh(&conn->cmd_lock);
1771
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001772 transport_generic_free_cmd(&cmd->se_cmd, 0);
1773 break;
1774 case ISCSI_OP_REJECT:
1775 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001776 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001777 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1778 /* If the continue bit is on, keep the command alive */
1779 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1780 break;
1781
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001782 spin_lock_bh(&conn->cmd_lock);
1783 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001784 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001785 spin_unlock_bh(&conn->cmd_lock);
1786
1787 /*
1788 * Handle special case for REJECT when iscsi_add_reject*() has
1789 * overwritten the original iscsi_opcode assignment, and the
1790 * associated cmd->se_cmd needs to be released.
1791 */
1792 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001793 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001794 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001795 transport_generic_free_cmd(&cmd->se_cmd, 0);
1796 break;
1797 }
1798 /*
1799 * Fall-through
1800 */
1801 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001802 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001803 break;
1804 }
1805}
1806
1807static void
1808isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1809{
1810 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001811 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001812 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1813 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1814 tx_desc->dma_addr = 0;
1815 }
1816}
1817
1818static void
1819isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001820 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001821{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001822 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001823 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001824 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1825 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1826 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001827 }
1828
1829 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001830 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001831}
1832
Sagi Grimberg96b79732014-03-17 12:52:18 +02001833static int
1834isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1835{
1836 struct ib_mr_status mr_status;
1837 int ret;
1838
1839 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1840 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001841 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001842 goto fail_mr_status;
1843 }
1844
1845 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1846 u64 sec_offset_err;
1847 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1848
1849 switch (mr_status.sig_err.err_type) {
1850 case IB_SIG_BAD_GUARD:
1851 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1852 break;
1853 case IB_SIG_BAD_REFTAG:
1854 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1855 break;
1856 case IB_SIG_BAD_APPTAG:
1857 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1858 break;
1859 }
1860 sec_offset_err = mr_status.sig_err.sig_err_offset;
1861 do_div(sec_offset_err, block_size);
1862 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1863
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001864 isert_err("PI error found type %d at sector 0x%llx "
1865 "expected 0x%x vs actual 0x%x\n",
1866 mr_status.sig_err.err_type,
1867 (unsigned long long)se_cmd->bad_sector,
1868 mr_status.sig_err.expected,
1869 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001870 ret = 1;
1871 }
1872
1873fail_mr_status:
1874 return ret;
1875}
1876
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001877static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001878isert_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001879{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001880 struct isert_conn *isert_conn = wc->qp->qp_context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001881 struct isert_device *device = isert_conn->device;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001882 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001883 struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001884 struct se_cmd *cmd = &isert_cmd->iscsi_cmd->se_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001885 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001886
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001887 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1888 isert_print_wc(wc, "rdma write");
1889 if (wc->status != IB_WC_WR_FLUSH_ERR)
1890 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1891 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1892 return;
1893 }
1894
1895 isert_dbg("Cmd %p\n", isert_cmd);
1896
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001897 if (isert_cmd->fr_desc && isert_cmd->fr_desc->ind & ISERT_PROTECTED) {
1898 ret = isert_check_pi_status(cmd,
1899 isert_cmd->fr_desc->pi_ctx->sig_mr);
1900 isert_cmd->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001901 }
1902
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001903 device->unreg_rdma_mem(isert_cmd, isert_conn);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001904 isert_cmd->rdma_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001905 if (ret)
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001906 transport_send_check_condition_and_sense(cmd, cmd->pi_err, 0);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001907 else
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001908 isert_put_response(isert_conn->conn, isert_cmd->iscsi_cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001909}
1910
1911static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001912isert_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001913{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001914 struct isert_conn *isert_conn = wc->qp->qp_context;
1915 struct isert_device *device = isert_conn->device;
1916 struct iser_tx_desc *desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02001917 struct isert_cmd *isert_cmd = tx_desc_to_cmd(desc);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001918 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001919 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001920 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001921
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001922 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1923 isert_print_wc(wc, "rdma read");
1924 if (wc->status != IB_WC_WR_FLUSH_ERR)
1925 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1926 isert_completion_put(desc, isert_cmd, device->ib_device, true);
1927 return;
1928 }
1929
1930 isert_dbg("Cmd %p\n", isert_cmd);
1931
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001932 if (isert_cmd->fr_desc && isert_cmd->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001933 ret = isert_check_pi_status(se_cmd,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001934 isert_cmd->fr_desc->pi_ctx->sig_mr);
1935 isert_cmd->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001936 }
1937
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001938 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001939 device->unreg_rdma_mem(isert_cmd, isert_conn);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02001940 cmd->write_data_done = isert_cmd->data.len;
1941 isert_cmd->rdma_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001942
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001943 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001944 spin_lock_bh(&cmd->istate_lock);
1945 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1946 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1947 spin_unlock_bh(&cmd->istate_lock);
1948
Sagi Grimberg364189f2015-03-29 15:52:03 +03001949 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001950 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001951 transport_send_check_condition_and_sense(se_cmd,
1952 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001953 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001954 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001955 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001956}
1957
1958static void
1959isert_do_control_comp(struct work_struct *work)
1960{
1961 struct isert_cmd *isert_cmd = container_of(work,
1962 struct isert_cmd, comp_work);
1963 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001964 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001965 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001966
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001967 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1968
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001969 switch (cmd->i_state) {
1970 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001971 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001972 case ISTATE_SEND_REJECT: /* FALLTHRU */
1973 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001974 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001975 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1976 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001977 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001978 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001979 iscsit_logout_post_handler(cmd, cmd->conn);
1980 break;
1981 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001982 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001983 dump_stack();
1984 break;
1985 }
1986}
1987
1988static void
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001989isert_login_send_done(struct ib_cq *cq, struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001990{
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001991 struct isert_conn *isert_conn = wc->qp->qp_context;
1992 struct ib_device *ib_dev = isert_conn->cm_id->device;
1993 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001994
Christoph Hellwig9679cc52016-02-24 19:24:06 +02001995 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1996 isert_print_wc(wc, "login send");
1997 if (wc->status != IB_WC_WR_FLUSH_ERR)
1998 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1999 }
2000
2001 isert_unmap_tx_desc(tx_desc, ib_dev);
2002}
2003
2004static void
2005isert_send_done(struct ib_cq *cq, struct ib_wc *wc)
2006{
2007 struct isert_conn *isert_conn = wc->qp->qp_context;
2008 struct ib_device *ib_dev = isert_conn->cm_id->device;
2009 struct iser_tx_desc *tx_desc = cqe_to_tx_desc(wc->wr_cqe);
Christoph Hellwige3416ab2016-02-24 19:24:08 +02002010 struct isert_cmd *isert_cmd = tx_desc_to_cmd(tx_desc);
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002011
2012 if (unlikely(wc->status != IB_WC_SUCCESS)) {
2013 isert_print_wc(wc, "send");
2014 if (wc->status != IB_WC_WR_FLUSH_ERR)
2015 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2016 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
2017 return;
2018 }
2019
2020 isert_dbg("Cmd %p\n", isert_cmd);
2021
2022 switch (isert_cmd->iscsi_cmd->i_state) {
2023 case ISTATE_SEND_TASKMGTRSP:
2024 case ISTATE_SEND_LOGOUTRSP:
2025 case ISTATE_SEND_REJECT:
2026 case ISTATE_SEND_TEXTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002027 isert_unmap_tx_desc(tx_desc, ib_dev);
2028
2029 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
2030 queue_work(isert_comp_wq, &isert_cmd->comp_work);
2031 return;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002032 default:
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002033 isert_cmd->iscsi_cmd->i_state = ISTATE_SENT_STATUS;
2034 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002035 break;
2036 }
2037}
2038
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002039static int
2040isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2041{
2042 struct ib_send_wr *wr_failed;
2043 int ret;
2044
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002045 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2046 if (ret) {
2047 isert_err("ib_post_recv failed with %d\n", ret);
2048 return ret;
2049 }
2050
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002051 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002052 &wr_failed);
2053 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002054 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002055 return ret;
2056 }
2057 return ret;
2058}
2059
2060static int
2061isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2062{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002063 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002064 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002065 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2066 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2067 &isert_cmd->tx_desc.iscsi_header;
2068
2069 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2070 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2071 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2072 /*
2073 * Attach SENSE DATA payload to iSCSI Response PDU
2074 */
2075 if (cmd->se_cmd.sense_buffer &&
2076 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2077 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002078 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002079 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002080 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002081 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002082
2083 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2084 cmd->sense_buffer);
2085 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2086
2087 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2088 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002089 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002090
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002091 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2092 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002093 DMA_TO_DEVICE);
2094
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002095 isert_cmd->pdu_buf_len = pdu_len;
2096 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2097 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002098 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002099 isert_cmd->tx_desc.num_sge = 2;
2100 }
2101
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002102 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002103
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002104 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002105
2106 return isert_post_response(isert_conn, isert_cmd);
2107}
2108
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002109static void
2110isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2111{
2112 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002113 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002114 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002115
2116 spin_lock_bh(&conn->cmd_lock);
2117 if (!list_empty(&cmd->i_conn_node))
2118 list_del_init(&cmd->i_conn_node);
2119 spin_unlock_bh(&conn->cmd_lock);
2120
2121 if (cmd->data_direction == DMA_TO_DEVICE)
2122 iscsit_stop_dataout_timer(cmd);
2123
2124 device->unreg_rdma_mem(isert_cmd, isert_conn);
2125}
2126
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002127static enum target_prot_op
2128isert_get_sup_prot_ops(struct iscsi_conn *conn)
2129{
Sagi Grimberg67004252015-03-29 15:52:09 +03002130 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002131 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002132
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002133 if (conn->tpg->tpg_attrib.t10_pi) {
2134 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002135 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002136 isert_conn->pi_support = true;
2137 return TARGET_PROT_ALL;
2138 }
2139 }
2140
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002141 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002142 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002143
2144 return TARGET_PROT_NORMAL;
2145}
2146
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002147static int
2148isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2149 bool nopout_response)
2150{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002151 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002152 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002153 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2154
2155 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2156 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2157 &isert_cmd->tx_desc.iscsi_header,
2158 nopout_response);
2159 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002160 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002161
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002162 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002163
2164 return isert_post_response(isert_conn, isert_cmd);
2165}
2166
2167static int
2168isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2169{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002170 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002171 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002172 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2173
2174 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2175 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2176 &isert_cmd->tx_desc.iscsi_header);
2177 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002178 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002179
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002180 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002181
2182 return isert_post_response(isert_conn, isert_cmd);
2183}
2184
2185static int
2186isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2187{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002188 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002189 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002190 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2191
2192 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2193 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2194 &isert_cmd->tx_desc.iscsi_header);
2195 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002196 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002197
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002198 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002199
2200 return isert_post_response(isert_conn, isert_cmd);
2201}
2202
2203static int
2204isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2205{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002206 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002207 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002208 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002209 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002210 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002211 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2212 struct iscsi_reject *hdr =
2213 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002214
2215 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002216 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002217 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002218
2219 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002220 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002221 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2222 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002223 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2224 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002225 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002226 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002227 isert_cmd->tx_desc.num_sge = 2;
2228
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002229 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002230
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002231 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002232
2233 return isert_post_response(isert_conn, isert_cmd);
2234}
2235
2236static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002237isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2238{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002239 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002240 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002241 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2242 struct iscsi_text_rsp *hdr =
2243 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2244 u32 txt_rsp_len;
2245 int rc;
2246
2247 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002248 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002249 if (rc < 0)
2250 return rc;
2251
2252 txt_rsp_len = rc;
2253 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2254
2255 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002256 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002257 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002258 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2259 void *txt_rsp_buf = cmd->buf_ptr;
2260
2261 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2262 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2263
2264 isert_cmd->pdu_buf_len = txt_rsp_len;
2265 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2266 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002267 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002268 isert_cmd->tx_desc.num_sge = 2;
2269 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002270 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002271
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002272 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002273
2274 return isert_post_response(isert_conn, isert_cmd);
2275}
2276
2277static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002278isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002279 struct ib_sge *ib_sge, struct ib_rdma_wr *rdma_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002280 u32 data_left, u32 offset)
2281{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002282 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002283 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002284 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002285 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002286 u32 sg_off, page_off;
2287 int i = 0, sg_nents;
2288
2289 sg_off = offset / PAGE_SIZE;
2290 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2291 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2292 page_off = offset % PAGE_SIZE;
2293
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002294 rdma_wr->wr.sg_list = ib_sge;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002295 rdma_wr->wr.wr_cqe = &isert_cmd->tx_desc.tx_cqe;
2296
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002297 /*
2298 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2299 */
2300 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002301 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2302 "page_off: %u\n",
2303 (unsigned long long)tmp_sg->dma_address,
2304 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002305
2306 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2307 ib_sge->length = min_t(u32, data_left,
2308 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002309 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002310
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002311 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2312 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002313 page_off = 0;
2314 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002315 if (!data_left)
2316 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002317 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002318 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002319 }
2320
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002321 rdma_wr->wr.num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002322 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002323 rdma_wr->wr.sg_list, rdma_wr->wr.num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002324
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002325 return rdma_wr->wr.num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002326}
2327
2328static int
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002329isert_map_rdma(struct isert_cmd *isert_cmd, struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002330{
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002331 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002332 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberg67004252015-03-29 15:52:09 +03002333 struct isert_conn *isert_conn = conn->context;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002334 struct isert_data_buf *data = &isert_cmd->data;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002335 struct ib_rdma_wr *rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002336 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002337 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2338 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002339
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002340 offset = isert_cmd->iser_ib_op == ISER_IB_RDMA_READ ?
2341 cmd->write_data_done : 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002342 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2343 se_cmd->t_data_nents, se_cmd->data_length,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002344 offset, isert_cmd->iser_ib_op,
2345 &isert_cmd->data);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002346 if (ret)
2347 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002348
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002349 data_left = data->len;
2350 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002351
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002352 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002353 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002354 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002355 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002356 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002357 }
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002358 isert_cmd->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002359
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002360 isert_cmd->rdma_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2361 isert_cmd->rdma_wr = kzalloc(sizeof(struct ib_rdma_wr) *
2362 isert_cmd->rdma_wr_num, GFP_KERNEL);
2363 if (!isert_cmd->rdma_wr) {
2364 isert_dbg("Unable to allocate isert_cmd->rdma_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002365 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002366 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002367 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002368
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002369 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002370
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002371 for (i = 0; i < isert_cmd->rdma_wr_num; i++) {
2372 rdma_wr = &isert_cmd->rdma_wr[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002373 data_len = min(data_left, rdma_write_max);
2374
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002375 rdma_wr->wr.send_flags = 0;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002376 if (isert_cmd->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002377 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
2378
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002379 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2380 rdma_wr->remote_addr = isert_cmd->read_va + offset;
2381 rdma_wr->rkey = isert_cmd->read_stag;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002382 if (i + 1 == isert_cmd->rdma_wr_num)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002383 rdma_wr->wr.next = &isert_cmd->tx_desc.send_wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002384 else
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002385 rdma_wr->wr.next = &isert_cmd->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002386 } else {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002387 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2388
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002389 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2390 rdma_wr->remote_addr = isert_cmd->write_va + va_offset;
2391 rdma_wr->rkey = isert_cmd->write_stag;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002392 if (i + 1 == isert_cmd->rdma_wr_num)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002393 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002394 else
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002395 rdma_wr->wr.next = &isert_cmd->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002396 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002397
2398 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002399 rdma_wr, data_len, offset);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002400 ib_sge += ib_sge_cnt;
2401
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002402 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002403 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002404 data_left -= data_len;
2405 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002406
2407 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002408unmap_cmd:
2409 isert_unmap_data_buf(isert_conn, data);
2410
Vu Pham90ecc6e2013-08-28 23:23:33 +03002411 return ret;
2412}
2413
Sagi Grimberg10633c32014-12-07 13:12:04 +02002414static inline void
2415isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2416{
2417 u32 rkey;
2418
2419 memset(inv_wr, 0, sizeof(*inv_wr));
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002420 inv_wr->wr_cqe = NULL;
Sagi Grimberg10633c32014-12-07 13:12:04 +02002421 inv_wr->opcode = IB_WR_LOCAL_INV;
2422 inv_wr->ex.invalidate_rkey = mr->rkey;
2423
2424 /* Bump the key */
2425 rkey = ib_inc_rkey(mr->rkey);
2426 ib_update_fast_reg_key(mr, rkey);
2427}
2428
Vu Pham59464ef2013-08-28 23:23:35 +03002429static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002430isert_fast_reg_mr(struct isert_conn *isert_conn,
2431 struct fast_reg_descriptor *fr_desc,
2432 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002433 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002434 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002435{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002436 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002437 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002438 struct ib_mr *mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002439 struct ib_reg_wr reg_wr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002440 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002441 int ret, n;
Vu Pham59464ef2013-08-28 23:23:35 +03002442
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002443 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002444 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002445 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2446 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002447 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2448 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002449 return 0;
2450 }
2451
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002452 if (ind == ISERT_DATA_KEY_VALID)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002453 /* Registering data buffer */
2454 mr = fr_desc->data_mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002455 else
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002456 /* Registering protection buffer */
2457 mr = fr_desc->pi_ctx->prot_mr;
Vu Pham59464ef2013-08-28 23:23:35 +03002458
Sagi Grimberg10633c32014-12-07 13:12:04 +02002459 if (!(fr_desc->ind & ind)) {
2460 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002461 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002462 }
2463
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002464 n = ib_map_mr_sg(mr, mem->sg, mem->nents, PAGE_SIZE);
2465 if (unlikely(n != mem->nents)) {
2466 isert_err("failed to map mr sg (%d/%d)\n",
2467 n, mem->nents);
2468 return n < 0 ? n : -EINVAL;
2469 }
2470
2471 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2472 fr_desc, mem->nents, mem->offset);
2473
2474 reg_wr.wr.next = NULL;
2475 reg_wr.wr.opcode = IB_WR_REG_MR;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002476 reg_wr.wr.wr_cqe = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002477 reg_wr.wr.send_flags = 0;
2478 reg_wr.wr.num_sge = 0;
2479 reg_wr.mr = mr;
2480 reg_wr.key = mr->lkey;
2481 reg_wr.access = IB_ACCESS_LOCAL_WRITE;
Vu Pham59464ef2013-08-28 23:23:35 +03002482
2483 if (!wr)
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002484 wr = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002485 else
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002486 wr->next = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002487
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002488 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002489 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002490 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002491 return ret;
2492 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002493 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002494
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002495 sge->lkey = mr->lkey;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002496 sge->addr = mr->iova;
2497 sge->length = mr->length;
Vu Pham59464ef2013-08-28 23:23:35 +03002498
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002499 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2500 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002501
2502 return ret;
2503}
2504
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002505static inline void
2506isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2507 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002508{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002509 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002510 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2511 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2512 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002513 /*
2514 * At the moment we hard code those, but if in the future
2515 * the target core would like to use it, we will take it
2516 * from se_cmd.
2517 */
2518 domain->sig.dif.apptag_check_mask = 0xffff;
2519 domain->sig.dif.app_escape = true;
2520 domain->sig.dif.ref_escape = true;
2521 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2522 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2523 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002524};
2525
2526static int
2527isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2528{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002529 switch (se_cmd->prot_op) {
2530 case TARGET_PROT_DIN_INSERT:
2531 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002532 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002533 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002534 break;
2535 case TARGET_PROT_DOUT_INSERT:
2536 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002537 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002538 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002539 break;
2540 case TARGET_PROT_DIN_PASS:
2541 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002542 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2543 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002544 break;
2545 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002546 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002547 return -EINVAL;
2548 }
2549
2550 return 0;
2551}
2552
2553static inline u8
2554isert_set_prot_checks(u8 prot_checks)
2555{
2556 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2557 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2558 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2559}
2560
2561static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002562isert_reg_sig_mr(struct isert_conn *isert_conn,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002563 struct isert_cmd *isert_cmd,
Sagi Grimberg570db172014-12-02 16:57:31 +02002564 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002565{
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002566 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002567 struct ib_sig_handover_wr sig_wr;
2568 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002569 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2570 struct ib_sig_attrs sig_attrs;
2571 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002572
2573 memset(&sig_attrs, 0, sizeof(sig_attrs));
2574 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2575 if (ret)
2576 goto err;
2577
2578 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2579
2580 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002581 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002582 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002583 }
2584
2585 memset(&sig_wr, 0, sizeof(sig_wr));
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002586 sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002587 sig_wr.wr.wr_cqe = NULL;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002588 sig_wr.wr.sg_list = &isert_cmd->ib_sg[DATA];
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002589 sig_wr.wr.num_sge = 1;
2590 sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
2591 sig_wr.sig_attrs = &sig_attrs;
2592 sig_wr.sig_mr = pi_ctx->sig_mr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002593 if (se_cmd->t_prot_sg)
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002594 sig_wr.prot = &isert_cmd->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002595
2596 if (!wr)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002597 wr = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002598 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002599 wr->next = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002600
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002601 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002602 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002603 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002604 goto err;
2605 }
2606 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2607
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002608 isert_cmd->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2609 isert_cmd->ib_sg[SIG].addr = 0;
2610 isert_cmd->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002611 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2612 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2613 /*
2614 * We have protection guards on the wire
2615 * so we need to set a larget transfer
2616 */
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002617 isert_cmd->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002618
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002619 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002620 isert_cmd->ib_sg[SIG].addr, isert_cmd->ib_sg[SIG].length,
2621 isert_cmd->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002622err:
2623 return ret;
2624}
2625
Vu Pham59464ef2013-08-28 23:23:35 +03002626static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002627isert_handle_prot_cmd(struct isert_conn *isert_conn,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002628 struct isert_cmd *isert_cmd)
Sagi Grimberg570db172014-12-02 16:57:31 +02002629{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002630 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002631 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2632 int ret;
2633
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002634 if (!isert_cmd->fr_desc->pi_ctx) {
2635 ret = isert_create_pi_ctx(isert_cmd->fr_desc,
Sagi Grimberg570db172014-12-02 16:57:31 +02002636 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002637 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002638 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002639 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002640 isert_conn);
2641 return ret;
2642 }
2643 }
2644
2645 if (se_cmd->t_prot_sg) {
2646 ret = isert_map_data_buf(isert_conn, isert_cmd,
2647 se_cmd->t_prot_sg,
2648 se_cmd->t_prot_nents,
2649 se_cmd->prot_length,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002650 0,
2651 isert_cmd->iser_ib_op,
2652 &isert_cmd->prot);
Sagi Grimberg570db172014-12-02 16:57:31 +02002653 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002654 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002655 isert_conn);
2656 return ret;
2657 }
2658
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002659 memset(&isert_cmd->ib_sg[PROT], 0, sizeof(isert_cmd->ib_sg[PROT]));
2660 ret = isert_fast_reg_mr(isert_conn, isert_cmd->fr_desc,
2661 &isert_cmd->prot,
2662 ISERT_PROT_KEY_VALID,
2663 &isert_cmd->ib_sg[PROT]);
Sagi Grimberg570db172014-12-02 16:57:31 +02002664 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002665 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002666 isert_conn);
2667 goto unmap_prot_cmd;
2668 }
2669 }
2670
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002671 ret = isert_reg_sig_mr(isert_conn, isert_cmd, isert_cmd->fr_desc);
Sagi Grimberg570db172014-12-02 16:57:31 +02002672 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002673 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002674 isert_conn);
2675 goto unmap_prot_cmd;
2676 }
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002677 isert_cmd->fr_desc->ind |= ISERT_PROTECTED;
Sagi Grimberg570db172014-12-02 16:57:31 +02002678
2679 return 0;
2680
2681unmap_prot_cmd:
2682 if (se_cmd->t_prot_sg)
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002683 isert_unmap_data_buf(isert_conn, &isert_cmd->prot);
Sagi Grimberg570db172014-12-02 16:57:31 +02002684
2685 return ret;
2686}
2687
2688static int
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002689isert_reg_rdma(struct isert_cmd *isert_cmd, struct iscsi_conn *conn)
Vu Pham59464ef2013-08-28 23:23:35 +03002690{
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002691 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002692 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002693 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002694 struct fast_reg_descriptor *fr_desc = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002695 struct ib_rdma_wr *rdma_wr;
Sagi Grimberg570db172014-12-02 16:57:31 +02002696 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002697 u32 offset;
2698 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002699 unsigned long flags;
2700
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002701 offset = isert_cmd->iser_ib_op == ISER_IB_RDMA_READ ?
2702 cmd->write_data_done : 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002703 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2704 se_cmd->t_data_nents, se_cmd->data_length,
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002705 offset, isert_cmd->iser_ib_op,
2706 &isert_cmd->data);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002707 if (ret)
2708 return ret;
2709
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002710 if (isert_cmd->data.dma_nents != 1 ||
2711 isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002712 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2713 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002714 struct fast_reg_descriptor, list);
2715 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002716 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002717 isert_cmd->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002718 }
2719
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002720 ret = isert_fast_reg_mr(isert_conn, fr_desc, &isert_cmd->data,
2721 ISERT_DATA_KEY_VALID, &isert_cmd->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002722 if (ret)
2723 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002724
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002725 if (isert_prot_cmd(isert_conn, se_cmd)) {
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002726 ret = isert_handle_prot_cmd(isert_conn, isert_cmd);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002727 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002728 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002729
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002730 ib_sg = &isert_cmd->ib_sg[SIG];
Sagi Grimberg570db172014-12-02 16:57:31 +02002731 } else {
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002732 ib_sg = &isert_cmd->ib_sg[DATA];
Sagi Grimberg570db172014-12-02 16:57:31 +02002733 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002734
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002735 memcpy(&isert_cmd->s_ib_sge, ib_sg, sizeof(*ib_sg));
2736 isert_cmd->ib_sge = &isert_cmd->s_ib_sge;
2737 isert_cmd->rdma_wr_num = 1;
2738 memset(&isert_cmd->s_rdma_wr, 0, sizeof(isert_cmd->s_rdma_wr));
2739 isert_cmd->rdma_wr = &isert_cmd->s_rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002740
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002741 rdma_wr = &isert_cmd->s_rdma_wr;
2742 rdma_wr->wr.sg_list = &isert_cmd->s_ib_sge;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002743 rdma_wr->wr.num_sge = 1;
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002744 rdma_wr->wr.wr_cqe = &isert_cmd->tx_desc.tx_cqe;
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002745 if (isert_cmd->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002746 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_write_done;
2747
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002748 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2749 rdma_wr->remote_addr = isert_cmd->read_va;
2750 rdma_wr->rkey = isert_cmd->read_stag;
2751 rdma_wr->wr.send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002752 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002753 } else {
Christoph Hellwig9679cc52016-02-24 19:24:06 +02002754 isert_cmd->tx_desc.tx_cqe.done = isert_rdma_read_done;
2755
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002756 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2757 rdma_wr->remote_addr = isert_cmd->write_va;
2758 rdma_wr->rkey = isert_cmd->write_stag;
2759 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002760 }
2761
Vu Pham59464ef2013-08-28 23:23:35 +03002762 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002763
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002764unmap_cmd:
2765 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002766 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2767 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2768 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002769 }
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002770 isert_unmap_data_buf(isert_conn, &isert_cmd->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002771
Vu Pham59464ef2013-08-28 23:23:35 +03002772 return ret;
2773}
2774
2775static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002776isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2777{
2778 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002779 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002780 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002781 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002782 struct ib_send_wr *wr_failed;
2783 int rc;
2784
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002785 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002786 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002787
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002788 isert_cmd->iser_ib_op = ISER_IB_RDMA_WRITE;
2789 rc = device->reg_rdma_mem(isert_cmd, conn);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002790 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002791 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002792 return rc;
2793 }
2794
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002795 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002796 /*
2797 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2798 */
2799 isert_create_send_desc(isert_conn, isert_cmd,
2800 &isert_cmd->tx_desc);
2801 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2802 &isert_cmd->tx_desc.iscsi_header);
2803 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2804 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002805 &isert_cmd->tx_desc.send_wr);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002806 isert_cmd->s_rdma_wr.wr.next = &isert_cmd->tx_desc.send_wr;
2807 isert_cmd->rdma_wr_num += 1;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002808
2809 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2810 if (rc) {
2811 isert_err("ib_post_recv failed with %d\n", rc);
2812 return rc;
2813 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002814 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002815
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002816 rc = ib_post_send(isert_conn->qp, &isert_cmd->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002817 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002818 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002819
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002820 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002821 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002822 "READ\n", isert_cmd);
2823 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002824 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002825 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002826
Vu Pham90ecc6e2013-08-28 23:23:33 +03002827 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002828}
2829
2830static int
2831isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2832{
2833 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002834 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002835 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002836 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002837 struct ib_send_wr *wr_failed;
2838 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002839
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002840 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002841 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002842 isert_cmd->iser_ib_op = ISER_IB_RDMA_READ;
2843 rc = device->reg_rdma_mem(isert_cmd, conn);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002844 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002845 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002846 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002847 }
2848
Christoph Hellwigd1ca2ed2016-02-24 19:24:07 +02002849 rc = ib_post_send(isert_conn->qp, &isert_cmd->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002850 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002851 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002852
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002853 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002854 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002855
Vu Pham90ecc6e2013-08-28 23:23:33 +03002856 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002857}
2858
2859static int
2860isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2861{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002862 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2863 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002864
2865 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002866 case ISTATE_REMOVE:
2867 spin_lock_bh(&conn->cmd_lock);
2868 list_del_init(&cmd->i_conn_node);
2869 spin_unlock_bh(&conn->cmd_lock);
2870 isert_put_cmd(isert_cmd, true);
2871 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002872 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2873 ret = isert_put_nopin(cmd, conn, false);
2874 break;
2875 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002876 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002877 ret = -EINVAL;
2878 break;
2879 }
2880
2881 return ret;
2882}
2883
2884static int
2885isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2886{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002887 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002888 int ret;
2889
2890 switch (state) {
2891 case ISTATE_SEND_LOGOUTRSP:
2892 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002893 if (!ret)
2894 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002895 break;
2896 case ISTATE_SEND_NOPIN:
2897 ret = isert_put_nopin(cmd, conn, true);
2898 break;
2899 case ISTATE_SEND_TASKMGTRSP:
2900 ret = isert_put_tm_rsp(cmd, conn);
2901 break;
2902 case ISTATE_SEND_REJECT:
2903 ret = isert_put_reject(cmd, conn);
2904 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002905 case ISTATE_SEND_TEXTRSP:
2906 ret = isert_put_text_rsp(cmd, conn);
2907 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002908 case ISTATE_SEND_STATUS:
2909 /*
2910 * Special case for sending non GOOD SCSI status from TX thread
2911 * context during pre se_cmd excecution failure.
2912 */
2913 ret = isert_put_response(conn, cmd);
2914 break;
2915 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002916 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002917 ret = -EINVAL;
2918 break;
2919 }
2920
2921 return ret;
2922}
2923
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002924struct rdma_cm_id *
2925isert_setup_id(struct isert_np *isert_np)
2926{
2927 struct iscsi_np *np = isert_np->np;
2928 struct rdma_cm_id *id;
2929 struct sockaddr *sa;
2930 int ret;
2931
2932 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002933 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002934
Guy Shapirofa201052015-10-22 15:20:10 +03002935 id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002936 RDMA_PS_TCP, IB_QPT_RC);
2937 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002938 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002939 ret = PTR_ERR(id);
2940 goto out;
2941 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002942 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002943
2944 ret = rdma_bind_addr(id, sa);
2945 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002946 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002947 goto out_id;
2948 }
2949
Sagi Grimberg992607e2015-03-29 15:52:18 +03002950 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002951 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002952 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002953 goto out_id;
2954 }
2955
2956 return id;
2957out_id:
2958 rdma_destroy_id(id);
2959out:
2960 return ERR_PTR(ret);
2961}
2962
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002963static int
2964isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07002965 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002966{
2967 struct isert_np *isert_np;
2968 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002969 int ret;
2970
2971 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2972 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002973 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002974 return -ENOMEM;
2975 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002976 sema_init(&isert_np->sem, 0);
2977 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03002978 INIT_LIST_HEAD(&isert_np->accepted);
2979 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002980 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002981
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002982 /*
2983 * Setup the np->np_sockaddr from the passed sockaddr setup
2984 * in iscsi_target_configfs.c code..
2985 */
2986 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07002987 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002988
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002989 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002990 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002991 ret = PTR_ERR(isert_lid);
2992 goto out;
2993 }
2994
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03002995 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002996 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002997
2998 return 0;
2999
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003000out:
3001 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003002
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003003 return ret;
3004}
3005
3006static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003007isert_rdma_accept(struct isert_conn *isert_conn)
3008{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003009 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003010 struct rdma_conn_param cp;
3011 int ret;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003012 struct iser_cm_hdr rsp_hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003013
3014 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003015 cp.initiator_depth = isert_conn->initiator_depth;
3016 cp.retry_count = 7;
3017 cp.rnr_retry_count = 7;
3018
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003019 memset(&rsp_hdr, 0, sizeof(rsp_hdr));
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02003020 rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
3021 if (!isert_conn->snd_w_inv)
3022 rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003023 cp.private_data = (void *)&rsp_hdr;
3024 cp.private_data_len = sizeof(rsp_hdr);
3025
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003026 ret = rdma_accept(cm_id, &cp);
3027 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003028 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003029 return ret;
3030 }
3031
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003032 return 0;
3033}
3034
3035static int
3036isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3037{
Sagi Grimberg67004252015-03-29 15:52:09 +03003038 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003039 int ret;
3040
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003041 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003042 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3043 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003044 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003045 isert_conn);
3046 return ret;
3047 }
3048 reinit_completion(&isert_conn->login_req_comp);
3049
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003050 /*
3051 * For login requests after the first PDU, isert_rx_login_req() will
3052 * kick schedule_delayed_work(&conn->login_work) as the packet is
3053 * received, which turns this callback from iscsi_target_do_login_rx()
3054 * into a NOP.
3055 */
3056 if (!login->first_request)
3057 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003058
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003059 isert_rx_login_req(isert_conn);
3060
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003061 isert_info("before login_comp conn: %p\n", conn);
3062 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003063 if (ret)
3064 return ret;
3065
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003066 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003067
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003068 return 0;
3069}
3070
3071static void
3072isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3073 struct isert_conn *isert_conn)
3074{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003075 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003076 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003077
3078 conn->login_family = np->np_sockaddr.ss_family;
3079
Andy Groverdc58f762015-08-24 10:26:05 -07003080 conn->login_sockaddr = cm_route->addr.dst_addr;
3081 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003082}
3083
3084static int
3085isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3086{
Sagi Grimberg67004252015-03-29 15:52:09 +03003087 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003088 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003089 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003090
3091accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003092 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003093 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003094 return -ENODEV;
3095
3096 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003097 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003098 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003099 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003100 np->np_thread_state);
3101 /**
3102 * No point in stalling here when np_thread
3103 * is in state RESET/SHUTDOWN/EXIT - bail
3104 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003105 return -ENODEV;
3106 }
3107 spin_unlock_bh(&np->np_thread_lock);
3108
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003109 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003110 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003111 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003112 goto accept_wait;
3113 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003114 isert_conn = list_first_entry(&isert_np->pending,
3115 struct isert_conn, node);
3116 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003117 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003118
3119 conn->context = isert_conn;
3120 isert_conn->conn = conn;
Jenny Derzhavetzaea92982016-02-24 19:23:59 +02003121 isert_conn->state = ISER_CONN_BOUND;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003122
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003123 isert_set_conn_info(np, conn, isert_conn);
3124
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003125 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003126
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003127 return 0;
3128}
3129
3130static void
3131isert_free_np(struct iscsi_np *np)
3132{
Sagi Grimberg67004252015-03-29 15:52:09 +03003133 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003134 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003135
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003136 if (isert_np->cm_id)
3137 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003138
Sagi Grimberg268e6812014-12-02 16:57:36 +02003139 /*
3140 * FIXME: At this point we don't have a good way to insure
3141 * that at this point we don't have hanging connections that
3142 * completed RDMA establishment but didn't start iscsi login
3143 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003144 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003145 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003146 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003147 if (!list_empty(&isert_np->pending)) {
3148 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003149 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003150 &isert_np->pending,
3151 node) {
3152 isert_info("cleaning isert_conn %p state (%d)\n",
3153 isert_conn, isert_conn->state);
3154 isert_connect_release(isert_conn);
3155 }
3156 }
3157
3158 if (!list_empty(&isert_np->accepted)) {
3159 isert_info("Still have isert accepted connections\n");
3160 list_for_each_entry_safe(isert_conn, n,
3161 &isert_np->accepted,
3162 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003163 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003164 isert_conn, isert_conn->state);
3165 isert_connect_release(isert_conn);
3166 }
3167 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003168 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003169
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003170 np->np_context = NULL;
3171 kfree(isert_np);
3172}
3173
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003174static void isert_release_work(struct work_struct *work)
3175{
3176 struct isert_conn *isert_conn = container_of(work,
3177 struct isert_conn,
3178 release_work);
3179
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003180 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003181
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003182 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003183 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003184 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003185
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003186 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003187 isert_put_conn(isert_conn);
3188}
3189
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003190static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003191isert_wait4logout(struct isert_conn *isert_conn)
3192{
3193 struct iscsi_conn *conn = isert_conn->conn;
3194
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003195 isert_info("conn %p\n", isert_conn);
3196
Sagi Grimberg991bb762014-12-07 13:12:01 +02003197 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003198 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003199 wait_for_completion_timeout(&conn->conn_logout_comp,
3200 SECONDS_FOR_LOGOUT_COMP * HZ);
3201 }
3202}
3203
3204static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003205isert_wait4cmds(struct iscsi_conn *conn)
3206{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003207 isert_info("iscsi_conn %p\n", conn);
3208
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003209 if (conn->sess) {
3210 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3211 target_wait_for_sess_cmds(conn->sess->se_sess);
3212 }
3213}
3214
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003215/**
3216 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3217 * unsolicitate dataout
3218 * @conn: iscsi connection
3219 *
3220 * We might still have commands that are waiting for unsolicited
3221 * dataouts messages. We must put the extra reference on those
3222 * before blocking on the target_wait_for_session_cmds
3223 */
3224static void
3225isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3226{
3227 struct iscsi_cmd *cmd, *tmp;
3228 static LIST_HEAD(drop_cmd_list);
3229
3230 spin_lock_bh(&conn->cmd_lock);
3231 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3232 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3233 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3234 (cmd->write_data_done < cmd->se_cmd.data_length))
3235 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3236 }
3237 spin_unlock_bh(&conn->cmd_lock);
3238
3239 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3240 list_del_init(&cmd->i_conn_node);
3241 if (cmd->i_state != ISTATE_REMOVE) {
3242 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3243
3244 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3245 isert_put_cmd(isert_cmd, true);
3246 }
3247 }
3248}
3249
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003250static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003251{
3252 struct isert_conn *isert_conn = conn->context;
3253
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003254 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003255
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003256 mutex_lock(&isert_conn->mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003257 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003258 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003259
Sagi Grimberg572a1432016-02-24 19:24:09 +02003260 ib_drain_qp(isert_conn->qp);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003261 isert_put_unsol_pending_cmds(conn);
3262 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003263 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003264
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003265 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003266}
3267
3268static void isert_free_conn(struct iscsi_conn *conn)
3269{
3270 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003271
Sagi Grimberg572a1432016-02-24 19:24:09 +02003272 ib_drain_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003273 isert_put_conn(isert_conn);
3274}
3275
Varun Prakash187e84f2016-04-20 00:00:10 +05303276static void isert_get_rx_pdu(struct iscsi_conn *conn)
3277{
3278 struct completion comp;
3279
3280 init_completion(&comp);
3281
3282 wait_for_completion_interruptible(&comp);
3283}
3284
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003285static struct iscsit_transport iser_target_transport = {
3286 .name = "IB/iSER",
3287 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07003288 .rdma_shutdown = true,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003289 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003290 .owner = THIS_MODULE,
3291 .iscsit_setup_np = isert_setup_np,
3292 .iscsit_accept_np = isert_accept_np,
3293 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003294 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003295 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003296 .iscsit_get_login_rx = isert_get_login_rx,
3297 .iscsit_put_login_tx = isert_put_login_tx,
3298 .iscsit_immediate_queue = isert_immediate_queue,
3299 .iscsit_response_queue = isert_response_queue,
3300 .iscsit_get_dataout = isert_get_dataout,
3301 .iscsit_queue_data_in = isert_put_datain,
3302 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003303 .iscsit_aborted_task = isert_aborted_task,
Varun Prakash187e84f2016-04-20 00:00:10 +05303304 .iscsit_get_rx_pdu = isert_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003305 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003306};
3307
3308static int __init isert_init(void)
3309{
3310 int ret;
3311
Sagi Grimberg631af552015-01-25 19:09:50 +02003312 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3313 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003314 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003315 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003316 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003317 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003318 }
3319
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003320 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3321 WQ_UNBOUND_MAX_ACTIVE);
3322 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003323 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003324 ret = -ENOMEM;
3325 goto destroy_comp_wq;
3326 }
3327
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003328 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003329 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003330
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003331 return 0;
3332
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003333destroy_comp_wq:
3334 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003335
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003336 return ret;
3337}
3338
3339static void __exit isert_exit(void)
3340{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003341 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003342 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003343 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003344 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003345 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003346}
3347
3348MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003349MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003350MODULE_AUTHOR("nab@Linux-iSCSI.org");
3351MODULE_LICENSE("GPL");
3352
3353module_init(isert_init);
3354module_exit(isert_exit);