blob: d99a0c8f14a422225c87afa1c19e59f1c0fa326a [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
32#include "isert_proto.h"
33#include "ib_isert.h"
34
35#define ISERT_MAX_CONN 8
36#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020038#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39 ISERT_MAX_CONN)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080040
Sagi Grimberg45678b62015-01-25 19:11:19 +020041static int isert_debug_level;
Sagi Grimberg24f412d2014-12-07 13:12:02 +020042module_param_named(debug_level, isert_debug_level, int, 0644);
43MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
44
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080045static DEFINE_MUTEX(device_list_mutex);
46static LIST_HEAD(device_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080047static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +020048static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080049
50static void
Vu Phamd40945d2013-08-28 23:23:34 +030051isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52static int
53isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030055static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020056isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030057static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020058isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
59 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020060static int
61isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020062static int
63isert_rdma_post_recvl(struct isert_conn *isert_conn);
64static int
65isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020066struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030067
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020068static inline bool
69isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
70{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020071 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020072 cmd->prot_op != TARGET_PROT_NORMAL);
73}
74
75
Vu Phamd40945d2013-08-28 23:23:34 +030076static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080077isert_qp_event_callback(struct ib_event *e, void *context)
78{
Sagi Grimberg67004252015-03-29 15:52:09 +030079 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080080
Sagi Grimbergea8a1612015-05-18 13:40:31 +030081 isert_err("%s (%d): conn %p\n",
82 ib_event_msg(e->event), e->event, isert_conn);
83
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080084 switch (e->event) {
85 case IB_EVENT_COMM_EST:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030086 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080087 break;
88 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020089 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080090 break;
91 default:
92 break;
93 }
94}
95
96static int
97isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
98{
99 int ret;
100
101 ret = ib_query_device(ib_dev, devattr);
102 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200103 isert_err("ib_query_device() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800104 return ret;
105 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200106 isert_dbg("devattr->max_sge: %d\n", devattr->max_sge);
107 isert_dbg("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800108
109 return 0;
110}
111
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300112static struct isert_comp *
113isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800114{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300115 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200116 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300117 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800118
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800119 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200120 for (i = 0; i < device->comps_used; i++)
121 if (device->comps[i].active_qps <
122 device->comps[min].active_qps)
123 min = i;
124 comp = &device->comps[min];
125 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300126 mutex_unlock(&device_list_mutex);
127
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200128 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200129 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300130
131 return comp;
132}
133
134static void
135isert_comp_put(struct isert_comp *comp)
136{
137 mutex_lock(&device_list_mutex);
138 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800139 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300140}
141
142static struct ib_qp *
143isert_create_qp(struct isert_conn *isert_conn,
144 struct isert_comp *comp,
145 struct rdma_cm_id *cma_id)
146{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300147 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300148 struct ib_qp_init_attr attr;
149 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800150
151 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
152 attr.event_handler = isert_qp_event_callback;
153 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200154 attr.send_cq = comp->cq;
155 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800156 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200157 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800158 /*
159 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700160 * work-around for RDMA_READs with ConnectX-2.
161 *
162 * Also, still make sure to have at least two SGEs for
163 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800164 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700165 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800166 isert_conn->max_sge = attr.cap.max_send_sge;
167
168 attr.cap.max_recv_sge = 1;
169 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
170 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200171 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200172 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800173
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300174 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800175 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200176 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300177 return ERR_PTR(ret);
178 }
179
180 return cma_id->qp;
181}
182
183static int
184isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
185{
186 struct isert_comp *comp;
187 int ret;
188
189 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300190 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
191 if (IS_ERR(isert_conn->qp)) {
192 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200193 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800194 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800195
196 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200197err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300198 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200199 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800200}
201
202static void
203isert_cq_event_callback(struct ib_event *e, void *context)
204{
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200205 isert_dbg("event: %d\n", e->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800206}
207
208static int
209isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
210{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300211 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300212 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800213 struct iser_rx_desc *rx_desc;
214 struct ib_sge *rx_sg;
215 u64 dma_addr;
216 int i, j;
217
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300218 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800219 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300220 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800221 goto fail;
222
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300223 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800224
225 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
226 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
227 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
228 if (ib_dma_mapping_error(ib_dev, dma_addr))
229 goto dma_map_fail;
230
231 rx_desc->dma_addr = dma_addr;
232
233 rx_sg = &rx_desc->rx_sg;
234 rx_sg->addr = rx_desc->dma_addr;
235 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300236 rx_sg->lkey = device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800237 }
238
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300239 isert_conn->rx_desc_head = 0;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200240
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800241 return 0;
242
243dma_map_fail:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300244 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800245 for (j = 0; j < i; j++, rx_desc++) {
246 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
247 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
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 -0800251fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200252 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
253
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800254 return -ENOMEM;
255}
256
257static void
258isert_free_rx_descriptors(struct isert_conn *isert_conn)
259{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300260 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800261 struct iser_rx_desc *rx_desc;
262 int i;
263
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300264 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800265 return;
266
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300267 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800268 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
269 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
270 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
271 }
272
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300273 kfree(isert_conn->rx_descs);
274 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800275}
276
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200277static void isert_cq_work(struct work_struct *);
278static void isert_cq_callback(struct ib_cq *, void *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800279
Sagi Grimberg172369c2015-03-29 15:52:11 +0300280static void
281isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800282{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300283 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800284
Sagi Grimberg172369c2015-03-29 15:52:11 +0300285 for (i = 0; i < device->comps_used; i++) {
286 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300287
Sagi Grimberg172369c2015-03-29 15:52:11 +0300288 if (comp->cq) {
289 cancel_work_sync(&comp->work);
290 ib_destroy_cq(comp->cq);
291 }
Vu Pham59464ef2013-08-28 23:23:35 +0300292 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300293 kfree(device->comps);
294}
Vu Phamd40945d2013-08-28 23:23:34 +0300295
Sagi Grimberg172369c2015-03-29 15:52:11 +0300296static int
297isert_alloc_comps(struct isert_device *device,
298 struct ib_device_attr *attr)
299{
300 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200301
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200302 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300303 device->ib_device->num_comp_vectors));
304
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200305 isert_info("Using %d CQs, %s supports %d vectors support "
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200306 "Fast registration %d pi_capable %d\n",
307 device->comps_used, device->ib_device->name,
308 device->ib_device->num_comp_vectors, device->use_fastreg,
309 device->pi_capable);
310
311 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
312 GFP_KERNEL);
313 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200314 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800315 return -ENOMEM;
316 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800317
Sagi Grimberg172369c2015-03-29 15:52:11 +0300318 max_cqe = min(ISER_MAX_CQ_LEN, attr->max_cqe);
319
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200320 for (i = 0; i < device->comps_used; i++) {
321 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800322
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200323 comp->device = device;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200324 INIT_WORK(&comp->work, isert_cq_work);
325 comp->cq = ib_create_cq(device->ib_device,
326 isert_cq_callback,
327 isert_cq_event_callback,
328 (void *)comp,
329 max_cqe, i);
330 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300331 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200332 ret = PTR_ERR(comp->cq);
333 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800334 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800335 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800336
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200337 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Wei Yongjun94a71112013-10-29 09:56:34 +0800338 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800339 goto out_cq;
340 }
341
Sagi Grimberg172369c2015-03-29 15:52:11 +0300342 return 0;
343out_cq:
344 isert_free_comps(device);
345 return ret;
346}
347
348static int
349isert_create_device_ib_res(struct isert_device *device)
350{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300351 struct ib_device_attr *dev_attr;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300352 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300353
354 dev_attr = &device->dev_attr;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300355 ret = isert_query_device(device->ib_device, dev_attr);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300356 if (ret)
357 return ret;
358
359 /* asign function handlers */
360 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
361 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
362 device->use_fastreg = 1;
363 device->reg_rdma_mem = isert_reg_rdma;
364 device->unreg_rdma_mem = isert_unreg_rdma;
365 } else {
366 device->use_fastreg = 0;
367 device->reg_rdma_mem = isert_map_rdma;
368 device->unreg_rdma_mem = isert_unmap_cmd;
369 }
370
371 ret = isert_alloc_comps(device, dev_attr);
372 if (ret)
373 return ret;
374
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300375 device->pd = ib_alloc_pd(device->ib_device);
376 if (IS_ERR(device->pd)) {
377 ret = PTR_ERR(device->pd);
378 isert_err("failed to allocate pd, device %p, ret=%d\n",
379 device, ret);
380 goto out_cq;
381 }
382
383 device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE);
384 if (IS_ERR(device->mr)) {
385 ret = PTR_ERR(device->mr);
386 isert_err("failed to create dma mr, device %p, ret=%d\n",
387 device, ret);
388 goto out_mr;
389 }
390
Sagi Grimberg172369c2015-03-29 15:52:11 +0300391 /* Check signature cap */
392 device->pi_capable = dev_attr->device_cap_flags &
393 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300394
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800395 return 0;
396
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300397out_mr:
398 ib_dealloc_pd(device->pd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800399out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300400 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800401 return ret;
402}
403
404static void
405isert_free_device_ib_res(struct isert_device *device)
406{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200407 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800408
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300409 ib_dereg_mr(device->mr);
410 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300411 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800412}
413
414static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300415isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800416{
417 mutex_lock(&device_list_mutex);
418 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200419 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800420 if (!device->refcount) {
421 isert_free_device_ib_res(device);
422 list_del(&device->dev_node);
423 kfree(device);
424 }
425 mutex_unlock(&device_list_mutex);
426}
427
428static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300429isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800430{
431 struct isert_device *device;
432 int ret;
433
434 mutex_lock(&device_list_mutex);
435 list_for_each_entry(device, &device_list, dev_node) {
436 if (device->ib_device->node_guid == cma_id->device->node_guid) {
437 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200438 isert_info("Found iser device %p refcount %d\n",
439 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800440 mutex_unlock(&device_list_mutex);
441 return device;
442 }
443 }
444
445 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
446 if (!device) {
447 mutex_unlock(&device_list_mutex);
448 return ERR_PTR(-ENOMEM);
449 }
450
451 INIT_LIST_HEAD(&device->dev_node);
452
453 device->ib_device = cma_id->device;
454 ret = isert_create_device_ib_res(device);
455 if (ret) {
456 kfree(device);
457 mutex_unlock(&device_list_mutex);
458 return ERR_PTR(ret);
459 }
460
461 device->refcount++;
462 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200463 isert_info("Created a new iser device %p refcount %d\n",
464 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800465 mutex_unlock(&device_list_mutex);
466
467 return device;
468}
469
Vu Pham59464ef2013-08-28 23:23:35 +0300470static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200471isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300472{
473 struct fast_reg_descriptor *fr_desc, *tmp;
474 int i = 0;
475
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300476 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300477 return;
478
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200479 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300480
481 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300482 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300483 list_del(&fr_desc->list);
484 ib_free_fast_reg_page_list(fr_desc->data_frpl);
485 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200486 if (fr_desc->pi_ctx) {
487 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
488 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
489 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
490 kfree(fr_desc->pi_ctx);
491 }
Vu Pham59464ef2013-08-28 23:23:35 +0300492 kfree(fr_desc);
493 ++i;
494 }
495
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300496 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200497 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300498 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300499}
500
501static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200502isert_create_pi_ctx(struct fast_reg_descriptor *desc,
503 struct ib_device *device,
504 struct ib_pd *pd)
505{
506 struct ib_mr_init_attr mr_init_attr;
507 struct pi_context *pi_ctx;
508 int ret;
509
510 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
511 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200512 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200513 return -ENOMEM;
514 }
515
516 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
517 ISCSI_ISER_SG_TABLESIZE);
518 if (IS_ERR(pi_ctx->prot_frpl)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200519 isert_err("Failed to allocate prot frpl err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200520 PTR_ERR(pi_ctx->prot_frpl));
521 ret = PTR_ERR(pi_ctx->prot_frpl);
522 goto err_pi_ctx;
523 }
524
525 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
526 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200527 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200528 PTR_ERR(pi_ctx->prot_mr));
529 ret = PTR_ERR(pi_ctx->prot_mr);
530 goto err_prot_frpl;
531 }
532 desc->ind |= ISERT_PROT_KEY_VALID;
533
534 memset(&mr_init_attr, 0, sizeof(mr_init_attr));
535 mr_init_attr.max_reg_descriptors = 2;
536 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
537 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
538 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200539 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200540 PTR_ERR(pi_ctx->sig_mr));
541 ret = PTR_ERR(pi_ctx->sig_mr);
542 goto err_prot_mr;
543 }
544
545 desc->pi_ctx = pi_ctx;
546 desc->ind |= ISERT_SIG_KEY_VALID;
547 desc->ind &= ~ISERT_PROTECTED;
548
549 return 0;
550
551err_prot_mr:
552 ib_dereg_mr(desc->pi_ctx->prot_mr);
553err_prot_frpl:
554 ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
555err_pi_ctx:
556 kfree(desc->pi_ctx);
557
558 return ret;
559}
560
561static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200562isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200563 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200564{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200565 int ret;
566
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200567 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
568 ISCSI_ISER_SG_TABLESIZE);
569 if (IS_ERR(fr_desc->data_frpl)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200570 isert_err("Failed to allocate data frpl err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200571 PTR_ERR(fr_desc->data_frpl));
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200572 return PTR_ERR(fr_desc->data_frpl);
573 }
574
575 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
576 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200577 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200578 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200579 ret = PTR_ERR(fr_desc->data_mr);
580 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200581 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200582 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200583
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200584 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200585
586 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +0200587
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200588err_data_frpl:
589 ib_free_fast_reg_page_list(fr_desc->data_frpl);
590
591 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200592}
593
594static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200595isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300596{
597 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300598 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700599 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
600 struct se_node_acl *se_nacl = se_sess->se_node_acl;
601 int i, ret, tag_num;
602 /*
603 * Setup the number of FRMRs based upon the number of tags
604 * available to session in iscsi_target_locate_portal().
605 */
606 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
607 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300608
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300609 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700610 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300611 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
612 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200613 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300614 ret = -ENOMEM;
615 goto err;
616 }
617
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200618 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300619 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200620 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200621 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200622 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800623 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300624 goto err;
625 }
626
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300627 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
628 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300629 }
630
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200631 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300632 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300633
634 return 0;
635
636err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200637 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300638 return ret;
639}
640
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300641static void
642isert_init_conn(struct isert_conn *isert_conn)
643{
644 isert_conn->state = ISER_CONN_INIT;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300645 INIT_LIST_HEAD(&isert_conn->accept_node);
646 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300647 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300648 init_completion(&isert_conn->wait);
649 kref_init(&isert_conn->kref);
650 mutex_init(&isert_conn->mutex);
651 spin_lock_init(&isert_conn->pool_lock);
652 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300653}
654
655static void
656isert_free_login_buf(struct isert_conn *isert_conn)
657{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300658 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300659
660 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
661 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
662 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
663 ISCSI_DEF_MAX_RECV_SEG_LEN,
664 DMA_FROM_DEVICE);
665 kfree(isert_conn->login_buf);
666}
667
668static int
669isert_alloc_login_buf(struct isert_conn *isert_conn,
670 struct ib_device *ib_dev)
671{
672 int ret;
673
674 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
675 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
676 if (!isert_conn->login_buf) {
677 isert_err("Unable to allocate isert_conn->login_buf\n");
678 return -ENOMEM;
679 }
680
681 isert_conn->login_req_buf = isert_conn->login_buf;
682 isert_conn->login_rsp_buf = isert_conn->login_buf +
683 ISCSI_DEF_MAX_RECV_SEG_LEN;
684
685 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
686 isert_conn->login_buf, isert_conn->login_req_buf,
687 isert_conn->login_rsp_buf);
688
689 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
690 (void *)isert_conn->login_req_buf,
691 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
692
693 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
694 if (ret) {
695 isert_err("login_req_dma mapping error: %d\n", ret);
696 isert_conn->login_req_dma = 0;
697 goto out_login_buf;
698 }
699
700 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
701 (void *)isert_conn->login_rsp_buf,
702 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
703
704 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
705 if (ret) {
706 isert_err("login_rsp_dma mapping error: %d\n", ret);
707 isert_conn->login_rsp_dma = 0;
708 goto out_req_dma_map;
709 }
710
711 return 0;
712
713out_req_dma_map:
714 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
715 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
716out_login_buf:
717 kfree(isert_conn->login_buf);
718 return ret;
719}
720
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800721static int
722isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
723{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200724 struct isert_np *isert_np = cma_id->context;
725 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800726 struct isert_conn *isert_conn;
727 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800728 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300729
730 spin_lock_bh(&np->np_thread_lock);
731 if (!np->enabled) {
732 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200733 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300734 return rdma_reject(cma_id, NULL, 0);
735 }
736 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800737
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200738 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800739 cma_id, cma_id->context);
740
741 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300742 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800743 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800744
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300745 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300746 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800747
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300748 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
749 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800750 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800751
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300752 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800753 if (IS_ERR(device)) {
754 ret = PTR_ERR(device);
755 goto out_rsp_dma_map;
756 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300757 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800758
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300759 /* Set max inflight RDMA READ requests */
760 isert_conn->initiator_depth = min_t(u8,
761 event->param.conn.initiator_depth,
762 device->dev_attr.max_qp_init_rd_atom);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200763 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300764
Sagi Grimberg570db172014-12-02 16:57:31 +0200765 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800766 if (ret)
767 goto out_conn_dev;
768
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200769 ret = isert_rdma_post_recvl(isert_conn);
770 if (ret)
771 goto out_conn_dev;
772
773 ret = isert_rdma_accept(isert_conn);
774 if (ret)
775 goto out_conn_dev;
776
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800777 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300778 list_add_tail(&isert_conn->accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800779 mutex_unlock(&isert_np->np_accept_mutex);
780
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200781 isert_info("np %p: Allow accept_np to continue\n", np);
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300782 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800783 return 0;
784
785out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300786 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800787out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300788 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800789out:
790 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200791 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800792 return ret;
793}
794
795static void
796isert_connect_release(struct isert_conn *isert_conn)
797{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300798 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800799
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200800 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800801
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300802 BUG_ON(!device);
803
804 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200805 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300806
Sagi Grimberg19e20902014-12-02 16:57:26 +0200807 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300808 if (isert_conn->cm_id)
809 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200810
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300811 if (isert_conn->qp) {
812 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200813
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300814 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300815 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800816 }
817
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300818 if (isert_conn->login_buf)
819 isert_free_login_buf(isert_conn);
820
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300821 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800822
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300823 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800824}
825
826static void
827isert_connected_handler(struct rdma_cm_id *cma_id)
828{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200829 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300830
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200831 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200832
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300833 if (!kref_get_unless_zero(&isert_conn->kref)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200834 isert_warn("conn %p connect_release is running\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200835 return;
836 }
837
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300838 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200839 if (isert_conn->state != ISER_CONN_FULL_FEATURE)
840 isert_conn->state = ISER_CONN_UP;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300841 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800842}
843
844static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300845isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800846{
847 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300848 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800849
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200850 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
851 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800852
853 isert_connect_release(isert_conn);
854}
855
856static void
857isert_put_conn(struct isert_conn *isert_conn)
858{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300859 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800860}
861
Sagi Grimberg954f2372014-12-02 16:57:17 +0200862/**
863 * isert_conn_terminate() - Initiate connection termination
864 * @isert_conn: isert connection struct
865 *
866 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200867 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200868 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200869 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200870 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300871 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200872 * safe to call multiple times.
873 */
874static void
875isert_conn_terminate(struct isert_conn *isert_conn)
876{
877 int err;
878
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200879 switch (isert_conn->state) {
880 case ISER_CONN_TERMINATING:
881 break;
882 case ISER_CONN_UP:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200883 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200884 isert_info("Terminating conn %p state %d\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200885 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200886 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300887 err = rdma_disconnect(isert_conn->cm_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200888 if (err)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200889 isert_warn("Failed rdma_disconnect isert_conn %p\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200890 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200891 break;
892 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200893 isert_warn("conn %p teminating in state %d\n",
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200894 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200895 }
896}
897
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700898static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200899isert_np_cma_handler(struct isert_np *isert_np,
900 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800901{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300902 isert_dbg("%s (%d): isert np %p\n",
903 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200904
905 switch (event) {
906 case RDMA_CM_EVENT_DEVICE_REMOVAL:
907 isert_np->np_cm_id = NULL;
908 break;
909 case RDMA_CM_EVENT_ADDR_CHANGE:
910 isert_np->np_cm_id = isert_setup_id(isert_np);
911 if (IS_ERR(isert_np->np_cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200912 isert_err("isert np %p setup id failed: %ld\n",
913 isert_np, PTR_ERR(isert_np->np_cm_id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200914 isert_np->np_cm_id = NULL;
915 }
916 break;
917 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200918 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200919 isert_np, event);
920 }
921
922 return -1;
923}
924
925static int
926isert_disconnected_handler(struct rdma_cm_id *cma_id,
927 enum rdma_cm_event_type event)
928{
929 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700930 struct isert_conn *isert_conn;
931
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200932 if (isert_np->np_cm_id == cma_id)
933 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700934
Sagi Grimberg19e20902014-12-02 16:57:26 +0200935 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800936
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300937 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200938 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300939 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200940
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300941 isert_info("conn %p completing wait\n", isert_conn);
942 complete(&isert_conn->wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700943
944 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800945}
946
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300947static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200948isert_connect_error(struct rdma_cm_id *cma_id)
949{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200950 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200951
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300952 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200953 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300954
955 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200956}
957
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800958static int
959isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
960{
961 int ret = 0;
962
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300963 isert_info("%s (%d): status %d id %p np %p\n",
964 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200965 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800966
967 switch (event->event) {
968 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800969 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700970 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200971 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800972 break;
973 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800974 isert_connected_handler(cma_id);
975 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300976 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
977 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
978 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300979 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200980 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800981 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200982 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
983 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800984 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300985 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200986 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800987 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200988 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800989 break;
990 }
991
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800992 return ret;
993}
994
995static int
996isert_post_recv(struct isert_conn *isert_conn, u32 count)
997{
998 struct ib_recv_wr *rx_wr, *rx_wr_failed;
999 int i, ret;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001000 unsigned int rx_head = isert_conn->rx_desc_head;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001001 struct iser_rx_desc *rx_desc;
1002
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001003 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
1004 rx_desc = &isert_conn->rx_descs[rx_head];
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001005 rx_wr->wr_id = (uintptr_t)rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001006 rx_wr->sg_list = &rx_desc->rx_sg;
1007 rx_wr->num_sge = 1;
1008 rx_wr->next = rx_wr + 1;
1009 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
1010 }
1011
1012 rx_wr--;
1013 rx_wr->next = NULL; /* mark end of work requests list */
1014
1015 isert_conn->post_recv_buf_count += count;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001016 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001017 &rx_wr_failed);
1018 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001019 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001020 isert_conn->post_recv_buf_count -= count;
1021 } else {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001022 isert_dbg("Posted %d RX buffers\n", count);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001023 isert_conn->rx_desc_head = rx_head;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001024 }
1025 return ret;
1026}
1027
1028static int
1029isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1030{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001031 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001032 struct ib_send_wr send_wr, *send_wr_failed;
1033 int ret;
1034
1035 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1036 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1037
1038 send_wr.next = NULL;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001039 send_wr.wr_id = (uintptr_t)tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001040 send_wr.sg_list = tx_desc->tx_sg;
1041 send_wr.num_sge = tx_desc->num_sge;
1042 send_wr.opcode = IB_WR_SEND;
1043 send_wr.send_flags = IB_SEND_SIGNALED;
1044
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001045 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001046 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001047 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001048
1049 return ret;
1050}
1051
1052static void
1053isert_create_send_desc(struct isert_conn *isert_conn,
1054 struct isert_cmd *isert_cmd,
1055 struct iser_tx_desc *tx_desc)
1056{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001057 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001058 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001059
1060 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1061 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1062
1063 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1064 tx_desc->iser_header.flags = ISER_VER;
1065
1066 tx_desc->num_sge = 1;
1067 tx_desc->isert_cmd = isert_cmd;
1068
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001069 if (tx_desc->tx_sg[0].lkey != device->mr->lkey) {
1070 tx_desc->tx_sg[0].lkey = device->mr->lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001071 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001072 }
1073}
1074
1075static int
1076isert_init_tx_hdrs(struct isert_conn *isert_conn,
1077 struct iser_tx_desc *tx_desc)
1078{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001079 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001080 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001081 u64 dma_addr;
1082
1083 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1084 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1085 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001086 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001087 return -ENOMEM;
1088 }
1089
1090 tx_desc->dma_addr = dma_addr;
1091 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1092 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001093 tx_desc->tx_sg[0].lkey = device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001094
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001095 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1096 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1097 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001098
1099 return 0;
1100}
1101
1102static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001103isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001104 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001105{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001106 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1107
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001108 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001109 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001110 send_wr->opcode = IB_WR_SEND;
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;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001126 sge.lkey = isert_conn->device->mr->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;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001176 tx_dsg->lkey = isert_conn->device->mr->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
1195 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1196 if (ret)
1197 return ret;
1198
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001199 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001200 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001201 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001202 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001203 goto post_send;
1204 }
1205
1206 ret = isert_rdma_post_recvl(isert_conn);
1207 if (ret)
1208 return ret;
1209 }
1210post_send:
1211 ret = isert_post_send(isert_conn, tx_desc);
1212 if (ret)
1213 return ret;
1214
1215 return 0;
1216}
1217
1218static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001219isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001220{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001221 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1222 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001223 struct iscsi_conn *conn = isert_conn->conn;
1224 struct iscsi_login *login = conn->conn_login;
1225 int size;
1226
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001227 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001228
1229 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001230
1231 if (login->first_request) {
1232 struct iscsi_login_req *login_req =
1233 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1234 /*
1235 * Setup the initial iscsi_login values from the leading
1236 * login request PDU.
1237 */
1238 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1239 login->current_stage =
1240 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1241 >> 2;
1242 login->version_min = login_req->min_version;
1243 login->version_max = login_req->max_version;
1244 memcpy(login->isid, login_req->isid, 6);
1245 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1246 login->init_task_tag = login_req->itt;
1247 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1248 login->cid = be16_to_cpu(login_req->cid);
1249 login->tsih = be16_to_cpu(login_req->tsih);
1250 }
1251
1252 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1253
1254 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001255 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1256 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1257 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001258 memcpy(login->req_buf, &rx_desc->data[0], size);
1259
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001260 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001261 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001262 return;
1263 }
1264 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001265}
1266
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001267static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001268*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001269{
Sagi Grimberg67004252015-03-29 15:52:09 +03001270 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001271 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001272 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001273
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001274 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001275 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001276 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001277 return NULL;
1278 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001279 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001280 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001281 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001282
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001283 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001284}
1285
1286static int
1287isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001288 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1289 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001290{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001291 struct iscsi_conn *conn = isert_conn->conn;
1292 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1293 struct scatterlist *sg;
1294 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1295 bool dump_payload = false;
1296
1297 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1298 if (rc < 0)
1299 return rc;
1300
1301 imm_data = cmd->immediate_data;
1302 imm_data_len = cmd->first_burst_len;
1303 unsol_data = cmd->unsolicited_data;
1304
1305 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1306 if (rc < 0) {
1307 return 0;
1308 } else if (rc > 0) {
1309 dump_payload = true;
1310 goto sequence_cmd;
1311 }
1312
1313 if (!imm_data)
1314 return 0;
1315
1316 sg = &cmd->se_cmd.t_data_sg[0];
1317 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1318
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001319 isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001320 sg, sg_nents, &rx_desc->data[0], imm_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001321
1322 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1323
1324 cmd->write_data_done += imm_data_len;
1325
1326 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1327 spin_lock_bh(&cmd->istate_lock);
1328 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1329 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1330 spin_unlock_bh(&cmd->istate_lock);
1331 }
1332
1333sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001334 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001335
1336 if (!rc && dump_payload == false && unsol_data)
1337 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001338 else if (dump_payload && imm_data)
1339 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001340
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001341 return 0;
1342}
1343
1344static int
1345isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1346 struct iser_rx_desc *rx_desc, unsigned char *buf)
1347{
1348 struct scatterlist *sg_start;
1349 struct iscsi_conn *conn = isert_conn->conn;
1350 struct iscsi_cmd *cmd = NULL;
1351 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1352 u32 unsol_data_len = ntoh24(hdr->dlength);
1353 int rc, sg_nents, sg_off, page_off;
1354
1355 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1356 if (rc < 0)
1357 return rc;
1358 else if (!cmd)
1359 return 0;
1360 /*
1361 * FIXME: Unexpected unsolicited_data out
1362 */
1363 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001364 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001365 dump_stack();
1366 return -1;
1367 }
1368
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001369 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1370 "write_data_done: %u, data_length: %u\n",
1371 unsol_data_len, cmd->write_data_done,
1372 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001373
1374 sg_off = cmd->write_data_done / PAGE_SIZE;
1375 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1376 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1377 page_off = cmd->write_data_done % PAGE_SIZE;
1378 /*
1379 * FIXME: Non page-aligned unsolicited_data out
1380 */
1381 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001382 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001383 dump_stack();
1384 return -1;
1385 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001386 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1387 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1388 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001389
1390 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1391 unsol_data_len);
1392
1393 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1394 if (rc < 0)
1395 return rc;
1396
1397 return 0;
1398}
1399
1400static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001401isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001402 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1403 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001404{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001405 struct iscsi_conn *conn = isert_conn->conn;
1406 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1407 int rc;
1408
1409 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1410 if (rc < 0)
1411 return rc;
1412 /*
1413 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1414 */
1415
1416 return iscsit_process_nop_out(conn, cmd, hdr);
1417}
1418
1419static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001420isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001421 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1422 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001423{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001424 struct iscsi_conn *conn = isert_conn->conn;
1425 u32 payload_length = ntoh24(hdr->dlength);
1426 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001427 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001428
1429 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1430 if (rc < 0)
1431 return rc;
1432
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001433 if (payload_length) {
1434 text_in = kzalloc(payload_length, GFP_KERNEL);
1435 if (!text_in) {
1436 isert_err("Unable to allocate text_in of payload_length: %u\n",
1437 payload_length);
1438 return -ENOMEM;
1439 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001440 }
1441 cmd->text_in_ptr = text_in;
1442
1443 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1444
1445 return iscsit_process_text_cmd(conn, cmd, hdr);
1446}
1447
1448static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001449isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1450 uint32_t read_stag, uint64_t read_va,
1451 uint32_t write_stag, uint64_t write_va)
1452{
1453 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1454 struct iscsi_conn *conn = isert_conn->conn;
1455 struct iscsi_cmd *cmd;
1456 struct isert_cmd *isert_cmd;
1457 int ret = -EINVAL;
1458 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1459
Sagi Grimbergfb140272015-03-29 15:52:08 +03001460 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001461 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001462 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001463 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001464 return 0;
1465 }
1466
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001467 switch (opcode) {
1468 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001469 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001470 if (!cmd)
1471 break;
1472
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001473 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001474 isert_cmd->read_stag = read_stag;
1475 isert_cmd->read_va = read_va;
1476 isert_cmd->write_stag = write_stag;
1477 isert_cmd->write_va = write_va;
1478
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001479 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001480 rx_desc, (unsigned char *)hdr);
1481 break;
1482 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001483 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001484 if (!cmd)
1485 break;
1486
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001487 isert_cmd = iscsit_priv_cmd(cmd);
1488 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001489 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001490 break;
1491 case ISCSI_OP_SCSI_DATA_OUT:
1492 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1493 (unsigned char *)hdr);
1494 break;
1495 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001496 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001497 if (!cmd)
1498 break;
1499
1500 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1501 (unsigned char *)hdr);
1502 break;
1503 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001504 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001505 if (!cmd)
1506 break;
1507
1508 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001509 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001510 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001511 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) {
1512 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
1513 if (!cmd)
1514 break;
1515 } else {
1516 cmd = isert_allocate_cmd(conn);
1517 if (!cmd)
1518 break;
1519 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001520
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001521 isert_cmd = iscsit_priv_cmd(cmd);
1522 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001523 rx_desc, (struct iscsi_text *)hdr);
1524 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001525 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001526 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001527 dump_stack();
1528 break;
1529 }
1530
1531 return ret;
1532}
1533
1534static void
1535isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1536{
1537 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1538 uint64_t read_va = 0, write_va = 0;
1539 uint32_t read_stag = 0, write_stag = 0;
1540 int rc;
1541
1542 switch (iser_hdr->flags & 0xF0) {
1543 case ISCSI_CTRL:
1544 if (iser_hdr->flags & ISER_RSV) {
1545 read_stag = be32_to_cpu(iser_hdr->read_stag);
1546 read_va = be64_to_cpu(iser_hdr->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001547 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1548 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001549 }
1550 if (iser_hdr->flags & ISER_WSV) {
1551 write_stag = be32_to_cpu(iser_hdr->write_stag);
1552 write_va = be64_to_cpu(iser_hdr->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001553 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1554 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001555 }
1556
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001557 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001558 break;
1559 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001560 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001561 break;
1562 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001563 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001564 break;
1565 }
1566
1567 rc = isert_rx_opcode(isert_conn, rx_desc,
1568 read_stag, read_va, write_stag, write_va);
1569}
1570
1571static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001572isert_rcv_completion(struct iser_rx_desc *desc,
1573 struct isert_conn *isert_conn,
1574 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001575{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001576 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001577 struct iscsi_hdr *hdr;
1578 u64 rx_dma;
1579 int rx_buflen, outstanding;
1580
1581 if ((char *)desc == isert_conn->login_req_buf) {
1582 rx_dma = isert_conn->login_req_dma;
1583 rx_buflen = ISER_RX_LOGIN_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001584 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001585 rx_dma, rx_buflen);
1586 } else {
1587 rx_dma = desc->dma_addr;
1588 rx_buflen = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001589 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001590 rx_dma, rx_buflen);
1591 }
1592
1593 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1594
1595 hdr = &desc->iscsi_header;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001596 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001597 hdr->opcode, hdr->itt, hdr->flags,
1598 (int)(xfer_len - ISER_HEADERS_LEN));
1599
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001600 if ((char *)desc == isert_conn->login_req_buf) {
1601 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1602 if (isert_conn->conn) {
1603 struct iscsi_login *login = isert_conn->conn->conn_login;
1604
1605 if (login && !login->first_request)
1606 isert_rx_login_req(isert_conn);
1607 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001608 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001609 complete(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001610 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001611 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001612 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001613 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001614
1615 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1616 DMA_FROM_DEVICE);
1617
1618 isert_conn->post_recv_buf_count--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001619 isert_dbg("Decremented post_recv_buf_count: %d\n",
1620 isert_conn->post_recv_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001621
1622 if ((char *)desc == isert_conn->login_req_buf)
1623 return;
1624
1625 outstanding = isert_conn->post_recv_buf_count;
1626 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1627 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1628 ISERT_MIN_POSTED_RX);
1629 err = isert_post_recv(isert_conn, count);
1630 if (err) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001631 isert_err("isert_post_recv() count: %d failed, %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001632 count, err);
1633 }
1634 }
1635}
1636
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001637static int
1638isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1639 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1640 enum iser_ib_op_code op, struct isert_data_buf *data)
1641{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001642 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001643
1644 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1645 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1646
1647 data->len = length - offset;
1648 data->offset = offset;
1649 data->sg_off = data->offset / PAGE_SIZE;
1650
1651 data->sg = &sg[data->sg_off];
1652 data->nents = min_t(unsigned int, nents - data->sg_off,
1653 ISCSI_ISER_SG_TABLESIZE);
1654 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1655 PAGE_SIZE);
1656
1657 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1658 data->dma_dir);
1659 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001660 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001661 return -EINVAL;
1662 }
1663
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001664 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001665 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001666
1667 return 0;
1668}
1669
1670static void
1671isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1672{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001673 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001674
1675 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1676 memset(data, 0, sizeof(*data));
1677}
1678
1679
1680
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001681static void
1682isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1683{
1684 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001685
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001686 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001687
1688 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001689 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001690 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001691 }
1692
Vu Pham90ecc6e2013-08-28 23:23:33 +03001693 if (wr->send_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001694 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001695 kfree(wr->send_wr);
1696 wr->send_wr = NULL;
1697 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001698
Vu Pham90ecc6e2013-08-28 23:23:33 +03001699 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001700 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001701 kfree(wr->ib_sge);
1702 wr->ib_sge = NULL;
1703 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001704}
1705
1706static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001707isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001708{
1709 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001710
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001711 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001712
1713 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001714 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001715 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1716 isert_unmap_data_buf(isert_conn, &wr->prot);
1717 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1718 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001719 spin_lock_bh(&isert_conn->pool_lock);
1720 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1721 spin_unlock_bh(&isert_conn->pool_lock);
Vu Pham59464ef2013-08-28 23:23:35 +03001722 wr->fr_desc = NULL;
1723 }
1724
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001725 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001726 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001727 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001728 }
1729
1730 wr->ib_sge = NULL;
1731 wr->send_wr = NULL;
1732}
1733
1734static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001735isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001736{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001737 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001738 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001739 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001740 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001741 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001742
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001743 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001744
1745 switch (cmd->iscsi_opcode) {
1746 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001747 spin_lock_bh(&conn->cmd_lock);
1748 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001749 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001750 spin_unlock_bh(&conn->cmd_lock);
1751
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001752 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001753 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001754 /*
1755 * Check for special case during comp_err where
1756 * WRITE_PENDING has been handed off from core,
1757 * but requires an extra target_put_sess_cmd()
1758 * before transport_generic_free_cmd() below.
1759 */
1760 if (comp_err &&
1761 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1762 struct se_cmd *se_cmd = &cmd->se_cmd;
1763
1764 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1765 }
1766 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001767
Vu Phamd40945d2013-08-28 23:23:34 +03001768 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001769 transport_generic_free_cmd(&cmd->se_cmd, 0);
1770 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001771 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001772 spin_lock_bh(&conn->cmd_lock);
1773 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001774 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001775 spin_unlock_bh(&conn->cmd_lock);
1776
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001777 transport_generic_free_cmd(&cmd->se_cmd, 0);
1778 break;
1779 case ISCSI_OP_REJECT:
1780 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001781 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001782 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1783 /* If the continue bit is on, keep the command alive */
1784 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1785 break;
1786
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001787 spin_lock_bh(&conn->cmd_lock);
1788 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001789 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001790 spin_unlock_bh(&conn->cmd_lock);
1791
1792 /*
1793 * Handle special case for REJECT when iscsi_add_reject*() has
1794 * overwritten the original iscsi_opcode assignment, and the
1795 * associated cmd->se_cmd needs to be released.
1796 */
1797 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001798 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001799 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001800 transport_generic_free_cmd(&cmd->se_cmd, 0);
1801 break;
1802 }
1803 /*
1804 * Fall-through
1805 */
1806 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001807 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001808 break;
1809 }
1810}
1811
1812static void
1813isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1814{
1815 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001816 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001817 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1818 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1819 tx_desc->dma_addr = 0;
1820 }
1821}
1822
1823static void
1824isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001825 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001826{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001827 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001828 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001829 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1830 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1831 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001832 }
1833
1834 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001835 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001836}
1837
Sagi Grimberg96b79732014-03-17 12:52:18 +02001838static int
1839isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1840{
1841 struct ib_mr_status mr_status;
1842 int ret;
1843
1844 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1845 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001846 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001847 goto fail_mr_status;
1848 }
1849
1850 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1851 u64 sec_offset_err;
1852 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1853
1854 switch (mr_status.sig_err.err_type) {
1855 case IB_SIG_BAD_GUARD:
1856 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1857 break;
1858 case IB_SIG_BAD_REFTAG:
1859 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1860 break;
1861 case IB_SIG_BAD_APPTAG:
1862 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1863 break;
1864 }
1865 sec_offset_err = mr_status.sig_err.sig_err_offset;
1866 do_div(sec_offset_err, block_size);
1867 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1868
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001869 isert_err("PI error found type %d at sector 0x%llx "
1870 "expected 0x%x vs actual 0x%x\n",
1871 mr_status.sig_err.err_type,
1872 (unsigned long long)se_cmd->bad_sector,
1873 mr_status.sig_err.expected,
1874 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001875 ret = 1;
1876 }
1877
1878fail_mr_status:
1879 return ret;
1880}
1881
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001882static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001883isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1884 struct isert_cmd *isert_cmd)
1885{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001886 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001887 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001888 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001889 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001890 struct isert_device *device = isert_conn->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001891 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001892
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001893 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001894 ret = isert_check_pi_status(se_cmd,
1895 wr->fr_desc->pi_ctx->sig_mr);
1896 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001897 }
1898
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001899 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001900 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001901 if (ret)
1902 transport_send_check_condition_and_sense(se_cmd,
1903 se_cmd->pi_err, 0);
1904 else
1905 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001906}
1907
1908static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001909isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1910 struct isert_cmd *isert_cmd)
1911{
1912 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001913 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001914 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001915 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001916 struct isert_device *device = isert_conn->device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001917 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001918
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001919 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001920 ret = isert_check_pi_status(se_cmd,
1921 wr->fr_desc->pi_ctx->sig_mr);
1922 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001923 }
1924
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001925 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001926 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001927 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001928 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001929
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001930 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001931 spin_lock_bh(&cmd->istate_lock);
1932 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1933 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1934 spin_unlock_bh(&cmd->istate_lock);
1935
Sagi Grimberg364189f2015-03-29 15:52:03 +03001936 if (ret) {
1937 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001938 transport_send_check_condition_and_sense(se_cmd,
1939 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001940 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001941 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001942 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001943}
1944
1945static void
1946isert_do_control_comp(struct work_struct *work)
1947{
1948 struct isert_cmd *isert_cmd = container_of(work,
1949 struct isert_cmd, comp_work);
1950 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001951 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001952 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001953
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001954 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1955
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001956 switch (cmd->i_state) {
1957 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001958 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001959 case ISTATE_SEND_REJECT: /* FALLTHRU */
1960 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001961 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001962 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1963 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001964 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001965 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001966 iscsit_logout_post_handler(cmd, cmd->conn);
1967 break;
1968 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001969 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001970 dump_stack();
1971 break;
1972 }
1973}
1974
1975static void
1976isert_response_completion(struct iser_tx_desc *tx_desc,
1977 struct isert_cmd *isert_cmd,
1978 struct isert_conn *isert_conn,
1979 struct ib_device *ib_dev)
1980{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001981 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001982
1983 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001984 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001985 cmd->i_state == ISTATE_SEND_REJECT ||
1986 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001987 isert_unmap_tx_desc(tx_desc, ib_dev);
1988
1989 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1990 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1991 return;
1992 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001993
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001994 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001995 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001996}
1997
1998static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001999isert_snd_completion(struct iser_tx_desc *tx_desc,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002000 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002001{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002002 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002003 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2004 struct isert_rdma_wr *wr;
2005
2006 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002007 isert_unmap_tx_desc(tx_desc, ib_dev);
2008 return;
2009 }
2010 wr = &isert_cmd->rdma_wr;
2011
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002012 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
2013
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002014 switch (wr->iser_ib_op) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002015 case ISER_IB_SEND:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002016 isert_response_completion(tx_desc, isert_cmd,
2017 isert_conn, ib_dev);
2018 break;
2019 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002020 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002021 break;
2022 case ISER_IB_RDMA_READ:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002023 isert_completion_rdma_read(tx_desc, isert_cmd);
2024 break;
2025 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002026 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002027 dump_stack();
2028 break;
2029 }
2030}
2031
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002032/**
2033 * is_isert_tx_desc() - Indicate if the completion wr_id
2034 * is a TX descriptor or not.
2035 * @isert_conn: iser connection
2036 * @wr_id: completion WR identifier
2037 *
2038 * Since we cannot rely on wc opcode in FLUSH errors
2039 * we must work around it by checking if the wr_id address
2040 * falls in the iser connection rx_descs buffer. If so
2041 * it is an RX descriptor, otherwize it is a TX.
2042 */
2043static inline bool
2044is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002045{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002046 void *start = isert_conn->rx_descs;
2047 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002048
2049 if (wr_id >= start && wr_id < start + len)
2050 return false;
2051
2052 return true;
2053}
2054
2055static void
2056isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
2057{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002058 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002059 isert_info("conn %p completing wait_comp_err\n",
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002060 isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002061 complete(&isert_conn->wait_comp_err);
Nicholas Bellingered4520a2014-12-07 13:12:05 +02002062 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002063 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002064 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002065 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002066
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002067 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2068 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002069 if (!isert_cmd)
2070 isert_unmap_tx_desc(desc, ib_dev);
2071 else
2072 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002073 } else {
2074 isert_conn->post_recv_buf_count--;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002075 if (!isert_conn->post_recv_buf_count)
2076 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002077 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002078}
2079
2080static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002081isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002082{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002083 struct isert_conn *isert_conn;
2084 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002085 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002086
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002087 isert_conn = wc->qp->qp_context;
2088 if (likely(wc->status == IB_WC_SUCCESS)) {
2089 if (wc->opcode == IB_WC_RECV) {
2090 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002091 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002092 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002093 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002094 isert_snd_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002095 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002096 } else {
2097 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002098 isert_err("%s (%d): wr id %llx vend_err %x\n",
2099 ib_wc_status_msg(wc->status), wc->status,
2100 wc->wr_id, wc->vendor_err);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002101 else
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002102 isert_dbg("%s (%d): wr id %llx\n",
2103 ib_wc_status_msg(wc->status), wc->status,
2104 wc->wr_id);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002105
2106 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2107 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002108 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002109}
2110
2111static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002112isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002113{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002114 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002115 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002116 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002117 struct ib_wc *const wcs = comp->wcs;
2118 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002119
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002120 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2121 for (i = 0; i < n; i++)
2122 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002123
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002124 completed += n;
2125 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002126 break;
2127 }
2128
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002129 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002130}
2131
2132static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002133isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002134{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002135 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002136
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002137 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002138}
2139
2140static int
2141isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2142{
2143 struct ib_send_wr *wr_failed;
2144 int ret;
2145
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002146 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002147 &wr_failed);
2148 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002149 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002150 return ret;
2151 }
2152 return ret;
2153}
2154
2155static int
2156isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2157{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002158 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002159 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002160 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2161 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2162 &isert_cmd->tx_desc.iscsi_header;
2163
2164 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2165 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2166 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2167 /*
2168 * Attach SENSE DATA payload to iSCSI Response PDU
2169 */
2170 if (cmd->se_cmd.sense_buffer &&
2171 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2172 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002173 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002174 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002175 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002176 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002177
2178 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2179 cmd->sense_buffer);
2180 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2181
2182 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2183 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002184 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002185
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002186 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2187 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002188 DMA_TO_DEVICE);
2189
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002190 isert_cmd->pdu_buf_len = pdu_len;
2191 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2192 tx_dsg->length = pdu_len;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002193 tx_dsg->lkey = device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002194 isert_cmd->tx_desc.num_sge = 2;
2195 }
2196
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002197 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002198
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002199 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002200
2201 return isert_post_response(isert_conn, isert_cmd);
2202}
2203
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002204static void
2205isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2206{
2207 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002208 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002209 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002210
2211 spin_lock_bh(&conn->cmd_lock);
2212 if (!list_empty(&cmd->i_conn_node))
2213 list_del_init(&cmd->i_conn_node);
2214 spin_unlock_bh(&conn->cmd_lock);
2215
2216 if (cmd->data_direction == DMA_TO_DEVICE)
2217 iscsit_stop_dataout_timer(cmd);
2218
2219 device->unreg_rdma_mem(isert_cmd, isert_conn);
2220}
2221
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002222static enum target_prot_op
2223isert_get_sup_prot_ops(struct iscsi_conn *conn)
2224{
Sagi Grimberg67004252015-03-29 15:52:09 +03002225 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002226 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002227
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002228 if (conn->tpg->tpg_attrib.t10_pi) {
2229 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002230 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002231 isert_conn->pi_support = true;
2232 return TARGET_PROT_ALL;
2233 }
2234 }
2235
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002236 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002237 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002238
2239 return TARGET_PROT_NORMAL;
2240}
2241
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002242static int
2243isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2244 bool nopout_response)
2245{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002246 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002247 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002248 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2249
2250 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2251 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2252 &isert_cmd->tx_desc.iscsi_header,
2253 nopout_response);
2254 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002255 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002256
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002257 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002258
2259 return isert_post_response(isert_conn, isert_cmd);
2260}
2261
2262static int
2263isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2264{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002265 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002266 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002267 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2268
2269 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2270 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2271 &isert_cmd->tx_desc.iscsi_header);
2272 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002273 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002274
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002275 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002276
2277 return isert_post_response(isert_conn, isert_cmd);
2278}
2279
2280static int
2281isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2282{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002283 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002284 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002285 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2286
2287 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2288 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2289 &isert_cmd->tx_desc.iscsi_header);
2290 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002291 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002292
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002293 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002294
2295 return isert_post_response(isert_conn, isert_cmd);
2296}
2297
2298static int
2299isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2300{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002301 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002302 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002303 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002304 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002305 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002306 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2307 struct iscsi_reject *hdr =
2308 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002309
2310 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002311 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002312 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002313
2314 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002315 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002316 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2317 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002318 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2319 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002320 tx_dsg->length = ISCSI_HDR_LEN;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002321 tx_dsg->lkey = device->mr->lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002322 isert_cmd->tx_desc.num_sge = 2;
2323
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002324 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002325
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002326 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002327
2328 return isert_post_response(isert_conn, isert_cmd);
2329}
2330
2331static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002332isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2333{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002334 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002335 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002336 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2337 struct iscsi_text_rsp *hdr =
2338 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2339 u32 txt_rsp_len;
2340 int rc;
2341
2342 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002343 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002344 if (rc < 0)
2345 return rc;
2346
2347 txt_rsp_len = rc;
2348 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2349
2350 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002351 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002352 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002353 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2354 void *txt_rsp_buf = cmd->buf_ptr;
2355
2356 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2357 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2358
2359 isert_cmd->pdu_buf_len = txt_rsp_len;
2360 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2361 tx_dsg->length = txt_rsp_len;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002362 tx_dsg->lkey = device->mr->lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002363 isert_cmd->tx_desc.num_sge = 2;
2364 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002365 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002366
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002367 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002368
2369 return isert_post_response(isert_conn, isert_cmd);
2370}
2371
2372static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002373isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2374 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2375 u32 data_left, u32 offset)
2376{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002377 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002378 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002379 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002380 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002381 u32 sg_off, page_off;
2382 int i = 0, sg_nents;
2383
2384 sg_off = offset / PAGE_SIZE;
2385 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2386 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2387 page_off = offset % PAGE_SIZE;
2388
2389 send_wr->sg_list = ib_sge;
2390 send_wr->num_sge = sg_nents;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002391 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002392 /*
2393 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2394 */
2395 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002396 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2397 "page_off: %u\n",
2398 (unsigned long long)tmp_sg->dma_address,
2399 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002400
2401 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2402 ib_sge->length = min_t(u32, data_left,
2403 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002404 ib_sge->lkey = device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002405
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002406 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2407 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002408 page_off = 0;
2409 data_left -= ib_sge->length;
2410 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002411 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002412 }
2413
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002414 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002415 send_wr->sg_list, send_wr->num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002416
2417 return sg_nents;
2418}
2419
2420static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002421isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2422 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002423{
2424 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002425 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002426 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002427 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002428 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002429 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002430 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2431 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002432
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002433 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002434
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002435 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2436 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2437 se_cmd->t_data_nents, se_cmd->data_length,
2438 offset, wr->iser_ib_op, &wr->data);
2439 if (ret)
2440 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002441
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002442 data_left = data->len;
2443 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002444
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002445 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002446 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002447 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002448 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002449 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002450 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002451 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002452
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002453 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002454 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2455 GFP_KERNEL);
2456 if (!wr->send_wr) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002457 isert_dbg("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002458 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002459 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002460 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002461
2462 wr->isert_cmd = isert_cmd;
2463 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002464
2465 for (i = 0; i < wr->send_wr_num; i++) {
2466 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2467 data_len = min(data_left, rdma_write_max);
2468
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002469 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002470 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2471 send_wr->opcode = IB_WR_RDMA_WRITE;
2472 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2473 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2474 if (i + 1 == wr->send_wr_num)
2475 send_wr->next = &isert_cmd->tx_desc.send_wr;
2476 else
2477 send_wr->next = &wr->send_wr[i + 1];
2478 } else {
2479 send_wr->opcode = IB_WR_RDMA_READ;
2480 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2481 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2482 if (i + 1 == wr->send_wr_num)
2483 send_wr->send_flags = IB_SEND_SIGNALED;
2484 else
2485 send_wr->next = &wr->send_wr[i + 1];
2486 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002487
2488 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2489 send_wr, data_len, offset);
2490 ib_sge += ib_sge_cnt;
2491
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002492 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002493 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002494 data_left -= data_len;
2495 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002496
2497 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002498unmap_cmd:
2499 isert_unmap_data_buf(isert_conn, data);
2500
Vu Pham90ecc6e2013-08-28 23:23:33 +03002501 return ret;
2502}
2503
2504static int
Vu Pham59464ef2013-08-28 23:23:35 +03002505isert_map_fr_pagelist(struct ib_device *ib_dev,
2506 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2507{
2508 u64 start_addr, end_addr, page, chunk_start = 0;
2509 struct scatterlist *tmp_sg;
2510 int i = 0, new_chunk, last_ent, n_pages;
2511
2512 n_pages = 0;
2513 new_chunk = 1;
2514 last_ent = sg_nents - 1;
2515 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2516 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2517 if (new_chunk)
2518 chunk_start = start_addr;
2519 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2520
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002521 isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
2522 i, (unsigned long long)tmp_sg->dma_address,
2523 tmp_sg->length);
Vu Pham59464ef2013-08-28 23:23:35 +03002524
2525 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2526 new_chunk = 0;
2527 continue;
2528 }
2529 new_chunk = 1;
2530
2531 page = chunk_start & PAGE_MASK;
2532 do {
2533 fr_pl[n_pages++] = page;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002534 isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
2535 n_pages - 1, page);
Vu Pham59464ef2013-08-28 23:23:35 +03002536 page += PAGE_SIZE;
2537 } while (page < end_addr);
2538 }
2539
2540 return n_pages;
2541}
2542
Sagi Grimberg10633c32014-12-07 13:12:04 +02002543static inline void
2544isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2545{
2546 u32 rkey;
2547
2548 memset(inv_wr, 0, sizeof(*inv_wr));
2549 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2550 inv_wr->opcode = IB_WR_LOCAL_INV;
2551 inv_wr->ex.invalidate_rkey = mr->rkey;
2552
2553 /* Bump the key */
2554 rkey = ib_inc_rkey(mr->rkey);
2555 ib_update_fast_reg_key(mr, rkey);
2556}
2557
Vu Pham59464ef2013-08-28 23:23:35 +03002558static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002559isert_fast_reg_mr(struct isert_conn *isert_conn,
2560 struct fast_reg_descriptor *fr_desc,
2561 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002562 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002563 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002564{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002565 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002566 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002567 struct ib_mr *mr;
2568 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002569 struct ib_send_wr fr_wr, inv_wr;
2570 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002571 int ret, pagelist_len;
2572 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002573
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002574 if (mem->dma_nents == 1) {
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002575 sge->lkey = device->mr->lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002576 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2577 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002578 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2579 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002580 return 0;
2581 }
2582
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002583 if (ind == ISERT_DATA_KEY_VALID) {
2584 /* Registering data buffer */
2585 mr = fr_desc->data_mr;
2586 frpl = fr_desc->data_frpl;
2587 } else {
2588 /* Registering protection buffer */
2589 mr = fr_desc->pi_ctx->prot_mr;
2590 frpl = fr_desc->pi_ctx->prot_frpl;
2591 }
2592
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002593 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002594
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002595 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002596 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002597
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002598 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002599 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002600
Sagi Grimberg10633c32014-12-07 13:12:04 +02002601 if (!(fr_desc->ind & ind)) {
2602 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002603 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002604 }
2605
2606 /* Prepare FASTREG WR */
2607 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002608 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002609 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002610 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2611 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002612 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2613 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002614 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002615 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002616 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2617
2618 if (!wr)
2619 wr = &fr_wr;
2620 else
2621 wr->next = &fr_wr;
2622
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002623 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002624 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002625 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002626 return ret;
2627 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002628 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002629
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002630 sge->lkey = mr->lkey;
2631 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002632 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002633
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002634 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2635 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002636
2637 return ret;
2638}
2639
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002640static inline void
2641isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2642 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002643{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002644 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002645 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2646 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2647 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002648 /*
2649 * At the moment we hard code those, but if in the future
2650 * the target core would like to use it, we will take it
2651 * from se_cmd.
2652 */
2653 domain->sig.dif.apptag_check_mask = 0xffff;
2654 domain->sig.dif.app_escape = true;
2655 domain->sig.dif.ref_escape = true;
2656 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2657 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2658 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002659};
2660
2661static int
2662isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2663{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002664 switch (se_cmd->prot_op) {
2665 case TARGET_PROT_DIN_INSERT:
2666 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002667 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002668 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002669 break;
2670 case TARGET_PROT_DOUT_INSERT:
2671 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002672 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002673 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002674 break;
2675 case TARGET_PROT_DIN_PASS:
2676 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002677 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2678 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002679 break;
2680 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002681 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002682 return -EINVAL;
2683 }
2684
2685 return 0;
2686}
2687
2688static inline u8
2689isert_set_prot_checks(u8 prot_checks)
2690{
2691 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2692 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2693 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2694}
2695
2696static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002697isert_reg_sig_mr(struct isert_conn *isert_conn,
2698 struct se_cmd *se_cmd,
2699 struct isert_rdma_wr *rdma_wr,
2700 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002701{
2702 struct ib_send_wr sig_wr, inv_wr;
2703 struct ib_send_wr *bad_wr, *wr = NULL;
2704 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2705 struct ib_sig_attrs sig_attrs;
2706 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002707
2708 memset(&sig_attrs, 0, sizeof(sig_attrs));
2709 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2710 if (ret)
2711 goto err;
2712
2713 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2714
2715 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002716 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002717 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002718 }
2719
2720 memset(&sig_wr, 0, sizeof(sig_wr));
2721 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002722 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg570db172014-12-02 16:57:31 +02002723 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002724 sig_wr.num_sge = 1;
2725 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2726 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2727 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2728 if (se_cmd->t_prot_sg)
Sagi Grimberg570db172014-12-02 16:57:31 +02002729 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002730
2731 if (!wr)
2732 wr = &sig_wr;
2733 else
2734 wr->next = &sig_wr;
2735
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002736 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002737 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002738 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002739 goto err;
2740 }
2741 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2742
Sagi Grimberg570db172014-12-02 16:57:31 +02002743 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2744 rdma_wr->ib_sg[SIG].addr = 0;
2745 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002746 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2747 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2748 /*
2749 * We have protection guards on the wire
2750 * so we need to set a larget transfer
2751 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002752 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002753
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002754 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002755 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2756 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002757err:
2758 return ret;
2759}
2760
Vu Pham59464ef2013-08-28 23:23:35 +03002761static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002762isert_handle_prot_cmd(struct isert_conn *isert_conn,
2763 struct isert_cmd *isert_cmd,
2764 struct isert_rdma_wr *wr)
2765{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002766 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002767 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2768 int ret;
2769
2770 if (!wr->fr_desc->pi_ctx) {
2771 ret = isert_create_pi_ctx(wr->fr_desc,
2772 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002773 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002774 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002775 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002776 isert_conn);
2777 return ret;
2778 }
2779 }
2780
2781 if (se_cmd->t_prot_sg) {
2782 ret = isert_map_data_buf(isert_conn, isert_cmd,
2783 se_cmd->t_prot_sg,
2784 se_cmd->t_prot_nents,
2785 se_cmd->prot_length,
2786 0, wr->iser_ib_op, &wr->prot);
2787 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002788 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002789 isert_conn);
2790 return ret;
2791 }
2792
2793 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2794 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2795 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2796 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002797 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002798 isert_conn);
2799 goto unmap_prot_cmd;
2800 }
2801 }
2802
2803 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2804 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002805 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002806 isert_conn);
2807 goto unmap_prot_cmd;
2808 }
2809 wr->fr_desc->ind |= ISERT_PROTECTED;
2810
2811 return 0;
2812
2813unmap_prot_cmd:
2814 if (se_cmd->t_prot_sg)
2815 isert_unmap_data_buf(isert_conn, &wr->prot);
2816
2817 return ret;
2818}
2819
2820static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002821isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2822 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002823{
2824 struct se_cmd *se_cmd = &cmd->se_cmd;
2825 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002826 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002827 struct fast_reg_descriptor *fr_desc = NULL;
Sagi Grimberg570db172014-12-02 16:57:31 +02002828 struct ib_send_wr *send_wr;
2829 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002830 u32 offset;
2831 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002832 unsigned long flags;
2833
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002834 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2835
2836 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2837 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2838 se_cmd->t_data_nents, se_cmd->data_length,
2839 offset, wr->iser_ib_op, &wr->data);
2840 if (ret)
2841 return ret;
2842
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002843 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002844 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2845 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002846 struct fast_reg_descriptor, list);
2847 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002848 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002849 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002850 }
2851
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002852 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002853 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002854 if (ret)
2855 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002856
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002857 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002858 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002859 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002860 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002861
Sagi Grimberg570db172014-12-02 16:57:31 +02002862 ib_sg = &wr->ib_sg[SIG];
2863 } else {
2864 ib_sg = &wr->ib_sg[DATA];
2865 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002866
Sagi Grimberg570db172014-12-02 16:57:31 +02002867 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002868 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002869 wr->send_wr_num = 1;
2870 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2871 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002872 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002873
2874 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002875 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002876 send_wr->num_sge = 1;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002877 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002878 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2879 send_wr->opcode = IB_WR_RDMA_WRITE;
2880 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2881 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002882 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002883 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002884 } else {
2885 send_wr->opcode = IB_WR_RDMA_READ;
2886 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2887 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2888 send_wr->send_flags = IB_SEND_SIGNALED;
2889 }
2890
Vu Pham59464ef2013-08-28 23:23:35 +03002891 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002892
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002893unmap_cmd:
2894 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002895 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2896 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2897 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002898 }
2899 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002900
Vu Pham59464ef2013-08-28 23:23:35 +03002901 return ret;
2902}
2903
2904static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002905isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2906{
2907 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002908 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002909 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002910 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002911 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002912 struct ib_send_wr *wr_failed;
2913 int rc;
2914
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002915 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002916 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002917
Vu Pham90ecc6e2013-08-28 23:23:33 +03002918 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002919 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002920 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002921 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002922 return rc;
2923 }
2924
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002925 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002926 /*
2927 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2928 */
2929 isert_create_send_desc(isert_conn, isert_cmd,
2930 &isert_cmd->tx_desc);
2931 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2932 &isert_cmd->tx_desc.iscsi_header);
2933 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2934 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002935 &isert_cmd->tx_desc.send_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002936 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002937 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002938 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002939
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002940 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002941 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002942 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002943
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002944 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002945 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002946 "READ\n", isert_cmd);
2947 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002948 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002949 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002950
Vu Pham90ecc6e2013-08-28 23:23:33 +03002951 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002952}
2953
2954static int
2955isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2956{
2957 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002958 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002959 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002960 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002961 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002962 struct ib_send_wr *wr_failed;
2963 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002964
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002965 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002966 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002967 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002968 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002969 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002970 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002971 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002972 }
2973
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002974 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002975 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002976 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002977
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002978 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002979 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002980
Vu Pham90ecc6e2013-08-28 23:23:33 +03002981 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002982}
2983
2984static int
2985isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2986{
2987 int ret;
2988
2989 switch (state) {
2990 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2991 ret = isert_put_nopin(cmd, conn, false);
2992 break;
2993 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002994 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002995 ret = -EINVAL;
2996 break;
2997 }
2998
2999 return ret;
3000}
3001
3002static int
3003isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3004{
Sagi Grimberg991bb762014-12-07 13:12:01 +02003005 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003006 int ret;
3007
3008 switch (state) {
3009 case ISTATE_SEND_LOGOUTRSP:
3010 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003011 if (!ret)
3012 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003013 break;
3014 case ISTATE_SEND_NOPIN:
3015 ret = isert_put_nopin(cmd, conn, true);
3016 break;
3017 case ISTATE_SEND_TASKMGTRSP:
3018 ret = isert_put_tm_rsp(cmd, conn);
3019 break;
3020 case ISTATE_SEND_REJECT:
3021 ret = isert_put_reject(cmd, conn);
3022 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003023 case ISTATE_SEND_TEXTRSP:
3024 ret = isert_put_text_rsp(cmd, conn);
3025 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003026 case ISTATE_SEND_STATUS:
3027 /*
3028 * Special case for sending non GOOD SCSI status from TX thread
3029 * context during pre se_cmd excecution failure.
3030 */
3031 ret = isert_put_response(conn, cmd);
3032 break;
3033 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003034 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003035 ret = -EINVAL;
3036 break;
3037 }
3038
3039 return ret;
3040}
3041
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003042struct rdma_cm_id *
3043isert_setup_id(struct isert_np *isert_np)
3044{
3045 struct iscsi_np *np = isert_np->np;
3046 struct rdma_cm_id *id;
3047 struct sockaddr *sa;
3048 int ret;
3049
3050 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003051 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003052
3053 id = rdma_create_id(isert_cma_handler, isert_np,
3054 RDMA_PS_TCP, IB_QPT_RC);
3055 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003056 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003057 ret = PTR_ERR(id);
3058 goto out;
3059 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003060 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003061
3062 ret = rdma_bind_addr(id, sa);
3063 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003064 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003065 goto out_id;
3066 }
3067
Sagi Grimberg992607e2015-03-29 15:52:18 +03003068 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003069 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003070 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003071 goto out_id;
3072 }
3073
3074 return id;
3075out_id:
3076 rdma_destroy_id(id);
3077out:
3078 return ERR_PTR(ret);
3079}
3080
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003081static int
3082isert_setup_np(struct iscsi_np *np,
3083 struct __kernel_sockaddr_storage *ksockaddr)
3084{
3085 struct isert_np *isert_np;
3086 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003087 int ret;
3088
3089 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3090 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003091 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003092 return -ENOMEM;
3093 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003094 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003095 mutex_init(&isert_np->np_accept_mutex);
3096 INIT_LIST_HEAD(&isert_np->np_accept_list);
3097 init_completion(&isert_np->np_login_comp);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003098 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003099
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003100 /*
3101 * Setup the np->np_sockaddr from the passed sockaddr setup
3102 * in iscsi_target_configfs.c code..
3103 */
3104 memcpy(&np->np_sockaddr, ksockaddr,
3105 sizeof(struct __kernel_sockaddr_storage));
3106
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003107 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003108 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003109 ret = PTR_ERR(isert_lid);
3110 goto out;
3111 }
3112
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003113 isert_np->np_cm_id = isert_lid;
3114 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003115
3116 return 0;
3117
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003118out:
3119 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003120
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003121 return ret;
3122}
3123
3124static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003125isert_rdma_accept(struct isert_conn *isert_conn)
3126{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003127 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003128 struct rdma_conn_param cp;
3129 int ret;
3130
3131 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003132 cp.initiator_depth = isert_conn->initiator_depth;
3133 cp.retry_count = 7;
3134 cp.rnr_retry_count = 7;
3135
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003136 ret = rdma_accept(cm_id, &cp);
3137 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003138 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003139 return ret;
3140 }
3141
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003142 return 0;
3143}
3144
3145static int
3146isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3147{
Sagi Grimberg67004252015-03-29 15:52:09 +03003148 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003149 int ret;
3150
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003151 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003152 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3153 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003154 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003155 isert_conn);
3156 return ret;
3157 }
3158 reinit_completion(&isert_conn->login_req_comp);
3159
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003160 /*
3161 * For login requests after the first PDU, isert_rx_login_req() will
3162 * kick schedule_delayed_work(&conn->login_work) as the packet is
3163 * received, which turns this callback from iscsi_target_do_login_rx()
3164 * into a NOP.
3165 */
3166 if (!login->first_request)
3167 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003168
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003169 isert_rx_login_req(isert_conn);
3170
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003171 isert_info("before login_comp conn: %p\n", conn);
3172 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003173 if (ret)
3174 return ret;
3175
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003176 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003177
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003178 return 0;
3179}
3180
3181static void
3182isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3183 struct isert_conn *isert_conn)
3184{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003185 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003186 struct rdma_route *cm_route = &cm_id->route;
3187 struct sockaddr_in *sock_in;
3188 struct sockaddr_in6 *sock_in6;
3189
3190 conn->login_family = np->np_sockaddr.ss_family;
3191
3192 if (np->np_sockaddr.ss_family == AF_INET6) {
3193 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3194 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3195 &sock_in6->sin6_addr.in6_u);
3196 conn->login_port = ntohs(sock_in6->sin6_port);
3197
3198 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3199 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3200 &sock_in6->sin6_addr.in6_u);
3201 conn->local_port = ntohs(sock_in6->sin6_port);
3202 } else {
3203 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3204 sprintf(conn->login_ip, "%pI4",
3205 &sock_in->sin_addr.s_addr);
3206 conn->login_port = ntohs(sock_in->sin_port);
3207
3208 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3209 sprintf(conn->local_ip, "%pI4",
3210 &sock_in->sin_addr.s_addr);
3211 conn->local_port = ntohs(sock_in->sin_port);
3212 }
3213}
3214
3215static int
3216isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3217{
Sagi Grimberg67004252015-03-29 15:52:09 +03003218 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003219 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003220 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003221
3222accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003223 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003224 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003225 return -ENODEV;
3226
3227 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003228 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003229 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003230 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003231 np->np_thread_state);
3232 /**
3233 * No point in stalling here when np_thread
3234 * is in state RESET/SHUTDOWN/EXIT - bail
3235 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003236 return -ENODEV;
3237 }
3238 spin_unlock_bh(&np->np_thread_lock);
3239
3240 mutex_lock(&isert_np->np_accept_mutex);
3241 if (list_empty(&isert_np->np_accept_list)) {
3242 mutex_unlock(&isert_np->np_accept_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003243 goto accept_wait;
3244 }
3245 isert_conn = list_first_entry(&isert_np->np_accept_list,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003246 struct isert_conn, accept_node);
3247 list_del_init(&isert_conn->accept_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003248 mutex_unlock(&isert_np->np_accept_mutex);
3249
3250 conn->context = isert_conn;
3251 isert_conn->conn = conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003252
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003253 isert_set_conn_info(np, conn, isert_conn);
3254
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003255 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003256
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003257 return 0;
3258}
3259
3260static void
3261isert_free_np(struct iscsi_np *np)
3262{
Sagi Grimberg67004252015-03-29 15:52:09 +03003263 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003264 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003265
Sagi Grimberg3b726ae2014-10-28 13:45:03 -07003266 if (isert_np->np_cm_id)
3267 rdma_destroy_id(isert_np->np_cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003268
Sagi Grimberg268e6812014-12-02 16:57:36 +02003269 /*
3270 * FIXME: At this point we don't have a good way to insure
3271 * that at this point we don't have hanging connections that
3272 * completed RDMA establishment but didn't start iscsi login
3273 * process. So work-around this by cleaning up what ever piled
3274 * up in np_accept_list.
3275 */
3276 mutex_lock(&isert_np->np_accept_mutex);
3277 if (!list_empty(&isert_np->np_accept_list)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003278 isert_info("Still have isert connections, cleaning up...\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003279 list_for_each_entry_safe(isert_conn, n,
3280 &isert_np->np_accept_list,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003281 accept_node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003282 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003283 isert_conn, isert_conn->state);
3284 isert_connect_release(isert_conn);
3285 }
3286 }
3287 mutex_unlock(&isert_np->np_accept_mutex);
3288
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003289 np->np_context = NULL;
3290 kfree(isert_np);
3291}
3292
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003293static void isert_release_work(struct work_struct *work)
3294{
3295 struct isert_conn *isert_conn = container_of(work,
3296 struct isert_conn,
3297 release_work);
3298
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003299 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003300
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003301 wait_for_completion(&isert_conn->wait);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003302
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003303 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003304 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003305 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003306
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003307 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003308 isert_put_conn(isert_conn);
3309}
3310
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003311static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003312isert_wait4logout(struct isert_conn *isert_conn)
3313{
3314 struct iscsi_conn *conn = isert_conn->conn;
3315
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003316 isert_info("conn %p\n", isert_conn);
3317
Sagi Grimberg991bb762014-12-07 13:12:01 +02003318 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003319 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003320 wait_for_completion_timeout(&conn->conn_logout_comp,
3321 SECONDS_FOR_LOGOUT_COMP * HZ);
3322 }
3323}
3324
3325static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003326isert_wait4cmds(struct iscsi_conn *conn)
3327{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003328 isert_info("iscsi_conn %p\n", conn);
3329
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003330 if (conn->sess) {
3331 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3332 target_wait_for_sess_cmds(conn->sess->se_sess);
3333 }
3334}
3335
3336static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003337isert_wait4flush(struct isert_conn *isert_conn)
3338{
3339 struct ib_recv_wr *bad_wr;
3340
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003341 isert_info("conn %p\n", isert_conn);
3342
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003343 init_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003344 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3345 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003346 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003347 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003348 return;
3349 }
3350
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003351 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003352}
3353
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003354static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003355{
3356 struct isert_conn *isert_conn = conn->context;
3357
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003358 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003359
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003360 mutex_lock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003361 /*
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003362 * Only wait for wait_comp_err if the isert_conn made it
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003363 * into full feature phase..
3364 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003365 if (isert_conn->state == ISER_CONN_INIT) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003366 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003367 return;
3368 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003369 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003370 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003371
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003372 isert_wait4cmds(conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003373 isert_wait4flush(isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003374 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003375
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003376 INIT_WORK(&isert_conn->release_work, isert_release_work);
3377 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003378}
3379
3380static void isert_free_conn(struct iscsi_conn *conn)
3381{
3382 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003383
3384 isert_put_conn(isert_conn);
3385}
3386
3387static struct iscsit_transport iser_target_transport = {
3388 .name = "IB/iSER",
3389 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003390 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003391 .owner = THIS_MODULE,
3392 .iscsit_setup_np = isert_setup_np,
3393 .iscsit_accept_np = isert_accept_np,
3394 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003395 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003396 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003397 .iscsit_get_login_rx = isert_get_login_rx,
3398 .iscsit_put_login_tx = isert_put_login_tx,
3399 .iscsit_immediate_queue = isert_immediate_queue,
3400 .iscsit_response_queue = isert_response_queue,
3401 .iscsit_get_dataout = isert_get_dataout,
3402 .iscsit_queue_data_in = isert_put_datain,
3403 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003404 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003405 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003406};
3407
3408static int __init isert_init(void)
3409{
3410 int ret;
3411
Sagi Grimberg631af552015-01-25 19:09:50 +02003412 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3413 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003414 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003415 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003416 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003417 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003418 }
3419
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003420 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3421 WQ_UNBOUND_MAX_ACTIVE);
3422 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003423 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003424 ret = -ENOMEM;
3425 goto destroy_comp_wq;
3426 }
3427
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003428 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003429 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003430
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003431 return 0;
3432
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003433destroy_comp_wq:
3434 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003435
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003436 return ret;
3437}
3438
3439static void __exit isert_exit(void)
3440{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003441 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003442 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003443 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003444 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003445 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003446}
3447
3448MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003449MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003450MODULE_AUTHOR("nab@Linux-iSCSI.org");
3451MODULE_LICENSE("GPL");
3452
3453module_init(isert_init);
3454module_exit(isert_exit);