blob: 320d3a55249c1f14ecc8c026527ae06d77704fad [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)
Nicholas Bellinger373a4cd2016-01-06 10:25:43 -0800349 goto out;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300350
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 Bellinger373a4cd2016-01-06 10:25:43 -0800367out:
368 if (ret > 0)
369 ret = -EINVAL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800370 return ret;
371}
372
373static void
374isert_free_device_ib_res(struct isert_device *device)
375{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200376 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800377
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300378 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300379 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800380}
381
382static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300383isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800384{
385 mutex_lock(&device_list_mutex);
386 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200387 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800388 if (!device->refcount) {
389 isert_free_device_ib_res(device);
390 list_del(&device->dev_node);
391 kfree(device);
392 }
393 mutex_unlock(&device_list_mutex);
394}
395
396static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300397isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800398{
399 struct isert_device *device;
400 int ret;
401
402 mutex_lock(&device_list_mutex);
403 list_for_each_entry(device, &device_list, dev_node) {
404 if (device->ib_device->node_guid == cma_id->device->node_guid) {
405 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200406 isert_info("Found iser device %p refcount %d\n",
407 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800408 mutex_unlock(&device_list_mutex);
409 return device;
410 }
411 }
412
413 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
414 if (!device) {
415 mutex_unlock(&device_list_mutex);
416 return ERR_PTR(-ENOMEM);
417 }
418
419 INIT_LIST_HEAD(&device->dev_node);
420
421 device->ib_device = cma_id->device;
422 ret = isert_create_device_ib_res(device);
423 if (ret) {
424 kfree(device);
425 mutex_unlock(&device_list_mutex);
426 return ERR_PTR(ret);
427 }
428
429 device->refcount++;
430 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200431 isert_info("Created a new iser device %p refcount %d\n",
432 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800433 mutex_unlock(&device_list_mutex);
434
435 return device;
436}
437
Vu Pham59464ef2013-08-28 23:23:35 +0300438static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200439isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300440{
441 struct fast_reg_descriptor *fr_desc, *tmp;
442 int i = 0;
443
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300444 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300445 return;
446
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200447 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300448
449 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300450 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300451 list_del(&fr_desc->list);
Vu Pham59464ef2013-08-28 23:23:35 +0300452 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200453 if (fr_desc->pi_ctx) {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200454 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300455 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200456 kfree(fr_desc->pi_ctx);
457 }
Vu Pham59464ef2013-08-28 23:23:35 +0300458 kfree(fr_desc);
459 ++i;
460 }
461
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300462 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200463 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300464 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300465}
466
467static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200468isert_create_pi_ctx(struct fast_reg_descriptor *desc,
469 struct ib_device *device,
470 struct ib_pd *pd)
471{
Sagi Grimberg570db172014-12-02 16:57:31 +0200472 struct pi_context *pi_ctx;
473 int ret;
474
475 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
476 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200477 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200478 return -ENOMEM;
479 }
480
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300481 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
482 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimberg570db172014-12-02 16:57:31 +0200483 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200484 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200485 PTR_ERR(pi_ctx->prot_mr));
486 ret = PTR_ERR(pi_ctx->prot_mr);
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300487 goto err_pi_ctx;
Sagi Grimberg570db172014-12-02 16:57:31 +0200488 }
489 desc->ind |= ISERT_PROT_KEY_VALID;
490
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300491 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg570db172014-12-02 16:57:31 +0200492 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200493 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200494 PTR_ERR(pi_ctx->sig_mr));
495 ret = PTR_ERR(pi_ctx->sig_mr);
496 goto err_prot_mr;
497 }
498
499 desc->pi_ctx = pi_ctx;
500 desc->ind |= ISERT_SIG_KEY_VALID;
501 desc->ind &= ~ISERT_PROTECTED;
502
503 return 0;
504
505err_prot_mr:
Roland Dreierb2feda42015-05-29 23:12:10 -0700506 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg570db172014-12-02 16:57:31 +0200507err_pi_ctx:
Roland Dreierb2feda42015-05-29 23:12:10 -0700508 kfree(pi_ctx);
Sagi Grimberg570db172014-12-02 16:57:31 +0200509
510 return ret;
511}
512
513static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200514isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200515 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200516{
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300517 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
518 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200519 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200520 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200521 PTR_ERR(fr_desc->data_mr));
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300522 return PTR_ERR(fr_desc->data_mr);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200523 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200524 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200525
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200526 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200527
528 return 0;
529}
530
531static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200532isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300533{
534 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300535 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700536 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
537 struct se_node_acl *se_nacl = se_sess->se_node_acl;
538 int i, ret, tag_num;
539 /*
540 * Setup the number of FRMRs based upon the number of tags
541 * available to session in iscsi_target_locate_portal().
542 */
543 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
544 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300545
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300546 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700547 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300548 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
549 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200550 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300551 ret = -ENOMEM;
552 goto err;
553 }
554
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200555 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300556 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200557 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200558 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200559 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800560 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300561 goto err;
562 }
563
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300564 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
565 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300566 }
567
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200568 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300569 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300570
571 return 0;
572
573err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200574 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300575 return ret;
576}
577
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300578static void
579isert_init_conn(struct isert_conn *isert_conn)
580{
581 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300582 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300583 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300584 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300585 init_completion(&isert_conn->wait);
586 kref_init(&isert_conn->kref);
587 mutex_init(&isert_conn->mutex);
588 spin_lock_init(&isert_conn->pool_lock);
589 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300590 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300591}
592
593static void
594isert_free_login_buf(struct isert_conn *isert_conn)
595{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300596 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300597
598 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
599 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
600 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
601 ISCSI_DEF_MAX_RECV_SEG_LEN,
602 DMA_FROM_DEVICE);
603 kfree(isert_conn->login_buf);
604}
605
606static int
607isert_alloc_login_buf(struct isert_conn *isert_conn,
608 struct ib_device *ib_dev)
609{
610 int ret;
611
612 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
613 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
614 if (!isert_conn->login_buf) {
615 isert_err("Unable to allocate isert_conn->login_buf\n");
616 return -ENOMEM;
617 }
618
619 isert_conn->login_req_buf = isert_conn->login_buf;
620 isert_conn->login_rsp_buf = isert_conn->login_buf +
621 ISCSI_DEF_MAX_RECV_SEG_LEN;
622
623 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
624 isert_conn->login_buf, isert_conn->login_req_buf,
625 isert_conn->login_rsp_buf);
626
627 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
628 (void *)isert_conn->login_req_buf,
629 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
630
631 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
632 if (ret) {
633 isert_err("login_req_dma mapping error: %d\n", ret);
634 isert_conn->login_req_dma = 0;
635 goto out_login_buf;
636 }
637
638 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
639 (void *)isert_conn->login_rsp_buf,
640 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
641
642 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
643 if (ret) {
644 isert_err("login_rsp_dma mapping error: %d\n", ret);
645 isert_conn->login_rsp_dma = 0;
646 goto out_req_dma_map;
647 }
648
649 return 0;
650
651out_req_dma_map:
652 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
653 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
654out_login_buf:
655 kfree(isert_conn->login_buf);
656 return ret;
657}
658
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200659static void
660isert_set_nego_params(struct isert_conn *isert_conn,
661 struct rdma_conn_param *param)
662{
663 struct ib_device_attr *attr = &isert_conn->device->ib_device->attrs;
664
665 /* Set max inflight RDMA READ requests */
666 isert_conn->initiator_depth = min_t(u8, param->initiator_depth,
667 attr->max_qp_init_rd_atom);
668 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
669
670 if (param->private_data) {
671 u8 flags = *(u8 *)param->private_data;
672
673 /*
674 * use remote invalidation if the both initiator
675 * and the HCA support it
676 */
677 isert_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP) &&
678 (attr->device_cap_flags &
679 IB_DEVICE_MEM_MGT_EXTENSIONS);
680 if (isert_conn->snd_w_inv)
681 isert_info("Using remote invalidation\n");
682 }
683}
684
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800685static int
686isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
687{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200688 struct isert_np *isert_np = cma_id->context;
689 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800690 struct isert_conn *isert_conn;
691 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800692 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300693
694 spin_lock_bh(&np->np_thread_lock);
695 if (!np->enabled) {
696 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200697 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300698 return rdma_reject(cma_id, NULL, 0);
699 }
700 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800701
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200702 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800703 cma_id, cma_id->context);
704
705 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300706 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800707 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800708
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300709 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300710 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800711
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300712 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
713 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800714 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800715
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300716 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800717 if (IS_ERR(device)) {
718 ret = PTR_ERR(device);
719 goto out_rsp_dma_map;
720 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300721 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800722
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +0200723 isert_set_nego_params(isert_conn, &event->param.conn);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300724
Sagi Grimberg570db172014-12-02 16:57:31 +0200725 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800726 if (ret)
727 goto out_conn_dev;
728
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200729 ret = isert_rdma_post_recvl(isert_conn);
730 if (ret)
731 goto out_conn_dev;
732
733 ret = isert_rdma_accept(isert_conn);
734 if (ret)
735 goto out_conn_dev;
736
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300737 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300738 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300739 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800740
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800741 return 0;
742
743out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300744 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800745out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300746 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800747out:
748 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200749 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800750 return ret;
751}
752
753static void
754isert_connect_release(struct isert_conn *isert_conn)
755{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300756 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800757
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200758 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800759
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300760 BUG_ON(!device);
761
762 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200763 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300764
Sagi Grimberg19e20902014-12-02 16:57:26 +0200765 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300766 if (isert_conn->cm_id)
767 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200768
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300769 if (isert_conn->qp) {
770 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200771
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300772 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300773 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800774 }
775
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300776 if (isert_conn->login_buf)
777 isert_free_login_buf(isert_conn);
778
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300779 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800780
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300781 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800782}
783
784static void
785isert_connected_handler(struct rdma_cm_id *cma_id)
786{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200787 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300788 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300789
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200790 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200791
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300792 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300793 isert_conn->state = ISER_CONN_UP;
794 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300795 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300796
797 mutex_lock(&isert_np->mutex);
798 list_move_tail(&isert_conn->node, &isert_np->pending);
799 mutex_unlock(&isert_np->mutex);
800
801 isert_info("np %p: Allow accept_np to continue\n", isert_np);
802 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800803}
804
805static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300806isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800807{
808 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300809 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800810
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200811 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
812 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800813
814 isert_connect_release(isert_conn);
815}
816
817static void
818isert_put_conn(struct isert_conn *isert_conn)
819{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300820 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800821}
822
Sagi Grimberg954f2372014-12-02 16:57:17 +0200823/**
824 * isert_conn_terminate() - Initiate connection termination
825 * @isert_conn: isert connection struct
826 *
827 * Notes:
Jenny Derzhavetzaea92982016-02-24 19:23:59 +0200828 * In case the connection state is BOUND, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200829 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200830 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200831 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300832 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200833 * safe to call multiple times.
834 */
835static void
836isert_conn_terminate(struct isert_conn *isert_conn)
837{
838 int err;
839
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200840 switch (isert_conn->state) {
841 case ISER_CONN_TERMINATING:
842 break;
843 case ISER_CONN_UP:
Jenny Derzhavetzaea92982016-02-24 19:23:59 +0200844 case ISER_CONN_BOUND:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200845 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200846 isert_info("Terminating conn %p state %d\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200847 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200848 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300849 err = rdma_disconnect(isert_conn->cm_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200850 if (err)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200851 isert_warn("Failed rdma_disconnect isert_conn %p\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200852 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200853 break;
854 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200855 isert_warn("conn %p teminating in state %d\n",
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200856 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200857 }
858}
859
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700860static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200861isert_np_cma_handler(struct isert_np *isert_np,
862 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800863{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300864 isert_dbg("%s (%d): isert np %p\n",
865 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200866
867 switch (event) {
868 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300869 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200870 break;
871 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300872 isert_np->cm_id = isert_setup_id(isert_np);
873 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200874 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300875 isert_np, PTR_ERR(isert_np->cm_id));
876 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200877 }
878 break;
879 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200880 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200881 isert_np, event);
882 }
883
884 return -1;
885}
886
887static int
888isert_disconnected_handler(struct rdma_cm_id *cma_id,
889 enum rdma_cm_event_type event)
890{
891 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700892 struct isert_conn *isert_conn;
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300893 bool terminating = false;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700894
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300895 if (isert_np->cm_id == cma_id)
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200896 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700897
Sagi Grimberg19e20902014-12-02 16:57:26 +0200898 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800899
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300900 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300901 terminating = (isert_conn->state == ISER_CONN_TERMINATING);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200902 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300903 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200904
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300905 isert_info("conn %p completing wait\n", isert_conn);
906 complete(&isert_conn->wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700907
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300908 if (terminating)
909 goto out;
910
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300911 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300912 if (!list_empty(&isert_conn->node)) {
913 list_del_init(&isert_conn->node);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300914 isert_put_conn(isert_conn);
915 queue_work(isert_release_wq, &isert_conn->release_work);
916 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300917 mutex_unlock(&isert_np->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300918
919out:
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700920 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800921}
922
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300923static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200924isert_connect_error(struct rdma_cm_id *cma_id)
925{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200926 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200927
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300928 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300929 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200930 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300931
932 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200933}
934
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800935static int
936isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
937{
938 int ret = 0;
939
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300940 isert_info("%s (%d): status %d id %p np %p\n",
941 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200942 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800943
944 switch (event->event) {
945 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800946 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700947 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200948 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800949 break;
950 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800951 isert_connected_handler(cma_id);
952 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300953 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
954 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
955 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300956 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200957 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800958 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200959 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
960 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800961 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300962 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200963 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800964 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200965 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800966 break;
967 }
968
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800969 return ret;
970}
971
972static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300973isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800974{
975 struct ib_recv_wr *rx_wr, *rx_wr_failed;
976 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800977 struct iser_rx_desc *rx_desc;
978
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300979 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300980 rx_desc = &isert_conn->rx_descs[i];
981 rx_wr->wr_id = (uintptr_t)rx_desc;
982 rx_wr->sg_list = &rx_desc->rx_sg;
983 rx_wr->num_sge = 1;
984 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800985 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800986 rx_wr--;
987 rx_wr->next = NULL; /* mark end of work requests list */
988
989 isert_conn->post_recv_buf_count += count;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300990 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300991 &rx_wr_failed);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800992 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200993 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800994 isert_conn->post_recv_buf_count -= count;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800995 }
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300996
997 return ret;
998}
999
1000static int
1001isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
1002{
1003 struct ib_recv_wr *rx_wr_failed, rx_wr;
1004 int ret;
1005
1006 rx_wr.wr_id = (uintptr_t)rx_desc;
1007 rx_wr.sg_list = &rx_desc->rx_sg;
1008 rx_wr.num_sge = 1;
1009 rx_wr.next = NULL;
1010
1011 isert_conn->post_recv_buf_count++;
1012 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
1013 if (ret) {
1014 isert_err("ib_post_recv() failed with ret: %d\n", ret);
1015 isert_conn->post_recv_buf_count--;
1016 }
1017
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001018 return ret;
1019}
1020
1021static int
1022isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1023{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001024 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001025 struct ib_send_wr send_wr, *send_wr_failed;
1026 int ret;
1027
1028 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1029 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1030
1031 send_wr.next = NULL;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001032 send_wr.wr_id = (uintptr_t)tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001033 send_wr.sg_list = tx_desc->tx_sg;
1034 send_wr.num_sge = tx_desc->num_sge;
1035 send_wr.opcode = IB_WR_SEND;
1036 send_wr.send_flags = IB_SEND_SIGNALED;
1037
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001038 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001039 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001040 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001041
1042 return ret;
1043}
1044
1045static void
1046isert_create_send_desc(struct isert_conn *isert_conn,
1047 struct isert_cmd *isert_cmd,
1048 struct iser_tx_desc *tx_desc)
1049{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001050 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001051 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001052
1053 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1054 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1055
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001056 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
1057 tx_desc->iser_header.flags = ISCSI_CTRL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001058
1059 tx_desc->num_sge = 1;
1060 tx_desc->isert_cmd = isert_cmd;
1061
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001062 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1063 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001064 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001065 }
1066}
1067
1068static int
1069isert_init_tx_hdrs(struct isert_conn *isert_conn,
1070 struct iser_tx_desc *tx_desc)
1071{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001072 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001073 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001074 u64 dma_addr;
1075
1076 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1077 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1078 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001079 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001080 return -ENOMEM;
1081 }
1082
1083 tx_desc->dma_addr = dma_addr;
1084 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1085 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001086 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001087
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001088 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1089 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1090 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001091
1092 return 0;
1093}
1094
1095static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001096isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001097 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001098{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001099 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1100
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001101 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001102 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001103
1104 if (isert_conn->snd_w_inv && isert_cmd->inv_rkey) {
1105 send_wr->opcode = IB_WR_SEND_WITH_INV;
1106 send_wr->ex.invalidate_rkey = isert_cmd->inv_rkey;
1107 } else {
1108 send_wr->opcode = IB_WR_SEND;
1109 }
1110
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001111 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001112 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001113 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001114}
1115
1116static int
1117isert_rdma_post_recvl(struct isert_conn *isert_conn)
1118{
1119 struct ib_recv_wr rx_wr, *rx_wr_fail;
1120 struct ib_sge sge;
1121 int ret;
1122
1123 memset(&sge, 0, sizeof(struct ib_sge));
1124 sge.addr = isert_conn->login_req_dma;
1125 sge.length = ISER_RX_LOGIN_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001126 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001127
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001128 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001129 sge.addr, sge.length, sge.lkey);
1130
1131 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001132 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001133 rx_wr.sg_list = &sge;
1134 rx_wr.num_sge = 1;
1135
1136 isert_conn->post_recv_buf_count++;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001137 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001138 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001139 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001140 isert_conn->post_recv_buf_count--;
1141 }
1142
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001143 return ret;
1144}
1145
1146static int
1147isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1148 u32 length)
1149{
1150 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001151 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001152 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001153 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001154 int ret;
1155
1156 isert_create_send_desc(isert_conn, NULL, tx_desc);
1157
1158 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1159 sizeof(struct iscsi_hdr));
1160
1161 isert_init_tx_hdrs(isert_conn, tx_desc);
1162
1163 if (length > 0) {
1164 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1165
1166 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1167 length, DMA_TO_DEVICE);
1168
1169 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1170
1171 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1172 length, DMA_TO_DEVICE);
1173
1174 tx_dsg->addr = isert_conn->login_rsp_dma;
1175 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001176 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001177 tx_desc->num_sge = 2;
1178 }
1179 if (!login->login_failed) {
1180 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001181 if (!conn->sess->sess_ops->SessionType &&
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001182 isert_conn->device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001183 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001184 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001185 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001186 " fastreg pool\n", isert_conn);
1187 return ret;
1188 }
1189 }
1190
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001191 ret = isert_alloc_rx_descriptors(isert_conn);
1192 if (ret)
1193 return ret;
1194
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001195 ret = isert_post_recvm(isert_conn,
1196 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001197 if (ret)
1198 return ret;
1199
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001200 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001201 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001202 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001203 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001204 goto post_send;
1205 }
1206
1207 ret = isert_rdma_post_recvl(isert_conn);
1208 if (ret)
1209 return ret;
1210 }
1211post_send:
1212 ret = isert_post_send(isert_conn, tx_desc);
1213 if (ret)
1214 return ret;
1215
1216 return 0;
1217}
1218
1219static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001220isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001221{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001222 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1223 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001224 struct iscsi_conn *conn = isert_conn->conn;
1225 struct iscsi_login *login = conn->conn_login;
1226 int size;
1227
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001228 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001229
1230 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001231
1232 if (login->first_request) {
1233 struct iscsi_login_req *login_req =
1234 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1235 /*
1236 * Setup the initial iscsi_login values from the leading
1237 * login request PDU.
1238 */
1239 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1240 login->current_stage =
1241 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1242 >> 2;
1243 login->version_min = login_req->min_version;
1244 login->version_max = login_req->max_version;
1245 memcpy(login->isid, login_req->isid, 6);
1246 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1247 login->init_task_tag = login_req->itt;
1248 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1249 login->cid = be16_to_cpu(login_req->cid);
1250 login->tsih = be16_to_cpu(login_req->tsih);
1251 }
1252
1253 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1254
1255 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001256 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1257 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1258 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001259 memcpy(login->req_buf, &rx_desc->data[0], size);
1260
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001261 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001262 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001263 return;
1264 }
1265 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001266}
1267
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001268static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001269*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001270{
Sagi Grimberg67004252015-03-29 15:52:09 +03001271 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001272 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001273 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001274
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001275 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001276 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001277 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001278 return NULL;
1279 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001280 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001281 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001282 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001283 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001284
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001285 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001286}
1287
1288static int
1289isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001290 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1291 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001292{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001293 struct iscsi_conn *conn = isert_conn->conn;
1294 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001295 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1296 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001297 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001298
1299 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1300 if (rc < 0)
1301 return rc;
1302
1303 imm_data = cmd->immediate_data;
1304 imm_data_len = cmd->first_burst_len;
1305 unsol_data = cmd->unsolicited_data;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001306 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001307
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001308 if (imm_data && imm_data_len == data_len)
1309 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001310 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1311 if (rc < 0) {
1312 return 0;
1313 } else if (rc > 0) {
1314 dump_payload = true;
1315 goto sequence_cmd;
1316 }
1317
1318 if (!imm_data)
1319 return 0;
1320
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001321 if (imm_data_len != data_len) {
1322 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1323 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1324 &rx_desc->data[0], imm_data_len);
1325 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1326 sg_nents, imm_data_len);
1327 } else {
1328 sg_init_table(&isert_cmd->sg, 1);
1329 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1330 cmd->se_cmd.t_data_nents = 1;
1331 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1332 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1333 imm_data_len);
1334 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001335
1336 cmd->write_data_done += imm_data_len;
1337
1338 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1339 spin_lock_bh(&cmd->istate_lock);
1340 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1341 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1342 spin_unlock_bh(&cmd->istate_lock);
1343 }
1344
1345sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001346 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001347
1348 if (!rc && dump_payload == false && unsol_data)
1349 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001350 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001351 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001352
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001353 return 0;
1354}
1355
1356static int
1357isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1358 struct iser_rx_desc *rx_desc, unsigned char *buf)
1359{
1360 struct scatterlist *sg_start;
1361 struct iscsi_conn *conn = isert_conn->conn;
1362 struct iscsi_cmd *cmd = NULL;
1363 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1364 u32 unsol_data_len = ntoh24(hdr->dlength);
1365 int rc, sg_nents, sg_off, page_off;
1366
1367 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1368 if (rc < 0)
1369 return rc;
1370 else if (!cmd)
1371 return 0;
1372 /*
1373 * FIXME: Unexpected unsolicited_data out
1374 */
1375 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001376 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001377 dump_stack();
1378 return -1;
1379 }
1380
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001381 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1382 "write_data_done: %u, data_length: %u\n",
1383 unsol_data_len, cmd->write_data_done,
1384 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001385
1386 sg_off = cmd->write_data_done / PAGE_SIZE;
1387 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1388 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1389 page_off = cmd->write_data_done % PAGE_SIZE;
1390 /*
1391 * FIXME: Non page-aligned unsolicited_data out
1392 */
1393 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001394 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001395 dump_stack();
1396 return -1;
1397 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001398 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1399 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1400 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001401
1402 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1403 unsol_data_len);
1404
1405 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1406 if (rc < 0)
1407 return rc;
1408
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001409 /*
1410 * multiple data-outs on the same command can arrive -
1411 * so post the buffer before hand
1412 */
1413 rc = isert_post_recv(isert_conn, rx_desc);
1414 if (rc) {
1415 isert_err("ib_post_recv failed with %d\n", rc);
1416 return rc;
1417 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001418 return 0;
1419}
1420
1421static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001422isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001423 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1424 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001425{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001426 struct iscsi_conn *conn = isert_conn->conn;
1427 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1428 int rc;
1429
1430 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1431 if (rc < 0)
1432 return rc;
1433 /*
1434 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1435 */
1436
1437 return iscsit_process_nop_out(conn, cmd, hdr);
1438}
1439
1440static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001441isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001442 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1443 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001444{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001445 struct iscsi_conn *conn = isert_conn->conn;
1446 u32 payload_length = ntoh24(hdr->dlength);
1447 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001448 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001449
1450 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1451 if (rc < 0)
1452 return rc;
1453
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001454 if (payload_length) {
1455 text_in = kzalloc(payload_length, GFP_KERNEL);
1456 if (!text_in) {
1457 isert_err("Unable to allocate text_in of payload_length: %u\n",
1458 payload_length);
1459 return -ENOMEM;
1460 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001461 }
1462 cmd->text_in_ptr = text_in;
1463
1464 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1465
1466 return iscsit_process_text_cmd(conn, cmd, hdr);
1467}
1468
1469static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001470isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1471 uint32_t read_stag, uint64_t read_va,
1472 uint32_t write_stag, uint64_t write_va)
1473{
1474 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1475 struct iscsi_conn *conn = isert_conn->conn;
1476 struct iscsi_cmd *cmd;
1477 struct isert_cmd *isert_cmd;
1478 int ret = -EINVAL;
1479 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1480
Sagi Grimbergfb140272015-03-29 15:52:08 +03001481 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001482 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001483 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001484 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001485 return 0;
1486 }
1487
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001488 switch (opcode) {
1489 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001490 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001491 if (!cmd)
1492 break;
1493
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001494 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001495 isert_cmd->read_stag = read_stag;
1496 isert_cmd->read_va = read_va;
1497 isert_cmd->write_stag = write_stag;
1498 isert_cmd->write_va = write_va;
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02001499 isert_cmd->inv_rkey = read_stag ? read_stag : write_stag;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001500
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001501 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001502 rx_desc, (unsigned char *)hdr);
1503 break;
1504 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001505 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001506 if (!cmd)
1507 break;
1508
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001509 isert_cmd = iscsit_priv_cmd(cmd);
1510 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001511 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001512 break;
1513 case ISCSI_OP_SCSI_DATA_OUT:
1514 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1515 (unsigned char *)hdr);
1516 break;
1517 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001518 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001519 if (!cmd)
1520 break;
1521
1522 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1523 (unsigned char *)hdr);
1524 break;
1525 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001526 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001527 if (!cmd)
1528 break;
1529
1530 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001531 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001532 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001533 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001534 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001535 else
1536 cmd = isert_allocate_cmd(conn, rx_desc);
1537
1538 if (!cmd)
1539 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001540
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001541 isert_cmd = iscsit_priv_cmd(cmd);
1542 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001543 rx_desc, (struct iscsi_text *)hdr);
1544 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001545 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001546 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001547 dump_stack();
1548 break;
1549 }
1550
1551 return ret;
1552}
1553
1554static void
1555isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1556{
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001557 struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001558 uint64_t read_va = 0, write_va = 0;
1559 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001560
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001561 switch (iser_ctrl->flags & 0xF0) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001562 case ISCSI_CTRL:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001563 if (iser_ctrl->flags & ISER_RSV) {
1564 read_stag = be32_to_cpu(iser_ctrl->read_stag);
1565 read_va = be64_to_cpu(iser_ctrl->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001566 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1567 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001568 }
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001569 if (iser_ctrl->flags & ISER_WSV) {
1570 write_stag = be32_to_cpu(iser_ctrl->write_stag);
1571 write_va = be64_to_cpu(iser_ctrl->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001572 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1573 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001574 }
1575
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001576 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001577 break;
1578 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001579 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001580 break;
1581 default:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001582 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001583 break;
1584 }
1585
Bart Van Assche6c760b32015-10-22 11:14:18 -07001586 isert_rx_opcode(isert_conn, rx_desc,
1587 read_stag, read_va, write_stag, write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001588}
1589
1590static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001591isert_rcv_completion(struct iser_rx_desc *desc,
1592 struct isert_conn *isert_conn,
1593 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001594{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001595 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001596 struct iscsi_hdr *hdr;
1597 u64 rx_dma;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001598 int rx_buflen;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001599
1600 if ((char *)desc == isert_conn->login_req_buf) {
1601 rx_dma = isert_conn->login_req_dma;
1602 rx_buflen = ISER_RX_LOGIN_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001603 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001604 rx_dma, rx_buflen);
1605 } else {
1606 rx_dma = desc->dma_addr;
1607 rx_buflen = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001608 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001609 rx_dma, rx_buflen);
1610 }
1611
1612 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1613
1614 hdr = &desc->iscsi_header;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001615 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001616 hdr->opcode, hdr->itt, hdr->flags,
1617 (int)(xfer_len - ISER_HEADERS_LEN));
1618
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001619 if ((char *)desc == isert_conn->login_req_buf) {
1620 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1621 if (isert_conn->conn) {
1622 struct iscsi_login *login = isert_conn->conn->conn_login;
1623
1624 if (login && !login->first_request)
1625 isert_rx_login_req(isert_conn);
1626 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001627 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001628 complete(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001629 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001630 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001631 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001632 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001633
1634 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1635 DMA_FROM_DEVICE);
1636
1637 isert_conn->post_recv_buf_count--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001638}
1639
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001640static int
1641isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1642 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1643 enum iser_ib_op_code op, struct isert_data_buf *data)
1644{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001645 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001646
1647 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1648 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1649
1650 data->len = length - offset;
1651 data->offset = offset;
1652 data->sg_off = data->offset / PAGE_SIZE;
1653
1654 data->sg = &sg[data->sg_off];
1655 data->nents = min_t(unsigned int, nents - data->sg_off,
1656 ISCSI_ISER_SG_TABLESIZE);
1657 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1658 PAGE_SIZE);
1659
1660 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1661 data->dma_dir);
1662 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001663 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001664 return -EINVAL;
1665 }
1666
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001667 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001668 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001669
1670 return 0;
1671}
1672
1673static void
1674isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1675{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001676 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001677
1678 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1679 memset(data, 0, sizeof(*data));
1680}
1681
1682
1683
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001684static void
1685isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1686{
1687 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001688
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001689 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001690
1691 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001692 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001693 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001694 }
1695
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001696 if (wr->rdma_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001697 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001698 kfree(wr->rdma_wr);
1699 wr->rdma_wr = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001700 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001701
Vu Pham90ecc6e2013-08-28 23:23:33 +03001702 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001703 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001704 kfree(wr->ib_sge);
1705 wr->ib_sge = NULL;
1706 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001707}
1708
1709static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001710isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001711{
1712 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001713
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001714 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001715
1716 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001717 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001718 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1719 isert_unmap_data_buf(isert_conn, &wr->prot);
1720 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1721 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001722 spin_lock_bh(&isert_conn->pool_lock);
1723 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1724 spin_unlock_bh(&isert_conn->pool_lock);
Vu Pham59464ef2013-08-28 23:23:35 +03001725 wr->fr_desc = NULL;
1726 }
1727
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001728 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001729 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001730 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001731 }
1732
1733 wr->ib_sge = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001734 wr->rdma_wr = NULL;
Vu Pham59464ef2013-08-28 23:23:35 +03001735}
1736
1737static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001738isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001739{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001740 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001741 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001742 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001743 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001744 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001745
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001746 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001747
1748 switch (cmd->iscsi_opcode) {
1749 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001750 spin_lock_bh(&conn->cmd_lock);
1751 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001752 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001753 spin_unlock_bh(&conn->cmd_lock);
1754
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001755 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001756 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001757 /*
1758 * Check for special case during comp_err where
1759 * WRITE_PENDING has been handed off from core,
1760 * but requires an extra target_put_sess_cmd()
1761 * before transport_generic_free_cmd() below.
1762 */
1763 if (comp_err &&
1764 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1765 struct se_cmd *se_cmd = &cmd->se_cmd;
1766
Bart Van Asscheafc16602015-04-27 13:52:36 +02001767 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001768 }
1769 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001770
Vu Phamd40945d2013-08-28 23:23:34 +03001771 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001772 transport_generic_free_cmd(&cmd->se_cmd, 0);
1773 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001774 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001775 spin_lock_bh(&conn->cmd_lock);
1776 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001777 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001778 spin_unlock_bh(&conn->cmd_lock);
1779
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001780 transport_generic_free_cmd(&cmd->se_cmd, 0);
1781 break;
1782 case ISCSI_OP_REJECT:
1783 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001784 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001785 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1786 /* If the continue bit is on, keep the command alive */
1787 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1788 break;
1789
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001790 spin_lock_bh(&conn->cmd_lock);
1791 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001792 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001793 spin_unlock_bh(&conn->cmd_lock);
1794
1795 /*
1796 * Handle special case for REJECT when iscsi_add_reject*() has
1797 * overwritten the original iscsi_opcode assignment, and the
1798 * associated cmd->se_cmd needs to be released.
1799 */
1800 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001801 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001802 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001803 transport_generic_free_cmd(&cmd->se_cmd, 0);
1804 break;
1805 }
1806 /*
1807 * Fall-through
1808 */
1809 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001810 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001811 break;
1812 }
1813}
1814
1815static void
1816isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1817{
1818 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001819 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001820 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1821 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1822 tx_desc->dma_addr = 0;
1823 }
1824}
1825
1826static void
1827isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001828 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001829{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001830 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001831 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001832 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1833 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1834 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001835 }
1836
1837 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001838 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001839}
1840
Sagi Grimberg96b79732014-03-17 12:52:18 +02001841static int
1842isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1843{
1844 struct ib_mr_status mr_status;
1845 int ret;
1846
1847 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1848 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001849 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001850 goto fail_mr_status;
1851 }
1852
1853 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1854 u64 sec_offset_err;
1855 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1856
1857 switch (mr_status.sig_err.err_type) {
1858 case IB_SIG_BAD_GUARD:
1859 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1860 break;
1861 case IB_SIG_BAD_REFTAG:
1862 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1863 break;
1864 case IB_SIG_BAD_APPTAG:
1865 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1866 break;
1867 }
1868 sec_offset_err = mr_status.sig_err.sig_err_offset;
1869 do_div(sec_offset_err, block_size);
1870 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1871
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001872 isert_err("PI error found type %d at sector 0x%llx "
1873 "expected 0x%x vs actual 0x%x\n",
1874 mr_status.sig_err.err_type,
1875 (unsigned long long)se_cmd->bad_sector,
1876 mr_status.sig_err.expected,
1877 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001878 ret = 1;
1879 }
1880
1881fail_mr_status:
1882 return ret;
1883}
1884
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001885static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001886isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1887 struct isert_cmd *isert_cmd)
1888{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001889 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001890 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001891 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001892 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001893 struct isert_device *device = isert_conn->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001894 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001895
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001896 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001897 ret = isert_check_pi_status(se_cmd,
1898 wr->fr_desc->pi_ctx->sig_mr);
1899 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001900 }
1901
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001902 device->unreg_rdma_mem(isert_cmd, isert_conn);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001903 wr->rdma_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001904 if (ret)
1905 transport_send_check_condition_and_sense(se_cmd,
1906 se_cmd->pi_err, 0);
1907 else
1908 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001909}
1910
1911static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001912isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1913 struct isert_cmd *isert_cmd)
1914{
1915 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001916 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001917 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001918 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001919 struct isert_device *device = isert_conn->device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001920 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001921
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001922 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001923 ret = isert_check_pi_status(se_cmd,
1924 wr->fr_desc->pi_ctx->sig_mr);
1925 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001926 }
1927
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001928 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001929 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001930 cmd->write_data_done = wr->data.len;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001931 wr->rdma_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001932
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001933 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001934 spin_lock_bh(&cmd->istate_lock);
1935 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1936 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1937 spin_unlock_bh(&cmd->istate_lock);
1938
Sagi Grimberg364189f2015-03-29 15:52:03 +03001939 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001940 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001941 transport_send_check_condition_and_sense(se_cmd,
1942 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001943 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001944 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001945 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001946}
1947
1948static void
1949isert_do_control_comp(struct work_struct *work)
1950{
1951 struct isert_cmd *isert_cmd = container_of(work,
1952 struct isert_cmd, comp_work);
1953 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001954 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001955 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001956
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001957 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1958
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001959 switch (cmd->i_state) {
1960 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001961 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001962 case ISTATE_SEND_REJECT: /* FALLTHRU */
1963 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001964 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001965 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1966 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001967 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001968 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001969 iscsit_logout_post_handler(cmd, cmd->conn);
1970 break;
1971 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001972 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001973 dump_stack();
1974 break;
1975 }
1976}
1977
1978static void
1979isert_response_completion(struct iser_tx_desc *tx_desc,
1980 struct isert_cmd *isert_cmd,
1981 struct isert_conn *isert_conn,
1982 struct ib_device *ib_dev)
1983{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001984 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001985
1986 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001987 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001988 cmd->i_state == ISTATE_SEND_REJECT ||
1989 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001990 isert_unmap_tx_desc(tx_desc, ib_dev);
1991
1992 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1993 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1994 return;
1995 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001996
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001997 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001998 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001999}
2000
2001static void
Sagi Grimberg77486812015-03-29 15:52:13 +03002002isert_snd_completion(struct iser_tx_desc *tx_desc,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002003 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002004{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002005 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002006 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2007 struct isert_rdma_wr *wr;
2008
2009 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002010 isert_unmap_tx_desc(tx_desc, ib_dev);
2011 return;
2012 }
2013 wr = &isert_cmd->rdma_wr;
2014
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002015 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
2016
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002017 switch (wr->iser_ib_op) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002018 case ISER_IB_SEND:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002019 isert_response_completion(tx_desc, isert_cmd,
2020 isert_conn, ib_dev);
2021 break;
2022 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002023 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002024 break;
2025 case ISER_IB_RDMA_READ:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002026 isert_completion_rdma_read(tx_desc, isert_cmd);
2027 break;
2028 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002029 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002030 dump_stack();
2031 break;
2032 }
2033}
2034
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002035/**
2036 * is_isert_tx_desc() - Indicate if the completion wr_id
2037 * is a TX descriptor or not.
2038 * @isert_conn: iser connection
2039 * @wr_id: completion WR identifier
2040 *
2041 * Since we cannot rely on wc opcode in FLUSH errors
2042 * we must work around it by checking if the wr_id address
2043 * falls in the iser connection rx_descs buffer. If so
2044 * it is an RX descriptor, otherwize it is a TX.
2045 */
2046static inline bool
2047is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002048{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002049 void *start = isert_conn->rx_descs;
2050 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002051
Jenny Derzhavetzb89a7c22016-02-24 19:23:58 +02002052 if ((wr_id >= start && wr_id < start + len) ||
2053 (wr_id == isert_conn->login_req_buf))
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002054 return false;
2055
2056 return true;
2057}
2058
2059static void
2060isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
2061{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002062 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002063 isert_info("conn %p completing wait_comp_err\n",
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002064 isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002065 complete(&isert_conn->wait_comp_err);
Nicholas Bellingered4520a2014-12-07 13:12:05 +02002066 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002067 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002068 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002069 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002070
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002071 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2072 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002073 if (!isert_cmd)
2074 isert_unmap_tx_desc(desc, ib_dev);
2075 else
2076 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002077 } else {
2078 isert_conn->post_recv_buf_count--;
Jenny Derzhavetzaea92982016-02-24 19:23:59 +02002079 if (!isert_conn->post_recv_buf_count &&
2080 isert_conn->state >= ISER_CONN_BOUND)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002081 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002082 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002083}
2084
2085static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002086isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002087{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002088 struct isert_conn *isert_conn;
2089 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002090 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002091
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002092 isert_conn = wc->qp->qp_context;
2093 if (likely(wc->status == IB_WC_SUCCESS)) {
2094 if (wc->opcode == IB_WC_RECV) {
2095 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002096 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002097 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002098 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002099 isert_snd_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002100 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002101 } else {
2102 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002103 isert_err("%s (%d): wr id %llx vend_err %x\n",
2104 ib_wc_status_msg(wc->status), wc->status,
2105 wc->wr_id, wc->vendor_err);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002106 else
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002107 isert_dbg("%s (%d): wr id %llx\n",
2108 ib_wc_status_msg(wc->status), wc->status,
2109 wc->wr_id);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002110
2111 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2112 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002113 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002114}
2115
2116static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002117isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002118{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002119 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002120 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002121 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002122 struct ib_wc *const wcs = comp->wcs;
2123 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002124
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002125 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2126 for (i = 0; i < n; i++)
2127 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002128
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002129 completed += n;
2130 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002131 break;
2132 }
2133
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002134 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002135}
2136
2137static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002138isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002139{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002140 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002141
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002142 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002143}
2144
2145static int
2146isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2147{
2148 struct ib_send_wr *wr_failed;
2149 int ret;
2150
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002151 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2152 if (ret) {
2153 isert_err("ib_post_recv failed with %d\n", ret);
2154 return ret;
2155 }
2156
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002157 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002158 &wr_failed);
2159 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002160 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002161 return ret;
2162 }
2163 return ret;
2164}
2165
2166static int
2167isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2168{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002169 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002170 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002171 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2172 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2173 &isert_cmd->tx_desc.iscsi_header;
2174
2175 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2176 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2177 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2178 /*
2179 * Attach SENSE DATA payload to iSCSI Response PDU
2180 */
2181 if (cmd->se_cmd.sense_buffer &&
2182 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2183 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002184 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002185 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002186 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002187 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002188
2189 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2190 cmd->sense_buffer);
2191 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2192
2193 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2194 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002195 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002196
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002197 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2198 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002199 DMA_TO_DEVICE);
2200
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002201 isert_cmd->pdu_buf_len = pdu_len;
2202 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2203 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002204 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002205 isert_cmd->tx_desc.num_sge = 2;
2206 }
2207
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002208 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002209
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002210 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002211
2212 return isert_post_response(isert_conn, isert_cmd);
2213}
2214
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002215static void
2216isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2217{
2218 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002219 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002220 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002221
2222 spin_lock_bh(&conn->cmd_lock);
2223 if (!list_empty(&cmd->i_conn_node))
2224 list_del_init(&cmd->i_conn_node);
2225 spin_unlock_bh(&conn->cmd_lock);
2226
2227 if (cmd->data_direction == DMA_TO_DEVICE)
2228 iscsit_stop_dataout_timer(cmd);
2229
2230 device->unreg_rdma_mem(isert_cmd, isert_conn);
2231}
2232
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002233static enum target_prot_op
2234isert_get_sup_prot_ops(struct iscsi_conn *conn)
2235{
Sagi Grimberg67004252015-03-29 15:52:09 +03002236 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002237 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002238
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002239 if (conn->tpg->tpg_attrib.t10_pi) {
2240 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002241 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002242 isert_conn->pi_support = true;
2243 return TARGET_PROT_ALL;
2244 }
2245 }
2246
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002247 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002248 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002249
2250 return TARGET_PROT_NORMAL;
2251}
2252
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002253static int
2254isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2255 bool nopout_response)
2256{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002257 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002258 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002259 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2260
2261 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2262 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2263 &isert_cmd->tx_desc.iscsi_header,
2264 nopout_response);
2265 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002266 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002267
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002268 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002269
2270 return isert_post_response(isert_conn, isert_cmd);
2271}
2272
2273static int
2274isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2275{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002276 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002277 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002278 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2279
2280 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2281 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2282 &isert_cmd->tx_desc.iscsi_header);
2283 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002284 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002285
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002286 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002287
2288 return isert_post_response(isert_conn, isert_cmd);
2289}
2290
2291static int
2292isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2293{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002294 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002295 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002296 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2297
2298 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2299 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2300 &isert_cmd->tx_desc.iscsi_header);
2301 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002302 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002303
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002304 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002305
2306 return isert_post_response(isert_conn, isert_cmd);
2307}
2308
2309static int
2310isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2311{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002312 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002313 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002314 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002315 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002316 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002317 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2318 struct iscsi_reject *hdr =
2319 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002320
2321 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002322 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002323 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002324
2325 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002326 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002327 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2328 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002329 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2330 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002331 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002332 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002333 isert_cmd->tx_desc.num_sge = 2;
2334
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002335 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002336
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002337 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002338
2339 return isert_post_response(isert_conn, isert_cmd);
2340}
2341
2342static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002343isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2344{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002345 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002346 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002347 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2348 struct iscsi_text_rsp *hdr =
2349 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2350 u32 txt_rsp_len;
2351 int rc;
2352
2353 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002354 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002355 if (rc < 0)
2356 return rc;
2357
2358 txt_rsp_len = rc;
2359 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2360
2361 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002362 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002363 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002364 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2365 void *txt_rsp_buf = cmd->buf_ptr;
2366
2367 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2368 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2369
2370 isert_cmd->pdu_buf_len = txt_rsp_len;
2371 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2372 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002373 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002374 isert_cmd->tx_desc.num_sge = 2;
2375 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002376 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002377
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002378 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002379
2380 return isert_post_response(isert_conn, isert_cmd);
2381}
2382
2383static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002384isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002385 struct ib_sge *ib_sge, struct ib_rdma_wr *rdma_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002386 u32 data_left, u32 offset)
2387{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002388 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002389 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002390 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002391 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002392 u32 sg_off, page_off;
2393 int i = 0, sg_nents;
2394
2395 sg_off = offset / PAGE_SIZE;
2396 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2397 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2398 page_off = offset % PAGE_SIZE;
2399
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002400 rdma_wr->wr.sg_list = ib_sge;
2401 rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002402 /*
2403 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2404 */
2405 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002406 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2407 "page_off: %u\n",
2408 (unsigned long long)tmp_sg->dma_address,
2409 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002410
2411 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2412 ib_sge->length = min_t(u32, data_left,
2413 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002414 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002415
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002416 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2417 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002418 page_off = 0;
2419 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002420 if (!data_left)
2421 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002422 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002423 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002424 }
2425
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002426 rdma_wr->wr.num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002427 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002428 rdma_wr->wr.sg_list, rdma_wr->wr.num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002429
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002430 return rdma_wr->wr.num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002431}
2432
2433static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002434isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2435 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002436{
2437 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002438 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002439 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002440 struct isert_data_buf *data = &wr->data;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002441 struct ib_rdma_wr *rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002442 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002443 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2444 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002445
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002446 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002447
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002448 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2449 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2450 se_cmd->t_data_nents, se_cmd->data_length,
2451 offset, wr->iser_ib_op, &wr->data);
2452 if (ret)
2453 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002454
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002455 data_left = data->len;
2456 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002457
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002458 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002459 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002460 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002461 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002462 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002463 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002464 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002465
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002466 wr->rdma_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2467 wr->rdma_wr = kzalloc(sizeof(struct ib_rdma_wr) * wr->rdma_wr_num,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002468 GFP_KERNEL);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002469 if (!wr->rdma_wr) {
2470 isert_dbg("Unable to allocate wr->rdma_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002471 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002472 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002473 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002474
2475 wr->isert_cmd = isert_cmd;
2476 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002477
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002478 for (i = 0; i < wr->rdma_wr_num; i++) {
2479 rdma_wr = &isert_cmd->rdma_wr.rdma_wr[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002480 data_len = min(data_left, rdma_write_max);
2481
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002482 rdma_wr->wr.send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002483 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002484 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2485 rdma_wr->remote_addr = isert_cmd->read_va + offset;
2486 rdma_wr->rkey = isert_cmd->read_stag;
2487 if (i + 1 == wr->rdma_wr_num)
2488 rdma_wr->wr.next = &isert_cmd->tx_desc.send_wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002489 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002490 rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002491 } else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002492 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2493 rdma_wr->remote_addr = isert_cmd->write_va + va_offset;
2494 rdma_wr->rkey = isert_cmd->write_stag;
2495 if (i + 1 == wr->rdma_wr_num)
2496 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002497 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002498 rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002499 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002500
2501 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002502 rdma_wr, data_len, offset);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002503 ib_sge += ib_sge_cnt;
2504
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002505 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002506 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002507 data_left -= data_len;
2508 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002509
2510 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002511unmap_cmd:
2512 isert_unmap_data_buf(isert_conn, data);
2513
Vu Pham90ecc6e2013-08-28 23:23:33 +03002514 return ret;
2515}
2516
Sagi Grimberg10633c32014-12-07 13:12:04 +02002517static inline void
2518isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2519{
2520 u32 rkey;
2521
2522 memset(inv_wr, 0, sizeof(*inv_wr));
2523 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2524 inv_wr->opcode = IB_WR_LOCAL_INV;
2525 inv_wr->ex.invalidate_rkey = mr->rkey;
2526
2527 /* Bump the key */
2528 rkey = ib_inc_rkey(mr->rkey);
2529 ib_update_fast_reg_key(mr, rkey);
2530}
2531
Vu Pham59464ef2013-08-28 23:23:35 +03002532static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002533isert_fast_reg_mr(struct isert_conn *isert_conn,
2534 struct fast_reg_descriptor *fr_desc,
2535 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002536 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002537 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002538{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002539 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002540 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002541 struct ib_mr *mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002542 struct ib_reg_wr reg_wr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002543 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002544 int ret, n;
Vu Pham59464ef2013-08-28 23:23:35 +03002545
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002546 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002547 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002548 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2549 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002550 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2551 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002552 return 0;
2553 }
2554
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002555 if (ind == ISERT_DATA_KEY_VALID)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002556 /* Registering data buffer */
2557 mr = fr_desc->data_mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002558 else
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002559 /* Registering protection buffer */
2560 mr = fr_desc->pi_ctx->prot_mr;
Vu Pham59464ef2013-08-28 23:23:35 +03002561
Sagi Grimberg10633c32014-12-07 13:12:04 +02002562 if (!(fr_desc->ind & ind)) {
2563 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002564 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002565 }
2566
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002567 n = ib_map_mr_sg(mr, mem->sg, mem->nents, PAGE_SIZE);
2568 if (unlikely(n != mem->nents)) {
2569 isert_err("failed to map mr sg (%d/%d)\n",
2570 n, mem->nents);
2571 return n < 0 ? n : -EINVAL;
2572 }
2573
2574 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2575 fr_desc, mem->nents, mem->offset);
2576
2577 reg_wr.wr.next = NULL;
2578 reg_wr.wr.opcode = IB_WR_REG_MR;
2579 reg_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
2580 reg_wr.wr.send_flags = 0;
2581 reg_wr.wr.num_sge = 0;
2582 reg_wr.mr = mr;
2583 reg_wr.key = mr->lkey;
2584 reg_wr.access = IB_ACCESS_LOCAL_WRITE;
Vu Pham59464ef2013-08-28 23:23:35 +03002585
2586 if (!wr)
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002587 wr = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002588 else
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002589 wr->next = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002590
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002591 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002592 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002593 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002594 return ret;
2595 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002596 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002597
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002598 sge->lkey = mr->lkey;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002599 sge->addr = mr->iova;
2600 sge->length = mr->length;
Vu Pham59464ef2013-08-28 23:23:35 +03002601
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002602 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2603 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002604
2605 return ret;
2606}
2607
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002608static inline void
2609isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2610 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002611{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002612 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002613 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2614 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2615 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002616 /*
2617 * At the moment we hard code those, but if in the future
2618 * the target core would like to use it, we will take it
2619 * from se_cmd.
2620 */
2621 domain->sig.dif.apptag_check_mask = 0xffff;
2622 domain->sig.dif.app_escape = true;
2623 domain->sig.dif.ref_escape = true;
2624 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2625 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2626 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002627};
2628
2629static int
2630isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2631{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002632 switch (se_cmd->prot_op) {
2633 case TARGET_PROT_DIN_INSERT:
2634 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002635 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002636 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002637 break;
2638 case TARGET_PROT_DOUT_INSERT:
2639 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002640 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002641 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002642 break;
2643 case TARGET_PROT_DIN_PASS:
2644 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002645 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2646 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002647 break;
2648 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002649 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002650 return -EINVAL;
2651 }
2652
2653 return 0;
2654}
2655
2656static inline u8
2657isert_set_prot_checks(u8 prot_checks)
2658{
2659 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2660 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2661 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2662}
2663
2664static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002665isert_reg_sig_mr(struct isert_conn *isert_conn,
2666 struct se_cmd *se_cmd,
2667 struct isert_rdma_wr *rdma_wr,
2668 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002669{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002670 struct ib_sig_handover_wr sig_wr;
2671 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002672 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2673 struct ib_sig_attrs sig_attrs;
2674 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002675
2676 memset(&sig_attrs, 0, sizeof(sig_attrs));
2677 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2678 if (ret)
2679 goto err;
2680
2681 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2682
2683 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002684 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002685 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002686 }
2687
2688 memset(&sig_wr, 0, sizeof(sig_wr));
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002689 sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
2690 sig_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
2691 sig_wr.wr.sg_list = &rdma_wr->ib_sg[DATA];
2692 sig_wr.wr.num_sge = 1;
2693 sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
2694 sig_wr.sig_attrs = &sig_attrs;
2695 sig_wr.sig_mr = pi_ctx->sig_mr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002696 if (se_cmd->t_prot_sg)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002697 sig_wr.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002698
2699 if (!wr)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002700 wr = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002701 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002702 wr->next = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002703
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002704 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002705 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002706 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002707 goto err;
2708 }
2709 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2710
Sagi Grimberg570db172014-12-02 16:57:31 +02002711 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2712 rdma_wr->ib_sg[SIG].addr = 0;
2713 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002714 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2715 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2716 /*
2717 * We have protection guards on the wire
2718 * so we need to set a larget transfer
2719 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002720 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002721
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002722 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002723 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2724 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002725err:
2726 return ret;
2727}
2728
Vu Pham59464ef2013-08-28 23:23:35 +03002729static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002730isert_handle_prot_cmd(struct isert_conn *isert_conn,
2731 struct isert_cmd *isert_cmd,
2732 struct isert_rdma_wr *wr)
2733{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002734 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002735 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2736 int ret;
2737
2738 if (!wr->fr_desc->pi_ctx) {
2739 ret = isert_create_pi_ctx(wr->fr_desc,
2740 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002741 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002742 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002743 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002744 isert_conn);
2745 return ret;
2746 }
2747 }
2748
2749 if (se_cmd->t_prot_sg) {
2750 ret = isert_map_data_buf(isert_conn, isert_cmd,
2751 se_cmd->t_prot_sg,
2752 se_cmd->t_prot_nents,
2753 se_cmd->prot_length,
2754 0, wr->iser_ib_op, &wr->prot);
2755 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002756 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002757 isert_conn);
2758 return ret;
2759 }
2760
2761 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2762 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2763 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2764 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002765 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002766 isert_conn);
2767 goto unmap_prot_cmd;
2768 }
2769 }
2770
2771 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2772 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002773 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002774 isert_conn);
2775 goto unmap_prot_cmd;
2776 }
2777 wr->fr_desc->ind |= ISERT_PROTECTED;
2778
2779 return 0;
2780
2781unmap_prot_cmd:
2782 if (se_cmd->t_prot_sg)
2783 isert_unmap_data_buf(isert_conn, &wr->prot);
2784
2785 return ret;
2786}
2787
2788static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002789isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2790 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002791{
2792 struct se_cmd *se_cmd = &cmd->se_cmd;
2793 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002794 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002795 struct fast_reg_descriptor *fr_desc = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002796 struct ib_rdma_wr *rdma_wr;
Sagi Grimberg570db172014-12-02 16:57:31 +02002797 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002798 u32 offset;
2799 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002800 unsigned long flags;
2801
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002802 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2803
2804 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2805 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2806 se_cmd->t_data_nents, se_cmd->data_length,
2807 offset, wr->iser_ib_op, &wr->data);
2808 if (ret)
2809 return ret;
2810
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002811 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002812 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2813 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002814 struct fast_reg_descriptor, list);
2815 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002816 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002817 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002818 }
2819
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002820 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002821 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002822 if (ret)
2823 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002824
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002825 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002826 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002827 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002828 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002829
Sagi Grimberg570db172014-12-02 16:57:31 +02002830 ib_sg = &wr->ib_sg[SIG];
2831 } else {
2832 ib_sg = &wr->ib_sg[DATA];
2833 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002834
Sagi Grimberg570db172014-12-02 16:57:31 +02002835 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002836 wr->ib_sge = &wr->s_ib_sge;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002837 wr->rdma_wr_num = 1;
2838 memset(&wr->s_rdma_wr, 0, sizeof(wr->s_rdma_wr));
2839 wr->rdma_wr = &wr->s_rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002840 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002841
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002842 rdma_wr = &isert_cmd->rdma_wr.s_rdma_wr;
2843 rdma_wr->wr.sg_list = &wr->s_ib_sge;
2844 rdma_wr->wr.num_sge = 1;
2845 rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002846 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002847 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2848 rdma_wr->remote_addr = isert_cmd->read_va;
2849 rdma_wr->rkey = isert_cmd->read_stag;
2850 rdma_wr->wr.send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002851 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002852 } else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002853 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2854 rdma_wr->remote_addr = isert_cmd->write_va;
2855 rdma_wr->rkey = isert_cmd->write_stag;
2856 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002857 }
2858
Vu Pham59464ef2013-08-28 23:23:35 +03002859 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002860
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002861unmap_cmd:
2862 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002863 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2864 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2865 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002866 }
2867 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002868
Vu Pham59464ef2013-08-28 23:23:35 +03002869 return ret;
2870}
2871
2872static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002873isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2874{
2875 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002876 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002877 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002878 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002879 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002880 struct ib_send_wr *wr_failed;
2881 int rc;
2882
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002883 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002884 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002885
Vu Pham90ecc6e2013-08-28 23:23:33 +03002886 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002887 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002888 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002889 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002890 return rc;
2891 }
2892
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002893 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002894 /*
2895 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2896 */
2897 isert_create_send_desc(isert_conn, isert_cmd,
2898 &isert_cmd->tx_desc);
2899 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2900 &isert_cmd->tx_desc.iscsi_header);
2901 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2902 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002903 &isert_cmd->tx_desc.send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002904 isert_cmd->rdma_wr.s_rdma_wr.wr.next = &isert_cmd->tx_desc.send_wr;
2905 wr->rdma_wr_num += 1;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002906
2907 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2908 if (rc) {
2909 isert_err("ib_post_recv failed with %d\n", rc);
2910 return rc;
2911 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002912 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002913
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002914 rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002915 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002916 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002917
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002918 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002919 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002920 "READ\n", isert_cmd);
2921 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002922 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002923 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002924
Vu Pham90ecc6e2013-08-28 23:23:33 +03002925 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002926}
2927
2928static int
2929isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2930{
2931 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002932 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002933 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002934 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002935 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002936 struct ib_send_wr *wr_failed;
2937 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002938
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002939 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002940 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002941 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002942 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002943 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002944 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002945 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002946 }
2947
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002948 rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002949 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002950 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002951
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002952 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002953 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002954
Vu Pham90ecc6e2013-08-28 23:23:33 +03002955 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002956}
2957
2958static int
2959isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2960{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002961 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2962 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002963
2964 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002965 case ISTATE_REMOVE:
2966 spin_lock_bh(&conn->cmd_lock);
2967 list_del_init(&cmd->i_conn_node);
2968 spin_unlock_bh(&conn->cmd_lock);
2969 isert_put_cmd(isert_cmd, true);
2970 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002971 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2972 ret = isert_put_nopin(cmd, conn, false);
2973 break;
2974 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002975 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002976 ret = -EINVAL;
2977 break;
2978 }
2979
2980 return ret;
2981}
2982
2983static int
2984isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2985{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002986 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002987 int ret;
2988
2989 switch (state) {
2990 case ISTATE_SEND_LOGOUTRSP:
2991 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002992 if (!ret)
2993 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002994 break;
2995 case ISTATE_SEND_NOPIN:
2996 ret = isert_put_nopin(cmd, conn, true);
2997 break;
2998 case ISTATE_SEND_TASKMGTRSP:
2999 ret = isert_put_tm_rsp(cmd, conn);
3000 break;
3001 case ISTATE_SEND_REJECT:
3002 ret = isert_put_reject(cmd, conn);
3003 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003004 case ISTATE_SEND_TEXTRSP:
3005 ret = isert_put_text_rsp(cmd, conn);
3006 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003007 case ISTATE_SEND_STATUS:
3008 /*
3009 * Special case for sending non GOOD SCSI status from TX thread
3010 * context during pre se_cmd excecution failure.
3011 */
3012 ret = isert_put_response(conn, cmd);
3013 break;
3014 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003015 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003016 ret = -EINVAL;
3017 break;
3018 }
3019
3020 return ret;
3021}
3022
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003023struct rdma_cm_id *
3024isert_setup_id(struct isert_np *isert_np)
3025{
3026 struct iscsi_np *np = isert_np->np;
3027 struct rdma_cm_id *id;
3028 struct sockaddr *sa;
3029 int ret;
3030
3031 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003032 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003033
Guy Shapirofa201052015-10-22 15:20:10 +03003034 id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003035 RDMA_PS_TCP, IB_QPT_RC);
3036 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003037 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003038 ret = PTR_ERR(id);
3039 goto out;
3040 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003041 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003042
3043 ret = rdma_bind_addr(id, sa);
3044 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003045 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003046 goto out_id;
3047 }
3048
Sagi Grimberg992607e2015-03-29 15:52:18 +03003049 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003050 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003051 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003052 goto out_id;
3053 }
3054
3055 return id;
3056out_id:
3057 rdma_destroy_id(id);
3058out:
3059 return ERR_PTR(ret);
3060}
3061
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003062static int
3063isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07003064 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003065{
3066 struct isert_np *isert_np;
3067 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003068 int ret;
3069
3070 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3071 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003072 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003073 return -ENOMEM;
3074 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003075 sema_init(&isert_np->sem, 0);
3076 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003077 INIT_LIST_HEAD(&isert_np->accepted);
3078 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003079 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003080
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003081 /*
3082 * Setup the np->np_sockaddr from the passed sockaddr setup
3083 * in iscsi_target_configfs.c code..
3084 */
3085 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07003086 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003087
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003088 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003089 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003090 ret = PTR_ERR(isert_lid);
3091 goto out;
3092 }
3093
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003094 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003095 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003096
3097 return 0;
3098
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003099out:
3100 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003101
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003102 return ret;
3103}
3104
3105static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003106isert_rdma_accept(struct isert_conn *isert_conn)
3107{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003108 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003109 struct rdma_conn_param cp;
3110 int ret;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003111 struct iser_cm_hdr rsp_hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003112
3113 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003114 cp.initiator_depth = isert_conn->initiator_depth;
3115 cp.retry_count = 7;
3116 cp.rnr_retry_count = 7;
3117
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003118 memset(&rsp_hdr, 0, sizeof(rsp_hdr));
Jenny Derzhavetz422bd0a2015-12-09 14:12:06 +02003119 rsp_hdr.flags = ISERT_ZBVA_NOT_USED;
3120 if (!isert_conn->snd_w_inv)
3121 rsp_hdr.flags = rsp_hdr.flags | ISERT_SEND_W_INV_NOT_USED;
Jenny Derzhavetz13bce482015-12-09 14:12:05 +02003122 cp.private_data = (void *)&rsp_hdr;
3123 cp.private_data_len = sizeof(rsp_hdr);
3124
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003125 ret = rdma_accept(cm_id, &cp);
3126 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003127 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003128 return ret;
3129 }
3130
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003131 return 0;
3132}
3133
3134static int
3135isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3136{
Sagi Grimberg67004252015-03-29 15:52:09 +03003137 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003138 int ret;
3139
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003140 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003141 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3142 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003143 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003144 isert_conn);
3145 return ret;
3146 }
3147 reinit_completion(&isert_conn->login_req_comp);
3148
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003149 /*
3150 * For login requests after the first PDU, isert_rx_login_req() will
3151 * kick schedule_delayed_work(&conn->login_work) as the packet is
3152 * received, which turns this callback from iscsi_target_do_login_rx()
3153 * into a NOP.
3154 */
3155 if (!login->first_request)
3156 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003157
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003158 isert_rx_login_req(isert_conn);
3159
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003160 isert_info("before login_comp conn: %p\n", conn);
3161 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003162 if (ret)
3163 return ret;
3164
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003165 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003166
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003167 return 0;
3168}
3169
3170static void
3171isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3172 struct isert_conn *isert_conn)
3173{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003174 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003175 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003176
3177 conn->login_family = np->np_sockaddr.ss_family;
3178
Andy Groverdc58f762015-08-24 10:26:05 -07003179 conn->login_sockaddr = cm_route->addr.dst_addr;
3180 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003181}
3182
3183static int
3184isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3185{
Sagi Grimberg67004252015-03-29 15:52:09 +03003186 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003187 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003188 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003189
3190accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003191 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003192 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003193 return -ENODEV;
3194
3195 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003196 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003197 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003198 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003199 np->np_thread_state);
3200 /**
3201 * No point in stalling here when np_thread
3202 * is in state RESET/SHUTDOWN/EXIT - bail
3203 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003204 return -ENODEV;
3205 }
3206 spin_unlock_bh(&np->np_thread_lock);
3207
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003208 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003209 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003210 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003211 goto accept_wait;
3212 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003213 isert_conn = list_first_entry(&isert_np->pending,
3214 struct isert_conn, node);
3215 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003216 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003217
3218 conn->context = isert_conn;
3219 isert_conn->conn = conn;
Jenny Derzhavetzaea92982016-02-24 19:23:59 +02003220 isert_conn->state = ISER_CONN_BOUND;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003221
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003222 isert_set_conn_info(np, conn, isert_conn);
3223
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003224 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003225
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003226 return 0;
3227}
3228
3229static void
3230isert_free_np(struct iscsi_np *np)
3231{
Sagi Grimberg67004252015-03-29 15:52:09 +03003232 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003233 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003234
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003235 if (isert_np->cm_id)
3236 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003237
Sagi Grimberg268e6812014-12-02 16:57:36 +02003238 /*
3239 * FIXME: At this point we don't have a good way to insure
3240 * that at this point we don't have hanging connections that
3241 * completed RDMA establishment but didn't start iscsi login
3242 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003243 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003244 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003245 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003246 if (!list_empty(&isert_np->pending)) {
3247 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003248 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003249 &isert_np->pending,
3250 node) {
3251 isert_info("cleaning isert_conn %p state (%d)\n",
3252 isert_conn, isert_conn->state);
3253 isert_connect_release(isert_conn);
3254 }
3255 }
3256
3257 if (!list_empty(&isert_np->accepted)) {
3258 isert_info("Still have isert accepted connections\n");
3259 list_for_each_entry_safe(isert_conn, n,
3260 &isert_np->accepted,
3261 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003262 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003263 isert_conn, isert_conn->state);
3264 isert_connect_release(isert_conn);
3265 }
3266 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003267 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003268
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003269 np->np_context = NULL;
3270 kfree(isert_np);
3271}
3272
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003273static void isert_release_work(struct work_struct *work)
3274{
3275 struct isert_conn *isert_conn = container_of(work,
3276 struct isert_conn,
3277 release_work);
3278
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003279 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003280
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003281 wait_for_completion(&isert_conn->wait);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003282
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003283 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003284 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003285 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003286
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003287 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003288 isert_put_conn(isert_conn);
3289}
3290
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003291static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003292isert_wait4logout(struct isert_conn *isert_conn)
3293{
3294 struct iscsi_conn *conn = isert_conn->conn;
3295
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003296 isert_info("conn %p\n", isert_conn);
3297
Sagi Grimberg991bb762014-12-07 13:12:01 +02003298 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003299 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003300 wait_for_completion_timeout(&conn->conn_logout_comp,
3301 SECONDS_FOR_LOGOUT_COMP * HZ);
3302 }
3303}
3304
3305static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003306isert_wait4cmds(struct iscsi_conn *conn)
3307{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003308 isert_info("iscsi_conn %p\n", conn);
3309
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003310 if (conn->sess) {
3311 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3312 target_wait_for_sess_cmds(conn->sess->se_sess);
3313 }
3314}
3315
3316static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003317isert_wait4flush(struct isert_conn *isert_conn)
3318{
3319 struct ib_recv_wr *bad_wr;
3320
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003321 isert_info("conn %p\n", isert_conn);
3322
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003323 init_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003324 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3325 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003326 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003327 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003328 return;
3329 }
3330
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003331 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003332}
3333
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003334/**
3335 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3336 * unsolicitate dataout
3337 * @conn: iscsi connection
3338 *
3339 * We might still have commands that are waiting for unsolicited
3340 * dataouts messages. We must put the extra reference on those
3341 * before blocking on the target_wait_for_session_cmds
3342 */
3343static void
3344isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3345{
3346 struct iscsi_cmd *cmd, *tmp;
3347 static LIST_HEAD(drop_cmd_list);
3348
3349 spin_lock_bh(&conn->cmd_lock);
3350 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3351 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3352 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3353 (cmd->write_data_done < cmd->se_cmd.data_length))
3354 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3355 }
3356 spin_unlock_bh(&conn->cmd_lock);
3357
3358 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3359 list_del_init(&cmd->i_conn_node);
3360 if (cmd->i_state != ISTATE_REMOVE) {
3361 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3362
3363 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3364 isert_put_cmd(isert_cmd, true);
3365 }
3366 }
3367}
3368
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003369static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003370{
3371 struct isert_conn *isert_conn = conn->context;
3372
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003373 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003374
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003375 mutex_lock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003376 /*
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003377 * Only wait for wait_comp_err if the isert_conn made it
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003378 * into full feature phase..
3379 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003380 if (isert_conn->state == ISER_CONN_INIT) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003381 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003382 return;
3383 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003384 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003385 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003386
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003387 isert_wait4flush(isert_conn);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003388 isert_put_unsol_pending_cmds(conn);
3389 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003390 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003391
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003392 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003393}
3394
3395static void isert_free_conn(struct iscsi_conn *conn)
3396{
3397 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003398
Sagi Grimberg524630d2015-06-04 19:49:21 +03003399 isert_wait4flush(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003400 isert_put_conn(isert_conn);
3401}
3402
3403static struct iscsit_transport iser_target_transport = {
3404 .name = "IB/iSER",
3405 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003406 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003407 .owner = THIS_MODULE,
3408 .iscsit_setup_np = isert_setup_np,
3409 .iscsit_accept_np = isert_accept_np,
3410 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003411 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003412 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003413 .iscsit_get_login_rx = isert_get_login_rx,
3414 .iscsit_put_login_tx = isert_put_login_tx,
3415 .iscsit_immediate_queue = isert_immediate_queue,
3416 .iscsit_response_queue = isert_response_queue,
3417 .iscsit_get_dataout = isert_get_dataout,
3418 .iscsit_queue_data_in = isert_put_datain,
3419 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003420 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003421 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003422};
3423
3424static int __init isert_init(void)
3425{
3426 int ret;
3427
Sagi Grimberg631af552015-01-25 19:09:50 +02003428 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3429 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003430 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003431 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003432 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003433 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003434 }
3435
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003436 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3437 WQ_UNBOUND_MAX_ACTIVE);
3438 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003439 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003440 ret = -ENOMEM;
3441 goto destroy_comp_wq;
3442 }
3443
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003444 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003445 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003446
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003447 return 0;
3448
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003449destroy_comp_wq:
3450 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003451
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003452 return ret;
3453}
3454
3455static void __exit isert_exit(void)
3456{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003457 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003458 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003459 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003460 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003461 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003462}
3463
3464MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003465MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003466MODULE_AUTHOR("nab@Linux-iSCSI.org");
3467MODULE_LICENSE("GPL");
3468
3469module_init(isert_init);
3470module_exit(isert_exit);