blob: abb3124f89bbd6e9e6ce7f125d728ccc8e221108 [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
52isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
53 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030054static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020055isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030056static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020057isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
58 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020059static int
60isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020061static int
62isert_rdma_post_recvl(struct isert_conn *isert_conn);
63static int
64isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020065struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030066
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +030067static void isert_release_work(struct work_struct *work);
68
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020069static inline bool
70isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
71{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020072 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020073 cmd->prot_op != TARGET_PROT_NORMAL);
74}
75
76
Vu Phamd40945d2013-08-28 23:23:34 +030077static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080078isert_qp_event_callback(struct ib_event *e, void *context)
79{
Sagi Grimberg67004252015-03-29 15:52:09 +030080 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080081
Sagi Grimbergea8a1612015-05-18 13:40:31 +030082 isert_err("%s (%d): conn %p\n",
83 ib_event_msg(e->event), e->event, isert_conn);
84
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080085 switch (e->event) {
86 case IB_EVENT_COMM_EST:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030087 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080088 break;
89 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020090 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080091 break;
92 default:
93 break;
94 }
95}
96
Sagi Grimberg40fc0692015-03-29 15:52:10 +030097static struct isert_comp *
98isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080099{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300100 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200101 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300102 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800103
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800104 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200105 for (i = 0; i < device->comps_used; i++)
106 if (device->comps[i].active_qps <
107 device->comps[min].active_qps)
108 min = i;
109 comp = &device->comps[min];
110 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300111 mutex_unlock(&device_list_mutex);
112
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200113 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200114 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300115
116 return comp;
117}
118
119static void
120isert_comp_put(struct isert_comp *comp)
121{
122 mutex_lock(&device_list_mutex);
123 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800124 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300125}
126
127static struct ib_qp *
128isert_create_qp(struct isert_conn *isert_conn,
129 struct isert_comp *comp,
130 struct rdma_cm_id *cma_id)
131{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300132 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300133 struct ib_qp_init_attr attr;
134 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800135
136 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
137 attr.event_handler = isert_qp_event_callback;
138 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200139 attr.send_cq = comp->cq;
140 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800141 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200142 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Or Gerlitz4a061b22015-12-18 10:59:46 +0200143 attr.cap.max_send_sge = device->ib_device->attrs.max_sge;
144 isert_conn->max_sge = min(device->ib_device->attrs.max_sge,
145 device->ib_device->attrs.max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800146 attr.cap.max_recv_sge = 1;
147 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
148 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200149 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200150 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800151
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300152 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800153 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200154 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300155 return ERR_PTR(ret);
156 }
157
158 return cma_id->qp;
159}
160
161static int
162isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
163{
164 struct isert_comp *comp;
165 int ret;
166
167 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300168 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
169 if (IS_ERR(isert_conn->qp)) {
170 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200171 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800172 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800173
174 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200175err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300176 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200177 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800178}
179
180static void
181isert_cq_event_callback(struct ib_event *e, void *context)
182{
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200183 isert_dbg("event: %d\n", e->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800184}
185
186static int
187isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
188{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300189 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300190 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800191 struct iser_rx_desc *rx_desc;
192 struct ib_sge *rx_sg;
193 u64 dma_addr;
194 int i, j;
195
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300196 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800197 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300198 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800199 goto fail;
200
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300201 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800202
203 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
204 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
205 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
206 if (ib_dma_mapping_error(ib_dev, dma_addr))
207 goto dma_map_fail;
208
209 rx_desc->dma_addr = dma_addr;
210
211 rx_sg = &rx_desc->rx_sg;
212 rx_sg->addr = rx_desc->dma_addr;
213 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600214 rx_sg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800215 }
216
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800217 return 0;
218
219dma_map_fail:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300220 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800221 for (j = 0; j < i; j++, rx_desc++) {
222 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
223 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
224 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300225 kfree(isert_conn->rx_descs);
226 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800227fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200228 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
229
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800230 return -ENOMEM;
231}
232
233static void
234isert_free_rx_descriptors(struct isert_conn *isert_conn)
235{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300236 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800237 struct iser_rx_desc *rx_desc;
238 int i;
239
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300240 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800241 return;
242
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300243 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800244 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
245 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
246 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
247 }
248
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300249 kfree(isert_conn->rx_descs);
250 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800251}
252
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200253static void isert_cq_work(struct work_struct *);
254static void isert_cq_callback(struct ib_cq *, void *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800255
Sagi Grimberg172369c2015-03-29 15:52:11 +0300256static void
257isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800258{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300259 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800260
Sagi Grimberg172369c2015-03-29 15:52:11 +0300261 for (i = 0; i < device->comps_used; i++) {
262 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300263
Sagi Grimberg172369c2015-03-29 15:52:11 +0300264 if (comp->cq) {
265 cancel_work_sync(&comp->work);
266 ib_destroy_cq(comp->cq);
267 }
Vu Pham59464ef2013-08-28 23:23:35 +0300268 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300269 kfree(device->comps);
270}
Vu Phamd40945d2013-08-28 23:23:34 +0300271
Sagi Grimberg172369c2015-03-29 15:52:11 +0300272static int
Or Gerlitz4a061b22015-12-18 10:59:46 +0200273isert_alloc_comps(struct isert_device *device)
Sagi Grimberg172369c2015-03-29 15:52:11 +0300274{
275 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200276
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200277 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300278 device->ib_device->num_comp_vectors));
279
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200280 isert_info("Using %d CQs, %s supports %d vectors support "
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200281 "Fast registration %d pi_capable %d\n",
282 device->comps_used, device->ib_device->name,
283 device->ib_device->num_comp_vectors, device->use_fastreg,
284 device->pi_capable);
285
286 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
287 GFP_KERNEL);
288 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200289 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800290 return -ENOMEM;
291 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800292
Or Gerlitz4a061b22015-12-18 10:59:46 +0200293 max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300294
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200295 for (i = 0; i < device->comps_used; i++) {
Matan Barak8e372102015-06-11 16:35:21 +0300296 struct ib_cq_init_attr cq_attr = {};
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200297 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800298
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200299 comp->device = device;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200300 INIT_WORK(&comp->work, isert_cq_work);
Matan Barak8e372102015-06-11 16:35:21 +0300301 cq_attr.cqe = max_cqe;
302 cq_attr.comp_vector = i;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200303 comp->cq = ib_create_cq(device->ib_device,
304 isert_cq_callback,
305 isert_cq_event_callback,
306 (void *)comp,
Matan Barak8e372102015-06-11 16:35:21 +0300307 &cq_attr);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200308 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300309 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200310 ret = PTR_ERR(comp->cq);
311 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800312 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800313 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800314
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200315 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Wei Yongjun94a71112013-10-29 09:56:34 +0800316 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800317 goto out_cq;
318 }
319
Sagi Grimberg172369c2015-03-29 15:52:11 +0300320 return 0;
321out_cq:
322 isert_free_comps(device);
323 return ret;
324}
325
326static int
327isert_create_device_ib_res(struct isert_device *device)
328{
Or Gerlitz4a061b22015-12-18 10:59:46 +0200329 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300330 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300331
Or Gerlitz4a061b22015-12-18 10:59:46 +0200332 isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
333 isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300334
335 /* asign function handlers */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200336 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
337 ib_dev->attrs.device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300338 device->use_fastreg = 1;
339 device->reg_rdma_mem = isert_reg_rdma;
340 device->unreg_rdma_mem = isert_unreg_rdma;
341 } else {
342 device->use_fastreg = 0;
343 device->reg_rdma_mem = isert_map_rdma;
344 device->unreg_rdma_mem = isert_unmap_cmd;
345 }
346
Or Gerlitz4a061b22015-12-18 10:59:46 +0200347 ret = isert_alloc_comps(device);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300348 if (ret)
349 return ret;
350
Or Gerlitz4a061b22015-12-18 10:59:46 +0200351 device->pd = ib_alloc_pd(ib_dev);
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300352 if (IS_ERR(device->pd)) {
353 ret = PTR_ERR(device->pd);
354 isert_err("failed to allocate pd, device %p, ret=%d\n",
355 device, ret);
356 goto out_cq;
357 }
358
Sagi Grimberg172369c2015-03-29 15:52:11 +0300359 /* Check signature cap */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200360 device->pi_capable = ib_dev->attrs.device_cap_flags &
Sagi Grimberg172369c2015-03-29 15:52:11 +0300361 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300362
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800363 return 0;
364
365out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300366 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800367 return ret;
368}
369
370static void
371isert_free_device_ib_res(struct isert_device *device)
372{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200373 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800374
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300375 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300376 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800377}
378
379static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300380isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800381{
382 mutex_lock(&device_list_mutex);
383 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200384 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800385 if (!device->refcount) {
386 isert_free_device_ib_res(device);
387 list_del(&device->dev_node);
388 kfree(device);
389 }
390 mutex_unlock(&device_list_mutex);
391}
392
393static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300394isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800395{
396 struct isert_device *device;
397 int ret;
398
399 mutex_lock(&device_list_mutex);
400 list_for_each_entry(device, &device_list, dev_node) {
401 if (device->ib_device->node_guid == cma_id->device->node_guid) {
402 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200403 isert_info("Found iser device %p refcount %d\n",
404 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800405 mutex_unlock(&device_list_mutex);
406 return device;
407 }
408 }
409
410 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
411 if (!device) {
412 mutex_unlock(&device_list_mutex);
413 return ERR_PTR(-ENOMEM);
414 }
415
416 INIT_LIST_HEAD(&device->dev_node);
417
418 device->ib_device = cma_id->device;
419 ret = isert_create_device_ib_res(device);
420 if (ret) {
421 kfree(device);
422 mutex_unlock(&device_list_mutex);
423 return ERR_PTR(ret);
424 }
425
426 device->refcount++;
427 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200428 isert_info("Created a new iser device %p refcount %d\n",
429 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800430 mutex_unlock(&device_list_mutex);
431
432 return device;
433}
434
Vu Pham59464ef2013-08-28 23:23:35 +0300435static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200436isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300437{
438 struct fast_reg_descriptor *fr_desc, *tmp;
439 int i = 0;
440
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300441 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300442 return;
443
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200444 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300445
446 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300447 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300448 list_del(&fr_desc->list);
Vu Pham59464ef2013-08-28 23:23:35 +0300449 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200450 if (fr_desc->pi_ctx) {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200451 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300452 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200453 kfree(fr_desc->pi_ctx);
454 }
Vu Pham59464ef2013-08-28 23:23:35 +0300455 kfree(fr_desc);
456 ++i;
457 }
458
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300459 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200460 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300461 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300462}
463
464static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200465isert_create_pi_ctx(struct fast_reg_descriptor *desc,
466 struct ib_device *device,
467 struct ib_pd *pd)
468{
Sagi Grimberg570db172014-12-02 16:57:31 +0200469 struct pi_context *pi_ctx;
470 int ret;
471
472 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
473 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200474 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200475 return -ENOMEM;
476 }
477
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300478 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
479 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimberg570db172014-12-02 16:57:31 +0200480 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200481 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200482 PTR_ERR(pi_ctx->prot_mr));
483 ret = PTR_ERR(pi_ctx->prot_mr);
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300484 goto err_pi_ctx;
Sagi Grimberg570db172014-12-02 16:57:31 +0200485 }
486 desc->ind |= ISERT_PROT_KEY_VALID;
487
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300488 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg570db172014-12-02 16:57:31 +0200489 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200490 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200491 PTR_ERR(pi_ctx->sig_mr));
492 ret = PTR_ERR(pi_ctx->sig_mr);
493 goto err_prot_mr;
494 }
495
496 desc->pi_ctx = pi_ctx;
497 desc->ind |= ISERT_SIG_KEY_VALID;
498 desc->ind &= ~ISERT_PROTECTED;
499
500 return 0;
501
502err_prot_mr:
Roland Dreierb2feda42015-05-29 23:12:10 -0700503 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg570db172014-12-02 16:57:31 +0200504err_pi_ctx:
Roland Dreierb2feda42015-05-29 23:12:10 -0700505 kfree(pi_ctx);
Sagi Grimberg570db172014-12-02 16:57:31 +0200506
507 return ret;
508}
509
510static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200511isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200512 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200513{
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300514 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
515 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200516 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200517 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200518 PTR_ERR(fr_desc->data_mr));
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300519 return PTR_ERR(fr_desc->data_mr);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200520 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200521 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200522
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200523 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200524
525 return 0;
526}
527
528static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200529isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300530{
531 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300532 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700533 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
534 struct se_node_acl *se_nacl = se_sess->se_node_acl;
535 int i, ret, tag_num;
536 /*
537 * Setup the number of FRMRs based upon the number of tags
538 * available to session in iscsi_target_locate_portal().
539 */
540 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
541 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300542
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300543 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700544 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300545 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
546 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200547 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300548 ret = -ENOMEM;
549 goto err;
550 }
551
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200552 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300553 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200554 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200555 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200556 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800557 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300558 goto err;
559 }
560
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300561 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
562 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300563 }
564
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200565 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300566 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300567
568 return 0;
569
570err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200571 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300572 return ret;
573}
574
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300575static void
576isert_init_conn(struct isert_conn *isert_conn)
577{
578 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300579 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300580 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300581 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300582 init_completion(&isert_conn->wait);
583 kref_init(&isert_conn->kref);
584 mutex_init(&isert_conn->mutex);
585 spin_lock_init(&isert_conn->pool_lock);
586 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300587 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300588}
589
590static void
591isert_free_login_buf(struct isert_conn *isert_conn)
592{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300593 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300594
595 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
596 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
597 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
598 ISCSI_DEF_MAX_RECV_SEG_LEN,
599 DMA_FROM_DEVICE);
600 kfree(isert_conn->login_buf);
601}
602
603static int
604isert_alloc_login_buf(struct isert_conn *isert_conn,
605 struct ib_device *ib_dev)
606{
607 int ret;
608
609 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
610 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
611 if (!isert_conn->login_buf) {
612 isert_err("Unable to allocate isert_conn->login_buf\n");
613 return -ENOMEM;
614 }
615
616 isert_conn->login_req_buf = isert_conn->login_buf;
617 isert_conn->login_rsp_buf = isert_conn->login_buf +
618 ISCSI_DEF_MAX_RECV_SEG_LEN;
619
620 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
621 isert_conn->login_buf, isert_conn->login_req_buf,
622 isert_conn->login_rsp_buf);
623
624 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
625 (void *)isert_conn->login_req_buf,
626 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
627
628 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
629 if (ret) {
630 isert_err("login_req_dma mapping error: %d\n", ret);
631 isert_conn->login_req_dma = 0;
632 goto out_login_buf;
633 }
634
635 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
636 (void *)isert_conn->login_rsp_buf,
637 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
638
639 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
640 if (ret) {
641 isert_err("login_rsp_dma mapping error: %d\n", ret);
642 isert_conn->login_rsp_dma = 0;
643 goto out_req_dma_map;
644 }
645
646 return 0;
647
648out_req_dma_map:
649 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
650 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
651out_login_buf:
652 kfree(isert_conn->login_buf);
653 return ret;
654}
655
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200656static void
657isert_set_nego_params(struct isert_conn *isert_conn,
658 struct rdma_conn_param *param)
659{
660 struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
661
662 /* Set max inflight RDMA READ requests */
663 isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
664 attr->max_qp_init_rd_atom);
665 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
666
667 if (param->private_data) {
668 u8 flags = *(u8 *)param->private_data;
669
670 /*
671 * use remote invalidation if the both initiator
672 * and the HCA support it
673 */
674 isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
675 (attr->device_cap_flags &
676 IB_DEVICE_MEM_MGT_EXTENSIONS);
677 if (isert_conn->snd_w_inv)
678 isert_info("Using remote invalidation\n");
679 }
680}
681
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800682static int
683isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
684{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200685 struct isert_np *isert_np = cma_id->context;
686 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800687 struct isert_conn *isert_conn;
688 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800689 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300690
691 spin_lock_bh(&np->np_thread_lock);
692 if (!np->enabled) {
693 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200694 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300695 return rdma_reject(cma_id, NULL, 0);
696 }
697 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800698
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200699 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800700 cma_id, cma_id->context);
701
702 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300703 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800704 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800705
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300706 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300707 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800708
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300709 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
710 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800711 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800712
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300713 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800714 if (IS_ERR(device)) {
715 ret = PTR_ERR(device);
716 goto out_rsp_dma_map;
717 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300718 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800719
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200720 isert_set_nego_params(isert_conn, &event->param.conn);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300721
Sagi Grimberg570db172014-12-02 16:57:31 +0200722 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800723 if (ret)
724 goto out_conn_dev;
725
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200726 ret = isert_rdma_post_recvl(isert_conn);
727 if (ret)
728 goto out_conn_dev;
729
730 ret = isert_rdma_accept(isert_conn);
731 if (ret)
732 goto out_conn_dev;
733
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300734 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300735 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300736 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800737
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800738 return 0;
739
740out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300741 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800742out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300743 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800744out:
745 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200746 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800747 return ret;
748}
749
750static void
751isert_connect_release(struct isert_conn *isert_conn)
752{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300753 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800754
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200755 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800756
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300757 BUG_ON(!device);
758
759 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200760 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300761
Sagi Grimberg19e20902014-12-02 16:57:26 +0200762 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300763 if (isert_conn->cm_id)
764 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200765
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300766 if (isert_conn->qp) {
767 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200768
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300769 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300770 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800771 }
772
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300773 if (isert_conn->login_buf)
774 isert_free_login_buf(isert_conn);
775
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300776 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800777
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300778 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800779}
780
781static void
782isert_connected_handler(struct rdma_cm_id *cma_id)
783{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200784 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300785 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300786
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200787 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200788
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300789 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300790 isert_conn->state = ISER_CONN_UP;
791 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300792 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300793
794 mutex_lock(&isert_np->mutex);
795 list_move_tail(&isert_conn->node, &isert_np->pending);
796 mutex_unlock(&isert_np->mutex);
797
798 isert_info("np %p: Allow accept_np to continue\n", isert_np);
799 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800800}
801
802static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300803isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800804{
805 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300806 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800807
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200808 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
809 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800810
811 isert_connect_release(isert_conn);
812}
813
814static void
815isert_put_conn(struct isert_conn *isert_conn)
816{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300817 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800818}
819
Sagi Grimberg954f2372014-12-02 16:57:17 +0200820/**
821 * isert_conn_terminate() - Initiate connection termination
822 * @isert_conn: isert connection struct
823 *
824 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200825 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200826 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200827 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200828 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300829 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200830 * safe to call multiple times.
831 */
832static void
833isert_conn_terminate(struct isert_conn *isert_conn)
834{
835 int err;
836
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200837 switch (isert_conn->state) {
838 case ISER_CONN_TERMINATING:
839 break;
840 case ISER_CONN_UP:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200841 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200842 isert_info("Terminating conn %p state %d\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200843 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200844 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300845 err = rdma_disconnect(isert_conn->cm_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200846 if (err)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200847 isert_warn("Failed rdma_disconnect isert_conn %p\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200848 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200849 break;
850 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200851 isert_warn("conn %p teminating in state %d\n",
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200852 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200853 }
854}
855
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700856static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200857isert_np_cma_handler(struct isert_np *isert_np,
858 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800859{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300860 isert_dbg("%s (%d): isert np %p\n",
861 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200862
863 switch (event) {
864 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300865 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200866 break;
867 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300868 isert_np->cm_id = isert_setup_id(isert_np);
869 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200870 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300871 isert_np, PTR_ERR(isert_np->cm_id));
872 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200873 }
874 break;
875 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200876 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200877 isert_np, event);
878 }
879
880 return -1;
881}
882
883static int
884isert_disconnected_handler(struct rdma_cm_id *cma_id,
885 enum rdma_cm_event_type event)
886{
887 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700888 struct isert_conn *isert_conn;
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300889 bool terminating = false;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700890
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300891 if (isert_np->cm_id == cma_id)
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200892 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700893
Sagi Grimberg19e20902014-12-02 16:57:26 +0200894 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800895
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300896 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300897 terminating = (isert_conn->state == ISER_CONN_TERMINATING);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200898 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300899 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200900
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300901 isert_info("conn %p completing wait\n", isert_conn);
902 complete(&isert_conn->wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700903
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300904 if (terminating)
905 goto out;
906
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300907 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300908 if (!list_empty(&isert_conn->node)) {
909 list_del_init(&isert_conn->node);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300910 isert_put_conn(isert_conn);
911 queue_work(isert_release_wq, &isert_conn->release_work);
912 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300913 mutex_unlock(&isert_np->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300914
915out:
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700916 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800917}
918
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300919static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200920isert_connect_error(struct rdma_cm_id *cma_id)
921{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200922 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200923
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300924 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300925 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200926 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300927
928 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200929}
930
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800931static int
932isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
933{
934 int ret = 0;
935
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300936 isert_info("%s (%d): status %d id %p np %p\n",
937 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200938 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800939
940 switch (event->event) {
941 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800942 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700943 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200944 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800945 break;
946 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800947 isert_connected_handler(cma_id);
948 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300949 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
950 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
951 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300952 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200953 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800954 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200955 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
956 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800957 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300958 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200959 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800960 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200961 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800962 break;
963 }
964
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800965 return ret;
966}
967
968static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300969isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800970{
971 struct ib_recv_wr *rx_wr, *rx_wr_failed;
972 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800973 struct iser_rx_desc *rx_desc;
974
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300975 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300976 rx_desc = &isert_conn->rx_descs[i];
977 rx_wr->wr_id = (uintptr_t)rx_desc;
978 rx_wr->sg_list = &rx_desc->rx_sg;
979 rx_wr->num_sge = 1;
980 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800981 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800982 rx_wr--;
983 rx_wr->next = NULL; /* mark end of work requests list */
984
985 isert_conn->post_recv_buf_count += count;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300986 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300987 &rx_wr_failed);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800988 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200989 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800990 isert_conn->post_recv_buf_count -= count;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800991 }
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300992
993 return ret;
994}
995
996static int
997isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
998{
999 struct ib_recv_wr *rx_wr_failed, rx_wr;
1000 int ret;
1001
1002 rx_wr.wr_id = (uintptr_t)rx_desc;
1003 rx_wr.sg_list = &rx_desc->rx_sg;
1004 rx_wr.num_sge = 1;
1005 rx_wr.next = NULL;
1006
1007 isert_conn->post_recv_buf_count++;
1008 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
1009 if (ret) {
1010 isert_err("ib_post_recv() failed with ret: %d\n", ret);
1011 isert_conn->post_recv_buf_count--;
1012 }
1013
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001014 return ret;
1015}
1016
1017static int
1018isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1019{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001020 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001021 struct ib_send_wr send_wr, *send_wr_failed;
1022 int ret;
1023
1024 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1025 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1026
1027 send_wr.next = NULL;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001028 send_wr.wr_id = (uintptr_t)tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001029 send_wr.sg_list = tx_desc->tx_sg;
1030 send_wr.num_sge = tx_desc->num_sge;
1031 send_wr.opcode = IB_WR_SEND;
1032 send_wr.send_flags = IB_SEND_SIGNALED;
1033
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001034 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001035 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001036 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001037
1038 return ret;
1039}
1040
1041static void
1042isert_create_send_desc(struct isert_conn *isert_conn,
1043 struct isert_cmd *isert_cmd,
1044 struct iser_tx_desc *tx_desc)
1045{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001046 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001047 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001048
1049 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1050 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1051
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001052 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
1053 tx_desc->iser_header.flags = ISCSI_CTRL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001054
1055 tx_desc->num_sge = 1;
1056 tx_desc->isert_cmd = isert_cmd;
1057
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001058 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1059 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001060 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001061 }
1062}
1063
1064static int
1065isert_init_tx_hdrs(struct isert_conn *isert_conn,
1066 struct iser_tx_desc *tx_desc)
1067{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001068 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001069 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001070 u64 dma_addr;
1071
1072 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1073 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1074 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001075 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001076 return -ENOMEM;
1077 }
1078
1079 tx_desc->dma_addr = dma_addr;
1080 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1081 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001082 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001083
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001084 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1085 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1086 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001087
1088 return 0;
1089}
1090
1091static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001092isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001093 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001094{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001095 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1096
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001097 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001098 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001099
1100 if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
1101 send_wr->opcode = IB_WR_SEND_WITH_INV;
1102 send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
1103 } else {
1104 send_wr->opcode = IB_WR_SEND;
1105 }
1106
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001107 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001108 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001109 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001110}
1111
1112static int
1113isert_rdma_post_recvl(struct isert_conn *isert_conn)
1114{
1115 struct ib_recv_wr rx_wr, *rx_wr_fail;
1116 struct ib_sge sge;
1117 int ret;
1118
1119 memset(&sge, 0, sizeof(struct ib_sge));
1120 sge.addr = isert_conn->login_req_dma;
1121 sge.length = ISER_RX_LOGIN_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001122 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001123
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001124 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001125 sge.addr, sge.length, sge.lkey);
1126
1127 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001128 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001129 rx_wr.sg_list = &sge;
1130 rx_wr.num_sge = 1;
1131
1132 isert_conn->post_recv_buf_count++;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001133 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001134 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001135 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001136 isert_conn->post_recv_buf_count--;
1137 }
1138
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001139 return ret;
1140}
1141
1142static int
1143isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1144 u32 length)
1145{
1146 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001147 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001148 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001149 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001150 int ret;
1151
1152 isert_create_send_desc(isert_conn, NULL, tx_desc);
1153
1154 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1155 sizeof(struct iscsi_hdr));
1156
1157 isert_init_tx_hdrs(isert_conn, tx_desc);
1158
1159 if (length > 0) {
1160 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1161
1162 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1163 length, DMA_TO_DEVICE);
1164
1165 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1166
1167 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1168 length, DMA_TO_DEVICE);
1169
1170 tx_dsg->addr = isert_conn->login_rsp_dma;
1171 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001172 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001173 tx_desc->num_sge = 2;
1174 }
1175 if (!login->login_failed) {
1176 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001177 if (!conn->sess->sess_ops->SessionType &&
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001178 isert_conn->device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001179 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001180 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001181 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001182 " fastreg pool\n", isert_conn);
1183 return ret;
1184 }
1185 }
1186
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001187 ret = isert_alloc_rx_descriptors(isert_conn);
1188 if (ret)
1189 return ret;
1190
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001191 ret = isert_post_recvm(isert_conn,
1192 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001193 if (ret)
1194 return ret;
1195
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001196 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001197 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001198 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001199 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001200 goto post_send;
1201 }
1202
1203 ret = isert_rdma_post_recvl(isert_conn);
1204 if (ret)
1205 return ret;
1206 }
1207post_send:
1208 ret = isert_post_send(isert_conn, tx_desc);
1209 if (ret)
1210 return ret;
1211
1212 return 0;
1213}
1214
1215static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001216isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001217{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001218 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1219 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001220 struct iscsi_conn *conn = isert_conn->conn;
1221 struct iscsi_login *login = conn->conn_login;
1222 int size;
1223
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001224 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001225
1226 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001227
1228 if (login->first_request) {
1229 struct iscsi_login_req *login_req =
1230 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1231 /*
1232 * Setup the initial iscsi_login values from the leading
1233 * login request PDU.
1234 */
1235 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1236 login->current_stage =
1237 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1238 >> 2;
1239 login->version_min = login_req->min_version;
1240 login->version_max = login_req->max_version;
1241 memcpy(login->isid, login_req->isid, 6);
1242 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1243 login->init_task_tag = login_req->itt;
1244 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1245 login->cid = be16_to_cpu(login_req->cid);
1246 login->tsih = be16_to_cpu(login_req->tsih);
1247 }
1248
1249 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1250
1251 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001252 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1253 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1254 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001255 memcpy(login->req_buf, &rx_desc->data[0], size);
1256
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001257 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001258 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001259 return;
1260 }
1261 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001262}
1263
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001264static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001265*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001266{
Sagi Grimberg67004252015-03-29 15:52:09 +03001267 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001268 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001269 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001270
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001271 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001272 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001273 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001274 return NULL;
1275 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001276 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001277 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001278 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001279 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001280
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001281 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001282}
1283
1284static int
1285isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001286 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1287 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001288{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001289 struct iscsi_conn *conn = isert_conn->conn;
1290 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001291 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1292 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001293 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001294
1295 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1296 if (rc < 0)
1297 return rc;
1298
1299 imm_data = cmd->immediate_data;
1300 imm_data_len = cmd->first_burst_len;
1301 unsol_data = cmd->unsolicited_data;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001302 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001303
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001304 if (imm_data && imm_data_len == data_len)
1305 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001306 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1307 if (rc < 0) {
1308 return 0;
1309 } else if (rc > 0) {
1310 dump_payload = true;
1311 goto sequence_cmd;
1312 }
1313
1314 if (!imm_data)
1315 return 0;
1316
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001317 if (imm_data_len != data_len) {
1318 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1319 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1320 &rx_desc->data[0], imm_data_len);
1321 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1322 sg_nents, imm_data_len);
1323 } else {
1324 sg_init_table(&isert_cmd->sg, 1);
1325 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1326 cmd->se_cmd.t_data_nents = 1;
1327 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1328 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1329 imm_data_len);
1330 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001331
1332 cmd->write_data_done += imm_data_len;
1333
1334 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1335 spin_lock_bh(&cmd->istate_lock);
1336 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1337 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1338 spin_unlock_bh(&cmd->istate_lock);
1339 }
1340
1341sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001342 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001343
1344 if (!rc && dump_payload == false && unsol_data)
1345 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001346 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001347 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001348
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001349 return 0;
1350}
1351
1352static int
1353isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1354 struct iser_rx_desc *rx_desc, unsigned char *buf)
1355{
1356 struct scatterlist *sg_start;
1357 struct iscsi_conn *conn = isert_conn->conn;
1358 struct iscsi_cmd *cmd = NULL;
1359 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1360 u32 unsol_data_len = ntoh24(hdr->dlength);
1361 int rc, sg_nents, sg_off, page_off;
1362
1363 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1364 if (rc < 0)
1365 return rc;
1366 else if (!cmd)
1367 return 0;
1368 /*
1369 * FIXME: Unexpected unsolicited_data out
1370 */
1371 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001372 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001373 dump_stack();
1374 return -1;
1375 }
1376
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001377 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1378 "write_data_done: %u, data_length: %u\n",
1379 unsol_data_len, cmd->write_data_done,
1380 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001381
1382 sg_off = cmd->write_data_done / PAGE_SIZE;
1383 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1384 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1385 page_off = cmd->write_data_done % PAGE_SIZE;
1386 /*
1387 * FIXME: Non page-aligned unsolicited_data out
1388 */
1389 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001390 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001391 dump_stack();
1392 return -1;
1393 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001394 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1395 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1396 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001397
1398 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1399 unsol_data_len);
1400
1401 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1402 if (rc < 0)
1403 return rc;
1404
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001405 /*
1406 * multiple data-outs on the same command can arrive -
1407 * so post the buffer before hand
1408 */
1409 rc = isert_post_recv(isert_conn, rx_desc);
1410 if (rc) {
1411 isert_err("ib_post_recv failed with %d\n", rc);
1412 return rc;
1413 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001414 return 0;
1415}
1416
1417static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001418isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001419 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1420 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001421{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001422 struct iscsi_conn *conn = isert_conn->conn;
1423 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1424 int rc;
1425
1426 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1427 if (rc < 0)
1428 return rc;
1429 /*
1430 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1431 */
1432
1433 return iscsit_process_nop_out(conn, cmd, hdr);
1434}
1435
1436static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001437isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001438 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1439 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001440{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001441 struct iscsi_conn *conn = isert_conn->conn;
1442 u32 payload_length = ntoh24(hdr->dlength);
1443 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001444 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001445
1446 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1447 if (rc < 0)
1448 return rc;
1449
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001450 if (payload_length) {
1451 text_in = kzalloc(payload_length, GFP_KERNEL);
1452 if (!text_in) {
1453 isert_err("Unable to allocate text_in of payload_length: %u\n",
1454 payload_length);
1455 return -ENOMEM;
1456 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001457 }
1458 cmd->text_in_ptr = text_in;
1459
1460 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1461
1462 return iscsit_process_text_cmd(conn, cmd, hdr);
1463}
1464
1465static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001466isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1467 uint32_t read_stag, uint64_t read_va,
1468 uint32_t write_stag, uint64_t write_va)
1469{
1470 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1471 struct iscsi_conn *conn = isert_conn->conn;
1472 struct iscsi_cmd *cmd;
1473 struct isert_cmd *isert_cmd;
1474 int ret = -EINVAL;
1475 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1476
Sagi Grimbergfb140272015-03-29 15:52:08 +03001477 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001478 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001479 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001480 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001481 return 0;
1482 }
1483
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001484 switch (opcode) {
1485 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001486 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001487 if (!cmd)
1488 break;
1489
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001490 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001491 isert_cmd->read_stag = read_stag;
1492 isert_cmd->read_va = read_va;
1493 isert_cmd->write_stag = write_stag;
1494 isert_cmd->write_va = write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001495 isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001496
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001497 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001498 rx_desc, (unsigned char *)hdr);
1499 break;
1500 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001501 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001502 if (!cmd)
1503 break;
1504
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001505 isert_cmd = iscsit_priv_cmd(cmd);
1506 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001507 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001508 break;
1509 case ISCSI_OP_SCSI_DATA_OUT:
1510 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1511 (unsigned char *)hdr);
1512 break;
1513 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001514 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001515 if (!cmd)
1516 break;
1517
1518 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1519 (unsigned char *)hdr);
1520 break;
1521 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001522 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001523 if (!cmd)
1524 break;
1525
1526 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001527 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001528 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001529 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001530 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001531 else
1532 cmd = isert_allocate_cmd(conn, rx_desc);
1533
1534 if (!cmd)
1535 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001536
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001537 isert_cmd = iscsit_priv_cmd(cmd);
1538 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001539 rx_desc, (struct iscsi_text *)hdr);
1540 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001541 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001542 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001543 dump_stack();
1544 break;
1545 }
1546
1547 return ret;
1548}
1549
1550static void
1551isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1552{
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001553 struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001554 uint64_t read_va = 0, write_va = 0;
1555 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001556
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001557 switch (iser_ctrl->flags & 0xF0) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001558 case ISCSI_CTRL:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001559 if (iser_ctrl->flags & ISER_RSV) {
1560 read_stag = be32_to_cpu(iser_ctrl->read_stag);
1561 read_va = be64_to_cpu(iser_ctrl->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001562 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1563 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001564 }
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001565 if (iser_ctrl->flags & ISER_WSV) {
1566 write_stag = be32_to_cpu(iser_ctrl->write_stag);
1567 write_va = be64_to_cpu(iser_ctrl->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001568 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1569 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001570 }
1571
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001572 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001573 break;
1574 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001575 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001576 break;
1577 default:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001578 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001579 break;
1580 }
1581
Bart Van Assche6c760b32015-10-22 11:14:18 -07001582 isert_rx_opcode(isert_conn, rx_desc,
1583 read_stag, read_va, write_stag, write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001584}
1585
1586static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001587isert_rcv_completion(struct iser_rx_desc *desc,
1588 struct isert_conn *isert_conn,
1589 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001590{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001591 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001592 struct iscsi_hdr *hdr;
1593 u64 rx_dma;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001594 int rx_buflen;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001595
1596 if ((char *)desc == isert_conn->login_req_buf) {
1597 rx_dma = isert_conn->login_req_dma;
1598 rx_buflen = ISER_RX_LOGIN_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001599 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001600 rx_dma, rx_buflen);
1601 } else {
1602 rx_dma = desc->dma_addr;
1603 rx_buflen = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001604 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001605 rx_dma, rx_buflen);
1606 }
1607
1608 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1609
1610 hdr = &desc->iscsi_header;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001611 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001612 hdr->opcode, hdr->itt, hdr->flags,
1613 (int)(xfer_len - ISER_HEADERS_LEN));
1614
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001615 if ((char *)desc == isert_conn->login_req_buf) {
1616 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1617 if (isert_conn->conn) {
1618 struct iscsi_login *login = isert_conn->conn->conn_login;
1619
1620 if (login && !login->first_request)
1621 isert_rx_login_req(isert_conn);
1622 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001623 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001624 complete(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001625 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001626 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001627 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001628 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001629
1630 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1631 DMA_FROM_DEVICE);
1632
1633 isert_conn->post_recv_buf_count--;
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{
1683 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001684
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001685 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001686
1687 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001688 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001689 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001690 }
1691
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001692 if (wr->rdma_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001693 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001694 kfree(wr->rdma_wr);
1695 wr->rdma_wr = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001696 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001697
Vu Pham90ecc6e2013-08-28 23:23:33 +03001698 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001699 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001700 kfree(wr->ib_sge);
1701 wr->ib_sge = NULL;
1702 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001703}
1704
1705static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001706isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001707{
1708 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001709
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001710 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001711
1712 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001713 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001714 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1715 isert_unmap_data_buf(isert_conn, &wr->prot);
1716 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1717 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001718 spin_lock_bh(&isert_conn->pool_lock);
1719 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1720 spin_unlock_bh(&isert_conn->pool_lock);
Vu Pham59464ef2013-08-28 23:23:35 +03001721 wr->fr_desc = NULL;
1722 }
1723
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001724 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001725 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001726 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001727 }
1728
1729 wr->ib_sge = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001730 wr->rdma_wr = NULL;
Vu Pham59464ef2013-08-28 23:23:35 +03001731}
1732
1733static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001734isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001735{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001736 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001737 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001738 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001739 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001740 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001741
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001742 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001743
1744 switch (cmd->iscsi_opcode) {
1745 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001746 spin_lock_bh(&conn->cmd_lock);
1747 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001748 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001749 spin_unlock_bh(&conn->cmd_lock);
1750
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001751 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001752 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001753 /*
1754 * Check for special case during comp_err where
1755 * WRITE_PENDING has been handed off from core,
1756 * but requires an extra target_put_sess_cmd()
1757 * before transport_generic_free_cmd() below.
1758 */
1759 if (comp_err &&
1760 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1761 struct se_cmd *se_cmd = &cmd->se_cmd;
1762
Bart Van Asscheafc16602015-04-27 13:52:36 +02001763 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001764 }
1765 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001766
Vu Phamd40945d2013-08-28 23:23:34 +03001767 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001768 transport_generic_free_cmd(&cmd->se_cmd, 0);
1769 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001770 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001771 spin_lock_bh(&conn->cmd_lock);
1772 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001773 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001774 spin_unlock_bh(&conn->cmd_lock);
1775
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001776 transport_generic_free_cmd(&cmd->se_cmd, 0);
1777 break;
1778 case ISCSI_OP_REJECT:
1779 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001780 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001781 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1782 /* If the continue bit is on, keep the command alive */
1783 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1784 break;
1785
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001786 spin_lock_bh(&conn->cmd_lock);
1787 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001788 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001789 spin_unlock_bh(&conn->cmd_lock);
1790
1791 /*
1792 * Handle special case for REJECT when iscsi_add_reject*() has
1793 * overwritten the original iscsi_opcode assignment, and the
1794 * associated cmd->se_cmd needs to be released.
1795 */
1796 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001797 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001798 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001799 transport_generic_free_cmd(&cmd->se_cmd, 0);
1800 break;
1801 }
1802 /*
1803 * Fall-through
1804 */
1805 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001806 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001807 break;
1808 }
1809}
1810
1811static void
1812isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1813{
1814 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001815 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001816 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1817 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1818 tx_desc->dma_addr = 0;
1819 }
1820}
1821
1822static void
1823isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001824 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001825{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001826 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001827 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001828 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1829 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1830 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001831 }
1832
1833 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001834 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001835}
1836
Sagi Grimberg96b79732014-03-17 12:52:18 +02001837static int
1838isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1839{
1840 struct ib_mr_status mr_status;
1841 int ret;
1842
1843 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1844 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001845 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001846 goto fail_mr_status;
1847 }
1848
1849 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1850 u64 sec_offset_err;
1851 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1852
1853 switch (mr_status.sig_err.err_type) {
1854 case IB_SIG_BAD_GUARD:
1855 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1856 break;
1857 case IB_SIG_BAD_REFTAG:
1858 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1859 break;
1860 case IB_SIG_BAD_APPTAG:
1861 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1862 break;
1863 }
1864 sec_offset_err = mr_status.sig_err.sig_err_offset;
1865 do_div(sec_offset_err, block_size);
1866 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1867
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001868 isert_err("PI error found type %d at sector 0x%llx "
1869 "expected 0x%x vs actual 0x%x\n",
1870 mr_status.sig_err.err_type,
1871 (unsigned long long)se_cmd->bad_sector,
1872 mr_status.sig_err.expected,
1873 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001874 ret = 1;
1875 }
1876
1877fail_mr_status:
1878 return ret;
1879}
1880
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001881static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001882isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1883 struct isert_cmd *isert_cmd)
1884{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001885 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001886 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001887 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001888 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001889 struct isert_device *device = isert_conn->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001890 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001891
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001892 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001893 ret = isert_check_pi_status(se_cmd,
1894 wr->fr_desc->pi_ctx->sig_mr);
1895 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001896 }
1897
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001898 device->unreg_rdma_mem(isert_cmd, isert_conn);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001899 wr->rdma_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001900 if (ret)
1901 transport_send_check_condition_and_sense(se_cmd,
1902 se_cmd->pi_err, 0);
1903 else
1904 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001905}
1906
1907static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001908isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1909 struct isert_cmd *isert_cmd)
1910{
1911 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001912 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001913 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001914 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001915 struct isert_device *device = isert_conn->device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001916 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001917
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001918 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001919 ret = isert_check_pi_status(se_cmd,
1920 wr->fr_desc->pi_ctx->sig_mr);
1921 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001922 }
1923
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001924 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001925 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001926 cmd->write_data_done = wr->data.len;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001927 wr->rdma_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001928
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001929 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001930 spin_lock_bh(&cmd->istate_lock);
1931 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1932 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1933 spin_unlock_bh(&cmd->istate_lock);
1934
Sagi Grimberg364189f2015-03-29 15:52:03 +03001935 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001936 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001937 transport_send_check_condition_and_sense(se_cmd,
1938 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001939 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001940 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001941 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001942}
1943
1944static void
1945isert_do_control_comp(struct work_struct *work)
1946{
1947 struct isert_cmd *isert_cmd = container_of(work,
1948 struct isert_cmd, comp_work);
1949 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001950 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001951 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001952
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001953 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1954
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001955 switch (cmd->i_state) {
1956 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001957 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001958 case ISTATE_SEND_REJECT: /* FALLTHRU */
1959 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001960 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001961 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1962 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001963 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001964 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001965 iscsit_logout_post_handler(cmd, cmd->conn);
1966 break;
1967 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001968 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001969 dump_stack();
1970 break;
1971 }
1972}
1973
1974static void
1975isert_response_completion(struct iser_tx_desc *tx_desc,
1976 struct isert_cmd *isert_cmd,
1977 struct isert_conn *isert_conn,
1978 struct ib_device *ib_dev)
1979{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001980 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001981
1982 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001983 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001984 cmd->i_state == ISTATE_SEND_REJECT ||
1985 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001986 isert_unmap_tx_desc(tx_desc, ib_dev);
1987
1988 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1989 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1990 return;
1991 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001992
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001993 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001994 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001995}
1996
1997static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001998isert_snd_completion(struct iser_tx_desc *tx_desc,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001999 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002000{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002001 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002002 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2003 struct isert_rdma_wr *wr;
2004
2005 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002006 isert_unmap_tx_desc(tx_desc, ib_dev);
2007 return;
2008 }
2009 wr = &isert_cmd->rdma_wr;
2010
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002011 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
2012
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002013 switch (wr->iser_ib_op) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002014 case ISER_IB_SEND:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002015 isert_response_completion(tx_desc, isert_cmd,
2016 isert_conn, ib_dev);
2017 break;
2018 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002019 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002020 break;
2021 case ISER_IB_RDMA_READ:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002022 isert_completion_rdma_read(tx_desc, isert_cmd);
2023 break;
2024 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002025 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002026 dump_stack();
2027 break;
2028 }
2029}
2030
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002031/**
2032 * is_isert_tx_desc() - Indicate if the completion wr_id
2033 * is a TX descriptor or not.
2034 * @isert_conn: iser connection
2035 * @wr_id: completion WR identifier
2036 *
2037 * Since we cannot rely on wc opcode in FLUSH errors
2038 * we must work around it by checking if the wr_id address
2039 * falls in the iser connection rx_descs buffer. If so
2040 * it is an RX descriptor, otherwize it is a TX.
2041 */
2042static inline bool
2043is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002044{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002045 void *start = isert_conn->rx_descs;
2046 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002047
2048 if (wr_id >= start && wr_id < start + len)
2049 return false;
2050
2051 return true;
2052}
2053
2054static void
2055isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
2056{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002057 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002058 isert_info("conn %p completing wait_comp_err\n",
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002059 isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002060 complete(&isert_conn->wait_comp_err);
Nicholas Bellingered4520a2014-12-07 13:12:05 +02002061 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002062 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002063 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002064 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002065
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002066 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2067 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002068 if (!isert_cmd)
2069 isert_unmap_tx_desc(desc, ib_dev);
2070 else
2071 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002072 } else {
2073 isert_conn->post_recv_buf_count--;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002074 if (!isert_conn->post_recv_buf_count)
2075 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002076 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002077}
2078
2079static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002080isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002081{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002082 struct isert_conn *isert_conn;
2083 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002084 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002085
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002086 isert_conn = wc->qp->qp_context;
2087 if (likely(wc->status == IB_WC_SUCCESS)) {
2088 if (wc->opcode == IB_WC_RECV) {
2089 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002090 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002091 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002092 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002093 isert_snd_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002094 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002095 } else {
2096 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002097 isert_err("%s (%d): wr id %llx vend_err %x\n",
2098 ib_wc_status_msg(wc->status), wc->status,
2099 wc->wr_id, wc->vendor_err);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002100 else
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002101 isert_dbg("%s (%d): wr id %llx\n",
2102 ib_wc_status_msg(wc->status), wc->status,
2103 wc->wr_id);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002104
2105 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2106 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002107 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002108}
2109
2110static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002111isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002112{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002113 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002114 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002115 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002116 struct ib_wc *const wcs = comp->wcs;
2117 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002118
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002119 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2120 for (i = 0; i < n; i++)
2121 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002122
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002123 completed += n;
2124 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002125 break;
2126 }
2127
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002128 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002129}
2130
2131static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002132isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002133{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002134 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002135
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002136 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002137}
2138
2139static int
2140isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2141{
2142 struct ib_send_wr *wr_failed;
2143 int ret;
2144
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002145 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2146 if (ret) {
2147 isert_err("ib_post_recv failed with %d\n", ret);
2148 return ret;
2149 }
2150
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002151 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002152 &wr_failed);
2153 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002154 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002155 return ret;
2156 }
2157 return ret;
2158}
2159
2160static int
2161isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2162{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002163 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002164 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002165 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2166 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2167 &isert_cmd->tx_desc.iscsi_header;
2168
2169 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2170 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2171 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2172 /*
2173 * Attach SENSE DATA payload to iSCSI Response PDU
2174 */
2175 if (cmd->se_cmd.sense_buffer &&
2176 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2177 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002178 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002179 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002180 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002181 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002182
2183 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2184 cmd->sense_buffer);
2185 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2186
2187 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2188 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002189 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002190
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002191 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2192 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002193 DMA_TO_DEVICE);
2194
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002195 isert_cmd->pdu_buf_len = pdu_len;
2196 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2197 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002198 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002199 isert_cmd->tx_desc.num_sge = 2;
2200 }
2201
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002202 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002203
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002204 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002205
2206 return isert_post_response(isert_conn, isert_cmd);
2207}
2208
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002209static void
2210isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2211{
2212 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002213 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002214 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002215
2216 spin_lock_bh(&conn->cmd_lock);
2217 if (!list_empty(&cmd->i_conn_node))
2218 list_del_init(&cmd->i_conn_node);
2219 spin_unlock_bh(&conn->cmd_lock);
2220
2221 if (cmd->data_direction == DMA_TO_DEVICE)
2222 iscsit_stop_dataout_timer(cmd);
2223
2224 device->unreg_rdma_mem(isert_cmd, isert_conn);
2225}
2226
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002227static enum target_prot_op
2228isert_get_sup_prot_ops(struct iscsi_conn *conn)
2229{
Sagi Grimberg67004252015-03-29 15:52:09 +03002230 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002231 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002232
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002233 if (conn->tpg->tpg_attrib.t10_pi) {
2234 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002235 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002236 isert_conn->pi_support = true;
2237 return TARGET_PROT_ALL;
2238 }
2239 }
2240
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002241 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002242 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002243
2244 return TARGET_PROT_NORMAL;
2245}
2246
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002247static int
2248isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2249 bool nopout_response)
2250{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002251 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002252 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002253 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2254
2255 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2256 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2257 &isert_cmd->tx_desc.iscsi_header,
2258 nopout_response);
2259 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002260 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002261
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002262 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002263
2264 return isert_post_response(isert_conn, isert_cmd);
2265}
2266
2267static int
2268isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2269{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002270 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002271 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002272 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2273
2274 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2275 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2276 &isert_cmd->tx_desc.iscsi_header);
2277 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002278 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002279
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002280 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002281
2282 return isert_post_response(isert_conn, isert_cmd);
2283}
2284
2285static int
2286isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2287{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002288 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002289 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002290 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2291
2292 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2293 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2294 &isert_cmd->tx_desc.iscsi_header);
2295 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002296 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002297
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002298 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002299
2300 return isert_post_response(isert_conn, isert_cmd);
2301}
2302
2303static int
2304isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2305{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002306 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002307 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002308 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002309 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002310 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002311 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2312 struct iscsi_reject *hdr =
2313 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002314
2315 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002316 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002317 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002318
2319 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002320 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002321 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2322 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002323 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2324 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002325 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002326 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002327 isert_cmd->tx_desc.num_sge = 2;
2328
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002329 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002330
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002331 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002332
2333 return isert_post_response(isert_conn, isert_cmd);
2334}
2335
2336static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002337isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2338{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002339 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002340 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002341 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2342 struct iscsi_text_rsp *hdr =
2343 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2344 u32 txt_rsp_len;
2345 int rc;
2346
2347 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002348 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002349 if (rc < 0)
2350 return rc;
2351
2352 txt_rsp_len = rc;
2353 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2354
2355 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002356 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002357 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002358 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2359 void *txt_rsp_buf = cmd->buf_ptr;
2360
2361 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2362 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2363
2364 isert_cmd->pdu_buf_len = txt_rsp_len;
2365 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2366 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002367 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002368 isert_cmd->tx_desc.num_sge = 2;
2369 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002370 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002371
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002372 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002373
2374 return isert_post_response(isert_conn, isert_cmd);
2375}
2376
2377static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002378isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002379 struct ib_sge *ib_sge, struct ib_rdma_wr *rdma_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002380 u32 data_left, u32 offset)
2381{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002382 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002383 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002384 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002385 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002386 u32 sg_off, page_off;
2387 int i = 0, sg_nents;
2388
2389 sg_off = offset / PAGE_SIZE;
2390 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2391 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2392 page_off = offset % PAGE_SIZE;
2393
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002394 rdma_wr->wr.sg_list = ib_sge;
2395 rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002396 /*
2397 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2398 */
2399 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002400 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2401 "page_off: %u\n",
2402 (unsigned long long)tmp_sg->dma_address,
2403 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002404
2405 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2406 ib_sge->length = min_t(u32, data_left,
2407 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002408 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002409
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002410 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2411 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002412 page_off = 0;
2413 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002414 if (!data_left)
2415 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002416 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002417 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002418 }
2419
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002420 rdma_wr->wr.num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002421 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002422 rdma_wr->wr.sg_list, rdma_wr->wr.num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002423
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002424 return rdma_wr->wr.num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002425}
2426
2427static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002428isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2429 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002430{
2431 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002432 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002433 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002434 struct isert_data_buf *data = &wr->data;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002435 struct ib_rdma_wr *rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002436 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002437 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2438 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002439
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002440 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002441
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002442 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2443 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2444 se_cmd->t_data_nents, se_cmd->data_length,
2445 offset, wr->iser_ib_op, &wr->data);
2446 if (ret)
2447 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002448
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002449 data_left = data->len;
2450 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002451
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002452 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002453 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002454 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002455 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002456 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002457 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002458 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002459
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002460 wr->rdma_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2461 wr->rdma_wr = kzalloc(sizeof(struct ib_rdma_wr) * wr->rdma_wr_num,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002462 GFP_KERNEL);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002463 if (!wr->rdma_wr) {
2464 isert_dbg("Unable to allocate wr->rdma_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002465 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002466 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002467 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002468
2469 wr->isert_cmd = isert_cmd;
2470 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002471
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002472 for (i = 0; i < wr->rdma_wr_num; i++) {
2473 rdma_wr = &isert_cmd->rdma_wr.rdma_wr[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002474 data_len = min(data_left, rdma_write_max);
2475
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002476 rdma_wr->wr.send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002477 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002478 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2479 rdma_wr->remote_addr = isert_cmd->read_va + offset;
2480 rdma_wr->rkey = isert_cmd->read_stag;
2481 if (i + 1 == wr->rdma_wr_num)
2482 rdma_wr->wr.next = &isert_cmd->tx_desc.send_wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002483 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002484 rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002485 } else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002486 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2487 rdma_wr->remote_addr = isert_cmd->write_va + va_offset;
2488 rdma_wr->rkey = isert_cmd->write_stag;
2489 if (i + 1 == wr->rdma_wr_num)
2490 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002491 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002492 rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002493 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002494
2495 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002496 rdma_wr, data_len, offset);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002497 ib_sge += ib_sge_cnt;
2498
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002499 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002500 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002501 data_left -= data_len;
2502 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002503
2504 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002505unmap_cmd:
2506 isert_unmap_data_buf(isert_conn, data);
2507
Vu Pham90ecc6e2013-08-28 23:23:33 +03002508 return ret;
2509}
2510
Sagi Grimberg10633c32014-12-07 13:12:04 +02002511static inline void
2512isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2513{
2514 u32 rkey;
2515
2516 memset(inv_wr, 0, sizeof(*inv_wr));
2517 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2518 inv_wr->opcode = IB_WR_LOCAL_INV;
2519 inv_wr->ex.invalidate_rkey = mr->rkey;
2520
2521 /* Bump the key */
2522 rkey = ib_inc_rkey(mr->rkey);
2523 ib_update_fast_reg_key(mr, rkey);
2524}
2525
Vu Pham59464ef2013-08-28 23:23:35 +03002526static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002527isert_fast_reg_mr(struct isert_conn *isert_conn,
2528 struct fast_reg_descriptor *fr_desc,
2529 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002530 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002531 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002532{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002533 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002534 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002535 struct ib_mr *mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002536 struct ib_reg_wr reg_wr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002537 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002538 int ret, n;
Vu Pham59464ef2013-08-28 23:23:35 +03002539
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002540 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002541 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002542 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2543 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002544 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2545 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002546 return 0;
2547 }
2548
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002549 if (ind == ISERT_DATA_KEY_VALID)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002550 /* Registering data buffer */
2551 mr = fr_desc->data_mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002552 else
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002553 /* Registering protection buffer */
2554 mr = fr_desc->pi_ctx->prot_mr;
Vu Pham59464ef2013-08-28 23:23:35 +03002555
Sagi Grimberg10633c32014-12-07 13:12:04 +02002556 if (!(fr_desc->ind & ind)) {
2557 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002558 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002559 }
2560
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002561 n = ib_map_mr_sg(mr, mem->sg, mem->nents, PAGE_SIZE);
2562 if (unlikely(n != mem->nents)) {
2563 isert_err("failed to map mr sg (%d/%d)\n",
2564 n, mem->nents);
2565 return n < 0 ? n : -EINVAL;
2566 }
2567
2568 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2569 fr_desc, mem->nents, mem->offset);
2570
2571 reg_wr.wr.next = NULL;
2572 reg_wr.wr.opcode = IB_WR_REG_MR;
2573 reg_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
2574 reg_wr.wr.send_flags = 0;
2575 reg_wr.wr.num_sge = 0;
2576 reg_wr.mr = mr;
2577 reg_wr.key = mr->lkey;
2578 reg_wr.access = IB_ACCESS_LOCAL_WRITE;
Vu Pham59464ef2013-08-28 23:23:35 +03002579
2580 if (!wr)
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002581 wr = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002582 else
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002583 wr->next = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002584
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002585 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002586 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002587 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002588 return ret;
2589 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002590 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002591
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002592 sge->lkey = mr->lkey;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002593 sge->addr = mr->iova;
2594 sge->length = mr->length;
Vu Pham59464ef2013-08-28 23:23:35 +03002595
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002596 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2597 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002598
2599 return ret;
2600}
2601
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002602static inline void
2603isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2604 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002605{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002606 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002607 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2608 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2609 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002610 /*
2611 * At the moment we hard code those, but if in the future
2612 * the target core would like to use it, we will take it
2613 * from se_cmd.
2614 */
2615 domain->sig.dif.apptag_check_mask = 0xffff;
2616 domain->sig.dif.app_escape = true;
2617 domain->sig.dif.ref_escape = true;
2618 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2619 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2620 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002621};
2622
2623static int
2624isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2625{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002626 switch (se_cmd->prot_op) {
2627 case TARGET_PROT_DIN_INSERT:
2628 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002629 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002630 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002631 break;
2632 case TARGET_PROT_DOUT_INSERT:
2633 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002634 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002635 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002636 break;
2637 case TARGET_PROT_DIN_PASS:
2638 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002639 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2640 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002641 break;
2642 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002643 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002644 return -EINVAL;
2645 }
2646
2647 return 0;
2648}
2649
2650static inline u8
2651isert_set_prot_checks(u8 prot_checks)
2652{
2653 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2654 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2655 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2656}
2657
2658static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002659isert_reg_sig_mr(struct isert_conn *isert_conn,
2660 struct se_cmd *se_cmd,
2661 struct isert_rdma_wr *rdma_wr,
2662 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002663{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002664 struct ib_sig_handover_wr sig_wr;
2665 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002666 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2667 struct ib_sig_attrs sig_attrs;
2668 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002669
2670 memset(&sig_attrs, 0, sizeof(sig_attrs));
2671 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2672 if (ret)
2673 goto err;
2674
2675 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2676
2677 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002678 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002679 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002680 }
2681
2682 memset(&sig_wr, 0, sizeof(sig_wr));
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002683 sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
2684 sig_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
2685 sig_wr.wr.sg_list = &rdma_wr->ib_sg[DATA];
2686 sig_wr.wr.num_sge = 1;
2687 sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
2688 sig_wr.sig_attrs = &sig_attrs;
2689 sig_wr.sig_mr = pi_ctx->sig_mr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002690 if (se_cmd->t_prot_sg)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002691 sig_wr.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002692
2693 if (!wr)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002694 wr = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002695 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002696 wr->next = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002697
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002698 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002699 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002700 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002701 goto err;
2702 }
2703 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2704
Sagi Grimberg570db172014-12-02 16:57:31 +02002705 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2706 rdma_wr->ib_sg[SIG].addr = 0;
2707 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002708 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2709 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2710 /*
2711 * We have protection guards on the wire
2712 * so we need to set a larget transfer
2713 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002714 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002715
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002716 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002717 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2718 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002719err:
2720 return ret;
2721}
2722
Vu Pham59464ef2013-08-28 23:23:35 +03002723static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002724isert_handle_prot_cmd(struct isert_conn *isert_conn,
2725 struct isert_cmd *isert_cmd,
2726 struct isert_rdma_wr *wr)
2727{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002728 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002729 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2730 int ret;
2731
2732 if (!wr->fr_desc->pi_ctx) {
2733 ret = isert_create_pi_ctx(wr->fr_desc,
2734 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002735 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002736 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002737 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002738 isert_conn);
2739 return ret;
2740 }
2741 }
2742
2743 if (se_cmd->t_prot_sg) {
2744 ret = isert_map_data_buf(isert_conn, isert_cmd,
2745 se_cmd->t_prot_sg,
2746 se_cmd->t_prot_nents,
2747 se_cmd->prot_length,
2748 0, wr->iser_ib_op, &wr->prot);
2749 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002750 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002751 isert_conn);
2752 return ret;
2753 }
2754
2755 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2756 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2757 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2758 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002759 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002760 isert_conn);
2761 goto unmap_prot_cmd;
2762 }
2763 }
2764
2765 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2766 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002767 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002768 isert_conn);
2769 goto unmap_prot_cmd;
2770 }
2771 wr->fr_desc->ind |= ISERT_PROTECTED;
2772
2773 return 0;
2774
2775unmap_prot_cmd:
2776 if (se_cmd->t_prot_sg)
2777 isert_unmap_data_buf(isert_conn, &wr->prot);
2778
2779 return ret;
2780}
2781
2782static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002783isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2784 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002785{
2786 struct se_cmd *se_cmd = &cmd->se_cmd;
2787 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002788 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002789 struct fast_reg_descriptor *fr_desc = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002790 struct ib_rdma_wr *rdma_wr;
Sagi Grimberg570db172014-12-02 16:57:31 +02002791 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002792 u32 offset;
2793 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002794 unsigned long flags;
2795
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002796 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2797
2798 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2799 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2800 se_cmd->t_data_nents, se_cmd->data_length,
2801 offset, wr->iser_ib_op, &wr->data);
2802 if (ret)
2803 return ret;
2804
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002805 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002806 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2807 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002808 struct fast_reg_descriptor, list);
2809 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002810 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002811 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002812 }
2813
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002814 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002815 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002816 if (ret)
2817 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002818
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002819 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002820 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002821 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002822 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002823
Sagi Grimberg570db172014-12-02 16:57:31 +02002824 ib_sg = &wr->ib_sg[SIG];
2825 } else {
2826 ib_sg = &wr->ib_sg[DATA];
2827 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002828
Sagi Grimberg570db172014-12-02 16:57:31 +02002829 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002830 wr->ib_sge = &wr->s_ib_sge;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002831 wr->rdma_wr_num = 1;
2832 memset(&wr->s_rdma_wr, 0, sizeof(wr->s_rdma_wr));
2833 wr->rdma_wr = &wr->s_rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002834 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002835
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002836 rdma_wr = &isert_cmd->rdma_wr.s_rdma_wr;
2837 rdma_wr->wr.sg_list = &wr->s_ib_sge;
2838 rdma_wr->wr.num_sge = 1;
2839 rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002840 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002841 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2842 rdma_wr->remote_addr = isert_cmd->read_va;
2843 rdma_wr->rkey = isert_cmd->read_stag;
2844 rdma_wr->wr.send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002845 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002846 } else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002847 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2848 rdma_wr->remote_addr = isert_cmd->write_va;
2849 rdma_wr->rkey = isert_cmd->write_stag;
2850 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002851 }
2852
Vu Pham59464ef2013-08-28 23:23:35 +03002853 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002854
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002855unmap_cmd:
2856 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002857 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2858 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2859 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002860 }
2861 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002862
Vu Pham59464ef2013-08-28 23:23:35 +03002863 return ret;
2864}
2865
2866static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002867isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2868{
2869 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002870 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002871 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002872 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002873 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002874 struct ib_send_wr *wr_failed;
2875 int rc;
2876
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002877 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002878 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002879
Vu Pham90ecc6e2013-08-28 23:23:33 +03002880 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002881 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002882 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002883 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002884 return rc;
2885 }
2886
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002887 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002888 /*
2889 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2890 */
2891 isert_create_send_desc(isert_conn, isert_cmd,
2892 &isert_cmd->tx_desc);
2893 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2894 &isert_cmd->tx_desc.iscsi_header);
2895 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2896 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002897 &isert_cmd->tx_desc.send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002898 isert_cmd->rdma_wr.s_rdma_wr.wr.next = &isert_cmd->tx_desc.send_wr;
2899 wr->rdma_wr_num += 1;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002900
2901 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2902 if (rc) {
2903 isert_err("ib_post_recv failed with %d\n", rc);
2904 return rc;
2905 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002906 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002907
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002908 rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002909 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002910 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002911
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002912 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002913 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002914 "READ\n", isert_cmd);
2915 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002916 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002917 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002918
Vu Pham90ecc6e2013-08-28 23:23:33 +03002919 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002920}
2921
2922static int
2923isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2924{
2925 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002926 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002927 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002928 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002929 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002930 struct ib_send_wr *wr_failed;
2931 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002932
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002933 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002934 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002935 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002936 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002937 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002938 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002939 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002940 }
2941
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002942 rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002943 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002944 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002945
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002946 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002947 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002948
Vu Pham90ecc6e2013-08-28 23:23:33 +03002949 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002950}
2951
2952static int
2953isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2954{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002955 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2956 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002957
2958 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002959 case ISTATE_REMOVE:
2960 spin_lock_bh(&conn->cmd_lock);
2961 list_del_init(&cmd->i_conn_node);
2962 spin_unlock_bh(&conn->cmd_lock);
2963 isert_put_cmd(isert_cmd, true);
2964 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002965 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2966 ret = isert_put_nopin(cmd, conn, false);
2967 break;
2968 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002969 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002970 ret = -EINVAL;
2971 break;
2972 }
2973
2974 return ret;
2975}
2976
2977static int
2978isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2979{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002980 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002981 int ret;
2982
2983 switch (state) {
2984 case ISTATE_SEND_LOGOUTRSP:
2985 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002986 if (!ret)
2987 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002988 break;
2989 case ISTATE_SEND_NOPIN:
2990 ret = isert_put_nopin(cmd, conn, true);
2991 break;
2992 case ISTATE_SEND_TASKMGTRSP:
2993 ret = isert_put_tm_rsp(cmd, conn);
2994 break;
2995 case ISTATE_SEND_REJECT:
2996 ret = isert_put_reject(cmd, conn);
2997 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002998 case ISTATE_SEND_TEXTRSP:
2999 ret = isert_put_text_rsp(cmd, conn);
3000 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003001 case ISTATE_SEND_STATUS:
3002 /*
3003 * Special case for sending non GOOD SCSI status from TX thread
3004 * context during pre se_cmd excecution failure.
3005 */
3006 ret = isert_put_response(conn, cmd);
3007 break;
3008 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003009 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003010 ret = -EINVAL;
3011 break;
3012 }
3013
3014 return ret;
3015}
3016
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003017struct rdma_cm_id *
3018isert_setup_id(struct isert_np *isert_np)
3019{
3020 struct iscsi_np *np = isert_np->np;
3021 struct rdma_cm_id *id;
3022 struct sockaddr *sa;
3023 int ret;
3024
3025 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003026 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003027
Guy Shapirofa201052015-10-22 15:20:10 +03003028 id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003029 RDMA_PS_TCP, IB_QPT_RC);
3030 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003031 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003032 ret = PTR_ERR(id);
3033 goto out;
3034 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003035 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003036
3037 ret = rdma_bind_addr(id, sa);
3038 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003039 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003040 goto out_id;
3041 }
3042
Sagi Grimberg992607e2015-03-29 15:52:18 +03003043 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003044 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003045 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003046 goto out_id;
3047 }
3048
3049 return id;
3050out_id:
3051 rdma_destroy_id(id);
3052out:
3053 return ERR_PTR(ret);
3054}
3055
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003056static int
3057isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07003058 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003059{
3060 struct isert_np *isert_np;
3061 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003062 int ret;
3063
3064 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3065 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003066 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003067 return -ENOMEM;
3068 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003069 sema_init(&isert_np->sem, 0);
3070 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003071 INIT_LIST_HEAD(&isert_np->accepted);
3072 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003073 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003074
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003075 /*
3076 * Setup the np->np_sockaddr from the passed sockaddr setup
3077 * in iscsi_target_configfs.c code..
3078 */
3079 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07003080 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003081
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003082 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003083 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003084 ret = PTR_ERR(isert_lid);
3085 goto out;
3086 }
3087
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003088 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003089 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003090
3091 return 0;
3092
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003093out:
3094 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003095
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003096 return ret;
3097}
3098
3099static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003100isert_rdma_accept(struct isert_conn *isert_conn)
3101{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003102 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003103 struct rdma_conn_param cp;
3104 int ret;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003105 struct iser_cm_hdr rsp_hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003106
3107 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003108 cp.initiator_depth = isert_conn->initiator_depth;
3109 cp.retry_count = 7;
3110 cp.rnr_retry_count = 7;
3111
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003112 memset(&rsp_hdr, 0, sizeof(rsp_hdr));
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02003113 rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
3114 if (!isert_conn->snd_w_inv)
3115 rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003116 cp.private_data = (void *)&rsp_hdr;
3117 cp.private_data_len = sizeof(rsp_hdr);
3118
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003119 ret = rdma_accept(cm_id, &cp);
3120 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003121 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003122 return ret;
3123 }
3124
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003125 return 0;
3126}
3127
3128static int
3129isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3130{
Sagi Grimberg67004252015-03-29 15:52:09 +03003131 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003132 int ret;
3133
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003134 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003135 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3136 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003137 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003138 isert_conn);
3139 return ret;
3140 }
3141 reinit_completion(&isert_conn->login_req_comp);
3142
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003143 /*
3144 * For login requests after the first PDU, isert_rx_login_req() will
3145 * kick schedule_delayed_work(&conn->login_work) as the packet is
3146 * received, which turns this callback from iscsi_target_do_login_rx()
3147 * into a NOP.
3148 */
3149 if (!login->first_request)
3150 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003151
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003152 isert_rx_login_req(isert_conn);
3153
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003154 isert_info("before login_comp conn: %p\n", conn);
3155 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003156 if (ret)
3157 return ret;
3158
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003159 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003160
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003161 return 0;
3162}
3163
3164static void
3165isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3166 struct isert_conn *isert_conn)
3167{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003168 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003169 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003170
3171 conn->login_family = np->np_sockaddr.ss_family;
3172
Andy Groverdc58f762015-08-24 10:26:05 -07003173 conn->login_sockaddr = cm_route->addr.dst_addr;
3174 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003175}
3176
3177static int
3178isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3179{
Sagi Grimberg67004252015-03-29 15:52:09 +03003180 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003181 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003182 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003183
3184accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003185 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003186 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003187 return -ENODEV;
3188
3189 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003190 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003191 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003192 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003193 np->np_thread_state);
3194 /**
3195 * No point in stalling here when np_thread
3196 * is in state RESET/SHUTDOWN/EXIT - bail
3197 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003198 return -ENODEV;
3199 }
3200 spin_unlock_bh(&np->np_thread_lock);
3201
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003202 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003203 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003204 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003205 goto accept_wait;
3206 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003207 isert_conn = list_first_entry(&isert_np->pending,
3208 struct isert_conn, node);
3209 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003210 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003211
3212 conn->context = isert_conn;
3213 isert_conn->conn = conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003214
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003215 isert_set_conn_info(np, conn, isert_conn);
3216
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003217 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003218
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003219 return 0;
3220}
3221
3222static void
3223isert_free_np(struct iscsi_np *np)
3224{
Sagi Grimberg67004252015-03-29 15:52:09 +03003225 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003226 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003227
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003228 if (isert_np->cm_id)
3229 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003230
Sagi Grimberg268e6812014-12-02 16:57:36 +02003231 /*
3232 * FIXME: At this point we don't have a good way to insure
3233 * that at this point we don't have hanging connections that
3234 * completed RDMA establishment but didn't start iscsi login
3235 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003236 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003237 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003238 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003239 if (!list_empty(&isert_np->pending)) {
3240 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003241 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003242 &isert_np->pending,
3243 node) {
3244 isert_info("cleaning isert_conn %p state (%d)\n",
3245 isert_conn, isert_conn->state);
3246 isert_connect_release(isert_conn);
3247 }
3248 }
3249
3250 if (!list_empty(&isert_np->accepted)) {
3251 isert_info("Still have isert accepted connections\n");
3252 list_for_each_entry_safe(isert_conn, n,
3253 &isert_np->accepted,
3254 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003255 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003256 isert_conn, isert_conn->state);
3257 isert_connect_release(isert_conn);
3258 }
3259 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003260 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003261
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003262 np->np_context = NULL;
3263 kfree(isert_np);
3264}
3265
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003266static void isert_release_work(struct work_struct *work)
3267{
3268 struct isert_conn *isert_conn = container_of(work,
3269 struct isert_conn,
3270 release_work);
3271
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003272 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003273
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003274 wait_for_completion(&isert_conn->wait);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003275
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003276 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003277 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003278 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003279
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003280 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003281 isert_put_conn(isert_conn);
3282}
3283
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003284static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003285isert_wait4logout(struct isert_conn *isert_conn)
3286{
3287 struct iscsi_conn *conn = isert_conn->conn;
3288
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003289 isert_info("conn %p\n", isert_conn);
3290
Sagi Grimberg991bb762014-12-07 13:12:01 +02003291 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003292 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003293 wait_for_completion_timeout(&conn->conn_logout_comp,
3294 SECONDS_FOR_LOGOUT_COMP * HZ);
3295 }
3296}
3297
3298static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003299isert_wait4cmds(struct iscsi_conn *conn)
3300{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003301 isert_info("iscsi_conn %p\n", conn);
3302
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003303 if (conn->sess) {
3304 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3305 target_wait_for_sess_cmds(conn->sess->se_sess);
3306 }
3307}
3308
3309static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003310isert_wait4flush(struct isert_conn *isert_conn)
3311{
3312 struct ib_recv_wr *bad_wr;
3313
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003314 isert_info("conn %p\n", isert_conn);
3315
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003316 init_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003317 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3318 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003319 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003320 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003321 return;
3322 }
3323
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003324 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003325}
3326
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003327/**
3328 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3329 * unsolicitate dataout
3330 * @conn: iscsi connection
3331 *
3332 * We might still have commands that are waiting for unsolicited
3333 * dataouts messages. We must put the extra reference on those
3334 * before blocking on the target_wait_for_session_cmds
3335 */
3336static void
3337isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3338{
3339 struct iscsi_cmd *cmd, *tmp;
3340 static LIST_HEAD(drop_cmd_list);
3341
3342 spin_lock_bh(&conn->cmd_lock);
3343 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3344 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3345 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3346 (cmd->write_data_done < cmd->se_cmd.data_length))
3347 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3348 }
3349 spin_unlock_bh(&conn->cmd_lock);
3350
3351 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3352 list_del_init(&cmd->i_conn_node);
3353 if (cmd->i_state != ISTATE_REMOVE) {
3354 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3355
3356 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3357 isert_put_cmd(isert_cmd, true);
3358 }
3359 }
3360}
3361
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003362static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003363{
3364 struct isert_conn *isert_conn = conn->context;
3365
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003366 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003367
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003368 mutex_lock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003369 /*
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003370 * Only wait for wait_comp_err if the isert_conn made it
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003371 * into full feature phase..
3372 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003373 if (isert_conn->state == ISER_CONN_INIT) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003374 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003375 return;
3376 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003377 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003378 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003379
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003380 isert_wait4flush(isert_conn);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003381 isert_put_unsol_pending_cmds(conn);
3382 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003383 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003384
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003385 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003386}
3387
3388static void isert_free_conn(struct iscsi_conn *conn)
3389{
3390 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003391
Sagi Grimberg524630d2015-06-04 19:49:21 +03003392 isert_wait4flush(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003393 isert_put_conn(isert_conn);
3394}
3395
3396static struct iscsit_transport iser_target_transport = {
3397 .name = "IB/iSER",
3398 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003399 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003400 .owner = THIS_MODULE,
3401 .iscsit_setup_np = isert_setup_np,
3402 .iscsit_accept_np = isert_accept_np,
3403 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003404 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003405 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003406 .iscsit_get_login_rx = isert_get_login_rx,
3407 .iscsit_put_login_tx = isert_put_login_tx,
3408 .iscsit_immediate_queue = isert_immediate_queue,
3409 .iscsit_response_queue = isert_response_queue,
3410 .iscsit_get_dataout = isert_get_dataout,
3411 .iscsit_queue_data_in = isert_put_datain,
3412 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003413 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003414 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003415};
3416
3417static int __init isert_init(void)
3418{
3419 int ret;
3420
Sagi Grimberg631af552015-01-25 19:09:50 +02003421 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3422 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003423 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003424 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003425 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003426 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003427 }
3428
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003429 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3430 WQ_UNBOUND_MAX_ACTIVE);
3431 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003432 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003433 ret = -ENOMEM;
3434 goto destroy_comp_wq;
3435 }
3436
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003437 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003438 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003439
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003440 return 0;
3441
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003442destroy_comp_wq:
3443 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003444
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003445 return ret;
3446}
3447
3448static void __exit isert_exit(void)
3449{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003450 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003451 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003452 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003453 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003454 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003455}
3456
3457MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003458MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003459MODULE_AUTHOR("nab@Linux-iSCSI.org");
3460MODULE_LICENSE("GPL");
3461
3462module_init(isert_init);
3463module_exit(isert_exit);